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"
31 #include "btcore/include/bdaddr.h"
32 #include "btcore/include/module.h"
33 #include "btif_common.h"
34 #include "btif_config.h"
35 #include "btif_util.h"
36 #include "osi/include/alarm.h"
37 #include "osi/include/allocator.h"
38 #include "osi/include/compat.h"
39 #include "osi/include/config.h"
40 #include "osi/include/log.h"
41 #include "osi/include/osi.h"
43 // TODO(armansito): Find a better way than searching by a hardcoded path.
44 #if defined(OS_GENERIC)
45 static const char *CONFIG_FILE_PATH = "bt_config.conf";
46 static const char *CONFIG_BACKUP_PATH = "bt_config.bak";
47 #else // !defined(OS_GENERIC)
48 static const char *CONFIG_FILE_PATH = "/data/misc/bluedroid/bt_config.conf";
49 static const char *CONFIG_BACKUP_PATH = "/data/misc/bluedroid/bt_config.bak";
50 #endif // defined(OS_GENERIC)
51 static const period_ms_t CONFIG_SETTLE_PERIOD_MS = 3000;
53 static void timer_config_save_cb(void *data);
54 static void btif_config_write(UINT16 event, char *p_param);
55 static void btif_config_devcache_cleanup(void);
57 static enum ConfigSource {
63 } btif_config_source = NOT_LOADED;
66 // TODO(zachoverflow): Move these two functions out, because they are too specific for this file
67 // {grumpy-cat/no, monty-python/you-make-me-sad}
68 bool btif_get_device_type(const BD_ADDR bd_addr, int *p_device_type)
70 if (p_device_type == NULL)
74 bdcpy(bda.address, bd_addr);
77 bdaddr_to_string(&bda, bd_addr_str, sizeof(bd_addr_str));
79 if (!btif_config_get_int(bd_addr_str, "DevType", p_device_type))
82 LOG_DEBUG(LOG_TAG, "%s: Device [%s] type %d", __FUNCTION__, bd_addr_str, *p_device_type);
86 bool btif_get_address_type(const BD_ADDR bd_addr, int *p_addr_type)
88 if (p_addr_type == NULL)
92 bdcpy(bda.address, bd_addr);
95 bdaddr_to_string(&bda, bd_addr_str, sizeof(bd_addr_str));
97 if (!btif_config_get_int(bd_addr_str, "AddrType", p_addr_type))
100 LOG_DEBUG(LOG_TAG, "%s: Device [%s] address type %d", __FUNCTION__, bd_addr_str, *p_addr_type);
104 static pthread_mutex_t lock; // protects operations on |config|.
105 static config_t *config;
106 static alarm_t *config_timer;
108 // Module lifecycle functions
110 static future_t *init(void) {
111 pthread_mutex_init(&lock, NULL);
112 config = config_new(CONFIG_FILE_PATH);
113 btif_config_source = ORIGINAL;
115 LOG_WARN(LOG_TAG, "%s unable to load config file: %s; using backup.",
116 __func__, CONFIG_FILE_PATH);
117 config = config_new(CONFIG_BACKUP_PATH);
118 btif_config_source = BACKUP;
120 LOG_ERROR(LOG_TAG, "%s unable to load backup; creating empty config.", __func__);
121 config = config_new_empty();
122 btif_config_source = NEW_FILE;
124 LOG_ERROR(LOG_TAG, "%s unable to allocate a config object.", __func__);
130 btif_config_devcache_cleanup();
132 // TODO(sharvil): use a non-wake alarm for this once we have
133 // API support for it. There's no need to wake the system to
134 // write back to disk.
135 config_timer = alarm_new("btif.config");
137 LOG_ERROR(LOG_TAG, "%s unable to create alarm.", __func__);
141 return future_new_immediate(FUTURE_SUCCESS);
144 alarm_free(config_timer);
146 pthread_mutex_destroy(&lock);
149 return future_new_immediate(FUTURE_FAIL);
152 static future_t *shut_down(void) {
154 return future_new_immediate(FUTURE_SUCCESS);
157 static future_t *clean_up(void) {
160 alarm_free(config_timer);
162 pthread_mutex_destroy(&lock);
165 return future_new_immediate(FUTURE_SUCCESS);
168 EXPORT_SYMBOL const module_t btif_config_module = {
169 .name = BTIF_CONFIG_MODULE,
172 .shut_down = shut_down,
173 .clean_up = clean_up,
179 bool btif_config_has_section(const char *section) {
180 assert(config != NULL);
181 assert(section != NULL);
183 pthread_mutex_lock(&lock);
184 bool ret = config_has_section(config, section);
185 pthread_mutex_unlock(&lock);
190 bool btif_config_exist(const char *section, const char *key) {
191 assert(config != NULL);
192 assert(section != NULL);
195 pthread_mutex_lock(&lock);
196 bool ret = config_has_key(config, section, key);
197 pthread_mutex_unlock(&lock);
202 bool btif_config_get_int(const char *section, const char *key, int *value) {
203 assert(config != NULL);
204 assert(section != NULL);
206 assert(value != NULL);
208 pthread_mutex_lock(&lock);
209 bool ret = config_has_key(config, section, key);
211 *value = config_get_int(config, section, key, *value);
212 pthread_mutex_unlock(&lock);
217 bool btif_config_set_int(const char *section, const char *key, int value) {
218 assert(config != NULL);
219 assert(section != NULL);
222 pthread_mutex_lock(&lock);
223 config_set_int(config, section, key, value);
224 pthread_mutex_unlock(&lock);
229 bool btif_config_get_str(const char *section, const char *key, char *value, int *size_bytes) {
230 assert(config != NULL);
231 assert(section != NULL);
233 assert(value != NULL);
234 assert(size_bytes != NULL);
236 pthread_mutex_lock(&lock);
237 const char *stored_value = config_get_string(config, section, key, NULL);
238 pthread_mutex_unlock(&lock);
243 strlcpy(value, stored_value, *size_bytes);
244 *size_bytes = strlen(value) + 1;
249 bool btif_config_set_str(const char *section, const char *key, const char *value) {
250 assert(config != NULL);
251 assert(section != NULL);
253 assert(value != NULL);
255 pthread_mutex_lock(&lock);
256 config_set_string(config, section, key, value);
257 pthread_mutex_unlock(&lock);
262 bool btif_config_get_bin(const char *section, const char *key, uint8_t *value, size_t *length) {
263 assert(config != NULL);
264 assert(section != NULL);
266 assert(value != NULL);
267 assert(length != NULL);
269 pthread_mutex_lock(&lock);
270 const char *value_str = config_get_string(config, section, key, NULL);
271 pthread_mutex_unlock(&lock);
276 size_t value_len = strlen(value_str);
277 if ((value_len % 2) != 0 || *length < (value_len / 2))
280 for (size_t i = 0; i < value_len; ++i)
281 if (!isxdigit(value_str[i]))
284 for (*length = 0; *value_str; value_str += 2, *length += 1)
285 sscanf(value_str, "%02hhx", &value[*length]);
290 size_t btif_config_get_bin_length(const char *section, const char *key) {
291 assert(config != NULL);
292 assert(section != NULL);
295 pthread_mutex_lock(&lock);
296 const char *value_str = config_get_string(config, section, key, NULL);
297 pthread_mutex_unlock(&lock);
302 size_t value_len = strlen(value_str);
303 return ((value_len % 2) != 0) ? 0 : (value_len / 2);
306 bool btif_config_set_bin(const char *section, const char *key, const uint8_t *value, size_t length) {
307 const char *lookup = "0123456789abcdef";
309 assert(config != NULL);
310 assert(section != NULL);
314 assert(value != NULL);
316 char *str = (char *)osi_calloc(length * 2 + 1);
318 for (size_t i = 0; i < length; ++i) {
319 str[(i * 2) + 0] = lookup[(value[i] >> 4) & 0x0F];
320 str[(i * 2) + 1] = lookup[value[i] & 0x0F];
323 pthread_mutex_lock(&lock);
324 config_set_string(config, section, key, str);
325 pthread_mutex_unlock(&lock);
331 const btif_config_section_iter_t *btif_config_section_begin(void) {
332 assert(config != NULL);
333 return (const btif_config_section_iter_t *)config_section_begin(config);
336 const btif_config_section_iter_t *btif_config_section_end(void) {
337 assert(config != NULL);
338 return (const btif_config_section_iter_t *)config_section_end(config);
341 const btif_config_section_iter_t *btif_config_section_next(const btif_config_section_iter_t *section) {
342 assert(config != NULL);
343 assert(section != NULL);
344 return (const btif_config_section_iter_t *)config_section_next((const config_section_node_t *)section);
347 const char *btif_config_section_name(const btif_config_section_iter_t *section) {
348 assert(config != NULL);
349 assert(section != NULL);
350 return config_section_name((const config_section_node_t *)section);
353 bool btif_config_remove(const char *section, const char *key) {
354 assert(config != NULL);
355 assert(section != NULL);
358 pthread_mutex_lock(&lock);
359 bool ret = config_remove_key(config, section, key);
360 pthread_mutex_unlock(&lock);
365 void btif_config_save(void) {
366 assert(config_timer != NULL);
367 assert(config != NULL);
369 alarm_set(config_timer, CONFIG_SETTLE_PERIOD_MS, timer_config_save_cb, NULL);
372 void btif_config_flush(void) {
373 assert(config != NULL);
374 assert(config_timer != NULL);
376 alarm_cancel(config_timer);
377 btif_config_write(0, NULL);
379 pthread_mutex_lock(&lock);
380 config_save(config, CONFIG_FILE_PATH);
381 pthread_mutex_unlock(&lock);
384 bool btif_config_clear(void){
385 assert(config != NULL);
386 assert(config_timer != NULL);
388 alarm_cancel(config_timer);
390 pthread_mutex_lock(&lock);
393 config = config_new_empty();
394 if (config == NULL) {
395 pthread_mutex_unlock(&lock);
399 bool ret = config_save(config, CONFIG_FILE_PATH);
400 btif_config_source = RESET;
401 pthread_mutex_unlock(&lock);
405 static void timer_config_save_cb(UNUSED_ATTR void *data) {
406 // Moving file I/O to btif context instead of timer callback because
407 // it usually takes a lot of time to be completed, introducing
408 // delays during A2DP playback causing blips or choppiness.
409 btif_transfer_context(btif_config_write, 0, NULL, 0, NULL);
412 static void btif_config_write(UNUSED_ATTR UINT16 event, UNUSED_ATTR char *p_param) {
413 assert(config != NULL);
414 assert(config_timer != NULL);
416 btif_config_devcache_cleanup();
418 pthread_mutex_lock(&lock);
419 rename(CONFIG_FILE_PATH, CONFIG_BACKUP_PATH);
420 config_save(config, CONFIG_FILE_PATH);
421 pthread_mutex_unlock(&lock);
424 static void btif_config_devcache_cleanup(void) {
425 assert(config != NULL);
427 // The config accumulates cached information about remote
428 // devices during regular inquiry scans. We remove some of these
429 // so the cache doesn't grow indefinitely.
430 // We don't remove information about bonded devices (which have link keys).
431 static const size_t ADDRS_MAX = 512;
432 size_t total_addrs = 0;
434 pthread_mutex_lock(&lock);
435 const config_section_node_t *snode = config_section_begin(config);
436 while (snode != config_section_end(config)) {
437 const char *section = config_section_name(snode);
438 if (string_is_bdaddr(section)) {
441 if ((total_addrs > ADDRS_MAX) &&
442 !config_has_key(config, section, "LinkKey") &&
443 !config_has_key(config, section, "LE_KEY_PENC") &&
444 !config_has_key(config, section, "LE_KEY_PID") &&
445 !config_has_key(config, section, "LE_KEY_PCSRK") &&
446 !config_has_key(config, section, "LE_KEY_LENC") &&
447 !config_has_key(config, section, "LE_KEY_LCSRK")) {
448 snode = config_section_next(snode);
449 config_remove_section(config, section);
453 snode = config_section_next(snode);
455 pthread_mutex_unlock(&lock);
458 void btif_debug_config_dump(int fd) {
459 pthread_mutex_lock(&lock);
461 dprintf(fd, "\nBluetooth Config:\n");
463 dprintf(fd, " Config Source: ");
464 switch(btif_config_source) {
466 dprintf(fd, "Not loaded\n");
469 dprintf(fd, "Original file\n");
472 dprintf(fd, "Backup file\n");
475 dprintf(fd, "New file\n");
478 dprintf(fd, "Reset file\n");
482 pthread_mutex_unlock(&lock);