cli_demo.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /**
  2. * \file cli_demo.c
  3. *
  4. * This File contains the "main" function for the Demo application.
  5. */
  6. /*
  7. * Copyright (C) 2017. Mindtree Ltd.
  8. * All rights reserved.
  9. */
  10. #ifdef DEMO
  11. /* ------------------------------- Header File Inclusion */
  12. #include "MS_access_api.h"
  13. #include "MS_config_api.h"
  14. #include "MS_generic_onoff_api.h"
  15. #include "cliface.h"
  16. #include "blebrr.h"
  17. /* ------------------------------- Global Variables */
  18. /* Console Input/Output */
  19. #define CONSOLE_OUT(...) printf(__VA_ARGS__)
  20. #define CONSOLE_IN(...) scanf(__VA_ARGS__)
  21. /* Macro to declare a CLI Command Handler */
  22. #define CLI_CMD_HANDLER_DECL(x) \
  23. API_RESULT (x) (UINT32 arc, UCHAR * arv[])
  24. /* Macro to define an empty CLI Command Handler */
  25. #define CLI_CMD_HANDLER_EMPTY_DEF(x) \
  26. API_RESULT (x) (UINT32 argc, UCHAR * argv[]) \
  27. { \
  28. UINT32 index; \
  29. \
  30. CLI_APP_TRC(#x ". Argc %d\n", argc); \
  31. \
  32. for (index = 0; index < argc; index++) \
  33. { \
  34. CLI_APP_TRC("Argv[%d]: %s\n", index, argv[index]); \
  35. } \
  36. \
  37. return API_SUCCESS; \
  38. }
  39. /* CLI Command Handler declaration for configuration client */
  40. CLI_CMD_HANDLER_DECL( cli_help );
  41. CLI_CMD_HANDLER_DECL( cli_provisioning_mode_adv_set );
  42. CLI_CMD_HANDLER_DECL( cli_provisioning_mode_gatt_set );
  43. CLI_CMD_HANDLER_DECL( cli_proxy_mode_set );
  44. CLI_CMD_HANDLER_DECL( cli_provisioner_mode_set );
  45. CLI_CMD_HANDLER_DECL( cli_start_provisioning );
  46. CLI_CMD_HANDLER_DECL( cli_clear_persistent );
  47. CLI_CMD_HANDLER_DECL( cli_switchon );
  48. CLI_CMD_HANDLER_DECL( cli_switchoff );
  49. /* CLI Server command table */
  50. DECL_STATIC CLI_COMMAND cli_server_cmd_list[] =
  51. {
  52. /* Help */
  53. { "help", "Help Menu", cli_help },
  54. /* Set in provisioning mode */
  55. { "provadv", "Device for Provisioning over ADV", cli_provisioning_mode_adv_set },
  56. { "provgatt", "Device for Provisioning over GATT", cli_provisioning_mode_gatt_set },
  57. /* Switch to Proxy */
  58. { "proxy", "Device for Proxy", cli_proxy_mode_set},
  59. #ifdef DEMO_CLIENT
  60. { "on", "Switch ON Light", cli_switchon},
  61. { "off", "Switch OFF Light", cli_switchoff},
  62. #endif /* DEMO_CLIENT */
  63. /* Clear the Mesh Database from persistent store */
  64. { "reset", "Clear Persistent storage", cli_clear_persistent }
  65. };
  66. /* Debug Macros */
  67. /* TBD: Mapped with debug sub-system */
  68. #define CLI_APP_ERR(...) printf(__VA_ARGS__)
  69. #define CLI_APP_TRC(...) printf(__VA_ARGS__)
  70. #define CLI_APP_INF(...) printf(__VA_ARGS__)
  71. /* TODO: Remove */
  72. /* Current Test Case ID */
  73. UINT32 appl_mesh_test_id;
  74. #ifdef DEMO_CLIENT
  75. static MS_ACCESS_MODEL_HANDLE appl_generic_onoff_client_model_handle;
  76. API_RESULT appl_generic_onoff_client_cb
  77. (
  78. /* IN */ MS_ACCESS_MODEL_HANDLE * handle,
  79. /* IN */ UINT32 opcode,
  80. /* IN */ UCHAR * data_param,
  81. /* IN */ UINT16 data_len
  82. );
  83. #endif /* DEMO_CLIENT */
  84. /* ------------------------------- Functions */
  85. void cli_input (char * cmd, int size)
  86. {
  87. CLI_process_line
  88. (
  89. cmd,
  90. size,
  91. cli_server_cmd_list,
  92. (sizeof(cli_server_cmd_list) / sizeof(CLI_COMMAND))
  93. );
  94. }
  95. /* CLI Command Handler Defines */
  96. API_RESULT cli_help(UINT32 argc, UCHAR *argv[])
  97. {
  98. UINT32 index;
  99. /* Print all the available commands */
  100. for (index = 0; index < (sizeof(cli_server_cmd_list) / sizeof(CLI_COMMAND)); index++)
  101. {
  102. CONSOLE_OUT(" %s\n", cli_server_cmd_list[index].cmd);
  103. }
  104. return API_SUCCESS;
  105. }
  106. API_RESULT cli_demo_init(void)
  107. {
  108. MS_ACCESS_NODE_ID node_id;
  109. MS_ACCESS_ELEMENT_HANDLE element_handle;
  110. API_RESULT retval;
  111. /* Create Node */
  112. retval = MS_access_create_node(&node_id);
  113. if (API_SUCCESS == retval)
  114. {
  115. /* Register the Models */
  116. main_register_models();
  117. #ifndef DEMO_CLIENT
  118. /* Initialize Generic ON/OFF Server */
  119. main_generic_onoff_server_operations(MS_FALSE);
  120. #else /* DEMO_CLIENT */
  121. cli_modelc_generic_onoff_setup(0, NULL);
  122. #endif /* DEMO_CLIENT */
  123. }
  124. CONSOLE_OUT("Model Registration Status: 0x%04X\n", retval);
  125. return API_SUCCESS;
  126. }
  127. API_RESULT cli_provisioning_mode_adv_set(UINT32 argc, UCHAR *argv[])
  128. {
  129. appl_prov_register();
  130. appl_prov_setup(PROV_ROLE_DEVICE, PROV_BRR_ADV);
  131. return API_SUCCESS;
  132. }
  133. API_RESULT cli_provisioning_mode_gatt_set(UINT32 argc, UCHAR *argv[])
  134. {
  135. appl_prov_register();
  136. /* Set the role to Prov with bearer */
  137. blebrr_gatt_mode_set(BLEBRR_GATT_PROV_MODE);
  138. appl_prov_setup(PROV_ROLE_DEVICE, PROV_BRR_GATT);
  139. return API_SUCCESS;
  140. }
  141. API_RESULT cli_proxy_mode_set(UINT32 argc, UCHAR *argv[])
  142. {
  143. appl_proxy_register();
  144. appl_proxy_start_net_id_adv();
  145. return API_SUCCESS;
  146. }
  147. void appl_prov_register(void);
  148. void appl_prov_setup_provisioner(void);
  149. API_RESULT cli_provisioner_mode_set(UINT32 argc, UCHAR *argv[])
  150. {
  151. appl_prov_register();
  152. appl_prov_setup_provisioner();
  153. return API_SUCCESS;
  154. }
  155. void appl_prov_start_provisioning(void);
  156. API_RESULT cli_start_provisioning(UINT32 argc, UCHAR *argv[])
  157. {
  158. appl_prov_start_provisioning();
  159. return API_SUCCESS;
  160. }
  161. API_RESULT cli_config_gatt_proxy_get(UINT32 argc, UCHAR *argv[])
  162. {
  163. return MS_config_client_gatt_proxy_get();
  164. }
  165. API_RESULT cli_config_gatt_proxy_set(UINT32 argc, UCHAR *argv[])
  166. {
  167. ACCESS_CONFIG_GATT_PROXY_SET_PARAM proxy;
  168. proxy.proxy = 0x01;
  169. return MS_config_client_gatt_proxy_set(&proxy);
  170. }
  171. API_RESULT cli_clear_persistent(UINT32 argc, UCHAR *argv[])
  172. {
  173. nvs_reset();
  174. printf ("Persistent database Reset Success!");
  175. /* appl_set_prov_state(false); */
  176. return API_SUCCESS;
  177. }
  178. API_RESULT cli_switchon(UINT32 argc, UCHAR *argv[])
  179. {
  180. API_RESULT retval;
  181. MS_GENERIC_ONOFF_SET_STRUCT param;
  182. param.onoff= 0x01;
  183. param.tid= 0;
  184. param.optional_fields_present = 0x00;
  185. printf ("Switch ON!");
  186. retval = MS_generic_onoff_set_unacknowledged(&param);
  187. printf ("Retval = 0x%04X\n", retval);
  188. return API_SUCCESS;
  189. }
  190. API_RESULT cli_switchoff(UINT32 argc, UCHAR *argv[])
  191. {
  192. API_RESULT retval;
  193. MS_GENERIC_ONOFF_SET_STRUCT param;
  194. param.onoff= 0x00;
  195. param.tid= 0;
  196. param.optional_fields_present = 0x00;
  197. printf ("Switch OFF!");
  198. retval = MS_generic_onoff_set_unacknowledged(&param);
  199. printf ("Retval = 0x%04X\n", retval);
  200. return API_SUCCESS;
  201. }
  202. void appl_indicate_provisioning_state (void)
  203. {
  204. MS_NET_ADDR addr;
  205. MS_access_cm_get_primary_unicast_address (&addr);
  206. if (0x0000 == addr)
  207. {
  208. printf ("Device Not Provisioned!");
  209. /* Put device in provisioning over GATT */
  210. cli_provisioning_mode_gatt_set(0, NULL);
  211. }
  212. else
  213. {
  214. printf ("Device Provisioned!");
  215. /* Put device in Proxy over GATT */
  216. /* cli_proxy_mode_set(0, NULL); */
  217. }
  218. }
  219. #endif /* DEMO */