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