刚刚移植了一下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#endifstruct 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 voidhelp(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 voidunknown(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},