xref: /btstack/src/ble/gatt_client.c (revision abdc9fb59b2eadafc2464f74b9b2ad9a5ac43b00)
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 
445*abdc9fb5SMatthias 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 }
449*abdc9fb5SMatthias 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 
968*abdc9fb5SMatthias Ringwald #ifdef ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERY
969*abdc9fb5SMatthias Ringwald         case P_W2_SEND_FIND_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY:
970*abdc9fb5SMatthias Ringwald             // use Find Information
971*abdc9fb5SMatthias Ringwald             peripheral->gatt_client_state = P_W4_FIND_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY_RESULT;
972*abdc9fb5SMatthias Ringwald             send_gatt_characteristic_descriptor_request(peripheral);
973*abdc9fb5SMatthias Ringwald #else
9743deb3ec6SMatthias Ringwald         case P_W2_SEND_READ_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY:
975*abdc9fb5SMatthias 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);
978*abdc9fb5SMatthias 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             gatt_client_report_error_if_pending(peripheral, ATT_ERROR_HCI_DISCONNECT_RECEIVED);
10963deb3ec6SMatthias Ringwald 
1097665d90f2SMatthias Ringwald             btstack_linked_list_remove(&gatt_client_connections, (btstack_linked_item_t *) peripheral);
10983deb3ec6SMatthias Ringwald             btstack_memory_gatt_client_free(peripheral);
10993deb3ec6SMatthias Ringwald             break;
1100f4b33574SMatthias Ringwald 
1101f4b33574SMatthias Ringwald #ifdef ENABLE_GATT_CLIENT_PAIRING
1102f4b33574SMatthias Ringwald         // Pairing complete (with/without bonding=storing of pairing information)
1103f4b33574SMatthias Ringwald         case SM_EVENT_PAIRING_COMPLETE:
1104f4b33574SMatthias Ringwald             con_handle = sm_event_pairing_complete_get_handle(packet);
1105f4b33574SMatthias Ringwald             peripheral = get_gatt_client_context_for_handle(con_handle);
1106f4b33574SMatthias Ringwald             if (!peripheral) break;
1107f4b33574SMatthias Ringwald 
1108f4b33574SMatthias Ringwald             if (peripheral->wait_for_pairing_complete){
1109f4b33574SMatthias Ringwald                 peripheral->wait_for_pairing_complete = 0;
1110f4b33574SMatthias Ringwald                 if (sm_event_pairing_complete_get_status(packet)){
1111f4b33574SMatthias Ringwald                     log_info("pairing failed, report previous error 0x%x", peripheral->pending_error_code);
1112f4b33574SMatthias Ringwald                     gatt_client_handle_transaction_complete(peripheral);
1113f4b33574SMatthias Ringwald                     emit_gatt_complete_event(peripheral, peripheral->pending_error_code);
1114f4b33574SMatthias Ringwald                 } else {
1115f4b33574SMatthias Ringwald                     log_info("pairing success, retry operation");
11163deb3ec6SMatthias Ringwald                 }
1117f4b33574SMatthias Ringwald             }
1118f4b33574SMatthias Ringwald             break;
1119f4b33574SMatthias Ringwald #endif
1120f4b33574SMatthias Ringwald 
11213deb3ec6SMatthias Ringwald         default:
11223deb3ec6SMatthias Ringwald             break;
11233deb3ec6SMatthias Ringwald     }
11243deb3ec6SMatthias Ringwald 
1125361b1363SMatthias Ringwald     gatt_client_run();
11263deb3ec6SMatthias Ringwald }
11273deb3ec6SMatthias Ringwald 
11283deb3ec6SMatthias Ringwald static void gatt_client_att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){
11293deb3ec6SMatthias Ringwald 
11300fbf59a1SMatthias Ringwald     gatt_client_t * peripheral;
11310fbf59a1SMatthias Ringwald 
11320fbf59a1SMatthias Ringwald     if (packet_type == HCI_EVENT_PACKET) {
11330fbf59a1SMatthias Ringwald         switch (packet[0]){
11340fbf59a1SMatthias Ringwald             case L2CAP_EVENT_CAN_SEND_NOW:
113551764b3bSMatthias Ringwald                 gatt_client_run();
11360fbf59a1SMatthias Ringwald                 break;
11370fbf59a1SMatthias Ringwald             // att_server has negotiated the mtu for this connection, cache if context exists
11380fbf59a1SMatthias Ringwald             case ATT_EVENT_MTU_EXCHANGE_COMPLETE:
11390fbf59a1SMatthias Ringwald                 peripheral = get_gatt_client_context_for_handle(handle);
11400fbf59a1SMatthias Ringwald                 if (!peripheral) break;
11410fbf59a1SMatthias Ringwald                 peripheral->mtu = little_endian_read_16(packet, 4);
11420fbf59a1SMatthias Ringwald                 break;
11430fbf59a1SMatthias Ringwald             default:
11440fbf59a1SMatthias Ringwald                 break;
11450fbf59a1SMatthias Ringwald         }
11460fbf59a1SMatthias Ringwald         return;
114751764b3bSMatthias Ringwald     }
114851764b3bSMatthias Ringwald 
11493deb3ec6SMatthias Ringwald     if (packet_type != ATT_DATA_PACKET) return;
11503deb3ec6SMatthias Ringwald 
11513deb3ec6SMatthias Ringwald     // special cases: notifications don't need a context while indications motivate creating one
11523deb3ec6SMatthias Ringwald     switch (packet[0]){
11533deb3ec6SMatthias Ringwald         case ATT_HANDLE_VALUE_NOTIFICATION:
1154f8fbdce0SMatthias Ringwald             report_gatt_notification(handle, little_endian_read_16(packet,1), &packet[3], size-3);
11553deb3ec6SMatthias Ringwald             return;
11563deb3ec6SMatthias Ringwald         case ATT_HANDLE_VALUE_INDICATION:
11573deb3ec6SMatthias Ringwald             peripheral = provide_context_for_conn_handle(handle);
11583deb3ec6SMatthias Ringwald             break;
11593deb3ec6SMatthias Ringwald         default:
11603deb3ec6SMatthias Ringwald             peripheral = get_gatt_client_context_for_handle(handle);
11613deb3ec6SMatthias Ringwald             break;
11623deb3ec6SMatthias Ringwald     }
11633deb3ec6SMatthias Ringwald 
11643deb3ec6SMatthias Ringwald     if (!peripheral) return;
11653deb3ec6SMatthias Ringwald 
11663deb3ec6SMatthias Ringwald     switch (packet[0]){
11673deb3ec6SMatthias Ringwald         case ATT_EXCHANGE_MTU_RESPONSE:
11683deb3ec6SMatthias Ringwald         {
1169f8fbdce0SMatthias Ringwald             uint16_t remote_rx_mtu = little_endian_read_16(packet, 1);
11703deb3ec6SMatthias Ringwald             uint16_t local_rx_mtu = l2cap_max_le_mtu();
11713deb3ec6SMatthias Ringwald             peripheral->mtu = remote_rx_mtu < local_rx_mtu ? remote_rx_mtu : local_rx_mtu;
11723deb3ec6SMatthias Ringwald             peripheral->mtu_state = MTU_EXCHANGED;
11738f37572aSJakob Krantz             emit_gatt_mtu_exchanged_result_event(peripheral, peripheral->mtu);
11743deb3ec6SMatthias Ringwald             break;
11753deb3ec6SMatthias Ringwald         }
11763deb3ec6SMatthias Ringwald         case ATT_READ_BY_GROUP_TYPE_RESPONSE:
11773deb3ec6SMatthias Ringwald             switch(peripheral->gatt_client_state){
11783deb3ec6SMatthias Ringwald                 case P_W4_SERVICE_QUERY_RESULT:
11793deb3ec6SMatthias Ringwald                     report_gatt_services(peripheral, packet, size);
11803deb3ec6SMatthias Ringwald                     trigger_next_service_query(peripheral, get_last_result_handle_from_service_list(packet, size));
11815611a760SMatthias Ringwald                     // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
11823deb3ec6SMatthias Ringwald                     break;
11833deb3ec6SMatthias Ringwald                 default:
11843deb3ec6SMatthias Ringwald                     break;
11853deb3ec6SMatthias Ringwald             }
11863deb3ec6SMatthias Ringwald             break;
11873deb3ec6SMatthias Ringwald         case ATT_HANDLE_VALUE_INDICATION:
1188f8fbdce0SMatthias Ringwald             report_gatt_indication(handle, little_endian_read_16(packet,1), &packet[3], size-3);
11893deb3ec6SMatthias Ringwald             peripheral->send_confirmation = 1;
11903deb3ec6SMatthias Ringwald             break;
11913deb3ec6SMatthias Ringwald 
11923deb3ec6SMatthias Ringwald         case ATT_READ_BY_TYPE_RESPONSE:
11933deb3ec6SMatthias Ringwald             switch (peripheral->gatt_client_state){
11943deb3ec6SMatthias Ringwald                 case P_W4_ALL_CHARACTERISTICS_OF_SERVICE_QUERY_RESULT:
11953deb3ec6SMatthias Ringwald                     report_gatt_characteristics(peripheral, packet, size);
11963deb3ec6SMatthias Ringwald                     trigger_next_characteristic_query(peripheral, get_last_result_handle_from_characteristics_list(packet, size));
11975611a760SMatthias Ringwald                     // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done, or by ATT_ERROR
11983deb3ec6SMatthias Ringwald                     break;
11993deb3ec6SMatthias Ringwald                 case P_W4_CHARACTERISTIC_WITH_UUID_QUERY_RESULT:
12003deb3ec6SMatthias Ringwald                     report_gatt_characteristics(peripheral, packet, size);
12013deb3ec6SMatthias Ringwald                     trigger_next_characteristic_query(peripheral, get_last_result_handle_from_characteristics_list(packet, size));
12025611a760SMatthias Ringwald                     // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done, or by ATT_ERROR
12033deb3ec6SMatthias Ringwald                     break;
12043deb3ec6SMatthias Ringwald                 case P_W4_INCLUDED_SERVICE_QUERY_RESULT:
12053deb3ec6SMatthias Ringwald                 {
12063deb3ec6SMatthias Ringwald                     uint16_t uuid16 = 0;
12073deb3ec6SMatthias Ringwald                     uint16_t pair_size = packet[1];
12083deb3ec6SMatthias Ringwald 
12093deb3ec6SMatthias Ringwald                     if (pair_size < 7){
12103deb3ec6SMatthias Ringwald                         // UUIDs not available, query first included service
1211f8fbdce0SMatthias Ringwald                         peripheral->start_group_handle = little_endian_read_16(packet, 2); // ready for next query
1212f8fbdce0SMatthias Ringwald                         peripheral->query_start_handle = little_endian_read_16(packet, 4);
1213f8fbdce0SMatthias Ringwald                         peripheral->query_end_handle = little_endian_read_16(packet,6);
12143deb3ec6SMatthias Ringwald                         peripheral->gatt_client_state = P_W2_SEND_INCLUDED_SERVICE_WITH_UUID_QUERY;
12153deb3ec6SMatthias Ringwald                         break;
12163deb3ec6SMatthias Ringwald                     }
12173deb3ec6SMatthias Ringwald 
12183deb3ec6SMatthias Ringwald                     uint16_t offset;
12193deb3ec6SMatthias Ringwald                     for (offset = 2; offset < size; offset += pair_size){
1220f8fbdce0SMatthias Ringwald                         uint16_t include_handle = little_endian_read_16(packet, offset);
1221f8fbdce0SMatthias Ringwald                         peripheral->query_start_handle = little_endian_read_16(packet,offset+2);
1222f8fbdce0SMatthias Ringwald                         peripheral->query_end_handle = little_endian_read_16(packet,offset+4);
1223f8fbdce0SMatthias Ringwald                         uuid16 = little_endian_read_16(packet, offset+6);
12243deb3ec6SMatthias Ringwald                         report_gatt_included_service_uuid16(peripheral, include_handle, uuid16);
12253deb3ec6SMatthias Ringwald                     }
12263deb3ec6SMatthias Ringwald 
12273deb3ec6SMatthias Ringwald                     trigger_next_included_service_query(peripheral, get_last_result_handle_from_included_services_list(packet, size));
12285611a760SMatthias Ringwald                     // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
12293deb3ec6SMatthias Ringwald                     break;
12303deb3ec6SMatthias Ringwald                 }
1231*abdc9fb5SMatthias Ringwald #ifndef ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERY
12323deb3ec6SMatthias Ringwald                 case P_W4_READ_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY_RESULT:
1233f8fbdce0SMatthias Ringwald                     peripheral->client_characteristic_configuration_handle = little_endian_read_16(packet, 2);
12343deb3ec6SMatthias Ringwald                     peripheral->gatt_client_state = P_W2_WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION;
12353deb3ec6SMatthias Ringwald                     break;
1236*abdc9fb5SMatthias Ringwald #endif
12373deb3ec6SMatthias Ringwald                 case P_W4_READ_BY_TYPE_RESPONSE: {
12383deb3ec6SMatthias Ringwald                     uint16_t pair_size = packet[1];
12393deb3ec6SMatthias Ringwald                     uint16_t offset;
12403deb3ec6SMatthias Ringwald                     uint16_t last_result_handle = 0;
12413deb3ec6SMatthias Ringwald                     for (offset = 2; offset < size ; offset += pair_size){
1242f8fbdce0SMatthias Ringwald                         uint16_t value_handle = little_endian_read_16(packet, offset);
12433deb3ec6SMatthias Ringwald                         report_gatt_characteristic_value(peripheral, value_handle, &packet[offset+2], pair_size-2);
12443deb3ec6SMatthias Ringwald                         last_result_handle = value_handle;
12453deb3ec6SMatthias Ringwald                     }
12463deb3ec6SMatthias Ringwald                     trigger_next_read_by_type_query(peripheral, last_result_handle);
12473deb3ec6SMatthias Ringwald                     break;
12483deb3ec6SMatthias Ringwald                 }
12493deb3ec6SMatthias Ringwald                 default:
12503deb3ec6SMatthias Ringwald                     break;
12513deb3ec6SMatthias Ringwald             }
12523deb3ec6SMatthias Ringwald             break;
12533deb3ec6SMatthias Ringwald         case ATT_READ_RESPONSE:
12543deb3ec6SMatthias Ringwald             switch (peripheral->gatt_client_state){
12553deb3ec6SMatthias Ringwald                 case P_W4_INCLUDED_SERVICE_UUID_WITH_QUERY_RESULT: {
12563deb3ec6SMatthias Ringwald                     uint8_t uuid128[16];
12579c80e4ccSMatthias Ringwald                     reverse_128(&packet[1], uuid128);
12583deb3ec6SMatthias Ringwald                     report_gatt_included_service_uuid128(peripheral, peripheral->start_group_handle, uuid128);
12593deb3ec6SMatthias Ringwald                     trigger_next_included_service_query(peripheral, peripheral->start_group_handle);
12605611a760SMatthias Ringwald                     // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
12613deb3ec6SMatthias Ringwald                     break;
12623deb3ec6SMatthias Ringwald                 }
12633deb3ec6SMatthias Ringwald                 case P_W4_READ_CHARACTERISTIC_VALUE_RESULT:
12643deb3ec6SMatthias Ringwald                     gatt_client_handle_transaction_complete(peripheral);
12653deb3ec6SMatthias Ringwald                     report_gatt_characteristic_value(peripheral, peripheral->attribute_handle, &packet[1], size-1);
12663deb3ec6SMatthias Ringwald                     emit_gatt_complete_event(peripheral, 0);
12673deb3ec6SMatthias Ringwald                     break;
12683deb3ec6SMatthias Ringwald 
12693deb3ec6SMatthias Ringwald                 case P_W4_READ_CHARACTERISTIC_DESCRIPTOR_RESULT:{
12703deb3ec6SMatthias Ringwald                     gatt_client_handle_transaction_complete(peripheral);
12713deb3ec6SMatthias Ringwald                     report_gatt_characteristic_descriptor(peripheral, peripheral->attribute_handle, &packet[1], size-1, 0);
12723deb3ec6SMatthias Ringwald                     emit_gatt_complete_event(peripheral, 0);
12733deb3ec6SMatthias Ringwald                     break;
12743deb3ec6SMatthias Ringwald                 }
12753deb3ec6SMatthias Ringwald                 default:
12763deb3ec6SMatthias Ringwald                     break;
12773deb3ec6SMatthias Ringwald             }
12783deb3ec6SMatthias Ringwald             break;
12793deb3ec6SMatthias Ringwald 
12803deb3ec6SMatthias Ringwald         case ATT_FIND_BY_TYPE_VALUE_RESPONSE:
12813deb3ec6SMatthias Ringwald         {
12823deb3ec6SMatthias Ringwald             uint8_t pair_size = 4;
12833deb3ec6SMatthias Ringwald             int i;
12843deb3ec6SMatthias Ringwald             uint16_t start_group_handle;
12855611a760SMatthias Ringwald             uint16_t   end_group_handle= 0xffff; // asserts GATT_EVENT_QUERY_COMPLETE is emitted if no results
12863deb3ec6SMatthias Ringwald             for (i = 1; i<size; i+=pair_size){
1287f8fbdce0SMatthias Ringwald                 start_group_handle = little_endian_read_16(packet,i);
1288f8fbdce0SMatthias Ringwald                 end_group_handle = little_endian_read_16(packet,i+2);
12893deb3ec6SMatthias Ringwald                 emit_gatt_service_query_result_event(peripheral, start_group_handle, end_group_handle, peripheral->uuid128);
12903deb3ec6SMatthias Ringwald             }
12913deb3ec6SMatthias Ringwald             trigger_next_service_by_uuid_query(peripheral, end_group_handle);
12925611a760SMatthias Ringwald             // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
12933deb3ec6SMatthias Ringwald             break;
12943deb3ec6SMatthias Ringwald         }
12953deb3ec6SMatthias Ringwald         case ATT_FIND_INFORMATION_REPLY:
12963deb3ec6SMatthias Ringwald         {
12973deb3ec6SMatthias Ringwald             uint8_t pair_size = 4;
12983deb3ec6SMatthias Ringwald             if (packet[1] == 2){
12993deb3ec6SMatthias Ringwald                 pair_size = 18;
13003deb3ec6SMatthias Ringwald             }
1301f8fbdce0SMatthias Ringwald             uint16_t last_descriptor_handle = little_endian_read_16(packet, size - pair_size);
1302*abdc9fb5SMatthias Ringwald 
1303*abdc9fb5SMatthias Ringwald #ifdef ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERY
1304*abdc9fb5SMatthias Ringwald             log_info("ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERY, state %x", peripheral->gatt_client_state);
1305*abdc9fb5SMatthias Ringwald             if (peripheral->gatt_client_state == P_W4_FIND_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY_RESULT){
1306*abdc9fb5SMatthias Ringwald                 // iterate over descriptors looking for CCC
1307*abdc9fb5SMatthias Ringwald                 if (pair_size == 4){
1308*abdc9fb5SMatthias Ringwald                     int offset = 2;
1309*abdc9fb5SMatthias Ringwald                     while (offset < size){
1310*abdc9fb5SMatthias Ringwald                         uint16_t uuid16 = little_endian_read_16(packet, offset + 2);
1311*abdc9fb5SMatthias Ringwald                         if (uuid16 == GATT_CLIENT_CHARACTERISTICS_CONFIGURATION){
1312*abdc9fb5SMatthias Ringwald                             peripheral->client_characteristic_configuration_handle = little_endian_read_16(packet, offset);
1313*abdc9fb5SMatthias Ringwald                             peripheral->gatt_client_state = P_W2_WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION;
1314*abdc9fb5SMatthias Ringwald                             log_info("CCC found %x", peripheral->client_characteristic_configuration_handle);
1315*abdc9fb5SMatthias Ringwald                             break;
1316*abdc9fb5SMatthias Ringwald                         }
1317*abdc9fb5SMatthias Ringwald                         offset += pair_size;
1318*abdc9fb5SMatthias Ringwald                     }
1319*abdc9fb5SMatthias Ringwald                 }
1320*abdc9fb5SMatthias Ringwald                 if (is_query_done(peripheral, last_descriptor_handle)){
1321*abdc9fb5SMatthias Ringwald 
1322*abdc9fb5SMatthias Ringwald                 } else {
1323*abdc9fb5SMatthias Ringwald                     // next
1324*abdc9fb5SMatthias Ringwald                     peripheral->start_group_handle = last_descriptor_handle + 1;
1325*abdc9fb5SMatthias Ringwald                     peripheral->gatt_client_state = P_W2_SEND_FIND_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY;
1326*abdc9fb5SMatthias Ringwald                 }
1327*abdc9fb5SMatthias Ringwald                 break;
1328*abdc9fb5SMatthias Ringwald             }
1329*abdc9fb5SMatthias Ringwald #endif
13303deb3ec6SMatthias Ringwald             report_gatt_all_characteristic_descriptors(peripheral, &packet[2], size-2, pair_size);
13313deb3ec6SMatthias Ringwald             trigger_next_characteristic_descriptor_query(peripheral, last_descriptor_handle);
13325611a760SMatthias Ringwald             // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
13333deb3ec6SMatthias Ringwald             break;
13343deb3ec6SMatthias Ringwald         }
13353deb3ec6SMatthias Ringwald 
13363deb3ec6SMatthias Ringwald         case ATT_WRITE_RESPONSE:
13373deb3ec6SMatthias Ringwald             switch (peripheral->gatt_client_state){
13383deb3ec6SMatthias Ringwald                 case P_W4_WRITE_CHARACTERISTIC_VALUE_RESULT:
13393deb3ec6SMatthias Ringwald                     gatt_client_handle_transaction_complete(peripheral);
13403deb3ec6SMatthias Ringwald                     emit_gatt_complete_event(peripheral, 0);
13413deb3ec6SMatthias Ringwald                     break;
13423deb3ec6SMatthias Ringwald                 case P_W4_CLIENT_CHARACTERISTIC_CONFIGURATION_RESULT:
13433deb3ec6SMatthias Ringwald                     gatt_client_handle_transaction_complete(peripheral);
13443deb3ec6SMatthias Ringwald                     emit_gatt_complete_event(peripheral, 0);
13453deb3ec6SMatthias Ringwald                     break;
13463deb3ec6SMatthias Ringwald                 case P_W4_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT:
13473deb3ec6SMatthias Ringwald                     gatt_client_handle_transaction_complete(peripheral);
13483deb3ec6SMatthias Ringwald                     emit_gatt_complete_event(peripheral, 0);
13493deb3ec6SMatthias Ringwald                     break;
13503deb3ec6SMatthias Ringwald                 default:
13513deb3ec6SMatthias Ringwald                     break;
13523deb3ec6SMatthias Ringwald             }
13533deb3ec6SMatthias Ringwald             break;
13543deb3ec6SMatthias Ringwald 
13553deb3ec6SMatthias Ringwald         case ATT_READ_BLOB_RESPONSE:{
13563deb3ec6SMatthias Ringwald             uint16_t received_blob_length = size-1;
13573deb3ec6SMatthias Ringwald             switch(peripheral->gatt_client_state){
13583deb3ec6SMatthias Ringwald                 case P_W4_READ_BLOB_RESULT:
13593deb3ec6SMatthias Ringwald                     report_gatt_long_characteristic_value_blob(peripheral, peripheral->attribute_handle, &packet[1], received_blob_length, peripheral->attribute_offset);
13603deb3ec6SMatthias Ringwald                     trigger_next_blob_query(peripheral, P_W2_SEND_READ_BLOB_QUERY, received_blob_length);
13615611a760SMatthias Ringwald                     // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
13623deb3ec6SMatthias Ringwald                     break;
13633deb3ec6SMatthias Ringwald                 case P_W4_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_RESULT:
13643deb3ec6SMatthias Ringwald                     report_gatt_long_characteristic_descriptor(peripheral, peripheral->attribute_handle,
13653deb3ec6SMatthias Ringwald                                                           &packet[1], received_blob_length,
13663deb3ec6SMatthias Ringwald                                                           peripheral->attribute_offset);
13673deb3ec6SMatthias Ringwald                     trigger_next_blob_query(peripheral, P_W2_SEND_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_QUERY, received_blob_length);
13685611a760SMatthias Ringwald                     // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
13693deb3ec6SMatthias Ringwald                     break;
13703deb3ec6SMatthias Ringwald                 default:
13713deb3ec6SMatthias Ringwald                     break;
13723deb3ec6SMatthias Ringwald             }
13733deb3ec6SMatthias Ringwald             break;
13743deb3ec6SMatthias Ringwald         }
13753deb3ec6SMatthias Ringwald         case ATT_PREPARE_WRITE_RESPONSE:
13763deb3ec6SMatthias Ringwald             switch (peripheral->gatt_client_state){
13773deb3ec6SMatthias Ringwald                 case P_W4_PREPARE_WRITE_SINGLE_RESULT:
13783deb3ec6SMatthias Ringwald                     gatt_client_handle_transaction_complete(peripheral);
13793deb3ec6SMatthias Ringwald                     if (is_value_valid(peripheral, packet, size)){
13803deb3ec6SMatthias Ringwald                         emit_gatt_complete_event(peripheral, 0);
13813deb3ec6SMatthias Ringwald                     } else {
13823deb3ec6SMatthias Ringwald                         emit_gatt_complete_event(peripheral, ATT_ERROR_DATA_MISMATCH);
13833deb3ec6SMatthias Ringwald                     }
13843deb3ec6SMatthias Ringwald                     break;
13853deb3ec6SMatthias Ringwald 
13863deb3ec6SMatthias Ringwald                 case P_W4_PREPARE_WRITE_RESULT:{
1387f8fbdce0SMatthias Ringwald                     peripheral->attribute_offset = little_endian_read_16(packet, 3);
13883deb3ec6SMatthias Ringwald                     trigger_next_prepare_write_query(peripheral, P_W2_PREPARE_WRITE, P_W2_EXECUTE_PREPARED_WRITE);
13895611a760SMatthias Ringwald                     // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
13903deb3ec6SMatthias Ringwald                     break;
13913deb3ec6SMatthias Ringwald                 }
13923deb3ec6SMatthias Ringwald                 case P_W4_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT:{
1393f8fbdce0SMatthias Ringwald                     peripheral->attribute_offset = little_endian_read_16(packet, 3);
13943deb3ec6SMatthias Ringwald                     trigger_next_prepare_write_query(peripheral, P_W2_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR, P_W2_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR);
13955611a760SMatthias Ringwald                     // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
13963deb3ec6SMatthias Ringwald                     break;
13973deb3ec6SMatthias Ringwald                 }
13983deb3ec6SMatthias Ringwald                 case P_W4_PREPARE_RELIABLE_WRITE_RESULT:{
13993deb3ec6SMatthias Ringwald                     if (is_value_valid(peripheral, packet, size)){
1400f8fbdce0SMatthias Ringwald                         peripheral->attribute_offset = little_endian_read_16(packet, 3);
14013deb3ec6SMatthias Ringwald                         trigger_next_prepare_write_query(peripheral, P_W2_PREPARE_RELIABLE_WRITE, P_W2_EXECUTE_PREPARED_WRITE);
14025611a760SMatthias Ringwald                         // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
14033deb3ec6SMatthias Ringwald                         break;
14043deb3ec6SMatthias Ringwald                     }
14053deb3ec6SMatthias Ringwald                     peripheral->gatt_client_state = P_W2_CANCEL_PREPARED_WRITE_DATA_MISMATCH;
14063deb3ec6SMatthias Ringwald                     break;
14073deb3ec6SMatthias Ringwald                 }
14083deb3ec6SMatthias Ringwald                 default:
14093deb3ec6SMatthias Ringwald                     break;
14103deb3ec6SMatthias Ringwald             }
14113deb3ec6SMatthias Ringwald             break;
14123deb3ec6SMatthias Ringwald 
14133deb3ec6SMatthias Ringwald         case ATT_EXECUTE_WRITE_RESPONSE:
14143deb3ec6SMatthias Ringwald             switch (peripheral->gatt_client_state){
14153deb3ec6SMatthias Ringwald                 case P_W4_EXECUTE_PREPARED_WRITE_RESULT:
14163deb3ec6SMatthias Ringwald                     gatt_client_handle_transaction_complete(peripheral);
14173deb3ec6SMatthias Ringwald                     emit_gatt_complete_event(peripheral, 0);
14183deb3ec6SMatthias Ringwald                     break;
14193deb3ec6SMatthias Ringwald                 case P_W4_CANCEL_PREPARED_WRITE_RESULT:
14203deb3ec6SMatthias Ringwald                     gatt_client_handle_transaction_complete(peripheral);
14213deb3ec6SMatthias Ringwald                     emit_gatt_complete_event(peripheral, 0);
14223deb3ec6SMatthias Ringwald                     break;
14233deb3ec6SMatthias Ringwald                 case P_W4_CANCEL_PREPARED_WRITE_DATA_MISMATCH_RESULT:
14243deb3ec6SMatthias Ringwald                     gatt_client_handle_transaction_complete(peripheral);
14253deb3ec6SMatthias Ringwald                     emit_gatt_complete_event(peripheral, ATT_ERROR_DATA_MISMATCH);
14263deb3ec6SMatthias Ringwald                     break;
14273deb3ec6SMatthias Ringwald                 case P_W4_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT:
14283deb3ec6SMatthias Ringwald                     gatt_client_handle_transaction_complete(peripheral);
14293deb3ec6SMatthias Ringwald                     emit_gatt_complete_event(peripheral, 0);
14303deb3ec6SMatthias Ringwald                     break;
14313deb3ec6SMatthias Ringwald                 default:
14323deb3ec6SMatthias Ringwald                     break;
14333deb3ec6SMatthias Ringwald 
14343deb3ec6SMatthias Ringwald             }
14353deb3ec6SMatthias Ringwald             break;
14363deb3ec6SMatthias Ringwald 
14373deb3ec6SMatthias Ringwald         case ATT_READ_MULTIPLE_RESPONSE:
14383deb3ec6SMatthias Ringwald             switch(peripheral->gatt_client_state){
14393deb3ec6SMatthias Ringwald                 case P_W4_READ_MULTIPLE_RESPONSE:
14403deb3ec6SMatthias Ringwald                     report_gatt_characteristic_value(peripheral, 0, &packet[1], size-1);
14413deb3ec6SMatthias Ringwald                     gatt_client_handle_transaction_complete(peripheral);
14423deb3ec6SMatthias Ringwald                     emit_gatt_complete_event(peripheral, 0);
14433deb3ec6SMatthias Ringwald                     break;
14443deb3ec6SMatthias Ringwald                 default:
14453deb3ec6SMatthias Ringwald                     break;
14463deb3ec6SMatthias Ringwald             }
14473deb3ec6SMatthias Ringwald             break;
14483deb3ec6SMatthias Ringwald 
14493deb3ec6SMatthias Ringwald         case ATT_ERROR_RESPONSE:
14503deb3ec6SMatthias Ringwald 
14513deb3ec6SMatthias Ringwald             switch (packet[4]){
14523deb3ec6SMatthias Ringwald                 case ATT_ERROR_ATTRIBUTE_NOT_FOUND: {
14533deb3ec6SMatthias Ringwald                     switch(peripheral->gatt_client_state){
14543deb3ec6SMatthias Ringwald                         case P_W4_SERVICE_QUERY_RESULT:
14553deb3ec6SMatthias Ringwald                         case P_W4_SERVICE_WITH_UUID_RESULT:
14563deb3ec6SMatthias Ringwald                         case P_W4_INCLUDED_SERVICE_QUERY_RESULT:
14573deb3ec6SMatthias Ringwald                         case P_W4_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY_RESULT:
14583deb3ec6SMatthias Ringwald                             gatt_client_handle_transaction_complete(peripheral);
14593deb3ec6SMatthias Ringwald                             emit_gatt_complete_event(peripheral, 0);
14603deb3ec6SMatthias Ringwald                             break;
14613deb3ec6SMatthias Ringwald                         case P_W4_ALL_CHARACTERISTICS_OF_SERVICE_QUERY_RESULT:
14623deb3ec6SMatthias Ringwald                         case P_W4_CHARACTERISTIC_WITH_UUID_QUERY_RESULT:
14633deb3ec6SMatthias Ringwald                             characteristic_end_found(peripheral, peripheral->end_group_handle);
14643deb3ec6SMatthias Ringwald                             gatt_client_handle_transaction_complete(peripheral);
14653deb3ec6SMatthias Ringwald                             emit_gatt_complete_event(peripheral, 0);
14663deb3ec6SMatthias Ringwald                             break;
14673deb3ec6SMatthias Ringwald                         case P_W4_READ_BY_TYPE_RESPONSE:
14683deb3ec6SMatthias Ringwald                             gatt_client_handle_transaction_complete(peripheral);
14693deb3ec6SMatthias Ringwald                             if (peripheral->start_group_handle == peripheral->query_start_handle){
14703deb3ec6SMatthias Ringwald                                 emit_gatt_complete_event(peripheral, ATT_ERROR_ATTRIBUTE_NOT_FOUND);
14713deb3ec6SMatthias Ringwald                             } else {
14723deb3ec6SMatthias Ringwald                                 emit_gatt_complete_event(peripheral, 0);
14733deb3ec6SMatthias Ringwald                             }
14743deb3ec6SMatthias Ringwald                             break;
14753deb3ec6SMatthias Ringwald                         default:
14763deb3ec6SMatthias Ringwald                             gatt_client_report_error_if_pending(peripheral, packet[4]);
14773deb3ec6SMatthias Ringwald                             break;
14783deb3ec6SMatthias Ringwald                     }
14793deb3ec6SMatthias Ringwald                     break;
14803deb3ec6SMatthias Ringwald                 }
1481f4b33574SMatthias Ringwald 
1482f4b33574SMatthias Ringwald #ifdef ENABLE_GATT_CLIENT_PAIRING
1483f4b33574SMatthias Ringwald 
1484f4b33574SMatthias Ringwald                 case ATT_ERROR_INSUFFICIENT_AUTHENTICATION:
1485f4b33574SMatthias Ringwald                 case ATT_ERROR_INSUFFICIENT_ENCRYPTION_KEY_SIZE:
1486f4b33574SMatthias Ringwald                 case ATT_ERROR_INSUFFICIENT_ENCRYPTION:
1487f4b33574SMatthias Ringwald                     // security too low
1488f4b33574SMatthias Ringwald                     if (peripheral->security_counter > 0) {
1489f4b33574SMatthias Ringwald                         gatt_client_report_error_if_pending(peripheral, packet[4]);
1490f4b33574SMatthias Ringwald                         break;
1491f4b33574SMatthias Ringwald                     }
1492f4b33574SMatthias Ringwald                     // start security
1493f4b33574SMatthias Ringwald                     peripheral->security_counter++;
1494f4b33574SMatthias Ringwald 
1495f4b33574SMatthias Ringwald                     // setup action
1496f4b33574SMatthias Ringwald                     int retry = 1;
1497f4b33574SMatthias Ringwald                     switch (peripheral->gatt_client_state){
1498f4b33574SMatthias Ringwald                         case P_W4_READ_CHARACTERISTIC_VALUE_RESULT:
1499f4b33574SMatthias Ringwald                             peripheral->gatt_client_state = P_W2_SEND_READ_CHARACTERISTIC_VALUE_QUERY ;
1500f4b33574SMatthias Ringwald                             break;
1501f4b33574SMatthias Ringwald                         case P_W4_READ_BLOB_RESULT:
1502f4b33574SMatthias Ringwald                             peripheral->gatt_client_state = P_W2_SEND_READ_BLOB_QUERY;
1503f4b33574SMatthias Ringwald                             break;
1504f4b33574SMatthias Ringwald                         case P_W4_READ_BY_TYPE_RESPONSE:
1505f4b33574SMatthias Ringwald                             peripheral->gatt_client_state = P_W2_SEND_READ_BY_TYPE_REQUEST;
1506f4b33574SMatthias Ringwald                             break;
1507f4b33574SMatthias Ringwald                         case P_W4_READ_MULTIPLE_RESPONSE:
1508f4b33574SMatthias Ringwald                             peripheral->gatt_client_state = P_W2_SEND_READ_MULTIPLE_REQUEST;
1509f4b33574SMatthias Ringwald                             break;
1510f4b33574SMatthias Ringwald                         case P_W4_WRITE_CHARACTERISTIC_VALUE_RESULT:
1511f4b33574SMatthias Ringwald                             peripheral->gatt_client_state = P_W2_SEND_WRITE_CHARACTERISTIC_VALUE;
1512f4b33574SMatthias Ringwald                             break;
1513f4b33574SMatthias Ringwald                         case P_W4_PREPARE_WRITE_RESULT:
1514f4b33574SMatthias Ringwald                             peripheral->gatt_client_state = P_W2_PREPARE_WRITE;
1515f4b33574SMatthias Ringwald                             break;
1516f4b33574SMatthias Ringwald                         case P_W4_PREPARE_WRITE_SINGLE_RESULT:
1517f4b33574SMatthias Ringwald                             peripheral->gatt_client_state = P_W2_PREPARE_WRITE_SINGLE;
1518f4b33574SMatthias Ringwald                             break;
1519f4b33574SMatthias Ringwald                         case P_W4_PREPARE_RELIABLE_WRITE_RESULT:
1520f4b33574SMatthias Ringwald                             peripheral->gatt_client_state = P_W2_PREPARE_RELIABLE_WRITE;
1521f4b33574SMatthias Ringwald                             break;
1522f4b33574SMatthias Ringwald                         case P_W4_EXECUTE_PREPARED_WRITE_RESULT:
1523f4b33574SMatthias Ringwald                             peripheral->gatt_client_state = P_W2_EXECUTE_PREPARED_WRITE;
1524f4b33574SMatthias Ringwald                             break;
1525f4b33574SMatthias Ringwald                         case P_W4_CANCEL_PREPARED_WRITE_RESULT:
1526f4b33574SMatthias Ringwald                             peripheral->gatt_client_state = P_W2_CANCEL_PREPARED_WRITE;
1527f4b33574SMatthias Ringwald                             break;
1528f4b33574SMatthias Ringwald                         case P_W4_CANCEL_PREPARED_WRITE_DATA_MISMATCH_RESULT:
1529f4b33574SMatthias Ringwald                             peripheral->gatt_client_state = P_W2_CANCEL_PREPARED_WRITE_DATA_MISMATCH;
1530f4b33574SMatthias Ringwald                             break;
1531f4b33574SMatthias Ringwald                         case P_W4_READ_CHARACTERISTIC_DESCRIPTOR_RESULT:
1532f4b33574SMatthias Ringwald                             peripheral->gatt_client_state = P_W2_SEND_READ_CHARACTERISTIC_DESCRIPTOR_QUERY;
1533f4b33574SMatthias Ringwald                             break;
1534f4b33574SMatthias Ringwald                         case P_W4_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_RESULT:
1535f4b33574SMatthias Ringwald                             peripheral->gatt_client_state = P_W2_SEND_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_QUERY;
1536f4b33574SMatthias Ringwald                             break;
1537f4b33574SMatthias Ringwald                         case P_W4_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT:
1538f4b33574SMatthias Ringwald                             peripheral->gatt_client_state = P_W2_SEND_WRITE_CHARACTERISTIC_DESCRIPTOR;
1539f4b33574SMatthias Ringwald                             break;
1540f4b33574SMatthias Ringwald                         case P_W4_CLIENT_CHARACTERISTIC_CONFIGURATION_RESULT:
1541f4b33574SMatthias Ringwald                             peripheral->gatt_client_state = P_W2_WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION;
1542f4b33574SMatthias Ringwald                             break;
1543f4b33574SMatthias Ringwald                         case P_W4_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT:
1544f4b33574SMatthias Ringwald                             peripheral->gatt_client_state = P_W2_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR;
1545f4b33574SMatthias Ringwald                             break;
1546f4b33574SMatthias Ringwald                         case P_W4_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT:
1547f4b33574SMatthias Ringwald                             peripheral->gatt_client_state = P_W2_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR;
1548f4b33574SMatthias Ringwald                             break;
1549f4b33574SMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
1550f4b33574SMatthias Ringwald                         case P_W4_SEND_SINGED_WRITE_DONE:
1551f4b33574SMatthias Ringwald                             peripheral->gatt_client_state = P_W2_SEND_SIGNED_WRITE;
1552f4b33574SMatthias Ringwald                             break;
1553f4b33574SMatthias Ringwald #endif
1554f4b33574SMatthias Ringwald                         default:
1555f4b33574SMatthias Ringwald                             log_info("retry not supported for state %x", peripheral->gatt_client_state);
1556f4b33574SMatthias Ringwald                             retry = 0;
1557f4b33574SMatthias Ringwald                             break;
1558f4b33574SMatthias Ringwald                     }
1559f4b33574SMatthias Ringwald 
1560f4b33574SMatthias Ringwald                     if (!retry) {
1561f4b33574SMatthias Ringwald                         gatt_client_report_error_if_pending(peripheral, packet[4]);
1562f4b33574SMatthias Ringwald                         break;
1563f4b33574SMatthias Ringwald                     }
1564f4b33574SMatthias Ringwald 
1565f4b33574SMatthias Ringwald                     log_info("security error, start pairing");
1566f4b33574SMatthias Ringwald 
1567f4b33574SMatthias Ringwald                     // requrest pairing
1568f4b33574SMatthias Ringwald                     peripheral->wait_for_pairing_complete = 1;
1569f4b33574SMatthias Ringwald                     peripheral->pending_error_code = packet[4];
1570f4b33574SMatthias Ringwald                     sm_request_pairing(peripheral->con_handle);
1571f4b33574SMatthias Ringwald                     break;
1572f4b33574SMatthias Ringwald #endif
1573f4b33574SMatthias Ringwald 
1574f4b33574SMatthias Ringwald                 // nothing we can do about that
1575f4b33574SMatthias Ringwald                 case ATT_ERROR_INSUFFICIENT_AUTHORIZATION:
15763deb3ec6SMatthias Ringwald                 default:
15773deb3ec6SMatthias Ringwald                     gatt_client_report_error_if_pending(peripheral, packet[4]);
15783deb3ec6SMatthias Ringwald                     break;
15793deb3ec6SMatthias Ringwald             }
15803deb3ec6SMatthias Ringwald             break;
15813deb3ec6SMatthias Ringwald 
15823deb3ec6SMatthias Ringwald         default:
15833deb3ec6SMatthias Ringwald             log_info("ATT Handler, unhandled response type 0x%02x", packet[0]);
15843deb3ec6SMatthias Ringwald             break;
15853deb3ec6SMatthias Ringwald     }
15863deb3ec6SMatthias Ringwald     gatt_client_run();
15873deb3ec6SMatthias Ringwald }
15883deb3ec6SMatthias Ringwald 
15897a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
15903deb3ec6SMatthias Ringwald static void att_signed_write_handle_cmac_result(uint8_t hash[8]){
1591665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1592665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &gatt_client_connections);
1593665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1594665d90f2SMatthias Ringwald         gatt_client_t * peripheral = (gatt_client_t *) btstack_linked_list_iterator_next(&it);
15953deb3ec6SMatthias Ringwald         if (peripheral->gatt_client_state == P_W4_CMAC_RESULT){
15963deb3ec6SMatthias Ringwald             // store result
15973deb3ec6SMatthias Ringwald             memcpy(peripheral->cmac, hash, 8);
15989c80e4ccSMatthias Ringwald             // reverse_64(hash, peripheral->cmac);
15993deb3ec6SMatthias Ringwald             peripheral->gatt_client_state = P_W2_SEND_SIGNED_WRITE;
16003deb3ec6SMatthias Ringwald             gatt_client_run();
16013deb3ec6SMatthias Ringwald             return;
16023deb3ec6SMatthias Ringwald         }
16033deb3ec6SMatthias Ringwald     }
16043deb3ec6SMatthias Ringwald }
16053deb3ec6SMatthias Ringwald 
1606711e6c80SMatthias 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){
16073deb3ec6SMatthias Ringwald     gatt_client_t * peripheral = provide_context_for_conn_handle(con_handle);
1608616edd56SMatthias Ringwald     if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE;
16093deb3ec6SMatthias Ringwald     peripheral->le_device_index = sm_le_device_index(con_handle);
1610616edd56SMatthias Ringwald     if (peripheral->le_device_index < 0) return GATT_CLIENT_IN_WRONG_STATE; // device lookup not done / no stored bonding information
16113deb3ec6SMatthias Ringwald 
16129c662c9bSMatthias Ringwald     peripheral->callback = callback;
16133deb3ec6SMatthias Ringwald     peripheral->attribute_handle = handle;
16143deb3ec6SMatthias Ringwald     peripheral->attribute_length = message_len;
16153deb3ec6SMatthias Ringwald     peripheral->attribute_value = message;
16163deb3ec6SMatthias Ringwald     peripheral->gatt_client_state = P_W4_CMAC_READY;
16173deb3ec6SMatthias Ringwald 
16183deb3ec6SMatthias Ringwald     gatt_client_run();
1619616edd56SMatthias Ringwald     return 0;
16203deb3ec6SMatthias Ringwald }
16217a766ebfSMatthias Ringwald #endif
16223deb3ec6SMatthias Ringwald 
1623711e6c80SMatthias Ringwald uint8_t gatt_client_discover_primary_services(btstack_packet_handler_t callback, hci_con_handle_t con_handle){
16243deb3ec6SMatthias Ringwald     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
1625616edd56SMatthias Ringwald     if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED;
1626616edd56SMatthias Ringwald     if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE;
16273deb3ec6SMatthias Ringwald 
16289c662c9bSMatthias Ringwald     peripheral->callback = callback;
16293deb3ec6SMatthias Ringwald     peripheral->start_group_handle = 0x0001;
16303deb3ec6SMatthias Ringwald     peripheral->end_group_handle   = 0xffff;
16313deb3ec6SMatthias Ringwald     peripheral->gatt_client_state = P_W2_SEND_SERVICE_QUERY;
16323deb3ec6SMatthias Ringwald     peripheral->uuid16 = 0;
16333deb3ec6SMatthias Ringwald     gatt_client_run();
1634616edd56SMatthias Ringwald     return 0;
16353deb3ec6SMatthias Ringwald }
16363deb3ec6SMatthias Ringwald 
16373deb3ec6SMatthias Ringwald 
1638711e6c80SMatthias Ringwald uint8_t gatt_client_discover_primary_services_by_uuid16(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t uuid16){
16393deb3ec6SMatthias Ringwald     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
16403deb3ec6SMatthias Ringwald 
1641616edd56SMatthias Ringwald     if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED;
1642616edd56SMatthias Ringwald     if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE;
16433deb3ec6SMatthias Ringwald 
16449c662c9bSMatthias Ringwald     peripheral->callback = callback;
16453deb3ec6SMatthias Ringwald     peripheral->start_group_handle = 0x0001;
16463deb3ec6SMatthias Ringwald     peripheral->end_group_handle   = 0xffff;
16473deb3ec6SMatthias Ringwald     peripheral->gatt_client_state = P_W2_SEND_SERVICE_WITH_UUID_QUERY;
16483deb3ec6SMatthias Ringwald     peripheral->uuid16 = uuid16;
1649e1a125dfSMatthias Ringwald     uuid_add_bluetooth_prefix((uint8_t*) &(peripheral->uuid128), peripheral->uuid16);
16503deb3ec6SMatthias Ringwald     gatt_client_run();
1651616edd56SMatthias Ringwald     return 0;
16523deb3ec6SMatthias Ringwald }
16533deb3ec6SMatthias Ringwald 
1654711e6c80SMatthias 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){
16553deb3ec6SMatthias Ringwald     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
16563deb3ec6SMatthias Ringwald 
1657616edd56SMatthias Ringwald     if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED;
1658616edd56SMatthias Ringwald     if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE;
16593deb3ec6SMatthias Ringwald 
16609c662c9bSMatthias Ringwald     peripheral->callback = callback;
16613deb3ec6SMatthias Ringwald     peripheral->start_group_handle = 0x0001;
16623deb3ec6SMatthias Ringwald     peripheral->end_group_handle   = 0xffff;
16633deb3ec6SMatthias Ringwald     peripheral->uuid16 = 0;
16643deb3ec6SMatthias Ringwald     memcpy(peripheral->uuid128, uuid128, 16);
16653deb3ec6SMatthias Ringwald     peripheral->gatt_client_state = P_W2_SEND_SERVICE_WITH_UUID_QUERY;
16663deb3ec6SMatthias Ringwald     gatt_client_run();
1667616edd56SMatthias Ringwald     return 0;
16683deb3ec6SMatthias Ringwald }
16693deb3ec6SMatthias Ringwald 
1670711e6c80SMatthias 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){
16713deb3ec6SMatthias Ringwald     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
16723deb3ec6SMatthias Ringwald 
1673616edd56SMatthias Ringwald     if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED;
1674616edd56SMatthias Ringwald     if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE;
16753deb3ec6SMatthias Ringwald 
16769c662c9bSMatthias Ringwald     peripheral->callback = callback;
16773deb3ec6SMatthias Ringwald     peripheral->start_group_handle = service->start_group_handle;
16783deb3ec6SMatthias Ringwald     peripheral->end_group_handle   = service->end_group_handle;
16793deb3ec6SMatthias Ringwald     peripheral->filter_with_uuid = 0;
16803deb3ec6SMatthias Ringwald     peripheral->characteristic_start_handle = 0;
16813deb3ec6SMatthias Ringwald     peripheral->gatt_client_state = P_W2_SEND_ALL_CHARACTERISTICS_OF_SERVICE_QUERY;
16823deb3ec6SMatthias Ringwald     gatt_client_run();
1683616edd56SMatthias Ringwald     return 0;
16843deb3ec6SMatthias Ringwald }
16853deb3ec6SMatthias Ringwald 
1686711e6c80SMatthias 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){
16873deb3ec6SMatthias Ringwald     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
16883deb3ec6SMatthias Ringwald 
1689616edd56SMatthias Ringwald     if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED;
1690616edd56SMatthias Ringwald     if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE;
16913deb3ec6SMatthias Ringwald 
16929c662c9bSMatthias Ringwald     peripheral->callback = callback;
16933deb3ec6SMatthias Ringwald     peripheral->start_group_handle = service->start_group_handle;
16943deb3ec6SMatthias Ringwald     peripheral->end_group_handle   = service->end_group_handle;
16953deb3ec6SMatthias Ringwald     peripheral->gatt_client_state = P_W2_SEND_INCLUDED_SERVICE_QUERY;
16963deb3ec6SMatthias Ringwald 
16973deb3ec6SMatthias Ringwald     gatt_client_run();
1698616edd56SMatthias Ringwald     return 0;
16993deb3ec6SMatthias Ringwald }
17003deb3ec6SMatthias Ringwald 
1701711e6c80SMatthias 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){
17023deb3ec6SMatthias Ringwald     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
17033deb3ec6SMatthias Ringwald 
1704616edd56SMatthias Ringwald     if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED;
1705616edd56SMatthias Ringwald     if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE;
17063deb3ec6SMatthias Ringwald 
17079c662c9bSMatthias Ringwald     peripheral->callback = callback;
17083deb3ec6SMatthias Ringwald     peripheral->start_group_handle = start_handle;
17093deb3ec6SMatthias Ringwald     peripheral->end_group_handle   = end_handle;
17103deb3ec6SMatthias Ringwald     peripheral->filter_with_uuid = 1;
17113deb3ec6SMatthias Ringwald     peripheral->uuid16 = uuid16;
1712e1a125dfSMatthias Ringwald     uuid_add_bluetooth_prefix((uint8_t*) &(peripheral->uuid128), uuid16);
17133deb3ec6SMatthias Ringwald     peripheral->characteristic_start_handle = 0;
17143deb3ec6SMatthias Ringwald     peripheral->gatt_client_state = P_W2_SEND_CHARACTERISTIC_WITH_UUID_QUERY;
17153deb3ec6SMatthias Ringwald 
17163deb3ec6SMatthias Ringwald     gatt_client_run();
1717616edd56SMatthias Ringwald     return 0;
17183deb3ec6SMatthias Ringwald }
17193deb3ec6SMatthias Ringwald 
1720711e6c80SMatthias 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){
17213deb3ec6SMatthias Ringwald     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
17223deb3ec6SMatthias Ringwald 
1723616edd56SMatthias Ringwald     if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED;
1724616edd56SMatthias Ringwald     if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE;
17253deb3ec6SMatthias Ringwald 
17269c662c9bSMatthias Ringwald     peripheral->callback = callback;
17273deb3ec6SMatthias Ringwald     peripheral->start_group_handle = start_handle;
17283deb3ec6SMatthias Ringwald     peripheral->end_group_handle   = end_handle;
17293deb3ec6SMatthias Ringwald     peripheral->filter_with_uuid = 1;
17303deb3ec6SMatthias Ringwald     peripheral->uuid16 = 0;
17313deb3ec6SMatthias Ringwald     memcpy(peripheral->uuid128, uuid128, 16);
17323deb3ec6SMatthias Ringwald     peripheral->characteristic_start_handle = 0;
17333deb3ec6SMatthias Ringwald     peripheral->gatt_client_state = P_W2_SEND_CHARACTERISTIC_WITH_UUID_QUERY;
17343deb3ec6SMatthias Ringwald 
17353deb3ec6SMatthias Ringwald     gatt_client_run();
1736616edd56SMatthias Ringwald     return 0;
17373deb3ec6SMatthias Ringwald }
17383deb3ec6SMatthias Ringwald 
17393deb3ec6SMatthias Ringwald 
1740d25c33e5SMatthias 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){
17419c662c9bSMatthias Ringwald     return gatt_client_discover_characteristics_for_handle_range_by_uuid16(callback, handle, service->start_group_handle, service->end_group_handle, uuid16);
17423deb3ec6SMatthias Ringwald }
17433deb3ec6SMatthias Ringwald 
1744d25c33e5SMatthias 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){
17459c662c9bSMatthias Ringwald     return gatt_client_discover_characteristics_for_handle_range_by_uuid128(callback, handle, service->start_group_handle, service->end_group_handle, uuid128);
17463deb3ec6SMatthias Ringwald }
17473deb3ec6SMatthias Ringwald 
1748711e6c80SMatthias Ringwald uint8_t gatt_client_discover_characteristic_descriptors(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_characteristic_t *characteristic){
17493deb3ec6SMatthias Ringwald     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
17503deb3ec6SMatthias Ringwald 
1751616edd56SMatthias Ringwald     if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED;
1752616edd56SMatthias Ringwald     if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE;
17533deb3ec6SMatthias Ringwald 
17543deb3ec6SMatthias Ringwald     if (characteristic->value_handle == characteristic->end_handle){
17553deb3ec6SMatthias Ringwald         emit_gatt_complete_event(peripheral, 0);
1756616edd56SMatthias Ringwald         return 0;
17573deb3ec6SMatthias Ringwald     }
17589c662c9bSMatthias Ringwald     peripheral->callback = callback;
17593deb3ec6SMatthias Ringwald     peripheral->start_group_handle = characteristic->value_handle + 1;
17603deb3ec6SMatthias Ringwald     peripheral->end_group_handle   = characteristic->end_handle;
17613deb3ec6SMatthias Ringwald     peripheral->gatt_client_state = P_W2_SEND_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY;
17623deb3ec6SMatthias Ringwald 
17633deb3ec6SMatthias Ringwald     gatt_client_run();
1764616edd56SMatthias Ringwald     return 0;
17653deb3ec6SMatthias Ringwald }
17663deb3ec6SMatthias Ringwald 
1767711e6c80SMatthias 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){
17683deb3ec6SMatthias Ringwald     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
17693deb3ec6SMatthias Ringwald 
1770616edd56SMatthias Ringwald     if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED;
1771616edd56SMatthias Ringwald     if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE;
17723deb3ec6SMatthias Ringwald 
17739c662c9bSMatthias Ringwald     peripheral->callback = callback;
17743deb3ec6SMatthias Ringwald     peripheral->attribute_handle = value_handle;
17753deb3ec6SMatthias Ringwald     peripheral->attribute_offset = 0;
17763deb3ec6SMatthias Ringwald     peripheral->gatt_client_state = P_W2_SEND_READ_CHARACTERISTIC_VALUE_QUERY;
17773deb3ec6SMatthias Ringwald     gatt_client_run();
1778616edd56SMatthias Ringwald     return 0;
17793deb3ec6SMatthias Ringwald }
17803deb3ec6SMatthias Ringwald 
1781711e6c80SMatthias 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){
17823deb3ec6SMatthias Ringwald     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
17833deb3ec6SMatthias Ringwald 
1784616edd56SMatthias Ringwald     if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED;
1785616edd56SMatthias Ringwald     if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE;
17863deb3ec6SMatthias Ringwald 
17879c662c9bSMatthias Ringwald     peripheral->callback = callback;
17883deb3ec6SMatthias Ringwald     peripheral->start_group_handle = start_handle;
17893deb3ec6SMatthias Ringwald     peripheral->end_group_handle = end_handle;
17903deb3ec6SMatthias Ringwald     peripheral->query_start_handle = start_handle;
17913deb3ec6SMatthias Ringwald     peripheral->query_end_handle = end_handle;
17923deb3ec6SMatthias Ringwald     peripheral->uuid16 = uuid16;
1793e1a125dfSMatthias Ringwald     uuid_add_bluetooth_prefix((uint8_t*) &(peripheral->uuid128), uuid16);
17943deb3ec6SMatthias Ringwald     peripheral->gatt_client_state = P_W2_SEND_READ_BY_TYPE_REQUEST;
17953deb3ec6SMatthias Ringwald     gatt_client_run();
1796616edd56SMatthias Ringwald     return 0;
17973deb3ec6SMatthias Ringwald }
17983deb3ec6SMatthias Ringwald 
1799711e6c80SMatthias 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){
18003deb3ec6SMatthias Ringwald     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
18013deb3ec6SMatthias Ringwald 
1802616edd56SMatthias Ringwald     if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED;
1803616edd56SMatthias Ringwald     if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE;
18043deb3ec6SMatthias Ringwald 
18059c662c9bSMatthias Ringwald     peripheral->callback = callback;
18063deb3ec6SMatthias Ringwald     peripheral->start_group_handle = start_handle;
18073deb3ec6SMatthias Ringwald     peripheral->end_group_handle = end_handle;
18083deb3ec6SMatthias Ringwald     peripheral->query_start_handle = start_handle;
18093deb3ec6SMatthias Ringwald     peripheral->query_end_handle = end_handle;
18103deb3ec6SMatthias Ringwald     peripheral->uuid16 = 0;
18113deb3ec6SMatthias Ringwald     memcpy(peripheral->uuid128, uuid128, 16);
18123deb3ec6SMatthias Ringwald     peripheral->gatt_client_state = P_W2_SEND_READ_BY_TYPE_REQUEST;
18133deb3ec6SMatthias Ringwald     gatt_client_run();
1814616edd56SMatthias Ringwald     return 0;
18153deb3ec6SMatthias Ringwald }
18163deb3ec6SMatthias Ringwald 
18173deb3ec6SMatthias Ringwald 
1818d25c33e5SMatthias Ringwald uint8_t gatt_client_read_value_of_characteristic(btstack_packet_handler_t callback, uint16_t handle, gatt_client_characteristic_t *characteristic){
18199c662c9bSMatthias Ringwald     return gatt_client_read_value_of_characteristic_using_value_handle(callback, handle, characteristic->value_handle);
18203deb3ec6SMatthias Ringwald }
18213deb3ec6SMatthias Ringwald 
1822711e6c80SMatthias 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){
18233deb3ec6SMatthias Ringwald     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
18243deb3ec6SMatthias Ringwald 
1825616edd56SMatthias Ringwald     if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED;
1826616edd56SMatthias Ringwald     if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE;
18273deb3ec6SMatthias Ringwald 
18289c662c9bSMatthias Ringwald     peripheral->callback = callback;
18293deb3ec6SMatthias Ringwald     peripheral->attribute_handle = characteristic_value_handle;
18303deb3ec6SMatthias Ringwald     peripheral->attribute_offset = offset;
18313deb3ec6SMatthias Ringwald     peripheral->gatt_client_state = P_W2_SEND_READ_BLOB_QUERY;
18323deb3ec6SMatthias Ringwald     gatt_client_run();
1833616edd56SMatthias Ringwald     return 0;
18343deb3ec6SMatthias Ringwald }
18353deb3ec6SMatthias Ringwald 
1836711e6c80SMatthias 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){
18379c662c9bSMatthias Ringwald     return gatt_client_read_long_value_of_characteristic_using_value_handle_with_offset(callback, con_handle, characteristic_value_handle, 0);
18383deb3ec6SMatthias Ringwald }
18393deb3ec6SMatthias Ringwald 
1840d25c33e5SMatthias Ringwald uint8_t gatt_client_read_long_value_of_characteristic(btstack_packet_handler_t callback, uint16_t handle, gatt_client_characteristic_t *characteristic){
18419c662c9bSMatthias Ringwald     return gatt_client_read_long_value_of_characteristic_using_value_handle(callback, handle, characteristic->value_handle);
18423deb3ec6SMatthias Ringwald }
18433deb3ec6SMatthias Ringwald 
1844711e6c80SMatthias 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){
18453deb3ec6SMatthias Ringwald     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
18463deb3ec6SMatthias Ringwald 
1847616edd56SMatthias Ringwald     if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED;
1848616edd56SMatthias Ringwald     if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE;
18493deb3ec6SMatthias Ringwald 
18509c662c9bSMatthias Ringwald     peripheral->callback = callback;
18513deb3ec6SMatthias Ringwald     peripheral->read_multiple_handle_count = num_value_handles;
18523deb3ec6SMatthias Ringwald     peripheral->read_multiple_handles = value_handles;
18533deb3ec6SMatthias Ringwald     peripheral->gatt_client_state = P_W2_SEND_READ_MULTIPLE_REQUEST;
18543deb3ec6SMatthias Ringwald     gatt_client_run();
1855616edd56SMatthias Ringwald     return 0;
18563deb3ec6SMatthias Ringwald }
18573deb3ec6SMatthias Ringwald 
1858de9f8e94SMatthias 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){
18593deb3ec6SMatthias Ringwald     gatt_client_t * peripheral = provide_context_for_conn_handle(con_handle);
18603deb3ec6SMatthias Ringwald 
1861616edd56SMatthias Ringwald     if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED;
1862616edd56SMatthias Ringwald     if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE;
18633deb3ec6SMatthias Ringwald 
1864616edd56SMatthias Ringwald     if (value_length > peripheral_mtu(peripheral) - 3) return GATT_CLIENT_VALUE_TOO_LONG;
186551764b3bSMatthias Ringwald     if (!att_dispatch_client_can_send_now(peripheral->con_handle)) return GATT_CLIENT_BUSY;
18663deb3ec6SMatthias Ringwald 
1867fc64f94aSMatthias Ringwald     att_write_request(ATT_WRITE_COMMAND, peripheral->con_handle, value_handle, value_length, value);
1868616edd56SMatthias Ringwald     return 0;
18693deb3ec6SMatthias Ringwald }
18703deb3ec6SMatthias Ringwald 
1871711e6c80SMatthias 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){
18723deb3ec6SMatthias Ringwald     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
18733deb3ec6SMatthias Ringwald 
1874616edd56SMatthias Ringwald     if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED;
1875616edd56SMatthias Ringwald     if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE;
18763deb3ec6SMatthias Ringwald 
18779c662c9bSMatthias Ringwald     peripheral->callback = callback;
18783deb3ec6SMatthias Ringwald     peripheral->attribute_handle = value_handle;
18793deb3ec6SMatthias Ringwald     peripheral->attribute_length = value_length;
18803deb3ec6SMatthias Ringwald     peripheral->attribute_value = data;
18813deb3ec6SMatthias Ringwald     peripheral->gatt_client_state = P_W2_SEND_WRITE_CHARACTERISTIC_VALUE;
18823deb3ec6SMatthias Ringwald     gatt_client_run();
1883616edd56SMatthias Ringwald     return 0;
18843deb3ec6SMatthias Ringwald }
18853deb3ec6SMatthias Ringwald 
1886711e6c80SMatthias 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){
18873deb3ec6SMatthias Ringwald     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
18883deb3ec6SMatthias Ringwald 
1889616edd56SMatthias Ringwald     if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED;
1890616edd56SMatthias Ringwald     if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE;
18913deb3ec6SMatthias Ringwald 
18929c662c9bSMatthias Ringwald     peripheral->callback = callback;
18933deb3ec6SMatthias Ringwald     peripheral->attribute_handle = value_handle;
18943deb3ec6SMatthias Ringwald     peripheral->attribute_length = value_length;
18953deb3ec6SMatthias Ringwald     peripheral->attribute_offset = offset;
18963deb3ec6SMatthias Ringwald     peripheral->attribute_value = data;
18973deb3ec6SMatthias Ringwald     peripheral->gatt_client_state = P_W2_PREPARE_WRITE;
18983deb3ec6SMatthias Ringwald     gatt_client_run();
1899616edd56SMatthias Ringwald     return 0;
19003deb3ec6SMatthias Ringwald }
19013deb3ec6SMatthias Ringwald 
1902711e6c80SMatthias 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){
19039c662c9bSMatthias Ringwald     return gatt_client_write_long_value_of_characteristic_with_offset(callback, con_handle, value_handle, 0, value_length, value);
19043deb3ec6SMatthias Ringwald }
19053deb3ec6SMatthias Ringwald 
1906711e6c80SMatthias 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){
19073deb3ec6SMatthias Ringwald     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
19083deb3ec6SMatthias Ringwald 
1909616edd56SMatthias Ringwald     if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED;
1910616edd56SMatthias Ringwald     if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE;
19113deb3ec6SMatthias Ringwald 
19129c662c9bSMatthias Ringwald     peripheral->callback = callback;
19133deb3ec6SMatthias Ringwald     peripheral->attribute_handle = value_handle;
19143deb3ec6SMatthias Ringwald     peripheral->attribute_length = value_length;
19153deb3ec6SMatthias Ringwald     peripheral->attribute_offset = 0;
19163deb3ec6SMatthias Ringwald     peripheral->attribute_value = value;
19173deb3ec6SMatthias Ringwald     peripheral->gatt_client_state = P_W2_PREPARE_RELIABLE_WRITE;
19183deb3ec6SMatthias Ringwald     gatt_client_run();
1919616edd56SMatthias Ringwald     return 0;
19203deb3ec6SMatthias Ringwald }
19213deb3ec6SMatthias Ringwald 
1922711e6c80SMatthias 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){
19233deb3ec6SMatthias Ringwald     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
19243deb3ec6SMatthias Ringwald 
1925616edd56SMatthias Ringwald     if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED;
1926616edd56SMatthias Ringwald     if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE;
19273deb3ec6SMatthias Ringwald 
19283deb3ec6SMatthias Ringwald     if ( (configuration & GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION) &&
19293deb3ec6SMatthias Ringwald         (characteristic->properties & ATT_PROPERTY_NOTIFY) == 0) {
1930d8e8f12aSMatthias Ringwald         log_info("gatt_client_write_client_characteristic_configuration: GATT_CLIENT_CHARACTERISTIC_NOTIFICATION_NOT_SUPPORTED");
1931616edd56SMatthias Ringwald         return GATT_CLIENT_CHARACTERISTIC_NOTIFICATION_NOT_SUPPORTED;
19323deb3ec6SMatthias Ringwald     } else if ( (configuration & GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_INDICATION) &&
19333deb3ec6SMatthias Ringwald                (characteristic->properties & ATT_PROPERTY_INDICATE) == 0){
1934d8e8f12aSMatthias Ringwald         log_info("gatt_client_write_client_characteristic_configuration: GATT_CLIENT_CHARACTERISTIC_INDICATION_NOT_SUPPORTED");
1935616edd56SMatthias Ringwald         return GATT_CLIENT_CHARACTERISTIC_INDICATION_NOT_SUPPORTED;
19363deb3ec6SMatthias Ringwald     }
19373deb3ec6SMatthias Ringwald 
19389c662c9bSMatthias Ringwald     peripheral->callback = callback;
19393deb3ec6SMatthias Ringwald     peripheral->start_group_handle = characteristic->value_handle;
19403deb3ec6SMatthias Ringwald     peripheral->end_group_handle = characteristic->end_handle;
1941f8fbdce0SMatthias Ringwald     little_endian_store_16(peripheral->client_characteristic_configuration_value, 0, configuration);
19423deb3ec6SMatthias Ringwald 
1943*abdc9fb5SMatthias Ringwald #ifdef ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERY
1944*abdc9fb5SMatthias Ringwald     peripheral->gatt_client_state = P_W2_SEND_FIND_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY;
1945*abdc9fb5SMatthias Ringwald #else
19463deb3ec6SMatthias Ringwald     peripheral->gatt_client_state = P_W2_SEND_READ_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY;
1947*abdc9fb5SMatthias Ringwald #endif
19483deb3ec6SMatthias Ringwald     gatt_client_run();
1949616edd56SMatthias Ringwald     return 0;
19503deb3ec6SMatthias Ringwald }
19513deb3ec6SMatthias Ringwald 
1952711e6c80SMatthias 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){
19533deb3ec6SMatthias Ringwald     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
19543deb3ec6SMatthias Ringwald 
1955616edd56SMatthias Ringwald     if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED;
1956616edd56SMatthias Ringwald     if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE;
19573deb3ec6SMatthias Ringwald 
19589c662c9bSMatthias Ringwald     peripheral->callback = callback;
19593deb3ec6SMatthias Ringwald     peripheral->attribute_handle = descriptor_handle;
19603deb3ec6SMatthias Ringwald 
19613deb3ec6SMatthias Ringwald     peripheral->gatt_client_state = P_W2_SEND_READ_CHARACTERISTIC_DESCRIPTOR_QUERY;
19623deb3ec6SMatthias Ringwald     gatt_client_run();
1963616edd56SMatthias Ringwald     return 0;
19643deb3ec6SMatthias Ringwald }
19653deb3ec6SMatthias Ringwald 
1966711e6c80SMatthias 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){
19679c662c9bSMatthias Ringwald     return gatt_client_read_characteristic_descriptor_using_descriptor_handle(callback, con_handle, descriptor->handle);
19683deb3ec6SMatthias Ringwald }
19693deb3ec6SMatthias Ringwald 
1970711e6c80SMatthias 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){
19713deb3ec6SMatthias Ringwald     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
19723deb3ec6SMatthias Ringwald 
1973616edd56SMatthias Ringwald     if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED;
1974616edd56SMatthias Ringwald     if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE;
19753deb3ec6SMatthias Ringwald 
19769c662c9bSMatthias Ringwald     peripheral->callback = callback;
19773deb3ec6SMatthias Ringwald     peripheral->attribute_handle = descriptor_handle;
19783deb3ec6SMatthias Ringwald     peripheral->attribute_offset = offset;
19793deb3ec6SMatthias Ringwald     peripheral->gatt_client_state = P_W2_SEND_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_QUERY;
19803deb3ec6SMatthias Ringwald     gatt_client_run();
1981616edd56SMatthias Ringwald     return 0;
19823deb3ec6SMatthias Ringwald }
19833deb3ec6SMatthias Ringwald 
1984711e6c80SMatthias 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){
19859c662c9bSMatthias Ringwald     return gatt_client_read_long_characteristic_descriptor_using_descriptor_handle_with_offset(callback, con_handle, descriptor_handle, 0);
19863deb3ec6SMatthias Ringwald }
19873deb3ec6SMatthias Ringwald 
1988711e6c80SMatthias 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){
19899c662c9bSMatthias Ringwald     return gatt_client_read_long_characteristic_descriptor_using_descriptor_handle(callback, con_handle, descriptor->handle);
19903deb3ec6SMatthias Ringwald }
19913deb3ec6SMatthias Ringwald 
1992711e6c80SMatthias 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){
19933deb3ec6SMatthias Ringwald     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
19943deb3ec6SMatthias Ringwald 
1995616edd56SMatthias Ringwald     if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED;
1996616edd56SMatthias Ringwald     if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE;
19973deb3ec6SMatthias Ringwald 
19989c662c9bSMatthias Ringwald     peripheral->callback = callback;
19993deb3ec6SMatthias Ringwald     peripheral->attribute_handle = descriptor_handle;
20003deb3ec6SMatthias Ringwald     peripheral->attribute_length = length;
20013deb3ec6SMatthias Ringwald     peripheral->attribute_offset = 0;
20023deb3ec6SMatthias Ringwald     peripheral->attribute_value = data;
20033deb3ec6SMatthias Ringwald     peripheral->gatt_client_state = P_W2_SEND_WRITE_CHARACTERISTIC_DESCRIPTOR;
20043deb3ec6SMatthias Ringwald     gatt_client_run();
2005616edd56SMatthias Ringwald     return 0;
20063deb3ec6SMatthias Ringwald }
20073deb3ec6SMatthias Ringwald 
2008711e6c80SMatthias 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){
20099c662c9bSMatthias Ringwald     return gatt_client_write_characteristic_descriptor_using_descriptor_handle(callback, con_handle, descriptor->handle, length, value);
20103deb3ec6SMatthias Ringwald }
20113deb3ec6SMatthias Ringwald 
2012711e6c80SMatthias 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){
20133deb3ec6SMatthias Ringwald     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
20143deb3ec6SMatthias Ringwald 
2015616edd56SMatthias Ringwald     if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED;
2016616edd56SMatthias Ringwald     if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE;
20173deb3ec6SMatthias Ringwald 
20189c662c9bSMatthias Ringwald     peripheral->callback = callback;
20193deb3ec6SMatthias Ringwald     peripheral->attribute_handle = descriptor_handle;
20203deb3ec6SMatthias Ringwald     peripheral->attribute_length = length;
20213deb3ec6SMatthias Ringwald     peripheral->attribute_offset = offset;
20223deb3ec6SMatthias Ringwald     peripheral->attribute_value = data;
20233deb3ec6SMatthias Ringwald     peripheral->gatt_client_state = P_W2_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR;
20243deb3ec6SMatthias Ringwald     gatt_client_run();
2025616edd56SMatthias Ringwald     return 0;
20263deb3ec6SMatthias Ringwald }
20273deb3ec6SMatthias Ringwald 
2028711e6c80SMatthias 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){
20299c662c9bSMatthias Ringwald     return gatt_client_write_long_characteristic_descriptor_using_descriptor_handle_with_offset(callback, con_handle, descriptor_handle, 0, length, data );
20303deb3ec6SMatthias Ringwald }
20313deb3ec6SMatthias Ringwald 
2032711e6c80SMatthias 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){
20339c662c9bSMatthias Ringwald     return gatt_client_write_long_characteristic_descriptor_using_descriptor_handle(callback, con_handle, descriptor->handle, length, value);
20343deb3ec6SMatthias Ringwald }
20353deb3ec6SMatthias Ringwald 
20363deb3ec6SMatthias Ringwald /**
20373deb3ec6SMatthias Ringwald  * @brief -> gatt complete event
20383deb3ec6SMatthias Ringwald  */
2039711e6c80SMatthias 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){
20403deb3ec6SMatthias Ringwald     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
20413deb3ec6SMatthias Ringwald 
2042616edd56SMatthias Ringwald     if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED;
2043616edd56SMatthias Ringwald     if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE;
20443deb3ec6SMatthias Ringwald 
20459c662c9bSMatthias Ringwald     peripheral->callback = callback;
20463deb3ec6SMatthias Ringwald     peripheral->attribute_handle = attribute_handle;
20473deb3ec6SMatthias Ringwald     peripheral->attribute_length = length;
20483deb3ec6SMatthias Ringwald     peripheral->attribute_offset = offset;
20493deb3ec6SMatthias Ringwald     peripheral->attribute_value = data;
20503deb3ec6SMatthias Ringwald     peripheral->gatt_client_state = P_W2_PREPARE_WRITE_SINGLE;
20513deb3ec6SMatthias Ringwald     gatt_client_run();
2052616edd56SMatthias Ringwald     return 0;
20533deb3ec6SMatthias Ringwald }
20543deb3ec6SMatthias Ringwald 
20553deb3ec6SMatthias Ringwald /**
20563deb3ec6SMatthias Ringwald  * @brief -> gatt complete event
20573deb3ec6SMatthias Ringwald  */
2058711e6c80SMatthias Ringwald uint8_t gatt_client_execute_write(btstack_packet_handler_t callback, hci_con_handle_t con_handle){
20593deb3ec6SMatthias Ringwald     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
20603deb3ec6SMatthias Ringwald 
2061616edd56SMatthias Ringwald     if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED;
2062616edd56SMatthias Ringwald     if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE;
20633deb3ec6SMatthias Ringwald 
20649c662c9bSMatthias Ringwald     peripheral->callback = callback;
20653deb3ec6SMatthias Ringwald     peripheral->gatt_client_state = P_W2_EXECUTE_PREPARED_WRITE;
20663deb3ec6SMatthias Ringwald     gatt_client_run();
2067616edd56SMatthias Ringwald     return 0;
20683deb3ec6SMatthias Ringwald }
20693deb3ec6SMatthias Ringwald 
20703deb3ec6SMatthias Ringwald /**
20713deb3ec6SMatthias Ringwald  * @brief -> gatt complete event
20723deb3ec6SMatthias Ringwald  */
2073711e6c80SMatthias Ringwald uint8_t gatt_client_cancel_write(btstack_packet_handler_t callback, hci_con_handle_t con_handle){
20743deb3ec6SMatthias Ringwald     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
20753deb3ec6SMatthias Ringwald 
2076616edd56SMatthias Ringwald     if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED;
2077616edd56SMatthias Ringwald     if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE;
20783deb3ec6SMatthias Ringwald 
20799c662c9bSMatthias Ringwald     peripheral->callback = callback;
20803deb3ec6SMatthias Ringwald     peripheral->gatt_client_state = P_W2_CANCEL_PREPARED_WRITE;
20813deb3ec6SMatthias Ringwald     gatt_client_run();
2082616edd56SMatthias Ringwald     return 0;
20833deb3ec6SMatthias Ringwald }
20843deb3ec6SMatthias Ringwald 
2085313e337bSMatthias Ringwald void gatt_client_deserialize_service(const uint8_t *packet, int offset, gatt_client_service_t *service){
20866ba2ad22SMatthias Ringwald     service->start_group_handle = little_endian_read_16(packet, offset);
20876ba2ad22SMatthias Ringwald     service->end_group_handle = little_endian_read_16(packet, offset + 2);
20886ba2ad22SMatthias Ringwald     reverse_128(&packet[offset + 4], service->uuid128);
20896ba2ad22SMatthias Ringwald     if (uuid_has_bluetooth_prefix(service->uuid128)){
20906ba2ad22SMatthias Ringwald         service->uuid16 = big_endian_read_32(service->uuid128, 0);
20916ba2ad22SMatthias Ringwald     }
20926ba2ad22SMatthias Ringwald }
20936ba2ad22SMatthias Ringwald 
2094313e337bSMatthias Ringwald void gatt_client_deserialize_characteristic(const uint8_t * packet, int offset, gatt_client_characteristic_t * characteristic){
20956ba2ad22SMatthias Ringwald     characteristic->start_handle = little_endian_read_16(packet, offset);
20966ba2ad22SMatthias Ringwald     characteristic->value_handle = little_endian_read_16(packet, offset + 2);
20976ba2ad22SMatthias Ringwald     characteristic->end_handle = little_endian_read_16(packet, offset + 4);
20986ba2ad22SMatthias Ringwald     characteristic->properties = little_endian_read_16(packet, offset + 6);
209986c38559SMatthias Ringwald     reverse_128(&packet[offset+8], characteristic->uuid128);
21006ba2ad22SMatthias Ringwald     if (uuid_has_bluetooth_prefix(characteristic->uuid128)){
21016ba2ad22SMatthias Ringwald         characteristic->uuid16 = big_endian_read_32(characteristic->uuid128, 0);
21026ba2ad22SMatthias Ringwald     }
21036ba2ad22SMatthias Ringwald }
21046ba2ad22SMatthias Ringwald 
2105313e337bSMatthias Ringwald void gatt_client_deserialize_characteristic_descriptor(const uint8_t * packet, int offset, gatt_client_characteristic_descriptor_t * descriptor){
21066ba2ad22SMatthias Ringwald     descriptor->handle = little_endian_read_16(packet, offset);
21076ba2ad22SMatthias Ringwald     reverse_128(&packet[offset+2], descriptor->uuid128);
2108b4895529SJakob Krantz     if (uuid_has_bluetooth_prefix(descriptor->uuid128)){
2109b4895529SJakob Krantz         descriptor->uuid16 = big_endian_read_32(descriptor->uuid128, 0);
2110b4895529SJakob Krantz     }
21116ba2ad22SMatthias Ringwald }
21125cf6c434SJakob Krantz 
21135cf6c434SJakob Krantz void gatt_client_send_mtu_negotiation(btstack_packet_handler_t callback, hci_con_handle_t con_handle){
21145cf6c434SJakob Krantz     gatt_client_t * context = provide_context_for_conn_handle(con_handle);
21155cf6c434SJakob Krantz     if (!context) return;
21165cf6c434SJakob Krantz     if (context->mtu_state == MTU_AUTO_EXCHANGE_DISABLED){
21175cf6c434SJakob Krantz         context->callback = callback;
21185cf6c434SJakob Krantz         context->mtu_state = SEND_MTU_EXCHANGE;
21195cf6c434SJakob Krantz         gatt_client_run();
21205cf6c434SJakob Krantz     }
21215cf6c434SJakob Krantz }
212247181045SMatthias Ringwald 
212347181045SMatthias Ringwald uint8_t gatt_client_request_can_write_without_response_event(btstack_packet_handler_t callback, hci_con_handle_t con_handle){
212447181045SMatthias Ringwald     gatt_client_t * context = provide_context_for_conn_handle(con_handle);
212547181045SMatthias Ringwald     if (!context) return BTSTACK_MEMORY_ALLOC_FAILED;
212647181045SMatthias Ringwald     if (context->write_without_response_callback) return GATT_CLIENT_IN_WRONG_STATE;
212747181045SMatthias Ringwald     context->write_without_response_callback = callback;
212847181045SMatthias Ringwald     att_dispatch_client_request_can_send_now_event(context->con_handle);
212947181045SMatthias Ringwald     return 0;
213047181045SMatthias Ringwald }
2131