初级会员

- 积分
- 103
- 金钱
- 103
- 注册时间
- 2018-10-15
- 在线时间
- 11 小时
|
该命令行解析框架,可以轻松实现,读写HEX类型的数据,读写数值型数据,读写IP,MAC,字符串,运行函数,浮点型数据暂时不支持,带符号的数据暂时不支持。
命令行解析框架,使用链表将所有注册的信息串联起来,然后每个节点node包含了,keyword,help,数据类型描述,命令执行的回调函数,和参数存储的回调函数。主要的操作是,已知函数变量地址,类型,和地址宽度,然后强制转换后对变量读写操作。
命令节点结构如下- typedef struct fun_node
- {
- struct fun_node *pNext;
- const char *pHelp;
- const char *pName;
- uint8_t paraNum;
- FUN_CMD_BK RunFun;
- uint8_t EnableState;
- }ST_FUN_NODE;
- typedef enum
- {
- emCmdDataType_Dec =1,
- emCmdDataType_Hex =2,
- emCmdDataType_Str =3,
- emCmdDataType_IP =4,
- emCmdDataType_MAC =5,
-
- }EM_CMD_DATA_TYPE;
- typedef struct cmd_para
- {
- struct cmd_para *pNext;
- const char *pName;
- void *pDataAddr;
- uint16_t dataLen;
- EM_CMD_DATA_TYPE dataType;
- uint8_t EnableState;
- }ST_CMD_PARA;
- typedef struct cmd_node
- {
- const char *pKeyword;
- const char *pHelp;
- ST_CMD_PARA *pData;
- ST_FUN_NODE *pFuntion;
- FUN_STOR_BK StorageFun;
- }ST_CMD_NODE;
[color=rgb(51, 102, 153) !important]复制代码
参数注册配置实例如下:
- void cla_parametersConfig(void)
- {
- cla_init();
- cla_regsister("uuid","uuid ");
- cla_addParameter("uuid","uuid ",(void *)&uuid,emCmdDataType_Str, sizeof(uuid));
- cla_regsister("wifi","wifi or wifi ssid=xxx password=xxx");
- cla_addParameter("wifi","ssid",(void *)ssid,emCmdDataType_Str, sizeof(ssid));
- cla_addParameter("wifi","password",(void *)password,emCmdDataType_Str, sizeof(password));
- cla_regsister("hex","hex or hex hex8=xx hex16=xx hex32=xx");
- cla_addParameter("hex","hex8",(void *)&hex8,emCmdDataType_Hex, sizeof(data8));
- cla_addParameter("hex","hex16",(void *)&hex16,emCmdDataType_Hex, sizeof(data16));
- cla_addParameter("hex","hex32",(void *)&hex32,emCmdDataType_Hex, sizeof(data32));
-
- cla_regsister("data","data or data data8=xx data16=xx data32=xx");
- cla_addParameter("data","data8",(void *)&data8,emCmdDataType_Dec, sizeof(data8));
- cla_addParameter("data","data16",(void *)&data16,emCmdDataType_Dec, sizeof(data16));
- cla_addParameter("data","data32",(void *)&data32,emCmdDataType_Dec, sizeof(data32));
- cla_regsister("ipconfig","ipconfig or ipconfig ipaddr=192.168.1.1");
- cla_addParameter("ipconfig","ipaddr",(void *)IPaddr,emCmdDataType_IP, sizeof(IPaddr));
- cla_addParameter("ipconfig","mask",(void *)maskIP,emCmdDataType_IP, sizeof(maskIP));
- cla_addParameter("ipconfig","dns",(void *)dnsIP,emCmdDataType_IP, sizeof(dnsIP));
- cla_addParameter("ipconfig","mac",(void *)Mac,emCmdDataType_MAC, sizeof(Mac));
- cla_regsister("run","");
- cla_addRunFun("run",ptest_3fun_help,"test",test_3fun,3);
- cla_addRunFun("run",ptest_2fun_help,"test2",test_2fun,2);
- }
[color=rgb(51, 102, 153) !important]复制代码
命令解析的进程如下:
- void cla_precess(uint8_t *pCmd,uint16_t cmdLen,FUN_PRINT_OUT printFun)
- {
- uint16_t i,outlen;
- FUN_STOR_BK storageFun=NULL;
- BOOLEAN res;
- static ST_CMD_STR cmdStr;
- static char outBuff[OUT_BUFF_MAX_LEN];
- if((pCmd !=NULL)&&(cmdLen > 2))
- {
- cla_cmdMake(&cmdStr,pCmd,cmdLen);
- if((cla_CheckIn(&cmdStr,printFun))&&(cmdStr.argCount >= 1))
- {
- for(i=0;i
- {
- if(CmdNodeGroup.pKeyword != NULL)
- {
- if(strcmp((const char *)CmdNodeGroup.pKeyword,cmdStr.pArgValue[0]) == 0)
- {
- if(cmdStr.argCount == 1)//查询参数
- {
- outlen =cla_sprintfPara(CmdNodeGroup.pData,outBuff,OUT_BUFF_MAX_LEN);
- if(printFun !=NULL)
- {
- printFun(outBuff,outlen);
- }
- Debug("out done\n");
- }
- else if(strcmp("run",cmdStr.pArgValue[0]) == 0)
- {
- cmdStr.argPos =1;
- res =cla_runFuntion(CmdNodeGroup.pFuntion,&cmdStr);
- Debug("run done\n");
- }
- else
- {
- cmdStr.argPos =1;
- res =cla_scanfPara(CmdNodeGroup.pData,&cmdStr);
- if(res)
- {
- Debug("cfg done\n");
- storageFun =CmdNodeGroup.StorageFun;
- if(storageFun !=NULL)
- {
- storageFun();
- }
- }
- }
- return ;
- }
- else if(strcmp("help",cmdStr.pArgValue[0]) == 0)
- {
- cla_help(printFun,outBuff,OUT_BUFF_MAX_LEN);
- return ;
- }
- }
- }
- }
- }
- }
|
|