原创 第三部分(2)-使用高速SPI以太网控制芯片W5200登录Telnet服务器

2012-1-13 13:11 1443 11 11 分类: MCU/ 嵌入式

在上一节我们为大家讲解了部分基于IAR编译器的代码例子,由于字数限制,我们分成两节,相关第一节部分请参照:

http://bbs.ednchina.com/BLOG_ARTICLE_3001247.HTM

 

4.5 Proc_command()功能

Proc_command()是用来处理在tel_input()程序中的输入的命令。它详细说明了有关“HELP,GET LED,LED0 ON/OFF,LED1 ON/OFF,LED2 ON/OFF”的命令。对于没有定义的命令,则会出现“BAD COMMAND”字样的信息。

void proc_command(SOCKET s)

{

char **cmdp;
 char *cp;

char *help = {"HELP: Show all available commands\ \r\nGET LED: Show all LED status\

\r\nLED3 ON/OFF: Turn ON/OFF the LED3\
 \r\nLED4 ON/OFF: Turn ON/OFF the LED4\

\r\nEXIT: Exit from W5200 TELNET server\r\n"}; /* command HELP : Message */

 

for(cp = data_buf; *cp != '\0';    cp++){

*cp = tolower(*cp);        /* Translate big letter to small letter */

}

 

if(*data_buf != '\0') {

/* Find the input command in table; if it isn't there, return Syntax Error */

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, "%s : BAD command\r\n", data_buf);
 send(s, (uint8 const *)buf, strlen(buf), FALSE);
 return;

}

 

switch(cmdp - commands) {

 

case HELP_CMD :       /* Process HELP command */

printf("HELP_CMD\r\n");

sprintf(buf, help);


send(s, (uint8 const *)buf, strlen(buf), FALSE); break;

 

case GET_LED_CMD :         /* Process GET LED command */

printf("GET_LED_CMD\r\n");

sprintf(buf, "LED%d is %s\r\n", 3, GPIO_ReadOutputDataBit(GPIOA, LED3) ? "OFF" : "ON"); send(s, (uint8 const *)buf, strlen(buf), FALSE);

sprintf(buf, "LED%d is %s\r\n", 4, GPIO_ReadOutputDataBit(GPIOA, LED4) ? "OFF" : "ON"); send(s, (uint8 const *)buf, strlen(buf), FALSE);

break;

 

case LED3_ON_CMD :         /* Process LED3 ON command */

printf("LED3_ON_CMD\r\n");

sprintf(buf, "Turn ON the LED3\r\n");

send(s, (uint8 const *)buf, strlen(buf), FALSE);
 GPIO_ResetBits(GPIOA, LED3); // led3 on
 break;

 

case LED4_ON_CMD :         /* Process LED4 ON command */

printf("LED4_ON_CMD\r\n");

sprintf(buf, "Turn ON the LED4\r\n");

send(s, (uint8 const *)buf, strlen(buf), FALSE);
 GPIO_ResetBits(GPIOA, LED4); // led4 on
 break;

 

case LED3_OFF_CMD :        /* Process LED3 OFF command */

printf("LED3_OFF_CMD\r\n");

sprintf(buf, "Turn OFF the LED3\r\n");

send(s, (uint8 const *)buf, strlen(buf), FALSE);
 GPIO_SetBits(GPIOA, LED3); // led3 off
 break;

 

case LED4_OFF_CMD :        /* Process LED4 OFF command */

printf("LED4_OFF_CMD\r\n");

sprintf(buf, "Turn OFF the LED4\r\n");

send(s, (uint8 const *)buf, strlen(buf), FALSE);
 GPIO_SetBits(GPIOA, LED4); // led4 off
 break;


case EXIT_CMD :       /* Process EXIT command */

printf("EXIT command\r\n");

sprintf(buf, "EXIT command\r\nGood bye!\r\nLogout from W5200 TELNET\r\n"); send(s, (uint8 const *)buf, strlen(buf), FALSE);

close(s);

user_state = LOGOUT;
 break;

default :
 break;

}

}

}        /* End proc_command function */


          
4.6 Willopt(),wontopt(),doopt()和dontopt()功能

Willopt(),wontopt(),doopt()还有dontopt()是用于远程服务选项协议的命令。它需要sockets和相应选项作为输入参数。更多信息可以参考Table2.1和Table2.2。

void willopt(SOCKET s, uint16 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;        /* If receive 'WILL' and it has TN_SUPPRESS_GA option, transmit 'DO' */

 

break;

default :

ack = DONT;        /* Refuse other commands which not defined */

}

sendIAC(s, ack, opt);

}        /* End willopt function */


void wontopt(SOCKET s, uint16 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 :

if(remote[opt] == 0) {

remote[opt] = 1;

sendIAC(s, DONT, opt);

}

break;

}


/* If receive WONT command with TN_SUPPRESS_GA option */

 

/* Set the TN_SUPPRESS_GA option */

/* Send DONT command with TN_SUPPRESS_GA */

 


}        /* End wontopt function */


void doopt(SOCKET s, uint16 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 :       /* If receive DO command with TN_SUPPRESS_GA option */

sendIAC(s, WILL, opt);         /* Send WILL command with TN_SUPPRESS_GA */

break;

case TN_ECHO :         /* If receive DO command with TN_ECHO option */

sprintf(buf, "WELCOME!\r\nID : ");

send(s, (uint8 const *)buf, strlen(buf), FALSE);

break;

default :

sendIAC(s, WONT, opt);

}

}        /* End doopt function */


void dontopt(uint16 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 :

if(remote[opt] == 0) {

remote[opt] = 1;

}

break;

}

}      /* End dontopt function */


/* If receive DONT command with TN_SUPPRESS_GA option */

 



如果您有什么疑问请留言或者来信:wiznetbj@wiznettechnology.com,

Tel: 86-10-84539974(转166)或者登陆我们的网站: http://www.wiznet.co.kr/
希望本篇文章可以给您带来帮助,谢谢。

文章评论0条评论)

登录后参与讨论
我要评论
0
11
关闭 站长推荐上一条 /2 下一条