1 /*
2  * SPDX-FileCopyrightText: 2015-2024 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_get_callback(void)32 esp_gattc_cb_t esp_ble_gattc_get_callback(void)
33 {
34     return (esp_gattc_cb_t) btc_profile_cb_get(BTC_PID_GATTC);
35 }
36 
esp_ble_gattc_app_register(uint16_t app_id)37 esp_err_t esp_ble_gattc_app_register(uint16_t app_id)
38 {
39     btc_msg_t msg = {0};
40     btc_ble_gattc_args_t arg;
41 
42     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
43 
44     if (app_id > ESP_APP_ID_MAX) {
45         return ESP_ERR_INVALID_ARG;
46     }
47 
48     msg.sig = BTC_SIG_API_CALL;
49     msg.pid = BTC_PID_GATTC;
50     msg.act = BTC_GATTC_ACT_APP_REGISTER;
51     arg.app_reg.app_id = app_id;
52 
53     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
54 }
55 
esp_ble_gattc_app_unregister(esp_gatt_if_t gattc_if)56 esp_err_t esp_ble_gattc_app_unregister(esp_gatt_if_t gattc_if)
57 {
58     btc_msg_t msg = {0};
59     btc_ble_gattc_args_t arg;
60 
61     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
62 
63     msg.sig = BTC_SIG_API_CALL;
64     msg.pid = BTC_PID_GATTC;
65     msg.act = BTC_GATTC_ACT_APP_UNREGISTER;
66     arg.app_unreg.gattc_if = gattc_if;
67 
68     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
69 }
70 
esp_ble_gattc_enh_open(esp_gatt_if_t gattc_if,esp_ble_gatt_creat_conn_params_t * creat_conn_params)71 esp_err_t esp_ble_gattc_enh_open(esp_gatt_if_t gattc_if, esp_ble_gatt_creat_conn_params_t *creat_conn_params)
72 {
73     btc_msg_t msg = {0};
74     btc_ble_gattc_args_t arg;
75     const esp_ble_conn_params_t *conn_params;
76 
77     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
78 
79     if (!creat_conn_params) {
80         return ESP_ERR_INVALID_ARG;
81     }
82 
83     msg.sig = BTC_SIG_API_CALL;
84     msg.pid = BTC_PID_GATTC;
85     msg.act = BTC_GATTC_ACT_OPEN;
86     arg.open.gattc_if = gattc_if;
87     memcpy(arg.open.remote_bda, creat_conn_params->remote_bda, ESP_BD_ADDR_LEN);
88     arg.open.remote_addr_type = creat_conn_params->remote_addr_type;
89     arg.open.is_direct = creat_conn_params->is_direct;
90     arg.open.is_aux= creat_conn_params->is_aux;
91     arg.open.own_addr_type = creat_conn_params->own_addr_type;
92     arg.open.phy_mask = creat_conn_params->phy_mask;
93 
94     // If not aux open, shouldn't set 2M and coded PHY connection params
95     if (!creat_conn_params->is_aux &&
96         ((creat_conn_params->phy_mask & ESP_BLE_PHY_2M_PREF_MASK) ||
97         (creat_conn_params->phy_mask & ESP_BLE_PHY_CODED_PREF_MASK))) {
98         return ESP_ERR_INVALID_ARG;
99     }
100 
101     if (creat_conn_params->phy_mask & ESP_BLE_PHY_1M_PREF_MASK) {
102         if (!creat_conn_params->phy_1m_conn_params) {
103             return ESP_ERR_INVALID_ARG;
104         }
105 
106         conn_params = creat_conn_params->phy_1m_conn_params;
107         if (ESP_BLE_IS_VALID_PARAM(conn_params->interval_min, ESP_BLE_CONN_INT_MIN, ESP_BLE_CONN_INT_MAX) &&
108             ESP_BLE_IS_VALID_PARAM(conn_params->interval_max, ESP_BLE_CONN_INT_MIN, ESP_BLE_CONN_INT_MAX) &&
109             ESP_BLE_IS_VALID_PARAM(conn_params->supervision_timeout, ESP_BLE_CONN_SUP_TOUT_MIN, ESP_BLE_CONN_SUP_TOUT_MAX) &&
110             (conn_params->latency <= ESP_BLE_CONN_LATENCY_MAX) &&
111             ((conn_params->supervision_timeout * 10) >= ((1 + conn_params->latency) * ((conn_params->interval_max * 5) >> 1))) &&
112             (conn_params->interval_min <= conn_params->interval_max)) {
113             memcpy(&arg.open.phy_1m_conn_params, conn_params, sizeof(esp_ble_conn_params_t));
114         } else {
115             LOG_ERROR("%s, invalid 1M PHY connection params: min_int = %d, max_int = %d, latency = %d, timeout = %d", __func__,
116                                     conn_params->interval_min,
117                                     conn_params->interval_max,
118                                     conn_params->latency,
119                                     conn_params->supervision_timeout);
120             return ESP_ERR_INVALID_ARG;
121         }
122     }
123 
124     if (creat_conn_params->phy_mask & ESP_BLE_PHY_2M_PREF_MASK) {
125         if (!creat_conn_params->phy_2m_conn_params) {
126             return ESP_ERR_INVALID_ARG;
127         }
128 
129         conn_params = creat_conn_params->phy_2m_conn_params;
130         if (ESP_BLE_IS_VALID_PARAM(conn_params->interval_min, ESP_BLE_CONN_INT_MIN, ESP_BLE_CONN_INT_MAX) &&
131             ESP_BLE_IS_VALID_PARAM(conn_params->interval_max, ESP_BLE_CONN_INT_MIN, ESP_BLE_CONN_INT_MAX) &&
132             ESP_BLE_IS_VALID_PARAM(conn_params->supervision_timeout, ESP_BLE_CONN_SUP_TOUT_MIN, ESP_BLE_CONN_SUP_TOUT_MAX) &&
133             (conn_params->latency <= ESP_BLE_CONN_LATENCY_MAX) &&
134             ((conn_params->supervision_timeout * 10) >= ((1 + conn_params->latency) * ((conn_params->interval_max * 5) >> 1))) &&
135             (conn_params->interval_min <= conn_params->interval_max)) {
136             memcpy(&arg.open.phy_2m_conn_params, conn_params, sizeof(esp_ble_conn_params_t));
137         } else {
138             LOG_ERROR("%s, invalid 2M PHY connection params: min_int = %d, max_int = %d, latency = %d, timeout = %d", __func__,
139                                     conn_params->interval_min,
140                                     conn_params->interval_max,
141                                     conn_params->latency,
142                                     conn_params->supervision_timeout);
143             return ESP_ERR_INVALID_ARG;
144         }
145     }
146 
147     if (creat_conn_params->phy_mask & ESP_BLE_PHY_CODED_PREF_MASK) {
148         if (!creat_conn_params->phy_coded_conn_params) {
149             return ESP_ERR_INVALID_ARG;
150         }
151 
152         conn_params = creat_conn_params->phy_coded_conn_params;
153         if (ESP_BLE_IS_VALID_PARAM(conn_params->interval_min, ESP_BLE_CONN_INT_MIN, ESP_BLE_CONN_INT_MAX) &&
154             ESP_BLE_IS_VALID_PARAM(conn_params->interval_max, ESP_BLE_CONN_INT_MIN, ESP_BLE_CONN_INT_MAX) &&
155             ESP_BLE_IS_VALID_PARAM(conn_params->supervision_timeout, ESP_BLE_CONN_SUP_TOUT_MIN, ESP_BLE_CONN_SUP_TOUT_MAX) &&
156             (conn_params->latency <= ESP_BLE_CONN_LATENCY_MAX) &&
157             ((conn_params->supervision_timeout * 10) >= ((1 + conn_params->latency) * ((conn_params->interval_max * 5) >> 1))) &&
158             (conn_params->interval_min <= conn_params->interval_max)) {
159             memcpy(&arg.open.phy_coded_conn_params, conn_params, sizeof(esp_ble_conn_params_t));
160         } else {
161             LOG_ERROR("%s, invalid Coded PHY connection params: min_int = %d, max_int = %d, latency = %d, timeout = %d", __func__,
162                                     conn_params->interval_min,
163                                     conn_params->interval_max,
164                                     conn_params->latency,
165                                     conn_params->supervision_timeout);
166             return ESP_ERR_INVALID_ARG;
167         }
168     }
169 
170     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
171 }
172 
173 #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)174 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)
175 {
176     esp_ble_gatt_creat_conn_params_t creat_conn_params = {0};
177     memcpy(creat_conn_params.remote_bda, remote_bda, ESP_BD_ADDR_LEN);
178     creat_conn_params.remote_addr_type = remote_addr_type;
179     creat_conn_params.is_direct = is_direct;
180     creat_conn_params.is_aux = false;
181     creat_conn_params.own_addr_type = 0xff; //undefined, will use local value
182     creat_conn_params.phy_mask = 0x0;
183     return esp_ble_gattc_enh_open(gattc_if, &creat_conn_params);
184 }
185 #endif // #if (BLE_42_FEATURE_SUPPORT == TRUE)
186 
187 #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)188 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)
189 {
190     esp_ble_gatt_creat_conn_params_t creat_conn_params = {0};
191     memcpy(creat_conn_params.remote_bda, remote_bda, ESP_BD_ADDR_LEN);
192     creat_conn_params.remote_addr_type = remote_addr_type;
193     creat_conn_params.is_direct = is_direct;
194     creat_conn_params.is_aux = true;
195     creat_conn_params.own_addr_type = 0xff; //undefined, will use local value
196     creat_conn_params.phy_mask = 0x0;
197     return esp_ble_gattc_enh_open(gattc_if, &creat_conn_params);
198 }
199 #endif // #if (BLE_50_FEATURE_SUPPORT == TRUE)
200 
esp_ble_gattc_close(esp_gatt_if_t gattc_if,uint16_t conn_id)201 esp_err_t esp_ble_gattc_close (esp_gatt_if_t gattc_if, uint16_t conn_id)
202 {
203     btc_msg_t msg = {0};
204     btc_ble_gattc_args_t arg;
205 
206     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
207 
208     msg.sig = BTC_SIG_API_CALL;
209     msg.pid = BTC_PID_GATTC;
210     msg.act = BTC_GATTC_ACT_CLOSE;
211     arg.close.conn_id = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
212 
213     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
214 }
215 
esp_ble_gattc_send_mtu_req(esp_gatt_if_t gattc_if,uint16_t conn_id)216 esp_err_t esp_ble_gattc_send_mtu_req (esp_gatt_if_t gattc_if, uint16_t conn_id)
217 {
218     btc_msg_t msg = {0};
219     btc_ble_gattc_args_t arg;
220 
221     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
222 
223     msg.sig = BTC_SIG_API_CALL;
224     msg.pid = BTC_PID_GATTC;
225     msg.act = BTC_GATTC_ACT_CFG_MTU;
226     arg.cfg_mtu.conn_id = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
227 
228     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
229 }
230 
esp_ble_gattc_search_service(esp_gatt_if_t gattc_if,uint16_t conn_id,esp_bt_uuid_t * filter_uuid)231 esp_err_t esp_ble_gattc_search_service(esp_gatt_if_t gattc_if, uint16_t conn_id, esp_bt_uuid_t *filter_uuid)
232 {
233     btc_msg_t msg = {0};
234     btc_ble_gattc_args_t arg;
235 
236     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
237 
238     msg.sig = BTC_SIG_API_CALL;
239     msg.pid = BTC_PID_GATTC;
240     msg.act = BTC_GATTC_ACT_SEARCH_SERVICE;
241     arg.search_srvc.conn_id = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
242 
243     if (filter_uuid) {
244         arg.search_srvc.filter_uuid_enable = true;
245         memcpy(&arg.search_srvc.filter_uuid, filter_uuid, sizeof(esp_bt_uuid_t));
246     } else {
247         arg.search_srvc.filter_uuid_enable = false;
248     }
249 
250     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
251 }
252 
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)253 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,
254                                             esp_gattc_service_elem_t *result, uint16_t *count, uint16_t offset)
255 {
256     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
257 
258     if (result == NULL || count == NULL || *count == 0) {
259         return ESP_GATT_INVALID_PDU;
260     }
261 
262     uint16_t conn_hdl = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
263     return btc_ble_gattc_get_service(conn_hdl, svc_uuid, result, count, offset);
264 }
265 
266 
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)267 esp_gatt_status_t esp_ble_gattc_get_all_char(esp_gatt_if_t gattc_if,
268                                              uint16_t conn_id,
269                                              uint16_t start_handle,
270                                              uint16_t end_handle,
271                                              esp_gattc_char_elem_t *result,
272                                              uint16_t *count, uint16_t offset)
273 {
274     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
275 
276     if ((start_handle == 0) && (end_handle == 0)) {
277         *count = 0;
278         return ESP_GATT_INVALID_HANDLE;
279     }
280 
281     if (result == NULL || count == NULL || *count == 0) {
282         return ESP_GATT_INVALID_PDU;
283     }
284 
285     uint16_t conn_hdl = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
286     return btc_ble_gattc_get_all_char(conn_hdl, start_handle, end_handle, result, count, offset);
287 }
288 
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)289 esp_gatt_status_t esp_ble_gattc_get_all_descr(esp_gatt_if_t gattc_if,
290                                               uint16_t conn_id,
291                                               uint16_t char_handle,
292                                               esp_gattc_descr_elem_t *result,
293                                               uint16_t *count, uint16_t offset)
294 {
295     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
296 
297     if (char_handle == 0) {
298         return ESP_GATT_INVALID_HANDLE;
299     }
300 
301     if (result == NULL || count == NULL || *count == 0) {
302         return ESP_GATT_INVALID_PDU;
303     }
304 
305     uint16_t conn_hdl = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
306     return btc_ble_gattc_get_all_descr(conn_hdl, char_handle, result, count, offset);
307 }
308 
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)309 esp_gatt_status_t esp_ble_gattc_get_char_by_uuid(esp_gatt_if_t gattc_if,
310                                                  uint16_t conn_id,
311                                                  uint16_t start_handle,
312                                                  uint16_t end_handle,
313                                                  esp_bt_uuid_t char_uuid,
314                                                  esp_gattc_char_elem_t *result,
315                                                  uint16_t *count)
316 {
317     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
318 
319     if (start_handle == 0 && end_handle == 0) {
320         *count = 0;
321         return ESP_GATT_INVALID_HANDLE;
322     }
323 
324     if (result == NULL || count == NULL || *count == 0) {
325         return ESP_GATT_INVALID_PDU;
326     }
327 
328     uint16_t conn_hdl = BTC_GATT_CREATE_CONN_ID(gattc_if,conn_id);
329     return btc_ble_gattc_get_char_by_uuid(conn_hdl, start_handle, end_handle, char_uuid, result, count);
330 }
331 
332 
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)333 esp_gatt_status_t esp_ble_gattc_get_descr_by_uuid(esp_gatt_if_t gattc_if,
334                                                   uint16_t conn_id,
335                                                   uint16_t start_handle,
336                                                   uint16_t end_handle,
337                                                   esp_bt_uuid_t char_uuid,
338                                                   esp_bt_uuid_t descr_uuid,
339                                                   esp_gattc_descr_elem_t *result,
340                                                   uint16_t *count)
341 {
342     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
343 
344     if (result == NULL || count == NULL || *count == 0) {
345         return ESP_GATT_INVALID_PDU;
346     }
347 
348     uint16_t conn_hdl = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
349     return btc_ble_gattc_get_descr_by_uuid(conn_hdl, start_handle, end_handle, char_uuid, descr_uuid, result, count);
350 }
351 
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)352 esp_gatt_status_t esp_ble_gattc_get_descr_by_char_handle(esp_gatt_if_t gattc_if,
353                                                          uint16_t conn_id,
354                                                          uint16_t char_handle,
355                                                          esp_bt_uuid_t descr_uuid,
356                                                          esp_gattc_descr_elem_t *result,
357                                                          uint16_t *count)
358 {
359     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
360 
361     if (char_handle == 0) {
362         *count = 0;
363         return ESP_GATT_INVALID_HANDLE;
364     }
365 
366     if (result == NULL || count == NULL || *count == 0) {
367         return ESP_GATT_INVALID_PDU;
368     }
369 
370     uint16_t conn_hdl = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
371     return btc_ble_gattc_get_descr_by_char_handle(conn_hdl, char_handle, descr_uuid, result, count);
372 }
373 
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)374 esp_gatt_status_t esp_ble_gattc_get_include_service(esp_gatt_if_t gattc_if,
375                                                     uint16_t conn_id,
376                                                     uint16_t start_handle,
377                                                     uint16_t end_handle,
378                                                     esp_bt_uuid_t *incl_uuid,
379                                                     esp_gattc_incl_svc_elem_t *result,
380                                                     uint16_t *count)
381 {
382     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
383 
384     if (start_handle == 0 && end_handle == 0) {
385         *count = 0;
386         return ESP_GATT_INVALID_HANDLE;
387     }
388 
389     if (result == NULL || count == NULL || *count == 0) {
390         return ESP_GATT_INVALID_PDU;
391     }
392 
393     uint16_t conn_hdl = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
394     return btc_ble_gattc_get_include_service(conn_hdl, start_handle, end_handle, incl_uuid, result, count);
395 }
396 
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)397 esp_gatt_status_t esp_ble_gattc_get_attr_count(esp_gatt_if_t gattc_if,
398                                                uint16_t conn_id,
399                                                esp_gatt_db_attr_type_t type,
400                                                uint16_t start_handle,
401                                                uint16_t end_handle,
402                                                uint16_t char_handle,
403                                                uint16_t *count)
404 {
405     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
406 
407     if ((start_handle == 0 && end_handle == 0) && (type != ESP_GATT_DB_DESCRIPTOR)) {
408         *count = 0;
409         return ESP_GATT_INVALID_HANDLE;
410     }
411 
412     if (count == NULL) {
413         return ESP_GATT_INVALID_PDU;
414     }
415 
416     uint16_t conn_hdl = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
417     return btc_ble_gattc_get_attr_count(conn_hdl, type, start_handle, end_handle, char_handle, count);
418 }
419 
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)420 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,
421                                        esp_gattc_db_elem_t *db, uint16_t *count)
422 {
423     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
424 
425     if (start_handle == 0 && end_handle == 0) {
426         *count = 0;
427         return ESP_GATT_INVALID_HANDLE;
428     }
429 
430     if (db == NULL || count == NULL || *count == 0) {
431         return ESP_GATT_INVALID_PDU;
432     }
433 
434     uint16_t conn_hdl = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
435     return btc_ble_gattc_get_db(conn_hdl, start_handle, end_handle, db, count);
436 }
437 
438 
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)439 esp_err_t esp_ble_gattc_read_char (esp_gatt_if_t gattc_if,
440                                    uint16_t conn_id, uint16_t handle,
441                                    esp_gatt_auth_req_t auth_req)
442 {
443     btc_msg_t msg = {0};
444     btc_ble_gattc_args_t arg;
445 
446     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
447     tGATT_TCB       *p_tcb = gatt_get_tcb_by_idx(conn_id);
448     if (!gatt_check_connection_state_by_tcb(p_tcb)) {
449         LOG_WARN("%s, The connection not created.", __func__);
450         return ESP_ERR_INVALID_STATE;
451     }
452 
453     if (L2CA_CheckIsCongest(L2CAP_ATT_CID, p_tcb->peer_bda)) {
454         LOG_DEBUG("%s, the l2cap channel is congest.", __func__);
455         return ESP_FAIL;
456     }
457 
458     if (handle == 0) {
459         return ESP_GATT_INVALID_HANDLE;
460     }
461 
462     msg.sig = BTC_SIG_API_CALL;
463     msg.pid = BTC_PID_GATTC;
464     msg.act = BTC_GATTC_ACT_READ_CHAR;
465     arg.read_char.conn_id = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
466     arg.read_char.handle = handle;
467     arg.read_char.auth_req = auth_req;
468 
469     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
470 }
471 
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)472 esp_err_t esp_ble_gattc_read_by_type (esp_gatt_if_t gattc_if,
473                                       uint16_t conn_id,
474                                       uint16_t start_handle,
475                                       uint16_t end_handle,
476                                       esp_bt_uuid_t *uuid,
477                                       esp_gatt_auth_req_t auth_req)
478 {
479     btc_msg_t msg = {0};
480     btc_ble_gattc_args_t arg;
481 
482     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
483 
484     if (uuid == NULL) {
485         return ESP_GATT_ILLEGAL_PARAMETER;
486     }
487 
488     tGATT_TCB       *p_tcb = gatt_get_tcb_by_idx(conn_id);
489     if (!gatt_check_connection_state_by_tcb(p_tcb)) {
490         LOG_WARN("%s, The connection not created.", __func__);
491         return ESP_ERR_INVALID_STATE;
492     }
493 
494     if (L2CA_CheckIsCongest(L2CAP_ATT_CID, p_tcb->peer_bda)) {
495         LOG_DEBUG("%s, the l2cap channel is congest.", __func__);
496         return ESP_FAIL;
497     }
498 
499     if (start_handle == 0 || end_handle == 0) {
500         return ESP_GATT_INVALID_HANDLE;
501     }
502 
503     msg.sig = BTC_SIG_API_CALL;
504     msg.pid = BTC_PID_GATTC;
505     msg.act = BTC_GATTC_ACT_READ_BY_TYPE;
506     arg.read_by_type.conn_id = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
507     arg.read_by_type.s_handle = start_handle;
508     arg.read_by_type.e_handle = end_handle;
509     arg.read_by_type.auth_req = auth_req;
510     memcpy(&(arg.read_by_type.uuid), uuid, sizeof(esp_bt_uuid_t));
511 
512     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
513 }
514 
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)515 esp_err_t esp_ble_gattc_read_multiple(esp_gatt_if_t gattc_if,
516                                       uint16_t conn_id, esp_gattc_multi_t *read_multi,
517                                       esp_gatt_auth_req_t auth_req)
518 {
519     btc_msg_t msg = {0};
520     btc_ble_gattc_args_t arg;
521 
522     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
523 
524     tGATT_TCB       *p_tcb = gatt_get_tcb_by_idx(conn_id);
525     if (!gatt_check_connection_state_by_tcb(p_tcb)) {
526         LOG_WARN("%s, The connection not created.", __func__);
527         return ESP_ERR_INVALID_STATE;
528     }
529 
530     if (L2CA_CheckIsCongest(L2CAP_ATT_CID, p_tcb->peer_bda)) {
531         LOG_DEBUG("%s, the l2cap channel is congest.", __func__);
532         return ESP_FAIL;
533     }
534 
535     msg.sig = BTC_SIG_API_CALL;
536     msg.pid = BTC_PID_GATTC;
537     msg.act = BTC_GATTC_ACT_READ_MULTIPLE_CHAR;
538     arg.read_multiple.conn_id = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
539     arg.read_multiple.num_attr = read_multi->num_attr;
540     arg.read_multiple.auth_req = auth_req;
541 
542     if (read_multi->num_attr > 0) {
543         memcpy(arg.read_multiple.handles, read_multi->handles, sizeof(uint16_t)*read_multi->num_attr);
544     } else {
545         LOG_ERROR("%s(), the num_attr should not be 0.", __func__);
546         return ESP_FAIL;
547     }
548     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
549 }
550 
esp_ble_gattc_read_multiple_variable(esp_gatt_if_t gattc_if,uint16_t conn_id,esp_gattc_multi_t * read_multi,esp_gatt_auth_req_t auth_req)551 esp_err_t esp_ble_gattc_read_multiple_variable(esp_gatt_if_t gattc_if,
552                                       uint16_t conn_id, esp_gattc_multi_t *read_multi,
553                                       esp_gatt_auth_req_t auth_req)
554 {
555     btc_msg_t msg = {0};
556     btc_ble_gattc_args_t arg;
557 
558     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
559 
560     tGATT_TCB       *p_tcb = gatt_get_tcb_by_idx(conn_id);
561     if (!gatt_check_connection_state_by_tcb(p_tcb)) {
562         LOG_WARN("%s, The connection not created.", __func__);
563         return ESP_ERR_INVALID_STATE;
564     }
565 
566     if (L2CA_CheckIsCongest(L2CAP_ATT_CID, p_tcb->peer_bda)) {
567         LOG_DEBUG("%s, the l2cap channel is congest.", __func__);
568         return ESP_FAIL;
569     }
570 
571     msg.sig = BTC_SIG_API_CALL;
572     msg.pid = BTC_PID_GATTC;
573     msg.act = BTC_GATTC_ACT_READ_MULTIPLE_VARIABLE_CHAR;
574     arg.read_multiple.conn_id = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
575     arg.read_multiple.num_attr = read_multi->num_attr;
576     arg.read_multiple.auth_req = auth_req;
577 
578     if (read_multi->num_attr > 0) {
579         memcpy(arg.read_multiple.handles, read_multi->handles, sizeof(uint16_t)*read_multi->num_attr);
580     } else {
581         LOG_ERROR("%s(), the num_attr should not be 0.", __func__);
582         return ESP_FAIL;
583     }
584     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
585 }
586 
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)587 esp_err_t esp_ble_gattc_read_char_descr (esp_gatt_if_t gattc_if,
588                                          uint16_t conn_id, uint16_t handle,
589                                          esp_gatt_auth_req_t auth_req)
590 {
591     btc_msg_t msg = {0};
592     btc_ble_gattc_args_t arg;
593 
594     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
595 
596     tGATT_TCB       *p_tcb = gatt_get_tcb_by_idx(conn_id);
597     if (!gatt_check_connection_state_by_tcb(p_tcb)) {
598         LOG_WARN("%s, The connection not created.", __func__);
599         return ESP_ERR_INVALID_STATE;
600     }
601 
602     if (L2CA_CheckIsCongest(L2CAP_ATT_CID, p_tcb->peer_bda)) {
603         LOG_DEBUG("%s, the l2cap channel is congest.", __func__);
604         return ESP_FAIL;
605     }
606 
607     if (handle == 0) {
608         return ESP_GATT_INVALID_HANDLE;
609     }
610 
611     msg.sig = BTC_SIG_API_CALL;
612     msg.pid = BTC_PID_GATTC;
613     msg.act = BTC_GATTC_ACT_READ_CHAR_DESCR;
614     arg.read_descr.conn_id = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
615     arg.read_descr.handle = handle;
616     arg.read_descr.auth_req = auth_req;
617 
618     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
619 }
620 
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)621 esp_err_t esp_ble_gattc_write_char(esp_gatt_if_t gattc_if,
622                                    uint16_t conn_id, uint16_t handle,
623                                    uint16_t value_len,
624                                    uint8_t *value,
625                                    esp_gatt_write_type_t write_type,
626                                    esp_gatt_auth_req_t auth_req)
627 {
628     btc_msg_t msg = {0};
629     btc_ble_gattc_args_t arg;
630 
631     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
632 
633     tGATT_TCB       *p_tcb = gatt_get_tcb_by_idx(conn_id);
634     if (!gatt_check_connection_state_by_tcb(p_tcb)) {
635         LOG_WARN("%s, The connection not created.", __func__);
636         return ESP_ERR_INVALID_STATE;
637     }
638 
639     if (L2CA_CheckIsCongest(L2CAP_ATT_CID, p_tcb->peer_bda)) {
640         LOG_DEBUG("%s, the l2cap channel is congest.", __func__);
641         return ESP_FAIL;
642     }
643 
644     if (handle == 0) {
645         return ESP_GATT_INVALID_HANDLE;
646     }
647 
648     msg.sig = BTC_SIG_API_CALL;
649     msg.pid = BTC_PID_GATTC;
650     msg.act = BTC_GATTC_ACT_WRITE_CHAR;
651     arg.write_char.conn_id = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
652     arg.write_char.handle = handle;
653     arg.write_char.value_len = value_len > ESP_GATT_MAX_ATTR_LEN ? ESP_GATT_MAX_ATTR_LEN : value_len;
654     arg.write_char.value = value;
655     arg.write_char.write_type = write_type;
656     arg.write_char.auth_req = auth_req;
657     if(write_type == ESP_GATT_WRITE_TYPE_NO_RSP){
658         l2ble_update_att_acl_pkt_num(L2CA_ADD_BTC_NUM, NULL);
659     }
660     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), btc_gattc_arg_deep_copy,
661                 btc_gattc_arg_deep_free) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
662 }
663 
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)664 esp_err_t esp_ble_gattc_write_char_descr (esp_gatt_if_t gattc_if,
665                                           uint16_t conn_id, uint16_t handle,
666                                           uint16_t value_len,
667                                           uint8_t *value,
668                                           esp_gatt_write_type_t write_type,
669                                           esp_gatt_auth_req_t auth_req)
670 {
671     btc_msg_t msg = {0};
672     btc_ble_gattc_args_t arg;
673 
674     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
675 
676     tGATT_TCB       *p_tcb = gatt_get_tcb_by_idx(conn_id);
677     if (!gatt_check_connection_state_by_tcb(p_tcb)) {
678         LOG_WARN("%s, The connection not created.", __func__);
679         return ESP_ERR_INVALID_STATE;
680     }
681 
682     if (L2CA_CheckIsCongest(L2CAP_ATT_CID, p_tcb->peer_bda)) {
683         LOG_DEBUG("%s, the l2cap channel is congest.", __func__);
684         return ESP_FAIL;
685     }
686 
687     if (handle == 0) {
688         return ESP_GATT_INVALID_HANDLE;
689     }
690 
691     msg.sig = BTC_SIG_API_CALL;
692     msg.pid = BTC_PID_GATTC;
693     msg.act = BTC_GATTC_ACT_WRITE_CHAR_DESCR;
694     arg.write_descr.conn_id = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
695     arg.write_descr.handle = handle;
696     arg.write_descr.value_len = value_len > ESP_GATT_MAX_ATTR_LEN ? ESP_GATT_MAX_ATTR_LEN : value_len;
697     arg.write_descr.value = value;
698     arg.write_descr.write_type = write_type;
699     arg.write_descr.auth_req = auth_req;
700     if(write_type == ESP_GATT_WRITE_TYPE_NO_RSP){
701         l2ble_update_att_acl_pkt_num(L2CA_ADD_BTC_NUM, NULL);
702     }
703     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), btc_gattc_arg_deep_copy,
704                 btc_gattc_arg_deep_free) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
705 }
706 
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)707 esp_err_t esp_ble_gattc_prepare_write(esp_gatt_if_t gattc_if,
708                                       uint16_t conn_id, uint16_t handle,
709                                       uint16_t offset,
710                                       uint16_t value_len,
711                                       uint8_t *value,
712                                       esp_gatt_auth_req_t auth_req)
713 {
714     btc_msg_t msg = {0};
715     btc_ble_gattc_args_t arg;
716 
717     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
718 
719     tGATT_TCB       *p_tcb = gatt_get_tcb_by_idx(conn_id);
720     if (!gatt_check_connection_state_by_tcb(p_tcb)) {
721         LOG_WARN("%s, The connection not created.", __func__);
722         return ESP_ERR_INVALID_STATE;
723     }
724 
725     if (L2CA_CheckIsCongest(L2CAP_ATT_CID, p_tcb->peer_bda)) {
726         LOG_DEBUG("%s, the l2cap channel is congest.", __func__);
727         return ESP_FAIL;
728     }
729 
730     if (handle == 0) {
731         return ESP_GATT_INVALID_HANDLE;
732     }
733 
734     msg.sig = BTC_SIG_API_CALL;
735     msg.pid = BTC_PID_GATTC;
736     msg.act = BTC_GATTC_ACT_PREPARE_WRITE;
737     arg.prep_write.conn_id = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
738     arg.prep_write.handle = handle;
739     arg.prep_write.offset = offset;
740     arg.prep_write.value_len = value_len > ESP_GATT_MAX_ATTR_LEN ? ESP_GATT_MAX_ATTR_LEN : value_len; // length check ?
741     arg.prep_write.value = value;
742     arg.prep_write.auth_req = auth_req;
743 
744     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), btc_gattc_arg_deep_copy,
745                 btc_gattc_arg_deep_free) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
746 }
747 
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)748 esp_err_t esp_ble_gattc_prepare_write_char_descr(esp_gatt_if_t gattc_if,
749                                                  uint16_t conn_id, uint16_t handle,
750                                                  uint16_t offset,
751                                                  uint16_t value_len,
752                                                  uint8_t *value,
753                                                  esp_gatt_auth_req_t auth_req)
754 {
755     btc_msg_t msg = {0};
756     btc_ble_gattc_args_t arg;
757 
758     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
759 
760     tGATT_TCB       *p_tcb = gatt_get_tcb_by_idx(conn_id);
761     if (!gatt_check_connection_state_by_tcb(p_tcb)) {
762         LOG_WARN("%s, The connection not created.", __func__);
763         return ESP_ERR_INVALID_STATE;
764     }
765 
766     if (L2CA_CheckIsCongest(L2CAP_ATT_CID, p_tcb->peer_bda)) {
767         LOG_DEBUG("%s, the l2cap channel is congest.", __func__);
768         return ESP_FAIL;
769     }
770 
771     if (handle == 0) {
772         return ESP_GATT_INVALID_HANDLE;
773     }
774 
775     msg.sig = BTC_SIG_API_CALL;
776     msg.pid = BTC_PID_GATTC;
777     msg.act = BTC_GATTC_ACT_PREPARE_WRITE_CHAR_DESCR;
778     arg.prep_write_descr.conn_id = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
779     arg.prep_write_descr.handle = handle;
780     arg.prep_write_descr.offset = offset;
781     arg.prep_write_descr.value_len = value_len > ESP_GATT_MAX_ATTR_LEN ? ESP_GATT_MAX_ATTR_LEN : value_len; // length check ?
782     arg.prep_write_descr.value = value;
783     arg.prep_write_descr.auth_req = auth_req;
784 
785     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), btc_gattc_arg_deep_copy,
786                 btc_gattc_arg_deep_free) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
787 }
788 
esp_ble_gattc_execute_write(esp_gatt_if_t gattc_if,uint16_t conn_id,bool is_execute)789 esp_err_t esp_ble_gattc_execute_write (esp_gatt_if_t gattc_if, uint16_t conn_id, bool is_execute)
790 {
791     btc_msg_t msg = {0};
792     btc_ble_gattc_args_t arg;
793 
794     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
795 
796     msg.sig = BTC_SIG_API_CALL;
797     msg.pid = BTC_PID_GATTC;
798     msg.act = BTC_GATTC_ACT_EXECUTE_WRITE;
799     arg.exec_write.conn_id = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id);
800     arg.exec_write.is_execute = is_execute;
801 
802     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
803 }
804 
esp_ble_gattc_register_for_notify(esp_gatt_if_t gattc_if,esp_bd_addr_t server_bda,uint16_t handle)805 esp_err_t esp_ble_gattc_register_for_notify (esp_gatt_if_t gattc_if,
806                                              esp_bd_addr_t server_bda, uint16_t handle)
807 {
808     btc_msg_t msg = {0};
809     btc_ble_gattc_args_t arg;
810 
811     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
812 
813     if (handle == 0) {
814         return ESP_GATT_INVALID_HANDLE;
815     }
816 
817     msg.sig = BTC_SIG_API_CALL;
818     msg.pid = BTC_PID_GATTC;
819     msg.act = BTC_GATTC_ACT_REG_FOR_NOTIFY;
820     arg.reg_for_notify.gattc_if = gattc_if;
821     memcpy(arg.reg_for_notify.remote_bda, server_bda, sizeof(esp_bd_addr_t));
822     arg.reg_for_notify.handle = handle;
823 
824     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
825 }
826 
esp_ble_gattc_unregister_for_notify(esp_gatt_if_t gattc_if,esp_bd_addr_t server_bda,uint16_t handle)827 esp_err_t esp_ble_gattc_unregister_for_notify (esp_gatt_if_t gattc_if,
828                                                esp_bd_addr_t server_bda, uint16_t handle)
829 {
830     btc_msg_t msg = {0};
831     btc_ble_gattc_args_t arg;
832 
833     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
834 
835     if (handle == 0) {
836         return ESP_GATT_INVALID_HANDLE;
837     }
838 
839     msg.sig = BTC_SIG_API_CALL;
840     msg.pid = BTC_PID_GATTC;
841     msg.act = BTC_GATTC_ACT_UNREG_FOR_NOTIFY;
842     arg.unreg_for_notify.gattc_if = gattc_if;
843     arg.unreg_for_notify.handle = handle;
844     memcpy(arg.unreg_for_notify.remote_bda, server_bda, sizeof(esp_bd_addr_t));
845     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
846 }
847 
esp_ble_gattc_cache_refresh(esp_bd_addr_t remote_bda)848 esp_err_t esp_ble_gattc_cache_refresh(esp_bd_addr_t remote_bda)
849 {
850     btc_msg_t msg = {0};
851     btc_ble_gattc_args_t arg;
852 
853     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
854 
855     msg.sig = BTC_SIG_API_CALL;
856     msg.pid = BTC_PID_GATTC;
857     msg.act = BTC_GATTC_ACT_CACHE_REFRESH;
858     memcpy(arg.cache_refresh.remote_bda, remote_bda, sizeof(esp_bd_addr_t));
859 
860     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
861 }
862 
esp_ble_gattc_cache_clean(esp_bd_addr_t remote_bda)863 esp_err_t esp_ble_gattc_cache_clean(esp_bd_addr_t remote_bda)
864 {
865     btc_msg_t msg = {0};
866     btc_ble_gattc_args_t arg;
867 
868     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
869 
870     msg.sig = BTC_SIG_API_CALL;
871     msg.pid = BTC_PID_GATTC;
872     msg.act = BTC_GATTC_ACT_CACHE_CLEAN;
873     memcpy(arg.cache_clean.remote_bda, remote_bda, sizeof(esp_bd_addr_t));
874 
875     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
876 }
877 
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)878 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)
879 {
880     btc_msg_t msg = {0};
881     btc_ble_gattc_args_t arg;
882 
883     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
884 
885     msg.sig = BTC_SIG_API_CALL;
886     msg.pid = BTC_PID_GATTC;
887     msg.act = BTC_GATTC_ACT_CACHE_ASSOC;
888     arg.cache_assoc.is_assoc = is_assoc;
889     arg.cache_assoc.gattc_if = gattc_if;
890     memcpy(arg.cache_assoc.src_addr, src_addr, sizeof(esp_bd_addr_t));
891     memcpy(arg.cache_assoc.assoc_addr, assoc_addr, sizeof(esp_bd_addr_t));
892 
893     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
894 }
895 
esp_ble_gattc_cache_get_addr_list(esp_gatt_if_t gattc_if)896 esp_err_t esp_ble_gattc_cache_get_addr_list(esp_gatt_if_t gattc_if)
897 {
898     btc_msg_t msg = {0};
899     btc_ble_gattc_args_t arg;
900 
901     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
902 
903     msg.sig = BTC_SIG_API_CALL;
904     msg.pid = BTC_PID_GATTC;
905     msg.act = BTC_GATTC_ATC_CACHE_GET_ADDR_LIST;
906     arg.get_addr_list.gattc_if = gattc_if;
907     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
908 }
909 
910 #endif  ///GATTC_INCLUDED == TRUE
911