appl_sample_example_1.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. /**
  2. * \file appl_sample_example_1.c
  3. *
  4. * Source File for Generic OnOff Server Standalone application without CLI or
  5. * menu based console input interface.
  6. */
  7. /*
  8. * Copyright (C) 2018. Mindtree Ltd.
  9. * All rights reserved.
  10. */
  11. #if (MESH_STANDALONE == 1)
  12. /* ----------------------------------------- Header File Inclusion */
  13. #include "MS_common.h"
  14. #include "MS_access_api.h"
  15. #include "MS_config_api.h"
  16. #include "MS_health_server_api.h"
  17. #include "MS_generic_onoff_api.h"
  18. #include "blebrr.h"
  19. #include "nvsto.h"
  20. #include "model_state_handler_pl.h"
  21. /* Console Input/Output */
  22. #define CONSOLE_OUT(...) printf(__VA_ARGS__)
  23. #define CONSOLE_IN(...) scanf(__VA_ARGS__)
  24. void appl_dump_bytes(UCHAR *buffer, UINT16 length);
  25. void appl_mesh_sample (void);
  26. /* ----------------------------------------- External Global Variables */
  27. /* ----------------------------------------- Exported Global Variables */
  28. /* ----------------------------------------- Static Global Variables */
  29. /* ----------------------------------------- Functions */
  30. /* Model Server - Foundation Models */
  31. /* Health Server - Test Routines */
  32. static void UI_health_self_test_00(UINT8 test_id, UINT16 company_id)
  33. {
  34. }
  35. static void UI_health_self_test_01(UINT8 test_id, UINT16 company_id)
  36. {
  37. }
  38. static void UI_health_self_test_FF(UINT8 test_id, UINT16 company_id)
  39. {
  40. }
  41. /* List of Self Tests */
  42. static MS_HEALTH_SERVER_SELF_TEST UI_health_server_self_tests[] =
  43. {
  44. {
  45. 0x00, /* Test ID: 0x00 */
  46. UI_health_self_test_00
  47. },
  48. {
  49. 0x01, /* Test ID: 0x01 */
  50. UI_health_self_test_01
  51. },
  52. {
  53. 0xFF, /* Test ID: 0xFF */
  54. UI_health_self_test_FF
  55. }
  56. };
  57. /**
  58. * \brief Health Server application Asynchronous Notification Callback.
  59. *
  60. * \par Description
  61. * Health Server calls the registered callback to indicate events occurred to the
  62. * application.
  63. *
  64. * \param handle Model Handle.
  65. * \param event_type Health Server Event type.
  66. * \param event_param Parameter associated with the event if any or NULL.
  67. * \param param_len Size of the event parameter data. 0 if event param is NULL.
  68. */
  69. static API_RESULT UI_health_server_cb
  70. (
  71. MS_ACCESS_MODEL_HANDLE * handle,
  72. UINT8 event_type,
  73. UINT8 * event_param,
  74. UINT16 param_len
  75. )
  76. {
  77. CONSOLE_OUT(
  78. "Health Server Callback. Not handled. Returning\n");
  79. return API_SUCCESS;
  80. }
  81. static API_RESULT UI_register_foundation_model_servers
  82. (
  83. MS_ACCESS_ELEMENT_HANDLE element_handle
  84. )
  85. {
  86. /* Configuration Server */
  87. MS_ACCESS_MODEL_HANDLE UI_config_server_model_handle;
  88. MS_ACCESS_MODEL_HANDLE UI_health_server_model_handle;
  89. API_RESULT retval;
  90. /* Health Server */
  91. UINT16 company_id;
  92. MS_HEALTH_SERVER_SELF_TEST * self_tests;
  93. UINT32 num_self_tests;
  94. CONSOLE_OUT("In Model Server - Foundation Models\n");
  95. retval = MS_config_server_init(element_handle, &UI_config_server_model_handle);
  96. CONSOLE_OUT("Config Model Server Registration Status: 0x%04X\n", retval);
  97. /* Health Server */
  98. company_id = MS_DEFAULT_COMPANY_ID;
  99. self_tests = &UI_health_server_self_tests[0];
  100. num_self_tests = sizeof(UI_health_server_self_tests)/sizeof(MS_HEALTH_SERVER_SELF_TEST);
  101. retval = MS_health_server_init
  102. (
  103. element_handle,
  104. &UI_health_server_model_handle,
  105. company_id,
  106. self_tests,
  107. num_self_tests,
  108. UI_health_server_cb
  109. );
  110. if (API_SUCCESS == retval)
  111. {
  112. CONSOLE_OUT(
  113. "Health Server Initialized. Model Handle: 0x%04X\n",
  114. UI_health_server_model_handle);
  115. }
  116. else
  117. {
  118. CONSOLE_OUT(
  119. "[ERR] Sensor Server Initialization Failed. Result: 0x%04X\n",
  120. retval);
  121. }
  122. return retval;
  123. }
  124. /* ---- Generic OnOff States and Get/Set Handlers */
  125. static MS_STATE_GENERIC_ONOFF_STRUCT UI_generic_onoff;
  126. /* Generic OnOff Model state Initialization */
  127. static void UI_generic_onoff_model_states_initialization(void)
  128. {
  129. EM_mem_set(&UI_generic_onoff, 0, sizeof(UI_generic_onoff));
  130. }
  131. /* Generic OnOff Model Get Handler */
  132. static API_RESULT UI_generic_onoff_model_state_get(UINT16 state_t, UINT16 state_inst, void * param, UINT8 direction)
  133. {
  134. API_RESULT retval;
  135. retval = API_SUCCESS;
  136. switch(state_t)
  137. {
  138. case MS_STATE_GENERIC_ONOFF_T:
  139. {
  140. MS_STATE_GENERIC_ONOFF_STRUCT * param_p;
  141. param_p = (MS_STATE_GENERIC_ONOFF_STRUCT *)param;
  142. /* Ignoring Instance and direction right now */
  143. *param_p = UI_generic_onoff;
  144. }
  145. break;
  146. default:
  147. break;
  148. }
  149. return retval;
  150. }
  151. /* Generic OnOff Model Set Handler */
  152. static API_RESULT UI_generic_onoff_model_state_set(UINT16 state_t, UINT16 state_inst, void * param, UINT8 direction)
  153. {
  154. API_RESULT retval;
  155. retval = API_SUCCESS;
  156. switch (state_t)
  157. {
  158. case MS_STATE_GENERIC_ONOFF_T:
  159. {
  160. MS_STATE_GENERIC_ONOFF_STRUCT * param_p;
  161. param_p = (MS_STATE_GENERIC_ONOFF_STRUCT *)param;
  162. /* Instantaneous Change */
  163. UI_generic_onoff.onoff = param_p->onoff;
  164. *param_p = UI_generic_onoff;
  165. CONSOLE_OUT("[state] current: 0x%02X\n", UI_generic_onoff.onoff);
  166. CONSOLE_OUT("[state] target: 0x%02X\n", UI_generic_onoff.target_onoff);
  167. CONSOLE_OUT("[state] remaining_time: 0x%02X\n", UI_generic_onoff.transition_time);
  168. generic_onoff_set_pl(param_p->onoff);
  169. /* Ignoring Instance and direction right now */
  170. }
  171. break;
  172. default:
  173. break;
  174. }
  175. return retval;
  176. }
  177. /* Model state Initialization */
  178. static void UI_model_states_initialization(void)
  179. {
  180. /* Generic OnOff States */
  181. UI_generic_onoff_model_states_initialization();
  182. }
  183. /* Generic OnOff Model Server */
  184. /**
  185. * \brief Server Application Asynchronous Notification Callback.
  186. *
  187. * \par Description
  188. * Generic_Onoff server calls the registered callback to indicate events occurred to the application.
  189. *
  190. * \param [in] ctx Context of message received for a specific model instance.
  191. * \param [in] msg_raw Uninterpreted/raw received message.
  192. * \param [in] req_type Requested message type.
  193. * \param [in] state_params Model specific state parameters.
  194. * \param [in] ext_params Additional parameters.
  195. */
  196. static API_RESULT UI_generic_onoff_server_cb
  197. (
  198. /* IN */ MS_ACCESS_MODEL_REQ_MSG_CONTEXT * ctx,
  199. /* IN */ MS_ACCESS_MODEL_REQ_MSG_RAW * msg_raw,
  200. /* IN */ MS_ACCESS_MODEL_REQ_MSG_T * req_type,
  201. /* IN */ MS_ACCESS_MODEL_STATE_PARAMS * state_params,
  202. /* IN */ MS_ACCESS_MODEL_EXT_PARAMS * ext_params
  203. )
  204. {
  205. MS_STATE_GENERIC_ONOFF_STRUCT param;
  206. MS_ACCESS_MODEL_STATE_PARAMS current_state_params;
  207. API_RESULT retval;
  208. retval = API_SUCCESS;
  209. /* Check message type */
  210. if (MS_ACCESS_MODEL_REQ_MSG_T_GET == req_type->type)
  211. {
  212. CONSOLE_OUT("[GENERIC_ONOFF] GET Request.\n");
  213. UI_generic_onoff_model_state_get(state_params->state_type, 0, &param, 0);
  214. current_state_params.state_type = state_params->state_type;
  215. current_state_params.state = &param;
  216. /* Using same as target state and remaining time as 0 */
  217. }
  218. else if (MS_ACCESS_MODEL_REQ_MSG_T_SET == req_type->type)
  219. {
  220. CONSOLE_OUT("[GENERIC_ONOFF] SET Request.\n");
  221. retval = UI_generic_onoff_model_state_set(state_params->state_type, 0, (MS_STATE_GENERIC_ONOFF_STRUCT *)state_params->state, 0);
  222. current_state_params.state_type = state_params->state_type;
  223. current_state_params.state = (MS_STATE_GENERIC_ONOFF_STRUCT *)state_params->state;
  224. }
  225. /* See if to be acknowledged */
  226. if (0x01 == req_type->to_be_acked)
  227. {
  228. CONSOLE_OUT("[GENERIC_ONOFF] Sending Response.\n");
  229. /* Parameters: Request Context, Current State, Target State (NULL: to be ignored), Remaining Time (0: to be ignored), Additional Parameters (NULL: to be ignored) */
  230. retval = MS_generic_onoff_server_state_update(ctx, &current_state_params, NULL, 0, NULL);
  231. }
  232. return retval;
  233. }
  234. static API_RESULT UI_register_generic_onoff_model_server
  235. (
  236. MS_ACCESS_ELEMENT_HANDLE element_handle
  237. )
  238. {
  239. /* Generic OnOff Server */
  240. MS_ACCESS_MODEL_HANDLE UI_generic_onoff_server_model_handle;
  241. API_RESULT retval;
  242. CONSOLE_OUT("In Generic OnOff Model Server\n");
  243. retval = MS_generic_onoff_server_init
  244. (
  245. element_handle,
  246. &UI_generic_onoff_server_model_handle,
  247. UI_generic_onoff_server_cb
  248. );
  249. if (API_SUCCESS == retval)
  250. {
  251. CONSOLE_OUT(
  252. "Generic Onoff Server Initialized. Model Handle: 0x%04X\n",
  253. UI_generic_onoff_server_model_handle);
  254. }
  255. else
  256. {
  257. CONSOLE_OUT(
  258. "[ERR] Generic Onoff Server Initialization Failed. Result: 0x%04X\n",
  259. retval);
  260. }
  261. return retval;
  262. }
  263. /* Provisionee */
  264. #define UI_PROV_OUTPUT_OOB_ACTIONS \
  265. (PROV_MASK_OOOB_ACTION_BLINK | PROV_MASK_OOOB_ACTION_BEEP | \
  266. PROV_MASK_OOOB_ACTION_VIBRATE | PROV_MASK_OOOB_ACTION_NUMERIC | \
  267. PROV_MASK_OOOB_ACTION_ALPHANUMERIC)
  268. /** Output OOB Maximum size supported */
  269. #define UI_PROV_OUTPUT_OOB_SIZE 0x08
  270. /** Input OOB Actions supported */
  271. #define UI_PROV_INPUT_OOB_ACTIONS \
  272. (PROV_MASK_IOOB_ACTION_PUSH | PROV_MASK_IOOB_ACTION_TWIST | \
  273. PROV_MASK_IOOB_ACTION_NUMERIC | PROV_MASK_IOOB_ACTION_ALPHANUMERIC)
  274. /** Input OOB Maximum size supported */
  275. #define UI_PROV_INPUT_OOB_SIZE 0x08
  276. /** Beacon setup timeout in seconds */
  277. #define UI_PROV_SETUP_TIMEOUT_SECS 30
  278. /** Attention timeout for device in seconds */
  279. #define UI_PROV_DEVICE_ATTENTION_TIMEOUT 30
  280. #define PROV_AUTHVAL_SIZE_PL 16
  281. /** Authentication values for OOB Display - To be made random */
  282. #define UI_DISPLAY_AUTH_DIGIT 3
  283. #define UI_DISPLAY_AUTH_NUMERIC 35007
  284. #define UI_DISPLAY_AUTH_STRING "f00l"
  285. /** Provisioning capabilities of local device */
  286. DECL_STATIC PROV_CAPABILITIES_S UI_prov_capab =
  287. {
  288. /** Number of Elements */
  289. 0x01,
  290. /** Supported algorithms */
  291. PROV_MASK_ALGO_EC_FIPS_P256,
  292. /** Public key type */
  293. PROV_MASK_PUBKEY_OOBINFO,
  294. /** Static OOB type */
  295. PROV_MASK_STATIC_OOBINFO,
  296. /** Output OOB information */
  297. { UI_PROV_OUTPUT_OOB_ACTIONS, UI_PROV_OUTPUT_OOB_SIZE },
  298. /** Input OOB information */
  299. { UI_PROV_INPUT_OOB_ACTIONS, UI_PROV_INPUT_OOB_SIZE },
  300. };
  301. /** Unprovisioned device identifier */
  302. PROV_DEVICE_S UI_lprov_device =
  303. {
  304. /** UUID */
  305. {0xa8, 0x01, 0xb1, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0x00, 0x00, 0x00},
  306. /** OOB Flag */
  307. 0x00,
  308. /**
  309. * Encoded URI Information
  310. * For example, to give a web address, "https://www.abc.com"
  311. * the URI encoded data would be -
  312. * 0x17 0x2F 0x2F 0x77 0x77 0x77 0x2E 0x61 0x62 0x63 0x2E 0x63 0x6F 0x6D
  313. * where 0x17 is the URI encoding for https:
  314. */
  315. NULL
  316. };
  317. /** Current role of application - Provisioner/Device */
  318. DECL_STATIC UCHAR UI_prov_role;
  319. /** Provisioning Handle */
  320. DECL_STATIC PROV_HANDLE UI_prov_handle;
  321. static API_RESULT UI_prov_callback
  322. (
  323. PROV_HANDLE * phandle,
  324. UCHAR event_type,
  325. API_RESULT event_result,
  326. void * event_data,
  327. UINT16 event_datalen
  328. )
  329. {
  330. PROV_DEVICE_S * rdev;
  331. PROV_CAPABILITIES_S * rcap;
  332. PROV_DATA_S * rdata;
  333. PROV_OOB_TYPE_S * oob_info;
  334. API_RESULT retval;
  335. UCHAR i;
  336. UCHAR authstr[PROV_AUTHVAL_SIZE_PL << 1];
  337. UINT32 authnum;
  338. UCHAR authtype;
  339. UCHAR * pauth;
  340. UINT16 authsize;
  341. UCHAR pdata[(MS_DEVICE_UUID_SIZE * 2) + 1];
  342. UCHAR * t_data;
  343. switch (event_type)
  344. {
  345. case PROV_EVT_PROVISIONING_SETUP:
  346. CONSOLE_OUT("Recvd PROV_EVT_PROVISIONING_SETUP\n");
  347. CONSOLE_OUT("Status - 0x%04X\n", event_result);
  348. /* Display the attention timeout */
  349. CONSOLE_OUT("Attention TImeout - %d\n", *((UCHAR *)event_data));
  350. break;
  351. case PROV_EVT_OOB_DISPLAY:
  352. CONSOLE_OUT("Recvd PROV_EVT_OOB_DISPLAY\n");
  353. CONSOLE_OUT("Status - 0x%04X\n", event_result);
  354. /* Reference the Authenticatio Type information */
  355. oob_info = (PROV_OOB_TYPE_S *)event_data;
  356. CONSOLE_OUT("Authenticaion Action - 0x%02X\n", oob_info->action);
  357. CONSOLE_OUT("Authenticaion Size - 0x%02X\n", oob_info->size);
  358. /* If role is Device, the action is of Output OOB, else Input OOB */
  359. if (PROV_ROLE_DEVICE == UI_prov_role)
  360. {
  361. if (PROV_OOOB_ACTION_ALPHANUMERIC == oob_info->action)
  362. {
  363. authtype = 1;
  364. }
  365. else if (PROV_OOOB_ACTION_NUMERIC == oob_info->action)
  366. {
  367. authtype = 2;
  368. }
  369. else
  370. {
  371. authtype = 0;
  372. }
  373. }
  374. else
  375. {
  376. if (PROV_IOOB_ACTION_ALPHANUMERIC == oob_info->action)
  377. {
  378. authtype = 1;
  379. }
  380. else if (PROV_IOOB_ACTION_NUMERIC == oob_info->action)
  381. {
  382. authtype = 2;
  383. }
  384. else
  385. {
  386. authtype = 0;
  387. }
  388. }
  389. if (1 == authtype)
  390. {
  391. EM_str_copy (authstr, UI_DISPLAY_AUTH_STRING);
  392. CONSOLE_OUT("\n\n>>> AuthVal - %s <<<\n\n", authstr);
  393. pauth = authstr;
  394. authsize = EM_str_len(authstr);
  395. }
  396. else if (2 == authtype)
  397. {
  398. authnum = (UINT32)UI_DISPLAY_AUTH_NUMERIC;
  399. CONSOLE_OUT("\n\n>>> AuthVal - %d <<<\n\n", authnum);
  400. pauth = (UCHAR *)&authnum;
  401. authsize = sizeof(UINT32);
  402. }
  403. else
  404. {
  405. authnum = (UINT32)UI_DISPLAY_AUTH_DIGIT;
  406. CONSOLE_OUT("\n\n>>> AuthVal - %d <<<\n\n", authnum);
  407. pauth = (UCHAR *)&authnum;
  408. authsize = sizeof(UINT32);
  409. }
  410. /* Call to input the oob */
  411. CONSOLE_OUT("Setting the Authval...\n");
  412. retval = MS_prov_set_authval(&UI_prov_handle, pauth, authsize);
  413. CONSOLE_OUT("Retval - 0x%04X\n", retval);
  414. break;
  415. case PROV_EVT_OOB_ENTRY:
  416. CONSOLE_OUT("Recvd PROV_EVT_OOB_ENTRY\n");
  417. CONSOLE_OUT("Status - 0x%04X\n", event_result);
  418. /* Reference the Authenticatio Type information */
  419. oob_info = (PROV_OOB_TYPE_S *)event_data;
  420. CONSOLE_OUT("Authenticaion Action - 0x%02X\n", oob_info->action);
  421. CONSOLE_OUT("Authenticaion Size - 0x%02X\n", oob_info->size);
  422. break;
  423. case PROV_EVT_DEVINPUT_COMPLETE:
  424. CONSOLE_OUT("Recvd PROV_EVT_DEVINPUT_COMPLETE\n");
  425. CONSOLE_OUT("Status - 0x%04X\n", event_result);
  426. break;
  427. case PROV_EVT_PROVDATA_INFO:
  428. CONSOLE_OUT("Recvd PROV_EVT_PROVDATA_INFO\n");
  429. CONSOLE_OUT("Status - 0x%04X\n", event_result);
  430. /* Reference the Provisioning Data */
  431. rdata = (PROV_DATA_S *)event_data;
  432. CONSOLE_OUT("NetKey : "); appl_dump_bytes(rdata->netkey, PROV_KEY_NETKEY_SIZE);
  433. CONSOLE_OUT("Key ID : 0x%04X\n", rdata->keyid);
  434. CONSOLE_OUT("Flags : 0x%02X\n", rdata->flags);
  435. CONSOLE_OUT("IVIndex : 0x%08X\n", rdata->ivindex);
  436. CONSOLE_OUT("UAddr : 0x%04X\n", rdata->uaddr);
  437. blebrr_scan_pl(FALSE); // HZF
  438. /* Provide Provisioning Data to Access Layer */
  439. MS_access_cm_set_prov_data
  440. (
  441. rdata
  442. );
  443. break;
  444. case PROV_EVT_PROVISIONING_COMPLETE:
  445. CONSOLE_OUT("Recvd PROV_EVT_PROVISIONING_COMPLETE\n");
  446. CONSOLE_OUT("Status - 0x%04X\n", event_result);
  447. if (API_SUCCESS == event_result)
  448. {
  449. /* Already Set while handling PROV_EVT_PROVDATA_INFO */
  450. /* LED ON/OFF for Provisioning Indication Abstraction Call */
  451. mesh_model_device_provisioned_ind_pl();
  452. }
  453. break;
  454. default:
  455. CONSOLE_OUT("Unknown Event - 0x%02X\n", event_type);
  456. }
  457. return API_SUCCESS;
  458. }
  459. static void UI_register_prov(void)
  460. {
  461. API_RESULT retval;
  462. CONSOLE_OUT("Registering with Provisioning layer...\n");
  463. retval = MS_prov_register(&UI_prov_capab, UI_prov_callback);
  464. CONSOLE_OUT("Retval - 0x%04X\n", retval);
  465. }
  466. static void UI_setup_prov(UCHAR role, UCHAR brr)
  467. {
  468. API_RESULT retval;
  469. if (PROV_ROLE_PROVISIONER != role)
  470. {
  471. CONSOLE_OUT("Setting up Device for Provisioning ...\n");
  472. retval = MS_prov_setup
  473. (
  474. brr,
  475. role,
  476. &UI_lprov_device,
  477. UI_PROV_SETUP_TIMEOUT_SECS
  478. );
  479. UI_prov_role = PROV_ROLE_DEVICE;
  480. }
  481. else
  482. {
  483. CONSOLE_OUT("Setting up Provisioner for Provisioning ...\n");
  484. retval = MS_prov_setup
  485. (
  486. brr,
  487. role,
  488. NULL,
  489. UI_PROV_SETUP_TIMEOUT_SECS
  490. );
  491. UI_prov_role = PROV_ROLE_PROVISIONER;
  492. }
  493. CONSOLE_OUT("Retval - 0x%04X\n", retval);
  494. }
  495. static void UI_prov_bind(UCHAR brr, UCHAR index)
  496. {
  497. API_RESULT retval;
  498. /* Call to bind with the selected device */
  499. CONSOLE_OUT("Binding with the selected device...\n");
  500. retval = MS_prov_bind(brr, &UI_lprov_device, UI_PROV_DEVICE_ATTENTION_TIMEOUT, &UI_prov_handle);
  501. CONSOLE_OUT("Retval - 0x%04X\n", retval);
  502. }
  503. void appl_mesh_sample (void)
  504. {
  505. MS_ACCESS_NODE_ID node_id;
  506. MS_ACCESS_ELEMENT_DESC element;
  507. MS_ACCESS_ELEMENT_HANDLE element_handle;
  508. API_RESULT retval;
  509. UCHAR role, brr;
  510. MS_CONFIG * config_ptr;
  511. #ifdef MS_HAVE_DYNAMIC_CONFIG
  512. MS_CONFIG config;
  513. /* Initialize dynamic configuration */
  514. MS_INIT_CONFIG(config);
  515. config_ptr = &config;
  516. #else
  517. config_ptr = NULL;
  518. #endif /* MS_HAVE_DYNAMIC_CONFIG */
  519. /* Initialize OSAL */
  520. EM_os_init();
  521. /* Initialize Debug Module */
  522. EM_debug_init();
  523. /* Initialize Timer Module */
  524. EM_timer_init();
  525. timer_em_init();
  526. /* Initialize utilities */
  527. nvsto_init();
  528. /* Initialize Mesh Stack */
  529. MS_init(config_ptr);
  530. /* Register with underlying BLE stack */
  531. blebrr_register();
  532. /* Enable LED Port */
  533. /* Platform Abstraction Initializations of GPIOs/LEDs etc. */
  534. mesh_model_platform_init_pl();
  535. /* LED ON */
  536. /* LED ON/OFF for BOOT UP Indication Abstraction Call */
  537. mesh_model_device_bootup_ind_pl();
  538. /* Create Node */
  539. retval = MS_access_create_node(&node_id);
  540. /* Register Element */
  541. /**
  542. * TBD: Define GATT Namespace Descriptions from
  543. * https://www.bluetooth.com/specifications/assigned-numbers/gatt-namespace-descriptors
  544. *
  545. * Using 'main' (0x0106) as Location temporarily.
  546. */
  547. element.loc = 0x0106;
  548. retval = MS_access_register_element
  549. (
  550. node_id,
  551. &element,
  552. &element_handle
  553. );
  554. if (API_SUCCESS == retval)
  555. {
  556. /* Register foundation model servers */
  557. retval = UI_register_foundation_model_servers(element_handle);
  558. }
  559. if (API_SUCCESS == retval)
  560. {
  561. /* Register Generic OnOff model server */
  562. retval = UI_register_generic_onoff_model_server(element_handle);
  563. }
  564. if (API_SUCCESS == retval)
  565. {
  566. /* Initialize model states */
  567. UI_model_states_initialization();
  568. }
  569. /* Configure as provisionee/device */
  570. UI_register_prov();
  571. /**
  572. * setup <role:[1 - Device, 2 - Provisioner]> <bearer:[1 - Adv, 2 - GATT]
  573. */
  574. role = PROV_ROLE_DEVICE;
  575. brr = PROV_BRR_ADV;
  576. UI_setup_prov(role, brr);
  577. UI_prov_bind(brr, 0x00);
  578. return;
  579. }
  580. #endif /* (MESH_STANDALONE == 1) */