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