• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  *  Copyright (C) 2008-2014 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 for BLE GAP.
22  *
23  ******************************************************************************/
24 
25 #include <string.h>
26 #include <stdio.h>
27 #include <stddef.h>
28 
29 #include "bt_types.h"
30 #include "bt_utils.h"
31 #include "btm_int.h"
32 #include "btm_ble_api.h"
33 #include "btu.h"
34 #include "hcimsgs.h"
35 #if (GAP_INCLUDED == TRUE)
36 #include "gap_api.h"
37 #endif
38 
39 #if BLE_INCLUDED == TRUE
40 #include "l2c_int.h"
41 #include "vendor_ble.h"
42 
43 #include "gattdefs.h"
44 #include "gatt_int.h"
45 
46 #include "btm_ble_int.h"
47 
48 #define BTM_BLE_NAME_SHORT                  0x01
49 #define BTM_BLE_NAME_CMPL                   0x02
50 
51 #define BTM_BLE_FILTER_TARGET_UNKNOWN       0xff
52 #define BTM_BLE_POLICY_UNKNOWN              0xff
53 
54 #define BTM_EXT_BLE_RMT_NAME_TIMEOUT        30
55 #define MIN_ADV_LENGTH                       2
56 
57 static tBTM_BLE_VSC_CB cmn_ble_vsc_cb;
58 static tBTM_BLE_CTRL_FEATURES_CBACK    *p_ctrl_le_feature_rd_cmpl_cback = NULL;
59 
60 /*******************************************************************************
61 **  Local functions
62 *******************************************************************************/
63 static void btm_ble_update_adv_flag(UINT8 flag);
64 static void btm_ble_process_adv_pkt_cont(BD_ADDR bda, UINT8 addr_type, UINT8 evt_type, UINT8 *p);
65 UINT8 *btm_ble_build_adv_data(tBTM_BLE_AD_MASK *p_data_mask, UINT8 **p_dst,
66                               tBTM_BLE_ADV_DATA *p_data);
67 static UINT8 btm_set_conn_mode_adv_init_addr(tBTM_BLE_INQ_CB *p_cb,
68                                      BD_ADDR_PTR p_addr_ptr,
69                                      tBLE_ADDR_TYPE *p_init_addr_type,
70                                      tBLE_ADDR_TYPE *p_own_addr_type);
71 static void btm_ble_stop_observe(void);
72 
73 #define BTM_BLE_INQ_RESULT          0x01
74 #define BTM_BLE_OBS_RESULT          0x02
75 #define BTM_BLE_SEL_CONN_RESULT     0x04
76 
77 /* LE states combo bit to check */
78 const UINT8 btm_le_state_combo_tbl[BTM_BLE_STATE_MAX][BTM_BLE_STATE_MAX][2] =
79 {
80     {/* single state support */
81         {HCI_SUPP_LE_STATES_CONN_ADV_MASK, HCI_SUPP_LE_STATES_CONN_ADV_OFF},  /* conn_adv */
82         {HCI_SUPP_LE_STATES_INIT_MASK, HCI_SUPP_LE_STATES_INIT_OFF}, /* init */
83         {HCI_SUPP_LE_STATES_INIT_MASK, HCI_SUPP_LE_STATES_INIT_OFF}, /* master */
84         {HCI_SUPP_LE_STATES_SLAVE_MASK, HCI_SUPP_LE_STATES_SLAVE_OFF}, /* slave */
85         {0, 0},                   /* todo: lo du dir adv, not covered ? */
86         {HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_MASK, HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_OFF}, /* hi duty dir adv */
87         {HCI_SUPP_LE_STATES_NON_CONN_ADV_MASK, HCI_SUPP_LE_STATES_NON_CONN_ADV_OFF},  /* non connectable adv */
88         {HCI_SUPP_LE_STATES_PASS_SCAN_MASK, HCI_SUPP_LE_STATES_PASS_SCAN_OFF},   /*  passive scan */
89         {HCI_SUPP_LE_STATES_ACTIVE_SCAN_MASK, HCI_SUPP_LE_STATES_ACTIVE_SCAN_OFF},  /*   active scan */
90         {HCI_SUPP_LE_STATES_SCAN_ADV_MASK, HCI_SUPP_LE_STATESSCAN_ADV_OFF}   /* scanable adv */
91     },
92     {    /* conn_adv =0 */
93         {0, 0},                                                                           /* conn_adv */
94         {HCI_SUPP_LE_STATES_CONN_ADV_INIT_MASK, HCI_SUPP_LE_STATES_CONN_ADV_INIT_OFF},      /* init: 32 */
95         {HCI_SUPP_LE_STATES_CONN_ADV_MASTER_MASK, HCI_SUPP_LE_STATES_CONN_ADV_MASTER_OFF},  /* master: 35 */
96         {HCI_SUPP_LE_STATES_CONN_ADV_SLAVE_MASK, HCI_SUPP_LE_STATES_CONN_ADV_SLAVE_OFF}, /* slave: 38,*/
97         {0, 0},                                                                           /* lo du dir adv */
98         {0, 0},                                                                            /* hi duty dir adv */
99         {0, 0},  /* non connectable adv */
100         {HCI_SUPP_LE_STATES_CONN_ADV_PASS_SCAN_MASK, HCI_SUPP_LE_STATES_CONN_ADV_PASS_SCAN_OFF},   /*  passive scan */
101         {HCI_SUPP_LE_STATES_CONN_ADV_ACTIVE_SCAN_MASK, HCI_SUPP_LE_STATES_CONN_ADV_ACTIVE_SCAN_OFF},  /*   active scan */
102         {0, 0}   /* scanable adv */
103     },
104     {   /* init */
105         {HCI_SUPP_LE_STATES_CONN_ADV_INIT_MASK, HCI_SUPP_LE_STATES_CONN_ADV_INIT_OFF},      /* conn_adv: 32 */
106         {0, 0},                                                                             /* init */
107         {HCI_SUPP_LE_STATES_INIT_MASTER_MASK, HCI_SUPP_LE_STATES_INIT_MASTER_OFF},          /* master 28 */
108         {HCI_SUPP_LE_STATES_INIT_MASTER_SLAVE_MASK, HCI_SUPP_LE_STATES_INIT_MASTER_SLAVE_OFF}, /* slave 41 */
109         {HCI_SUPP_LE_STATES_LO_DUTY_DIR_ADV_INIT_MASK, HCI_SUPP_LE_STATES_LO_DUTY_DIR_ADV_INIT_OFF} ,/* lo du dir adv 34 */
110         {HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_INIT_MASK, HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_INIT_OFF},     /* hi duty dir adv 33 */
111         {HCI_SUPP_LE_STATES_NON_CONN_INIT_MASK, HCI_SUPP_LE_STATES_NON_CONN_INIT_OFF},  /*  non connectable adv */
112         {HCI_SUPP_LE_STATES_PASS_SCAN_INIT_MASK, HCI_SUPP_LE_STATES_PASS_SCAN_INIT_OFF},   /* passive scan */
113         {HCI_SUPP_LE_STATES_ACTIVE_SCAN_INIT_MASK, HCI_SUPP_LE_STATES_ACTIVE_SCAN_INIT_OFF},  /*  active scan */
114         {HCI_SUPP_LE_STATES_SCAN_ADV_INIT_MASK, HCI_SUPP_LE_STATES_SCAN_ADV_INIT_OFF}   /* scanable adv */
115 
116     },
117     {   /* master */
118         {HCI_SUPP_LE_STATES_CONN_ADV_MASTER_MASK, HCI_SUPP_LE_STATES_CONN_ADV_MASTER_OFF},  /* conn_adv: 35 */
119         {HCI_SUPP_LE_STATES_INIT_MASTER_MASK, HCI_SUPP_LE_STATES_INIT_MASTER_OFF},          /* init 28 */
120         {HCI_SUPP_LE_STATES_INIT_MASTER_MASK, HCI_SUPP_LE_STATES_INIT_MASTER_OFF},          /* master 28 */
121         {HCI_SUPP_LE_STATES_CONN_ADV_INIT_MASK, HCI_SUPP_LE_STATES_CONN_ADV_INIT_OFF},      /* slave: 32 */
122         {HCI_SUPP_LE_STATES_LO_DUTY_DIR_ADV_MASTER_MASK, HCI_SUPP_LE_STATES_LO_DUTY_DIR_ADV_MASTER_OFF},  /* lo duty cycle adv 37 */
123         {HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_MASTER_MASK, HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_MASTER_OFF},   /* hi duty cycle adv 36 */
124         {HCI_SUPP_LE_STATES_NON_CONN_ADV_MASTER_MASK, HCI_SUPP_LE_STATES_NON_CONN_ADV_MASTER_OFF},  /*  non connectable adv */
125         {HCI_SUPP_LE_STATES_PASS_SCAN_MASTER_MASK, HCI_SUPP_LE_STATES_PASS_SCAN_MASTER_OFF},   /*  passive scan */
126         {HCI_SUPP_LE_STATES_ACTIVE_SCAN_MASTER_MASK, HCI_SUPP_LE_STATES_ACTIVE_SCAN_MASTER_OFF},  /*   active scan */
127         {HCI_SUPP_LE_STATES_SCAN_ADV_MASTER_MASK, HCI_SUPP_LE_STATES_SCAN_ADV_MASTER_OFF}   /*  scanable adv */
128 
129     },
130     { /* slave */
131         {HCI_SUPP_LE_STATES_CONN_ADV_SLAVE_MASK, HCI_SUPP_LE_STATES_CONN_ADV_SLAVE_OFF}, /* conn_adv: 38,*/
132         {HCI_SUPP_LE_STATES_INIT_MASTER_SLAVE_MASK, HCI_SUPP_LE_STATES_INIT_MASTER_SLAVE_OFF}, /* init 41 */
133         {HCI_SUPP_LE_STATES_INIT_MASTER_SLAVE_MASK, HCI_SUPP_LE_STATES_INIT_MASTER_SLAVE_OFF}, /* master 41 */
134         {HCI_SUPP_LE_STATES_CONN_ADV_SLAVE_MASK, HCI_SUPP_LE_STATES_CONN_ADV_SLAVE_OFF},        /* slave: 38,*/
135         {HCI_SUPP_LE_STATES_LO_DUTY_DIR_ADV_SLAVE_MASK, HCI_SUPP_LE_STATES_LO_DUTY_DIR_ADV_SLAVE_OFF},  /* lo duty cycle adv 40 */
136         {HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_SLAVE_MASK, HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_SLAVE_OFF},   /* hi duty cycle adv 39 */
137         {HCI_SUPP_LE_STATES_NON_CONN_ADV_SLAVE_MASK, HCI_SUPP_LE_STATES_NON_CONN_ADV_SLAVE_OFF},  /* non connectable adv */
138         {HCI_SUPP_LE_STATES_PASS_SCAN_SLAVE_MASK, HCI_SUPP_LE_STATES_PASS_SCAN_SLAVE_OFF},   /* passive scan */
139         {HCI_SUPP_LE_STATES_ACTIVE_SCAN_SLAVE_MASK, HCI_SUPP_LE_STATES_ACTIVE_SCAN_SLAVE_OFF},  /*  active scan */
140         {HCI_SUPP_LE_STATES_SCAN_ADV_SLAVE_MASK, HCI_SUPP_LE_STATES_SCAN_ADV_SLAVE_OFF}   /* scanable adv */
141 
142     },
143     { /* lo duty cycle adv */
144         {0, 0}, /* conn_adv: 38,*/
145         {HCI_SUPP_LE_STATES_LO_DUTY_DIR_ADV_INIT_MASK, HCI_SUPP_LE_STATES_LO_DUTY_DIR_ADV_INIT_OFF} ,/* init 34 */
146         {HCI_SUPP_LE_STATES_LO_DUTY_DIR_ADV_MASTER_MASK, HCI_SUPP_LE_STATES_LO_DUTY_DIR_ADV_MASTER_OFF}, /* master 37 */
147         {HCI_SUPP_LE_STATES_LO_DUTY_DIR_ADV_SLAVE_MASK, HCI_SUPP_LE_STATES_LO_DUTY_DIR_ADV_SLAVE_OFF}, /* slave: 40 */
148         {0, 0},  /* lo duty cycle adv 40 */
149         {0, 0},   /* hi duty cycle adv 39 */
150         {0, 0},  /*  non connectable adv */
151         {0, 0},   /* TODO: passive scan, not covered? */
152         {0, 0},  /* TODO:  active scan, not covered? */
153         {0, 0}   /*  scanable adv */
154     },
155     { /* hi duty cycle adv */
156         {0, 0}, /* conn_adv: 38,*/
157         {HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_INIT_MASK, HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_INIT_OFF}, /* init 33 */
158         {HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_MASTER_MASK, HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_MASTER_OFF}, /* master 36 */
159         {HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_SLAVE_MASK, HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_SLAVE_OFF},   /* slave: 39*/
160         {0, 0},  /* lo duty cycle adv 40 */
161         {0, 0},   /* hi duty cycle adv 39 */
162         {0, 0},  /* non connectable adv */
163         {HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_PASS_SCAN_MASK, HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_PASS_SCAN_OFF},   /* passive scan */
164         {HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_ACTIVE_SCAN_MASK, HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_ACTIVE_SCAN_OFF},  /* active scan */
165         {0, 0}   /* scanable adv */
166     },
167     { /* non connectable adv */
168         {0, 0}, /* conn_adv: */
169         {HCI_SUPP_LE_STATES_NON_CONN_INIT_MASK, HCI_SUPP_LE_STATES_NON_CONN_INIT_OFF}, /* init  */
170         {HCI_SUPP_LE_STATES_NON_CONN_ADV_MASTER_MASK, HCI_SUPP_LE_STATES_NON_CONN_ADV_MASTER_OFF}, /* master  */
171         {HCI_SUPP_LE_STATES_NON_CONN_ADV_SLAVE_MASK, HCI_SUPP_LE_STATES_NON_CONN_ADV_SLAVE_OFF},   /* slave: */
172         {0, 0},  /* lo duty cycle adv */
173         {0, 0},   /* hi duty cycle adv */
174         {0, 0},  /* non connectable adv */
175         {HCI_SUPP_LE_STATES_NON_CONN_ADV_PASS_SCAN_MASK, HCI_SUPP_LE_STATES_NON_CONN_ADV_PASS_SCAN_OFF},   /* passive scan */
176         {HCI_SUPP_LE_STATES_NON_CONN_ADV_ACTIVE_SCAN_MASK, HCI_SUPP_LE_STATES_NON_CONN_ADV_ACTIVE_SCAN_OFF},  /*  active scan */
177         {0, 0}   /* scanable adv */
178     },
179     { /* passive scan */
180         {HCI_SUPP_LE_STATES_CONN_ADV_PASS_SCAN_MASK, HCI_SUPP_LE_STATES_CONN_ADV_PASS_SCAN_OFF}, /* conn_adv: */
181         {HCI_SUPP_LE_STATES_PASS_SCAN_INIT_MASK, HCI_SUPP_LE_STATES_PASS_SCAN_INIT_OFF}, /* init  */
182         {HCI_SUPP_LE_STATES_PASS_SCAN_MASTER_MASK, HCI_SUPP_LE_STATES_PASS_SCAN_MASTER_OFF}, /* master  */
183         {HCI_SUPP_LE_STATES_PASS_SCAN_SLAVE_MASK, HCI_SUPP_LE_STATES_PASS_SCAN_SLAVE_OFF},   /* slave: */
184         {0, 0},  /* lo duty cycle adv */
185         {HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_PASS_SCAN_MASK, HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_PASS_SCAN_OFF},   /* hi duty cycle adv */
186         {HCI_SUPP_LE_STATES_NON_CONN_ADV_PASS_SCAN_MASK, HCI_SUPP_LE_STATES_NON_CONN_ADV_PASS_SCAN_OFF},  /*  non connectable adv */
187         {0, 0},   /* passive scan */
188         {0, 0},  /* active scan */
189          {HCI_SUPP_LE_STATES_SCAN_ADV_PASS_SCAN_MASK, HCI_SUPP_LE_STATES_SCAN_ADV_PASS_SCAN_OFF}   /* scanable adv */
190     },
191     { /* active scan */
192         {HCI_SUPP_LE_STATES_CONN_ADV_ACTIVE_SCAN_MASK, HCI_SUPP_LE_STATES_CONN_ADV_ACTIVE_SCAN_OFF}, /* conn_adv: */
193         {HCI_SUPP_LE_STATES_ACTIVE_SCAN_INIT_MASK, HCI_SUPP_LE_STATES_ACTIVE_SCAN_INIT_OFF}, /* init  */
194         {HCI_SUPP_LE_STATES_ACTIVE_SCAN_MASTER_MASK, HCI_SUPP_LE_STATES_ACTIVE_SCAN_MASTER_OFF}, /* master  */
195         {HCI_SUPP_LE_STATES_ACTIVE_SCAN_SLAVE_MASK, HCI_SUPP_LE_STATES_ACTIVE_SCAN_SLAVE_OFF},   /* slave: */
196         {0, 0},  /* lo duty cycle adv */
197         {HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_ACTIVE_SCAN_MASK, HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_ACTIVE_SCAN_OFF},   /* hi duty cycle adv */
198         {HCI_SUPP_LE_STATES_NON_CONN_ADV_ACTIVE_SCAN_MASK, HCI_SUPP_LE_STATES_NON_CONN_ADV_ACTIVE_SCAN_OFF},  /*  non connectable adv */
199         {0, 0},   /* TODO: passive scan */
200         {0, 0},  /* TODO:  active scan */
201         {HCI_SUPP_LE_STATES_SCAN_ADV_ACTIVE_SCAN_MASK, HCI_SUPP_LE_STATES_SCAN_ADV_ACTIVE_SCAN_OFF}   /*  scanable adv */
202     },
203     { /* scanable adv */
204         {0, 0}, /* conn_adv: */
205         {HCI_SUPP_LE_STATES_SCAN_ADV_INIT_MASK, HCI_SUPP_LE_STATES_SCAN_ADV_INIT_OFF}, /* init  */
206         {HCI_SUPP_LE_STATES_SCAN_ADV_MASTER_MASK, HCI_SUPP_LE_STATES_SCAN_ADV_MASTER_OFF}, /* master  */
207         {HCI_SUPP_LE_STATES_SCAN_ADV_SLAVE_MASK, HCI_SUPP_LE_STATES_SCAN_ADV_SLAVE_OFF},   /* slave: */
208         {0, 0},  /* lo duty cycle adv */
209         {0, 0},   /* hi duty cycle adv */
210         {0, 0},  /* non connectable adv */
211         {HCI_SUPP_LE_STATES_SCAN_ADV_PASS_SCAN_MASK, HCI_SUPP_LE_STATES_SCAN_ADV_PASS_SCAN_OFF},   /*  passive scan */
212         {HCI_SUPP_LE_STATES_SCAN_ADV_ACTIVE_SCAN_MASK, HCI_SUPP_LE_STATES_SCAN_ADV_ACTIVE_SCAN_OFF},  /*  active scan */
213         {0, 0}   /* scanable adv */
214     }
215 
216 };
217 /* check LE combo state supported */
218 #define BTM_LE_STATES_SUPPORTED(x, y, z)      ((x)[(z)] & (y))
219 
220 
221 /*******************************************************************************
222 **
223 ** Function         BTM_BleUpdateAdvWhitelist
224 **
225 ** Description      Add or remove device from advertising white list
226 **
227 ** Returns          void
228 **
229 *******************************************************************************/
BTM_BleUpdateAdvWhitelist(BOOLEAN add_remove,BD_ADDR remote_bda)230 BOOLEAN BTM_BleUpdateAdvWhitelist(BOOLEAN add_remove, BD_ADDR remote_bda)
231 {
232     UNUSED(add_remove);
233     UNUSED(remote_bda);
234 
235     return FALSE;
236 }
237 
238 /*******************************************************************************
239 **
240 ** Function         BTM_BleUpdateAdvFilterPolicy
241 **
242 ** Description      This function update the filter policy of advertiser.
243 **
244 ** Parameter        adv_policy: advertising filter policy
245 **
246 ** Return           void
247 *******************************************************************************/
BTM_BleUpdateAdvFilterPolicy(tBTM_BLE_AFP adv_policy)248 void BTM_BleUpdateAdvFilterPolicy(tBTM_BLE_AFP adv_policy)
249 {
250     tBTM_BLE_INQ_CB *p_cb = &btm_cb.ble_ctr_cb.inq_var;
251     tBLE_ADDR_TYPE   init_addr_type = BLE_ADDR_PUBLIC;
252     BD_ADDR          p_addr_ptr= {0};
253     UINT8            adv_mode = p_cb->adv_mode;
254 
255     BTM_TRACE_EVENT ("BTM_BleUpdateAdvFilterPolicy");
256 
257     if (!HCI_LE_HOST_SUPPORTED(btm_cb.devcb.local_lmp_features[HCI_EXT_FEATURES_PAGE_1]))
258         return;
259 
260     if (p_cb->afp != adv_policy)
261     {
262         p_cb->afp = adv_policy;
263 
264         /* if adv active, stop and restart */
265         btm_ble_stop_adv ();
266 
267         if (p_cb->connectable_mode & BTM_BLE_CONNECTABLE)
268             p_cb->evt_type = btm_set_conn_mode_adv_init_addr(p_cb, p_addr_ptr, &init_addr_type,
269                                                               &p_cb->adv_addr_type);
270 
271         btsnd_hcic_ble_write_adv_params ((UINT16)(p_cb->adv_interval_min ? p_cb->adv_interval_min :
272                                          BTM_BLE_GAP_ADV_SLOW_INT),
273                                          (UINT16)(p_cb->adv_interval_max ? p_cb->adv_interval_max :
274                                          BTM_BLE_GAP_ADV_SLOW_INT),
275                                          p_cb->evt_type,
276                                          p_cb->adv_addr_type,
277                                          init_addr_type,
278                                          p_addr_ptr,
279                                          p_cb->adv_chnl_map,
280                                          p_cb->afp);
281 
282         if (adv_mode == BTM_BLE_ADV_ENABLE)
283             btm_ble_start_adv ();
284 
285     }
286 }
287 /*******************************************************************************
288 **
289 ** Function         BTM_BleObserve
290 **
291 ** Description      This procedure keep the device listening for advertising
292 **                  events from a broadcast device.
293 **
294 ** Parameters       start: start or stop observe.
295 **                  white_list: use white list in observer mode or not.
296 **
297 ** Returns          void
298 **
299 *******************************************************************************/
BTM_BleObserve(BOOLEAN start,UINT8 duration,tBTM_INQ_RESULTS_CB * p_results_cb,tBTM_CMPL_CB * p_cmpl_cb)300 tBTM_STATUS BTM_BleObserve(BOOLEAN start, UINT8 duration,
301                            tBTM_INQ_RESULTS_CB *p_results_cb, tBTM_CMPL_CB *p_cmpl_cb)
302 {
303     tBTM_BLE_INQ_CB *p_inq = &btm_cb.ble_ctr_cb.inq_var;
304     tBTM_STATUS     status = BTM_WRONG_MODE;
305 
306     BTM_TRACE_EVENT ("BTM_BleObserve : scan_type:%d",btm_cb.btm_inq_vars.scan_type);
307 
308     if (!HCI_LE_HOST_SUPPORTED(btm_cb.devcb.local_lmp_features[HCI_EXT_FEATURES_PAGE_1]))
309         return BTM_ILLEGAL_VALUE;
310 
311     if (start)
312     {
313         /* shared inquiry database, do not allow observe if any inquiry is active */
314         if (BTM_BLE_IS_OBS_ACTIVE(btm_cb.ble_ctr_cb.scan_activity))
315         {
316             BTM_TRACE_ERROR("Observe Already Active");
317             return status;
318         }
319 
320         btm_cb.ble_ctr_cb.p_obs_results_cb = p_results_cb;
321         btm_cb.ble_ctr_cb.p_obs_cmpl_cb = p_cmpl_cb;
322         status = BTM_CMD_STARTED;
323 
324         /* scan is not started */
325         if (!BTM_BLE_IS_SCAN_ACTIVE(btm_cb.ble_ctr_cb.scan_activity))
326         {
327             p_inq->scan_type = (p_inq->scan_type == BTM_BLE_SCAN_MODE_NONE) ? BTM_BLE_SCAN_MODE_ACTI: p_inq->scan_type;
328             /* allow config scanning type */
329             btsnd_hcic_ble_set_scan_params (p_inq->scan_type,
330                                             (UINT16)(!p_inq->scan_interval ? BTM_BLE_GAP_DISC_SCAN_INT : p_inq->scan_interval),
331                                             (UINT16)(!p_inq->scan_window ? BTM_BLE_GAP_DISC_SCAN_WIN : p_inq->scan_window),
332                                             btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type,
333                                             BTM_BLE_DEFAULT_SFP); /* assume observe always not using white list */
334 
335 #if (defined BLE_PRIVACY_SPT && BLE_PRIVACY_SPT == TRUE)
336             /* enable IRK list */
337             btm_ble_vendor_irk_list_known_dev (TRUE);
338 #endif
339             status = btm_ble_start_scan(BTM_BLE_DUPLICATE_DISABLE);
340         }
341         if (status == BTM_CMD_STARTED)
342         {
343             btm_cb.ble_ctr_cb.scan_activity |= BTM_LE_OBSERVE_ACTIVE;
344 
345             if (duration != 0)
346                 /* start observer timer */
347                 btu_start_timer (&btm_cb.ble_ctr_cb.obs_timer_ent, BTU_TTYPE_BLE_OBSERVE, duration);
348         }
349     }
350     else if (BTM_BLE_IS_OBS_ACTIVE(btm_cb.ble_ctr_cb.scan_activity))
351     {
352         status = BTM_CMD_STARTED;
353         btm_ble_stop_observe();
354     }
355     else
356     {
357         BTM_TRACE_ERROR("Observe not active");
358     }
359 
360     return status;
361 
362 }
363 
364 /*******************************************************************************
365 **
366 ** Function         BTM_BleBroadcast
367 **
368 ** Description      This function is to start or stop broadcasting.
369 **
370 ** Parameters       start: start or stop broadcasting.
371 **
372 ** Returns          status.
373 **
374 *******************************************************************************/
BTM_BleBroadcast(BOOLEAN start)375 tBTM_STATUS BTM_BleBroadcast(BOOLEAN start)
376 {
377     tBTM_STATUS status = BTM_NO_RESOURCES;
378     tBTM_LE_RANDOM_CB *p_addr_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
379     tBTM_BLE_INQ_CB *p_cb = &btm_cb.ble_ctr_cb.inq_var;
380     UINT8 evt_type = p_cb->scan_rsp ? BTM_BLE_DISCOVER_EVT: BTM_BLE_NON_CONNECT_EVT;
381 
382     if (!HCI_LE_HOST_SUPPORTED(btm_cb.devcb.local_lmp_features[HCI_EXT_FEATURES_PAGE_1]))
383         return BTM_ILLEGAL_VALUE;
384 
385 #ifdef  BTM_BLE_PC_ADV_TEST_MODE
386     if (BTM_BLE_PC_ADV_TEST_MODE)
387     {
388         evt_type = p_cb->scan_rsp ? BTM_BLE_CONNECT_EVT: BTM_BLE_NON_CONNECT_EVT;
389     }
390 #endif
391 
392     if (start && p_cb->adv_mode == BTM_BLE_ADV_DISABLE)
393     {
394         /* update adv params */
395         if (!btsnd_hcic_ble_write_adv_params ((UINT16)(p_cb->adv_interval_min ? p_cb->adv_interval_min :
396                                               BTM_BLE_GAP_ADV_INT),
397                                               (UINT16)(p_cb->adv_interval_max ? p_cb->adv_interval_max :
398                                               BTM_BLE_GAP_ADV_INT),
399                                               evt_type,
400                                               p_addr_cb->own_addr_type,
401                                               p_cb->direct_bda.type,
402                                               p_cb->direct_bda.bda,
403                                               p_cb->adv_chnl_map,
404                                               p_cb->afp))
405 
406             status = BTM_NO_RESOURCES;
407         else
408             p_cb->evt_type = evt_type;
409 
410         status = btm_ble_start_adv ();
411     }
412     else if (!start)
413     {
414         status = btm_ble_stop_adv();
415     }
416     else
417     {
418         status = BTM_WRONG_MODE;
419         BTM_TRACE_ERROR("Can not %s Broadcast, device %s in Broadcast mode",
420             (start ? "Start" : "Stop"), (start ? "already" :"not"));
421     }
422     return status;
423 }
424 
425 /*******************************************************************************
426 **
427 ** Function         btm_vsc_brcm_features_complete
428 **
429 ** Description      Command Complete callback for HCI_BLE_VENDOR_CAP_OCF
430 **
431 ** Returns          void
432 **
433 *******************************************************************************/
btm_ble_vendor_capability_vsc_cmpl_cback(tBTM_VSC_CMPL * p_vcs_cplt_params)434 static void btm_ble_vendor_capability_vsc_cmpl_cback (tBTM_VSC_CMPL *p_vcs_cplt_params)
435 {
436 #if BLE_VND_INCLUDED == TRUE
437     UINT8  status = 0xFF, *p;
438 
439     BTM_TRACE_DEBUG("btm_ble_vendor_capability_vsc_cmpl_cback");
440 
441     /* Check status of command complete event */
442     if ((p_vcs_cplt_params->opcode == HCI_BLE_VENDOR_CAP_OCF) &&(p_vcs_cplt_params->param_len > 0))
443     {
444         p = p_vcs_cplt_params->p_param_buf;
445         STREAM_TO_UINT8  (status, p);
446     }
447 
448     if (status == HCI_SUCCESS)
449     {
450         STREAM_TO_UINT8  (btm_cb.cmn_ble_vsc_cb.adv_inst_max, p);
451         STREAM_TO_UINT8  (btm_cb.cmn_ble_vsc_cb.rpa_offloading, p);
452         STREAM_TO_UINT16 (btm_cb.cmn_ble_vsc_cb.tot_scan_results_strg, p);
453         STREAM_TO_UINT8  (btm_cb.cmn_ble_vsc_cb.max_irk_list_sz, p);
454         STREAM_TO_UINT8  (btm_cb.cmn_ble_vsc_cb.filter_support, p);
455         STREAM_TO_UINT8  (btm_cb.cmn_ble_vsc_cb.max_filter, p);
456         STREAM_TO_UINT8  (btm_cb.cmn_ble_vsc_cb.energy_support, p);
457         btm_cb.cmn_ble_vsc_cb.values_read = TRUE;
458     }
459 
460     BTM_TRACE_DEBUG("btm_ble_vnd_cap_vsc_cmpl_cback: stat=%d, irk=%d, ADV ins:%d, rpa=%d, ener=%d",
461          status, btm_ble_vendor_cb.irk_avail_size, btm_cb.cmn_ble_vsc_cb.adv_inst_max,
462          btm_cb.cmn_ble_vsc_cb.rpa_offloading, btm_cb.cmn_ble_vsc_cb.energy_support);
463 
464     if (BTM_BleMaxMultiAdvInstanceCount() > 0)
465         btm_ble_multi_adv_init();
466 
467     if (btm_cb.cmn_ble_vsc_cb.max_filter > 0)
468     {
469         btm_ble_adv_filter_init();
470     }
471 
472     if (btm_cb.cmn_ble_vsc_cb.max_irk_list_sz > 0)
473     {
474         btm_ble_vendor_init(btm_cb.cmn_ble_vsc_cb.max_irk_list_sz);
475     }
476 
477     if (btm_cb.cmn_ble_vsc_cb.tot_scan_results_strg > 0)
478         btm_ble_batchscan_init();
479 
480     if (p_ctrl_le_feature_rd_cmpl_cback != NULL)
481         p_ctrl_le_feature_rd_cmpl_cback(status);
482 
483 #else
484     UNUSED(p_vcs_cplt_params);
485 #endif
486 }
487 
488 /*******************************************************************************
489 **
490 ** Function         BTM_BleGetVendorCapabilities
491 **
492 ** Description      This function reads local LE features
493 **
494 ** Parameters       p_cmn_vsc_cb : Locala LE capability structure
495 **
496 ** Returns          void
497 **
498 *******************************************************************************/
BTM_BleGetVendorCapabilities(tBTM_BLE_VSC_CB * p_cmn_vsc_cb)499 BTM_API extern void BTM_BleGetVendorCapabilities(tBTM_BLE_VSC_CB *p_cmn_vsc_cb)
500 {
501     BTM_TRACE_DEBUG("BTM_BleGetVendorCapabilities");
502 
503     if (NULL != p_cmn_vsc_cb)
504     {
505         *p_cmn_vsc_cb = btm_cb.cmn_ble_vsc_cb;
506     }
507 }
508 
509 /******************************************************************************
510 **
511 ** Function         BTM_BleReadControllerFeatures
512 **
513 ** Description      Reads BLE specific controller features
514 **
515 ** Parameters:      tBTM_BLE_CTRL_FEATURES_CBACK : Callback to notify when features are read
516 **
517 ** Returns          void
518 **
519 *******************************************************************************/
BTM_BleReadControllerFeatures(tBTM_BLE_CTRL_FEATURES_CBACK * p_vsc_cback)520 BTM_API extern void BTM_BleReadControllerFeatures(tBTM_BLE_CTRL_FEATURES_CBACK  *p_vsc_cback)
521 {
522     if (TRUE == btm_cb.cmn_ble_vsc_cb.values_read)
523         return;
524 
525 #if BLE_VND_INCLUDED == TRUE
526     BTM_TRACE_DEBUG("BTM_BleReadControllerFeatures");
527 
528     p_ctrl_le_feature_rd_cmpl_cback = p_vsc_cback;
529     if ( BTM_VendorSpecificCommand (HCI_BLE_VENDOR_CAP_OCF,
530                                     0,
531                                     NULL,
532                                     btm_ble_vendor_capability_vsc_cmpl_cback)
533                                     != BTM_CMD_STARTED)
534     {
535         BTM_TRACE_ERROR("LE Get_Vendor Capabilities Command Failed.");
536     }
537 #else
538     UNUSED(p_vsc_cback);
539 #endif
540     return ;
541 }
542 
543 /*******************************************************************************
544 **
545 ** Function         BTM_RegisterScanReqEvt
546 **
547 ** Description      This function is called to register a scan request callback
548 **                  on the advertiser.
549 **
550 ** Parameters       p_scan_req_cback: scan request callback.  If NULL, remove the
551 **                                    registration.
552 **
553 ** Returns          void
554 **
555 *******************************************************************************/
BTM_RegisterScanReqEvt(tBTM_BLE_SCAN_REQ_CBACK * p_scan_req_cback)556 void BTM_RegisterScanReqEvt(tBTM_BLE_SCAN_REQ_CBACK   *p_scan_req_cback)
557 {
558 #ifdef BTM_BLE_PC_ADV_TEST_MODE /* For general stack code (e.g. BTInsight testing), we simply do not define it to exclude or set it to TRUE to include */
559     if (BTM_BLE_PC_ADV_TEST_MODE)   /* For stack component, it is always defined and maps to a global variable g_bDraculaAdvertisingMode */
560     {
561         tBTM_BLE_CB *p_cb = &btm_cb.ble_ctr_cb;
562         p_cb->p_scan_req_cback = p_scan_req_cback;
563     }
564 #else
565     UNUSED(p_scan_req_cback);
566 #endif
567 }
568 
569 /*******************************************************************************
570 **
571 ** Function         BTM_BleConfigPrivacy
572 **
573 ** Description      This function is called to enable or disable the privacy in
574 **                   LE channel of the local device.
575 **
576 ** Parameters       enable: TRUE to enable it; FALSE to disable it.
577 **
578 ** Returns          void
579 **
580 *******************************************************************************/
BTM_BleConfigPrivacy(BOOLEAN enable)581 void BTM_BleConfigPrivacy(BOOLEAN enable)
582 {
583     tBTM_BLE_CB     *p_cb = &btm_cb.ble_ctr_cb;
584 
585     BTM_TRACE_EVENT (" BTM_BleConfigPrivacy");
586 
587     if (p_cb->privacy != enable)
588     {
589         p_cb->privacy = enable;
590 
591         if (p_cb->privacy)
592         {
593             /* generate resolvable private address */
594             btm_gen_resolvable_private_addr((void*)btm_gen_resolve_paddr_low);
595         }
596         else /* if privacy disabled, always use public address */
597         {
598             p_cb->addr_mgnt_cb.own_addr_type = BLE_ADDR_PUBLIC;
599         }
600 
601         if (BTM_BleMaxMultiAdvInstanceCount() > 0)
602             btm_ble_multi_adv_enb_privacy(p_cb->privacy);
603     }
604 }
605 
606 /*******************************************************************************
607 **
608 ** Function          BTM_BleMaxMultiAdvInstanceCount
609 **
610 ** Description        Returns max number of multi adv instances supported by controller
611 **
612 ** Returns          Max multi adv instance count
613 **
614 *******************************************************************************/
BTM_BleMaxMultiAdvInstanceCount()615 BTM_API extern UINT8  BTM_BleMaxMultiAdvInstanceCount()
616 {
617     return btm_cb.cmn_ble_vsc_cb.adv_inst_max < BTM_BLE_MULTI_ADV_MAX ?
618         btm_cb.cmn_ble_vsc_cb.adv_inst_max : BTM_BLE_MULTI_ADV_MAX;
619 }
620 
621 #if BLE_PRIVACY_SPT == TRUE
622 /*******************************************************************************
623 **
624 ** Function         btm_ble_resolve_random_addr_on_adv
625 **
626 ** Description      resolve random address complete callback.
627 **
628 ** Returns          void
629 **
630 *******************************************************************************/
btm_ble_resolve_random_addr_on_adv(void * p_rec,void * p)631 static void btm_ble_resolve_random_addr_on_adv(void * p_rec, void *p)
632 {
633     tBTM_SEC_DEV_REC    *match_rec = (tBTM_SEC_DEV_REC *) p_rec;
634     UINT8       addr_type = BLE_ADDR_RANDOM;
635     BD_ADDR     bda;
636     UINT8       *pp = (UINT8 *)p + 1;
637     UINT8           evt_type;
638 
639     BTM_TRACE_EVENT ("btm_ble_resolve_random_addr_on_adv ");
640 
641     STREAM_TO_UINT8    (evt_type, pp);
642     STREAM_TO_UINT8    (addr_type, pp);
643     STREAM_TO_BDADDR   (bda, pp);
644 
645     if (match_rec)
646     {
647         BTM_TRACE_ERROR("Random match");
648         match_rec->ble.active_addr_type = BTM_BLE_ADDR_RRA;
649         memcpy(match_rec->ble.cur_rand_addr, bda, BD_ADDR_LEN);
650         memcpy(bda, match_rec->bd_addr, BD_ADDR_LEN);
651         addr_type = match_rec->ble.ble_addr_type;
652     }
653     else
654     {
655         BTM_TRACE_ERROR("Random unmatch");
656     }
657 
658     btm_ble_process_adv_pkt_cont(bda, addr_type, evt_type, pp);
659 
660     return;
661 }
662 #endif
663 
664 /*******************************************************************************
665 **
666 ** Function         BTM_BleLocalPrivacyEnabled
667 **
668 ** Description        Checks if local device supports private address
669 **
670 ** Returns          Return TRUE if local privacy is enabled else FALSE
671 **
672 *******************************************************************************/
BTM_BleLocalPrivacyEnabled()673 BOOLEAN BTM_BleLocalPrivacyEnabled()
674 {
675 #if BLE_PRIVACY_SPT == TRUE
676     return btm_cb.ble_ctr_cb.privacy;
677 #else
678     return false;
679 #endif
680 }
681 
682 /*******************************************************************************
683 **
684 ** Function         BTM_BleSetBgConnType
685 **
686 ** Description      This function is called to set BLE connectable mode for a
687 **                  peripheral device.
688 **
689 ** Parameters       bg_conn_type: it can be auto connection, or selective connection.
690 **                  p_select_cback: callback function when selective connection procedure
691 **                              is being used.
692 **
693 ** Returns          void
694 **
695 *******************************************************************************/
BTM_BleSetBgConnType(tBTM_BLE_CONN_TYPE bg_conn_type,tBTM_BLE_SEL_CBACK * p_select_cback)696 BOOLEAN BTM_BleSetBgConnType(tBTM_BLE_CONN_TYPE   bg_conn_type,
697                              tBTM_BLE_SEL_CBACK   *p_select_cback)
698 {
699     BOOLEAN started = TRUE;
700 
701     BTM_TRACE_EVENT ("BTM_BleSetBgConnType ");
702     if (!HCI_LE_HOST_SUPPORTED(btm_cb.devcb.local_lmp_features[HCI_EXT_FEATURES_PAGE_1]))
703         return FALSE;
704 
705     if (btm_cb.ble_ctr_cb.bg_conn_type != bg_conn_type)
706     {
707         switch (bg_conn_type)
708         {
709             case BTM_BLE_CONN_AUTO:
710                 btm_ble_start_auto_conn(TRUE);
711                 break;
712 
713             case BTM_BLE_CONN_SELECTIVE:
714                 if (btm_cb.ble_ctr_cb.bg_conn_type == BTM_BLE_CONN_AUTO)
715                 {
716                     btm_ble_start_auto_conn(FALSE);
717                 }
718                 started = btm_ble_start_select_conn(TRUE, p_select_cback);
719                 break;
720 
721             case BTM_BLE_CONN_NONE:
722                 if (btm_cb.ble_ctr_cb.bg_conn_type == BTM_BLE_CONN_AUTO)
723                 {
724                     btm_ble_start_auto_conn(FALSE);
725                 }
726                 else if (btm_cb.ble_ctr_cb.bg_conn_type == BTM_BLE_CONN_SELECTIVE)
727                 {
728                     btm_ble_start_select_conn(FALSE, NULL);
729                 }
730                 started = TRUE;
731                 break;
732 
733             default:
734                 BTM_TRACE_ERROR("invalid bg connection type : %d ", bg_conn_type);
735                 started = FALSE;
736                 break;
737         }
738 
739         if (started)
740             btm_cb.ble_ctr_cb.bg_conn_type = bg_conn_type;
741     }
742     return started;
743 }
744 
745 /*******************************************************************************
746 **
747 ** Function         BTM_BleClearBgConnDev
748 **
749 ** Description      This function is called to clear the whitelist,
750 **                  end any pending whitelist connections,
751 *                   and reset the local bg device list.
752 **
753 ** Parameters       void
754 **
755 ** Returns          void
756 **
757 *******************************************************************************/
BTM_BleClearBgConnDev(void)758 void BTM_BleClearBgConnDev(void)
759 {
760     btm_ble_start_auto_conn(FALSE);
761     btm_ble_clear_white_list();
762     gatt_reset_bgdev_list();
763 }
764 
765 /*******************************************************************************
766 **
767 ** Function         BTM_BleUpdateBgConnDev
768 **
769 ** Description      This function is called to add or remove a device into/from
770 **                  background connection procedure. The background connection
771 *                   procedure is decided by the background connection type, it can be
772 *                   auto connection, or selective connection.
773 **
774 ** Parameters       add_remove: TRUE to add; FALSE to remove.
775 **                  remote_bda: device address to add/remove.
776 **
777 ** Returns          void
778 **
779 *******************************************************************************/
BTM_BleUpdateBgConnDev(BOOLEAN add_remove,BD_ADDR remote_bda)780 BOOLEAN BTM_BleUpdateBgConnDev(BOOLEAN add_remove, BD_ADDR   remote_bda)
781 {
782     BOOLEAN ret = TRUE;
783     BTM_TRACE_EVENT (" BTM_BleUpdateBgConnDev");
784 
785     /* update white list */
786     ret = btm_update_bg_conn_list(add_remove, remote_bda);
787 
788     btm_update_dev_to_white_list(add_remove, remote_bda);
789 
790     return ret;
791 }
792 
793 /*******************************************************************************
794 **
795 ** Function         BTM_BleSetConnMode
796 **
797 ** Description      This function is called to set BLE connectable mode for a
798 **                  peripheral device.
799 **
800 ** Parameters       directed: is directed connectable mode, or non-directed.
801 **                  p_dir_bda: connectable direct initiator's LE device address
802 **
803 ** Returns          void
804 **
805 *******************************************************************************/
BTM_BleSetConnMode(BOOLEAN is_directed)806 tBTM_STATUS BTM_BleSetConnMode(BOOLEAN is_directed)
807 {
808     tBTM_BLE_INQ_CB *p_cb = &btm_cb.ble_ctr_cb.inq_var;
809 
810     BTM_TRACE_EVENT ("BTM_BleSetConnMode is_directed = %d ", is_directed);
811     if (!HCI_LE_HOST_SUPPORTED(btm_cb.devcb.local_lmp_features[HCI_EXT_FEATURES_PAGE_1]))
812         return BTM_ILLEGAL_VALUE;
813 
814     p_cb->directed_conn = is_directed;
815     return btm_ble_set_connectability( p_cb->connectable_mode);
816 
817 }
818 
819 /*******************************************************************************
820 **
821 ** Function         btm_set_conn_mode_adv_init_addr
822 **
823 ** Description      set initator address type and local address type based on adv
824 **                  mode.
825 **
826 **
827 *******************************************************************************/
btm_set_conn_mode_adv_init_addr(tBTM_BLE_INQ_CB * p_cb,BD_ADDR_PTR p_addr_ptr,tBLE_ADDR_TYPE * p_init_addr_type,tBLE_ADDR_TYPE * p_own_addr_type)828 static UINT8 btm_set_conn_mode_adv_init_addr(tBTM_BLE_INQ_CB *p_cb,
829                                      BD_ADDR_PTR p_addr_ptr,
830                                      tBLE_ADDR_TYPE *p_init_addr_type,
831                                      tBLE_ADDR_TYPE *p_own_addr_type)
832 {
833     UINT8 evt_type;
834 
835     UNUSED(p_own_addr_type);
836 
837     if ( p_cb->directed_conn)
838     {
839         /*  direct adv mode does not have privacy if privacy
840         is not enabled or no reconn addr config */
841 
842         *p_init_addr_type  = p_cb->direct_bda.type;
843          memcpy(p_addr_ptr, p_cb->direct_bda.bda, BD_ADDR_LEN);
844         evt_type = BTM_BLE_CONNECT_DIR_EVT;
845     }
846     else /* undirect adv mode */
847     {
848         evt_type = BTM_BLE_CONNECT_EVT;
849 
850         /* may need to reset random address if privacy is enabled */
851         if (btm_cb.ble_ctr_cb.privacy && /* own addr_type is random */
852             !BTM_BLE_IS_RESOLVE_BDA(btm_cb.ble_ctr_cb.addr_mgnt_cb.private_addr))
853         {
854             /* need to generate RRA and update random addresss in controller */
855             btm_gen_resolvable_private_addr((void *)btm_gen_resolve_paddr_low);
856         }
857     }
858 
859     return evt_type;
860 
861 }
862 
863 /*******************************************************************************
864 **
865 ** Function         BTM_BleSetAdvParams
866 **
867 ** Description      This function is called to set advertising parameters.
868 **
869 ** Parameters       adv_int_min: minimum advertising interval
870 **                  adv_int_max: maximum advertising interval
871 **                  p_dir_bda: connectable direct initiator's LE device address
872 **                  chnl_map: advertising channel map.
873 **
874 ** Returns          void
875 **
876 *******************************************************************************/
BTM_BleSetAdvParams(UINT16 adv_int_min,UINT16 adv_int_max,tBLE_BD_ADDR * p_dir_bda,tBTM_BLE_ADV_CHNL_MAP chnl_map)877 tBTM_STATUS BTM_BleSetAdvParams(UINT16 adv_int_min, UINT16 adv_int_max,
878                                 tBLE_BD_ADDR *p_dir_bda,
879                                 tBTM_BLE_ADV_CHNL_MAP chnl_map)
880 {
881     tBTM_LE_RANDOM_CB *p_addr_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
882     tBTM_BLE_INQ_CB *p_cb = &btm_cb.ble_ctr_cb.inq_var;
883     tBTM_STATUS status = BTM_SUCCESS;
884     BD_ADDR     p_addr_ptr =  {0};
885     tBLE_ADDR_TYPE   init_addr_type = BLE_ADDR_PUBLIC;
886     tBLE_ADDR_TYPE   own_addr_type = p_addr_cb->own_addr_type;
887     UINT8            adv_mode = p_cb->adv_mode;
888 
889     BTM_TRACE_EVENT ("BTM_BleSetAdvParams");
890 
891     if (!HCI_LE_HOST_SUPPORTED(btm_cb.devcb.local_lmp_features[HCI_EXT_FEATURES_PAGE_1]))
892         return BTM_ILLEGAL_VALUE;
893 
894     if (!BTM_BLE_VALID_PRAM(adv_int_min, BTM_BLE_ADV_INT_MIN, BTM_BLE_ADV_INT_MAX) ||
895         !BTM_BLE_VALID_PRAM(adv_int_max, BTM_BLE_ADV_INT_MIN, BTM_BLE_ADV_INT_MAX))
896     {
897         return BTM_ILLEGAL_VALUE;
898     }
899 
900     p_cb->adv_interval_min = adv_int_min;
901     p_cb->adv_interval_max = adv_int_max;
902     p_cb->adv_chnl_map = chnl_map;
903 
904     if (p_dir_bda)
905     {
906         memcpy(&p_cb->direct_bda, p_dir_bda, sizeof(tBLE_BD_ADDR));
907     }
908 
909     BTM_TRACE_EVENT ("update params for an active adv");
910 
911     btm_ble_stop_adv();
912 
913     if (p_cb->connectable_mode & BTM_BLE_CONNECTABLE)
914         p_cb->evt_type = btm_set_conn_mode_adv_init_addr(p_cb, p_addr_ptr, &init_addr_type, &own_addr_type);
915 
916     /* update adv params */
917     btsnd_hcic_ble_write_adv_params (p_cb->adv_interval_min,
918                                      p_cb->adv_interval_max,
919                                      p_cb->evt_type,
920                                      own_addr_type,
921                                      init_addr_type,
922                                      p_addr_ptr,
923                                      p_cb->adv_chnl_map,
924                                      p_cb->afp);
925 
926     if (adv_mode == BTM_BLE_ADV_ENABLE)
927         btm_ble_start_adv();
928 
929     return status;
930 }
931 
932 /*******************************************************************************
933 **
934 ** Function         BTM_BleReadAdvParams
935 **
936 ** Description      This function is called to set advertising parameters.
937 **
938 ** Parameters       adv_int_min: minimum advertising interval
939 **                  adv_int_max: maximum advertising interval
940 **                  p_dir_bda: connectable direct initiator's LE device address
941 **                  chnl_map: advertising channel map.
942 **
943 ** Returns          void
944 **
945 *******************************************************************************/
BTM_BleReadAdvParams(UINT16 * adv_int_min,UINT16 * adv_int_max,tBLE_BD_ADDR * p_dir_bda,tBTM_BLE_ADV_CHNL_MAP * p_chnl_map)946 void BTM_BleReadAdvParams (UINT16 *adv_int_min, UINT16 *adv_int_max,
947                            tBLE_BD_ADDR *p_dir_bda, tBTM_BLE_ADV_CHNL_MAP *p_chnl_map)
948 {
949     tBTM_BLE_INQ_CB *p_cb = &btm_cb.ble_ctr_cb.inq_var;
950 
951     BTM_TRACE_EVENT ("BTM_BleReadAdvParams ");
952     if (!HCI_LE_HOST_SUPPORTED(btm_cb.devcb.local_lmp_features[HCI_EXT_FEATURES_PAGE_1]))
953         return ;
954 
955     *adv_int_min = p_cb->adv_interval_min;
956     *adv_int_max = p_cb->adv_interval_max;
957     *p_chnl_map = p_cb->adv_chnl_map;
958 
959     if (p_dir_bda != NULL)
960     {
961         memcpy(p_dir_bda, &p_cb->direct_bda, sizeof(tBLE_BD_ADDR));
962     }
963 }
964 
965 /*******************************************************************************
966 **
967 ** Function         BTM_BleSetScanParams
968 **
969 ** Description      This function is called to set Scan parameters.
970 **
971 ** Parameters       adv_int_min: minimum advertising interval
972 **                  adv_int_max: maximum advertising interval
973 **                  p_dir_bda: connectable direct initiator's LE device address
974 **                  chnl_map: advertising channel map.
975 **                  scan_type: active scan or passive scan
976 **
977 ** Returns          void
978 **
979 *******************************************************************************/
BTM_BleSetScanParams(UINT16 scan_interval,UINT16 scan_window,tBTM_BLE_SCAN_MODE scan_mode)980 void BTM_BleSetScanParams(UINT16 scan_interval, UINT16 scan_window, tBTM_BLE_SCAN_MODE scan_mode)
981 {
982     tBTM_BLE_INQ_CB *p_cb = &btm_cb.ble_ctr_cb.inq_var;
983 
984     BTM_TRACE_EVENT (" BTM_BleSetScanParams");
985     if (!HCI_LE_HOST_SUPPORTED(btm_cb.devcb.local_lmp_features[HCI_EXT_FEATURES_PAGE_1]))
986         return ;
987 
988     if (BTM_BLE_VALID_PRAM(scan_interval, BTM_BLE_SCAN_INT_MIN, BTM_BLE_SCAN_INT_MAX) &&
989         BTM_BLE_VALID_PRAM(scan_window, BTM_BLE_SCAN_WIN_MIN, BTM_BLE_SCAN_WIN_MAX) &&
990         (scan_mode == BTM_BLE_SCAN_MODE_ACTI || scan_mode == BTM_BLE_SCAN_MODE_PASS))
991     {
992         p_cb->scan_type     = scan_mode;
993 
994         if (BTM_BLE_CONN_PARAM_UNDEF != scan_interval)
995             p_cb->scan_interval = scan_interval;
996 
997         if (BTM_BLE_CONN_PARAM_UNDEF != scan_window)
998             p_cb->scan_window   = scan_window;
999     }
1000     else
1001     {
1002         BTM_TRACE_ERROR("Illegal params: scan_interval = %d scan_window = %d",
1003                         scan_interval, scan_window);
1004     }
1005 
1006 }
1007 
1008 /*******************************************************************************
1009 **
1010 ** Function         BTM_BleWriteScanRsp
1011 **
1012 ** Description      This function is called to write LE scan response.
1013 **
1014 ** Parameters:      p_scan_rsp: scan response information.
1015 **
1016 ** Returns          void
1017 **
1018 *******************************************************************************/
BTM_BleWriteScanRsp(tBTM_BLE_AD_MASK data_mask,tBTM_BLE_ADV_DATA * p_data)1019 tBTM_STATUS BTM_BleWriteScanRsp(tBTM_BLE_AD_MASK data_mask, tBTM_BLE_ADV_DATA *p_data)
1020 {
1021     tBTM_STATUS     status = BTM_NO_RESOURCES;
1022     UINT8   rsp_data[BTM_BLE_AD_DATA_LEN],
1023             *p = rsp_data;
1024 
1025     BTM_TRACE_EVENT (" BTM_BleWriteScanRsp");
1026 
1027     if (!HCI_LE_HOST_SUPPORTED(btm_cb.devcb.local_lmp_features[HCI_EXT_FEATURES_PAGE_1]))
1028         return BTM_ILLEGAL_VALUE;
1029 
1030     memset(rsp_data, 0, BTM_BLE_AD_DATA_LEN);
1031     btm_ble_build_adv_data(&data_mask, &p, p_data);
1032 
1033     if (btsnd_hcic_ble_set_scan_rsp_data((UINT8)(p - rsp_data), rsp_data))
1034     {
1035         status = BTM_SUCCESS;
1036 
1037         if (data_mask != 0)
1038             btm_cb.ble_ctr_cb.inq_var.scan_rsp = TRUE;
1039         else
1040             btm_cb.ble_ctr_cb.inq_var.scan_rsp = FALSE;
1041     }
1042     else
1043         status = BTM_ILLEGAL_VALUE;
1044 
1045     return status;
1046 }
1047 
1048 /*******************************************************************************
1049 **
1050 ** Function         BTM_BleWriteAdvData
1051 **
1052 ** Description      This function is called to write advertising data.
1053 **
1054 ** Parameters:       None.
1055 **
1056 ** Returns          void
1057 **
1058 *******************************************************************************/
BTM_BleWriteAdvData(tBTM_BLE_AD_MASK data_mask,tBTM_BLE_ADV_DATA * p_data)1059 tBTM_STATUS BTM_BleWriteAdvData(tBTM_BLE_AD_MASK data_mask, tBTM_BLE_ADV_DATA *p_data)
1060 {
1061     tBTM_BLE_LOCAL_ADV_DATA *p_cb_data = &btm_cb.ble_ctr_cb.inq_var.adv_data;
1062     UINT8  *p;
1063     tBTM_BLE_AD_MASK   mask = data_mask;
1064 
1065     BTM_TRACE_EVENT ("BTM_BleWriteAdvData ");
1066 
1067     if (!HCI_LE_HOST_SUPPORTED(btm_cb.devcb.local_lmp_features[HCI_EXT_FEATURES_PAGE_1]))
1068         return BTM_ILLEGAL_VALUE;
1069 
1070     memset(p_cb_data, 0, sizeof(tBTM_BLE_LOCAL_ADV_DATA));
1071     p = p_cb_data->ad_data;
1072     p_cb_data->data_mask = data_mask;
1073 
1074     p_cb_data->p_flags = btm_ble_build_adv_data(&mask, &p, p_data);
1075 
1076     p_cb_data->p_pad = p;
1077 
1078     if (mask != 0)
1079     {
1080         BTM_TRACE_ERROR("Partial data write into ADV");
1081     }
1082 
1083     p_cb_data->data_mask &= ~mask;
1084 
1085     if (btsnd_hcic_ble_set_adv_data((UINT8)(p_cb_data->p_pad - p_cb_data->ad_data),
1086                                     p_cb_data->ad_data))
1087         return BTM_SUCCESS;
1088     else
1089         return BTM_NO_RESOURCES;
1090 
1091 }
1092 
1093 /*******************************************************************************
1094 **
1095 ** Function         BTM_CheckAdvData
1096 **
1097 ** Description      This function is called to get ADV data for a specific type.
1098 **
1099 ** Parameters       p_adv - pointer of ADV data
1100 **                  type   - finding ADV data type
1101 **                  p_length - return the length of ADV data not including type
1102 **
1103 ** Returns          pointer of ADV data
1104 **
1105 *******************************************************************************/
BTM_CheckAdvData(UINT8 * p_adv,UINT8 type,UINT8 * p_length)1106 UINT8 *BTM_CheckAdvData( UINT8 *p_adv, UINT8 type, UINT8 *p_length)
1107 {
1108     UINT8 *p = p_adv;
1109     UINT8 length;
1110     UINT8 adv_type;
1111     BTM_TRACE_API("BTM_CheckAdvData type=0x%02X", type);
1112 
1113     STREAM_TO_UINT8(length, p);
1114 
1115     while ( length && (p - p_adv <= BTM_BLE_CACHE_ADV_DATA_MAX))
1116     {
1117         STREAM_TO_UINT8(adv_type, p);
1118 
1119         if ( adv_type == type )
1120         {
1121             /* length doesn't include itself */
1122             *p_length = length - 1; /* minus the length of type */
1123             return p;
1124         }
1125         p += length - 1; /* skip the length of data */
1126         STREAM_TO_UINT8(length, p);
1127     }
1128 
1129     *p_length = 0;
1130     return NULL;
1131 }
1132 
1133 /*******************************************************************************
1134 **
1135 ** Function         btm_ble_build_adv_data
1136 **
1137 ** Description      This function is called build the adv data and rsp data.
1138 *******************************************************************************/
btm_ble_build_adv_data(tBTM_BLE_AD_MASK * p_data_mask,UINT8 ** p_dst,tBTM_BLE_ADV_DATA * p_data)1139 UINT8 *btm_ble_build_adv_data(tBTM_BLE_AD_MASK *p_data_mask, UINT8 **p_dst,
1140                               tBTM_BLE_ADV_DATA *p_data)
1141 {
1142     UINT32 data_mask = *p_data_mask;
1143     UINT8   *p = *p_dst,
1144     *p_flag = NULL;
1145     UINT16  len = BTM_BLE_AD_DATA_LEN, cp_len = 0;
1146     UINT8   i = 0;
1147     tBTM_BLE_PROP_ELEM      *p_elem;
1148 
1149     BTM_TRACE_EVENT (" btm_ble_build_adv_data");
1150 
1151     /* build the adv data structure and build the data string */
1152     if (data_mask)
1153     {
1154         /* flags */
1155         if (data_mask & BTM_BLE_AD_BIT_FLAGS)
1156         {
1157             *p++ = MIN_ADV_LENGTH;
1158             *p++ = BTM_BLE_AD_TYPE_FLAG;
1159             p_flag = p;
1160             if (p_data)
1161                 *p++ = p_data->flag;
1162             else
1163                 *p++ = 0;
1164 
1165             len -= 3;
1166 
1167             data_mask &= ~BTM_BLE_AD_BIT_FLAGS;
1168         }
1169         /* appearance data */
1170         if (len > 3 && data_mask & BTM_BLE_AD_BIT_APPEARANCE)
1171         {
1172             *p++ = 3; /* length */
1173             *p++ = BTM_BLE_AD_TYPE_APPEARANCE;
1174             UINT16_TO_STREAM(p, p_data->appearance);
1175             len -= 4;
1176 
1177             data_mask &= ~BTM_BLE_AD_BIT_APPEARANCE;
1178         }
1179         /* device name */
1180 #if BTM_MAX_LOC_BD_NAME_LEN > 0
1181         if (len > MIN_ADV_LENGTH && data_mask & BTM_BLE_AD_BIT_DEV_NAME)
1182         {
1183             if (strlen(btm_cb.cfg.bd_name) > (UINT16)(len - MIN_ADV_LENGTH))
1184             {
1185                 *p++ = len - MIN_ADV_LENGTH + 1;
1186                 *p++ = BTM_BLE_AD_TYPE_NAME_SHORT;
1187                 ARRAY_TO_STREAM(p, btm_cb.cfg.bd_name, len - MIN_ADV_LENGTH);
1188             }
1189             else
1190             {
1191                 cp_len = (UINT16)strlen(btm_cb.cfg.bd_name);
1192                 *p++ = cp_len + 1;
1193                 *p++ = BTM_BLE_AD_TYPE_NAME_CMPL;
1194                 ARRAY_TO_STREAM(p, btm_cb.cfg.bd_name, cp_len);
1195             }
1196             len -= (cp_len + MIN_ADV_LENGTH);
1197             data_mask &= ~BTM_BLE_AD_BIT_DEV_NAME;
1198         }
1199 #endif
1200         /* manufacturer data */
1201         if (len > MIN_ADV_LENGTH && data_mask & BTM_BLE_AD_BIT_MANU &&
1202             p_data && p_data->p_manu &&
1203             p_data->p_manu->len != 0 && p_data->p_manu->p_val)
1204         {
1205             if (p_data->p_manu->len > (len - MIN_ADV_LENGTH))
1206                 cp_len = len - MIN_ADV_LENGTH;
1207             else
1208                 cp_len = p_data->p_manu->len;
1209 
1210             *p++ = cp_len + 1;
1211             *p++ = BTM_BLE_AD_TYPE_MANU;
1212             ARRAY_TO_STREAM(p, p_data->p_manu->p_val, cp_len);
1213 
1214             len -= (cp_len + MIN_ADV_LENGTH);
1215             data_mask &= ~BTM_BLE_AD_BIT_MANU;
1216         }
1217         /* TX power */
1218         if (len > MIN_ADV_LENGTH && data_mask & BTM_BLE_AD_BIT_TX_PWR)
1219         {
1220             *p++ = MIN_ADV_LENGTH;
1221             *p++ = BTM_BLE_AD_TYPE_TX_PWR;
1222             if (p_data->tx_power > BTM_BLE_ADV_TX_POWER_MAX)
1223                 p_data->tx_power = BTM_BLE_ADV_TX_POWER_MAX;
1224             *p++ = btm_ble_map_adv_tx_power(p_data->tx_power);
1225             len -= 3;
1226             data_mask &= ~BTM_BLE_AD_BIT_TX_PWR;
1227         }
1228         /* 16 bits services */
1229         if (len > MIN_ADV_LENGTH && data_mask & BTM_BLE_AD_BIT_SERVICE &&
1230             p_data && p_data->p_services &&
1231             p_data->p_services->num_service != 0 &&
1232             p_data->p_services->p_uuid)
1233         {
1234             if (p_data->p_services->num_service * LEN_UUID_16 > (len - MIN_ADV_LENGTH))
1235             {
1236                 cp_len = (len - MIN_ADV_LENGTH)/LEN_UUID_16;
1237                 *p ++ = 1 + cp_len * LEN_UUID_16;
1238                 *p++ = BTM_BLE_AD_TYPE_16SRV_PART;
1239             }
1240             else
1241             {
1242                 cp_len = p_data->p_services->num_service;
1243                 *p++ = 1 + cp_len * LEN_UUID_16;
1244                 *p++ = BTM_BLE_AD_TYPE_16SRV_CMPL;
1245             }
1246             for (i = 0; i < cp_len; i ++)
1247             {
1248                 UINT16_TO_STREAM(p, *(p_data->p_services->p_uuid + i));
1249             }
1250 
1251             len -= (cp_len * MIN_ADV_LENGTH + MIN_ADV_LENGTH);
1252             data_mask &= ~BTM_BLE_AD_BIT_SERVICE;
1253         }
1254         /* 32 bits service uuid */
1255         if (len > MIN_ADV_LENGTH && data_mask & BTM_BLE_AD_BIT_SERVICE_32 &&
1256             p_data && p_data->p_service_32b &&
1257             p_data->p_service_32b->num_service != 0 &&
1258             p_data->p_service_32b->p_uuid)
1259         {
1260             if ((p_data->p_service_32b->num_service * LEN_UUID_32) > (len - MIN_ADV_LENGTH))
1261             {
1262                 cp_len = (len - MIN_ADV_LENGTH)/LEN_UUID_32;
1263                 *p ++ = 1 + cp_len * LEN_UUID_32;
1264                 *p++ = BTM_BLE_AD_TYPE_32SRV_PART;
1265             }
1266             else
1267             {
1268                 cp_len = p_data->p_service_32b->num_service;
1269                 *p++ = 1 + cp_len * LEN_UUID_32;
1270                 *p++ = BTM_BLE_AD_TYPE_32SRV_CMPL;
1271             }
1272             for (i = 0; i < cp_len; i ++)
1273             {
1274                 UINT32_TO_STREAM(p, *(p_data->p_service_32b->p_uuid + i));
1275             }
1276 
1277             len -= (cp_len * LEN_UUID_32 + MIN_ADV_LENGTH);
1278             data_mask &= ~BTM_BLE_AD_BIT_SERVICE_32;
1279         }
1280         /* 128 bits services */
1281         if (len >= (MAX_UUID_SIZE + 2) && data_mask & BTM_BLE_AD_BIT_SERVICE_128 &&
1282             p_data && p_data->p_services_128b)
1283         {
1284             *p ++ = 1 + MAX_UUID_SIZE;
1285             if (!p_data->p_services_128b->list_cmpl)
1286                 *p++ = BTM_BLE_AD_TYPE_128SRV_PART;
1287             else
1288                 *p++ = BTM_BLE_AD_TYPE_128SRV_CMPL;
1289 
1290             ARRAY_TO_STREAM(p, p_data->p_services_128b->uuid128, MAX_UUID_SIZE);
1291 
1292             len -= (MAX_UUID_SIZE + MIN_ADV_LENGTH);
1293             data_mask &= ~BTM_BLE_AD_BIT_SERVICE_128;
1294         }
1295         /* 32 bits Service Solicitation UUIDs */
1296         if (len > MIN_ADV_LENGTH && data_mask & BTM_BLE_AD_BIT_SERVICE_32SOL &&
1297             p_data && p_data->p_sol_service_32b &&
1298             p_data->p_sol_service_32b->num_service != 0 &&
1299             p_data->p_sol_service_32b->p_uuid)
1300         {
1301             if ((p_data->p_sol_service_32b->num_service * LEN_UUID_32) > (len - MIN_ADV_LENGTH))
1302             {
1303                 cp_len = (len - MIN_ADV_LENGTH)/LEN_UUID_32;
1304                 *p ++ = 1 + cp_len * LEN_UUID_32;
1305             }
1306             else
1307             {
1308                 cp_len = p_data->p_sol_service_32b->num_service;
1309                 *p++ = 1 + cp_len * LEN_UUID_32;
1310             }
1311 
1312             *p++ = BTM_BLE_AD_TYPE_32SOL_SRV_UUID;
1313             for (i = 0; i < cp_len; i ++)
1314             {
1315                 UINT32_TO_STREAM(p, *(p_data->p_sol_service_32b->p_uuid + i));
1316             }
1317 
1318             len -= (cp_len * LEN_UUID_32 + MIN_ADV_LENGTH);
1319             data_mask &= ~BTM_BLE_AD_BIT_SERVICE_32SOL;
1320         }
1321         /* 128 bits Solicitation services UUID */
1322         if (len >= (MAX_UUID_SIZE + MIN_ADV_LENGTH) && data_mask & BTM_BLE_AD_BIT_SERVICE_128SOL &&
1323             p_data && p_data->p_sol_service_128b)
1324         {
1325             *p ++ = 1 + MAX_UUID_SIZE;
1326             *p++ = BTM_BLE_AD_TYPE_128SOL_SRV_UUID;
1327             ARRAY_TO_STREAM(p, p_data->p_sol_service_128b->uuid128, MAX_UUID_SIZE);
1328             len -= (MAX_UUID_SIZE + MIN_ADV_LENGTH);
1329             data_mask &= ~BTM_BLE_AD_BIT_SERVICE_128SOL;
1330         }
1331         /* 16bits/32bits/128bits Service Data */
1332         if (len > MIN_ADV_LENGTH && data_mask & BTM_BLE_AD_BIT_SERVICE_DATA &&
1333             p_data && p_data->p_service_data->len != 0 && p_data->p_service_data->p_val)
1334         {
1335             if (len  > (p_data->p_service_data->service_uuid.len + MIN_ADV_LENGTH))
1336             {
1337                 if (p_data->p_service_data->len > (len - MIN_ADV_LENGTH))
1338                     cp_len = len - MIN_ADV_LENGTH- p_data->p_service_data->service_uuid.len;
1339                 else
1340                     cp_len = p_data->p_service_data->len;
1341 
1342                 *p++ = cp_len + 1 + p_data->p_service_data->service_uuid.len;
1343                 if (p_data->p_service_data->service_uuid.len == LEN_UUID_16)
1344                 {
1345                     *p++ = BTM_BLE_AD_TYPE_SERVICE_DATA;
1346                     UINT16_TO_STREAM(p, p_data->p_service_data->service_uuid.uu.uuid16);
1347                 }
1348                 else if (p_data->p_service_data->service_uuid.len == LEN_UUID_32)
1349                 {
1350                     *p++ = BTM_BLE_AD_TYPE_32SERVICE_DATA;
1351                     UINT32_TO_STREAM(p, p_data->p_service_data->service_uuid.uu.uuid32);
1352                 }
1353                 else
1354                 {
1355                     *p++ = BTM_BLE_AD_TYPE_128SERVICE_DATA;
1356                     ARRAY_TO_STREAM(p, p_data->p_service_data->service_uuid.uu.uuid128,
1357                                     LEN_UUID_128);
1358                 }
1359 
1360                 ARRAY_TO_STREAM(p, p_data->p_service_data->p_val, cp_len);
1361 
1362                 len -= (cp_len + MIN_ADV_LENGTH + p_data->p_service_data->service_uuid.len);
1363                 data_mask &= ~BTM_BLE_AD_BIT_SERVICE_DATA;
1364             }
1365             else
1366             {
1367                 BTM_TRACE_WARNING("service data does not fit");
1368             }
1369         }
1370 
1371         if (len >= 6 && data_mask & BTM_BLE_AD_BIT_INT_RANGE &&
1372             p_data)
1373         {
1374             *p++ = 5;
1375             *p++ = BTM_BLE_AD_TYPE_INT_RANGE;
1376             UINT16_TO_STREAM(p, p_data->int_range.low);
1377             UINT16_TO_STREAM(p, p_data->int_range.hi);
1378             len -= 6;
1379             data_mask &= ~BTM_BLE_AD_BIT_INT_RANGE;
1380         }
1381         if (data_mask & BTM_BLE_AD_BIT_PROPRIETARY && p_data && p_data->p_proprietary)
1382         {
1383             for (i = 0; i < p_data->p_proprietary->num_elem ; i ++)
1384             {
1385                 p_elem = p_data->p_proprietary->p_elem  + i;
1386 
1387                 if (len >= (MIN_ADV_LENGTH + p_elem->len))/* len byte(1) + ATTR type(1) + Uuid len(2)
1388                                                           + value length */
1389                 {
1390                     *p ++ = p_elem->len + 1; /* Uuid len + value length */
1391                     *p ++ = p_elem->adv_type;
1392                     ARRAY_TO_STREAM(p, p_elem->p_val, p_elem->len);
1393 
1394                     len -= (MIN_ADV_LENGTH + p_elem->len);
1395                 }
1396                 else
1397                 {
1398                     BTM_TRACE_WARNING("data exceed max adv packet length");
1399                     break;
1400                 }
1401             }
1402             data_mask &= ~BTM_BLE_AD_BIT_PROPRIETARY;
1403         }
1404     }
1405 
1406     *p_data_mask = data_mask;
1407     *p_dst = p;
1408 
1409     return p_flag;
1410 }
1411 /*******************************************************************************
1412 **
1413 ** Function         btm_ble_select_adv_interval
1414 **
1415 ** Description      select adv interval based on device mode
1416 **
1417 ** Returns          void
1418 **
1419 *******************************************************************************/
btm_ble_select_adv_interval(tBTM_BLE_INQ_CB * p_cb,UINT8 evt_type,UINT16 * p_adv_int_min,UINT16 * p_adv_int_max)1420 void btm_ble_select_adv_interval(tBTM_BLE_INQ_CB *p_cb, UINT8 evt_type, UINT16 *p_adv_int_min, UINT16 *p_adv_int_max)
1421 {
1422     if (p_cb->adv_interval_min && p_cb->adv_interval_max)
1423     {
1424         *p_adv_int_min = p_cb->adv_interval_min;
1425         *p_adv_int_max = p_cb->adv_interval_max;
1426     }
1427     else
1428     {
1429         switch (evt_type)
1430         {
1431         case BTM_BLE_CONNECT_EVT:
1432             *p_adv_int_min = *p_adv_int_max = BTM_BLE_GAP_ADV_FAST_INT_1;
1433             break;
1434 
1435         case BTM_BLE_NON_CONNECT_EVT:
1436         case BTM_BLE_DISCOVER_EVT:
1437             *p_adv_int_min = *p_adv_int_max = BTM_BLE_GAP_ADV_FAST_INT_2;
1438             break;
1439         /* connectable directed event */
1440         case BTM_BLE_CONNECT_DIR_EVT:
1441             *p_adv_int_min = BTM_BLE_GAP_ADV_DIR_MIN_INT;
1442             *p_adv_int_max = BTM_BLE_GAP_ADV_DIR_MAX_INT;
1443             break;
1444 
1445         default:
1446             *p_adv_int_min = *p_adv_int_max = BTM_BLE_GAP_ADV_SLOW_INT;
1447             break;
1448         }
1449     }
1450     return;
1451 }
1452 /*******************************************************************************
1453 **
1454 ** Function         btm_ble_set_adv_flag
1455 **
1456 ** Description      Set adv flag in adv data.
1457 **
1458 ** Returns          void
1459 **
1460 *******************************************************************************/
btm_ble_set_adv_flag(UINT16 connect_mode,UINT16 disc_mode)1461 void btm_ble_set_adv_flag(UINT16 connect_mode, UINT16 disc_mode)
1462 {
1463     UINT8 flag = 0, old_flag = 0;
1464     tBTM_BLE_LOCAL_ADV_DATA *p_adv_data = &btm_cb.ble_ctr_cb.inq_var.adv_data;
1465 
1466     if (p_adv_data->p_flags != NULL)
1467         flag = old_flag = *(p_adv_data->p_flags);
1468 
1469     /* BR/EDR non-discoverable , non-connectable */
1470     if ((disc_mode & BTM_DISCOVERABLE_MASK) == 0 &&
1471         (connect_mode & BTM_CONNECTABLE_MASK) == 0)
1472         flag |= BTM_BLE_BREDR_NOT_SPT;
1473     else
1474         flag &= ~BTM_BLE_BREDR_NOT_SPT;
1475 
1476     /* if local controller support, mark both controller and host support in flag */
1477     if (HCI_SIMUL_LE_BREDR_SUPPORTED(btm_cb.devcb.local_lmp_features[HCI_EXT_FEATURES_PAGE_0]))
1478         flag |= (BTM_BLE_DMT_CONTROLLER_SPT|BTM_BLE_DMT_HOST_SPT);
1479     else
1480         flag &= ~(BTM_BLE_DMT_CONTROLLER_SPT|BTM_BLE_DMT_HOST_SPT);
1481 
1482     BTM_TRACE_DEBUG("disc_mode %04x", disc_mode);
1483     /* update discoverable flag */
1484     if (disc_mode & BTM_BLE_LIMITED_DISCOVERABLE)
1485     {
1486         flag &= ~BTM_BLE_GEN_DISC_FLAG;
1487         flag |= BTM_BLE_LIMIT_DISC_FLAG ;
1488     }
1489     else if (disc_mode & BTM_BLE_GENERAL_DISCOVERABLE)
1490     {
1491         flag |= BTM_BLE_GEN_DISC_FLAG;
1492         flag &= ~BTM_BLE_LIMIT_DISC_FLAG;
1493     }
1494     else /* remove all discoverable flags */
1495     {
1496         flag &= ~(BTM_BLE_LIMIT_DISC_FLAG|BTM_BLE_GEN_DISC_FLAG);
1497     }
1498 
1499     if (flag != old_flag)
1500     {
1501         btm_ble_update_adv_flag(flag);
1502     }
1503 }
1504 /*******************************************************************************
1505 **
1506 ** Function         btm_ble_set_discoverability
1507 **
1508 ** Description      This function is called to set BLE discoverable mode.
1509 **
1510 ** Parameters:      combined_mode: discoverability mode.
1511 **
1512 ** Returns          BTM_SUCCESS is status set successfully; otherwise failure.
1513 **
1514 *******************************************************************************/
btm_ble_set_discoverability(UINT16 combined_mode)1515 tBTM_STATUS btm_ble_set_discoverability(UINT16 combined_mode)
1516 {
1517     tBTM_LE_RANDOM_CB   *p_addr_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
1518     tBTM_BLE_INQ_CB     *p_cb = &btm_cb.ble_ctr_cb.inq_var;
1519     UINT16              mode = (combined_mode &  BTM_BLE_DISCOVERABLE_MASK);
1520     UINT8               new_mode = BTM_BLE_ADV_ENABLE;
1521     UINT8               evt_type = (p_cb->connectable_mode == BTM_BLE_NON_CONNECTABLE) ? \
1522                                    ((p_cb->scan_rsp) ? BTM_BLE_DISCOVER_EVT : BTM_BLE_NON_CONNECT_EVT )\
1523                                    : BTM_BLE_CONNECT_EVT;
1524     tBTM_STATUS         status = BTM_SUCCESS;
1525     BD_ADDR             p_addr_ptr= {0};
1526     tBLE_ADDR_TYPE      init_addr_type = BLE_ADDR_PUBLIC,
1527                         own_addr_type = p_addr_cb->own_addr_type;
1528     UINT16              adv_int_min, adv_int_max;
1529 
1530     BTM_TRACE_EVENT ("btm_ble_set_discoverability mode=0x%0x combined_mode=0x%x", mode, combined_mode);
1531 
1532     /*** Check mode parameter ***/
1533     if (mode > BTM_BLE_MAX_DISCOVERABLE)
1534         return(BTM_ILLEGAL_VALUE);
1535 
1536     btm_ble_set_adv_flag (btm_cb.btm_inq_vars.connectable_mode, combined_mode);
1537 
1538     evt_type = btm_set_conn_mode_adv_init_addr(p_cb, p_addr_ptr, &init_addr_type, &own_addr_type);
1539 
1540     if (p_cb->connectable_mode == BTM_BLE_NON_CONNECTABLE && mode == BTM_BLE_NON_DISCOVERABLE)
1541         new_mode = BTM_BLE_ADV_DISABLE;
1542 
1543     btm_ble_select_adv_interval(p_cb, evt_type, &adv_int_min, &adv_int_max);
1544 
1545     btu_stop_timer(&p_cb->fast_adv_timer);
1546 
1547     /* update adv params if start advertising */
1548     BTM_TRACE_EVENT ("evt_type=0x%x p-cb->evt_type=0x%x ", evt_type, p_cb->evt_type);
1549 
1550     if (new_mode == BTM_BLE_ADV_ENABLE &&
1551         (evt_type != p_cb->evt_type ||p_cb->adv_addr_type != own_addr_type || !p_cb->fast_adv_on))
1552     {
1553         btm_ble_stop_adv();
1554 
1555         /* update adv params */
1556         if (!btsnd_hcic_ble_write_adv_params (adv_int_min,
1557                                               adv_int_max,
1558                                               evt_type,
1559                                               own_addr_type,
1560                                               init_addr_type,
1561                                               p_addr_ptr,
1562                                               p_cb->adv_chnl_map,
1563                                               p_cb->afp))
1564 
1565             status = BTM_NO_RESOURCES;
1566         else
1567         {
1568             p_cb->evt_type = evt_type;
1569             p_cb->adv_addr_type = own_addr_type;
1570         }
1571 
1572     }
1573 
1574     if (status == BTM_SUCCESS && p_cb->adv_mode != new_mode)
1575     {
1576         if (new_mode == BTM_BLE_ADV_ENABLE)
1577             status = btm_ble_start_adv();
1578         else
1579             status = btm_ble_stop_adv();
1580     }
1581     if (p_cb->adv_mode == BTM_BLE_ADV_ENABLE)
1582     {
1583         p_cb->fast_adv_on = TRUE;
1584         /* start initial GAP mode adv timer */
1585         btu_start_timer (&p_cb->fast_adv_timer, BTU_TTYPE_BLE_GAP_FAST_ADV,
1586                           BTM_BLE_GAP_FAST_ADV_TOUT);
1587     }
1588 
1589     /* set up stop advertising timer */
1590     if (status == BTM_SUCCESS && mode == BTM_BLE_LIMITED_DISCOVERABLE)
1591     {
1592         BTM_TRACE_EVENT ("start timer for limited disc mode duration=%d (180 secs)", BTM_BLE_GAP_LIM_TOUT);
1593         /* start Tgap(lim_timeout) */
1594         btu_start_timer (&p_cb->inq_timer_ent, BTU_TTYPE_BLE_GAP_LIM_DISC,
1595                          BTM_BLE_GAP_LIM_TOUT);
1596     }
1597     return status;
1598 }
1599 
1600 /*******************************************************************************
1601 **
1602 ** Function         btm_ble_set_connectability
1603 **
1604 ** Description      This function is called to set BLE connectability mode.
1605 **
1606 ** Parameters:      combined_mode: connectability mode.
1607 **
1608 ** Returns          BTM_SUCCESS is status set successfully; otherwise failure.
1609 **
1610 *******************************************************************************/
btm_ble_set_connectability(UINT16 combined_mode)1611 tBTM_STATUS btm_ble_set_connectability(UINT16 combined_mode)
1612 {
1613     tBTM_LE_RANDOM_CB       *p_addr_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
1614     tBTM_BLE_INQ_CB         *p_cb = &btm_cb.ble_ctr_cb.inq_var;
1615     UINT16                  mode = (combined_mode & BTM_BLE_CONNECTABLE_MASK);
1616     UINT8                   new_mode = BTM_BLE_ADV_ENABLE;
1617     UINT8                   evt_type = (p_cb->scan_rsp) ? BTM_BLE_DISCOVER_EVT: BTM_BLE_NON_CONNECT_EVT;
1618     tBTM_STATUS             status = BTM_SUCCESS;
1619     BD_ADDR                 p_addr_ptr =  {0};
1620     tBLE_ADDR_TYPE          init_addr_type = BLE_ADDR_PUBLIC,
1621                             own_addr_type = p_addr_cb->own_addr_type;
1622     UINT16                  adv_int_min, adv_int_max;
1623 
1624     BTM_TRACE_EVENT ("btm_ble_set_connectability mode=0x%0x combined_mode=0x%x", mode, combined_mode);
1625 
1626     /*** Check mode parameter ***/
1627     if (mode > BTM_BLE_MAX_CONNECTABLE)
1628         return(BTM_ILLEGAL_VALUE);
1629 
1630     p_cb->connectable_mode = mode;
1631 
1632     btm_ble_set_adv_flag (combined_mode,  btm_cb.btm_inq_vars.discoverable_mode);
1633 
1634     evt_type = btm_set_conn_mode_adv_init_addr(p_cb, p_addr_ptr, &init_addr_type, &own_addr_type);
1635 
1636     if (mode == BTM_BLE_NON_CONNECTABLE && p_cb->discoverable_mode == BTM_BLE_NON_DISCOVERABLE)
1637         new_mode = BTM_BLE_ADV_DISABLE;
1638 
1639     btm_ble_select_adv_interval(p_cb, evt_type, &adv_int_min, &adv_int_max);
1640 
1641     btu_stop_timer(&p_cb->fast_adv_timer);
1642     /* update adv params if needed */
1643     if ((p_cb->evt_type != evt_type || p_cb->adv_addr_type != p_addr_cb->own_addr_type || !p_cb->fast_adv_on))
1644     {
1645         btm_ble_stop_adv();
1646 
1647         if (!btsnd_hcic_ble_write_adv_params (adv_int_min,
1648                                               adv_int_max,
1649                                               evt_type,
1650                                               own_addr_type,
1651                                               init_addr_type,
1652                                               p_addr_ptr,
1653                                               p_cb->adv_chnl_map,
1654                                               p_cb->afp))
1655             status = BTM_NO_RESOURCES;
1656         else
1657         {
1658             p_cb->evt_type = evt_type;
1659             p_cb->adv_addr_type = own_addr_type;
1660         }
1661     }
1662 
1663     /* update advertising mode */
1664     if (status == BTM_SUCCESS && new_mode != p_cb->adv_mode)
1665     {
1666         if (new_mode == BTM_BLE_ADV_ENABLE)
1667             status = btm_ble_start_adv();
1668         else
1669             status = btm_ble_stop_adv();
1670     }
1671     if (p_cb->adv_mode == BTM_BLE_ADV_ENABLE)
1672     {
1673         p_cb->fast_adv_on = TRUE;
1674         /* start initial GAP mode adv timer */
1675         btu_start_timer (&p_cb->fast_adv_timer, BTU_TTYPE_BLE_GAP_FAST_ADV,
1676                              BTM_BLE_GAP_FAST_ADV_TOUT);
1677     }
1678     return status;
1679 }
1680 
1681 
1682 /*******************************************************************************
1683 **
1684 ** Function         btm_ble_start_inquiry
1685 **
1686 ** Description      This function is called to start BLE inquiry procedure.
1687 **                  If the duration is zero, the periodic inquiry mode is cancelled.
1688 **
1689 ** Parameters:      mode - GENERAL or LIMITED inquiry
1690 **                  p_inq_params - pointer to the BLE inquiry parameter.
1691 **                  p_results_cb - callback returning pointer to results (tBTM_INQ_RESULTS)
1692 **                  p_cmpl_cb - callback indicating the end of an inquiry
1693 **
1694 **
1695 **
1696 ** Returns          BTM_CMD_STARTED if successfully started
1697 **                  BTM_NO_RESOURCES if could not allocate a message buffer
1698 **                  BTM_BUSY - if an inquiry is already active
1699 **
1700 *******************************************************************************/
btm_ble_start_inquiry(UINT8 mode,UINT8 duration)1701 tBTM_STATUS btm_ble_start_inquiry (UINT8 mode, UINT8   duration)
1702 {
1703     tBTM_STATUS status = BTM_CMD_STARTED;
1704     tBTM_BLE_CB *p_ble_cb = &btm_cb.ble_ctr_cb;
1705     tBTM_INQUIRY_VAR_ST      *p_inq = &btm_cb.btm_inq_vars;
1706 
1707     BTM_TRACE_DEBUG("btm_ble_start_inquiry: mode = %02x inq_active = 0x%02x", mode, btm_cb.btm_inq_vars.inq_active);
1708 
1709     /* if selective connection is active, or inquiry is already active, reject it */
1710     if (BTM_BLE_IS_INQ_ACTIVE(p_ble_cb->scan_activity) ||
1711         BTM_BLE_IS_SEL_CONN_ACTIVE (p_ble_cb->scan_activity))
1712     {
1713         BTM_TRACE_ERROR("LE Inquiry is active, can not start inquiry");
1714         return(BTM_BUSY);
1715     }
1716 
1717     if (!BTM_BLE_IS_SCAN_ACTIVE(p_ble_cb->scan_activity))
1718     {
1719         btm_update_scanner_filter_policy(SP_ADV_ALL);
1720 
1721 #if (defined BLE_PRIVACY_SPT && BLE_PRIVACY_SPT == TRUE)
1722         /* enable IRK list */
1723         btm_ble_vendor_irk_list_known_dev (TRUE);
1724 #endif
1725         status = btm_ble_start_scan(BTM_BLE_DUPLICATE_DISABLE);
1726     }
1727 
1728     if (status == BTM_CMD_STARTED)
1729     {
1730         p_inq->inq_active |= mode;
1731         p_ble_cb->scan_activity |= mode;
1732 
1733         BTM_TRACE_DEBUG("btm_ble_start_inquiry inq_active = 0x%02x", p_inq->inq_active);
1734 
1735         if (duration != 0)
1736         {
1737             /* start inquiry timer */
1738             btu_start_timer (&p_inq->inq_timer_ent, BTU_TTYPE_BLE_INQUIRY, duration);
1739         }
1740     }
1741 
1742     return status;
1743 
1744 }
1745 
1746 /*******************************************************************************
1747 **
1748 ** Function         btm_ble_read_remote_name_cmpl
1749 **
1750 ** Description      This function is called when BLE remote name is received.
1751 **
1752 ** Returns          void
1753 **
1754 *******************************************************************************/
btm_ble_read_remote_name_cmpl(BOOLEAN status,BD_ADDR bda,UINT16 length,char * p_name)1755 void btm_ble_read_remote_name_cmpl(BOOLEAN status, BD_ADDR bda, UINT16 length, char *p_name)
1756 {
1757     UINT8   hci_status = HCI_SUCCESS;
1758     BD_NAME bd_name;
1759 
1760     memset(bd_name, 0, (BD_NAME_LEN + 1));
1761     if (length > BD_NAME_LEN)
1762     {
1763         length = BD_NAME_LEN;
1764     }
1765     memcpy((UINT8*)bd_name, p_name, length);
1766 
1767     if ((!status) || (length==0))
1768     {
1769         hci_status = HCI_ERR_HOST_TIMEOUT;
1770     }
1771 
1772     btm_process_remote_name(bda, bd_name, length +1, hci_status);
1773     btm_sec_rmt_name_request_complete (bda, (UINT8 *)p_name, hci_status);
1774 }
1775 
1776 /*******************************************************************************
1777 **
1778 ** Function         btm_ble_read_remote_name
1779 **
1780 ** Description      This function read remote LE device name using GATT read
1781 **                  procedure.
1782 **
1783 ** Parameters:       None.
1784 **
1785 ** Returns          void
1786 **
1787 *******************************************************************************/
btm_ble_read_remote_name(BD_ADDR remote_bda,tBTM_INQ_INFO * p_cur,tBTM_CMPL_CB * p_cb)1788 tBTM_STATUS btm_ble_read_remote_name(BD_ADDR remote_bda, tBTM_INQ_INFO *p_cur, tBTM_CMPL_CB *p_cb)
1789 {
1790     tBTM_INQUIRY_VAR_ST      *p_inq = &btm_cb.btm_inq_vars;
1791 
1792     if (!HCI_LE_HOST_SUPPORTED(btm_cb.devcb.local_lmp_features[HCI_EXT_FEATURES_PAGE_1]))
1793         return BTM_ERR_PROCESSING;
1794 
1795     if (p_cur &&
1796         p_cur->results.ble_evt_type != BTM_BLE_EVT_CONN_ADV &&
1797         p_cur->results.ble_evt_type != BTM_BLE_EVT_CONN_DIR_ADV)
1798     {
1799         BTM_TRACE_DEBUG("name request to non-connectable device failed.");
1800         return BTM_ERR_PROCESSING;
1801     }
1802 
1803     /* read remote device name using GATT procedure */
1804     if (p_inq->remname_active)
1805         return BTM_BUSY;
1806 
1807     if (!GAP_BleReadPeerDevName(remote_bda, btm_ble_read_remote_name_cmpl))
1808         return BTM_BUSY;
1809 
1810     p_inq->p_remname_cmpl_cb = p_cb;
1811     p_inq->remname_active = TRUE;
1812 
1813     memcpy(p_inq->remname_bda, remote_bda, BD_ADDR_LEN);
1814 
1815     btu_start_timer (&p_inq->rmt_name_timer_ent,
1816                      BTU_TTYPE_BTM_RMT_NAME,
1817                      BTM_EXT_BLE_RMT_NAME_TIMEOUT);
1818 
1819     return BTM_CMD_STARTED;
1820 }
1821 
1822 /*******************************************************************************
1823 **
1824 ** Function         btm_ble_cancel_remote_name
1825 **
1826 ** Description      This function cancel read remote LE device name.
1827 **
1828 ** Parameters:       None.
1829 **
1830 ** Returns          void
1831 **
1832 *******************************************************************************/
btm_ble_cancel_remote_name(BD_ADDR remote_bda)1833 BOOLEAN btm_ble_cancel_remote_name(BD_ADDR remote_bda)
1834 {
1835     tBTM_INQUIRY_VAR_ST      *p_inq = &btm_cb.btm_inq_vars;
1836     BOOLEAN     status;
1837 
1838     status = GAP_BleCancelReadPeerDevName(remote_bda);
1839 
1840     p_inq->remname_active = FALSE;
1841     memset(p_inq->remname_bda, 0, BD_ADDR_LEN);
1842     btu_stop_timer(&p_inq->rmt_name_timer_ent);
1843 
1844     return status;
1845 }
1846 
1847 /*******************************************************************************
1848 **
1849 ** Function         btm_ble_update_adv_flag
1850 **
1851 ** Description      This function update the limited discoverable flag in the adv
1852 **                  data.
1853 **
1854 ** Parameters:       None.
1855 **
1856 ** Returns          void
1857 **
1858 *******************************************************************************/
btm_ble_update_adv_flag(UINT8 flag)1859 static void btm_ble_update_adv_flag(UINT8 flag)
1860 {
1861     tBTM_BLE_LOCAL_ADV_DATA *p_adv_data = &btm_cb.ble_ctr_cb.inq_var.adv_data;
1862     UINT8   *p;
1863 
1864     BTM_TRACE_DEBUG ("btm_ble_update_adv_flag new=0x%x", flag);
1865 
1866     if (p_adv_data->p_flags != NULL)
1867     {
1868         BTM_TRACE_DEBUG ("btm_ble_update_adv_flag old=0x%x",   *p_adv_data->p_flags);
1869         *p_adv_data->p_flags = flag;
1870     }
1871     else /* no FLAGS in ADV data*/
1872     {
1873         p = (p_adv_data->p_pad == NULL) ? p_adv_data->ad_data : p_adv_data->p_pad;
1874         /* need 3 bytes space to stuff in the flags, if not */
1875         /* erase all written data, just for flags */
1876         if ((BTM_BLE_AD_DATA_LEN - (p - p_adv_data->ad_data)) < 3)
1877         {
1878             p = p_adv_data->p_pad = p_adv_data->ad_data;
1879             memset(p_adv_data->ad_data, 0, BTM_BLE_AD_DATA_LEN);
1880         }
1881 
1882         *p++ = 2;
1883         *p++ = BTM_BLE_AD_TYPE_FLAG;
1884         p_adv_data->p_flags = p;
1885         *p++ = flag;
1886         p_adv_data->p_pad = p;
1887     }
1888 
1889     if (btsnd_hcic_ble_set_adv_data((UINT8)(p_adv_data->p_pad - p_adv_data->ad_data),
1890                                     p_adv_data->ad_data))
1891         p_adv_data->data_mask |= BTM_BLE_AD_BIT_FLAGS;
1892 
1893 }
1894 
1895 #if 0
1896 /*******************************************************************************
1897 **
1898 ** Function         btm_ble_parse_adv_data
1899 **
1900 ** Description      This function parse the adv data into a structure.
1901 **
1902 ** Returns          pointer to entry, or NULL if not found
1903 **
1904 *******************************************************************************/
1905 static void btm_ble_parse_adv_data(tBTM_INQ_INFO *p_info, UINT8 *p_data,
1906                                    UINT8 len, tBTM_BLE_INQ_DATA *p_adv_data, UINT8 *p_buf)
1907 {
1908     UINT8   *p_cur = p_data;
1909     UINT8   ad_len, ad_type, ad_flag;
1910 
1911     BTM_TRACE_EVENT (" btm_ble_parse_adv_data");
1912 
1913     while (len > 0)
1914     {
1915         BTM_TRACE_DEBUG("btm_ble_parse_adv_data: len = %d", len);
1916         if ((ad_len = *p_cur ++) == 0)
1917             break;
1918 
1919         ad_type = *p_cur ++;
1920 
1921         BTM_TRACE_DEBUG("     ad_type = %02x ad_len = %d", ad_type, ad_len);
1922 
1923         switch (ad_type)
1924         {
1925             case BTM_BLE_AD_TYPE_NAME_SHORT:
1926 
1927             case BTM_BLE_AD_TYPE_NAME_CMPL:
1928                 p_adv_data->ad_mask |= BTM_BLE_AD_BIT_DEV_NAME;
1929                 if (p_info)
1930                 {
1931                     p_info->remote_name_type =(ad_type == BTM_BLE_AD_TYPE_NAME_SHORT) ?
1932                                               BTM_BLE_NAME_SHORT: BTM_BLE_NAME_CMPL;
1933                     memcpy(p_info->remote_name, p_cur, ad_len -1);
1934                     p_info->remote_name[ad_len] = 0;
1935                     p_adv_data->p_remote_name = p_info->remote_name;
1936                     p_info->remote_name_len = p_adv_data->remote_name_len = ad_len - 1;
1937                     BTM_TRACE_DEBUG("BTM_BLE_AD_TYPE_NAME name = %s",p_adv_data->p_remote_name);
1938                 }
1939                 p_cur += (ad_len -1);
1940 
1941                 break;
1942 
1943             case BTM_BLE_AD_TYPE_FLAG:
1944                 p_adv_data->ad_mask |= BTM_BLE_AD_BIT_FLAGS;
1945                 ad_flag = *p_cur ++;
1946                 p_adv_data->flag = (UINT8)(ad_flag & BTM_BLE_ADV_FLAG_MASK) ;
1947                 BTM_TRACE_DEBUG("BTM_BLE_AD_TYPE_FLAG flag = %s | %s | %s",
1948                                  (p_adv_data->flag & BTM_BLE_LIMIT_DISC_FLAG)? "LE_LIMIT_DISC" : "",
1949                                  (p_adv_data->flag & BTM_BLE_GEN_DISC_FLAG)? "LE_GENERAL_DISC" : "",
1950                                  (p_adv_data->flag & BTM_BLE_BREDR_NOT_SPT)? "LE Only device" : "");
1951                 break;
1952 
1953             case BTM_BLE_AD_TYPE_TX_PWR:
1954                 p_adv_data->ad_mask |= BTM_BLE_AD_BIT_TX_PWR;
1955                 p_adv_data->tx_power_level = (INT8)*p_cur ++;
1956                 BTM_TRACE_DEBUG("BTM_BLE_AD_TYPE_TX_PWR tx_level = %d", p_adv_data->tx_power_level);
1957                 break;
1958 
1959             case BTM_BLE_AD_TYPE_MANU:
1960 
1961             case BTM_BLE_AD_TYPE_16SRV_PART:
1962             case BTM_BLE_AD_TYPE_16SRV_CMPL:
1963                 p_adv_data->ad_mask |= BTM_BLE_AD_BIT_SERVICE;
1964                 /* need allocate memory to store UUID list */
1965                 p_adv_data->service.num_service = (ad_len - 1)/2;
1966                 BTM_TRACE_DEBUG("service UUID list, num = %d", p_adv_data->service.num_service);
1967                 p_cur += (ad_len - 1);
1968                 break;
1969 
1970             case BTM_BLE_AD_TYPE_SOL_SRV_UUID:
1971                 p_adv_data->ad_mask |= BTM_BLE_AD_BIT_SERVICE_SOL;
1972                 /* need allocate memory to store UUID list */
1973                 p_adv_data->service.num_service = (ad_len - 1)/2;
1974                 BTM_TRACE_DEBUG("service UUID list, num = %d", p_adv_data->service.num_service);
1975                 p_cur += (ad_len - 1);
1976                 break;
1977 
1978             case BTM_BLE_AD_TYPE_128SOL_SRV_UUID:
1979                 p_adv_data->ad_mask |= BTM_BLE_AD_BIT_SERVICE_128SOL;
1980                 /* need allocate memory to store UUID list */
1981                 p_adv_data->service.num_service = (ad_len - 1)/16;
1982                 BTM_TRACE_DEBUG("service UUID list, num = %d", p_adv_data->service.num_service);
1983                 p_cur += (ad_len - 1);
1984                 break;
1985 
1986             case BTM_BLE_AD_TYPE_APPEARANCE:
1987             case BTM_BLE_AD_TYPE_PUBLIC_TARGET:
1988             case BTM_BLE_AD_TYPE_RANDOM_TARGET:
1989             default:
1990                 break;
1991         }
1992         len -= (ad_len + 1);
1993     }
1994 }
1995 #endif
1996 
1997 /*******************************************************************************
1998 **
1999 ** Function         btm_ble_cache_adv_data
2000 **
2001 ** Description      Update advertising cache data.
2002 **
2003 ** Returns          void
2004 **
2005 *******************************************************************************/
btm_ble_cache_adv_data(tBTM_INQ_RESULTS * p_cur,UINT8 data_len,UINT8 * p,UINT8 evt_type)2006 void btm_ble_cache_adv_data(tBTM_INQ_RESULTS *p_cur, UINT8 data_len, UINT8 *p, UINT8 evt_type)
2007 {
2008     tBTM_BLE_INQ_CB     *p_le_inq_cb = &btm_cb.ble_ctr_cb.inq_var;
2009     UINT8 *p_cache;
2010     UINT8 length;
2011     UNUSED(p_cur);
2012 
2013     /* cache adv report/scan response data */
2014     if (evt_type != BTM_BLE_SCAN_RSP_EVT)
2015     {
2016         p_le_inq_cb->adv_len = 0;
2017         memset(p_le_inq_cb->adv_data_cache, 0, BTM_BLE_CACHE_ADV_DATA_MAX);
2018     }
2019 
2020     if (data_len > 0)
2021     {
2022         p_cache = &p_le_inq_cb->adv_data_cache[p_le_inq_cb->adv_len];
2023         STREAM_TO_UINT8(length, p);
2024         while ( length && ((p_le_inq_cb->adv_len + length + 1) <= BTM_BLE_CACHE_ADV_DATA_MAX))
2025         {
2026             /* copy from the length byte & data into cache */
2027             memcpy(p_cache, p-1, length+1);
2028             /* advance the cache pointer past data */
2029             p_cache += length+1;
2030             /* increment cache length */
2031             p_le_inq_cb->adv_len += length+1;
2032             /* skip the length of data */
2033             p += length;
2034             STREAM_TO_UINT8(length, p);
2035         }
2036     }
2037 
2038     /* parse service UUID from adv packet and save it in inq db eir_uuid */
2039     /* TODO */
2040 }
2041 
2042 /*******************************************************************************
2043 **
2044 ** Function         btm_ble_is_discoverable
2045 **
2046 ** Description      check ADV flag to make sure device is discoverable and match
2047 **                  the search condition
2048 **
2049 ** Parameters
2050 **
2051 ** Returns          void
2052 **
2053 *******************************************************************************/
btm_ble_is_discoverable(BD_ADDR bda,UINT8 evt_type,UINT8 * p)2054 UINT8 btm_ble_is_discoverable(BD_ADDR bda, UINT8 evt_type, UINT8 *p)
2055 {
2056     UINT8               *p_flag, flag = 0, rt = 0;
2057     UINT8                data_len;
2058     tBTM_INQ_PARMS      *p_cond = &btm_cb.btm_inq_vars.inqparms;
2059     tBTM_BLE_INQ_CB     *p_le_inq_cb = &btm_cb.ble_ctr_cb.inq_var;
2060 
2061     UNUSED(p);
2062 
2063     /* for observer, always "discoverable */
2064     if (BTM_BLE_IS_OBS_ACTIVE(btm_cb.ble_ctr_cb.scan_activity))
2065         rt |= BTM_BLE_OBS_RESULT;
2066 
2067     if (BTM_BLE_IS_SEL_CONN_ACTIVE(btm_cb.ble_ctr_cb.scan_activity) &&
2068         (evt_type == BTM_BLE_CONNECT_EVT || evt_type == BTM_BLE_CONNECT_DIR_EVT))
2069         rt |= BTM_BLE_SEL_CONN_RESULT;
2070 
2071     /* does not match filter condition */
2072     if (p_cond->filter_cond_type == BTM_FILTER_COND_BD_ADDR &&
2073         memcmp(bda, p_cond->filter_cond.bdaddr_cond, BD_ADDR_LEN) != 0)
2074     {
2075         BTM_TRACE_DEBUG("BD ADDR does not meet filter condition");
2076         return rt;
2077     }
2078 
2079     if (p_le_inq_cb->adv_len != 0)
2080     {
2081         if ((p_flag = BTM_CheckAdvData(p_le_inq_cb->adv_data_cache,
2082             BTM_BLE_AD_TYPE_FLAG, &data_len)) != NULL)
2083         {
2084             flag = * p_flag;
2085 
2086             if ((btm_cb.btm_inq_vars.inq_active & BTM_BLE_GENERAL_INQUIRY) &&
2087                 (flag & (BTM_BLE_LIMIT_DISC_FLAG|BTM_BLE_GEN_DISC_FLAG)) != 0)
2088             {
2089                 BTM_TRACE_DEBUG("Find Generable Discoverable device");
2090                 rt |= BTM_BLE_INQ_RESULT;
2091             }
2092 
2093             else if (btm_cb.btm_inq_vars.inq_active & BTM_BLE_LIMITED_INQUIRY &&
2094                      (flag & BTM_BLE_LIMIT_DISC_FLAG) != 0)
2095             {
2096                 BTM_TRACE_DEBUG("Find limited discoverable device");
2097                 rt |= BTM_BLE_INQ_RESULT;
2098             }
2099         }
2100     }
2101     return rt;
2102 }
2103 
btm_ble_appearance_to_cod(UINT16 appearance,UINT8 * dev_class)2104 static void btm_ble_appearance_to_cod(UINT16 appearance, UINT8 *dev_class)
2105 {
2106     dev_class[0] = 0;
2107 
2108     switch (appearance)
2109     {
2110         case BTM_BLE_APPEARANCE_GENERIC_PHONE:
2111             dev_class[1] = BTM_COD_MAJOR_PHONE;
2112             dev_class[2] = BTM_COD_MINOR_UNCLASSIFIED;
2113             break;
2114         case BTM_BLE_APPEARANCE_GENERIC_COMPUTER:
2115             dev_class[1] = BTM_COD_MAJOR_COMPUTER;
2116             dev_class[2] = BTM_COD_MINOR_UNCLASSIFIED;
2117             break;
2118         case BTM_BLE_APPEARANCE_GENERIC_REMOTE:
2119             dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
2120             dev_class[2] = BTM_COD_MINOR_REMOTE_CONTROL;
2121             break;
2122         case BTM_BLE_APPEARANCE_GENERIC_THERMOMETER:
2123         case BTM_BLE_APPEARANCE_THERMOMETER_EAR:
2124             dev_class[1] = BTM_COD_MAJOR_HEALTH;
2125             dev_class[2] = BTM_COD_MINOR_THERMOMETER;
2126             break;
2127         case BTM_BLE_APPEARANCE_GENERIC_HEART_RATE:
2128         case BTM_BLE_APPEARANCE_HEART_RATE_BELT:
2129             dev_class[1] = BTM_COD_MAJOR_HEALTH;
2130             dev_class[2] = BTM_COD_MINOR_HEART_PULSE_MONITOR;
2131             break;
2132         case BTM_BLE_APPEARANCE_GENERIC_BLOOD_PRESSURE:
2133         case BTM_BLE_APPEARANCE_BLOOD_PRESSURE_ARM:
2134         case BTM_BLE_APPEARANCE_BLOOD_PRESSURE_WRIST:
2135             dev_class[1] = BTM_COD_MAJOR_HEALTH;
2136             dev_class[2] = BTM_COD_MINOR_BLOOD_MONITOR;
2137             break;
2138         case BTM_BLE_APPEARANCE_GENERIC_PULSE_OXIMETER:
2139         case BTM_BLE_APPEARANCE_PULSE_OXIMETER_FINGERTIP:
2140         case BTM_BLE_APPEARANCE_PULSE_OXIMETER_WRIST:
2141             dev_class[1] = BTM_COD_MAJOR_HEALTH;
2142             dev_class[2] = BTM_COD_MINOR_PULSE_OXIMETER;
2143             break;
2144         case BTM_BLE_APPEARANCE_GENERIC_GLUCOSE:
2145             dev_class[1] = BTM_COD_MAJOR_HEALTH;
2146             dev_class[2] = BTM_COD_MINOR_GLUCOSE_METER;
2147             break;
2148         case BTM_BLE_APPEARANCE_GENERIC_WEIGHT:
2149             dev_class[1] = BTM_COD_MAJOR_HEALTH;
2150             dev_class[2] = BTM_COD_MINOR_WEIGHING_SCALE;
2151             break;
2152         case BTM_BLE_APPEARANCE_GENERIC_WALKING:
2153         case BTM_BLE_APPEARANCE_WALKING_IN_SHOE:
2154         case BTM_BLE_APPEARANCE_WALKING_ON_SHOE:
2155         case BTM_BLE_APPEARANCE_WALKING_ON_HIP:
2156             dev_class[1] = BTM_COD_MAJOR_HEALTH;
2157             dev_class[2] = BTM_COD_MINOR_STEP_COUNTER;
2158             break;
2159         case BTM_BLE_APPEARANCE_GENERIC_WATCH:
2160         case BTM_BLE_APPEARANCE_SPORTS_WATCH:
2161             dev_class[1] = BTM_COD_MAJOR_WEARABLE;
2162             dev_class[2] = BTM_COD_MINOR_WRIST_WATCH;
2163             break;
2164         case BTM_BLE_APPEARANCE_GENERIC_EYEGLASSES:
2165             dev_class[1] = BTM_COD_MAJOR_WEARABLE;
2166             dev_class[2] = BTM_COD_MINOR_GLASSES;
2167             break;
2168         case BTM_BLE_APPEARANCE_GENERIC_DISPLAY:
2169             dev_class[1] = BTM_COD_MAJOR_IMAGING;
2170             dev_class[2] = BTM_COD_MINOR_DISPLAY;
2171             break;
2172         case BTM_BLE_APPEARANCE_GENERIC_MEDIA_PLAYER:
2173             dev_class[1] = BTM_COD_MAJOR_AUDIO;
2174             dev_class[2] = BTM_COD_MINOR_UNCLASSIFIED;
2175             break;
2176         case BTM_BLE_APPEARANCE_GENERIC_BARCODE_SCANNER:
2177         case BTM_BLE_APPEARANCE_HID_BARCODE_SCANNER:
2178         case BTM_BLE_APPEARANCE_GENERIC_HID:
2179             dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
2180             dev_class[2] = BTM_COD_MINOR_UNCLASSIFIED;
2181             break;
2182         case BTM_BLE_APPEARANCE_HID_KEYBOARD:
2183             dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
2184             dev_class[2] = BTM_COD_MINOR_KEYBOARD;
2185             break;
2186         case BTM_BLE_APPEARANCE_HID_MOUSE:
2187             dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
2188             dev_class[2] = BTM_COD_MINOR_POINTING;
2189             break;
2190         case BTM_BLE_APPEARANCE_HID_JOYSTICK:
2191             dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
2192             dev_class[2] = BTM_COD_MINOR_JOYSTICK;
2193             break;
2194         case BTM_BLE_APPEARANCE_HID_GAMEPAD:
2195             dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
2196             dev_class[2] = BTM_COD_MINOR_GAMEPAD;
2197             break;
2198         case BTM_BLE_APPEARANCE_HID_DIGITIZER_TABLET:
2199             dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
2200             dev_class[2] = BTM_COD_MINOR_DIGITIZING_TABLET;
2201             break;
2202         case BTM_BLE_APPEARANCE_HID_CARD_READER:
2203             dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
2204             dev_class[2] = BTM_COD_MINOR_CARD_READER;
2205             break;
2206         case BTM_BLE_APPEARANCE_HID_DIGITAL_PEN:
2207             dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
2208             dev_class[2] = BTM_COD_MINOR_DIGITAL_PAN;
2209             break;
2210         case BTM_BLE_APPEARANCE_UKNOWN:
2211         case BTM_BLE_APPEARANCE_GENERIC_CLOCK:
2212         case BTM_BLE_APPEARANCE_GENERIC_TAG:
2213         case BTM_BLE_APPEARANCE_GENERIC_KEYRING:
2214         case BTM_BLE_APPEARANCE_GENERIC_CYCLING:
2215         case BTM_BLE_APPEARANCE_CYCLING_COMPUTER:
2216         case BTM_BLE_APPEARANCE_CYCLING_SPEED:
2217         case BTM_BLE_APPEARANCE_CYCLING_CADENCE:
2218         case BTM_BLE_APPEARANCE_CYCLING_POWER:
2219         case BTM_BLE_APPEARANCE_CYCLING_SPEED_CADENCE:
2220         case BTM_BLE_APPEARANCE_GENERIC_OUTDOOR_SPORTS:
2221         case BTM_BLE_APPEARANCE_OUTDOOR_SPORTS_LOCATION:
2222         case BTM_BLE_APPEARANCE_OUTDOOR_SPORTS_LOCATION_AND_NAV:
2223         case BTM_BLE_APPEARANCE_OUTDOOR_SPORTS_LOCATION_POD:
2224         case BTM_BLE_APPEARANCE_OUTDOOR_SPORTS_LOCATION_POD_AND_NAV:
2225         default:
2226             dev_class[1] = BTM_COD_MAJOR_UNCLASSIFIED;
2227             dev_class[2] = BTM_COD_MINOR_UNCLASSIFIED;
2228     };
2229 }
2230 
2231 /*******************************************************************************
2232 **
2233 ** Function         btm_ble_update_inq_result
2234 **
2235 ** Description      Update adv packet information into inquiry result.
2236 **
2237 ** Parameters
2238 **
2239 ** Returns          void
2240 **
2241 *******************************************************************************/
btm_ble_update_inq_result(tINQ_DB_ENT * p_i,UINT8 addr_type,UINT8 evt_type,UINT8 * p)2242 BOOLEAN btm_ble_update_inq_result(tINQ_DB_ENT *p_i, UINT8 addr_type, UINT8 evt_type, UINT8 *p)
2243 {
2244     BOOLEAN             to_report = TRUE;
2245     tBTM_INQ_RESULTS     *p_cur = &p_i->inq_info.results;
2246     UINT8               len;
2247     UINT8               *p_flag;
2248     tBTM_INQUIRY_VAR_ST  *p_inq = &btm_cb.btm_inq_vars;
2249     UINT8                data_len, rssi;
2250     tBTM_BLE_INQ_CB     *p_le_inq_cb = &btm_cb.ble_ctr_cb.inq_var;
2251     UINT8 *p1;
2252     UINT8               *p_uuid16;
2253 
2254     STREAM_TO_UINT8    (data_len, p);
2255 
2256     if (data_len > BTM_BLE_ADV_DATA_LEN_MAX)
2257     {
2258         BTM_TRACE_WARNING("EIR data too long %d. discard", data_len);
2259         return FALSE;
2260     }
2261     btm_ble_cache_adv_data(p_cur, data_len, p, evt_type);
2262 
2263     p1 = (p + data_len);
2264     STREAM_TO_UINT8 (rssi, p1);
2265 
2266     /* Save the info */
2267     p_cur->inq_result_type = BTM_INQ_RESULT_BLE;
2268     p_cur->ble_addr_type    = addr_type;
2269     p_cur->rssi = rssi;
2270 
2271     /* active scan, always wait until get scan_rsp to report the result */
2272     if ((btm_cb.ble_ctr_cb.inq_var.scan_type == BTM_BLE_SCAN_MODE_ACTI &&
2273          (evt_type == BTM_BLE_CONNECT_EVT || evt_type == BTM_BLE_DISCOVER_EVT)))
2274     {
2275         BTM_TRACE_DEBUG("btm_ble_update_inq_result scan_rsp=false, to_report=false,\
2276                               scan_type_active=%d", btm_cb.ble_ctr_cb.inq_var.scan_type);
2277         p_i->scan_rsp = FALSE;
2278         to_report = FALSE;
2279     }
2280     else
2281         p_i->scan_rsp = TRUE;
2282 
2283     if (p_i->inq_count != p_inq->inq_counter)
2284         p_cur->device_type = BT_DEVICE_TYPE_BLE;
2285     else
2286         p_cur->device_type |= BT_DEVICE_TYPE_BLE;
2287 
2288     if (evt_type != BTM_BLE_SCAN_RSP_EVT)
2289         p_cur->ble_evt_type     = evt_type;
2290 
2291     p_i->inq_count = p_inq->inq_counter;   /* Mark entry for current inquiry */
2292 
2293     if (p_le_inq_cb->adv_len != 0)
2294     {
2295         if ((p_flag = BTM_CheckAdvData(p_le_inq_cb->adv_data_cache, BTM_BLE_AD_TYPE_FLAG, &len)) != NULL)
2296             p_cur->flag = * p_flag;
2297     }
2298 
2299     if (p_le_inq_cb->adv_len != 0)
2300     {
2301         /* Check to see the BLE device has the Appearance UUID in the advertising data.  If it does
2302          * then try to convert the appearance value to a class of device value Bluedroid can use.
2303          * Otherwise fall back to trying to infer if it is a HID device based on the service class.
2304          */
2305         p_uuid16 = BTM_CheckAdvData(p_le_inq_cb->adv_data_cache, BTM_BLE_AD_TYPE_APPEARANCE, &len);
2306         if (p_uuid16 && len == 2)
2307         {
2308             btm_ble_appearance_to_cod((UINT16)p_uuid16[0] | (p_uuid16[1] << 8), p_cur->dev_class);
2309         }
2310         else
2311         {
2312             if ((p_uuid16 = BTM_CheckAdvData(p_le_inq_cb->adv_data_cache,
2313                                              BTM_BLE_AD_TYPE_16SRV_CMPL, &len)) != NULL)
2314             {
2315                 UINT8 i;
2316                 for (i = 0; i + 2 <= len; i = i + 2)
2317                 {
2318                     /* if this BLE device support HID over LE, set HID Major in class of device */
2319                     if ((p_uuid16[i] | (p_uuid16[i+1] << 8)) == UUID_SERVCLASS_LE_HID)
2320                     {
2321                         p_cur->dev_class[0] = 0;
2322                         p_cur->dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
2323                         p_cur->dev_class[2] = 0;
2324                         break;
2325                     }
2326                 }
2327             }
2328         }
2329     }
2330 
2331     /* if BR/EDR not supported is not set, assume is a DUMO device */
2332     if ((p_cur->flag & BTM_BLE_BREDR_NOT_SPT) == 0 &&
2333          evt_type != BTM_BLE_CONNECT_DIR_EVT)
2334     {
2335         if (p_cur->ble_addr_type != BLE_ADDR_RANDOM)
2336         {
2337             BTM_TRACE_DEBUG("BR/EDR NOT support bit not set, treat as DUMO");
2338             p_cur->device_type |= BT_DEVICE_TYPE_DUMO;
2339         } else {
2340             BTM_TRACE_DEBUG("Random address, treating device as LE only");
2341         }
2342     }
2343     else
2344     {
2345         BTM_TRACE_DEBUG("BR/EDR NOT SUPPORT bit set, LE only device");
2346     }
2347 
2348     return to_report;
2349 
2350 }
2351 
2352 /*******************************************************************************
2353 **
2354 ** Function         btm_send_sel_conn_callback
2355 **
2356 ** Description      send selection connection request callback.
2357 **
2358 ** Parameters
2359 **
2360 ** Returns          void
2361 **
2362 *******************************************************************************/
btm_send_sel_conn_callback(BD_ADDR remote_bda,UINT8 evt_type,UINT8 * p_data,UINT8 addr_type)2363 void btm_send_sel_conn_callback(BD_ADDR remote_bda, UINT8 evt_type, UINT8 *p_data, UINT8 addr_type)
2364 {
2365     UINT8   data_len, len;
2366     UINT8   *p_dev_name, remname[31] = {0};
2367     UNUSED(addr_type);
2368 
2369     if (btm_cb.ble_ctr_cb.p_select_cback == NULL ||
2370         /* non-connectable device */
2371         (evt_type != BTM_BLE_EVT_CONN_ADV && evt_type != BTM_BLE_EVT_CONN_DIR_ADV))
2372         return;
2373 
2374     STREAM_TO_UINT8    (data_len, p_data);
2375 
2376     /* get the device name if exist in ADV data */
2377     if (data_len != 0)
2378     {
2379         p_dev_name = BTM_CheckAdvData(p_data, BTM_BLE_AD_TYPE_NAME_CMPL, &len);
2380 
2381         if (p_dev_name == NULL)
2382             p_dev_name = BTM_CheckAdvData(p_data, BTM_BLE_AD_TYPE_NAME_SHORT, &len);
2383 
2384         if (p_dev_name)
2385             memcpy(remname, p_dev_name, len);
2386     }
2387     /* allow connection */
2388     if ((* btm_cb.ble_ctr_cb.p_select_cback)(remote_bda, remname))
2389     {
2390         /* terminate selective connection, initiate connection */
2391         btm_ble_initiate_select_conn(remote_bda);
2392     }
2393 }
2394 
2395 /*******************************************************************************
2396 **
2397 ** Function         btm_ble_process_adv_pkt
2398 **
2399 ** Description      This function is called when adv packet report events are
2400 **                  received from the device. It updates the inquiry database.
2401 **                  If the inquiry database is full, the oldest entry is discarded.
2402 **
2403 ** Parameters
2404 **
2405 ** Returns          void
2406 **
2407 *******************************************************************************/
btm_ble_process_adv_pkt(UINT8 * p_data)2408 void btm_ble_process_adv_pkt (UINT8 *p_data)
2409 {
2410     BD_ADDR             bda;
2411     UINT8               evt_type = 0, *p = p_data;
2412     UINT8               addr_type = 0;
2413     UINT8               num_reports;
2414     UINT8               data_len;
2415 #if (defined BLE_PRIVACY_SPT && BLE_PRIVACY_SPT == TRUE)
2416     BOOLEAN             match = FALSE;
2417 #endif
2418 
2419     /* Extract the number of reports in this event. */
2420     STREAM_TO_UINT8(num_reports, p);
2421 
2422     while (num_reports--)
2423     {
2424         /* Extract inquiry results */
2425         STREAM_TO_UINT8    (evt_type, p);
2426         STREAM_TO_UINT8    (addr_type, p);
2427         STREAM_TO_BDADDR   (bda, p);
2428 
2429 #ifdef BTM_BLE_PC_ADV_TEST_MODE /* For general stack code (e.g. BTInsight testing), we simply do not define it to exclude or set it to TRUE to include */
2430         if (BTM_BLE_PC_ADV_TEST_MODE)   /* For stack component, it is always defined and maps to a global variable g_bDraculaAdvertisingMode */
2431         {
2432             if (btm_cb.ble_ctr_cb.p_scan_req_cback)
2433                 (*btm_cb.ble_ctr_cb.p_scan_req_cback)(bda, addr_type, evt_type);
2434         }
2435 #endif
2436 
2437 #if (defined BLE_PRIVACY_SPT && BLE_PRIVACY_SPT == TRUE)
2438     /* map address to security record */
2439     btm_public_addr_to_random_pseudo(bda, &addr_type);
2440     BTM_TRACE_ERROR("new address: %02x:%02x:%02x:%02x:%02x:%02x",
2441                      bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);
2442 #endif
2443 
2444         /* Only process the results if the inquiry is still active */
2445         if (!BTM_BLE_IS_SCAN_ACTIVE(btm_cb.ble_ctr_cb.scan_activity))
2446             return;
2447 
2448     BTM_TRACE_DEBUG("btm_ble_process_adv_pkt:bda= %0x:%0x:%0x:%0x:%0x:%0x",
2449                                      bda[0],bda[1],bda[2],bda[3],bda[4],bda[5]);
2450 #if (defined BLE_PRIVACY_SPT && BLE_PRIVACY_SPT == TRUE)
2451 #if SMP_INCLUDED == TRUE
2452         /* always do RRA resolution on host */
2453         if (!match && BTM_BLE_IS_RESOLVE_BDA(bda))
2454         {
2455             btm_ble_resolve_random_addr(bda, btm_ble_resolve_random_addr_on_adv, p_data);
2456         }
2457         else
2458 #endif
2459 #endif
2460         {
2461             btm_ble_process_adv_pkt_cont(bda, addr_type, evt_type, p);
2462         }
2463 
2464         STREAM_TO_UINT8(data_len, p);
2465 
2466         /* Advance to the next event data_len + rssi byte */
2467         p += data_len + 1;
2468     }
2469 }
2470 
2471 /*******************************************************************************
2472 **
2473 ** Function         btm_ble_process_adv_pkt_cont
2474 **
2475 ** Description      This function is called after random address resolution is
2476 **                  done, and proceed to process adv packet.
2477 **
2478 ** Parameters
2479 **
2480 ** Returns          void
2481 **
2482 *******************************************************************************/
btm_ble_process_adv_pkt_cont(BD_ADDR bda,UINT8 addr_type,UINT8 evt_type,UINT8 * p)2483 static void btm_ble_process_adv_pkt_cont(BD_ADDR bda, UINT8 addr_type, UINT8 evt_type, UINT8 *p)
2484 {
2485     tINQ_DB_ENT          *p_i;
2486     tBTM_INQUIRY_VAR_ST  *p_inq = &btm_cb.btm_inq_vars;
2487     tBTM_INQ_RESULTS_CB  *p_inq_results_cb = p_inq->p_inq_results_cb;
2488     tBTM_INQ_RESULTS_CB  *p_obs_results_cb = btm_cb.ble_ctr_cb.p_obs_results_cb;
2489     tBTM_BLE_INQ_CB      *p_le_inq_cb = &btm_cb.ble_ctr_cb.inq_var;
2490     BOOLEAN     update = TRUE;
2491     UINT8       result = 0;
2492 
2493     p_i = btm_inq_db_find (bda);
2494 
2495     /* Check if this address has already been processed for this inquiry */
2496     if (btm_inq_find_bdaddr(bda))
2497     {
2498         /* never been report as an LE device */
2499         if (p_i &&
2500             (!(p_i->inq_info.results.device_type & BT_DEVICE_TYPE_BLE) ||
2501               /* scan repsonse to be updated */
2502               (!p_i->scan_rsp)))
2503         {
2504             update = TRUE;
2505         }
2506         else if (BTM_BLE_IS_OBS_ACTIVE(btm_cb.ble_ctr_cb.scan_activity))
2507         {
2508             update = FALSE;
2509         }
2510         else
2511         {
2512             /* if yes, skip it */
2513             return; /* assumption: one result per event */
2514         }
2515     }
2516     /* If existing entry, use that, else get  a new one (possibly reusing the oldest) */
2517     if (p_i == NULL)
2518     {
2519         if ((p_i = btm_inq_db_new (bda)) != NULL)
2520         {
2521             p_inq->inq_cmpl_info.num_resp++;
2522         }
2523         else
2524             return;
2525     }
2526     else if (p_i->inq_count != p_inq->inq_counter) /* first time seen in this inquiry */
2527     {
2528         p_inq->inq_cmpl_info.num_resp++;
2529     }
2530     /* update the LE device information in inquiry database */
2531     if (!btm_ble_update_inq_result(p_i, addr_type, evt_type, p))
2532         return;
2533 
2534     if ((result = btm_ble_is_discoverable(bda, evt_type, p)) == 0)
2535     {
2536         BTM_TRACE_ERROR("discard adv pkt");
2537         return;
2538     }
2539     if (!update)
2540         result &= ~BTM_BLE_INQ_RESULT;
2541 #if BTM_USE_INQ_RESULTS_FILTER == TRUE
2542     /* If the number of responses found and limited, issue a cancel inquiry */
2543     if (p_inq->inqparms.max_resps &&
2544         p_inq->inq_cmpl_info.num_resp == p_inq->inqparms.max_resps)
2545     {
2546         /* new device */
2547         if (p_i == NULL ||
2548             (/* assume a DUMO device, BR/EDR inquiry is always active */
2549              p_i && p_i->inq_info.results.device_type == BT_DEVICE_TYPE_BLE && p_i->scan_rsp))
2550         {
2551             BTM_TRACE_WARNING("INQ RES: Extra Response Received...cancelling inquiry..");
2552 
2553             /* if is non-periodic inquiry active, cancel now */
2554             if ((p_inq->inq_active & BTM_BR_INQ_ACTIVE_MASK) != 0 &&
2555                 (p_inq->inq_active & BTM_PERIODIC_INQUIRY_ACTIVE) == 0)
2556                 btsnd_hcic_inq_cancel();
2557 
2558             btm_ble_stop_inquiry();
2559 
2560 #if BTM_BUSY_LEVEL_CHANGE_INCLUDED == TRUE
2561             btm_acl_update_busy_level (BTM_BLI_INQ_DONE_EVT);
2562 #endif
2563         }
2564     }
2565 #endif
2566     /* background connection in selective connection mode */
2567     if (btm_cb.ble_ctr_cb.bg_conn_type == BTM_BLE_CONN_SELECTIVE)
2568     {
2569         if (result & BTM_BLE_SEL_CONN_RESULT)
2570             btm_send_sel_conn_callback(bda, evt_type, p, addr_type);
2571         else
2572         {
2573             BTM_TRACE_DEBUG("None LE device, can not initiate selective connection");
2574         }
2575     }
2576     else
2577     {
2578         if (p_inq_results_cb && (result & BTM_BLE_INQ_RESULT))
2579         {
2580             (p_inq_results_cb)((tBTM_INQ_RESULTS *) &p_i->inq_info.results, p_le_inq_cb->adv_data_cache);
2581         }
2582         if (p_obs_results_cb && (result & BTM_BLE_OBS_RESULT))
2583         {
2584             (p_obs_results_cb)((tBTM_INQ_RESULTS *) &p_i->inq_info.results, p_le_inq_cb->adv_data_cache);
2585         }
2586     }
2587 }
2588 
2589 /*******************************************************************************
2590 **
2591 ** Function         btm_ble_start_scan
2592 **
2593 ** Description      Start the BLE scan.
2594 **
2595 ** Returns          void
2596 **
2597 *******************************************************************************/
btm_ble_start_scan(UINT8 filter_enable)2598 tBTM_STATUS btm_ble_start_scan (UINT8 filter_enable)
2599 {
2600     tBTM_BLE_INQ_CB *p_inq = &btm_cb.ble_ctr_cb.inq_var;
2601     tBTM_STATUS status = BTM_CMD_STARTED;
2602 
2603     /* start scan, disable duplicate filtering */
2604     if (!btsnd_hcic_ble_set_scan_enable (BTM_BLE_SCAN_ENABLE, filter_enable)) {
2605         status = BTM_NO_RESOURCES;
2606         btm_cb.ble_ctr_cb.wl_state &= ~BTM_BLE_WL_SCAN;
2607     }
2608     else
2609     {
2610         btm_cb.ble_ctr_cb.wl_state |= BTM_BLE_WL_SCAN;
2611         if (p_inq->scan_type == BTM_BLE_SCAN_MODE_ACTI)
2612             btm_ble_set_topology_mask(BTM_BLE_STATE_ACTIVE_SCAN_BIT);
2613         else
2614             btm_ble_set_topology_mask(BTM_BLE_STATE_PASSIVE_SCAN_BIT);
2615     }
2616     return status;
2617 }
2618 
2619 /*******************************************************************************
2620 **
2621 ** Function         btm_ble_stop_scan
2622 **
2623 ** Description      Stop the BLE scan.
2624 **
2625 ** Returns          void
2626 **
2627 *******************************************************************************/
btm_ble_stop_scan(void)2628 void btm_ble_stop_scan(void)
2629 {
2630     BTM_TRACE_EVENT ("btm_ble_stop_scan ");
2631 
2632     /* Clear the inquiry callback if set */
2633     btm_cb.ble_ctr_cb.inq_var.scan_type = BTM_BLE_SCAN_MODE_NONE;
2634 
2635     /* stop discovery now */
2636     btsnd_hcic_ble_set_scan_enable (BTM_BLE_SCAN_DISABLE, BTM_BLE_DUPLICATE_ENABLE);
2637 
2638     btm_update_scanner_filter_policy(SP_ADV_ALL);
2639 
2640     btm_cb.ble_ctr_cb.wl_state &= ~BTM_BLE_WL_SCAN;
2641 }
2642 /*******************************************************************************
2643 **
2644 ** Function         btm_ble_stop_inquiry
2645 **
2646 ** Description      Stop the BLE Inquiry.
2647 **
2648 ** Returns          void
2649 **
2650 *******************************************************************************/
btm_ble_stop_inquiry(void)2651 void btm_ble_stop_inquiry(void)
2652 {
2653     tBTM_INQUIRY_VAR_ST *p_inq = &btm_cb.btm_inq_vars;
2654     tBTM_BLE_CB *p_ble_cb = &btm_cb.ble_ctr_cb;
2655 
2656     btu_stop_timer (&p_ble_cb->inq_var.inq_timer_ent);
2657 
2658     p_ble_cb->scan_activity &=  ~BTM_BLE_INQUIRY_MASK;
2659 
2660     /* If no more scan activity, stop LE scan now */
2661     if (!BTM_BLE_IS_SCAN_ACTIVE(p_ble_cb->scan_activity))
2662         btm_ble_stop_scan();
2663 
2664     /* If we have a callback registered for inquiry complete, call it */
2665     BTM_TRACE_DEBUG ("BTM Inq Compl Callback: status 0x%02x, num results %d",
2666                       p_inq->inq_cmpl_info.status, p_inq->inq_cmpl_info.num_resp);
2667 
2668     btm_process_inq_complete(HCI_SUCCESS, (UINT8)(p_inq->inqparms.mode & BTM_BLE_INQUIRY_MASK));
2669 }
2670 
2671 /*******************************************************************************
2672 **
2673 ** Function         btm_ble_stop_observe
2674 **
2675 ** Description      Stop the BLE Observe.
2676 **
2677 ** Returns          void
2678 **
2679 *******************************************************************************/
btm_ble_stop_observe(void)2680 static void btm_ble_stop_observe(void)
2681 {
2682     tBTM_BLE_CB *p_ble_cb = & btm_cb.ble_ctr_cb;
2683     tBTM_CMPL_CB *p_obs_cb = p_ble_cb->p_obs_cmpl_cb;
2684 
2685     btu_stop_timer (&p_ble_cb->obs_timer_ent);
2686 
2687     p_ble_cb->scan_activity &= ~BTM_LE_OBSERVE_ACTIVE;
2688 
2689     p_ble_cb->p_obs_results_cb = NULL;
2690     p_ble_cb->p_obs_cmpl_cb = NULL;
2691 
2692     if (!BTM_BLE_IS_SCAN_ACTIVE(p_ble_cb->scan_activity))
2693         btm_ble_stop_scan();
2694 
2695     if (p_obs_cb)
2696         (p_obs_cb)((tBTM_INQUIRY_CMPL *) &btm_cb.btm_inq_vars.inq_cmpl_info);
2697 }
2698 /*******************************************************************************
2699 **
2700 ** Function         btm_ble_adv_states_operation
2701 **
2702 ** Description      Set or clear adv states in topology mask
2703 **
2704 ** Returns          operation status. TRUE if sucessful, FALSE otherwise.
2705 **
2706 *******************************************************************************/
2707 typedef BOOLEAN (BTM_TOPOLOGY_FUNC_PTR)(tBTM_BLE_STATE_MASK);
btm_ble_adv_states_operation(BTM_TOPOLOGY_FUNC_PTR * p_handler,UINT8 adv_evt)2708 static BOOLEAN btm_ble_adv_states_operation(BTM_TOPOLOGY_FUNC_PTR *p_handler, UINT8 adv_evt)
2709 {
2710     BOOLEAN rt = FALSE;
2711 
2712     switch (adv_evt)
2713     {
2714     case BTM_BLE_CONNECT_EVT:
2715         rt  = (*p_handler)(BTM_BLE_STATE_CONN_ADV_BIT);
2716         break;
2717 
2718     case  BTM_BLE_NON_CONNECT_EVT:
2719         rt  = (*p_handler) (BTM_BLE_STATE_NON_CONN_ADV_BIT);
2720         break;
2721     case BTM_BLE_CONNECT_DIR_EVT:
2722         rt  =  (*p_handler) (BTM_BLE_STATE_HI_DUTY_DIR_ADV_BIT);
2723         break;
2724 
2725     case BTM_BLE_DISCOVER_EVT:
2726         rt  =  (*p_handler) (BTM_BLE_STATE_SCAN_ADV_BIT);
2727         break;
2728 
2729     default:
2730         BTM_TRACE_ERROR("unknown adv event : %d", adv_evt);
2731         break;
2732     }
2733 
2734     return rt;
2735 }
2736 
2737 
2738 /*******************************************************************************
2739 **
2740 ** Function         btm_ble_start_adv
2741 **
2742 ** Description      start the BLE advertising.
2743 **
2744 ** Returns          void
2745 **
2746 *******************************************************************************/
btm_ble_start_adv(void)2747 tBTM_STATUS btm_ble_start_adv(void)
2748 {
2749     tBTM_BLE_INQ_CB *p_cb = &btm_cb.ble_ctr_cb.inq_var;
2750     tBTM_STATUS     rt = BTM_NO_RESOURCES;
2751 
2752     if (!btm_ble_adv_states_operation (btm_ble_topology_check, p_cb->evt_type))
2753         return BTM_WRONG_MODE;
2754 
2755     if (p_cb->afp != AP_SCAN_CONN_ALL)
2756     {
2757         btm_execute_wl_dev_operation();
2758         btm_cb.ble_ctr_cb.wl_state |= BTM_BLE_WL_ADV;
2759     }
2760 
2761     if (btsnd_hcic_ble_set_adv_enable (BTM_BLE_ADV_ENABLE))
2762     {
2763         if (p_cb->afp != AP_SCAN_CONN_ALL)
2764              btm_cb.ble_ctr_cb.wl_state |= BTM_BLE_WL_ADV;
2765 
2766          p_cb->adv_mode = BTM_BLE_ADV_ENABLE;
2767 
2768          rt = BTM_SUCCESS;
2769     }
2770     else
2771     {
2772         p_cb->adv_mode = BTM_BLE_ADV_DISABLE;
2773         btm_cb.ble_ctr_cb.wl_state &= ~BTM_BLE_WL_ADV;
2774     }
2775     return rt;
2776 }
2777 /*******************************************************************************
2778 **
2779 ** Function         btm_ble_stop_adv
2780 **
2781 ** Description      Stop the BLE advertising.
2782 **
2783 ** Returns          void
2784 **
2785 *******************************************************************************/
btm_ble_stop_adv(void)2786 tBTM_STATUS btm_ble_stop_adv(void)
2787 {
2788     tBTM_BLE_INQ_CB *p_cb = &btm_cb.ble_ctr_cb.inq_var;
2789     tBTM_STATUS rt = BTM_SUCCESS;
2790 
2791     if (p_cb->adv_mode == BTM_BLE_ADV_ENABLE)
2792     {
2793         if (btsnd_hcic_ble_set_adv_enable (BTM_BLE_ADV_DISABLE))
2794         {
2795             p_cb->fast_adv_on = FALSE;
2796             p_cb->adv_mode = BTM_BLE_ADV_DISABLE;
2797             btm_cb.ble_ctr_cb.wl_state &= ~BTM_BLE_WL_ADV;
2798 
2799             /* clear all adv states */
2800             btm_ble_clear_topology_mask (BTM_BLE_STATE_ALL_ADV_MASK);
2801         }
2802         else
2803             rt = BTM_NO_RESOURCES;
2804     }
2805     return rt;
2806 }
2807 
2808 
2809 /*******************************************************************************
2810 **
2811 ** Function         btm_ble_set_topology_mask
2812 **
2813 ** Description      set BLE topology mask
2814 **
2815 ** Returns          TRUE is request is allowed, FALSE otherwise.
2816 **
2817 *******************************************************************************/
btm_ble_set_topology_mask(tBTM_BLE_STATE_MASK request_state_mask)2818 BOOLEAN btm_ble_set_topology_mask(tBTM_BLE_STATE_MASK request_state_mask)
2819 {
2820     BOOLEAN rt = TRUE;
2821 
2822     request_state_mask &= BTM_BLE_STATE_ALL_MASK;
2823 
2824     btm_cb.ble_ctr_cb.cur_states |= request_state_mask;
2825 
2826     return rt;
2827 }
2828 /*******************************************************************************
2829 **
2830 ** Function         btm_ble_clear_topology_mask
2831 **
2832 ** Description      Clear BLE topology bit mask
2833 **
2834 ** Returns          TRUE is request is allowed, FALSE otherwise.
2835 **
2836 *******************************************************************************/
btm_ble_clear_topology_mask(tBTM_BLE_STATE_MASK request_state_mask)2837 BOOLEAN btm_ble_clear_topology_mask (tBTM_BLE_STATE_MASK request_state_mask)
2838 {
2839     request_state_mask &= BTM_BLE_STATE_ALL_MASK;
2840 
2841     btm_cb.ble_ctr_cb.cur_states &= ~request_state_mask;
2842 
2843     return TRUE;
2844 }
2845 /*******************************************************************************
2846 **
2847 ** Function         btm_ble_update_mode_operation
2848 **
2849 ** Description      This function update the GAP role operation when a link status
2850 **                  is updated.
2851 **
2852 ** Returns          void
2853 **
2854 *******************************************************************************/
btm_ble_update_mode_operation(UINT8 link_role,BD_ADDR bd_addr,UINT8 status)2855 void btm_ble_update_mode_operation(UINT8 link_role, BD_ADDR bd_addr, UINT8 status)
2856 {
2857     tACL_CONN   *pa = &btm_cb.acl_db[0];
2858     UINT16       xx;
2859     UINT16       mask = BTM_BLE_STATE_ALL_CONN_MASK;
2860 
2861     UNUSED(bd_addr);
2862 
2863     if (link_role == HCI_ROLE_SLAVE)
2864     {
2865         btm_cb.ble_ctr_cb.inq_var.adv_mode  = BTM_BLE_ADV_DISABLE;
2866         /* clear all adv states */
2867         mask |= BTM_BLE_STATE_ALL_ADV_MASK;
2868     }
2869 
2870     btm_ble_clear_topology_mask (mask);
2871 
2872     /* check the device link role maps */
2873     for (xx = 0; xx < MAX_L2CAP_LINKS; xx++, pa++)
2874     {
2875         if (pa->in_use && pa->transport == BT_TRANSPORT_LE)
2876         {
2877             if (pa->link_role == HCI_ROLE_MASTER)
2878                 btm_ble_set_topology_mask (BTM_BLE_STATE_MASTER_BIT);
2879             else
2880                 btm_ble_set_topology_mask (BTM_BLE_STATE_SLAVE_BIT);
2881         }
2882     }
2883 
2884     if (btm_cb.cmn_ble_vsc_cb.adv_inst_max == 0 &&
2885         btm_cb.ble_ctr_cb.inq_var.connectable_mode == BTM_BLE_CONNECTABLE)
2886     {
2887         btm_ble_set_connectability ( btm_cb.ble_ctr_cb.inq_var.connectable_mode );
2888     }
2889 
2890     if (btm_ble_get_conn_st() == BLE_CONN_IDLE && status != HCI_ERR_HOST_REJECT_RESOURCES)
2891     {
2892         if (!btm_send_pending_direct_conn())
2893         {
2894             btm_ble_resume_bg_conn();
2895         }
2896     }
2897 }
2898 
2899 /*******************************************************************************
2900 **
2901 ** Function         btm_ble_start_slow_adv
2902 **
2903 ** Description      Restart adv with slow adv interval
2904 **
2905 ** Returns          void
2906 **
2907 *******************************************************************************/
btm_ble_start_slow_adv(void)2908 static void btm_ble_start_slow_adv (void)
2909 {
2910     tBTM_BLE_INQ_CB         *p_cb = &btm_cb.ble_ctr_cb.inq_var;
2911     BD_ADDR                 p_addr_ptr= {0};
2912 
2913     if (p_cb->adv_mode == BTM_BLE_ADV_ENABLE)
2914     {
2915         btm_ble_stop_adv();
2916 
2917         btsnd_hcic_ble_write_adv_params (BTM_BLE_GAP_ADV_SLOW_INT,
2918                                          BTM_BLE_GAP_ADV_SLOW_INT,
2919                                          p_cb->evt_type,
2920                                          p_cb->adv_addr_type,
2921                                          btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type,/* slow adv
2922                                          mode never goes into directed adv */
2923                                          p_addr_ptr,
2924                                          p_cb->adv_chnl_map,
2925                                          p_cb->afp);
2926 
2927         btm_ble_start_adv();
2928     }
2929 }
2930 /*******************************************************************************
2931 **
2932 ** Function         btm_ble_timeout
2933 **
2934 ** Description      Called when BTM BLE inquiry timer expires
2935 **
2936 ** Returns          void
2937 **
2938 *******************************************************************************/
btm_ble_timeout(TIMER_LIST_ENT * p_tle)2939 void btm_ble_timeout(TIMER_LIST_ENT *p_tle)
2940 {
2941     BTM_TRACE_EVENT ("btm_ble_timeout");
2942 
2943     switch (p_tle->event)
2944     {
2945         case BTU_TTYPE_BLE_OBSERVE:
2946             btm_ble_stop_observe();
2947             break;
2948 
2949         case BTU_TTYPE_BLE_INQUIRY:
2950             btm_ble_stop_inquiry();
2951             break;
2952 
2953         case BTU_TTYPE_BLE_GAP_LIM_DISC:
2954             /* lim_timeout expiried, limited discovery should exit now */
2955             btm_cb.btm_inq_vars.discoverable_mode &= ~BTM_BLE_LIMITED_DISCOVERABLE;
2956             btm_ble_set_adv_flag(btm_cb.btm_inq_vars.connectable_mode, btm_cb.btm_inq_vars.discoverable_mode);
2957             break;
2958 
2959         case BTU_TTYPE_BLE_RANDOM_ADDR:
2960             if (btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type == BLE_ADDR_RANDOM)
2961             {
2962                 if (NULL == (void *)(p_tle->param))
2963                 {
2964                     /* refresh the random addr */
2965                     btm_gen_resolvable_private_addr((void *)btm_gen_resolve_paddr_low);
2966                 }
2967                 else
2968                 {
2969                     if (BTM_BleMaxMultiAdvInstanceCount() > 0)
2970                        btm_ble_multi_adv_configure_rpa((tBTM_BLE_MULTI_ADV_INST*)p_tle->param);
2971                 }
2972             }
2973             break;
2974 
2975         case BTU_TTYPE_BLE_GAP_FAST_ADV:
2976             /* fast adv is completed, fall back to slow adv interval */
2977             btm_ble_start_slow_adv();
2978             break;
2979 
2980         default:
2981             break;
2982 
2983     }
2984 }
2985 
2986 
2987 /*******************************************************************************
2988 **
2989 ** Function         btm_ble_read_remote_features_complete
2990 **
2991 ** Description      This function is called when the command complete message
2992 **                  is received from the HCI for the read LE remote feature supported
2993 **                  complete event.
2994 **
2995 ** Returns          void
2996 **
2997 *******************************************************************************/
btm_ble_read_remote_features_complete(UINT8 * p)2998 void btm_ble_read_remote_features_complete(UINT8 *p)
2999 {
3000     tACL_CONN        *p_acl_cb = &btm_cb.acl_db[0];
3001     UINT16            handle;
3002     UINT8             status;
3003     int               xx;
3004 
3005     BTM_TRACE_EVENT ("btm_ble_read_remote_features_complete ");
3006 
3007     STREAM_TO_UINT8(status, p);
3008     /* if LE read remote feature failed, expect disconnect complete to be received */
3009     if (status == HCI_SUCCESS)
3010     {
3011         STREAM_TO_UINT16 (handle, p);
3012 
3013         /* Look up the connection by handle and copy features */
3014         for (xx = 0; xx < MAX_L2CAP_LINKS; xx++, p_acl_cb++)
3015         {
3016             if ((p_acl_cb->in_use) && (p_acl_cb->hci_handle == handle))
3017             {
3018                 STREAM_TO_ARRAY(p_acl_cb->peer_le_features, p, BD_FEATURES_LEN);
3019                 /*notify link up here */
3020                 l2cble_notify_le_connection (p_acl_cb->remote_addr);
3021                 break;
3022             }
3023         }
3024     }
3025 
3026 }
3027 
3028 /*******************************************************************************
3029 **
3030 ** Function         btm_ble_write_adv_enable_complete
3031 **
3032 ** Description      This function process the write adv enable command complete.
3033 **
3034 ** Returns          void
3035 **
3036 *******************************************************************************/
btm_ble_write_adv_enable_complete(UINT8 * p)3037 void btm_ble_write_adv_enable_complete(UINT8 * p)
3038 {
3039     tBTM_BLE_INQ_CB *p_cb = &btm_cb.ble_ctr_cb.inq_var;
3040 
3041     /* if write adv enable/disbale not succeed */
3042     if (*p != HCI_SUCCESS)
3043     {
3044         /* toggle back the adv mode */
3045         p_cb->adv_mode = !p_cb->adv_mode;
3046     }
3047 }
3048 
3049 /*******************************************************************************
3050 **
3051 ** Function         btm_ble_dir_adv_tout
3052 **
3053 ** Description      when directed adv time out
3054 **
3055 ** Returns          void
3056 **
3057 *******************************************************************************/
btm_ble_dir_adv_tout(void)3058 void btm_ble_dir_adv_tout(void)
3059 {
3060     btm_cb.ble_ctr_cb.inq_var.adv_mode = BTM_BLE_ADV_DISABLE;
3061 
3062     /* make device fall back into undirected adv mode by default */
3063     btm_cb.ble_ctr_cb.inq_var.directed_conn = FALSE;
3064 }
3065 
3066 /*******************************************************************************
3067 **
3068 ** Function         btm_ble_init
3069 **
3070 ** Description      Initialize the control block variable values.
3071 **
3072 ** Returns          void
3073 **
3074 *******************************************************************************/
btm_ble_init(void)3075 void btm_ble_init (void)
3076 {
3077     tBTM_BLE_CB *p_cb = &btm_cb.ble_ctr_cb;
3078 
3079     BTM_TRACE_EVENT ("btm_ble_init ");
3080 
3081     memset(p_cb, 0, sizeof(tBTM_BLE_CB));
3082     memset(&(btm_cb.cmn_ble_vsc_cb), 0 , sizeof(tBTM_BLE_VSC_CB));
3083     btm_cb.cmn_ble_vsc_cb.values_read = FALSE;
3084     p_cb->cur_states       = 0;
3085 
3086     p_cb->inq_var.adv_mode = BTM_BLE_ADV_DISABLE;
3087     p_cb->inq_var.scan_type = BTM_BLE_SCAN_MODE_NONE;
3088     p_cb->inq_var.adv_chnl_map = BTM_BLE_DEFAULT_ADV_CHNL_MAP;
3089     p_cb->inq_var.afp = BTM_BLE_DEFAULT_AFP;
3090     p_cb->inq_var.sfp = BTM_BLE_DEFAULT_SFP;
3091     p_cb->inq_var.connectable_mode = BTM_BLE_NON_CONNECTABLE;
3092     p_cb->inq_var.discoverable_mode = BTM_BLE_NON_DISCOVERABLE;
3093 
3094     /* for background connection, reset connection params to be undefined */
3095     p_cb->scan_int = p_cb->scan_win = BTM_BLE_CONN_PARAM_UNDEF;
3096 
3097     p_cb->inq_var.evt_type = BTM_BLE_NON_CONNECT_EVT;
3098 
3099 #if BLE_VND_INCLUDED == FALSE
3100     btm_ble_vendor_init(BTM_CS_IRK_LIST_MAX);
3101     btm_ble_adv_filter_init();
3102 #endif
3103 }
3104 
3105 /*******************************************************************************
3106 **
3107 ** Function         btm_ble_topology_check
3108 **
3109 ** Description      check to see requested state is supported. One state check at
3110 **                  a time is supported
3111 **
3112 ** Returns          TRUE is request is allowed, FALSE otherwise.
3113 **
3114 *******************************************************************************/
btm_ble_topology_check(tBTM_BLE_STATE_MASK request_state_mask)3115 BOOLEAN btm_ble_topology_check(tBTM_BLE_STATE_MASK request_state_mask)
3116 {
3117     BOOLEAN rt = FALSE;
3118     UINT32  llt_mask = 0;
3119     UINT8   *p;
3120 
3121     UINT8   state_offset = 0;
3122     UINT16  cur_states = btm_cb.ble_ctr_cb.cur_states;
3123     UINT8   mask, offset;
3124     UINT8   request_state = 0;
3125 
3126     /* check only one bit is set and within valid range */
3127     if (request_state_mask == BTM_BLE_STATE_INVALID ||
3128         request_state_mask > BTM_BLE_STATE_SCAN_ADV_BIT ||
3129         (request_state_mask & (request_state_mask -1 )) != 0)
3130     {
3131         BTM_TRACE_ERROR("illegal state requested: %d", request_state_mask);
3132         return rt;
3133     }
3134 
3135     while (request_state_mask)
3136     {
3137         request_state_mask >>= 1;
3138         request_state ++;
3139     }
3140 
3141     /* check if the requested state is supported or not */
3142     mask = btm_le_state_combo_tbl[0][request_state - 1][0];
3143     offset = btm_le_state_combo_tbl[0][request_state-1][1];
3144 
3145     if (!BTM_LE_STATES_SUPPORTED(btm_cb.devcb.le_supported_states, mask, offset))
3146     {
3147         BTM_TRACE_ERROR("state requested not supported: %d", request_state);
3148         return rt;
3149     }
3150 
3151     rt = TRUE;
3152     /* make sure currently active states are all supported in conjunction with the requested
3153        state. If the bit in table is not set, the combination is not supported */
3154     while (cur_states != 0)
3155     {
3156         if (cur_states & 0x01)
3157         {
3158             mask = btm_le_state_combo_tbl[request_state][state_offset][0];
3159             offset = btm_le_state_combo_tbl[request_state][state_offset][1];
3160 
3161             if (mask != 0 && offset != 0)
3162             {
3163                 if (!BTM_LE_STATES_SUPPORTED(btm_cb.devcb.le_supported_states, mask, offset))
3164                 {
3165                     rt = FALSE;
3166                     break;
3167                 }
3168             }
3169         }
3170         cur_states >>= 1;
3171         state_offset ++;
3172     }
3173     return rt;
3174 }
3175 
3176 
3177 #endif  /* BLE_INCLUDED */
3178