1 /******************************************************************************
3 * Copyright (C) 2014 Google, Inc.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 ******************************************************************************/
19 #define LOG_TAG "bt_btif_config"
21 #include "btif_config.h"
32 #include "btcore/include/bdaddr.h"
33 #include "btcore/include/module.h"
35 #include "btif_common.h"
36 #include "btif_config.h"
37 #include "btif_config_transcode.h"
38 #include "btif_util.h"
39 #include "osi/include/alarm.h"
40 #include "osi/include/allocator.h"
41 #include "osi/include/compat.h"
42 #include "osi/include/config.h"
43 #include "osi/include/log.h"
44 #include "osi/include/osi.h"
47 * TODO(apanicke): cutils/properties.h is only being used to pull-in runtime
48 * settings on Android. Remove this conditional include once we have a generic
49 * way to obtain system properties.
51 #if !defined(OS_GENERIC)
52 #include <cutils/properties.h>
53 #endif /* !defined(OS_GENERIC) */
55 #define INFO_SECTION "Info"
56 #define FILE_TIMESTAMP "TimeCreated"
57 #define TIME_STRING_LENGTH sizeof("YYYY-MM-DD HH:MM:SS")
58 static const char* TIME_STRING_FORMAT = "%Y-%m-%d %H:%M:%S";
60 // TODO(armansito): Find a better way than searching by a hardcoded path.
61 #if defined(OS_GENERIC)
62 static const char *CONFIG_FILE_PATH = "bt_config.conf";
63 static const char *CONFIG_BACKUP_PATH = "bt_config.bak";
64 static const char *CONFIG_LEGACY_FILE_PATH = "bt_config.xml";
65 #else // !defined(OS_GENERIC)
66 static const char *CONFIG_FILE_PATH = "/data/misc/bluedroid/bt_config.conf";
67 static const char *CONFIG_BACKUP_PATH = "/data/misc/bluedroid/bt_config.bak";
68 static const char *CONFIG_LEGACY_FILE_PATH = "/data/misc/bluedroid/bt_config.xml";
69 #endif // defined(OS_GENERIC)
70 static const period_ms_t CONFIG_SETTLE_PERIOD_MS = 3000;
72 static void timer_config_save_cb(void *data);
73 static void btif_config_write(UINT16 event, char *p_param);
74 static bool is_factory_reset(void);
75 static void delete_config_files(void);
76 static void btif_config_remove_unpaired(config_t *config);
77 static void btif_config_remove_restricted(config_t *config);
79 static enum ConfigSource {
86 } btif_config_source = NOT_LOADED;
88 static int btif_config_devices_loaded = -1;
89 static char btif_config_time_created[TIME_STRING_LENGTH];
91 // TODO(zachoverflow): Move these two functions out, because they are too specific for this file
92 // {grumpy-cat/no, monty-python/you-make-me-sad}
93 bool btif_get_device_type(const BD_ADDR bd_addr, int *p_device_type)
95 if (p_device_type == NULL)
99 bdcpy(bda.address, bd_addr);
102 bdaddr_to_string(&bda, bd_addr_str, sizeof(bd_addr_str));
104 if (!btif_config_get_int(bd_addr_str, "DevType", p_device_type))
107 LOG_DEBUG(LOG_TAG, "%s: Device [%s] type %d", __FUNCTION__, bd_addr_str, *p_device_type);
111 bool btif_get_address_type(const BD_ADDR bd_addr, int *p_addr_type)
113 if (p_addr_type == NULL)
117 bdcpy(bda.address, bd_addr);
120 bdaddr_to_string(&bda, bd_addr_str, sizeof(bd_addr_str));
122 if (!btif_config_get_int(bd_addr_str, "AddrType", p_addr_type))
125 LOG_DEBUG(LOG_TAG, "%s: Device [%s] address type %d", __FUNCTION__, bd_addr_str, *p_addr_type);
129 static pthread_mutex_t lock; // protects operations on |config|.
130 static config_t *config;
131 static alarm_t *config_timer;
133 // Module lifecycle functions
135 static future_t *init(void) {
136 pthread_mutex_init(&lock, NULL);
137 pthread_mutex_lock(&lock);
139 if (is_factory_reset())
140 delete_config_files();
142 config = config_new(CONFIG_FILE_PATH);
143 btif_config_source = ORIGINAL;
145 LOG_WARN("%s unable to load config file: %s; using backup.",
146 __func__, CONFIG_FILE_PATH);
147 config = config_new(CONFIG_BACKUP_PATH);
148 btif_config_source = BACKUP;
151 LOG_WARN("%s unable to load backup; attempting to transcode legacy file.", __func__);
152 config = btif_config_transcode(CONFIG_LEGACY_FILE_PATH);
153 btif_config_source = LEGACY;
156 LOG_ERROR("%s unable to transcode legacy file; creating empty config.", __func__);
157 config = config_new_empty();
158 btif_config_source = NEW_FILE;
161 LOG_ERROR("%s unable to allocate a config object.", __func__);
165 btif_config_remove_unpaired(config);
167 // Cleanup temporary pairings if we have left guest mode
168 if (!is_restricted_mode())
169 btif_config_remove_restricted(config);
171 // Read or set config file creation timestamp
172 const char* time_str = config_get_string(config, INFO_SECTION, FILE_TIMESTAMP, NULL);
173 if (time_str != NULL) {
174 strlcpy(btif_config_time_created, time_str, TIME_STRING_LENGTH);
176 time_t current_time = time(NULL);
177 struct tm* time_created = localtime(¤t_time);
178 strftime(btif_config_time_created, TIME_STRING_LENGTH, TIME_STRING_FORMAT, time_created);
179 config_set_string(config, INFO_SECTION, FILE_TIMESTAMP, btif_config_time_created);
182 // TODO(sharvil): use a non-wake alarm for this once we have
183 // API support for it. There's no need to wake the system to
184 // write back to disk.
185 config_timer = alarm_new("btif.config");
187 LOG_ERROR(LOG_TAG, "%s unable to create alarm.", __func__);
191 pthread_mutex_unlock(&lock);
192 return future_new_immediate(FUTURE_SUCCESS);
195 alarm_free(config_timer);
197 pthread_mutex_unlock(&lock);
198 pthread_mutex_destroy(&lock);
201 btif_config_source = NOT_LOADED;
202 return future_new_immediate(FUTURE_FAIL);
205 static future_t *shut_down(void) {
207 return future_new_immediate(FUTURE_SUCCESS);
210 static future_t *clean_up(void) {
213 alarm_free(config_timer);
215 pthread_mutex_destroy(&lock);
218 return future_new_immediate(FUTURE_SUCCESS);
221 EXPORT_SYMBOL const module_t btif_config_module = {
222 .name = BTIF_CONFIG_MODULE,
225 .shut_down = shut_down,
226 .clean_up = clean_up,
232 bool btif_config_has_section(const char *section) {
233 assert(config != NULL);
234 assert(section != NULL);
236 pthread_mutex_lock(&lock);
237 bool ret = config_has_section(config, section);
238 pthread_mutex_unlock(&lock);
243 bool btif_config_exist(const char *section, const char *key) {
244 assert(config != NULL);
245 assert(section != NULL);
248 pthread_mutex_lock(&lock);
249 bool ret = config_has_key(config, section, key);
250 pthread_mutex_unlock(&lock);
255 bool btif_config_get_int(const char *section, const char *key, int *value) {
256 assert(config != NULL);
257 assert(section != NULL);
259 assert(value != NULL);
261 pthread_mutex_lock(&lock);
262 bool ret = config_has_key(config, section, key);
264 *value = config_get_int(config, section, key, *value);
265 pthread_mutex_unlock(&lock);
270 bool btif_config_set_int(const char *section, const char *key, int value) {
271 assert(config != NULL);
272 assert(section != NULL);
275 pthread_mutex_lock(&lock);
276 config_set_int(config, section, key, value);
277 pthread_mutex_unlock(&lock);
282 bool btif_config_get_str(const char *section, const char *key, char *value, int *size_bytes) {
283 assert(config != NULL);
284 assert(section != NULL);
286 assert(value != NULL);
287 assert(size_bytes != NULL);
289 pthread_mutex_lock(&lock);
290 const char *stored_value = config_get_string(config, section, key, NULL);
291 pthread_mutex_unlock(&lock);
296 strlcpy(value, stored_value, *size_bytes);
297 *size_bytes = strlen(value) + 1;
302 bool btif_config_set_str(const char *section, const char *key, const char *value) {
303 assert(config != NULL);
304 assert(section != NULL);
306 assert(value != NULL);
308 pthread_mutex_lock(&lock);
309 config_set_string(config, section, key, value);
310 pthread_mutex_unlock(&lock);
315 bool btif_config_get_bin(const char *section, const char *key, uint8_t *value, size_t *length) {
316 assert(config != NULL);
317 assert(section != NULL);
319 assert(value != NULL);
320 assert(length != NULL);
322 pthread_mutex_lock(&lock);
323 const char *value_str = config_get_string(config, section, key, NULL);
324 pthread_mutex_unlock(&lock);
329 size_t value_len = strlen(value_str);
330 if ((value_len % 2) != 0 || *length < (value_len / 2))
333 for (size_t i = 0; i < value_len; ++i)
334 if (!isxdigit(value_str[i]))
337 for (*length = 0; *value_str; value_str += 2, *length += 1)
338 sscanf(value_str, "%02hhx", &value[*length]);
343 size_t btif_config_get_bin_length(const char *section, const char *key) {
344 assert(config != NULL);
345 assert(section != NULL);
348 pthread_mutex_lock(&lock);
349 const char *value_str = config_get_string(config, section, key, NULL);
350 pthread_mutex_unlock(&lock);
355 size_t value_len = strlen(value_str);
356 return ((value_len % 2) != 0) ? 0 : (value_len / 2);
359 bool btif_config_set_bin(const char *section, const char *key, const uint8_t *value, size_t length) {
360 const char *lookup = "0123456789abcdef";
362 assert(config != NULL);
363 assert(section != NULL);
367 assert(value != NULL);
369 char *str = (char *)osi_calloc(length * 2 + 1);
371 for (size_t i = 0; i < length; ++i) {
372 str[(i * 2) + 0] = lookup[(value[i] >> 4) & 0x0F];
373 str[(i * 2) + 1] = lookup[value[i] & 0x0F];
376 pthread_mutex_lock(&lock);
377 config_set_string(config, section, key, str);
378 pthread_mutex_unlock(&lock);
384 const btif_config_section_iter_t *btif_config_section_begin(void) {
385 assert(config != NULL);
386 return (const btif_config_section_iter_t *)config_section_begin(config);
389 const btif_config_section_iter_t *btif_config_section_end(void) {
390 assert(config != NULL);
391 return (const btif_config_section_iter_t *)config_section_end(config);
394 const btif_config_section_iter_t *btif_config_section_next(const btif_config_section_iter_t *section) {
395 assert(config != NULL);
396 assert(section != NULL);
397 return (const btif_config_section_iter_t *)config_section_next((const config_section_node_t *)section);
400 const char *btif_config_section_name(const btif_config_section_iter_t *section) {
401 assert(config != NULL);
402 assert(section != NULL);
403 return config_section_name((const config_section_node_t *)section);
406 bool btif_config_remove(const char *section, const char *key) {
407 assert(config != NULL);
408 assert(section != NULL);
411 pthread_mutex_lock(&lock);
412 bool ret = config_remove_key(config, section, key);
413 pthread_mutex_unlock(&lock);
418 void btif_config_save(void) {
419 assert(config != NULL);
420 assert(config_timer != NULL);
422 alarm_set(config_timer, CONFIG_SETTLE_PERIOD_MS, timer_config_save_cb, NULL);
425 void btif_config_flush(void) {
426 assert(config != NULL);
427 assert(config_timer != NULL);
429 alarm_cancel(config_timer);
430 btif_config_write(0, NULL);
433 bool btif_config_clear(void) {
434 assert(config != NULL);
435 assert(config_timer != NULL);
437 alarm_cancel(config_timer);
439 pthread_mutex_lock(&lock);
442 config = config_new_empty();
443 if (config == NULL) {
444 pthread_mutex_unlock(&lock);
448 bool ret = config_save(config, CONFIG_FILE_PATH);
449 btif_config_source = RESET;
450 pthread_mutex_unlock(&lock);
454 static void timer_config_save_cb(UNUSED_ATTR void *data) {
455 // Moving file I/O to btif context instead of timer callback because
456 // it usually takes a lot of time to be completed, introducing
457 // delays during A2DP playback causing blips or choppiness.
458 btif_transfer_context(btif_config_write, 0, NULL, 0, NULL);
461 static void btif_config_write(UNUSED_ATTR UINT16 event, UNUSED_ATTR char *p_param) {
462 assert(config != NULL);
463 assert(config_timer != NULL);
465 pthread_mutex_lock(&lock);
466 rename(CONFIG_FILE_PATH, CONFIG_BACKUP_PATH);
468 config_t *config_paired = config_new_clone(config);
469 btif_config_remove_unpaired(config_paired);
470 config_save(config_paired, CONFIG_FILE_PATH);
471 config_free(config_paired);
472 pthread_mutex_unlock(&lock);
475 static void btif_config_remove_unpaired(config_t *conf) {
476 assert(conf != NULL);
477 int paired_devices = 0;
479 // The paired config used to carry information about
480 // discovered devices during regular inquiry scans.
481 // We remove these now and cache them in memory instead.
482 const config_section_node_t *snode = config_section_begin(conf);
483 while (snode != config_section_end(conf)) {
484 const char *section = config_section_name(snode);
485 if (string_is_bdaddr(section)) {
486 if (!config_has_key(conf, section, "LinkKey") &&
487 !config_has_key(conf, section, "LE_KEY_PENC") &&
488 !config_has_key(conf, section, "LE_KEY_PID") &&
489 !config_has_key(conf, section, "LE_KEY_PCSRK") &&
490 !config_has_key(conf, section, "LE_KEY_LENC") &&
491 !config_has_key(conf, section, "LE_KEY_LCSRK")) {
492 snode = config_section_next(snode);
493 config_remove_section(conf, section);
498 snode = config_section_next(snode);
501 // should only happen once, at initial load time
502 if (btif_config_devices_loaded == -1)
503 btif_config_devices_loaded = paired_devices;
506 void btif_debug_config_dump(int fd) {
507 dprintf(fd, "\nBluetooth Config:\n");
509 dprintf(fd, " Config Source: ");
510 switch(btif_config_source) {
512 dprintf(fd, "Not loaded\n");
515 dprintf(fd, "Original file\n");
518 dprintf(fd, "Backup file\n");
521 dprintf(fd, "Legacy file\n");
524 dprintf(fd, "New file\n");
527 dprintf(fd, "Reset file\n");
531 dprintf(fd, " Devices loaded: %d\n", btif_config_devices_loaded);
532 dprintf(fd, " File created/tagged: %s\n", btif_config_time_created);
535 static void btif_config_remove_restricted(config_t* config) {
536 assert(config != NULL);
538 const config_section_node_t *snode = config_section_begin(config);
539 while (snode != config_section_end(config)) {
540 const char *section = config_section_name(snode);
541 if (string_is_bdaddr(section) && config_has_key(config, section, "Restricted")) {
542 BTIF_TRACE_DEBUG("%s: Removing restricted device %s", __func__, section);
543 config_remove_section(config, section);
545 snode = config_section_next(snode);
549 static bool is_factory_reset(void) {
550 char factory_reset[PROPERTY_VALUE_MAX] = {0};
551 property_get("persist.bluetooth.factoryreset", factory_reset, "false");
552 return strncmp(factory_reset, "true", 4) == 0;
555 static void delete_config_files(void) {
556 remove(CONFIG_FILE_PATH);
557 remove(CONFIG_BACKUP_PATH);
558 property_set("persist.bluetooth.factoryreset", "false");