appl_sample_example_8.c 33 KB

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