1 /******************************************************************************
2 *
3 * Copyright (C) 2009-2012 Broadcom Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19 /************************************************************************************
20 *
21 * Filename: btif_dm.c
22 *
23 * Description: Contains Device Management (DM) related functionality
24 *
25 *
26 ***********************************************************************************/
27
28 #define LOG_TAG "bt_btif_dm"
29
30 #include "btif_dm.h"
31
32 #include <assert.h>
33 #include <pthread.h>
34 #include <signal.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <sys/types.h>
39 #include <time.h>
40 #include <unistd.h>
41
42 #include <hardware/bluetooth.h>
43
44 #include "bdaddr.h"
45 #include "bta_gatt_api.h"
46 #include "btif_api.h"
47 #include "btif_config.h"
48 #include "btif_hh.h"
49 #include "btif_sdp.h"
50 #include "btif_storage.h"
51 #include "btif_util.h"
52 #include "btu.h"
53 #include "bt_common.h"
54 #include "bta_gatt_api.h"
55 #include "device/include/interop.h"
56 #include "include/stack_config.h"
57 #include "osi/include/allocator.h"
58 #include "osi/include/log.h"
59 #include "osi/include/metrics.h"
60 #include "osi/include/properties.h"
61 #include "stack_config.h"
62 #include "stack/btm/btm_int.h"
63
64 /******************************************************************************
65 ** Constants & Macros
66 ******************************************************************************/
67
68 #define COD_MASK 0x07FF
69
70 #define COD_UNCLASSIFIED ((0x1F) << 8)
71 #define COD_HID_KEYBOARD 0x0540
72 #define COD_HID_POINTING 0x0580
73 #define COD_HID_COMBO 0x05C0
74 #define COD_HID_MAJOR 0x0500
75 #define COD_HID_MASK 0x0700
76 #define COD_AV_HEADSETS 0x0404
77 #define COD_AV_HANDSFREE 0x0408
78 #define COD_AV_HEADPHONES 0x0418
79 #define COD_AV_PORTABLE_AUDIO 0x041C
80 #define COD_AV_HIFI_AUDIO 0x0428
81
82 #define BTIF_DM_DEFAULT_INQ_MAX_RESULTS 0
83 #define BTIF_DM_DEFAULT_INQ_MAX_DURATION 10
84 #define BTIF_DM_MAX_SDP_ATTEMPTS_AFTER_PAIRING 2
85
86 #define NUM_TIMEOUT_RETRIES 5
87
88 #define PROPERTY_PRODUCT_MODEL "ro.product.model"
89 #define DEFAULT_LOCAL_NAME_MAX 31
90 #if (DEFAULT_LOCAL_NAME_MAX > BTM_MAX_LOC_BD_NAME_LEN)
91 #error "default btif local name size exceeds stack supported length"
92 #endif
93
94 #if (defined(BTA_HOST_INTERLEAVE_SEARCH) && BTA_HOST_INTERLEAVE_SEARCH == TRUE)
95 #define BTIF_DM_INTERLEAVE_DURATION_BR_ONE 2
96 #define BTIF_DM_INTERLEAVE_DURATION_LE_ONE 2
97 #define BTIF_DM_INTERLEAVE_DURATION_BR_TWO 3
98 #define BTIF_DM_INTERLEAVE_DURATION_LE_TWO 4
99 #endif
100
101 #define ENCRYPTED_BREDR 2
102 #define ENCRYPTED_LE 4
103
104 typedef struct
105 {
106 bt_bond_state_t state;
107 bt_bdaddr_t static_bdaddr;
108 BD_ADDR bd_addr;
109 tBTM_BOND_TYPE bond_type;
110 UINT8 pin_code_len;
111 UINT8 is_ssp;
112 UINT8 auth_req;
113 UINT8 io_cap;
114 UINT8 autopair_attempts;
115 UINT8 timeout_retries;
116 UINT8 is_local_initiated;
117 UINT8 sdp_attempts;
118 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
119 BOOLEAN is_le_only;
120 BOOLEAN is_le_nc; /* LE Numeric comparison */
121 btif_dm_ble_cb_t ble;
122 #endif
123 } btif_dm_pairing_cb_t;
124
125 typedef struct
126 {
127 UINT8 ir[BT_OCTET16_LEN];
128 UINT8 irk[BT_OCTET16_LEN];
129 UINT8 dhk[BT_OCTET16_LEN];
130 }btif_dm_local_key_id_t;
131
132 typedef struct
133 {
134 BOOLEAN is_er_rcvd;
135 UINT8 er[BT_OCTET16_LEN];
136 BOOLEAN is_id_keys_rcvd;
137 btif_dm_local_key_id_t id_keys; /* ID kyes */
138
139 }btif_dm_local_key_cb_t;
140
141 typedef struct
142 {
143 BD_ADDR bd_addr;
144 BD_NAME bd_name;
145 } btif_dm_remote_name_t;
146
147 /* this structure holds optional OOB data for remote device */
148 typedef struct
149 {
150 BD_ADDR bdaddr; /* peer bdaddr */
151 bt_out_of_band_data_t oob_data;
152 } btif_dm_oob_cb_t;
153
154 typedef struct
155 {
156 bt_bdaddr_t bdaddr;
157 UINT8 transport; /* 0=Unknown, 1=BR/EDR, 2=LE */
158 } btif_dm_create_bond_cb_t;
159
160 typedef struct
161 {
162 uint8_t status;
163 uint8_t ctrl_state;
164 uint64_t tx_time;
165 uint64_t rx_time;
166 uint64_t idle_time;
167 uint64_t energy_used;
168 } btif_activity_energy_info_cb_t;
169
170 typedef struct
171 {
172 unsigned int manufact_id;
173 }skip_sdp_entry_t;
174
175 typedef enum
176 {
177 BTIF_DM_FUNC_CREATE_BOND,
178 BTIF_DM_FUNC_CANCEL_BOND,
179 BTIF_DM_FUNC_REMOVE_BOND,
180 BTIF_DM_FUNC_BOND_STATE_CHANGED,
181 } bt_bond_function_t;
182
183 typedef struct
184 {
185 bt_bdaddr_t bd_addr;
186 bt_bond_function_t function;
187 bt_bond_state_t state;
188 struct timespec timestamp;
189 } btif_bond_event_t;
190
191 #define BTA_SERVICE_ID_TO_SERVICE_MASK(id) (1 << (id))
192
193 #define UUID_HUMAN_INTERFACE_DEVICE "00001124-0000-1000-8000-00805f9b34fb"
194
195 #define MAX_BTIF_BOND_EVENT_ENTRIES 15
196
197 static skip_sdp_entry_t sdp_blacklist[] = {{76}}; //Apple Mouse and Keyboard
198
199 /* This flag will be true if HCI_Inquiry is in progress */
200 static BOOLEAN btif_dm_inquiry_in_progress = FALSE;
201
202 /************************************************************************************
203 ** Static variables
204 ************************************************************************************/
205 static char btif_default_local_name[DEFAULT_LOCAL_NAME_MAX+1] = {'\0'};
206 static uid_set_t* uid_set = NULL;
207
208 /* A circular array to keep track of the most recent bond events */
209 static btif_bond_event_t btif_dm_bond_events[MAX_BTIF_BOND_EVENT_ENTRIES + 1];
210
211 static pthread_mutex_t bond_event_lock;
212
213 /* |btif_num_bond_events| keeps track of the total number of events and can be
214 greater than |MAX_BTIF_BOND_EVENT_ENTRIES| */
215 static size_t btif_num_bond_events = 0;
216 static size_t btif_events_start_index = 0;
217 static size_t btif_events_end_index = 0;
218
219 /******************************************************************************
220 ** Static functions
221 ******************************************************************************/
222 static btif_dm_pairing_cb_t pairing_cb;
223 static btif_dm_oob_cb_t oob_cb;
224 static void btif_dm_generic_evt(UINT16 event, char* p_param);
225 static void btif_dm_cb_create_bond(bt_bdaddr_t *bd_addr, tBTA_TRANSPORT transport);
226 static void btif_dm_cb_hid_remote_name(tBTM_REMOTE_DEV_NAME *p_remote_name);
227 static void btif_update_remote_properties(BD_ADDR bd_addr, BD_NAME bd_name,
228 DEV_CLASS dev_class, tBT_DEVICE_TYPE dev_type);
229 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
230 static btif_dm_local_key_cb_t ble_local_key_cb;
231 static void btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif);
232 static void btif_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl);
233 static void btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ *p_pin_req);
234 static void btif_dm_ble_key_nc_req_evt(tBTA_DM_SP_KEY_NOTIF *p_notif_req) ;
235 static void btif_dm_ble_oob_req_evt(tBTA_DM_SP_RMT_OOB *req_oob_type);
236 #endif
237
238 static void bte_scan_filt_param_cfg_evt(UINT8 action_type,
239 tBTA_DM_BLE_PF_AVBL_SPACE avbl_space,
240 tBTA_DM_BLE_REF_VALUE ref_value,
241 tBTA_STATUS status);
242
243 static char* btif_get_default_local_name();
244
245 static void btif_stats_add_bond_event(const bt_bdaddr_t *bd_addr,
246 bt_bond_function_t function,
247 bt_bond_state_t state);
248
249 /******************************************************************************
250 ** Externs
251 ******************************************************************************/
252 extern UINT16 bta_service_id_to_uuid_lkup_tbl [BTA_MAX_SERVICE_ID];
253 extern bt_status_t btif_hf_execute_service(BOOLEAN b_enable);
254 extern bt_status_t btif_av_execute_service(BOOLEAN b_enable);
255 extern bt_status_t btif_av_sink_execute_service(BOOLEAN b_enable);
256 extern bt_status_t btif_hh_execute_service(BOOLEAN b_enable);
257 extern bt_status_t btif_hf_client_execute_service(BOOLEAN b_enable);
258 extern bt_status_t btif_sdp_execute_service(BOOLEAN b_enable);
259 extern int btif_hh_connect(bt_bdaddr_t *bd_addr);
260 extern void bta_gatt_convert_uuid16_to_uuid128(UINT8 uuid_128[LEN_UUID_128], UINT16 uuid_16);
261
262 /******************************************************************************
263 ** Functions
264 ******************************************************************************/
265
is_empty_128bit(uint8_t * data)266 static bool is_empty_128bit(uint8_t *data)
267 {
268 static const uint8_t zero[16] = { 0 };
269 return !memcmp(zero, data, sizeof(zero));
270 }
271
btif_dm_data_copy(uint16_t event,char * dst,char * src)272 static void btif_dm_data_copy(uint16_t event, char *dst, char *src)
273 {
274 tBTA_DM_SEC *dst_dm_sec = (tBTA_DM_SEC*)dst;
275 tBTA_DM_SEC *src_dm_sec = (tBTA_DM_SEC*)src;
276
277 if (!src_dm_sec)
278 return;
279
280 assert(dst_dm_sec);
281 maybe_non_aligned_memcpy(dst_dm_sec, src_dm_sec, sizeof(*src_dm_sec));
282
283 if (event == BTA_DM_BLE_KEY_EVT)
284 {
285 dst_dm_sec->ble_key.p_key_value = osi_malloc(sizeof(tBTM_LE_KEY_VALUE));
286 assert(src_dm_sec->ble_key.p_key_value);
287 memcpy(dst_dm_sec->ble_key.p_key_value, src_dm_sec->ble_key.p_key_value, sizeof(tBTM_LE_KEY_VALUE));
288 }
289 }
290
btif_dm_data_free(uint16_t event,tBTA_DM_SEC * dm_sec)291 static void btif_dm_data_free(uint16_t event, tBTA_DM_SEC *dm_sec)
292 {
293 if (event == BTA_DM_BLE_KEY_EVT)
294 osi_free_and_reset((void **)&dm_sec->ble_key.p_key_value);
295 }
296
btif_dm_init(uid_set_t * set)297 void btif_dm_init(uid_set_t* set)
298 {
299 uid_set = set;
300 pthread_mutex_init(&bond_event_lock, NULL);
301 }
302
btif_dm_cleanup(void)303 void btif_dm_cleanup(void)
304 {
305 if (uid_set) {
306 uid_set_destroy(uid_set);
307 uid_set = NULL;
308 }
309 pthread_mutex_destroy(&bond_event_lock);
310 }
311
btif_in_execute_service_request(tBTA_SERVICE_ID service_id,BOOLEAN b_enable)312 bt_status_t btif_in_execute_service_request(tBTA_SERVICE_ID service_id,
313 BOOLEAN b_enable)
314 {
315 BTIF_TRACE_DEBUG("%s service_id: %d", __FUNCTION__, service_id);
316 /* Check the service_ID and invoke the profile's BT state changed API */
317 switch (service_id)
318 {
319 case BTA_HFP_SERVICE_ID:
320 case BTA_HSP_SERVICE_ID:
321 {
322 btif_hf_execute_service(b_enable);
323 }break;
324 case BTA_A2DP_SOURCE_SERVICE_ID:
325 {
326 btif_av_execute_service(b_enable);
327 }break;
328 case BTA_A2DP_SINK_SERVICE_ID:
329 {
330 btif_av_sink_execute_service(b_enable);
331 }break;
332 case BTA_HID_SERVICE_ID:
333 {
334 btif_hh_execute_service(b_enable);
335 }break;
336 case BTA_HFP_HS_SERVICE_ID:
337 {
338 btif_hf_client_execute_service(b_enable);
339 }break;
340 case BTA_SDP_SERVICE_ID:
341 {
342 btif_sdp_execute_service(b_enable);
343 }break;
344 default:
345 BTIF_TRACE_ERROR("%s: Unknown service %d being %s",
346 __func__, service_id,
347 (b_enable) ? "enabled" : "disabled");
348 return BT_STATUS_FAIL;
349 }
350 return BT_STATUS_SUCCESS;
351 }
352
353 /*******************************************************************************
354 **
355 ** Function check_eir_remote_name
356 **
357 ** Description Check if remote name is in the EIR data
358 **
359 ** Returns TRUE if remote name found
360 ** Populate p_remote_name, if provided and remote name found
361 **
362 *******************************************************************************/
check_eir_remote_name(tBTA_DM_SEARCH * p_search_data,UINT8 * p_remote_name,UINT8 * p_remote_name_len)363 static BOOLEAN check_eir_remote_name(tBTA_DM_SEARCH *p_search_data,
364 UINT8 *p_remote_name, UINT8 *p_remote_name_len)
365 {
366 UINT8 *p_eir_remote_name = NULL;
367 UINT8 remote_name_len = 0;
368
369 /* Check EIR for remote name and services */
370 if (p_search_data->inq_res.p_eir)
371 {
372 p_eir_remote_name = BTM_CheckEirData(p_search_data->inq_res.p_eir,
373 BTM_EIR_COMPLETE_LOCAL_NAME_TYPE, &remote_name_len);
374 if (!p_eir_remote_name)
375 {
376 p_eir_remote_name = BTM_CheckEirData(p_search_data->inq_res.p_eir,
377 BTM_EIR_SHORTENED_LOCAL_NAME_TYPE, &remote_name_len);
378 }
379
380 if (p_eir_remote_name)
381 {
382 if (remote_name_len > BD_NAME_LEN)
383 remote_name_len = BD_NAME_LEN;
384
385 if (p_remote_name && p_remote_name_len)
386 {
387 memcpy(p_remote_name, p_eir_remote_name, remote_name_len);
388 *(p_remote_name + remote_name_len) = 0;
389 *p_remote_name_len = remote_name_len;
390 }
391
392 return TRUE;
393 }
394 }
395
396 return FALSE;
397
398 }
399
400 /*******************************************************************************
401 **
402 ** Function check_cached_remote_name
403 **
404 ** Description Check if remote name is in the NVRAM cache
405 **
406 ** Returns TRUE if remote name found
407 ** Populate p_remote_name, if provided and remote name found
408 **
409 *******************************************************************************/
check_cached_remote_name(tBTA_DM_SEARCH * p_search_data,UINT8 * p_remote_name,UINT8 * p_remote_name_len)410 static BOOLEAN check_cached_remote_name(tBTA_DM_SEARCH *p_search_data,
411 UINT8 *p_remote_name, UINT8 *p_remote_name_len)
412 {
413 bt_bdname_t bdname;
414 bt_bdaddr_t remote_bdaddr;
415 bt_property_t prop_name;
416
417 /* check if we already have it in our btif_storage cache */
418 bdcpy(remote_bdaddr.address, p_search_data->inq_res.bd_addr);
419 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_BDNAME,
420 sizeof(bt_bdname_t), &bdname);
421 if (btif_storage_get_remote_device_property(
422 &remote_bdaddr, &prop_name) == BT_STATUS_SUCCESS)
423 {
424 if (p_remote_name && p_remote_name_len)
425 {
426 strcpy((char *)p_remote_name, (char *)bdname.name);
427 *p_remote_name_len = strlen((char *)p_remote_name);
428 }
429 return TRUE;
430 }
431
432 return FALSE;
433 }
434
get_cod(const bt_bdaddr_t * remote_bdaddr)435 static uint32_t get_cod(const bt_bdaddr_t *remote_bdaddr) {
436 uint32_t remote_cod;
437 bt_property_t prop_name;
438
439 /* check if we already have it in our btif_storage cache */
440 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_CLASS_OF_DEVICE,
441 sizeof(uint32_t), &remote_cod);
442 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr, &prop_name) == BT_STATUS_SUCCESS)
443 {
444 LOG_INFO(LOG_TAG, "%s remote_cod = 0x%08x", __func__, remote_cod);
445 return remote_cod & COD_MASK;
446 }
447
448 return 0;
449 }
450
check_cod(const bt_bdaddr_t * remote_bdaddr,uint32_t cod)451 BOOLEAN check_cod(const bt_bdaddr_t *remote_bdaddr, uint32_t cod)
452 {
453 return get_cod(remote_bdaddr) == cod;
454 }
455
check_cod_hid(const bt_bdaddr_t * remote_bdaddr)456 BOOLEAN check_cod_hid(const bt_bdaddr_t *remote_bdaddr)
457 {
458 return (get_cod(remote_bdaddr) & COD_HID_MASK) == COD_HID_MAJOR;
459 }
460
check_hid_le(const bt_bdaddr_t * remote_bdaddr)461 BOOLEAN check_hid_le(const bt_bdaddr_t *remote_bdaddr)
462 {
463 uint32_t remote_dev_type;
464 bt_property_t prop_name;
465
466 /* check if we already have it in our btif_storage cache */
467 BTIF_STORAGE_FILL_PROPERTY(&prop_name,BT_PROPERTY_TYPE_OF_DEVICE,
468 sizeof(uint32_t), &remote_dev_type);
469 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr,
470 &prop_name) == BT_STATUS_SUCCESS)
471 {
472 if (remote_dev_type == BT_DEVICE_DEVTYPE_BLE)
473 {
474 bdstr_t bdstr;
475 bdaddr_to_string(remote_bdaddr, bdstr, sizeof(bdstr));
476 if(btif_config_exist(bdstr, "HidAppId"))
477 return TRUE;
478 }
479 }
480 return FALSE;
481 }
482
483 /*****************************************************************************
484 **
485 ** Function check_sdp_bl
486 **
487 ** Description Checks if a given device is blacklisted to skip sdp
488 **
489 ** Parameters skip_sdp_entry
490 **
491 ** Returns TRUE if the device is present in blacklist, else FALSE
492 **
493 *******************************************************************************/
check_sdp_bl(const bt_bdaddr_t * remote_bdaddr)494 BOOLEAN check_sdp_bl(const bt_bdaddr_t *remote_bdaddr)
495 {
496 UINT16 manufacturer = 0;
497 UINT8 lmp_ver = 0;
498 UINT16 lmp_subver = 0;
499 bt_property_t prop_name;
500 bt_remote_version_t info;
501
502 if (remote_bdaddr == NULL)
503 return FALSE;
504
505 /* fetch additional info about remote device used in iop query */
506 BTM_ReadRemoteVersion(*(BD_ADDR*)remote_bdaddr, &lmp_ver,
507 &manufacturer, &lmp_subver);
508
509 /* if not available yet, try fetching from config database */
510 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_REMOTE_VERSION_INFO,
511 sizeof(bt_remote_version_t), &info);
512
513 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr,
514 &prop_name) != BT_STATUS_SUCCESS)
515 {
516
517 return FALSE;
518 }
519 manufacturer = info.manufacturer;
520
521 for (unsigned int i = 0; i < ARRAY_SIZE(sdp_blacklist); i++)
522 {
523 if (manufacturer == sdp_blacklist[i].manufact_id)
524 return TRUE;
525 }
526 return FALSE;
527 }
528
bond_state_changed(bt_status_t status,bt_bdaddr_t * bd_addr,bt_bond_state_t state)529 static void bond_state_changed(bt_status_t status, bt_bdaddr_t *bd_addr, bt_bond_state_t state)
530 {
531
532 btif_stats_add_bond_event(bd_addr, BTIF_DM_FUNC_BOND_STATE_CHANGED, state);
533
534 // Send bonding state only once - based on outgoing/incoming we may receive duplicates
535 if ((pairing_cb.state == state) && (state == BT_BOND_STATE_BONDING))
536 {
537 // Cross key pairing so send callback for static address
538 if (!bdaddr_is_empty(&pairing_cb.static_bdaddr))
539 {
540 HAL_CBACK(bt_hal_cbacks, bond_state_changed_cb, status, bd_addr, state);
541 }
542 return;
543 }
544
545 if (pairing_cb.bond_type == BOND_TYPE_TEMPORARY)
546 state = BT_BOND_STATE_NONE;
547
548 BTIF_TRACE_DEBUG("%s: state=%d, prev_state=%d, sdp_attempts = %d", __func__,
549 state, pairing_cb.state, pairing_cb.sdp_attempts);
550
551 HAL_CBACK(bt_hal_cbacks, bond_state_changed_cb, status, bd_addr, state);
552
553 if (state == BT_BOND_STATE_BONDING)
554 {
555 pairing_cb.state = state;
556 bdcpy(pairing_cb.bd_addr, bd_addr->address);
557 } else {
558 if (!pairing_cb.sdp_attempts)
559 memset(&pairing_cb, 0, sizeof(pairing_cb));
560 else
561 BTIF_TRACE_DEBUG("%s: BR-EDR service discovery active", __func__);
562 }
563 }
564
565 /* store remote version in bt config to always have access
566 to it post pairing*/
btif_update_remote_version_property(bt_bdaddr_t * p_bd)567 static void btif_update_remote_version_property(bt_bdaddr_t *p_bd)
568 {
569 bt_property_t property;
570 UINT8 lmp_ver = 0;
571 UINT16 lmp_subver = 0;
572 UINT16 mfct_set = 0;
573 tBTM_STATUS btm_status;
574 bt_remote_version_t info;
575 bt_status_t status;
576 bdstr_t bdstr;
577
578 btm_status = BTM_ReadRemoteVersion(*(BD_ADDR*)p_bd, &lmp_ver,
579 &mfct_set, &lmp_subver);
580
581 LOG_DEBUG(LOG_TAG, "remote version info [%s]: %x, %x, %x", bdaddr_to_string(p_bd, bdstr, sizeof(bdstr)),
582 lmp_ver, mfct_set, lmp_subver);
583
584 if (btm_status == BTM_SUCCESS)
585 {
586 // Always update cache to ensure we have availability whenever BTM API is not populated
587 info.manufacturer = mfct_set;
588 info.sub_ver = lmp_subver;
589 info.version = lmp_ver;
590 BTIF_STORAGE_FILL_PROPERTY(&property,
591 BT_PROPERTY_REMOTE_VERSION_INFO, sizeof(bt_remote_version_t),
592 &info);
593 status = btif_storage_set_remote_device_property(p_bd, &property);
594 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote version", status);
595 }
596 }
597
btif_update_remote_properties(BD_ADDR bd_addr,BD_NAME bd_name,DEV_CLASS dev_class,tBT_DEVICE_TYPE device_type)598 static void btif_update_remote_properties(BD_ADDR bd_addr, BD_NAME bd_name,
599 DEV_CLASS dev_class, tBT_DEVICE_TYPE device_type)
600 {
601 int num_properties = 0;
602 bt_property_t properties[3];
603 bt_bdaddr_t bdaddr;
604 bt_status_t status;
605 UINT32 cod;
606 bt_device_type_t dev_type;
607
608 memset(properties, 0, sizeof(properties));
609 bdcpy(bdaddr.address, bd_addr);
610
611 /* remote name */
612 if (strlen((const char *) bd_name))
613 {
614 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
615 BT_PROPERTY_BDNAME, strlen((char *)bd_name), bd_name);
616 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
617 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device name", status);
618 num_properties++;
619 }
620
621 /* class of device */
622 cod = devclass2uint(dev_class);
623 BTIF_TRACE_DEBUG("%s cod is 0x%06x", __func__, cod);
624 if ( cod == 0) {
625 /* Try to retrieve cod from storage */
626 BTIF_TRACE_DEBUG("%s cod is 0, checking cod from storage", __func__);
627 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
628 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
629 status = btif_storage_get_remote_device_property(&bdaddr, &properties[num_properties]);
630 BTIF_TRACE_DEBUG("%s cod retrieved from storage is 0x%06x", __func__, cod);
631 if ( cod == 0) {
632 BTIF_TRACE_DEBUG("%s cod is again 0, set as unclassified", __func__);
633 cod = COD_UNCLASSIFIED;
634 }
635 }
636
637 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
638 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
639 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
640 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device class", status);
641 num_properties++;
642
643 /* device type */
644 bt_property_t prop_name;
645 uint8_t remote_dev_type;
646 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_TYPE_OF_DEVICE,
647 sizeof(uint8_t), &remote_dev_type);
648 if (btif_storage_get_remote_device_property(&bdaddr, &prop_name) == BT_STATUS_SUCCESS)
649 dev_type = remote_dev_type | device_type;
650 else
651 dev_type = device_type;
652
653 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
654 BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type), &dev_type);
655 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
656 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device type", status);
657 num_properties++;
658
659 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
660 status, &bdaddr, num_properties, properties);
661 }
662
663 /*******************************************************************************
664 **
665 ** Function btif_dm_cb_hid_remote_name
666 **
667 ** Description Remote name callback for HID device. Called in btif context
668 ** Special handling for HID devices
669 **
670 ** Returns void
671 **
672 *******************************************************************************/
btif_dm_cb_hid_remote_name(tBTM_REMOTE_DEV_NAME * p_remote_name)673 static void btif_dm_cb_hid_remote_name(tBTM_REMOTE_DEV_NAME *p_remote_name)
674 {
675 BTIF_TRACE_DEBUG("%s: status=%d pairing_cb.state=%d", __FUNCTION__, p_remote_name->status, pairing_cb.state);
676 if (pairing_cb.state == BT_BOND_STATE_BONDING)
677 {
678 bt_bdaddr_t remote_bd;
679
680 bdcpy(remote_bd.address, pairing_cb.bd_addr);
681
682 if (p_remote_name->status == BTM_SUCCESS)
683 {
684 bond_state_changed(BT_STATUS_SUCCESS, &remote_bd, BT_BOND_STATE_BONDED);
685 }
686 else
687 bond_state_changed(BT_STATUS_FAIL, &remote_bd, BT_BOND_STATE_NONE);
688 }
689 }
690
691 /*******************************************************************************
692 **
693 ** Function btif_dm_cb_create_bond
694 **
695 ** Description Create bond initiated from the BTIF thread context
696 ** Special handling for HID devices
697 **
698 ** Returns void
699 **
700 *******************************************************************************/
btif_dm_cb_create_bond(bt_bdaddr_t * bd_addr,tBTA_TRANSPORT transport)701 static void btif_dm_cb_create_bond(bt_bdaddr_t *bd_addr, tBTA_TRANSPORT transport)
702 {
703 BOOLEAN is_hid = check_cod(bd_addr, COD_HID_POINTING);
704 bond_state_changed(BT_STATUS_SUCCESS, bd_addr, BT_BOND_STATE_BONDING);
705
706 #if BLE_INCLUDED == TRUE
707 int device_type;
708 int addr_type;
709 bdstr_t bdstr;
710 bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr));
711 if (transport == BT_TRANSPORT_LE)
712 {
713 if (!btif_config_get_int((char const *)&bdstr,"DevType", &device_type))
714 {
715 btif_config_set_int(bdstr, "DevType", BT_DEVICE_TYPE_BLE);
716 }
717 if (btif_storage_get_remote_addr_type(bd_addr, &addr_type) != BT_STATUS_SUCCESS)
718 {
719 btif_storage_set_remote_addr_type(bd_addr, BLE_ADDR_PUBLIC);
720 }
721 }
722 if((btif_config_get_int((char const *)&bdstr,"DevType", &device_type) &&
723 (btif_storage_get_remote_addr_type(bd_addr, &addr_type) == BT_STATUS_SUCCESS) &&
724 (device_type & BT_DEVICE_TYPE_BLE) == BT_DEVICE_TYPE_BLE) || (transport == BT_TRANSPORT_LE))
725 {
726 BTA_DmAddBleDevice(bd_addr->address, addr_type, device_type);
727 }
728 #endif
729
730 #if BLE_INCLUDED == TRUE
731 if(is_hid && (device_type & BT_DEVICE_TYPE_BLE) == 0)
732 #else
733 if(is_hid)
734 #endif
735 {
736 int status;
737 status = btif_hh_connect(bd_addr);
738 if(status != BT_STATUS_SUCCESS)
739 bond_state_changed(status, bd_addr, BT_BOND_STATE_NONE);
740 }
741 else
742 {
743 BTA_DmBondByTransport((UINT8 *)bd_addr->address, transport);
744 }
745 /* Track originator of bond creation */
746 pairing_cb.is_local_initiated = TRUE;
747
748 }
749
750 /*******************************************************************************
751 **
752 ** Function btif_dm_cb_remove_bond
753 **
754 ** Description remove bond initiated from the BTIF thread context
755 ** Special handling for HID devices
756 **
757 ** Returns void
758 **
759 *******************************************************************************/
btif_dm_cb_remove_bond(bt_bdaddr_t * bd_addr)760 void btif_dm_cb_remove_bond(bt_bdaddr_t *bd_addr)
761 {
762 /*special handling for HID devices */
763 /* VUP needs to be sent if its a HID Device. The HID HOST module will check if there
764 is a valid hid connection with this bd_addr. If yes VUP will be issued.*/
765 #if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE))
766 if (btif_hh_virtual_unplug(bd_addr) != BT_STATUS_SUCCESS)
767 #endif
768 {
769 BTIF_TRACE_DEBUG("%s: Removing HH device", __func__);
770 BTA_DmRemoveDevice((UINT8 *)bd_addr->address);
771 }
772 }
773
774 /*******************************************************************************
775 **
776 ** Function btif_dm_get_connection_state
777 **
778 ** Description Returns whether the remote device is currently connected
779 ** and whether encryption is active for the connection
780 **
781 ** Returns 0 if not connected; 1 if connected and > 1 if connection is
782 ** encrypted
783 **
784 *******************************************************************************/
btif_dm_get_connection_state(const bt_bdaddr_t * bd_addr)785 uint16_t btif_dm_get_connection_state(const bt_bdaddr_t *bd_addr)
786 {
787 uint8_t *bda = (uint8_t*)bd_addr->address;
788 uint16_t rc = BTA_DmGetConnectionState(bda);
789
790 if (rc != 0)
791 {
792 uint8_t flags = 0;
793
794 BTM_GetSecurityFlagsByTransport(bda, &flags, BT_TRANSPORT_BR_EDR);
795 BTIF_TRACE_DEBUG("%s: security flags (BR/EDR)=0x%02x", __FUNCTION__, flags);
796 if (flags & BTM_SEC_FLAG_ENCRYPTED)
797 rc |= ENCRYPTED_BREDR;
798
799 BTM_GetSecurityFlagsByTransport(bda, &flags, BT_TRANSPORT_LE);
800 BTIF_TRACE_DEBUG("%s: security flags (LE)=0x%02x", __FUNCTION__, flags);
801 if (flags & BTM_SEC_FLAG_ENCRYPTED)
802 rc |= ENCRYPTED_LE;
803 }
804
805 return rc;
806 }
807
808 /*******************************************************************************
809 **
810 ** Function search_devices_copy_cb
811 **
812 ** Description Deep copy callback for search devices event
813 **
814 ** Returns void
815 **
816 *******************************************************************************/
search_devices_copy_cb(UINT16 event,char * p_dest,char * p_src)817 static void search_devices_copy_cb(UINT16 event, char *p_dest, char *p_src)
818 {
819 tBTA_DM_SEARCH *p_dest_data = (tBTA_DM_SEARCH *) p_dest;
820 tBTA_DM_SEARCH *p_src_data = (tBTA_DM_SEARCH *) p_src;
821
822 if (!p_src)
823 return;
824
825 BTIF_TRACE_DEBUG("%s: event=%s", __FUNCTION__, dump_dm_search_event(event));
826 maybe_non_aligned_memcpy(p_dest_data, p_src_data, sizeof(*p_src_data));
827 switch (event)
828 {
829 case BTA_DM_INQ_RES_EVT:
830 {
831 if (p_src_data->inq_res.p_eir)
832 {
833 p_dest_data->inq_res.p_eir = (UINT8 *)(p_dest + sizeof(tBTA_DM_SEARCH));
834 memcpy(p_dest_data->inq_res.p_eir, p_src_data->inq_res.p_eir, HCI_EXT_INQ_RESPONSE_LEN);
835 }
836 }
837 break;
838
839 case BTA_DM_DISC_RES_EVT:
840 {
841 if (p_src_data->disc_res.raw_data_size && p_src_data->disc_res.p_raw_data)
842 {
843 p_dest_data->disc_res.p_raw_data = (UINT8 *)(p_dest + sizeof(tBTA_DM_SEARCH));
844 memcpy(p_dest_data->disc_res.p_raw_data,
845 p_src_data->disc_res.p_raw_data, p_src_data->disc_res.raw_data_size);
846 }
847 }
848 break;
849 }
850 }
851
search_services_copy_cb(UINT16 event,char * p_dest,char * p_src)852 static void search_services_copy_cb(UINT16 event, char *p_dest, char *p_src)
853 {
854 tBTA_DM_SEARCH *p_dest_data = (tBTA_DM_SEARCH *) p_dest;
855 tBTA_DM_SEARCH *p_src_data = (tBTA_DM_SEARCH *) p_src;
856
857 if (!p_src)
858 return;
859 maybe_non_aligned_memcpy(p_dest_data, p_src_data, sizeof(*p_src_data));
860 switch (event)
861 {
862 case BTA_DM_DISC_RES_EVT:
863 {
864 if (p_src_data->disc_res.result == BTA_SUCCESS)
865 {
866 if (p_src_data->disc_res.num_uuids > 0)
867 {
868 p_dest_data->disc_res.p_uuid_list =
869 (UINT8*)(p_dest + sizeof(tBTA_DM_SEARCH));
870 memcpy(p_dest_data->disc_res.p_uuid_list, p_src_data->disc_res.p_uuid_list,
871 p_src_data->disc_res.num_uuids*MAX_UUID_SIZE);
872 osi_free_and_reset((void **)&p_src_data->disc_res.p_uuid_list);
873 }
874 osi_free_and_reset((void **)&p_src_data->disc_res.p_raw_data);
875 }
876 } break;
877 }
878 }
879 /******************************************************************************
880 **
881 ** BTIF DM callback events
882 **
883 *****************************************************************************/
884
885 /*******************************************************************************
886 **
887 ** Function btif_dm_pin_req_evt
888 **
889 ** Description Executes pin request event in btif context
890 **
891 ** Returns void
892 **
893 *******************************************************************************/
btif_dm_pin_req_evt(tBTA_DM_PIN_REQ * p_pin_req)894 static void btif_dm_pin_req_evt(tBTA_DM_PIN_REQ *p_pin_req)
895 {
896 bt_bdaddr_t bd_addr;
897 bt_bdname_t bd_name;
898 UINT32 cod;
899 bt_pin_code_t pin_code;
900 int dev_type;
901
902 /* Remote properties update */
903 if (!btif_get_device_type(p_pin_req->bd_addr, &dev_type))
904 {
905 dev_type = BT_DEVICE_TYPE_BREDR;
906 }
907 btif_update_remote_properties(p_pin_req->bd_addr, p_pin_req->bd_name,
908 p_pin_req->dev_class, (tBT_DEVICE_TYPE) dev_type);
909
910 bdcpy(bd_addr.address, p_pin_req->bd_addr);
911 memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN);
912
913 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
914
915 cod = devclass2uint(p_pin_req->dev_class);
916
917 if (cod == 0) {
918 BTIF_TRACE_DEBUG("%s cod is 0, set as unclassified", __func__);
919 cod = COD_UNCLASSIFIED;
920 }
921
922 /* check for auto pair possiblity only if bond was initiated by local device */
923 if (pairing_cb.is_local_initiated && (p_pin_req->min_16_digit == FALSE))
924 {
925 if (check_cod(&bd_addr, COD_AV_HEADSETS) ||
926 check_cod(&bd_addr, COD_AV_HANDSFREE) ||
927 check_cod(&bd_addr, COD_AV_HEADPHONES) ||
928 check_cod(&bd_addr, COD_AV_PORTABLE_AUDIO) ||
929 check_cod(&bd_addr, COD_AV_HIFI_AUDIO) ||
930 check_cod(&bd_addr, COD_HID_POINTING))
931 {
932 /* Check if this device can be auto paired */
933 if (!interop_match_addr(INTEROP_DISABLE_AUTO_PAIRING, &bd_addr) &&
934 !interop_match_name(INTEROP_DISABLE_AUTO_PAIRING, (const char *)bd_name.name) &&
935 (pairing_cb.autopair_attempts == 0))
936 {
937 BTIF_TRACE_DEBUG("%s() Attempting auto pair", __FUNCTION__);
938 pin_code.pin[0] = 0x30;
939 pin_code.pin[1] = 0x30;
940 pin_code.pin[2] = 0x30;
941 pin_code.pin[3] = 0x30;
942
943 pairing_cb.autopair_attempts++;
944 BTA_DmPinReply( (UINT8*)bd_addr.address, TRUE, 4, pin_code.pin);
945 return;
946 }
947 }
948 else if (check_cod(&bd_addr, COD_HID_KEYBOARD) ||
949 check_cod(&bd_addr, COD_HID_COMBO))
950 {
951 if ((interop_match_addr(INTEROP_KEYBOARD_REQUIRES_FIXED_PIN, &bd_addr) == TRUE) &&
952 (pairing_cb.autopair_attempts == 0))
953 {
954 BTIF_TRACE_DEBUG("%s() Attempting auto pair", __FUNCTION__);
955 pin_code.pin[0] = 0x30;
956 pin_code.pin[1] = 0x30;
957 pin_code.pin[2] = 0x30;
958 pin_code.pin[3] = 0x30;
959
960 pairing_cb.autopair_attempts++;
961 BTA_DmPinReply( (UINT8*)bd_addr.address, TRUE, 4, pin_code.pin);
962 return;
963 }
964 }
965 }
966 HAL_CBACK(bt_hal_cbacks, pin_request_cb,
967 &bd_addr, &bd_name, cod, p_pin_req->min_16_digit);
968 }
969
970 /*******************************************************************************
971 **
972 ** Function btif_dm_ssp_cfm_req_evt
973 **
974 ** Description Executes SSP confirm request event in btif context
975 **
976 ** Returns void
977 **
978 *******************************************************************************/
btif_dm_ssp_cfm_req_evt(tBTA_DM_SP_CFM_REQ * p_ssp_cfm_req)979 static void btif_dm_ssp_cfm_req_evt(tBTA_DM_SP_CFM_REQ *p_ssp_cfm_req)
980 {
981 bt_bdaddr_t bd_addr;
982 bt_bdname_t bd_name;
983 UINT32 cod;
984 BOOLEAN is_incoming = !(pairing_cb.state == BT_BOND_STATE_BONDING);
985 int dev_type;
986
987 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
988
989 /* Remote properties update */
990 if (!btif_get_device_type(p_ssp_cfm_req->bd_addr, &dev_type))
991 {
992 dev_type = BT_DEVICE_TYPE_BREDR;
993 }
994 btif_update_remote_properties(p_ssp_cfm_req->bd_addr, p_ssp_cfm_req->bd_name,
995 p_ssp_cfm_req->dev_class, (tBT_DEVICE_TYPE) dev_type);
996
997 bdcpy(bd_addr.address, p_ssp_cfm_req->bd_addr);
998 memcpy(bd_name.name, p_ssp_cfm_req->bd_name, BD_NAME_LEN);
999
1000 /* Set the pairing_cb based on the local & remote authentication requirements */
1001 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
1002
1003 /* if just_works and bonding bit is not set treat this as temporary */
1004 if (p_ssp_cfm_req->just_works && !(p_ssp_cfm_req->loc_auth_req & BTM_AUTH_BONDS) &&
1005 !(p_ssp_cfm_req->rmt_auth_req & BTM_AUTH_BONDS) &&
1006 !(check_cod((bt_bdaddr_t*)&p_ssp_cfm_req->bd_addr, COD_HID_POINTING)))
1007 pairing_cb.bond_type = BOND_TYPE_TEMPORARY;
1008 else
1009 pairing_cb.bond_type = BOND_TYPE_PERSISTENT;
1010
1011 btm_set_bond_type_dev(p_ssp_cfm_req->bd_addr, pairing_cb.bond_type);
1012
1013 pairing_cb.is_ssp = TRUE;
1014
1015 /* If JustWorks auto-accept */
1016 if (p_ssp_cfm_req->just_works)
1017 {
1018 /* Pairing consent for JustWorks needed if:
1019 * 1. Incoming (non-temporary) pairing is detected AND
1020 * 2. local IO capabilities are DisplayYesNo AND
1021 * 3. remote IO capabiltiies are DisplayOnly or NoInputNoOutput;
1022 */
1023 if (is_incoming && pairing_cb.bond_type != BOND_TYPE_TEMPORARY &&
1024 ((p_ssp_cfm_req->loc_io_caps == HCI_IO_CAP_DISPLAY_YESNO) &&
1025 (p_ssp_cfm_req->rmt_io_caps == HCI_IO_CAP_DISPLAY_ONLY ||
1026 p_ssp_cfm_req->rmt_io_caps == HCI_IO_CAP_NO_IO)))
1027 {
1028 BTIF_TRACE_EVENT("%s: User consent needed for incoming pairing request. loc_io_caps: %d, rmt_io_caps: %d",
1029 __FUNCTION__, p_ssp_cfm_req->loc_io_caps, p_ssp_cfm_req->rmt_io_caps);
1030 }
1031 else
1032 {
1033 BTIF_TRACE_EVENT("%s: Auto-accept JustWorks pairing", __FUNCTION__);
1034 btif_dm_ssp_reply(&bd_addr, BT_SSP_VARIANT_CONSENT, TRUE, 0);
1035 return;
1036 }
1037 }
1038
1039 cod = devclass2uint(p_ssp_cfm_req->dev_class);
1040
1041 if (cod == 0) {
1042 LOG_DEBUG(LOG_TAG, "%s cod is 0, set as unclassified", __func__);
1043 cod = COD_UNCLASSIFIED;
1044 }
1045
1046 pairing_cb.sdp_attempts = 0;
1047 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod,
1048 (p_ssp_cfm_req->just_works ? BT_SSP_VARIANT_CONSENT : BT_SSP_VARIANT_PASSKEY_CONFIRMATION),
1049 p_ssp_cfm_req->num_val);
1050 }
1051
btif_dm_ssp_key_notif_evt(tBTA_DM_SP_KEY_NOTIF * p_ssp_key_notif)1052 static void btif_dm_ssp_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif)
1053 {
1054 bt_bdaddr_t bd_addr;
1055 bt_bdname_t bd_name;
1056 UINT32 cod;
1057 int dev_type;
1058
1059 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
1060
1061 /* Remote properties update */
1062 if (!btif_get_device_type(p_ssp_key_notif->bd_addr, &dev_type))
1063 {
1064 dev_type = BT_DEVICE_TYPE_BREDR;
1065 }
1066 btif_update_remote_properties(p_ssp_key_notif->bd_addr, p_ssp_key_notif->bd_name,
1067 p_ssp_key_notif->dev_class, (tBT_DEVICE_TYPE) dev_type);
1068
1069 bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr);
1070 memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN);
1071
1072 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
1073 pairing_cb.is_ssp = TRUE;
1074 cod = devclass2uint(p_ssp_key_notif->dev_class);
1075
1076 if (cod == 0) {
1077 LOG_DEBUG(LOG_TAG, "%s cod is 0, set as unclassified", __func__);
1078 cod = COD_UNCLASSIFIED;
1079 }
1080
1081 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name,
1082 cod, BT_SSP_VARIANT_PASSKEY_NOTIFICATION,
1083 p_ssp_key_notif->passkey);
1084 }
1085 /*******************************************************************************
1086 **
1087 ** Function btif_dm_auth_cmpl_evt
1088 **
1089 ** Description Executes authentication complete event in btif context
1090 **
1091 ** Returns void
1092 **
1093 *******************************************************************************/
btif_dm_auth_cmpl_evt(tBTA_DM_AUTH_CMPL * p_auth_cmpl)1094 static void btif_dm_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl)
1095 {
1096 /* Save link key, if not temporary */
1097 bt_bdaddr_t bd_addr;
1098 bt_status_t status = BT_STATUS_FAIL;
1099 bt_bond_state_t state = BT_BOND_STATE_NONE;
1100 BOOLEAN skip_sdp = FALSE;
1101
1102 BTIF_TRACE_DEBUG("%s: bond state=%d", __func__, pairing_cb.state);
1103
1104 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
1105 if ( (p_auth_cmpl->success == TRUE) && (p_auth_cmpl->key_present) )
1106 {
1107 if ((p_auth_cmpl->key_type < HCI_LKEY_TYPE_DEBUG_COMB) ||
1108 (p_auth_cmpl->key_type == HCI_LKEY_TYPE_AUTH_COMB) ||
1109 (p_auth_cmpl->key_type == HCI_LKEY_TYPE_CHANGED_COMB) ||
1110 (p_auth_cmpl->key_type == HCI_LKEY_TYPE_AUTH_COMB_P_256) ||
1111 pairing_cb.bond_type == BOND_TYPE_PERSISTENT)
1112 {
1113 bt_status_t ret;
1114 BTIF_TRACE_DEBUG("%s: Storing link key. key_type=0x%x, bond_type=%d",
1115 __FUNCTION__, p_auth_cmpl->key_type, pairing_cb.bond_type);
1116 ret = btif_storage_add_bonded_device(&bd_addr,
1117 p_auth_cmpl->key, p_auth_cmpl->key_type,
1118 pairing_cb.pin_code_len);
1119 ASSERTC(ret == BT_STATUS_SUCCESS, "storing link key failed", ret);
1120 }
1121 else
1122 {
1123 BTIF_TRACE_DEBUG("%s: Temporary key. Not storing. key_type=0x%x, bond_type=%d",
1124 __FUNCTION__, p_auth_cmpl->key_type, pairing_cb.bond_type);
1125 if(pairing_cb.bond_type == BOND_TYPE_TEMPORARY)
1126 {
1127 BTIF_TRACE_DEBUG("%s: sending BT_BOND_STATE_NONE for Temp pairing",
1128 __FUNCTION__);
1129 btif_storage_remove_bonded_device(&bd_addr);
1130 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE);
1131 return;
1132 }
1133 }
1134 }
1135
1136 // We could have received a new link key without going through the pairing flow.
1137 // If so, we don't want to perform SDP or any other operations on the authenticated
1138 // device.
1139 if (bdcmp(p_auth_cmpl->bd_addr, pairing_cb.bd_addr) != 0) {
1140 char address[32];
1141 bt_bdaddr_t bt_bdaddr;
1142
1143 memcpy(bt_bdaddr.address, p_auth_cmpl->bd_addr,
1144 sizeof(bt_bdaddr.address));
1145 bdaddr_to_string(&bt_bdaddr, address, sizeof(address));
1146 LOG_INFO(LOG_TAG, "%s skipping SDP since we did not initiate pairing to %s.", __func__, address);
1147 return;
1148 }
1149
1150 // Skip SDP for certain HID Devices
1151 if (p_auth_cmpl->success)
1152 {
1153 #if BLE_INCLUDED == TRUE
1154 btif_storage_set_remote_addr_type(&bd_addr, p_auth_cmpl->addr_type);
1155 #endif
1156 btif_update_remote_properties(p_auth_cmpl->bd_addr,
1157 p_auth_cmpl->bd_name, NULL, p_auth_cmpl->dev_type);
1158 pairing_cb.timeout_retries = 0;
1159 status = BT_STATUS_SUCCESS;
1160 state = BT_BOND_STATE_BONDED;
1161 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
1162
1163 if (check_sdp_bl(&bd_addr) && check_cod_hid(&bd_addr))
1164 {
1165 LOG_WARN(LOG_TAG, "%s:skip SDP", __FUNCTION__);
1166 skip_sdp = TRUE;
1167 }
1168 if(!pairing_cb.is_local_initiated && skip_sdp)
1169 {
1170 bond_state_changed(status, &bd_addr, state);
1171
1172 LOG_WARN(LOG_TAG, "%s: Incoming HID Connection",__FUNCTION__);
1173 bt_property_t prop;
1174 bt_bdaddr_t bd_addr;
1175 bt_uuid_t uuid;
1176 char uuid_str[128] = UUID_HUMAN_INTERFACE_DEVICE;
1177
1178 string_to_uuid(uuid_str, &uuid);
1179
1180 prop.type = BT_PROPERTY_UUIDS;
1181 prop.val = uuid.uu;
1182 prop.len = MAX_UUID_SIZE;
1183
1184 /* Send the event to the BTIF */
1185 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1186 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1187 }
1188 else
1189 {
1190 #if BLE_INCLUDED == TRUE
1191 BOOLEAN is_crosskey = FALSE;
1192 /* If bonded due to cross-key, save the static address too*/
1193 if(pairing_cb.state == BT_BOND_STATE_BONDING &&
1194 (bdcmp(p_auth_cmpl->bd_addr, pairing_cb.bd_addr) != 0))
1195 {
1196 BTIF_TRACE_DEBUG("%s: bonding initiated due to cross key, adding static address",
1197 __func__);
1198 bdcpy(pairing_cb.static_bdaddr.address, p_auth_cmpl->bd_addr);
1199 is_crosskey = TRUE;
1200 }
1201 if (!is_crosskey || !(stack_config_get_interface()->get_pts_crosskey_sdp_disable()))
1202 {
1203 #endif
1204
1205 // Ensure inquiry is stopped before attempting service discovery
1206 btif_dm_cancel_discovery();
1207
1208 /* Trigger SDP on the device */
1209 pairing_cb.sdp_attempts = 1;
1210 btif_dm_get_remote_services(&bd_addr);
1211 #if BLE_INCLUDED == TRUE
1212 }
1213 #endif
1214 }
1215 // Do not call bond_state_changed_cb yet. Wait until remote service discovery is complete
1216 }
1217 else
1218 {
1219 // Map the HCI fail reason to bt status
1220 switch(p_auth_cmpl->fail_reason)
1221 {
1222 case HCI_ERR_PAGE_TIMEOUT:
1223 if (interop_match_addr(INTEROP_AUTO_RETRY_PAIRING, &bd_addr)
1224 && pairing_cb.timeout_retries)
1225 {
1226 BTIF_TRACE_WARNING("%s() - Pairing timeout; retrying (%d) ...", __FUNCTION__, pairing_cb.timeout_retries);
1227 --pairing_cb.timeout_retries;
1228 btif_dm_cb_create_bond (&bd_addr, BTA_TRANSPORT_UNKNOWN);
1229 return;
1230 }
1231 /* Fall-through */
1232 case HCI_ERR_CONNECTION_TOUT:
1233 status = BT_STATUS_RMT_DEV_DOWN;
1234 break;
1235
1236 case HCI_ERR_PAIRING_NOT_ALLOWED:
1237 status = BT_STATUS_AUTH_REJECTED;
1238 break;
1239
1240 case HCI_ERR_LMP_RESPONSE_TIMEOUT:
1241 status = BT_STATUS_AUTH_FAILURE;
1242 break;
1243
1244 /* map the auth failure codes, so we can retry pairing if necessary */
1245 case HCI_ERR_AUTH_FAILURE:
1246 case HCI_ERR_KEY_MISSING:
1247 btif_storage_remove_bonded_device(&bd_addr);
1248 case HCI_ERR_HOST_REJECT_SECURITY:
1249 case HCI_ERR_ENCRY_MODE_NOT_ACCEPTABLE:
1250 case HCI_ERR_UNIT_KEY_USED:
1251 case HCI_ERR_PAIRING_WITH_UNIT_KEY_NOT_SUPPORTED:
1252 case HCI_ERR_INSUFFCIENT_SECURITY:
1253 case HCI_ERR_PEER_USER:
1254 case HCI_ERR_UNSPECIFIED:
1255 BTIF_TRACE_DEBUG(" %s() Authentication fail reason %d",
1256 __FUNCTION__, p_auth_cmpl->fail_reason);
1257 if (pairing_cb.autopair_attempts == 1)
1258 {
1259 /* Create the Bond once again */
1260 BTIF_TRACE_WARNING("%s() auto pair failed. Reinitiate Bond", __FUNCTION__);
1261 btif_dm_cb_create_bond (&bd_addr, BTA_TRANSPORT_UNKNOWN);
1262 return;
1263 }
1264 else
1265 {
1266 /* if autopair attempts are more than 1, or not attempted */
1267 status = BT_STATUS_AUTH_FAILURE;
1268 }
1269 break;
1270
1271 default:
1272 status = BT_STATUS_FAIL;
1273 }
1274 /* Special Handling for HID Devices */
1275 if (check_cod(&bd_addr, COD_HID_POINTING)) {
1276 /* Remove Device as bonded in nvram as authentication failed */
1277 BTIF_TRACE_DEBUG("%s(): removing hid pointing device from nvram", __FUNCTION__);
1278 btif_storage_remove_bonded_device(&bd_addr);
1279 }
1280 bond_state_changed(status, &bd_addr, state);
1281 }
1282 }
1283
1284 /******************************************************************************
1285 **
1286 ** Function btif_dm_search_devices_evt
1287 **
1288 ** Description Executes search devices callback events in btif context
1289 **
1290 ** Returns void
1291 **
1292 ******************************************************************************/
btif_dm_search_devices_evt(UINT16 event,char * p_param)1293 static void btif_dm_search_devices_evt (UINT16 event, char *p_param)
1294 {
1295 tBTA_DM_SEARCH *p_search_data;
1296 BTIF_TRACE_EVENT("%s event=%s", __FUNCTION__, dump_dm_search_event(event));
1297
1298 switch (event)
1299 {
1300 case BTA_DM_DISC_RES_EVT:
1301 {
1302 p_search_data = (tBTA_DM_SEARCH *)p_param;
1303 /* Remote name update */
1304 if (strlen((const char *) p_search_data->disc_res.bd_name))
1305 {
1306 bt_property_t properties[1];
1307 bt_bdaddr_t bdaddr;
1308 bt_status_t status;
1309
1310 properties[0].type = BT_PROPERTY_BDNAME;
1311 properties[0].val = p_search_data->disc_res.bd_name;
1312 properties[0].len = strlen((char *)p_search_data->disc_res.bd_name);
1313 bdcpy(bdaddr.address, p_search_data->disc_res.bd_addr);
1314
1315 status = btif_storage_set_remote_device_property(&bdaddr, &properties[0]);
1316 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device property", status);
1317 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1318 status, &bdaddr, 1, properties);
1319 }
1320 /* TODO: Services? */
1321 }
1322 break;
1323
1324 case BTA_DM_INQ_RES_EVT:
1325 {
1326 /* inquiry result */
1327 UINT32 cod;
1328 bt_bdname_t bdname;
1329 bt_bdaddr_t bdaddr;
1330 UINT8 remote_name_len;
1331 tBTA_SERVICE_MASK services = 0;
1332 bdstr_t bdstr;
1333
1334 p_search_data = (tBTA_DM_SEARCH *)p_param;
1335 bdcpy(bdaddr.address, p_search_data->inq_res.bd_addr);
1336
1337 BTIF_TRACE_DEBUG("%s() %s device_type = 0x%x\n", __FUNCTION__, bdaddr_to_string(&bdaddr, bdstr, sizeof(bdstr)),
1338 #if (BLE_INCLUDED == TRUE)
1339 p_search_data->inq_res.device_type);
1340 #else
1341 BT_DEVICE_TYPE_BREDR);
1342 #endif
1343 bdname.name[0] = 0;
1344
1345 cod = devclass2uint (p_search_data->inq_res.dev_class);
1346
1347 if (cod == 0) {
1348 LOG_DEBUG(LOG_TAG, "%s cod is 0, set as unclassified", __func__);
1349 cod = COD_UNCLASSIFIED;
1350 }
1351
1352 if (!check_eir_remote_name(p_search_data, bdname.name, &remote_name_len))
1353 check_cached_remote_name(p_search_data, bdname.name, &remote_name_len);
1354
1355 /* Check EIR for remote name and services */
1356 if (p_search_data->inq_res.p_eir)
1357 {
1358 BTA_GetEirService(p_search_data->inq_res.p_eir, &services);
1359 BTIF_TRACE_DEBUG("%s()EIR BTA services = %08X", __FUNCTION__, (UINT32)services);
1360 /* TODO: Get the service list and check to see which uuids we got and send it back to the client. */
1361 }
1362
1363 {
1364 bt_property_t properties[5];
1365 bt_device_type_t dev_type;
1366 uint32_t num_properties = 0;
1367 bt_status_t status;
1368 int addr_type = 0;
1369
1370 memset(properties, 0, sizeof(properties));
1371 /* BD_ADDR */
1372 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1373 BT_PROPERTY_BDADDR, sizeof(bdaddr), &bdaddr);
1374 num_properties++;
1375 /* BD_NAME */
1376 /* Don't send BDNAME if it is empty */
1377 if (bdname.name[0])
1378 {
1379 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1380 BT_PROPERTY_BDNAME,
1381 strlen((char *)bdname.name), &bdname);
1382 num_properties++;
1383 }
1384
1385 /* DEV_CLASS */
1386 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1387 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
1388 num_properties++;
1389 /* DEV_TYPE */
1390 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1391 /* FixMe: Assumption is that bluetooth.h and BTE enums match */
1392
1393 /* Verify if the device is dual mode in NVRAM */
1394 int stored_device_type = 0;
1395 if (btif_get_device_type(bdaddr.address, &stored_device_type) &&
1396 ((stored_device_type != BT_DEVICE_TYPE_BREDR &&
1397 p_search_data->inq_res.device_type == BT_DEVICE_TYPE_BREDR) ||
1398 (stored_device_type != BT_DEVICE_TYPE_BLE &&
1399 p_search_data->inq_res.device_type == BT_DEVICE_TYPE_BLE))) {
1400 dev_type = BT_DEVICE_TYPE_DUMO;
1401 } else {
1402 dev_type = p_search_data->inq_res.device_type;
1403 }
1404
1405 if (p_search_data->inq_res.device_type == BT_DEVICE_TYPE_BLE)
1406 addr_type = p_search_data->inq_res.ble_addr_type;
1407 #else
1408 dev_type = BT_DEVICE_TYPE_BREDR;
1409 #endif
1410 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1411 BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type), &dev_type);
1412 num_properties++;
1413 /* RSSI */
1414 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1415 BT_PROPERTY_REMOTE_RSSI, sizeof(int8_t),
1416 &(p_search_data->inq_res.rssi));
1417 num_properties++;
1418
1419 status = btif_storage_add_remote_device(&bdaddr, num_properties, properties);
1420 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device (inquiry)", status);
1421 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1422 status = btif_storage_set_remote_addr_type(&bdaddr, addr_type);
1423 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote addr type (inquiry)", status);
1424 #endif
1425 /* Callback to notify upper layer of device */
1426 HAL_CBACK(bt_hal_cbacks, device_found_cb,
1427 num_properties, properties);
1428 }
1429 }
1430 break;
1431
1432 case BTA_DM_INQ_CMPL_EVT:
1433 {
1434 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1435 tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param;
1436 memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS));
1437 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, &adv_filt_param, NULL,
1438 bte_scan_filt_param_cfg_evt, 0);
1439 #endif
1440 }
1441 break;
1442 case BTA_DM_DISC_CMPL_EVT:
1443 {
1444 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STOPPED);
1445 }
1446 break;
1447 case BTA_DM_SEARCH_CANCEL_CMPL_EVT:
1448 {
1449 /* if inquiry is not in progress and we get a cancel event, then
1450 * it means we are done with inquiry, but remote_name fetches are in
1451 * progress
1452 *
1453 * if inquiry is in progress, then we don't want to act on this cancel_cmpl_evt
1454 * but instead wait for the cancel_cmpl_evt via the Busy Level
1455 *
1456 */
1457 if (btif_dm_inquiry_in_progress == FALSE)
1458 {
1459 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1460 tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param;
1461 memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS));
1462 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, &adv_filt_param, NULL,
1463 bte_scan_filt_param_cfg_evt, 0);
1464 #endif
1465 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STOPPED);
1466 }
1467 }
1468 break;
1469 }
1470 }
1471
1472 /*******************************************************************************
1473 **
1474 ** Function btif_dm_search_services_evt
1475 **
1476 ** Description Executes search services event in btif context
1477 **
1478 ** Returns void
1479 **
1480 *******************************************************************************/
btif_dm_search_services_evt(UINT16 event,char * p_param)1481 static void btif_dm_search_services_evt(UINT16 event, char *p_param)
1482 {
1483 tBTA_DM_SEARCH *p_data = (tBTA_DM_SEARCH*)p_param;
1484
1485 BTIF_TRACE_EVENT("%s: event = %d", __FUNCTION__, event);
1486 switch (event)
1487 {
1488 case BTA_DM_DISC_RES_EVT:
1489 {
1490 bt_property_t prop;
1491 uint32_t i = 0;
1492 bt_bdaddr_t bd_addr;
1493 bt_status_t ret;
1494
1495 bdcpy(bd_addr.address, p_data->disc_res.bd_addr);
1496
1497 BTIF_TRACE_DEBUG("%s:(result=0x%x, services 0x%x)", __FUNCTION__,
1498 p_data->disc_res.result, p_data->disc_res.services);
1499 if ((p_data->disc_res.result != BTA_SUCCESS) &&
1500 (pairing_cb.state == BT_BOND_STATE_BONDING ) &&
1501 (pairing_cb.sdp_attempts < BTIF_DM_MAX_SDP_ATTEMPTS_AFTER_PAIRING))
1502 {
1503 BTIF_TRACE_WARNING("%s:SDP failed after bonding re-attempting", __FUNCTION__);
1504 pairing_cb.sdp_attempts++;
1505 btif_dm_get_remote_services(&bd_addr);
1506 return;
1507 }
1508 prop.type = BT_PROPERTY_UUIDS;
1509 prop.len = 0;
1510 if ((p_data->disc_res.result == BTA_SUCCESS) && (p_data->disc_res.num_uuids > 0))
1511 {
1512 prop.val = p_data->disc_res.p_uuid_list;
1513 prop.len = p_data->disc_res.num_uuids * MAX_UUID_SIZE;
1514 for (i=0; i < p_data->disc_res.num_uuids; i++)
1515 {
1516 char temp[256];
1517 uuid_to_string_legacy((bt_uuid_t*)(p_data->disc_res.p_uuid_list + (i*MAX_UUID_SIZE)), temp);
1518 LOG_INFO(LOG_TAG, "%s index:%d uuid:%s", __func__, i, temp);
1519 }
1520 }
1521
1522 /* onUuidChanged requires getBondedDevices to be populated.
1523 ** bond_state_changed needs to be sent prior to remote_device_property
1524 */
1525 if ((pairing_cb.state == BT_BOND_STATE_BONDING) &&
1526 ((bdcmp(p_data->disc_res.bd_addr, pairing_cb.bd_addr) == 0) ||
1527 (bdcmp(p_data->disc_res.bd_addr, pairing_cb.static_bdaddr.address) == 0)) &&
1528 pairing_cb.sdp_attempts > 0)
1529 {
1530 BTIF_TRACE_DEBUG("%s Remote Service SDP done. Call bond_state_changed_cb BONDED",
1531 __FUNCTION__);
1532 pairing_cb.sdp_attempts = 0;
1533
1534 // If bonding occured due to cross-key pairing, send bonding callback
1535 // for static address now
1536 if (bdcmp(p_data->disc_res.bd_addr, pairing_cb.static_bdaddr.address) == 0)
1537 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
1538
1539 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDED);
1540 }
1541
1542 if (p_data->disc_res.num_uuids != 0)
1543 {
1544 /* Also write this to the NVRAM */
1545 ret = btif_storage_set_remote_device_property(&bd_addr, &prop);
1546 ASSERTC(ret == BT_STATUS_SUCCESS, "storing remote services failed", ret);
1547 /* Send the event to the BTIF */
1548 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1549 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1550 }
1551 }
1552 break;
1553
1554 case BTA_DM_DISC_CMPL_EVT:
1555 /* fixme */
1556 break;
1557
1558 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1559 case BTA_DM_DISC_BLE_RES_EVT:
1560 BTIF_TRACE_DEBUG("%s:, services 0x%x)", __FUNCTION__,
1561 p_data->disc_ble_res.service.uu.uuid16);
1562 bt_uuid_t uuid;
1563 int i = 0;
1564 int j = 15;
1565 if (p_data->disc_ble_res.service.uu.uuid16 == UUID_SERVCLASS_LE_HID)
1566 {
1567 BTIF_TRACE_DEBUG("%s: Found HOGP UUID",__FUNCTION__);
1568 bt_property_t prop;
1569 bt_bdaddr_t bd_addr;
1570 char temp[256];
1571 bt_status_t ret;
1572
1573 bta_gatt_convert_uuid16_to_uuid128(uuid.uu,p_data->disc_ble_res.service.uu.uuid16);
1574
1575 while(i < j )
1576 {
1577 unsigned char c = uuid.uu[j];
1578 uuid.uu[j] = uuid.uu[i];
1579 uuid.uu[i] = c;
1580 i++;
1581 j--;
1582 }
1583
1584 uuid_to_string_legacy(&uuid, temp);
1585 LOG_INFO(LOG_TAG, "%s uuid:%s", __func__, temp);
1586
1587 bdcpy(bd_addr.address, p_data->disc_ble_res.bd_addr);
1588 prop.type = BT_PROPERTY_UUIDS;
1589 prop.val = uuid.uu;
1590 prop.len = MAX_UUID_SIZE;
1591
1592 /* Also write this to the NVRAM */
1593 ret = btif_storage_set_remote_device_property(&bd_addr, &prop);
1594 ASSERTC(ret == BT_STATUS_SUCCESS, "storing remote services failed", ret);
1595
1596 /* Send the event to the BTIF */
1597 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1598 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1599
1600 }
1601 break;
1602 #endif /* BLE_INCLUDED */
1603
1604 default:
1605 {
1606 ASSERTC(0, "unhandled search services event", event);
1607 }
1608 break;
1609 }
1610 }
1611
1612 /*******************************************************************************
1613 **
1614 ** Function btif_dm_remote_service_record_evt
1615 **
1616 ** Description Executes search service record event in btif context
1617 **
1618 ** Returns void
1619 **
1620 *******************************************************************************/
btif_dm_remote_service_record_evt(UINT16 event,char * p_param)1621 static void btif_dm_remote_service_record_evt(UINT16 event, char *p_param)
1622 {
1623 tBTA_DM_SEARCH *p_data = (tBTA_DM_SEARCH*)p_param;
1624
1625 BTIF_TRACE_EVENT("%s: event = %d", __FUNCTION__, event);
1626 switch (event)
1627 {
1628 case BTA_DM_DISC_RES_EVT:
1629 {
1630 bt_service_record_t rec;
1631 bt_property_t prop;
1632 bt_bdaddr_t bd_addr;
1633
1634 memset(&rec, 0, sizeof(bt_service_record_t));
1635 bdcpy(bd_addr.address, p_data->disc_res.bd_addr);
1636
1637 BTIF_TRACE_DEBUG("%s:(result=0x%x, services 0x%x)", __FUNCTION__,
1638 p_data->disc_res.result, p_data->disc_res.services);
1639 prop.type = BT_PROPERTY_SERVICE_RECORD;
1640 prop.val = (void*)&rec;
1641 prop.len = sizeof(rec);
1642
1643 /* disc_res.result is overloaded with SCN. Cannot check result */
1644 p_data->disc_res.services &= ~BTA_USER_SERVICE_MASK;
1645 /* TODO: Get the UUID as well */
1646 rec.channel = p_data->disc_res.result - 3;
1647 /* TODO: Need to get the service name using p_raw_data */
1648 rec.name[0] = 0;
1649
1650 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1651 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1652 }
1653 break;
1654
1655 default:
1656 {
1657 ASSERTC(0, "unhandled remote service record event", event);
1658 }
1659 break;
1660 }
1661 }
1662
1663 /*******************************************************************************
1664 **
1665 ** Function btif_dm_upstreams_cback
1666 **
1667 ** Description Executes UPSTREAMS events in btif context
1668 **
1669 ** Returns void
1670 **
1671 *******************************************************************************/
btif_dm_upstreams_evt(UINT16 event,char * p_param)1672 static void btif_dm_upstreams_evt(UINT16 event, char* p_param)
1673 {
1674 tBTA_DM_SEC *p_data = (tBTA_DM_SEC*)p_param;
1675 tBTA_SERVICE_MASK service_mask;
1676 uint32_t i;
1677 bt_bdaddr_t bd_addr;
1678
1679 BTIF_TRACE_EVENT("btif_dm_upstreams_cback ev: %s", dump_dm_event(event));
1680
1681 switch (event)
1682 {
1683 case BTA_DM_ENABLE_EVT:
1684 {
1685 BD_NAME bdname;
1686 bt_status_t status;
1687 bt_property_t prop;
1688 prop.type = BT_PROPERTY_BDNAME;
1689 prop.len = BD_NAME_LEN;
1690 prop.val = (void*)bdname;
1691
1692 status = btif_storage_get_adapter_property(&prop);
1693 if (status == BT_STATUS_SUCCESS)
1694 {
1695 /* A name exists in the storage. Make this the device name */
1696 BTA_DmSetDeviceName((char*)prop.val);
1697 }
1698 else
1699 {
1700 /* Storage does not have a name yet.
1701 * Use the default name and write it to the chip
1702 */
1703 BTA_DmSetDeviceName(btif_get_default_local_name());
1704 }
1705
1706 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1707 /* Enable local privacy */
1708 BTA_DmBleConfigLocalPrivacy(BLE_LOCAL_PRIVACY_ENABLED);
1709 #endif
1710
1711 /* for each of the enabled services in the mask, trigger the profile
1712 * enable */
1713 service_mask = btif_get_enabled_services_mask();
1714 for (i=0; i <= BTA_MAX_SERVICE_ID; i++)
1715 {
1716 if (service_mask &
1717 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i)))
1718 {
1719 btif_in_execute_service_request(i, TRUE);
1720 }
1721 }
1722 /* clear control blocks */
1723 memset(&pairing_cb, 0, sizeof(btif_dm_pairing_cb_t));
1724 pairing_cb.bond_type = BOND_TYPE_PERSISTENT;
1725
1726 /* This function will also trigger the adapter_properties_cb
1727 ** and bonded_devices_info_cb
1728 */
1729 btif_storage_load_bonded_devices();
1730
1731 btif_enable_bluetooth_evt(p_data->enable.status);
1732 }
1733 break;
1734
1735 case BTA_DM_DISABLE_EVT:
1736 /* for each of the enabled services in the mask, trigger the profile
1737 * disable */
1738 service_mask = btif_get_enabled_services_mask();
1739 for (i=0; i <= BTA_MAX_SERVICE_ID; i++)
1740 {
1741 if (service_mask &
1742 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i)))
1743 {
1744 btif_in_execute_service_request(i, FALSE);
1745 }
1746 }
1747 btif_disable_bluetooth_evt();
1748 break;
1749
1750 case BTA_DM_PIN_REQ_EVT:
1751 btif_dm_pin_req_evt(&p_data->pin_req);
1752 break;
1753
1754 case BTA_DM_AUTH_CMPL_EVT:
1755 btif_dm_auth_cmpl_evt(&p_data->auth_cmpl);
1756 break;
1757
1758 case BTA_DM_BOND_CANCEL_CMPL_EVT:
1759 if (pairing_cb.state == BT_BOND_STATE_BONDING)
1760 {
1761 bdcpy(bd_addr.address, pairing_cb.bd_addr);
1762 btm_set_bond_type_dev(pairing_cb.bd_addr, BOND_TYPE_UNKNOWN);
1763 bond_state_changed(p_data->bond_cancel_cmpl.result, &bd_addr, BT_BOND_STATE_NONE);
1764 }
1765 break;
1766
1767 case BTA_DM_SP_CFM_REQ_EVT:
1768 btif_dm_ssp_cfm_req_evt(&p_data->cfm_req);
1769 break;
1770 case BTA_DM_SP_KEY_NOTIF_EVT:
1771 btif_dm_ssp_key_notif_evt(&p_data->key_notif);
1772 break;
1773
1774 case BTA_DM_DEV_UNPAIRED_EVT:
1775 bdcpy(bd_addr.address, p_data->link_down.bd_addr);
1776 btm_set_bond_type_dev(p_data->link_down.bd_addr, BOND_TYPE_UNKNOWN);
1777
1778 /*special handling for HID devices */
1779 #if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE))
1780 btif_hh_remove_device(bd_addr);
1781 #endif
1782 btif_storage_remove_bonded_device(&bd_addr);
1783 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE);
1784 break;
1785
1786 case BTA_DM_BUSY_LEVEL_EVT:
1787 {
1788
1789 if (p_data->busy_level.level_flags & BTM_BL_INQUIRY_PAGING_MASK)
1790 {
1791 if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_STARTED)
1792 {
1793 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
1794 BT_DISCOVERY_STARTED);
1795 btif_dm_inquiry_in_progress = TRUE;
1796 }
1797 else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_CANCELLED)
1798 {
1799 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
1800 BT_DISCOVERY_STOPPED);
1801 btif_dm_inquiry_in_progress = FALSE;
1802 }
1803 else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_COMPLETE)
1804 {
1805 btif_dm_inquiry_in_progress = FALSE;
1806 }
1807 }
1808 }break;
1809
1810 case BTA_DM_LINK_UP_EVT:
1811 bdcpy(bd_addr.address, p_data->link_up.bd_addr);
1812 BTIF_TRACE_DEBUG("BTA_DM_LINK_UP_EVT. Sending BT_ACL_STATE_CONNECTED");
1813
1814 btif_update_remote_version_property(&bd_addr);
1815
1816 HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS,
1817 &bd_addr, BT_ACL_STATE_CONNECTED);
1818 break;
1819
1820 case BTA_DM_LINK_DOWN_EVT:
1821 bdcpy(bd_addr.address, p_data->link_down.bd_addr);
1822 btm_set_bond_type_dev(p_data->link_down.bd_addr, BOND_TYPE_UNKNOWN);
1823 BTIF_TRACE_DEBUG("BTA_DM_LINK_DOWN_EVT. Sending BT_ACL_STATE_DISCONNECTED");
1824 HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS,
1825 &bd_addr, BT_ACL_STATE_DISCONNECTED);
1826 break;
1827
1828 case BTA_DM_HW_ERROR_EVT:
1829 BTIF_TRACE_ERROR("Received H/W Error. ");
1830 /* Flush storage data */
1831 btif_config_flush();
1832 usleep(100000); /* 100milliseconds */
1833 /* Killing the process to force a restart as part of fault tolerance */
1834 kill(getpid(), SIGKILL);
1835 break;
1836
1837 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1838 case BTA_DM_BLE_KEY_EVT:
1839 BTIF_TRACE_DEBUG("BTA_DM_BLE_KEY_EVT key_type=0x%02x ", p_data->ble_key.key_type);
1840
1841 /* If this pairing is by-product of local initiated GATT client Read or Write,
1842 BTA would not have sent BTA_DM_BLE_SEC_REQ_EVT event and Bond state would not
1843 have setup properly. Setup pairing_cb and notify App about Bonding state now*/
1844 if (pairing_cb.state != BT_BOND_STATE_BONDING)
1845 {
1846 BTIF_TRACE_DEBUG("Bond state not sent to App so far.Notify the app now");
1847 bond_state_changed(BT_STATUS_SUCCESS, (bt_bdaddr_t*)p_data->ble_key.bd_addr,
1848 BT_BOND_STATE_BONDING);
1849 }
1850 else if (memcmp (pairing_cb.bd_addr, p_data->ble_key.bd_addr, BD_ADDR_LEN)!=0)
1851 {
1852 BTIF_TRACE_ERROR("BD mismatch discard BLE key_type=%d ",p_data->ble_key.key_type);
1853 break;
1854 }
1855
1856 switch (p_data->ble_key.key_type)
1857 {
1858 case BTA_LE_KEY_PENC:
1859 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PENC");
1860 pairing_cb.ble.is_penc_key_rcvd = TRUE;
1861 pairing_cb.ble.penc_key = p_data->ble_key.p_key_value->penc_key;
1862 break;
1863
1864 case BTA_LE_KEY_PID:
1865 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PID");
1866 pairing_cb.ble.is_pid_key_rcvd = TRUE;
1867 pairing_cb.ble.pid_key = p_data->ble_key.p_key_value->pid_key;
1868 break;
1869
1870 case BTA_LE_KEY_PCSRK:
1871 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PCSRK");
1872 pairing_cb.ble.is_pcsrk_key_rcvd = TRUE;
1873 pairing_cb.ble.pcsrk_key = p_data->ble_key.p_key_value->pcsrk_key;
1874 break;
1875
1876 case BTA_LE_KEY_LENC:
1877 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LENC");
1878 pairing_cb.ble.is_lenc_key_rcvd = TRUE;
1879 pairing_cb.ble.lenc_key = p_data->ble_key.p_key_value->lenc_key;
1880 break;
1881
1882 case BTA_LE_KEY_LCSRK:
1883 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LCSRK");
1884 pairing_cb.ble.is_lcsrk_key_rcvd = TRUE;
1885 pairing_cb.ble.lcsrk_key = p_data->ble_key.p_key_value->lcsrk_key;
1886 break;
1887
1888 case BTA_LE_KEY_LID:
1889 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LID");
1890 pairing_cb.ble.is_lidk_key_rcvd = TRUE;
1891 break;
1892
1893 default:
1894 BTIF_TRACE_ERROR("unknown BLE key type (0x%02x)", p_data->ble_key.key_type);
1895 break;
1896 }
1897 break;
1898 case BTA_DM_BLE_SEC_REQ_EVT:
1899 BTIF_TRACE_DEBUG("BTA_DM_BLE_SEC_REQ_EVT. ");
1900 btif_dm_ble_sec_req_evt(&p_data->ble_req);
1901 break;
1902 case BTA_DM_BLE_PASSKEY_NOTIF_EVT:
1903 BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_NOTIF_EVT. ");
1904 btif_dm_ble_key_notif_evt(&p_data->key_notif);
1905 break;
1906 case BTA_DM_BLE_PASSKEY_REQ_EVT:
1907 BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_REQ_EVT. ");
1908 btif_dm_ble_passkey_req_evt(&p_data->pin_req);
1909 break;
1910 case BTA_DM_BLE_NC_REQ_EVT:
1911 BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_REQ_EVT. ");
1912 btif_dm_ble_key_nc_req_evt(&p_data->key_notif);
1913 break;
1914 case BTA_DM_BLE_OOB_REQ_EVT:
1915 BTIF_TRACE_DEBUG("BTA_DM_BLE_OOB_REQ_EVT. ");
1916 btif_dm_ble_oob_req_evt(&p_data->rmt_oob);
1917 break;
1918 case BTA_DM_BLE_LOCAL_IR_EVT:
1919 BTIF_TRACE_DEBUG("BTA_DM_BLE_LOCAL_IR_EVT. ");
1920 ble_local_key_cb.is_id_keys_rcvd = TRUE;
1921 memcpy(&ble_local_key_cb.id_keys.irk[0],
1922 &p_data->ble_id_keys.irk[0], sizeof(BT_OCTET16));
1923 memcpy(&ble_local_key_cb.id_keys.ir[0],
1924 &p_data->ble_id_keys.ir[0], sizeof(BT_OCTET16));
1925 memcpy(&ble_local_key_cb.id_keys.dhk[0],
1926 &p_data->ble_id_keys.dhk[0], sizeof(BT_OCTET16));
1927 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.irk[0],
1928 BTIF_DM_LE_LOCAL_KEY_IRK,
1929 BT_OCTET16_LEN);
1930 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.ir[0],
1931 BTIF_DM_LE_LOCAL_KEY_IR,
1932 BT_OCTET16_LEN);
1933 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.dhk[0],
1934 BTIF_DM_LE_LOCAL_KEY_DHK,
1935 BT_OCTET16_LEN);
1936 break;
1937 case BTA_DM_BLE_LOCAL_ER_EVT:
1938 BTIF_TRACE_DEBUG("BTA_DM_BLE_LOCAL_ER_EVT. ");
1939 ble_local_key_cb.is_er_rcvd = TRUE;
1940 memcpy(&ble_local_key_cb.er[0], &p_data->ble_er[0], sizeof(BT_OCTET16));
1941 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.er[0],
1942 BTIF_DM_LE_LOCAL_KEY_ER,
1943 BT_OCTET16_LEN);
1944 break;
1945
1946 case BTA_DM_BLE_AUTH_CMPL_EVT:
1947 BTIF_TRACE_DEBUG("BTA_DM_BLE_AUTH_CMPL_EVT. ");
1948 btif_dm_ble_auth_cmpl_evt(&p_data->auth_cmpl);
1949 break;
1950
1951 case BTA_DM_LE_FEATURES_READ:
1952 {
1953 tBTM_BLE_VSC_CB cmn_vsc_cb;
1954 bt_local_le_features_t local_le_features;
1955 char buf[512];
1956 bt_property_t prop;
1957 prop.type = BT_PROPERTY_LOCAL_LE_FEATURES;
1958 prop.val = (void*)buf;
1959 prop.len = sizeof(buf);
1960
1961 /* LE features are not stored in storage. Should be retrived from stack */
1962 BTM_BleGetVendorCapabilities(&cmn_vsc_cb);
1963 local_le_features.local_privacy_enabled = BTM_BleLocalPrivacyEnabled();
1964
1965 prop.len = sizeof (bt_local_le_features_t);
1966 if (cmn_vsc_cb.filter_support == 1)
1967 local_le_features.max_adv_filter_supported = cmn_vsc_cb.max_filter;
1968 else
1969 local_le_features.max_adv_filter_supported = 0;
1970 local_le_features.max_adv_instance = cmn_vsc_cb.adv_inst_max;
1971 local_le_features.max_irk_list_size = cmn_vsc_cb.max_irk_list_sz;
1972 local_le_features.rpa_offload_supported = cmn_vsc_cb.rpa_offloading;
1973 local_le_features.activity_energy_info_supported = cmn_vsc_cb.energy_support;
1974 local_le_features.scan_result_storage_size = cmn_vsc_cb.tot_scan_results_strg;
1975 local_le_features.version_supported = cmn_vsc_cb.version_supported;
1976 local_le_features.total_trackable_advertisers =
1977 cmn_vsc_cb.total_trackable_advertisers;
1978
1979 local_le_features.extended_scan_support = cmn_vsc_cb.extended_scan_support > 0;
1980 local_le_features.debug_logging_supported = cmn_vsc_cb.debug_logging_supported > 0;
1981
1982 memcpy(prop.val, &local_le_features, prop.len);
1983 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, BT_STATUS_SUCCESS, 1, &prop);
1984 break;
1985 }
1986
1987 case BTA_DM_ENER_INFO_READ:
1988 {
1989 btif_activity_energy_info_cb_t *p_ener_data = (btif_activity_energy_info_cb_t*) p_param;
1990 bt_activity_energy_info energy_info;
1991 energy_info.status = p_ener_data->status;
1992 energy_info.ctrl_state = p_ener_data->ctrl_state;
1993 energy_info.rx_time = p_ener_data->rx_time;
1994 energy_info.tx_time = p_ener_data->tx_time;
1995 energy_info.idle_time = p_ener_data->idle_time;
1996 energy_info.energy_used = p_ener_data->energy_used;
1997
1998 bt_uid_traffic_t* data = uid_set_read_and_clear(uid_set);
1999 HAL_CBACK(bt_hal_cbacks, energy_info_cb, &energy_info, data);
2000 osi_free(data);
2001 break;
2002 }
2003 #endif
2004
2005 case BTA_DM_AUTHORIZE_EVT:
2006 case BTA_DM_SIG_STRENGTH_EVT:
2007 case BTA_DM_SP_RMT_OOB_EVT:
2008 case BTA_DM_SP_KEYPRESS_EVT:
2009 case BTA_DM_ROLE_CHG_EVT:
2010
2011 default:
2012 BTIF_TRACE_WARNING( "btif_dm_cback : unhandled event (%d)", event );
2013 break;
2014 }
2015
2016 btif_dm_data_free(event, p_data);
2017 }
2018
2019 /*******************************************************************************
2020 **
2021 ** Function btif_dm_generic_evt
2022 **
2023 ** Description Executes non-BTA upstream events in BTIF context
2024 **
2025 ** Returns void
2026 **
2027 *******************************************************************************/
btif_dm_generic_evt(UINT16 event,char * p_param)2028 static void btif_dm_generic_evt(UINT16 event, char* p_param)
2029 {
2030 BTIF_TRACE_EVENT("%s: event=%d", __FUNCTION__, event);
2031 switch(event)
2032 {
2033 case BTIF_DM_CB_DISCOVERY_STARTED:
2034 {
2035 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STARTED);
2036 }
2037 break;
2038
2039 case BTIF_DM_CB_CREATE_BOND:
2040 {
2041 pairing_cb.timeout_retries = NUM_TIMEOUT_RETRIES;
2042 btif_dm_create_bond_cb_t *create_bond_cb = (btif_dm_create_bond_cb_t*)p_param;
2043 btif_dm_cb_create_bond(&create_bond_cb->bdaddr, create_bond_cb->transport);
2044 }
2045 break;
2046
2047 case BTIF_DM_CB_REMOVE_BOND:
2048 {
2049 btif_dm_cb_remove_bond((bt_bdaddr_t *)p_param);
2050 }
2051 break;
2052
2053 case BTIF_DM_CB_HID_REMOTE_NAME:
2054 {
2055 btif_dm_cb_hid_remote_name((tBTM_REMOTE_DEV_NAME *)p_param);
2056 }
2057 break;
2058
2059 case BTIF_DM_CB_BOND_STATE_BONDING:
2060 {
2061 bond_state_changed(BT_STATUS_SUCCESS, (bt_bdaddr_t *)p_param, BT_BOND_STATE_BONDING);
2062 }
2063 break;
2064 case BTIF_DM_CB_LE_TX_TEST:
2065 case BTIF_DM_CB_LE_RX_TEST:
2066 {
2067 uint8_t status;
2068 STREAM_TO_UINT8(status, p_param);
2069 HAL_CBACK(bt_hal_cbacks, le_test_mode_cb,
2070 (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, 0);
2071 }
2072 break;
2073 case BTIF_DM_CB_LE_TEST_END:
2074 {
2075 uint8_t status;
2076 uint16_t count = 0;
2077 STREAM_TO_UINT8(status, p_param);
2078 if (status == 0)
2079 STREAM_TO_UINT16(count, p_param);
2080 HAL_CBACK(bt_hal_cbacks, le_test_mode_cb,
2081 (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, count);
2082 }
2083 break;
2084 default:
2085 {
2086 BTIF_TRACE_WARNING("%s : Unknown event 0x%x", __FUNCTION__, event);
2087 }
2088 break;
2089 }
2090 }
2091
2092 /*******************************************************************************
2093 **
2094 ** Function bte_dm_evt
2095 **
2096 ** Description Switches context from BTE to BTIF for all DM events
2097 **
2098 ** Returns void
2099 **
2100 *******************************************************************************/
2101
bte_dm_evt(tBTA_DM_SEC_EVT event,tBTA_DM_SEC * p_data)2102 void bte_dm_evt(tBTA_DM_SEC_EVT event, tBTA_DM_SEC *p_data)
2103 {
2104 /* switch context to btif task context (copy full union size for convenience) */
2105 bt_status_t status = btif_transfer_context(btif_dm_upstreams_evt, (uint16_t)event,
2106 (void*)p_data, sizeof(tBTA_DM_SEC), btif_dm_data_copy);
2107
2108 /* catch any failed context transfers */
2109 ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status);
2110 }
2111
2112 /*******************************************************************************
2113 **
2114 ** Function bte_search_devices_evt
2115 **
2116 ** Description Switches context from BTE to BTIF for DM search events
2117 **
2118 ** Returns void
2119 **
2120 *******************************************************************************/
bte_search_devices_evt(tBTA_DM_SEARCH_EVT event,tBTA_DM_SEARCH * p_data)2121 static void bte_search_devices_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
2122 {
2123 UINT16 param_len = 0;
2124
2125 if (p_data)
2126 param_len += sizeof(tBTA_DM_SEARCH);
2127 /* Allocate buffer to hold the pointers (deep copy). The pointers will point to the end of the tBTA_DM_SEARCH */
2128 switch (event)
2129 {
2130 case BTA_DM_INQ_RES_EVT:
2131 {
2132 if (p_data->inq_res.p_eir)
2133 param_len += HCI_EXT_INQ_RESPONSE_LEN;
2134 }
2135 break;
2136
2137 case BTA_DM_DISC_RES_EVT:
2138 {
2139 if (p_data->disc_res.raw_data_size && p_data->disc_res.p_raw_data)
2140 param_len += p_data->disc_res.raw_data_size;
2141 }
2142 break;
2143 }
2144 BTIF_TRACE_DEBUG("%s event=%s param_len=%d", __FUNCTION__, dump_dm_search_event(event), param_len);
2145
2146 /* if remote name is available in EIR, set teh flag so that stack doesnt trigger RNR */
2147 if (event == BTA_DM_INQ_RES_EVT)
2148 p_data->inq_res.remt_name_not_required = check_eir_remote_name(p_data, NULL, NULL);
2149
2150 btif_transfer_context (btif_dm_search_devices_evt , (UINT16) event, (void *)p_data, param_len,
2151 (param_len > sizeof(tBTA_DM_SEARCH)) ? search_devices_copy_cb : NULL);
2152 }
2153
2154 /*******************************************************************************
2155 **
2156 ** Function bte_dm_search_services_evt
2157 **
2158 ** Description Switches context from BTE to BTIF for DM search services
2159 ** event
2160 **
2161 ** Returns void
2162 **
2163 *******************************************************************************/
bte_dm_search_services_evt(tBTA_DM_SEARCH_EVT event,tBTA_DM_SEARCH * p_data)2164 static void bte_dm_search_services_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
2165 {
2166 UINT16 param_len = 0;
2167 if (p_data)
2168 param_len += sizeof(tBTA_DM_SEARCH);
2169 switch (event)
2170 {
2171 case BTA_DM_DISC_RES_EVT:
2172 {
2173 if ((p_data->disc_res.result == BTA_SUCCESS) && (p_data->disc_res.num_uuids > 0)) {
2174 param_len += (p_data->disc_res.num_uuids * MAX_UUID_SIZE);
2175 }
2176 } break;
2177 }
2178 /* TODO: The only other member that needs a deep copy is the p_raw_data. But not sure
2179 * if raw_data is needed. */
2180 btif_transfer_context(btif_dm_search_services_evt, event, (char*)p_data, param_len,
2181 (param_len > sizeof(tBTA_DM_SEARCH)) ? search_services_copy_cb : NULL);
2182 }
2183
2184 /*******************************************************************************
2185 **
2186 ** Function bte_dm_remote_service_record_evt
2187 **
2188 ** Description Switches context from BTE to BTIF for DM search service
2189 ** record event
2190 **
2191 ** Returns void
2192 **
2193 *******************************************************************************/
bte_dm_remote_service_record_evt(tBTA_DM_SEARCH_EVT event,tBTA_DM_SEARCH * p_data)2194 static void bte_dm_remote_service_record_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
2195 {
2196 /* TODO: The only member that needs a deep copy is the p_raw_data. But not sure yet if this is needed. */
2197 btif_transfer_context(btif_dm_remote_service_record_evt, event, (char*)p_data, sizeof(tBTA_DM_SEARCH), NULL);
2198 }
2199
2200 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2201 /*******************************************************************************
2202 **
2203 ** Function bta_energy_info_cb
2204 **
2205 ** Description Switches context from BTE to BTIF for DM energy info event
2206 **
2207 ** Returns void
2208 **
2209 *******************************************************************************/
bta_energy_info_cb(tBTA_DM_BLE_TX_TIME_MS tx_time,tBTA_DM_BLE_RX_TIME_MS rx_time,tBTA_DM_BLE_IDLE_TIME_MS idle_time,tBTA_DM_BLE_ENERGY_USED energy_used,tBTA_DM_CONTRL_STATE ctrl_state,tBTA_STATUS status)2210 static void bta_energy_info_cb(tBTA_DM_BLE_TX_TIME_MS tx_time, tBTA_DM_BLE_RX_TIME_MS rx_time,
2211 tBTA_DM_BLE_IDLE_TIME_MS idle_time,
2212 tBTA_DM_BLE_ENERGY_USED energy_used,
2213 tBTA_DM_CONTRL_STATE ctrl_state, tBTA_STATUS status)
2214 {
2215 BTIF_TRACE_DEBUG("energy_info_cb-Status:%d,state=%d,tx_t=%ld, rx_t=%ld, idle_time=%ld,used=%ld",
2216 status, ctrl_state, tx_time, rx_time, idle_time, energy_used);
2217
2218 btif_activity_energy_info_cb_t btif_cb;
2219 btif_cb.status = status;
2220 btif_cb.ctrl_state = ctrl_state;
2221 btif_cb.tx_time = (uint64_t) tx_time;
2222 btif_cb.rx_time = (uint64_t) rx_time;
2223 btif_cb.idle_time =(uint64_t) idle_time;
2224 btif_cb.energy_used =(uint64_t) energy_used;
2225 btif_transfer_context(btif_dm_upstreams_evt, BTA_DM_ENER_INFO_READ,
2226 (char*) &btif_cb, sizeof(btif_activity_energy_info_cb_t), NULL);
2227 }
2228 #endif
2229
2230 /*******************************************************************************
2231 **
2232 ** Function bte_scan_filt_param_cfg_evt
2233 **
2234 ** Description Scan filter param config event
2235 **
2236 ** Returns void
2237 **
2238 *******************************************************************************/
bte_scan_filt_param_cfg_evt(UINT8 action_type,tBTA_DM_BLE_PF_AVBL_SPACE avbl_space,tBTA_DM_BLE_REF_VALUE ref_value,tBTA_STATUS status)2239 static void bte_scan_filt_param_cfg_evt(UINT8 action_type,
2240 tBTA_DM_BLE_PF_AVBL_SPACE avbl_space,
2241 tBTA_DM_BLE_REF_VALUE ref_value, tBTA_STATUS status)
2242 {
2243 /* This event occurs on calling BTA_DmBleCfgFilterCondition internally,
2244 ** and that is why there is no HAL callback
2245 */
2246 if(BTA_SUCCESS != status)
2247 {
2248 BTIF_TRACE_ERROR("%s, %d", __FUNCTION__, status);
2249 }
2250 else
2251 {
2252 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
2253 }
2254 }
2255
2256 /*****************************************************************************
2257 **
2258 ** btif api functions (no context switch)
2259 **
2260 *****************************************************************************/
2261
2262 /*******************************************************************************
2263 **
2264 ** Function btif_dm_start_discovery
2265 **
2266 ** Description Start device discovery/inquiry
2267 **
2268 ** Returns bt_status_t
2269 **
2270 *******************************************************************************/
btif_dm_start_discovery(void)2271 bt_status_t btif_dm_start_discovery(void)
2272 {
2273 tBTA_DM_INQ inq_params;
2274 tBTA_SERVICE_MASK services = 0;
2275 tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param;
2276
2277 BTIF_TRACE_EVENT("%s", __FUNCTION__);
2278
2279 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2280 memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS));
2281 /* Cleanup anything remaining on index 0 */
2282 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, &adv_filt_param, NULL,
2283 bte_scan_filt_param_cfg_evt, 0);
2284
2285 /* Add an allow-all filter on index 0*/
2286 adv_filt_param.dely_mode = IMMEDIATE_DELY_MODE;
2287 adv_filt_param.feat_seln = ALLOW_ALL_FILTER;
2288 adv_filt_param.filt_logic_type = BTA_DM_BLE_PF_FILT_LOGIC_OR;
2289 adv_filt_param.list_logic_type = BTA_DM_BLE_PF_LIST_LOGIC_OR;
2290 adv_filt_param.rssi_low_thres = LOWEST_RSSI_VALUE;
2291 adv_filt_param.rssi_high_thres = LOWEST_RSSI_VALUE;
2292 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_ADD, 0, &adv_filt_param, NULL,
2293 bte_scan_filt_param_cfg_evt, 0);
2294
2295 /* TODO: Do we need to handle multiple inquiries at the same time? */
2296
2297 /* Set inquiry params and call API */
2298 inq_params.mode = BTA_DM_GENERAL_INQUIRY|BTA_BLE_GENERAL_INQUIRY;
2299 #if (defined(BTA_HOST_INTERLEAVE_SEARCH) && BTA_HOST_INTERLEAVE_SEARCH == TRUE)
2300 inq_params.intl_duration[0]= BTIF_DM_INTERLEAVE_DURATION_BR_ONE;
2301 inq_params.intl_duration[1]= BTIF_DM_INTERLEAVE_DURATION_LE_ONE;
2302 inq_params.intl_duration[2]= BTIF_DM_INTERLEAVE_DURATION_BR_TWO;
2303 inq_params.intl_duration[3]= BTIF_DM_INTERLEAVE_DURATION_LE_TWO;
2304 #endif
2305 #else
2306 inq_params.mode = BTA_DM_GENERAL_INQUIRY;
2307 #endif
2308 inq_params.duration = BTIF_DM_DEFAULT_INQ_MAX_DURATION;
2309
2310 inq_params.max_resps = BTIF_DM_DEFAULT_INQ_MAX_RESULTS;
2311 inq_params.report_dup = TRUE;
2312
2313 inq_params.filter_type = BTA_DM_INQ_CLR;
2314 /* TODO: Filter device by BDA needs to be implemented here */
2315
2316 /* Will be enabled to TRUE once inquiry busy level has been received */
2317 btif_dm_inquiry_in_progress = FALSE;
2318 /* find nearby devices */
2319 BTA_DmSearch(&inq_params, services, bte_search_devices_evt);
2320
2321 return BT_STATUS_SUCCESS;
2322 }
2323
2324 /*******************************************************************************
2325 **
2326 ** Function btif_dm_cancel_discovery
2327 **
2328 ** Description Cancels search
2329 **
2330 ** Returns bt_status_t
2331 **
2332 *******************************************************************************/
btif_dm_cancel_discovery(void)2333 bt_status_t btif_dm_cancel_discovery(void)
2334 {
2335 BTIF_TRACE_EVENT("%s", __FUNCTION__);
2336 BTA_DmSearchCancel();
2337 return BT_STATUS_SUCCESS;
2338 }
2339
2340 /*******************************************************************************
2341 **
2342 ** Function btif_dm_create_bond
2343 **
2344 ** Description Initiate bonding with the specified device
2345 **
2346 ** Returns bt_status_t
2347 **
2348 *******************************************************************************/
btif_dm_create_bond(const bt_bdaddr_t * bd_addr,int transport)2349 bt_status_t btif_dm_create_bond(const bt_bdaddr_t *bd_addr, int transport)
2350 {
2351 btif_dm_create_bond_cb_t create_bond_cb;
2352 create_bond_cb.transport = transport;
2353 bdcpy(create_bond_cb.bdaddr.address, bd_addr->address);
2354
2355 bdstr_t bdstr;
2356 BTIF_TRACE_EVENT("%s: bd_addr=%s, transport=%d", __FUNCTION__, bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr)), transport);
2357 if (pairing_cb.state != BT_BOND_STATE_NONE)
2358 return BT_STATUS_BUSY;
2359
2360 btif_stats_add_bond_event(bd_addr, BTIF_DM_FUNC_CREATE_BOND, pairing_cb.state);
2361
2362 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_CREATE_BOND,
2363 (char *)&create_bond_cb, sizeof(btif_dm_create_bond_cb_t), NULL);
2364
2365 return BT_STATUS_SUCCESS;
2366 }
2367
2368 /*******************************************************************************
2369 **
2370 ** Function btif_dm_create_bond_out_of_band
2371 **
2372 ** Description Initiate bonding with the specified device using out of band data
2373 **
2374 ** Returns bt_status_t
2375 **
2376 *******************************************************************************/
btif_dm_create_bond_out_of_band(const bt_bdaddr_t * bd_addr,int transport,const bt_out_of_band_data_t * oob_data)2377 bt_status_t btif_dm_create_bond_out_of_band(const bt_bdaddr_t *bd_addr, int transport, const bt_out_of_band_data_t *oob_data)
2378 {
2379 bdcpy(oob_cb.bdaddr, bd_addr->address);
2380 memcpy(&oob_cb.oob_data, oob_data, sizeof(bt_out_of_band_data_t));
2381
2382 bdstr_t bdstr;
2383 BTIF_TRACE_EVENT("%s: bd_addr=%s, transport=%d", __FUNCTION__, bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr)), transport);
2384 return btif_dm_create_bond(bd_addr, transport);
2385 }
2386
2387 /*******************************************************************************
2388 **
2389 ** Function btif_dm_cancel_bond
2390 **
2391 ** Description Initiate bonding with the specified device
2392 **
2393 ** Returns bt_status_t
2394 **
2395 *******************************************************************************/
2396
btif_dm_cancel_bond(const bt_bdaddr_t * bd_addr)2397 bt_status_t btif_dm_cancel_bond(const bt_bdaddr_t *bd_addr)
2398 {
2399 bdstr_t bdstr;
2400
2401 BTIF_TRACE_EVENT("%s: bd_addr=%s", __FUNCTION__, bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr)));
2402
2403 btif_stats_add_bond_event(bd_addr, BTIF_DM_FUNC_CANCEL_BOND, pairing_cb.state);
2404
2405 /* TODO:
2406 ** 1. Restore scan modes
2407 ** 2. special handling for HID devices
2408 */
2409 if (pairing_cb.state == BT_BOND_STATE_BONDING)
2410 {
2411
2412 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2413
2414 if (pairing_cb.is_ssp)
2415 {
2416 if (pairing_cb.is_le_only)
2417 {
2418 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_PAIR_NOT_SPT);
2419 }
2420 else
2421 {
2422 BTA_DmConfirm( (UINT8 *)bd_addr->address, FALSE);
2423 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2424 btif_storage_remove_bonded_device((bt_bdaddr_t *)bd_addr);
2425 }
2426 }
2427 else
2428 {
2429 if (pairing_cb.is_le_only)
2430 {
2431 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2432 }
2433 else
2434 {
2435 BTA_DmPinReply( (UINT8 *)bd_addr->address, FALSE, 0, NULL);
2436 }
2437 /* Cancel bonding, in case it is in ACL connection setup state */
2438 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2439 }
2440
2441 #else
2442 if (pairing_cb.is_ssp)
2443 {
2444 BTA_DmConfirm( (UINT8 *)bd_addr->address, FALSE);
2445 }
2446 else
2447 {
2448 BTA_DmPinReply( (UINT8 *)bd_addr->address, FALSE, 0, NULL);
2449 }
2450 /* Cancel bonding, in case it is in ACL connection setup state */
2451 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2452 btif_storage_remove_bonded_device((bt_bdaddr_t *)bd_addr);
2453 #endif
2454 }
2455
2456 return BT_STATUS_SUCCESS;
2457 }
2458
2459 /*******************************************************************************
2460 **
2461 ** Function btif_dm_hh_open_failed
2462 **
2463 ** Description informs the upper layers if the HH have failed during bonding
2464 **
2465 ** Returns none
2466 **
2467 *******************************************************************************/
2468
btif_dm_hh_open_failed(bt_bdaddr_t * bdaddr)2469 void btif_dm_hh_open_failed(bt_bdaddr_t *bdaddr)
2470 {
2471 if (pairing_cb.state == BT_BOND_STATE_BONDING &&
2472 bdcmp(bdaddr->address, pairing_cb.bd_addr) == 0)
2473 {
2474 bond_state_changed(BT_STATUS_FAIL, bdaddr, BT_BOND_STATE_NONE);
2475 }
2476 }
2477
2478 /*******************************************************************************
2479 **
2480 ** Function btif_dm_remove_bond
2481 **
2482 ** Description Removes bonding with the specified device
2483 **
2484 ** Returns bt_status_t
2485 **
2486 *******************************************************************************/
2487
btif_dm_remove_bond(const bt_bdaddr_t * bd_addr)2488 bt_status_t btif_dm_remove_bond(const bt_bdaddr_t *bd_addr)
2489 {
2490 bdstr_t bdstr;
2491
2492 BTIF_TRACE_EVENT("%s: bd_addr=%s", __FUNCTION__, bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr)));
2493
2494 btif_stats_add_bond_event(bd_addr, BTIF_DM_FUNC_REMOVE_BOND, pairing_cb.state);
2495
2496 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_REMOVE_BOND,
2497 (char *)bd_addr, sizeof(bt_bdaddr_t), NULL);
2498
2499 return BT_STATUS_SUCCESS;
2500 }
2501
2502 /*******************************************************************************
2503 **
2504 ** Function btif_dm_pin_reply
2505 **
2506 ** Description BT legacy pairing - PIN code reply
2507 **
2508 ** Returns bt_status_t
2509 **
2510 *******************************************************************************/
2511
btif_dm_pin_reply(const bt_bdaddr_t * bd_addr,uint8_t accept,uint8_t pin_len,bt_pin_code_t * pin_code)2512 bt_status_t btif_dm_pin_reply( const bt_bdaddr_t *bd_addr, uint8_t accept,
2513 uint8_t pin_len, bt_pin_code_t *pin_code)
2514 {
2515 BTIF_TRACE_EVENT("%s: accept=%d", __FUNCTION__, accept);
2516 if (pin_code == NULL || pin_len > PIN_CODE_LEN)
2517 return BT_STATUS_FAIL;
2518 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2519
2520 if (pairing_cb.is_le_only)
2521 {
2522 int i;
2523 UINT32 passkey = 0;
2524 int multi[] = {100000, 10000, 1000, 100, 10,1};
2525 BD_ADDR remote_bd_addr;
2526 bdcpy(remote_bd_addr, bd_addr->address);
2527 for (i = 0; i < 6; i++)
2528 {
2529 passkey += (multi[i] * (pin_code->pin[i] - '0'));
2530 }
2531 BTIF_TRACE_DEBUG("btif_dm_pin_reply: passkey: %d", passkey);
2532 BTA_DmBlePasskeyReply(remote_bd_addr, accept, passkey);
2533
2534 }
2535 else
2536 {
2537 BTA_DmPinReply( (UINT8 *)bd_addr->address, accept, pin_len, pin_code->pin);
2538 if (accept)
2539 pairing_cb.pin_code_len = pin_len;
2540 }
2541 #else
2542 BTA_DmPinReply( (UINT8 *)bd_addr->address, accept, pin_len, pin_code->pin);
2543
2544 if (accept)
2545 pairing_cb.pin_code_len = pin_len;
2546 #endif
2547 return BT_STATUS_SUCCESS;
2548 }
2549
2550 /*******************************************************************************
2551 **
2552 ** Function btif_dm_ssp_reply
2553 **
2554 ** Description BT SSP Reply - Just Works, Numeric Comparison & Passkey Entry
2555 **
2556 ** Returns bt_status_t
2557 **
2558 *******************************************************************************/
btif_dm_ssp_reply(const bt_bdaddr_t * bd_addr,bt_ssp_variant_t variant,uint8_t accept,uint32_t passkey)2559 bt_status_t btif_dm_ssp_reply(const bt_bdaddr_t *bd_addr,
2560 bt_ssp_variant_t variant, uint8_t accept,
2561 uint32_t passkey)
2562 {
2563 UNUSED(passkey);
2564
2565 if (variant == BT_SSP_VARIANT_PASSKEY_ENTRY)
2566 {
2567 /* This is not implemented in the stack.
2568 * For devices with display, this is not needed
2569 */
2570 BTIF_TRACE_WARNING("%s: Not implemented", __FUNCTION__);
2571 return BT_STATUS_FAIL;
2572 }
2573 /* BT_SSP_VARIANT_CONSENT & BT_SSP_VARIANT_PASSKEY_CONFIRMATION supported */
2574 BTIF_TRACE_EVENT("%s: accept=%d", __FUNCTION__, accept);
2575 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2576 if (pairing_cb.is_le_only)
2577 {
2578 if(pairing_cb.is_le_nc)
2579 {
2580 BTA_DmBleConfirmReply((UINT8 *)bd_addr->address,accept);
2581 } else {
2582 if (accept)
2583 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_GRANTED);
2584 else
2585 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_PAIR_NOT_SPT);
2586 }
2587 } else {
2588 BTA_DmConfirm( (UINT8 *)bd_addr->address, accept);
2589 }
2590 #else
2591 BTA_DmConfirm( (UINT8 *)bd_addr->address, accept);
2592 #endif
2593 return BT_STATUS_SUCCESS;
2594 }
2595
2596 /*******************************************************************************
2597 **
2598 ** Function btif_dm_get_adapter_property
2599 **
2600 ** Description Queries the BTA for the adapter property
2601 **
2602 ** Returns bt_status_t
2603 **
2604 *******************************************************************************/
btif_dm_get_adapter_property(bt_property_t * prop)2605 bt_status_t btif_dm_get_adapter_property(bt_property_t *prop)
2606 {
2607 BTIF_TRACE_EVENT("%s: type=0x%x", __FUNCTION__, prop->type);
2608 switch (prop->type)
2609 {
2610 case BT_PROPERTY_BDNAME:
2611 {
2612 bt_bdname_t *bd_name = (bt_bdname_t*)prop->val;
2613 strncpy((char *)bd_name->name, (char *)btif_get_default_local_name(),
2614 sizeof(bd_name->name) - 1);
2615 bd_name->name[sizeof(bd_name->name) - 1] = 0;
2616 prop->len = strlen((char *)bd_name->name);
2617 }
2618 break;
2619
2620 case BT_PROPERTY_ADAPTER_SCAN_MODE:
2621 {
2622 /* if the storage does not have it. Most likely app never set it. Default is NONE */
2623 bt_scan_mode_t *mode = (bt_scan_mode_t*)prop->val;
2624 *mode = BT_SCAN_MODE_NONE;
2625 prop->len = sizeof(bt_scan_mode_t);
2626 }
2627 break;
2628
2629 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
2630 {
2631 uint32_t *tmt = (uint32_t*)prop->val;
2632 *tmt = 120; /* default to 120s, if not found in NV */
2633 prop->len = sizeof(uint32_t);
2634 }
2635 break;
2636
2637 default:
2638 prop->len = 0;
2639 return BT_STATUS_FAIL;
2640 }
2641 return BT_STATUS_SUCCESS;
2642 }
2643
2644 /*******************************************************************************
2645 **
2646 ** Function btif_dm_get_remote_services
2647 **
2648 ** Description Start SDP to get remote services
2649 **
2650 ** Returns bt_status_t
2651 **
2652 *******************************************************************************/
btif_dm_get_remote_services(bt_bdaddr_t * remote_addr)2653 bt_status_t btif_dm_get_remote_services(bt_bdaddr_t *remote_addr)
2654 {
2655 bdstr_t bdstr;
2656
2657 BTIF_TRACE_EVENT("%s: remote_addr=%s", __FUNCTION__, bdaddr_to_string(remote_addr, bdstr, sizeof(bdstr)));
2658
2659 BTA_DmDiscover(remote_addr->address, BTA_ALL_SERVICE_MASK,
2660 bte_dm_search_services_evt, TRUE);
2661
2662 return BT_STATUS_SUCCESS;
2663 }
2664
2665 /*******************************************************************************
2666 **
2667 ** Function btif_dm_get_remote_services_transport
2668 **
2669 ** Description Start SDP to get remote services by transport
2670 **
2671 ** Returns bt_status_t
2672 **
2673 *******************************************************************************/
btif_dm_get_remote_services_by_transport(bt_bdaddr_t * remote_addr,const int transport)2674 bt_status_t btif_dm_get_remote_services_by_transport(bt_bdaddr_t *remote_addr, const int transport)
2675 {
2676 BTIF_TRACE_EVENT("%s", __func__);
2677
2678 /* Set the mask extension */
2679 tBTA_SERVICE_MASK_EXT mask_ext;
2680 mask_ext.num_uuid = 0;
2681 mask_ext.p_uuid = NULL;
2682 mask_ext.srvc_mask = BTA_ALL_SERVICE_MASK;
2683
2684 BTA_DmDiscoverByTransport(remote_addr->address, &mask_ext,
2685 bte_dm_search_services_evt, TRUE, transport);
2686
2687 return BT_STATUS_SUCCESS;
2688 }
2689
2690 /*******************************************************************************
2691 **
2692 ** Function btif_dm_get_remote_service_record
2693 **
2694 ** Description Start SDP to get remote service record
2695 **
2696 **
2697 ** Returns bt_status_t
2698 *******************************************************************************/
btif_dm_get_remote_service_record(bt_bdaddr_t * remote_addr,bt_uuid_t * uuid)2699 bt_status_t btif_dm_get_remote_service_record(bt_bdaddr_t *remote_addr,
2700 bt_uuid_t *uuid)
2701 {
2702 tSDP_UUID sdp_uuid;
2703 bdstr_t bdstr;
2704
2705 BTIF_TRACE_EVENT("%s: remote_addr=%s", __FUNCTION__, bdaddr_to_string(remote_addr, bdstr, sizeof(bdstr)));
2706
2707 sdp_uuid.len = MAX_UUID_SIZE;
2708 memcpy(sdp_uuid.uu.uuid128, uuid->uu, MAX_UUID_SIZE);
2709
2710 BTA_DmDiscoverUUID(remote_addr->address, &sdp_uuid,
2711 bte_dm_remote_service_record_evt, TRUE);
2712
2713 return BT_STATUS_SUCCESS;
2714 }
2715
btif_dm_execute_service_request(UINT16 event,char * p_param)2716 void btif_dm_execute_service_request(UINT16 event, char *p_param)
2717 {
2718 BOOLEAN b_enable = FALSE;
2719 bt_status_t status;
2720 if (event == BTIF_DM_ENABLE_SERVICE)
2721 {
2722 b_enable = TRUE;
2723 }
2724 status = btif_in_execute_service_request(*((tBTA_SERVICE_ID*)p_param), b_enable);
2725 if (status == BT_STATUS_SUCCESS)
2726 {
2727 bt_property_t property;
2728 bt_uuid_t local_uuids[BT_MAX_NUM_UUIDS];
2729
2730 /* Now send the UUID_PROPERTY_CHANGED event to the upper layer */
2731 BTIF_STORAGE_FILL_PROPERTY(&property, BT_PROPERTY_UUIDS,
2732 sizeof(local_uuids), local_uuids);
2733 btif_storage_get_adapter_property(&property);
2734 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb,
2735 BT_STATUS_SUCCESS, 1, &property);
2736 }
2737 return;
2738 }
2739
btif_dm_proc_io_req(BD_ADDR bd_addr,tBTA_IO_CAP * p_io_cap,tBTA_OOB_DATA * p_oob_data,tBTA_AUTH_REQ * p_auth_req,BOOLEAN is_orig)2740 void btif_dm_proc_io_req(BD_ADDR bd_addr, tBTA_IO_CAP *p_io_cap, tBTA_OOB_DATA *p_oob_data,
2741 tBTA_AUTH_REQ *p_auth_req, BOOLEAN is_orig)
2742 {
2743 UINT8 yes_no_bit = BTA_AUTH_SP_YES & *p_auth_req;
2744 /* if local initiated:
2745 ** 1. set DD + MITM
2746 ** if remote initiated:
2747 ** 1. Copy over the auth_req from peer's io_rsp
2748 ** 2. Set the MITM if peer has it set or if peer has DisplayYesNo (iPhone)
2749 ** as a fallback set MITM+GB if peer had MITM set
2750 */
2751 UNUSED (bd_addr);
2752 UNUSED (p_io_cap);
2753 UNUSED (p_oob_data);
2754
2755 BTIF_TRACE_DEBUG("+%s: p_auth_req=%d", __FUNCTION__, *p_auth_req);
2756 if(pairing_cb.is_local_initiated)
2757 {
2758 /* if initing/responding to a dedicated bonding, use dedicate bonding bit */
2759 *p_auth_req = BTA_AUTH_DD_BOND | BTA_AUTH_SP_YES;
2760 }
2761 else if (!is_orig)
2762 {
2763 /* peer initiated paring. They probably know what they want.
2764 ** Copy the mitm from peer device.
2765 */
2766 BTIF_TRACE_DEBUG("%s: setting p_auth_req to peer's: %d",
2767 __FUNCTION__, pairing_cb.auth_req);
2768 *p_auth_req = (pairing_cb.auth_req & BTA_AUTH_BONDS);
2769
2770 /* copy over the MITM bit as well. In addition if the peer has DisplayYesNo, force MITM */
2771 if ((yes_no_bit) || (pairing_cb.io_cap & BTM_IO_CAP_IO) )
2772 *p_auth_req |= BTA_AUTH_SP_YES;
2773 }
2774 else if (yes_no_bit)
2775 {
2776 /* set the general bonding bit for stored device */
2777 *p_auth_req = BTA_AUTH_GEN_BOND | yes_no_bit;
2778 }
2779 BTIF_TRACE_DEBUG("-%s: p_auth_req=%d", __FUNCTION__, *p_auth_req);
2780 }
2781
btif_dm_proc_io_rsp(BD_ADDR bd_addr,tBTA_IO_CAP io_cap,tBTA_OOB_DATA oob_data,tBTA_AUTH_REQ auth_req)2782 void btif_dm_proc_io_rsp(BD_ADDR bd_addr, tBTA_IO_CAP io_cap,
2783 tBTA_OOB_DATA oob_data, tBTA_AUTH_REQ auth_req)
2784 {
2785 UNUSED (bd_addr);
2786 UNUSED (oob_data);
2787
2788 if(auth_req & BTA_AUTH_BONDS)
2789 {
2790 BTIF_TRACE_DEBUG("%s auth_req:%d", __FUNCTION__, auth_req);
2791 pairing_cb.auth_req = auth_req;
2792 pairing_cb.io_cap = io_cap;
2793 }
2794 }
2795
btif_dm_set_oob_for_io_req(tBTA_OOB_DATA * p_has_oob_data)2796 void btif_dm_set_oob_for_io_req(tBTA_OOB_DATA *p_has_oob_data)
2797 {
2798 if (is_empty_128bit(oob_cb.oob_data.c192))
2799 {
2800 *p_has_oob_data = FALSE;
2801 }
2802 else
2803 {
2804 *p_has_oob_data = TRUE;
2805 }
2806 BTIF_TRACE_DEBUG("%s: *p_has_oob_data=%d", __func__, *p_has_oob_data);
2807 }
2808
btif_dm_set_oob_for_le_io_req(BD_ADDR bd_addr,tBTA_OOB_DATA * p_has_oob_data,tBTA_LE_AUTH_REQ * p_auth_req)2809 void btif_dm_set_oob_for_le_io_req(BD_ADDR bd_addr, tBTA_OOB_DATA *p_has_oob_data,
2810 tBTA_LE_AUTH_REQ *p_auth_req)
2811 {
2812
2813 /* We currently support only Security Manager TK as OOB data for LE transport.
2814 If it's not present mark no OOB data.
2815 */
2816 if (!is_empty_128bit(oob_cb.oob_data.sm_tk))
2817 {
2818 /* make sure OOB data is for this particular device */
2819 if (memcmp(bd_addr, oob_cb.bdaddr, BD_ADDR_LEN) == 0) {
2820 // When using OOB with TK, SC Secure Connections bit must be disabled.
2821 tBTA_LE_AUTH_REQ mask = ~BTM_LE_AUTH_REQ_SC_ONLY;
2822 *p_auth_req = ((*p_auth_req) & mask);
2823
2824 *p_has_oob_data = TRUE;
2825 }
2826 else
2827 {
2828 *p_has_oob_data = FALSE;
2829 BTIF_TRACE_WARNING("%s: remote address didn't match OOB data address",
2830 __func__);
2831 }
2832 }
2833 else
2834 {
2835 *p_has_oob_data = FALSE;
2836 }
2837 BTIF_TRACE_DEBUG("%s *p_has_oob_data=%d", __func__, *p_has_oob_data);
2838 }
2839
2840 #ifdef BTIF_DM_OOB_TEST
btif_dm_load_local_oob(void)2841 void btif_dm_load_local_oob(void)
2842 {
2843 char prop_oob[PROPERTY_VALUE_MAX];
2844 osi_property_get("service.brcm.bt.oob", prop_oob, "3");
2845 BTIF_TRACE_DEBUG("%s: prop_oob = %s", __func__, prop_oob);
2846 if (prop_oob[0] != '3')
2847 {
2848 if (is_empty_128bit(oob_cb.oob_data.c192))
2849 {
2850 BTIF_TRACE_DEBUG("%s: read OOB, call BTA_DmLocalOob()", __func__);
2851 BTA_DmLocalOob();
2852 }
2853 }
2854 }
2855
btif_dm_proc_loc_oob(BOOLEAN valid,BT_OCTET16 c,BT_OCTET16 r)2856 void btif_dm_proc_loc_oob(BOOLEAN valid, BT_OCTET16 c, BT_OCTET16 r)
2857 {
2858 FILE *fp;
2859 char *path_a = "/data/misc/bluedroid/LOCAL/a.key";
2860 char *path_b = "/data/misc/bluedroid/LOCAL/b.key";
2861 char *path = NULL;
2862 char prop_oob[PROPERTY_VALUE_MAX];
2863 BTIF_TRACE_DEBUG("%s: valid=%d", __func__, valid);
2864 if (is_empty_128bit(oob_cb.oob_data.c192) && valid)
2865 {
2866 BTIF_TRACE_DEBUG("save local OOB data in memory");
2867 memcpy(oob_cb.oob_data.c192, c, BT_OCTET16_LEN);
2868 memcpy(oob_cb.oob_data.r192, r, BT_OCTET16_LEN);
2869 osi_property_get("service.brcm.bt.oob", prop_oob, "3");
2870 BTIF_TRACE_DEBUG("%s: prop_oob = %s", __func__, prop_oob);
2871 if (prop_oob[0] == '1')
2872 path = path_a;
2873 else if (prop_oob[0] == '2')
2874 path = path_b;
2875 if (path)
2876 {
2877 fp = fopen(path, "wb+");
2878 if (fp == NULL)
2879 {
2880 BTIF_TRACE_DEBUG("%s: failed to save local OOB data to %s", __func__, path);
2881 }
2882 else
2883 {
2884 BTIF_TRACE_DEBUG("%s: save local OOB data into file %s", __func__, path);
2885 fwrite (c , 1 , BT_OCTET16_LEN , fp );
2886 fwrite (r , 1 , BT_OCTET16_LEN , fp );
2887 fclose(fp);
2888 }
2889 }
2890 }
2891 }
2892
2893 /*******************************************************************************
2894 **
2895 ** Function btif_dm_get_smp_config
2896 **
2897 ** Description Retrieve the SMP pairing options from the bt_stack.conf
2898 ** file. To provide specific pairing options for the host
2899 ** add a node with label "SmpOptions" to the config file
2900 ** and assign it a comma separated list of 5 values in the
2901 ** format: auth, io, ikey, rkey, ksize, oob
2902 ** eg: PTS_SmpOptions=0xD,0x4,0xf,0xf,0x10
2903 **
2904 ** Parameters: tBTE_APPL_CFG*: pointer to struct defining pairing options
2905 **
2906 ** Returns TRUE if the options were successfully read, else FALSE
2907 **
2908 *******************************************************************************/
btif_dm_get_smp_config(tBTE_APPL_CFG * p_cfg)2909 BOOLEAN btif_dm_get_smp_config(tBTE_APPL_CFG* p_cfg) {
2910
2911 if(!stack_config_get_interface()->get_pts_smp_options()) {
2912 BTIF_TRACE_DEBUG ("%s: SMP options not found in configuration", __func__);
2913 return FALSE;
2914 }
2915
2916 char conf[64];
2917 const char* recv = stack_config_get_interface()->get_pts_smp_options();
2918 char* pch;
2919 char* endptr;
2920
2921 strncpy(conf, recv, 64);
2922 conf[63] = 0; // null terminate
2923
2924 if ((pch = strtok(conf, ",")) != NULL)
2925 p_cfg->ble_auth_req = (UINT8) strtoul(pch, &endptr, 16);
2926 else
2927 return FALSE;
2928
2929 if ((pch = strtok(NULL, ",")) != NULL)
2930 p_cfg->ble_io_cap = (UINT8) strtoul(pch, &endptr, 16);
2931 else
2932 return FALSE;
2933
2934 if ((pch = strtok(NULL, ",")) != NULL)
2935 p_cfg->ble_init_key = (UINT8) strtoul(pch, &endptr, 16);
2936 else
2937 return FALSE;
2938
2939 if ((pch = strtok(NULL, ",")) != NULL)
2940 p_cfg->ble_resp_key = (UINT8) strtoul(pch, &endptr, 16);
2941 else
2942 return FALSE;
2943
2944 if ((pch = strtok(NULL, ",")) != NULL)
2945 p_cfg->ble_max_key_size = (UINT8) strtoul(pch, &endptr, 16);
2946 else
2947 return FALSE;
2948
2949 return TRUE;
2950 }
2951
btif_dm_proc_rmt_oob(BD_ADDR bd_addr,BT_OCTET16 p_c,BT_OCTET16 p_r)2952 BOOLEAN btif_dm_proc_rmt_oob(BD_ADDR bd_addr, BT_OCTET16 p_c, BT_OCTET16 p_r)
2953 {
2954 char t[128];
2955 FILE *fp;
2956 char *path_a = "/data/misc/bluedroid/LOCAL/a.key";
2957 char *path_b = "/data/misc/bluedroid/LOCAL/b.key";
2958 char *path = NULL;
2959 char prop_oob[PROPERTY_VALUE_MAX];
2960 BOOLEAN result = FALSE;
2961 bt_bdaddr_t bt_bd_addr;
2962 bdcpy(oob_cb.bdaddr, bd_addr);
2963 osi_property_get("service.brcm.bt.oob", prop_oob, "3");
2964 BTIF_TRACE_DEBUG("%s: prop_oob = %s", __func__, prop_oob);
2965 if (prop_oob[0] == '1')
2966 path = path_b;
2967 else if (prop_oob[0] == '2')
2968 path = path_a;
2969 if (path)
2970 {
2971 fp = fopen(path, "rb");
2972 if (fp == NULL)
2973 {
2974 BTIF_TRACE_DEBUG("%s: failed to read OOB keys from %s", __func__, path);
2975 return FALSE;
2976 }
2977 else
2978 {
2979 BTIF_TRACE_DEBUG("%s: read OOB data from %s", __func__, path);
2980 fread (p_c , 1 , BT_OCTET16_LEN , fp );
2981 fread (p_r , 1 , BT_OCTET16_LEN , fp );
2982 fclose(fp);
2983 }
2984 BTIF_TRACE_DEBUG("----%s: TRUE", __func__);
2985 sprintf(t, "%02x:%02x:%02x:%02x:%02x:%02x",
2986 oob_cb.bdaddr[0], oob_cb.bdaddr[1], oob_cb.bdaddr[2],
2987 oob_cb.bdaddr[3], oob_cb.bdaddr[4], oob_cb.bdaddr[5]);
2988 BTIF_TRACE_DEBUG("----%s: peer_bdaddr = %s", __func__, t);
2989 sprintf(t, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
2990 p_c[0], p_c[1], p_c[2], p_c[3], p_c[4], p_c[5], p_c[6], p_c[7],
2991 p_c[8], p_c[9], p_c[10], p_c[11], p_c[12], p_c[13], p_c[14], p_c[15]);
2992 BTIF_TRACE_DEBUG("----%s: c = %s", __func__, t);
2993 sprintf(t, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
2994 p_r[0], p_r[1], p_r[2], p_r[3], p_r[4], p_r[5], p_r[6], p_r[7],
2995 p_r[8], p_r[9], p_r[10], p_r[11], p_r[12], p_r[13], p_r[14], p_r[15]);
2996 BTIF_TRACE_DEBUG("----%s: r = %s", __func__, t);
2997 bdcpy(bt_bd_addr.address, bd_addr);
2998 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_BOND_STATE_BONDING,
2999 (char *)&bt_bd_addr, sizeof(bt_bdaddr_t), NULL);
3000 result = TRUE;
3001 }
3002 BTIF_TRACE_DEBUG("%s: result=%d", __func__, result);
3003 return result;
3004 }
3005 #endif /* BTIF_DM_OOB_TEST */
3006 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
3007
btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF * p_ssp_key_notif)3008 static void btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif)
3009 {
3010 bt_bdaddr_t bd_addr;
3011 bt_bdname_t bd_name;
3012 UINT32 cod;
3013 int dev_type;
3014
3015 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
3016
3017 /* Remote name update */
3018 if (!btif_get_device_type(p_ssp_key_notif->bd_addr, &dev_type))
3019 {
3020 dev_type = BT_DEVICE_TYPE_BLE;
3021 }
3022 btif_dm_update_ble_remote_properties(p_ssp_key_notif->bd_addr , p_ssp_key_notif->bd_name,
3023 (tBT_DEVICE_TYPE) dev_type);
3024 bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr);
3025 memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN);
3026
3027 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
3028 pairing_cb.is_ssp = FALSE;
3029 cod = COD_UNCLASSIFIED;
3030
3031 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name,
3032 cod, BT_SSP_VARIANT_PASSKEY_NOTIFICATION,
3033 p_ssp_key_notif->passkey);
3034 }
3035
3036 /*******************************************************************************
3037 **
3038 ** Function btif_dm_ble_auth_cmpl_evt
3039 **
3040 ** Description Executes authentication complete event in btif context
3041 **
3042 ** Returns void
3043 **
3044 *******************************************************************************/
btif_dm_ble_auth_cmpl_evt(tBTA_DM_AUTH_CMPL * p_auth_cmpl)3045 static void btif_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl)
3046 {
3047 /* Save link key, if not temporary */
3048 bt_bdaddr_t bd_addr;
3049 bt_status_t status = BT_STATUS_FAIL;
3050 bt_bond_state_t state = BT_BOND_STATE_NONE;
3051
3052 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
3053
3054 /* Clear OOB data */
3055 memset(&oob_cb, 0, sizeof(oob_cb));
3056
3057 if ( (p_auth_cmpl->success == TRUE) && (p_auth_cmpl->key_present) )
3058 {
3059 /* store keys */
3060 }
3061 if (p_auth_cmpl->success)
3062 {
3063 status = BT_STATUS_SUCCESS;
3064 state = BT_BOND_STATE_BONDED;
3065 int addr_type;
3066 bt_bdaddr_t bdaddr;
3067 bdcpy(bdaddr.address, p_auth_cmpl->bd_addr);
3068 if (btif_storage_get_remote_addr_type(&bdaddr, &addr_type) != BT_STATUS_SUCCESS)
3069 btif_storage_set_remote_addr_type(&bdaddr, p_auth_cmpl->addr_type);
3070
3071 /* Test for temporary bonding */
3072 if (btm_get_bond_type_dev(p_auth_cmpl->bd_addr) == BOND_TYPE_TEMPORARY) {
3073 BTIF_TRACE_DEBUG("%s: sending BT_BOND_STATE_NONE for Temp pairing",
3074 __func__);
3075 btif_storage_remove_bonded_device(&bdaddr);
3076 state = BT_BOND_STATE_NONE;
3077 } else {
3078 btif_dm_save_ble_bonding_keys();
3079 BTA_GATTC_Refresh(bd_addr.address);
3080 btif_dm_get_remote_services_by_transport(&bd_addr, BTA_GATT_TRANSPORT_LE);
3081 }
3082 }
3083 else
3084 {
3085 /*Map the HCI fail reason to bt status */
3086 switch (p_auth_cmpl->fail_reason)
3087 {
3088 case BTA_DM_AUTH_SMP_PAIR_AUTH_FAIL:
3089 case BTA_DM_AUTH_SMP_CONFIRM_VALUE_FAIL:
3090 btif_dm_remove_ble_bonding_keys();
3091 status = BT_STATUS_AUTH_FAILURE;
3092 break;
3093 case BTA_DM_AUTH_SMP_PAIR_NOT_SUPPORT:
3094 status = BT_STATUS_AUTH_REJECTED;
3095 break;
3096 default:
3097 btif_dm_remove_ble_bonding_keys();
3098 status = BT_STATUS_FAIL;
3099 break;
3100 }
3101 }
3102 bond_state_changed(status, &bd_addr, state);
3103 }
3104
btif_dm_load_ble_local_keys(void)3105 void btif_dm_load_ble_local_keys(void)
3106 {
3107 memset(&ble_local_key_cb, 0, sizeof(btif_dm_local_key_cb_t));
3108
3109 if (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_ER,(char*)&ble_local_key_cb.er[0],
3110 BT_OCTET16_LEN)== BT_STATUS_SUCCESS)
3111 {
3112 ble_local_key_cb.is_er_rcvd = TRUE;
3113 BTIF_TRACE_DEBUG("%s BLE ER key loaded",__FUNCTION__ );
3114 }
3115
3116 if ((btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IR,(char*)&ble_local_key_cb.id_keys.ir[0],
3117 BT_OCTET16_LEN)== BT_STATUS_SUCCESS )&&
3118 (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IRK, (char*)&ble_local_key_cb.id_keys.irk[0],
3119 BT_OCTET16_LEN)== BT_STATUS_SUCCESS)&&
3120 (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_DHK,(char*)&ble_local_key_cb.id_keys.dhk[0],
3121 BT_OCTET16_LEN)== BT_STATUS_SUCCESS))
3122 {
3123 ble_local_key_cb.is_id_keys_rcvd = TRUE;
3124 BTIF_TRACE_DEBUG("%s BLE ID keys loaded",__FUNCTION__ );
3125 }
3126
3127 }
btif_dm_get_ble_local_keys(tBTA_DM_BLE_LOCAL_KEY_MASK * p_key_mask,BT_OCTET16 er,tBTA_BLE_LOCAL_ID_KEYS * p_id_keys)3128 void btif_dm_get_ble_local_keys(tBTA_DM_BLE_LOCAL_KEY_MASK *p_key_mask, BT_OCTET16 er,
3129 tBTA_BLE_LOCAL_ID_KEYS *p_id_keys)
3130 {
3131 if (ble_local_key_cb.is_er_rcvd )
3132 {
3133 memcpy(&er[0], &ble_local_key_cb.er[0], sizeof(BT_OCTET16));
3134 *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ER;
3135 }
3136
3137 if (ble_local_key_cb.is_id_keys_rcvd)
3138 {
3139 memcpy(&p_id_keys->ir[0], &ble_local_key_cb.id_keys.ir[0], sizeof(BT_OCTET16));
3140 memcpy(&p_id_keys->irk[0], &ble_local_key_cb.id_keys.irk[0], sizeof(BT_OCTET16));
3141 memcpy(&p_id_keys->dhk[0], &ble_local_key_cb.id_keys.dhk[0], sizeof(BT_OCTET16));
3142 *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ID;
3143 }
3144 BTIF_TRACE_DEBUG("%s *p_key_mask=0x%02x",__FUNCTION__, *p_key_mask);
3145 }
3146
btif_dm_save_ble_bonding_keys(void)3147 void btif_dm_save_ble_bonding_keys(void)
3148 {
3149
3150 bt_bdaddr_t bd_addr;
3151
3152 BTIF_TRACE_DEBUG("%s",__FUNCTION__ );
3153
3154 bdcpy(bd_addr.address, pairing_cb.bd_addr);
3155
3156 if (pairing_cb.ble.is_penc_key_rcvd)
3157 {
3158 btif_storage_add_ble_bonding_key(&bd_addr,
3159 (char *) &pairing_cb.ble.penc_key,
3160 BTIF_DM_LE_KEY_PENC,
3161 sizeof(tBTM_LE_PENC_KEYS));
3162 }
3163
3164 if (pairing_cb.ble.is_pid_key_rcvd)
3165 {
3166 btif_storage_add_ble_bonding_key(&bd_addr,
3167 (char *) &pairing_cb.ble.pid_key,
3168 BTIF_DM_LE_KEY_PID,
3169 sizeof(tBTM_LE_PID_KEYS));
3170 }
3171
3172 if (pairing_cb.ble.is_pcsrk_key_rcvd)
3173 {
3174 btif_storage_add_ble_bonding_key(&bd_addr,
3175 (char *) &pairing_cb.ble.pcsrk_key,
3176 BTIF_DM_LE_KEY_PCSRK,
3177 sizeof(tBTM_LE_PCSRK_KEYS));
3178 }
3179
3180 if (pairing_cb.ble.is_lenc_key_rcvd)
3181 {
3182 btif_storage_add_ble_bonding_key(&bd_addr,
3183 (char *) &pairing_cb.ble.lenc_key,
3184 BTIF_DM_LE_KEY_LENC,
3185 sizeof(tBTM_LE_LENC_KEYS));
3186 }
3187
3188 if (pairing_cb.ble.is_lcsrk_key_rcvd)
3189 {
3190 btif_storage_add_ble_bonding_key(&bd_addr,
3191 (char *) &pairing_cb.ble.lcsrk_key,
3192 BTIF_DM_LE_KEY_LCSRK,
3193 sizeof(tBTM_LE_LCSRK_KEYS));
3194 }
3195
3196 if (pairing_cb.ble.is_lidk_key_rcvd)
3197 {
3198 btif_storage_add_ble_bonding_key(&bd_addr,
3199 NULL,
3200 BTIF_DM_LE_KEY_LID,
3201 0);
3202 }
3203
3204 }
3205
btif_dm_remove_ble_bonding_keys(void)3206 void btif_dm_remove_ble_bonding_keys(void)
3207 {
3208 bt_bdaddr_t bd_addr;
3209
3210 BTIF_TRACE_DEBUG("%s",__FUNCTION__ );
3211
3212 bdcpy(bd_addr.address, pairing_cb.bd_addr);
3213 btif_storage_remove_ble_bonding_keys(&bd_addr);
3214 }
3215
3216 /*******************************************************************************
3217 **
3218 ** Function btif_dm_ble_sec_req_evt
3219 **
3220 ** Description Eprocess security request event in btif context
3221 **
3222 ** Returns void
3223 **
3224 *******************************************************************************/
btif_dm_ble_sec_req_evt(tBTA_DM_BLE_SEC_REQ * p_ble_req)3225 void btif_dm_ble_sec_req_evt(tBTA_DM_BLE_SEC_REQ *p_ble_req)
3226 {
3227 bt_bdaddr_t bd_addr;
3228 bt_bdname_t bd_name;
3229 UINT32 cod;
3230 int dev_type;
3231
3232 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
3233
3234 if (pairing_cb.state == BT_BOND_STATE_BONDING)
3235 {
3236 BTIF_TRACE_DEBUG("%s Discard security request", __FUNCTION__);
3237 return;
3238 }
3239
3240 /* Remote name update */
3241 if (!btif_get_device_type(p_ble_req->bd_addr, &dev_type))
3242 {
3243 dev_type = BT_DEVICE_TYPE_BLE;
3244 }
3245 btif_dm_update_ble_remote_properties(p_ble_req->bd_addr, p_ble_req->bd_name,
3246 (tBT_DEVICE_TYPE) dev_type);
3247
3248 bdcpy(bd_addr.address, p_ble_req->bd_addr);
3249 memcpy(bd_name.name, p_ble_req->bd_name, BD_NAME_LEN);
3250
3251 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
3252
3253 pairing_cb.bond_type = BOND_TYPE_PERSISTENT;
3254 pairing_cb.is_le_only = TRUE;
3255 pairing_cb.is_le_nc = FALSE;
3256 pairing_cb.is_ssp = TRUE;
3257 btm_set_bond_type_dev(p_ble_req->bd_addr, pairing_cb.bond_type);
3258
3259 cod = COD_UNCLASSIFIED;
3260
3261 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod,
3262 BT_SSP_VARIANT_CONSENT, 0);
3263 }
3264
3265 /*******************************************************************************
3266 **
3267 ** Function btif_dm_ble_passkey_req_evt
3268 **
3269 ** Description Executes pin request event in btif context
3270 **
3271 ** Returns void
3272 **
3273 *******************************************************************************/
btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ * p_pin_req)3274 static void btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ *p_pin_req)
3275 {
3276 bt_bdaddr_t bd_addr;
3277 bt_bdname_t bd_name;
3278 UINT32 cod;
3279 int dev_type;
3280
3281 /* Remote name update */
3282 if (!btif_get_device_type(p_pin_req->bd_addr, &dev_type))
3283 {
3284 dev_type = BT_DEVICE_TYPE_BLE;
3285 }
3286 btif_dm_update_ble_remote_properties(p_pin_req->bd_addr,p_pin_req->bd_name,
3287 (tBT_DEVICE_TYPE) dev_type);
3288
3289 bdcpy(bd_addr.address, p_pin_req->bd_addr);
3290 memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN);
3291
3292 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
3293 pairing_cb.is_le_only = TRUE;
3294
3295 cod = COD_UNCLASSIFIED;
3296
3297 HAL_CBACK(bt_hal_cbacks, pin_request_cb,
3298 &bd_addr, &bd_name, cod, FALSE);
3299 }
btif_dm_ble_key_nc_req_evt(tBTA_DM_SP_KEY_NOTIF * p_notif_req)3300 static void btif_dm_ble_key_nc_req_evt(tBTA_DM_SP_KEY_NOTIF *p_notif_req)
3301 {
3302 /* TODO implement key notification for numeric comparison */
3303 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
3304
3305 /* Remote name update */
3306 btif_update_remote_properties(p_notif_req->bd_addr , p_notif_req->bd_name,
3307 NULL, BT_DEVICE_TYPE_BLE);
3308
3309 bt_bdaddr_t bd_addr;
3310 bdcpy(bd_addr.address, p_notif_req->bd_addr);
3311
3312 bt_bdname_t bd_name;
3313 memcpy(bd_name.name, p_notif_req->bd_name, BD_NAME_LEN);
3314
3315 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
3316 pairing_cb.is_ssp = FALSE;
3317 pairing_cb.is_le_only = TRUE;
3318 pairing_cb.is_le_nc = TRUE;
3319
3320 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name,
3321 COD_UNCLASSIFIED, BT_SSP_VARIANT_PASSKEY_CONFIRMATION,
3322 p_notif_req->passkey);
3323 }
3324
btif_dm_ble_oob_req_evt(tBTA_DM_SP_RMT_OOB * req_oob_type)3325 static void btif_dm_ble_oob_req_evt(tBTA_DM_SP_RMT_OOB *req_oob_type)
3326 {
3327 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
3328
3329 bt_bdaddr_t bd_addr;
3330 bdcpy(bd_addr.address, req_oob_type->bd_addr);
3331
3332 /* We currently support only Security Manager TK as OOB data. We already
3333 * checked if it's present in btif_dm_set_oob_for_le_io_req, but check here
3334 * again. If it's not present do nothing, pairing will timeout.
3335 */
3336 if (is_empty_128bit(oob_cb.oob_data.sm_tk)) {
3337 return;
3338 }
3339
3340 /* make sure OOB data is for this particular device */
3341 if (memcmp(req_oob_type->bd_addr, oob_cb.bdaddr, BD_ADDR_LEN) != 0) {
3342 BTIF_TRACE_WARNING("%s: remote address didn't match OOB data address", __func__);
3343 return;
3344 }
3345
3346 /* Remote name update */
3347 btif_update_remote_properties(req_oob_type->bd_addr , req_oob_type->bd_name,
3348 NULL, BT_DEVICE_TYPE_BLE);
3349
3350 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
3351 pairing_cb.is_ssp = FALSE;
3352 pairing_cb.is_le_only = TRUE;
3353 pairing_cb.is_le_nc = FALSE;
3354
3355 BTM_BleOobDataReply(req_oob_type->bd_addr, 0, 16, oob_cb.oob_data.sm_tk);
3356 }
3357
btif_dm_update_ble_remote_properties(BD_ADDR bd_addr,BD_NAME bd_name,tBT_DEVICE_TYPE dev_type)3358 void btif_dm_update_ble_remote_properties( BD_ADDR bd_addr, BD_NAME bd_name,
3359 tBT_DEVICE_TYPE dev_type)
3360 {
3361 btif_update_remote_properties(bd_addr,bd_name,NULL,dev_type);
3362 }
3363
btif_dm_ble_tx_test_cback(void * p)3364 static void btif_dm_ble_tx_test_cback(void *p)
3365 {
3366 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TX_TEST,
3367 (char *)p, 1, NULL);
3368 }
3369
btif_dm_ble_rx_test_cback(void * p)3370 static void btif_dm_ble_rx_test_cback(void *p)
3371 {
3372 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_RX_TEST,
3373 (char *)p, 1, NULL);
3374 }
3375
btif_dm_ble_test_end_cback(void * p)3376 static void btif_dm_ble_test_end_cback(void *p)
3377 {
3378 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TEST_END,
3379 (char *)p, 3, NULL);
3380 }
3381 /*******************************************************************************
3382 **
3383 ** Function btif_le_test_mode
3384 **
3385 ** Description Sends a HCI BLE Test command to the Controller
3386 **
3387 ** Returns BT_STATUS_SUCCESS on success
3388 **
3389 *******************************************************************************/
btif_le_test_mode(uint16_t opcode,uint8_t * buf,uint8_t len)3390 bt_status_t btif_le_test_mode(uint16_t opcode, uint8_t *buf, uint8_t len)
3391 {
3392 switch (opcode) {
3393 case HCI_BLE_TRANSMITTER_TEST:
3394 if (len != 3) return BT_STATUS_PARM_INVALID;
3395 BTM_BleTransmitterTest(buf[0],buf[1],buf[2], btif_dm_ble_tx_test_cback);
3396 break;
3397 case HCI_BLE_RECEIVER_TEST:
3398 if (len != 1) return BT_STATUS_PARM_INVALID;
3399 BTM_BleReceiverTest(buf[0], btif_dm_ble_rx_test_cback);
3400 break;
3401 case HCI_BLE_TEST_END:
3402 BTM_BleTestEnd((tBTM_CMPL_CB*) btif_dm_ble_test_end_cback);
3403 break;
3404 default:
3405 BTIF_TRACE_ERROR("%s: Unknown LE Test Mode Command 0x%x", __FUNCTION__, opcode);
3406 return BT_STATUS_UNSUPPORTED;
3407 }
3408 return BT_STATUS_SUCCESS;
3409 }
3410 #endif
3411
btif_dm_on_disable()3412 void btif_dm_on_disable()
3413 {
3414 /* cancel any pending pairing requests */
3415 if (pairing_cb.state == BT_BOND_STATE_BONDING)
3416 {
3417 bt_bdaddr_t bd_addr;
3418
3419 BTIF_TRACE_DEBUG("%s: Cancel pending pairing request", __FUNCTION__);
3420 bdcpy(bd_addr.address, pairing_cb.bd_addr);
3421 btif_dm_cancel_bond(&bd_addr);
3422 }
3423 }
3424
3425 /*******************************************************************************
3426 **
3427 ** Function btif_dm_read_energy_info
3428 **
3429 ** Description Reads the energy info from controller
3430 **
3431 ** Returns void
3432 **
3433 *******************************************************************************/
btif_dm_read_energy_info()3434 void btif_dm_read_energy_info()
3435 {
3436 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
3437 BTA_DmBleGetEnergyInfo(bta_energy_info_cb);
3438 #endif
3439 }
3440
btif_get_default_local_name()3441 static char* btif_get_default_local_name() {
3442 if (btif_default_local_name[0] == '\0')
3443 {
3444 int max_len = sizeof(btif_default_local_name) - 1;
3445 if (BTM_DEF_LOCAL_NAME[0] != '\0')
3446 {
3447 strncpy(btif_default_local_name, BTM_DEF_LOCAL_NAME, max_len);
3448 }
3449 else
3450 {
3451 char prop_model[PROPERTY_VALUE_MAX];
3452 osi_property_get(PROPERTY_PRODUCT_MODEL, prop_model, "");
3453 strncpy(btif_default_local_name, prop_model, max_len);
3454 }
3455 btif_default_local_name[max_len] = '\0';
3456 }
3457 return btif_default_local_name;
3458 }
3459
btif_stats_add_bond_event(const bt_bdaddr_t * bd_addr,bt_bond_function_t function,bt_bond_state_t state)3460 static void btif_stats_add_bond_event(const bt_bdaddr_t *bd_addr,
3461 bt_bond_function_t function,
3462 bt_bond_state_t state) {
3463 pthread_mutex_lock(&bond_event_lock);
3464
3465 btif_bond_event_t* event = &btif_dm_bond_events[btif_events_end_index];
3466 memcpy(&event->bd_addr, bd_addr, sizeof(bt_bdaddr_t));
3467 event->function = function;
3468 event->state = state;
3469 clock_gettime(CLOCK_REALTIME, &event->timestamp);
3470
3471 btif_num_bond_events++;
3472 btif_events_end_index = (btif_events_end_index + 1) % (MAX_BTIF_BOND_EVENT_ENTRIES + 1);
3473 if (btif_events_end_index == btif_events_start_index) {
3474 btif_events_start_index = (btif_events_start_index + 1) % (MAX_BTIF_BOND_EVENT_ENTRIES + 1);
3475 }
3476
3477 int type;
3478 btif_get_device_type(bd_addr->address, &type);
3479
3480 device_type_t device_type;
3481 switch (type) {
3482 case BT_DEVICE_TYPE_BREDR:
3483 device_type = DEVICE_TYPE_BREDR;
3484 break;
3485 case BT_DEVICE_TYPE_BLE:
3486 device_type = DEVICE_TYPE_LE;
3487 break;
3488 case BT_DEVICE_TYPE_DUMO:
3489 device_type = DEVICE_TYPE_DUMO;
3490 break;
3491 default:
3492 device_type = DEVICE_TYPE_UNKNOWN;
3493 break;
3494 }
3495
3496 uint32_t cod = get_cod(bd_addr);
3497 uint64_t ts = event->timestamp.tv_sec * 1000 +
3498 event->timestamp.tv_nsec / 1000000;
3499 metrics_pair_event(0, ts, cod, device_type);
3500
3501 pthread_mutex_unlock(&bond_event_lock);
3502 }
3503
btif_debug_bond_event_dump(int fd)3504 void btif_debug_bond_event_dump(int fd) {
3505 pthread_mutex_lock(&bond_event_lock);
3506 dprintf(fd, "\nBond Events: \n");
3507 dprintf(fd, " Total Number of events: %zu\n", btif_num_bond_events);
3508 if (btif_num_bond_events > 0)
3509 dprintf(fd, " Time BD_ADDR Function State\n");
3510
3511 for (size_t i = btif_events_start_index; i != btif_events_end_index;
3512 i = (i + 1) % (MAX_BTIF_BOND_EVENT_ENTRIES + 1)) {
3513 btif_bond_event_t* event = &btif_dm_bond_events[i];
3514
3515 char eventtime[20];
3516 char temptime[20];
3517 struct tm *tstamp = localtime(&event->timestamp.tv_sec);
3518 strftime(temptime, sizeof(temptime), "%H:%M:%S", tstamp);
3519 snprintf(eventtime, sizeof(eventtime), "%s.%03ld", temptime,
3520 event->timestamp.tv_nsec / 1000000);
3521
3522 char bdaddr[18];
3523 bdaddr_to_string(&event->bd_addr, bdaddr, sizeof(bdaddr));
3524
3525 char* func_name;
3526 switch (event->function) {
3527 case BTIF_DM_FUNC_CREATE_BOND:
3528 func_name = "btif_dm_create_bond";
3529 break;
3530 case BTIF_DM_FUNC_REMOVE_BOND:
3531 func_name = "btif_dm_remove_bond";
3532 break;
3533 case BTIF_DM_FUNC_BOND_STATE_CHANGED:
3534 func_name = "bond_state_changed ";
3535 break;
3536 default:
3537 func_name = "Invalid value ";
3538 break;
3539 }
3540
3541 char* bond_state;
3542 switch (event->state) {
3543 case BT_BOND_STATE_NONE:
3544 bond_state = "BOND_STATE_NONE";
3545 break;
3546 case BT_BOND_STATE_BONDING:
3547 bond_state = "BOND_STATE_BONDING";
3548 break;
3549 case BT_BOND_STATE_BONDED:
3550 bond_state = "BOND_STATE_BONDED";
3551 break;
3552 default:
3553 bond_state = "Invalid bond state";
3554 break;
3555 }
3556 dprintf(fd, " %s %s %s %s\n", eventtime, bdaddr, func_name, bond_state);
3557 }
3558 pthread_mutex_unlock(&bond_event_lock);
3559 }
3560