From 9a88eef632f587fce964bdff02885c571c89f994 Mon Sep 17 00:00:00 2001 From: boblane Date: Thu, 9 Jul 2020 11:17:51 +0800 Subject: [PATCH] fix esp_bt_gap_get_bond_device_list bug Closes https://github.com/espressif/esp-idf/issues/5395 --- components/bt/common/osi/config.c | 8 +++++++- components/bt/host/bluedroid/api/esp_gap_bt_api.c | 10 ++-------- components/bt/host/bluedroid/btc/core/btc_storage.c | 10 +++++++--- .../bt/host/bluedroid/btc/include/btc/btc_storage.h | 2 +- 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/components/bt/common/osi/config.c b/components/bt/common/osi/config.c index 14b98ca1d..b3b881603 100644 --- a/components/bt/common/osi/config.c +++ b/components/bt/common/osi/config.c @@ -267,6 +267,7 @@ bool config_remove_key(config_t *config, const char *section, const char *key) assert(config != NULL); assert(section != NULL); assert(key != NULL); + bool ret; section_t *sec = section_find(config, section); entry_t *entry = entry_find(config, section, key); @@ -274,7 +275,12 @@ bool config_remove_key(config_t *config, const char *section, const char *key) return false; } - return list_remove(sec->entries, entry); + ret = list_remove(sec->entries, entry); + if (list_length(sec->entries) == 0) { + OSI_TRACE_DEBUG("%s remove section name:%s",__func__, section); + ret &= config_remove_section(config, section); + } + return ret; } const config_section_node_t *config_section_begin(const config_t *config) diff --git a/components/bt/host/bluedroid/api/esp_gap_bt_api.c b/components/bt/host/bluedroid/api/esp_gap_bt_api.c index 567e38d0c..f1168ec22 100644 --- a/components/bt/host/bluedroid/api/esp_gap_bt_api.c +++ b/components/bt/host/bluedroid/api/esp_gap_bt_api.c @@ -246,7 +246,7 @@ esp_err_t esp_bt_gap_remove_bond_device(esp_bd_addr_t bd_addr) int esp_bt_gap_get_bond_device_num(void) { if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) { - return ESP_FAIL; + return ESP_ERR_INVALID_STATE; } return btc_storage_get_num_bt_bond_devices(); } @@ -254,7 +254,6 @@ int esp_bt_gap_get_bond_device_num(void) esp_err_t esp_bt_gap_get_bond_device_list(int *dev_num, esp_bd_addr_t *dev_list) { int ret; - int dev_num_total; if (dev_num == NULL || dev_list == NULL) { return ESP_ERR_INVALID_ARG; @@ -264,12 +263,7 @@ esp_err_t esp_bt_gap_get_bond_device_list(int *dev_num, esp_bd_addr_t *dev_list) return ESP_ERR_INVALID_STATE; } - dev_num_total = btc_storage_get_num_bt_bond_devices(); - if (*dev_num > dev_num_total) { - *dev_num = dev_num_total; - } - - ret = btc_storage_get_bonded_bt_devices_list((bt_bdaddr_t *)dev_list, *dev_num); + ret = btc_storage_get_bonded_bt_devices_list((bt_bdaddr_t *)dev_list, dev_num); return (ret == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } diff --git a/components/bt/host/bluedroid/btc/core/btc_storage.c b/components/bt/host/bluedroid/btc/core/btc_storage.c index f7ffba015..97bef05fc 100644 --- a/components/bt/host/bluedroid/btc/core/btc_storage.c +++ b/components/bt/host/bluedroid/btc/core/btc_storage.c @@ -201,7 +201,6 @@ int btc_storage_get_num_bt_bond_devices(void) } } btc_config_unlock(); - return num_dev; } @@ -215,15 +214,17 @@ int btc_storage_get_num_bt_bond_devices(void) ** BT_STATUS_FAIL otherwise ** *******************************************************************************/ -bt_status_t btc_storage_get_bonded_bt_devices_list(bt_bdaddr_t *bond_dev, int dev_num) +bt_status_t btc_storage_get_bonded_bt_devices_list(bt_bdaddr_t *bond_dev, int *dev_num) { bt_bdaddr_t bd_addr; + int in_dev_num = *dev_num; /* buffer size */ + int out_dev_num = 0; /* bond_dev size */ 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) { + if (in_dev_num <= 0) { break; } @@ -236,9 +237,12 @@ bt_status_t btc_storage_get_bonded_bt_devices_list(bt_bdaddr_t *bond_dev, int de btc_config_exist(name, BTC_STORAGE_LINK_KEY_STR)) { string_to_bdaddr(name, &bd_addr); memcpy(bond_dev, &bd_addr, sizeof(bt_bdaddr_t)); + in_dev_num--; + out_dev_num++; bond_dev++; } } + *dev_num = out_dev_num; /* out_dev_num <= in_dev_num */ btc_config_unlock(); return BT_STATUS_SUCCESS; diff --git a/components/bt/host/bluedroid/btc/include/btc/btc_storage.h b/components/bt/host/bluedroid/btc/include/btc/btc_storage.h index 9016502bd..f74c3eeb3 100644 --- a/components/bt/host/bluedroid/btc/include/btc/btc_storage.h +++ b/components/bt/host/bluedroid/btc/include/btc/btc_storage.h @@ -89,6 +89,6 @@ int btc_storage_get_num_bt_bond_devices(void); ** BT_STATUS_FAIL otherwise ** *******************************************************************************/ -bt_status_t btc_storage_get_bonded_bt_devices_list(bt_bdaddr_t *bond_dev, int dev_num); +bt_status_t btc_storage_get_bonded_bt_devices_list(bt_bdaddr_t *bond_dev, int *dev_num); #endif /* BTC_STORAGE_H */