分享⼀个ESP32命令⾏解析框架
该命令⾏解析框架,可以轻松实现,读写HEX类型的数据,读写数值型数据,读写IP,MAC,字符串,运⾏函数,浮点型数据暂时不⽀持,带符号的数据暂时不⽀持。命令⾏解析框架,使⽤链表将所有注册的信息串联起来,然后每个节点node包含了,keyword,help,数据类型描述,命令执⾏的回调函数,和参数存储的回调函数。主要的操作是,已知函数变量地址,类型,和地址宽度,然后强制转换后对变量读写操作。
命令节点结构如下
01. typedef struct fun_node
02. {
03. struct fun_node *pNext;
04. const char *pHelp;
05. const char *pName;
06. uint8_t paraNum;
07. FUN_CMD_BK RunFun;
08. uint8_t EnableState;
09. }ST_FUN_NODE;wifi分享
10.
11. typedef enum
12. {
13. emCmdDataType_Dec =1,
14. emCmdDataType_Hex =2,
15. emCmdDataType_Str =3,
16. emCmdDataType_IP =4,
17. emCmdDataType_MAC =5,
18.
19. }EM_CMD_DATA_TYPE;
20.
21.
22. typedef struct cmd_para
23. {
24. struct cmd_para *pNext;
25. const char *pName;
26. void *pDataAddr;
27. uint16_t dataLen;
28. EM_CMD_DATA_TYPE dataType;
29. uint8_t EnableState;
30. }ST_CMD_PARA;
31.
32.
33. typedef struct cmd_node
34. {
35. const char *pKeyword;
36. const char *pHelp;
37. ST_CMD_PARA *pData;
38. ST_FUN_NODE *pFuntion;
39. FUN_STOR_BK StorageFun;
40. }ST_CMD_NODE;
复制代码
参数注册配置实例如下:
01. void cla_parametersConfig(void)
02. {
03. cla_init();
04. cla_regsister("uuid","uuid ");
05. cla_addParameter("uuid","uuid ",(void *)&uuid,emCmdDataType_Str, sizeof(uuid));
06.
07. cla_regsister("wifi","wifi or wifi ssid=xxx password=xxx");
08. cla_addParameter("wifi","ssid",(void *)ssid,emCmdDataType_Str, sizeof(ssid));
09. cla_addParameter("wifi","password",(void *)password,emCmdDataType_Str, sizeof(password));
10.
11. cla_regsister("hex","hex or hex hex8=xx hex16=xx hex32=xx");
12. cla_addParameter("hex","hex8",(void *)&hex8,emCmdDataType_Hex, sizeof(data8));
13. cla_addParameter("hex","hex16",(void *)&hex16,emCmdDataType_Hex, sizeof(data16));
14. cla_addParameter("hex","hex32",(void *)&hex32,emCmdDataType_Hex, sizeof(data32));
15.
16. cla_regsister("data","data or data data8=xx data16=xx data32=xx");
17. cla_addParameter("data","data8",(void *)&data8,emCmdDataType_Dec, sizeof(data8));
18. cla_addParameter("data","data16",(void *)&data16,emCmdDataType_Dec, sizeof(data16));
19. cla_addParameter("data","data32",(void *)&data32,emCmdDataType_Dec, sizeof(data32));
20.
21. cla_regsister("ipconfig","ipconfig or ipconfig ipaddr=192.168.1.1");
22. cla_addParameter("ipconfig","ipaddr",(void *)IPaddr,emCmdDataType_IP, sizeof(IPaddr));
23. cla_addParameter("ipconfig","mask",(void *)maskIP,emCmdDataType_IP, sizeof(maskIP));
24. cla_addParameter("ipconfig","dns",(void *)dnsIP,emCmdDataType_IP, sizeof(dnsIP));
25. cla_addParameter("ipconfig","mac",(void *)Mac,emCmdDataType_MAC, sizeof(Mac));
26.
27. cla_regsister("run","");
28. cla_addRunFun("run",ptest_3fun_help,"test",test_3fun,3);
29. cla_addRunFun("run",ptest_2fun_help,"test2",test_2fun,2);
30. }
复制代码
命令解析的进程如下:
01.
02. void cla_precess(uint8_t *pCmd,uint16_t cmdLen,FUN_PRINT_OUT printFun)
03. {
04. uint16_t i,outlen;
05. FUN_STOR_BK storageFun=NULL;
06. BOOLEAN res;
07. static ST_CMD_STR cmdStr;
08. static char outBuff[OUT_BUFF_MAX_LEN];
09. if((pCmd !=NULL)&&(cmdLen > 2))
10. {
11. cla_cmdMake(&cmdStr,pCmd,cmdLen);
12. if((cla_CheckIn(&cmdStr,printFun))&&(cmdStr.argCount >= 1))
13. {
14. for(i=0;i
15. {
16. if(CmdNodeGroup[i].pKeyword != NULL)
17. {
18. if(strcmp((const char *)CmdNodeGroup[i].pKeyword,cmdStr.pArgValue[0]) == 0)
19. {
20. if(cmdStr.argCount == 1)//查询参数
21. {
22. outlen
=cla_sprintfPara(CmdNodeGroup[i].pData,outBuff,OUT_BUFF_MAX_LEN);
23. if(printFun !=NULL)
24. {
25. printFun(outBuff,outlen);
26. }
27. Debug("out done\n");
28. }
29. else if(strcmp("run",cmdStr.pArgValue[0]) == 0)
30. {
31. cmdStr.argPos =1;
32. res =cla_runFuntion(CmdNodeGroup[i].pFuntion,&cmdStr);
33. Debug("run done\n");
34. }
35. else
36. {
37. cmdStr.argPos =1;
38. res =cla_scanfPara(CmdNodeGroup[i].pData,&cmdStr);
39. if(res)
40. {
41. Debug("cfg done\n");
42. storageFun =CmdNodeGroup[i].StorageFun;
43. if(storageFun !=NULL)
44. {
45. storageFun();
46. }
47. }
48. }
49. return ;
50. }
51. else if(strcmp("help",cmdStr.pArgValue[0]) == 0)
52. {
53. cla_help(printFun,outBuff,OUT_BUFF_MAX_LEN);
54. return ;
55. }
56. }
57. }
58. }
59. }
60. }
61.
62.
复制代码
发布评论