static bt_status_t btc_storage_in_fetch_bonded_ble_devices(int add) { bt_status_t status = BT_STATUS_FAIL; uint32_t device_type = 0; btc_config_lock(); for (const btc_config_section_iter_t *iter = btc_config_section_begin(); iter != btc_config_section_end(); iter = btc_config_section_next(iter)) { const char *name = btc_config_section_name(iter); if (!string_is_bdaddr(name) || !btc_config_get_int(name, BTC_BLE_STORAGE_DEV_TYPE_STR, (int *)&device_type) || ((device_type & BT_DEVICE_TYPE_BLE) != BT_DEVICE_TYPE_BLE)) { continue; } LOG_DEBUG("%s, name = %s", __func__, name); if (_btc_storage_in_fetch_bonded_ble_device(name, add) != BT_STATUS_SUCCESS) { LOG_DEBUG("Remote device:%s, no link key or ble key found", name); } else { status = BT_STATUS_SUCCESS; } } btc_config_unlock(); return status; }
static void _btc_storage_save(void) { const btc_config_section_iter_t *iter = btc_config_section_begin(); while (iter != btc_config_section_end()) { //store the next iter, if remove section, then will not loss the point const char *section = btc_config_section_name(iter); if (!string_is_bdaddr(section)) { iter = btc_config_section_next(iter); continue; } if (!btc_config_exist(section, BTC_BLE_STORAGE_DEV_TYPE_STR) && !btc_config_exist(section, BTC_BLE_STORAGE_ADDR_TYPE_STR) && !btc_config_exist(section, BTC_BLE_STORAGE_LINK_KEY_STR) && !btc_config_exist(section, BTC_BLE_STORAGE_LE_KEY_PENC_STR) && !btc_config_exist(section, BTC_BLE_STORAGE_LE_KEY_PID_STR) && !btc_config_exist(section, BTC_BLE_STORAGE_LE_KEY_PCSRK_STR) && !btc_config_exist(section, BTC_BLE_STORAGE_LE_KEY_LENC_STR) && !btc_config_exist(section, BTC_BLE_STORAGE_LE_KEY_LCSRK_STR)) { iter = btc_config_section_next(iter); btc_config_remove_section(section); continue; } iter = btc_config_section_next(iter); } btc_config_flush(); }
bt_status_t btc_storage_get_bonded_ble_devices_list(esp_ble_bond_dev_t *bond_dev, int dev_num) { bt_bdaddr_t bd_addr; uint32_t device_type = 0; char buffer[sizeof(tBTM_LE_KEY_VALUE)] = {0}; btc_config_lock(); for (const btc_config_section_iter_t *iter = btc_config_section_begin(); iter != btc_config_section_end(); iter = btc_config_section_next(iter)) { if (dev_num-- <= 0) { break; } const char *name = btc_config_section_name(iter); if (!string_is_bdaddr(name) || !btc_config_get_int(name, BTC_BLE_STORAGE_DEV_TYPE_STR, (int *)&device_type) || !(device_type & BT_DEVICE_TYPE_BLE)) { continue; } string_to_bdaddr(name, &bd_addr); memcpy(bond_dev->bd_addr, bd_addr.address, sizeof(bt_bdaddr_t)); //resolve the peer device long term key if (_btc_storage_get_ble_bonding_key(&bd_addr, BTM_LE_KEY_PENC, buffer, sizeof(tBTM_LE_PENC_KEYS)) == BT_STATUS_SUCCESS) { bond_dev->bond_key.key_mask |= ESP_BLE_ENC_KEY_MASK; memcpy(&bond_dev->bond_key.penc_key, buffer, sizeof(tBTM_LE_PENC_KEYS)); } //resolve the peer device csrk if (_btc_storage_get_ble_bonding_key(&bd_addr, BTM_LE_KEY_PCSRK, buffer, sizeof(tBTM_LE_PCSRK_KEYS)) == BT_STATUS_SUCCESS) { bond_dev->bond_key.key_mask |= ESP_BLE_CSR_KEY_MASK; memcpy(&bond_dev->bond_key.pcsrk_key, buffer, sizeof(tBTM_LE_PCSRK_KEYS)); } //resolve the peer device irk if (_btc_storage_get_ble_bonding_key(&bd_addr, BTM_LE_KEY_PID, buffer, sizeof(tBTM_LE_PID_KEYS)) == BT_STATUS_SUCCESS) { bond_dev->bond_key.key_mask |= ESP_BLE_ID_KEY_MASK; memcpy(&bond_dev->bond_key.pid_key, buffer, sizeof(tBTM_LE_PID_KEYS)); } //serch for the next bond device bond_dev++; } btc_config_unlock(); return BT_STATUS_SUCCESS; }
void btc_config_save(void) { assert(config != NULL); // Garbage collection process: the config file accumulates // cached information about remote devices during regular // inquiry scans. We remove some of these junk entries // so the file doesn't grow indefinitely. We have to take care // to make sure we don't remove information about bonded // devices (hence the check for link keys). static const size_t CACHE_MAX = 256; const char *keys[CACHE_MAX]; size_t num_keys = 0; size_t total_candidates = 0; osi_mutex_lock(&lock, OSI_MUTEX_MAX_TIMEOUT); for (const config_section_node_t *snode = config_section_begin(config); snode != config_section_end(config); snode = config_section_next(snode)) { const char *section = config_section_name(snode); if (!string_is_bdaddr(section)) { continue; } if (config_has_key(config, section, "LinkKey") || config_has_key(config, section, "LE_KEY_PENC") || config_has_key(config, section, "LE_KEY_PID") || config_has_key(config, section, "LE_KEY_PCSRK") || config_has_key(config, section, "LE_KEY_LENC") || config_has_key(config, section, "LE_KEY_LCSRK")) { continue; } if (num_keys < CACHE_MAX) { keys[num_keys++] = section; } ++total_candidates; } if (total_candidates > CACHE_MAX * 2) while (num_keys > 0) { config_remove_section(config, keys[--num_keys]); } config_save(config, CONFIG_FILE_PATH); osi_mutex_unlock(&lock); }
int btc_storage_get_num_ble_bond_devices(void) { int num_dev = 0; uint32_t device_type = 0; btc_config_lock(); for (const btc_config_section_iter_t *iter = btc_config_section_begin(); iter != btc_config_section_end(); iter = btc_config_section_next(iter)) { const char *name = btc_config_section_name(iter); if (!string_is_bdaddr(name) || !btc_config_get_int(name, BTC_BLE_STORAGE_DEV_TYPE_STR, (int *)&device_type) || !(device_type & BT_DEVICE_TYPE_BLE)) { continue; } num_dev++; } btc_config_unlock(); return num_dev; }