]> granicus.if.org Git - esp-idf/commitdiff
Component/bt:add API to prepare write descriptor for gattc
authorzhiweijian <zhiweijian@espressif.com>
Fri, 28 Apr 2017 06:51:23 +0000 (14:51 +0800)
committerzhiweijian <zhiweijian@espressif.com>
Fri, 5 May 2017 10:08:47 +0000 (18:08 +0800)
components/bt/bluedroid/api/esp_gattc_api.c
components/bt/bluedroid/api/include/esp_gattc_api.h
components/bt/bluedroid/bta/gatt/bta_gattc_api.c
components/bt/bluedroid/bta/include/bta_gatt_api.h
components/bt/bluedroid/btc/profile/std/gatt/btc_gattc.c
components/bt/bluedroid/btc/profile/std/include/btc_gattc.h
docs/api-reference/bluetooth/esp_gattc.rst

index bfe7db4654da8e49fdc94a89df01b656cb890dba..bb368a68de1a549738821f6e4f1a96cd34aa6cca 100644 (file)
@@ -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;
index b52dabbdaca364b03ac3dcb4c691e7dabdf68de2..ae53bf57510ed53e88fd25190df86c591000898a 100644 (file)
@@ -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.
  *
index beaa235dedbcc8b6c6bbc9d546789053a9e734ec..8d25f51da213869316c21937b022cccb0952231a 100644 (file)
@@ -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;
+
 }
 /*******************************************************************************
 **
index f4c0e06f0789dd0ad42b141c1bf257930a69dac8..c4e9ace658676733c36895cfdb7a71ff5fdb7359 100644 (file)
@@ -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.
index 5a7a5b2f6703a3ba89f1de1dc7f6e056575fd0f0..4d126ff6c342843552f3e27f3995e05d4bf41e67 100644 (file)
@@ -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;
index aef84189570adbf880b7c7dd20ebb3c38a8c1f15..2528668381c00ce7f09d2837d587c91b70bfa980 100644 (file)
@@ -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;
index b41c7e09e368b2aa6658d29510e506b4c3053adb..755e7dc71426b79123c3611dbc3c334589965ed7 100644 (file)
@@ -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