btif_config.cc revision b7f64bc45dec7f7fec74ceb04874f322b9434bbf
1/****************************************************************************** 2 * 3 * Copyright (C) 2014 Google, Inc. 4 * 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: 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 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. 16 * 17 ******************************************************************************/ 18 19#define LOG_TAG "bt_btif_config" 20 21#include "btif_config.h" 22 23#include <assert.h> 24#include <ctype.h> 25#include <pthread.h> 26#include <stdio.h> 27#include <string> 28#include <string.h> 29#include <time.h> 30#include <unistd.h> 31 32#include "bt_types.h" 33#include "btcore/include/bdaddr.h" 34#include "btcore/include/module.h" 35#include "btif_api.h" 36#include "btif_common.h" 37#include "btif_config.h" 38#include "btif_config_transcode.h" 39#include "btif_util.h" 40#include "osi/include/alarm.h" 41#include "osi/include/allocator.h" 42#include "osi/include/compat.h" 43#include "osi/include/config.h" 44#include "osi/include/log.h" 45#include "osi/include/osi.h" 46#include "osi/include/properties.h" 47 48#define INFO_SECTION "Info" 49#define FILE_TIMESTAMP "TimeCreated" 50#define FILE_SOURCE "FileSource" 51#define TIME_STRING_LENGTH sizeof("YYYY-MM-DD HH:MM:SS") 52static const char* TIME_STRING_FORMAT = "%Y-%m-%d %H:%M:%S"; 53 54// TODO(armansito): Find a better way than searching by a hardcoded path. 55#if defined(OS_GENERIC) 56static const char *CONFIG_FILE_PATH = "bt_config.conf"; 57static const char *CONFIG_BACKUP_PATH = "bt_config.bak"; 58static const char *CONFIG_LEGACY_FILE_PATH = "bt_config.xml"; 59#else // !defined(OS_GENERIC) 60static const char *CONFIG_FILE_PATH = "/data/misc/bluedroid/bt_config.conf"; 61static const char *CONFIG_BACKUP_PATH = "/data/misc/bluedroid/bt_config.bak"; 62static const char *CONFIG_LEGACY_FILE_PATH = "/data/misc/bluedroid/bt_config.xml"; 63#endif // defined(OS_GENERIC) 64static const period_ms_t CONFIG_SETTLE_PERIOD_MS = 3000; 65 66static void timer_config_save_cb(void *data); 67static void btif_config_write(uint16_t event, char *p_param); 68static bool is_factory_reset(void); 69static void delete_config_files(void); 70static void btif_config_remove_unpaired(config_t *config); 71static void btif_config_remove_restricted(config_t *config); 72static config_t *btif_config_open(const char* filename); 73 74static enum ConfigSource { 75 NOT_LOADED, 76 ORIGINAL, 77 BACKUP, 78 LEGACY, 79 NEW_FILE, 80 RESET 81} btif_config_source = NOT_LOADED; 82 83static int btif_config_devices_loaded = -1; 84static char btif_config_time_created[TIME_STRING_LENGTH]; 85 86// TODO(zachoverflow): Move these two functions out, because they are too specific for this file 87// {grumpy-cat/no, monty-python/you-make-me-sad} 88bool btif_get_device_type(const BD_ADDR bd_addr, int *p_device_type) 89{ 90 if (p_device_type == NULL) 91 return false; 92 93 bt_bdaddr_t bda; 94 bdcpy(bda.address, bd_addr); 95 96 bdstr_t bd_addr_str; 97 bdaddr_to_string(&bda, bd_addr_str, sizeof(bd_addr_str)); 98 99 if (!btif_config_get_int(bd_addr_str, "DevType", p_device_type)) 100 return false; 101 102 LOG_DEBUG(LOG_TAG, "%s: Device [%s] type %d", __func__, bd_addr_str, *p_device_type); 103 return true; 104} 105 106bool btif_get_address_type(const BD_ADDR bd_addr, int *p_addr_type) 107{ 108 if (p_addr_type == NULL) 109 return false; 110 111 bt_bdaddr_t bda; 112 bdcpy(bda.address, bd_addr); 113 114 bdstr_t bd_addr_str; 115 bdaddr_to_string(&bda, bd_addr_str, sizeof(bd_addr_str)); 116 117 if (!btif_config_get_int(bd_addr_str, "AddrType", p_addr_type)) 118 return false; 119 120 LOG_DEBUG(LOG_TAG, "%s: Device [%s] address type %d", __func__, bd_addr_str, *p_addr_type); 121 return true; 122} 123 124static pthread_mutex_t lock; // protects operations on |config|. 125static config_t *config; 126static alarm_t *config_timer; 127 128// Module lifecycle functions 129 130static future_t *init(void) { 131 pthread_mutex_init(&lock, NULL); 132 pthread_mutex_lock(&lock); 133 134 if (is_factory_reset()) 135 delete_config_files(); 136 137 std::string file_source; 138 139 config = btif_config_open(CONFIG_FILE_PATH); 140 btif_config_source = ORIGINAL; 141 if (!config) { 142 LOG_WARN(LOG_TAG, "%s unable to load config file: %s; using backup.", 143 __func__, CONFIG_FILE_PATH); 144 config = btif_config_open(CONFIG_BACKUP_PATH); 145 btif_config_source = BACKUP; 146 file_source = "Backup"; 147 } 148 if (!config) { 149 LOG_WARN(LOG_TAG, "%s unable to load backup; attempting to transcode legacy file.", __func__); 150 config = btif_config_transcode(CONFIG_LEGACY_FILE_PATH); 151 btif_config_source = LEGACY; 152 file_source = "Legacy"; 153 } 154 if (!config) { 155 LOG_ERROR(LOG_TAG, "%s unable to transcode legacy file; creating empty config.", __func__); 156 config = config_new_empty(); 157 btif_config_source = NEW_FILE; 158 file_source = "Empty"; 159 } 160 161 if (!file_source.empty()) 162 config_set_string(config, INFO_SECTION, FILE_SOURCE, file_source.c_str()); 163 164 if (!config) { 165 LOG_ERROR(LOG_TAG, "%s unable to allocate a config object.", __func__); 166 goto error; 167 } 168 169 btif_config_remove_unpaired(config); 170 171 // Cleanup temporary pairings if we have left guest mode 172 if (!is_restricted_mode()) 173 btif_config_remove_restricted(config); 174 175 // Read or set config file creation timestamp 176 const char* time_str; 177 time_str = config_get_string(config, INFO_SECTION, FILE_TIMESTAMP, NULL); 178 if (time_str != NULL) { 179 strlcpy(btif_config_time_created, time_str, TIME_STRING_LENGTH); 180 } else { 181 time_t current_time = time(NULL); 182 struct tm* time_created = localtime(¤t_time); 183 strftime(btif_config_time_created, TIME_STRING_LENGTH, TIME_STRING_FORMAT, time_created); 184 config_set_string(config, INFO_SECTION, FILE_TIMESTAMP, btif_config_time_created); 185 } 186 187 // TODO(sharvil): use a non-wake alarm for this once we have 188 // API support for it. There's no need to wake the system to 189 // write back to disk. 190 config_timer = alarm_new("btif.config"); 191 if (!config_timer) { 192 LOG_ERROR(LOG_TAG, "%s unable to create alarm.", __func__); 193 goto error; 194 } 195 196 pthread_mutex_unlock(&lock); 197 return future_new_immediate(FUTURE_SUCCESS); 198 199error: 200 alarm_free(config_timer); 201 config_free(config); 202 pthread_mutex_unlock(&lock); 203 pthread_mutex_destroy(&lock); 204 config_timer = NULL; 205 config = NULL; 206 btif_config_source = NOT_LOADED; 207 return future_new_immediate(FUTURE_FAIL); 208} 209 210static config_t *btif_config_open(const char *filename) { 211 config_t *config = config_new(filename); 212 if (!config) 213 return NULL; 214 215 if (!config_has_section(config, "Adapter")) { 216 LOG_ERROR(LOG_TAG, "Config is missing adapter section"); 217 config_free(config); 218 return NULL; 219 } 220 221 return config; 222} 223 224static future_t *shut_down(void) { 225 btif_config_flush(); 226 return future_new_immediate(FUTURE_SUCCESS); 227} 228 229static future_t *clean_up(void) { 230 btif_config_flush(); 231 232 alarm_free(config_timer); 233 config_free(config); 234 pthread_mutex_destroy(&lock); 235 config_timer = NULL; 236 config = NULL; 237 return future_new_immediate(FUTURE_SUCCESS); 238} 239 240EXPORT_SYMBOL module_t btif_config_module = { 241 .name = BTIF_CONFIG_MODULE, 242 .init = init, 243 .start_up = NULL, 244 .shut_down = shut_down, 245 .clean_up = clean_up 246}; 247 248bool btif_config_has_section(const char *section) { 249 assert(config != NULL); 250 assert(section != NULL); 251 252 pthread_mutex_lock(&lock); 253 bool ret = config_has_section(config, section); 254 pthread_mutex_unlock(&lock); 255 256 return ret; 257} 258 259bool btif_config_exist(const char *section, const char *key) { 260 assert(config != NULL); 261 assert(section != NULL); 262 assert(key != NULL); 263 264 pthread_mutex_lock(&lock); 265 bool ret = config_has_key(config, section, key); 266 pthread_mutex_unlock(&lock); 267 268 return ret; 269} 270 271bool btif_config_get_int(const char *section, const char *key, int *value) { 272 assert(config != NULL); 273 assert(section != NULL); 274 assert(key != NULL); 275 assert(value != NULL); 276 277 pthread_mutex_lock(&lock); 278 bool ret = config_has_key(config, section, key); 279 if (ret) 280 *value = config_get_int(config, section, key, *value); 281 pthread_mutex_unlock(&lock); 282 283 return ret; 284} 285 286bool btif_config_set_int(const char *section, const char *key, int value) { 287 assert(config != NULL); 288 assert(section != NULL); 289 assert(key != NULL); 290 291 pthread_mutex_lock(&lock); 292 config_set_int(config, section, key, value); 293 pthread_mutex_unlock(&lock); 294 295 return true; 296} 297 298bool btif_config_get_str(const char *section, const char *key, char *value, int *size_bytes) { 299 assert(config != NULL); 300 assert(section != NULL); 301 assert(key != NULL); 302 assert(value != NULL); 303 assert(size_bytes != NULL); 304 305 pthread_mutex_lock(&lock); 306 const char *stored_value = config_get_string(config, section, key, NULL); 307 pthread_mutex_unlock(&lock); 308 309 if (!stored_value) 310 return false; 311 312 strlcpy(value, stored_value, *size_bytes); 313 *size_bytes = strlen(value) + 1; 314 315 return true; 316} 317 318bool btif_config_set_str(const char *section, const char *key, const char *value) { 319 assert(config != NULL); 320 assert(section != NULL); 321 assert(key != NULL); 322 assert(value != NULL); 323 324 pthread_mutex_lock(&lock); 325 config_set_string(config, section, key, value); 326 pthread_mutex_unlock(&lock); 327 328 return true; 329} 330 331bool btif_config_get_bin(const char *section, const char *key, uint8_t *value, size_t *length) { 332 assert(config != NULL); 333 assert(section != NULL); 334 assert(key != NULL); 335 assert(value != NULL); 336 assert(length != NULL); 337 338 pthread_mutex_lock(&lock); 339 const char *value_str = config_get_string(config, section, key, NULL); 340 pthread_mutex_unlock(&lock); 341 342 if (!value_str) 343 return false; 344 345 size_t value_len = strlen(value_str); 346 if ((value_len % 2) != 0 || *length < (value_len / 2)) 347 return false; 348 349 for (size_t i = 0; i < value_len; ++i) 350 if (!isxdigit(value_str[i])) 351 return false; 352 353 for (*length = 0; *value_str; value_str += 2, *length += 1) 354 sscanf(value_str, "%02hhx", &value[*length]); 355 356 return true; 357} 358 359size_t btif_config_get_bin_length(const char *section, const char *key) { 360 assert(config != NULL); 361 assert(section != NULL); 362 assert(key != NULL); 363 364 pthread_mutex_lock(&lock); 365 const char *value_str = config_get_string(config, section, key, NULL); 366 pthread_mutex_unlock(&lock); 367 368 if (!value_str) 369 return 0; 370 371 size_t value_len = strlen(value_str); 372 return ((value_len % 2) != 0) ? 0 : (value_len / 2); 373} 374 375bool btif_config_set_bin(const char *section, const char *key, const uint8_t *value, size_t length) { 376 const char *lookup = "0123456789abcdef"; 377 378 assert(config != NULL); 379 assert(section != NULL); 380 assert(key != NULL); 381 382 if (length > 0) 383 assert(value != NULL); 384 385 char *str = (char *)osi_calloc(length * 2 + 1); 386 387 for (size_t i = 0; i < length; ++i) { 388 str[(i * 2) + 0] = lookup[(value[i] >> 4) & 0x0F]; 389 str[(i * 2) + 1] = lookup[value[i] & 0x0F]; 390 } 391 392 pthread_mutex_lock(&lock); 393 config_set_string(config, section, key, str); 394 pthread_mutex_unlock(&lock); 395 396 osi_free(str); 397 return true; 398} 399 400const btif_config_section_iter_t *btif_config_section_begin(void) { 401 assert(config != NULL); 402 return (const btif_config_section_iter_t *)config_section_begin(config); 403} 404 405const btif_config_section_iter_t *btif_config_section_end(void) { 406 assert(config != NULL); 407 return (const btif_config_section_iter_t *)config_section_end(config); 408} 409 410const btif_config_section_iter_t *btif_config_section_next(const btif_config_section_iter_t *section) { 411 assert(config != NULL); 412 assert(section != NULL); 413 return (const btif_config_section_iter_t *)config_section_next((const config_section_node_t *)section); 414} 415 416const char *btif_config_section_name(const btif_config_section_iter_t *section) { 417 assert(config != NULL); 418 assert(section != NULL); 419 return config_section_name((const config_section_node_t *)section); 420} 421 422bool btif_config_remove(const char *section, const char *key) { 423 assert(config != NULL); 424 assert(section != NULL); 425 assert(key != NULL); 426 427 pthread_mutex_lock(&lock); 428 bool ret = config_remove_key(config, section, key); 429 pthread_mutex_unlock(&lock); 430 431 return ret; 432} 433 434void btif_config_save(void) { 435 assert(config != NULL); 436 assert(config_timer != NULL); 437 438 alarm_set(config_timer, CONFIG_SETTLE_PERIOD_MS, timer_config_save_cb, NULL); 439} 440 441void btif_config_flush(void) { 442 assert(config != NULL); 443 assert(config_timer != NULL); 444 445 alarm_cancel(config_timer); 446 btif_config_write(0, NULL); 447} 448 449bool btif_config_clear(void) { 450 assert(config != NULL); 451 assert(config_timer != NULL); 452 453 alarm_cancel(config_timer); 454 455 pthread_mutex_lock(&lock); 456 config_free(config); 457 458 config = config_new_empty(); 459 if (config == NULL) { 460 pthread_mutex_unlock(&lock); 461 return false; 462 } 463 464 bool ret = config_save(config, CONFIG_FILE_PATH); 465 btif_config_source = RESET; 466 pthread_mutex_unlock(&lock); 467 return ret; 468} 469 470static void timer_config_save_cb(UNUSED_ATTR void *data) { 471 // Moving file I/O to btif context instead of timer callback because 472 // it usually takes a lot of time to be completed, introducing 473 // delays during A2DP playback causing blips or choppiness. 474 btif_transfer_context(btif_config_write, 0, NULL, 0, NULL); 475} 476 477static void btif_config_write(UNUSED_ATTR uint16_t event, UNUSED_ATTR char *p_param) { 478 assert(config != NULL); 479 assert(config_timer != NULL); 480 481 pthread_mutex_lock(&lock); 482 rename(CONFIG_FILE_PATH, CONFIG_BACKUP_PATH); 483 config_t *config_paired = config_new_clone(config); 484 btif_config_remove_unpaired(config_paired); 485 config_save(config_paired, CONFIG_FILE_PATH); 486 config_free(config_paired); 487 pthread_mutex_unlock(&lock); 488} 489 490static void btif_config_remove_unpaired(config_t *conf) { 491 assert(conf != NULL); 492 int paired_devices = 0; 493 494 // The paired config used to carry information about 495 // discovered devices during regular inquiry scans. 496 // We remove these now and cache them in memory instead. 497 const config_section_node_t *snode = config_section_begin(conf); 498 while (snode != config_section_end(conf)) { 499 const char *section = config_section_name(snode); 500 if (string_is_bdaddr(section)) { 501 if (!config_has_key(conf, section, "LinkKey") && 502 !config_has_key(conf, section, "LE_KEY_PENC") && 503 !config_has_key(conf, section, "LE_KEY_PID") && 504 !config_has_key(conf, section, "LE_KEY_PCSRK") && 505 !config_has_key(conf, section, "LE_KEY_LENC") && 506 !config_has_key(conf, section, "LE_KEY_LCSRK")) { 507 snode = config_section_next(snode); 508 config_remove_section(conf, section); 509 continue; 510 } 511 paired_devices++; 512 } 513 snode = config_section_next(snode); 514 } 515 516 // should only happen once, at initial load time 517 if (btif_config_devices_loaded == -1) 518 btif_config_devices_loaded = paired_devices; 519} 520 521void btif_debug_config_dump(int fd) { 522 dprintf(fd, "\nBluetooth Config:\n"); 523 524 dprintf(fd, " Config Source: "); 525 switch(btif_config_source) { 526 case NOT_LOADED: 527 dprintf(fd, "Not loaded\n"); 528 break; 529 case ORIGINAL: 530 dprintf(fd, "Original file\n"); 531 break; 532 case BACKUP: 533 dprintf(fd, "Backup file\n"); 534 break; 535 case LEGACY: 536 dprintf(fd, "Legacy file\n"); 537 break; 538 case NEW_FILE: 539 dprintf(fd, "New file\n"); 540 break; 541 case RESET: 542 dprintf(fd, "Reset file\n"); 543 break; 544 } 545 546 dprintf(fd, " Devices loaded: %d\n", btif_config_devices_loaded); 547 dprintf(fd, " File created/tagged: %s\n", btif_config_time_created); 548 dprintf(fd, " File source: %s\n", config_get_string(config, INFO_SECTION, 549 FILE_SOURCE, "Original")); 550} 551 552static void btif_config_remove_restricted(config_t* config) { 553 assert(config != NULL); 554 555 const config_section_node_t *snode = config_section_begin(config); 556 while (snode != config_section_end(config)) { 557 const char *section = config_section_name(snode); 558 if (string_is_bdaddr(section) && config_has_key(config, section, "Restricted")) { 559 BTIF_TRACE_DEBUG("%s: Removing restricted device %s", __func__, section); 560 config_remove_section(config, section); 561 } 562 snode = config_section_next(snode); 563 } 564} 565 566static bool is_factory_reset(void) { 567 char factory_reset[PROPERTY_VALUE_MAX] = {0}; 568 osi_property_get("persist.bluetooth.factoryreset", factory_reset, "false"); 569 return strncmp(factory_reset, "true", 4) == 0; 570} 571 572static void delete_config_files(void) { 573 remove(CONFIG_FILE_PATH); 574 remove(CONFIG_BACKUP_PATH); 575 osi_property_set("persist.bluetooth.factoryreset", "false"); 576} 577