xref: /btstack/src/ble/gatt_client.c (revision 7d2258b36d2f51a5e637d94fc8cddd9cdc2c9e4f)
13deb3ec6SMatthias Ringwald /*
23deb3ec6SMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
33deb3ec6SMatthias Ringwald  *
43deb3ec6SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
53deb3ec6SMatthias Ringwald  * modification, are permitted provided that the following conditions
63deb3ec6SMatthias Ringwald  * are met:
73deb3ec6SMatthias Ringwald  *
83deb3ec6SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
93deb3ec6SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
103deb3ec6SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
113deb3ec6SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
123deb3ec6SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
133deb3ec6SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
143deb3ec6SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
153deb3ec6SMatthias Ringwald  *    from this software without specific prior written permission.
163deb3ec6SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
173deb3ec6SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
183deb3ec6SMatthias Ringwald  *    monetary gain.
193deb3ec6SMatthias Ringwald  *
203deb3ec6SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
213deb3ec6SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
223deb3ec6SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
233deb3ec6SMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
243deb3ec6SMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
253deb3ec6SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
263deb3ec6SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
273deb3ec6SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
283deb3ec6SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
293deb3ec6SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
303deb3ec6SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
313deb3ec6SMatthias Ringwald  * SUCH DAMAGE.
323deb3ec6SMatthias Ringwald  *
333deb3ec6SMatthias Ringwald  * Please inquire about commercial licensing options at
343deb3ec6SMatthias Ringwald  * [email protected]
353deb3ec6SMatthias Ringwald  *
363deb3ec6SMatthias Ringwald  */
373deb3ec6SMatthias Ringwald 
38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "gatt_client.c"
39ab2c6ae4SMatthias Ringwald 
403deb3ec6SMatthias Ringwald #include <stdint.h>
413deb3ec6SMatthias Ringwald #include <string.h>
423deb3ec6SMatthias Ringwald 
437907f069SMatthias Ringwald #include "btstack_config.h"
443deb3ec6SMatthias Ringwald 
450e2df43fSMatthias Ringwald #include "att_dispatch.h"
46fac60feaSMatthias Ringwald #include "ad_parser.h"
470e2df43fSMatthias Ringwald #include "ble/att_db.h"
4859c6af15SMatthias Ringwald #include "ble/core.h"
490e2df43fSMatthias Ringwald #include "ble/gatt_client.h"
500e2df43fSMatthias Ringwald #include "ble/le_device_db.h"
510e2df43fSMatthias Ringwald #include "ble/sm.h"
5216ece135SMatthias Ringwald #include "btstack_debug.h"
530e2df43fSMatthias Ringwald #include "btstack_event.h"
543deb3ec6SMatthias Ringwald #include "btstack_memory.h"
550e2df43fSMatthias Ringwald #include "btstack_run_loop.h"
560e2df43fSMatthias Ringwald #include "btstack_util.h"
570e2df43fSMatthias Ringwald #include "classic/sdp_util.h"
583deb3ec6SMatthias Ringwald #include "hci.h"
590e2df43fSMatthias Ringwald #include "hci_cmd.h"
603deb3ec6SMatthias Ringwald #include "hci_dump.h"
613deb3ec6SMatthias Ringwald #include "l2cap.h"
623deb3ec6SMatthias Ringwald 
639c662c9bSMatthias Ringwald static btstack_linked_list_t gatt_client_connections;
649c662c9bSMatthias Ringwald static btstack_linked_list_t gatt_client_value_listeners;
65361b1363SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
663deb3ec6SMatthias Ringwald 
67793cf6ceSMatthias Ringwald #if defined(ENABLE_GATT_CLIENT_PAIRING) || defined (ENABLE_LE_SIGNED_WRITE)
68f4b33574SMatthias Ringwald static btstack_packet_callback_registration_t sm_event_callback_registration;
69f4b33574SMatthias Ringwald #endif
70f4b33574SMatthias Ringwald 
715cf6c434SJakob Krantz static uint8_t mtu_exchange_enabled;
725cf6c434SJakob Krantz 
733deb3ec6SMatthias Ringwald static void gatt_client_att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size);
74f4b33574SMatthias Ringwald static void gatt_client_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
75*7d2258b3SMilanka Ringwald static void gatt_client_report_error_if_pending(gatt_client_t *peripheral, uint8_t att_error_code);
767a766ebfSMatthias Ringwald 
777a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
783deb3ec6SMatthias Ringwald static void att_signed_write_handle_cmac_result(uint8_t hash[8]);
797a766ebfSMatthias Ringwald #endif
803deb3ec6SMatthias Ringwald 
813deb3ec6SMatthias Ringwald static uint16_t peripheral_mtu(gatt_client_t *peripheral){
823deb3ec6SMatthias Ringwald     if (peripheral->mtu > l2cap_max_le_mtu()){
833deb3ec6SMatthias Ringwald         log_error("Peripheral mtu is not initialized");
843deb3ec6SMatthias Ringwald         return l2cap_max_le_mtu();
853deb3ec6SMatthias Ringwald     }
863deb3ec6SMatthias Ringwald     return peripheral->mtu;
873deb3ec6SMatthias Ringwald }
883deb3ec6SMatthias Ringwald 
893deb3ec6SMatthias Ringwald void gatt_client_init(void){
903deb3ec6SMatthias Ringwald     gatt_client_connections = NULL;
915cf6c434SJakob Krantz     mtu_exchange_enabled = 1;
92f4b33574SMatthias Ringwald 
93361b1363SMatthias Ringwald     // regsister for HCI Events
94f4b33574SMatthias Ringwald     hci_event_callback_registration.callback = &gatt_client_event_packet_handler;
95361b1363SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
96361b1363SMatthias Ringwald 
97793cf6ceSMatthias Ringwald #if defined(ENABLE_GATT_CLIENT_PAIRING) || defined (ENABLE_LE_SIGNED_WRITE)
98f4b33574SMatthias Ringwald     // register for SM Events
99f4b33574SMatthias Ringwald     sm_event_callback_registration.callback = &gatt_client_event_packet_handler;
100f4b33574SMatthias Ringwald     sm_add_event_handler(&sm_event_callback_registration);
101f4b33574SMatthias Ringwald #endif
102f4b33574SMatthias Ringwald 
103361b1363SMatthias Ringwald     // and ATT Client PDUs
1043deb3ec6SMatthias Ringwald     att_dispatch_register_client(gatt_client_att_packet_handler);
1053deb3ec6SMatthias Ringwald }
1063deb3ec6SMatthias Ringwald 
107ec820d77SMatthias Ringwald static gatt_client_t * gatt_client_for_timer(btstack_timer_source_t * ts){
108665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
109665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &gatt_client_connections);
110665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
111665d90f2SMatthias Ringwald         gatt_client_t * peripheral = (gatt_client_t *) btstack_linked_list_iterator_next(&it);
1123deb3ec6SMatthias Ringwald         if ( &peripheral->gc_timeout == ts) {
1133deb3ec6SMatthias Ringwald             return peripheral;
1143deb3ec6SMatthias Ringwald         }
1153deb3ec6SMatthias Ringwald     }
1163deb3ec6SMatthias Ringwald     return NULL;
1173deb3ec6SMatthias Ringwald }
1183deb3ec6SMatthias Ringwald 
119ec820d77SMatthias Ringwald static void gatt_client_timeout_handler(btstack_timer_source_t * timer){
1203deb3ec6SMatthias Ringwald     gatt_client_t * peripheral = gatt_client_for_timer(timer);
121*7d2258b3SMilanka Ringwald     if (peripheral == NULL) return;
122fc64f94aSMatthias Ringwald     log_info("GATT client timeout handle, handle 0x%02x", peripheral->con_handle);
1233deb3ec6SMatthias Ringwald     gatt_client_report_error_if_pending(peripheral, ATT_ERROR_TIMEOUT);
1243deb3ec6SMatthias Ringwald }
1253deb3ec6SMatthias Ringwald 
1263deb3ec6SMatthias Ringwald static void gatt_client_timeout_start(gatt_client_t * peripheral){
127fc64f94aSMatthias Ringwald     log_info("GATT client timeout start, handle 0x%02x", peripheral->con_handle);
128528a4a3bSMatthias Ringwald     btstack_run_loop_remove_timer(&peripheral->gc_timeout);
129528a4a3bSMatthias Ringwald     btstack_run_loop_set_timer_handler(&peripheral->gc_timeout, gatt_client_timeout_handler);
130528a4a3bSMatthias Ringwald     btstack_run_loop_set_timer(&peripheral->gc_timeout, 30000); // 30 seconds sm timeout
131528a4a3bSMatthias Ringwald     btstack_run_loop_add_timer(&peripheral->gc_timeout);
1323deb3ec6SMatthias Ringwald }
1333deb3ec6SMatthias Ringwald 
1343deb3ec6SMatthias Ringwald static void gatt_client_timeout_stop(gatt_client_t * peripheral){
135fc64f94aSMatthias Ringwald     log_info("GATT client timeout stop, handle 0x%02x", peripheral->con_handle);
136528a4a3bSMatthias Ringwald     btstack_run_loop_remove_timer(&peripheral->gc_timeout);
1373deb3ec6SMatthias Ringwald }
1383deb3ec6SMatthias Ringwald 
1393deb3ec6SMatthias Ringwald static gatt_client_t * get_gatt_client_context_for_handle(uint16_t handle){
140665d90f2SMatthias Ringwald     btstack_linked_item_t *it;
141665d90f2SMatthias Ringwald     for (it = (btstack_linked_item_t *) gatt_client_connections; it ; it = it->next){
1423deb3ec6SMatthias Ringwald         gatt_client_t * peripheral = (gatt_client_t *) it;
143fc64f94aSMatthias Ringwald         if (peripheral->con_handle == handle){
1443deb3ec6SMatthias Ringwald             return peripheral;
1453deb3ec6SMatthias Ringwald         }
1463deb3ec6SMatthias Ringwald     }
1473deb3ec6SMatthias Ringwald     return NULL;
1483deb3ec6SMatthias Ringwald }
1493deb3ec6SMatthias Ringwald 
1503deb3ec6SMatthias Ringwald 
1513deb3ec6SMatthias Ringwald // @returns context
1523deb3ec6SMatthias Ringwald // returns existing one, or tries to setup new one
153711e6c80SMatthias Ringwald static gatt_client_t * provide_context_for_conn_handle(hci_con_handle_t con_handle){
1543deb3ec6SMatthias Ringwald     gatt_client_t * context = get_gatt_client_context_for_handle(con_handle);
1553deb3ec6SMatthias Ringwald     if (context) return context;
1563deb3ec6SMatthias Ringwald 
157ae1d1237SMatthias Ringwald     // bail if no such hci connection
158ae1d1237SMatthias Ringwald     if (!hci_connection_for_handle(con_handle)){
159ae1d1237SMatthias Ringwald         log_error("No connection for handle 0x%04x", con_handle);
160ae1d1237SMatthias Ringwald         return NULL;
161ae1d1237SMatthias Ringwald     }
1623deb3ec6SMatthias Ringwald     context = btstack_memory_gatt_client_get();
1633deb3ec6SMatthias Ringwald     if (!context) return NULL;
1643deb3ec6SMatthias Ringwald     // init state
165fc64f94aSMatthias Ringwald     context->con_handle = con_handle;
1663deb3ec6SMatthias Ringwald     context->mtu = ATT_DEFAULT_MTU;
1675cf6c434SJakob Krantz     if (mtu_exchange_enabled){
1683deb3ec6SMatthias Ringwald         context->mtu_state = SEND_MTU_EXCHANGE;
1695cf6c434SJakob Krantz     } else {
1705cf6c434SJakob Krantz         context->mtu_state = MTU_AUTO_EXCHANGE_DISABLED;
1715cf6c434SJakob Krantz     }
1723deb3ec6SMatthias Ringwald     context->gatt_client_state = P_READY;
173665d90f2SMatthias Ringwald     btstack_linked_list_add(&gatt_client_connections, (btstack_linked_item_t*)context);
1743deb3ec6SMatthias Ringwald     return context;
1753deb3ec6SMatthias Ringwald }
1763deb3ec6SMatthias Ringwald 
177711e6c80SMatthias Ringwald static gatt_client_t * provide_context_for_conn_handle_and_start_timer(hci_con_handle_t con_handle){
1783deb3ec6SMatthias Ringwald     gatt_client_t * context = provide_context_for_conn_handle(con_handle);
179*7d2258b3SMilanka Ringwald     if (context == NULL) return NULL;
1803deb3ec6SMatthias Ringwald     gatt_client_timeout_start(context);
1813deb3ec6SMatthias Ringwald     return context;
1823deb3ec6SMatthias Ringwald }
1833deb3ec6SMatthias Ringwald 
1843deb3ec6SMatthias Ringwald static int is_ready(gatt_client_t * context){
1853deb3ec6SMatthias Ringwald     return context->gatt_client_state == P_READY;
1863deb3ec6SMatthias Ringwald }
1873deb3ec6SMatthias Ringwald 
188fc64f94aSMatthias Ringwald int gatt_client_is_ready(hci_con_handle_t con_handle){
189fc64f94aSMatthias Ringwald     gatt_client_t * context = provide_context_for_conn_handle(con_handle);
190*7d2258b3SMilanka Ringwald     if (context == NULL) return 0;
1913deb3ec6SMatthias Ringwald     return is_ready(context);
1923deb3ec6SMatthias Ringwald }
1933deb3ec6SMatthias Ringwald 
1945cf6c434SJakob Krantz void gatt_client_mtu_enable_auto_negotiation(uint8_t enabled){
1955cf6c434SJakob Krantz     mtu_exchange_enabled = enabled;
1965cf6c434SJakob Krantz }
1975cf6c434SJakob Krantz 
198fc64f94aSMatthias Ringwald uint8_t gatt_client_get_mtu(hci_con_handle_t con_handle, uint16_t * mtu){
199fc64f94aSMatthias Ringwald     gatt_client_t * context = provide_context_for_conn_handle(con_handle);
200*7d2258b3SMilanka Ringwald     if (context == NULL) return BTSTACK_MEMORY_ALLOC_FAILED;
201*7d2258b3SMilanka Ringwald 
202*7d2258b3SMilanka Ringwald     if (context->mtu_state == MTU_EXCHANGED || context->mtu_state == MTU_AUTO_EXCHANGE_DISABLED){
2033deb3ec6SMatthias Ringwald         *mtu = context->mtu;
204*7d2258b3SMilanka Ringwald         return ERROR_CODE_SUCCESS;
2053deb3ec6SMatthias Ringwald     }
2063deb3ec6SMatthias Ringwald     *mtu = ATT_DEFAULT_MTU;
207616edd56SMatthias Ringwald     return GATT_CLIENT_IN_WRONG_STATE;
2083deb3ec6SMatthias Ringwald }
2093deb3ec6SMatthias Ringwald 
2103deb3ec6SMatthias Ringwald // precondition: can_send_packet_now == TRUE
2113deb3ec6SMatthias Ringwald static void att_confirmation(uint16_t peripheral_handle){
2123deb3ec6SMatthias Ringwald     l2cap_reserve_packet_buffer();
2133deb3ec6SMatthias Ringwald     uint8_t * request = l2cap_get_outgoing_buffer();
2143deb3ec6SMatthias Ringwald     request[0] = ATT_HANDLE_VALUE_CONFIRMATION;
2153deb3ec6SMatthias Ringwald     l2cap_send_prepared_connectionless(peripheral_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, 1);
2163deb3ec6SMatthias Ringwald }
2173deb3ec6SMatthias Ringwald 
2183deb3ec6SMatthias Ringwald // precondition: can_send_packet_now == TRUE
2193deb3ec6SMatthias Ringwald static void att_find_information_request(uint16_t request_type, uint16_t peripheral_handle, uint16_t start_handle, uint16_t end_handle){
2203deb3ec6SMatthias Ringwald     l2cap_reserve_packet_buffer();
2213deb3ec6SMatthias Ringwald     uint8_t * request = l2cap_get_outgoing_buffer();
2223deb3ec6SMatthias Ringwald     request[0] = request_type;
223f8fbdce0SMatthias Ringwald     little_endian_store_16(request, 1, start_handle);
224f8fbdce0SMatthias Ringwald     little_endian_store_16(request, 3, end_handle);
2253deb3ec6SMatthias Ringwald 
2263deb3ec6SMatthias Ringwald     l2cap_send_prepared_connectionless(peripheral_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, 5);
2273deb3ec6SMatthias Ringwald }
2283deb3ec6SMatthias Ringwald 
2293deb3ec6SMatthias Ringwald // precondition: can_send_packet_now == TRUE
2303deb3ec6SMatthias 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){
2313deb3ec6SMatthias Ringwald     l2cap_reserve_packet_buffer();
2323deb3ec6SMatthias Ringwald     uint8_t * request = l2cap_get_outgoing_buffer();
2333deb3ec6SMatthias Ringwald 
2343deb3ec6SMatthias Ringwald     request[0] = request_type;
235f8fbdce0SMatthias Ringwald     little_endian_store_16(request, 1, start_handle);
236f8fbdce0SMatthias Ringwald     little_endian_store_16(request, 3, end_handle);
237f8fbdce0SMatthias Ringwald     little_endian_store_16(request, 5, attribute_group_type);
2383deb3ec6SMatthias Ringwald     memcpy(&request[7], value, value_size);
2393deb3ec6SMatthias Ringwald 
2403deb3ec6SMatthias Ringwald     l2cap_send_prepared_connectionless(peripheral_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, 7+value_size);
2413deb3ec6SMatthias Ringwald }
2423deb3ec6SMatthias Ringwald 
2433deb3ec6SMatthias Ringwald // precondition: can_send_packet_now == TRUE
2443deb3ec6SMatthias 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){
2453deb3ec6SMatthias Ringwald     l2cap_reserve_packet_buffer();
2463deb3ec6SMatthias Ringwald     uint8_t * request = l2cap_get_outgoing_buffer();
2473deb3ec6SMatthias Ringwald     request[0] = request_type;
248f8fbdce0SMatthias Ringwald     little_endian_store_16(request, 1, start_handle);
249f8fbdce0SMatthias Ringwald     little_endian_store_16(request, 3, end_handle);
250f8fbdce0SMatthias Ringwald     little_endian_store_16(request, 5, uuid16);
2513deb3ec6SMatthias Ringwald 
2523deb3ec6SMatthias Ringwald     l2cap_send_prepared_connectionless(peripheral_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, 7);
2533deb3ec6SMatthias Ringwald }
2543deb3ec6SMatthias Ringwald 
2553deb3ec6SMatthias Ringwald // precondition: can_send_packet_now == TRUE
2563deb3ec6SMatthias 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){
2573deb3ec6SMatthias Ringwald     l2cap_reserve_packet_buffer();
2583deb3ec6SMatthias Ringwald     uint8_t * request = l2cap_get_outgoing_buffer();
2593deb3ec6SMatthias Ringwald     request[0] = request_type;
260f8fbdce0SMatthias Ringwald     little_endian_store_16(request, 1, start_handle);
261f8fbdce0SMatthias Ringwald     little_endian_store_16(request, 3, end_handle);
2629c80e4ccSMatthias Ringwald     reverse_128(uuid128, &request[5]);
2633deb3ec6SMatthias Ringwald 
2643deb3ec6SMatthias Ringwald     l2cap_send_prepared_connectionless(peripheral_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, 21);
2653deb3ec6SMatthias Ringwald }
2663deb3ec6SMatthias Ringwald 
2673deb3ec6SMatthias Ringwald // precondition: can_send_packet_now == TRUE
2683deb3ec6SMatthias Ringwald static void att_read_request(uint16_t request_type, uint16_t peripheral_handle, uint16_t attribute_handle){
2693deb3ec6SMatthias Ringwald     l2cap_reserve_packet_buffer();
2703deb3ec6SMatthias Ringwald     uint8_t * request = l2cap_get_outgoing_buffer();
2713deb3ec6SMatthias Ringwald     request[0] = request_type;
272f8fbdce0SMatthias Ringwald     little_endian_store_16(request, 1, attribute_handle);
2733deb3ec6SMatthias Ringwald 
2743deb3ec6SMatthias Ringwald     l2cap_send_prepared_connectionless(peripheral_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, 3);
2753deb3ec6SMatthias Ringwald }
2763deb3ec6SMatthias Ringwald 
2773deb3ec6SMatthias Ringwald // precondition: can_send_packet_now == TRUE
2783deb3ec6SMatthias Ringwald static void att_read_blob_request(uint16_t request_type, uint16_t peripheral_handle, uint16_t attribute_handle, uint16_t value_offset){
2793deb3ec6SMatthias Ringwald     l2cap_reserve_packet_buffer();
2803deb3ec6SMatthias Ringwald     uint8_t * request = l2cap_get_outgoing_buffer();
2813deb3ec6SMatthias Ringwald     request[0] = request_type;
282f8fbdce0SMatthias Ringwald     little_endian_store_16(request, 1, attribute_handle);
283f8fbdce0SMatthias Ringwald     little_endian_store_16(request, 3, value_offset);
2843deb3ec6SMatthias Ringwald 
2853deb3ec6SMatthias Ringwald     l2cap_send_prepared_connectionless(peripheral_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, 5);
2863deb3ec6SMatthias Ringwald }
2873deb3ec6SMatthias Ringwald 
2883deb3ec6SMatthias Ringwald static void att_read_multiple_request(uint16_t peripheral_handle, uint16_t num_value_handles, uint16_t * value_handles){
2893deb3ec6SMatthias Ringwald     l2cap_reserve_packet_buffer();
2903deb3ec6SMatthias Ringwald     uint8_t * request = l2cap_get_outgoing_buffer();
2913deb3ec6SMatthias Ringwald     request[0] = ATT_READ_MULTIPLE_REQUEST;
2923deb3ec6SMatthias Ringwald     int i;
2933deb3ec6SMatthias Ringwald     int offset = 1;
2943deb3ec6SMatthias Ringwald     for (i=0;i<num_value_handles;i++){
295f8fbdce0SMatthias Ringwald         little_endian_store_16(request, offset, value_handles[i]);
2963deb3ec6SMatthias Ringwald         offset += 2;
2973deb3ec6SMatthias Ringwald     }
2983deb3ec6SMatthias Ringwald     l2cap_send_prepared_connectionless(peripheral_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, offset);
2993deb3ec6SMatthias Ringwald }
3003deb3ec6SMatthias Ringwald 
3017a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
3023deb3ec6SMatthias Ringwald // precondition: can_send_packet_now == TRUE
3033deb3ec6SMatthias 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]){
3043deb3ec6SMatthias Ringwald     l2cap_reserve_packet_buffer();
3053deb3ec6SMatthias Ringwald     uint8_t * request = l2cap_get_outgoing_buffer();
3063deb3ec6SMatthias Ringwald     request[0] = request_type;
307f8fbdce0SMatthias Ringwald     little_endian_store_16(request, 1, attribute_handle);
3083deb3ec6SMatthias Ringwald     memcpy(&request[3], value, value_length);
309f8fbdce0SMatthias Ringwald     little_endian_store_32(request, 3 + value_length, sign_counter);
3109c80e4ccSMatthias Ringwald     reverse_64(sgn, &request[3 + value_length + 4]);
3113deb3ec6SMatthias Ringwald     l2cap_send_prepared_connectionless(peripheral_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, 3 + value_length + 12);
3123deb3ec6SMatthias Ringwald }
3137a766ebfSMatthias Ringwald #endif
3143deb3ec6SMatthias Ringwald 
3153deb3ec6SMatthias Ringwald // precondition: can_send_packet_now == TRUE
3163deb3ec6SMatthias 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){
3173deb3ec6SMatthias Ringwald     l2cap_reserve_packet_buffer();
3183deb3ec6SMatthias Ringwald     uint8_t * request = l2cap_get_outgoing_buffer();
3193deb3ec6SMatthias Ringwald     request[0] = request_type;
320f8fbdce0SMatthias Ringwald     little_endian_store_16(request, 1, attribute_handle);
3213deb3ec6SMatthias Ringwald     memcpy(&request[3], value, value_length);
3223deb3ec6SMatthias Ringwald 
3233deb3ec6SMatthias Ringwald     l2cap_send_prepared_connectionless(peripheral_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, 3 + value_length);
3243deb3ec6SMatthias Ringwald }
3253deb3ec6SMatthias Ringwald 
3263deb3ec6SMatthias Ringwald // precondition: can_send_packet_now == TRUE
3273deb3ec6SMatthias Ringwald static void att_execute_write_request(uint16_t request_type, uint16_t peripheral_handle, uint8_t execute_write){
3283deb3ec6SMatthias Ringwald     l2cap_reserve_packet_buffer();
3293deb3ec6SMatthias Ringwald     uint8_t * request = l2cap_get_outgoing_buffer();
3303deb3ec6SMatthias Ringwald     request[0] = request_type;
3313deb3ec6SMatthias Ringwald     request[1] = execute_write;
3323deb3ec6SMatthias Ringwald     l2cap_send_prepared_connectionless(peripheral_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, 2);
3333deb3ec6SMatthias Ringwald }
3343deb3ec6SMatthias Ringwald 
3353deb3ec6SMatthias Ringwald // precondition: can_send_packet_now == TRUE
3363deb3ec6SMatthias 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){
3373deb3ec6SMatthias Ringwald     l2cap_reserve_packet_buffer();
3383deb3ec6SMatthias Ringwald     uint8_t * request = l2cap_get_outgoing_buffer();
3393deb3ec6SMatthias Ringwald     request[0] = request_type;
340f8fbdce0SMatthias Ringwald     little_endian_store_16(request, 1, attribute_handle);
341f8fbdce0SMatthias Ringwald     little_endian_store_16(request, 3, value_offset);
3423deb3ec6SMatthias Ringwald     memcpy(&request[5], &value[value_offset], blob_length);
3433deb3ec6SMatthias Ringwald 
3443deb3ec6SMatthias Ringwald     l2cap_send_prepared_connectionless(peripheral_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, 5+blob_length);
3453deb3ec6SMatthias Ringwald }
3463deb3ec6SMatthias Ringwald 
3473deb3ec6SMatthias Ringwald static void att_exchange_mtu_request(uint16_t peripheral_handle){
3483deb3ec6SMatthias Ringwald     uint16_t mtu = l2cap_max_le_mtu();
3493deb3ec6SMatthias Ringwald     l2cap_reserve_packet_buffer();
3503deb3ec6SMatthias Ringwald     uint8_t * request = l2cap_get_outgoing_buffer();
3513deb3ec6SMatthias Ringwald     request[0] = ATT_EXCHANGE_MTU_REQUEST;
352f8fbdce0SMatthias Ringwald     little_endian_store_16(request, 1, mtu);
3533deb3ec6SMatthias Ringwald     l2cap_send_prepared_connectionless(peripheral_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, 3);
3543deb3ec6SMatthias Ringwald }
3553deb3ec6SMatthias Ringwald 
3563deb3ec6SMatthias Ringwald static uint16_t write_blob_length(gatt_client_t * peripheral){
3573deb3ec6SMatthias Ringwald     uint16_t max_blob_length = peripheral_mtu(peripheral) - 5;
3583deb3ec6SMatthias Ringwald     if (peripheral->attribute_offset >= peripheral->attribute_length) {
3593deb3ec6SMatthias Ringwald         return 0;
3603deb3ec6SMatthias Ringwald     }
3613deb3ec6SMatthias Ringwald     uint16_t rest_length = peripheral->attribute_length - peripheral->attribute_offset;
3623deb3ec6SMatthias Ringwald     if (max_blob_length > rest_length){
3633deb3ec6SMatthias Ringwald         return rest_length;
3643deb3ec6SMatthias Ringwald     }
3653deb3ec6SMatthias Ringwald     return max_blob_length;
3663deb3ec6SMatthias Ringwald }
3673deb3ec6SMatthias Ringwald 
3683deb3ec6SMatthias Ringwald static void send_gatt_services_request(gatt_client_t *peripheral){
369fc64f94aSMatthias 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);
3703deb3ec6SMatthias Ringwald }
3713deb3ec6SMatthias Ringwald 
3723deb3ec6SMatthias Ringwald static void send_gatt_by_uuid_request(gatt_client_t *peripheral, uint16_t attribute_group_type){
3733deb3ec6SMatthias Ringwald     if (peripheral->uuid16){
3743deb3ec6SMatthias Ringwald         uint8_t uuid16[2];
375f8fbdce0SMatthias Ringwald         little_endian_store_16(uuid16, 0, peripheral->uuid16);
376fc64f94aSMatthias 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);
3773deb3ec6SMatthias Ringwald         return;
3783deb3ec6SMatthias Ringwald     }
3793deb3ec6SMatthias Ringwald     uint8_t uuid128[16];
3809c80e4ccSMatthias Ringwald     reverse_128(peripheral->uuid128, uuid128);
381fc64f94aSMatthias 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);
3823deb3ec6SMatthias Ringwald }
3833deb3ec6SMatthias Ringwald 
3843deb3ec6SMatthias Ringwald static void send_gatt_services_by_uuid_request(gatt_client_t *peripheral){
3853deb3ec6SMatthias Ringwald     send_gatt_by_uuid_request(peripheral, GATT_PRIMARY_SERVICE_UUID);
3863deb3ec6SMatthias Ringwald }
3873deb3ec6SMatthias Ringwald 
3883deb3ec6SMatthias Ringwald static void send_gatt_included_service_uuid_request(gatt_client_t *peripheral){
389fc64f94aSMatthias Ringwald     att_read_request(ATT_READ_REQUEST, peripheral->con_handle, peripheral->query_start_handle);
3903deb3ec6SMatthias Ringwald }
3913deb3ec6SMatthias Ringwald 
3923deb3ec6SMatthias Ringwald static void send_gatt_included_service_request(gatt_client_t *peripheral){
393fc64f94aSMatthias 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);
3943deb3ec6SMatthias Ringwald }
3953deb3ec6SMatthias Ringwald 
3963deb3ec6SMatthias Ringwald static void send_gatt_characteristic_request(gatt_client_t *peripheral){
397fc64f94aSMatthias 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);
3983deb3ec6SMatthias Ringwald }
3993deb3ec6SMatthias Ringwald 
4003deb3ec6SMatthias Ringwald static void send_gatt_characteristic_descriptor_request(gatt_client_t *peripheral){
401fc64f94aSMatthias Ringwald     att_find_information_request(ATT_FIND_INFORMATION_REQUEST, peripheral->con_handle, peripheral->start_group_handle, peripheral->end_group_handle);
4023deb3ec6SMatthias Ringwald }
4033deb3ec6SMatthias Ringwald 
4043deb3ec6SMatthias Ringwald static void send_gatt_read_characteristic_value_request(gatt_client_t *peripheral){
405fc64f94aSMatthias Ringwald     att_read_request(ATT_READ_REQUEST, peripheral->con_handle, peripheral->attribute_handle);
4063deb3ec6SMatthias Ringwald }
4073deb3ec6SMatthias Ringwald 
4083deb3ec6SMatthias Ringwald static void send_gatt_read_by_type_request(gatt_client_t * peripheral){
4093deb3ec6SMatthias Ringwald     if (peripheral->uuid16){
410fc64f94aSMatthias 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);
4113deb3ec6SMatthias Ringwald     } else {
412fc64f94aSMatthias 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);
4133deb3ec6SMatthias Ringwald     }
4143deb3ec6SMatthias Ringwald }
4153deb3ec6SMatthias Ringwald 
4163deb3ec6SMatthias Ringwald static void send_gatt_read_blob_request(gatt_client_t *peripheral){
417fc64f94aSMatthias Ringwald     att_read_blob_request(ATT_READ_BLOB_REQUEST, peripheral->con_handle, peripheral->attribute_handle, peripheral->attribute_offset);
4183deb3ec6SMatthias Ringwald }
4193deb3ec6SMatthias Ringwald 
4203deb3ec6SMatthias Ringwald static void send_gatt_read_multiple_request(gatt_client_t * peripheral){
421fc64f94aSMatthias Ringwald     att_read_multiple_request(peripheral->con_handle, peripheral->read_multiple_handle_count, peripheral->read_multiple_handles);
4223deb3ec6SMatthias Ringwald }
4233deb3ec6SMatthias Ringwald 
4243deb3ec6SMatthias Ringwald static void send_gatt_write_attribute_value_request(gatt_client_t * peripheral){
425fc64f94aSMatthias Ringwald     att_write_request(ATT_WRITE_REQUEST, peripheral->con_handle, peripheral->attribute_handle, peripheral->attribute_length, peripheral->attribute_value);
4263deb3ec6SMatthias Ringwald }
4273deb3ec6SMatthias Ringwald 
4283deb3ec6SMatthias Ringwald static void send_gatt_write_client_characteristic_configuration_request(gatt_client_t * peripheral){
429fc64f94aSMatthias Ringwald     att_write_request(ATT_WRITE_REQUEST, peripheral->con_handle, peripheral->client_characteristic_configuration_handle, 2, peripheral->client_characteristic_configuration_value);
4303deb3ec6SMatthias Ringwald }
4313deb3ec6SMatthias Ringwald 
4323deb3ec6SMatthias Ringwald static void send_gatt_prepare_write_request(gatt_client_t * peripheral){
433fc64f94aSMatthias 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);
4343deb3ec6SMatthias Ringwald }
4353deb3ec6SMatthias Ringwald 
4363deb3ec6SMatthias Ringwald static void send_gatt_execute_write_request(gatt_client_t * peripheral){
437fc64f94aSMatthias Ringwald     att_execute_write_request(ATT_EXECUTE_WRITE_REQUEST, peripheral->con_handle, 1);
4383deb3ec6SMatthias Ringwald }
4393deb3ec6SMatthias Ringwald 
4403deb3ec6SMatthias Ringwald static void send_gatt_cancel_prepared_write_request(gatt_client_t * peripheral){
441fc64f94aSMatthias Ringwald     att_execute_write_request(ATT_EXECUTE_WRITE_REQUEST, peripheral->con_handle, 0);
4423deb3ec6SMatthias Ringwald }
4433deb3ec6SMatthias Ringwald 
444abdc9fb5SMatthias Ringwald #ifndef ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERY
4453deb3ec6SMatthias Ringwald static void send_gatt_read_client_characteristic_configuration_request(gatt_client_t * peripheral){
446fc64f94aSMatthias 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);
4473deb3ec6SMatthias Ringwald }
448abdc9fb5SMatthias Ringwald #endif
4493deb3ec6SMatthias Ringwald 
4503deb3ec6SMatthias Ringwald static void send_gatt_read_characteristic_descriptor_request(gatt_client_t * peripheral){
451fc64f94aSMatthias Ringwald     att_read_request(ATT_READ_REQUEST, peripheral->con_handle, peripheral->attribute_handle);
4523deb3ec6SMatthias Ringwald }
4533deb3ec6SMatthias Ringwald 
4547a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
4553deb3ec6SMatthias Ringwald static void send_gatt_signed_write_request(gatt_client_t * peripheral, uint32_t sign_counter){
456fc64f94aSMatthias 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);
4573deb3ec6SMatthias Ringwald }
4587a766ebfSMatthias Ringwald #endif
4593deb3ec6SMatthias Ringwald 
4603deb3ec6SMatthias Ringwald static uint16_t get_last_result_handle_from_service_list(uint8_t * packet, uint16_t size){
4613deb3ec6SMatthias Ringwald     uint8_t attr_length = packet[1];
462f8fbdce0SMatthias Ringwald     return little_endian_read_16(packet, size - attr_length + 2);
4633deb3ec6SMatthias Ringwald }
4643deb3ec6SMatthias Ringwald 
4653deb3ec6SMatthias Ringwald static uint16_t get_last_result_handle_from_characteristics_list(uint8_t * packet, uint16_t size){
4663deb3ec6SMatthias Ringwald     uint8_t attr_length = packet[1];
467f8fbdce0SMatthias Ringwald     return little_endian_read_16(packet, size - attr_length + 3);
4683deb3ec6SMatthias Ringwald }
4693deb3ec6SMatthias Ringwald 
4703deb3ec6SMatthias Ringwald static uint16_t get_last_result_handle_from_included_services_list(uint8_t * packet, uint16_t size){
4713deb3ec6SMatthias Ringwald     uint8_t attr_length = packet[1];
472f8fbdce0SMatthias Ringwald     return little_endian_read_16(packet, size - attr_length);
4733deb3ec6SMatthias Ringwald }
4743deb3ec6SMatthias Ringwald 
4753deb3ec6SMatthias Ringwald static void gatt_client_handle_transaction_complete(gatt_client_t * peripheral){
4763deb3ec6SMatthias Ringwald     peripheral->gatt_client_state = P_READY;
4773deb3ec6SMatthias Ringwald     gatt_client_timeout_stop(peripheral);
4783deb3ec6SMatthias Ringwald }
4793deb3ec6SMatthias Ringwald 
4809c662c9bSMatthias Ringwald static void emit_event_new(btstack_packet_handler_t callback, uint8_t * packet, uint16_t size){
4819c662c9bSMatthias Ringwald     if (!callback) return;
482b6e003bcSMatthias Ringwald     hci_dump_packet(HCI_EVENT_PACKET, 0, packet, size);
4839c662c9bSMatthias Ringwald     (*callback)(HCI_EVENT_PACKET, 0, packet, size);
4843deb3ec6SMatthias Ringwald }
4853deb3ec6SMatthias Ringwald 
48686c38559SMatthias 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){
48786c38559SMatthias Ringwald     notification->callback = packet_handler;
4889c662c9bSMatthias Ringwald     notification->con_handle = con_handle;
4899c662c9bSMatthias Ringwald     notification->attribute_handle = characteristic->value_handle;
4909c662c9bSMatthias Ringwald     btstack_linked_list_add(&gatt_client_value_listeners, (btstack_linked_item_t*) notification);
4919c662c9bSMatthias Ringwald }
4929c662c9bSMatthias Ringwald 
4931f734b5fSMatthias Ringwald void gatt_client_stop_listening_for_characteristic_value_updates(gatt_client_notification_t * notification){
4941f734b5fSMatthias Ringwald     btstack_linked_list_remove(&gatt_client_value_listeners, (btstack_linked_item_t*) notification);
4951f734b5fSMatthias Ringwald }
4961f734b5fSMatthias Ringwald 
497711e6c80SMatthias Ringwald static void emit_event_to_registered_listeners(hci_con_handle_t con_handle, uint16_t attribute_handle, uint8_t * packet, uint16_t size){
498665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
4999c662c9bSMatthias Ringwald     btstack_linked_list_iterator_init(&it, &gatt_client_value_listeners);
500665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
50186c38559SMatthias Ringwald         gatt_client_notification_t * notification = (gatt_client_notification_t*) btstack_linked_list_iterator_next(&it);
50286c38559SMatthias Ringwald         if (notification->con_handle != con_handle) continue;
50386c38559SMatthias Ringwald         if (notification->attribute_handle != attribute_handle) continue;
50486c38559SMatthias Ringwald         (*notification->callback)(HCI_EVENT_PACKET, 0, packet, size);
5053deb3ec6SMatthias Ringwald     }
5063deb3ec6SMatthias Ringwald }
5073deb3ec6SMatthias Ringwald 
508*7d2258b3SMilanka Ringwald static void emit_gatt_complete_event(gatt_client_t * peripheral, uint8_t att_status){
5093deb3ec6SMatthias Ringwald     // @format H1
5103deb3ec6SMatthias Ringwald     uint8_t packet[5];
5115611a760SMatthias Ringwald     packet[0] = GATT_EVENT_QUERY_COMPLETE;
5123deb3ec6SMatthias Ringwald     packet[1] = 3;
513fc64f94aSMatthias Ringwald     little_endian_store_16(packet, 2, peripheral->con_handle);
514*7d2258b3SMilanka Ringwald     packet[4] = att_status;
5159c662c9bSMatthias Ringwald     emit_event_new(peripheral->callback, packet, sizeof(packet));
5163deb3ec6SMatthias Ringwald }
5173deb3ec6SMatthias Ringwald 
5183deb3ec6SMatthias 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){
5193deb3ec6SMatthias Ringwald     // @format HX
5203deb3ec6SMatthias Ringwald     uint8_t packet[24];
5215611a760SMatthias Ringwald     packet[0] = GATT_EVENT_SERVICE_QUERY_RESULT;
5223deb3ec6SMatthias Ringwald     packet[1] = sizeof(packet) - 2;
523fc64f94aSMatthias Ringwald     little_endian_store_16(packet, 2, peripheral->con_handle);
5243deb3ec6SMatthias Ringwald     ///
525f8fbdce0SMatthias Ringwald     little_endian_store_16(packet, 4, start_group_handle);
526f8fbdce0SMatthias Ringwald     little_endian_store_16(packet, 6, end_group_handle);
5279c80e4ccSMatthias Ringwald     reverse_128(uuid128, &packet[8]);
5289c662c9bSMatthias Ringwald     emit_event_new(peripheral->callback, packet, sizeof(packet));
5293deb3ec6SMatthias Ringwald }
5303deb3ec6SMatthias Ringwald 
5313deb3ec6SMatthias 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){
5323deb3ec6SMatthias Ringwald     // @format HX
5333deb3ec6SMatthias Ringwald     uint8_t packet[26];
5345611a760SMatthias Ringwald     packet[0] = GATT_EVENT_INCLUDED_SERVICE_QUERY_RESULT;
5353deb3ec6SMatthias Ringwald     packet[1] = sizeof(packet) - 2;
536fc64f94aSMatthias Ringwald     little_endian_store_16(packet, 2, peripheral->con_handle);
5373deb3ec6SMatthias Ringwald     ///
538f8fbdce0SMatthias Ringwald     little_endian_store_16(packet, 4, include_handle);
5393deb3ec6SMatthias Ringwald     //
540f8fbdce0SMatthias Ringwald     little_endian_store_16(packet, 6, start_group_handle);
541f8fbdce0SMatthias Ringwald     little_endian_store_16(packet, 8, end_group_handle);
5429c80e4ccSMatthias Ringwald     reverse_128(uuid128, &packet[10]);
5439c662c9bSMatthias Ringwald     emit_event_new(peripheral->callback, packet, sizeof(packet));
5443deb3ec6SMatthias Ringwald }
5453deb3ec6SMatthias Ringwald 
5463deb3ec6SMatthias 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,
5473deb3ec6SMatthias Ringwald         uint16_t properties, uint8_t * uuid128){
5483deb3ec6SMatthias Ringwald     // @format HY
5493deb3ec6SMatthias Ringwald     uint8_t packet[28];
5505611a760SMatthias Ringwald     packet[0] = GATT_EVENT_CHARACTERISTIC_QUERY_RESULT;
5513deb3ec6SMatthias Ringwald     packet[1] = sizeof(packet) - 2;
552fc64f94aSMatthias Ringwald     little_endian_store_16(packet, 2, peripheral->con_handle);
5533deb3ec6SMatthias Ringwald     ///
554f8fbdce0SMatthias Ringwald     little_endian_store_16(packet, 4,  start_handle);
555f8fbdce0SMatthias Ringwald     little_endian_store_16(packet, 6,  value_handle);
556f8fbdce0SMatthias Ringwald     little_endian_store_16(packet, 8,  end_handle);
557f8fbdce0SMatthias Ringwald     little_endian_store_16(packet, 10, properties);
5589c80e4ccSMatthias Ringwald     reverse_128(uuid128, &packet[12]);
5599c662c9bSMatthias Ringwald     emit_event_new(peripheral->callback, packet, sizeof(packet));
5603deb3ec6SMatthias Ringwald }
5613deb3ec6SMatthias Ringwald 
5623deb3ec6SMatthias Ringwald static void emit_gatt_all_characteristic_descriptors_result_event(
5633deb3ec6SMatthias Ringwald     gatt_client_t * peripheral, uint16_t descriptor_handle, uint8_t * uuid128){
5643deb3ec6SMatthias Ringwald     // @format HZ
5653deb3ec6SMatthias Ringwald     uint8_t packet[22];
5665611a760SMatthias Ringwald     packet[0] = GATT_EVENT_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY_RESULT;
5673deb3ec6SMatthias Ringwald     packet[1] = sizeof(packet) - 2;
568fc64f94aSMatthias Ringwald     little_endian_store_16(packet, 2, peripheral->con_handle);
5693deb3ec6SMatthias Ringwald     ///
570f8fbdce0SMatthias Ringwald     little_endian_store_16(packet, 4,  descriptor_handle);
5719c80e4ccSMatthias Ringwald     reverse_128(uuid128, &packet[6]);
5729c662c9bSMatthias Ringwald     emit_event_new(peripheral->callback, packet, sizeof(packet));
5733deb3ec6SMatthias Ringwald }
5743deb3ec6SMatthias Ringwald 
5758f37572aSJakob Krantz static void emit_gatt_mtu_exchanged_result_event(gatt_client_t * peripheral, uint16_t new_mtu){
5768f37572aSJakob Krantz     // @format H2
5778f37572aSJakob Krantz     uint8_t packet[6];
5788f37572aSJakob Krantz     packet[0] = GATT_EVENT_MTU;
5798f37572aSJakob Krantz     packet[1] = sizeof(packet) - 2;
5808f37572aSJakob Krantz     little_endian_store_16(packet, 2, peripheral->con_handle);
5818f37572aSJakob Krantz     little_endian_store_16(packet, 4, new_mtu);
582b12646c5SJakob Krantz     att_dispatch_client_mtu_exchanged(peripheral->con_handle, new_mtu);
5838f37572aSJakob Krantz     emit_event_new(peripheral->callback, packet, sizeof(packet));
5848f37572aSJakob Krantz }
5858f37572aSJakob Krantz ///
5863deb3ec6SMatthias Ringwald static void report_gatt_services(gatt_client_t * peripheral, uint8_t * packet,  uint16_t size){
5873deb3ec6SMatthias Ringwald     uint8_t attr_length = packet[1];
5883deb3ec6SMatthias Ringwald     uint8_t uuid_length = attr_length - 4;
5893deb3ec6SMatthias Ringwald 
5903deb3ec6SMatthias Ringwald     int i;
5913deb3ec6SMatthias Ringwald     for (i = 2; i < size; i += attr_length){
592f8fbdce0SMatthias Ringwald         uint16_t start_group_handle = little_endian_read_16(packet,i);
593f8fbdce0SMatthias Ringwald         uint16_t end_group_handle   = little_endian_read_16(packet,i+2);
5943deb3ec6SMatthias Ringwald         uint8_t  uuid128[16];
5953deb3ec6SMatthias Ringwald         uint16_t uuid16 = 0;
5963deb3ec6SMatthias Ringwald 
5973deb3ec6SMatthias Ringwald         if (uuid_length == 2){
598f8fbdce0SMatthias Ringwald             uuid16 = little_endian_read_16(packet, i+4);
599e1a125dfSMatthias Ringwald             uuid_add_bluetooth_prefix((uint8_t*) &uuid128, uuid16);
6003deb3ec6SMatthias Ringwald         } else {
6019c80e4ccSMatthias Ringwald             reverse_128(&packet[i+4], uuid128);
6023deb3ec6SMatthias Ringwald         }
6033deb3ec6SMatthias Ringwald         emit_gatt_service_query_result_event(peripheral, start_group_handle, end_group_handle, uuid128);
6043deb3ec6SMatthias Ringwald     }
605fc64f94aSMatthias Ringwald     // log_info("report_gatt_services for %02X done", peripheral->con_handle);
6063deb3ec6SMatthias Ringwald }
6073deb3ec6SMatthias Ringwald 
6083deb3ec6SMatthias Ringwald // helper
6093deb3ec6SMatthias 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){
6103deb3ec6SMatthias Ringwald     uint8_t uuid128[16];
6113deb3ec6SMatthias Ringwald     uint16_t uuid16 = 0;
6123deb3ec6SMatthias Ringwald     if (uuid_length == 2){
613f8fbdce0SMatthias Ringwald         uuid16 = little_endian_read_16(uuid, 0);
614e1a125dfSMatthias Ringwald         uuid_add_bluetooth_prefix((uint8_t*) uuid128, uuid16);
6153deb3ec6SMatthias Ringwald     } else {
6169c80e4ccSMatthias Ringwald         reverse_128(uuid, uuid128);
6173deb3ec6SMatthias Ringwald     }
6183deb3ec6SMatthias Ringwald 
6193deb3ec6SMatthias Ringwald     if (peripheral->filter_with_uuid && memcmp(peripheral->uuid128, uuid128, 16) != 0) return;
6203deb3ec6SMatthias Ringwald 
6213deb3ec6SMatthias Ringwald     peripheral->characteristic_properties = properties;
6223deb3ec6SMatthias Ringwald     peripheral->characteristic_start_handle = start_handle;
6233deb3ec6SMatthias Ringwald     peripheral->attribute_handle = value_handle;
6243deb3ec6SMatthias Ringwald 
6253deb3ec6SMatthias Ringwald     if (peripheral->filter_with_uuid) return;
6263deb3ec6SMatthias Ringwald 
6273deb3ec6SMatthias Ringwald     peripheral->uuid16 = uuid16;
6283deb3ec6SMatthias Ringwald     memcpy(peripheral->uuid128, uuid128, 16);
6293deb3ec6SMatthias Ringwald }
6303deb3ec6SMatthias Ringwald 
6313deb3ec6SMatthias Ringwald static void characteristic_end_found(gatt_client_t * peripheral, uint16_t end_handle){
6323deb3ec6SMatthias Ringwald     // TODO: stop searching if filter and uuid found
6333deb3ec6SMatthias Ringwald 
6343deb3ec6SMatthias Ringwald     if (!peripheral->characteristic_start_handle) return;
6353deb3ec6SMatthias Ringwald 
6363deb3ec6SMatthias Ringwald     emit_gatt_characteristic_query_result_event(peripheral, peripheral->characteristic_start_handle, peripheral->attribute_handle,
6373deb3ec6SMatthias Ringwald         end_handle, peripheral->characteristic_properties, peripheral->uuid128);
6383deb3ec6SMatthias Ringwald 
6393deb3ec6SMatthias Ringwald     peripheral->characteristic_start_handle = 0;
6403deb3ec6SMatthias Ringwald }
6413deb3ec6SMatthias Ringwald 
6423deb3ec6SMatthias Ringwald static void report_gatt_characteristics(gatt_client_t * peripheral, uint8_t * packet,  uint16_t size){
6433deb3ec6SMatthias Ringwald     uint8_t attr_length = packet[1];
6443deb3ec6SMatthias Ringwald     uint8_t uuid_length = attr_length - 5;
6453deb3ec6SMatthias Ringwald     int i;
6463deb3ec6SMatthias Ringwald     for (i = 2; i < size; i += attr_length){
647f8fbdce0SMatthias Ringwald         uint16_t start_handle = little_endian_read_16(packet, i);
6483deb3ec6SMatthias Ringwald         uint8_t  properties = packet[i+2];
649f8fbdce0SMatthias Ringwald         uint16_t value_handle = little_endian_read_16(packet, i+3);
6503deb3ec6SMatthias Ringwald         characteristic_end_found(peripheral, start_handle-1);
6513deb3ec6SMatthias Ringwald         characteristic_start_found(peripheral, start_handle, properties, value_handle, &packet[i+5], uuid_length);
6523deb3ec6SMatthias Ringwald     }
6533deb3ec6SMatthias Ringwald }
6543deb3ec6SMatthias Ringwald 
6553deb3ec6SMatthias Ringwald static void report_gatt_included_service_uuid16(gatt_client_t * peripheral, uint16_t include_handle, uint16_t uuid16){
6563deb3ec6SMatthias Ringwald     uint8_t normalized_uuid128[16];
657e1a125dfSMatthias Ringwald     uuid_add_bluetooth_prefix(normalized_uuid128, uuid16);
6583deb3ec6SMatthias Ringwald     emit_gatt_included_service_query_result_event(peripheral, include_handle, peripheral->query_start_handle,
6593deb3ec6SMatthias Ringwald         peripheral->query_end_handle, normalized_uuid128);
6603deb3ec6SMatthias Ringwald }
6613deb3ec6SMatthias Ringwald 
6623deb3ec6SMatthias Ringwald static void report_gatt_included_service_uuid128(gatt_client_t * peripheral, uint16_t include_handle, uint8_t *uuid128){
6633deb3ec6SMatthias Ringwald     emit_gatt_included_service_query_result_event(peripheral, include_handle, peripheral->query_start_handle,
6643deb3ec6SMatthias Ringwald         peripheral->query_end_handle, uuid128);
6653deb3ec6SMatthias Ringwald }
6663deb3ec6SMatthias Ringwald 
6673deb3ec6SMatthias Ringwald // @returns packet pointer
6683deb3ec6SMatthias Ringwald // @note assume that value is part of an l2cap buffer - overwrite HCI + L2CAP packet headers
6693deb3ec6SMatthias Ringwald static const int characteristic_value_event_header_size = 8;
670711e6c80SMatthias 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){
6713deb3ec6SMatthias Ringwald     // before the value inside the ATT PDU
6723deb3ec6SMatthias Ringwald     uint8_t * packet = value - characteristic_value_event_header_size;
6733deb3ec6SMatthias Ringwald     packet[0] = type;
6743deb3ec6SMatthias Ringwald     packet[1] = characteristic_value_event_header_size - 2 + length;
675f8fbdce0SMatthias Ringwald     little_endian_store_16(packet, 2, con_handle);
676f8fbdce0SMatthias Ringwald     little_endian_store_16(packet, 4, attribute_handle);
677f8fbdce0SMatthias Ringwald     little_endian_store_16(packet, 6, length);
6783deb3ec6SMatthias Ringwald     return packet;
6793deb3ec6SMatthias Ringwald }
6803deb3ec6SMatthias Ringwald 
6813deb3ec6SMatthias Ringwald // @returns packet pointer
6823deb3ec6SMatthias Ringwald // @note assume that value is part of an l2cap buffer - overwrite parts of the HCI/L2CAP/ATT packet (4/4/3) bytes
6833deb3ec6SMatthias Ringwald static const int long_characteristic_value_event_header_size = 10;
684711e6c80SMatthias 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){
6853deb3ec6SMatthias Ringwald #if defined(HCI_INCOMING_PRE_BUFFER_SIZE) && (HCI_INCOMING_PRE_BUFFER_SIZE >= 10 - 8) // L2CAP Header (4) - ACL Header (4)
6863deb3ec6SMatthias Ringwald     // before the value inside the ATT PDU
6873deb3ec6SMatthias Ringwald     uint8_t * packet = value - long_characteristic_value_event_header_size;
6883deb3ec6SMatthias Ringwald     packet[0] = type;
6893deb3ec6SMatthias Ringwald     packet[1] = long_characteristic_value_event_header_size - 2 + length;
690f8fbdce0SMatthias Ringwald     little_endian_store_16(packet, 2, con_handle);
691f8fbdce0SMatthias Ringwald     little_endian_store_16(packet, 4, attribute_handle);
692f8fbdce0SMatthias Ringwald     little_endian_store_16(packet, 6, offset);
693f8fbdce0SMatthias Ringwald     little_endian_store_16(packet, 8, length);
6943deb3ec6SMatthias Ringwald     return packet;
6953deb3ec6SMatthias Ringwald #else
6963deb3ec6SMatthias Ringwald     log_error("HCI_INCOMING_PRE_BUFFER_SIZE >= 2 required for long characteristic reads");
6973deb3ec6SMatthias Ringwald     return NULL;
6983deb3ec6SMatthias Ringwald #endif
6993deb3ec6SMatthias Ringwald }
7003deb3ec6SMatthias Ringwald 
7013deb3ec6SMatthias Ringwald 
7023deb3ec6SMatthias Ringwald // @note assume that value is part of an l2cap buffer - overwrite parts of the HCI/L2CAP/ATT packet (4/4/3) bytes
703711e6c80SMatthias Ringwald static void report_gatt_notification(hci_con_handle_t con_handle, uint16_t value_handle, uint8_t * value, int length){
7045611a760SMatthias Ringwald     uint8_t * packet = setup_characteristic_value_packet(GATT_EVENT_NOTIFICATION, con_handle, value_handle, value, length);
7059c662c9bSMatthias Ringwald     emit_event_to_registered_listeners(con_handle, value_handle, packet, characteristic_value_event_header_size + length);
7063deb3ec6SMatthias Ringwald }
7073deb3ec6SMatthias Ringwald 
7083deb3ec6SMatthias Ringwald // @note assume that value is part of an l2cap buffer - overwrite parts of the HCI/L2CAP/ATT packet (4/4/3) bytes
709711e6c80SMatthias Ringwald static void report_gatt_indication(hci_con_handle_t con_handle, uint16_t value_handle, uint8_t * value, int length){
7105611a760SMatthias Ringwald     uint8_t * packet = setup_characteristic_value_packet(GATT_EVENT_INDICATION, con_handle, value_handle, value, length);
7119c662c9bSMatthias Ringwald     emit_event_to_registered_listeners(con_handle, value_handle, packet, characteristic_value_event_header_size + length);
7123deb3ec6SMatthias Ringwald }
7133deb3ec6SMatthias Ringwald 
7143deb3ec6SMatthias Ringwald // @note assume that value is part of an l2cap buffer - overwrite parts of the HCI/L2CAP/ATT packet (4/4/3) bytes
7153deb3ec6SMatthias Ringwald static void report_gatt_characteristic_value(gatt_client_t * peripheral, uint16_t attribute_handle, uint8_t * value, uint16_t length){
716fc64f94aSMatthias Ringwald     uint8_t * packet = setup_characteristic_value_packet(GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT, peripheral->con_handle, attribute_handle, value, length);
7179c662c9bSMatthias Ringwald     emit_event_new(peripheral->callback, packet, characteristic_value_event_header_size + length);
7183deb3ec6SMatthias Ringwald }
7193deb3ec6SMatthias Ringwald 
7203deb3ec6SMatthias Ringwald // @note assume that value is part of an l2cap buffer - overwrite parts of the HCI/L2CAP/ATT packet (4/4/3) bytes
7213deb3ec6SMatthias 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){
722fc64f94aSMatthias 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);
7233deb3ec6SMatthias Ringwald     if (!packet) return;
7249c662c9bSMatthias Ringwald     emit_event_new(peripheral->callback, packet, blob_length + long_characteristic_value_event_header_size);
7253deb3ec6SMatthias Ringwald }
7263deb3ec6SMatthias Ringwald 
7273deb3ec6SMatthias 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){
7289ec2630cSMatthias Ringwald     UNUSED(value_offset);
729fc64f94aSMatthias Ringwald     uint8_t * packet = setup_characteristic_value_packet(GATT_EVENT_CHARACTERISTIC_DESCRIPTOR_QUERY_RESULT, peripheral->con_handle, descriptor_handle, value, value_length);
7309c662c9bSMatthias Ringwald     emit_event_new(peripheral->callback, packet, value_length + 8);
7313deb3ec6SMatthias Ringwald }
7323deb3ec6SMatthias Ringwald 
7333deb3ec6SMatthias 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){
734fc64f94aSMatthias 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);
7353deb3ec6SMatthias Ringwald     if (!packet) return;
7369c662c9bSMatthias Ringwald     emit_event_new(peripheral->callback, packet, blob_length + long_characteristic_value_event_header_size);
7373deb3ec6SMatthias Ringwald }
7383deb3ec6SMatthias Ringwald 
7393deb3ec6SMatthias Ringwald static void report_gatt_all_characteristic_descriptors(gatt_client_t * peripheral, uint8_t * packet, uint16_t size, uint16_t pair_size){
7403deb3ec6SMatthias Ringwald     int i;
7413deb3ec6SMatthias Ringwald     for (i = 0; i<size; i+=pair_size){
742f8fbdce0SMatthias Ringwald         uint16_t descriptor_handle = little_endian_read_16(packet,i);
7433deb3ec6SMatthias Ringwald         uint8_t uuid128[16];
7443deb3ec6SMatthias Ringwald         uint16_t uuid16 = 0;
7453deb3ec6SMatthias Ringwald         if (pair_size == 4){
746f8fbdce0SMatthias Ringwald             uuid16 = little_endian_read_16(packet,i+2);
747e1a125dfSMatthias Ringwald             uuid_add_bluetooth_prefix(uuid128, uuid16);
7483deb3ec6SMatthias Ringwald         } else {
7499c80e4ccSMatthias Ringwald             reverse_128(&packet[i+2], uuid128);
7503deb3ec6SMatthias Ringwald         }
7513deb3ec6SMatthias Ringwald         emit_gatt_all_characteristic_descriptors_result_event(peripheral, descriptor_handle, uuid128);
7523deb3ec6SMatthias Ringwald     }
7533deb3ec6SMatthias Ringwald 
7543deb3ec6SMatthias Ringwald }
7553deb3ec6SMatthias Ringwald 
7563deb3ec6SMatthias Ringwald static int is_query_done(gatt_client_t * peripheral, uint16_t last_result_handle){
7573deb3ec6SMatthias Ringwald     return last_result_handle >= peripheral->end_group_handle;
7583deb3ec6SMatthias Ringwald }
7593deb3ec6SMatthias Ringwald 
7603deb3ec6SMatthias Ringwald static void trigger_next_query(gatt_client_t * peripheral, uint16_t last_result_handle, gatt_client_state_t next_query_state){
7613deb3ec6SMatthias Ringwald     if (is_query_done(peripheral, last_result_handle)){
7623deb3ec6SMatthias Ringwald         gatt_client_handle_transaction_complete(peripheral);
7639cb80b17SMilanka Ringwald         emit_gatt_complete_event(peripheral, ATT_ERROR_SUCCESS);
7643deb3ec6SMatthias Ringwald         return;
7653deb3ec6SMatthias Ringwald     }
7663deb3ec6SMatthias Ringwald     // next
7673deb3ec6SMatthias Ringwald     peripheral->start_group_handle = last_result_handle + 1;
7683deb3ec6SMatthias Ringwald     peripheral->gatt_client_state = next_query_state;
7693deb3ec6SMatthias Ringwald }
7703deb3ec6SMatthias Ringwald 
77100748105SMatthias Ringwald static void trigger_next_included_service_query(gatt_client_t * peripheral, uint16_t last_result_handle){
7723deb3ec6SMatthias Ringwald     trigger_next_query(peripheral, last_result_handle, P_W2_SEND_INCLUDED_SERVICE_QUERY);
7733deb3ec6SMatthias Ringwald }
7743deb3ec6SMatthias Ringwald 
77500748105SMatthias Ringwald static void trigger_next_service_query(gatt_client_t * peripheral, uint16_t last_result_handle){
7763deb3ec6SMatthias Ringwald     trigger_next_query(peripheral, last_result_handle, P_W2_SEND_SERVICE_QUERY);
7773deb3ec6SMatthias Ringwald }
7783deb3ec6SMatthias Ringwald 
77900748105SMatthias Ringwald static void trigger_next_service_by_uuid_query(gatt_client_t * peripheral, uint16_t last_result_handle){
7803deb3ec6SMatthias Ringwald     trigger_next_query(peripheral, last_result_handle, P_W2_SEND_SERVICE_WITH_UUID_QUERY);
7813deb3ec6SMatthias Ringwald }
7823deb3ec6SMatthias Ringwald 
78300748105SMatthias Ringwald static void trigger_next_characteristic_query(gatt_client_t * peripheral, uint16_t last_result_handle){
7843deb3ec6SMatthias Ringwald     if (is_query_done(peripheral, last_result_handle)){
7853deb3ec6SMatthias Ringwald         // report last characteristic
7863deb3ec6SMatthias Ringwald         characteristic_end_found(peripheral, peripheral->end_group_handle);
7873deb3ec6SMatthias Ringwald     }
7883deb3ec6SMatthias Ringwald     trigger_next_query(peripheral, last_result_handle, P_W2_SEND_ALL_CHARACTERISTICS_OF_SERVICE_QUERY);
7893deb3ec6SMatthias Ringwald }
7903deb3ec6SMatthias Ringwald 
79100748105SMatthias Ringwald static void trigger_next_characteristic_descriptor_query(gatt_client_t * peripheral, uint16_t last_result_handle){
7923deb3ec6SMatthias Ringwald     trigger_next_query(peripheral, last_result_handle, P_W2_SEND_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY);
7933deb3ec6SMatthias Ringwald }
7943deb3ec6SMatthias Ringwald 
79500748105SMatthias Ringwald static void trigger_next_read_by_type_query(gatt_client_t * peripheral, uint16_t last_result_handle){
7963deb3ec6SMatthias Ringwald     trigger_next_query(peripheral, last_result_handle, P_W2_SEND_READ_BY_TYPE_REQUEST);
7973deb3ec6SMatthias Ringwald }
7983deb3ec6SMatthias Ringwald 
79900748105SMatthias 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){
8003deb3ec6SMatthias Ringwald     peripheral->attribute_offset += write_blob_length(peripheral);
8013deb3ec6SMatthias Ringwald     uint16_t next_blob_length =  write_blob_length(peripheral);
8023deb3ec6SMatthias Ringwald 
8033deb3ec6SMatthias Ringwald     if (next_blob_length == 0){
8043deb3ec6SMatthias Ringwald         peripheral->gatt_client_state = done_state;
8053deb3ec6SMatthias Ringwald         return;
8063deb3ec6SMatthias Ringwald     }
8073deb3ec6SMatthias Ringwald     peripheral->gatt_client_state = next_query_state;
8083deb3ec6SMatthias Ringwald }
8093deb3ec6SMatthias Ringwald 
81000748105SMatthias Ringwald static void trigger_next_blob_query(gatt_client_t * peripheral, gatt_client_state_t next_query_state, uint16_t received_blob_length){
8113deb3ec6SMatthias Ringwald 
8123deb3ec6SMatthias Ringwald     uint16_t max_blob_length = peripheral_mtu(peripheral) - 1;
8133deb3ec6SMatthias Ringwald     if (received_blob_length < max_blob_length){
8143deb3ec6SMatthias Ringwald         gatt_client_handle_transaction_complete(peripheral);
8159cb80b17SMilanka Ringwald         emit_gatt_complete_event(peripheral, ATT_ERROR_SUCCESS);
8163deb3ec6SMatthias Ringwald         return;
8173deb3ec6SMatthias Ringwald     }
8183deb3ec6SMatthias Ringwald 
8193deb3ec6SMatthias Ringwald     peripheral->attribute_offset += received_blob_length;
8203deb3ec6SMatthias Ringwald     peripheral->gatt_client_state = next_query_state;
8213deb3ec6SMatthias Ringwald }
8223deb3ec6SMatthias Ringwald 
8233deb3ec6SMatthias Ringwald 
8243deb3ec6SMatthias Ringwald static int is_value_valid(gatt_client_t *peripheral, uint8_t *packet, uint16_t size){
825f8fbdce0SMatthias Ringwald     uint16_t attribute_handle = little_endian_read_16(packet, 1);
826f8fbdce0SMatthias Ringwald     uint16_t value_offset = little_endian_read_16(packet, 3);
8273deb3ec6SMatthias Ringwald 
8283deb3ec6SMatthias Ringwald     if (peripheral->attribute_handle != attribute_handle) return 0;
8293deb3ec6SMatthias Ringwald     if (peripheral->attribute_offset != value_offset) return 0;
8303deb3ec6SMatthias Ringwald     return memcmp(&peripheral->attribute_value[peripheral->attribute_offset], &packet[5], size-5) == 0;
8313deb3ec6SMatthias Ringwald }
8323deb3ec6SMatthias Ringwald 
833544128c3SMatthias Ringwald // returns 1 if packet was sent
834544128c3SMatthias Ringwald static int gatt_client_run_for_peripheral( gatt_client_t * peripheral){
8353deb3ec6SMatthias Ringwald     // log_info("- handle_peripheral_list, mtu state %u, client state %u", peripheral->mtu_state, peripheral->gatt_client_state);
8363deb3ec6SMatthias Ringwald 
837d1e1a57fSMatthias Ringwald     // wait until re-encryption as central is complete
838d1e1a57fSMatthias Ringwald     if (gap_reconnect_security_setup_active(peripheral->con_handle)) return 0;
839d1e1a57fSMatthias Ringwald 
840f4b33574SMatthias Ringwald #ifdef ENABLE_GATT_CLIENT_PAIRING
841f4b33574SMatthias Ringwald     // wait until pairing complete
842f4b33574SMatthias Ringwald     if (peripheral->wait_for_pairing_complete) return 0;
843f4b33574SMatthias Ringwald #endif
844f4b33574SMatthias Ringwald 
8453deb3ec6SMatthias Ringwald     switch (peripheral->mtu_state) {
846544128c3SMatthias Ringwald         case SEND_MTU_EXCHANGE:
8473deb3ec6SMatthias Ringwald             peripheral->mtu_state = SENT_MTU_EXCHANGE;
848fc64f94aSMatthias Ringwald             att_exchange_mtu_request(peripheral->con_handle);
849544128c3SMatthias Ringwald             return 1;
8503deb3ec6SMatthias Ringwald         case SENT_MTU_EXCHANGE:
851544128c3SMatthias Ringwald             return 0;
8523deb3ec6SMatthias Ringwald         default:
8533deb3ec6SMatthias Ringwald             break;
8543deb3ec6SMatthias Ringwald     }
8553deb3ec6SMatthias Ringwald 
8563deb3ec6SMatthias Ringwald     if (peripheral->send_confirmation){
8573deb3ec6SMatthias Ringwald         peripheral->send_confirmation = 0;
858fc64f94aSMatthias Ringwald         att_confirmation(peripheral->con_handle);
859544128c3SMatthias Ringwald         return 1;
8603deb3ec6SMatthias Ringwald     }
8613deb3ec6SMatthias Ringwald 
8623deb3ec6SMatthias Ringwald     // check MTU for writes
8633deb3ec6SMatthias Ringwald     switch (peripheral->gatt_client_state){
8643deb3ec6SMatthias Ringwald         case P_W2_SEND_WRITE_CHARACTERISTIC_VALUE:
8653deb3ec6SMatthias Ringwald         case P_W2_SEND_WRITE_CHARACTERISTIC_DESCRIPTOR:
8663deb3ec6SMatthias Ringwald             if (peripheral->attribute_length <= peripheral_mtu(peripheral) - 3) break;
8673deb3ec6SMatthias Ringwald             log_error("gatt_client_run: value len %u > MTU %u - 3\n", peripheral->attribute_length, peripheral_mtu(peripheral));
8683deb3ec6SMatthias Ringwald             gatt_client_handle_transaction_complete(peripheral);
8693deb3ec6SMatthias Ringwald             emit_gatt_complete_event(peripheral, ATT_ERROR_INVALID_ATTRIBUTE_VALUE_LENGTH);
870544128c3SMatthias Ringwald             return 0;
8713deb3ec6SMatthias Ringwald         default:
8723deb3ec6SMatthias Ringwald             break;
8733deb3ec6SMatthias Ringwald     }
8743deb3ec6SMatthias Ringwald 
8753deb3ec6SMatthias Ringwald     // log_info("gatt_client_state %u", peripheral->gatt_client_state);
8763deb3ec6SMatthias Ringwald     switch (peripheral->gatt_client_state){
8773deb3ec6SMatthias Ringwald         case P_W2_SEND_SERVICE_QUERY:
8783deb3ec6SMatthias Ringwald             peripheral->gatt_client_state = P_W4_SERVICE_QUERY_RESULT;
8793deb3ec6SMatthias Ringwald             send_gatt_services_request(peripheral);
880544128c3SMatthias Ringwald             return 1;
8813deb3ec6SMatthias Ringwald 
8823deb3ec6SMatthias Ringwald         case P_W2_SEND_SERVICE_WITH_UUID_QUERY:
8833deb3ec6SMatthias Ringwald             peripheral->gatt_client_state = P_W4_SERVICE_WITH_UUID_RESULT;
8843deb3ec6SMatthias Ringwald             send_gatt_services_by_uuid_request(peripheral);
885544128c3SMatthias Ringwald             return 1;
8863deb3ec6SMatthias Ringwald 
8873deb3ec6SMatthias Ringwald         case P_W2_SEND_ALL_CHARACTERISTICS_OF_SERVICE_QUERY:
8883deb3ec6SMatthias Ringwald             peripheral->gatt_client_state = P_W4_ALL_CHARACTERISTICS_OF_SERVICE_QUERY_RESULT;
8893deb3ec6SMatthias Ringwald             send_gatt_characteristic_request(peripheral);
890544128c3SMatthias Ringwald             return 1;
8913deb3ec6SMatthias Ringwald 
8923deb3ec6SMatthias Ringwald         case P_W2_SEND_CHARACTERISTIC_WITH_UUID_QUERY:
8933deb3ec6SMatthias Ringwald             peripheral->gatt_client_state = P_W4_CHARACTERISTIC_WITH_UUID_QUERY_RESULT;
8943deb3ec6SMatthias Ringwald             send_gatt_characteristic_request(peripheral);
895544128c3SMatthias Ringwald             return 1;
8963deb3ec6SMatthias Ringwald 
8973deb3ec6SMatthias Ringwald         case P_W2_SEND_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY:
8983deb3ec6SMatthias Ringwald             peripheral->gatt_client_state = P_W4_CHARACTERISTIC_WITH_UUID_QUERY_RESULT;
8993deb3ec6SMatthias Ringwald             send_gatt_characteristic_descriptor_request(peripheral);
900544128c3SMatthias Ringwald             return 1;
9013deb3ec6SMatthias Ringwald 
9023deb3ec6SMatthias Ringwald         case P_W2_SEND_INCLUDED_SERVICE_QUERY:
9033deb3ec6SMatthias Ringwald             peripheral->gatt_client_state = P_W4_INCLUDED_SERVICE_QUERY_RESULT;
9043deb3ec6SMatthias Ringwald             send_gatt_included_service_request(peripheral);
905544128c3SMatthias Ringwald             return 1;
9063deb3ec6SMatthias Ringwald 
9073deb3ec6SMatthias Ringwald         case P_W2_SEND_INCLUDED_SERVICE_WITH_UUID_QUERY:
9083deb3ec6SMatthias Ringwald             peripheral->gatt_client_state = P_W4_INCLUDED_SERVICE_UUID_WITH_QUERY_RESULT;
9093deb3ec6SMatthias Ringwald             send_gatt_included_service_uuid_request(peripheral);
910544128c3SMatthias Ringwald             return 1;
9113deb3ec6SMatthias Ringwald 
9123deb3ec6SMatthias Ringwald         case P_W2_SEND_READ_CHARACTERISTIC_VALUE_QUERY:
9133deb3ec6SMatthias Ringwald             peripheral->gatt_client_state = P_W4_READ_CHARACTERISTIC_VALUE_RESULT;
9143deb3ec6SMatthias Ringwald             send_gatt_read_characteristic_value_request(peripheral);
915544128c3SMatthias Ringwald             return 1;
9163deb3ec6SMatthias Ringwald 
9173deb3ec6SMatthias Ringwald         case P_W2_SEND_READ_BLOB_QUERY:
9183deb3ec6SMatthias Ringwald             peripheral->gatt_client_state = P_W4_READ_BLOB_RESULT;
9193deb3ec6SMatthias Ringwald             send_gatt_read_blob_request(peripheral);
920544128c3SMatthias Ringwald             return 1;
9213deb3ec6SMatthias Ringwald 
9223deb3ec6SMatthias Ringwald         case P_W2_SEND_READ_BY_TYPE_REQUEST:
9233deb3ec6SMatthias Ringwald             peripheral->gatt_client_state = P_W4_READ_BY_TYPE_RESPONSE;
9243deb3ec6SMatthias Ringwald             send_gatt_read_by_type_request(peripheral);
925544128c3SMatthias Ringwald             return 1;
9263deb3ec6SMatthias Ringwald 
9273deb3ec6SMatthias Ringwald         case P_W2_SEND_READ_MULTIPLE_REQUEST:
9283deb3ec6SMatthias Ringwald             peripheral->gatt_client_state = P_W4_READ_MULTIPLE_RESPONSE;
9293deb3ec6SMatthias Ringwald             send_gatt_read_multiple_request(peripheral);
930544128c3SMatthias Ringwald             return 1;
9313deb3ec6SMatthias Ringwald 
9323deb3ec6SMatthias Ringwald         case P_W2_SEND_WRITE_CHARACTERISTIC_VALUE:
9333deb3ec6SMatthias Ringwald             peripheral->gatt_client_state = P_W4_WRITE_CHARACTERISTIC_VALUE_RESULT;
9343deb3ec6SMatthias Ringwald             send_gatt_write_attribute_value_request(peripheral);
935544128c3SMatthias Ringwald             return 1;
9363deb3ec6SMatthias Ringwald 
9373deb3ec6SMatthias Ringwald         case P_W2_PREPARE_WRITE:
9383deb3ec6SMatthias Ringwald             peripheral->gatt_client_state = P_W4_PREPARE_WRITE_RESULT;
9393deb3ec6SMatthias Ringwald             send_gatt_prepare_write_request(peripheral);
940544128c3SMatthias Ringwald             return 1;
9413deb3ec6SMatthias Ringwald 
9423deb3ec6SMatthias Ringwald         case P_W2_PREPARE_WRITE_SINGLE:
9433deb3ec6SMatthias Ringwald             peripheral->gatt_client_state = P_W4_PREPARE_WRITE_SINGLE_RESULT;
9443deb3ec6SMatthias Ringwald             send_gatt_prepare_write_request(peripheral);
945544128c3SMatthias Ringwald             return 1;
9463deb3ec6SMatthias Ringwald 
9473deb3ec6SMatthias Ringwald         case P_W2_PREPARE_RELIABLE_WRITE:
9483deb3ec6SMatthias Ringwald             peripheral->gatt_client_state = P_W4_PREPARE_RELIABLE_WRITE_RESULT;
9493deb3ec6SMatthias Ringwald             send_gatt_prepare_write_request(peripheral);
950544128c3SMatthias Ringwald             return 1;
9513deb3ec6SMatthias Ringwald 
9523deb3ec6SMatthias Ringwald         case P_W2_EXECUTE_PREPARED_WRITE:
9533deb3ec6SMatthias Ringwald             peripheral->gatt_client_state = P_W4_EXECUTE_PREPARED_WRITE_RESULT;
9543deb3ec6SMatthias Ringwald             send_gatt_execute_write_request(peripheral);
955544128c3SMatthias Ringwald             return 1;
9563deb3ec6SMatthias Ringwald 
9573deb3ec6SMatthias Ringwald         case P_W2_CANCEL_PREPARED_WRITE:
9583deb3ec6SMatthias Ringwald             peripheral->gatt_client_state = P_W4_CANCEL_PREPARED_WRITE_RESULT;
9593deb3ec6SMatthias Ringwald             send_gatt_cancel_prepared_write_request(peripheral);
960544128c3SMatthias Ringwald             return 1;
9613deb3ec6SMatthias Ringwald 
9623deb3ec6SMatthias Ringwald         case P_W2_CANCEL_PREPARED_WRITE_DATA_MISMATCH:
9633deb3ec6SMatthias Ringwald             peripheral->gatt_client_state = P_W4_CANCEL_PREPARED_WRITE_DATA_MISMATCH_RESULT;
9643deb3ec6SMatthias Ringwald             send_gatt_cancel_prepared_write_request(peripheral);
965544128c3SMatthias Ringwald             return 1;
9663deb3ec6SMatthias Ringwald 
967abdc9fb5SMatthias Ringwald #ifdef ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERY
968abdc9fb5SMatthias Ringwald         case P_W2_SEND_FIND_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY:
969abdc9fb5SMatthias Ringwald             // use Find Information
970abdc9fb5SMatthias Ringwald             peripheral->gatt_client_state = P_W4_FIND_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY_RESULT;
971abdc9fb5SMatthias Ringwald             send_gatt_characteristic_descriptor_request(peripheral);
972abdc9fb5SMatthias Ringwald #else
9733deb3ec6SMatthias Ringwald         case P_W2_SEND_READ_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY:
974abdc9fb5SMatthias Ringwald             // Use Read By Type
9753deb3ec6SMatthias Ringwald             peripheral->gatt_client_state = P_W4_READ_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY_RESULT;
9763deb3ec6SMatthias Ringwald             send_gatt_read_client_characteristic_configuration_request(peripheral);
977abdc9fb5SMatthias Ringwald #endif
978544128c3SMatthias Ringwald             return 1;
9793deb3ec6SMatthias Ringwald 
9803deb3ec6SMatthias Ringwald         case P_W2_SEND_READ_CHARACTERISTIC_DESCRIPTOR_QUERY:
9813deb3ec6SMatthias Ringwald             peripheral->gatt_client_state = P_W4_READ_CHARACTERISTIC_DESCRIPTOR_RESULT;
9823deb3ec6SMatthias Ringwald             send_gatt_read_characteristic_descriptor_request(peripheral);
983544128c3SMatthias Ringwald             return 1;
9843deb3ec6SMatthias Ringwald 
9853deb3ec6SMatthias Ringwald         case P_W2_SEND_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_QUERY:
9863deb3ec6SMatthias Ringwald             peripheral->gatt_client_state = P_W4_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_RESULT;
9873deb3ec6SMatthias Ringwald             send_gatt_read_blob_request(peripheral);
988544128c3SMatthias Ringwald             return 1;
9893deb3ec6SMatthias Ringwald 
9903deb3ec6SMatthias Ringwald         case P_W2_SEND_WRITE_CHARACTERISTIC_DESCRIPTOR:
9913deb3ec6SMatthias Ringwald             peripheral->gatt_client_state = P_W4_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT;
9923deb3ec6SMatthias Ringwald             send_gatt_write_attribute_value_request(peripheral);
993544128c3SMatthias Ringwald             return 1;
9943deb3ec6SMatthias Ringwald 
9953deb3ec6SMatthias Ringwald         case P_W2_WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION:
9963deb3ec6SMatthias Ringwald             peripheral->gatt_client_state = P_W4_CLIENT_CHARACTERISTIC_CONFIGURATION_RESULT;
9973deb3ec6SMatthias Ringwald             send_gatt_write_client_characteristic_configuration_request(peripheral);
998544128c3SMatthias Ringwald             return 1;
9993deb3ec6SMatthias Ringwald 
10003deb3ec6SMatthias Ringwald         case P_W2_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR:
10013deb3ec6SMatthias Ringwald             peripheral->gatt_client_state = P_W4_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT;
10023deb3ec6SMatthias Ringwald             send_gatt_prepare_write_request(peripheral);
1003544128c3SMatthias Ringwald             return 1;
10043deb3ec6SMatthias Ringwald 
10053deb3ec6SMatthias Ringwald         case P_W2_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR:
10063deb3ec6SMatthias Ringwald             peripheral->gatt_client_state = P_W4_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT;
10073deb3ec6SMatthias Ringwald             send_gatt_execute_write_request(peripheral);
1008544128c3SMatthias Ringwald             return 1;
10093deb3ec6SMatthias Ringwald 
10107a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
1011793cf6ceSMatthias Ringwald         case P_W4_IDENTITY_RESOLVING:
1012793cf6ceSMatthias Ringwald             log_info("P_W4_IDENTITY_RESOLVING - state %x", sm_identity_resolving_state(peripheral->con_handle));
1013793cf6ceSMatthias Ringwald             switch (sm_identity_resolving_state(peripheral->con_handle)){
1014793cf6ceSMatthias Ringwald                 case IRK_LOOKUP_SUCCEEDED:
1015793cf6ceSMatthias Ringwald                     peripheral->le_device_index = sm_le_device_index(peripheral->con_handle);
1016793cf6ceSMatthias Ringwald                     peripheral->gatt_client_state = P_W4_CMAC_READY;
1017793cf6ceSMatthias Ringwald                     break;
1018793cf6ceSMatthias Ringwald                 case IRK_LOOKUP_FAILED:
1019793cf6ceSMatthias Ringwald                     gatt_client_handle_transaction_complete(peripheral);
1020793cf6ceSMatthias Ringwald                     emit_gatt_complete_event(peripheral, ATT_ERROR_BONDING_INFORMATION_MISSING);
1021793cf6ceSMatthias Ringwald                     return 0;
1022793cf6ceSMatthias Ringwald                 default:
1023793cf6ceSMatthias Ringwald                     return 0;
1024793cf6ceSMatthias Ringwald             }
1025793cf6ceSMatthias Ringwald 
1026793cf6ceSMatthias Ringwald             /* Fall through */
1027793cf6ceSMatthias Ringwald 
10283deb3ec6SMatthias Ringwald         case P_W4_CMAC_READY:
10293deb3ec6SMatthias Ringwald             if (sm_cmac_ready()){
10303deb3ec6SMatthias Ringwald                 sm_key_t csrk;
10313deb3ec6SMatthias Ringwald                 le_device_db_local_csrk_get(peripheral->le_device_index, csrk);
10323deb3ec6SMatthias Ringwald                 uint32_t sign_counter = le_device_db_local_counter_get(peripheral->le_device_index);
10333deb3ec6SMatthias Ringwald                 peripheral->gatt_client_state = P_W4_CMAC_RESULT;
10344dfd504aSMatthias 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);
10353deb3ec6SMatthias Ringwald             }
1036544128c3SMatthias Ringwald             return 0;
10373deb3ec6SMatthias Ringwald 
10383deb3ec6SMatthias Ringwald         case P_W2_SEND_SIGNED_WRITE: {
10393deb3ec6SMatthias Ringwald             peripheral->gatt_client_state = P_W4_SEND_SINGED_WRITE_DONE;
10403deb3ec6SMatthias Ringwald             // bump local signing counter
10413deb3ec6SMatthias Ringwald             uint32_t sign_counter = le_device_db_local_counter_get(peripheral->le_device_index);
10423deb3ec6SMatthias Ringwald             le_device_db_local_counter_set(peripheral->le_device_index, sign_counter + 1);
104364b12680SMatthias Ringwald             // send signed write command
10443deb3ec6SMatthias Ringwald             send_gatt_signed_write_request(peripheral, sign_counter);
10453deb3ec6SMatthias Ringwald             // finally, notifiy client that write is complete
10463deb3ec6SMatthias Ringwald             gatt_client_handle_transaction_complete(peripheral);
10479cb80b17SMilanka Ringwald             emit_gatt_complete_event(peripheral, ATT_ERROR_SUCCESS);
1048544128c3SMatthias Ringwald             return 1;
10493deb3ec6SMatthias Ringwald         }
10507a766ebfSMatthias Ringwald #endif
10513deb3ec6SMatthias Ringwald         default:
10523deb3ec6SMatthias Ringwald             break;
10533deb3ec6SMatthias Ringwald     }
105447181045SMatthias Ringwald 
105547181045SMatthias Ringwald     // requested can send snow?
105647181045SMatthias Ringwald     if (peripheral->write_without_response_callback){
105747181045SMatthias Ringwald         btstack_packet_handler_t packet_handler = peripheral->write_without_response_callback;
105847181045SMatthias Ringwald         peripheral->write_without_response_callback = NULL;
105947181045SMatthias Ringwald         uint8_t event[4];
106047181045SMatthias Ringwald         event[0] = GATT_EVENT_CAN_WRITE_WITHOUT_RESPONSE;
106147181045SMatthias Ringwald         event[1] = sizeof(event) - 2;
106247181045SMatthias Ringwald         little_endian_store_16(event, 2, peripheral->con_handle);
106347181045SMatthias Ringwald         packet_handler(HCI_EVENT_PACKET, peripheral->con_handle, event, sizeof(event));
106447181045SMatthias Ringwald         return 1; // to trigger requeueing (even if higher layer didn't sent)
106547181045SMatthias Ringwald     }
106647181045SMatthias Ringwald 
1067544128c3SMatthias Ringwald     return 0;
10683deb3ec6SMatthias Ringwald }
10693deb3ec6SMatthias Ringwald 
1070544128c3SMatthias Ringwald static void gatt_client_run(void){
1071544128c3SMatthias Ringwald     btstack_linked_item_t *it;
1072544128c3SMatthias Ringwald     for (it = (btstack_linked_item_t *) gatt_client_connections; it ; it = it->next){
1073544128c3SMatthias Ringwald         gatt_client_t * peripheral = (gatt_client_t *) it;
1074544128c3SMatthias Ringwald         if (!att_dispatch_client_can_send_now(peripheral->con_handle)) {
1075544128c3SMatthias Ringwald             att_dispatch_client_request_can_send_now_event(peripheral->con_handle);
1076544128c3SMatthias Ringwald             return;
1077544128c3SMatthias Ringwald         }
1078544128c3SMatthias Ringwald         int packet_sent = gatt_client_run_for_peripheral(peripheral);
1079544128c3SMatthias Ringwald         if (packet_sent){
108045f2a740SMatthias Ringwald             // request new permission
1081544128c3SMatthias Ringwald             att_dispatch_client_request_can_send_now_event(peripheral->con_handle);
108245f2a740SMatthias Ringwald             // requeue client for fairness and exit
108345f2a740SMatthias Ringwald             // note: iterator has become invalid
108445f2a740SMatthias Ringwald             btstack_linked_list_remove(&gatt_client_connections, (btstack_linked_item_t *) peripheral);
108545f2a740SMatthias Ringwald             btstack_linked_list_add_tail(&gatt_client_connections, (btstack_linked_item_t *) peripheral);
1086544128c3SMatthias Ringwald             return;
1087544128c3SMatthias Ringwald         }
1088544128c3SMatthias Ringwald     }
10893deb3ec6SMatthias Ringwald }
10903deb3ec6SMatthias Ringwald 
1091*7d2258b3SMilanka Ringwald static void gatt_client_report_error_if_pending(gatt_client_t *peripheral, uint8_t att_error_code) {
1092*7d2258b3SMilanka Ringwald     if (is_ready(peripheral) == 1) return;
10933deb3ec6SMatthias Ringwald     gatt_client_handle_transaction_complete(peripheral);
1094*7d2258b3SMilanka Ringwald     emit_gatt_complete_event(peripheral, att_error_code);
10953deb3ec6SMatthias Ringwald }
10963deb3ec6SMatthias Ringwald 
1097f4b33574SMatthias Ringwald static void gatt_client_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
10981ac26e06SMatthias Ringwald     UNUSED(channel);    // ok: handling own l2cap events
10991ac26e06SMatthias Ringwald     UNUSED(size);       // ok: there is no channel
11009ec2630cSMatthias Ringwald 
1101361b1363SMatthias Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
1102361b1363SMatthias Ringwald 
1103f4b33574SMatthias Ringwald     hci_con_handle_t con_handle;
1104f4b33574SMatthias Ringwald     gatt_client_t * peripheral;
11050e2df43fSMatthias Ringwald     switch (hci_event_packet_get_type(packet)) {
11063deb3ec6SMatthias Ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
11073deb3ec6SMatthias Ringwald             log_info("GATT Client: HCI_EVENT_DISCONNECTION_COMPLETE");
1108f4b33574SMatthias Ringwald             con_handle = little_endian_read_16(packet,3);
1109f4b33574SMatthias Ringwald             peripheral = get_gatt_client_context_for_handle(con_handle);
1110*7d2258b3SMilanka Ringwald             if (peripheral == NULL) break;
11113deb3ec6SMatthias Ringwald 
1112cce308d6SMatthias Ringwald             gatt_client_report_error_if_pending(peripheral, ATT_ERROR_HCI_DISCONNECT_RECEIVED);
1113cce308d6SMatthias Ringwald             gatt_client_timeout_stop(peripheral);
1114665d90f2SMatthias Ringwald             btstack_linked_list_remove(&gatt_client_connections, (btstack_linked_item_t *) peripheral);
11153deb3ec6SMatthias Ringwald             btstack_memory_gatt_client_free(peripheral);
11163deb3ec6SMatthias Ringwald             break;
1117f4b33574SMatthias Ringwald 
1118f4b33574SMatthias Ringwald #ifdef ENABLE_GATT_CLIENT_PAIRING
1119f4b33574SMatthias Ringwald         // Pairing complete (with/without bonding=storing of pairing information)
1120f4b33574SMatthias Ringwald         case SM_EVENT_PAIRING_COMPLETE:
1121f4b33574SMatthias Ringwald             con_handle = sm_event_pairing_complete_get_handle(packet);
1122f4b33574SMatthias Ringwald             peripheral = get_gatt_client_context_for_handle(con_handle);
1123*7d2258b3SMilanka Ringwald             if (peripheral == NULL) break;
1124f4b33574SMatthias Ringwald 
1125f4b33574SMatthias Ringwald             if (peripheral->wait_for_pairing_complete){
1126f4b33574SMatthias Ringwald                 peripheral->wait_for_pairing_complete = 0;
1127f4b33574SMatthias Ringwald                 if (sm_event_pairing_complete_get_status(packet)){
1128f4b33574SMatthias Ringwald                     log_info("pairing failed, report previous error 0x%x", peripheral->pending_error_code);
1129f4b33574SMatthias Ringwald                     gatt_client_handle_transaction_complete(peripheral);
1130f4b33574SMatthias Ringwald                     emit_gatt_complete_event(peripheral, peripheral->pending_error_code);
1131f4b33574SMatthias Ringwald                 } else {
1132f4b33574SMatthias Ringwald                     log_info("pairing success, retry operation");
11333deb3ec6SMatthias Ringwald                 }
1134f4b33574SMatthias Ringwald             }
1135f4b33574SMatthias Ringwald             break;
1136f4b33574SMatthias Ringwald #endif
1137793cf6ceSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
1138793cf6ceSMatthias Ringwald         // Identity Resolving completed (no code, gatt_client_run will continue)
1139793cf6ceSMatthias Ringwald         case SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED:
1140793cf6ceSMatthias Ringwald         case SM_EVENT_IDENTITY_RESOLVING_FAILED:
1141793cf6ceSMatthias Ringwald             break;
1142793cf6ceSMatthias Ringwald #endif
1143f4b33574SMatthias Ringwald 
11443deb3ec6SMatthias Ringwald         default:
11453deb3ec6SMatthias Ringwald             break;
11463deb3ec6SMatthias Ringwald     }
11473deb3ec6SMatthias Ringwald 
1148361b1363SMatthias Ringwald     gatt_client_run();
11493deb3ec6SMatthias Ringwald }
11503deb3ec6SMatthias Ringwald 
11513deb3ec6SMatthias Ringwald static void gatt_client_att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){
11523deb3ec6SMatthias Ringwald 
11530fbf59a1SMatthias Ringwald     gatt_client_t * peripheral;
11540fbf59a1SMatthias Ringwald 
11550fbf59a1SMatthias Ringwald     if (packet_type == HCI_EVENT_PACKET) {
11560fbf59a1SMatthias Ringwald         switch (packet[0]){
11570fbf59a1SMatthias Ringwald             case L2CAP_EVENT_CAN_SEND_NOW:
115851764b3bSMatthias Ringwald                 gatt_client_run();
11590fbf59a1SMatthias Ringwald                 break;
11600fbf59a1SMatthias Ringwald             // att_server has negotiated the mtu for this connection, cache if context exists
11610fbf59a1SMatthias Ringwald             case ATT_EVENT_MTU_EXCHANGE_COMPLETE:
11620fbf59a1SMatthias Ringwald                 peripheral = get_gatt_client_context_for_handle(handle);
1163*7d2258b3SMilanka Ringwald                 if (peripheral == NULL) break;
11640fbf59a1SMatthias Ringwald                 peripheral->mtu = little_endian_read_16(packet, 4);
11650fbf59a1SMatthias Ringwald                 break;
11660fbf59a1SMatthias Ringwald             default:
11670fbf59a1SMatthias Ringwald                 break;
11680fbf59a1SMatthias Ringwald         }
11690fbf59a1SMatthias Ringwald         return;
117051764b3bSMatthias Ringwald     }
117151764b3bSMatthias Ringwald 
11723deb3ec6SMatthias Ringwald     if (packet_type != ATT_DATA_PACKET) return;
11733deb3ec6SMatthias Ringwald 
11743deb3ec6SMatthias Ringwald     // special cases: notifications don't need a context while indications motivate creating one
11753deb3ec6SMatthias Ringwald     switch (packet[0]){
11763deb3ec6SMatthias Ringwald         case ATT_HANDLE_VALUE_NOTIFICATION:
1177f8fbdce0SMatthias Ringwald             report_gatt_notification(handle, little_endian_read_16(packet,1), &packet[3], size-3);
11783deb3ec6SMatthias Ringwald             return;
11793deb3ec6SMatthias Ringwald         case ATT_HANDLE_VALUE_INDICATION:
11803deb3ec6SMatthias Ringwald             peripheral = provide_context_for_conn_handle(handle);
11813deb3ec6SMatthias Ringwald             break;
11823deb3ec6SMatthias Ringwald         default:
11833deb3ec6SMatthias Ringwald             peripheral = get_gatt_client_context_for_handle(handle);
11843deb3ec6SMatthias Ringwald             break;
11853deb3ec6SMatthias Ringwald     }
11863deb3ec6SMatthias Ringwald 
1187*7d2258b3SMilanka Ringwald     if (peripheral == NULL) return;
11883deb3ec6SMatthias Ringwald 
11893deb3ec6SMatthias Ringwald     switch (packet[0]){
11903deb3ec6SMatthias Ringwald         case ATT_EXCHANGE_MTU_RESPONSE:
11913deb3ec6SMatthias Ringwald         {
1192f8fbdce0SMatthias Ringwald             uint16_t remote_rx_mtu = little_endian_read_16(packet, 1);
11933deb3ec6SMatthias Ringwald             uint16_t local_rx_mtu = l2cap_max_le_mtu();
11943deb3ec6SMatthias Ringwald             peripheral->mtu = remote_rx_mtu < local_rx_mtu ? remote_rx_mtu : local_rx_mtu;
11953deb3ec6SMatthias Ringwald             peripheral->mtu_state = MTU_EXCHANGED;
11968f37572aSJakob Krantz             emit_gatt_mtu_exchanged_result_event(peripheral, peripheral->mtu);
11973deb3ec6SMatthias Ringwald             break;
11983deb3ec6SMatthias Ringwald         }
11993deb3ec6SMatthias Ringwald         case ATT_READ_BY_GROUP_TYPE_RESPONSE:
12003deb3ec6SMatthias Ringwald             switch(peripheral->gatt_client_state){
12013deb3ec6SMatthias Ringwald                 case P_W4_SERVICE_QUERY_RESULT:
12023deb3ec6SMatthias Ringwald                     report_gatt_services(peripheral, packet, size);
12033deb3ec6SMatthias Ringwald                     trigger_next_service_query(peripheral, get_last_result_handle_from_service_list(packet, size));
12045611a760SMatthias Ringwald                     // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
12053deb3ec6SMatthias Ringwald                     break;
12063deb3ec6SMatthias Ringwald                 default:
12073deb3ec6SMatthias Ringwald                     break;
12083deb3ec6SMatthias Ringwald             }
12093deb3ec6SMatthias Ringwald             break;
12103deb3ec6SMatthias Ringwald         case ATT_HANDLE_VALUE_INDICATION:
1211f8fbdce0SMatthias Ringwald             report_gatt_indication(handle, little_endian_read_16(packet,1), &packet[3], size-3);
12123deb3ec6SMatthias Ringwald             peripheral->send_confirmation = 1;
12133deb3ec6SMatthias Ringwald             break;
12143deb3ec6SMatthias Ringwald 
12153deb3ec6SMatthias Ringwald         case ATT_READ_BY_TYPE_RESPONSE:
12163deb3ec6SMatthias Ringwald             switch (peripheral->gatt_client_state){
12173deb3ec6SMatthias Ringwald                 case P_W4_ALL_CHARACTERISTICS_OF_SERVICE_QUERY_RESULT:
12183deb3ec6SMatthias Ringwald                     report_gatt_characteristics(peripheral, packet, size);
12193deb3ec6SMatthias Ringwald                     trigger_next_characteristic_query(peripheral, get_last_result_handle_from_characteristics_list(packet, size));
12205611a760SMatthias Ringwald                     // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done, or by ATT_ERROR
12213deb3ec6SMatthias Ringwald                     break;
12223deb3ec6SMatthias Ringwald                 case P_W4_CHARACTERISTIC_WITH_UUID_QUERY_RESULT:
12233deb3ec6SMatthias Ringwald                     report_gatt_characteristics(peripheral, packet, size);
12243deb3ec6SMatthias Ringwald                     trigger_next_characteristic_query(peripheral, get_last_result_handle_from_characteristics_list(packet, size));
12255611a760SMatthias Ringwald                     // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done, or by ATT_ERROR
12263deb3ec6SMatthias Ringwald                     break;
12273deb3ec6SMatthias Ringwald                 case P_W4_INCLUDED_SERVICE_QUERY_RESULT:
12283deb3ec6SMatthias Ringwald                 {
12293deb3ec6SMatthias Ringwald                     uint16_t uuid16 = 0;
12303deb3ec6SMatthias Ringwald                     uint16_t pair_size = packet[1];
12313deb3ec6SMatthias Ringwald 
12323deb3ec6SMatthias Ringwald                     if (pair_size < 7){
12333deb3ec6SMatthias Ringwald                         // UUIDs not available, query first included service
1234f8fbdce0SMatthias Ringwald                         peripheral->start_group_handle = little_endian_read_16(packet, 2); // ready for next query
1235f8fbdce0SMatthias Ringwald                         peripheral->query_start_handle = little_endian_read_16(packet, 4);
1236f8fbdce0SMatthias Ringwald                         peripheral->query_end_handle = little_endian_read_16(packet,6);
12373deb3ec6SMatthias Ringwald                         peripheral->gatt_client_state = P_W2_SEND_INCLUDED_SERVICE_WITH_UUID_QUERY;
12383deb3ec6SMatthias Ringwald                         break;
12393deb3ec6SMatthias Ringwald                     }
12403deb3ec6SMatthias Ringwald 
12413deb3ec6SMatthias Ringwald                     uint16_t offset;
12423deb3ec6SMatthias Ringwald                     for (offset = 2; offset < size; offset += pair_size){
1243f8fbdce0SMatthias Ringwald                         uint16_t include_handle = little_endian_read_16(packet, offset);
1244f8fbdce0SMatthias Ringwald                         peripheral->query_start_handle = little_endian_read_16(packet,offset+2);
1245f8fbdce0SMatthias Ringwald                         peripheral->query_end_handle = little_endian_read_16(packet,offset+4);
1246f8fbdce0SMatthias Ringwald                         uuid16 = little_endian_read_16(packet, offset+6);
12473deb3ec6SMatthias Ringwald                         report_gatt_included_service_uuid16(peripheral, include_handle, uuid16);
12483deb3ec6SMatthias Ringwald                     }
12493deb3ec6SMatthias Ringwald 
12503deb3ec6SMatthias Ringwald                     trigger_next_included_service_query(peripheral, get_last_result_handle_from_included_services_list(packet, size));
12515611a760SMatthias Ringwald                     // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
12523deb3ec6SMatthias Ringwald                     break;
12533deb3ec6SMatthias Ringwald                 }
1254abdc9fb5SMatthias Ringwald #ifndef ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERY
12553deb3ec6SMatthias Ringwald                 case P_W4_READ_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY_RESULT:
1256f8fbdce0SMatthias Ringwald                     peripheral->client_characteristic_configuration_handle = little_endian_read_16(packet, 2);
12573deb3ec6SMatthias Ringwald                     peripheral->gatt_client_state = P_W2_WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION;
12583deb3ec6SMatthias Ringwald                     break;
1259abdc9fb5SMatthias Ringwald #endif
12603deb3ec6SMatthias Ringwald                 case P_W4_READ_BY_TYPE_RESPONSE: {
12613deb3ec6SMatthias Ringwald                     uint16_t pair_size = packet[1];
12623deb3ec6SMatthias Ringwald                     uint16_t offset;
12633deb3ec6SMatthias Ringwald                     uint16_t last_result_handle = 0;
12643deb3ec6SMatthias Ringwald                     for (offset = 2; offset < size ; offset += pair_size){
1265f8fbdce0SMatthias Ringwald                         uint16_t value_handle = little_endian_read_16(packet, offset);
12663deb3ec6SMatthias Ringwald                         report_gatt_characteristic_value(peripheral, value_handle, &packet[offset+2], pair_size-2);
12673deb3ec6SMatthias Ringwald                         last_result_handle = value_handle;
12683deb3ec6SMatthias Ringwald                     }
12693deb3ec6SMatthias Ringwald                     trigger_next_read_by_type_query(peripheral, last_result_handle);
12703deb3ec6SMatthias Ringwald                     break;
12713deb3ec6SMatthias Ringwald                 }
12723deb3ec6SMatthias Ringwald                 default:
12733deb3ec6SMatthias Ringwald                     break;
12743deb3ec6SMatthias Ringwald             }
12753deb3ec6SMatthias Ringwald             break;
12763deb3ec6SMatthias Ringwald         case ATT_READ_RESPONSE:
12773deb3ec6SMatthias Ringwald             switch (peripheral->gatt_client_state){
12783deb3ec6SMatthias Ringwald                 case P_W4_INCLUDED_SERVICE_UUID_WITH_QUERY_RESULT: {
12793deb3ec6SMatthias Ringwald                     uint8_t uuid128[16];
12809c80e4ccSMatthias Ringwald                     reverse_128(&packet[1], uuid128);
12813deb3ec6SMatthias Ringwald                     report_gatt_included_service_uuid128(peripheral, peripheral->start_group_handle, uuid128);
12823deb3ec6SMatthias Ringwald                     trigger_next_included_service_query(peripheral, peripheral->start_group_handle);
12835611a760SMatthias Ringwald                     // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
12843deb3ec6SMatthias Ringwald                     break;
12853deb3ec6SMatthias Ringwald                 }
12863deb3ec6SMatthias Ringwald                 case P_W4_READ_CHARACTERISTIC_VALUE_RESULT:
12873deb3ec6SMatthias Ringwald                     gatt_client_handle_transaction_complete(peripheral);
12883deb3ec6SMatthias Ringwald                     report_gatt_characteristic_value(peripheral, peripheral->attribute_handle, &packet[1], size-1);
12899cb80b17SMilanka Ringwald                     emit_gatt_complete_event(peripheral, ATT_ERROR_SUCCESS);
12903deb3ec6SMatthias Ringwald                     break;
12913deb3ec6SMatthias Ringwald 
12923deb3ec6SMatthias Ringwald                 case P_W4_READ_CHARACTERISTIC_DESCRIPTOR_RESULT:{
12933deb3ec6SMatthias Ringwald                     gatt_client_handle_transaction_complete(peripheral);
12943deb3ec6SMatthias Ringwald                     report_gatt_characteristic_descriptor(peripheral, peripheral->attribute_handle, &packet[1], size-1, 0);
12959cb80b17SMilanka Ringwald                     emit_gatt_complete_event(peripheral, ATT_ERROR_SUCCESS);
12963deb3ec6SMatthias Ringwald                     break;
12973deb3ec6SMatthias Ringwald                 }
12983deb3ec6SMatthias Ringwald                 default:
12993deb3ec6SMatthias Ringwald                     break;
13003deb3ec6SMatthias Ringwald             }
13013deb3ec6SMatthias Ringwald             break;
13023deb3ec6SMatthias Ringwald 
13033deb3ec6SMatthias Ringwald         case ATT_FIND_BY_TYPE_VALUE_RESPONSE:
13043deb3ec6SMatthias Ringwald         {
13053deb3ec6SMatthias Ringwald             uint8_t pair_size = 4;
13063deb3ec6SMatthias Ringwald             int i;
13073deb3ec6SMatthias Ringwald             uint16_t start_group_handle;
13085611a760SMatthias Ringwald             uint16_t   end_group_handle= 0xffff; // asserts GATT_EVENT_QUERY_COMPLETE is emitted if no results
13093deb3ec6SMatthias Ringwald             for (i = 1; i<size; i+=pair_size){
1310f8fbdce0SMatthias Ringwald                 start_group_handle = little_endian_read_16(packet,i);
1311f8fbdce0SMatthias Ringwald                 end_group_handle = little_endian_read_16(packet,i+2);
13123deb3ec6SMatthias Ringwald                 emit_gatt_service_query_result_event(peripheral, start_group_handle, end_group_handle, peripheral->uuid128);
13133deb3ec6SMatthias Ringwald             }
13143deb3ec6SMatthias Ringwald             trigger_next_service_by_uuid_query(peripheral, end_group_handle);
13155611a760SMatthias Ringwald             // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
13163deb3ec6SMatthias Ringwald             break;
13173deb3ec6SMatthias Ringwald         }
13183deb3ec6SMatthias Ringwald         case ATT_FIND_INFORMATION_REPLY:
13193deb3ec6SMatthias Ringwald         {
13203deb3ec6SMatthias Ringwald             uint8_t pair_size = 4;
13213deb3ec6SMatthias Ringwald             if (packet[1] == 2){
13223deb3ec6SMatthias Ringwald                 pair_size = 18;
13233deb3ec6SMatthias Ringwald             }
1324f8fbdce0SMatthias Ringwald             uint16_t last_descriptor_handle = little_endian_read_16(packet, size - pair_size);
1325abdc9fb5SMatthias Ringwald 
1326abdc9fb5SMatthias Ringwald #ifdef ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERY
1327abdc9fb5SMatthias Ringwald             log_info("ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERY, state %x", peripheral->gatt_client_state);
1328abdc9fb5SMatthias Ringwald             if (peripheral->gatt_client_state == P_W4_FIND_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY_RESULT){
1329abdc9fb5SMatthias Ringwald                 // iterate over descriptors looking for CCC
1330abdc9fb5SMatthias Ringwald                 if (pair_size == 4){
1331abdc9fb5SMatthias Ringwald                     int offset = 2;
1332abdc9fb5SMatthias Ringwald                     while (offset < size){
1333abdc9fb5SMatthias Ringwald                         uint16_t uuid16 = little_endian_read_16(packet, offset + 2);
1334abdc9fb5SMatthias Ringwald                         if (uuid16 == GATT_CLIENT_CHARACTERISTICS_CONFIGURATION){
1335abdc9fb5SMatthias Ringwald                             peripheral->client_characteristic_configuration_handle = little_endian_read_16(packet, offset);
1336abdc9fb5SMatthias Ringwald                             peripheral->gatt_client_state = P_W2_WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION;
1337abdc9fb5SMatthias Ringwald                             log_info("CCC found %x", peripheral->client_characteristic_configuration_handle);
1338abdc9fb5SMatthias Ringwald                             break;
1339abdc9fb5SMatthias Ringwald                         }
1340abdc9fb5SMatthias Ringwald                         offset += pair_size;
1341abdc9fb5SMatthias Ringwald                     }
1342abdc9fb5SMatthias Ringwald                 }
1343abdc9fb5SMatthias Ringwald                 if (is_query_done(peripheral, last_descriptor_handle)){
1344abdc9fb5SMatthias Ringwald 
1345abdc9fb5SMatthias Ringwald                 } else {
1346abdc9fb5SMatthias Ringwald                     // next
1347abdc9fb5SMatthias Ringwald                     peripheral->start_group_handle = last_descriptor_handle + 1;
1348abdc9fb5SMatthias Ringwald                     peripheral->gatt_client_state = P_W2_SEND_FIND_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY;
1349abdc9fb5SMatthias Ringwald                 }
1350abdc9fb5SMatthias Ringwald                 break;
1351abdc9fb5SMatthias Ringwald             }
1352abdc9fb5SMatthias Ringwald #endif
13533deb3ec6SMatthias Ringwald             report_gatt_all_characteristic_descriptors(peripheral, &packet[2], size-2, pair_size);
13543deb3ec6SMatthias Ringwald             trigger_next_characteristic_descriptor_query(peripheral, last_descriptor_handle);
13555611a760SMatthias Ringwald             // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
13563deb3ec6SMatthias Ringwald             break;
13573deb3ec6SMatthias Ringwald         }
13583deb3ec6SMatthias Ringwald 
13593deb3ec6SMatthias Ringwald         case ATT_WRITE_RESPONSE:
13603deb3ec6SMatthias Ringwald             switch (peripheral->gatt_client_state){
13613deb3ec6SMatthias Ringwald                 case P_W4_WRITE_CHARACTERISTIC_VALUE_RESULT:
13623deb3ec6SMatthias Ringwald                     gatt_client_handle_transaction_complete(peripheral);
13639cb80b17SMilanka Ringwald                     emit_gatt_complete_event(peripheral, ATT_ERROR_SUCCESS);
13643deb3ec6SMatthias Ringwald                     break;
13653deb3ec6SMatthias Ringwald                 case P_W4_CLIENT_CHARACTERISTIC_CONFIGURATION_RESULT:
13663deb3ec6SMatthias Ringwald                     gatt_client_handle_transaction_complete(peripheral);
13679cb80b17SMilanka Ringwald                     emit_gatt_complete_event(peripheral, ATT_ERROR_SUCCESS);
13683deb3ec6SMatthias Ringwald                     break;
13693deb3ec6SMatthias Ringwald                 case P_W4_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT:
13703deb3ec6SMatthias Ringwald                     gatt_client_handle_transaction_complete(peripheral);
13719cb80b17SMilanka Ringwald                     emit_gatt_complete_event(peripheral, ATT_ERROR_SUCCESS);
13723deb3ec6SMatthias Ringwald                     break;
13733deb3ec6SMatthias Ringwald                 default:
13743deb3ec6SMatthias Ringwald                     break;
13753deb3ec6SMatthias Ringwald             }
13763deb3ec6SMatthias Ringwald             break;
13773deb3ec6SMatthias Ringwald 
13783deb3ec6SMatthias Ringwald         case ATT_READ_BLOB_RESPONSE:{
13793deb3ec6SMatthias Ringwald             uint16_t received_blob_length = size-1;
13803deb3ec6SMatthias Ringwald             switch(peripheral->gatt_client_state){
13813deb3ec6SMatthias Ringwald                 case P_W4_READ_BLOB_RESULT:
13823deb3ec6SMatthias Ringwald                     report_gatt_long_characteristic_value_blob(peripheral, peripheral->attribute_handle, &packet[1], received_blob_length, peripheral->attribute_offset);
13833deb3ec6SMatthias Ringwald                     trigger_next_blob_query(peripheral, P_W2_SEND_READ_BLOB_QUERY, received_blob_length);
13845611a760SMatthias Ringwald                     // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
13853deb3ec6SMatthias Ringwald                     break;
13863deb3ec6SMatthias Ringwald                 case P_W4_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_RESULT:
13873deb3ec6SMatthias Ringwald                     report_gatt_long_characteristic_descriptor(peripheral, peripheral->attribute_handle,
13883deb3ec6SMatthias Ringwald                                                           &packet[1], received_blob_length,
13893deb3ec6SMatthias Ringwald                                                           peripheral->attribute_offset);
13903deb3ec6SMatthias Ringwald                     trigger_next_blob_query(peripheral, P_W2_SEND_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_QUERY, received_blob_length);
13915611a760SMatthias Ringwald                     // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
13923deb3ec6SMatthias Ringwald                     break;
13933deb3ec6SMatthias Ringwald                 default:
13943deb3ec6SMatthias Ringwald                     break;
13953deb3ec6SMatthias Ringwald             }
13963deb3ec6SMatthias Ringwald             break;
13973deb3ec6SMatthias Ringwald         }
13983deb3ec6SMatthias Ringwald         case ATT_PREPARE_WRITE_RESPONSE:
13993deb3ec6SMatthias Ringwald             switch (peripheral->gatt_client_state){
14003deb3ec6SMatthias Ringwald                 case P_W4_PREPARE_WRITE_SINGLE_RESULT:
14013deb3ec6SMatthias Ringwald                     gatt_client_handle_transaction_complete(peripheral);
14023deb3ec6SMatthias Ringwald                     if (is_value_valid(peripheral, packet, size)){
14039cb80b17SMilanka Ringwald                         emit_gatt_complete_event(peripheral, ATT_ERROR_SUCCESS);
14043deb3ec6SMatthias Ringwald                     } else {
14053deb3ec6SMatthias Ringwald                         emit_gatt_complete_event(peripheral, ATT_ERROR_DATA_MISMATCH);
14063deb3ec6SMatthias Ringwald                     }
14073deb3ec6SMatthias Ringwald                     break;
14083deb3ec6SMatthias Ringwald 
14093deb3ec6SMatthias Ringwald                 case P_W4_PREPARE_WRITE_RESULT:{
1410f8fbdce0SMatthias Ringwald                     peripheral->attribute_offset = little_endian_read_16(packet, 3);
14113deb3ec6SMatthias Ringwald                     trigger_next_prepare_write_query(peripheral, P_W2_PREPARE_WRITE, P_W2_EXECUTE_PREPARED_WRITE);
14125611a760SMatthias Ringwald                     // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
14133deb3ec6SMatthias Ringwald                     break;
14143deb3ec6SMatthias Ringwald                 }
14153deb3ec6SMatthias Ringwald                 case P_W4_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT:{
1416f8fbdce0SMatthias Ringwald                     peripheral->attribute_offset = little_endian_read_16(packet, 3);
14173deb3ec6SMatthias Ringwald                     trigger_next_prepare_write_query(peripheral, P_W2_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR, P_W2_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR);
14185611a760SMatthias Ringwald                     // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
14193deb3ec6SMatthias Ringwald                     break;
14203deb3ec6SMatthias Ringwald                 }
14213deb3ec6SMatthias Ringwald                 case P_W4_PREPARE_RELIABLE_WRITE_RESULT:{
14223deb3ec6SMatthias Ringwald                     if (is_value_valid(peripheral, packet, size)){
1423f8fbdce0SMatthias Ringwald                         peripheral->attribute_offset = little_endian_read_16(packet, 3);
14243deb3ec6SMatthias Ringwald                         trigger_next_prepare_write_query(peripheral, P_W2_PREPARE_RELIABLE_WRITE, P_W2_EXECUTE_PREPARED_WRITE);
14255611a760SMatthias Ringwald                         // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
14263deb3ec6SMatthias Ringwald                         break;
14273deb3ec6SMatthias Ringwald                     }
14283deb3ec6SMatthias Ringwald                     peripheral->gatt_client_state = P_W2_CANCEL_PREPARED_WRITE_DATA_MISMATCH;
14293deb3ec6SMatthias Ringwald                     break;
14303deb3ec6SMatthias Ringwald                 }
14313deb3ec6SMatthias Ringwald                 default:
14323deb3ec6SMatthias Ringwald                     break;
14333deb3ec6SMatthias Ringwald             }
14343deb3ec6SMatthias Ringwald             break;
14353deb3ec6SMatthias Ringwald 
14363deb3ec6SMatthias Ringwald         case ATT_EXECUTE_WRITE_RESPONSE:
14373deb3ec6SMatthias Ringwald             switch (peripheral->gatt_client_state){
14383deb3ec6SMatthias Ringwald                 case P_W4_EXECUTE_PREPARED_WRITE_RESULT:
14393deb3ec6SMatthias Ringwald                     gatt_client_handle_transaction_complete(peripheral);
14409cb80b17SMilanka Ringwald                     emit_gatt_complete_event(peripheral, ATT_ERROR_SUCCESS);
14413deb3ec6SMatthias Ringwald                     break;
14423deb3ec6SMatthias Ringwald                 case P_W4_CANCEL_PREPARED_WRITE_RESULT:
14433deb3ec6SMatthias Ringwald                     gatt_client_handle_transaction_complete(peripheral);
14449cb80b17SMilanka Ringwald                     emit_gatt_complete_event(peripheral, ATT_ERROR_SUCCESS);
14453deb3ec6SMatthias Ringwald                     break;
14463deb3ec6SMatthias Ringwald                 case P_W4_CANCEL_PREPARED_WRITE_DATA_MISMATCH_RESULT:
14473deb3ec6SMatthias Ringwald                     gatt_client_handle_transaction_complete(peripheral);
14483deb3ec6SMatthias Ringwald                     emit_gatt_complete_event(peripheral, ATT_ERROR_DATA_MISMATCH);
14493deb3ec6SMatthias Ringwald                     break;
14503deb3ec6SMatthias Ringwald                 case P_W4_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT:
14513deb3ec6SMatthias Ringwald                     gatt_client_handle_transaction_complete(peripheral);
14529cb80b17SMilanka Ringwald                     emit_gatt_complete_event(peripheral, ATT_ERROR_SUCCESS);
14533deb3ec6SMatthias Ringwald                     break;
14543deb3ec6SMatthias Ringwald                 default:
14553deb3ec6SMatthias Ringwald                     break;
14563deb3ec6SMatthias Ringwald 
14573deb3ec6SMatthias Ringwald             }
14583deb3ec6SMatthias Ringwald             break;
14593deb3ec6SMatthias Ringwald 
14603deb3ec6SMatthias Ringwald         case ATT_READ_MULTIPLE_RESPONSE:
14613deb3ec6SMatthias Ringwald             switch(peripheral->gatt_client_state){
14623deb3ec6SMatthias Ringwald                 case P_W4_READ_MULTIPLE_RESPONSE:
14633deb3ec6SMatthias Ringwald                     report_gatt_characteristic_value(peripheral, 0, &packet[1], size-1);
14643deb3ec6SMatthias Ringwald                     gatt_client_handle_transaction_complete(peripheral);
14659cb80b17SMilanka Ringwald                     emit_gatt_complete_event(peripheral, ATT_ERROR_SUCCESS);
14663deb3ec6SMatthias Ringwald                     break;
14673deb3ec6SMatthias Ringwald                 default:
14683deb3ec6SMatthias Ringwald                     break;
14693deb3ec6SMatthias Ringwald             }
14703deb3ec6SMatthias Ringwald             break;
14713deb3ec6SMatthias Ringwald 
14723deb3ec6SMatthias Ringwald         case ATT_ERROR_RESPONSE:
14733deb3ec6SMatthias Ringwald 
14743deb3ec6SMatthias Ringwald             switch (packet[4]){
14753deb3ec6SMatthias Ringwald                 case ATT_ERROR_ATTRIBUTE_NOT_FOUND: {
14763deb3ec6SMatthias Ringwald                     switch(peripheral->gatt_client_state){
14773deb3ec6SMatthias Ringwald                         case P_W4_SERVICE_QUERY_RESULT:
14783deb3ec6SMatthias Ringwald                         case P_W4_SERVICE_WITH_UUID_RESULT:
14793deb3ec6SMatthias Ringwald                         case P_W4_INCLUDED_SERVICE_QUERY_RESULT:
14803deb3ec6SMatthias Ringwald                         case P_W4_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY_RESULT:
14813deb3ec6SMatthias Ringwald                             gatt_client_handle_transaction_complete(peripheral);
14829cb80b17SMilanka Ringwald                             emit_gatt_complete_event(peripheral, ATT_ERROR_SUCCESS);
14833deb3ec6SMatthias Ringwald                             break;
14843deb3ec6SMatthias Ringwald                         case P_W4_ALL_CHARACTERISTICS_OF_SERVICE_QUERY_RESULT:
14853deb3ec6SMatthias Ringwald                         case P_W4_CHARACTERISTIC_WITH_UUID_QUERY_RESULT:
14863deb3ec6SMatthias Ringwald                             characteristic_end_found(peripheral, peripheral->end_group_handle);
14873deb3ec6SMatthias Ringwald                             gatt_client_handle_transaction_complete(peripheral);
14889cb80b17SMilanka Ringwald                             emit_gatt_complete_event(peripheral, ATT_ERROR_SUCCESS);
14893deb3ec6SMatthias Ringwald                             break;
14903deb3ec6SMatthias Ringwald                         case P_W4_READ_BY_TYPE_RESPONSE:
14913deb3ec6SMatthias Ringwald                             gatt_client_handle_transaction_complete(peripheral);
14923deb3ec6SMatthias Ringwald                             if (peripheral->start_group_handle == peripheral->query_start_handle){
14933deb3ec6SMatthias Ringwald                                 emit_gatt_complete_event(peripheral, ATT_ERROR_ATTRIBUTE_NOT_FOUND);
14943deb3ec6SMatthias Ringwald                             } else {
14959cb80b17SMilanka Ringwald                                 emit_gatt_complete_event(peripheral, ATT_ERROR_SUCCESS);
14963deb3ec6SMatthias Ringwald                             }
14973deb3ec6SMatthias Ringwald                             break;
14983deb3ec6SMatthias Ringwald                         default:
14993deb3ec6SMatthias Ringwald                             gatt_client_report_error_if_pending(peripheral, packet[4]);
15003deb3ec6SMatthias Ringwald                             break;
15013deb3ec6SMatthias Ringwald                     }
15023deb3ec6SMatthias Ringwald                     break;
15033deb3ec6SMatthias Ringwald                 }
1504f4b33574SMatthias Ringwald 
1505f4b33574SMatthias Ringwald #ifdef ENABLE_GATT_CLIENT_PAIRING
1506f4b33574SMatthias Ringwald 
1507f4b33574SMatthias Ringwald                 case ATT_ERROR_INSUFFICIENT_AUTHENTICATION:
1508f4b33574SMatthias Ringwald                 case ATT_ERROR_INSUFFICIENT_ENCRYPTION_KEY_SIZE:
1509a0e0c2beSMatthias Ringwald                 case ATT_ERROR_INSUFFICIENT_ENCRYPTION: {
1510a0e0c2beSMatthias Ringwald 
1511f4b33574SMatthias Ringwald                     // security too low
1512f4b33574SMatthias Ringwald                     if (peripheral->security_counter > 0) {
1513f4b33574SMatthias Ringwald                         gatt_client_report_error_if_pending(peripheral, packet[4]);
1514f4b33574SMatthias Ringwald                         break;
1515f4b33574SMatthias Ringwald                     }
1516f4b33574SMatthias Ringwald                     // start security
1517f4b33574SMatthias Ringwald                     peripheral->security_counter++;
1518f4b33574SMatthias Ringwald 
1519f4b33574SMatthias Ringwald                     // setup action
1520f4b33574SMatthias Ringwald                     int retry = 1;
1521f4b33574SMatthias Ringwald                     switch (peripheral->gatt_client_state){
1522f4b33574SMatthias Ringwald                         case P_W4_READ_CHARACTERISTIC_VALUE_RESULT:
1523f4b33574SMatthias Ringwald                             peripheral->gatt_client_state = P_W2_SEND_READ_CHARACTERISTIC_VALUE_QUERY ;
1524f4b33574SMatthias Ringwald                             break;
1525f4b33574SMatthias Ringwald                         case P_W4_READ_BLOB_RESULT:
1526f4b33574SMatthias Ringwald                             peripheral->gatt_client_state = P_W2_SEND_READ_BLOB_QUERY;
1527f4b33574SMatthias Ringwald                             break;
1528f4b33574SMatthias Ringwald                         case P_W4_READ_BY_TYPE_RESPONSE:
1529f4b33574SMatthias Ringwald                             peripheral->gatt_client_state = P_W2_SEND_READ_BY_TYPE_REQUEST;
1530f4b33574SMatthias Ringwald                             break;
1531f4b33574SMatthias Ringwald                         case P_W4_READ_MULTIPLE_RESPONSE:
1532f4b33574SMatthias Ringwald                             peripheral->gatt_client_state = P_W2_SEND_READ_MULTIPLE_REQUEST;
1533f4b33574SMatthias Ringwald                             break;
1534f4b33574SMatthias Ringwald                         case P_W4_WRITE_CHARACTERISTIC_VALUE_RESULT:
1535f4b33574SMatthias Ringwald                             peripheral->gatt_client_state = P_W2_SEND_WRITE_CHARACTERISTIC_VALUE;
1536f4b33574SMatthias Ringwald                             break;
1537f4b33574SMatthias Ringwald                         case P_W4_PREPARE_WRITE_RESULT:
1538f4b33574SMatthias Ringwald                             peripheral->gatt_client_state = P_W2_PREPARE_WRITE;
1539f4b33574SMatthias Ringwald                             break;
1540f4b33574SMatthias Ringwald                         case P_W4_PREPARE_WRITE_SINGLE_RESULT:
1541f4b33574SMatthias Ringwald                             peripheral->gatt_client_state = P_W2_PREPARE_WRITE_SINGLE;
1542f4b33574SMatthias Ringwald                             break;
1543f4b33574SMatthias Ringwald                         case P_W4_PREPARE_RELIABLE_WRITE_RESULT:
1544f4b33574SMatthias Ringwald                             peripheral->gatt_client_state = P_W2_PREPARE_RELIABLE_WRITE;
1545f4b33574SMatthias Ringwald                             break;
1546f4b33574SMatthias Ringwald                         case P_W4_EXECUTE_PREPARED_WRITE_RESULT:
1547f4b33574SMatthias Ringwald                             peripheral->gatt_client_state = P_W2_EXECUTE_PREPARED_WRITE;
1548f4b33574SMatthias Ringwald                             break;
1549f4b33574SMatthias Ringwald                         case P_W4_CANCEL_PREPARED_WRITE_RESULT:
1550f4b33574SMatthias Ringwald                             peripheral->gatt_client_state = P_W2_CANCEL_PREPARED_WRITE;
1551f4b33574SMatthias Ringwald                             break;
1552f4b33574SMatthias Ringwald                         case P_W4_CANCEL_PREPARED_WRITE_DATA_MISMATCH_RESULT:
1553f4b33574SMatthias Ringwald                             peripheral->gatt_client_state = P_W2_CANCEL_PREPARED_WRITE_DATA_MISMATCH;
1554f4b33574SMatthias Ringwald                             break;
1555f4b33574SMatthias Ringwald                         case P_W4_READ_CHARACTERISTIC_DESCRIPTOR_RESULT:
1556f4b33574SMatthias Ringwald                             peripheral->gatt_client_state = P_W2_SEND_READ_CHARACTERISTIC_DESCRIPTOR_QUERY;
1557f4b33574SMatthias Ringwald                             break;
1558f4b33574SMatthias Ringwald                         case P_W4_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_RESULT:
1559f4b33574SMatthias Ringwald                             peripheral->gatt_client_state = P_W2_SEND_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_QUERY;
1560f4b33574SMatthias Ringwald                             break;
1561f4b33574SMatthias Ringwald                         case P_W4_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT:
1562f4b33574SMatthias Ringwald                             peripheral->gatt_client_state = P_W2_SEND_WRITE_CHARACTERISTIC_DESCRIPTOR;
1563f4b33574SMatthias Ringwald                             break;
1564f4b33574SMatthias Ringwald                         case P_W4_CLIENT_CHARACTERISTIC_CONFIGURATION_RESULT:
1565f4b33574SMatthias Ringwald                             peripheral->gatt_client_state = P_W2_WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION;
1566f4b33574SMatthias Ringwald                             break;
1567f4b33574SMatthias Ringwald                         case P_W4_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT:
1568f4b33574SMatthias Ringwald                             peripheral->gatt_client_state = P_W2_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR;
1569f4b33574SMatthias Ringwald                             break;
1570f4b33574SMatthias Ringwald                         case P_W4_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT:
1571f4b33574SMatthias Ringwald                             peripheral->gatt_client_state = P_W2_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR;
1572f4b33574SMatthias Ringwald                             break;
1573f4b33574SMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
1574f4b33574SMatthias Ringwald                         case P_W4_SEND_SINGED_WRITE_DONE:
1575f4b33574SMatthias Ringwald                             peripheral->gatt_client_state = P_W2_SEND_SIGNED_WRITE;
1576f4b33574SMatthias Ringwald                             break;
1577f4b33574SMatthias Ringwald #endif
1578f4b33574SMatthias Ringwald                         default:
1579f4b33574SMatthias Ringwald                             log_info("retry not supported for state %x", peripheral->gatt_client_state);
1580f4b33574SMatthias Ringwald                             retry = 0;
1581f4b33574SMatthias Ringwald                             break;
1582f4b33574SMatthias Ringwald                     }
1583f4b33574SMatthias Ringwald 
1584f4b33574SMatthias Ringwald                     if (!retry) {
1585f4b33574SMatthias Ringwald                         gatt_client_report_error_if_pending(peripheral, packet[4]);
1586f4b33574SMatthias Ringwald                         break;
1587f4b33574SMatthias Ringwald                     }
1588f4b33574SMatthias Ringwald 
1589f4b33574SMatthias Ringwald                     log_info("security error, start pairing");
1590f4b33574SMatthias Ringwald 
1591f4b33574SMatthias Ringwald                     // requrest pairing
1592f4b33574SMatthias Ringwald                     peripheral->wait_for_pairing_complete = 1;
1593f4b33574SMatthias Ringwald                     peripheral->pending_error_code = packet[4];
1594f4b33574SMatthias Ringwald                     sm_request_pairing(peripheral->con_handle);
1595f4b33574SMatthias Ringwald                     break;
1596a0e0c2beSMatthias Ringwald                 }
1597f4b33574SMatthias Ringwald #endif
1598f4b33574SMatthias Ringwald 
1599f4b33574SMatthias Ringwald                 // nothing we can do about that
1600f4b33574SMatthias Ringwald                 case ATT_ERROR_INSUFFICIENT_AUTHORIZATION:
16013deb3ec6SMatthias Ringwald                 default:
16023deb3ec6SMatthias Ringwald                     gatt_client_report_error_if_pending(peripheral, packet[4]);
16033deb3ec6SMatthias Ringwald                     break;
16043deb3ec6SMatthias Ringwald             }
16053deb3ec6SMatthias Ringwald             break;
16063deb3ec6SMatthias Ringwald 
16073deb3ec6SMatthias Ringwald         default:
16083deb3ec6SMatthias Ringwald             log_info("ATT Handler, unhandled response type 0x%02x", packet[0]);
16093deb3ec6SMatthias Ringwald             break;
16103deb3ec6SMatthias Ringwald     }
16113deb3ec6SMatthias Ringwald     gatt_client_run();
16123deb3ec6SMatthias Ringwald }
16133deb3ec6SMatthias Ringwald 
16147a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
16153deb3ec6SMatthias Ringwald static void att_signed_write_handle_cmac_result(uint8_t hash[8]){
1616665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1617665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &gatt_client_connections);
1618665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1619665d90f2SMatthias Ringwald         gatt_client_t * peripheral = (gatt_client_t *) btstack_linked_list_iterator_next(&it);
16203deb3ec6SMatthias Ringwald         if (peripheral->gatt_client_state == P_W4_CMAC_RESULT){
16213deb3ec6SMatthias Ringwald             // store result
16223deb3ec6SMatthias Ringwald             memcpy(peripheral->cmac, hash, 8);
16239c80e4ccSMatthias Ringwald             // reverse_64(hash, peripheral->cmac);
16243deb3ec6SMatthias Ringwald             peripheral->gatt_client_state = P_W2_SEND_SIGNED_WRITE;
16253deb3ec6SMatthias Ringwald             gatt_client_run();
16263deb3ec6SMatthias Ringwald             return;
16273deb3ec6SMatthias Ringwald         }
16283deb3ec6SMatthias Ringwald     }
16293deb3ec6SMatthias Ringwald }
16303deb3ec6SMatthias Ringwald 
1631711e6c80SMatthias 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){
16323deb3ec6SMatthias Ringwald     gatt_client_t * peripheral = provide_context_for_conn_handle(con_handle);
1633*7d2258b3SMilanka Ringwald     if (peripheral == NULL) return BTSTACK_MEMORY_ALLOC_FAILED;
1634*7d2258b3SMilanka Ringwald     if (is_ready(peripheral) == 0) return GATT_CLIENT_IN_WRONG_STATE;
16353deb3ec6SMatthias Ringwald 
16369c662c9bSMatthias Ringwald     peripheral->callback = callback;
16373deb3ec6SMatthias Ringwald     peripheral->attribute_handle = handle;
16383deb3ec6SMatthias Ringwald     peripheral->attribute_length = message_len;
16393deb3ec6SMatthias Ringwald     peripheral->attribute_value = message;
1640793cf6ceSMatthias Ringwald     peripheral->gatt_client_state = P_W4_IDENTITY_RESOLVING;
16413deb3ec6SMatthias Ringwald 
16423deb3ec6SMatthias Ringwald     gatt_client_run();
1643616edd56SMatthias Ringwald     return 0;
16443deb3ec6SMatthias Ringwald }
16457a766ebfSMatthias Ringwald #endif
16463deb3ec6SMatthias Ringwald 
1647711e6c80SMatthias Ringwald uint8_t gatt_client_discover_primary_services(btstack_packet_handler_t callback, hci_con_handle_t con_handle){
16483deb3ec6SMatthias Ringwald     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
1649*7d2258b3SMilanka Ringwald     if (peripheral == NULL) return BTSTACK_MEMORY_ALLOC_FAILED;
1650*7d2258b3SMilanka Ringwald     if (is_ready(peripheral) == 0) return GATT_CLIENT_IN_WRONG_STATE;
16513deb3ec6SMatthias Ringwald 
16529c662c9bSMatthias Ringwald     peripheral->callback = callback;
16533deb3ec6SMatthias Ringwald     peripheral->start_group_handle = 0x0001;
16543deb3ec6SMatthias Ringwald     peripheral->end_group_handle   = 0xffff;
16553deb3ec6SMatthias Ringwald     peripheral->gatt_client_state = P_W2_SEND_SERVICE_QUERY;
16563deb3ec6SMatthias Ringwald     peripheral->uuid16 = 0;
16573deb3ec6SMatthias Ringwald     gatt_client_run();
1658616edd56SMatthias Ringwald     return 0;
16593deb3ec6SMatthias Ringwald }
16603deb3ec6SMatthias Ringwald 
16613deb3ec6SMatthias Ringwald 
1662711e6c80SMatthias Ringwald uint8_t gatt_client_discover_primary_services_by_uuid16(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t uuid16){
16633deb3ec6SMatthias Ringwald     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
1664*7d2258b3SMilanka Ringwald     if (peripheral == NULL) return BTSTACK_MEMORY_ALLOC_FAILED;
1665*7d2258b3SMilanka Ringwald     if (is_ready(peripheral) == 0) return GATT_CLIENT_IN_WRONG_STATE;
16663deb3ec6SMatthias Ringwald 
16679c662c9bSMatthias Ringwald     peripheral->callback = callback;
16683deb3ec6SMatthias Ringwald     peripheral->start_group_handle = 0x0001;
16693deb3ec6SMatthias Ringwald     peripheral->end_group_handle   = 0xffff;
16703deb3ec6SMatthias Ringwald     peripheral->gatt_client_state = P_W2_SEND_SERVICE_WITH_UUID_QUERY;
16713deb3ec6SMatthias Ringwald     peripheral->uuid16 = uuid16;
1672e1a125dfSMatthias Ringwald     uuid_add_bluetooth_prefix((uint8_t*) &(peripheral->uuid128), peripheral->uuid16);
16733deb3ec6SMatthias Ringwald     gatt_client_run();
1674616edd56SMatthias Ringwald     return 0;
16753deb3ec6SMatthias Ringwald }
16763deb3ec6SMatthias Ringwald 
1677711e6c80SMatthias 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){
16783deb3ec6SMatthias Ringwald     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
1679*7d2258b3SMilanka Ringwald     if (peripheral == NULL) return BTSTACK_MEMORY_ALLOC_FAILED;
1680*7d2258b3SMilanka Ringwald     if (is_ready(peripheral) == 0) return GATT_CLIENT_IN_WRONG_STATE;
16813deb3ec6SMatthias Ringwald 
16829c662c9bSMatthias Ringwald     peripheral->callback = callback;
16833deb3ec6SMatthias Ringwald     peripheral->start_group_handle = 0x0001;
16843deb3ec6SMatthias Ringwald     peripheral->end_group_handle   = 0xffff;
16853deb3ec6SMatthias Ringwald     peripheral->uuid16 = 0;
16863deb3ec6SMatthias Ringwald     memcpy(peripheral->uuid128, uuid128, 16);
16873deb3ec6SMatthias Ringwald     peripheral->gatt_client_state = P_W2_SEND_SERVICE_WITH_UUID_QUERY;
16883deb3ec6SMatthias Ringwald     gatt_client_run();
1689616edd56SMatthias Ringwald     return 0;
16903deb3ec6SMatthias Ringwald }
16913deb3ec6SMatthias Ringwald 
1692711e6c80SMatthias 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){
16933deb3ec6SMatthias Ringwald     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
1694*7d2258b3SMilanka Ringwald     if (peripheral == NULL) return BTSTACK_MEMORY_ALLOC_FAILED;
1695*7d2258b3SMilanka Ringwald     if (is_ready(peripheral) == 0) return GATT_CLIENT_IN_WRONG_STATE;
16963deb3ec6SMatthias Ringwald 
16979c662c9bSMatthias Ringwald     peripheral->callback = callback;
16983deb3ec6SMatthias Ringwald     peripheral->start_group_handle = service->start_group_handle;
16993deb3ec6SMatthias Ringwald     peripheral->end_group_handle   = service->end_group_handle;
17003deb3ec6SMatthias Ringwald     peripheral->filter_with_uuid = 0;
17013deb3ec6SMatthias Ringwald     peripheral->characteristic_start_handle = 0;
17023deb3ec6SMatthias Ringwald     peripheral->gatt_client_state = P_W2_SEND_ALL_CHARACTERISTICS_OF_SERVICE_QUERY;
17033deb3ec6SMatthias Ringwald     gatt_client_run();
1704616edd56SMatthias Ringwald     return 0;
17053deb3ec6SMatthias Ringwald }
17063deb3ec6SMatthias Ringwald 
1707711e6c80SMatthias Ringwald uint8_t gatt_client_find_included_services_for_service(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_service_t *service){
17083deb3ec6SMatthias Ringwald     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
1709*7d2258b3SMilanka Ringwald     if (peripheral == NULL) return BTSTACK_MEMORY_ALLOC_FAILED;
1710*7d2258b3SMilanka Ringwald     if (is_ready(peripheral) == 0) return GATT_CLIENT_IN_WRONG_STATE;
17113deb3ec6SMatthias Ringwald 
17129c662c9bSMatthias Ringwald     peripheral->callback = callback;
17133deb3ec6SMatthias Ringwald     peripheral->start_group_handle = service->start_group_handle;
17143deb3ec6SMatthias Ringwald     peripheral->end_group_handle   = service->end_group_handle;
17153deb3ec6SMatthias Ringwald     peripheral->gatt_client_state = P_W2_SEND_INCLUDED_SERVICE_QUERY;
17163deb3ec6SMatthias Ringwald 
17173deb3ec6SMatthias Ringwald     gatt_client_run();
1718616edd56SMatthias Ringwald     return 0;
17193deb3ec6SMatthias Ringwald }
17203deb3ec6SMatthias Ringwald 
1721711e6c80SMatthias Ringwald uint8_t gatt_client_discover_characteristics_for_handle_range_by_uuid16(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t start_handle, uint16_t end_handle, uint16_t uuid16){
17223deb3ec6SMatthias Ringwald     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
1723*7d2258b3SMilanka Ringwald     if (peripheral == NULL) return BTSTACK_MEMORY_ALLOC_FAILED;
1724*7d2258b3SMilanka Ringwald     if (is_ready(peripheral) == 0) 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 = uuid16;
1731e1a125dfSMatthias Ringwald     uuid_add_bluetooth_prefix((uint8_t*) &(peripheral->uuid128), uuid16);
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 
1739711e6c80SMatthias 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){
17403deb3ec6SMatthias Ringwald     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
1741*7d2258b3SMilanka Ringwald     if (peripheral == NULL) return BTSTACK_MEMORY_ALLOC_FAILED;
1742*7d2258b3SMilanka Ringwald     if (is_ready(peripheral) == 0) return GATT_CLIENT_IN_WRONG_STATE;
17433deb3ec6SMatthias Ringwald 
17449c662c9bSMatthias Ringwald     peripheral->callback = callback;
17453deb3ec6SMatthias Ringwald     peripheral->start_group_handle = start_handle;
17463deb3ec6SMatthias Ringwald     peripheral->end_group_handle   = end_handle;
17473deb3ec6SMatthias Ringwald     peripheral->filter_with_uuid = 1;
17483deb3ec6SMatthias Ringwald     peripheral->uuid16 = 0;
17493deb3ec6SMatthias Ringwald     memcpy(peripheral->uuid128, uuid128, 16);
17503deb3ec6SMatthias Ringwald     peripheral->characteristic_start_handle = 0;
17513deb3ec6SMatthias Ringwald     peripheral->gatt_client_state = P_W2_SEND_CHARACTERISTIC_WITH_UUID_QUERY;
17523deb3ec6SMatthias Ringwald 
17533deb3ec6SMatthias Ringwald     gatt_client_run();
1754616edd56SMatthias Ringwald     return 0;
17553deb3ec6SMatthias Ringwald }
17563deb3ec6SMatthias Ringwald 
17573deb3ec6SMatthias Ringwald 
1758d25c33e5SMatthias 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){
17599c662c9bSMatthias Ringwald     return gatt_client_discover_characteristics_for_handle_range_by_uuid16(callback, handle, service->start_group_handle, service->end_group_handle, uuid16);
17603deb3ec6SMatthias Ringwald }
17613deb3ec6SMatthias Ringwald 
1762d25c33e5SMatthias 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){
17639c662c9bSMatthias Ringwald     return gatt_client_discover_characteristics_for_handle_range_by_uuid128(callback, handle, service->start_group_handle, service->end_group_handle, uuid128);
17643deb3ec6SMatthias Ringwald }
17653deb3ec6SMatthias Ringwald 
1766711e6c80SMatthias Ringwald uint8_t gatt_client_discover_characteristic_descriptors(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_characteristic_t *characteristic){
17673deb3ec6SMatthias Ringwald     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
1768*7d2258b3SMilanka Ringwald     if (peripheral == NULL) return BTSTACK_MEMORY_ALLOC_FAILED;
1769*7d2258b3SMilanka Ringwald     if (is_ready(peripheral) == 0) return GATT_CLIENT_IN_WRONG_STATE;
17703deb3ec6SMatthias Ringwald 
17713deb3ec6SMatthias Ringwald     if (characteristic->value_handle == characteristic->end_handle){
17729cb80b17SMilanka Ringwald         emit_gatt_complete_event(peripheral, ATT_ERROR_SUCCESS);
1773616edd56SMatthias Ringwald         return 0;
17743deb3ec6SMatthias Ringwald     }
17759c662c9bSMatthias Ringwald     peripheral->callback = callback;
17763deb3ec6SMatthias Ringwald     peripheral->start_group_handle = characteristic->value_handle + 1;
17773deb3ec6SMatthias Ringwald     peripheral->end_group_handle   = characteristic->end_handle;
17783deb3ec6SMatthias Ringwald     peripheral->gatt_client_state = P_W2_SEND_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY;
17793deb3ec6SMatthias Ringwald 
17803deb3ec6SMatthias Ringwald     gatt_client_run();
1781616edd56SMatthias Ringwald     return 0;
17823deb3ec6SMatthias Ringwald }
17833deb3ec6SMatthias Ringwald 
1784711e6c80SMatthias 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){
17853deb3ec6SMatthias Ringwald     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
1786*7d2258b3SMilanka Ringwald     if (peripheral == NULL) return BTSTACK_MEMORY_ALLOC_FAILED;
1787*7d2258b3SMilanka Ringwald     if (is_ready(peripheral) == 0) return GATT_CLIENT_IN_WRONG_STATE;
17883deb3ec6SMatthias Ringwald 
17899c662c9bSMatthias Ringwald     peripheral->callback = callback;
17903deb3ec6SMatthias Ringwald     peripheral->attribute_handle = value_handle;
17913deb3ec6SMatthias Ringwald     peripheral->attribute_offset = 0;
17923deb3ec6SMatthias Ringwald     peripheral->gatt_client_state = P_W2_SEND_READ_CHARACTERISTIC_VALUE_QUERY;
17933deb3ec6SMatthias Ringwald     gatt_client_run();
1794616edd56SMatthias Ringwald     return 0;
17953deb3ec6SMatthias Ringwald }
17963deb3ec6SMatthias Ringwald 
1797711e6c80SMatthias 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){
17983deb3ec6SMatthias Ringwald     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
1799*7d2258b3SMilanka Ringwald     if (peripheral == NULL) return BTSTACK_MEMORY_ALLOC_FAILED;
1800*7d2258b3SMilanka Ringwald     if (is_ready(peripheral) == 0) return GATT_CLIENT_IN_WRONG_STATE;
18013deb3ec6SMatthias Ringwald 
18029c662c9bSMatthias Ringwald     peripheral->callback = callback;
18033deb3ec6SMatthias Ringwald     peripheral->start_group_handle = start_handle;
18043deb3ec6SMatthias Ringwald     peripheral->end_group_handle = end_handle;
18053deb3ec6SMatthias Ringwald     peripheral->query_start_handle = start_handle;
18063deb3ec6SMatthias Ringwald     peripheral->query_end_handle = end_handle;
18073deb3ec6SMatthias Ringwald     peripheral->uuid16 = uuid16;
1808e1a125dfSMatthias Ringwald     uuid_add_bluetooth_prefix((uint8_t*) &(peripheral->uuid128), uuid16);
18093deb3ec6SMatthias Ringwald     peripheral->gatt_client_state = P_W2_SEND_READ_BY_TYPE_REQUEST;
18103deb3ec6SMatthias Ringwald     gatt_client_run();
1811616edd56SMatthias Ringwald     return 0;
18123deb3ec6SMatthias Ringwald }
18133deb3ec6SMatthias Ringwald 
1814711e6c80SMatthias 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){
18153deb3ec6SMatthias Ringwald     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
1816*7d2258b3SMilanka Ringwald     if (peripheral == NULL) return BTSTACK_MEMORY_ALLOC_FAILED;
1817*7d2258b3SMilanka Ringwald     if (is_ready(peripheral) == 0) return GATT_CLIENT_IN_WRONG_STATE;
18183deb3ec6SMatthias Ringwald 
18199c662c9bSMatthias Ringwald     peripheral->callback = callback;
18203deb3ec6SMatthias Ringwald     peripheral->start_group_handle = start_handle;
18213deb3ec6SMatthias Ringwald     peripheral->end_group_handle = end_handle;
18223deb3ec6SMatthias Ringwald     peripheral->query_start_handle = start_handle;
18233deb3ec6SMatthias Ringwald     peripheral->query_end_handle = end_handle;
18243deb3ec6SMatthias Ringwald     peripheral->uuid16 = 0;
18253deb3ec6SMatthias Ringwald     memcpy(peripheral->uuid128, uuid128, 16);
18263deb3ec6SMatthias Ringwald     peripheral->gatt_client_state = P_W2_SEND_READ_BY_TYPE_REQUEST;
18273deb3ec6SMatthias Ringwald     gatt_client_run();
1828616edd56SMatthias Ringwald     return 0;
18293deb3ec6SMatthias Ringwald }
18303deb3ec6SMatthias Ringwald 
18313deb3ec6SMatthias Ringwald 
1832d25c33e5SMatthias Ringwald uint8_t gatt_client_read_value_of_characteristic(btstack_packet_handler_t callback, uint16_t handle, gatt_client_characteristic_t *characteristic){
18339c662c9bSMatthias Ringwald     return gatt_client_read_value_of_characteristic_using_value_handle(callback, handle, characteristic->value_handle);
18343deb3ec6SMatthias Ringwald }
18353deb3ec6SMatthias Ringwald 
1836711e6c80SMatthias 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){
18373deb3ec6SMatthias Ringwald     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
1838*7d2258b3SMilanka Ringwald     if (peripheral == NULL) return BTSTACK_MEMORY_ALLOC_FAILED;
1839*7d2258b3SMilanka Ringwald     if (is_ready(peripheral) == 0) return GATT_CLIENT_IN_WRONG_STATE;
18403deb3ec6SMatthias Ringwald 
18419c662c9bSMatthias Ringwald     peripheral->callback = callback;
18423deb3ec6SMatthias Ringwald     peripheral->attribute_handle = characteristic_value_handle;
18433deb3ec6SMatthias Ringwald     peripheral->attribute_offset = offset;
18443deb3ec6SMatthias Ringwald     peripheral->gatt_client_state = P_W2_SEND_READ_BLOB_QUERY;
18453deb3ec6SMatthias Ringwald     gatt_client_run();
1846616edd56SMatthias Ringwald     return 0;
18473deb3ec6SMatthias Ringwald }
18483deb3ec6SMatthias Ringwald 
1849711e6c80SMatthias 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){
18509c662c9bSMatthias Ringwald     return gatt_client_read_long_value_of_characteristic_using_value_handle_with_offset(callback, con_handle, characteristic_value_handle, 0);
18513deb3ec6SMatthias Ringwald }
18523deb3ec6SMatthias Ringwald 
1853d25c33e5SMatthias Ringwald uint8_t gatt_client_read_long_value_of_characteristic(btstack_packet_handler_t callback, uint16_t handle, gatt_client_characteristic_t *characteristic){
18549c662c9bSMatthias Ringwald     return gatt_client_read_long_value_of_characteristic_using_value_handle(callback, handle, characteristic->value_handle);
18553deb3ec6SMatthias Ringwald }
18563deb3ec6SMatthias Ringwald 
1857711e6c80SMatthias 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){
18583deb3ec6SMatthias Ringwald     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
1859*7d2258b3SMilanka Ringwald     if (peripheral == NULL) return BTSTACK_MEMORY_ALLOC_FAILED;
1860*7d2258b3SMilanka Ringwald     if (is_ready(peripheral) == 0) return GATT_CLIENT_IN_WRONG_STATE;
18613deb3ec6SMatthias Ringwald 
18629c662c9bSMatthias Ringwald     peripheral->callback = callback;
18633deb3ec6SMatthias Ringwald     peripheral->read_multiple_handle_count = num_value_handles;
18643deb3ec6SMatthias Ringwald     peripheral->read_multiple_handles = value_handles;
18653deb3ec6SMatthias Ringwald     peripheral->gatt_client_state = P_W2_SEND_READ_MULTIPLE_REQUEST;
18663deb3ec6SMatthias Ringwald     gatt_client_run();
1867616edd56SMatthias Ringwald     return 0;
18683deb3ec6SMatthias Ringwald }
18693deb3ec6SMatthias Ringwald 
1870de9f8e94SMatthias 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){
18713deb3ec6SMatthias Ringwald     gatt_client_t * peripheral = provide_context_for_conn_handle(con_handle);
18723deb3ec6SMatthias Ringwald 
1873*7d2258b3SMilanka Ringwald     if (peripheral == NULL) return BTSTACK_MEMORY_ALLOC_FAILED;
1874*7d2258b3SMilanka Ringwald     if (is_ready(peripheral) == 0) return GATT_CLIENT_IN_WRONG_STATE;
18753deb3ec6SMatthias Ringwald 
1876616edd56SMatthias Ringwald     if (value_length > peripheral_mtu(peripheral) - 3) return GATT_CLIENT_VALUE_TOO_LONG;
187751764b3bSMatthias Ringwald     if (!att_dispatch_client_can_send_now(peripheral->con_handle)) return GATT_CLIENT_BUSY;
18783deb3ec6SMatthias Ringwald 
1879fc64f94aSMatthias Ringwald     att_write_request(ATT_WRITE_COMMAND, peripheral->con_handle, value_handle, value_length, value);
1880616edd56SMatthias Ringwald     return 0;
18813deb3ec6SMatthias Ringwald }
18823deb3ec6SMatthias Ringwald 
1883711e6c80SMatthias 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){
18843deb3ec6SMatthias Ringwald     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
1885*7d2258b3SMilanka Ringwald     if (peripheral == NULL) return BTSTACK_MEMORY_ALLOC_FAILED;
1886*7d2258b3SMilanka Ringwald     if (is_ready(peripheral) == 0) return GATT_CLIENT_IN_WRONG_STATE;
18873deb3ec6SMatthias Ringwald 
18889c662c9bSMatthias Ringwald     peripheral->callback = callback;
18893deb3ec6SMatthias Ringwald     peripheral->attribute_handle = value_handle;
18903deb3ec6SMatthias Ringwald     peripheral->attribute_length = value_length;
18913deb3ec6SMatthias Ringwald     peripheral->attribute_value = data;
18923deb3ec6SMatthias Ringwald     peripheral->gatt_client_state = P_W2_SEND_WRITE_CHARACTERISTIC_VALUE;
18933deb3ec6SMatthias Ringwald     gatt_client_run();
1894616edd56SMatthias Ringwald     return 0;
18953deb3ec6SMatthias Ringwald }
18963deb3ec6SMatthias Ringwald 
1897711e6c80SMatthias 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){
18983deb3ec6SMatthias Ringwald     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
1899*7d2258b3SMilanka Ringwald     if (peripheral == NULL) return BTSTACK_MEMORY_ALLOC_FAILED;
1900*7d2258b3SMilanka Ringwald     if (is_ready(peripheral) == 0) return GATT_CLIENT_IN_WRONG_STATE;
19013deb3ec6SMatthias Ringwald 
19029c662c9bSMatthias Ringwald     peripheral->callback = callback;
19033deb3ec6SMatthias Ringwald     peripheral->attribute_handle = value_handle;
19043deb3ec6SMatthias Ringwald     peripheral->attribute_length = value_length;
19053deb3ec6SMatthias Ringwald     peripheral->attribute_offset = offset;
19063deb3ec6SMatthias Ringwald     peripheral->attribute_value = data;
19073deb3ec6SMatthias Ringwald     peripheral->gatt_client_state = P_W2_PREPARE_WRITE;
19083deb3ec6SMatthias Ringwald     gatt_client_run();
1909616edd56SMatthias Ringwald     return 0;
19103deb3ec6SMatthias Ringwald }
19113deb3ec6SMatthias Ringwald 
1912711e6c80SMatthias 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){
19139c662c9bSMatthias Ringwald     return gatt_client_write_long_value_of_characteristic_with_offset(callback, con_handle, value_handle, 0, value_length, value);
19143deb3ec6SMatthias Ringwald }
19153deb3ec6SMatthias Ringwald 
1916711e6c80SMatthias 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){
19173deb3ec6SMatthias Ringwald     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
1918*7d2258b3SMilanka Ringwald     if (peripheral == NULL) return BTSTACK_MEMORY_ALLOC_FAILED;
1919*7d2258b3SMilanka Ringwald     if (is_ready(peripheral) == 0) return GATT_CLIENT_IN_WRONG_STATE;
19203deb3ec6SMatthias Ringwald 
19219c662c9bSMatthias Ringwald     peripheral->callback = callback;
19223deb3ec6SMatthias Ringwald     peripheral->attribute_handle = value_handle;
19233deb3ec6SMatthias Ringwald     peripheral->attribute_length = value_length;
19243deb3ec6SMatthias Ringwald     peripheral->attribute_offset = 0;
19253deb3ec6SMatthias Ringwald     peripheral->attribute_value = value;
19263deb3ec6SMatthias Ringwald     peripheral->gatt_client_state = P_W2_PREPARE_RELIABLE_WRITE;
19273deb3ec6SMatthias Ringwald     gatt_client_run();
1928616edd56SMatthias Ringwald     return 0;
19293deb3ec6SMatthias Ringwald }
19303deb3ec6SMatthias Ringwald 
1931711e6c80SMatthias 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){
19323deb3ec6SMatthias Ringwald     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
1933*7d2258b3SMilanka Ringwald     if (peripheral == NULL) return BTSTACK_MEMORY_ALLOC_FAILED;
1934*7d2258b3SMilanka Ringwald     if (is_ready(peripheral) == 0) return GATT_CLIENT_IN_WRONG_STATE;
19353deb3ec6SMatthias Ringwald 
19363deb3ec6SMatthias Ringwald     if ( (configuration & GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION) &&
19373deb3ec6SMatthias Ringwald         (characteristic->properties & ATT_PROPERTY_NOTIFY) == 0) {
1938d8e8f12aSMatthias Ringwald         log_info("gatt_client_write_client_characteristic_configuration: GATT_CLIENT_CHARACTERISTIC_NOTIFICATION_NOT_SUPPORTED");
1939616edd56SMatthias Ringwald         return GATT_CLIENT_CHARACTERISTIC_NOTIFICATION_NOT_SUPPORTED;
19403deb3ec6SMatthias Ringwald     } else if ( (configuration & GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_INDICATION) &&
19413deb3ec6SMatthias Ringwald                (characteristic->properties & ATT_PROPERTY_INDICATE) == 0){
1942d8e8f12aSMatthias Ringwald         log_info("gatt_client_write_client_characteristic_configuration: GATT_CLIENT_CHARACTERISTIC_INDICATION_NOT_SUPPORTED");
1943616edd56SMatthias Ringwald         return GATT_CLIENT_CHARACTERISTIC_INDICATION_NOT_SUPPORTED;
19443deb3ec6SMatthias Ringwald     }
19453deb3ec6SMatthias Ringwald 
19469c662c9bSMatthias Ringwald     peripheral->callback = callback;
19473deb3ec6SMatthias Ringwald     peripheral->start_group_handle = characteristic->value_handle;
19483deb3ec6SMatthias Ringwald     peripheral->end_group_handle = characteristic->end_handle;
1949f8fbdce0SMatthias Ringwald     little_endian_store_16(peripheral->client_characteristic_configuration_value, 0, configuration);
19503deb3ec6SMatthias Ringwald 
1951abdc9fb5SMatthias Ringwald #ifdef ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERY
1952abdc9fb5SMatthias Ringwald     peripheral->gatt_client_state = P_W2_SEND_FIND_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY;
1953abdc9fb5SMatthias Ringwald #else
19543deb3ec6SMatthias Ringwald     peripheral->gatt_client_state = P_W2_SEND_READ_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY;
1955abdc9fb5SMatthias Ringwald #endif
19563deb3ec6SMatthias Ringwald     gatt_client_run();
19579cb80b17SMilanka Ringwald     return ERROR_CODE_SUCCESS;
19583deb3ec6SMatthias Ringwald }
19593deb3ec6SMatthias Ringwald 
1960711e6c80SMatthias 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){
19613deb3ec6SMatthias Ringwald     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
1962*7d2258b3SMilanka Ringwald     if (peripheral == NULL) return BTSTACK_MEMORY_ALLOC_FAILED;
1963*7d2258b3SMilanka Ringwald     if (is_ready(peripheral) == 0) return GATT_CLIENT_IN_WRONG_STATE;
19643deb3ec6SMatthias Ringwald 
19659c662c9bSMatthias Ringwald     peripheral->callback = callback;
19663deb3ec6SMatthias Ringwald     peripheral->attribute_handle = descriptor_handle;
19673deb3ec6SMatthias Ringwald 
19683deb3ec6SMatthias Ringwald     peripheral->gatt_client_state = P_W2_SEND_READ_CHARACTERISTIC_DESCRIPTOR_QUERY;
19693deb3ec6SMatthias Ringwald     gatt_client_run();
1970616edd56SMatthias Ringwald     return 0;
19713deb3ec6SMatthias Ringwald }
19723deb3ec6SMatthias Ringwald 
1973711e6c80SMatthias 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){
19749c662c9bSMatthias Ringwald     return gatt_client_read_characteristic_descriptor_using_descriptor_handle(callback, con_handle, descriptor->handle);
19753deb3ec6SMatthias Ringwald }
19763deb3ec6SMatthias Ringwald 
1977711e6c80SMatthias 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){
19783deb3ec6SMatthias Ringwald     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
1979*7d2258b3SMilanka Ringwald     if (peripheral == NULL) return BTSTACK_MEMORY_ALLOC_FAILED;
1980*7d2258b3SMilanka Ringwald     if (is_ready(peripheral) == 0) return GATT_CLIENT_IN_WRONG_STATE;
19813deb3ec6SMatthias Ringwald 
19829c662c9bSMatthias Ringwald     peripheral->callback = callback;
19833deb3ec6SMatthias Ringwald     peripheral->attribute_handle = descriptor_handle;
19843deb3ec6SMatthias Ringwald     peripheral->attribute_offset = offset;
19853deb3ec6SMatthias Ringwald     peripheral->gatt_client_state = P_W2_SEND_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_QUERY;
19863deb3ec6SMatthias Ringwald     gatt_client_run();
1987616edd56SMatthias Ringwald     return 0;
19883deb3ec6SMatthias Ringwald }
19893deb3ec6SMatthias Ringwald 
1990711e6c80SMatthias 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){
19919c662c9bSMatthias Ringwald     return gatt_client_read_long_characteristic_descriptor_using_descriptor_handle_with_offset(callback, con_handle, descriptor_handle, 0);
19923deb3ec6SMatthias Ringwald }
19933deb3ec6SMatthias Ringwald 
1994711e6c80SMatthias 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){
19959c662c9bSMatthias Ringwald     return gatt_client_read_long_characteristic_descriptor_using_descriptor_handle(callback, con_handle, descriptor->handle);
19963deb3ec6SMatthias Ringwald }
19973deb3ec6SMatthias Ringwald 
1998711e6c80SMatthias 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){
19993deb3ec6SMatthias Ringwald     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
2000*7d2258b3SMilanka Ringwald     if (peripheral == NULL) return BTSTACK_MEMORY_ALLOC_FAILED;
2001*7d2258b3SMilanka Ringwald     if (is_ready(peripheral) == 0) return GATT_CLIENT_IN_WRONG_STATE;
20023deb3ec6SMatthias Ringwald 
20039c662c9bSMatthias Ringwald     peripheral->callback = callback;
20043deb3ec6SMatthias Ringwald     peripheral->attribute_handle = descriptor_handle;
20053deb3ec6SMatthias Ringwald     peripheral->attribute_length = length;
20063deb3ec6SMatthias Ringwald     peripheral->attribute_offset = 0;
20073deb3ec6SMatthias Ringwald     peripheral->attribute_value = data;
20083deb3ec6SMatthias Ringwald     peripheral->gatt_client_state = P_W2_SEND_WRITE_CHARACTERISTIC_DESCRIPTOR;
20093deb3ec6SMatthias Ringwald     gatt_client_run();
2010616edd56SMatthias Ringwald     return 0;
20113deb3ec6SMatthias Ringwald }
20123deb3ec6SMatthias Ringwald 
2013711e6c80SMatthias 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){
20149c662c9bSMatthias Ringwald     return gatt_client_write_characteristic_descriptor_using_descriptor_handle(callback, con_handle, descriptor->handle, length, value);
20153deb3ec6SMatthias Ringwald }
20163deb3ec6SMatthias Ringwald 
2017711e6c80SMatthias 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){
20183deb3ec6SMatthias Ringwald     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
2019*7d2258b3SMilanka Ringwald     if (peripheral == NULL) return BTSTACK_MEMORY_ALLOC_FAILED;
2020*7d2258b3SMilanka Ringwald     if (is_ready(peripheral) == 0) return GATT_CLIENT_IN_WRONG_STATE;
20213deb3ec6SMatthias Ringwald 
20229c662c9bSMatthias Ringwald     peripheral->callback = callback;
20233deb3ec6SMatthias Ringwald     peripheral->attribute_handle = descriptor_handle;
20243deb3ec6SMatthias Ringwald     peripheral->attribute_length = length;
20253deb3ec6SMatthias Ringwald     peripheral->attribute_offset = offset;
20263deb3ec6SMatthias Ringwald     peripheral->attribute_value = data;
20273deb3ec6SMatthias Ringwald     peripheral->gatt_client_state = P_W2_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR;
20283deb3ec6SMatthias Ringwald     gatt_client_run();
2029616edd56SMatthias Ringwald     return 0;
20303deb3ec6SMatthias Ringwald }
20313deb3ec6SMatthias Ringwald 
2032711e6c80SMatthias 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){
20339c662c9bSMatthias Ringwald     return gatt_client_write_long_characteristic_descriptor_using_descriptor_handle_with_offset(callback, con_handle, descriptor_handle, 0, length, data );
20343deb3ec6SMatthias Ringwald }
20353deb3ec6SMatthias Ringwald 
2036711e6c80SMatthias 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){
20379c662c9bSMatthias Ringwald     return gatt_client_write_long_characteristic_descriptor_using_descriptor_handle(callback, con_handle, descriptor->handle, length, value);
20383deb3ec6SMatthias Ringwald }
20393deb3ec6SMatthias Ringwald 
20403deb3ec6SMatthias Ringwald /**
20413deb3ec6SMatthias Ringwald  * @brief -> gatt complete event
20423deb3ec6SMatthias Ringwald  */
2043711e6c80SMatthias 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){
20443deb3ec6SMatthias Ringwald     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
2045*7d2258b3SMilanka Ringwald     if (peripheral == NULL) return BTSTACK_MEMORY_ALLOC_FAILED;
2046*7d2258b3SMilanka Ringwald     if (is_ready(peripheral) == 0) return GATT_CLIENT_IN_WRONG_STATE;
20473deb3ec6SMatthias Ringwald 
20489c662c9bSMatthias Ringwald     peripheral->callback = callback;
20493deb3ec6SMatthias Ringwald     peripheral->attribute_handle = attribute_handle;
20503deb3ec6SMatthias Ringwald     peripheral->attribute_length = length;
20513deb3ec6SMatthias Ringwald     peripheral->attribute_offset = offset;
20523deb3ec6SMatthias Ringwald     peripheral->attribute_value = data;
20533deb3ec6SMatthias Ringwald     peripheral->gatt_client_state = P_W2_PREPARE_WRITE_SINGLE;
20543deb3ec6SMatthias Ringwald     gatt_client_run();
2055616edd56SMatthias Ringwald     return 0;
20563deb3ec6SMatthias Ringwald }
20573deb3ec6SMatthias Ringwald 
20583deb3ec6SMatthias Ringwald /**
20593deb3ec6SMatthias Ringwald  * @brief -> gatt complete event
20603deb3ec6SMatthias Ringwald  */
2061711e6c80SMatthias Ringwald uint8_t gatt_client_execute_write(btstack_packet_handler_t callback, hci_con_handle_t con_handle){
20623deb3ec6SMatthias Ringwald     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
20633deb3ec6SMatthias Ringwald 
2064*7d2258b3SMilanka Ringwald     if (peripheral == NULL) return BTSTACK_MEMORY_ALLOC_FAILED;
2065*7d2258b3SMilanka Ringwald     if (is_ready(peripheral) == 0) return GATT_CLIENT_IN_WRONG_STATE;
20663deb3ec6SMatthias Ringwald 
20679c662c9bSMatthias Ringwald     peripheral->callback = callback;
20683deb3ec6SMatthias Ringwald     peripheral->gatt_client_state = P_W2_EXECUTE_PREPARED_WRITE;
20693deb3ec6SMatthias Ringwald     gatt_client_run();
2070616edd56SMatthias Ringwald     return 0;
20713deb3ec6SMatthias Ringwald }
20723deb3ec6SMatthias Ringwald 
20733deb3ec6SMatthias Ringwald /**
20743deb3ec6SMatthias Ringwald  * @brief -> gatt complete event
20753deb3ec6SMatthias Ringwald  */
2076711e6c80SMatthias Ringwald uint8_t gatt_client_cancel_write(btstack_packet_handler_t callback, hci_con_handle_t con_handle){
20773deb3ec6SMatthias Ringwald     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
2078*7d2258b3SMilanka Ringwald     if (peripheral == NULL) return BTSTACK_MEMORY_ALLOC_FAILED;
2079*7d2258b3SMilanka Ringwald     if (is_ready(peripheral) == 0) return GATT_CLIENT_IN_WRONG_STATE;
20803deb3ec6SMatthias Ringwald 
20819c662c9bSMatthias Ringwald     peripheral->callback = callback;
20823deb3ec6SMatthias Ringwald     peripheral->gatt_client_state = P_W2_CANCEL_PREPARED_WRITE;
20833deb3ec6SMatthias Ringwald     gatt_client_run();
2084*7d2258b3SMilanka Ringwald     return ERROR_CODE_SUCCESS;
20853deb3ec6SMatthias Ringwald }
20863deb3ec6SMatthias Ringwald 
2087313e337bSMatthias Ringwald void gatt_client_deserialize_service(const uint8_t *packet, int offset, gatt_client_service_t *service){
20886ba2ad22SMatthias Ringwald     service->start_group_handle = little_endian_read_16(packet, offset);
20896ba2ad22SMatthias Ringwald     service->end_group_handle = little_endian_read_16(packet, offset + 2);
20906ba2ad22SMatthias Ringwald     reverse_128(&packet[offset + 4], service->uuid128);
20916ba2ad22SMatthias Ringwald     if (uuid_has_bluetooth_prefix(service->uuid128)){
20926ba2ad22SMatthias Ringwald         service->uuid16 = big_endian_read_32(service->uuid128, 0);
20936ba2ad22SMatthias Ringwald     }
20946ba2ad22SMatthias Ringwald }
20956ba2ad22SMatthias Ringwald 
2096313e337bSMatthias Ringwald void gatt_client_deserialize_characteristic(const uint8_t * packet, int offset, gatt_client_characteristic_t * characteristic){
20976ba2ad22SMatthias Ringwald     characteristic->start_handle = little_endian_read_16(packet, offset);
20986ba2ad22SMatthias Ringwald     characteristic->value_handle = little_endian_read_16(packet, offset + 2);
20996ba2ad22SMatthias Ringwald     characteristic->end_handle = little_endian_read_16(packet, offset + 4);
21006ba2ad22SMatthias Ringwald     characteristic->properties = little_endian_read_16(packet, offset + 6);
210186c38559SMatthias Ringwald     reverse_128(&packet[offset+8], characteristic->uuid128);
21026ba2ad22SMatthias Ringwald     if (uuid_has_bluetooth_prefix(characteristic->uuid128)){
21036ba2ad22SMatthias Ringwald         characteristic->uuid16 = big_endian_read_32(characteristic->uuid128, 0);
21046ba2ad22SMatthias Ringwald     }
21056ba2ad22SMatthias Ringwald }
21066ba2ad22SMatthias Ringwald 
2107313e337bSMatthias Ringwald void gatt_client_deserialize_characteristic_descriptor(const uint8_t * packet, int offset, gatt_client_characteristic_descriptor_t * descriptor){
21086ba2ad22SMatthias Ringwald     descriptor->handle = little_endian_read_16(packet, offset);
21096ba2ad22SMatthias Ringwald     reverse_128(&packet[offset+2], descriptor->uuid128);
2110b4895529SJakob Krantz     if (uuid_has_bluetooth_prefix(descriptor->uuid128)){
2111b4895529SJakob Krantz         descriptor->uuid16 = big_endian_read_32(descriptor->uuid128, 0);
2112b4895529SJakob Krantz     }
21136ba2ad22SMatthias Ringwald }
21145cf6c434SJakob Krantz 
21155cf6c434SJakob Krantz void gatt_client_send_mtu_negotiation(btstack_packet_handler_t callback, hci_con_handle_t con_handle){
21165cf6c434SJakob Krantz     gatt_client_t * context = provide_context_for_conn_handle(con_handle);
2117*7d2258b3SMilanka Ringwald     if (context == NULL) return;
21185cf6c434SJakob Krantz     if (context->mtu_state == MTU_AUTO_EXCHANGE_DISABLED){
21195cf6c434SJakob Krantz         context->callback = callback;
21205cf6c434SJakob Krantz         context->mtu_state = SEND_MTU_EXCHANGE;
21215cf6c434SJakob Krantz         gatt_client_run();
21225cf6c434SJakob Krantz     }
21235cf6c434SJakob Krantz }
212447181045SMatthias Ringwald 
212547181045SMatthias Ringwald uint8_t gatt_client_request_can_write_without_response_event(btstack_packet_handler_t callback, hci_con_handle_t con_handle){
212647181045SMatthias Ringwald     gatt_client_t * context = provide_context_for_conn_handle(con_handle);
2127*7d2258b3SMilanka Ringwald     if (context == NULL) return BTSTACK_MEMORY_ALLOC_FAILED;
2128*7d2258b3SMilanka Ringwald     if (context->write_without_response_callback != NULL) return GATT_CLIENT_IN_WRONG_STATE;
212947181045SMatthias Ringwald     context->write_without_response_callback = callback;
213047181045SMatthias Ringwald     att_dispatch_client_request_can_send_now_event(context->con_handle);
2131*7d2258b3SMilanka Ringwald     return ERROR_CODE_SUCCESS;
213247181045SMatthias Ringwald }
2133