1 /*
2 * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
3
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8 #include <string.h>
9
10 #include "stack/btm_ble_api.h"
11 #include "btc_gattc.h"
12 #include "btc_gatt_util.h"
13 #include "btc/btc_manage.h"
14 #include "bta/bta_gatt_api.h"
15 #include "common/bt_trace.h"
16 #include "osi/allocator.h"
17 #include "esp_gattc_api.h"
18 #include "btc/btc_storage.h"
19 #include "common/bt_defs.h"
20
21 #if (GATTC_INCLUDED == TRUE)
btc_gattc_cb_to_app(esp_gattc_cb_event_t event,esp_gatt_if_t gattc_if,esp_ble_gattc_cb_param_t * param)22 static inline void btc_gattc_cb_to_app(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param)
23 {
24 esp_gattc_cb_t btc_gattc_cb = (esp_gattc_cb_t )btc_profile_cb_get(BTC_PID_GATTC);
25 if (btc_gattc_cb) {
26 btc_gattc_cb(event, gattc_if, param);
27 }
28 }
29
btc_gattc_arg_deep_copy(btc_msg_t * msg,void * p_dest,void * p_src)30 void btc_gattc_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src)
31 {
32 btc_ble_gattc_args_t *dst = (btc_ble_gattc_args_t *) p_dest;
33 btc_ble_gattc_args_t *src = (btc_ble_gattc_args_t *)p_src;
34
35 switch (msg->act) {
36 case BTC_GATTC_ACT_WRITE_CHAR: {
37 dst->write_char.value = (uint8_t *)osi_malloc(src->write_char.value_len);
38 if (dst->write_char.value) {
39 memcpy(dst->write_char.value, src->write_char.value, src->write_char.value_len);
40 } else {
41 BTC_TRACE_ERROR("%s %d no mem\n", __func__, msg->act);
42 }
43 break;
44 }
45 case BTC_GATTC_ACT_WRITE_CHAR_DESCR: {
46 dst->write_descr.value = (uint8_t *)osi_malloc(src->write_descr.value_len);
47 if (dst->write_descr.value) {
48 memcpy(dst->write_descr.value, src->write_descr.value, src->write_descr.value_len);
49 } else {
50 BTC_TRACE_ERROR("%s %d no mem\n", __func__, msg->act);
51 }
52 break;
53 }
54 case BTC_GATTC_ACT_PREPARE_WRITE: {
55 dst->prep_write.value = (uint8_t *)osi_malloc(src->prep_write.value_len);
56 if (dst->prep_write.value) {
57 memcpy(dst->prep_write.value, src->prep_write.value, src->prep_write.value_len);
58 } else {
59 BTC_TRACE_ERROR("%s %d no mem\n", __func__, msg->act);
60 }
61 break;
62 }
63 case BTC_GATTC_ACT_PREPARE_WRITE_CHAR_DESCR: {
64 dst->prep_write_descr.value = (uint8_t *)osi_malloc(src->prep_write_descr.value_len);
65 if (dst->prep_write_descr.value) {
66 memcpy(dst->prep_write_descr.value, src->prep_write_descr.value, src->prep_write_descr.value_len);
67 } else {
68 BTC_TRACE_ERROR("%s %d no mem\n", __func__, msg->act);
69 }
70 break;
71 }
72 default:
73 BTC_TRACE_DEBUG("%s Unhandled deep copy %d\n", __func__, msg->act);
74 break;
75 }
76 }
77
btc_gattc_arg_deep_free(btc_msg_t * msg)78 void btc_gattc_arg_deep_free(btc_msg_t *msg)
79 {
80 btc_ble_gattc_args_t *arg = (btc_ble_gattc_args_t *)msg->arg;
81
82 switch (msg->act) {
83 case BTC_GATTC_ACT_WRITE_CHAR: {
84 if (arg->write_char.value) {
85 osi_free(arg->write_char.value);
86 }
87 break;
88 }
89 case BTC_GATTC_ACT_WRITE_CHAR_DESCR: {
90 if (arg->write_descr.value) {
91 osi_free(arg->write_descr.value);
92 }
93 break;
94 }
95 case BTC_GATTC_ACT_PREPARE_WRITE: {
96 if (arg->prep_write.value) {
97 osi_free(arg->prep_write.value);
98 }
99 break;
100 }
101 case BTC_GATTC_ACT_PREPARE_WRITE_CHAR_DESCR: {
102 if (arg->prep_write_descr.value) {
103 osi_free(arg->prep_write_descr.value);
104 }
105 break;
106 }
107 default:
108 BTC_TRACE_DEBUG("%s Unhandled deep free %d\n", __func__, msg->act);
109 break;
110 }
111
112 }
113
btc_gattc_copy_req_data(btc_msg_t * msg,void * p_dest,void * p_src)114 static void btc_gattc_copy_req_data(btc_msg_t *msg, void *p_dest, void *p_src)
115 {
116 tBTA_GATTC *p_dest_data = (tBTA_GATTC *) p_dest;
117 tBTA_GATTC *p_src_data = (tBTA_GATTC *) p_src;
118
119 if (!p_src_data || !p_dest_data || !msg) {
120 return;
121 }
122
123 // Allocate buffer for request data if necessary
124 switch (msg->act) {
125 case BTA_GATTC_READ_DESCR_EVT:
126 case BTA_GATTC_READ_CHAR_EVT:
127 case BTA_GATTC_READ_MULTIPLE_EVT:
128 case BTA_GATTC_READ_MULTI_VAR_EVT: {
129 if (p_src_data->read.p_value && p_src_data->read.p_value->p_value) {
130 p_dest_data->read.p_value = (tBTA_GATT_UNFMT *)osi_malloc(sizeof(tBTA_GATT_UNFMT) + p_src_data->read.p_value->len);
131 p_dest_data->read.p_value->p_value = (uint8_t *)(p_dest_data->read.p_value + 1);
132 if (p_dest_data->read.p_value && p_dest_data->read.p_value->p_value) {
133 p_dest_data->read.p_value->len = p_src_data->read.p_value->len;
134 memcpy(p_dest_data->read.p_value->p_value, p_src_data->read.p_value->p_value, p_src_data->read.p_value->len);
135 } else {
136 BTC_TRACE_ERROR("%s %d no mem\n", __func__, msg->act);
137 }
138 }
139 break;
140 }
141 case BTA_GATTC_GET_ADDR_LIST_EVT: {
142 if (p_src_data->get_addr_list.bda_list != NULL) {
143 uint8_t num_addr = p_src_data->get_addr_list.num_addr;
144 p_dest_data->get_addr_list.bda_list = (BD_ADDR *)osi_malloc(sizeof(BD_ADDR) * num_addr);
145 if (p_dest_data->get_addr_list.bda_list) {
146 memcpy(p_dest_data->get_addr_list.bda_list, p_src_data->get_addr_list.bda_list, sizeof(BD_ADDR) * num_addr);
147 } else {
148 BTC_TRACE_ERROR("%s %d no mem\n", __func__, msg->act);
149 }
150 }
151 break;
152 }
153 default:
154 break;
155 }
156 }
157
btc_gattc_free_req_data(btc_msg_t * msg)158 static void btc_gattc_free_req_data(btc_msg_t *msg)
159 {
160 tBTA_GATTC *arg = (tBTA_GATTC *)(msg->arg);
161 switch (msg->act) {
162 case BTA_GATTC_READ_DESCR_EVT:
163 case BTA_GATTC_READ_CHAR_EVT:
164 case BTA_GATTC_READ_MULTIPLE_EVT:
165 case BTA_GATTC_READ_MULTI_VAR_EVT: {
166 if (arg->read.p_value) {
167 osi_free(arg->read.p_value);
168 }
169 break;
170 }
171 case BTA_GATTC_GET_ADDR_LIST_EVT: {
172 if (arg->get_addr_list.bda_list) {
173 osi_free(arg->get_addr_list.bda_list);
174 }
175 break;
176 }
177 default:
178 break;
179 }
180 return;
181 }
182
btc_gattc_cback(tBTA_GATTC_EVT event,tBTA_GATTC * p_data)183 static void btc_gattc_cback(tBTA_GATTC_EVT event, tBTA_GATTC *p_data)
184 {
185 bt_status_t ret;
186 btc_msg_t msg = {0};
187
188 msg.sig = BTC_SIG_API_CB;
189 msg.pid = BTC_PID_GATTC;
190 msg.act = (uint8_t) event;
191 ret = btc_transfer_context(&msg, p_data, sizeof(tBTA_GATTC),
192 btc_gattc_copy_req_data, btc_gattc_free_req_data);
193
194 if (ret) {
195 BTC_TRACE_ERROR("%s transfer failed\n", __func__);
196 }
197 }
198
btc_gattc_app_register(btc_ble_gattc_args_t * arg)199 static void btc_gattc_app_register(btc_ble_gattc_args_t *arg)
200 {
201 tBT_UUID app_uuid;
202 app_uuid.len = 2;
203 app_uuid.uu.uuid16 = arg->app_reg.app_id;
204 BTA_GATTC_AppRegister(&app_uuid, btc_gattc_cback);
205 }
206
btc_gattc_app_unregister(btc_ble_gattc_args_t * arg)207 static void btc_gattc_app_unregister(btc_ble_gattc_args_t *arg)
208 {
209 BTA_GATTC_AppDeregister(arg->app_unreg.gattc_if);
210 }
211
btc_gattc_open(btc_ble_gattc_args_t * arg)212 static void btc_gattc_open(btc_ble_gattc_args_t *arg)
213 {
214 tBTA_GATT_TRANSPORT transport = BTA_GATT_TRANSPORT_LE;
215
216 BTA_GATTC_Enh_Open(arg->open.gattc_if, arg->open.remote_bda,
217 arg->open.remote_addr_type, arg->open.is_direct,
218 transport, arg->open.is_aux, arg->open.own_addr_type,
219 arg->open.phy_mask, (void *)&arg->open.phy_1m_conn_params,
220 (void *)&arg->open.phy_2m_conn_params, (void *)&arg->open.phy_coded_conn_params);
221 }
222
btc_gattc_close(btc_ble_gattc_args_t * arg)223 static void btc_gattc_close(btc_ble_gattc_args_t *arg)
224 {
225 // TODO; Review this call of BTA_API, check the usage of BTA_GATTC_CancelOpen
226 BTA_GATTC_Close(arg->close.conn_id);
227 }
228
btc_gattc_cfg_mtu(btc_ble_gattc_args_t * arg)229 static void btc_gattc_cfg_mtu(btc_ble_gattc_args_t *arg)
230 {
231 BTA_GATTC_ConfigureMTU (arg->cfg_mtu.conn_id);
232 }
233
btc_gattc_check_valid_param(int num,uint16_t offset)234 static esp_gatt_status_t btc_gattc_check_valid_param(int num, uint16_t offset)
235 {
236 if (num == 0) {
237 return ESP_GATT_NOT_FOUND;
238 } else if (offset >= num) {
239 return ESP_GATT_INVALID_OFFSET;
240 }
241
242 return ESP_GATT_OK;
243 }
244
btc_gattc_fill_gatt_db_conversion(uint16_t count,uint16_t num,esp_gatt_db_attr_type_t type,uint16_t offset,void * result,btgatt_db_element_t * db)245 static void btc_gattc_fill_gatt_db_conversion(uint16_t count, uint16_t num, esp_gatt_db_attr_type_t type,
246 uint16_t offset, void *result, btgatt_db_element_t *db)
247 {
248 tBT_UUID bta_uuid = {0};
249 uint16_t db_size = (count + offset > num) ? (num - offset) : count;
250 switch(type) {
251 case ESP_GATT_DB_PRIMARY_SERVICE:
252 case ESP_GATT_DB_SECONDARY_SERVICE: {
253 esp_gattc_service_elem_t *svc_result = (esp_gattc_service_elem_t *)result;
254 for (int i = 0; i < db_size; i++) {
255 svc_result->is_primary = (db[offset + i].type == BTGATT_DB_PRIMARY_SERVICE) ? true : false;
256 svc_result->start_handle = db[offset + i].start_handle;
257 svc_result->end_handle = db[offset + i].end_handle;
258 btc128_to_bta_uuid(&bta_uuid, db[offset + i].uuid.uu);
259 bta_to_btc_uuid(&svc_result->uuid, &bta_uuid);
260 svc_result++;
261 }
262 break;
263 }
264 case ESP_GATT_DB_CHARACTERISTIC: {
265 esp_gattc_char_elem_t *char_result = (esp_gattc_char_elem_t *)result;
266 for (int i = 0; i < db_size; i++) {
267 char_result->char_handle = db[offset + i].attribute_handle;
268 char_result->properties = db[offset + i].properties;
269 btc128_to_bta_uuid(&bta_uuid, db[offset + i].uuid.uu);
270 bta_to_btc_uuid(&char_result->uuid, &bta_uuid);
271 char_result++;
272 }
273 break;
274 }
275 case ESP_GATT_DB_DESCRIPTOR: {
276 esp_gattc_descr_elem_t *descr_result = (esp_gattc_descr_elem_t *)result;
277 for (int i = 0; i < db_size; i++) {
278 descr_result->handle = db[offset + i].attribute_handle;
279 btc128_to_bta_uuid(&bta_uuid, db[offset + i].uuid.uu);
280 bta_to_btc_uuid(&descr_result->uuid, &bta_uuid);
281 descr_result++;
282 }
283 break;
284 }
285 case ESP_GATT_DB_INCLUDED_SERVICE: {
286 esp_gattc_incl_svc_elem_t *incl_result = (esp_gattc_incl_svc_elem_t *)result;
287 for (int i = 0; i < db_size; i++) {
288 incl_result->handle = db[offset + i].attribute_handle;
289 incl_result->incl_srvc_s_handle = db[offset + i].start_handle;
290 incl_result->incl_srvc_e_handle = db[offset + i].end_handle;
291 btc128_to_bta_uuid(&bta_uuid, db[offset + i].uuid.uu);
292 bta_to_btc_uuid(&incl_result->uuid, &bta_uuid);
293 incl_result++;
294 }
295 break;
296 }
297 default:
298 BTC_TRACE_WARNING("%s(), Not support type(%d)", __func__, type);
299 break;
300 }
301 }
302
btc_gattc_search_service(btc_ble_gattc_args_t * arg)303 static void btc_gattc_search_service(btc_ble_gattc_args_t *arg)
304 {
305 tBT_UUID srvc_uuid;
306
307 if (arg->search_srvc.filter_uuid_enable) {
308 btc_to_bta_uuid(&srvc_uuid, &arg->search_srvc.filter_uuid);
309 BTA_GATTC_ServiceSearchRequest(arg->search_srvc.conn_id, &srvc_uuid);
310 } else {
311 BTA_GATTC_ServiceSearchRequest(arg->search_srvc.conn_id, NULL);
312 }
313 }
314
btc_ble_gattc_get_service(uint16_t conn_id,esp_bt_uuid_t * svc_uuid,esp_gattc_service_elem_t * result,uint16_t * count,uint16_t offset)315 esp_gatt_status_t btc_ble_gattc_get_service(uint16_t conn_id, esp_bt_uuid_t *svc_uuid,
316 esp_gattc_service_elem_t *result,
317 uint16_t *count, uint16_t offset)
318 {
319 esp_gatt_status_t status;
320 btgatt_db_element_t *db = NULL;
321 uint16_t svc_num = 0;
322 tBT_UUID *bta_uuid = NULL;
323 if (svc_uuid) {
324 bta_uuid = osi_malloc(sizeof(tBT_UUID));
325 btc_to_bta_uuid(bta_uuid, svc_uuid);
326 }
327
328 BTA_GATTC_GetServiceWithUUID(conn_id, bta_uuid, &db, &svc_num);
329
330 if ((status = btc_gattc_check_valid_param((int)svc_num, offset)) != ESP_GATT_OK) {
331 if (db) {
332 osi_free(db);
333 }
334 if (bta_uuid) {
335 osi_free(bta_uuid);
336 }
337 *count = 0;
338 return status;
339 } else {
340 btc_gattc_fill_gatt_db_conversion(*count, svc_num, ESP_GATT_DB_PRIMARY_SERVICE, offset, (void *)result, db);
341 }
342
343 *count = svc_num;
344 //don't forget to free the db buffer after used.
345 if (db) {
346 osi_free(db);
347 }
348 if (bta_uuid) {
349 osi_free(bta_uuid);
350 }
351 return ESP_GATT_OK;
352 }
353
btc_ble_gattc_get_all_char(uint16_t conn_id,uint16_t start_handle,uint16_t end_handle,esp_gattc_char_elem_t * result,uint16_t * count,uint16_t offset)354 esp_gatt_status_t btc_ble_gattc_get_all_char(uint16_t conn_id,
355 uint16_t start_handle,
356 uint16_t end_handle,
357 esp_gattc_char_elem_t *result,
358 uint16_t *count, uint16_t offset)
359 {
360 esp_gatt_status_t status;
361 btgatt_db_element_t *db = NULL;
362 uint16_t char_num = 0;
363 BTA_GATTC_GetAllChar(conn_id, start_handle, end_handle, &db, &char_num);
364
365 if ((status = btc_gattc_check_valid_param((int)char_num, offset)) != ESP_GATT_OK) {
366 if (db) {
367 osi_free(db);
368 }
369 *count = 0;
370 return status;
371 } else {
372 btc_gattc_fill_gatt_db_conversion(*count, char_num, ESP_GATT_DB_CHARACTERISTIC, offset, (void *)result, db);
373 }
374
375 *count = char_num;
376 //don't forget to free the db buffer after used.
377 if (db) {
378 osi_free(db);
379 }
380 return ESP_GATT_OK;
381 }
382
btc_ble_gattc_get_all_descr(uint16_t conn_id,uint16_t char_handle,esp_gattc_descr_elem_t * result,uint16_t * count,uint16_t offset)383 esp_gatt_status_t btc_ble_gattc_get_all_descr(uint16_t conn_id,
384 uint16_t char_handle,
385 esp_gattc_descr_elem_t *result,
386 uint16_t *count, uint16_t offset)
387 {
388 esp_gatt_status_t status;
389 btgatt_db_element_t *db = NULL;
390 uint16_t descr_num = 0;
391 BTA_GATTC_GetAllDescriptor(conn_id, char_handle, &db, &descr_num);
392
393 if ((status = btc_gattc_check_valid_param((int)descr_num, offset)) != ESP_GATT_OK) {
394 if (db) {
395 osi_free(db);
396 }
397 *count = 0;
398 return status;
399 } else {
400 btc_gattc_fill_gatt_db_conversion(*count, descr_num, ESP_GATT_DB_DESCRIPTOR, offset, (void *)result, db);
401 }
402
403 *count = descr_num;
404 //don't forget to free the db buffer after used.
405 if (db) {
406 osi_free(db);
407 }
408 return ESP_GATT_OK;
409 }
410
btc_ble_gattc_get_char_by_uuid(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)411 esp_gatt_status_t btc_ble_gattc_get_char_by_uuid(uint16_t conn_id,
412 uint16_t start_handle,
413 uint16_t end_handle,
414 esp_bt_uuid_t char_uuid,
415 esp_gattc_char_elem_t *result,
416 uint16_t *count)
417 {
418 esp_gatt_status_t status;
419 btgatt_db_element_t *db = NULL;
420 uint16_t char_num = 0;
421 tBT_UUID bta_uuid = {0};
422 btc_to_bta_uuid(&bta_uuid, &char_uuid);
423 BTA_GATTC_GetCharByUUID(conn_id, start_handle, end_handle, bta_uuid, &db, &char_num);
424
425 if ((status = btc_gattc_check_valid_param((int)char_num, 0)) != ESP_GATT_OK) {
426 if (db) {
427 osi_free(db);
428 }
429 *count = 0;
430 return status;
431 } else {
432 btc_gattc_fill_gatt_db_conversion(*count, char_num, ESP_GATT_DB_CHARACTERISTIC, 0, (void *)result, db);
433 }
434
435 *count = char_num;
436 //don't forget to free the db buffer after used.
437 if (db) {
438 osi_free(db);
439 }
440 return ESP_GATT_OK;
441 }
442
btc_ble_gattc_get_descr_by_uuid(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)443 esp_gatt_status_t btc_ble_gattc_get_descr_by_uuid(uint16_t conn_id,
444 uint16_t start_handle,
445 uint16_t end_handle,
446 esp_bt_uuid_t char_uuid,
447 esp_bt_uuid_t descr_uuid,
448 esp_gattc_descr_elem_t *result,
449 uint16_t *count)
450 {
451 esp_gatt_status_t status;
452 btgatt_db_element_t *db = NULL;
453 uint16_t descr_num = 0;
454 tBT_UUID bta_char_uuid = {0};
455 tBT_UUID bta_descr_uuid = {0};
456 btc_to_bta_uuid(&bta_char_uuid, &char_uuid);
457 btc_to_bta_uuid(&bta_descr_uuid, &descr_uuid);
458
459 BTA_GATTC_GetDescrByUUID(conn_id, start_handle, end_handle,
460 bta_char_uuid, bta_descr_uuid, &db, &descr_num);
461
462 if ((status = btc_gattc_check_valid_param((int)descr_num, 0)) != ESP_GATT_OK) {
463 if (db) {
464 osi_free(db);
465 }
466 *count = 0;
467 return status;
468 } else {
469 btc_gattc_fill_gatt_db_conversion(*count, descr_num, ESP_GATT_DB_DESCRIPTOR, 0, (void *)result, db);
470 }
471
472 *count = descr_num;
473 //don't forget to free the db buffer after used.
474 if (db) {
475 osi_free(db);
476 }
477 return ESP_GATT_OK;
478 }
479
btc_ble_gattc_get_descr_by_char_handle(uint16_t conn_id,uint16_t char_handle,esp_bt_uuid_t descr_uuid,esp_gattc_descr_elem_t * result,uint16_t * count)480 esp_gatt_status_t btc_ble_gattc_get_descr_by_char_handle(uint16_t conn_id,
481 uint16_t char_handle,
482 esp_bt_uuid_t descr_uuid,
483 esp_gattc_descr_elem_t *result,
484 uint16_t *count)
485 {
486 esp_gatt_status_t status;
487 btgatt_db_element_t *db = NULL;
488 uint16_t descr_num = 0;
489 tBT_UUID bta_descr_uuid = {0};
490 btc_to_bta_uuid(&bta_descr_uuid, &descr_uuid);
491
492 BTA_GATTC_GetDescrByCharHandle(conn_id, char_handle, bta_descr_uuid, &db, &descr_num);
493
494 if ((status = btc_gattc_check_valid_param((int)descr_num, 0)) != ESP_GATT_OK) {
495 if (db) {
496 osi_free(db);
497 }
498 *count = 0;
499 return status;
500 } else {
501 btc_gattc_fill_gatt_db_conversion(*count, descr_num, ESP_GATT_DB_DESCRIPTOR, 0, (void *)result, db);
502 }
503
504 *count = descr_num;
505 //don't forget to free the db buffer after used.
506 if (db) {
507 osi_free(db);
508 }
509 return ESP_GATT_OK;
510
511 }
512
btc_ble_gattc_get_include_service(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)513 esp_gatt_status_t btc_ble_gattc_get_include_service(uint16_t conn_id,
514 uint16_t start_handle,
515 uint16_t end_handle,
516 esp_bt_uuid_t *incl_uuid,
517 esp_gattc_incl_svc_elem_t *result,
518 uint16_t *count)
519 {
520 esp_gatt_status_t status;
521 btgatt_db_element_t *db = NULL;
522 uint16_t incl_num = 0;
523 tBT_UUID bta_uuid = {0};
524
525 if (incl_uuid != NULL) {
526 btc_to_bta_uuid(&bta_uuid, incl_uuid);
527 BTA_GATTC_GetIncludeService(conn_id, start_handle, end_handle, &bta_uuid, &db, &incl_num);
528 } else {
529 BTA_GATTC_GetIncludeService(conn_id, start_handle, end_handle, NULL, &db, &incl_num);
530 }
531
532 if ((status = btc_gattc_check_valid_param((int)incl_num, 0)) != ESP_GATT_OK) {
533 if (db) {
534 osi_free(db);
535 }
536 *count = 0;
537 return status;
538 }else {
539 btc_gattc_fill_gatt_db_conversion(*count, incl_num, ESP_GATT_DB_INCLUDED_SERVICE, 0, (void *)result, db);
540 }
541
542 *count = incl_num;
543 //don't forget to free the db buffer after used.
544 if (db) {
545 osi_free(db);
546 }
547 return ESP_GATT_OK;
548 }
549
btc_ble_gattc_get_attr_count(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)550 esp_gatt_status_t btc_ble_gattc_get_attr_count(uint16_t conn_id,
551 esp_gatt_db_attr_type_t type,
552 uint16_t start_handle,
553 uint16_t end_handle,
554 uint16_t char_handle,
555 uint16_t *count)
556 {
557 if (type == ESP_GATT_DB_ALL) {
558 BTA_GATTC_GetDBSize(conn_id, start_handle, end_handle, count);
559 } else {
560 BTA_GATTC_GetDBSizeByType(conn_id, type, start_handle, end_handle, char_handle, count);
561 }
562
563 return ESP_GATT_OK;
564 }
565
btc_ble_gattc_get_db(uint16_t conn_id,uint16_t start_handle,uint16_t end_handle,esp_gattc_db_elem_t * db,uint16_t * count)566 esp_gatt_status_t btc_ble_gattc_get_db(uint16_t conn_id, uint16_t start_handle, uint16_t end_handle,
567 esp_gattc_db_elem_t *db, uint16_t *count)
568 {
569 btgatt_db_element_t *get_db = NULL;
570 uint16_t num = 0;
571 tBT_UUID bta_uuid;
572 uint16_t db_size = 0;
573 BTA_GATTC_GetGattDb(conn_id, start_handle, end_handle, &get_db, &num);
574
575 if (num == 0) {
576 if (get_db) {
577 osi_free(get_db);
578 }
579 *count = 0;
580 return ESP_GATT_NOT_FOUND;
581 }
582
583 db_size = (*count > num) ? num : (*count);
584 for (int i = 0; i < db_size; i++) {
585 db[i].type = get_db[i].type;
586 db[i].attribute_handle = get_db[i].id;
587 db[i].start_handle = get_db[i].start_handle;
588 db[i].end_handle = get_db[i].end_handle;
589 db[i].properties = get_db[i].properties;
590 btc128_to_bta_uuid(&bta_uuid, get_db[i].uuid.uu);
591 bta_to_btc_uuid(&db[i].uuid, &bta_uuid);
592 }
593 *count = db_size;
594 //don't forget to free the db buffer after used.
595 if (get_db) {
596 osi_free(get_db);
597 }
598 return ESP_GATT_OK;
599 }
600
btc_gattc_read_char(btc_ble_gattc_args_t * arg)601 static void btc_gattc_read_char(btc_ble_gattc_args_t *arg)
602 {
603 BTA_GATTC_ReadCharacteristic(arg->read_char.conn_id, arg->read_char.handle, arg->read_char.auth_req);
604 }
605
btc_gattc_read_multiple_char(btc_ble_gattc_args_t * arg)606 static void btc_gattc_read_multiple_char(btc_ble_gattc_args_t *arg)
607 {
608 tBTA_GATTC_MULTI bta_multi;
609 bta_multi.num_attr = arg->read_multiple.num_attr;
610 memcpy(bta_multi.handles, arg->read_multiple.handles, BTA_GATTC_MULTI_MAX);
611 BTA_GATTC_ReadMultiple(arg->read_multiple.conn_id, &bta_multi, arg->read_multiple.auth_req);
612 }
613
btc_gattc_read_multiple_variable_char(btc_ble_gattc_args_t * arg)614 static void btc_gattc_read_multiple_variable_char(btc_ble_gattc_args_t *arg)
615 {
616 tBTA_GATTC_MULTI bta_multi;
617 bta_multi.num_attr = arg->read_multiple.num_attr;
618 memcpy(bta_multi.handles, arg->read_multiple.handles, BTA_GATTC_MULTI_MAX);
619 BTA_GATTC_ReadMultipleVariable(arg->read_multiple.conn_id, &bta_multi, arg->read_multiple.auth_req);
620 }
621
btc_gattc_read_char_descr(btc_ble_gattc_args_t * arg)622 static void btc_gattc_read_char_descr(btc_ble_gattc_args_t *arg)
623 {
624 BTA_GATTC_ReadCharDescr(arg->read_descr.conn_id, arg->read_descr.handle, arg->read_descr.auth_req);
625 }
626
btc_gattc_read_by_type(btc_ble_gattc_args_t * arg)627 static void btc_gattc_read_by_type(btc_ble_gattc_args_t *arg)
628 {
629 tBT_UUID uuid;
630 btc_to_bta_uuid(&uuid, &(arg->read_by_type.uuid));
631 BTA_GATTC_Read_by_type(arg->read_by_type.conn_id, arg->read_by_type.s_handle, arg->read_by_type.e_handle, &uuid, arg->read_by_type.auth_req);
632 }
633
btc_gattc_write_char(btc_ble_gattc_args_t * arg)634 static void btc_gattc_write_char(btc_ble_gattc_args_t *arg)
635 {
636 BTA_GATTC_WriteCharValue(arg->write_char.conn_id,
637 arg->write_char.handle,
638 arg->write_char.write_type,
639 arg->write_char.value_len,
640 arg->write_char.value,
641 arg->write_char.auth_req);
642 }
643
btc_gattc_write_char_descr(btc_ble_gattc_args_t * arg)644 static void btc_gattc_write_char_descr(btc_ble_gattc_args_t *arg)
645 {
646 tBTA_GATT_UNFMT descr_val;
647
648 descr_val.len = arg->write_descr.value_len;
649 descr_val.p_value = arg->write_descr.value;
650
651 BTA_GATTC_WriteCharDescr(arg->write_descr.conn_id,
652 arg->write_descr.handle,
653 arg->write_descr.write_type, &descr_val,
654 arg->write_descr.auth_req);
655 }
656
btc_gattc_prepare_write(btc_ble_gattc_args_t * arg)657 static void btc_gattc_prepare_write(btc_ble_gattc_args_t *arg)
658 {
659 BTA_GATTC_PrepareWrite(arg->prep_write.conn_id,
660 arg->prep_write.handle,
661 arg->prep_write.offset,
662 arg->prep_write.value_len,
663 arg->prep_write.value,
664 arg->prep_write.auth_req);
665 }
btc_gattc_prepare_write_char_descr(btc_ble_gattc_args_t * arg)666 static void btc_gattc_prepare_write_char_descr(btc_ble_gattc_args_t *arg)
667 {
668 tBTA_GATT_UNFMT descr_val;
669
670 descr_val.len = arg->prep_write_descr.value_len;
671 descr_val.p_value = arg->prep_write_descr.value;
672 BTA_GATTC_PrepareWriteCharDescr(arg->prep_write_descr.conn_id,
673 arg->prep_write_descr.handle,
674 arg->prep_write_descr.offset,
675 &descr_val,
676 arg->prep_write_descr.auth_req);
677 }
678
btc_gattc_execute_write(btc_ble_gattc_args_t * arg)679 static void btc_gattc_execute_write(btc_ble_gattc_args_t *arg)
680 {
681 BTA_GATTC_ExecuteWrite(arg->exec_write.conn_id, arg->exec_write.is_execute);
682 }
683
btc_gattc_reg_for_notify(btc_ble_gattc_args_t * arg)684 static void btc_gattc_reg_for_notify(btc_ble_gattc_args_t *arg)
685 {
686 tBTA_GATT_STATUS status;
687 esp_ble_gattc_cb_param_t param;
688
689 status = BTA_GATTC_RegisterForNotifications(arg->reg_for_notify.gattc_if,
690 arg->reg_for_notify.remote_bda,
691 arg->reg_for_notify.handle);
692
693 memset(¶m, 0, sizeof(esp_ble_gattc_cb_param_t));
694 param.reg_for_notify.status = status;
695 param.reg_for_notify.handle = arg->reg_for_notify.handle;
696 btc_gattc_cb_to_app(ESP_GATTC_REG_FOR_NOTIFY_EVT, arg->reg_for_notify.gattc_if, ¶m);
697 }
698
btc_gattc_unreg_for_notify(btc_ble_gattc_args_t * arg)699 static void btc_gattc_unreg_for_notify(btc_ble_gattc_args_t *arg)
700 {
701 tBTA_GATT_STATUS status;
702 esp_ble_gattc_cb_param_t param;
703
704 status = BTA_GATTC_DeregisterForNotifications(arg->unreg_for_notify.gattc_if,
705 arg->unreg_for_notify.remote_bda,
706 arg->unreg_for_notify.handle);
707
708 memset(¶m, 0, sizeof(esp_ble_gattc_cb_param_t));
709 param.unreg_for_notify.status = status;
710 param.unreg_for_notify.handle = arg->unreg_for_notify.handle;
711 btc_gattc_cb_to_app(ESP_GATTC_UNREG_FOR_NOTIFY_EVT, arg->unreg_for_notify.gattc_if, ¶m);
712 }
713
btc_gattc_call_handler(btc_msg_t * msg)714 void btc_gattc_call_handler(btc_msg_t *msg)
715 {
716 btc_ble_gattc_args_t *arg = (btc_ble_gattc_args_t *)(msg->arg);
717 switch (msg->act) {
718 case BTC_GATTC_ACT_APP_REGISTER:
719 btc_gattc_app_register(arg);
720 break;
721 case BTC_GATTC_ACT_APP_UNREGISTER:
722 btc_gattc_app_unregister(arg);
723 break;
724 case BTC_GATTC_ACT_OPEN:
725 #if (BLE_50_FEATURE_SUPPORT == TRUE)
726 case BTC_GATTC_ACT_AUX_OPEN:
727 #endif // #if (BLE_50_FEATURE_SUPPORT == TRUE)
728 btc_gattc_open(arg);
729 break;
730 case BTC_GATTC_ACT_CLOSE:
731 btc_gattc_close(arg);
732 break;
733 case BTC_GATTC_ACT_CFG_MTU:
734 btc_gattc_cfg_mtu(arg);
735 break;
736 case BTC_GATTC_ACT_SEARCH_SERVICE:
737 btc_gattc_search_service(arg);
738 break;
739 case BTC_GATTC_ACT_READ_CHAR:
740 btc_gattc_read_char(arg);
741 break;
742 case BTC_GATTC_ACT_READ_MULTIPLE_CHAR:
743 btc_gattc_read_multiple_char(arg);
744 break;
745 case BTC_GATTC_ACT_READ_MULTIPLE_VARIABLE_CHAR:
746 btc_gattc_read_multiple_variable_char(arg);
747 break;
748 case BTC_GATTC_ACT_READ_CHAR_DESCR:
749 btc_gattc_read_char_descr(arg);
750 break;
751 case BTC_GATTC_ACT_READ_BY_TYPE:
752 btc_gattc_read_by_type(arg);
753 break;
754 case BTC_GATTC_ACT_WRITE_CHAR:
755 btc_gattc_write_char(arg);
756 break;
757 case BTC_GATTC_ACT_WRITE_CHAR_DESCR:
758 btc_gattc_write_char_descr(arg);
759 break;
760 case BTC_GATTC_ACT_PREPARE_WRITE:
761 btc_gattc_prepare_write(arg);
762 break;
763 case BTC_GATTC_ACT_PREPARE_WRITE_CHAR_DESCR:
764 btc_gattc_prepare_write_char_descr(arg);
765 break;
766 case BTC_GATTC_ACT_EXECUTE_WRITE:
767 btc_gattc_execute_write(arg);
768 break;
769 case BTC_GATTC_ACT_REG_FOR_NOTIFY:
770 btc_gattc_reg_for_notify(arg);
771 break;
772 case BTC_GATTC_ACT_UNREG_FOR_NOTIFY:
773 btc_gattc_unreg_for_notify(arg);
774 break;
775 case BTC_GATTC_ACT_CACHE_REFRESH:
776 BTA_GATTC_Refresh(arg->cache_refresh.remote_bda, true);
777 break;
778 case BTC_GATTC_ACT_CACHE_ASSOC:
779 BTA_GATTC_CacheAssoc(arg->cache_assoc.gattc_if,
780 arg->cache_assoc.src_addr,
781 arg->cache_assoc.assoc_addr,
782 arg->cache_assoc.is_assoc);
783 break;
784 case BTC_GATTC_ATC_CACHE_GET_ADDR_LIST:
785 BTA_GATTC_CacheGetAddrList(arg->get_addr_list.gattc_if);
786 break;
787 case BTC_GATTC_ACT_CACHE_CLEAN:
788 BTA_GATTC_Clean(arg->cache_clean.remote_bda);
789 break;
790 default:
791 BTC_TRACE_ERROR("%s: Unhandled event (%d)!\n", __FUNCTION__, msg->act);
792 break;
793 }
794
795 btc_gattc_arg_deep_free(msg);
796 }
797
btc_gattc_cb_handler(btc_msg_t * msg)798 void btc_gattc_cb_handler(btc_msg_t *msg)
799 {
800 tBTA_GATTC *arg = (tBTA_GATTC *)(msg->arg);
801 esp_gatt_if_t gattc_if = 0;
802 esp_ble_gattc_cb_param_t param = {0};
803
804 memset(¶m, 0, sizeof(esp_ble_gattc_cb_param_t));
805
806 switch (msg->act) {
807 case BTA_GATTC_REG_EVT: {
808 tBTA_GATTC_REG *reg_oper = &arg->reg_oper;
809
810 gattc_if = reg_oper->client_if;
811 param.reg.status = reg_oper->status;
812 param.reg.app_id = reg_oper->app_uuid.uu.uuid16;
813 btc_gattc_cb_to_app(ESP_GATTC_REG_EVT, gattc_if, ¶m);
814 break;
815 }
816 case BTA_GATTC_DEREG_EVT: {
817 tBTA_GATTC_REG *reg_oper = &arg->reg_oper;
818
819 gattc_if = reg_oper->client_if;
820 btc_gattc_cb_to_app(ESP_GATTC_UNREG_EVT, gattc_if, NULL);
821 break;
822 }
823 case BTA_GATTC_READ_CHAR_EVT: {
824 set_read_value(&gattc_if, ¶m, &arg->read);
825 btc_gattc_cb_to_app(ESP_GATTC_READ_CHAR_EVT, gattc_if, ¶m);
826 break;
827 }
828 case BTA_GATTC_WRITE_CHAR_EVT:
829 case BTA_GATTC_PREP_WRITE_EVT: {
830 tBTA_GATTC_WRITE *write = &arg->write;
831 uint32_t ret_evt = (msg->act == BTA_GATTC_WRITE_CHAR_EVT) ?
832 ESP_GATTC_WRITE_CHAR_EVT : ESP_GATTC_PREP_WRITE_EVT;
833
834 gattc_if = BTC_GATT_GET_GATT_IF(write->conn_id);
835 param.write.conn_id = BTC_GATT_GET_CONN_ID(write->conn_id);
836 param.write.status = write->status;
837 param.write.handle = write->handle;
838 param.write.offset = write->offset;
839 btc_gattc_cb_to_app(ret_evt, gattc_if, ¶m);
840 break;
841 }
842
843 case BTA_GATTC_EXEC_EVT: {
844 tBTA_GATTC_EXEC_CMPL *exec_cmpl = &arg->exec_cmpl;
845
846 gattc_if = BTC_GATT_GET_GATT_IF(exec_cmpl->conn_id);
847 param.exec_cmpl.conn_id = BTC_GATT_GET_CONN_ID(exec_cmpl->conn_id);
848 param.exec_cmpl.status = exec_cmpl->status;
849 btc_gattc_cb_to_app(ESP_GATTC_EXEC_EVT, gattc_if, ¶m);
850 break;
851 }
852
853 case BTA_GATTC_SEARCH_CMPL_EVT: {
854 tBTA_GATTC_SEARCH_CMPL *search_cmpl = &arg->search_cmpl;
855
856 gattc_if = BTC_GATT_GET_GATT_IF(search_cmpl->conn_id);
857 param.search_cmpl.conn_id = BTC_GATT_GET_CONN_ID(search_cmpl->conn_id);
858 param.search_cmpl.status = search_cmpl->status;
859 param.search_cmpl.searched_service_source = search_cmpl->searched_service_source;
860 btc_gattc_cb_to_app(ESP_GATTC_SEARCH_CMPL_EVT, gattc_if, ¶m);
861 break;
862 }
863 case BTA_GATTC_SEARCH_RES_EVT: {
864 tBTA_GATTC_SRVC_RES *srvc_res = &arg->srvc_res;
865
866 gattc_if = BTC_GATT_GET_GATT_IF(srvc_res->conn_id);
867 param.search_res.conn_id = BTC_GATT_GET_CONN_ID(srvc_res->conn_id);
868 param.search_res.start_handle = srvc_res->start_handle;
869 param.search_res.end_handle = srvc_res->end_handle;
870 param.search_res.is_primary = srvc_res->is_primary;
871 bta_to_btc_gatt_id(¶m.search_res.srvc_id, &srvc_res->service_uuid);
872 btc_gattc_cb_to_app(ESP_GATTC_SEARCH_RES_EVT, gattc_if, ¶m);
873 break;
874 }
875 case BTA_GATTC_READ_DESCR_EVT: {
876 set_read_value(&gattc_if, ¶m, &arg->read);
877 btc_gattc_cb_to_app(ESP_GATTC_READ_DESCR_EVT, gattc_if, ¶m);
878 break;
879 }
880 case BTA_GATTC_READ_MULTIPLE_EVT: {
881 set_read_value(&gattc_if, ¶m, &arg->read);
882 btc_gattc_cb_to_app(ESP_GATTC_READ_MULTIPLE_EVT, gattc_if, ¶m);
883 break;
884 }
885 case BTA_GATTC_READ_MULTI_VAR_EVT: {
886 set_read_value(&gattc_if, ¶m, &arg->read);
887 btc_gattc_cb_to_app(ESP_GATTC_READ_MULTI_VAR_EVT, gattc_if, ¶m);
888 break;
889 }
890 case BTA_GATTC_WRITE_DESCR_EVT: {
891 tBTA_GATTC_WRITE *write = &arg->write;
892
893 gattc_if = BTC_GATT_GET_GATT_IF(write->conn_id);
894 param.write.conn_id = BTC_GATT_GET_CONN_ID(write->conn_id);
895 param.write.status = write->status;
896 param.write.handle = write->handle;
897 btc_gattc_cb_to_app(ESP_GATTC_WRITE_DESCR_EVT, gattc_if, ¶m);
898 break;
899 }
900 case BTA_GATTC_NOTIF_EVT: {
901 tBTA_GATTC_NOTIFY *notify = &arg->notify;
902
903 gattc_if = BTC_GATT_GET_GATT_IF(notify->conn_id);
904 param.notify.conn_id = BTC_GATT_GET_CONN_ID(notify->conn_id);
905 memcpy(param.notify.remote_bda, notify->bda, sizeof(esp_bd_addr_t));
906 param.notify.handle = notify->handle;
907 param.notify.is_notify = (notify->is_notify == TRUE) ? true : false;
908 param.notify.value_len = (notify->len > ESP_GATT_MAX_ATTR_LEN) ? \
909 ESP_GATT_MAX_ATTR_LEN : notify->len;
910 param.notify.value = notify->value;
911
912 if (notify->is_notify == FALSE) {
913 BTA_GATTC_SendIndConfirm(notify->conn_id, notify->handle);
914 }
915
916 btc_gattc_cb_to_app(ESP_GATTC_NOTIFY_EVT, gattc_if, ¶m);
917 break;
918 }
919 case BTA_GATTC_OPEN_EVT: {
920 tBTA_GATTC_OPEN *open = &arg->open;
921
922 gattc_if = open->client_if;
923 param.open.status = open->status;
924 param.open.conn_id = BTC_GATT_GET_CONN_ID(open->conn_id);
925 memcpy(param.open.remote_bda, open->remote_bda, sizeof(esp_bd_addr_t));
926 param.open.mtu = open->mtu;
927 btc_gattc_cb_to_app(ESP_GATTC_OPEN_EVT, gattc_if, ¶m);
928 break;
929 }
930 case BTA_GATTC_CONNECT_EVT: {
931 tBTA_GATTC_CONNECT *connect = &arg->connect;
932 #if (SMP_INCLUDED == TRUE)
933 bt_bdaddr_t bt_addr;
934
935 memcpy(bt_addr.address, connect->remote_bda, sizeof(bt_addr.address));
936 if (btc_storage_update_active_device(&bt_addr)) {
937 BTC_TRACE_EVENT("Device: %02x:%02x:%02x:%02x:%02x:%02x, is not in bond list",
938 bt_addr.address[0], bt_addr.address[1],
939 bt_addr.address[2], bt_addr.address[3],
940 bt_addr.address[4], bt_addr.address[5]);
941 }
942 #endif ///SMP_INCLUDED == TRUE
943 gattc_if = connect->client_if;
944 param.connect.conn_id = BTC_GATT_GET_CONN_ID(connect->conn_id);
945 param.connect.link_role = connect->link_role;
946 memcpy(param.connect.remote_bda, connect->remote_bda, sizeof(esp_bd_addr_t));
947 param.connect.conn_params.interval = connect->conn_params.interval;
948 param.connect.conn_params.latency = connect->conn_params.latency;
949 param.connect.conn_params.timeout = connect->conn_params.timeout;
950 param.connect.ble_addr_type = connect->ble_addr_type;
951 param.connect.conn_handle = connect->conn_handle;
952 btc_gattc_cb_to_app(ESP_GATTC_CONNECT_EVT, gattc_if, ¶m);
953 break;
954 }
955 case BTA_GATTC_CLOSE_EVT: {
956 tBTA_GATTC_CLOSE *close = &arg->close;
957
958 // Free gattc clcb in BTC task to avoid race condition
959 bta_gattc_clcb_dealloc_by_conn_id(close->conn_id);
960 gattc_if = close->client_if;
961 param.close.status = close->status;
962 param.close.conn_id = BTC_GATT_GET_CONN_ID(close->conn_id);
963 memcpy(param.close.remote_bda, close->remote_bda, sizeof(esp_bd_addr_t));
964 param.close.reason = close->reason;
965 btc_gattc_cb_to_app(ESP_GATTC_CLOSE_EVT, gattc_if, ¶m);
966 break;
967 }
968 case BTA_GATTC_DISCONNECT_EVT: {
969 tBTA_GATTC_DISCONNECT *disconnect = &arg->disconnect;
970
971 gattc_if = disconnect->client_if;
972 param.disconnect.reason = disconnect->reason;
973 param.disconnect.conn_id = BTC_GATT_GET_CONN_ID(disconnect->conn_id);
974 memcpy(param.disconnect.remote_bda, disconnect->remote_bda, sizeof(esp_bd_addr_t));
975 btc_gattc_cb_to_app(ESP_GATTC_DISCONNECT_EVT, gattc_if, ¶m);
976 break;
977 }
978 case BTA_GATTC_CFG_MTU_EVT: {
979 tBTA_GATTC_CFG_MTU *cfg_mtu = &arg->cfg_mtu;
980
981 gattc_if = BTC_GATT_GET_GATT_IF(cfg_mtu->conn_id);
982 param.cfg_mtu.conn_id = BTC_GATT_GET_CONN_ID(cfg_mtu->conn_id);
983 param.cfg_mtu.status = cfg_mtu->status;
984 param.cfg_mtu.mtu = cfg_mtu->mtu;
985 btc_gattc_cb_to_app(ESP_GATTC_CFG_MTU_EVT, gattc_if, ¶m);
986 break;
987 }
988
989 case BTA_GATTC_ACL_EVT: {
990 /* Currently, this event will never happen */
991 break;
992 }
993 case BTA_GATTC_CANCEL_OPEN_EVT: {
994 /* Currently, this event will never happen */
995 break;
996 }
997 case BTA_GATTC_CONGEST_EVT: {
998 tBTA_GATTC_CONGEST *congest = &arg->congest;
999
1000 gattc_if = BTC_GATT_GET_GATT_IF(congest->conn_id);
1001 param.congest.conn_id = BTC_GATT_GET_CONN_ID(congest->conn_id);
1002 param.congest.congested = (congest->congested == TRUE) ? true : false;
1003 btc_gattc_cb_to_app(ESP_GATTC_CONGEST_EVT, gattc_if, ¶m);
1004 break;
1005 }
1006 case BTA_GATTC_SRVC_CHG_EVT: {
1007 tBTA_GATTC_SERVICE_CHANGE *srvc_change = &arg->srvc_chg;
1008 gattc_if = BTC_GATT_GET_GATT_IF(srvc_change->conn_id);
1009 memcpy(param.srvc_chg.remote_bda, srvc_change->remote_bda, sizeof(esp_bd_addr_t));
1010 btc_gattc_cb_to_app(ESP_GATTC_SRVC_CHG_EVT, gattc_if, ¶m);
1011 break;
1012 }
1013 case BTA_GATTC_QUEUE_FULL_EVT: {
1014 tBTA_GATTC_QUEUE_FULL *queue_full = &arg->queue_full;
1015 gattc_if = BTC_GATT_GET_GATT_IF(queue_full->conn_id);
1016 param.queue_full.conn_id = BTC_GATT_GET_CONN_ID(queue_full->conn_id);
1017 param.queue_full.status = arg->status;
1018 param.queue_full.is_full = queue_full->is_full;
1019 btc_gattc_cb_to_app(ESP_GATTC_QUEUE_FULL_EVT, gattc_if, ¶m);
1020 break;
1021 }
1022 case BTA_GATTC_ASSOC_EVT: {
1023 gattc_if = arg->set_assoc.client_if;
1024 param.set_assoc_cmp.status = arg->set_assoc.status;
1025 btc_gattc_cb_to_app(ESP_GATTC_SET_ASSOC_EVT, gattc_if, ¶m);
1026 break;
1027 }
1028 case BTA_GATTC_GET_ADDR_LIST_EVT: {
1029 gattc_if = arg->get_addr_list.client_if;
1030 param.get_addr_list.status = arg->get_addr_list.status;
1031 param.get_addr_list.num_addr = arg->get_addr_list.num_addr;
1032 param.get_addr_list.addr_list = arg->get_addr_list.bda_list;
1033 btc_gattc_cb_to_app(ESP_GATTC_GET_ADDR_LIST_EVT, gattc_if, ¶m);
1034 break;
1035 }
1036 case BTA_GATTC_DIS_SRVC_CMPL_EVT:
1037 gattc_if = BTC_GATT_GET_GATT_IF(arg->dis_cmpl.conn_id);
1038 param.dis_srvc_cmpl.status = arg->dis_cmpl.status;
1039 param.dis_srvc_cmpl.conn_id = BTC_GATT_GET_CONN_ID(arg->dis_cmpl.conn_id);
1040 btc_gattc_cb_to_app(ESP_GATTC_DIS_SRVC_CMPL_EVT, gattc_if, ¶m);
1041 break;
1042 default:
1043 BTC_TRACE_DEBUG("%s: Unhandled event (%d)!", __FUNCTION__, msg->act);
1044 break;
1045 }
1046
1047 // free the deep-copied data
1048 btc_gattc_free_req_data(msg);
1049 }
1050
btc_gattc_congest_callback(tBTA_GATTC * param)1051 void btc_gattc_congest_callback(tBTA_GATTC *param)
1052 {
1053 esp_ble_gattc_cb_param_t esp_param = {0};
1054 memset(&esp_param, 0, sizeof(esp_ble_gattc_cb_param_t));
1055
1056 uint8_t gattc_if = BTC_GATT_GET_GATT_IF(param->congest.conn_id);
1057 esp_param.congest.conn_id = BTC_GATT_GET_CONN_ID(param->congest.conn_id);
1058 esp_param.congest.congested = (param->congest.congested == TRUE) ? true : false;
1059 btc_gattc_cb_to_app(ESP_GATTC_CONGEST_EVT, gattc_if, &esp_param);
1060
1061 }
1062
1063 #endif ///GATTC_INCLUDED == TRUE
1064