From e7020460c622b502a6a74a79cbdf421903e3d8df Mon Sep 17 00:00:00 2001 From: zhiweijian Date: Thu, 26 Oct 2017 14:39:49 +0800 Subject: [PATCH] Component/bt: add value callback when send notification or indicate failed --- .../bt/bluedroid/api/include/esp_gatts_api.h | 2 ++ components/bt/bluedroid/bta/gatt/bta_gatts_act.c | 9 +++++++++ components/bt/bluedroid/bta/include/bta_gatt_api.h | 2 ++ .../bt/bluedroid/btc/profile/std/gatt/btc_gatts.c | 11 +++++++++++ components/bt/bluedroid/stack/gatt/gatt_api.c | 5 ++++- examples/bluetooth/gatt_client/main/gattc_demo.c | 6 +++++- examples/bluetooth/gatt_server/main/gatts_demo.c | 14 ++++++++++++-- 7 files changed, 45 insertions(+), 4 deletions(-) diff --git a/components/bt/bluedroid/api/include/esp_gatts_api.h b/components/bt/bluedroid/api/include/esp_gatts_api.h index d964ac9b9..07869ba05 100644 --- a/components/bt/bluedroid/api/include/esp_gatts_api.h +++ b/components/bt/bluedroid/api/include/esp_gatts_api.h @@ -119,6 +119,8 @@ typedef union { struct gatts_conf_evt_param { esp_gatt_status_t status; /*!< Operation status */ uint16_t conn_id; /*!< Connection id */ + uint16_t len; /*!< The indication or notification value length, len is valid when send notification or indication failed */ + uint8_t *value; /*!< The indication or notification value , value is valid when send notification or indication failed */ } conf; /*!< Gatt server callback param of ESP_GATTS_CONF_EVT (confirm) */ /** diff --git a/components/bt/bluedroid/bta/gatt/bta_gatts_act.c b/components/bt/bluedroid/bta/gatt/bta_gatts_act.c index c617fef82..4c1380a3c 100644 --- a/components/bt/bluedroid/bta/gatt/bta_gatts_act.c +++ b/components/bt/bluedroid/bta/gatt/bta_gatts_act.c @@ -692,6 +692,15 @@ void bta_gatts_indicate_handle (tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA *p_msg) cb_data.req_data.status = status; cb_data.req_data.conn_id = p_msg->api_indicate.hdr.layer_specific; + cb_data.req_data.value =(uint8_t *)osi_malloc(p_msg->api_indicate.len); + if (cb_data.req_data.value != NULL){ + memset(cb_data.req_data.value, 0, p_msg->api_indicate.len); + cb_data.req_data.data_len = p_msg->api_indicate.len; + memcpy(cb_data.req_data.value, p_msg->api_indicate.value, p_msg->api_indicate.len); + }else{ + cb_data.req_data.data_len = 0; + APPL_TRACE_ERROR("%s, malloc failed", __func__); + } (*p_rcb->p_cback)(BTA_GATTS_CONF_EVT, &cb_data); } } else { diff --git a/components/bt/bluedroid/bta/include/bta_gatt_api.h b/components/bt/bluedroid/bta/include/bta_gatt_api.h index 35fb42d71..9342e55c4 100644 --- a/components/bt/bluedroid/bta/include/bta_gatt_api.h +++ b/components/bt/bluedroid/bta/include/bta_gatt_api.h @@ -523,6 +523,8 @@ typedef struct { UINT32 trans_id; UINT16 conn_id; tBTA_GATTS_REQ_DATA *p_data; + UINT16 data_len; + UINT8 *value; } tBTA_GATTS_REQ; typedef struct { diff --git a/components/bt/bluedroid/btc/profile/std/gatt/btc_gatts.c b/components/bt/bluedroid/btc/profile/std/gatt/btc_gatts.c index df2d74212..635e4a740 100644 --- a/components/bt/bluedroid/btc/profile/std/gatt/btc_gatts.c +++ b/components/bt/bluedroid/btc/profile/std/gatt/btc_gatts.c @@ -527,6 +527,11 @@ static void btc_gatts_cb_param_copy_free(btc_msg_t *msg, tBTA_GATTS *p_data) osi_free(p_data->req_data.p_data); } break; + case BTA_GATTS_CONF_EVT: + if (p_data && p_data->req_data.value){ + osi_free(p_data->req_data.value); + } + break; default: break; } @@ -787,6 +792,12 @@ void btc_gatts_cb_handler(btc_msg_t *msg) param.conf.conn_id = BTC_GATT_GET_CONN_ID(p_data->req_data.conn_id); param.conf.status = p_data->req_data.status; + if (p_data->req_data.status != ESP_GATT_OK && p_data->req_data.value){ + param.conf.len = p_data->req_data.data_len; + param.conf.value = p_data->req_data.value; + }else{ + param.conf.len = 0; + } btc_gatts_cb_to_app(ESP_GATTS_CONF_EVT, gatts_if, ¶m); break; case BTA_GATTS_CREATE_EVT: diff --git a/components/bt/bluedroid/stack/gatt/gatt_api.c b/components/bt/bluedroid/stack/gatt/gatt_api.c index 6737183fd..44b8527f9 100644 --- a/components/bt/bluedroid/stack/gatt/gatt_api.c +++ b/components/bt/bluedroid/stack/gatt/gatt_api.c @@ -567,7 +567,6 @@ tGATT_STATUS GATTS_HandleValueIndication (UINT16 conn_id, UINT16 attr_handle, U tGATT_VALUE indication; BT_HDR *p_msg; - tGATT_VALUE *p_buf; tGATT_IF gatt_if = GATT_GET_GATT_IF(conn_id); UINT8 tcb_idx = GATT_GET_TCB_IDX(conn_id); tGATT_REG *p_reg = gatt_get_regcb(gatt_if); @@ -591,12 +590,16 @@ tGATT_STATUS GATTS_HandleValueIndication (UINT16 conn_id, UINT16 attr_handle, U indication.auth_req = GATT_AUTH_REQ_NONE; if (GATT_HANDLE_IS_VALID(p_tcb->indicate_handle)) { + /* TODO: need to further check whether deleting pending queue here cause reducing transport performance */ + /* GATT_TRACE_DEBUG ("Add a pending indication"); if ((p_buf = gatt_add_pending_ind(p_tcb, &indication)) != NULL) { cmd_status = GATT_SUCCESS; } else { cmd_status = GATT_NO_RESOURCES; } + */ + return GATT_BUSY; } else { if ( (p_msg = attp_build_sr_msg (p_tcb, GATT_HANDLE_VALUE_IND, (tGATT_SR_MSG *)&indication)) != NULL) { diff --git a/examples/bluetooth/gatt_client/main/gattc_demo.c b/examples/bluetooth/gatt_client/main/gattc_demo.c index 14290c2d8..1215b5d70 100644 --- a/examples/bluetooth/gatt_client/main/gattc_demo.c +++ b/examples/bluetooth/gatt_client/main/gattc_demo.c @@ -257,7 +257,11 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_ break; } case ESP_GATTC_NOTIFY_EVT: - ESP_LOGI(GATTC_TAG, "ESP_GATTC_NOTIFY_EVT, receive notify value:"); + if (p_data->notify.is_notify){ + ESP_LOGI(GATTC_TAG, "ESP_GATTC_NOTIFY_EVT, receive notify value:"); + }else{ + ESP_LOGI(GATTC_TAG, "ESP_GATTC_NOTIFY_EVT, receive indicate value:"); + } esp_log_buffer_hex(GATTC_TAG, p_data->notify.value, p_data->notify.value_len); break; case ESP_GATTC_WRITE_DESCR_EVT: diff --git a/examples/bluetooth/gatt_server/main/gatts_demo.c b/examples/bluetooth/gatt_server/main/gatts_demo.c index a60d567c8..5902ee7bf 100644 --- a/examples/bluetooth/gatt_server/main/gatts_demo.c +++ b/examples/bluetooth/gatt_server/main/gatts_demo.c @@ -411,7 +411,6 @@ static void gatts_profile_a_event_handler(esp_gatts_cb_event_t event, esp_gatt_i case ESP_GATTS_MTU_EVT: ESP_LOGI(GATTS_TAG, "ESP_GATTS_MTU_EVT, MTU %d", param->mtu.mtu); break; - case ESP_GATTS_CONF_EVT: case ESP_GATTS_UNREG_EVT: break; case ESP_GATTS_CREATE_EVT: @@ -492,6 +491,12 @@ static void gatts_profile_a_event_handler(esp_gatts_cb_event_t event, esp_gatt_i ESP_LOGI(GATTS_TAG, "ESP_GATTS_DISCONNECT_EVT"); esp_ble_gap_start_advertising(&adv_params); break; + case ESP_GATTS_CONF_EVT: + ESP_LOGI(GATTS_TAG, "ESP_GATTS_CONF_EVT, status %d", param->conf.status); + if (param->conf.status != ESP_GATT_OK){ + esp_log_buffer_hex(GATTS_TAG, param->conf.value, param->conf.len); + } + break; case ESP_GATTS_OPEN_EVT: case ESP_GATTS_CANCEL_OPEN_EVT: case ESP_GATTS_CLOSE_EVT: @@ -578,7 +583,6 @@ static void gatts_profile_b_event_handler(esp_gatts_cb_event_t event, esp_gatt_i case ESP_GATTS_MTU_EVT: ESP_LOGI(GATTS_TAG, "ESP_GATTS_MTU_EVT, MTU %d", param->mtu.mtu); break; - case ESP_GATTS_CONF_EVT: case ESP_GATTS_UNREG_EVT: break; case ESP_GATTS_CREATE_EVT: @@ -631,6 +635,12 @@ static void gatts_profile_b_event_handler(esp_gatts_cb_event_t event, esp_gatt_i param->connect.is_connected); gl_profile_tab[PROFILE_B_APP_ID].conn_id = param->connect.conn_id; break; + case ESP_GATTS_CONF_EVT: + ESP_LOGI(GATTS_TAG, "ESP_GATTS_CONF_EVT status %d", param->conf.status); + if (param->conf.status != ESP_GATT_OK){ + esp_log_buffer_hex(GATTS_TAG, param->conf.value, param->conf.len); + } + break; case ESP_GATTS_DISCONNECT_EVT: case ESP_GATTS_OPEN_EVT: case ESP_GATTS_CANCEL_OPEN_EVT: