From 970502f904f7efb03c90d39a948292869a3e1270 Mon Sep 17 00:00:00 2001 From: zhiweijian Date: Fri, 13 Jul 2018 20:58:19 +0800 Subject: [PATCH] Component/bt: fix gattc get count invalid --- components/bt/bluedroid/api/esp_gattc_api.c | 6 ++++++ .../bluedroid/api/include/api/esp_gatts_api.h | 3 ++- .../bt/bluedroid/bta/gatt/bta_gattc_cache.c | 19 +++++++------------ .../bta/gatt/include/bta_gattc_int.h | 2 +- .../btc/profile/std/gatt/btc_gattc.c | 10 +++++++++- 5 files changed, 25 insertions(+), 15 deletions(-) diff --git a/components/bt/bluedroid/api/esp_gattc_api.c b/components/bt/bluedroid/api/esp_gattc_api.c index fc431d6fb..578916e5f 100644 --- a/components/bt/bluedroid/api/esp_gattc_api.c +++ b/components/bt/bluedroid/api/esp_gattc_api.c @@ -164,6 +164,7 @@ esp_gatt_status_t esp_ble_gattc_get_all_char(esp_gatt_if_t gattc_if, ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED); if ((start_handle == 0) && (end_handle == 0)) { + *count = 0; return ESP_GATT_INVALID_HANDLE; } @@ -206,6 +207,7 @@ esp_gatt_status_t esp_ble_gattc_get_char_by_uuid(esp_gatt_if_t gattc_if, ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED); if (start_handle == 0 && end_handle == 0) { + *count = 0; return ESP_GATT_INVALID_HANDLE; } @@ -247,6 +249,7 @@ esp_gatt_status_t esp_ble_gattc_get_descr_by_char_handle(esp_gatt_if_t gattc_if, ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED); if (char_handle == 0) { + *count = 0; return ESP_GATT_INVALID_HANDLE; } @@ -269,6 +272,7 @@ esp_gatt_status_t esp_ble_gattc_get_include_service(esp_gatt_if_t gattc_if, ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED); if (start_handle == 0 && end_handle == 0) { + *count = 0; return ESP_GATT_INVALID_HANDLE; } @@ -291,6 +295,7 @@ esp_gatt_status_t esp_ble_gattc_get_attr_count(esp_gatt_if_t gattc_if, ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED); if ((start_handle == 0 && end_handle == 0) && (type != ESP_GATT_DB_DESCRIPTOR)) { + *count = 0; return ESP_GATT_INVALID_HANDLE; } @@ -308,6 +313,7 @@ esp_gatt_status_t esp_ble_gattc_get_db(esp_gatt_if_t gattc_if, uint16_t conn_id, ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED); if (start_handle == 0 && end_handle == 0) { + *count = 0; return ESP_GATT_INVALID_HANDLE; } diff --git a/components/bt/bluedroid/api/include/api/esp_gatts_api.h b/components/bt/bluedroid/api/include/api/esp_gatts_api.h index bc97b76e4..d25d29780 100644 --- a/components/bt/bluedroid/api/include/api/esp_gatts_api.h +++ b/components/bt/bluedroid/api/include/api/esp_gatts_api.h @@ -350,7 +350,8 @@ esp_err_t esp_ble_gatts_create_attr_tab(const esp_gatts_attr_db_t *gatts_attr_db uint8_t max_nb_attr, uint8_t srvc_inst_id); /** - * @brief This function is called to add an included service. After included + * @brief This function is called to add an included service. This function have to be called between + * 'esp_ble_gatts_create_service' and 'esp_ble_gatts_add_char'. After included * service is included, a callback event BTA_GATTS_ADD_INCL_SRVC_EVT * is reported the included service ID. * diff --git a/components/bt/bluedroid/bta/gatt/bta_gattc_cache.c b/components/bt/bluedroid/bta/gatt/bta_gattc_cache.c index 7c8ec58c6..90a9d1350 100644 --- a/components/bt/bluedroid/bta/gatt/bta_gattc_cache.c +++ b/components/bt/bluedroid/bta/gatt/bta_gattc_cache.c @@ -376,14 +376,8 @@ static tBTA_GATT_STATUS bta_gattc_add_attr_to_cache(tBTA_GATTC_SERV *p_srvc_cb, isvc->included_service = bta_gattc_find_matching_service( p_srvc_cb->p_srvc_cache, incl_srvc_s_handle); if (!isvc->included_service) { - // if it is a secondary service, wait to update later - if(property == 0){ - p_srvc_cb->update_sec_sev = true; - } else { - APPL_TRACE_ERROR("%s: Illegal action to add non-existing included service!", __func__); - osi_free(isvc); - return GATT_WRONG_STATE; - } + // if can't find included service, wait to update later + p_srvc_cb->update_incl_srvc = true; } list_append(service->included_svc, isvc); @@ -606,10 +600,10 @@ static void bta_gattc_explore_srvc(UINT16 conn_id, tBTA_GATTC_SERV *p_srvc_cb) return; } } - //update include service when have secondary service - if(p_srvc_cb->update_sec_sev) { + // if update_incl_srvc is true, update include service + if(p_srvc_cb->update_incl_srvc) { bta_gattc_update_include_service(p_srvc_cb->p_srvc_cache); - p_srvc_cb->update_sec_sev = false; + p_srvc_cb->update_incl_srvc = false; } /* no service found at all, the end of server discovery*/ APPL_TRACE_DEBUG("%s no more services found", __func__); @@ -1658,7 +1652,8 @@ void bta_gattc_get_db_size_handle(UINT16 conn_id, UINT16 start_handle, UINT16 en tBTA_GATTC_CLCB *p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id); if (p_clcb == NULL) { - return NULL; + *count = 0; + return; } tBTA_GATTC_SERV *p_srcb = p_clcb->p_srcb; diff --git a/components/bt/bluedroid/bta/gatt/include/bta_gattc_int.h b/components/bt/bluedroid/bta/gatt/include/bta_gattc_int.h index 2b550d908..68887f089 100644 --- a/components/bt/bluedroid/bta/gatt/include/bta_gattc_int.h +++ b/components/bt/bluedroid/bta/gatt/include/bta_gattc_int.h @@ -300,7 +300,7 @@ typedef struct { UINT16 attr_index; /* cahce NV saving/loading attribute index */ UINT16 mtu; - bool update_sec_sev; + bool update_incl_srvc; } tBTA_GATTC_SERV; #ifndef BTA_GATTC_NOTIF_REG_MAX diff --git a/components/bt/bluedroid/btc/profile/std/gatt/btc_gattc.c b/components/bt/bluedroid/btc/profile/std/gatt/btc_gattc.c index d48cd0b40..bb7d6cbbd 100644 --- a/components/bt/bluedroid/btc/profile/std/gatt/btc_gattc.c +++ b/components/bt/bluedroid/btc/profile/std/gatt/btc_gattc.c @@ -331,6 +331,7 @@ esp_gatt_status_t btc_ble_gattc_get_service(uint16_t conn_id, esp_bt_uuid_t *svc if (bta_uuid) { osi_free(bta_uuid); } + *count = 0; return status; } else { btc_gattc_fill_gatt_db_conversion(*count, (uint16_t)svc_num, ESP_GATT_DB_PRIMARY_SERVICE, offset, (void *)result, db); @@ -362,6 +363,7 @@ esp_gatt_status_t btc_ble_gattc_get_all_char(uint16_t conn_id, if (db) { osi_free(db); } + *count = 0; return status; } else { btc_gattc_fill_gatt_db_conversion(*count, (uint16_t)char_num, ESP_GATT_DB_CHARACTERISTIC, offset, (void *)result, db); @@ -389,6 +391,7 @@ esp_gatt_status_t btc_ble_gattc_get_all_descr(uint16_t conn_id, if (db) { osi_free(db); } + *count = 0; return status; } else { btc_gattc_fill_gatt_db_conversion(*count, (uint16_t)descr_num, ESP_GATT_DB_DESCRIPTOR, offset, (void *)result, db); @@ -420,6 +423,7 @@ esp_gatt_status_t btc_ble_gattc_get_char_by_uuid(uint16_t conn_id, if (db) { osi_free(db); } + *count = 0; return status; } else { btc_gattc_fill_gatt_db_conversion(*count, (uint16_t)char_num, ESP_GATT_DB_CHARACTERISTIC, 0, (void *)result, db); @@ -456,6 +460,7 @@ esp_gatt_status_t btc_ble_gattc_get_descr_by_uuid(uint16_t conn_id, if (db) { osi_free(db); } + *count = 0; return status; } else { btc_gattc_fill_gatt_db_conversion(*count, (uint16_t)descr_num, ESP_GATT_DB_DESCRIPTOR, 0, (void *)result, db); @@ -487,6 +492,7 @@ esp_gatt_status_t btc_ble_gattc_get_descr_by_char_handle(uint16_t conn_id, if (db) { osi_free(db); } + *count = 0; return status; } else { btc_gattc_fill_gatt_db_conversion(*count, (uint16_t)descr_num, ESP_GATT_DB_DESCRIPTOR, 0, (void *)result, db); @@ -524,6 +530,7 @@ esp_gatt_status_t btc_ble_gattc_get_include_service(uint16_t conn_id, if (db) { osi_free(db); } + *count = 0; return status; }else { btc_gattc_fill_gatt_db_conversion(*count, (uint16_t)incl_num, ESP_GATT_DB_INCLUDED_SERVICE, 0, (void *)result, db); @@ -566,6 +573,7 @@ esp_gatt_status_t btc_ble_gattc_get_db(uint16_t conn_id, uint16_t start_handle, if (get_db) { osi_free(get_db); } + *count = 0; return ESP_GATT_NOT_FOUND; } @@ -579,7 +587,7 @@ esp_gatt_status_t btc_ble_gattc_get_db(uint16_t conn_id, uint16_t start_handle, btc128_to_bta_uuid(&bta_uuid, get_db[i].uuid.uu); bta_to_btc_uuid(&db[i].uuid, &bta_uuid); } - *count = num; + *count = db_size; //don't forget to free the db buffer after used. if (get_db) { osi_free(get_db);