uip移植telnetd并添加自定义命令

刚刚移植了一下uip的telnetd,还是比较简单方便的.


首先添加文件,注意usershell是自己写的.



在tcp.c中添加tcp端口监听程序

添加#include "telnetd.h"

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. void tcp_demo_appcall(void)  
  2. {     
  3.     if(uip_conn->lport == HTONS(TCP_ClinetPort))  
  4.     {  
  5.         tcp_client_demo_appcall();  
  6.     }  
  7.     else if(uip_conn->lport == HTONS(80))  
  8.     {  
  9.         httpd_appcall();   
  10.     }  
  11.     else if(uip_conn->lport == HTONS(23))  
  12.     {  
  13.         telnetd_appcall();  
  14.     }  
  15. }  

在主函数中初始化

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. telnetd_init();  

//自定义shell

usershell.c

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //用户自定义shell  
  2. #include "shell.h"  
  3. #include "usershell.h"  
  4. #include <string.h>  
  5. #include "rtc.h"  
  6.   
  7. char printfbuff[64];  
  8.   
  9. //获取时间  
  10. void SHELL_GetTime(char *pStr)  
  11. {  
  12.     //RTC_Get();        //更新时间  
  13.     sprintf(printfbuff, "%02d:%02d:%02d\r\n",timer.hour, timer.min, timer.sec);  
  14.     shell_output("[获取时间成功]: ", printfbuff);  
  15. }  
  16.   
  17. //获取日期  
  18. void SHELL_GetDate(char *pStr)  
  19. {  
  20.     //RTC_Get();        //更新时间  
  21.     sprintf(printfbuff, "%04d-%02d-%02d\r\n",timer.w_year, timer.w_month, timer.w_date);  
  22.     shell_output("[获取日期成功]: ", printfbuff);  
  23. }  


usershell.h

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #ifndef __USER_SHELL_H__  
  2. #define __USER_SHELL_H__  
  3.   
  4.   
  5.   
  6.   
  7. void SHELL_GetTime(char *pStr); //获取时间  
  8. void SHELL_GetDate(char *pStr); 获取日期  
  9.   
  10.   
  11. #endif /* __USER_SHELL_H__ */  


修改telnetd.h

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #ifndef __TELNETD_H__  
  2. #define __TELNETD_H__  
  3.   
  4. #include "uipopt.h"  
  5.   
  6. void telnetd_init(void);  
  7. void telnetd_appcall(void);  
  8.   
  9. #ifndef TELNETD_CONF_LINELEN  
  10. #define TELNETD_CONF_LINELEN 40  
  11. #endif  
  12. #ifndef TELNETD_CONF_NUMLINES  
  13. #define TELNETD_CONF_NUMLINES 16  
  14. #endif  
  15.   
  16. struct telnetd_state {  
  17.   char *lines[TELNETD_CONF_NUMLINES];  
  18.   char buf[TELNETD_CONF_LINELEN];  
  19.   char bufptr;  
  20.   u8_t numsent;  
  21.   u8_t state;  
  22. };  
  23.   
  24. //typedef struct telnetd_state uip_tcp_appstate_t;  
  25.   
  26. //#ifndef UIP_APPCALL  
  27. //#define UIP_APPCALL     telnetd_appcall  
  28. //#endif  
  29.   
  30. #endif /* __TELNETD_H__ */  
添加

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. void telnetd_init(void);  


在shell.c中添加自定义命令支持

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. static void  
  2. help(char *str)  
  3. {  
  4.   shell_output("Available commands:""");  
  5.   shell_output("stats   - show network statistics""");  
  6.   shell_output("conn    - show TCP connections""");  
  7.   shell_output("help, ? - show help""");  
  8.   shell_output("exit    - exit shell""");  
  9.     shell_output("time?    - 获取当前系统时间""");  
  10.     shell_output("date?,   - 获取当前系统日期""");  
  11. }  
  12. /*---------------------------------------------------------------------------*/  
  13. static void  
  14. unknown(char *str)  
  15. {  
  16.   if(strlen(str) > 0) {  
  17.     shell_output("Unknown command: ", str);  
  18.   }  
  19. }  
  20. /*---------------------------------------------------------------------------*/  
  21. static struct ptentry parsetab[] =  
  22.   {{"stats", help},  
  23.    {"conn", help},  
  24.    {"help", help},  
  25.    {"exit", shell_quit},  
  26.    {"?", help},  
  27.     {"time?", SHELL_GetTime},  
  28.     {"date?", SHELL_GetDate},  
  29.       
  30.    /* Default action */  
  31.    {NULL, unknown}};  
  32. /*---------------------------------------------------------------------------*/  
添加命令显示

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. shell_output("time?    - 获取当前系统时间""");  
  2. shell_output("date?,   - 获取当前系统日期""");  

命令列表

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. {"time?", SHELL_GetTime},  
  2. {"date?", SHELL_GetDate},  



执行之后的效果

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值