刚刚移植了一下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();
- }
- }
在主函数中初始化
//自定义shell
usershell.c
-
- #include "shell.h"
- #include "usershell.h"
- #include <string.h>
- #include "rtc.h"
-
- char printfbuff[64];
-
-
- void SHELL_GetTime(char *pStr)
- {
-
- sprintf(printfbuff, "%02d:%02d:%02d\r\n",timer.hour, timer.min, timer.sec);
- shell_output("[获取时间成功]: ", printfbuff);
- }
-
-
- void SHELL_GetDate(char *pStr)
- {
-
- 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;
- };
-
-
-
-
-
-
-
- #endif /* __TELNETD_H__ */
添加
在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},
-
-
- {NULL, unknown}};
-
添加命令显示
- shell_output("time? - 获取当前系统时间", "");
- shell_output("date?, - 获取当前系统日期", "");
命令列表
- {"time?", SHELL_GetTime},
- {"date?", SHELL_GetDate},
执行之后的效果