gatt_attr.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  1. /******************************************************************************
  2. *
  3. * Copyright (C) 2008-2012 Broadcom Corporation
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at:
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. ******************************************************************************/
  18. /******************************************************************************
  19. *
  20. * this file contains the main GATT server attributes access request
  21. * handling functions.
  22. *
  23. ******************************************************************************/
  24. #include "common/bt_target.h"
  25. //#include "bt_utils.h"
  26. #include "stack/gatt_api.h"
  27. #include "gatt_int.h"
  28. #include "stack/sdpdefs.h"
  29. #include "bta/bta_gatts_co.h"
  30. #if (BLE_INCLUDED == TRUE && GATTS_INCLUDED == TRUE)
  31. #define BLE_GATT_SR_SUPP_FEAT_EATT_BITMASK 0x01
  32. #define BLE_GATT_CL_SUPP_FEAT_ROBUST_CACHING_BITMASK 0x01
  33. #define BLE_GATT_CL_SUPP_FEAT_EATT_BITMASK 0x02
  34. #define BLE_GATT_CL_SUPP_FEAT_MULTI_NOTIF_BITMASK 0x04
  35. #define BLE_GATT_CL_SUPP_FEAT_BITMASK 0x07
  36. #define GATTP_MAX_NUM_INC_SVR 0
  37. #define GATTP_MAX_CHAR_NUM 5
  38. #define GATTP_MAX_ATTR_NUM (GATTP_MAX_CHAR_NUM * 2 + GATTP_MAX_NUM_INC_SVR + 1)
  39. #define GATTP_MAX_CHAR_VALUE_SIZE 50
  40. #ifndef GATTP_ATTR_DB_SIZE
  41. #define GATTP_ATTR_DB_SIZE GATT_DB_MEM_SIZE(GATTP_MAX_NUM_INC_SVR, GATTP_MAX_CHAR_NUM, GATTP_MAX_CHAR_VALUE_SIZE)
  42. #endif
  43. static void gatt_request_cback(UINT16 conn_id, UINT32 trans_id, UINT8 op_code, tGATTS_DATA *p_data);
  44. static void gatt_connect_cback(tGATT_IF gatt_if, BD_ADDR bda, UINT16 conn_id, BOOLEAN connected,
  45. tGATT_DISCONN_REASON reason, tBT_TRANSPORT transport);
  46. static void gatt_disc_res_cback(UINT16 conn_id, tGATT_DISC_TYPE disc_type, tGATT_DISC_RES *p_data);
  47. static void gatt_disc_cmpl_cback(UINT16 conn_id, tGATT_DISC_TYPE disc_type, tGATT_STATUS status);
  48. static void gatt_cl_op_cmpl_cback(UINT16 conn_id, tGATTC_OPTYPE op, tGATT_STATUS status,
  49. tGATT_CL_COMPLETE *p_data);
  50. static void gatt_cl_start_config_ccc(tGATT_PROFILE_CLCB *p_clcb);
  51. static const tGATT_CBACK gatt_profile_cback = {
  52. gatt_connect_cback,
  53. gatt_cl_op_cmpl_cback,
  54. gatt_disc_res_cback,
  55. gatt_disc_cmpl_cback,
  56. gatt_request_cback,
  57. NULL,
  58. NULL
  59. } ;
  60. /*******************************************************************************
  61. **
  62. ** Function gatt_profile_find_conn_id_by_bd_addr
  63. **
  64. ** Description Find the connection ID by remote address
  65. **
  66. ** Returns Connection ID
  67. **
  68. *******************************************************************************/
  69. #if (GATTS_INCLUDED == TRUE)
  70. UINT16 gatt_profile_find_conn_id_by_bd_addr(BD_ADDR remote_bda)
  71. {
  72. UINT16 conn_id = GATT_INVALID_CONN_ID;
  73. GATT_GetConnIdIfConnected (gatt_cb.gatt_if, remote_bda, &conn_id, BT_TRANSPORT_LE);
  74. return conn_id;
  75. }
  76. #endif ///GATTS_INCLUDED == TRUE
  77. /*******************************************************************************
  78. **
  79. ** Function gatt_profile_find_clcb_by_conn_id
  80. **
  81. ** Description find clcb by Connection ID
  82. **
  83. ** Returns Pointer to the found link conenction control block.
  84. **
  85. *******************************************************************************/
  86. static tGATT_PROFILE_CLCB *gatt_profile_find_clcb_by_conn_id(UINT16 conn_id)
  87. {
  88. UINT8 i_clcb;
  89. tGATT_PROFILE_CLCB *p_clcb = NULL;
  90. for (i_clcb = 0, p_clcb = gatt_cb.profile_clcb; i_clcb < GATT_MAX_APPS; i_clcb++, p_clcb++) {
  91. if (p_clcb->in_use && p_clcb->conn_id == conn_id) {
  92. return p_clcb;
  93. }
  94. }
  95. return NULL;
  96. }
  97. /*******************************************************************************
  98. **
  99. ** Function gatt_profile_find_clcb_by_bd_addr
  100. **
  101. ** Description The function searches all LCBs with macthing bd address.
  102. **
  103. ** Returns Pointer to the found link conenction control block.
  104. **
  105. *******************************************************************************/
  106. static tGATT_PROFILE_CLCB *gatt_profile_find_clcb_by_bd_addr(BD_ADDR bda, tBT_TRANSPORT transport)
  107. {
  108. UINT8 i_clcb;
  109. tGATT_PROFILE_CLCB *p_clcb = NULL;
  110. for (i_clcb = 0, p_clcb = gatt_cb.profile_clcb; i_clcb < GATT_MAX_APPS; i_clcb++, p_clcb++) {
  111. if (p_clcb->in_use && p_clcb->transport == transport &&
  112. p_clcb->connected && !memcmp(p_clcb->bda, bda, BD_ADDR_LEN)) {
  113. return p_clcb;
  114. }
  115. }
  116. return NULL;
  117. }
  118. /*******************************************************************************
  119. **
  120. ** Function gatt_profile_clcb_alloc
  121. **
  122. ** Description The function allocates a GATT profile connection link control block
  123. **
  124. ** Returns NULL if not found. Otherwise pointer to the connection link block.
  125. **
  126. *******************************************************************************/
  127. tGATT_PROFILE_CLCB *gatt_profile_clcb_alloc (UINT16 conn_id, BD_ADDR bda, tBT_TRANSPORT tranport)
  128. {
  129. UINT8 i_clcb = 0;
  130. tGATT_PROFILE_CLCB *p_clcb = NULL;
  131. for (i_clcb = 0, p_clcb = gatt_cb.profile_clcb; i_clcb < GATT_MAX_APPS; i_clcb++, p_clcb++) {
  132. if (!p_clcb->in_use) {
  133. p_clcb->in_use = TRUE;
  134. p_clcb->conn_id = conn_id;
  135. p_clcb->connected = TRUE;
  136. p_clcb->transport = tranport;
  137. memcpy (p_clcb->bda, bda, BD_ADDR_LEN);
  138. break;
  139. }
  140. }
  141. if (i_clcb < GATT_MAX_APPS) {
  142. return p_clcb;
  143. }
  144. return NULL;
  145. }
  146. /*******************************************************************************
  147. **
  148. ** Function gatt_profile_clcb_dealloc
  149. **
  150. ** Description The function deallocates a GATT profile connection link control block
  151. **
  152. ** Returns void
  153. **
  154. *******************************************************************************/
  155. void gatt_profile_clcb_dealloc (tGATT_PROFILE_CLCB *p_clcb)
  156. {
  157. memset(p_clcb, 0, sizeof(tGATT_PROFILE_CLCB));
  158. }
  159. /*******************************************************************************
  160. **
  161. ** Function gatt_proc_read
  162. **
  163. ** Description GATT Attributes Database Read/Read Blob Request process
  164. **
  165. ** Returns GATT_SUCCESS if successfully sent; otherwise error code.
  166. **
  167. *******************************************************************************/
  168. tGATT_STATUS gatt_proc_read (UINT16 conn_id, tGATTS_REQ_TYPE type, tGATT_READ_REQ *p_data, tGATTS_RSP *p_rsp)
  169. {
  170. tGATT_STATUS status = GATT_NO_RESOURCES;
  171. UINT16 len = 0;
  172. UINT8 *value;
  173. UNUSED(type);
  174. GATT_TRACE_DEBUG("%s handle %x", __func__, p_data->handle);
  175. UINT8 tcb_idx = GATT_GET_TCB_IDX(conn_id);
  176. tGATT_TCB *tcb = gatt_get_tcb_by_idx(tcb_idx);
  177. if (p_data->is_long) {
  178. p_rsp->attr_value.offset = p_data->offset;
  179. }
  180. p_rsp->attr_value.handle = p_data->handle;
  181. /* handle request for reading client supported features */
  182. if (p_data->handle == gatt_cb.handle_of_cl_supported_feat) {
  183. if (tcb == NULL) {
  184. return GATT_INSUF_RESOURCE;
  185. }
  186. p_rsp->attr_value.len = 1;
  187. memcpy(p_rsp->attr_value.value, &tcb->cl_supp_feat, 1);
  188. return GATT_SUCCESS;
  189. }
  190. /* handle request for reading database hash */
  191. if (p_data->handle == gatt_cb.handle_of_database_hash) {
  192. p_rsp->attr_value.len = BT_OCTET16_LEN;
  193. memcpy(p_rsp->attr_value.value, gatt_cb.database_hash, BT_OCTET16_LEN);
  194. gatt_sr_update_cl_status(tcb, true);
  195. return GATT_SUCCESS;
  196. }
  197. /* handle request for reading server supported features */
  198. if (p_data->handle == gatt_cb.handle_of_sr_supported_feat) {
  199. p_rsp->attr_value.len = 1;
  200. memcpy(p_rsp->attr_value.value, &gatt_cb.gatt_sr_supported_feat_mask, 1);
  201. return GATT_SUCCESS;
  202. }
  203. /* handle request for reading service changed des and the others */
  204. status = GATTS_GetAttributeValue(p_data->handle, &len, &value);
  205. if(status == GATT_SUCCESS && len > 0 && value) {
  206. if(len > GATT_MAX_ATTR_LEN) {
  207. len = GATT_MAX_ATTR_LEN;
  208. }
  209. p_rsp->attr_value.len = len;
  210. memcpy(p_rsp->attr_value.value, value, len);
  211. }
  212. return status;
  213. }
  214. static tGATT_STATUS gatt_sr_write_cl_supp_feat(UINT16 conn_id, tGATT_WRITE_REQ *p_data)
  215. {
  216. UINT8 val_new;
  217. UINT8 val_old;
  218. UINT8 val_xor;
  219. UINT8 val_and;
  220. UINT8 *p = p_data->value;
  221. UINT8 tcb_idx = GATT_GET_TCB_IDX(conn_id);
  222. tGATT_TCB *p_tcb = gatt_get_tcb_by_idx(tcb_idx);
  223. GATT_TRACE_DEBUG("%s len %u, feat %x", __func__, p_data->len, *p);
  224. if (p_tcb == NULL) {
  225. GATT_TRACE_ERROR("%s no conn", __func__);
  226. return GATT_NOT_FOUND;
  227. }
  228. if (p_data->len != 1) {
  229. GATT_TRACE_ERROR("%s len %u", __func__, p_data->len);
  230. return GATT_INVALID_PDU;
  231. }
  232. STREAM_TO_UINT8(val_new, p);
  233. val_new = (val_new & BLE_GATT_CL_SUPP_FEAT_BITMASK);
  234. if (val_new == 0) {
  235. GATT_TRACE_ERROR("%s bit cannot be all zero", __func__);
  236. return GATT_VALUE_NOT_ALLOWED;
  237. }
  238. val_old = p_tcb->cl_supp_feat;
  239. val_xor = val_old ^ val_new;
  240. val_and = val_xor & val_new;
  241. if (val_and != val_xor) {
  242. GATT_TRACE_ERROR("%s bit cannot be reset", __func__);
  243. return GATT_VALUE_NOT_ALLOWED;
  244. }
  245. p_tcb->cl_supp_feat = val_new;
  246. #if (SMP_INCLUDED == TRUE)
  247. bta_gatts_co_cl_feat_save(p_tcb->peer_bda, &p_tcb->cl_supp_feat);
  248. #endif
  249. return GATT_SUCCESS;
  250. }
  251. /******************************************************************************
  252. **
  253. ** Function gatt_proc_write_req
  254. **
  255. ** Description GATT server process a write request.
  256. **
  257. ** Returns GATT_SUCCESS if successfully sent; otherwise error code.
  258. **
  259. *******************************************************************************/
  260. tGATT_STATUS gatt_proc_write_req(UINT16 conn_id, tGATTS_REQ_TYPE type, tGATT_WRITE_REQ *p_data)
  261. {
  262. if(p_data->len > GATT_MAX_ATTR_LEN) {
  263. p_data->len = GATT_MAX_ATTR_LEN;
  264. }
  265. if (p_data->handle == gatt_cb.handle_of_h_r) {
  266. return GATT_WRITE_NOT_PERMIT;
  267. }
  268. if (p_data->handle == gatt_cb.handle_of_cl_supported_feat) {
  269. return gatt_sr_write_cl_supp_feat(conn_id, p_data);
  270. }
  271. if (p_data->handle == gatt_cb.handle_of_database_hash) {
  272. return GATT_WRITE_NOT_PERMIT;
  273. }
  274. if (p_data->handle == gatt_cb.handle_of_sr_supported_feat) {
  275. return GATT_WRITE_NOT_PERMIT;
  276. }
  277. return GATTS_SetAttributeValue(p_data->handle,
  278. p_data->len,
  279. p_data->value);
  280. }
  281. /*******************************************************************************
  282. **
  283. ** Function gatt_request_cback
  284. **
  285. ** Description GATT profile attribute access request callback.
  286. **
  287. ** Returns void.
  288. **
  289. *******************************************************************************/
  290. static void gatt_request_cback (UINT16 conn_id, UINT32 trans_id, tGATTS_REQ_TYPE type,
  291. tGATTS_DATA *p_data)
  292. {
  293. UINT8 status = GATT_INVALID_PDU;
  294. tGATTS_RSP rsp_msg ;
  295. BOOLEAN ignore = FALSE;
  296. GATT_TRACE_DEBUG("%s",__func__);
  297. memset(&rsp_msg, 0, sizeof(tGATTS_RSP));
  298. switch (type) {
  299. case GATTS_REQ_TYPE_READ:
  300. status = gatt_proc_read(conn_id, type, &p_data->read_req, &rsp_msg);
  301. break;
  302. case GATTS_REQ_TYPE_WRITE:
  303. if (!p_data->write_req.need_rsp) {
  304. ignore = TRUE;
  305. }
  306. status = gatt_proc_write_req(conn_id, type, &p_data->write_req);
  307. break;
  308. case GATTS_REQ_TYPE_WRITE_EXEC:
  309. case GATT_CMD_WRITE:
  310. ignore = TRUE;
  311. GATT_TRACE_EVENT("Ignore GATT_REQ_EXEC_WRITE/WRITE_CMD" );
  312. break;
  313. case GATTS_REQ_TYPE_MTU:
  314. GATT_TRACE_EVENT("Get MTU exchange new mtu size: %d", p_data->mtu);
  315. ignore = TRUE;
  316. break;
  317. default:
  318. GATT_TRACE_EVENT("Unknown/unexpected LE GAP ATT request: 0x%02x", type);
  319. break;
  320. }
  321. if (!ignore) {
  322. GATTS_SendRsp (conn_id, trans_id, status, &rsp_msg);
  323. }
  324. }
  325. /*******************************************************************************
  326. **
  327. ** Function gatt_connect_cback
  328. **
  329. ** Description Gatt profile connection callback.
  330. **
  331. ** Returns void
  332. **
  333. *******************************************************************************/
  334. static void gatt_connect_cback (tGATT_IF gatt_if, BD_ADDR bda, UINT16 conn_id,
  335. BOOLEAN connected, tGATT_DISCONN_REASON reason,
  336. tBT_TRANSPORT transport)
  337. {
  338. UNUSED(gatt_if);
  339. GATT_TRACE_DEBUG ("%s: from %08x%04x connected:%d conn_id=%d reason = 0x%04x", __FUNCTION__,
  340. (bda[0] << 24) + (bda[1] << 16) + (bda[2] << 8) + bda[3],
  341. (bda[4] << 8) + bda[5], connected, conn_id, reason);
  342. tGATT_PROFILE_CLCB *p_clcb = gatt_profile_find_clcb_by_bd_addr(bda, transport);
  343. if (p_clcb == NULL) {
  344. p_clcb = gatt_profile_clcb_alloc (conn_id, bda, transport);
  345. }
  346. if (p_clcb == NULL) {
  347. return;
  348. }
  349. if (GATT_GetConnIdIfConnected (gatt_cb.gatt_if, bda, &p_clcb->conn_id, transport)) {
  350. p_clcb->connected = TRUE;
  351. p_clcb->conn_id = conn_id;
  352. }
  353. if (!p_clcb->connected) {
  354. /* wait for connection */
  355. return;
  356. }
  357. if (connected) {
  358. p_clcb->conn_id = conn_id;
  359. p_clcb->connected = TRUE;
  360. } else {
  361. gatt_profile_clcb_dealloc(p_clcb);
  362. }
  363. }
  364. /*******************************************************************************
  365. **
  366. ** Function gatt_profile_db_init
  367. **
  368. ** Description Initializa the GATT profile attribute database.
  369. **
  370. *******************************************************************************/
  371. void gatt_profile_db_init (void)
  372. {
  373. tBT_UUID app_uuid = {LEN_UUID_128, {0}};
  374. tBT_UUID uuid = {LEN_UUID_16, {UUID_SERVCLASS_GATT_SERVER}};
  375. UINT16 service_handle = 0;
  376. tGATT_STATUS status;
  377. /* Fill our internal UUID with a fixed pattern 0x81 */
  378. memset (&app_uuid.uu.uuid128, 0x81, LEN_UUID_128);
  379. /* Create a GATT profile service */
  380. gatt_cb.gatt_if = GATT_Register(&app_uuid, &gatt_profile_cback);
  381. GATT_StartIf(gatt_cb.gatt_if);
  382. service_handle = GATTS_CreateService (gatt_cb.gatt_if , &uuid, 0, GATTP_MAX_ATTR_NUM, TRUE);
  383. GATT_TRACE_DEBUG ("GATTS_CreateService: handle of service handle%x", service_handle);
  384. /* add Service Changed characteristic
  385. */
  386. uuid.uu.uuid16 = gatt_cb.gattp_attr.uuid = GATT_UUID_GATT_SRV_CHGD;
  387. gatt_cb.gattp_attr.service_change = 0;
  388. gatt_cb.gattp_attr.handle =
  389. gatt_cb.handle_of_h_r = GATTS_AddCharacteristic(service_handle, &uuid, 0, GATT_CHAR_PROP_BIT_INDICATE,
  390. NULL, NULL);
  391. GATT_TRACE_DEBUG ("gatt_profile_db_init: handle of service changed%d\n",
  392. gatt_cb.handle_of_h_r);
  393. tBT_UUID descr_uuid = {LEN_UUID_16, {GATT_UUID_CHAR_CLIENT_CONFIG}};
  394. uint8_t ccc_value[2] ={ 0x00, 0x00};
  395. tGATT_ATTR_VAL attr_val = {
  396. .attr_max_len = sizeof(UINT16),
  397. .attr_len = sizeof(UINT16),
  398. .attr_val = ccc_value,
  399. };
  400. GATTS_AddCharDescriptor (service_handle, GATT_PERM_READ | GATT_PERM_WRITE , &descr_uuid, &attr_val, NULL);
  401. /* add Client Supported Features characteristic */
  402. uuid.uu.uuid16 = GATT_UUID_CLIENT_SUP_FEAT;
  403. gatt_cb.handle_of_cl_supported_feat = GATTS_AddCharacteristic(service_handle, &uuid, GATT_PERM_READ | GATT_PERM_WRITE,
  404. GATT_CHAR_PROP_BIT_READ | GATT_CHAR_PROP_BIT_WRITE, NULL, NULL);
  405. /* add Database Hash characteristic */
  406. uuid.uu.uuid16 = GATT_UUID_GATT_DATABASE_HASH;
  407. gatt_cb.handle_of_database_hash = GATTS_AddCharacteristic(service_handle, &uuid, GATT_PERM_READ, GATT_CHAR_PROP_BIT_READ, NULL, NULL);
  408. /* add Server Supported Features characteristic */
  409. uuid.uu.uuid16 = GATT_UUID_SERVER_SUP_FEAT;
  410. gatt_cb.handle_of_sr_supported_feat = GATTS_AddCharacteristic(service_handle, &uuid, GATT_PERM_READ, GATT_CHAR_PROP_BIT_READ, NULL, NULL);
  411. /* start service */
  412. status = GATTS_StartService (gatt_cb.gatt_if, service_handle, GATTP_TRANSPORT_SUPPORTED );
  413. #if (CONFIG_BT_STACK_NO_LOG)
  414. (void) status;
  415. #endif
  416. GATT_TRACE_DEBUG ("gatt_profile_db_init: gatt_if=%d start status%d\n",
  417. gatt_cb.gatt_if, status);
  418. }
  419. /*******************************************************************************
  420. **
  421. ** Function gatt_disc_res_cback
  422. **
  423. ** Description Gatt profile discovery result callback
  424. **
  425. ** Returns void
  426. **
  427. *******************************************************************************/
  428. static void gatt_disc_res_cback (UINT16 conn_id, tGATT_DISC_TYPE disc_type, tGATT_DISC_RES *p_data)
  429. {
  430. GATT_TRACE_DEBUG("%s, disc_type = %d",__func__, disc_type);
  431. tGATT_PROFILE_CLCB *p_clcb = gatt_profile_find_clcb_by_conn_id(conn_id);
  432. if (p_clcb == NULL) {
  433. return;
  434. }
  435. switch (disc_type) {
  436. case GATT_DISC_SRVC_BY_UUID:/* stage 1 */
  437. p_clcb->e_handle = p_data->value.group_value.e_handle;
  438. p_clcb->ccc_result ++;
  439. break;
  440. case GATT_DISC_CHAR:/* stage 2 */
  441. p_clcb->s_handle = p_data->value.dclr_value.val_handle;
  442. p_clcb->ccc_result ++;
  443. break;
  444. case GATT_DISC_CHAR_DSCPT: /* stage 3 */
  445. if (p_data->type.uu.uuid16 == GATT_UUID_CHAR_CLIENT_CONFIG) {
  446. p_clcb->s_handle = p_data->handle;
  447. p_clcb->ccc_result ++;
  448. }
  449. break;
  450. }
  451. }
  452. /*******************************************************************************
  453. **
  454. ** Function gatt_disc_cmpl_cback
  455. **
  456. ** Description Gatt profile discovery complete callback
  457. **
  458. ** Returns void
  459. **
  460. *******************************************************************************/
  461. static void gatt_disc_cmpl_cback (UINT16 conn_id, tGATT_DISC_TYPE disc_type, tGATT_STATUS status)
  462. {
  463. GATT_TRACE_DEBUG("%s",__func__);
  464. tGATT_PROFILE_CLCB *p_clcb = gatt_profile_find_clcb_by_conn_id(conn_id);
  465. if (p_clcb == NULL) {
  466. return;
  467. }
  468. if (status == GATT_SUCCESS && p_clcb->ccc_result > 0) {
  469. p_clcb->ccc_result = 0;
  470. p_clcb->ccc_stage ++;
  471. gatt_cl_start_config_ccc(p_clcb);
  472. } else {
  473. GATT_TRACE_ERROR("%s() - Register for service changed indication failure", __FUNCTION__);
  474. }
  475. }
  476. /*******************************************************************************
  477. **
  478. ** Function gatt_cl_op_cmpl_cback
  479. **
  480. ** Description Gatt profile client operation complete callback
  481. **
  482. ** Returns void
  483. **
  484. *******************************************************************************/
  485. static void gatt_cl_op_cmpl_cback (UINT16 conn_id, tGATTC_OPTYPE op,
  486. tGATT_STATUS status, tGATT_CL_COMPLETE *p_data)
  487. {
  488. GATT_TRACE_DEBUG("%s",__func__);
  489. tGATT_PROFILE_CLCB *p_clcb = gatt_profile_find_clcb_by_conn_id(conn_id);
  490. if (p_clcb == NULL) {
  491. return;
  492. }
  493. if (op == GATTC_OPTYPE_WRITE) {
  494. GATT_TRACE_DEBUG("%s() - ccc write status : %d", __FUNCTION__, status);
  495. }
  496. }
  497. /*******************************************************************************
  498. **
  499. ** Function gatt_cl_start_config_ccc
  500. **
  501. ** Description Gatt profile start configure service change CCC
  502. **
  503. ** Returns void
  504. **
  505. *******************************************************************************/
  506. static void gatt_cl_start_config_ccc(tGATT_PROFILE_CLCB *p_clcb)
  507. {
  508. tGATT_DISC_PARAM srvc_disc_param;
  509. tGATT_VALUE ccc_value;
  510. GATT_TRACE_DEBUG("%s() - stage: %d", __FUNCTION__, p_clcb->ccc_stage);
  511. memset (&srvc_disc_param, 0 , sizeof(tGATT_DISC_PARAM));
  512. memset (&ccc_value, 0 , sizeof(tGATT_VALUE));
  513. switch (p_clcb->ccc_stage) {
  514. case GATT_SVC_CHANGED_SERVICE: /* discover GATT service */
  515. srvc_disc_param.s_handle = 1;
  516. srvc_disc_param.e_handle = 0xffff;
  517. srvc_disc_param.service.len = 2;
  518. srvc_disc_param.service.uu.uuid16 = UUID_SERVCLASS_GATT_SERVER;
  519. #if (GATTC_INCLUDED == TRUE)
  520. if (GATTC_Discover (p_clcb->conn_id, GATT_DISC_SRVC_BY_UUID, &srvc_disc_param) != GATT_SUCCESS) {
  521. GATT_TRACE_ERROR("%s() - ccc service error", __FUNCTION__);
  522. }
  523. #endif ///GATTC_INCLUDED == TRUE
  524. break;
  525. case GATT_SVC_CHANGED_CHARACTERISTIC: /* discover service change char */
  526. srvc_disc_param.s_handle = 1;
  527. srvc_disc_param.e_handle = p_clcb->e_handle;
  528. srvc_disc_param.service.len = 2;
  529. srvc_disc_param.service.uu.uuid16 = GATT_UUID_GATT_SRV_CHGD;
  530. #if (GATTC_INCLUDED == TRUE)
  531. if (GATTC_Discover (p_clcb->conn_id, GATT_DISC_CHAR, &srvc_disc_param) != GATT_SUCCESS) {
  532. GATT_TRACE_ERROR("%s() - ccc char error", __FUNCTION__);
  533. }
  534. #endif ///GATTC_INCLUDED == TRUE
  535. break;
  536. case GATT_SVC_CHANGED_DESCRIPTOR: /* discover service change ccc */
  537. srvc_disc_param.s_handle = p_clcb->s_handle;
  538. srvc_disc_param.e_handle = p_clcb->e_handle;
  539. #if (GATTC_INCLUDED == TRUE)
  540. if (GATTC_Discover (p_clcb->conn_id, GATT_DISC_CHAR_DSCPT, &srvc_disc_param) != GATT_SUCCESS) {
  541. GATT_TRACE_ERROR("%s() - ccc char descriptor error", __FUNCTION__);
  542. }
  543. #endif ///GATTC_INCLUDED == TRUE
  544. break;
  545. case GATT_SVC_CHANGED_CONFIGURE_CCCD: /* write ccc */
  546. ccc_value.handle = p_clcb->s_handle;
  547. ccc_value.len = 2;
  548. ccc_value.value[0] = GATT_CLT_CONFIG_INDICATION;
  549. #if (GATTC_INCLUDED == TRUE)
  550. if (GATTC_Write (p_clcb->conn_id, GATT_WRITE, &ccc_value) != GATT_SUCCESS) {
  551. GATT_TRACE_ERROR("%s() - write ccc error", __FUNCTION__);
  552. }
  553. #endif ///GATTC_INCLUDED == TRUE
  554. break;
  555. }
  556. }
  557. /*******************************************************************************
  558. **
  559. ** Function GATT_ConfigServiceChangeCCC
  560. **
  561. ** Description Configure service change indication on remote device
  562. **
  563. ** Returns none
  564. **
  565. *******************************************************************************/
  566. void GATT_ConfigServiceChangeCCC (BD_ADDR remote_bda, BOOLEAN enable, tBT_TRANSPORT transport)
  567. {
  568. tGATT_PROFILE_CLCB *p_clcb = gatt_profile_find_clcb_by_bd_addr (remote_bda, transport);
  569. if (p_clcb == NULL) {
  570. p_clcb = gatt_profile_clcb_alloc (0, remote_bda, transport);
  571. }
  572. if (p_clcb == NULL) {
  573. return;
  574. }
  575. if (GATT_GetConnIdIfConnected (gatt_cb.gatt_if, remote_bda, &p_clcb->conn_id, transport)) {
  576. p_clcb->connected = TRUE;
  577. }
  578. /* hold the link here */
  579. GATT_Connect(gatt_cb.gatt_if, remote_bda, BLE_ADDR_UNKNOWN_TYPE, TRUE, transport, FALSE);
  580. p_clcb->ccc_stage = GATT_SVC_CHANGED_CONNECTING;
  581. if (!p_clcb->connected) {
  582. /* wait for connection */
  583. return;
  584. }
  585. p_clcb->ccc_stage ++;
  586. gatt_cl_start_config_ccc(p_clcb);
  587. }
  588. /*******************************************************************************
  589. **
  590. ** Function gatt_sr_is_cl_robust_caching_supported
  591. **
  592. ** Description Check if Robust Caching is supported for the connection
  593. **
  594. ** Returns true if enabled by client side, otherwise false
  595. **
  596. *******************************************************************************/
  597. static BOOLEAN gatt_sr_is_cl_robust_caching_supported(tGATT_TCB *p_tcb)
  598. {
  599. // Server robust caching not enabled
  600. if (!GATTS_ROBUST_CACHING_ENABLED) {
  601. return FALSE;
  602. }
  603. return (p_tcb->cl_supp_feat & BLE_GATT_CL_SUPP_FEAT_ROBUST_CACHING_BITMASK);
  604. }
  605. /*******************************************************************************
  606. **
  607. ** Function gatt_sr_is_cl_change_aware
  608. **
  609. ** Description Check if the connection is change-aware
  610. **
  611. ** Returns true if change aware, otherwise false
  612. **
  613. *******************************************************************************/
  614. BOOLEAN gatt_sr_is_cl_change_aware(tGATT_TCB *p_tcb)
  615. {
  616. // If robust caching is not supported, should always return true by default
  617. if (!gatt_sr_is_cl_robust_caching_supported(p_tcb)) {
  618. return true;
  619. }
  620. return p_tcb->is_robust_cache_change_aware;
  621. }
  622. /*******************************************************************************
  623. **
  624. ** Function gatt_sr_init_cl_status
  625. **
  626. ** Description Restore status for trusted device
  627. **
  628. ** Returns none
  629. **
  630. *******************************************************************************/
  631. void gatt_sr_init_cl_status(tGATT_TCB *p_tcb)
  632. {
  633. #if (SMP_INCLUDED == TRUE)
  634. bta_gatts_co_cl_feat_load(p_tcb->peer_bda, &p_tcb->cl_supp_feat);
  635. #endif
  636. // This is used to reset bit when robust caching is disabled
  637. if (!GATTS_ROBUST_CACHING_ENABLED) {
  638. p_tcb->cl_supp_feat &= ~BLE_GATT_CL_SUPP_FEAT_ROBUST_CACHING_BITMASK;
  639. }
  640. if (gatt_sr_is_cl_robust_caching_supported(p_tcb)) {
  641. BT_OCTET16 stored_hash = {0};
  642. #if (SMP_INCLUDED == TRUE)
  643. bta_gatts_co_db_hash_load(p_tcb->peer_bda, stored_hash);
  644. #endif
  645. p_tcb->is_robust_cache_change_aware = (memcmp(stored_hash, gatt_cb.database_hash, BT_OCTET16_LEN) == 0);
  646. } else {
  647. p_tcb->is_robust_cache_change_aware = true;
  648. }
  649. GATT_TRACE_DEBUG("%s feat %x aware %d", __func__, p_tcb->cl_supp_feat, p_tcb->is_robust_cache_change_aware);
  650. }
  651. /*******************************************************************************
  652. **
  653. ** Function gatt_sr_update_cl_status
  654. **
  655. ** Description Update change-aware status for the remote device
  656. **
  657. ** Returns none
  658. **
  659. *******************************************************************************/
  660. void gatt_sr_update_cl_status(tGATT_TCB *p_tcb, BOOLEAN chg_aware)
  661. {
  662. if (p_tcb == NULL) {
  663. return;
  664. }
  665. // if robust caching is not supported, do nothing
  666. if (!gatt_sr_is_cl_robust_caching_supported(p_tcb)) {
  667. return;
  668. }
  669. // only when client status is changed from unaware to aware, we should store database hash
  670. if (!p_tcb->is_robust_cache_change_aware && chg_aware) {
  671. #if (SMP_INCLUDED == TRUE)
  672. bta_gatts_co_db_hash_save(p_tcb->peer_bda, gatt_cb.database_hash);
  673. #endif
  674. }
  675. p_tcb->is_robust_cache_change_aware = chg_aware;
  676. GATT_TRACE_DEBUG("%s status %d", __func__, chg_aware);
  677. }
  678. #endif /* BLE_INCLUDED == TRUE && GATTS_INCLUDED == TRUE */