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 // TODO(zachoverflow): Move these two functions out, because they are too specific for this file
58 // {grumpy-cat/no, monty-python/you-make-me-sad}
59 bool btif_get_device_type(const BD_ADDR bd_addr, int *p_device_type)
61 if (p_device_type == NULL)
65 bdcpy(bda.address, bd_addr);
68 bdaddr_to_string(&bda, bd_addr_str, sizeof(bd_addr_str));
70 if (!btif_config_get_int(bd_addr_str, "DevType", p_device_type))
73 LOG_DEBUG(LOG_TAG, "%s: Device [%s] type %d", __FUNCTION__, bd_addr_str, *p_device_type);
77 bool btif_get_address_type(const BD_ADDR bd_addr, int *p_addr_type)
79 if (p_addr_type == NULL)
83 bdcpy(bda.address, bd_addr);
86 bdaddr_to_string(&bda, bd_addr_str, sizeof(bd_addr_str));
88 if (!btif_config_get_int(bd_addr_str, "AddrType", p_addr_type))
91 LOG_DEBUG(LOG_TAG, "%s: Device [%s] address type %d", __FUNCTION__, bd_addr_str, *p_addr_type);
95 static pthread_mutex_t lock; // protects operations on |config|.
96 static config_t *config;
97 static alarm_t *config_timer;
99 // Module lifecycle functions
101 static future_t *init(void) {
102 pthread_mutex_init(&lock, NULL);
103 config = config_new(CONFIG_FILE_PATH);
105 LOG_WARN(LOG_TAG, "%s unable to load config file: %s; using backup.",
106 __func__, CONFIG_FILE_PATH);
107 config = config_new(CONFIG_BACKUP_PATH);
109 LOG_ERROR(LOG_TAG, "%s unable to load backup; creating empty config.", __func__);
110 config = config_new_empty();
112 LOG_ERROR(LOG_TAG, "%s unable to allocate a config object.", __func__);
118 btif_config_devcache_cleanup();
120 // TODO(sharvil): use a non-wake alarm for this once we have
121 // API support for it. There's no need to wake the system to
122 // write back to disk.
123 config_timer = alarm_new("btif.config");
125 LOG_ERROR(LOG_TAG, "%s unable to create alarm.", __func__);
129 return future_new_immediate(FUTURE_SUCCESS);
132 alarm_free(config_timer);
134 pthread_mutex_destroy(&lock);
137 return future_new_immediate(FUTURE_FAIL);
140 static future_t *shut_down(void) {
142 return future_new_immediate(FUTURE_SUCCESS);
145 static future_t *clean_up(void) {
148 alarm_free(config_timer);
150 pthread_mutex_destroy(&lock);
153 return future_new_immediate(FUTURE_SUCCESS);
156 EXPORT_SYMBOL const module_t btif_config_module = {
157 .name = BTIF_CONFIG_MODULE,
160 .shut_down = shut_down,
161 .clean_up = clean_up,
167 bool btif_config_has_section(const char *section) {
168 assert(config != NULL);
169 assert(section != NULL);
171 pthread_mutex_lock(&lock);
172 bool ret = config_has_section(config, section);
173 pthread_mutex_unlock(&lock);
178 bool btif_config_exist(const char *section, const char *key) {
179 assert(config != NULL);
180 assert(section != NULL);
183 pthread_mutex_lock(&lock);
184 bool ret = config_has_key(config, section, key);
185 pthread_mutex_unlock(&lock);
190 bool btif_config_get_int(const char *section, const char *key, int *value) {
191 assert(config != NULL);
192 assert(section != NULL);
194 assert(value != NULL);
196 pthread_mutex_lock(&lock);
197 bool ret = config_has_key(config, section, key);
199 *value = config_get_int(config, section, key, *value);
200 pthread_mutex_unlock(&lock);
205 bool btif_config_set_int(const char *section, const char *key, int value) {
206 assert(config != NULL);
207 assert(section != NULL);
210 pthread_mutex_lock(&lock);
211 config_set_int(config, section, key, value);
212 pthread_mutex_unlock(&lock);
217 bool btif_config_get_str(const char *section, const char *key, char *value, int *size_bytes) {
218 assert(config != NULL);
219 assert(section != NULL);
221 assert(value != NULL);
222 assert(size_bytes != NULL);
224 pthread_mutex_lock(&lock);
225 const char *stored_value = config_get_string(config, section, key, NULL);
226 pthread_mutex_unlock(&lock);
231 strlcpy(value, stored_value, *size_bytes);
232 *size_bytes = strlen(value) + 1;
237 bool btif_config_set_str(const char *section, const char *key, const char *value) {
238 assert(config != NULL);
239 assert(section != NULL);
241 assert(value != NULL);
243 pthread_mutex_lock(&lock);
244 config_set_string(config, section, key, value);
245 pthread_mutex_unlock(&lock);
250 bool btif_config_get_bin(const char *section, const char *key, uint8_t *value, size_t *length) {
251 assert(config != NULL);
252 assert(section != NULL);
254 assert(value != NULL);
255 assert(length != NULL);
257 pthread_mutex_lock(&lock);
258 const char *value_str = config_get_string(config, section, key, NULL);
259 pthread_mutex_unlock(&lock);
264 size_t value_len = strlen(value_str);
265 if ((value_len % 2) != 0 || *length < (value_len / 2))
268 for (size_t i = 0; i < value_len; ++i)
269 if (!isxdigit(value_str[i]))
272 for (*length = 0; *value_str; value_str += 2, *length += 1)
273 sscanf(value_str, "%02hhx", &value[*length]);
278 size_t btif_config_get_bin_length(const char *section, const char *key) {
279 assert(config != NULL);
280 assert(section != NULL);
283 pthread_mutex_lock(&lock);
284 const char *value_str = config_get_string(config, section, key, NULL);
285 pthread_mutex_unlock(&lock);
290 size_t value_len = strlen(value_str);
291 return ((value_len % 2) != 0) ? 0 : (value_len / 2);
294 bool btif_config_set_bin(const char *section, const char *key, const uint8_t *value, size_t length) {
295 const char *lookup = "0123456789abcdef";
297 assert(config != NULL);
298 assert(section != NULL);
302 assert(value != NULL);
304 char *str = (char *)osi_calloc(length * 2 + 1);
306 for (size_t i = 0; i < length; ++i) {
307 str[(i * 2) + 0] = lookup[(value[i] >> 4) & 0x0F];
308 str[(i * 2) + 1] = lookup[value[i] & 0x0F];
311 pthread_mutex_lock(&lock);
312 config_set_string(config, section, key, str);
313 pthread_mutex_unlock(&lock);
319 const btif_config_section_iter_t *btif_config_section_begin(void) {
320 assert(config != NULL);
321 return (const btif_config_section_iter_t *)config_section_begin(config);
324 const btif_config_section_iter_t *btif_config_section_end(void) {
325 assert(config != NULL);
326 return (const btif_config_section_iter_t *)config_section_end(config);
329 const btif_config_section_iter_t *btif_config_section_next(const btif_config_section_iter_t *section) {
330 assert(config != NULL);
331 assert(section != NULL);
332 return (const btif_config_section_iter_t *)config_section_next((const config_section_node_t *)section);
335 const char *btif_config_section_name(const btif_config_section_iter_t *section) {
336 assert(config != NULL);
337 assert(section != NULL);
338 return config_section_name((const config_section_node_t *)section);
341 bool btif_config_remove(const char *section, const char *key) {
342 assert(config != NULL);
343 assert(section != NULL);
346 pthread_mutex_lock(&lock);
347 bool ret = config_remove_key(config, section, key);
348 pthread_mutex_unlock(&lock);
353 void btif_config_save(void) {
354 assert(config_timer != NULL);
355 assert(config != NULL);
357 alarm_set(config_timer, CONFIG_SETTLE_PERIOD_MS, timer_config_save_cb, NULL);
360 void btif_config_flush(void) {
361 assert(config != NULL);
362 assert(config_timer != NULL);
364 alarm_cancel(config_timer);
365 btif_config_write(0, NULL);
367 pthread_mutex_lock(&lock);
368 config_save(config, CONFIG_FILE_PATH);
369 pthread_mutex_unlock(&lock);
372 bool btif_config_clear(void){
373 assert(config != NULL);
374 assert(config_timer != NULL);
376 alarm_cancel(config_timer);
378 pthread_mutex_lock(&lock);
381 config = config_new_empty();
382 if (config == NULL) {
383 pthread_mutex_unlock(&lock);
387 bool ret = config_save(config, CONFIG_FILE_PATH);
388 pthread_mutex_unlock(&lock);
392 static void timer_config_save_cb(UNUSED_ATTR void *data) {
393 // Moving file I/O to btif context instead of timer callback because
394 // it usually takes a lot of time to be completed, introducing
395 // delays during A2DP playback causing blips or choppiness.
396 btif_transfer_context(btif_config_write, 0, NULL, 0, NULL);
399 static void btif_config_write(UNUSED_ATTR UINT16 event, UNUSED_ATTR char *p_param) {
400 assert(config != NULL);
401 assert(config_timer != NULL);
403 btif_config_devcache_cleanup();
405 pthread_mutex_lock(&lock);
406 rename(CONFIG_FILE_PATH, CONFIG_BACKUP_PATH);
407 config_save(config, CONFIG_FILE_PATH);
408 pthread_mutex_unlock(&lock);
411 static void btif_config_devcache_cleanup(void) {
412 assert(config != NULL);
414 // The config accumulates cached information about remote
415 // devices during regular inquiry scans. We remove some of these
416 // so the cache doesn't grow indefinitely.
417 // We don't remove information about bonded devices (which have link keys).
418 static const size_t ADDRS_MAX = 512;
419 size_t total_addrs = 0;
421 pthread_mutex_lock(&lock);
422 const config_section_node_t *snode = config_section_begin(config);
423 while (snode != config_section_end(config)) {
424 const char *section = config_section_name(snode);
425 if (string_is_bdaddr(section)) {
428 if ((total_addrs > ADDRS_MAX) &&
429 !config_has_key(config, section, "LinkKey") &&
430 !config_has_key(config, section, "LE_KEY_PENC") &&
431 !config_has_key(config, section, "LE_KEY_PID") &&
432 !config_has_key(config, section, "LE_KEY_PCSRK") &&
433 !config_has_key(config, section, "LE_KEY_LENC") &&
434 !config_has_key(config, section, "LE_KEY_LCSRK")) {
435 snode = config_section_next(snode);
436 config_remove_section(config, section);
440 snode = config_section_next(snode);
442 pthread_mutex_unlock(&lock);