13deb3ec6SMatthias Ringwald /* 23deb3ec6SMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH 33deb3ec6SMatthias Ringwald * 43deb3ec6SMatthias Ringwald * Redistribution and use in source and binary forms, with or without 53deb3ec6SMatthias Ringwald * modification, are permitted provided that the following conditions 63deb3ec6SMatthias Ringwald * are met: 73deb3ec6SMatthias Ringwald * 83deb3ec6SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 93deb3ec6SMatthias Ringwald * notice, this list of conditions and the following disclaimer. 103deb3ec6SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 113deb3ec6SMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 123deb3ec6SMatthias Ringwald * documentation and/or other materials provided with the distribution. 133deb3ec6SMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 143deb3ec6SMatthias Ringwald * contributors may be used to endorse or promote products derived 153deb3ec6SMatthias Ringwald * from this software without specific prior written permission. 163deb3ec6SMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 173deb3ec6SMatthias Ringwald * personal benefit and not for any commercial purpose or for 183deb3ec6SMatthias Ringwald * monetary gain. 193deb3ec6SMatthias Ringwald * 203deb3ec6SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 213deb3ec6SMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 223deb3ec6SMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 233deb3ec6SMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 243deb3ec6SMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 253deb3ec6SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 263deb3ec6SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 273deb3ec6SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 283deb3ec6SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 293deb3ec6SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 303deb3ec6SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 313deb3ec6SMatthias Ringwald * SUCH DAMAGE. 323deb3ec6SMatthias Ringwald * 333deb3ec6SMatthias Ringwald * Please inquire about commercial licensing options at 343deb3ec6SMatthias Ringwald * [email protected] 353deb3ec6SMatthias Ringwald * 363deb3ec6SMatthias Ringwald */ 373deb3ec6SMatthias Ringwald 38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "gatt_client.c" 39ab2c6ae4SMatthias Ringwald 403deb3ec6SMatthias Ringwald #include <stdint.h> 413deb3ec6SMatthias Ringwald #include <string.h> 423deb3ec6SMatthias Ringwald 437907f069SMatthias Ringwald #include "btstack_config.h" 443deb3ec6SMatthias Ringwald 450e2df43fSMatthias Ringwald #include "att_dispatch.h" 46fac60feaSMatthias Ringwald #include "ad_parser.h" 470e2df43fSMatthias Ringwald #include "ble/att_db.h" 4859c6af15SMatthias Ringwald #include "ble/core.h" 490e2df43fSMatthias Ringwald #include "ble/gatt_client.h" 500e2df43fSMatthias Ringwald #include "ble/le_device_db.h" 510e2df43fSMatthias Ringwald #include "ble/sm.h" 5216ece135SMatthias Ringwald #include "btstack_debug.h" 530e2df43fSMatthias Ringwald #include "btstack_event.h" 543deb3ec6SMatthias Ringwald #include "btstack_memory.h" 550e2df43fSMatthias Ringwald #include "btstack_run_loop.h" 560e2df43fSMatthias Ringwald #include "btstack_util.h" 570e2df43fSMatthias Ringwald #include "classic/sdp_util.h" 583deb3ec6SMatthias Ringwald #include "hci.h" 590e2df43fSMatthias Ringwald #include "hci_cmd.h" 603deb3ec6SMatthias Ringwald #include "hci_dump.h" 613deb3ec6SMatthias Ringwald #include "l2cap.h" 623deb3ec6SMatthias Ringwald 639c662c9bSMatthias Ringwald static btstack_linked_list_t gatt_client_connections; 649c662c9bSMatthias Ringwald static btstack_linked_list_t gatt_client_value_listeners; 65361b1363SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 663deb3ec6SMatthias Ringwald 67793cf6ceSMatthias Ringwald #if defined(ENABLE_GATT_CLIENT_PAIRING) || defined (ENABLE_LE_SIGNED_WRITE) 68f4b33574SMatthias Ringwald static btstack_packet_callback_registration_t sm_event_callback_registration; 69f4b33574SMatthias Ringwald #endif 70f4b33574SMatthias Ringwald 715cf6c434SJakob Krantz static uint8_t mtu_exchange_enabled; 725cf6c434SJakob Krantz 733deb3ec6SMatthias Ringwald static void gatt_client_att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size); 74f4b33574SMatthias Ringwald static void gatt_client_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 753deb3ec6SMatthias Ringwald static void gatt_client_report_error_if_pending(gatt_client_t *peripheral, uint8_t error_code); 767a766ebfSMatthias Ringwald 777a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE 783deb3ec6SMatthias Ringwald static void att_signed_write_handle_cmac_result(uint8_t hash[8]); 797a766ebfSMatthias Ringwald #endif 803deb3ec6SMatthias Ringwald 813deb3ec6SMatthias Ringwald static uint16_t peripheral_mtu(gatt_client_t *peripheral){ 823deb3ec6SMatthias Ringwald if (peripheral->mtu > l2cap_max_le_mtu()){ 833deb3ec6SMatthias Ringwald log_error("Peripheral mtu is not initialized"); 843deb3ec6SMatthias Ringwald return l2cap_max_le_mtu(); 853deb3ec6SMatthias Ringwald } 863deb3ec6SMatthias Ringwald return peripheral->mtu; 873deb3ec6SMatthias Ringwald } 883deb3ec6SMatthias Ringwald 893deb3ec6SMatthias Ringwald void gatt_client_init(void){ 903deb3ec6SMatthias Ringwald gatt_client_connections = NULL; 915cf6c434SJakob Krantz mtu_exchange_enabled = 1; 92f4b33574SMatthias Ringwald 93361b1363SMatthias Ringwald // regsister for HCI Events 94f4b33574SMatthias Ringwald hci_event_callback_registration.callback = &gatt_client_event_packet_handler; 95361b1363SMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 96361b1363SMatthias Ringwald 97793cf6ceSMatthias Ringwald #if defined(ENABLE_GATT_CLIENT_PAIRING) || defined (ENABLE_LE_SIGNED_WRITE) 98f4b33574SMatthias Ringwald // register for SM Events 99f4b33574SMatthias Ringwald sm_event_callback_registration.callback = &gatt_client_event_packet_handler; 100f4b33574SMatthias Ringwald sm_add_event_handler(&sm_event_callback_registration); 101f4b33574SMatthias Ringwald #endif 102f4b33574SMatthias Ringwald 103361b1363SMatthias Ringwald // and ATT Client PDUs 1043deb3ec6SMatthias Ringwald att_dispatch_register_client(gatt_client_att_packet_handler); 1053deb3ec6SMatthias Ringwald } 1063deb3ec6SMatthias Ringwald 107ec820d77SMatthias Ringwald static gatt_client_t * gatt_client_for_timer(btstack_timer_source_t * ts){ 108665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 109665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &gatt_client_connections); 110665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 111665d90f2SMatthias Ringwald gatt_client_t * peripheral = (gatt_client_t *) btstack_linked_list_iterator_next(&it); 1123deb3ec6SMatthias Ringwald if ( &peripheral->gc_timeout == ts) { 1133deb3ec6SMatthias Ringwald return peripheral; 1143deb3ec6SMatthias Ringwald } 1153deb3ec6SMatthias Ringwald } 1163deb3ec6SMatthias Ringwald return NULL; 1173deb3ec6SMatthias Ringwald } 1183deb3ec6SMatthias Ringwald 119ec820d77SMatthias Ringwald static void gatt_client_timeout_handler(btstack_timer_source_t * timer){ 1203deb3ec6SMatthias Ringwald gatt_client_t * peripheral = gatt_client_for_timer(timer); 1213deb3ec6SMatthias Ringwald if (!peripheral) return; 122fc64f94aSMatthias Ringwald log_info("GATT client timeout handle, handle 0x%02x", peripheral->con_handle); 1233deb3ec6SMatthias Ringwald gatt_client_report_error_if_pending(peripheral, ATT_ERROR_TIMEOUT); 1243deb3ec6SMatthias Ringwald } 1253deb3ec6SMatthias Ringwald 1263deb3ec6SMatthias Ringwald static void gatt_client_timeout_start(gatt_client_t * peripheral){ 127fc64f94aSMatthias Ringwald log_info("GATT client timeout start, handle 0x%02x", peripheral->con_handle); 128528a4a3bSMatthias Ringwald btstack_run_loop_remove_timer(&peripheral->gc_timeout); 129528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&peripheral->gc_timeout, gatt_client_timeout_handler); 130528a4a3bSMatthias Ringwald btstack_run_loop_set_timer(&peripheral->gc_timeout, 30000); // 30 seconds sm timeout 131528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&peripheral->gc_timeout); 1323deb3ec6SMatthias Ringwald } 1333deb3ec6SMatthias Ringwald 1343deb3ec6SMatthias Ringwald static void gatt_client_timeout_stop(gatt_client_t * peripheral){ 135fc64f94aSMatthias Ringwald log_info("GATT client timeout stop, handle 0x%02x", peripheral->con_handle); 136528a4a3bSMatthias Ringwald btstack_run_loop_remove_timer(&peripheral->gc_timeout); 1373deb3ec6SMatthias Ringwald } 1383deb3ec6SMatthias Ringwald 1393deb3ec6SMatthias Ringwald static gatt_client_t * get_gatt_client_context_for_handle(uint16_t handle){ 140665d90f2SMatthias Ringwald btstack_linked_item_t *it; 141665d90f2SMatthias Ringwald for (it = (btstack_linked_item_t *) gatt_client_connections; it ; it = it->next){ 1423deb3ec6SMatthias Ringwald gatt_client_t * peripheral = (gatt_client_t *) it; 143fc64f94aSMatthias Ringwald if (peripheral->con_handle == handle){ 1443deb3ec6SMatthias Ringwald return peripheral; 1453deb3ec6SMatthias Ringwald } 1463deb3ec6SMatthias Ringwald } 1473deb3ec6SMatthias Ringwald return NULL; 1483deb3ec6SMatthias Ringwald } 1493deb3ec6SMatthias Ringwald 1503deb3ec6SMatthias Ringwald 1513deb3ec6SMatthias Ringwald // @returns context 1523deb3ec6SMatthias Ringwald // returns existing one, or tries to setup new one 153711e6c80SMatthias Ringwald static gatt_client_t * provide_context_for_conn_handle(hci_con_handle_t con_handle){ 1543deb3ec6SMatthias Ringwald gatt_client_t * context = get_gatt_client_context_for_handle(con_handle); 1553deb3ec6SMatthias Ringwald if (context) return context; 1563deb3ec6SMatthias Ringwald 157ae1d1237SMatthias Ringwald // bail if no such hci connection 158ae1d1237SMatthias Ringwald if (!hci_connection_for_handle(con_handle)){ 159ae1d1237SMatthias Ringwald log_error("No connection for handle 0x%04x", con_handle); 160ae1d1237SMatthias Ringwald return NULL; 161ae1d1237SMatthias Ringwald } 1623deb3ec6SMatthias Ringwald context = btstack_memory_gatt_client_get(); 1633deb3ec6SMatthias Ringwald if (!context) return NULL; 1643deb3ec6SMatthias Ringwald // init state 165fc64f94aSMatthias Ringwald context->con_handle = con_handle; 1663deb3ec6SMatthias Ringwald context->mtu = ATT_DEFAULT_MTU; 1675cf6c434SJakob Krantz if (mtu_exchange_enabled){ 1683deb3ec6SMatthias Ringwald context->mtu_state = SEND_MTU_EXCHANGE; 1695cf6c434SJakob Krantz } else { 1705cf6c434SJakob Krantz context->mtu_state = MTU_AUTO_EXCHANGE_DISABLED; 1715cf6c434SJakob Krantz } 1723deb3ec6SMatthias Ringwald context->gatt_client_state = P_READY; 173665d90f2SMatthias Ringwald btstack_linked_list_add(&gatt_client_connections, (btstack_linked_item_t*)context); 1743deb3ec6SMatthias Ringwald return context; 1753deb3ec6SMatthias Ringwald } 1763deb3ec6SMatthias Ringwald 177711e6c80SMatthias Ringwald static gatt_client_t * provide_context_for_conn_handle_and_start_timer(hci_con_handle_t con_handle){ 1783deb3ec6SMatthias Ringwald gatt_client_t * context = provide_context_for_conn_handle(con_handle); 1793deb3ec6SMatthias Ringwald if (!context) return NULL; 1803deb3ec6SMatthias Ringwald gatt_client_timeout_start(context); 1813deb3ec6SMatthias Ringwald return context; 1823deb3ec6SMatthias Ringwald } 1833deb3ec6SMatthias Ringwald 1843deb3ec6SMatthias Ringwald static int is_ready(gatt_client_t * context){ 1853deb3ec6SMatthias Ringwald return context->gatt_client_state == P_READY; 1863deb3ec6SMatthias Ringwald } 1873deb3ec6SMatthias Ringwald 188fc64f94aSMatthias Ringwald int gatt_client_is_ready(hci_con_handle_t con_handle){ 189fc64f94aSMatthias Ringwald gatt_client_t * context = provide_context_for_conn_handle(con_handle); 1903deb3ec6SMatthias Ringwald if (!context) return 0; 1913deb3ec6SMatthias Ringwald return is_ready(context); 1923deb3ec6SMatthias Ringwald } 1933deb3ec6SMatthias Ringwald 1945cf6c434SJakob Krantz void gatt_client_mtu_enable_auto_negotiation(uint8_t enabled){ 1955cf6c434SJakob Krantz mtu_exchange_enabled = enabled; 1965cf6c434SJakob Krantz } 1975cf6c434SJakob Krantz 198fc64f94aSMatthias Ringwald uint8_t gatt_client_get_mtu(hci_con_handle_t con_handle, uint16_t * mtu){ 199fc64f94aSMatthias Ringwald gatt_client_t * context = provide_context_for_conn_handle(con_handle); 2005cf6c434SJakob Krantz if (context && (context->mtu_state == MTU_EXCHANGED || context->mtu_state == MTU_AUTO_EXCHANGE_DISABLED)){ 2013deb3ec6SMatthias Ringwald *mtu = context->mtu; 202616edd56SMatthias Ringwald return 0; 2033deb3ec6SMatthias Ringwald } 2043deb3ec6SMatthias Ringwald *mtu = ATT_DEFAULT_MTU; 205616edd56SMatthias Ringwald return GATT_CLIENT_IN_WRONG_STATE; 2063deb3ec6SMatthias Ringwald } 2073deb3ec6SMatthias Ringwald 2083deb3ec6SMatthias Ringwald // precondition: can_send_packet_now == TRUE 2093deb3ec6SMatthias Ringwald static void att_confirmation(uint16_t peripheral_handle){ 2103deb3ec6SMatthias Ringwald l2cap_reserve_packet_buffer(); 2113deb3ec6SMatthias Ringwald uint8_t * request = l2cap_get_outgoing_buffer(); 2123deb3ec6SMatthias Ringwald request[0] = ATT_HANDLE_VALUE_CONFIRMATION; 2133deb3ec6SMatthias Ringwald l2cap_send_prepared_connectionless(peripheral_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, 1); 2143deb3ec6SMatthias Ringwald } 2153deb3ec6SMatthias Ringwald 2163deb3ec6SMatthias Ringwald // precondition: can_send_packet_now == TRUE 2173deb3ec6SMatthias Ringwald static void att_find_information_request(uint16_t request_type, uint16_t peripheral_handle, uint16_t start_handle, uint16_t end_handle){ 2183deb3ec6SMatthias Ringwald l2cap_reserve_packet_buffer(); 2193deb3ec6SMatthias Ringwald uint8_t * request = l2cap_get_outgoing_buffer(); 2203deb3ec6SMatthias Ringwald request[0] = request_type; 221f8fbdce0SMatthias Ringwald little_endian_store_16(request, 1, start_handle); 222f8fbdce0SMatthias Ringwald little_endian_store_16(request, 3, end_handle); 2233deb3ec6SMatthias Ringwald 2243deb3ec6SMatthias Ringwald l2cap_send_prepared_connectionless(peripheral_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, 5); 2253deb3ec6SMatthias Ringwald } 2263deb3ec6SMatthias Ringwald 2273deb3ec6SMatthias Ringwald // precondition: can_send_packet_now == TRUE 2283deb3ec6SMatthias Ringwald static void att_find_by_type_value_request(uint16_t request_type, uint16_t attribute_group_type, uint16_t peripheral_handle, uint16_t start_handle, uint16_t end_handle, uint8_t * value, uint16_t value_size){ 2293deb3ec6SMatthias Ringwald l2cap_reserve_packet_buffer(); 2303deb3ec6SMatthias Ringwald uint8_t * request = l2cap_get_outgoing_buffer(); 2313deb3ec6SMatthias Ringwald 2323deb3ec6SMatthias Ringwald request[0] = request_type; 233f8fbdce0SMatthias Ringwald little_endian_store_16(request, 1, start_handle); 234f8fbdce0SMatthias Ringwald little_endian_store_16(request, 3, end_handle); 235f8fbdce0SMatthias Ringwald little_endian_store_16(request, 5, attribute_group_type); 2363deb3ec6SMatthias Ringwald memcpy(&request[7], value, value_size); 2373deb3ec6SMatthias Ringwald 2383deb3ec6SMatthias Ringwald l2cap_send_prepared_connectionless(peripheral_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, 7+value_size); 2393deb3ec6SMatthias Ringwald } 2403deb3ec6SMatthias Ringwald 2413deb3ec6SMatthias Ringwald // precondition: can_send_packet_now == TRUE 2423deb3ec6SMatthias Ringwald static void att_read_by_type_or_group_request_for_uuid16(uint16_t request_type, uint16_t uuid16, uint16_t peripheral_handle, uint16_t start_handle, uint16_t end_handle){ 2433deb3ec6SMatthias Ringwald l2cap_reserve_packet_buffer(); 2443deb3ec6SMatthias Ringwald uint8_t * request = l2cap_get_outgoing_buffer(); 2453deb3ec6SMatthias Ringwald request[0] = request_type; 246f8fbdce0SMatthias Ringwald little_endian_store_16(request, 1, start_handle); 247f8fbdce0SMatthias Ringwald little_endian_store_16(request, 3, end_handle); 248f8fbdce0SMatthias Ringwald little_endian_store_16(request, 5, uuid16); 2493deb3ec6SMatthias Ringwald 2503deb3ec6SMatthias Ringwald l2cap_send_prepared_connectionless(peripheral_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, 7); 2513deb3ec6SMatthias Ringwald } 2523deb3ec6SMatthias Ringwald 2533deb3ec6SMatthias Ringwald // precondition: can_send_packet_now == TRUE 2543deb3ec6SMatthias Ringwald static void att_read_by_type_or_group_request_for_uuid128(uint16_t request_type, uint8_t * uuid128, uint16_t peripheral_handle, uint16_t start_handle, uint16_t end_handle){ 2553deb3ec6SMatthias Ringwald l2cap_reserve_packet_buffer(); 2563deb3ec6SMatthias Ringwald uint8_t * request = l2cap_get_outgoing_buffer(); 2573deb3ec6SMatthias Ringwald request[0] = request_type; 258f8fbdce0SMatthias Ringwald little_endian_store_16(request, 1, start_handle); 259f8fbdce0SMatthias Ringwald little_endian_store_16(request, 3, end_handle); 2609c80e4ccSMatthias Ringwald reverse_128(uuid128, &request[5]); 2613deb3ec6SMatthias Ringwald 2623deb3ec6SMatthias Ringwald l2cap_send_prepared_connectionless(peripheral_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, 21); 2633deb3ec6SMatthias Ringwald } 2643deb3ec6SMatthias Ringwald 2653deb3ec6SMatthias Ringwald // precondition: can_send_packet_now == TRUE 2663deb3ec6SMatthias Ringwald static void att_read_request(uint16_t request_type, uint16_t peripheral_handle, uint16_t attribute_handle){ 2673deb3ec6SMatthias Ringwald l2cap_reserve_packet_buffer(); 2683deb3ec6SMatthias Ringwald uint8_t * request = l2cap_get_outgoing_buffer(); 2693deb3ec6SMatthias Ringwald request[0] = request_type; 270f8fbdce0SMatthias Ringwald little_endian_store_16(request, 1, attribute_handle); 2713deb3ec6SMatthias Ringwald 2723deb3ec6SMatthias Ringwald l2cap_send_prepared_connectionless(peripheral_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, 3); 2733deb3ec6SMatthias Ringwald } 2743deb3ec6SMatthias Ringwald 2753deb3ec6SMatthias Ringwald // precondition: can_send_packet_now == TRUE 2763deb3ec6SMatthias Ringwald static void att_read_blob_request(uint16_t request_type, uint16_t peripheral_handle, uint16_t attribute_handle, uint16_t value_offset){ 2773deb3ec6SMatthias Ringwald l2cap_reserve_packet_buffer(); 2783deb3ec6SMatthias Ringwald uint8_t * request = l2cap_get_outgoing_buffer(); 2793deb3ec6SMatthias Ringwald request[0] = request_type; 280f8fbdce0SMatthias Ringwald little_endian_store_16(request, 1, attribute_handle); 281f8fbdce0SMatthias Ringwald little_endian_store_16(request, 3, value_offset); 2823deb3ec6SMatthias Ringwald 2833deb3ec6SMatthias Ringwald l2cap_send_prepared_connectionless(peripheral_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, 5); 2843deb3ec6SMatthias Ringwald } 2853deb3ec6SMatthias Ringwald 2863deb3ec6SMatthias Ringwald static void att_read_multiple_request(uint16_t peripheral_handle, uint16_t num_value_handles, uint16_t * value_handles){ 2873deb3ec6SMatthias Ringwald l2cap_reserve_packet_buffer(); 2883deb3ec6SMatthias Ringwald uint8_t * request = l2cap_get_outgoing_buffer(); 2893deb3ec6SMatthias Ringwald request[0] = ATT_READ_MULTIPLE_REQUEST; 2903deb3ec6SMatthias Ringwald int i; 2913deb3ec6SMatthias Ringwald int offset = 1; 2923deb3ec6SMatthias Ringwald for (i=0;i<num_value_handles;i++){ 293f8fbdce0SMatthias Ringwald little_endian_store_16(request, offset, value_handles[i]); 2943deb3ec6SMatthias Ringwald offset += 2; 2953deb3ec6SMatthias Ringwald } 2963deb3ec6SMatthias Ringwald l2cap_send_prepared_connectionless(peripheral_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, offset); 2973deb3ec6SMatthias Ringwald } 2983deb3ec6SMatthias Ringwald 2997a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE 3003deb3ec6SMatthias Ringwald // precondition: can_send_packet_now == TRUE 3013deb3ec6SMatthias Ringwald static void att_signed_write_request(uint16_t request_type, uint16_t peripheral_handle, uint16_t attribute_handle, uint16_t value_length, uint8_t * value, uint32_t sign_counter, uint8_t sgn[8]){ 3023deb3ec6SMatthias Ringwald l2cap_reserve_packet_buffer(); 3033deb3ec6SMatthias Ringwald uint8_t * request = l2cap_get_outgoing_buffer(); 3043deb3ec6SMatthias Ringwald request[0] = request_type; 305f8fbdce0SMatthias Ringwald little_endian_store_16(request, 1, attribute_handle); 3063deb3ec6SMatthias Ringwald memcpy(&request[3], value, value_length); 307f8fbdce0SMatthias Ringwald little_endian_store_32(request, 3 + value_length, sign_counter); 3089c80e4ccSMatthias Ringwald reverse_64(sgn, &request[3 + value_length + 4]); 3093deb3ec6SMatthias Ringwald l2cap_send_prepared_connectionless(peripheral_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, 3 + value_length + 12); 3103deb3ec6SMatthias Ringwald } 3117a766ebfSMatthias Ringwald #endif 3123deb3ec6SMatthias Ringwald 3133deb3ec6SMatthias Ringwald // precondition: can_send_packet_now == TRUE 3143deb3ec6SMatthias Ringwald static void att_write_request(uint16_t request_type, uint16_t peripheral_handle, uint16_t attribute_handle, uint16_t value_length, uint8_t * value){ 3153deb3ec6SMatthias Ringwald l2cap_reserve_packet_buffer(); 3163deb3ec6SMatthias Ringwald uint8_t * request = l2cap_get_outgoing_buffer(); 3173deb3ec6SMatthias Ringwald request[0] = request_type; 318f8fbdce0SMatthias Ringwald little_endian_store_16(request, 1, attribute_handle); 3193deb3ec6SMatthias Ringwald memcpy(&request[3], value, value_length); 3203deb3ec6SMatthias Ringwald 3213deb3ec6SMatthias Ringwald l2cap_send_prepared_connectionless(peripheral_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, 3 + value_length); 3223deb3ec6SMatthias Ringwald } 3233deb3ec6SMatthias Ringwald 3243deb3ec6SMatthias Ringwald // precondition: can_send_packet_now == TRUE 3253deb3ec6SMatthias Ringwald static void att_execute_write_request(uint16_t request_type, uint16_t peripheral_handle, uint8_t execute_write){ 3263deb3ec6SMatthias Ringwald l2cap_reserve_packet_buffer(); 3273deb3ec6SMatthias Ringwald uint8_t * request = l2cap_get_outgoing_buffer(); 3283deb3ec6SMatthias Ringwald request[0] = request_type; 3293deb3ec6SMatthias Ringwald request[1] = execute_write; 3303deb3ec6SMatthias Ringwald l2cap_send_prepared_connectionless(peripheral_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, 2); 3313deb3ec6SMatthias Ringwald } 3323deb3ec6SMatthias Ringwald 3333deb3ec6SMatthias Ringwald // precondition: can_send_packet_now == TRUE 3343deb3ec6SMatthias Ringwald static void att_prepare_write_request(uint16_t request_type, uint16_t peripheral_handle, uint16_t attribute_handle, uint16_t value_offset, uint16_t blob_length, uint8_t * value){ 3353deb3ec6SMatthias Ringwald l2cap_reserve_packet_buffer(); 3363deb3ec6SMatthias Ringwald uint8_t * request = l2cap_get_outgoing_buffer(); 3373deb3ec6SMatthias Ringwald request[0] = request_type; 338f8fbdce0SMatthias Ringwald little_endian_store_16(request, 1, attribute_handle); 339f8fbdce0SMatthias Ringwald little_endian_store_16(request, 3, value_offset); 3403deb3ec6SMatthias Ringwald memcpy(&request[5], &value[value_offset], blob_length); 3413deb3ec6SMatthias Ringwald 3423deb3ec6SMatthias Ringwald l2cap_send_prepared_connectionless(peripheral_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, 5+blob_length); 3433deb3ec6SMatthias Ringwald } 3443deb3ec6SMatthias Ringwald 3453deb3ec6SMatthias Ringwald static void att_exchange_mtu_request(uint16_t peripheral_handle){ 3463deb3ec6SMatthias Ringwald uint16_t mtu = l2cap_max_le_mtu(); 3473deb3ec6SMatthias Ringwald l2cap_reserve_packet_buffer(); 3483deb3ec6SMatthias Ringwald uint8_t * request = l2cap_get_outgoing_buffer(); 3493deb3ec6SMatthias Ringwald request[0] = ATT_EXCHANGE_MTU_REQUEST; 350f8fbdce0SMatthias Ringwald little_endian_store_16(request, 1, mtu); 3513deb3ec6SMatthias Ringwald l2cap_send_prepared_connectionless(peripheral_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, 3); 3523deb3ec6SMatthias Ringwald } 3533deb3ec6SMatthias Ringwald 3543deb3ec6SMatthias Ringwald static uint16_t write_blob_length(gatt_client_t * peripheral){ 3553deb3ec6SMatthias Ringwald uint16_t max_blob_length = peripheral_mtu(peripheral) - 5; 3563deb3ec6SMatthias Ringwald if (peripheral->attribute_offset >= peripheral->attribute_length) { 3573deb3ec6SMatthias Ringwald return 0; 3583deb3ec6SMatthias Ringwald } 3593deb3ec6SMatthias Ringwald uint16_t rest_length = peripheral->attribute_length - peripheral->attribute_offset; 3603deb3ec6SMatthias Ringwald if (max_blob_length > rest_length){ 3613deb3ec6SMatthias Ringwald return rest_length; 3623deb3ec6SMatthias Ringwald } 3633deb3ec6SMatthias Ringwald return max_blob_length; 3643deb3ec6SMatthias Ringwald } 3653deb3ec6SMatthias Ringwald 3663deb3ec6SMatthias Ringwald static void send_gatt_services_request(gatt_client_t *peripheral){ 367fc64f94aSMatthias Ringwald att_read_by_type_or_group_request_for_uuid16(ATT_READ_BY_GROUP_TYPE_REQUEST, GATT_PRIMARY_SERVICE_UUID, peripheral->con_handle, peripheral->start_group_handle, peripheral->end_group_handle); 3683deb3ec6SMatthias Ringwald } 3693deb3ec6SMatthias Ringwald 3703deb3ec6SMatthias Ringwald static void send_gatt_by_uuid_request(gatt_client_t *peripheral, uint16_t attribute_group_type){ 3713deb3ec6SMatthias Ringwald if (peripheral->uuid16){ 3723deb3ec6SMatthias Ringwald uint8_t uuid16[2]; 373f8fbdce0SMatthias Ringwald little_endian_store_16(uuid16, 0, peripheral->uuid16); 374fc64f94aSMatthias Ringwald att_find_by_type_value_request(ATT_FIND_BY_TYPE_VALUE_REQUEST, attribute_group_type, peripheral->con_handle, peripheral->start_group_handle, peripheral->end_group_handle, uuid16, 2); 3753deb3ec6SMatthias Ringwald return; 3763deb3ec6SMatthias Ringwald } 3773deb3ec6SMatthias Ringwald uint8_t uuid128[16]; 3789c80e4ccSMatthias Ringwald reverse_128(peripheral->uuid128, uuid128); 379fc64f94aSMatthias Ringwald att_find_by_type_value_request(ATT_FIND_BY_TYPE_VALUE_REQUEST, attribute_group_type, peripheral->con_handle, peripheral->start_group_handle, peripheral->end_group_handle, uuid128, 16); 3803deb3ec6SMatthias Ringwald } 3813deb3ec6SMatthias Ringwald 3823deb3ec6SMatthias Ringwald static void send_gatt_services_by_uuid_request(gatt_client_t *peripheral){ 3833deb3ec6SMatthias Ringwald send_gatt_by_uuid_request(peripheral, GATT_PRIMARY_SERVICE_UUID); 3843deb3ec6SMatthias Ringwald } 3853deb3ec6SMatthias Ringwald 3863deb3ec6SMatthias Ringwald static void send_gatt_included_service_uuid_request(gatt_client_t *peripheral){ 387fc64f94aSMatthias Ringwald att_read_request(ATT_READ_REQUEST, peripheral->con_handle, peripheral->query_start_handle); 3883deb3ec6SMatthias Ringwald } 3893deb3ec6SMatthias Ringwald 3903deb3ec6SMatthias Ringwald static void send_gatt_included_service_request(gatt_client_t *peripheral){ 391fc64f94aSMatthias Ringwald att_read_by_type_or_group_request_for_uuid16(ATT_READ_BY_TYPE_REQUEST, GATT_INCLUDE_SERVICE_UUID, peripheral->con_handle, peripheral->start_group_handle, peripheral->end_group_handle); 3923deb3ec6SMatthias Ringwald } 3933deb3ec6SMatthias Ringwald 3943deb3ec6SMatthias Ringwald static void send_gatt_characteristic_request(gatt_client_t *peripheral){ 395fc64f94aSMatthias Ringwald att_read_by_type_or_group_request_for_uuid16(ATT_READ_BY_TYPE_REQUEST, GATT_CHARACTERISTICS_UUID, peripheral->con_handle, peripheral->start_group_handle, peripheral->end_group_handle); 3963deb3ec6SMatthias Ringwald } 3973deb3ec6SMatthias Ringwald 3983deb3ec6SMatthias Ringwald static void send_gatt_characteristic_descriptor_request(gatt_client_t *peripheral){ 399fc64f94aSMatthias Ringwald att_find_information_request(ATT_FIND_INFORMATION_REQUEST, peripheral->con_handle, peripheral->start_group_handle, peripheral->end_group_handle); 4003deb3ec6SMatthias Ringwald } 4013deb3ec6SMatthias Ringwald 4023deb3ec6SMatthias Ringwald static void send_gatt_read_characteristic_value_request(gatt_client_t *peripheral){ 403fc64f94aSMatthias Ringwald att_read_request(ATT_READ_REQUEST, peripheral->con_handle, peripheral->attribute_handle); 4043deb3ec6SMatthias Ringwald } 4053deb3ec6SMatthias Ringwald 4063deb3ec6SMatthias Ringwald static void send_gatt_read_by_type_request(gatt_client_t * peripheral){ 4073deb3ec6SMatthias Ringwald if (peripheral->uuid16){ 408fc64f94aSMatthias Ringwald att_read_by_type_or_group_request_for_uuid16(ATT_READ_BY_TYPE_REQUEST, peripheral->uuid16, peripheral->con_handle, peripheral->start_group_handle, peripheral->end_group_handle); 4093deb3ec6SMatthias Ringwald } else { 410fc64f94aSMatthias Ringwald att_read_by_type_or_group_request_for_uuid128(ATT_READ_BY_TYPE_REQUEST, peripheral->uuid128, peripheral->con_handle, peripheral->start_group_handle, peripheral->end_group_handle); 4113deb3ec6SMatthias Ringwald } 4123deb3ec6SMatthias Ringwald } 4133deb3ec6SMatthias Ringwald 4143deb3ec6SMatthias Ringwald static void send_gatt_read_blob_request(gatt_client_t *peripheral){ 415fc64f94aSMatthias Ringwald att_read_blob_request(ATT_READ_BLOB_REQUEST, peripheral->con_handle, peripheral->attribute_handle, peripheral->attribute_offset); 4163deb3ec6SMatthias Ringwald } 4173deb3ec6SMatthias Ringwald 4183deb3ec6SMatthias Ringwald static void send_gatt_read_multiple_request(gatt_client_t * peripheral){ 419fc64f94aSMatthias Ringwald att_read_multiple_request(peripheral->con_handle, peripheral->read_multiple_handle_count, peripheral->read_multiple_handles); 4203deb3ec6SMatthias Ringwald } 4213deb3ec6SMatthias Ringwald 4223deb3ec6SMatthias Ringwald static void send_gatt_write_attribute_value_request(gatt_client_t * peripheral){ 423fc64f94aSMatthias Ringwald att_write_request(ATT_WRITE_REQUEST, peripheral->con_handle, peripheral->attribute_handle, peripheral->attribute_length, peripheral->attribute_value); 4243deb3ec6SMatthias Ringwald } 4253deb3ec6SMatthias Ringwald 4263deb3ec6SMatthias Ringwald static void send_gatt_write_client_characteristic_configuration_request(gatt_client_t * peripheral){ 427fc64f94aSMatthias Ringwald att_write_request(ATT_WRITE_REQUEST, peripheral->con_handle, peripheral->client_characteristic_configuration_handle, 2, peripheral->client_characteristic_configuration_value); 4283deb3ec6SMatthias Ringwald } 4293deb3ec6SMatthias Ringwald 4303deb3ec6SMatthias Ringwald static void send_gatt_prepare_write_request(gatt_client_t * peripheral){ 431fc64f94aSMatthias Ringwald att_prepare_write_request(ATT_PREPARE_WRITE_REQUEST, peripheral->con_handle, peripheral->attribute_handle, peripheral->attribute_offset, write_blob_length(peripheral), peripheral->attribute_value); 4323deb3ec6SMatthias Ringwald } 4333deb3ec6SMatthias Ringwald 4343deb3ec6SMatthias Ringwald static void send_gatt_execute_write_request(gatt_client_t * peripheral){ 435fc64f94aSMatthias Ringwald att_execute_write_request(ATT_EXECUTE_WRITE_REQUEST, peripheral->con_handle, 1); 4363deb3ec6SMatthias Ringwald } 4373deb3ec6SMatthias Ringwald 4383deb3ec6SMatthias Ringwald static void send_gatt_cancel_prepared_write_request(gatt_client_t * peripheral){ 439fc64f94aSMatthias Ringwald att_execute_write_request(ATT_EXECUTE_WRITE_REQUEST, peripheral->con_handle, 0); 4403deb3ec6SMatthias Ringwald } 4413deb3ec6SMatthias Ringwald 442abdc9fb5SMatthias Ringwald #ifndef ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERY 4433deb3ec6SMatthias Ringwald static void send_gatt_read_client_characteristic_configuration_request(gatt_client_t * peripheral){ 444fc64f94aSMatthias Ringwald att_read_by_type_or_group_request_for_uuid16(ATT_READ_BY_TYPE_REQUEST, GATT_CLIENT_CHARACTERISTICS_CONFIGURATION, peripheral->con_handle, peripheral->start_group_handle, peripheral->end_group_handle); 4453deb3ec6SMatthias Ringwald } 446abdc9fb5SMatthias Ringwald #endif 4473deb3ec6SMatthias Ringwald 4483deb3ec6SMatthias Ringwald static void send_gatt_read_characteristic_descriptor_request(gatt_client_t * peripheral){ 449fc64f94aSMatthias Ringwald att_read_request(ATT_READ_REQUEST, peripheral->con_handle, peripheral->attribute_handle); 4503deb3ec6SMatthias Ringwald } 4513deb3ec6SMatthias Ringwald 4527a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE 4533deb3ec6SMatthias Ringwald static void send_gatt_signed_write_request(gatt_client_t * peripheral, uint32_t sign_counter){ 454fc64f94aSMatthias Ringwald att_signed_write_request(ATT_SIGNED_WRITE_COMMAND, peripheral->con_handle, peripheral->attribute_handle, peripheral->attribute_length, peripheral->attribute_value, sign_counter, peripheral->cmac); 4553deb3ec6SMatthias Ringwald } 4567a766ebfSMatthias Ringwald #endif 4573deb3ec6SMatthias Ringwald 4583deb3ec6SMatthias Ringwald static uint16_t get_last_result_handle_from_service_list(uint8_t * packet, uint16_t size){ 4593deb3ec6SMatthias Ringwald uint8_t attr_length = packet[1]; 460f8fbdce0SMatthias Ringwald return little_endian_read_16(packet, size - attr_length + 2); 4613deb3ec6SMatthias Ringwald } 4623deb3ec6SMatthias Ringwald 4633deb3ec6SMatthias Ringwald static uint16_t get_last_result_handle_from_characteristics_list(uint8_t * packet, uint16_t size){ 4643deb3ec6SMatthias Ringwald uint8_t attr_length = packet[1]; 465f8fbdce0SMatthias Ringwald return little_endian_read_16(packet, size - attr_length + 3); 4663deb3ec6SMatthias Ringwald } 4673deb3ec6SMatthias Ringwald 4683deb3ec6SMatthias Ringwald static uint16_t get_last_result_handle_from_included_services_list(uint8_t * packet, uint16_t size){ 4693deb3ec6SMatthias Ringwald uint8_t attr_length = packet[1]; 470f8fbdce0SMatthias Ringwald return little_endian_read_16(packet, size - attr_length); 4713deb3ec6SMatthias Ringwald } 4723deb3ec6SMatthias Ringwald 4733deb3ec6SMatthias Ringwald static void gatt_client_handle_transaction_complete(gatt_client_t * peripheral){ 4743deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_READY; 4753deb3ec6SMatthias Ringwald gatt_client_timeout_stop(peripheral); 4763deb3ec6SMatthias Ringwald } 4773deb3ec6SMatthias Ringwald 4789c662c9bSMatthias Ringwald static void emit_event_new(btstack_packet_handler_t callback, uint8_t * packet, uint16_t size){ 4799c662c9bSMatthias Ringwald if (!callback) return; 480b6e003bcSMatthias Ringwald hci_dump_packet(HCI_EVENT_PACKET, 0, packet, size); 4819c662c9bSMatthias Ringwald (*callback)(HCI_EVENT_PACKET, 0, packet, size); 4823deb3ec6SMatthias Ringwald } 4833deb3ec6SMatthias Ringwald 48486c38559SMatthias Ringwald 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){ 48586c38559SMatthias Ringwald notification->callback = packet_handler; 4869c662c9bSMatthias Ringwald notification->con_handle = con_handle; 4879c662c9bSMatthias Ringwald notification->attribute_handle = characteristic->value_handle; 4889c662c9bSMatthias Ringwald btstack_linked_list_add(&gatt_client_value_listeners, (btstack_linked_item_t*) notification); 4899c662c9bSMatthias Ringwald } 4909c662c9bSMatthias Ringwald 4911f734b5fSMatthias Ringwald void gatt_client_stop_listening_for_characteristic_value_updates(gatt_client_notification_t * notification){ 4921f734b5fSMatthias Ringwald btstack_linked_list_remove(&gatt_client_value_listeners, (btstack_linked_item_t*) notification); 4931f734b5fSMatthias Ringwald } 4941f734b5fSMatthias Ringwald 495711e6c80SMatthias Ringwald static void emit_event_to_registered_listeners(hci_con_handle_t con_handle, uint16_t attribute_handle, uint8_t * packet, uint16_t size){ 496665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 4979c662c9bSMatthias Ringwald btstack_linked_list_iterator_init(&it, &gatt_client_value_listeners); 498665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 49986c38559SMatthias Ringwald gatt_client_notification_t * notification = (gatt_client_notification_t*) btstack_linked_list_iterator_next(&it); 50086c38559SMatthias Ringwald if (notification->con_handle != con_handle) continue; 50186c38559SMatthias Ringwald if (notification->attribute_handle != attribute_handle) continue; 50286c38559SMatthias Ringwald (*notification->callback)(HCI_EVENT_PACKET, 0, packet, size); 5033deb3ec6SMatthias Ringwald } 5043deb3ec6SMatthias Ringwald } 5053deb3ec6SMatthias Ringwald 5063deb3ec6SMatthias Ringwald static void emit_gatt_complete_event(gatt_client_t * peripheral, uint8_t status){ 5073deb3ec6SMatthias Ringwald // @format H1 5083deb3ec6SMatthias Ringwald uint8_t packet[5]; 5095611a760SMatthias Ringwald packet[0] = GATT_EVENT_QUERY_COMPLETE; 5103deb3ec6SMatthias Ringwald packet[1] = 3; 511fc64f94aSMatthias Ringwald little_endian_store_16(packet, 2, peripheral->con_handle); 5123deb3ec6SMatthias Ringwald packet[4] = status; 5139c662c9bSMatthias Ringwald emit_event_new(peripheral->callback, packet, sizeof(packet)); 5143deb3ec6SMatthias Ringwald } 5153deb3ec6SMatthias Ringwald 5163deb3ec6SMatthias Ringwald static void emit_gatt_service_query_result_event(gatt_client_t * peripheral, uint16_t start_group_handle, uint16_t end_group_handle, uint8_t * uuid128){ 5173deb3ec6SMatthias Ringwald // @format HX 5183deb3ec6SMatthias Ringwald uint8_t packet[24]; 5195611a760SMatthias Ringwald packet[0] = GATT_EVENT_SERVICE_QUERY_RESULT; 5203deb3ec6SMatthias Ringwald packet[1] = sizeof(packet) - 2; 521fc64f94aSMatthias Ringwald little_endian_store_16(packet, 2, peripheral->con_handle); 5223deb3ec6SMatthias Ringwald /// 523f8fbdce0SMatthias Ringwald little_endian_store_16(packet, 4, start_group_handle); 524f8fbdce0SMatthias Ringwald little_endian_store_16(packet, 6, end_group_handle); 5259c80e4ccSMatthias Ringwald reverse_128(uuid128, &packet[8]); 5269c662c9bSMatthias Ringwald emit_event_new(peripheral->callback, packet, sizeof(packet)); 5273deb3ec6SMatthias Ringwald } 5283deb3ec6SMatthias Ringwald 5293deb3ec6SMatthias Ringwald static void emit_gatt_included_service_query_result_event(gatt_client_t * peripheral, uint16_t include_handle, uint16_t start_group_handle, uint16_t end_group_handle, uint8_t * uuid128){ 5303deb3ec6SMatthias Ringwald // @format HX 5313deb3ec6SMatthias Ringwald uint8_t packet[26]; 5325611a760SMatthias Ringwald packet[0] = GATT_EVENT_INCLUDED_SERVICE_QUERY_RESULT; 5333deb3ec6SMatthias Ringwald packet[1] = sizeof(packet) - 2; 534fc64f94aSMatthias Ringwald little_endian_store_16(packet, 2, peripheral->con_handle); 5353deb3ec6SMatthias Ringwald /// 536f8fbdce0SMatthias Ringwald little_endian_store_16(packet, 4, include_handle); 5373deb3ec6SMatthias Ringwald // 538f8fbdce0SMatthias Ringwald little_endian_store_16(packet, 6, start_group_handle); 539f8fbdce0SMatthias Ringwald little_endian_store_16(packet, 8, end_group_handle); 5409c80e4ccSMatthias Ringwald reverse_128(uuid128, &packet[10]); 5419c662c9bSMatthias Ringwald emit_event_new(peripheral->callback, packet, sizeof(packet)); 5423deb3ec6SMatthias Ringwald } 5433deb3ec6SMatthias Ringwald 5443deb3ec6SMatthias Ringwald static void emit_gatt_characteristic_query_result_event(gatt_client_t * peripheral, uint16_t start_handle, uint16_t value_handle, uint16_t end_handle, 5453deb3ec6SMatthias Ringwald uint16_t properties, uint8_t * uuid128){ 5463deb3ec6SMatthias Ringwald // @format HY 5473deb3ec6SMatthias Ringwald uint8_t packet[28]; 5485611a760SMatthias Ringwald packet[0] = GATT_EVENT_CHARACTERISTIC_QUERY_RESULT; 5493deb3ec6SMatthias Ringwald packet[1] = sizeof(packet) - 2; 550fc64f94aSMatthias Ringwald little_endian_store_16(packet, 2, peripheral->con_handle); 5513deb3ec6SMatthias Ringwald /// 552f8fbdce0SMatthias Ringwald little_endian_store_16(packet, 4, start_handle); 553f8fbdce0SMatthias Ringwald little_endian_store_16(packet, 6, value_handle); 554f8fbdce0SMatthias Ringwald little_endian_store_16(packet, 8, end_handle); 555f8fbdce0SMatthias Ringwald little_endian_store_16(packet, 10, properties); 5569c80e4ccSMatthias Ringwald reverse_128(uuid128, &packet[12]); 5579c662c9bSMatthias Ringwald emit_event_new(peripheral->callback, packet, sizeof(packet)); 5583deb3ec6SMatthias Ringwald } 5593deb3ec6SMatthias Ringwald 5603deb3ec6SMatthias Ringwald static void emit_gatt_all_characteristic_descriptors_result_event( 5613deb3ec6SMatthias Ringwald gatt_client_t * peripheral, uint16_t descriptor_handle, uint8_t * uuid128){ 5623deb3ec6SMatthias Ringwald // @format HZ 5633deb3ec6SMatthias Ringwald uint8_t packet[22]; 5645611a760SMatthias Ringwald packet[0] = GATT_EVENT_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY_RESULT; 5653deb3ec6SMatthias Ringwald packet[1] = sizeof(packet) - 2; 566fc64f94aSMatthias Ringwald little_endian_store_16(packet, 2, peripheral->con_handle); 5673deb3ec6SMatthias Ringwald /// 568f8fbdce0SMatthias Ringwald little_endian_store_16(packet, 4, descriptor_handle); 5699c80e4ccSMatthias Ringwald reverse_128(uuid128, &packet[6]); 5709c662c9bSMatthias Ringwald emit_event_new(peripheral->callback, packet, sizeof(packet)); 5713deb3ec6SMatthias Ringwald } 5723deb3ec6SMatthias Ringwald 5738f37572aSJakob Krantz static void emit_gatt_mtu_exchanged_result_event(gatt_client_t * peripheral, uint16_t new_mtu){ 5748f37572aSJakob Krantz // @format H2 5758f37572aSJakob Krantz uint8_t packet[6]; 5768f37572aSJakob Krantz packet[0] = GATT_EVENT_MTU; 5778f37572aSJakob Krantz packet[1] = sizeof(packet) - 2; 5788f37572aSJakob Krantz little_endian_store_16(packet, 2, peripheral->con_handle); 5798f37572aSJakob Krantz little_endian_store_16(packet, 4, new_mtu); 580b12646c5SJakob Krantz att_dispatch_client_mtu_exchanged(peripheral->con_handle, new_mtu); 5818f37572aSJakob Krantz emit_event_new(peripheral->callback, packet, sizeof(packet)); 5828f37572aSJakob Krantz } 5838f37572aSJakob Krantz /// 5843deb3ec6SMatthias Ringwald static void report_gatt_services(gatt_client_t * peripheral, uint8_t * packet, uint16_t size){ 5853deb3ec6SMatthias Ringwald uint8_t attr_length = packet[1]; 5863deb3ec6SMatthias Ringwald uint8_t uuid_length = attr_length - 4; 5873deb3ec6SMatthias Ringwald 5883deb3ec6SMatthias Ringwald int i; 5893deb3ec6SMatthias Ringwald for (i = 2; i < size; i += attr_length){ 590f8fbdce0SMatthias Ringwald uint16_t start_group_handle = little_endian_read_16(packet,i); 591f8fbdce0SMatthias Ringwald uint16_t end_group_handle = little_endian_read_16(packet,i+2); 5923deb3ec6SMatthias Ringwald uint8_t uuid128[16]; 5933deb3ec6SMatthias Ringwald uint16_t uuid16 = 0; 5943deb3ec6SMatthias Ringwald 5953deb3ec6SMatthias Ringwald if (uuid_length == 2){ 596f8fbdce0SMatthias Ringwald uuid16 = little_endian_read_16(packet, i+4); 597e1a125dfSMatthias Ringwald uuid_add_bluetooth_prefix((uint8_t*) &uuid128, uuid16); 5983deb3ec6SMatthias Ringwald } else { 5999c80e4ccSMatthias Ringwald reverse_128(&packet[i+4], uuid128); 6003deb3ec6SMatthias Ringwald } 6013deb3ec6SMatthias Ringwald emit_gatt_service_query_result_event(peripheral, start_group_handle, end_group_handle, uuid128); 6023deb3ec6SMatthias Ringwald } 603fc64f94aSMatthias Ringwald // log_info("report_gatt_services for %02X done", peripheral->con_handle); 6043deb3ec6SMatthias Ringwald } 6053deb3ec6SMatthias Ringwald 6063deb3ec6SMatthias Ringwald // helper 6073deb3ec6SMatthias Ringwald static void characteristic_start_found(gatt_client_t * peripheral, uint16_t start_handle, uint8_t properties, uint16_t value_handle, uint8_t * uuid, uint16_t uuid_length){ 6083deb3ec6SMatthias Ringwald uint8_t uuid128[16]; 6093deb3ec6SMatthias Ringwald uint16_t uuid16 = 0; 6103deb3ec6SMatthias Ringwald if (uuid_length == 2){ 611f8fbdce0SMatthias Ringwald uuid16 = little_endian_read_16(uuid, 0); 612e1a125dfSMatthias Ringwald uuid_add_bluetooth_prefix((uint8_t*) uuid128, uuid16); 6133deb3ec6SMatthias Ringwald } else { 6149c80e4ccSMatthias Ringwald reverse_128(uuid, uuid128); 6153deb3ec6SMatthias Ringwald } 6163deb3ec6SMatthias Ringwald 6173deb3ec6SMatthias Ringwald if (peripheral->filter_with_uuid && memcmp(peripheral->uuid128, uuid128, 16) != 0) return; 6183deb3ec6SMatthias Ringwald 6193deb3ec6SMatthias Ringwald peripheral->characteristic_properties = properties; 6203deb3ec6SMatthias Ringwald peripheral->characteristic_start_handle = start_handle; 6213deb3ec6SMatthias Ringwald peripheral->attribute_handle = value_handle; 6223deb3ec6SMatthias Ringwald 6233deb3ec6SMatthias Ringwald if (peripheral->filter_with_uuid) return; 6243deb3ec6SMatthias Ringwald 6253deb3ec6SMatthias Ringwald peripheral->uuid16 = uuid16; 6263deb3ec6SMatthias Ringwald memcpy(peripheral->uuid128, uuid128, 16); 6273deb3ec6SMatthias Ringwald } 6283deb3ec6SMatthias Ringwald 6293deb3ec6SMatthias Ringwald static void characteristic_end_found(gatt_client_t * peripheral, uint16_t end_handle){ 6303deb3ec6SMatthias Ringwald // TODO: stop searching if filter and uuid found 6313deb3ec6SMatthias Ringwald 6323deb3ec6SMatthias Ringwald if (!peripheral->characteristic_start_handle) return; 6333deb3ec6SMatthias Ringwald 6343deb3ec6SMatthias Ringwald emit_gatt_characteristic_query_result_event(peripheral, peripheral->characteristic_start_handle, peripheral->attribute_handle, 6353deb3ec6SMatthias Ringwald end_handle, peripheral->characteristic_properties, peripheral->uuid128); 6363deb3ec6SMatthias Ringwald 6373deb3ec6SMatthias Ringwald peripheral->characteristic_start_handle = 0; 6383deb3ec6SMatthias Ringwald } 6393deb3ec6SMatthias Ringwald 6403deb3ec6SMatthias Ringwald static void report_gatt_characteristics(gatt_client_t * peripheral, uint8_t * packet, uint16_t size){ 6413deb3ec6SMatthias Ringwald uint8_t attr_length = packet[1]; 6423deb3ec6SMatthias Ringwald uint8_t uuid_length = attr_length - 5; 6433deb3ec6SMatthias Ringwald int i; 6443deb3ec6SMatthias Ringwald for (i = 2; i < size; i += attr_length){ 645f8fbdce0SMatthias Ringwald uint16_t start_handle = little_endian_read_16(packet, i); 6463deb3ec6SMatthias Ringwald uint8_t properties = packet[i+2]; 647f8fbdce0SMatthias Ringwald uint16_t value_handle = little_endian_read_16(packet, i+3); 6483deb3ec6SMatthias Ringwald characteristic_end_found(peripheral, start_handle-1); 6493deb3ec6SMatthias Ringwald characteristic_start_found(peripheral, start_handle, properties, value_handle, &packet[i+5], uuid_length); 6503deb3ec6SMatthias Ringwald } 6513deb3ec6SMatthias Ringwald } 6523deb3ec6SMatthias Ringwald 6533deb3ec6SMatthias Ringwald static void report_gatt_included_service_uuid16(gatt_client_t * peripheral, uint16_t include_handle, uint16_t uuid16){ 6543deb3ec6SMatthias Ringwald uint8_t normalized_uuid128[16]; 655e1a125dfSMatthias Ringwald uuid_add_bluetooth_prefix(normalized_uuid128, uuid16); 6563deb3ec6SMatthias Ringwald emit_gatt_included_service_query_result_event(peripheral, include_handle, peripheral->query_start_handle, 6573deb3ec6SMatthias Ringwald peripheral->query_end_handle, normalized_uuid128); 6583deb3ec6SMatthias Ringwald } 6593deb3ec6SMatthias Ringwald 6603deb3ec6SMatthias Ringwald static void report_gatt_included_service_uuid128(gatt_client_t * peripheral, uint16_t include_handle, uint8_t *uuid128){ 6613deb3ec6SMatthias Ringwald emit_gatt_included_service_query_result_event(peripheral, include_handle, peripheral->query_start_handle, 6623deb3ec6SMatthias Ringwald peripheral->query_end_handle, uuid128); 6633deb3ec6SMatthias Ringwald } 6643deb3ec6SMatthias Ringwald 6653deb3ec6SMatthias Ringwald // @returns packet pointer 6663deb3ec6SMatthias Ringwald // @note assume that value is part of an l2cap buffer - overwrite HCI + L2CAP packet headers 6673deb3ec6SMatthias Ringwald static const int characteristic_value_event_header_size = 8; 668711e6c80SMatthias Ringwald 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){ 6693deb3ec6SMatthias Ringwald // before the value inside the ATT PDU 6703deb3ec6SMatthias Ringwald uint8_t * packet = value - characteristic_value_event_header_size; 6713deb3ec6SMatthias Ringwald packet[0] = type; 6723deb3ec6SMatthias Ringwald packet[1] = characteristic_value_event_header_size - 2 + length; 673f8fbdce0SMatthias Ringwald little_endian_store_16(packet, 2, con_handle); 674f8fbdce0SMatthias Ringwald little_endian_store_16(packet, 4, attribute_handle); 675f8fbdce0SMatthias Ringwald little_endian_store_16(packet, 6, length); 6763deb3ec6SMatthias Ringwald return packet; 6773deb3ec6SMatthias Ringwald } 6783deb3ec6SMatthias Ringwald 6793deb3ec6SMatthias Ringwald // @returns packet pointer 6803deb3ec6SMatthias Ringwald // @note assume that value is part of an l2cap buffer - overwrite parts of the HCI/L2CAP/ATT packet (4/4/3) bytes 6813deb3ec6SMatthias Ringwald static const int long_characteristic_value_event_header_size = 10; 682711e6c80SMatthias Ringwald 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){ 6833deb3ec6SMatthias Ringwald #if defined(HCI_INCOMING_PRE_BUFFER_SIZE) && (HCI_INCOMING_PRE_BUFFER_SIZE >= 10 - 8) // L2CAP Header (4) - ACL Header (4) 6843deb3ec6SMatthias Ringwald // before the value inside the ATT PDU 6853deb3ec6SMatthias Ringwald uint8_t * packet = value - long_characteristic_value_event_header_size; 6863deb3ec6SMatthias Ringwald packet[0] = type; 6873deb3ec6SMatthias Ringwald packet[1] = long_characteristic_value_event_header_size - 2 + length; 688f8fbdce0SMatthias Ringwald little_endian_store_16(packet, 2, con_handle); 689f8fbdce0SMatthias Ringwald little_endian_store_16(packet, 4, attribute_handle); 690f8fbdce0SMatthias Ringwald little_endian_store_16(packet, 6, offset); 691f8fbdce0SMatthias Ringwald little_endian_store_16(packet, 8, length); 6923deb3ec6SMatthias Ringwald return packet; 6933deb3ec6SMatthias Ringwald #else 6943deb3ec6SMatthias Ringwald log_error("HCI_INCOMING_PRE_BUFFER_SIZE >= 2 required for long characteristic reads"); 6953deb3ec6SMatthias Ringwald return NULL; 6963deb3ec6SMatthias Ringwald #endif 6973deb3ec6SMatthias Ringwald } 6983deb3ec6SMatthias Ringwald 6993deb3ec6SMatthias Ringwald 7003deb3ec6SMatthias Ringwald // @note assume that value is part of an l2cap buffer - overwrite parts of the HCI/L2CAP/ATT packet (4/4/3) bytes 701711e6c80SMatthias Ringwald static void report_gatt_notification(hci_con_handle_t con_handle, uint16_t value_handle, uint8_t * value, int length){ 7025611a760SMatthias Ringwald uint8_t * packet = setup_characteristic_value_packet(GATT_EVENT_NOTIFICATION, con_handle, value_handle, value, length); 7039c662c9bSMatthias Ringwald emit_event_to_registered_listeners(con_handle, value_handle, packet, characteristic_value_event_header_size + length); 7043deb3ec6SMatthias Ringwald } 7053deb3ec6SMatthias Ringwald 7063deb3ec6SMatthias Ringwald // @note assume that value is part of an l2cap buffer - overwrite parts of the HCI/L2CAP/ATT packet (4/4/3) bytes 707711e6c80SMatthias Ringwald static void report_gatt_indication(hci_con_handle_t con_handle, uint16_t value_handle, uint8_t * value, int length){ 7085611a760SMatthias Ringwald uint8_t * packet = setup_characteristic_value_packet(GATT_EVENT_INDICATION, con_handle, value_handle, value, length); 7099c662c9bSMatthias Ringwald emit_event_to_registered_listeners(con_handle, value_handle, packet, characteristic_value_event_header_size + length); 7103deb3ec6SMatthias Ringwald } 7113deb3ec6SMatthias Ringwald 7123deb3ec6SMatthias Ringwald // @note assume that value is part of an l2cap buffer - overwrite parts of the HCI/L2CAP/ATT packet (4/4/3) bytes 7133deb3ec6SMatthias Ringwald static void report_gatt_characteristic_value(gatt_client_t * peripheral, uint16_t attribute_handle, uint8_t * value, uint16_t length){ 714fc64f94aSMatthias Ringwald uint8_t * packet = setup_characteristic_value_packet(GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT, peripheral->con_handle, attribute_handle, value, length); 7159c662c9bSMatthias Ringwald emit_event_new(peripheral->callback, packet, characteristic_value_event_header_size + length); 7163deb3ec6SMatthias Ringwald } 7173deb3ec6SMatthias Ringwald 7183deb3ec6SMatthias Ringwald // @note assume that value is part of an l2cap buffer - overwrite parts of the HCI/L2CAP/ATT packet (4/4/3) bytes 7193deb3ec6SMatthias Ringwald static void report_gatt_long_characteristic_value_blob(gatt_client_t * peripheral, uint16_t attribute_handle, uint8_t * blob, uint16_t blob_length, int value_offset){ 720fc64f94aSMatthias Ringwald uint8_t * packet = setup_long_characteristic_value_packet(GATT_EVENT_LONG_CHARACTERISTIC_VALUE_QUERY_RESULT, peripheral->con_handle, attribute_handle, value_offset, blob, blob_length); 7213deb3ec6SMatthias Ringwald if (!packet) return; 7229c662c9bSMatthias Ringwald emit_event_new(peripheral->callback, packet, blob_length + long_characteristic_value_event_header_size); 7233deb3ec6SMatthias Ringwald } 7243deb3ec6SMatthias Ringwald 7253deb3ec6SMatthias Ringwald static void report_gatt_characteristic_descriptor(gatt_client_t * peripheral, uint16_t descriptor_handle, uint8_t *value, uint16_t value_length, uint16_t value_offset){ 7269ec2630cSMatthias Ringwald UNUSED(value_offset); 727fc64f94aSMatthias Ringwald uint8_t * packet = setup_characteristic_value_packet(GATT_EVENT_CHARACTERISTIC_DESCRIPTOR_QUERY_RESULT, peripheral->con_handle, descriptor_handle, value, value_length); 7289c662c9bSMatthias Ringwald emit_event_new(peripheral->callback, packet, value_length + 8); 7293deb3ec6SMatthias Ringwald } 7303deb3ec6SMatthias Ringwald 7313deb3ec6SMatthias Ringwald static void report_gatt_long_characteristic_descriptor(gatt_client_t * peripheral, uint16_t descriptor_handle, uint8_t *blob, uint16_t blob_length, uint16_t value_offset){ 732fc64f94aSMatthias Ringwald uint8_t * packet = setup_long_characteristic_value_packet(GATT_EVENT_LONG_CHARACTERISTIC_DESCRIPTOR_QUERY_RESULT, peripheral->con_handle, descriptor_handle, value_offset, blob, blob_length); 7333deb3ec6SMatthias Ringwald if (!packet) return; 7349c662c9bSMatthias Ringwald emit_event_new(peripheral->callback, packet, blob_length + long_characteristic_value_event_header_size); 7353deb3ec6SMatthias Ringwald } 7363deb3ec6SMatthias Ringwald 7373deb3ec6SMatthias Ringwald static void report_gatt_all_characteristic_descriptors(gatt_client_t * peripheral, uint8_t * packet, uint16_t size, uint16_t pair_size){ 7383deb3ec6SMatthias Ringwald int i; 7393deb3ec6SMatthias Ringwald for (i = 0; i<size; i+=pair_size){ 740f8fbdce0SMatthias Ringwald uint16_t descriptor_handle = little_endian_read_16(packet,i); 7413deb3ec6SMatthias Ringwald uint8_t uuid128[16]; 7423deb3ec6SMatthias Ringwald uint16_t uuid16 = 0; 7433deb3ec6SMatthias Ringwald if (pair_size == 4){ 744f8fbdce0SMatthias Ringwald uuid16 = little_endian_read_16(packet,i+2); 745e1a125dfSMatthias Ringwald uuid_add_bluetooth_prefix(uuid128, uuid16); 7463deb3ec6SMatthias Ringwald } else { 7479c80e4ccSMatthias Ringwald reverse_128(&packet[i+2], uuid128); 7483deb3ec6SMatthias Ringwald } 7493deb3ec6SMatthias Ringwald emit_gatt_all_characteristic_descriptors_result_event(peripheral, descriptor_handle, uuid128); 7503deb3ec6SMatthias Ringwald } 7513deb3ec6SMatthias Ringwald 7523deb3ec6SMatthias Ringwald } 7533deb3ec6SMatthias Ringwald 7543deb3ec6SMatthias Ringwald static int is_query_done(gatt_client_t * peripheral, uint16_t last_result_handle){ 7553deb3ec6SMatthias Ringwald return last_result_handle >= peripheral->end_group_handle; 7563deb3ec6SMatthias Ringwald } 7573deb3ec6SMatthias Ringwald 7583deb3ec6SMatthias Ringwald static void trigger_next_query(gatt_client_t * peripheral, uint16_t last_result_handle, gatt_client_state_t next_query_state){ 7593deb3ec6SMatthias Ringwald if (is_query_done(peripheral, last_result_handle)){ 7603deb3ec6SMatthias Ringwald gatt_client_handle_transaction_complete(peripheral); 761*9cb80b17SMilanka Ringwald emit_gatt_complete_event(peripheral, ATT_ERROR_SUCCESS); 7623deb3ec6SMatthias Ringwald return; 7633deb3ec6SMatthias Ringwald } 7643deb3ec6SMatthias Ringwald // next 7653deb3ec6SMatthias Ringwald peripheral->start_group_handle = last_result_handle + 1; 7663deb3ec6SMatthias Ringwald peripheral->gatt_client_state = next_query_state; 7673deb3ec6SMatthias Ringwald } 7683deb3ec6SMatthias Ringwald 76900748105SMatthias Ringwald static void trigger_next_included_service_query(gatt_client_t * peripheral, uint16_t last_result_handle){ 7703deb3ec6SMatthias Ringwald trigger_next_query(peripheral, last_result_handle, P_W2_SEND_INCLUDED_SERVICE_QUERY); 7713deb3ec6SMatthias Ringwald } 7723deb3ec6SMatthias Ringwald 77300748105SMatthias Ringwald static void trigger_next_service_query(gatt_client_t * peripheral, uint16_t last_result_handle){ 7743deb3ec6SMatthias Ringwald trigger_next_query(peripheral, last_result_handle, P_W2_SEND_SERVICE_QUERY); 7753deb3ec6SMatthias Ringwald } 7763deb3ec6SMatthias Ringwald 77700748105SMatthias Ringwald static void trigger_next_service_by_uuid_query(gatt_client_t * peripheral, uint16_t last_result_handle){ 7783deb3ec6SMatthias Ringwald trigger_next_query(peripheral, last_result_handle, P_W2_SEND_SERVICE_WITH_UUID_QUERY); 7793deb3ec6SMatthias Ringwald } 7803deb3ec6SMatthias Ringwald 78100748105SMatthias Ringwald static void trigger_next_characteristic_query(gatt_client_t * peripheral, uint16_t last_result_handle){ 7823deb3ec6SMatthias Ringwald if (is_query_done(peripheral, last_result_handle)){ 7833deb3ec6SMatthias Ringwald // report last characteristic 7843deb3ec6SMatthias Ringwald characteristic_end_found(peripheral, peripheral->end_group_handle); 7853deb3ec6SMatthias Ringwald } 7863deb3ec6SMatthias Ringwald trigger_next_query(peripheral, last_result_handle, P_W2_SEND_ALL_CHARACTERISTICS_OF_SERVICE_QUERY); 7873deb3ec6SMatthias Ringwald } 7883deb3ec6SMatthias Ringwald 78900748105SMatthias Ringwald static void trigger_next_characteristic_descriptor_query(gatt_client_t * peripheral, uint16_t last_result_handle){ 7903deb3ec6SMatthias Ringwald trigger_next_query(peripheral, last_result_handle, P_W2_SEND_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY); 7913deb3ec6SMatthias Ringwald } 7923deb3ec6SMatthias Ringwald 79300748105SMatthias Ringwald static void trigger_next_read_by_type_query(gatt_client_t * peripheral, uint16_t last_result_handle){ 7943deb3ec6SMatthias Ringwald trigger_next_query(peripheral, last_result_handle, P_W2_SEND_READ_BY_TYPE_REQUEST); 7953deb3ec6SMatthias Ringwald } 7963deb3ec6SMatthias Ringwald 79700748105SMatthias Ringwald static void trigger_next_prepare_write_query(gatt_client_t * peripheral, gatt_client_state_t next_query_state, gatt_client_state_t done_state){ 7983deb3ec6SMatthias Ringwald peripheral->attribute_offset += write_blob_length(peripheral); 7993deb3ec6SMatthias Ringwald uint16_t next_blob_length = write_blob_length(peripheral); 8003deb3ec6SMatthias Ringwald 8013deb3ec6SMatthias Ringwald if (next_blob_length == 0){ 8023deb3ec6SMatthias Ringwald peripheral->gatt_client_state = done_state; 8033deb3ec6SMatthias Ringwald return; 8043deb3ec6SMatthias Ringwald } 8053deb3ec6SMatthias Ringwald peripheral->gatt_client_state = next_query_state; 8063deb3ec6SMatthias Ringwald } 8073deb3ec6SMatthias Ringwald 80800748105SMatthias Ringwald static void trigger_next_blob_query(gatt_client_t * peripheral, gatt_client_state_t next_query_state, uint16_t received_blob_length){ 8093deb3ec6SMatthias Ringwald 8103deb3ec6SMatthias Ringwald uint16_t max_blob_length = peripheral_mtu(peripheral) - 1; 8113deb3ec6SMatthias Ringwald if (received_blob_length < max_blob_length){ 8123deb3ec6SMatthias Ringwald gatt_client_handle_transaction_complete(peripheral); 813*9cb80b17SMilanka Ringwald emit_gatt_complete_event(peripheral, ATT_ERROR_SUCCESS); 8143deb3ec6SMatthias Ringwald return; 8153deb3ec6SMatthias Ringwald } 8163deb3ec6SMatthias Ringwald 8173deb3ec6SMatthias Ringwald peripheral->attribute_offset += received_blob_length; 8183deb3ec6SMatthias Ringwald peripheral->gatt_client_state = next_query_state; 8193deb3ec6SMatthias Ringwald } 8203deb3ec6SMatthias Ringwald 8213deb3ec6SMatthias Ringwald 8223deb3ec6SMatthias Ringwald static int is_value_valid(gatt_client_t *peripheral, uint8_t *packet, uint16_t size){ 823f8fbdce0SMatthias Ringwald uint16_t attribute_handle = little_endian_read_16(packet, 1); 824f8fbdce0SMatthias Ringwald uint16_t value_offset = little_endian_read_16(packet, 3); 8253deb3ec6SMatthias Ringwald 8263deb3ec6SMatthias Ringwald if (peripheral->attribute_handle != attribute_handle) return 0; 8273deb3ec6SMatthias Ringwald if (peripheral->attribute_offset != value_offset) return 0; 8283deb3ec6SMatthias Ringwald return memcmp(&peripheral->attribute_value[peripheral->attribute_offset], &packet[5], size-5) == 0; 8293deb3ec6SMatthias Ringwald } 8303deb3ec6SMatthias Ringwald 831544128c3SMatthias Ringwald // returns 1 if packet was sent 832544128c3SMatthias Ringwald static int gatt_client_run_for_peripheral( gatt_client_t * peripheral){ 8333deb3ec6SMatthias Ringwald // log_info("- handle_peripheral_list, mtu state %u, client state %u", peripheral->mtu_state, peripheral->gatt_client_state); 8343deb3ec6SMatthias Ringwald 835d1e1a57fSMatthias Ringwald // wait until re-encryption as central is complete 836d1e1a57fSMatthias Ringwald if (gap_reconnect_security_setup_active(peripheral->con_handle)) return 0; 837d1e1a57fSMatthias Ringwald 838f4b33574SMatthias Ringwald #ifdef ENABLE_GATT_CLIENT_PAIRING 839f4b33574SMatthias Ringwald // wait until pairing complete 840f4b33574SMatthias Ringwald if (peripheral->wait_for_pairing_complete) return 0; 841f4b33574SMatthias Ringwald #endif 842f4b33574SMatthias Ringwald 8433deb3ec6SMatthias Ringwald switch (peripheral->mtu_state) { 844544128c3SMatthias Ringwald case SEND_MTU_EXCHANGE: 8453deb3ec6SMatthias Ringwald peripheral->mtu_state = SENT_MTU_EXCHANGE; 846fc64f94aSMatthias Ringwald att_exchange_mtu_request(peripheral->con_handle); 847544128c3SMatthias Ringwald return 1; 8483deb3ec6SMatthias Ringwald case SENT_MTU_EXCHANGE: 849544128c3SMatthias Ringwald return 0; 8503deb3ec6SMatthias Ringwald default: 8513deb3ec6SMatthias Ringwald break; 8523deb3ec6SMatthias Ringwald } 8533deb3ec6SMatthias Ringwald 8543deb3ec6SMatthias Ringwald if (peripheral->send_confirmation){ 8553deb3ec6SMatthias Ringwald peripheral->send_confirmation = 0; 856fc64f94aSMatthias Ringwald att_confirmation(peripheral->con_handle); 857544128c3SMatthias Ringwald return 1; 8583deb3ec6SMatthias Ringwald } 8593deb3ec6SMatthias Ringwald 8603deb3ec6SMatthias Ringwald // check MTU for writes 8613deb3ec6SMatthias Ringwald switch (peripheral->gatt_client_state){ 8623deb3ec6SMatthias Ringwald case P_W2_SEND_WRITE_CHARACTERISTIC_VALUE: 8633deb3ec6SMatthias Ringwald case P_W2_SEND_WRITE_CHARACTERISTIC_DESCRIPTOR: 8643deb3ec6SMatthias Ringwald if (peripheral->attribute_length <= peripheral_mtu(peripheral) - 3) break; 8653deb3ec6SMatthias Ringwald log_error("gatt_client_run: value len %u > MTU %u - 3\n", peripheral->attribute_length, peripheral_mtu(peripheral)); 8663deb3ec6SMatthias Ringwald gatt_client_handle_transaction_complete(peripheral); 8673deb3ec6SMatthias Ringwald emit_gatt_complete_event(peripheral, ATT_ERROR_INVALID_ATTRIBUTE_VALUE_LENGTH); 868544128c3SMatthias Ringwald return 0; 8693deb3ec6SMatthias Ringwald default: 8703deb3ec6SMatthias Ringwald break; 8713deb3ec6SMatthias Ringwald } 8723deb3ec6SMatthias Ringwald 8733deb3ec6SMatthias Ringwald // log_info("gatt_client_state %u", peripheral->gatt_client_state); 8743deb3ec6SMatthias Ringwald switch (peripheral->gatt_client_state){ 8753deb3ec6SMatthias Ringwald case P_W2_SEND_SERVICE_QUERY: 8763deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W4_SERVICE_QUERY_RESULT; 8773deb3ec6SMatthias Ringwald send_gatt_services_request(peripheral); 878544128c3SMatthias Ringwald return 1; 8793deb3ec6SMatthias Ringwald 8803deb3ec6SMatthias Ringwald case P_W2_SEND_SERVICE_WITH_UUID_QUERY: 8813deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W4_SERVICE_WITH_UUID_RESULT; 8823deb3ec6SMatthias Ringwald send_gatt_services_by_uuid_request(peripheral); 883544128c3SMatthias Ringwald return 1; 8843deb3ec6SMatthias Ringwald 8853deb3ec6SMatthias Ringwald case P_W2_SEND_ALL_CHARACTERISTICS_OF_SERVICE_QUERY: 8863deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W4_ALL_CHARACTERISTICS_OF_SERVICE_QUERY_RESULT; 8873deb3ec6SMatthias Ringwald send_gatt_characteristic_request(peripheral); 888544128c3SMatthias Ringwald return 1; 8893deb3ec6SMatthias Ringwald 8903deb3ec6SMatthias Ringwald case P_W2_SEND_CHARACTERISTIC_WITH_UUID_QUERY: 8913deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W4_CHARACTERISTIC_WITH_UUID_QUERY_RESULT; 8923deb3ec6SMatthias Ringwald send_gatt_characteristic_request(peripheral); 893544128c3SMatthias Ringwald return 1; 8943deb3ec6SMatthias Ringwald 8953deb3ec6SMatthias Ringwald case P_W2_SEND_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY: 8963deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W4_CHARACTERISTIC_WITH_UUID_QUERY_RESULT; 8973deb3ec6SMatthias Ringwald send_gatt_characteristic_descriptor_request(peripheral); 898544128c3SMatthias Ringwald return 1; 8993deb3ec6SMatthias Ringwald 9003deb3ec6SMatthias Ringwald case P_W2_SEND_INCLUDED_SERVICE_QUERY: 9013deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W4_INCLUDED_SERVICE_QUERY_RESULT; 9023deb3ec6SMatthias Ringwald send_gatt_included_service_request(peripheral); 903544128c3SMatthias Ringwald return 1; 9043deb3ec6SMatthias Ringwald 9053deb3ec6SMatthias Ringwald case P_W2_SEND_INCLUDED_SERVICE_WITH_UUID_QUERY: 9063deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W4_INCLUDED_SERVICE_UUID_WITH_QUERY_RESULT; 9073deb3ec6SMatthias Ringwald send_gatt_included_service_uuid_request(peripheral); 908544128c3SMatthias Ringwald return 1; 9093deb3ec6SMatthias Ringwald 9103deb3ec6SMatthias Ringwald case P_W2_SEND_READ_CHARACTERISTIC_VALUE_QUERY: 9113deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W4_READ_CHARACTERISTIC_VALUE_RESULT; 9123deb3ec6SMatthias Ringwald send_gatt_read_characteristic_value_request(peripheral); 913544128c3SMatthias Ringwald return 1; 9143deb3ec6SMatthias Ringwald 9153deb3ec6SMatthias Ringwald case P_W2_SEND_READ_BLOB_QUERY: 9163deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W4_READ_BLOB_RESULT; 9173deb3ec6SMatthias Ringwald send_gatt_read_blob_request(peripheral); 918544128c3SMatthias Ringwald return 1; 9193deb3ec6SMatthias Ringwald 9203deb3ec6SMatthias Ringwald case P_W2_SEND_READ_BY_TYPE_REQUEST: 9213deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W4_READ_BY_TYPE_RESPONSE; 9223deb3ec6SMatthias Ringwald send_gatt_read_by_type_request(peripheral); 923544128c3SMatthias Ringwald return 1; 9243deb3ec6SMatthias Ringwald 9253deb3ec6SMatthias Ringwald case P_W2_SEND_READ_MULTIPLE_REQUEST: 9263deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W4_READ_MULTIPLE_RESPONSE; 9273deb3ec6SMatthias Ringwald send_gatt_read_multiple_request(peripheral); 928544128c3SMatthias Ringwald return 1; 9293deb3ec6SMatthias Ringwald 9303deb3ec6SMatthias Ringwald case P_W2_SEND_WRITE_CHARACTERISTIC_VALUE: 9313deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W4_WRITE_CHARACTERISTIC_VALUE_RESULT; 9323deb3ec6SMatthias Ringwald send_gatt_write_attribute_value_request(peripheral); 933544128c3SMatthias Ringwald return 1; 9343deb3ec6SMatthias Ringwald 9353deb3ec6SMatthias Ringwald case P_W2_PREPARE_WRITE: 9363deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W4_PREPARE_WRITE_RESULT; 9373deb3ec6SMatthias Ringwald send_gatt_prepare_write_request(peripheral); 938544128c3SMatthias Ringwald return 1; 9393deb3ec6SMatthias Ringwald 9403deb3ec6SMatthias Ringwald case P_W2_PREPARE_WRITE_SINGLE: 9413deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W4_PREPARE_WRITE_SINGLE_RESULT; 9423deb3ec6SMatthias Ringwald send_gatt_prepare_write_request(peripheral); 943544128c3SMatthias Ringwald return 1; 9443deb3ec6SMatthias Ringwald 9453deb3ec6SMatthias Ringwald case P_W2_PREPARE_RELIABLE_WRITE: 9463deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W4_PREPARE_RELIABLE_WRITE_RESULT; 9473deb3ec6SMatthias Ringwald send_gatt_prepare_write_request(peripheral); 948544128c3SMatthias Ringwald return 1; 9493deb3ec6SMatthias Ringwald 9503deb3ec6SMatthias Ringwald case P_W2_EXECUTE_PREPARED_WRITE: 9513deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W4_EXECUTE_PREPARED_WRITE_RESULT; 9523deb3ec6SMatthias Ringwald send_gatt_execute_write_request(peripheral); 953544128c3SMatthias Ringwald return 1; 9543deb3ec6SMatthias Ringwald 9553deb3ec6SMatthias Ringwald case P_W2_CANCEL_PREPARED_WRITE: 9563deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W4_CANCEL_PREPARED_WRITE_RESULT; 9573deb3ec6SMatthias Ringwald send_gatt_cancel_prepared_write_request(peripheral); 958544128c3SMatthias Ringwald return 1; 9593deb3ec6SMatthias Ringwald 9603deb3ec6SMatthias Ringwald case P_W2_CANCEL_PREPARED_WRITE_DATA_MISMATCH: 9613deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W4_CANCEL_PREPARED_WRITE_DATA_MISMATCH_RESULT; 9623deb3ec6SMatthias Ringwald send_gatt_cancel_prepared_write_request(peripheral); 963544128c3SMatthias Ringwald return 1; 9643deb3ec6SMatthias Ringwald 965abdc9fb5SMatthias Ringwald #ifdef ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERY 966abdc9fb5SMatthias Ringwald case P_W2_SEND_FIND_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY: 967abdc9fb5SMatthias Ringwald // use Find Information 968abdc9fb5SMatthias Ringwald peripheral->gatt_client_state = P_W4_FIND_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY_RESULT; 969abdc9fb5SMatthias Ringwald send_gatt_characteristic_descriptor_request(peripheral); 970abdc9fb5SMatthias Ringwald #else 9713deb3ec6SMatthias Ringwald case P_W2_SEND_READ_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY: 972abdc9fb5SMatthias Ringwald // Use Read By Type 9733deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W4_READ_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY_RESULT; 9743deb3ec6SMatthias Ringwald send_gatt_read_client_characteristic_configuration_request(peripheral); 975abdc9fb5SMatthias Ringwald #endif 976544128c3SMatthias Ringwald return 1; 9773deb3ec6SMatthias Ringwald 9783deb3ec6SMatthias Ringwald case P_W2_SEND_READ_CHARACTERISTIC_DESCRIPTOR_QUERY: 9793deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W4_READ_CHARACTERISTIC_DESCRIPTOR_RESULT; 9803deb3ec6SMatthias Ringwald send_gatt_read_characteristic_descriptor_request(peripheral); 981544128c3SMatthias Ringwald return 1; 9823deb3ec6SMatthias Ringwald 9833deb3ec6SMatthias Ringwald case P_W2_SEND_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_QUERY: 9843deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W4_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_RESULT; 9853deb3ec6SMatthias Ringwald send_gatt_read_blob_request(peripheral); 986544128c3SMatthias Ringwald return 1; 9873deb3ec6SMatthias Ringwald 9883deb3ec6SMatthias Ringwald case P_W2_SEND_WRITE_CHARACTERISTIC_DESCRIPTOR: 9893deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W4_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT; 9903deb3ec6SMatthias Ringwald send_gatt_write_attribute_value_request(peripheral); 991544128c3SMatthias Ringwald return 1; 9923deb3ec6SMatthias Ringwald 9933deb3ec6SMatthias Ringwald case P_W2_WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION: 9943deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W4_CLIENT_CHARACTERISTIC_CONFIGURATION_RESULT; 9953deb3ec6SMatthias Ringwald send_gatt_write_client_characteristic_configuration_request(peripheral); 996544128c3SMatthias Ringwald return 1; 9973deb3ec6SMatthias Ringwald 9983deb3ec6SMatthias Ringwald case P_W2_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR: 9993deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W4_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT; 10003deb3ec6SMatthias Ringwald send_gatt_prepare_write_request(peripheral); 1001544128c3SMatthias Ringwald return 1; 10023deb3ec6SMatthias Ringwald 10033deb3ec6SMatthias Ringwald case P_W2_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR: 10043deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W4_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT; 10053deb3ec6SMatthias Ringwald send_gatt_execute_write_request(peripheral); 1006544128c3SMatthias Ringwald return 1; 10073deb3ec6SMatthias Ringwald 10087a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE 1009793cf6ceSMatthias Ringwald case P_W4_IDENTITY_RESOLVING: 1010793cf6ceSMatthias Ringwald log_info("P_W4_IDENTITY_RESOLVING - state %x", sm_identity_resolving_state(peripheral->con_handle)); 1011793cf6ceSMatthias Ringwald switch (sm_identity_resolving_state(peripheral->con_handle)){ 1012793cf6ceSMatthias Ringwald case IRK_LOOKUP_SUCCEEDED: 1013793cf6ceSMatthias Ringwald peripheral->le_device_index = sm_le_device_index(peripheral->con_handle); 1014793cf6ceSMatthias Ringwald peripheral->gatt_client_state = P_W4_CMAC_READY; 1015793cf6ceSMatthias Ringwald break; 1016793cf6ceSMatthias Ringwald case IRK_LOOKUP_FAILED: 1017793cf6ceSMatthias Ringwald gatt_client_handle_transaction_complete(peripheral); 1018793cf6ceSMatthias Ringwald emit_gatt_complete_event(peripheral, ATT_ERROR_BONDING_INFORMATION_MISSING); 1019793cf6ceSMatthias Ringwald return 0; 1020793cf6ceSMatthias Ringwald default: 1021793cf6ceSMatthias Ringwald return 0; 1022793cf6ceSMatthias Ringwald } 1023793cf6ceSMatthias Ringwald 1024793cf6ceSMatthias Ringwald /* Fall through */ 1025793cf6ceSMatthias Ringwald 10263deb3ec6SMatthias Ringwald case P_W4_CMAC_READY: 10273deb3ec6SMatthias Ringwald if (sm_cmac_ready()){ 10283deb3ec6SMatthias Ringwald sm_key_t csrk; 10293deb3ec6SMatthias Ringwald le_device_db_local_csrk_get(peripheral->le_device_index, csrk); 10303deb3ec6SMatthias Ringwald uint32_t sign_counter = le_device_db_local_counter_get(peripheral->le_device_index); 10313deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W4_CMAC_RESULT; 10324dfd504aSMatthias Ringwald sm_cmac_signed_write_start(csrk, ATT_SIGNED_WRITE_COMMAND, peripheral->attribute_handle, peripheral->attribute_length, peripheral->attribute_value, sign_counter, att_signed_write_handle_cmac_result); 10333deb3ec6SMatthias Ringwald } 1034544128c3SMatthias Ringwald return 0; 10353deb3ec6SMatthias Ringwald 10363deb3ec6SMatthias Ringwald case P_W2_SEND_SIGNED_WRITE: { 10373deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W4_SEND_SINGED_WRITE_DONE; 10383deb3ec6SMatthias Ringwald // bump local signing counter 10393deb3ec6SMatthias Ringwald uint32_t sign_counter = le_device_db_local_counter_get(peripheral->le_device_index); 10403deb3ec6SMatthias Ringwald le_device_db_local_counter_set(peripheral->le_device_index, sign_counter + 1); 104164b12680SMatthias Ringwald // send signed write command 10423deb3ec6SMatthias Ringwald send_gatt_signed_write_request(peripheral, sign_counter); 10433deb3ec6SMatthias Ringwald // finally, notifiy client that write is complete 10443deb3ec6SMatthias Ringwald gatt_client_handle_transaction_complete(peripheral); 1045*9cb80b17SMilanka Ringwald emit_gatt_complete_event(peripheral, ATT_ERROR_SUCCESS); 1046544128c3SMatthias Ringwald return 1; 10473deb3ec6SMatthias Ringwald } 10487a766ebfSMatthias Ringwald #endif 10493deb3ec6SMatthias Ringwald default: 10503deb3ec6SMatthias Ringwald break; 10513deb3ec6SMatthias Ringwald } 105247181045SMatthias Ringwald 105347181045SMatthias Ringwald // requested can send snow? 105447181045SMatthias Ringwald if (peripheral->write_without_response_callback){ 105547181045SMatthias Ringwald btstack_packet_handler_t packet_handler = peripheral->write_without_response_callback; 105647181045SMatthias Ringwald peripheral->write_without_response_callback = NULL; 105747181045SMatthias Ringwald uint8_t event[4]; 105847181045SMatthias Ringwald event[0] = GATT_EVENT_CAN_WRITE_WITHOUT_RESPONSE; 105947181045SMatthias Ringwald event[1] = sizeof(event) - 2; 106047181045SMatthias Ringwald little_endian_store_16(event, 2, peripheral->con_handle); 106147181045SMatthias Ringwald packet_handler(HCI_EVENT_PACKET, peripheral->con_handle, event, sizeof(event)); 106247181045SMatthias Ringwald return 1; // to trigger requeueing (even if higher layer didn't sent) 106347181045SMatthias Ringwald } 106447181045SMatthias Ringwald 1065544128c3SMatthias Ringwald return 0; 10663deb3ec6SMatthias Ringwald } 10673deb3ec6SMatthias Ringwald 1068544128c3SMatthias Ringwald static void gatt_client_run(void){ 1069544128c3SMatthias Ringwald btstack_linked_item_t *it; 1070544128c3SMatthias Ringwald for (it = (btstack_linked_item_t *) gatt_client_connections; it ; it = it->next){ 1071544128c3SMatthias Ringwald gatt_client_t * peripheral = (gatt_client_t *) it; 1072544128c3SMatthias Ringwald if (!att_dispatch_client_can_send_now(peripheral->con_handle)) { 1073544128c3SMatthias Ringwald att_dispatch_client_request_can_send_now_event(peripheral->con_handle); 1074544128c3SMatthias Ringwald return; 1075544128c3SMatthias Ringwald } 1076544128c3SMatthias Ringwald int packet_sent = gatt_client_run_for_peripheral(peripheral); 1077544128c3SMatthias Ringwald if (packet_sent){ 107845f2a740SMatthias Ringwald // request new permission 1079544128c3SMatthias Ringwald att_dispatch_client_request_can_send_now_event(peripheral->con_handle); 108045f2a740SMatthias Ringwald // requeue client for fairness and exit 108145f2a740SMatthias Ringwald // note: iterator has become invalid 108245f2a740SMatthias Ringwald btstack_linked_list_remove(&gatt_client_connections, (btstack_linked_item_t *) peripheral); 108345f2a740SMatthias Ringwald btstack_linked_list_add_tail(&gatt_client_connections, (btstack_linked_item_t *) peripheral); 1084544128c3SMatthias Ringwald return; 1085544128c3SMatthias Ringwald } 1086544128c3SMatthias Ringwald } 10873deb3ec6SMatthias Ringwald } 10883deb3ec6SMatthias Ringwald 10893deb3ec6SMatthias Ringwald static void gatt_client_report_error_if_pending(gatt_client_t *peripheral, uint8_t error_code) { 10903deb3ec6SMatthias Ringwald if (is_ready(peripheral)) return; 10913deb3ec6SMatthias Ringwald gatt_client_handle_transaction_complete(peripheral); 10923deb3ec6SMatthias Ringwald emit_gatt_complete_event(peripheral, error_code); 10933deb3ec6SMatthias Ringwald } 10943deb3ec6SMatthias Ringwald 1095f4b33574SMatthias Ringwald static void gatt_client_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 10961ac26e06SMatthias Ringwald UNUSED(channel); // ok: handling own l2cap events 10971ac26e06SMatthias Ringwald UNUSED(size); // ok: there is no channel 10989ec2630cSMatthias Ringwald 1099361b1363SMatthias Ringwald if (packet_type != HCI_EVENT_PACKET) return; 1100361b1363SMatthias Ringwald 1101f4b33574SMatthias Ringwald hci_con_handle_t con_handle; 1102f4b33574SMatthias Ringwald gatt_client_t * peripheral; 11030e2df43fSMatthias Ringwald switch (hci_event_packet_get_type(packet)) { 11043deb3ec6SMatthias Ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 11053deb3ec6SMatthias Ringwald log_info("GATT Client: HCI_EVENT_DISCONNECTION_COMPLETE"); 1106f4b33574SMatthias Ringwald con_handle = little_endian_read_16(packet,3); 1107f4b33574SMatthias Ringwald peripheral = get_gatt_client_context_for_handle(con_handle); 11083deb3ec6SMatthias Ringwald if (!peripheral) break; 11093deb3ec6SMatthias Ringwald 1110cce308d6SMatthias Ringwald gatt_client_report_error_if_pending(peripheral, ATT_ERROR_HCI_DISCONNECT_RECEIVED); 1111cce308d6SMatthias Ringwald gatt_client_timeout_stop(peripheral); 1112665d90f2SMatthias Ringwald btstack_linked_list_remove(&gatt_client_connections, (btstack_linked_item_t *) peripheral); 11133deb3ec6SMatthias Ringwald btstack_memory_gatt_client_free(peripheral); 11143deb3ec6SMatthias Ringwald break; 1115f4b33574SMatthias Ringwald 1116f4b33574SMatthias Ringwald #ifdef ENABLE_GATT_CLIENT_PAIRING 1117f4b33574SMatthias Ringwald // Pairing complete (with/without bonding=storing of pairing information) 1118f4b33574SMatthias Ringwald case SM_EVENT_PAIRING_COMPLETE: 1119f4b33574SMatthias Ringwald con_handle = sm_event_pairing_complete_get_handle(packet); 1120f4b33574SMatthias Ringwald peripheral = get_gatt_client_context_for_handle(con_handle); 1121f4b33574SMatthias Ringwald if (!peripheral) break; 1122f4b33574SMatthias Ringwald 1123f4b33574SMatthias Ringwald if (peripheral->wait_for_pairing_complete){ 1124f4b33574SMatthias Ringwald peripheral->wait_for_pairing_complete = 0; 1125f4b33574SMatthias Ringwald if (sm_event_pairing_complete_get_status(packet)){ 1126f4b33574SMatthias Ringwald log_info("pairing failed, report previous error 0x%x", peripheral->pending_error_code); 1127f4b33574SMatthias Ringwald gatt_client_handle_transaction_complete(peripheral); 1128f4b33574SMatthias Ringwald emit_gatt_complete_event(peripheral, peripheral->pending_error_code); 1129f4b33574SMatthias Ringwald } else { 1130f4b33574SMatthias Ringwald log_info("pairing success, retry operation"); 11313deb3ec6SMatthias Ringwald } 1132f4b33574SMatthias Ringwald } 1133f4b33574SMatthias Ringwald break; 1134f4b33574SMatthias Ringwald #endif 1135793cf6ceSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE 1136793cf6ceSMatthias Ringwald // Identity Resolving completed (no code, gatt_client_run will continue) 1137793cf6ceSMatthias Ringwald case SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED: 1138793cf6ceSMatthias Ringwald case SM_EVENT_IDENTITY_RESOLVING_FAILED: 1139793cf6ceSMatthias Ringwald break; 1140793cf6ceSMatthias Ringwald #endif 1141f4b33574SMatthias Ringwald 11423deb3ec6SMatthias Ringwald default: 11433deb3ec6SMatthias Ringwald break; 11443deb3ec6SMatthias Ringwald } 11453deb3ec6SMatthias Ringwald 1146361b1363SMatthias Ringwald gatt_client_run(); 11473deb3ec6SMatthias Ringwald } 11483deb3ec6SMatthias Ringwald 11493deb3ec6SMatthias Ringwald static void gatt_client_att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){ 11503deb3ec6SMatthias Ringwald 11510fbf59a1SMatthias Ringwald gatt_client_t * peripheral; 11520fbf59a1SMatthias Ringwald 11530fbf59a1SMatthias Ringwald if (packet_type == HCI_EVENT_PACKET) { 11540fbf59a1SMatthias Ringwald switch (packet[0]){ 11550fbf59a1SMatthias Ringwald case L2CAP_EVENT_CAN_SEND_NOW: 115651764b3bSMatthias Ringwald gatt_client_run(); 11570fbf59a1SMatthias Ringwald break; 11580fbf59a1SMatthias Ringwald // att_server has negotiated the mtu for this connection, cache if context exists 11590fbf59a1SMatthias Ringwald case ATT_EVENT_MTU_EXCHANGE_COMPLETE: 11600fbf59a1SMatthias Ringwald peripheral = get_gatt_client_context_for_handle(handle); 11610fbf59a1SMatthias Ringwald if (!peripheral) break; 11620fbf59a1SMatthias Ringwald peripheral->mtu = little_endian_read_16(packet, 4); 11630fbf59a1SMatthias Ringwald break; 11640fbf59a1SMatthias Ringwald default: 11650fbf59a1SMatthias Ringwald break; 11660fbf59a1SMatthias Ringwald } 11670fbf59a1SMatthias Ringwald return; 116851764b3bSMatthias Ringwald } 116951764b3bSMatthias Ringwald 11703deb3ec6SMatthias Ringwald if (packet_type != ATT_DATA_PACKET) return; 11713deb3ec6SMatthias Ringwald 11723deb3ec6SMatthias Ringwald // special cases: notifications don't need a context while indications motivate creating one 11733deb3ec6SMatthias Ringwald switch (packet[0]){ 11743deb3ec6SMatthias Ringwald case ATT_HANDLE_VALUE_NOTIFICATION: 1175f8fbdce0SMatthias Ringwald report_gatt_notification(handle, little_endian_read_16(packet,1), &packet[3], size-3); 11763deb3ec6SMatthias Ringwald return; 11773deb3ec6SMatthias Ringwald case ATT_HANDLE_VALUE_INDICATION: 11783deb3ec6SMatthias Ringwald peripheral = provide_context_for_conn_handle(handle); 11793deb3ec6SMatthias Ringwald break; 11803deb3ec6SMatthias Ringwald default: 11813deb3ec6SMatthias Ringwald peripheral = get_gatt_client_context_for_handle(handle); 11823deb3ec6SMatthias Ringwald break; 11833deb3ec6SMatthias Ringwald } 11843deb3ec6SMatthias Ringwald 11853deb3ec6SMatthias Ringwald if (!peripheral) return; 11863deb3ec6SMatthias Ringwald 11873deb3ec6SMatthias Ringwald switch (packet[0]){ 11883deb3ec6SMatthias Ringwald case ATT_EXCHANGE_MTU_RESPONSE: 11893deb3ec6SMatthias Ringwald { 1190f8fbdce0SMatthias Ringwald uint16_t remote_rx_mtu = little_endian_read_16(packet, 1); 11913deb3ec6SMatthias Ringwald uint16_t local_rx_mtu = l2cap_max_le_mtu(); 11923deb3ec6SMatthias Ringwald peripheral->mtu = remote_rx_mtu < local_rx_mtu ? remote_rx_mtu : local_rx_mtu; 11933deb3ec6SMatthias Ringwald peripheral->mtu_state = MTU_EXCHANGED; 11948f37572aSJakob Krantz emit_gatt_mtu_exchanged_result_event(peripheral, peripheral->mtu); 11953deb3ec6SMatthias Ringwald break; 11963deb3ec6SMatthias Ringwald } 11973deb3ec6SMatthias Ringwald case ATT_READ_BY_GROUP_TYPE_RESPONSE: 11983deb3ec6SMatthias Ringwald switch(peripheral->gatt_client_state){ 11993deb3ec6SMatthias Ringwald case P_W4_SERVICE_QUERY_RESULT: 12003deb3ec6SMatthias Ringwald report_gatt_services(peripheral, packet, size); 12013deb3ec6SMatthias Ringwald trigger_next_service_query(peripheral, get_last_result_handle_from_service_list(packet, size)); 12025611a760SMatthias Ringwald // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done 12033deb3ec6SMatthias Ringwald break; 12043deb3ec6SMatthias Ringwald default: 12053deb3ec6SMatthias Ringwald break; 12063deb3ec6SMatthias Ringwald } 12073deb3ec6SMatthias Ringwald break; 12083deb3ec6SMatthias Ringwald case ATT_HANDLE_VALUE_INDICATION: 1209f8fbdce0SMatthias Ringwald report_gatt_indication(handle, little_endian_read_16(packet,1), &packet[3], size-3); 12103deb3ec6SMatthias Ringwald peripheral->send_confirmation = 1; 12113deb3ec6SMatthias Ringwald break; 12123deb3ec6SMatthias Ringwald 12133deb3ec6SMatthias Ringwald case ATT_READ_BY_TYPE_RESPONSE: 12143deb3ec6SMatthias Ringwald switch (peripheral->gatt_client_state){ 12153deb3ec6SMatthias Ringwald case P_W4_ALL_CHARACTERISTICS_OF_SERVICE_QUERY_RESULT: 12163deb3ec6SMatthias Ringwald report_gatt_characteristics(peripheral, packet, size); 12173deb3ec6SMatthias Ringwald trigger_next_characteristic_query(peripheral, get_last_result_handle_from_characteristics_list(packet, size)); 12185611a760SMatthias Ringwald // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done, or by ATT_ERROR 12193deb3ec6SMatthias Ringwald break; 12203deb3ec6SMatthias Ringwald case P_W4_CHARACTERISTIC_WITH_UUID_QUERY_RESULT: 12213deb3ec6SMatthias Ringwald report_gatt_characteristics(peripheral, packet, size); 12223deb3ec6SMatthias Ringwald trigger_next_characteristic_query(peripheral, get_last_result_handle_from_characteristics_list(packet, size)); 12235611a760SMatthias Ringwald // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done, or by ATT_ERROR 12243deb3ec6SMatthias Ringwald break; 12253deb3ec6SMatthias Ringwald case P_W4_INCLUDED_SERVICE_QUERY_RESULT: 12263deb3ec6SMatthias Ringwald { 12273deb3ec6SMatthias Ringwald uint16_t uuid16 = 0; 12283deb3ec6SMatthias Ringwald uint16_t pair_size = packet[1]; 12293deb3ec6SMatthias Ringwald 12303deb3ec6SMatthias Ringwald if (pair_size < 7){ 12313deb3ec6SMatthias Ringwald // UUIDs not available, query first included service 1232f8fbdce0SMatthias Ringwald peripheral->start_group_handle = little_endian_read_16(packet, 2); // ready for next query 1233f8fbdce0SMatthias Ringwald peripheral->query_start_handle = little_endian_read_16(packet, 4); 1234f8fbdce0SMatthias Ringwald peripheral->query_end_handle = little_endian_read_16(packet,6); 12353deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W2_SEND_INCLUDED_SERVICE_WITH_UUID_QUERY; 12363deb3ec6SMatthias Ringwald break; 12373deb3ec6SMatthias Ringwald } 12383deb3ec6SMatthias Ringwald 12393deb3ec6SMatthias Ringwald uint16_t offset; 12403deb3ec6SMatthias Ringwald for (offset = 2; offset < size; offset += pair_size){ 1241f8fbdce0SMatthias Ringwald uint16_t include_handle = little_endian_read_16(packet, offset); 1242f8fbdce0SMatthias Ringwald peripheral->query_start_handle = little_endian_read_16(packet,offset+2); 1243f8fbdce0SMatthias Ringwald peripheral->query_end_handle = little_endian_read_16(packet,offset+4); 1244f8fbdce0SMatthias Ringwald uuid16 = little_endian_read_16(packet, offset+6); 12453deb3ec6SMatthias Ringwald report_gatt_included_service_uuid16(peripheral, include_handle, uuid16); 12463deb3ec6SMatthias Ringwald } 12473deb3ec6SMatthias Ringwald 12483deb3ec6SMatthias Ringwald trigger_next_included_service_query(peripheral, get_last_result_handle_from_included_services_list(packet, size)); 12495611a760SMatthias Ringwald // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done 12503deb3ec6SMatthias Ringwald break; 12513deb3ec6SMatthias Ringwald } 1252abdc9fb5SMatthias Ringwald #ifndef ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERY 12533deb3ec6SMatthias Ringwald case P_W4_READ_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY_RESULT: 1254f8fbdce0SMatthias Ringwald peripheral->client_characteristic_configuration_handle = little_endian_read_16(packet, 2); 12553deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W2_WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION; 12563deb3ec6SMatthias Ringwald break; 1257abdc9fb5SMatthias Ringwald #endif 12583deb3ec6SMatthias Ringwald case P_W4_READ_BY_TYPE_RESPONSE: { 12593deb3ec6SMatthias Ringwald uint16_t pair_size = packet[1]; 12603deb3ec6SMatthias Ringwald uint16_t offset; 12613deb3ec6SMatthias Ringwald uint16_t last_result_handle = 0; 12623deb3ec6SMatthias Ringwald for (offset = 2; offset < size ; offset += pair_size){ 1263f8fbdce0SMatthias Ringwald uint16_t value_handle = little_endian_read_16(packet, offset); 12643deb3ec6SMatthias Ringwald report_gatt_characteristic_value(peripheral, value_handle, &packet[offset+2], pair_size-2); 12653deb3ec6SMatthias Ringwald last_result_handle = value_handle; 12663deb3ec6SMatthias Ringwald } 12673deb3ec6SMatthias Ringwald trigger_next_read_by_type_query(peripheral, last_result_handle); 12683deb3ec6SMatthias Ringwald break; 12693deb3ec6SMatthias Ringwald } 12703deb3ec6SMatthias Ringwald default: 12713deb3ec6SMatthias Ringwald break; 12723deb3ec6SMatthias Ringwald } 12733deb3ec6SMatthias Ringwald break; 12743deb3ec6SMatthias Ringwald case ATT_READ_RESPONSE: 12753deb3ec6SMatthias Ringwald switch (peripheral->gatt_client_state){ 12763deb3ec6SMatthias Ringwald case P_W4_INCLUDED_SERVICE_UUID_WITH_QUERY_RESULT: { 12773deb3ec6SMatthias Ringwald uint8_t uuid128[16]; 12789c80e4ccSMatthias Ringwald reverse_128(&packet[1], uuid128); 12793deb3ec6SMatthias Ringwald report_gatt_included_service_uuid128(peripheral, peripheral->start_group_handle, uuid128); 12803deb3ec6SMatthias Ringwald trigger_next_included_service_query(peripheral, peripheral->start_group_handle); 12815611a760SMatthias Ringwald // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done 12823deb3ec6SMatthias Ringwald break; 12833deb3ec6SMatthias Ringwald } 12843deb3ec6SMatthias Ringwald case P_W4_READ_CHARACTERISTIC_VALUE_RESULT: 12853deb3ec6SMatthias Ringwald gatt_client_handle_transaction_complete(peripheral); 12863deb3ec6SMatthias Ringwald report_gatt_characteristic_value(peripheral, peripheral->attribute_handle, &packet[1], size-1); 1287*9cb80b17SMilanka Ringwald emit_gatt_complete_event(peripheral, ATT_ERROR_SUCCESS); 12883deb3ec6SMatthias Ringwald break; 12893deb3ec6SMatthias Ringwald 12903deb3ec6SMatthias Ringwald case P_W4_READ_CHARACTERISTIC_DESCRIPTOR_RESULT:{ 12913deb3ec6SMatthias Ringwald gatt_client_handle_transaction_complete(peripheral); 12923deb3ec6SMatthias Ringwald report_gatt_characteristic_descriptor(peripheral, peripheral->attribute_handle, &packet[1], size-1, 0); 1293*9cb80b17SMilanka Ringwald emit_gatt_complete_event(peripheral, ATT_ERROR_SUCCESS); 12943deb3ec6SMatthias Ringwald break; 12953deb3ec6SMatthias Ringwald } 12963deb3ec6SMatthias Ringwald default: 12973deb3ec6SMatthias Ringwald break; 12983deb3ec6SMatthias Ringwald } 12993deb3ec6SMatthias Ringwald break; 13003deb3ec6SMatthias Ringwald 13013deb3ec6SMatthias Ringwald case ATT_FIND_BY_TYPE_VALUE_RESPONSE: 13023deb3ec6SMatthias Ringwald { 13033deb3ec6SMatthias Ringwald uint8_t pair_size = 4; 13043deb3ec6SMatthias Ringwald int i; 13053deb3ec6SMatthias Ringwald uint16_t start_group_handle; 13065611a760SMatthias Ringwald uint16_t end_group_handle= 0xffff; // asserts GATT_EVENT_QUERY_COMPLETE is emitted if no results 13073deb3ec6SMatthias Ringwald for (i = 1; i<size; i+=pair_size){ 1308f8fbdce0SMatthias Ringwald start_group_handle = little_endian_read_16(packet,i); 1309f8fbdce0SMatthias Ringwald end_group_handle = little_endian_read_16(packet,i+2); 13103deb3ec6SMatthias Ringwald emit_gatt_service_query_result_event(peripheral, start_group_handle, end_group_handle, peripheral->uuid128); 13113deb3ec6SMatthias Ringwald } 13123deb3ec6SMatthias Ringwald trigger_next_service_by_uuid_query(peripheral, end_group_handle); 13135611a760SMatthias Ringwald // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done 13143deb3ec6SMatthias Ringwald break; 13153deb3ec6SMatthias Ringwald } 13163deb3ec6SMatthias Ringwald case ATT_FIND_INFORMATION_REPLY: 13173deb3ec6SMatthias Ringwald { 13183deb3ec6SMatthias Ringwald uint8_t pair_size = 4; 13193deb3ec6SMatthias Ringwald if (packet[1] == 2){ 13203deb3ec6SMatthias Ringwald pair_size = 18; 13213deb3ec6SMatthias Ringwald } 1322f8fbdce0SMatthias Ringwald uint16_t last_descriptor_handle = little_endian_read_16(packet, size - pair_size); 1323abdc9fb5SMatthias Ringwald 1324abdc9fb5SMatthias Ringwald #ifdef ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERY 1325abdc9fb5SMatthias Ringwald log_info("ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERY, state %x", peripheral->gatt_client_state); 1326abdc9fb5SMatthias Ringwald if (peripheral->gatt_client_state == P_W4_FIND_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY_RESULT){ 1327abdc9fb5SMatthias Ringwald // iterate over descriptors looking for CCC 1328abdc9fb5SMatthias Ringwald if (pair_size == 4){ 1329abdc9fb5SMatthias Ringwald int offset = 2; 1330abdc9fb5SMatthias Ringwald while (offset < size){ 1331abdc9fb5SMatthias Ringwald uint16_t uuid16 = little_endian_read_16(packet, offset + 2); 1332abdc9fb5SMatthias Ringwald if (uuid16 == GATT_CLIENT_CHARACTERISTICS_CONFIGURATION){ 1333abdc9fb5SMatthias Ringwald peripheral->client_characteristic_configuration_handle = little_endian_read_16(packet, offset); 1334abdc9fb5SMatthias Ringwald peripheral->gatt_client_state = P_W2_WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION; 1335abdc9fb5SMatthias Ringwald log_info("CCC found %x", peripheral->client_characteristic_configuration_handle); 1336abdc9fb5SMatthias Ringwald break; 1337abdc9fb5SMatthias Ringwald } 1338abdc9fb5SMatthias Ringwald offset += pair_size; 1339abdc9fb5SMatthias Ringwald } 1340abdc9fb5SMatthias Ringwald } 1341abdc9fb5SMatthias Ringwald if (is_query_done(peripheral, last_descriptor_handle)){ 1342abdc9fb5SMatthias Ringwald 1343abdc9fb5SMatthias Ringwald } else { 1344abdc9fb5SMatthias Ringwald // next 1345abdc9fb5SMatthias Ringwald peripheral->start_group_handle = last_descriptor_handle + 1; 1346abdc9fb5SMatthias Ringwald peripheral->gatt_client_state = P_W2_SEND_FIND_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY; 1347abdc9fb5SMatthias Ringwald } 1348abdc9fb5SMatthias Ringwald break; 1349abdc9fb5SMatthias Ringwald } 1350abdc9fb5SMatthias Ringwald #endif 13513deb3ec6SMatthias Ringwald report_gatt_all_characteristic_descriptors(peripheral, &packet[2], size-2, pair_size); 13523deb3ec6SMatthias Ringwald trigger_next_characteristic_descriptor_query(peripheral, last_descriptor_handle); 13535611a760SMatthias Ringwald // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done 13543deb3ec6SMatthias Ringwald break; 13553deb3ec6SMatthias Ringwald } 13563deb3ec6SMatthias Ringwald 13573deb3ec6SMatthias Ringwald case ATT_WRITE_RESPONSE: 13583deb3ec6SMatthias Ringwald switch (peripheral->gatt_client_state){ 13593deb3ec6SMatthias Ringwald case P_W4_WRITE_CHARACTERISTIC_VALUE_RESULT: 13603deb3ec6SMatthias Ringwald gatt_client_handle_transaction_complete(peripheral); 1361*9cb80b17SMilanka Ringwald emit_gatt_complete_event(peripheral, ATT_ERROR_SUCCESS); 13623deb3ec6SMatthias Ringwald break; 13633deb3ec6SMatthias Ringwald case P_W4_CLIENT_CHARACTERISTIC_CONFIGURATION_RESULT: 13643deb3ec6SMatthias Ringwald gatt_client_handle_transaction_complete(peripheral); 1365*9cb80b17SMilanka Ringwald emit_gatt_complete_event(peripheral, ATT_ERROR_SUCCESS); 13663deb3ec6SMatthias Ringwald break; 13673deb3ec6SMatthias Ringwald case P_W4_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT: 13683deb3ec6SMatthias Ringwald gatt_client_handle_transaction_complete(peripheral); 1369*9cb80b17SMilanka Ringwald emit_gatt_complete_event(peripheral, ATT_ERROR_SUCCESS); 13703deb3ec6SMatthias Ringwald break; 13713deb3ec6SMatthias Ringwald default: 13723deb3ec6SMatthias Ringwald break; 13733deb3ec6SMatthias Ringwald } 13743deb3ec6SMatthias Ringwald break; 13753deb3ec6SMatthias Ringwald 13763deb3ec6SMatthias Ringwald case ATT_READ_BLOB_RESPONSE:{ 13773deb3ec6SMatthias Ringwald uint16_t received_blob_length = size-1; 13783deb3ec6SMatthias Ringwald switch(peripheral->gatt_client_state){ 13793deb3ec6SMatthias Ringwald case P_W4_READ_BLOB_RESULT: 13803deb3ec6SMatthias Ringwald report_gatt_long_characteristic_value_blob(peripheral, peripheral->attribute_handle, &packet[1], received_blob_length, peripheral->attribute_offset); 13813deb3ec6SMatthias Ringwald trigger_next_blob_query(peripheral, P_W2_SEND_READ_BLOB_QUERY, received_blob_length); 13825611a760SMatthias Ringwald // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done 13833deb3ec6SMatthias Ringwald break; 13843deb3ec6SMatthias Ringwald case P_W4_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_RESULT: 13853deb3ec6SMatthias Ringwald report_gatt_long_characteristic_descriptor(peripheral, peripheral->attribute_handle, 13863deb3ec6SMatthias Ringwald &packet[1], received_blob_length, 13873deb3ec6SMatthias Ringwald peripheral->attribute_offset); 13883deb3ec6SMatthias Ringwald trigger_next_blob_query(peripheral, P_W2_SEND_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_QUERY, received_blob_length); 13895611a760SMatthias Ringwald // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done 13903deb3ec6SMatthias Ringwald break; 13913deb3ec6SMatthias Ringwald default: 13923deb3ec6SMatthias Ringwald break; 13933deb3ec6SMatthias Ringwald } 13943deb3ec6SMatthias Ringwald break; 13953deb3ec6SMatthias Ringwald } 13963deb3ec6SMatthias Ringwald case ATT_PREPARE_WRITE_RESPONSE: 13973deb3ec6SMatthias Ringwald switch (peripheral->gatt_client_state){ 13983deb3ec6SMatthias Ringwald case P_W4_PREPARE_WRITE_SINGLE_RESULT: 13993deb3ec6SMatthias Ringwald gatt_client_handle_transaction_complete(peripheral); 14003deb3ec6SMatthias Ringwald if (is_value_valid(peripheral, packet, size)){ 1401*9cb80b17SMilanka Ringwald emit_gatt_complete_event(peripheral, ATT_ERROR_SUCCESS); 14023deb3ec6SMatthias Ringwald } else { 14033deb3ec6SMatthias Ringwald emit_gatt_complete_event(peripheral, ATT_ERROR_DATA_MISMATCH); 14043deb3ec6SMatthias Ringwald } 14053deb3ec6SMatthias Ringwald break; 14063deb3ec6SMatthias Ringwald 14073deb3ec6SMatthias Ringwald case P_W4_PREPARE_WRITE_RESULT:{ 1408f8fbdce0SMatthias Ringwald peripheral->attribute_offset = little_endian_read_16(packet, 3); 14093deb3ec6SMatthias Ringwald trigger_next_prepare_write_query(peripheral, P_W2_PREPARE_WRITE, P_W2_EXECUTE_PREPARED_WRITE); 14105611a760SMatthias Ringwald // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done 14113deb3ec6SMatthias Ringwald break; 14123deb3ec6SMatthias Ringwald } 14133deb3ec6SMatthias Ringwald case P_W4_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT:{ 1414f8fbdce0SMatthias Ringwald peripheral->attribute_offset = little_endian_read_16(packet, 3); 14153deb3ec6SMatthias Ringwald trigger_next_prepare_write_query(peripheral, P_W2_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR, P_W2_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR); 14165611a760SMatthias Ringwald // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done 14173deb3ec6SMatthias Ringwald break; 14183deb3ec6SMatthias Ringwald } 14193deb3ec6SMatthias Ringwald case P_W4_PREPARE_RELIABLE_WRITE_RESULT:{ 14203deb3ec6SMatthias Ringwald if (is_value_valid(peripheral, packet, size)){ 1421f8fbdce0SMatthias Ringwald peripheral->attribute_offset = little_endian_read_16(packet, 3); 14223deb3ec6SMatthias Ringwald trigger_next_prepare_write_query(peripheral, P_W2_PREPARE_RELIABLE_WRITE, P_W2_EXECUTE_PREPARED_WRITE); 14235611a760SMatthias Ringwald // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done 14243deb3ec6SMatthias Ringwald break; 14253deb3ec6SMatthias Ringwald } 14263deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W2_CANCEL_PREPARED_WRITE_DATA_MISMATCH; 14273deb3ec6SMatthias Ringwald break; 14283deb3ec6SMatthias Ringwald } 14293deb3ec6SMatthias Ringwald default: 14303deb3ec6SMatthias Ringwald break; 14313deb3ec6SMatthias Ringwald } 14323deb3ec6SMatthias Ringwald break; 14333deb3ec6SMatthias Ringwald 14343deb3ec6SMatthias Ringwald case ATT_EXECUTE_WRITE_RESPONSE: 14353deb3ec6SMatthias Ringwald switch (peripheral->gatt_client_state){ 14363deb3ec6SMatthias Ringwald case P_W4_EXECUTE_PREPARED_WRITE_RESULT: 14373deb3ec6SMatthias Ringwald gatt_client_handle_transaction_complete(peripheral); 1438*9cb80b17SMilanka Ringwald emit_gatt_complete_event(peripheral, ATT_ERROR_SUCCESS); 14393deb3ec6SMatthias Ringwald break; 14403deb3ec6SMatthias Ringwald case P_W4_CANCEL_PREPARED_WRITE_RESULT: 14413deb3ec6SMatthias Ringwald gatt_client_handle_transaction_complete(peripheral); 1442*9cb80b17SMilanka Ringwald emit_gatt_complete_event(peripheral, ATT_ERROR_SUCCESS); 14433deb3ec6SMatthias Ringwald break; 14443deb3ec6SMatthias Ringwald case P_W4_CANCEL_PREPARED_WRITE_DATA_MISMATCH_RESULT: 14453deb3ec6SMatthias Ringwald gatt_client_handle_transaction_complete(peripheral); 14463deb3ec6SMatthias Ringwald emit_gatt_complete_event(peripheral, ATT_ERROR_DATA_MISMATCH); 14473deb3ec6SMatthias Ringwald break; 14483deb3ec6SMatthias Ringwald case P_W4_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT: 14493deb3ec6SMatthias Ringwald gatt_client_handle_transaction_complete(peripheral); 1450*9cb80b17SMilanka Ringwald emit_gatt_complete_event(peripheral, ATT_ERROR_SUCCESS); 14513deb3ec6SMatthias Ringwald break; 14523deb3ec6SMatthias Ringwald default: 14533deb3ec6SMatthias Ringwald break; 14543deb3ec6SMatthias Ringwald 14553deb3ec6SMatthias Ringwald } 14563deb3ec6SMatthias Ringwald break; 14573deb3ec6SMatthias Ringwald 14583deb3ec6SMatthias Ringwald case ATT_READ_MULTIPLE_RESPONSE: 14593deb3ec6SMatthias Ringwald switch(peripheral->gatt_client_state){ 14603deb3ec6SMatthias Ringwald case P_W4_READ_MULTIPLE_RESPONSE: 14613deb3ec6SMatthias Ringwald report_gatt_characteristic_value(peripheral, 0, &packet[1], size-1); 14623deb3ec6SMatthias Ringwald gatt_client_handle_transaction_complete(peripheral); 1463*9cb80b17SMilanka Ringwald emit_gatt_complete_event(peripheral, ATT_ERROR_SUCCESS); 14643deb3ec6SMatthias Ringwald break; 14653deb3ec6SMatthias Ringwald default: 14663deb3ec6SMatthias Ringwald break; 14673deb3ec6SMatthias Ringwald } 14683deb3ec6SMatthias Ringwald break; 14693deb3ec6SMatthias Ringwald 14703deb3ec6SMatthias Ringwald case ATT_ERROR_RESPONSE: 14713deb3ec6SMatthias Ringwald 14723deb3ec6SMatthias Ringwald switch (packet[4]){ 14733deb3ec6SMatthias Ringwald case ATT_ERROR_ATTRIBUTE_NOT_FOUND: { 14743deb3ec6SMatthias Ringwald switch(peripheral->gatt_client_state){ 14753deb3ec6SMatthias Ringwald case P_W4_SERVICE_QUERY_RESULT: 14763deb3ec6SMatthias Ringwald case P_W4_SERVICE_WITH_UUID_RESULT: 14773deb3ec6SMatthias Ringwald case P_W4_INCLUDED_SERVICE_QUERY_RESULT: 14783deb3ec6SMatthias Ringwald case P_W4_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY_RESULT: 14793deb3ec6SMatthias Ringwald gatt_client_handle_transaction_complete(peripheral); 1480*9cb80b17SMilanka Ringwald emit_gatt_complete_event(peripheral, ATT_ERROR_SUCCESS); 14813deb3ec6SMatthias Ringwald break; 14823deb3ec6SMatthias Ringwald case P_W4_ALL_CHARACTERISTICS_OF_SERVICE_QUERY_RESULT: 14833deb3ec6SMatthias Ringwald case P_W4_CHARACTERISTIC_WITH_UUID_QUERY_RESULT: 14843deb3ec6SMatthias Ringwald characteristic_end_found(peripheral, peripheral->end_group_handle); 14853deb3ec6SMatthias Ringwald gatt_client_handle_transaction_complete(peripheral); 1486*9cb80b17SMilanka Ringwald emit_gatt_complete_event(peripheral, ATT_ERROR_SUCCESS); 14873deb3ec6SMatthias Ringwald break; 14883deb3ec6SMatthias Ringwald case P_W4_READ_BY_TYPE_RESPONSE: 14893deb3ec6SMatthias Ringwald gatt_client_handle_transaction_complete(peripheral); 14903deb3ec6SMatthias Ringwald if (peripheral->start_group_handle == peripheral->query_start_handle){ 14913deb3ec6SMatthias Ringwald emit_gatt_complete_event(peripheral, ATT_ERROR_ATTRIBUTE_NOT_FOUND); 14923deb3ec6SMatthias Ringwald } else { 1493*9cb80b17SMilanka Ringwald emit_gatt_complete_event(peripheral, ATT_ERROR_SUCCESS); 14943deb3ec6SMatthias Ringwald } 14953deb3ec6SMatthias Ringwald break; 14963deb3ec6SMatthias Ringwald default: 14973deb3ec6SMatthias Ringwald gatt_client_report_error_if_pending(peripheral, packet[4]); 14983deb3ec6SMatthias Ringwald break; 14993deb3ec6SMatthias Ringwald } 15003deb3ec6SMatthias Ringwald break; 15013deb3ec6SMatthias Ringwald } 1502f4b33574SMatthias Ringwald 1503f4b33574SMatthias Ringwald #ifdef ENABLE_GATT_CLIENT_PAIRING 1504f4b33574SMatthias Ringwald 1505f4b33574SMatthias Ringwald case ATT_ERROR_INSUFFICIENT_AUTHENTICATION: 1506f4b33574SMatthias Ringwald case ATT_ERROR_INSUFFICIENT_ENCRYPTION_KEY_SIZE: 1507a0e0c2beSMatthias Ringwald case ATT_ERROR_INSUFFICIENT_ENCRYPTION: { 1508a0e0c2beSMatthias Ringwald 1509f4b33574SMatthias Ringwald // security too low 1510f4b33574SMatthias Ringwald if (peripheral->security_counter > 0) { 1511f4b33574SMatthias Ringwald gatt_client_report_error_if_pending(peripheral, packet[4]); 1512f4b33574SMatthias Ringwald break; 1513f4b33574SMatthias Ringwald } 1514f4b33574SMatthias Ringwald // start security 1515f4b33574SMatthias Ringwald peripheral->security_counter++; 1516f4b33574SMatthias Ringwald 1517f4b33574SMatthias Ringwald // setup action 1518f4b33574SMatthias Ringwald int retry = 1; 1519f4b33574SMatthias Ringwald switch (peripheral->gatt_client_state){ 1520f4b33574SMatthias Ringwald case P_W4_READ_CHARACTERISTIC_VALUE_RESULT: 1521f4b33574SMatthias Ringwald peripheral->gatt_client_state = P_W2_SEND_READ_CHARACTERISTIC_VALUE_QUERY ; 1522f4b33574SMatthias Ringwald break; 1523f4b33574SMatthias Ringwald case P_W4_READ_BLOB_RESULT: 1524f4b33574SMatthias Ringwald peripheral->gatt_client_state = P_W2_SEND_READ_BLOB_QUERY; 1525f4b33574SMatthias Ringwald break; 1526f4b33574SMatthias Ringwald case P_W4_READ_BY_TYPE_RESPONSE: 1527f4b33574SMatthias Ringwald peripheral->gatt_client_state = P_W2_SEND_READ_BY_TYPE_REQUEST; 1528f4b33574SMatthias Ringwald break; 1529f4b33574SMatthias Ringwald case P_W4_READ_MULTIPLE_RESPONSE: 1530f4b33574SMatthias Ringwald peripheral->gatt_client_state = P_W2_SEND_READ_MULTIPLE_REQUEST; 1531f4b33574SMatthias Ringwald break; 1532f4b33574SMatthias Ringwald case P_W4_WRITE_CHARACTERISTIC_VALUE_RESULT: 1533f4b33574SMatthias Ringwald peripheral->gatt_client_state = P_W2_SEND_WRITE_CHARACTERISTIC_VALUE; 1534f4b33574SMatthias Ringwald break; 1535f4b33574SMatthias Ringwald case P_W4_PREPARE_WRITE_RESULT: 1536f4b33574SMatthias Ringwald peripheral->gatt_client_state = P_W2_PREPARE_WRITE; 1537f4b33574SMatthias Ringwald break; 1538f4b33574SMatthias Ringwald case P_W4_PREPARE_WRITE_SINGLE_RESULT: 1539f4b33574SMatthias Ringwald peripheral->gatt_client_state = P_W2_PREPARE_WRITE_SINGLE; 1540f4b33574SMatthias Ringwald break; 1541f4b33574SMatthias Ringwald case P_W4_PREPARE_RELIABLE_WRITE_RESULT: 1542f4b33574SMatthias Ringwald peripheral->gatt_client_state = P_W2_PREPARE_RELIABLE_WRITE; 1543f4b33574SMatthias Ringwald break; 1544f4b33574SMatthias Ringwald case P_W4_EXECUTE_PREPARED_WRITE_RESULT: 1545f4b33574SMatthias Ringwald peripheral->gatt_client_state = P_W2_EXECUTE_PREPARED_WRITE; 1546f4b33574SMatthias Ringwald break; 1547f4b33574SMatthias Ringwald case P_W4_CANCEL_PREPARED_WRITE_RESULT: 1548f4b33574SMatthias Ringwald peripheral->gatt_client_state = P_W2_CANCEL_PREPARED_WRITE; 1549f4b33574SMatthias Ringwald break; 1550f4b33574SMatthias Ringwald case P_W4_CANCEL_PREPARED_WRITE_DATA_MISMATCH_RESULT: 1551f4b33574SMatthias Ringwald peripheral->gatt_client_state = P_W2_CANCEL_PREPARED_WRITE_DATA_MISMATCH; 1552f4b33574SMatthias Ringwald break; 1553f4b33574SMatthias Ringwald case P_W4_READ_CHARACTERISTIC_DESCRIPTOR_RESULT: 1554f4b33574SMatthias Ringwald peripheral->gatt_client_state = P_W2_SEND_READ_CHARACTERISTIC_DESCRIPTOR_QUERY; 1555f4b33574SMatthias Ringwald break; 1556f4b33574SMatthias Ringwald case P_W4_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_RESULT: 1557f4b33574SMatthias Ringwald peripheral->gatt_client_state = P_W2_SEND_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_QUERY; 1558f4b33574SMatthias Ringwald break; 1559f4b33574SMatthias Ringwald case P_W4_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT: 1560f4b33574SMatthias Ringwald peripheral->gatt_client_state = P_W2_SEND_WRITE_CHARACTERISTIC_DESCRIPTOR; 1561f4b33574SMatthias Ringwald break; 1562f4b33574SMatthias Ringwald case P_W4_CLIENT_CHARACTERISTIC_CONFIGURATION_RESULT: 1563f4b33574SMatthias Ringwald peripheral->gatt_client_state = P_W2_WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION; 1564f4b33574SMatthias Ringwald break; 1565f4b33574SMatthias Ringwald case P_W4_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT: 1566f4b33574SMatthias Ringwald peripheral->gatt_client_state = P_W2_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR; 1567f4b33574SMatthias Ringwald break; 1568f4b33574SMatthias Ringwald case P_W4_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT: 1569f4b33574SMatthias Ringwald peripheral->gatt_client_state = P_W2_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR; 1570f4b33574SMatthias Ringwald break; 1571f4b33574SMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE 1572f4b33574SMatthias Ringwald case P_W4_SEND_SINGED_WRITE_DONE: 1573f4b33574SMatthias Ringwald peripheral->gatt_client_state = P_W2_SEND_SIGNED_WRITE; 1574f4b33574SMatthias Ringwald break; 1575f4b33574SMatthias Ringwald #endif 1576f4b33574SMatthias Ringwald default: 1577f4b33574SMatthias Ringwald log_info("retry not supported for state %x", peripheral->gatt_client_state); 1578f4b33574SMatthias Ringwald retry = 0; 1579f4b33574SMatthias Ringwald break; 1580f4b33574SMatthias Ringwald } 1581f4b33574SMatthias Ringwald 1582f4b33574SMatthias Ringwald if (!retry) { 1583f4b33574SMatthias Ringwald gatt_client_report_error_if_pending(peripheral, packet[4]); 1584f4b33574SMatthias Ringwald break; 1585f4b33574SMatthias Ringwald } 1586f4b33574SMatthias Ringwald 1587f4b33574SMatthias Ringwald log_info("security error, start pairing"); 1588f4b33574SMatthias Ringwald 1589f4b33574SMatthias Ringwald // requrest pairing 1590f4b33574SMatthias Ringwald peripheral->wait_for_pairing_complete = 1; 1591f4b33574SMatthias Ringwald peripheral->pending_error_code = packet[4]; 1592f4b33574SMatthias Ringwald sm_request_pairing(peripheral->con_handle); 1593f4b33574SMatthias Ringwald break; 1594a0e0c2beSMatthias Ringwald } 1595f4b33574SMatthias Ringwald #endif 1596f4b33574SMatthias Ringwald 1597f4b33574SMatthias Ringwald // nothing we can do about that 1598f4b33574SMatthias Ringwald case ATT_ERROR_INSUFFICIENT_AUTHORIZATION: 15993deb3ec6SMatthias Ringwald default: 16003deb3ec6SMatthias Ringwald gatt_client_report_error_if_pending(peripheral, packet[4]); 16013deb3ec6SMatthias Ringwald break; 16023deb3ec6SMatthias Ringwald } 16033deb3ec6SMatthias Ringwald break; 16043deb3ec6SMatthias Ringwald 16053deb3ec6SMatthias Ringwald default: 16063deb3ec6SMatthias Ringwald log_info("ATT Handler, unhandled response type 0x%02x", packet[0]); 16073deb3ec6SMatthias Ringwald break; 16083deb3ec6SMatthias Ringwald } 16093deb3ec6SMatthias Ringwald gatt_client_run(); 16103deb3ec6SMatthias Ringwald } 16113deb3ec6SMatthias Ringwald 16127a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE 16133deb3ec6SMatthias Ringwald static void att_signed_write_handle_cmac_result(uint8_t hash[8]){ 1614665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1615665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &gatt_client_connections); 1616665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1617665d90f2SMatthias Ringwald gatt_client_t * peripheral = (gatt_client_t *) btstack_linked_list_iterator_next(&it); 16183deb3ec6SMatthias Ringwald if (peripheral->gatt_client_state == P_W4_CMAC_RESULT){ 16193deb3ec6SMatthias Ringwald // store result 16203deb3ec6SMatthias Ringwald memcpy(peripheral->cmac, hash, 8); 16219c80e4ccSMatthias Ringwald // reverse_64(hash, peripheral->cmac); 16223deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W2_SEND_SIGNED_WRITE; 16233deb3ec6SMatthias Ringwald gatt_client_run(); 16243deb3ec6SMatthias Ringwald return; 16253deb3ec6SMatthias Ringwald } 16263deb3ec6SMatthias Ringwald } 16273deb3ec6SMatthias Ringwald } 16283deb3ec6SMatthias Ringwald 1629711e6c80SMatthias Ringwald 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){ 16303deb3ec6SMatthias Ringwald gatt_client_t * peripheral = provide_context_for_conn_handle(con_handle); 1631616edd56SMatthias Ringwald if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE; 16323deb3ec6SMatthias Ringwald 16339c662c9bSMatthias Ringwald peripheral->callback = callback; 16343deb3ec6SMatthias Ringwald peripheral->attribute_handle = handle; 16353deb3ec6SMatthias Ringwald peripheral->attribute_length = message_len; 16363deb3ec6SMatthias Ringwald peripheral->attribute_value = message; 1637793cf6ceSMatthias Ringwald peripheral->gatt_client_state = P_W4_IDENTITY_RESOLVING; 16383deb3ec6SMatthias Ringwald 16393deb3ec6SMatthias Ringwald gatt_client_run(); 1640616edd56SMatthias Ringwald return 0; 16413deb3ec6SMatthias Ringwald } 16427a766ebfSMatthias Ringwald #endif 16433deb3ec6SMatthias Ringwald 1644711e6c80SMatthias Ringwald uint8_t gatt_client_discover_primary_services(btstack_packet_handler_t callback, hci_con_handle_t con_handle){ 16453deb3ec6SMatthias Ringwald gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle); 1646616edd56SMatthias Ringwald if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED; 1647616edd56SMatthias Ringwald if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE; 16483deb3ec6SMatthias Ringwald 16499c662c9bSMatthias Ringwald peripheral->callback = callback; 16503deb3ec6SMatthias Ringwald peripheral->start_group_handle = 0x0001; 16513deb3ec6SMatthias Ringwald peripheral->end_group_handle = 0xffff; 16523deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W2_SEND_SERVICE_QUERY; 16533deb3ec6SMatthias Ringwald peripheral->uuid16 = 0; 16543deb3ec6SMatthias Ringwald gatt_client_run(); 1655616edd56SMatthias Ringwald return 0; 16563deb3ec6SMatthias Ringwald } 16573deb3ec6SMatthias Ringwald 16583deb3ec6SMatthias Ringwald 1659711e6c80SMatthias Ringwald uint8_t gatt_client_discover_primary_services_by_uuid16(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t uuid16){ 16603deb3ec6SMatthias Ringwald gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle); 16613deb3ec6SMatthias Ringwald 1662616edd56SMatthias Ringwald if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED; 1663616edd56SMatthias Ringwald if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE; 16643deb3ec6SMatthias Ringwald 16659c662c9bSMatthias Ringwald peripheral->callback = callback; 16663deb3ec6SMatthias Ringwald peripheral->start_group_handle = 0x0001; 16673deb3ec6SMatthias Ringwald peripheral->end_group_handle = 0xffff; 16683deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W2_SEND_SERVICE_WITH_UUID_QUERY; 16693deb3ec6SMatthias Ringwald peripheral->uuid16 = uuid16; 1670e1a125dfSMatthias Ringwald uuid_add_bluetooth_prefix((uint8_t*) &(peripheral->uuid128), peripheral->uuid16); 16713deb3ec6SMatthias Ringwald gatt_client_run(); 1672616edd56SMatthias Ringwald return 0; 16733deb3ec6SMatthias Ringwald } 16743deb3ec6SMatthias Ringwald 1675711e6c80SMatthias Ringwald uint8_t gatt_client_discover_primary_services_by_uuid128(btstack_packet_handler_t callback, hci_con_handle_t con_handle, const uint8_t * uuid128){ 16763deb3ec6SMatthias Ringwald gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle); 16773deb3ec6SMatthias Ringwald 1678616edd56SMatthias Ringwald if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED; 1679616edd56SMatthias Ringwald if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE; 16803deb3ec6SMatthias Ringwald 16819c662c9bSMatthias Ringwald peripheral->callback = callback; 16823deb3ec6SMatthias Ringwald peripheral->start_group_handle = 0x0001; 16833deb3ec6SMatthias Ringwald peripheral->end_group_handle = 0xffff; 16843deb3ec6SMatthias Ringwald peripheral->uuid16 = 0; 16853deb3ec6SMatthias Ringwald memcpy(peripheral->uuid128, uuid128, 16); 16863deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W2_SEND_SERVICE_WITH_UUID_QUERY; 16873deb3ec6SMatthias Ringwald gatt_client_run(); 1688616edd56SMatthias Ringwald return 0; 16893deb3ec6SMatthias Ringwald } 16903deb3ec6SMatthias Ringwald 1691711e6c80SMatthias Ringwald uint8_t gatt_client_discover_characteristics_for_service(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_service_t *service){ 16923deb3ec6SMatthias Ringwald gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle); 16933deb3ec6SMatthias Ringwald 1694616edd56SMatthias Ringwald if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED; 1695616edd56SMatthias Ringwald if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE; 16963deb3ec6SMatthias Ringwald 16979c662c9bSMatthias Ringwald peripheral->callback = callback; 16983deb3ec6SMatthias Ringwald peripheral->start_group_handle = service->start_group_handle; 16993deb3ec6SMatthias Ringwald peripheral->end_group_handle = service->end_group_handle; 17003deb3ec6SMatthias Ringwald peripheral->filter_with_uuid = 0; 17013deb3ec6SMatthias Ringwald peripheral->characteristic_start_handle = 0; 17023deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W2_SEND_ALL_CHARACTERISTICS_OF_SERVICE_QUERY; 17033deb3ec6SMatthias Ringwald gatt_client_run(); 1704616edd56SMatthias Ringwald return 0; 17053deb3ec6SMatthias Ringwald } 17063deb3ec6SMatthias Ringwald 1707711e6c80SMatthias Ringwald 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){ 17083deb3ec6SMatthias Ringwald gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle); 17093deb3ec6SMatthias Ringwald 1710616edd56SMatthias Ringwald if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED; 1711616edd56SMatthias Ringwald if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE; 17123deb3ec6SMatthias Ringwald 17139c662c9bSMatthias Ringwald peripheral->callback = callback; 17143deb3ec6SMatthias Ringwald peripheral->start_group_handle = service->start_group_handle; 17153deb3ec6SMatthias Ringwald peripheral->end_group_handle = service->end_group_handle; 17163deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W2_SEND_INCLUDED_SERVICE_QUERY; 17173deb3ec6SMatthias Ringwald 17183deb3ec6SMatthias Ringwald gatt_client_run(); 1719616edd56SMatthias Ringwald return 0; 17203deb3ec6SMatthias Ringwald } 17213deb3ec6SMatthias Ringwald 1722711e6c80SMatthias Ringwald 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){ 17233deb3ec6SMatthias Ringwald gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle); 17243deb3ec6SMatthias Ringwald 1725616edd56SMatthias Ringwald if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED; 1726616edd56SMatthias Ringwald if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE; 17273deb3ec6SMatthias Ringwald 17289c662c9bSMatthias Ringwald peripheral->callback = callback; 17293deb3ec6SMatthias Ringwald peripheral->start_group_handle = start_handle; 17303deb3ec6SMatthias Ringwald peripheral->end_group_handle = end_handle; 17313deb3ec6SMatthias Ringwald peripheral->filter_with_uuid = 1; 17323deb3ec6SMatthias Ringwald peripheral->uuid16 = uuid16; 1733e1a125dfSMatthias Ringwald uuid_add_bluetooth_prefix((uint8_t*) &(peripheral->uuid128), uuid16); 17343deb3ec6SMatthias Ringwald peripheral->characteristic_start_handle = 0; 17353deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W2_SEND_CHARACTERISTIC_WITH_UUID_QUERY; 17363deb3ec6SMatthias Ringwald 17373deb3ec6SMatthias Ringwald gatt_client_run(); 1738616edd56SMatthias Ringwald return 0; 17393deb3ec6SMatthias Ringwald } 17403deb3ec6SMatthias Ringwald 1741711e6c80SMatthias Ringwald 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){ 17423deb3ec6SMatthias Ringwald gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle); 17433deb3ec6SMatthias Ringwald 1744616edd56SMatthias Ringwald if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED; 1745616edd56SMatthias Ringwald if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE; 17463deb3ec6SMatthias Ringwald 17479c662c9bSMatthias Ringwald peripheral->callback = callback; 17483deb3ec6SMatthias Ringwald peripheral->start_group_handle = start_handle; 17493deb3ec6SMatthias Ringwald peripheral->end_group_handle = end_handle; 17503deb3ec6SMatthias Ringwald peripheral->filter_with_uuid = 1; 17513deb3ec6SMatthias Ringwald peripheral->uuid16 = 0; 17523deb3ec6SMatthias Ringwald memcpy(peripheral->uuid128, uuid128, 16); 17533deb3ec6SMatthias Ringwald peripheral->characteristic_start_handle = 0; 17543deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W2_SEND_CHARACTERISTIC_WITH_UUID_QUERY; 17553deb3ec6SMatthias Ringwald 17563deb3ec6SMatthias Ringwald gatt_client_run(); 1757616edd56SMatthias Ringwald return 0; 17583deb3ec6SMatthias Ringwald } 17593deb3ec6SMatthias Ringwald 17603deb3ec6SMatthias Ringwald 1761d25c33e5SMatthias Ringwald 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){ 17629c662c9bSMatthias Ringwald return gatt_client_discover_characteristics_for_handle_range_by_uuid16(callback, handle, service->start_group_handle, service->end_group_handle, uuid16); 17633deb3ec6SMatthias Ringwald } 17643deb3ec6SMatthias Ringwald 1765d25c33e5SMatthias Ringwald 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){ 17669c662c9bSMatthias Ringwald return gatt_client_discover_characteristics_for_handle_range_by_uuid128(callback, handle, service->start_group_handle, service->end_group_handle, uuid128); 17673deb3ec6SMatthias Ringwald } 17683deb3ec6SMatthias Ringwald 1769711e6c80SMatthias Ringwald uint8_t gatt_client_discover_characteristic_descriptors(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_characteristic_t *characteristic){ 17703deb3ec6SMatthias Ringwald gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle); 17713deb3ec6SMatthias Ringwald 1772616edd56SMatthias Ringwald if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED; 1773616edd56SMatthias Ringwald if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE; 17743deb3ec6SMatthias Ringwald 17753deb3ec6SMatthias Ringwald if (characteristic->value_handle == characteristic->end_handle){ 1776*9cb80b17SMilanka Ringwald emit_gatt_complete_event(peripheral, ATT_ERROR_SUCCESS); 1777616edd56SMatthias Ringwald return 0; 17783deb3ec6SMatthias Ringwald } 17799c662c9bSMatthias Ringwald peripheral->callback = callback; 17803deb3ec6SMatthias Ringwald peripheral->start_group_handle = characteristic->value_handle + 1; 17813deb3ec6SMatthias Ringwald peripheral->end_group_handle = characteristic->end_handle; 17823deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W2_SEND_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY; 17833deb3ec6SMatthias Ringwald 17843deb3ec6SMatthias Ringwald gatt_client_run(); 1785616edd56SMatthias Ringwald return 0; 17863deb3ec6SMatthias Ringwald } 17873deb3ec6SMatthias Ringwald 1788711e6c80SMatthias Ringwald 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){ 17893deb3ec6SMatthias Ringwald gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle); 17903deb3ec6SMatthias Ringwald 1791616edd56SMatthias Ringwald if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED; 1792616edd56SMatthias Ringwald if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE; 17933deb3ec6SMatthias Ringwald 17949c662c9bSMatthias Ringwald peripheral->callback = callback; 17953deb3ec6SMatthias Ringwald peripheral->attribute_handle = value_handle; 17963deb3ec6SMatthias Ringwald peripheral->attribute_offset = 0; 17973deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W2_SEND_READ_CHARACTERISTIC_VALUE_QUERY; 17983deb3ec6SMatthias Ringwald gatt_client_run(); 1799616edd56SMatthias Ringwald return 0; 18003deb3ec6SMatthias Ringwald } 18013deb3ec6SMatthias Ringwald 1802711e6c80SMatthias Ringwald 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){ 18033deb3ec6SMatthias Ringwald gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle); 18043deb3ec6SMatthias Ringwald 1805616edd56SMatthias Ringwald if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED; 1806616edd56SMatthias Ringwald if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE; 18073deb3ec6SMatthias Ringwald 18089c662c9bSMatthias Ringwald peripheral->callback = callback; 18093deb3ec6SMatthias Ringwald peripheral->start_group_handle = start_handle; 18103deb3ec6SMatthias Ringwald peripheral->end_group_handle = end_handle; 18113deb3ec6SMatthias Ringwald peripheral->query_start_handle = start_handle; 18123deb3ec6SMatthias Ringwald peripheral->query_end_handle = end_handle; 18133deb3ec6SMatthias Ringwald peripheral->uuid16 = uuid16; 1814e1a125dfSMatthias Ringwald uuid_add_bluetooth_prefix((uint8_t*) &(peripheral->uuid128), uuid16); 18153deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W2_SEND_READ_BY_TYPE_REQUEST; 18163deb3ec6SMatthias Ringwald gatt_client_run(); 1817616edd56SMatthias Ringwald return 0; 18183deb3ec6SMatthias Ringwald } 18193deb3ec6SMatthias Ringwald 1820711e6c80SMatthias Ringwald 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){ 18213deb3ec6SMatthias Ringwald gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle); 18223deb3ec6SMatthias Ringwald 1823616edd56SMatthias Ringwald if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED; 1824616edd56SMatthias Ringwald if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE; 18253deb3ec6SMatthias Ringwald 18269c662c9bSMatthias Ringwald peripheral->callback = callback; 18273deb3ec6SMatthias Ringwald peripheral->start_group_handle = start_handle; 18283deb3ec6SMatthias Ringwald peripheral->end_group_handle = end_handle; 18293deb3ec6SMatthias Ringwald peripheral->query_start_handle = start_handle; 18303deb3ec6SMatthias Ringwald peripheral->query_end_handle = end_handle; 18313deb3ec6SMatthias Ringwald peripheral->uuid16 = 0; 18323deb3ec6SMatthias Ringwald memcpy(peripheral->uuid128, uuid128, 16); 18333deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W2_SEND_READ_BY_TYPE_REQUEST; 18343deb3ec6SMatthias Ringwald gatt_client_run(); 1835616edd56SMatthias Ringwald return 0; 18363deb3ec6SMatthias Ringwald } 18373deb3ec6SMatthias Ringwald 18383deb3ec6SMatthias Ringwald 1839d25c33e5SMatthias Ringwald uint8_t gatt_client_read_value_of_characteristic(btstack_packet_handler_t callback, uint16_t handle, gatt_client_characteristic_t *characteristic){ 18409c662c9bSMatthias Ringwald return gatt_client_read_value_of_characteristic_using_value_handle(callback, handle, characteristic->value_handle); 18413deb3ec6SMatthias Ringwald } 18423deb3ec6SMatthias Ringwald 1843711e6c80SMatthias Ringwald 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){ 18443deb3ec6SMatthias Ringwald gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle); 18453deb3ec6SMatthias Ringwald 1846616edd56SMatthias Ringwald if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED; 1847616edd56SMatthias Ringwald if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE; 18483deb3ec6SMatthias Ringwald 18499c662c9bSMatthias Ringwald peripheral->callback = callback; 18503deb3ec6SMatthias Ringwald peripheral->attribute_handle = characteristic_value_handle; 18513deb3ec6SMatthias Ringwald peripheral->attribute_offset = offset; 18523deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W2_SEND_READ_BLOB_QUERY; 18533deb3ec6SMatthias Ringwald gatt_client_run(); 1854616edd56SMatthias Ringwald return 0; 18553deb3ec6SMatthias Ringwald } 18563deb3ec6SMatthias Ringwald 1857711e6c80SMatthias Ringwald 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){ 18589c662c9bSMatthias Ringwald return gatt_client_read_long_value_of_characteristic_using_value_handle_with_offset(callback, con_handle, characteristic_value_handle, 0); 18593deb3ec6SMatthias Ringwald } 18603deb3ec6SMatthias Ringwald 1861d25c33e5SMatthias Ringwald uint8_t gatt_client_read_long_value_of_characteristic(btstack_packet_handler_t callback, uint16_t handle, gatt_client_characteristic_t *characteristic){ 18629c662c9bSMatthias Ringwald return gatt_client_read_long_value_of_characteristic_using_value_handle(callback, handle, characteristic->value_handle); 18633deb3ec6SMatthias Ringwald } 18643deb3ec6SMatthias Ringwald 1865711e6c80SMatthias Ringwald 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){ 18663deb3ec6SMatthias Ringwald gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle); 18673deb3ec6SMatthias Ringwald 1868616edd56SMatthias Ringwald if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED; 1869616edd56SMatthias Ringwald if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE; 18703deb3ec6SMatthias Ringwald 18719c662c9bSMatthias Ringwald peripheral->callback = callback; 18723deb3ec6SMatthias Ringwald peripheral->read_multiple_handle_count = num_value_handles; 18733deb3ec6SMatthias Ringwald peripheral->read_multiple_handles = value_handles; 18743deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W2_SEND_READ_MULTIPLE_REQUEST; 18753deb3ec6SMatthias Ringwald gatt_client_run(); 1876616edd56SMatthias Ringwald return 0; 18773deb3ec6SMatthias Ringwald } 18783deb3ec6SMatthias Ringwald 1879de9f8e94SMatthias Ringwald 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){ 18803deb3ec6SMatthias Ringwald gatt_client_t * peripheral = provide_context_for_conn_handle(con_handle); 18813deb3ec6SMatthias Ringwald 1882616edd56SMatthias Ringwald if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED; 1883616edd56SMatthias Ringwald if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE; 18843deb3ec6SMatthias Ringwald 1885616edd56SMatthias Ringwald if (value_length > peripheral_mtu(peripheral) - 3) return GATT_CLIENT_VALUE_TOO_LONG; 188651764b3bSMatthias Ringwald if (!att_dispatch_client_can_send_now(peripheral->con_handle)) return GATT_CLIENT_BUSY; 18873deb3ec6SMatthias Ringwald 1888fc64f94aSMatthias Ringwald att_write_request(ATT_WRITE_COMMAND, peripheral->con_handle, value_handle, value_length, value); 1889616edd56SMatthias Ringwald return 0; 18903deb3ec6SMatthias Ringwald } 18913deb3ec6SMatthias Ringwald 1892711e6c80SMatthias Ringwald 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){ 18933deb3ec6SMatthias Ringwald gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle); 18943deb3ec6SMatthias Ringwald 1895616edd56SMatthias Ringwald if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED; 1896616edd56SMatthias Ringwald if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE; 18973deb3ec6SMatthias Ringwald 18989c662c9bSMatthias Ringwald peripheral->callback = callback; 18993deb3ec6SMatthias Ringwald peripheral->attribute_handle = value_handle; 19003deb3ec6SMatthias Ringwald peripheral->attribute_length = value_length; 19013deb3ec6SMatthias Ringwald peripheral->attribute_value = data; 19023deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W2_SEND_WRITE_CHARACTERISTIC_VALUE; 19033deb3ec6SMatthias Ringwald gatt_client_run(); 1904616edd56SMatthias Ringwald return 0; 19053deb3ec6SMatthias Ringwald } 19063deb3ec6SMatthias Ringwald 1907711e6c80SMatthias Ringwald 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){ 19083deb3ec6SMatthias Ringwald gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle); 19093deb3ec6SMatthias Ringwald 1910616edd56SMatthias Ringwald if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED; 1911616edd56SMatthias Ringwald if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE; 19123deb3ec6SMatthias Ringwald 19139c662c9bSMatthias Ringwald peripheral->callback = callback; 19143deb3ec6SMatthias Ringwald peripheral->attribute_handle = value_handle; 19153deb3ec6SMatthias Ringwald peripheral->attribute_length = value_length; 19163deb3ec6SMatthias Ringwald peripheral->attribute_offset = offset; 19173deb3ec6SMatthias Ringwald peripheral->attribute_value = data; 19183deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W2_PREPARE_WRITE; 19193deb3ec6SMatthias Ringwald gatt_client_run(); 1920616edd56SMatthias Ringwald return 0; 19213deb3ec6SMatthias Ringwald } 19223deb3ec6SMatthias Ringwald 1923711e6c80SMatthias Ringwald 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){ 19249c662c9bSMatthias Ringwald return gatt_client_write_long_value_of_characteristic_with_offset(callback, con_handle, value_handle, 0, value_length, value); 19253deb3ec6SMatthias Ringwald } 19263deb3ec6SMatthias Ringwald 1927711e6c80SMatthias Ringwald 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){ 19283deb3ec6SMatthias Ringwald gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle); 19293deb3ec6SMatthias Ringwald 1930616edd56SMatthias Ringwald if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED; 1931616edd56SMatthias Ringwald if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE; 19323deb3ec6SMatthias Ringwald 19339c662c9bSMatthias Ringwald peripheral->callback = callback; 19343deb3ec6SMatthias Ringwald peripheral->attribute_handle = value_handle; 19353deb3ec6SMatthias Ringwald peripheral->attribute_length = value_length; 19363deb3ec6SMatthias Ringwald peripheral->attribute_offset = 0; 19373deb3ec6SMatthias Ringwald peripheral->attribute_value = value; 19383deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W2_PREPARE_RELIABLE_WRITE; 19393deb3ec6SMatthias Ringwald gatt_client_run(); 1940616edd56SMatthias Ringwald return 0; 19413deb3ec6SMatthias Ringwald } 19423deb3ec6SMatthias Ringwald 1943711e6c80SMatthias Ringwald 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){ 19443deb3ec6SMatthias Ringwald gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle); 19453deb3ec6SMatthias Ringwald 1946616edd56SMatthias Ringwald if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED; 1947616edd56SMatthias Ringwald if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE; 19483deb3ec6SMatthias Ringwald 19493deb3ec6SMatthias Ringwald if ( (configuration & GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION) && 19503deb3ec6SMatthias Ringwald (characteristic->properties & ATT_PROPERTY_NOTIFY) == 0) { 1951d8e8f12aSMatthias Ringwald log_info("gatt_client_write_client_characteristic_configuration: GATT_CLIENT_CHARACTERISTIC_NOTIFICATION_NOT_SUPPORTED"); 1952616edd56SMatthias Ringwald return GATT_CLIENT_CHARACTERISTIC_NOTIFICATION_NOT_SUPPORTED; 19533deb3ec6SMatthias Ringwald } else if ( (configuration & GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_INDICATION) && 19543deb3ec6SMatthias Ringwald (characteristic->properties & ATT_PROPERTY_INDICATE) == 0){ 1955d8e8f12aSMatthias Ringwald log_info("gatt_client_write_client_characteristic_configuration: GATT_CLIENT_CHARACTERISTIC_INDICATION_NOT_SUPPORTED"); 1956616edd56SMatthias Ringwald return GATT_CLIENT_CHARACTERISTIC_INDICATION_NOT_SUPPORTED; 19573deb3ec6SMatthias Ringwald } 19583deb3ec6SMatthias Ringwald 19599c662c9bSMatthias Ringwald peripheral->callback = callback; 19603deb3ec6SMatthias Ringwald peripheral->start_group_handle = characteristic->value_handle; 19613deb3ec6SMatthias Ringwald peripheral->end_group_handle = characteristic->end_handle; 1962f8fbdce0SMatthias Ringwald little_endian_store_16(peripheral->client_characteristic_configuration_value, 0, configuration); 19633deb3ec6SMatthias Ringwald 1964abdc9fb5SMatthias Ringwald #ifdef ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERY 1965abdc9fb5SMatthias Ringwald peripheral->gatt_client_state = P_W2_SEND_FIND_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY; 1966abdc9fb5SMatthias Ringwald #else 19673deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W2_SEND_READ_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY; 1968abdc9fb5SMatthias Ringwald #endif 19693deb3ec6SMatthias Ringwald gatt_client_run(); 1970*9cb80b17SMilanka Ringwald return ERROR_CODE_SUCCESS; 19713deb3ec6SMatthias Ringwald } 19723deb3ec6SMatthias Ringwald 1973711e6c80SMatthias Ringwald 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){ 19743deb3ec6SMatthias Ringwald gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle); 19753deb3ec6SMatthias Ringwald 1976616edd56SMatthias Ringwald if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED; 1977616edd56SMatthias Ringwald if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE; 19783deb3ec6SMatthias Ringwald 19799c662c9bSMatthias Ringwald peripheral->callback = callback; 19803deb3ec6SMatthias Ringwald peripheral->attribute_handle = descriptor_handle; 19813deb3ec6SMatthias Ringwald 19823deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W2_SEND_READ_CHARACTERISTIC_DESCRIPTOR_QUERY; 19833deb3ec6SMatthias Ringwald gatt_client_run(); 1984616edd56SMatthias Ringwald return 0; 19853deb3ec6SMatthias Ringwald } 19863deb3ec6SMatthias Ringwald 1987711e6c80SMatthias Ringwald uint8_t gatt_client_read_characteristic_descriptor(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_characteristic_descriptor_t * descriptor){ 19889c662c9bSMatthias Ringwald return gatt_client_read_characteristic_descriptor_using_descriptor_handle(callback, con_handle, descriptor->handle); 19893deb3ec6SMatthias Ringwald } 19903deb3ec6SMatthias Ringwald 1991711e6c80SMatthias Ringwald 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){ 19923deb3ec6SMatthias Ringwald gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle); 19933deb3ec6SMatthias Ringwald 1994616edd56SMatthias Ringwald if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED; 1995616edd56SMatthias Ringwald if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE; 19963deb3ec6SMatthias Ringwald 19979c662c9bSMatthias Ringwald peripheral->callback = callback; 19983deb3ec6SMatthias Ringwald peripheral->attribute_handle = descriptor_handle; 19993deb3ec6SMatthias Ringwald peripheral->attribute_offset = offset; 20003deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W2_SEND_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_QUERY; 20013deb3ec6SMatthias Ringwald gatt_client_run(); 2002616edd56SMatthias Ringwald return 0; 20033deb3ec6SMatthias Ringwald } 20043deb3ec6SMatthias Ringwald 2005711e6c80SMatthias Ringwald 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){ 20069c662c9bSMatthias Ringwald return gatt_client_read_long_characteristic_descriptor_using_descriptor_handle_with_offset(callback, con_handle, descriptor_handle, 0); 20073deb3ec6SMatthias Ringwald } 20083deb3ec6SMatthias Ringwald 2009711e6c80SMatthias Ringwald 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){ 20109c662c9bSMatthias Ringwald return gatt_client_read_long_characteristic_descriptor_using_descriptor_handle(callback, con_handle, descriptor->handle); 20113deb3ec6SMatthias Ringwald } 20123deb3ec6SMatthias Ringwald 2013711e6c80SMatthias Ringwald 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){ 20143deb3ec6SMatthias Ringwald gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle); 20153deb3ec6SMatthias Ringwald 2016616edd56SMatthias Ringwald if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED; 2017616edd56SMatthias Ringwald if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE; 20183deb3ec6SMatthias Ringwald 20199c662c9bSMatthias Ringwald peripheral->callback = callback; 20203deb3ec6SMatthias Ringwald peripheral->attribute_handle = descriptor_handle; 20213deb3ec6SMatthias Ringwald peripheral->attribute_length = length; 20223deb3ec6SMatthias Ringwald peripheral->attribute_offset = 0; 20233deb3ec6SMatthias Ringwald peripheral->attribute_value = data; 20243deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W2_SEND_WRITE_CHARACTERISTIC_DESCRIPTOR; 20253deb3ec6SMatthias Ringwald gatt_client_run(); 2026616edd56SMatthias Ringwald return 0; 20273deb3ec6SMatthias Ringwald } 20283deb3ec6SMatthias Ringwald 2029711e6c80SMatthias Ringwald 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){ 20309c662c9bSMatthias Ringwald return gatt_client_write_characteristic_descriptor_using_descriptor_handle(callback, con_handle, descriptor->handle, length, value); 20313deb3ec6SMatthias Ringwald } 20323deb3ec6SMatthias Ringwald 2033711e6c80SMatthias Ringwald 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){ 20343deb3ec6SMatthias Ringwald gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle); 20353deb3ec6SMatthias Ringwald 2036616edd56SMatthias Ringwald if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED; 2037616edd56SMatthias Ringwald if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE; 20383deb3ec6SMatthias Ringwald 20399c662c9bSMatthias Ringwald peripheral->callback = callback; 20403deb3ec6SMatthias Ringwald peripheral->attribute_handle = descriptor_handle; 20413deb3ec6SMatthias Ringwald peripheral->attribute_length = length; 20423deb3ec6SMatthias Ringwald peripheral->attribute_offset = offset; 20433deb3ec6SMatthias Ringwald peripheral->attribute_value = data; 20443deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W2_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR; 20453deb3ec6SMatthias Ringwald gatt_client_run(); 2046616edd56SMatthias Ringwald return 0; 20473deb3ec6SMatthias Ringwald } 20483deb3ec6SMatthias Ringwald 2049711e6c80SMatthias Ringwald 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){ 20509c662c9bSMatthias Ringwald return gatt_client_write_long_characteristic_descriptor_using_descriptor_handle_with_offset(callback, con_handle, descriptor_handle, 0, length, data ); 20513deb3ec6SMatthias Ringwald } 20523deb3ec6SMatthias Ringwald 2053711e6c80SMatthias Ringwald 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){ 20549c662c9bSMatthias Ringwald return gatt_client_write_long_characteristic_descriptor_using_descriptor_handle(callback, con_handle, descriptor->handle, length, value); 20553deb3ec6SMatthias Ringwald } 20563deb3ec6SMatthias Ringwald 20573deb3ec6SMatthias Ringwald /** 20583deb3ec6SMatthias Ringwald * @brief -> gatt complete event 20593deb3ec6SMatthias Ringwald */ 2060711e6c80SMatthias Ringwald 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){ 20613deb3ec6SMatthias Ringwald gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle); 20623deb3ec6SMatthias Ringwald 2063616edd56SMatthias Ringwald if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED; 2064616edd56SMatthias Ringwald if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE; 20653deb3ec6SMatthias Ringwald 20669c662c9bSMatthias Ringwald peripheral->callback = callback; 20673deb3ec6SMatthias Ringwald peripheral->attribute_handle = attribute_handle; 20683deb3ec6SMatthias Ringwald peripheral->attribute_length = length; 20693deb3ec6SMatthias Ringwald peripheral->attribute_offset = offset; 20703deb3ec6SMatthias Ringwald peripheral->attribute_value = data; 20713deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W2_PREPARE_WRITE_SINGLE; 20723deb3ec6SMatthias Ringwald gatt_client_run(); 2073616edd56SMatthias Ringwald return 0; 20743deb3ec6SMatthias Ringwald } 20753deb3ec6SMatthias Ringwald 20763deb3ec6SMatthias Ringwald /** 20773deb3ec6SMatthias Ringwald * @brief -> gatt complete event 20783deb3ec6SMatthias Ringwald */ 2079711e6c80SMatthias Ringwald uint8_t gatt_client_execute_write(btstack_packet_handler_t callback, hci_con_handle_t con_handle){ 20803deb3ec6SMatthias Ringwald gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle); 20813deb3ec6SMatthias Ringwald 2082616edd56SMatthias Ringwald if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED; 2083616edd56SMatthias Ringwald if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE; 20843deb3ec6SMatthias Ringwald 20859c662c9bSMatthias Ringwald peripheral->callback = callback; 20863deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W2_EXECUTE_PREPARED_WRITE; 20873deb3ec6SMatthias Ringwald gatt_client_run(); 2088616edd56SMatthias Ringwald return 0; 20893deb3ec6SMatthias Ringwald } 20903deb3ec6SMatthias Ringwald 20913deb3ec6SMatthias Ringwald /** 20923deb3ec6SMatthias Ringwald * @brief -> gatt complete event 20933deb3ec6SMatthias Ringwald */ 2094711e6c80SMatthias Ringwald uint8_t gatt_client_cancel_write(btstack_packet_handler_t callback, hci_con_handle_t con_handle){ 20953deb3ec6SMatthias Ringwald gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle); 20963deb3ec6SMatthias Ringwald 2097616edd56SMatthias Ringwald if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED; 2098616edd56SMatthias Ringwald if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE; 20993deb3ec6SMatthias Ringwald 21009c662c9bSMatthias Ringwald peripheral->callback = callback; 21013deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W2_CANCEL_PREPARED_WRITE; 21023deb3ec6SMatthias Ringwald gatt_client_run(); 2103616edd56SMatthias Ringwald return 0; 21043deb3ec6SMatthias Ringwald } 21053deb3ec6SMatthias Ringwald 2106313e337bSMatthias Ringwald void gatt_client_deserialize_service(const uint8_t *packet, int offset, gatt_client_service_t *service){ 21076ba2ad22SMatthias Ringwald service->start_group_handle = little_endian_read_16(packet, offset); 21086ba2ad22SMatthias Ringwald service->end_group_handle = little_endian_read_16(packet, offset + 2); 21096ba2ad22SMatthias Ringwald reverse_128(&packet[offset + 4], service->uuid128); 21106ba2ad22SMatthias Ringwald if (uuid_has_bluetooth_prefix(service->uuid128)){ 21116ba2ad22SMatthias Ringwald service->uuid16 = big_endian_read_32(service->uuid128, 0); 21126ba2ad22SMatthias Ringwald } 21136ba2ad22SMatthias Ringwald } 21146ba2ad22SMatthias Ringwald 2115313e337bSMatthias Ringwald void gatt_client_deserialize_characteristic(const uint8_t * packet, int offset, gatt_client_characteristic_t * characteristic){ 21166ba2ad22SMatthias Ringwald characteristic->start_handle = little_endian_read_16(packet, offset); 21176ba2ad22SMatthias Ringwald characteristic->value_handle = little_endian_read_16(packet, offset + 2); 21186ba2ad22SMatthias Ringwald characteristic->end_handle = little_endian_read_16(packet, offset + 4); 21196ba2ad22SMatthias Ringwald characteristic->properties = little_endian_read_16(packet, offset + 6); 212086c38559SMatthias Ringwald reverse_128(&packet[offset+8], characteristic->uuid128); 21216ba2ad22SMatthias Ringwald if (uuid_has_bluetooth_prefix(characteristic->uuid128)){ 21226ba2ad22SMatthias Ringwald characteristic->uuid16 = big_endian_read_32(characteristic->uuid128, 0); 21236ba2ad22SMatthias Ringwald } 21246ba2ad22SMatthias Ringwald } 21256ba2ad22SMatthias Ringwald 2126313e337bSMatthias Ringwald void gatt_client_deserialize_characteristic_descriptor(const uint8_t * packet, int offset, gatt_client_characteristic_descriptor_t * descriptor){ 21276ba2ad22SMatthias Ringwald descriptor->handle = little_endian_read_16(packet, offset); 21286ba2ad22SMatthias Ringwald reverse_128(&packet[offset+2], descriptor->uuid128); 2129b4895529SJakob Krantz if (uuid_has_bluetooth_prefix(descriptor->uuid128)){ 2130b4895529SJakob Krantz descriptor->uuid16 = big_endian_read_32(descriptor->uuid128, 0); 2131b4895529SJakob Krantz } 21326ba2ad22SMatthias Ringwald } 21335cf6c434SJakob Krantz 21345cf6c434SJakob Krantz void gatt_client_send_mtu_negotiation(btstack_packet_handler_t callback, hci_con_handle_t con_handle){ 21355cf6c434SJakob Krantz gatt_client_t * context = provide_context_for_conn_handle(con_handle); 21365cf6c434SJakob Krantz if (!context) return; 21375cf6c434SJakob Krantz if (context->mtu_state == MTU_AUTO_EXCHANGE_DISABLED){ 21385cf6c434SJakob Krantz context->callback = callback; 21395cf6c434SJakob Krantz context->mtu_state = SEND_MTU_EXCHANGE; 21405cf6c434SJakob Krantz gatt_client_run(); 21415cf6c434SJakob Krantz } 21425cf6c434SJakob Krantz } 214347181045SMatthias Ringwald 214447181045SMatthias Ringwald uint8_t gatt_client_request_can_write_without_response_event(btstack_packet_handler_t callback, hci_con_handle_t con_handle){ 214547181045SMatthias Ringwald gatt_client_t * context = provide_context_for_conn_handle(con_handle); 214647181045SMatthias Ringwald if (!context) return BTSTACK_MEMORY_ALLOC_FAILED; 214747181045SMatthias Ringwald if (context->write_without_response_callback) return GATT_CLIENT_IN_WRONG_STATE; 214847181045SMatthias Ringwald context->write_without_response_callback = callback; 214947181045SMatthias Ringwald att_dispatch_client_request_can_send_now_event(context->con_handle); 215047181045SMatthias Ringwald return 0; 215147181045SMatthias Ringwald } 2152