appl_sample_example_6.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  1. /**
  2. * \file appl_sample_example_6.c
  3. *
  4. * Source File for Generic OnOff Server and a vendor defined model Server
  5. * Standalone application without CLI or menu based console input interface.
  6. */
  7. /*
  8. * Copyright (C) 2018. Mindtree Ltd.
  9. * All rights reserved.
  10. */
  11. #if (MESH_STANDALONE == 6)
  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. /* --------------------------------------------- Global Definitions */
  27. #define MS_MODEL_ID_VENDOR_EXAMPLE_SERVER 0xA001A001
  28. #define MS_ACCESS_VENDOR_EXAMPLE_SET_OPCODE 0xA0010001
  29. #define MS_ACCESS_VENDOR_EXAMPLE_GET_OPCODE 0xA0010002
  30. #define MS_ACCESS_VENDOR_EXAMPLE_SET_UNACKNOWLEDGED_OPCODE 0xA0010003
  31. #define MS_STATE_VENDOR_EXAMPLE_T 0xA1
  32. /**
  33. * Vendor Example Server application Asynchronous Notification Callback.
  34. *
  35. * Vendor Example Server calls the registered callback to indicate events occurred to the
  36. * application.
  37. *
  38. * \param [in] ctx Context of the message received for a specific model instance.
  39. * \param [in] msg_raw Uninterpreted/raw received message.
  40. * \param [in] req_type Requested message type.
  41. * \param [in] state_params Model specific state parameters.
  42. * \param [in] ext_params Additional parameters.
  43. */
  44. typedef API_RESULT (* MS_VENDOR_EXAMPLE_SERVER_CB)
  45. (
  46. MS_ACCESS_MODEL_REQ_MSG_CONTEXT * ctx,
  47. MS_ACCESS_MODEL_REQ_MSG_RAW * msg_raw,
  48. MS_ACCESS_MODEL_REQ_MSG_T * req_type,
  49. MS_ACCESS_MODEL_STATE_PARAMS * state_params,
  50. MS_ACCESS_MODEL_EXT_PARAMS * ext_params
  51. ) DECL_REENTRANT;
  52. typedef struct MS_state_vendor_example_struct
  53. {
  54. UCHAR value;
  55. } MS_STATE_VENDOR_EXAMPLE_STRUCT;
  56. /* ----------------------------------------- External Global Variables */
  57. /* ----------------------------------------- Exported Global Variables */
  58. /* ----------------------------------------- Static Global Variables */
  59. static DECL_CONST UINT32 vendor_example_server_opcode_list[] =
  60. {
  61. MS_ACCESS_VENDOR_EXAMPLE_SET_OPCODE,
  62. MS_ACCESS_VENDOR_EXAMPLE_GET_OPCODE,
  63. MS_ACCESS_VENDOR_EXAMPLE_SET_UNACKNOWLEDGED_OPCODE,
  64. };
  65. static MS_ACCESS_MODEL_HANDLE vendor_example_server_model_handle;
  66. static MS_VENDOR_EXAMPLE_SERVER_CB vendor_example_server_UI_cb;
  67. /* ----------------------------------------- Functions */
  68. /**
  69. * \brief API to send reply or to update state change
  70. *
  71. * \par Description
  72. * This is to send reply for a request or to inform change in state.
  73. *
  74. * \param [in] ctx Context of the message.
  75. * \param [in] current_state_params Model specific current state parameters.
  76. * \param [in] target_state_params Model specific target state parameters (NULL: to be ignored).
  77. * \param [in] remaining_time Time from current state to target state (0: to be ignored).
  78. * \param [in] ext_params Additional parameters (NULL: to be ignored).
  79. *
  80. * \return API_SUCCESS or an error code indicating reason for failure
  81. */
  82. API_RESULT MS_vendor_example_server_state_update
  83. (
  84. /* IN */ MS_ACCESS_MODEL_REQ_MSG_CONTEXT * ctx,
  85. /* IN */ MS_ACCESS_MODEL_STATE_PARAMS * current_state_params,
  86. /* IN */ MS_ACCESS_MODEL_STATE_PARAMS * target_state_params,
  87. /* IN */ UINT16 remaining_time,
  88. /* IN */ MS_ACCESS_MODEL_EXT_PARAMS * ext_params
  89. )
  90. {
  91. API_RESULT retval;
  92. /* TODO: Check what should be maximum length */
  93. UCHAR buffer[32];
  94. UCHAR * pdu_ptr;
  95. UINT16 marker;
  96. UINT32 opcode;
  97. retval = API_FAILURE;
  98. marker = 0;
  99. CONSOLE_OUT(
  100. "[VENDOR_EXAMPLE_SERVER] State Update.\n");
  101. switch (current_state_params->state_type)
  102. {
  103. }
  104. /* Publish - reliable */
  105. if (0 == marker)
  106. {
  107. pdu_ptr = NULL;
  108. }
  109. else
  110. {
  111. pdu_ptr = buffer;
  112. }
  113. retval = MS_access_reply
  114. (
  115. &ctx->handle,
  116. ctx->daddr,
  117. ctx->saddr,
  118. ctx->subnet_handle,
  119. ctx->appkey_handle,
  120. ACCESS_INVALID_DEFAULT_TTL,
  121. opcode,
  122. pdu_ptr,
  123. marker
  124. );
  125. return retval;
  126. }
  127. /* Empty Model Opcode Handler Defines */
  128. MODEL_OPCODE_HANDLER_EMPTY_DEF(vendor_example_set_handler)
  129. MODEL_OPCODE_HANDLER_EMPTY_DEF(vendor_example_get_handler)
  130. MODEL_OPCODE_HANDLER_EMPTY_DEF(vendor_example_set_unacknowledged_handler)
  131. /**
  132. * \brief Access Layer Application Asynchronous Notification Callback.
  133. *
  134. * \par Description
  135. * Access Layer calls the registered callback to indicate events occurred to the application.
  136. *
  137. * \param [in] handle Model Handle.
  138. * \param [in] saddr 16 bit Source Address.
  139. * \param [in] daddr 16 bit Destination Address.
  140. * \param [in] appkey_handle AppKey Handle.
  141. * \param [in] subnet_handle Subnet Handle.
  142. * \param [in] opcode Opcode.
  143. * \param [in] data_param Data associated with the event if any or NULL.
  144. * \param [in] data_len Size of the event data. 0 if event data is NULL.
  145. */
  146. API_RESULT vendor_example_server_cb
  147. (
  148. /* IN */ MS_ACCESS_MODEL_HANDLE * handle,
  149. /* IN */ MS_NET_ADDR saddr,
  150. /* IN */ MS_NET_ADDR daddr,
  151. /* IN */ MS_SUBNET_HANDLE subnet_handle,
  152. /* IN */ MS_APPKEY_HANDLE appkey_handle,
  153. /* IN */ UINT32 opcode,
  154. /* IN */ UCHAR * data_param,
  155. /* IN */ UINT16 data_len
  156. )
  157. {
  158. MS_ACCESS_MODEL_REQ_MSG_CONTEXT req_context;
  159. MS_ACCESS_MODEL_REQ_MSG_RAW req_raw;
  160. MS_ACCESS_MODEL_REQ_MSG_T req_type;
  161. MS_ACCESS_MODEL_EXT_PARAMS * ext_params_p;
  162. MS_ACCESS_MODEL_STATE_PARAMS state_params;
  163. UINT16 marker;
  164. API_RESULT retval;
  165. retval = API_SUCCESS;
  166. ext_params_p = NULL;
  167. /* Request Context */
  168. req_context.handle = *handle;
  169. req_context.saddr = saddr;
  170. req_context.daddr = daddr;
  171. req_context.subnet_handle = subnet_handle;
  172. req_context.appkey_handle = appkey_handle;
  173. /* Request Raw */
  174. req_raw.opcode = opcode;
  175. req_raw.data_param = data_param;
  176. req_raw.data_len = data_len;
  177. CONSOLE_OUT(
  178. "[VENDOR_EXAMPLE_SERVER] Callback. Opcode 0x%04X\n", opcode);
  179. appl_dump_bytes(data_param, data_len);
  180. switch(opcode)
  181. {
  182. case MS_ACCESS_VENDOR_EXAMPLE_GET_OPCODE:
  183. {
  184. CONSOLE_OUT(
  185. "MS_ACCESS_VENDOR_EXAMPLE_GET_OPCODE\n");
  186. MODEL_OPCODE_HANDLER_CALL(vendor_example_get_handler);
  187. /* Get Request Type */
  188. req_type.type = MS_ACCESS_MODEL_REQ_MSG_T_GET;
  189. req_type.to_be_acked = 0x01;
  190. /* Assign reqeusted state type to the application */
  191. state_params.state_type = MS_STATE_VENDOR_EXAMPLE_T;
  192. }
  193. break;
  194. case MS_ACCESS_VENDOR_EXAMPLE_SET_OPCODE:
  195. {
  196. CONSOLE_OUT(
  197. "MS_ACCESS_VENDOR_EXAMPLE_SET_OPCODE\n");
  198. MODEL_OPCODE_HANDLER_CALL(vendor_example_set_handler);
  199. /* Set Request Type */
  200. req_type.type = MS_ACCESS_MODEL_REQ_MSG_T_SET;
  201. req_type.to_be_acked = 0x01;
  202. }
  203. break;
  204. case MS_ACCESS_VENDOR_EXAMPLE_SET_UNACKNOWLEDGED_OPCODE:
  205. {
  206. CONSOLE_OUT(
  207. "MS_ACCESS_VENDOR_EXAMPLE_SET_UNACKNOWLEDGED_OPCODE\n");
  208. MODEL_OPCODE_HANDLER_CALL(vendor_example_set_unacknowledged_handler);
  209. /* Set Request Type */
  210. req_type.type = MS_ACCESS_MODEL_REQ_MSG_T_SET;
  211. req_type.to_be_acked = 0x00;
  212. }
  213. break;
  214. }
  215. /* Application callback */
  216. if (NULL != vendor_example_server_UI_cb)
  217. {
  218. vendor_example_server_UI_cb(&req_context, &req_raw, &req_type, &state_params, ext_params_p);
  219. }
  220. return retval;
  221. }
  222. /**
  223. * \brief API to initialize Vendor_Example_1 Server model
  224. *
  225. * \par Description
  226. * This is to initialize Vendor_Example_1 Server model and to register with Acess layer.
  227. *
  228. * \param [in] element_handle
  229. * Element identifier to be associated with the model instance.
  230. *
  231. * \param [in, out] model_handle
  232. * Model identifier associated with the model instance on successful initialization.
  233. * After power cycle of an already provisioned node, the model handle will have
  234. * valid value and the same will be reused for registration.
  235. *
  236. * \param [in] UI_cb Application Callback to be used by the Vendor_Example_1 Server.
  237. *
  238. * \return API_SUCCESS or an error code indicating reason for failure
  239. */
  240. API_RESULT MS_vendor_example_server_init
  241. (
  242. /* IN */ MS_ACCESS_ELEMENT_HANDLE element_handle,
  243. /* INOUT */ MS_ACCESS_MODEL_HANDLE * model_handle,
  244. /* IN */ MS_VENDOR_EXAMPLE_SERVER_CB UI_cb
  245. )
  246. {
  247. API_RESULT retval;
  248. MS_ACCESS_NODE_ID node_id;
  249. MS_ACCESS_MODEL model;
  250. /* TBD: Initialize MUTEX and other data structures */
  251. /* Using default node ID */
  252. node_id = MS_ACCESS_DEFAULT_NODE_ID;
  253. CONSOLE_OUT(
  254. "[VENDOR_EXAMPLE] Registered Element Handle 0x%02X\n", element_handle);
  255. /* Configure Model */
  256. model.model_id.id = MS_MODEL_ID_VENDOR_EXAMPLE_SERVER;
  257. model.model_id.type = MS_ACCESS_MODEL_TYPE_VENDOR;
  258. model.elem_handle = element_handle;
  259. /* Register Callback */
  260. model.cb = vendor_example_server_cb;
  261. /* List of Opcodes */
  262. model.opcodes = vendor_example_server_opcode_list;
  263. model.num_opcodes = sizeof(vendor_example_server_opcode_list) / sizeof(UINT32);
  264. retval = MS_access_register_model
  265. (
  266. node_id,
  267. &model,
  268. model_handle
  269. );
  270. /* Save Application Callback */
  271. vendor_example_server_UI_cb = UI_cb;
  272. /* TODO: Remove */
  273. vendor_example_server_model_handle = *model_handle;
  274. return retval;
  275. }
  276. /* Model Server - Foundation Models */
  277. /* Health Server - Test Routines */
  278. static void UI_health_self_test_00(UINT8 test_id, UINT16 company_id)
  279. {
  280. }
  281. static void UI_health_self_test_01(UINT8 test_id, UINT16 company_id)
  282. {
  283. }
  284. static void UI_health_self_test_FF(UINT8 test_id, UINT16 company_id)
  285. {
  286. }
  287. /* List of Self Tests */
  288. static MS_HEALTH_SERVER_SELF_TEST UI_health_server_self_tests[] =
  289. {
  290. {
  291. 0x00, /* Test ID: 0x00 */
  292. UI_health_self_test_00
  293. },
  294. {
  295. 0x01, /* Test ID: 0x01 */
  296. UI_health_self_test_01
  297. },
  298. {
  299. 0xFF, /* Test ID: 0xFF */
  300. UI_health_self_test_FF
  301. }
  302. };
  303. /**
  304. * \brief Health Server application Asynchronous Notification Callback.
  305. *
  306. * \par Description
  307. * Health Server calls the registered callback to indicate events occurred to the
  308. * application.
  309. *
  310. * \param handle Model Handle.
  311. * \param event_type Health Server Event type.
  312. * \param event_param Parameter associated with the event if any or NULL.
  313. * \param param_len Size of the event parameter data. 0 if event param is NULL.
  314. */
  315. static API_RESULT UI_health_server_cb
  316. (
  317. MS_ACCESS_MODEL_HANDLE * handle,
  318. UINT8 event_type,
  319. UINT8 * event_param,
  320. UINT16 param_len
  321. )
  322. {
  323. CONSOLE_OUT(
  324. "Health Server Callback. Not handled. Returning\n");
  325. return API_SUCCESS;
  326. }
  327. API_RESULT UI_register_foundation_model_servers
  328. (
  329. MS_ACCESS_ELEMENT_HANDLE element_handle
  330. )
  331. {
  332. /* Configuration Server */
  333. MS_ACCESS_MODEL_HANDLE UI_config_server_model_handle;
  334. MS_ACCESS_MODEL_HANDLE UI_health_server_model_handle;
  335. API_RESULT retval;
  336. /* Health Server */
  337. UINT16 company_id;
  338. MS_HEALTH_SERVER_SELF_TEST * self_tests;
  339. UINT32 num_self_tests;
  340. CONSOLE_OUT("In Model Server - Foundation Models\n");
  341. retval = MS_config_server_init(element_handle, &UI_config_server_model_handle);
  342. CONSOLE_OUT("Config Model Server Registration Status: 0x%04X\n", retval);
  343. /* Health Server */
  344. company_id = 0x0000;
  345. self_tests = &UI_health_server_self_tests[0];
  346. num_self_tests = sizeof(UI_health_server_self_tests)/sizeof(MS_HEALTH_SERVER_SELF_TEST);
  347. retval = MS_health_server_init
  348. (
  349. element_handle,
  350. &UI_health_server_model_handle,
  351. company_id,
  352. self_tests,
  353. num_self_tests,
  354. UI_health_server_cb
  355. );
  356. if (API_SUCCESS == retval)
  357. {
  358. CONSOLE_OUT(
  359. "Health Server Initialized. Model Handle: 0x%04X\n",
  360. UI_health_server_model_handle);
  361. }
  362. else
  363. {
  364. CONSOLE_OUT(
  365. "[ERR] Sensor Server Initialization Failed. Result: 0x%04X\n",
  366. retval);
  367. }
  368. return retval;
  369. }
  370. /* ---- Generic OnOff States */
  371. static MS_STATE_GENERIC_ONOFF_STRUCT UI_generic_onoff;
  372. /** -- Vendor Defined States */
  373. static MS_STATE_VENDOR_EXAMPLE_STRUCT UI_vendor_example;
  374. /* Get/Set State Handlers */
  375. /* Generic OnOff Model state Initialization */
  376. void UI_generic_onoff_model_states_initialization(void)
  377. {
  378. EM_mem_set(&UI_generic_onoff, 0, sizeof(UI_generic_onoff));
  379. }
  380. /* Generic OnOff Model Get Handler */
  381. API_RESULT UI_generic_onoff_model_state_get(UINT16 state_t, UINT16 state_inst, void * param, UINT8 direction)
  382. {
  383. API_RESULT retval;
  384. retval = API_SUCCESS;
  385. switch(state_t)
  386. {
  387. case MS_STATE_GENERIC_ONOFF_T:
  388. {
  389. MS_STATE_GENERIC_ONOFF_STRUCT * param_p;
  390. param_p = (MS_STATE_GENERIC_ONOFF_STRUCT *)param;
  391. /* Ignoring Instance and direction right now */
  392. *param_p = UI_generic_onoff;
  393. }
  394. break;
  395. default:
  396. break;
  397. }
  398. return retval;
  399. }
  400. /* Generic OnOff Model Set Handler */
  401. API_RESULT UI_generic_onoff_model_state_set(UINT16 state_t, UINT16 state_inst, void * param, UINT8 direction)
  402. {
  403. API_RESULT retval;
  404. retval = API_SUCCESS;
  405. switch (state_t)
  406. {
  407. case MS_STATE_GENERIC_ONOFF_T:
  408. {
  409. MS_STATE_GENERIC_ONOFF_STRUCT * param_p;
  410. param_p = (MS_STATE_GENERIC_ONOFF_STRUCT *)param;
  411. /* Instantaneous Change */
  412. UI_generic_onoff.onoff = param_p->onoff;
  413. *param_p = UI_generic_onoff;
  414. CONSOLE_OUT("[state] current: 0x%02X\n", UI_generic_onoff.onoff);
  415. CONSOLE_OUT("[state] target: 0x%02X\n", UI_generic_onoff.target_onoff);
  416. CONSOLE_OUT("[state] remaining_time: 0x%02X\n", UI_generic_onoff.transition_time);
  417. generic_onoff_set_pl(param_p->onoff);
  418. /* Ignoring Instance and direction right now */
  419. }
  420. break;
  421. default:
  422. break;
  423. }
  424. return retval;
  425. }
  426. /* Vendor Defined Model state Initialization */
  427. void UI_vendor_defined_model_states_initialization(void)
  428. {
  429. /* Vendor Defined States */
  430. EM_mem_set (&UI_vendor_example, 0, sizeof(UI_vendor_example));
  431. }
  432. /* Vendor Defined Model Get Handler */
  433. void UI_vendor_example_model_state_get(UINT16 state_t, UINT16 state_inst, void * param, UINT8 direction)
  434. {
  435. switch(state_t)
  436. {
  437. case MS_STATE_VENDOR_EXAMPLE_T:
  438. {
  439. MS_STATE_VENDOR_EXAMPLE_STRUCT * param_p;
  440. param_p = (MS_STATE_VENDOR_EXAMPLE_STRUCT *)param;
  441. /* Ignoring Instance and direction right now */
  442. *param_p = UI_vendor_example;
  443. }
  444. break;
  445. default:
  446. break;
  447. }
  448. }
  449. /* Vendor Defined Model Set Handler */
  450. void UI_vendor_example_model_state_set(UINT16 state_t, UINT16 state_inst, void * param, UINT8 direction)
  451. {
  452. switch(state_t)
  453. {
  454. case MS_STATE_VENDOR_EXAMPLE_T:
  455. {
  456. MS_STATE_VENDOR_EXAMPLE_STRUCT * param_p;
  457. param_p = (MS_STATE_VENDOR_EXAMPLE_STRUCT *)param;
  458. /* Ignoring Instance and direction right now */
  459. UI_vendor_example = *param_p;
  460. }
  461. break;
  462. default:
  463. break;
  464. }
  465. }
  466. /* Model state Initialization */
  467. void UI_model_states_initialization(void)
  468. {
  469. /* Generic OnOff States */
  470. UI_generic_onoff_model_states_initialization();
  471. /* Vendor Defined States */
  472. UI_vendor_defined_model_states_initialization();
  473. }
  474. /* Generic OnOff Model Server */
  475. /**
  476. * \brief Server Application Asynchronous Notification Callback.
  477. *
  478. * \par Description
  479. * Generic_Onoff server calls the registered callback to indicate events occurred to the application.
  480. *
  481. * \param [in] ctx Context of message received for a specific model instance.
  482. * \param [in] msg_raw Uninterpreted/raw received message.
  483. * \param [in] req_type Requested message type.
  484. * \param [in] state_params Model specific state parameters.
  485. * \param [in] ext_params Additional parameters.
  486. */
  487. API_RESULT UI_generic_onoff_server_cb
  488. (
  489. /* IN */ MS_ACCESS_MODEL_REQ_MSG_CONTEXT * ctx,
  490. /* IN */ MS_ACCESS_MODEL_REQ_MSG_RAW * msg_raw,
  491. /* IN */ MS_ACCESS_MODEL_REQ_MSG_T * req_type,
  492. /* IN */ MS_ACCESS_MODEL_STATE_PARAMS * state_params,
  493. /* IN */ MS_ACCESS_MODEL_EXT_PARAMS * ext_params
  494. )
  495. {
  496. MS_STATE_GENERIC_ONOFF_STRUCT param;
  497. MS_ACCESS_MODEL_STATE_PARAMS current_state_params;
  498. API_RESULT retval;
  499. retval = API_SUCCESS;
  500. /* Check message type */
  501. if (MS_ACCESS_MODEL_REQ_MSG_T_GET == req_type->type)
  502. {
  503. CONSOLE_OUT("[GENERIC_ONOFF] GET Request.\n");
  504. UI_generic_onoff_model_state_get(state_params->state_type, 0, &param, 0);
  505. current_state_params.state_type = state_params->state_type;
  506. current_state_params.state = &param;
  507. /* Using same as target state and remaining time as 0 */
  508. }
  509. else if (MS_ACCESS_MODEL_REQ_MSG_T_SET == req_type->type)
  510. {
  511. CONSOLE_OUT("[GENERIC_ONOFF] SET Request.\n");
  512. retval = UI_generic_onoff_model_state_set(state_params->state_type, 0, (MS_STATE_GENERIC_ONOFF_STRUCT *)state_params->state, 0);
  513. current_state_params.state_type = state_params->state_type;
  514. current_state_params.state = (MS_STATE_GENERIC_ONOFF_STRUCT *)state_params->state;
  515. }
  516. /* See if to be acknowledged */
  517. if (0x01 == req_type->to_be_acked)
  518. {
  519. CONSOLE_OUT("[GENERIC_ONOFF] Sending Response.\n");
  520. /* Parameters: Request Context, Current State, Target State (NULL: to be ignored), Remaining Time (0: to be ignored), Additional Parameters (NULL: to be ignored) */
  521. retval = MS_generic_onoff_server_state_update(ctx, &current_state_params, NULL, 0, NULL);
  522. }
  523. return retval;
  524. }
  525. API_RESULT UI_register_generic_onoff_model_server
  526. (
  527. MS_ACCESS_ELEMENT_HANDLE element_handle
  528. )
  529. {
  530. /* Generic OnOff Server */
  531. MS_ACCESS_MODEL_HANDLE UI_generic_onoff_server_model_handle;
  532. API_RESULT retval;
  533. CONSOLE_OUT("In Generic OnOff Model Server\n");
  534. retval = MS_generic_onoff_server_init
  535. (
  536. element_handle,
  537. &UI_generic_onoff_server_model_handle,
  538. UI_generic_onoff_server_cb
  539. );
  540. if (API_SUCCESS == retval)
  541. {
  542. CONSOLE_OUT(
  543. "Generic Onoff Server Initialized. Model Handle: 0x%04X\n",
  544. UI_generic_onoff_server_model_handle);
  545. }
  546. else
  547. {
  548. CONSOLE_OUT(
  549. "[ERR] Generic Onoff Server Initialization Failed. Result: 0x%04X\n",
  550. retval);
  551. }
  552. return retval;
  553. }
  554. /* Vendor Defined Model Server */
  555. /**
  556. * \brief Server Application Asynchronous Notification Callback.
  557. *
  558. * \par Description
  559. * Vendor_Example_1 server calls the registered callback to indicate events occurred to the application.
  560. *
  561. * \param [in] ctx Context of message received for a specific model instance.
  562. * \param [in] msg_raw Uninterpreted/raw received message.
  563. * \param [in] req_type Requested message type.
  564. * \param [in] state_params Model specific state parameters.
  565. * \param [in] ext_params Additional parameters.
  566. */
  567. API_RESULT UI_vendor_example_server_cb
  568. (
  569. /* IN */ MS_ACCESS_MODEL_REQ_MSG_CONTEXT * ctx,
  570. /* IN */ MS_ACCESS_MODEL_REQ_MSG_RAW * msg_raw,
  571. /* IN */ MS_ACCESS_MODEL_REQ_MSG_T * req_type,
  572. /* IN */ MS_ACCESS_MODEL_STATE_PARAMS * state_params,
  573. /* IN */ MS_ACCESS_MODEL_EXT_PARAMS * ext_params
  574. )
  575. {
  576. MS_STATE_VENDOR_EXAMPLE_STRUCT param;
  577. MS_ACCESS_MODEL_STATE_PARAMS current_state_params;
  578. API_RESULT retval;
  579. retval = API_SUCCESS;
  580. /* Check message type */
  581. if (MS_ACCESS_MODEL_REQ_MSG_T_GET == req_type->type)
  582. {
  583. // CONSOLE_OUT(
  584. // "[VENDOR_EXAMPLE] GET Request.\n");
  585. UI_vendor_example_model_state_get(state_params->state_type, 0, &param, 0);
  586. current_state_params.state_type = state_params->state_type;
  587. current_state_params.state = &param;
  588. }
  589. else if (MS_ACCESS_MODEL_REQ_MSG_T_SET == req_type->type)
  590. {
  591. // CONSOLE_OUT(
  592. // "[VENDOR_EXAMPLE] SET Request.\n");
  593. UI_vendor_example_model_state_set(state_params->state_type, 0, (MS_STATE_VENDOR_EXAMPLE_STRUCT *)state_params->state, 0);
  594. current_state_params.state_type = state_params->state_type;
  595. current_state_params.state = (MS_STATE_VENDOR_EXAMPLE_STRUCT *)state_params->state;
  596. }
  597. /* See if to be acknowledged */
  598. if (0x01 == req_type->to_be_acked)
  599. {
  600. // CONSOLE_OUT(
  601. // "[VENDOR_EXAMPLE] Sending Response.\n");
  602. /* Parameters: Request Context, Current State, Target State (NULL: to be ignored), Remaining Time (0: to be ignored), Additional Parameters (NULL: to be ignored) */
  603. retval = MS_vendor_example_server_state_update(ctx, &current_state_params, NULL, 0, NULL);
  604. }
  605. return retval;
  606. }
  607. API_RESULT UI_register_vendor_defined_model_server
  608. (
  609. MS_ACCESS_ELEMENT_HANDLE element_handle
  610. )
  611. {
  612. /* Vendor Defined Server */
  613. MS_ACCESS_MODEL_HANDLE UI_vendor_defined_server_model_handle;
  614. API_RESULT retval;
  615. retval = MS_vendor_example_server_init
  616. (
  617. element_handle,
  618. &UI_vendor_defined_server_model_handle,
  619. UI_vendor_example_server_cb
  620. );
  621. if (API_SUCCESS == retval)
  622. {
  623. CONSOLE_OUT(
  624. "Vendor Defined Server Initialized. Model Handle: 0x%04X\n",
  625. UI_vendor_defined_server_model_handle);
  626. }
  627. else
  628. {
  629. CONSOLE_OUT(
  630. "[ERR] Vendor Defined Server Initialization Failed. Result: 0x%04X\n",
  631. retval);
  632. }
  633. return retval;
  634. }
  635. /* Provisionee */
  636. #define UI_PROV_OUTPUT_OOB_ACTIONS \
  637. (PROV_MASK_OOOB_ACTION_BLINK | PROV_MASK_OOOB_ACTION_BEEP | \
  638. PROV_MASK_OOOB_ACTION_VIBRATE | PROV_MASK_OOOB_ACTION_NUMERIC | \
  639. PROV_MASK_OOOB_ACTION_ALPHANUMERIC)
  640. /** Output OOB Maximum size supported */
  641. #define UI_PROV_OUTPUT_OOB_SIZE 0x08
  642. /** Input OOB Actions supported */
  643. #define UI_PROV_INPUT_OOB_ACTIONS \
  644. (PROV_MASK_IOOB_ACTION_PUSH | PROV_MASK_IOOB_ACTION_TWIST | \
  645. PROV_MASK_IOOB_ACTION_NUMERIC | PROV_MASK_IOOB_ACTION_ALPHANUMERIC)
  646. /** Input OOB Maximum size supported */
  647. #define UI_PROV_INPUT_OOB_SIZE 0x08
  648. /** Beacon setup timeout in seconds */
  649. #define UI_PROV_SETUP_TIMEOUT_SECS 30
  650. /** Attention timeout for device in seconds */
  651. #define UI_PROV_DEVICE_ATTENTION_TIMEOUT 30
  652. #define PROV_AUTHVAL_SIZE_PL 16
  653. /** Authentication values for OOB Display - To be made random */
  654. #define UI_DISPLAY_AUTH_DIGIT 3
  655. #define UI_DISPLAY_AUTH_NUMERIC 35007
  656. #define UI_DISPLAY_AUTH_STRING "f00l"
  657. /** Provisioning capabilities of local device */
  658. DECL_STATIC PROV_CAPABILITIES_S UI_prov_capab =
  659. {
  660. /** Number of Elements */
  661. 0x01,
  662. /** Supported algorithms */
  663. PROV_MASK_ALGO_EC_FIPS_P256,
  664. /** Public key type */
  665. PROV_MASK_PUBKEY_OOBINFO,
  666. /** Static OOB type */
  667. PROV_MASK_STATIC_OOBINFO,
  668. /** Output OOB information */
  669. { UI_PROV_OUTPUT_OOB_ACTIONS, UI_PROV_OUTPUT_OOB_SIZE },
  670. /** Input OOB information */
  671. { UI_PROV_INPUT_OOB_ACTIONS, UI_PROV_INPUT_OOB_SIZE },
  672. };
  673. /** Unprovisioned device identifier */
  674. PROV_DEVICE_S UI_lprov_device =
  675. {
  676. /** UUID */
  677. {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF},
  678. /** OOB Flag */
  679. 0x00,
  680. /**
  681. * Encoded URI Information
  682. * For example, to give a web address, "https://www.abc.com"
  683. * the URI encoded data would be -
  684. * 0x17 0x2F 0x2F 0x77 0x77 0x77 0x2E 0x61 0x62 0x63 0x2E 0x63 0x6F 0x6D
  685. * where 0x17 is the URI encoding for https:
  686. */
  687. NULL
  688. };
  689. /** Data exchanged during Provisiong procedure */
  690. DECL_STATIC PROV_DATA_S UI_prov_data =
  691. {
  692. /** NetKey */
  693. { 0x45, 0x74, 0x68, 0x65, 0x72, 0x4d, 0x69, 0x6e, 0x64, 0x4e, 0x65, 0x74, 0x4b, 0x65, 0x79, 0x00 },
  694. /** Index of the NetKey */
  695. 0x0000,
  696. /** Flags bitmask */
  697. 0x00,
  698. /** Current value of the IV index */
  699. 0x00000001,
  700. /** Unicast address of the primary element */
  701. 0x0002
  702. };
  703. /** Current role of application - Provisioner/Device */
  704. DECL_STATIC UCHAR UI_prov_role;
  705. /** Provisioning Handle */
  706. DECL_STATIC PROV_HANDLE UI_prov_handle;
  707. API_RESULT UI_prov_callback
  708. (
  709. PROV_HANDLE * phandle,
  710. UCHAR event_type,
  711. API_RESULT event_result,
  712. void * event_data,
  713. UINT16 event_datalen
  714. )
  715. {
  716. PROV_DEVICE_S * rdev;
  717. PROV_CAPABILITIES_S * rcap;
  718. PROV_DATA_S * rdata;
  719. PROV_OOB_TYPE_S * oob_info;
  720. API_RESULT retval;
  721. UCHAR i;
  722. UCHAR authstr[PROV_AUTHVAL_SIZE_PL << 1];
  723. UINT32 authnum;
  724. UCHAR authtype;
  725. UCHAR * pauth;
  726. UINT16 authsize;
  727. UCHAR pdata[(MS_DEVICE_UUID_SIZE * 2) + 1];
  728. UCHAR * t_data;
  729. switch (event_type)
  730. {
  731. case PROV_EVT_PROVISIONING_SETUP:
  732. CONSOLE_OUT("Recvd PROV_EVT_PROVISIONING_SETUP\n");
  733. CONSOLE_OUT("Status - 0x%04X\n", event_result);
  734. /* Display the attention timeout */
  735. CONSOLE_OUT("Attention TImeout - %d\n", *((UCHAR *)event_data));
  736. break;
  737. case PROV_EVT_OOB_DISPLAY:
  738. CONSOLE_OUT("Recvd PROV_EVT_OOB_DISPLAY\n");
  739. CONSOLE_OUT("Status - 0x%04X\n", event_result);
  740. /* Reference the Authenticatio Type information */
  741. oob_info = (PROV_OOB_TYPE_S *)event_data;
  742. CONSOLE_OUT("Authenticaion Action - 0x%02X\n", oob_info->action);
  743. CONSOLE_OUT("Authenticaion Size - 0x%02X\n", oob_info->size);
  744. /* If role is Device, the action is of Output OOB, else Input OOB */
  745. if (PROV_ROLE_DEVICE == UI_prov_role)
  746. {
  747. if (PROV_OOOB_ACTION_ALPHANUMERIC == oob_info->action)
  748. {
  749. authtype = 1;
  750. }
  751. else if (PROV_OOOB_ACTION_NUMERIC == oob_info->action)
  752. {
  753. authtype = 2;
  754. }
  755. else
  756. {
  757. authtype = 0;
  758. }
  759. }
  760. else
  761. {
  762. if (PROV_IOOB_ACTION_ALPHANUMERIC == oob_info->action)
  763. {
  764. authtype = 1;
  765. }
  766. else if (PROV_IOOB_ACTION_NUMERIC == oob_info->action)
  767. {
  768. authtype = 2;
  769. }
  770. else
  771. {
  772. authtype = 0;
  773. }
  774. }
  775. if (1 == authtype)
  776. {
  777. EM_str_copy (authstr, UI_DISPLAY_AUTH_STRING);
  778. CONSOLE_OUT("\n\n>>> AuthVal - %s <<<\n\n", authstr);
  779. pauth = authstr;
  780. authsize = EM_str_len(authstr);
  781. }
  782. else if (2 == authtype)
  783. {
  784. authnum = (UINT32)UI_DISPLAY_AUTH_NUMERIC;
  785. CONSOLE_OUT("\n\n>>> AuthVal - %d <<<\n\n", authnum);
  786. pauth = (UCHAR *)&authnum;
  787. authsize = sizeof(UINT32);
  788. }
  789. else
  790. {
  791. authnum = (UINT32)UI_DISPLAY_AUTH_DIGIT;
  792. CONSOLE_OUT("\n\n>>> AuthVal - %d <<<\n\n", authnum);
  793. pauth = (UCHAR *)&authnum;
  794. authsize = sizeof(UINT32);
  795. }
  796. /* Call to input the oob */
  797. CONSOLE_OUT("Setting the Authval...\n");
  798. retval = MS_prov_set_authval(&UI_prov_handle, pauth, authsize);
  799. CONSOLE_OUT("Retval - 0x%04X\n", retval);
  800. break;
  801. case PROV_EVT_OOB_ENTRY:
  802. CONSOLE_OUT("Recvd PROV_EVT_OOB_ENTRY\n");
  803. CONSOLE_OUT("Status - 0x%04X\n", event_result);
  804. /* Reference the Authenticatio Type information */
  805. oob_info = (PROV_OOB_TYPE_S *)event_data;
  806. CONSOLE_OUT("Authenticaion Action - 0x%02X\n", oob_info->action);
  807. CONSOLE_OUT("Authenticaion Size - 0x%02X\n", oob_info->size);
  808. break;
  809. case PROV_EVT_DEVINPUT_COMPLETE:
  810. CONSOLE_OUT("Recvd PROV_EVT_DEVINPUT_COMPLETE\n");
  811. CONSOLE_OUT("Status - 0x%04X\n", event_result);
  812. break;
  813. case PROV_EVT_PROVDATA_INFO:
  814. CONSOLE_OUT("Recvd PROV_EVT_PROVDATA_INFO\n");
  815. CONSOLE_OUT("Status - 0x%04X\n", event_result);
  816. /* Reference the Provisioning Data */
  817. rdata = (PROV_DATA_S *)event_data;
  818. CONSOLE_OUT("NetKey : "); appl_dump_bytes(rdata->netkey, PROV_KEY_NETKEY_SIZE);
  819. CONSOLE_OUT("Key ID : 0x%04X\n", rdata->keyid);
  820. CONSOLE_OUT("Flags : 0x%02X\n", rdata->flags);
  821. CONSOLE_OUT("IVIndex : 0x%08X\n", rdata->ivindex);
  822. CONSOLE_OUT("UAddr : 0x%04X\n", rdata->uaddr);
  823. /* Provide Provisioning Data to Access Layer */
  824. MS_access_cm_set_prov_data
  825. (
  826. rdata
  827. );
  828. break;
  829. case PROV_EVT_PROVISIONING_COMPLETE:
  830. CONSOLE_OUT("Recvd PROV_EVT_PROVISIONING_COMPLETE\n");
  831. CONSOLE_OUT("Status - 0x%04X\n", event_result);
  832. if (API_SUCCESS == event_result)
  833. {
  834. /* Already Set while handling PROV_EVT_PROVDATA_INFO */
  835. }
  836. break;
  837. default:
  838. CONSOLE_OUT("Unknown Event - 0x%02X\n", event_type);
  839. }
  840. return API_SUCCESS;
  841. }
  842. void UI_register_prov(void)
  843. {
  844. API_RESULT retval;
  845. CONSOLE_OUT("Registering with Provisioning layer...\n");
  846. retval = MS_prov_register(&UI_prov_capab, UI_prov_callback);
  847. CONSOLE_OUT("Retval - 0x%04X\n", retval);
  848. }
  849. void UI_setup_prov(UCHAR role, UCHAR brr)
  850. {
  851. API_RESULT retval;
  852. if (PROV_ROLE_PROVISIONER != role)
  853. {
  854. CONSOLE_OUT("Setting up Device for Provisioning ...\n");
  855. retval = MS_prov_setup
  856. (
  857. brr,
  858. role,
  859. &UI_lprov_device,
  860. UI_PROV_SETUP_TIMEOUT_SECS
  861. );
  862. UI_prov_role = PROV_ROLE_DEVICE;
  863. }
  864. else
  865. {
  866. CONSOLE_OUT("Setting up Provisioner for Provisioning ...\n");
  867. retval = MS_prov_setup
  868. (
  869. brr,
  870. role,
  871. NULL,
  872. UI_PROV_SETUP_TIMEOUT_SECS
  873. );
  874. UI_prov_role = PROV_ROLE_PROVISIONER;
  875. }
  876. CONSOLE_OUT("Retval - 0x%04X\n", retval);
  877. }
  878. void UI_prov_bind(UCHAR brr, UCHAR index)
  879. {
  880. API_RESULT retval;
  881. /* Call to bind with the selected device */
  882. CONSOLE_OUT("Binding with the selected device...\n");
  883. retval = MS_prov_bind(brr, &UI_lprov_device, UI_PROV_DEVICE_ATTENTION_TIMEOUT, &UI_prov_handle);
  884. CONSOLE_OUT("Retval - 0x%04X\n", retval);
  885. }
  886. void appl_mesh_sample (void)
  887. {
  888. MS_ACCESS_NODE_ID node_id;
  889. MS_ACCESS_ELEMENT_DESC element;
  890. MS_ACCESS_ELEMENT_HANDLE element_handle;
  891. API_RESULT retval;
  892. UCHAR role, brr;
  893. MS_CONFIG * config_ptr;
  894. #ifdef MS_HAVE_DYNAMIC_CONFIG
  895. MS_CONFIG config;
  896. /* Initialize dynamic configuration */
  897. MS_INIT_CONFIG(config);
  898. config_ptr = &config;
  899. #else
  900. config_ptr = NULL;
  901. #endif /* MS_HAVE_DYNAMIC_CONFIG */
  902. /* Initialize OSAL */
  903. EM_os_init();
  904. /* Initialize Debug Module */
  905. EM_debug_init();
  906. /* Initialize Timer Module */
  907. EM_timer_init();
  908. timer_em_init();
  909. /* Initialize utilities */
  910. nvsto_init();
  911. /* Initialize Mesh Stack */
  912. MS_init(config_ptr);
  913. /* Register with underlying BLE stack */
  914. blebrr_register();
  915. /* Create Node */
  916. retval = MS_access_create_node(&node_id);
  917. /* Register Element */
  918. /**
  919. * TBD: Define GATT Namespace Descriptions from
  920. * https://www.bluetooth.com/specifications/assigned-numbers/gatt-namespace-descriptors
  921. *
  922. * Using 'main' (0x0106) as Location temporarily.
  923. */
  924. element.loc = 0x0106;
  925. retval = MS_access_register_element
  926. (
  927. node_id,
  928. &element,
  929. &element_handle
  930. );
  931. if (API_SUCCESS == retval)
  932. {
  933. /* Register foundation model servers */
  934. retval = UI_register_foundation_model_servers(element_handle);
  935. }
  936. if (API_SUCCESS == retval)
  937. {
  938. /* Register Generic OnOff model server */
  939. retval = UI_register_generic_onoff_model_server(element_handle);
  940. }
  941. if (API_SUCCESS == retval)
  942. {
  943. /* Register Vendor Defined model server */
  944. retval = UI_register_vendor_defined_model_server(element_handle);
  945. }
  946. if (API_SUCCESS == retval)
  947. {
  948. /* Initialize model states */
  949. UI_model_states_initialization();
  950. }
  951. /* Configure as provisionee/device */
  952. UI_register_prov();
  953. /**
  954. * setup <role:[1 - Device, 2 - Provisioner]> <bearer:[1 - Adv, 2 - GATT]
  955. */
  956. role = PROV_ROLE_DEVICE;
  957. brr = PROV_BRR_ADV;
  958. UI_setup_prov(role, brr);
  959. UI_prov_bind(brr, 0x00);
  960. return;
  961. }
  962. #endif /* (MESH_STANDALONE == 6) */