]> granicus.if.org Git - esp-idf/commitdiff
component/bt: bluetooth storage module clean up and bug fix.
authorwangmengyang <wangmengyang@espressif.com>
Fri, 20 Jan 2017 08:46:10 +0000 (16:46 +0800)
committerwangmengyang <wangmengyang@espressif.com>
Fri, 20 Jan 2017 08:46:10 +0000 (16:46 +0800)
1. clean up bluetooth storage log prints;
2. reduce maximum link key storage size
3. modify so that the link key information of the latest connected device is stored at first

components/bt/bluedroid/btif/btif_config.c
components/bt/bluedroid/btif/btif_dm.c
components/bt/bluedroid/btif/btif_storage.c
components/bt/bluedroid/osi/config.c
components/bt/bluedroid/osi/include/config.h

index b080b2a8e2ea99ad5d5d1aab5e8472b0747b6142..5d32d0da4ca73df9cbdbe786b0bd59172d53e7e9 100644 (file)
@@ -18,9 +18,7 @@
 
 #define LOG_TAG "bt_btif_config"
 
-// #include <assert.h>
 #include <ctype.h>
-// #include <pthread.h>
 #include <stdio.h>
 #include <string.h>
 
@@ -31,7 +29,6 @@
 #include "bdaddr.h"
 #include "btif_config.h"
 #include "btif_util.h"
-// #include "osi/include/compat.h"
 #include "config.h"
 #include "osi.h"
 
@@ -60,7 +57,7 @@ bool btif_get_device_type(const BD_ADDR bd_addr, int *p_device_type)
         return FALSE;
     }
 
-    LOG_DEBUG("%s: Device [%s] type %d", __FUNCTION__, bd_addr_str, *p_device_type);
+    LOG_DEBUG("%s: Device [%s] type %d\n", __FUNCTION__, bd_addr_str, *p_device_type);
     return TRUE;
 }
 
@@ -80,7 +77,7 @@ bool btif_get_address_type(const BD_ADDR bd_addr, int *p_addr_type)
         return FALSE;
     }
 
-    LOG_DEBUG("%s: Device [%s] address type %d", __FUNCTION__, bd_addr_str, *p_addr_type);
+    LOG_DEBUG("%s: Device [%s] address type %d\n", __FUNCTION__, bd_addr_str, *p_addr_type);
     return TRUE;
 }
 
@@ -92,15 +89,13 @@ static osi_alarm_t *alarm_timer;
 
 bool btif_config_init(void)
 {
-    // karl LOG_ERROR("btif config_init\n");
-
     pthread_mutex_init(&lock, NULL);
     config = config_new(CONFIG_FILE_PATH);
     if (!config) {
         LOG_WARN("%s unable to load config file; starting unconfigured.\n", __func__);
         config = config_new_empty();
         if (!config) {
-            LOG_ERROR("%s unable to allocate a config object.", __func__);
+            LOG_ERROR("%s unable to allocate a config object.\n", __func__);
             goto error;
         }
     }
@@ -114,11 +109,10 @@ bool btif_config_init(void)
     // write back to disk.
     alarm_timer = osi_alarm_new("btif_config", timer_config_save, NULL, CONFIG_SETTLE_PERIOD_MS, false);
     if (!alarm_timer) {
-        LOG_ERROR("%s unable to create alarm.", __func__);
+        LOG_ERROR("%s unable to create alarm.\n", __func__);
         goto error;
     }
 
-    // LOG_ERROR("btif config_init end ok\n");
     return true;
 
 error:;
@@ -127,7 +121,7 @@ error:;
     pthread_mutex_destroy(&lock);
     alarm_timer = NULL;
     config = NULL;
-    LOG_ERROR("btif config_init end failed\n");
+    LOG_ERROR("%s failed\n", __func__);
     return false;
 }
 
@@ -234,7 +228,7 @@ bool btif_config_set_str(const char *section, const char *key, const char *value
     assert(value != NULL);
 
     pthread_mutex_lock(&lock);
-    config_set_string(config, section, key, value);
+    config_set_string(config, section, key, value, false);
     pthread_mutex_unlock(&lock);
 
     return true;
@@ -316,7 +310,7 @@ bool btif_config_set_bin(const char *section, const char *key, const uint8_t *va
     }
 
     pthread_mutex_lock(&lock);
-    config_set_string(config, section, key, str);
+    config_set_string(config, section, key, str, false);
     pthread_mutex_unlock(&lock);
 
     osi_free(str);
@@ -374,13 +368,11 @@ void btif_config_flush(void)
 {
     assert(config != NULL);
     assert(alarm_timer != NULL);
-    LOG_ERROR("flush bgn\n"); // karl
-    // osi_alarm_cancel(alarm_timer);
+    osi_alarm_cancel(alarm_timer);
 
     pthread_mutex_lock(&lock);
     config_save(config, CONFIG_FILE_PATH);
     pthread_mutex_unlock(&lock);
-    LOG_ERROR("flush end\n"); // karl
 }
 
 int btif_config_clear(void)
index f4ba2a0a8b8452ae5329695fd16d8b3bac01cdb5..b74d858cdda46e8a22aafdde5b98a9b6fd3defdb 100644 (file)
@@ -225,6 +225,7 @@ static void btif_dm_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl)
                 status =  BT_STATUS_FAIL;
         }
     }
+    (void) status;
 }
 
 /*******************************************************************************
index 19eeaf1f23d87502858ca8bcf94aeb05473fbca1..16e50bc4c2f9e3c8baa39b857cb628ba1fab0f15 100644 (file)
@@ -28,18 +28,14 @@ bt_status_t btif_storage_add_bonded_device(bt_bdaddr_t *remote_bd_addr,
     bdstr_t bdstr;
 
     bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
-    BTIF_TRACE_WARNING("add to storage: Remote device:%s", bdstr);
+    BTIF_TRACE_EVENT("add to storage: Remote device:%s\n", bdstr);
 
     int ret = btif_config_set_int(bdstr, "LinkKeyType", (int)key_type);
-    BTIF_TRACE_WARNING("p1\n");
     ret &= btif_config_set_int(bdstr, "PinLength", (int)pin_length);
-    BTIF_TRACE_WARNING("p2\n");
     ret &= btif_config_set_bin(bdstr, "LinkKey", link_key, sizeof(LINK_KEY));
-    BTIF_TRACE_WARNING("p3\n");
     /* write bonded info immediately */
-    // karl
     btif_config_flush();
-    BTIF_TRACE_WARNING("Storage add rslt %d\n", ret);
+    BTIF_TRACE_EVENT("Storage add rslt %d\n", ret);
     return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
 }
 
@@ -56,7 +52,6 @@ bt_status_t btif_storage_add_bonded_device(bt_bdaddr_t *remote_bd_addr,
 static bt_status_t btif_in_fetch_bonded_devices(int add)
 {
     BOOLEAN bt_linkkey_file_found = FALSE;
-    int device_type;
 
     for (const btif_config_section_iter_t *iter = btif_config_section_begin(); iter != btif_config_section_end(); iter = btif_config_section_next(iter)) {
         const char *name = btif_config_section_name(iter);
@@ -64,7 +59,7 @@ static bt_status_t btif_in_fetch_bonded_devices(int add)
             continue;
         }
 
-        BTIF_TRACE_WARNING("Remote device:%s", name);
+        BTIF_TRACE_WARNING("Remote device:%s\n", name);
         LINK_KEY link_key;
         size_t size = sizeof(link_key);
         if (btif_config_get_bin(name, "LinkKey", link_key, &size)) {
@@ -87,55 +82,14 @@ static bt_status_t btif_in_fetch_bonded_devices(int add)
                 }
                 bt_linkkey_file_found = TRUE;
             } else {
-                BTIF_TRACE_ERROR("bounded device:%s, LinkKeyType or PinLength is invalid", name);
+                BTIF_TRACE_ERROR("bounded device:%s, LinkKeyType or PinLength is invalid\n", name);
             }
         }
         if (!bt_linkkey_file_found) {
-            BTIF_TRACE_EVENT("Remote device:%s, no link key", name);
+            BTIF_TRACE_EVENT("Remote device:%s, no link key\n", name);
         }
     }
     return BT_STATUS_SUCCESS;
-#if 0
-    int device_type;
-
-    BTIF_TRACE_WARNING("fetch from storage");
-    do {
-        const char *name = "bt_host";
-
-        bt_bdaddr_t bd_addr;
-        size_t size = sizeof(bt_bdaddr_t);
-        if (!btif_config_get_bin(name, "BdAddr", bd_addr.address, &size)) {
-            continue;
-        } else {
-            bdstr_t bdstr;
-            bdaddr_to_string(&bd_addr, bdstr, sizeof(bdstr));
-            BTIF_TRACE_WARNING("fetch from storage: Remote device:%s\n", bdstr);
-        }
-
-        LINK_KEY link_key;
-        size = sizeof(link_key);
-        if (btif_config_get_bin(name, "LinkKey", link_key, &size)) {
-            int linkkey_type;
-            if (btif_config_get_int(name, "LinkKeyType", &linkkey_type)) {
-                if (add) {
-                    DEV_CLASS dev_class = {0, 0, 0};
-                    int cod;
-                    int pin_length = 0;
-                    if (btif_config_get_int(name, "DevClass", &cod)) {
-                        uint2devclass((UINT32)cod, dev_class);
-                    }
-                    btif_config_get_int(name, "PinLength", &pin_length);
-                    BTA_DmAddDevice(bd_addr.address, dev_class, link_key, 0, 0,
-                                    (UINT8)linkkey_type, 0, pin_length);
-
-                }
-            } else {
-                BTIF_TRACE_ERROR("bounded device:%s, LinkKeyType or PinLength is invalid\n", name);
-            }
-        }
-    } while (0);
-    return BT_STATUS_SUCCESS;
-#endif
 }
 
 
@@ -155,7 +109,7 @@ bt_status_t btif_storage_load_bonded_devices(void)
 {
     bt_status_t status;
     status = btif_in_fetch_bonded_devices(1);
-    BTIF_TRACE_WARNING("Storage load rslt %d\n", status);
+    BTIF_TRACE_EVENT("Storage load rslt %d\n", status);
     return status;
 }
 
@@ -173,7 +127,7 @@ bt_status_t btif_storage_remove_bonded_device(bt_bdaddr_t *remote_bd_addr)
 {
     bdstr_t bdstr;
     bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
-    BTIF_TRACE_EVENT("add to storage: Remote device:%s\n", bdstr);
+    BTIF_TRACE_EVENT("Add to storage: Remote device:%s\n", bdstr);
 
     int ret = 1;
     if (btif_config_exist(bdstr, "LinkKeyType")) {
index fadb75bc743e304469a22571718783af2836a231..f42459027217a6b6e8a0b3cb66582289b2771e0e 100644 (file)
@@ -21,7 +21,6 @@
 #include "nvs_flash.h"
 #include "nvs.h"
 
-// #include <assert.h>
 #include <ctype.h>
 #include <errno.h>
 #include <stdio.h>
@@ -34,8 +33,8 @@
 #include "list.h"
 #include "bt_trace.h"
 
-#define CONFIG_FILE_MAX_SIZE      (4096)
-#define CONFIG_KEY                "cfg_key8"
+#define CONFIG_FILE_MAX_SIZE      (512)
+#define CONFIG_KEY                "cfg_key1"
 typedef struct {
     char *key;
     char *value;
@@ -63,24 +62,17 @@ static entry_t *entry_new(const char *key, const char *value);
 static void entry_free(void *ptr);
 static entry_t *entry_find(const config_t *config, const char *section, const char *key);
 
-static void my_nvs_close(nvs_handle fp)
-{
-    // LOG_ERROR("nvs close %d\n", (int)fp);
-    nvs_close(fp);
-}
-
-
 config_t *config_new_empty(void)
 {
     config_t *config = osi_calloc(sizeof(config_t));
     if (!config) {
-        LOG_ERROR("%s unable to allocate memory for config_t.", __func__);
+        LOG_ERROR("%s unable to allocate memory for config_t.\n", __func__);
         goto error;
     }
 
     config->sections = list_new(section_free);
     if (!config->sections) {
-        LOG_ERROR("%s unable to allocate list for sections.", __func__);
+        LOG_ERROR("%s unable to allocate list for sections.\n", __func__);
         goto error;
     }
 
@@ -95,7 +87,6 @@ config_t *config_new(const char *filename)
 {
     assert(filename != NULL);
 
-    // LOG_ERROR("config new bgn\n");
     config_t *config = config_new_empty();
     if (!config) {
         return NULL;
@@ -105,15 +96,13 @@ config_t *config_new(const char *filename)
     nvs_handle fp;
     err = nvs_open(filename, NVS_READWRITE, &fp);
     if (err != ESP_OK) {
-        LOG_ERROR("%s unable to open file '%s'", __func__, filename);
+        LOG_ERROR("%s unable to open file '%s'\n", __func__, filename);
         config_free(config);
         return NULL;
     }
 
-    // LOG_ERROR("config parse bgn\n");
     config_parse(fp, config);
-    // LOG_ERROR("config parse end\n");
-    my_nvs_close(fp);
+    nvs_close(fp);
     return config;
 }
 
@@ -203,7 +192,7 @@ void config_set_int(config_t *config, const char *section, const char *key, int
 
     char value_str[32] = { 0 };
     sprintf(value_str, "%d", value);
-    config_set_string(config, section, key, value_str);
+    config_set_string(config, section, key, value_str, false);
 }
 
 void config_set_bool(config_t *config, const char *section, const char *key, bool value)
@@ -212,15 +201,19 @@ void config_set_bool(config_t *config, const char *section, const char *key, boo
     assert(section != NULL);
     assert(key != NULL);
 
-    config_set_string(config, section, key, value ? "true" : "false");
+    config_set_string(config, section, key, value ? "true" : "false", false);
 }
 
-void config_set_string(config_t *config, const char *section, const char *key, const char *value)
+void config_set_string(config_t *config, const char *section, const char *key, const char *value, bool insert_back)
 {
     section_t *sec = section_find(config, section);
     if (!sec) {
         sec = section_new(section);
-        list_append(config->sections, sec);
+        if (insert_back) {
+            list_append(config->sections, sec);
+        } else {
+            list_prepend(config->sections, sec);
+        }
     }
 
     for (const list_node_t *node = list_begin(sec->entries); node != list_end(sec->entries); node = list_next(node)) {
@@ -297,45 +290,40 @@ bool config_save(const config_t *config, const char *filename)
     assert(*filename != '\0');
 
     esp_err_t err;
+    int err_code = 0;
     nvs_handle fp;
     char *line = osi_calloc(1024);
     char *buf = osi_calloc(CONFIG_FILE_MAX_SIZE);
     if (!line || !buf) {
+        err_code |= 0x01;
         goto error;
     }
 
     err = nvs_open(filename, NVS_READWRITE, &fp);
-    // LOG_ERROR("nvs open: %d\n", (int)fp);
     if (err != ESP_OK) {
-        // LOG_ERROR("%s unable to write file '%s'\n", __func__, filename);
+        err_code |= 0x02;
         goto error;
     }
 
-    // LOG_ERROR("m1, %s\n", filename);
     int w_cnt, w_cnt_total = 0;
     for (const list_node_t *node = list_begin(config->sections); node != list_end(config->sections); node = list_next(node)) {
         const section_t *section = (const section_t *)list_node(node);
-        // LOG_ERROR("m11\n");
-        // LOG_ERROR("m12, %s\n", section->name);
+        LOG_DEBUG("section name: %s\n", section->name);
         w_cnt = snprintf(line, 1024, "[%s]\n", section->name);
-        // LOG_ERROR("m2 : %s\n", section->name);
         if (w_cnt + w_cnt_total < CONFIG_FILE_MAX_SIZE) {
             memcpy(buf + w_cnt_total, line, w_cnt);
             w_cnt_total += w_cnt;
-            // LOG_ERROR("m21\n");
         } else {
             break;
         }
-        // LOG_ERROR("m22\n");
+
         for (const list_node_t *enode = list_begin(section->entries); enode != list_end(section->entries); enode = list_next(enode)) {
             const entry_t *entry = (const entry_t *)list_node(enode);
-            // LOG_ERROR("m30: %s, %s\n", entry->key, entry->value);
+            LOG_DEBUG("(key, val): (%s, %s)\n", entry->key, entry->value);
             w_cnt = snprintf(line, 1024, "%s = %s\n", entry->key, entry->value);
-            // LOG_ERROR("m3 : %s, %s\n", entry->key, entry->value);
             if (w_cnt + w_cnt_total < CONFIG_FILE_MAX_SIZE) {
                 memcpy(buf + w_cnt_total, line, w_cnt);
                 w_cnt_total += w_cnt;
-
             } else {
                 break;
             }
@@ -352,55 +340,38 @@ bool config_save(const config_t *config, const char *filename)
         }
     }
 
-    LOG_ERROR("m4 : %s, %d\n", buf, w_cnt_total);
-    {
-        // LOG_ERROR("m4x\n");
-        size_t tmp_len = 4096;
-        char *buf1 = osi_calloc(tmp_len);
-        err = nvs_get_str(fp, CONFIG_KEY, buf1, &tmp_len);
-        if (err == ESP_OK) {
-            LOG_ERROR("rd %d\n%s", tmp_len, buf1);
-        }
-        // err = nvs_erase_key(fp, CONFIG_KEY);
-        // LOG_ERROR("m4y\n");
-        osi_free(buf1);
-    }
     buf[w_cnt_total] = '\0';
 
-    // LOG_ERROR("set str bgn %d, %s, %d %d\n", (int)fp, CONFIG_KEY, w_cnt_total, strlen(buf));
     err = nvs_set_blob(fp, CONFIG_KEY, buf, w_cnt_total);
-    // err = nvs_set_str(fp, CONFIG_KEY, "abc");
 
-    // LOG_ERROR("set str end\n");
     if (err != ESP_OK) {
-        // LOG_ERROR("m40\n");
-        my_nvs_close(fp);
-        LOG_ERROR("m41\n");
+        nvs_close(fp);
+        err_code |= 0x04;
         goto error;
     }
-    // LOG_ERROR("m5\n");
+
     err = nvs_commit(fp);
     if (err != ESP_OK) {
-        LOG_ERROR("m50\n");
-        my_nvs_close(fp);
-        LOG_ERROR("m51\n");
+        nvs_close(fp);
+        err_code |= 0x08;
         goto error;
     }
 
-    // LOG_ERROR("m6\n");
-    my_nvs_close(fp);
+    nvs_close(fp);
     osi_free(line);
     osi_free(buf);
     return true;
 
 error:
-    LOG_ERROR("m7\n");
     if (buf) {
         osi_free(buf);
     }
     if (line) {
         osi_free(line);
     }
+    if (err_code) {
+        LOG_ERROR("%s, err_code: 0x%x\n", __func__, err_code);
+    }
     return false;
 }
 
@@ -428,41 +399,39 @@ static void config_parse(nvs_handle fp, config_t *config)
     assert(fp != 0);
     assert(config != NULL);
 
-    LOG_ERROR("cfg parse\n");
     int line_num = 0;
+    int err_code = 0;
     char *line = osi_calloc(1024);
     char *section = osi_calloc(1024);
     char *buf = osi_calloc(CONFIG_FILE_MAX_SIZE);
     if (!line || !section || !buf) {
+        err_code |= 0x01;
         goto error;
     }
 
-    // LOG_ERROR("p1\n");
     esp_err_t err;
     size_t length = CONFIG_FILE_MAX_SIZE;
     err = nvs_get_blob(fp, CONFIG_KEY, buf, &length);
     if (err != ESP_OK) {
-        // LOG_ERROR("p2\n");
+        err_code |= 0x02;
         goto error;
     }
 
-    LOG_ERROR("p3 %d\n%s\n", length, buf);
     char *p_line_end;
     char *p_line_bgn = buf;
     strcpy(section, CONFIG_DEFAULT_SECTION);
-    // LOG_ERROR("p4\n");
+
     while ( (p_line_bgn < buf + length - 1) && (p_line_end = strchr(p_line_bgn, '\n'))) {
 
         // get one line
         int line_len = p_line_end - p_line_bgn;
-        // LOG_ERROR("pii, %d, %x, %x, %x\n", line_len, p_line_bgn, p_line_end, buf);
         if (line_len > 1023) {
+            LOG_WARN("%s exceed max line length on line %d.\n", __func__, line_num);
             break;
         }
         memcpy(line, p_line_bgn, line_len);
         line[line_len] = '\0';
         p_line_bgn = p_line_end + 1;
-        // LOG_ERROR("pi0\n");
         char *line_ptr = trim(line);
         ++line_num;
 
@@ -471,31 +440,26 @@ static void config_parse(nvs_handle fp, config_t *config)
             continue;
         }
 
-        // LOG_ERROR("pi1\n");
         if (*line_ptr == '[') {
             size_t len = strlen(line_ptr);
             if (line_ptr[len - 1] != ']') {
-                LOG_WARN("%s unterminated section name on line %d.", __func__, line_num);
+                LOG_WARN("%s unterminated section name on line %d.\n", __func__, line_num);
                 continue;
             }
             strncpy(section, line_ptr + 1, len - 2);
             section[len - 2] = '\0';
-            // LOG_ERROR("pi2\n");
         } else {
-            // LOG_ERROR("pi3\n");
             char *split = strchr(line_ptr, '=');
             if (!split) {
-                LOG_DEBUG("%s no key/value separator found on line %d.", __func__, line_num);
+                LOG_DEBUG("%s no key/value separator found on line %d.\n", __func__, line_num);
                 continue;
             }
-            // LOG_ERROR("pi4\n");
             *split = '\0';
-            config_set_string(config, section, trim(line_ptr), trim(split + 1));
+            config_set_string(config, section, trim(line_ptr), trim(split + 1), true);
         }
     }
 
 error:
-    // LOG_ERROR("p5\n");
     if (buf) {
         osi_free(buf);
     }
@@ -505,7 +469,9 @@ error:
     if (section) {
         osi_free(section);
     }
-    // LOG_ERROR("p6\n");
+    if (err_code) {
+        LOG_ERROR("%s returned with err code: %d\n", __func__, err_code);
+    }
 }
 
 static section_t *section_new(const char *name)
index ab3096d4e90bd2e85abf21d4577f3d4d78b1c8e0..b721fb55bcd940a29e3e9d9355de4072d39f4a5d 100755 (executable)
@@ -81,7 +81,7 @@ void config_set_bool(config_t *config, const char *section, const char *key, boo
 // Sets a string value for the |key| in |section|. If |key| or |section| do
 // not already exist, this function creates them. |config|, |section|, |key|, and
 // |value| must not be NULL.
-void config_set_string(config_t *config, const char *section, const char *key, const char *value);
+void config_set_string(config_t *config, const char *section, const char *key, const char *value, bool insert_back);
 
 // Removes |section| from the |config| (and, as a result, all keys in the section).
 // Returns true if |section| was found and removed from |config|, false otherwise.