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