gatt_main.cc 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  1. /******************************************************************************
  2. *
  3. * Copyright 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 "bt_target.h"
  24. #include "bt_common.h"
  25. #include "bt_utils.h"
  26. #include "btif_storage.h"
  27. #include "btm_ble_int.h"
  28. #include "btm_int.h"
  29. #include "connection_manager.h"
  30. #include "device/include/interop.h"
  31. #include "gatt_int.h"
  32. #include "l2c_api.h"
  33. #include "osi/include/osi.h"
  34. using base::StringPrintf;
  35. /* Configuration flags. */
  36. #define GATT_L2C_CFG_IND_DONE (1 << 0)
  37. #define GATT_L2C_CFG_CFM_DONE (1 << 1)
  38. /* minimum GATT MTU size over BR/EDR link
  39. */
  40. #define GATT_MIN_BR_MTU_SIZE 48
  41. /******************************************************************************/
  42. /* L O C A L F U N C T I O N P R O T O T Y P E S */
  43. /******************************************************************************/
  44. static void gatt_le_connect_cback(uint16_t chan, const RawAddress& bd_addr,
  45. bool connected, uint16_t reason,
  46. tBT_TRANSPORT transport);
  47. static void gatt_le_data_ind(uint16_t chan, const RawAddress& bd_addr,
  48. BT_HDR* p_buf);
  49. static void gatt_le_cong_cback(const RawAddress& remote_bda, bool congest);
  50. static void gatt_l2cif_connect_ind_cback(const RawAddress& bd_addr,
  51. uint16_t l2cap_cid, uint16_t psm,
  52. uint8_t l2cap_id);
  53. static void gatt_l2cif_connect_cfm_cback(uint16_t l2cap_cid, uint16_t result);
  54. static void gatt_l2cif_config_ind_cback(uint16_t l2cap_cid,
  55. tL2CAP_CFG_INFO* p_cfg);
  56. static void gatt_l2cif_config_cfm_cback(uint16_t l2cap_cid,
  57. tL2CAP_CFG_INFO* p_cfg);
  58. static void gatt_l2cif_disconnect_ind_cback(uint16_t l2cap_cid,
  59. bool ack_needed);
  60. static void gatt_l2cif_disconnect_cfm_cback(uint16_t l2cap_cid,
  61. uint16_t result);
  62. static void gatt_l2cif_data_ind_cback(uint16_t l2cap_cid, BT_HDR* p_msg);
  63. static void gatt_send_conn_cback(tGATT_TCB* p_tcb);
  64. static void gatt_l2cif_congest_cback(uint16_t cid, bool congested);
  65. static const tL2CAP_APPL_INFO dyn_info = {gatt_l2cif_connect_ind_cback,
  66. gatt_l2cif_connect_cfm_cback,
  67. NULL,
  68. gatt_l2cif_config_ind_cback,
  69. gatt_l2cif_config_cfm_cback,
  70. gatt_l2cif_disconnect_ind_cback,
  71. gatt_l2cif_disconnect_cfm_cback,
  72. NULL,
  73. gatt_l2cif_data_ind_cback,
  74. gatt_l2cif_congest_cback,
  75. NULL,
  76. NULL /* tL2CA_CREDITS_RECEIVED_CB */};
  77. tGATT_CB gatt_cb;
  78. /*******************************************************************************
  79. *
  80. * Function gatt_init
  81. *
  82. * Description This function is enable the GATT profile on the device.
  83. * It clears out the control blocks, and registers with L2CAP.
  84. *
  85. * Returns void
  86. *
  87. ******************************************************************************/
  88. void gatt_init(void) {
  89. tL2CAP_FIXED_CHNL_REG fixed_reg;
  90. VLOG(1) << __func__;
  91. gatt_cb = tGATT_CB();
  92. connection_manager::reset(true);
  93. memset(&fixed_reg, 0, sizeof(tL2CAP_FIXED_CHNL_REG));
  94. gatt_cb.def_mtu_size = GATT_DEF_BLE_MTU_SIZE;
  95. gatt_cb.sign_op_queue = fixed_queue_new(SIZE_MAX);
  96. gatt_cb.srv_chg_clt_q = fixed_queue_new(SIZE_MAX);
  97. /* First, register fixed L2CAP channel for ATT over BLE */
  98. fixed_reg.fixed_chnl_opts.mode = L2CAP_FCR_BASIC_MODE;
  99. fixed_reg.fixed_chnl_opts.max_transmit = 0xFF;
  100. fixed_reg.fixed_chnl_opts.rtrans_tout = 2000;
  101. fixed_reg.fixed_chnl_opts.mon_tout = 12000;
  102. fixed_reg.fixed_chnl_opts.mps = 670;
  103. fixed_reg.fixed_chnl_opts.tx_win_sz = 1;
  104. fixed_reg.pL2CA_FixedConn_Cb = gatt_le_connect_cback;
  105. fixed_reg.pL2CA_FixedData_Cb = gatt_le_data_ind;
  106. fixed_reg.pL2CA_FixedCong_Cb = gatt_le_cong_cback; /* congestion callback */
  107. fixed_reg.default_idle_tout = 0xffff; /* 0xffff default idle timeout */
  108. L2CA_RegisterFixedChannel(L2CAP_ATT_CID, &fixed_reg);
  109. /* Now, register with L2CAP for ATT PSM over BR/EDR */
  110. if (!L2CA_Register(BT_PSM_ATT, (tL2CAP_APPL_INFO*)&dyn_info,
  111. false /* enable_snoop */)) {
  112. LOG(ERROR) << "ATT Dynamic Registration failed";
  113. }
  114. BTM_SetSecurityLevel(true, "", BTM_SEC_SERVICE_ATT, BTM_SEC_NONE, BT_PSM_ATT,
  115. 0, 0);
  116. BTM_SetSecurityLevel(false, "", BTM_SEC_SERVICE_ATT, BTM_SEC_NONE, BT_PSM_ATT,
  117. 0, 0);
  118. gatt_cb.hdl_cfg.gatt_start_hdl = GATT_GATT_START_HANDLE;
  119. gatt_cb.hdl_cfg.gap_start_hdl = GATT_GAP_START_HANDLE;
  120. gatt_cb.hdl_cfg.app_start_hdl = GATT_APP_START_HANDLE;
  121. gatt_cb.hdl_list_info = new std::list<tGATT_HDL_LIST_ELEM>();
  122. gatt_cb.srv_list_info = new std::list<tGATT_SRV_LIST_ELEM>();
  123. gatt_profile_db_init();
  124. }
  125. /*******************************************************************************
  126. *
  127. * Function gatt_free
  128. *
  129. * Description This function frees resources used by the GATT profile.
  130. *
  131. * Returns void
  132. *
  133. ******************************************************************************/
  134. void gatt_free(void) {
  135. int i;
  136. VLOG(1) << __func__;
  137. fixed_queue_free(gatt_cb.sign_op_queue, NULL);
  138. gatt_cb.sign_op_queue = NULL;
  139. fixed_queue_free(gatt_cb.srv_chg_clt_q, NULL);
  140. gatt_cb.srv_chg_clt_q = NULL;
  141. for (i = 0; i < GATT_MAX_PHY_CHANNEL; i++) {
  142. gatt_cb.tcb[i].pending_enc_clcb = std::queue<tGATT_CLCB*>();
  143. fixed_queue_free(gatt_cb.tcb[i].pending_ind_q, NULL);
  144. gatt_cb.tcb[i].pending_ind_q = NULL;
  145. alarm_free(gatt_cb.tcb[i].conf_timer);
  146. gatt_cb.tcb[i].conf_timer = NULL;
  147. alarm_free(gatt_cb.tcb[i].ind_ack_timer);
  148. gatt_cb.tcb[i].ind_ack_timer = NULL;
  149. fixed_queue_free(gatt_cb.tcb[i].sr_cmd.multi_rsp_q, NULL);
  150. gatt_cb.tcb[i].sr_cmd.multi_rsp_q = NULL;
  151. }
  152. gatt_cb.hdl_list_info->clear();
  153. gatt_cb.hdl_list_info = nullptr;
  154. gatt_cb.srv_list_info->clear();
  155. gatt_cb.srv_list_info = nullptr;
  156. }
  157. /*******************************************************************************
  158. *
  159. * Function gatt_connect
  160. *
  161. * Description This function is called to initiate a connection to a peer
  162. * device.
  163. *
  164. * Parameter rem_bda: remote device address to connect to.
  165. *
  166. * Returns true if connection is started, otherwise return false.
  167. *
  168. ******************************************************************************/
  169. bool gatt_connect(const RawAddress& rem_bda, tGATT_TCB* p_tcb,
  170. tBT_TRANSPORT transport, uint8_t initiating_phys,
  171. tGATT_IF gatt_if) {
  172. if (gatt_get_ch_state(p_tcb) != GATT_CH_OPEN)
  173. gatt_set_ch_state(p_tcb, GATT_CH_CONN);
  174. if (transport != BT_TRANSPORT_LE) {
  175. p_tcb->att_lcid = L2CA_ConnectReq(BT_PSM_ATT, rem_bda);
  176. return p_tcb->att_lcid != 0;
  177. }
  178. // Already connected, mark the link as used
  179. if (gatt_get_ch_state(p_tcb) == GATT_CH_OPEN) {
  180. gatt_update_app_use_link_flag(gatt_if, p_tcb, true, true);
  181. return true;
  182. }
  183. p_tcb->att_lcid = L2CAP_ATT_CID;
  184. return connection_manager::direct_connect_add(gatt_if, rem_bda);
  185. }
  186. /*******************************************************************************
  187. *
  188. * Function gatt_disconnect
  189. *
  190. * Description This function is called to disconnect to an ATT device.
  191. *
  192. * Parameter p_tcb: pointer to the TCB to disconnect.
  193. *
  194. * Returns true: if connection found and to be disconnected; otherwise
  195. * return false.
  196. *
  197. ******************************************************************************/
  198. bool gatt_disconnect(tGATT_TCB* p_tcb) {
  199. VLOG(1) << __func__;
  200. if (!p_tcb) return false;
  201. tGATT_CH_STATE ch_state = gatt_get_ch_state(p_tcb);
  202. if (ch_state == GATT_CH_CLOSING) {
  203. VLOG(1) << __func__ << " already in closing state";
  204. return true;
  205. }
  206. bool ret = true;
  207. if (p_tcb->att_lcid == L2CAP_ATT_CID) {
  208. if (ch_state == GATT_CH_OPEN) {
  209. /* only LCB exist between remote device and local */
  210. ret = L2CA_RemoveFixedChnl(L2CAP_ATT_CID, p_tcb->peer_bda);
  211. } else {
  212. L2CA_CancelBleConnectReq(p_tcb->peer_bda);
  213. gatt_cleanup_upon_disc(p_tcb->peer_bda, HCI_ERR_CONN_CAUSE_LOCAL_HOST, p_tcb->transport);
  214. return true;
  215. }
  216. gatt_set_ch_state(p_tcb, GATT_CH_CLOSING);
  217. } else {
  218. if ((ch_state == GATT_CH_OPEN) || (ch_state == GATT_CH_CFG))
  219. ret = L2CA_DisconnectReq(p_tcb->att_lcid);
  220. else
  221. VLOG(1) << __func__ << " gatt_disconnect channel not opened";
  222. }
  223. return ret;
  224. }
  225. /*******************************************************************************
  226. *
  227. * Function gatt_update_app_hold_link_status
  228. *
  229. * Description Update the application use link status
  230. *
  231. * Returns true if any modifications are made or
  232. * when it already exists, false otherwise.
  233. *
  234. ******************************************************************************/
  235. bool gatt_update_app_hold_link_status(tGATT_IF gatt_if, tGATT_TCB* p_tcb,
  236. bool is_add) {
  237. auto& holders = p_tcb->app_hold_link;
  238. VLOG(1) << __func__;
  239. if (is_add) {
  240. auto ret = holders.insert(gatt_if);
  241. if (ret.second) {
  242. VLOG(1) << "added gatt_if=" << +gatt_if;
  243. } else {
  244. VLOG(1) << "attempt to add already existing gatt_if=" << +gatt_if;
  245. }
  246. return true;
  247. }
  248. //! is_add
  249. if (!holders.erase(gatt_if)) {
  250. VLOG(1) << "attempt to remove nonexisting gatt_if=" << +gatt_if;
  251. return false;
  252. }
  253. VLOG(1) << "removed gatt_if=" << +gatt_if;
  254. return true;
  255. }
  256. /*******************************************************************************
  257. *
  258. * Function gatt_update_app_use_link_flag
  259. *
  260. * Description Update the application use link flag and optional to check
  261. * the acl link if the link is up then set the idle time out
  262. * accordingly
  263. *
  264. * Returns void.
  265. *
  266. ******************************************************************************/
  267. void gatt_update_app_use_link_flag(tGATT_IF gatt_if, tGATT_TCB* p_tcb,
  268. bool is_add, bool check_acl_link) {
  269. VLOG(1) << StringPrintf("%s: is_add=%d chk_link=%d", __func__, is_add,
  270. check_acl_link);
  271. if (!p_tcb) return;
  272. // If we make no modification, i.e. kill app that was never connected to a
  273. // device, skip updating the device state.
  274. if (!gatt_update_app_hold_link_status(gatt_if, p_tcb, is_add)) return;
  275. if (!check_acl_link) {
  276. return;
  277. }
  278. bool is_valid_handle =
  279. (BTM_GetHCIConnHandle(p_tcb->peer_bda, p_tcb->transport) !=
  280. GATT_INVALID_ACL_HANDLE);
  281. if (is_add) {
  282. if (p_tcb->att_lcid == L2CAP_ATT_CID && is_valid_handle) {
  283. VLOG(1) << "disable link idle timer";
  284. /* acl link is connected disable the idle timeout */
  285. GATT_SetIdleTimeout(p_tcb->peer_bda, GATT_LINK_NO_IDLE_TIMEOUT,
  286. p_tcb->transport);
  287. }
  288. } else {
  289. if (p_tcb->app_hold_link.empty()) {
  290. // acl link is connected but no application needs to use the link
  291. if (p_tcb->att_lcid == L2CAP_ATT_CID && is_valid_handle) {
  292. /* for fixed channel, set the timeout value to
  293. GATT_LINK_IDLE_TIMEOUT_WHEN_NO_APP seconds */
  294. VLOG(1) << " start link idle timer = "
  295. << GATT_LINK_IDLE_TIMEOUT_WHEN_NO_APP << " sec";
  296. GATT_SetIdleTimeout(p_tcb->peer_bda, GATT_LINK_IDLE_TIMEOUT_WHEN_NO_APP,
  297. p_tcb->transport);
  298. } else
  299. // disconnect the dynamic channel
  300. gatt_disconnect(p_tcb);
  301. }
  302. }
  303. }
  304. /** GATT connection initiation */
  305. bool gatt_act_connect(tGATT_REG* p_reg, const RawAddress& bd_addr,
  306. tBT_TRANSPORT transport, int8_t initiating_phys) {
  307. tGATT_TCB* p_tcb = gatt_find_tcb_by_addr(bd_addr, transport);
  308. if (p_tcb != NULL) {
  309. /* before link down, another app try to open a GATT connection */
  310. uint8_t st = gatt_get_ch_state(p_tcb);
  311. if (st == GATT_CH_OPEN && p_tcb->app_hold_link.empty() &&
  312. transport == BT_TRANSPORT_LE) {
  313. if (!gatt_connect(bd_addr, p_tcb, transport, initiating_phys,
  314. p_reg->gatt_if))
  315. return false;
  316. } else if (st == GATT_CH_CLOSING) {
  317. LOG(INFO) << "Must finish disconnection before new connection";
  318. /* need to complete the closing first */
  319. return false;
  320. }
  321. return true;
  322. }
  323. p_tcb = gatt_allocate_tcb_by_bdaddr(bd_addr, transport);
  324. if (!p_tcb) {
  325. LOG(ERROR) << "Max TCB for gatt_if [ " << +p_reg->gatt_if << "] reached.";
  326. return false;
  327. }
  328. if (!gatt_connect(bd_addr, p_tcb, transport, initiating_phys,
  329. p_reg->gatt_if)) {
  330. LOG(ERROR) << "gatt_connect failed";
  331. fixed_queue_free(p_tcb->pending_ind_q, NULL);
  332. *p_tcb = tGATT_TCB();
  333. return false;
  334. }
  335. return true;
  336. }
  337. namespace connection_manager {
  338. void on_connection_timed_out(uint8_t app_id, const RawAddress& address) {
  339. gatt_le_connect_cback(L2CAP_ATT_CID, address, false, 0xff, BT_TRANSPORT_LE);
  340. }
  341. } // namespace connection_manager
  342. /** This callback function is called by L2CAP to indicate that the ATT fixed
  343. * channel for LE is connected (conn = true)/disconnected (conn = false).
  344. */
  345. static void gatt_le_connect_cback(uint16_t chan, const RawAddress& bd_addr,
  346. bool connected, uint16_t reason,
  347. tBT_TRANSPORT transport) {
  348. tGATT_TCB* p_tcb = gatt_find_tcb_by_addr(bd_addr, transport);
  349. bool check_srv_chg = false;
  350. tGATTS_SRV_CHG* p_srv_chg_clt = NULL;
  351. /* ignore all fixed channel connect/disconnect on BR/EDR link for GATT */
  352. if (transport == BT_TRANSPORT_BR_EDR) return;
  353. VLOG(1) << "GATT ATT protocol channel with BDA: " << bd_addr << " is "
  354. << ((connected) ? "connected" : "disconnected");
  355. p_srv_chg_clt = gatt_is_bda_in_the_srv_chg_clt_list(bd_addr);
  356. if (p_srv_chg_clt != NULL) {
  357. check_srv_chg = true;
  358. } else {
  359. if (btm_sec_is_a_bonded_dev(bd_addr))
  360. gatt_add_a_bonded_dev_for_srv_chg(bd_addr);
  361. }
  362. if (!connected) {
  363. gatt_cleanup_upon_disc(bd_addr, reason, transport);
  364. VLOG(1) << "ATT disconnected";
  365. return;
  366. }
  367. /* do we have a channel initiating a connection? */
  368. if (p_tcb) {
  369. /* we are initiating connection */
  370. if (gatt_get_ch_state(p_tcb) == GATT_CH_CONN) {
  371. /* send callback */
  372. gatt_set_ch_state(p_tcb, GATT_CH_OPEN);
  373. p_tcb->payload_size = GATT_DEF_BLE_MTU_SIZE;
  374. gatt_send_conn_cback(p_tcb);
  375. }
  376. if (check_srv_chg) gatt_chk_srv_chg(p_srv_chg_clt);
  377. }
  378. /* this is incoming connection or background connection callback */
  379. else {
  380. p_tcb = gatt_allocate_tcb_by_bdaddr(bd_addr, BT_TRANSPORT_LE);
  381. if (!p_tcb) {
  382. LOG(ERROR) << "CCB max out, no rsources";
  383. return;
  384. }
  385. p_tcb->att_lcid = L2CAP_ATT_CID;
  386. gatt_set_ch_state(p_tcb, GATT_CH_OPEN);
  387. p_tcb->payload_size = GATT_DEF_BLE_MTU_SIZE;
  388. gatt_send_conn_cback(p_tcb);
  389. if (check_srv_chg) {
  390. gatt_chk_srv_chg(p_srv_chg_clt);
  391. }
  392. }
  393. }
  394. /** This function is called to process the congestion callback from lcb */
  395. static void gatt_channel_congestion(tGATT_TCB* p_tcb, bool congested) {
  396. uint8_t i = 0;
  397. tGATT_REG* p_reg = NULL;
  398. uint16_t conn_id;
  399. /* if uncongested, check to see if there is any more pending data */
  400. if (p_tcb != NULL && !congested) {
  401. gatt_cl_send_next_cmd_inq(*p_tcb);
  402. }
  403. /* notifying all applications for the connection up event */
  404. for (i = 0, p_reg = gatt_cb.cl_rcb; i < GATT_MAX_APPS; i++, p_reg++) {
  405. if (p_reg->in_use) {
  406. if (p_reg->app_cb.p_congestion_cb) {
  407. conn_id = GATT_CREATE_CONN_ID(p_tcb->tcb_idx, p_reg->gatt_if);
  408. (*p_reg->app_cb.p_congestion_cb)(conn_id, congested);
  409. }
  410. }
  411. }
  412. }
  413. void gatt_notify_phy_updated(uint8_t status, uint16_t handle, uint8_t tx_phy,
  414. uint8_t rx_phy) {
  415. tBTM_SEC_DEV_REC* p_dev_rec = btm_find_dev_by_handle(handle);
  416. if (!p_dev_rec) {
  417. BTM_TRACE_WARNING("%s: No Device Found!", __func__);
  418. return;
  419. }
  420. tGATT_TCB* p_tcb =
  421. gatt_find_tcb_by_addr(p_dev_rec->ble.pseudo_addr, BT_TRANSPORT_LE);
  422. if (!p_tcb) return;
  423. for (int i = 0; i < GATT_MAX_APPS; i++) {
  424. tGATT_REG* p_reg = &gatt_cb.cl_rcb[i];
  425. if (p_reg->in_use && p_reg->app_cb.p_phy_update_cb) {
  426. uint16_t conn_id = GATT_CREATE_CONN_ID(p_tcb->tcb_idx, p_reg->gatt_if);
  427. (*p_reg->app_cb.p_phy_update_cb)(p_reg->gatt_if, conn_id, tx_phy, rx_phy,
  428. status);
  429. }
  430. }
  431. }
  432. void gatt_notify_conn_update(uint16_t handle, uint16_t interval,
  433. uint16_t latency, uint16_t timeout,
  434. uint8_t status) {
  435. tBTM_SEC_DEV_REC* p_dev_rec = btm_find_dev_by_handle(handle);
  436. if (!p_dev_rec) return;
  437. tGATT_TCB* p_tcb =
  438. gatt_find_tcb_by_addr(p_dev_rec->ble.pseudo_addr, BT_TRANSPORT_LE);
  439. if (!p_tcb) return;
  440. for (int i = 0; i < GATT_MAX_APPS; i++) {
  441. tGATT_REG* p_reg = &gatt_cb.cl_rcb[i];
  442. if (p_reg->in_use && p_reg->app_cb.p_conn_update_cb) {
  443. uint16_t conn_id = GATT_CREATE_CONN_ID(p_tcb->tcb_idx, p_reg->gatt_if);
  444. (*p_reg->app_cb.p_conn_update_cb)(p_reg->gatt_if, conn_id, interval,
  445. latency, timeout, status);
  446. }
  447. }
  448. }
  449. /** This function is called when GATT fixed channel is congested or uncongested
  450. */
  451. static void gatt_le_cong_cback(const RawAddress& remote_bda, bool congested) {
  452. tGATT_TCB* p_tcb = gatt_find_tcb_by_addr(remote_bda, BT_TRANSPORT_LE);
  453. if (!p_tcb) return;
  454. /* if uncongested, check to see if there is any more pending data */
  455. gatt_channel_congestion(p_tcb, congested);
  456. }
  457. /*******************************************************************************
  458. *
  459. * Function gatt_le_data_ind
  460. *
  461. * Description This function is called when data is received from L2CAP.
  462. * if we are the originator of the connection, we are the ATT
  463. * client, and the received message is queued up for the
  464. * client.
  465. *
  466. * If we are the destination of the connection, we are the ATT
  467. * server, so the message is passed to the server processing
  468. * function.
  469. *
  470. * Returns void
  471. *
  472. ******************************************************************************/
  473. static void gatt_le_data_ind(uint16_t chan, const RawAddress& bd_addr,
  474. BT_HDR* p_buf) {
  475. /* Find CCB based on bd addr */
  476. tGATT_TCB* p_tcb = gatt_find_tcb_by_addr(bd_addr, BT_TRANSPORT_LE);
  477. if (p_tcb) {
  478. if (gatt_get_ch_state(p_tcb) < GATT_CH_OPEN) {
  479. LOG(WARNING) << "ATT - Ignored L2CAP data while in state: "
  480. << +gatt_get_ch_state(p_tcb);
  481. } else
  482. gatt_data_process(*p_tcb, p_buf);
  483. }
  484. osi_free(p_buf);
  485. }
  486. /*******************************************************************************
  487. *
  488. * Function gatt_l2cif_connect_ind
  489. *
  490. * Description This function handles an inbound connection indication
  491. * from L2CAP. This is the case where we are acting as a
  492. * server.
  493. *
  494. * Returns void
  495. *
  496. ******************************************************************************/
  497. static void gatt_l2cif_connect_ind_cback(const RawAddress& bd_addr,
  498. uint16_t lcid,
  499. UNUSED_ATTR uint16_t psm, uint8_t id) {
  500. uint8_t result = L2CAP_CONN_OK;
  501. LOG(INFO) << "Connection indication cid = " << +lcid;
  502. /* new connection ? */
  503. tGATT_TCB* p_tcb = gatt_find_tcb_by_addr(bd_addr, BT_TRANSPORT_BR_EDR);
  504. if (p_tcb == NULL) {
  505. /* allocate tcb */
  506. p_tcb = gatt_allocate_tcb_by_bdaddr(bd_addr, BT_TRANSPORT_BR_EDR);
  507. if (p_tcb == NULL) {
  508. /* no tcb available, reject L2CAP connection */
  509. result = L2CAP_CONN_NO_RESOURCES;
  510. } else
  511. p_tcb->att_lcid = lcid;
  512. } else /* existing connection , reject it */
  513. {
  514. result = L2CAP_CONN_NO_RESOURCES;
  515. }
  516. /* Send L2CAP connect rsp */
  517. L2CA_ConnectRsp(bd_addr, id, lcid, result, 0);
  518. /* if result ok, proceed with connection */
  519. if (result != L2CAP_CONN_OK) return;
  520. /* transition to configuration state */
  521. gatt_set_ch_state(p_tcb, GATT_CH_CFG);
  522. /* Send L2CAP config req */
  523. tL2CAP_CFG_INFO cfg;
  524. memset(&cfg, 0, sizeof(tL2CAP_CFG_INFO));
  525. cfg.mtu_present = true;
  526. cfg.mtu = GATT_MAX_MTU_SIZE;
  527. L2CA_ConfigReq(lcid, &cfg);
  528. }
  529. /** This is the L2CAP connect confirm callback function */
  530. static void gatt_l2cif_connect_cfm_cback(uint16_t lcid, uint16_t result) {
  531. tGATT_TCB* p_tcb;
  532. tL2CAP_CFG_INFO cfg;
  533. /* look up clcb for this channel */
  534. p_tcb = gatt_find_tcb_by_cid(lcid);
  535. if (!p_tcb) return;
  536. VLOG(1) << __func__
  537. << StringPrintf(" result: %d ch_state: %d, lcid:0x%x", result,
  538. gatt_get_ch_state(p_tcb), p_tcb->att_lcid);
  539. /* if in correct state */
  540. if (gatt_get_ch_state(p_tcb) == GATT_CH_CONN) {
  541. /* if result successful */
  542. if (result == L2CAP_CONN_OK) {
  543. /* set channel state */
  544. gatt_set_ch_state(p_tcb, GATT_CH_CFG);
  545. /* Send L2CAP config req */
  546. memset(&cfg, 0, sizeof(tL2CAP_CFG_INFO));
  547. cfg.mtu_present = true;
  548. cfg.mtu = GATT_MAX_MTU_SIZE;
  549. L2CA_ConfigReq(lcid, &cfg);
  550. }
  551. /* else initiating connection failure */
  552. else {
  553. gatt_cleanup_upon_disc(p_tcb->peer_bda, result, GATT_TRANSPORT_BR_EDR);
  554. }
  555. } else /* wrong state, disconnect it */
  556. {
  557. if (result == L2CAP_CONN_OK) {
  558. /* just in case the peer also accepts our connection - Send L2CAP
  559. * disconnect req */
  560. L2CA_DisconnectReq(lcid);
  561. }
  562. }
  563. }
  564. /** This is the L2CAP config confirm callback function */
  565. void gatt_l2cif_config_cfm_cback(uint16_t lcid, tL2CAP_CFG_INFO* p_cfg) {
  566. /* look up clcb for this channel */
  567. tGATT_TCB* p_tcb = gatt_find_tcb_by_cid(lcid);
  568. if (!p_tcb) return;
  569. /* if in incorrect state */
  570. if (gatt_get_ch_state(p_tcb) != GATT_CH_CFG) return;
  571. /* if result not successful */
  572. if (p_cfg->result != L2CAP_CFG_OK) {
  573. /* Send L2CAP disconnect req */
  574. L2CA_DisconnectReq(lcid);
  575. return;
  576. }
  577. /* update flags */
  578. p_tcb->ch_flags |= GATT_L2C_CFG_CFM_DONE;
  579. /* if configuration not complete */
  580. if (!(p_tcb->ch_flags & GATT_L2C_CFG_IND_DONE)) return;
  581. gatt_set_ch_state(p_tcb, GATT_CH_OPEN);
  582. tGATTS_SRV_CHG* p_srv_chg_clt =
  583. gatt_is_bda_in_the_srv_chg_clt_list(p_tcb->peer_bda);
  584. if (p_srv_chg_clt != NULL) {
  585. gatt_chk_srv_chg(p_srv_chg_clt);
  586. } else {
  587. if (btm_sec_is_a_bonded_dev(p_tcb->peer_bda))
  588. gatt_add_a_bonded_dev_for_srv_chg(p_tcb->peer_bda);
  589. }
  590. /* send callback */
  591. gatt_send_conn_cback(p_tcb);
  592. }
  593. /** This is the L2CAP config indication callback function */
  594. void gatt_l2cif_config_ind_cback(uint16_t lcid, tL2CAP_CFG_INFO* p_cfg) {
  595. tGATTS_SRV_CHG* p_srv_chg_clt = NULL;
  596. /* look up clcb for this channel */
  597. tGATT_TCB* p_tcb = gatt_find_tcb_by_cid(lcid);
  598. if (!p_tcb) return;
  599. /* GATT uses the smaller of our MTU and peer's MTU */
  600. if (p_cfg->mtu_present &&
  601. (p_cfg->mtu >= GATT_MIN_BR_MTU_SIZE && p_cfg->mtu < L2CAP_DEFAULT_MTU))
  602. p_tcb->payload_size = p_cfg->mtu;
  603. else
  604. p_tcb->payload_size = L2CAP_DEFAULT_MTU;
  605. /* send L2CAP configure response */
  606. memset(p_cfg, 0, sizeof(tL2CAP_CFG_INFO));
  607. p_cfg->result = L2CAP_CFG_OK;
  608. L2CA_ConfigRsp(lcid, p_cfg);
  609. /* if not first config ind */
  610. if ((p_tcb->ch_flags & GATT_L2C_CFG_IND_DONE)) return;
  611. /* update flags */
  612. p_tcb->ch_flags |= GATT_L2C_CFG_IND_DONE;
  613. /* if configuration not complete */
  614. if ((p_tcb->ch_flags & GATT_L2C_CFG_CFM_DONE) == 0) return;
  615. gatt_set_ch_state(p_tcb, GATT_CH_OPEN);
  616. p_srv_chg_clt = gatt_is_bda_in_the_srv_chg_clt_list(p_tcb->peer_bda);
  617. if (p_srv_chg_clt != NULL) {
  618. gatt_chk_srv_chg(p_srv_chg_clt);
  619. } else {
  620. if (btm_sec_is_a_bonded_dev(p_tcb->peer_bda))
  621. gatt_add_a_bonded_dev_for_srv_chg(p_tcb->peer_bda);
  622. }
  623. /* send callback */
  624. gatt_send_conn_cback(p_tcb);
  625. }
  626. /** This is the L2CAP disconnect indication callback function */
  627. void gatt_l2cif_disconnect_ind_cback(uint16_t lcid, bool ack_needed) {
  628. /* look up clcb for this channel */
  629. tGATT_TCB* p_tcb = gatt_find_tcb_by_cid(lcid);
  630. if (!p_tcb) return;
  631. if (ack_needed) {
  632. /* send L2CAP disconnect response */
  633. L2CA_DisconnectRsp(lcid);
  634. }
  635. if (gatt_is_bda_in_the_srv_chg_clt_list(p_tcb->peer_bda) == NULL) {
  636. if (btm_sec_is_a_bonded_dev(p_tcb->peer_bda))
  637. gatt_add_a_bonded_dev_for_srv_chg(p_tcb->peer_bda);
  638. }
  639. /* if ACL link is still up, no reason is logged, l2cap is disconnect from
  640. * peer */
  641. uint16_t reason = L2CA_GetDisconnectReason(p_tcb->peer_bda, p_tcb->transport);
  642. if (reason == 0) reason = GATT_CONN_TERMINATE_PEER_USER;
  643. /* send disconnect callback */
  644. gatt_cleanup_upon_disc(p_tcb->peer_bda, reason, GATT_TRANSPORT_BR_EDR);
  645. }
  646. /** This is the L2CAP disconnect confirm callback function */
  647. static void gatt_l2cif_disconnect_cfm_cback(uint16_t lcid,
  648. UNUSED_ATTR uint16_t result) {
  649. /* look up clcb for this channel */
  650. tGATT_TCB* p_tcb = gatt_find_tcb_by_cid(lcid);
  651. if (!p_tcb) return;
  652. /* If the device is not in the service changed client list, add it... */
  653. if (gatt_is_bda_in_the_srv_chg_clt_list(p_tcb->peer_bda) == NULL) {
  654. if (btm_sec_is_a_bonded_dev(p_tcb->peer_bda))
  655. gatt_add_a_bonded_dev_for_srv_chg(p_tcb->peer_bda);
  656. }
  657. /* send disconnect callback */
  658. /* if ACL link is still up, no reason is logged, l2cap is disconnect from
  659. * peer */
  660. uint16_t reason = L2CA_GetDisconnectReason(p_tcb->peer_bda, p_tcb->transport);
  661. if (reason == 0) reason = GATT_CONN_TERMINATE_LOCAL_HOST;
  662. gatt_cleanup_upon_disc(p_tcb->peer_bda, reason, GATT_TRANSPORT_BR_EDR);
  663. }
  664. /** This is the L2CAP data indication callback function */
  665. static void gatt_l2cif_data_ind_cback(uint16_t lcid, BT_HDR* p_buf) {
  666. /* look up clcb for this channel */
  667. tGATT_TCB* p_tcb = gatt_find_tcb_by_cid(lcid);
  668. if (p_tcb && gatt_get_ch_state(p_tcb) == GATT_CH_OPEN) {
  669. /* process the data */
  670. gatt_data_process(*p_tcb, p_buf);
  671. }
  672. osi_free(p_buf);
  673. }
  674. /** L2CAP congestion callback */
  675. static void gatt_l2cif_congest_cback(uint16_t lcid, bool congested) {
  676. tGATT_TCB* p_tcb = gatt_find_tcb_by_cid(lcid);
  677. if (p_tcb != NULL) {
  678. gatt_channel_congestion(p_tcb, congested);
  679. }
  680. }
  681. /** Callback used to notify layer above about a connection */
  682. static void gatt_send_conn_cback(tGATT_TCB* p_tcb) {
  683. uint8_t i;
  684. tGATT_REG* p_reg;
  685. uint16_t conn_id;
  686. std::set<tGATT_IF> apps =
  687. connection_manager::get_apps_connecting_to(p_tcb->peer_bda);
  688. /* notifying all applications for the connection up event */
  689. for (i = 0, p_reg = gatt_cb.cl_rcb; i < GATT_MAX_APPS; i++, p_reg++) {
  690. if (!p_reg->in_use) continue;
  691. if (apps.find(p_reg->gatt_if) != apps.end())
  692. gatt_update_app_use_link_flag(p_reg->gatt_if, p_tcb, true, true);
  693. if (p_reg->app_cb.p_conn_cb) {
  694. conn_id = GATT_CREATE_CONN_ID(p_tcb->tcb_idx, p_reg->gatt_if);
  695. (*p_reg->app_cb.p_conn_cb)(p_reg->gatt_if, p_tcb->peer_bda, conn_id, true,
  696. 0, p_tcb->transport);
  697. }
  698. }
  699. /* Remove the direct connection */
  700. connection_manager::on_connection_complete(p_tcb->peer_bda);
  701. if (!p_tcb->app_hold_link.empty() && p_tcb->att_lcid == L2CAP_ATT_CID) {
  702. /* disable idle timeout if one or more clients are holding the link disable
  703. * the idle timer */
  704. GATT_SetIdleTimeout(p_tcb->peer_bda, GATT_LINK_NO_IDLE_TIMEOUT,
  705. p_tcb->transport);
  706. }
  707. }
  708. /*******************************************************************************
  709. *
  710. * Function gatt_le_data_ind
  711. *
  712. * Description This function is called when data is received from L2CAP.
  713. * if we are the originator of the connection, we are the ATT
  714. * client, and the received message is queued up for the
  715. * client.
  716. *
  717. * If we are the destination of the connection, we are the ATT
  718. * server, so the message is passed to the server processing
  719. * function.
  720. *
  721. * Returns void
  722. *
  723. ******************************************************************************/
  724. void gatt_data_process(tGATT_TCB& tcb, BT_HDR* p_buf) {
  725. uint8_t* p = (uint8_t*)(p_buf + 1) + p_buf->offset;
  726. uint8_t op_code, pseudo_op_code;
  727. if (p_buf->len <= 0) {
  728. LOG(ERROR) << "invalid data length, ignore";
  729. return;
  730. }
  731. uint16_t msg_len = p_buf->len - 1;
  732. STREAM_TO_UINT8(op_code, p);
  733. /* remove the two MSBs associated with sign write and write cmd */
  734. pseudo_op_code = op_code & (~GATT_WRITE_CMD_MASK);
  735. if (pseudo_op_code >= GATT_OP_CODE_MAX) {
  736. /* Note: PTS: GATT/SR/UNS/BI-01-C mandates error on unsupported ATT request.
  737. */
  738. LOG(ERROR) << __func__
  739. << ": ATT - Rcvd L2CAP data, unknown cmd: " << loghex(op_code);
  740. gatt_send_error_rsp(tcb, GATT_REQ_NOT_SUPPORTED, op_code, 0, false);
  741. return;
  742. }
  743. if (op_code == GATT_SIGN_CMD_WRITE) {
  744. gatt_verify_signature(tcb, p_buf);
  745. } else {
  746. /* message from client */
  747. if ((op_code % 2) == 0)
  748. gatt_server_handle_client_req(tcb, op_code, msg_len, p);
  749. else
  750. gatt_client_handle_server_rsp(tcb, op_code, msg_len, p);
  751. }
  752. }
  753. /** Add a bonded dev to the service changed client list */
  754. void gatt_add_a_bonded_dev_for_srv_chg(const RawAddress& bda) {
  755. tGATTS_SRV_CHG_REQ req;
  756. tGATTS_SRV_CHG srv_chg_clt;
  757. srv_chg_clt.bda = bda;
  758. srv_chg_clt.srv_changed = false;
  759. if (!gatt_add_srv_chg_clt(&srv_chg_clt)) return;
  760. req.srv_chg.bda = bda;
  761. req.srv_chg.srv_changed = false;
  762. if (gatt_cb.cb_info.p_srv_chg_callback)
  763. (*gatt_cb.cb_info.p_srv_chg_callback)(GATTS_SRV_CHG_CMD_ADD_CLIENT, &req,
  764. NULL);
  765. }
  766. /** This function is called to send a service chnaged indication to the
  767. * specified bd address */
  768. void gatt_send_srv_chg_ind(const RawAddress& peer_bda) {
  769. VLOG(1) << __func__;
  770. if (!gatt_cb.handle_of_h_r) return;
  771. uint16_t conn_id = gatt_profile_find_conn_id_by_bd_addr(peer_bda);
  772. if (conn_id == GATT_INVALID_CONN_ID) {
  773. LOG(ERROR) << "Unable to find conn_id for " << peer_bda;
  774. return;
  775. }
  776. uint8_t handle_range[GATT_SIZE_OF_SRV_CHG_HNDL_RANGE];
  777. uint8_t* p = handle_range;
  778. UINT16_TO_STREAM(p, 1);
  779. UINT16_TO_STREAM(p, 0xFFFF);
  780. GATTS_HandleValueIndication(conn_id, gatt_cb.handle_of_h_r,
  781. GATT_SIZE_OF_SRV_CHG_HNDL_RANGE, handle_range);
  782. }
  783. /** Check sending service chnaged Indication is required or not if required then
  784. * send the Indication */
  785. void gatt_chk_srv_chg(tGATTS_SRV_CHG* p_srv_chg_clt) {
  786. VLOG(1) << __func__ << " srv_changed=" << +p_srv_chg_clt->srv_changed;
  787. if (p_srv_chg_clt->srv_changed) {
  788. gatt_send_srv_chg_ind(p_srv_chg_clt->bda);
  789. }
  790. }
  791. /** This function is used to initialize the service changed attribute value */
  792. void gatt_init_srv_chg(void) {
  793. tGATTS_SRV_CHG_REQ req;
  794. tGATTS_SRV_CHG_RSP rsp;
  795. tGATTS_SRV_CHG srv_chg_clt;
  796. VLOG(1) << __func__;
  797. if (!gatt_cb.cb_info.p_srv_chg_callback) {
  798. VLOG(1) << __func__ << " callback not registered yet";
  799. return;
  800. }
  801. bool status = (*gatt_cb.cb_info.p_srv_chg_callback)(
  802. GATTS_SRV_CHG_CMD_READ_NUM_CLENTS, NULL, &rsp);
  803. if (!(status && rsp.num_clients)) return;
  804. VLOG(1) << "num_srv_chg_clt_clients=" << +rsp.num_clients;
  805. uint8_t num_clients = rsp.num_clients;
  806. uint8_t i = 1; /* use one based index */
  807. while ((i <= num_clients) && status) {
  808. req.client_read_index = i;
  809. status = (*gatt_cb.cb_info.p_srv_chg_callback)(GATTS_SRV_CHG_CMD_READ_CLENT,
  810. &req, &rsp);
  811. if (status) {
  812. memcpy(&srv_chg_clt, &rsp.srv_chg, sizeof(tGATTS_SRV_CHG));
  813. if (gatt_add_srv_chg_clt(&srv_chg_clt) == NULL) {
  814. LOG(ERROR) << "Unable to add a service change client";
  815. status = false;
  816. }
  817. }
  818. i++;
  819. }
  820. }
  821. /**This function is process the service changed request */
  822. void gatt_proc_srv_chg(void) {
  823. RawAddress bda;
  824. tBT_TRANSPORT transport;
  825. uint8_t found_idx;
  826. VLOG(1) << __func__;
  827. if (!gatt_cb.cb_info.p_srv_chg_callback || !gatt_cb.handle_of_h_r) return;
  828. gatt_set_srv_chg();
  829. uint8_t start_idx = 0;
  830. while (gatt_find_the_connected_bda(start_idx, bda, &found_idx, &transport)) {
  831. tGATT_TCB* p_tcb = &gatt_cb.tcb[found_idx];
  832. bool send_indication = true;
  833. if (gatt_is_srv_chg_ind_pending(p_tcb)) {
  834. send_indication = false;
  835. VLOG(1) << "discard srv chg - already has one in the queue";
  836. }
  837. // Some LE GATT clients don't respond to service changed indications.
  838. char remote_name[BTM_MAX_REM_BD_NAME_LEN] = "";
  839. if (send_indication &&
  840. btif_storage_get_stored_remote_name(bda, remote_name)) {
  841. if (interop_match_name(INTEROP_GATTC_NO_SERVICE_CHANGED_IND,
  842. remote_name)) {
  843. VLOG(1) << "discard srv chg - interop matched " << remote_name;
  844. send_indication = false;
  845. }
  846. }
  847. if (send_indication) gatt_send_srv_chg_ind(bda);
  848. start_idx = ++found_idx;
  849. }
  850. }
  851. /** This function set the ch_state in tcb */
  852. void gatt_set_ch_state(tGATT_TCB* p_tcb, tGATT_CH_STATE ch_state) {
  853. if (!p_tcb) return;
  854. VLOG(1) << __func__ << ": old=" << +p_tcb->ch_state
  855. << " new=" << loghex(ch_state);
  856. p_tcb->ch_state = ch_state;
  857. }
  858. /** This function get the ch_state in tcb */
  859. tGATT_CH_STATE gatt_get_ch_state(tGATT_TCB* p_tcb) {
  860. if (!p_tcb) return GATT_CH_CLOSE;
  861. VLOG(1) << "gatt_get_ch_state: ch_state=" << +p_tcb->ch_state;
  862. return p_tcb->ch_state;
  863. }