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_config_transcode.h"
34 #include "btif_util.h"
35 #include "osi/include/alarm.h"
36 #include "osi/include/allocator.h"
37 #include "osi/include/compat.h"
38 #include "osi/include/config.h"
39 #include "osi/include/log.h"
40 #include "osi/include/osi.h"
42 // TODO(armansito): Find a better way than searching by a hardcoded path.
43 #if defined(OS_GENERIC)
44 static const char *CONFIG_FILE_PATH = "bt_config.conf";
45 #else // !defined(OS_GENERIC)
46 static const char *CONFIG_FILE_PATH = "/data/misc/bluedroid/bt_config.conf";
47 #endif // defined(OS_GENERIC)
48 static const char *LEGACY_CONFIG_FILE_PATH = "/data/misc/bluedroid/bt_config.xml";
49 static const period_ms_t CONFIG_SETTLE_PERIOD_MS = 3000;
51 static void timer_config_save(void *data);
53 // TODO(zachoverflow): Move these two functions out, because they are too specific for this file
54 // {grumpy-cat/no, monty-python/you-make-me-sad}
55 bool btif_get_device_type(const BD_ADDR bd_addr, int *p_device_type)
57 if (p_device_type == NULL)
61 bdcpy(bda.address, bd_addr);
64 bdaddr_to_string(&bda, bd_addr_str, sizeof(bd_addr_str));
66 if (!btif_config_get_int(bd_addr_str, "DevType", p_device_type))
69 LOG_DEBUG(LOG_TAG, "%s: Device [%s] type %d", __FUNCTION__, bd_addr_str, *p_device_type);
73 bool btif_get_address_type(const BD_ADDR bd_addr, int *p_addr_type)
75 if (p_addr_type == NULL)
79 bdcpy(bda.address, bd_addr);
82 bdaddr_to_string(&bda, bd_addr_str, sizeof(bd_addr_str));
84 if (!btif_config_get_int(bd_addr_str, "AddrType", p_addr_type))
87 LOG_DEBUG(LOG_TAG, "%s: Device [%s] address type %d", __FUNCTION__, bd_addr_str, *p_addr_type);
91 static pthread_mutex_t lock; // protects operations on |config|.
92 static config_t *config;
93 static alarm_t *alarm_timer;
95 // Module lifecycle functions
97 static future_t *init(void) {
98 pthread_mutex_init(&lock, NULL);
99 config = config_new(CONFIG_FILE_PATH);
101 LOG_WARN(LOG_TAG, "%s unable to load config file; attempting to transcode legacy file.", __func__);
102 config = btif_config_transcode(LEGACY_CONFIG_FILE_PATH);
104 LOG_WARN(LOG_TAG, "%s unable to transcode legacy file, starting unconfigured.", __func__);
105 config = config_new_empty();
107 LOG_ERROR(LOG_TAG, "%s unable to allocate a config object.", __func__);
112 if (config_save(config, CONFIG_FILE_PATH))
113 unlink(LEGACY_CONFIG_FILE_PATH);
116 // TODO(sharvil): use a non-wake alarm for this once we have
117 // API support for it. There's no need to wake the system to
118 // write back to disk.
119 alarm_timer = alarm_new();
121 LOG_ERROR(LOG_TAG, "%s unable to create alarm.", __func__);
125 return future_new_immediate(FUTURE_SUCCESS);
128 alarm_free(alarm_timer);
130 pthread_mutex_destroy(&lock);
133 return future_new_immediate(FUTURE_FAIL);
136 static future_t *shut_down(void) {
138 return future_new_immediate(FUTURE_SUCCESS);
141 static future_t *clean_up(void) {
144 alarm_free(alarm_timer);
146 pthread_mutex_destroy(&lock);
149 return future_new_immediate(FUTURE_SUCCESS);
152 EXPORT_SYMBOL const module_t btif_config_module = {
153 .name = BTIF_CONFIG_MODULE,
156 .shut_down = shut_down,
157 .clean_up = clean_up,
163 bool btif_config_has_section(const char *section) {
164 assert(config != NULL);
165 assert(section != NULL);
167 pthread_mutex_lock(&lock);
168 bool ret = config_has_section(config, section);
169 pthread_mutex_unlock(&lock);
174 bool btif_config_exist(const char *section, const char *key) {
175 assert(config != NULL);
176 assert(section != NULL);
179 pthread_mutex_lock(&lock);
180 bool ret = config_has_key(config, section, key);
181 pthread_mutex_unlock(&lock);
186 bool btif_config_get_int(const char *section, const char *key, int *value) {
187 assert(config != NULL);
188 assert(section != NULL);
190 assert(value != NULL);
192 pthread_mutex_lock(&lock);
193 bool ret = config_has_key(config, section, key);
195 *value = config_get_int(config, section, key, *value);
196 pthread_mutex_unlock(&lock);
201 bool btif_config_set_int(const char *section, const char *key, int value) {
202 assert(config != NULL);
203 assert(section != NULL);
206 pthread_mutex_lock(&lock);
207 config_set_int(config, section, key, value);
208 pthread_mutex_unlock(&lock);
213 bool btif_config_get_str(const char *section, const char *key, char *value, int *size_bytes) {
214 assert(config != NULL);
215 assert(section != NULL);
217 assert(value != NULL);
218 assert(size_bytes != NULL);
220 pthread_mutex_lock(&lock);
221 const char *stored_value = config_get_string(config, section, key, NULL);
222 pthread_mutex_unlock(&lock);
227 strlcpy(value, stored_value, *size_bytes);
228 *size_bytes = strlen(value) + 1;
233 bool btif_config_set_str(const char *section, const char *key, const char *value) {
234 assert(config != NULL);
235 assert(section != NULL);
237 assert(value != NULL);
239 pthread_mutex_lock(&lock);
240 config_set_string(config, section, key, value);
241 pthread_mutex_unlock(&lock);
246 bool btif_config_get_bin(const char *section, const char *key, uint8_t *value, size_t *length) {
247 assert(config != NULL);
248 assert(section != NULL);
250 assert(value != NULL);
251 assert(length != NULL);
253 pthread_mutex_lock(&lock);
254 const char *value_str = config_get_string(config, section, key, NULL);
255 pthread_mutex_unlock(&lock);
260 size_t value_len = strlen(value_str);
261 if ((value_len % 2) != 0 || *length < (value_len / 2))
264 for (size_t i = 0; i < value_len; ++i)
265 if (!isxdigit(value_str[i]))
268 for (*length = 0; *value_str; value_str += 2, *length += 1)
269 sscanf(value_str, "%02hhx", &value[*length]);
274 size_t btif_config_get_bin_length(const char *section, const char *key) {
275 assert(config != NULL);
276 assert(section != NULL);
279 pthread_mutex_lock(&lock);
280 const char *value_str = config_get_string(config, section, key, NULL);
281 pthread_mutex_unlock(&lock);
286 size_t value_len = strlen(value_str);
287 return ((value_len % 2) != 0) ? 0 : (value_len / 2);
290 bool btif_config_set_bin(const char *section, const char *key, const uint8_t *value, size_t length) {
291 const char *lookup = "0123456789abcdef";
293 assert(config != NULL);
294 assert(section != NULL);
298 assert(value != NULL);
300 char *str = (char *)osi_calloc(length * 2 + 1);
304 for (size_t i = 0; i < length; ++i) {
305 str[(i * 2) + 0] = lookup[(value[i] >> 4) & 0x0F];
306 str[(i * 2) + 1] = lookup[value[i] & 0x0F];
309 pthread_mutex_lock(&lock);
310 config_set_string(config, section, key, str);
311 pthread_mutex_unlock(&lock);
317 const btif_config_section_iter_t *btif_config_section_begin(void) {
318 assert(config != NULL);
319 return (const btif_config_section_iter_t *)config_section_begin(config);
322 const btif_config_section_iter_t *btif_config_section_end(void) {
323 assert(config != NULL);
324 return (const btif_config_section_iter_t *)config_section_end(config);
327 const btif_config_section_iter_t *btif_config_section_next(const btif_config_section_iter_t *section) {
328 assert(config != NULL);
329 assert(section != NULL);
330 return (const btif_config_section_iter_t *)config_section_next((const config_section_node_t *)section);
333 const char *btif_config_section_name(const btif_config_section_iter_t *section) {
334 assert(config != NULL);
335 assert(section != NULL);
336 return config_section_name((const config_section_node_t *)section);
339 bool btif_config_remove(const char *section, const char *key) {
340 assert(config != NULL);
341 assert(section != NULL);
344 pthread_mutex_lock(&lock);
345 bool ret = config_remove_key(config, section, key);
346 pthread_mutex_unlock(&lock);
351 void btif_config_save(void) {
352 assert(alarm_timer != NULL);
353 assert(config != NULL);
355 alarm_set(alarm_timer, CONFIG_SETTLE_PERIOD_MS, timer_config_save, NULL);
358 void btif_config_flush(void) {
359 assert(config != NULL);
360 assert(alarm_timer != NULL);
362 alarm_cancel(alarm_timer);
364 pthread_mutex_lock(&lock);
365 config_save(config, CONFIG_FILE_PATH);
366 pthread_mutex_unlock(&lock);
369 int btif_config_clear(void){
370 assert(config != NULL);
371 assert(alarm_timer != NULL);
373 alarm_cancel(alarm_timer);
375 pthread_mutex_lock(&lock);
378 config = config_new_empty();
379 if (config == NULL) {
380 pthread_mutex_unlock(&lock);
384 int ret = config_save(config, CONFIG_FILE_PATH);
385 pthread_mutex_unlock(&lock);
389 static void timer_config_save(UNUSED_ATTR void *data) {
390 assert(config != NULL);
391 assert(alarm_timer != NULL);
393 // Garbage collection process: the config file accumulates
394 // cached information about remote devices during regular
395 // inquiry scans. We remove some of these junk entries
396 // so the file doesn't grow indefinitely. We have to take care
397 // to make sure we don't remove information about bonded
398 // devices (hence the check for link keys).
399 static const size_t CACHE_MAX = 256;
400 const char *keys[CACHE_MAX];
402 size_t total_candidates = 0;
404 pthread_mutex_lock(&lock);
405 for (const config_section_node_t *snode = config_section_begin(config); snode != config_section_end(config); snode = config_section_next(snode)) {
406 const char *section = config_section_name(snode);
407 if (!string_is_bdaddr(section))
410 if (config_has_key(config, section, "LinkKey") ||
411 config_has_key(config, section, "LE_KEY_PENC") ||
412 config_has_key(config, section, "LE_KEY_PID") ||
413 config_has_key(config, section, "LE_KEY_PCSRK") ||
414 config_has_key(config, section, "LE_KEY_LENC") ||
415 config_has_key(config, section, "LE_KEY_LCSRK"))
418 if (num_keys < CACHE_MAX)
419 keys[num_keys++] = section;
424 if (total_candidates > CACHE_MAX * 2)
426 config_remove_section(config, keys[--num_keys]);
428 config_save(config, CONFIG_FILE_PATH);
429 pthread_mutex_unlock(&lock);