gatt_main.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209
  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 ATT functions
  21. *
  22. ******************************************************************************/
  23. #include "common/bt_target.h"
  24. #if BLE_INCLUDED == TRUE
  25. #include "gatt_int.h"
  26. #include "stack/l2c_api.h"
  27. #include "btm_int.h"
  28. #include "btm_ble_int.h"
  29. #include "osi/allocator.h"
  30. /* Configuration flags. */
  31. #define GATT_L2C_CFG_IND_DONE (1<<0)
  32. #define GATT_L2C_CFG_CFM_DONE (1<<1)
  33. /* minimum GATT MTU size over BR/EDR link
  34. */
  35. #define GATT_MIN_BR_MTU_SIZE 48
  36. /********************************************************************************/
  37. /* L O C A L F U N C T I O N P R O T O T Y P E S */
  38. /********************************************************************************/
  39. static void gatt_le_connect_cback (UINT16 chan, BD_ADDR bd_addr, BOOLEAN connected,
  40. UINT16 reason, tBT_TRANSPORT transport);
  41. static void gatt_le_data_ind (UINT16 chan, BD_ADDR bd_addr, BT_HDR *p_buf);
  42. static void gatt_le_cong_cback(BD_ADDR remote_bda, BOOLEAN congest);
  43. #if (CLASSIC_BT_GATT_INCLUDED == TRUE)
  44. static void gatt_l2cif_connect_ind_cback (BD_ADDR bd_addr, UINT16 l2cap_cid,
  45. UINT16 psm, UINT8 l2cap_id);
  46. static void gatt_l2cif_connect_cfm_cback (UINT16 l2cap_cid, UINT16 result);
  47. static void gatt_l2cif_config_ind_cback (UINT16 l2cap_cid, tL2CAP_CFG_INFO *p_cfg);
  48. static void gatt_l2cif_config_cfm_cback (UINT16 l2cap_cid, tL2CAP_CFG_INFO *p_cfg);
  49. static void gatt_l2cif_disconnect_ind_cback (UINT16 l2cap_cid, BOOLEAN ack_needed);
  50. static void gatt_l2cif_disconnect_cfm_cback (UINT16 l2cap_cid, UINT16 result);
  51. static void gatt_l2cif_data_ind_cback (UINT16 l2cap_cid, BT_HDR *p_msg);
  52. #endif ///CLASSIC_BT_GATT_INCLUDED == TRUE
  53. static void gatt_send_conn_cback (tGATT_TCB *p_tcb);
  54. #if (CLASSIC_BT_GATT_INCLUDED == TRUE)
  55. static void gatt_l2cif_congest_cback (UINT16 cid, BOOLEAN congested);
  56. static const tL2CAP_APPL_INFO dyn_info = {
  57. gatt_l2cif_connect_ind_cback,
  58. gatt_l2cif_connect_cfm_cback,
  59. NULL,
  60. gatt_l2cif_config_ind_cback,
  61. gatt_l2cif_config_cfm_cback,
  62. gatt_l2cif_disconnect_ind_cback,
  63. gatt_l2cif_disconnect_cfm_cback,
  64. NULL,
  65. gatt_l2cif_data_ind_cback,
  66. gatt_l2cif_congest_cback,
  67. NULL
  68. } ;
  69. #endif ///CLASSIC_BT_GATT_INCLUDED == TRUE
  70. #if GATT_DYNAMIC_MEMORY == FALSE
  71. tGATT_CB gatt_cb;
  72. #else
  73. tGATT_CB *gatt_cb_ptr;
  74. #endif
  75. tGATT_DEFAULT gatt_default;
  76. /*******************************************************************************
  77. **
  78. ** Function gatt_init
  79. **
  80. ** Description This function is enable the GATT profile on the device.
  81. ** It clears out the control blocks, and registers with L2CAP.
  82. **
  83. ** Returns void
  84. **
  85. *******************************************************************************/
  86. void gatt_init (void)
  87. {
  88. tL2CAP_FIXED_CHNL_REG fixed_reg;
  89. #if GATT_DYNAMIC_MEMORY
  90. gatt_cb_ptr = (tGATT_CB *)osi_malloc(sizeof(tGATT_CB));
  91. #endif /* #if GATT_DYNAMIC_MEMORY */
  92. memset (&gatt_cb, 0, sizeof(tGATT_CB));
  93. memset (&fixed_reg, 0, sizeof(tL2CAP_FIXED_CHNL_REG));
  94. #if defined(GATT_INITIAL_TRACE_LEVEL)
  95. gatt_cb.trace_level = GATT_INITIAL_TRACE_LEVEL;
  96. #else
  97. gatt_cb.trace_level = BT_TRACE_LEVEL_NONE; /* No traces */
  98. #endif
  99. gatt_cb.def_mtu_size = GATT_DEF_BLE_MTU_SIZE;
  100. gatt_cb.sign_op_queue = fixed_queue_new(QUEUE_SIZE_MAX);
  101. gatt_cb.srv_chg_clt_q = fixed_queue_new(QUEUE_SIZE_MAX);
  102. gatt_cb.pending_new_srv_start_q = fixed_queue_new(QUEUE_SIZE_MAX);
  103. /* First, register fixed L2CAP channel for ATT over BLE */
  104. fixed_reg.fixed_chnl_opts.mode = L2CAP_FCR_BASIC_MODE;
  105. fixed_reg.fixed_chnl_opts.max_transmit = 0xFF;
  106. fixed_reg.fixed_chnl_opts.rtrans_tout = 2000;
  107. fixed_reg.fixed_chnl_opts.mon_tout = 12000;
  108. fixed_reg.fixed_chnl_opts.mps = 670;
  109. fixed_reg.fixed_chnl_opts.tx_win_sz = 1;
  110. fixed_reg.pL2CA_FixedConn_Cb = gatt_le_connect_cback;
  111. fixed_reg.pL2CA_FixedData_Cb = gatt_le_data_ind;
  112. fixed_reg.pL2CA_FixedCong_Cb = gatt_le_cong_cback; /* congestion callback */
  113. fixed_reg.default_idle_tout = 0xffff; /* 0xffff default idle timeout */
  114. L2CA_RegisterFixedChannel (L2CAP_ATT_CID, &fixed_reg);
  115. #if (CLASSIC_BT_GATT_INCLUDED == TRUE)
  116. /* Now, register with L2CAP for ATT PSM over BR/EDR */
  117. if (!L2CA_Register (BT_PSM_ATT, (tL2CAP_APPL_INFO *) &dyn_info)) {
  118. GATT_TRACE_ERROR ("ATT Dynamic Registration failed");
  119. }
  120. #endif ///CLASSIC_BT_GATT_INCLUDED == TRUE
  121. BTM_SetSecurityLevel(TRUE, "", BTM_SEC_SERVICE_ATT, BTM_SEC_NONE, BT_PSM_ATT, 0, 0);
  122. BTM_SetSecurityLevel(FALSE, "", BTM_SEC_SERVICE_ATT, BTM_SEC_NONE, BT_PSM_ATT, 0, 0);
  123. gatt_cb.hdl_cfg.gatt_start_hdl = GATT_GATT_START_HANDLE;
  124. gatt_cb.hdl_cfg.gap_start_hdl = GATT_GAP_START_HANDLE;
  125. gatt_cb.hdl_cfg.app_start_hdl = GATT_APP_START_HANDLE;
  126. #if (GATTS_INCLUDED == TRUE)
  127. gatt_profile_db_init();
  128. #endif ///GATTS_INCLUDED == TRUE
  129. //init local MTU size
  130. gatt_default.local_mtu = GATT_MAX_MTU_SIZE;
  131. }
  132. /*******************************************************************************
  133. **
  134. ** Function gatt_free
  135. **
  136. ** Description This function frees resources used by the GATT profile.
  137. **
  138. ** Returns void
  139. **
  140. *******************************************************************************/
  141. #if (GATT_INCLUDED == TRUE)
  142. void gatt_free(void)
  143. {
  144. int i;
  145. GATT_TRACE_DEBUG("gatt_free()");
  146. fixed_queue_free(gatt_cb.sign_op_queue, NULL);
  147. gatt_cb.sign_op_queue = NULL;
  148. fixed_queue_free(gatt_cb.srv_chg_clt_q, NULL);
  149. gatt_cb.srv_chg_clt_q = NULL;
  150. fixed_queue_free(gatt_cb.pending_new_srv_start_q, NULL);
  151. gatt_cb.pending_new_srv_start_q = NULL;
  152. for (i = 0; i < GATT_MAX_PHY_CHANNEL; i++)
  153. {
  154. fixed_queue_free(gatt_cb.tcb[i].pending_enc_clcb, NULL);
  155. gatt_cb.tcb[i].pending_enc_clcb = NULL;
  156. fixed_queue_free(gatt_cb.tcb[i].pending_ind_q, NULL);
  157. gatt_cb.tcb[i].pending_ind_q = NULL;
  158. btu_free_timer(&gatt_cb.tcb[i].conf_timer_ent);
  159. memset(&gatt_cb.tcb[i].conf_timer_ent, 0, sizeof(TIMER_LIST_ENT));
  160. btu_free_timer(&gatt_cb.tcb[i].ind_ack_timer_ent);
  161. memset(&gatt_cb.tcb[i].ind_ack_timer_ent, 0, sizeof(TIMER_LIST_ENT));
  162. #if (GATTS_INCLUDED == TRUE)
  163. fixed_queue_free(gatt_cb.tcb[i].sr_cmd.multi_rsp_q, NULL);
  164. gatt_cb.tcb[i].sr_cmd.multi_rsp_q = NULL;
  165. #endif /* #if (GATTS_INCLUDED == TRUE) */
  166. }
  167. #if (GATTS_INCLUDED == TRUE)
  168. for (i = 0; i < GATT_MAX_SR_PROFILES; i++) {
  169. gatt_remove_an_item_from_list(&gatt_cb.hdl_list_info, &gatt_cb.hdl_list[i]);
  170. gatt_free_attr_value_buffer(&gatt_cb.hdl_list[i]);
  171. gatt_free_hdl_buffer(&gatt_cb.hdl_list[i]);
  172. }
  173. #endif /* #if (GATTS_INCLUDED == TRUE) */
  174. #if GATT_DYNAMIC_MEMORY
  175. FREE_AND_RESET(gatt_cb_ptr);
  176. #endif /* #if GATT_DYNAMIC_MEMORY */
  177. }
  178. #endif ///GATTS_INCLUDED == TRUE
  179. /*******************************************************************************
  180. **
  181. ** Function gatt_connect
  182. **
  183. ** Description This function is called to initiate a connection to a peer device.
  184. **
  185. ** Parameter rem_bda: remote device address to connect to.
  186. ** bd_addr_type: emote device address type.
  187. ** Returns TRUE if connection is started, otherwise return FALSE.
  188. **
  189. *******************************************************************************/
  190. BOOLEAN gatt_connect (BD_ADDR rem_bda, tBLE_ADDR_TYPE bd_addr_type, tGATT_TCB *p_tcb, tBT_TRANSPORT transport)
  191. {
  192. BOOLEAN gatt_ret = FALSE;
  193. if (gatt_get_ch_state(p_tcb) != GATT_CH_OPEN) {
  194. gatt_set_ch_state(p_tcb, GATT_CH_CONN);
  195. }
  196. if (transport == BT_TRANSPORT_LE) {
  197. p_tcb->att_lcid = L2CAP_ATT_CID;
  198. gatt_ret = L2CA_ConnectFixedChnl (L2CAP_ATT_CID, rem_bda, bd_addr_type);
  199. #if (CLASSIC_BT_GATT_INCLUDED == TRUE)
  200. } else {
  201. if ((p_tcb->att_lcid = L2CA_ConnectReq(BT_PSM_ATT, rem_bda)) != 0) {
  202. gatt_ret = TRUE;
  203. }
  204. #endif ///CLASSIC_BT_GATT_INCLUDED == TRUE
  205. }
  206. return gatt_ret;
  207. }
  208. /*******************************************************************************
  209. **
  210. ** Function gatt_disconnect
  211. **
  212. ** Description This function is called to disconnect to an ATT device.
  213. **
  214. ** Parameter p_tcb: pointer to the TCB to disconnect.
  215. **
  216. ** Returns TRUE: if connection found and to be disconnected; otherwise
  217. ** return FALSE.
  218. **
  219. *******************************************************************************/
  220. BOOLEAN gatt_disconnect (tGATT_TCB *p_tcb)
  221. {
  222. BOOLEAN ret = FALSE;
  223. tGATT_CH_STATE ch_state;
  224. GATT_TRACE_DEBUG ("gatt_disconnect ");
  225. if (p_tcb != NULL) {
  226. ret = TRUE;
  227. if ( (ch_state = gatt_get_ch_state(p_tcb)) != GATT_CH_CLOSING ) {
  228. if (p_tcb->att_lcid == L2CAP_ATT_CID) {
  229. if (ch_state == GATT_CH_OPEN) {
  230. /* only LCB exist between remote device and local */
  231. ret = L2CA_RemoveFixedChnl (L2CAP_ATT_CID, p_tcb->peer_bda);
  232. } else {
  233. gatt_set_ch_state(p_tcb, GATT_CH_CLOSING);
  234. ret = L2CA_CancelBleConnectReq (p_tcb->peer_bda);
  235. }
  236. #if (CLASSIC_BT_GATT_INCLUDED == TRUE)
  237. } else {
  238. ret = L2CA_DisconnectReq(p_tcb->att_lcid);
  239. #endif ///CLASSIC_BT_GATT_INCLUDED == TRUE
  240. }
  241. } else {
  242. GATT_TRACE_DEBUG ("gatt_disconnect already in closing state");
  243. }
  244. }
  245. return ret;
  246. }
  247. /*******************************************************************************
  248. **
  249. ** Function gatt_update_app_hold_link_status
  250. **
  251. ** Description Update the application use link status
  252. **
  253. ** Returns void.
  254. **
  255. *******************************************************************************/
  256. void gatt_update_app_hold_link_status (tGATT_IF gatt_if, tGATT_TCB *p_tcb, BOOLEAN is_add)
  257. {
  258. UINT8 i;
  259. BOOLEAN found = FALSE;
  260. if (p_tcb == NULL) {
  261. GATT_TRACE_ERROR("gatt_update_app_hold_link_status p_tcb=NULL");
  262. return;
  263. }
  264. for (i = 0; i < GATT_MAX_APPS; i++) {
  265. if (p_tcb->app_hold_link[i] == gatt_if) {
  266. found = TRUE;
  267. if (!is_add) {
  268. p_tcb->app_hold_link[i] = 0;
  269. break;
  270. }
  271. }
  272. }
  273. if (!found && is_add) {
  274. for (i = 0; i < GATT_MAX_APPS; i++) {
  275. if (p_tcb->app_hold_link[i] == 0) {
  276. p_tcb->app_hold_link[i] = gatt_if;
  277. found = TRUE;
  278. break;
  279. }
  280. }
  281. }
  282. GATT_TRACE_DEBUG("gatt_update_app_hold_link_status found=%d[1-found] idx=%d gatt_if=%d is_add=%d", found, i, gatt_if, is_add);
  283. }
  284. /*******************************************************************************
  285. **
  286. ** Function gatt_update_app_use_link_flag
  287. **
  288. ** Description Update the application use link flag and optional to check the acl link
  289. ** if the link is up then set the idle time out accordingly
  290. **
  291. ** Returns void.
  292. **
  293. *******************************************************************************/
  294. void gatt_update_app_use_link_flag (tGATT_IF gatt_if, tGATT_TCB *p_tcb, BOOLEAN is_add, BOOLEAN check_acl_link)
  295. {
  296. GATT_TRACE_DEBUG("gatt_update_app_use_link_flag is_add=%d chk_link=%d",
  297. is_add, check_acl_link);
  298. gatt_update_app_hold_link_status(gatt_if, p_tcb, is_add);
  299. if (check_acl_link &&
  300. p_tcb &&
  301. p_tcb->att_lcid == L2CAP_ATT_CID && /* only update link idle timer for fixed channel */
  302. (BTM_GetHCIConnHandle(p_tcb->peer_bda, p_tcb->transport) != GATT_INVALID_ACL_HANDLE)) {
  303. if (is_add) {
  304. GATT_TRACE_DEBUG("GATT disables link idle timer");
  305. /* acl link is connected disable the idle timeout */
  306. GATT_SetIdleTimeout(p_tcb->peer_bda, GATT_LINK_NO_IDLE_TIMEOUT, p_tcb->transport);
  307. } else {
  308. if (!gatt_num_apps_hold_link(p_tcb)) {
  309. /* acl link is connected but no application needs to use the link
  310. so set the timeout value to GATT_LINK_IDLE_TIMEOUT_WHEN_NO_APP seconds */
  311. GATT_TRACE_DEBUG("GATT starts link idle timer =%d sec", GATT_LINK_IDLE_TIMEOUT_WHEN_NO_APP);
  312. GATT_SetIdleTimeout(p_tcb->peer_bda, GATT_LINK_IDLE_TIMEOUT_WHEN_NO_APP, p_tcb->transport);
  313. }
  314. }
  315. }
  316. }
  317. /*******************************************************************************
  318. **
  319. ** Function gatt_act_connect
  320. **
  321. ** Description GATT connection initiation.
  322. **
  323. ** Returns void.
  324. **
  325. *******************************************************************************/
  326. BOOLEAN gatt_act_connect (tGATT_REG *p_reg, BD_ADDR bd_addr, tBLE_ADDR_TYPE bd_addr_type, tBT_TRANSPORT transport)
  327. {
  328. BOOLEAN ret = FALSE;
  329. tGATT_TCB *p_tcb;
  330. UINT8 st;
  331. if ((p_tcb = gatt_find_tcb_by_addr(bd_addr, transport)) != NULL) {
  332. ret = TRUE;
  333. st = gatt_get_ch_state(p_tcb);
  334. /* before link down, another app try to open a GATT connection */
  335. if (st == GATT_CH_OPEN && gatt_num_apps_hold_link(p_tcb) == 0 &&
  336. transport == BT_TRANSPORT_LE ) {
  337. if (!gatt_connect(bd_addr, bd_addr_type, p_tcb, transport)) {
  338. ret = FALSE;
  339. }
  340. } else if (st == GATT_CH_CLOSING) {
  341. /* need to complete the closing first */
  342. ret = FALSE;
  343. }
  344. } else {
  345. if ((p_tcb = gatt_allocate_tcb_by_bdaddr(bd_addr, transport)) != NULL) {
  346. if (!gatt_connect(bd_addr, bd_addr_type, p_tcb, transport)) {
  347. GATT_TRACE_ERROR("gatt_connect failed");
  348. fixed_queue_free(p_tcb->pending_enc_clcb, NULL);
  349. fixed_queue_free(p_tcb->pending_ind_q, NULL);
  350. memset(p_tcb, 0, sizeof(tGATT_TCB));
  351. } else {
  352. ret = TRUE;
  353. }
  354. } else {
  355. ret = 0;
  356. GATT_TRACE_ERROR("Max TCB for gatt_if [%d] reached.", p_reg->gatt_if);
  357. }
  358. }
  359. if (ret) {
  360. gatt_update_app_use_link_flag(p_reg->gatt_if, p_tcb, TRUE, FALSE);
  361. }
  362. return ret;
  363. }
  364. /*******************************************************************************
  365. **
  366. ** Function gatt_le_connect_cback
  367. **
  368. ** Description This callback function is called by L2CAP to indicate that
  369. ** the ATT fixed channel for LE is
  370. ** connected (conn = TRUE)/disconnected (conn = FALSE).
  371. **
  372. *******************************************************************************/
  373. static void gatt_le_connect_cback (UINT16 chan, BD_ADDR bd_addr, BOOLEAN connected,
  374. UINT16 reason, tBT_TRANSPORT transport)
  375. {
  376. tGATT_TCB *p_tcb = gatt_find_tcb_by_addr(bd_addr, transport);
  377. BOOLEAN check_srv_chg = FALSE;
  378. tGATTS_SRV_CHG *p_srv_chg_clt = NULL;
  379. /* ignore all fixed channel connect/disconnect on BR/EDR link for GATT */
  380. if (transport == BT_TRANSPORT_BR_EDR) {
  381. return;
  382. }
  383. GATT_TRACE_DEBUG ("GATT ATT protocol channel with BDA: %08x%04x is %s",
  384. (bd_addr[0] << 24) + (bd_addr[1] << 16) + (bd_addr[2] << 8) + bd_addr[3],
  385. (bd_addr[4] << 8) + bd_addr[5], (connected) ? "connected" : "disconnected");
  386. if ((p_srv_chg_clt = gatt_is_bda_in_the_srv_chg_clt_list(bd_addr)) != NULL) {
  387. check_srv_chg = TRUE;
  388. } else {
  389. if (btm_sec_is_a_bonded_dev(bd_addr)) {
  390. gatt_add_a_bonded_dev_for_srv_chg(bd_addr);
  391. }
  392. }
  393. if (connected) {
  394. /* do we have a channel initiating a connection? */
  395. if (p_tcb) {
  396. /* we are initiating connection */
  397. if ( gatt_get_ch_state(p_tcb) == GATT_CH_CONN) {
  398. /* send callback */
  399. gatt_set_ch_state(p_tcb, GATT_CH_OPEN);
  400. p_tcb->payload_size = GATT_DEF_BLE_MTU_SIZE;
  401. gatt_send_conn_cback(p_tcb);
  402. }
  403. if (check_srv_chg) {
  404. #if (GATTS_INCLUDED == TRUE)
  405. gatt_chk_srv_chg (p_srv_chg_clt);
  406. #endif ///GATTS_INCLUDED == TRUE
  407. }
  408. }
  409. /* this is incoming connection or background connection callback */
  410. else {
  411. if ((p_tcb = gatt_allocate_tcb_by_bdaddr(bd_addr, BT_TRANSPORT_LE)) != NULL) {
  412. p_tcb->att_lcid = L2CAP_ATT_CID;
  413. gatt_set_ch_state(p_tcb, GATT_CH_OPEN);
  414. p_tcb->payload_size = GATT_DEF_BLE_MTU_SIZE;
  415. gatt_send_conn_cback (p_tcb);
  416. if (check_srv_chg) {
  417. #if (GATTS_INCLUDED == TRUE)
  418. gatt_chk_srv_chg (p_srv_chg_clt);
  419. #endif ///GATTS_INCLUDED == TRUE
  420. }
  421. } else {
  422. GATT_TRACE_ERROR("CCB max out, no rsources");
  423. }
  424. }
  425. } else {
  426. gatt_cleanup_upon_disc(bd_addr, reason, transport);
  427. GATT_TRACE_DEBUG ("ATT disconnected");
  428. }
  429. }
  430. /*******************************************************************************
  431. **
  432. ** Function gatt_channel_congestion
  433. **
  434. ** Description This function is called to process the congestion callback
  435. ** from lcb
  436. **
  437. ** Returns void
  438. **
  439. *******************************************************************************/
  440. static void gatt_channel_congestion(tGATT_TCB *p_tcb, BOOLEAN congested)
  441. {
  442. UINT8 i = 0;
  443. tGATT_REG *p_reg = NULL;
  444. UINT16 conn_id;
  445. #if (GATTC_INCLUDED == TRUE)
  446. /* if uncongested, check to see if there is any more pending data */
  447. if (p_tcb != NULL && congested == FALSE) {
  448. gatt_cl_send_next_cmd_inq(p_tcb);
  449. }
  450. #endif ///GATTC_INCLUDED == TRUE
  451. /* notifying all applications for the connection up event */
  452. for (i = 0, p_reg = gatt_cb.cl_rcb ; i < GATT_MAX_APPS; i++, p_reg++) {
  453. if (p_reg->in_use) {
  454. if (p_reg->app_cb.p_congestion_cb) {
  455. conn_id = GATT_CREATE_CONN_ID(p_tcb->tcb_idx, p_reg->gatt_if);
  456. (*p_reg->app_cb.p_congestion_cb)(conn_id, congested);
  457. }
  458. }
  459. }
  460. }
  461. /*******************************************************************************
  462. **
  463. ** Function gatt_le_cong_cback
  464. **
  465. ** Description This function is called when GATT fixed channel is congested
  466. ** or uncongested.
  467. **
  468. ** Returns void
  469. **
  470. *******************************************************************************/
  471. static void gatt_le_cong_cback(BD_ADDR remote_bda, BOOLEAN congested)
  472. {
  473. tGATT_TCB *p_tcb = gatt_find_tcb_by_addr(remote_bda, BT_TRANSPORT_LE);
  474. /* if uncongested, check to see if there is any more pending data */
  475. if (p_tcb != NULL) {
  476. gatt_channel_congestion(p_tcb, congested);
  477. }
  478. }
  479. /*******************************************************************************
  480. **
  481. ** Function gatt_le_data_ind
  482. **
  483. ** Description This function is called when data is received from L2CAP.
  484. ** if we are the originator of the connection, we are the ATT
  485. ** client, and the received message is queued up for the client.
  486. **
  487. ** If we are the destination of the connection, we are the ATT
  488. ** server, so the message is passed to the server processing
  489. ** function.
  490. **
  491. ** Returns void
  492. **
  493. *******************************************************************************/
  494. static void gatt_le_data_ind (UINT16 chan, BD_ADDR bd_addr, BT_HDR *p_buf)
  495. {
  496. tGATT_TCB *p_tcb;
  497. /* Find CCB based on bd addr */
  498. if ((p_tcb = gatt_find_tcb_by_addr (bd_addr, BT_TRANSPORT_LE)) != NULL &&
  499. gatt_get_ch_state(p_tcb) >= GATT_CH_OPEN) {
  500. gatt_data_process(p_tcb, p_buf);
  501. } else {
  502. osi_free (p_buf);
  503. if (p_tcb != NULL) {
  504. GATT_TRACE_WARNING ("ATT - Ignored L2CAP data while in state: %d\n",
  505. gatt_get_ch_state(p_tcb));
  506. }
  507. }
  508. }
  509. /*******************************************************************************
  510. **
  511. ** Function gatt_l2cif_connect_ind
  512. **
  513. ** Description This function handles an inbound connection indication
  514. ** from L2CAP. This is the case where we are acting as a
  515. ** server.
  516. **
  517. ** Returns void
  518. **
  519. *******************************************************************************/
  520. #if (CLASSIC_BT_GATT_INCLUDED == TRUE)
  521. static void gatt_l2cif_connect_ind_cback (BD_ADDR bd_addr, UINT16 lcid, UINT16 psm, UINT8 id)
  522. {
  523. /* do we already have a control channel for this peer? */
  524. UINT8 result = L2CAP_CONN_OK;
  525. tL2CAP_CFG_INFO cfg;
  526. tGATT_TCB *p_tcb = gatt_find_tcb_by_addr(bd_addr, BT_TRANSPORT_BR_EDR);
  527. UNUSED(psm);
  528. GATT_TRACE_ERROR("Connection indication cid = %d", lcid);
  529. /* new connection ? */
  530. if (p_tcb == NULL) {
  531. /* allocate tcb */
  532. if ((p_tcb = gatt_allocate_tcb_by_bdaddr(bd_addr, BT_TRANSPORT_BR_EDR)) == NULL) {
  533. /* no tcb available, reject L2CAP connection */
  534. result = L2CAP_CONN_NO_RESOURCES;
  535. } else {
  536. p_tcb->att_lcid = lcid;
  537. }
  538. } else { /* existing connection , reject it */
  539. result = L2CAP_CONN_NO_RESOURCES;
  540. }
  541. /* Send L2CAP connect rsp */
  542. L2CA_ConnectRsp(bd_addr, id, lcid, result, 0);
  543. /* if result ok, proceed with connection */
  544. if (result == L2CAP_CONN_OK) {
  545. /* transition to configuration state */
  546. gatt_set_ch_state(p_tcb, GATT_CH_CFG);
  547. /* Send L2CAP config req */
  548. memset(&cfg, 0, sizeof(tL2CAP_CFG_INFO));
  549. cfg.mtu_present = TRUE;
  550. cfg.mtu = gatt_default.local_mtu;
  551. L2CA_ConfigReq(lcid, &cfg);
  552. }
  553. }
  554. /*******************************************************************************
  555. **
  556. ** Function gatt_l2c_connect_cfm_cback
  557. **
  558. ** Description This is the L2CAP connect confirm callback function.
  559. **
  560. **
  561. ** Returns void
  562. **
  563. *******************************************************************************/
  564. static void gatt_l2cif_connect_cfm_cback(UINT16 lcid, UINT16 result)
  565. {
  566. tGATT_TCB *p_tcb;
  567. tL2CAP_CFG_INFO cfg;
  568. /* look up clcb for this channel */
  569. if ((p_tcb = gatt_find_tcb_by_cid(lcid)) != NULL) {
  570. GATT_TRACE_DEBUG("gatt_l2c_connect_cfm_cback result: %d ch_state: %d, lcid:0x%x", result, gatt_get_ch_state(p_tcb), p_tcb->att_lcid);
  571. /* if in correct state */
  572. if (gatt_get_ch_state(p_tcb) == GATT_CH_CONN) {
  573. /* if result successful */
  574. if (result == L2CAP_CONN_OK) {
  575. /* set channel state */
  576. gatt_set_ch_state(p_tcb, GATT_CH_CFG);
  577. /* Send L2CAP config req */
  578. memset(&cfg, 0, sizeof(tL2CAP_CFG_INFO));
  579. cfg.mtu_present = TRUE;
  580. cfg.mtu = gatt_default.local_mtu;
  581. L2CA_ConfigReq(lcid, &cfg);
  582. }
  583. /* else initiating connection failure */
  584. else {
  585. gatt_cleanup_upon_disc(p_tcb->peer_bda, result, GATT_TRANSPORT_BR_EDR);
  586. }
  587. } else { /* wrong state, disconnect it */
  588. if (result == L2CAP_CONN_OK) {
  589. /* just in case the peer also accepts our connection - Send L2CAP disconnect req */
  590. L2CA_DisconnectReq(lcid);
  591. }
  592. }
  593. }
  594. }
  595. /*******************************************************************************
  596. **
  597. ** Function gatt_l2cif_config_cfm_cback
  598. **
  599. ** Description This is the L2CAP config confirm callback function.
  600. **
  601. **
  602. ** Returns void
  603. **
  604. *******************************************************************************/
  605. void gatt_l2cif_config_cfm_cback(UINT16 lcid, tL2CAP_CFG_INFO *p_cfg)
  606. {
  607. tGATT_TCB *p_tcb;
  608. tGATTS_SRV_CHG *p_srv_chg_clt = NULL;
  609. /* look up clcb for this channel */
  610. if ((p_tcb = gatt_find_tcb_by_cid(lcid)) != NULL) {
  611. /* if in correct state */
  612. if ( gatt_get_ch_state(p_tcb) == GATT_CH_CFG) {
  613. /* if result successful */
  614. if (p_cfg->result == L2CAP_CFG_OK) {
  615. /* update flags */
  616. p_tcb->ch_flags |= GATT_L2C_CFG_CFM_DONE;
  617. /* if configuration complete */
  618. if (p_tcb->ch_flags & GATT_L2C_CFG_IND_DONE) {
  619. gatt_set_ch_state(p_tcb, GATT_CH_OPEN);
  620. if ((p_srv_chg_clt = gatt_is_bda_in_the_srv_chg_clt_list(p_tcb->peer_bda)) != NULL) {
  621. #if (GATTS_INCLUDED == TRUE)
  622. gatt_chk_srv_chg(p_srv_chg_clt);
  623. #endif ///GATTS_INCLUDED == TRUE
  624. } else {
  625. if (btm_sec_is_a_bonded_dev(p_tcb->peer_bda)) {
  626. gatt_add_a_bonded_dev_for_srv_chg(p_tcb->peer_bda);
  627. }
  628. }
  629. /* send callback */
  630. gatt_send_conn_cback(p_tcb);
  631. }
  632. }
  633. /* else failure */
  634. else {
  635. /* Send L2CAP disconnect req */
  636. L2CA_DisconnectReq(lcid);
  637. }
  638. }
  639. }
  640. }
  641. /*******************************************************************************
  642. **
  643. ** Function gatt_l2cif_config_ind_cback
  644. **
  645. ** Description This is the L2CAP config indication callback function.
  646. **
  647. **
  648. ** Returns void
  649. **
  650. *******************************************************************************/
  651. void gatt_l2cif_config_ind_cback(UINT16 lcid, tL2CAP_CFG_INFO *p_cfg)
  652. {
  653. tGATT_TCB *p_tcb;
  654. tGATTS_SRV_CHG *p_srv_chg_clt = NULL;
  655. /* look up clcb for this channel */
  656. if ((p_tcb = gatt_find_tcb_by_cid(lcid)) != NULL) {
  657. /* GATT uses the smaller of our MTU and peer's MTU */
  658. if ( p_cfg->mtu_present &&
  659. (p_cfg->mtu >= GATT_MIN_BR_MTU_SIZE && p_cfg->mtu < L2CAP_DEFAULT_MTU)) {
  660. p_tcb->payload_size = p_cfg->mtu;
  661. } else {
  662. p_tcb->payload_size = L2CAP_DEFAULT_MTU;
  663. }
  664. /* send L2CAP configure response */
  665. memset(p_cfg, 0, sizeof(tL2CAP_CFG_INFO));
  666. p_cfg->result = L2CAP_CFG_OK;
  667. L2CA_ConfigRsp(lcid, p_cfg);
  668. /* if first config ind */
  669. if ((p_tcb->ch_flags & GATT_L2C_CFG_IND_DONE) == 0) {
  670. /* update flags */
  671. p_tcb->ch_flags |= GATT_L2C_CFG_IND_DONE;
  672. /* if configuration complete */
  673. if (p_tcb->ch_flags & GATT_L2C_CFG_CFM_DONE) {
  674. gatt_set_ch_state(p_tcb, GATT_CH_OPEN);
  675. if ((p_srv_chg_clt = gatt_is_bda_in_the_srv_chg_clt_list(p_tcb->peer_bda)) != NULL) {
  676. #if (GATTS_INCLUDED == TRUE)
  677. gatt_chk_srv_chg(p_srv_chg_clt);
  678. #endif ///GATTS_INCLUDED == TRUE
  679. } else {
  680. if (btm_sec_is_a_bonded_dev(p_tcb->peer_bda)) {
  681. gatt_add_a_bonded_dev_for_srv_chg(p_tcb->peer_bda);
  682. }
  683. }
  684. /* send callback */
  685. gatt_send_conn_cback(p_tcb);
  686. }
  687. }
  688. }
  689. }
  690. /*******************************************************************************
  691. **
  692. ** Function gatt_l2cif_disconnect_ind_cback
  693. **
  694. ** Description This is the L2CAP disconnect indication callback function.
  695. **
  696. **
  697. ** Returns void
  698. **
  699. *******************************************************************************/
  700. void gatt_l2cif_disconnect_ind_cback(UINT16 lcid, BOOLEAN ack_needed)
  701. {
  702. tGATT_TCB *p_tcb;
  703. UINT16 reason;
  704. /* look up clcb for this channel */
  705. if ((p_tcb = gatt_find_tcb_by_cid(lcid)) != NULL) {
  706. if (ack_needed) {
  707. /* send L2CAP disconnect response */
  708. L2CA_DisconnectRsp(lcid);
  709. }
  710. if (gatt_is_bda_in_the_srv_chg_clt_list(p_tcb->peer_bda) == NULL) {
  711. if (btm_sec_is_a_bonded_dev(p_tcb->peer_bda)) {
  712. gatt_add_a_bonded_dev_for_srv_chg(p_tcb->peer_bda);
  713. }
  714. }
  715. /* if ACL link is still up, no reason is logged, l2cap is disconnect from peer */
  716. if ((reason = L2CA_GetDisconnectReason(p_tcb->peer_bda, p_tcb->transport)) == 0) {
  717. reason = GATT_CONN_TERMINATE_PEER_USER;
  718. }
  719. /* send disconnect callback */
  720. gatt_cleanup_upon_disc(p_tcb->peer_bda, reason, GATT_TRANSPORT_BR_EDR);
  721. }
  722. }
  723. /*******************************************************************************
  724. **
  725. ** Function gatt_l2cif_disconnect_cfm_cback
  726. **
  727. ** Description This is the L2CAP disconnect confirm callback function.
  728. **
  729. **
  730. ** Returns void
  731. **
  732. *******************************************************************************/
  733. static void gatt_l2cif_disconnect_cfm_cback(UINT16 lcid, UINT16 result)
  734. {
  735. tGATT_TCB *p_tcb;
  736. UINT16 reason;
  737. UNUSED(result);
  738. /* look up clcb for this channel */
  739. if ((p_tcb = gatt_find_tcb_by_cid(lcid)) != NULL) {
  740. /* If the device is not in the service changed client list, add it... */
  741. if (gatt_is_bda_in_the_srv_chg_clt_list(p_tcb->peer_bda) == NULL) {
  742. if (btm_sec_is_a_bonded_dev(p_tcb->peer_bda)) {
  743. gatt_add_a_bonded_dev_for_srv_chg(p_tcb->peer_bda);
  744. }
  745. }
  746. /* send disconnect callback */
  747. /* if ACL link is still up, no reason is logged, l2cap is disconnect from peer */
  748. if ((reason = L2CA_GetDisconnectReason(p_tcb->peer_bda, p_tcb->transport)) == 0) {
  749. reason = GATT_CONN_TERMINATE_LOCAL_HOST;
  750. }
  751. gatt_cleanup_upon_disc(p_tcb->peer_bda, reason, GATT_TRANSPORT_BR_EDR);
  752. }
  753. }
  754. /*******************************************************************************
  755. **
  756. ** Function gatt_l2cif_data_ind_cback
  757. **
  758. ** Description This is the L2CAP data indication callback function.
  759. **
  760. **
  761. ** Returns void
  762. **
  763. *******************************************************************************/
  764. static void gatt_l2cif_data_ind_cback(UINT16 lcid, BT_HDR *p_buf)
  765. {
  766. tGATT_TCB *p_tcb;
  767. /* look up clcb for this channel */
  768. if ((p_tcb = gatt_find_tcb_by_cid(lcid)) != NULL &&
  769. gatt_get_ch_state(p_tcb) == GATT_CH_OPEN) {
  770. /* process the data */
  771. gatt_data_process(p_tcb, p_buf);
  772. } else { /* prevent buffer leak */
  773. osi_free(p_buf);
  774. }
  775. }
  776. /*******************************************************************************
  777. **
  778. ** Function gatt_l2cif_congest_cback
  779. **
  780. ** Description L2CAP congestion callback
  781. **
  782. ** Returns void
  783. **
  784. *******************************************************************************/
  785. static void gatt_l2cif_congest_cback (UINT16 lcid, BOOLEAN congested)
  786. {
  787. tGATT_TCB *p_tcb = gatt_find_tcb_by_cid(lcid);
  788. if (p_tcb != NULL) {
  789. gatt_channel_congestion(p_tcb, congested);
  790. }
  791. }
  792. #endif ///CLASSIC_BT_GATT_INCLUDED == TRUE
  793. /*******************************************************************************
  794. **
  795. ** Function gatt_send_conn_cback
  796. **
  797. ** Description Callback used to notify layer above about a connection.
  798. **
  799. **
  800. ** Returns void
  801. **
  802. *******************************************************************************/
  803. static void gatt_send_conn_cback(tGATT_TCB *p_tcb)
  804. {
  805. UINT8 i;
  806. tGATT_REG *p_reg;
  807. tGATT_BG_CONN_DEV *p_bg_dev = NULL;
  808. UINT16 conn_id;
  809. p_bg_dev = gatt_find_bg_dev(p_tcb->peer_bda);
  810. /* notifying all applications for the connection up event */
  811. for (i = 0, p_reg = gatt_cb.cl_rcb ; i < GATT_MAX_APPS; i++, p_reg++) {
  812. if (p_reg->in_use) {
  813. if (p_bg_dev && gatt_is_bg_dev_for_app(p_bg_dev, p_reg->gatt_if)) {
  814. gatt_update_app_use_link_flag(p_reg->gatt_if, p_tcb, TRUE, TRUE);
  815. }
  816. if (p_reg->app_cb.p_conn_cb) {
  817. conn_id = GATT_CREATE_CONN_ID(p_tcb->tcb_idx, p_reg->gatt_if);
  818. (*p_reg->app_cb.p_conn_cb)(p_reg->gatt_if, p_tcb->peer_bda, conn_id,
  819. TRUE, 0, p_tcb->transport);
  820. }
  821. }
  822. }
  823. if (gatt_num_apps_hold_link(p_tcb) && p_tcb->att_lcid == L2CAP_ATT_CID ) {
  824. /* disable idle timeout if one or more clients are holding the link disable the idle timer */
  825. GATT_SetIdleTimeout(p_tcb->peer_bda, GATT_LINK_NO_IDLE_TIMEOUT, p_tcb->transport);
  826. }
  827. }
  828. /*******************************************************************************
  829. **
  830. ** Function gatt_le_data_ind
  831. **
  832. ** Description This function is called when data is received from L2CAP.
  833. ** if we are the originator of the connection, we are the ATT
  834. ** client, and the received message is queued up for the client.
  835. **
  836. ** If we are the destination of the connection, we are the ATT
  837. ** server, so the message is passed to the server processing
  838. ** function.
  839. **
  840. ** Returns void
  841. **
  842. *******************************************************************************/
  843. void gatt_data_process (tGATT_TCB *p_tcb, BT_HDR *p_buf)
  844. {
  845. UINT8 *p = (UINT8 *)(p_buf + 1) + p_buf->offset;
  846. UINT8 op_code, pseudo_op_code;
  847. #if (GATTS_INCLUDED == TRUE) || (GATTC_INCLUDED == TRUE)
  848. UINT16 msg_len;
  849. #endif ///(GATTS_INCLUDED == TRUE) || (GATTC_INCLUDED == TRUE)
  850. if (p_buf->len > 0) {
  851. #if (GATTS_INCLUDED == TRUE) || (GATTC_INCLUDED == TRUE)
  852. msg_len = p_buf->len - 1;
  853. #endif ///(GATTS_INCLUDED == TRUE) || (GATTC_INCLUDED == TRUE)
  854. STREAM_TO_UINT8(op_code, p);
  855. /* remove the two MSBs associated with sign write and write cmd */
  856. pseudo_op_code = op_code & (~GATT_WRITE_CMD_MASK);
  857. if (pseudo_op_code < GATT_OP_CODE_MAX) {
  858. if (op_code == GATT_SIGN_CMD_WRITE) {
  859. #if (SMP_INCLUDED == TRUE)
  860. gatt_verify_signature(p_tcb, p_buf);
  861. #endif ///SMP_INCLUDED == TRUE
  862. } else {
  863. /* message from client */
  864. if ((op_code % 2) == 0) {
  865. #if (GATTS_INCLUDED == TRUE)
  866. gatt_server_handle_client_req (p_tcb, op_code, msg_len, p);
  867. #endif ///GATTS_INCLUDED == TRUE
  868. } else {
  869. #if (GATTC_INCLUDED == TRUE)
  870. gatt_client_handle_server_rsp (p_tcb, op_code, msg_len, p);
  871. #endif ///GATTC_INCLUDED == TRUE
  872. }
  873. }
  874. } else {
  875. if (op_code & GATT_COMMAND_FLAG) {
  876. GATT_TRACE_ERROR ("ATT - Rcvd L2CAP data, unknown cmd: 0x%x\n", op_code);
  877. } else {
  878. GATT_TRACE_ERROR ("ATT - Rcvd L2CAP data, unknown req: 0x%x\n", op_code);
  879. gatt_send_error_rsp (p_tcb, GATT_REQ_NOT_SUPPORTED, op_code, 0, FALSE);
  880. }
  881. }
  882. } else {
  883. GATT_TRACE_ERROR ("invalid data length, ignore\n");
  884. }
  885. osi_free (p_buf);
  886. }
  887. /*******************************************************************************
  888. **
  889. ** Function gatt_add_a_bonded_dev_for_srv_chg
  890. **
  891. ** Description Add a bonded dev to the service changed client list
  892. **
  893. ** Returns void
  894. **
  895. *******************************************************************************/
  896. void gatt_add_a_bonded_dev_for_srv_chg (BD_ADDR bda)
  897. {
  898. tGATTS_SRV_CHG_REQ req;
  899. tGATTS_SRV_CHG srv_chg_clt;
  900. memcpy(srv_chg_clt.bda, bda, BD_ADDR_LEN);
  901. srv_chg_clt.srv_changed = FALSE;
  902. if (gatt_add_srv_chg_clt(&srv_chg_clt) != NULL) {
  903. memcpy(req.srv_chg.bda, bda, BD_ADDR_LEN);
  904. req.srv_chg.srv_changed = FALSE;
  905. if (gatt_cb.cb_info.p_srv_chg_callback) {
  906. (*gatt_cb.cb_info.p_srv_chg_callback)(GATTS_SRV_CHG_CMD_ADD_CLIENT, &req, NULL);
  907. }
  908. }
  909. }
  910. /*******************************************************************************
  911. **
  912. ** Function gatt_send_srv_chg_ind
  913. **
  914. ** Description This function is called to send a service changed indication to
  915. ** the specified bd address
  916. **
  917. ** Returns GATT_SUCCESS if successfully sent; otherwise error code
  918. **
  919. *******************************************************************************/
  920. #if (GATTS_INCLUDED == TRUE)
  921. tGATT_STATUS gatt_send_srv_chg_ind (BD_ADDR peer_bda)
  922. {
  923. UINT8 handle_range[GATT_SIZE_OF_SRV_CHG_HNDL_RANGE];
  924. UINT8 *p = handle_range;
  925. UINT16 conn_id;
  926. tGATT_STATUS status = GATT_ERROR;
  927. GATT_TRACE_DEBUG("gatt_send_srv_chg_ind");
  928. if (gatt_cb.handle_of_h_r) {
  929. if ((conn_id = gatt_profile_find_conn_id_by_bd_addr(peer_bda)) != GATT_INVALID_CONN_ID) {
  930. UINT16_TO_STREAM (p, 1);
  931. UINT16_TO_STREAM (p, 0xFFFF);
  932. status = GATTS_HandleValueIndication (conn_id,
  933. gatt_cb.handle_of_h_r,
  934. GATT_SIZE_OF_SRV_CHG_HNDL_RANGE,
  935. handle_range);
  936. } else {
  937. status = GATT_NOT_FOUND;
  938. GATT_TRACE_ERROR("Unable to find conn_id for %02x%02x%02x%02x%02x%02x ",
  939. peer_bda[0], peer_bda[1], peer_bda[2], peer_bda[3], peer_bda[4], peer_bda[5]);
  940. }
  941. }
  942. return status;
  943. }
  944. /*******************************************************************************
  945. **
  946. ** Function gatt_chk_srv_chg
  947. **
  948. ** Description Check sending service changed Indication is required or not
  949. ** if required then send the Indication
  950. **
  951. ** Returns void
  952. **
  953. *******************************************************************************/
  954. void gatt_chk_srv_chg(tGATTS_SRV_CHG *p_srv_chg_clt)
  955. {
  956. GATT_TRACE_DEBUG("gatt_chk_srv_chg srv_changed=%d", p_srv_chg_clt->srv_changed );
  957. if (p_srv_chg_clt->srv_changed) {
  958. gatt_send_srv_chg_ind(p_srv_chg_clt->bda);
  959. }
  960. }
  961. #endif ///GATTS_INCLUDED == TRUE
  962. /*******************************************************************************
  963. **
  964. ** Function gatt_init_srv_chg
  965. **
  966. ** Description This function is used to initialize the service changed
  967. ** attribute value
  968. **
  969. ** Returns void
  970. **
  971. *******************************************************************************/
  972. void gatt_init_srv_chg (void)
  973. {
  974. tGATTS_SRV_CHG_REQ req;
  975. tGATTS_SRV_CHG_RSP rsp;
  976. BOOLEAN status;
  977. UINT8 num_clients, i;
  978. tGATTS_SRV_CHG srv_chg_clt;
  979. GATT_TRACE_DEBUG("gatt_init_srv_chg");
  980. if (gatt_cb.cb_info.p_srv_chg_callback) {
  981. status = (*gatt_cb.cb_info.p_srv_chg_callback)(GATTS_SRV_CHG_CMD_READ_NUM_CLENTS, NULL, &rsp);
  982. if (status && rsp.num_clients) {
  983. GATT_TRACE_DEBUG("gatt_init_srv_chg num_srv_chg_clt_clients=%d", rsp.num_clients);
  984. num_clients = rsp.num_clients;
  985. i = 1; /* use one based index */
  986. while ((i <= num_clients) && status) {
  987. req.client_read_index = i;
  988. if ((status = (*gatt_cb.cb_info.p_srv_chg_callback)(GATTS_SRV_CHG_CMD_READ_CLENT, &req, &rsp)) == TRUE) {
  989. memcpy(&srv_chg_clt, &rsp.srv_chg , sizeof(tGATTS_SRV_CHG));
  990. if (gatt_add_srv_chg_clt(&srv_chg_clt) == NULL) {
  991. GATT_TRACE_ERROR("Unable to add a service change client");
  992. status = FALSE;
  993. }
  994. }
  995. i++;
  996. }
  997. }
  998. } else {
  999. GATT_TRACE_DEBUG("gatt_init_srv_chg callback not registered yet");
  1000. }
  1001. }
  1002. /*******************************************************************************
  1003. **
  1004. ** Function gatt_proc_srv_chg
  1005. **
  1006. ** Description This function is process the service changed request
  1007. **
  1008. ** Returns void
  1009. **
  1010. *******************************************************************************/
  1011. #if (GATTS_INCLUDED == TRUE)
  1012. void gatt_proc_srv_chg (void)
  1013. {
  1014. UINT8 start_idx, found_idx;
  1015. BD_ADDR bda;
  1016. BOOLEAN srv_chg_ind_pending = FALSE;
  1017. tGATT_TCB *p_tcb;
  1018. tBT_TRANSPORT transport;
  1019. GATT_TRACE_DEBUG ("gatt_proc_srv_chg");
  1020. if (gatt_cb.cb_info.p_srv_chg_callback && gatt_cb.handle_of_h_r) {
  1021. gatt_set_srv_chg();
  1022. start_idx = 0;
  1023. while (gatt_find_the_connected_bda(start_idx, bda, &found_idx, &transport)) {
  1024. p_tcb = &gatt_cb.tcb[found_idx];
  1025. srv_chg_ind_pending = gatt_is_srv_chg_ind_pending(p_tcb);
  1026. if (!srv_chg_ind_pending) {
  1027. gatt_send_srv_chg_ind(bda);
  1028. } else {
  1029. GATT_TRACE_DEBUG ("discard srv chg - already has one in the queue");
  1030. }
  1031. start_idx = ++found_idx;
  1032. }
  1033. }
  1034. }
  1035. #endif ///GATTS_INCLUDED == TRUE
  1036. /*******************************************************************************
  1037. **
  1038. ** Function gatt_set_ch_state
  1039. **
  1040. ** Description This function set the ch_state in tcb
  1041. **
  1042. ** Returns none
  1043. **
  1044. *******************************************************************************/
  1045. void gatt_set_ch_state(tGATT_TCB *p_tcb, tGATT_CH_STATE ch_state)
  1046. {
  1047. if (p_tcb) {
  1048. GATT_TRACE_DEBUG ("gatt_set_ch_state: old=%d new=%d", p_tcb->ch_state, ch_state);
  1049. p_tcb->ch_state = ch_state;
  1050. }
  1051. }
  1052. /*******************************************************************************
  1053. **
  1054. ** Function gatt_get_ch_state
  1055. **
  1056. ** Description This function get the ch_state in tcb
  1057. **
  1058. ** Returns none
  1059. **
  1060. *******************************************************************************/
  1061. tGATT_CH_STATE gatt_get_ch_state(tGATT_TCB *p_tcb)
  1062. {
  1063. tGATT_CH_STATE ch_state = GATT_CH_CLOSE;
  1064. if (p_tcb) {
  1065. GATT_TRACE_DEBUG ("gatt_get_ch_state: ch_state=%d", p_tcb->ch_state);
  1066. ch_state = p_tcb->ch_state;
  1067. }
  1068. return ch_state;
  1069. }
  1070. uint16_t gatt_get_local_mtu(void)
  1071. {
  1072. return gatt_default.local_mtu;
  1073. }
  1074. void gatt_set_local_mtu(uint16_t mtu)
  1075. {
  1076. gatt_default.local_mtu = mtu;
  1077. }
  1078. #endif /* BLE_INCLUDED */