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 MATTHIAS 24 * RINGWALD 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 43 #include "btstack_config.h" 44 45 #include "att_dispatch.h" 46 #include "ad_parser.h" 47 #include "ble/att_db.h" 48 #include "ble/core.h" 49 #include "ble/gatt_client.h" 50 #include "ble/le_device_db.h" 51 #include "ble/sm.h" 52 #include "btstack_debug.h" 53 #include "btstack_event.h" 54 #include "btstack_memory.h" 55 #include "btstack_run_loop.h" 56 #include "btstack_util.h" 57 #include "classic/sdp_util.h" 58 #include "hci.h" 59 #include "hci_cmd.h" 60 #include "hci_dump.h" 61 #include "l2cap.h" 62 63 static btstack_linked_list_t gatt_client_connections; 64 static btstack_linked_list_t gatt_client_value_listeners; 65 static btstack_packet_callback_registration_t hci_event_callback_registration; 66 67 #if defined(ENABLE_GATT_CLIENT_PAIRING) || defined (ENABLE_LE_SIGNED_WRITE) || defined (ENABLE_LE_PROACTIVE_AUTHENTICATION) 68 static btstack_packet_callback_registration_t sm_event_callback_registration; 69 #endif 70 71 static uint8_t mtu_exchange_enabled; 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 mtu_exchange_enabled = 1; 84 85 // regsister for HCI Events 86 hci_event_callback_registration.callback = &gatt_client_event_packet_handler; 87 hci_add_event_handler(&hci_event_callback_registration); 88 89 #if defined(ENABLE_GATT_CLIENT_PAIRING) || defined (ENABLE_LE_SIGNED_WRITE) || defined (ENABLE_LE_PROACTIVE_AUTHENTICATION) 90 // register for SM Events 91 sm_event_callback_registration.callback = &gatt_client_event_packet_handler; 92 sm_add_event_handler(&sm_event_callback_registration); 93 #endif 94 95 // and ATT Client PDUs 96 att_dispatch_register_client(gatt_client_att_packet_handler); 97 } 98 99 static gatt_client_t * gatt_client_for_timer(btstack_timer_source_t * ts){ 100 btstack_linked_list_iterator_t it; 101 btstack_linked_list_iterator_init(&it, &gatt_client_connections); 102 while (btstack_linked_list_iterator_has_next(&it)){ 103 gatt_client_t * gatt_client = (gatt_client_t *) btstack_linked_list_iterator_next(&it); 104 if (&gatt_client->gc_timeout == ts) { 105 return gatt_client; 106 } 107 } 108 return NULL; 109 } 110 111 static void gatt_client_timeout_handler(btstack_timer_source_t * timer){ 112 gatt_client_t * gatt_client = gatt_client_for_timer(timer); 113 if (gatt_client == NULL) return; 114 log_info("GATT client timeout handle, handle 0x%02x", gatt_client->con_handle); 115 gatt_client_report_error_if_pending(gatt_client, ATT_ERROR_TIMEOUT); 116 } 117 118 static void gatt_client_timeout_start(gatt_client_t * gatt_client){ 119 log_info("GATT client timeout start, handle 0x%02x", gatt_client->con_handle); 120 btstack_run_loop_remove_timer(&gatt_client->gc_timeout); 121 btstack_run_loop_set_timer_handler(&gatt_client->gc_timeout, gatt_client_timeout_handler); 122 btstack_run_loop_set_timer(&gatt_client->gc_timeout, 30000); // 30 seconds sm timeout 123 btstack_run_loop_add_timer(&gatt_client->gc_timeout); 124 } 125 126 static void gatt_client_timeout_stop(gatt_client_t * gatt_client){ 127 log_info("GATT client timeout stop, handle 0x%02x", gatt_client->con_handle); 128 btstack_run_loop_remove_timer(&gatt_client->gc_timeout); 129 } 130 131 static gatt_client_t * gatt_client_get_context_for_handle(uint16_t handle){ 132 btstack_linked_item_t *it; 133 for (it = (btstack_linked_item_t *) gatt_client_connections; it != NULL; it = it->next){ 134 gatt_client_t * gatt_client = (gatt_client_t *) it; 135 if (gatt_client->con_handle == handle){ 136 return gatt_client; 137 } 138 } 139 return NULL; 140 } 141 142 143 // @returns gatt_client context 144 // returns existing one, or tries to setup new one 145 static gatt_client_t * gatt_client_provide_context_for_handle(hci_con_handle_t con_handle){ 146 gatt_client_t * gatt_client = gatt_client_get_context_for_handle(con_handle); 147 if (gatt_client) return gatt_client; 148 149 // bail if no such hci connection 150 if (!hci_connection_for_handle(con_handle)){ 151 log_error("No connection for handle 0x%04x", con_handle); 152 return NULL; 153 } 154 gatt_client = btstack_memory_gatt_client_get(); 155 if (!gatt_client) return NULL; 156 // init state 157 gatt_client->con_handle = con_handle; 158 gatt_client->mtu = ATT_DEFAULT_MTU; 159 if (mtu_exchange_enabled){ 160 gatt_client->mtu_state = SEND_MTU_EXCHANGE; 161 } else { 162 gatt_client->mtu_state = MTU_AUTO_EXCHANGE_DISABLED; 163 } 164 gatt_client->gatt_client_state = P_READY; 165 btstack_linked_list_add(&gatt_client_connections, (btstack_linked_item_t*)gatt_client); 166 return gatt_client; 167 } 168 169 static gatt_client_t * gatt_client_provide_context_for_handle_and_start_timer(hci_con_handle_t con_handle){ 170 gatt_client_t * gatt_client = gatt_client_provide_context_for_handle(con_handle); 171 if (gatt_client == NULL) return NULL; 172 gatt_client_timeout_start(gatt_client); 173 return gatt_client; 174 } 175 176 static int is_ready(gatt_client_t * gatt_client){ 177 return gatt_client->gatt_client_state == P_READY; 178 } 179 180 int gatt_client_is_ready(hci_con_handle_t con_handle){ 181 gatt_client_t * gatt_client = gatt_client_provide_context_for_handle(con_handle); 182 if (gatt_client == NULL) return 0; 183 return is_ready(gatt_client); 184 } 185 186 void gatt_client_mtu_enable_auto_negotiation(uint8_t enabled){ 187 mtu_exchange_enabled = enabled; 188 } 189 190 uint8_t gatt_client_get_mtu(hci_con_handle_t con_handle, uint16_t * mtu){ 191 gatt_client_t * gatt_client = gatt_client_provide_context_for_handle(con_handle); 192 if (gatt_client == NULL) return BTSTACK_MEMORY_ALLOC_FAILED; 193 194 if ((gatt_client->mtu_state == MTU_EXCHANGED) || (gatt_client->mtu_state == MTU_AUTO_EXCHANGE_DISABLED)){ 195 *mtu = gatt_client->mtu; 196 return ERROR_CODE_SUCCESS; 197 } 198 *mtu = ATT_DEFAULT_MTU; 199 return GATT_CLIENT_IN_WRONG_STATE; 200 } 201 202 // precondition: can_send_packet_now == TRUE 203 static uint8_t att_confirmation(uint16_t con_handle){ 204 l2cap_reserve_packet_buffer(); 205 uint8_t * request = l2cap_get_outgoing_buffer(); 206 request[0] = ATT_HANDLE_VALUE_CONFIRMATION; 207 208 return l2cap_send_prepared_connectionless(con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, 1); 209 } 210 211 // precondition: can_send_packet_now == TRUE 212 static uint8_t att_find_information_request(uint16_t request_type, uint16_t con_handle, uint16_t start_handle, uint16_t end_handle){ 213 l2cap_reserve_packet_buffer(); 214 uint8_t * request = l2cap_get_outgoing_buffer(); 215 request[0] = request_type; 216 little_endian_store_16(request, 1, start_handle); 217 little_endian_store_16(request, 3, end_handle); 218 219 return l2cap_send_prepared_connectionless(con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, 5); 220 } 221 222 // precondition: can_send_packet_now == TRUE 223 static uint8_t att_find_by_type_value_request(uint16_t request_type, uint16_t attribute_group_type, uint16_t con_handle, uint16_t start_handle, uint16_t end_handle, uint8_t * value, uint16_t value_size){ 224 l2cap_reserve_packet_buffer(); 225 uint8_t * request = l2cap_get_outgoing_buffer(); 226 227 request[0] = request_type; 228 little_endian_store_16(request, 1, start_handle); 229 little_endian_store_16(request, 3, end_handle); 230 little_endian_store_16(request, 5, attribute_group_type); 231 (void)memcpy(&request[7], value, value_size); 232 233 return l2cap_send_prepared_connectionless(con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, 7u + value_size); 234 } 235 236 // precondition: can_send_packet_now == TRUE 237 static uint8_t att_read_by_type_or_group_request_for_uuid16(uint16_t request_type, uint16_t uuid16, uint16_t con_handle, uint16_t start_handle, uint16_t end_handle){ 238 l2cap_reserve_packet_buffer(); 239 uint8_t * request = l2cap_get_outgoing_buffer(); 240 request[0] = request_type; 241 little_endian_store_16(request, 1, start_handle); 242 little_endian_store_16(request, 3, end_handle); 243 little_endian_store_16(request, 5, uuid16); 244 245 return l2cap_send_prepared_connectionless(con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, 7); 246 } 247 248 // precondition: can_send_packet_now == TRUE 249 static uint8_t att_read_by_type_or_group_request_for_uuid128(uint16_t request_type, uint8_t * uuid128, uint16_t con_handle, uint16_t start_handle, uint16_t end_handle){ 250 l2cap_reserve_packet_buffer(); 251 uint8_t * request = l2cap_get_outgoing_buffer(); 252 request[0] = request_type; 253 little_endian_store_16(request, 1, start_handle); 254 little_endian_store_16(request, 3, end_handle); 255 reverse_128(uuid128, &request[5]); 256 257 return l2cap_send_prepared_connectionless(con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, 21); 258 } 259 260 // precondition: can_send_packet_now == TRUE 261 static uint8_t att_read_request(uint16_t request_type, uint16_t con_handle, uint16_t attribute_handle){ 262 l2cap_reserve_packet_buffer(); 263 uint8_t * request = l2cap_get_outgoing_buffer(); 264 request[0] = request_type; 265 little_endian_store_16(request, 1, attribute_handle); 266 267 return l2cap_send_prepared_connectionless(con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, 3); 268 } 269 270 // precondition: can_send_packet_now == TRUE 271 static uint8_t att_read_blob_request(uint16_t request_type, uint16_t con_handle, uint16_t attribute_handle, uint16_t value_offset){ 272 l2cap_reserve_packet_buffer(); 273 uint8_t * request = l2cap_get_outgoing_buffer(); 274 request[0] = request_type; 275 little_endian_store_16(request, 1, attribute_handle); 276 little_endian_store_16(request, 3, value_offset); 277 278 return l2cap_send_prepared_connectionless(con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, 5); 279 } 280 281 static uint8_t att_read_multiple_request(uint16_t con_handle, uint16_t num_value_handles, uint16_t * value_handles){ 282 l2cap_reserve_packet_buffer(); 283 uint8_t * request = l2cap_get_outgoing_buffer(); 284 request[0] = ATT_READ_MULTIPLE_REQUEST; 285 int i; 286 int offset = 1; 287 for (i=0;i<num_value_handles;i++){ 288 little_endian_store_16(request, offset, value_handles[i]); 289 offset += 2; 290 } 291 292 return l2cap_send_prepared_connectionless(con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, offset); 293 } 294 295 #ifdef ENABLE_LE_SIGNED_WRITE 296 // precondition: can_send_packet_now == TRUE 297 static uint8_t att_signed_write_request(uint16_t request_type, uint16_t con_handle, uint16_t attribute_handle, uint16_t value_length, uint8_t * value, uint32_t sign_counter, uint8_t sgn[8]){ 298 l2cap_reserve_packet_buffer(); 299 uint8_t * request = l2cap_get_outgoing_buffer(); 300 request[0] = request_type; 301 little_endian_store_16(request, 1, attribute_handle); 302 (void)memcpy(&request[3], value, value_length); 303 little_endian_store_32(request, 3 + value_length, sign_counter); 304 reverse_64(sgn, &request[3 + value_length + 4]); 305 306 return l2cap_send_prepared_connectionless(con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, 3 + value_length + 12); 307 } 308 #endif 309 310 // precondition: can_send_packet_now == TRUE 311 static uint8_t att_write_request(uint16_t request_type, uint16_t con_handle, uint16_t attribute_handle, uint16_t value_length, uint8_t * value){ 312 l2cap_reserve_packet_buffer(); 313 uint8_t * request = l2cap_get_outgoing_buffer(); 314 request[0] = request_type; 315 little_endian_store_16(request, 1, attribute_handle); 316 (void)memcpy(&request[3], value, value_length); 317 318 return l2cap_send_prepared_connectionless(con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, 3u + value_length); 319 } 320 321 // precondition: can_send_packet_now == TRUE 322 static uint8_t att_execute_write_request(uint16_t request_type, uint16_t con_handle, uint8_t execute_write){ 323 l2cap_reserve_packet_buffer(); 324 uint8_t * request = l2cap_get_outgoing_buffer(); 325 request[0] = request_type; 326 request[1] = execute_write; 327 328 return l2cap_send_prepared_connectionless(con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, 2); 329 } 330 331 // precondition: can_send_packet_now == TRUE 332 static uint8_t att_prepare_write_request(uint16_t request_type, uint16_t con_handle, uint16_t attribute_handle, uint16_t value_offset, uint16_t blob_length, uint8_t * value){ 333 l2cap_reserve_packet_buffer(); 334 uint8_t * request = l2cap_get_outgoing_buffer(); 335 request[0] = request_type; 336 little_endian_store_16(request, 1, attribute_handle); 337 little_endian_store_16(request, 3, value_offset); 338 (void)memcpy(&request[5], &value[value_offset], blob_length); 339 340 return l2cap_send_prepared_connectionless(con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, 5u + blob_length); 341 } 342 343 static uint8_t att_exchange_mtu_request(uint16_t con_handle){ 344 uint16_t mtu = l2cap_max_le_mtu(); 345 l2cap_reserve_packet_buffer(); 346 uint8_t * request = l2cap_get_outgoing_buffer(); 347 request[0] = ATT_EXCHANGE_MTU_REQUEST; 348 little_endian_store_16(request, 1, mtu); 349 350 return l2cap_send_prepared_connectionless(con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, 3); 351 } 352 353 static uint16_t write_blob_length(gatt_client_t * gatt_client){ 354 uint16_t max_blob_length = gatt_client->mtu - 5u; 355 if (gatt_client->attribute_offset >= gatt_client->attribute_length) { 356 return 0; 357 } 358 uint16_t rest_length = gatt_client->attribute_length - gatt_client->attribute_offset; 359 if (max_blob_length > rest_length){ 360 return rest_length; 361 } 362 return max_blob_length; 363 } 364 365 static void send_gatt_services_request(gatt_client_t *gatt_client){ 366 att_read_by_type_or_group_request_for_uuid16(ATT_READ_BY_GROUP_TYPE_REQUEST, GATT_PRIMARY_SERVICE_UUID, gatt_client->con_handle, gatt_client->start_group_handle, gatt_client->end_group_handle); 367 } 368 369 static void send_gatt_by_uuid_request(gatt_client_t *gatt_client, uint16_t attribute_group_type){ 370 if (gatt_client->uuid16){ 371 uint8_t uuid16[2]; 372 little_endian_store_16(uuid16, 0, gatt_client->uuid16); 373 att_find_by_type_value_request(ATT_FIND_BY_TYPE_VALUE_REQUEST, attribute_group_type, gatt_client->con_handle, gatt_client->start_group_handle, gatt_client->end_group_handle, uuid16, 2); 374 return; 375 } 376 uint8_t uuid128[16]; 377 reverse_128(gatt_client->uuid128, uuid128); 378 att_find_by_type_value_request(ATT_FIND_BY_TYPE_VALUE_REQUEST, attribute_group_type, gatt_client->con_handle, gatt_client->start_group_handle, gatt_client->end_group_handle, uuid128, 16); 379 } 380 381 static void send_gatt_services_by_uuid_request(gatt_client_t *gatt_client){ 382 send_gatt_by_uuid_request(gatt_client, GATT_PRIMARY_SERVICE_UUID); 383 } 384 385 static void send_gatt_included_service_uuid_request(gatt_client_t *gatt_client){ 386 att_read_request(ATT_READ_REQUEST, gatt_client->con_handle, gatt_client->query_start_handle); 387 } 388 389 static void send_gatt_included_service_request(gatt_client_t *gatt_client){ 390 att_read_by_type_or_group_request_for_uuid16(ATT_READ_BY_TYPE_REQUEST, GATT_INCLUDE_SERVICE_UUID, gatt_client->con_handle, gatt_client->start_group_handle, gatt_client->end_group_handle); 391 } 392 393 static void send_gatt_characteristic_request(gatt_client_t *gatt_client){ 394 att_read_by_type_or_group_request_for_uuid16(ATT_READ_BY_TYPE_REQUEST, GATT_CHARACTERISTICS_UUID, gatt_client->con_handle, gatt_client->start_group_handle, gatt_client->end_group_handle); 395 } 396 397 static void send_gatt_characteristic_descriptor_request(gatt_client_t *gatt_client){ 398 att_find_information_request(ATT_FIND_INFORMATION_REQUEST, gatt_client->con_handle, gatt_client->start_group_handle, gatt_client->end_group_handle); 399 } 400 401 static void send_gatt_read_characteristic_value_request(gatt_client_t *gatt_client){ 402 att_read_request(ATT_READ_REQUEST, gatt_client->con_handle, gatt_client->attribute_handle); 403 } 404 405 static void send_gatt_read_by_type_request(gatt_client_t * gatt_client){ 406 if (gatt_client->uuid16){ 407 att_read_by_type_or_group_request_for_uuid16(ATT_READ_BY_TYPE_REQUEST, gatt_client->uuid16, gatt_client->con_handle, gatt_client->start_group_handle, gatt_client->end_group_handle); 408 } else { 409 att_read_by_type_or_group_request_for_uuid128(ATT_READ_BY_TYPE_REQUEST, gatt_client->uuid128, gatt_client->con_handle, gatt_client->start_group_handle, gatt_client->end_group_handle); 410 } 411 } 412 413 static void send_gatt_read_blob_request(gatt_client_t *gatt_client){ 414 att_read_blob_request(ATT_READ_BLOB_REQUEST, gatt_client->con_handle, gatt_client->attribute_handle, gatt_client->attribute_offset); 415 } 416 417 static void send_gatt_read_multiple_request(gatt_client_t * gatt_client){ 418 att_read_multiple_request(gatt_client->con_handle, gatt_client->read_multiple_handle_count, gatt_client->read_multiple_handles); 419 } 420 421 static void send_gatt_write_attribute_value_request(gatt_client_t * gatt_client){ 422 att_write_request(ATT_WRITE_REQUEST, gatt_client->con_handle, gatt_client->attribute_handle, gatt_client->attribute_length, gatt_client->attribute_value); 423 } 424 425 static void send_gatt_write_client_characteristic_configuration_request(gatt_client_t * gatt_client){ 426 att_write_request(ATT_WRITE_REQUEST, gatt_client->con_handle, gatt_client->client_characteristic_configuration_handle, 2, gatt_client->client_characteristic_configuration_value); 427 } 428 429 static void send_gatt_prepare_write_request(gatt_client_t * gatt_client){ 430 att_prepare_write_request(ATT_PREPARE_WRITE_REQUEST, gatt_client->con_handle, gatt_client->attribute_handle, gatt_client->attribute_offset, write_blob_length(gatt_client), gatt_client->attribute_value); 431 } 432 433 static void send_gatt_execute_write_request(gatt_client_t * gatt_client){ 434 att_execute_write_request(ATT_EXECUTE_WRITE_REQUEST, gatt_client->con_handle, 1); 435 } 436 437 static void send_gatt_cancel_prepared_write_request(gatt_client_t * gatt_client){ 438 att_execute_write_request(ATT_EXECUTE_WRITE_REQUEST, gatt_client->con_handle, 0); 439 } 440 441 #ifndef ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERY 442 static void send_gatt_read_client_characteristic_configuration_request(gatt_client_t * gatt_client){ 443 att_read_by_type_or_group_request_for_uuid16(ATT_READ_BY_TYPE_REQUEST, GATT_CLIENT_CHARACTERISTICS_CONFIGURATION, gatt_client->con_handle, gatt_client->start_group_handle, gatt_client->end_group_handle); 444 } 445 #endif 446 447 static void send_gatt_read_characteristic_descriptor_request(gatt_client_t * gatt_client){ 448 att_read_request(ATT_READ_REQUEST, gatt_client->con_handle, gatt_client->attribute_handle); 449 } 450 451 #ifdef ENABLE_LE_SIGNED_WRITE 452 static void send_gatt_signed_write_request(gatt_client_t * gatt_client, uint32_t sign_counter){ 453 att_signed_write_request(ATT_SIGNED_WRITE_COMMAND, gatt_client->con_handle, gatt_client->attribute_handle, gatt_client->attribute_length, gatt_client->attribute_value, sign_counter, gatt_client->cmac); 454 } 455 #endif 456 457 static uint16_t get_last_result_handle_from_service_list(uint8_t * packet, uint16_t size){ 458 uint8_t attr_length = packet[1]; 459 return little_endian_read_16(packet, size - attr_length + 2u); 460 } 461 462 static uint16_t get_last_result_handle_from_characteristics_list(uint8_t * packet, uint16_t size){ 463 uint8_t attr_length = packet[1]; 464 return little_endian_read_16(packet, size - attr_length + 3u); 465 } 466 467 static uint16_t get_last_result_handle_from_included_services_list(uint8_t * packet, uint16_t size){ 468 uint8_t attr_length = packet[1]; 469 return little_endian_read_16(packet, size - attr_length); 470 } 471 472 static void gatt_client_handle_transaction_complete(gatt_client_t * gatt_client){ 473 gatt_client->gatt_client_state = P_READY; 474 gatt_client_timeout_stop(gatt_client); 475 } 476 477 static void emit_event_new(btstack_packet_handler_t callback, uint8_t * packet, uint16_t size){ 478 if (!callback) return; 479 hci_dump_packet(HCI_EVENT_PACKET, 0, packet, size); 480 (*callback)(HCI_EVENT_PACKET, 0, packet, size); 481 } 482 483 void gatt_client_listen_for_characteristic_value_updates(gatt_client_notification_t * notification, btstack_packet_handler_t packet_handler, hci_con_handle_t con_handle, gatt_client_characteristic_t * characteristic){ 484 notification->callback = packet_handler; 485 notification->con_handle = con_handle; 486 if (characteristic == NULL){ 487 notification->attribute_handle = GATT_CLIENT_ANY_VALUE_HANDLE; 488 } else { 489 notification->attribute_handle = characteristic->value_handle; 490 } 491 btstack_linked_list_add(&gatt_client_value_listeners, (btstack_linked_item_t*) notification); 492 } 493 494 void gatt_client_stop_listening_for_characteristic_value_updates(gatt_client_notification_t * notification){ 495 btstack_linked_list_remove(&gatt_client_value_listeners, (btstack_linked_item_t*) notification); 496 } 497 498 static void emit_event_to_registered_listeners(hci_con_handle_t con_handle, uint16_t attribute_handle, uint8_t * packet, uint16_t size){ 499 btstack_linked_list_iterator_t it; 500 btstack_linked_list_iterator_init(&it, &gatt_client_value_listeners); 501 while (btstack_linked_list_iterator_has_next(&it)){ 502 gatt_client_notification_t * notification = (gatt_client_notification_t*) btstack_linked_list_iterator_next(&it); 503 if ((notification->con_handle != GATT_CLIENT_ANY_CONNECTION) && (notification->con_handle != con_handle)) continue; 504 if ((notification->attribute_handle != GATT_CLIENT_ANY_VALUE_HANDLE) && (notification->attribute_handle != attribute_handle)) continue; 505 (*notification->callback)(HCI_EVENT_PACKET, 0, packet, size); 506 } 507 } 508 509 static void emit_gatt_complete_event(gatt_client_t * gatt_client, uint8_t att_status){ 510 // @format H1 511 uint8_t packet[5]; 512 packet[0] = GATT_EVENT_QUERY_COMPLETE; 513 packet[1] = 3; 514 little_endian_store_16(packet, 2, gatt_client->con_handle); 515 packet[4] = att_status; 516 emit_event_new(gatt_client->callback, packet, sizeof(packet)); 517 } 518 519 static void emit_gatt_service_query_result_event(gatt_client_t * gatt_client, uint16_t start_group_handle, uint16_t end_group_handle, uint8_t * uuid128){ 520 // @format HX 521 uint8_t packet[24]; 522 packet[0] = GATT_EVENT_SERVICE_QUERY_RESULT; 523 packet[1] = sizeof(packet) - 2u; 524 little_endian_store_16(packet, 2, gatt_client->con_handle); 525 /// 526 little_endian_store_16(packet, 4, start_group_handle); 527 little_endian_store_16(packet, 6, end_group_handle); 528 reverse_128(uuid128, &packet[8]); 529 emit_event_new(gatt_client->callback, packet, sizeof(packet)); 530 } 531 532 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, uint8_t * uuid128){ 533 // @format HX 534 uint8_t packet[26]; 535 packet[0] = GATT_EVENT_INCLUDED_SERVICE_QUERY_RESULT; 536 packet[1] = sizeof(packet) - 2u; 537 little_endian_store_16(packet, 2, gatt_client->con_handle); 538 /// 539 little_endian_store_16(packet, 4, include_handle); 540 // 541 little_endian_store_16(packet, 6, start_group_handle); 542 little_endian_store_16(packet, 8, end_group_handle); 543 reverse_128(uuid128, &packet[10]); 544 emit_event_new(gatt_client->callback, packet, sizeof(packet)); 545 } 546 547 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, 548 uint16_t properties, uint8_t * uuid128){ 549 // @format HY 550 uint8_t packet[28]; 551 packet[0] = GATT_EVENT_CHARACTERISTIC_QUERY_RESULT; 552 packet[1] = sizeof(packet) - 2u; 553 little_endian_store_16(packet, 2, gatt_client->con_handle); 554 /// 555 little_endian_store_16(packet, 4, start_handle); 556 little_endian_store_16(packet, 6, value_handle); 557 little_endian_store_16(packet, 8, end_handle); 558 little_endian_store_16(packet, 10, properties); 559 reverse_128(uuid128, &packet[12]); 560 emit_event_new(gatt_client->callback, packet, sizeof(packet)); 561 } 562 563 static void emit_gatt_all_characteristic_descriptors_result_event( 564 gatt_client_t * gatt_client, uint16_t descriptor_handle, uint8_t * uuid128){ 565 // @format HZ 566 uint8_t packet[22]; 567 packet[0] = GATT_EVENT_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY_RESULT; 568 packet[1] = sizeof(packet) - 2u; 569 little_endian_store_16(packet, 2, gatt_client->con_handle); 570 /// 571 little_endian_store_16(packet, 4, descriptor_handle); 572 reverse_128(uuid128, &packet[6]); 573 emit_event_new(gatt_client->callback, packet, sizeof(packet)); 574 } 575 576 static void emit_gatt_mtu_exchanged_result_event(gatt_client_t * gatt_client, uint16_t new_mtu){ 577 // @format H2 578 uint8_t packet[6]; 579 packet[0] = GATT_EVENT_MTU; 580 packet[1] = sizeof(packet) - 2u; 581 little_endian_store_16(packet, 2, gatt_client->con_handle); 582 little_endian_store_16(packet, 4, new_mtu); 583 att_dispatch_client_mtu_exchanged(gatt_client->con_handle, new_mtu); 584 emit_event_new(gatt_client->callback, packet, sizeof(packet)); 585 } 586 /// 587 static void report_gatt_services(gatt_client_t * gatt_client, uint8_t * packet, uint16_t size){ 588 uint8_t attr_length = packet[1]; 589 uint8_t uuid_length = attr_length - 4u; 590 591 int i; 592 for (i = 2; i < size; i += attr_length){ 593 uint16_t start_group_handle = little_endian_read_16(packet,i); 594 uint16_t end_group_handle = little_endian_read_16(packet,i+2); 595 uint8_t uuid128[16]; 596 uint16_t uuid16 = 0; 597 598 if (uuid_length == 2u){ 599 uuid16 = little_endian_read_16(packet, i+4); 600 uuid_add_bluetooth_prefix((uint8_t*) &uuid128, uuid16); 601 } else { 602 reverse_128(&packet[i+4], uuid128); 603 } 604 emit_gatt_service_query_result_event(gatt_client, start_group_handle, end_group_handle, uuid128); 605 } 606 } 607 608 // helper 609 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){ 610 uint8_t uuid128[16]; 611 uint16_t uuid16 = 0; 612 if (uuid_length == 2u){ 613 uuid16 = little_endian_read_16(uuid, 0); 614 uuid_add_bluetooth_prefix((uint8_t*) uuid128, uuid16); 615 } else if (uuid_length == 16u){ 616 reverse_128(uuid, uuid128); 617 } else { 618 return; 619 } 620 621 if (gatt_client->filter_with_uuid && (memcmp(gatt_client->uuid128, uuid128, 16) != 0)) return; 622 623 gatt_client->characteristic_properties = properties; 624 gatt_client->characteristic_start_handle = start_handle; 625 gatt_client->attribute_handle = value_handle; 626 627 if (gatt_client->filter_with_uuid) return; 628 629 gatt_client->uuid16 = uuid16; 630 (void)memcpy(gatt_client->uuid128, uuid128, 16); 631 } 632 633 static void characteristic_end_found(gatt_client_t * gatt_client, uint16_t end_handle){ 634 // TODO: stop searching if filter and uuid found 635 636 if (!gatt_client->characteristic_start_handle) return; 637 638 emit_gatt_characteristic_query_result_event(gatt_client, gatt_client->characteristic_start_handle, gatt_client->attribute_handle, 639 end_handle, gatt_client->characteristic_properties, gatt_client->uuid128); 640 641 gatt_client->characteristic_start_handle = 0; 642 } 643 644 static void report_gatt_characteristics(gatt_client_t * gatt_client, uint8_t * packet, uint16_t size){ 645 if (size < 2u) return; 646 uint8_t attr_length = packet[1]; 647 if ((attr_length != 7u) && (attr_length != 21u)) return; 648 uint8_t uuid_length = attr_length - 5u; 649 int i; 650 for (i = 2u; (i + attr_length) <= size; i += attr_length){ 651 uint16_t start_handle = little_endian_read_16(packet, i); 652 uint8_t properties = packet[i+2]; 653 uint16_t value_handle = little_endian_read_16(packet, i+3); 654 characteristic_end_found(gatt_client, start_handle - 1u); 655 characteristic_start_found(gatt_client, start_handle, properties, value_handle, &packet[i + 5], uuid_length); 656 } 657 } 658 659 static void report_gatt_included_service_uuid16(gatt_client_t * gatt_client, uint16_t include_handle, uint16_t uuid16){ 660 uint8_t normalized_uuid128[16]; 661 uuid_add_bluetooth_prefix(normalized_uuid128, uuid16); 662 emit_gatt_included_service_query_result_event(gatt_client, include_handle, gatt_client->query_start_handle, 663 gatt_client->query_end_handle, normalized_uuid128); 664 } 665 666 static void report_gatt_included_service_uuid128(gatt_client_t * gatt_client, uint16_t include_handle, uint8_t *uuid128){ 667 emit_gatt_included_service_query_result_event(gatt_client, include_handle, gatt_client->query_start_handle, 668 gatt_client->query_end_handle, uuid128); 669 } 670 671 // @returns packet pointer 672 // @note assume that value is part of an l2cap buffer - overwrite HCI + L2CAP packet headers 673 static const int characteristic_value_event_header_size = 8; 674 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){ 675 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION 676 // avoid using pre ATT headers. 677 return NULL; 678 #endif 679 // before the value inside the ATT PDU 680 uint8_t * packet = value - characteristic_value_event_header_size; 681 packet[0] = type; 682 packet[1] = characteristic_value_event_header_size - 2 + length; 683 little_endian_store_16(packet, 2, con_handle); 684 little_endian_store_16(packet, 4, attribute_handle); 685 little_endian_store_16(packet, 6, length); 686 return packet; 687 } 688 689 // @returns packet pointer 690 // @note assume that value is part of an l2cap buffer - overwrite parts of the HCI/L2CAP/ATT packet (4/4/3) bytes 691 static const int long_characteristic_value_event_header_size = 10; 692 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){ 693 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION 694 // avoid using pre ATT headers. 695 return NULL; 696 #endif 697 #if defined(HCI_INCOMING_PRE_BUFFER_SIZE) && (HCI_INCOMING_PRE_BUFFER_SIZE >= 10 - 8) // L2CAP Header (4) - ACL Header (4) 698 // before the value inside the ATT PDU 699 uint8_t * packet = value - long_characteristic_value_event_header_size; 700 packet[0] = type; 701 packet[1] = long_characteristic_value_event_header_size - 2 + length; 702 little_endian_store_16(packet, 2, con_handle); 703 little_endian_store_16(packet, 4, attribute_handle); 704 little_endian_store_16(packet, 6, offset); 705 little_endian_store_16(packet, 8, length); 706 return packet; 707 #else 708 log_error("HCI_INCOMING_PRE_BUFFER_SIZE >= 2 required for long characteristic reads"); 709 return NULL; 710 #endif 711 } 712 713 // test if notification/indication should be delivered to application (BLESA) 714 static bool gatt_client_accept_server_message(hci_con_handle_t con_handle){ 715 #ifdef ENABLE_LE_PROACTIVE_AUTHENTICATION 716 // ignore messages until re-encryption is complete 717 if (gap_reconnect_security_setup_active(con_handle)) return false; 718 719 // after that ignore if bonded but not encrypted 720 return !gap_bonded(con_handle) || (gap_encryption_key_size(con_handle) > 0); 721 #else 722 return true; 723 #endif 724 } 725 726 727 // @note assume that value is part of an l2cap buffer - overwrite parts of the HCI/L2CAP/ATT packet (4/4/3) bytes 728 static void report_gatt_notification(hci_con_handle_t con_handle, uint16_t value_handle, uint8_t * value, int length){ 729 if (!gatt_client_accept_server_message(con_handle)) return; 730 uint8_t * packet = setup_characteristic_value_packet(GATT_EVENT_NOTIFICATION, con_handle, value_handle, value, length); 731 emit_event_to_registered_listeners(con_handle, value_handle, packet, characteristic_value_event_header_size + length); 732 } 733 734 // @note assume that value is part of an l2cap buffer - overwrite parts of the HCI/L2CAP/ATT packet (4/4/3) bytes 735 static void report_gatt_indication(hci_con_handle_t con_handle, uint16_t value_handle, uint8_t * value, int length){ 736 if (!gatt_client_accept_server_message(con_handle)) return; 737 uint8_t * packet = setup_characteristic_value_packet(GATT_EVENT_INDICATION, con_handle, value_handle, value, length); 738 emit_event_to_registered_listeners(con_handle, value_handle, packet, characteristic_value_event_header_size + length); 739 } 740 741 // @note assume that value is part of an l2cap buffer - overwrite parts of the HCI/L2CAP/ATT packet (4/4/3) bytes 742 static void report_gatt_characteristic_value(gatt_client_t * gatt_client, uint16_t attribute_handle, uint8_t * value, uint16_t length){ 743 uint8_t * packet = setup_characteristic_value_packet(GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT, gatt_client->con_handle, attribute_handle, value, length); 744 emit_event_new(gatt_client->callback, packet, characteristic_value_event_header_size + length); 745 } 746 747 // @note assume that value is part of an l2cap buffer - overwrite parts of the HCI/L2CAP/ATT packet (4/4/3) bytes 748 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){ 749 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); 750 if (!packet) return; 751 emit_event_new(gatt_client->callback, packet, blob_length + long_characteristic_value_event_header_size); 752 } 753 754 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){ 755 UNUSED(value_offset); 756 uint8_t * packet = setup_characteristic_value_packet(GATT_EVENT_CHARACTERISTIC_DESCRIPTOR_QUERY_RESULT, gatt_client->con_handle, descriptor_handle, value, value_length); 757 emit_event_new(gatt_client->callback, packet, value_length + 8u); 758 } 759 760 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){ 761 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); 762 if (!packet) return; 763 emit_event_new(gatt_client->callback, packet, blob_length + long_characteristic_value_event_header_size); 764 } 765 766 static void report_gatt_all_characteristic_descriptors(gatt_client_t * gatt_client, uint8_t * packet, uint16_t size, uint16_t pair_size){ 767 int i; 768 for (i = 0u; (i + pair_size) <= size; i += pair_size){ 769 uint16_t descriptor_handle = little_endian_read_16(packet,i); 770 uint8_t uuid128[16]; 771 uint16_t uuid16 = 0; 772 if (pair_size == 4u){ 773 uuid16 = little_endian_read_16(packet,i+2); 774 uuid_add_bluetooth_prefix(uuid128, uuid16); 775 } else { 776 reverse_128(&packet[i+2], uuid128); 777 } 778 emit_gatt_all_characteristic_descriptors_result_event(gatt_client, descriptor_handle, uuid128); 779 } 780 781 } 782 783 static int is_query_done(gatt_client_t * gatt_client, uint16_t last_result_handle){ 784 return last_result_handle >= gatt_client->end_group_handle; 785 } 786 787 static void trigger_next_query(gatt_client_t * gatt_client, uint16_t last_result_handle, gatt_client_state_t next_query_state){ 788 if (is_query_done(gatt_client, last_result_handle)){ 789 gatt_client_handle_transaction_complete(gatt_client); 790 emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS); 791 return; 792 } 793 // next 794 gatt_client->start_group_handle = last_result_handle + 1u; 795 gatt_client->gatt_client_state = next_query_state; 796 } 797 798 static void trigger_next_included_service_query(gatt_client_t * gatt_client, uint16_t last_result_handle){ 799 trigger_next_query(gatt_client, last_result_handle, P_W2_SEND_INCLUDED_SERVICE_QUERY); 800 } 801 802 static void trigger_next_service_query(gatt_client_t * gatt_client, uint16_t last_result_handle){ 803 trigger_next_query(gatt_client, last_result_handle, P_W2_SEND_SERVICE_QUERY); 804 } 805 806 static void trigger_next_service_by_uuid_query(gatt_client_t * gatt_client, uint16_t last_result_handle){ 807 trigger_next_query(gatt_client, last_result_handle, P_W2_SEND_SERVICE_WITH_UUID_QUERY); 808 } 809 810 static void trigger_next_characteristic_query(gatt_client_t * gatt_client, uint16_t last_result_handle){ 811 if (is_query_done(gatt_client, last_result_handle)){ 812 // report last characteristic 813 characteristic_end_found(gatt_client, gatt_client->end_group_handle); 814 } 815 trigger_next_query(gatt_client, last_result_handle, P_W2_SEND_ALL_CHARACTERISTICS_OF_SERVICE_QUERY); 816 } 817 818 static void trigger_next_characteristic_descriptor_query(gatt_client_t * gatt_client, uint16_t last_result_handle){ 819 trigger_next_query(gatt_client, last_result_handle, P_W2_SEND_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY); 820 } 821 822 static void trigger_next_read_by_type_query(gatt_client_t * gatt_client, uint16_t last_result_handle){ 823 trigger_next_query(gatt_client, last_result_handle, P_W2_SEND_READ_BY_TYPE_REQUEST); 824 } 825 826 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){ 827 gatt_client->attribute_offset += write_blob_length(gatt_client); 828 uint16_t next_blob_length = write_blob_length(gatt_client); 829 830 if (next_blob_length == 0u){ 831 gatt_client->gatt_client_state = done_state; 832 return; 833 } 834 gatt_client->gatt_client_state = next_query_state; 835 } 836 837 static void trigger_next_blob_query(gatt_client_t * gatt_client, gatt_client_state_t next_query_state, uint16_t received_blob_length){ 838 839 uint16_t max_blob_length = gatt_client->mtu - 1u; 840 if (received_blob_length < max_blob_length){ 841 gatt_client_handle_transaction_complete(gatt_client); 842 emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS); 843 return; 844 } 845 846 gatt_client->attribute_offset += received_blob_length; 847 gatt_client->gatt_client_state = next_query_state; 848 } 849 850 851 static int is_value_valid(gatt_client_t *gatt_client, uint8_t *packet, uint16_t size){ 852 uint16_t attribute_handle = little_endian_read_16(packet, 1); 853 uint16_t value_offset = little_endian_read_16(packet, 3); 854 855 if (gatt_client->attribute_handle != attribute_handle) return 0; 856 if (gatt_client->attribute_offset != value_offset) return 0; 857 return memcmp(&gatt_client->attribute_value[gatt_client->attribute_offset], &packet[5], size - 5u) == 0u; 858 } 859 860 // returns 1 if packet was sent 861 static int gatt_client_run_for_gatt_client(gatt_client_t * gatt_client){ 862 863 // wait until re-encryption as central is complete 864 if (gap_reconnect_security_setup_active(gatt_client->con_handle)) return 0; 865 866 #ifdef ENABLE_GATT_CLIENT_PAIRING 867 // wait until pairing complete 868 if (gatt_client->wait_for_pairing_complete) return 0; 869 #endif 870 871 switch (gatt_client->mtu_state) { 872 case SEND_MTU_EXCHANGE: 873 gatt_client->mtu_state = SENT_MTU_EXCHANGE; 874 att_exchange_mtu_request(gatt_client->con_handle); 875 return 1; 876 case SENT_MTU_EXCHANGE: 877 return 0; 878 default: 879 break; 880 } 881 882 if (gatt_client->send_confirmation){ 883 gatt_client->send_confirmation = 0; 884 att_confirmation(gatt_client->con_handle); 885 return 1; 886 } 887 888 // check MTU for writes 889 switch (gatt_client->gatt_client_state){ 890 case P_W2_SEND_WRITE_CHARACTERISTIC_VALUE: 891 case P_W2_SEND_WRITE_CHARACTERISTIC_DESCRIPTOR: 892 if (gatt_client->attribute_length <= (gatt_client->mtu - 3u)) break; 893 log_error("gatt_client_run: value len %u > MTU %u - 3\n", gatt_client->attribute_length,gatt_client->mtu); 894 gatt_client_handle_transaction_complete(gatt_client); 895 emit_gatt_complete_event(gatt_client, ATT_ERROR_INVALID_ATTRIBUTE_VALUE_LENGTH); 896 return 0; 897 default: 898 break; 899 } 900 901 switch (gatt_client->gatt_client_state){ 902 case P_W2_SEND_SERVICE_QUERY: 903 gatt_client->gatt_client_state = P_W4_SERVICE_QUERY_RESULT; 904 send_gatt_services_request(gatt_client); 905 return 1; 906 907 case P_W2_SEND_SERVICE_WITH_UUID_QUERY: 908 gatt_client->gatt_client_state = P_W4_SERVICE_WITH_UUID_RESULT; 909 send_gatt_services_by_uuid_request(gatt_client); 910 return 1; 911 912 case P_W2_SEND_ALL_CHARACTERISTICS_OF_SERVICE_QUERY: 913 gatt_client->gatt_client_state = P_W4_ALL_CHARACTERISTICS_OF_SERVICE_QUERY_RESULT; 914 send_gatt_characteristic_request(gatt_client); 915 return 1; 916 917 case P_W2_SEND_CHARACTERISTIC_WITH_UUID_QUERY: 918 gatt_client->gatt_client_state = P_W4_CHARACTERISTIC_WITH_UUID_QUERY_RESULT; 919 send_gatt_characteristic_request(gatt_client); 920 return 1; 921 922 case P_W2_SEND_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY: 923 gatt_client->gatt_client_state = P_W4_CHARACTERISTIC_WITH_UUID_QUERY_RESULT; 924 send_gatt_characteristic_descriptor_request(gatt_client); 925 return 1; 926 927 case P_W2_SEND_INCLUDED_SERVICE_QUERY: 928 gatt_client->gatt_client_state = P_W4_INCLUDED_SERVICE_QUERY_RESULT; 929 send_gatt_included_service_request(gatt_client); 930 return 1; 931 932 case P_W2_SEND_INCLUDED_SERVICE_WITH_UUID_QUERY: 933 gatt_client->gatt_client_state = P_W4_INCLUDED_SERVICE_UUID_WITH_QUERY_RESULT; 934 send_gatt_included_service_uuid_request(gatt_client); 935 return 1; 936 937 case P_W2_SEND_READ_CHARACTERISTIC_VALUE_QUERY: 938 gatt_client->gatt_client_state = P_W4_READ_CHARACTERISTIC_VALUE_RESULT; 939 send_gatt_read_characteristic_value_request(gatt_client); 940 return 1; 941 942 case P_W2_SEND_READ_BLOB_QUERY: 943 gatt_client->gatt_client_state = P_W4_READ_BLOB_RESULT; 944 send_gatt_read_blob_request(gatt_client); 945 return 1; 946 947 case P_W2_SEND_READ_BY_TYPE_REQUEST: 948 gatt_client->gatt_client_state = P_W4_READ_BY_TYPE_RESPONSE; 949 send_gatt_read_by_type_request(gatt_client); 950 return 1; 951 952 case P_W2_SEND_READ_MULTIPLE_REQUEST: 953 gatt_client->gatt_client_state = P_W4_READ_MULTIPLE_RESPONSE; 954 send_gatt_read_multiple_request(gatt_client); 955 return 1; 956 957 case P_W2_SEND_WRITE_CHARACTERISTIC_VALUE: 958 gatt_client->gatt_client_state = P_W4_WRITE_CHARACTERISTIC_VALUE_RESULT; 959 send_gatt_write_attribute_value_request(gatt_client); 960 return 1; 961 962 case P_W2_PREPARE_WRITE: 963 gatt_client->gatt_client_state = P_W4_PREPARE_WRITE_RESULT; 964 send_gatt_prepare_write_request(gatt_client); 965 return 1; 966 967 case P_W2_PREPARE_WRITE_SINGLE: 968 gatt_client->gatt_client_state = P_W4_PREPARE_WRITE_SINGLE_RESULT; 969 send_gatt_prepare_write_request(gatt_client); 970 return 1; 971 972 case P_W2_PREPARE_RELIABLE_WRITE: 973 gatt_client->gatt_client_state = P_W4_PREPARE_RELIABLE_WRITE_RESULT; 974 send_gatt_prepare_write_request(gatt_client); 975 return 1; 976 977 case P_W2_EXECUTE_PREPARED_WRITE: 978 gatt_client->gatt_client_state = P_W4_EXECUTE_PREPARED_WRITE_RESULT; 979 send_gatt_execute_write_request(gatt_client); 980 return 1; 981 982 case P_W2_CANCEL_PREPARED_WRITE: 983 gatt_client->gatt_client_state = P_W4_CANCEL_PREPARED_WRITE_RESULT; 984 send_gatt_cancel_prepared_write_request(gatt_client); 985 return 1; 986 987 case P_W2_CANCEL_PREPARED_WRITE_DATA_MISMATCH: 988 gatt_client->gatt_client_state = P_W4_CANCEL_PREPARED_WRITE_DATA_MISMATCH_RESULT; 989 send_gatt_cancel_prepared_write_request(gatt_client); 990 return 1; 991 992 #ifdef ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERY 993 case P_W2_SEND_FIND_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY: 994 // use Find Information 995 gatt_client->gatt_client_state = P_W4_FIND_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY_RESULT; 996 send_gatt_characteristic_descriptor_request(gatt_client); 997 #else 998 case P_W2_SEND_READ_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY: 999 // Use Read By Type 1000 gatt_client->gatt_client_state = P_W4_READ_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY_RESULT; 1001 send_gatt_read_client_characteristic_configuration_request(gatt_client); 1002 #endif 1003 return 1; 1004 1005 case P_W2_SEND_READ_CHARACTERISTIC_DESCRIPTOR_QUERY: 1006 gatt_client->gatt_client_state = P_W4_READ_CHARACTERISTIC_DESCRIPTOR_RESULT; 1007 send_gatt_read_characteristic_descriptor_request(gatt_client); 1008 return 1; 1009 1010 case P_W2_SEND_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_QUERY: 1011 gatt_client->gatt_client_state = P_W4_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_RESULT; 1012 send_gatt_read_blob_request(gatt_client); 1013 return 1; 1014 1015 case P_W2_SEND_WRITE_CHARACTERISTIC_DESCRIPTOR: 1016 gatt_client->gatt_client_state = P_W4_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT; 1017 send_gatt_write_attribute_value_request(gatt_client); 1018 return 1; 1019 1020 case P_W2_WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION: 1021 gatt_client->gatt_client_state = P_W4_CLIENT_CHARACTERISTIC_CONFIGURATION_RESULT; 1022 send_gatt_write_client_characteristic_configuration_request(gatt_client); 1023 return 1; 1024 1025 case P_W2_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR: 1026 gatt_client->gatt_client_state = P_W4_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT; 1027 send_gatt_prepare_write_request(gatt_client); 1028 return 1; 1029 1030 case P_W2_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR: 1031 gatt_client->gatt_client_state = P_W4_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT; 1032 send_gatt_execute_write_request(gatt_client); 1033 return 1; 1034 1035 #ifdef ENABLE_LE_SIGNED_WRITE 1036 case P_W4_IDENTITY_RESOLVING: 1037 log_info("P_W4_IDENTITY_RESOLVING - state %x", sm_identity_resolving_state(gatt_client->con_handle)); 1038 switch (sm_identity_resolving_state(gatt_client->con_handle)){ 1039 case IRK_LOOKUP_SUCCEEDED: 1040 gatt_client->le_device_index = sm_le_device_index(gatt_client->con_handle); 1041 gatt_client->gatt_client_state = P_W4_CMAC_READY; 1042 break; 1043 case IRK_LOOKUP_FAILED: 1044 gatt_client_handle_transaction_complete(gatt_client); 1045 emit_gatt_complete_event(gatt_client, ATT_ERROR_BONDING_INFORMATION_MISSING); 1046 return 0; 1047 default: 1048 return 0; 1049 } 1050 1051 /* Fall through */ 1052 1053 case P_W4_CMAC_READY: 1054 if (sm_cmac_ready()){ 1055 sm_key_t csrk; 1056 le_device_db_local_csrk_get(gatt_client->le_device_index, csrk); 1057 uint32_t sign_counter = le_device_db_local_counter_get(gatt_client->le_device_index); 1058 gatt_client->gatt_client_state = P_W4_CMAC_RESULT; 1059 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); 1060 } 1061 return 0; 1062 1063 case P_W2_SEND_SIGNED_WRITE: { 1064 gatt_client->gatt_client_state = P_W4_SEND_SINGED_WRITE_DONE; 1065 // bump local signing counter 1066 uint32_t sign_counter = le_device_db_local_counter_get(gatt_client->le_device_index); 1067 le_device_db_local_counter_set(gatt_client->le_device_index, sign_counter + 1); 1068 // send signed write command 1069 send_gatt_signed_write_request(gatt_client, sign_counter); 1070 // finally, notifiy client that write is complete 1071 gatt_client_handle_transaction_complete(gatt_client); 1072 emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS); 1073 return 1; 1074 } 1075 #endif 1076 default: 1077 break; 1078 } 1079 1080 // requested can send snow? 1081 if (gatt_client->write_without_response_callback){ 1082 btstack_packet_handler_t packet_handler = gatt_client->write_without_response_callback; 1083 gatt_client->write_without_response_callback = NULL; 1084 uint8_t event[4]; 1085 event[0] = GATT_EVENT_CAN_WRITE_WITHOUT_RESPONSE; 1086 event[1] = sizeof(event) - 2u; 1087 little_endian_store_16(event, 2, gatt_client->con_handle); 1088 packet_handler(HCI_EVENT_PACKET, gatt_client->con_handle, event, sizeof(event)); 1089 return 1; // to trigger requeueing (even if higher layer didn't sent) 1090 } 1091 1092 return 0; 1093 } 1094 1095 static void gatt_client_run(void){ 1096 btstack_linked_item_t *it; 1097 for (it = (btstack_linked_item_t *) gatt_client_connections; it != NULL; it = it->next){ 1098 gatt_client_t * gatt_client = (gatt_client_t *) it; 1099 if (!att_dispatch_client_can_send_now(gatt_client->con_handle)) { 1100 att_dispatch_client_request_can_send_now_event(gatt_client->con_handle); 1101 return; 1102 } 1103 int packet_sent = gatt_client_run_for_gatt_client(gatt_client); 1104 if (packet_sent){ 1105 // request new permission 1106 att_dispatch_client_request_can_send_now_event(gatt_client->con_handle); 1107 // requeue client for fairness and exit 1108 // note: iterator has become invalid 1109 btstack_linked_list_remove(&gatt_client_connections, (btstack_linked_item_t *) gatt_client); 1110 btstack_linked_list_add_tail(&gatt_client_connections, (btstack_linked_item_t *) gatt_client); 1111 return; 1112 } 1113 } 1114 } 1115 1116 static void gatt_client_report_error_if_pending(gatt_client_t *gatt_client, uint8_t att_error_code) { 1117 if (is_ready(gatt_client) == 1) return; 1118 gatt_client_handle_transaction_complete(gatt_client); 1119 emit_gatt_complete_event(gatt_client, att_error_code); 1120 } 1121 1122 static void gatt_client_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1123 UNUSED(channel); // ok: handling own l2cap events 1124 UNUSED(size); // ok: there is no channel 1125 1126 if (packet_type != HCI_EVENT_PACKET) return; 1127 1128 hci_con_handle_t con_handle; 1129 gatt_client_t * gatt_client; 1130 switch (hci_event_packet_get_type(packet)) { 1131 case HCI_EVENT_DISCONNECTION_COMPLETE: 1132 log_info("GATT Client: HCI_EVENT_DISCONNECTION_COMPLETE"); 1133 con_handle = little_endian_read_16(packet,3); 1134 gatt_client = gatt_client_get_context_for_handle(con_handle); 1135 if (gatt_client == NULL) break; 1136 1137 gatt_client_report_error_if_pending(gatt_client, ATT_ERROR_HCI_DISCONNECT_RECEIVED); 1138 gatt_client_timeout_stop(gatt_client); 1139 btstack_linked_list_remove(&gatt_client_connections, (btstack_linked_item_t *) gatt_client); 1140 btstack_memory_gatt_client_free(gatt_client); 1141 break; 1142 1143 #ifdef ENABLE_GATT_CLIENT_PAIRING 1144 // Pairing complete (with/without bonding=storing of pairing information) 1145 case SM_EVENT_PAIRING_COMPLETE: 1146 con_handle = sm_event_pairing_complete_get_handle(packet); 1147 gatt_client = gatt_client_get_context_for_handle(con_handle); 1148 if (gatt_client == NULL) break; 1149 1150 if (gatt_client->wait_for_pairing_complete){ 1151 gatt_client->wait_for_pairing_complete = 0; 1152 if (sm_event_pairing_complete_get_status(packet)){ 1153 log_info("pairing failed, report previous error 0x%x", gatt_client->pending_error_code); 1154 gatt_client_report_error_if_pending(gatt_client, gatt_client->pending_error_code); 1155 } else { 1156 log_info("pairing success, retry operation"); 1157 } 1158 } 1159 break; 1160 #endif 1161 #ifdef ENABLE_LE_SIGNED_WRITE 1162 // Identity Resolving completed (no code, gatt_client_run will continue) 1163 case SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED: 1164 case SM_EVENT_IDENTITY_RESOLVING_FAILED: 1165 break; 1166 #endif 1167 #ifdef ENABLE_LE_PROACTIVE_AUTHENTICATION 1168 // re-encryption complete 1169 case SM_EVENT_REENCRYPTION_COMPLETE: 1170 con_handle = sm_event_reencryption_complete_get_handle(packet); 1171 gatt_client = gatt_client_get_context_for_handle(con_handle); 1172 if (gatt_client == NULL) break; 1173 1174 // report bonding information missing, if re-encryption failed 1175 if (sm_event_reencryption_complete_get_status(packet)){ 1176 gatt_client_report_error_if_pending(gatt_client, ATT_ERROR_BONDING_INFORMATION_MISSING); 1177 } else { 1178 log_info("re-encryption success, retry operation"); 1179 } 1180 break; 1181 #endif 1182 default: 1183 break; 1184 } 1185 1186 gatt_client_run(); 1187 } 1188 1189 static void gatt_client_att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){ 1190 gatt_client_t * gatt_client; 1191 if (size < 1u) return; 1192 1193 if (packet_type == HCI_EVENT_PACKET) { 1194 switch (packet[0]){ 1195 case L2CAP_EVENT_CAN_SEND_NOW: 1196 gatt_client_run(); 1197 break; 1198 // att_server has negotiated the mtu for this connection, cache if context exists 1199 case ATT_EVENT_MTU_EXCHANGE_COMPLETE: 1200 if (size < 6u) break; 1201 gatt_client = gatt_client_get_context_for_handle(handle); 1202 if (gatt_client == NULL) break; 1203 gatt_client->mtu = little_endian_read_16(packet, 4); 1204 break; 1205 default: 1206 break; 1207 } 1208 return; 1209 } 1210 1211 if (packet_type != ATT_DATA_PACKET) return; 1212 1213 // special cases: notifications don't need a context while indications motivate creating one 1214 switch (packet[0]){ 1215 case ATT_HANDLE_VALUE_NOTIFICATION: 1216 if (size < 3u) return; 1217 report_gatt_notification(handle, little_endian_read_16(packet,1u), &packet[3], size-3u); 1218 return; 1219 case ATT_HANDLE_VALUE_INDICATION: 1220 gatt_client = gatt_client_provide_context_for_handle(handle); 1221 break; 1222 default: 1223 gatt_client = gatt_client_get_context_for_handle(handle); 1224 break; 1225 } 1226 1227 if (gatt_client == NULL) return; 1228 1229 uint8_t error_code; 1230 switch (packet[0]){ 1231 case ATT_EXCHANGE_MTU_RESPONSE: 1232 { 1233 if (size < 3u) break; 1234 uint16_t remote_rx_mtu = little_endian_read_16(packet, 1); 1235 uint16_t local_rx_mtu = l2cap_max_le_mtu(); 1236 gatt_client->mtu = (remote_rx_mtu < local_rx_mtu) ? remote_rx_mtu : local_rx_mtu; 1237 gatt_client->mtu_state = MTU_EXCHANGED; 1238 emit_gatt_mtu_exchanged_result_event(gatt_client, gatt_client->mtu); 1239 break; 1240 } 1241 case ATT_READ_BY_GROUP_TYPE_RESPONSE: 1242 switch(gatt_client->gatt_client_state){ 1243 case P_W4_SERVICE_QUERY_RESULT: 1244 report_gatt_services(gatt_client, packet, size); 1245 trigger_next_service_query(gatt_client, get_last_result_handle_from_service_list(packet, size)); 1246 // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done 1247 break; 1248 default: 1249 break; 1250 } 1251 break; 1252 case ATT_HANDLE_VALUE_INDICATION: 1253 if (size < 3u) break; 1254 report_gatt_indication(handle, little_endian_read_16(packet,1u), &packet[3], size-3u); 1255 gatt_client->send_confirmation = 1; 1256 break; 1257 1258 case ATT_READ_BY_TYPE_RESPONSE: 1259 switch (gatt_client->gatt_client_state){ 1260 case P_W4_ALL_CHARACTERISTICS_OF_SERVICE_QUERY_RESULT: 1261 report_gatt_characteristics(gatt_client, packet, size); 1262 trigger_next_characteristic_query(gatt_client, get_last_result_handle_from_characteristics_list(packet, size)); 1263 // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done, or by ATT_ERROR 1264 break; 1265 case P_W4_CHARACTERISTIC_WITH_UUID_QUERY_RESULT: 1266 report_gatt_characteristics(gatt_client, packet, size); 1267 trigger_next_characteristic_query(gatt_client, get_last_result_handle_from_characteristics_list(packet, size)); 1268 // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done, or by ATT_ERROR 1269 break; 1270 case P_W4_INCLUDED_SERVICE_QUERY_RESULT: 1271 { 1272 if (size < 2u) break; 1273 uint16_t uuid16 = 0; 1274 uint16_t pair_size = packet[1]; 1275 1276 if (pair_size == 6u){ 1277 if (size < 8u) break; 1278 // UUIDs not available, query first included service 1279 gatt_client->start_group_handle = little_endian_read_16(packet, 2); // ready for next query 1280 gatt_client->query_start_handle = little_endian_read_16(packet, 4); 1281 gatt_client->query_end_handle = little_endian_read_16(packet, 6); 1282 gatt_client->gatt_client_state = P_W2_SEND_INCLUDED_SERVICE_WITH_UUID_QUERY; 1283 break; 1284 } 1285 1286 if (pair_size != 8u) break; 1287 1288 // UUIDs included, report all of them 1289 uint16_t offset; 1290 for (offset = 2u; (offset + 8u) <= size; offset += pair_size){ 1291 uint16_t include_handle = little_endian_read_16(packet, offset); 1292 gatt_client->query_start_handle = little_endian_read_16(packet, offset + 2u); 1293 gatt_client->query_end_handle = little_endian_read_16(packet, offset + 4u); 1294 uuid16 = little_endian_read_16(packet, offset+6u); 1295 report_gatt_included_service_uuid16(gatt_client, include_handle, uuid16); 1296 } 1297 1298 trigger_next_included_service_query(gatt_client, get_last_result_handle_from_included_services_list(packet, size)); 1299 // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done 1300 break; 1301 } 1302 #ifndef ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERY 1303 case P_W4_READ_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY_RESULT: 1304 gatt_client->client_characteristic_configuration_handle = little_endian_read_16(packet, 2); 1305 gatt_client->gatt_client_state = P_W2_WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION; 1306 break; 1307 #endif 1308 case P_W4_READ_BY_TYPE_RESPONSE: { 1309 uint16_t pair_size = packet[1]; 1310 uint16_t offset; 1311 uint16_t last_result_handle = 0; 1312 for (offset = 2; offset < size ; offset += pair_size){ 1313 uint16_t value_handle = little_endian_read_16(packet, offset); 1314 report_gatt_characteristic_value(gatt_client, value_handle, &packet[offset + 2u], pair_size - 2u); 1315 last_result_handle = value_handle; 1316 } 1317 trigger_next_read_by_type_query(gatt_client, last_result_handle); 1318 break; 1319 } 1320 default: 1321 break; 1322 } 1323 break; 1324 case ATT_READ_RESPONSE: 1325 switch (gatt_client->gatt_client_state){ 1326 case P_W4_INCLUDED_SERVICE_UUID_WITH_QUERY_RESULT: { 1327 uint8_t uuid128[16]; 1328 reverse_128(&packet[1], uuid128); 1329 report_gatt_included_service_uuid128(gatt_client, gatt_client->start_group_handle, uuid128); 1330 trigger_next_included_service_query(gatt_client, gatt_client->start_group_handle); 1331 // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done 1332 break; 1333 } 1334 case P_W4_READ_CHARACTERISTIC_VALUE_RESULT: 1335 gatt_client_handle_transaction_complete(gatt_client); 1336 report_gatt_characteristic_value(gatt_client, gatt_client->attribute_handle, &packet[1], size - 1u); 1337 emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS); 1338 break; 1339 1340 case P_W4_READ_CHARACTERISTIC_DESCRIPTOR_RESULT:{ 1341 gatt_client_handle_transaction_complete(gatt_client); 1342 report_gatt_characteristic_descriptor(gatt_client, gatt_client->attribute_handle, &packet[1], size - 1u, 0u); 1343 emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS); 1344 break; 1345 } 1346 default: 1347 break; 1348 } 1349 break; 1350 1351 case ATT_FIND_BY_TYPE_VALUE_RESPONSE: 1352 { 1353 uint8_t pair_size = 4; 1354 int i; 1355 uint16_t start_group_handle; 1356 uint16_t end_group_handle= 0xffff; // asserts GATT_EVENT_QUERY_COMPLETE is emitted if no results 1357 for (i = 1u; (i + pair_size) <= size; i += pair_size){ 1358 start_group_handle = little_endian_read_16(packet,i); 1359 end_group_handle = little_endian_read_16(packet,i+2); 1360 emit_gatt_service_query_result_event(gatt_client, start_group_handle, end_group_handle, gatt_client->uuid128); 1361 } 1362 trigger_next_service_by_uuid_query(gatt_client, end_group_handle); 1363 // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done 1364 break; 1365 } 1366 case ATT_FIND_INFORMATION_REPLY: 1367 { 1368 if (size < 2u) break; 1369 1370 uint8_t pair_size = 4; 1371 if (packet[1u] == 2u){ 1372 pair_size = 18; 1373 } 1374 uint16_t offset = 2; 1375 1376 if (size < (pair_size + offset)) break; 1377 uint16_t last_descriptor_handle = little_endian_read_16(packet, size - pair_size); 1378 1379 #ifdef ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERY 1380 log_info("ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERY, state %x", gatt_client->gatt_client_state); 1381 if (gatt_client->gatt_client_state == P_W4_FIND_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY_RESULT){ 1382 // iterate over descriptors looking for CCC 1383 if (pair_size == 4){ 1384 while ((offset + 4) <= size){ 1385 uint16_t uuid16 = little_endian_read_16(packet, offset + 2); 1386 if (uuid16 == GATT_CLIENT_CHARACTERISTICS_CONFIGURATION){ 1387 gatt_client->client_characteristic_configuration_handle = little_endian_read_16(packet, offset); 1388 gatt_client->gatt_client_state = P_W2_WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION; 1389 log_info("CCC found %x", gatt_client->client_characteristic_configuration_handle); 1390 break; 1391 } 1392 offset += pair_size; 1393 } 1394 } 1395 if (is_query_done(gatt_client, last_descriptor_handle)){ 1396 1397 } else { 1398 // next 1399 gatt_client->start_group_handle = last_descriptor_handle + 1; 1400 gatt_client->gatt_client_state = P_W2_SEND_FIND_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY; 1401 } 1402 break; 1403 } 1404 #endif 1405 report_gatt_all_characteristic_descriptors(gatt_client, &packet[2], size - 2u, pair_size); 1406 trigger_next_characteristic_descriptor_query(gatt_client, last_descriptor_handle); 1407 // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done 1408 break; 1409 } 1410 1411 case ATT_WRITE_RESPONSE: 1412 switch (gatt_client->gatt_client_state){ 1413 case P_W4_WRITE_CHARACTERISTIC_VALUE_RESULT: 1414 gatt_client_handle_transaction_complete(gatt_client); 1415 emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS); 1416 break; 1417 case P_W4_CLIENT_CHARACTERISTIC_CONFIGURATION_RESULT: 1418 gatt_client_handle_transaction_complete(gatt_client); 1419 emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS); 1420 break; 1421 case P_W4_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT: 1422 gatt_client_handle_transaction_complete(gatt_client); 1423 emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS); 1424 break; 1425 default: 1426 break; 1427 } 1428 break; 1429 1430 case ATT_READ_BLOB_RESPONSE:{ 1431 uint16_t received_blob_length = size-1u; 1432 switch(gatt_client->gatt_client_state){ 1433 case P_W4_READ_BLOB_RESULT: 1434 report_gatt_long_characteristic_value_blob(gatt_client, gatt_client->attribute_handle, &packet[1], received_blob_length, gatt_client->attribute_offset); 1435 trigger_next_blob_query(gatt_client, P_W2_SEND_READ_BLOB_QUERY, received_blob_length); 1436 // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done 1437 break; 1438 case P_W4_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_RESULT: 1439 report_gatt_long_characteristic_descriptor(gatt_client, gatt_client->attribute_handle, 1440 &packet[1], received_blob_length, 1441 gatt_client->attribute_offset); 1442 trigger_next_blob_query(gatt_client, P_W2_SEND_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_QUERY, received_blob_length); 1443 // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done 1444 break; 1445 default: 1446 break; 1447 } 1448 break; 1449 } 1450 case ATT_PREPARE_WRITE_RESPONSE: 1451 switch (gatt_client->gatt_client_state){ 1452 case P_W4_PREPARE_WRITE_SINGLE_RESULT: 1453 gatt_client_handle_transaction_complete(gatt_client); 1454 if (is_value_valid(gatt_client, packet, size)){ 1455 emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS); 1456 } else { 1457 emit_gatt_complete_event(gatt_client, ATT_ERROR_DATA_MISMATCH); 1458 } 1459 break; 1460 1461 case P_W4_PREPARE_WRITE_RESULT:{ 1462 gatt_client->attribute_offset = little_endian_read_16(packet, 3); 1463 trigger_next_prepare_write_query(gatt_client, P_W2_PREPARE_WRITE, P_W2_EXECUTE_PREPARED_WRITE); 1464 // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done 1465 break; 1466 } 1467 case P_W4_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT:{ 1468 gatt_client->attribute_offset = little_endian_read_16(packet, 3); 1469 trigger_next_prepare_write_query(gatt_client, P_W2_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR, P_W2_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR); 1470 // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done 1471 break; 1472 } 1473 case P_W4_PREPARE_RELIABLE_WRITE_RESULT:{ 1474 if (is_value_valid(gatt_client, packet, size)){ 1475 gatt_client->attribute_offset = little_endian_read_16(packet, 3); 1476 trigger_next_prepare_write_query(gatt_client, P_W2_PREPARE_RELIABLE_WRITE, P_W2_EXECUTE_PREPARED_WRITE); 1477 // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done 1478 break; 1479 } 1480 gatt_client->gatt_client_state = P_W2_CANCEL_PREPARED_WRITE_DATA_MISMATCH; 1481 break; 1482 } 1483 default: 1484 break; 1485 } 1486 break; 1487 1488 case ATT_EXECUTE_WRITE_RESPONSE: 1489 switch (gatt_client->gatt_client_state){ 1490 case P_W4_EXECUTE_PREPARED_WRITE_RESULT: 1491 gatt_client_handle_transaction_complete(gatt_client); 1492 emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS); 1493 break; 1494 case P_W4_CANCEL_PREPARED_WRITE_RESULT: 1495 gatt_client_handle_transaction_complete(gatt_client); 1496 emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS); 1497 break; 1498 case P_W4_CANCEL_PREPARED_WRITE_DATA_MISMATCH_RESULT: 1499 gatt_client_handle_transaction_complete(gatt_client); 1500 emit_gatt_complete_event(gatt_client, ATT_ERROR_DATA_MISMATCH); 1501 break; 1502 case P_W4_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT: 1503 gatt_client_handle_transaction_complete(gatt_client); 1504 emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS); 1505 break; 1506 default: 1507 break; 1508 1509 } 1510 break; 1511 1512 case ATT_READ_MULTIPLE_RESPONSE: 1513 switch(gatt_client->gatt_client_state){ 1514 case P_W4_READ_MULTIPLE_RESPONSE: 1515 report_gatt_characteristic_value(gatt_client, 0u, &packet[1], size - 1u); 1516 gatt_client_handle_transaction_complete(gatt_client); 1517 emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS); 1518 break; 1519 default: 1520 break; 1521 } 1522 break; 1523 1524 case ATT_ERROR_RESPONSE: 1525 if (size < 5u) return; 1526 error_code = packet[4]; 1527 switch (error_code){ 1528 case ATT_ERROR_ATTRIBUTE_NOT_FOUND: { 1529 switch(gatt_client->gatt_client_state){ 1530 case P_W4_SERVICE_QUERY_RESULT: 1531 case P_W4_SERVICE_WITH_UUID_RESULT: 1532 case P_W4_INCLUDED_SERVICE_QUERY_RESULT: 1533 case P_W4_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY_RESULT: 1534 gatt_client_handle_transaction_complete(gatt_client); 1535 emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS); 1536 break; 1537 case P_W4_ALL_CHARACTERISTICS_OF_SERVICE_QUERY_RESULT: 1538 case P_W4_CHARACTERISTIC_WITH_UUID_QUERY_RESULT: 1539 characteristic_end_found(gatt_client, gatt_client->end_group_handle); 1540 gatt_client_handle_transaction_complete(gatt_client); 1541 emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS); 1542 break; 1543 case P_W4_READ_BY_TYPE_RESPONSE: 1544 gatt_client_handle_transaction_complete(gatt_client); 1545 if (gatt_client->start_group_handle == gatt_client->query_start_handle){ 1546 emit_gatt_complete_event(gatt_client, ATT_ERROR_ATTRIBUTE_NOT_FOUND); 1547 } else { 1548 emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS); 1549 } 1550 break; 1551 default: 1552 gatt_client_report_error_if_pending(gatt_client, error_code); 1553 break; 1554 } 1555 break; 1556 } 1557 1558 #ifdef ENABLE_GATT_CLIENT_PAIRING 1559 1560 case ATT_ERROR_INSUFFICIENT_AUTHENTICATION: 1561 case ATT_ERROR_INSUFFICIENT_ENCRYPTION_KEY_SIZE: 1562 case ATT_ERROR_INSUFFICIENT_ENCRYPTION: { 1563 1564 // security too low 1565 if (gatt_client->security_counter > 0) { 1566 gatt_client_report_error_if_pending(gatt_client, error_code); 1567 break; 1568 } 1569 // start security 1570 gatt_client->security_counter++; 1571 1572 // setup action 1573 int retry = 1; 1574 switch (gatt_client->gatt_client_state){ 1575 case P_W4_READ_CHARACTERISTIC_VALUE_RESULT: 1576 gatt_client->gatt_client_state = P_W2_SEND_READ_CHARACTERISTIC_VALUE_QUERY ; 1577 break; 1578 case P_W4_READ_BLOB_RESULT: 1579 gatt_client->gatt_client_state = P_W2_SEND_READ_BLOB_QUERY; 1580 break; 1581 case P_W4_READ_BY_TYPE_RESPONSE: 1582 gatt_client->gatt_client_state = P_W2_SEND_READ_BY_TYPE_REQUEST; 1583 break; 1584 case P_W4_READ_MULTIPLE_RESPONSE: 1585 gatt_client->gatt_client_state = P_W2_SEND_READ_MULTIPLE_REQUEST; 1586 break; 1587 case P_W4_WRITE_CHARACTERISTIC_VALUE_RESULT: 1588 gatt_client->gatt_client_state = P_W2_SEND_WRITE_CHARACTERISTIC_VALUE; 1589 break; 1590 case P_W4_PREPARE_WRITE_RESULT: 1591 gatt_client->gatt_client_state = P_W2_PREPARE_WRITE; 1592 break; 1593 case P_W4_PREPARE_WRITE_SINGLE_RESULT: 1594 gatt_client->gatt_client_state = P_W2_PREPARE_WRITE_SINGLE; 1595 break; 1596 case P_W4_PREPARE_RELIABLE_WRITE_RESULT: 1597 gatt_client->gatt_client_state = P_W2_PREPARE_RELIABLE_WRITE; 1598 break; 1599 case P_W4_EXECUTE_PREPARED_WRITE_RESULT: 1600 gatt_client->gatt_client_state = P_W2_EXECUTE_PREPARED_WRITE; 1601 break; 1602 case P_W4_CANCEL_PREPARED_WRITE_RESULT: 1603 gatt_client->gatt_client_state = P_W2_CANCEL_PREPARED_WRITE; 1604 break; 1605 case P_W4_CANCEL_PREPARED_WRITE_DATA_MISMATCH_RESULT: 1606 gatt_client->gatt_client_state = P_W2_CANCEL_PREPARED_WRITE_DATA_MISMATCH; 1607 break; 1608 case P_W4_READ_CHARACTERISTIC_DESCRIPTOR_RESULT: 1609 gatt_client->gatt_client_state = P_W2_SEND_READ_CHARACTERISTIC_DESCRIPTOR_QUERY; 1610 break; 1611 case P_W4_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_RESULT: 1612 gatt_client->gatt_client_state = P_W2_SEND_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_QUERY; 1613 break; 1614 case P_W4_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT: 1615 gatt_client->gatt_client_state = P_W2_SEND_WRITE_CHARACTERISTIC_DESCRIPTOR; 1616 break; 1617 case P_W4_CLIENT_CHARACTERISTIC_CONFIGURATION_RESULT: 1618 gatt_client->gatt_client_state = P_W2_WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION; 1619 break; 1620 case P_W4_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT: 1621 gatt_client->gatt_client_state = P_W2_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR; 1622 break; 1623 case P_W4_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT: 1624 gatt_client->gatt_client_state = P_W2_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR; 1625 break; 1626 #ifdef ENABLE_LE_SIGNED_WRITE 1627 case P_W4_SEND_SINGED_WRITE_DONE: 1628 gatt_client->gatt_client_state = P_W2_SEND_SIGNED_WRITE; 1629 break; 1630 #endif 1631 default: 1632 log_info("retry not supported for state %x", gatt_client->gatt_client_state); 1633 retry = 0; 1634 break; 1635 } 1636 1637 if (!retry) { 1638 gatt_client_report_error_if_pending(gatt_client, error_code); 1639 break; 1640 } 1641 1642 log_info("security error, start pairing"); 1643 1644 // start pairing for higher security level 1645 gatt_client->wait_for_pairing_complete = 1; 1646 gatt_client->pending_error_code = error_code; 1647 sm_request_pairing(gatt_client->con_handle); 1648 break; 1649 } 1650 #endif 1651 1652 // nothing we can do about that 1653 case ATT_ERROR_INSUFFICIENT_AUTHORIZATION: 1654 default: 1655 gatt_client_report_error_if_pending(gatt_client, error_code); 1656 break; 1657 } 1658 break; 1659 1660 default: 1661 log_info("ATT Handler, unhandled response type 0x%02x", packet[0]); 1662 break; 1663 } 1664 gatt_client_run(); 1665 } 1666 1667 #ifdef ENABLE_LE_SIGNED_WRITE 1668 static void att_signed_write_handle_cmac_result(uint8_t hash[8]){ 1669 btstack_linked_list_iterator_t it; 1670 btstack_linked_list_iterator_init(&it, &gatt_client_connections); 1671 while (btstack_linked_list_iterator_has_next(&it)){ 1672 gatt_client_t * gatt_client = (gatt_client_t *) btstack_linked_list_iterator_next(&it); 1673 if (gatt_client->gatt_client_state == P_W4_CMAC_RESULT){ 1674 // store result 1675 (void)memcpy(gatt_client->cmac, hash, 8); 1676 // reverse_64(hash, gatt_client->cmac); 1677 gatt_client->gatt_client_state = P_W2_SEND_SIGNED_WRITE; 1678 gatt_client_run(); 1679 return; 1680 } 1681 } 1682 } 1683 1684 uint8_t gatt_client_signed_write_without_response(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t handle, uint16_t message_len, uint8_t * message){ 1685 gatt_client_t * gatt_client = gatt_client_provide_context_for_handle(con_handle); 1686 if (gatt_client == NULL) return BTSTACK_MEMORY_ALLOC_FAILED; 1687 if (is_ready(gatt_client) == 0) return GATT_CLIENT_IN_WRONG_STATE; 1688 1689 gatt_client->callback = callback; 1690 gatt_client->attribute_handle = handle; 1691 gatt_client->attribute_length = message_len; 1692 gatt_client->attribute_value = message; 1693 gatt_client->gatt_client_state = P_W4_IDENTITY_RESOLVING; 1694 gatt_client_run(); 1695 return ERROR_CODE_SUCCESS; 1696 } 1697 #endif 1698 1699 uint8_t gatt_client_discover_primary_services(btstack_packet_handler_t callback, hci_con_handle_t con_handle){ 1700 gatt_client_t * gatt_client = gatt_client_provide_context_for_handle_and_start_timer(con_handle); 1701 if (gatt_client == NULL) return BTSTACK_MEMORY_ALLOC_FAILED; 1702 if (is_ready(gatt_client) == 0) return GATT_CLIENT_IN_WRONG_STATE; 1703 1704 gatt_client->callback = callback; 1705 gatt_client->start_group_handle = 0x0001; 1706 gatt_client->end_group_handle = 0xffff; 1707 gatt_client->gatt_client_state = P_W2_SEND_SERVICE_QUERY; 1708 gatt_client->uuid16 = 0; 1709 gatt_client_run(); 1710 return ERROR_CODE_SUCCESS; 1711 } 1712 1713 1714 uint8_t gatt_client_discover_primary_services_by_uuid16(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t uuid16){ 1715 gatt_client_t * gatt_client = gatt_client_provide_context_for_handle_and_start_timer(con_handle); 1716 if (gatt_client == NULL) return BTSTACK_MEMORY_ALLOC_FAILED; 1717 if (is_ready(gatt_client) == 0) return GATT_CLIENT_IN_WRONG_STATE; 1718 1719 gatt_client->callback = callback; 1720 gatt_client->start_group_handle = 0x0001; 1721 gatt_client->end_group_handle = 0xffff; 1722 gatt_client->gatt_client_state = P_W2_SEND_SERVICE_WITH_UUID_QUERY; 1723 gatt_client->uuid16 = uuid16; 1724 uuid_add_bluetooth_prefix((uint8_t*) &(gatt_client->uuid128), gatt_client->uuid16); 1725 gatt_client_run(); 1726 return ERROR_CODE_SUCCESS; 1727 } 1728 1729 uint8_t gatt_client_discover_primary_services_by_uuid128(btstack_packet_handler_t callback, hci_con_handle_t con_handle, const uint8_t * uuid128){ 1730 gatt_client_t * gatt_client = gatt_client_provide_context_for_handle_and_start_timer(con_handle); 1731 if (gatt_client == NULL) return BTSTACK_MEMORY_ALLOC_FAILED; 1732 if (is_ready(gatt_client) == 0) return GATT_CLIENT_IN_WRONG_STATE; 1733 1734 gatt_client->callback = callback; 1735 gatt_client->start_group_handle = 0x0001; 1736 gatt_client->end_group_handle = 0xffff; 1737 gatt_client->uuid16 = 0; 1738 (void)memcpy(gatt_client->uuid128, uuid128, 16); 1739 gatt_client->gatt_client_state = P_W2_SEND_SERVICE_WITH_UUID_QUERY; 1740 gatt_client_run(); 1741 return ERROR_CODE_SUCCESS; 1742 } 1743 1744 uint8_t gatt_client_discover_characteristics_for_service(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_service_t *service){ 1745 gatt_client_t * gatt_client = gatt_client_provide_context_for_handle_and_start_timer(con_handle); 1746 if (gatt_client == NULL) return BTSTACK_MEMORY_ALLOC_FAILED; 1747 if (is_ready(gatt_client) == 0) return GATT_CLIENT_IN_WRONG_STATE; 1748 1749 gatt_client->callback = callback; 1750 gatt_client->start_group_handle = service->start_group_handle; 1751 gatt_client->end_group_handle = service->end_group_handle; 1752 gatt_client->filter_with_uuid = 0; 1753 gatt_client->characteristic_start_handle = 0; 1754 gatt_client->gatt_client_state = P_W2_SEND_ALL_CHARACTERISTICS_OF_SERVICE_QUERY; 1755 gatt_client_run(); 1756 return ERROR_CODE_SUCCESS; 1757 } 1758 1759 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){ 1760 gatt_client_t * gatt_client = gatt_client_provide_context_for_handle_and_start_timer(con_handle); 1761 if (gatt_client == NULL) return BTSTACK_MEMORY_ALLOC_FAILED; 1762 if (is_ready(gatt_client) == 0) return GATT_CLIENT_IN_WRONG_STATE; 1763 gatt_client->callback = callback; 1764 gatt_client->start_group_handle = service->start_group_handle; 1765 gatt_client->end_group_handle = service->end_group_handle; 1766 gatt_client->gatt_client_state = P_W2_SEND_INCLUDED_SERVICE_QUERY; 1767 1768 gatt_client_run(); 1769 return ERROR_CODE_SUCCESS; 1770 } 1771 1772 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){ 1773 gatt_client_t * gatt_client = gatt_client_provide_context_for_handle_and_start_timer(con_handle); 1774 if (gatt_client == NULL) return BTSTACK_MEMORY_ALLOC_FAILED; 1775 if (is_ready(gatt_client) == 0) return GATT_CLIENT_IN_WRONG_STATE; 1776 1777 gatt_client->callback = callback; 1778 gatt_client->start_group_handle = start_handle; 1779 gatt_client->end_group_handle = end_handle; 1780 gatt_client->filter_with_uuid = 1; 1781 gatt_client->uuid16 = uuid16; 1782 uuid_add_bluetooth_prefix((uint8_t*) &(gatt_client->uuid128), uuid16); 1783 gatt_client->characteristic_start_handle = 0; 1784 gatt_client->gatt_client_state = P_W2_SEND_CHARACTERISTIC_WITH_UUID_QUERY; 1785 gatt_client_run(); 1786 return ERROR_CODE_SUCCESS; 1787 } 1788 1789 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, uint8_t * uuid128){ 1790 gatt_client_t * gatt_client = gatt_client_provide_context_for_handle_and_start_timer(con_handle); 1791 if (gatt_client == NULL) return BTSTACK_MEMORY_ALLOC_FAILED; 1792 if (is_ready(gatt_client) == 0) return GATT_CLIENT_IN_WRONG_STATE; 1793 1794 gatt_client->callback = callback; 1795 gatt_client->start_group_handle = start_handle; 1796 gatt_client->end_group_handle = end_handle; 1797 gatt_client->filter_with_uuid = 1; 1798 gatt_client->uuid16 = 0; 1799 (void)memcpy(gatt_client->uuid128, uuid128, 16); 1800 gatt_client->characteristic_start_handle = 0; 1801 gatt_client->gatt_client_state = P_W2_SEND_CHARACTERISTIC_WITH_UUID_QUERY; 1802 gatt_client_run(); 1803 return ERROR_CODE_SUCCESS; 1804 } 1805 1806 1807 uint8_t gatt_client_discover_characteristics_for_service_by_uuid16(btstack_packet_handler_t callback, uint16_t handle, gatt_client_service_t *service, uint16_t uuid16){ 1808 return gatt_client_discover_characteristics_for_handle_range_by_uuid16(callback, handle, service->start_group_handle, service->end_group_handle, uuid16); 1809 } 1810 1811 uint8_t gatt_client_discover_characteristics_for_service_by_uuid128(btstack_packet_handler_t callback, uint16_t handle, gatt_client_service_t *service, uint8_t * uuid128){ 1812 return gatt_client_discover_characteristics_for_handle_range_by_uuid128(callback, handle, service->start_group_handle, service->end_group_handle, uuid128); 1813 } 1814 1815 uint8_t gatt_client_discover_characteristic_descriptors(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_characteristic_t *characteristic){ 1816 gatt_client_t * gatt_client = gatt_client_provide_context_for_handle_and_start_timer(con_handle); 1817 if (gatt_client == NULL) return BTSTACK_MEMORY_ALLOC_FAILED; 1818 if (is_ready(gatt_client) == 0) return GATT_CLIENT_IN_WRONG_STATE; 1819 1820 if (characteristic->value_handle == characteristic->end_handle){ 1821 emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS); 1822 return ERROR_CODE_SUCCESS; 1823 } 1824 gatt_client->callback = callback; 1825 gatt_client->start_group_handle = characteristic->value_handle + 1u; 1826 gatt_client->end_group_handle = characteristic->end_handle; 1827 gatt_client->gatt_client_state = P_W2_SEND_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY; 1828 gatt_client_run(); 1829 return ERROR_CODE_SUCCESS; 1830 } 1831 1832 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){ 1833 gatt_client_t * gatt_client = gatt_client_provide_context_for_handle_and_start_timer(con_handle); 1834 if (gatt_client == NULL) return BTSTACK_MEMORY_ALLOC_FAILED; 1835 if (is_ready(gatt_client) == 0) return GATT_CLIENT_IN_WRONG_STATE; 1836 1837 gatt_client->callback = callback; 1838 gatt_client->attribute_handle = value_handle; 1839 gatt_client->attribute_offset = 0; 1840 gatt_client->gatt_client_state = P_W2_SEND_READ_CHARACTERISTIC_VALUE_QUERY; 1841 gatt_client_run(); 1842 return ERROR_CODE_SUCCESS; 1843 } 1844 1845 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){ 1846 gatt_client_t * gatt_client = gatt_client_provide_context_for_handle_and_start_timer(con_handle); 1847 if (gatt_client == NULL) return BTSTACK_MEMORY_ALLOC_FAILED; 1848 if (is_ready(gatt_client) == 0) return GATT_CLIENT_IN_WRONG_STATE; 1849 1850 gatt_client->callback = callback; 1851 gatt_client->start_group_handle = start_handle; 1852 gatt_client->end_group_handle = end_handle; 1853 gatt_client->query_start_handle = start_handle; 1854 gatt_client->query_end_handle = end_handle; 1855 gatt_client->uuid16 = uuid16; 1856 uuid_add_bluetooth_prefix((uint8_t*) &(gatt_client->uuid128), uuid16); 1857 gatt_client->gatt_client_state = P_W2_SEND_READ_BY_TYPE_REQUEST; 1858 gatt_client_run(); 1859 return ERROR_CODE_SUCCESS; 1860 } 1861 1862 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, uint8_t * uuid128){ 1863 gatt_client_t * gatt_client = gatt_client_provide_context_for_handle_and_start_timer(con_handle); 1864 if (gatt_client == NULL) return BTSTACK_MEMORY_ALLOC_FAILED; 1865 if (is_ready(gatt_client) == 0) return GATT_CLIENT_IN_WRONG_STATE; 1866 1867 gatt_client->callback = callback; 1868 gatt_client->start_group_handle = start_handle; 1869 gatt_client->end_group_handle = end_handle; 1870 gatt_client->query_start_handle = start_handle; 1871 gatt_client->query_end_handle = end_handle; 1872 gatt_client->uuid16 = 0; 1873 (void)memcpy(gatt_client->uuid128, uuid128, 16); 1874 gatt_client->gatt_client_state = P_W2_SEND_READ_BY_TYPE_REQUEST; 1875 gatt_client_run(); 1876 return ERROR_CODE_SUCCESS; 1877 } 1878 1879 1880 uint8_t gatt_client_read_value_of_characteristic(btstack_packet_handler_t callback, uint16_t handle, gatt_client_characteristic_t *characteristic){ 1881 return gatt_client_read_value_of_characteristic_using_value_handle(callback, handle, characteristic->value_handle); 1882 } 1883 1884 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 characteristic_value_handle, uint16_t offset){ 1885 gatt_client_t * gatt_client = gatt_client_provide_context_for_handle_and_start_timer(con_handle); 1886 if (gatt_client == NULL) return BTSTACK_MEMORY_ALLOC_FAILED; 1887 if (is_ready(gatt_client) == 0) return GATT_CLIENT_IN_WRONG_STATE; 1888 1889 gatt_client->callback = callback; 1890 gatt_client->attribute_handle = characteristic_value_handle; 1891 gatt_client->attribute_offset = offset; 1892 gatt_client->gatt_client_state = P_W2_SEND_READ_BLOB_QUERY; 1893 gatt_client_run(); 1894 return ERROR_CODE_SUCCESS; 1895 } 1896 1897 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 characteristic_value_handle){ 1898 return gatt_client_read_long_value_of_characteristic_using_value_handle_with_offset(callback, con_handle, characteristic_value_handle, 0); 1899 } 1900 1901 uint8_t gatt_client_read_long_value_of_characteristic(btstack_packet_handler_t callback, uint16_t handle, gatt_client_characteristic_t *characteristic){ 1902 return gatt_client_read_long_value_of_characteristic_using_value_handle(callback, handle, characteristic->value_handle); 1903 } 1904 1905 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){ 1906 gatt_client_t * gatt_client = gatt_client_provide_context_for_handle_and_start_timer(con_handle); 1907 if (gatt_client == NULL) return BTSTACK_MEMORY_ALLOC_FAILED; 1908 if (is_ready(gatt_client) == 0) return GATT_CLIENT_IN_WRONG_STATE; 1909 1910 gatt_client->callback = callback; 1911 gatt_client->read_multiple_handle_count = num_value_handles; 1912 gatt_client->read_multiple_handles = value_handles; 1913 gatt_client->gatt_client_state = P_W2_SEND_READ_MULTIPLE_REQUEST; 1914 gatt_client_run(); 1915 return ERROR_CODE_SUCCESS; 1916 } 1917 1918 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){ 1919 gatt_client_t * gatt_client = gatt_client_provide_context_for_handle(con_handle); 1920 if (gatt_client == NULL) return BTSTACK_MEMORY_ALLOC_FAILED; 1921 1922 if (value_length > (gatt_client->mtu - 3u)) return GATT_CLIENT_VALUE_TOO_LONG; 1923 if (!att_dispatch_client_can_send_now(gatt_client->con_handle)) return GATT_CLIENT_BUSY; 1924 1925 return att_write_request(ATT_WRITE_COMMAND, gatt_client->con_handle, value_handle, value_length, value); 1926 } 1927 1928 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 * data){ 1929 gatt_client_t * gatt_client = gatt_client_provide_context_for_handle_and_start_timer(con_handle); 1930 if (gatt_client == NULL) return BTSTACK_MEMORY_ALLOC_FAILED; 1931 if (is_ready(gatt_client) == 0) return GATT_CLIENT_IN_WRONG_STATE; 1932 1933 gatt_client->callback = callback; 1934 gatt_client->attribute_handle = value_handle; 1935 gatt_client->attribute_length = value_length; 1936 gatt_client->attribute_value = data; 1937 gatt_client->gatt_client_state = P_W2_SEND_WRITE_CHARACTERISTIC_VALUE; 1938 gatt_client_run(); 1939 return ERROR_CODE_SUCCESS; 1940 } 1941 1942 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 * data){ 1943 gatt_client_t * gatt_client = gatt_client_provide_context_for_handle_and_start_timer(con_handle); 1944 if (gatt_client == NULL) return BTSTACK_MEMORY_ALLOC_FAILED; 1945 if (is_ready(gatt_client) == 0) return GATT_CLIENT_IN_WRONG_STATE; 1946 1947 gatt_client->callback = callback; 1948 gatt_client->attribute_handle = value_handle; 1949 gatt_client->attribute_length = value_length; 1950 gatt_client->attribute_offset = offset; 1951 gatt_client->attribute_value = data; 1952 gatt_client->gatt_client_state = P_W2_PREPARE_WRITE; 1953 gatt_client_run(); 1954 return ERROR_CODE_SUCCESS; 1955 } 1956 1957 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){ 1958 return gatt_client_write_long_value_of_characteristic_with_offset(callback, con_handle, value_handle, 0, value_length, value); 1959 } 1960 1961 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){ 1962 gatt_client_t * gatt_client = gatt_client_provide_context_for_handle_and_start_timer(con_handle); 1963 if (gatt_client == NULL) return BTSTACK_MEMORY_ALLOC_FAILED; 1964 if (is_ready(gatt_client) == 0) return GATT_CLIENT_IN_WRONG_STATE; 1965 1966 gatt_client->callback = callback; 1967 gatt_client->attribute_handle = value_handle; 1968 gatt_client->attribute_length = value_length; 1969 gatt_client->attribute_offset = 0; 1970 gatt_client->attribute_value = value; 1971 gatt_client->gatt_client_state = P_W2_PREPARE_RELIABLE_WRITE; 1972 gatt_client_run(); 1973 return ERROR_CODE_SUCCESS; 1974 } 1975 1976 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){ 1977 gatt_client_t * gatt_client = gatt_client_provide_context_for_handle_and_start_timer(con_handle); 1978 if (gatt_client == NULL) return BTSTACK_MEMORY_ALLOC_FAILED; 1979 if (is_ready(gatt_client) == 0) return GATT_CLIENT_IN_WRONG_STATE; 1980 1981 if ( (configuration & GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION) && 1982 ((characteristic->properties & ATT_PROPERTY_NOTIFY) == 0u)) { 1983 log_info("gatt_client_write_client_characteristic_configuration: GATT_CLIENT_CHARACTERISTIC_NOTIFICATION_NOT_SUPPORTED"); 1984 return GATT_CLIENT_CHARACTERISTIC_NOTIFICATION_NOT_SUPPORTED; 1985 } else if ( (configuration & GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_INDICATION) && 1986 ((characteristic->properties & ATT_PROPERTY_INDICATE) == 0u)){ 1987 log_info("gatt_client_write_client_characteristic_configuration: GATT_CLIENT_CHARACTERISTIC_INDICATION_NOT_SUPPORTED"); 1988 return GATT_CLIENT_CHARACTERISTIC_INDICATION_NOT_SUPPORTED; 1989 } 1990 1991 gatt_client->callback = callback; 1992 gatt_client->start_group_handle = characteristic->value_handle; 1993 gatt_client->end_group_handle = characteristic->end_handle; 1994 little_endian_store_16(gatt_client->client_characteristic_configuration_value, 0, configuration); 1995 1996 #ifdef ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERY 1997 gatt_client->gatt_client_state = P_W2_SEND_FIND_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY; 1998 #else 1999 gatt_client->gatt_client_state = P_W2_SEND_READ_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY; 2000 #endif 2001 gatt_client_run(); 2002 return ERROR_CODE_SUCCESS; 2003 } 2004 2005 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){ 2006 gatt_client_t * gatt_client = gatt_client_provide_context_for_handle_and_start_timer(con_handle); 2007 if (gatt_client == NULL) return BTSTACK_MEMORY_ALLOC_FAILED; 2008 if (is_ready(gatt_client) == 0) return GATT_CLIENT_IN_WRONG_STATE; 2009 2010 gatt_client->callback = callback; 2011 gatt_client->attribute_handle = descriptor_handle; 2012 2013 gatt_client->gatt_client_state = P_W2_SEND_READ_CHARACTERISTIC_DESCRIPTOR_QUERY; 2014 gatt_client_run(); 2015 return ERROR_CODE_SUCCESS; 2016 } 2017 2018 uint8_t gatt_client_read_characteristic_descriptor(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_characteristic_descriptor_t * descriptor){ 2019 return gatt_client_read_characteristic_descriptor_using_descriptor_handle(callback, con_handle, descriptor->handle); 2020 } 2021 2022 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){ 2023 gatt_client_t * gatt_client = gatt_client_provide_context_for_handle_and_start_timer(con_handle); 2024 if (gatt_client == NULL) return BTSTACK_MEMORY_ALLOC_FAILED; 2025 if (is_ready(gatt_client) == 0) return GATT_CLIENT_IN_WRONG_STATE; 2026 2027 gatt_client->callback = callback; 2028 gatt_client->attribute_handle = descriptor_handle; 2029 gatt_client->attribute_offset = offset; 2030 gatt_client->gatt_client_state = P_W2_SEND_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_QUERY; 2031 gatt_client_run(); 2032 return ERROR_CODE_SUCCESS; 2033 } 2034 2035 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){ 2036 return gatt_client_read_long_characteristic_descriptor_using_descriptor_handle_with_offset(callback, con_handle, descriptor_handle, 0); 2037 } 2038 2039 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){ 2040 return gatt_client_read_long_characteristic_descriptor_using_descriptor_handle(callback, con_handle, descriptor->handle); 2041 } 2042 2043 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 length, uint8_t * data){ 2044 gatt_client_t * gatt_client = gatt_client_provide_context_for_handle_and_start_timer(con_handle); 2045 if (gatt_client == NULL) return BTSTACK_MEMORY_ALLOC_FAILED; 2046 if (is_ready(gatt_client) == 0) return GATT_CLIENT_IN_WRONG_STATE; 2047 2048 gatt_client->callback = callback; 2049 gatt_client->attribute_handle = descriptor_handle; 2050 gatt_client->attribute_length = length; 2051 gatt_client->attribute_offset = 0; 2052 gatt_client->attribute_value = data; 2053 gatt_client->gatt_client_state = P_W2_SEND_WRITE_CHARACTERISTIC_DESCRIPTOR; 2054 gatt_client_run(); 2055 return ERROR_CODE_SUCCESS; 2056 } 2057 2058 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 length, uint8_t * value){ 2059 return gatt_client_write_characteristic_descriptor_using_descriptor_handle(callback, con_handle, descriptor->handle, length, value); 2060 } 2061 2062 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 length, uint8_t * data){ 2063 gatt_client_t * gatt_client = gatt_client_provide_context_for_handle_and_start_timer(con_handle); 2064 if (gatt_client == NULL) return BTSTACK_MEMORY_ALLOC_FAILED; 2065 if (is_ready(gatt_client) == 0) return GATT_CLIENT_IN_WRONG_STATE; 2066 2067 gatt_client->callback = callback; 2068 gatt_client->attribute_handle = descriptor_handle; 2069 gatt_client->attribute_length = length; 2070 gatt_client->attribute_offset = offset; 2071 gatt_client->attribute_value = data; 2072 gatt_client->gatt_client_state = P_W2_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR; 2073 gatt_client_run(); 2074 return ERROR_CODE_SUCCESS; 2075 } 2076 2077 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 length, uint8_t * data){ 2078 return gatt_client_write_long_characteristic_descriptor_using_descriptor_handle_with_offset(callback, con_handle, descriptor_handle, 0, length, data ); 2079 } 2080 2081 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 length, uint8_t * value){ 2082 return gatt_client_write_long_characteristic_descriptor_using_descriptor_handle(callback, con_handle, descriptor->handle, length, value); 2083 } 2084 2085 /** 2086 * @brief -> gatt complete event 2087 */ 2088 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 length, uint8_t * data){ 2089 gatt_client_t * gatt_client = gatt_client_provide_context_for_handle_and_start_timer(con_handle); 2090 if (gatt_client == NULL) return BTSTACK_MEMORY_ALLOC_FAILED; 2091 if (is_ready(gatt_client) == 0) return GATT_CLIENT_IN_WRONG_STATE; 2092 2093 gatt_client->callback = callback; 2094 gatt_client->attribute_handle = attribute_handle; 2095 gatt_client->attribute_length = length; 2096 gatt_client->attribute_offset = offset; 2097 gatt_client->attribute_value = data; 2098 gatt_client->gatt_client_state = P_W2_PREPARE_WRITE_SINGLE; 2099 gatt_client_run(); 2100 return ERROR_CODE_SUCCESS; 2101 } 2102 2103 /** 2104 * @brief -> gatt complete event 2105 */ 2106 uint8_t gatt_client_execute_write(btstack_packet_handler_t callback, hci_con_handle_t con_handle){ 2107 gatt_client_t * gatt_client = gatt_client_provide_context_for_handle_and_start_timer(con_handle); 2108 2109 if (gatt_client == NULL) return BTSTACK_MEMORY_ALLOC_FAILED; 2110 if (is_ready(gatt_client) == 0) return GATT_CLIENT_IN_WRONG_STATE; 2111 2112 gatt_client->callback = callback; 2113 gatt_client->gatt_client_state = P_W2_EXECUTE_PREPARED_WRITE; 2114 gatt_client_run(); 2115 return ERROR_CODE_SUCCESS; 2116 } 2117 2118 /** 2119 * @brief -> gatt complete event 2120 */ 2121 uint8_t gatt_client_cancel_write(btstack_packet_handler_t callback, hci_con_handle_t con_handle){ 2122 gatt_client_t * gatt_client = gatt_client_provide_context_for_handle_and_start_timer(con_handle); 2123 if (gatt_client == NULL) return BTSTACK_MEMORY_ALLOC_FAILED; 2124 if (is_ready(gatt_client) == 0) return GATT_CLIENT_IN_WRONG_STATE; 2125 2126 gatt_client->callback = callback; 2127 gatt_client->gatt_client_state = P_W2_CANCEL_PREPARED_WRITE; 2128 gatt_client_run(); 2129 return ERROR_CODE_SUCCESS; 2130 } 2131 2132 void gatt_client_deserialize_service(const uint8_t *packet, int offset, gatt_client_service_t *service){ 2133 service->start_group_handle = little_endian_read_16(packet, offset); 2134 service->end_group_handle = little_endian_read_16(packet, offset + 2); 2135 reverse_128(&packet[offset + 4], service->uuid128); 2136 if (uuid_has_bluetooth_prefix(service->uuid128)){ 2137 service->uuid16 = big_endian_read_32(service->uuid128, 0); 2138 } else { 2139 service->uuid16 = 0; 2140 } 2141 } 2142 2143 void gatt_client_deserialize_characteristic(const uint8_t * packet, int offset, gatt_client_characteristic_t * characteristic){ 2144 characteristic->start_handle = little_endian_read_16(packet, offset); 2145 characteristic->value_handle = little_endian_read_16(packet, offset + 2); 2146 characteristic->end_handle = little_endian_read_16(packet, offset + 4); 2147 characteristic->properties = little_endian_read_16(packet, offset + 6); 2148 reverse_128(&packet[offset+8], characteristic->uuid128); 2149 if (uuid_has_bluetooth_prefix(characteristic->uuid128)){ 2150 characteristic->uuid16 = big_endian_read_32(characteristic->uuid128, 0); 2151 } else { 2152 characteristic->uuid16 = 0; 2153 } 2154 } 2155 2156 void gatt_client_deserialize_characteristic_descriptor(const uint8_t * packet, int offset, gatt_client_characteristic_descriptor_t * descriptor){ 2157 descriptor->handle = little_endian_read_16(packet, offset); 2158 reverse_128(&packet[offset+2], descriptor->uuid128); 2159 if (uuid_has_bluetooth_prefix(descriptor->uuid128)){ 2160 descriptor->uuid16 = big_endian_read_32(descriptor->uuid128, 0); 2161 } else { 2162 descriptor->uuid16 = 0; 2163 } 2164 } 2165 2166 void gatt_client_send_mtu_negotiation(btstack_packet_handler_t callback, hci_con_handle_t con_handle){ 2167 gatt_client_t * context = gatt_client_provide_context_for_handle(con_handle); 2168 if (context == NULL) return; 2169 if (context->mtu_state == MTU_AUTO_EXCHANGE_DISABLED){ 2170 context->callback = callback; 2171 context->mtu_state = SEND_MTU_EXCHANGE; 2172 gatt_client_run(); 2173 } 2174 } 2175 2176 uint8_t gatt_client_request_can_write_without_response_event(btstack_packet_handler_t callback, hci_con_handle_t con_handle){ 2177 gatt_client_t * context = gatt_client_provide_context_for_handle(con_handle); 2178 if (context == NULL) return BTSTACK_MEMORY_ALLOC_FAILED; 2179 if (context->write_without_response_callback != NULL) return GATT_CLIENT_IN_WRONG_STATE; 2180 context->write_without_response_callback = callback; 2181 att_dispatch_client_request_can_send_now_event(context->con_handle); 2182 return ERROR_CODE_SUCCESS; 2183 } 2184 2185 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION 2186 void gatt_client_att_packet_handler_fuzz(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){ 2187 gatt_client_att_packet_handler(packet_type, handle, packet, size); 2188 } 2189 #endif 2190