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