l2c_ble.c 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707
  1. /******************************************************************************
  2. *
  3. * Copyright (C) 2009-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 functions relating to BLE management.
  21. *
  22. ******************************************************************************/
  23. #include <string.h>
  24. #include "common/bt_target.h"
  25. //#include "bt_utils.h"
  26. #include "stack/l2cdefs.h"
  27. #include "l2c_int.h"
  28. #include "stack/btu.h"
  29. #include "btm_int.h"
  30. #include "stack/hcimsgs.h"
  31. #include "device/controller.h"
  32. #if (BLE_INCLUDED == TRUE)
  33. #if (BLE_50_FEATURE_SUPPORT == TRUE)
  34. #define EXT_CONN_INT_DEF_1M MAX(((MAX_ACL_CONNECTIONS + 1) * 4), 12)
  35. #define EXT_CONN_INT_DEF_2M MAX(((MAX_ACL_CONNECTIONS + 1) * 2), 12)
  36. #define EXT_CONN_INT_DEF_CODED (320) // 306-> 362Kbps
  37. const static tHCI_ExtConnParams ext_conn_params_1m_phy = {
  38. .scan_interval = 0x40,
  39. .scan_window = 0x40,
  40. .conn_interval_min = EXT_CONN_INT_DEF_1M,
  41. .conn_interval_max = EXT_CONN_INT_DEF_1M,
  42. .conn_latency = 0,
  43. .sup_timeout = 600,
  44. .min_ce_len = 0,
  45. .max_ce_len = 0,
  46. };
  47. const static tHCI_ExtConnParams ext_conn_params_2m_phy = {
  48. .scan_interval = 0x40,
  49. .scan_window = 0x40,
  50. .conn_interval_min = EXT_CONN_INT_DEF_2M,
  51. .conn_interval_max = EXT_CONN_INT_DEF_2M,
  52. .conn_latency = 0,
  53. .sup_timeout = 600,
  54. .min_ce_len = 0,
  55. .max_ce_len = 0,
  56. };
  57. const static tHCI_ExtConnParams ext_conn_params_coded_phy = {
  58. .scan_interval = 0x40,
  59. .scan_window = 0x40,
  60. .conn_interval_min = EXT_CONN_INT_DEF_CODED,
  61. .conn_interval_max = EXT_CONN_INT_DEF_CODED,
  62. .conn_latency = 0,
  63. .sup_timeout = 600,
  64. .min_ce_len = 0,
  65. .max_ce_len = 0,
  66. };
  67. #define BLE_PHY_NO_PREF 0
  68. #define BLE_PHY_PREF_MASK ((1 << 2) | (1 << 1) | (1 << 0))
  69. #endif // #if (BLE_50_FEATURE_SUPPORT == TRUE)
  70. static BOOLEAN l2cble_start_conn_update (tL2C_LCB *p_lcb);
  71. extern int64_t esp_system_get_time(void);
  72. /*******************************************************************************
  73. **
  74. ** Function L2CA_CancelBleConnectReq
  75. **
  76. ** Description Cancel a pending connection attempt to a BLE device.
  77. **
  78. ** Parameters: BD Address of remote
  79. **
  80. ** Return value: TRUE if connection was cancelled
  81. **
  82. *******************************************************************************/
  83. BOOLEAN L2CA_CancelBleConnectReq (BD_ADDR rem_bda)
  84. {
  85. tL2C_LCB *p_lcb;
  86. /* There can be only one BLE connection request outstanding at a time */
  87. if (btm_ble_get_conn_st() == BLE_CONN_IDLE) {
  88. L2CAP_TRACE_WARNING ("L2CA_CancelBleConnectReq - no connection pending");
  89. return (FALSE);
  90. }
  91. if (memcmp (rem_bda, l2cb.ble_connecting_bda, BD_ADDR_LEN)) {
  92. L2CAP_TRACE_WARNING ("L2CA_CancelBleConnectReq - different BDA Connecting: %08x%04x Cancel: %08x%04x",
  93. (l2cb.ble_connecting_bda[0] << 24) + (l2cb.ble_connecting_bda[1] << 16) + (l2cb.ble_connecting_bda[2] << 8) + l2cb.ble_connecting_bda[3],
  94. (l2cb.ble_connecting_bda[4] << 8) + l2cb.ble_connecting_bda[5],
  95. (rem_bda[0] << 24) + (rem_bda[1] << 16) + (rem_bda[2] << 8) + rem_bda[3], (rem_bda[4] << 8) + rem_bda[5]);
  96. return (FALSE);
  97. }
  98. if (btsnd_hcic_ble_create_conn_cancel()) {
  99. p_lcb = l2cu_find_lcb_by_bd_addr(rem_bda, BT_TRANSPORT_LE);
  100. /* Do not remove lcb if an LE link is already up as a peripheral */
  101. if (p_lcb != NULL &&
  102. !(p_lcb->link_role == HCI_ROLE_SLAVE && BTM_ACL_IS_CONNECTED(rem_bda))) {
  103. p_lcb->disc_reason = L2CAP_CONN_CANCEL;
  104. l2cu_release_lcb (p_lcb);
  105. }
  106. /* update state to be cancel, wait for connection cancel complete */
  107. btm_ble_set_conn_st (BLE_CONN_CANCEL);
  108. return (TRUE);
  109. } else {
  110. return (FALSE);
  111. }
  112. }
  113. /*******************************************************************************
  114. **
  115. ** Function L2CA_UpdateBleConnParams
  116. **
  117. ** Description Update BLE connection parameters.
  118. **
  119. ** Parameters: BD Address of remote
  120. **
  121. ** Return value: TRUE if update started
  122. **
  123. *******************************************************************************/
  124. BOOLEAN L2CA_UpdateBleConnParams (BD_ADDR rem_bda, UINT16 min_int, UINT16 max_int,
  125. UINT16 latency, UINT16 timeout)
  126. {
  127. tL2C_LCB *p_lcb;
  128. tACL_CONN *p_acl_cb = btm_bda_to_acl(rem_bda, BT_TRANSPORT_LE);
  129. UINT8 status = HCI_SUCCESS;
  130. BOOLEAN need_cb = false;
  131. /* See if we have a link control block for the remote device */
  132. p_lcb = l2cu_find_lcb_by_bd_addr (rem_bda, BT_TRANSPORT_LE);
  133. /* If we don't have one, create one and accept the connection. */
  134. if (!p_lcb || !p_acl_cb) {
  135. L2CAP_TRACE_WARNING ("L2CA_UpdateBleConnParams - unknown BD_ADDR %08x%04x",
  136. (rem_bda[0] << 24) + (rem_bda[1] << 16) + (rem_bda[2] << 8) + rem_bda[3],
  137. (rem_bda[4] << 8) + rem_bda[5]);
  138. return (FALSE);
  139. }
  140. if (p_lcb->transport != BT_TRANSPORT_LE) {
  141. L2CAP_TRACE_WARNING ("L2CA_UpdateBleConnParams - BD_ADDR %08x%04x not LE",
  142. (rem_bda[0] << 24) + (rem_bda[1] << 16) + (rem_bda[2] << 8) + rem_bda[3],
  143. (rem_bda[4] << 8) + rem_bda[5]);
  144. return (FALSE);
  145. }
  146. /* Check whether the request conn params is already set */
  147. if ((max_int == p_lcb->current_used_conn_interval) && (latency == p_lcb->current_used_conn_latency) &&
  148. (timeout == p_lcb->current_used_conn_timeout)) {
  149. status = HCI_SUCCESS;
  150. need_cb = true;
  151. L2CAP_TRACE_WARNING("%s connection parameter already set", __func__);
  152. }
  153. if (p_lcb->conn_update_mask & L2C_BLE_UPDATE_PARAM_FULL){
  154. status = HCI_ERR_ILLEGAL_COMMAND;
  155. need_cb = true;
  156. L2CAP_TRACE_ERROR("There are two connection parameter requests that are being updated, please try later ");
  157. }
  158. if ((need_cb == TRUE) && (conn_param_update_cb.update_conn_param_cb != NULL)) {
  159. tBTM_LE_UPDATE_CONN_PRAMS update_param;
  160. update_param.max_conn_int = max_int;
  161. update_param.min_conn_int = min_int;
  162. update_param.conn_int = p_lcb->current_used_conn_interval;
  163. update_param.slave_latency = p_lcb->current_used_conn_latency;
  164. update_param.supervision_tout = p_lcb->current_used_conn_timeout;
  165. (conn_param_update_cb.update_conn_param_cb)(status, p_lcb->remote_bd_addr, &update_param);
  166. return (status == HCI_SUCCESS);
  167. }
  168. p_lcb->waiting_update_conn_min_interval = min_int;
  169. p_lcb->waiting_update_conn_max_interval = max_int;
  170. p_lcb->waiting_update_conn_latency = latency;
  171. p_lcb->waiting_update_conn_timeout = timeout;
  172. p_lcb->conn_update_mask |= L2C_BLE_NEW_CONN_PARAM;
  173. if(l2cble_start_conn_update(p_lcb) == TRUE) {
  174. UINT32 time = CalConnectParamTimeout(p_lcb);
  175. btu_start_timer(&p_lcb->upda_con_timer, BTU_TTYPE_L2CAP_UPDA_CONN_PARAMS, time);
  176. }
  177. return (TRUE);
  178. }
  179. /*******************************************************************************
  180. **
  181. ** Function L2CA_EnableUpdateBleConnParams
  182. **
  183. ** Description Enable or disable update based on the request from the peer
  184. **
  185. ** Parameters: BD Address of remote
  186. **
  187. ** Return value: TRUE if update started
  188. **
  189. *******************************************************************************/
  190. BOOLEAN L2CA_EnableUpdateBleConnParams (BD_ADDR rem_bda, BOOLEAN enable)
  191. {
  192. tL2C_LCB *p_lcb;
  193. /* See if we have a link control block for the remote device */
  194. p_lcb = l2cu_find_lcb_by_bd_addr (rem_bda, BT_TRANSPORT_LE);
  195. if (!p_lcb) {
  196. L2CAP_TRACE_WARNING ("L2CA_EnableUpdateBleConnParams - unknown BD_ADDR %08x%04x",
  197. (rem_bda[0] << 24) + (rem_bda[1] << 16) + (rem_bda[2] << 8) + rem_bda[3],
  198. (rem_bda[4] << 8) + rem_bda[5]);
  199. return (FALSE);
  200. }
  201. L2CAP_TRACE_API ("%s - BD_ADDR %08x%04x enable %d current upd state 0x%02x", __FUNCTION__,
  202. (rem_bda[0] << 24) + (rem_bda[1] << 16) + (rem_bda[2] << 8) + rem_bda[3],
  203. (rem_bda[4] << 8) + rem_bda[5], enable, p_lcb->conn_update_mask);
  204. if (p_lcb->transport != BT_TRANSPORT_LE) {
  205. L2CAP_TRACE_WARNING ("%s - BD_ADDR %08x%04x not LE (link role %d)", __FUNCTION__,
  206. (rem_bda[0] << 24) + (rem_bda[1] << 16) + (rem_bda[2] << 8) + rem_bda[3],
  207. (rem_bda[4] << 8) + rem_bda[5], p_lcb->link_role);
  208. return (FALSE);
  209. }
  210. if (p_lcb->current_used_conn_interval <= BTM_BLE_CONN_INT_MAX_DEF && (p_lcb->conn_update_mask & L2C_BLE_CONN_UPDATE_DISABLE) == 0){
  211. return (FALSE);
  212. }
  213. bool is_disable = (p_lcb->conn_update_mask & L2C_BLE_CONN_UPDATE_DISABLE);
  214. if(l2cu_plcb_active_count() >1 && !(enable && is_disable)) {
  215. return FALSE;
  216. }
  217. if (enable) {
  218. p_lcb->conn_update_mask &= ~L2C_BLE_CONN_UPDATE_DISABLE;
  219. } else {
  220. p_lcb->conn_update_mask |= L2C_BLE_CONN_UPDATE_DISABLE;
  221. }
  222. if (l2cble_start_conn_update(p_lcb) == TRUE) {
  223. UINT32 time = CalConnectParamTimeout(p_lcb);
  224. btu_start_timer(&p_lcb->upda_con_timer, BTU_TTYPE_L2CAP_UPDA_CONN_PARAMS, time);
  225. }
  226. return (TRUE);
  227. }
  228. /*******************************************************************************
  229. **
  230. ** Function L2CA_GetBleConnRole
  231. **
  232. ** Description This function returns the connection role.
  233. **
  234. ** Returns link role.
  235. **
  236. *******************************************************************************/
  237. UINT8 L2CA_GetBleConnRole (BD_ADDR bd_addr)
  238. {
  239. UINT8 role = HCI_ROLE_UNKNOWN;
  240. tL2C_LCB *p_lcb;
  241. if ((p_lcb = l2cu_find_lcb_by_bd_addr (bd_addr, BT_TRANSPORT_LE)) != NULL) {
  242. role = p_lcb->link_role;
  243. }
  244. return role;
  245. }
  246. /*******************************************************************************
  247. **
  248. ** Function l2cble_notify_le_connection
  249. **
  250. ** Description This function notifiy the l2cap connection to the app layer
  251. **
  252. ** Returns none
  253. **
  254. *******************************************************************************/
  255. void l2cble_notify_le_connection (BD_ADDR bda)
  256. {
  257. tL2C_LCB *p_lcb = l2cu_find_lcb_by_bd_addr (bda, BT_TRANSPORT_LE);
  258. tACL_CONN *p_acl = btm_bda_to_acl(bda, BT_TRANSPORT_LE) ;
  259. if (p_lcb != NULL && p_acl != NULL && p_lcb->link_state != LST_CONNECTED) {
  260. if(p_acl->link_role == HCI_ROLE_SLAVE) {
  261. //clear p_cb->state, controller will stop adv when ble connected.
  262. tBTM_BLE_INQ_CB *p_cb = &btm_cb.ble_ctr_cb.inq_var;
  263. if(p_cb) {
  264. p_cb->adv_mode = BTM_BLE_ADV_DISABLE;
  265. p_cb->state &= ~BTM_BLE_ADVERTISING;
  266. }
  267. }
  268. /* update link status */
  269. btm_establish_continue(p_acl);
  270. /* update l2cap link status and send callback */
  271. p_lcb->link_state = LST_CONNECTED;
  272. l2cu_process_fixed_chnl_resp (p_lcb);
  273. }
  274. }
  275. /*******************************************************************************
  276. **
  277. ** Function l2cble_scanner_conn_comp
  278. **
  279. ** Description This function is called when an HCI Connection Complete
  280. ** event is received while we are a scanner (so we are master).
  281. **
  282. ** Returns void
  283. **
  284. *******************************************************************************/
  285. void l2cble_scanner_conn_comp (UINT16 handle, BD_ADDR bda, tBLE_ADDR_TYPE type,
  286. UINT16 conn_interval, UINT16 conn_latency, UINT16 conn_timeout)
  287. {
  288. tL2C_LCB *p_lcb;
  289. tBTM_SEC_DEV_REC *p_dev_rec = btm_find_or_alloc_dev (bda);
  290. L2CAP_TRACE_DEBUG ("l2cble_scanner_conn_comp: HANDLE=%d addr_type=%d conn_interval=%d slave_latency=%d supervision_tout=%d",
  291. handle, type, conn_interval, conn_latency, conn_timeout);
  292. l2cb.is_ble_connecting = FALSE;
  293. /* See if we have a link control block for the remote device */
  294. p_lcb = l2cu_find_lcb_by_bd_addr (bda, BT_TRANSPORT_LE);
  295. /* If we don't have one, create one. this is auto connection complete. */
  296. if (!p_lcb) {
  297. p_lcb = l2cu_allocate_lcb (bda, FALSE, BT_TRANSPORT_LE);
  298. if (!p_lcb) {
  299. #if (SMP_INCLUDED == TRUE)
  300. btm_sec_disconnect (handle, HCI_ERR_NO_CONNECTION);
  301. L2CAP_TRACE_ERROR ("l2cble_scanner_conn_comp - failed to allocate LCB");
  302. #endif ///SMP_INCLUDED == TRUE
  303. return;
  304. } else {
  305. if (!l2cu_initialize_fixed_ccb (p_lcb, L2CAP_ATT_CID, &l2cb.fixed_reg[L2CAP_ATT_CID - L2CAP_FIRST_FIXED_CHNL].fixed_chnl_opts)) {
  306. #if (SMP_INCLUDED == TRUE)
  307. btm_sec_disconnect (handle, HCI_ERR_NO_CONNECTION);
  308. L2CAP_TRACE_WARNING ("l2cble_scanner_conn_comp - LCB but no CCB");
  309. #endif ///SMP_INCLUDED == TRUE
  310. return ;
  311. }
  312. }
  313. } else if (p_lcb->link_state != LST_CONNECTING) {
  314. L2CAP_TRACE_ERROR ("L2CAP got BLE scanner conn_comp in bad state: %d", p_lcb->link_state);
  315. return;
  316. }
  317. btu_stop_timer(&p_lcb->timer_entry);
  318. /* Save the handle */
  319. p_lcb->handle = handle;
  320. /* Connected OK. Change state to connected, we were scanning so we are master */
  321. p_lcb->link_role = HCI_ROLE_MASTER;
  322. p_lcb->transport = BT_TRANSPORT_LE;
  323. /* update link parameter, set slave link as non-spec default upon link up */
  324. p_lcb->waiting_update_conn_min_interval = p_lcb->waiting_update_conn_max_interval = p_lcb->current_used_conn_interval = conn_interval;
  325. p_lcb->waiting_update_conn_timeout = p_lcb->current_used_conn_timeout = conn_timeout;
  326. p_lcb->waiting_update_conn_latency = p_lcb->current_used_conn_latency = conn_latency;
  327. p_lcb->conn_update_mask = L2C_BLE_NOT_DEFAULT_PARAM;
  328. p_lcb->updating_param_flag = false;
  329. /* If there are any preferred connection parameters, set them now */
  330. if ( (p_dev_rec->conn_params.min_conn_int >= BTM_BLE_CONN_INT_MIN ) &&
  331. (p_dev_rec->conn_params.min_conn_int <= BTM_BLE_CONN_INT_MAX ) &&
  332. (p_dev_rec->conn_params.max_conn_int >= BTM_BLE_CONN_INT_MIN ) &&
  333. (p_dev_rec->conn_params.max_conn_int <= BTM_BLE_CONN_INT_MAX ) &&
  334. (p_dev_rec->conn_params.slave_latency <= BTM_BLE_CONN_LATENCY_MAX ) &&
  335. (p_dev_rec->conn_params.supervision_tout >= BTM_BLE_CONN_SUP_TOUT_MIN) &&
  336. (p_dev_rec->conn_params.supervision_tout <= BTM_BLE_CONN_SUP_TOUT_MAX) &&
  337. ((conn_interval < p_dev_rec->conn_params.min_conn_int &&
  338. p_dev_rec->conn_params.min_conn_int != BTM_BLE_CONN_PARAM_UNDEF) ||
  339. (conn_interval > p_dev_rec->conn_params.max_conn_int) ||
  340. (conn_latency > p_dev_rec->conn_params.slave_latency) ||
  341. (conn_timeout > p_dev_rec->conn_params.supervision_tout))) {
  342. L2CAP_TRACE_ERROR ("upd_ll_conn_params: HANDLE=%d min_conn_int=%d max_conn_int=%d slave_latency=%d supervision_tout=%d",
  343. handle, p_dev_rec->conn_params.min_conn_int, p_dev_rec->conn_params.max_conn_int,
  344. p_dev_rec->conn_params.slave_latency, p_dev_rec->conn_params.supervision_tout);
  345. p_lcb->waiting_update_conn_min_interval = p_dev_rec->conn_params.min_conn_int;
  346. p_lcb->waiting_update_conn_max_interval = p_dev_rec->conn_params.max_conn_int;
  347. p_lcb->waiting_update_conn_timeout = p_dev_rec->conn_params.supervision_tout;
  348. p_lcb->waiting_update_conn_latency = p_dev_rec->conn_params.slave_latency;
  349. btsnd_hcic_ble_upd_ll_conn_params (handle,
  350. p_dev_rec->conn_params.min_conn_int,
  351. p_dev_rec->conn_params.max_conn_int,
  352. p_dev_rec->conn_params.slave_latency,
  353. p_dev_rec->conn_params.supervision_tout,
  354. BLE_CE_LEN_MIN, BLE_CE_LEN_MIN);
  355. }
  356. /* Tell BTM Acl management about the link */
  357. btm_acl_created (bda, NULL, p_dev_rec->sec_bd_name, handle, p_lcb->link_role, BT_TRANSPORT_LE);
  358. p_lcb->peer_chnl_mask[0] = L2CAP_FIXED_CHNL_ATT_BIT | L2CAP_FIXED_CHNL_BLE_SIG_BIT | L2CAP_FIXED_CHNL_SMP_BIT;
  359. btm_ble_set_conn_st(BLE_CONN_IDLE);
  360. #if BLE_PRIVACY_SPT == TRUE
  361. btm_ble_disable_resolving_list(BTM_BLE_RL_INIT, TRUE);
  362. #endif
  363. }
  364. /*******************************************************************************
  365. **
  366. ** Function l2cble_advertiser_conn_comp
  367. **
  368. ** Description This function is called when an HCI Connection Complete
  369. ** event is received while we are an advertiser (so we are slave).
  370. **
  371. ** Returns void
  372. **
  373. *******************************************************************************/
  374. void l2cble_advertiser_conn_comp (UINT16 handle, BD_ADDR bda, tBLE_ADDR_TYPE type,
  375. UINT16 conn_interval, UINT16 conn_latency, UINT16 conn_timeout)
  376. {
  377. tL2C_LCB *p_lcb;
  378. tBTM_SEC_DEV_REC *p_dev_rec;
  379. UNUSED(type);
  380. UNUSED(conn_interval);
  381. UNUSED(conn_latency);
  382. UNUSED(conn_timeout);
  383. /* See if we have a link control block for the remote device */
  384. p_lcb = l2cu_find_lcb_by_bd_addr (bda, BT_TRANSPORT_LE);
  385. /* If we don't have one, create one and accept the connection. */
  386. if (!p_lcb) {
  387. p_lcb = l2cu_allocate_lcb (bda, FALSE, BT_TRANSPORT_LE);
  388. if (!p_lcb) {
  389. #if (SMP_INCLUDED == TRUE)
  390. btm_sec_disconnect (handle, HCI_ERR_NO_CONNECTION);
  391. #endif ///SMP_INCLUDED == TRUE
  392. L2CAP_TRACE_ERROR ("l2cble_advertiser_conn_comp - failed to allocate LCB");
  393. return;
  394. } else {
  395. if (!l2cu_initialize_fixed_ccb (p_lcb, L2CAP_ATT_CID, &l2cb.fixed_reg[L2CAP_ATT_CID - L2CAP_FIRST_FIXED_CHNL].fixed_chnl_opts)) {
  396. #if (SMP_INCLUDED == TRUE)
  397. btm_sec_disconnect (handle, HCI_ERR_NO_CONNECTION);
  398. #endif ///SMP_INCLUDED == TRUE
  399. L2CAP_TRACE_WARNING ("l2cble_scanner_conn_comp - LCB but no CCB");
  400. return ;
  401. }
  402. }
  403. }
  404. /* Save the handle */
  405. p_lcb->handle = handle;
  406. /* Connected OK. Change state to connected, we were advertising, so we are slave */
  407. p_lcb->link_role = HCI_ROLE_SLAVE;
  408. p_lcb->transport = BT_TRANSPORT_LE;
  409. /* update link parameter, set slave link as non-spec default upon link up */
  410. p_lcb->waiting_update_conn_min_interval = p_lcb->waiting_update_conn_max_interval = p_lcb->current_used_conn_interval = conn_interval;
  411. p_lcb->waiting_update_conn_timeout = p_lcb->current_used_conn_timeout = conn_timeout;
  412. p_lcb->waiting_update_conn_latency = p_lcb->current_used_conn_latency = conn_latency;
  413. p_lcb->conn_update_mask = L2C_BLE_NOT_DEFAULT_PARAM;
  414. p_lcb->updating_param_flag = false;
  415. /* Tell BTM Acl management about the link */
  416. p_dev_rec = btm_find_or_alloc_dev (bda);
  417. btm_acl_created (bda, NULL, p_dev_rec->sec_bd_name, handle, p_lcb->link_role, BT_TRANSPORT_LE);
  418. #if BLE_PRIVACY_SPT == TRUE
  419. btm_ble_disable_resolving_list(BTM_BLE_RL_ADV, TRUE);
  420. #endif
  421. p_lcb->peer_chnl_mask[0] = L2CAP_FIXED_CHNL_ATT_BIT | L2CAP_FIXED_CHNL_BLE_SIG_BIT | L2CAP_FIXED_CHNL_SMP_BIT;
  422. if (!HCI_LE_SLAVE_INIT_FEAT_EXC_SUPPORTED(controller_get_interface()->get_features_ble()->as_array)) {
  423. p_lcb->link_state = LST_CONNECTED;
  424. l2cu_process_fixed_chnl_resp (p_lcb);
  425. }
  426. /* when adv and initiating are both active, cancel the direct connection */
  427. if (l2cb.is_ble_connecting && memcmp(bda, l2cb.ble_connecting_bda, BD_ADDR_LEN) == 0) {
  428. L2CA_CancelBleConnectReq(bda);
  429. }
  430. }
  431. /*******************************************************************************
  432. **
  433. ** Function l2cble_conn_comp
  434. **
  435. ** Description This function is called when an HCI Connection Complete
  436. ** event is received.
  437. **
  438. ** Returns void
  439. **
  440. *******************************************************************************/
  441. void l2cble_conn_comp(UINT16 handle, UINT8 role, BD_ADDR bda, tBLE_ADDR_TYPE type,
  442. UINT16 conn_interval, UINT16 conn_latency, UINT16 conn_timeout)
  443. {
  444. btm_ble_update_link_topology_mask(role, TRUE);
  445. if (role == HCI_ROLE_MASTER) {
  446. l2cble_scanner_conn_comp(handle, bda, type, conn_interval, conn_latency, conn_timeout);
  447. } else {
  448. l2cble_advertiser_conn_comp(handle, bda, type, conn_interval, conn_latency, conn_timeout);
  449. }
  450. }
  451. /*******************************************************************************
  452. **
  453. ** Function l2cble_start_conn_update
  454. **
  455. ** Description start BLE connection parameter update process based on status
  456. **
  457. ** Parameters: lcb : l2cap link control block
  458. **
  459. ** Return value: true if successfully sending the request to peer device, else false.
  460. **
  461. *******************************************************************************/
  462. static BOOLEAN l2cble_start_conn_update (tL2C_LCB *p_lcb)
  463. {
  464. UINT16 min_conn_int, max_conn_int, slave_latency, supervision_tout;
  465. #if (defined BLE_LLT_INCLUDED) && (BLE_LLT_INCLUDED == TRUE) && (BLE_SLAVE_UPD_CONN_PARAMS == TRUE)
  466. tACL_CONN *p_acl_cb = btm_bda_to_acl(p_lcb->remote_bd_addr, BT_TRANSPORT_LE);
  467. #endif /* defined BLE_LLT_INCLUDED) && (BLE_LLT_INCLUDED == TRUE) && (BLE_SLAVE_UPD_CONN_PARAMS == TRUE */
  468. if (p_lcb->conn_update_mask & L2C_BLE_UPDATE_PENDING) {
  469. L2CAP_TRACE_WARNING("%s, the last connection update command still pending.", __func__);
  470. p_lcb->conn_update_mask |= L2C_BLE_UPDATE_PARAM_FULL;
  471. return FALSE;
  472. }
  473. if (p_lcb->conn_update_mask & L2C_BLE_CONN_UPDATE_DISABLE) {
  474. /* application requests to disable parameters update.
  475. If parameters are already updated, lets set them
  476. up to what has been requested during connection establishement */
  477. if (p_lcb->conn_update_mask & L2C_BLE_NOT_DEFAULT_PARAM &&
  478. /* current connection interval is greater than default min */
  479. p_lcb->current_used_conn_interval > BTM_BLE_CONN_INT_MAX_DEF) {
  480. /* use 6 * 1.25 = 7.5 ms as fast connection parameter, 0 slave latency */
  481. min_conn_int = max_conn_int = BTM_BLE_CONN_INT_MIN;
  482. slave_latency = BTM_BLE_CONN_SLAVE_LATENCY_DEF;
  483. supervision_tout = BTM_BLE_CONN_TIMEOUT_DEF;
  484. /* if both side 4.1, or we are master device, send HCI command */
  485. if (p_lcb->link_role == HCI_ROLE_MASTER
  486. #if (defined BLE_LLT_INCLUDED) && (BLE_LLT_INCLUDED == TRUE) && (BLE_SLAVE_UPD_CONN_PARAMS == TRUE)
  487. || (HCI_LE_CONN_PARAM_REQ_SUPPORTED(controller_get_interface()->get_features_ble()->as_array) &&
  488. HCI_LE_CONN_PARAM_REQ_SUPPORTED(p_acl_cb->peer_le_features))
  489. #endif
  490. ) {
  491. btsnd_hcic_ble_upd_ll_conn_params(p_lcb->handle, min_conn_int, max_conn_int,
  492. slave_latency, supervision_tout, BLE_CE_LEN_MIN, BLE_CE_LEN_MIN);
  493. } else {
  494. l2cu_send_peer_ble_par_req (p_lcb, min_conn_int, max_conn_int, slave_latency, supervision_tout);
  495. }
  496. //cache save
  497. p_lcb->updating_conn_min_interval = min_conn_int;
  498. p_lcb->updating_conn_max_interval = max_conn_int;
  499. p_lcb->updating_param_flag = true;
  500. p_lcb->conn_update_mask |= L2C_BLE_UPDATE_PENDING;
  501. p_lcb->conn_update_mask &= ~L2C_BLE_NOT_DEFAULT_PARAM;
  502. p_lcb->conn_update_mask |= L2C_BLE_NEW_CONN_PARAM;
  503. return TRUE;
  504. }else {
  505. return FALSE;
  506. }
  507. } else {
  508. /* application allows to do update, if we were delaying one do it now */
  509. if (p_lcb->conn_update_mask & L2C_BLE_NEW_CONN_PARAM) {
  510. /* if both side 4.1, or we are master device, send HCI command */
  511. if (p_lcb->link_role == HCI_ROLE_MASTER
  512. #if (defined BLE_LLT_INCLUDED) && (BLE_LLT_INCLUDED == TRUE) && (BLE_SLAVE_UPD_CONN_PARAMS == TRUE)
  513. || (HCI_LE_CONN_PARAM_REQ_SUPPORTED(controller_get_interface()->get_features_ble()->as_array) &&
  514. HCI_LE_CONN_PARAM_REQ_SUPPORTED(p_acl_cb->peer_le_features))
  515. #endif
  516. ) {
  517. btsnd_hcic_ble_upd_ll_conn_params(p_lcb->handle, p_lcb->waiting_update_conn_min_interval,
  518. p_lcb->waiting_update_conn_max_interval, p_lcb->waiting_update_conn_latency, p_lcb->waiting_update_conn_timeout, BLE_CE_LEN_MIN, BLE_CE_LEN_MIN);
  519. } else {
  520. l2cu_send_peer_ble_par_req (p_lcb, p_lcb->waiting_update_conn_min_interval, p_lcb->waiting_update_conn_max_interval,
  521. p_lcb->waiting_update_conn_latency, p_lcb->waiting_update_conn_timeout);
  522. }
  523. //cache save
  524. p_lcb->updating_conn_min_interval = p_lcb->waiting_update_conn_min_interval;
  525. p_lcb->updating_conn_max_interval = p_lcb->waiting_update_conn_max_interval;
  526. p_lcb->updating_param_flag = true;
  527. p_lcb->conn_update_mask |= L2C_BLE_UPDATE_PENDING;
  528. p_lcb->conn_update_mask &= ~L2C_BLE_NEW_CONN_PARAM;
  529. p_lcb->conn_update_mask |= L2C_BLE_NOT_DEFAULT_PARAM;
  530. return TRUE;
  531. } else {
  532. return FALSE;
  533. }
  534. }
  535. }
  536. /*******************************************************************************
  537. **
  538. ** Function l2cble_process_conn_update_evt
  539. **
  540. ** Description This function enables the connection update request from remote
  541. ** after a successful connection update response is received.
  542. **
  543. ** Returns void
  544. **
  545. *******************************************************************************/
  546. void l2cble_process_conn_update_evt (UINT16 handle, UINT8 status, UINT16 conn_interval,
  547. UINT16 conn_latency, UINT16 conn_timeout)
  548. {
  549. tL2C_LCB *p_lcb;
  550. /* See if we have a link control block for the remote device */
  551. p_lcb = l2cu_find_lcb_by_handle(handle);
  552. if (!p_lcb) {
  553. L2CAP_TRACE_WARNING("le con upd: inv hdl=%d", handle);
  554. return;
  555. }
  556. if (status == HCI_SUCCESS){
  557. p_lcb->current_used_conn_interval = conn_interval;
  558. p_lcb->current_used_conn_latency = conn_latency;
  559. p_lcb->current_used_conn_timeout = conn_timeout;
  560. }else{
  561. L2CAP_TRACE_WARNING("le con upd: err_stat=0x%x", status);
  562. }
  563. p_lcb->conn_update_mask &= ~L2C_BLE_UPDATE_PENDING;
  564. p_lcb->conn_update_mask &= ~L2C_BLE_UPDATE_PARAM_FULL;
  565. btu_stop_timer(&p_lcb->upda_con_timer);
  566. if (conn_param_update_cb.update_conn_param_cb != NULL) {
  567. l2c_send_update_conn_params_cb(p_lcb, status);
  568. }
  569. if (l2cble_start_conn_update(p_lcb) == TRUE) {
  570. UINT32 time = CalConnectParamTimeout(p_lcb);
  571. btu_start_timer(&p_lcb->upda_con_timer, BTU_TTYPE_L2CAP_UPDA_CONN_PARAMS, time);
  572. }
  573. btu_stop_timer (&p_lcb->timer_entry);
  574. L2CAP_TRACE_DEBUG("le con upd: conn_update_mask=%d", p_lcb->conn_update_mask);
  575. }
  576. /*******************************************************************************
  577. **
  578. ** Function l2cble_get_conn_param_format_err_from_contoller
  579. **
  580. ** Description This function is called when host get illegal connection paramrters
  581. ** format status from controller
  582. **
  583. ** Returns void
  584. **
  585. *******************************************************************************/
  586. void l2cble_get_conn_param_format_err_from_contoller (UINT8 status, UINT16 handle)
  587. {
  588. tL2C_LCB *p_lcb;
  589. /* See if we have a link control block for the remote device */
  590. p_lcb = l2cu_find_lcb_by_handle(handle);
  591. if (!p_lcb) {
  592. L2CAP_TRACE_ERROR("%s: Invalid handle: %d", __FUNCTION__, handle);
  593. return;
  594. }
  595. p_lcb->conn_update_mask &= ~L2C_BLE_UPDATE_PENDING;
  596. btu_stop_timer (&p_lcb->upda_con_timer);
  597. if (conn_param_update_cb.update_conn_param_cb != NULL) {
  598. l2c_send_update_conn_params_cb(p_lcb, status);
  599. }
  600. if ((p_lcb->conn_update_mask & L2C_BLE_UPDATE_PARAM_FULL) != 0){
  601. p_lcb->conn_update_mask &= ~L2C_BLE_UPDATE_PARAM_FULL;
  602. if (l2cble_start_conn_update(p_lcb) == TRUE) {
  603. UINT32 time = CalConnectParamTimeout(p_lcb);
  604. btu_start_timer(&p_lcb->upda_con_timer, BTU_TTYPE_L2CAP_UPDA_CONN_PARAMS, time);
  605. }
  606. }
  607. }
  608. /*******************************************************************************
  609. **
  610. ** Function l2cble_process_sig_cmd
  611. **
  612. ** Description This function is called when a signalling packet is received
  613. ** on the BLE signalling CID
  614. **
  615. ** Returns void
  616. **
  617. *******************************************************************************/
  618. void l2cble_process_sig_cmd (tL2C_LCB *p_lcb, UINT8 *p, UINT16 pkt_len)
  619. {
  620. UINT8 *p_pkt_end;
  621. UINT8 cmd_code, id;
  622. UINT16 cmd_len;
  623. UINT16 min_interval, max_interval, latency, timeout;
  624. p_pkt_end = p + pkt_len;
  625. STREAM_TO_UINT8 (cmd_code, p);
  626. STREAM_TO_UINT8 (id, p);
  627. STREAM_TO_UINT16 (cmd_len, p);
  628. /* Check command length does not exceed packet length */
  629. if ((p + cmd_len) > p_pkt_end) {
  630. L2CAP_TRACE_WARNING ("L2CAP - LE - format error, pkt_len: %d cmd_len: %d code: %d", pkt_len, cmd_len, cmd_code);
  631. return;
  632. }
  633. switch (cmd_code) {
  634. case L2CAP_CMD_REJECT:
  635. case L2CAP_CMD_ECHO_RSP:
  636. case L2CAP_CMD_INFO_RSP:
  637. p += 2;
  638. break;
  639. case L2CAP_CMD_ECHO_REQ:
  640. case L2CAP_CMD_INFO_REQ:
  641. l2cu_send_peer_cmd_reject (p_lcb, L2CAP_CMD_REJ_NOT_UNDERSTOOD, id, 0, 0);
  642. break;
  643. case L2CAP_CMD_BLE_UPDATE_REQ:
  644. STREAM_TO_UINT16 (min_interval, p); /* 0x0006 - 0x0C80 */
  645. STREAM_TO_UINT16 (max_interval, p); /* 0x0006 - 0x0C80 */
  646. STREAM_TO_UINT16 (latency, p); /* 0x0000 - 0x03E8 */
  647. STREAM_TO_UINT16 (timeout, p); /* 0x000A - 0x0C80 */
  648. /* If we are a master, the slave wants to update the parameters */
  649. if (p_lcb->link_role == HCI_ROLE_MASTER) {
  650. if (min_interval < BTM_BLE_CONN_INT_MIN || min_interval > BTM_BLE_CONN_INT_MAX ||
  651. max_interval < BTM_BLE_CONN_INT_MIN || max_interval > BTM_BLE_CONN_INT_MAX ||
  652. latency > BTM_BLE_CONN_LATENCY_MAX ||
  653. timeout < BTM_BLE_CONN_SUP_TOUT_MIN || timeout > BTM_BLE_CONN_SUP_TOUT_MAX ||
  654. /* The supervision_timeout parameter defines the link supervision timeout for the connection.
  655. The supervision_timeout in milliseconds shall be large than (1 + latency) * max_interval * 2,
  656. where max_interval is given in milliseconds. (See [Vol 6] Part B, Section 4.5.2).
  657. supervision_timeout (mult of 10ms); conn_interval (mult of 1.25ms)
  658. (max_interval * 1.25 * 2) replaced by ((max_interval * 5) >> 1).
  659. */
  660. ((timeout * 10) < ((1 + latency) *((max_interval * 5) >> 1))) ||
  661. max_interval < min_interval) {
  662. l2cu_send_peer_ble_par_rsp (p_lcb, L2CAP_CFG_UNACCEPTABLE_PARAMS, id);
  663. L2CAP_TRACE_ERROR("slave connection parameters update failed, the parameters are out of range");
  664. } else {
  665. l2cu_send_peer_ble_par_rsp (p_lcb, L2CAP_CFG_OK, id);
  666. p_lcb->waiting_update_conn_min_interval = min_interval;
  667. p_lcb->waiting_update_conn_max_interval = max_interval;
  668. p_lcb->waiting_update_conn_latency = latency;
  669. p_lcb->waiting_update_conn_timeout = timeout;
  670. p_lcb->conn_update_mask |= L2C_BLE_NEW_CONN_PARAM;
  671. if (l2cble_start_conn_update(p_lcb) == TRUE) {
  672. UINT32 time = CalConnectParamTimeout(p_lcb);
  673. btu_start_timer(&p_lcb->upda_con_timer, BTU_TTYPE_L2CAP_UPDA_CONN_PARAMS, time);
  674. }
  675. }
  676. } else {
  677. l2cu_send_peer_cmd_reject (p_lcb, L2CAP_CMD_REJ_NOT_UNDERSTOOD, id, 0, 0);
  678. }
  679. break;
  680. case L2CAP_CMD_BLE_UPDATE_RSP: {
  681. UINT16 result = 0;
  682. STREAM_TO_UINT16(result, p); //result = 0 connection param accepted, result = 1 connection param rejected.
  683. UINT8 status = (result == 0) ? HCI_SUCCESS : HCI_ERR_PARAM_OUT_OF_RANGE;
  684. if (status != HCI_SUCCESS) {
  685. btu_stop_timer(&p_lcb->upda_con_timer);
  686. p_lcb->conn_update_mask &= ~L2C_BLE_UPDATE_PENDING;
  687. p_lcb->conn_update_mask &= ~L2C_BLE_UPDATE_PARAM_FULL;
  688. l2c_send_update_conn_params_cb(p_lcb, status);
  689. }
  690. break;
  691. }
  692. case L2CAP_CMD_BLE_CREDIT_BASED_CONN_REQ: {
  693. tL2C_CCB *p_ccb = NULL;
  694. tL2C_RCB *p_rcb = NULL;
  695. UINT16 spsm;
  696. UINT16 scid;
  697. UINT16 mtu;
  698. UINT16 mps;
  699. UINT16 credits;
  700. STREAM_TO_UINT16(spsm, p);
  701. STREAM_TO_UINT16(scid, p);
  702. STREAM_TO_UINT16(mtu, p);
  703. STREAM_TO_UINT16(mps, p);
  704. STREAM_TO_UINT16(credits, p);
  705. L2CAP_TRACE_DEBUG("%s spsm %x, scid %x", __func__, spsm, scid);
  706. UNUSED(spsm);
  707. p_ccb = l2cu_find_ccb_by_remote_cid(p_lcb, scid);
  708. if (p_ccb) {
  709. l2cu_reject_ble_connection(p_lcb, id, L2CAP_LE_RESULT_SOURCE_CID_ALREADY_ALLOCATED);
  710. break;
  711. }
  712. #if 0
  713. p_rcb = l2cu_find_ble_rcb_by_psm(spsm);
  714. if (p_rcb == NULL) {
  715. break;
  716. }
  717. #endif
  718. p_ccb = l2cu_allocate_ccb(p_lcb, 0);
  719. if (p_ccb == NULL) {
  720. l2cu_reject_ble_connection(p_lcb, id, L2CAP_LE_RESULT_NO_RESOURCES);
  721. break;
  722. }
  723. p_ccb->remote_id = id;
  724. p_ccb->p_rcb = p_rcb;
  725. p_ccb->remote_cid = scid;
  726. p_ccb->local_conn_cfg.mtu = mtu;
  727. p_ccb->local_conn_cfg.mps = controller_get_interface()->get_acl_data_size_ble();
  728. p_ccb->local_conn_cfg.credits = credits;
  729. p_ccb->peer_conn_cfg.mtu = mtu;
  730. p_ccb->peer_conn_cfg.mps = mps;
  731. p_ccb->peer_conn_cfg.credits = credits;
  732. l2cu_send_peer_ble_credit_based_conn_res(p_ccb, L2CAP_LE_RESULT_CONN_OK);
  733. break;
  734. }
  735. case L2CAP_CMD_DISC_REQ: {
  736. tL2C_CCB *p_ccb = NULL;
  737. UINT16 lcid;
  738. UINT16 rcid;
  739. STREAM_TO_UINT16(lcid, p);
  740. STREAM_TO_UINT16(rcid, p);
  741. p_ccb = l2cu_find_ccb_by_cid(p_lcb, lcid);
  742. if (p_ccb) {
  743. p_ccb->remote_id = id;
  744. // TODO
  745. }
  746. l2cu_send_peer_disc_rsp(p_lcb, id, lcid, rcid);
  747. break;
  748. }
  749. default:
  750. L2CAP_TRACE_WARNING ("L2CAP - LE - unknown cmd code: %d", cmd_code);
  751. l2cu_send_peer_cmd_reject (p_lcb, L2CAP_CMD_REJ_NOT_UNDERSTOOD, id, 0, 0);
  752. return;
  753. }
  754. }
  755. /*******************************************************************************
  756. **
  757. ** Function l2cble_init_direct_conn
  758. **
  759. ** Description This function is to initate a direct connection
  760. **
  761. ** Returns TRUE connection initiated, FALSE otherwise.
  762. **
  763. *******************************************************************************/
  764. BOOLEAN l2cble_init_direct_conn (tL2C_LCB *p_lcb)
  765. {
  766. #if ( (defined BLE_PRIVACY_SPT) && (BLE_PRIVACY_SPT == TRUE) && (!CONTROLLER_RPA_LIST_ENABLE))
  767. //check for security device information in the cache
  768. bool dev_rec_exist = true;
  769. tBTM_SEC_DEV_REC *find_dev_rec = btm_find_dev (p_lcb->remote_bd_addr);
  770. if(find_dev_rec == NULL) {
  771. dev_rec_exist = false;
  772. }
  773. #endif // ( (defined BLE_PRIVACY_SPT) && (BLE_PRIVACY_SPT == TRUE) && (!CONTROLLER_RPA_LIST_ENABLE))
  774. tBTM_SEC_DEV_REC *p_dev_rec = btm_find_or_alloc_dev (p_lcb->remote_bd_addr);
  775. tBTM_BLE_CB *p_cb = &btm_cb.ble_ctr_cb;
  776. UINT16 scan_int;
  777. UINT16 scan_win;
  778. BD_ADDR peer_addr;
  779. UINT8 peer_addr_type = BLE_ADDR_PUBLIC;
  780. UINT8 own_addr_type = BLE_ADDR_PUBLIC;
  781. /* There can be only one BLE connection request outstanding at a time */
  782. if (p_dev_rec == NULL) {
  783. L2CAP_TRACE_WARNING ("unknown device, can not initate connection");
  784. return (FALSE);
  785. }
  786. scan_int = (p_cb->scan_int == BTM_BLE_SCAN_PARAM_UNDEF) ? BTM_BLE_SCAN_FAST_INT : p_cb->scan_int;
  787. scan_win = (p_cb->scan_win == BTM_BLE_SCAN_PARAM_UNDEF) ? BTM_BLE_SCAN_FAST_WIN : p_cb->scan_win;
  788. peer_addr_type = p_lcb->ble_addr_type;
  789. memcpy(peer_addr, p_lcb->remote_bd_addr, BD_ADDR_LEN);
  790. #if ( (defined BLE_PRIVACY_SPT) && (BLE_PRIVACY_SPT == TRUE))
  791. own_addr_type = btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type;
  792. #if (!CONTROLLER_RPA_LIST_ENABLE)
  793. if(dev_rec_exist) {
  794. // if the current address information is valid, get the real address information
  795. if(p_dev_rec->ble.current_addr_valid) {
  796. peer_addr_type = p_dev_rec->ble.current_addr_type;
  797. memcpy(peer_addr, p_dev_rec->ble.current_addr, 6);
  798. } else {
  799. /* find security device information but not find the real address information
  800. * This state may be directly open without scanning. In this case, you must
  801. * use the current adv address of the device to open*/
  802. }
  803. } else {
  804. //not find security device information, We think this is a new device, connect directly
  805. }
  806. /* It will cause that scanner doesn't send scan request to advertiser
  807. * which has sent IRK to us and we have stored the IRK in controller.
  808. * It is a hardware limitation. The preliminary solution is not to
  809. * send key to the controller, but to resolve the random address in host.
  810. * so we need send the real address information to controller. */
  811. #endif // (!CONTROLLER_RPA_LIST_ENABLE)
  812. #if (CONTROLLER_RPA_LIST_ENABLE)
  813. if (p_dev_rec->ble.in_controller_list & BTM_RESOLVING_LIST_BIT) {
  814. if (btm_cb.ble_ctr_cb.privacy_mode >= BTM_PRIVACY_1_2) {
  815. own_addr_type |= BLE_ADDR_TYPE_ID_BIT;
  816. }
  817. //btm_ble_enable_resolving_list(BTM_BLE_RL_INIT);
  818. btm_random_pseudo_to_identity_addr(peer_addr, &peer_addr_type);
  819. } else {
  820. btm_ble_disable_resolving_list(BTM_BLE_RL_INIT, TRUE);
  821. }
  822. #endif // CONTROLLER_RPA_LIST_ENABLE
  823. #endif // (defined BLE_PRIVACY_SPT) && (BLE_PRIVACY_SPT == TRUE)
  824. if (!btm_ble_topology_check(BTM_BLE_STATE_INIT)) {
  825. l2cu_release_lcb (p_lcb);
  826. L2CAP_TRACE_ERROR("initate direct connection fail, topology limitation");
  827. return FALSE;
  828. }
  829. uint32_t link_timeout = L2CAP_BLE_LINK_CONNECT_TOUT;
  830. if(GATTC_CONNECT_RETRY_COUNT) {
  831. if(!p_lcb->retry_create_con) {
  832. p_lcb->start_time_s = (esp_system_get_time()/1000);
  833. }
  834. uint32_t current_time = (esp_system_get_time()/1000);
  835. link_timeout = (L2CAP_BLE_LINK_CONNECT_TOUT*1000 - (current_time - p_lcb->start_time_s))/1000;
  836. if(link_timeout == 0 || link_timeout > L2CAP_BLE_LINK_CONNECT_TOUT) {
  837. link_timeout = L2CAP_BLE_LINK_CONNECT_TOUT;
  838. }
  839. }
  840. if (!p_lcb->is_aux) {
  841. if (!btsnd_hcic_ble_create_ll_conn (scan_int,/* UINT16 scan_int */
  842. scan_win, /* UINT16 scan_win */
  843. FALSE, /* UINT8 white_list */
  844. peer_addr_type, /* UINT8 addr_type_peer */
  845. peer_addr, /* BD_ADDR bda_peer */
  846. own_addr_type, /* UINT8 addr_type_own */
  847. (UINT16) ((p_dev_rec->conn_params.min_conn_int != BTM_BLE_CONN_PARAM_UNDEF) ?
  848. p_dev_rec->conn_params.min_conn_int : BTM_BLE_CONN_INT_MIN_DEF), /* UINT16 conn_int_min */
  849. (UINT16) ((p_dev_rec->conn_params.max_conn_int != BTM_BLE_CONN_PARAM_UNDEF) ?
  850. p_dev_rec->conn_params.max_conn_int : BTM_BLE_CONN_INT_MAX_DEF), /* UINT16 conn_int_max */
  851. (UINT16) ((p_dev_rec->conn_params.slave_latency != BTM_BLE_CONN_PARAM_UNDEF) ?
  852. p_dev_rec->conn_params.slave_latency : BTM_BLE_CONN_SLAVE_LATENCY_DEF), /* UINT16 conn_latency */
  853. (UINT16) ((p_dev_rec->conn_params.supervision_tout != BTM_BLE_CONN_PARAM_UNDEF) ?
  854. p_dev_rec->conn_params.supervision_tout : BTM_BLE_CONN_TIMEOUT_DEF), /* conn_timeout */
  855. BLE_CE_LEN_MIN, /* UINT16 min_len */
  856. BLE_CE_LEN_MIN)) { /* UINT16 max_len */
  857. l2cu_release_lcb (p_lcb);
  858. L2CAP_TRACE_ERROR("initate direct connection fail, no resources");
  859. return (FALSE);
  860. } else {
  861. p_lcb->link_state = LST_CONNECTING;
  862. l2cb.is_ble_connecting = TRUE;
  863. memcpy (l2cb.ble_connecting_bda, p_lcb->remote_bd_addr, BD_ADDR_LEN);
  864. btu_start_timer (&p_lcb->timer_entry, BTU_TTYPE_L2CAP_LINK, link_timeout);
  865. btm_ble_set_conn_st (BLE_DIR_CONN);
  866. return (TRUE);
  867. }
  868. } else {
  869. #if (BLE_50_FEATURE_SUPPORT == TRUE)
  870. /*
  871. * 0x00 Public Device Address
  872. * 0x01 Random Device Address
  873. * 0x02 Public Identity Address (corresponds to Resolved Private Address)
  874. * 0x03 Random (static) Identity Address (corresponds to Resolved Private Address)
  875. * 0xFF No address provided (anonymous advertisement)
  876. */
  877. if ((peer_addr_type & BLE_ADDR_RANDOM) == BLE_ADDR_RANDOM) {
  878. peer_addr_type = BLE_ADDR_RANDOM;
  879. } else {
  880. peer_addr_type = BLE_ADDR_PUBLIC;
  881. }
  882. tHCI_CreatExtConn aux_conn = {0};
  883. aux_conn.filter_policy = FALSE;
  884. aux_conn.own_addr_type = own_addr_type;
  885. aux_conn.peer_addr_type = peer_addr_type;
  886. memcpy(aux_conn.peer_addr, peer_addr, sizeof(BD_ADDR));
  887. if (p_dev_rec->ext_conn_params.phy_mask == BLE_PHY_NO_PREF) {
  888. L2CAP_TRACE_WARNING("No extend connection parameters set, use default parameters");
  889. aux_conn.init_phy_mask = BLE_PHY_PREF_MASK;
  890. memcpy(&aux_conn.params[0], &ext_conn_params_1m_phy, sizeof(tHCI_ExtConnParams));
  891. memcpy(&aux_conn.params[1], &ext_conn_params_2m_phy, sizeof(tHCI_ExtConnParams));
  892. memcpy(&aux_conn.params[2], &ext_conn_params_coded_phy, sizeof(tHCI_ExtConnParams));
  893. } else {
  894. aux_conn.init_phy_mask = p_dev_rec->ext_conn_params.phy_mask;
  895. memcpy(&aux_conn.params[0], &p_dev_rec->ext_conn_params.phy_1m_conn_params, sizeof(tHCI_ExtConnParams));
  896. memcpy(&aux_conn.params[1], &p_dev_rec->ext_conn_params.phy_2m_conn_params, sizeof(tHCI_ExtConnParams));
  897. memcpy(&aux_conn.params[2], &p_dev_rec->ext_conn_params.phy_coded_conn_params, sizeof(tHCI_ExtConnParams));
  898. }
  899. p_lcb->link_state = LST_CONNECTING;
  900. l2cb.is_ble_connecting = TRUE;
  901. memcpy (l2cb.ble_connecting_bda, p_lcb->remote_bd_addr, BD_ADDR_LEN);
  902. btu_start_timer (&p_lcb->timer_entry, BTU_TTYPE_L2CAP_LINK, link_timeout);
  903. btm_ble_set_conn_st (BLE_DIR_CONN);
  904. if(!btsnd_hcic_ble_create_ext_conn(&aux_conn)) {
  905. l2cu_release_lcb (p_lcb);
  906. L2CAP_TRACE_ERROR("initate Aux connection failed, no resources");
  907. }
  908. #else
  909. L2CAP_TRACE_ERROR("BLE 5.0 not support!\n");
  910. #endif // #if (BLE_50_FEATURE_SUPPORT == TRUE)
  911. return (TRUE);
  912. }
  913. }
  914. /*******************************************************************************
  915. **
  916. ** Function l2cble_create_conn
  917. **
  918. ** Description This function initiates an acl connection via HCI
  919. **
  920. ** Returns TRUE if successful, FALSE if connection not started.
  921. **
  922. *******************************************************************************/
  923. BOOLEAN l2cble_create_conn (tL2C_LCB *p_lcb)
  924. {
  925. tBTM_BLE_CONN_ST conn_st = btm_ble_get_conn_st();
  926. BOOLEAN rt = FALSE;
  927. /* There can be only one BLE connection request outstanding at a time */
  928. if (conn_st == BLE_CONN_IDLE) {
  929. rt = l2cble_init_direct_conn(p_lcb);
  930. } else {
  931. L2CAP_TRACE_WARNING ("L2CAP - LE - cannot start new connection at conn st: %d", conn_st);
  932. btm_ble_enqueue_direct_conn_req(p_lcb);
  933. if (conn_st == BLE_BG_CONN) {
  934. btm_ble_suspend_bg_conn();
  935. }
  936. rt = TRUE;
  937. }
  938. return rt;
  939. }
  940. /*******************************************************************************
  941. **
  942. ** Function l2c_link_processs_ble_num_bufs
  943. **
  944. ** Description This function is called when a "controller buffer size"
  945. ** event is first received from the controller. It updates
  946. ** the L2CAP values.
  947. **
  948. ** Returns void
  949. **
  950. *******************************************************************************/
  951. void l2c_link_processs_ble_num_bufs (UINT16 num_lm_ble_bufs)
  952. {
  953. if (num_lm_ble_bufs == 0) {
  954. num_lm_ble_bufs = L2C_DEF_NUM_BLE_BUF_SHARED;
  955. l2cb.num_lm_acl_bufs -= L2C_DEF_NUM_BLE_BUF_SHARED;
  956. }
  957. L2CAP_TRACE_DEBUG("num_lm_ble_bufs = %d",num_lm_ble_bufs);
  958. l2cb.num_lm_ble_bufs = l2cb.controller_le_xmit_window = num_lm_ble_bufs;
  959. }
  960. /*******************************************************************************
  961. **
  962. ** Function l2c_ble_link_adjust_allocation
  963. **
  964. ** Description This function is called when a link is created or removed
  965. ** to calculate the amount of packets each link may send to
  966. ** the HCI without an ack coming back.
  967. **
  968. ** Currently, this is a simple allocation, dividing the
  969. ** number of Controller Packets by the number of links. In
  970. ** the future, QOS configuration should be examined.
  971. **
  972. ** Returns void
  973. **
  974. *******************************************************************************/
  975. void l2c_ble_link_adjust_allocation (void)
  976. {
  977. UINT16 qq, qq_remainder;
  978. tL2C_LCB *p_lcb;
  979. UINT16 hi_quota, low_quota;
  980. UINT16 num_lowpri_links = 0;
  981. UINT16 num_hipri_links = 0;
  982. UINT16 controller_xmit_quota = l2cb.num_lm_ble_bufs;
  983. UINT16 high_pri_link_quota = L2CAP_HIGH_PRI_MIN_XMIT_QUOTA_A;
  984. list_node_t *p_node = NULL;
  985. /* If no links active, reset buffer quotas and controller buffers */
  986. if (l2cb.num_ble_links_active == 0) {
  987. l2cb.controller_le_xmit_window = l2cb.num_lm_ble_bufs;
  988. l2cb.ble_round_robin_quota = l2cb.ble_round_robin_unacked = 0;
  989. return;
  990. }
  991. /* First, count the links */
  992. for (p_node = list_begin(l2cb.p_lcb_pool); p_node; p_node = list_next(p_node)) {
  993. p_lcb = list_node(p_node);
  994. if (p_lcb->in_use && p_lcb->transport == BT_TRANSPORT_LE) {
  995. if (p_lcb->acl_priority == L2CAP_PRIORITY_HIGH) {
  996. num_hipri_links++;
  997. } else {
  998. num_lowpri_links++;
  999. }
  1000. }
  1001. }
  1002. /* now adjust high priority link quota */
  1003. low_quota = num_lowpri_links ? 1 : 0;
  1004. while ( (num_hipri_links * high_pri_link_quota + low_quota) > controller_xmit_quota ) {
  1005. high_pri_link_quota--;
  1006. }
  1007. /* Work out the xmit quota and buffer quota high and low priorities */
  1008. hi_quota = num_hipri_links * high_pri_link_quota;
  1009. low_quota = (hi_quota < controller_xmit_quota) ? controller_xmit_quota - hi_quota : 1;
  1010. /* Work out and save the HCI xmit quota for each low priority link */
  1011. /* If each low priority link cannot have at least one buffer */
  1012. if (num_lowpri_links > low_quota) {
  1013. l2cb.ble_round_robin_quota = low_quota;
  1014. qq = qq_remainder = 0;
  1015. }
  1016. /* If each low priority link can have at least one buffer */
  1017. else if (num_lowpri_links > 0) {
  1018. l2cb.ble_round_robin_quota = 0;
  1019. l2cb.ble_round_robin_unacked = 0;
  1020. qq = low_quota / num_lowpri_links;
  1021. qq_remainder = low_quota % num_lowpri_links;
  1022. }
  1023. /* If no low priority link */
  1024. else {
  1025. l2cb.ble_round_robin_quota = 0;
  1026. l2cb.ble_round_robin_unacked = 0;
  1027. qq = qq_remainder = 0;
  1028. }
  1029. L2CAP_TRACE_EVENT ("l2c_ble_link_adjust_allocation num_hipri: %u num_lowpri: %u low_quota: %u round_robin_quota: %u qq: %u",
  1030. num_hipri_links, num_lowpri_links, low_quota,
  1031. l2cb.ble_round_robin_quota, qq);
  1032. /* Now, assign the quotas to each link */
  1033. p_node = NULL;
  1034. for (p_node = list_begin(l2cb.p_lcb_pool); p_node; p_node = list_next(p_node)) {
  1035. p_lcb = list_node(p_node);
  1036. if (p_lcb->in_use && p_lcb->transport == BT_TRANSPORT_LE) {
  1037. if (p_lcb->acl_priority == L2CAP_PRIORITY_HIGH) {
  1038. p_lcb->link_xmit_quota = high_pri_link_quota;
  1039. } else {
  1040. /* Safety check in case we switched to round-robin with something outstanding */
  1041. /* if sent_not_acked is added into round_robin_unacked then don't add it again */
  1042. /* l2cap keeps updating sent_not_acked for exiting from round robin */
  1043. if (( p_lcb->link_xmit_quota > 0 ) && ( qq == 0 )) {
  1044. l2cb.ble_round_robin_unacked += p_lcb->sent_not_acked;
  1045. }
  1046. p_lcb->link_xmit_quota = qq;
  1047. if (qq_remainder > 0) {
  1048. p_lcb->link_xmit_quota++;
  1049. qq_remainder--;
  1050. }
  1051. }
  1052. L2CAP_TRACE_EVENT("l2c_ble_link_adjust_allocation Priority: %d XmitQuota: %d",
  1053. p_lcb->acl_priority, p_lcb->link_xmit_quota);
  1054. L2CAP_TRACE_EVENT(" SentNotAcked: %d RRUnacked: %d",
  1055. p_lcb->sent_not_acked, l2cb.round_robin_unacked);
  1056. /* There is a special case where we have readjusted the link quotas and */
  1057. /* this link may have sent anything but some other link sent packets so */
  1058. /* so we may need a timer to kick off this link's transmissions. */
  1059. if ( (p_lcb->link_state == LST_CONNECTED)
  1060. && (!list_is_empty(p_lcb->link_xmit_data_q))
  1061. && (p_lcb->sent_not_acked < p_lcb->link_xmit_quota) ) {
  1062. btu_start_timer (&p_lcb->timer_entry, BTU_TTYPE_L2CAP_LINK, L2CAP_LINK_FLOW_CONTROL_TOUT);
  1063. }
  1064. }
  1065. }
  1066. }
  1067. #if (defined BLE_LLT_INCLUDED) && (BLE_LLT_INCLUDED == TRUE)
  1068. /*******************************************************************************
  1069. **
  1070. ** Function l2cble_process_rc_param_request_evt
  1071. **
  1072. ** Description process LE Remote Connection Parameter Request Event.
  1073. **
  1074. ** Returns void
  1075. **
  1076. *******************************************************************************/
  1077. void l2cble_process_rc_param_request_evt(UINT16 handle, UINT16 int_min, UINT16 int_max,
  1078. UINT16 latency, UINT16 timeout)
  1079. {
  1080. tL2C_LCB *p_lcb = l2cu_find_lcb_by_handle (handle);
  1081. if (p_lcb != NULL) {
  1082. /* if update is enabled, always accept connection parameter update */
  1083. if ((p_lcb->conn_update_mask & L2C_BLE_CONN_UPDATE_DISABLE) == 0) {
  1084. p_lcb->conn_update_mask |= L2C_BLE_UPDATE_PENDING;
  1085. btsnd_hcic_ble_rc_param_req_reply(handle, int_min, int_max, latency, timeout, BLE_CE_LEN_MIN, BLE_CE_LEN_MIN);
  1086. }else {
  1087. /* always accept connection parameters request which is sent by itself */
  1088. if (int_max == BTM_BLE_CONN_INT_MIN) {
  1089. p_lcb->conn_update_mask |= L2C_BLE_UPDATE_PENDING;
  1090. btsnd_hcic_ble_rc_param_req_reply(handle, int_min, int_max, latency, timeout, BLE_CE_LEN_MIN, BLE_CE_LEN_MIN);
  1091. }else {
  1092. L2CAP_TRACE_EVENT ("L2CAP - LE - update currently disabled");
  1093. p_lcb->conn_update_mask |= L2C_BLE_NEW_CONN_PARAM;
  1094. btsnd_hcic_ble_rc_param_req_neg_reply (handle, HCI_ERR_UNACCEPT_CONN_INTERVAL);
  1095. }
  1096. }
  1097. } else {
  1098. L2CAP_TRACE_WARNING("No link to update connection parameter")
  1099. }
  1100. }
  1101. #endif
  1102. /*******************************************************************************
  1103. **
  1104. ** Function l2cble_update_data_length
  1105. **
  1106. ** Description This function update link tx data length if applicable
  1107. **
  1108. ** Returns void
  1109. **
  1110. *******************************************************************************/
  1111. void l2cble_update_data_length(tL2C_LCB *p_lcb)
  1112. {
  1113. UINT16 tx_mtu = 0;
  1114. UINT16 i = 0;
  1115. L2CAP_TRACE_DEBUG("%s", __FUNCTION__);
  1116. /* See if we have a link control block for the connection */
  1117. if (p_lcb == NULL) {
  1118. return;
  1119. }
  1120. for (i = 0; i < L2CAP_NUM_FIXED_CHNLS; i++) {
  1121. if (i + L2CAP_FIRST_FIXED_CHNL != L2CAP_BLE_SIGNALLING_CID) {
  1122. if ((p_lcb->p_fixed_ccbs[i] != NULL) &&
  1123. (tx_mtu < (p_lcb->p_fixed_ccbs[i]->tx_data_len + L2CAP_PKT_OVERHEAD))) {
  1124. tx_mtu = p_lcb->p_fixed_ccbs[i]->tx_data_len + L2CAP_PKT_OVERHEAD;
  1125. }
  1126. }
  1127. }
  1128. if (tx_mtu > BTM_BLE_DATA_SIZE_MAX) {
  1129. tx_mtu = BTM_BLE_DATA_SIZE_MAX;
  1130. }
  1131. /* update TX data length if changed */
  1132. if (p_lcb->tx_data_len != tx_mtu) {
  1133. BTM_SetBleDataLength(p_lcb->remote_bd_addr, tx_mtu);
  1134. }
  1135. }
  1136. /*******************************************************************************
  1137. **
  1138. ** Function l2cble_process_data_length_change_evt
  1139. **
  1140. ** Description This function process the data length change event
  1141. **
  1142. ** Returns void
  1143. **
  1144. *******************************************************************************/
  1145. void l2cble_process_data_length_change_event(UINT16 handle, UINT16 tx_data_len, UINT16 rx_data_len)
  1146. {
  1147. tL2C_LCB *p_lcb = l2cu_find_lcb_by_handle(handle);
  1148. tACL_CONN *p_acl = btm_handle_to_acl(handle);
  1149. tBTM_LE_SET_PKT_DATA_LENGTH_PARAMS data_length_params;
  1150. L2CAP_TRACE_DEBUG("%s TX data len = %d", __FUNCTION__, tx_data_len);
  1151. if (p_lcb == NULL) {
  1152. return;
  1153. }
  1154. if (tx_data_len > 0) {
  1155. p_lcb->tx_data_len = tx_data_len;
  1156. }
  1157. data_length_params.rx_len = rx_data_len;
  1158. data_length_params.tx_len = tx_data_len;
  1159. if(p_acl) {
  1160. p_acl->data_length_params = data_length_params;
  1161. if (p_acl->p_set_pkt_data_cback) {
  1162. (*p_acl->p_set_pkt_data_cback)(BTM_SUCCESS, &data_length_params);
  1163. }
  1164. p_acl->data_len_updating = false;
  1165. if(p_acl->data_len_waiting) {
  1166. p_acl->data_len_waiting = false;
  1167. p_acl->p_set_pkt_data_cback = p_acl->p_set_data_len_cback_waiting;
  1168. p_acl->p_set_data_len_cback_waiting = NULL;
  1169. // if value is same, triger callback directly
  1170. if(p_acl->tx_len_waiting == p_acl->data_length_params.tx_len) {
  1171. if(p_acl->p_set_pkt_data_cback) {
  1172. (*p_acl->p_set_pkt_data_cback)(BTM_SUCCESS, &p_acl->data_length_params);
  1173. }
  1174. return;
  1175. }
  1176. p_acl->data_len_updating = true;
  1177. /* always set the TxTime to be max, as controller does not care for now */
  1178. btsnd_hcic_ble_set_data_length(handle, p_acl->tx_len_waiting,
  1179. BTM_BLE_DATA_TX_TIME_MAX);
  1180. }
  1181. }
  1182. }
  1183. /*******************************************************************************
  1184. **
  1185. ** Function l2cble_set_fixed_channel_tx_data_length
  1186. **
  1187. ** Description This function update max fixed channel tx data length if applicable
  1188. **
  1189. ** Returns void
  1190. **
  1191. *******************************************************************************/
  1192. void l2cble_set_fixed_channel_tx_data_length(BD_ADDR remote_bda, UINT16 fix_cid, UINT16 tx_mtu)
  1193. {
  1194. tL2C_LCB *p_lcb = l2cu_find_lcb_by_bd_addr(remote_bda, BT_TRANSPORT_LE);
  1195. UINT16 cid = fix_cid - L2CAP_FIRST_FIXED_CHNL;
  1196. L2CAP_TRACE_DEBUG("%s TX MTU = %d", __FUNCTION__, tx_mtu);
  1197. if (!controller_get_interface()->supports_ble_packet_extension()) {
  1198. L2CAP_TRACE_WARNING("%s, request not supported", __FUNCTION__);
  1199. return;
  1200. }
  1201. /* See if we have a link control block for the connection */
  1202. if (p_lcb == NULL) {
  1203. return;
  1204. }
  1205. if (p_lcb->p_fixed_ccbs[cid] != NULL) {
  1206. if (tx_mtu > BTM_BLE_DATA_SIZE_MAX) {
  1207. tx_mtu = BTM_BLE_DATA_SIZE_MAX;
  1208. }
  1209. p_lcb->p_fixed_ccbs[cid]->tx_data_len = tx_mtu;
  1210. }
  1211. l2cble_update_data_length(p_lcb);
  1212. }
  1213. /*******************************************************************************
  1214. **
  1215. ** Function l2c_send_update_conn_params_cb
  1216. **
  1217. ** Description This function send the update connection parameter callback to the uplayer.
  1218. **
  1219. ** Returns void
  1220. **
  1221. *******************************************************************************/
  1222. void l2c_send_update_conn_params_cb(tL2C_LCB *p_lcb, UINT8 status)
  1223. {
  1224. if(conn_param_update_cb.update_conn_param_cb != NULL){
  1225. tBTM_LE_UPDATE_CONN_PRAMS update_param;
  1226. //if myself update the connection parameters
  1227. if (p_lcb->updating_param_flag){
  1228. update_param.max_conn_int = p_lcb->updating_conn_max_interval;
  1229. update_param.min_conn_int = p_lcb->updating_conn_min_interval;
  1230. p_lcb->updating_param_flag = false;
  1231. }else{
  1232. // remote device update the connection parameters
  1233. update_param.max_conn_int = update_param.min_conn_int = 0;
  1234. }
  1235. // current connection parameters
  1236. update_param.conn_int = p_lcb->current_used_conn_interval;
  1237. update_param.slave_latency = p_lcb->current_used_conn_latency;
  1238. update_param.supervision_tout = p_lcb->current_used_conn_timeout;
  1239. (conn_param_update_cb.update_conn_param_cb)(status, p_lcb->remote_bd_addr, &update_param);
  1240. }
  1241. }
  1242. /*******************************************************************************
  1243. **
  1244. ** Function CalConnectParamTimeout
  1245. **
  1246. ** Description This function is called to calculate the connection parameter timeout.
  1247. **
  1248. ** Returns timeout
  1249. **
  1250. *******************************************************************************/
  1251. UINT32 CalConnectParamTimeout(tL2C_LCB *p_lcb)
  1252. {
  1253. UINT32 timeout = 6;
  1254. if (p_lcb != NULL){
  1255. //1.25 * conn_int *(1+ latency) *32
  1256. timeout = (40 * ( 1 + p_lcb->current_used_conn_latency) * p_lcb->current_used_conn_interval + 1.25 * p_lcb->waiting_update_conn_max_interval + 1000) / 1000;
  1257. if (timeout < 1){
  1258. timeout = 1;
  1259. }else if (timeout > 120){
  1260. timeout = 120;
  1261. }
  1262. }
  1263. return timeout;
  1264. }
  1265. /*******************************************************************************
  1266. **
  1267. ** Function l2cble_credit_based_conn_req
  1268. **
  1269. ** Description This function sends LE Credit Based Connection Request for
  1270. ** LE connection oriented channels.
  1271. **
  1272. ** Returns void
  1273. **
  1274. *******************************************************************************/
  1275. void l2cble_credit_based_conn_req (tL2C_CCB *p_ccb)
  1276. {
  1277. if (!p_ccb) {
  1278. return;
  1279. }
  1280. if (p_ccb->p_lcb && p_ccb->p_lcb->transport != BT_TRANSPORT_LE)
  1281. {
  1282. L2CAP_TRACE_WARNING ("LE link doesn't exist");
  1283. return;
  1284. }
  1285. l2cu_send_peer_ble_credit_based_conn_req (p_ccb);
  1286. return;
  1287. }
  1288. /*******************************************************************************
  1289. **
  1290. ** Function l2cble_credit_based_conn_res
  1291. **
  1292. ** Description This function sends LE Credit Based Connection Response for
  1293. ** LE connection oriented channels.
  1294. **
  1295. ** Returns void
  1296. **
  1297. *******************************************************************************/
  1298. void l2cble_credit_based_conn_res (tL2C_CCB *p_ccb, UINT16 result)
  1299. {
  1300. if (!p_ccb) {
  1301. return;
  1302. }
  1303. if (p_ccb->p_lcb && p_ccb->p_lcb->transport != BT_TRANSPORT_LE)
  1304. {
  1305. L2CAP_TRACE_WARNING ("LE link doesn't exist");
  1306. return;
  1307. }
  1308. l2cu_send_peer_ble_credit_based_conn_res (p_ccb, result);
  1309. return;
  1310. }
  1311. /*******************************************************************************
  1312. **
  1313. ** Function l2cble_send_flow_control_credit
  1314. **
  1315. ** Description This function sends flow control credits for
  1316. ** LE connection oriented channels.
  1317. **
  1318. ** Returns void
  1319. **
  1320. *******************************************************************************/
  1321. void l2cble_send_flow_control_credit(tL2C_CCB *p_ccb, UINT16 credit_value)
  1322. {
  1323. if (!p_ccb) {
  1324. return;
  1325. }
  1326. if (p_ccb->p_lcb && p_ccb->p_lcb->transport != BT_TRANSPORT_LE)
  1327. {
  1328. L2CAP_TRACE_WARNING ("LE link doesn't exist");
  1329. return;
  1330. }
  1331. l2cu_send_peer_ble_flow_control_credit(p_ccb, credit_value);
  1332. return;
  1333. }
  1334. /*******************************************************************************
  1335. **
  1336. ** Function l2cble_send_peer_disc_req
  1337. **
  1338. ** Description This function sends disconnect request
  1339. ** to the peer LE device
  1340. **
  1341. ** Returns void
  1342. **
  1343. *******************************************************************************/
  1344. void l2cble_send_peer_disc_req(tL2C_CCB *p_ccb)
  1345. {
  1346. L2CAP_TRACE_DEBUG ("%s",__func__);
  1347. if (!p_ccb) {
  1348. return;
  1349. }
  1350. if (p_ccb->p_lcb && p_ccb->p_lcb->transport != BT_TRANSPORT_LE)
  1351. {
  1352. L2CAP_TRACE_WARNING ("LE link doesn't exist");
  1353. return;
  1354. }
  1355. l2cu_send_peer_ble_credit_based_disconn_req(p_ccb);
  1356. return;
  1357. }
  1358. #if (SMP_INCLUDED == TRUE)
  1359. /*******************************************************************************
  1360. **
  1361. ** Function l2cble_sec_comp
  1362. **
  1363. ** Description This function is called when security procedure for an LE COC
  1364. ** link is done
  1365. **
  1366. ** Returns void
  1367. **
  1368. *******************************************************************************/
  1369. void l2cble_sec_comp(BD_ADDR p_bda, tBT_TRANSPORT transport, void *p_ref_data, UINT8 status)
  1370. {
  1371. tL2C_LCB *p_lcb = l2cu_find_lcb_by_bd_addr(p_bda, BT_TRANSPORT_LE);
  1372. tL2CAP_SEC_DATA *p_buf = NULL;
  1373. UINT8 sec_flag;
  1374. UINT8 sec_act;
  1375. if (!p_lcb)
  1376. {
  1377. L2CAP_TRACE_WARNING ("%s security complete for unknown device", __func__);
  1378. return;
  1379. }
  1380. sec_act = p_lcb->sec_act;
  1381. p_lcb->sec_act = 0;
  1382. if (!fixed_queue_is_empty(p_lcb->le_sec_pending_q))
  1383. {
  1384. p_buf = (tL2CAP_SEC_DATA*) fixed_queue_dequeue(p_lcb->le_sec_pending_q, FIXED_QUEUE_MAX_TIMEOUT);
  1385. if (!p_buf)
  1386. {
  1387. L2CAP_TRACE_WARNING ("%s Security complete for request not initiated from L2CAP",
  1388. __func__);
  1389. return;
  1390. }
  1391. if (status != BTM_SUCCESS)
  1392. {
  1393. (*(p_buf->p_callback))(p_bda, BT_TRANSPORT_LE, p_buf->p_ref_data, status);
  1394. }
  1395. else
  1396. {
  1397. if (sec_act == BTM_SEC_ENCRYPT_MITM)
  1398. {
  1399. BTM_GetSecurityFlagsByTransport(p_bda, &sec_flag, transport);
  1400. if (sec_flag & BTM_SEC_FLAG_LKEY_AUTHED) {
  1401. (*(p_buf->p_callback))(p_bda, BT_TRANSPORT_LE, p_buf->p_ref_data, status);
  1402. }
  1403. else
  1404. {
  1405. L2CAP_TRACE_DEBUG ("%s MITM Protection Not present", __func__);
  1406. (*(p_buf->p_callback))(p_bda, BT_TRANSPORT_LE, p_buf->p_ref_data,
  1407. BTM_FAILED_ON_SECURITY);
  1408. }
  1409. }
  1410. else
  1411. {
  1412. L2CAP_TRACE_DEBUG ("%s MITM Protection not required sec_act = %d",
  1413. __func__, p_lcb->sec_act);
  1414. (*(p_buf->p_callback))(p_bda, BT_TRANSPORT_LE, p_buf->p_ref_data, status);
  1415. }
  1416. }
  1417. }
  1418. else
  1419. {
  1420. L2CAP_TRACE_WARNING ("%s Security complete for request not initiated from L2CAP", __func__);
  1421. return;
  1422. }
  1423. osi_free(p_buf);
  1424. while (!fixed_queue_is_empty(p_lcb->le_sec_pending_q))
  1425. {
  1426. p_buf = (tL2CAP_SEC_DATA*) fixed_queue_dequeue(p_lcb->le_sec_pending_q, FIXED_QUEUE_MAX_TIMEOUT);
  1427. if (status != BTM_SUCCESS) {
  1428. (*(p_buf->p_callback))(p_bda, BT_TRANSPORT_LE, p_buf->p_ref_data, status);
  1429. } else {
  1430. l2ble_sec_access_req(p_bda, p_buf->psm, p_buf->is_originator,
  1431. p_buf->p_callback, p_buf->p_ref_data);
  1432. }
  1433. osi_free(p_buf);
  1434. }
  1435. }
  1436. /*******************************************************************************
  1437. **
  1438. ** Function l2ble_sec_access_req
  1439. **
  1440. ** Description This function is called by LE COC link to meet the
  1441. ** security requirement for the link
  1442. **
  1443. ** Returns TRUE - security procedures are started
  1444. ** FALSE - failure
  1445. **
  1446. *******************************************************************************/
  1447. BOOLEAN l2ble_sec_access_req(BD_ADDR bd_addr, UINT16 psm, BOOLEAN is_originator, tL2CAP_SEC_CBACK *p_callback, void *p_ref_data)
  1448. {
  1449. L2CAP_TRACE_DEBUG ("%s", __func__);
  1450. BOOLEAN status;
  1451. tL2C_LCB *p_lcb = NULL;
  1452. if (!p_callback)
  1453. {
  1454. L2CAP_TRACE_ERROR("%s No callback function", __func__);
  1455. return FALSE;
  1456. }
  1457. p_lcb = l2cu_find_lcb_by_bd_addr(bd_addr, BT_TRANSPORT_LE);
  1458. if (!p_lcb)
  1459. {
  1460. L2CAP_TRACE_ERROR ("%s Security check for unknown device", __func__);
  1461. p_callback(bd_addr, BT_TRANSPORT_LE, p_ref_data, BTM_UNKNOWN_ADDR);
  1462. return FALSE;
  1463. }
  1464. tL2CAP_SEC_DATA *p_buf = (tL2CAP_SEC_DATA*) osi_malloc((UINT16)sizeof(tL2CAP_SEC_DATA));
  1465. if (!p_buf)
  1466. {
  1467. p_callback(bd_addr, BT_TRANSPORT_LE, p_ref_data, BTM_NO_RESOURCES);
  1468. return FALSE;
  1469. }
  1470. p_buf->psm = psm;
  1471. p_buf->is_originator = is_originator;
  1472. p_buf->p_callback = p_callback;
  1473. p_buf->p_ref_data = p_ref_data;
  1474. fixed_queue_enqueue(p_lcb->le_sec_pending_q, p_buf, FIXED_QUEUE_MAX_TIMEOUT);
  1475. status = btm_ble_start_sec_check(bd_addr, psm, is_originator, &l2cble_sec_comp, p_ref_data);
  1476. return status;
  1477. }
  1478. #endif /* #if (SMP_INCLUDED == TRUE) */
  1479. #endif /* (BLE_INCLUDED == TRUE) */
  1480. /*******************************************************************************
  1481. **
  1482. ** Function L2CA_GetDisconnectReason
  1483. **
  1484. ** Description This function returns the disconnect reason code.
  1485. **
  1486. ** Returns disconnect reason
  1487. **
  1488. *******************************************************************************/
  1489. UINT16 L2CA_GetDisconnectReason (BD_ADDR remote_bda, tBT_TRANSPORT transport)
  1490. {
  1491. tL2C_LCB *p_lcb;
  1492. UINT16 reason = 0;
  1493. if ((p_lcb = l2cu_find_lcb_by_bd_addr (remote_bda, transport)) != NULL) {
  1494. reason = p_lcb->disc_reason;
  1495. }
  1496. L2CAP_TRACE_DEBUG ("L2CA_GetDisconnectReason=%d ", reason);
  1497. return reason;
  1498. }