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