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