1 /*
2  * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <string.h>
8 
9 #include "esp_gattc_api.h"
10 #include "esp_bt_main.h"
11 #include "btc/btc_manage.h"
12 #include "btc_gattc.h"
13 #include "btc_gatt_util.h"
14 #include "stack/l2cdefs.h"
15 #include "stack/l2c_api.h"
16 #include "gatt_int.h"
17 
18 
19 #if (GATTC_INCLUDED == TRUE)
esp_ble_gattc_register_callback(esp_gattc_cb_t callback)20 esp_err_t esp_ble_gattc_register_callback(esp_gattc_cb_t callback)
21 {
22     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
23 
24     if (callback == NULL) {
25         return ESP_FAIL;
26     }
27 
28     btc_profile_cb_set(BTC_PID_GATTC, callback);
29     return ESP_OK;
30 }
31 
esp_ble_gattc_app_register(uint16_t app_id)32 esp_err_t esp_ble_gattc_app_register(uint16_t app_id)
33 {
34     btc_msg_t msg = {0};
35     btc_ble_gattc_args_t arg;
36 
37     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
38 
39     if (app_id > ESP_APP_ID_MAX) {
40         return ESP_ERR_INVALID_ARG;
41     }
42 
43     msg.sig = BTC_SIG_API_CALL;
44     msg.pid = BTC_PID_GATTC;
45     msg.act = BTC_GATTC_ACT_APP_REGISTER;
46     arg.app_reg.app_id = app_id;
47 
48     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
49 }
50 
esp_ble_gattc_app_unregister(esp_gatt_if_t gattc_if)51 esp_err_t esp_ble_gattc_app_unregister(esp_gatt_if_t gattc_if)
52 {
53     btc_msg_t msg = {0};
54     btc_ble_gattc_args_t arg;
55 
56     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
57 
58     msg.sig = BTC_SIG_API_CALL;
59     msg.pid = BTC_PID_GATTC;
60     msg.act = BTC_GATTC_ACT_APP_UNREGISTER;
61     arg.app_unreg.gattc_if = gattc_if;
62 
63     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
64 }
65 #if (BLE_42_FEATURE_SUPPORT == TRUE)
esp_ble_gattc_open(esp_gatt_if_t gattc_if,esp_bd_addr_t remote_bda,esp_ble_addr_type_t remote_addr_type,bool is_direct)66 esp_err_t esp_ble_gattc_open(esp_gatt_if_t gattc_if, esp_bd_addr_t remote_bda, esp_ble_addr_type_t remote_addr_type, bool is_direct)
67 {
68     btc_msg_t msg = {0};
69     btc_ble_gattc_args_t arg;
70 
71     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
72 
73     msg.sig = BTC_SIG_API_CALL;
74     msg.pid = BTC_PID_GATTC;
75     msg.act = BTC_GATTC_ACT_OPEN;
76     arg.open.gattc_if = gattc_if;
77     memcpy(arg.open.remote_bda, remote_bda, ESP_BD_ADDR_LEN);
78     arg.open.remote_addr_type = remote_addr_type;
79     arg.open.is_direct = is_direct;
80     arg.open.is_aux = false;
81 
82     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
83 }
84 #endif // #if (BLE_42_FEATURE_SUPPORT == TRUE)
85 
86 #if (BLE_50_FEATURE_SUPPORT == TRUE)
esp_ble_gattc_aux_open(esp_gatt_if_t gattc_if,esp_bd_addr_t remote_bda,esp_ble_addr_type_t remote_addr_type,bool is_direct)87 esp_err_t esp_ble_gattc_aux_open(esp_gatt_if_t gattc_if, esp_bd_addr_t remote_bda, esp_ble_addr_type_t remote_addr_type, bool is_direct)
88 {
89     btc_msg_t msg;
90     btc_ble_gattc_args_t arg;
91 
92     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
93 
94     msg.sig = BTC_SIG_API_CALL;
95     msg.pid = BTC_PID_GATTC;
96     msg.act = BTC_GATTC_ACT_AUX_OPEN;
97     arg.open.gattc_if = gattc_if;
98     memcpy(arg.open.remote_bda, remote_bda, ESP_BD_ADDR_LEN);
99     arg.open.remote_addr_type = remote_addr_type;
100     arg.open.is_direct = is_direct;
101     arg.open.is_aux = true;
102 
103     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
104 
105 }
106 #endif // #if (BLE_50_FEATURE_SUPPORT == TRUE)
107 
esp_ble_gattc_close(esp_gatt_if_t gattc_if,uint16_t conn_id)108 esp_err_t esp_ble_gattc_close (esp_gatt_if_t gattc_if, uint16_t conn_id)
109 {
110     btc_msg_t msg = {0};
111     btc_ble_gattc_args_t arg;
112 
113     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
114 
115     msg.sig = BTC_SIG_API_CALL;
116     msg.pid = BTC_PID_GATTC;
117     msg.act = BTC_GATTC_ACT_CLOSE;
118     arg.close.conn_id = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
119 
120     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
121 }
122 
esp_ble_gattc_send_mtu_req(esp_gatt_if_t gattc_if,uint16_t conn_id)123 esp_err_t esp_ble_gattc_send_mtu_req (esp_gatt_if_t gattc_if, uint16_t conn_id)
124 {
125     btc_msg_t msg = {0};
126     btc_ble_gattc_args_t arg;
127 
128     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
129 
130     msg.sig = BTC_SIG_API_CALL;
131     msg.pid = BTC_PID_GATTC;
132     msg.act = BTC_GATTC_ACT_CFG_MTU;
133     arg.cfg_mtu.conn_id = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
134 
135     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
136 }
137 
esp_ble_gattc_search_service(esp_gatt_if_t gattc_if,uint16_t conn_id,esp_bt_uuid_t * filter_uuid)138 esp_err_t esp_ble_gattc_search_service(esp_gatt_if_t gattc_if, uint16_t conn_id, esp_bt_uuid_t *filter_uuid)
139 {
140     btc_msg_t msg = {0};
141     btc_ble_gattc_args_t arg;
142 
143     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
144 
145     msg.sig = BTC_SIG_API_CALL;
146     msg.pid = BTC_PID_GATTC;
147     msg.act = BTC_GATTC_ACT_SEARCH_SERVICE;
148     arg.search_srvc.conn_id = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
149 
150     if (filter_uuid) {
151         arg.search_srvc.filter_uuid_enable = true;
152         memcpy(&arg.search_srvc.filter_uuid, filter_uuid, sizeof(esp_bt_uuid_t));
153     } else {
154         arg.search_srvc.filter_uuid_enable = false;
155     }
156 
157     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
158 }
159 
esp_ble_gattc_get_service(esp_gatt_if_t gattc_if,uint16_t conn_id,esp_bt_uuid_t * svc_uuid,esp_gattc_service_elem_t * result,uint16_t * count,uint16_t offset)160 esp_gatt_status_t esp_ble_gattc_get_service(esp_gatt_if_t gattc_if, uint16_t conn_id, esp_bt_uuid_t *svc_uuid,
161                                             esp_gattc_service_elem_t *result, uint16_t *count, uint16_t offset)
162 {
163     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
164 
165     if (result == NULL || count == NULL || *count == 0) {
166         return ESP_GATT_INVALID_PDU;
167     }
168 
169     uint16_t conn_hdl = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
170     return btc_ble_gattc_get_service(conn_hdl, svc_uuid, result, count, offset);
171 }
172 
173 
esp_ble_gattc_get_all_char(esp_gatt_if_t gattc_if,uint16_t conn_id,uint16_t start_handle,uint16_t end_handle,esp_gattc_char_elem_t * result,uint16_t * count,uint16_t offset)174 esp_gatt_status_t esp_ble_gattc_get_all_char(esp_gatt_if_t gattc_if,
175                                              uint16_t conn_id,
176                                              uint16_t start_handle,
177                                              uint16_t end_handle,
178                                              esp_gattc_char_elem_t *result,
179                                              uint16_t *count, uint16_t offset)
180 {
181     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
182 
183     if ((start_handle == 0) && (end_handle == 0)) {
184         *count = 0;
185         return ESP_GATT_INVALID_HANDLE;
186     }
187 
188     if (result == NULL || count == NULL || *count == 0) {
189         return ESP_GATT_INVALID_PDU;
190     }
191 
192     uint16_t conn_hdl = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
193     return btc_ble_gattc_get_all_char(conn_hdl, start_handle, end_handle, result, count, offset);
194 }
195 
esp_ble_gattc_get_all_descr(esp_gatt_if_t gattc_if,uint16_t conn_id,uint16_t char_handle,esp_gattc_descr_elem_t * result,uint16_t * count,uint16_t offset)196 esp_gatt_status_t esp_ble_gattc_get_all_descr(esp_gatt_if_t gattc_if,
197                                               uint16_t conn_id,
198                                               uint16_t char_handle,
199                                               esp_gattc_descr_elem_t *result,
200                                               uint16_t *count, uint16_t offset)
201 {
202     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
203 
204     if (char_handle == 0) {
205         return ESP_GATT_INVALID_HANDLE;
206     }
207 
208     if (result == NULL || count == NULL || *count == 0) {
209         return ESP_GATT_INVALID_PDU;
210     }
211 
212     uint16_t conn_hdl = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
213     return btc_ble_gattc_get_all_descr(conn_hdl, char_handle, result, count, offset);
214 }
215 
esp_ble_gattc_get_char_by_uuid(esp_gatt_if_t gattc_if,uint16_t conn_id,uint16_t start_handle,uint16_t end_handle,esp_bt_uuid_t char_uuid,esp_gattc_char_elem_t * result,uint16_t * count)216 esp_gatt_status_t esp_ble_gattc_get_char_by_uuid(esp_gatt_if_t gattc_if,
217                                                  uint16_t conn_id,
218                                                  uint16_t start_handle,
219                                                  uint16_t end_handle,
220                                                  esp_bt_uuid_t char_uuid,
221                                                  esp_gattc_char_elem_t *result,
222                                                  uint16_t *count)
223 {
224     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
225 
226     if (start_handle == 0 && end_handle == 0) {
227         *count = 0;
228         return ESP_GATT_INVALID_HANDLE;
229     }
230 
231     if (result == NULL || count == NULL || *count == 0) {
232         return ESP_GATT_INVALID_PDU;
233     }
234 
235     uint16_t conn_hdl = BTC_GATT_CREATE_CONN_ID(gattc_if,conn_id);
236     return btc_ble_gattc_get_char_by_uuid(conn_hdl, start_handle, end_handle, char_uuid, result, count);
237 }
238 
239 
esp_ble_gattc_get_descr_by_uuid(esp_gatt_if_t gattc_if,uint16_t conn_id,uint16_t start_handle,uint16_t end_handle,esp_bt_uuid_t char_uuid,esp_bt_uuid_t descr_uuid,esp_gattc_descr_elem_t * result,uint16_t * count)240 esp_gatt_status_t esp_ble_gattc_get_descr_by_uuid(esp_gatt_if_t gattc_if,
241                                                   uint16_t conn_id,
242                                                   uint16_t start_handle,
243                                                   uint16_t end_handle,
244                                                   esp_bt_uuid_t char_uuid,
245                                                   esp_bt_uuid_t descr_uuid,
246                                                   esp_gattc_descr_elem_t *result,
247                                                   uint16_t *count)
248 {
249     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
250 
251     if (result == NULL || count == NULL || *count == 0) {
252         return ESP_GATT_INVALID_PDU;
253     }
254 
255     uint16_t conn_hdl = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
256     return btc_ble_gattc_get_descr_by_uuid(conn_hdl, start_handle, end_handle, char_uuid, descr_uuid, result, count);
257 }
258 
esp_ble_gattc_get_descr_by_char_handle(esp_gatt_if_t gattc_if,uint16_t conn_id,uint16_t char_handle,esp_bt_uuid_t descr_uuid,esp_gattc_descr_elem_t * result,uint16_t * count)259 esp_gatt_status_t esp_ble_gattc_get_descr_by_char_handle(esp_gatt_if_t gattc_if,
260                                                          uint16_t conn_id,
261                                                          uint16_t char_handle,
262                                                          esp_bt_uuid_t descr_uuid,
263                                                          esp_gattc_descr_elem_t *result,
264                                                          uint16_t *count)
265 {
266     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
267 
268     if (char_handle == 0) {
269         *count = 0;
270         return ESP_GATT_INVALID_HANDLE;
271     }
272 
273     if (result == NULL || count == NULL || *count == 0) {
274         return ESP_GATT_INVALID_PDU;
275     }
276 
277     uint16_t conn_hdl = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
278     return btc_ble_gattc_get_descr_by_char_handle(conn_hdl, char_handle, descr_uuid, result, count);
279 }
280 
esp_ble_gattc_get_include_service(esp_gatt_if_t gattc_if,uint16_t conn_id,uint16_t start_handle,uint16_t end_handle,esp_bt_uuid_t * incl_uuid,esp_gattc_incl_svc_elem_t * result,uint16_t * count)281 esp_gatt_status_t esp_ble_gattc_get_include_service(esp_gatt_if_t gattc_if,
282                                                     uint16_t conn_id,
283                                                     uint16_t start_handle,
284                                                     uint16_t end_handle,
285                                                     esp_bt_uuid_t *incl_uuid,
286                                                     esp_gattc_incl_svc_elem_t *result,
287                                                     uint16_t *count)
288 {
289     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
290 
291     if (start_handle == 0 && end_handle == 0) {
292         *count = 0;
293         return ESP_GATT_INVALID_HANDLE;
294     }
295 
296     if (result == NULL || count == NULL || *count == 0) {
297         return ESP_GATT_INVALID_PDU;
298     }
299 
300     uint16_t conn_hdl = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
301     return btc_ble_gattc_get_include_service(conn_hdl, start_handle, end_handle, incl_uuid, result, count);
302 }
303 
esp_ble_gattc_get_attr_count(esp_gatt_if_t gattc_if,uint16_t conn_id,esp_gatt_db_attr_type_t type,uint16_t start_handle,uint16_t end_handle,uint16_t char_handle,uint16_t * count)304 esp_gatt_status_t esp_ble_gattc_get_attr_count(esp_gatt_if_t gattc_if,
305                                                uint16_t conn_id,
306                                                esp_gatt_db_attr_type_t type,
307                                                uint16_t start_handle,
308                                                uint16_t end_handle,
309                                                uint16_t char_handle,
310                                                uint16_t *count)
311 {
312     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
313 
314     if ((start_handle == 0 && end_handle == 0) && (type != ESP_GATT_DB_DESCRIPTOR)) {
315         *count = 0;
316         return ESP_GATT_INVALID_HANDLE;
317     }
318 
319     if (count == NULL) {
320         return ESP_GATT_INVALID_PDU;
321     }
322 
323     uint16_t conn_hdl = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
324     return btc_ble_gattc_get_attr_count(conn_hdl, type, start_handle, end_handle, char_handle, count);
325 }
326 
esp_ble_gattc_get_db(esp_gatt_if_t gattc_if,uint16_t conn_id,uint16_t start_handle,uint16_t end_handle,esp_gattc_db_elem_t * db,uint16_t * count)327 esp_gatt_status_t esp_ble_gattc_get_db(esp_gatt_if_t gattc_if, uint16_t conn_id, uint16_t start_handle, uint16_t end_handle,
328                                        esp_gattc_db_elem_t *db, uint16_t *count)
329 {
330     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
331 
332     if (start_handle == 0 && end_handle == 0) {
333         *count = 0;
334         return ESP_GATT_INVALID_HANDLE;
335     }
336 
337     if (db == NULL || count == NULL || *count == 0) {
338         return ESP_GATT_INVALID_PDU;
339     }
340 
341     uint16_t conn_hdl = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
342     return btc_ble_gattc_get_db(conn_hdl, start_handle, end_handle, db, count);
343 }
344 
345 
esp_ble_gattc_read_char(esp_gatt_if_t gattc_if,uint16_t conn_id,uint16_t handle,esp_gatt_auth_req_t auth_req)346 esp_err_t esp_ble_gattc_read_char (esp_gatt_if_t gattc_if,
347                                    uint16_t conn_id, uint16_t handle,
348                                    esp_gatt_auth_req_t auth_req)
349 {
350     btc_msg_t msg = {0};
351     btc_ble_gattc_args_t arg;
352 
353     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
354     tGATT_TCB       *p_tcb = gatt_get_tcb_by_idx(conn_id);
355     if (!p_tcb) {
356         LOG_WARN("%s, The connection not created.", __func__);
357         return ESP_ERR_INVALID_STATE;
358     }
359 
360     if (L2CA_CheckIsCongest(L2CAP_ATT_CID, p_tcb->peer_bda)) {
361         LOG_DEBUG("%s, the l2cap chanel is congest.", __func__);
362         return ESP_FAIL;
363     }
364 
365     msg.sig = BTC_SIG_API_CALL;
366     msg.pid = BTC_PID_GATTC;
367     msg.act = BTC_GATTC_ACT_READ_CHAR;
368     arg.read_char.conn_id = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
369     arg.read_char.handle = handle;
370     arg.read_char.auth_req = auth_req;
371 
372     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
373 }
374 
esp_ble_gattc_read_by_type(esp_gatt_if_t gattc_if,uint16_t conn_id,uint16_t start_handle,uint16_t end_handle,esp_bt_uuid_t * uuid,esp_gatt_auth_req_t auth_req)375 esp_err_t esp_ble_gattc_read_by_type (esp_gatt_if_t gattc_if,
376                                       uint16_t conn_id,
377                                       uint16_t start_handle,
378                                       uint16_t end_handle,
379                                       esp_bt_uuid_t *uuid,
380                                       esp_gatt_auth_req_t auth_req)
381 {
382     btc_msg_t msg = {0};
383     btc_ble_gattc_args_t arg;
384 
385     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
386 
387     if (uuid == NULL) {
388         return ESP_GATT_ILLEGAL_PARAMETER;
389     }
390 
391     tGATT_TCB       *p_tcb = gatt_get_tcb_by_idx(conn_id);
392     if (!p_tcb) {
393         LOG_WARN("%s, The connection not created.", __func__);
394         return ESP_ERR_INVALID_STATE;
395     }
396 
397     if (L2CA_CheckIsCongest(L2CAP_ATT_CID, p_tcb->peer_bda)) {
398         LOG_DEBUG("%s, the l2cap chanel is congest.", __func__);
399         return ESP_FAIL;
400     }
401 
402     msg.sig = BTC_SIG_API_CALL;
403     msg.pid = BTC_PID_GATTC;
404     msg.act = BTC_GATTC_ACT_READ_BY_TYPE;
405     arg.read_by_type.conn_id = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
406     arg.read_by_type.s_handle = start_handle;
407     arg.read_by_type.e_handle = end_handle;
408     arg.read_by_type.auth_req = auth_req;
409     memcpy(&(arg.read_by_type.uuid), uuid, sizeof(esp_bt_uuid_t));
410 
411     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
412 }
413 
esp_ble_gattc_read_multiple(esp_gatt_if_t gattc_if,uint16_t conn_id,esp_gattc_multi_t * read_multi,esp_gatt_auth_req_t auth_req)414 esp_err_t esp_ble_gattc_read_multiple(esp_gatt_if_t gattc_if,
415                                       uint16_t conn_id, esp_gattc_multi_t *read_multi,
416                                       esp_gatt_auth_req_t auth_req)
417 {
418     btc_msg_t msg = {0};
419     btc_ble_gattc_args_t arg;
420 
421     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
422 
423     tGATT_TCB       *p_tcb = gatt_get_tcb_by_idx(conn_id);
424     if (!p_tcb) {
425         LOG_WARN("%s, The connection not created.", __func__);
426         return ESP_ERR_INVALID_STATE;
427     }
428 
429     if (L2CA_CheckIsCongest(L2CAP_ATT_CID, p_tcb->peer_bda)) {
430         LOG_DEBUG("%s, the l2cap chanel is congest.", __func__);
431         return ESP_FAIL;
432     }
433 
434     msg.sig = BTC_SIG_API_CALL;
435     msg.pid = BTC_PID_GATTC;
436     msg.act = BTC_GATTC_ACT_READ_MULTIPLE_CHAR;
437     arg.read_multiple.conn_id = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
438     arg.read_multiple.num_attr = read_multi->num_attr;
439     arg.read_multiple.auth_req = auth_req;
440 
441     if (read_multi->num_attr > 0) {
442         memcpy(arg.read_multiple.handles, read_multi->handles, sizeof(uint16_t)*read_multi->num_attr);
443     } else {
444         LOG_ERROR("%s(), the num_attr should not be 0.", __func__);
445         return ESP_FAIL;
446     }
447     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
448 }
449 
450 
esp_ble_gattc_read_char_descr(esp_gatt_if_t gattc_if,uint16_t conn_id,uint16_t handle,esp_gatt_auth_req_t auth_req)451 esp_err_t esp_ble_gattc_read_char_descr (esp_gatt_if_t gattc_if,
452                                          uint16_t conn_id, uint16_t handle,
453                                          esp_gatt_auth_req_t auth_req)
454 {
455     btc_msg_t msg = {0};
456     btc_ble_gattc_args_t arg;
457 
458     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
459 
460     tGATT_TCB       *p_tcb = gatt_get_tcb_by_idx(conn_id);
461     if (!p_tcb) {
462         LOG_WARN("%s, The connection not created.", __func__);
463         return ESP_ERR_INVALID_STATE;
464     }
465 
466     if (L2CA_CheckIsCongest(L2CAP_ATT_CID, p_tcb->peer_bda)) {
467         LOG_DEBUG("%s, the l2cap chanel is congest.", __func__);
468         return ESP_FAIL;
469     }
470 
471     msg.sig = BTC_SIG_API_CALL;
472     msg.pid = BTC_PID_GATTC;
473     msg.act = BTC_GATTC_ACT_READ_CHAR_DESCR;
474     arg.read_descr.conn_id = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
475     arg.read_descr.handle = handle;
476     arg.read_descr.auth_req = auth_req;
477 
478     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
479 }
480 
esp_ble_gattc_write_char(esp_gatt_if_t gattc_if,uint16_t conn_id,uint16_t handle,uint16_t value_len,uint8_t * value,esp_gatt_write_type_t write_type,esp_gatt_auth_req_t auth_req)481 esp_err_t esp_ble_gattc_write_char(esp_gatt_if_t gattc_if,
482                                    uint16_t conn_id, uint16_t handle,
483                                    uint16_t value_len,
484                                    uint8_t *value,
485                                    esp_gatt_write_type_t write_type,
486                                    esp_gatt_auth_req_t auth_req)
487 {
488     btc_msg_t msg = {0};
489     btc_ble_gattc_args_t arg;
490 
491     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
492 
493     tGATT_TCB       *p_tcb = gatt_get_tcb_by_idx(conn_id);
494     if (!p_tcb) {
495         LOG_WARN("%s, The connection not created.", __func__);
496         return ESP_ERR_INVALID_STATE;
497     }
498 
499     if (L2CA_CheckIsCongest(L2CAP_ATT_CID, p_tcb->peer_bda)) {
500         LOG_DEBUG("%s, the l2cap chanel is congest.", __func__);
501         return ESP_FAIL;
502     }
503 
504     msg.sig = BTC_SIG_API_CALL;
505     msg.pid = BTC_PID_GATTC;
506     msg.act = BTC_GATTC_ACT_WRITE_CHAR;
507     arg.write_char.conn_id = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
508     arg.write_char.handle = handle;
509     arg.write_char.value_len = value_len > ESP_GATT_MAX_ATTR_LEN ? ESP_GATT_MAX_ATTR_LEN : value_len;
510     arg.write_char.value = value;
511     arg.write_char.write_type = write_type;
512     arg.write_char.auth_req = auth_req;
513     if(write_type == ESP_GATT_WRITE_TYPE_NO_RSP){
514         l2ble_update_att_acl_pkt_num(L2CA_ADD_BTC_NUM, NULL);
515     }
516     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), btc_gattc_arg_deep_copy) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
517 }
518 
esp_ble_gattc_write_char_descr(esp_gatt_if_t gattc_if,uint16_t conn_id,uint16_t handle,uint16_t value_len,uint8_t * value,esp_gatt_write_type_t write_type,esp_gatt_auth_req_t auth_req)519 esp_err_t esp_ble_gattc_write_char_descr (esp_gatt_if_t gattc_if,
520                                           uint16_t conn_id, uint16_t handle,
521                                           uint16_t value_len,
522                                           uint8_t *value,
523                                           esp_gatt_write_type_t write_type,
524                                           esp_gatt_auth_req_t auth_req)
525 {
526     btc_msg_t msg = {0};
527     btc_ble_gattc_args_t arg;
528 
529     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
530 
531     tGATT_TCB       *p_tcb = gatt_get_tcb_by_idx(conn_id);
532     if (!p_tcb) {
533         LOG_WARN("%s, The connection not created.", __func__);
534         return ESP_ERR_INVALID_STATE;
535     }
536 
537     if (L2CA_CheckIsCongest(L2CAP_ATT_CID, p_tcb->peer_bda)) {
538         LOG_DEBUG("%s, the l2cap chanel is congest.", __func__);
539         return ESP_FAIL;
540     }
541 
542     msg.sig = BTC_SIG_API_CALL;
543     msg.pid = BTC_PID_GATTC;
544     msg.act = BTC_GATTC_ACT_WRITE_CHAR_DESCR;
545     arg.write_descr.conn_id = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
546     arg.write_descr.handle = handle;
547     arg.write_descr.value_len = value_len > ESP_GATT_MAX_ATTR_LEN ? ESP_GATT_MAX_ATTR_LEN : value_len;
548     arg.write_descr.value = value;
549     arg.write_descr.write_type = write_type;
550     arg.write_descr.auth_req = auth_req;
551     if(write_type == ESP_GATT_WRITE_TYPE_NO_RSP){
552         l2ble_update_att_acl_pkt_num(L2CA_ADD_BTC_NUM, NULL);
553     }
554     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), btc_gattc_arg_deep_copy) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
555 }
556 
esp_ble_gattc_prepare_write(esp_gatt_if_t gattc_if,uint16_t conn_id,uint16_t handle,uint16_t offset,uint16_t value_len,uint8_t * value,esp_gatt_auth_req_t auth_req)557 esp_err_t esp_ble_gattc_prepare_write(esp_gatt_if_t gattc_if,
558                                       uint16_t conn_id, uint16_t handle,
559                                       uint16_t offset,
560                                       uint16_t value_len,
561                                       uint8_t *value,
562                                       esp_gatt_auth_req_t auth_req)
563 {
564     btc_msg_t msg = {0};
565     btc_ble_gattc_args_t arg;
566 
567     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
568 
569     tGATT_TCB       *p_tcb = gatt_get_tcb_by_idx(conn_id);
570     if (!p_tcb) {
571         LOG_WARN("%s, The connection not created.", __func__);
572         return ESP_ERR_INVALID_STATE;
573     }
574 
575     if (L2CA_CheckIsCongest(L2CAP_ATT_CID, p_tcb->peer_bda)) {
576         LOG_DEBUG("%s, the l2cap chanel is congest.", __func__);
577         return ESP_FAIL;
578     }
579 
580     msg.sig = BTC_SIG_API_CALL;
581     msg.pid = BTC_PID_GATTC;
582     msg.act = BTC_GATTC_ACT_PREPARE_WRITE;
583     arg.prep_write.conn_id = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
584     arg.prep_write.handle = handle;
585     arg.prep_write.offset = offset;
586     arg.prep_write.value_len = value_len > ESP_GATT_MAX_ATTR_LEN ? ESP_GATT_MAX_ATTR_LEN : value_len; // length check ?
587     arg.prep_write.value = value;
588     arg.prep_write.auth_req = auth_req;
589 
590     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), btc_gattc_arg_deep_copy) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
591 }
592 
esp_ble_gattc_prepare_write_char_descr(esp_gatt_if_t gattc_if,uint16_t conn_id,uint16_t handle,uint16_t offset,uint16_t value_len,uint8_t * value,esp_gatt_auth_req_t auth_req)593 esp_err_t esp_ble_gattc_prepare_write_char_descr(esp_gatt_if_t gattc_if,
594                                                  uint16_t conn_id, uint16_t handle,
595                                                  uint16_t offset,
596                                                  uint16_t value_len,
597                                                  uint8_t *value,
598                                                  esp_gatt_auth_req_t auth_req)
599 {
600     btc_msg_t msg = {0};
601     btc_ble_gattc_args_t arg;
602 
603     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
604 
605     tGATT_TCB       *p_tcb = gatt_get_tcb_by_idx(conn_id);
606     if (!p_tcb) {
607         LOG_WARN("%s, The connection not created.", __func__);
608         return ESP_ERR_INVALID_STATE;
609     }
610 
611     if (L2CA_CheckIsCongest(L2CAP_ATT_CID, p_tcb->peer_bda)) {
612         LOG_DEBUG("%s, the l2cap chanel is congest.", __func__);
613         return ESP_FAIL;
614     }
615 
616     msg.sig = BTC_SIG_API_CALL;
617     msg.pid = BTC_PID_GATTC;
618     msg.act = BTC_GATTC_ACT_PREPARE_WRITE_CHAR_DESCR;
619     arg.prep_write_descr.conn_id = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
620     arg.prep_write_descr.handle = handle;
621     arg.prep_write_descr.offset = offset;
622     arg.prep_write_descr.value_len = value_len > ESP_GATT_MAX_ATTR_LEN ? ESP_GATT_MAX_ATTR_LEN : value_len; // length check ?
623     arg.prep_write_descr.value = value;
624     arg.prep_write_descr.auth_req = auth_req;
625 
626     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), btc_gattc_arg_deep_copy) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
627 }
628 
esp_ble_gattc_execute_write(esp_gatt_if_t gattc_if,uint16_t conn_id,bool is_execute)629 esp_err_t esp_ble_gattc_execute_write (esp_gatt_if_t gattc_if, uint16_t conn_id, bool is_execute)
630 {
631     btc_msg_t msg = {0};
632     btc_ble_gattc_args_t arg;
633 
634     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
635 
636     msg.sig = BTC_SIG_API_CALL;
637     msg.pid = BTC_PID_GATTC;
638     msg.act = BTC_GATTC_ACT_EXECUTE_WRITE;
639     arg.exec_write.conn_id = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
640     arg.exec_write.is_execute = is_execute;
641 
642     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
643 }
644 
esp_ble_gattc_register_for_notify(esp_gatt_if_t gattc_if,esp_bd_addr_t server_bda,uint16_t handle)645 esp_err_t esp_ble_gattc_register_for_notify (esp_gatt_if_t gattc_if,
646                                              esp_bd_addr_t server_bda, uint16_t handle)
647 {
648     btc_msg_t msg = {0};
649     btc_ble_gattc_args_t arg;
650 
651     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
652 
653     msg.sig = BTC_SIG_API_CALL;
654     msg.pid = BTC_PID_GATTC;
655     msg.act = BTC_GATTC_ACT_REG_FOR_NOTIFY;
656     arg.reg_for_notify.gattc_if = gattc_if;
657     memcpy(arg.reg_for_notify.remote_bda, server_bda, sizeof(esp_bd_addr_t));
658     arg.reg_for_notify.handle = handle;
659 
660     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
661 }
662 
esp_ble_gattc_unregister_for_notify(esp_gatt_if_t gattc_if,esp_bd_addr_t server_bda,uint16_t handle)663 esp_err_t esp_ble_gattc_unregister_for_notify (esp_gatt_if_t gattc_if,
664                                                esp_bd_addr_t server_bda, uint16_t handle)
665 {
666     btc_msg_t msg = {0};
667     btc_ble_gattc_args_t arg;
668 
669     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
670 
671     msg.sig = BTC_SIG_API_CALL;
672     msg.pid = BTC_PID_GATTC;
673     msg.act = BTC_GATTC_ACT_UNREG_FOR_NOTIFY;
674     arg.unreg_for_notify.gattc_if = gattc_if;
675     arg.unreg_for_notify.handle = handle;
676     memcpy(arg.unreg_for_notify.remote_bda, server_bda, sizeof(esp_bd_addr_t));
677     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
678 }
679 
esp_ble_gattc_cache_refresh(esp_bd_addr_t remote_bda)680 esp_err_t esp_ble_gattc_cache_refresh(esp_bd_addr_t remote_bda)
681 {
682     btc_msg_t msg = {0};
683     btc_ble_gattc_args_t arg;
684 
685     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
686 
687     msg.sig = BTC_SIG_API_CALL;
688     msg.pid = BTC_PID_GATTC;
689     msg.act = BTC_GATTC_ACT_CACHE_REFRESH;
690     memcpy(arg.cache_refresh.remote_bda, remote_bda, sizeof(esp_bd_addr_t));
691 
692     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
693 }
694 
esp_ble_gattc_cache_clean(esp_bd_addr_t remote_bda)695 esp_err_t esp_ble_gattc_cache_clean(esp_bd_addr_t remote_bda)
696 {
697     btc_msg_t msg = {0};
698     btc_ble_gattc_args_t arg;
699 
700     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
701 
702     msg.sig = BTC_SIG_API_CALL;
703     msg.pid = BTC_PID_GATTC;
704     msg.act = BTC_GATTC_ACT_CACHE_CLEAN;
705     memcpy(arg.cache_clean.remote_bda, remote_bda, sizeof(esp_bd_addr_t));
706 
707     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
708 }
709 
esp_ble_gattc_cache_assoc(esp_gatt_if_t gattc_if,esp_bd_addr_t src_addr,esp_bd_addr_t assoc_addr,bool is_assoc)710 esp_err_t esp_ble_gattc_cache_assoc(esp_gatt_if_t gattc_if, esp_bd_addr_t src_addr, esp_bd_addr_t assoc_addr, bool is_assoc)
711 {
712     btc_msg_t msg = {0};
713     btc_ble_gattc_args_t arg;
714 
715     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
716 
717     msg.sig = BTC_SIG_API_CALL;
718     msg.pid = BTC_PID_GATTC;
719     msg.act = BTC_GATTC_ACT_CACHE_ASSOC;
720     arg.cache_assoc.is_assoc = is_assoc;
721     arg.cache_assoc.gattc_if = gattc_if;
722     memcpy(arg.cache_assoc.src_addr, src_addr, sizeof(esp_bd_addr_t));
723     memcpy(arg.cache_assoc.assoc_addr, assoc_addr, sizeof(esp_bd_addr_t));
724 
725     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
726 }
727 
esp_ble_gattc_cache_get_addr_list(esp_gatt_if_t gattc_if)728 esp_err_t esp_ble_gattc_cache_get_addr_list(esp_gatt_if_t gattc_if)
729 {
730     btc_msg_t msg = {0};
731     btc_ble_gattc_args_t arg;
732 
733     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
734 
735     msg.sig = BTC_SIG_API_CALL;
736     msg.pid = BTC_PID_GATTC;
737     msg.act = BTC_GATTC_ATC_CACHE_GET_ADDR_LIST;
738     arg.get_addr_list.gattc_if = gattc_if;
739     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
740 }
741 
742 #endif  ///GATTC_INCLUDED == TRUE
743