在上两篇博文(如何使用W7100A實現Telnet伺服器(一)、如何使用W7100A實現Telnet伺服器(二))里我们简单地介绍一下Telnet,第三章将会演示Telnet函数的具体功能,第四章将涉及到代码的分析。这篇文档中所有的示例代码都是基于Keil uVision3。
下面我们介绍第三部分(主要内容是第四章的余下的代码分析部分):
4.3 tel_input()函数
tel_input()函数用来处理Telnet终端机内的输入命令。具体每一个命令以及处理方法请参考表3.1。
void tel_input(SOCKET s) { uint8 xdata c; while(1){ if((getSn_RX_RSR(s)) == 0) break; /*如果没有接收到的数据,断开*/ if(recv(s, &c, 1) == 0) break; /*如果接收到的数据为0,断开*/ if(user_state == LOGOUT) break; /*如果用户的声明是LOGOUT, 断开*/ if(c != IAC){ /*如果接收到的数据不是控制字符*/ data_buf[buf_index++] = c; /*保存接收到的数据到data_buf*/ putchar(c); if(user_state == LOGOUT) break; if(user_state != PASSWORD){ sprintf(buf, "%c", c); send(s, buf, strlen(buf)); } if(c == '\n'){ /*如果接收到一个\n’ ASCII 代码*/ if(buf_index > 1){ if(data_buf[buf_index-2] == '\r') data_buf[buf_index-2] = '\0'; else data_buf[buf_index-1] = '\0'; proc_command(s); /* 处理接收到的数据*/ if(user_state == LOGIN) { sprintf(buf, "W7100>"); send(s, buf, strlen(buf)); } } else{ sprintf(buf, "W7100>"); send(s, buf, strlen(buf)); } buf_index = 0; } continue; } if(recv(s, &c, 1) == 0) break; switch(c){ /*如果接收到一个IAC字符*/ case WILL: if(recv(s, &c, 1) == 0) break; willopt(s, c); /*调用willopt()处理WILL命令*/ break; case WONT: if(recv(s, &c, 1) == 0) break; wontopt(s, c); /*调用wontopt()来处理WONT命令*/ break; case DO: if(recv(s, &c, 1) == 0) break; doopt(s, c); /*调用doopt()处理DO命令*/ break; case DONT: if(recv(s, &c, 1) == 0) break; dontopt(c); /*调用dontopt()处理DON’T命令*/ break; case IAC: break; } break; } return; } |
程序4.3parseMSG()函数
4.4 proc_command()函数
proc_command()函数处理在tel_input()函数中输入的命令。它定义了HELP、GET LED、LED0 ON/OFF和LED2 ON/OFF命令。对于未定义的命令它显示“BAD COMMAND”。
void proc_command(SOCKET s) { uint8 i; char **cmdp, *cp; char *help = {"HELP: Show all available commands\r\n\GET LED: Show all LED status\ \r\nLED0 ON/OFF: Turn ON/OFF the LED0\r\nLED1 ON/OFF: Turn ON/OFF the LED1\ \r\nLED2 ON/OFF: Turn ON/OFF the LED2\r\nEXIT: Exit from W7100 Telnet server\r\n"}; /*Translate the first word to lower case*/ for(cp = data_buf; *cp !='\0'; cp++){ *cp = tolower(*cp); /* 将大写字母翻译成小写字母*/ } if(user_state == USERNAME){ strcpy(user_name, data_buf); sprintf(buf, "Please insert your PW: "); send(s, buf, strlen(buf)); user_state = PASSWORD; return; } else if(user_state == PASSWORD){ strcpy(user_password, data_buf); sprintf(buf, "\r\nSuccessfully connected!!\r\nImplemented Command: \ HELP, GET LED, LED0 ON/OFF, LED1 ON/OFF, LED2 ON/OFF, EXIT\r\n"); send(s, buf, strlen(buf)); user_state = LOGIN; return; } /*查找表中的输入命令; 如果不表内,返回语法错误*/ for(cmdp = commands; *cmdp != NULL; cmdp++){ if(strncmp(*cmdp, data_buf, strlen(*cmdp)) == 0) break; } if(*cmdp == NULL){ printf("NULL command\r\n"); sprintf(buf, "BAD command\r\n"); send(s, buf, strlen(buf)); return; } switch(cmdp - commands){ case HELP_CMD: /* 处理HELP命令*/ printf("HELP_CMD\r\n"); sprintf(buf, help); send(s, buf, strlen(buf)); break; case GET_LED_CMD: /*处理GET LED 命令*/ printf("GET_LED_CMD\r\n"); for(i = 0 ; i < 3 ; i++){ sprintf(buf, "LED%bd is %s\r\n", i, ((P0 >> (i+3)) & 0x01) ? "OFF" : "ON"); send(s, buf, strlen(buf)); } break; case LED0_ON_CMD: /*处理LED0 ON命令*/ printf("LED0_ON_CMD\r\n"); sprintf(buf, "Turn ON the LED0\r\n"); send(s, buf, strlen(buf)); P0_3 = 0; /* 将GPIO 0_3设置为0, 与低有效LED连接*/ break; case LED1_ON_CMD:/*处理LED1 ON命令*/ printf("LED1_ON_CMD\r\n"); sprintf(buf, "Turn ON the LED1\r\n"); send(s, buf, strlen(buf)); P0_4 = 0; /*将GPIO 0_4设置为0, 与低有效LED连接*/ break; case LED2_ON_CMD: /*处理LED2 ON命令*/ printf("LED2_ON_CMD\r\n"); sprintf(buf, "Turn ON the LED2\r\n"); send(s, buf, strlen(buf)); P0_5 = 0; /* 将GPIO 0_5设置为0,与低有效LED连接*/ break; case LED0_OFF_CMD: /* 处理LED0 OFF命令*/ printf("LED0_OFF_CMD\r\n"); sprintf(buf, "Turn OFF the LED0\r\n"); send(s, buf, strlen(buf)); P0_3 = 1; /*将GPIO 0_3设置为1,与低有效LED连接*/ break; case LED1_OFF_CMD: /* 处理LED1 OFF命令*/ printf("LED1_OFF_CMD\r\n"); sprintf(buf, "Turn OFF the LED1\r\n"); send(s, buf, strlen(buf)); P0_4 = 1; /* 将GPIO 0_4设置为1,与低有效LED连接*/ break; case LED2_OFF_CMD: /* 处理LED2 OFF命令*/ printf("LED2_OFF_CMD\r\n"); sprintf(buf, "Turn OFF the LED2\r\n"); send(s, buf, strlen(buf)); P0_5 = 1; /* 将GPIO 0_5设置为,与低有效LED连接*/ break; case EXIT_CMD: /*处理EXIT 命令*/ printf("EXIT command\r\n"); sprintf(buf, "EXIT command\r\n Good Bye~~\r\n Logout from W7100 TELNET"); send(s, buf, strlen(buf)); close(s); user_state = LOGOUT; break; default: break; } } |
程序4.4proc_command()函数
4.5 willopt(), wontopt(), doopt()和dontopt()函数
willopt(), wontopt(), doopt()和dontopt()函数是用于协商Telnet选项的命令。它们需要socket s和选项作为输入参数。更多关于每个命令和选项的详细信息,请参阅表2.1和2.2。
void willopt(SOCKET s, int opt) { int ack; printf("Recv: will"); if(opt <= NOPTIONS) printf("%s\r\n", tel_options[opt]); else printf("%u\r\n", opt); switch(opt){ case TN_TRANSMIT_BINARY: case TN_ECHO: case TN_SUPPRESS_GA: ack = DO; /*如果接收到‘WILL’并且它包含TN_SUPPRESS_GA选项,发送‘DO’ */ break; default: ack = DONT; /* 拒绝其它未定义的命令*/ } sendIAC(s, ack, opt); } void wontopt(SOCKET s, int opt) { printf("recv: wont"); if (opt <= NOPTIONS) printf("%s\r\n", tel_options[opt]); else printf("%u\r\n", opt); switch(opt){ case TN_TRANSMIT_BINARY: case TN_ECHO: case TN_SUPPRESS_GA: /*如果接收到WONT带有TN_SUPPRESS_GA选项的命令*/ if (remote[opt] == 0){ remote[opt] = 1; /*设置TN_SUPPRESS_GA选项*/ sendIAC(s, DONT, opt); /* 发送带有TN_SUPPRESS_GA 选项的DONT命令*/ } break; } } void doopt(SOCKET s, int opt) { printf("recv: do "); if (opt <= NOPTIONS) printf("%s\r\n", tel_options[opt]); else printf("%u\r\n", opt); switch(opt){ case TN_SUPPRESS_GA: /*如果接收到带有TN_SUPPRESS_GA 选项的DO命令*/ sendIAC(s, WILL, opt); /* 发送带有TN_SUPPRESS_GA选项的WILL命令*/ break; case TN_ECHO: /* 如果接收带有TN_ECHO选项的DO命令*/ sprintf(buf, "WELCOME TO THE W7100 TELNET SERVER!!\r\nPlease insert your ID: "); send(s, buf, strlen(buf)); break; default: sendIAC(s, WONT, opt); } } void dontopt(int opt) { printf("recv: dont "); if (opt <= NOPTIONS) printf("%s\r\n", tel_options[opt]); else printf("%u\r\n", opt); switch(opt){ case TN_TRANSMIT_BINARY: case TN_ECHO: case TN_SUPPRESS_GA: /*如果接收带有TN_SUPPRESS_GA 选项的DON’T命令*/ if (remote[opt] == 0) remote[opt] = 1; /*设置TN_SUPPRESS_GA选型*/ break; } } |
程序4.5willopt(), wontopt(), doopt() and dontopt()函数
版权2011 WIZnet, Inc. 保留所有权利。
欲要更多详情,请查阅我们的网站http://www.wiznettechnology.cn
如果您有任何疑问,请直接留言或登录WIZ net官方网站:http://www.wiznettechnology.cn/
或者来电:86-10-84539974(转166),QQ:2377211388,
邮箱:wiznetbj@wiznettechnology.com 联系人:Jerry ,谢谢!
没有评论:
发表评论