appl_sample_example_9.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  1. /**
  2. * \file appl_sample_example_9.c
  3. *
  4. * Source File for Generic OnOff Client Standalone application without CLI or
  5. * menu based console input interface over GATT Bearer and Function as Proxy
  6. * Device or a normal node based on the proxy configuration setting
  7. * once Provisioning is Complete.
  8. */
  9. /*
  10. * Copyright (C) 2018. Mindtree Ltd.
  11. * All rights reserved.
  12. */
  13. #if (MESH_STANDALONE == 9)
  14. /* ----------------------------------------- Header File Inclusion */
  15. #include "MS_common.h"
  16. #include "MS_access_api.h"
  17. #include "MS_config_api.h"
  18. #include "MS_health_server_api.h"
  19. #include "MS_generic_onoff_api.h"
  20. #include "MS_net_api.h"
  21. #include "blebrr.h"
  22. #include "nvsto.h"
  23. #include "model_state_handler_pl.h"
  24. /* Console Input/Output */
  25. #define CONSOLE_OUT(...) printf(__VA_ARGS__)
  26. #define CONSOLE_IN(...) scanf(__VA_ARGS__)
  27. void appl_dump_bytes(UCHAR *buffer, UINT16 length);
  28. void appl_mesh_sample (void);
  29. /* ----------------------------------------- External Global Variables */
  30. /* ----------------------------------------- Exported Global Variables */
  31. API_RESULT UI_generic_onoff_client_cb
  32. (
  33. /* IN */ MS_ACCESS_MODEL_HANDLE * handle,
  34. /* IN */ UINT32 opcode,
  35. /* IN */ UCHAR * data_param,
  36. /* IN */ UINT16 data_len
  37. );
  38. API_RESULT UI_health_server_cb
  39. (
  40. MS_ACCESS_MODEL_HANDLE * handle,
  41. UINT32 opcode,
  42. UCHAR * data_param,
  43. UINT16 data_len
  44. );
  45. void UI_prov_bind(UCHAR brr, UCHAR index);
  46. API_RESULT UI_prov_callback
  47. (
  48. PROV_HANDLE * phandle,
  49. UCHAR event_type,
  50. API_RESULT event_result,
  51. void * event_data,
  52. UINT16 event_datalen
  53. );
  54. void UI_proxy_callback
  55. (
  56. NETIF_HANDLE * handle,
  57. UCHAR p_evt,
  58. UCHAR * data_param,
  59. UINT16 data_len
  60. );
  61. void UI_proxy_start_adv(MS_SUBNET_HANDLE subnet_handle, UCHAR proxy_adv_mode);
  62. API_RESULT UI_register_foundation_model_servers
  63. (
  64. MS_ACCESS_ELEMENT_HANDLE element_handle
  65. );
  66. API_RESULT UI_register_generic_onoff_model_client
  67. (
  68. MS_ACCESS_ELEMENT_HANDLE element_handle
  69. );
  70. void UI_register_prov(void);
  71. void UI_register_proxy(void);
  72. void UI_sample_reinit(void);
  73. void UI_gatt_iface_event_pl_cb
  74. (
  75. UCHAR ev_name,
  76. UCHAR ev_param
  77. );
  78. API_RESULT UI_sample_check_app_key(void);
  79. API_RESULT UI_set_brr_scan_rsp_data (void);
  80. void UI_setup_prov(UCHAR role, UCHAR brr);
  81. /* ----------------------------------------- Static Global Variables */
  82. /* ----------------------------------------- Functions */
  83. /* Model Server - Foundation Models */
  84. /* Health Server - Test Routines */
  85. static void UI_health_self_test_00(UINT8 test_id, UINT16 company_id)
  86. {
  87. }
  88. static void UI_health_self_test_01(UINT8 test_id, UINT16 company_id)
  89. {
  90. }
  91. static void UI_health_self_test_FF(UINT8 test_id, UINT16 company_id)
  92. {
  93. }
  94. /* List of Self Tests */
  95. static MS_HEALTH_SERVER_SELF_TEST UI_health_server_self_tests[] =
  96. {
  97. {
  98. 0x00, /* Test ID: 0x00 */
  99. UI_health_self_test_00
  100. },
  101. {
  102. 0x01, /* Test ID: 0x01 */
  103. UI_health_self_test_01
  104. },
  105. {
  106. 0xFF, /* Test ID: 0xFF */
  107. UI_health_self_test_FF
  108. }
  109. };
  110. /**
  111. * \brief Health Server application Asynchronous Notification Callback.
  112. *
  113. * \par Description
  114. * Health Server calls the registered callback to indicate events occurred to the
  115. * application.
  116. *
  117. * \param handle Model Handle.
  118. * \param opcode Opcode.
  119. * \param data_param Data associated with the event if any or NULL.
  120. * \param data_len Size of the event data. 0 if event data is NULL.
  121. */
  122. API_RESULT UI_health_server_cb
  123. (
  124. MS_ACCESS_MODEL_HANDLE * handle,
  125. UINT32 opcode,
  126. UCHAR * data_param,
  127. UINT16 data_len
  128. )
  129. {
  130. CONSOLE_OUT(
  131. "Health Server Callback. Not handled. Returning\n");
  132. return API_SUCCESS;
  133. }
  134. API_RESULT UI_register_foundation_model_servers
  135. (
  136. MS_ACCESS_ELEMENT_HANDLE element_handle
  137. )
  138. {
  139. /* Configuration Server */
  140. MS_ACCESS_MODEL_HANDLE UI_config_server_model_handle;
  141. MS_ACCESS_MODEL_HANDLE UI_health_server_model_handle;
  142. API_RESULT retval;
  143. /* Health Server */
  144. UINT16 company_id;
  145. MS_HEALTH_SERVER_SELF_TEST * self_tests;
  146. UINT32 num_self_tests;
  147. CONSOLE_OUT("In Model Server - Foundation Models\n");
  148. retval = MS_config_server_init(element_handle, &UI_config_server_model_handle);
  149. CONSOLE_OUT("Config Model Server Registration Status: 0x%04X\n", retval);
  150. /* Health Server */
  151. company_id = MS_DEFAULT_COMPANY_ID;
  152. self_tests = &UI_health_server_self_tests[0];
  153. num_self_tests = sizeof(UI_health_server_self_tests)/sizeof(MS_HEALTH_SERVER_SELF_TEST);
  154. retval = MS_health_server_init
  155. (
  156. element_handle,
  157. &UI_health_server_model_handle,
  158. company_id,
  159. self_tests,
  160. num_self_tests,
  161. UI_health_server_cb
  162. );
  163. if (API_SUCCESS == retval)
  164. {
  165. CONSOLE_OUT(
  166. "Health Server Initialized. Model Handle: 0x%04X\n",
  167. UI_health_server_model_handle);
  168. }
  169. else
  170. {
  171. CONSOLE_OUT(
  172. "[ERR] Sensor Server Initialization Failed. Result: 0x%04X\n",
  173. retval);
  174. }
  175. return retval;
  176. }
  177. /* Generic OnOff Model Client */
  178. void UI_generic_onoff_set(UCHAR state)
  179. {
  180. API_RESULT retval;
  181. MS_GENERIC_ONOFF_SET_STRUCT param;
  182. CONSOLE_OUT
  183. ("Send Generic Onoff Set\n");
  184. param.onoff = state;
  185. param.tid = 0;
  186. param.optional_fields_present = 0x00;
  187. retval = MS_generic_onoff_set(&param);
  188. CONSOLE_OUT
  189. ("Retval - 0x%04X\n", retval);
  190. }
  191. /**
  192. * \brief Client Application Asynchronous Notification Callback.
  193. *
  194. * \par Description
  195. * Generic_Onoff client calls the registered callback to indicate events occurred to the application.
  196. *
  197. * \param [in] handle Model Handle.
  198. * \param [in] opcode Opcode.
  199. * \param [in] data_param Data associated with the event if any or NULL.
  200. * \param [in] data_len Size of the event data. 0 if event data is NULL.
  201. */
  202. API_RESULT UI_generic_onoff_client_cb
  203. (
  204. /* IN */ MS_ACCESS_MODEL_HANDLE * handle,
  205. /* IN */ UINT32 opcode,
  206. /* IN */ UCHAR * data_param,
  207. /* IN */ UINT16 data_len
  208. )
  209. {
  210. API_RESULT retval;
  211. retval = API_SUCCESS;
  212. CONSOLE_OUT (
  213. "[GENERIC_ONOFF_CLIENT] Callback. Opcode 0x%04X\n", opcode);
  214. appl_dump_bytes(data_param, data_len);
  215. switch(opcode)
  216. {
  217. case MS_ACCESS_GENERIC_ONOFF_STATUS_OPCODE:
  218. {
  219. CONSOLE_OUT(
  220. "MS_ACCESS_GENERIC_ONOFF_STATUS_OPCODE\n");
  221. }
  222. break;
  223. }
  224. return retval;
  225. }
  226. API_RESULT UI_register_generic_onoff_model_client
  227. (
  228. MS_ACCESS_ELEMENT_HANDLE element_handle
  229. )
  230. {
  231. /* Generic OnOff Server */
  232. MS_ACCESS_MODEL_HANDLE UI_generic_onoff_client_model_handle;
  233. API_RESULT retval;
  234. CONSOLE_OUT("In Generic OnOff Model Client\n");
  235. retval = MS_generic_onoff_client_init
  236. (
  237. element_handle,
  238. &UI_generic_onoff_client_model_handle,
  239. UI_generic_onoff_client_cb
  240. );
  241. if (API_SUCCESS == retval)
  242. {
  243. CONSOLE_OUT(
  244. "Generic Onoff Client Initialized. Model Handle: 0x%04X\n",
  245. UI_generic_onoff_client_model_handle);
  246. }
  247. else
  248. {
  249. CONSOLE_OUT(
  250. "[ERR] Generic Onoff Client Initialization Failed. Result: 0x%04X\n",
  251. retval);
  252. }
  253. return retval;
  254. }
  255. /* Provisionee */
  256. /** Public Key OOB Flag */
  257. #define UI_PROV_PUBKEY_OOBINFO 0x00
  258. /** Static OOB Flag */
  259. #define UI_PROV_STATIC_OOBINFO 0x00
  260. /** Output OOB Actions Supported */
  261. #ifdef MESH_SAMPLE_HAVE_OUTPUT_OOB_DISPLAY
  262. /** Currently Selecting the Output OOB Actions as Alphanumeric OOB Action */
  263. #define UI_PROV_OUTPUT_OOB_ACTIONS PROV_MASK_OOOB_ACTION_ALPHANUMERIC
  264. /** Output OOB Maximum size supported */
  265. #define UI_PROV_OUTPUT_OOB_SIZE 0x04
  266. #else /* MESH_SAMPLE_HAVE_OUTPUT_OOB_DISPLAY */
  267. /** Currently Selecting the Output OOB Actions as Alphanumeric OOB Action */
  268. #define UI_PROV_OUTPUT_OOB_ACTIONS 0x00
  269. /** Output OOB Maximum size supported */
  270. #define UI_PROV_OUTPUT_OOB_SIZE 0x00
  271. #endif /* MESH_SAMPLE_HAVE_OUTPUT_OOB_DISPLAY */
  272. /** Input OOB Actions supported */
  273. #define UI_PROV_INPUT_OOB_ACTIONS 0x00
  274. /** Input OOB Maximum size supported */
  275. #define UI_PROV_INPUT_OOB_SIZE 0x00
  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. UI_PROV_PUBKEY_OOBINFO,
  294. /** Static OOB type */
  295. UI_PROV_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. //DECL_STATIC PROV_DEVICE_S UI_lprov_device =
  303. PROV_DEVICE_S UI_lprov_device =
  304. {
  305. /** UUID */
  306. {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF},
  307. /** OOB Flag */
  308. 0x00,
  309. /**
  310. * Encoded URI Information
  311. * For example, to give a web address, "https://www.abc.com"
  312. * the URI encoded data would be -
  313. * 0x17 0x2F 0x2F 0x77 0x77 0x77 0x2E 0x61 0x62 0x63 0x2E 0x63 0x6F 0x6D
  314. * where 0x17 is the URI encoding for https:
  315. */
  316. NULL
  317. };
  318. /** Current role of application - Provisioner/Device */
  319. DECL_STATIC UCHAR UI_prov_role;
  320. /** Provisioning Handle */
  321. DECL_STATIC PROV_HANDLE UI_prov_handle;
  322. API_RESULT UI_prov_callback
  323. (
  324. PROV_HANDLE * phandle,
  325. UCHAR event_type,
  326. API_RESULT event_result,
  327. void * event_data,
  328. UINT16 event_datalen
  329. )
  330. {
  331. PROV_DATA_S * rdata;
  332. PROV_OOB_TYPE_S * oob_info;
  333. API_RESULT retval;
  334. UCHAR authstr[PROV_AUTHVAL_SIZE_PL << 1];
  335. UINT32 authnum;
  336. UCHAR authtype;
  337. UCHAR * pauth;
  338. UINT16 authsize;
  339. switch (event_type)
  340. {
  341. case PROV_EVT_PROVISIONING_SETUP:
  342. CONSOLE_OUT("Recvd PROV_EVT_PROVISIONING_SETUP\n");
  343. CONSOLE_OUT("Status - 0x%04X\n", event_result);
  344. /* Display the attention timeout */
  345. CONSOLE_OUT("Attention TImeout - %d\n", *((UCHAR *)event_data));
  346. break;
  347. case PROV_EVT_OOB_DISPLAY:
  348. CONSOLE_OUT("Recvd PROV_EVT_OOB_DISPLAY\n");
  349. CONSOLE_OUT("Status - 0x%04X\n", event_result);
  350. /* Reference the Authenticatio Type information */
  351. oob_info = (PROV_OOB_TYPE_S *)event_data;
  352. CONSOLE_OUT("Authenticaion Action - 0x%02X\n", oob_info->action);
  353. CONSOLE_OUT("Authenticaion Size - 0x%02X\n", oob_info->size);
  354. /* If role is Device, the action is of Output OOB, else Input OOB */
  355. if (PROV_ROLE_DEVICE == UI_prov_role)
  356. {
  357. if (PROV_OOOB_ACTION_ALPHANUMERIC == oob_info->action)
  358. {
  359. authtype = 1;
  360. }
  361. else if (PROV_OOOB_ACTION_NUMERIC == oob_info->action)
  362. {
  363. authtype = 2;
  364. }
  365. else
  366. {
  367. authtype = 0;
  368. }
  369. }
  370. else
  371. {
  372. if (PROV_IOOB_ACTION_ALPHANUMERIC == oob_info->action)
  373. {
  374. authtype = 1;
  375. }
  376. else if (PROV_IOOB_ACTION_NUMERIC == oob_info->action)
  377. {
  378. authtype = 2;
  379. }
  380. else
  381. {
  382. authtype = 0;
  383. }
  384. }
  385. if (1 == authtype)
  386. {
  387. EM_str_copy (authstr, UI_DISPLAY_AUTH_STRING);
  388. CONSOLE_OUT("\n\n>>> AuthVal - %s <<<\n\n", authstr);
  389. pauth = authstr;
  390. authsize = (UINT16)EM_str_len(authstr);
  391. }
  392. else if (2 == authtype)
  393. {
  394. authnum = (UINT32)UI_DISPLAY_AUTH_NUMERIC;
  395. CONSOLE_OUT("\n\n>>> AuthVal - %d <<<\n\n", authnum);
  396. pauth = (UCHAR *)&authnum;
  397. authsize = sizeof(UINT32);
  398. }
  399. else
  400. {
  401. authnum = (UINT32)UI_DISPLAY_AUTH_DIGIT;
  402. CONSOLE_OUT("\n\n>>> AuthVal - %d <<<\n\n", authnum);
  403. pauth = (UCHAR *)&authnum;
  404. authsize = sizeof(UINT32);
  405. }
  406. /* Call to input the oob */
  407. CONSOLE_OUT("Setting the Authval...\n");
  408. retval = MS_prov_set_authval(&UI_prov_handle, pauth, authsize);
  409. CONSOLE_OUT("Retval - 0x%04X\n", retval);
  410. break;
  411. case PROV_EVT_OOB_ENTRY:
  412. CONSOLE_OUT("Recvd PROV_EVT_OOB_ENTRY\n");
  413. CONSOLE_OUT("Status - 0x%04X\n", event_result);
  414. /* Reference the Authenticatio Type information */
  415. oob_info = (PROV_OOB_TYPE_S *)event_data;
  416. CONSOLE_OUT("Authenticaion Action - 0x%02X\n", oob_info->action);
  417. CONSOLE_OUT("Authenticaion Size - 0x%02X\n", oob_info->size);
  418. break;
  419. case PROV_EVT_DEVINPUT_COMPLETE:
  420. CONSOLE_OUT("Recvd PROV_EVT_DEVINPUT_COMPLETE\n");
  421. CONSOLE_OUT("Status - 0x%04X\n", event_result);
  422. break;
  423. case PROV_EVT_PROVDATA_INFO:
  424. CONSOLE_OUT("Recvd PROV_EVT_PROVDATA_INFO\n");
  425. CONSOLE_OUT("Status - 0x%04X\n", event_result);
  426. /* Reference the Provisioning Data */
  427. rdata = (PROV_DATA_S *)event_data;
  428. CONSOLE_OUT("NetKey : "); appl_dump_bytes(rdata->netkey, PROV_KEY_NETKEY_SIZE);
  429. CONSOLE_OUT("Key ID : 0x%04X\n", rdata->keyid);
  430. CONSOLE_OUT("Flags : 0x%02X\n", rdata->flags);
  431. CONSOLE_OUT("IVIndex : 0x%08X\n", rdata->ivindex);
  432. CONSOLE_OUT("UAddr : 0x%04X\n", rdata->uaddr);
  433. /* Provide Provisioning Data to Access Layer */
  434. MS_access_cm_set_prov_data
  435. (
  436. rdata
  437. );
  438. break;
  439. case PROV_EVT_PROVISIONING_COMPLETE:
  440. CONSOLE_OUT("Recvd PROV_EVT_PROVISIONING_COMPLETE\n");
  441. CONSOLE_OUT("Status - 0x%04X\n", event_result);
  442. if (API_SUCCESS == event_result)
  443. {
  444. /* Already Set while handling PROV_EVT_PROVDATA_INFO */
  445. /* LED ON/OFF for Provisioning Indication Abstraction Call */
  446. mesh_model_device_provisioned_ind_pl();
  447. }
  448. break;
  449. default:
  450. CONSOLE_OUT("Unknown Event - 0x%02X\n", event_type);
  451. }
  452. return API_SUCCESS;
  453. }
  454. void UI_register_prov(void)
  455. {
  456. API_RESULT retval;
  457. CONSOLE_OUT("Registering with Provisioning layer...\n");
  458. retval = MS_prov_register(&UI_prov_capab, UI_prov_callback);
  459. CONSOLE_OUT("Retval - 0x%04X\n", retval);
  460. }
  461. void UI_setup_prov(UCHAR role, UCHAR brr)
  462. {
  463. API_RESULT retval;
  464. if (PROV_BRR_GATT == brr)
  465. {
  466. blebrr_gatt_mode_set(BLEBRR_GATT_PROV_MODE);
  467. }
  468. if (PROV_ROLE_PROVISIONER != role)
  469. {
  470. CONSOLE_OUT("Setting up Device for Provisioning ...\n");
  471. retval = MS_prov_setup
  472. (
  473. brr,
  474. role,
  475. &UI_lprov_device,
  476. UI_PROV_SETUP_TIMEOUT_SECS
  477. );
  478. UI_prov_role = PROV_ROLE_DEVICE;
  479. }
  480. else
  481. {
  482. CONSOLE_OUT("Setting up Provisioner for Provisioning ...\n");
  483. retval = MS_prov_setup
  484. (
  485. brr,
  486. role,
  487. NULL,
  488. UI_PROV_SETUP_TIMEOUT_SECS
  489. );
  490. UI_prov_role = PROV_ROLE_PROVISIONER;
  491. }
  492. CONSOLE_OUT("Retval - 0x%04X\n", retval);
  493. }
  494. void UI_prov_bind(UCHAR brr, UCHAR index)
  495. {
  496. API_RESULT retval;
  497. /* Call to bind with the selected device */
  498. CONSOLE_OUT("Binding with the selected device...\n");
  499. retval = MS_prov_bind(brr, &UI_lprov_device, UI_PROV_DEVICE_ATTENTION_TIMEOUT, &UI_prov_handle);
  500. CONSOLE_OUT("Retval - 0x%04X\n", retval);
  501. }
  502. void UI_proxy_start_adv(MS_SUBNET_HANDLE subnet_handle, UCHAR proxy_adv_mode)
  503. {
  504. API_RESULT retval;
  505. DECL_STATIC UINT8 first_time = 0;
  506. if (0 == first_time)
  507. {
  508. /**
  509. * Register with Proxy Module as Device is going to be a Proxy.
  510. * This is typically a one-time-event, and hence registering the
  511. * PROXY when Proxy ADV is being initiated!
  512. */
  513. UI_register_proxy();
  514. first_time = 1;
  515. }
  516. /* Set the role to Proxy with bearer */
  517. blebrr_gatt_mode_set(BLEBRR_GATT_PROXY_MODE);
  518. CONSOLE_OUT("Start Proxy Advertisements with %s for Subnet 0x%04X\n",
  519. (proxy_adv_mode == MS_PROXY_NET_ID_ADV_MODE) ? "Network ID" : "Node Identity",
  520. subnet_handle);
  521. retval = MS_proxy_server_adv_start
  522. (
  523. subnet_handle,
  524. proxy_adv_mode
  525. );
  526. CONSOLE_OUT("Retval - 0x%04X\n", retval);
  527. }
  528. void UI_proxy_callback
  529. (
  530. NETIF_HANDLE * handle,
  531. UCHAR p_evt,
  532. UCHAR * data_param,
  533. UINT16 data_len
  534. )
  535. {
  536. UCHAR role;
  537. MS_IGNORE_UNUSED_PARAM(data_len);
  538. switch(p_evt)
  539. {
  540. case MS_PROXY_UP_EVENT:
  541. CONSOLE_OUT(
  542. "\n\n[PROXY APPL]: MS_PROXY_UP_EVENT Received for NETIF Handle 0x%02X\n\n", *handle);
  543. if (NULL != data_param)
  544. {
  545. /* Catch the current role into a local */
  546. role = data_param[0];
  547. if (BRR_SERVER_ROLE == role)
  548. {
  549. /* Send Secure Network Beacons */
  550. /* MS_net_broadcast_secure_beacon(0x0000); */
  551. }
  552. }
  553. break;
  554. case MS_PROXY_DOWN_EVENT:
  555. CONSOLE_OUT(
  556. "\n\n[PROXY APPL]: MS_PROXY_DOWN_EVENT Received for NETIF Handle 0x%02X\n\n", *handle);
  557. break;
  558. default:
  559. CONSOLE_OUT(
  560. "\n\n[PROXY APPL ERR]: Unknown Event Received for NETIF Handle 0x%02X!!\n\n", *handle);
  561. break;
  562. }
  563. }
  564. void UI_register_proxy(void)
  565. {
  566. API_RESULT retval;
  567. CONSOLE_OUT("Registering with Proxy layer...\n");
  568. retval = MS_proxy_register(UI_proxy_callback);
  569. CONSOLE_OUT("Retval - 0x%04X\n", retval);
  570. }
  571. API_RESULT UI_set_brr_scan_rsp_data (void)
  572. {
  573. /**
  574. * Currently setting MT-MESH-SAMPLE-9 as Complete Device Name!
  575. * This can be updated to each individual devices as per requirement.
  576. */
  577. UCHAR UI_brr_scanrsp_data[] =
  578. {
  579. /**
  580. * Shortened Device Name: MT-MESH-SAMPLE-9
  581. */
  582. 0x11, 0x09, 'M', 'T', '-', 'M', 'E', 'S', 'H', '-', 'S', 'A', 'M', 'P', 'L', 'E', '-', '9'
  583. };
  584. CONSOLE_OUT("\n Setting MT-MESH-SAMPLE-8 as Complete Device Name!\n");
  585. /* Set the Scan Response Data at the Bearer Layer */
  586. blebrr_set_adv_scanrsp_data_pl
  587. (
  588. UI_brr_scanrsp_data,
  589. sizeof(UI_brr_scanrsp_data)
  590. );
  591. return API_SUCCESS;
  592. }
  593. EM_timer_handle thandle;
  594. void timeout_cb (void * args, UINT16 size)
  595. {
  596. thandle = EM_TIMER_HANDLE_INIT_VAL;
  597. UI_sample_reinit();
  598. }
  599. void UI_gatt_iface_event_pl_cb
  600. (
  601. UCHAR ev_name,
  602. UCHAR ev_param
  603. )
  604. {
  605. switch(ev_name)
  606. {
  607. /* GATT Bearer BLE Link Layer Disconnected */
  608. case BLEBRR_GATT_IFACE_DOWN:
  609. CONSOLE_OUT("\r\n >> GATT Bearer BLE Link Layer Disconnection Event Received!\r\n");
  610. //UI_sample_reinit();
  611. EM_start_timer (&thandle, 3, timeout_cb, NULL, 0);
  612. break;
  613. /* GATT Bearer BLE Link Layer Connected */
  614. case BLEBRR_GATT_IFACE_UP:
  615. CONSOLE_OUT("\r\n >> GATT Bearer BLE Link Layer Connection Event Received!\r\n");
  616. /* Do Nothing! */
  617. if (BLEBRR_GATT_PROV_MODE == blebrr_gatt_mode_get())
  618. {
  619. MS_brr_bcast_end(BRR_BCON_TYPE_UNPROV_DEVICE, BRR_BCON_ACTIVE);
  620. }
  621. else if (BLEBRR_GATT_PROXY_MODE == blebrr_gatt_mode_get())
  622. {
  623. MS_proxy_server_adv_stop();
  624. }
  625. break;
  626. case BLEBRR_GATT_IFACE_ENABLE:
  627. CONSOLE_OUT("\r\n >> GATT Bearer Active Event Received!\r\n");
  628. {
  629. if (BLEBRR_GATT_PROV_MODE == ev_param)
  630. {
  631. /* Call to bind with the selected device */
  632. UI_prov_bind(PROV_BRR_GATT, 0);
  633. }
  634. }
  635. break;
  636. case BLEBRR_GATT_IFACE_DISABLE:
  637. CONSOLE_OUT("\r\n >> GATT Bearer Inactive Event Received!\r\n");
  638. break;
  639. /* Unknown Event! */
  640. default:
  641. CONSOLE_OUT("\r\n >> GATT Bearer BLE Link Layer Unknown Event 0x%02X Received!\r\n", ev_name);
  642. /* Do Nothing! */
  643. break;
  644. }
  645. }
  646. void appl_mesh_sample (void)
  647. {
  648. MS_ACCESS_NODE_ID node_id;
  649. MS_ACCESS_ELEMENT_DESC element;
  650. MS_ACCESS_ELEMENT_HANDLE element_handle;
  651. API_RESULT retval;
  652. MS_CONFIG * config_ptr;
  653. #ifdef MS_HAVE_DYNAMIC_CONFIG
  654. MS_CONFIG config;
  655. /* Initialize dynamic configuration */
  656. MS_INIT_CONFIG(config);
  657. config_ptr = &config;
  658. #else
  659. config_ptr = NULL;
  660. #endif /* MS_HAVE_DYNAMIC_CONFIG */
  661. /* Initialize OSAL */
  662. EM_os_init();
  663. /* Initialize Debug Module */
  664. EM_debug_init();
  665. /* Initialize Timer Module */
  666. EM_timer_init();
  667. timer_em_init();
  668. /* Initialize utilities */
  669. nvsto_init();
  670. /* Initialize Mesh Stack */
  671. MS_init(config_ptr);
  672. /* Register with underlying BLE stack */
  673. blebrr_register();
  674. /* Register GATT Bearer Connection/Disconnection Event Hook */
  675. blebrr_register_gatt_iface_event_pl(UI_gatt_iface_event_pl_cb);
  676. /* Enable LED Port */
  677. /* Platform Abstraction Initializations of GPIOs/LEDs etc. */
  678. mesh_model_platform_init_pl();
  679. /* LED ON */
  680. /* LED ON/OFF for BOOT UP Indication Abstraction Call */
  681. mesh_model_device_bootup_ind_pl();
  682. /* Create Node */
  683. retval = MS_access_create_node(&node_id);
  684. /* Register Element */
  685. /**
  686. * TBD: Define GATT Namespace Descriptions from
  687. * https://www.bluetooth.com/specifications/assigned-numbers/gatt-namespace-descriptors
  688. *
  689. * Using 'main' (0x0106) as Location temporarily.
  690. */
  691. element.loc = 0x0106;
  692. retval = MS_access_register_element
  693. (
  694. node_id,
  695. &element,
  696. &element_handle
  697. );
  698. if (API_SUCCESS == retval)
  699. {
  700. /* Register foundation model servers */
  701. retval = UI_register_foundation_model_servers(element_handle);
  702. }
  703. if (API_SUCCESS == retval)
  704. {
  705. /* Register Generic OnOff model client */
  706. retval = UI_register_generic_onoff_model_client(element_handle);
  707. }
  708. /* Configure as provisionee/device */
  709. UI_register_prov();
  710. /**
  711. * Set Scan Response Data Before Starting Provisioning.
  712. * This is optional/additional set of Data that the device can
  713. * set to enhance the User Experience.
  714. * For Example, set a specific device name or URL as part of the
  715. * Scan Response Data when awaiting connections over GATT bearer.
  716. */
  717. UI_set_brr_scan_rsp_data();
  718. //UI_sample_reinit();
  719. EM_start_timer (&thandle, 5, timeout_cb, NULL, 0);
  720. return;
  721. }
  722. API_RESULT UI_sample_check_app_key(void)
  723. {
  724. MS_APPKEY_HANDLE handle;
  725. UINT8 * key;
  726. UINT8 aid;
  727. DECL_CONST UINT8 t_key[16] = {0};
  728. API_RESULT retval;
  729. CONSOLE_OUT("Fetching App Key for Handle 0x0000\n");
  730. handle = 0x0000;
  731. retval = MS_access_cm_get_app_key
  732. (
  733. handle,
  734. &key,
  735. &aid
  736. );
  737. /* Check Retval. Print App Key */
  738. if (API_SUCCESS == retval)
  739. {
  740. CONSOLE_OUT("App Key[0x%02X]: %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X\r\n",
  741. handle, key[0], key[1], key[2], key[3], key[4], key[5], key[6], key[7],
  742. key[8], key[9], key[10], key[11], key[12], key[13], key[14], key[15]);
  743. if (0 == EM_mem_cmp(key, t_key, 16))
  744. {
  745. /* NO AppKey Bound */
  746. retval = API_FAILURE;
  747. }
  748. else
  749. {
  750. /* Found a Valid App Key */
  751. /* Keeping the retval as API_SUCCESS */
  752. }
  753. }
  754. return retval;
  755. }
  756. /**
  757. * On GATT Bearer Disconnection or on Startup:
  758. * 1. If device not provisioned, start unprovisioned beacons over GATT bearer
  759. * 2. If device is provisioned and App Key is bound i.e. Device is Configured
  760. * - Check if Proxy Feature is enabled then Start ADV as Proxy,
  761. * Else,
  762. * Do nothing!
  763. * Else,
  764. * If device is provisioned and App Key is not bound i.e. Device is not Configured
  765. * Start ADV as Proxy.
  766. */
  767. void UI_sample_reinit(void)
  768. {
  769. API_RESULT retval;
  770. MS_NET_ADDR addr;
  771. UCHAR is_prov_req;
  772. UCHAR role, brr;
  773. UCHAR state;
  774. retval = API_SUCCESS;
  775. is_prov_req = MS_TRUE;
  776. retval = MS_access_cm_get_primary_unicast_address(&addr);
  777. if (API_SUCCESS == retval)
  778. {
  779. if (MS_NET_ADDR_UNASSIGNED != addr)
  780. {
  781. /* Set Provisioning is not Required */
  782. is_prov_req = MS_FALSE;
  783. }
  784. }
  785. if (MS_TRUE == is_prov_req)
  786. {
  787. /* Start Provisioning over GATT here */
  788. /**
  789. * setup <role:[1 - Device, 2 - Provisioner]> <bearer:[1 - Adv, 2 - GATT]
  790. */
  791. role = PROV_ROLE_DEVICE;
  792. brr = PROV_BRR_GATT;
  793. /**
  794. * Setting up an Unprovisioned Device over GATT
  795. */
  796. UI_setup_prov(role, brr);
  797. CONSOLE_OUT("\r\n Setting up as an Unprovisioned Device\r\n");
  798. }
  799. else
  800. {
  801. /* Fetch PROXY feature state */
  802. MS_access_cm_get_features_field(&state, MS_FEATURE_PROXY);
  803. /**
  804. * Check if the Device is Configured.
  805. * If not Configured, Start Proxy ADV.
  806. * If it is Configured,
  807. * Check if the Proxy Feature is Enabled.
  808. * If not enabled, then Do Nothing!
  809. * If it is, Start Proxy ADV.
  810. */
  811. if (API_SUCCESS == UI_sample_check_app_key())
  812. {
  813. if (MS_ENABLE == state)
  814. {
  815. CONSOLE_OUT("\r\n Provisioned Device - Starting Proxy with NetID on Subnet 0x0000!\r\n");
  816. /* Start Proxy ADV with Network ID here */
  817. UI_proxy_start_adv(0x0000, MS_PROXY_NET_ID_ADV_MODE);
  818. }
  819. else
  820. {
  821. /**
  822. * Do Nothing!
  823. * Already Scaning is Enabled at Start Up
  824. */
  825. blebrr_scan_enable();
  826. }
  827. }
  828. else
  829. {
  830. /**
  831. * Provisioned but not configured device.
  832. * Still checking if the PROXY Feature is enabled or not.
  833. * Depending on the state of Proxy Feature:
  834. * - If enabled, Start Proxy ADV with Network ID
  835. * - Else, Start Proxy ADV with Node Identity.
  836. */
  837. (MS_ENABLE == state) ?
  838. UI_proxy_start_adv(0x0000, MS_PROXY_NET_ID_ADV_MODE):
  839. UI_proxy_start_adv(0x0000, MS_PROXY_NODE_ID_ADV_MODE);
  840. }
  841. }
  842. }
  843. //void UI_set_uuid_octet (UCHAR uuid_0)
  844. //{
  845. // UI_lprov_device.uuid[0] = uuid_0;
  846. //}
  847. #endif /* (MESH_STANDALONE == 9) */