OSDN Git Service

DO NOT MERGE Don't persist bonds using sample LTK
[android-x86/system-bt.git] / btif / src / btif_storage.c
1 /******************************************************************************
2  *
3  *  Copyright (c) 2014 The Android Open Source Project
4  *  Copyright (C) 2009-2012 Broadcom Corporation
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at:
9  *
10  *  http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  ******************************************************************************/
19
20 /************************************************************************************
21  *
22  *  Filename:      btif_storage.c
23  *
24  *  Description:   Stores the local BT adapter and remote device properties in
25  *                 NVRAM storage, typically as xml file in the
26  *                 mobile's filesystem
27  *
28  *
29  */
30
31 #define LOG_TAG "bt_btif_storage"
32
33 #include "btif_storage.h"
34
35 #include <alloca.h>
36 #include <assert.h>
37 #include <ctype.h>
38 #include <log/log.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <time.h>
42
43 #include "bta_hh_api.h"
44 #include "btcore/include/bdaddr.h"
45 #include "btif_api.h"
46 #include "btif_config.h"
47 #include "btif_hh.h"
48 #include "btif_util.h"
49 #include "bt_common.h"
50 #include "osi/include/allocator.h"
51 #include "osi/include/compat.h"
52 #include "osi/include/config.h"
53 #include "osi/include/list.h"
54 #include "osi/include/log.h"
55 #include "osi/include/osi.h"
56
57 /************************************************************************************
58 **  Constants & Macros
59 ************************************************************************************/
60
61 // TODO(armansito): Find a better way than using a hardcoded path.
62 #define BTIF_STORAGE_PATH_BLUEDROID "/data/misc/bluedroid"
63
64 //#define BTIF_STORAGE_PATH_ADAPTER_INFO "adapter_info"
65 //#define BTIF_STORAGE_PATH_REMOTE_DEVICES "remote_devices"
66 #define BTIF_STORAGE_PATH_REMOTE_DEVTIME "Timestamp"
67 #define BTIF_STORAGE_PATH_REMOTE_DEVCLASS "DevClass"
68 #define BTIF_STORAGE_PATH_REMOTE_DEVTYPE "DevType"
69 #define BTIF_STORAGE_PATH_REMOTE_NAME "Name"
70 #define BTIF_STORAGE_PATH_REMOTE_VER_MFCT "Manufacturer"
71 #define BTIF_STORAGE_PATH_REMOTE_VER_VER "LmpVer"
72 #define BTIF_STORAGE_PATH_REMOTE_VER_SUBVER "LmpSubVer"
73
74 //#define BTIF_STORAGE_PATH_REMOTE_LINKKEYS "remote_linkkeys"
75 #define BTIF_STORAGE_PATH_REMOTE_ALIASE "Aliase"
76 #define BTIF_STORAGE_PATH_REMOTE_SERVICE "Service"
77 #define BTIF_STORAGE_PATH_REMOTE_HIDINFO "HidInfo"
78 #define BTIF_STORAGE_KEY_ADAPTER_NAME "Name"
79 #define BTIF_STORAGE_KEY_ADAPTER_SCANMODE "ScanMode"
80 #define BTIF_STORAGE_KEY_ADAPTER_DISC_TIMEOUT "DiscoveryTimeout"
81
82 /* This is a local property to add a device found */
83 #define BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP 0xFF
84
85 #define BTIF_STORAGE_GET_ADAPTER_PROP(t,v,l,p) \
86       {p.type=t;p.val=v;p.len=l; btif_storage_get_adapter_property(&p);}
87
88 #define BTIF_STORAGE_GET_REMOTE_PROP(b,t,v,l,p) \
89       {p.type=t;p.val=v;p.len=l;btif_storage_get_remote_device_property(b,&p);}
90
91 #define STORAGE_BDADDR_STRING_SZ           (18)      /* 00:11:22:33:44:55 */
92 #define STORAGE_UUID_STRING_SIZE           (36+1)    /* 00001200-0000-1000-8000-00805f9b34fb; */
93 #define STORAGE_PINLEN_STRING_MAX_SIZE     (2)       /* ascii pinlen max chars */
94 #define STORAGE_KEYTYPE_STRING_MAX_SIZE    (1)       /* ascii keytype max chars */
95
96 #define STORAGE_KEY_TYPE_MAX               (10)
97
98 #define STORAGE_HID_ATRR_MASK_SIZE           (4)
99 #define STORAGE_HID_SUB_CLASS_SIZE           (2)
100 #define STORAGE_HID_APP_ID_SIZE              (2)
101 #define STORAGE_HID_VENDOR_ID_SIZE           (4)
102 #define STORAGE_HID_PRODUCT_ID_SIZE          (4)
103 #define STORAGE_HID_VERSION_SIZE             (4)
104 #define STORAGE_HID_CTRY_CODE_SIZE           (2)
105 #define STORAGE_HID_DESC_LEN_SIZE            (4)
106 #define STORAGE_HID_DESC_MAX_SIZE            (2*512)
107
108 /* <18 char bd addr> <space> LIST< <36 char uuid> <;> > <keytype (dec)> <pinlen> */
109 #define BTIF_REMOTE_SERVICES_ENTRY_SIZE_MAX (STORAGE_BDADDR_STRING_SZ + 1 +\
110                                              STORAGE_UUID_STRING_SIZE*BT_MAX_NUM_UUIDS + \
111                                              STORAGE_PINLEN_STRING_MAX_SIZE +\
112                                              STORAGE_KEYTYPE_STRING_MAX_SIZE)
113
114 #define STORAGE_REMOTE_LINKKEYS_ENTRY_SIZE (LINK_KEY_LEN*2 + 1 + 2 + 1 + 2)
115
116 /* <18 char bd addr> <space>LIST <attr_mask> <space> > <sub_class> <space> <app_id> <space>
117                                 <vendor_id> <space> > <product_id> <space> <version> <space>
118                                 <ctry_code> <space> > <desc_len> <space> <desc_list> <space> */
119 #define BTIF_HID_INFO_ENTRY_SIZE_MAX    (STORAGE_BDADDR_STRING_SZ + 1 +\
120                                          STORAGE_HID_ATRR_MASK_SIZE + 1 +\
121                                          STORAGE_HID_SUB_CLASS_SIZE + 1 +\
122                                          STORAGE_HID_APP_ID_SIZE+ 1 +\
123                                          STORAGE_HID_VENDOR_ID_SIZE+ 1 +\
124                                          STORAGE_HID_PRODUCT_ID_SIZE+ 1 +\
125                                          STORAGE_HID_VERSION_SIZE+ 1 +\
126                                          STORAGE_HID_CTRY_CODE_SIZE+ 1 +\
127                                          STORAGE_HID_DESC_LEN_SIZE+ 1 +\
128                                          STORAGE_HID_DESC_MAX_SIZE+ 1 )
129
130 /* currently remote services is the potentially largest entry */
131 #define BTIF_STORAGE_MAX_LINE_SZ BTIF_REMOTE_SERVICES_ENTRY_SIZE_MAX
132
133 /* check against unv max entry size at compile time */
134 #if (BTIF_STORAGE_ENTRY_MAX_SIZE > UNV_MAXLINE_LENGTH)
135     #error "btif storage entry size exceeds unv max line size"
136 #endif
137
138 #define BTIF_STORAGE_HL_APP          "hl_app"
139 #define BTIF_STORAGE_HL_APP_CB       "hl_app_cb"
140 #define BTIF_STORAGE_HL_APP_DATA     "hl_app_data_"
141 #define BTIF_STORAGE_HL_APP_MDL_DATA "hl_app_mdl_data_"
142
143 /************************************************************************************
144 **  Local type definitions
145 ************************************************************************************/
146 typedef struct
147 {
148     uint32_t num_devices;
149     bt_bdaddr_t devices[BTM_SEC_MAX_DEVICE_RECORDS];
150 } btif_bonded_devices_t;
151
152 /************************************************************************************
153 **  External variables
154 ************************************************************************************/
155 extern UINT16 bta_service_id_to_uuid_lkup_tbl [BTA_MAX_SERVICE_ID];
156 extern bt_bdaddr_t btif_local_bd_addr;
157
158 /************************************************************************************
159 **  External functions
160 ************************************************************************************/
161
162 extern void btif_gatts_add_bonded_dev_from_nv(BD_ADDR bda);
163
164 /************************************************************************************
165 **  Internal Functions
166 ************************************************************************************/
167
168 static bt_status_t btif_in_fetch_bonded_ble_device(const char *remote_bd_addr,int add,
169                                               btif_bonded_devices_t *p_bonded_devices);
170 static bt_status_t btif_in_fetch_bonded_device(const char *bdstr);
171
172 /************************************************************************************
173 **  Static functions
174 ************************************************************************************/
175
176 static int prop2cfg(bt_bdaddr_t *remote_bd_addr, bt_property_t *prop)
177 {
178     bdstr_t bdstr = {0};
179     if(remote_bd_addr)
180         bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
181     BTIF_TRACE_DEBUG("in, bd addr:%s, prop type:%d, len:%d", bdstr, prop->type, prop->len);
182     char value[1024];
183     if(prop->len <= 0 || prop->len > (int)sizeof(value) - 1)
184     {
185         BTIF_TRACE_ERROR("property type:%d, len:%d is invalid", prop->type, prop->len);
186         return FALSE;
187     }
188     switch(prop->type)
189     {
190        case BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP:
191             btif_config_set_int(bdstr,
192                                 BTIF_STORAGE_PATH_REMOTE_DEVTIME, (int)time(NULL));
193             break;
194         case BT_PROPERTY_BDNAME:
195             strncpy(value, (char*)prop->val, prop->len);
196             value[prop->len]='\0';
197             if(remote_bd_addr)
198                 btif_config_set_str(bdstr,
199                                 BTIF_STORAGE_PATH_REMOTE_NAME, value);
200             else btif_config_set_str("Adapter",
201                                 BTIF_STORAGE_KEY_ADAPTER_NAME, value);
202             break;
203         case BT_PROPERTY_REMOTE_FRIENDLY_NAME:
204             strncpy(value, (char*)prop->val, prop->len);
205             value[prop->len]='\0';
206             btif_config_set_str(bdstr, BTIF_STORAGE_PATH_REMOTE_ALIASE, value);
207             break;
208         case BT_PROPERTY_ADAPTER_SCAN_MODE:
209             btif_config_set_int("Adapter",
210                                 BTIF_STORAGE_KEY_ADAPTER_SCANMODE, *(int*)prop->val);
211             break;
212         case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
213             btif_config_set_int("Adapter",
214                                 BTIF_STORAGE_KEY_ADAPTER_DISC_TIMEOUT, *(int*)prop->val);
215             break;
216         case BT_PROPERTY_CLASS_OF_DEVICE:
217             btif_config_set_int(bdstr,
218                                 BTIF_STORAGE_PATH_REMOTE_DEVCLASS, *(int*)prop->val);
219             break;
220         case BT_PROPERTY_TYPE_OF_DEVICE:
221             btif_config_set_int(bdstr,
222                                 BTIF_STORAGE_PATH_REMOTE_DEVTYPE, *(int*)prop->val);
223             break;
224         case BT_PROPERTY_UUIDS:
225         {
226             uint32_t i;
227             char buf[64];
228             value[0] = 0;
229             for (i=0; i < (prop->len)/sizeof(bt_uuid_t); i++)
230             {
231                 bt_uuid_t *p_uuid = (bt_uuid_t*)prop->val + i;
232                 memset(buf, 0, sizeof(buf));
233                 uuid_to_string_legacy(p_uuid, buf);
234                 if (strlen(value) + strlen(buf) + 1 > (int) sizeof(value) - 1) {
235                     android_errorWriteLog(0x534e4554, "73963551");
236                     return FALSE;
237                 }
238                 strcat(value, buf);
239                 //strcat(value, ";");
240                 strcat(value, " ");
241             }
242             btif_config_set_str(bdstr, BTIF_STORAGE_PATH_REMOTE_SERVICE, value);
243             break;
244         }
245         case BT_PROPERTY_REMOTE_VERSION_INFO:
246         {
247             bt_remote_version_t *info = (bt_remote_version_t *)prop->val;
248
249             if (!info)
250                 return FALSE;
251
252             btif_config_set_int(bdstr,
253                                 BTIF_STORAGE_PATH_REMOTE_VER_MFCT, info->manufacturer);
254             btif_config_set_int(bdstr,
255                                 BTIF_STORAGE_PATH_REMOTE_VER_VER, info->version);
256             btif_config_set_int(bdstr,
257                                 BTIF_STORAGE_PATH_REMOTE_VER_SUBVER, info->sub_ver);
258          } break;
259
260         default:
261              BTIF_TRACE_ERROR("Unknown prop type:%d", prop->type);
262              return FALSE;
263     }
264
265     /* save changes if the device was bonded */
266     if (btif_in_fetch_bonded_device(bdstr) == BT_STATUS_SUCCESS) {
267       btif_config_save();
268     }
269
270     return TRUE;
271 }
272
273 static int cfg2prop(bt_bdaddr_t *remote_bd_addr, bt_property_t *prop)
274 {
275     bdstr_t bdstr = {0};
276     if(remote_bd_addr)
277         bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
278     BTIF_TRACE_DEBUG("in, bd addr:%s, prop type:%d, len:%d", bdstr, prop->type, prop->len);
279     if(prop->len <= 0)
280     {
281         BTIF_TRACE_ERROR("property type:%d, len:%d is invalid", prop->type, prop->len);
282         return FALSE;
283     }
284     int ret = FALSE;
285     switch(prop->type)
286     {
287        case BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP:
288             if(prop->len >= (int)sizeof(int))
289                 ret = btif_config_get_int(bdstr,
290                                         BTIF_STORAGE_PATH_REMOTE_DEVTIME, (int*)prop->val);
291             break;
292         case BT_PROPERTY_BDNAME:
293         {
294             int len = prop->len;
295             if(remote_bd_addr)
296                 ret = btif_config_get_str(bdstr,
297                                         BTIF_STORAGE_PATH_REMOTE_NAME, (char*)prop->val, &len);
298             else ret = btif_config_get_str("Adapter",
299                                         BTIF_STORAGE_KEY_ADAPTER_NAME, (char*)prop->val, &len);
300             if(ret && len && len <= prop->len)
301                 prop->len = len - 1;
302             else
303             {
304                 prop->len = 0;
305                 ret = FALSE;
306             }
307             break;
308         }
309         case BT_PROPERTY_REMOTE_FRIENDLY_NAME:
310         {
311             int len = prop->len;
312             ret = btif_config_get_str(bdstr,
313                                        BTIF_STORAGE_PATH_REMOTE_ALIASE, (char*)prop->val, &len);
314             if(ret && len && len <= prop->len)
315                 prop->len = len - 1;
316             else
317             {
318                 prop->len = 0;
319                 ret = FALSE;
320             }
321             break;
322         }
323         case BT_PROPERTY_ADAPTER_SCAN_MODE:
324            if(prop->len >= (int)sizeof(int))
325                 ret = btif_config_get_int("Adapter",
326                                           BTIF_STORAGE_KEY_ADAPTER_SCANMODE, (int*)prop->val);
327            break;
328         case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
329            if(prop->len >= (int)sizeof(int))
330                 ret = btif_config_get_int("Adapter",
331                                           BTIF_STORAGE_KEY_ADAPTER_DISC_TIMEOUT, (int*)prop->val);
332             break;
333         case BT_PROPERTY_CLASS_OF_DEVICE:
334             if(prop->len >= (int)sizeof(int))
335                 ret = btif_config_get_int(bdstr,
336                                 BTIF_STORAGE_PATH_REMOTE_DEVCLASS, (int*)prop->val);
337             break;
338         case BT_PROPERTY_TYPE_OF_DEVICE:
339             if(prop->len >= (int)sizeof(int))
340                 ret = btif_config_get_int(bdstr, BTIF_STORAGE_PATH_REMOTE_DEVTYPE, (int*)prop->val);
341             break;
342         case BT_PROPERTY_UUIDS:
343         {
344             char value[1280];
345             int size = sizeof(value);
346             if(btif_config_get_str(bdstr,
347                                     BTIF_STORAGE_PATH_REMOTE_SERVICE, value, &size))
348             {
349                 bt_uuid_t *p_uuid = (bt_uuid_t*)prop->val;
350                 size_t num_uuids = btif_split_uuids_string(value, p_uuid, BT_MAX_NUM_UUIDS);
351                 prop->len = num_uuids * sizeof(bt_uuid_t);
352                 ret = TRUE;
353             }
354             else
355             {
356                 prop->val = NULL;
357                 prop->len = 0;
358             }
359         } break;
360
361         case BT_PROPERTY_REMOTE_VERSION_INFO:
362         {
363             bt_remote_version_t *info = (bt_remote_version_t *)prop->val;
364
365             if(prop->len >= (int)sizeof(bt_remote_version_t))
366             {
367                 ret = btif_config_get_int(bdstr,
368                                 BTIF_STORAGE_PATH_REMOTE_VER_MFCT, &info->manufacturer);
369
370                 if (ret == TRUE)
371                     ret = btif_config_get_int(bdstr,
372                                 BTIF_STORAGE_PATH_REMOTE_VER_VER, &info->version);
373
374                 if (ret == TRUE)
375                     ret = btif_config_get_int(bdstr,
376                                 BTIF_STORAGE_PATH_REMOTE_VER_SUBVER, &info->sub_ver);
377             }
378          } break;
379
380         default:
381             BTIF_TRACE_ERROR("Unknow prop type:%d", prop->type);
382             return FALSE;
383     }
384     return ret;
385 }
386
387 /*******************************************************************************
388 **
389 ** Function         btif_in_fetch_bonded_devices
390 **
391 ** Description      Internal helper function to fetch the bonded devices
392 **                  from NVRAM
393 **
394 ** Returns          BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise
395 **
396 *******************************************************************************/
397 static bt_status_t btif_in_fetch_bonded_device(const char *bdstr)
398 {
399     BOOLEAN bt_linkkey_file_found=FALSE;
400
401         LINK_KEY link_key;
402         size_t size = sizeof(link_key);
403         if(btif_config_get_bin(bdstr, "LinkKey", (uint8_t *)link_key, &size))
404         {
405             int linkkey_type;
406             if(btif_config_get_int(bdstr, "LinkKeyType", &linkkey_type))
407             {
408                 bt_linkkey_file_found = TRUE;
409             }
410             else
411             {
412                 bt_linkkey_file_found = FALSE;
413             }
414         }
415 #if (BLE_INCLUDED == TRUE)
416         if((btif_in_fetch_bonded_ble_device(bdstr, FALSE, NULL) != BT_STATUS_SUCCESS)
417                 && (!bt_linkkey_file_found))
418         {
419             BTIF_TRACE_DEBUG("Remote device:%s, no link key or ble key found", bdstr);
420             return BT_STATUS_FAIL;
421         }
422 #else
423         if((!bt_linkkey_file_found))
424         {
425             BTIF_TRACE_DEBUG("Remote device:%s, no link key found", bdstr);
426             return BT_STATUS_FAIL;
427         }
428 #endif
429     return BT_STATUS_SUCCESS;
430 }
431
432 /*******************************************************************************
433 **
434 ** Function         btif_in_fetch_bonded_devices
435 **
436 ** Description      Internal helper function to fetch the bonded devices
437 **                  from NVRAM
438 **
439 ** Returns          BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise
440 **
441 *******************************************************************************/
442 static bt_status_t btif_in_fetch_bonded_devices(btif_bonded_devices_t *p_bonded_devices, int add)
443 {
444     memset(p_bonded_devices, 0, sizeof(btif_bonded_devices_t));
445
446     BOOLEAN bt_linkkey_file_found=FALSE;
447     int device_type;
448
449     for (const btif_config_section_iter_t *iter = btif_config_section_begin(); iter != btif_config_section_end(); iter = btif_config_section_next(iter)) {
450         const char *name = btif_config_section_name(iter);
451         if (!string_is_bdaddr(name))
452             continue;
453
454         BTIF_TRACE_DEBUG("Remote device:%s", name);
455         LINK_KEY link_key;
456         size_t size = sizeof(link_key);
457         if (btif_config_get_bin(name, "LinkKey", link_key, &size)) {
458             int linkkey_type;
459             if (btif_config_get_int(name, "LinkKeyType", &linkkey_type)) {
460                 bt_bdaddr_t bd_addr;
461                 string_to_bdaddr(name, &bd_addr);
462                 if (add) {
463                     DEV_CLASS dev_class = {0, 0, 0};
464                     int cod;
465                     int pin_length = 0;
466                     if (btif_config_get_int(name, "DevClass", &cod))
467                         uint2devclass((UINT32)cod, dev_class);
468                     btif_config_get_int(name, "PinLength", &pin_length);
469                     BTA_DmAddDevice(bd_addr.address, dev_class, link_key, 0, 0,
470                                     (UINT8)linkkey_type, 0, pin_length);
471
472 #if BLE_INCLUDED == TRUE
473                     if (btif_config_get_int(name, "DevType", &device_type) &&
474                         (device_type == BT_DEVICE_TYPE_DUMO) ) {
475                         btif_gatts_add_bonded_dev_from_nv(bd_addr.address);
476                     }
477 #endif
478                 }
479                 bt_linkkey_file_found = TRUE;
480                 memcpy(&p_bonded_devices->devices[p_bonded_devices->num_devices++], &bd_addr, sizeof(bt_bdaddr_t));
481             } else {
482 #if (BLE_INCLUDED == TRUE)
483                 bt_linkkey_file_found = FALSE;
484 #else
485                 BTIF_TRACE_ERROR("bounded device:%s, LinkKeyType or PinLength is invalid", name);
486 #endif
487             }
488         }
489 #if (BLE_INCLUDED == TRUE)
490         if (!btif_in_fetch_bonded_ble_device(name, add, p_bonded_devices) &&
491             !bt_linkkey_file_found) {
492             BTIF_TRACE_DEBUG("Remote device:%s, no link key or ble key found", name);
493         }
494 #else
495         if(!bt_linkkey_file_found)
496             BTIF_TRACE_DEBUG("Remote device:%s, no link key", name);
497 #endif
498     }
499     return BT_STATUS_SUCCESS;
500 }
501
502 static void btif_read_le_key(const uint8_t key_type, const size_t key_len, bt_bdaddr_t bd_addr,
503                  const uint8_t addr_type, const bool add_key, bool *device_added, bool *key_found)
504 {
505     assert(device_added);
506     assert(key_found);
507
508     char buffer[100];
509     memset(buffer, 0, sizeof(buffer));
510
511     if (btif_storage_get_ble_bonding_key(&bd_addr, key_type, buffer, key_len) == BT_STATUS_SUCCESS)
512     {
513         if (add_key)
514         {
515             BD_ADDR bta_bd_addr;
516             bdcpy(bta_bd_addr, bd_addr.address);
517
518             if (!*device_added)
519             {
520                 BTA_DmAddBleDevice(bta_bd_addr, addr_type, BT_DEVICE_TYPE_BLE);
521                 *device_added = true;
522             }
523
524             char bd_str[20] = {0};
525             BTIF_TRACE_DEBUG("%s() Adding key type %d for %s", __func__,
526                 key_type, bdaddr_to_string(&bd_addr, bd_str, sizeof(bd_str)));
527             BTA_DmAddBleKey(bta_bd_addr, (tBTA_LE_KEY_VALUE *)buffer, key_type);
528         }
529
530         *key_found = true;
531     }
532 }
533
534 /*******************************************************************************
535  * Functions
536  *
537  * Functions are synchronous and can be called by both from internal modules
538  * such as BTIF_DM and by external entiries from HAL via BTIF_context_switch.
539  * For OUT parameters, the caller is expected to provide the memory.
540  * Caller is expected to provide a valid pointer to 'property->value' based on
541  * the property->type.
542  *******************************************************************************/
543
544 /*******************************************************************************
545 **
546 ** Function         btif_split_uuids_string
547 **
548 ** Description      Internal helper function to split the string of UUIDs
549 **                  read from the NVRAM to an array
550 **
551 ** Returns          Number of UUIDs parsed from the supplied string
552 **
553 *******************************************************************************/
554 size_t btif_split_uuids_string(const char *str, bt_uuid_t *p_uuid, size_t max_uuids)
555 {
556     assert(str);
557     assert(p_uuid);
558
559     size_t num_uuids = 0;
560     while (str && num_uuids < max_uuids)
561     {
562         bool rc = string_to_uuid(str, p_uuid++);
563         if (!rc) break;
564         num_uuids++;
565         str = strchr(str, ' ');
566         if (str) str++;
567     }
568
569     return num_uuids;
570 }
571
572 /*******************************************************************************
573 **
574 ** Function         btif_storage_get_adapter_property
575 **
576 ** Description      BTIF storage API - Fetches the adapter property->type
577 **                  from NVRAM and fills property->val.
578 **                  Caller should provide memory for property->val and
579 **                  set the property->val
580 **
581 ** Returns          BT_STATUS_SUCCESS if the fetch was successful,
582 **                  BT_STATUS_FAIL otherwise
583 **
584 *******************************************************************************/
585 bt_status_t btif_storage_get_adapter_property(bt_property_t *property)
586 {
587
588     /* Special handling for adapter BD_ADDR and BONDED_DEVICES */
589     if (property->type == BT_PROPERTY_BDADDR)
590     {
591         bt_bdaddr_t *bd_addr = (bt_bdaddr_t*)property->val;
592         /* This has been cached in btif. Just fetch it from there */
593         memcpy(bd_addr, &btif_local_bd_addr, sizeof(bt_bdaddr_t));
594         property->len = sizeof(bt_bdaddr_t);
595         return BT_STATUS_SUCCESS;
596     }
597     else if (property->type == BT_PROPERTY_ADAPTER_BONDED_DEVICES)
598     {
599         btif_bonded_devices_t bonded_devices;
600
601         btif_in_fetch_bonded_devices(&bonded_devices, 0);
602
603         BTIF_TRACE_DEBUG("%s: Number of bonded devices: %d Property:BT_PROPERTY_ADAPTER_BONDED_DEVICES", __FUNCTION__, bonded_devices.num_devices);
604
605         if (bonded_devices.num_devices > 0)
606         {
607             property->len = bonded_devices.num_devices * sizeof(bt_bdaddr_t);
608             memcpy(property->val, bonded_devices.devices, property->len);
609         }
610
611         /* if there are no bonded_devices, then length shall be 0 */
612         return BT_STATUS_SUCCESS;
613     }
614     else if (property->type == BT_PROPERTY_UUIDS)
615     {
616         /* publish list of local supported services */
617         bt_uuid_t *p_uuid = (bt_uuid_t*)property->val;
618         uint32_t num_uuids = 0;
619         uint32_t i;
620
621         tBTA_SERVICE_MASK service_mask = btif_get_enabled_services_mask();
622         LOG_INFO(LOG_TAG, "%s service_mask:0x%x", __FUNCTION__, service_mask);
623         for (i=0; i < BTA_MAX_SERVICE_ID; i++)
624         {
625             /* This should eventually become a function when more services are enabled */
626             if (service_mask
627                 &(tBTA_SERVICE_MASK)(1 << i))
628             {
629                 switch (i)
630                 {
631                     case BTA_HFP_SERVICE_ID:
632                         {
633                             uuid16_to_uuid128(UUID_SERVCLASS_AG_HANDSFREE,
634                                               p_uuid+num_uuids);
635                             num_uuids++;
636                         }
637                     /* intentional fall through: Send both BFP & HSP UUIDs if HFP is enabled */
638                     case BTA_HSP_SERVICE_ID:
639                         {
640                             uuid16_to_uuid128(UUID_SERVCLASS_HEADSET_AUDIO_GATEWAY,
641                                               p_uuid+num_uuids);
642                             num_uuids++;
643                         }break;
644                     case BTA_A2DP_SOURCE_SERVICE_ID:
645                         {
646                             uuid16_to_uuid128(UUID_SERVCLASS_AUDIO_SOURCE,
647                                               p_uuid+num_uuids);
648                             num_uuids++;
649                         }break;
650                     case BTA_A2DP_SINK_SERVICE_ID:
651                         {
652                             uuid16_to_uuid128(UUID_SERVCLASS_AUDIO_SINK,
653                                               p_uuid+num_uuids);
654                             num_uuids++;
655                         }break;
656                     case BTA_HFP_HS_SERVICE_ID:
657                         {
658                             uuid16_to_uuid128(UUID_SERVCLASS_HF_HANDSFREE,
659                                               p_uuid+num_uuids);
660                             num_uuids++;
661                         }break;
662                 }
663             }
664         }
665         property->len = (num_uuids)*sizeof(bt_uuid_t);
666         return BT_STATUS_SUCCESS;
667     }
668
669     /* fall through for other properties */
670     if(!cfg2prop(NULL, property))
671     {
672         return btif_dm_get_adapter_property(property);
673     }
674     return BT_STATUS_SUCCESS;
675  }
676
677 /*******************************************************************************
678 **
679 ** Function         btif_storage_set_adapter_property
680 **
681 ** Description      BTIF storage API - Stores the adapter property
682 **                  to NVRAM
683 **
684 ** Returns          BT_STATUS_SUCCESS if the store was successful,
685 **                  BT_STATUS_FAIL otherwise
686 **
687 *******************************************************************************/
688 bt_status_t btif_storage_set_adapter_property(bt_property_t *property)
689 {
690     return prop2cfg(NULL, property) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
691 }
692
693 /*******************************************************************************
694 **
695 ** Function         btif_storage_get_remote_device_property
696 **
697 ** Description      BTIF storage API - Fetches the remote device property->type
698 **                  from NVRAM and fills property->val.
699 **                  Caller should provide memory for property->val and
700 **                  set the property->val
701 **
702 ** Returns          BT_STATUS_SUCCESS if the fetch was successful,
703 **                  BT_STATUS_FAIL otherwise
704 **
705 *******************************************************************************/
706 bt_status_t btif_storage_get_remote_device_property(bt_bdaddr_t *remote_bd_addr,
707                                                     bt_property_t *property)
708 {
709     return cfg2prop(remote_bd_addr, property) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
710 }
711 /*******************************************************************************
712 **
713 ** Function         btif_storage_set_remote_device_property
714 **
715 ** Description      BTIF storage API - Stores the remote device property
716 **                  to NVRAM
717 **
718 ** Returns          BT_STATUS_SUCCESS if the store was successful,
719 **                  BT_STATUS_FAIL otherwise
720 **
721 *******************************************************************************/
722 bt_status_t btif_storage_set_remote_device_property(bt_bdaddr_t *remote_bd_addr,
723                                                     bt_property_t *property)
724 {
725     return prop2cfg(remote_bd_addr, property) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
726 }
727
728 /*******************************************************************************
729 **
730 ** Function         btif_storage_add_remote_device
731 **
732 ** Description      BTIF storage API - Adds a newly discovered device to NVRAM
733 **                  along with the timestamp. Also, stores the various
734 **                  properties - RSSI, BDADDR, NAME (if found in EIR)
735 **
736 ** Returns          BT_STATUS_SUCCESS if the store was successful,
737 **                  BT_STATUS_FAIL otherwise
738 **
739 *******************************************************************************/
740 bt_status_t btif_storage_add_remote_device(bt_bdaddr_t *remote_bd_addr,
741                                            uint32_t num_properties,
742                                            bt_property_t *properties)
743 {
744     uint32_t i = 0;
745     /* TODO: If writing a property, fails do we go back undo the earlier
746      * written properties? */
747     for (i=0; i < num_properties; i++)
748     {
749         /* Ignore the RSSI as this is not stored in DB */
750         if (properties[i].type == BT_PROPERTY_REMOTE_RSSI)
751             continue;
752
753         /* BD_ADDR for remote device needs special handling as we also store timestamp */
754         if (properties[i].type == BT_PROPERTY_BDADDR)
755         {
756             bt_property_t addr_prop;
757             memcpy(&addr_prop, &properties[i], sizeof(bt_property_t));
758             addr_prop.type = BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP;
759             btif_storage_set_remote_device_property(remote_bd_addr,
760                                                     &addr_prop);
761         }
762         else
763         {
764             btif_storage_set_remote_device_property(remote_bd_addr,
765                                                     &properties[i]);
766         }
767     }
768     return BT_STATUS_SUCCESS;
769 }
770
771 /*******************************************************************************
772 **
773 ** Function         btif_storage_add_bonded_device
774 **
775 ** Description      BTIF storage API - Adds the newly bonded device to NVRAM
776 **                  along with the link-key, Key type and Pin key length
777 **
778 ** Returns          BT_STATUS_SUCCESS if the store was successful,
779 **                  BT_STATUS_FAIL otherwise
780 **
781 *******************************************************************************/
782
783 bt_status_t btif_storage_add_bonded_device(bt_bdaddr_t *remote_bd_addr,
784                                            LINK_KEY link_key,
785                                            uint8_t key_type,
786                                            uint8_t pin_length)
787 {
788     bdstr_t bdstr;
789     bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
790     int ret = btif_config_set_int(bdstr, "LinkKeyType", (int)key_type);
791     ret &= btif_config_set_int(bdstr, "PinLength", (int)pin_length);
792     ret &= btif_config_set_bin(bdstr, "LinkKey", link_key, sizeof(LINK_KEY));
793
794     if (is_restricted_mode()) {
795         BTIF_TRACE_WARNING("%s: '%s' pairing will be removed if unrestricted",
796                          __func__, bdstr);
797         btif_config_set_int(bdstr, "Restricted", 1);
798     }
799
800     /* write bonded info immediately */
801     btif_config_flush();
802     return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
803 }
804
805 /*******************************************************************************
806 **
807 ** Function         btif_storage_remove_bonded_device
808 **
809 ** Description      BTIF storage API - Deletes the bonded device from NVRAM
810 **
811 ** Returns          BT_STATUS_SUCCESS if the deletion was successful,
812 **                  BT_STATUS_FAIL otherwise
813 **
814 *******************************************************************************/
815 bt_status_t btif_storage_remove_bonded_device(bt_bdaddr_t *remote_bd_addr)
816 {
817     bdstr_t bdstr;
818     bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
819     BTIF_TRACE_DEBUG("in bd addr:%s", bdstr);
820
821 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
822     btif_storage_remove_ble_bonding_keys(remote_bd_addr);
823 #endif
824
825     int ret = 1;
826     if(btif_config_exist(bdstr, "LinkKeyType"))
827         ret &= btif_config_remove(bdstr, "LinkKeyType");
828     if(btif_config_exist(bdstr, "PinLength"))
829         ret &= btif_config_remove(bdstr, "PinLength");
830     if(btif_config_exist(bdstr, "LinkKey"))
831         ret &= btif_config_remove(bdstr, "LinkKey");
832     /* write bonded info immediately */
833     btif_config_flush();
834     return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
835
836 }
837
838 /* Some devices hardcode sample LTK value from spec, instead of generating one.
839  * Treat such devices as insecure, and remove such bonds when bluetooth restarts.
840  * Removing them after disconnection is handled separately.
841  *
842  * We still allow such devices to bond in order to give the user a chance to update
843  * firmware.
844  */
845 static void remove_devices_with_sample_ltk() {
846     list_t *bad_ltk = list_new(osi_free);
847
848     for (const btif_config_section_iter_t *iter = btif_config_section_begin(); iter != btif_config_section_end(); iter = btif_config_section_next(iter)) {
849         const char *name = btif_config_section_name(iter);
850         if (!string_is_bdaddr(name)) {
851             continue;
852         }
853
854         bt_bdaddr_t *bda = osi_malloc(sizeof(bt_bdaddr_t));
855         string_to_bdaddr(name, bda);
856
857         tBTA_LE_KEY_VALUE key;
858         memset(&key, 0, sizeof(key));
859
860         if (btif_storage_get_ble_bonding_key(bda, BTIF_DM_LE_KEY_PENC, (char*)&key, sizeof(tBTM_LE_PENC_KEYS)) ==
861               BT_STATUS_SUCCESS) {
862               if (is_sample_ltk(key.penc_key.ltk)) {
863                   list_append(bad_ltk, (void*)bda);
864               }
865         }
866     }
867
868     for (list_node_t *sn = list_begin(bad_ltk); sn != list_end(bad_ltk); sn = list_next(sn)) {
869         android_errorWriteLog(0x534e4554, "128437297");
870         BTIF_TRACE_ERROR("%s: removing bond to device using test TLK", __func__);
871
872         bt_bdaddr_t *bda = (bt_bdaddr_t*)list_node(sn);
873         btif_storage_remove_bonded_device(bda);
874     }
875
876     list_free(bad_ltk);
877 }
878
879 /*******************************************************************************
880 **
881 ** Function         btif_storage_load_bonded_devices
882 **
883 ** Description      BTIF storage API - Loads all the bonded devices from NVRAM
884 **                  and adds to the BTA.
885 **                  Additionally, this API also invokes the adaper_properties_cb
886 **                  and remote_device_properties_cb for each of the bonded devices.
887 **
888 ** Returns          BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise
889 **
890 *******************************************************************************/
891 bt_status_t btif_storage_load_bonded_devices(void)
892 {
893     btif_bonded_devices_t bonded_devices;
894     uint32_t i = 0;
895     bt_property_t adapter_props[6];
896     uint32_t num_props = 0;
897     bt_property_t remote_properties[8];
898     bt_bdaddr_t addr;
899     bt_bdname_t name, alias;
900     bt_scan_mode_t mode;
901     uint32_t disc_timeout;
902     bt_uuid_t local_uuids[BT_MAX_NUM_UUIDS];
903     bt_uuid_t remote_uuids[BT_MAX_NUM_UUIDS];
904
905     remove_devices_with_sample_ltk();
906
907     btif_in_fetch_bonded_devices(&bonded_devices, 1);
908
909     /* Now send the adapter_properties_cb with all adapter_properties */
910     {
911         memset(adapter_props, 0, sizeof(adapter_props));
912
913         /* BD_ADDR */
914         BTIF_STORAGE_GET_ADAPTER_PROP(BT_PROPERTY_BDADDR, &addr, sizeof(addr),
915                                       adapter_props[num_props]);
916         num_props++;
917
918         /* BD_NAME */
919         BTIF_STORAGE_GET_ADAPTER_PROP(BT_PROPERTY_BDNAME, &name, sizeof(name),
920                                       adapter_props[num_props]);
921         num_props++;
922
923         /* SCAN_MODE */
924         /* TODO: At the time of BT on, always report the scan mode as 0 irrespective
925          of the scan_mode during the previous enable cycle.
926          This needs to be re-visited as part of the app/stack enable sequence
927          synchronization */
928         mode = BT_SCAN_MODE_NONE;
929         adapter_props[num_props].type = BT_PROPERTY_ADAPTER_SCAN_MODE;
930         adapter_props[num_props].len = sizeof(mode);
931         adapter_props[num_props].val = &mode;
932         num_props++;
933
934         /* DISC_TIMEOUT */
935         BTIF_STORAGE_GET_ADAPTER_PROP(BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT,
936                                       &disc_timeout, sizeof(disc_timeout),
937                                       adapter_props[num_props]);
938         num_props++;
939
940         /* BONDED_DEVICES */
941         bt_bdaddr_t *devices_list =
942             (bt_bdaddr_t *)osi_malloc(sizeof(bt_bdaddr_t) * bonded_devices.num_devices);
943         adapter_props[num_props].type = BT_PROPERTY_ADAPTER_BONDED_DEVICES;
944         adapter_props[num_props].len = bonded_devices.num_devices * sizeof(bt_bdaddr_t);
945         adapter_props[num_props].val = devices_list;
946         for (i=0; i < bonded_devices.num_devices; i++)
947         {
948             memcpy(devices_list + i, &bonded_devices.devices[i], sizeof(bt_bdaddr_t));
949         }
950         num_props++;
951
952         /* LOCAL UUIDs */
953         BTIF_STORAGE_GET_ADAPTER_PROP(BT_PROPERTY_UUIDS,
954                                       local_uuids, sizeof(local_uuids),
955                                       adapter_props[num_props]);
956         num_props++;
957
958         btif_adapter_properties_evt(BT_STATUS_SUCCESS, num_props, adapter_props);
959
960         osi_free(devices_list);
961     }
962
963     BTIF_TRACE_EVENT("%s: %d bonded devices found", __FUNCTION__, bonded_devices.num_devices);
964
965     {
966         for (i = 0; i < bonded_devices.num_devices; i++)
967         {
968             bt_bdaddr_t *p_remote_addr;
969
970             /*
971              * TODO: improve handling of missing fields in NVRAM.
972              */
973             uint32_t cod = 0;
974             uint32_t devtype = 0;
975
976             num_props = 0;
977             p_remote_addr = &bonded_devices.devices[i];
978             memset(remote_properties, 0, sizeof(remote_properties));
979             BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_BDNAME,
980                                          &name, sizeof(name),
981                                          remote_properties[num_props]);
982             num_props++;
983
984             BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_REMOTE_FRIENDLY_NAME,
985                                          &alias, sizeof(alias),
986                                          remote_properties[num_props]);
987             num_props++;
988
989             BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_CLASS_OF_DEVICE,
990                                          &cod, sizeof(cod),
991                                          remote_properties[num_props]);
992             num_props++;
993
994             BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_TYPE_OF_DEVICE,
995                                          &devtype, sizeof(devtype),
996                                          remote_properties[num_props]);
997             num_props++;
998
999             BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_UUIDS,
1000                                          remote_uuids, sizeof(remote_uuids),
1001                                          remote_properties[num_props]);
1002             num_props++;
1003
1004             btif_remote_properties_evt(BT_STATUS_SUCCESS, p_remote_addr,
1005                                        num_props, remote_properties);
1006         }
1007     }
1008     return BT_STATUS_SUCCESS;
1009 }
1010
1011 #if (BLE_INCLUDED == TRUE)
1012
1013 /*******************************************************************************
1014 **
1015 ** Function         btif_storage_add_ble_bonding_key
1016 **
1017 ** Description      BTIF storage API - Adds the newly bonded device to NVRAM
1018 **                  along with the ble-key, Key type and Pin key length
1019 **
1020 ** Returns          BT_STATUS_SUCCESS if the store was successful,
1021 **                  BT_STATUS_FAIL otherwise
1022 **
1023 *******************************************************************************/
1024
1025 bt_status_t btif_storage_add_ble_bonding_key(bt_bdaddr_t *remote_bd_addr,
1026                                            char *key,
1027                                            UINT8 key_type,
1028                                            UINT8 key_length)
1029 {
1030     bdstr_t bdstr;
1031     bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
1032     const char* name;
1033     switch(key_type)
1034     {
1035         case BTIF_DM_LE_KEY_PENC:
1036             name = "LE_KEY_PENC";
1037             break;
1038         case BTIF_DM_LE_KEY_PID:
1039             name = "LE_KEY_PID";
1040             break;
1041         case BTIF_DM_LE_KEY_PCSRK:
1042             name = "LE_KEY_PCSRK";
1043             break;
1044         case BTIF_DM_LE_KEY_LENC:
1045             name = "LE_KEY_LENC";
1046             break;
1047         case BTIF_DM_LE_KEY_LCSRK:
1048             name = "LE_KEY_LCSRK";
1049             break;
1050         case BTIF_DM_LE_KEY_LID:
1051             name = "LE_KEY_LID";
1052             break;
1053         default:
1054             return BT_STATUS_FAIL;
1055     }
1056     int ret = btif_config_set_bin(bdstr, name, (const uint8_t *)key, key_length);
1057     btif_config_save();
1058     return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1059 }
1060
1061 /*******************************************************************************
1062 **
1063 ** Function         btif_storage_get_ble_bonding_key
1064 **
1065 ** Description
1066 **
1067 ** Returns          BT_STATUS_SUCCESS if the fetch was successful,
1068 **                  BT_STATUS_FAIL otherwise
1069 **
1070 *******************************************************************************/
1071 bt_status_t btif_storage_get_ble_bonding_key(bt_bdaddr_t *remote_bd_addr,
1072                                              UINT8 key_type,
1073                                              char *key_value,
1074                                              int key_length)
1075 {
1076     bdstr_t bdstr;
1077     bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
1078     const char* name;
1079     switch(key_type)
1080     {
1081         case BTIF_DM_LE_KEY_PENC:
1082             name = "LE_KEY_PENC";
1083             break;
1084         case BTIF_DM_LE_KEY_PID:
1085             name = "LE_KEY_PID";
1086             break;
1087         case BTIF_DM_LE_KEY_PCSRK:
1088             name = "LE_KEY_PCSRK";
1089             break;
1090         case BTIF_DM_LE_KEY_LENC:
1091             name = "LE_KEY_LENC";
1092             break;
1093         case BTIF_DM_LE_KEY_LCSRK:
1094             name = "LE_KEY_LCSRK";
1095             break;
1096         case BTIF_DM_LE_KEY_LID:
1097             name =  "LE_KEY_LID";
1098         default:
1099             return BT_STATUS_FAIL;
1100     }
1101     size_t length = key_length;
1102     int ret = btif_config_get_bin(bdstr, name, (uint8_t *)key_value, &length);
1103     return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1104
1105 }
1106
1107 /*******************************************************************************
1108 **
1109 ** Function         btif_storage_remove_ble_keys
1110 **
1111 ** Description      BTIF storage API - Deletes the bonded device from NVRAM
1112 **
1113 ** Returns          BT_STATUS_SUCCESS if the deletion was successful,
1114 **                  BT_STATUS_FAIL otherwise
1115 **
1116 *******************************************************************************/
1117 bt_status_t btif_storage_remove_ble_bonding_keys(bt_bdaddr_t *remote_bd_addr)
1118 {
1119     bdstr_t bdstr;
1120     bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
1121     BTIF_TRACE_DEBUG(" %s in bd addr:%s",__FUNCTION__, bdstr);
1122     int ret = 1;
1123     if(btif_config_exist(bdstr, "LE_KEY_PENC"))
1124         ret &= btif_config_remove(bdstr, "LE_KEY_PENC");
1125     if(btif_config_exist(bdstr, "LE_KEY_PID"))
1126         ret &= btif_config_remove(bdstr, "LE_KEY_PID");
1127     if(btif_config_exist(bdstr, "LE_KEY_PCSRK"))
1128         ret &= btif_config_remove(bdstr, "LE_KEY_PCSRK");
1129     if(btif_config_exist(bdstr, "LE_KEY_LENC"))
1130         ret &= btif_config_remove(bdstr, "LE_KEY_LENC");
1131     if(btif_config_exist(bdstr, "LE_KEY_LCSRK"))
1132         ret &= btif_config_remove(bdstr, "LE_KEY_LCSRK");
1133     btif_config_save();
1134     return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1135 }
1136
1137 /*******************************************************************************
1138 **
1139 ** Function         btif_storage_add_ble_local_key
1140 **
1141 ** Description      BTIF storage API - Adds the ble key to NVRAM
1142 **
1143 ** Returns          BT_STATUS_SUCCESS if the store was successful,
1144 **                  BT_STATUS_FAIL otherwise
1145 **
1146 *******************************************************************************/
1147 bt_status_t btif_storage_add_ble_local_key(char *key,
1148                                            uint8_t key_type,
1149                                            uint8_t key_length)
1150 {
1151     const char* name;
1152     switch(key_type)
1153     {
1154         case BTIF_DM_LE_LOCAL_KEY_IR:
1155             name = "LE_LOCAL_KEY_IR";
1156             break;
1157         case BTIF_DM_LE_LOCAL_KEY_IRK:
1158             name = "LE_LOCAL_KEY_IRK";
1159             break;
1160         case BTIF_DM_LE_LOCAL_KEY_DHK:
1161             name = "LE_LOCAL_KEY_DHK";
1162             break;
1163         case BTIF_DM_LE_LOCAL_KEY_ER:
1164             name = "LE_LOCAL_KEY_ER";
1165             break;
1166         default:
1167             return BT_STATUS_FAIL;
1168     }
1169     int ret = btif_config_set_bin("Adapter", name, (const uint8_t *)key, key_length);
1170     btif_config_save();
1171     return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1172 }
1173
1174 /*******************************************************************************
1175 **
1176 ** Function         btif_storage_get_ble_local_key
1177 **
1178 ** Description
1179 **
1180 ** Returns          BT_STATUS_SUCCESS if the fetch was successful,
1181 **                  BT_STATUS_FAIL otherwise
1182 **
1183 *******************************************************************************/
1184 bt_status_t btif_storage_get_ble_local_key(UINT8 key_type,
1185                                            char *key_value,
1186                                            int key_length)
1187 {
1188     const char* name;
1189     switch(key_type)
1190     {
1191         case BTIF_DM_LE_LOCAL_KEY_IR:
1192             name = "LE_LOCAL_KEY_IR";
1193             break;
1194         case BTIF_DM_LE_LOCAL_KEY_IRK:
1195             name = "LE_LOCAL_KEY_IRK";
1196             break;
1197         case BTIF_DM_LE_LOCAL_KEY_DHK:
1198             name = "LE_LOCAL_KEY_DHK";
1199             break;
1200         case BTIF_DM_LE_LOCAL_KEY_ER:
1201             name = "LE_LOCAL_KEY_ER";
1202             break;
1203         default:
1204             return BT_STATUS_FAIL;
1205     }
1206     size_t length = key_length;
1207     int ret = btif_config_get_bin("Adapter", name, (uint8_t *)key_value, &length);
1208     return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1209 }
1210
1211 /*******************************************************************************
1212 **
1213 ** Function         btif_storage_remove_ble_local_keys
1214 **
1215 ** Description      BTIF storage API - Deletes the bonded device from NVRAM
1216 **
1217 ** Returns          BT_STATUS_SUCCESS if the deletion was successful,
1218 **                  BT_STATUS_FAIL otherwise
1219 **
1220 *******************************************************************************/
1221 bt_status_t btif_storage_remove_ble_local_keys(void)
1222 {
1223     int ret = 1;
1224     if(btif_config_exist("Adapter", "LE_LOCAL_KEY_IR"))
1225         ret &= btif_config_remove("Adapter", "LE_LOCAL_KEY_IR");
1226     if(btif_config_exist("Adapter", "LE_LOCAL_KEY_IRK"))
1227         ret &= btif_config_remove("Adapter", "LE_LOCAL_KEY_IRK");
1228     if(btif_config_exist("Adapter", "LE_LOCAL_KEY_DHK"))
1229         ret &= btif_config_remove("Adapter", "LE_LOCAL_KEY_DHK");
1230     if(btif_config_exist("Adapter", "LE_LOCAL_KEY_ER"))
1231         ret &= btif_config_remove("Adapter", "LE_LOCAL_KEY_ER");
1232     btif_config_save();
1233     return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1234 }
1235
1236 static bt_status_t btif_in_fetch_bonded_ble_device(const char *remote_bd_addr, int add, btif_bonded_devices_t *p_bonded_devices)
1237 {
1238     int device_type;
1239     int addr_type;
1240     bt_bdaddr_t bd_addr;
1241     BD_ADDR bta_bd_addr;
1242     bool device_added = false;
1243     bool key_found = false;
1244
1245     if (!btif_config_get_int(remote_bd_addr, "DevType", &device_type))
1246         return BT_STATUS_FAIL;
1247
1248     if ((device_type & BT_DEVICE_TYPE_BLE) == BT_DEVICE_TYPE_BLE)
1249     {
1250         BTIF_TRACE_DEBUG("%s Found a LE device: %s", __func__, remote_bd_addr);
1251
1252         string_to_bdaddr(remote_bd_addr, &bd_addr);
1253         bdcpy(bta_bd_addr, bd_addr.address);
1254
1255         if (btif_storage_get_remote_addr_type(&bd_addr, &addr_type) != BT_STATUS_SUCCESS)
1256         {
1257             addr_type = BLE_ADDR_PUBLIC;
1258             btif_storage_set_remote_addr_type(&bd_addr, BLE_ADDR_PUBLIC);
1259         }
1260
1261         btif_read_le_key(BTIF_DM_LE_KEY_PENC, sizeof(tBTM_LE_PENC_KEYS),
1262                          bd_addr, addr_type, add, &device_added, &key_found);
1263
1264         btif_read_le_key(BTIF_DM_LE_KEY_PID, sizeof(tBTM_LE_PID_KEYS),
1265                          bd_addr, addr_type, add, &device_added, &key_found);
1266
1267         btif_read_le_key(BTIF_DM_LE_KEY_LID, sizeof(tBTM_LE_PID_KEYS),
1268                          bd_addr, addr_type, add, &device_added, &key_found);
1269
1270         btif_read_le_key(BTIF_DM_LE_KEY_PCSRK, sizeof(tBTM_LE_PCSRK_KEYS),
1271                          bd_addr, addr_type, add, &device_added, &key_found);
1272
1273         btif_read_le_key(BTIF_DM_LE_KEY_LENC, sizeof(tBTM_LE_LENC_KEYS),
1274                          bd_addr, addr_type, add, &device_added, &key_found);
1275
1276         btif_read_le_key(BTIF_DM_LE_KEY_LCSRK, sizeof(tBTM_LE_LCSRK_KEYS),
1277                          bd_addr, addr_type, add, &device_added, &key_found);
1278
1279         // Fill in the bonded devices
1280         if (device_added)
1281         {
1282             memcpy(&p_bonded_devices->devices[p_bonded_devices->num_devices++], &bd_addr, sizeof(bt_bdaddr_t));
1283             btif_gatts_add_bonded_dev_from_nv(bta_bd_addr);
1284         }
1285
1286         if (key_found)
1287             return BT_STATUS_SUCCESS;
1288     }
1289     return BT_STATUS_FAIL;
1290 }
1291
1292 bt_status_t btif_storage_set_remote_addr_type(bt_bdaddr_t *remote_bd_addr,
1293                                               UINT8 addr_type)
1294 {
1295     bdstr_t bdstr;
1296     bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
1297     int ret = btif_config_set_int(bdstr, "AddrType", (int)addr_type);
1298     return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1299 }
1300
1301 /*******************************************************************************
1302 **
1303 ** Function         btif_storage_get_remote_addr_type
1304 **
1305 ** Description      BTIF storage API - Fetches the remote addr type
1306 **
1307 ** Returns          BT_STATUS_SUCCESS if the fetch was successful,
1308 **                  BT_STATUS_FAIL otherwise
1309 **
1310 *******************************************************************************/
1311 bt_status_t btif_storage_get_remote_addr_type(bt_bdaddr_t *remote_bd_addr,
1312                                               int*addr_type)
1313 {
1314     bdstr_t bdstr;
1315     bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
1316     int ret = btif_config_get_int(bdstr, "AddrType", addr_type);
1317     return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1318 }
1319 #endif
1320 /*******************************************************************************
1321 **
1322 ** Function         btif_storage_add_hid_device_info
1323 **
1324 ** Description      BTIF storage API - Adds the hid information of bonded hid devices-to NVRAM
1325 **
1326 ** Returns          BT_STATUS_SUCCESS if the store was successful,
1327 **                  BT_STATUS_FAIL otherwise
1328 **
1329 *******************************************************************************/
1330
1331 bt_status_t btif_storage_add_hid_device_info(bt_bdaddr_t *remote_bd_addr,
1332                                                     UINT16 attr_mask, UINT8 sub_class,
1333                                                     UINT8 app_id, UINT16 vendor_id,
1334                                                     UINT16 product_id, UINT16 version,
1335                                                     UINT8 ctry_code, UINT16 ssr_max_latency,
1336                                                     UINT16 ssr_min_tout, UINT16 dl_len, UINT8 *dsc_list)
1337 {
1338     bdstr_t bdstr;
1339     BTIF_TRACE_DEBUG("btif_storage_add_hid_device_info:");
1340     bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
1341     btif_config_set_int(bdstr, "HidAttrMask", attr_mask);
1342     btif_config_set_int(bdstr, "HidSubClass", sub_class);
1343     btif_config_set_int(bdstr, "HidAppId", app_id);
1344     btif_config_set_int(bdstr, "HidVendorId", vendor_id);
1345     btif_config_set_int(bdstr, "HidProductId", product_id);
1346     btif_config_set_int(bdstr, "HidVersion", version);
1347     btif_config_set_int(bdstr, "HidCountryCode", ctry_code);
1348     btif_config_set_int(bdstr, "HidSSRMaxLatency", ssr_max_latency);
1349     btif_config_set_int(bdstr, "HidSSRMinTimeout", ssr_min_tout);
1350     if(dl_len > 0)
1351         btif_config_set_bin(bdstr, "HidDescriptor", dsc_list, dl_len);
1352     btif_config_save();
1353     return BT_STATUS_SUCCESS;
1354 }
1355
1356 /*******************************************************************************
1357 **
1358 ** Function         btif_storage_load_bonded_hid_info
1359 **
1360 ** Description      BTIF storage API - Loads hid info for all the bonded devices from NVRAM
1361 **                  and adds those devices  to the BTA_HH.
1362 **
1363 ** Returns          BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise
1364 **
1365 *******************************************************************************/
1366 bt_status_t btif_storage_load_bonded_hid_info(void)
1367 {
1368     bt_bdaddr_t bd_addr;
1369     tBTA_HH_DEV_DSCP_INFO dscp_info;
1370     uint16_t attr_mask;
1371     uint8_t  sub_class;
1372     uint8_t  app_id;
1373
1374     memset(&dscp_info, 0, sizeof(dscp_info));
1375     for (const btif_config_section_iter_t *iter = btif_config_section_begin(); iter != btif_config_section_end(); iter = btif_config_section_next(iter)) {
1376         const char *name = btif_config_section_name(iter);
1377         if (!string_is_bdaddr(name))
1378             continue;
1379
1380         BTIF_TRACE_DEBUG("Remote device:%s", name);
1381         int value;
1382         if(btif_in_fetch_bonded_device(name) == BT_STATUS_SUCCESS)
1383         {
1384             if(btif_config_get_int(name, "HidAttrMask", &value))
1385             {
1386                 attr_mask = (uint16_t)value;
1387
1388                 btif_config_get_int(name, "HidSubClass", &value);
1389                 sub_class = (uint8_t)value;
1390
1391                 btif_config_get_int(name, "HidAppId", &value);
1392                 app_id = (uint8_t)value;
1393
1394                 btif_config_get_int(name, "HidVendorId", &value);
1395                 dscp_info.vendor_id = (uint16_t) value;
1396
1397                 btif_config_get_int(name, "HidProductId", &value);
1398                 dscp_info.product_id = (uint16_t) value;
1399
1400                 btif_config_get_int(name, "HidVersion", &value);
1401                 dscp_info.version = (uint8_t) value;
1402
1403                 btif_config_get_int(name, "HidCountryCode", &value);
1404                 dscp_info.ctry_code = (uint8_t) value;
1405
1406                 value = 0;
1407                 btif_config_get_int(name, "HidSSRMaxLatency", &value);
1408                 dscp_info.ssr_max_latency = (uint16_t) value;
1409
1410                 value = 0;
1411                 btif_config_get_int(name, "HidSSRMinTimeout", &value);
1412                 dscp_info.ssr_min_tout = (uint16_t) value;
1413
1414                 size_t len = btif_config_get_bin_length(name, "HidDescriptor");
1415                 if(len > 0)
1416                 {
1417                     dscp_info.descriptor.dl_len = (uint16_t)len;
1418                     dscp_info.descriptor.dsc_list = (uint8_t*)alloca(len);
1419                     btif_config_get_bin(name, "HidDescriptor", (uint8_t *)dscp_info.descriptor.dsc_list, &len);
1420                 }
1421                 string_to_bdaddr(name, &bd_addr);
1422                 // add extracted information to BTA HH
1423                 if (btif_hh_add_added_dev(bd_addr,attr_mask))
1424                 {
1425                     BTA_HhAddDev(bd_addr.address, attr_mask, sub_class,
1426                             app_id, dscp_info);
1427                 }
1428             }
1429         }
1430         else
1431         {
1432             if(btif_config_get_int(name, "HidAttrMask", &value))
1433             {
1434                 btif_storage_remove_hid_info(&bd_addr);
1435                 string_to_bdaddr(name, &bd_addr);
1436             }
1437         }
1438     }
1439
1440     return BT_STATUS_SUCCESS;
1441 }
1442
1443 /*******************************************************************************
1444 **
1445 ** Function         btif_storage_remove_hid_info
1446 **
1447 ** Description      BTIF storage API - Deletes the bonded hid device info from NVRAM
1448 **
1449 ** Returns          BT_STATUS_SUCCESS if the deletion was successful,
1450 **                  BT_STATUS_FAIL otherwise
1451 **
1452 *******************************************************************************/
1453 bt_status_t btif_storage_remove_hid_info(bt_bdaddr_t *remote_bd_addr)
1454 {
1455     bdstr_t bdstr;
1456     bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
1457
1458     btif_config_remove(bdstr, "HidAttrMask");
1459     btif_config_remove(bdstr, "HidSubClass");
1460     btif_config_remove(bdstr, "HidAppId");
1461     btif_config_remove(bdstr, "HidVendorId");
1462     btif_config_remove(bdstr, "HidProductId");
1463     btif_config_remove(bdstr, "HidVersion");
1464     btif_config_remove(bdstr, "HidCountryCode");
1465     btif_config_remove(bdstr, "HidSSRMaxLatency");
1466     btif_config_remove(bdstr, "HidSSRMinTimeout");
1467     btif_config_remove(bdstr, "HidDescriptor");
1468     btif_config_save();
1469     return BT_STATUS_SUCCESS;
1470 }
1471
1472 /*******************************************************************************
1473 **
1474 ** Function         btif_storage_get_num_bonded_devices
1475 **
1476 ** Description      BTIF storage API - Gets the number of bonded devices
1477 **
1478 ** Returns          the number of bonded devices
1479 **
1480 *******************************************************************************/
1481 int btif_storage_get_num_bonded_devices(void) {
1482     btif_bonded_devices_t bonded_devices;
1483     btif_in_fetch_bonded_devices(&bonded_devices, 0);
1484     return bonded_devices.num_devices;
1485 }
1486
1487 /*******************************************************************************
1488 **
1489 ** Function         btif_storage_read_hl_apps_cb
1490 **
1491 ** Description      BTIF storage API - Read HL application control block from NVRAM
1492 **
1493 ** Returns          BT_STATUS_SUCCESS if the operation was successful,
1494 **                  BT_STATUS_FAIL otherwise
1495 **
1496 *******************************************************************************/
1497 bt_status_t btif_storage_read_hl_apps_cb(char *value, int value_size)
1498 {
1499     bt_status_t bt_status = BT_STATUS_SUCCESS;
1500
1501     if (!btif_config_exist(BTIF_STORAGE_HL_APP, BTIF_STORAGE_HL_APP_CB)) {
1502         memset(value, 0, value_size);
1503         if (!btif_config_set_bin(BTIF_STORAGE_HL_APP,BTIF_STORAGE_HL_APP_CB,
1504                              (const uint8_t *)value, value_size)) {
1505             bt_status = BT_STATUS_FAIL;
1506         } else {
1507             btif_config_save();
1508         }
1509     } else {
1510         size_t read_size = value_size;
1511         if (!btif_config_get_bin(BTIF_STORAGE_HL_APP, BTIF_STORAGE_HL_APP_CB,
1512                              (uint8_t *)value, &read_size)) {
1513             bt_status = BT_STATUS_FAIL;
1514         } else {
1515             if (read_size != (size_t)value_size) {
1516                 BTIF_TRACE_ERROR("%s  value_size=%d read_size=%d",
1517                                   __FUNCTION__, value_size, read_size);
1518                 bt_status = BT_STATUS_FAIL;
1519             }
1520         }
1521     }
1522
1523     BTIF_TRACE_DEBUG("%s  status=%d value_size=%d", __FUNCTION__, bt_status, value_size);
1524     return bt_status;
1525 }
1526
1527 /*******************************************************************************
1528 **
1529 ** Function         btif_storage_is_restricted_device
1530 **
1531 ** Description      BTIF storage API - checks if this device is a restricted device
1532 **
1533 ** Returns          TRUE  if the device is labeled as restricted
1534 **                  FALSE otherwise
1535 **
1536 *******************************************************************************/
1537 BOOLEAN btif_storage_is_restricted_device(const bt_bdaddr_t *remote_bd_addr)
1538 {
1539     bdstr_t bdstr;
1540     bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
1541
1542     return btif_config_exist(bdstr, "Restricted");
1543 }