mesh_clients.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. /**
  2. * \file mesh_clients.c
  3. *
  4. *
  5. */
  6. /*
  7. * Copyright (C) 2018. Mindtree Limited.
  8. * All rights reserved.
  9. */
  10. /* --------------------------------------------- Header File Inclusion */
  11. #include "mesh_clients.h"
  12. /* --------------------------------------------- Global Definitions */
  13. /* Compilation Switch to have this module print trace on console */
  14. #define MESH_CLIENT_CONSOLE_DEBUG
  15. /* --------------------------------------------- Macros */
  16. #ifdef MESH_CLIENT_CONSOLE_DEBUG
  17. #define MESH_CLIENT_TRC(...) printf(__VA_ARGS__)
  18. #else /* MESH_CLIENT_CONSOLE_DEBUG */
  19. #define MESH_CLIENT_TRC(...)
  20. #endif /* MESH_CLIENT_CONSOLE_DEBUG */
  21. /* --------------------------------------------- External Global Variables */
  22. /* --------------------------------------------- Exported Global Variables */
  23. /* --------------------------------------------- Static Global Variables */
  24. /**
  25. * This Task ID is the identifier used by the GATT APIs to reference
  26. * the BLE Mesh Application Task.
  27. * The definition and usage of this is from the bleMesh.c application
  28. * file.
  29. */
  30. extern uint8_t bleMesh_TaskID;
  31. /* --------------------------------------------- Functions */
  32. #define MESH_MAX_CLI_ENV 1
  33. /* Global variable definition */
  34. struct mesh_cli_env_tag mesh_cli_env[MESH_MAX_CLI_ENV];
  35. /* Mesh Provisioning Service Client Related Callbacks */
  36. static mesh_prov_client_cb * prov_cli_cb;
  37. /* Mesh Proxy Service Client Related Callbacks */
  38. static mesh_proxy_client_cb * proxy_cli_cb;
  39. /* Track current UUID being Discovered */
  40. static uint16_t mesh_client_curr_dis_uuid;
  41. /* ----------------------------------------------------------------------------
  42. * Function : void mesh_client_init(void)
  43. * ----------------------------------------------------------------------------
  44. * Description : Initialize Mesh Client environment
  45. * Inputs : None
  46. * Outputs : None
  47. * Assumptions : None
  48. * ------------------------------------------------------------------------- */
  49. void mesh_client_init(void)
  50. {
  51. for (unsigned int i = 0; i < MESH_MAX_CLI_ENV; i++)
  52. {
  53. /* Reset the application manager environment */
  54. memset(&mesh_cli_env[i], 0, sizeof(struct mesh_cli_env_tag));
  55. }
  56. mesh_client_curr_dis_uuid = 0x0000;
  57. prov_cli_cb = NULL;
  58. proxy_cli_cb = NULL;
  59. }
  60. /**
  61. * Routine to update the connection identifier for Mesh.
  62. */
  63. void mesh_client_update_conidx (uint16_t conidx)
  64. {
  65. mesh_cli_env[0].conidx = conidx;
  66. }
  67. /**
  68. * \brief Register Mesh Provisioning Client instance
  69. *
  70. * Function registers new Mesh Provisioning Client instance.
  71. *
  72. * \param [in] cb client application callbacks
  73. *
  74. * \return None
  75. *
  76. */
  77. void mesh_prov_client_init(mesh_prov_client_cb * cb)
  78. {
  79. /* Register the upper layer provided Provisioning Client Callbacks */
  80. if (NULL != cb)
  81. {
  82. prov_cli_cb = cb;
  83. }
  84. }
  85. /**
  86. * \brief Register Mesh Proxy Client instance
  87. *
  88. * Function registers new Mesh Proxy Client instance.
  89. *
  90. * \param [in] cb client application callbacks
  91. *
  92. * \return None
  93. *
  94. */
  95. void mesh_proxy_client_init(mesh_proxy_client_cb * cb)
  96. {
  97. /* Register the upper layer provided Provisioning Client Callbacks */
  98. if (NULL != cb)
  99. {
  100. proxy_cli_cb = cb;
  101. }
  102. }
  103. API_RESULT mesh_client_discover_services
  104. (
  105. uint16_t conidx,
  106. uint8_t serv_mode
  107. )
  108. {
  109. uint8_t svc_uuid[2];
  110. bStatus_t ret;
  111. if (BLEBRR_GATT_PROV_MODE == serv_mode)
  112. {
  113. /* UUID is Provisioning Service */
  114. svc_uuid[0] = (uint8_t)(UUID_MESH_PROVISIONING_SERVICE);
  115. svc_uuid[1] = (uint8_t)(UUID_MESH_PROVISIONING_SERVICE >> 8);
  116. /* EM_mem_copy(svc_uuid, MESH_PROV_SERVICE_UUID128, 16); */
  117. mesh_client_curr_dis_uuid = UUID_MESH_PROVISIONING_SERVICE;
  118. }
  119. else
  120. {
  121. /* UUID is Proxy Service */
  122. svc_uuid[0] = (uint8_t)(UUID_MESH_PROXY_SERVICE);
  123. svc_uuid[1] = (uint8_t)(UUID_MESH_PROXY_SERVICE >> 8);
  124. /* EM_mem_copy(svc_uuid, MESH_PROXY_SERVICE_UUID128, 16); */
  125. mesh_client_curr_dis_uuid = UUID_MESH_PROVISIONING_SERVICE;
  126. }
  127. /* Discover service by UUID */
  128. ret = GATT_DiscPrimaryServiceByUUID
  129. (
  130. conidx,
  131. svc_uuid,
  132. ATT_BT_UUID_SIZE,
  133. bleMesh_TaskID
  134. );
  135. MESH_CLIENT_TRC(
  136. "Discovery Primiary Service UUID 0x%04X "
  137. "returned with retval 0x%04X\r\n",
  138. mesh_client_curr_dis_uuid, ret);
  139. return (0 == ret) ? API_SUCCESS : API_FAILURE;
  140. }
  141. /* ----------------------------------------------------------------------------
  142. * Function : void mesh_client_send_wwr(uint8_t conidx, uint8_t *value,
  143. * uint16_t handle, uint8_t offset,
  144. * uint16_t length, uint8_t type)
  145. * ----------------------------------------------------------------------------
  146. * Description : Send a write command or request to the client device
  147. * Inputs : - conidx - Connection index
  148. * - value - Pointer to value
  149. * - handle - Attribute handle
  150. * - length - Length of value
  151. * - type - Type of write message
  152. * Outputs : None
  153. * Assumptions : None
  154. * ------------------------------------------------------------------------- */
  155. void mesh_client_send_wwr
  156. (
  157. uint16_t conidx,
  158. uint8_t * value,
  159. uint16_t length,
  160. uint8_t serv_pref
  161. )
  162. {
  163. attWriteReq_t req;
  164. bStatus_t ret;
  165. /* Assign the Handle Based on the Service Preference for Write */
  166. req.handle = (BLEBRR_GATT_PROV_MODE == serv_pref) ? \
  167. mesh_cli_env[0].prov_data_in_hdl : \
  168. mesh_cli_env[0].proxy_data_in_hdl;
  169. req.len = length;
  170. req.sig = 0x00;
  171. req.cmd = 0x01; /* Write witout response */
  172. osal_memcpy(req.value, value, length);
  173. ret = GATT_WriteNoRsp(conidx, &req);
  174. MESH_CLIENT_TRC(
  175. "Writing data : len 0x%04X : handle 0x%02X : ret 0x%04X\n",
  176. req.len, req.handle, ret);
  177. return;
  178. }
  179. /* ----------------------------------------------------------------------------
  180. * Function : void mesh_client_config_ntf
  181. * (
  182. * uint16_t conidx,
  183. * uint8_t serv_pref,
  184. * uint8_t flag
  185. * )
  186. * ----------------------------------------------------------------------------
  187. * Description : Send a write command or request to the client device
  188. * Inputs : - conidx - Connection index
  189. * - serv_pref - Provisioning or Proxy Service
  190. * - flag - enable or disable
  191. * Outputs : None
  192. * Assumptions : None
  193. * ------------------------------------------------------------------------- */
  194. API_RESULT mesh_client_config_ntf
  195. (
  196. uint16_t conidx,
  197. uint8_t serv_pref,
  198. uint8_t flag
  199. )
  200. {
  201. attWriteReq_t req;
  202. bStatus_t ret;
  203. /* Assign the Handle Based on the Service Preference for Write */
  204. /**
  205. * Increment the Handle by 1 as the CCCD is present in the handle next to
  206. * the Value handle
  207. */
  208. req.handle = (BLEBRR_GATT_PROV_MODE == serv_pref) ? \
  209. mesh_cli_env[0].prov_data_out_cccd_hdl : \
  210. mesh_cli_env[0].proxy_data_out_cccd_hdl;
  211. req.len = 0x02;
  212. req.sig = 0x00;
  213. req.cmd = 0x00;
  214. req.value[0] = (true == flag) ? 0x01 : 0x00;
  215. req.value[1] = 0x00;
  216. /* Store CCCD Mode and State in global */
  217. mesh_cli_env[0].curr_notif_state = flag;
  218. mesh_cli_env[0].curr_notif_mode = (BLEBRR_GATT_PROV_MODE == serv_pref) ? \
  219. BLEBRR_GATT_PROV_MODE : \
  220. BLEBRR_GATT_PROXY_MODE;
  221. ret = GATT_WriteCharValue(mesh_cli_env[0].conidx, &req, bleMesh_TaskID);
  222. MESH_CLIENT_TRC
  223. ("Writing %d to CCCD 0x%04X for Mode 0x%02X\n",
  224. flag, req.handle, serv_pref);
  225. return (0 == ret) ? API_SUCCESS : API_FAILURE;
  226. }
  227. void mesh_client_process_gattMsg
  228. (
  229. gattMsgEvent_t *pMsg,
  230. uint8_t t_id
  231. )
  232. {
  233. bStatus_t ret;
  234. uint16_t serv_start_hndl, serv_end_hndl;
  235. MESH_CLIENT_TRC("Processing GATT Messages:\n");
  236. MESH_CLIENT_TRC("pMsg->method: 0x%X\n",pMsg->method);
  237. MESH_CLIENT_TRC("pMsg->hdr.status: 0x%X\n",pMsg->hdr.status);
  238. /* Cache the Task ID */
  239. bleMesh_TaskID = t_id;
  240. /* Process the GATT server message */
  241. switch ( pMsg->method )
  242. {
  243. case ATT_EXCHANGE_MTU_RSP:
  244. break;
  245. case ATT_FIND_BY_TYPE_VALUE_RSP:
  246. {
  247. if (bleProcedureComplete == pMsg->hdr.status)
  248. {
  249. if (UUID_MESH_PROVISIONING_SERVICE == mesh_client_curr_dis_uuid)
  250. {
  251. serv_start_hndl = mesh_cli_env[0].prov_start_hdl;
  252. serv_end_hndl = mesh_cli_env[0].prov_end_hdl;
  253. }
  254. else if (UUID_MESH_PROXY_SERVICE == mesh_client_curr_dis_uuid)
  255. {
  256. serv_start_hndl = mesh_cli_env[0].proxy_start_hdl;
  257. serv_end_hndl = mesh_cli_env[0].proxy_end_hdl;
  258. }
  259. else
  260. {
  261. MESH_CLIENT_TRC( "Mesh Services not Present\r\n");
  262. }
  263. /* Start Charactertistic Discovery */
  264. ret = GATT_DiscAllChars
  265. (
  266. mesh_cli_env[0].conidx,
  267. serv_start_hndl,
  268. serv_end_hndl,
  269. bleMesh_TaskID
  270. );
  271. MESH_CLIENT_TRC(
  272. "Disc Characteristics btw Handles 0x%04X :: 0x%04X"
  273. "returned with retval 0x%04X\r\n",
  274. serv_start_hndl, serv_end_hndl, ret);
  275. }
  276. else
  277. {
  278. if (pMsg->msg.findByTypeValueRsp.numInfo > 0)
  279. {
  280. MESH_CLIENT_TRC(
  281. "\nServices found :%d\n",
  282. pMsg->msg.findByTypeValueRsp.numInfo);
  283. MESH_CLIENT_TRC(
  284. "Service Handles are 0x%04X :: 0x%04X",
  285. pMsg->msg.findByTypeValueRsp.handlesInfo[0].handle,
  286. pMsg->msg.findByTypeValueRsp.handlesInfo[0].grpEndHandle);
  287. if (mesh_client_curr_dis_uuid == UUID_MESH_PROVISIONING_SERVICE)
  288. {
  289. mesh_cli_env[0].prov_start_hdl = pMsg->msg.findByTypeValueRsp.handlesInfo[0].handle;
  290. mesh_cli_env[0].prov_end_hdl = pMsg->msg.findByTypeValueRsp.handlesInfo[0].grpEndHandle;
  291. MESH_CLIENT_TRC(
  292. "\r\n Prov Service Start Handle is 0x%04X\r\n",
  293. mesh_cli_env[0].prov_start_hdl);
  294. MESH_CLIENT_TRC(
  295. "\r\n Prov Service End Handle is 0x%04X\r\n",
  296. mesh_cli_env[0].prov_end_hdl);
  297. }
  298. else if (mesh_client_curr_dis_uuid == UUID_MESH_PROXY_SERVICE)
  299. {
  300. mesh_cli_env[0].proxy_start_hdl = pMsg->msg.findByTypeValueRsp.handlesInfo[0].handle;
  301. mesh_cli_env[0].proxy_end_hdl = pMsg->msg.findByTypeValueRsp.handlesInfo[0].grpEndHandle;
  302. MESH_CLIENT_TRC(
  303. "\r\nProxy Service Start Handle is 0x%04X\r\n",
  304. mesh_cli_env[0].proxy_start_hdl);
  305. MESH_CLIENT_TRC(
  306. "\r\nProxy Service End Handle is 0x%04X\r\n",
  307. mesh_cli_env[0].proxy_end_hdl);
  308. }
  309. }
  310. else
  311. {
  312. MESH_CLIENT_TRC("\n No Services Found!\n");
  313. /**
  314. * TODO: Provide Discovery completion Callback.
  315. */
  316. }
  317. }
  318. }
  319. break;
  320. case ATT_READ_BY_TYPE_RSP:
  321. {
  322. if (bleProcedureComplete == pMsg->hdr.status)
  323. {
  324. uint16_t t_char_start_hndl;
  325. if (UUID_MESH_PROVISIONING_SERVICE == mesh_client_curr_dis_uuid)
  326. {
  327. t_char_start_hndl = mesh_cli_env[0].prov_data_out_hdl;
  328. serv_end_hndl = mesh_cli_env[0].prov_end_hdl;
  329. }
  330. else if (UUID_MESH_PROXY_SERVICE == mesh_client_curr_dis_uuid)
  331. {
  332. t_char_start_hndl = mesh_cli_env[0].proxy_data_out_hdl;
  333. serv_end_hndl = mesh_cli_env[0].proxy_end_hdl;
  334. }
  335. ret = GATT_DiscAllCharDescs
  336. (
  337. mesh_cli_env[0].conidx,
  338. t_char_start_hndl,
  339. serv_end_hndl,
  340. bleMesh_TaskID
  341. );
  342. MESH_CLIENT_TRC(
  343. "Disc Char Desc btw Handles 0x%04X :: 0x%04X"
  344. "returned with retval 0x%04X\r\n",
  345. t_char_start_hndl, serv_end_hndl, ret);
  346. }
  347. else
  348. {
  349. // Pointer to the pair list data in the GATT response.
  350. uint8_t *pCharPairList;
  351. // Will store the start and end handles of the current pair.
  352. uint16_t charStartHandle;
  353. // Stores to the UUID of the current pair.
  354. uint16_t charUuid;
  355. // Stores what pair the loop is currently processing.
  356. uint8_t currentCharIndex;
  357. // Set the pair pointer to the first pair.
  358. pCharPairList = pMsg->msg.readByTypeRsp.dataList;
  359. // Iterate through all three pairs found.
  360. for(currentCharIndex = 0; currentCharIndex < pMsg->msg.readByTypeRsp.numPairs ; currentCharIndex++)
  361. {
  362. /* uint16_t* p_chars_hdl = pservie->chars_hdl; */
  363. // Extract the starting handle, ending handle, and UUID of the current characteristic.
  364. charStartHandle = BUILD_UINT16(pCharPairList[3], pCharPairList[4]);
  365. charUuid = BUILD_UINT16(pCharPairList[5], pCharPairList[6]);
  366. MESH_CLIENT_TRC(
  367. "Chars found handle is %d, uuid is %x\n",
  368. charStartHandle, charUuid);
  369. switch (charUuid)
  370. {
  371. /* "Mesh Provisioning Data IN" */
  372. case UUID_MESH_PROVISIONING_DATA_IN:
  373. mesh_cli_env[0].prov_data_in_hdl = charStartHandle;
  374. break;
  375. /* "Mesh Provisioning Data OUT" */
  376. case UUID_MESH_PROVISIONING_DATA_OUT:
  377. mesh_cli_env[0].prov_data_out_hdl = charStartHandle;
  378. mesh_cli_env[0].prov_data_out_cccd_hdl = charStartHandle + 1;
  379. break;
  380. /* "Mesh Proxy Data IN" */
  381. case UUID_MESH_PROXY_DATA_IN:
  382. mesh_cli_env[0].proxy_data_in_hdl = charStartHandle;
  383. break;
  384. /* "Mesh Proxy Data OUT" */
  385. case UUID_MESH_PROXY_DATA_OUT:
  386. mesh_cli_env[0].proxy_data_out_hdl = charStartHandle;
  387. mesh_cli_env[0].proxy_data_out_cccd_hdl = charStartHandle + 1;
  388. break;
  389. }
  390. // Increment the pair pointer to the next pair.
  391. pCharPairList += 5 + 2;
  392. }
  393. }
  394. }
  395. break;
  396. case ATT_FIND_INFO_RSP:
  397. {
  398. if (bleProcedureComplete == pMsg->hdr.status)
  399. {
  400. /**
  401. * TODO: Provide Discovery completion Callback.
  402. */
  403. }
  404. else
  405. {
  406. if ( (pMsg->msg.findInfoRsp.numInfo > 0) &&
  407. (pMsg->msg.findInfoRsp.format == ATT_HANDLE_BT_UUID_TYPE) )
  408. {
  409. // This will keep track of the current pair being processed.
  410. uint8_t currentPair;
  411. // Iterate through the pair list.
  412. for(currentPair = 0; currentPair < pMsg->msg.findInfoRsp.numInfo; currentPair++)
  413. {
  414. // Check if the pair is a CCCD.
  415. uint16_t uuid = BUILD_UINT16(pMsg->msg.findInfoRsp.info.btPair[currentPair].uuid[0], pMsg->msg.findInfoRsp.info.btPair[currentPair].uuid[1]);
  416. if (uuid == GATT_CLIENT_CHAR_CFG_UUID)
  417. {
  418. MESH_CLIENT_TRC(
  419. "CCCD is found at handle 0x%X for Mode %d\n",
  420. pMsg->msg.findInfoRsp.info.btPair[currentPair].handle, blebrr_gatt_mode_get());
  421. if (BLEBRR_GATT_PROV_MODE == blebrr_gatt_mode_get())
  422. {
  423. mesh_cli_env[0].prov_data_out_cccd_hdl = pMsg->msg.findInfoRsp.info.btPair[currentPair].handle;
  424. }
  425. else
  426. {
  427. mesh_cli_env[0].proxy_data_out_cccd_hdl = pMsg->msg.findInfoRsp.info.btPair[currentPair].handle;
  428. }
  429. }
  430. }
  431. }
  432. }
  433. }
  434. break;
  435. case ATT_HANDLE_VALUE_NOTI:
  436. {
  437. uint16_t notifHandle = pMsg->msg.handleValueNoti.handle;
  438. if (pMsg->msg.handleValueNoti.len > 0)
  439. {
  440. /* Check the Attribute Handle:
  441. * - If Provisioning Data Out Handle: then call the Prov Callback
  442. * - If Proxy Data Out Handle: the ncall the Proxy Callback
  443. */
  444. if (mesh_cli_env[0].prov_data_out_hdl == notifHandle)
  445. {
  446. if (NULL != prov_cli_cb)
  447. {
  448. prov_cli_cb->mesh_prov_data_out_notif
  449. (
  450. mesh_cli_env[0].conidx,
  451. pMsg->msg.handleValueNoti.len,
  452. pMsg->msg.handleValueNoti.value
  453. );
  454. }
  455. }
  456. else if (mesh_cli_env[0].proxy_data_out_hdl == notifHandle)
  457. {
  458. if (NULL != proxy_cli_cb)
  459. {
  460. proxy_cli_cb->mesh_proxy_data_out_notif
  461. (
  462. mesh_cli_env[0].conidx,
  463. pMsg->msg.handleValueNoti.len,
  464. pMsg->msg.handleValueNoti.value
  465. );
  466. }
  467. }
  468. }
  469. }
  470. break;
  471. case ATT_WRITE_RSP:
  472. {
  473. if (BLEBRR_GATT_PROV_MODE == mesh_cli_env[0].curr_notif_mode)
  474. {
  475. /* Call the Prov CCCD ntf complete Callback */
  476. if (NULL != prov_cli_cb)
  477. {
  478. prov_cli_cb->mesh_prov_ntf_status
  479. (
  480. mesh_cli_env[0].conidx,
  481. mesh_cli_env[0].curr_notif_state,
  482. 0x00
  483. );
  484. }
  485. }
  486. else if (BLEBRR_GATT_PROXY_MODE == mesh_cli_env[0].curr_notif_mode)
  487. {
  488. /* Call the Proxy CCCD ntf complete Callback */
  489. if (NULL != proxy_cli_cb)
  490. {
  491. proxy_cli_cb->mesh_proxy_ntf_status
  492. (
  493. mesh_cli_env[0].conidx,
  494. mesh_cli_env[0].curr_notif_state,
  495. 0x00
  496. );
  497. }
  498. }
  499. else
  500. {
  501. /* DO Nothing ! */
  502. }
  503. }
  504. break;
  505. case ATT_ERROR_RSP:
  506. {
  507. attErrorRsp_t* perr = &(pMsg->msg.errorRsp);
  508. MESH_CLIENT_TRC(
  509. "\nATT_ERROR_RSP 0x%x, 0x%x, 0x%x\n",
  510. perr->errCode, perr->handle, perr->reqOpcode);
  511. }
  512. break;
  513. default:
  514. break;
  515. }
  516. (void)ret;
  517. }