From: Yulong Date: Thu, 26 Apr 2018 13:44:12 +0000 (-0400) Subject: component/bt: Added the hash key generated directly in the bluedroid module. X-Git-Tag: v3.1-beta1~204^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=6c20bc320343cfd04af8be070332853b85707145;p=esp-idf component/bt: Added the hash key generated directly in the bluedroid module. 1. Change all the hash used from the coap module to generated by the bluedroid module directly. component/bt: Added the include file to the gattc_co module to avoid compile error. --- diff --git a/components/bt/bluedroid/bta/gatt/bta_gattc_co.c b/components/bt/bluedroid/bta/gatt/bta_gattc_co.c index b3d73b6a5e..e6b49cb381 100644 --- a/components/bt/bluedroid/bta/gatt/bta_gattc_co.c +++ b/components/bt/bluedroid/bta/gatt/bta_gattc_co.c @@ -16,12 +16,13 @@ * ******************************************************************************/ #ifdef BT_SUPPORT_NVM -#include #include #endif /* BT_SUPPORT_NVM */ - +#include +#include #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) { diff --git a/components/bt/bluedroid/bta/include/bta/bta_gattc_co.h b/components/bt/bluedroid/bta/include/bta/bta_gattc_co.h index 561986f882..a902a2edf2 100644 --- a/components/bt/bluedroid/bta/include/bta/bta_gattc_co.h +++ b/components/bt/bluedroid/bta/include/bta/bta_gattc_co.h @@ -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); diff --git a/components/bt/bluedroid/osi/hash_functions.c b/components/bt/bluedroid/osi/hash_functions.c index 78879421a7..9622f8d696 100644 --- a/components/bt/bluedroid/osi/hash_functions.c +++ b/components/bt/bluedroid/osi/hash_functions.c @@ -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++; + } +} + diff --git a/components/bt/bluedroid/osi/include/osi/hash_functions.h b/components/bt/bluedroid/osi/include/osi/hash_functions.h index 3d0b254ea3..8102a0c14d 100644 --- a/components/bt/bluedroid/osi/include/osi/hash_functions.h +++ b/components/bt/bluedroid/osi/include/osi/hash_functions.h @@ -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_ */