cli_main.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /**
  2. * \file cli_main.c
  3. *
  4. * This File contains the "main" function for the CLI application,
  5. * to exercise various functionalities of the Mindtree Mesh stack.
  6. */
  7. /*
  8. * Copyright (C) 2017. Mindtree Ltd.
  9. * All rights reserved.
  10. */
  11. #ifndef DEMO
  12. /* ------------------------------- Header File Inclusion */
  13. #include "cli_main.h"
  14. #include "nvs.h"
  15. #include "common.h"
  16. /* ------------------------------- Global Variables */
  17. #ifndef CLI_NO_MAIN
  18. DECL_STATIC UCHAR r_buf[1024];
  19. #else
  20. void cli_input (char * cmd, int size);
  21. #endif /* CLI_NO_MAIN */
  22. /* Level - Root */
  23. DECL_CONST CLI_COMMAND cli_root_cmd_list[] =
  24. {
  25. /* Help */
  26. { "help", "Help", cli_help },
  27. /* Core */
  28. { "core", "Core Options", cli_core },
  29. /* Model */
  30. { "model", "Model Options", cli_model },
  31. /* Reset */
  32. { "reset", "Reset Node", cli_reset },
  33. /* Persistent Storage */
  34. { "ps", "Persistent Storage", cli_ps },
  35. /* Mesh ADV/GATT bearer Operations */
  36. { "brr", "Mesh bearer Operations", cli_brr },
  37. /* Set Log Level */
  38. { "loglevel", "Set Log Level <ERR/INF/TRC/ALL>", cli_set_log_level }
  39. };
  40. /* Command List Stack */
  41. typedef struct _CLI_COMMAND_STACK_FRAME
  42. {
  43. CLI_COMMAND * cmd_list;
  44. UINT16 cmd_list_len;
  45. } CLI_COMMAND_STACK_FRAME;
  46. #define CLI_CMD_LIST_MAX_DEPTH 10
  47. DECL_STATIC UINT16 cli_cmd_list_sp;
  48. DECL_STATIC CLI_COMMAND_STACK_FRAME cli_cmd_list_stack[CLI_CMD_LIST_MAX_DEPTH];
  49. /* ------------------------------- Functions */
  50. /* Common Utility Routines */
  51. void cli_cmd_stack_push(/* IN */ CLI_COMMAND *cmd_list, /* IN */ UINT16 cmd_list_len)
  52. {
  53. cli_cmd_list_sp++;
  54. cli_cmd_list_stack[cli_cmd_list_sp].cmd_list = cmd_list;
  55. cli_cmd_list_stack[cli_cmd_list_sp].cmd_list_len = cmd_list_len;
  56. }
  57. void cli_cmd_stack_pop(void)
  58. {
  59. /* Check if the Stack point in not 0. Decrement by one */
  60. if (0 != cli_cmd_list_sp)
  61. {
  62. cli_cmd_list_sp--;
  63. }
  64. else
  65. {
  66. CONSOLE_OUT("Already at Root Level\n");
  67. }
  68. }
  69. API_RESULT cli_reset(UINT32 argc, UCHAR *argv[])
  70. {
  71. MS_IGNORE_UNUSED_PARAM(argc);
  72. MS_IGNORE_UNUSED_PARAM(argv);
  73. CONSOLE_OUT ("Clearing Storage\n");
  74. nvs_reset(NVS_BANK_PERSISTENT);
  75. return API_SUCCESS;
  76. }
  77. extern uint8 llState;
  78. extern uint8 llSecondaryState;
  79. extern UCHAR blebrr_state;
  80. extern uint32 blebrr_advscan_timeout_count;
  81. /* Help */
  82. API_RESULT cli_help(UINT32 argc, UCHAR *argv[])
  83. {
  84. UINT32 index;
  85. UINT32 sum = 0;
  86. MS_IGNORE_UNUSED_PARAM(argc);
  87. MS_IGNORE_UNUSED_PARAM(argv);
  88. CONSOLE_OUT("In Help\n");
  89. /* Print all the available commands */
  90. for (index = 0; index < cli_cmd_list_stack[cli_cmd_list_sp].cmd_list_len; index++)
  91. {
  92. CONSOLE_OUT(" %s: %s\n",
  93. cli_cmd_list_stack[cli_cmd_list_sp].cmd_list[index].cmd,
  94. cli_cmd_list_stack[cli_cmd_list_sp].cmd_list[index].desc);
  95. }
  96. sum = osal_memory_statics();
  97. printf("osal_memory sum = %d\r\n", sum);
  98. printf("\r\n===== internal status ============\r\n");
  99. printf("llState = %d, llSecondaryState = %d\r\n", llState, llSecondaryState);
  100. printf("blebrr_state = %d\r\n", blebrr_state);
  101. printf("blebrr_advscan_timeout_count = %d\r\n", blebrr_advscan_timeout_count);
  102. return API_SUCCESS;
  103. }
  104. /* Back */
  105. API_RESULT cli_back(UINT32 argc, UCHAR *argv[])
  106. {
  107. CONSOLE_OUT("Going Back \n");
  108. cli_cmd_stack_pop();
  109. /* Print Help of the new level */
  110. cli_help(argc, argv);
  111. return API_SUCCESS;
  112. }
  113. /* Root */
  114. API_RESULT cli_root(UINT32 argc, UCHAR *argv[])
  115. {
  116. CONSOLE_OUT("Level - Root\n");
  117. /* Set Stack Pointer to Root */
  118. cli_cmd_list_sp = 0;
  119. cli_cmd_list_stack[cli_cmd_list_sp].cmd_list = (CLI_COMMAND *)cli_root_cmd_list;
  120. cli_cmd_list_stack[cli_cmd_list_sp].cmd_list_len = sizeof(cli_root_cmd_list) / sizeof(CLI_COMMAND);
  121. /* Print Help of the Root level */
  122. cli_help(argc, argv);
  123. return API_SUCCESS;
  124. }
  125. /* Log Level */
  126. API_RESULT cli_set_log_level(UINT32 argc, UCHAR *argv[])
  127. {
  128. UINT32 index;
  129. UCHAR * log_levels[4] = { "ERR", "TRC", "INF", "ALL" };
  130. CONSOLE_OUT("In Set Log Level\n");
  131. if (1 == argc)
  132. {
  133. for (index = 0; index < 4; index++)
  134. {
  135. if (0 == CLI_STR_COMPARE(argv[0], log_levels[index]))
  136. {
  137. break;
  138. }
  139. }
  140. if (index > 3)
  141. {
  142. CONSOLE_OUT("Invalid Argument:%s\n", argv[0]);
  143. return API_FAILURE;
  144. }
  145. if (index < 3)
  146. {
  147. index += 1;
  148. }
  149. EM_set_debug_level((UCHAR)index);
  150. CONSOLE_OUT("Set Log Level:0x%02X\n", (UCHAR)index);
  151. }
  152. else
  153. {
  154. CONSOLE_OUT("Invalid Number of Arguments:0x%04X\n", argc);
  155. }
  156. return API_SUCCESS;
  157. }
  158. /* Module related Utility Routines */
  159. void cli_gatt_bearer_iface_event_pl_cb
  160. (
  161. UCHAR ev_name,
  162. UCHAR ev_param
  163. )
  164. {
  165. switch(ev_name)
  166. {
  167. /* GATT Bearer BLE Link Layer Disconnected */
  168. case BLEBRR_GATT_IFACE_DOWN:
  169. CONSOLE_OUT("\r\n >> GATT Bearer BLE Link Layer Disconnection Event Received!\r\n");
  170. CONSOLE_OUT("Invoke relevant CLI Commands! \r\n");
  171. break;
  172. /* GATT Bearer BLE Link Layer Connected */
  173. case BLEBRR_GATT_IFACE_UP:
  174. CONSOLE_OUT("\r\n >> GATT Bearer BLE Link Layer Connection Event Received!\r\n");
  175. /**
  176. * TODO:
  177. * Check if this only when DUT is Unprovisioned Device!
  178. * also check if this needs to be done.
  179. */
  180. MS_prov_stop_interleave_timer();
  181. MS_brr_bcast_end(BRR_BCON_TYPE_UNPROV_DEVICE, BRR_BCON_ACTIVE);
  182. break;
  183. /* GATT Bearer Service Enabled for Communication */
  184. case BLEBRR_GATT_IFACE_ENABLE:
  185. CONSOLE_OUT("\r\n >> GATT Bearer Active Event Received!\r\n");
  186. {
  187. if (BLEBRR_GATT_PROV_MODE == ev_param)
  188. {
  189. /* Call to bind with the selected device */
  190. /**
  191. * TODO:
  192. * This needs to be made role Specific.
  193. * For Provisioner role, this below function needs to be
  194. * updated. Typically for Provisioner Role, "Bind" needs
  195. * to happen from the CLI command pointing to the desired
  196. * PROV_DEVICE_S of Remote Unprovisioned Device.
  197. */
  198. appl_prov_bind_device(PROV_BRR_GATT);
  199. }
  200. }
  201. break;
  202. /* GATT Bearer Service Disabled for Communication */
  203. case BLEBRR_GATT_IFACE_DISABLE:
  204. CONSOLE_OUT("\r\n >> GATT Bearer Inactive Event Received!\r\n");
  205. CONSOLE_OUT("Invoke relevant CLI Commands! \r\n");
  206. break;
  207. /* Unknown Event! */
  208. default:
  209. CONSOLE_OUT("\r\n >> GATT Bearer BLE Link Layer Unknown Event 0x%02X Received!\r\n", ev_name);
  210. break;
  211. }
  212. }
  213. #ifndef CLI_NO_MAIN
  214. int main (int argc, char **argv)
  215. {
  216. MS_IGNORE_UNUSED_PARAM(argc);
  217. MS_IGNORE_UNUSED_PARAM(argv);
  218. /* Initialize CLI */
  219. CLI_init();
  220. /* Set Root Command List and Length */
  221. cli_root(0, NULL);
  222. MS_LOOP_FOREVER()
  223. {
  224. /* */
  225. fgets(r_buf, sizeof(r_buf), stdin);
  226. CLI_process_line
  227. (
  228. r_buf,
  229. CLI_strlen(r_buf),
  230. cli_cmd_list_stack[cli_cmd_list_sp].cmd_list,
  231. cli_cmd_list_stack[cli_cmd_list_sp].cmd_list_len
  232. );
  233. }
  234. return 0;
  235. }
  236. #else /* CLI_NO_MAIN */
  237. void cli_input (char * cmd, int size)
  238. {
  239. CLI_process_line
  240. (
  241. (UCHAR *) cmd,
  242. size,
  243. cli_cmd_list_stack[cli_cmd_list_sp].cmd_list,
  244. cli_cmd_list_stack[cli_cmd_list_sp].cmd_list_len
  245. );
  246. }
  247. #endif /* CLI_NO_MAIN */
  248. #endif /* DEMO */