• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "bt_target.h"
27 #include "bt_utils.h"
28 #include "l2cdefs.h"
29 #include "l2c_int.h"
30 #include "btu.h"
31 #include "btm_int.h"
32 #include "hcimsgs.h"
33 
34 #if (BLE_INCLUDED == TRUE)
35 static void l2cble_start_conn_update (tL2C_LCB *p_lcb);
36 
37 #include "vendor_ble.h"
38 /*******************************************************************************
39 **
40 **  Function        L2CA_CancelBleConnectReq
41 **
42 **  Description     Cancel a pending connection attempt to a BLE device.
43 **
44 **  Parameters:     BD Address of remote
45 **
46 **  Return value:   TRUE if connection was cancelled
47 **
48 *******************************************************************************/
L2CA_CancelBleConnectReq(BD_ADDR rem_bda)49 BOOLEAN L2CA_CancelBleConnectReq (BD_ADDR rem_bda)
50 {
51     tL2C_LCB *p_lcb;
52 
53     /* There can be only one BLE connection request outstanding at a time */
54     if (btm_ble_get_conn_st() == BLE_CONN_IDLE)
55     {
56         L2CAP_TRACE_WARNING ("L2CA_CancelBleConnectReq - no connection pending");
57         return(FALSE);
58     }
59 
60     if (memcmp (rem_bda, l2cb.ble_connecting_bda, BD_ADDR_LEN))
61     {
62         L2CAP_TRACE_WARNING ("L2CA_CancelBleConnectReq - different  BDA Connecting: %08x%04x  Cancel: %08x%04x",
63                               (l2cb.ble_connecting_bda[0]<<24)+(l2cb.ble_connecting_bda[1]<<16)+(l2cb.ble_connecting_bda[2]<<8)+l2cb.ble_connecting_bda[3],
64                               (l2cb.ble_connecting_bda[4]<<8)+l2cb.ble_connecting_bda[5],
65                               (rem_bda[0]<<24)+(rem_bda[1]<<16)+(rem_bda[2]<<8)+rem_bda[3], (rem_bda[4]<<8)+rem_bda[5]);
66 
67         return(FALSE);
68     }
69 
70     if (btsnd_hcic_ble_create_conn_cancel())
71     {
72 
73         if ((p_lcb = l2cu_find_lcb_by_bd_addr (rem_bda, BT_TRANSPORT_LE)) != NULL)
74         {
75             p_lcb->disc_reason = L2CAP_CONN_CANCEL;
76             l2cu_release_lcb (p_lcb);
77         }
78         /* update state to be cancel, wait for connection cancel complete */
79         btm_ble_set_conn_st (BLE_CONN_CANCEL);
80 
81         return(TRUE);
82     }
83     else
84         return(FALSE);
85 }
86 
87 /*******************************************************************************
88 **
89 **  Function        L2CA_UpdateBleConnParams
90 **
91 **  Description     Update BLE connection parameters.
92 **
93 **  Parameters:     BD Address of remote
94 **
95 **  Return value:   TRUE if update started
96 **
97 *******************************************************************************/
L2CA_UpdateBleConnParams(BD_ADDR rem_bda,UINT16 min_int,UINT16 max_int,UINT16 latency,UINT16 timeout)98 BOOLEAN L2CA_UpdateBleConnParams (BD_ADDR rem_bda, UINT16 min_int, UINT16 max_int,
99                                             UINT16 latency, UINT16 timeout)
100 {
101         tL2C_LCB            *p_lcb;
102         tACL_CONN           *p_acl_cb = btm_bda_to_acl(rem_bda, BT_TRANSPORT_LE);
103 
104         /* See if we have a link control block for the remote device */
105         p_lcb = l2cu_find_lcb_by_bd_addr (rem_bda, BT_TRANSPORT_LE);
106 
107         /* If we don't have one, create one and accept the connection. */
108         if (!p_lcb || !p_acl_cb)
109         {
110             L2CAP_TRACE_WARNING ("L2CA_UpdateBleConnParams - unknown BD_ADDR %08x%04x",
111                                   (rem_bda[0]<<24)+(rem_bda[1]<<16)+(rem_bda[2]<<8)+rem_bda[3],
112                                   (rem_bda[4]<<8)+rem_bda[5]);
113             return(FALSE);
114         }
115 
116         if (p_lcb->transport != BT_TRANSPORT_LE)
117         {
118             L2CAP_TRACE_WARNING ("L2CA_UpdateBleConnParams - BD_ADDR %08x%04x not LE",
119                                   (rem_bda[0]<<24)+(rem_bda[1]<<16)+(rem_bda[2]<<8)+rem_bda[3],
120                                   (rem_bda[4]<<8)+rem_bda[5]);
121             return(FALSE);
122         }
123 #if (defined BLE_LLT_INCLUDED) && (BLE_LLT_INCLUDED == TRUE)
124         /* if both 4.1 compliant */
125         if ((HCI_LE_CONN_PARAM_REQ_SUPPORTED(btm_cb.devcb.local_le_features) &&
126          HCI_LE_CONN_PARAM_REQ_SUPPORTED(p_acl_cb->peer_le_features)))
127         {
128             p_lcb->min_interval = min_int;
129             p_lcb->max_interval = max_int;
130             p_lcb->latency = latency;
131             p_lcb->timeout = timeout;
132             p_lcb->conn_update_mask |= L2C_BLE_NEW_CONN_PARAM;
133 
134             l2cble_start_conn_update(p_lcb);
135         }
136         else
137         /* if either side does not support Connection Parameters Request
138         Link Layer Control Procedure,
139            use Link Layer Connection Update procedure */
140 #endif
141         {
142             if (p_lcb->link_role == HCI_ROLE_MASTER)
143         {
144             p_lcb->min_interval = min_int;
145             p_lcb->max_interval = max_int;
146             p_lcb->latency = latency;
147             p_lcb->timeout = timeout;
148             p_lcb->conn_update_mask |= L2C_BLE_NEW_CONN_PARAM;
149 
150             l2cble_start_conn_update(p_lcb);
151         }
152             else
153                 l2cu_send_peer_ble_par_req (p_lcb, min_int, max_int, latency, timeout);
154         }
155         return(TRUE);
156 
157 }
158 
159 
160 /*******************************************************************************
161 **
162 **  Function        L2CA_EnableUpdateBleConnParams
163 **
164 **  Description     Enable or disable update based on the request from the peer
165 **
166 **  Parameters:     BD Address of remote
167 **
168 **  Return value:   TRUE if update started
169 **
170 *******************************************************************************/
L2CA_EnableUpdateBleConnParams(BD_ADDR rem_bda,BOOLEAN enable)171 BOOLEAN L2CA_EnableUpdateBleConnParams (BD_ADDR rem_bda, BOOLEAN enable)
172 {
173     tL2C_LCB            *p_lcb;
174 
175     /* See if we have a link control block for the remote device */
176     p_lcb = l2cu_find_lcb_by_bd_addr (rem_bda, BT_TRANSPORT_LE);
177 
178     if (!p_lcb)
179     {
180         L2CAP_TRACE_WARNING ("L2CA_EnableUpdateBleConnParams - unknown BD_ADDR %08x%04x",
181             (rem_bda[0]<<24)+(rem_bda[1]<<16)+(rem_bda[2]<<8)+rem_bda[3],
182             (rem_bda[4]<<8)+rem_bda[5]);
183         return (FALSE);
184     }
185 
186     L2CAP_TRACE_API ("%s - BD_ADDR %08x%04x enable %d current upd state 0x%02x",__FUNCTION__,
187         (rem_bda[0]<<24)+(rem_bda[1]<<16)+(rem_bda[2]<<8)+rem_bda[3],
188         (rem_bda[4]<<8)+rem_bda[5], enable, p_lcb->conn_update_mask);
189 
190     if (p_lcb->transport != BT_TRANSPORT_LE || (p_lcb->link_role != HCI_ROLE_MASTER))
191     {
192         L2CAP_TRACE_WARNING ("%s - BD_ADDR %08x%04x not LE or not master %d", __FUNCTION__,
193                               (rem_bda[0]<<24)+(rem_bda[1]<<16)+(rem_bda[2]<<8)+rem_bda[3],
194                               (rem_bda[4]<<8)+rem_bda[5], p_lcb->link_role);
195         return (FALSE);
196     }
197 
198     if (enable)
199         p_lcb->conn_update_mask &= ~L2C_BLE_CONN_UPDATE_DISABLE;
200     else
201         p_lcb->conn_update_mask |= L2C_BLE_CONN_UPDATE_DISABLE;
202 
203     l2cble_start_conn_update(p_lcb);
204 
205     return (TRUE);
206 }
207 
208 
209 /*******************************************************************************
210 **
211 ** Function         L2CA_GetBleConnRole
212 **
213 ** Description      This function returns the connection role.
214 **
215 ** Returns          link role.
216 **
217 *******************************************************************************/
L2CA_GetBleConnRole(BD_ADDR bd_addr)218 UINT8 L2CA_GetBleConnRole (BD_ADDR bd_addr)
219 {
220     UINT8       role = HCI_ROLE_UNKNOWN;
221 
222     tL2C_LCB *p_lcb;
223 
224     if ((p_lcb = l2cu_find_lcb_by_bd_addr (bd_addr, BT_TRANSPORT_LE)) != NULL)
225         role = p_lcb->link_role;
226 
227     return role;
228 }
229 /*******************************************************************************
230 **
231 ** Function         L2CA_GetDisconnectReason
232 **
233 ** Description      This function returns the disconnect reason code.
234 **
235 ** Returns          disconnect reason
236 **
237 *******************************************************************************/
L2CA_GetDisconnectReason(BD_ADDR remote_bda,tBT_TRANSPORT transport)238 UINT16 L2CA_GetDisconnectReason (BD_ADDR remote_bda, tBT_TRANSPORT transport)
239 {
240     tL2C_LCB            *p_lcb;
241     UINT16              reason = 0;
242 
243     if ((p_lcb = l2cu_find_lcb_by_bd_addr (remote_bda, transport)) != NULL)
244         reason = p_lcb->disc_reason;
245 
246     L2CAP_TRACE_DEBUG ("L2CA_GetDisconnectReason=%d ",reason);
247 
248     return reason;
249 }
250 
251 /*******************************************************************************
252 **
253 ** Function l2cble_notify_le_connection
254 **
255 ** Description This function notifiy the l2cap connection to the app layer
256 **
257 ** Returns none
258 **
259 *******************************************************************************/
l2cble_notify_le_connection(BD_ADDR bda)260 void l2cble_notify_le_connection (BD_ADDR bda)
261 {
262     tL2C_LCB *p_lcb = l2cu_find_lcb_by_bd_addr (bda, BT_TRANSPORT_LE);
263     tACL_CONN *p_acl = btm_bda_to_acl(bda, BT_TRANSPORT_LE) ;
264 
265     if (p_lcb != NULL && p_acl != NULL && p_lcb->link_state != LST_CONNECTED)
266     {
267         /* update link status */
268         btm_establish_continue(p_acl);
269         /* update l2cap link status and send callback */
270         p_lcb->link_state = LST_CONNECTED;
271         l2cu_process_fixed_chnl_resp (p_lcb);
272     }
273 }
274 
275 /*******************************************************************************
276 **
277 ** Function         l2cble_scanner_conn_comp
278 **
279 ** Description      This function is called when an HCI Connection Complete
280 **                  event is received while we are a scanner (so we are master).
281 **
282 ** Returns          void
283 **
284 *******************************************************************************/
l2cble_scanner_conn_comp(UINT16 handle,BD_ADDR bda,tBLE_ADDR_TYPE type,UINT16 conn_interval,UINT16 conn_latency,UINT16 conn_timeout)285 void l2cble_scanner_conn_comp (UINT16 handle, BD_ADDR bda, tBLE_ADDR_TYPE type,
286                                UINT16 conn_interval, UINT16 conn_latency, UINT16 conn_timeout)
287 {
288     tL2C_LCB            *p_lcb;
289     tBTM_SEC_DEV_REC    *p_dev_rec = btm_find_or_alloc_dev (bda);
290 
291     L2CAP_TRACE_DEBUG ("l2cble_scanner_conn_comp: HANDLE=%d addr_type=%d conn_interval=%d slave_latency=%d supervision_tout=%d",
292                         handle,  type, conn_interval, conn_latency, conn_timeout);
293 
294     l2cb.is_ble_connecting = FALSE;
295 
296     /* See if we have a link control block for the remote device */
297     p_lcb = l2cu_find_lcb_by_bd_addr (bda, BT_TRANSPORT_LE);
298 
299     /* If we don't have one, create one. this is auto connection complete. */
300     if (!p_lcb)
301     {
302         p_lcb = l2cu_allocate_lcb (bda, FALSE, BT_TRANSPORT_LE);
303         if (!p_lcb)
304         {
305             btm_sec_disconnect (handle, HCI_ERR_NO_CONNECTION);
306             L2CAP_TRACE_ERROR ("l2cble_scanner_conn_comp - failed to allocate LCB");
307             return;
308         }
309         else
310         {
311             if (!l2cu_initialize_fixed_ccb (p_lcb, L2CAP_ATT_CID, &l2cb.fixed_reg[L2CAP_ATT_CID - L2CAP_FIRST_FIXED_CHNL].fixed_chnl_opts))
312             {
313                 btm_sec_disconnect (handle, HCI_ERR_NO_CONNECTION);
314                 L2CAP_TRACE_WARNING ("l2cble_scanner_conn_comp - LCB but no CCB");
315                 return ;
316             }
317         }
318     }
319     else if (p_lcb->link_state != LST_CONNECTING)
320     {
321         L2CAP_TRACE_ERROR ("L2CAP got BLE scanner conn_comp in bad state: %d", p_lcb->link_state);
322         return;
323     }
324     btu_stop_timer(&p_lcb->timer_entry);
325 
326     /* Save the handle */
327     p_lcb->handle = handle;
328 
329     /* Connected OK. Change state to connected, we were scanning so we are master */
330     p_lcb->link_role  = HCI_ROLE_MASTER;
331     p_lcb->transport  = BT_TRANSPORT_LE;
332 
333     /* If there are any preferred connection parameters, set them now */
334     if ( (p_dev_rec->conn_params.min_conn_int     >= BTM_BLE_CONN_INT_MIN ) &&
335          (p_dev_rec->conn_params.min_conn_int     <= BTM_BLE_CONN_INT_MAX ) &&
336          (p_dev_rec->conn_params.max_conn_int     >= BTM_BLE_CONN_INT_MIN ) &&
337          (p_dev_rec->conn_params.max_conn_int     <= BTM_BLE_CONN_INT_MAX ) &&
338          (p_dev_rec->conn_params.slave_latency    <= BTM_BLE_CONN_LATENCY_MAX ) &&
339          (p_dev_rec->conn_params.supervision_tout >= BTM_BLE_CONN_SUP_TOUT_MIN) &&
340          (p_dev_rec->conn_params.supervision_tout <= BTM_BLE_CONN_SUP_TOUT_MAX) &&
341          ((conn_interval < p_dev_rec->conn_params.min_conn_int &&
342           p_dev_rec->conn_params.min_conn_int != BTM_BLE_CONN_PARAM_UNDEF) ||
343           (conn_interval > p_dev_rec->conn_params.max_conn_int) ||
344           (conn_latency > p_dev_rec->conn_params.slave_latency) ||
345           (conn_timeout > p_dev_rec->conn_params.supervision_tout)))
346     {
347         L2CAP_TRACE_ERROR ("upd_ll_conn_params: HANDLE=%d min_conn_int=%d max_conn_int=%d slave_latency=%d supervision_tout=%d",
348                             handle, p_dev_rec->conn_params.min_conn_int, p_dev_rec->conn_params.max_conn_int,
349                             p_dev_rec->conn_params.slave_latency, p_dev_rec->conn_params.supervision_tout);
350 
351         btsnd_hcic_ble_upd_ll_conn_params (handle,
352                                            p_dev_rec->conn_params.min_conn_int,
353                                            p_dev_rec->conn_params.max_conn_int,
354                                            p_dev_rec->conn_params.slave_latency,
355                                            p_dev_rec->conn_params.supervision_tout,
356                                            0, 0);
357     }
358 
359     /* Tell BTM Acl management about the link */
360     btm_acl_created (bda, NULL, p_dev_rec->sec_bd_name, handle, p_lcb->link_role, BT_TRANSPORT_LE);
361 
362     p_lcb->peer_chnl_mask[0] = L2CAP_FIXED_CHNL_ATT_BIT | L2CAP_FIXED_CHNL_BLE_SIG_BIT | L2CAP_FIXED_CHNL_SMP_BIT;
363 
364     btm_ble_set_conn_st(BLE_CONN_IDLE);
365 }
366 
367 
368 /*******************************************************************************
369 **
370 ** Function         l2cble_advertiser_conn_comp
371 **
372 ** Description      This function is called when an HCI Connection Complete
373 **                  event is received while we are an advertiser (so we are slave).
374 **
375 ** Returns          void
376 **
377 *******************************************************************************/
l2cble_advertiser_conn_comp(UINT16 handle,BD_ADDR bda,tBLE_ADDR_TYPE type,UINT16 conn_interval,UINT16 conn_latency,UINT16 conn_timeout)378 void l2cble_advertiser_conn_comp (UINT16 handle, BD_ADDR bda, tBLE_ADDR_TYPE type,
379                                   UINT16 conn_interval, UINT16 conn_latency, UINT16 conn_timeout)
380 {
381     tL2C_LCB            *p_lcb;
382     tBTM_SEC_DEV_REC    *p_dev_rec;
383     UNUSED(type);
384     UNUSED(conn_interval);
385     UNUSED(conn_latency);
386     UNUSED(conn_timeout);
387 
388     /* See if we have a link control block for the remote device */
389     p_lcb = l2cu_find_lcb_by_bd_addr (bda, BT_TRANSPORT_LE);
390 
391     /* If we don't have one, create one and accept the connection. */
392     if (!p_lcb)
393     {
394         p_lcb = l2cu_allocate_lcb (bda, FALSE, BT_TRANSPORT_LE);
395         if (!p_lcb)
396         {
397             btm_sec_disconnect (handle, HCI_ERR_NO_CONNECTION);
398             L2CAP_TRACE_ERROR ("l2cble_advertiser_conn_comp - failed to allocate LCB");
399             return;
400         }
401         else
402         {
403             if (!l2cu_initialize_fixed_ccb (p_lcb, L2CAP_ATT_CID, &l2cb.fixed_reg[L2CAP_ATT_CID - L2CAP_FIRST_FIXED_CHNL].fixed_chnl_opts))
404             {
405                 btm_sec_disconnect (handle, HCI_ERR_NO_CONNECTION);
406                 L2CAP_TRACE_WARNING ("l2cble_scanner_conn_comp - LCB but no CCB");
407                 return ;
408             }
409         }
410     }
411 
412     /* Save the handle */
413     p_lcb->handle = handle;
414 
415     /* Connected OK. Change state to connected, we were advertising, so we are slave */
416     p_lcb->link_role  = HCI_ROLE_SLAVE;
417     p_lcb->transport  = BT_TRANSPORT_LE;
418 
419     /* Tell BTM Acl management about the link */
420     p_dev_rec = btm_find_or_alloc_dev (bda);
421 
422     btm_acl_created (bda, NULL, p_dev_rec->sec_bd_name, handle, p_lcb->link_role, BT_TRANSPORT_LE);
423 
424     p_lcb->peer_chnl_mask[0] = L2CAP_FIXED_CHNL_ATT_BIT | L2CAP_FIXED_CHNL_BLE_SIG_BIT | L2CAP_FIXED_CHNL_SMP_BIT;
425 
426     if (!HCI_LE_SLAVE_INIT_FEAT_EXC_SUPPORTED(btm_cb.devcb.local_le_features))
427     {
428         p_lcb->link_state = LST_CONNECTED;
429         l2cu_process_fixed_chnl_resp (p_lcb);
430     }
431 
432     /* when adv and initiating are both active, cancel the direct connection */
433     if (l2cb.is_ble_connecting && memcmp(bda, l2cb.ble_connecting_bda, BD_ADDR_LEN) == 0)
434     {
435         L2CA_CancelBleConnectReq(bda);
436     }
437 }
438 
439 /*******************************************************************************
440 **
441 ** Function         l2cble_conn_comp
442 **
443 ** Description      This function is called when an HCI Connection Complete
444 **                  event is received.
445 **
446 ** Returns          void
447 **
448 *******************************************************************************/
l2cble_conn_comp(UINT16 handle,UINT8 role,BD_ADDR bda,tBLE_ADDR_TYPE type,UINT16 conn_interval,UINT16 conn_latency,UINT16 conn_timeout)449 void l2cble_conn_comp(UINT16 handle, UINT8 role, BD_ADDR bda, tBLE_ADDR_TYPE type,
450                       UINT16 conn_interval, UINT16 conn_latency, UINT16 conn_timeout)
451 {
452     if (role == HCI_ROLE_MASTER)
453     {
454         l2cble_scanner_conn_comp(handle, bda, type, conn_interval, conn_latency, conn_timeout);
455     }
456     else
457     {
458         l2cble_advertiser_conn_comp(handle, bda, type, conn_interval, conn_latency, conn_timeout);
459     }
460 }
461 
462 /*******************************************************************************
463 **
464 **  Function        l2cble_start_conn_update
465 **
466 **  Description     start BLE connection parameter update process based on status
467 **
468 **  Parameters:     lcb : l2cap link control block
469 **
470 **  Return value:   none
471 **
472 *******************************************************************************/
l2cble_start_conn_update(tL2C_LCB * p_lcb)473 static void l2cble_start_conn_update (tL2C_LCB *p_lcb)
474 {
475     tBTM_SEC_DEV_REC *p_dev_rec = btm_find_or_alloc_dev(p_lcb->remote_bd_addr);
476 
477     if (p_lcb->conn_update_mask & L2C_BLE_UPDATE_PENDING) return;
478 
479     if (p_lcb->conn_update_mask & L2C_BLE_CONN_UPDATE_DISABLE)
480     {
481         /* application requests to disable parameters update.
482            If parameters are already updated, lets set them
483            up to what has been requested during connection establishement */
484         if (p_lcb->conn_update_mask & L2C_BLE_NOT_DEFAULT_PARAM)
485         {
486             btsnd_hcic_ble_upd_ll_conn_params(p_lcb->handle,
487                 (UINT16)((p_dev_rec->conn_params.min_conn_int != BTM_BLE_CONN_PARAM_UNDEF) ?
488                          p_dev_rec->conn_params.min_conn_int : BTM_BLE_CONN_INT_MIN_DEF),
489                 (UINT16)((p_dev_rec->conn_params.max_conn_int != BTM_BLE_CONN_PARAM_UNDEF) ?
490                          p_dev_rec->conn_params.max_conn_int : BTM_BLE_CONN_INT_MAX_DEF),
491                 (UINT16)((p_dev_rec->conn_params.slave_latency != BTM_BLE_CONN_PARAM_UNDEF) ?
492                          p_dev_rec->conn_params.slave_latency : BTM_BLE_CONN_SLAVE_LATENCY_DEF),
493                 (UINT16)((p_dev_rec->conn_params.supervision_tout != BTM_BLE_CONN_PARAM_UNDEF) ?
494                          p_dev_rec->conn_params.supervision_tout : BTM_BLE_CONN_TIMEOUT_DEF),
495                 0, 0);
496             p_lcb->conn_update_mask &= ~L2C_BLE_NOT_DEFAULT_PARAM;
497             p_lcb->conn_update_mask |= (L2C_BLE_UPDATE_PENDING | L2C_BLE_NEW_CONN_PARAM);
498         }
499     }
500     else
501     {
502         /* application allows to do update, if we were delaying one do it now */
503         if (p_lcb->conn_update_mask & L2C_BLE_NEW_CONN_PARAM)
504         {
505             btsnd_hcic_ble_upd_ll_conn_params(p_lcb->handle, p_lcb->min_interval,
506                 p_lcb->max_interval, p_lcb->latency, p_lcb->timeout, 0, 0);
507             p_lcb->conn_update_mask &= ~L2C_BLE_NEW_CONN_PARAM;
508             p_lcb->conn_update_mask |= (L2C_BLE_UPDATE_PENDING | L2C_BLE_NOT_DEFAULT_PARAM);
509         }
510     }
511 }
512 
513 /*******************************************************************************
514 **
515 ** Function         l2cble_process_conn_update_evt
516 **
517 ** Description      This function enables the connection update request from remote
518 **                  after a successful connection update response is received.
519 **
520 ** Returns          void
521 **
522 *******************************************************************************/
l2cble_process_conn_update_evt(UINT16 handle,UINT8 status)523 void l2cble_process_conn_update_evt (UINT16 handle, UINT8 status)
524 {
525     tL2C_LCB *p_lcb;
526 
527     L2CAP_TRACE_DEBUG("l2cble_process_conn_update_evt");
528 
529     /* See if we have a link control block for the remote device */
530     p_lcb = l2cu_find_lcb_by_handle(handle);
531     if (!p_lcb)
532     {
533         L2CAP_TRACE_WARNING("l2cble_process_conn_update_evt: Invalid handle: %d", handle);
534         return;
535     }
536 
537     p_lcb->conn_update_mask &= ~L2C_BLE_UPDATE_PENDING;
538 
539     if (status != HCI_SUCCESS)
540     {
541         L2CAP_TRACE_WARNING("l2cble_process_conn_update_evt: Error status: %d", status);
542     }
543 
544     l2cble_start_conn_update(p_lcb);
545 
546     L2CAP_TRACE_DEBUG("l2cble_process_conn_update_evt: conn_update_mask=%d", p_lcb->conn_update_mask);
547 }
548 /*******************************************************************************
549 **
550 ** Function         l2cble_process_sig_cmd
551 **
552 ** Description      This function is called when a signalling packet is received
553 **                  on the BLE signalling CID
554 **
555 ** Returns          void
556 **
557 *******************************************************************************/
l2cble_process_sig_cmd(tL2C_LCB * p_lcb,UINT8 * p,UINT16 pkt_len)558 void l2cble_process_sig_cmd (tL2C_LCB *p_lcb, UINT8 *p, UINT16 pkt_len)
559 {
560     UINT8           *p_pkt_end;
561     UINT8           cmd_code, id;
562     UINT16          cmd_len, rej_reason;
563     UINT16          result;
564     UINT16          min_interval, max_interval, latency, timeout;
565 
566     p_pkt_end = p + pkt_len;
567 
568     STREAM_TO_UINT8  (cmd_code, p);
569     STREAM_TO_UINT8  (id, p);
570     STREAM_TO_UINT16 (cmd_len, p);
571 
572     /* Check command length does not exceed packet length */
573     if ((p + cmd_len) > p_pkt_end)
574     {
575         L2CAP_TRACE_WARNING ("L2CAP - LE - format error, pkt_len: %d  cmd_len: %d  code: %d", pkt_len, cmd_len, cmd_code);
576         return;
577     }
578 
579     switch (cmd_code)
580     {
581         case L2CAP_CMD_REJECT:
582         case L2CAP_CMD_ECHO_RSP:
583         case L2CAP_CMD_INFO_RSP:
584             STREAM_TO_UINT16 (rej_reason, p);
585             break;
586         case L2CAP_CMD_ECHO_REQ:
587         case L2CAP_CMD_INFO_REQ:
588             l2cu_send_peer_cmd_reject (p_lcb, L2CAP_CMD_REJ_NOT_UNDERSTOOD, id, 0, 0);
589             break;
590 
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             {
599                 if (min_interval < BTM_BLE_CONN_INT_MIN || min_interval > BTM_BLE_CONN_INT_MAX ||
600                     max_interval < BTM_BLE_CONN_INT_MIN || max_interval > BTM_BLE_CONN_INT_MAX ||
601                     latency  > BTM_BLE_CONN_LATENCY_MAX ||
602                     /*(timeout >= max_interval && latency > (timeout * 10/(max_interval * 1.25) - 1)) ||*/
603                     timeout < BTM_BLE_CONN_SUP_TOUT_MIN || timeout > BTM_BLE_CONN_SUP_TOUT_MAX ||
604                     max_interval < min_interval)
605                 {
606                     l2cu_send_peer_ble_par_rsp (p_lcb, L2CAP_CFG_UNACCEPTABLE_PARAMS, id);
607                 }
608                 else
609                 {
610 
611                     l2cu_send_peer_ble_par_rsp (p_lcb, L2CAP_CFG_OK, id);
612 
613                      p_lcb->min_interval = min_interval;
614                      p_lcb->max_interval = max_interval;
615                      p_lcb->latency = latency;
616                      p_lcb->timeout = timeout;
617                      p_lcb->conn_update_mask |= L2C_BLE_NEW_CONN_PARAM;
618 
619                      l2cble_start_conn_update(p_lcb);
620                 }
621             }
622             else
623                 l2cu_send_peer_cmd_reject (p_lcb, L2CAP_CMD_REJ_NOT_UNDERSTOOD, id, 0, 0);
624             break;
625 
626         case L2CAP_CMD_BLE_UPDATE_RSP:
627             STREAM_TO_UINT16 (result, p);
628             break;
629 
630         default:
631             L2CAP_TRACE_WARNING ("L2CAP - LE - unknown cmd code: %d", cmd_code);
632             l2cu_send_peer_cmd_reject (p_lcb, L2CAP_CMD_REJ_NOT_UNDERSTOOD, id, 0, 0);
633             return;
634     }
635 }
636 
637 /*******************************************************************************
638 **
639 ** Function         l2cble_init_direct_conn
640 **
641 ** Description      This function is to initate a direct connection
642 **
643 ** Returns          TRUE connection initiated, FALSE otherwise.
644 **
645 *******************************************************************************/
l2cble_init_direct_conn(tL2C_LCB * p_lcb)646 BOOLEAN l2cble_init_direct_conn (tL2C_LCB *p_lcb)
647 {
648     tBTM_SEC_DEV_REC    *p_dev_rec = btm_find_or_alloc_dev (p_lcb->remote_bd_addr);
649     tBTM_BLE_CB         *p_cb = &btm_cb.ble_ctr_cb;
650     UINT16               scan_int, scan_win;
651     BD_ADDR         init_addr;
652     UINT8           init_addr_type = BLE_ADDR_PUBLIC,
653                     own_addr_type = BLE_ADDR_PUBLIC;
654 
655     /* There can be only one BLE connection request outstanding at a time */
656     if (p_dev_rec == NULL)
657     {
658         L2CAP_TRACE_WARNING ("unknown device, can not initate connection");
659         return(FALSE);
660     }
661 
662     scan_int = (p_cb->scan_int == BTM_BLE_CONN_PARAM_UNDEF) ? BTM_BLE_SCAN_FAST_INT : p_cb->scan_int;
663     scan_win = (p_cb->scan_win == BTM_BLE_CONN_PARAM_UNDEF) ? BTM_BLE_SCAN_FAST_WIN : p_cb->scan_win;
664 
665     init_addr_type = p_lcb->ble_addr_type;
666     memcpy(init_addr, p_lcb->remote_bd_addr, BD_ADDR_LEN);
667 
668 #if BLE_PRIVACY_SPT == TRUE
669     /* if RPA offloading supported */
670     if (btm_ble_vendor_irk_list_load_dev(p_dev_rec))
671         btm_random_pseudo_to_public(init_addr, &init_addr_type);
672     /* otherwise, if remote is RPA enabled, use latest RPA */
673     else if (p_dev_rec->ble.active_addr_type == BTM_BLE_ADDR_RRA)
674     {
675         init_addr_type = BLE_ADDR_RANDOM;
676         memcpy(init_addr, p_dev_rec->ble.cur_rand_addr, BD_ADDR_LEN);
677     }
678     /* if privacy is on and current do not consider using reconnection address */
679     if (btm_cb.ble_ctr_cb.privacy ) /* && p_dev_rec->ble.use_reconn_addr */
680         own_addr_type = BLE_ADDR_RANDOM;
681 #endif
682 
683     if (!btm_ble_topology_check(BTM_BLE_STATE_INIT))
684     {
685         l2cu_release_lcb (p_lcb);
686         L2CAP_TRACE_ERROR("initate direct connection fail, topology limitation");
687         return FALSE;
688     }
689 
690     if (!btsnd_hcic_ble_create_ll_conn (scan_int,/* UINT16 scan_int      */
691                                         scan_win, /* UINT16 scan_win      */
692                                         FALSE,                   /* UINT8 white_list     */
693                                         init_addr_type,          /* UINT8 addr_type_peer */
694                                         init_addr,               /* BD_ADDR bda_peer     */
695                                         own_addr_type,         /* UINT8 addr_type_own  */
696         (UINT16) ((p_dev_rec->conn_params.min_conn_int != BTM_BLE_CONN_PARAM_UNDEF) ?
697         p_dev_rec->conn_params.min_conn_int : BTM_BLE_CONN_INT_MIN_DEF),  /* conn_int_min  */
698         (UINT16) ((p_dev_rec->conn_params.max_conn_int != BTM_BLE_CONN_PARAM_UNDEF) ?
699         p_dev_rec->conn_params.max_conn_int : BTM_BLE_CONN_INT_MAX_DEF),  /* conn_int_max  */
700         (UINT16) ((p_dev_rec->conn_params.slave_latency != BTM_BLE_CONN_PARAM_UNDEF) ?
701         p_dev_rec->conn_params.slave_latency : BTM_BLE_CONN_SLAVE_LATENCY_DEF), /* UINT16 conn_latency  */
702         (UINT16) ((p_dev_rec->conn_params.supervision_tout != BTM_BLE_CONN_PARAM_UNDEF) ?
703         p_dev_rec->conn_params.supervision_tout : BTM_BLE_CONN_TIMEOUT_DEF), /* conn_timeout */
704                                         0,                       /* UINT16 min_len       */
705                                         0))                      /* UINT16 max_len       */
706     {
707         l2cu_release_lcb (p_lcb);
708         L2CAP_TRACE_ERROR("initate direct connection fail, no resources");
709         return (FALSE);
710     }
711     else
712     {
713         p_lcb->link_state = LST_CONNECTING;
714         l2cb.is_ble_connecting = TRUE;
715         memcpy (l2cb.ble_connecting_bda, p_lcb->remote_bd_addr, BD_ADDR_LEN);
716         btu_start_timer (&p_lcb->timer_entry, BTU_TTYPE_L2CAP_LINK, L2CAP_BLE_LINK_CONNECT_TOUT);
717         btm_ble_set_conn_st (BLE_DIR_CONN);
718 
719         return (TRUE);
720     }
721 }
722 
723 /*******************************************************************************
724 **
725 ** Function         l2cble_create_conn
726 **
727 ** Description      This function initiates an acl connection via HCI
728 **
729 ** Returns          TRUE if successful, FALSE if connection not started.
730 **
731 *******************************************************************************/
l2cble_create_conn(tL2C_LCB * p_lcb)732 BOOLEAN l2cble_create_conn (tL2C_LCB *p_lcb)
733 {
734     tBTM_BLE_CONN_ST     conn_st = btm_ble_get_conn_st();
735     BOOLEAN         rt = FALSE;
736 
737     /* There can be only one BLE connection request outstanding at a time */
738     if (conn_st == BLE_CONN_IDLE)
739     {
740         rt = l2cble_init_direct_conn(p_lcb);
741     }
742     else
743     {
744         L2CAP_TRACE_WARNING ("L2CAP - LE - cannot start new connection at conn st: %d", conn_st);
745 
746         btm_ble_enqueue_direct_conn_req(p_lcb);
747 
748         if (conn_st == BLE_BG_CONN)
749             btm_ble_suspend_bg_conn();
750 
751         rt = TRUE;
752     }
753     return rt;
754 }
755 
756 /*******************************************************************************
757 **
758 ** Function         l2c_link_processs_ble_num_bufs
759 **
760 ** Description      This function is called when a "controller buffer size"
761 **                  event is first received from the controller. It updates
762 **                  the L2CAP values.
763 **
764 ** Returns          void
765 **
766 *******************************************************************************/
l2c_link_processs_ble_num_bufs(UINT16 num_lm_ble_bufs)767 void l2c_link_processs_ble_num_bufs (UINT16 num_lm_ble_bufs)
768 {
769     if (num_lm_ble_bufs == 0)
770     {
771         num_lm_ble_bufs = L2C_DEF_NUM_BLE_BUF_SHARED;
772         l2cb.num_lm_acl_bufs -= L2C_DEF_NUM_BLE_BUF_SHARED;
773     }
774 
775     l2cb.num_lm_ble_bufs = l2cb.controller_le_xmit_window = num_lm_ble_bufs;
776 }
777 
778 /*******************************************************************************
779 **
780 ** Function         l2c_ble_link_adjust_allocation
781 **
782 ** Description      This function is called when a link is created or removed
783 **                  to calculate the amount of packets each link may send to
784 **                  the HCI without an ack coming back.
785 **
786 **                  Currently, this is a simple allocation, dividing the
787 **                  number of Controller Packets by the number of links. In
788 **                  the future, QOS configuration should be examined.
789 **
790 ** Returns          void
791 **
792 *******************************************************************************/
l2c_ble_link_adjust_allocation(void)793 void l2c_ble_link_adjust_allocation (void)
794 {
795     UINT16      qq, yy, qq_remainder;
796     tL2C_LCB    *p_lcb;
797     UINT16      hi_quota, low_quota;
798     UINT16      num_lowpri_links = 0;
799     UINT16      num_hipri_links  = 0;
800     UINT16      controller_xmit_quota = l2cb.num_lm_ble_bufs;
801     UINT16      high_pri_link_quota = L2CAP_HIGH_PRI_MIN_XMIT_QUOTA_A;
802 
803     /* If no links active, reset buffer quotas and controller buffers */
804     if (l2cb.num_ble_links_active == 0)
805     {
806         l2cb.controller_le_xmit_window = l2cb.num_lm_ble_bufs;
807         l2cb.ble_round_robin_quota = l2cb.ble_round_robin_unacked = 0;
808         return;
809     }
810 
811     /* First, count the links */
812     for (yy = 0, p_lcb = &l2cb.lcb_pool[0]; yy < MAX_L2CAP_LINKS; yy++, p_lcb++)
813     {
814         if (p_lcb->in_use && p_lcb->transport == BT_TRANSPORT_LE)
815         {
816             if (p_lcb->acl_priority == L2CAP_PRIORITY_HIGH)
817                 num_hipri_links++;
818             else
819                 num_lowpri_links++;
820         }
821     }
822 
823     /* now adjust high priority link quota */
824     low_quota = num_lowpri_links ? 1 : 0;
825     while ( (num_hipri_links * high_pri_link_quota + low_quota) > controller_xmit_quota )
826         high_pri_link_quota--;
827 
828 
829     /* Work out the xmit quota and buffer quota high and low priorities */
830     hi_quota  = num_hipri_links * high_pri_link_quota;
831     low_quota = (hi_quota < controller_xmit_quota) ? controller_xmit_quota - hi_quota : 1;
832 
833     /* Work out and save the HCI xmit quota for each low priority link */
834 
835     /* If each low priority link cannot have at least one buffer */
836     if (num_lowpri_links > low_quota)
837     {
838         l2cb.ble_round_robin_quota = low_quota;
839         qq = qq_remainder = 1;
840     }
841     /* If each low priority link can have at least one buffer */
842     else if (num_lowpri_links > 0)
843     {
844         l2cb.ble_round_robin_quota = 0;
845         l2cb.ble_round_robin_unacked = 0;
846         qq = low_quota / num_lowpri_links;
847         qq_remainder = low_quota % num_lowpri_links;
848     }
849     /* If no low priority link */
850     else
851     {
852         l2cb.ble_round_robin_quota = 0;
853         l2cb.ble_round_robin_unacked = 0;
854         qq = qq_remainder = 1;
855     }
856     L2CAP_TRACE_EVENT ("l2c_ble_link_adjust_allocation  num_hipri: %u  num_lowpri: %u  low_quota: %u  round_robin_quota: %u  qq: %u",
857                         num_hipri_links, num_lowpri_links, low_quota,
858                         l2cb.ble_round_robin_quota, qq);
859 
860     /* Now, assign the quotas to each link */
861     for (yy = 0, p_lcb = &l2cb.lcb_pool[0]; yy < MAX_L2CAP_LINKS; yy++, p_lcb++)
862     {
863         if (p_lcb->in_use && p_lcb->transport == BT_TRANSPORT_LE)
864         {
865             if (p_lcb->acl_priority == L2CAP_PRIORITY_HIGH)
866             {
867                 p_lcb->link_xmit_quota   = high_pri_link_quota;
868             }
869             else
870             {
871                 /* Safety check in case we switched to round-robin with something outstanding */
872                 /* if sent_not_acked is added into round_robin_unacked then don't add it again */
873                 /* l2cap keeps updating sent_not_acked for exiting from round robin */
874                 if (( p_lcb->link_xmit_quota > 0 )&&( qq == 0 ))
875                     l2cb.ble_round_robin_unacked += p_lcb->sent_not_acked;
876 
877                 p_lcb->link_xmit_quota   = qq;
878                 if (qq_remainder > 0)
879                 {
880                     p_lcb->link_xmit_quota++;
881                     qq_remainder--;
882                 }
883             }
884 
885             L2CAP_TRACE_EVENT("l2c_ble_link_adjust_allocation LCB %d   Priority: %d  XmitQuota: %d",
886                                 yy, p_lcb->acl_priority, p_lcb->link_xmit_quota);
887 
888             L2CAP_TRACE_EVENT("        SentNotAcked: %d  RRUnacked: %d",
889                                 p_lcb->sent_not_acked, l2cb.round_robin_unacked);
890 
891             /* There is a special case where we have readjusted the link quotas and  */
892             /* this link may have sent anything but some other link sent packets so  */
893             /* so we may need a timer to kick off this link's transmissions.         */
894             if ( (p_lcb->link_state == LST_CONNECTED)
895               && (p_lcb->link_xmit_data_q.count)
896               && (p_lcb->sent_not_acked < p_lcb->link_xmit_quota) )
897                 btu_start_timer (&p_lcb->timer_entry, BTU_TTYPE_L2CAP_LINK, L2CAP_LINK_FLOW_CONTROL_TOUT);
898         }
899     }
900 }
901 
902 #if (defined BLE_LLT_INCLUDED) && (BLE_LLT_INCLUDED == TRUE)
903 /*******************************************************************************
904 **
905 ** Function         l2cble_process_rc_param_request_evt
906 **
907 ** Description      process LE Remote Connection Parameter Request Event.
908 **
909 ** Returns          void
910 **
911 *******************************************************************************/
l2cble_process_rc_param_request_evt(UINT16 handle,UINT16 int_min,UINT16 int_max,UINT16 latency,UINT16 timeout)912 void l2cble_process_rc_param_request_evt(UINT16 handle, UINT16 int_min, UINT16 int_max,
913                                      UINT16 latency, UINT16 timeout)
914 {
915     tL2C_LCB    *p_lcb = l2cu_find_lcb_by_handle (handle);
916 
917     if (p_lcb != NULL)
918     {
919         p_lcb->min_interval = int_min;
920         p_lcb->max_interval = int_max;
921         p_lcb->latency = latency;
922         p_lcb->timeout = timeout;
923 
924         /* if update is enabled, always accept connection parameter update */
925         if ((p_lcb->conn_update_mask & L2C_BLE_CONN_UPDATE_DISABLE) == 0)
926         {
927             btsnd_hcic_ble_rc_param_req_reply(handle, int_min, int_max, latency, timeout, 0, 0);
928         }
929         else
930         {
931             L2CAP_TRACE_EVENT ("L2CAP - LE - update currently disabled");
932             btsnd_hcic_ble_rc_param_req_neg_reply (handle,HCI_ERR_UNACCEPT_CONN_INTERVAL);
933         }
934 
935     }
936     else
937     {
938         L2CAP_TRACE_WARNING("No link to update connection parameter")
939     }
940 }
941 #endif
942 
943 
944 #endif /* (BLE_INCLUDED == TRUE) */
945