From 71401bc9b349ab0aef21f861b099238a9fcaccb4 Mon Sep 17 00:00:00 2001 From: zhiweijian Date: Fri, 28 Apr 2017 14:51:23 +0800 Subject: [PATCH] Component/bt:add API to prepare write descriptor for gattc --- components/bt/bluedroid/api/esp_gattc_api.c | 55 +++++++++++++++---- .../bt/bluedroid/api/include/esp_gattc_api.h | 47 +++++++++++++--- .../bt/bluedroid/bta/gatt/bta_gattc_api.c | 52 ++++++++++++++++++ .../bt/bluedroid/bta/include/bta_gatt_api.h | 22 +++++++- .../btc/profile/std/gatt/btc_gattc.c | 34 ++++++++++++ .../btc/profile/std/include/btc_gattc.h | 12 ++++ docs/api-reference/bluetooth/esp_gattc.rst | 1 + 7 files changed, 201 insertions(+), 22 deletions(-) diff --git a/components/bt/bluedroid/api/esp_gattc_api.c b/components/bt/bluedroid/api/esp_gattc_api.c index bfe7db465..bb368a68d 100644 --- a/components/bt/bluedroid/api/esp_gattc_api.c +++ b/components/bt/bluedroid/api/esp_gattc_api.c @@ -324,15 +324,15 @@ esp_err_t esp_ble_gattc_write_char( esp_gatt_if_t gattc_if, return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), btc_gattc_arg_deep_copy) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } -esp_err_t esp_ble_gattc_write_char_descr (esp_gatt_if_t gattc_if, - uint16_t conn_id, - esp_gatt_srvc_id_t *srvc_id, - esp_gatt_id_t *char_id, - esp_gatt_id_t *descr_id, - uint16_t value_len, - uint8_t *value, - esp_gatt_write_type_t write_type, - esp_gatt_auth_req_t auth_req) +esp_err_t esp_ble_gattc_write_char_descr (esp_gatt_if_t gattc_if, + uint16_t conn_id, + esp_gatt_srvc_id_t *srvc_id, + esp_gatt_id_t *char_id, + esp_gatt_id_t *descr_id, + uint16_t value_len, + uint8_t *value, + esp_gatt_write_type_t write_type, + esp_gatt_auth_req_t auth_req) { btc_msg_t msg; btc_ble_gattc_args_t arg; @@ -340,7 +340,7 @@ esp_err_t esp_ble_gattc_write_char_descr (esp_gatt_if_t gattc_if, if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) { return ESP_ERR_INVALID_STATE; } - + msg.sig = BTC_SIG_API_CALL; msg.pid = BTC_PID_GATTC; msg.act = BTC_GATTC_ACT_WRITE_CHAR_DESCR; @@ -365,14 +365,13 @@ esp_err_t esp_ble_gattc_prepare_write(esp_gatt_if_t gattc_if, uint8_t *value, esp_gatt_auth_req_t auth_req) { - //TODO: Review this function btc_msg_t msg; btc_ble_gattc_args_t arg; if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) { return ESP_ERR_INVALID_STATE; } - + msg.sig = BTC_SIG_API_CALL; msg.pid = BTC_PID_GATTC; msg.act = BTC_GATTC_ACT_PREPARE_WRITE; @@ -387,6 +386,38 @@ esp_err_t esp_ble_gattc_prepare_write(esp_gatt_if_t gattc_if, return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), btc_gattc_arg_deep_copy) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } +esp_err_t esp_ble_gattc_prepare_write_char_descr(esp_gatt_if_t gattc_if, + uint16_t conn_id, + esp_gatt_srvc_id_t *srvc_id, + esp_gatt_id_t *char_id, + esp_gatt_id_t *descr_id, + uint16_t offset, + uint16_t value_len, + uint8_t *value, + esp_gatt_auth_req_t auth_req) +{ + btc_msg_t msg; + btc_ble_gattc_args_t arg; + + if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) { + return ESP_ERR_INVALID_STATE; + } + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_GATTC; + msg.act = BTC_GATTC_ACT_PREPARE_WRITE_CHAR_DESCR; + arg.prep_write_descr.conn_id = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id); + memcpy(&arg.prep_write_descr.service_id, srvc_id, sizeof(esp_gatt_srvc_id_t)); + memcpy(&arg.prep_write_descr.char_id, char_id, sizeof(esp_gatt_id_t)); + memcpy(&arg.prep_write_descr.descr_id, descr_id, sizeof(esp_gatt_id_t)); + arg.prep_write_descr.offset = offset; + arg.prep_write_descr.value_len = value_len > ESP_GATT_MAX_ATTR_LEN ? ESP_GATT_MAX_ATTR_LEN : value_len; // length check ? + arg.prep_write_descr.value = value; + arg.prep_write_descr.auth_req = auth_req; + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), btc_gattc_arg_deep_copy) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + esp_err_t esp_ble_gattc_execute_write (esp_gatt_if_t gattc_if, uint16_t conn_id, bool is_execute) { btc_msg_t msg; diff --git a/components/bt/bluedroid/api/include/esp_gattc_api.h b/components/bt/bluedroid/api/include/esp_gattc_api.h index b52dabbda..ae53bf575 100644 --- a/components/bt/bluedroid/api/include/esp_gattc_api.h +++ b/components/bt/bluedroid/api/include/esp_gattc_api.h @@ -510,15 +510,15 @@ esp_err_t esp_ble_gattc_write_char( esp_gatt_if_t gattc_if, * - other: failed * */ -esp_err_t esp_ble_gattc_write_char_descr (esp_gatt_if_t gattc_if, - uint16_t conn_id, - esp_gatt_srvc_id_t *srvc_id, - esp_gatt_id_t *char_id, - esp_gatt_id_t *descr_id, - uint16_t value_len, - uint8_t *value, - esp_gatt_write_type_t write_type, - esp_gatt_auth_req_t auth_req); +esp_err_t esp_ble_gattc_write_char_descr (esp_gatt_if_t gattc_if, + uint16_t conn_id, + esp_gatt_srvc_id_t *srvc_id, + esp_gatt_id_t *char_id, + esp_gatt_id_t *descr_id, + uint16_t value_len, + uint8_t *value, + esp_gatt_write_type_t write_type, + esp_gatt_auth_req_t auth_req); /** @@ -548,6 +548,35 @@ esp_err_t esp_ble_gattc_prepare_write(esp_gatt_if_t gattc_if, esp_gatt_auth_req_t auth_req); +/** + * @brief This function is called to prepare write a characteristic descriptor value. + * + * @param[in] gattc_if: Gatt client access interface. + * @param[in] conn_id : connection ID. + * @param[in] srvc_id : service ID. + * @param[in] char_id : GATT characteristic ID of the service. + * @param[in] descr_id : characteristic descriptor ID to write. + * @param[in] offset : offset of the write value. + * @param[in] value_len: length of the value to be written. + * @param[in] value : the value to be written. + * @param[in] auth_req : authentication request. + * + * @return + * - ESP_OK: success + * - other: failed + * + */ +esp_err_t esp_ble_gattc_prepare_write_char_descr(esp_gatt_if_t gattc_if, + uint16_t conn_id, + esp_gatt_srvc_id_t *srvc_id, + esp_gatt_id_t *char_id, + esp_gatt_id_t *descr_id, + uint16_t offset, + uint16_t value_len, + uint8_t *value, + esp_gatt_auth_req_t auth_req); + + /** * @brief This function is called to execute write a prepare write sequence. * diff --git a/components/bt/bluedroid/bta/gatt/bta_gattc_api.c b/components/bt/bluedroid/bta/gatt/bta_gattc_api.c index beaa235de..8d25f51da 100644 --- a/components/bt/bluedroid/bta/gatt/bta_gattc_api.c +++ b/components/bt/bluedroid/bta/gatt/bta_gattc_api.c @@ -792,6 +792,58 @@ void BTA_GATTC_PrepareWrite (UINT16 conn_id, tBTA_GATTC_CHAR_ID *p_char_id, } return; +} +/******************************************************************************* +** +** Function BTA_GATTC_PrepareWriteCharDescr +** +** Description This function is called to prepare write a characteristic descriptor value. +** +** Parameters conn_id - connection ID. +** p_char_descr_id - GATT characteritic descriptor ID of the service. +** offset - offset of the write value. +** len: length of the data to be written. +** p_value - the value to be written. +** +** Returns None +** +*******************************************************************************/ +void BTA_GATTC_PrepareWriteCharDescr (UINT16 conn_id, tBTA_GATTC_CHAR_DESCR_ID *p_char_descr_id, + UINT16 offset,tBTA_GATT_UNFMT *p_data, + tBTA_GATT_AUTH_REQ auth_req) +{ + tBTA_GATTC_API_WRITE *p_buf; + UINT16 len = sizeof(tBTA_GATTC_API_WRITE) + sizeof(tBTA_GATT_ID); + + if (p_data != NULL) { + len += p_data->len; + } + + if ((p_buf = (tBTA_GATTC_API_WRITE *) GKI_getbuf(len)) != NULL) { + memset(p_buf, 0, len); + + p_buf->hdr.event = BTA_GATTC_API_WRITE_EVT; + p_buf->hdr.layer_specific = conn_id; + p_buf->auth_req = auth_req; + + memcpy(&p_buf->srvc_id, &p_char_descr_id->char_id.srvc_id, sizeof(tBTA_GATT_SRVC_ID)); + memcpy(&p_buf->char_id, &p_char_descr_id->char_id.char_id, sizeof(tBTA_GATT_ID)); + p_buf->p_descr_type = (tBTA_GATT_ID *)(p_buf + 1); + memcpy(p_buf->p_descr_type, &p_char_descr_id->descr_id, sizeof(tBTA_GATT_ID)); + p_buf->write_type = BTA_GATTC_WRITE_PREPARE; + p_buf->offset = offset; + + if (p_data && p_data->len != 0) { + p_buf->p_value = (UINT8 *)(p_buf->p_descr_type + 1); + p_buf->len = p_data->len; + /* pack the descr data */ + memcpy(p_buf->p_value, p_data->p_value, p_data->len); + } + + bta_sys_sendmsg(p_buf); + } + return; + } /******************************************************************************* ** diff --git a/components/bt/bluedroid/bta/include/bta_gatt_api.h b/components/bt/bluedroid/bta/include/bta_gatt_api.h index f4c0e06f0..c4e9ace65 100644 --- a/components/bt/bluedroid/bta/include/bta_gatt_api.h +++ b/components/bt/bluedroid/bta/include/bta_gatt_api.h @@ -984,7 +984,7 @@ extern tBTA_GATT_STATUS BTA_GATTC_DeregisterForNotifications (tBTA_GATTC_IF ** Description This function is called to prepare write a characteristic value. ** ** Parameters conn_id - connection ID. -** p_char_id - GATT characteritic ID of the service. +** p_char_id - GATT characteritic ID of the service. ** offset - offset of the write value. ** len: length of the data to be written. ** p_value - the value to be written. @@ -1001,6 +1001,26 @@ extern void BTA_GATTC_PrepareWrite (UINT16 conn_id, /******************************************************************************* ** +** Function BTA_GATTC_PrepareWriteCharDescr +** +** Description This function is called to prepare write a characteristic descriptor value. +** +** Parameters conn_id - connection ID. +** p_char_descr_id - GATT characteritic descriptor ID of the service. +** offset - offset of the write value. +** len: length of the data to be written. +** p_value - the value to be written. +** +** Returns None +** +*******************************************************************************/ +extern void BTA_GATTC_PrepareWriteCharDescr (UINT16 conn_id, + tBTA_GATTC_CHAR_DESCR_ID *p_char_descr_id, + UINT16 offset, + tBTA_GATT_UNFMT *p_data, + tBTA_GATT_AUTH_REQ auth_req); +/******************************************************************************* +** ** Function BTA_GATTC_ExecuteWrite ** ** Description This function is called to execute write a prepare write sequence. 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 5a7a5b2f6..4d126ff6c 100644 --- a/components/bt/bluedroid/btc/profile/std/gatt/btc_gattc.c +++ b/components/bt/bluedroid/btc/profile/std/gatt/btc_gattc.c @@ -64,6 +64,15 @@ void btc_gattc_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src) } break; } + case BTC_GATTC_ACT_PREPARE_WRITE_CHAR_DESCR: { + dst->prep_write_descr.value = (uint8_t *)GKI_getbuf(src->prep_write_descr.value_len); + if (dst->prep_write_descr.value) { + memcpy(dst->prep_write_descr.value, src->prep_write_descr.value, src->prep_write_descr.value_len); + } else { + LOG_ERROR("%s %d no mem\n", __func__, msg->act); + } + break; + } default: LOG_DEBUG("%s Unhandled deep copy %d\n", __func__, msg->act); break; @@ -93,6 +102,12 @@ void btc_gattc_arg_deep_free(btc_msg_t *msg) } break; } + case BTC_GATTC_ACT_PREPARE_WRITE_CHAR_DESCR: { + if (arg->prep_write_descr.value) { + GKI_freebuf(arg->prep_write_descr.value); + } + break; + } default: LOG_DEBUG("%s Unhandled deep free %d\n", __func__, msg->act); break; @@ -407,6 +422,22 @@ static void btc_gattc_prepare_write(btc_ble_gattc_args_t *arg) arg->prep_write.value, arg->prep_write.auth_req); } +static void btc_gattc_prepare_write_char_descr(btc_ble_gattc_args_t *arg) +{ + tBTA_GATTC_CHAR_DESCR_ID in_char_descr_id; + tBTA_GATT_UNFMT descr_val; + btc_to_bta_srvc_id(&in_char_descr_id.char_id.srvc_id, &arg->prep_write_descr.service_id); + btc_to_bta_gatt_id(&in_char_descr_id.char_id.char_id, &arg->prep_write_descr.char_id); + btc_to_bta_gatt_id(&in_char_descr_id.descr_id, &arg->prep_write_descr.descr_id); + + descr_val.len = arg->prep_write_descr.value_len; + descr_val.p_value = arg->prep_write_descr.value; + BTA_GATTC_PrepareWriteCharDescr(arg->prep_write_descr.conn_id, + &in_char_descr_id, + arg->prep_write_descr.offset, + &descr_val, + arg->prep_write_descr.auth_req); +} static void btc_gattc_execute_wrtie(btc_ble_gattc_args_t *arg) { @@ -508,6 +539,9 @@ void btc_gattc_call_handler(btc_msg_t *msg) case BTC_GATTC_ACT_PREPARE_WRITE: btc_gattc_prepare_write(arg); break; + case BTC_GATTC_ACT_PREPARE_WRITE_CHAR_DESCR: + btc_gattc_prepare_write_char_descr(arg); + break; case BTC_GATTC_ACT_EXECUTE_WRITE: btc_gattc_execute_wrtie(arg); break; diff --git a/components/bt/bluedroid/btc/profile/std/include/btc_gattc.h b/components/bt/bluedroid/btc/profile/std/include/btc_gattc.h index aef841895..252866838 100644 --- a/components/bt/bluedroid/btc/profile/std/include/btc_gattc.h +++ b/components/bt/bluedroid/btc/profile/std/include/btc_gattc.h @@ -38,6 +38,7 @@ typedef enum { BTC_GATTC_ACT_WRITE_CHAR, BTC_GATTC_ACT_WRITE_CHAR_DESCR, BTC_GATTC_ACT_PREPARE_WRITE, + BTC_GATTC_ACT_PREPARE_WRITE_CHAR_DESCR, BTC_GATTC_ACT_EXECUTE_WRITE, BTC_GATTC_ACT_REG_FOR_NOTIFY, BTC_GATTC_ACT_UNREG_FOR_NOTIFY @@ -155,6 +156,17 @@ typedef union { uint8_t *value; esp_gatt_auth_req_t auth_req; } prep_write; + //BTC_GATTC_ACT_PREPARE_WRITE_CHAR_DESCR, + struct prep_write_descr_arg { + uint16_t conn_id; + esp_gatt_srvc_id_t service_id; + esp_gatt_id_t char_id; + esp_gatt_id_t descr_id; + uint16_t offset; + uint16_t value_len; + uint8_t *value; + esp_gatt_auth_req_t auth_req; + } prep_write_descr; //BTC_GATTC_ACT_EXECUTE_WRITE, struct exec_write_arg { uint16_t conn_id; diff --git a/docs/api-reference/bluetooth/esp_gattc.rst b/docs/api-reference/bluetooth/esp_gattc.rst index b41c7e09e..755e7dc71 100644 --- a/docs/api-reference/bluetooth/esp_gattc.rst +++ b/docs/api-reference/bluetooth/esp_gattc.rst @@ -118,6 +118,7 @@ Functions .. doxygenfunction:: esp_ble_gattc_write_char .. doxygenfunction:: esp_ble_gattc_write_char_descr .. doxygenfunction:: esp_ble_gattc_prepare_write +.. doxygenfunction:: esp_ble_gattc_prepare_write_char_descr .. doxygenfunction:: esp_ble_gattc_execute_write .. doxygenfunction:: esp_ble_gattc_register_for_notify .. doxygenfunction:: esp_ble_gattc_unregister_for_notify