Telnet用户接口文件的实现
KEIL官网有提供Telnet的接口文件,名为Telnet_uif.c文件。我们就是在这个文件上修改。具体修改后的代码如下:
/* Net_Config.c */
extern struct tcp_cfg tcp_config;
extern struct tnet_cfg tnet_config;
#define tcp_NumSocks tcp_config.NumSocks
#define tcp_socket tcp_config.Scb
#define tnet_EnAuth tnet_config.EnAuth
#define tnet_auth_passw tnet_config.Passw
/* ANSI ESC Sequences for terminal control. */
#define CLS " 33[2J"
#define TBLUE " 33[37;44m"
#define TNORM " 33[0m"
/* My structure of a Telnet U32 storage variable. This variable is private */
/* for each Telnet Session and is not altered by Telnet Server. It is only */
/* set to zero when tnet_process_cmd() is called for the first time. */
typedef struct
{
U8 id;
U8 nmax;
U8 idx;
} MY_BUF;
#define MYBUF(p) ((MY_BUF *)p)
/* Local variables */
static U8 const tnet_header[] =
{
CLS "rn"
" " TBLUE
"*=============================================================*rn" TNORM
" " TBLUE
"* RL-TCPnet之Telnet服务器实验 *rn" TNORM
" " TBLUE
"*=============================================================*rn" TNORM
};
static U8 const tnet_help1[] =
{
"rnrn"
" 当前支持的命令:rn"
" ----------------------------rn"
" ledon - 打开LED4rn"
" ledoff - 关闭LED4rn"
" rinfo - 显示远程设备的IP地址和MACrn"
};
static U8 const tnet_help2[] =
{
" help, ? - 显示帮助rn"
" bye - 断开连接rnrn"
" ,<^C> - ESC按键,断开连接rn"
" - 回车函数,删除左侧字符rn"
" - 按键UP和DOWM,浏览历史命令rn"
};
/*----------------------------------------------------------------------------
* Telnet CallBack Functions
*---------------------------------------------------------------------------*/
/*--------------------------- tnet_cbfunc -----------------------------------*/
U16 tnet_cbfunc (U8 code, U8 *buf, U16 buflen)
{
/* This function is called by the Telnet Client to get formated system */
/* messages for different code values. */
/* Values for 'code': */
/* 0 - initial header */
/* 1 - prompt string */
/* 2 - header for login only if authorization is enabled */
/* 3 - string 'Username' for login */
/* 4 - string 'Password' for login */
/* 5 - message 'Login incorrect' */
/* 6 - message 'Login timeout' */
/* 7 - Unsolicited messages from Server (ie. Basic Interpreter) */
U16 len = 0;
/* Make a reference to disable compiler warning. */
buflen = buflen;
switch (code)
{
case 0:
/* Write initial header after login. */
len = str_copy (buf, (U8 *)&tnet_header);
break;
case 1:
/* Write a prompt string. */
len = str_copy (buf, "rnarmfly> ");
break;
case 2:
/* Write Login header. */
len = str_copy (buf, CLS "rnRL-TCPnet之Telnet服务器,"
" 请登录...rn");
break;
case 3:
/* Write 'username' prompt. */
len = str_copy (buf, "rn用户名: ");
break;
case 4:
/* Write 'Password' prompt. */
len = str_copy (buf, "rn密 码: ");
break;
case 5:
/* Write 'Login incorrect'.message. */
len = str_copy (buf, "rn登录失败");
break;
case 6:
/* Write 'Login Timeout' message. */
len = str_copy (buf, "rn120秒无操作,退出登录rn");
break;
}
return (len);
}
/*--------------------------- tnet_process_cmd ------------------------------*/
U16 tnet_process_cmd (U8 *cmd, U8 *buf, U16 buflen, U32 *pvar)
{
/* This is a Telnet Client callback function to make a formatted output */
/* for 'stdout'. It returns the number of bytes written to the out buffer.*/
/* Hi-bit of return value (len is or-ed with 0x8000) is a disconnect flag.*/
/* Bit 14 (len is or-ed with 0x4000) is a repeat flag for the Tnet client.*/
/* If this bit is set to 1, the system will call the 'tnet_process_cmd()' */
/* again with parameter 'pvar' pointing to a 4-byte buffer. This buffer */
/* can be used for storing different status variables for this function. */
/* It is set to 0 by Telnet server on first call and is not altered by */
/* Telnet server for repeated calls. This function should NEVER write */
/* more than 'buflen' bytes to the buffer. */
/* Parameters: */
/* cmd - telnet received command string */
/* buf - Telnet transmit buffer */
/* buflen - length of this buffer (500-1400 bytes - depends on MSS) */
/* pvar - pointer to local storage buffer used for repeated loops */
/* This is a U32 variable - size is 4 bytes. Value is: */
/* - on 1st call = 0 */
/* - 2nd call = as set by this function on first call */
REMOTEM rm;
U16 len = 0;
switch (MYBUF(pvar)->id)
{
case 0:
/* First call to this function, the value of '*pvar' is 0 */
break;
case 1:
/* Request a repeated call, bit 14 is a repeat flag. */
return (len | 0x4000);
case 2:
/* Request a repeated call, bit 14 is a repeat flag. */
return (len |= 0x4000);
}
/* Simple Command line parser */
len = strlen ((const char *)cmd);
if (tnet_ccmp (cmd, "LEDON") == __TRUE)
{
/* 'LED4' command received */
len = str_copy (buf,"rn=====>LED4 Lights ON");
bsp_LedOn(4);
return (len);
}
if (tnet_ccmp (cmd, "LEDOFF") == __TRUE)
{
/* 'LED4' command received */
len = str_copy (buf,"rn=====>LED4 Lights OFF");
bsp_LedOff(4);
return (len);
}
if (tnet_ccmp (cmd, "BYE") == __TRUE)
{
/* 'BYE' command, send message and disconnect */
len = str_copy (buf, "rnDisconnect...rn");
/* Hi bit of return value is a disconnect flag */
return (len | 0x8000);
}
if (tnet_ccmp (cmd, "RINFO") == __TRUE)
{
/* Display Remote Machine IP and MAC address. */
tnet_get_info (&rm);
len = sprintf ((char *)buf,"rn Remote IP : %d.%d.%d.%d",
rm.IpAdr[0],rm.IpAdr[1],rm.IpAdr[2],rm.IpAdr[3]);
len += sprintf ((char *)(buf+len),
"rn Remote MAC: %02X-%02X-%02X-%02X-%02X-%02X",
rm.HwAdr[0],rm.HwAdr[1],rm.HwAdr[2],
rm.HwAdr[3],rm.HwAdr[4],rm.HwAdr[5]);
return (len);
}
if (tnet_ccmp (cmd, "HELP") == __TRUE || tnet_ccmp (cmd, "?") == __TRUE)
{
/* 'HELP' command, display help text */
len = str_copy (buf,(U8 *)tnet_help1);
len += str_copy (buf+len,(U8 *)tnet_help2);
return (len);
}
/* Unknown command, display message */
len = str_copy (buf, "rn==> Unknown Command: ");
len += str_copy (buf+len, cmd);
return (len);
} |