btif_storage.c revision fbf76881316471faf631dc543089efaeaf72df8d
1ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/******************************************************************************
2ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *
3b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho *  Copyright (C) 2009-2012 Broadcom Corporation
4ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *
5ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *  Licensed under the Apache License, Version 2.0 (the "License");
6ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *  you may not use this file except in compliance with the License.
7ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *  You may obtain a copy of the License at:
8ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *
9ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *  http://www.apache.org/licenses/LICENSE-2.0
10ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *
11ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *  Unless required by applicable law or agreed to in writing, software
1283a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius *  distributed under the License is distributed on an "AS IS" BASIS,
13b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho *  See the License for the specific language governing permissions and
15ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *  limitations under the License.
16ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *
17ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru ******************************************************************************/
18ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
19ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/************************************************************************************
20ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *
21ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *  Filename:      btif_storage.c
22ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *
23ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *  Description:   Stores the local BT adapter and remote device properties in
24ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *                 NVRAM storage, typically as xml file in the
25ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *                 mobile's filesystem
26ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *
27ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *
28ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */
29ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include <stdlib.h>
30ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include <time.h>
31ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include <string.h>
32ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include <ctype.h>
33ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include <alloca.h>
34ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
35ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
36ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include <hardware/bluetooth.h>
37ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "btif_config.h"
38ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define LOG_TAG "BTIF_STORAGE"
39ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
40ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "btif_api.h"
41ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "btif_storage.h"
42ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "btif_util.h"
43ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "bd.h"
44ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "gki.h"
45ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "bta_hh_api.h"
46ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "btif_hh.h"
47ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
48ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include <cutils/log.h>
49ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
50ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/************************************************************************************
51ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru**  Constants & Macros
52ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru************************************************************************************/
53ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
54ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define BTIF_STORAGE_PATH_BLUEDROID "/data/misc/bluedroid"
55ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
56ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru//#define BTIF_STORAGE_PATH_ADAPTER_INFO "adapter_info"
57ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru//#define BTIF_STORAGE_PATH_REMOTE_DEVICES "remote_devices"
58ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define BTIF_STORAGE_PATH_REMOTE_DEVTIME "Timestamp"
59ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define BTIF_STORAGE_PATH_REMOTE_DEVCLASS "DevClass"
60ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define BTIF_STORAGE_PATH_REMOTE_DEVTYPE "DevType"
61ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define BTIF_STORAGE_PATH_REMOTE_NAME "Name"
62ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define BTIF_STORAGE_PATH_REMOTE_VER_MFCT "Manufacturer"
63ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define BTIF_STORAGE_PATH_REMOTE_VER_VER "LmpVer"
64ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define BTIF_STORAGE_PATH_REMOTE_VER_SUBVER "LmpSubVer"
65ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
66ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru//#define BTIF_STORAGE_PATH_REMOTE_LINKKEYS "remote_linkkeys"
67ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define BTIF_STORAGE_PATH_REMOTE_ALIASE "Aliase"
68ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define BTIF_STORAGE_PATH_REMOTE_SERVICE "Service"
69ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define BTIF_STORAGE_PATH_REMOTE_HIDINFO "HidInfo"
70ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define BTIF_STORAGE_KEY_ADAPTER_NAME "Name"
71ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define BTIF_STORAGE_KEY_ADAPTER_SCANMODE "ScanMode"
72ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define BTIF_STORAGE_KEY_ADAPTER_DISC_TIMEOUT "DiscoveryTimeout"
73ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
74ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
75ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define BTIF_AUTO_PAIR_CONF_FILE  "/etc/bluetooth/auto_pair_devlist.conf"
76ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST "AutoPairBlacklist"
77ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_ADDR "AddressBlacklist"
78ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_EXACTNAME "ExactNameBlacklist"
79ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_PARTIALNAME "PartialNameBlacklist"
80ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define BTIF_STORAGE_KEY_AUTOPAIR_FIXPIN_KBLIST "FixedPinZerosKeyboardBlacklist"
81ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define BTIF_STORAGE_KEY_AUTOPAIR_DYNAMIC_BLACKLIST_ADDR "DynamicAddressBlacklist"
82ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
83ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define BTIF_AUTO_PAIR_CONF_VALUE_SEPARATOR ","
8485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#define BTIF_AUTO_PAIR_CONF_SPACE ' '
8585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#define BTIF_AUTO_PAIR_CONF_COMMENT '#'
8685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#define BTIF_AUTO_PAIR_CONF_KEY_VAL_DELIMETER "="
8785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
88ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
89ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/* This is a local property to add a device found */
90ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP 0xFF
9185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
9285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#define BTIF_STORAGE_GET_ADAPTER_PROP(t,v,l,p) \
9385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho      {p.type=t;p.val=v;p.len=l; btif_storage_get_adapter_property(&p);}
9485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
95ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define BTIF_STORAGE_GET_REMOTE_PROP(b,t,v,l,p) \
96ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru      {p.type=t;p.val=v;p.len=l;btif_storage_get_remote_device_property(b,&p);}
97ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
98ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define STORAGE_BDADDR_STRING_SZ           (18)      /* 00:11:22:33:44:55 */
99ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define STORAGE_UUID_STRING_SIZE           (36+1)    /* 00001200-0000-1000-8000-00805f9b34fb; */
100ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define STORAGE_PINLEN_STRING_MAX_SIZE     (2)       /* ascii pinlen max chars */
101ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define STORAGE_KEYTYPE_STRING_MAX_SIZE    (1)       /* ascii keytype max chars */
102ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
103ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define STORAGE_KEY_TYPE_MAX               (10)
104ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
105ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define STORAGE_HID_ATRR_MASK_SIZE           (4)
106ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define STORAGE_HID_SUB_CLASS_SIZE           (2)
107ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define STORAGE_HID_APP_ID_SIZE              (2)
108ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define STORAGE_HID_VENDOR_ID_SIZE           (4)
109ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define STORAGE_HID_PRODUCT_ID_SIZE          (4)
110ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define STORAGE_HID_VERSION_SIZE             (4)
111ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define STORAGE_HID_CTRY_CODE_SIZE           (2)
112ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define STORAGE_HID_DESC_LEN_SIZE            (4)
113ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define STORAGE_HID_DESC_MAX_SIZE            (2*512)
114ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
115ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/* <18 char bd addr> <space> LIST< <36 char uuid> <;> > <keytype (dec)> <pinlen> */
116ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define BTIF_REMOTE_SERVICES_ENTRY_SIZE_MAX (STORAGE_BDADDR_STRING_SZ + 1 +\
117ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                                             STORAGE_UUID_STRING_SIZE*BT_MAX_NUM_UUIDS + \
118ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                                             STORAGE_PINLEN_STRING_MAX_SIZE +\
119ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                                             STORAGE_KEYTYPE_STRING_MAX_SIZE)
120ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
121ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define STORAGE_REMOTE_LINKKEYS_ENTRY_SIZE (LINK_KEY_LEN*2 + 1 + 2 + 1 + 2)
122ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
123ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/* <18 char bd addr> <space>LIST <attr_mask> <space> > <sub_class> <space> <app_id> <space>
124ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                                <vendor_id> <space> > <product_id> <space> <version> <space>
125ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                                <ctry_code> <space> > <desc_len> <space> <desc_list> <space> */
126ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define BTIF_HID_INFO_ENTRY_SIZE_MAX    (STORAGE_BDADDR_STRING_SZ + 1 +\
127ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                                         STORAGE_HID_ATRR_MASK_SIZE + 1 +\
128ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                                         STORAGE_HID_SUB_CLASS_SIZE + 1 +\
129ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                                         STORAGE_HID_APP_ID_SIZE+ 1 +\
130ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                                         STORAGE_HID_VENDOR_ID_SIZE+ 1 +\
131ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                                         STORAGE_HID_PRODUCT_ID_SIZE+ 1 +\
132ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                                         STORAGE_HID_VERSION_SIZE+ 1 +\
133ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                                         STORAGE_HID_CTRY_CODE_SIZE+ 1 +\
134ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                                         STORAGE_HID_DESC_LEN_SIZE+ 1 +\
135ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                                         STORAGE_HID_DESC_MAX_SIZE+ 1 )
136b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho
137b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho
138ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/* currently remote services is the potentially largest entry */
139ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define BTIF_STORAGE_MAX_LINE_SZ BTIF_REMOTE_SERVICES_ENTRY_SIZE_MAX
140ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
141ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
142ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/* check against unv max entry size at compile time */
143ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#if (BTIF_STORAGE_ENTRY_MAX_SIZE > UNV_MAXLINE_LENGTH)
144ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    #error "btif storage entry size exceeds unv max line size"
145b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho#endif
146b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho
147ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
148ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define BTIF_STORAGE_HL_APP          "hl_app"
149ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define BTIF_STORAGE_HL_APP_CB       "hl_app_cb"
150ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define BTIF_STORAGE_HL_APP_DATA     "hl_app_data_"
151ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define BTIF_STORAGE_HL_APP_MDL_DATA "hl_app_mdl_data_"
152ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
153ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/************************************************************************************
154ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru**  Local type definitions
155ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru************************************************************************************/
156ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querutypedef struct
157ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru{
158ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    uint32_t num_devices;
159ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    bt_bdaddr_t devices[BTM_SEC_MAX_DEVICE_RECORDS];
160ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} btif_bonded_devices_t;
161ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
162ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/************************************************************************************
163ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru**  External variables
164ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru************************************************************************************/
165ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruextern UINT16 bta_service_id_to_uuid_lkup_tbl [BTA_MAX_SERVICE_ID];
166ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruextern bt_bdaddr_t btif_local_bd_addr;
167ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
168ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/************************************************************************************
169ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru**  External functions
170ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru************************************************************************************/
171ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
172b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2clairehoextern void btif_gatts_add_bonded_dev_from_nv(BD_ADDR bda);
173ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
17483a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius/************************************************************************************
175ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru**  Internal Functions
17683a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius************************************************************************************/
177ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
178ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querubt_status_t btif_in_fetch_bonded_ble_device(char *remote_bd_addr,int add,
179ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                                              btif_bonded_devices_t *p_bonded_devices);
180ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querubt_status_t btif_storage_get_remote_addr_type(bt_bdaddr_t *remote_bd_addr,
181ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                                              int *addr_type);
182ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
183ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/************************************************************************************
184ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru**  Static functions
185ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru************************************************************************************/
186ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
187ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/*******************************************************************************
188ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru**
189ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru** Function         btif_in_make_filename
190ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru**
191ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru** Description      Internal helper function to create NVRAM file path
192ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru**                  from address and filename
193ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru**
194ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru** Returns          NVRAM file path if successfull, NULL otherwise
195ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru**
196ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*******************************************************************************/
197ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic char* btif_in_make_filename(bt_bdaddr_t *bd_addr, char *fname)
198ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru{
199ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    static char path[256];
200ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    bdstr_t bdstr;
201ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
202ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if (fname == NULL)return NULL;
203ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if (bd_addr)
204ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    {
205ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        sprintf(path, "%s/%s/%s", BTIF_STORAGE_PATH_BLUEDROID,
206ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                bd2str(bd_addr, &bdstr), fname);
207ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
208ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    else
209ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    {
210ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        /* local adapter */
211ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        sprintf(path, "%s/LOCAL/%s", BTIF_STORAGE_PATH_BLUEDROID, fname);
212ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
213ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
214ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return(char*)path;
215ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
216b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho/*******************************************************************************
217ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru**
218ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru** Function         btif_in_split_uuids_string_to_list
219ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru**
220ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru** Description      Internal helper function to split the string of UUIDs
221ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru**                  read from the NVRAM to an array
222ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru**
223ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru** Returns          None
224ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru**
225ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*******************************************************************************/
226ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic void btif_in_split_uuids_string_to_list(char *str, bt_uuid_t *p_uuid,
227ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                                               uint32_t *p_num_uuid)
228ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru{
229ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    char buf[64];
230ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    char *p_start = str;
231ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    char *p_needle;
232ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    uint32_t num = 0;
233ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    do
234ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    {
235ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        //p_needle = strchr(p_start, ';');
236ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        p_needle = strchr(p_start, ' ');
237ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        if (p_needle < p_start) break;
238ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        memset(buf, 0, sizeof(buf));
239ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        strncpy(buf, p_start, (p_needle-p_start));
240ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        string_to_uuid(buf, p_uuid + num);
241ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        num++;
242ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        p_start = ++p_needle;
243ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
244ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    } while (*p_start != 0);
245b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho    *p_num_uuid = num;
24683a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius}
247ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic int prop2cfg(bt_bdaddr_t *remote_bd_addr, bt_property_t *prop)
248ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru{
249ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    bdstr_t bdstr = {0};
250ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(remote_bd_addr)
251ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        bd2str(remote_bd_addr, &bdstr);
252ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    BTIF_TRACE_DEBUG3("in, bd addr:%s, prop type:%d, len:%d", bdstr, prop->type, prop->len);
253ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    char value[1024];
254ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(prop->len <= 0 || prop->len > (int)sizeof(value) - 1)
255ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    {
256ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        BTIF_TRACE_ERROR2("property type:%d, len:%d is invalid", prop->type, prop->len);
257ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        return FALSE;
25883a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius    }
25983a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius    switch(prop->type)
260ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    {
261ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru       case BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP:
262ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            btif_config_set_int("Remote", bdstr,
263ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                                BTIF_STORAGE_PATH_REMOTE_DEVTIME, (int)time(NULL));
264ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            static const char* exclude_filter[] =
265ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                        {"LinkKey", "LE_KEY_PENC", "LE_KEY_PID", "LE_KEY_PCSRK", "LE_KEY_LENC", "LE_KEY_LCSRK"};
266ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            btif_config_filter_remove("Remote", exclude_filter, sizeof(exclude_filter)/sizeof(char*),
267ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                        BTIF_STORAGE_MAX_ALLOWED_REMOTE_DEVICE);
268ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            break;
269ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        case BT_PROPERTY_BDNAME:
270ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            strncpy(value, (char*)prop->val, prop->len);
271ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            value[prop->len]='\0';
272ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            if(remote_bd_addr)
273ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                btif_config_set_str("Remote", bdstr,
274ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                                BTIF_STORAGE_PATH_REMOTE_NAME, value);
275ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            else btif_config_set_str("Local", "Adapter",
276ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                                BTIF_STORAGE_KEY_ADAPTER_NAME, value);
277ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            /* save name immediately */
278ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            btif_config_save();
279ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            break;
280ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        case BT_PROPERTY_REMOTE_FRIENDLY_NAME:
281ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            strncpy(value, (char*)prop->val, prop->len);
282ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            value[prop->len]='\0';
283ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            btif_config_set_str("Remote", bdstr, BTIF_STORAGE_PATH_REMOTE_ALIASE, value);
284ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            break;
285ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        case BT_PROPERTY_ADAPTER_SCAN_MODE:
286ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            btif_config_set_int("Local", "Adapter",
287ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                                BTIF_STORAGE_KEY_ADAPTER_SCANMODE, *(int*)prop->val);
288ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            break;
289ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
290ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            btif_config_set_int("Local", "Adapter",
291ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                                BTIF_STORAGE_KEY_ADAPTER_DISC_TIMEOUT, *(int*)prop->val);
292ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            break;
293ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        case BT_PROPERTY_CLASS_OF_DEVICE:
294ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            btif_config_set_int("Remote", bdstr,
295ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                                BTIF_STORAGE_PATH_REMOTE_DEVCLASS, *(int*)prop->val);
296ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            break;
297ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        case BT_PROPERTY_TYPE_OF_DEVICE:
298ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            btif_config_set_int("Remote", bdstr,
299ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                                BTIF_STORAGE_PATH_REMOTE_DEVTYPE, *(int*)prop->val);
300ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            break;
301ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        case BT_PROPERTY_UUIDS:
302ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        {
303ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            uint32_t i;
304ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            char buf[64];
305ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            value[0] = 0;
306ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            for (i=0; i < (prop->len)/sizeof(bt_uuid_t); i++)
307ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            {
308ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                bt_uuid_t *p_uuid = (bt_uuid_t*)prop->val + i;
309ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                memset(buf, 0, sizeof(buf));
310ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                uuid_to_string(p_uuid, buf);
311ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                strcat(value, buf);
312ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                //strcat(value, ";");
313ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                strcat(value, " ");
314ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            }
315ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            btif_config_set_str("Remote", bdstr, BTIF_STORAGE_PATH_REMOTE_SERVICE, value);
316ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            btif_config_save();
317ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            break;
318ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        }
319ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        case BT_PROPERTY_REMOTE_VERSION_INFO:
320ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        {
321ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            bt_remote_version_t *info = (bt_remote_version_t *)prop->val;
322ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
323ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            if (!info)
324ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                return FALSE;
325ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
326ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            btif_config_set_int("Remote", bdstr,
327ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                                BTIF_STORAGE_PATH_REMOTE_VER_MFCT, info->manufacturer);
328ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            btif_config_set_int("Remote", bdstr,
329ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                                BTIF_STORAGE_PATH_REMOTE_VER_VER, info->version);
330ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            btif_config_set_int("Remote", bdstr,
331ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                                BTIF_STORAGE_PATH_REMOTE_VER_SUBVER, info->sub_ver);
332ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            btif_config_save();
333ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru         } break;
334ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
335ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        default:
336ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru             BTIF_TRACE_ERROR1("Unknow prop type:%d", prop->type);
337ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru             return FALSE;
338ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
339ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return TRUE;
340ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
341ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic int cfg2prop(bt_bdaddr_t *remote_bd_addr, bt_property_t *prop)
342ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru{
343ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    bdstr_t bdstr = {0};
344ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(remote_bd_addr)
345ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        bd2str(remote_bd_addr, &bdstr);
346ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    BTIF_TRACE_DEBUG3("in, bd addr:%s, prop type:%d, len:%d", bdstr, prop->type, prop->len);
347ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(prop->len <= 0)
348ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    {
349ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        BTIF_TRACE_ERROR2("property type:%d, len:%d is invalid", prop->type, prop->len);
350ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        return FALSE;
351ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
352ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    int ret = FALSE;
353ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    switch(prop->type)
354ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    {
355ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru       case BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP:
356ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            if(prop->len >= (int)sizeof(int))
357ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                ret = btif_config_get_int("Remote", bdstr,
358ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                                        BTIF_STORAGE_PATH_REMOTE_DEVTIME, (int*)prop->val);
359ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            break;
360ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        case BT_PROPERTY_BDNAME:
361ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        {
362ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            int len = prop->len;
363ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            if(remote_bd_addr)
364ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                ret = btif_config_get_str("Remote", bdstr,
365ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                                        BTIF_STORAGE_PATH_REMOTE_NAME, (char*)prop->val, &len);
366ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            else ret = btif_config_get_str("Local", "Adapter",
367ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                                        BTIF_STORAGE_KEY_ADAPTER_NAME, (char*)prop->val, &len);
368ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            if(ret && len && len <= prop->len)
369ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                prop->len = len - 1;
370b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho            else
371ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            {
372ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                prop->len = 0;
373ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                ret = FALSE;
374ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            }
375ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            break;
376ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        }
377ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        case BT_PROPERTY_REMOTE_FRIENDLY_NAME:
378ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        {
379ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            int len = prop->len;
380ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            ret = btif_config_get_str("Remote", bdstr,
381ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                                       BTIF_STORAGE_PATH_REMOTE_ALIASE, (char*)prop->val, &len);
382ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            if(ret && len && len <= prop->len)
383ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                prop->len = len - 1;
384ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            else
385ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            {
386ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                prop->len = 0;
387ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                ret = FALSE;
388ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            }
389ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            break;
390ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        }
391ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        case BT_PROPERTY_ADAPTER_SCAN_MODE:
392ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru           if(prop->len >= (int)sizeof(int))
393ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                ret = btif_config_get_int("Local", "Adapter",
394ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                                          BTIF_STORAGE_KEY_ADAPTER_SCANMODE, (int*)prop->val);
395ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru           break;
396ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
397ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru           if(prop->len >= (int)sizeof(int))
398ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                ret = btif_config_get_int("Local", "Adapter",
399ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                                          BTIF_STORAGE_KEY_ADAPTER_DISC_TIMEOUT, (int*)prop->val);
400ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            break;
401ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        case BT_PROPERTY_CLASS_OF_DEVICE:
402ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            if(prop->len >= (int)sizeof(int))
403ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                ret = btif_config_get_int("Remote", bdstr,
404ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                                BTIF_STORAGE_PATH_REMOTE_DEVCLASS, (int*)prop->val);
405ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            break;
406ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        case BT_PROPERTY_TYPE_OF_DEVICE:
407ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            if(prop->len >= (int)sizeof(int))
408ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                ret = btif_config_get_int("Remote",
409ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                                    bdstr, BTIF_STORAGE_PATH_REMOTE_DEVTYPE, (int*)prop->val);
410            break;
411        case BT_PROPERTY_UUIDS:
412        {
413            char value[1280];
414            int size = sizeof(value);
415            if(btif_config_get_str("Remote", bdstr,
416                                    BTIF_STORAGE_PATH_REMOTE_SERVICE, value, &size))
417            {
418                bt_uuid_t *p_uuid = (bt_uuid_t*)prop->val;
419                uint32_t num_uuids = 0;
420                btif_in_split_uuids_string_to_list(value, p_uuid, &num_uuids);
421                prop->len = num_uuids * sizeof(bt_uuid_t);
422                ret = TRUE;
423            }
424            else
425            {
426                prop->val = NULL;
427                prop->len = 0;
428            }
429        } break;
430
431        case BT_PROPERTY_REMOTE_VERSION_INFO:
432        {
433            bt_remote_version_t *info = (bt_remote_version_t *)prop->val;
434
435            if(prop->len >= (int)sizeof(bt_remote_version_t))
436            {
437                ret = btif_config_get_int("Remote", bdstr,
438                                BTIF_STORAGE_PATH_REMOTE_VER_MFCT, &info->manufacturer);
439
440                if (ret == TRUE)
441                    ret = btif_config_get_int("Remote", bdstr,
442                                BTIF_STORAGE_PATH_REMOTE_VER_VER, &info->version);
443
444                if (ret == TRUE)
445                    ret = btif_config_get_int("Remote", bdstr,
446                                BTIF_STORAGE_PATH_REMOTE_VER_SUBVER, &info->sub_ver);
447            }
448         } break;
449
450        default:
451            BTIF_TRACE_ERROR1("Unknow prop type:%d", prop->type);
452            return FALSE;
453    }
454    return ret;
455}
456
457
458/*******************************************************************************
459**
460** Function         btif_in_fetch_bonded_devices
461**
462** Description      Internal helper function to fetch the bonded devices
463**                  from NVRAM
464**
465** Returns          BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise
466**
467*******************************************************************************/
468static bt_status_t btif_in_fetch_bonded_devices(btif_bonded_devices_t *p_bonded_devices, int add)
469{
470    BTIF_TRACE_DEBUG1("in add:%d", add);
471    memset(p_bonded_devices, 0, sizeof(btif_bonded_devices_t));
472
473    char kname[128], vname[128];
474    short kpos;
475    int kname_size;
476    kname_size = sizeof(kname);
477    kname[0] = 0;
478    kpos = 0;
479    BOOLEAN bt_linkkey_file_found=FALSE;
480    int device_type;
481
482    do
483    {
484        kpos = btif_config_next_key(kpos, "Remote", kname, &kname_size);
485        BTIF_TRACE_DEBUG2("Remote device:%s, size:%d", kname, kname_size);
486        int type = BTIF_CFG_TYPE_BIN;
487        LINK_KEY link_key;
488        int size = sizeof(link_key);
489        if(btif_config_get("Remote", kname, "LinkKey", (char*)link_key, &size, &type))
490        {
491            int linkkey_type;
492            if(btif_config_get_int("Remote", kname, "LinkKeyType", &linkkey_type))
493            {
494                //int pin_len;
495                //btif_config_get_int("Remote", kname, "PinLength", &pin_len))
496                bt_bdaddr_t bd_addr;
497                str2bd(kname, &bd_addr);
498                if(add)
499                {
500                    DEV_CLASS dev_class = {0, 0, 0};
501                    int cod;
502                    if(btif_config_get_int("Remote", kname, "DevClass", &cod))
503                        uint2devclass((UINT32)cod, dev_class);
504                    BTA_DmAddDevice(bd_addr.address, dev_class, link_key, 0, 0, (UINT8)linkkey_type, 0);
505
506#if BLE_INCLUDED == TRUE
507                    if (btif_config_get_int("Remote", kname, "DevType", &device_type) &&
508                       (device_type == BT_DEVICE_TYPE_DUMO) )
509                    {
510                        btif_gatts_add_bonded_dev_from_nv(bd_addr.address);
511                    }
512#endif
513                }
514                bt_linkkey_file_found = TRUE;
515                memcpy(&p_bonded_devices->devices[p_bonded_devices->num_devices++], &bd_addr, sizeof(bt_bdaddr_t));
516            }
517            else
518            {
519#if (BLE_INCLUDED == TRUE)
520                bt_linkkey_file_found = FALSE;
521#else
522                BTIF_TRACE_ERROR1("bounded device:%s, LinkKeyType or PinLength is invalid", kname);
523#endif
524            }
525        }
526#if (BLE_INCLUDED == TRUE)
527            if(!(btif_in_fetch_bonded_ble_device(kname,add, p_bonded_devices)) && (!bt_linkkey_file_found))
528            {
529                BTIF_TRACE_DEBUG1("Remote device:%s, no link key or ble key found", kname);
530            }
531#else
532            if(!bt_linkkey_file_found)
533                BTIF_TRACE_DEBUG1("Remote device:%s, no link key", kname);
534#endif
535        kname_size = sizeof(kname);
536        kname[0] = 0;
537    } while(kpos != -1);
538    BTIF_TRACE_DEBUG0("out");
539    return BT_STATUS_SUCCESS;
540}
541
542static int hex_str_to_int(const char* str, int size)
543{
544    int  n = 0;
545    char c = *str++;
546    while (size-- != 0)
547    {
548        n <<= 4;
549        if (c >= '0' && c <= '9') {
550            n |= c - '0';
551        }
552        else if (c >= 'a' && c <= 'z') {
553            n |= c - 'a' + 10;
554        }
555        else // (c >= 'A' && c <= 'Z')
556        {
557            n |= c - 'A' + 10;
558        }
559
560        c = *str++;
561    }
562    return n;
563}
564
565/************************************************************************************
566**  Externs
567************************************************************************************/
568
569/************************************************************************************
570**  Functions
571************************************************************************************/
572
573/** functions are synchronous.
574 * functions can be called by both internal modules such as BTIF_DM and by external entiries from HAL via BTIF_context_switch
575 * For OUT parameters,  caller is expected to provide the memory.
576 * Caller is expected to provide a valid pointer to 'property->value' based on the property->type
577 */
578/*******************************************************************************
579**
580** Function         btif_storage_get_adapter_property
581**
582** Description      BTIF storage API - Fetches the adapter property->type
583**                  from NVRAM and fills property->val.
584**                  Caller should provide memory for property->val and
585**                  set the property->val
586**
587** Returns          BT_STATUS_SUCCESS if the fetch was successful,
588**                  BT_STATUS_FAIL otherwise
589**
590*******************************************************************************/
591bt_status_t btif_storage_get_adapter_property(bt_property_t *property)
592{
593
594    /* Special handling for adapter BD_ADDR and BONDED_DEVICES */
595    if (property->type == BT_PROPERTY_BDADDR)
596    {
597        BD_ADDR addr;
598        bt_bdaddr_t *bd_addr = (bt_bdaddr_t*)property->val;
599        /* This has been cached in btif. Just fetch it from there */
600        memcpy(bd_addr, &btif_local_bd_addr, sizeof(bt_bdaddr_t));
601        property->len = sizeof(bt_bdaddr_t);
602        return BT_STATUS_SUCCESS;
603    }
604    else if (property->type == BT_PROPERTY_ADAPTER_BONDED_DEVICES)
605    {
606        btif_bonded_devices_t bonded_devices;
607
608        btif_in_fetch_bonded_devices(&bonded_devices, 0);
609
610        BTIF_TRACE_DEBUG2("%s: Number of bonded devices: %d Property:BT_PROPERTY_ADAPTER_BONDED_DEVICES", __FUNCTION__, bonded_devices.num_devices);
611
612        if (bonded_devices.num_devices > 0)
613        {
614            property->len = bonded_devices.num_devices * sizeof(bt_bdaddr_t);
615            memcpy(property->val, bonded_devices.devices, property->len);
616        }
617
618        /* if there are no bonded_devices, then length shall be 0 */
619        return BT_STATUS_SUCCESS;
620    }
621    else if (property->type == BT_PROPERTY_UUIDS)
622    {
623        /* publish list of local supported services */
624        bt_uuid_t *p_uuid = (bt_uuid_t*)property->val;
625        uint32_t num_uuids = 0;
626        uint32_t i;
627
628        tBTA_SERVICE_MASK service_mask = btif_get_enabled_services_mask();
629        BTIF_TRACE_ERROR2("%s service_mask:0x%x", __FUNCTION__, service_mask);
630        for (i=0; i < BTA_MAX_SERVICE_ID; i++)
631        {
632            /* This should eventually become a function when more services are enabled */
633            if (service_mask
634                &(tBTA_SERVICE_MASK)(1 << i))
635            {
636                switch (i)
637                {
638                    case BTA_HFP_SERVICE_ID:
639                        {
640                            uuid16_to_uuid128(UUID_SERVCLASS_AG_HANDSFREE,
641                                              p_uuid+num_uuids);
642                            num_uuids++;
643                        }
644                    /* intentional fall through: Send both BFP & HSP UUIDs if HFP is enabled */
645                    case BTA_HSP_SERVICE_ID:
646                        {
647                            uuid16_to_uuid128(UUID_SERVCLASS_HEADSET_AUDIO_GATEWAY,
648                                              p_uuid+num_uuids);
649                            num_uuids++;
650                        }break;
651                    case BTA_A2DP_SERVICE_ID:
652                        {
653                            uuid16_to_uuid128(UUID_SERVCLASS_AUDIO_SOURCE,
654                                              p_uuid+num_uuids);
655                            num_uuids++;
656                        }break;
657                }
658            }
659        }
660        property->len = (num_uuids)*sizeof(bt_uuid_t);
661        return BT_STATUS_SUCCESS;
662    }
663
664    /* fall through for other properties */
665    if(!cfg2prop(NULL, property))
666    {
667        return btif_dm_get_adapter_property(property);
668    }
669    return BT_STATUS_SUCCESS;
670 }
671
672/*******************************************************************************
673**
674** Function         btif_storage_set_adapter_property
675**
676** Description      BTIF storage API - Stores the adapter property
677**                  to NVRAM
678**
679** Returns          BT_STATUS_SUCCESS if the store was successful,
680**                  BT_STATUS_FAIL otherwise
681**
682*******************************************************************************/
683bt_status_t btif_storage_set_adapter_property(bt_property_t *property)
684{
685    return prop2cfg(NULL, property) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
686}
687
688/*******************************************************************************
689**
690** Function         btif_storage_get_remote_device_property
691**
692** Description      BTIF storage API - Fetches the remote device property->type
693**                  from NVRAM and fills property->val.
694**                  Caller should provide memory for property->val and
695**                  set the property->val
696**
697** Returns          BT_STATUS_SUCCESS if the fetch was successful,
698**                  BT_STATUS_FAIL otherwise
699**
700*******************************************************************************/
701bt_status_t btif_storage_get_remote_device_property(bt_bdaddr_t *remote_bd_addr,
702                                                    bt_property_t *property)
703{
704    return cfg2prop(remote_bd_addr, property) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
705}
706/*******************************************************************************
707**
708** Function         btif_storage_set_remote_device_property
709**
710** Description      BTIF storage API - Stores the remote device property
711**                  to NVRAM
712**
713** Returns          BT_STATUS_SUCCESS if the store was successful,
714**                  BT_STATUS_FAIL otherwise
715**
716*******************************************************************************/
717bt_status_t btif_storage_set_remote_device_property(bt_bdaddr_t *remote_bd_addr,
718                                                    bt_property_t *property)
719{
720    return prop2cfg(remote_bd_addr, property) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
721}
722
723/*******************************************************************************
724**
725** Function         btif_storage_add_remote_device
726**
727** Description      BTIF storage API - Adds a newly discovered device to NVRAM
728**                  along with the timestamp. Also, stores the various
729**                  properties - RSSI, BDADDR, NAME (if found in EIR)
730**
731** Returns          BT_STATUS_SUCCESS if the store was successful,
732**                  BT_STATUS_FAIL otherwise
733**
734*******************************************************************************/
735bt_status_t btif_storage_add_remote_device(bt_bdaddr_t *remote_bd_addr,
736                                           uint32_t num_properties,
737                                           bt_property_t *properties)
738{
739    uint32_t i = 0;
740    /* TODO: If writing a property, fails do we go back undo the earlier
741     * written properties? */
742    for (i=0; i < num_properties; i++)
743    {
744        /* Ignore the RSSI as this is not stored in DB */
745        if (properties[i].type == BT_PROPERTY_REMOTE_RSSI)
746            continue;
747
748        /* BD_ADDR for remote device needs special handling as we also store timestamp */
749        if (properties[i].type == BT_PROPERTY_BDADDR)
750        {
751            bt_property_t addr_prop;
752            memcpy(&addr_prop, &properties[i], sizeof(bt_property_t));
753            addr_prop.type = BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP;
754            btif_storage_set_remote_device_property(remote_bd_addr,
755                                                    &addr_prop);
756        }
757        else
758        {
759            btif_storage_set_remote_device_property(remote_bd_addr,
760                                                    &properties[i]);
761        }
762    }
763    return BT_STATUS_SUCCESS;
764}
765
766/*******************************************************************************
767**
768** Function         btif_storage_add_bonded_device
769**
770** Description      BTIF storage API - Adds the newly bonded device to NVRAM
771**                  along with the link-key, Key type and Pin key length
772**
773** Returns          BT_STATUS_SUCCESS if the store was successful,
774**                  BT_STATUS_FAIL otherwise
775**
776*******************************************************************************/
777
778bt_status_t btif_storage_add_bonded_device(bt_bdaddr_t *remote_bd_addr,
779                                           LINK_KEY link_key,
780                                           uint8_t key_type,
781                                           uint8_t pin_length)
782{
783    bdstr_t bdstr;
784    bd2str(remote_bd_addr, &bdstr);
785    int ret = btif_config_set_int("Remote", bdstr, "LinkKeyType", (int)key_type);
786    ret &= btif_config_set_int("Remote", bdstr, "PinLength", (int)pin_length);
787    ret &= btif_config_set("Remote", bdstr, "LinkKey", (const char*)link_key, sizeof(LINK_KEY), BTIF_CFG_TYPE_BIN);
788    /* write bonded info immediately */
789    btif_config_flush();
790    return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
791}
792
793/*******************************************************************************
794**
795** Function         btif_storage_remove_bonded_device
796**
797** Description      BTIF storage API - Deletes the bonded device from NVRAM
798**
799** Returns          BT_STATUS_SUCCESS if the deletion was successful,
800**                  BT_STATUS_FAIL otherwise
801**
802*******************************************************************************/
803bt_status_t btif_storage_remove_bonded_device(bt_bdaddr_t *remote_bd_addr)
804{
805    bdstr_t bdstr;
806    bd2str(remote_bd_addr, &bdstr);
807    BTIF_TRACE_DEBUG1("in bd addr:%s", bdstr);
808    int ret = 1;
809    if(btif_config_exist("Remote", bdstr, "LinkKeyType"))
810        ret &= btif_config_remove("Remote", bdstr, "LinkKeyType");
811    if(btif_config_exist("Remote", bdstr, "PinLength"))
812        ret &= btif_config_remove("Remote", bdstr, "PinLength");
813    if(btif_config_exist("Remote", bdstr, "LinkKey"))
814        ret &= btif_config_remove("Remote", bdstr, "LinkKey");
815    /* write bonded info immediately */
816    btif_config_flush();
817    return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
818
819}
820
821/*******************************************************************************
822**
823** Function         btif_storage_load_bonded_devices
824**
825** Description      BTIF storage API - Loads all the bonded devices from NVRAM
826**                  and adds to the BTA.
827**                  Additionally, this API also invokes the adaper_properties_cb
828**                  and remote_device_properties_cb for each of the bonded devices.
829**
830** Returns          BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise
831**
832*******************************************************************************/
833bt_status_t btif_storage_load_bonded_devices(void)
834{
835    char *fname;
836    btif_bonded_devices_t bonded_devices;
837    uint32_t i = 0;
838    bt_property_t adapter_props[6];
839    uint32_t num_props = 0;
840    bt_property_t remote_properties[8];
841    bt_bdaddr_t addr;
842    bt_bdname_t name, alias;
843    bt_scan_mode_t mode;
844    uint32_t disc_timeout;
845    bt_bdaddr_t *devices_list;
846    bt_uuid_t local_uuids[BT_MAX_NUM_UUIDS];
847    bt_uuid_t remote_uuids[BT_MAX_NUM_UUIDS];
848    uint32_t cod, devtype;
849
850    btif_in_fetch_bonded_devices(&bonded_devices, 1);
851
852    /* Now send the adapter_properties_cb with all adapter_properties */
853    {
854        memset(adapter_props, 0, sizeof(adapter_props));
855
856        /* BD_ADDR */
857        BTIF_STORAGE_GET_ADAPTER_PROP(BT_PROPERTY_BDADDR, &addr, sizeof(addr),
858                                      adapter_props[num_props]);
859        num_props++;
860
861        /* BD_NAME */
862        BTIF_STORAGE_GET_ADAPTER_PROP(BT_PROPERTY_BDNAME, &name, sizeof(name),
863                                      adapter_props[num_props]);
864        num_props++;
865
866        /* SCAN_MODE */
867        /* TODO: At the time of BT on, always report the scan mode as 0 irrespective
868         of the scan_mode during the previous enable cycle.
869         This needs to be re-visited as part of the app/stack enable sequence
870         synchronization */
871        mode = BT_SCAN_MODE_NONE;
872        adapter_props[num_props].type = BT_PROPERTY_ADAPTER_SCAN_MODE;
873        adapter_props[num_props].len = sizeof(mode);
874        adapter_props[num_props].val = &mode;
875        num_props++;
876
877        /* DISC_TIMEOUT */
878        BTIF_STORAGE_GET_ADAPTER_PROP(BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT,
879                                      &disc_timeout, sizeof(disc_timeout),
880                                      adapter_props[num_props]);
881        num_props++;
882
883        /* BONDED_DEVICES */
884        devices_list = (bt_bdaddr_t*)malloc(sizeof(bt_bdaddr_t)*bonded_devices.num_devices);
885        adapter_props[num_props].type = BT_PROPERTY_ADAPTER_BONDED_DEVICES;
886        adapter_props[num_props].len = bonded_devices.num_devices * sizeof(bt_bdaddr_t);
887        adapter_props[num_props].val = devices_list;
888        for (i=0; i < bonded_devices.num_devices; i++)
889        {
890            memcpy(devices_list + i, &bonded_devices.devices[i], sizeof(bt_bdaddr_t));
891        }
892        num_props++;
893
894        /* LOCAL UUIDs */
895        BTIF_STORAGE_GET_ADAPTER_PROP(BT_PROPERTY_UUIDS,
896                                      local_uuids, sizeof(local_uuids),
897                                      adapter_props[num_props]);
898        num_props++;
899
900        btif_adapter_properties_evt(BT_STATUS_SUCCESS, num_props, adapter_props);
901
902        free(devices_list);
903    }
904
905    BTIF_TRACE_EVENT2("%s: %d bonded devices found", __FUNCTION__, bonded_devices.num_devices);
906
907    {
908        for (i = 0; i < bonded_devices.num_devices; i++)
909        {
910            bt_bdaddr_t *p_remote_addr;
911
912            num_props = 0;
913            p_remote_addr = &bonded_devices.devices[i];
914            memset(remote_properties, 0, sizeof(remote_properties));
915            BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_BDNAME,
916                                         &name, sizeof(name),
917                                         remote_properties[num_props]);
918            num_props++;
919
920            BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_REMOTE_FRIENDLY_NAME,
921                                         &alias, sizeof(alias),
922                                         remote_properties[num_props]);
923            num_props++;
924
925            BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_CLASS_OF_DEVICE,
926                                         &cod, sizeof(cod),
927                                         remote_properties[num_props]);
928            num_props++;
929
930            BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_TYPE_OF_DEVICE,
931                                         &devtype, sizeof(devtype),
932                                         remote_properties[num_props]);
933            num_props++;
934
935            BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_UUIDS,
936                                         remote_uuids, sizeof(remote_uuids),
937                                         remote_properties[num_props]);
938            num_props++;
939
940            btif_remote_properties_evt(BT_STATUS_SUCCESS, p_remote_addr,
941                                       num_props, remote_properties);
942        }
943    }
944    return BT_STATUS_SUCCESS;
945}
946
947#if (BLE_INCLUDED == TRUE)
948
949/*******************************************************************************
950**
951** Function         btif_storage_add_ble_bonding_key
952**
953** Description      BTIF storage API - Adds the newly bonded device to NVRAM
954**                  along with the ble-key, Key type and Pin key length
955**
956** Returns          BT_STATUS_SUCCESS if the store was successful,
957**                  BT_STATUS_FAIL otherwise
958**
959*******************************************************************************/
960
961bt_status_t btif_storage_add_ble_bonding_key(bt_bdaddr_t *remote_bd_addr,
962                                           char *key,
963                                           UINT8 key_type,
964                                           UINT8 key_length)
965{
966    bdstr_t bdstr;
967    bd2str(remote_bd_addr, &bdstr);
968    const char* name;
969    switch(key_type)
970    {
971        case BTIF_DM_LE_KEY_PENC:
972            name = "LE_KEY_PENC";
973            break;
974        case BTIF_DM_LE_KEY_PID:
975            name = "LE_KEY_PID";
976            break;
977        case BTIF_DM_LE_KEY_PCSRK:
978            name = "LE_KEY_PCSRK";
979            break;
980        case BTIF_DM_LE_KEY_LENC:
981            name = "LE_KEY_LENC";
982            break;
983        case BTIF_DM_LE_KEY_LCSRK:
984            name = "LE_KEY_LCSRK";
985            break;
986        default:
987            return BT_STATUS_FAIL;
988    }
989    int ret = btif_config_set("Remote", bdstr, name, (const char*)key, (int)key_length, BTIF_CFG_TYPE_BIN);
990    btif_config_save();
991    return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
992}
993
994/*******************************************************************************
995**
996** Function         btif_storage_get_ble_bonding_key
997**
998** Description
999**
1000** Returns          BT_STATUS_SUCCESS if the fetch was successful,
1001**                  BT_STATUS_FAIL otherwise
1002**
1003*******************************************************************************/
1004bt_status_t btif_storage_get_ble_bonding_key(bt_bdaddr_t *remote_bd_addr,
1005                                             UINT8 key_type,
1006                                             char *key_value,
1007                                             int key_length)
1008{
1009    bdstr_t bdstr;
1010    bd2str(remote_bd_addr, &bdstr);
1011    const char* name;
1012    int type = BTIF_CFG_TYPE_BIN;
1013    switch(key_type)
1014    {
1015        case BTIF_DM_LE_KEY_PENC:
1016            name = "LE_KEY_PENC";
1017            break;
1018        case BTIF_DM_LE_KEY_PID:
1019            name = "LE_KEY_PID";
1020            break;
1021        case BTIF_DM_LE_KEY_PCSRK:
1022            name = "LE_KEY_PCSRK";
1023            break;
1024        case BTIF_DM_LE_KEY_LENC:
1025            name = "LE_KEY_LENC";
1026            break;
1027        case BTIF_DM_LE_KEY_LCSRK:
1028            name = "LE_KEY_LCSRK";
1029            break;
1030        default:
1031            return BT_STATUS_FAIL;
1032    }
1033    int ret = btif_config_get("Remote", bdstr, name, key_value, &key_length, &type);
1034    return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1035
1036}
1037
1038/*******************************************************************************
1039**
1040** Function         btif_storage_remove_ble_keys
1041**
1042** Description      BTIF storage API - Deletes the bonded device from NVRAM
1043**
1044** Returns          BT_STATUS_SUCCESS if the deletion was successful,
1045**                  BT_STATUS_FAIL otherwise
1046**
1047*******************************************************************************/
1048bt_status_t btif_storage_remove_ble_bonding_keys(bt_bdaddr_t *remote_bd_addr)
1049{
1050    bdstr_t bdstr;
1051    bd2str(remote_bd_addr, &bdstr);
1052    BTIF_TRACE_DEBUG2(" %s in bd addr:%s",__FUNCTION__, bdstr);
1053    int ret = 1;
1054    if(btif_config_exist("Remote", bdstr, "LE_KEY_PENC"))
1055        ret &= btif_config_remove("Remote", bdstr, "LE_KEY_PENC");
1056    if(btif_config_exist("Remote", bdstr, "LE_KEY_PID"))
1057        ret &= btif_config_remove("Remote", bdstr, "LE_KEY_PID");
1058    if(btif_config_exist("Remote", bdstr, "LE_KEY_PCSRK"))
1059        ret &= btif_config_remove("Remote", bdstr, "LE_KEY_PCSRK");
1060    if(btif_config_exist("Remote", bdstr, "LE_KEY_LENC"))
1061        ret &= btif_config_remove("Remote", bdstr, "LE_KEY_LENC");
1062    if(btif_config_exist("Remote", bdstr, "LE_KEY_LCSRK"))
1063        ret &= btif_config_remove("Remote", bdstr, "LE_KEY_LCSRK");
1064    btif_config_save();
1065    return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1066}
1067
1068/*******************************************************************************
1069**
1070** Function         btif_storage_add_ble_local_key
1071**
1072** Description      BTIF storage API - Adds the ble key to NVRAM
1073**
1074** Returns          BT_STATUS_SUCCESS if the store was successful,
1075**                  BT_STATUS_FAIL otherwise
1076**
1077*******************************************************************************/
1078bt_status_t btif_storage_add_ble_local_key(char *key,
1079                                           uint8_t key_type,
1080                                           uint8_t key_length)
1081{
1082    const char* name;
1083    switch(key_type)
1084    {
1085        case BTIF_DM_LE_LOCAL_KEY_IR:
1086            name = "LE_LOCAL_KEY_IR";
1087            break;
1088        case BTIF_DM_LE_LOCAL_KEY_IRK:
1089            name = "LE_LOCAL_KEY_IRK";
1090            break;
1091        case BTIF_DM_LE_LOCAL_KEY_DHK:
1092            name = "LE_LOCAL_KEY_DHK";
1093            break;
1094        case BTIF_DM_LE_LOCAL_KEY_ER:
1095            name = "LE_LOCAL_KEY_ER";
1096            break;
1097        default:
1098            return BT_STATUS_FAIL;
1099    }
1100    int ret = btif_config_set("Local", "Adapter", name, (const char*)key, key_length, BTIF_CFG_TYPE_BIN);
1101    btif_config_save();
1102    return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1103}
1104
1105/*******************************************************************************
1106**
1107** Function         btif_storage_get_ble_local_key
1108**
1109** Description
1110**
1111** Returns          BT_STATUS_SUCCESS if the fetch was successful,
1112**                  BT_STATUS_FAIL otherwise
1113**
1114*******************************************************************************/
1115bt_status_t btif_storage_get_ble_local_key(UINT8 key_type,
1116                                           char *key_value,
1117                                           int key_length)
1118{
1119    const char* name;
1120    int type = BTIF_CFG_TYPE_BIN;
1121    switch(key_type)
1122    {
1123        case BTIF_DM_LE_LOCAL_KEY_IR:
1124            name = "LE_LOCAL_KEY_IR";
1125            break;
1126        case BTIF_DM_LE_LOCAL_KEY_IRK:
1127            name = "LE_LOCAL_KEY_IRK";
1128            break;
1129        case BTIF_DM_LE_LOCAL_KEY_DHK:
1130            name = "LE_LOCAL_KEY_DHK";
1131            break;
1132        case BTIF_DM_LE_LOCAL_KEY_ER:
1133            name = "LE_LOCAL_KEY_ER";
1134            break;
1135        default:
1136            return BT_STATUS_FAIL;
1137    }
1138    int ret = btif_config_get("Local", "Adapter", name, key_value, &key_length, &type);
1139    return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1140
1141}
1142
1143/*******************************************************************************
1144**
1145** Function         btif_storage_remove_ble_local_keys
1146**
1147** Description      BTIF storage API - Deletes the bonded device from NVRAM
1148**
1149** Returns          BT_STATUS_SUCCESS if the deletion was successful,
1150**                  BT_STATUS_FAIL otherwise
1151**
1152*******************************************************************************/
1153bt_status_t btif_storage_remove_ble_local_keys(void)
1154{
1155    int ret = 1;
1156    if(btif_config_exist("Local", "Adapter", "LE_LOCAL_KEY_IR"))
1157        ret &= btif_config_remove("Local", "Adapter", "LE_LOCAL_KEY_IR");
1158    if(btif_config_exist("Local", "Adapter", "LE_LOCAL_KEY_IRK"))
1159        ret &= btif_config_remove("Local", "Adapter", "LE_LOCAL_KEY_IRK");
1160    if(btif_config_exist("Local", "Adapter", "LE_LOCAL_KEY_DHK"))
1161        ret &= btif_config_remove("Local", "Adapter", "LE_LOCAL_KEY_DHK");
1162    if(btif_config_exist("Local", "Adapter", "LE_LOCAL_KEY_ER"))
1163        ret &= btif_config_remove("Local", "Adapter", "LE_LOCAL_KEY_ER");
1164    btif_config_save();
1165    return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1166}
1167
1168bt_status_t btif_in_fetch_bonded_ble_device(char *remote_bd_addr,int add, btif_bonded_devices_t *p_bonded_devices)
1169{
1170    int device_type;
1171    int addr_type;
1172    char buf[100];
1173    UINT32 i;
1174    bt_bdaddr_t bd_addr;
1175    BD_ADDR bta_bd_addr;
1176    BOOLEAN is_device_added =FALSE;
1177    tBTA_LE_KEY_VALUE *p;
1178
1179    if(!btif_config_get_int("Remote", remote_bd_addr,"DevType", &device_type))
1180        return BT_STATUS_FAIL;
1181    if(device_type == BT_DEVICE_TYPE_BLE)
1182    {
1183            BTIF_TRACE_DEBUG2("%s %s found a BLE device", __FUNCTION__,remote_bd_addr);
1184            str2bd(remote_bd_addr, &bd_addr);
1185            bdcpy(bta_bd_addr, bd_addr.address);
1186            if (btif_storage_get_remote_addr_type(&bd_addr, &addr_type) != BT_STATUS_SUCCESS)
1187            {
1188                return BT_STATUS_FAIL;
1189            }
1190
1191            memset(buf, 0, sizeof(buf));
1192            if (btif_storage_get_ble_bonding_key(&bd_addr,
1193                                                 BTIF_DM_LE_KEY_PENC,
1194                                                 buf,
1195                                                 sizeof(btif_dm_ble_penc_keys_t)) == BT_STATUS_SUCCESS)
1196            {
1197                if(add)
1198                {
1199                    if (!is_device_added)
1200                    {
1201                        BTA_DmAddBleDevice(bta_bd_addr, addr_type, BT_DEVICE_TYPE_BLE);
1202                        is_device_added = TRUE;
1203                    }
1204                    p = (tBTA_LE_KEY_VALUE *)buf;
1205                    for (i=0; i<16; i++)
1206                    {
1207                        BTIF_TRACE_DEBUG2("penc_key.ltk[%d]=0x%02x",i,p->penc_key.ltk[i]);
1208                    }
1209                    for (i=0; i<8; i++)
1210                    {
1211                        BTIF_TRACE_DEBUG2("penc_key.rand[%d]=0x%02x",i,p->penc_key.rand[i]);
1212                    }
1213                    BTIF_TRACE_DEBUG1("p->penc_key.ediv=0x%04x",p->penc_key.ediv);
1214                    BTIF_TRACE_DEBUG1("p->penc_key.sec_level=0x%02x",p->penc_key.sec_level);
1215                    BTIF_TRACE_DEBUG1("p->penc_key.key_size=0x%02x",p->penc_key.key_size);
1216                    BTA_DmAddBleKey (bta_bd_addr, (tBTA_LE_KEY_VALUE *)buf, BTIF_DM_LE_KEY_PENC);
1217                }
1218            }
1219
1220            memset(buf, 0, sizeof(buf));
1221            if (btif_storage_get_ble_bonding_key(&bd_addr,
1222                                                 BTIF_DM_LE_KEY_PID,
1223                                                 buf,
1224                                                 sizeof(btif_dm_ble_pcsrk_keys_t)) == BT_STATUS_SUCCESS)
1225            {
1226                if(add)
1227                {
1228                    if (!is_device_added)
1229                    {
1230                        BTA_DmAddBleDevice(bta_bd_addr, addr_type, BT_DEVICE_TYPE_BLE);
1231                        is_device_added = TRUE;
1232                    }
1233                    p = (tBTA_LE_KEY_VALUE *)buf;
1234                    for (i=0; i<16; i++)
1235                    {
1236                        BTIF_TRACE_DEBUG2("p->pid_key[%d]=0x%02x",i,p->pid_key.irk[i]);
1237                    }
1238
1239                    BTA_DmAddBleKey (bta_bd_addr, (tBTA_LE_KEY_VALUE *)buf, BTIF_DM_LE_KEY_PID);
1240                }
1241            }
1242
1243            memset(buf, 0, sizeof(buf));
1244            if (btif_storage_get_ble_bonding_key(&bd_addr,
1245                                                 BTIF_DM_LE_KEY_PCSRK,
1246                                                 buf,
1247                                                 sizeof(btif_dm_ble_pcsrk_keys_t)) == BT_STATUS_SUCCESS)
1248            {
1249                if(add)
1250                {
1251                    if (!is_device_added)
1252                    {
1253                        BTA_DmAddBleDevice(bta_bd_addr, addr_type, BT_DEVICE_TYPE_BLE);
1254                        is_device_added = TRUE;
1255                    }
1256
1257                    p = (tBTA_LE_KEY_VALUE *)buf;
1258                    for (i=0; i<16; i++)
1259                    {
1260                        BTIF_TRACE_DEBUG2("p->pcsrk_key.csrk[%d]=0x%02x",i, p->psrk_key.csrk[i]);
1261                    }
1262                    BTIF_TRACE_DEBUG1("p->pcsrk_key.counter=0x%08x",p->psrk_key.counter);
1263                    BTIF_TRACE_DEBUG1("p->pcsrk_key.sec_level=0x%02x",p->psrk_key.sec_level);
1264
1265                    BTA_DmAddBleKey (bta_bd_addr, (tBTA_LE_KEY_VALUE *)buf, BTIF_DM_LE_KEY_PCSRK);
1266                }
1267            }
1268
1269            memset(buf, 0, sizeof(buf));
1270            if (btif_storage_get_ble_bonding_key(&bd_addr,
1271                                                 BTIF_DM_LE_KEY_LENC,
1272                                                 buf,
1273                                                 sizeof(btif_dm_ble_lenc_keys_t)) == BT_STATUS_SUCCESS)
1274            {
1275                if(add)
1276                {
1277                    if (!is_device_added)
1278                    {
1279                        BTA_DmAddBleDevice(bta_bd_addr, addr_type, BT_DEVICE_TYPE_BLE);
1280                        is_device_added = TRUE;
1281                    }
1282                    p = (tBTA_LE_KEY_VALUE *)buf;
1283                    BTIF_TRACE_DEBUG1("p->lenc_key.div=0x%04x",p->lenc_key.div);
1284                    BTIF_TRACE_DEBUG1("p->lenc_key.key_size=0x%02x",p->lenc_key.key_size);
1285                    BTIF_TRACE_DEBUG1("p->lenc_key.sec_level=0x%02x",p->lenc_key.sec_level);
1286
1287                    BTA_DmAddBleKey (bta_bd_addr, (tBTA_LE_KEY_VALUE *)buf, BTIF_DM_LE_KEY_LENC);
1288                }
1289            }
1290
1291            memset(buf, 0, sizeof(buf));
1292            if (btif_storage_get_ble_bonding_key(&bd_addr,
1293                                                 BTIF_DM_LE_KEY_LCSRK,
1294                                                 buf,
1295                                                 sizeof(btif_dm_ble_lcsrk_keys_t)) == BT_STATUS_SUCCESS)
1296            {
1297                if(add)
1298                {
1299                    if (!is_device_added)
1300                    {
1301                        BTA_DmAddBleDevice(bta_bd_addr, addr_type, BT_DEVICE_TYPE_BLE);
1302                        is_device_added = TRUE;
1303                    }
1304                    p = (tBTA_LE_KEY_VALUE *)buf;
1305                    BTIF_TRACE_DEBUG1("p->lcsrk_key.div=0x%04x",p->lcsrk_key.div);
1306                    BTIF_TRACE_DEBUG1("p->lcsrk_key.counter=0x%08x",p->lcsrk_key.counter);
1307                    BTIF_TRACE_DEBUG1("p->lcsrk_key.sec_level=0x%02x",p->lcsrk_key.sec_level);
1308
1309                    BTA_DmAddBleKey (bta_bd_addr, (tBTA_LE_KEY_VALUE *)buf, BTIF_DM_LE_KEY_LCSRK);
1310                }
1311            }
1312
1313            /* Fill in the bonded devices */
1314            if (is_device_added)
1315            {
1316                memcpy(&p_bonded_devices->devices[p_bonded_devices->num_devices++], &bd_addr, sizeof(bt_bdaddr_t));
1317                btif_gatts_add_bonded_dev_from_nv(bta_bd_addr);
1318            }
1319
1320            return BT_STATUS_SUCCESS;
1321    }
1322    return BT_STATUS_FAIL;
1323}
1324
1325bt_status_t btif_storage_set_remote_addr_type(bt_bdaddr_t *remote_bd_addr,
1326                                              UINT8 addr_type)
1327{
1328    bdstr_t bdstr;
1329    bd2str(remote_bd_addr, &bdstr);
1330    int ret = btif_config_set_int("Remote", bdstr, "AddrType", (int)addr_type);
1331    btif_config_save();
1332    return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1333}
1334
1335/*******************************************************************************
1336**
1337** Function         btif_storage_get_remote_addr_type
1338**
1339** Description      BTIF storage API - Fetches the remote addr type
1340**
1341** Returns          BT_STATUS_SUCCESS if the fetch was successful,
1342**                  BT_STATUS_FAIL otherwise
1343**
1344*******************************************************************************/
1345bt_status_t btif_storage_get_remote_addr_type(bt_bdaddr_t *remote_bd_addr,
1346                                              int*addr_type)
1347{
1348    bdstr_t bdstr;
1349    bd2str(remote_bd_addr, &bdstr);
1350    int ret = btif_config_get_int("Remote", bdstr, "AddrType", addr_type);
1351    return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1352}
1353#endif
1354/*******************************************************************************
1355**
1356** Function         btif_storage_add_hid_device_info
1357**
1358** Description      BTIF storage API - Adds the hid information of bonded hid devices-to NVRAM
1359**
1360** Returns          BT_STATUS_SUCCESS if the store was successful,
1361**                  BT_STATUS_FAIL otherwise
1362**
1363*******************************************************************************/
1364
1365bt_status_t btif_storage_add_hid_device_info(bt_bdaddr_t *remote_bd_addr,
1366                                                    UINT16 attr_mask, UINT8 sub_class,
1367                                                    UINT8 app_id, UINT16 vendor_id,
1368                                                    UINT16 product_id, UINT16 version,
1369                                                    UINT8 ctry_code, UINT16 ssr_max_latency,
1370                                                    UINT16 ssr_min_tout, UINT16 dl_len, UINT8 *dsc_list)
1371{
1372    bdstr_t bdstr;
1373    BTIF_TRACE_DEBUG0("btif_storage_add_hid_device_info:");
1374    bd2str(remote_bd_addr, &bdstr);
1375    btif_config_set_int("Remote", bdstr, "HidAttrMask", attr_mask);
1376    btif_config_set_int("Remote", bdstr, "HidSubClass", sub_class);
1377    btif_config_set_int("Remote", bdstr, "HidAppId", app_id);
1378    btif_config_set_int("Remote", bdstr, "HidVendorId", vendor_id);
1379    btif_config_set_int("Remote", bdstr, "HidProductId", product_id);
1380    btif_config_set_int("Remote", bdstr, "HidVersion", version);
1381    btif_config_set_int("Remote", bdstr, "HidCountryCode", ctry_code);
1382    btif_config_set_int("Remote", bdstr, "HidSSRMaxLatency", ssr_max_latency);
1383    btif_config_set_int("Remote", bdstr, "HidSSRMinTimeout", ssr_min_tout);
1384    if(dl_len > 0)
1385        btif_config_set("Remote", bdstr, "HidDescriptor", (const char*)dsc_list, dl_len,
1386                        BTIF_CFG_TYPE_BIN);
1387    btif_config_save();
1388    return BT_STATUS_SUCCESS;
1389}
1390
1391/*******************************************************************************
1392**
1393** Function         btif_storage_load_bonded_hid_info
1394**
1395** Description      BTIF storage API - Loads hid info for all the bonded devices from NVRAM
1396**                  and adds those devices  to the BTA_HH.
1397**
1398** Returns          BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise
1399**
1400*******************************************************************************/
1401bt_status_t btif_storage_load_bonded_hid_info(void)
1402{
1403    bt_bdaddr_t bd_addr;
1404    tBTA_HH_DEV_DSCP_INFO dscp_info;
1405    uint32_t i;
1406    uint16_t attr_mask;
1407    uint8_t  sub_class;
1408    uint8_t  app_id;
1409
1410    char kname[128], vname[128];
1411    short kpos;
1412    int kname_size;
1413    kname_size = sizeof(kname);
1414    kname[0] = 0;
1415    kpos = 0;
1416    memset(&dscp_info, 0, sizeof(dscp_info));
1417    do
1418    {
1419        kpos = btif_config_next_key(kpos, "Remote", kname, &kname_size);
1420        BTIF_TRACE_DEBUG2("Remote device:%s, size:%d", kname, kname_size);
1421        int value;
1422        if(btif_config_get_int("Remote", kname, "HidAttrMask", &value))
1423        {
1424            attr_mask = (uint16_t)value;
1425
1426            btif_config_get_int("Remote", kname, "HidSubClass", &value);
1427            sub_class = (uint8_t)value;
1428
1429            btif_config_get_int("Remote", kname, "HidAppId", &value);
1430            app_id = (uint8_t)value;
1431
1432            btif_config_get_int("Remote", kname, "HidVendorId", &value);
1433            dscp_info.vendor_id = (uint16_t) value;
1434
1435            btif_config_get_int("Remote", kname, "HidProductId", &value);
1436            dscp_info.product_id = (uint16_t) value;
1437
1438            btif_config_get_int("Remote", kname, "HidVersion", &value);
1439            dscp_info.version = (uint8_t) value;
1440
1441            btif_config_get_int("Remote", kname, "HidCountryCode", &value);
1442            dscp_info.ctry_code = (uint8_t) value;
1443
1444            value = 0;
1445            btif_config_get_int("Remote", kname, "HidSSRMaxLatency", &value);
1446            dscp_info.ssr_max_latency = (uint16_t) value;
1447
1448            value = 0;
1449            btif_config_get_int("Remote", kname, "HidSSRMinTimeout", &value);
1450            dscp_info.ssr_min_tout = (uint16_t) value;
1451
1452            int len = 0;
1453            int type;
1454            btif_config_get("Remote", kname, "HidDescriptor", NULL, &len, &type);
1455            if(len > 0)
1456            {
1457                dscp_info.descriptor.dl_len = (uint16_t)len;
1458                dscp_info.descriptor.dsc_list = (uint8_t*)alloca(len);
1459                btif_config_get("Remote", kname, "HidDescriptor", (char*)dscp_info.descriptor.dsc_list, &len, &type);
1460            }
1461            str2bd(kname, &bd_addr);
1462            // add extracted information to BTA HH
1463            if (btif_hh_add_added_dev(bd_addr,attr_mask))
1464            {
1465                BTA_HhAddDev(bd_addr.address, attr_mask, sub_class,
1466                        app_id, dscp_info);
1467            }
1468        }
1469    } while(kpos != -1);
1470
1471    return BT_STATUS_SUCCESS;
1472}
1473
1474/*******************************************************************************
1475**
1476** Function         btif_storage_remove_hid_info
1477**
1478** Description      BTIF storage API - Deletes the bonded hid device info from NVRAM
1479**
1480** Returns          BT_STATUS_SUCCESS if the deletion was successful,
1481**                  BT_STATUS_FAIL otherwise
1482**
1483*******************************************************************************/
1484bt_status_t btif_storage_remove_hid_info(bt_bdaddr_t *remote_bd_addr)
1485{
1486    char *fname;
1487    int ret;
1488    bdstr_t bdstr;
1489    bd2str(remote_bd_addr, &bdstr);
1490
1491    btif_config_remove("Remote", bdstr, "HidAttrMask");
1492    btif_config_remove("Remote", bdstr, "HidSubClass");
1493    btif_config_remove("Remote", bdstr, "HidAppId");
1494    btif_config_remove("Remote", bdstr, "HidVendorId");
1495    btif_config_remove("Remote", bdstr, "HidProductId");
1496    btif_config_remove("Remote", bdstr, "HidVersion");
1497    btif_config_remove("Remote", bdstr, "HidCountryCode");
1498    btif_config_remove("Remote", bdstr, "HidSSRMaxLatency");
1499    btif_config_remove("Remote", bdstr, "HidSSRMinTimeout");
1500    btif_config_remove("Remote", bdstr, "HidDescriptor");
1501    btif_config_save();
1502    return BT_STATUS_SUCCESS;
1503}
1504
1505/*******************************************************************************
1506**
1507** Function         btif_storage_read_hl_apps_cb
1508**
1509** Description      BTIF storage API - Read HL application control block from NVRAM
1510**
1511** Returns          BT_STATUS_SUCCESS if the operation was successful,
1512**                  BT_STATUS_FAIL otherwise
1513**
1514*******************************************************************************/
1515bt_status_t btif_storage_read_hl_apps_cb(char *value, int value_size)
1516{
1517    bt_status_t bt_status = BT_STATUS_SUCCESS;
1518    int read_size=value_size, read_type=BTIF_CFG_TYPE_BIN;
1519
1520    if (!btif_config_exist("Local", BTIF_STORAGE_HL_APP, BTIF_STORAGE_HL_APP_CB))
1521    {
1522        memset(value, 0, value_size);
1523        if (!btif_config_set("Local", BTIF_STORAGE_HL_APP,BTIF_STORAGE_HL_APP_CB,
1524                             value, value_size, BTIF_CFG_TYPE_BIN))
1525        {
1526            bt_status = BT_STATUS_FAIL;
1527        }
1528        else
1529        {
1530            btif_config_save();
1531        }
1532    }
1533    else
1534    {
1535        if (!btif_config_get("Local", BTIF_STORAGE_HL_APP, BTIF_STORAGE_HL_APP_CB,
1536                             value, &read_size, &read_type))
1537        {
1538            bt_status = BT_STATUS_FAIL;
1539        }
1540        else
1541        {
1542            if ((read_size != value_size) || (read_type != BTIF_CFG_TYPE_BIN) )
1543            {
1544                BTIF_TRACE_ERROR4("%s  value_size=%d read_size=%d read_type=%d",
1545                                  __FUNCTION__, value_size, read_size, read_type);
1546                bt_status = BT_STATUS_FAIL;
1547            }
1548        }
1549
1550    }
1551
1552    BTIF_TRACE_DEBUG3("%s  status=%d value_size=%d", __FUNCTION__, bt_status, value_size);
1553    return bt_status;
1554}
1555
1556
1557/*******************************************************************************
1558**
1559** Function         btif_storage_load_autopair_device_list
1560**
1561** Description      BTIF storage API - Populates auto pair device list
1562**
1563** Returns          BT_STATUS_SUCCESS if the auto pair blacklist is successfully populated
1564**                  BT_STATUS_FAIL otherwise
1565**
1566*******************************************************************************/
1567
1568bt_status_t btif_storage_load_autopair_device_list()
1569{
1570    char *key_name, *key_value;
1571    int i=0;
1572    char linebuf[BTIF_STORAGE_MAX_LINE_SZ];
1573    char *line;
1574    FILE *fp;
1575
1576    if(!btif_config_exist("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST, NULL))
1577    {
1578        /* first time loading of auto pair blacklist configuration  */
1579
1580        fp = fopen (BTIF_AUTO_PAIR_CONF_FILE, "r");
1581
1582        if (fp == NULL)
1583        {
1584            ALOGE("%s: Failed to open auto pair blacklist conf file at %s", __FUNCTION__,BTIF_AUTO_PAIR_CONF_FILE );
1585            return BT_STATUS_FAIL;
1586        }
1587
1588        /* read through auto_pairing.conf file and create the key value pairs specific to  auto pair blacklist devices */
1589        while (fgets(linebuf, BTIF_STORAGE_MAX_LINE_SZ, fp) != NULL)
1590        {
1591            /* trip  leading white spaces */
1592            while (linebuf[i] == BTIF_AUTO_PAIR_CONF_SPACE)
1593                i++;
1594
1595            /* skip  commented lines */
1596            if (linebuf[i] == BTIF_AUTO_PAIR_CONF_COMMENT)
1597                continue;
1598
1599            line = (char*)&(linebuf[i]);
1600
1601            if (line == NULL)
1602                continue;
1603
1604            key_name = strtok(line, BTIF_AUTO_PAIR_CONF_KEY_VAL_DELIMETER);
1605
1606            if (key_name == NULL)
1607                continue;
1608            else if((strcmp(key_name, BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_ADDR) == 0) ||
1609                    (strcmp(key_name, BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_EXACTNAME) ==0) ||
1610                    (strcmp(key_name, BTIF_STORAGE_KEY_AUTOPAIR_FIXPIN_KBLIST) ==0 ) ||
1611                    (strcmp(key_name, BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_PARTIALNAME) == 0) ||
1612                    (strcmp(key_name, BTIF_STORAGE_KEY_AUTOPAIR_DYNAMIC_BLACKLIST_ADDR) == 0))
1613            {
1614                key_value = strtok(NULL, BTIF_AUTO_PAIR_CONF_KEY_VAL_DELIMETER);
1615                btif_config_set_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST, key_name, key_value);
1616            }
1617        }
1618        fclose(fp);
1619    }
1620    return BT_STATUS_SUCCESS;
1621}
1622
1623/*******************************************************************************
1624**
1625** Function         btif_storage_is_device_autopair_blacklisted
1626**
1627** Description      BTIF storage API  Checks if the given device is blacklisted for auto pairing
1628**
1629** Returns          TRUE if the device is found in the auto pair blacklist
1630**                  FALSE otherwise
1631**
1632*******************************************************************************/
1633BOOLEAN  btif_storage_is_device_autopair_blacklisted(bt_bdaddr_t *remote_bd_addr)
1634{
1635    char *token;
1636    int ret;
1637    bdstr_t bdstr;
1638    char *dev_name_str;
1639    uint8_t i = 0;
1640    char value[BTIF_STORAGE_MAX_LINE_SZ];
1641    int value_size = sizeof(value);
1642
1643    bd2str(remote_bd_addr, &bdstr);
1644
1645    /* Consider only  Lower Address Part from BD Address */
1646    bdstr[8] = '\0';
1647
1648    if(btif_config_get_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1649                BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_ADDR, value, &value_size))
1650    {
1651        if (strcasestr(value,bdstr) != NULL)
1652            return TRUE;
1653    }
1654
1655    dev_name_str = BTM_SecReadDevName((remote_bd_addr->address));
1656
1657    if (dev_name_str != NULL)
1658    {
1659        value_size = sizeof(value);
1660        if(btif_config_get_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1661                    BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_EXACTNAME, value, &value_size))
1662        {
1663            if (strstr(value,dev_name_str) != NULL)
1664                return TRUE;
1665        }
1666        value_size = sizeof(value);
1667        if(btif_config_get_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1668                    BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_PARTIALNAME, value, &value_size))
1669        {
1670            token = strtok(value, BTIF_AUTO_PAIR_CONF_VALUE_SEPARATOR);
1671            while (token != NULL)
1672            {
1673                if (strstr(dev_name_str, token) != NULL)
1674                    return TRUE;
1675
1676                token = strtok(NULL, BTIF_AUTO_PAIR_CONF_VALUE_SEPARATOR);
1677            }
1678        }
1679    }
1680    if(btif_config_get_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1681                BTIF_STORAGE_KEY_AUTOPAIR_DYNAMIC_BLACKLIST_ADDR, value, &value_size))
1682    {
1683        if (strstr(value,bdstr) != NULL)
1684            return TRUE;
1685    }
1686    return FALSE;
1687}
1688
1689/*******************************************************************************
1690**
1691** Function         btif_storage_add_device_to_autopair_blacklist
1692**
1693** Description      BTIF storage API - Add a remote device to the auto pairing blacklist
1694**
1695** Returns          BT_STATUS_SUCCESS if the device is successfully added to the auto pair blacklist
1696**                  BT_STATUS_FAIL otherwise
1697**
1698*******************************************************************************/
1699bt_status_t btif_storage_add_device_to_autopair_blacklist(bt_bdaddr_t *remote_bd_addr)
1700{
1701    int ret;
1702    bdstr_t bdstr;
1703    char linebuf[BTIF_STORAGE_MAX_LINE_SZ+20];
1704    char input_value [20];
1705
1706    bd2str(remote_bd_addr, &bdstr);
1707    strlcpy(input_value, (char*)bdstr, sizeof(input_value));
1708    strlcat(input_value,BTIF_AUTO_PAIR_CONF_VALUE_SEPARATOR, sizeof(input_value));
1709
1710    int line_size = sizeof(linebuf);
1711    if(btif_config_get_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1712                            BTIF_STORAGE_KEY_AUTOPAIR_DYNAMIC_BLACKLIST_ADDR, linebuf, &line_size))
1713    {
1714         /* Append this address to the dynamic List of BD address  */
1715        strncat (linebuf, input_value, BTIF_STORAGE_MAX_LINE_SZ);
1716    }
1717    else
1718    {
1719        strncpy( linebuf,input_value, BTIF_STORAGE_MAX_LINE_SZ);
1720    }
1721
1722    /* Write back the key value */
1723    ret = btif_config_set_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1724                        BTIF_STORAGE_KEY_AUTOPAIR_DYNAMIC_BLACKLIST_ADDR, linebuf);
1725
1726    return ret ? BT_STATUS_SUCCESS:BT_STATUS_FAIL;
1727}
1728
1729/*******************************************************************************
1730**
1731** Function         btif_storage_is_fixed_pin_zeros_keyboard
1732**
1733** Description      BTIF storage API - checks if this device has fixed PIN key device list
1734**
1735** Returns          TRUE   if the device is found in the fixed pin keyboard device list
1736**                  FALSE otherwise
1737**
1738*******************************************************************************/
1739BOOLEAN btif_storage_is_fixed_pin_zeros_keyboard(bt_bdaddr_t *remote_bd_addr)
1740{
1741    int ret;
1742    bdstr_t bdstr;
1743    char *dev_name_str;
1744    uint8_t i = 0;
1745    char linebuf[BTIF_STORAGE_MAX_LINE_SZ];
1746
1747    bd2str(remote_bd_addr, &bdstr);
1748
1749    /*consider on LAP part of BDA string*/
1750    bdstr[8] = '\0';
1751
1752    int line_size = sizeof(linebuf);
1753    if(btif_config_get_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1754                            BTIF_STORAGE_KEY_AUTOPAIR_FIXPIN_KBLIST, linebuf, &line_size))
1755    {
1756        if (strcasestr(linebuf,bdstr) != NULL)
1757            return TRUE;
1758    }
1759    return FALSE;
1760
1761}
1762
1763/*******************************************************************************
1764**
1765** Function         btif_storage_set_dmt_support_type
1766**
1767** Description      Sets DMT support status for a remote device
1768**
1769** Returns          BT_STATUS_SUCCESS if config update is successful
1770**                  BT_STATUS_FAIL otherwise
1771**
1772*******************************************************************************/
1773
1774bt_status_t btif_storage_set_dmt_support_type(const bt_bdaddr_t *remote_bd_addr,
1775                                                   BOOLEAN dmt_supported)
1776{
1777    int ret;
1778    bdstr_t bdstr = {0};
1779    if(remote_bd_addr)
1780    {
1781        bd2str(remote_bd_addr, &bdstr);
1782    }
1783    else
1784    {
1785        BTIF_TRACE_ERROR1("%s  NULL BD Address", __FUNCTION__);
1786        return BT_STATUS_FAIL;
1787    }
1788
1789   ret = btif_config_set_int("Remote", bdstr,"DMTSupported", (int)dmt_supported);
1790   return ret ? BT_STATUS_SUCCESS:BT_STATUS_FAIL;
1791
1792}
1793
1794/*******************************************************************************
1795**
1796** Function         btif_storage_is_dmt_supported_device
1797**
1798** Description      checks if a device supports Dual mode topology
1799**
1800** Returns         TRUE if remote address is valid and supports DMT else FALSE
1801**
1802*******************************************************************************/
1803
1804BOOLEAN btif_storage_is_dmt_supported_device(const bt_bdaddr_t *remote_bd_addr)
1805{
1806    int    dmt_supported = 0;
1807    bdstr_t bdstr = {0};
1808    if(remote_bd_addr)
1809        bd2str(remote_bd_addr, &bdstr);
1810
1811    if(remote_bd_addr)
1812    {
1813        bd2str(remote_bd_addr, &bdstr);
1814    }
1815    else
1816    {
1817        BTIF_TRACE_ERROR1("%s  NULL BD Address", __FUNCTION__);
1818        return FALSE;
1819    }
1820
1821    btif_config_get_int("Remote", bdstr,"DMTSupported", &dmt_supported);
1822
1823    return dmt_supported == 1 ? TRUE:FALSE;
1824}
1825
1826
1827