刚刚移植了一下uip的telnetd,还是比较简单方便的.
首先添加文件,注意usershell是自己写的.
在tcp.c中添加tcp端口监听程序
添加#include "telnetd.h"
- void tcp_demo_appcall(void)
- {
- if(uip_conn->lport == HTONS(TCP_ClinetPort))
- {
- tcp_client_demo_appcall();
- }
- else if(uip_conn->lport == HTONS(80))
- {
- httpd_appcall();
- }
- else if(uip_conn->lport == HTONS(23))
- {
- telnetd_appcall();
- }
- }
在主函数中初始化
- telnetd_init();
//自定义shell
usershell.c
- //用户自定义shell
- #include "shell.h"
- #include "usershell.h"
- #include <string.h>
- #include "rtc.h"
- char printfbuff[64];
- //获取时间
- void SHELL_GetTime(char *pStr)
- {
- //RTC_Get(); //更新时间
- sprintf(printfbuff, "%02d:%02d:%02d\r\n",timer.hour, timer.min, timer.sec);
- shell_output("[获取时间成功]: ", printfbuff);
- }
- //获取日期
- void SHELL_GetDate(char *pStr)
- {
- //RTC_Get(); //更新时间
- sprintf(printfbuff, "%04d-%02d-%02d\r\n",timer.w_year, timer.w_month, timer.w_date);
- shell_output("[获取日期成功]: ", printfbuff);
- }
usershell.h
- #ifndef __USER_SHELL_H__
- #define __USER_SHELL_H__
- void SHELL_GetTime(char *pStr); //获取时间
- void SHELL_GetDate(char *pStr); 获取日期
- #endif /* __USER_SHELL_H__ */
修改telnetd.h
- #ifndef __TELNETD_H__
- #define __TELNETD_H__
- #include "uipopt.h"
- void telnetd_init(void);
- void telnetd_appcall(void);
- #ifndef TELNETD_CONF_LINELEN
- #define TELNETD_CONF_LINELEN 40
- #endif
- #ifndef TELNETD_CONF_NUMLINES
- #define TELNETD_CONF_NUMLINES 16
- #endif
- struct telnetd_state {
- char *lines[TELNETD_CONF_NUMLINES];
- char buf[TELNETD_CONF_LINELEN];
- char bufptr;
- u8_t numsent;
- u8_t state;
- };
- //typedef struct telnetd_state uip_tcp_appstate_t;
- //#ifndef UIP_APPCALL
- //#define UIP_APPCALL telnetd_appcall
- //#endif
- #endif /* __TELNETD_H__ */
- void telnetd_init(void);
在shell.c中添加自定义命令支持
- static void
- help(char *str)
- {
- shell_output("Available commands:", "");
- shell_output("stats - show network statistics", "");
- shell_output("conn - show TCP connections", "");
- shell_output("help, ? - show help", "");
- shell_output("exit - exit shell", "");
- shell_output("time? - 获取当前系统时间", "");
- shell_output("date?, - 获取当前系统日期", "");
- }
- /*---------------------------------------------------------------------------*/
- static void
- unknown(char *str)
- {
- if(strlen(str) > 0) {
- shell_output("Unknown command: ", str);
- }
- }
- /*---------------------------------------------------------------------------*/
- static struct ptentry parsetab[] =
- {{"stats", help},
- {"conn", help},
- {"help", help},
- {"exit", shell_quit},
- {"?", help},
- {"time?", SHELL_GetTime},
- {"date?", SHELL_GetDate},
- /* Default action */
- {NULL, unknown}};
- /*---------------------------------------------------------------------------*/
- shell_output("time? - 获取当前系统时间", "");
- shell_output("date?, - 获取当前系统日期", "");
命令列表
- {"time?", SHELL_GetTime},
- {"date?", SHELL_GetDate},