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