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