mesh_services.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  1. /**
  2. * \file mesh_services.c
  3. *
  4. * Generic Module to handle both
  5. * - Mesh Provisioning Service :: 0x1827
  6. * - Mesh Proxy Service :: 0x1828
  7. */
  8. /*
  9. * Copyright (C) 2018. Mindtree Limited.
  10. * All rights reserved.
  11. */
  12. #include "mesh_services.h"
  13. /*********************************************************************
  14. * GLOBAL VARIABLES
  15. */
  16. /* Mesh Prov Service UUID: 0x1827 */
  17. CONST uint8 mesh_prov_UUID[ATT_BT_UUID_SIZE] =
  18. {
  19. LO_UINT16(MESH_PROV_SERVICE_UUID), HI_UINT16(MESH_PROV_SERVICE_UUID)
  20. };
  21. /* Mesh Prov Data IN UUID: 0x2ADB */
  22. CONST uint8 mesh_prov_data_in_UUID[ATT_BT_UUID_SIZE] =
  23. {
  24. LO_UINT16(MESH_PROV_DATA_IN_UUID), HI_UINT16(MESH_PROV_DATA_IN_UUID)
  25. };
  26. /* Mesh Prov Data OUT UUID: 0x2ADC */
  27. CONST uint8 mesh_prov_data_out_UUID[ATT_BT_UUID_SIZE] =
  28. {
  29. LO_UINT16(MESH_PROV_DATA_OUT_UUID), HI_UINT16(MESH_PROV_DATA_OUT_UUID)
  30. };
  31. /* Mesh Proxy Service UUID: 0x1828 */
  32. CONST uint8 mesh_proxy_UUID[ATT_BT_UUID_SIZE] =
  33. {
  34. LO_UINT16(MESH_PROXY_SERVICE_UUID), HI_UINT16(MESH_PROXY_SERVICE_UUID)
  35. };
  36. /* Mesh Proxy Data IN UUID: 0x2ADD */
  37. CONST uint8 mesh_proxy_data_in_UUID[ATT_BT_UUID_SIZE] =
  38. {
  39. LO_UINT16(MESH_PROXY_DATA_IN_UUID), HI_UINT16(MESH_PROXY_DATA_IN_UUID)
  40. };
  41. /* Mesh Proxy Data OUT UUID: 0x2ADE */
  42. CONST uint8 mesh_proxy_data_out_UUID[ATT_BT_UUID_SIZE] =
  43. {
  44. LO_UINT16(MESH_PROXY_DATA_OUT_UUID), HI_UINT16(MESH_PROXY_DATA_OUT_UUID)
  45. };
  46. /*********************************************************************
  47. * EXTERNAL VARIABLES
  48. */
  49. /*********************************************************************
  50. * EXTERNAL FUNCTIONS
  51. */
  52. /*********************************************************************
  53. * LOCAL VARIABLES
  54. */
  55. /* Mesh Provisioning Service Related Callbacks */
  56. static mesh_prov_cb * prov_cb;
  57. /* Mesh Proxy Service Related Callbacks */
  58. static mesh_proxy_cb * proxy_cb;
  59. /*********************************************************************
  60. * Profile Attributes - variables
  61. */
  62. /* Mesh Provisioning Service */
  63. static CONST gattAttrType_t mesh_prov_service =
  64. {ATT_BT_UUID_SIZE, mesh_prov_UUID};
  65. /* Mesh Prov Data IN Properties */
  66. static uint8 mesh_prov_data_in_props = GATT_PROP_WRITE_NO_RSP;
  67. /* Mesh Prov Data IN Value */
  68. static uint8 mesh_prov_data_in_val[20];
  69. /* Mesh Prov Data OUT Properties */
  70. static uint8 mesh_prov_data_out_props = GATT_PROP_NOTIFY;
  71. /* Mesh Prov Data OUT Value */
  72. static uint8 mesh_prov_data_out_val[20];
  73. /* Mesh Prov Data OUT CCCD value array */
  74. static gattCharCfg_t mesh_prov_data_out_cccd[GATT_MAX_NUM_CONN];
  75. /* Mesh Proxy Service */
  76. static CONST gattAttrType_t mesh_proxy_service =
  77. {ATT_BT_UUID_SIZE, mesh_proxy_UUID};
  78. /* Mesh Proxy Data IN Properties */
  79. static uint8 mesh_proxy_data_in_props = GATT_PROP_WRITE_NO_RSP;
  80. /* Mesh Proxy Data IN Value */
  81. static uint8 mesh_proxy_data_in_val[20];
  82. /* Mesh Proxy Data OUT Properties */
  83. static uint8 mesh_proxy_data_out_props = GATT_PROP_NOTIFY;
  84. /* Mesh Proxy Data OUT Value */
  85. static uint8 mesh_proxy_data_out_val[20];
  86. /* Mesh Proxy Data OUT CCCD value array */
  87. static gattCharCfg_t mesh_proxy_data_out_cccd[GATT_MAX_NUM_CONN];
  88. /*********************************************************************
  89. * Profile Attributes - Table
  90. */
  91. /* Mesh Provisioning Service Attribute Table */
  92. static gattAttribute_t mesh_prov_attr_tbl[MESH_PROV_IDX_NB] =
  93. {
  94. /* Mesh Provisioning Service Declaration */
  95. {
  96. { ATT_BT_UUID_SIZE, primaryServiceUUID }, /* type */
  97. GATT_PERMIT_READ, /* permissions */
  98. 0, /* handle */
  99. (uint8 *)&mesh_prov_service /* pValue */
  100. },
  101. /* Mesh Prov Data IN Characteristic Declaration */
  102. {
  103. { ATT_BT_UUID_SIZE, characterUUID },
  104. GATT_PERMIT_READ,
  105. 0,
  106. &mesh_prov_data_in_props
  107. },
  108. /* Mesh Prov Data IN Characteristic Value */
  109. {
  110. { ATT_BT_UUID_SIZE, mesh_prov_data_in_UUID },
  111. GATT_PERMIT_WRITE,
  112. 0,
  113. &mesh_prov_data_in_val[0]
  114. },
  115. /* Mesh Prov Data OUT Characteristic Declaration */
  116. {
  117. { ATT_BT_UUID_SIZE, characterUUID },
  118. GATT_PERMIT_READ,
  119. 0,
  120. &mesh_prov_data_out_props
  121. },
  122. /* Mesh Prov Data OUT Characteristic Value */
  123. {
  124. { ATT_BT_UUID_SIZE, mesh_prov_data_out_UUID },
  125. 0,
  126. 0,
  127. &mesh_prov_data_out_val[0]
  128. },
  129. /* Mesh Prov Data OUT CCCD */
  130. {
  131. { ATT_BT_UUID_SIZE, clientCharCfgUUID },
  132. GATT_PERMIT_READ | GATT_PERMIT_WRITE,
  133. 0,
  134. (uint8 *) &mesh_prov_data_out_cccd
  135. },
  136. };
  137. /* Mesh Proxy Service Attribute Table */
  138. static gattAttribute_t mesh_proxy_attr_tbl[MESH_PROXY_IDX_NB] =
  139. {
  140. /* Mesh Proxy Service Declaration */
  141. {
  142. { ATT_BT_UUID_SIZE, primaryServiceUUID }, /* type */
  143. GATT_PERMIT_READ, /* permissions */
  144. 0, /* handle */
  145. (uint8 *)&mesh_proxy_service /* pValue */
  146. },
  147. /* Mesh Proxy Data IN Characteristic Declaration */
  148. {
  149. { ATT_BT_UUID_SIZE, characterUUID },
  150. GATT_PERMIT_READ,
  151. 0,
  152. &mesh_proxy_data_in_props
  153. },
  154. /* Mesh Proxy Data IN Characteristic Value */
  155. {
  156. { ATT_BT_UUID_SIZE, mesh_proxy_data_in_UUID },
  157. GATT_PERMIT_WRITE,
  158. 0,
  159. &mesh_proxy_data_in_val[0]
  160. },
  161. /* Mesh Proxy Data OUT Characteristic Declaration */
  162. {
  163. { ATT_BT_UUID_SIZE, characterUUID },
  164. GATT_PERMIT_READ,
  165. 0,
  166. &mesh_proxy_data_out_props
  167. },
  168. /* Mesh Proxy Data OUT Characteristic Value */
  169. {
  170. { ATT_BT_UUID_SIZE, mesh_proxy_data_out_UUID },
  171. 0,
  172. 0,
  173. &mesh_proxy_data_out_val[0]
  174. },
  175. /* Mesh Proxy Data OUT CCCD */
  176. {
  177. { ATT_BT_UUID_SIZE, clientCharCfgUUID },
  178. GATT_PERMIT_READ | GATT_PERMIT_WRITE,
  179. 0,
  180. (uint8 *) &mesh_proxy_data_out_cccd
  181. },
  182. };
  183. /*********************************************************************
  184. * LOCAL FUNCTIONS
  185. */
  186. static bStatus_t mesh_prov_write_cb
  187. (
  188. uint16 connHandle,
  189. gattAttribute_t *pAttr,
  190. uint8 *pValue,
  191. uint8 len,
  192. uint16 offset
  193. );
  194. static bStatus_t mesh_proxy_write_cb
  195. (
  196. uint16 connHandle,
  197. gattAttribute_t *pAttr,
  198. uint8 *pValue,
  199. uint8 len,
  200. uint16 offset
  201. );
  202. static void mesh_prov_handle_conn
  203. (
  204. uint16 connHandle,
  205. uint8 changeType
  206. );
  207. static void mesh_proxy_handle_conn
  208. (
  209. uint16 connHandle,
  210. uint8 changeType
  211. );
  212. /*********************************************************************
  213. * PROFILE CALLBACKS
  214. */
  215. /* Mesh Provisioning Service Internal Callbacks */
  216. CONST gattServiceCBs_t mesh_prov_internal_cbs =
  217. {
  218. NULL, // Read callback function pointer
  219. mesh_prov_write_cb, // Write callback function pointer
  220. NULL // Authorization callback function pointer
  221. };
  222. /* Mesh Proxy Service Internal Callbacks */
  223. CONST gattServiceCBs_t mesh_proxy_internal_cbs =
  224. {
  225. NULL, // Read callback function pointer
  226. mesh_proxy_write_cb, // Write callback function pointer
  227. NULL // Authorization callback function pointer
  228. };
  229. /* ----------------------------------------------------------------------------
  230. * Function : bStatus_t mesh_prov_init(mesh_prov_cb *cb)
  231. * ----------------------------------------------------------------------------
  232. * Description : Send request to add Mesh Provisioning Service[0x1827]
  233. * into the attribute database.
  234. * Defines the different access functions
  235. .* (setter/getter commands to access the different
  236. * characteristic attributes).
  237. * Inputs : Pointer to mesh_prov_cb.
  238. * Outputs : bStatus_t : SUCCESS or failure reasons
  239. * Assumptions : None
  240. * ------------------------------------------------------------------------- */
  241. bStatus_t mesh_prov_init(mesh_prov_cb *cb)
  242. {
  243. uint8 status = FAILURE;
  244. /* Initialize Client Characteristic Configuration attributes */
  245. GATTServApp_InitCharCfg
  246. (
  247. INVALID_CONNHANDLE,
  248. mesh_prov_data_out_cccd
  249. );
  250. /* Register with Link DB to receive link status change callback */
  251. VOID linkDB_Register(mesh_prov_handle_conn);
  252. /* Register GATT attribute list and CBs with GATT Server App */
  253. status = GATTServApp_RegisterService
  254. (
  255. mesh_prov_attr_tbl,
  256. GATT_NUM_ATTRS(mesh_prov_attr_tbl),
  257. &mesh_prov_internal_cbs
  258. );
  259. /* Save the Callback Provided */
  260. prov_cb = cb;
  261. return (status);
  262. }
  263. /* ----------------------------------------------------------------------------
  264. * Function : bStatus_t mesh_proxy_init(mesh_proxy_cb *cb)
  265. * ----------------------------------------------------------------------------
  266. * Description : Send request to add Mesh Proxy Service[0x1828]
  267. * into the attribute database.
  268. * Defines the different access functions
  269. .* (setter/getter commands to access the different
  270. * characteristic attributes).
  271. * Inputs : Pointer to mesh_proxy_cb.
  272. * Outputs : bStatus_t : SUCCESS or failure reasons
  273. * Assumptions : None
  274. * ------------------------------------------------------------------------- */
  275. bStatus_t mesh_proxy_init(mesh_proxy_cb *cb)
  276. {
  277. uint8 status = FAILURE;
  278. /* Initialize Client Characteristic Configuration attributes */
  279. GATTServApp_InitCharCfg
  280. (
  281. INVALID_CONNHANDLE,
  282. mesh_proxy_data_out_cccd
  283. );
  284. /* Register with Link DB to receive link status change callback */
  285. VOID linkDB_Register(mesh_proxy_handle_conn);
  286. /* Register GATT attribute list and CBs with GATT Server App */
  287. status = GATTServApp_RegisterService
  288. (
  289. mesh_proxy_attr_tbl,
  290. GATT_NUM_ATTRS(mesh_proxy_attr_tbl),
  291. &mesh_proxy_internal_cbs
  292. );
  293. /* Save the Callback Provided */
  294. proxy_cb = cb;
  295. return (status);
  296. }
  297. /* ----------------------------------------------------------------------------
  298. * Function : bStatus_t mesh_prov_deinit(void)
  299. * ----------------------------------------------------------------------------
  300. * Description : Send request to delete Mesh Provisioning Service[0x1827]
  301. * from the attribute database.
  302. * Inputs : None
  303. * Outputs : bStatus_t : SUCCESS or failure reasons
  304. * Assumptions : None
  305. * ------------------------------------------------------------------------- */
  306. bStatus_t mesh_prov_deinit(void)
  307. {
  308. uint8 status = SUCCESS;
  309. /* Reset the Callback Provided */
  310. prov_cb = NULL;
  311. /**
  312. * Deregister Mesh Prov Service attribute list and
  313. * CBs from GATT Server Application
  314. */
  315. status = GATTServApp_DeregisterService
  316. (
  317. GATT_SERVICE_HANDLE(mesh_prov_attr_tbl),
  318. NULL
  319. );
  320. return ( status );
  321. }
  322. /* ----------------------------------------------------------------------------
  323. * Function : bStatus_t mesh_proxy_deinit(void)
  324. * ----------------------------------------------------------------------------
  325. * Description : Send request to delete Mesh Proxy Service[0x1828]
  326. * from the attribute database.
  327. * Inputs : None
  328. * Outputs : bStatus_t : SUCCESS or failure reasons
  329. * Assumptions : None
  330. * ------------------------------------------------------------------------- */
  331. bStatus_t mesh_proxy_deinit(void)
  332. {
  333. uint8 status = SUCCESS;
  334. /* Reset the Callback Provided */
  335. proxy_cb = NULL;
  336. /**
  337. * Deregister Mesh Prov Service attribute list and
  338. * CBs from GATT Server Application
  339. */
  340. status = GATTServApp_DeregisterService
  341. (
  342. GATT_SERVICE_HANDLE(mesh_proxy_attr_tbl),
  343. NULL
  344. );
  345. return ( status );
  346. }
  347. /* ----------------------------------------------------------------------------
  348. * Function : bStatus_t mesh_prov_notify_data_out(uint16 conn_hndl,
  349. * uint8 attidx, uint8 *val, uint8 val_len)
  350. * ----------------------------------------------------------------------------
  351. * Description : Send Mesh Provisiong Data Out notification to Peer
  352. * Inputs : - conn_hndl - connection index
  353. * - attidx - index to attributes in the service
  354. * - val - pointer to value
  355. * - val_len - length of value
  356. * Outputs : bStatus_t : SUCCESS or failure reasons
  357. * Assumptions : None
  358. * ------------------------------------------------------------------------- */
  359. bStatus_t mesh_prov_notify_data_out
  360. (
  361. uint16 conn_hndl,
  362. uint8 attidx,
  363. uint8 * val,
  364. uint8 val_len
  365. )
  366. {
  367. attHandleValueNoti_t mesh_prov_notif;
  368. uint16 value = GATTServApp_ReadCharCfg
  369. (
  370. conn_hndl,
  371. mesh_prov_data_out_cccd
  372. );
  373. /* NOTE: Currently the attidx is ignored */
  374. (void)attidx;
  375. /* Assign the Mesh Prov Data Out Value Handle */
  376. mesh_prov_notif.handle =\
  377. mesh_prov_attr_tbl[MESH_PROV_DATA_OUT_VALUE_VAL].handle;
  378. /* Copy the Value and Length */
  379. mesh_prov_notif.len = val_len;
  380. osal_memcpy(mesh_prov_notif.value, val, val_len);
  381. /* If notifications enabled */
  382. if ( value & GATT_CLIENT_CFG_NOTIFY )
  383. {
  384. /* Send the notification */
  385. return GATT_Notification( conn_hndl, &mesh_prov_notif, FALSE );
  386. }
  387. return bleIncorrectMode;
  388. }
  389. /* ----------------------------------------------------------------------------
  390. * Function : bStatus_t mesh_proxy_notify_data_out(uint16 conn_hndl,
  391. * uint8 attidx, uint8 *val, uint8 val_len)
  392. * ----------------------------------------------------------------------------
  393. * Description : Send Mesh Proxy Data Out notification to Peer
  394. * Inputs : - conn_hndl - connection index
  395. * - attidx - index to attributes in the service
  396. * - val - pointer to value
  397. * - val_len - length of value
  398. * Outputs : bStatus_t : SUCCESS or failure reasons
  399. * Assumptions : None
  400. * ------------------------------------------------------------------------- */
  401. bStatus_t mesh_proxy_notify_data_out
  402. (
  403. uint16 conn_hndl,
  404. uint8 attidx,
  405. uint8 * val,
  406. uint8 val_len
  407. )
  408. {
  409. attHandleValueNoti_t mesh_proxy_notif;
  410. uint16 value = GATTServApp_ReadCharCfg
  411. (
  412. conn_hndl,
  413. mesh_proxy_data_out_cccd
  414. );
  415. /* NOTE: Currently the attidx is ignored */
  416. (void)attidx;
  417. /* Assign the Mesh Prov Data Out Value Handle */
  418. mesh_proxy_notif.handle =\
  419. mesh_proxy_attr_tbl[MESH_PROV_DATA_OUT_VALUE_VAL].handle;
  420. /* Copy the Value and Length */
  421. mesh_proxy_notif.len = val_len;
  422. osal_memcpy(mesh_proxy_notif.value, val, val_len);
  423. /* If notifications enabled */
  424. if ( value & GATT_CLIENT_CFG_NOTIFY )
  425. {
  426. /* Send the notification */
  427. return GATT_Notification( conn_hndl, &mesh_proxy_notif, FALSE );
  428. }
  429. return bleIncorrectMode;
  430. }
  431. /* ----------------------------------------------------------------------------
  432. * Function : bStatus_t mesh_prov_write_cb (uint16 connHandle,
  433. gattAttribute_t * pAttr, uint8 * pValue,
  434. uint8 len, uint16 offset)
  435. * ----------------------------------------------------------------------------
  436. * Description : Validate attribute data prior to a write operation
  437. * Inputs : - connHandle - connection Handle
  438. * - pAttr - pointer to attribute
  439. * - pValue - pointer to data to be written
  440. * - len - length of data
  441. * - offset - offset of the first octet to be written
  442. * Outputs : Success or Failure
  443. * Assumptions : None
  444. * ------------------------------------------------------------------------- */
  445. static bStatus_t mesh_prov_write_cb
  446. (
  447. uint16 connHandle,
  448. gattAttribute_t * pAttr,
  449. uint8 * pValue,
  450. uint8 len,
  451. uint16 offset
  452. )
  453. {
  454. bStatus_t status = SUCCESS;
  455. /* If attribute permissions require authorization to write, return error */
  456. if ( gattPermitAuthorWrite( pAttr->permissions ) )
  457. {
  458. /* Insufficient authorization */
  459. return ( ATT_ERR_INSUFFICIENT_AUTHOR );
  460. }
  461. if ( pAttr->type.len == ATT_BT_UUID_SIZE )
  462. {
  463. /* 16-bit UUID */
  464. uint16 uuid = BUILD_UINT16( pAttr->type.uuid[0], pAttr->type.uuid[1]);
  465. switch (uuid)
  466. {
  467. case MESH_PROV_DATA_IN_UUID:
  468. /* Validate the value */
  469. /* Make sure it's not a blob oper */
  470. /* phy_printf("\r\n MESH_PROV_DATA_IN WWR of length %d\r\n", len); */
  471. if ( offset != 0 )
  472. {
  473. status = ATT_ERR_ATTR_NOT_LONG;
  474. }
  475. /* Write the value */
  476. if (SUCCESS == status)
  477. {
  478. /* TODO: Check if this is needed */
  479. /* len = (len <= MESH_PROV_DATA_IN_MAX_LENGTH) ?\
  480. len : MESH_PROV_DATA_IN_MAX_LENGTH; */
  481. /* Notify Data IN Write Callback */
  482. /* Invoke Data IN Callback */
  483. if (NULL != prov_cb)
  484. {
  485. prov_cb->prov_data_in_cb
  486. (
  487. connHandle,
  488. offset,
  489. len,
  490. pValue
  491. );
  492. }
  493. }
  494. break;
  495. case GATT_CLIENT_CHAR_CFG_UUID:
  496. /* phy_printf("\r\n MESH_PROV_DATA_OUT CCCD Write for %d Attr Handle of length %d\r\n", pAttr->handle, len); */
  497. status = GATTServApp_ProcessCCCWriteReq
  498. (
  499. connHandle,
  500. pAttr,
  501. pValue,
  502. len,
  503. offset,
  504. GATT_CLIENT_CFG_NOTIFY
  505. );
  506. if ( status == SUCCESS )
  507. {
  508. uint16 t_cccd_val = BUILD_UINT16( pValue[0], pValue[1] );
  509. t_cccd_val = (GATT_CLIENT_CFG_NOTIFY == t_cccd_val) ?\
  510. TRUE : FALSE;
  511. /* Invoke CCCD Update Callback */
  512. if (NULL != prov_cb)
  513. {
  514. prov_cb->prov_data_out_ccd_cb
  515. (
  516. connHandle,
  517. t_cccd_val
  518. );
  519. }
  520. }
  521. break;
  522. default:
  523. status = ATT_ERR_ATTR_NOT_FOUND;
  524. break;
  525. }
  526. }
  527. else
  528. {
  529. // 128-bit UUID
  530. status = ATT_ERR_INVALID_HANDLE;
  531. }
  532. return ( status );
  533. }
  534. /* ----------------------------------------------------------------------------
  535. * Function : bStatus_t mesh_proxy_write_cb (uint16 connHandle,
  536. gattAttribute_t * pAttr, uint8 * pValue,
  537. uint8 len, uint16 offset)
  538. * ----------------------------------------------------------------------------
  539. * Description : Validate attribute data prior to a write operation
  540. * Inputs : - connHandle - connection Handle
  541. * - pAttr - pointer to attribute
  542. * - pValue - pointer to data to be written
  543. * - len - length of data
  544. * - offset - offset of the first octet to be written
  545. * Outputs : Success or Failure
  546. * Assumptions : None
  547. * ------------------------------------------------------------------------- */
  548. static bStatus_t mesh_proxy_write_cb
  549. (
  550. uint16 connHandle,
  551. gattAttribute_t * pAttr,
  552. uint8 * pValue,
  553. uint8 len,
  554. uint16 offset
  555. )
  556. {
  557. bStatus_t status = SUCCESS;
  558. /* If attribute permissions require authorization to write, return error */
  559. if ( gattPermitAuthorWrite( pAttr->permissions ) )
  560. {
  561. /* Insufficient authorization */
  562. return ( ATT_ERR_INSUFFICIENT_AUTHOR );
  563. }
  564. if ( pAttr->type.len == ATT_BT_UUID_SIZE )
  565. {
  566. /* 16-bit UUID */
  567. uint16 uuid = BUILD_UINT16( pAttr->type.uuid[0], pAttr->type.uuid[1]);
  568. switch (uuid)
  569. {
  570. case MESH_PROXY_DATA_IN_UUID:
  571. /* Validate the value */
  572. /* Make sure it's not a blob oper */
  573. /* phy_printf("\r\n MESH_PROXY_DATA_IN WWR of length %d\r\n", len); */
  574. if ( offset != 0 )
  575. {
  576. status = ATT_ERR_ATTR_NOT_LONG;
  577. }
  578. /* Write the value */
  579. if (SUCCESS == status)
  580. {
  581. /* TODO: Check if this is needed */
  582. /* len = (len <= MESH_PROXY_DATA_IN_MAX_LENGTH) ?\
  583. len : MESH_PROXY_DATA_IN_MAX_LENGTH; */
  584. /* Notify Data IN Write Callback */
  585. /* Invoke Data IN Callback */
  586. if (NULL != proxy_cb)
  587. {
  588. proxy_cb->proxy_data_in_cb
  589. (
  590. connHandle,
  591. offset,
  592. len,
  593. pValue
  594. );
  595. }
  596. }
  597. break;
  598. case GATT_CLIENT_CHAR_CFG_UUID:
  599. /* phy_printf("\r\n MESH_PROXY_DATA_OUT CCCD Write for %d Attr Handle of length %d\r\n", pAttr->handle, len); */
  600. status = GATTServApp_ProcessCCCWriteReq
  601. (
  602. connHandle,
  603. pAttr,
  604. pValue,
  605. len,
  606. offset,
  607. GATT_CLIENT_CFG_NOTIFY
  608. );
  609. if ( status == SUCCESS )
  610. {
  611. uint16 t_cccd_val = BUILD_UINT16( pValue[0], pValue[1] );
  612. t_cccd_val = (GATT_CLIENT_CFG_NOTIFY == t_cccd_val) ?\
  613. TRUE : FALSE;
  614. /* Invoke CCCD Update Callback */
  615. if (NULL != proxy_cb)
  616. {
  617. proxy_cb->proxy_data_out_ccd_cb
  618. (
  619. connHandle,
  620. t_cccd_val
  621. );
  622. }
  623. }
  624. break;
  625. default:
  626. status = ATT_ERR_ATTR_NOT_FOUND;
  627. break;
  628. }
  629. }
  630. else
  631. {
  632. // 128-bit UUID
  633. status = ATT_ERR_INVALID_HANDLE;
  634. }
  635. return ( status );
  636. }
  637. /* ----------------------------------------------------------------------------
  638. * Function : void mesh_prov_handle_conn(uint16 connHandle,
  639. * uint8 changeType)
  640. * ----------------------------------------------------------------------------
  641. * Description : Callback to Handle Mesh Prov Connection event
  642. * Inputs : - connHandle - connection Handle
  643. * - changeType - type of change
  644. * Outputs : None
  645. * Assumptions : None
  646. * ------------------------------------------------------------------------- */
  647. static void mesh_prov_handle_conn( uint16 connHandle, uint8 changeType )
  648. {
  649. // Make sure this is not loopback connection
  650. if ( connHandle != LOOPBACK_CONNHANDLE )
  651. {
  652. // Reset Client Char Config if connection has dropped
  653. if ( ( changeType == LINKDB_STATUS_UPDATE_REMOVED ) ||
  654. ( ( changeType == LINKDB_STATUS_UPDATE_STATEFLAGS ) &&
  655. ( !linkDB_Up( connHandle ) ) ) )
  656. {
  657. GATTServApp_InitCharCfg( connHandle, mesh_prov_data_out_cccd );
  658. }
  659. }
  660. }
  661. /* ----------------------------------------------------------------------------
  662. * Function : void mesh_proxy_handle_conn(uint16 connHandle,
  663. * uint8 changeType)
  664. * ----------------------------------------------------------------------------
  665. * Description : Callback to Handle Mesh Proxy Connection event
  666. * Inputs : - connHandle - connection Handle
  667. * - changeType - type of change
  668. * Outputs : None
  669. * Assumptions : None
  670. * ------------------------------------------------------------------------- */
  671. static void mesh_proxy_handle_conn( uint16 connHandle, uint8 changeType )
  672. {
  673. // Make sure this is not loopback connection
  674. if ( connHandle != LOOPBACK_CONNHANDLE )
  675. {
  676. // Reset Client Char Config if connection has dropped
  677. if ( ( changeType == LINKDB_STATUS_UPDATE_REMOVED ) ||
  678. ( ( changeType == LINKDB_STATUS_UPDATE_STATEFLAGS ) &&
  679. ( !linkDB_Up( connHandle ) ) ) )
  680. {
  681. GATTServApp_InitCharCfg( connHandle, mesh_proxy_data_out_cccd );
  682. }
  683. }
  684. }