1 /* 2 * Copyright (C) 2014 BlueKitchen GmbH 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the copyright holders nor the names of 14 * contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 4. Any redistribution, use, or modification is done solely for 17 * personal benefit and not for any commercial purpose or for 18 * monetary gain. 19 * 20 * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN 24 * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * Please inquire about commercial licensing options at 34 * [email protected] 35 * 36 */ 37 38 #define BTSTACK_FILE__ "gatt_client.c" 39 40 #include <stdint.h> 41 #include <string.h> 42 #include <stddef.h> 43 44 #include "btstack_config.h" 45 46 #include "ble/att_dispatch.h" 47 #include "ble/att_db.h" 48 #include "ble/gatt_client.h" 49 #include "ble/le_device_db.h" 50 #include "ble/sm.h" 51 #include "btstack_debug.h" 52 #include "btstack_event.h" 53 #include "btstack_memory.h" 54 #include "btstack_run_loop.h" 55 #include "btstack_util.h" 56 #include "hci.h" 57 #include "hci_dump.h" 58 #include "l2cap.h" 59 #include "classic/sdp_client.h" 60 #include "bluetooth_gatt.h" 61 #include "bluetooth_sdp.h" 62 #include "classic/sdp_util.h" 63 64 static btstack_linked_list_t gatt_client_connections; 65 static btstack_linked_list_t gatt_client_value_listeners; 66 static btstack_packet_callback_registration_t hci_event_callback_registration; 67 static btstack_packet_callback_registration_t sm_event_callback_registration; 68 69 // GATT Client Configuration 70 static bool gatt_client_mtu_exchange_enabled; 71 static gap_security_level_t gatt_client_required_security_level; 72 73 static void gatt_client_att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size); 74 static void gatt_client_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 75 static void gatt_client_report_error_if_pending(gatt_client_t *gatt_client, uint8_t att_error_code); 76 77 #ifdef ENABLE_LE_SIGNED_WRITE 78 static void att_signed_write_handle_cmac_result(uint8_t hash[8]); 79 #endif 80 81 #ifdef ENABLE_GATT_OVER_EATT 82 static bool gatt_client_le_enhanced_handle_can_send_query(gatt_client_t * gatt_client); 83 #endif 84 85 void gatt_client_init(void){ 86 gatt_client_connections = NULL; 87 88 // default configuration 89 gatt_client_mtu_exchange_enabled = true; 90 gatt_client_required_security_level = LEVEL_0; 91 92 // register for HCI Events 93 hci_event_callback_registration.callback = &gatt_client_event_packet_handler; 94 hci_add_event_handler(&hci_event_callback_registration); 95 96 // register for SM Events 97 sm_event_callback_registration.callback = &gatt_client_event_packet_handler; 98 sm_add_event_handler(&sm_event_callback_registration); 99 100 // and ATT Client PDUs 101 att_dispatch_register_client(gatt_client_att_packet_handler); 102 } 103 104 void gatt_client_set_required_security_level(gap_security_level_t level){ 105 gatt_client_required_security_level = level; 106 } 107 108 static gatt_client_t * gatt_client_for_timer(btstack_timer_source_t * ts){ 109 btstack_linked_list_iterator_t it; 110 btstack_linked_list_iterator_init(&it, &gatt_client_connections); 111 while (btstack_linked_list_iterator_has_next(&it)){ 112 gatt_client_t * gatt_client = (gatt_client_t *) btstack_linked_list_iterator_next(&it); 113 if (&gatt_client->gc_timeout == ts) { 114 return gatt_client; 115 } 116 } 117 return NULL; 118 } 119 120 static void gatt_client_timeout_handler(btstack_timer_source_t * timer){ 121 gatt_client_t * gatt_client = gatt_client_for_timer(timer); 122 if (gatt_client == NULL) return; 123 log_info("GATT client timeout handle, handle 0x%02x", gatt_client->con_handle); 124 gatt_client_report_error_if_pending(gatt_client, ATT_ERROR_TIMEOUT); 125 } 126 127 static void gatt_client_timeout_start(gatt_client_t * gatt_client){ 128 log_info("GATT client timeout start, handle 0x%02x", gatt_client->con_handle); 129 btstack_run_loop_remove_timer(&gatt_client->gc_timeout); 130 btstack_run_loop_set_timer_handler(&gatt_client->gc_timeout, gatt_client_timeout_handler); 131 btstack_run_loop_set_timer(&gatt_client->gc_timeout, 30000); // 30 seconds sm timeout 132 btstack_run_loop_add_timer(&gatt_client->gc_timeout); 133 } 134 135 static void gatt_client_timeout_stop(gatt_client_t * gatt_client){ 136 log_info("GATT client timeout stop, handle 0x%02x", gatt_client->con_handle); 137 btstack_run_loop_remove_timer(&gatt_client->gc_timeout); 138 } 139 140 static gap_security_level_t gatt_client_le_security_level_for_connection(hci_con_handle_t con_handle){ 141 uint8_t encryption_key_size = gap_encryption_key_size(con_handle); 142 if (encryption_key_size == 0) return LEVEL_0; 143 144 bool authenticated = gap_authenticated(con_handle); 145 if (!authenticated) return LEVEL_2; 146 147 return encryption_key_size == 16 ? LEVEL_4 : LEVEL_3; 148 } 149 150 static gatt_client_t * gatt_client_get_context_for_handle(uint16_t handle){ 151 btstack_linked_item_t *it; 152 for (it = (btstack_linked_item_t *) gatt_client_connections; it != NULL; it = it->next){ 153 gatt_client_t * gatt_client = (gatt_client_t *) it; 154 if (gatt_client->con_handle == handle){ 155 return gatt_client; 156 } 157 } 158 return NULL; 159 } 160 161 162 // @return gatt_client context 163 // returns existing one, or tries to setup new one 164 static uint8_t gatt_client_provide_context_for_handle(hci_con_handle_t con_handle, gatt_client_t ** out_gatt_client){ 165 gatt_client_t * gatt_client = gatt_client_get_context_for_handle(con_handle); 166 167 if (gatt_client != NULL){ 168 *out_gatt_client = gatt_client; 169 return ERROR_CODE_SUCCESS; 170 } 171 172 // bail if no such hci connection 173 hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 174 if (hci_connection == NULL){ 175 log_error("No connection for handle 0x%04x", con_handle); 176 *out_gatt_client = NULL; 177 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 178 } 179 180 gatt_client = btstack_memory_gatt_client_get(); 181 if (gatt_client == NULL){ 182 *out_gatt_client = NULL; 183 return ERROR_CODE_MEMORY_CAPACITY_EXCEEDED; 184 } 185 // init state 186 gatt_client->bearer_type = ATT_BEARER_UNENHANCED_LE; 187 gatt_client->con_handle = con_handle; 188 gatt_client->mtu = ATT_DEFAULT_MTU; 189 gatt_client->security_level = gatt_client_le_security_level_for_connection(con_handle); 190 if (gatt_client_mtu_exchange_enabled){ 191 gatt_client->mtu_state = SEND_MTU_EXCHANGE; 192 } else { 193 gatt_client->mtu_state = MTU_AUTO_EXCHANGE_DISABLED; 194 } 195 gatt_client->gatt_client_state = P_READY; 196 #ifdef ENABLE_GATT_OVER_EATT 197 gatt_client->eatt_state = GATT_CLIENT_EATT_IDLE; 198 #endif 199 btstack_linked_list_add(&gatt_client_connections, (btstack_linked_item_t*)gatt_client); 200 201 // get unenhanced att bearer state 202 if (hci_connection->att_connection.mtu_exchanged){ 203 gatt_client->mtu = hci_connection->att_connection.mtu; 204 gatt_client->mtu_state = MTU_EXCHANGED; 205 } 206 *out_gatt_client = gatt_client; 207 return ERROR_CODE_SUCCESS; 208 } 209 210 static bool is_ready(gatt_client_t * gatt_client){ 211 return gatt_client->gatt_client_state == P_READY; 212 } 213 214 static uint8_t gatt_client_provide_context_for_request(hci_con_handle_t con_handle, gatt_client_t ** out_gatt_client){ 215 gatt_client_t * gatt_client = NULL; 216 uint8_t status = gatt_client_provide_context_for_handle(con_handle, &gatt_client); 217 if (status != ERROR_CODE_SUCCESS){ 218 return status; 219 } 220 221 #ifdef ENABLE_GATT_OVER_EATT 222 if (gatt_client->eatt_state == GATT_CLIENT_EATT_READY){ 223 btstack_linked_list_iterator_t it; 224 gatt_client_t * eatt_client = NULL; 225 // find free eatt client 226 btstack_linked_list_iterator_init(&it, &gatt_client->eatt_clients); 227 while (btstack_linked_list_iterator_has_next(&it)){ 228 gatt_client_t * client = (gatt_client_t *) btstack_linked_list_iterator_next(&it); 229 if (client->gatt_client_state == P_READY){ 230 eatt_client = client; 231 break; 232 } 233 } 234 if (eatt_client == NULL){ 235 return ERROR_CODE_COMMAND_DISALLOWED; 236 } 237 gatt_client = eatt_client; 238 } 239 #endif 240 241 if (is_ready(gatt_client) == 0){ 242 return GATT_CLIENT_IN_WRONG_STATE; 243 } 244 245 gatt_client_timeout_start(gatt_client); 246 247 *out_gatt_client = gatt_client; 248 249 return status; 250 } 251 252 int gatt_client_is_ready(hci_con_handle_t con_handle){ 253 gatt_client_t * gatt_client; 254 uint8_t status = gatt_client_provide_context_for_handle(con_handle, &gatt_client); 255 if (status != ERROR_CODE_SUCCESS){ 256 return 0; 257 } 258 return is_ready(gatt_client) ? 1 : 0; 259 } 260 261 void gatt_client_mtu_enable_auto_negotiation(uint8_t enabled){ 262 gatt_client_mtu_exchange_enabled = enabled != 0; 263 } 264 265 uint8_t gatt_client_get_mtu(hci_con_handle_t con_handle, uint16_t * mtu){ 266 gatt_client_t * gatt_client; 267 uint8_t status = gatt_client_provide_context_for_handle(con_handle, &gatt_client); 268 if (status != ERROR_CODE_SUCCESS){ 269 return status; 270 } 271 272 if ((gatt_client->mtu_state == MTU_EXCHANGED) || (gatt_client->mtu_state == MTU_AUTO_EXCHANGE_DISABLED)){ 273 *mtu = gatt_client->mtu; 274 return ERROR_CODE_SUCCESS; 275 } 276 *mtu = ATT_DEFAULT_MTU; 277 return GATT_CLIENT_IN_WRONG_STATE; 278 } 279 280 #ifdef ENABLE_GATT_OVER_CLASSIC 281 // TODO: re-use single buffer for all eatt channels 282 static uint8_t gatt_client_le_enhanced_request_buffer[512]; 283 #endif 284 285 static uint8_t *gatt_client_reserve_request_buffer(gatt_client_t *gatt_client) { 286 switch (gatt_client->bearer_type){ 287 #ifdef ENABLE_GATT_OVER_CLASSIC 288 case ATT_BEARER_UNENHANCED_CLASSIC: 289 #endif 290 case ATT_BEARER_UNENHANCED_LE: 291 l2cap_reserve_packet_buffer(); 292 return l2cap_get_outgoing_buffer(); 293 #ifdef ENABLE_GATT_OVER_EATT 294 case ATT_BEARER_ENHANCED_LE: 295 return gatt_client->eatt_storage_buffer; 296 #endif 297 default: 298 btstack_unreachable(); 299 break; 300 } 301 return NULL; 302 } 303 304 // precondition: can_send_packet_now == TRUE 305 static uint8_t gatt_client_send(gatt_client_t * gatt_client, uint16_t len){ 306 switch (gatt_client->bearer_type){ 307 case ATT_BEARER_UNENHANCED_LE: 308 return l2cap_send_prepared_connectionless(gatt_client->con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, len); 309 #ifdef ENABLE_GATT_OVER_CLASSIC 310 case ATT_BEARER_UNENHANCED_CLASSIC: 311 return l2cap_send_prepared(gatt_client->l2cap_cid, len); 312 #endif 313 #ifdef ENABLE_GATT_OVER_EATT 314 case ATT_BEARER_ENHANCED_LE: 315 return l2cap_send(gatt_client->l2cap_cid, gatt_client->eatt_storage_buffer, len); 316 #endif 317 default: 318 btstack_unreachable(); 319 return ERROR_CODE_HARDWARE_FAILURE; 320 } 321 } 322 323 // precondition: can_send_packet_now == TRUE 324 static uint8_t att_confirmation(gatt_client_t * gatt_client) { 325 uint8_t *request = gatt_client_reserve_request_buffer(gatt_client); 326 327 request[0] = ATT_HANDLE_VALUE_CONFIRMATION; 328 329 return gatt_client_send(gatt_client, 1); 330 } 331 332 // precondition: can_send_packet_now == TRUE 333 static uint8_t att_find_information_request(gatt_client_t *gatt_client, uint8_t request_type, uint16_t start_handle, 334 uint16_t end_handle) { 335 uint8_t *request = gatt_client_reserve_request_buffer(gatt_client); 336 337 request[0] = request_type; 338 little_endian_store_16(request, 1, start_handle); 339 little_endian_store_16(request, 3, end_handle); 340 341 return gatt_client_send(gatt_client, 5); 342 } 343 344 // precondition: can_send_packet_now == TRUE 345 static uint8_t 346 att_find_by_type_value_request(gatt_client_t *gatt_client, uint8_t request_type, uint16_t attribute_group_type, 347 uint16_t start_handle, uint16_t end_handle, uint8_t *value, uint16_t value_size) { 348 uint8_t *request = gatt_client_reserve_request_buffer(gatt_client); 349 request[0] = request_type; 350 351 little_endian_store_16(request, 1, start_handle); 352 little_endian_store_16(request, 3, end_handle); 353 little_endian_store_16(request, 5, attribute_group_type); 354 (void)memcpy(&request[7], value, value_size); 355 356 return gatt_client_send(gatt_client, 7u + value_size); 357 } 358 359 // precondition: can_send_packet_now == TRUE 360 static uint8_t 361 att_read_by_type_or_group_request_for_uuid16(gatt_client_t *gatt_client, uint8_t request_type, uint16_t uuid16, 362 uint16_t start_handle, uint16_t end_handle) { 363 uint8_t *request = gatt_client_reserve_request_buffer(gatt_client); 364 365 request[0] = request_type; 366 little_endian_store_16(request, 1, start_handle); 367 little_endian_store_16(request, 3, end_handle); 368 little_endian_store_16(request, 5, uuid16); 369 370 return gatt_client_send(gatt_client, 7); 371 } 372 373 // precondition: can_send_packet_now == TRUE 374 static uint8_t 375 att_read_by_type_or_group_request_for_uuid128(gatt_client_t *gatt_client, uint8_t request_type, const uint8_t *uuid128, 376 uint16_t start_handle, uint16_t end_handle) { 377 uint8_t *request = gatt_client_reserve_request_buffer(gatt_client); 378 379 request[0] = request_type; 380 little_endian_store_16(request, 1, start_handle); 381 little_endian_store_16(request, 3, end_handle); 382 reverse_128(uuid128, &request[5]); 383 384 return gatt_client_send(gatt_client, 21); 385 } 386 387 // precondition: can_send_packet_now == TRUE 388 static uint8_t att_read_request(gatt_client_t *gatt_client, uint8_t request_type, uint16_t attribute_handle) { 389 uint8_t *request = gatt_client_reserve_request_buffer(gatt_client); 390 391 request[0] = request_type; 392 little_endian_store_16(request, 1, attribute_handle); 393 394 return gatt_client_send(gatt_client, 3); 395 } 396 397 // precondition: can_send_packet_now == TRUE 398 static uint8_t att_read_blob_request(gatt_client_t *gatt_client, uint8_t request_type, uint16_t attribute_handle, 399 uint16_t value_offset) { 400 uint8_t *request = gatt_client_reserve_request_buffer(gatt_client); 401 402 request[0] = request_type; 403 little_endian_store_16(request, 1, attribute_handle); 404 little_endian_store_16(request, 3, value_offset); 405 406 return gatt_client_send(gatt_client, 5); 407 } 408 409 static uint8_t 410 att_read_multiple_request_with_opcode(gatt_client_t *gatt_client, uint16_t num_value_handles, uint16_t *value_handles, uint8_t opcode) { 411 uint8_t *request = gatt_client_reserve_request_buffer(gatt_client); 412 413 request[0] = opcode; 414 uint16_t i; 415 uint16_t offset = 1; 416 for (i=0;i<num_value_handles;i++){ 417 little_endian_store_16(request, offset, value_handles[i]); 418 offset += 2; 419 } 420 421 return gatt_client_send(gatt_client, offset); 422 } 423 424 static uint8_t 425 att_read_multiple_request(gatt_client_t *gatt_client, uint16_t num_value_handles, uint16_t *value_handles) { 426 return att_read_multiple_request_with_opcode(gatt_client, num_value_handles, value_handles, ATT_READ_MULTIPLE_REQUEST); 427 } 428 429 #ifdef ENABLE_GATT_OVER_EATT 430 static uint8_t 431 att_read_multiple_variable_request(gatt_client_t *gatt_client, uint16_t num_value_handles, uint16_t *value_handles) { 432 return att_read_multiple_request_with_opcode(gatt_client, num_value_handles, value_handles, ATT_READ_MULTIPLE_VARIABLE_REQ); 433 } 434 #endif 435 436 #ifdef ENABLE_LE_SIGNED_WRITE 437 // precondition: can_send_packet_now == TRUE 438 static uint8_t att_signed_write_request(gatt_client_t *gatt_client, uint16_t request_type, uint16_t attribute_handle, 439 uint16_t value_length, uint8_t *value, uint32_t sign_counter, uint8_t sgn[8]) { 440 uint8_t *request = gatt_client_reserve_request_buffer(gatt_client); 441 442 request[0] = request_type; 443 little_endian_store_16(request, 1, attribute_handle); 444 (void)memcpy(&request[3], value, value_length); 445 little_endian_store_32(request, 3 + value_length, sign_counter); 446 reverse_64(sgn, &request[3 + value_length + 4]); 447 448 return gatt_client_send(gatt_client, 3 + value_length + 12); 449 } 450 #endif 451 452 // precondition: can_send_packet_now == TRUE 453 static uint8_t 454 att_write_request(gatt_client_t *gatt_client, uint8_t request_type, uint16_t attribute_handle, uint16_t value_length, 455 uint8_t *value) { 456 uint8_t *request = gatt_client_reserve_request_buffer(gatt_client); 457 458 request[0] = request_type; 459 little_endian_store_16(request, 1, attribute_handle); 460 (void)memcpy(&request[3], value, value_length); 461 462 return gatt_client_send(gatt_client, 3u + value_length); 463 } 464 465 // precondition: can_send_packet_now == TRUE 466 static uint8_t att_execute_write_request(gatt_client_t *gatt_client, uint8_t request_type, uint8_t execute_write) { 467 uint8_t *request = gatt_client_reserve_request_buffer(gatt_client); 468 469 request[0] = request_type; 470 request[1] = execute_write; 471 472 return gatt_client_send(gatt_client, 2); 473 } 474 475 // precondition: can_send_packet_now == TRUE 476 static uint8_t att_prepare_write_request(gatt_client_t *gatt_client, uint8_t request_type, uint16_t attribute_handle, 477 uint16_t value_offset, uint16_t blob_length, uint8_t *value) { 478 uint8_t *request = gatt_client_reserve_request_buffer(gatt_client); 479 480 request[0] = request_type; 481 little_endian_store_16(request, 1, attribute_handle); 482 little_endian_store_16(request, 3, value_offset); 483 (void)memcpy(&request[5], &value[value_offset], blob_length); 484 485 return gatt_client_send(gatt_client, 5u + blob_length); 486 } 487 488 static uint8_t att_exchange_mtu_request(gatt_client_t *gatt_client) { 489 uint8_t *request = gatt_client_reserve_request_buffer(gatt_client); 490 491 request[0] = ATT_EXCHANGE_MTU_REQUEST; 492 uint16_t mtu = l2cap_max_le_mtu(); 493 little_endian_store_16(request, 1, mtu); 494 495 return gatt_client_send(gatt_client, 3); 496 } 497 498 static uint16_t write_blob_length(gatt_client_t * gatt_client){ 499 uint16_t max_blob_length = gatt_client->mtu - 5u; 500 if (gatt_client->attribute_offset >= gatt_client->attribute_length) { 501 return 0; 502 } 503 uint16_t rest_length = gatt_client->attribute_length - gatt_client->attribute_offset; 504 if (max_blob_length > rest_length){ 505 return rest_length; 506 } 507 return max_blob_length; 508 } 509 510 static void send_gatt_services_request(gatt_client_t *gatt_client){ 511 att_read_by_type_or_group_request_for_uuid16(gatt_client, ATT_READ_BY_GROUP_TYPE_REQUEST, 512 gatt_client->uuid16, gatt_client->start_group_handle, 513 gatt_client->end_group_handle); 514 } 515 516 static void send_gatt_by_uuid_request(gatt_client_t *gatt_client, uint16_t attribute_group_type){ 517 if (gatt_client->uuid16){ 518 uint8_t uuid16[2]; 519 little_endian_store_16(uuid16, 0, gatt_client->uuid16); 520 att_find_by_type_value_request(gatt_client, ATT_FIND_BY_TYPE_VALUE_REQUEST, attribute_group_type, 521 gatt_client->start_group_handle, gatt_client->end_group_handle, uuid16, 2); 522 return; 523 } 524 uint8_t uuid128[16]; 525 reverse_128(gatt_client->uuid128, uuid128); 526 att_find_by_type_value_request(gatt_client, ATT_FIND_BY_TYPE_VALUE_REQUEST, attribute_group_type, 527 gatt_client->start_group_handle, gatt_client->end_group_handle, uuid128, 16); 528 } 529 530 static void send_gatt_services_by_uuid_request(gatt_client_t *gatt_client){ 531 send_gatt_by_uuid_request(gatt_client, GATT_PRIMARY_SERVICE_UUID); 532 } 533 534 static void send_gatt_included_service_uuid_request(gatt_client_t *gatt_client){ 535 att_read_request(gatt_client, ATT_READ_REQUEST, gatt_client->query_start_handle); 536 } 537 538 static void send_gatt_included_service_request(gatt_client_t *gatt_client){ 539 att_read_by_type_or_group_request_for_uuid16(gatt_client, ATT_READ_BY_TYPE_REQUEST, 540 GATT_INCLUDE_SERVICE_UUID, gatt_client->start_group_handle, 541 gatt_client->end_group_handle); 542 } 543 544 static void send_gatt_characteristic_request(gatt_client_t *gatt_client){ 545 att_read_by_type_or_group_request_for_uuid16(gatt_client, ATT_READ_BY_TYPE_REQUEST, 546 GATT_CHARACTERISTICS_UUID, gatt_client->start_group_handle, 547 gatt_client->end_group_handle); 548 } 549 550 static void send_gatt_characteristic_descriptor_request(gatt_client_t *gatt_client){ 551 att_find_information_request(gatt_client, ATT_FIND_INFORMATION_REQUEST, gatt_client->start_group_handle, 552 gatt_client->end_group_handle); 553 } 554 555 static void send_gatt_read_characteristic_value_request(gatt_client_t *gatt_client){ 556 att_read_request(gatt_client, ATT_READ_REQUEST, gatt_client->attribute_handle); 557 } 558 559 static void send_gatt_read_by_type_request(gatt_client_t * gatt_client){ 560 if (gatt_client->uuid16){ 561 att_read_by_type_or_group_request_for_uuid16(gatt_client, ATT_READ_BY_TYPE_REQUEST, 562 gatt_client->uuid16, gatt_client->start_group_handle, 563 gatt_client->end_group_handle); 564 } else { 565 att_read_by_type_or_group_request_for_uuid128(gatt_client, ATT_READ_BY_TYPE_REQUEST, 566 gatt_client->uuid128, gatt_client->start_group_handle, 567 gatt_client->end_group_handle); 568 } 569 } 570 571 static void send_gatt_read_blob_request(gatt_client_t *gatt_client){ 572 if (gatt_client->attribute_offset == 0){ 573 att_read_request(gatt_client, ATT_READ_REQUEST, gatt_client->attribute_handle); 574 } else { 575 att_read_blob_request(gatt_client, ATT_READ_BLOB_REQUEST, gatt_client->attribute_handle, 576 gatt_client->attribute_offset); 577 } 578 } 579 580 static void send_gatt_read_multiple_request(gatt_client_t * gatt_client){ 581 att_read_multiple_request(gatt_client, gatt_client->read_multiple_handle_count, gatt_client->read_multiple_handles); 582 } 583 584 #ifdef ENABLE_GATT_OVER_EATT 585 static void send_gatt_read_multiple_variable_request(gatt_client_t * gatt_client){ 586 att_read_multiple_variable_request(gatt_client, gatt_client->read_multiple_handle_count, gatt_client->read_multiple_handles); 587 } 588 #endif 589 590 static void send_gatt_write_attribute_value_request(gatt_client_t * gatt_client){ 591 att_write_request(gatt_client, ATT_WRITE_REQUEST, gatt_client->attribute_handle, gatt_client->attribute_length, 592 gatt_client->attribute_value); 593 } 594 595 static void send_gatt_write_client_characteristic_configuration_request(gatt_client_t * gatt_client){ 596 att_write_request(gatt_client, ATT_WRITE_REQUEST, gatt_client->client_characteristic_configuration_handle, 2, 597 gatt_client->client_characteristic_configuration_value); 598 } 599 600 static void send_gatt_prepare_write_request(gatt_client_t * gatt_client){ 601 att_prepare_write_request(gatt_client, ATT_PREPARE_WRITE_REQUEST, gatt_client->attribute_handle, 602 gatt_client->attribute_offset, write_blob_length(gatt_client), 603 gatt_client->attribute_value); 604 } 605 606 static void send_gatt_execute_write_request(gatt_client_t * gatt_client){ 607 att_execute_write_request(gatt_client, ATT_EXECUTE_WRITE_REQUEST, 1); 608 } 609 610 static void send_gatt_cancel_prepared_write_request(gatt_client_t * gatt_client){ 611 att_execute_write_request(gatt_client, ATT_EXECUTE_WRITE_REQUEST, 0); 612 } 613 614 #ifndef ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERY 615 static void send_gatt_read_client_characteristic_configuration_request(gatt_client_t * gatt_client){ 616 att_read_by_type_or_group_request_for_uuid16(gatt_client, ATT_READ_BY_TYPE_REQUEST, 617 GATT_CLIENT_CHARACTERISTICS_CONFIGURATION, 618 gatt_client->start_group_handle, gatt_client->end_group_handle); 619 } 620 #endif 621 622 static void send_gatt_read_characteristic_descriptor_request(gatt_client_t * gatt_client){ 623 att_read_request(gatt_client, ATT_READ_REQUEST, gatt_client->attribute_handle); 624 } 625 626 #ifdef ENABLE_LE_SIGNED_WRITE 627 static void send_gatt_signed_write_request(gatt_client_t * gatt_client, uint32_t sign_counter){ 628 att_signed_write_request(gatt_client, ATT_SIGNED_WRITE_COMMAND, gatt_client->attribute_handle, 629 gatt_client->attribute_length, gatt_client->attribute_value, sign_counter, 630 gatt_client->cmac); 631 } 632 #endif 633 634 static uint16_t get_last_result_handle_from_service_list(uint8_t * packet, uint16_t size){ 635 if (size < 2) return 0xffff; 636 uint8_t attr_length = packet[1]; 637 if ((2 + attr_length) > size) return 0xffff; 638 return little_endian_read_16(packet, size - attr_length + 2u); 639 } 640 641 static uint16_t get_last_result_handle_from_characteristics_list(uint8_t * packet, uint16_t size){ 642 if (size < 2) return 0xffff; 643 uint8_t attr_length = packet[1]; 644 if ((2 + attr_length) > size) return 0xffff; 645 return little_endian_read_16(packet, size - attr_length + 3u); 646 } 647 648 static uint16_t get_last_result_handle_from_included_services_list(uint8_t * packet, uint16_t size){ 649 if (size < 2) return 0xffff; 650 uint8_t attr_length = packet[1]; 651 if ((2 + attr_length) > size) return 0xffff; 652 return little_endian_read_16(packet, size - attr_length); 653 } 654 655 static void gatt_client_notify_can_send_query(gatt_client_t * gatt_client){ 656 while (gatt_client->gatt_client_state == P_READY){ 657 #ifdef ENABLE_GATT_OVER_EATT 658 bool query_sent = gatt_client_le_enhanced_handle_can_send_query(gatt_client); 659 if (query_sent){ 660 continue; 661 } 662 #endif 663 btstack_context_callback_registration_t * callback = (btstack_context_callback_registration_t *) btstack_linked_list_pop(&gatt_client->query_requests); 664 if (callback == NULL) { 665 return; 666 } 667 (*callback->callback)(callback->context); 668 } 669 } 670 671 // test if notification/indication should be delivered to application (BLESA) 672 static bool gatt_client_accept_server_message(gatt_client_t *gatt_client) { 673 #ifdef ENABLE_LE_PROACTIVE_AUTHENTICATION 674 // ignore messages until re-encryption is complete 675 if (gap_reconnect_security_setup_active(gatt_client->con_handle)) return false; 676 677 // after that ignore if bonded but not encrypted 678 return !gap_bonded(gatt_client->con_handle) || (gap_encryption_key_size(gatt_client->con_handle) > 0); 679 #else 680 UNUSED(gatt_client); 681 return true; 682 #endif 683 } 684 685 static void emit_event_new(btstack_packet_handler_t callback, uint8_t * packet, uint16_t size){ 686 if (!callback) return; 687 hci_dump_packet(HCI_EVENT_PACKET, 1, packet, size); 688 (*callback)(HCI_EVENT_PACKET, 0, packet, size); 689 } 690 691 static void emit_event_to_registered_listeners(hci_con_handle_t con_handle, uint16_t attribute_handle, uint8_t * packet, uint16_t size){ 692 btstack_linked_list_iterator_t it; 693 btstack_linked_list_iterator_init(&it, &gatt_client_value_listeners); 694 while (btstack_linked_list_iterator_has_next(&it)){ 695 gatt_client_notification_t * notification = (gatt_client_notification_t*) btstack_linked_list_iterator_next(&it); 696 if ((notification->con_handle != GATT_CLIENT_ANY_CONNECTION) && (notification->con_handle != con_handle)) continue; 697 if ((notification->attribute_handle != GATT_CLIENT_ANY_VALUE_HANDLE) && (notification->attribute_handle != attribute_handle)) continue; 698 (*notification->callback)(HCI_EVENT_PACKET, 0, packet, size); 699 } 700 } 701 702 static void emit_gatt_complete_event(gatt_client_t * gatt_client, uint8_t att_status){ 703 // @format H1 704 uint8_t packet[5]; 705 packet[0] = GATT_EVENT_QUERY_COMPLETE; 706 packet[1] = 3; 707 little_endian_store_16(packet, 2, gatt_client->con_handle); 708 packet[4] = att_status; 709 emit_event_new(gatt_client->callback, packet, sizeof(packet)); 710 } 711 712 static void emit_gatt_service_query_result_event(gatt_client_t * gatt_client, uint16_t start_group_handle, uint16_t end_group_handle, const uint8_t * uuid128){ 713 // @format HX 714 uint8_t packet[24]; 715 packet[0] = GATT_EVENT_SERVICE_QUERY_RESULT; 716 packet[1] = sizeof(packet) - 2u; 717 little_endian_store_16(packet, 2, gatt_client->con_handle); 718 /// 719 little_endian_store_16(packet, 4, start_group_handle); 720 little_endian_store_16(packet, 6, end_group_handle); 721 reverse_128(uuid128, &packet[8]); 722 emit_event_new(gatt_client->callback, packet, sizeof(packet)); 723 } 724 725 static void emit_gatt_included_service_query_result_event(gatt_client_t * gatt_client, uint16_t include_handle, uint16_t start_group_handle, uint16_t end_group_handle, const uint8_t * uuid128){ 726 // @format HX 727 uint8_t packet[26]; 728 packet[0] = GATT_EVENT_INCLUDED_SERVICE_QUERY_RESULT; 729 packet[1] = sizeof(packet) - 2u; 730 little_endian_store_16(packet, 2, gatt_client->con_handle); 731 /// 732 little_endian_store_16(packet, 4, include_handle); 733 // 734 little_endian_store_16(packet, 6, start_group_handle); 735 little_endian_store_16(packet, 8, end_group_handle); 736 reverse_128(uuid128, &packet[10]); 737 emit_event_new(gatt_client->callback, packet, sizeof(packet)); 738 } 739 740 static void emit_gatt_characteristic_query_result_event(gatt_client_t * gatt_client, uint16_t start_handle, uint16_t value_handle, uint16_t end_handle, 741 uint16_t properties, const uint8_t * uuid128){ 742 // @format HY 743 uint8_t packet[28]; 744 packet[0] = GATT_EVENT_CHARACTERISTIC_QUERY_RESULT; 745 packet[1] = sizeof(packet) - 2u; 746 little_endian_store_16(packet, 2, gatt_client->con_handle); 747 /// 748 little_endian_store_16(packet, 4, start_handle); 749 little_endian_store_16(packet, 6, value_handle); 750 little_endian_store_16(packet, 8, end_handle); 751 little_endian_store_16(packet, 10, properties); 752 reverse_128(uuid128, &packet[12]); 753 emit_event_new(gatt_client->callback, packet, sizeof(packet)); 754 } 755 756 static void emit_gatt_all_characteristic_descriptors_result_event( 757 gatt_client_t * gatt_client, uint16_t descriptor_handle, const uint8_t * uuid128){ 758 // @format HZ 759 uint8_t packet[22]; 760 packet[0] = GATT_EVENT_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY_RESULT; 761 packet[1] = sizeof(packet) - 2u; 762 little_endian_store_16(packet, 2, gatt_client->con_handle); 763 /// 764 little_endian_store_16(packet, 4, descriptor_handle); 765 reverse_128(uuid128, &packet[6]); 766 emit_event_new(gatt_client->callback, packet, sizeof(packet)); 767 } 768 769 static void emit_gatt_mtu_exchanged_result_event(gatt_client_t * gatt_client, uint16_t new_mtu){ 770 // @format H2 771 uint8_t packet[6]; 772 packet[0] = GATT_EVENT_MTU; 773 packet[1] = sizeof(packet) - 2u; 774 little_endian_store_16(packet, 2, gatt_client->con_handle); 775 little_endian_store_16(packet, 4, new_mtu); 776 att_dispatch_client_mtu_exchanged(gatt_client->con_handle, new_mtu); 777 emit_event_new(gatt_client->callback, packet, sizeof(packet)); 778 } 779 780 // helper 781 static void gatt_client_handle_transaction_complete(gatt_client_t *gatt_client, uint8_t att_status) { 782 gatt_client->gatt_client_state = P_READY; 783 gatt_client_timeout_stop(gatt_client); 784 emit_gatt_complete_event(gatt_client, att_status); 785 gatt_client_notify_can_send_query(gatt_client); 786 } 787 788 // @return packet pointer 789 // @note assume that value is part of an l2cap buffer - overwrite HCI + L2CAP packet headers 790 #define CHARACTERISTIC_VALUE_EVENT_HEADER_SIZE 8 791 static uint8_t * setup_characteristic_value_packet(uint8_t type, hci_con_handle_t con_handle, uint16_t attribute_handle, uint8_t * value, uint16_t length){ 792 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION 793 // copy value into test packet for testing 794 static uint8_t packet[1000]; 795 memcpy(&packet[8], value, length); 796 #else 797 // before the value inside the ATT PDU 798 uint8_t * packet = value - CHARACTERISTIC_VALUE_EVENT_HEADER_SIZE; 799 #endif 800 packet[0] = type; 801 packet[1] = CHARACTERISTIC_VALUE_EVENT_HEADER_SIZE - 2 + length; 802 little_endian_store_16(packet, 2, con_handle); 803 little_endian_store_16(packet, 4, attribute_handle); 804 little_endian_store_16(packet, 6, length); 805 return packet; 806 } 807 808 // @return packet pointer 809 // @note assume that value is part of an l2cap buffer - overwrite parts of the HCI/L2CAP/ATT packet (4/4/3) bytes 810 #define LONG_CHARACTERISTIC_VALUE_EVENT_HEADER_SIZE 10 811 static uint8_t * setup_long_characteristic_value_packet(uint8_t type, hci_con_handle_t con_handle, uint16_t attribute_handle, uint16_t offset, uint8_t * value, uint16_t length){ 812 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION 813 // avoid using pre ATT headers. 814 return NULL; 815 #endif 816 #if defined(HCI_INCOMING_PRE_BUFFER_SIZE) && (HCI_INCOMING_PRE_BUFFER_SIZE >= LONG_CHARACTERISTIC_VALUE_EVENT_HEADER_SIZE - 8) // L2CAP Header (4) - ACL Header (4) 817 // before the value inside the ATT PDU 818 uint8_t * packet = value - LONG_CHARACTERISTIC_VALUE_EVENT_HEADER_SIZE; 819 packet[0] = type; 820 packet[1] = LONG_CHARACTERISTIC_VALUE_EVENT_HEADER_SIZE - 2 + length; 821 little_endian_store_16(packet, 2, con_handle); 822 little_endian_store_16(packet, 4, attribute_handle); 823 little_endian_store_16(packet, 6, offset); 824 little_endian_store_16(packet, 8, length); 825 return packet; 826 #else 827 log_error("HCI_INCOMING_PRE_BUFFER_SIZE >= 2 required for long characteristic reads"); 828 return NULL; 829 #endif 830 } 831 832 #if (LONG_CHARACTERISTIC_VALUE_EVENT_HEADER_SIZE > CHARACTERISTIC_VALUE_EVENT_HEADER_SIZE) 833 #define REPORT_PREBUFFER_HEADER LONG_CHARACTERISTIC_VALUE_EVENT_HEADER_SIZE 834 #else 835 #define REPORT_PREBUFFER_HEADER CHARACTERISTIC_VALUE_EVENT_HEADER_SIZE 836 #endif 837 838 /// 839 static void report_gatt_services(gatt_client_t * gatt_client, uint8_t * packet, uint16_t size){ 840 if (size < 2) return; 841 uint8_t attr_length = packet[1]; 842 uint8_t uuid_length = attr_length - 4u; 843 844 int i; 845 for (i = 2; (i+attr_length) <= size; i += attr_length){ 846 uint16_t start_group_handle = little_endian_read_16(packet,i); 847 uint16_t end_group_handle = little_endian_read_16(packet,i+2); 848 uint8_t uuid128[16]; 849 uint16_t uuid16 = 0; 850 851 if (uuid_length == 2u){ 852 uuid16 = little_endian_read_16(packet, i+4); 853 uuid_add_bluetooth_prefix((uint8_t*) &uuid128, uuid16); 854 } else if (uuid_length == 16u) { 855 reverse_128(&packet[i+4], uuid128); 856 } else { 857 return; 858 } 859 emit_gatt_service_query_result_event(gatt_client, start_group_handle, end_group_handle, uuid128); 860 } 861 } 862 863 static void report_gatt_characteristic_start_found(gatt_client_t * gatt_client, uint16_t start_handle, uint8_t properties, uint16_t value_handle, uint8_t * uuid, uint16_t uuid_length){ 864 uint8_t uuid128[16]; 865 uint16_t uuid16 = 0; 866 if (uuid_length == 2u){ 867 uuid16 = little_endian_read_16(uuid, 0); 868 uuid_add_bluetooth_prefix((uint8_t*) uuid128, uuid16); 869 } else if (uuid_length == 16u){ 870 reverse_128(uuid, uuid128); 871 } else { 872 return; 873 } 874 875 if (gatt_client->filter_with_uuid && (memcmp(gatt_client->uuid128, uuid128, 16) != 0)) return; 876 877 gatt_client->characteristic_properties = properties; 878 gatt_client->characteristic_start_handle = start_handle; 879 gatt_client->attribute_handle = value_handle; 880 881 if (gatt_client->filter_with_uuid) return; 882 883 gatt_client->uuid16 = uuid16; 884 (void)memcpy(gatt_client->uuid128, uuid128, 16); 885 } 886 887 static void report_gatt_characteristic_end_found(gatt_client_t * gatt_client, uint16_t end_handle){ 888 // TODO: stop searching if filter and uuid found 889 890 if (!gatt_client->characteristic_start_handle) return; 891 892 emit_gatt_characteristic_query_result_event(gatt_client, gatt_client->characteristic_start_handle, gatt_client->attribute_handle, 893 end_handle, gatt_client->characteristic_properties, gatt_client->uuid128); 894 895 gatt_client->characteristic_start_handle = 0; 896 } 897 898 899 static void report_gatt_characteristics(gatt_client_t * gatt_client, uint8_t * packet, uint16_t size){ 900 if (size < 2u) return; 901 uint8_t attr_length = packet[1]; 902 if ((attr_length != 7u) && (attr_length != 21u)) return; 903 uint8_t uuid_length = attr_length - 5u; 904 int i; 905 for (i = 2u; (i + attr_length) <= size; i += attr_length){ 906 uint16_t start_handle = little_endian_read_16(packet, i); 907 uint8_t properties = packet[i+2]; 908 uint16_t value_handle = little_endian_read_16(packet, i+3); 909 report_gatt_characteristic_end_found(gatt_client, start_handle - 1u); 910 report_gatt_characteristic_start_found(gatt_client, start_handle, properties, value_handle, &packet[i + 5], 911 uuid_length); 912 } 913 } 914 915 static void report_gatt_included_service_uuid16(gatt_client_t * gatt_client, uint16_t include_handle, uint16_t uuid16){ 916 uint8_t normalized_uuid128[16]; 917 uuid_add_bluetooth_prefix(normalized_uuid128, uuid16); 918 emit_gatt_included_service_query_result_event(gatt_client, include_handle, gatt_client->query_start_handle, 919 gatt_client->query_end_handle, normalized_uuid128); 920 } 921 922 static void report_gatt_included_service_uuid128(gatt_client_t * gatt_client, uint16_t include_handle, const uint8_t * uuid128){ 923 emit_gatt_included_service_query_result_event(gatt_client, include_handle, gatt_client->query_start_handle, 924 gatt_client->query_end_handle, uuid128); 925 } 926 927 // @note assume that value is part of an l2cap buffer - overwrite parts of the HCI/L2CAP/ATT packet (4/4/3) bytes 928 static void report_gatt_notification(gatt_client_t *gatt_client, uint16_t value_handle, uint8_t *value, int length) { 929 if (!gatt_client_accept_server_message(gatt_client)) return; 930 uint8_t * packet = setup_characteristic_value_packet(GATT_EVENT_NOTIFICATION, gatt_client->con_handle, value_handle, value, length); 931 emit_event_to_registered_listeners(gatt_client->con_handle, value_handle, packet, CHARACTERISTIC_VALUE_EVENT_HEADER_SIZE + length); 932 } 933 934 // @note assume that value is part of an l2cap buffer - overwrite parts of the HCI/L2CAP/ATT packet (4/4/3) bytes 935 static void report_gatt_indication(gatt_client_t *gatt_client, uint16_t value_handle, uint8_t *value, int length) { 936 if (!gatt_client_accept_server_message(gatt_client)) return; 937 uint8_t * packet = setup_characteristic_value_packet(GATT_EVENT_INDICATION, gatt_client->con_handle, value_handle, value, length); 938 emit_event_to_registered_listeners(gatt_client->con_handle, value_handle, packet, CHARACTERISTIC_VALUE_EVENT_HEADER_SIZE + length); 939 } 940 941 // @note assume that value is part of an l2cap buffer - overwrite parts of the HCI/L2CAP/ATT packet (4/4/3) bytes 942 static void report_gatt_characteristic_value(gatt_client_t * gatt_client, uint16_t attribute_handle, uint8_t * value, uint16_t length){ 943 uint8_t * packet = setup_characteristic_value_packet(GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT, gatt_client->con_handle, attribute_handle, value, length); 944 emit_event_new(gatt_client->callback, packet, CHARACTERISTIC_VALUE_EVENT_HEADER_SIZE + length); 945 } 946 947 // @note assume that value is part of an l2cap buffer - overwrite parts of the HCI/L2CAP/ATT packet (4/4/3) bytes 948 static void report_gatt_long_characteristic_value_blob(gatt_client_t * gatt_client, uint16_t attribute_handle, uint8_t * blob, uint16_t blob_length, int value_offset){ 949 uint8_t * packet = setup_long_characteristic_value_packet(GATT_EVENT_LONG_CHARACTERISTIC_VALUE_QUERY_RESULT, gatt_client->con_handle, attribute_handle, value_offset, blob, blob_length); 950 if (!packet) return; 951 emit_event_new(gatt_client->callback, packet, blob_length + LONG_CHARACTERISTIC_VALUE_EVENT_HEADER_SIZE); 952 } 953 954 static void report_gatt_characteristic_descriptor(gatt_client_t * gatt_client, uint16_t descriptor_handle, uint8_t *value, uint16_t value_length, uint16_t value_offset){ 955 UNUSED(value_offset); 956 uint8_t * packet = setup_characteristic_value_packet(GATT_EVENT_CHARACTERISTIC_DESCRIPTOR_QUERY_RESULT, gatt_client->con_handle, descriptor_handle, value, value_length); 957 emit_event_new(gatt_client->callback, packet, value_length + 8u); 958 } 959 960 static void report_gatt_long_characteristic_descriptor(gatt_client_t * gatt_client, uint16_t descriptor_handle, uint8_t *blob, uint16_t blob_length, uint16_t value_offset){ 961 uint8_t * packet = setup_long_characteristic_value_packet(GATT_EVENT_LONG_CHARACTERISTIC_DESCRIPTOR_QUERY_RESULT, gatt_client->con_handle, descriptor_handle, value_offset, blob, blob_length); 962 if (!packet) return; 963 emit_event_new(gatt_client->callback, packet, blob_length + LONG_CHARACTERISTIC_VALUE_EVENT_HEADER_SIZE); 964 } 965 966 static void report_gatt_all_characteristic_descriptors(gatt_client_t * gatt_client, uint8_t * packet, uint16_t size, uint16_t pair_size){ 967 int i; 968 for (i = 0u; (i + pair_size) <= size; i += pair_size){ 969 uint16_t descriptor_handle = little_endian_read_16(packet,i); 970 uint8_t uuid128[16]; 971 uint16_t uuid16 = 0; 972 if (pair_size == 4u){ 973 uuid16 = little_endian_read_16(packet,i+2); 974 uuid_add_bluetooth_prefix(uuid128, uuid16); 975 } else { 976 reverse_128(&packet[i+2], uuid128); 977 } 978 emit_gatt_all_characteristic_descriptors_result_event(gatt_client, descriptor_handle, uuid128); 979 } 980 981 } 982 983 static int is_query_done(gatt_client_t * gatt_client, uint16_t last_result_handle){ 984 return last_result_handle >= gatt_client->end_group_handle; 985 } 986 987 static void trigger_next_query(gatt_client_t * gatt_client, uint16_t last_result_handle, gatt_client_state_t next_query_state){ 988 if (is_query_done(gatt_client, last_result_handle)){ 989 gatt_client_handle_transaction_complete(gatt_client, ATT_ERROR_SUCCESS); 990 return; 991 } 992 // next 993 gatt_client->start_group_handle = last_result_handle + 1u; 994 gatt_client->gatt_client_state = next_query_state; 995 } 996 997 static void trigger_next_included_service_query(gatt_client_t * gatt_client, uint16_t last_result_handle){ 998 trigger_next_query(gatt_client, last_result_handle, P_W2_SEND_INCLUDED_SERVICE_QUERY); 999 } 1000 1001 static void trigger_next_service_query(gatt_client_t * gatt_client, uint16_t last_result_handle){ 1002 trigger_next_query(gatt_client, last_result_handle, P_W2_SEND_SERVICE_QUERY); 1003 } 1004 1005 static void trigger_next_service_by_uuid_query(gatt_client_t * gatt_client, uint16_t last_result_handle){ 1006 trigger_next_query(gatt_client, last_result_handle, P_W2_SEND_SERVICE_WITH_UUID_QUERY); 1007 } 1008 1009 static void trigger_next_characteristic_query(gatt_client_t * gatt_client, uint16_t last_result_handle){ 1010 if (is_query_done(gatt_client, last_result_handle)){ 1011 // report last characteristic 1012 report_gatt_characteristic_end_found(gatt_client, gatt_client->end_group_handle); 1013 } 1014 trigger_next_query(gatt_client, last_result_handle, P_W2_SEND_ALL_CHARACTERISTICS_OF_SERVICE_QUERY); 1015 } 1016 1017 static void trigger_next_characteristic_descriptor_query(gatt_client_t * gatt_client, uint16_t last_result_handle){ 1018 trigger_next_query(gatt_client, last_result_handle, P_W2_SEND_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY); 1019 } 1020 1021 static void trigger_next_read_by_type_query(gatt_client_t * gatt_client, uint16_t last_result_handle){ 1022 trigger_next_query(gatt_client, last_result_handle, P_W2_SEND_READ_BY_TYPE_REQUEST); 1023 } 1024 1025 static void trigger_next_prepare_write_query(gatt_client_t * gatt_client, gatt_client_state_t next_query_state, gatt_client_state_t done_state){ 1026 gatt_client->attribute_offset += write_blob_length(gatt_client); 1027 uint16_t next_blob_length = write_blob_length(gatt_client); 1028 1029 if (next_blob_length == 0u){ 1030 gatt_client->gatt_client_state = done_state; 1031 return; 1032 } 1033 gatt_client->gatt_client_state = next_query_state; 1034 } 1035 1036 static void trigger_next_blob_query(gatt_client_t * gatt_client, gatt_client_state_t next_query_state, uint16_t received_blob_length){ 1037 1038 uint16_t max_blob_length = gatt_client->mtu - 1u; 1039 if (received_blob_length < max_blob_length){ 1040 gatt_client_handle_transaction_complete(gatt_client, ATT_ERROR_SUCCESS); 1041 return; 1042 } 1043 1044 gatt_client->attribute_offset += received_blob_length; 1045 gatt_client->gatt_client_state = next_query_state; 1046 } 1047 1048 void gatt_client_listen_for_characteristic_value_updates(gatt_client_notification_t * notification, btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_characteristic_t * characteristic){ 1049 notification->callback = callback; 1050 notification->con_handle = con_handle; 1051 if (characteristic == NULL){ 1052 notification->attribute_handle = GATT_CLIENT_ANY_VALUE_HANDLE; 1053 } else { 1054 notification->attribute_handle = characteristic->value_handle; 1055 } 1056 btstack_linked_list_add(&gatt_client_value_listeners, (btstack_linked_item_t*) notification); 1057 } 1058 1059 void gatt_client_stop_listening_for_characteristic_value_updates(gatt_client_notification_t * notification){ 1060 btstack_linked_list_remove(&gatt_client_value_listeners, (btstack_linked_item_t*) notification); 1061 } 1062 1063 static int is_value_valid(gatt_client_t *gatt_client, uint8_t *packet, uint16_t size){ 1064 uint16_t attribute_handle = little_endian_read_16(packet, 1); 1065 uint16_t value_offset = little_endian_read_16(packet, 3); 1066 1067 if (gatt_client->attribute_handle != attribute_handle) return 0; 1068 if (gatt_client->attribute_offset != value_offset) return 0; 1069 return memcmp(&gatt_client->attribute_value[gatt_client->attribute_offset], &packet[5], size - 5u) == 0u; 1070 } 1071 1072 #ifdef ENABLE_LE_SIGNED_WRITE 1073 static void gatt_client_run_for_client_start_signed_write(gatt_client_t *gatt_client) { 1074 sm_key_t csrk; 1075 le_device_db_local_csrk_get(gatt_client->le_device_index, csrk); 1076 uint32_t sign_counter = le_device_db_local_counter_get(gatt_client->le_device_index); 1077 gatt_client->gatt_client_state = P_W4_CMAC_RESULT; 1078 sm_cmac_signed_write_start(csrk, ATT_SIGNED_WRITE_COMMAND, gatt_client->attribute_handle, gatt_client->attribute_length, gatt_client->attribute_value, sign_counter, att_signed_write_handle_cmac_result); 1079 } 1080 #endif 1081 1082 // returns true if packet was sent 1083 static bool gatt_client_run_for_gatt_client(gatt_client_t * gatt_client){ 1084 1085 // wait until re-encryption is complete 1086 if (gap_reconnect_security_setup_active(gatt_client->con_handle)) return false; 1087 1088 // wait until re-encryption is complete 1089 if (gatt_client->reencryption_active) return false; 1090 1091 // wait until pairing complete (either reactive authentication or due to required security level) 1092 if (gatt_client->wait_for_authentication_complete) return false; 1093 1094 bool client_request_pending = gatt_client->gatt_client_state != P_READY; 1095 1096 // verify security level for Mandatory Authentication over LE 1097 bool check_security; 1098 switch (gatt_client->bearer_type){ 1099 case ATT_BEARER_UNENHANCED_LE: 1100 check_security = true; 1101 break; 1102 default: 1103 check_security = false; 1104 break; 1105 } 1106 if (client_request_pending && (gatt_client_required_security_level > gatt_client->security_level) && check_security){ 1107 log_info("Trigger pairing, current security level %u, required %u\n", gatt_client->security_level, gatt_client_required_security_level); 1108 gatt_client->wait_for_authentication_complete = 1; 1109 // set att error code for pairing failure based on required level 1110 switch (gatt_client_required_security_level){ 1111 case LEVEL_4: 1112 case LEVEL_3: 1113 gatt_client->pending_error_code = ATT_ERROR_INSUFFICIENT_AUTHENTICATION; 1114 break; 1115 default: 1116 gatt_client->pending_error_code = ATT_ERROR_INSUFFICIENT_ENCRYPTION; 1117 break; 1118 } 1119 sm_request_pairing(gatt_client->con_handle); 1120 // sm probably just sent a pdu 1121 return true; 1122 } 1123 1124 switch (gatt_client->mtu_state) { 1125 case SEND_MTU_EXCHANGE: 1126 gatt_client->mtu_state = SENT_MTU_EXCHANGE; 1127 att_exchange_mtu_request(gatt_client); 1128 return true; 1129 case SENT_MTU_EXCHANGE: 1130 return false; 1131 default: 1132 break; 1133 } 1134 1135 if (gatt_client->send_confirmation){ 1136 gatt_client->send_confirmation = 0; 1137 att_confirmation(gatt_client); 1138 return true; 1139 } 1140 1141 // check MTU for writes 1142 switch (gatt_client->gatt_client_state){ 1143 case P_W2_SEND_WRITE_CHARACTERISTIC_VALUE: 1144 case P_W2_SEND_WRITE_CHARACTERISTIC_DESCRIPTOR: 1145 if (gatt_client->attribute_length <= (gatt_client->mtu - 3u)) break; 1146 log_error("gatt_client_run: value len %u > MTU %u - 3\n", gatt_client->attribute_length,gatt_client->mtu); 1147 gatt_client_handle_transaction_complete(gatt_client, ATT_ERROR_INVALID_ATTRIBUTE_VALUE_LENGTH); 1148 return false; 1149 default: 1150 break; 1151 } 1152 1153 bool packet_sent = true; 1154 bool done = true; 1155 switch (gatt_client->gatt_client_state){ 1156 case P_W2_SEND_SERVICE_QUERY: 1157 gatt_client->gatt_client_state = P_W4_SERVICE_QUERY_RESULT; 1158 send_gatt_services_request(gatt_client); 1159 break; 1160 1161 case P_W2_SEND_SERVICE_WITH_UUID_QUERY: 1162 gatt_client->gatt_client_state = P_W4_SERVICE_WITH_UUID_RESULT; 1163 send_gatt_services_by_uuid_request(gatt_client); 1164 break; 1165 1166 case P_W2_SEND_ALL_CHARACTERISTICS_OF_SERVICE_QUERY: 1167 gatt_client->gatt_client_state = P_W4_ALL_CHARACTERISTICS_OF_SERVICE_QUERY_RESULT; 1168 send_gatt_characteristic_request(gatt_client); 1169 break; 1170 1171 case P_W2_SEND_CHARACTERISTIC_WITH_UUID_QUERY: 1172 gatt_client->gatt_client_state = P_W4_CHARACTERISTIC_WITH_UUID_QUERY_RESULT; 1173 send_gatt_characteristic_request(gatt_client); 1174 break; 1175 1176 case P_W2_SEND_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY: 1177 gatt_client->gatt_client_state = P_W4_CHARACTERISTIC_WITH_UUID_QUERY_RESULT; 1178 send_gatt_characteristic_descriptor_request(gatt_client); 1179 break; 1180 1181 case P_W2_SEND_INCLUDED_SERVICE_QUERY: 1182 gatt_client->gatt_client_state = P_W4_INCLUDED_SERVICE_QUERY_RESULT; 1183 send_gatt_included_service_request(gatt_client); 1184 break; 1185 1186 case P_W2_SEND_INCLUDED_SERVICE_WITH_UUID_QUERY: 1187 gatt_client->gatt_client_state = P_W4_INCLUDED_SERVICE_UUID_WITH_QUERY_RESULT; 1188 send_gatt_included_service_uuid_request(gatt_client); 1189 break; 1190 1191 case P_W2_SEND_READ_CHARACTERISTIC_VALUE_QUERY: 1192 gatt_client->gatt_client_state = P_W4_READ_CHARACTERISTIC_VALUE_RESULT; 1193 send_gatt_read_characteristic_value_request(gatt_client); 1194 break; 1195 1196 case P_W2_SEND_READ_BLOB_QUERY: 1197 gatt_client->gatt_client_state = P_W4_READ_BLOB_RESULT; 1198 send_gatt_read_blob_request(gatt_client); 1199 break; 1200 1201 case P_W2_SEND_READ_BY_TYPE_REQUEST: 1202 gatt_client->gatt_client_state = P_W4_READ_BY_TYPE_RESPONSE; 1203 send_gatt_read_by_type_request(gatt_client); 1204 break; 1205 1206 case P_W2_SEND_READ_MULTIPLE_REQUEST: 1207 gatt_client->gatt_client_state = P_W4_READ_MULTIPLE_RESPONSE; 1208 send_gatt_read_multiple_request(gatt_client); 1209 break; 1210 1211 #ifdef ENABLE_GATT_OVER_EATT 1212 case P_W2_SEND_READ_MULTIPLE_VARIABLE_REQUEST: 1213 gatt_client->gatt_client_state = P_W4_READ_MULTIPLE_VARIABLE_RESPONSE; 1214 send_gatt_read_multiple_variable_request(gatt_client); 1215 break; 1216 #endif 1217 1218 case P_W2_SEND_WRITE_CHARACTERISTIC_VALUE: 1219 gatt_client->gatt_client_state = P_W4_WRITE_CHARACTERISTIC_VALUE_RESULT; 1220 send_gatt_write_attribute_value_request(gatt_client); 1221 break; 1222 1223 case P_W2_PREPARE_WRITE: 1224 gatt_client->gatt_client_state = P_W4_PREPARE_WRITE_RESULT; 1225 send_gatt_prepare_write_request(gatt_client); 1226 break; 1227 1228 case P_W2_PREPARE_WRITE_SINGLE: 1229 gatt_client->gatt_client_state = P_W4_PREPARE_WRITE_SINGLE_RESULT; 1230 send_gatt_prepare_write_request(gatt_client); 1231 break; 1232 1233 case P_W2_PREPARE_RELIABLE_WRITE: 1234 gatt_client->gatt_client_state = P_W4_PREPARE_RELIABLE_WRITE_RESULT; 1235 send_gatt_prepare_write_request(gatt_client); 1236 break; 1237 1238 case P_W2_EXECUTE_PREPARED_WRITE: 1239 gatt_client->gatt_client_state = P_W4_EXECUTE_PREPARED_WRITE_RESULT; 1240 send_gatt_execute_write_request(gatt_client); 1241 break; 1242 1243 case P_W2_CANCEL_PREPARED_WRITE: 1244 gatt_client->gatt_client_state = P_W4_CANCEL_PREPARED_WRITE_RESULT; 1245 send_gatt_cancel_prepared_write_request(gatt_client); 1246 break; 1247 1248 case P_W2_CANCEL_PREPARED_WRITE_DATA_MISMATCH: 1249 gatt_client->gatt_client_state = P_W4_CANCEL_PREPARED_WRITE_DATA_MISMATCH_RESULT; 1250 send_gatt_cancel_prepared_write_request(gatt_client); 1251 break; 1252 1253 #ifdef ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERY 1254 case P_W2_SEND_FIND_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY: 1255 // use Find Information 1256 gatt_client->gatt_client_state = P_W4_FIND_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY_RESULT; 1257 send_gatt_characteristic_descriptor_request(gatt_client); 1258 #else 1259 case P_W2_SEND_READ_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY: 1260 // Use Read By Type 1261 gatt_client->gatt_client_state = P_W4_READ_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY_RESULT; 1262 send_gatt_read_client_characteristic_configuration_request(gatt_client); 1263 #endif 1264 break; 1265 1266 case P_W2_SEND_READ_CHARACTERISTIC_DESCRIPTOR_QUERY: 1267 gatt_client->gatt_client_state = P_W4_READ_CHARACTERISTIC_DESCRIPTOR_RESULT; 1268 send_gatt_read_characteristic_descriptor_request(gatt_client); 1269 break; 1270 1271 case P_W2_SEND_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_QUERY: 1272 gatt_client->gatt_client_state = P_W4_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_RESULT; 1273 send_gatt_read_blob_request(gatt_client); 1274 break; 1275 1276 case P_W2_SEND_WRITE_CHARACTERISTIC_DESCRIPTOR: 1277 gatt_client->gatt_client_state = P_W4_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT; 1278 send_gatt_write_attribute_value_request(gatt_client); 1279 break; 1280 1281 case P_W2_WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION: 1282 gatt_client->gatt_client_state = P_W4_CLIENT_CHARACTERISTIC_CONFIGURATION_RESULT; 1283 send_gatt_write_client_characteristic_configuration_request(gatt_client); 1284 break; 1285 1286 case P_W2_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR: 1287 gatt_client->gatt_client_state = P_W4_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT; 1288 send_gatt_prepare_write_request(gatt_client); 1289 break; 1290 1291 case P_W2_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR: 1292 gatt_client->gatt_client_state = P_W4_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT; 1293 send_gatt_execute_write_request(gatt_client); 1294 break; 1295 1296 #ifdef ENABLE_LE_SIGNED_WRITE 1297 case P_W4_IDENTITY_RESOLVING: 1298 log_info("P_W4_IDENTITY_RESOLVING - state %x", sm_identity_resolving_state(gatt_client->con_handle)); 1299 switch (sm_identity_resolving_state(gatt_client->con_handle)){ 1300 case IRK_LOOKUP_SUCCEEDED: 1301 gatt_client->le_device_index = sm_le_device_index(gatt_client->con_handle); 1302 gatt_client->gatt_client_state = P_W4_CMAC_READY; 1303 if (sm_cmac_ready()){ 1304 gatt_client_run_for_client_start_signed_write(gatt_client); 1305 } 1306 break; 1307 case IRK_LOOKUP_FAILED: 1308 gatt_client_handle_transaction_complete(gatt_client, ATT_ERROR_BONDING_INFORMATION_MISSING); 1309 break; 1310 default: 1311 break; 1312 } 1313 packet_sent = false; 1314 break; 1315 1316 case P_W4_CMAC_READY: 1317 if (sm_cmac_ready()){ 1318 gatt_client_run_for_client_start_signed_write(gatt_client); 1319 } 1320 packet_sent = false; 1321 break; 1322 1323 case P_W2_SEND_SIGNED_WRITE: { 1324 gatt_client->gatt_client_state = P_W4_SEND_SINGED_WRITE_DONE; 1325 // bump local signing counter 1326 uint32_t sign_counter = le_device_db_local_counter_get(gatt_client->le_device_index); 1327 le_device_db_local_counter_set(gatt_client->le_device_index, sign_counter + 1); 1328 // send signed write command 1329 send_gatt_signed_write_request(gatt_client, sign_counter); 1330 // finally, notifiy client that write is complete 1331 gatt_client_handle_transaction_complete(gatt_client, ATT_ERROR_SUCCESS); 1332 break; 1333 } 1334 #endif 1335 default: 1336 done = false; 1337 break; 1338 } 1339 1340 if (done){ 1341 return packet_sent; 1342 } 1343 1344 // write without response callback 1345 btstack_context_callback_registration_t * callback = 1346 (btstack_context_callback_registration_t *) btstack_linked_list_pop(&gatt_client->write_without_response_requests); 1347 if (callback != NULL){ 1348 (*callback->callback)(callback->context); 1349 return true; 1350 } 1351 1352 // requested can send now old 1353 if (gatt_client->write_without_response_callback){ 1354 btstack_packet_handler_t packet_handler = gatt_client->write_without_response_callback; 1355 gatt_client->write_without_response_callback = NULL; 1356 uint8_t event[4]; 1357 event[0] = GATT_EVENT_CAN_WRITE_WITHOUT_RESPONSE; 1358 event[1] = sizeof(event) - 2u; 1359 little_endian_store_16(event, 2, gatt_client->con_handle); 1360 packet_handler(HCI_EVENT_PACKET, gatt_client->con_handle, event, sizeof(event)); 1361 return true; // to trigger requeueing (even if higher layer didn't sent) 1362 } 1363 1364 return false; 1365 } 1366 1367 static void gatt_client_run(void){ 1368 btstack_linked_item_t *it; 1369 bool packet_sent; 1370 #ifdef ENABLE_GATT_OVER_EATT 1371 btstack_linked_list_iterator_t it_eatt; 1372 #endif 1373 for (it = (btstack_linked_item_t *) gatt_client_connections; it != NULL; it = it->next){ 1374 gatt_client_t * gatt_client = (gatt_client_t *) it; 1375 switch (gatt_client->bearer_type){ 1376 case ATT_BEARER_UNENHANCED_LE: 1377 #ifdef ENABLE_GATT_OVER_EATT 1378 btstack_linked_list_iterator_init(&it_eatt, &gatt_client->eatt_clients); 1379 while (btstack_linked_list_iterator_has_next(&it_eatt)) { 1380 gatt_client_t * eatt_client = (gatt_client_t *) btstack_linked_list_iterator_next(&it_eatt); 1381 if (eatt_client->gatt_client_state != P_READY){ 1382 if (l2cap_can_send_packet_now(eatt_client->l2cap_cid)){ 1383 gatt_client_run_for_gatt_client(eatt_client); 1384 } 1385 } 1386 } 1387 #endif 1388 if (!att_dispatch_client_can_send_now(gatt_client->con_handle)) { 1389 att_dispatch_client_request_can_send_now_event(gatt_client->con_handle); 1390 return; 1391 } 1392 packet_sent = gatt_client_run_for_gatt_client(gatt_client); 1393 if (packet_sent){ 1394 // request new permission 1395 att_dispatch_client_request_can_send_now_event(gatt_client->con_handle); 1396 // requeue client for fairness and exit 1397 // note: iterator has become invalid 1398 btstack_linked_list_remove(&gatt_client_connections, (btstack_linked_item_t *) gatt_client); 1399 btstack_linked_list_add_tail(&gatt_client_connections, (btstack_linked_item_t *) gatt_client); 1400 return; 1401 } 1402 break; 1403 #ifdef ENABLE_GATT_OVER_CLASSIC 1404 case ATT_BEARER_UNENHANCED_CLASSIC: 1405 if (gatt_client->con_handle == HCI_CON_HANDLE_INVALID) { 1406 continue; 1407 } 1408 1409 // handle GATT over BR/EDR 1410 if (l2cap_can_send_packet_now(gatt_client->l2cap_cid) == false){ 1411 l2cap_request_can_send_now_event(gatt_client->l2cap_cid); 1412 return; 1413 } 1414 packet_sent = gatt_client_run_for_gatt_client(gatt_client); 1415 if (packet_sent){ 1416 // request new permission 1417 att_dispatch_client_request_can_send_now_event(gatt_client->con_handle); 1418 // requeue client for fairness and exit 1419 // note: iterator has become invalid 1420 btstack_linked_list_remove(&gatt_client_connections, (btstack_linked_item_t *) gatt_client); 1421 btstack_linked_list_add_tail(&gatt_client_connections, (btstack_linked_item_t *) gatt_client); 1422 return; 1423 } 1424 break; 1425 #endif 1426 default: 1427 btstack_unreachable(); 1428 break; 1429 } 1430 1431 1432 } 1433 } 1434 1435 static void gatt_client_report_error_if_pending(gatt_client_t *gatt_client, uint8_t att_error_code) { 1436 if (is_ready(gatt_client) == 1) return; 1437 gatt_client_handle_transaction_complete(gatt_client, att_error_code); 1438 } 1439 1440 static void gatt_client_handle_reencryption_complete(const uint8_t * packet){ 1441 hci_con_handle_t con_handle = sm_event_reencryption_complete_get_handle(packet); 1442 gatt_client_t * gatt_client = gatt_client_get_context_for_handle(con_handle); 1443 if (gatt_client == NULL) return; 1444 1445 // update security level 1446 gatt_client->security_level = gatt_client_le_security_level_for_connection(con_handle); 1447 1448 gatt_client->reencryption_result = sm_event_reencryption_complete_get_status(packet); 1449 gatt_client->reencryption_active = false; 1450 gatt_client->wait_for_authentication_complete = 0; 1451 1452 if (gatt_client->gatt_client_state == P_READY) return; 1453 1454 switch (sm_event_reencryption_complete_get_status(packet)){ 1455 case ERROR_CODE_SUCCESS: 1456 log_info("re-encryption success, retry operation"); 1457 break; 1458 case ERROR_CODE_AUTHENTICATION_FAILURE: 1459 case ERROR_CODE_PIN_OR_KEY_MISSING: 1460 #if defined(ENABLE_GATT_CLIENT_PAIRING) && !defined(ENABLE_LE_PROACTIVE_AUTHENTICATION) 1461 if (gatt_client_required_security_level == LEVEL_0) { 1462 // re-encryption failed for reactive authentication with pairing and we have a pending client request 1463 // => try to resolve it by deleting bonding information if we started pairing before 1464 // delete bonding information 1465 int le_device_db_index = sm_le_device_index(gatt_client->con_handle); 1466 btstack_assert(le_device_db_index >= 0); 1467 log_info("reactive auth with pairing: delete bonding and start pairing"); 1468 #ifdef ENABLE_LE_PRIVACY_ADDRESS_RESOLUTION 1469 hci_remove_le_device_db_entry_from_resolving_list((uint16_t) le_device_db_index); 1470 #endif 1471 le_device_db_remove(le_device_db_index); 1472 // trigger pairing again 1473 sm_request_pairing(gatt_client->con_handle); 1474 break; 1475 } 1476 #endif 1477 // report bonding information missing 1478 gatt_client_handle_transaction_complete(gatt_client, ATT_ERROR_BONDING_INFORMATION_MISSING); 1479 break; 1480 default: 1481 // report bonding information missing 1482 gatt_client_handle_transaction_complete(gatt_client, gatt_client->pending_error_code); 1483 break; 1484 } 1485 } 1486 1487 static void gatt_client_handle_disconnection_complete(const uint8_t * packet){ 1488 log_info("GATT Client: HCI_EVENT_DISCONNECTION_COMPLETE"); 1489 hci_con_handle_t con_handle = little_endian_read_16(packet,3); 1490 gatt_client_t * gatt_client = gatt_client_get_context_for_handle(con_handle); 1491 if (gatt_client == NULL) return; 1492 1493 gatt_client_report_error_if_pending(gatt_client, ATT_ERROR_HCI_DISCONNECT_RECEIVED); 1494 gatt_client_timeout_stop(gatt_client); 1495 btstack_linked_list_remove(&gatt_client_connections, (btstack_linked_item_t *) gatt_client); 1496 btstack_memory_gatt_client_free(gatt_client); 1497 } 1498 1499 static void gatt_client_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1500 UNUSED(channel); // ok: handling own l2cap events 1501 UNUSED(size); // ok: there is no channel 1502 1503 if (packet_type != HCI_EVENT_PACKET) return; 1504 1505 hci_con_handle_t con_handle; 1506 gatt_client_t * gatt_client; 1507 switch (hci_event_packet_get_type(packet)) { 1508 case HCI_EVENT_DISCONNECTION_COMPLETE: 1509 gatt_client_handle_disconnection_complete(packet); 1510 break; 1511 1512 // Pairing complete (with/without bonding=storing of pairing information) 1513 case SM_EVENT_PAIRING_COMPLETE: 1514 con_handle = sm_event_pairing_complete_get_handle(packet); 1515 gatt_client = gatt_client_get_context_for_handle(con_handle); 1516 if (gatt_client == NULL) break; 1517 1518 // update security level 1519 gatt_client->security_level = gatt_client_le_security_level_for_connection(con_handle); 1520 1521 if (gatt_client->wait_for_authentication_complete){ 1522 gatt_client->wait_for_authentication_complete = 0; 1523 if (sm_event_pairing_complete_get_status(packet)){ 1524 log_info("pairing failed, report previous error 0x%x", gatt_client->pending_error_code); 1525 gatt_client_report_error_if_pending(gatt_client, gatt_client->pending_error_code); 1526 } else { 1527 log_info("pairing success, retry operation"); 1528 } 1529 } 1530 break; 1531 1532 #ifdef ENABLE_LE_SIGNED_WRITE 1533 // Identity Resolving completed (no code, gatt_client_run will continue) 1534 case SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED: 1535 case SM_EVENT_IDENTITY_RESOLVING_FAILED: 1536 break; 1537 #endif 1538 1539 // re-encryption started 1540 case SM_EVENT_REENCRYPTION_STARTED: 1541 con_handle = sm_event_reencryption_complete_get_handle(packet); 1542 gatt_client = gatt_client_get_context_for_handle(con_handle); 1543 if (gatt_client == NULL) break; 1544 1545 gatt_client->reencryption_active = true; 1546 gatt_client->reencryption_result = ERROR_CODE_SUCCESS; 1547 break; 1548 1549 // re-encryption complete 1550 case SM_EVENT_REENCRYPTION_COMPLETE: 1551 gatt_client_handle_reencryption_complete(packet); 1552 break; 1553 default: 1554 break; 1555 } 1556 1557 gatt_client_run(); 1558 } 1559 1560 static void gatt_client_handle_att_read_response(gatt_client_t *gatt_client, uint8_t *packet, uint16_t size) { 1561 switch (gatt_client->gatt_client_state) { 1562 case P_W4_INCLUDED_SERVICE_UUID_WITH_QUERY_RESULT: 1563 if (size >= 17) { 1564 uint8_t uuid128[16]; 1565 reverse_128(&packet[1], uuid128); 1566 report_gatt_included_service_uuid128(gatt_client, gatt_client->start_group_handle, uuid128); 1567 } 1568 trigger_next_included_service_query(gatt_client, gatt_client->start_group_handle); 1569 // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done 1570 break; 1571 1572 case P_W4_READ_CHARACTERISTIC_VALUE_RESULT: 1573 report_gatt_characteristic_value(gatt_client, gatt_client->attribute_handle, &packet[1], size - 1u); 1574 gatt_client_handle_transaction_complete(gatt_client, ATT_ERROR_SUCCESS); 1575 break; 1576 1577 case P_W4_READ_CHARACTERISTIC_DESCRIPTOR_RESULT: 1578 report_gatt_characteristic_descriptor(gatt_client, gatt_client->attribute_handle, &packet[1], 1579 size - 1u, 0u); 1580 gatt_client_handle_transaction_complete(gatt_client, ATT_ERROR_SUCCESS); 1581 break; 1582 1583 // Use ATT_READ_REQUEST for first blob of Read Long Characteristic 1584 case P_W4_READ_BLOB_RESULT: 1585 report_gatt_long_characteristic_value_blob(gatt_client, gatt_client->attribute_handle, &packet[1], 1586 size - 1u, gatt_client->attribute_offset); 1587 trigger_next_blob_query(gatt_client, P_W2_SEND_READ_BLOB_QUERY, size - 1u); 1588 // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done 1589 break; 1590 1591 // Use ATT_READ_REQUEST for first blob of Read Long Characteristic Descriptor 1592 case P_W4_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_RESULT: 1593 report_gatt_long_characteristic_descriptor(gatt_client, gatt_client->attribute_handle, &packet[1], 1594 size - 1u, gatt_client->attribute_offset); 1595 trigger_next_blob_query(gatt_client, P_W2_SEND_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_QUERY, 1596 size - 1u); 1597 // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done 1598 break; 1599 1600 default: 1601 break; 1602 } 1603 } 1604 1605 static void gatt_client_handle_att_read_by_type_response(gatt_client_t *gatt_client, uint8_t *packet, uint16_t size) { 1606 switch (gatt_client->gatt_client_state) { 1607 case P_W4_ALL_CHARACTERISTICS_OF_SERVICE_QUERY_RESULT: 1608 report_gatt_characteristics(gatt_client, packet, size); 1609 trigger_next_characteristic_query(gatt_client, 1610 get_last_result_handle_from_characteristics_list(packet, size)); 1611 // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done, or by ATT_ERROR 1612 break; 1613 case P_W4_CHARACTERISTIC_WITH_UUID_QUERY_RESULT: 1614 report_gatt_characteristics(gatt_client, packet, size); 1615 trigger_next_characteristic_query(gatt_client, 1616 get_last_result_handle_from_characteristics_list(packet, size)); 1617 // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done, or by ATT_ERROR 1618 break; 1619 case P_W4_INCLUDED_SERVICE_QUERY_RESULT: { 1620 if (size < 2u) break; 1621 uint16_t uuid16 = 0; 1622 uint16_t pair_size = packet[1]; 1623 1624 if (pair_size == 6u) { 1625 if (size < 8u) break; 1626 // UUIDs not available, query first included service 1627 gatt_client->start_group_handle = little_endian_read_16(packet, 2); // ready for next query 1628 gatt_client->query_start_handle = little_endian_read_16(packet, 4); 1629 gatt_client->query_end_handle = little_endian_read_16(packet, 6); 1630 gatt_client->gatt_client_state = P_W2_SEND_INCLUDED_SERVICE_WITH_UUID_QUERY; 1631 break; 1632 } 1633 1634 if (pair_size != 8u) break; 1635 1636 // UUIDs included, report all of them 1637 uint16_t offset; 1638 for (offset = 2u; (offset + 8u) <= size; offset += pair_size) { 1639 uint16_t include_handle = little_endian_read_16(packet, offset); 1640 gatt_client->query_start_handle = little_endian_read_16(packet, offset + 2u); 1641 gatt_client->query_end_handle = little_endian_read_16(packet, offset + 4u); 1642 uuid16 = little_endian_read_16(packet, offset + 6u); 1643 report_gatt_included_service_uuid16(gatt_client, include_handle, uuid16); 1644 } 1645 1646 trigger_next_included_service_query(gatt_client, 1647 get_last_result_handle_from_included_services_list(packet, 1648 size)); 1649 // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done 1650 break; 1651 } 1652 #ifndef ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERY 1653 case P_W4_READ_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY_RESULT: 1654 gatt_client->client_characteristic_configuration_handle = little_endian_read_16(packet, 2); 1655 gatt_client->gatt_client_state = P_W2_WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION; 1656 break; 1657 #endif 1658 case P_W4_READ_BY_TYPE_RESPONSE: { 1659 uint16_t pair_size = packet[1]; 1660 // set last result handle to last valid handle, only used if pair_size invalid 1661 uint16_t last_result_handle = 0xffff; 1662 if (pair_size > 2) { 1663 uint16_t offset; 1664 for (offset = 2; offset < size; offset += pair_size) { 1665 uint16_t value_handle = little_endian_read_16(packet, offset); 1666 report_gatt_characteristic_value(gatt_client, value_handle, &packet[offset + 2u], 1667 pair_size - 2u); 1668 last_result_handle = value_handle; 1669 } 1670 } 1671 trigger_next_read_by_type_query(gatt_client, last_result_handle); 1672 break; 1673 } 1674 default: 1675 break; 1676 } 1677 } 1678 1679 static void gatt_client_handle_att_write_response(gatt_client_t *gatt_client) { 1680 switch (gatt_client->gatt_client_state) { 1681 case P_W4_WRITE_CHARACTERISTIC_VALUE_RESULT: 1682 gatt_client_handle_transaction_complete(gatt_client, ATT_ERROR_SUCCESS); 1683 break; 1684 case P_W4_CLIENT_CHARACTERISTIC_CONFIGURATION_RESULT: 1685 gatt_client_handle_transaction_complete(gatt_client, ATT_ERROR_SUCCESS); 1686 break; 1687 case P_W4_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT: 1688 gatt_client_handle_transaction_complete(gatt_client, ATT_ERROR_SUCCESS); 1689 break; 1690 default: 1691 break; 1692 } 1693 } 1694 1695 static void gatt_client_handle_att_response(gatt_client_t * gatt_client, uint8_t * packet, uint16_t size) { 1696 uint8_t att_status; 1697 switch (packet[0]) { 1698 case ATT_EXCHANGE_MTU_RESPONSE: { 1699 if (size < 3u) break; 1700 bool update_gatt_server_att_mtu = false; 1701 uint16_t remote_rx_mtu = little_endian_read_16(packet, 1); 1702 uint16_t local_rx_mtu = l2cap_max_le_mtu(); 1703 switch (gatt_client->bearer_type){ 1704 case ATT_BEARER_UNENHANCED_LE: 1705 update_gatt_server_att_mtu = true; 1706 break; 1707 #ifdef ENABLE_GATT_OVER_CLASSIC 1708 case ATT_BEARER_UNENHANCED_CLASSIC: 1709 local_rx_mtu = gatt_client->mtu; 1710 break; 1711 #endif 1712 default: 1713 btstack_unreachable(); 1714 break; 1715 } 1716 1717 uint16_t mtu = (remote_rx_mtu < local_rx_mtu) ? remote_rx_mtu : local_rx_mtu; 1718 1719 // set gatt client mtu 1720 gatt_client->mtu = mtu; 1721 gatt_client->mtu_state = MTU_EXCHANGED; 1722 1723 if (update_gatt_server_att_mtu){ 1724 // set per connection mtu state - for fixed channel 1725 hci_connection_t *hci_connection = hci_connection_for_handle(gatt_client->con_handle); 1726 hci_connection->att_connection.mtu = gatt_client->mtu; 1727 hci_connection->att_connection.mtu_exchanged = true; 1728 } 1729 emit_gatt_mtu_exchanged_result_event(gatt_client, gatt_client->mtu); 1730 break; 1731 } 1732 case ATT_READ_BY_GROUP_TYPE_RESPONSE: 1733 switch (gatt_client->gatt_client_state) { 1734 case P_W4_SERVICE_QUERY_RESULT: 1735 report_gatt_services(gatt_client, packet, size); 1736 trigger_next_service_query(gatt_client, get_last_result_handle_from_service_list(packet, size)); 1737 // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done 1738 break; 1739 default: 1740 break; 1741 } 1742 break; 1743 case ATT_HANDLE_VALUE_NOTIFICATION: 1744 if (size < 3u) return; 1745 report_gatt_notification(gatt_client, little_endian_read_16(packet, 1u), &packet[3], size - 3u); 1746 return; 1747 #ifdef ENABLE_GATT_OVER_EATT 1748 case ATT_MULTIPLE_HANDLE_VALUE_NTF: 1749 if (size >= 5u) { 1750 uint16_t offset = 1; 1751 while (true){ 1752 uint16_t value_handle = little_endian_read_16(packet, offset); 1753 offset += 2; 1754 uint16_t value_length = little_endian_read_16(packet, offset); 1755 offset += 2; 1756 if ((offset + value_length) > size) break; 1757 report_gatt_notification(gatt_client, value_handle, &packet[offset], value_length); 1758 offset += value_length; 1759 } 1760 } 1761 return; 1762 #endif 1763 case ATT_HANDLE_VALUE_INDICATION: 1764 if (size < 3u) break; 1765 report_gatt_indication(gatt_client, little_endian_read_16(packet, 1u), &packet[3], size - 3u); 1766 gatt_client->send_confirmation = 1; 1767 break; 1768 case ATT_READ_BY_TYPE_RESPONSE: 1769 gatt_client_handle_att_read_by_type_response(gatt_client, packet, size); 1770 break; 1771 case ATT_READ_RESPONSE: 1772 gatt_client_handle_att_read_response(gatt_client, packet, size); 1773 break; 1774 case ATT_FIND_BY_TYPE_VALUE_RESPONSE: { 1775 uint8_t pair_size = 4; 1776 int i; 1777 uint16_t start_group_handle; 1778 uint16_t end_group_handle = 0xffff; // asserts GATT_EVENT_QUERY_COMPLETE is emitted if no results 1779 for (i = 1u; (i + pair_size) <= size; i += pair_size) { 1780 start_group_handle = little_endian_read_16(packet, i); 1781 end_group_handle = little_endian_read_16(packet, i + 2); 1782 emit_gatt_service_query_result_event(gatt_client, start_group_handle, end_group_handle, 1783 gatt_client->uuid128); 1784 } 1785 trigger_next_service_by_uuid_query(gatt_client, end_group_handle); 1786 // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done 1787 break; 1788 } 1789 case ATT_FIND_INFORMATION_REPLY: { 1790 if (size < 2u) break; 1791 1792 uint8_t pair_size = 4; 1793 if (packet[1u] == 2u) { 1794 pair_size = 18; 1795 } 1796 uint16_t offset = 2; 1797 1798 if (size < (pair_size + offset)) break; 1799 uint16_t last_descriptor_handle = little_endian_read_16(packet, size - pair_size); 1800 1801 #ifdef ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERY 1802 log_info("ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERY, state %x", gatt_client->gatt_client_state); 1803 if (gatt_client->gatt_client_state == P_W4_FIND_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY_RESULT){ 1804 // iterate over descriptors looking for CCC 1805 if (pair_size == 4){ 1806 while ((offset + 4) <= size){ 1807 uint16_t uuid16 = little_endian_read_16(packet, offset + 2); 1808 if (uuid16 == GATT_CLIENT_CHARACTERISTICS_CONFIGURATION){ 1809 gatt_client->client_characteristic_configuration_handle = little_endian_read_16(packet, offset); 1810 gatt_client->gatt_client_state = P_W2_WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION; 1811 log_info("CCC found %x", gatt_client->client_characteristic_configuration_handle); 1812 break; 1813 } 1814 offset += pair_size; 1815 } 1816 } 1817 if (is_query_done(gatt_client, last_descriptor_handle)){ 1818 1819 } else { 1820 // next 1821 gatt_client->start_group_handle = last_descriptor_handle + 1; 1822 gatt_client->gatt_client_state = P_W2_SEND_FIND_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY; 1823 } 1824 break; 1825 } 1826 #endif 1827 report_gatt_all_characteristic_descriptors(gatt_client, &packet[2], size - 2u, pair_size); 1828 trigger_next_characteristic_descriptor_query(gatt_client, last_descriptor_handle); 1829 // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done 1830 break; 1831 } 1832 1833 case ATT_WRITE_RESPONSE: 1834 gatt_client_handle_att_write_response(gatt_client); 1835 break; 1836 1837 case ATT_READ_BLOB_RESPONSE: { 1838 uint16_t received_blob_length = size - 1u; 1839 switch (gatt_client->gatt_client_state) { 1840 case P_W4_READ_BLOB_RESULT: 1841 report_gatt_long_characteristic_value_blob(gatt_client, gatt_client->attribute_handle, &packet[1], 1842 received_blob_length, gatt_client->attribute_offset); 1843 trigger_next_blob_query(gatt_client, P_W2_SEND_READ_BLOB_QUERY, received_blob_length); 1844 // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done 1845 break; 1846 case P_W4_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_RESULT: 1847 report_gatt_long_characteristic_descriptor(gatt_client, gatt_client->attribute_handle, 1848 &packet[1], received_blob_length, 1849 gatt_client->attribute_offset); 1850 trigger_next_blob_query(gatt_client, P_W2_SEND_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_QUERY, 1851 received_blob_length); 1852 // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done 1853 break; 1854 default: 1855 break; 1856 } 1857 break; 1858 } 1859 case ATT_PREPARE_WRITE_RESPONSE: 1860 switch (gatt_client->gatt_client_state) { 1861 case P_W4_PREPARE_WRITE_SINGLE_RESULT: 1862 if (is_value_valid(gatt_client, packet, size)) { 1863 att_status = ATT_ERROR_SUCCESS; 1864 } else { 1865 att_status = ATT_ERROR_DATA_MISMATCH; 1866 } 1867 gatt_client_handle_transaction_complete(gatt_client, att_status); 1868 break; 1869 1870 case P_W4_PREPARE_WRITE_RESULT: { 1871 gatt_client->attribute_offset = little_endian_read_16(packet, 3); 1872 trigger_next_prepare_write_query(gatt_client, P_W2_PREPARE_WRITE, P_W2_EXECUTE_PREPARED_WRITE); 1873 // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done 1874 break; 1875 } 1876 case P_W4_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT: { 1877 gatt_client->attribute_offset = little_endian_read_16(packet, 3); 1878 trigger_next_prepare_write_query(gatt_client, P_W2_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR, 1879 P_W2_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR); 1880 // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done 1881 break; 1882 } 1883 case P_W4_PREPARE_RELIABLE_WRITE_RESULT: { 1884 if (is_value_valid(gatt_client, packet, size)) { 1885 gatt_client->attribute_offset = little_endian_read_16(packet, 3); 1886 trigger_next_prepare_write_query(gatt_client, P_W2_PREPARE_RELIABLE_WRITE, 1887 P_W2_EXECUTE_PREPARED_WRITE); 1888 // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done 1889 break; 1890 } 1891 gatt_client->gatt_client_state = P_W2_CANCEL_PREPARED_WRITE_DATA_MISMATCH; 1892 break; 1893 } 1894 default: 1895 break; 1896 } 1897 break; 1898 1899 case ATT_EXECUTE_WRITE_RESPONSE: 1900 switch (gatt_client->gatt_client_state) { 1901 case P_W4_EXECUTE_PREPARED_WRITE_RESULT: 1902 gatt_client_handle_transaction_complete(gatt_client, ATT_ERROR_SUCCESS); 1903 break; 1904 case P_W4_CANCEL_PREPARED_WRITE_RESULT: 1905 gatt_client_handle_transaction_complete(gatt_client, ATT_ERROR_SUCCESS); 1906 break; 1907 case P_W4_CANCEL_PREPARED_WRITE_DATA_MISMATCH_RESULT: 1908 gatt_client_handle_transaction_complete(gatt_client, ATT_ERROR_DATA_MISMATCH); 1909 break; 1910 case P_W4_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT: 1911 gatt_client_handle_transaction_complete(gatt_client, ATT_ERROR_SUCCESS); 1912 break; 1913 default: 1914 break; 1915 1916 } 1917 break; 1918 1919 case ATT_READ_MULTIPLE_RESPONSE: 1920 switch (gatt_client->gatt_client_state) { 1921 case P_W4_READ_MULTIPLE_RESPONSE: 1922 report_gatt_characteristic_value(gatt_client, 0u, &packet[1], size - 1u); 1923 gatt_client_handle_transaction_complete(gatt_client, ATT_ERROR_SUCCESS); 1924 break; 1925 default: 1926 break; 1927 } 1928 break; 1929 1930 #ifdef ENABLE_GATT_OVER_EATT 1931 case ATT_READ_MULTIPLE_VARIABLE_RSP: 1932 switch (gatt_client->gatt_client_state) { 1933 case P_W4_READ_MULTIPLE_VARIABLE_RESPONSE: 1934 report_gatt_characteristic_value(gatt_client, 0u, &packet[1], size - 1u); 1935 gatt_client_handle_transaction_complete(gatt_client, ATT_ERROR_SUCCESS); 1936 break; 1937 default: 1938 break; 1939 } 1940 break; 1941 #endif 1942 1943 case ATT_ERROR_RESPONSE: 1944 if (size < 5u) return; 1945 att_status = packet[4]; 1946 switch (att_status) { 1947 case ATT_ERROR_ATTRIBUTE_NOT_FOUND: { 1948 switch (gatt_client->gatt_client_state) { 1949 case P_W4_SERVICE_QUERY_RESULT: 1950 case P_W4_SERVICE_WITH_UUID_RESULT: 1951 case P_W4_INCLUDED_SERVICE_QUERY_RESULT: 1952 case P_W4_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY_RESULT: 1953 gatt_client_handle_transaction_complete(gatt_client, ATT_ERROR_SUCCESS); 1954 break; 1955 case P_W4_ALL_CHARACTERISTICS_OF_SERVICE_QUERY_RESULT: 1956 case P_W4_CHARACTERISTIC_WITH_UUID_QUERY_RESULT: 1957 report_gatt_characteristic_end_found(gatt_client, gatt_client->end_group_handle); 1958 gatt_client_handle_transaction_complete(gatt_client, ATT_ERROR_SUCCESS); 1959 break; 1960 case P_W4_READ_BY_TYPE_RESPONSE: 1961 if (gatt_client->start_group_handle == gatt_client->query_start_handle) { 1962 att_status = ATT_ERROR_ATTRIBUTE_NOT_FOUND; 1963 } else { 1964 att_status = ATT_ERROR_SUCCESS; 1965 } 1966 gatt_client_handle_transaction_complete(gatt_client, att_status); 1967 break; 1968 default: 1969 gatt_client_report_error_if_pending(gatt_client, att_status); 1970 break; 1971 } 1972 break; 1973 } 1974 1975 #ifdef ENABLE_GATT_CLIENT_PAIRING 1976 1977 case ATT_ERROR_INSUFFICIENT_AUTHENTICATION: 1978 case ATT_ERROR_INSUFFICIENT_ENCRYPTION_KEY_SIZE: 1979 case ATT_ERROR_INSUFFICIENT_ENCRYPTION: { 1980 1981 // security too low 1982 if (gatt_client->security_counter > 0) { 1983 gatt_client_report_error_if_pending(gatt_client, att_status); 1984 break; 1985 } 1986 // start security 1987 gatt_client->security_counter++; 1988 1989 // setup action 1990 int retry = 1; 1991 switch (gatt_client->gatt_client_state){ 1992 case P_W4_READ_CHARACTERISTIC_VALUE_RESULT: 1993 gatt_client->gatt_client_state = P_W2_SEND_READ_CHARACTERISTIC_VALUE_QUERY ; 1994 break; 1995 case P_W4_READ_BLOB_RESULT: 1996 gatt_client->gatt_client_state = P_W2_SEND_READ_BLOB_QUERY; 1997 break; 1998 case P_W4_READ_BY_TYPE_RESPONSE: 1999 gatt_client->gatt_client_state = P_W2_SEND_READ_BY_TYPE_REQUEST; 2000 break; 2001 case P_W4_READ_MULTIPLE_RESPONSE: 2002 gatt_client->gatt_client_state = P_W2_SEND_READ_MULTIPLE_REQUEST; 2003 break; 2004 case P_W4_READ_MULTIPLE_VARIABLE_RESPONSE: 2005 gatt_client->gatt_client_state = P_W2_SEND_READ_MULTIPLE_VARIABLE_REQUEST; 2006 break; 2007 case P_W4_WRITE_CHARACTERISTIC_VALUE_RESULT: 2008 gatt_client->gatt_client_state = P_W2_SEND_WRITE_CHARACTERISTIC_VALUE; 2009 break; 2010 case P_W4_PREPARE_WRITE_RESULT: 2011 gatt_client->gatt_client_state = P_W2_PREPARE_WRITE; 2012 break; 2013 case P_W4_PREPARE_WRITE_SINGLE_RESULT: 2014 gatt_client->gatt_client_state = P_W2_PREPARE_WRITE_SINGLE; 2015 break; 2016 case P_W4_PREPARE_RELIABLE_WRITE_RESULT: 2017 gatt_client->gatt_client_state = P_W2_PREPARE_RELIABLE_WRITE; 2018 break; 2019 case P_W4_EXECUTE_PREPARED_WRITE_RESULT: 2020 gatt_client->gatt_client_state = P_W2_EXECUTE_PREPARED_WRITE; 2021 break; 2022 case P_W4_CANCEL_PREPARED_WRITE_RESULT: 2023 gatt_client->gatt_client_state = P_W2_CANCEL_PREPARED_WRITE; 2024 break; 2025 case P_W4_CANCEL_PREPARED_WRITE_DATA_MISMATCH_RESULT: 2026 gatt_client->gatt_client_state = P_W2_CANCEL_PREPARED_WRITE_DATA_MISMATCH; 2027 break; 2028 case P_W4_READ_CHARACTERISTIC_DESCRIPTOR_RESULT: 2029 gatt_client->gatt_client_state = P_W2_SEND_READ_CHARACTERISTIC_DESCRIPTOR_QUERY; 2030 break; 2031 case P_W4_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_RESULT: 2032 gatt_client->gatt_client_state = P_W2_SEND_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_QUERY; 2033 break; 2034 case P_W4_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT: 2035 gatt_client->gatt_client_state = P_W2_SEND_WRITE_CHARACTERISTIC_DESCRIPTOR; 2036 break; 2037 case P_W4_CLIENT_CHARACTERISTIC_CONFIGURATION_RESULT: 2038 gatt_client->gatt_client_state = P_W2_WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION; 2039 break; 2040 case P_W4_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT: 2041 gatt_client->gatt_client_state = P_W2_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR; 2042 break; 2043 case P_W4_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT: 2044 gatt_client->gatt_client_state = P_W2_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR; 2045 break; 2046 #ifdef ENABLE_LE_SIGNED_WRITE 2047 case P_W4_SEND_SINGED_WRITE_DONE: 2048 gatt_client->gatt_client_state = P_W2_SEND_SIGNED_WRITE; 2049 break; 2050 #endif 2051 default: 2052 log_info("retry not supported for state %x", gatt_client->gatt_client_state); 2053 retry = 0; 2054 break; 2055 } 2056 2057 if (!retry) { 2058 gatt_client_report_error_if_pending(gatt_client, att_status); 2059 break; 2060 } 2061 2062 log_info("security error, start pairing"); 2063 2064 // start pairing for higher security level 2065 gatt_client->wait_for_authentication_complete = 1; 2066 gatt_client->pending_error_code = att_status; 2067 sm_request_pairing(gatt_client->con_handle); 2068 break; 2069 } 2070 #endif 2071 2072 // nothing we can do about that 2073 case ATT_ERROR_INSUFFICIENT_AUTHORIZATION: 2074 default: 2075 gatt_client_report_error_if_pending(gatt_client, att_status); 2076 break; 2077 } 2078 break; 2079 2080 default: 2081 log_info("ATT Handler, unhandled response type 0x%02x", packet[0]); 2082 break; 2083 } 2084 } 2085 2086 static void gatt_client_att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size) { 2087 gatt_client_t *gatt_client; 2088 if (size < 1u) return; 2089 2090 if (packet_type == HCI_EVENT_PACKET) { 2091 switch (packet[0]) { 2092 case L2CAP_EVENT_CAN_SEND_NOW: 2093 gatt_client_run(); 2094 break; 2095 // att_server has negotiated the mtu for this connection, cache if context exists 2096 case ATT_EVENT_MTU_EXCHANGE_COMPLETE: 2097 if (size < 6u) break; 2098 gatt_client = gatt_client_get_context_for_handle(handle); 2099 if (gatt_client == NULL) break; 2100 gatt_client->mtu = little_endian_read_16(packet, 4); 2101 break; 2102 default: 2103 break; 2104 } 2105 return; 2106 } 2107 2108 if (packet_type != ATT_DATA_PACKET) return; 2109 2110 // special cases: notifications & indications motivate creating context 2111 switch (packet[0]) { 2112 case ATT_HANDLE_VALUE_NOTIFICATION: 2113 case ATT_HANDLE_VALUE_INDICATION: 2114 gatt_client_provide_context_for_handle(handle, &gatt_client); 2115 break; 2116 default: 2117 gatt_client = gatt_client_get_context_for_handle(handle); 2118 break; 2119 } 2120 2121 if (gatt_client != NULL) { 2122 gatt_client_handle_att_response(gatt_client, packet, size); 2123 gatt_client_run(); 2124 } 2125 } 2126 2127 #ifdef ENABLE_LE_SIGNED_WRITE 2128 static void att_signed_write_handle_cmac_result(uint8_t hash[8]){ 2129 btstack_linked_list_iterator_t it; 2130 btstack_linked_list_iterator_init(&it, &gatt_client_connections); 2131 while (btstack_linked_list_iterator_has_next(&it)){ 2132 gatt_client_t * gatt_client = (gatt_client_t *) btstack_linked_list_iterator_next(&it); 2133 if (gatt_client->gatt_client_state == P_W4_CMAC_RESULT){ 2134 // store result 2135 (void)memcpy(gatt_client->cmac, hash, 8); 2136 // reverse_64(hash, gatt_client->cmac); 2137 gatt_client->gatt_client_state = P_W2_SEND_SIGNED_WRITE; 2138 gatt_client_run(); 2139 return; 2140 } 2141 } 2142 } 2143 2144 uint8_t gatt_client_signed_write_without_response(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t value_handle, uint16_t message_len, uint8_t * message){ 2145 gatt_client_t * gatt_client; 2146 uint8_t status = gatt_client_provide_context_for_handle(con_handle, &gatt_client); 2147 if (status != ERROR_CODE_SUCCESS){ 2148 return status; 2149 } 2150 if (is_ready(gatt_client) == 0){ 2151 return GATT_CLIENT_IN_WRONG_STATE; 2152 } 2153 2154 gatt_client->callback = callback; 2155 gatt_client->attribute_handle = value_handle; 2156 gatt_client->attribute_length = message_len; 2157 gatt_client->attribute_value = message; 2158 gatt_client->gatt_client_state = P_W4_IDENTITY_RESOLVING; 2159 gatt_client_run(); 2160 return ERROR_CODE_SUCCESS; 2161 } 2162 #endif 2163 2164 uint8_t gatt_client_discover_primary_services(btstack_packet_handler_t callback, hci_con_handle_t con_handle){ 2165 gatt_client_t * gatt_client; 2166 uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client); 2167 if (status != ERROR_CODE_SUCCESS){ 2168 return status; 2169 } 2170 2171 gatt_client->callback = callback; 2172 gatt_client->start_group_handle = 0x0001; 2173 gatt_client->end_group_handle = 0xffff; 2174 gatt_client->gatt_client_state = P_W2_SEND_SERVICE_QUERY; 2175 gatt_client->uuid16 = GATT_PRIMARY_SERVICE_UUID; 2176 gatt_client_run(); 2177 return ERROR_CODE_SUCCESS; 2178 } 2179 2180 uint8_t gatt_client_discover_secondary_services(btstack_packet_handler_t callback, hci_con_handle_t con_handle){ 2181 gatt_client_t * gatt_client; 2182 uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client); 2183 if (status != ERROR_CODE_SUCCESS){ 2184 return status; 2185 } 2186 2187 gatt_client->callback = callback; 2188 gatt_client->start_group_handle = 0x0001; 2189 gatt_client->end_group_handle = 0xffff; 2190 gatt_client->gatt_client_state = P_W2_SEND_SERVICE_QUERY; 2191 gatt_client->uuid16 = GATT_SECONDARY_SERVICE_UUID; 2192 gatt_client_run(); 2193 return ERROR_CODE_SUCCESS; 2194 } 2195 2196 uint8_t gatt_client_discover_primary_services_by_uuid16(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t uuid16){ 2197 gatt_client_t * gatt_client; 2198 uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client); 2199 if (status != ERROR_CODE_SUCCESS){ 2200 return status; 2201 } 2202 2203 gatt_client->callback = callback; 2204 gatt_client->start_group_handle = 0x0001; 2205 gatt_client->end_group_handle = 0xffff; 2206 gatt_client->gatt_client_state = P_W2_SEND_SERVICE_WITH_UUID_QUERY; 2207 gatt_client->uuid16 = uuid16; 2208 uuid_add_bluetooth_prefix((uint8_t*) &(gatt_client->uuid128), gatt_client->uuid16); 2209 gatt_client_run(); 2210 return ERROR_CODE_SUCCESS; 2211 } 2212 2213 uint8_t gatt_client_discover_primary_services_by_uuid128(btstack_packet_handler_t callback, hci_con_handle_t con_handle, const uint8_t * uuid128){ 2214 gatt_client_t * gatt_client; 2215 uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client); 2216 if (status != ERROR_CODE_SUCCESS){ 2217 return status; 2218 } 2219 2220 gatt_client->callback = callback; 2221 gatt_client->start_group_handle = 0x0001; 2222 gatt_client->end_group_handle = 0xffff; 2223 gatt_client->uuid16 = 0; 2224 (void)memcpy(gatt_client->uuid128, uuid128, 16); 2225 gatt_client->gatt_client_state = P_W2_SEND_SERVICE_WITH_UUID_QUERY; 2226 gatt_client_run(); 2227 return ERROR_CODE_SUCCESS; 2228 } 2229 2230 uint8_t gatt_client_discover_characteristics_for_service(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_service_t * service){ 2231 gatt_client_t * gatt_client; 2232 uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client); 2233 if (status != ERROR_CODE_SUCCESS){ 2234 return status; 2235 } 2236 2237 gatt_client->callback = callback; 2238 gatt_client->start_group_handle = service->start_group_handle; 2239 gatt_client->end_group_handle = service->end_group_handle; 2240 gatt_client->filter_with_uuid = 0; 2241 gatt_client->characteristic_start_handle = 0; 2242 gatt_client->gatt_client_state = P_W2_SEND_ALL_CHARACTERISTICS_OF_SERVICE_QUERY; 2243 gatt_client_run(); 2244 return ERROR_CODE_SUCCESS; 2245 } 2246 2247 uint8_t gatt_client_find_included_services_for_service(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_service_t * service){ 2248 gatt_client_t * gatt_client; 2249 uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client); 2250 if (status != ERROR_CODE_SUCCESS){ 2251 return status; 2252 } 2253 2254 gatt_client->callback = callback; 2255 gatt_client->start_group_handle = service->start_group_handle; 2256 gatt_client->end_group_handle = service->end_group_handle; 2257 gatt_client->gatt_client_state = P_W2_SEND_INCLUDED_SERVICE_QUERY; 2258 2259 gatt_client_run(); 2260 return ERROR_CODE_SUCCESS; 2261 } 2262 2263 uint8_t gatt_client_discover_characteristics_for_handle_range_by_uuid16(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t start_handle, uint16_t end_handle, uint16_t uuid16){ 2264 gatt_client_t * gatt_client; 2265 uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client); 2266 if (status != ERROR_CODE_SUCCESS){ 2267 return status; 2268 } 2269 2270 gatt_client->callback = callback; 2271 gatt_client->start_group_handle = start_handle; 2272 gatt_client->end_group_handle = end_handle; 2273 gatt_client->filter_with_uuid = 1; 2274 gatt_client->uuid16 = uuid16; 2275 uuid_add_bluetooth_prefix((uint8_t*) &(gatt_client->uuid128), uuid16); 2276 gatt_client->characteristic_start_handle = 0; 2277 gatt_client->gatt_client_state = P_W2_SEND_CHARACTERISTIC_WITH_UUID_QUERY; 2278 gatt_client_run(); 2279 return ERROR_CODE_SUCCESS; 2280 } 2281 2282 uint8_t gatt_client_discover_characteristics_for_handle_range_by_uuid128(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t start_handle, uint16_t end_handle, const uint8_t * uuid128){ 2283 gatt_client_t * gatt_client; 2284 uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client); 2285 if (status != ERROR_CODE_SUCCESS){ 2286 return status; 2287 } 2288 2289 gatt_client->callback = callback; 2290 gatt_client->start_group_handle = start_handle; 2291 gatt_client->end_group_handle = end_handle; 2292 gatt_client->filter_with_uuid = 1; 2293 gatt_client->uuid16 = 0; 2294 (void)memcpy(gatt_client->uuid128, uuid128, 16); 2295 gatt_client->characteristic_start_handle = 0; 2296 gatt_client->gatt_client_state = P_W2_SEND_CHARACTERISTIC_WITH_UUID_QUERY; 2297 gatt_client_run(); 2298 return ERROR_CODE_SUCCESS; 2299 } 2300 2301 2302 uint8_t gatt_client_discover_characteristics_for_service_by_uuid16(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_service_t * service, uint16_t uuid16){ 2303 return gatt_client_discover_characteristics_for_handle_range_by_uuid16(callback, con_handle, service->start_group_handle, service->end_group_handle, uuid16); 2304 } 2305 2306 uint8_t gatt_client_discover_characteristics_for_service_by_uuid128(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_service_t * service, const uint8_t * uuid128){ 2307 return gatt_client_discover_characteristics_for_handle_range_by_uuid128(callback, con_handle, service->start_group_handle, service->end_group_handle, uuid128); 2308 } 2309 2310 uint8_t gatt_client_discover_characteristic_descriptors(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_characteristic_t * characteristic){ 2311 gatt_client_t * gatt_client; 2312 uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client); 2313 if (status != ERROR_CODE_SUCCESS){ 2314 return status; 2315 } 2316 2317 if (characteristic->value_handle == characteristic->end_handle){ 2318 return ERROR_CODE_SUCCESS; 2319 } 2320 gatt_client->callback = callback; 2321 gatt_client->start_group_handle = characteristic->value_handle + 1u; 2322 gatt_client->end_group_handle = characteristic->end_handle; 2323 gatt_client->gatt_client_state = P_W2_SEND_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY; 2324 gatt_client_run(); 2325 return ERROR_CODE_SUCCESS; 2326 } 2327 2328 uint8_t gatt_client_read_value_of_characteristic_using_value_handle(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t value_handle){ 2329 gatt_client_t * gatt_client; 2330 uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client); 2331 if (status != ERROR_CODE_SUCCESS){ 2332 return status; 2333 } 2334 2335 gatt_client->callback = callback; 2336 gatt_client->attribute_handle = value_handle; 2337 gatt_client->attribute_offset = 0; 2338 gatt_client->gatt_client_state = P_W2_SEND_READ_CHARACTERISTIC_VALUE_QUERY; 2339 gatt_client_run(); 2340 return ERROR_CODE_SUCCESS; 2341 } 2342 2343 uint8_t gatt_client_read_value_of_characteristics_by_uuid16(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t start_handle, uint16_t end_handle, uint16_t uuid16){ 2344 gatt_client_t * gatt_client; 2345 uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client); 2346 if (status != ERROR_CODE_SUCCESS){ 2347 return status; 2348 } 2349 2350 gatt_client->callback = callback; 2351 gatt_client->start_group_handle = start_handle; 2352 gatt_client->end_group_handle = end_handle; 2353 gatt_client->query_start_handle = start_handle; 2354 gatt_client->query_end_handle = end_handle; 2355 gatt_client->uuid16 = uuid16; 2356 uuid_add_bluetooth_prefix((uint8_t*) &(gatt_client->uuid128), uuid16); 2357 gatt_client->gatt_client_state = P_W2_SEND_READ_BY_TYPE_REQUEST; 2358 gatt_client_run(); 2359 return ERROR_CODE_SUCCESS; 2360 } 2361 2362 uint8_t gatt_client_read_value_of_characteristics_by_uuid128(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t start_handle, uint16_t end_handle, const uint8_t * uuid128){ 2363 gatt_client_t * gatt_client; 2364 uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client); 2365 if (status != ERROR_CODE_SUCCESS){ 2366 return status; 2367 } 2368 2369 gatt_client->callback = callback; 2370 gatt_client->start_group_handle = start_handle; 2371 gatt_client->end_group_handle = end_handle; 2372 gatt_client->query_start_handle = start_handle; 2373 gatt_client->query_end_handle = end_handle; 2374 gatt_client->uuid16 = 0; 2375 (void)memcpy(gatt_client->uuid128, uuid128, 16); 2376 gatt_client->gatt_client_state = P_W2_SEND_READ_BY_TYPE_REQUEST; 2377 gatt_client_run(); 2378 return ERROR_CODE_SUCCESS; 2379 } 2380 2381 2382 uint8_t gatt_client_read_value_of_characteristic(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_characteristic_t * characteristic){ 2383 return gatt_client_read_value_of_characteristic_using_value_handle(callback, con_handle, characteristic->value_handle); 2384 } 2385 2386 uint8_t gatt_client_read_long_value_of_characteristic_using_value_handle_with_offset(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t value_handle, uint16_t offset){ 2387 gatt_client_t * gatt_client; 2388 uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client); 2389 if (status != ERROR_CODE_SUCCESS){ 2390 return status; 2391 } 2392 2393 gatt_client->callback = callback; 2394 gatt_client->attribute_handle = value_handle; 2395 gatt_client->attribute_offset = offset; 2396 gatt_client->gatt_client_state = P_W2_SEND_READ_BLOB_QUERY; 2397 gatt_client_run(); 2398 return ERROR_CODE_SUCCESS; 2399 } 2400 2401 uint8_t gatt_client_read_long_value_of_characteristic_using_value_handle(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t value_handle){ 2402 return gatt_client_read_long_value_of_characteristic_using_value_handle_with_offset(callback, con_handle, value_handle, 0); 2403 } 2404 2405 uint8_t gatt_client_read_long_value_of_characteristic(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_characteristic_t * characteristic){ 2406 return gatt_client_read_long_value_of_characteristic_using_value_handle(callback, con_handle, characteristic->value_handle); 2407 } 2408 2409 static uint8_t gatt_client_read_multiple_characteristic_values_with_state(btstack_packet_handler_t callback, hci_con_handle_t con_handle, int num_value_handles, uint16_t * value_handles, gatt_client_state_t state){ 2410 gatt_client_t * gatt_client; 2411 uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client); 2412 if (status != ERROR_CODE_SUCCESS){ 2413 return status; 2414 } 2415 2416 #ifdef ENABLE_GATT_OVER_EATT 2417 if (state == P_W2_SEND_READ_MULTIPLE_VARIABLE_REQUEST){ 2418 if (gatt_client->bearer_type != ATT_BEARER_ENHANCED_LE){ 2419 return ERROR_CODE_COMMAND_DISALLOWED; 2420 } 2421 } 2422 #endif 2423 2424 gatt_client->callback = callback; 2425 gatt_client->read_multiple_handle_count = num_value_handles; 2426 gatt_client->read_multiple_handles = value_handles; 2427 gatt_client->gatt_client_state = state; 2428 gatt_client_run(); 2429 return ERROR_CODE_SUCCESS; 2430 } 2431 2432 uint8_t gatt_client_read_multiple_characteristic_values(btstack_packet_handler_t callback, hci_con_handle_t con_handle, int num_value_handles, uint16_t * value_handles){ 2433 return gatt_client_read_multiple_characteristic_values_with_state(callback, con_handle, num_value_handles, value_handles, P_W2_SEND_READ_MULTIPLE_REQUEST); 2434 } 2435 2436 #ifdef ENABLE_GATT_OVER_EATT 2437 uint8_t gatt_client_read_multiple_variable_characteristic_values(btstack_packet_handler_t callback, hci_con_handle_t con_handle, int num_value_handles, uint16_t * value_handles){ 2438 return gatt_client_read_multiple_characteristic_values_with_state(callback, con_handle, num_value_handles, value_handles, P_W2_SEND_READ_MULTIPLE_VARIABLE_REQUEST); 2439 } 2440 #endif 2441 2442 uint8_t gatt_client_write_value_of_characteristic_without_response(hci_con_handle_t con_handle, uint16_t value_handle, uint16_t value_length, uint8_t * value){ 2443 gatt_client_t * gatt_client; 2444 uint8_t status = gatt_client_provide_context_for_handle(con_handle, &gatt_client); 2445 if (status != ERROR_CODE_SUCCESS){ 2446 return status; 2447 } 2448 2449 if (value_length > (gatt_client->mtu - 3u)) return GATT_CLIENT_VALUE_TOO_LONG; 2450 if (!att_dispatch_client_can_send_now(gatt_client->con_handle)) return GATT_CLIENT_BUSY; 2451 2452 return att_write_request(gatt_client, ATT_WRITE_COMMAND, value_handle, value_length, value); 2453 } 2454 2455 uint8_t gatt_client_write_value_of_characteristic(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t value_handle, uint16_t value_length, uint8_t * value){ 2456 gatt_client_t * gatt_client; 2457 uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client); 2458 if (status != ERROR_CODE_SUCCESS){ 2459 return status; 2460 } 2461 2462 gatt_client->callback = callback; 2463 gatt_client->attribute_handle = value_handle; 2464 gatt_client->attribute_length = value_length; 2465 gatt_client->attribute_value = value; 2466 gatt_client->gatt_client_state = P_W2_SEND_WRITE_CHARACTERISTIC_VALUE; 2467 gatt_client_run(); 2468 return ERROR_CODE_SUCCESS; 2469 } 2470 2471 uint8_t gatt_client_write_long_value_of_characteristic_with_offset(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t value_handle, uint16_t offset, uint16_t value_length, uint8_t * value){ 2472 gatt_client_t * gatt_client; 2473 uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client); 2474 if (status != ERROR_CODE_SUCCESS){ 2475 return status; 2476 } 2477 2478 gatt_client->callback = callback; 2479 gatt_client->attribute_handle = value_handle; 2480 gatt_client->attribute_length = value_length; 2481 gatt_client->attribute_offset = offset; 2482 gatt_client->attribute_value = value; 2483 gatt_client->gatt_client_state = P_W2_PREPARE_WRITE; 2484 gatt_client_run(); 2485 return ERROR_CODE_SUCCESS; 2486 } 2487 2488 uint8_t gatt_client_write_long_value_of_characteristic(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t value_handle, uint16_t value_length, uint8_t * value){ 2489 return gatt_client_write_long_value_of_characteristic_with_offset(callback, con_handle, value_handle, 0, value_length, value); 2490 } 2491 2492 uint8_t gatt_client_reliable_write_long_value_of_characteristic(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t value_handle, uint16_t value_length, uint8_t * value){ 2493 gatt_client_t * gatt_client; 2494 uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client); 2495 if (status != ERROR_CODE_SUCCESS){ 2496 return status; 2497 } 2498 2499 gatt_client->callback = callback; 2500 gatt_client->attribute_handle = value_handle; 2501 gatt_client->attribute_length = value_length; 2502 gatt_client->attribute_offset = 0; 2503 gatt_client->attribute_value = value; 2504 gatt_client->gatt_client_state = P_W2_PREPARE_RELIABLE_WRITE; 2505 gatt_client_run(); 2506 return ERROR_CODE_SUCCESS; 2507 } 2508 2509 uint8_t gatt_client_write_client_characteristic_configuration(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_characteristic_t * characteristic, uint16_t configuration){ 2510 gatt_client_t * gatt_client; 2511 uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client); 2512 if (status != ERROR_CODE_SUCCESS){ 2513 return status; 2514 } 2515 2516 if ( (configuration & GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION) && 2517 ((characteristic->properties & ATT_PROPERTY_NOTIFY) == 0u)) { 2518 log_info("gatt_client_write_client_characteristic_configuration: GATT_CLIENT_CHARACTERISTIC_NOTIFICATION_NOT_SUPPORTED"); 2519 return GATT_CLIENT_CHARACTERISTIC_NOTIFICATION_NOT_SUPPORTED; 2520 } else if ( (configuration & GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_INDICATION) && 2521 ((characteristic->properties & ATT_PROPERTY_INDICATE) == 0u)){ 2522 log_info("gatt_client_write_client_characteristic_configuration: GATT_CLIENT_CHARACTERISTIC_INDICATION_NOT_SUPPORTED"); 2523 return GATT_CLIENT_CHARACTERISTIC_INDICATION_NOT_SUPPORTED; 2524 } 2525 2526 gatt_client->callback = callback; 2527 gatt_client->start_group_handle = characteristic->value_handle; 2528 gatt_client->end_group_handle = characteristic->end_handle; 2529 little_endian_store_16(gatt_client->client_characteristic_configuration_value, 0, configuration); 2530 2531 #ifdef ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERY 2532 gatt_client->gatt_client_state = P_W2_SEND_FIND_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY; 2533 #else 2534 gatt_client->gatt_client_state = P_W2_SEND_READ_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY; 2535 #endif 2536 gatt_client_run(); 2537 return ERROR_CODE_SUCCESS; 2538 } 2539 2540 uint8_t gatt_client_read_characteristic_descriptor_using_descriptor_handle(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t descriptor_handle){ 2541 gatt_client_t * gatt_client; 2542 uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client); 2543 if (status != ERROR_CODE_SUCCESS){ 2544 return status; 2545 } 2546 2547 gatt_client->callback = callback; 2548 gatt_client->attribute_handle = descriptor_handle; 2549 2550 gatt_client->gatt_client_state = P_W2_SEND_READ_CHARACTERISTIC_DESCRIPTOR_QUERY; 2551 gatt_client_run(); 2552 return ERROR_CODE_SUCCESS; 2553 } 2554 2555 uint8_t gatt_client_read_characteristic_descriptor(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_characteristic_descriptor_t * descriptor){ 2556 return gatt_client_read_characteristic_descriptor_using_descriptor_handle(callback, con_handle, descriptor->handle); 2557 } 2558 2559 uint8_t gatt_client_read_long_characteristic_descriptor_using_descriptor_handle_with_offset(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t descriptor_handle, uint16_t offset){ 2560 gatt_client_t * gatt_client; 2561 uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client); 2562 if (status != ERROR_CODE_SUCCESS){ 2563 return status; 2564 } 2565 2566 gatt_client->callback = callback; 2567 gatt_client->attribute_handle = descriptor_handle; 2568 gatt_client->attribute_offset = offset; 2569 gatt_client->gatt_client_state = P_W2_SEND_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_QUERY; 2570 gatt_client_run(); 2571 return ERROR_CODE_SUCCESS; 2572 } 2573 2574 uint8_t gatt_client_read_long_characteristic_descriptor_using_descriptor_handle(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t descriptor_handle){ 2575 return gatt_client_read_long_characteristic_descriptor_using_descriptor_handle_with_offset(callback, con_handle, descriptor_handle, 0); 2576 } 2577 2578 uint8_t gatt_client_read_long_characteristic_descriptor(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_characteristic_descriptor_t * descriptor){ 2579 return gatt_client_read_long_characteristic_descriptor_using_descriptor_handle(callback, con_handle, descriptor->handle); 2580 } 2581 2582 uint8_t gatt_client_write_characteristic_descriptor_using_descriptor_handle(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t descriptor_handle, uint16_t value_length, uint8_t * value){ 2583 gatt_client_t * gatt_client; 2584 uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client); 2585 if (status != ERROR_CODE_SUCCESS){ 2586 return status; 2587 } 2588 2589 gatt_client->callback = callback; 2590 gatt_client->attribute_handle = descriptor_handle; 2591 gatt_client->attribute_length = value_length; 2592 gatt_client->attribute_offset = 0; 2593 gatt_client->attribute_value = value; 2594 gatt_client->gatt_client_state = P_W2_SEND_WRITE_CHARACTERISTIC_DESCRIPTOR; 2595 gatt_client_run(); 2596 return ERROR_CODE_SUCCESS; 2597 } 2598 2599 uint8_t gatt_client_write_characteristic_descriptor(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_characteristic_descriptor_t * descriptor, uint16_t value_length, uint8_t * value){ 2600 return gatt_client_write_characteristic_descriptor_using_descriptor_handle(callback, con_handle, descriptor->handle, value_length, value); 2601 } 2602 2603 uint8_t gatt_client_write_long_characteristic_descriptor_using_descriptor_handle_with_offset(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t descriptor_handle, uint16_t offset, uint16_t value_length, uint8_t * value){ 2604 gatt_client_t * gatt_client; 2605 uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client); 2606 if (status != ERROR_CODE_SUCCESS){ 2607 return status; 2608 } 2609 2610 gatt_client->callback = callback; 2611 gatt_client->attribute_handle = descriptor_handle; 2612 gatt_client->attribute_length = value_length; 2613 gatt_client->attribute_offset = offset; 2614 gatt_client->attribute_value = value; 2615 gatt_client->gatt_client_state = P_W2_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR; 2616 gatt_client_run(); 2617 return ERROR_CODE_SUCCESS; 2618 } 2619 2620 uint8_t gatt_client_write_long_characteristic_descriptor_using_descriptor_handle(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t descriptor_handle, uint16_t value_length, uint8_t * value){ 2621 return gatt_client_write_long_characteristic_descriptor_using_descriptor_handle_with_offset(callback, con_handle, descriptor_handle, 0, value_length, value); 2622 } 2623 2624 uint8_t gatt_client_write_long_characteristic_descriptor(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_characteristic_descriptor_t * descriptor, uint16_t value_length, uint8_t * value){ 2625 return gatt_client_write_long_characteristic_descriptor_using_descriptor_handle(callback, con_handle, descriptor->handle, value_length, value); 2626 } 2627 2628 /** 2629 * @brief -> gatt complete event 2630 */ 2631 uint8_t gatt_client_prepare_write(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t attribute_handle, uint16_t offset, uint16_t value_length, uint8_t * value){ 2632 gatt_client_t * gatt_client; 2633 uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client); 2634 if (status != ERROR_CODE_SUCCESS){ 2635 return status; 2636 } 2637 2638 gatt_client->callback = callback; 2639 gatt_client->attribute_handle = attribute_handle; 2640 gatt_client->attribute_length = value_length; 2641 gatt_client->attribute_offset = offset; 2642 gatt_client->attribute_value = value; 2643 gatt_client->gatt_client_state = P_W2_PREPARE_WRITE_SINGLE; 2644 gatt_client_run(); 2645 return ERROR_CODE_SUCCESS; 2646 } 2647 2648 /** 2649 * @brief -> gatt complete event 2650 */ 2651 uint8_t gatt_client_execute_write(btstack_packet_handler_t callback, hci_con_handle_t con_handle){ 2652 gatt_client_t * gatt_client; 2653 uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client); 2654 if (status != ERROR_CODE_SUCCESS){ 2655 return status; 2656 } 2657 2658 gatt_client->callback = callback; 2659 gatt_client->gatt_client_state = P_W2_EXECUTE_PREPARED_WRITE; 2660 gatt_client_run(); 2661 return ERROR_CODE_SUCCESS; 2662 } 2663 2664 /** 2665 * @brief -> gatt complete event 2666 */ 2667 uint8_t gatt_client_cancel_write(btstack_packet_handler_t callback, hci_con_handle_t con_handle){ 2668 gatt_client_t * gatt_client; 2669 uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client); 2670 if (status != ERROR_CODE_SUCCESS){ 2671 return status; 2672 } 2673 2674 gatt_client->callback = callback; 2675 gatt_client->gatt_client_state = P_W2_CANCEL_PREPARED_WRITE; 2676 gatt_client_run(); 2677 return ERROR_CODE_SUCCESS; 2678 } 2679 2680 void gatt_client_deserialize_service(const uint8_t *packet, int offset, gatt_client_service_t * service){ 2681 service->start_group_handle = little_endian_read_16(packet, offset); 2682 service->end_group_handle = little_endian_read_16(packet, offset + 2); 2683 reverse_128(&packet[offset + 4], service->uuid128); 2684 if (uuid_has_bluetooth_prefix(service->uuid128)){ 2685 service->uuid16 = big_endian_read_32(service->uuid128, 0); 2686 } else { 2687 service->uuid16 = 0; 2688 } 2689 } 2690 2691 void gatt_client_deserialize_characteristic(const uint8_t * packet, int offset, gatt_client_characteristic_t * characteristic){ 2692 characteristic->start_handle = little_endian_read_16(packet, offset); 2693 characteristic->value_handle = little_endian_read_16(packet, offset + 2); 2694 characteristic->end_handle = little_endian_read_16(packet, offset + 4); 2695 characteristic->properties = little_endian_read_16(packet, offset + 6); 2696 reverse_128(&packet[offset+8], characteristic->uuid128); 2697 if (uuid_has_bluetooth_prefix(characteristic->uuid128)){ 2698 characteristic->uuid16 = big_endian_read_32(characteristic->uuid128, 0); 2699 } else { 2700 characteristic->uuid16 = 0; 2701 } 2702 } 2703 2704 void gatt_client_deserialize_characteristic_descriptor(const uint8_t * packet, int offset, gatt_client_characteristic_descriptor_t * descriptor){ 2705 descriptor->handle = little_endian_read_16(packet, offset); 2706 reverse_128(&packet[offset+2], descriptor->uuid128); 2707 if (uuid_has_bluetooth_prefix(descriptor->uuid128)){ 2708 descriptor->uuid16 = big_endian_read_32(descriptor->uuid128, 0); 2709 } else { 2710 descriptor->uuid16 = 0; 2711 } 2712 } 2713 2714 void gatt_client_send_mtu_negotiation(btstack_packet_handler_t callback, hci_con_handle_t con_handle){ 2715 gatt_client_t * gatt_client; 2716 uint8_t status = gatt_client_provide_context_for_handle(con_handle, &gatt_client); 2717 if (status != ERROR_CODE_SUCCESS){ 2718 return; 2719 } 2720 if (gatt_client->mtu_state == MTU_AUTO_EXCHANGE_DISABLED){ 2721 gatt_client->callback = callback; 2722 gatt_client->mtu_state = SEND_MTU_EXCHANGE; 2723 gatt_client_run(); 2724 } 2725 } 2726 2727 uint8_t gatt_client_request_to_write_without_response(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){ 2728 gatt_client_t * gatt_client; 2729 uint8_t status = gatt_client_provide_context_for_handle(con_handle, &gatt_client); 2730 if (status != ERROR_CODE_SUCCESS){ 2731 return status; 2732 } 2733 bool added = btstack_linked_list_add_tail(&gatt_client->write_without_response_requests, (btstack_linked_item_t*) callback_registration); 2734 if (added == false){ 2735 return ERROR_CODE_COMMAND_DISALLOWED; 2736 } else { 2737 att_dispatch_client_request_can_send_now_event(gatt_client->con_handle); 2738 return ERROR_CODE_SUCCESS; 2739 } 2740 } 2741 2742 uint8_t gatt_client_request_to_send_gatt_query(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){ 2743 gatt_client_t * gatt_client; 2744 uint8_t status = gatt_client_provide_context_for_handle(con_handle, &gatt_client); 2745 if (status != ERROR_CODE_SUCCESS){ 2746 return status; 2747 } 2748 bool added = btstack_linked_list_add_tail(&gatt_client->query_requests, (btstack_linked_item_t*) callback_registration); 2749 if (added == false){ 2750 return ERROR_CODE_COMMAND_DISALLOWED; 2751 } else { 2752 gatt_client_notify_can_send_query(gatt_client); 2753 return ERROR_CODE_SUCCESS; 2754 } 2755 } 2756 2757 uint8_t gatt_client_request_can_write_without_response_event(btstack_packet_handler_t callback, hci_con_handle_t con_handle){ 2758 gatt_client_t * gatt_client; 2759 uint8_t status = gatt_client_provide_context_for_handle(con_handle, &gatt_client); 2760 if (status != ERROR_CODE_SUCCESS){ 2761 return status; 2762 } 2763 if (gatt_client->write_without_response_callback != NULL){ 2764 return GATT_CLIENT_IN_WRONG_STATE; 2765 } 2766 gatt_client->write_without_response_callback = callback; 2767 att_dispatch_client_request_can_send_now_event(gatt_client->con_handle); 2768 return ERROR_CODE_SUCCESS; 2769 } 2770 2771 2772 #if defined(ENABLE_GATT_OVER_CLASSIC) || defined(ENABLE_GATT_OVER_EATT) 2773 2774 #include "hci_event.h" 2775 2776 static const hci_event_t gatt_client_connected = { 2777 GATT_EVENT_CONNECTED, 0, "1BH" 2778 }; 2779 2780 static const hci_event_t gatt_client_disconnected = { 2781 GATT_EVENT_DISCONNECTED, 0, "H" 2782 }; 2783 #endif 2784 2785 #ifdef ENABLE_GATT_OVER_CLASSIC 2786 2787 #include "bluetooth_psm.h" 2788 2789 // single active SDP query 2790 static gatt_client_t * gatt_client_classic_active_sdp_query; 2791 2792 // macos protocol descriptor list requires 16 bytes 2793 static uint8_t gatt_client_classic_sdp_buffer[32]; 2794 2795 2796 static gatt_client_t * gatt_client_get_context_for_classic_addr(bd_addr_t addr){ 2797 btstack_linked_item_t *it; 2798 for (it = (btstack_linked_item_t *) gatt_client_connections; it != NULL; it = it->next){ 2799 gatt_client_t * gatt_client = (gatt_client_t *) it; 2800 if (memcmp(gatt_client->addr, addr, 6) == 0){ 2801 return gatt_client; 2802 } 2803 } 2804 return NULL; 2805 } 2806 2807 static gatt_client_t * gatt_client_get_context_for_l2cap_cid(uint16_t l2cap_cid){ 2808 btstack_linked_item_t *it; 2809 for (it = (btstack_linked_item_t *) gatt_client_connections; it != NULL; it = it->next){ 2810 gatt_client_t * gatt_client = (gatt_client_t *) it; 2811 if (gatt_client->l2cap_cid == l2cap_cid){ 2812 return gatt_client; 2813 } 2814 } 2815 return NULL; 2816 } 2817 2818 static void gatt_client_classic_handle_connected(gatt_client_t * gatt_client, uint8_t status){ 2819 bd_addr_t addr; 2820 // cppcheck-suppress uninitvar ; addr is reported as uninitialized although it's the destination of the memcpy 2821 memcpy(addr, gatt_client->addr, 6); 2822 hci_con_handle_t con_handle = gatt_client->con_handle; 2823 btstack_packet_handler_t callback = gatt_client->callback; 2824 if (status != ERROR_CODE_SUCCESS){ 2825 btstack_linked_list_remove(&gatt_client_connections, (btstack_linked_item_t *) gatt_client); 2826 btstack_memory_gatt_client_free(gatt_client); 2827 } 2828 uint8_t buffer[20]; 2829 uint16_t len = hci_event_create_from_template_and_arguments(buffer, sizeof(buffer), &gatt_client_connected, status, addr, 2830 con_handle); 2831 (*callback)(HCI_EVENT_PACKET, 0, buffer, len); 2832 } 2833 2834 static void gatt_client_classic_handle_disconnected(gatt_client_t * gatt_client){ 2835 2836 gatt_client_report_error_if_pending(gatt_client, ATT_ERROR_HCI_DISCONNECT_RECEIVED); 2837 gatt_client_timeout_stop(gatt_client); 2838 2839 hci_con_handle_t con_handle = gatt_client->con_handle; 2840 btstack_packet_handler_t callback = gatt_client->callback; 2841 btstack_linked_list_remove(&gatt_client_connections, (btstack_linked_item_t *) gatt_client); 2842 btstack_memory_gatt_client_free(gatt_client); 2843 2844 uint8_t buffer[20]; 2845 uint16_t len = hci_event_create_from_template_and_arguments(buffer, sizeof(buffer), &gatt_client_disconnected, con_handle); 2846 (*callback)(HCI_EVENT_PACKET, 0, buffer, len); 2847 } 2848 2849 static void gatt_client_l2cap_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 2850 gatt_client_t * gatt_client = NULL; 2851 uint8_t status; 2852 switch (packet_type){ 2853 case HCI_EVENT_PACKET: 2854 switch (hci_event_packet_get_type(packet)) { 2855 case L2CAP_EVENT_CHANNEL_OPENED: 2856 status = l2cap_event_channel_opened_get_status(packet); 2857 gatt_client = gatt_client_get_context_for_l2cap_cid(l2cap_event_channel_opened_get_local_cid(packet)); 2858 btstack_assert(gatt_client != NULL); 2859 // if status != 0, gatt_client will be discarded 2860 gatt_client->gatt_client_state = P_READY; 2861 gatt_client->con_handle = l2cap_event_channel_opened_get_handle(packet); 2862 gatt_client->mtu = l2cap_event_channel_opened_get_remote_mtu(packet); 2863 gatt_client_classic_handle_connected(gatt_client, status); 2864 break; 2865 case L2CAP_EVENT_CHANNEL_CLOSED: 2866 gatt_client = gatt_client_get_context_for_l2cap_cid(l2cap_event_channel_closed_get_local_cid(packet)); 2867 gatt_client_classic_handle_disconnected(gatt_client); 2868 break; 2869 default: 2870 break; 2871 } 2872 break; 2873 case L2CAP_DATA_PACKET: 2874 gatt_client = gatt_client_get_context_for_l2cap_cid(channel); 2875 btstack_assert(gatt_client != NULL); 2876 gatt_client_handle_att_response(gatt_client, packet, size); 2877 gatt_client_run(); 2878 break; 2879 default: 2880 break; 2881 } 2882 } 2883 2884 static void gatt_client_handle_sdp_client_query_attribute_value(gatt_client_t * connection, uint8_t *packet){ 2885 des_iterator_t des_list_it; 2886 des_iterator_t prot_it; 2887 2888 if (sdp_event_query_attribute_byte_get_attribute_length(packet) <= sizeof(gatt_client_classic_sdp_buffer)) { 2889 gatt_client_classic_sdp_buffer[sdp_event_query_attribute_byte_get_data_offset(packet)] = sdp_event_query_attribute_byte_get_data(packet); 2890 if ((uint16_t)(sdp_event_query_attribute_byte_get_data_offset(packet)+1) == sdp_event_query_attribute_byte_get_attribute_length(packet)) { 2891 switch(sdp_event_query_attribute_byte_get_attribute_id(packet)) { 2892 case BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST: 2893 for (des_iterator_init(&des_list_it, gatt_client_classic_sdp_buffer); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)) { 2894 uint8_t *des_element; 2895 uint8_t *element; 2896 uint32_t uuid; 2897 2898 if (des_iterator_get_type(&des_list_it) != DE_DES) continue; 2899 2900 des_element = des_iterator_get_element(&des_list_it); 2901 des_iterator_init(&prot_it, des_element); 2902 element = des_iterator_get_element(&prot_it); 2903 2904 if (de_get_element_type(element) != DE_UUID) continue; 2905 2906 uuid = de_get_uuid32(element); 2907 des_iterator_next(&prot_it); 2908 // we assume that the even if there are both roles supported, remote device uses the same psm and avdtp version for both 2909 switch (uuid){ 2910 case BLUETOOTH_PROTOCOL_L2CAP: 2911 if (!des_iterator_has_more(&prot_it)) continue; 2912 de_element_get_uint16(des_iterator_get_element(&prot_it), &connection->l2cap_psm); 2913 break; 2914 default: 2915 break; 2916 } 2917 } 2918 break; 2919 2920 default: 2921 break; 2922 } 2923 } 2924 } 2925 } 2926 2927 static void gatt_client_classic_sdp_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){ 2928 gatt_client_t * gatt_client = gatt_client_classic_active_sdp_query; 2929 btstack_assert(gatt_client != NULL); 2930 uint8_t status; 2931 2932 // TODO: handle sdp events, get l2cap psm 2933 switch (hci_event_packet_get_type(packet)){ 2934 case SDP_EVENT_QUERY_ATTRIBUTE_VALUE: 2935 gatt_client_handle_sdp_client_query_attribute_value(gatt_client, packet); 2936 // TODO: 2937 return; 2938 case SDP_EVENT_QUERY_COMPLETE: 2939 status = sdp_event_query_complete_get_status(packet); 2940 gatt_client_classic_active_sdp_query = NULL; 2941 log_info("l2cap psm: %0x, status %02x", gatt_client->l2cap_psm, status); 2942 if (status != ERROR_CODE_SUCCESS) break; 2943 if (gatt_client->l2cap_psm == 0) { 2944 status = SDP_SERVICE_NOT_FOUND; 2945 break; 2946 } 2947 break; 2948 default: 2949 btstack_assert(false); 2950 return; 2951 } 2952 2953 // done 2954 if (status == ERROR_CODE_SUCCESS){ 2955 gatt_client->gatt_client_state = P_W4_L2CAP_CONNECTION; 2956 status = l2cap_create_channel(gatt_client_l2cap_handler, gatt_client->addr, gatt_client->l2cap_psm, 0xffff, 2957 &gatt_client->l2cap_cid); 2958 } 2959 if (status != ERROR_CODE_SUCCESS) { 2960 gatt_client_classic_handle_connected(gatt_client, status); 2961 } 2962 } 2963 2964 static void gatt_client_classic_sdp_start(void * context){ 2965 gatt_client_classic_active_sdp_query = (gatt_client_t *) context; 2966 gatt_client_classic_active_sdp_query->gatt_client_state = P_W4_SDP_QUERY; 2967 sdp_client_query_uuid16(gatt_client_classic_sdp_handler, gatt_client_classic_active_sdp_query->addr, ORG_BLUETOOTH_SERVICE_GENERIC_ATTRIBUTE); 2968 } 2969 2970 uint8_t gatt_client_classic_connect(btstack_packet_handler_t callback, bd_addr_t addr){ 2971 gatt_client_t * gatt_client = gatt_client_get_context_for_classic_addr(addr); 2972 if (gatt_client != NULL){ 2973 return ERROR_CODE_ACL_CONNECTION_ALREADY_EXISTS; 2974 } 2975 gatt_client = btstack_memory_gatt_client_get(); 2976 if (gatt_client == NULL){ 2977 return ERROR_CODE_MEMORY_CAPACITY_EXCEEDED; 2978 } 2979 // init state 2980 gatt_client->bearer_type = ATT_BEARER_UNENHANCED_CLASSIC; 2981 gatt_client->con_handle = HCI_CON_HANDLE_INVALID; 2982 memcpy(gatt_client->addr, addr, 6); 2983 gatt_client->mtu = ATT_DEFAULT_MTU; 2984 gatt_client->security_level = LEVEL_0; 2985 gatt_client->mtu_state = MTU_AUTO_EXCHANGE_DISABLED; 2986 gatt_client->gatt_client_state = P_W2_SDP_QUERY; 2987 gatt_client->sdp_query_request.callback = &gatt_client_classic_sdp_start; 2988 gatt_client->sdp_query_request.context = gatt_client; 2989 gatt_client->callback = callback; 2990 #ifdef ENABLE_GATT_OVER_EATT 2991 gatt_client->eatt_state = GATT_CLIENT_EATT_IDLE; 2992 #endif 2993 btstack_linked_list_add(&gatt_client_connections, (btstack_linked_item_t*)gatt_client); 2994 sdp_client_register_query_callback(&gatt_client->sdp_query_request); 2995 return ERROR_CODE_SUCCESS; 2996 } 2997 2998 uint8_t gatt_client_classic_disconnect(btstack_packet_handler_t callback, hci_con_handle_t con_handle){ 2999 gatt_client_t * gatt_client = gatt_client_get_context_for_handle(con_handle); 3000 if (gatt_client == NULL){ 3001 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 3002 } 3003 gatt_client->callback = callback; 3004 return l2cap_disconnect(gatt_client->l2cap_cid); 3005 } 3006 #endif 3007 3008 #ifdef ENABLE_GATT_OVER_EATT 3009 3010 #define MAX_NR_EATT_CHANNELS 5 3011 3012 static void gatt_client_le_enhanced_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 3013 3014 static void gatt_client_eatt_finalize(gatt_client_t * gatt_client) { 3015 // free eatt clients 3016 btstack_linked_list_iterator_t it; 3017 btstack_linked_list_iterator_init(&it, &gatt_client_connections); 3018 while (btstack_linked_list_iterator_has_next(&it)) { 3019 gatt_client_t *eatt_client = (gatt_client_t *) btstack_linked_list_iterator_next(&it); 3020 btstack_linked_list_iterator_remove(&it); 3021 btstack_memory_gatt_client_free(eatt_client); 3022 } 3023 } 3024 3025 // all channels connected 3026 static void gatt_client_le_enhanced_handle_connected(gatt_client_t * gatt_client, uint8_t status) { 3027 if (status == ERROR_CODE_SUCCESS){ 3028 gatt_client->eatt_state = GATT_CLIENT_EATT_READY; 3029 } else { 3030 gatt_client_eatt_finalize(gatt_client); 3031 gatt_client->eatt_state = GATT_CLIENT_EATT_IDLE; 3032 } 3033 3034 uint8_t buffer[20]; 3035 uint16_t len = hci_event_create_from_template_and_arguments(buffer, sizeof(buffer), &gatt_client_connected, status, gatt_client->addr, 3036 gatt_client->con_handle); 3037 (*gatt_client->callback)(HCI_EVENT_PACKET, 0, buffer, len); 3038 } 3039 3040 // single channel disconnected 3041 static void gatt_client_le_enhanced_handle_ecbm_disconnected(gatt_client_t * gatt_client, gatt_client_t * eatt_client) { 3042 if (gatt_client->eatt_state == GATT_CLIENT_EATT_READY) { 3043 3044 // report error 3045 gatt_client_report_error_if_pending(eatt_client, ATT_ERROR_HCI_DISCONNECT_RECEIVED); 3046 3047 // free memory 3048 btstack_linked_list_remove(&gatt_client->eatt_clients, (btstack_linked_item_t *) eatt_client); 3049 btstack_memory_gatt_client_free(eatt_client); 3050 3051 // report disconnected if last channel closed 3052 if (btstack_linked_list_empty(&gatt_client->eatt_clients)){ 3053 // emit disconnected when last eatt client is gone 3054 uint8_t buffer[20]; 3055 uint16_t len = hci_event_create_from_template_and_arguments(buffer, sizeof(buffer), &gatt_client_disconnected, gatt_client->con_handle); 3056 (*gatt_client->callback)(HCI_EVENT_PACKET, 0, buffer, len); 3057 } 3058 } 3059 } 3060 3061 static gatt_client_t * gatt_client_le_enhanced_get_context_for_l2cap_cid(uint16_t l2cap_cid, gatt_client_t ** out_eatt_client){ 3062 btstack_linked_list_iterator_t it; 3063 btstack_linked_list_iterator_init(&it, &gatt_client_connections); 3064 while (btstack_linked_list_iterator_has_next(&it)) { 3065 gatt_client_t * gatt_client = (gatt_client_t *) btstack_linked_list_iterator_next(&it); 3066 btstack_linked_list_iterator_t it2; 3067 btstack_linked_list_iterator_init(&it2, &gatt_client->eatt_clients); 3068 while (btstack_linked_list_iterator_has_next(&it2)) { 3069 gatt_client_t * eatt_client = (gatt_client_t *) btstack_linked_list_iterator_next(&it2); 3070 if (eatt_client->l2cap_cid == l2cap_cid){ 3071 *out_eatt_client = eatt_client; 3072 return gatt_client; 3073 } 3074 } 3075 } 3076 return NULL; 3077 } 3078 3079 static void gatt_client_le_enhanced_setup_l2cap_channel(gatt_client_t * gatt_client){ 3080 uint8_t num_channels = gatt_client->eatt_num_clients; 3081 3082 // setup channels 3083 uint16_t buffer_size_per_client = gatt_client->eatt_storage_size / num_channels; 3084 uint16_t max_mtu = (buffer_size_per_client - REPORT_PREBUFFER_HEADER) / 2; 3085 uint8_t * receive_buffers[MAX_NR_EATT_CHANNELS]; 3086 uint16_t new_cids[MAX_NR_EATT_CHANNELS]; 3087 memset(gatt_client->eatt_storage_buffer, 0, gatt_client->eatt_storage_size); 3088 uint8_t i; 3089 for (i=0;i<gatt_client->eatt_num_clients; i++){ 3090 receive_buffers[i] = &gatt_client->eatt_storage_buffer[REPORT_PREBUFFER_HEADER]; 3091 gatt_client->eatt_storage_buffer += REPORT_PREBUFFER_HEADER + max_mtu; 3092 } 3093 3094 log_info("%u EATT clients with receive buffer size %u", gatt_client->eatt_num_clients, buffer_size_per_client); 3095 3096 uint8_t status = l2cap_ecbm_create_channels(&gatt_client_le_enhanced_packet_handler, 3097 gatt_client->con_handle, 3098 gatt_client->security_level, 3099 BLUETOOTH_PSM_EATT, num_channels, 3100 L2CAP_LE_AUTOMATIC_CREDITS, 3101 buffer_size_per_client, 3102 receive_buffers, 3103 new_cids); 3104 3105 if (status == ERROR_CODE_SUCCESS){ 3106 i = 0; 3107 btstack_linked_list_iterator_t it; 3108 btstack_linked_list_iterator_init(&it, &gatt_client->eatt_clients); 3109 while (btstack_linked_list_iterator_has_next(&it)) { 3110 gatt_client_t *new_eatt_client = (gatt_client_t *) btstack_linked_list_iterator_next(&it); 3111 3112 // init state with new cid and transmit buffer 3113 new_eatt_client->bearer_type = ATT_BEARER_ENHANCED_LE; 3114 new_eatt_client->con_handle = gatt_client->con_handle; 3115 new_eatt_client->mtu = 64; 3116 new_eatt_client->security_level = LEVEL_0; 3117 new_eatt_client->mtu_state = MTU_AUTO_EXCHANGE_DISABLED; 3118 new_eatt_client->gatt_client_state = P_W4_L2CAP_CONNECTION; 3119 new_eatt_client->l2cap_cid = new_cids[i]; 3120 new_eatt_client->eatt_storage_buffer = gatt_client->eatt_storage_buffer; 3121 gatt_client->eatt_storage_buffer += max_mtu; 3122 i++; 3123 } 3124 gatt_client->eatt_state = GATT_CLIENT_EATT_L2CAP_SETUP; 3125 } else { 3126 gatt_client_le_enhanced_handle_connected(gatt_client, status); 3127 } 3128 } 3129 3130 static void gatt_client_le_enhanced_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) { 3131 gatt_client_t *gatt_client; 3132 gatt_client_t *eatt_client; 3133 hci_con_handle_t con_handle; 3134 uint16_t l2cap_cid; 3135 uint8_t status; 3136 gatt_client_characteristic_t characteristic; 3137 gatt_client_service_t service; 3138 switch (packet_type) { 3139 case HCI_EVENT_PACKET: 3140 switch (hci_event_packet_get_type(packet)) { 3141 case GATT_EVENT_SERVICE_QUERY_RESULT: 3142 con_handle = gatt_event_service_query_result_get_handle(packet); 3143 gatt_client = gatt_client_get_context_for_handle(con_handle); 3144 btstack_assert(gatt_client != NULL); 3145 btstack_assert(gatt_client->eatt_state == GATT_CLIENT_EATT_DISCOVER_GATT_SERVICE_W4_DONE); 3146 gatt_event_service_query_result_get_service(packet, &service); 3147 gatt_client->gatt_service_start_group_handle = service.start_group_handle; 3148 gatt_client->gatt_service_end_group_handle = service.end_group_handle; 3149 break; 3150 case GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT: 3151 con_handle = gatt_event_characteristic_value_query_result_get_handle(packet); 3152 gatt_client = gatt_client_get_context_for_handle(con_handle); 3153 btstack_assert(gatt_client != NULL); 3154 btstack_assert(gatt_client->eatt_state == GATT_CLIENT_EATT_READ_SERVER_SUPPORTED_FEATURES_W4_DONE); 3155 if (gatt_event_characteristic_value_query_result_get_value_length(packet) >= 1) { 3156 gatt_client->gatt_server_supported_features = gatt_event_characteristic_value_query_result_get_value(packet)[0]; 3157 } 3158 break; 3159 case GATT_EVENT_CHARACTERISTIC_QUERY_RESULT: 3160 con_handle = gatt_event_characteristic_query_result_get_handle(packet); 3161 gatt_client = gatt_client_get_context_for_handle(con_handle); 3162 btstack_assert(gatt_client != NULL); 3163 btstack_assert(gatt_client->eatt_state == GATT_CLIENT_EATT_FIND_CLIENT_SUPPORTED_FEATURES_W4_DONE); 3164 gatt_event_characteristic_query_result_get_characteristic(packet, &characteristic); 3165 gatt_client->gatt_client_supported_features_handle = characteristic.value_handle; 3166 break; 3167 case GATT_EVENT_QUERY_COMPLETE: 3168 con_handle = gatt_event_query_complete_get_handle(packet); 3169 gatt_client = gatt_client_get_context_for_handle(con_handle); 3170 btstack_assert(gatt_client != NULL); 3171 switch (gatt_client->eatt_state){ 3172 case GATT_CLIENT_EATT_DISCOVER_GATT_SERVICE_W4_DONE: 3173 if (gatt_client->gatt_service_start_group_handle == 0){ 3174 gatt_client_le_enhanced_handle_connected(gatt_client, ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE); 3175 } else { 3176 gatt_client->eatt_state = GATT_CLIENT_EATT_READ_SERVER_SUPPORTED_FEATURES_W2_SEND; 3177 } 3178 break; 3179 case GATT_CLIENT_EATT_READ_SERVER_SUPPORTED_FEATURES_W4_DONE: 3180 if ((gatt_client->gatt_server_supported_features & 1) == 0) { 3181 gatt_client_le_enhanced_handle_connected(gatt_client, ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE); 3182 } else { 3183 gatt_client->eatt_state = GATT_CLIENT_EATT_FIND_CLIENT_SUPPORTED_FEATURES_W2_SEND; 3184 } 3185 break; 3186 case GATT_CLIENT_EATT_FIND_CLIENT_SUPPORTED_FEATURES_W4_DONE: 3187 if (gatt_client->gatt_client_supported_features_handle == 0){ 3188 gatt_client_le_enhanced_handle_connected(gatt_client, ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE); 3189 } else { 3190 gatt_client->eatt_state = GATT_CLIENT_EATT_WRITE_ClIENT_SUPPORTED_FEATURES_W2_SEND; 3191 } 3192 break; 3193 case GATT_CLIENT_EATT_WRITE_ClIENT_SUPPORTED_FEATURES_W4_DONE: 3194 gatt_client_le_enhanced_setup_l2cap_channel(gatt_client); 3195 break; 3196 default: 3197 break; 3198 } 3199 break; 3200 case L2CAP_EVENT_ECBM_CHANNEL_OPENED: 3201 l2cap_cid = l2cap_event_ecbm_channel_opened_get_local_cid(packet); 3202 gatt_client = gatt_client_le_enhanced_get_context_for_l2cap_cid(l2cap_cid, &eatt_client); 3203 3204 btstack_assert(gatt_client != NULL); 3205 btstack_assert(eatt_client != NULL); 3206 btstack_assert(eatt_client->gatt_client_state == P_W4_L2CAP_CONNECTION); 3207 3208 status = l2cap_event_channel_opened_get_status(packet); 3209 if (status == ERROR_CODE_SUCCESS){ 3210 eatt_client->gatt_client_state = P_READY; 3211 eatt_client->mtu = l2cap_event_channel_opened_get_remote_mtu(packet); 3212 } else { 3213 gatt_client_le_enhanced_handle_ecbm_disconnected(gatt_client, eatt_client); 3214 } 3215 // all channels opened? 3216 gatt_client->eatt_num_clients--; 3217 if (gatt_client->eatt_num_clients == 0){ 3218 gatt_client_le_enhanced_handle_connected(gatt_client, ERROR_CODE_SUCCESS); 3219 } 3220 break; 3221 case L2CAP_EVENT_CHANNEL_CLOSED: 3222 l2cap_cid = l2cap_event_channel_closed_get_local_cid(packet); 3223 gatt_client = gatt_client_le_enhanced_get_context_for_l2cap_cid(l2cap_cid, &eatt_client); 3224 btstack_assert(gatt_client != NULL); 3225 btstack_assert(eatt_client != NULL); 3226 gatt_client_le_enhanced_handle_ecbm_disconnected(gatt_client, eatt_client); 3227 break; 3228 default: 3229 break; 3230 } 3231 break; 3232 case L2CAP_DATA_PACKET: 3233 gatt_client = gatt_client_le_enhanced_get_context_for_l2cap_cid(channel, &eatt_client); 3234 btstack_assert(gatt_client != NULL); 3235 btstack_assert(eatt_client != NULL); 3236 gatt_client_handle_att_response(eatt_client, packet, size); 3237 gatt_client_run(); 3238 break; 3239 default: 3240 break; 3241 } 3242 } 3243 3244 static bool gatt_client_le_enhanced_handle_can_send_query(gatt_client_t * gatt_client){ 3245 uint8_t status = ERROR_CODE_SUCCESS; 3246 uint8_t gatt_client_supported_features = 0x06; // eatt + multiple value notifications 3247 switch (gatt_client->eatt_state){ 3248 case GATT_CLIENT_EATT_DISCOVER_GATT_SERVICE_W2_SEND: 3249 gatt_client->gatt_service_start_group_handle = 0; 3250 gatt_client->eatt_state = GATT_CLIENT_EATT_DISCOVER_GATT_SERVICE_W4_DONE; 3251 status = gatt_client_discover_primary_services_by_uuid16(&gatt_client_le_enhanced_packet_handler, 3252 gatt_client->con_handle, 3253 ORG_BLUETOOTH_SERVICE_GENERIC_ATTRIBUTE); 3254 break; 3255 case GATT_CLIENT_EATT_READ_SERVER_SUPPORTED_FEATURES_W2_SEND: 3256 gatt_client->gatt_server_supported_features = 0; 3257 gatt_client->eatt_state = GATT_CLIENT_EATT_READ_SERVER_SUPPORTED_FEATURES_W4_DONE; 3258 status = gatt_client_read_value_of_characteristics_by_uuid16(&gatt_client_le_enhanced_packet_handler, 3259 gatt_client->con_handle, 3260 gatt_client->gatt_service_start_group_handle, 3261 gatt_client->gatt_service_end_group_handle, 3262 ORG_BLUETOOTH_CHARACTERISTIC_SERVER_SUPPORTED_FEATURES); 3263 return true; 3264 case GATT_CLIENT_EATT_FIND_CLIENT_SUPPORTED_FEATURES_W2_SEND: 3265 gatt_client->gatt_client_supported_features_handle = 0; 3266 gatt_client->eatt_state = GATT_CLIENT_EATT_FIND_CLIENT_SUPPORTED_FEATURES_W4_DONE; 3267 status = gatt_client_discover_characteristics_for_handle_range_by_uuid16(&gatt_client_le_enhanced_packet_handler, 3268 gatt_client->con_handle, 3269 gatt_client->gatt_service_start_group_handle, 3270 gatt_client->gatt_service_end_group_handle, 3271 ORG_BLUETOOTH_CHARACTERISTIC_CLIENT_SUPPORTED_FEATURES); 3272 return true; 3273 case GATT_CLIENT_EATT_WRITE_ClIENT_SUPPORTED_FEATURES_W2_SEND: 3274 gatt_client->eatt_state = GATT_CLIENT_EATT_WRITE_ClIENT_SUPPORTED_FEATURES_W4_DONE; 3275 status = gatt_client_write_value_of_characteristic(&gatt_client_le_enhanced_packet_handler, gatt_client->con_handle, 3276 gatt_client->gatt_client_supported_features_handle, 1, 3277 &gatt_client_supported_features); 3278 return true; 3279 default: 3280 break; 3281 } 3282 btstack_assert(status == ERROR_CODE_SUCCESS); 3283 UNUSED(status); 3284 return false; 3285 } 3286 3287 uint8_t gatt_client_le_enhanced_connect(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint8_t num_channels, uint8_t * storage_buffer, uint16_t storage_size) { 3288 gatt_client_t * gatt_client; 3289 uint8_t status = gatt_client_provide_context_for_handle(con_handle, &gatt_client); 3290 if (status != ERROR_CODE_SUCCESS){ 3291 return status; 3292 } 3293 3294 if (gatt_client->eatt_state != GATT_CLIENT_EATT_IDLE){ 3295 return ERROR_CODE_COMMAND_DISALLOWED; 3296 } 3297 3298 // need one buffer for sending and one for receiving. Receiving includes pre-buffer for reports 3299 uint16_t buffer_size_per_client = storage_size / num_channels; 3300 uint16_t max_mtu = (buffer_size_per_client - REPORT_PREBUFFER_HEADER) / 2; 3301 if (max_mtu < 64) { 3302 return ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS; 3303 } 3304 3305 if ((num_channels == 0) || (num_channels > MAX_NR_EATT_CHANNELS)){ 3306 return ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS; 3307 } 3308 3309 // create max num_channel eatt clients 3310 uint8_t i; 3311 btstack_linked_list_t eatt_clients = NULL; 3312 for (i=0;i<num_channels;i++) { 3313 gatt_client_t * new_gatt_client = btstack_memory_gatt_client_get(); 3314 if (new_gatt_client == NULL) { 3315 break; 3316 } 3317 btstack_linked_list_add(&eatt_clients, (btstack_linked_item_t*)new_gatt_client); 3318 } 3319 3320 if (i != num_channels){ 3321 while (true){ 3322 gatt_client = (gatt_client_t *) btstack_linked_list_pop(&eatt_clients); 3323 if (gatt_client == NULL) { 3324 break; 3325 } 3326 btstack_memory_gatt_client_free(gatt_client); 3327 } 3328 return ERROR_CODE_MEMORY_CAPACITY_EXCEEDED; 3329 } 3330 3331 gatt_client->callback = callback; 3332 gatt_client->eatt_num_clients = num_channels; 3333 gatt_client->eatt_storage_buffer = storage_buffer; 3334 gatt_client->eatt_storage_size = storage_size; 3335 gatt_client->eatt_clients = eatt_clients; 3336 gatt_client->eatt_state = GATT_CLIENT_EATT_DISCOVER_GATT_SERVICE_W2_SEND; 3337 gatt_client_notify_can_send_query(gatt_client); 3338 3339 return ERROR_CODE_SUCCESS; 3340 } 3341 3342 #endif 3343 3344 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION 3345 void gatt_client_att_packet_handler_fuzz(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){ 3346 gatt_client_att_packet_handler(packet_type, handle, packet, size); 3347 } 3348 3349 uint8_t gatt_client_get_client(hci_con_handle_t con_handle, gatt_client_t ** out_gatt_client){ 3350 uint8_t status = gatt_client_provide_context_for_handle(con_handle, out_gatt_client); 3351 return status; 3352 } 3353 #endif 3354