OSDN Git Service

Do not mask out secure connections (SC) bit for BT <4.2 am: 97305504e5 am: 5d30292aaa
[android-x86/system-bt.git] / btif / src / btif_dm.c
1 /******************************************************************************
2  *
3  *  Copyright (C) 2009-2012 Broadcom Corporation
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18
19 /************************************************************************************
20  *
21  *  Filename:      btif_dm.c
22  *
23  *  Description:   Contains Device Management (DM) related functionality
24  *
25  *
26  ***********************************************************************************/
27
28 #define LOG_TAG "bt_btif_dm"
29
30 #include "btif_dm.h"
31
32 #include <assert.h>
33 #include <signal.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <sys/types.h>
38 #include <unistd.h>
39
40 #include <hardware/bluetooth.h>
41
42 /**
43  * TODO(armansito): cutils/properties.h is only being used to pull-in runtime
44  * settings on Android. Remove this conditional include once we have a generic
45  * way to obtain system properties.
46  */
47 #if !defined(OS_GENERIC)
48 #include <cutils/properties.h>
49 #endif  /* !defined(OS_GENERIC) */
50
51 #include "bdaddr.h"
52 #include "bta_gatt_api.h"
53 #include "btif_api.h"
54 #include "btif_config.h"
55 #include "btif_hh.h"
56 #include "btif_sdp.h"
57 #include "btif_storage.h"
58 #include "btif_util.h"
59 #include "btu.h"
60 #include "bt_common.h"
61 #include "bta_gatt_api.h"
62 #include "device/include/interop.h"
63 #include "include/stack_config.h"
64 #include "osi/include/log.h"
65 #include "osi/include/allocator.h"
66 #include "osi/include/log.h"
67 #include "stack_config.h"
68 #include "stack/btm/btm_int.h"
69
70 /******************************************************************************
71 **  Constants & Macros
72 ******************************************************************************/
73
74 #define COD_UNCLASSIFIED ((0x1F) << 8)
75 #define COD_HID_KEYBOARD                    0x0540
76 #define COD_HID_POINTING                    0x0580
77 #define COD_HID_COMBO                       0x05C0
78 #define COD_HID_MAJOR                       0x0500
79 #define COD_AV_HEADSETS                     0x0404
80 #define COD_AV_HANDSFREE                    0x0408
81 #define COD_AV_HEADPHONES                   0x0418
82 #define COD_AV_PORTABLE_AUDIO               0x041C
83 #define COD_AV_HIFI_AUDIO                   0x0428
84
85 #define BTIF_DM_DEFAULT_INQ_MAX_RESULTS     0
86 #define BTIF_DM_DEFAULT_INQ_MAX_DURATION    10
87 #define BTIF_DM_MAX_SDP_ATTEMPTS_AFTER_PAIRING 2
88
89 #define NUM_TIMEOUT_RETRIES                 5
90
91 #define PROPERTY_PRODUCT_MODEL "ro.product.model"
92 #define DEFAULT_LOCAL_NAME_MAX  31
93 #if (DEFAULT_LOCAL_NAME_MAX > BTM_MAX_LOC_BD_NAME_LEN)
94     #error "default btif local name size exceeds stack supported length"
95 #endif
96
97 #if (defined(BTA_HOST_INTERLEAVE_SEARCH) && BTA_HOST_INTERLEAVE_SEARCH == TRUE)
98 #define BTIF_DM_INTERLEAVE_DURATION_BR_ONE    2
99 #define BTIF_DM_INTERLEAVE_DURATION_LE_ONE    2
100 #define BTIF_DM_INTERLEAVE_DURATION_BR_TWO    3
101 #define BTIF_DM_INTERLEAVE_DURATION_LE_TWO    4
102 #endif
103
104 #define MAX_SDP_BL_ENTRIES 3
105
106 #define ENCRYPTED_BREDR       2
107 #define ENCRYPTED_LE          4
108
109 typedef struct
110 {
111     bt_bond_state_t state;
112     bt_bdaddr_t static_bdaddr;
113     BD_ADDR bd_addr;
114     tBTM_BOND_TYPE bond_type;
115     UINT8 pin_code_len;
116     UINT8 is_ssp;
117     UINT8 auth_req;
118     UINT8 io_cap;
119     UINT8 autopair_attempts;
120     UINT8 timeout_retries;
121     UINT8 is_local_initiated;
122     UINT8 sdp_attempts;
123 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
124     BOOLEAN is_le_only;
125     BOOLEAN is_le_nc; /* LE Numeric comparison */
126     btif_dm_ble_cb_t ble;
127 #endif
128 } btif_dm_pairing_cb_t;
129
130 typedef struct
131 {
132     UINT8       ir[BT_OCTET16_LEN];
133     UINT8       irk[BT_OCTET16_LEN];
134     UINT8       dhk[BT_OCTET16_LEN];
135 }btif_dm_local_key_id_t;
136
137 typedef struct
138 {
139     BOOLEAN                 is_er_rcvd;
140     UINT8                   er[BT_OCTET16_LEN];
141     BOOLEAN                 is_id_keys_rcvd;
142     btif_dm_local_key_id_t  id_keys;  /* ID kyes */
143
144 }btif_dm_local_key_cb_t;
145
146 typedef struct
147 {
148     BD_ADDR bd_addr;
149     BD_NAME bd_name;
150 } btif_dm_remote_name_t;
151
152 typedef struct
153 {
154     BT_OCTET16 sp_c;
155     BT_OCTET16 sp_r;
156     BD_ADDR  oob_bdaddr;  /* peer bdaddr*/
157 } btif_dm_oob_cb_t;
158
159 typedef struct
160 {
161     bt_bdaddr_t  bdaddr;
162     UINT8        transport; /* 0=Unknown, 1=BR/EDR, 2=LE */
163 } btif_dm_create_bond_cb_t;
164
165 typedef struct
166 {
167     uint8_t  status;
168     uint8_t  ctrl_state;
169     uint64_t tx_time;
170     uint64_t rx_time;
171     uint64_t idle_time;
172     uint64_t energy_used;
173 } btif_activity_energy_info_cb_t;
174
175 typedef struct
176 {
177     unsigned int   manufact_id;
178 }skip_sdp_entry_t;
179
180 #define BTA_SERVICE_ID_TO_SERVICE_MASK(id)       (1 << (id))
181
182 #define MAX_SDP_BL_ENTRIES 3
183 #define UUID_HUMAN_INTERFACE_DEVICE "00001124-0000-1000-8000-00805f9b34fb"
184
185 static skip_sdp_entry_t sdp_blacklist[] = {{76}}; //Apple Mouse and Keyboard
186
187 /* This flag will be true if HCI_Inquiry is in progress */
188 static BOOLEAN btif_dm_inquiry_in_progress = FALSE;
189
190 /************************************************************************************
191 **  Static variables
192 ************************************************************************************/
193 static char btif_default_local_name[DEFAULT_LOCAL_NAME_MAX+1] = {'\0'};
194
195 /******************************************************************************
196 **  Static functions
197 ******************************************************************************/
198 static btif_dm_pairing_cb_t pairing_cb;
199 static btif_dm_oob_cb_t     oob_cb;
200 static void btif_dm_generic_evt(UINT16 event, char* p_param);
201 static void btif_dm_cb_create_bond(bt_bdaddr_t *bd_addr, tBTA_TRANSPORT transport);
202 static void btif_dm_cb_hid_remote_name(tBTM_REMOTE_DEV_NAME *p_remote_name);
203 static void btif_update_remote_properties(BD_ADDR bd_addr, BD_NAME bd_name,
204                                           DEV_CLASS dev_class, tBT_DEVICE_TYPE dev_type);
205 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
206 static btif_dm_local_key_cb_t ble_local_key_cb;
207 static void btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif);
208 static void btif_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl);
209 static void btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ *p_pin_req);
210 static void btif_dm_ble_key_nc_req_evt(tBTA_DM_SP_KEY_NOTIF *p_notif_req) ;
211 #endif
212
213 static void bte_scan_filt_param_cfg_evt(UINT8 action_type,
214                                            tBTA_DM_BLE_PF_AVBL_SPACE avbl_space,
215                                            tBTA_DM_BLE_REF_VALUE ref_value, tBTA_STATUS status);
216
217 static char* btif_get_default_local_name();
218 /******************************************************************************
219 **  Externs
220 ******************************************************************************/
221 extern UINT16 bta_service_id_to_uuid_lkup_tbl [BTA_MAX_SERVICE_ID];
222 extern bt_status_t btif_hf_execute_service(BOOLEAN b_enable);
223 extern bt_status_t btif_av_execute_service(BOOLEAN b_enable);
224 extern bt_status_t btif_av_sink_execute_service(BOOLEAN b_enable);
225 extern bt_status_t btif_hh_execute_service(BOOLEAN b_enable);
226 extern bt_status_t btif_hf_client_execute_service(BOOLEAN b_enable);
227 extern bt_status_t btif_sdp_execute_service(BOOLEAN b_enable);
228 extern int btif_hh_connect(bt_bdaddr_t *bd_addr);
229 extern void bta_gatt_convert_uuid16_to_uuid128(UINT8 uuid_128[LEN_UUID_128], UINT16 uuid_16);
230
231 /******************************************************************************
232 **  Functions
233 ******************************************************************************/
234
235 static void btif_dm_data_copy(uint16_t event, char *dst, char *src)
236 {
237     tBTA_DM_SEC *dst_dm_sec = (tBTA_DM_SEC*)dst;
238     tBTA_DM_SEC *src_dm_sec = (tBTA_DM_SEC*)src;
239
240     if (!src_dm_sec)
241         return;
242
243     assert(dst_dm_sec);
244     maybe_non_aligned_memcpy(dst_dm_sec, src_dm_sec, sizeof(*src_dm_sec));
245
246     if (event == BTA_DM_BLE_KEY_EVT)
247     {
248         dst_dm_sec->ble_key.p_key_value = osi_malloc(sizeof(tBTM_LE_KEY_VALUE));
249         assert(src_dm_sec->ble_key.p_key_value);
250         assert(dst_dm_sec->ble_key.p_key_value);
251         memcpy(dst_dm_sec->ble_key.p_key_value, src_dm_sec->ble_key.p_key_value, sizeof(tBTM_LE_KEY_VALUE));
252     }
253 }
254
255 static void btif_dm_data_free(uint16_t event, tBTA_DM_SEC *dm_sec)
256 {
257     if (event == BTA_DM_BLE_KEY_EVT)
258         osi_free(dm_sec->ble_key.p_key_value);
259 }
260
261 bt_status_t btif_in_execute_service_request(tBTA_SERVICE_ID service_id,
262                                                 BOOLEAN b_enable)
263 {
264     BTIF_TRACE_DEBUG("%s service_id: %d", __FUNCTION__, service_id);
265     /* Check the service_ID and invoke the profile's BT state changed API */
266     switch (service_id)
267     {
268          case BTA_HFP_SERVICE_ID:
269          case BTA_HSP_SERVICE_ID:
270          {
271               btif_hf_execute_service(b_enable);
272          }break;
273          case BTA_A2DP_SOURCE_SERVICE_ID:
274          {
275               btif_av_execute_service(b_enable);
276          }break;
277          case BTA_A2DP_SINK_SERVICE_ID:
278          {
279             btif_av_sink_execute_service(b_enable);
280          }break;
281          case BTA_HID_SERVICE_ID:
282          {
283               btif_hh_execute_service(b_enable);
284          }break;
285          case BTA_HFP_HS_SERVICE_ID:
286          {
287              btif_hf_client_execute_service(b_enable);
288          }break;
289          case BTA_SDP_SERVICE_ID:
290          {
291              btif_sdp_execute_service(b_enable);
292          }break;
293          default:
294               BTIF_TRACE_ERROR("%s: Unknown service being enabled", __FUNCTION__);
295               return BT_STATUS_FAIL;
296     }
297     return BT_STATUS_SUCCESS;
298 }
299
300 /*******************************************************************************
301 **
302 ** Function         check_eir_remote_name
303 **
304 ** Description      Check if remote name is in the EIR data
305 **
306 ** Returns          TRUE if remote name found
307 **                  Populate p_remote_name, if provided and remote name found
308 **
309 *******************************************************************************/
310 static BOOLEAN check_eir_remote_name(tBTA_DM_SEARCH *p_search_data,
311                             UINT8 *p_remote_name, UINT8 *p_remote_name_len)
312 {
313     UINT8 *p_eir_remote_name = NULL;
314     UINT8 remote_name_len = 0;
315
316     /* Check EIR for remote name and services */
317     if (p_search_data->inq_res.p_eir)
318     {
319         p_eir_remote_name = BTM_CheckEirData(p_search_data->inq_res.p_eir,
320                 BTM_EIR_COMPLETE_LOCAL_NAME_TYPE, &remote_name_len);
321         if (!p_eir_remote_name)
322         {
323             p_eir_remote_name = BTM_CheckEirData(p_search_data->inq_res.p_eir,
324                     BTM_EIR_SHORTENED_LOCAL_NAME_TYPE, &remote_name_len);
325         }
326
327         if (p_eir_remote_name)
328         {
329             if (remote_name_len > BD_NAME_LEN)
330                 remote_name_len = BD_NAME_LEN;
331
332             if (p_remote_name && p_remote_name_len)
333             {
334                 memcpy(p_remote_name, p_eir_remote_name, remote_name_len);
335                 *(p_remote_name + remote_name_len) = 0;
336                 *p_remote_name_len = remote_name_len;
337             }
338
339             return TRUE;
340         }
341     }
342
343     return FALSE;
344
345 }
346
347 /*******************************************************************************
348 **
349 ** Function         check_cached_remote_name
350 **
351 ** Description      Check if remote name is in the NVRAM cache
352 **
353 ** Returns          TRUE if remote name found
354 **                  Populate p_remote_name, if provided and remote name found
355 **
356 *******************************************************************************/
357 static BOOLEAN check_cached_remote_name(tBTA_DM_SEARCH *p_search_data,
358                                 UINT8 *p_remote_name, UINT8 *p_remote_name_len)
359 {
360     bt_bdname_t bdname;
361     bt_bdaddr_t remote_bdaddr;
362     bt_property_t prop_name;
363
364     /* check if we already have it in our btif_storage cache */
365     bdcpy(remote_bdaddr.address, p_search_data->inq_res.bd_addr);
366     BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_BDNAME,
367                                sizeof(bt_bdname_t), &bdname);
368     if (btif_storage_get_remote_device_property(
369         &remote_bdaddr, &prop_name) == BT_STATUS_SUCCESS)
370     {
371         if (p_remote_name && p_remote_name_len)
372         {
373             strcpy((char *)p_remote_name, (char *)bdname.name);
374             *p_remote_name_len = strlen((char *)p_remote_name);
375         }
376         return TRUE;
377     }
378
379     return FALSE;
380 }
381
382 BOOLEAN check_cod(const bt_bdaddr_t *remote_bdaddr, uint32_t cod)
383 {
384     uint32_t    remote_cod;
385     bt_property_t prop_name;
386
387     /* check if we already have it in our btif_storage cache */
388     BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_CLASS_OF_DEVICE,
389                                sizeof(uint32_t), &remote_cod);
390     if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr, &prop_name) == BT_STATUS_SUCCESS)
391     {
392         LOG_INFO(LOG_TAG, "%s remote_cod = 0x%08x cod = 0x%08x", __func__, remote_cod, cod);
393         if ((remote_cod & 0x7ff) == cod)
394             return TRUE;
395     }
396
397     return FALSE;
398 }
399
400 BOOLEAN check_cod_hid(const bt_bdaddr_t *remote_bdaddr, uint32_t cod)
401 {
402     uint32_t    remote_cod;
403     bt_property_t prop_name;
404
405     /* check if we already have it in our btif_storage cache */
406     BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_CLASS_OF_DEVICE,
407                                sizeof(uint32_t), &remote_cod);
408     if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr,
409                                 &prop_name) == BT_STATUS_SUCCESS)
410     {
411         BTIF_TRACE_DEBUG("%s: remote_cod = 0x%06x", __FUNCTION__, remote_cod);
412         if ((remote_cod & 0x700) == cod)
413             return TRUE;
414     }
415     return FALSE;
416 }
417
418 BOOLEAN check_hid_le(const bt_bdaddr_t *remote_bdaddr)
419 {
420     uint32_t    remote_dev_type;
421     bt_property_t prop_name;
422
423     /* check if we already have it in our btif_storage cache */
424     BTIF_STORAGE_FILL_PROPERTY(&prop_name,BT_PROPERTY_TYPE_OF_DEVICE,
425                                sizeof(uint32_t), &remote_dev_type);
426     if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr,
427                                 &prop_name) == BT_STATUS_SUCCESS)
428     {
429         if (remote_dev_type == BT_DEVICE_DEVTYPE_BLE)
430         {
431             bdstr_t bdstr;
432             bdaddr_to_string(remote_bdaddr, bdstr, sizeof(bdstr));
433             if(btif_config_exist(bdstr, "HidAppId"))
434                 return TRUE;
435         }
436     }
437     return FALSE;
438 }
439
440 /*****************************************************************************
441 **
442 ** Function        check_sdp_bl
443 **
444 ** Description     Checks if a given device is blacklisted to skip sdp
445 **
446 ** Parameters     skip_sdp_entry
447 **
448 ** Returns         TRUE if the device is present in blacklist, else FALSE
449 **
450 *******************************************************************************/
451 BOOLEAN check_sdp_bl(const bt_bdaddr_t *remote_bdaddr)
452 {
453     UINT16 manufacturer = 0;
454     UINT8 lmp_ver = 0;
455     UINT16 lmp_subver = 0;
456     bt_property_t prop_name;
457     bt_remote_version_t info;
458
459     if (remote_bdaddr == NULL)
460         return FALSE;
461
462 /* fetch additional info about remote device used in iop query */
463     BTM_ReadRemoteVersion(*(BD_ADDR*)remote_bdaddr, &lmp_ver,
464                     &manufacturer, &lmp_subver);
465
466  /* if not available yet, try fetching from config database */
467     BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_REMOTE_VERSION_INFO,
468                             sizeof(bt_remote_version_t), &info);
469
470     if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr,
471                                               &prop_name) != BT_STATUS_SUCCESS)
472     {
473
474         return FALSE;
475     }
476     manufacturer = info.manufacturer;
477
478     for (int i = 0; i < MAX_SDP_BL_ENTRIES; i++)
479     {
480         if (manufacturer == sdp_blacklist[i].manufact_id)
481             return TRUE;
482     }
483     return FALSE;
484 }
485
486 static void bond_state_changed(bt_status_t status, bt_bdaddr_t *bd_addr, bt_bond_state_t state)
487 {
488     // Send bonding state only once - based on outgoing/incoming we may receive duplicates
489     if ((pairing_cb.state == state) && (state == BT_BOND_STATE_BONDING))
490     {
491         // Cross key pairing so send callback for static address
492         if (!bdaddr_is_empty(&pairing_cb.static_bdaddr))
493         {
494             HAL_CBACK(bt_hal_cbacks, bond_state_changed_cb, status, bd_addr, state);
495         }
496         return;
497     }
498
499     if (pairing_cb.bond_type == BOND_TYPE_TEMPORARY)
500         state = BT_BOND_STATE_NONE;
501
502     BTIF_TRACE_DEBUG("%s: state=%d, prev_state=%d, sdp_attempts = %d", __func__,
503                       state, pairing_cb.state, pairing_cb.sdp_attempts);
504
505     HAL_CBACK(bt_hal_cbacks, bond_state_changed_cb, status, bd_addr, state);
506
507     if (state == BT_BOND_STATE_BONDING)
508     {
509         pairing_cb.state = state;
510         bdcpy(pairing_cb.bd_addr, bd_addr->address);
511     } else {
512         if (!pairing_cb.sdp_attempts)
513             memset(&pairing_cb, 0, sizeof(pairing_cb));
514         else
515             BTIF_TRACE_DEBUG("%s: BR-EDR service discovery active", __func__);
516     }
517 }
518
519 /* store remote version in bt config to always have access
520    to it post pairing*/
521 static void btif_update_remote_version_property(bt_bdaddr_t *p_bd)
522 {
523     bt_property_t property;
524     UINT8 lmp_ver = 0;
525     UINT16 lmp_subver = 0;
526     UINT16 mfct_set = 0;
527     tBTM_STATUS btm_status;
528     bt_remote_version_t info;
529     bt_status_t status;
530     bdstr_t bdstr;
531
532     btm_status = BTM_ReadRemoteVersion(*(BD_ADDR*)p_bd, &lmp_ver,
533                           &mfct_set, &lmp_subver);
534
535     LOG_DEBUG(LOG_TAG, "remote version info [%s]: %x, %x, %x", bdaddr_to_string(p_bd, bdstr, sizeof(bdstr)),
536                lmp_ver, mfct_set, lmp_subver);
537
538     if (btm_status == BTM_SUCCESS)
539     {
540         // Always update cache to ensure we have availability whenever BTM API is not populated
541         info.manufacturer = mfct_set;
542         info.sub_ver = lmp_subver;
543         info.version = lmp_ver;
544         BTIF_STORAGE_FILL_PROPERTY(&property,
545                             BT_PROPERTY_REMOTE_VERSION_INFO, sizeof(bt_remote_version_t),
546                             &info);
547         status = btif_storage_set_remote_device_property(p_bd, &property);
548         ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote version", status);
549     }
550 }
551
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 *******************************************************************************/
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 *******************************************************************************/
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 *******************************************************************************/
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 *******************************************************************************/
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 *******************************************************************************/
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     maybe_non_aligned_memcpy(p_dest_data, p_src_data, sizeof(*p_src_data));
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
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     maybe_non_aligned_memcpy(p_dest_data, p_src_data, sizeof(*p_src_data));
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                        osi_freebuf(p_src_data->disc_res.p_uuid_list);
827                   }
828                   if (p_src_data->disc_res.p_raw_data != NULL)
829                   {
830                       osi_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 *******************************************************************************/
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 *******************************************************************************/
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(LOG_TAG, "%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
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(LOG_TAG, "%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 *******************************************************************************/
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(LOG_TAG, "%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(LOG_TAG, "%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 ******************************************************************************/
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(LOG_TAG, "%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                 bt_property_t properties[5];
1316                 bt_device_type_t dev_type;
1317                 uint32_t num_properties = 0;
1318                 bt_status_t status;
1319                 int addr_type = 0;
1320
1321                 memset(properties, 0, sizeof(properties));
1322                 /* BD_ADDR */
1323                 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1324                                     BT_PROPERTY_BDADDR, sizeof(bdaddr), &bdaddr);
1325                 num_properties++;
1326                 /* BD_NAME */
1327                 /* Don't send BDNAME if it is empty */
1328                 if (bdname.name[0])
1329                 {
1330                     BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1331                                                BT_PROPERTY_BDNAME,
1332                                                strlen((char *)bdname.name), &bdname);
1333                     num_properties++;
1334                 }
1335
1336                 /* DEV_CLASS */
1337                 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1338                                     BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
1339                 num_properties++;
1340                 /* DEV_TYPE */
1341 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1342                 /* FixMe: Assumption is that bluetooth.h and BTE enums match */
1343
1344                 /* Verify if the device is dual mode in NVRAM */
1345                 int stored_device_type = 0;
1346                 if (btif_get_device_type(bdaddr.address, &stored_device_type) &&
1347                     ((stored_device_type == BT_DEVICE_TYPE_BLE &&
1348                         p_search_data->inq_res.device_type == BT_DEVICE_TYPE_BREDR) ||
1349                      (stored_device_type == BT_DEVICE_TYPE_BREDR &&
1350                         p_search_data->inq_res.device_type == BT_DEVICE_TYPE_BLE))) {
1351                     dev_type = BT_DEVICE_TYPE_DUMO;
1352                 } else {
1353                     dev_type = p_search_data->inq_res.device_type;
1354                 }
1355
1356                 if (p_search_data->inq_res.device_type == BT_DEVICE_TYPE_BLE)
1357                     addr_type = p_search_data->inq_res.ble_addr_type;
1358 #else
1359                 dev_type = BT_DEVICE_TYPE_BREDR;
1360 #endif
1361                 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1362                                     BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type), &dev_type);
1363                 num_properties++;
1364                 /* RSSI */
1365                 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1366                                     BT_PROPERTY_REMOTE_RSSI, sizeof(int8_t),
1367                                     &(p_search_data->inq_res.rssi));
1368                 num_properties++;
1369
1370                 status = btif_storage_add_remote_device(&bdaddr, num_properties, properties);
1371                 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device (inquiry)", status);
1372 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1373                 status = btif_storage_set_remote_addr_type(&bdaddr, addr_type);
1374                 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote addr type (inquiry)", status);
1375 #endif
1376                 /* Callback to notify upper layer of device */
1377                 HAL_CBACK(bt_hal_cbacks, device_found_cb,
1378                                  num_properties, properties);
1379             }
1380         }
1381         break;
1382
1383         case BTA_DM_INQ_CMPL_EVT:
1384         {
1385 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1386             tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param;
1387             memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS));
1388             BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, &adv_filt_param, NULL,
1389                                      bte_scan_filt_param_cfg_evt, 0);
1390 #endif
1391         }
1392         break;
1393         case BTA_DM_DISC_CMPL_EVT:
1394         {
1395             HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STOPPED);
1396         }
1397         break;
1398         case BTA_DM_SEARCH_CANCEL_CMPL_EVT:
1399         {
1400            /* if inquiry is not in progress and we get a cancel event, then
1401             * it means we are done with inquiry, but remote_name fetches are in
1402             * progress
1403             *
1404             * if inquiry  is in progress, then we don't want to act on this cancel_cmpl_evt
1405             * but instead wait for the cancel_cmpl_evt via the Busy Level
1406             *
1407             */
1408            if (btif_dm_inquiry_in_progress == FALSE)
1409            {
1410 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1411                tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param;
1412                memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS));
1413                BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, &adv_filt_param, NULL,
1414                                         bte_scan_filt_param_cfg_evt, 0);
1415 #endif
1416                HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STOPPED);
1417            }
1418         }
1419         break;
1420     }
1421 }
1422
1423 /*******************************************************************************
1424 **
1425 ** Function         btif_dm_search_services_evt
1426 **
1427 ** Description      Executes search services event in btif context
1428 **
1429 ** Returns          void
1430 **
1431 *******************************************************************************/
1432 static void btif_dm_search_services_evt(UINT16 event, char *p_param)
1433 {
1434     tBTA_DM_SEARCH *p_data = (tBTA_DM_SEARCH*)p_param;
1435
1436     BTIF_TRACE_EVENT("%s:  event = %d", __FUNCTION__, event);
1437     switch (event)
1438     {
1439         case BTA_DM_DISC_RES_EVT:
1440         {
1441             bt_property_t prop;
1442             uint32_t i = 0;
1443             bt_bdaddr_t bd_addr;
1444             bt_status_t ret;
1445
1446             bdcpy(bd_addr.address, p_data->disc_res.bd_addr);
1447
1448             BTIF_TRACE_DEBUG("%s:(result=0x%x, services 0x%x)", __FUNCTION__,
1449                     p_data->disc_res.result, p_data->disc_res.services);
1450             if  ((p_data->disc_res.result != BTA_SUCCESS) &&
1451                  (pairing_cb.state == BT_BOND_STATE_BONDING ) &&
1452                  (pairing_cb.sdp_attempts < BTIF_DM_MAX_SDP_ATTEMPTS_AFTER_PAIRING))
1453             {
1454                 BTIF_TRACE_WARNING("%s:SDP failed after bonding re-attempting", __FUNCTION__);
1455                 pairing_cb.sdp_attempts++;
1456                 btif_dm_get_remote_services(&bd_addr);
1457                 return;
1458             }
1459             prop.type = BT_PROPERTY_UUIDS;
1460             prop.len = 0;
1461             if ((p_data->disc_res.result == BTA_SUCCESS) && (p_data->disc_res.num_uuids > 0))
1462             {
1463                  prop.val = p_data->disc_res.p_uuid_list;
1464                  prop.len = p_data->disc_res.num_uuids * MAX_UUID_SIZE;
1465                  for (i=0; i < p_data->disc_res.num_uuids; i++)
1466                  {
1467                       char temp[256];
1468                       uuid_to_string_legacy((bt_uuid_t*)(p_data->disc_res.p_uuid_list + (i*MAX_UUID_SIZE)), temp);
1469                       LOG_INFO(LOG_TAG, "%s index:%d uuid:%s", __func__, i, temp);
1470                  }
1471             }
1472
1473             /* onUuidChanged requires getBondedDevices to be populated.
1474             ** bond_state_changed needs to be sent prior to remote_device_property
1475             */
1476             if ((pairing_cb.state == BT_BOND_STATE_BONDING) &&
1477                 ((bdcmp(p_data->disc_res.bd_addr, pairing_cb.bd_addr) == 0) ||
1478                  (bdcmp(p_data->disc_res.bd_addr, pairing_cb.static_bdaddr.address) == 0)) &&
1479                   pairing_cb.sdp_attempts > 0)
1480             {
1481                  BTIF_TRACE_DEBUG("%s Remote Service SDP done. Call bond_state_changed_cb BONDED",
1482                                    __FUNCTION__);
1483                  pairing_cb.sdp_attempts  = 0;
1484
1485                  // If bonding occured due to cross-key pairing, send bonding callback
1486                  // for static address now
1487                  if (bdcmp(p_data->disc_res.bd_addr, pairing_cb.static_bdaddr.address) == 0)
1488                     bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
1489
1490                  bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDED);
1491             }
1492
1493             if (p_data->disc_res.num_uuids != 0)
1494             {
1495                 /* Also write this to the NVRAM */
1496                 ret = btif_storage_set_remote_device_property(&bd_addr, &prop);
1497                 ASSERTC(ret == BT_STATUS_SUCCESS, "storing remote services failed", ret);
1498                 /* Send the event to the BTIF */
1499                 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1500                                  BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1501             }
1502         }
1503         break;
1504
1505         case BTA_DM_DISC_CMPL_EVT:
1506             /* fixme */
1507         break;
1508
1509 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1510         case BTA_DM_DISC_BLE_RES_EVT:
1511              BTIF_TRACE_DEBUG("%s:, services 0x%x)", __FUNCTION__,
1512                                 p_data->disc_ble_res.service.uu.uuid16);
1513              bt_uuid_t  uuid;
1514              int i = 0;
1515              int j = 15;
1516              if (p_data->disc_ble_res.service.uu.uuid16 == UUID_SERVCLASS_LE_HID)
1517              {
1518                 BTIF_TRACE_DEBUG("%s: Found HOGP UUID",__FUNCTION__);
1519                 bt_property_t prop;
1520                 bt_bdaddr_t bd_addr;
1521                 char temp[256];
1522                 bt_status_t ret;
1523
1524                 bta_gatt_convert_uuid16_to_uuid128(uuid.uu,p_data->disc_ble_res.service.uu.uuid16);
1525
1526                 while(i < j )
1527                 {
1528                     unsigned char c = uuid.uu[j];
1529                     uuid.uu[j] = uuid.uu[i];
1530                     uuid.uu[i] = c;
1531                     i++;
1532                     j--;
1533                 }
1534
1535                 uuid_to_string_legacy(&uuid, temp);
1536                 LOG_INFO(LOG_TAG, "%s uuid:%s", __func__, temp);
1537
1538                 bdcpy(bd_addr.address, p_data->disc_ble_res.bd_addr);
1539                 prop.type = BT_PROPERTY_UUIDS;
1540                 prop.val = uuid.uu;
1541                 prop.len = MAX_UUID_SIZE;
1542
1543                 /* Also write this to the NVRAM */
1544                 ret = btif_storage_set_remote_device_property(&bd_addr, &prop);
1545                 ASSERTC(ret == BT_STATUS_SUCCESS, "storing remote services failed", ret);
1546
1547                 /* Send the event to the BTIF */
1548                 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1549                                  BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1550
1551             }
1552         break;
1553 #endif /* BLE_INCLUDED */
1554
1555         default:
1556         {
1557             ASSERTC(0, "unhandled search services event", event);
1558         }
1559         break;
1560     }
1561 }
1562
1563 /*******************************************************************************
1564 **
1565 ** Function         btif_dm_remote_service_record_evt
1566 **
1567 ** Description      Executes search service record event in btif context
1568 **
1569 ** Returns          void
1570 **
1571 *******************************************************************************/
1572 static void btif_dm_remote_service_record_evt(UINT16 event, char *p_param)
1573 {
1574     tBTA_DM_SEARCH *p_data = (tBTA_DM_SEARCH*)p_param;
1575
1576     BTIF_TRACE_EVENT("%s:  event = %d", __FUNCTION__, event);
1577     switch (event)
1578     {
1579         case BTA_DM_DISC_RES_EVT:
1580         {
1581             bt_service_record_t rec;
1582             bt_property_t prop;
1583             bt_bdaddr_t bd_addr;
1584
1585             memset(&rec, 0, sizeof(bt_service_record_t));
1586             bdcpy(bd_addr.address, p_data->disc_res.bd_addr);
1587
1588             BTIF_TRACE_DEBUG("%s:(result=0x%x, services 0x%x)", __FUNCTION__,
1589                     p_data->disc_res.result, p_data->disc_res.services);
1590             prop.type = BT_PROPERTY_SERVICE_RECORD;
1591             prop.val = (void*)&rec;
1592             prop.len = sizeof(rec);
1593
1594             /* disc_res.result is overloaded with SCN. Cannot check result */
1595             p_data->disc_res.services &= ~BTA_USER_SERVICE_MASK;
1596             /* TODO: Get the UUID as well */
1597             rec.channel = p_data->disc_res.result - 3;
1598             /* TODO: Need to get the service name using p_raw_data */
1599             rec.name[0] = 0;
1600
1601             HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1602                              BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1603         }
1604         break;
1605
1606         default:
1607         {
1608            ASSERTC(0, "unhandled remote service record event", event);
1609         }
1610         break;
1611     }
1612 }
1613
1614 /*******************************************************************************
1615 **
1616 ** Function         btif_dm_upstreams_cback
1617 **
1618 ** Description      Executes UPSTREAMS events in btif context
1619 **
1620 ** Returns          void
1621 **
1622 *******************************************************************************/
1623 static void btif_dm_upstreams_evt(UINT16 event, char* p_param)
1624 {
1625     tBTA_DM_SEC *p_data = (tBTA_DM_SEC*)p_param;
1626     tBTA_SERVICE_MASK service_mask;
1627     uint32_t i;
1628     bt_bdaddr_t bd_addr;
1629
1630     BTIF_TRACE_EVENT("btif_dm_upstreams_cback  ev: %s", dump_dm_event(event));
1631
1632     switch (event)
1633     {
1634         case BTA_DM_ENABLE_EVT:
1635         {
1636              BD_NAME bdname;
1637              bt_status_t status;
1638              bt_property_t prop;
1639              prop.type = BT_PROPERTY_BDNAME;
1640              prop.len = BD_NAME_LEN;
1641              prop.val = (void*)bdname;
1642
1643              status = btif_storage_get_adapter_property(&prop);
1644              if (status == BT_STATUS_SUCCESS)
1645              {
1646                  /* A name exists in the storage. Make this the device name */
1647                  BTA_DmSetDeviceName((char*)prop.val);
1648              }
1649              else
1650              {
1651                  /* Storage does not have a name yet.
1652                   * Use the default name and write it to the chip
1653                   */
1654                  BTA_DmSetDeviceName(btif_get_default_local_name());
1655              }
1656
1657 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1658              /* Enable local privacy */
1659              BTA_DmBleConfigLocalPrivacy(BLE_LOCAL_PRIVACY_ENABLED);
1660 #endif
1661
1662              /* for each of the enabled services in the mask, trigger the profile
1663               * enable */
1664              service_mask = btif_get_enabled_services_mask();
1665              for (i=0; i <= BTA_MAX_SERVICE_ID; i++)
1666              {
1667                  if (service_mask &
1668                      (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i)))
1669                  {
1670                      btif_in_execute_service_request(i, TRUE);
1671                  }
1672              }
1673              /* clear control blocks */
1674              memset(&pairing_cb, 0, sizeof(btif_dm_pairing_cb_t));
1675              pairing_cb.bond_type = BOND_TYPE_PERSISTENT;
1676
1677              /* This function will also trigger the adapter_properties_cb
1678              ** and bonded_devices_info_cb
1679              */
1680              btif_storage_load_bonded_devices();
1681
1682              btif_storage_load_autopair_device_list();
1683
1684              btif_enable_bluetooth_evt(p_data->enable.status);
1685         }
1686         break;
1687
1688         case BTA_DM_DISABLE_EVT:
1689             /* for each of the enabled services in the mask, trigger the profile
1690              * disable */
1691             service_mask = btif_get_enabled_services_mask();
1692             for (i=0; i <= BTA_MAX_SERVICE_ID; i++)
1693             {
1694                 if (service_mask &
1695                     (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i)))
1696                 {
1697                     btif_in_execute_service_request(i, FALSE);
1698                 }
1699             }
1700             btif_disable_bluetooth_evt();
1701             break;
1702
1703         case BTA_DM_PIN_REQ_EVT:
1704             btif_dm_pin_req_evt(&p_data->pin_req);
1705             break;
1706
1707         case BTA_DM_AUTH_CMPL_EVT:
1708             btif_dm_auth_cmpl_evt(&p_data->auth_cmpl);
1709             break;
1710
1711         case BTA_DM_BOND_CANCEL_CMPL_EVT:
1712             if (pairing_cb.state == BT_BOND_STATE_BONDING)
1713             {
1714                 bdcpy(bd_addr.address, pairing_cb.bd_addr);
1715                 btm_set_bond_type_dev(pairing_cb.bd_addr, BOND_TYPE_UNKNOWN);
1716                 bond_state_changed(p_data->bond_cancel_cmpl.result, &bd_addr, BT_BOND_STATE_NONE);
1717             }
1718             break;
1719
1720         case BTA_DM_SP_CFM_REQ_EVT:
1721             btif_dm_ssp_cfm_req_evt(&p_data->cfm_req);
1722             break;
1723         case BTA_DM_SP_KEY_NOTIF_EVT:
1724             btif_dm_ssp_key_notif_evt(&p_data->key_notif);
1725             break;
1726
1727         case BTA_DM_DEV_UNPAIRED_EVT:
1728             bdcpy(bd_addr.address, p_data->link_down.bd_addr);
1729             btm_set_bond_type_dev(p_data->link_down.bd_addr, BOND_TYPE_UNKNOWN);
1730
1731             /*special handling for HID devices */
1732             #if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE))
1733             btif_hh_remove_device(bd_addr);
1734             #endif
1735             btif_storage_remove_bonded_device(&bd_addr);
1736             bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE);
1737             break;
1738
1739         case BTA_DM_BUSY_LEVEL_EVT:
1740         {
1741
1742             if (p_data->busy_level.level_flags & BTM_BL_INQUIRY_PAGING_MASK)
1743             {
1744                 if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_STARTED)
1745                 {
1746                        HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
1747                                                 BT_DISCOVERY_STARTED);
1748                        btif_dm_inquiry_in_progress = TRUE;
1749                 }
1750                 else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_CANCELLED)
1751                 {
1752                        HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
1753                                                 BT_DISCOVERY_STOPPED);
1754                        btif_dm_inquiry_in_progress = FALSE;
1755                 }
1756                 else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_COMPLETE)
1757                 {
1758                        btif_dm_inquiry_in_progress = FALSE;
1759                 }
1760             }
1761         }break;
1762
1763         case BTA_DM_LINK_UP_EVT:
1764             bdcpy(bd_addr.address, p_data->link_up.bd_addr);
1765             BTIF_TRACE_DEBUG("BTA_DM_LINK_UP_EVT. Sending BT_ACL_STATE_CONNECTED");
1766
1767             btif_update_remote_version_property(&bd_addr);
1768
1769             HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS,
1770                       &bd_addr, BT_ACL_STATE_CONNECTED);
1771             break;
1772
1773         case BTA_DM_LINK_DOWN_EVT:
1774             bdcpy(bd_addr.address, p_data->link_down.bd_addr);
1775             btm_set_bond_type_dev(p_data->link_down.bd_addr, BOND_TYPE_UNKNOWN);
1776             BTIF_TRACE_DEBUG("BTA_DM_LINK_DOWN_EVT. Sending BT_ACL_STATE_DISCONNECTED");
1777             HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS,
1778                       &bd_addr, BT_ACL_STATE_DISCONNECTED);
1779             break;
1780
1781         case BTA_DM_HW_ERROR_EVT:
1782             BTIF_TRACE_ERROR("Received H/W Error. ");
1783             /* Flush storage data */
1784             btif_config_flush();
1785             usleep(100000); /* 100milliseconds */
1786             /* Killing the process to force a restart as part of fault tolerance */
1787             kill(getpid(), SIGKILL);
1788             break;
1789
1790 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1791         case BTA_DM_BLE_KEY_EVT:
1792             BTIF_TRACE_DEBUG("BTA_DM_BLE_KEY_EVT key_type=0x%02x ", p_data->ble_key.key_type);
1793
1794             /* If this pairing is by-product of local initiated GATT client Read or Write,
1795             BTA would not have sent BTA_DM_BLE_SEC_REQ_EVT event and Bond state would not
1796             have setup properly. Setup pairing_cb and notify App about Bonding state now*/
1797             if (pairing_cb.state != BT_BOND_STATE_BONDING)
1798             {
1799                 BTIF_TRACE_DEBUG("Bond state not sent to App so far.Notify the app now");
1800                 bond_state_changed(BT_STATUS_SUCCESS, (bt_bdaddr_t*)p_data->ble_key.bd_addr,
1801                                    BT_BOND_STATE_BONDING);
1802             }
1803             else if (memcmp (pairing_cb.bd_addr, p_data->ble_key.bd_addr, BD_ADDR_LEN)!=0)
1804             {
1805                 BTIF_TRACE_ERROR("BD mismatch discard BLE key_type=%d ",p_data->ble_key.key_type);
1806                 break;
1807             }
1808
1809             switch (p_data->ble_key.key_type)
1810             {
1811                 case BTA_LE_KEY_PENC:
1812                     BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PENC");
1813                     pairing_cb.ble.is_penc_key_rcvd = TRUE;
1814                     pairing_cb.ble.penc_key = p_data->ble_key.p_key_value->penc_key;
1815                     break;
1816
1817                 case BTA_LE_KEY_PID:
1818                     BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PID");
1819                     pairing_cb.ble.is_pid_key_rcvd = TRUE;
1820                     pairing_cb.ble.pid_key = p_data->ble_key.p_key_value->pid_key;
1821                     break;
1822
1823                 case BTA_LE_KEY_PCSRK:
1824                     BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PCSRK");
1825                     pairing_cb.ble.is_pcsrk_key_rcvd = TRUE;
1826                     pairing_cb.ble.pcsrk_key = p_data->ble_key.p_key_value->pcsrk_key;
1827                     break;
1828
1829                 case BTA_LE_KEY_LENC:
1830                     BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LENC");
1831                     pairing_cb.ble.is_lenc_key_rcvd = TRUE;
1832                     pairing_cb.ble.lenc_key = p_data->ble_key.p_key_value->lenc_key;
1833                     break;
1834
1835                 case BTA_LE_KEY_LCSRK:
1836                     BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LCSRK");
1837                     pairing_cb.ble.is_lcsrk_key_rcvd = TRUE;
1838                     pairing_cb.ble.lcsrk_key = p_data->ble_key.p_key_value->lcsrk_key;
1839                     break;
1840
1841                 case BTA_LE_KEY_LID:
1842                     BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LID");
1843                     pairing_cb.ble.is_lidk_key_rcvd =  TRUE;
1844                     break;
1845
1846                 default:
1847                     BTIF_TRACE_ERROR("unknown BLE key type (0x%02x)", p_data->ble_key.key_type);
1848                     break;
1849             }
1850             break;
1851         case BTA_DM_BLE_SEC_REQ_EVT:
1852             BTIF_TRACE_DEBUG("BTA_DM_BLE_SEC_REQ_EVT. ");
1853             btif_dm_ble_sec_req_evt(&p_data->ble_req);
1854             break;
1855         case BTA_DM_BLE_PASSKEY_NOTIF_EVT:
1856             BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_NOTIF_EVT. ");
1857             btif_dm_ble_key_notif_evt(&p_data->key_notif);
1858             break;
1859         case BTA_DM_BLE_PASSKEY_REQ_EVT:
1860             BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_REQ_EVT. ");
1861             btif_dm_ble_passkey_req_evt(&p_data->pin_req);
1862             break;
1863         case BTA_DM_BLE_NC_REQ_EVT:
1864             BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_REQ_EVT. ");
1865             btif_dm_ble_key_nc_req_evt(&p_data->key_notif);
1866             break;
1867         case BTA_DM_BLE_OOB_REQ_EVT:
1868             BTIF_TRACE_DEBUG("BTA_DM_BLE_OOB_REQ_EVT. ");
1869             break;
1870         case BTA_DM_BLE_LOCAL_IR_EVT:
1871             BTIF_TRACE_DEBUG("BTA_DM_BLE_LOCAL_IR_EVT. ");
1872             ble_local_key_cb.is_id_keys_rcvd = TRUE;
1873             memcpy(&ble_local_key_cb.id_keys.irk[0],
1874                    &p_data->ble_id_keys.irk[0], sizeof(BT_OCTET16));
1875             memcpy(&ble_local_key_cb.id_keys.ir[0],
1876                    &p_data->ble_id_keys.ir[0], sizeof(BT_OCTET16));
1877             memcpy(&ble_local_key_cb.id_keys.dhk[0],
1878                    &p_data->ble_id_keys.dhk[0], sizeof(BT_OCTET16));
1879             btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.irk[0],
1880                                             BTIF_DM_LE_LOCAL_KEY_IRK,
1881                                             BT_OCTET16_LEN);
1882             btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.ir[0],
1883                                             BTIF_DM_LE_LOCAL_KEY_IR,
1884                                             BT_OCTET16_LEN);
1885             btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.dhk[0],
1886                                             BTIF_DM_LE_LOCAL_KEY_DHK,
1887                                             BT_OCTET16_LEN);
1888             break;
1889         case BTA_DM_BLE_LOCAL_ER_EVT:
1890             BTIF_TRACE_DEBUG("BTA_DM_BLE_LOCAL_ER_EVT. ");
1891             ble_local_key_cb.is_er_rcvd = TRUE;
1892             memcpy(&ble_local_key_cb.er[0], &p_data->ble_er[0], sizeof(BT_OCTET16));
1893             btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.er[0],
1894                                             BTIF_DM_LE_LOCAL_KEY_ER,
1895                                             BT_OCTET16_LEN);
1896             break;
1897
1898         case BTA_DM_BLE_AUTH_CMPL_EVT:
1899             BTIF_TRACE_DEBUG("BTA_DM_BLE_AUTH_CMPL_EVT. ");
1900             btif_dm_ble_auth_cmpl_evt(&p_data->auth_cmpl);
1901             break;
1902
1903         case BTA_DM_LE_FEATURES_READ:
1904         {
1905             tBTM_BLE_VSC_CB cmn_vsc_cb;
1906             bt_local_le_features_t local_le_features;
1907             char buf[512];
1908             bt_property_t prop;
1909             prop.type = BT_PROPERTY_LOCAL_LE_FEATURES;
1910             prop.val = (void*)buf;
1911             prop.len = sizeof(buf);
1912
1913            /* LE features are not stored in storage. Should be retrived from stack */
1914             BTM_BleGetVendorCapabilities(&cmn_vsc_cb);
1915             local_le_features.local_privacy_enabled = BTM_BleLocalPrivacyEnabled();
1916
1917             prop.len = sizeof (bt_local_le_features_t);
1918             if (cmn_vsc_cb.filter_support == 1)
1919                 local_le_features.max_adv_filter_supported = cmn_vsc_cb.max_filter;
1920              else
1921                 local_le_features.max_adv_filter_supported = 0;
1922             local_le_features.max_adv_instance = cmn_vsc_cb.adv_inst_max;
1923             local_le_features.max_irk_list_size = cmn_vsc_cb.max_irk_list_sz;
1924             local_le_features.rpa_offload_supported = cmn_vsc_cb.rpa_offloading;
1925             local_le_features.activity_energy_info_supported = cmn_vsc_cb.energy_support;
1926             local_le_features.scan_result_storage_size = cmn_vsc_cb.tot_scan_results_strg;
1927             local_le_features.version_supported = cmn_vsc_cb.version_supported;
1928             local_le_features.total_trackable_advertisers =
1929                         cmn_vsc_cb.total_trackable_advertisers;
1930
1931             local_le_features.extended_scan_support = cmn_vsc_cb.extended_scan_support > 0;
1932             local_le_features.debug_logging_supported = cmn_vsc_cb.debug_logging_supported > 0;
1933
1934             memcpy(prop.val, &local_le_features, prop.len);
1935             HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, BT_STATUS_SUCCESS, 1, &prop);
1936             break;
1937          }
1938
1939         case BTA_DM_ENER_INFO_READ:
1940         {
1941             btif_activity_energy_info_cb_t *p_ener_data = (btif_activity_energy_info_cb_t*) p_param;
1942             bt_activity_energy_info energy_info;
1943             energy_info.status = p_ener_data->status;
1944             energy_info.ctrl_state = p_ener_data->ctrl_state;
1945             energy_info.rx_time = p_ener_data->rx_time;
1946             energy_info.tx_time = p_ener_data->tx_time;
1947             energy_info.idle_time = p_ener_data->idle_time;
1948             energy_info.energy_used = p_ener_data->energy_used;
1949             HAL_CBACK(bt_hal_cbacks, energy_info_cb, &energy_info);
1950             break;
1951         }
1952 #endif
1953
1954         case BTA_DM_AUTHORIZE_EVT:
1955         case BTA_DM_SIG_STRENGTH_EVT:
1956         case BTA_DM_SP_RMT_OOB_EVT:
1957         case BTA_DM_SP_KEYPRESS_EVT:
1958         case BTA_DM_ROLE_CHG_EVT:
1959
1960         default:
1961             BTIF_TRACE_WARNING( "btif_dm_cback : unhandled event (%d)", event );
1962             break;
1963     }
1964
1965     btif_dm_data_free(event, p_data);
1966 }
1967
1968 /*******************************************************************************
1969 **
1970 ** Function         btif_dm_generic_evt
1971 **
1972 ** Description      Executes non-BTA upstream events in BTIF context
1973 **
1974 ** Returns          void
1975 **
1976 *******************************************************************************/
1977 static void btif_dm_generic_evt(UINT16 event, char* p_param)
1978 {
1979     BTIF_TRACE_EVENT("%s: event=%d", __FUNCTION__, event);
1980     switch(event)
1981     {
1982         case BTIF_DM_CB_DISCOVERY_STARTED:
1983         {
1984             HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STARTED);
1985         }
1986         break;
1987
1988         case BTIF_DM_CB_CREATE_BOND:
1989         {
1990             pairing_cb.timeout_retries = NUM_TIMEOUT_RETRIES;
1991             btif_dm_create_bond_cb_t *create_bond_cb = (btif_dm_create_bond_cb_t*)p_param;
1992             btif_dm_cb_create_bond(&create_bond_cb->bdaddr, create_bond_cb->transport);
1993         }
1994         break;
1995
1996         case BTIF_DM_CB_REMOVE_BOND:
1997         {
1998             btif_dm_cb_remove_bond((bt_bdaddr_t *)p_param);
1999         }
2000         break;
2001
2002         case BTIF_DM_CB_HID_REMOTE_NAME:
2003         {
2004             btif_dm_cb_hid_remote_name((tBTM_REMOTE_DEV_NAME *)p_param);
2005         }
2006         break;
2007
2008         case BTIF_DM_CB_BOND_STATE_BONDING:
2009             {
2010                 bond_state_changed(BT_STATUS_SUCCESS, (bt_bdaddr_t *)p_param, BT_BOND_STATE_BONDING);
2011             }
2012             break;
2013         case BTIF_DM_CB_LE_TX_TEST:
2014         case BTIF_DM_CB_LE_RX_TEST:
2015             {
2016                 uint8_t status;
2017                 STREAM_TO_UINT8(status, p_param);
2018                 HAL_CBACK(bt_hal_cbacks, le_test_mode_cb,
2019                       (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, 0);
2020             }
2021             break;
2022         case BTIF_DM_CB_LE_TEST_END:
2023             {
2024                 uint8_t status;
2025                 uint16_t count = 0;
2026                 STREAM_TO_UINT8(status, p_param);
2027                 if (status == 0)
2028                     STREAM_TO_UINT16(count, p_param);
2029                 HAL_CBACK(bt_hal_cbacks, le_test_mode_cb,
2030                       (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, count);
2031             }
2032             break;
2033         default:
2034         {
2035             BTIF_TRACE_WARNING("%s : Unknown event 0x%x", __FUNCTION__, event);
2036         }
2037         break;
2038     }
2039 }
2040
2041 /*******************************************************************************
2042 **
2043 ** Function         bte_dm_evt
2044 **
2045 ** Description      Switches context from BTE to BTIF for all DM events
2046 **
2047 ** Returns          void
2048 **
2049 *******************************************************************************/
2050
2051 void bte_dm_evt(tBTA_DM_SEC_EVT event, tBTA_DM_SEC *p_data)
2052 {
2053     /* switch context to btif task context (copy full union size for convenience) */
2054     bt_status_t status = btif_transfer_context(btif_dm_upstreams_evt, (uint16_t)event,
2055                                 (void*)p_data, sizeof(tBTA_DM_SEC), btif_dm_data_copy);
2056
2057     /* catch any failed context transfers */
2058     ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status);
2059 }
2060
2061 /*******************************************************************************
2062 **
2063 ** Function         bte_search_devices_evt
2064 **
2065 ** Description      Switches context from BTE to BTIF for DM search events
2066 **
2067 ** Returns          void
2068 **
2069 *******************************************************************************/
2070 static void bte_search_devices_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
2071 {
2072     UINT16 param_len = 0;
2073
2074     if (p_data)
2075         param_len += sizeof(tBTA_DM_SEARCH);
2076     /* Allocate buffer to hold the pointers (deep copy). The pointers will point to the end of the tBTA_DM_SEARCH */
2077     switch (event)
2078     {
2079         case BTA_DM_INQ_RES_EVT:
2080         {
2081             if (p_data->inq_res.p_eir)
2082                 param_len += HCI_EXT_INQ_RESPONSE_LEN;
2083         }
2084         break;
2085
2086         case BTA_DM_DISC_RES_EVT:
2087         {
2088             if (p_data->disc_res.raw_data_size && p_data->disc_res.p_raw_data)
2089                 param_len += p_data->disc_res.raw_data_size;
2090         }
2091         break;
2092     }
2093     BTIF_TRACE_DEBUG("%s event=%s param_len=%d", __FUNCTION__, dump_dm_search_event(event), param_len);
2094
2095     /* if remote name is available in EIR, set teh flag so that stack doesnt trigger RNR */
2096     if (event == BTA_DM_INQ_RES_EVT)
2097         p_data->inq_res.remt_name_not_required = check_eir_remote_name(p_data, NULL, NULL);
2098
2099     btif_transfer_context (btif_dm_search_devices_evt , (UINT16) event, (void *)p_data, param_len,
2100         (param_len > sizeof(tBTA_DM_SEARCH)) ? search_devices_copy_cb : NULL);
2101 }
2102
2103 /*******************************************************************************
2104 **
2105 ** Function         bte_dm_search_services_evt
2106 **
2107 ** Description      Switches context from BTE to BTIF for DM search services
2108 **                  event
2109 **
2110 ** Returns          void
2111 **
2112 *******************************************************************************/
2113 static void bte_dm_search_services_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
2114 {
2115     UINT16 param_len = 0;
2116    if (p_data)
2117        param_len += sizeof(tBTA_DM_SEARCH);
2118    switch (event)
2119    {
2120          case BTA_DM_DISC_RES_EVT:
2121          {
2122              if ((p_data->disc_res.result == BTA_SUCCESS) && (p_data->disc_res.num_uuids > 0)) {
2123                   param_len += (p_data->disc_res.num_uuids * MAX_UUID_SIZE);
2124              }
2125          } break;
2126    }
2127    /* TODO: The only other member that needs a deep copy is the p_raw_data. But not sure
2128     * if raw_data is needed. */
2129    btif_transfer_context(btif_dm_search_services_evt, event, (char*)p_data, param_len,
2130          (param_len > sizeof(tBTA_DM_SEARCH)) ? search_services_copy_cb : NULL);
2131 }
2132
2133 /*******************************************************************************
2134 **
2135 ** Function         bte_dm_remote_service_record_evt
2136 **
2137 ** Description      Switches context from BTE to BTIF for DM search service
2138 **                  record event
2139 **
2140 ** Returns          void
2141 **
2142 *******************************************************************************/
2143 static void bte_dm_remote_service_record_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
2144 {
2145    /* TODO: The only member that needs a deep copy is the p_raw_data. But not sure yet if this is needed. */
2146    btif_transfer_context(btif_dm_remote_service_record_evt, event, (char*)p_data, sizeof(tBTA_DM_SEARCH), NULL);
2147 }
2148
2149 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2150 /*******************************************************************************
2151 **
2152 ** Function         bta_energy_info_cb
2153 **
2154 ** Description      Switches context from BTE to BTIF for DM energy info event
2155 **
2156 ** Returns          void
2157 **
2158 *******************************************************************************/
2159 static void bta_energy_info_cb(tBTA_DM_BLE_TX_TIME_MS tx_time, tBTA_DM_BLE_RX_TIME_MS rx_time,
2160                                     tBTA_DM_BLE_IDLE_TIME_MS idle_time,
2161                                     tBTA_DM_BLE_ENERGY_USED energy_used,
2162                                     tBTA_DM_CONTRL_STATE ctrl_state, tBTA_STATUS status)
2163 {
2164     BTIF_TRACE_DEBUG("energy_info_cb-Status:%d,state=%d,tx_t=%ld, rx_t=%ld, idle_time=%ld,used=%ld",
2165         status, ctrl_state, tx_time, rx_time, idle_time, energy_used);
2166
2167     btif_activity_energy_info_cb_t btif_cb;
2168     btif_cb.status = status;
2169     btif_cb.ctrl_state = ctrl_state;
2170     btif_cb.tx_time = (uint64_t) tx_time;
2171     btif_cb.rx_time = (uint64_t) rx_time;
2172     btif_cb.idle_time =(uint64_t) idle_time;
2173     btif_cb.energy_used =(uint64_t) energy_used;
2174     btif_transfer_context(btif_dm_upstreams_evt, BTA_DM_ENER_INFO_READ,
2175                           (char*) &btif_cb, sizeof(btif_activity_energy_info_cb_t), NULL);
2176 }
2177 #endif
2178
2179 /*******************************************************************************
2180 **
2181 ** Function         bte_scan_filt_param_cfg_evt
2182 **
2183 ** Description      Scan filter param config event
2184 **
2185 ** Returns          void
2186 **
2187 *******************************************************************************/
2188 static void bte_scan_filt_param_cfg_evt(UINT8 action_type,
2189                                         tBTA_DM_BLE_PF_AVBL_SPACE avbl_space,
2190                                         tBTA_DM_BLE_REF_VALUE ref_value, tBTA_STATUS status)
2191 {
2192     /* This event occurs on calling BTA_DmBleCfgFilterCondition internally,
2193     ** and that is why there is no HAL callback
2194     */
2195     if(BTA_SUCCESS != status)
2196     {
2197         BTIF_TRACE_ERROR("%s, %d", __FUNCTION__, status);
2198     }
2199     else
2200     {
2201         BTIF_TRACE_DEBUG("%s", __FUNCTION__);
2202     }
2203 }
2204
2205 /*****************************************************************************
2206 **
2207 **   btif api functions (no context switch)
2208 **
2209 *****************************************************************************/
2210
2211 /*******************************************************************************
2212 **
2213 ** Function         btif_dm_start_discovery
2214 **
2215 ** Description      Start device discovery/inquiry
2216 **
2217 ** Returns          bt_status_t
2218 **
2219 *******************************************************************************/
2220 bt_status_t btif_dm_start_discovery(void)
2221 {
2222     tBTA_DM_INQ inq_params;
2223     tBTA_SERVICE_MASK services = 0;
2224     tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param;
2225
2226     BTIF_TRACE_EVENT("%s", __FUNCTION__);
2227
2228 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2229     memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS));
2230     /* Cleanup anything remaining on index 0 */
2231     BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, &adv_filt_param, NULL,
2232                              bte_scan_filt_param_cfg_evt, 0);
2233
2234     /* Add an allow-all filter on index 0*/
2235     adv_filt_param.dely_mode = IMMEDIATE_DELY_MODE;
2236     adv_filt_param.feat_seln = ALLOW_ALL_FILTER;
2237     adv_filt_param.filt_logic_type = BTA_DM_BLE_PF_FILT_LOGIC_OR;
2238     adv_filt_param.list_logic_type = BTA_DM_BLE_PF_LIST_LOGIC_OR;
2239     adv_filt_param.rssi_low_thres = LOWEST_RSSI_VALUE;
2240     adv_filt_param.rssi_high_thres = LOWEST_RSSI_VALUE;
2241     BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_ADD, 0, &adv_filt_param, NULL,
2242                              bte_scan_filt_param_cfg_evt, 0);
2243
2244     /* TODO: Do we need to handle multiple inquiries at the same time? */
2245
2246     /* Set inquiry params and call API */
2247     inq_params.mode = BTA_DM_GENERAL_INQUIRY|BTA_BLE_GENERAL_INQUIRY;
2248 #if (defined(BTA_HOST_INTERLEAVE_SEARCH) && BTA_HOST_INTERLEAVE_SEARCH == TRUE)
2249     inq_params.intl_duration[0]= BTIF_DM_INTERLEAVE_DURATION_BR_ONE;
2250     inq_params.intl_duration[1]= BTIF_DM_INTERLEAVE_DURATION_LE_ONE;
2251     inq_params.intl_duration[2]= BTIF_DM_INTERLEAVE_DURATION_BR_TWO;
2252     inq_params.intl_duration[3]= BTIF_DM_INTERLEAVE_DURATION_LE_TWO;
2253 #endif
2254 #else
2255     inq_params.mode = BTA_DM_GENERAL_INQUIRY;
2256 #endif
2257     inq_params.duration = BTIF_DM_DEFAULT_INQ_MAX_DURATION;
2258
2259     inq_params.max_resps = BTIF_DM_DEFAULT_INQ_MAX_RESULTS;
2260     inq_params.report_dup = TRUE;
2261
2262     inq_params.filter_type = BTA_DM_INQ_CLR;
2263     /* TODO: Filter device by BDA needs to be implemented here */
2264
2265     /* Will be enabled to TRUE once inquiry busy level has been received */
2266     btif_dm_inquiry_in_progress = FALSE;
2267     /* find nearby devices */
2268     BTA_DmSearch(&inq_params, services, bte_search_devices_evt);
2269
2270     return BT_STATUS_SUCCESS;
2271 }
2272
2273 /*******************************************************************************
2274 **
2275 ** Function         btif_dm_cancel_discovery
2276 **
2277 ** Description      Cancels search
2278 **
2279 ** Returns          bt_status_t
2280 **
2281 *******************************************************************************/
2282 bt_status_t btif_dm_cancel_discovery(void)
2283 {
2284     BTIF_TRACE_EVENT("%s", __FUNCTION__);
2285     BTA_DmSearchCancel();
2286     return BT_STATUS_SUCCESS;
2287 }
2288
2289 /*******************************************************************************
2290 **
2291 ** Function         btif_dm_create_bond
2292 **
2293 ** Description      Initiate bonding with the specified device
2294 **
2295 ** Returns          bt_status_t
2296 **
2297 *******************************************************************************/
2298 bt_status_t btif_dm_create_bond(const bt_bdaddr_t *bd_addr, int transport)
2299 {
2300     btif_dm_create_bond_cb_t create_bond_cb;
2301     create_bond_cb.transport = transport;
2302     bdcpy(create_bond_cb.bdaddr.address, bd_addr->address);
2303
2304     bdstr_t bdstr;
2305     BTIF_TRACE_EVENT("%s: bd_addr=%s, transport=%d", __FUNCTION__, bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr)), transport);
2306     if (pairing_cb.state != BT_BOND_STATE_NONE)
2307         return BT_STATUS_BUSY;
2308
2309     btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_CREATE_BOND,
2310                           (char *)&create_bond_cb, sizeof(btif_dm_create_bond_cb_t), NULL);
2311
2312     return BT_STATUS_SUCCESS;
2313 }
2314
2315 /*******************************************************************************
2316 **
2317 ** Function         btif_dm_cancel_bond
2318 **
2319 ** Description      Initiate bonding with the specified device
2320 **
2321 ** Returns          bt_status_t
2322 **
2323 *******************************************************************************/
2324
2325 bt_status_t btif_dm_cancel_bond(const bt_bdaddr_t *bd_addr)
2326 {
2327     bdstr_t bdstr;
2328
2329     BTIF_TRACE_EVENT("%s: bd_addr=%s", __FUNCTION__, bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr)));
2330
2331     /* TODO:
2332     **  1. Restore scan modes
2333     **  2. special handling for HID devices
2334     */
2335     if (pairing_cb.state == BT_BOND_STATE_BONDING)
2336     {
2337
2338 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2339
2340         if (pairing_cb.is_ssp)
2341         {
2342             if (pairing_cb.is_le_only)
2343             {
2344                 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_PAIR_NOT_SPT);
2345             }
2346             else
2347             {
2348                 BTA_DmConfirm( (UINT8 *)bd_addr->address, FALSE);
2349                 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2350                 btif_storage_remove_bonded_device((bt_bdaddr_t *)bd_addr);
2351             }
2352         }
2353         else
2354         {
2355             if (pairing_cb.is_le_only)
2356             {
2357                 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2358             }
2359             else
2360             {
2361                 BTA_DmPinReply( (UINT8 *)bd_addr->address, FALSE, 0, NULL);
2362             }
2363         /* Cancel bonding, in case it is in ACL connection setup state */
2364         BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2365         }
2366
2367 #else
2368         if (pairing_cb.is_ssp)
2369         {
2370             BTA_DmConfirm( (UINT8 *)bd_addr->address, FALSE);
2371         }
2372         else
2373         {
2374             BTA_DmPinReply( (UINT8 *)bd_addr->address, FALSE, 0, NULL);
2375         }
2376         /* Cancel bonding, in case it is in ACL connection setup state */
2377         BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2378         btif_storage_remove_bonded_device((bt_bdaddr_t *)bd_addr);
2379 #endif
2380     }
2381
2382     return BT_STATUS_SUCCESS;
2383 }
2384
2385 /*******************************************************************************
2386 **
2387 ** Function         btif_dm_hh_open_failed
2388 **
2389 ** Description      informs the upper layers if the HH have failed during bonding
2390 **
2391 ** Returns          none
2392 **
2393 *******************************************************************************/
2394
2395 void btif_dm_hh_open_failed(bt_bdaddr_t *bdaddr)
2396 {
2397     if (pairing_cb.state == BT_BOND_STATE_BONDING &&
2398             bdcmp(bdaddr->address, pairing_cb.bd_addr) == 0)
2399     {
2400         bond_state_changed(BT_STATUS_FAIL, bdaddr, BT_BOND_STATE_NONE);
2401     }
2402 }
2403
2404 /*******************************************************************************
2405 **
2406 ** Function         btif_dm_remove_bond
2407 **
2408 ** Description      Removes bonding with the specified device
2409 **
2410 ** Returns          bt_status_t
2411 **
2412 *******************************************************************************/
2413
2414 bt_status_t btif_dm_remove_bond(const bt_bdaddr_t *bd_addr)
2415 {
2416     bdstr_t bdstr;
2417
2418     BTIF_TRACE_EVENT("%s: bd_addr=%s", __FUNCTION__, bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr)));
2419     btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_REMOVE_BOND,
2420                           (char *)bd_addr, sizeof(bt_bdaddr_t), NULL);
2421
2422     return BT_STATUS_SUCCESS;
2423 }
2424
2425 /*******************************************************************************
2426 **
2427 ** Function         btif_dm_pin_reply
2428 **
2429 ** Description      BT legacy pairing - PIN code reply
2430 **
2431 ** Returns          bt_status_t
2432 **
2433 *******************************************************************************/
2434
2435 bt_status_t btif_dm_pin_reply( const bt_bdaddr_t *bd_addr, uint8_t accept,
2436                                uint8_t pin_len, bt_pin_code_t *pin_code)
2437 {
2438     BTIF_TRACE_EVENT("%s: accept=%d", __FUNCTION__, accept);
2439     if (pin_code == NULL)
2440         return BT_STATUS_FAIL;
2441 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2442
2443     if (pairing_cb.is_le_only)
2444     {
2445         int i;
2446         UINT32 passkey = 0;
2447         int multi[] = {100000, 10000, 1000, 100, 10,1};
2448         BD_ADDR remote_bd_addr;
2449         bdcpy(remote_bd_addr, bd_addr->address);
2450         for (i = 0; i < 6; i++)
2451         {
2452             passkey += (multi[i] * (pin_code->pin[i] - '0'));
2453         }
2454         BTIF_TRACE_DEBUG("btif_dm_pin_reply: passkey: %d", passkey);
2455         BTA_DmBlePasskeyReply(remote_bd_addr, accept, passkey);
2456
2457     }
2458     else
2459     {
2460         BTA_DmPinReply( (UINT8 *)bd_addr->address, accept, pin_len, pin_code->pin);
2461         if (accept)
2462             pairing_cb.pin_code_len = pin_len;
2463     }
2464 #else
2465     BTA_DmPinReply( (UINT8 *)bd_addr->address, accept, pin_len, pin_code->pin);
2466
2467     if (accept)
2468         pairing_cb.pin_code_len = pin_len;
2469 #endif
2470     return BT_STATUS_SUCCESS;
2471 }
2472
2473 /*******************************************************************************
2474 **
2475 ** Function         btif_dm_ssp_reply
2476 **
2477 ** Description      BT SSP Reply - Just Works, Numeric Comparison & Passkey Entry
2478 **
2479 ** Returns          bt_status_t
2480 **
2481 *******************************************************************************/
2482 bt_status_t btif_dm_ssp_reply(const bt_bdaddr_t *bd_addr,
2483                                  bt_ssp_variant_t variant, uint8_t accept,
2484                                  uint32_t passkey)
2485 {
2486     UNUSED(passkey);
2487
2488     if (variant == BT_SSP_VARIANT_PASSKEY_ENTRY)
2489     {
2490         /* This is not implemented in the stack.
2491          * For devices with display, this is not needed
2492         */
2493         BTIF_TRACE_WARNING("%s: Not implemented", __FUNCTION__);
2494         return BT_STATUS_FAIL;
2495     }
2496     /* BT_SSP_VARIANT_CONSENT & BT_SSP_VARIANT_PASSKEY_CONFIRMATION supported */
2497     BTIF_TRACE_EVENT("%s: accept=%d", __FUNCTION__, accept);
2498 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2499     if (pairing_cb.is_le_only)
2500     {
2501         if(pairing_cb.is_le_nc)
2502         {
2503             BTA_DmBleConfirmReply((UINT8 *)bd_addr->address,accept);
2504         } else {
2505             if (accept)
2506                 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_GRANTED);
2507             else
2508                 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_PAIR_NOT_SPT);
2509         }
2510     } else {
2511         BTA_DmConfirm( (UINT8 *)bd_addr->address, accept);
2512     }
2513 #else
2514     BTA_DmConfirm( (UINT8 *)bd_addr->address, accept);
2515 #endif
2516     return BT_STATUS_SUCCESS;
2517 }
2518
2519 /*******************************************************************************
2520 **
2521 ** Function         btif_dm_get_adapter_property
2522 **
2523 ** Description     Queries the BTA for the adapter property
2524 **
2525 ** Returns          bt_status_t
2526 **
2527 *******************************************************************************/
2528 bt_status_t btif_dm_get_adapter_property(bt_property_t *prop)
2529 {
2530     BTIF_TRACE_EVENT("%s: type=0x%x", __FUNCTION__, prop->type);
2531     switch (prop->type)
2532     {
2533         case BT_PROPERTY_BDNAME:
2534         {
2535             bt_bdname_t *bd_name = (bt_bdname_t*)prop->val;
2536             strncpy((char *)bd_name->name, (char *)btif_get_default_local_name(),
2537                    sizeof(bd_name->name) - 1);
2538             bd_name->name[sizeof(bd_name->name) - 1] = 0;
2539             prop->len = strlen((char *)bd_name->name);
2540         }
2541         break;
2542
2543         case BT_PROPERTY_ADAPTER_SCAN_MODE:
2544         {
2545             /* if the storage does not have it. Most likely app never set it. Default is NONE */
2546             bt_scan_mode_t *mode = (bt_scan_mode_t*)prop->val;
2547             *mode = BT_SCAN_MODE_NONE;
2548             prop->len = sizeof(bt_scan_mode_t);
2549         }
2550         break;
2551
2552         case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
2553         {
2554             uint32_t *tmt = (uint32_t*)prop->val;
2555             *tmt = 120; /* default to 120s, if not found in NV */
2556             prop->len = sizeof(uint32_t);
2557         }
2558         break;
2559
2560         default:
2561             prop->len = 0;
2562             return BT_STATUS_FAIL;
2563     }
2564     return BT_STATUS_SUCCESS;
2565 }
2566
2567 /*******************************************************************************
2568 **
2569 ** Function         btif_dm_get_remote_services
2570 **
2571 ** Description      Start SDP to get remote services
2572 **
2573 ** Returns          bt_status_t
2574 **
2575 *******************************************************************************/
2576 bt_status_t btif_dm_get_remote_services(bt_bdaddr_t *remote_addr)
2577 {
2578     bdstr_t bdstr;
2579
2580     BTIF_TRACE_EVENT("%s: remote_addr=%s", __FUNCTION__, bdaddr_to_string(remote_addr, bdstr, sizeof(bdstr)));
2581
2582     BTA_DmDiscover(remote_addr->address, BTA_ALL_SERVICE_MASK,
2583                    bte_dm_search_services_evt, TRUE);
2584
2585     return BT_STATUS_SUCCESS;
2586 }
2587
2588 /*******************************************************************************
2589 **
2590 ** Function         btif_dm_get_remote_services_transport
2591 **
2592 ** Description      Start SDP to get remote services by transport
2593 **
2594 ** Returns          bt_status_t
2595 **
2596 *******************************************************************************/
2597 bt_status_t btif_dm_get_remote_services_by_transport(bt_bdaddr_t *remote_addr, const int transport)
2598 {
2599     BTIF_TRACE_EVENT("%s", __func__);
2600
2601     /* Set the mask extension */
2602     tBTA_SERVICE_MASK_EXT mask_ext;
2603     mask_ext.num_uuid = 0;
2604     mask_ext.p_uuid = NULL;
2605     mask_ext.srvc_mask = BTA_ALL_SERVICE_MASK;
2606
2607     BTA_DmDiscoverByTransport(remote_addr->address, &mask_ext,
2608                    bte_dm_search_services_evt, TRUE, transport);
2609
2610     return BT_STATUS_SUCCESS;
2611 }
2612
2613 /*******************************************************************************
2614 **
2615 ** Function         btif_dm_get_remote_service_record
2616 **
2617 ** Description      Start SDP to get remote service record
2618 **
2619 **
2620 ** Returns          bt_status_t
2621 *******************************************************************************/
2622 bt_status_t btif_dm_get_remote_service_record(bt_bdaddr_t *remote_addr,
2623                                                     bt_uuid_t *uuid)
2624 {
2625     tSDP_UUID sdp_uuid;
2626     bdstr_t bdstr;
2627
2628     BTIF_TRACE_EVENT("%s: remote_addr=%s", __FUNCTION__, bdaddr_to_string(remote_addr, bdstr, sizeof(bdstr)));
2629
2630     sdp_uuid.len = MAX_UUID_SIZE;
2631     memcpy(sdp_uuid.uu.uuid128, uuid->uu, MAX_UUID_SIZE);
2632
2633     BTA_DmDiscoverUUID(remote_addr->address, &sdp_uuid,
2634                        bte_dm_remote_service_record_evt, TRUE);
2635
2636     return BT_STATUS_SUCCESS;
2637 }
2638
2639 void btif_dm_execute_service_request(UINT16 event, char *p_param)
2640 {
2641     BOOLEAN b_enable = FALSE;
2642     bt_status_t status;
2643     if (event == BTIF_DM_ENABLE_SERVICE)
2644     {
2645         b_enable = TRUE;
2646     }
2647     status = btif_in_execute_service_request(*((tBTA_SERVICE_ID*)p_param), b_enable);
2648     if (status == BT_STATUS_SUCCESS)
2649     {
2650         bt_property_t property;
2651         bt_uuid_t local_uuids[BT_MAX_NUM_UUIDS];
2652
2653         /* Now send the UUID_PROPERTY_CHANGED event to the upper layer */
2654         BTIF_STORAGE_FILL_PROPERTY(&property, BT_PROPERTY_UUIDS,
2655                                     sizeof(local_uuids), local_uuids);
2656         btif_storage_get_adapter_property(&property);
2657         HAL_CBACK(bt_hal_cbacks, adapter_properties_cb,
2658                           BT_STATUS_SUCCESS, 1, &property);
2659     }
2660     return;
2661 }
2662
2663 void btif_dm_proc_io_req(BD_ADDR bd_addr, tBTA_IO_CAP *p_io_cap, tBTA_OOB_DATA *p_oob_data,
2664                       tBTA_AUTH_REQ *p_auth_req, BOOLEAN is_orig)
2665 {
2666     UINT8   yes_no_bit = BTA_AUTH_SP_YES & *p_auth_req;
2667     /* if local initiated:
2668     **      1. set DD + MITM
2669     ** if remote initiated:
2670     **      1. Copy over the auth_req from peer's io_rsp
2671     **      2. Set the MITM if peer has it set or if peer has DisplayYesNo (iPhone)
2672     ** as a fallback set MITM+GB if peer had MITM set
2673     */
2674     UNUSED (bd_addr);
2675     UNUSED (p_io_cap);
2676     UNUSED (p_oob_data);
2677
2678     BTIF_TRACE_DEBUG("+%s: p_auth_req=%d", __FUNCTION__, *p_auth_req);
2679     if(pairing_cb.is_local_initiated)
2680     {
2681         /* if initing/responding to a dedicated bonding, use dedicate bonding bit */
2682         *p_auth_req = BTA_AUTH_DD_BOND | BTA_AUTH_SP_YES;
2683     }
2684     else if (!is_orig)
2685     {
2686         /* peer initiated paring. They probably know what they want.
2687         ** Copy the mitm from peer device.
2688         */
2689         BTIF_TRACE_DEBUG("%s: setting p_auth_req to peer's: %d",
2690                 __FUNCTION__, pairing_cb.auth_req);
2691         *p_auth_req = (pairing_cb.auth_req & BTA_AUTH_BONDS);
2692
2693         /* copy over the MITM bit as well. In addition if the peer has DisplayYesNo, force MITM */
2694         if ((yes_no_bit) || (pairing_cb.io_cap & BTM_IO_CAP_IO) )
2695             *p_auth_req |= BTA_AUTH_SP_YES;
2696     }
2697     else if (yes_no_bit)
2698     {
2699         /* set the general bonding bit for stored device */
2700         *p_auth_req = BTA_AUTH_GEN_BOND | yes_no_bit;
2701     }
2702     BTIF_TRACE_DEBUG("-%s: p_auth_req=%d", __FUNCTION__, *p_auth_req);
2703 }
2704
2705 void btif_dm_proc_io_rsp(BD_ADDR bd_addr, tBTA_IO_CAP io_cap,
2706                       tBTA_OOB_DATA oob_data, tBTA_AUTH_REQ auth_req)
2707 {
2708     UNUSED (bd_addr);
2709     UNUSED (oob_data);
2710
2711     if(auth_req & BTA_AUTH_BONDS)
2712     {
2713         BTIF_TRACE_DEBUG("%s auth_req:%d", __FUNCTION__, auth_req);
2714         pairing_cb.auth_req = auth_req;
2715         pairing_cb.io_cap = io_cap;
2716     }
2717 }
2718
2719 void btif_dm_set_oob_for_io_req(tBTA_OOB_DATA  *p_oob_data)
2720 {
2721     if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2722         oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 )
2723     {
2724         *p_oob_data = FALSE;
2725     }
2726     else
2727     {
2728         *p_oob_data = TRUE;
2729     }
2730     BTIF_TRACE_DEBUG("btif_dm_set_oob_for_io_req *p_oob_data=%d", *p_oob_data);
2731 }
2732
2733 #ifdef BTIF_DM_OOB_TEST
2734 void btif_dm_load_local_oob(void)
2735 {
2736   /**
2737    * TODO(armansito): On OSs other than Android, the sys/properties.h system
2738    * does not exist. Remove this conditional include once we have a generic way
2739    * to obtain system properties.
2740    */
2741 #if !defined(OS_GENERIC)
2742     char prop_oob[PROPERTY_VALUE_MAX];
2743     property_get("service.brcm.bt.oob", prop_oob, "3");
2744     BTIF_TRACE_DEBUG("btif_dm_load_local_oob prop_oob = %s",prop_oob);
2745     if (prop_oob[0] != '3')
2746     {
2747         if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2748             oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 )
2749         {
2750             BTIF_TRACE_DEBUG("btif_dm_load_local_oob: read OOB, call BTA_DmLocalOob()");
2751             BTA_DmLocalOob();
2752         }
2753     }
2754 #endif  /* !defined(OS_GENERIC) */
2755 }
2756
2757 void btif_dm_proc_loc_oob(BOOLEAN valid, BT_OCTET16 c, BT_OCTET16 r)
2758 {
2759   /**
2760    * TODO(armansito): On OSs other than Android, the sys/properties.h system
2761    * does not exist. Remove this conditional include once we have a generic way
2762    * to obtain system properties.
2763    */
2764 #if !defined(OS_GENERIC)
2765     FILE *fp;
2766     char *path_a = "/data/misc/bluedroid/LOCAL/a.key";
2767     char *path_b = "/data/misc/bluedroid/LOCAL/b.key";
2768     char *path = NULL;
2769     char prop_oob[PROPERTY_VALUE_MAX];
2770     BTIF_TRACE_DEBUG("btif_dm_proc_loc_oob: valid=%d", valid);
2771     if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2772         oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 &&
2773         valid)
2774     {
2775         BTIF_TRACE_DEBUG("save local OOB data in memory");
2776         memcpy(oob_cb.sp_c, c, BT_OCTET16_LEN);
2777         memcpy(oob_cb.sp_r, r, BT_OCTET16_LEN);
2778         property_get("service.brcm.bt.oob", prop_oob, "3");
2779         BTIF_TRACE_DEBUG("btif_dm_proc_loc_oob prop_oob = %s",prop_oob);
2780         if (prop_oob[0] == '1')
2781             path = path_a;
2782         else if (prop_oob[0] == '2')
2783             path = path_b;
2784         if (path)
2785         {
2786             fp = fopen(path, "wb+");
2787             if (fp == NULL)
2788             {
2789                 BTIF_TRACE_DEBUG("btif_dm_proc_loc_oob: failed to save local OOB data to %s", path);
2790             }
2791             else
2792             {
2793                 BTIF_TRACE_DEBUG("btif_dm_proc_loc_oob: save local OOB data into file %s",path);
2794                 fwrite (c , 1 , BT_OCTET16_LEN , fp );
2795                 fwrite (r , 1 , BT_OCTET16_LEN , fp );
2796                 fclose(fp);
2797             }
2798         }
2799     }
2800 #endif  /* !defined(OS_GENERIC) */
2801 }
2802
2803 BOOLEAN btif_dm_proc_rmt_oob(BD_ADDR bd_addr,  BT_OCTET16 p_c, BT_OCTET16 p_r)
2804 {
2805   /**
2806    * TODO(armansito): On OSs other than Android, the sys/properties.h system
2807    * does not exist. Remove this conditional include once we have a generic way
2808    * to obtain system properties.
2809    */
2810 #if !defined(OS_GENERIC)
2811     char t[128];
2812     FILE *fp;
2813     char *path_a = "/data/misc/bluedroid/LOCAL/a.key";
2814     char *path_b = "/data/misc/bluedroid/LOCAL/b.key";
2815     char *path = NULL;
2816     char prop_oob[PROPERTY_VALUE_MAX];
2817     BOOLEAN result = FALSE;
2818     bt_bdaddr_t bt_bd_addr;
2819     bdcpy(oob_cb.oob_bdaddr, bd_addr);
2820     property_get("service.brcm.bt.oob", prop_oob, "3");
2821     BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob prop_oob = %s",prop_oob);
2822     if (prop_oob[0] == '1')
2823         path = path_b;
2824     else if (prop_oob[0] == '2')
2825         path = path_a;
2826     if (path)
2827     {
2828         fp = fopen(path, "rb");
2829         if (fp == NULL)
2830         {
2831             BTIF_TRACE_DEBUG("btapp_dm_rmt_oob_reply: failed to read OOB keys from %s",path);
2832             return FALSE;
2833         }
2834         else
2835         {
2836             BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob: read OOB data from %s",path);
2837             fread (p_c , 1 , BT_OCTET16_LEN , fp );
2838             fread (p_r , 1 , BT_OCTET16_LEN , fp );
2839             fclose(fp);
2840         }
2841         BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: TRUE");
2842         sprintf(t, "%02x:%02x:%02x:%02x:%02x:%02x",
2843                 oob_cb.oob_bdaddr[0], oob_cb.oob_bdaddr[1], oob_cb.oob_bdaddr[2],
2844                 oob_cb.oob_bdaddr[3], oob_cb.oob_bdaddr[4], oob_cb.oob_bdaddr[5]);
2845         BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: peer_bdaddr = %s", t);
2846         sprintf(t, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
2847                 p_c[0], p_c[1], p_c[2],  p_c[3],  p_c[4],  p_c[5],  p_c[6],  p_c[7],
2848                 p_c[8], p_c[9], p_c[10], p_c[11], p_c[12], p_c[13], p_c[14], p_c[15]);
2849         BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: c = %s",t);
2850         sprintf(t, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
2851                 p_r[0], p_r[1], p_r[2],  p_r[3],  p_r[4],  p_r[5],  p_r[6],  p_r[7],
2852                 p_r[8], p_r[9], p_r[10], p_r[11], p_r[12], p_r[13], p_r[14], p_r[15]);
2853         BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: r = %s",t);
2854         bdcpy(bt_bd_addr.address, bd_addr);
2855         btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_BOND_STATE_BONDING,
2856                               (char *)&bt_bd_addr, sizeof(bt_bdaddr_t), NULL);
2857         result = TRUE;
2858     }
2859     BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob result=%d",result);
2860     return result;
2861 #else  /* defined(OS_GENERIC) */
2862     return FALSE;
2863 #endif  /* !defined(OS_GENERIC) */
2864 }
2865 #endif /*  BTIF_DM_OOB_TEST */
2866 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2867
2868 static void btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif)
2869 {
2870     bt_bdaddr_t bd_addr;
2871     bt_bdname_t bd_name;
2872     UINT32 cod;
2873     int dev_type;
2874
2875     BTIF_TRACE_DEBUG("%s", __FUNCTION__);
2876
2877     /* Remote name update */
2878     if (!btif_get_device_type(p_ssp_key_notif->bd_addr, &dev_type))
2879     {
2880         dev_type = BT_DEVICE_TYPE_BLE;
2881     }
2882     btif_dm_update_ble_remote_properties(p_ssp_key_notif->bd_addr , p_ssp_key_notif->bd_name,
2883                                          (tBT_DEVICE_TYPE) dev_type);
2884     bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr);
2885     memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN);
2886
2887     bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
2888     pairing_cb.is_ssp = FALSE;
2889     cod = COD_UNCLASSIFIED;
2890
2891     HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name,
2892               cod, BT_SSP_VARIANT_PASSKEY_NOTIFICATION,
2893               p_ssp_key_notif->passkey);
2894 }
2895
2896 /*******************************************************************************
2897 **
2898 ** Function         btif_dm_ble_auth_cmpl_evt
2899 **
2900 ** Description      Executes authentication complete event in btif context
2901 **
2902 ** Returns          void
2903 **
2904 *******************************************************************************/
2905 static void btif_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl)
2906 {
2907     /* Save link key, if not temporary */
2908     bt_bdaddr_t bd_addr;
2909     bt_status_t status = BT_STATUS_FAIL;
2910     bt_bond_state_t state = BT_BOND_STATE_NONE;
2911
2912     bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
2913     if ( (p_auth_cmpl->success == TRUE) && (p_auth_cmpl->key_present) )
2914     {
2915         /* store keys */
2916     }
2917     if (p_auth_cmpl->success)
2918     {
2919         status = BT_STATUS_SUCCESS;
2920         state = BT_BOND_STATE_BONDED;
2921         int addr_type;
2922         bt_bdaddr_t bdaddr;
2923         bdcpy(bdaddr.address, p_auth_cmpl->bd_addr);
2924         if (btif_storage_get_remote_addr_type(&bdaddr, &addr_type) != BT_STATUS_SUCCESS)
2925             btif_storage_set_remote_addr_type(&bdaddr, p_auth_cmpl->addr_type);
2926
2927         /* Test for temporary bonding */
2928         if (btm_get_bond_type_dev(p_auth_cmpl->bd_addr) == BOND_TYPE_TEMPORARY) {
2929             BTIF_TRACE_DEBUG("%s: sending BT_BOND_STATE_NONE for Temp pairing",
2930                              __func__);
2931             btif_storage_remove_bonded_device(&bdaddr);
2932             state = BT_BOND_STATE_NONE;
2933         } else {
2934             btif_dm_save_ble_bonding_keys();
2935             BTA_GATTC_Refresh(bd_addr.address);
2936             btif_dm_get_remote_services_by_transport(&bd_addr, BTA_GATT_TRANSPORT_LE);
2937         }
2938     }
2939     else
2940     {
2941         /*Map the HCI fail reason  to  bt status  */
2942         switch (p_auth_cmpl->fail_reason)
2943         {
2944             case BTA_DM_AUTH_SMP_PAIR_AUTH_FAIL:
2945             case BTA_DM_AUTH_SMP_CONFIRM_VALUE_FAIL:
2946                 btif_dm_remove_ble_bonding_keys();
2947                 status = BT_STATUS_AUTH_FAILURE;
2948                 break;
2949             case BTA_DM_AUTH_SMP_PAIR_NOT_SUPPORT:
2950                 status = BT_STATUS_AUTH_REJECTED;
2951                 break;
2952             default:
2953                 btif_dm_remove_ble_bonding_keys();
2954                 status =  BT_STATUS_FAIL;
2955                 break;
2956         }
2957     }
2958     bond_state_changed(status, &bd_addr, state);
2959 }
2960
2961 void    btif_dm_load_ble_local_keys(void)
2962 {
2963     memset(&ble_local_key_cb, 0, sizeof(btif_dm_local_key_cb_t));
2964
2965     if (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_ER,(char*)&ble_local_key_cb.er[0],
2966                                        BT_OCTET16_LEN)== BT_STATUS_SUCCESS)
2967     {
2968         ble_local_key_cb.is_er_rcvd = TRUE;
2969         BTIF_TRACE_DEBUG("%s BLE ER key loaded",__FUNCTION__ );
2970     }
2971
2972     if ((btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IR,(char*)&ble_local_key_cb.id_keys.ir[0],
2973                                         BT_OCTET16_LEN)== BT_STATUS_SUCCESS )&&
2974         (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IRK, (char*)&ble_local_key_cb.id_keys.irk[0],
2975                                         BT_OCTET16_LEN)== BT_STATUS_SUCCESS)&&
2976         (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_DHK,(char*)&ble_local_key_cb.id_keys.dhk[0],
2977                                         BT_OCTET16_LEN)== BT_STATUS_SUCCESS))
2978     {
2979         ble_local_key_cb.is_id_keys_rcvd = TRUE;
2980         BTIF_TRACE_DEBUG("%s BLE ID keys loaded",__FUNCTION__ );
2981     }
2982
2983 }
2984 void    btif_dm_get_ble_local_keys(tBTA_DM_BLE_LOCAL_KEY_MASK *p_key_mask, BT_OCTET16 er,
2985                                    tBTA_BLE_LOCAL_ID_KEYS *p_id_keys)
2986 {
2987     if (ble_local_key_cb.is_er_rcvd )
2988     {
2989         memcpy(&er[0], &ble_local_key_cb.er[0], sizeof(BT_OCTET16));
2990         *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ER;
2991     }
2992
2993     if (ble_local_key_cb.is_id_keys_rcvd)
2994     {
2995         memcpy(&p_id_keys->ir[0], &ble_local_key_cb.id_keys.ir[0], sizeof(BT_OCTET16));
2996         memcpy(&p_id_keys->irk[0],  &ble_local_key_cb.id_keys.irk[0], sizeof(BT_OCTET16));
2997         memcpy(&p_id_keys->dhk[0],  &ble_local_key_cb.id_keys.dhk[0], sizeof(BT_OCTET16));
2998         *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ID;
2999     }
3000     BTIF_TRACE_DEBUG("%s  *p_key_mask=0x%02x",__FUNCTION__,   *p_key_mask);
3001 }
3002
3003 void btif_dm_save_ble_bonding_keys(void)
3004 {
3005
3006     bt_bdaddr_t bd_addr;
3007
3008     BTIF_TRACE_DEBUG("%s",__FUNCTION__ );
3009
3010     bdcpy(bd_addr.address, pairing_cb.bd_addr);
3011
3012     if (pairing_cb.ble.is_penc_key_rcvd)
3013     {
3014         btif_storage_add_ble_bonding_key(&bd_addr,
3015                                          (char *) &pairing_cb.ble.penc_key,
3016                                          BTIF_DM_LE_KEY_PENC,
3017                                          sizeof(tBTM_LE_PENC_KEYS));
3018     }
3019
3020     if (pairing_cb.ble.is_pid_key_rcvd)
3021     {
3022         btif_storage_add_ble_bonding_key(&bd_addr,
3023                                          (char *) &pairing_cb.ble.pid_key,
3024                                          BTIF_DM_LE_KEY_PID,
3025                                          sizeof(tBTM_LE_PID_KEYS));
3026     }
3027
3028     if (pairing_cb.ble.is_pcsrk_key_rcvd)
3029     {
3030         btif_storage_add_ble_bonding_key(&bd_addr,
3031                                          (char *) &pairing_cb.ble.pcsrk_key,
3032                                          BTIF_DM_LE_KEY_PCSRK,
3033                                          sizeof(tBTM_LE_PCSRK_KEYS));
3034     }
3035
3036     if (pairing_cb.ble.is_lenc_key_rcvd)
3037     {
3038         btif_storage_add_ble_bonding_key(&bd_addr,
3039                                          (char *) &pairing_cb.ble.lenc_key,
3040                                          BTIF_DM_LE_KEY_LENC,
3041                                          sizeof(tBTM_LE_LENC_KEYS));
3042     }
3043
3044     if (pairing_cb.ble.is_lcsrk_key_rcvd)
3045     {
3046         btif_storage_add_ble_bonding_key(&bd_addr,
3047                                          (char *) &pairing_cb.ble.lcsrk_key,
3048                                          BTIF_DM_LE_KEY_LCSRK,
3049                                          sizeof(tBTM_LE_LCSRK_KEYS));
3050     }
3051
3052     if (pairing_cb.ble.is_lidk_key_rcvd)
3053     {
3054         btif_storage_add_ble_bonding_key(&bd_addr,
3055                                          NULL,
3056                                          BTIF_DM_LE_KEY_LID,
3057                                          0);
3058     }
3059
3060 }
3061
3062 void btif_dm_remove_ble_bonding_keys(void)
3063 {
3064     bt_bdaddr_t bd_addr;
3065
3066     BTIF_TRACE_DEBUG("%s",__FUNCTION__ );
3067
3068     bdcpy(bd_addr.address, pairing_cb.bd_addr);
3069     btif_storage_remove_ble_bonding_keys(&bd_addr);
3070 }
3071
3072 /*******************************************************************************
3073 **
3074 ** Function         btif_dm_ble_sec_req_evt
3075 **
3076 ** Description      Eprocess security request event in btif context
3077 **
3078 ** Returns          void
3079 **
3080 *******************************************************************************/
3081 void btif_dm_ble_sec_req_evt(tBTA_DM_BLE_SEC_REQ *p_ble_req)
3082 {
3083     bt_bdaddr_t bd_addr;
3084     bt_bdname_t bd_name;
3085     UINT32 cod;
3086     int dev_type;
3087
3088     BTIF_TRACE_DEBUG("%s", __FUNCTION__);
3089
3090     if (pairing_cb.state == BT_BOND_STATE_BONDING)
3091     {
3092         BTIF_TRACE_DEBUG("%s Discard security request", __FUNCTION__);
3093         return;
3094     }
3095
3096     /* Remote name update */
3097     if (!btif_get_device_type(p_ble_req->bd_addr, &dev_type))
3098     {
3099         dev_type = BT_DEVICE_TYPE_BLE;
3100     }
3101     btif_dm_update_ble_remote_properties(p_ble_req->bd_addr, p_ble_req->bd_name,
3102                                          (tBT_DEVICE_TYPE) dev_type);
3103
3104     bdcpy(bd_addr.address, p_ble_req->bd_addr);
3105     memcpy(bd_name.name, p_ble_req->bd_name, BD_NAME_LEN);
3106
3107     bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
3108
3109     pairing_cb.bond_type = BOND_TYPE_PERSISTENT;
3110     pairing_cb.is_le_only = TRUE;
3111     pairing_cb.is_le_nc = FALSE;
3112     pairing_cb.is_ssp = TRUE;
3113     btm_set_bond_type_dev(p_ble_req->bd_addr, pairing_cb.bond_type);
3114
3115     cod = COD_UNCLASSIFIED;
3116
3117     HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod,
3118               BT_SSP_VARIANT_CONSENT, 0);
3119 }
3120
3121 /*******************************************************************************
3122 **
3123 ** Function         btif_dm_ble_passkey_req_evt
3124 **
3125 ** Description      Executes pin request event in btif context
3126 **
3127 ** Returns          void
3128 **
3129 *******************************************************************************/
3130 static void btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ *p_pin_req)
3131 {
3132     bt_bdaddr_t bd_addr;
3133     bt_bdname_t bd_name;
3134     UINT32 cod;
3135     int dev_type;
3136
3137     /* Remote name update */
3138     if (!btif_get_device_type(p_pin_req->bd_addr, &dev_type))
3139     {
3140         dev_type = BT_DEVICE_TYPE_BLE;
3141     }
3142     btif_dm_update_ble_remote_properties(p_pin_req->bd_addr,p_pin_req->bd_name,
3143                                          (tBT_DEVICE_TYPE) dev_type);
3144
3145     bdcpy(bd_addr.address, p_pin_req->bd_addr);
3146     memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN);
3147
3148     bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
3149     pairing_cb.is_le_only = TRUE;
3150
3151     cod = COD_UNCLASSIFIED;
3152
3153     HAL_CBACK(bt_hal_cbacks, pin_request_cb,
3154               &bd_addr, &bd_name, cod, FALSE);
3155 }
3156 static void btif_dm_ble_key_nc_req_evt(tBTA_DM_SP_KEY_NOTIF *p_notif_req)
3157 {
3158     /* TODO implement key notification for numeric comparison */
3159     BTIF_TRACE_DEBUG("%s", __FUNCTION__);
3160
3161     /* Remote name update */
3162     btif_update_remote_properties(p_notif_req->bd_addr , p_notif_req->bd_name,
3163                                           NULL, BT_DEVICE_TYPE_BLE);
3164
3165     bt_bdaddr_t bd_addr;
3166     bdcpy(bd_addr.address, p_notif_req->bd_addr);
3167
3168     bt_bdname_t bd_name;
3169     memcpy(bd_name.name, p_notif_req->bd_name, BD_NAME_LEN);
3170
3171     bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
3172     pairing_cb.is_ssp = FALSE;
3173     pairing_cb.is_le_only = TRUE;
3174     pairing_cb.is_le_nc = TRUE;
3175
3176     HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name,
3177               COD_UNCLASSIFIED, BT_SSP_VARIANT_PASSKEY_CONFIRMATION,
3178               p_notif_req->passkey);
3179 }
3180
3181 void btif_dm_update_ble_remote_properties( BD_ADDR bd_addr, BD_NAME bd_name,
3182                                            tBT_DEVICE_TYPE dev_type)
3183 {
3184    btif_update_remote_properties(bd_addr,bd_name,NULL,dev_type);
3185 }
3186
3187 static void btif_dm_ble_tx_test_cback(void *p)
3188 {
3189     btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TX_TEST,
3190                           (char *)p, 1, NULL);
3191 }
3192
3193 static void btif_dm_ble_rx_test_cback(void *p)
3194 {
3195     btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_RX_TEST,
3196                           (char *)p, 1, NULL);
3197 }
3198
3199 static void btif_dm_ble_test_end_cback(void *p)
3200 {
3201     btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TEST_END,
3202                           (char *)p, 3, NULL);
3203 }
3204 /*******************************************************************************
3205 **
3206 ** Function         btif_le_test_mode
3207 **
3208 ** Description     Sends a HCI BLE Test command to the Controller
3209 **
3210 ** Returns          BT_STATUS_SUCCESS on success
3211 **
3212 *******************************************************************************/
3213 bt_status_t btif_le_test_mode(uint16_t opcode, uint8_t *buf, uint8_t len)
3214 {
3215      switch (opcode) {
3216          case HCI_BLE_TRANSMITTER_TEST:
3217              if (len != 3) return BT_STATUS_PARM_INVALID;
3218              BTM_BleTransmitterTest(buf[0],buf[1],buf[2], btif_dm_ble_tx_test_cback);
3219              break;
3220          case HCI_BLE_RECEIVER_TEST:
3221              if (len != 1) return BT_STATUS_PARM_INVALID;
3222              BTM_BleReceiverTest(buf[0], btif_dm_ble_rx_test_cback);
3223              break;
3224          case HCI_BLE_TEST_END:
3225              BTM_BleTestEnd((tBTM_CMPL_CB*) btif_dm_ble_test_end_cback);
3226              break;
3227          default:
3228              BTIF_TRACE_ERROR("%s: Unknown LE Test Mode Command 0x%x", __FUNCTION__, opcode);
3229              return BT_STATUS_UNSUPPORTED;
3230      }
3231      return BT_STATUS_SUCCESS;
3232 }
3233 #endif
3234
3235 void btif_dm_on_disable()
3236 {
3237     /* cancel any pending pairing requests */
3238     if (pairing_cb.state == BT_BOND_STATE_BONDING)
3239     {
3240         bt_bdaddr_t bd_addr;
3241
3242         BTIF_TRACE_DEBUG("%s: Cancel pending pairing request", __FUNCTION__);
3243         bdcpy(bd_addr.address, pairing_cb.bd_addr);
3244         btif_dm_cancel_bond(&bd_addr);
3245     }
3246 }
3247
3248 /*******************************************************************************
3249 **
3250 ** Function         btif_dm_read_energy_info
3251 **
3252 ** Description     Reads the energy info from controller
3253 **
3254 ** Returns         void
3255 **
3256 *******************************************************************************/
3257 void btif_dm_read_energy_info()
3258 {
3259 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
3260     BTA_DmBleGetEnergyInfo(bta_energy_info_cb);
3261 #endif
3262 }
3263
3264 static char* btif_get_default_local_name() {
3265   /**
3266    * TODO(armansito): On OSs other than Android, the sys/properties.h system
3267    * does not exist. Remove this conditional include once we have a generic way
3268    * to obtain system properties.
3269    */
3270 #if !defined(OS_GENERIC)
3271     if (btif_default_local_name[0] == '\0')
3272     {
3273         int max_len = sizeof(btif_default_local_name) - 1;
3274         if (BTM_DEF_LOCAL_NAME[0] != '\0')
3275         {
3276             strncpy(btif_default_local_name, BTM_DEF_LOCAL_NAME, max_len);
3277         }
3278         else
3279         {
3280             char prop_model[PROPERTY_VALUE_MAX];
3281             property_get(PROPERTY_PRODUCT_MODEL, prop_model, "");
3282             strncpy(btif_default_local_name, prop_model, max_len);
3283         }
3284         btif_default_local_name[max_len] = '\0';
3285     }
3286 #endif  /* !defined(OS_GENERIC) */
3287     return btif_default_local_name;
3288 }