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