gatt_main.c 43 KB

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