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