cmd.c
来自「基于ARM和uC/OS-II实现的串口控制台」· C语言 代码 · 共 1,090 行 · 第 1/2 页
C
1,090 行
/*******************************************************************************
**
** File Name: cmd.c
** Author: Zhoudan
** Last Modified: 2007-03-14
** Last Version: 1.0
** Environment: LPC2214/RVDSv2.2/uCOS-II2.52
** Descriptions: Command Line Interface/Serial Port Console
**
*******************************************************************************/
#include "config.h"
/* 用户模式下的命令 */
CmdInfo CmdDBUser[] = {
{"enable ", CmdEnable, "Change the EXEC mode to PRIVATE.", 0, NULL},
{"help ", CmdHelp, "Provides a brief description of the help system.", 1, NULL},
{"man ", CmdMan, "Display a command help information.", 2, ParaManDB},
{"show ", CmdShow, "Display the system status and configurations.", 3, ParaShowDB},
{""},
};
/* 特权模式下的命令 */
CmdInfo CmdDBPrivate[] = {
{"configure ", CmdConf, "Change the EXEC mode to GLOBAL CONFIGURATION.", 0, ParaConfDB},
{"debug ", CmdDebug, "Enable or disable debug informations.", 1, ParaDebugDB},
{"disable ", CmdDisable, "Return to USER EXEC mode.", 2, NULL},
{"help ", CmdHelp, "Provides a brief description of the help system.", 3, NULL},
{"man ", CmdMan, "Display a command help information.", 4, ParaManDB},
{"restart ", CmdReset, "Reset the system.", 5, NULL},
{"show ", CmdShow, "Display the system status and configurations.", 6, ParaShowDB},
{"save ", CmdSave, "Save current configurations.", 7, NULL},
{"clear ", CmdClear, "Clear statistics.", 8, NULL},
{""},
};
/* 配置模式下的命令 */
CmdInfo CmdDBConfig[] = {
{"end ", CmdEnd, "Return to PRIVATE mode.", 0, NULL},
{"exit ", CmdExit, "Exit to front mode.", 1, NULL},
{"help ", CmdHelp, "Provides a brief description of the help system.", 2, NULL},
{"hostname ", CmdHostname, "Set the host name.", 3, NULL},
{"line ", CmdLine, "Configure the parameter of a line.", 4, ParaConfLineDB},
{"man ", CmdMan, "Display a command help information.", 5, ParaManDB},
{"password ", CmdPasswd, "Set or change the password of PRIVATE mode.", 6, NULL},
{"show ", CmdShow, "Display the system status and configurations.", 9, ParaShowDB},
{""},
};
/* 线路配置模式下的命令 */
CmdInfo CmdDBLine[] = {
{"databits ", CmdLineDataBits,"Set the data-bits width of console port.", 0, NULL},
{"exec-timeout ", CmdLineTimeout, "Set the EXEC timeout.", 1, NULL},
{"exit ", CmdExit, "Exit to front mode.", 2, NULL},
{"end ", CmdEnd, "Return to PRIVATE mode.", 3, NULL},
{"help ", CmdHelp, "Provides a brief description of the help system.", 4, NULL},
{"man ", CmdMan, "Display a command help information.", 5, ParaManDB},
{"parity ", CmdLineParity, "Set the parity mode of console port.", 6, ParaParityDB},
{"show ", CmdShow, "Display the system status and configurations.", 7, ParaShowDB},
{"speed ", CmdLineSpeed, "Set the speed of console port.", 8, NULL},
{"stopbits ", CmdLineStop, "Set the stop-bits of console port", 9, NULL},
{""},
};
/* 接口配置模式下的命令 */
CmdInfo CmdDBIntf[] = {
{"exit ", CmdExit, "Exit to front mode.", 0, NULL},
{"end ", CmdEnd, "Return to PRIVATE mode.", 1, NULL},
{"help ", CmdHelp, "Provides a brief description of the help system.", 2, NULL},
{"man ", CmdMan, "Display a command help information.", 3, ParaManDB},
{"show ", CmdShow, "Display the system status and configurations.", 4, ParaShowDB},
{""},
};
////////////////////////////////////////////////////////////////////////////////
/* debug 的子命令 */
CmdInfo ParaDebugDB[] = {
{"disable ", SubDebugDis, "", 0, NULL},
{"nmp ", SubDebugNmp, "", 1, NULL},
{"errcnt ", SubDebugErrCnt, "", 2, NULL},
{""},
};
/* man 的子命令 */
CmdInfo ParaManDB[] = {
{"configure ", SubManConf, "", 0, NULL},
{"databits ", SubManDatab, "", 1, NULL},
{"disable ", SubManDisable, "", 2, NULL},
{"enable ", SubManEnable, "", 3, NULL},
{"exit ", SubManExit, "", 4, NULL},
{"end ", SubManEnd, "", 5, NULL},
{"hostname ", SubManHostname, "", 6, NULL},
{"line ", SubManLine, "", 7, NULL},
{"password ", SubManPasswd, "", 8, NULL},
{"processor ", SubManProc, "", 9, NULL},
{"parity ", SubManParity, "", 10, NULL},
{"route ", SubManRoute, "", 11, NULL},
{"show ", SubManShow, "", 12, NULL},
{"save ", SubManSave, "", 13, NULL},
{"speed ", SubManSpeed, "", 14, NULL},
{"stopbits ", SubManStopb, "", 15, NULL},
{"tableset ", SubManTableset, "", 16, NULL},
{"v.35 ", SubManV, "", 17, NULL},
{""},
};
/* configure 的子命令 */
CmdInfo ParaConfDB[] = {
{"terminal ", SubConfTer, "", 0, NULL},
{""},
};
/* line 的子命令 */
CmdInfo ParaConfLineDB[] = {
{"console ", SubLineCon, "", 0, NULL},
{""},
};
/* show 的子命令 */
CmdInfo ParaShowDB[] = {
{"console ", SubShowLine, "", 0, NULL},
{"node ", SubShowNode, "", 4, NULL},
{"randomtest ", SubShowRandTest,"", 6, NULL},
{"version ", SubShowVer, "", 8, NULL},
{"errcnt ", SubShowErrCnt, "", 9, NULL},
{""},
};
/* parity 的子命令 */
CmdInfo ParaParityDB[] = {
{"none ", SubParityNone, "", 0, NULL},
{"odd ", SubParityOdd, "", 1, NULL},
{"even ", SubParityEven, "", 2, NULL},
{""},
};
void reset(void)
{
void (*reboot)(void) = 0x00000000;
reboot();
}
void CmdHelp(void)
{
const char *CMDINFO = "\n\r\
Help may be requested at any point in a command by entering a question mark '?'\
.If nothing matches, the help list will be empty and you must backup until \n\r\
entering a '?' shows the available options.\
Two styles of help are provided:\
\n\r\
1. Full help is available when you are ready to enter a command argument \n\r\
(e.g. 'show ?') and describes each possilbe argument.\
\n\r\
2. Partial help is provided when an abbreviated argument is entered and you \n\r\
want to know what arguments match the input (e.g. 'show pr?'.)\n\r\
";
SendStr(CMDINFO);
BufClean();
CurPrompt();
}
/*******************************************************************************
* Function: CmdPasswd()
* Description:
*******************************************************************************/
void CmdPasswd(void)
{
char CurCmdBuf[CMD_BUF_MAX], passwd[64], *pConf;
static uint8 i = 0;
pConf = (char *)CLIConfigFile;
GetCmd(CurCmdBuf);
if(GetPara(CurCmdBuf, passwd, 1))
{
for(i = 0; i < strlen(passwd); i++)
{
pConf[CFG_PASSWORD * 4 + i] = passwd[i];
}
pConf[CFG_PASSWORD * 4 + i] = 0x00;
}
BufClean();
CurPrompt();
}
/*******************************************************************************
* Function: CmdMan()
* Description:
*******************************************************************************/
void CmdMan(void)
{
char CurCmdBuf[CMD_BUF_MAX];
uint8 err, ParaID = 0xff;
GetCmd(CurCmdBuf);
if(SearchPara(CurCmdBuf, 1, ParaManDB, &ParaID, &err) == ERR_CLI_PARA_OK)
{
ParaManDB[ParaID].func();
BufClean();
CurPrompt();
}
else
{
CmdErr();
}
}
void SubManPasswd(void)
{
const char *CMDINFO = "\n\r\
Function: Set or change the password of PRIVATE mode.\n\r\
\n\r\
Syntax: password PASSWORD\n\r\
\n\r\
Options:\n\r\
PASSWORD The PASSWORD area can support numbers(0-9), word(a-z or A-Z),and '_'.\n\r\
The longest of this area is 16 words.\n\r\
\n\r\
Execute Mode: Console(config)#\n\r\
";
SendStr(CMDINFO);
}
void SubManEnable(void)
{
const char *CMDINFO = "\n\r\
Function: Change the EXEC mode to PRIVATE.\n\r\
\n\r\
Syntax: enable \n\r\
\n\r\
Options: No options. \n\r\
\n\r\
Execute Mode: Console> \n\r\
";
SendStr(CMDINFO);
}
void SubManDisable(void)
{
const char *CMDINFO = "\n\r\
Function: Return to USER EXEC mode.\n\r\
\n\r\
Syntax: disable \n\r\
\n\r\
Options: No options. \n\r\
\n\r\
Execute Mode: Console# \n\r\
";
SendStr(CMDINFO);
}
void SubManShow(void)
{
const char *CMDINFO = "\n\r\
Function: Display the system status and configurations.\n\r\
\n\r\
Syntax: show [ console | fpgastate | interface | monitor-register | node | route |\n\r\
randomtest | ruletable TABLEID POLICY | version ] \n\r\
\n\r\
Options:\n\r\
console Show the console port information. \n\r\
fpgastate Show the FPGA register \n\r\
interface V.35 interface configuration \n\r\
monitor-register \n\r\
node Show the node information. \n\r\
route Show the route table of this machine. \n\r\
randomtest Show the random test result. \n\r\
ruletable Show the rule table. \n\r\
version Show the software version of this machine.\n\r\
Execute Mode: Console>\n\r\
Console#\n\r\
";
SendStr(CMDINFO);
}
void SubManConf(void)
{
const char *CMDINFO = "\n\r\
Function: Change the EXEC mode to GLOBAL CONFIGURATION.\n\r\
\n\r\
Syntax: configure terminal \n\r\
\n\r\
Options: \n\r\
terminal Change the EXEC mode from PRIVATE to GLOBAL CONFIGURATION.\n\r\
\n\r\
Execute Mode: Console#\n\r\
";
SendStr(CMDINFO);
}
void SubManSave(void)
{
const char *CMDINFO = "\n\r\
Function: Save current configurations. \n\r\
\n\r\
Syntax: save \n\r\
\n\r\
Options: No options. \n\r\
\n\r\
Execute Mode: Console# \n\r\
";
SendStr(CMDINFO);
}
void SubManExit(void)
{
const char *CMDINFO = "\n\r\
Function: Exit to front mode. \n\r\
\n\r\
Syntax: exit \n\r\
\n\r\
Options: No options. \n\r\
\n\r\
Execute Mode: Console(config)# \n\r\
Console(config-line)# \n\r\
";
SendStr(CMDINFO);
}
void SubManEnd(void)
{
const char *CMDINFO = "\n\r\
Function: Return to PRIVATE mode. \n\r\
\n\r\
Syntax: end \n\r\
\n\r\
Options: No options. \n\r\
\n\r\
Execute Mode: Console(config)# \n\r\
Console(config-line)# \n\r\
";
SendStr(CMDINFO);
}
void SubManHostname(void)
{
const char *CMDINFO = "\n\r\
Function: Change the name of this machine, that will change the prompt.\n\r\
\n\r\
Syntax: hostname NAME\n\r\
\n\r\
Options:\n\r\
NAME The NAME area can support numbers(0-9), word(a-z or A-Z),and '_'.\n\r\
The longest of this area is 16 words.\n\r\
\n\r\
Execute Mode: Console(config)#\n\r\
";
SendStr(CMDINFO);
}
void SubManLine(void)
{
const char *CMDINFO = "\n\r\
Function: Change the configuration of Console Port. And the EXEC mode will \n\r\
be changed to Line Configure mode. Console(config-line)# \n\r\
\n\r\
Syntax: line console \n\r\
\n\r\
Options: No options. \n\r\
\n\r\
Execute Mode: Console(config)# \n\r\
";
SendStr(CMDINFO);
}
void SubManRoute(void)
{
const char *CMDINFO = "\n\r\
Function: Add or Delete a route item on this machine. \n\r\
\n\r\
Syntax: route [ add | del ] \n\r\
\n\r\
Options:\n\r\
route add StartNetwork EndNetwork \n\r\
route del RouteItemID \n\r\
\n\r\
Execute Mode: Console(config)# \n\r\
";
SendStr(CMDINFO);
}
void SubManProc(void)
{
const char *CMDINFO = "\n\r\
Function: Enable or Disable Communication Processor.\n\r\
\n\r\
Syntax: processor [ enable | disable ] \n\r\
\n\r\
Options: \n\r\
enable . \n\r\
disable . \n\r\
\n\r\
Execute Mode: Console(config)#\n\r\
";
SendStr(CMDINFO);
}
void SubManV(void)
{
const char *CMDINFO = "\n\r\
Function: Configure V.35 interface.\n\r\
\n\r\
Syntax: v.35 \n\r\
\n\r\
Options: No options. \n\r\
\n\r\
Execute Mode: Console(config)# \n\r\
";
SendStr(CMDINFO);
}
void SubManTableset(void)
{
const char *CMDINFO = "\n\r\
Function: Set Policy for a DLCI. \n\r\
\n\r\
Syntax: tableset RuleTableID (0:Table0; 1:Table1)DLCI(0-1023), \n\r\
Policy(block, clear, encrypt, decrypt), ID(0-1023) \n\r\
\n\r\
Options: \n\r\
\n\r\
Execute Mode: \n\r\
";
SendStr(CMDINFO);
}
void SubManSpeed(void)
{
const char *CMDINFO = "\n\r\
Function: Change the Speed of console port. \n\r\
\n\r\
Syntax: speed SPEED \n\r\
\n\r\
Options: \n\r\
SPEED The console port can support standard speed: 1200bps 2400bps \n\r\
4800bps 9600bps 19200bps 38400bps 57600bps 115200bps. \n\r\
\n\r\
Execute Mode: Console(config-line)# \n\r\
";
SendStr(CMDINFO);
}
void SubManStopb(void)
{
const char *CMDINFO = "\n\r\
Function: Change the Stopbits width of console port. \n\r\
\n\r\
Syntax: stopbits WIDTH \n\r\
\n\r\
Options: \n\r\
WIDTH The console port can support 1 or 2. \n\r\
\n\r\
Execute Mode: Console(config-line)# \n\r\
";
SendStr(CMDINFO);
}
void SubManParity(void)
{
const char *CMDINFO = "\n\r\
Function: Change the Parity mode of console port.\n\r\
\n\r\
Syntax: parity [ none | odd | even ] \n\r\
\n\r\
Options: \n\r\
none Disable the parity. \n\r\
odd Change the parity mode to odd. \n\r\
even Change the parity mode to even. \n\r\
\n\r\
Execute Mode: Console(config-line)# \n\r\
";
SendStr(CMDINFO);
}
void SubManDatab(void)
{
const char *CMDINFO = "\n\r\
Function: Change the Databits width of console port. \n\r\
\n\r\
Syntax: databits WIDTH \n\r\
\n\r\
Options: \n\r\
WIDTH The scope of this option is 5 to 8. \n\r\
\n\r\
Execute Mode: Console(config-line)# \n\r\
";
SendStr(CMDINFO);
}
/*******************************************************************************
* Function: CmdSave()
* Description:
*******************************************************************************/
void CmdSave(void)
{
hal_flash_fd *fd;
SendStr(BLANKROW);
SendStr("\n\rBuilding Configurations...");
/* 保存配置标志位 */
CLIConfigFile[CFG_FLAG] = 0x6D73636E;
fd = hal_flash_dev_open(Sst39vf3201_Name);
/* 往外部FLASH写入4Kbytes数据 */
hal_write_flash(fd, CONF_ADDR / 2, (uint8 *)CLIConfigFile, 2048);
hal_flash_dev_close(fd);
SendStr(BLANKROW);
SendStr("[OK]");
CurPrompt();
BufClean();
}
/*******************************************************************************
* Function: CmdClear()
* Description:
*******************************************************************************/
void CmdClear(void)
{
InfFrInCalEth = 0;
InfFrOutCalEth = 0;
InfFrErrCalEth = 0;
InfPkgInCalEth = 0;
InfPkgOutCalEth = 0;
InfPkgErrCalEth = 0;
InfFrInCalV = 0;
InfFrOutCalV = 0;
InfFrErrCalV = 0;
InfPkgInCalV = 0;
InfPkgOutCalV = 0;
InfPkgErrCalV = 0;
ClearTimeCnt = 0;
CurPrompt();
BufClean();
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?