]> granicus.if.org Git - esp-idf/commitdiff
component/bt: Added the hash key generated directly in the bluedroid module.
authorYulong <huangyulong@espressif.com>
Thu, 26 Apr 2018 13:44:12 +0000 (09:44 -0400)
committerYulong <huangyulong@espressif.com>
Sat, 28 Apr 2018 07:11:05 +0000 (03:11 -0400)
1. Change all the hash used from the coap module to generated by the bluedroid module directly.

component/bt: Added the <string.h> include file to the gattc_co module to avoid compile error.

components/bt/bluedroid/bta/gatt/bta_gattc_co.c
components/bt/bluedroid/bta/include/bta/bta_gattc_co.h
components/bt/bluedroid/osi/hash_functions.c
components/bt/bluedroid/osi/include/osi/hash_functions.h

index b3d73b6a5ecf2137c5ec7c6e8bcf5718e8bebde5..e6b49cb381eaa975a3cd802bf14ee0842d9b8241 100644 (file)
  *
  ******************************************************************************/
 #ifdef  BT_SUPPORT_NVM
-#include <stdio.h>
 #include <unistd.h>
 #endif /* BT_SUPPORT_NVM */
-
+#include <string.h>
+#include <stdio.h>
 #include "bta/bta_gattc_co.h"
 #include "bta/bta_gattc_ci.h"
+
 // #include "btif_util.h"
 #include "btm_int.h"
 #include "nvs.h"
@@ -83,7 +84,7 @@ typedef struct {
     nvs_handle cache_fp;
     BOOLEAN is_open;
     BD_ADDR addr;
-    coap_key_t hash_key;
+    hash_key_t hash_key;
     list_t *assoc_addr;
 }cache_addr_info_t;
 
@@ -97,7 +98,7 @@ typedef struct {
 
 cache_env_t cache_env;
 
-static void getFilename(char *buffer, coap_key_t hash)
+static void getFilename(char *buffer, hash_key_t hash)
 {
     sprintf(buffer, "%s%02x%02x%02x%02x", GATT_CACHE_PREFIX,
             hash[0], hash[1], hash[2], hash[3]);
@@ -120,13 +121,13 @@ static bool cacheOpen(BD_ADDR bda, bool to_save, UINT8 *index)
     char fname[255] = {0};
     UINT8 *assoc_addr = NULL;
     esp_err_t status = ESP_FAIL;
-    coap_key_t hash_key = {0};
+    hash_key_t hash_key = {0};
     if (((*index = bta_gattc_co_find_addr_in_cache(bda)) != INVALID_ADDR_NUM) ||
         ((assoc_addr = bta_gattc_co_cache_find_src_addr(bda, index)) != NULL)) {
         if (cache_env.cache_addr[*index].is_open) {
             return TRUE;
         } else {
-            memcpy(hash_key, cache_env.cache_addr[*index].hash_key, sizeof(coap_key_t));
+            memcpy(hash_key, cache_env.cache_addr[*index].hash_key, sizeof(hash_key_t));
             getFilename(fname, hash_key);
             if ((status = nvs_open(fname, NVS_READWRITE, &cache_env.cache_addr[*index].cache_fp)) == ESP_OK) {
                 // Set the open flag to TRUE when success to open the hash file.
@@ -258,10 +259,10 @@ void bta_gattc_co_cache_save (BD_ADDR server_bda, UINT16 num_attr,
                               tBTA_GATTC_NV_ATTR *p_attr_list)
 {
     tBTA_GATT_STATUS    status = BTA_GATT_OK;
-    coap_key_t hash_key = {0};
+    hash_key_t hash_key = {0};
     UINT8 index = INVALID_ADDR_NUM;
     //calculate  the hash value of the attribute table which should be added to the nvs flash.
-    coap_hash_impl((unsigned char *)p_attr_list, sizeof(tBTA_GATTC_NV_ATTR)*num_attr, hash_key);
+    hash_function_blob((unsigned char *)p_attr_list, sizeof(tBTA_GATTC_NV_ATTR)*num_attr, hash_key);
     //save the address list to the nvs flash
     bta_gattc_co_cache_addr_save(server_bda, hash_key);
 
@@ -334,13 +335,13 @@ void bta_gattc_co_cache_addr_init(void)
             osi_free(p_buf);
             return;
         }
-        num_addr = length / (sizeof(BD_ADDR) + sizeof(coap_key_t));
+        num_addr = length / (sizeof(BD_ADDR) + sizeof(hash_key_t));
         cache_env.num_addr = num_addr;
         //read the address from nvs flash to cache address list.
         for (UINT8 i = 0; i < num_addr; i++) {
-            memcpy(cache_env.cache_addr[i].addr, p_buf + i*(sizeof(BD_ADDR) + sizeof(coap_key_t)), sizeof(BD_ADDR));
+            memcpy(cache_env.cache_addr[i].addr, p_buf + i*(sizeof(BD_ADDR) + sizeof(hash_key_t)), sizeof(BD_ADDR));
             memcpy(cache_env.cache_addr[i].hash_key,
-                    p_buf + i*(sizeof(BD_ADDR) + sizeof(coap_key_t)) + sizeof(BD_ADDR), sizeof(coap_key_t));
+                    p_buf + i*(sizeof(BD_ADDR) + sizeof(hash_key_t)) + sizeof(BD_ADDR), sizeof(hash_key_t));
 
             APPL_TRACE_DEBUG("cache_addr[%x] = %x:%x:%x:%x:%x:%x", i, cache_env.cache_addr[i].addr[0], cache_env.cache_addr[i].addr[1], cache_env.cache_addr[i].addr[2],
                              cache_env.cache_addr[i].addr[3], cache_env.cache_addr[i].addr[4], cache_env.cache_addr[i].addr[5]);
@@ -387,13 +388,13 @@ UINT8 bta_gattc_co_find_addr_in_cache(BD_ADDR bda)
     return INVALID_ADDR_NUM;
 }
 
-UINT8 bta_gattc_co_find_hash_in_cache(coap_key_t hash_key)
+UINT8 bta_gattc_co_find_hash_in_cache(hash_key_t hash_key)
 {
     UINT8 index = 0;
     UINT8 num = cache_env.num_addr;
     cache_addr_info_t *addr_info = &cache_env.cache_addr[0];
     for (index = 0; index < num; index++) {
-        if (!memcmp(addr_info->hash_key, hash_key, sizeof(coap_key_t))) {
+        if (!memcmp(addr_info->hash_key, hash_key, sizeof(hash_key_t))) {
             return index;
         }
     }
@@ -414,7 +415,7 @@ void bta_gattc_co_get_addr_list(BD_ADDR *addr_list)
     }
 }
 
-void bta_gattc_co_cache_addr_save(BD_ADDR bd_addr, coap_key_t hash_key)
+void bta_gattc_co_cache_addr_save(BD_ADDR bd_addr, hash_key_t hash_key)
 {
     esp_err_t err_code;
     UINT8 num = ++cache_env.num_addr;
@@ -427,28 +428,28 @@ void bta_gattc_co_cache_addr_save(BD_ADDR bd_addr, coap_key_t hash_key)
             APPL_TRACE_DEBUG("%s(), the hash bd_addr already in the cache list, index = %x", __func__, index);
             //if the bd_addr already in the address list, update the hash key in it.
             memcpy(cache_env.cache_addr[index].addr, bd_addr, sizeof(BD_ADDR));
-            memcpy(cache_env.cache_addr[index].hash_key, hash_key, sizeof(coap_key_t));
+            memcpy(cache_env.cache_addr[index].hash_key, hash_key, sizeof(hash_key_t));
         } else {
             //if the bd_addr didn't in the address list, added the bd_addr to the last of the address list.
-            memcpy(cache_env.cache_addr[num - 1].hash_key, hash_key, sizeof(coap_key_t));
+            memcpy(cache_env.cache_addr[num - 1].hash_key, hash_key, sizeof(hash_key_t));
             memcpy(cache_env.cache_addr[num - 1].addr, bd_addr, sizeof(BD_ADDR));
         }
 
     } else {
         APPL_TRACE_DEBUG("%s(), num = %d", __func__, num);
         memcpy(cache_env.cache_addr[num - 1].addr, bd_addr, sizeof(BD_ADDR));
-        memcpy(cache_env.cache_addr[num - 1].hash_key, hash_key, sizeof(coap_key_t));
+        memcpy(cache_env.cache_addr[num - 1].hash_key, hash_key, sizeof(hash_key_t));
     }
 
     nvs_handle *fp = &cache_env.addr_fp;
-    UINT16 length = num*(sizeof(BD_ADDR) + sizeof(coap_key_t));
+    UINT16 length = num*(sizeof(BD_ADDR) + sizeof(hash_key_t));
 
     for (UINT8 i = 0; i < num; i++) {
         //copy the address to the buffer.
-        memcpy(p_buf + i*(sizeof(BD_ADDR) + sizeof(coap_key_t)), cache_env.cache_addr[i].addr, sizeof(BD_ADDR));
+        memcpy(p_buf + i*(sizeof(BD_ADDR) + sizeof(hash_key_t)), cache_env.cache_addr[i].addr, sizeof(BD_ADDR));
         //copy the hash key to the buffer.
-        memcpy(p_buf + i*(sizeof(BD_ADDR) + sizeof(coap_key_t)) + sizeof(BD_ADDR),
-                cache_env.cache_addr[i].hash_key, sizeof(coap_key_t));
+        memcpy(p_buf + i*(sizeof(BD_ADDR) + sizeof(hash_key_t)) + sizeof(BD_ADDR),
+                cache_env.cache_addr[i].hash_key, sizeof(hash_key_t));
     }
 
     if (cache_env.is_open) {
index 561986f8823df7a26faabfd0870ba04a7a5d30c5..a902a2edf2eb1af725324b92018345406b689258 100644 (file)
@@ -25,7 +25,7 @@
 #define BTA_GATTC_CO_H
 
 #include "bta/bta_gatt_api.h"
-#include "coap/hashkey.h"
+#include "osi/hash_functions.h"
 
 /*******************************************************************************
 **
@@ -117,13 +117,13 @@ extern BOOLEAN bta_gattc_co_addr_in_cache(BD_ADDR bda);
 
 extern uint8_t bta_gattc_co_find_addr_in_cache(BD_ADDR bda);
 
-extern uint8_t bta_gattc_co_find_hash_in_cache(coap_key_t hash_key);
+extern uint8_t bta_gattc_co_find_hash_in_cache(hash_key_t hash_key);
 
 extern UINT8 bta_gattc_co_get_addr_num(void);
 
 extern void bta_gattc_co_get_addr_list(BD_ADDR *addr_list);
 
-extern void bta_gattc_co_cache_addr_save(BD_ADDR bd_addr, coap_key_t hash_key);
+extern void bta_gattc_co_cache_addr_save(BD_ADDR bd_addr, hash_key_t hash_key);
 
 extern BOOLEAN bta_gattc_co_cache_new_assoc_list(BD_ADDR src_addr, uint8_t index);
 
index 78879421a7f14e2b0da90c54ad37c66454d3ae8e..9622f8d696b91c26ed47f886e2d210c06754aa91 100644 (file)
@@ -45,3 +45,20 @@ hash_index_t hash_function_string(const void *key)
     }
     return hash;
 }
+
+void hash_function_blob(const unsigned char *s, unsigned int len, hash_key_t h)
+{
+    size_t j;
+
+    while (len--) {
+        j = sizeof(hash_key_t)-1;
+
+        while (j) {
+            h[j] = ((h[j] << 7) | (h[j-1] >> 1)) + h[j];
+            --j;
+        }
+
+        h[0] = (h[0] << 7) + h[0] + *s++;
+    }
+}
+
index 3d0b254ea3af2e7751e5d799beaa9c291cb653bb..8102a0c14d3aed25d018fd8cfbfad26ecba598dd 100644 (file)
@@ -21,6 +21,8 @@
 
 #include "osi/hash_map.h"
 
+typedef unsigned char hash_key_t[4];
+
 hash_index_t hash_function_naive(const void *key);
 
 hash_index_t hash_function_integer(const void *key);
@@ -30,4 +32,6 @@ hash_index_t hash_function_pointer(const void *key);
 
 hash_index_t hash_function_string(const void *key);
 
+void hash_function_blob(const unsigned char *s, unsigned int len, hash_key_t h);
+
 #endif /* _HASH_FUNCTIONS_H_ */