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