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 38ab2c6ae4SMatthias Ringwald #define __BTSTACK_FILE__ "gatt_client.c" 39ab2c6ae4SMatthias Ringwald 403deb3ec6SMatthias Ringwald #include <stdint.h> 413deb3ec6SMatthias Ringwald #include <stdio.h> 423deb3ec6SMatthias Ringwald #include <stdlib.h> 433deb3ec6SMatthias Ringwald #include <string.h> 443deb3ec6SMatthias Ringwald 457907f069SMatthias Ringwald #include "btstack_config.h" 463deb3ec6SMatthias Ringwald 470e2df43fSMatthias Ringwald #include "att_dispatch.h" 48fac60feaSMatthias Ringwald #include "ad_parser.h" 490e2df43fSMatthias Ringwald #include "ble/att_db.h" 5059c6af15SMatthias Ringwald #include "ble/core.h" 510e2df43fSMatthias Ringwald #include "ble/gatt_client.h" 520e2df43fSMatthias Ringwald #include "ble/le_device_db.h" 530e2df43fSMatthias Ringwald #include "ble/sm.h" 5416ece135SMatthias Ringwald #include "btstack_debug.h" 550e2df43fSMatthias Ringwald #include "btstack_event.h" 563deb3ec6SMatthias Ringwald #include "btstack_memory.h" 570e2df43fSMatthias Ringwald #include "btstack_run_loop.h" 580e2df43fSMatthias Ringwald #include "btstack_util.h" 590e2df43fSMatthias Ringwald #include "classic/sdp_util.h" 603deb3ec6SMatthias Ringwald #include "hci.h" 610e2df43fSMatthias Ringwald #include "hci_cmd.h" 623deb3ec6SMatthias Ringwald #include "hci_dump.h" 633deb3ec6SMatthias Ringwald #include "l2cap.h" 643deb3ec6SMatthias Ringwald 659c662c9bSMatthias Ringwald static btstack_linked_list_t gatt_client_connections; 669c662c9bSMatthias Ringwald static btstack_linked_list_t gatt_client_value_listeners; 67361b1363SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 683deb3ec6SMatthias Ringwald 69f4b33574SMatthias Ringwald #ifdef ENABLE_GATT_CLIENT_PAIRING 70f4b33574SMatthias Ringwald static btstack_packet_callback_registration_t sm_event_callback_registration; 71f4b33574SMatthias Ringwald #endif 72f4b33574SMatthias Ringwald 735cf6c434SJakob Krantz static uint8_t mtu_exchange_enabled; 745cf6c434SJakob Krantz 753deb3ec6SMatthias Ringwald static void gatt_client_att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size); 76f4b33574SMatthias Ringwald static void gatt_client_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 773deb3ec6SMatthias Ringwald static void gatt_client_report_error_if_pending(gatt_client_t *peripheral, uint8_t error_code); 787a766ebfSMatthias Ringwald 797a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE 803deb3ec6SMatthias Ringwald static void att_signed_write_handle_cmac_result(uint8_t hash[8]); 817a766ebfSMatthias Ringwald #endif 823deb3ec6SMatthias Ringwald 833deb3ec6SMatthias Ringwald static uint16_t peripheral_mtu(gatt_client_t *peripheral){ 843deb3ec6SMatthias Ringwald if (peripheral->mtu > l2cap_max_le_mtu()){ 853deb3ec6SMatthias Ringwald log_error("Peripheral mtu is not initialized"); 863deb3ec6SMatthias Ringwald return l2cap_max_le_mtu(); 873deb3ec6SMatthias Ringwald } 883deb3ec6SMatthias Ringwald return peripheral->mtu; 893deb3ec6SMatthias Ringwald } 903deb3ec6SMatthias Ringwald 913deb3ec6SMatthias Ringwald void gatt_client_init(void){ 923deb3ec6SMatthias Ringwald gatt_client_connections = NULL; 935cf6c434SJakob Krantz mtu_exchange_enabled = 1; 94f4b33574SMatthias Ringwald 95361b1363SMatthias Ringwald // regsister for HCI Events 96f4b33574SMatthias Ringwald hci_event_callback_registration.callback = &gatt_client_event_packet_handler; 97361b1363SMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 98361b1363SMatthias Ringwald 99f4b33574SMatthias Ringwald #ifdef ENABLE_GATT_CLIENT_PAIRING 100f4b33574SMatthias Ringwald // register for SM Events 101f4b33574SMatthias Ringwald sm_event_callback_registration.callback = &gatt_client_event_packet_handler; 102f4b33574SMatthias Ringwald sm_add_event_handler(&sm_event_callback_registration); 103f4b33574SMatthias Ringwald #endif 104f4b33574SMatthias Ringwald 105361b1363SMatthias Ringwald // and ATT Client PDUs 1063deb3ec6SMatthias Ringwald att_dispatch_register_client(gatt_client_att_packet_handler); 1073deb3ec6SMatthias Ringwald } 1083deb3ec6SMatthias Ringwald 109ec820d77SMatthias Ringwald static gatt_client_t * gatt_client_for_timer(btstack_timer_source_t * ts){ 110665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 111665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &gatt_client_connections); 112665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 113665d90f2SMatthias Ringwald gatt_client_t * peripheral = (gatt_client_t *) btstack_linked_list_iterator_next(&it); 1143deb3ec6SMatthias Ringwald if ( &peripheral->gc_timeout == ts) { 1153deb3ec6SMatthias Ringwald return peripheral; 1163deb3ec6SMatthias Ringwald } 1173deb3ec6SMatthias Ringwald } 1183deb3ec6SMatthias Ringwald return NULL; 1193deb3ec6SMatthias Ringwald } 1203deb3ec6SMatthias Ringwald 121ec820d77SMatthias Ringwald static void gatt_client_timeout_handler(btstack_timer_source_t * timer){ 1223deb3ec6SMatthias Ringwald gatt_client_t * peripheral = gatt_client_for_timer(timer); 1233deb3ec6SMatthias Ringwald if (!peripheral) return; 124fc64f94aSMatthias Ringwald log_info("GATT client timeout handle, handle 0x%02x", peripheral->con_handle); 1253deb3ec6SMatthias Ringwald gatt_client_report_error_if_pending(peripheral, ATT_ERROR_TIMEOUT); 1263deb3ec6SMatthias Ringwald } 1273deb3ec6SMatthias Ringwald 1283deb3ec6SMatthias Ringwald static void gatt_client_timeout_start(gatt_client_t * peripheral){ 129fc64f94aSMatthias Ringwald log_info("GATT client timeout start, handle 0x%02x", peripheral->con_handle); 130528a4a3bSMatthias Ringwald btstack_run_loop_remove_timer(&peripheral->gc_timeout); 131528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&peripheral->gc_timeout, gatt_client_timeout_handler); 132528a4a3bSMatthias Ringwald btstack_run_loop_set_timer(&peripheral->gc_timeout, 30000); // 30 seconds sm timeout 133528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&peripheral->gc_timeout); 1343deb3ec6SMatthias Ringwald } 1353deb3ec6SMatthias Ringwald 1363deb3ec6SMatthias Ringwald static void gatt_client_timeout_stop(gatt_client_t * peripheral){ 137fc64f94aSMatthias Ringwald log_info("GATT client timeout stop, handle 0x%02x", peripheral->con_handle); 138528a4a3bSMatthias Ringwald btstack_run_loop_remove_timer(&peripheral->gc_timeout); 1393deb3ec6SMatthias Ringwald } 1403deb3ec6SMatthias Ringwald 1413deb3ec6SMatthias Ringwald static gatt_client_t * get_gatt_client_context_for_handle(uint16_t handle){ 142665d90f2SMatthias Ringwald btstack_linked_item_t *it; 143665d90f2SMatthias Ringwald for (it = (btstack_linked_item_t *) gatt_client_connections; it ; it = it->next){ 1443deb3ec6SMatthias Ringwald gatt_client_t * peripheral = (gatt_client_t *) it; 145fc64f94aSMatthias Ringwald if (peripheral->con_handle == handle){ 1463deb3ec6SMatthias Ringwald return peripheral; 1473deb3ec6SMatthias Ringwald } 1483deb3ec6SMatthias Ringwald } 1493deb3ec6SMatthias Ringwald return NULL; 1503deb3ec6SMatthias Ringwald } 1513deb3ec6SMatthias Ringwald 1523deb3ec6SMatthias Ringwald 1533deb3ec6SMatthias Ringwald // @returns context 1543deb3ec6SMatthias Ringwald // returns existing one, or tries to setup new one 155711e6c80SMatthias Ringwald static gatt_client_t * provide_context_for_conn_handle(hci_con_handle_t con_handle){ 1563deb3ec6SMatthias Ringwald gatt_client_t * context = get_gatt_client_context_for_handle(con_handle); 1573deb3ec6SMatthias Ringwald if (context) return context; 1583deb3ec6SMatthias Ringwald 159ae1d1237SMatthias Ringwald // bail if no such hci connection 160ae1d1237SMatthias Ringwald if (!hci_connection_for_handle(con_handle)){ 161ae1d1237SMatthias Ringwald log_error("No connection for handle 0x%04x", con_handle); 162ae1d1237SMatthias Ringwald return NULL; 163ae1d1237SMatthias Ringwald } 1643deb3ec6SMatthias Ringwald context = btstack_memory_gatt_client_get(); 1653deb3ec6SMatthias Ringwald if (!context) return NULL; 1663deb3ec6SMatthias Ringwald // init state 1673deb3ec6SMatthias Ringwald memset(context, 0, sizeof(gatt_client_t)); 168fc64f94aSMatthias Ringwald context->con_handle = con_handle; 1693deb3ec6SMatthias Ringwald context->mtu = ATT_DEFAULT_MTU; 1705cf6c434SJakob Krantz if (mtu_exchange_enabled){ 1713deb3ec6SMatthias Ringwald context->mtu_state = SEND_MTU_EXCHANGE; 1725cf6c434SJakob Krantz } else { 1735cf6c434SJakob Krantz context->mtu_state = MTU_AUTO_EXCHANGE_DISABLED; 1745cf6c434SJakob Krantz } 1753deb3ec6SMatthias Ringwald context->gatt_client_state = P_READY; 176665d90f2SMatthias Ringwald btstack_linked_list_add(&gatt_client_connections, (btstack_linked_item_t*)context); 1773deb3ec6SMatthias Ringwald return context; 1783deb3ec6SMatthias Ringwald } 1793deb3ec6SMatthias Ringwald 180711e6c80SMatthias Ringwald static gatt_client_t * provide_context_for_conn_handle_and_start_timer(hci_con_handle_t con_handle){ 1813deb3ec6SMatthias Ringwald gatt_client_t * context = provide_context_for_conn_handle(con_handle); 1823deb3ec6SMatthias Ringwald if (!context) return NULL; 1833deb3ec6SMatthias Ringwald gatt_client_timeout_start(context); 1843deb3ec6SMatthias Ringwald return context; 1853deb3ec6SMatthias Ringwald } 1863deb3ec6SMatthias Ringwald 1873deb3ec6SMatthias Ringwald static int is_ready(gatt_client_t * context){ 1883deb3ec6SMatthias Ringwald return context->gatt_client_state == P_READY; 1893deb3ec6SMatthias Ringwald } 1903deb3ec6SMatthias Ringwald 191fc64f94aSMatthias Ringwald int gatt_client_is_ready(hci_con_handle_t con_handle){ 192fc64f94aSMatthias Ringwald gatt_client_t * context = provide_context_for_conn_handle(con_handle); 1933deb3ec6SMatthias Ringwald if (!context) return 0; 1943deb3ec6SMatthias Ringwald return is_ready(context); 1953deb3ec6SMatthias Ringwald } 1963deb3ec6SMatthias Ringwald 1975cf6c434SJakob Krantz void gatt_client_mtu_enable_auto_negotiation(uint8_t enabled){ 1985cf6c434SJakob Krantz mtu_exchange_enabled = enabled; 1995cf6c434SJakob Krantz } 2005cf6c434SJakob Krantz 201fc64f94aSMatthias Ringwald uint8_t gatt_client_get_mtu(hci_con_handle_t con_handle, uint16_t * mtu){ 202fc64f94aSMatthias Ringwald gatt_client_t * context = provide_context_for_conn_handle(con_handle); 2035cf6c434SJakob Krantz if (context && (context->mtu_state == MTU_EXCHANGED || context->mtu_state == MTU_AUTO_EXCHANGE_DISABLED)){ 2043deb3ec6SMatthias Ringwald *mtu = context->mtu; 205616edd56SMatthias Ringwald return 0; 2063deb3ec6SMatthias Ringwald } 2073deb3ec6SMatthias Ringwald *mtu = ATT_DEFAULT_MTU; 208616edd56SMatthias Ringwald return GATT_CLIENT_IN_WRONG_STATE; 2093deb3ec6SMatthias Ringwald } 2103deb3ec6SMatthias Ringwald 2113deb3ec6SMatthias Ringwald // precondition: can_send_packet_now == TRUE 2123deb3ec6SMatthias Ringwald static void att_confirmation(uint16_t peripheral_handle){ 2133deb3ec6SMatthias Ringwald l2cap_reserve_packet_buffer(); 2143deb3ec6SMatthias Ringwald uint8_t * request = l2cap_get_outgoing_buffer(); 2153deb3ec6SMatthias Ringwald request[0] = ATT_HANDLE_VALUE_CONFIRMATION; 2163deb3ec6SMatthias Ringwald l2cap_send_prepared_connectionless(peripheral_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, 1); 2173deb3ec6SMatthias Ringwald } 2183deb3ec6SMatthias Ringwald 2193deb3ec6SMatthias Ringwald // precondition: can_send_packet_now == TRUE 2203deb3ec6SMatthias Ringwald static void att_find_information_request(uint16_t request_type, uint16_t peripheral_handle, uint16_t start_handle, uint16_t end_handle){ 2213deb3ec6SMatthias Ringwald l2cap_reserve_packet_buffer(); 2223deb3ec6SMatthias Ringwald uint8_t * request = l2cap_get_outgoing_buffer(); 2233deb3ec6SMatthias Ringwald request[0] = request_type; 224f8fbdce0SMatthias Ringwald little_endian_store_16(request, 1, start_handle); 225f8fbdce0SMatthias Ringwald little_endian_store_16(request, 3, end_handle); 2263deb3ec6SMatthias Ringwald 2273deb3ec6SMatthias Ringwald l2cap_send_prepared_connectionless(peripheral_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, 5); 2283deb3ec6SMatthias Ringwald } 2293deb3ec6SMatthias Ringwald 2303deb3ec6SMatthias Ringwald // precondition: can_send_packet_now == TRUE 2313deb3ec6SMatthias 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){ 2323deb3ec6SMatthias Ringwald l2cap_reserve_packet_buffer(); 2333deb3ec6SMatthias Ringwald uint8_t * request = l2cap_get_outgoing_buffer(); 2343deb3ec6SMatthias Ringwald 2353deb3ec6SMatthias Ringwald request[0] = request_type; 236f8fbdce0SMatthias Ringwald little_endian_store_16(request, 1, start_handle); 237f8fbdce0SMatthias Ringwald little_endian_store_16(request, 3, end_handle); 238f8fbdce0SMatthias Ringwald little_endian_store_16(request, 5, attribute_group_type); 2393deb3ec6SMatthias Ringwald memcpy(&request[7], value, value_size); 2403deb3ec6SMatthias Ringwald 2413deb3ec6SMatthias Ringwald l2cap_send_prepared_connectionless(peripheral_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, 7+value_size); 2423deb3ec6SMatthias Ringwald } 2433deb3ec6SMatthias Ringwald 2443deb3ec6SMatthias Ringwald // precondition: can_send_packet_now == TRUE 2453deb3ec6SMatthias 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){ 2463deb3ec6SMatthias Ringwald l2cap_reserve_packet_buffer(); 2473deb3ec6SMatthias Ringwald uint8_t * request = l2cap_get_outgoing_buffer(); 2483deb3ec6SMatthias Ringwald request[0] = request_type; 249f8fbdce0SMatthias Ringwald little_endian_store_16(request, 1, start_handle); 250f8fbdce0SMatthias Ringwald little_endian_store_16(request, 3, end_handle); 251f8fbdce0SMatthias Ringwald little_endian_store_16(request, 5, uuid16); 2523deb3ec6SMatthias Ringwald 2533deb3ec6SMatthias Ringwald l2cap_send_prepared_connectionless(peripheral_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, 7); 2543deb3ec6SMatthias Ringwald } 2553deb3ec6SMatthias Ringwald 2563deb3ec6SMatthias Ringwald // precondition: can_send_packet_now == TRUE 2573deb3ec6SMatthias 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){ 2583deb3ec6SMatthias Ringwald l2cap_reserve_packet_buffer(); 2593deb3ec6SMatthias Ringwald uint8_t * request = l2cap_get_outgoing_buffer(); 2603deb3ec6SMatthias Ringwald request[0] = request_type; 261f8fbdce0SMatthias Ringwald little_endian_store_16(request, 1, start_handle); 262f8fbdce0SMatthias Ringwald little_endian_store_16(request, 3, end_handle); 2639c80e4ccSMatthias Ringwald reverse_128(uuid128, &request[5]); 2643deb3ec6SMatthias Ringwald 2653deb3ec6SMatthias Ringwald l2cap_send_prepared_connectionless(peripheral_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, 21); 2663deb3ec6SMatthias Ringwald } 2673deb3ec6SMatthias Ringwald 2683deb3ec6SMatthias Ringwald // precondition: can_send_packet_now == TRUE 2693deb3ec6SMatthias Ringwald static void att_read_request(uint16_t request_type, uint16_t peripheral_handle, uint16_t attribute_handle){ 2703deb3ec6SMatthias Ringwald l2cap_reserve_packet_buffer(); 2713deb3ec6SMatthias Ringwald uint8_t * request = l2cap_get_outgoing_buffer(); 2723deb3ec6SMatthias Ringwald request[0] = request_type; 273f8fbdce0SMatthias Ringwald little_endian_store_16(request, 1, attribute_handle); 2743deb3ec6SMatthias Ringwald 2753deb3ec6SMatthias Ringwald l2cap_send_prepared_connectionless(peripheral_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, 3); 2763deb3ec6SMatthias Ringwald } 2773deb3ec6SMatthias Ringwald 2783deb3ec6SMatthias Ringwald // precondition: can_send_packet_now == TRUE 2793deb3ec6SMatthias Ringwald static void att_read_blob_request(uint16_t request_type, uint16_t peripheral_handle, uint16_t attribute_handle, uint16_t value_offset){ 2803deb3ec6SMatthias Ringwald l2cap_reserve_packet_buffer(); 2813deb3ec6SMatthias Ringwald uint8_t * request = l2cap_get_outgoing_buffer(); 2823deb3ec6SMatthias Ringwald request[0] = request_type; 283f8fbdce0SMatthias Ringwald little_endian_store_16(request, 1, attribute_handle); 284f8fbdce0SMatthias Ringwald little_endian_store_16(request, 3, value_offset); 2853deb3ec6SMatthias Ringwald 2863deb3ec6SMatthias Ringwald l2cap_send_prepared_connectionless(peripheral_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, 5); 2873deb3ec6SMatthias Ringwald } 2883deb3ec6SMatthias Ringwald 2893deb3ec6SMatthias Ringwald static void att_read_multiple_request(uint16_t peripheral_handle, uint16_t num_value_handles, uint16_t * value_handles){ 2903deb3ec6SMatthias Ringwald l2cap_reserve_packet_buffer(); 2913deb3ec6SMatthias Ringwald uint8_t * request = l2cap_get_outgoing_buffer(); 2923deb3ec6SMatthias Ringwald request[0] = ATT_READ_MULTIPLE_REQUEST; 2933deb3ec6SMatthias Ringwald int i; 2943deb3ec6SMatthias Ringwald int offset = 1; 2953deb3ec6SMatthias Ringwald for (i=0;i<num_value_handles;i++){ 296f8fbdce0SMatthias Ringwald little_endian_store_16(request, offset, value_handles[i]); 2973deb3ec6SMatthias Ringwald offset += 2; 2983deb3ec6SMatthias Ringwald } 2993deb3ec6SMatthias Ringwald l2cap_send_prepared_connectionless(peripheral_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, offset); 3003deb3ec6SMatthias Ringwald } 3013deb3ec6SMatthias Ringwald 3027a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE 3033deb3ec6SMatthias Ringwald // precondition: can_send_packet_now == TRUE 3043deb3ec6SMatthias 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]){ 3053deb3ec6SMatthias Ringwald l2cap_reserve_packet_buffer(); 3063deb3ec6SMatthias Ringwald uint8_t * request = l2cap_get_outgoing_buffer(); 3073deb3ec6SMatthias Ringwald request[0] = request_type; 308f8fbdce0SMatthias Ringwald little_endian_store_16(request, 1, attribute_handle); 3093deb3ec6SMatthias Ringwald memcpy(&request[3], value, value_length); 310f8fbdce0SMatthias Ringwald little_endian_store_32(request, 3 + value_length, sign_counter); 3119c80e4ccSMatthias Ringwald reverse_64(sgn, &request[3 + value_length + 4]); 3123deb3ec6SMatthias Ringwald l2cap_send_prepared_connectionless(peripheral_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, 3 + value_length + 12); 3133deb3ec6SMatthias Ringwald } 3147a766ebfSMatthias Ringwald #endif 3153deb3ec6SMatthias Ringwald 3163deb3ec6SMatthias Ringwald // precondition: can_send_packet_now == TRUE 3173deb3ec6SMatthias 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){ 3183deb3ec6SMatthias Ringwald l2cap_reserve_packet_buffer(); 3193deb3ec6SMatthias Ringwald uint8_t * request = l2cap_get_outgoing_buffer(); 3203deb3ec6SMatthias Ringwald request[0] = request_type; 321f8fbdce0SMatthias Ringwald little_endian_store_16(request, 1, attribute_handle); 3223deb3ec6SMatthias Ringwald memcpy(&request[3], value, value_length); 3233deb3ec6SMatthias Ringwald 3243deb3ec6SMatthias Ringwald l2cap_send_prepared_connectionless(peripheral_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, 3 + value_length); 3253deb3ec6SMatthias Ringwald } 3263deb3ec6SMatthias Ringwald 3273deb3ec6SMatthias Ringwald // precondition: can_send_packet_now == TRUE 3283deb3ec6SMatthias Ringwald static void att_execute_write_request(uint16_t request_type, uint16_t peripheral_handle, uint8_t execute_write){ 3293deb3ec6SMatthias Ringwald l2cap_reserve_packet_buffer(); 3303deb3ec6SMatthias Ringwald uint8_t * request = l2cap_get_outgoing_buffer(); 3313deb3ec6SMatthias Ringwald request[0] = request_type; 3323deb3ec6SMatthias Ringwald request[1] = execute_write; 3333deb3ec6SMatthias Ringwald l2cap_send_prepared_connectionless(peripheral_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, 2); 3343deb3ec6SMatthias Ringwald } 3353deb3ec6SMatthias Ringwald 3363deb3ec6SMatthias Ringwald // precondition: can_send_packet_now == TRUE 3373deb3ec6SMatthias 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){ 3383deb3ec6SMatthias Ringwald l2cap_reserve_packet_buffer(); 3393deb3ec6SMatthias Ringwald uint8_t * request = l2cap_get_outgoing_buffer(); 3403deb3ec6SMatthias Ringwald request[0] = request_type; 341f8fbdce0SMatthias Ringwald little_endian_store_16(request, 1, attribute_handle); 342f8fbdce0SMatthias Ringwald little_endian_store_16(request, 3, value_offset); 3433deb3ec6SMatthias Ringwald memcpy(&request[5], &value[value_offset], blob_length); 3443deb3ec6SMatthias Ringwald 3453deb3ec6SMatthias Ringwald l2cap_send_prepared_connectionless(peripheral_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, 5+blob_length); 3463deb3ec6SMatthias Ringwald } 3473deb3ec6SMatthias Ringwald 3483deb3ec6SMatthias Ringwald static void att_exchange_mtu_request(uint16_t peripheral_handle){ 3493deb3ec6SMatthias Ringwald uint16_t mtu = l2cap_max_le_mtu(); 3503deb3ec6SMatthias Ringwald l2cap_reserve_packet_buffer(); 3513deb3ec6SMatthias Ringwald uint8_t * request = l2cap_get_outgoing_buffer(); 3523deb3ec6SMatthias Ringwald request[0] = ATT_EXCHANGE_MTU_REQUEST; 353f8fbdce0SMatthias Ringwald little_endian_store_16(request, 1, mtu); 3543deb3ec6SMatthias Ringwald l2cap_send_prepared_connectionless(peripheral_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, 3); 3553deb3ec6SMatthias Ringwald } 3563deb3ec6SMatthias Ringwald 3573deb3ec6SMatthias Ringwald static uint16_t write_blob_length(gatt_client_t * peripheral){ 3583deb3ec6SMatthias Ringwald uint16_t max_blob_length = peripheral_mtu(peripheral) - 5; 3593deb3ec6SMatthias Ringwald if (peripheral->attribute_offset >= peripheral->attribute_length) { 3603deb3ec6SMatthias Ringwald return 0; 3613deb3ec6SMatthias Ringwald } 3623deb3ec6SMatthias Ringwald uint16_t rest_length = peripheral->attribute_length - peripheral->attribute_offset; 3633deb3ec6SMatthias Ringwald if (max_blob_length > rest_length){ 3643deb3ec6SMatthias Ringwald return rest_length; 3653deb3ec6SMatthias Ringwald } 3663deb3ec6SMatthias Ringwald return max_blob_length; 3673deb3ec6SMatthias Ringwald } 3683deb3ec6SMatthias Ringwald 3693deb3ec6SMatthias Ringwald static void send_gatt_services_request(gatt_client_t *peripheral){ 370fc64f94aSMatthias 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); 3713deb3ec6SMatthias Ringwald } 3723deb3ec6SMatthias Ringwald 3733deb3ec6SMatthias Ringwald static void send_gatt_by_uuid_request(gatt_client_t *peripheral, uint16_t attribute_group_type){ 3743deb3ec6SMatthias Ringwald if (peripheral->uuid16){ 3753deb3ec6SMatthias Ringwald uint8_t uuid16[2]; 376f8fbdce0SMatthias Ringwald little_endian_store_16(uuid16, 0, peripheral->uuid16); 377fc64f94aSMatthias 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); 3783deb3ec6SMatthias Ringwald return; 3793deb3ec6SMatthias Ringwald } 3803deb3ec6SMatthias Ringwald uint8_t uuid128[16]; 3819c80e4ccSMatthias Ringwald reverse_128(peripheral->uuid128, uuid128); 382fc64f94aSMatthias 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); 3833deb3ec6SMatthias Ringwald } 3843deb3ec6SMatthias Ringwald 3853deb3ec6SMatthias Ringwald static void send_gatt_services_by_uuid_request(gatt_client_t *peripheral){ 3863deb3ec6SMatthias Ringwald send_gatt_by_uuid_request(peripheral, GATT_PRIMARY_SERVICE_UUID); 3873deb3ec6SMatthias Ringwald } 3883deb3ec6SMatthias Ringwald 3893deb3ec6SMatthias Ringwald static void send_gatt_included_service_uuid_request(gatt_client_t *peripheral){ 390fc64f94aSMatthias Ringwald att_read_request(ATT_READ_REQUEST, peripheral->con_handle, peripheral->query_start_handle); 3913deb3ec6SMatthias Ringwald } 3923deb3ec6SMatthias Ringwald 3933deb3ec6SMatthias Ringwald static void send_gatt_included_service_request(gatt_client_t *peripheral){ 394fc64f94aSMatthias 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); 3953deb3ec6SMatthias Ringwald } 3963deb3ec6SMatthias Ringwald 3973deb3ec6SMatthias Ringwald static void send_gatt_characteristic_request(gatt_client_t *peripheral){ 398fc64f94aSMatthias 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); 3993deb3ec6SMatthias Ringwald } 4003deb3ec6SMatthias Ringwald 4013deb3ec6SMatthias Ringwald static void send_gatt_characteristic_descriptor_request(gatt_client_t *peripheral){ 402fc64f94aSMatthias Ringwald att_find_information_request(ATT_FIND_INFORMATION_REQUEST, peripheral->con_handle, peripheral->start_group_handle, peripheral->end_group_handle); 4033deb3ec6SMatthias Ringwald } 4043deb3ec6SMatthias Ringwald 4053deb3ec6SMatthias Ringwald static void send_gatt_read_characteristic_value_request(gatt_client_t *peripheral){ 406fc64f94aSMatthias Ringwald att_read_request(ATT_READ_REQUEST, peripheral->con_handle, peripheral->attribute_handle); 4073deb3ec6SMatthias Ringwald } 4083deb3ec6SMatthias Ringwald 4093deb3ec6SMatthias Ringwald static void send_gatt_read_by_type_request(gatt_client_t * peripheral){ 4103deb3ec6SMatthias Ringwald if (peripheral->uuid16){ 411fc64f94aSMatthias 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); 4123deb3ec6SMatthias Ringwald } else { 413fc64f94aSMatthias 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); 4143deb3ec6SMatthias Ringwald } 4153deb3ec6SMatthias Ringwald } 4163deb3ec6SMatthias Ringwald 4173deb3ec6SMatthias Ringwald static void send_gatt_read_blob_request(gatt_client_t *peripheral){ 418fc64f94aSMatthias Ringwald att_read_blob_request(ATT_READ_BLOB_REQUEST, peripheral->con_handle, peripheral->attribute_handle, peripheral->attribute_offset); 4193deb3ec6SMatthias Ringwald } 4203deb3ec6SMatthias Ringwald 4213deb3ec6SMatthias Ringwald static void send_gatt_read_multiple_request(gatt_client_t * peripheral){ 422fc64f94aSMatthias Ringwald att_read_multiple_request(peripheral->con_handle, peripheral->read_multiple_handle_count, peripheral->read_multiple_handles); 4233deb3ec6SMatthias Ringwald } 4243deb3ec6SMatthias Ringwald 4253deb3ec6SMatthias Ringwald static void send_gatt_write_attribute_value_request(gatt_client_t * peripheral){ 426fc64f94aSMatthias Ringwald att_write_request(ATT_WRITE_REQUEST, peripheral->con_handle, peripheral->attribute_handle, peripheral->attribute_length, peripheral->attribute_value); 4273deb3ec6SMatthias Ringwald } 4283deb3ec6SMatthias Ringwald 4293deb3ec6SMatthias Ringwald static void send_gatt_write_client_characteristic_configuration_request(gatt_client_t * peripheral){ 430fc64f94aSMatthias Ringwald att_write_request(ATT_WRITE_REQUEST, peripheral->con_handle, peripheral->client_characteristic_configuration_handle, 2, peripheral->client_characteristic_configuration_value); 4313deb3ec6SMatthias Ringwald } 4323deb3ec6SMatthias Ringwald 4333deb3ec6SMatthias Ringwald static void send_gatt_prepare_write_request(gatt_client_t * peripheral){ 434fc64f94aSMatthias 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); 4353deb3ec6SMatthias Ringwald } 4363deb3ec6SMatthias Ringwald 4373deb3ec6SMatthias Ringwald static void send_gatt_execute_write_request(gatt_client_t * peripheral){ 438fc64f94aSMatthias Ringwald att_execute_write_request(ATT_EXECUTE_WRITE_REQUEST, peripheral->con_handle, 1); 4393deb3ec6SMatthias Ringwald } 4403deb3ec6SMatthias Ringwald 4413deb3ec6SMatthias Ringwald static void send_gatt_cancel_prepared_write_request(gatt_client_t * peripheral){ 442fc64f94aSMatthias Ringwald att_execute_write_request(ATT_EXECUTE_WRITE_REQUEST, peripheral->con_handle, 0); 4433deb3ec6SMatthias Ringwald } 4443deb3ec6SMatthias Ringwald 445abdc9fb5SMatthias Ringwald #ifndef ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERY 4463deb3ec6SMatthias Ringwald static void send_gatt_read_client_characteristic_configuration_request(gatt_client_t * peripheral){ 447fc64f94aSMatthias 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); 4483deb3ec6SMatthias Ringwald } 449abdc9fb5SMatthias Ringwald #endif 4503deb3ec6SMatthias Ringwald 4513deb3ec6SMatthias Ringwald static void send_gatt_read_characteristic_descriptor_request(gatt_client_t * peripheral){ 452fc64f94aSMatthias Ringwald att_read_request(ATT_READ_REQUEST, peripheral->con_handle, peripheral->attribute_handle); 4533deb3ec6SMatthias Ringwald } 4543deb3ec6SMatthias Ringwald 4557a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE 4563deb3ec6SMatthias Ringwald static void send_gatt_signed_write_request(gatt_client_t * peripheral, uint32_t sign_counter){ 457fc64f94aSMatthias 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); 4583deb3ec6SMatthias Ringwald } 4597a766ebfSMatthias Ringwald #endif 4603deb3ec6SMatthias Ringwald 4613deb3ec6SMatthias Ringwald static uint16_t get_last_result_handle_from_service_list(uint8_t * packet, uint16_t size){ 4623deb3ec6SMatthias Ringwald uint8_t attr_length = packet[1]; 463f8fbdce0SMatthias Ringwald return little_endian_read_16(packet, size - attr_length + 2); 4643deb3ec6SMatthias Ringwald } 4653deb3ec6SMatthias Ringwald 4663deb3ec6SMatthias Ringwald static uint16_t get_last_result_handle_from_characteristics_list(uint8_t * packet, uint16_t size){ 4673deb3ec6SMatthias Ringwald uint8_t attr_length = packet[1]; 468f8fbdce0SMatthias Ringwald return little_endian_read_16(packet, size - attr_length + 3); 4693deb3ec6SMatthias Ringwald } 4703deb3ec6SMatthias Ringwald 4713deb3ec6SMatthias Ringwald static uint16_t get_last_result_handle_from_included_services_list(uint8_t * packet, uint16_t size){ 4723deb3ec6SMatthias Ringwald uint8_t attr_length = packet[1]; 473f8fbdce0SMatthias Ringwald return little_endian_read_16(packet, size - attr_length); 4743deb3ec6SMatthias Ringwald } 4753deb3ec6SMatthias Ringwald 4763deb3ec6SMatthias Ringwald static void gatt_client_handle_transaction_complete(gatt_client_t * peripheral){ 4773deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_READY; 4783deb3ec6SMatthias Ringwald gatt_client_timeout_stop(peripheral); 4793deb3ec6SMatthias Ringwald } 4803deb3ec6SMatthias Ringwald 4819c662c9bSMatthias Ringwald static void emit_event_new(btstack_packet_handler_t callback, uint8_t * packet, uint16_t size){ 4829c662c9bSMatthias Ringwald if (!callback) return; 483b6e003bcSMatthias Ringwald hci_dump_packet(HCI_EVENT_PACKET, 0, packet, size); 4849c662c9bSMatthias Ringwald (*callback)(HCI_EVENT_PACKET, 0, packet, size); 4853deb3ec6SMatthias Ringwald } 4863deb3ec6SMatthias Ringwald 48786c38559SMatthias 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){ 48886c38559SMatthias Ringwald notification->callback = packet_handler; 4899c662c9bSMatthias Ringwald notification->con_handle = con_handle; 4909c662c9bSMatthias Ringwald notification->attribute_handle = characteristic->value_handle; 4919c662c9bSMatthias Ringwald btstack_linked_list_add(&gatt_client_value_listeners, (btstack_linked_item_t*) notification); 4929c662c9bSMatthias Ringwald } 4939c662c9bSMatthias Ringwald 4941f734b5fSMatthias Ringwald void gatt_client_stop_listening_for_characteristic_value_updates(gatt_client_notification_t * notification){ 4951f734b5fSMatthias Ringwald btstack_linked_list_remove(&gatt_client_value_listeners, (btstack_linked_item_t*) notification); 4961f734b5fSMatthias Ringwald } 4971f734b5fSMatthias Ringwald 498711e6c80SMatthias Ringwald static void emit_event_to_registered_listeners(hci_con_handle_t con_handle, uint16_t attribute_handle, uint8_t * packet, uint16_t size){ 499665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 5009c662c9bSMatthias Ringwald btstack_linked_list_iterator_init(&it, &gatt_client_value_listeners); 501665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 50286c38559SMatthias Ringwald gatt_client_notification_t * notification = (gatt_client_notification_t*) btstack_linked_list_iterator_next(&it); 50386c38559SMatthias Ringwald if (notification->con_handle != con_handle) continue; 50486c38559SMatthias Ringwald if (notification->attribute_handle != attribute_handle) continue; 50586c38559SMatthias Ringwald (*notification->callback)(HCI_EVENT_PACKET, 0, packet, size); 5063deb3ec6SMatthias Ringwald } 5073deb3ec6SMatthias Ringwald } 5083deb3ec6SMatthias Ringwald 5093deb3ec6SMatthias Ringwald static void emit_gatt_complete_event(gatt_client_t * peripheral, uint8_t status){ 5103deb3ec6SMatthias Ringwald // @format H1 5113deb3ec6SMatthias Ringwald uint8_t packet[5]; 5125611a760SMatthias Ringwald packet[0] = GATT_EVENT_QUERY_COMPLETE; 5133deb3ec6SMatthias Ringwald packet[1] = 3; 514fc64f94aSMatthias Ringwald little_endian_store_16(packet, 2, peripheral->con_handle); 5153deb3ec6SMatthias Ringwald packet[4] = status; 5169c662c9bSMatthias Ringwald emit_event_new(peripheral->callback, packet, sizeof(packet)); 5173deb3ec6SMatthias Ringwald } 5183deb3ec6SMatthias Ringwald 5193deb3ec6SMatthias 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){ 5203deb3ec6SMatthias Ringwald // @format HX 5213deb3ec6SMatthias Ringwald uint8_t packet[24]; 5225611a760SMatthias Ringwald packet[0] = GATT_EVENT_SERVICE_QUERY_RESULT; 5233deb3ec6SMatthias Ringwald packet[1] = sizeof(packet) - 2; 524fc64f94aSMatthias Ringwald little_endian_store_16(packet, 2, peripheral->con_handle); 5253deb3ec6SMatthias Ringwald /// 526f8fbdce0SMatthias Ringwald little_endian_store_16(packet, 4, start_group_handle); 527f8fbdce0SMatthias Ringwald little_endian_store_16(packet, 6, end_group_handle); 5289c80e4ccSMatthias Ringwald reverse_128(uuid128, &packet[8]); 5299c662c9bSMatthias Ringwald emit_event_new(peripheral->callback, packet, sizeof(packet)); 5303deb3ec6SMatthias Ringwald } 5313deb3ec6SMatthias Ringwald 5323deb3ec6SMatthias 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){ 5333deb3ec6SMatthias Ringwald // @format HX 5343deb3ec6SMatthias Ringwald uint8_t packet[26]; 5355611a760SMatthias Ringwald packet[0] = GATT_EVENT_INCLUDED_SERVICE_QUERY_RESULT; 5363deb3ec6SMatthias Ringwald packet[1] = sizeof(packet) - 2; 537fc64f94aSMatthias Ringwald little_endian_store_16(packet, 2, peripheral->con_handle); 5383deb3ec6SMatthias Ringwald /// 539f8fbdce0SMatthias Ringwald little_endian_store_16(packet, 4, include_handle); 5403deb3ec6SMatthias Ringwald // 541f8fbdce0SMatthias Ringwald little_endian_store_16(packet, 6, start_group_handle); 542f8fbdce0SMatthias Ringwald little_endian_store_16(packet, 8, end_group_handle); 5439c80e4ccSMatthias Ringwald reverse_128(uuid128, &packet[10]); 5449c662c9bSMatthias Ringwald emit_event_new(peripheral->callback, packet, sizeof(packet)); 5453deb3ec6SMatthias Ringwald } 5463deb3ec6SMatthias Ringwald 5473deb3ec6SMatthias 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, 5483deb3ec6SMatthias Ringwald uint16_t properties, uint8_t * uuid128){ 5493deb3ec6SMatthias Ringwald // @format HY 5503deb3ec6SMatthias Ringwald uint8_t packet[28]; 5515611a760SMatthias Ringwald packet[0] = GATT_EVENT_CHARACTERISTIC_QUERY_RESULT; 5523deb3ec6SMatthias Ringwald packet[1] = sizeof(packet) - 2; 553fc64f94aSMatthias Ringwald little_endian_store_16(packet, 2, peripheral->con_handle); 5543deb3ec6SMatthias Ringwald /// 555f8fbdce0SMatthias Ringwald little_endian_store_16(packet, 4, start_handle); 556f8fbdce0SMatthias Ringwald little_endian_store_16(packet, 6, value_handle); 557f8fbdce0SMatthias Ringwald little_endian_store_16(packet, 8, end_handle); 558f8fbdce0SMatthias Ringwald little_endian_store_16(packet, 10, properties); 5599c80e4ccSMatthias Ringwald reverse_128(uuid128, &packet[12]); 5609c662c9bSMatthias Ringwald emit_event_new(peripheral->callback, packet, sizeof(packet)); 5613deb3ec6SMatthias Ringwald } 5623deb3ec6SMatthias Ringwald 5633deb3ec6SMatthias Ringwald static void emit_gatt_all_characteristic_descriptors_result_event( 5643deb3ec6SMatthias Ringwald gatt_client_t * peripheral, uint16_t descriptor_handle, uint8_t * uuid128){ 5653deb3ec6SMatthias Ringwald // @format HZ 5663deb3ec6SMatthias Ringwald uint8_t packet[22]; 5675611a760SMatthias Ringwald packet[0] = GATT_EVENT_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY_RESULT; 5683deb3ec6SMatthias Ringwald packet[1] = sizeof(packet) - 2; 569fc64f94aSMatthias Ringwald little_endian_store_16(packet, 2, peripheral->con_handle); 5703deb3ec6SMatthias Ringwald /// 571f8fbdce0SMatthias Ringwald little_endian_store_16(packet, 4, descriptor_handle); 5729c80e4ccSMatthias Ringwald reverse_128(uuid128, &packet[6]); 5739c662c9bSMatthias Ringwald emit_event_new(peripheral->callback, packet, sizeof(packet)); 5743deb3ec6SMatthias Ringwald } 5753deb3ec6SMatthias Ringwald 5768f37572aSJakob Krantz static void emit_gatt_mtu_exchanged_result_event(gatt_client_t * peripheral, uint16_t new_mtu){ 5778f37572aSJakob Krantz // @format H2 5788f37572aSJakob Krantz uint8_t packet[6]; 5798f37572aSJakob Krantz packet[0] = GATT_EVENT_MTU; 5808f37572aSJakob Krantz packet[1] = sizeof(packet) - 2; 5818f37572aSJakob Krantz little_endian_store_16(packet, 2, peripheral->con_handle); 5828f37572aSJakob Krantz little_endian_store_16(packet, 4, new_mtu); 583b12646c5SJakob Krantz att_dispatch_client_mtu_exchanged(peripheral->con_handle, new_mtu); 5848f37572aSJakob Krantz emit_event_new(peripheral->callback, packet, sizeof(packet)); 5858f37572aSJakob Krantz } 5868f37572aSJakob Krantz /// 5873deb3ec6SMatthias Ringwald static void report_gatt_services(gatt_client_t * peripheral, uint8_t * packet, uint16_t size){ 5883deb3ec6SMatthias Ringwald uint8_t attr_length = packet[1]; 5893deb3ec6SMatthias Ringwald uint8_t uuid_length = attr_length - 4; 5903deb3ec6SMatthias Ringwald 5913deb3ec6SMatthias Ringwald int i; 5923deb3ec6SMatthias Ringwald for (i = 2; i < size; i += attr_length){ 593f8fbdce0SMatthias Ringwald uint16_t start_group_handle = little_endian_read_16(packet,i); 594f8fbdce0SMatthias Ringwald uint16_t end_group_handle = little_endian_read_16(packet,i+2); 5953deb3ec6SMatthias Ringwald uint8_t uuid128[16]; 5963deb3ec6SMatthias Ringwald uint16_t uuid16 = 0; 5973deb3ec6SMatthias Ringwald 5983deb3ec6SMatthias Ringwald if (uuid_length == 2){ 599f8fbdce0SMatthias Ringwald uuid16 = little_endian_read_16(packet, i+4); 600e1a125dfSMatthias Ringwald uuid_add_bluetooth_prefix((uint8_t*) &uuid128, uuid16); 6013deb3ec6SMatthias Ringwald } else { 6029c80e4ccSMatthias Ringwald reverse_128(&packet[i+4], uuid128); 6033deb3ec6SMatthias Ringwald } 6043deb3ec6SMatthias Ringwald emit_gatt_service_query_result_event(peripheral, start_group_handle, end_group_handle, uuid128); 6053deb3ec6SMatthias Ringwald } 606fc64f94aSMatthias Ringwald // log_info("report_gatt_services for %02X done", peripheral->con_handle); 6073deb3ec6SMatthias Ringwald } 6083deb3ec6SMatthias Ringwald 6093deb3ec6SMatthias Ringwald // helper 6103deb3ec6SMatthias 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){ 6113deb3ec6SMatthias Ringwald uint8_t uuid128[16]; 6123deb3ec6SMatthias Ringwald uint16_t uuid16 = 0; 6133deb3ec6SMatthias Ringwald if (uuid_length == 2){ 614f8fbdce0SMatthias Ringwald uuid16 = little_endian_read_16(uuid, 0); 615e1a125dfSMatthias Ringwald uuid_add_bluetooth_prefix((uint8_t*) uuid128, uuid16); 6163deb3ec6SMatthias Ringwald } else { 6179c80e4ccSMatthias Ringwald reverse_128(uuid, uuid128); 6183deb3ec6SMatthias Ringwald } 6193deb3ec6SMatthias Ringwald 6203deb3ec6SMatthias Ringwald if (peripheral->filter_with_uuid && memcmp(peripheral->uuid128, uuid128, 16) != 0) return; 6213deb3ec6SMatthias Ringwald 6223deb3ec6SMatthias Ringwald peripheral->characteristic_properties = properties; 6233deb3ec6SMatthias Ringwald peripheral->characteristic_start_handle = start_handle; 6243deb3ec6SMatthias Ringwald peripheral->attribute_handle = value_handle; 6253deb3ec6SMatthias Ringwald 6263deb3ec6SMatthias Ringwald if (peripheral->filter_with_uuid) return; 6273deb3ec6SMatthias Ringwald 6283deb3ec6SMatthias Ringwald peripheral->uuid16 = uuid16; 6293deb3ec6SMatthias Ringwald memcpy(peripheral->uuid128, uuid128, 16); 6303deb3ec6SMatthias Ringwald } 6313deb3ec6SMatthias Ringwald 6323deb3ec6SMatthias Ringwald static void characteristic_end_found(gatt_client_t * peripheral, uint16_t end_handle){ 6333deb3ec6SMatthias Ringwald // TODO: stop searching if filter and uuid found 6343deb3ec6SMatthias Ringwald 6353deb3ec6SMatthias Ringwald if (!peripheral->characteristic_start_handle) return; 6363deb3ec6SMatthias Ringwald 6373deb3ec6SMatthias Ringwald emit_gatt_characteristic_query_result_event(peripheral, peripheral->characteristic_start_handle, peripheral->attribute_handle, 6383deb3ec6SMatthias Ringwald end_handle, peripheral->characteristic_properties, peripheral->uuid128); 6393deb3ec6SMatthias Ringwald 6403deb3ec6SMatthias Ringwald peripheral->characteristic_start_handle = 0; 6413deb3ec6SMatthias Ringwald } 6423deb3ec6SMatthias Ringwald 6433deb3ec6SMatthias Ringwald static void report_gatt_characteristics(gatt_client_t * peripheral, uint8_t * packet, uint16_t size){ 6443deb3ec6SMatthias Ringwald uint8_t attr_length = packet[1]; 6453deb3ec6SMatthias Ringwald uint8_t uuid_length = attr_length - 5; 6463deb3ec6SMatthias Ringwald int i; 6473deb3ec6SMatthias Ringwald for (i = 2; i < size; i += attr_length){ 648f8fbdce0SMatthias Ringwald uint16_t start_handle = little_endian_read_16(packet, i); 6493deb3ec6SMatthias Ringwald uint8_t properties = packet[i+2]; 650f8fbdce0SMatthias Ringwald uint16_t value_handle = little_endian_read_16(packet, i+3); 6513deb3ec6SMatthias Ringwald characteristic_end_found(peripheral, start_handle-1); 6523deb3ec6SMatthias Ringwald characteristic_start_found(peripheral, start_handle, properties, value_handle, &packet[i+5], uuid_length); 6533deb3ec6SMatthias Ringwald } 6543deb3ec6SMatthias Ringwald } 6553deb3ec6SMatthias Ringwald 6563deb3ec6SMatthias Ringwald static void report_gatt_included_service_uuid16(gatt_client_t * peripheral, uint16_t include_handle, uint16_t uuid16){ 6573deb3ec6SMatthias Ringwald uint8_t normalized_uuid128[16]; 658e1a125dfSMatthias Ringwald uuid_add_bluetooth_prefix(normalized_uuid128, uuid16); 6593deb3ec6SMatthias Ringwald emit_gatt_included_service_query_result_event(peripheral, include_handle, peripheral->query_start_handle, 6603deb3ec6SMatthias Ringwald peripheral->query_end_handle, normalized_uuid128); 6613deb3ec6SMatthias Ringwald } 6623deb3ec6SMatthias Ringwald 6633deb3ec6SMatthias Ringwald static void report_gatt_included_service_uuid128(gatt_client_t * peripheral, uint16_t include_handle, uint8_t *uuid128){ 6643deb3ec6SMatthias Ringwald emit_gatt_included_service_query_result_event(peripheral, include_handle, peripheral->query_start_handle, 6653deb3ec6SMatthias Ringwald peripheral->query_end_handle, uuid128); 6663deb3ec6SMatthias Ringwald } 6673deb3ec6SMatthias Ringwald 6683deb3ec6SMatthias Ringwald // @returns packet pointer 6693deb3ec6SMatthias Ringwald // @note assume that value is part of an l2cap buffer - overwrite HCI + L2CAP packet headers 6703deb3ec6SMatthias Ringwald static const int characteristic_value_event_header_size = 8; 671711e6c80SMatthias 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){ 6723deb3ec6SMatthias Ringwald // before the value inside the ATT PDU 6733deb3ec6SMatthias Ringwald uint8_t * packet = value - characteristic_value_event_header_size; 6743deb3ec6SMatthias Ringwald packet[0] = type; 6753deb3ec6SMatthias Ringwald packet[1] = characteristic_value_event_header_size - 2 + length; 676f8fbdce0SMatthias Ringwald little_endian_store_16(packet, 2, con_handle); 677f8fbdce0SMatthias Ringwald little_endian_store_16(packet, 4, attribute_handle); 678f8fbdce0SMatthias Ringwald little_endian_store_16(packet, 6, length); 6793deb3ec6SMatthias Ringwald return packet; 6803deb3ec6SMatthias Ringwald } 6813deb3ec6SMatthias Ringwald 6823deb3ec6SMatthias Ringwald // @returns packet pointer 6833deb3ec6SMatthias Ringwald // @note assume that value is part of an l2cap buffer - overwrite parts of the HCI/L2CAP/ATT packet (4/4/3) bytes 6843deb3ec6SMatthias Ringwald static const int long_characteristic_value_event_header_size = 10; 685711e6c80SMatthias 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){ 6863deb3ec6SMatthias Ringwald #if defined(HCI_INCOMING_PRE_BUFFER_SIZE) && (HCI_INCOMING_PRE_BUFFER_SIZE >= 10 - 8) // L2CAP Header (4) - ACL Header (4) 6873deb3ec6SMatthias Ringwald // before the value inside the ATT PDU 6883deb3ec6SMatthias Ringwald uint8_t * packet = value - long_characteristic_value_event_header_size; 6893deb3ec6SMatthias Ringwald packet[0] = type; 6903deb3ec6SMatthias Ringwald packet[1] = long_characteristic_value_event_header_size - 2 + length; 691f8fbdce0SMatthias Ringwald little_endian_store_16(packet, 2, con_handle); 692f8fbdce0SMatthias Ringwald little_endian_store_16(packet, 4, attribute_handle); 693f8fbdce0SMatthias Ringwald little_endian_store_16(packet, 6, offset); 694f8fbdce0SMatthias Ringwald little_endian_store_16(packet, 8, length); 6953deb3ec6SMatthias Ringwald return packet; 6963deb3ec6SMatthias Ringwald #else 6973deb3ec6SMatthias Ringwald log_error("HCI_INCOMING_PRE_BUFFER_SIZE >= 2 required for long characteristic reads"); 6983deb3ec6SMatthias Ringwald return NULL; 6993deb3ec6SMatthias Ringwald #endif 7003deb3ec6SMatthias Ringwald } 7013deb3ec6SMatthias Ringwald 7023deb3ec6SMatthias Ringwald 7033deb3ec6SMatthias Ringwald // @note assume that value is part of an l2cap buffer - overwrite parts of the HCI/L2CAP/ATT packet (4/4/3) bytes 704711e6c80SMatthias Ringwald static void report_gatt_notification(hci_con_handle_t con_handle, uint16_t value_handle, uint8_t * value, int length){ 7055611a760SMatthias Ringwald uint8_t * packet = setup_characteristic_value_packet(GATT_EVENT_NOTIFICATION, con_handle, value_handle, value, length); 7069c662c9bSMatthias Ringwald emit_event_to_registered_listeners(con_handle, value_handle, packet, characteristic_value_event_header_size + length); 7073deb3ec6SMatthias Ringwald } 7083deb3ec6SMatthias Ringwald 7093deb3ec6SMatthias Ringwald // @note assume that value is part of an l2cap buffer - overwrite parts of the HCI/L2CAP/ATT packet (4/4/3) bytes 710711e6c80SMatthias Ringwald static void report_gatt_indication(hci_con_handle_t con_handle, uint16_t value_handle, uint8_t * value, int length){ 7115611a760SMatthias Ringwald uint8_t * packet = setup_characteristic_value_packet(GATT_EVENT_INDICATION, con_handle, value_handle, value, length); 7129c662c9bSMatthias Ringwald emit_event_to_registered_listeners(con_handle, value_handle, packet, characteristic_value_event_header_size + length); 7133deb3ec6SMatthias Ringwald } 7143deb3ec6SMatthias Ringwald 7153deb3ec6SMatthias Ringwald // @note assume that value is part of an l2cap buffer - overwrite parts of the HCI/L2CAP/ATT packet (4/4/3) bytes 7163deb3ec6SMatthias Ringwald static void report_gatt_characteristic_value(gatt_client_t * peripheral, uint16_t attribute_handle, uint8_t * value, uint16_t length){ 717fc64f94aSMatthias Ringwald uint8_t * packet = setup_characteristic_value_packet(GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT, peripheral->con_handle, attribute_handle, value, length); 7189c662c9bSMatthias Ringwald emit_event_new(peripheral->callback, packet, characteristic_value_event_header_size + length); 7193deb3ec6SMatthias Ringwald } 7203deb3ec6SMatthias Ringwald 7213deb3ec6SMatthias Ringwald // @note assume that value is part of an l2cap buffer - overwrite parts of the HCI/L2CAP/ATT packet (4/4/3) bytes 7223deb3ec6SMatthias 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){ 723fc64f94aSMatthias 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); 7243deb3ec6SMatthias Ringwald if (!packet) return; 7259c662c9bSMatthias Ringwald emit_event_new(peripheral->callback, packet, blob_length + long_characteristic_value_event_header_size); 7263deb3ec6SMatthias Ringwald } 7273deb3ec6SMatthias Ringwald 7283deb3ec6SMatthias 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){ 7299ec2630cSMatthias Ringwald UNUSED(value_offset); 730fc64f94aSMatthias Ringwald uint8_t * packet = setup_characteristic_value_packet(GATT_EVENT_CHARACTERISTIC_DESCRIPTOR_QUERY_RESULT, peripheral->con_handle, descriptor_handle, value, value_length); 7319c662c9bSMatthias Ringwald emit_event_new(peripheral->callback, packet, value_length + 8); 7323deb3ec6SMatthias Ringwald } 7333deb3ec6SMatthias Ringwald 7343deb3ec6SMatthias 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){ 735fc64f94aSMatthias 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); 7363deb3ec6SMatthias Ringwald if (!packet) return; 7379c662c9bSMatthias Ringwald emit_event_new(peripheral->callback, packet, blob_length + long_characteristic_value_event_header_size); 7383deb3ec6SMatthias Ringwald } 7393deb3ec6SMatthias Ringwald 7403deb3ec6SMatthias Ringwald static void report_gatt_all_characteristic_descriptors(gatt_client_t * peripheral, uint8_t * packet, uint16_t size, uint16_t pair_size){ 7413deb3ec6SMatthias Ringwald int i; 7423deb3ec6SMatthias Ringwald for (i = 0; i<size; i+=pair_size){ 743f8fbdce0SMatthias Ringwald uint16_t descriptor_handle = little_endian_read_16(packet,i); 7443deb3ec6SMatthias Ringwald uint8_t uuid128[16]; 7453deb3ec6SMatthias Ringwald uint16_t uuid16 = 0; 7463deb3ec6SMatthias Ringwald if (pair_size == 4){ 747f8fbdce0SMatthias Ringwald uuid16 = little_endian_read_16(packet,i+2); 748e1a125dfSMatthias Ringwald uuid_add_bluetooth_prefix(uuid128, uuid16); 7493deb3ec6SMatthias Ringwald } else { 7509c80e4ccSMatthias Ringwald reverse_128(&packet[i+2], uuid128); 7513deb3ec6SMatthias Ringwald } 7523deb3ec6SMatthias Ringwald emit_gatt_all_characteristic_descriptors_result_event(peripheral, descriptor_handle, uuid128); 7533deb3ec6SMatthias Ringwald } 7543deb3ec6SMatthias Ringwald 7553deb3ec6SMatthias Ringwald } 7563deb3ec6SMatthias Ringwald 7573deb3ec6SMatthias Ringwald static int is_query_done(gatt_client_t * peripheral, uint16_t last_result_handle){ 7583deb3ec6SMatthias Ringwald return last_result_handle >= peripheral->end_group_handle; 7593deb3ec6SMatthias Ringwald } 7603deb3ec6SMatthias Ringwald 7613deb3ec6SMatthias Ringwald static void trigger_next_query(gatt_client_t * peripheral, uint16_t last_result_handle, gatt_client_state_t next_query_state){ 7623deb3ec6SMatthias Ringwald if (is_query_done(peripheral, last_result_handle)){ 7633deb3ec6SMatthias Ringwald gatt_client_handle_transaction_complete(peripheral); 7643deb3ec6SMatthias Ringwald emit_gatt_complete_event(peripheral, 0); 7653deb3ec6SMatthias Ringwald return; 7663deb3ec6SMatthias Ringwald } 7673deb3ec6SMatthias Ringwald // next 7683deb3ec6SMatthias Ringwald peripheral->start_group_handle = last_result_handle + 1; 7693deb3ec6SMatthias Ringwald peripheral->gatt_client_state = next_query_state; 7703deb3ec6SMatthias Ringwald } 7713deb3ec6SMatthias Ringwald 77200748105SMatthias Ringwald static void trigger_next_included_service_query(gatt_client_t * peripheral, uint16_t last_result_handle){ 7733deb3ec6SMatthias Ringwald trigger_next_query(peripheral, last_result_handle, P_W2_SEND_INCLUDED_SERVICE_QUERY); 7743deb3ec6SMatthias Ringwald } 7753deb3ec6SMatthias Ringwald 77600748105SMatthias Ringwald static void trigger_next_service_query(gatt_client_t * peripheral, uint16_t last_result_handle){ 7773deb3ec6SMatthias Ringwald trigger_next_query(peripheral, last_result_handle, P_W2_SEND_SERVICE_QUERY); 7783deb3ec6SMatthias Ringwald } 7793deb3ec6SMatthias Ringwald 78000748105SMatthias Ringwald static void trigger_next_service_by_uuid_query(gatt_client_t * peripheral, uint16_t last_result_handle){ 7813deb3ec6SMatthias Ringwald trigger_next_query(peripheral, last_result_handle, P_W2_SEND_SERVICE_WITH_UUID_QUERY); 7823deb3ec6SMatthias Ringwald } 7833deb3ec6SMatthias Ringwald 78400748105SMatthias Ringwald static void trigger_next_characteristic_query(gatt_client_t * peripheral, uint16_t last_result_handle){ 7853deb3ec6SMatthias Ringwald if (is_query_done(peripheral, last_result_handle)){ 7863deb3ec6SMatthias Ringwald // report last characteristic 7873deb3ec6SMatthias Ringwald characteristic_end_found(peripheral, peripheral->end_group_handle); 7883deb3ec6SMatthias Ringwald } 7893deb3ec6SMatthias Ringwald trigger_next_query(peripheral, last_result_handle, P_W2_SEND_ALL_CHARACTERISTICS_OF_SERVICE_QUERY); 7903deb3ec6SMatthias Ringwald } 7913deb3ec6SMatthias Ringwald 79200748105SMatthias Ringwald static void trigger_next_characteristic_descriptor_query(gatt_client_t * peripheral, uint16_t last_result_handle){ 7933deb3ec6SMatthias Ringwald trigger_next_query(peripheral, last_result_handle, P_W2_SEND_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY); 7943deb3ec6SMatthias Ringwald } 7953deb3ec6SMatthias Ringwald 79600748105SMatthias Ringwald static void trigger_next_read_by_type_query(gatt_client_t * peripheral, uint16_t last_result_handle){ 7973deb3ec6SMatthias Ringwald trigger_next_query(peripheral, last_result_handle, P_W2_SEND_READ_BY_TYPE_REQUEST); 7983deb3ec6SMatthias Ringwald } 7993deb3ec6SMatthias Ringwald 80000748105SMatthias 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){ 8013deb3ec6SMatthias Ringwald peripheral->attribute_offset += write_blob_length(peripheral); 8023deb3ec6SMatthias Ringwald uint16_t next_blob_length = write_blob_length(peripheral); 8033deb3ec6SMatthias Ringwald 8043deb3ec6SMatthias Ringwald if (next_blob_length == 0){ 8053deb3ec6SMatthias Ringwald peripheral->gatt_client_state = done_state; 8063deb3ec6SMatthias Ringwald return; 8073deb3ec6SMatthias Ringwald } 8083deb3ec6SMatthias Ringwald peripheral->gatt_client_state = next_query_state; 8093deb3ec6SMatthias Ringwald } 8103deb3ec6SMatthias Ringwald 81100748105SMatthias Ringwald static void trigger_next_blob_query(gatt_client_t * peripheral, gatt_client_state_t next_query_state, uint16_t received_blob_length){ 8123deb3ec6SMatthias Ringwald 8133deb3ec6SMatthias Ringwald uint16_t max_blob_length = peripheral_mtu(peripheral) - 1; 8143deb3ec6SMatthias Ringwald if (received_blob_length < max_blob_length){ 8153deb3ec6SMatthias Ringwald gatt_client_handle_transaction_complete(peripheral); 8163deb3ec6SMatthias Ringwald emit_gatt_complete_event(peripheral, 0); 8173deb3ec6SMatthias Ringwald return; 8183deb3ec6SMatthias Ringwald } 8193deb3ec6SMatthias Ringwald 8203deb3ec6SMatthias Ringwald peripheral->attribute_offset += received_blob_length; 8213deb3ec6SMatthias Ringwald peripheral->gatt_client_state = next_query_state; 8223deb3ec6SMatthias Ringwald } 8233deb3ec6SMatthias Ringwald 8243deb3ec6SMatthias Ringwald 8253deb3ec6SMatthias Ringwald static int is_value_valid(gatt_client_t *peripheral, uint8_t *packet, uint16_t size){ 826f8fbdce0SMatthias Ringwald uint16_t attribute_handle = little_endian_read_16(packet, 1); 827f8fbdce0SMatthias Ringwald uint16_t value_offset = little_endian_read_16(packet, 3); 8283deb3ec6SMatthias Ringwald 8293deb3ec6SMatthias Ringwald if (peripheral->attribute_handle != attribute_handle) return 0; 8303deb3ec6SMatthias Ringwald if (peripheral->attribute_offset != value_offset) return 0; 8313deb3ec6SMatthias Ringwald return memcmp(&peripheral->attribute_value[peripheral->attribute_offset], &packet[5], size-5) == 0; 8323deb3ec6SMatthias Ringwald } 8333deb3ec6SMatthias Ringwald 834544128c3SMatthias Ringwald // returns 1 if packet was sent 835544128c3SMatthias Ringwald static int gatt_client_run_for_peripheral( gatt_client_t * peripheral){ 8363deb3ec6SMatthias Ringwald // log_info("- handle_peripheral_list, mtu state %u, client state %u", peripheral->mtu_state, peripheral->gatt_client_state); 8373deb3ec6SMatthias Ringwald 838d1e1a57fSMatthias Ringwald // wait until re-encryption as central is complete 839d1e1a57fSMatthias Ringwald if (gap_reconnect_security_setup_active(peripheral->con_handle)) return 0; 840d1e1a57fSMatthias Ringwald 841f4b33574SMatthias Ringwald #ifdef ENABLE_GATT_CLIENT_PAIRING 842f4b33574SMatthias Ringwald // wait until pairing complete 843f4b33574SMatthias Ringwald if (peripheral->wait_for_pairing_complete) return 0; 844f4b33574SMatthias Ringwald #endif 845f4b33574SMatthias Ringwald 8463deb3ec6SMatthias Ringwald switch (peripheral->mtu_state) { 847544128c3SMatthias Ringwald case SEND_MTU_EXCHANGE: 8483deb3ec6SMatthias Ringwald peripheral->mtu_state = SENT_MTU_EXCHANGE; 849fc64f94aSMatthias Ringwald att_exchange_mtu_request(peripheral->con_handle); 850544128c3SMatthias Ringwald return 1; 8513deb3ec6SMatthias Ringwald case SENT_MTU_EXCHANGE: 852544128c3SMatthias Ringwald return 0; 8533deb3ec6SMatthias Ringwald default: 8543deb3ec6SMatthias Ringwald break; 8553deb3ec6SMatthias Ringwald } 8563deb3ec6SMatthias Ringwald 8573deb3ec6SMatthias Ringwald if (peripheral->send_confirmation){ 8583deb3ec6SMatthias Ringwald peripheral->send_confirmation = 0; 859fc64f94aSMatthias Ringwald att_confirmation(peripheral->con_handle); 860544128c3SMatthias Ringwald return 1; 8613deb3ec6SMatthias Ringwald } 8623deb3ec6SMatthias Ringwald 8633deb3ec6SMatthias Ringwald // check MTU for writes 8643deb3ec6SMatthias Ringwald switch (peripheral->gatt_client_state){ 8653deb3ec6SMatthias Ringwald case P_W2_SEND_WRITE_CHARACTERISTIC_VALUE: 8663deb3ec6SMatthias Ringwald case P_W2_SEND_WRITE_CHARACTERISTIC_DESCRIPTOR: 8673deb3ec6SMatthias Ringwald if (peripheral->attribute_length <= peripheral_mtu(peripheral) - 3) break; 8683deb3ec6SMatthias Ringwald log_error("gatt_client_run: value len %u > MTU %u - 3\n", peripheral->attribute_length, peripheral_mtu(peripheral)); 8693deb3ec6SMatthias Ringwald gatt_client_handle_transaction_complete(peripheral); 8703deb3ec6SMatthias Ringwald emit_gatt_complete_event(peripheral, ATT_ERROR_INVALID_ATTRIBUTE_VALUE_LENGTH); 871544128c3SMatthias Ringwald return 0; 8723deb3ec6SMatthias Ringwald default: 8733deb3ec6SMatthias Ringwald break; 8743deb3ec6SMatthias Ringwald } 8753deb3ec6SMatthias Ringwald 8763deb3ec6SMatthias Ringwald // log_info("gatt_client_state %u", peripheral->gatt_client_state); 8773deb3ec6SMatthias Ringwald switch (peripheral->gatt_client_state){ 8783deb3ec6SMatthias Ringwald case P_W2_SEND_SERVICE_QUERY: 8793deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W4_SERVICE_QUERY_RESULT; 8803deb3ec6SMatthias Ringwald send_gatt_services_request(peripheral); 881544128c3SMatthias Ringwald return 1; 8823deb3ec6SMatthias Ringwald 8833deb3ec6SMatthias Ringwald case P_W2_SEND_SERVICE_WITH_UUID_QUERY: 8843deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W4_SERVICE_WITH_UUID_RESULT; 8853deb3ec6SMatthias Ringwald send_gatt_services_by_uuid_request(peripheral); 886544128c3SMatthias Ringwald return 1; 8873deb3ec6SMatthias Ringwald 8883deb3ec6SMatthias Ringwald case P_W2_SEND_ALL_CHARACTERISTICS_OF_SERVICE_QUERY: 8893deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W4_ALL_CHARACTERISTICS_OF_SERVICE_QUERY_RESULT; 8903deb3ec6SMatthias Ringwald send_gatt_characteristic_request(peripheral); 891544128c3SMatthias Ringwald return 1; 8923deb3ec6SMatthias Ringwald 8933deb3ec6SMatthias Ringwald case P_W2_SEND_CHARACTERISTIC_WITH_UUID_QUERY: 8943deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W4_CHARACTERISTIC_WITH_UUID_QUERY_RESULT; 8953deb3ec6SMatthias Ringwald send_gatt_characteristic_request(peripheral); 896544128c3SMatthias Ringwald return 1; 8973deb3ec6SMatthias Ringwald 8983deb3ec6SMatthias Ringwald case P_W2_SEND_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY: 8993deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W4_CHARACTERISTIC_WITH_UUID_QUERY_RESULT; 9003deb3ec6SMatthias Ringwald send_gatt_characteristic_descriptor_request(peripheral); 901544128c3SMatthias Ringwald return 1; 9023deb3ec6SMatthias Ringwald 9033deb3ec6SMatthias Ringwald case P_W2_SEND_INCLUDED_SERVICE_QUERY: 9043deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W4_INCLUDED_SERVICE_QUERY_RESULT; 9053deb3ec6SMatthias Ringwald send_gatt_included_service_request(peripheral); 906544128c3SMatthias Ringwald return 1; 9073deb3ec6SMatthias Ringwald 9083deb3ec6SMatthias Ringwald case P_W2_SEND_INCLUDED_SERVICE_WITH_UUID_QUERY: 9093deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W4_INCLUDED_SERVICE_UUID_WITH_QUERY_RESULT; 9103deb3ec6SMatthias Ringwald send_gatt_included_service_uuid_request(peripheral); 911544128c3SMatthias Ringwald return 1; 9123deb3ec6SMatthias Ringwald 9133deb3ec6SMatthias Ringwald case P_W2_SEND_READ_CHARACTERISTIC_VALUE_QUERY: 9143deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W4_READ_CHARACTERISTIC_VALUE_RESULT; 9153deb3ec6SMatthias Ringwald send_gatt_read_characteristic_value_request(peripheral); 916544128c3SMatthias Ringwald return 1; 9173deb3ec6SMatthias Ringwald 9183deb3ec6SMatthias Ringwald case P_W2_SEND_READ_BLOB_QUERY: 9193deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W4_READ_BLOB_RESULT; 9203deb3ec6SMatthias Ringwald send_gatt_read_blob_request(peripheral); 921544128c3SMatthias Ringwald return 1; 9223deb3ec6SMatthias Ringwald 9233deb3ec6SMatthias Ringwald case P_W2_SEND_READ_BY_TYPE_REQUEST: 9243deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W4_READ_BY_TYPE_RESPONSE; 9253deb3ec6SMatthias Ringwald send_gatt_read_by_type_request(peripheral); 926544128c3SMatthias Ringwald return 1; 9273deb3ec6SMatthias Ringwald 9283deb3ec6SMatthias Ringwald case P_W2_SEND_READ_MULTIPLE_REQUEST: 9293deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W4_READ_MULTIPLE_RESPONSE; 9303deb3ec6SMatthias Ringwald send_gatt_read_multiple_request(peripheral); 931544128c3SMatthias Ringwald return 1; 9323deb3ec6SMatthias Ringwald 9333deb3ec6SMatthias Ringwald case P_W2_SEND_WRITE_CHARACTERISTIC_VALUE: 9343deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W4_WRITE_CHARACTERISTIC_VALUE_RESULT; 9353deb3ec6SMatthias Ringwald send_gatt_write_attribute_value_request(peripheral); 936544128c3SMatthias Ringwald return 1; 9373deb3ec6SMatthias Ringwald 9383deb3ec6SMatthias Ringwald case P_W2_PREPARE_WRITE: 9393deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W4_PREPARE_WRITE_RESULT; 9403deb3ec6SMatthias Ringwald send_gatt_prepare_write_request(peripheral); 941544128c3SMatthias Ringwald return 1; 9423deb3ec6SMatthias Ringwald 9433deb3ec6SMatthias Ringwald case P_W2_PREPARE_WRITE_SINGLE: 9443deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W4_PREPARE_WRITE_SINGLE_RESULT; 9453deb3ec6SMatthias Ringwald send_gatt_prepare_write_request(peripheral); 946544128c3SMatthias Ringwald return 1; 9473deb3ec6SMatthias Ringwald 9483deb3ec6SMatthias Ringwald case P_W2_PREPARE_RELIABLE_WRITE: 9493deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W4_PREPARE_RELIABLE_WRITE_RESULT; 9503deb3ec6SMatthias Ringwald send_gatt_prepare_write_request(peripheral); 951544128c3SMatthias Ringwald return 1; 9523deb3ec6SMatthias Ringwald 9533deb3ec6SMatthias Ringwald case P_W2_EXECUTE_PREPARED_WRITE: 9543deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W4_EXECUTE_PREPARED_WRITE_RESULT; 9553deb3ec6SMatthias Ringwald send_gatt_execute_write_request(peripheral); 956544128c3SMatthias Ringwald return 1; 9573deb3ec6SMatthias Ringwald 9583deb3ec6SMatthias Ringwald case P_W2_CANCEL_PREPARED_WRITE: 9593deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W4_CANCEL_PREPARED_WRITE_RESULT; 9603deb3ec6SMatthias Ringwald send_gatt_cancel_prepared_write_request(peripheral); 961544128c3SMatthias Ringwald return 1; 9623deb3ec6SMatthias Ringwald 9633deb3ec6SMatthias Ringwald case P_W2_CANCEL_PREPARED_WRITE_DATA_MISMATCH: 9643deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W4_CANCEL_PREPARED_WRITE_DATA_MISMATCH_RESULT; 9653deb3ec6SMatthias Ringwald send_gatt_cancel_prepared_write_request(peripheral); 966544128c3SMatthias Ringwald return 1; 9673deb3ec6SMatthias Ringwald 968abdc9fb5SMatthias Ringwald #ifdef ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERY 969abdc9fb5SMatthias Ringwald case P_W2_SEND_FIND_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY: 970abdc9fb5SMatthias Ringwald // use Find Information 971abdc9fb5SMatthias Ringwald peripheral->gatt_client_state = P_W4_FIND_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY_RESULT; 972abdc9fb5SMatthias Ringwald send_gatt_characteristic_descriptor_request(peripheral); 973abdc9fb5SMatthias Ringwald #else 9743deb3ec6SMatthias Ringwald case P_W2_SEND_READ_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY: 975abdc9fb5SMatthias Ringwald // Use Read By Type 9763deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W4_READ_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY_RESULT; 9773deb3ec6SMatthias Ringwald send_gatt_read_client_characteristic_configuration_request(peripheral); 978abdc9fb5SMatthias Ringwald #endif 979544128c3SMatthias Ringwald return 1; 9803deb3ec6SMatthias Ringwald 9813deb3ec6SMatthias Ringwald case P_W2_SEND_READ_CHARACTERISTIC_DESCRIPTOR_QUERY: 9823deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W4_READ_CHARACTERISTIC_DESCRIPTOR_RESULT; 9833deb3ec6SMatthias Ringwald send_gatt_read_characteristic_descriptor_request(peripheral); 984544128c3SMatthias Ringwald return 1; 9853deb3ec6SMatthias Ringwald 9863deb3ec6SMatthias Ringwald case P_W2_SEND_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_QUERY: 9873deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W4_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_RESULT; 9883deb3ec6SMatthias Ringwald send_gatt_read_blob_request(peripheral); 989544128c3SMatthias Ringwald return 1; 9903deb3ec6SMatthias Ringwald 9913deb3ec6SMatthias Ringwald case P_W2_SEND_WRITE_CHARACTERISTIC_DESCRIPTOR: 9923deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W4_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT; 9933deb3ec6SMatthias Ringwald send_gatt_write_attribute_value_request(peripheral); 994544128c3SMatthias Ringwald return 1; 9953deb3ec6SMatthias Ringwald 9963deb3ec6SMatthias Ringwald case P_W2_WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION: 9973deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W4_CLIENT_CHARACTERISTIC_CONFIGURATION_RESULT; 9983deb3ec6SMatthias Ringwald send_gatt_write_client_characteristic_configuration_request(peripheral); 999544128c3SMatthias Ringwald return 1; 10003deb3ec6SMatthias Ringwald 10013deb3ec6SMatthias Ringwald case P_W2_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR: 10023deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W4_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT; 10033deb3ec6SMatthias Ringwald send_gatt_prepare_write_request(peripheral); 1004544128c3SMatthias Ringwald return 1; 10053deb3ec6SMatthias Ringwald 10063deb3ec6SMatthias Ringwald case P_W2_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR: 10073deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W4_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT; 10083deb3ec6SMatthias Ringwald send_gatt_execute_write_request(peripheral); 1009544128c3SMatthias Ringwald return 1; 10103deb3ec6SMatthias Ringwald 10117a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE 10123deb3ec6SMatthias Ringwald case P_W4_CMAC_READY: 10133deb3ec6SMatthias Ringwald if (sm_cmac_ready()){ 10143deb3ec6SMatthias Ringwald sm_key_t csrk; 10153deb3ec6SMatthias Ringwald le_device_db_local_csrk_get(peripheral->le_device_index, csrk); 10163deb3ec6SMatthias Ringwald uint32_t sign_counter = le_device_db_local_counter_get(peripheral->le_device_index); 10173deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W4_CMAC_RESULT; 10184dfd504aSMatthias 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); 10193deb3ec6SMatthias Ringwald } 1020544128c3SMatthias Ringwald return 0; 10213deb3ec6SMatthias Ringwald 10223deb3ec6SMatthias Ringwald case P_W2_SEND_SIGNED_WRITE: { 10233deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W4_SEND_SINGED_WRITE_DONE; 10243deb3ec6SMatthias Ringwald // bump local signing counter 10253deb3ec6SMatthias Ringwald uint32_t sign_counter = le_device_db_local_counter_get(peripheral->le_device_index); 10263deb3ec6SMatthias Ringwald le_device_db_local_counter_set(peripheral->le_device_index, sign_counter + 1); 10273deb3ec6SMatthias Ringwald 10283deb3ec6SMatthias Ringwald send_gatt_signed_write_request(peripheral, sign_counter); 10293deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_READY; 10303deb3ec6SMatthias Ringwald // finally, notifiy client that write is complete 10313deb3ec6SMatthias Ringwald gatt_client_handle_transaction_complete(peripheral); 1032544128c3SMatthias Ringwald return 1; 10333deb3ec6SMatthias Ringwald } 10347a766ebfSMatthias Ringwald #endif 10353deb3ec6SMatthias Ringwald default: 10363deb3ec6SMatthias Ringwald break; 10373deb3ec6SMatthias Ringwald } 103847181045SMatthias Ringwald 103947181045SMatthias Ringwald // requested can send snow? 104047181045SMatthias Ringwald if (peripheral->write_without_response_callback){ 104147181045SMatthias Ringwald btstack_packet_handler_t packet_handler = peripheral->write_without_response_callback; 104247181045SMatthias Ringwald peripheral->write_without_response_callback = NULL; 104347181045SMatthias Ringwald uint8_t event[4]; 104447181045SMatthias Ringwald event[0] = GATT_EVENT_CAN_WRITE_WITHOUT_RESPONSE; 104547181045SMatthias Ringwald event[1] = sizeof(event) - 2; 104647181045SMatthias Ringwald little_endian_store_16(event, 2, peripheral->con_handle); 104747181045SMatthias Ringwald packet_handler(HCI_EVENT_PACKET, peripheral->con_handle, event, sizeof(event)); 104847181045SMatthias Ringwald return 1; // to trigger requeueing (even if higher layer didn't sent) 104947181045SMatthias Ringwald } 105047181045SMatthias Ringwald 1051544128c3SMatthias Ringwald return 0; 10523deb3ec6SMatthias Ringwald } 10533deb3ec6SMatthias Ringwald 1054544128c3SMatthias Ringwald static void gatt_client_run(void){ 1055544128c3SMatthias Ringwald btstack_linked_item_t *it; 1056544128c3SMatthias Ringwald for (it = (btstack_linked_item_t *) gatt_client_connections; it ; it = it->next){ 1057544128c3SMatthias Ringwald gatt_client_t * peripheral = (gatt_client_t *) it; 1058544128c3SMatthias Ringwald if (!att_dispatch_client_can_send_now(peripheral->con_handle)) { 1059544128c3SMatthias Ringwald att_dispatch_client_request_can_send_now_event(peripheral->con_handle); 1060544128c3SMatthias Ringwald return; 1061544128c3SMatthias Ringwald } 1062544128c3SMatthias Ringwald int packet_sent = gatt_client_run_for_peripheral(peripheral); 1063544128c3SMatthias Ringwald if (packet_sent){ 106445f2a740SMatthias Ringwald // request new permission 1065544128c3SMatthias Ringwald att_dispatch_client_request_can_send_now_event(peripheral->con_handle); 106645f2a740SMatthias Ringwald // requeue client for fairness and exit 106745f2a740SMatthias Ringwald // note: iterator has become invalid 106845f2a740SMatthias Ringwald btstack_linked_list_remove(&gatt_client_connections, (btstack_linked_item_t *) peripheral); 106945f2a740SMatthias Ringwald btstack_linked_list_add_tail(&gatt_client_connections, (btstack_linked_item_t *) peripheral); 1070544128c3SMatthias Ringwald return; 1071544128c3SMatthias Ringwald } 1072544128c3SMatthias Ringwald } 10733deb3ec6SMatthias Ringwald } 10743deb3ec6SMatthias Ringwald 10753deb3ec6SMatthias Ringwald static void gatt_client_report_error_if_pending(gatt_client_t *peripheral, uint8_t error_code) { 10763deb3ec6SMatthias Ringwald if (is_ready(peripheral)) return; 10773deb3ec6SMatthias Ringwald gatt_client_handle_transaction_complete(peripheral); 10783deb3ec6SMatthias Ringwald emit_gatt_complete_event(peripheral, error_code); 10793deb3ec6SMatthias Ringwald } 10803deb3ec6SMatthias Ringwald 1081f4b33574SMatthias Ringwald static void gatt_client_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 10821ac26e06SMatthias Ringwald UNUSED(channel); // ok: handling own l2cap events 10831ac26e06SMatthias Ringwald UNUSED(size); // ok: there is no channel 10849ec2630cSMatthias Ringwald 1085361b1363SMatthias Ringwald if (packet_type != HCI_EVENT_PACKET) return; 1086361b1363SMatthias Ringwald 1087f4b33574SMatthias Ringwald hci_con_handle_t con_handle; 1088f4b33574SMatthias Ringwald gatt_client_t * peripheral; 10890e2df43fSMatthias Ringwald switch (hci_event_packet_get_type(packet)) { 10903deb3ec6SMatthias Ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 10913deb3ec6SMatthias Ringwald log_info("GATT Client: HCI_EVENT_DISCONNECTION_COMPLETE"); 1092f4b33574SMatthias Ringwald con_handle = little_endian_read_16(packet,3); 1093f4b33574SMatthias Ringwald peripheral = get_gatt_client_context_for_handle(con_handle); 10943deb3ec6SMatthias Ringwald if (!peripheral) break; 10953deb3ec6SMatthias Ringwald 1096*cce308d6SMatthias Ringwald gatt_client_report_error_if_pending(peripheral, ATT_ERROR_HCI_DISCONNECT_RECEIVED); 1097*cce308d6SMatthias Ringwald gatt_client_timeout_stop(peripheral); 1098665d90f2SMatthias Ringwald btstack_linked_list_remove(&gatt_client_connections, (btstack_linked_item_t *) peripheral); 10993deb3ec6SMatthias Ringwald btstack_memory_gatt_client_free(peripheral); 11003deb3ec6SMatthias Ringwald break; 1101f4b33574SMatthias Ringwald 1102f4b33574SMatthias Ringwald #ifdef ENABLE_GATT_CLIENT_PAIRING 1103f4b33574SMatthias Ringwald // Pairing complete (with/without bonding=storing of pairing information) 1104f4b33574SMatthias Ringwald case SM_EVENT_PAIRING_COMPLETE: 1105f4b33574SMatthias Ringwald con_handle = sm_event_pairing_complete_get_handle(packet); 1106f4b33574SMatthias Ringwald peripheral = get_gatt_client_context_for_handle(con_handle); 1107f4b33574SMatthias Ringwald if (!peripheral) break; 1108f4b33574SMatthias Ringwald 1109f4b33574SMatthias Ringwald if (peripheral->wait_for_pairing_complete){ 1110f4b33574SMatthias Ringwald peripheral->wait_for_pairing_complete = 0; 1111f4b33574SMatthias Ringwald if (sm_event_pairing_complete_get_status(packet)){ 1112f4b33574SMatthias Ringwald log_info("pairing failed, report previous error 0x%x", peripheral->pending_error_code); 1113f4b33574SMatthias Ringwald gatt_client_handle_transaction_complete(peripheral); 1114f4b33574SMatthias Ringwald emit_gatt_complete_event(peripheral, peripheral->pending_error_code); 1115f4b33574SMatthias Ringwald } else { 1116f4b33574SMatthias Ringwald log_info("pairing success, retry operation"); 11173deb3ec6SMatthias Ringwald } 1118f4b33574SMatthias Ringwald } 1119f4b33574SMatthias Ringwald break; 1120f4b33574SMatthias Ringwald #endif 1121f4b33574SMatthias Ringwald 11223deb3ec6SMatthias Ringwald default: 11233deb3ec6SMatthias Ringwald break; 11243deb3ec6SMatthias Ringwald } 11253deb3ec6SMatthias Ringwald 1126361b1363SMatthias Ringwald gatt_client_run(); 11273deb3ec6SMatthias Ringwald } 11283deb3ec6SMatthias Ringwald 11293deb3ec6SMatthias Ringwald static void gatt_client_att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){ 11303deb3ec6SMatthias Ringwald 11310fbf59a1SMatthias Ringwald gatt_client_t * peripheral; 11320fbf59a1SMatthias Ringwald 11330fbf59a1SMatthias Ringwald if (packet_type == HCI_EVENT_PACKET) { 11340fbf59a1SMatthias Ringwald switch (packet[0]){ 11350fbf59a1SMatthias Ringwald case L2CAP_EVENT_CAN_SEND_NOW: 113651764b3bSMatthias Ringwald gatt_client_run(); 11370fbf59a1SMatthias Ringwald break; 11380fbf59a1SMatthias Ringwald // att_server has negotiated the mtu for this connection, cache if context exists 11390fbf59a1SMatthias Ringwald case ATT_EVENT_MTU_EXCHANGE_COMPLETE: 11400fbf59a1SMatthias Ringwald peripheral = get_gatt_client_context_for_handle(handle); 11410fbf59a1SMatthias Ringwald if (!peripheral) break; 11420fbf59a1SMatthias Ringwald peripheral->mtu = little_endian_read_16(packet, 4); 11430fbf59a1SMatthias Ringwald break; 11440fbf59a1SMatthias Ringwald default: 11450fbf59a1SMatthias Ringwald break; 11460fbf59a1SMatthias Ringwald } 11470fbf59a1SMatthias Ringwald return; 114851764b3bSMatthias Ringwald } 114951764b3bSMatthias Ringwald 11503deb3ec6SMatthias Ringwald if (packet_type != ATT_DATA_PACKET) return; 11513deb3ec6SMatthias Ringwald 11523deb3ec6SMatthias Ringwald // special cases: notifications don't need a context while indications motivate creating one 11533deb3ec6SMatthias Ringwald switch (packet[0]){ 11543deb3ec6SMatthias Ringwald case ATT_HANDLE_VALUE_NOTIFICATION: 1155f8fbdce0SMatthias Ringwald report_gatt_notification(handle, little_endian_read_16(packet,1), &packet[3], size-3); 11563deb3ec6SMatthias Ringwald return; 11573deb3ec6SMatthias Ringwald case ATT_HANDLE_VALUE_INDICATION: 11583deb3ec6SMatthias Ringwald peripheral = provide_context_for_conn_handle(handle); 11593deb3ec6SMatthias Ringwald break; 11603deb3ec6SMatthias Ringwald default: 11613deb3ec6SMatthias Ringwald peripheral = get_gatt_client_context_for_handle(handle); 11623deb3ec6SMatthias Ringwald break; 11633deb3ec6SMatthias Ringwald } 11643deb3ec6SMatthias Ringwald 11653deb3ec6SMatthias Ringwald if (!peripheral) return; 11663deb3ec6SMatthias Ringwald 11673deb3ec6SMatthias Ringwald switch (packet[0]){ 11683deb3ec6SMatthias Ringwald case ATT_EXCHANGE_MTU_RESPONSE: 11693deb3ec6SMatthias Ringwald { 1170f8fbdce0SMatthias Ringwald uint16_t remote_rx_mtu = little_endian_read_16(packet, 1); 11713deb3ec6SMatthias Ringwald uint16_t local_rx_mtu = l2cap_max_le_mtu(); 11723deb3ec6SMatthias Ringwald peripheral->mtu = remote_rx_mtu < local_rx_mtu ? remote_rx_mtu : local_rx_mtu; 11733deb3ec6SMatthias Ringwald peripheral->mtu_state = MTU_EXCHANGED; 11748f37572aSJakob Krantz emit_gatt_mtu_exchanged_result_event(peripheral, peripheral->mtu); 11753deb3ec6SMatthias Ringwald break; 11763deb3ec6SMatthias Ringwald } 11773deb3ec6SMatthias Ringwald case ATT_READ_BY_GROUP_TYPE_RESPONSE: 11783deb3ec6SMatthias Ringwald switch(peripheral->gatt_client_state){ 11793deb3ec6SMatthias Ringwald case P_W4_SERVICE_QUERY_RESULT: 11803deb3ec6SMatthias Ringwald report_gatt_services(peripheral, packet, size); 11813deb3ec6SMatthias Ringwald trigger_next_service_query(peripheral, get_last_result_handle_from_service_list(packet, size)); 11825611a760SMatthias Ringwald // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done 11833deb3ec6SMatthias Ringwald break; 11843deb3ec6SMatthias Ringwald default: 11853deb3ec6SMatthias Ringwald break; 11863deb3ec6SMatthias Ringwald } 11873deb3ec6SMatthias Ringwald break; 11883deb3ec6SMatthias Ringwald case ATT_HANDLE_VALUE_INDICATION: 1189f8fbdce0SMatthias Ringwald report_gatt_indication(handle, little_endian_read_16(packet,1), &packet[3], size-3); 11903deb3ec6SMatthias Ringwald peripheral->send_confirmation = 1; 11913deb3ec6SMatthias Ringwald break; 11923deb3ec6SMatthias Ringwald 11933deb3ec6SMatthias Ringwald case ATT_READ_BY_TYPE_RESPONSE: 11943deb3ec6SMatthias Ringwald switch (peripheral->gatt_client_state){ 11953deb3ec6SMatthias Ringwald case P_W4_ALL_CHARACTERISTICS_OF_SERVICE_QUERY_RESULT: 11963deb3ec6SMatthias Ringwald report_gatt_characteristics(peripheral, packet, size); 11973deb3ec6SMatthias Ringwald trigger_next_characteristic_query(peripheral, get_last_result_handle_from_characteristics_list(packet, size)); 11985611a760SMatthias Ringwald // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done, or by ATT_ERROR 11993deb3ec6SMatthias Ringwald break; 12003deb3ec6SMatthias Ringwald case P_W4_CHARACTERISTIC_WITH_UUID_QUERY_RESULT: 12013deb3ec6SMatthias Ringwald report_gatt_characteristics(peripheral, packet, size); 12023deb3ec6SMatthias Ringwald trigger_next_characteristic_query(peripheral, get_last_result_handle_from_characteristics_list(packet, size)); 12035611a760SMatthias Ringwald // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done, or by ATT_ERROR 12043deb3ec6SMatthias Ringwald break; 12053deb3ec6SMatthias Ringwald case P_W4_INCLUDED_SERVICE_QUERY_RESULT: 12063deb3ec6SMatthias Ringwald { 12073deb3ec6SMatthias Ringwald uint16_t uuid16 = 0; 12083deb3ec6SMatthias Ringwald uint16_t pair_size = packet[1]; 12093deb3ec6SMatthias Ringwald 12103deb3ec6SMatthias Ringwald if (pair_size < 7){ 12113deb3ec6SMatthias Ringwald // UUIDs not available, query first included service 1212f8fbdce0SMatthias Ringwald peripheral->start_group_handle = little_endian_read_16(packet, 2); // ready for next query 1213f8fbdce0SMatthias Ringwald peripheral->query_start_handle = little_endian_read_16(packet, 4); 1214f8fbdce0SMatthias Ringwald peripheral->query_end_handle = little_endian_read_16(packet,6); 12153deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W2_SEND_INCLUDED_SERVICE_WITH_UUID_QUERY; 12163deb3ec6SMatthias Ringwald break; 12173deb3ec6SMatthias Ringwald } 12183deb3ec6SMatthias Ringwald 12193deb3ec6SMatthias Ringwald uint16_t offset; 12203deb3ec6SMatthias Ringwald for (offset = 2; offset < size; offset += pair_size){ 1221f8fbdce0SMatthias Ringwald uint16_t include_handle = little_endian_read_16(packet, offset); 1222f8fbdce0SMatthias Ringwald peripheral->query_start_handle = little_endian_read_16(packet,offset+2); 1223f8fbdce0SMatthias Ringwald peripheral->query_end_handle = little_endian_read_16(packet,offset+4); 1224f8fbdce0SMatthias Ringwald uuid16 = little_endian_read_16(packet, offset+6); 12253deb3ec6SMatthias Ringwald report_gatt_included_service_uuid16(peripheral, include_handle, uuid16); 12263deb3ec6SMatthias Ringwald } 12273deb3ec6SMatthias Ringwald 12283deb3ec6SMatthias Ringwald trigger_next_included_service_query(peripheral, get_last_result_handle_from_included_services_list(packet, size)); 12295611a760SMatthias Ringwald // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done 12303deb3ec6SMatthias Ringwald break; 12313deb3ec6SMatthias Ringwald } 1232abdc9fb5SMatthias Ringwald #ifndef ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERY 12333deb3ec6SMatthias Ringwald case P_W4_READ_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY_RESULT: 1234f8fbdce0SMatthias Ringwald peripheral->client_characteristic_configuration_handle = little_endian_read_16(packet, 2); 12353deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W2_WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION; 12363deb3ec6SMatthias Ringwald break; 1237abdc9fb5SMatthias Ringwald #endif 12383deb3ec6SMatthias Ringwald case P_W4_READ_BY_TYPE_RESPONSE: { 12393deb3ec6SMatthias Ringwald uint16_t pair_size = packet[1]; 12403deb3ec6SMatthias Ringwald uint16_t offset; 12413deb3ec6SMatthias Ringwald uint16_t last_result_handle = 0; 12423deb3ec6SMatthias Ringwald for (offset = 2; offset < size ; offset += pair_size){ 1243f8fbdce0SMatthias Ringwald uint16_t value_handle = little_endian_read_16(packet, offset); 12443deb3ec6SMatthias Ringwald report_gatt_characteristic_value(peripheral, value_handle, &packet[offset+2], pair_size-2); 12453deb3ec6SMatthias Ringwald last_result_handle = value_handle; 12463deb3ec6SMatthias Ringwald } 12473deb3ec6SMatthias Ringwald trigger_next_read_by_type_query(peripheral, last_result_handle); 12483deb3ec6SMatthias Ringwald break; 12493deb3ec6SMatthias Ringwald } 12503deb3ec6SMatthias Ringwald default: 12513deb3ec6SMatthias Ringwald break; 12523deb3ec6SMatthias Ringwald } 12533deb3ec6SMatthias Ringwald break; 12543deb3ec6SMatthias Ringwald case ATT_READ_RESPONSE: 12553deb3ec6SMatthias Ringwald switch (peripheral->gatt_client_state){ 12563deb3ec6SMatthias Ringwald case P_W4_INCLUDED_SERVICE_UUID_WITH_QUERY_RESULT: { 12573deb3ec6SMatthias Ringwald uint8_t uuid128[16]; 12589c80e4ccSMatthias Ringwald reverse_128(&packet[1], uuid128); 12593deb3ec6SMatthias Ringwald report_gatt_included_service_uuid128(peripheral, peripheral->start_group_handle, uuid128); 12603deb3ec6SMatthias Ringwald trigger_next_included_service_query(peripheral, peripheral->start_group_handle); 12615611a760SMatthias Ringwald // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done 12623deb3ec6SMatthias Ringwald break; 12633deb3ec6SMatthias Ringwald } 12643deb3ec6SMatthias Ringwald case P_W4_READ_CHARACTERISTIC_VALUE_RESULT: 12653deb3ec6SMatthias Ringwald gatt_client_handle_transaction_complete(peripheral); 12663deb3ec6SMatthias Ringwald report_gatt_characteristic_value(peripheral, peripheral->attribute_handle, &packet[1], size-1); 12673deb3ec6SMatthias Ringwald emit_gatt_complete_event(peripheral, 0); 12683deb3ec6SMatthias Ringwald break; 12693deb3ec6SMatthias Ringwald 12703deb3ec6SMatthias Ringwald case P_W4_READ_CHARACTERISTIC_DESCRIPTOR_RESULT:{ 12713deb3ec6SMatthias Ringwald gatt_client_handle_transaction_complete(peripheral); 12723deb3ec6SMatthias Ringwald report_gatt_characteristic_descriptor(peripheral, peripheral->attribute_handle, &packet[1], size-1, 0); 12733deb3ec6SMatthias Ringwald emit_gatt_complete_event(peripheral, 0); 12743deb3ec6SMatthias Ringwald break; 12753deb3ec6SMatthias Ringwald } 12763deb3ec6SMatthias Ringwald default: 12773deb3ec6SMatthias Ringwald break; 12783deb3ec6SMatthias Ringwald } 12793deb3ec6SMatthias Ringwald break; 12803deb3ec6SMatthias Ringwald 12813deb3ec6SMatthias Ringwald case ATT_FIND_BY_TYPE_VALUE_RESPONSE: 12823deb3ec6SMatthias Ringwald { 12833deb3ec6SMatthias Ringwald uint8_t pair_size = 4; 12843deb3ec6SMatthias Ringwald int i; 12853deb3ec6SMatthias Ringwald uint16_t start_group_handle; 12865611a760SMatthias Ringwald uint16_t end_group_handle= 0xffff; // asserts GATT_EVENT_QUERY_COMPLETE is emitted if no results 12873deb3ec6SMatthias Ringwald for (i = 1; i<size; i+=pair_size){ 1288f8fbdce0SMatthias Ringwald start_group_handle = little_endian_read_16(packet,i); 1289f8fbdce0SMatthias Ringwald end_group_handle = little_endian_read_16(packet,i+2); 12903deb3ec6SMatthias Ringwald emit_gatt_service_query_result_event(peripheral, start_group_handle, end_group_handle, peripheral->uuid128); 12913deb3ec6SMatthias Ringwald } 12923deb3ec6SMatthias Ringwald trigger_next_service_by_uuid_query(peripheral, end_group_handle); 12935611a760SMatthias Ringwald // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done 12943deb3ec6SMatthias Ringwald break; 12953deb3ec6SMatthias Ringwald } 12963deb3ec6SMatthias Ringwald case ATT_FIND_INFORMATION_REPLY: 12973deb3ec6SMatthias Ringwald { 12983deb3ec6SMatthias Ringwald uint8_t pair_size = 4; 12993deb3ec6SMatthias Ringwald if (packet[1] == 2){ 13003deb3ec6SMatthias Ringwald pair_size = 18; 13013deb3ec6SMatthias Ringwald } 1302f8fbdce0SMatthias Ringwald uint16_t last_descriptor_handle = little_endian_read_16(packet, size - pair_size); 1303abdc9fb5SMatthias Ringwald 1304abdc9fb5SMatthias Ringwald #ifdef ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERY 1305abdc9fb5SMatthias Ringwald log_info("ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERY, state %x", peripheral->gatt_client_state); 1306abdc9fb5SMatthias Ringwald if (peripheral->gatt_client_state == P_W4_FIND_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY_RESULT){ 1307abdc9fb5SMatthias Ringwald // iterate over descriptors looking for CCC 1308abdc9fb5SMatthias Ringwald if (pair_size == 4){ 1309abdc9fb5SMatthias Ringwald int offset = 2; 1310abdc9fb5SMatthias Ringwald while (offset < size){ 1311abdc9fb5SMatthias Ringwald uint16_t uuid16 = little_endian_read_16(packet, offset + 2); 1312abdc9fb5SMatthias Ringwald if (uuid16 == GATT_CLIENT_CHARACTERISTICS_CONFIGURATION){ 1313abdc9fb5SMatthias Ringwald peripheral->client_characteristic_configuration_handle = little_endian_read_16(packet, offset); 1314abdc9fb5SMatthias Ringwald peripheral->gatt_client_state = P_W2_WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION; 1315abdc9fb5SMatthias Ringwald log_info("CCC found %x", peripheral->client_characteristic_configuration_handle); 1316abdc9fb5SMatthias Ringwald break; 1317abdc9fb5SMatthias Ringwald } 1318abdc9fb5SMatthias Ringwald offset += pair_size; 1319abdc9fb5SMatthias Ringwald } 1320abdc9fb5SMatthias Ringwald } 1321abdc9fb5SMatthias Ringwald if (is_query_done(peripheral, last_descriptor_handle)){ 1322abdc9fb5SMatthias Ringwald 1323abdc9fb5SMatthias Ringwald } else { 1324abdc9fb5SMatthias Ringwald // next 1325abdc9fb5SMatthias Ringwald peripheral->start_group_handle = last_descriptor_handle + 1; 1326abdc9fb5SMatthias Ringwald peripheral->gatt_client_state = P_W2_SEND_FIND_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY; 1327abdc9fb5SMatthias Ringwald } 1328abdc9fb5SMatthias Ringwald break; 1329abdc9fb5SMatthias Ringwald } 1330abdc9fb5SMatthias Ringwald #endif 13313deb3ec6SMatthias Ringwald report_gatt_all_characteristic_descriptors(peripheral, &packet[2], size-2, pair_size); 13323deb3ec6SMatthias Ringwald trigger_next_characteristic_descriptor_query(peripheral, last_descriptor_handle); 13335611a760SMatthias Ringwald // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done 13343deb3ec6SMatthias Ringwald break; 13353deb3ec6SMatthias Ringwald } 13363deb3ec6SMatthias Ringwald 13373deb3ec6SMatthias Ringwald case ATT_WRITE_RESPONSE: 13383deb3ec6SMatthias Ringwald switch (peripheral->gatt_client_state){ 13393deb3ec6SMatthias Ringwald case P_W4_WRITE_CHARACTERISTIC_VALUE_RESULT: 13403deb3ec6SMatthias Ringwald gatt_client_handle_transaction_complete(peripheral); 13413deb3ec6SMatthias Ringwald emit_gatt_complete_event(peripheral, 0); 13423deb3ec6SMatthias Ringwald break; 13433deb3ec6SMatthias Ringwald case P_W4_CLIENT_CHARACTERISTIC_CONFIGURATION_RESULT: 13443deb3ec6SMatthias Ringwald gatt_client_handle_transaction_complete(peripheral); 13453deb3ec6SMatthias Ringwald emit_gatt_complete_event(peripheral, 0); 13463deb3ec6SMatthias Ringwald break; 13473deb3ec6SMatthias Ringwald case P_W4_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT: 13483deb3ec6SMatthias Ringwald gatt_client_handle_transaction_complete(peripheral); 13493deb3ec6SMatthias Ringwald emit_gatt_complete_event(peripheral, 0); 13503deb3ec6SMatthias Ringwald break; 13513deb3ec6SMatthias Ringwald default: 13523deb3ec6SMatthias Ringwald break; 13533deb3ec6SMatthias Ringwald } 13543deb3ec6SMatthias Ringwald break; 13553deb3ec6SMatthias Ringwald 13563deb3ec6SMatthias Ringwald case ATT_READ_BLOB_RESPONSE:{ 13573deb3ec6SMatthias Ringwald uint16_t received_blob_length = size-1; 13583deb3ec6SMatthias Ringwald switch(peripheral->gatt_client_state){ 13593deb3ec6SMatthias Ringwald case P_W4_READ_BLOB_RESULT: 13603deb3ec6SMatthias Ringwald report_gatt_long_characteristic_value_blob(peripheral, peripheral->attribute_handle, &packet[1], received_blob_length, peripheral->attribute_offset); 13613deb3ec6SMatthias Ringwald trigger_next_blob_query(peripheral, P_W2_SEND_READ_BLOB_QUERY, received_blob_length); 13625611a760SMatthias Ringwald // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done 13633deb3ec6SMatthias Ringwald break; 13643deb3ec6SMatthias Ringwald case P_W4_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_RESULT: 13653deb3ec6SMatthias Ringwald report_gatt_long_characteristic_descriptor(peripheral, peripheral->attribute_handle, 13663deb3ec6SMatthias Ringwald &packet[1], received_blob_length, 13673deb3ec6SMatthias Ringwald peripheral->attribute_offset); 13683deb3ec6SMatthias Ringwald trigger_next_blob_query(peripheral, P_W2_SEND_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_QUERY, received_blob_length); 13695611a760SMatthias Ringwald // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done 13703deb3ec6SMatthias Ringwald break; 13713deb3ec6SMatthias Ringwald default: 13723deb3ec6SMatthias Ringwald break; 13733deb3ec6SMatthias Ringwald } 13743deb3ec6SMatthias Ringwald break; 13753deb3ec6SMatthias Ringwald } 13763deb3ec6SMatthias Ringwald case ATT_PREPARE_WRITE_RESPONSE: 13773deb3ec6SMatthias Ringwald switch (peripheral->gatt_client_state){ 13783deb3ec6SMatthias Ringwald case P_W4_PREPARE_WRITE_SINGLE_RESULT: 13793deb3ec6SMatthias Ringwald gatt_client_handle_transaction_complete(peripheral); 13803deb3ec6SMatthias Ringwald if (is_value_valid(peripheral, packet, size)){ 13813deb3ec6SMatthias Ringwald emit_gatt_complete_event(peripheral, 0); 13823deb3ec6SMatthias Ringwald } else { 13833deb3ec6SMatthias Ringwald emit_gatt_complete_event(peripheral, ATT_ERROR_DATA_MISMATCH); 13843deb3ec6SMatthias Ringwald } 13853deb3ec6SMatthias Ringwald break; 13863deb3ec6SMatthias Ringwald 13873deb3ec6SMatthias Ringwald case P_W4_PREPARE_WRITE_RESULT:{ 1388f8fbdce0SMatthias Ringwald peripheral->attribute_offset = little_endian_read_16(packet, 3); 13893deb3ec6SMatthias Ringwald trigger_next_prepare_write_query(peripheral, P_W2_PREPARE_WRITE, P_W2_EXECUTE_PREPARED_WRITE); 13905611a760SMatthias Ringwald // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done 13913deb3ec6SMatthias Ringwald break; 13923deb3ec6SMatthias Ringwald } 13933deb3ec6SMatthias Ringwald case P_W4_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT:{ 1394f8fbdce0SMatthias Ringwald peripheral->attribute_offset = little_endian_read_16(packet, 3); 13953deb3ec6SMatthias Ringwald trigger_next_prepare_write_query(peripheral, P_W2_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR, P_W2_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR); 13965611a760SMatthias Ringwald // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done 13973deb3ec6SMatthias Ringwald break; 13983deb3ec6SMatthias Ringwald } 13993deb3ec6SMatthias Ringwald case P_W4_PREPARE_RELIABLE_WRITE_RESULT:{ 14003deb3ec6SMatthias Ringwald if (is_value_valid(peripheral, packet, size)){ 1401f8fbdce0SMatthias Ringwald peripheral->attribute_offset = little_endian_read_16(packet, 3); 14023deb3ec6SMatthias Ringwald trigger_next_prepare_write_query(peripheral, P_W2_PREPARE_RELIABLE_WRITE, P_W2_EXECUTE_PREPARED_WRITE); 14035611a760SMatthias Ringwald // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done 14043deb3ec6SMatthias Ringwald break; 14053deb3ec6SMatthias Ringwald } 14063deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W2_CANCEL_PREPARED_WRITE_DATA_MISMATCH; 14073deb3ec6SMatthias Ringwald break; 14083deb3ec6SMatthias Ringwald } 14093deb3ec6SMatthias Ringwald default: 14103deb3ec6SMatthias Ringwald break; 14113deb3ec6SMatthias Ringwald } 14123deb3ec6SMatthias Ringwald break; 14133deb3ec6SMatthias Ringwald 14143deb3ec6SMatthias Ringwald case ATT_EXECUTE_WRITE_RESPONSE: 14153deb3ec6SMatthias Ringwald switch (peripheral->gatt_client_state){ 14163deb3ec6SMatthias Ringwald case P_W4_EXECUTE_PREPARED_WRITE_RESULT: 14173deb3ec6SMatthias Ringwald gatt_client_handle_transaction_complete(peripheral); 14183deb3ec6SMatthias Ringwald emit_gatt_complete_event(peripheral, 0); 14193deb3ec6SMatthias Ringwald break; 14203deb3ec6SMatthias Ringwald case P_W4_CANCEL_PREPARED_WRITE_RESULT: 14213deb3ec6SMatthias Ringwald gatt_client_handle_transaction_complete(peripheral); 14223deb3ec6SMatthias Ringwald emit_gatt_complete_event(peripheral, 0); 14233deb3ec6SMatthias Ringwald break; 14243deb3ec6SMatthias Ringwald case P_W4_CANCEL_PREPARED_WRITE_DATA_MISMATCH_RESULT: 14253deb3ec6SMatthias Ringwald gatt_client_handle_transaction_complete(peripheral); 14263deb3ec6SMatthias Ringwald emit_gatt_complete_event(peripheral, ATT_ERROR_DATA_MISMATCH); 14273deb3ec6SMatthias Ringwald break; 14283deb3ec6SMatthias Ringwald case P_W4_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT: 14293deb3ec6SMatthias Ringwald gatt_client_handle_transaction_complete(peripheral); 14303deb3ec6SMatthias Ringwald emit_gatt_complete_event(peripheral, 0); 14313deb3ec6SMatthias Ringwald break; 14323deb3ec6SMatthias Ringwald default: 14333deb3ec6SMatthias Ringwald break; 14343deb3ec6SMatthias Ringwald 14353deb3ec6SMatthias Ringwald } 14363deb3ec6SMatthias Ringwald break; 14373deb3ec6SMatthias Ringwald 14383deb3ec6SMatthias Ringwald case ATT_READ_MULTIPLE_RESPONSE: 14393deb3ec6SMatthias Ringwald switch(peripheral->gatt_client_state){ 14403deb3ec6SMatthias Ringwald case P_W4_READ_MULTIPLE_RESPONSE: 14413deb3ec6SMatthias Ringwald report_gatt_characteristic_value(peripheral, 0, &packet[1], size-1); 14423deb3ec6SMatthias Ringwald gatt_client_handle_transaction_complete(peripheral); 14433deb3ec6SMatthias Ringwald emit_gatt_complete_event(peripheral, 0); 14443deb3ec6SMatthias Ringwald break; 14453deb3ec6SMatthias Ringwald default: 14463deb3ec6SMatthias Ringwald break; 14473deb3ec6SMatthias Ringwald } 14483deb3ec6SMatthias Ringwald break; 14493deb3ec6SMatthias Ringwald 14503deb3ec6SMatthias Ringwald case ATT_ERROR_RESPONSE: 14513deb3ec6SMatthias Ringwald 14523deb3ec6SMatthias Ringwald switch (packet[4]){ 14533deb3ec6SMatthias Ringwald case ATT_ERROR_ATTRIBUTE_NOT_FOUND: { 14543deb3ec6SMatthias Ringwald switch(peripheral->gatt_client_state){ 14553deb3ec6SMatthias Ringwald case P_W4_SERVICE_QUERY_RESULT: 14563deb3ec6SMatthias Ringwald case P_W4_SERVICE_WITH_UUID_RESULT: 14573deb3ec6SMatthias Ringwald case P_W4_INCLUDED_SERVICE_QUERY_RESULT: 14583deb3ec6SMatthias Ringwald case P_W4_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY_RESULT: 14593deb3ec6SMatthias Ringwald gatt_client_handle_transaction_complete(peripheral); 14603deb3ec6SMatthias Ringwald emit_gatt_complete_event(peripheral, 0); 14613deb3ec6SMatthias Ringwald break; 14623deb3ec6SMatthias Ringwald case P_W4_ALL_CHARACTERISTICS_OF_SERVICE_QUERY_RESULT: 14633deb3ec6SMatthias Ringwald case P_W4_CHARACTERISTIC_WITH_UUID_QUERY_RESULT: 14643deb3ec6SMatthias Ringwald characteristic_end_found(peripheral, peripheral->end_group_handle); 14653deb3ec6SMatthias Ringwald gatt_client_handle_transaction_complete(peripheral); 14663deb3ec6SMatthias Ringwald emit_gatt_complete_event(peripheral, 0); 14673deb3ec6SMatthias Ringwald break; 14683deb3ec6SMatthias Ringwald case P_W4_READ_BY_TYPE_RESPONSE: 14693deb3ec6SMatthias Ringwald gatt_client_handle_transaction_complete(peripheral); 14703deb3ec6SMatthias Ringwald if (peripheral->start_group_handle == peripheral->query_start_handle){ 14713deb3ec6SMatthias Ringwald emit_gatt_complete_event(peripheral, ATT_ERROR_ATTRIBUTE_NOT_FOUND); 14723deb3ec6SMatthias Ringwald } else { 14733deb3ec6SMatthias Ringwald emit_gatt_complete_event(peripheral, 0); 14743deb3ec6SMatthias Ringwald } 14753deb3ec6SMatthias Ringwald break; 14763deb3ec6SMatthias Ringwald default: 14773deb3ec6SMatthias Ringwald gatt_client_report_error_if_pending(peripheral, packet[4]); 14783deb3ec6SMatthias Ringwald break; 14793deb3ec6SMatthias Ringwald } 14803deb3ec6SMatthias Ringwald break; 14813deb3ec6SMatthias Ringwald } 1482f4b33574SMatthias Ringwald 1483f4b33574SMatthias Ringwald #ifdef ENABLE_GATT_CLIENT_PAIRING 1484f4b33574SMatthias Ringwald 1485f4b33574SMatthias Ringwald case ATT_ERROR_INSUFFICIENT_AUTHENTICATION: 1486f4b33574SMatthias Ringwald case ATT_ERROR_INSUFFICIENT_ENCRYPTION_KEY_SIZE: 1487f4b33574SMatthias Ringwald case ATT_ERROR_INSUFFICIENT_ENCRYPTION: 1488f4b33574SMatthias Ringwald // security too low 1489f4b33574SMatthias Ringwald if (peripheral->security_counter > 0) { 1490f4b33574SMatthias Ringwald gatt_client_report_error_if_pending(peripheral, packet[4]); 1491f4b33574SMatthias Ringwald break; 1492f4b33574SMatthias Ringwald } 1493f4b33574SMatthias Ringwald // start security 1494f4b33574SMatthias Ringwald peripheral->security_counter++; 1495f4b33574SMatthias Ringwald 1496f4b33574SMatthias Ringwald // setup action 1497f4b33574SMatthias Ringwald int retry = 1; 1498f4b33574SMatthias Ringwald switch (peripheral->gatt_client_state){ 1499f4b33574SMatthias Ringwald case P_W4_READ_CHARACTERISTIC_VALUE_RESULT: 1500f4b33574SMatthias Ringwald peripheral->gatt_client_state = P_W2_SEND_READ_CHARACTERISTIC_VALUE_QUERY ; 1501f4b33574SMatthias Ringwald break; 1502f4b33574SMatthias Ringwald case P_W4_READ_BLOB_RESULT: 1503f4b33574SMatthias Ringwald peripheral->gatt_client_state = P_W2_SEND_READ_BLOB_QUERY; 1504f4b33574SMatthias Ringwald break; 1505f4b33574SMatthias Ringwald case P_W4_READ_BY_TYPE_RESPONSE: 1506f4b33574SMatthias Ringwald peripheral->gatt_client_state = P_W2_SEND_READ_BY_TYPE_REQUEST; 1507f4b33574SMatthias Ringwald break; 1508f4b33574SMatthias Ringwald case P_W4_READ_MULTIPLE_RESPONSE: 1509f4b33574SMatthias Ringwald peripheral->gatt_client_state = P_W2_SEND_READ_MULTIPLE_REQUEST; 1510f4b33574SMatthias Ringwald break; 1511f4b33574SMatthias Ringwald case P_W4_WRITE_CHARACTERISTIC_VALUE_RESULT: 1512f4b33574SMatthias Ringwald peripheral->gatt_client_state = P_W2_SEND_WRITE_CHARACTERISTIC_VALUE; 1513f4b33574SMatthias Ringwald break; 1514f4b33574SMatthias Ringwald case P_W4_PREPARE_WRITE_RESULT: 1515f4b33574SMatthias Ringwald peripheral->gatt_client_state = P_W2_PREPARE_WRITE; 1516f4b33574SMatthias Ringwald break; 1517f4b33574SMatthias Ringwald case P_W4_PREPARE_WRITE_SINGLE_RESULT: 1518f4b33574SMatthias Ringwald peripheral->gatt_client_state = P_W2_PREPARE_WRITE_SINGLE; 1519f4b33574SMatthias Ringwald break; 1520f4b33574SMatthias Ringwald case P_W4_PREPARE_RELIABLE_WRITE_RESULT: 1521f4b33574SMatthias Ringwald peripheral->gatt_client_state = P_W2_PREPARE_RELIABLE_WRITE; 1522f4b33574SMatthias Ringwald break; 1523f4b33574SMatthias Ringwald case P_W4_EXECUTE_PREPARED_WRITE_RESULT: 1524f4b33574SMatthias Ringwald peripheral->gatt_client_state = P_W2_EXECUTE_PREPARED_WRITE; 1525f4b33574SMatthias Ringwald break; 1526f4b33574SMatthias Ringwald case P_W4_CANCEL_PREPARED_WRITE_RESULT: 1527f4b33574SMatthias Ringwald peripheral->gatt_client_state = P_W2_CANCEL_PREPARED_WRITE; 1528f4b33574SMatthias Ringwald break; 1529f4b33574SMatthias Ringwald case P_W4_CANCEL_PREPARED_WRITE_DATA_MISMATCH_RESULT: 1530f4b33574SMatthias Ringwald peripheral->gatt_client_state = P_W2_CANCEL_PREPARED_WRITE_DATA_MISMATCH; 1531f4b33574SMatthias Ringwald break; 1532f4b33574SMatthias Ringwald case P_W4_READ_CHARACTERISTIC_DESCRIPTOR_RESULT: 1533f4b33574SMatthias Ringwald peripheral->gatt_client_state = P_W2_SEND_READ_CHARACTERISTIC_DESCRIPTOR_QUERY; 1534f4b33574SMatthias Ringwald break; 1535f4b33574SMatthias Ringwald case P_W4_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_RESULT: 1536f4b33574SMatthias Ringwald peripheral->gatt_client_state = P_W2_SEND_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_QUERY; 1537f4b33574SMatthias Ringwald break; 1538f4b33574SMatthias Ringwald case P_W4_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT: 1539f4b33574SMatthias Ringwald peripheral->gatt_client_state = P_W2_SEND_WRITE_CHARACTERISTIC_DESCRIPTOR; 1540f4b33574SMatthias Ringwald break; 1541f4b33574SMatthias Ringwald case P_W4_CLIENT_CHARACTERISTIC_CONFIGURATION_RESULT: 1542f4b33574SMatthias Ringwald peripheral->gatt_client_state = P_W2_WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION; 1543f4b33574SMatthias Ringwald break; 1544f4b33574SMatthias Ringwald case P_W4_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT: 1545f4b33574SMatthias Ringwald peripheral->gatt_client_state = P_W2_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR; 1546f4b33574SMatthias Ringwald break; 1547f4b33574SMatthias Ringwald case P_W4_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT: 1548f4b33574SMatthias Ringwald peripheral->gatt_client_state = P_W2_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR; 1549f4b33574SMatthias Ringwald break; 1550f4b33574SMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE 1551f4b33574SMatthias Ringwald case P_W4_SEND_SINGED_WRITE_DONE: 1552f4b33574SMatthias Ringwald peripheral->gatt_client_state = P_W2_SEND_SIGNED_WRITE; 1553f4b33574SMatthias Ringwald break; 1554f4b33574SMatthias Ringwald #endif 1555f4b33574SMatthias Ringwald default: 1556f4b33574SMatthias Ringwald log_info("retry not supported for state %x", peripheral->gatt_client_state); 1557f4b33574SMatthias Ringwald retry = 0; 1558f4b33574SMatthias Ringwald break; 1559f4b33574SMatthias Ringwald } 1560f4b33574SMatthias Ringwald 1561f4b33574SMatthias Ringwald if (!retry) { 1562f4b33574SMatthias Ringwald gatt_client_report_error_if_pending(peripheral, packet[4]); 1563f4b33574SMatthias Ringwald break; 1564f4b33574SMatthias Ringwald } 1565f4b33574SMatthias Ringwald 1566f4b33574SMatthias Ringwald log_info("security error, start pairing"); 1567f4b33574SMatthias Ringwald 1568f4b33574SMatthias Ringwald // requrest pairing 1569f4b33574SMatthias Ringwald peripheral->wait_for_pairing_complete = 1; 1570f4b33574SMatthias Ringwald peripheral->pending_error_code = packet[4]; 1571f4b33574SMatthias Ringwald sm_request_pairing(peripheral->con_handle); 1572f4b33574SMatthias Ringwald break; 1573f4b33574SMatthias Ringwald #endif 1574f4b33574SMatthias Ringwald 1575f4b33574SMatthias Ringwald // nothing we can do about that 1576f4b33574SMatthias Ringwald case ATT_ERROR_INSUFFICIENT_AUTHORIZATION: 15773deb3ec6SMatthias Ringwald default: 15783deb3ec6SMatthias Ringwald gatt_client_report_error_if_pending(peripheral, packet[4]); 15793deb3ec6SMatthias Ringwald break; 15803deb3ec6SMatthias Ringwald } 15813deb3ec6SMatthias Ringwald break; 15823deb3ec6SMatthias Ringwald 15833deb3ec6SMatthias Ringwald default: 15843deb3ec6SMatthias Ringwald log_info("ATT Handler, unhandled response type 0x%02x", packet[0]); 15853deb3ec6SMatthias Ringwald break; 15863deb3ec6SMatthias Ringwald } 15873deb3ec6SMatthias Ringwald gatt_client_run(); 15883deb3ec6SMatthias Ringwald } 15893deb3ec6SMatthias Ringwald 15907a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE 15913deb3ec6SMatthias Ringwald static void att_signed_write_handle_cmac_result(uint8_t hash[8]){ 1592665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1593665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &gatt_client_connections); 1594665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1595665d90f2SMatthias Ringwald gatt_client_t * peripheral = (gatt_client_t *) btstack_linked_list_iterator_next(&it); 15963deb3ec6SMatthias Ringwald if (peripheral->gatt_client_state == P_W4_CMAC_RESULT){ 15973deb3ec6SMatthias Ringwald // store result 15983deb3ec6SMatthias Ringwald memcpy(peripheral->cmac, hash, 8); 15999c80e4ccSMatthias Ringwald // reverse_64(hash, peripheral->cmac); 16003deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W2_SEND_SIGNED_WRITE; 16013deb3ec6SMatthias Ringwald gatt_client_run(); 16023deb3ec6SMatthias Ringwald return; 16033deb3ec6SMatthias Ringwald } 16043deb3ec6SMatthias Ringwald } 16053deb3ec6SMatthias Ringwald } 16063deb3ec6SMatthias Ringwald 1607711e6c80SMatthias 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){ 16083deb3ec6SMatthias Ringwald gatt_client_t * peripheral = provide_context_for_conn_handle(con_handle); 1609616edd56SMatthias Ringwald if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE; 16103deb3ec6SMatthias Ringwald peripheral->le_device_index = sm_le_device_index(con_handle); 1611616edd56SMatthias Ringwald if (peripheral->le_device_index < 0) return GATT_CLIENT_IN_WRONG_STATE; // device lookup not done / no stored bonding information 16123deb3ec6SMatthias Ringwald 16139c662c9bSMatthias Ringwald peripheral->callback = callback; 16143deb3ec6SMatthias Ringwald peripheral->attribute_handle = handle; 16153deb3ec6SMatthias Ringwald peripheral->attribute_length = message_len; 16163deb3ec6SMatthias Ringwald peripheral->attribute_value = message; 16173deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W4_CMAC_READY; 16183deb3ec6SMatthias Ringwald 16193deb3ec6SMatthias Ringwald gatt_client_run(); 1620616edd56SMatthias Ringwald return 0; 16213deb3ec6SMatthias Ringwald } 16227a766ebfSMatthias Ringwald #endif 16233deb3ec6SMatthias Ringwald 1624711e6c80SMatthias Ringwald uint8_t gatt_client_discover_primary_services(btstack_packet_handler_t callback, hci_con_handle_t con_handle){ 16253deb3ec6SMatthias Ringwald gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle); 1626616edd56SMatthias Ringwald if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED; 1627616edd56SMatthias Ringwald if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE; 16283deb3ec6SMatthias Ringwald 16299c662c9bSMatthias Ringwald peripheral->callback = callback; 16303deb3ec6SMatthias Ringwald peripheral->start_group_handle = 0x0001; 16313deb3ec6SMatthias Ringwald peripheral->end_group_handle = 0xffff; 16323deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W2_SEND_SERVICE_QUERY; 16333deb3ec6SMatthias Ringwald peripheral->uuid16 = 0; 16343deb3ec6SMatthias Ringwald gatt_client_run(); 1635616edd56SMatthias Ringwald return 0; 16363deb3ec6SMatthias Ringwald } 16373deb3ec6SMatthias Ringwald 16383deb3ec6SMatthias Ringwald 1639711e6c80SMatthias Ringwald uint8_t gatt_client_discover_primary_services_by_uuid16(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t uuid16){ 16403deb3ec6SMatthias Ringwald gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle); 16413deb3ec6SMatthias Ringwald 1642616edd56SMatthias Ringwald if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED; 1643616edd56SMatthias Ringwald if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE; 16443deb3ec6SMatthias Ringwald 16459c662c9bSMatthias Ringwald peripheral->callback = callback; 16463deb3ec6SMatthias Ringwald peripheral->start_group_handle = 0x0001; 16473deb3ec6SMatthias Ringwald peripheral->end_group_handle = 0xffff; 16483deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W2_SEND_SERVICE_WITH_UUID_QUERY; 16493deb3ec6SMatthias Ringwald peripheral->uuid16 = uuid16; 1650e1a125dfSMatthias Ringwald uuid_add_bluetooth_prefix((uint8_t*) &(peripheral->uuid128), peripheral->uuid16); 16513deb3ec6SMatthias Ringwald gatt_client_run(); 1652616edd56SMatthias Ringwald return 0; 16533deb3ec6SMatthias Ringwald } 16543deb3ec6SMatthias Ringwald 1655711e6c80SMatthias 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){ 16563deb3ec6SMatthias Ringwald gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle); 16573deb3ec6SMatthias Ringwald 1658616edd56SMatthias Ringwald if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED; 1659616edd56SMatthias Ringwald if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE; 16603deb3ec6SMatthias Ringwald 16619c662c9bSMatthias Ringwald peripheral->callback = callback; 16623deb3ec6SMatthias Ringwald peripheral->start_group_handle = 0x0001; 16633deb3ec6SMatthias Ringwald peripheral->end_group_handle = 0xffff; 16643deb3ec6SMatthias Ringwald peripheral->uuid16 = 0; 16653deb3ec6SMatthias Ringwald memcpy(peripheral->uuid128, uuid128, 16); 16663deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W2_SEND_SERVICE_WITH_UUID_QUERY; 16673deb3ec6SMatthias Ringwald gatt_client_run(); 1668616edd56SMatthias Ringwald return 0; 16693deb3ec6SMatthias Ringwald } 16703deb3ec6SMatthias Ringwald 1671711e6c80SMatthias 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){ 16723deb3ec6SMatthias Ringwald gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle); 16733deb3ec6SMatthias Ringwald 1674616edd56SMatthias Ringwald if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED; 1675616edd56SMatthias Ringwald if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE; 16763deb3ec6SMatthias Ringwald 16779c662c9bSMatthias Ringwald peripheral->callback = callback; 16783deb3ec6SMatthias Ringwald peripheral->start_group_handle = service->start_group_handle; 16793deb3ec6SMatthias Ringwald peripheral->end_group_handle = service->end_group_handle; 16803deb3ec6SMatthias Ringwald peripheral->filter_with_uuid = 0; 16813deb3ec6SMatthias Ringwald peripheral->characteristic_start_handle = 0; 16823deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W2_SEND_ALL_CHARACTERISTICS_OF_SERVICE_QUERY; 16833deb3ec6SMatthias Ringwald gatt_client_run(); 1684616edd56SMatthias Ringwald return 0; 16853deb3ec6SMatthias Ringwald } 16863deb3ec6SMatthias Ringwald 1687711e6c80SMatthias 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){ 16883deb3ec6SMatthias Ringwald gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle); 16893deb3ec6SMatthias Ringwald 1690616edd56SMatthias Ringwald if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED; 1691616edd56SMatthias Ringwald if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE; 16923deb3ec6SMatthias Ringwald 16939c662c9bSMatthias Ringwald peripheral->callback = callback; 16943deb3ec6SMatthias Ringwald peripheral->start_group_handle = service->start_group_handle; 16953deb3ec6SMatthias Ringwald peripheral->end_group_handle = service->end_group_handle; 16963deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W2_SEND_INCLUDED_SERVICE_QUERY; 16973deb3ec6SMatthias Ringwald 16983deb3ec6SMatthias Ringwald gatt_client_run(); 1699616edd56SMatthias Ringwald return 0; 17003deb3ec6SMatthias Ringwald } 17013deb3ec6SMatthias Ringwald 1702711e6c80SMatthias 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){ 17033deb3ec6SMatthias Ringwald gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle); 17043deb3ec6SMatthias Ringwald 1705616edd56SMatthias Ringwald if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED; 1706616edd56SMatthias Ringwald if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE; 17073deb3ec6SMatthias Ringwald 17089c662c9bSMatthias Ringwald peripheral->callback = callback; 17093deb3ec6SMatthias Ringwald peripheral->start_group_handle = start_handle; 17103deb3ec6SMatthias Ringwald peripheral->end_group_handle = end_handle; 17113deb3ec6SMatthias Ringwald peripheral->filter_with_uuid = 1; 17123deb3ec6SMatthias Ringwald peripheral->uuid16 = uuid16; 1713e1a125dfSMatthias Ringwald uuid_add_bluetooth_prefix((uint8_t*) &(peripheral->uuid128), uuid16); 17143deb3ec6SMatthias Ringwald peripheral->characteristic_start_handle = 0; 17153deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W2_SEND_CHARACTERISTIC_WITH_UUID_QUERY; 17163deb3ec6SMatthias Ringwald 17173deb3ec6SMatthias Ringwald gatt_client_run(); 1718616edd56SMatthias Ringwald return 0; 17193deb3ec6SMatthias Ringwald } 17203deb3ec6SMatthias Ringwald 1721711e6c80SMatthias 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){ 17223deb3ec6SMatthias Ringwald gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle); 17233deb3ec6SMatthias Ringwald 1724616edd56SMatthias Ringwald if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED; 1725616edd56SMatthias Ringwald if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE; 17263deb3ec6SMatthias Ringwald 17279c662c9bSMatthias Ringwald peripheral->callback = callback; 17283deb3ec6SMatthias Ringwald peripheral->start_group_handle = start_handle; 17293deb3ec6SMatthias Ringwald peripheral->end_group_handle = end_handle; 17303deb3ec6SMatthias Ringwald peripheral->filter_with_uuid = 1; 17313deb3ec6SMatthias Ringwald peripheral->uuid16 = 0; 17323deb3ec6SMatthias Ringwald memcpy(peripheral->uuid128, uuid128, 16); 17333deb3ec6SMatthias Ringwald peripheral->characteristic_start_handle = 0; 17343deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W2_SEND_CHARACTERISTIC_WITH_UUID_QUERY; 17353deb3ec6SMatthias Ringwald 17363deb3ec6SMatthias Ringwald gatt_client_run(); 1737616edd56SMatthias Ringwald return 0; 17383deb3ec6SMatthias Ringwald } 17393deb3ec6SMatthias Ringwald 17403deb3ec6SMatthias Ringwald 1741d25c33e5SMatthias 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){ 17429c662c9bSMatthias Ringwald return gatt_client_discover_characteristics_for_handle_range_by_uuid16(callback, handle, service->start_group_handle, service->end_group_handle, uuid16); 17433deb3ec6SMatthias Ringwald } 17443deb3ec6SMatthias Ringwald 1745d25c33e5SMatthias 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){ 17469c662c9bSMatthias Ringwald return gatt_client_discover_characteristics_for_handle_range_by_uuid128(callback, handle, service->start_group_handle, service->end_group_handle, uuid128); 17473deb3ec6SMatthias Ringwald } 17483deb3ec6SMatthias Ringwald 1749711e6c80SMatthias Ringwald uint8_t gatt_client_discover_characteristic_descriptors(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_characteristic_t *characteristic){ 17503deb3ec6SMatthias Ringwald gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle); 17513deb3ec6SMatthias Ringwald 1752616edd56SMatthias Ringwald if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED; 1753616edd56SMatthias Ringwald if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE; 17543deb3ec6SMatthias Ringwald 17553deb3ec6SMatthias Ringwald if (characteristic->value_handle == characteristic->end_handle){ 17563deb3ec6SMatthias Ringwald emit_gatt_complete_event(peripheral, 0); 1757616edd56SMatthias Ringwald return 0; 17583deb3ec6SMatthias Ringwald } 17599c662c9bSMatthias Ringwald peripheral->callback = callback; 17603deb3ec6SMatthias Ringwald peripheral->start_group_handle = characteristic->value_handle + 1; 17613deb3ec6SMatthias Ringwald peripheral->end_group_handle = characteristic->end_handle; 17623deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W2_SEND_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY; 17633deb3ec6SMatthias Ringwald 17643deb3ec6SMatthias Ringwald gatt_client_run(); 1765616edd56SMatthias Ringwald return 0; 17663deb3ec6SMatthias Ringwald } 17673deb3ec6SMatthias Ringwald 1768711e6c80SMatthias 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){ 17693deb3ec6SMatthias Ringwald gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle); 17703deb3ec6SMatthias Ringwald 1771616edd56SMatthias Ringwald if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED; 1772616edd56SMatthias Ringwald if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE; 17733deb3ec6SMatthias Ringwald 17749c662c9bSMatthias Ringwald peripheral->callback = callback; 17753deb3ec6SMatthias Ringwald peripheral->attribute_handle = value_handle; 17763deb3ec6SMatthias Ringwald peripheral->attribute_offset = 0; 17773deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W2_SEND_READ_CHARACTERISTIC_VALUE_QUERY; 17783deb3ec6SMatthias Ringwald gatt_client_run(); 1779616edd56SMatthias Ringwald return 0; 17803deb3ec6SMatthias Ringwald } 17813deb3ec6SMatthias Ringwald 1782711e6c80SMatthias 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){ 17833deb3ec6SMatthias Ringwald gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle); 17843deb3ec6SMatthias Ringwald 1785616edd56SMatthias Ringwald if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED; 1786616edd56SMatthias Ringwald if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE; 17873deb3ec6SMatthias Ringwald 17889c662c9bSMatthias Ringwald peripheral->callback = callback; 17893deb3ec6SMatthias Ringwald peripheral->start_group_handle = start_handle; 17903deb3ec6SMatthias Ringwald peripheral->end_group_handle = end_handle; 17913deb3ec6SMatthias Ringwald peripheral->query_start_handle = start_handle; 17923deb3ec6SMatthias Ringwald peripheral->query_end_handle = end_handle; 17933deb3ec6SMatthias Ringwald peripheral->uuid16 = uuid16; 1794e1a125dfSMatthias Ringwald uuid_add_bluetooth_prefix((uint8_t*) &(peripheral->uuid128), uuid16); 17953deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W2_SEND_READ_BY_TYPE_REQUEST; 17963deb3ec6SMatthias Ringwald gatt_client_run(); 1797616edd56SMatthias Ringwald return 0; 17983deb3ec6SMatthias Ringwald } 17993deb3ec6SMatthias Ringwald 1800711e6c80SMatthias 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){ 18013deb3ec6SMatthias Ringwald gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle); 18023deb3ec6SMatthias Ringwald 1803616edd56SMatthias Ringwald if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED; 1804616edd56SMatthias Ringwald if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE; 18053deb3ec6SMatthias Ringwald 18069c662c9bSMatthias Ringwald peripheral->callback = callback; 18073deb3ec6SMatthias Ringwald peripheral->start_group_handle = start_handle; 18083deb3ec6SMatthias Ringwald peripheral->end_group_handle = end_handle; 18093deb3ec6SMatthias Ringwald peripheral->query_start_handle = start_handle; 18103deb3ec6SMatthias Ringwald peripheral->query_end_handle = end_handle; 18113deb3ec6SMatthias Ringwald peripheral->uuid16 = 0; 18123deb3ec6SMatthias Ringwald memcpy(peripheral->uuid128, uuid128, 16); 18133deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W2_SEND_READ_BY_TYPE_REQUEST; 18143deb3ec6SMatthias Ringwald gatt_client_run(); 1815616edd56SMatthias Ringwald return 0; 18163deb3ec6SMatthias Ringwald } 18173deb3ec6SMatthias Ringwald 18183deb3ec6SMatthias Ringwald 1819d25c33e5SMatthias Ringwald uint8_t gatt_client_read_value_of_characteristic(btstack_packet_handler_t callback, uint16_t handle, gatt_client_characteristic_t *characteristic){ 18209c662c9bSMatthias Ringwald return gatt_client_read_value_of_characteristic_using_value_handle(callback, handle, characteristic->value_handle); 18213deb3ec6SMatthias Ringwald } 18223deb3ec6SMatthias Ringwald 1823711e6c80SMatthias 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){ 18243deb3ec6SMatthias Ringwald gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle); 18253deb3ec6SMatthias Ringwald 1826616edd56SMatthias Ringwald if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED; 1827616edd56SMatthias Ringwald if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE; 18283deb3ec6SMatthias Ringwald 18299c662c9bSMatthias Ringwald peripheral->callback = callback; 18303deb3ec6SMatthias Ringwald peripheral->attribute_handle = characteristic_value_handle; 18313deb3ec6SMatthias Ringwald peripheral->attribute_offset = offset; 18323deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W2_SEND_READ_BLOB_QUERY; 18333deb3ec6SMatthias Ringwald gatt_client_run(); 1834616edd56SMatthias Ringwald return 0; 18353deb3ec6SMatthias Ringwald } 18363deb3ec6SMatthias Ringwald 1837711e6c80SMatthias 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){ 18389c662c9bSMatthias Ringwald return gatt_client_read_long_value_of_characteristic_using_value_handle_with_offset(callback, con_handle, characteristic_value_handle, 0); 18393deb3ec6SMatthias Ringwald } 18403deb3ec6SMatthias Ringwald 1841d25c33e5SMatthias Ringwald uint8_t gatt_client_read_long_value_of_characteristic(btstack_packet_handler_t callback, uint16_t handle, gatt_client_characteristic_t *characteristic){ 18429c662c9bSMatthias Ringwald return gatt_client_read_long_value_of_characteristic_using_value_handle(callback, handle, characteristic->value_handle); 18433deb3ec6SMatthias Ringwald } 18443deb3ec6SMatthias Ringwald 1845711e6c80SMatthias 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){ 18463deb3ec6SMatthias Ringwald gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle); 18473deb3ec6SMatthias Ringwald 1848616edd56SMatthias Ringwald if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED; 1849616edd56SMatthias Ringwald if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE; 18503deb3ec6SMatthias Ringwald 18519c662c9bSMatthias Ringwald peripheral->callback = callback; 18523deb3ec6SMatthias Ringwald peripheral->read_multiple_handle_count = num_value_handles; 18533deb3ec6SMatthias Ringwald peripheral->read_multiple_handles = value_handles; 18543deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W2_SEND_READ_MULTIPLE_REQUEST; 18553deb3ec6SMatthias Ringwald gatt_client_run(); 1856616edd56SMatthias Ringwald return 0; 18573deb3ec6SMatthias Ringwald } 18583deb3ec6SMatthias Ringwald 1859de9f8e94SMatthias 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){ 18603deb3ec6SMatthias Ringwald gatt_client_t * peripheral = provide_context_for_conn_handle(con_handle); 18613deb3ec6SMatthias Ringwald 1862616edd56SMatthias Ringwald if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED; 1863616edd56SMatthias Ringwald if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE; 18643deb3ec6SMatthias Ringwald 1865616edd56SMatthias Ringwald if (value_length > peripheral_mtu(peripheral) - 3) return GATT_CLIENT_VALUE_TOO_LONG; 186651764b3bSMatthias Ringwald if (!att_dispatch_client_can_send_now(peripheral->con_handle)) return GATT_CLIENT_BUSY; 18673deb3ec6SMatthias Ringwald 1868fc64f94aSMatthias Ringwald att_write_request(ATT_WRITE_COMMAND, peripheral->con_handle, value_handle, value_length, value); 1869616edd56SMatthias Ringwald return 0; 18703deb3ec6SMatthias Ringwald } 18713deb3ec6SMatthias Ringwald 1872711e6c80SMatthias 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){ 18733deb3ec6SMatthias Ringwald gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle); 18743deb3ec6SMatthias Ringwald 1875616edd56SMatthias Ringwald if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED; 1876616edd56SMatthias Ringwald if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE; 18773deb3ec6SMatthias Ringwald 18789c662c9bSMatthias Ringwald peripheral->callback = callback; 18793deb3ec6SMatthias Ringwald peripheral->attribute_handle = value_handle; 18803deb3ec6SMatthias Ringwald peripheral->attribute_length = value_length; 18813deb3ec6SMatthias Ringwald peripheral->attribute_value = data; 18823deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W2_SEND_WRITE_CHARACTERISTIC_VALUE; 18833deb3ec6SMatthias Ringwald gatt_client_run(); 1884616edd56SMatthias Ringwald return 0; 18853deb3ec6SMatthias Ringwald } 18863deb3ec6SMatthias Ringwald 1887711e6c80SMatthias 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){ 18883deb3ec6SMatthias Ringwald gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle); 18893deb3ec6SMatthias Ringwald 1890616edd56SMatthias Ringwald if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED; 1891616edd56SMatthias Ringwald if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE; 18923deb3ec6SMatthias Ringwald 18939c662c9bSMatthias Ringwald peripheral->callback = callback; 18943deb3ec6SMatthias Ringwald peripheral->attribute_handle = value_handle; 18953deb3ec6SMatthias Ringwald peripheral->attribute_length = value_length; 18963deb3ec6SMatthias Ringwald peripheral->attribute_offset = offset; 18973deb3ec6SMatthias Ringwald peripheral->attribute_value = data; 18983deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W2_PREPARE_WRITE; 18993deb3ec6SMatthias Ringwald gatt_client_run(); 1900616edd56SMatthias Ringwald return 0; 19013deb3ec6SMatthias Ringwald } 19023deb3ec6SMatthias Ringwald 1903711e6c80SMatthias 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){ 19049c662c9bSMatthias Ringwald return gatt_client_write_long_value_of_characteristic_with_offset(callback, con_handle, value_handle, 0, value_length, value); 19053deb3ec6SMatthias Ringwald } 19063deb3ec6SMatthias Ringwald 1907711e6c80SMatthias 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){ 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 = 0; 19173deb3ec6SMatthias Ringwald peripheral->attribute_value = value; 19183deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W2_PREPARE_RELIABLE_WRITE; 19193deb3ec6SMatthias Ringwald gatt_client_run(); 1920616edd56SMatthias Ringwald return 0; 19213deb3ec6SMatthias Ringwald } 19223deb3ec6SMatthias Ringwald 1923711e6c80SMatthias 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){ 19243deb3ec6SMatthias Ringwald gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle); 19253deb3ec6SMatthias Ringwald 1926616edd56SMatthias Ringwald if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED; 1927616edd56SMatthias Ringwald if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE; 19283deb3ec6SMatthias Ringwald 19293deb3ec6SMatthias Ringwald if ( (configuration & GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION) && 19303deb3ec6SMatthias Ringwald (characteristic->properties & ATT_PROPERTY_NOTIFY) == 0) { 1931d8e8f12aSMatthias Ringwald log_info("gatt_client_write_client_characteristic_configuration: GATT_CLIENT_CHARACTERISTIC_NOTIFICATION_NOT_SUPPORTED"); 1932616edd56SMatthias Ringwald return GATT_CLIENT_CHARACTERISTIC_NOTIFICATION_NOT_SUPPORTED; 19333deb3ec6SMatthias Ringwald } else if ( (configuration & GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_INDICATION) && 19343deb3ec6SMatthias Ringwald (characteristic->properties & ATT_PROPERTY_INDICATE) == 0){ 1935d8e8f12aSMatthias Ringwald log_info("gatt_client_write_client_characteristic_configuration: GATT_CLIENT_CHARACTERISTIC_INDICATION_NOT_SUPPORTED"); 1936616edd56SMatthias Ringwald return GATT_CLIENT_CHARACTERISTIC_INDICATION_NOT_SUPPORTED; 19373deb3ec6SMatthias Ringwald } 19383deb3ec6SMatthias Ringwald 19399c662c9bSMatthias Ringwald peripheral->callback = callback; 19403deb3ec6SMatthias Ringwald peripheral->start_group_handle = characteristic->value_handle; 19413deb3ec6SMatthias Ringwald peripheral->end_group_handle = characteristic->end_handle; 1942f8fbdce0SMatthias Ringwald little_endian_store_16(peripheral->client_characteristic_configuration_value, 0, configuration); 19433deb3ec6SMatthias Ringwald 1944abdc9fb5SMatthias Ringwald #ifdef ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERY 1945abdc9fb5SMatthias Ringwald peripheral->gatt_client_state = P_W2_SEND_FIND_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY; 1946abdc9fb5SMatthias Ringwald #else 19473deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W2_SEND_READ_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY; 1948abdc9fb5SMatthias Ringwald #endif 19493deb3ec6SMatthias Ringwald gatt_client_run(); 1950616edd56SMatthias Ringwald return 0; 19513deb3ec6SMatthias Ringwald } 19523deb3ec6SMatthias Ringwald 1953711e6c80SMatthias 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){ 19543deb3ec6SMatthias Ringwald gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle); 19553deb3ec6SMatthias Ringwald 1956616edd56SMatthias Ringwald if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED; 1957616edd56SMatthias Ringwald if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE; 19583deb3ec6SMatthias Ringwald 19599c662c9bSMatthias Ringwald peripheral->callback = callback; 19603deb3ec6SMatthias Ringwald peripheral->attribute_handle = descriptor_handle; 19613deb3ec6SMatthias Ringwald 19623deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W2_SEND_READ_CHARACTERISTIC_DESCRIPTOR_QUERY; 19633deb3ec6SMatthias Ringwald gatt_client_run(); 1964616edd56SMatthias Ringwald return 0; 19653deb3ec6SMatthias Ringwald } 19663deb3ec6SMatthias Ringwald 1967711e6c80SMatthias 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){ 19689c662c9bSMatthias Ringwald return gatt_client_read_characteristic_descriptor_using_descriptor_handle(callback, con_handle, descriptor->handle); 19693deb3ec6SMatthias Ringwald } 19703deb3ec6SMatthias Ringwald 1971711e6c80SMatthias 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){ 19723deb3ec6SMatthias Ringwald gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle); 19733deb3ec6SMatthias Ringwald 1974616edd56SMatthias Ringwald if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED; 1975616edd56SMatthias Ringwald if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE; 19763deb3ec6SMatthias Ringwald 19779c662c9bSMatthias Ringwald peripheral->callback = callback; 19783deb3ec6SMatthias Ringwald peripheral->attribute_handle = descriptor_handle; 19793deb3ec6SMatthias Ringwald peripheral->attribute_offset = offset; 19803deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W2_SEND_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_QUERY; 19813deb3ec6SMatthias Ringwald gatt_client_run(); 1982616edd56SMatthias Ringwald return 0; 19833deb3ec6SMatthias Ringwald } 19843deb3ec6SMatthias Ringwald 1985711e6c80SMatthias 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){ 19869c662c9bSMatthias Ringwald return gatt_client_read_long_characteristic_descriptor_using_descriptor_handle_with_offset(callback, con_handle, descriptor_handle, 0); 19873deb3ec6SMatthias Ringwald } 19883deb3ec6SMatthias Ringwald 1989711e6c80SMatthias 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){ 19909c662c9bSMatthias Ringwald return gatt_client_read_long_characteristic_descriptor_using_descriptor_handle(callback, con_handle, descriptor->handle); 19913deb3ec6SMatthias Ringwald } 19923deb3ec6SMatthias Ringwald 1993711e6c80SMatthias 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){ 19943deb3ec6SMatthias Ringwald gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle); 19953deb3ec6SMatthias Ringwald 1996616edd56SMatthias Ringwald if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED; 1997616edd56SMatthias Ringwald if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE; 19983deb3ec6SMatthias Ringwald 19999c662c9bSMatthias Ringwald peripheral->callback = callback; 20003deb3ec6SMatthias Ringwald peripheral->attribute_handle = descriptor_handle; 20013deb3ec6SMatthias Ringwald peripheral->attribute_length = length; 20023deb3ec6SMatthias Ringwald peripheral->attribute_offset = 0; 20033deb3ec6SMatthias Ringwald peripheral->attribute_value = data; 20043deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W2_SEND_WRITE_CHARACTERISTIC_DESCRIPTOR; 20053deb3ec6SMatthias Ringwald gatt_client_run(); 2006616edd56SMatthias Ringwald return 0; 20073deb3ec6SMatthias Ringwald } 20083deb3ec6SMatthias Ringwald 2009711e6c80SMatthias 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){ 20109c662c9bSMatthias Ringwald return gatt_client_write_characteristic_descriptor_using_descriptor_handle(callback, con_handle, descriptor->handle, length, value); 20113deb3ec6SMatthias Ringwald } 20123deb3ec6SMatthias Ringwald 2013711e6c80SMatthias 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){ 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 = offset; 20233deb3ec6SMatthias Ringwald peripheral->attribute_value = data; 20243deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W2_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR; 20253deb3ec6SMatthias Ringwald gatt_client_run(); 2026616edd56SMatthias Ringwald return 0; 20273deb3ec6SMatthias Ringwald } 20283deb3ec6SMatthias Ringwald 2029711e6c80SMatthias 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){ 20309c662c9bSMatthias Ringwald return gatt_client_write_long_characteristic_descriptor_using_descriptor_handle_with_offset(callback, con_handle, descriptor_handle, 0, length, data ); 20313deb3ec6SMatthias Ringwald } 20323deb3ec6SMatthias Ringwald 2033711e6c80SMatthias 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){ 20349c662c9bSMatthias Ringwald return gatt_client_write_long_characteristic_descriptor_using_descriptor_handle(callback, con_handle, descriptor->handle, length, value); 20353deb3ec6SMatthias Ringwald } 20363deb3ec6SMatthias Ringwald 20373deb3ec6SMatthias Ringwald /** 20383deb3ec6SMatthias Ringwald * @brief -> gatt complete event 20393deb3ec6SMatthias Ringwald */ 2040711e6c80SMatthias 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){ 20413deb3ec6SMatthias Ringwald gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle); 20423deb3ec6SMatthias Ringwald 2043616edd56SMatthias Ringwald if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED; 2044616edd56SMatthias Ringwald if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE; 20453deb3ec6SMatthias Ringwald 20469c662c9bSMatthias Ringwald peripheral->callback = callback; 20473deb3ec6SMatthias Ringwald peripheral->attribute_handle = attribute_handle; 20483deb3ec6SMatthias Ringwald peripheral->attribute_length = length; 20493deb3ec6SMatthias Ringwald peripheral->attribute_offset = offset; 20503deb3ec6SMatthias Ringwald peripheral->attribute_value = data; 20513deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W2_PREPARE_WRITE_SINGLE; 20523deb3ec6SMatthias Ringwald gatt_client_run(); 2053616edd56SMatthias Ringwald return 0; 20543deb3ec6SMatthias Ringwald } 20553deb3ec6SMatthias Ringwald 20563deb3ec6SMatthias Ringwald /** 20573deb3ec6SMatthias Ringwald * @brief -> gatt complete event 20583deb3ec6SMatthias Ringwald */ 2059711e6c80SMatthias Ringwald uint8_t gatt_client_execute_write(btstack_packet_handler_t callback, hci_con_handle_t con_handle){ 20603deb3ec6SMatthias Ringwald gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle); 20613deb3ec6SMatthias Ringwald 2062616edd56SMatthias Ringwald if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED; 2063616edd56SMatthias Ringwald if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE; 20643deb3ec6SMatthias Ringwald 20659c662c9bSMatthias Ringwald peripheral->callback = callback; 20663deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W2_EXECUTE_PREPARED_WRITE; 20673deb3ec6SMatthias Ringwald gatt_client_run(); 2068616edd56SMatthias Ringwald return 0; 20693deb3ec6SMatthias Ringwald } 20703deb3ec6SMatthias Ringwald 20713deb3ec6SMatthias Ringwald /** 20723deb3ec6SMatthias Ringwald * @brief -> gatt complete event 20733deb3ec6SMatthias Ringwald */ 2074711e6c80SMatthias Ringwald uint8_t gatt_client_cancel_write(btstack_packet_handler_t callback, hci_con_handle_t con_handle){ 20753deb3ec6SMatthias Ringwald gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle); 20763deb3ec6SMatthias Ringwald 2077616edd56SMatthias Ringwald if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED; 2078616edd56SMatthias Ringwald if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE; 20793deb3ec6SMatthias Ringwald 20809c662c9bSMatthias Ringwald peripheral->callback = callback; 20813deb3ec6SMatthias Ringwald peripheral->gatt_client_state = P_W2_CANCEL_PREPARED_WRITE; 20823deb3ec6SMatthias Ringwald gatt_client_run(); 2083616edd56SMatthias Ringwald return 0; 20843deb3ec6SMatthias Ringwald } 20853deb3ec6SMatthias Ringwald 2086313e337bSMatthias Ringwald void gatt_client_deserialize_service(const uint8_t *packet, int offset, gatt_client_service_t *service){ 20876ba2ad22SMatthias Ringwald service->start_group_handle = little_endian_read_16(packet, offset); 20886ba2ad22SMatthias Ringwald service->end_group_handle = little_endian_read_16(packet, offset + 2); 20896ba2ad22SMatthias Ringwald reverse_128(&packet[offset + 4], service->uuid128); 20906ba2ad22SMatthias Ringwald if (uuid_has_bluetooth_prefix(service->uuid128)){ 20916ba2ad22SMatthias Ringwald service->uuid16 = big_endian_read_32(service->uuid128, 0); 20926ba2ad22SMatthias Ringwald } 20936ba2ad22SMatthias Ringwald } 20946ba2ad22SMatthias Ringwald 2095313e337bSMatthias Ringwald void gatt_client_deserialize_characteristic(const uint8_t * packet, int offset, gatt_client_characteristic_t * characteristic){ 20966ba2ad22SMatthias Ringwald characteristic->start_handle = little_endian_read_16(packet, offset); 20976ba2ad22SMatthias Ringwald characteristic->value_handle = little_endian_read_16(packet, offset + 2); 20986ba2ad22SMatthias Ringwald characteristic->end_handle = little_endian_read_16(packet, offset + 4); 20996ba2ad22SMatthias Ringwald characteristic->properties = little_endian_read_16(packet, offset + 6); 210086c38559SMatthias Ringwald reverse_128(&packet[offset+8], characteristic->uuid128); 21016ba2ad22SMatthias Ringwald if (uuid_has_bluetooth_prefix(characteristic->uuid128)){ 21026ba2ad22SMatthias Ringwald characteristic->uuid16 = big_endian_read_32(characteristic->uuid128, 0); 21036ba2ad22SMatthias Ringwald } 21046ba2ad22SMatthias Ringwald } 21056ba2ad22SMatthias Ringwald 2106313e337bSMatthias Ringwald void gatt_client_deserialize_characteristic_descriptor(const uint8_t * packet, int offset, gatt_client_characteristic_descriptor_t * descriptor){ 21076ba2ad22SMatthias Ringwald descriptor->handle = little_endian_read_16(packet, offset); 21086ba2ad22SMatthias Ringwald reverse_128(&packet[offset+2], descriptor->uuid128); 2109b4895529SJakob Krantz if (uuid_has_bluetooth_prefix(descriptor->uuid128)){ 2110b4895529SJakob Krantz descriptor->uuid16 = big_endian_read_32(descriptor->uuid128, 0); 2111b4895529SJakob Krantz } 21126ba2ad22SMatthias Ringwald } 21135cf6c434SJakob Krantz 21145cf6c434SJakob Krantz void gatt_client_send_mtu_negotiation(btstack_packet_handler_t callback, hci_con_handle_t con_handle){ 21155cf6c434SJakob Krantz gatt_client_t * context = provide_context_for_conn_handle(con_handle); 21165cf6c434SJakob Krantz if (!context) return; 21175cf6c434SJakob Krantz if (context->mtu_state == MTU_AUTO_EXCHANGE_DISABLED){ 21185cf6c434SJakob Krantz context->callback = callback; 21195cf6c434SJakob Krantz context->mtu_state = SEND_MTU_EXCHANGE; 21205cf6c434SJakob Krantz gatt_client_run(); 21215cf6c434SJakob Krantz } 21225cf6c434SJakob Krantz } 212347181045SMatthias Ringwald 212447181045SMatthias Ringwald uint8_t gatt_client_request_can_write_without_response_event(btstack_packet_handler_t callback, hci_con_handle_t con_handle){ 212547181045SMatthias Ringwald gatt_client_t * context = provide_context_for_conn_handle(con_handle); 212647181045SMatthias Ringwald if (!context) return BTSTACK_MEMORY_ALLOC_FAILED; 212747181045SMatthias Ringwald if (context->write_without_response_callback) return GATT_CLIENT_IN_WRONG_STATE; 212847181045SMatthias Ringwald context->write_without_response_callback = callback; 212947181045SMatthias Ringwald att_dispatch_client_request_can_send_now_event(context->con_handle); 213047181045SMatthias Ringwald return 0; 213147181045SMatthias Ringwald } 2132