hv_comm_shell.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. * @file hv_comm_shell.h
  3. * @brief Header file of shell.
  4. *
  5. * @verbatim
  6. * ==============================================================================
  7. * ##### How to use #####
  8. * ==============================================================================
  9. * (+)
  10. * @endverbatim
  11. * @author HiView SoC Software Team
  12. * @version 1.0.0
  13. * @date 2022-08-18
  14. */
  15. #ifndef __HV_COMM_SHELL_H__
  16. #define __HV_COMM_SHELL_H__
  17. #include "hv_vos_Comm.h"
  18. #include "hv_comm_shell_getopt.h"
  19. #include "hv_comm_shell_cfg.h"
  20. #include "hv_comm_shell_port.h"
  21. #include "hv_comm_Log.h"
  22. #if SHELL_USING_AUTH == 1
  23. #if !defined(SHELL_USER_PASSWORD)
  24. #error "please config shell user password (int shell_cfg.h) "
  25. #endif
  26. #endif
  27. #define SHELL_VERSION "2.0.8" /**< 版本号 */
  28. /**
  29. * @brief shell变量类型定义
  30. *
  31. */
  32. #define SHELL_VAR_INT 0
  33. #define SHELL_VAR_SHORT 1
  34. #define SHELL_VAR_CHAR 2
  35. #define SHELL_VAR_POINTER 3
  36. #define SHELL_VAL 4
  37. #if defined(__CC_ARM) || (defined(__ARMCC_VERSION) && __ARMCC_VERSION >= 6000000)
  38. #define SECTION(x) __attribute__((section(x)))
  39. #elif defined(__ICCARM__)
  40. #define SECTION(x) @ x
  41. #elif defined(__GNUC__)
  42. #define SECTION(x) __attribute__((section(x)))
  43. #else
  44. #define SECTION(x)
  45. #endif
  46. /**
  47. * @brief shell命令导出
  48. *
  49. * @attention 命令导出方式支持keil,iar的编译器以及gcc,具体参考readme
  50. */
  51. #if SHELL_USING_CMD_EXPORT == 1
  52. #if SHELL_LONG_HELP == 1
  53. #define SHELL_EXPORT_CMD(cmd, func, desc) \
  54. const char shellCmd##cmd[] = #cmd; \
  55. const char shellDesc##cmd[] = #desc; \
  56. const SHELL_CommandTypeDef \
  57. shellCommand##cmd SECTION("shellCommand") = \
  58. { \
  59. shellCmd##cmd, \
  60. (int (*)())func, \
  61. shellDesc##cmd, \
  62. (void *)0 \
  63. }
  64. #define SHELL_EXPORT_CMD_EX(cmd, func, desc, help) \
  65. const char shellCmd##cmd[] = #cmd; \
  66. const char shellDesc##cmd[] = #desc; \
  67. const char shellHelp##cmd[] = #help; \
  68. const SHELL_CommandTypeDef \
  69. shellCommand##cmd SECTION("shellCommand") = \
  70. { \
  71. shellCmd##cmd, \
  72. (int (*)())func, \
  73. shellDesc##cmd, \
  74. shellHelp##cmd \
  75. }
  76. #else /** SHELL_LONG_HELP == 1 */
  77. #define SHELL_EXPORT_CMD(cmd, func, desc) \
  78. const char shellCmd##cmd[] = #cmd; \
  79. const char shellDesc##cmd[] = #desc; \
  80. const SHELL_CommandTypeDef \
  81. shellCommand##cmd SECTION("shellCommand") = \
  82. { \
  83. shellCmd##cmd, \
  84. (int (*)())func, \
  85. shellDesc##cmd \
  86. }
  87. #define SHELL_EXPORT_CMD_EX(cmd, func, desc, help) \
  88. const char shellCmd##cmd[] = #cmd; \
  89. const char shellDesc##cmd[] = #desc; \
  90. const SHELL_CommandTypeDef \
  91. shellCommand##cmd SECTION("shellCommand") = \
  92. { \
  93. shellCmd##cmd, \
  94. (int (*)())func, \
  95. shellDesc##cmd \
  96. }
  97. #endif /** SHELL_LONG_HELP == 1 */
  98. #if SHELL_USING_VAR == 1
  99. #define SHELL_EXPORT_VAR(var, variable, desc, type) \
  100. const char shellVar##var[] = #var; \
  101. const char shellDesc##var[] = #desc; \
  102. const SHELL_VaribaleTypeDef \
  103. shellVariable##var SECTION("shellVariable") = \
  104. { \
  105. shellVar##var, \
  106. (void *)(variable), \
  107. shellDesc##var, \
  108. type \
  109. }
  110. #else
  111. #define SHELL_EXPORT_VAR(var, variable, desc, type)
  112. #endif /** SHELL_USING_VAR == 1 */
  113. #else
  114. #define SHELL_EXPORT_CMD(cmd, func, desc)
  115. #define SHELL_EXPORT_CMD_EX(cmd, func, desc, help)
  116. #define SHELL_EXPORT_VAR(var, variable, desc, type)
  117. #endif /** SHELL_USING_CMD_EXPORT == 1 */
  118. #define SHELL_EXPORT_VAR_INT(var, variable, desc) \
  119. SHELL_EXPORT_VAR(var, &variable, desc, SHELL_VAR_INT)
  120. #define SHELL_EXPORT_VAR_SHORT(var, variable, desc) \
  121. SHELL_EXPORT_VAR(var, &variable, desc, SHELL_VAR_SHORT)
  122. #define SHELL_EXPORT_VAR_CHAR(var, variable, desc) \
  123. SHELL_EXPORT_VAR(var, &variable, desc, SHELL_VAR_CHAR)
  124. #define SHELL_EXPORT_VAR_POINTER(var, variable, desc) \
  125. SHELL_EXPORT_VAR(var, variable, desc, SHELL_VAR_POINTER)
  126. #define SHELL_EXPORT_VAL(val, value, desc) \
  127. SHELL_EXPORT_VAR(val, value, desc, SHELL_VAL)
  128. /**
  129. * @brief shell命令条目
  130. *
  131. * @note 用于shell命令通过命令表的方式定义
  132. */
  133. #if SHELL_USING_CMD_EXPORT == 0
  134. #if SHELL_LONG_HELP == 1
  135. #define SHELL_CMD_ITEM(cmd, func, desc) \
  136. { \
  137. #cmd, \
  138. (int (*)())func, \
  139. #desc, \
  140. (void *)0 \
  141. }
  142. #define SHELL_CMD_ITEM_EX(cmd, func, desc, help) \
  143. { \
  144. #cmd, \
  145. (int (*)())func, \
  146. #desc, \
  147. #help \
  148. }
  149. #else /** SHELL_LONG_HELP == 1 */
  150. #define SHELL_CMD_ITEM(cmd, func, desc) \
  151. { \
  152. #cmd, \
  153. (int (*)())func, \
  154. #desc \
  155. }
  156. #define SHELL_CMD_ITEM_EX(cmd, func, desc, help) \
  157. { \
  158. #cmd, \
  159. (int (*)())func, \
  160. #desc, \
  161. }
  162. #endif /** SHELL_LONG_HELP == 1 */
  163. #define SHELL_VAR_ITEM(var, variable, desc, type) \
  164. { \
  165. #var, \
  166. varialbe, \
  167. #desc, \
  168. type, \
  169. }
  170. #define SHELL_VAR_ITEM_INT(var, variable, desc) \
  171. SHELL_VAR_ITEM(var, &variable, desc, SHELL_VAR_INT)
  172. #define SHELL_VAR_ITEM_SHORT(var, variable, desc) \
  173. SHELL_VAR_ITEM(var, &variable, desc, SHELL_VAR_SHORT)
  174. #define SHELL_VAR_ITEM_CHAR(var, variable, desc) \
  175. SHELL_VAR_ITEM(var, &variable, desc, SHELL_VAR_CHAR)
  176. #define SHELL_VAR_ITEM_POINTER(var, variable, desc) \
  177. SHELL_VAR_ITEM(var, variable, desc, SHELL_VAR_POINTER)
  178. #endif /** SHELL_USING_CMD_EXPORT == 0 */
  179. /**
  180. * @brief shell读取数据函数原型
  181. *
  182. * @param char shell读取的字符
  183. *
  184. * @return char 0 读取数据成功
  185. * @return char -1 读取数据失败
  186. */
  187. typedef signed char (*shellRead)(char *);
  188. /**
  189. * @brief shell写数据函数原型
  190. *
  191. * @param const char 需写的字符
  192. */
  193. typedef void (*shellWrite)(const char);
  194. /**
  195. * @brief shell指令执行函数原型
  196. *
  197. */
  198. typedef int (*shellFunction)();
  199. /**
  200. * @brief shell输入状态
  201. *
  202. */
  203. typedef enum
  204. {
  205. SHELL_IN_NORMAL = 0,
  206. SHELL_ANSI_ESC,
  207. SHELL_ANSI_CSI,
  208. }SHELL_InputMode;
  209. /**
  210. * @brief shell 命令定义
  211. *
  212. */
  213. typedef struct
  214. {
  215. const char *name; /**< shell命令名称 */
  216. shellFunction function; /**< shell命令函数 */
  217. const char *desc; /**< shell命令描述 */
  218. #if SHELL_LONG_HELP == 1
  219. const char *help; /**< shell长帮助信息 */
  220. #endif
  221. }SHELL_CommandTypeDef;
  222. #if SHELL_USING_VAR == 1
  223. /**
  224. * @brief shell 变量定义
  225. *
  226. */
  227. typedef struct
  228. {
  229. const char *name; /**< shell变量名称 */
  230. const void *value; /**< shell变量值 */
  231. const char *desc; /**< shell变量描述 */
  232. const int type; /**< shell变量类型 */
  233. } SHELL_VaribaleTypeDef;
  234. #endif /** SHELL_USING_VAR == 1 */
  235. /**
  236. * @brief shell对象定义
  237. *
  238. */
  239. typedef struct
  240. {
  241. char *command; /**< shell命令提示符 */
  242. char buffer[SHELL_COMMAND_MAX_LENGTH]; /**< shell命令缓冲 */
  243. unsigned short length; /**< shell命令长度 */
  244. unsigned short cursor; /**< shell光标位置 */
  245. char *param[SHELL_PARAMETER_MAX_NUMBER]; /**< shell参数 */
  246. char history[SHELL_HISTORY_MAX_NUMBER][SHELL_COMMAND_MAX_LENGTH]; /**< 历史记录 */
  247. unsigned short historyCount; /**< 历史记录数量 */
  248. short historyFlag; /**< 当前记录位置 */
  249. short historyOffset; /**< 历史记录偏移 */
  250. SHELL_CommandTypeDef *commandBase; /**< 命令表基址 */
  251. unsigned short commandNumber; /**< 命令数量 */
  252. #if SHELL_USING_VAR == 1
  253. SHELL_VaribaleTypeDef *variableBase; /**< 变量表基址 */
  254. unsigned short variableNumber; /**< 变量数量 */
  255. #endif
  256. int keyFuncBase; /**< 按键响应表基址 */
  257. unsigned short keyFuncNumber; /**< 按键响应数量 */
  258. struct
  259. {
  260. unsigned char inputMode : 2; /**< 输入模式 */
  261. unsigned char isActive: 1; /**< 是否是当前活动shell */
  262. unsigned char tabFlag : 1; /**< tab标志 */
  263. unsigned char authFlag : 1; /**< 密码标志 */
  264. } status; /**< shell状态 */
  265. shellRead read; /**< shell读字符 */
  266. shellWrite write; /**< shell写字符 */
  267. #if SHELL_LONG_HELP == 1 || (SHELL_USING_AUTH && SHELL_LOCK_TIMEOUT > 0)
  268. int activeTime; /**< shell激活时间戳 */
  269. #endif
  270. }SHELL_TypeDef;
  271. /**
  272. * @brief shell按键功能定义
  273. *
  274. */
  275. typedef struct
  276. {
  277. unsigned char keyCode; /**< shell按键键值 */
  278. void (*keyFunction)(SHELL_TypeDef *); /**< 按键响应函数 */
  279. } SHELL_KeyFunctionDef;
  280. void shellInit(SHELL_TypeDef *shell);
  281. void shellSetCommandList(SHELL_TypeDef *shell, SHELL_CommandTypeDef *base, unsigned short size);
  282. #if SHELL_USING_VAR == 1
  283. void shellSetVariableList(SHELL_TypeDef *shell, SHELL_VaribaleTypeDef *base, unsigned short size);
  284. int shellGetVariable(SHELL_TypeDef *shell, char *var);
  285. #endif /** SHELL_USING_VAR == 1 */
  286. void shellSetKeyFuncList(SHELL_TypeDef *shell, SHELL_KeyFunctionDef *base, unsigned short size);
  287. SHELL_TypeDef *shellGetCurrent(void);
  288. void shellPrint(SHELL_TypeDef *shell, char *fmt, ...);
  289. unsigned short shellDisplay(SHELL_TypeDef *shell, const char *string);
  290. void shellHandler(SHELL_TypeDef *shell, char data);
  291. #define shellInput shellHandler
  292. void shellHelp(int argc, char *argv[]);
  293. void shellClear(void);
  294. SHELL_TypeDef* GetShellObject();
  295. #if SHELL_USING_TASK == 1
  296. void Hv_Debug_ShellTaskHandle(void *param);
  297. #endif
  298. #endif