l2c_ble.c 63 KB

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