l2c_ble.c 58 KB

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