blob: 608fa7eba522a1e7ca81319c81397710ec1f0297 [file] [log] [blame]
The Android Open Source Project5738f832012-12-12 16:00:35 -08001/******************************************************************************
2 *
3 * Copyright (C) 2008-2012 Broadcom Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19/******************************************************************************
20 *
21 * This file contains functions for BLE GAP.
22 *
23 ******************************************************************************/
24
25#include <string.h>
26#include <stdio.h>
27#include <stddef.h>
28
29#include "bt_types.h"
Mike J. Chen5cd8bff2014-01-31 18:16:59 -080030#include "bt_utils.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080031#include "btu.h"
32#include "btm_int.h"
33#include "hcimsgs.h"
34#if (GAP_INCLUDED == TRUE)
35#include "gap_api.h"
36#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -080037#if (BLE_INCLUDED == TRUE)
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080038#include "gattdefs.h"
39
The Android Open Source Project5738f832012-12-12 16:00:35 -080040#define BTM_BLE_NAME_SHORT 0x01
41#define BTM_BLE_NAME_CMPL 0x02
42
43#define BTM_BLE_FILTER_TARGET_UNKNOWN 0xff
44#define BTM_BLE_POLICY_UNKNOWN 0xff
45
The Android Open Source Project5738f832012-12-12 16:00:35 -080046#define BTM_EXT_BLE_RMT_NAME_TIMEOUT 30
The Android Open Source Project5738f832012-12-12 16:00:35 -080047
48/*******************************************************************************
49** Local functions
50*******************************************************************************/
51static void btm_ble_update_adv_flag(UINT8 flag);
52static void btm_ble_process_adv_pkt_cont(BD_ADDR bda, UINT8 addr_type, UINT8 evt_type, UINT8 *p);
53static UINT8 *btm_ble_build_adv_data(tBTM_BLE_AD_MASK *p_data_mask, UINT8 **p_dst, tBTM_BLE_ADV_DATA *p_data);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080054static UINT8 btm_set_conn_mode_adv_init_addr(tBTM_BLE_INQ_CB *p_cb,
55 BD_ADDR_PTR p_addr_ptr,
56 tBLE_ADDR_TYPE *p_init_addr_type,
57 tBLE_ADDR_TYPE *p_own_addr_type);
58static BOOLEAN btm_ble_start_adv(void);
59static tBTM_STATUS btm_ble_stop_adv(void);
The Android Open Source Project5738f832012-12-12 16:00:35 -080060
61
62
63/*******************************************************************************
64**
65** Function BTM_BleReset
66**
67** Description This function is called to reset ULP controller.
68**
69** Parameters None.
70**
71** Returns void
72**
73*******************************************************************************/
74void BTM_BleReset(void)
75{
76 btsnd_hcic_ble_reset();
77}
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080078/*******************************************************************************
79**
80** Function BTM_BleUpdateAdvWhitelist
81**
82** Description Add or remove device from advertising white list
83**
84** Returns void
85**
86*******************************************************************************/
87BOOLEAN BTM_BleUpdateAdvWhitelist(BOOLEAN add_remove, BD_ADDR remote_bda)
88{
Mike J. Chen5cd8bff2014-01-31 18:16:59 -080089 UNUSED(add_remove);
90 UNUSED(remote_bda);
91
92 return FALSE;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080093}
The Android Open Source Project5738f832012-12-12 16:00:35 -080094
95/*******************************************************************************
96**
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080097** Function BTM_BleUpdateAdvFilterPolicy
98**
99** Description This function update the filter policy of advertiser.
100**
101** Parameter adv_policy: advertising filter policy
102**
103** Return void
104*******************************************************************************/
105void BTM_BleUpdateAdvFilterPolicy(tBTM_BLE_AFP adv_policy)
106{
107 tBTM_BLE_INQ_CB *p_cb = &btm_cb.ble_ctr_cb.inq_var;
108 tBLE_ADDR_TYPE init_addr_type = BLE_ADDR_PUBLIC;
109 BD_ADDR p_addr_ptr= {0};
110 UINT8 adv_mode = p_cb->adv_mode;
111
112 BTM_TRACE_EVENT0 ("BTM_BleUpdateAdvFilterPolicy");
113
Andre Eisenbach3aa60542013-03-22 18:00:51 -0700114 if (!HCI_LE_HOST_SUPPORTED(btm_cb.devcb.local_lmp_features[HCI_EXT_FEATURES_PAGE_1]))
115 return;
116
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800117 if (p_cb->afp != adv_policy)
118 {
119 p_cb->afp = adv_policy;
120
121 /* if adv active, stop and restart */
122 btm_ble_stop_adv ();
123
124 if (p_cb->connectable_mode & BTM_BLE_CONNECTABLE)
125 p_cb->evt_type = btm_set_conn_mode_adv_init_addr(p_cb, p_addr_ptr, &init_addr_type, &p_cb->adv_addr_type);
126
127 btsnd_hcic_ble_write_adv_params (p_cb->adv_interval_min,
128 p_cb->adv_interval_max,
129 p_cb->evt_type,
130 p_cb->adv_addr_type,
131 init_addr_type,
132 p_addr_ptr,
133 p_cb->adv_chnl_map,
134 p_cb->afp);
135
136 if (adv_mode == BTM_BLE_ADV_ENABLE)
137 btm_ble_start_adv ();
138
139 }
140}
141/*******************************************************************************
142**
The Android Open Source Project5738f832012-12-12 16:00:35 -0800143** Function BTM_BleObserve
144**
145** Description This procedure keep the device listening for advertising
146** events from a broadcast device.
147**
148** Parameters start: start or stop observe.
149** white_list: use white list in observer mode or not.
150**
151** Returns void
152**
153*******************************************************************************/
154tBTM_STATUS BTM_BleObserve(BOOLEAN start, UINT8 duration,
155 tBTM_INQ_RESULTS_CB *p_results_cb, tBTM_CMPL_CB *p_cmpl_cb)
156{
157 tBTM_BLE_INQ_CB *p_inq = &btm_cb.ble_ctr_cb.inq_var;
158 tBTM_STATUS status = BTM_NO_RESOURCES;
159
Nitin Arora0830ad42014-02-21 18:20:37 -0800160 BTM_TRACE_EVENT1 ("BTM_BleObserve : scan_type:%d",btm_cb.btm_inq_vars.scan_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800161
Andre Eisenbach3aa60542013-03-22 18:00:51 -0700162 if (!HCI_LE_HOST_SUPPORTED(btm_cb.devcb.local_lmp_features[HCI_EXT_FEATURES_PAGE_1]))
163 return BTM_ILLEGAL_VALUE;
164
The Android Open Source Project5738f832012-12-12 16:00:35 -0800165 if (start)
166 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800167 /* shared inquiry database, do not allow observe if any inquiry is active */
168 if (btm_cb.btm_inq_vars.inq_active || p_inq->proc_mode != BTM_BLE_INQUIRY_NONE)
Nitin Arora0830ad42014-02-21 18:20:37 -0800169 {
170 /*check if an interleave scan is already in progress*/
171 if(btm_cb.btm_inq_vars.scan_type == INQ_GENERAL
172 && btm_cb.btm_inq_vars.p_inq_results_cb != NULL)
173 {
174 BTM_TRACE_EVENT0 ("BTM_BleObserve general inq in progress, redirecting the results");
175 btm_cb.btm_inq_vars.p_inq_ble_results_cb = p_results_cb;
176 btm_cb.btm_inq_vars.p_inq_ble_cmpl_cb = p_cmpl_cb;
177 return BTM_SUCCESS;
178 }
179 else
180 return BTM_BUSY;
181 }
182 btm_cb.btm_inq_vars.scan_type = INQ_LE_OBSERVE;
183 btm_cb.btm_inq_vars.p_inq_ble_results_cb = p_results_cb;
184 btm_cb.btm_inq_vars.p_inq_ble_cmpl_cb = p_cmpl_cb;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800185 p_inq->scan_type = (p_inq->scan_type == BTM_BLE_SCAN_MODE_NONE) ? BTM_BLE_SCAN_MODE_ACTI: p_inq->scan_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800186
187 /* allow config scanning type */
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800188 if (btsnd_hcic_ble_set_scan_params (p_inq->scan_type,
The Android Open Source Project5738f832012-12-12 16:00:35 -0800189 (UINT16)(!p_inq->scan_interval ? BTM_BLE_GAP_DISC_SCAN_INT : p_inq->scan_interval),
190 (UINT16)(!p_inq->scan_window ? BTM_BLE_GAP_DISC_SCAN_WIN : p_inq->scan_window),
191 BLE_ADDR_PUBLIC,
192 BTM_BLE_DEFAULT_SFP)) /* assume observe always not using white list */
193 {
194 /* start scan, disable duplicate filtering */
195 if (btsnd_hcic_ble_set_scan_enable (BTM_BLE_SCAN_ENABLE, BTM_BLE_DUPLICATE_DISABLE))
196 {
197 status = BTM_SUCCESS;
198 p_inq->proc_mode = BTM_BLE_OBSERVE;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800199 btm_cb.btm_inq_vars.inq_active |= BTM_LE_OBSERVE_ACTIVE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800200
201 if (duration != 0)
202 {
203 /* start inquiry timer */
204 btu_start_timer (&p_inq->inq_timer_ent, BTU_TTYPE_BLE_INQUIRY, duration);
205 }
206 }
207 }
208 }
Nitin Arora0830ad42014-02-21 18:20:37 -0800209 else/*start = 0*/
The Android Open Source Project5738f832012-12-12 16:00:35 -0800210 {
Nitin Arora0830ad42014-02-21 18:20:37 -0800211 if(btm_cb.btm_inq_vars.scan_type == INQ_GENERAL)
212 {
213 //Dont stop the scan. Just nullify the cbs
214 btm_cb.btm_inq_vars.p_inq_ble_results_cb = NULL;
215 btm_cb.btm_inq_vars.p_inq_ble_cmpl_cb = NULL;
216 }
217 else if (p_inq->proc_mode == BTM_BLE_OBSERVE)
218 {
219 btm_cb.btm_inq_vars.inq_active &= ~BTM_LE_OBSERVE_ACTIVE;
220 btm_ble_stop_scan();
221 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800222 }
223
224 return status;
225}
226
227/*******************************************************************************
228**
229** Function BTM_BleBroadcast
230**
231** Description This function is to start or stop broadcasting.
232**
233** Parameters start: start or stop broadcasting.
234**
235** Returns status.
236**
237*******************************************************************************/
238tBTM_STATUS BTM_BleBroadcast(BOOLEAN start)
239{
240 tBTM_STATUS status = BTM_NO_RESOURCES;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800241 tBTM_LE_RANDOM_CB *p_addr_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800242 tBTM_BLE_INQ_CB *p_cb = &btm_cb.ble_ctr_cb.inq_var;
243 UINT8 evt_type = p_cb->scan_rsp ? BTM_BLE_DISCOVER_EVT: BTM_BLE_NON_CONNECT_EVT;
244
Andre Eisenbach3aa60542013-03-22 18:00:51 -0700245 if (!HCI_LE_HOST_SUPPORTED(btm_cb.devcb.local_lmp_features[HCI_EXT_FEATURES_PAGE_1]))
246 return BTM_ILLEGAL_VALUE;
247
The Android Open Source Project5738f832012-12-12 16:00:35 -0800248#ifdef BTM_BLE_PC_ADV_TEST_MODE
249 if (BTM_BLE_PC_ADV_TEST_MODE)
250 {
251 evt_type = p_cb->scan_rsp ? BTM_BLE_CONNECT_EVT: BTM_BLE_NON_CONNECT_EVT;
252 }
253#endif
254
255 if (start && p_cb->adv_mode == BTM_BLE_ADV_DISABLE)
256 {
257 /* update adv params */
258 if (!btsnd_hcic_ble_write_adv_params ((UINT16)(p_cb->adv_interval_min ? p_cb->adv_interval_min : BTM_BLE_GAP_ADV_INT),
259 (UINT16)(p_cb->adv_interval_max ? p_cb->adv_interval_max : BTM_BLE_GAP_ADV_INT),
260 evt_type,
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800261 p_addr_cb->own_addr_type,
262 p_cb->direct_bda.type,
263 p_cb->direct_bda.bda,
The Android Open Source Project5738f832012-12-12 16:00:35 -0800264 p_cb->adv_chnl_map,
265 p_cb->afp))
266
267 status = BTM_NO_RESOURCES;
268 else
269 p_cb->evt_type = evt_type;
270
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800271 status = btm_ble_start_adv ();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800272 }
273 else if (!start && p_cb->adv_mode == BTM_BLE_ADV_ENABLE)
274 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800275 status = btm_ble_stop_adv();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800276 }
277 else
278 {
279 status = BTM_WRONG_MODE;
280 BTM_TRACE_ERROR2("Can not %s Broadcast, device %s in Broadcast mode",
281 (start ? "Start" : "Stop"), (start ? "alerady" :"not"));
282 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800283 return status;
284}
285
The Android Open Source Project5738f832012-12-12 16:00:35 -0800286/*******************************************************************************
287**
288** Function BTM_RegisterScanReqEvt
289**
290** Description This function is called to register a scan request callback
291** on the advertiser.
292**
293** Parameters p_scan_req_cback: scan request callback. If NULL, remove the
294** registration.
295**
296** Returns void
297**
298*******************************************************************************/
299void BTM_RegisterScanReqEvt(tBTM_BLE_SCAN_REQ_CBACK *p_scan_req_cback)
300{
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800301#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 */
302 if (BTM_BLE_PC_ADV_TEST_MODE) /* For stack component, it is always defined and maps to a global variable g_bDraculaAdvertisingMode */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800303 {
304 tBTM_BLE_CB *p_cb = &btm_cb.ble_ctr_cb;
305 p_cb->p_scan_req_cback = p_scan_req_cback;
306 }
Mike J. Chen5cd8bff2014-01-31 18:16:59 -0800307#else
308 UNUSED(p_scan_req_cback);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800309#endif
310}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800311
Zhihai Xu8b35b3f2014-03-11 15:01:45 -0700312#if BTM_BLE_PRIVACY_SPT == TRUE
313/*******************************************************************************
314**
315** Function btm_ble_resolve_random_addr_on_adv
316**
317** Description resolve random address complete callback.
318**
319** Returns void
320**
321*******************************************************************************/
322static void btm_ble_resolve_random_addr_on_adv(void * p_rec, void *p)
323{
324 tBTM_SEC_DEV_REC *match_rec = (tBTM_SEC_DEV_REC *) p_rec;
325 UINT8 addr_type = BLE_ADDR_RANDOM;
326 BD_ADDR bda;
327 UINT8 *pp = (UINT8 *)p + 1;
328 UINT8 evt_type;
329
330 BTM_TRACE_EVENT0 ("btm_ble_resolve_random_addr_on_adv ");
331
332 STREAM_TO_UINT8 (evt_type, pp);
333 STREAM_TO_UINT8 (addr_type, pp);
334 STREAM_TO_BDADDR (bda, pp);
335
336 if (match_rec)
337 {
338 BTM_TRACE_ERROR0("Random match");
339 match_rec->ble.active_addr_type = BTM_BLE_ADDR_RRA;
340 memcpy(match_rec->ble.cur_rand_addr, bda, BD_ADDR_LEN);
341 memcpy(bda, match_rec->bd_addr, BD_ADDR_LEN);
342 addr_type = match_rec->ble.ble_addr_type;
343 }
344 else
345 {
346 BTM_TRACE_ERROR0("Random unmatch");
347 }
348
349 btm_ble_process_adv_pkt_cont(bda, addr_type, evt_type, pp);
350
351 return;
352}
353#endif
354
The Android Open Source Project5738f832012-12-12 16:00:35 -0800355/*******************************************************************************
356**
357** Function BTM_BleSetBgConnType
358**
359** Description This function is called to set BLE connectable mode for a
360** peripheral device.
361**
362** Parameters bg_conn_type: it can be auto connection, or selective connection.
363** p_select_cback: callback function when selective connection procedure
364** is being used.
365**
366** Returns void
367**
368*******************************************************************************/
369BOOLEAN BTM_BleSetBgConnType(tBTM_BLE_CONN_TYPE bg_conn_type,
370 tBTM_BLE_SEL_CBACK *p_select_cback)
371{
372 BOOLEAN started = TRUE;
373
374 BTM_TRACE_EVENT0 ("BTM_BleSetBgConnType ");
Andre Eisenbach3aa60542013-03-22 18:00:51 -0700375 if (!HCI_LE_HOST_SUPPORTED(btm_cb.devcb.local_lmp_features[HCI_EXT_FEATURES_PAGE_1]))
376 return FALSE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800377
378 if (btm_cb.ble_ctr_cb.bg_conn_type != bg_conn_type)
379 {
380 switch (bg_conn_type)
381 {
382 case BTM_BLE_CONN_AUTO:
383 btm_ble_start_auto_conn(TRUE);
384 break;
385
386 case BTM_BLE_CONN_SELECTIVE:
387 if (btm_cb.ble_ctr_cb.bg_conn_type == BTM_BLE_CONN_AUTO)
388 {
389 btm_ble_start_auto_conn(FALSE);
390 }
391 started = btm_ble_start_select_conn(TRUE, p_select_cback);
392 break;
393
394 case BTM_BLE_CONN_NONE:
395 if (btm_cb.ble_ctr_cb.bg_conn_type == BTM_BLE_CONN_AUTO)
396 {
397 btm_ble_start_auto_conn(FALSE);
398 }
399 else if (btm_cb.ble_ctr_cb.bg_conn_type == BTM_BLE_CONN_SELECTIVE)
400 {
401 btm_ble_start_select_conn(FALSE, NULL);
402 }
403 started = TRUE;
404 break;
405
406 default:
407 BTM_TRACE_ERROR1("invalid bg connection type : %d ", bg_conn_type);
408 started = FALSE;
409 break;
410 }
411
412 if (started)
413 btm_cb.ble_ctr_cb.bg_conn_type = bg_conn_type;
414 }
415 return started;
416}
417
418/*******************************************************************************
419**
420** Function BTM_BleUpdateBgConnDev
421**
422** Description This function is called to add or remove a device into/from
423** background connection procedure. The background connection
424* procedure is decided by the background connection type, it can be
425* auto connection, or selective connection.
426**
427** Parameters add_remove: TRUE to add; FALSE to remove.
428** remote_bda: device address to add/remove.
429**
430** Returns void
431**
432*******************************************************************************/
433BOOLEAN BTM_BleUpdateBgConnDev(BOOLEAN add_remove, BD_ADDR remote_bda)
434{
The Android Open Source Project5738f832012-12-12 16:00:35 -0800435 BOOLEAN ret = TRUE;
Andre Eisenbach6975b4d2013-08-05 16:55:38 -0700436 UINT8 dev_wl_type = 0;
Andre Eisenbach946c6812013-04-04 14:53:04 -0700437 BTM_TRACE_EVENT0 (" BTM_BleUpdateBgConnDev");
438
439 /* update white list */
440 ret = btm_update_bg_conn_list(add_remove, remote_bda, &dev_wl_type);
441
442 btm_update_dev_to_white_list(add_remove, remote_bda, dev_wl_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800443
The Android Open Source Project5738f832012-12-12 16:00:35 -0800444 return ret;
445}
446
447/*******************************************************************************
448**
449** Function BTM_BleSetConnMode
450**
451** Description This function is called to set BLE connectable mode for a
452** peripheral device.
453**
454** Parameters directed: is directed connectable mode, or non-directed.
455** p_dir_bda: connectable direct initiator's LE device address
456**
457** Returns void
458**
459*******************************************************************************/
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800460tBTM_STATUS BTM_BleSetConnMode(BOOLEAN is_directed)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800461{
462 tBTM_BLE_INQ_CB *p_cb = &btm_cb.ble_ctr_cb.inq_var;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800463
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800464 BTM_TRACE_EVENT1 ("BTM_BleSetConnMode is_directed = %d ", is_directed);
Andre Eisenbach3aa60542013-03-22 18:00:51 -0700465 if (!HCI_LE_HOST_SUPPORTED(btm_cb.devcb.local_lmp_features[HCI_EXT_FEATURES_PAGE_1]))
466 return BTM_ILLEGAL_VALUE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800467
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800468 p_cb->directed_conn = is_directed;
469 return btm_ble_set_connectability( p_cb->connectable_mode);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800470
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800471}
472
473/*******************************************************************************
474**
475** Function btm_set_conn_mode_adv_init_addr
476**
477** Description set initator address type and local address type based on adv
478** mode.
479**
480**
481*******************************************************************************/
482static UINT8 btm_set_conn_mode_adv_init_addr(tBTM_BLE_INQ_CB *p_cb,
483 BD_ADDR_PTR p_addr_ptr,
484 tBLE_ADDR_TYPE *p_init_addr_type,
485 tBLE_ADDR_TYPE *p_own_addr_type)
486{
487 UINT8 evt_type;
488
489 if ( p_cb->directed_conn)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800490 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800491 /* direct adv mode does not have privacy if privacy
492 is not enabled or no reconn addr config */
493 *p_own_addr_type = BLE_ADDR_PUBLIC;
494 *p_init_addr_type = p_cb->direct_bda.type;
495 memcpy(p_addr_ptr, p_cb->direct_bda.bda, BD_ADDR_LEN);
496 evt_type = BTM_BLE_CONNECT_DIR_EVT;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800497 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800498 else /* undirect adv mode */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800499 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800500 evt_type = BTM_BLE_CONNECT_EVT;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800501 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800502
503 return evt_type;
504
The Android Open Source Project5738f832012-12-12 16:00:35 -0800505}
506
507/*******************************************************************************
508**
509** Function BTM_BleSetAdvParams
510**
511** Description This function is called to set advertising parameters.
512**
513** Parameters adv_int_min: minimum advertising interval
514** adv_int_max: maximum advertising interval
515** p_dir_bda: connectable direct initiator's LE device address
516** chnl_map: advertising channel map.
517**
518** Returns void
519**
520*******************************************************************************/
521tBTM_STATUS BTM_BleSetAdvParams(UINT16 adv_int_min, UINT16 adv_int_max,
522 tBLE_BD_ADDR *p_dir_bda,
523 tBTM_BLE_ADV_CHNL_MAP chnl_map)
524{
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800525 tBTM_LE_RANDOM_CB *p_addr_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800526 tBTM_BLE_INQ_CB *p_cb = &btm_cb.ble_ctr_cb.inq_var;
527 tBTM_STATUS status = BTM_SUCCESS;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800528 BD_ADDR p_addr_ptr = {0};
529 tBLE_ADDR_TYPE init_addr_type = BLE_ADDR_PUBLIC;
530 tBLE_ADDR_TYPE own_addr_type = p_addr_cb->own_addr_type;
531 UINT8 adv_mode = p_cb->adv_mode;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800532
533 BTM_TRACE_EVENT0 ("BTM_BleSetAdvParams");
534
Andre Eisenbach3aa60542013-03-22 18:00:51 -0700535 if (!HCI_LE_HOST_SUPPORTED(btm_cb.devcb.local_lmp_features[HCI_EXT_FEATURES_PAGE_1]))
536 return BTM_ILLEGAL_VALUE;
537
The Android Open Source Project5738f832012-12-12 16:00:35 -0800538 if (!BTM_BLE_VALID_PRAM(adv_int_min, BTM_BLE_ADV_INT_MIN, BTM_BLE_ADV_INT_MAX) ||
539 !BTM_BLE_VALID_PRAM(adv_int_max, BTM_BLE_ADV_INT_MIN, BTM_BLE_ADV_INT_MAX))
540 {
541 return BTM_ILLEGAL_VALUE;
542 }
543
544 p_cb->adv_interval_min = adv_int_min;
545 p_cb->adv_interval_max = adv_int_max;
546 p_cb->adv_chnl_map = chnl_map;
547
548 if (p_dir_bda)
549 {
550 memcpy(&p_cb->direct_bda, p_dir_bda, sizeof(tBLE_BD_ADDR));
551 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800552
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800553 BTM_TRACE_EVENT0 ("update params for an active adv");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800554
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800555 btm_ble_stop_adv();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800556
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800557 if (p_cb->connectable_mode & BTM_BLE_CONNECTABLE)
558 p_cb->evt_type = btm_set_conn_mode_adv_init_addr(p_cb, p_addr_ptr, &init_addr_type, &own_addr_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800559
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800560 /* update adv params */
561 btsnd_hcic_ble_write_adv_params (p_cb->adv_interval_min,
562 p_cb->adv_interval_max,
563 p_cb->evt_type,
564 own_addr_type,
565 init_addr_type,
566 p_addr_ptr,
567 p_cb->adv_chnl_map,
568 p_cb->afp);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800569
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800570 if (adv_mode == BTM_BLE_ADV_ENABLE)
571 btm_ble_start_adv();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800572
573 return status;
574}
575
576/*******************************************************************************
577**
578** Function BTM_BleReadAdvParams
579**
580** Description This function is called to set advertising parameters.
581**
582** Parameters adv_int_min: minimum advertising interval
583** adv_int_max: maximum advertising interval
584** p_dir_bda: connectable direct initiator's LE device address
585** chnl_map: advertising channel map.
586**
587** Returns void
588**
589*******************************************************************************/
590void BTM_BleReadAdvParams (UINT16 *adv_int_min, UINT16 *adv_int_max,
591 tBLE_BD_ADDR *p_dir_bda, tBTM_BLE_ADV_CHNL_MAP *p_chnl_map)
592{
593 tBTM_BLE_INQ_CB *p_cb = &btm_cb.ble_ctr_cb.inq_var;
594
595 BTM_TRACE_EVENT0 ("BTM_BleReadAdvParams ");
Andre Eisenbach3aa60542013-03-22 18:00:51 -0700596 if (!HCI_LE_HOST_SUPPORTED(btm_cb.devcb.local_lmp_features[HCI_EXT_FEATURES_PAGE_1]))
597 return ;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800598
599 *adv_int_min = p_cb->adv_interval_min;
600 *adv_int_max = p_cb->adv_interval_max;
601 *p_chnl_map = p_cb->adv_chnl_map;
602
603 if (p_dir_bda != NULL)
604 {
605 memcpy(p_dir_bda, &p_cb->direct_bda, sizeof(tBLE_BD_ADDR));
606 }
607}
608
The Android Open Source Project5738f832012-12-12 16:00:35 -0800609/*******************************************************************************
610**
611** Function BTM_BleSetScanParams
612**
613** Description This function is called to set Scan parameters.
614**
615** Parameters adv_int_min: minimum advertising interval
616** adv_int_max: maximum advertising interval
617** p_dir_bda: connectable direct initiator's LE device address
618** chnl_map: advertising channel map.
619** scan_type: active scan or passive scan
620**
621** Returns void
622**
623*******************************************************************************/
624void BTM_BleSetScanParams(UINT16 scan_interval, UINT16 scan_window, tBTM_BLE_SCAN_MODE scan_mode)
625{
626 tBTM_BLE_INQ_CB *p_cb = &btm_cb.ble_ctr_cb.inq_var;
627
628 BTM_TRACE_EVENT0 (" BTM_BleSetScanParams");
Andre Eisenbach3aa60542013-03-22 18:00:51 -0700629 if (!HCI_LE_HOST_SUPPORTED(btm_cb.devcb.local_lmp_features[HCI_EXT_FEATURES_PAGE_1]))
630 return ;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800631
632 if (BTM_BLE_VALID_PRAM(scan_interval, BTM_BLE_SCAN_INT_MIN, BTM_BLE_SCAN_INT_MAX) &&
633 BTM_BLE_VALID_PRAM(scan_window, BTM_BLE_SCAN_WIN_MIN, BTM_BLE_SCAN_WIN_MAX) &&
634 (scan_mode == BTM_BLE_SCAN_MODE_ACTI || scan_mode == BTM_BLE_SCAN_MODE_PASS))
635 {
636 p_cb->scan_type = scan_mode;
637
638 if (BTM_BLE_CONN_PARAM_UNDEF != scan_interval)
639 p_cb->scan_interval = scan_interval;
640
641 if (BTM_BLE_CONN_PARAM_UNDEF != scan_window)
642 p_cb->scan_window = scan_window;
643 }
644 else
645 {
646 BTM_TRACE_ERROR2("Illegal params: scan_interval = %d scan_window = %d",
647 scan_interval, scan_window);
648 }
649
650}
651
652/*******************************************************************************
653**
654** Function BTM_BleWriteScanRsp
655**
656** Description This function is called to write LE scan response.
657**
658** Parameters: p_scan_rsp: scan response information.
659**
660** Returns void
661**
662*******************************************************************************/
663tBTM_STATUS BTM_BleWriteScanRsp(tBTM_BLE_AD_MASK data_mask, tBTM_BLE_ADV_DATA *p_data)
664{
665 tBTM_STATUS status = BTM_NO_RESOURCES;
666 UINT8 rsp_data[BTM_BLE_AD_DATA_LEN],
667 *p = rsp_data;
668
669 BTM_TRACE_EVENT0 (" BTM_BleWriteScanRsp");
Andre Eisenbach3aa60542013-03-22 18:00:51 -0700670
671 if (!HCI_LE_HOST_SUPPORTED(btm_cb.devcb.local_lmp_features[HCI_EXT_FEATURES_PAGE_1]))
672 return BTM_ILLEGAL_VALUE;
673
The Android Open Source Project5738f832012-12-12 16:00:35 -0800674 memset(rsp_data, 0, BTM_BLE_AD_DATA_LEN);
675 btm_ble_build_adv_data(&data_mask, &p, p_data);
676
677 if (btsnd_hcic_ble_set_scan_rsp_data((UINT8)(p - rsp_data), rsp_data))
678 {
679 status = BTM_SUCCESS;
680
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800681 if (data_mask != 0)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800682 btm_cb.ble_ctr_cb.inq_var.scan_rsp = TRUE;
683 else
684 btm_cb.ble_ctr_cb.inq_var.scan_rsp = FALSE;
685 }
686 else
687 status = BTM_ILLEGAL_VALUE;
688
689 return status;
690}
691
692/*******************************************************************************
693**
694** Function BTM_BleWriteAdvData
695**
696** Description This function is called to write advertising data.
697**
698** Parameters: None.
699**
700** Returns void
701**
702*******************************************************************************/
703tBTM_STATUS BTM_BleWriteAdvData(tBTM_BLE_AD_MASK data_mask, tBTM_BLE_ADV_DATA *p_data)
704{
705 tBTM_BLE_LOCAL_ADV_DATA *p_cb_data = &btm_cb.ble_ctr_cb.inq_var.adv_data;
706 UINT8 *p;
707 UINT16 mask = data_mask;
708
Andre Eisenbach3aa60542013-03-22 18:00:51 -0700709 BTM_TRACE_EVENT0 ("BTM_BleWriteAdvData ");
710
711 if (!HCI_LE_HOST_SUPPORTED(btm_cb.devcb.local_lmp_features[HCI_EXT_FEATURES_PAGE_1]))
712 return BTM_ILLEGAL_VALUE;
713
The Android Open Source Project5738f832012-12-12 16:00:35 -0800714 memset(p_cb_data, 0, sizeof(tBTM_BLE_LOCAL_ADV_DATA));
715 p = p_cb_data->ad_data;
716 p_cb_data->data_mask = data_mask;
717
The Android Open Source Project5738f832012-12-12 16:00:35 -0800718 p_cb_data->p_flags = btm_ble_build_adv_data(&mask, &p, p_data);
719
720 p_cb_data->p_pad = p;
721
722 if (data_mask != 0)
723 {
724 BTM_TRACE_ERROR0("Partial data write into ADV");
725 }
726
727 p_cb_data->data_mask &= ~mask;
728
729 if (btsnd_hcic_ble_set_adv_data((UINT8)(p_cb_data->p_pad - p_cb_data->ad_data),
730 p_cb_data->ad_data))
731 return BTM_SUCCESS;
732 else
733 return BTM_NO_RESOURCES;
734
735}
736
737/*******************************************************************************
738**
739** Function BTM_CheckAdvData
740**
741** Description This function is called to get ADV data for a specific type.
742**
743** Parameters p_adv - pointer of ADV data
744** type - finding ADV data type
745** p_length - return the length of ADV data not including type
746**
747** Returns pointer of ADV data
748**
749*******************************************************************************/
750UINT8 *BTM_CheckAdvData( UINT8 *p_adv, UINT8 type, UINT8 *p_length)
751{
752 UINT8 *p = p_adv;
753 UINT8 length;
754 UINT8 adv_type;
755 BTM_TRACE_API1("BTM_CheckAdvData type=0x%02X", type);
756
757 STREAM_TO_UINT8(length, p);
758
759 while ( length && (p - p_adv <= BTM_BLE_CACHE_ADV_DATA_MAX))
760 {
761 STREAM_TO_UINT8(adv_type, p);
762
763 if ( adv_type == type )
764 {
765 /* length doesn't include itself */
766 *p_length = length - 1; /* minus the length of type */
767 return p;
768 }
769 p += length - 1; /* skip the length of data */
770 STREAM_TO_UINT8(length, p);
771 }
772
773 *p_length = 0;
774 return NULL;
775}
776
777/*******************************************************************************
778**
779** Function btm_ble_build_adv_data
780**
781** Description This function is called build the adv data and rsp data.
782*******************************************************************************/
783static UINT8 *btm_ble_build_adv_data(tBTM_BLE_AD_MASK *p_data_mask, UINT8 **p_dst, tBTM_BLE_ADV_DATA *p_data)
784{
785 UINT16 data_mask = *p_data_mask;
786 UINT8 *p = *p_dst,
787 *p_flag = NULL;
788 UINT16 len = BTM_BLE_AD_DATA_LEN, cp_len = 0;
789 UINT8 i = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800790 tBTM_BLE_PROP_ELEM *p_elem;
791
792 BTM_TRACE_EVENT0 (" btm_ble_build_adv_data");
793
794 /* build the adv data structure and build the data string */
795 if (data_mask)
796 {
797 /* flags */
798 if (data_mask & BTM_BLE_AD_BIT_FLAGS)
799 {
800 *p++ = 2;
801 *p++ = BTM_BLE_AD_TYPE_FLAG;
802 p_flag = p;
803 if (p_data)
804 *p++ = p_data->flag;
805 else
806 *p++ = 0;
807
808 len -= 3;
809
810 data_mask &= ~BTM_BLE_AD_BIT_FLAGS;
811 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800812 /* appearance data */
813 if (len > 3 && data_mask & BTM_BLE_AD_BIT_APPEARANCE)
814 {
815 *p++ = 3; /* length */
816 *p++ = BTM_BLE_AD_TYPE_APPEARANCE;
817 UINT16_TO_STREAM(p, p_data->appearance);
818 len -= 4;
819
820 data_mask &= ~BTM_BLE_AD_BIT_APPEARANCE;
821 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800822 /* device name */
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800823#if BTM_MAX_LOC_BD_NAME_LEN > 0
The Android Open Source Project5738f832012-12-12 16:00:35 -0800824 if (len > 2 && data_mask & BTM_BLE_AD_BIT_DEV_NAME)
825 {
826 if (strlen(btm_cb.cfg.bd_name) > (UINT16)(len - 2))
827 {
828 *p++ = len - 2 + 1;
829 *p++ = BTM_BLE_AD_TYPE_NAME_SHORT;
830 ARRAY_TO_STREAM(p, btm_cb.cfg.bd_name, len - 2);
831 }
832 else
833 {
834 cp_len = (UINT16)strlen(btm_cb.cfg.bd_name);
835 *p++ = cp_len + 1;
836 *p++ = BTM_BLE_AD_TYPE_NAME_CMPL;
837 ARRAY_TO_STREAM(p, btm_cb.cfg.bd_name, cp_len);
838 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800839 len -= (cp_len + 2);
840 data_mask &= ~BTM_BLE_AD_BIT_DEV_NAME;
841 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800842#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -0800843 /* manufacturer data */
844 if (len > 2 && data_mask & BTM_BLE_AD_BIT_MANU &&
845 p_data && p_data->manu.len != 0 && p_data->manu.p_val)
846 {
847 if (p_data->manu.len > (len - 2))
848 cp_len = len - 2;
849 else
850 cp_len = p_data->manu.len;
851
852 *p++ = cp_len + 1;
853 *p++ = BTM_BLE_AD_TYPE_MANU;
854 ARRAY_TO_STREAM(p, p_data->manu.p_val, cp_len);
855
856 len -= (cp_len + 2);
857 data_mask &= ~BTM_BLE_AD_BIT_MANU;
858 }
859 /* TX power */
860 if (len > 2 && data_mask & BTM_BLE_AD_BIT_TX_PWR)
861 {
862 *p++ = 2;
863 *p++ = BTM_BLE_AD_TYPE_TX_PWR;
864 *p++ = btm_cb.ble_ctr_cb.inq_var.tx_power;
865 len -= 3;
866
867 data_mask &= ~BTM_BLE_AD_BIT_TX_PWR;
868 }
869 /* services */
870 if (len > 2 && data_mask & BTM_BLE_AD_BIT_SERVICE &&
871 p_data && p_data->services.num_service != 0 &&
872 p_data->services.p_uuid)
873 {
874 if (p_data->services.num_service * 2 > (len - 2))
875 {
876 cp_len = (len - 2)/2;
877 *p ++ = 1 + cp_len * 2;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800878 *p++ = BTM_BLE_AD_TYPE_16SRV_PART;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800879 }
880 else
881 {
882 cp_len = p_data->services.num_service;
883 *p++ = 1 + cp_len * 2;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800884 *p++ = BTM_BLE_AD_TYPE_16SRV_CMPL;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800885 }
886 for (i = 0; i < cp_len; i ++)
887 {
888 UINT16_TO_STREAM(p, *(p_data->services.p_uuid + i));
889 }
890
891 len -= (cp_len * 2 + 2);
892 data_mask &= ~BTM_BLE_AD_BIT_SERVICE;
893 }
894 if (len >= 6 && data_mask & BTM_BLE_AD_BIT_INT_RANGE &&
895 p_data)
896 {
897 *p++ = 5;
898 *p++ = BTM_BLE_AD_TYPE_INT_RANGE;
899 UINT16_TO_STREAM(p, p_data->int_range.low);
900 UINT16_TO_STREAM(p, p_data->int_range.hi);
901 len -= 6;
902 data_mask &= ~BTM_BLE_AD_BIT_INT_RANGE;
903 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800904 if (data_mask & BTM_BLE_AD_BIT_PROPRIETARY && p_data && p_data->p_proprietary)
905 {
906 for (i = 0; i < p_data->p_proprietary->num_elem ; i ++)
907 {
908 p_elem = p_data->p_proprietary->p_elem + i;
909
910 if (len >= (2 + p_elem->len))/* len byte(1) + ATTR type(1) + Uuid len(2) + value length */
911 {
912 *p ++ = p_elem->len + 1; /* Uuid len + value length */
913 *p ++ = p_elem->adv_type;
914 ARRAY_TO_STREAM(p, p_elem->p_val, p_elem->len);
915
916 len -= (2 + p_elem->len);
917 }
918 else
919 {
920 BTM_TRACE_WARNING0("data exceed max adv packet length");
921 break;
922 }
923 }
924 data_mask &= ~BTM_BLE_AD_BIT_PROPRIETARY;
925 }
926 }
927
928 *p_data_mask = data_mask;
929 *p_dst = p;
930
931 return p_flag;
932}
933
934/*******************************************************************************
935**
936** Function btm_ble_set_discoverability
937**
938** Description This function is called to set BLE discoverable mode.
939**
940** Parameters: mode: discoverability mode.
941**
942** Returns void
943**
944*******************************************************************************/
945tBTM_STATUS btm_ble_set_discoverability(UINT16 combined_mode)
946{
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800947 tBTM_LE_RANDOM_CB *p_addr_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800948 tBTM_BLE_INQ_CB *p_cb = &btm_cb.ble_ctr_cb.inq_var;
949 UINT16 mode = (combined_mode & BTM_BLE_DISCOVERABLE_MASK);
950 UINT8 flag = 0;
951 UINT8 new_mode = BTM_BLE_ADV_ENABLE;
952 UINT8 evt_type = (p_cb->connectable_mode == BTM_BLE_NON_CONNECTABLE) ? \
953 ((p_cb->scan_rsp) ? BTM_BLE_DISCOVER_EVT : BTM_BLE_NON_CONNECT_EVT )\
954 : BTM_BLE_CONNECT_EVT;
955 tBTM_STATUS status = BTM_SUCCESS;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800956 BD_ADDR p_addr_ptr= {0};
957 tBLE_ADDR_TYPE init_addr_type = BLE_ADDR_PUBLIC,
958 own_addr_type = p_addr_cb->own_addr_type;;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800959
960 BTM_TRACE_EVENT2 ("btm_ble_set_discoverability mode=0x%0x combined_mode=0x%x", mode, combined_mode);
961
962 /*** Check mode parameter ***/
963 if (mode > BTM_BLE_MAX_DISCOVERABLE)
964 return(BTM_ILLEGAL_VALUE);
965
966 p_cb->br_edr_supported_flag |= (combined_mode & BTM_DISCOVERABLE_MASK);
967 p_cb->discoverable_mode = mode;
968
969 if (!p_cb->br_edr_supported_flag)
970 {
971 flag = BTM_BLE_BREDR_NOT_SPT;
972 BTM_TRACE_DEBUG1("btm_ble_set_discoverability (BREDR not sup)flag=0x%x",flag);
973 }
974
975 BTM_TRACE_DEBUG1 ("br_edr_supported=0x%x", p_cb->br_edr_supported_flag);
976
977 if (mode == BTM_BLE_LIMITED_DISCOVERABLE || mode == BTM_BLE_GENERAL_DISCOVERABLE)
978 {
979 BTM_TRACE_EVENT0 ("mode == BTM_BLE_LIMITED_DISCOVERABLE ");
980 /* write ADV data with limited disc flag */
981 if (mode == BTM_BLE_LIMITED_DISCOVERABLE)
982 flag |= BTM_BLE_LIMIT_DISC_FLAG ;
983 else
984 flag |= BTM_BLE_GEN_DISC_FLAG;
985 }
986 else /* non-discoverable */
987 {
988 BTM_TRACE_EVENT0 ("mode == BTM_BLE_NON_DISCOVERABLE ");
989
990 if (p_cb->connectable_mode == BTM_BLE_NON_CONNECTABLE)
991 {
992 p_cb->br_edr_supported_flag = 0;
993
994 BTM_TRACE_EVENT0 ("always disable adv in non-discoverable non-connectable mode if no scan rsp ");
995 if (!p_cb->scan_rsp )
996 new_mode = BTM_BLE_ADV_DISABLE;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800997
The Android Open Source Project5738f832012-12-12 16:00:35 -0800998 }
999 else
1000 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001001 p_cb->evt_type = btm_set_conn_mode_adv_init_addr(p_cb, p_addr_ptr, &init_addr_type, &own_addr_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001002 }
1003 }
1004 btm_ble_update_adv_flag(flag);
1005
1006 /* update adv params if start advertising */
1007 BTM_TRACE_EVENT2 ("evt_type=0x%x p-cb->evt_type=0x%x ", evt_type, p_cb->evt_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001008 if (new_mode == BTM_BLE_ADV_ENABLE &&
1009 (evt_type != p_cb->evt_type ||p_cb->adv_addr_type != own_addr_type))
The Android Open Source Project5738f832012-12-12 16:00:35 -08001010 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001011 btm_ble_stop_adv();
The Android Open Source Project5738f832012-12-12 16:00:35 -08001012
1013 /* update adv params */
1014 if (!btsnd_hcic_ble_write_adv_params ((UINT16)(p_cb->adv_interval_min ? p_cb->adv_interval_min : BTM_BLE_GAP_ADV_INT),
1015 (UINT16)(p_cb->adv_interval_max ? p_cb->adv_interval_max : BTM_BLE_GAP_ADV_INT),
1016 evt_type,
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001017 own_addr_type,
1018 init_addr_type,
1019 p_addr_ptr,
The Android Open Source Project5738f832012-12-12 16:00:35 -08001020 p_cb->adv_chnl_map,
1021 p_cb->afp))
1022
1023 status = BTM_NO_RESOURCES;
1024 else
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001025 {
The Android Open Source Project5738f832012-12-12 16:00:35 -08001026 p_cb->evt_type = evt_type;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001027 p_cb->adv_addr_type = own_addr_type;
1028 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001029
1030 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001031
The Android Open Source Project5738f832012-12-12 16:00:35 -08001032 if (status == BTM_SUCCESS && p_cb->adv_mode != new_mode)
1033 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001034 if (new_mode == BTM_BLE_ADV_ENABLE)
1035 status = btm_ble_start_adv();
The Android Open Source Project5738f832012-12-12 16:00:35 -08001036 else
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001037 status = btm_ble_stop_adv();
The Android Open Source Project5738f832012-12-12 16:00:35 -08001038 }
1039
1040 /* set up stop advertising timer */
1041 if (status == BTM_SUCCESS && mode == BTM_BLE_LIMITED_DISCOVERABLE)
1042 {
1043 BTM_TRACE_EVENT1 ("start timer for limited disc mode duration=%d (30 secs)", BTM_BLE_GAP_LIM_TOUT);
1044 /* start Tgap(lim_timeout) */
1045 btu_start_timer (&p_cb->inq_timer_ent, BTU_TTYPE_BLE_GAP_LIM_DISC,
1046 BTM_BLE_GAP_LIM_TOUT);
1047 }
1048 return status;
1049}
1050
1051/*******************************************************************************
1052**
1053** Function btm_ble_set_connectability
1054**
1055** Description This function is called to set BLE connectability mode.
1056**
1057** Parameters: mode: connectability mode.
1058**
1059** Returns void
1060**
1061*******************************************************************************/
1062tBTM_STATUS btm_ble_set_connectability(UINT16 combined_mode)
1063{
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001064 tBTM_LE_RANDOM_CB *p_addr_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001065 tBTM_BLE_INQ_CB *p_cb = &btm_cb.ble_ctr_cb.inq_var;
1066 UINT16 mode = (combined_mode & BTM_BLE_CONNECTABLE_MASK);
1067 UINT8 cur_flag = 0;
1068 UINT8 cur_br_edr_not_sup_flag;
1069 UINT8 new_flag;
1070 UINT8 new_mode = BTM_BLE_ADV_ENABLE;
1071 UINT8 evt_type = (p_cb->scan_rsp) ? BTM_BLE_DISCOVER_EVT: BTM_BLE_NON_CONNECT_EVT;
1072 tBTM_STATUS status = BTM_SUCCESS;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001073 BD_ADDR p_addr_ptr = {0};
1074 tBLE_ADDR_TYPE init_addr_type = BLE_ADDR_PUBLIC,
1075 own_addr_type = p_addr_cb->own_addr_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001076
1077 BTM_TRACE_EVENT2 ("btm_ble_set_connectability mode=0x%0x combined_mode=0x%x", mode, combined_mode);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001078 /*** Check mode parameter ***/
1079 if (mode > BTM_BLE_MAX_CONNECTABLE)
1080 return(BTM_ILLEGAL_VALUE);
1081 if (btm_cb.ble_ctr_cb.inq_var.adv_data.p_flags)
1082 cur_flag = *btm_cb.ble_ctr_cb.inq_var.adv_data.p_flags ;
1083 cur_br_edr_not_sup_flag = (cur_flag & ((UINT8) BTM_BLE_BREDR_NOT_SPT));
1084
1085 p_cb->br_edr_supported_flag |= ((combined_mode & BTM_CONNECTABLE_MASK) << 4);
1086 if (p_cb->br_edr_supported_flag && cur_br_edr_not_sup_flag)
1087 {
1088 new_flag = cur_flag & ((UINT8) (~BTM_BLE_BREDR_NOT_SPT));
1089 BTM_TRACE_EVENT2 ("new flag=0x%x cur flag=0x%x",new_flag, cur_flag);
1090 btm_ble_update_adv_flag(new_flag);
1091 }
1092 p_cb->connectable_mode = mode;
1093
1094 if (mode == BTM_BLE_NON_CONNECTABLE)
1095 {
1096 if (p_cb->discoverable_mode == BTM_BLE_NON_DISCOVERABLE)
1097 {
1098 p_cb->br_edr_supported_flag = 0;
1099 BTM_TRACE_EVENT0 ("always disable adv in non-discoverable non-connectable mode with no scan rsp");
1100 if(!p_cb->scan_rsp)
1101 new_mode = BTM_BLE_ADV_DISABLE;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001102
The Android Open Source Project5738f832012-12-12 16:00:35 -08001103 }
1104 }
1105 else /* connectable */
1106 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001107 evt_type = btm_set_conn_mode_adv_init_addr(p_cb, p_addr_ptr, &init_addr_type, &own_addr_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001108 }
1109
1110 /* update adv params if needed */
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001111 if ((p_cb->evt_type != evt_type || p_cb->adv_addr_type != p_addr_cb->own_addr_type)
1112 && new_mode == BTM_BLE_ADV_ENABLE)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001113 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001114 btm_ble_stop_adv();
The Android Open Source Project5738f832012-12-12 16:00:35 -08001115
1116 if (!btsnd_hcic_ble_write_adv_params ((UINT16)(p_cb->adv_interval_min ? p_cb->adv_interval_min : BTM_BLE_GAP_ADV_INT),
1117 (UINT16)(p_cb->adv_interval_max ? p_cb->adv_interval_max : BTM_BLE_GAP_ADV_INT),
1118 evt_type,
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001119 own_addr_type,
1120 init_addr_type,
1121 p_addr_ptr,
The Android Open Source Project5738f832012-12-12 16:00:35 -08001122 p_cb->adv_chnl_map,
1123 p_cb->afp))
1124 status = BTM_NO_RESOURCES;
1125 else
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001126 {
The Android Open Source Project5738f832012-12-12 16:00:35 -08001127 p_cb->evt_type = evt_type;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001128 p_cb->adv_addr_type = own_addr_type;
1129 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001130 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001131
The Android Open Source Project5738f832012-12-12 16:00:35 -08001132 /* update advertising mode */
1133 if (status == BTM_SUCCESS && new_mode != p_cb->adv_mode)
1134 {
1135 if (btsnd_hcic_ble_set_adv_enable (new_mode))
1136 {
1137 status = BTM_SUCCESS;
1138
1139 p_cb->adv_mode = new_mode;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001140
1141 if (p_cb->adv_mode == BTM_BLE_ADV_ENABLE &&
1142 p_cb->afp != AP_SCAN_CONN_ALL)
1143 btm_cb.ble_ctr_cb.wl_state |= BTM_BLE_WL_ADV;
1144 else
1145 btm_cb.ble_ctr_cb.wl_state &= ~BTM_BLE_WL_ADV;
1146
The Android Open Source Project5738f832012-12-12 16:00:35 -08001147 }
1148 }
1149
1150 return status;
1151}
1152
1153/*******************************************************************************
1154**
1155** Function btm_ble_start_inquiry
1156**
1157** Description This function is called to start BLE inquiry procedure.
1158** If the duration is zero, the periodic inquiry mode is cancelled.
1159**
1160** Parameters: mode - GENERAL or LIMITED inquiry
1161** p_inq_params - pointer to the BLE inquiry parameter.
1162** p_results_cb - callback returning pointer to results (tBTM_INQ_RESULTS)
1163** p_cmpl_cb - callback indicating the end of an inquiry
1164**
1165**
1166**
1167** Returns BTM_CMD_STARTED if successfully started
The Android Open Source Project5738f832012-12-12 16:00:35 -08001168** BTM_NO_RESOURCES if could not allocate a message buffer
The Android Open Source Project5738f832012-12-12 16:00:35 -08001169** BTM_BUSY - if an inquiry is already active
The Android Open Source Project5738f832012-12-12 16:00:35 -08001170**
1171*******************************************************************************/
1172tBTM_STATUS btm_ble_start_inquiry (UINT8 mode, UINT8 duration)
1173{
1174 tBTM_STATUS status = BTM_NO_RESOURCES;
1175 tBTM_BLE_INQ_CB *p_inq = &btm_cb.ble_ctr_cb.inq_var;
1176
1177 BTM_TRACE_DEBUG2("btm_ble_start_inquiry: mode = %02x inq_active = %d", mode, btm_cb.btm_inq_vars.inq_active);
1178
1179 if (p_inq->proc_mode != BTM_BLE_INQUIRY_NONE)
1180 {
1181 BTM_TRACE_ERROR0("LE scan is active, can not start inquiry");
1182 return(BTM_BUSY);
1183 }
1184
1185 btm_update_scanner_filter_policy(SP_ADV_ALL);
1186
1187 /* start scan, already enable duplicate filtering */
1188 if (btsnd_hcic_ble_set_scan_enable (BTM_BLE_SCAN_ENABLE, BTM_BLE_DUPLICATE_DISABLE))
1189 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001190 status = BTM_CMD_STARTED;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001191 p_inq->proc_mode = mode;
1192
1193 if (duration != 0)
1194 {
1195 /* start inquiry timer */
1196 btu_start_timer (&p_inq->inq_timer_ent, BTU_TTYPE_BLE_INQUIRY, duration);
1197 }
1198 }
1199
1200 return status;
1201}
1202
1203/*******************************************************************************
1204**
1205** Function btm_ble_read_remote_name_cmpl
1206**
1207** Description This function is called when BLE remote name is received.
1208**
1209** Returns void
1210**
1211*******************************************************************************/
1212void btm_ble_read_remote_name_cmpl(BOOLEAN status, BD_ADDR bda, UINT16 length, char *p_name)
1213{
1214 UINT8 hci_status = HCI_SUCCESS;
1215 BD_NAME bd_name;
1216
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001217 memset(bd_name, 0, (BD_NAME_LEN + 1));
Zhihai Xu7f9bcf82014-01-15 15:49:21 -08001218 if (length > BD_NAME_LEN)
1219 {
1220 length = BD_NAME_LEN;
1221 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001222 memcpy((UINT8*)bd_name, p_name, length);
1223
1224 if ((!status) || (length==0))
1225 {
1226 hci_status = HCI_ERR_HOST_TIMEOUT;
1227 }
1228
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001229 btm_process_remote_name(bda, bd_name, length +1, hci_status);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001230 btm_sec_rmt_name_request_complete (bda, (UINT8 *)p_name, hci_status);
1231}
1232
1233/*******************************************************************************
1234**
1235** Function btm_ble_read_remote_name
1236**
1237** Description This function read remote LE device name using GATT read
1238** procedure.
1239**
1240** Parameters: None.
1241**
1242** Returns void
1243**
1244*******************************************************************************/
1245tBTM_STATUS btm_ble_read_remote_name(BD_ADDR remote_bda, tBTM_INQ_INFO *p_cur, tBTM_CMPL_CB *p_cb)
1246{
1247 tBTM_INQUIRY_VAR_ST *p_inq = &btm_cb.btm_inq_vars;
1248
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001249 if (!HCI_LE_HOST_SUPPORTED(btm_cb.devcb.local_lmp_features[HCI_EXT_FEATURES_PAGE_1]))
1250 return BTM_ERR_PROCESSING;
1251
The Android Open Source Project5738f832012-12-12 16:00:35 -08001252 if (p_cur &&
1253 p_cur->results.ble_evt_type != BTM_BLE_EVT_CONN_ADV &&
1254 p_cur->results.ble_evt_type != BTM_BLE_EVT_CONN_DIR_ADV)
1255 {
1256 BTM_TRACE_DEBUG0("name request to non-connectable device failed.");
1257 return BTM_ERR_PROCESSING;
1258 }
1259
1260 /* read remote device name using GATT procedure */
1261 if (p_inq->remname_active)
1262 return BTM_BUSY;
1263
1264 if (!GAP_BleReadPeerDevName(remote_bda, btm_ble_read_remote_name_cmpl))
1265 return BTM_BUSY;
1266
1267 p_inq->p_remname_cmpl_cb = p_cb;
1268 p_inq->remname_active = TRUE;
1269
1270 memcpy(p_inq->remname_bda, remote_bda, BD_ADDR_LEN);
1271
1272 btu_start_timer (&p_inq->rmt_name_timer_ent,
1273 BTU_TTYPE_BTM_RMT_NAME,
1274 BTM_EXT_BLE_RMT_NAME_TIMEOUT);
1275
1276 return BTM_CMD_STARTED;
1277}
1278
1279/*******************************************************************************
1280**
1281** Function btm_ble_cancel_remote_name
1282**
1283** Description This function cancel read remote LE device name.
1284**
1285** Parameters: None.
1286**
1287** Returns void
1288**
1289*******************************************************************************/
1290BOOLEAN btm_ble_cancel_remote_name(BD_ADDR remote_bda)
1291{
1292 tBTM_INQUIRY_VAR_ST *p_inq = &btm_cb.btm_inq_vars;
1293 BOOLEAN status;
1294
1295 status = GAP_BleCancelReadPeerDevName(remote_bda);
1296
1297 p_inq->remname_active = FALSE;
1298 memset(p_inq->remname_bda, 0, BD_ADDR_LEN);
1299 btu_stop_timer(&p_inq->rmt_name_timer_ent);
1300
1301 return status;
1302}
1303
1304/*******************************************************************************
1305**
1306** Function btm_ble_update_adv_flag
1307**
1308** Description This function update the limited discoverable flag in the adv
1309** data.
1310**
1311** Parameters: None.
1312**
1313** Returns void
1314**
1315*******************************************************************************/
1316static void btm_ble_update_adv_flag(UINT8 flag)
1317{
1318 tBTM_BLE_LOCAL_ADV_DATA *p_adv_data = &btm_cb.ble_ctr_cb.inq_var.adv_data;
1319 UINT8 *p;
1320
1321 BTM_TRACE_DEBUG1 ("btm_ble_update_adv_flag new=0x%x", flag);
1322
1323 if (p_adv_data->p_flags != NULL)
1324 {
1325 BTM_TRACE_DEBUG1 ("btm_ble_update_adv_flag old=0x%x", *p_adv_data->p_flags);
1326 *p_adv_data->p_flags = flag;
1327 }
1328 else /* no FLAGS in ADV data*/
1329 {
1330 p = (p_adv_data->p_pad == NULL) ? p_adv_data->ad_data : p_adv_data->p_pad;
1331 /* need 3 bytes space to stuff in the flags, if not */
1332 /* erase all written data, just for flags */
1333 if ((BTM_BLE_AD_DATA_LEN - (p - p_adv_data->ad_data)) < 3)
1334 {
1335 p = p_adv_data->p_pad = p_adv_data->ad_data;
1336 memset(p_adv_data->ad_data, 0, BTM_BLE_AD_DATA_LEN);
1337 }
1338
1339 *p++ = 2;
1340 *p++ = BTM_BLE_AD_TYPE_FLAG;
1341 p_adv_data->p_flags = p;
1342 *p++ = flag;
1343 p_adv_data->p_pad = p;
1344 }
1345
1346 if (btsnd_hcic_ble_set_adv_data((UINT8)(p_adv_data->p_pad - p_adv_data->ad_data),
1347 p_adv_data->ad_data))
1348 p_adv_data->data_mask |= BTM_BLE_AD_BIT_FLAGS;
1349
1350}
1351
1352#if 0
1353/*******************************************************************************
1354**
1355** Function btm_ble_parse_adv_data
1356**
1357** Description This function parse the adv data into a structure.
1358**
1359** Returns pointer to entry, or NULL if not found
1360**
1361*******************************************************************************/
1362static void btm_ble_parse_adv_data(tBTM_INQ_INFO *p_info, UINT8 *p_data,
1363 UINT8 len, tBTM_BLE_INQ_DATA *p_adv_data, UINT8 *p_buf)
1364{
1365 UINT8 *p_cur = p_data;
1366 UINT8 ad_len, ad_type, ad_flag;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001367
1368 BTM_TRACE_EVENT0 (" btm_ble_parse_adv_data");
1369
1370 while (len > 0)
1371 {
1372 BTM_TRACE_DEBUG1("btm_ble_parse_adv_data: len = %d", len);
1373 if ((ad_len = *p_cur ++) == 0)
1374 break;
1375
1376 ad_type = *p_cur ++;
1377
1378 BTM_TRACE_DEBUG2(" ad_type = %02x ad_len = %d", ad_type, ad_len);
1379
1380 switch (ad_type)
1381 {
1382 case BTM_BLE_AD_TYPE_NAME_SHORT:
1383
1384 case BTM_BLE_AD_TYPE_NAME_CMPL:
1385 p_adv_data->ad_mask |= BTM_BLE_AD_BIT_DEV_NAME;
1386 if (p_info)
1387 {
1388 p_info->remote_name_type =(ad_type == BTM_BLE_AD_TYPE_NAME_SHORT) ?
1389 BTM_BLE_NAME_SHORT: BTM_BLE_NAME_CMPL;
1390 memcpy(p_info->remote_name, p_cur, ad_len -1);
1391 p_info->remote_name[ad_len] = 0;
1392 p_adv_data->p_remote_name = p_info->remote_name;
1393 p_info->remote_name_len = p_adv_data->remote_name_len = ad_len - 1;
1394 BTM_TRACE_DEBUG1("BTM_BLE_AD_TYPE_NAME name = %s",p_adv_data->p_remote_name);
1395 }
1396 p_cur += (ad_len -1);
1397
1398 break;
1399
1400 case BTM_BLE_AD_TYPE_FLAG:
1401 p_adv_data->ad_mask |= BTM_BLE_AD_BIT_FLAGS;
1402 ad_flag = *p_cur ++;
1403 p_adv_data->flag = (UINT8)(ad_flag & BTM_BLE_ADV_FLAG_MASK) ;
1404 BTM_TRACE_DEBUG3("BTM_BLE_AD_TYPE_FLAG flag = %s | %s | %s",
1405 (p_adv_data->flag & BTM_BLE_LIMIT_DISC_FLAG)? "LE_LIMIT_DISC" : "",
1406 (p_adv_data->flag & BTM_BLE_GEN_DISC_FLAG)? "LE_GENERAL_DISC" : "",
1407 (p_adv_data->flag & BTM_BLE_BREDR_NOT_SPT)? "LE Only device" : "");
1408 break;
1409
1410 case BTM_BLE_AD_TYPE_TX_PWR:
1411 p_adv_data->ad_mask |= BTM_BLE_AD_BIT_TX_PWR;
1412 p_adv_data->tx_power_level = (INT8)*p_cur ++;
1413 BTM_TRACE_DEBUG1("BTM_BLE_AD_TYPE_TX_PWR tx_level = %d", p_adv_data->tx_power_level);
1414 break;
1415
The Android Open Source Project5738f832012-12-12 16:00:35 -08001416 case BTM_BLE_AD_TYPE_MANU:
1417
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001418 case BTM_BLE_AD_TYPE_16SRV_PART:
1419 case BTM_BLE_AD_TYPE_16SRV_CMPL:
1420 p_adv_data->ad_mask |= BTM_BLE_AD_BIT_SERVICE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001421 /* need allocate memory to store UUID list */
1422 p_adv_data->service.num_service = (ad_len - 1)/2;
1423 BTM_TRACE_DEBUG1("service UUID list, num = %d", p_adv_data->service.num_service);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001424 p_cur += (ad_len - 1);
1425 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001426
1427 case BTM_BLE_AD_TYPE_SOL_SRV_UUID:
1428 p_adv_data->ad_mask |= BTM_BLE_AD_BIT_SERVICE_SOL;
1429 /* need allocate memory to store UUID list */
1430 p_adv_data->service.num_service = (ad_len - 1)/2;
1431 BTM_TRACE_DEBUG1("service UUID list, num = %d", p_adv_data->service.num_service);
1432 p_cur += (ad_len - 1);
1433 break;
1434
1435 case BTM_BLE_AD_TYPE_128SOL_SRV_UUID:
1436 p_adv_data->ad_mask |= BTM_BLE_AD_BIT_SERVICE_128SOL;
1437 /* need allocate memory to store UUID list */
1438 p_adv_data->service.num_service = (ad_len - 1)/16;
1439 BTM_TRACE_DEBUG1("service UUID list, num = %d", p_adv_data->service.num_service);
1440 p_cur += (ad_len - 1);
1441 break;
1442
1443 case BTM_BLE_AD_TYPE_APPEARANCE:
1444 case BTM_BLE_AD_TYPE_PUBLIC_TARGET:
1445 case BTM_BLE_AD_TYPE_RANDOM_TARGET:
1446 default:
1447 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001448 }
1449 len -= (ad_len + 1);
1450 }
1451}
1452#endif
1453
1454/*******************************************************************************
1455**
1456** Function btm_ble_cache_adv_data
1457**
1458** Description Update advertising cache data.
1459**
1460** Returns void
1461**
1462*******************************************************************************/
1463void btm_ble_cache_adv_data(tBTM_INQ_RESULTS *p_cur, UINT8 data_len, UINT8 *p, UINT8 evt_type)
1464{
1465 tBTM_BLE_INQ_CB *p_le_inq_cb = &btm_cb.ble_ctr_cb.inq_var;
1466 UINT8 *p_cache;
1467 UINT8 length;
Mike J. Chen5cd8bff2014-01-31 18:16:59 -08001468 UNUSED(p_cur);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001469
1470 /* cache adv report/scan response data */
1471 if (evt_type != BTM_BLE_SCAN_RSP_EVT)
1472 {
1473 p_le_inq_cb->adv_len = 0;
1474 memset(p_le_inq_cb->adv_data_cache, 0, BTM_BLE_CACHE_ADV_DATA_MAX);
1475 }
1476
1477 if (data_len > 0)
1478 {
1479 p_cache = &p_le_inq_cb->adv_data_cache[p_le_inq_cb->adv_len];
1480 STREAM_TO_UINT8(length, p);
1481 while ( length && ((p_le_inq_cb->adv_len + length + 1) <= BTM_BLE_CACHE_ADV_DATA_MAX))
1482 {
1483 /* copy from the length byte & data into cache */
1484 memcpy(p_cache, p-1, length+1);
1485 /* advance the cache pointer past data */
1486 p_cache += length+1;
1487 /* increment cache length */
1488 p_le_inq_cb->adv_len += length+1;
1489 /* skip the length of data */
1490 p += length;
1491 STREAM_TO_UINT8(length, p);
1492 }
1493 }
1494
1495 /* parse service UUID from adv packet and save it in inq db eir_uuid */
1496 /* TODO */
1497}
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001498
The Android Open Source Project5738f832012-12-12 16:00:35 -08001499/*******************************************************************************
1500**
1501** Function btm_ble_is_discoverable
1502**
1503** Description check ADV flag to make sure device is discoverable and match
1504** the search condition
1505**
1506** Parameters
1507**
1508** Returns void
1509**
1510*******************************************************************************/
1511BOOLEAN btm_ble_is_discoverable(BD_ADDR bda, UINT8 evt_type, UINT8 *p)
1512{
1513 BOOLEAN is_discoverable = FALSE;
1514 UINT8 *p_flag, flag = 0;
1515 UINT8 data_len;
1516 tBTM_INQ_PARMS *p_cond = &btm_cb.btm_inq_vars.inqparms;
1517
1518 STREAM_TO_UINT8 (data_len, p);
1519
1520 /* for observer, always "discoverable */
1521 if (btm_cb.ble_ctr_cb.inq_var.proc_mode == BTM_BLE_OBSERVE ||
1522 (btm_cb.ble_ctr_cb.inq_var.proc_mode == BTM_BLE_SELECT_SCAN &&
1523 btm_cb.ble_ctr_cb.bg_conn_type == BTM_BLE_CONN_SELECTIVE))
1524 return TRUE;
1525
1526 /* does not match filter condition */
1527 if (p_cond->filter_cond_type == BTM_FILTER_COND_BD_ADDR &&
1528 memcmp(bda, p_cond->filter_cond.bdaddr_cond, BD_ADDR_LEN) != 0)
1529 {
1530 BTM_TRACE_DEBUG0("BD ADDR does not meet filter condition");
1531 return FALSE;
1532 }
1533
1534 /* scan response does not include the flag */
1535 if (evt_type == BTM_BLE_SCAN_RSP_EVT)
1536 return FALSE;
1537
1538 if (data_len > BTM_BLE_ADV_DATA_LEN_MAX)
1539 {
1540 BTM_TRACE_WARNING1("ADV data too long %d. discard", data_len);
1541 return FALSE;
1542 }
1543
1544 if (data_len != 0)
1545 {
1546 if ((p_flag = BTM_CheckAdvData(p, BTM_BLE_AD_TYPE_FLAG, &data_len)) != NULL)
1547 {
1548 flag = * p_flag;
1549
1550 if ((btm_cb.ble_ctr_cb.inq_var.proc_mode == BTM_BLE_GENERAL_INQUIRY) &&
1551 (flag & (BTM_BLE_LIMIT_DISC_FLAG|BTM_BLE_GEN_DISC_FLAG)) != 0)
1552 {
1553 BTM_TRACE_DEBUG0("Find Generable Discoverable device");
1554 is_discoverable = TRUE;
1555 }
1556
1557 else if (btm_cb.ble_ctr_cb.inq_var.proc_mode == BTM_BLE_LIMITED_INQUIRY &&
1558 (flag & BTM_BLE_LIMIT_DISC_FLAG) != 0)
1559 {
1560 BTM_TRACE_DEBUG0("Find limited discoverable device");
1561 is_discoverable = TRUE;
1562 }
1563
1564 }
1565 }
1566
1567 if (!is_discoverable)
1568 {
1569 BTM_TRACE_ERROR1("discoverable flag not desired: %d", flag);
1570 }
1571
1572 return is_discoverable;
1573}
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001574
The Android Open Source Project5738f832012-12-12 16:00:35 -08001575/*******************************************************************************
1576**
1577** Function btm_ble_update_inq_result
1578**
1579** Description Update adv packet information into inquiry result.
1580**
1581** Parameters
1582**
1583** Returns void
1584**
1585*******************************************************************************/
1586BOOLEAN btm_ble_update_inq_result(tINQ_DB_ENT *p_i, UINT8 addr_type, UINT8 evt_type, UINT8 *p)
1587{
1588 BOOLEAN to_report = TRUE;
1589 tBTM_INQ_RESULTS *p_cur = &p_i->inq_info.results;
1590 UINT8 len;
1591 UINT8 *p_flag;
1592 tBTM_INQUIRY_VAR_ST *p_inq = &btm_cb.btm_inq_vars;
1593 UINT8 data_len, rssi;
1594 tBTM_BLE_INQ_CB *p_le_inq_cb = &btm_cb.ble_ctr_cb.inq_var;
1595 UINT8 *p1;
Zhihai Xu1237ee32013-11-26 18:18:29 -08001596 UINT8 *p_uuid16;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001597
1598 STREAM_TO_UINT8 (data_len, p);
1599
1600 if (data_len > BTM_BLE_ADV_DATA_LEN_MAX)
1601 {
1602 BTM_TRACE_WARNING1("EIR data too long %d. discard", data_len);
1603 return FALSE;
1604 }
1605 btm_ble_cache_adv_data(p_cur, data_len, p, evt_type);
1606
1607 p1 = (p + data_len);
1608 STREAM_TO_UINT8 (rssi, p1);
1609
1610 /* Save the info */
1611 p_cur->inq_result_type = BTM_INQ_RESULT_BLE;
1612 p_cur->ble_addr_type = addr_type;
1613 p_cur->rssi = rssi;
1614
1615 /* active scan, always wait until get scan_rsp to report the result */
1616 if ((btm_cb.ble_ctr_cb.inq_var.scan_type == BTM_BLE_SCAN_MODE_ACTI &&
1617 (evt_type == BTM_BLE_CONNECT_EVT || evt_type == BTM_BLE_DISCOVER_EVT)))
1618 {
Nitin Arora0830ad42014-02-21 18:20:37 -08001619 BTM_TRACE_DEBUG1("btm_ble_update_inq_result scan_rsp=false, to_report=false,\
1620 scan_type_active=%d", btm_cb.ble_ctr_cb.inq_var.scan_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001621 p_i->scan_rsp = FALSE;
1622 to_report = FALSE;
1623 }
1624 else
1625 p_i->scan_rsp = TRUE;
1626
1627 if (p_i->inq_count != p_inq->inq_counter)
1628 p_cur->device_type = BT_DEVICE_TYPE_BLE;
1629 else
1630 p_cur->device_type |= BT_DEVICE_TYPE_BLE;
1631
1632 if (evt_type != BTM_BLE_SCAN_RSP_EVT)
1633 p_cur->ble_evt_type = evt_type;
1634
1635 p_i->inq_count = p_inq->inq_counter; /* Mark entry for current inquiry */
1636
1637 if (p_le_inq_cb->adv_len != 0)
1638 {
1639 if ((p_flag = BTM_CheckAdvData(p_le_inq_cb->adv_data_cache, BTM_BLE_AD_TYPE_FLAG, &len)) != NULL)
1640 p_cur->flag = * p_flag;
1641 }
1642
Zhihai Xu1237ee32013-11-26 18:18:29 -08001643 if (p_le_inq_cb->adv_len != 0)
1644 {
1645 if ((p_uuid16 = BTM_CheckAdvData(p_le_inq_cb->adv_data_cache,
1646 BTM_BLE_AD_TYPE_16SRV_CMPL, &len)) != NULL)
1647 {
1648 UINT8 i;
1649 for (i = 0; i + 2 <= len; i = i + 2)
1650 {
1651 /* if this BLE device support HID over LE, set HID Major in class of device */
1652 if ((p_uuid16[i] | (p_uuid16[i+1] << 8)) == UUID_SERVCLASS_LE_HID)
1653 {
1654 p_cur->dev_class[0] = 0;
1655 p_cur->dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
1656 p_cur->dev_class[2] = 0;
1657 break;
1658 }
1659 }
1660 }
1661 }
1662
The Android Open Source Project5738f832012-12-12 16:00:35 -08001663 /* if BR/EDR not supported is not set, assume is a DUMO device */
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001664 if ((p_cur->flag & BTM_BLE_BREDR_NOT_SPT) == 0 &&
1665 evt_type != BTM_BLE_CONNECT_DIR_EVT)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001666 {
Andre Eisenbachbf68ac72013-10-25 16:17:45 -07001667 if (p_cur->ble_addr_type != BLE_ADDR_RANDOM)
1668 {
1669 BTM_TRACE_DEBUG0("BR/EDR NOT support bit not set, treat as DUMO");
1670 p_cur->device_type |= BT_DEVICE_TYPE_DUMO;
1671 } else {
1672 BTM_TRACE_DEBUG0("Random address, treating device as LE only");
1673 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001674 }
1675 else
1676 {
1677 BTM_TRACE_DEBUG0("BR/EDR NOT SUPPORT bit set, LE only device");
1678 }
1679
1680 return to_report;
1681
1682}
1683
1684/*******************************************************************************
1685**
1686** Function btm_send_sel_conn_callback
1687**
1688** Description send selection connection request callback.
1689**
1690** Parameters
1691**
1692** Returns void
1693**
1694*******************************************************************************/
1695void btm_send_sel_conn_callback(BD_ADDR remote_bda, UINT8 evt_type, UINT8 *p_data, UINT8 addr_type)
1696{
1697 UINT8 data_len, len;
1698 UINT8 *p_dev_name, remname[31] = {0};
Mike J. Chen5cd8bff2014-01-31 18:16:59 -08001699 UNUSED(addr_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001700
1701 if (btm_cb.ble_ctr_cb.p_select_cback == NULL ||
1702 /* non-connectable device */
1703 (evt_type != BTM_BLE_EVT_CONN_ADV && evt_type != BTM_BLE_EVT_CONN_DIR_ADV))
1704 return;
1705
1706 STREAM_TO_UINT8 (data_len, p_data);
1707
1708 /* get the device name if exist in ADV data */
1709 if (data_len != 0)
1710 {
1711 p_dev_name = BTM_CheckAdvData(p_data, BTM_BLE_AD_TYPE_NAME_CMPL, &len);
1712
1713 if (p_dev_name == NULL)
1714 p_dev_name = BTM_CheckAdvData(p_data, BTM_BLE_AD_TYPE_NAME_SHORT, &len);
1715
1716 if (p_dev_name)
1717 memcpy(remname, p_dev_name, len);
1718 }
1719 /* allow connection */
1720 if ((* btm_cb.ble_ctr_cb.p_select_cback)(remote_bda, remname))
1721 {
1722 /* terminate selective connection, initiate connection */
1723 btm_ble_initiate_select_conn(remote_bda);
1724 }
1725}
1726
1727/*******************************************************************************
1728**
The Android Open Source Project5738f832012-12-12 16:00:35 -08001729** Function btm_ble_process_adv_pkt
1730**
1731** Description This function is called when adv packet report events are
1732** received from the device. It updates the inquiry database.
1733** If the inquiry database is full, the oldest entry is discarded.
1734**
1735** Parameters
1736**
1737** Returns void
1738**
1739*******************************************************************************/
1740void btm_ble_process_adv_pkt (UINT8 *p_data)
1741{
1742 BD_ADDR bda;
1743 UINT8 evt_type = 0, *p = p_data;
1744 UINT8 addr_type = 0;
Zhihai Xu8b35b3f2014-03-11 15:01:45 -07001745#if (defined BTM_BLE_PRIVACY_SPT && BTM_BLE_PRIVACY_SPT == TRUE)
1746 BOOLEAN match = FALSE;
1747#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001748
The Android Open Source Project5738f832012-12-12 16:00:35 -08001749 /* always get one device at a time */
1750 p ++;
1751
1752 /* Extract inquiry results */
1753 STREAM_TO_UINT8 (evt_type, p);
1754 STREAM_TO_UINT8 (addr_type, p);
1755 STREAM_TO_BDADDR (bda, p);
1756
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001757#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 */
1758 if (BTM_BLE_PC_ADV_TEST_MODE) /* For stack component, it is always defined and maps to a global variable g_bDraculaAdvertisingMode */
The Android Open Source Project5738f832012-12-12 16:00:35 -08001759 {
1760 if (btm_cb.ble_ctr_cb.p_scan_req_cback)
1761 (*btm_cb.ble_ctr_cb.p_scan_req_cback)(bda, addr_type, evt_type);
1762 }
1763#endif
1764
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001765
1766
The Android Open Source Project5738f832012-12-12 16:00:35 -08001767 /* Only process the results if the inquiry is still active */
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001768 if ((btm_cb.btm_inq_vars.inq_active & BTM_LE_SCAN_ACTIVE_MASK) == 0 &&
The Android Open Source Project5738f832012-12-12 16:00:35 -08001769 (btm_cb.ble_ctr_cb.bg_conn_type != BTM_BLE_CONN_SELECTIVE ||
1770 /* or selective auto connection is active */
1771 btm_cb.ble_ctr_cb.p_select_cback == NULL))
1772 return;
1773
Nitin Arora0830ad42014-02-21 18:20:37 -08001774 BTM_TRACE_DEBUG6("btm_ble_process_adv_pkt:bda= %0x:%0x:%0x:%0x:%0x:%0x",
1775 bda[0],bda[1],bda[2],bda[3],bda[4],bda[5]);
Zhihai Xu8b35b3f2014-03-11 15:01:45 -07001776#if (defined BTM_BLE_PRIVACY_SPT && BTM_BLE_PRIVACY_SPT == TRUE)
1777#if SMP_INCLUDED == TRUE
1778 /* always do RRA resolution on host */
1779 if (!match && BTM_BLE_IS_RESOLVE_BDA(bda))
1780 {
1781 btm_ble_resolve_random_addr(bda, btm_ble_resolve_random_addr_on_adv, p_data);
1782 }
1783 else
1784#endif
1785#endif
1786 {
1787 btm_ble_process_adv_pkt_cont(bda, addr_type, evt_type, p);
1788 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001789}
1790
1791/*******************************************************************************
1792**
1793** Function btm_ble_process_adv_pkt_cont
1794**
1795** Description This function is called after random address resolution is
1796** done, and proceed to process adv packet.
1797**
1798** Parameters
1799**
1800** Returns void
1801**
1802*******************************************************************************/
1803static void btm_ble_process_adv_pkt_cont(BD_ADDR bda, UINT8 addr_type, UINT8 evt_type, UINT8 *p)
1804{
1805 tINQ_DB_ENT *p_i;
1806 BOOLEAN to_report = FALSE;
Nitin Arora0830ad42014-02-21 18:20:37 -08001807 BOOLEAN to_report_LE = TRUE; //var for reporting to LE observe
The Android Open Source Project5738f832012-12-12 16:00:35 -08001808 tBTM_INQUIRY_VAR_ST *p_inq = &btm_cb.btm_inq_vars;
1809 tBTM_INQ_RESULTS_CB *p_inq_results_cb = p_inq->p_inq_results_cb;
Nitin Arora0830ad42014-02-21 18:20:37 -08001810 tBTM_INQ_RESULTS_CB *p_inq_ble_results_cb = p_inq->p_inq_ble_results_cb;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001811 tBTM_BLE_INQ_CB *p_le_inq_cb = &btm_cb.ble_ctr_cb.inq_var;
Nitin Arora0830ad42014-02-21 18:20:37 -08001812 BTM_TRACE_DEBUG2("btm_ble_process_adv_pkt_cont: addr_type: %d, evt_type: %d", addr_type, evt_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001813
1814 p_i = btm_inq_db_find (bda);
1815
1816 /* Check if this address has already been processed for this inquiry */
1817 if (btm_inq_find_bdaddr(bda))
1818 {
1819 /* never been report as an LE device */
1820 if ((p_i &&
1821 (!(p_i->inq_info.results.device_type & BT_DEVICE_TYPE_BLE) ||
1822 /* scan repsonse to be updated */
1823 (!p_i->scan_rsp)))
1824 ||
1825 btm_cb.ble_ctr_cb.inq_var.proc_mode == BTM_BLE_OBSERVE)
1826 {
1827 BTM_TRACE_DEBUG0("update new BLE information ");
1828 to_report = TRUE;
1829 }
1830 else
1831 {
Nitin Arora0830ad42014-02-21 18:20:37 -08001832 to_report = FALSE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001833 }
1834 }
Nitin Arora0830ad42014-02-21 18:20:37 -08001835 else /* not been processed in this round */
The Android Open Source Project5738f832012-12-12 16:00:35 -08001836 {
The Android Open Source Project5738f832012-12-12 16:00:35 -08001837 to_report = TRUE;
1838 }
1839
1840 /* If existing entry, use that, else get a new one (possibly reusing the oldest) */
1841 if (p_i == NULL)
1842 {
Nitin Arora0830ad42014-02-21 18:20:37 -08001843 if ((p_i = btm_inq_db_new (bda)) != NULL)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001844 {
Nitin Arora0830ad42014-02-21 18:20:37 -08001845 p_inq->inq_cmpl_info.num_resp++;
1846 }
1847 else
1848 return;
1849
1850 if (to_report && btm_ble_is_discoverable(bda, evt_type, p))
1851 {
1852 to_report = TRUE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001853 }
1854 else
1855 {
1856 BTM_TRACE_ERROR0("discard adv pkt");
Nitin Arora0830ad42014-02-21 18:20:37 -08001857 to_report = FALSE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001858 }
1859 }
1860 else if (p_i->inq_count != p_inq->inq_counter) /* first time seen in this inquiry */
1861 {
1862 p_inq->inq_cmpl_info.num_resp++;
1863 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001864 /* update the LE device information in inquiry database */
Nitin Arora0830ad42014-02-21 18:20:37 -08001865 to_report_LE = btm_ble_update_inq_result(p_i, addr_type, evt_type, p);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001866 if (to_report)
Nitin Arora0830ad42014-02-21 18:20:37 -08001867 to_report = to_report_LE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001868#if BTM_USE_INQ_RESULTS_FILTER == TRUE
1869 /* If the number of responses found and limited, issue a cancel inquiry */
1870 if (p_inq->inqparms.max_resps &&
1871 p_inq->inq_cmpl_info.num_resp == p_inq->inqparms.max_resps)
1872 {
1873 /* new device */
1874 if (p_i == NULL ||
1875 (/* assume a DUMO device, BR/EDR inquiry is always active */
1876 p_i && p_i->inq_info.results.device_type == BT_DEVICE_TYPE_BLE && p_i->scan_rsp))
1877 {
1878 BTM_TRACE_WARNING0("INQ RES: Extra Response Received...cancelling inquiry..");
1879
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001880 /* if is non-periodic inquiry active, cancel now */
1881 if ((p_inq->inq_active & BTM_BR_INQ_ACTIVE_MASK) != 0 &&
1882 (p_inq->inq_active & BTM_PERIODIC_INQUIRY_ACTIVE) == 0)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001883 btsnd_hcic_inq_cancel();
1884
1885 /* stop LE scan now */
1886 btm_ble_stop_scan();
1887
1888#if BTM_BUSY_LEVEL_CHANGE_INCLUDED == TRUE
1889 btm_acl_update_busy_level (BTM_BLI_INQ_DONE_EVT);
1890#endif
1891 }
1892 }
1893#endif
1894
Nitin Arora0830ad42014-02-21 18:20:37 -08001895 BTM_TRACE_DEBUG2("btm_ble_process_adv_pkt_cont: to_report =%d, to_report_le=%d",
1896 to_report, to_report_LE);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001897 /* background connection in selective connection mode */
1898 if (btm_cb.ble_ctr_cb.bg_conn_type == BTM_BLE_CONN_SELECTIVE)
1899 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001900 if (p_i->inq_info.results.device_type == BT_DEVICE_TYPE_BLE &&
1901 (evt_type == BTM_BLE_CONNECT_EVT || evt_type == BTM_BLE_CONNECT_DIR_EVT))
1902 btm_send_sel_conn_callback(bda, evt_type, p, addr_type);
1903 else
1904 {
1905 BTM_TRACE_DEBUG0("None LE device, can not initiate selective connection");
1906 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001907 }
Nitin Arora0830ad42014-02-21 18:20:37 -08001908 else if (to_report || to_report_LE)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001909 {
Nitin Arora0830ad42014-02-21 18:20:37 -08001910 if(p_inq_results_cb && to_report)
1911 (p_inq_results_cb)((tBTM_INQ_RESULTS *) &p_i->inq_info.results, p_le_inq_cb->adv_data_cache);
1912 if(p_inq_ble_results_cb && to_report_LE)
1913 (p_inq_ble_results_cb)((tBTM_INQ_RESULTS *) &p_i->inq_info.results,
1914 p_le_inq_cb->adv_data_cache);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001915 }
1916}
1917
1918/*******************************************************************************
1919**
1920** Function btm_ble_stop_scan
1921**
1922** Description Stop the BLE scan.
1923**
1924** Returns void
1925**
1926*******************************************************************************/
1927void btm_ble_stop_scan(void)
1928{
1929 tBTM_BLE_INQ_CB *p_cb = &btm_cb.ble_ctr_cb.inq_var;
1930 tBTM_INQUIRY_VAR_ST *p_inq = &btm_cb.btm_inq_vars;
1931
1932 BTM_TRACE_EVENT0 ("btm_ble_stop_scan ");
1933
1934 btu_stop_timer (&p_cb->inq_timer_ent);
1935
1936 /* Clear the inquiry callback if set */
1937 p_cb->scan_type = BTM_BLE_SCAN_MODE_NONE;
1938 p_cb->proc_mode = BTM_BLE_INQUIRY_NONE;
1939
1940 /* stop discovery now */
1941 btsnd_hcic_ble_set_scan_enable (BTM_BLE_SCAN_DISABLE, BTM_BLE_DUPLICATE_ENABLE);
1942
1943 /* If we have a callback registered for inquiry complete, call it */
1944 BTM_TRACE_DEBUG2 ("BTM Inq Compl Callback: status 0x%02x, num results %d",
1945 p_inq->inq_cmpl_info.status, p_inq->inq_cmpl_info.num_resp);
1946
1947 btm_update_scanner_filter_policy(SP_ADV_ALL);
1948
1949 btm_process_inq_complete(HCI_SUCCESS, (UINT8)(p_inq->inqparms.mode & BTM_BLE_INQUIRY_MASK));
1950
1951}
1952
1953/*******************************************************************************
1954**
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001955** Function btm_ble_start_adv
1956**
1957** Description Stop the BLE advertising.
1958**
1959** Returns void
1960**
1961*******************************************************************************/
1962static tBTM_STATUS btm_ble_start_adv(void)
1963{
1964 tBTM_BLE_INQ_CB *p_cb = &btm_cb.ble_ctr_cb.inq_var;
1965 tBTM_STATUS rt = BTM_NO_RESOURCES;
1966
1967 if (btsnd_hcic_ble_set_adv_enable (BTM_BLE_ADV_ENABLE))
1968 {
1969 if (p_cb->afp != AP_SCAN_CONN_ALL)
1970 btm_cb.ble_ctr_cb.wl_state |= BTM_BLE_WL_ADV;
1971
1972 p_cb->adv_mode = BTM_BLE_ADV_ENABLE;
1973
1974 rt = BTM_SUCCESS;
1975 }
1976 else
1977 {
1978 p_cb->adv_mode = BTM_BLE_ADV_DISABLE;
1979 btm_cb.ble_ctr_cb.wl_state &= ~BTM_BLE_WL_ADV;
1980 }
1981 return rt;
1982}
1983/*******************************************************************************
1984**
The Android Open Source Project5738f832012-12-12 16:00:35 -08001985** Function btm_ble_stop_adv
1986**
1987** Description Stop the BLE advertising.
1988**
1989** Returns void
1990**
1991*******************************************************************************/
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001992static tBTM_STATUS btm_ble_stop_adv(void)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001993{
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001994 tBTM_BLE_INQ_CB *p_cb = &btm_cb.ble_ctr_cb.inq_var;
1995 tBTM_STATUS rt = BTM_SUCCESS;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001996
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001997 if (p_cb->adv_mode == BTM_BLE_ADV_ENABLE)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001998 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001999 if (btsnd_hcic_ble_set_adv_enable (BTM_BLE_ADV_DISABLE))
2000 {
2001 p_cb->adv_mode = BTM_BLE_ADV_DISABLE;
2002 btm_cb.ble_ctr_cb.wl_state &= ~BTM_BLE_WL_ADV;
2003 }
2004 else
2005 rt = BTM_NO_RESOURCES;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002006 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002007 return rt;
2008
The Android Open Source Project5738f832012-12-12 16:00:35 -08002009}
2010
2011/*******************************************************************************
2012**
2013** Function btm_ble_timeout
2014**
2015** Description Called when BTM BLE inquiry timer expires
2016**
2017** Returns void
2018**
2019*******************************************************************************/
2020void btm_ble_timeout(TIMER_LIST_ENT *p_tle)
2021{
The Android Open Source Project5738f832012-12-12 16:00:35 -08002022 switch (p_tle->event)
2023 {
2024 case BTU_TTYPE_BLE_INQUIRY:
2025 btm_ble_stop_scan();
2026 break;
2027
2028 case BTU_TTYPE_BLE_GAP_LIM_DISC:
2029 /* lim_timeout expiried, limited discovery should exit now */
2030 btm_ble_update_adv_flag(BTM_BLE_NON_LIMIT_DISC_FLAG);
2031
2032 btm_ble_stop_adv();
2033 break;
2034
2035 case BTU_TTYPE_BLE_RANDOM_ADDR:
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002036 if (btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type == BLE_ADDR_RANDOM)
The Android Open Source Project5738f832012-12-12 16:00:35 -08002037 {
2038 /* refresh the random addr */
2039 btm_gen_resolvable_private_addr();
2040 }
2041 break;
2042
2043 }
2044}
2045
The Android Open Source Project5738f832012-12-12 16:00:35 -08002046
2047/*******************************************************************************
2048**
2049** Function btm_ble_read_remote_features_complete
2050**
2051** Description This function is called when the command complete message
2052** is received from the HCI for the read LE remote feature supported
2053** complete event.
2054**
2055** Returns void
2056**
2057*******************************************************************************/
2058void btm_ble_read_remote_features_complete(UINT8 *p)
2059{
2060 tACL_CONN *p_acl_cb = &btm_cb.acl_db[0];
Andre Eisenbach3aa60542013-03-22 18:00:51 -07002061 UINT8 status;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002062 UINT16 handle;
Andre Eisenbach3aa60542013-03-22 18:00:51 -07002063 int xx;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002064
2065 BTM_TRACE_EVENT0 ("btm_ble_read_remote_features_complete ");
2066
Andre Eisenbach3aa60542013-03-22 18:00:51 -07002067 STREAM_TO_UINT8 (status, p);
2068 STREAM_TO_UINT16 (handle, p);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002069
2070 /* Look up the connection by handle and copy features */
2071 for (xx = 0; xx < MAX_L2CAP_LINKS; xx++, p_acl_cb++)
2072 {
2073 if ((p_acl_cb->in_use) && (p_acl_cb->hci_handle == handle))
2074 {
Andre Eisenbach3aa60542013-03-22 18:00:51 -07002075 STREAM_TO_ARRAY(p_acl_cb->peer_le_features, p, BD_FEATURES_LEN);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002076 break;
2077 }
2078 }
2079}
2080
2081/*******************************************************************************
2082**
2083** Function btm_ble_write_adv_enable_complete
2084**
2085** Description This function process the write adv enable command complete.
2086**
2087** Returns void
2088**
2089*******************************************************************************/
2090void btm_ble_write_adv_enable_complete(UINT8 * p)
2091{
2092 tBTM_BLE_INQ_CB *p_cb = &btm_cb.ble_ctr_cb.inq_var;
2093
2094 /* if write adv enable/disbale not succeed */
2095 if (*p != HCI_SUCCESS)
2096 {
2097 /* toggle back the adv mode */
2098 p_cb->adv_mode = !p_cb->adv_mode;
2099 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002100
2101
The Android Open Source Project5738f832012-12-12 16:00:35 -08002102}
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002103
2104/*******************************************************************************
2105**
2106** Function btm_ble_dir_adv_tout
2107**
2108** Description when directed adv time out
2109**
2110** Returns void
2111**
2112*******************************************************************************/
2113void btm_ble_dir_adv_tout(void)
2114{
2115 btm_cb.ble_ctr_cb.inq_var.adv_mode = BTM_BLE_ADV_DISABLE;
2116
2117 /* make device fall back into undirected adv mode by default */
2118 btm_cb.ble_ctr_cb.inq_var.directed_conn = FALSE;
2119}
2120
2121/*******************************************************************************
2122**
2123** Function btm_ble_update_mode_operation
2124**
2125** Description This function update the GAP role operation when a link status
2126** is updated.
2127**
2128** Returns void
2129**
2130*******************************************************************************/
2131void btm_ble_update_mode_operation(UINT8 link_role, BD_ADDR bd_addr, BOOLEAN conn_cancel)
2132{
2133 tACL_CONN *pa = &btm_cb.acl_db[0];
2134 UINT16 xx;
2135 UINT8 dev_role = link_role;
Mike J. Chen5cd8bff2014-01-31 18:16:59 -08002136 UNUSED(bd_addr);
2137 UNUSED(conn_cancel);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002138
2139 BTM_TRACE_DEBUG1("btm_ble_update_mode_operation adv_mode = %d", btm_cb.ble_ctr_cb.inq_var.adv_mode );
2140
2141 /* update periphera role operation */
2142 /* If we are LE connectable, check if we need to start advertising again */
2143 if (link_role == HCI_ROLE_UNKNOWN)
2144 /* && btm_cb.ble_ctr_cb.inq_var.connectable_mode != BTM_BLE_NON_CONNECTABLE) */
2145 {
2146 for (xx = 0; xx < MAX_L2CAP_LINKS; xx++, pa++)
2147 {
2148 /* If any other LE link is up, we are still not connectable */
2149 if (pa->in_use && pa->is_le_link)
2150 {
2151 dev_role = pa->link_role;
2152 break;
2153 }
2154 }
2155 }
2156
2157 if (btm_cb.ble_ctr_cb.inq_var.connectable_mode == BTM_BLE_CONNECTABLE &&
2158 (dev_role == HCI_ROLE_UNKNOWN )) /* when device has no connection, update adv here */
2159 /* if already in connection, no connectable adv is allowed unless scatternet is enabled */
2160 {
2161 btm_ble_set_connectability ( btm_cb.ble_ctr_cb.inq_var.connectable_mode );
2162 }
2163
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002164 if (btm_ble_get_conn_st() == BLE_CONN_IDLE)
2165 {
2166 if (!btm_send_pending_direct_conn())
2167 {
2168 btm_ble_resume_bg_conn();
2169 }
2170 }
2171}
2172
The Android Open Source Project5738f832012-12-12 16:00:35 -08002173/*******************************************************************************
2174**
2175** Function btm_ble_init
2176**
2177** Description Initialize the control block variable values.
2178**
2179** Returns void
2180**
2181*******************************************************************************/
2182void btm_ble_init (void)
2183{
2184 tBTM_BLE_CB *p_cb = &btm_cb.ble_ctr_cb;
2185
2186 BTM_TRACE_EVENT0 ("btm_ble_init ");
2187
2188 memset(p_cb, 0, sizeof(tBTM_BLE_CB));
2189
2190 p_cb->inq_var.adv_mode = BTM_BLE_ADV_DISABLE;
2191 p_cb->inq_var.scan_type = BTM_BLE_SCAN_MODE_NONE;
2192 p_cb->inq_var.adv_chnl_map = BTM_BLE_DEFAULT_ADV_CHNL_MAP;
2193 p_cb->inq_var.afp = BTM_BLE_DEFAULT_AFP;
2194 p_cb->inq_var.sfp = BTM_BLE_DEFAULT_SFP;
2195 p_cb->inq_var.connectable_mode = BTM_BLE_NON_CONNECTABLE;
2196 p_cb->inq_var.discoverable_mode = BTM_BLE_NON_DISCOVERABLE;
2197
2198 /* for background connection, reset connection params to be undefined */
2199 p_cb->scan_int = p_cb->scan_win = BTM_BLE_CONN_PARAM_UNDEF;
2200
Andre Eisenbach6975b4d2013-08-05 16:55:38 -07002201 p_cb->inq_var.evt_type = BTM_BLE_NON_CONNECT_EVT;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002202}
2203
2204#endif /* BLE_INCLUDED */