xref: /btstack/src/ble/gatt_client.c (revision f6a28e254279c4e28b97d795f9e996e5068ab731)
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
232fca4dadSMilanka Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
242fca4dadSMilanka Ringwald  * GMBH 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>
42a95ca902SMatthias Ringwald #include <stddef.h>
433deb3ec6SMatthias Ringwald 
447907f069SMatthias Ringwald #include "btstack_config.h"
453deb3ec6SMatthias Ringwald 
46296289e3SMatthias Ringwald #include "ble/att_dispatch.h"
470e2df43fSMatthias Ringwald #include "ble/att_db.h"
480e2df43fSMatthias Ringwald #include "ble/gatt_client.h"
490e2df43fSMatthias Ringwald #include "ble/le_device_db.h"
500e2df43fSMatthias Ringwald #include "ble/sm.h"
51c40517c3SMatthias Ringwald #include "bluetooth_psm.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"
573deb3ec6SMatthias Ringwald #include "hci.h"
583deb3ec6SMatthias Ringwald #include "hci_dump.h"
5958e8c9f5SMatthias Ringwald #include "hci_event_builder.h"
603deb3ec6SMatthias Ringwald #include "l2cap.h"
611450cdc6SMatthias Ringwald #include "classic/sdp_client.h"
621450cdc6SMatthias Ringwald #include "bluetooth_gatt.h"
631450cdc6SMatthias Ringwald #include "bluetooth_sdp.h"
641450cdc6SMatthias Ringwald #include "classic/sdp_util.h"
653deb3ec6SMatthias Ringwald 
664797b71aSMatthias Ringwald #if defined(ENABLE_GATT_OVER_EATT) && !defined(ENABLE_L2CAP_ENHANCED_CREDIT_BASED_FLOW_CONTROL_MODE)
674797b71aSMatthias Ringwald #error "GATT Over EATT requires support for L2CAP Enhanced CoC. Please enable ENABLE_L2CAP_ENHANCED_CREDIT_BASED_FLOW_CONTROL_MODE"
684797b71aSMatthias Ringwald #endif
694797b71aSMatthias Ringwald 
70b1da4983SMatthias Ringwald // L2CAP Test Spec p35 defines a minimum of 100 ms, but PTS might indicate an error if we sent after 100 ms
71b1da4983SMatthias Ringwald #define GATT_CLIENT_COLLISION_BACKOFF_MS 150
72b1da4983SMatthias Ringwald 
739c662c9bSMatthias Ringwald static btstack_linked_list_t gatt_client_connections;
749c662c9bSMatthias Ringwald static btstack_linked_list_t gatt_client_value_listeners;
75842492f0SMatthias Ringwald #ifdef ENABLE_GATT_CLIENT_SERVICE_CHANGED
7658e8c9f5SMatthias Ringwald static btstack_linked_list_t gatt_client_service_changed_handler;
77842492f0SMatthias Ringwald #endif
78361b1363SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
79f4b33574SMatthias Ringwald static btstack_packet_callback_registration_t sm_event_callback_registration;
80544a5845SMatthias Ringwald static btstack_context_callback_registration_t gatt_client_deferred_event_emit;
81f4b33574SMatthias Ringwald 
8211279da7SMatthias Ringwald // GATT Client Configuration
8311279da7SMatthias Ringwald static bool                 gatt_client_mtu_exchange_enabled;
8411279da7SMatthias Ringwald static gap_security_level_t gatt_client_required_security_level;
855cf6c434SJakob Krantz 
863deb3ec6SMatthias Ringwald static void gatt_client_att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size);
87f4b33574SMatthias Ringwald static void gatt_client_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
885cf1669fSMatthias Ringwald static void gatt_client_report_error_if_pending(gatt_client_t *gatt_client, uint8_t att_error_code);
897a766ebfSMatthias Ringwald 
907a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
913deb3ec6SMatthias Ringwald static void att_signed_write_handle_cmac_result(uint8_t hash[8]);
927a766ebfSMatthias Ringwald #endif
933deb3ec6SMatthias Ringwald 
9459b5e157SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
9559b5e157SMatthias Ringwald static gatt_client_t * gatt_client_get_context_for_l2cap_cid(uint16_t l2cap_cid);
9659b5e157SMatthias Ringwald static void gatt_client_classic_handle_connected(gatt_client_t * gatt_client, uint8_t status);
9759b5e157SMatthias Ringwald static void gatt_client_classic_handle_disconnected(gatt_client_t * gatt_client);
98180cbe79SMatthias Ringwald static void gatt_client_classic_retry(btstack_timer_source_t * ts);
9959b5e157SMatthias Ringwald #endif
10059b5e157SMatthias Ringwald 
10126166ecfSMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT
1027627a0deSMatthias Ringwald static bool gatt_client_le_enhanced_handle_can_send_query(gatt_client_t * gatt_client);
1036d0f6f49SMatthias Ringwald static void gatt_client_le_enhanced_retry(btstack_timer_source_t * ts);
10426166ecfSMatthias Ringwald #endif
10526166ecfSMatthias Ringwald 
1063deb3ec6SMatthias Ringwald void gatt_client_init(void){
1073deb3ec6SMatthias Ringwald     gatt_client_connections = NULL;
108842492f0SMatthias Ringwald #ifdef ENABLE_GATT_CLIENT_SERVICE_CHANGED
10958e8c9f5SMatthias Ringwald     gatt_client_service_changed_handler = NULL;
110842492f0SMatthias Ringwald #endif
11111279da7SMatthias Ringwald     // default configuration
11211279da7SMatthias Ringwald     gatt_client_mtu_exchange_enabled    = true;
11311279da7SMatthias Ringwald     gatt_client_required_security_level = LEVEL_0;
11411279da7SMatthias Ringwald 
11511279da7SMatthias Ringwald     // register for HCI Events
116f4b33574SMatthias Ringwald     hci_event_callback_registration.callback = &gatt_client_event_packet_handler;
117361b1363SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
118361b1363SMatthias Ringwald 
119f4b33574SMatthias Ringwald     // register for SM Events
120f4b33574SMatthias Ringwald     sm_event_callback_registration.callback = &gatt_client_event_packet_handler;
121f4b33574SMatthias Ringwald     sm_add_event_handler(&sm_event_callback_registration);
122f4b33574SMatthias Ringwald 
123361b1363SMatthias Ringwald     // and ATT Client PDUs
1243deb3ec6SMatthias Ringwald     att_dispatch_register_client(gatt_client_att_packet_handler);
1253deb3ec6SMatthias Ringwald }
1263deb3ec6SMatthias Ringwald 
12711279da7SMatthias Ringwald void gatt_client_set_required_security_level(gap_security_level_t level){
12811279da7SMatthias Ringwald     gatt_client_required_security_level = level;
12911279da7SMatthias Ringwald }
13011279da7SMatthias Ringwald 
131ec820d77SMatthias Ringwald static gatt_client_t * gatt_client_for_timer(btstack_timer_source_t * ts){
132665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
133665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &gatt_client_connections);
134665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1355cf1669fSMatthias Ringwald         gatt_client_t * gatt_client = (gatt_client_t *) btstack_linked_list_iterator_next(&it);
1365cf1669fSMatthias Ringwald         if (&gatt_client->gc_timeout == ts) {
1375cf1669fSMatthias Ringwald             return gatt_client;
1383deb3ec6SMatthias Ringwald         }
1393deb3ec6SMatthias Ringwald     }
1403deb3ec6SMatthias Ringwald     return NULL;
1413deb3ec6SMatthias Ringwald }
1423deb3ec6SMatthias Ringwald 
143ec820d77SMatthias Ringwald static void gatt_client_timeout_handler(btstack_timer_source_t * timer){
1445cf1669fSMatthias Ringwald     gatt_client_t * gatt_client = gatt_client_for_timer(timer);
1455cf1669fSMatthias Ringwald     if (gatt_client == NULL) return;
1465cf1669fSMatthias Ringwald     log_info("GATT client timeout handle, handle 0x%02x", gatt_client->con_handle);
1475cf1669fSMatthias Ringwald     gatt_client_report_error_if_pending(gatt_client, ATT_ERROR_TIMEOUT);
1483deb3ec6SMatthias Ringwald }
1493deb3ec6SMatthias Ringwald 
1505cf1669fSMatthias Ringwald static void gatt_client_timeout_start(gatt_client_t * gatt_client){
1518ac3a0b3SMatthias Ringwald     log_debug("GATT client timeout start, handle 0x%02x", gatt_client->con_handle);
1525cf1669fSMatthias Ringwald     btstack_run_loop_remove_timer(&gatt_client->gc_timeout);
1535cf1669fSMatthias Ringwald     btstack_run_loop_set_timer_handler(&gatt_client->gc_timeout, gatt_client_timeout_handler);
1545cf1669fSMatthias Ringwald     btstack_run_loop_set_timer(&gatt_client->gc_timeout, 30000); // 30 seconds sm timeout
1555cf1669fSMatthias Ringwald     btstack_run_loop_add_timer(&gatt_client->gc_timeout);
1563deb3ec6SMatthias Ringwald }
1573deb3ec6SMatthias Ringwald 
1585cf1669fSMatthias Ringwald static void gatt_client_timeout_stop(gatt_client_t * gatt_client){
1598ac3a0b3SMatthias Ringwald     log_debug("GATT client timeout stop, handle 0x%02x", gatt_client->con_handle);
1605cf1669fSMatthias Ringwald     btstack_run_loop_remove_timer(&gatt_client->gc_timeout);
1613deb3ec6SMatthias Ringwald }
1623deb3ec6SMatthias Ringwald 
1631dfae9c7SMatthias Ringwald static gap_security_level_t gatt_client_le_security_level_for_connection(hci_con_handle_t con_handle){
1641dfae9c7SMatthias Ringwald     uint8_t encryption_key_size = gap_encryption_key_size(con_handle);
1651dfae9c7SMatthias Ringwald     if (encryption_key_size == 0) return LEVEL_0;
1661dfae9c7SMatthias Ringwald 
167ff3f1026SMatthias Ringwald     bool authenticated = gap_authenticated(con_handle);
1681dfae9c7SMatthias Ringwald     if (!authenticated) return LEVEL_2;
1691dfae9c7SMatthias Ringwald 
1701dfae9c7SMatthias Ringwald     return encryption_key_size == 16 ? LEVEL_4 : LEVEL_3;
1711dfae9c7SMatthias Ringwald }
1721dfae9c7SMatthias Ringwald 
1735cf1669fSMatthias Ringwald static gatt_client_t * gatt_client_get_context_for_handle(uint16_t handle){
174665d90f2SMatthias Ringwald     btstack_linked_item_t *it;
175a0da043fSMatthias Ringwald     for (it = (btstack_linked_item_t *) gatt_client_connections; it != NULL; it = it->next){
1765cf1669fSMatthias Ringwald         gatt_client_t * gatt_client = (gatt_client_t *) it;
1775cf1669fSMatthias Ringwald         if (gatt_client->con_handle == handle){
1785cf1669fSMatthias Ringwald             return gatt_client;
1793deb3ec6SMatthias Ringwald         }
1803deb3ec6SMatthias Ringwald     }
1813deb3ec6SMatthias Ringwald     return NULL;
1823deb3ec6SMatthias Ringwald }
1833deb3ec6SMatthias Ringwald 
1843deb3ec6SMatthias Ringwald 
1856b65794dSMilanka Ringwald // @return gatt_client context
1863deb3ec6SMatthias Ringwald // returns existing one, or tries to setup new one
18740faeb84SMilanka Ringwald static uint8_t gatt_client_provide_context_for_handle(hci_con_handle_t con_handle, gatt_client_t ** out_gatt_client){
1885cf1669fSMatthias Ringwald     gatt_client_t * gatt_client = gatt_client_get_context_for_handle(con_handle);
18940faeb84SMilanka Ringwald 
19040faeb84SMilanka Ringwald     if (gatt_client != NULL){
19140faeb84SMilanka Ringwald         *out_gatt_client = gatt_client;
19240faeb84SMilanka Ringwald         return ERROR_CODE_SUCCESS;
19340faeb84SMilanka Ringwald     }
1943deb3ec6SMatthias Ringwald 
195ae1d1237SMatthias Ringwald     // bail if no such hci connection
1960e0bba86SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
1970e0bba86SMatthias Ringwald     if (hci_connection == NULL){
198ae1d1237SMatthias Ringwald         log_error("No connection for handle 0x%04x", con_handle);
19940faeb84SMilanka Ringwald         *out_gatt_client = NULL;
20040faeb84SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
201ae1d1237SMatthias Ringwald     }
20240faeb84SMilanka Ringwald 
2035cf1669fSMatthias Ringwald     gatt_client = btstack_memory_gatt_client_get();
20440faeb84SMilanka Ringwald     if (gatt_client == NULL){
20540faeb84SMilanka Ringwald         *out_gatt_client = NULL;
20640faeb84SMilanka Ringwald         return ERROR_CODE_MEMORY_CAPACITY_EXCEEDED;
20740faeb84SMilanka Ringwald     }
2083deb3ec6SMatthias Ringwald     // init state
209e9cdf30bSMatthias Ringwald     gatt_client->bearer_type = ATT_BEARER_UNENHANCED_LE;
2105cf1669fSMatthias Ringwald     gatt_client->con_handle = con_handle;
2115cf1669fSMatthias Ringwald     gatt_client->mtu = ATT_DEFAULT_MTU;
2121dfae9c7SMatthias Ringwald     gatt_client->security_level = gatt_client_le_security_level_for_connection(con_handle);
21311279da7SMatthias Ringwald     if (gatt_client_mtu_exchange_enabled){
2145cf1669fSMatthias Ringwald         gatt_client->mtu_state = SEND_MTU_EXCHANGE;
2155cf6c434SJakob Krantz     } else {
2165cf1669fSMatthias Ringwald         gatt_client->mtu_state = MTU_AUTO_EXCHANGE_DISABLED;
2175cf6c434SJakob Krantz     }
218052dc82aSMatthias Ringwald     gatt_client->state = P_READY;
219d9ced76fSMatthias Ringwald     gatt_client->gatt_service_state = GATT_CLIENT_SERVICE_DISCOVER_W2_SEND;
220cbd76cecSMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT
221cbd76cecSMatthias Ringwald     gatt_client->eatt_state = GATT_CLIENT_EATT_IDLE;
222cbd76cecSMatthias Ringwald #endif
2235cf1669fSMatthias Ringwald     btstack_linked_list_add(&gatt_client_connections, (btstack_linked_item_t*)gatt_client);
2240e0bba86SMatthias Ringwald 
2250e0bba86SMatthias Ringwald     // get unenhanced att bearer state
2260e0bba86SMatthias Ringwald     if (hci_connection->att_connection.mtu_exchanged){
2270e0bba86SMatthias Ringwald         gatt_client->mtu = hci_connection->att_connection.mtu;
2280e0bba86SMatthias Ringwald         gatt_client->mtu_state = MTU_EXCHANGED;
2290e0bba86SMatthias Ringwald     }
23040faeb84SMilanka Ringwald     *out_gatt_client = gatt_client;
23140faeb84SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2323deb3ec6SMatthias Ringwald }
2333deb3ec6SMatthias Ringwald 
234de27733dSMatthias Ringwald static bool is_ready(gatt_client_t * gatt_client){
235052dc82aSMatthias Ringwald     return gatt_client->state == P_READY;
236de27733dSMatthias Ringwald }
237de27733dSMatthias Ringwald 
238de27733dSMatthias Ringwald static uint8_t gatt_client_provide_context_for_request(hci_con_handle_t con_handle, gatt_client_t ** out_gatt_client){
239de27733dSMatthias Ringwald     gatt_client_t * gatt_client = NULL;
240de27733dSMatthias Ringwald     uint8_t status = gatt_client_provide_context_for_handle(con_handle, &gatt_client);
24140faeb84SMilanka Ringwald     if (status != ERROR_CODE_SUCCESS){
24240faeb84SMilanka Ringwald         return status;
24340faeb84SMilanka Ringwald     }
244de27733dSMatthias Ringwald 
2457627a0deSMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT
2467627a0deSMatthias Ringwald     if (gatt_client->eatt_state == GATT_CLIENT_EATT_READY){
2477627a0deSMatthias Ringwald         btstack_linked_list_iterator_t it;
2487627a0deSMatthias Ringwald         gatt_client_t * eatt_client = NULL;
2497627a0deSMatthias Ringwald         // find free eatt client
2507627a0deSMatthias Ringwald         btstack_linked_list_iterator_init(&it, &gatt_client->eatt_clients);
2517627a0deSMatthias Ringwald         while (btstack_linked_list_iterator_has_next(&it)){
2527627a0deSMatthias Ringwald             gatt_client_t * client = (gatt_client_t *) btstack_linked_list_iterator_next(&it);
253052dc82aSMatthias Ringwald             if (client->state == P_READY){
2547627a0deSMatthias Ringwald                 eatt_client = client;
2557627a0deSMatthias Ringwald                 break;
2567627a0deSMatthias Ringwald             }
2577627a0deSMatthias Ringwald         }
2587627a0deSMatthias Ringwald         if (eatt_client == NULL){
2597627a0deSMatthias Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
2607627a0deSMatthias Ringwald         }
2617627a0deSMatthias Ringwald         gatt_client = eatt_client;
2627627a0deSMatthias Ringwald     }
2637627a0deSMatthias Ringwald #endif
2647627a0deSMatthias Ringwald 
265643a64fcSMatthias Ringwald     if (is_ready(gatt_client) == false){
266de27733dSMatthias Ringwald         return GATT_CLIENT_IN_WRONG_STATE;
2673deb3ec6SMatthias Ringwald     }
2683deb3ec6SMatthias Ringwald 
269de27733dSMatthias Ringwald     gatt_client_timeout_start(gatt_client);
270de27733dSMatthias Ringwald 
271de27733dSMatthias Ringwald     *out_gatt_client = gatt_client;
272de27733dSMatthias Ringwald 
273de27733dSMatthias Ringwald     return status;
2743deb3ec6SMatthias Ringwald }
2753deb3ec6SMatthias Ringwald 
276fc64f94aSMatthias Ringwald int gatt_client_is_ready(hci_con_handle_t con_handle){
27740faeb84SMilanka Ringwald     gatt_client_t * gatt_client;
27840faeb84SMilanka Ringwald     uint8_t status = gatt_client_provide_context_for_handle(con_handle, &gatt_client);
27940faeb84SMilanka Ringwald     if (status != ERROR_CODE_SUCCESS){
28040faeb84SMilanka Ringwald         return 0;
28140faeb84SMilanka Ringwald     }
282aacf1b1aSMilanka Ringwald     return is_ready(gatt_client) ? 1 : 0;
2833deb3ec6SMatthias Ringwald }
2843deb3ec6SMatthias Ringwald 
2855cf6c434SJakob Krantz void gatt_client_mtu_enable_auto_negotiation(uint8_t enabled){
28611279da7SMatthias Ringwald     gatt_client_mtu_exchange_enabled = enabled != 0;
2875cf6c434SJakob Krantz }
2885cf6c434SJakob Krantz 
289fc64f94aSMatthias Ringwald uint8_t gatt_client_get_mtu(hci_con_handle_t con_handle, uint16_t * mtu){
29040faeb84SMilanka Ringwald     gatt_client_t * gatt_client;
29140faeb84SMilanka Ringwald     uint8_t status = gatt_client_provide_context_for_handle(con_handle, &gatt_client);
29240faeb84SMilanka Ringwald     if (status != ERROR_CODE_SUCCESS){
29340faeb84SMilanka Ringwald         return status;
29440faeb84SMilanka Ringwald     }
2957d2258b3SMilanka Ringwald 
2965cf1669fSMatthias Ringwald     if ((gatt_client->mtu_state == MTU_EXCHANGED) || (gatt_client->mtu_state == MTU_AUTO_EXCHANGE_DISABLED)){
2975cf1669fSMatthias Ringwald         *mtu = gatt_client->mtu;
2987d2258b3SMilanka Ringwald         return ERROR_CODE_SUCCESS;
2993deb3ec6SMatthias Ringwald     }
3003deb3ec6SMatthias Ringwald     *mtu = ATT_DEFAULT_MTU;
301616edd56SMatthias Ringwald     return GATT_CLIENT_IN_WRONG_STATE;
3023deb3ec6SMatthias Ringwald }
3033deb3ec6SMatthias Ringwald 
3049228fd32SMatthias Ringwald static uint8_t *gatt_client_reserve_request_buffer(gatt_client_t *gatt_client) {
3057627a0deSMatthias Ringwald     switch (gatt_client->bearer_type){
3067627a0deSMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
3077627a0deSMatthias Ringwald         case ATT_BEARER_UNENHANCED_CLASSIC:
3087627a0deSMatthias Ringwald #endif
3097627a0deSMatthias Ringwald         case ATT_BEARER_UNENHANCED_LE:
3109228fd32SMatthias Ringwald             l2cap_reserve_packet_buffer();
3119228fd32SMatthias Ringwald             return l2cap_get_outgoing_buffer();
3127627a0deSMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT
3137627a0deSMatthias Ringwald         case ATT_BEARER_ENHANCED_LE:
3147627a0deSMatthias Ringwald             return gatt_client->eatt_storage_buffer;
3157627a0deSMatthias Ringwald #endif
3167627a0deSMatthias Ringwald         default:
3177627a0deSMatthias Ringwald             btstack_unreachable();
3187627a0deSMatthias Ringwald             break;
3197627a0deSMatthias Ringwald     }
3207627a0deSMatthias Ringwald     return NULL;
3219228fd32SMatthias Ringwald }
3229228fd32SMatthias Ringwald 
3233deb3ec6SMatthias Ringwald // precondition: can_send_packet_now == TRUE
3246e7b444cSMatthias Ringwald static uint8_t gatt_client_send(gatt_client_t * gatt_client, uint16_t len){
32546012949SMatthias Ringwald     switch (gatt_client->bearer_type){
32646012949SMatthias Ringwald         case ATT_BEARER_UNENHANCED_LE:
3276e7b444cSMatthias Ringwald             return l2cap_send_prepared_connectionless(gatt_client->con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, len);
32846012949SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
32946012949SMatthias Ringwald         case ATT_BEARER_UNENHANCED_CLASSIC:
33046012949SMatthias Ringwald             return l2cap_send_prepared(gatt_client->l2cap_cid, len);
33146012949SMatthias Ringwald #endif
3327627a0deSMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT
3337627a0deSMatthias Ringwald         case ATT_BEARER_ENHANCED_LE:
3347627a0deSMatthias Ringwald             return l2cap_send(gatt_client->l2cap_cid, gatt_client->eatt_storage_buffer, len);
3357627a0deSMatthias Ringwald #endif
33646012949SMatthias Ringwald         default:
33746012949SMatthias Ringwald             btstack_unreachable();
33846012949SMatthias Ringwald             return ERROR_CODE_HARDWARE_FAILURE;
33946012949SMatthias Ringwald     }
3403deb3ec6SMatthias Ringwald }
3413deb3ec6SMatthias Ringwald 
3423deb3ec6SMatthias Ringwald // precondition: can_send_packet_now == TRUE
3436e7b444cSMatthias Ringwald static uint8_t att_confirmation(gatt_client_t * gatt_client) {
3449228fd32SMatthias Ringwald     uint8_t *request = gatt_client_reserve_request_buffer(gatt_client);
3456e7b444cSMatthias Ringwald 
3466e7b444cSMatthias Ringwald     request[0] = ATT_HANDLE_VALUE_CONFIRMATION;
3476e7b444cSMatthias Ringwald 
3486e7b444cSMatthias Ringwald     return gatt_client_send(gatt_client, 1);
3496e7b444cSMatthias Ringwald }
3506e7b444cSMatthias Ringwald 
3516e7b444cSMatthias Ringwald // precondition: can_send_packet_now == TRUE
3526e7b444cSMatthias Ringwald static uint8_t att_find_information_request(gatt_client_t *gatt_client, uint8_t request_type, uint16_t start_handle,
3536e7b444cSMatthias Ringwald                                             uint16_t end_handle) {
3549228fd32SMatthias Ringwald     uint8_t *request = gatt_client_reserve_request_buffer(gatt_client);
3556e7b444cSMatthias Ringwald 
3563deb3ec6SMatthias Ringwald     request[0] = request_type;
357f8fbdce0SMatthias Ringwald     little_endian_store_16(request, 1, start_handle);
358f8fbdce0SMatthias Ringwald     little_endian_store_16(request, 3, end_handle);
3593deb3ec6SMatthias Ringwald 
3606e7b444cSMatthias Ringwald     return gatt_client_send(gatt_client, 5);
3613deb3ec6SMatthias Ringwald }
3623deb3ec6SMatthias Ringwald 
3633deb3ec6SMatthias Ringwald // precondition: can_send_packet_now == TRUE
3646e7b444cSMatthias Ringwald static uint8_t
3656e7b444cSMatthias Ringwald att_find_by_type_value_request(gatt_client_t *gatt_client, uint8_t request_type, uint16_t attribute_group_type,
3666e7b444cSMatthias Ringwald                                uint16_t start_handle, uint16_t end_handle, uint8_t *value, uint16_t value_size) {
3679228fd32SMatthias Ringwald     uint8_t *request = gatt_client_reserve_request_buffer(gatt_client);
3683deb3ec6SMatthias Ringwald     request[0] = request_type;
3699228fd32SMatthias Ringwald 
370f8fbdce0SMatthias Ringwald     little_endian_store_16(request, 1, start_handle);
371f8fbdce0SMatthias Ringwald     little_endian_store_16(request, 3, end_handle);
372f8fbdce0SMatthias Ringwald     little_endian_store_16(request, 5, attribute_group_type);
3736535961aSMatthias Ringwald     (void)memcpy(&request[7], value, value_size);
3743deb3ec6SMatthias Ringwald 
3756e7b444cSMatthias Ringwald     return gatt_client_send(gatt_client, 7u + value_size);
3763deb3ec6SMatthias Ringwald }
3773deb3ec6SMatthias Ringwald 
3783deb3ec6SMatthias Ringwald // precondition: can_send_packet_now == TRUE
3796e7b444cSMatthias Ringwald static uint8_t
3806e7b444cSMatthias Ringwald att_read_by_type_or_group_request_for_uuid16(gatt_client_t *gatt_client, uint8_t request_type, uint16_t uuid16,
3816e7b444cSMatthias Ringwald                                              uint16_t start_handle, uint16_t end_handle) {
3829228fd32SMatthias Ringwald     uint8_t *request = gatt_client_reserve_request_buffer(gatt_client);
3836e7b444cSMatthias Ringwald 
3843deb3ec6SMatthias Ringwald     request[0] = request_type;
385f8fbdce0SMatthias Ringwald     little_endian_store_16(request, 1, start_handle);
386f8fbdce0SMatthias Ringwald     little_endian_store_16(request, 3, end_handle);
387f8fbdce0SMatthias Ringwald     little_endian_store_16(request, 5, uuid16);
3883deb3ec6SMatthias Ringwald 
3896e7b444cSMatthias Ringwald     return gatt_client_send(gatt_client, 7);
3903deb3ec6SMatthias Ringwald }
3913deb3ec6SMatthias Ringwald 
3923deb3ec6SMatthias Ringwald // precondition: can_send_packet_now == TRUE
3936e7b444cSMatthias Ringwald static uint8_t
3946e7b444cSMatthias Ringwald att_read_by_type_or_group_request_for_uuid128(gatt_client_t *gatt_client, uint8_t request_type, const uint8_t *uuid128,
3956e7b444cSMatthias Ringwald                                               uint16_t start_handle, uint16_t end_handle) {
3969228fd32SMatthias Ringwald     uint8_t *request = gatt_client_reserve_request_buffer(gatt_client);
3976e7b444cSMatthias Ringwald 
3983deb3ec6SMatthias Ringwald     request[0] = request_type;
399f8fbdce0SMatthias Ringwald     little_endian_store_16(request, 1, start_handle);
400f8fbdce0SMatthias Ringwald     little_endian_store_16(request, 3, end_handle);
4019c80e4ccSMatthias Ringwald     reverse_128(uuid128, &request[5]);
4023deb3ec6SMatthias Ringwald 
4036e7b444cSMatthias Ringwald     return gatt_client_send(gatt_client, 21);
4043deb3ec6SMatthias Ringwald }
4053deb3ec6SMatthias Ringwald 
4063deb3ec6SMatthias Ringwald // precondition: can_send_packet_now == TRUE
4076e7b444cSMatthias Ringwald static uint8_t att_read_request(gatt_client_t *gatt_client, uint8_t request_type, uint16_t attribute_handle) {
4089228fd32SMatthias Ringwald     uint8_t *request = gatt_client_reserve_request_buffer(gatt_client);
4096e7b444cSMatthias Ringwald 
4103deb3ec6SMatthias Ringwald     request[0] = request_type;
411f8fbdce0SMatthias Ringwald     little_endian_store_16(request, 1, attribute_handle);
4123deb3ec6SMatthias Ringwald 
4136e7b444cSMatthias Ringwald     return gatt_client_send(gatt_client, 3);
4143deb3ec6SMatthias Ringwald }
4153deb3ec6SMatthias Ringwald 
4163deb3ec6SMatthias Ringwald // precondition: can_send_packet_now == TRUE
4176e7b444cSMatthias Ringwald static uint8_t att_read_blob_request(gatt_client_t *gatt_client, uint8_t request_type, uint16_t attribute_handle,
4186e7b444cSMatthias Ringwald                                      uint16_t value_offset) {
4199228fd32SMatthias Ringwald     uint8_t *request = gatt_client_reserve_request_buffer(gatt_client);
4209228fd32SMatthias Ringwald 
4213deb3ec6SMatthias Ringwald     request[0] = request_type;
422f8fbdce0SMatthias Ringwald     little_endian_store_16(request, 1, attribute_handle);
423f8fbdce0SMatthias Ringwald     little_endian_store_16(request, 3, value_offset);
4243deb3ec6SMatthias Ringwald 
4256e7b444cSMatthias Ringwald     return gatt_client_send(gatt_client, 5);
4263deb3ec6SMatthias Ringwald }
4273deb3ec6SMatthias Ringwald 
4286e7b444cSMatthias Ringwald static uint8_t
429f125a8efSMatthias Ringwald att_read_multiple_request_with_opcode(gatt_client_t *gatt_client, uint16_t num_value_handles, uint16_t *value_handles, uint8_t opcode) {
4309228fd32SMatthias Ringwald     uint8_t *request = gatt_client_reserve_request_buffer(gatt_client);
4319228fd32SMatthias Ringwald 
432f125a8efSMatthias Ringwald     request[0] = opcode;
4339228fd32SMatthias Ringwald     uint16_t i;
4349228fd32SMatthias Ringwald     uint16_t offset = 1;
4353deb3ec6SMatthias Ringwald     for (i=0;i<num_value_handles;i++){
436f8fbdce0SMatthias Ringwald         little_endian_store_16(request, offset, value_handles[i]);
4373deb3ec6SMatthias Ringwald         offset += 2;
4383deb3ec6SMatthias Ringwald     }
43925b7c058SMilanka Ringwald 
4406e7b444cSMatthias Ringwald     return gatt_client_send(gatt_client, offset);
4413deb3ec6SMatthias Ringwald }
4423deb3ec6SMatthias Ringwald 
443f125a8efSMatthias Ringwald static uint8_t
444f125a8efSMatthias Ringwald att_read_multiple_request(gatt_client_t *gatt_client, uint16_t num_value_handles, uint16_t *value_handles) {
445f125a8efSMatthias Ringwald     return att_read_multiple_request_with_opcode(gatt_client, num_value_handles, value_handles, ATT_READ_MULTIPLE_REQUEST);
446f125a8efSMatthias Ringwald }
447f125a8efSMatthias Ringwald 
448f125a8efSMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT
449f125a8efSMatthias Ringwald static uint8_t
450f125a8efSMatthias Ringwald att_read_multiple_variable_request(gatt_client_t *gatt_client, uint16_t num_value_handles, uint16_t *value_handles) {
451f125a8efSMatthias Ringwald     return att_read_multiple_request_with_opcode(gatt_client, num_value_handles, value_handles, ATT_READ_MULTIPLE_VARIABLE_REQ);
452f125a8efSMatthias Ringwald }
453f125a8efSMatthias Ringwald #endif
454f125a8efSMatthias Ringwald 
4557a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
4563deb3ec6SMatthias Ringwald // precondition: can_send_packet_now == TRUE
4576e7b444cSMatthias Ringwald static uint8_t att_signed_write_request(gatt_client_t *gatt_client, uint16_t request_type, uint16_t attribute_handle,
4586e7b444cSMatthias Ringwald                                         uint16_t value_length, uint8_t *value, uint32_t sign_counter, uint8_t sgn[8]) {
4599228fd32SMatthias Ringwald     uint8_t *request = gatt_client_reserve_request_buffer(gatt_client);
4609228fd32SMatthias Ringwald 
4613deb3ec6SMatthias Ringwald     request[0] = request_type;
462f8fbdce0SMatthias Ringwald     little_endian_store_16(request, 1, attribute_handle);
4636535961aSMatthias Ringwald     (void)memcpy(&request[3], value, value_length);
464f8fbdce0SMatthias Ringwald     little_endian_store_32(request, 3 + value_length, sign_counter);
4659c80e4ccSMatthias Ringwald     reverse_64(sgn, &request[3 + value_length + 4]);
46625b7c058SMilanka Ringwald 
4676e7b444cSMatthias Ringwald     return gatt_client_send(gatt_client, 3 + value_length + 12);
4683deb3ec6SMatthias Ringwald }
4697a766ebfSMatthias Ringwald #endif
4703deb3ec6SMatthias Ringwald 
4713deb3ec6SMatthias Ringwald // precondition: can_send_packet_now == TRUE
4726e7b444cSMatthias Ringwald static uint8_t
4736e7b444cSMatthias Ringwald att_write_request(gatt_client_t *gatt_client, uint8_t request_type, uint16_t attribute_handle, uint16_t value_length,
4746e7b444cSMatthias Ringwald                   uint8_t *value) {
4759228fd32SMatthias Ringwald     uint8_t *request = gatt_client_reserve_request_buffer(gatt_client);
4769228fd32SMatthias Ringwald 
4773deb3ec6SMatthias Ringwald     request[0] = request_type;
478f8fbdce0SMatthias Ringwald     little_endian_store_16(request, 1, attribute_handle);
4796535961aSMatthias Ringwald     (void)memcpy(&request[3], value, value_length);
4803deb3ec6SMatthias Ringwald 
4816e7b444cSMatthias Ringwald     return gatt_client_send(gatt_client, 3u + value_length);
4823deb3ec6SMatthias Ringwald }
4833deb3ec6SMatthias Ringwald 
4843deb3ec6SMatthias Ringwald // precondition: can_send_packet_now == TRUE
4856e7b444cSMatthias Ringwald static uint8_t att_execute_write_request(gatt_client_t *gatt_client, uint8_t request_type, uint8_t execute_write) {
4869228fd32SMatthias Ringwald     uint8_t *request = gatt_client_reserve_request_buffer(gatt_client);
4879228fd32SMatthias Ringwald 
4883deb3ec6SMatthias Ringwald     request[0] = request_type;
4893deb3ec6SMatthias Ringwald     request[1] = execute_write;
49025b7c058SMilanka Ringwald 
4916e7b444cSMatthias Ringwald     return gatt_client_send(gatt_client, 2);
4923deb3ec6SMatthias Ringwald }
4933deb3ec6SMatthias Ringwald 
4943deb3ec6SMatthias Ringwald // precondition: can_send_packet_now == TRUE
4956e7b444cSMatthias Ringwald static uint8_t att_prepare_write_request(gatt_client_t *gatt_client, uint8_t request_type, uint16_t attribute_handle,
4966e7b444cSMatthias Ringwald                                          uint16_t value_offset, uint16_t blob_length, uint8_t *value) {
4979228fd32SMatthias Ringwald     uint8_t *request = gatt_client_reserve_request_buffer(gatt_client);
4989228fd32SMatthias Ringwald 
4993deb3ec6SMatthias Ringwald     request[0] = request_type;
500f8fbdce0SMatthias Ringwald     little_endian_store_16(request, 1, attribute_handle);
501f8fbdce0SMatthias Ringwald     little_endian_store_16(request, 3, value_offset);
5026535961aSMatthias Ringwald     (void)memcpy(&request[5], &value[value_offset], blob_length);
5033deb3ec6SMatthias Ringwald 
5046e7b444cSMatthias Ringwald     return gatt_client_send(gatt_client,  5u + blob_length);
5053deb3ec6SMatthias Ringwald }
5063deb3ec6SMatthias Ringwald 
5076e7b444cSMatthias Ringwald static uint8_t att_exchange_mtu_request(gatt_client_t *gatt_client) {
5089228fd32SMatthias Ringwald     uint8_t *request = gatt_client_reserve_request_buffer(gatt_client);
5099228fd32SMatthias Ringwald 
5103deb3ec6SMatthias Ringwald     request[0] = ATT_EXCHANGE_MTU_REQUEST;
5119228fd32SMatthias Ringwald     uint16_t mtu = l2cap_max_le_mtu();
512f8fbdce0SMatthias Ringwald     little_endian_store_16(request, 1, mtu);
51325b7c058SMilanka Ringwald 
5146e7b444cSMatthias Ringwald     return gatt_client_send(gatt_client, 3);
5153deb3ec6SMatthias Ringwald }
5163deb3ec6SMatthias Ringwald 
5175cf1669fSMatthias Ringwald static uint16_t write_blob_length(gatt_client_t * gatt_client){
518dda77937SMatthias Ringwald     uint16_t max_blob_length = gatt_client->mtu - 5u;
5195cf1669fSMatthias Ringwald     if (gatt_client->attribute_offset >= gatt_client->attribute_length) {
5203deb3ec6SMatthias Ringwald         return 0;
5213deb3ec6SMatthias Ringwald     }
5225cf1669fSMatthias Ringwald     uint16_t rest_length = gatt_client->attribute_length - gatt_client->attribute_offset;
5233deb3ec6SMatthias Ringwald     if (max_blob_length > rest_length){
5243deb3ec6SMatthias Ringwald         return rest_length;
5253deb3ec6SMatthias Ringwald     }
5263deb3ec6SMatthias Ringwald     return max_blob_length;
5273deb3ec6SMatthias Ringwald }
5283deb3ec6SMatthias Ringwald 
5295cf1669fSMatthias Ringwald static void send_gatt_services_request(gatt_client_t *gatt_client){
5306e7b444cSMatthias Ringwald     att_read_by_type_or_group_request_for_uuid16(gatt_client, ATT_READ_BY_GROUP_TYPE_REQUEST,
5316e7b444cSMatthias Ringwald                                                  gatt_client->uuid16, gatt_client->start_group_handle,
5326e7b444cSMatthias Ringwald                                                  gatt_client->end_group_handle);
5333deb3ec6SMatthias Ringwald }
5343deb3ec6SMatthias Ringwald 
5355cf1669fSMatthias Ringwald static void send_gatt_by_uuid_request(gatt_client_t *gatt_client, uint16_t attribute_group_type){
53652377058SMatthias Ringwald     if (gatt_client->uuid16 != 0u){
5373deb3ec6SMatthias Ringwald         uint8_t uuid16[2];
5385cf1669fSMatthias Ringwald         little_endian_store_16(uuid16, 0, gatt_client->uuid16);
5396e7b444cSMatthias Ringwald         att_find_by_type_value_request(gatt_client, ATT_FIND_BY_TYPE_VALUE_REQUEST, attribute_group_type,
5406e7b444cSMatthias Ringwald                                        gatt_client->start_group_handle, gatt_client->end_group_handle, uuid16, 2);
5413deb3ec6SMatthias Ringwald         return;
5423deb3ec6SMatthias Ringwald     }
5433deb3ec6SMatthias Ringwald     uint8_t uuid128[16];
5445cf1669fSMatthias Ringwald     reverse_128(gatt_client->uuid128, uuid128);
5456e7b444cSMatthias Ringwald     att_find_by_type_value_request(gatt_client, ATT_FIND_BY_TYPE_VALUE_REQUEST, attribute_group_type,
5466e7b444cSMatthias Ringwald                                    gatt_client->start_group_handle, gatt_client->end_group_handle, uuid128, 16);
5473deb3ec6SMatthias Ringwald }
5483deb3ec6SMatthias Ringwald 
5495cf1669fSMatthias Ringwald static void send_gatt_services_by_uuid_request(gatt_client_t *gatt_client){
5505cf1669fSMatthias Ringwald     send_gatt_by_uuid_request(gatt_client, GATT_PRIMARY_SERVICE_UUID);
5513deb3ec6SMatthias Ringwald }
5523deb3ec6SMatthias Ringwald 
5535cf1669fSMatthias Ringwald static void send_gatt_included_service_uuid_request(gatt_client_t *gatt_client){
5546e7b444cSMatthias Ringwald     att_read_request(gatt_client, ATT_READ_REQUEST, gatt_client->query_start_handle);
5553deb3ec6SMatthias Ringwald }
5563deb3ec6SMatthias Ringwald 
5575cf1669fSMatthias Ringwald static void send_gatt_included_service_request(gatt_client_t *gatt_client){
5586e7b444cSMatthias Ringwald     att_read_by_type_or_group_request_for_uuid16(gatt_client, ATT_READ_BY_TYPE_REQUEST,
5596e7b444cSMatthias Ringwald                                                  GATT_INCLUDE_SERVICE_UUID, gatt_client->start_group_handle,
5606e7b444cSMatthias Ringwald                                                  gatt_client->end_group_handle);
5613deb3ec6SMatthias Ringwald }
5623deb3ec6SMatthias Ringwald 
5635cf1669fSMatthias Ringwald static void send_gatt_characteristic_request(gatt_client_t *gatt_client){
5646e7b444cSMatthias Ringwald     att_read_by_type_or_group_request_for_uuid16(gatt_client, ATT_READ_BY_TYPE_REQUEST,
5656e7b444cSMatthias Ringwald                                                  GATT_CHARACTERISTICS_UUID, gatt_client->start_group_handle,
5666e7b444cSMatthias Ringwald                                                  gatt_client->end_group_handle);
5673deb3ec6SMatthias Ringwald }
5683deb3ec6SMatthias Ringwald 
5695cf1669fSMatthias Ringwald static void send_gatt_characteristic_descriptor_request(gatt_client_t *gatt_client){
5706e7b444cSMatthias Ringwald     att_find_information_request(gatt_client, ATT_FIND_INFORMATION_REQUEST, gatt_client->start_group_handle,
5716e7b444cSMatthias Ringwald                                  gatt_client->end_group_handle);
5723deb3ec6SMatthias Ringwald }
5733deb3ec6SMatthias Ringwald 
5745cf1669fSMatthias Ringwald static void send_gatt_read_characteristic_value_request(gatt_client_t *gatt_client){
5756e7b444cSMatthias Ringwald     att_read_request(gatt_client, ATT_READ_REQUEST, gatt_client->attribute_handle);
5763deb3ec6SMatthias Ringwald }
5773deb3ec6SMatthias Ringwald 
5785cf1669fSMatthias Ringwald static void send_gatt_read_by_type_request(gatt_client_t * gatt_client){
57952377058SMatthias Ringwald     if (gatt_client->uuid16 != 0u){
5806e7b444cSMatthias Ringwald         att_read_by_type_or_group_request_for_uuid16(gatt_client, ATT_READ_BY_TYPE_REQUEST,
5816e7b444cSMatthias Ringwald                                                      gatt_client->uuid16, gatt_client->start_group_handle,
5826e7b444cSMatthias Ringwald                                                      gatt_client->end_group_handle);
5833deb3ec6SMatthias Ringwald     } else {
5846e7b444cSMatthias Ringwald         att_read_by_type_or_group_request_for_uuid128(gatt_client, ATT_READ_BY_TYPE_REQUEST,
5856e7b444cSMatthias Ringwald                                                       gatt_client->uuid128, gatt_client->start_group_handle,
5866e7b444cSMatthias Ringwald                                                       gatt_client->end_group_handle);
5873deb3ec6SMatthias Ringwald     }
5883deb3ec6SMatthias Ringwald }
5893deb3ec6SMatthias Ringwald 
5905cf1669fSMatthias Ringwald static void send_gatt_read_blob_request(gatt_client_t *gatt_client){
59130952227SMilanka Ringwald     if (gatt_client->attribute_offset == 0){
5926e7b444cSMatthias Ringwald         att_read_request(gatt_client, ATT_READ_REQUEST, gatt_client->attribute_handle);
59330952227SMilanka Ringwald     } else {
5946e7b444cSMatthias Ringwald         att_read_blob_request(gatt_client, ATT_READ_BLOB_REQUEST, gatt_client->attribute_handle,
5956e7b444cSMatthias Ringwald                               gatt_client->attribute_offset);
5963deb3ec6SMatthias Ringwald     }
59730952227SMilanka Ringwald }
5983deb3ec6SMatthias Ringwald 
5995cf1669fSMatthias Ringwald static void send_gatt_read_multiple_request(gatt_client_t * gatt_client){
6006e7b444cSMatthias Ringwald     att_read_multiple_request(gatt_client, gatt_client->read_multiple_handle_count, gatt_client->read_multiple_handles);
6013deb3ec6SMatthias Ringwald }
6023deb3ec6SMatthias Ringwald 
603f125a8efSMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT
604f125a8efSMatthias Ringwald static void send_gatt_read_multiple_variable_request(gatt_client_t * gatt_client){
605f125a8efSMatthias Ringwald     att_read_multiple_variable_request(gatt_client, gatt_client->read_multiple_handle_count, gatt_client->read_multiple_handles);
606f125a8efSMatthias Ringwald }
607f125a8efSMatthias Ringwald #endif
608f125a8efSMatthias Ringwald 
6095cf1669fSMatthias Ringwald static void send_gatt_write_attribute_value_request(gatt_client_t * gatt_client){
6106e7b444cSMatthias Ringwald     att_write_request(gatt_client, ATT_WRITE_REQUEST, gatt_client->attribute_handle, gatt_client->attribute_length,
6116e7b444cSMatthias Ringwald                       gatt_client->attribute_value);
6123deb3ec6SMatthias Ringwald }
6133deb3ec6SMatthias Ringwald 
6145cf1669fSMatthias Ringwald static void send_gatt_write_client_characteristic_configuration_request(gatt_client_t * gatt_client){
6156e7b444cSMatthias Ringwald     att_write_request(gatt_client, ATT_WRITE_REQUEST, gatt_client->client_characteristic_configuration_handle, 2,
6166e7b444cSMatthias Ringwald                       gatt_client->client_characteristic_configuration_value);
6173deb3ec6SMatthias Ringwald }
6183deb3ec6SMatthias Ringwald 
6195cf1669fSMatthias Ringwald static void send_gatt_prepare_write_request(gatt_client_t * gatt_client){
6206e7b444cSMatthias Ringwald     att_prepare_write_request(gatt_client, ATT_PREPARE_WRITE_REQUEST, gatt_client->attribute_handle,
6216e7b444cSMatthias Ringwald                               gatt_client->attribute_offset, write_blob_length(gatt_client),
6226e7b444cSMatthias Ringwald                               gatt_client->attribute_value);
6233deb3ec6SMatthias Ringwald }
6243deb3ec6SMatthias Ringwald 
6255cf1669fSMatthias Ringwald static void send_gatt_execute_write_request(gatt_client_t * gatt_client){
6266e7b444cSMatthias Ringwald     att_execute_write_request(gatt_client, ATT_EXECUTE_WRITE_REQUEST, 1);
6273deb3ec6SMatthias Ringwald }
6283deb3ec6SMatthias Ringwald 
6295cf1669fSMatthias Ringwald static void send_gatt_cancel_prepared_write_request(gatt_client_t * gatt_client){
6306e7b444cSMatthias Ringwald     att_execute_write_request(gatt_client, ATT_EXECUTE_WRITE_REQUEST, 0);
6313deb3ec6SMatthias Ringwald }
6323deb3ec6SMatthias Ringwald 
633abdc9fb5SMatthias Ringwald #ifndef ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERY
6345cf1669fSMatthias Ringwald static void send_gatt_read_client_characteristic_configuration_request(gatt_client_t * gatt_client){
6356e7b444cSMatthias Ringwald     att_read_by_type_or_group_request_for_uuid16(gatt_client, ATT_READ_BY_TYPE_REQUEST,
6366e7b444cSMatthias Ringwald                                                  GATT_CLIENT_CHARACTERISTICS_CONFIGURATION,
6376e7b444cSMatthias Ringwald                                                  gatt_client->start_group_handle, gatt_client->end_group_handle);
6383deb3ec6SMatthias Ringwald }
639abdc9fb5SMatthias Ringwald #endif
6403deb3ec6SMatthias Ringwald 
6415cf1669fSMatthias Ringwald static void send_gatt_read_characteristic_descriptor_request(gatt_client_t * gatt_client){
6426e7b444cSMatthias Ringwald     att_read_request(gatt_client, ATT_READ_REQUEST, gatt_client->attribute_handle);
6433deb3ec6SMatthias Ringwald }
6443deb3ec6SMatthias Ringwald 
6457a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
6465cf1669fSMatthias Ringwald static void send_gatt_signed_write_request(gatt_client_t * gatt_client, uint32_t sign_counter){
6476e7b444cSMatthias Ringwald     att_signed_write_request(gatt_client, ATT_SIGNED_WRITE_COMMAND, gatt_client->attribute_handle,
6486e7b444cSMatthias Ringwald                              gatt_client->attribute_length, gatt_client->attribute_value, sign_counter,
6496e7b444cSMatthias Ringwald                              gatt_client->cmac);
6503deb3ec6SMatthias Ringwald }
6517a766ebfSMatthias Ringwald #endif
6523deb3ec6SMatthias Ringwald 
6533deb3ec6SMatthias Ringwald static uint16_t get_last_result_handle_from_service_list(uint8_t * packet, uint16_t size){
65439ac9711SMatthias Ringwald     if (size < 2) return 0xffff;
6553deb3ec6SMatthias Ringwald     uint8_t attr_length = packet[1];
65639ac9711SMatthias Ringwald     if ((2 + attr_length) > size) return 0xffff;
6574ea43905SMatthias Ringwald     return little_endian_read_16(packet, size - attr_length + 2u);
6583deb3ec6SMatthias Ringwald }
6593deb3ec6SMatthias Ringwald 
6603deb3ec6SMatthias Ringwald static uint16_t get_last_result_handle_from_characteristics_list(uint8_t * packet, uint16_t size){
66139ac9711SMatthias Ringwald     if (size < 2) return 0xffff;
6623deb3ec6SMatthias Ringwald     uint8_t attr_length = packet[1];
66339ac9711SMatthias Ringwald     if ((2 + attr_length) > size) return 0xffff;
6644ea43905SMatthias Ringwald     return little_endian_read_16(packet, size - attr_length + 3u);
6653deb3ec6SMatthias Ringwald }
6663deb3ec6SMatthias Ringwald 
6673deb3ec6SMatthias Ringwald static uint16_t get_last_result_handle_from_included_services_list(uint8_t * packet, uint16_t size){
66839ac9711SMatthias Ringwald     if (size < 2) return 0xffff;
6693deb3ec6SMatthias Ringwald     uint8_t attr_length = packet[1];
67039ac9711SMatthias Ringwald     if ((2 + attr_length) > size) return 0xffff;
671f8fbdce0SMatthias Ringwald     return little_endian_read_16(packet, size - attr_length);
6723deb3ec6SMatthias Ringwald }
6733deb3ec6SMatthias Ringwald 
674842492f0SMatthias Ringwald #ifdef ENABLE_GATT_CLIENT_SERVICE_CHANGED
67558e8c9f5SMatthias Ringwald static void gatt_client_service_emit_event(gatt_client_t * gatt_client, uint8_t * event, uint16_t size){
67658e8c9f5SMatthias Ringwald     btstack_linked_list_iterator_t it;
67758e8c9f5SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &gatt_client_service_changed_handler);
67858e8c9f5SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)) {
67958e8c9f5SMatthias Ringwald         btstack_packet_callback_registration_t *callback = (btstack_packet_callback_registration_t *) btstack_linked_list_iterator_next(&it);
68058e8c9f5SMatthias Ringwald         (*callback->callback)(HCI_EVENT_PACKET, (uint16_t) gatt_client->con_handle, event, size);
68158e8c9f5SMatthias Ringwald     }
68258e8c9f5SMatthias Ringwald }
68358e8c9f5SMatthias Ringwald 
68458e8c9f5SMatthias Ringwald static void
68558e8c9f5SMatthias Ringwald gatt_client_service_emit_database_hash(gatt_client_t *gatt_client, const uint8_t *value, uint16_t value_len) {
68658e8c9f5SMatthias Ringwald     if (value_len == 16){
68758e8c9f5SMatthias Ringwald         uint8_t event[21];
68858e8c9f5SMatthias Ringwald         hci_event_builder_context_t context;
68958e8c9f5SMatthias Ringwald         hci_event_builder_init(&context, event, sizeof(event), HCI_EVENT_GATTSERVICE_META, GATTSERVICE_SUBEVENT_GATT_DATABASE_HASH);
69058e8c9f5SMatthias Ringwald         hci_event_builder_add_con_handle(&context, gatt_client->con_handle);
69158e8c9f5SMatthias Ringwald         hci_event_builder_add_bytes(&context, value, 16);
69258e8c9f5SMatthias Ringwald         gatt_client_service_emit_event(gatt_client, event, hci_event_builder_get_length(&context));
69358e8c9f5SMatthias Ringwald     }
69458e8c9f5SMatthias Ringwald }
69558e8c9f5SMatthias Ringwald 
69658e8c9f5SMatthias Ringwald static void
69758e8c9f5SMatthias Ringwald gatt_client_service_emit_service_changed(gatt_client_t *gatt_client, const uint8_t *value, uint16_t value_len) {
69858e8c9f5SMatthias Ringwald     if (value_len == 4){
69958e8c9f5SMatthias Ringwald         uint8_t event[9];
70058e8c9f5SMatthias Ringwald         hci_event_builder_context_t context;
70158e8c9f5SMatthias Ringwald         hci_event_builder_init(&context, event, sizeof(event), HCI_EVENT_GATTSERVICE_META, GATTSERVICE_SUBEVENT_GATT_SERVICE_CHANGED);
70258e8c9f5SMatthias Ringwald         hci_event_builder_add_con_handle(&context, gatt_client->con_handle);
70358e8c9f5SMatthias Ringwald         hci_event_builder_add_bytes(&context, value, 4);
70458e8c9f5SMatthias Ringwald         gatt_client_service_emit_event(gatt_client, event, hci_event_builder_get_length(&context));
70558e8c9f5SMatthias Ringwald     }
70658e8c9f5SMatthias Ringwald }
70758e8c9f5SMatthias Ringwald 
708d9ced76fSMatthias Ringwald static void gatt_client_service_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
709e2c98440SMatthias Ringwald     UNUSED(channel);  // ok: handling own l2cap events
710e2c98440SMatthias Ringwald     UNUSED(size);     // ok: there is no channel
711e2c98440SMatthias Ringwald 
712d9ced76fSMatthias Ringwald     hci_con_handle_t con_handle;
713d9ced76fSMatthias Ringwald     gatt_client_t *gatt_client;
714d9ced76fSMatthias Ringwald     gatt_client_service_t service;
715d9ced76fSMatthias Ringwald     gatt_client_characteristic_t characteristic;
716d9ced76fSMatthias Ringwald     switch (packet_type) {
717d9ced76fSMatthias Ringwald         case HCI_EVENT_PACKET:
718d9ced76fSMatthias Ringwald             switch (hci_event_packet_get_type(packet)) {
719d9ced76fSMatthias Ringwald                 case GATT_EVENT_SERVICE_QUERY_RESULT:
720d9ced76fSMatthias Ringwald                     con_handle = gatt_event_service_query_result_get_handle(packet);
721d9ced76fSMatthias Ringwald                     gatt_client = gatt_client_get_context_for_handle(con_handle);
722d9ced76fSMatthias Ringwald                     btstack_assert(gatt_client != NULL);
723d9ced76fSMatthias Ringwald                     btstack_assert(gatt_client->gatt_service_state == GATT_CLIENT_SERVICE_DISCOVER_W4_DONE);
724d9ced76fSMatthias Ringwald                     gatt_event_service_query_result_get_service(packet, &service);
725d9ced76fSMatthias Ringwald                     gatt_client->gatt_service_start_group_handle = service.start_group_handle;
726d9ced76fSMatthias Ringwald                     gatt_client->gatt_service_end_group_handle = service.end_group_handle;
727d9ced76fSMatthias Ringwald                     break;
728d9ced76fSMatthias Ringwald                 case GATT_EVENT_CHARACTERISTIC_QUERY_RESULT:
729d9ced76fSMatthias Ringwald                     con_handle = gatt_event_characteristic_query_result_get_handle(packet);
730d9ced76fSMatthias Ringwald                     gatt_client = gatt_client_get_context_for_handle(con_handle);
731d9ced76fSMatthias Ringwald                     btstack_assert(gatt_client != NULL);
732d9ced76fSMatthias Ringwald                     btstack_assert(gatt_client->gatt_service_state == GATT_CLIENT_SERVICE_DISCOVER_CHARACTERISTICS_W4_DONE);
733d9ced76fSMatthias Ringwald                     gatt_event_characteristic_query_result_get_characteristic(packet, &characteristic);
734d9ced76fSMatthias Ringwald                     switch (characteristic.uuid16){
735d9ced76fSMatthias Ringwald                         case ORG_BLUETOOTH_CHARACTERISTIC_GATT_SERVICE_CHANGED:
736d9ced76fSMatthias Ringwald                             gatt_client->gatt_service_changed_value_handle = characteristic.value_handle;
737d9ced76fSMatthias Ringwald                             gatt_client->gatt_service_changed_end_handle = characteristic.end_handle;
738d9ced76fSMatthias Ringwald                             break;
739d9ced76fSMatthias Ringwald                         case ORG_BLUETOOTH_CHARACTERISTIC_DATABASE_HASH:
740d9ced76fSMatthias Ringwald                             gatt_client->gatt_service_database_hash_value_handle = characteristic.value_handle;
741d9ced76fSMatthias Ringwald                             gatt_client->gatt_service_database_hash_end_handle = characteristic.end_handle;
742d9ced76fSMatthias Ringwald                             break;
743d9ced76fSMatthias Ringwald                         default:
744d9ced76fSMatthias Ringwald                             break;
745d9ced76fSMatthias Ringwald                     }
746d9ced76fSMatthias Ringwald                     break;
747d9ced76fSMatthias Ringwald                 case GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT:
748d9ced76fSMatthias Ringwald                     con_handle = gatt_event_characteristic_value_query_result_get_handle(packet);
749d9ced76fSMatthias Ringwald                     gatt_client = gatt_client_get_context_for_handle(con_handle);
750d9ced76fSMatthias Ringwald                     btstack_assert(gatt_client != NULL);
751d9ced76fSMatthias Ringwald                     btstack_assert(gatt_client->gatt_service_state == GATT_CLIENT_SERVICE_DATABASE_HASH_READ_W4_DONE);
75258e8c9f5SMatthias Ringwald                         gatt_client_service_emit_database_hash(gatt_client,
75358e8c9f5SMatthias Ringwald                                                                gatt_event_characteristic_value_query_result_get_value(packet),
75458e8c9f5SMatthias Ringwald                                                                gatt_event_characteristic_value_query_result_get_value_length(packet));
755d9ced76fSMatthias Ringwald                     break;
756d9ced76fSMatthias Ringwald                 case GATT_EVENT_QUERY_COMPLETE:
757d9ced76fSMatthias Ringwald                     con_handle = gatt_event_query_complete_get_handle(packet);
758d9ced76fSMatthias Ringwald                     gatt_client = gatt_client_get_context_for_handle(con_handle);
759d9ced76fSMatthias Ringwald                     btstack_assert(gatt_client != NULL);
760d9ced76fSMatthias Ringwald                     switch (gatt_client->gatt_service_state) {
761d9ced76fSMatthias Ringwald                         case GATT_CLIENT_SERVICE_DISCOVER_W4_DONE:
762d9ced76fSMatthias Ringwald                             gatt_client->gatt_service_state = GATT_CLIENT_SERVICE_DISCOVER_CHARACTERISTICS_W2_SEND;
763d9ced76fSMatthias Ringwald                             break;
764d9ced76fSMatthias Ringwald                         case GATT_CLIENT_SERVICE_DISCOVER_CHARACTERISTICS_W4_DONE:
765d9ced76fSMatthias Ringwald                             gatt_client->gatt_service_state = GATT_CLIENT_SERVICE_SERVICE_CHANGED_WRITE_CCCD_W2_SEND;
766d9ced76fSMatthias Ringwald                             break;
767d9ced76fSMatthias Ringwald                         case GATT_CLIENT_SERVICE_SERVICE_CHANGED_WRITE_CCCD_W4_DONE:
768d9ced76fSMatthias Ringwald                             gatt_client->gatt_service_state = GATT_CLIENT_SERVICE_DATABASE_HASH_READ_W2_SEND;
769d9ced76fSMatthias Ringwald                             break;
770d9ced76fSMatthias Ringwald                         case GATT_CLIENT_SERVICE_DATABASE_HASH_READ_W4_DONE:
771d9ced76fSMatthias Ringwald                             gatt_client->gatt_service_state = GATT_CLIENT_SERVICE_DATABASE_HASH_WRITE_CCCD_W2_SEND;
772d9ced76fSMatthias Ringwald                             break;
773d9ced76fSMatthias Ringwald                         case GATT_CLIENT_SERVICE_DATABASE_HASH_WRITE_CCCD_W4_DONE:
774d9ced76fSMatthias Ringwald                             gatt_client->gatt_service_state = GATT_CLIENT_SERVICE_DONE;
775d9ced76fSMatthias Ringwald                             break;
776d9ced76fSMatthias Ringwald                         default:
777d9ced76fSMatthias Ringwald                             btstack_unreachable();
778d9ced76fSMatthias Ringwald                             break;
779d9ced76fSMatthias Ringwald                     }
780d9ced76fSMatthias Ringwald                     break;
781d9ced76fSMatthias Ringwald                 default:
782d9ced76fSMatthias Ringwald                     break;
783d9ced76fSMatthias Ringwald             }
784d9ced76fSMatthias Ringwald             break;
785d9ced76fSMatthias Ringwald         default:
786d9ced76fSMatthias Ringwald             break;
787d9ced76fSMatthias Ringwald     }
788d9ced76fSMatthias Ringwald }
789842492f0SMatthias Ringwald #endif
790d9ced76fSMatthias Ringwald 
79153e9c18fSMatthias Ringwald static void gatt_client_notify_can_send_query(gatt_client_t * gatt_client){
79219c614acSMatthias Ringwald 
79319c614acSMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT
79419c614acSMatthias Ringwald     // if eatt is ready, notify all clients that can send a query
79519c614acSMatthias Ringwald     if (gatt_client->eatt_state == GATT_CLIENT_EATT_READY){
79619c614acSMatthias Ringwald         btstack_linked_list_iterator_t it;
79719c614acSMatthias Ringwald         btstack_linked_list_iterator_init(&it, &gatt_client->eatt_clients);
79819c614acSMatthias Ringwald         while (btstack_linked_list_iterator_has_next(&it)){
79919c614acSMatthias Ringwald             gatt_client_t * client = (gatt_client_t *) btstack_linked_list_iterator_next(&it);
800052dc82aSMatthias Ringwald             if (client->state == P_READY){
80119c614acSMatthias Ringwald                 // call callback
80219c614acSMatthias Ringwald                 btstack_context_callback_registration_t * callback = (btstack_context_callback_registration_t *) btstack_linked_list_pop(&gatt_client->query_requests);
80319c614acSMatthias Ringwald                 if (callback == NULL) {
80419c614acSMatthias Ringwald                     return;
80519c614acSMatthias Ringwald                 }
80619c614acSMatthias Ringwald                 (*callback->callback)(callback->context);
80719c614acSMatthias Ringwald             }
80819c614acSMatthias Ringwald         }
80919c614acSMatthias Ringwald         return;
81019c614acSMatthias Ringwald     }
81119c614acSMatthias Ringwald #endif
81219c614acSMatthias Ringwald 
813842492f0SMatthias Ringwald     while (gatt_client->state == P_READY){
814842492f0SMatthias Ringwald         bool query_sent = false;
815842492f0SMatthias Ringwald         UNUSED(query_sent);
816842492f0SMatthias Ringwald 
817842492f0SMatthias Ringwald #ifdef ENABLE_GATT_CLIENT_SERVICE_CHANGED
818d9ced76fSMatthias Ringwald         uint8_t status = ERROR_CODE_SUCCESS;
819d9ced76fSMatthias Ringwald         gatt_client_service_t gatt_service;
820d9ced76fSMatthias Ringwald         gatt_client_characteristic_t characteristic;
821d9ced76fSMatthias Ringwald         switch (gatt_client->gatt_service_state){
822d9ced76fSMatthias Ringwald             case GATT_CLIENT_SERVICE_DISCOVER_W2_SEND:
823d9ced76fSMatthias Ringwald                 gatt_client->gatt_service_state = GATT_CLIENT_SERVICE_DISCOVER_W4_DONE;
824d9ced76fSMatthias Ringwald                 status = gatt_client_discover_primary_services_by_uuid16(&gatt_client_service_packet_handler,
825d9ced76fSMatthias Ringwald                                                                                          gatt_client->con_handle,
826d9ced76fSMatthias Ringwald                                                                                          ORG_BLUETOOTH_SERVICE_GENERIC_ATTRIBUTE);
827d9ced76fSMatthias Ringwald                 query_sent = true;
828d9ced76fSMatthias Ringwald                 break;
829d9ced76fSMatthias Ringwald             case GATT_CLIENT_SERVICE_DISCOVER_CHARACTERISTICS_W2_SEND:
830d9ced76fSMatthias Ringwald                 if (gatt_client->gatt_service_start_group_handle != 0){
831d9ced76fSMatthias Ringwald                     gatt_client->gatt_service_state = GATT_CLIENT_SERVICE_DISCOVER_CHARACTERISTICS_W4_DONE;
832d9ced76fSMatthias Ringwald                     gatt_service.start_group_handle = gatt_client->gatt_service_start_group_handle;
833d9ced76fSMatthias Ringwald                     gatt_service.end_group_handle   = gatt_client->gatt_service_end_group_handle;
834d9ced76fSMatthias Ringwald                     status = gatt_client_discover_characteristics_for_service(&gatt_client_service_packet_handler, gatt_client->con_handle, &gatt_service);
835d9ced76fSMatthias Ringwald                     query_sent = true;
836d9ced76fSMatthias Ringwald                     break;
837d9ced76fSMatthias Ringwald                 }
838d9ced76fSMatthias Ringwald 
839d9ced76fSMatthias Ringwald                 /* fall through */
840d9ced76fSMatthias Ringwald 
841d9ced76fSMatthias Ringwald             case GATT_CLIENT_SERVICE_SERVICE_CHANGED_WRITE_CCCD_W2_SEND:
842d9ced76fSMatthias Ringwald                 if (gatt_client->gatt_service_changed_value_handle != 0){
843d9ced76fSMatthias Ringwald                     gatt_client->gatt_service_state = GATT_CLIENT_SERVICE_SERVICE_CHANGED_WRITE_CCCD_W4_DONE;
844d9ced76fSMatthias Ringwald                     characteristic.value_handle = gatt_client->gatt_service_changed_value_handle;
845d9ced76fSMatthias Ringwald                     characteristic.end_handle   = gatt_client->gatt_service_changed_end_handle;
846d9ced76fSMatthias Ringwald                     // we assume good case. We cannot do much otherwise
847d9ced76fSMatthias Ringwald                     characteristic.properties = ATT_PROPERTY_INDICATE;
848d9ced76fSMatthias Ringwald                     status = gatt_client_write_client_characteristic_configuration(&gatt_client_service_packet_handler,
849d9ced76fSMatthias Ringwald                                                                                    gatt_client->con_handle, &characteristic,
850d9ced76fSMatthias Ringwald                                                                                    GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_INDICATION);
851d9ced76fSMatthias Ringwald                     query_sent = true;
852d9ced76fSMatthias Ringwald                     break;
853d9ced76fSMatthias Ringwald                 }
854d9ced76fSMatthias Ringwald 
855d9ced76fSMatthias Ringwald                 /* fall through */
856d9ced76fSMatthias Ringwald 
857d9ced76fSMatthias Ringwald             case GATT_CLIENT_SERVICE_DATABASE_HASH_READ_W2_SEND:
858d9ced76fSMatthias Ringwald                 if (gatt_client->gatt_service_database_hash_value_handle != 0){
859d9ced76fSMatthias Ringwald                     gatt_client->gatt_service_state = GATT_CLIENT_SERVICE_DATABASE_HASH_READ_W4_DONE;
860d9ced76fSMatthias Ringwald                     status = gatt_client_read_value_of_characteristics_by_uuid16(&gatt_client_service_packet_handler,
861d9ced76fSMatthias Ringwald                                                                                          gatt_client->con_handle,
862d9ced76fSMatthias Ringwald                                                                                          0x0001, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_DATABASE_HASH);
863d9ced76fSMatthias Ringwald                     query_sent = true;
864d9ced76fSMatthias Ringwald                     break;
865d9ced76fSMatthias Ringwald                 }
866d9ced76fSMatthias Ringwald 
867d9ced76fSMatthias Ringwald                 /* fall through */
868d9ced76fSMatthias Ringwald 
869d9ced76fSMatthias Ringwald             case GATT_CLIENT_SERVICE_DATABASE_HASH_WRITE_CCCD_W2_SEND:
870d9ced76fSMatthias Ringwald                 if (gatt_client->gatt_service_database_hash_value_handle != 0) {
871d9ced76fSMatthias Ringwald                     gatt_client->gatt_service_state = GATT_CLIENT_SERVICE_DATABASE_HASH_WRITE_CCCD_W4_DONE;
872d9ced76fSMatthias Ringwald                     characteristic.value_handle = gatt_client->gatt_service_database_hash_value_handle;
873d9ced76fSMatthias Ringwald                     characteristic.end_handle = gatt_client->gatt_service_database_hash_end_handle;
874d9ced76fSMatthias Ringwald                     // we assume good case. We cannot do much otherwise
875d9ced76fSMatthias Ringwald                     characteristic.properties = ATT_PROPERTY_INDICATE;
876d9ced76fSMatthias Ringwald                     status = gatt_client_write_client_characteristic_configuration(&gatt_client_service_packet_handler,
877d9ced76fSMatthias Ringwald                                                                                    gatt_client->con_handle,
878d9ced76fSMatthias Ringwald                                                                                    &characteristic,
879d9ced76fSMatthias Ringwald                                                                                    GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_INDICATION);
880d9ced76fSMatthias Ringwald                     query_sent = true;
881d9ced76fSMatthias Ringwald                     break;
882d9ced76fSMatthias Ringwald                 }
883d9ced76fSMatthias Ringwald 
884d9ced76fSMatthias Ringwald                 // DONE
885d9ced76fSMatthias Ringwald                 gatt_client->gatt_service_state = GATT_CLIENT_SERVICE_DONE;
886d9ced76fSMatthias Ringwald                 break;
887d9ced76fSMatthias Ringwald             default:
888d9ced76fSMatthias Ringwald                 break;
889d9ced76fSMatthias Ringwald         }
890d9ced76fSMatthias Ringwald         btstack_assert(status == ERROR_CODE_SUCCESS);
891d9ced76fSMatthias Ringwald         UNUSED(status);
892d9ced76fSMatthias Ringwald         if (query_sent){
893d9ced76fSMatthias Ringwald             continue;
894d9ced76fSMatthias Ringwald         }
895842492f0SMatthias Ringwald #endif
896d9ced76fSMatthias Ringwald 
89726166ecfSMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT
898d9ced76fSMatthias Ringwald         query_sent = gatt_client_le_enhanced_handle_can_send_query(gatt_client);
89926166ecfSMatthias Ringwald         if (query_sent){
90026166ecfSMatthias Ringwald             continue;
90126166ecfSMatthias Ringwald         }
90226166ecfSMatthias Ringwald #endif
90353e9c18fSMatthias Ringwald         btstack_context_callback_registration_t * callback = (btstack_context_callback_registration_t *) btstack_linked_list_pop(&gatt_client->query_requests);
90409834412SMatthias Ringwald         if (callback == NULL) {
90509834412SMatthias Ringwald             return;
90653e9c18fSMatthias Ringwald         }
90709834412SMatthias Ringwald         (*callback->callback)(callback->context);
90853e9c18fSMatthias Ringwald     }
90953e9c18fSMatthias Ringwald }
91053e9c18fSMatthias Ringwald 
91123d583b8SMatthias Ringwald // test if notification/indication should be delivered to application (BLESA)
91223d583b8SMatthias Ringwald static bool gatt_client_accept_server_message(gatt_client_t *gatt_client) {
91323d583b8SMatthias Ringwald     // ignore messages until re-encryption is complete
91423d583b8SMatthias Ringwald     if (gap_reconnect_security_setup_active(gatt_client->con_handle)) return false;
91523d583b8SMatthias Ringwald 
91623d583b8SMatthias Ringwald 	// after that ignore if bonded but not encrypted
91723d583b8SMatthias Ringwald 	return !gap_bonded(gatt_client->con_handle) || (gap_encryption_key_size(gatt_client->con_handle) > 0);
91823d583b8SMatthias Ringwald }
91923d583b8SMatthias Ringwald 
9209c662c9bSMatthias Ringwald static void emit_event_new(btstack_packet_handler_t callback, uint8_t * packet, uint16_t size){
9219c662c9bSMatthias Ringwald     if (!callback) return;
9229da9850bSMatthias Ringwald     hci_dump_btstack_event(packet, size);
9239c662c9bSMatthias Ringwald     (*callback)(HCI_EVENT_PACKET, 0, packet, size);
9243deb3ec6SMatthias Ringwald }
9253deb3ec6SMatthias Ringwald 
926711e6c80SMatthias Ringwald static void emit_event_to_registered_listeners(hci_con_handle_t con_handle, uint16_t attribute_handle, uint8_t * packet, uint16_t size){
927665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
9289c662c9bSMatthias Ringwald     btstack_linked_list_iterator_init(&it, &gatt_client_value_listeners);
929665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
93086c38559SMatthias Ringwald         gatt_client_notification_t * notification = (gatt_client_notification_t*) btstack_linked_list_iterator_next(&it);
931b3f03c84SMatthias Ringwald         if ((notification->con_handle       != GATT_CLIENT_ANY_CONNECTION)   && (notification->con_handle       != con_handle)) continue;
932b3f03c84SMatthias Ringwald         if ((notification->attribute_handle != GATT_CLIENT_ANY_VALUE_HANDLE) && (notification->attribute_handle != attribute_handle)) continue;
93386c38559SMatthias Ringwald         (*notification->callback)(HCI_EVENT_PACKET, 0, packet, size);
9343deb3ec6SMatthias Ringwald     }
9353deb3ec6SMatthias Ringwald }
9363deb3ec6SMatthias Ringwald 
9375cf1669fSMatthias Ringwald static void emit_gatt_complete_event(gatt_client_t * gatt_client, uint8_t att_status){
93888371317SMatthias Ringwald     // @format H122
93988371317SMatthias Ringwald     uint8_t packet[9];
9406a88036eSMatthias Ringwald     hci_event_builder_context_t context;
9416a88036eSMatthias Ringwald     hci_event_builder_init(&context, packet, sizeof(packet), GATT_EVENT_QUERY_COMPLETE, 0);
9426a88036eSMatthias Ringwald     hci_event_builder_add_con_handle(&context, gatt_client->con_handle);
94388371317SMatthias Ringwald     hci_event_builder_add_16(&context, gatt_client->service_id);
94488371317SMatthias Ringwald     hci_event_builder_add_16(&context, gatt_client->connection_id);
9456a88036eSMatthias Ringwald     hci_event_builder_add_08(&context, att_status);
9466a88036eSMatthias Ringwald     emit_event_new(gatt_client->callback, packet, hci_event_builder_get_length(&context));
9473deb3ec6SMatthias Ringwald }
9483deb3ec6SMatthias Ringwald 
949045d700dSDavid Lechner static void emit_gatt_service_query_result_event(gatt_client_t * gatt_client, uint16_t start_group_handle, uint16_t end_group_handle, const uint8_t * uuid128){
950521f5820SMatthias Ringwald     // @format H22X
951521f5820SMatthias Ringwald     uint8_t packet[28];
952c668d4cfSMatthias Ringwald     hci_event_builder_context_t context;
953c668d4cfSMatthias Ringwald     hci_event_builder_init(&context, packet, sizeof(packet), GATT_EVENT_SERVICE_QUERY_RESULT, 0);
954c668d4cfSMatthias Ringwald     hci_event_builder_add_con_handle(&context, gatt_client->con_handle);
955521f5820SMatthias Ringwald     hci_event_builder_add_16(&context, gatt_client->service_id);
956521f5820SMatthias Ringwald     hci_event_builder_add_16(&context, gatt_client->connection_id);
957c668d4cfSMatthias Ringwald     hci_event_builder_add_16(&context, start_group_handle);
958c668d4cfSMatthias Ringwald     hci_event_builder_add_16(&context, end_group_handle);
959c668d4cfSMatthias Ringwald     hci_event_builder_add_128(&context, uuid128);
960c668d4cfSMatthias Ringwald     emit_event_new(gatt_client->callback, packet, hci_event_builder_get_length(&context));
9613deb3ec6SMatthias Ringwald }
9623deb3ec6SMatthias Ringwald 
963045d700dSDavid Lechner static void emit_gatt_included_service_query_result_event(gatt_client_t * gatt_client, uint16_t include_handle, uint16_t start_group_handle, uint16_t end_group_handle, const uint8_t * uuid128){
964d578995fSMatthias Ringwald     // @format H22X
965d578995fSMatthias Ringwald     uint8_t packet[30];
966d8ef101dSMatthias Ringwald     hci_event_builder_context_t context;
967d8ef101dSMatthias Ringwald     hci_event_builder_init(&context, packet, sizeof(packet), GATT_EVENT_INCLUDED_SERVICE_QUERY_RESULT, 0);
968d8ef101dSMatthias Ringwald     hci_event_builder_add_con_handle(&context, gatt_client->con_handle);
969d578995fSMatthias Ringwald     hci_event_builder_add_16(&context, gatt_client->service_id);
970d578995fSMatthias Ringwald     hci_event_builder_add_16(&context, gatt_client->connection_id);
971d8ef101dSMatthias Ringwald     hci_event_builder_add_16(&context, include_handle);
972d8ef101dSMatthias Ringwald     hci_event_builder_add_16(&context, start_group_handle);
973d8ef101dSMatthias Ringwald     hci_event_builder_add_16(&context, end_group_handle);
974d8ef101dSMatthias Ringwald     hci_event_builder_add_128(&context, uuid128);
975d8ef101dSMatthias Ringwald     emit_event_new(gatt_client->callback, packet, hci_event_builder_get_length(&context));
9763deb3ec6SMatthias Ringwald }
9773deb3ec6SMatthias Ringwald 
9785cf1669fSMatthias Ringwald static void emit_gatt_characteristic_query_result_event(gatt_client_t * gatt_client, uint16_t start_handle, uint16_t value_handle, uint16_t end_handle,
979045d700dSDavid Lechner                                                         uint16_t properties, const uint8_t * uuid128){
980185497a5SMatthias Ringwald     // @format H22Y
981185497a5SMatthias Ringwald     uint8_t packet[32];
982b5a7d6a2SMatthias Ringwald     hci_event_builder_context_t context;
983b5a7d6a2SMatthias Ringwald     hci_event_builder_init(&context, packet, sizeof(packet), GATT_EVENT_CHARACTERISTIC_QUERY_RESULT, 0);
984b5a7d6a2SMatthias Ringwald     hci_event_builder_add_con_handle(&context, gatt_client->con_handle);
985185497a5SMatthias Ringwald     hci_event_builder_add_16(&context, gatt_client->service_id);
986185497a5SMatthias Ringwald     hci_event_builder_add_16(&context, gatt_client->connection_id);
987b5a7d6a2SMatthias Ringwald     hci_event_builder_add_16(&context, start_handle);
988b5a7d6a2SMatthias Ringwald     hci_event_builder_add_16(&context, value_handle);
989b5a7d6a2SMatthias Ringwald     hci_event_builder_add_16(&context, end_handle);
990b5a7d6a2SMatthias Ringwald     hci_event_builder_add_16(&context, properties);
991b5a7d6a2SMatthias Ringwald     hci_event_builder_add_128(&context, uuid128);
992b5a7d6a2SMatthias Ringwald     emit_event_new(gatt_client->callback, packet, hci_event_builder_get_length(&context));
9933deb3ec6SMatthias Ringwald }
9943deb3ec6SMatthias Ringwald 
9953deb3ec6SMatthias Ringwald static void emit_gatt_all_characteristic_descriptors_result_event(
996045d700dSDavid Lechner         gatt_client_t * gatt_client, uint16_t descriptor_handle, const uint8_t * uuid128){
9975a6c9e2aSMatthias Ringwald     // @format H22Z
9985a6c9e2aSMatthias Ringwald     uint8_t packet[26];
9991cfd1613SMatthias Ringwald     hci_event_builder_context_t context;
10001cfd1613SMatthias Ringwald     hci_event_builder_init(&context, packet, sizeof(packet), GATT_EVENT_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY_RESULT, 0);
10011cfd1613SMatthias Ringwald     hci_event_builder_add_con_handle(&context, gatt_client->con_handle);
10025a6c9e2aSMatthias Ringwald     hci_event_builder_add_16(&context, gatt_client->service_id);
10035a6c9e2aSMatthias Ringwald     hci_event_builder_add_16(&context, gatt_client->connection_id);
10041cfd1613SMatthias Ringwald     hci_event_builder_add_16(&context, descriptor_handle);
10051cfd1613SMatthias Ringwald     hci_event_builder_add_128(&context, uuid128);
10061cfd1613SMatthias Ringwald     emit_event_new(gatt_client->callback, packet, hci_event_builder_get_length(&context));
10073deb3ec6SMatthias Ringwald }
10083deb3ec6SMatthias Ringwald 
10095cf1669fSMatthias Ringwald static void emit_gatt_mtu_exchanged_result_event(gatt_client_t * gatt_client, uint16_t new_mtu){
10108f37572aSJakob Krantz     // @format H2
10118f37572aSJakob Krantz     uint8_t packet[6];
10128f37572aSJakob Krantz     packet[0] = GATT_EVENT_MTU;
10134ea43905SMatthias Ringwald     packet[1] = sizeof(packet) - 2u;
10145cf1669fSMatthias Ringwald     little_endian_store_16(packet, 2, gatt_client->con_handle);
10158f37572aSJakob Krantz     little_endian_store_16(packet, 4, new_mtu);
10165cf1669fSMatthias Ringwald     att_dispatch_client_mtu_exchanged(gatt_client->con_handle, new_mtu);
10175cf1669fSMatthias Ringwald     emit_event_new(gatt_client->callback, packet, sizeof(packet));
10188f37572aSJakob Krantz }
101923d583b8SMatthias Ringwald 
102023d583b8SMatthias Ringwald // helper
102123d583b8SMatthias Ringwald static void gatt_client_handle_transaction_complete(gatt_client_t *gatt_client, uint8_t att_status) {
1022052dc82aSMatthias Ringwald     gatt_client->state = P_READY;
102323d583b8SMatthias Ringwald     gatt_client_timeout_stop(gatt_client);
102423d583b8SMatthias Ringwald     emit_gatt_complete_event(gatt_client, att_status);
102523d583b8SMatthias Ringwald     gatt_client_notify_can_send_query(gatt_client);
102623d583b8SMatthias Ringwald }
102723d583b8SMatthias Ringwald 
102823d583b8SMatthias Ringwald // @return packet pointer
1029*f6a28e25SMatthias Ringwald // @note assume that value is part of an l2cap buffer - overwrite HCI + L2CAP packet headers + 4 pre_buffer bytes
1030*f6a28e25SMatthias Ringwald #define CHARACTERISTIC_VALUE_EVENT_HEADER_SIZE 12
10318450fbf5SMatthias Ringwald static uint8_t *
10328450fbf5SMatthias Ringwald setup_characteristic_value_packet(const gatt_client_t *gatt_client, uint8_t type, uint16_t attribute_handle,
10338450fbf5SMatthias Ringwald                                   uint8_t *value, uint16_t length) {
103423d583b8SMatthias Ringwald #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
103523d583b8SMatthias Ringwald     // copy value into test packet for testing
103623d583b8SMatthias Ringwald     static uint8_t packet[1000];
1037f51e6883SMatthias Ringwald     memcpy(&packet[CHARACTERISTIC_VALUE_EVENT_HEADER_SIZE], value, length);
103823d583b8SMatthias Ringwald #else
103923d583b8SMatthias Ringwald     // before the value inside the ATT PDU
104023d583b8SMatthias Ringwald     uint8_t * packet = value - CHARACTERISTIC_VALUE_EVENT_HEADER_SIZE;
104123d583b8SMatthias Ringwald #endif
104223d583b8SMatthias Ringwald     packet[0] = type;
104323d583b8SMatthias Ringwald     packet[1] = CHARACTERISTIC_VALUE_EVENT_HEADER_SIZE - 2 + length;
10448450fbf5SMatthias Ringwald     little_endian_store_16(packet, 2, gatt_client->con_handle);
1045*f6a28e25SMatthias Ringwald     little_endian_store_16(packet, 4, gatt_client->service_id);
1046*f6a28e25SMatthias Ringwald     little_endian_store_16(packet, 6, gatt_client->connection_id);
1047*f6a28e25SMatthias Ringwald     little_endian_store_16(packet, 8, attribute_handle);
1048*f6a28e25SMatthias Ringwald     little_endian_store_16(packet, 10, length);
104923d583b8SMatthias Ringwald     return packet;
105023d583b8SMatthias Ringwald }
105123d583b8SMatthias Ringwald 
105223d583b8SMatthias Ringwald // @return packet pointer
1053b641e2afSMatthias Ringwald // @note assume that value is part of an l2cap buffer - overwrite HCI + L2CAP packet headers + 6 pre_buffer bytes
1054b641e2afSMatthias Ringwald #define LONG_CHARACTERISTIC_VALUE_EVENT_HEADER_SIZE 14
1055f51e6883SMatthias Ringwald 
1056f51e6883SMatthias Ringwald // L2CAP Header (4) + ACL Header (4) => 8 bytes
1057f51e6883SMatthias Ringwald #if !defined(HCI_INCOMING_PRE_BUFFER_SIZE) || ((HCI_INCOMING_PRE_BUFFER_SIZE < LONG_CHARACTERISTIC_VALUE_EVENT_HEADER_SIZE - 8))
1058f51e6883SMatthias Ringwald #error "Long Characteristic reads requires HCI_INCOMING_PRE_BUFFER_SIZE >= 2"
1059f51e6883SMatthias Ringwald #endif
1060f51e6883SMatthias Ringwald 
10613e78e462SMatthias Ringwald static uint8_t *
10623e78e462SMatthias Ringwald setup_long_characteristic_value_packet(const gatt_client_t *gatt_client, uint8_t type, uint16_t attribute_handle,
10633e78e462SMatthias Ringwald                                        uint16_t offset, uint8_t *value, uint16_t length) {
106423d583b8SMatthias Ringwald #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
106523d583b8SMatthias Ringwald     // avoid using pre ATT headers.
1066464b6e7bSMatthias Ringwald     // copy value into test packet for testing
1067464b6e7bSMatthias Ringwald     static uint8_t packet[1000];
1068464b6e7bSMatthias Ringwald     memcpy(&packet[LONG_CHARACTERISTIC_VALUE_EVENT_HEADER_SIZE], value, length);
1069464b6e7bSMatthias Ringwald #else
107023d583b8SMatthias Ringwald     // before the value inside the ATT PDU
107123d583b8SMatthias Ringwald     uint8_t * packet = value - LONG_CHARACTERISTIC_VALUE_EVENT_HEADER_SIZE;
1072464b6e7bSMatthias Ringwald #endif
107323d583b8SMatthias Ringwald     packet[0] = type;
107423d583b8SMatthias Ringwald     packet[1] = LONG_CHARACTERISTIC_VALUE_EVENT_HEADER_SIZE - 2 + length;
10753e78e462SMatthias Ringwald     little_endian_store_16(packet, 2, gatt_client->con_handle);
1076b641e2afSMatthias Ringwald     little_endian_store_16(packet, 4, gatt_client->service_id);
1077b641e2afSMatthias Ringwald     little_endian_store_16(packet, 6, gatt_client->connection_id);
1078b641e2afSMatthias Ringwald     little_endian_store_16(packet, 8, attribute_handle);
1079b641e2afSMatthias Ringwald     little_endian_store_16(packet, 10, offset);
1080b641e2afSMatthias Ringwald     little_endian_store_16(packet, 12, length);
108123d583b8SMatthias Ringwald     return packet;
108223d583b8SMatthias Ringwald }
108323d583b8SMatthias Ringwald 
1084f7a42e72SMatthias Ringwald #if (LONG_CHARACTERISTIC_VALUE_EVENT_HEADER_SIZE > CHARACTERISTIC_VALUE_EVENT_HEADER_SIZE)
1085f7a42e72SMatthias Ringwald #define REPORT_PREBUFFER_HEADER LONG_CHARACTERISTIC_VALUE_EVENT_HEADER_SIZE
1086f7a42e72SMatthias Ringwald #else
1087f7a42e72SMatthias Ringwald #define REPORT_PREBUFFER_HEADER CHARACTERISTIC_VALUE_EVENT_HEADER_SIZE
1088f7a42e72SMatthias Ringwald #endif
1089f7a42e72SMatthias Ringwald 
10908f37572aSJakob Krantz ///
10915cf1669fSMatthias Ringwald static void report_gatt_services(gatt_client_t * gatt_client, uint8_t * packet, uint16_t size){
109239ac9711SMatthias Ringwald     if (size < 2) return;
10933deb3ec6SMatthias Ringwald     uint8_t attr_length = packet[1];
10944ea43905SMatthias Ringwald     uint8_t uuid_length = attr_length - 4u;
10953deb3ec6SMatthias Ringwald 
10963deb3ec6SMatthias Ringwald     int i;
109739ac9711SMatthias Ringwald     for (i = 2; (i+attr_length) <= size; i += attr_length){
1098f8fbdce0SMatthias Ringwald         uint16_t start_group_handle = little_endian_read_16(packet,i);
1099f8fbdce0SMatthias Ringwald         uint16_t end_group_handle   = little_endian_read_16(packet,i+2);
11003deb3ec6SMatthias Ringwald         uint8_t  uuid128[16];
11013deb3ec6SMatthias Ringwald         uint16_t uuid16 = 0;
11023deb3ec6SMatthias Ringwald 
11034ea43905SMatthias Ringwald         if (uuid_length == 2u){
1104f8fbdce0SMatthias Ringwald             uuid16 = little_endian_read_16(packet, i+4);
1105e1a125dfSMatthias Ringwald             uuid_add_bluetooth_prefix((uint8_t*) &uuid128, uuid16);
110639ac9711SMatthias Ringwald         } else if (uuid_length == 16u) {
11079c80e4ccSMatthias Ringwald             reverse_128(&packet[i+4], uuid128);
110839ac9711SMatthias Ringwald         } else {
110939ac9711SMatthias Ringwald             return;
11103deb3ec6SMatthias Ringwald         }
11115cf1669fSMatthias Ringwald         emit_gatt_service_query_result_event(gatt_client, start_group_handle, end_group_handle, uuid128);
11123deb3ec6SMatthias Ringwald     }
11133deb3ec6SMatthias Ringwald }
11143deb3ec6SMatthias Ringwald 
111523d583b8SMatthias Ringwald static void report_gatt_characteristic_start_found(gatt_client_t * gatt_client, uint16_t start_handle, uint8_t properties, uint16_t value_handle, uint8_t * uuid, uint16_t uuid_length){
11163deb3ec6SMatthias Ringwald     uint8_t uuid128[16];
11173deb3ec6SMatthias Ringwald     uint16_t uuid16 = 0;
11184ea43905SMatthias Ringwald     if (uuid_length == 2u){
1119f8fbdce0SMatthias Ringwald         uuid16 = little_endian_read_16(uuid, 0);
1120e1a125dfSMatthias Ringwald         uuid_add_bluetooth_prefix((uint8_t*) uuid128, uuid16);
11214ea43905SMatthias Ringwald     } else if (uuid_length == 16u){
11229c80e4ccSMatthias Ringwald         reverse_128(uuid, uuid128);
1123d73c9dbeSMilanka Ringwald     } else {
1124d73c9dbeSMilanka Ringwald         return;
11253deb3ec6SMatthias Ringwald     }
11263deb3ec6SMatthias Ringwald 
11275cf1669fSMatthias Ringwald     if (gatt_client->filter_with_uuid && (memcmp(gatt_client->uuid128, uuid128, 16) != 0)) return;
11283deb3ec6SMatthias Ringwald 
11295cf1669fSMatthias Ringwald     gatt_client->characteristic_properties = properties;
11305cf1669fSMatthias Ringwald     gatt_client->characteristic_start_handle = start_handle;
11315cf1669fSMatthias Ringwald     gatt_client->attribute_handle = value_handle;
11323deb3ec6SMatthias Ringwald 
11335cf1669fSMatthias Ringwald     if (gatt_client->filter_with_uuid) return;
11343deb3ec6SMatthias Ringwald 
11355cf1669fSMatthias Ringwald     gatt_client->uuid16 = uuid16;
11365cf1669fSMatthias Ringwald     (void)memcpy(gatt_client->uuid128, uuid128, 16);
11373deb3ec6SMatthias Ringwald }
11383deb3ec6SMatthias Ringwald 
113923d583b8SMatthias Ringwald static void report_gatt_characteristic_end_found(gatt_client_t * gatt_client, uint16_t end_handle){
11403deb3ec6SMatthias Ringwald     // TODO: stop searching if filter and uuid found
11413deb3ec6SMatthias Ringwald 
11425cf1669fSMatthias Ringwald     if (!gatt_client->characteristic_start_handle) return;
11433deb3ec6SMatthias Ringwald 
11445cf1669fSMatthias Ringwald     emit_gatt_characteristic_query_result_event(gatt_client, gatt_client->characteristic_start_handle, gatt_client->attribute_handle,
11455cf1669fSMatthias Ringwald                                                 end_handle, gatt_client->characteristic_properties, gatt_client->uuid128);
11463deb3ec6SMatthias Ringwald 
11475cf1669fSMatthias Ringwald     gatt_client->characteristic_start_handle = 0;
11483deb3ec6SMatthias Ringwald }
11493deb3ec6SMatthias Ringwald 
115023d583b8SMatthias Ringwald 
11515cf1669fSMatthias Ringwald static void report_gatt_characteristics(gatt_client_t * gatt_client, uint8_t * packet, uint16_t size){
11524ea43905SMatthias Ringwald     if (size < 2u) return;
11533deb3ec6SMatthias Ringwald     uint8_t attr_length = packet[1];
11544ea43905SMatthias Ringwald     if ((attr_length != 7u) && (attr_length != 21u)) return;
11554ea43905SMatthias Ringwald     uint8_t uuid_length = attr_length - 5u;
11563deb3ec6SMatthias Ringwald     int i;
11574ea43905SMatthias Ringwald     for (i = 2u; (i + attr_length) <= size; i += attr_length){
1158f8fbdce0SMatthias Ringwald         uint16_t start_handle = little_endian_read_16(packet, i);
11593deb3ec6SMatthias Ringwald         uint8_t  properties = packet[i+2];
1160f8fbdce0SMatthias Ringwald         uint16_t value_handle = little_endian_read_16(packet, i+3);
116123d583b8SMatthias Ringwald         report_gatt_characteristic_end_found(gatt_client, start_handle - 1u);
116223d583b8SMatthias Ringwald         report_gatt_characteristic_start_found(gatt_client, start_handle, properties, value_handle, &packet[i + 5],
116323d583b8SMatthias Ringwald                                                uuid_length);
11643deb3ec6SMatthias Ringwald     }
11653deb3ec6SMatthias Ringwald }
11663deb3ec6SMatthias Ringwald 
11675cf1669fSMatthias Ringwald static void report_gatt_included_service_uuid16(gatt_client_t * gatt_client, uint16_t include_handle, uint16_t uuid16){
11683deb3ec6SMatthias Ringwald     uint8_t normalized_uuid128[16];
1169e1a125dfSMatthias Ringwald     uuid_add_bluetooth_prefix(normalized_uuid128, uuid16);
11705cf1669fSMatthias Ringwald     emit_gatt_included_service_query_result_event(gatt_client, include_handle, gatt_client->query_start_handle,
11715cf1669fSMatthias Ringwald                                                   gatt_client->query_end_handle, normalized_uuid128);
11723deb3ec6SMatthias Ringwald }
11733deb3ec6SMatthias Ringwald 
1174045d700dSDavid Lechner static void report_gatt_included_service_uuid128(gatt_client_t * gatt_client, uint16_t include_handle, const uint8_t * uuid128){
11755cf1669fSMatthias Ringwald     emit_gatt_included_service_query_result_event(gatt_client, include_handle, gatt_client->query_start_handle,
11765cf1669fSMatthias Ringwald                                                   gatt_client->query_end_handle, uuid128);
11773deb3ec6SMatthias Ringwald }
11783deb3ec6SMatthias Ringwald 
11793deb3ec6SMatthias Ringwald // @note assume that value is part of an l2cap buffer - overwrite parts of the HCI/L2CAP/ATT packet (4/4/3) bytes
11800fde3c5eSMatthias Ringwald static void report_gatt_notification(gatt_client_t *gatt_client, uint16_t value_handle, uint8_t *value, int length) {
11810fde3c5eSMatthias Ringwald 	if (!gatt_client_accept_server_message(gatt_client)) return;
11828450fbf5SMatthias Ringwald     uint8_t * packet = setup_characteristic_value_packet(gatt_client, GATT_EVENT_NOTIFICATION, value_handle,
11838450fbf5SMatthias Ringwald                                                          value, length);
118423d583b8SMatthias Ringwald     emit_event_to_registered_listeners(gatt_client->con_handle, value_handle, packet, CHARACTERISTIC_VALUE_EVENT_HEADER_SIZE + length);
11853deb3ec6SMatthias Ringwald }
11863deb3ec6SMatthias Ringwald 
11873deb3ec6SMatthias Ringwald // @note assume that value is part of an l2cap buffer - overwrite parts of the HCI/L2CAP/ATT packet (4/4/3) bytes
11880fde3c5eSMatthias Ringwald static void report_gatt_indication(gatt_client_t *gatt_client, uint16_t value_handle, uint8_t *value, int length) {
11890fde3c5eSMatthias Ringwald 	if (!gatt_client_accept_server_message(gatt_client)) return;
1190842492f0SMatthias Ringwald #ifdef ENABLE_GATT_CLIENT_SERVICE_CHANGED
119158e8c9f5SMatthias Ringwald     // Directly Handle GATT Service Changed and Database Hash indications
119258e8c9f5SMatthias Ringwald     if (value_handle == gatt_client->gatt_service_database_hash_value_handle){
119358e8c9f5SMatthias Ringwald         gatt_client_service_emit_database_hash(gatt_client, value, length);
119458e8c9f5SMatthias Ringwald     }
119558e8c9f5SMatthias Ringwald     if (value_handle == gatt_client->gatt_service_changed_value_handle){
119658e8c9f5SMatthias Ringwald         gatt_client_service_emit_service_changed(gatt_client, value, length);
119758e8c9f5SMatthias Ringwald     }
1198842492f0SMatthias Ringwald #endif
11998450fbf5SMatthias Ringwald     uint8_t * packet = setup_characteristic_value_packet(gatt_client, GATT_EVENT_INDICATION, value_handle,
12008450fbf5SMatthias Ringwald                                                          value, length);
120123d583b8SMatthias Ringwald     emit_event_to_registered_listeners(gatt_client->con_handle, value_handle, packet, CHARACTERISTIC_VALUE_EVENT_HEADER_SIZE + length);
12023deb3ec6SMatthias Ringwald }
12033deb3ec6SMatthias Ringwald 
12043deb3ec6SMatthias Ringwald // @note assume that value is part of an l2cap buffer - overwrite parts of the HCI/L2CAP/ATT packet (4/4/3) bytes
12055cf1669fSMatthias Ringwald static void report_gatt_characteristic_value(gatt_client_t * gatt_client, uint16_t attribute_handle, uint8_t * value, uint16_t length){
12068450fbf5SMatthias Ringwald     uint8_t * packet = setup_characteristic_value_packet(
12078450fbf5SMatthias Ringwald             gatt_client, GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT, attribute_handle, value, length);
120823d583b8SMatthias Ringwald     emit_event_new(gatt_client->callback, packet, CHARACTERISTIC_VALUE_EVENT_HEADER_SIZE + length);
12093deb3ec6SMatthias Ringwald }
12103deb3ec6SMatthias Ringwald 
12113deb3ec6SMatthias Ringwald // @note assume that value is part of an l2cap buffer - overwrite parts of the HCI/L2CAP/ATT packet (4/4/3) bytes
12125cf1669fSMatthias Ringwald static void report_gatt_long_characteristic_value_blob(gatt_client_t * gatt_client, uint16_t attribute_handle, uint8_t * blob, uint16_t blob_length, int value_offset){
12133e78e462SMatthias Ringwald     uint8_t * packet = setup_long_characteristic_value_packet(gatt_client,
12143e78e462SMatthias Ringwald                                                               GATT_EVENT_LONG_CHARACTERISTIC_VALUE_QUERY_RESULT,
12153e78e462SMatthias Ringwald                                                               attribute_handle, value_offset,
12163e78e462SMatthias Ringwald                                                               blob, blob_length);
121723d583b8SMatthias Ringwald     emit_event_new(gatt_client->callback, packet, blob_length + LONG_CHARACTERISTIC_VALUE_EVENT_HEADER_SIZE);
12183deb3ec6SMatthias Ringwald }
12193deb3ec6SMatthias Ringwald 
12205cf1669fSMatthias Ringwald static void report_gatt_characteristic_descriptor(gatt_client_t * gatt_client, uint16_t descriptor_handle, uint8_t *value, uint16_t value_length, uint16_t value_offset){
12219ec2630cSMatthias Ringwald     UNUSED(value_offset);
12228450fbf5SMatthias Ringwald     uint8_t * packet = setup_characteristic_value_packet(gatt_client, GATT_EVENT_CHARACTERISTIC_DESCRIPTOR_QUERY_RESULT,
12238450fbf5SMatthias Ringwald                                                          descriptor_handle, value,
12248450fbf5SMatthias Ringwald                                                          value_length);
12255cf1669fSMatthias Ringwald     emit_event_new(gatt_client->callback, packet, value_length + 8u);
12263deb3ec6SMatthias Ringwald }
12273deb3ec6SMatthias Ringwald 
12285cf1669fSMatthias Ringwald static void report_gatt_long_characteristic_descriptor(gatt_client_t * gatt_client, uint16_t descriptor_handle, uint8_t *blob, uint16_t blob_length, uint16_t value_offset){
12293e78e462SMatthias Ringwald     uint8_t * packet = setup_long_characteristic_value_packet(gatt_client,
12303e78e462SMatthias Ringwald                                                               GATT_EVENT_LONG_CHARACTERISTIC_DESCRIPTOR_QUERY_RESULT,
12313e78e462SMatthias Ringwald                                                               descriptor_handle, value_offset,
12323e78e462SMatthias Ringwald                                                               blob, blob_length);
123323d583b8SMatthias Ringwald     emit_event_new(gatt_client->callback, packet, blob_length + LONG_CHARACTERISTIC_VALUE_EVENT_HEADER_SIZE);
12343deb3ec6SMatthias Ringwald }
12353deb3ec6SMatthias Ringwald 
12365cf1669fSMatthias Ringwald static void report_gatt_all_characteristic_descriptors(gatt_client_t * gatt_client, uint8_t * packet, uint16_t size, uint16_t pair_size){
12373deb3ec6SMatthias Ringwald     int i;
12384ea43905SMatthias Ringwald     for (i = 0u; (i + pair_size) <= size; i += pair_size){
1239f8fbdce0SMatthias Ringwald         uint16_t descriptor_handle = little_endian_read_16(packet,i);
12403deb3ec6SMatthias Ringwald         uint8_t uuid128[16];
12413deb3ec6SMatthias Ringwald         uint16_t uuid16 = 0;
12424ea43905SMatthias Ringwald         if (pair_size == 4u){
1243f8fbdce0SMatthias Ringwald             uuid16 = little_endian_read_16(packet,i+2);
1244e1a125dfSMatthias Ringwald             uuid_add_bluetooth_prefix(uuid128, uuid16);
12453deb3ec6SMatthias Ringwald         } else {
12469c80e4ccSMatthias Ringwald             reverse_128(&packet[i+2], uuid128);
12473deb3ec6SMatthias Ringwald         }
12485cf1669fSMatthias Ringwald         emit_gatt_all_characteristic_descriptors_result_event(gatt_client, descriptor_handle, uuid128);
12493deb3ec6SMatthias Ringwald     }
12503deb3ec6SMatthias Ringwald 
12513deb3ec6SMatthias Ringwald }
12523deb3ec6SMatthias Ringwald 
125352377058SMatthias Ringwald static bool is_query_done(gatt_client_t * gatt_client, uint16_t last_result_handle){
12545cf1669fSMatthias Ringwald     return last_result_handle >= gatt_client->end_group_handle;
12553deb3ec6SMatthias Ringwald }
12563deb3ec6SMatthias Ringwald 
12575cf1669fSMatthias Ringwald static void trigger_next_query(gatt_client_t * gatt_client, uint16_t last_result_handle, gatt_client_state_t next_query_state){
12585cf1669fSMatthias Ringwald     if (is_query_done(gatt_client, last_result_handle)){
1259526859e7SMatthias Ringwald         gatt_client_handle_transaction_complete(gatt_client, ATT_ERROR_SUCCESS);
12603deb3ec6SMatthias Ringwald         return;
12613deb3ec6SMatthias Ringwald     }
12623deb3ec6SMatthias Ringwald     // next
12635cf1669fSMatthias Ringwald     gatt_client->start_group_handle = last_result_handle + 1u;
1264052dc82aSMatthias Ringwald     gatt_client->state = next_query_state;
12653deb3ec6SMatthias Ringwald }
12663deb3ec6SMatthias Ringwald 
12675cf1669fSMatthias Ringwald static void trigger_next_included_service_query(gatt_client_t * gatt_client, uint16_t last_result_handle){
12685cf1669fSMatthias Ringwald     trigger_next_query(gatt_client, last_result_handle, P_W2_SEND_INCLUDED_SERVICE_QUERY);
12693deb3ec6SMatthias Ringwald }
12703deb3ec6SMatthias Ringwald 
12715cf1669fSMatthias Ringwald static void trigger_next_service_query(gatt_client_t * gatt_client, uint16_t last_result_handle){
12725cf1669fSMatthias Ringwald     trigger_next_query(gatt_client, last_result_handle, P_W2_SEND_SERVICE_QUERY);
12733deb3ec6SMatthias Ringwald }
12743deb3ec6SMatthias Ringwald 
12755cf1669fSMatthias Ringwald static void trigger_next_service_by_uuid_query(gatt_client_t * gatt_client, uint16_t last_result_handle){
12765cf1669fSMatthias Ringwald     trigger_next_query(gatt_client, last_result_handle, P_W2_SEND_SERVICE_WITH_UUID_QUERY);
12773deb3ec6SMatthias Ringwald }
12783deb3ec6SMatthias Ringwald 
12795cf1669fSMatthias Ringwald static void trigger_next_characteristic_query(gatt_client_t * gatt_client, uint16_t last_result_handle){
12805cf1669fSMatthias Ringwald     if (is_query_done(gatt_client, last_result_handle)){
12813deb3ec6SMatthias Ringwald         // report last characteristic
128223d583b8SMatthias Ringwald         report_gatt_characteristic_end_found(gatt_client, gatt_client->end_group_handle);
12833deb3ec6SMatthias Ringwald     }
12845cf1669fSMatthias Ringwald     trigger_next_query(gatt_client, last_result_handle, P_W2_SEND_ALL_CHARACTERISTICS_OF_SERVICE_QUERY);
12853deb3ec6SMatthias Ringwald }
12863deb3ec6SMatthias Ringwald 
12875cf1669fSMatthias Ringwald static void trigger_next_characteristic_descriptor_query(gatt_client_t * gatt_client, uint16_t last_result_handle){
12885cf1669fSMatthias Ringwald     trigger_next_query(gatt_client, last_result_handle, P_W2_SEND_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY);
12893deb3ec6SMatthias Ringwald }
12903deb3ec6SMatthias Ringwald 
12915cf1669fSMatthias Ringwald static void trigger_next_read_by_type_query(gatt_client_t * gatt_client, uint16_t last_result_handle){
12925cf1669fSMatthias Ringwald     trigger_next_query(gatt_client, last_result_handle, P_W2_SEND_READ_BY_TYPE_REQUEST);
12933deb3ec6SMatthias Ringwald }
12943deb3ec6SMatthias Ringwald 
12955cf1669fSMatthias Ringwald static void trigger_next_prepare_write_query(gatt_client_t * gatt_client, gatt_client_state_t next_query_state, gatt_client_state_t done_state){
12965cf1669fSMatthias Ringwald     gatt_client->attribute_offset += write_blob_length(gatt_client);
12975cf1669fSMatthias Ringwald     uint16_t next_blob_length =  write_blob_length(gatt_client);
12983deb3ec6SMatthias Ringwald 
12994ea43905SMatthias Ringwald     if (next_blob_length == 0u){
1300052dc82aSMatthias Ringwald         gatt_client->state = done_state;
13013deb3ec6SMatthias Ringwald         return;
13023deb3ec6SMatthias Ringwald     }
1303052dc82aSMatthias Ringwald     gatt_client->state = next_query_state;
13043deb3ec6SMatthias Ringwald }
13053deb3ec6SMatthias Ringwald 
13065cf1669fSMatthias Ringwald static void trigger_next_blob_query(gatt_client_t * gatt_client, gatt_client_state_t next_query_state, uint16_t received_blob_length){
13073deb3ec6SMatthias Ringwald 
1308dda77937SMatthias Ringwald     uint16_t max_blob_length = gatt_client->mtu - 1u;
13093deb3ec6SMatthias Ringwald     if (received_blob_length < max_blob_length){
1310526859e7SMatthias Ringwald         gatt_client_handle_transaction_complete(gatt_client, ATT_ERROR_SUCCESS);
13113deb3ec6SMatthias Ringwald         return;
13123deb3ec6SMatthias Ringwald     }
13133deb3ec6SMatthias Ringwald 
13145cf1669fSMatthias Ringwald     gatt_client->attribute_offset += received_blob_length;
1315052dc82aSMatthias Ringwald     gatt_client->state = next_query_state;
13163deb3ec6SMatthias Ringwald }
13173deb3ec6SMatthias Ringwald 
131823d583b8SMatthias Ringwald void gatt_client_listen_for_characteristic_value_updates(gatt_client_notification_t * notification, btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_characteristic_t * characteristic){
131923d583b8SMatthias Ringwald     notification->callback = callback;
132023d583b8SMatthias Ringwald     notification->con_handle = con_handle;
132123d583b8SMatthias Ringwald     if (characteristic == NULL){
132223d583b8SMatthias Ringwald         notification->attribute_handle = GATT_CLIENT_ANY_VALUE_HANDLE;
132323d583b8SMatthias Ringwald     } else {
132423d583b8SMatthias Ringwald         notification->attribute_handle = characteristic->value_handle;
132523d583b8SMatthias Ringwald     }
132623d583b8SMatthias Ringwald     btstack_linked_list_add(&gatt_client_value_listeners, (btstack_linked_item_t*) notification);
132723d583b8SMatthias Ringwald }
132823d583b8SMatthias Ringwald 
132923d583b8SMatthias Ringwald void gatt_client_stop_listening_for_characteristic_value_updates(gatt_client_notification_t * notification){
133023d583b8SMatthias Ringwald     btstack_linked_list_remove(&gatt_client_value_listeners, (btstack_linked_item_t*) notification);
133123d583b8SMatthias Ringwald }
13323deb3ec6SMatthias Ringwald 
133362f7b41dSMatthias Ringwald static bool is_value_valid(gatt_client_t *gatt_client, uint8_t *packet, uint16_t size){
1334f8fbdce0SMatthias Ringwald     uint16_t attribute_handle = little_endian_read_16(packet, 1);
1335f8fbdce0SMatthias Ringwald     uint16_t value_offset = little_endian_read_16(packet, 3);
13363deb3ec6SMatthias Ringwald 
133762f7b41dSMatthias Ringwald     if (gatt_client->attribute_handle != attribute_handle) return false;
133862f7b41dSMatthias Ringwald     if (gatt_client->attribute_offset != value_offset) return false;
13395cf1669fSMatthias Ringwald     return memcmp(&gatt_client->attribute_value[gatt_client->attribute_offset], &packet[5], size - 5u) == 0u;
13403deb3ec6SMatthias Ringwald }
13413deb3ec6SMatthias Ringwald 
13420038504eSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
134392a7335eSMatthias Ringwald static void gatt_client_run_for_client_start_signed_write(gatt_client_t *gatt_client) {
134492a7335eSMatthias Ringwald     sm_key_t csrk;
134592a7335eSMatthias Ringwald     le_device_db_local_csrk_get(gatt_client->le_device_index, csrk);
134692a7335eSMatthias Ringwald     uint32_t sign_counter = le_device_db_local_counter_get(gatt_client->le_device_index);
1347052dc82aSMatthias Ringwald     gatt_client->state = P_W4_CMAC_RESULT;
134892a7335eSMatthias Ringwald     sm_cmac_signed_write_start(csrk, ATT_SIGNED_WRITE_COMMAND, gatt_client->attribute_handle, gatt_client->attribute_length, gatt_client->attribute_value, sign_counter, att_signed_write_handle_cmac_result);
134992a7335eSMatthias Ringwald }
13500038504eSMatthias Ringwald #endif
135192a7335eSMatthias Ringwald 
135292a7335eSMatthias Ringwald // returns true if packet was sent
13531979f09cSMatthias Ringwald static bool gatt_client_run_for_gatt_client(gatt_client_t * gatt_client){
13543deb3ec6SMatthias Ringwald 
1355e4d159baSMatthias Ringwald     // wait until re-encryption is complete
13561979f09cSMatthias Ringwald     if (gap_reconnect_security_setup_active(gatt_client->con_handle)) return false;
1357d1e1a57fSMatthias Ringwald 
1358e4d159baSMatthias Ringwald     // wait until re-encryption is complete
13591979f09cSMatthias Ringwald     if (gatt_client->reencryption_active) return false;
13606c124bc2SMatthias Ringwald 
136111279da7SMatthias Ringwald     // wait until pairing complete (either reactive authentication or due to required security level)
13621979f09cSMatthias Ringwald     if (gatt_client->wait_for_authentication_complete) return false;
136311279da7SMatthias Ringwald 
1364052dc82aSMatthias Ringwald     bool client_request_pending = gatt_client->state != P_READY;
1365fd14b205SMatthias Ringwald 
13661aa9e3e8SMatthias Ringwald     // verify security level for Mandatory Authentication over LE
136746012949SMatthias Ringwald     bool check_security;
136846012949SMatthias Ringwald     switch (gatt_client->bearer_type){
136946012949SMatthias Ringwald         case ATT_BEARER_UNENHANCED_LE:
137046012949SMatthias Ringwald             check_security = true;
137146012949SMatthias Ringwald             break;
137246012949SMatthias Ringwald         default:
13739ced96e1SMatthias Ringwald             check_security = false;
137446012949SMatthias Ringwald             break;
13759ced96e1SMatthias Ringwald     }
13769ced96e1SMatthias Ringwald     if (client_request_pending && (gatt_client_required_security_level > gatt_client->security_level) && check_security){
13771dfae9c7SMatthias Ringwald         log_info("Trigger pairing, current security level %u, required %u\n", gatt_client->security_level, gatt_client_required_security_level);
13782197dbafSMatthias Ringwald         gatt_client->wait_for_authentication_complete = true;
137911279da7SMatthias Ringwald         // set att error code for pairing failure based on required level
138011279da7SMatthias Ringwald         switch (gatt_client_required_security_level){
138111279da7SMatthias Ringwald             case LEVEL_4:
138211279da7SMatthias Ringwald             case LEVEL_3:
138311279da7SMatthias Ringwald                 gatt_client->pending_error_code = ATT_ERROR_INSUFFICIENT_AUTHENTICATION;
138411279da7SMatthias Ringwald                 break;
138511279da7SMatthias Ringwald             default:
138611279da7SMatthias Ringwald                 gatt_client->pending_error_code = ATT_ERROR_INSUFFICIENT_ENCRYPTION;
138711279da7SMatthias Ringwald                 break;
138811279da7SMatthias Ringwald         }
138911279da7SMatthias Ringwald         sm_request_pairing(gatt_client->con_handle);
139011279da7SMatthias Ringwald         // sm probably just sent a pdu
13911979f09cSMatthias Ringwald         return true;
139211279da7SMatthias Ringwald     }
1393f4b33574SMatthias Ringwald 
13945cf1669fSMatthias Ringwald     switch (gatt_client->mtu_state) {
1395544128c3SMatthias Ringwald         case SEND_MTU_EXCHANGE:
13965cf1669fSMatthias Ringwald             gatt_client->mtu_state = SENT_MTU_EXCHANGE;
13976e7b444cSMatthias Ringwald             att_exchange_mtu_request(gatt_client);
13981979f09cSMatthias Ringwald             return true;
13993deb3ec6SMatthias Ringwald         case SENT_MTU_EXCHANGE:
14001979f09cSMatthias Ringwald             return false;
14013deb3ec6SMatthias Ringwald         default:
14023deb3ec6SMatthias Ringwald             break;
14033deb3ec6SMatthias Ringwald     }
14043deb3ec6SMatthias Ringwald 
14055cf1669fSMatthias Ringwald     if (gatt_client->send_confirmation){
140652377058SMatthias Ringwald         gatt_client->send_confirmation = false;
14076e7b444cSMatthias Ringwald         att_confirmation(gatt_client);
14081979f09cSMatthias Ringwald         return true;
14093deb3ec6SMatthias Ringwald     }
14103deb3ec6SMatthias Ringwald 
14113deb3ec6SMatthias Ringwald     // check MTU for writes
1412052dc82aSMatthias Ringwald     switch (gatt_client->state){
14133deb3ec6SMatthias Ringwald         case P_W2_SEND_WRITE_CHARACTERISTIC_VALUE:
14143deb3ec6SMatthias Ringwald         case P_W2_SEND_WRITE_CHARACTERISTIC_DESCRIPTOR:
1415dda77937SMatthias Ringwald             if (gatt_client->attribute_length <= (gatt_client->mtu - 3u)) break;
1416dda77937SMatthias Ringwald             log_error("gatt_client_run: value len %u > MTU %u - 3\n", gatt_client->attribute_length,gatt_client->mtu);
1417526859e7SMatthias Ringwald             gatt_client_handle_transaction_complete(gatt_client, ATT_ERROR_INVALID_ATTRIBUTE_VALUE_LENGTH);
14181979f09cSMatthias Ringwald             return false;
14193deb3ec6SMatthias Ringwald         default:
14203deb3ec6SMatthias Ringwald             break;
14213deb3ec6SMatthias Ringwald     }
14223deb3ec6SMatthias Ringwald 
14230038504eSMatthias Ringwald     bool packet_sent = true;
14240038504eSMatthias Ringwald     bool done = true;
1425052dc82aSMatthias Ringwald     switch (gatt_client->state){
14263deb3ec6SMatthias Ringwald         case P_W2_SEND_SERVICE_QUERY:
1427052dc82aSMatthias Ringwald             gatt_client->state = P_W4_SERVICE_QUERY_RESULT;
14285cf1669fSMatthias Ringwald             send_gatt_services_request(gatt_client);
14290038504eSMatthias Ringwald             break;
14303deb3ec6SMatthias Ringwald 
14313deb3ec6SMatthias Ringwald         case P_W2_SEND_SERVICE_WITH_UUID_QUERY:
1432052dc82aSMatthias Ringwald             gatt_client->state = P_W4_SERVICE_WITH_UUID_RESULT;
14335cf1669fSMatthias Ringwald             send_gatt_services_by_uuid_request(gatt_client);
14340038504eSMatthias Ringwald             break;
14353deb3ec6SMatthias Ringwald 
14363deb3ec6SMatthias Ringwald         case P_W2_SEND_ALL_CHARACTERISTICS_OF_SERVICE_QUERY:
1437052dc82aSMatthias Ringwald             gatt_client->state = P_W4_ALL_CHARACTERISTICS_OF_SERVICE_QUERY_RESULT;
14385cf1669fSMatthias Ringwald             send_gatt_characteristic_request(gatt_client);
14390038504eSMatthias Ringwald             break;
14403deb3ec6SMatthias Ringwald 
14413deb3ec6SMatthias Ringwald         case P_W2_SEND_CHARACTERISTIC_WITH_UUID_QUERY:
1442052dc82aSMatthias Ringwald             gatt_client->state = P_W4_CHARACTERISTIC_WITH_UUID_QUERY_RESULT;
14435cf1669fSMatthias Ringwald             send_gatt_characteristic_request(gatt_client);
14440038504eSMatthias Ringwald             break;
14453deb3ec6SMatthias Ringwald 
14463deb3ec6SMatthias Ringwald         case P_W2_SEND_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY:
1447052dc82aSMatthias Ringwald             gatt_client->state = P_W4_CHARACTERISTIC_WITH_UUID_QUERY_RESULT;
14485cf1669fSMatthias Ringwald             send_gatt_characteristic_descriptor_request(gatt_client);
14490038504eSMatthias Ringwald             break;
14503deb3ec6SMatthias Ringwald 
14513deb3ec6SMatthias Ringwald         case P_W2_SEND_INCLUDED_SERVICE_QUERY:
1452052dc82aSMatthias Ringwald             gatt_client->state = P_W4_INCLUDED_SERVICE_QUERY_RESULT;
14535cf1669fSMatthias Ringwald             send_gatt_included_service_request(gatt_client);
14540038504eSMatthias Ringwald             break;
14553deb3ec6SMatthias Ringwald 
14563deb3ec6SMatthias Ringwald         case P_W2_SEND_INCLUDED_SERVICE_WITH_UUID_QUERY:
1457052dc82aSMatthias Ringwald             gatt_client->state = P_W4_INCLUDED_SERVICE_UUID_WITH_QUERY_RESULT;
14585cf1669fSMatthias Ringwald             send_gatt_included_service_uuid_request(gatt_client);
14590038504eSMatthias Ringwald             break;
14603deb3ec6SMatthias Ringwald 
14613deb3ec6SMatthias Ringwald         case P_W2_SEND_READ_CHARACTERISTIC_VALUE_QUERY:
1462052dc82aSMatthias Ringwald             gatt_client->state = P_W4_READ_CHARACTERISTIC_VALUE_RESULT;
14635cf1669fSMatthias Ringwald             send_gatt_read_characteristic_value_request(gatt_client);
14640038504eSMatthias Ringwald             break;
14653deb3ec6SMatthias Ringwald 
14663deb3ec6SMatthias Ringwald         case P_W2_SEND_READ_BLOB_QUERY:
1467052dc82aSMatthias Ringwald             gatt_client->state = P_W4_READ_BLOB_RESULT;
14685cf1669fSMatthias Ringwald             send_gatt_read_blob_request(gatt_client);
14690038504eSMatthias Ringwald             break;
14703deb3ec6SMatthias Ringwald 
14713deb3ec6SMatthias Ringwald         case P_W2_SEND_READ_BY_TYPE_REQUEST:
1472052dc82aSMatthias Ringwald             gatt_client->state = P_W4_READ_BY_TYPE_RESPONSE;
14735cf1669fSMatthias Ringwald             send_gatt_read_by_type_request(gatt_client);
14740038504eSMatthias Ringwald             break;
14753deb3ec6SMatthias Ringwald 
14763deb3ec6SMatthias Ringwald         case P_W2_SEND_READ_MULTIPLE_REQUEST:
1477052dc82aSMatthias Ringwald             gatt_client->state = P_W4_READ_MULTIPLE_RESPONSE;
14785cf1669fSMatthias Ringwald             send_gatt_read_multiple_request(gatt_client);
14790038504eSMatthias Ringwald             break;
14803deb3ec6SMatthias Ringwald 
1481f125a8efSMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT
1482f125a8efSMatthias Ringwald         case P_W2_SEND_READ_MULTIPLE_VARIABLE_REQUEST:
1483052dc82aSMatthias Ringwald             gatt_client->state = P_W4_READ_MULTIPLE_VARIABLE_RESPONSE;
1484f125a8efSMatthias Ringwald             send_gatt_read_multiple_variable_request(gatt_client);
1485f125a8efSMatthias Ringwald             break;
1486f125a8efSMatthias Ringwald #endif
1487f125a8efSMatthias Ringwald 
14883deb3ec6SMatthias Ringwald         case P_W2_SEND_WRITE_CHARACTERISTIC_VALUE:
1489052dc82aSMatthias Ringwald             gatt_client->state = P_W4_WRITE_CHARACTERISTIC_VALUE_RESULT;
14905cf1669fSMatthias Ringwald             send_gatt_write_attribute_value_request(gatt_client);
14910038504eSMatthias Ringwald             break;
14923deb3ec6SMatthias Ringwald 
14933deb3ec6SMatthias Ringwald         case P_W2_PREPARE_WRITE:
1494052dc82aSMatthias Ringwald             gatt_client->state = P_W4_PREPARE_WRITE_RESULT;
14955cf1669fSMatthias Ringwald             send_gatt_prepare_write_request(gatt_client);
14960038504eSMatthias Ringwald             break;
14973deb3ec6SMatthias Ringwald 
14983deb3ec6SMatthias Ringwald         case P_W2_PREPARE_WRITE_SINGLE:
1499052dc82aSMatthias Ringwald             gatt_client->state = P_W4_PREPARE_WRITE_SINGLE_RESULT;
15005cf1669fSMatthias Ringwald             send_gatt_prepare_write_request(gatt_client);
15010038504eSMatthias Ringwald             break;
15023deb3ec6SMatthias Ringwald 
15033deb3ec6SMatthias Ringwald         case P_W2_PREPARE_RELIABLE_WRITE:
1504052dc82aSMatthias Ringwald             gatt_client->state = P_W4_PREPARE_RELIABLE_WRITE_RESULT;
15055cf1669fSMatthias Ringwald             send_gatt_prepare_write_request(gatt_client);
15060038504eSMatthias Ringwald             break;
15073deb3ec6SMatthias Ringwald 
15083deb3ec6SMatthias Ringwald         case P_W2_EXECUTE_PREPARED_WRITE:
1509052dc82aSMatthias Ringwald             gatt_client->state = P_W4_EXECUTE_PREPARED_WRITE_RESULT;
15105cf1669fSMatthias Ringwald             send_gatt_execute_write_request(gatt_client);
15110038504eSMatthias Ringwald             break;
15123deb3ec6SMatthias Ringwald 
15133deb3ec6SMatthias Ringwald         case P_W2_CANCEL_PREPARED_WRITE:
1514052dc82aSMatthias Ringwald             gatt_client->state = P_W4_CANCEL_PREPARED_WRITE_RESULT;
15155cf1669fSMatthias Ringwald             send_gatt_cancel_prepared_write_request(gatt_client);
15160038504eSMatthias Ringwald             break;
15173deb3ec6SMatthias Ringwald 
15183deb3ec6SMatthias Ringwald         case P_W2_CANCEL_PREPARED_WRITE_DATA_MISMATCH:
1519052dc82aSMatthias Ringwald             gatt_client->state = P_W4_CANCEL_PREPARED_WRITE_DATA_MISMATCH_RESULT;
15205cf1669fSMatthias Ringwald             send_gatt_cancel_prepared_write_request(gatt_client);
15210038504eSMatthias Ringwald             break;
15223deb3ec6SMatthias Ringwald 
1523abdc9fb5SMatthias Ringwald #ifdef ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERY
1524abdc9fb5SMatthias Ringwald         case P_W2_SEND_FIND_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY:
1525abdc9fb5SMatthias Ringwald             // use Find Information
15265cf1669fSMatthias Ringwald             gatt_client->gatt_client_state = P_W4_FIND_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY_RESULT;
15275cf1669fSMatthias Ringwald             send_gatt_characteristic_descriptor_request(gatt_client);
1528abdc9fb5SMatthias Ringwald #else
15293deb3ec6SMatthias Ringwald         case P_W2_SEND_READ_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY:
1530abdc9fb5SMatthias Ringwald             // Use Read By Type
1531052dc82aSMatthias Ringwald             gatt_client->state = P_W4_READ_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY_RESULT;
15325cf1669fSMatthias Ringwald             send_gatt_read_client_characteristic_configuration_request(gatt_client);
1533abdc9fb5SMatthias Ringwald #endif
15340038504eSMatthias Ringwald             break;
15353deb3ec6SMatthias Ringwald 
15363deb3ec6SMatthias Ringwald         case P_W2_SEND_READ_CHARACTERISTIC_DESCRIPTOR_QUERY:
1537052dc82aSMatthias Ringwald             gatt_client->state = P_W4_READ_CHARACTERISTIC_DESCRIPTOR_RESULT;
15385cf1669fSMatthias Ringwald             send_gatt_read_characteristic_descriptor_request(gatt_client);
15390038504eSMatthias Ringwald             break;
15403deb3ec6SMatthias Ringwald 
15413deb3ec6SMatthias Ringwald         case P_W2_SEND_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_QUERY:
1542052dc82aSMatthias Ringwald             gatt_client->state = P_W4_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_RESULT;
15435cf1669fSMatthias Ringwald             send_gatt_read_blob_request(gatt_client);
15440038504eSMatthias Ringwald             break;
15453deb3ec6SMatthias Ringwald 
15463deb3ec6SMatthias Ringwald         case P_W2_SEND_WRITE_CHARACTERISTIC_DESCRIPTOR:
1547052dc82aSMatthias Ringwald             gatt_client->state = P_W4_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT;
15485cf1669fSMatthias Ringwald             send_gatt_write_attribute_value_request(gatt_client);
15490038504eSMatthias Ringwald             break;
15503deb3ec6SMatthias Ringwald 
15513deb3ec6SMatthias Ringwald         case P_W2_WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION:
1552052dc82aSMatthias Ringwald             gatt_client->state = P_W4_CLIENT_CHARACTERISTIC_CONFIGURATION_RESULT;
15535cf1669fSMatthias Ringwald             send_gatt_write_client_characteristic_configuration_request(gatt_client);
15540038504eSMatthias Ringwald             break;
15553deb3ec6SMatthias Ringwald 
15563deb3ec6SMatthias Ringwald         case P_W2_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR:
1557052dc82aSMatthias Ringwald             gatt_client->state = P_W4_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT;
15585cf1669fSMatthias Ringwald             send_gatt_prepare_write_request(gatt_client);
15590038504eSMatthias Ringwald             break;
15603deb3ec6SMatthias Ringwald 
15613deb3ec6SMatthias Ringwald         case P_W2_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR:
1562052dc82aSMatthias Ringwald             gatt_client->state = P_W4_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT;
15635cf1669fSMatthias Ringwald             send_gatt_execute_write_request(gatt_client);
15640038504eSMatthias Ringwald             break;
15653deb3ec6SMatthias Ringwald 
15667a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
1567793cf6ceSMatthias Ringwald         case P_W4_IDENTITY_RESOLVING:
15685cf1669fSMatthias Ringwald             log_info("P_W4_IDENTITY_RESOLVING - state %x", sm_identity_resolving_state(gatt_client->con_handle));
15695cf1669fSMatthias Ringwald             switch (sm_identity_resolving_state(gatt_client->con_handle)){
1570793cf6ceSMatthias Ringwald                 case IRK_LOOKUP_SUCCEEDED:
15715cf1669fSMatthias Ringwald                     gatt_client->le_device_index = sm_le_device_index(gatt_client->con_handle);
1572052dc82aSMatthias Ringwald                     gatt_client->state = P_W4_CMAC_READY;
157392a7335eSMatthias Ringwald                     if (sm_cmac_ready()){
157492a7335eSMatthias Ringwald                         gatt_client_run_for_client_start_signed_write(gatt_client);
157592a7335eSMatthias Ringwald                     }
1576793cf6ceSMatthias Ringwald                     break;
1577793cf6ceSMatthias Ringwald                 case IRK_LOOKUP_FAILED:
1578526859e7SMatthias Ringwald                     gatt_client_handle_transaction_complete(gatt_client, ATT_ERROR_BONDING_INFORMATION_MISSING);
157992a7335eSMatthias Ringwald                     break;
1580793cf6ceSMatthias Ringwald                 default:
158192a7335eSMatthias Ringwald                     break;
1582793cf6ceSMatthias Ringwald             }
15830038504eSMatthias Ringwald             packet_sent = false;
15840038504eSMatthias Ringwald             break;
1585793cf6ceSMatthias Ringwald 
15863deb3ec6SMatthias Ringwald         case P_W4_CMAC_READY:
15873deb3ec6SMatthias Ringwald             if (sm_cmac_ready()){
158892a7335eSMatthias Ringwald                 gatt_client_run_for_client_start_signed_write(gatt_client);
15893deb3ec6SMatthias Ringwald             }
15900038504eSMatthias Ringwald             packet_sent = false;
15910038504eSMatthias Ringwald             break;
15923deb3ec6SMatthias Ringwald 
15933deb3ec6SMatthias Ringwald         case P_W2_SEND_SIGNED_WRITE: {
15944c7c987fSMatthias Ringwald             gatt_client->state = P_W4_SEND_SIGNED_WRITE_DONE;
15953deb3ec6SMatthias Ringwald             // bump local signing counter
15965cf1669fSMatthias Ringwald             uint32_t sign_counter = le_device_db_local_counter_get(gatt_client->le_device_index);
15975cf1669fSMatthias Ringwald             le_device_db_local_counter_set(gatt_client->le_device_index, sign_counter + 1);
159864b12680SMatthias Ringwald             // send signed write command
15995cf1669fSMatthias Ringwald             send_gatt_signed_write_request(gatt_client, sign_counter);
16003deb3ec6SMatthias Ringwald             // finally, notifiy client that write is complete
1601526859e7SMatthias Ringwald             gatt_client_handle_transaction_complete(gatt_client, ATT_ERROR_SUCCESS);
1602b68ac7a5SMatthias Ringwald             break;
16033deb3ec6SMatthias Ringwald         }
16047a766ebfSMatthias Ringwald #endif
16053deb3ec6SMatthias Ringwald         default:
1606b68ac7a5SMatthias Ringwald             done = false;
16073deb3ec6SMatthias Ringwald             break;
16083deb3ec6SMatthias Ringwald     }
160947181045SMatthias Ringwald 
16100038504eSMatthias Ringwald     if (done){
16110038504eSMatthias Ringwald         return packet_sent;
16120038504eSMatthias Ringwald     }
16130038504eSMatthias Ringwald 
161459d34cd2SMatthias Ringwald     // write without response callback
161559d34cd2SMatthias Ringwald     btstack_context_callback_registration_t * callback =
161659d34cd2SMatthias Ringwald             (btstack_context_callback_registration_t *) btstack_linked_list_pop(&gatt_client->write_without_response_requests);
161759d34cd2SMatthias Ringwald     if (callback != NULL){
161859d34cd2SMatthias Ringwald         (*callback->callback)(callback->context);
161959d34cd2SMatthias Ringwald         return true;
162059d34cd2SMatthias Ringwald     }
162159d34cd2SMatthias Ringwald 
162259d34cd2SMatthias Ringwald     // requested can send now old
1623f688bdb8SMatthias Ringwald     if (gatt_client->write_without_response_callback != NULL){
16245cf1669fSMatthias Ringwald         btstack_packet_handler_t packet_handler = gatt_client->write_without_response_callback;
16255cf1669fSMatthias Ringwald         gatt_client->write_without_response_callback = NULL;
162647181045SMatthias Ringwald         uint8_t event[4];
162747181045SMatthias Ringwald         event[0] = GATT_EVENT_CAN_WRITE_WITHOUT_RESPONSE;
16284ea43905SMatthias Ringwald         event[1] = sizeof(event) - 2u;
16295cf1669fSMatthias Ringwald         little_endian_store_16(event, 2, gatt_client->con_handle);
16305cf1669fSMatthias Ringwald         packet_handler(HCI_EVENT_PACKET, gatt_client->con_handle, event, sizeof(event));
16311979f09cSMatthias Ringwald         return true; // to trigger requeueing (even if higher layer didn't sent)
163247181045SMatthias Ringwald     }
163347181045SMatthias Ringwald 
16341979f09cSMatthias Ringwald     return false;
16353deb3ec6SMatthias Ringwald }
16363deb3ec6SMatthias Ringwald 
1637544128c3SMatthias Ringwald static void gatt_client_run(void){
1638544128c3SMatthias Ringwald     btstack_linked_item_t *it;
1639109c2548SMatthias Ringwald     bool packet_sent;
16407627a0deSMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT
16417627a0deSMatthias Ringwald     btstack_linked_list_iterator_t it_eatt;
16427627a0deSMatthias Ringwald #endif
1643a0da043fSMatthias Ringwald     for (it = (btstack_linked_item_t *) gatt_client_connections; it != NULL; it = it->next){
16445cf1669fSMatthias Ringwald         gatt_client_t * gatt_client = (gatt_client_t *) it;
164546012949SMatthias Ringwald         switch (gatt_client->bearer_type){
164646012949SMatthias Ringwald             case ATT_BEARER_UNENHANCED_LE:
16477627a0deSMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT
16487627a0deSMatthias Ringwald                 btstack_linked_list_iterator_init(&it_eatt, &gatt_client->eatt_clients);
16497627a0deSMatthias Ringwald                 while (btstack_linked_list_iterator_has_next(&it_eatt)) {
16507627a0deSMatthias Ringwald                     gatt_client_t * eatt_client = (gatt_client_t *) btstack_linked_list_iterator_next(&it_eatt);
1651052dc82aSMatthias Ringwald                     if (eatt_client->state != P_READY){
16524c226876SMatthias Ringwald                         if (att_dispatch_client_can_send_now(gatt_client->con_handle)){
16537627a0deSMatthias Ringwald                             gatt_client_run_for_gatt_client(eatt_client);
16547627a0deSMatthias Ringwald                         }
16557627a0deSMatthias Ringwald                     }
16567627a0deSMatthias Ringwald                 }
16577627a0deSMatthias Ringwald #endif
165846012949SMatthias Ringwald                 if (!att_dispatch_client_can_send_now(gatt_client->con_handle)) {
165946012949SMatthias Ringwald                     att_dispatch_client_request_can_send_now_event(gatt_client->con_handle);
166046012949SMatthias Ringwald                     return;
166146012949SMatthias Ringwald                 }
1662109c2548SMatthias Ringwald                 packet_sent = gatt_client_run_for_gatt_client(gatt_client);
166346012949SMatthias Ringwald                 if (packet_sent){
166446012949SMatthias Ringwald                     // request new permission
166546012949SMatthias Ringwald                     att_dispatch_client_request_can_send_now_event(gatt_client->con_handle);
166646012949SMatthias Ringwald                     // requeue client for fairness and exit
166746012949SMatthias Ringwald                     // note: iterator has become invalid
166846012949SMatthias Ringwald                     btstack_linked_list_remove(&gatt_client_connections, (btstack_linked_item_t *) gatt_client);
166946012949SMatthias Ringwald                     btstack_linked_list_add_tail(&gatt_client_connections, (btstack_linked_item_t *) gatt_client);
167046012949SMatthias Ringwald                     return;
167146012949SMatthias Ringwald                 }
167246012949SMatthias Ringwald                 break;
16731450cdc6SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
167446012949SMatthias Ringwald             case ATT_BEARER_UNENHANCED_CLASSIC:
16751450cdc6SMatthias Ringwald                 if (gatt_client->con_handle == HCI_CON_HANDLE_INVALID) {
16761450cdc6SMatthias Ringwald                     continue;
16771450cdc6SMatthias Ringwald                 }
16781aa9e3e8SMatthias Ringwald 
16791aa9e3e8SMatthias Ringwald                 // handle GATT over BR/EDR
16804c226876SMatthias Ringwald                 if (att_dispatch_client_can_send_now(gatt_client->con_handle) == false) {
16814c226876SMatthias Ringwald                     att_dispatch_client_request_can_send_now_event(gatt_client->con_handle);
16821aa9e3e8SMatthias Ringwald                     return;
16831aa9e3e8SMatthias Ringwald                 }
1684109c2548SMatthias Ringwald                 packet_sent = gatt_client_run_for_gatt_client(gatt_client);
16851aa9e3e8SMatthias Ringwald                 if (packet_sent){
16861aa9e3e8SMatthias Ringwald                     // request new permission
16871aa9e3e8SMatthias Ringwald                     att_dispatch_client_request_can_send_now_event(gatt_client->con_handle);
16881aa9e3e8SMatthias Ringwald                     // requeue client for fairness and exit
16891aa9e3e8SMatthias Ringwald                     // note: iterator has become invalid
16901aa9e3e8SMatthias Ringwald                     btstack_linked_list_remove(&gatt_client_connections, (btstack_linked_item_t *) gatt_client);
16911aa9e3e8SMatthias Ringwald                     btstack_linked_list_add_tail(&gatt_client_connections, (btstack_linked_item_t *) gatt_client);
16921aa9e3e8SMatthias Ringwald                     return;
16931aa9e3e8SMatthias Ringwald                 }
169446012949SMatthias Ringwald                 break;
16951450cdc6SMatthias Ringwald #endif
169646012949SMatthias Ringwald             default:
169746012949SMatthias Ringwald                 btstack_unreachable();
169846012949SMatthias Ringwald                 break;
1699544128c3SMatthias Ringwald         }
170046012949SMatthias Ringwald 
170146012949SMatthias Ringwald 
1702544128c3SMatthias Ringwald     }
17033deb3ec6SMatthias Ringwald }
17043deb3ec6SMatthias Ringwald 
1705544a5845SMatthias Ringwald // emit complete event, used to avoid emitting event from API call
1706544a5845SMatthias Ringwald static void gatt_client_emit_events(void * context){
1707544a5845SMatthias Ringwald     UNUSED(context);
1708544a5845SMatthias Ringwald     btstack_linked_item_t *it;
1709544a5845SMatthias Ringwald     for (it = (btstack_linked_item_t *) gatt_client_connections; it != NULL; it = it->next) {
1710544a5845SMatthias Ringwald         gatt_client_t *gatt_client = (gatt_client_t *) it;
1711544a5845SMatthias Ringwald         if (gatt_client->state == P_W2_EMIT_QUERY_COMPLETE_EVENT){
1712544a5845SMatthias Ringwald             gatt_client->state = P_READY;
1713544a5845SMatthias Ringwald             emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS);
1714544a5845SMatthias Ringwald         }
1715544a5845SMatthias Ringwald     }
1716544a5845SMatthias Ringwald }
1717544a5845SMatthias Ringwald 
17185cf1669fSMatthias Ringwald static void gatt_client_report_error_if_pending(gatt_client_t *gatt_client, uint8_t att_error_code) {
1719643a64fcSMatthias Ringwald     if (is_ready(gatt_client)) return;
1720526859e7SMatthias Ringwald     gatt_client_handle_transaction_complete(gatt_client, att_error_code);
17213deb3ec6SMatthias Ringwald }
17223deb3ec6SMatthias Ringwald 
172378c4542aSMatthias Ringwald static void gatt_client_handle_reencryption_complete(const uint8_t * packet){
172478c4542aSMatthias Ringwald     hci_con_handle_t con_handle = sm_event_reencryption_complete_get_handle(packet);
172578c4542aSMatthias Ringwald     gatt_client_t * gatt_client = gatt_client_get_context_for_handle(con_handle);
172678c4542aSMatthias Ringwald     if (gatt_client == NULL) return;
172778c4542aSMatthias Ringwald 
172878c4542aSMatthias Ringwald     // update security level
172978c4542aSMatthias Ringwald     gatt_client->security_level = gatt_client_le_security_level_for_connection(con_handle);
173078c4542aSMatthias Ringwald 
173178c4542aSMatthias Ringwald     gatt_client->reencryption_result = sm_event_reencryption_complete_get_status(packet);
173278c4542aSMatthias Ringwald     gatt_client->reencryption_active = false;
17332197dbafSMatthias Ringwald     gatt_client->wait_for_authentication_complete = false;
173478c4542aSMatthias Ringwald 
1735052dc82aSMatthias Ringwald     if (gatt_client->state == P_READY) return;
173678c4542aSMatthias Ringwald 
173778c4542aSMatthias Ringwald     switch (sm_event_reencryption_complete_get_status(packet)){
173878c4542aSMatthias Ringwald         case ERROR_CODE_SUCCESS:
173978c4542aSMatthias Ringwald             log_info("re-encryption success, retry operation");
174078c4542aSMatthias Ringwald             break;
174178c4542aSMatthias Ringwald         case ERROR_CODE_AUTHENTICATION_FAILURE:
174278c4542aSMatthias Ringwald         case ERROR_CODE_PIN_OR_KEY_MISSING:
174378c4542aSMatthias Ringwald #if defined(ENABLE_GATT_CLIENT_PAIRING) && !defined(ENABLE_LE_PROACTIVE_AUTHENTICATION)
174478c4542aSMatthias Ringwald             if (gatt_client_required_security_level == LEVEL_0) {
174578c4542aSMatthias Ringwald                 // re-encryption failed for reactive authentication with pairing and we have a pending client request
174678c4542aSMatthias Ringwald                 // => try to resolve it by deleting bonding information if we started pairing before
174778c4542aSMatthias Ringwald                 // delete bonding information
174878c4542aSMatthias Ringwald                 int le_device_db_index = sm_le_device_index(gatt_client->con_handle);
174978c4542aSMatthias Ringwald                 btstack_assert(le_device_db_index >= 0);
175078c4542aSMatthias Ringwald                 log_info("reactive auth with pairing: delete bonding and start pairing");
175178c4542aSMatthias Ringwald #ifdef ENABLE_LE_PRIVACY_ADDRESS_RESOLUTION
175278c4542aSMatthias Ringwald                 hci_remove_le_device_db_entry_from_resolving_list((uint16_t) le_device_db_index);
175378c4542aSMatthias Ringwald #endif
175478c4542aSMatthias Ringwald                 le_device_db_remove(le_device_db_index);
175578c4542aSMatthias Ringwald                 // trigger pairing again
175678c4542aSMatthias Ringwald                 sm_request_pairing(gatt_client->con_handle);
175778c4542aSMatthias Ringwald                 break;
175878c4542aSMatthias Ringwald             }
175978c4542aSMatthias Ringwald #endif
176078c4542aSMatthias Ringwald             // report bonding information missing
1761526859e7SMatthias Ringwald             gatt_client_handle_transaction_complete(gatt_client, ATT_ERROR_BONDING_INFORMATION_MISSING);
176278c4542aSMatthias Ringwald             break;
176378c4542aSMatthias Ringwald         default:
176478c4542aSMatthias Ringwald             // report bonding information missing
1765526859e7SMatthias Ringwald             gatt_client_handle_transaction_complete(gatt_client, gatt_client->pending_error_code);
176678c4542aSMatthias Ringwald             break;
176778c4542aSMatthias Ringwald     }
176878c4542aSMatthias Ringwald }
176978c4542aSMatthias Ringwald 
177078c4542aSMatthias Ringwald static void gatt_client_handle_disconnection_complete(const uint8_t * packet){
177178c4542aSMatthias Ringwald     log_info("GATT Client: HCI_EVENT_DISCONNECTION_COMPLETE");
177278c4542aSMatthias Ringwald     hci_con_handle_t con_handle = little_endian_read_16(packet,3);
177378c4542aSMatthias Ringwald     gatt_client_t * gatt_client = gatt_client_get_context_for_handle(con_handle);
177478c4542aSMatthias Ringwald     if (gatt_client == NULL) return;
177578c4542aSMatthias Ringwald 
177678c4542aSMatthias Ringwald     gatt_client_report_error_if_pending(gatt_client, ATT_ERROR_HCI_DISCONNECT_RECEIVED);
177778c4542aSMatthias Ringwald     gatt_client_timeout_stop(gatt_client);
177878c4542aSMatthias Ringwald     btstack_linked_list_remove(&gatt_client_connections, (btstack_linked_item_t *) gatt_client);
177978c4542aSMatthias Ringwald     btstack_memory_gatt_client_free(gatt_client);
178078c4542aSMatthias Ringwald }
178178c4542aSMatthias Ringwald 
1782f4b33574SMatthias Ringwald static void gatt_client_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
17831ac26e06SMatthias Ringwald     UNUSED(channel);    // ok: handling own l2cap events
17841ac26e06SMatthias Ringwald     UNUSED(size);       // ok: there is no channel
17859ec2630cSMatthias Ringwald 
1786361b1363SMatthias Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
1787361b1363SMatthias Ringwald 
1788f4b33574SMatthias Ringwald     hci_con_handle_t con_handle;
17895cf1669fSMatthias Ringwald     gatt_client_t * gatt_client;
17900e2df43fSMatthias Ringwald     switch (hci_event_packet_get_type(packet)) {
17913deb3ec6SMatthias Ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
179278c4542aSMatthias Ringwald             gatt_client_handle_disconnection_complete(packet);
17933deb3ec6SMatthias Ringwald             break;
1794f4b33574SMatthias Ringwald 
1795f4b33574SMatthias Ringwald         // Pairing complete (with/without bonding=storing of pairing information)
1796f4b33574SMatthias Ringwald         case SM_EVENT_PAIRING_COMPLETE:
1797f4b33574SMatthias Ringwald             con_handle = sm_event_pairing_complete_get_handle(packet);
17985cf1669fSMatthias Ringwald             gatt_client = gatt_client_get_context_for_handle(con_handle);
17995cf1669fSMatthias Ringwald             if (gatt_client == NULL) break;
1800f4b33574SMatthias Ringwald 
18011dfae9c7SMatthias Ringwald             // update security level
18021dfae9c7SMatthias Ringwald             gatt_client->security_level = gatt_client_le_security_level_for_connection(con_handle);
18031dfae9c7SMatthias Ringwald 
18043503dc74SMatthias Ringwald             if (gatt_client->wait_for_authentication_complete){
18052197dbafSMatthias Ringwald                 gatt_client->wait_for_authentication_complete = false;
1806f688bdb8SMatthias Ringwald                 if (sm_event_pairing_complete_get_status(packet) != ERROR_CODE_SUCCESS){
18075cf1669fSMatthias Ringwald                     log_info("pairing failed, report previous error 0x%x", gatt_client->pending_error_code);
1808ec9babacSMatthias Ringwald                     gatt_client_report_error_if_pending(gatt_client, gatt_client->pending_error_code);
1809f4b33574SMatthias Ringwald                 } else {
1810f4b33574SMatthias Ringwald                     log_info("pairing success, retry operation");
18113deb3ec6SMatthias Ringwald                 }
1812f4b33574SMatthias Ringwald             }
1813f4b33574SMatthias Ringwald             break;
181411279da7SMatthias Ringwald 
1815793cf6ceSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
1816793cf6ceSMatthias Ringwald         // Identity Resolving completed (no code, gatt_client_run will continue)
1817793cf6ceSMatthias Ringwald         case SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED:
1818793cf6ceSMatthias Ringwald         case SM_EVENT_IDENTITY_RESOLVING_FAILED:
1819793cf6ceSMatthias Ringwald             break;
1820793cf6ceSMatthias Ringwald #endif
1821a95ca902SMatthias Ringwald 
18228918bbdaSMatthias Ringwald         // re-encryption started
18238918bbdaSMatthias Ringwald         case SM_EVENT_REENCRYPTION_STARTED:
18248918bbdaSMatthias Ringwald             con_handle = sm_event_reencryption_complete_get_handle(packet);
18258918bbdaSMatthias Ringwald             gatt_client = gatt_client_get_context_for_handle(con_handle);
18268918bbdaSMatthias Ringwald             if (gatt_client == NULL) break;
18278918bbdaSMatthias Ringwald 
18288918bbdaSMatthias Ringwald             gatt_client->reencryption_active = true;
18298918bbdaSMatthias Ringwald             gatt_client->reencryption_result = ERROR_CODE_SUCCESS;
18308918bbdaSMatthias Ringwald             break;
18318918bbdaSMatthias Ringwald 
1832ec9babacSMatthias Ringwald         // re-encryption complete
1833ec9babacSMatthias Ringwald         case SM_EVENT_REENCRYPTION_COMPLETE:
183478c4542aSMatthias Ringwald             gatt_client_handle_reencryption_complete(packet);
1835ec9babacSMatthias Ringwald             break;
18363deb3ec6SMatthias Ringwald         default:
18373deb3ec6SMatthias Ringwald             break;
18383deb3ec6SMatthias Ringwald     }
18393deb3ec6SMatthias Ringwald 
1840361b1363SMatthias Ringwald     gatt_client_run();
18413deb3ec6SMatthias Ringwald }
18423deb3ec6SMatthias Ringwald 
18432da0d963SMatthias Ringwald static void gatt_client_handle_att_read_response(gatt_client_t *gatt_client, uint8_t *packet, uint16_t size) {
1844052dc82aSMatthias Ringwald     switch (gatt_client->state) {
18452da0d963SMatthias Ringwald         case P_W4_INCLUDED_SERVICE_UUID_WITH_QUERY_RESULT:
18462da0d963SMatthias Ringwald             if (size >= 17) {
18472da0d963SMatthias Ringwald                 uint8_t uuid128[16];
18482da0d963SMatthias Ringwald                 reverse_128(&packet[1], uuid128);
18492da0d963SMatthias Ringwald                 report_gatt_included_service_uuid128(gatt_client, gatt_client->start_group_handle, uuid128);
18502da0d963SMatthias Ringwald             }
18512da0d963SMatthias Ringwald             trigger_next_included_service_query(gatt_client, gatt_client->start_group_handle);
18525611a760SMatthias Ringwald             // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
18533deb3ec6SMatthias Ringwald             break;
18542da0d963SMatthias Ringwald 
18552da0d963SMatthias Ringwald         case P_W4_READ_CHARACTERISTIC_VALUE_RESULT:
18562da0d963SMatthias Ringwald             report_gatt_characteristic_value(gatt_client, gatt_client->attribute_handle, &packet[1], size - 1u);
1857526859e7SMatthias Ringwald             gatt_client_handle_transaction_complete(gatt_client, ATT_ERROR_SUCCESS);
18582da0d963SMatthias Ringwald             break;
18592da0d963SMatthias Ringwald 
18602da0d963SMatthias Ringwald         case P_W4_READ_CHARACTERISTIC_DESCRIPTOR_RESULT:
18612da0d963SMatthias Ringwald             report_gatt_characteristic_descriptor(gatt_client, gatt_client->attribute_handle, &packet[1],
18622da0d963SMatthias Ringwald                                                   size - 1u, 0u);
1863526859e7SMatthias Ringwald             gatt_client_handle_transaction_complete(gatt_client, ATT_ERROR_SUCCESS);
18642da0d963SMatthias Ringwald             break;
18652da0d963SMatthias Ringwald 
18662da0d963SMatthias Ringwald             // Use ATT_READ_REQUEST for first blob of Read Long Characteristic
18672da0d963SMatthias Ringwald         case P_W4_READ_BLOB_RESULT:
18682da0d963SMatthias Ringwald             report_gatt_long_characteristic_value_blob(gatt_client, gatt_client->attribute_handle, &packet[1],
18692da0d963SMatthias Ringwald                                                        size - 1u, gatt_client->attribute_offset);
18702da0d963SMatthias Ringwald             trigger_next_blob_query(gatt_client, P_W2_SEND_READ_BLOB_QUERY, size - 1u);
18712da0d963SMatthias Ringwald             // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
18722da0d963SMatthias Ringwald             break;
18732da0d963SMatthias Ringwald 
18742da0d963SMatthias Ringwald             // Use ATT_READ_REQUEST for first blob of Read Long Characteristic Descriptor
18752da0d963SMatthias Ringwald         case P_W4_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_RESULT:
18762da0d963SMatthias Ringwald             report_gatt_long_characteristic_descriptor(gatt_client, gatt_client->attribute_handle, &packet[1],
18772da0d963SMatthias Ringwald                                                        size - 1u, gatt_client->attribute_offset);
18782da0d963SMatthias Ringwald             trigger_next_blob_query(gatt_client, P_W2_SEND_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_QUERY,
18792da0d963SMatthias Ringwald                                     size - 1u);
18802da0d963SMatthias Ringwald             // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
18812da0d963SMatthias Ringwald             break;
18822da0d963SMatthias Ringwald 
18833deb3ec6SMatthias Ringwald         default:
18843deb3ec6SMatthias Ringwald             break;
18853deb3ec6SMatthias Ringwald     }
18862da0d963SMatthias Ringwald }
18873deb3ec6SMatthias Ringwald 
18882da0d963SMatthias Ringwald static void gatt_client_handle_att_read_by_type_response(gatt_client_t *gatt_client, uint8_t *packet, uint16_t size) {
1889052dc82aSMatthias Ringwald     switch (gatt_client->state) {
18903deb3ec6SMatthias Ringwald         case P_W4_ALL_CHARACTERISTICS_OF_SERVICE_QUERY_RESULT:
18915cf1669fSMatthias Ringwald             report_gatt_characteristics(gatt_client, packet, size);
1892cf8e5718SMatthias Ringwald             trigger_next_characteristic_query(gatt_client,
1893cf8e5718SMatthias Ringwald                                               get_last_result_handle_from_characteristics_list(packet, size));
18945611a760SMatthias Ringwald             // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done, or by ATT_ERROR
18953deb3ec6SMatthias Ringwald             break;
18963deb3ec6SMatthias Ringwald         case P_W4_CHARACTERISTIC_WITH_UUID_QUERY_RESULT:
18975cf1669fSMatthias Ringwald             report_gatt_characteristics(gatt_client, packet, size);
1898cf8e5718SMatthias Ringwald             trigger_next_characteristic_query(gatt_client,
1899cf8e5718SMatthias Ringwald                                               get_last_result_handle_from_characteristics_list(packet, size));
19005611a760SMatthias Ringwald             // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done, or by ATT_ERROR
19013deb3ec6SMatthias Ringwald             break;
1902cf8e5718SMatthias Ringwald         case P_W4_INCLUDED_SERVICE_QUERY_RESULT: {
19034ea43905SMatthias Ringwald             if (size < 2u) break;
19043deb3ec6SMatthias Ringwald             uint16_t uuid16 = 0;
19053deb3ec6SMatthias Ringwald             uint16_t pair_size = packet[1];
19063deb3ec6SMatthias Ringwald 
19074ea43905SMatthias Ringwald             if (pair_size == 6u) {
19084ea43905SMatthias Ringwald                 if (size < 8u) break;
19093deb3ec6SMatthias Ringwald                 // UUIDs not available, query first included service
19105cf1669fSMatthias Ringwald                 gatt_client->start_group_handle = little_endian_read_16(packet, 2); // ready for next query
19115cf1669fSMatthias Ringwald                 gatt_client->query_start_handle = little_endian_read_16(packet, 4);
19125cf1669fSMatthias Ringwald                 gatt_client->query_end_handle = little_endian_read_16(packet, 6);
1913052dc82aSMatthias Ringwald                 gatt_client->state = P_W2_SEND_INCLUDED_SERVICE_WITH_UUID_QUERY;
19143deb3ec6SMatthias Ringwald                 break;
19153deb3ec6SMatthias Ringwald             }
19163deb3ec6SMatthias Ringwald 
19174ea43905SMatthias Ringwald             if (pair_size != 8u) break;
19189f698c82SMatthias Ringwald 
19199f698c82SMatthias Ringwald             // UUIDs included, report all of them
19203deb3ec6SMatthias Ringwald             uint16_t offset;
19214ea43905SMatthias Ringwald             for (offset = 2u; (offset + 8u) <= size; offset += pair_size) {
1922f8fbdce0SMatthias Ringwald                 uint16_t include_handle = little_endian_read_16(packet, offset);
19235cf1669fSMatthias Ringwald                 gatt_client->query_start_handle = little_endian_read_16(packet, offset + 2u);
19245cf1669fSMatthias Ringwald                 gatt_client->query_end_handle = little_endian_read_16(packet, offset + 4u);
19254ea43905SMatthias Ringwald                 uuid16 = little_endian_read_16(packet, offset + 6u);
19265cf1669fSMatthias Ringwald                 report_gatt_included_service_uuid16(gatt_client, include_handle, uuid16);
19273deb3ec6SMatthias Ringwald             }
19283deb3ec6SMatthias Ringwald 
1929cf8e5718SMatthias Ringwald             trigger_next_included_service_query(gatt_client,
1930cf8e5718SMatthias Ringwald                                                 get_last_result_handle_from_included_services_list(packet,
1931cf8e5718SMatthias Ringwald                                                                                                    size));
19325611a760SMatthias Ringwald             // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
19333deb3ec6SMatthias Ringwald             break;
19343deb3ec6SMatthias Ringwald         }
1935abdc9fb5SMatthias Ringwald #ifndef ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERY
19363deb3ec6SMatthias Ringwald         case P_W4_READ_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY_RESULT:
19375cf1669fSMatthias Ringwald             gatt_client->client_characteristic_configuration_handle = little_endian_read_16(packet, 2);
1938052dc82aSMatthias Ringwald             gatt_client->state = P_W2_WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION;
19393deb3ec6SMatthias Ringwald             break;
1940abdc9fb5SMatthias Ringwald #endif
19413deb3ec6SMatthias Ringwald         case P_W4_READ_BY_TYPE_RESPONSE: {
19423deb3ec6SMatthias Ringwald             uint16_t pair_size = packet[1];
194339ac9711SMatthias Ringwald             // set last result handle to last valid handle, only used if pair_size invalid
194439ac9711SMatthias Ringwald             uint16_t last_result_handle = 0xffff;
194539ac9711SMatthias Ringwald             if (pair_size > 2) {
19463deb3ec6SMatthias Ringwald                 uint16_t offset;
19473deb3ec6SMatthias Ringwald                 for (offset = 2; offset < size; offset += pair_size) {
1948f8fbdce0SMatthias Ringwald                     uint16_t value_handle = little_endian_read_16(packet, offset);
1949cf8e5718SMatthias Ringwald                     report_gatt_characteristic_value(gatt_client, value_handle, &packet[offset + 2u],
1950cf8e5718SMatthias Ringwald                                                      pair_size - 2u);
19513deb3ec6SMatthias Ringwald                     last_result_handle = value_handle;
19523deb3ec6SMatthias Ringwald                 }
195339ac9711SMatthias Ringwald             }
19545cf1669fSMatthias Ringwald             trigger_next_read_by_type_query(gatt_client, last_result_handle);
19553deb3ec6SMatthias Ringwald             break;
19563deb3ec6SMatthias Ringwald         }
19573deb3ec6SMatthias Ringwald         default:
19583deb3ec6SMatthias Ringwald             break;
19593deb3ec6SMatthias Ringwald     }
196039ac9711SMatthias Ringwald }
1961dc13fd8dSMatthias Ringwald 
19622da0d963SMatthias Ringwald static void gatt_client_handle_att_write_response(gatt_client_t *gatt_client) {
1963052dc82aSMatthias Ringwald     switch (gatt_client->state) {
19642da0d963SMatthias Ringwald         case P_W4_WRITE_CHARACTERISTIC_VALUE_RESULT:
1965526859e7SMatthias Ringwald             gatt_client_handle_transaction_complete(gatt_client, ATT_ERROR_SUCCESS);
19663deb3ec6SMatthias Ringwald             break;
19672da0d963SMatthias Ringwald         case P_W4_CLIENT_CHARACTERISTIC_CONFIGURATION_RESULT:
1968526859e7SMatthias Ringwald             gatt_client_handle_transaction_complete(gatt_client, ATT_ERROR_SUCCESS);
19693deb3ec6SMatthias Ringwald             break;
19702da0d963SMatthias Ringwald         case P_W4_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT:
1971526859e7SMatthias Ringwald             gatt_client_handle_transaction_complete(gatt_client, ATT_ERROR_SUCCESS);
19722da0d963SMatthias Ringwald             break;
19732da0d963SMatthias Ringwald         default:
19742da0d963SMatthias Ringwald             break;
19752da0d963SMatthias Ringwald     }
19762da0d963SMatthias Ringwald }
1977dc13fd8dSMatthias Ringwald 
19782da0d963SMatthias Ringwald static void gatt_client_handle_att_response(gatt_client_t * gatt_client, uint8_t * packet, uint16_t size) {
19794740230fSMatthias Ringwald     uint8_t att_status;
19802da0d963SMatthias Ringwald     switch (packet[0]) {
19812da0d963SMatthias Ringwald         case ATT_EXCHANGE_MTU_RESPONSE: {
19822da0d963SMatthias Ringwald             if (size < 3u) break;
198346012949SMatthias Ringwald             bool update_gatt_server_att_mtu = false;
19842da0d963SMatthias Ringwald             uint16_t remote_rx_mtu = little_endian_read_16(packet, 1);
19852da0d963SMatthias Ringwald             uint16_t local_rx_mtu = l2cap_max_le_mtu();
198646012949SMatthias Ringwald             switch (gatt_client->bearer_type){
198746012949SMatthias Ringwald                 case ATT_BEARER_UNENHANCED_LE:
198846012949SMatthias Ringwald                     update_gatt_server_att_mtu = true;
198946012949SMatthias Ringwald                     break;
19902da0d963SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
199146012949SMatthias Ringwald                 case ATT_BEARER_UNENHANCED_CLASSIC:
19922da0d963SMatthias Ringwald                     local_rx_mtu = gatt_client->mtu;
199346012949SMatthias Ringwald                     break;
19942da0d963SMatthias Ringwald #endif
199546012949SMatthias Ringwald                 default:
199646012949SMatthias Ringwald                     btstack_unreachable();
199746012949SMatthias Ringwald                     break;
199846012949SMatthias Ringwald             }
199946012949SMatthias Ringwald 
20002da0d963SMatthias Ringwald             uint16_t mtu = (remote_rx_mtu < local_rx_mtu) ? remote_rx_mtu : local_rx_mtu;
20012da0d963SMatthias Ringwald 
20022da0d963SMatthias Ringwald             // set gatt client mtu
20032da0d963SMatthias Ringwald             gatt_client->mtu = mtu;
20042da0d963SMatthias Ringwald             gatt_client->mtu_state = MTU_EXCHANGED;
20052da0d963SMatthias Ringwald 
200646012949SMatthias Ringwald             if (update_gatt_server_att_mtu){
20072da0d963SMatthias Ringwald                 // set per connection mtu state - for fixed channel
20082da0d963SMatthias Ringwald                 hci_connection_t *hci_connection = hci_connection_for_handle(gatt_client->con_handle);
20092da0d963SMatthias Ringwald                 hci_connection->att_connection.mtu = gatt_client->mtu;
20102da0d963SMatthias Ringwald                 hci_connection->att_connection.mtu_exchanged = true;
20112da0d963SMatthias Ringwald             }
20122da0d963SMatthias Ringwald             emit_gatt_mtu_exchanged_result_event(gatt_client, gatt_client->mtu);
20132da0d963SMatthias Ringwald             break;
20142da0d963SMatthias Ringwald         }
20152da0d963SMatthias Ringwald         case ATT_READ_BY_GROUP_TYPE_RESPONSE:
2016052dc82aSMatthias Ringwald             switch (gatt_client->state) {
20172da0d963SMatthias Ringwald                 case P_W4_SERVICE_QUERY_RESULT:
20182da0d963SMatthias Ringwald                     report_gatt_services(gatt_client, packet, size);
20192da0d963SMatthias Ringwald                     trigger_next_service_query(gatt_client, get_last_result_handle_from_service_list(packet, size));
202030952227SMilanka Ringwald                     // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
202130952227SMilanka Ringwald                     break;
20223deb3ec6SMatthias Ringwald                 default:
20233deb3ec6SMatthias Ringwald                     break;
20243deb3ec6SMatthias Ringwald             }
20253deb3ec6SMatthias Ringwald             break;
20262da0d963SMatthias Ringwald         case ATT_HANDLE_VALUE_NOTIFICATION:
20272da0d963SMatthias Ringwald             if (size < 3u) return;
20282da0d963SMatthias Ringwald             report_gatt_notification(gatt_client, little_endian_read_16(packet, 1u), &packet[3], size - 3u);
20292da0d963SMatthias Ringwald             return;
2030cbba69d5SMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT
2031cbba69d5SMatthias Ringwald         case ATT_MULTIPLE_HANDLE_VALUE_NTF:
2032cbba69d5SMatthias Ringwald             if (size >= 5u) {
2033cbba69d5SMatthias Ringwald                 uint16_t offset = 1;
2034cbba69d5SMatthias Ringwald                 while (true){
2035cbba69d5SMatthias Ringwald                     uint16_t value_handle = little_endian_read_16(packet, offset);
2036cbba69d5SMatthias Ringwald                     offset += 2;
2037cbba69d5SMatthias Ringwald                     uint16_t value_length = little_endian_read_16(packet, offset);
2038cbba69d5SMatthias Ringwald                     offset += 2;
2039cbba69d5SMatthias Ringwald                     if ((offset + value_length) > size) break;
2040cbba69d5SMatthias Ringwald                     report_gatt_notification(gatt_client, value_handle, &packet[offset], value_length);
2041cbba69d5SMatthias Ringwald                     offset += value_length;
2042cbba69d5SMatthias Ringwald                 }
2043cbba69d5SMatthias Ringwald             }
2044cbba69d5SMatthias Ringwald             return;
2045cbba69d5SMatthias Ringwald #endif
20462da0d963SMatthias Ringwald         case ATT_HANDLE_VALUE_INDICATION:
20472da0d963SMatthias Ringwald             if (size < 3u) break;
20482da0d963SMatthias Ringwald             report_gatt_indication(gatt_client, little_endian_read_16(packet, 1u), &packet[3], size - 3u);
204952377058SMatthias Ringwald             gatt_client->send_confirmation = true;
20502da0d963SMatthias Ringwald             break;
20512da0d963SMatthias Ringwald         case ATT_READ_BY_TYPE_RESPONSE:
20522da0d963SMatthias Ringwald             gatt_client_handle_att_read_by_type_response(gatt_client, packet, size);
20532da0d963SMatthias Ringwald             break;
20542da0d963SMatthias Ringwald         case ATT_READ_RESPONSE:
20552da0d963SMatthias Ringwald             gatt_client_handle_att_read_response(gatt_client, packet, size);
20562da0d963SMatthias Ringwald             break;
2057cf8e5718SMatthias Ringwald         case ATT_FIND_BY_TYPE_VALUE_RESPONSE: {
20583deb3ec6SMatthias Ringwald             uint8_t pair_size = 4;
20593deb3ec6SMatthias Ringwald             int i;
20603deb3ec6SMatthias Ringwald             uint16_t start_group_handle;
20615611a760SMatthias Ringwald             uint16_t end_group_handle = 0xffff; // asserts GATT_EVENT_QUERY_COMPLETE is emitted if no results
20624ea43905SMatthias Ringwald             for (i = 1u; (i + pair_size) <= size; i += pair_size) {
2063f8fbdce0SMatthias Ringwald                 start_group_handle = little_endian_read_16(packet, i);
2064f8fbdce0SMatthias Ringwald                 end_group_handle = little_endian_read_16(packet, i + 2);
2065cf8e5718SMatthias Ringwald                 emit_gatt_service_query_result_event(gatt_client, start_group_handle, end_group_handle,
2066cf8e5718SMatthias Ringwald                                                      gatt_client->uuid128);
20673deb3ec6SMatthias Ringwald             }
20685cf1669fSMatthias Ringwald             trigger_next_service_by_uuid_query(gatt_client, end_group_handle);
20695611a760SMatthias Ringwald             // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
20703deb3ec6SMatthias Ringwald             break;
20713deb3ec6SMatthias Ringwald         }
2072cf8e5718SMatthias Ringwald         case ATT_FIND_INFORMATION_REPLY: {
20734ea43905SMatthias Ringwald             if (size < 2u) break;
2074a6121b51SMilanka Ringwald 
20753deb3ec6SMatthias Ringwald             uint8_t pair_size = 4;
20764ea43905SMatthias Ringwald             if (packet[1u] == 2u) {
20773deb3ec6SMatthias Ringwald                 pair_size = 18;
20783deb3ec6SMatthias Ringwald             }
2079a6121b51SMilanka Ringwald             uint16_t offset = 2;
2080a6121b51SMilanka Ringwald 
2081a6121b51SMilanka Ringwald             if (size < (pair_size + offset)) break;
2082f8fbdce0SMatthias Ringwald             uint16_t last_descriptor_handle = little_endian_read_16(packet, size - pair_size);
2083abdc9fb5SMatthias Ringwald 
2084abdc9fb5SMatthias Ringwald #ifdef ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERY
20855cf1669fSMatthias Ringwald             log_info("ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERY, state %x", gatt_client->gatt_client_state);
20865cf1669fSMatthias Ringwald             if (gatt_client->gatt_client_state == P_W4_FIND_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY_RESULT){
2087abdc9fb5SMatthias Ringwald                 // iterate over descriptors looking for CCC
2088abdc9fb5SMatthias Ringwald                 if (pair_size == 4){
2089a6121b51SMilanka Ringwald                     while ((offset + 4) <= size){
2090abdc9fb5SMatthias Ringwald                         uint16_t uuid16 = little_endian_read_16(packet, offset + 2);
2091abdc9fb5SMatthias Ringwald                         if (uuid16 == GATT_CLIENT_CHARACTERISTICS_CONFIGURATION){
20925cf1669fSMatthias Ringwald                             gatt_client->client_characteristic_configuration_handle = little_endian_read_16(packet, offset);
20935cf1669fSMatthias Ringwald                             gatt_client->gatt_client_state = P_W2_WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION;
20945cf1669fSMatthias Ringwald                             log_info("CCC found %x", gatt_client->client_characteristic_configuration_handle);
2095abdc9fb5SMatthias Ringwald                             break;
2096abdc9fb5SMatthias Ringwald                         }
2097abdc9fb5SMatthias Ringwald                         offset += pair_size;
2098abdc9fb5SMatthias Ringwald                     }
2099abdc9fb5SMatthias Ringwald                 }
21005cf1669fSMatthias Ringwald                 if (is_query_done(gatt_client, last_descriptor_handle)){
2101abdc9fb5SMatthias Ringwald 
2102abdc9fb5SMatthias Ringwald                 } else {
2103abdc9fb5SMatthias Ringwald                     // next
21045cf1669fSMatthias Ringwald                     gatt_client->start_group_handle = last_descriptor_handle + 1;
21055cf1669fSMatthias Ringwald                     gatt_client->gatt_client_state = P_W2_SEND_FIND_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY;
2106abdc9fb5SMatthias Ringwald                 }
2107abdc9fb5SMatthias Ringwald                 break;
2108abdc9fb5SMatthias Ringwald             }
2109abdc9fb5SMatthias Ringwald #endif
21105cf1669fSMatthias Ringwald             report_gatt_all_characteristic_descriptors(gatt_client, &packet[2], size - 2u, pair_size);
21115cf1669fSMatthias Ringwald             trigger_next_characteristic_descriptor_query(gatt_client, last_descriptor_handle);
21125611a760SMatthias Ringwald             // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
21133deb3ec6SMatthias Ringwald             break;
21143deb3ec6SMatthias Ringwald         }
21153deb3ec6SMatthias Ringwald 
21163deb3ec6SMatthias Ringwald         case ATT_WRITE_RESPONSE:
21172da0d963SMatthias Ringwald             gatt_client_handle_att_write_response(gatt_client);
21183deb3ec6SMatthias Ringwald             break;
21193deb3ec6SMatthias Ringwald 
21203deb3ec6SMatthias Ringwald         case ATT_READ_BLOB_RESPONSE: {
21214ea43905SMatthias Ringwald             uint16_t received_blob_length = size - 1u;
2122052dc82aSMatthias Ringwald             switch (gatt_client->state) {
21233deb3ec6SMatthias Ringwald                 case P_W4_READ_BLOB_RESULT:
2124cf8e5718SMatthias Ringwald                     report_gatt_long_characteristic_value_blob(gatt_client, gatt_client->attribute_handle, &packet[1],
2125cf8e5718SMatthias Ringwald                                                                received_blob_length, gatt_client->attribute_offset);
21265cf1669fSMatthias Ringwald                     trigger_next_blob_query(gatt_client, P_W2_SEND_READ_BLOB_QUERY, received_blob_length);
21275611a760SMatthias Ringwald                     // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
21283deb3ec6SMatthias Ringwald                     break;
21293deb3ec6SMatthias Ringwald                 case P_W4_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_RESULT:
21305cf1669fSMatthias Ringwald                     report_gatt_long_characteristic_descriptor(gatt_client, gatt_client->attribute_handle,
21313deb3ec6SMatthias Ringwald                                                                &packet[1], received_blob_length,
21325cf1669fSMatthias Ringwald                                                                gatt_client->attribute_offset);
2133cf8e5718SMatthias Ringwald                     trigger_next_blob_query(gatt_client, P_W2_SEND_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_QUERY,
2134cf8e5718SMatthias Ringwald                                             received_blob_length);
21355611a760SMatthias Ringwald                     // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
21363deb3ec6SMatthias Ringwald                     break;
21373deb3ec6SMatthias Ringwald                 default:
21383deb3ec6SMatthias Ringwald                     break;
21393deb3ec6SMatthias Ringwald             }
21403deb3ec6SMatthias Ringwald             break;
21413deb3ec6SMatthias Ringwald         }
21423deb3ec6SMatthias Ringwald         case ATT_PREPARE_WRITE_RESPONSE:
2143052dc82aSMatthias Ringwald             switch (gatt_client->state) {
21443deb3ec6SMatthias Ringwald                 case P_W4_PREPARE_WRITE_SINGLE_RESULT:
21455cf1669fSMatthias Ringwald                     if (is_value_valid(gatt_client, packet, size)) {
21464740230fSMatthias Ringwald                         att_status = ATT_ERROR_SUCCESS;
21473deb3ec6SMatthias Ringwald                     } else {
21484740230fSMatthias Ringwald                         att_status = ATT_ERROR_DATA_MISMATCH;
21493deb3ec6SMatthias Ringwald                     }
2150526859e7SMatthias Ringwald                     gatt_client_handle_transaction_complete(gatt_client, att_status);
21513deb3ec6SMatthias Ringwald                     break;
21523deb3ec6SMatthias Ringwald 
21533deb3ec6SMatthias Ringwald                 case P_W4_PREPARE_WRITE_RESULT: {
21545cf1669fSMatthias Ringwald                     gatt_client->attribute_offset = little_endian_read_16(packet, 3);
21555cf1669fSMatthias Ringwald                     trigger_next_prepare_write_query(gatt_client, P_W2_PREPARE_WRITE, P_W2_EXECUTE_PREPARED_WRITE);
21565611a760SMatthias Ringwald                     // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
21573deb3ec6SMatthias Ringwald                     break;
21583deb3ec6SMatthias Ringwald                 }
21593deb3ec6SMatthias Ringwald                 case P_W4_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT: {
21605cf1669fSMatthias Ringwald                     gatt_client->attribute_offset = little_endian_read_16(packet, 3);
2161cf8e5718SMatthias Ringwald                     trigger_next_prepare_write_query(gatt_client, P_W2_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR,
2162cf8e5718SMatthias Ringwald                                                      P_W2_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR);
21635611a760SMatthias Ringwald                     // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
21643deb3ec6SMatthias Ringwald                     break;
21653deb3ec6SMatthias Ringwald                 }
21663deb3ec6SMatthias Ringwald                 case P_W4_PREPARE_RELIABLE_WRITE_RESULT: {
21675cf1669fSMatthias Ringwald                     if (is_value_valid(gatt_client, packet, size)) {
21685cf1669fSMatthias Ringwald                         gatt_client->attribute_offset = little_endian_read_16(packet, 3);
2169cf8e5718SMatthias Ringwald                         trigger_next_prepare_write_query(gatt_client, P_W2_PREPARE_RELIABLE_WRITE,
2170cf8e5718SMatthias Ringwald                                                          P_W2_EXECUTE_PREPARED_WRITE);
21715611a760SMatthias Ringwald                         // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
21723deb3ec6SMatthias Ringwald                         break;
21733deb3ec6SMatthias Ringwald                     }
2174052dc82aSMatthias Ringwald                     gatt_client->state = P_W2_CANCEL_PREPARED_WRITE_DATA_MISMATCH;
21753deb3ec6SMatthias Ringwald                     break;
21763deb3ec6SMatthias Ringwald                 }
21773deb3ec6SMatthias Ringwald                 default:
21783deb3ec6SMatthias Ringwald                     break;
21793deb3ec6SMatthias Ringwald             }
21803deb3ec6SMatthias Ringwald             break;
21813deb3ec6SMatthias Ringwald 
21823deb3ec6SMatthias Ringwald         case ATT_EXECUTE_WRITE_RESPONSE:
2183052dc82aSMatthias Ringwald             switch (gatt_client->state) {
21843deb3ec6SMatthias Ringwald                 case P_W4_EXECUTE_PREPARED_WRITE_RESULT:
2185526859e7SMatthias Ringwald                     gatt_client_handle_transaction_complete(gatt_client, ATT_ERROR_SUCCESS);
21863deb3ec6SMatthias Ringwald                     break;
21873deb3ec6SMatthias Ringwald                 case P_W4_CANCEL_PREPARED_WRITE_RESULT:
2188526859e7SMatthias Ringwald                     gatt_client_handle_transaction_complete(gatt_client, ATT_ERROR_SUCCESS);
21893deb3ec6SMatthias Ringwald                     break;
21903deb3ec6SMatthias Ringwald                 case P_W4_CANCEL_PREPARED_WRITE_DATA_MISMATCH_RESULT:
2191526859e7SMatthias Ringwald                     gatt_client_handle_transaction_complete(gatt_client, ATT_ERROR_DATA_MISMATCH);
21923deb3ec6SMatthias Ringwald                     break;
21933deb3ec6SMatthias Ringwald                 case P_W4_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT:
2194526859e7SMatthias Ringwald                     gatt_client_handle_transaction_complete(gatt_client, ATT_ERROR_SUCCESS);
21953deb3ec6SMatthias Ringwald                     break;
21963deb3ec6SMatthias Ringwald                 default:
21973deb3ec6SMatthias Ringwald                     break;
21983deb3ec6SMatthias Ringwald 
21993deb3ec6SMatthias Ringwald             }
22003deb3ec6SMatthias Ringwald             break;
22013deb3ec6SMatthias Ringwald 
22023deb3ec6SMatthias Ringwald         case ATT_READ_MULTIPLE_RESPONSE:
2203052dc82aSMatthias Ringwald             switch (gatt_client->state) {
22043deb3ec6SMatthias Ringwald                 case P_W4_READ_MULTIPLE_RESPONSE:
22055cf1669fSMatthias Ringwald                     report_gatt_characteristic_value(gatt_client, 0u, &packet[1], size - 1u);
2206526859e7SMatthias Ringwald                     gatt_client_handle_transaction_complete(gatt_client, ATT_ERROR_SUCCESS);
22073deb3ec6SMatthias Ringwald                     break;
22083deb3ec6SMatthias Ringwald                 default:
22093deb3ec6SMatthias Ringwald                     break;
22103deb3ec6SMatthias Ringwald             }
22113deb3ec6SMatthias Ringwald             break;
22123deb3ec6SMatthias Ringwald 
2213f125a8efSMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT
2214f125a8efSMatthias Ringwald         case ATT_READ_MULTIPLE_VARIABLE_RSP:
2215052dc82aSMatthias Ringwald             switch (gatt_client->state) {
2216169b9876SMatthias Ringwald                 case P_W4_READ_MULTIPLE_VARIABLE_RESPONSE:
2217f125a8efSMatthias Ringwald                     report_gatt_characteristic_value(gatt_client, 0u, &packet[1], size - 1u);
2218f125a8efSMatthias Ringwald                     gatt_client_handle_transaction_complete(gatt_client, ATT_ERROR_SUCCESS);
2219f125a8efSMatthias Ringwald                     break;
2220f125a8efSMatthias Ringwald                 default:
2221f125a8efSMatthias Ringwald                     break;
2222f125a8efSMatthias Ringwald             }
2223f125a8efSMatthias Ringwald             break;
2224f125a8efSMatthias Ringwald #endif
2225f125a8efSMatthias Ringwald 
22263deb3ec6SMatthias Ringwald         case ATT_ERROR_RESPONSE:
22274ea43905SMatthias Ringwald             if (size < 5u) return;
22284740230fSMatthias Ringwald             att_status = packet[4];
22294740230fSMatthias Ringwald             switch (att_status) {
22303deb3ec6SMatthias Ringwald                 case ATT_ERROR_ATTRIBUTE_NOT_FOUND: {
2231052dc82aSMatthias Ringwald                     switch (gatt_client->state) {
22323deb3ec6SMatthias Ringwald                         case P_W4_SERVICE_QUERY_RESULT:
22333deb3ec6SMatthias Ringwald                         case P_W4_SERVICE_WITH_UUID_RESULT:
22343deb3ec6SMatthias Ringwald                         case P_W4_INCLUDED_SERVICE_QUERY_RESULT:
22353deb3ec6SMatthias Ringwald                         case P_W4_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY_RESULT:
2236526859e7SMatthias Ringwald                             gatt_client_handle_transaction_complete(gatt_client, ATT_ERROR_SUCCESS);
22373deb3ec6SMatthias Ringwald                             break;
22383deb3ec6SMatthias Ringwald                         case P_W4_ALL_CHARACTERISTICS_OF_SERVICE_QUERY_RESULT:
22393deb3ec6SMatthias Ringwald                         case P_W4_CHARACTERISTIC_WITH_UUID_QUERY_RESULT:
224023d583b8SMatthias Ringwald                             report_gatt_characteristic_end_found(gatt_client, gatt_client->end_group_handle);
2241526859e7SMatthias Ringwald                             gatt_client_handle_transaction_complete(gatt_client, ATT_ERROR_SUCCESS);
22423deb3ec6SMatthias Ringwald                             break;
22433deb3ec6SMatthias Ringwald                         case P_W4_READ_BY_TYPE_RESPONSE:
22445cf1669fSMatthias Ringwald                             if (gatt_client->start_group_handle == gatt_client->query_start_handle) {
22454740230fSMatthias Ringwald                                 att_status = ATT_ERROR_ATTRIBUTE_NOT_FOUND;
22463deb3ec6SMatthias Ringwald                             } else {
22474740230fSMatthias Ringwald                                 att_status = ATT_ERROR_SUCCESS;
22483deb3ec6SMatthias Ringwald                             }
2249526859e7SMatthias Ringwald                             gatt_client_handle_transaction_complete(gatt_client, att_status);
22503deb3ec6SMatthias Ringwald                             break;
22513deb3ec6SMatthias Ringwald                         default:
22524740230fSMatthias Ringwald                             gatt_client_report_error_if_pending(gatt_client, att_status);
22533deb3ec6SMatthias Ringwald                             break;
22543deb3ec6SMatthias Ringwald                     }
22553deb3ec6SMatthias Ringwald                     break;
22563deb3ec6SMatthias Ringwald                 }
2257f4b33574SMatthias Ringwald 
2258f4b33574SMatthias Ringwald #ifdef ENABLE_GATT_CLIENT_PAIRING
2259f4b33574SMatthias Ringwald 
2260f4b33574SMatthias Ringwald                     case ATT_ERROR_INSUFFICIENT_AUTHENTICATION:
2261f4b33574SMatthias Ringwald                     case ATT_ERROR_INSUFFICIENT_ENCRYPTION_KEY_SIZE:
2262a0e0c2beSMatthias Ringwald                     case ATT_ERROR_INSUFFICIENT_ENCRYPTION: {
2263a0e0c2beSMatthias Ringwald 
2264f4b33574SMatthias Ringwald                         // security too low
22655cf1669fSMatthias Ringwald                         if (gatt_client->security_counter > 0) {
22664740230fSMatthias Ringwald                             gatt_client_report_error_if_pending(gatt_client, att_status);
2267f4b33574SMatthias Ringwald                             break;
2268f4b33574SMatthias Ringwald                         }
2269f4b33574SMatthias Ringwald                         // start security
22705cf1669fSMatthias Ringwald                         gatt_client->security_counter++;
2271f4b33574SMatthias Ringwald 
2272f4b33574SMatthias Ringwald                         // setup action
2273f4b33574SMatthias Ringwald                         int retry = 1;
2274052dc82aSMatthias Ringwald                         switch (gatt_client->state){
2275f4b33574SMatthias Ringwald                             case P_W4_READ_CHARACTERISTIC_VALUE_RESULT:
2276052dc82aSMatthias Ringwald                                 gatt_client->state = P_W2_SEND_READ_CHARACTERISTIC_VALUE_QUERY ;
2277f4b33574SMatthias Ringwald                                 break;
2278f4b33574SMatthias Ringwald                             case P_W4_READ_BLOB_RESULT:
2279052dc82aSMatthias Ringwald                                 gatt_client->state = P_W2_SEND_READ_BLOB_QUERY;
2280f4b33574SMatthias Ringwald                                 break;
2281f4b33574SMatthias Ringwald                             case P_W4_READ_BY_TYPE_RESPONSE:
2282052dc82aSMatthias Ringwald                                 gatt_client->state = P_W2_SEND_READ_BY_TYPE_REQUEST;
2283f4b33574SMatthias Ringwald                                 break;
2284f4b33574SMatthias Ringwald                             case P_W4_READ_MULTIPLE_RESPONSE:
2285052dc82aSMatthias Ringwald                                 gatt_client->state = P_W2_SEND_READ_MULTIPLE_REQUEST;
2286f4b33574SMatthias Ringwald                                 break;
2287f125a8efSMatthias Ringwald                             case P_W4_READ_MULTIPLE_VARIABLE_RESPONSE:
2288052dc82aSMatthias Ringwald                                 gatt_client->state = P_W2_SEND_READ_MULTIPLE_VARIABLE_REQUEST;
2289f125a8efSMatthias Ringwald                                 break;
2290f4b33574SMatthias Ringwald                             case P_W4_WRITE_CHARACTERISTIC_VALUE_RESULT:
2291052dc82aSMatthias Ringwald                                 gatt_client->state = P_W2_SEND_WRITE_CHARACTERISTIC_VALUE;
2292f4b33574SMatthias Ringwald                                 break;
2293f4b33574SMatthias Ringwald                             case P_W4_PREPARE_WRITE_RESULT:
2294052dc82aSMatthias Ringwald                                 gatt_client->state = P_W2_PREPARE_WRITE;
2295f4b33574SMatthias Ringwald                                 break;
2296f4b33574SMatthias Ringwald                             case P_W4_PREPARE_WRITE_SINGLE_RESULT:
2297052dc82aSMatthias Ringwald                                 gatt_client->state = P_W2_PREPARE_WRITE_SINGLE;
2298f4b33574SMatthias Ringwald                                 break;
2299f4b33574SMatthias Ringwald                             case P_W4_PREPARE_RELIABLE_WRITE_RESULT:
2300052dc82aSMatthias Ringwald                                 gatt_client->state = P_W2_PREPARE_RELIABLE_WRITE;
2301f4b33574SMatthias Ringwald                                 break;
2302f4b33574SMatthias Ringwald                             case P_W4_EXECUTE_PREPARED_WRITE_RESULT:
2303052dc82aSMatthias Ringwald                                 gatt_client->state = P_W2_EXECUTE_PREPARED_WRITE;
2304f4b33574SMatthias Ringwald                                 break;
2305f4b33574SMatthias Ringwald                             case P_W4_CANCEL_PREPARED_WRITE_RESULT:
2306052dc82aSMatthias Ringwald                                 gatt_client->state = P_W2_CANCEL_PREPARED_WRITE;
2307f4b33574SMatthias Ringwald                                 break;
2308f4b33574SMatthias Ringwald                             case P_W4_CANCEL_PREPARED_WRITE_DATA_MISMATCH_RESULT:
2309052dc82aSMatthias Ringwald                                 gatt_client->state = P_W2_CANCEL_PREPARED_WRITE_DATA_MISMATCH;
2310f4b33574SMatthias Ringwald                                 break;
2311f4b33574SMatthias Ringwald                             case P_W4_READ_CHARACTERISTIC_DESCRIPTOR_RESULT:
2312052dc82aSMatthias Ringwald                                 gatt_client->state = P_W2_SEND_READ_CHARACTERISTIC_DESCRIPTOR_QUERY;
2313f4b33574SMatthias Ringwald                                 break;
2314f4b33574SMatthias Ringwald                             case P_W4_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_RESULT:
2315052dc82aSMatthias Ringwald                                 gatt_client->state = P_W2_SEND_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_QUERY;
2316f4b33574SMatthias Ringwald                                 break;
2317f4b33574SMatthias Ringwald                             case P_W4_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT:
2318052dc82aSMatthias Ringwald                                 gatt_client->state = P_W2_SEND_WRITE_CHARACTERISTIC_DESCRIPTOR;
2319f4b33574SMatthias Ringwald                                 break;
2320f4b33574SMatthias Ringwald                             case P_W4_CLIENT_CHARACTERISTIC_CONFIGURATION_RESULT:
2321052dc82aSMatthias Ringwald                                 gatt_client->state = P_W2_WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION;
2322f4b33574SMatthias Ringwald                                 break;
2323f4b33574SMatthias Ringwald                             case P_W4_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT:
2324052dc82aSMatthias Ringwald                                 gatt_client->state = P_W2_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR;
2325f4b33574SMatthias Ringwald                                 break;
2326f4b33574SMatthias Ringwald                             case P_W4_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT:
2327052dc82aSMatthias Ringwald                                 gatt_client->state = P_W2_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR;
2328f4b33574SMatthias Ringwald                                 break;
2329f4b33574SMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
233030cf4543SMilanka Ringwald                             case P_W4_SEND_SIGNED_WRITE_DONE:
2331052dc82aSMatthias Ringwald                                 gatt_client->state = P_W2_SEND_SIGNED_WRITE;
2332f4b33574SMatthias Ringwald                                 break;
2333f4b33574SMatthias Ringwald #endif
2334f4b33574SMatthias Ringwald                             default:
2335052dc82aSMatthias Ringwald                                 log_info("retry not supported for state %x", gatt_client->state);
2336f4b33574SMatthias Ringwald                                 retry = 0;
2337f4b33574SMatthias Ringwald                                 break;
2338f4b33574SMatthias Ringwald                         }
2339f4b33574SMatthias Ringwald 
2340f4b33574SMatthias Ringwald                         if (!retry) {
23414740230fSMatthias Ringwald                             gatt_client_report_error_if_pending(gatt_client, att_status);
2342f4b33574SMatthias Ringwald                             break;
2343f4b33574SMatthias Ringwald                         }
2344f4b33574SMatthias Ringwald 
2345f4b33574SMatthias Ringwald                         log_info("security error, start pairing");
2346f4b33574SMatthias Ringwald 
2347f060f108SMatthias Ringwald                         // start pairing for higher security level
23482197dbafSMatthias Ringwald                         gatt_client->wait_for_authentication_complete = true;
23494740230fSMatthias Ringwald                         gatt_client->pending_error_code = att_status;
23505cf1669fSMatthias Ringwald                         sm_request_pairing(gatt_client->con_handle);
2351f4b33574SMatthias Ringwald                         break;
2352a0e0c2beSMatthias Ringwald                     }
2353f4b33574SMatthias Ringwald #endif
2354f4b33574SMatthias Ringwald 
2355f4b33574SMatthias Ringwald                     // nothing we can do about that
2356f4b33574SMatthias Ringwald                 case ATT_ERROR_INSUFFICIENT_AUTHORIZATION:
23573deb3ec6SMatthias Ringwald                 default:
23584740230fSMatthias Ringwald                     gatt_client_report_error_if_pending(gatt_client, att_status);
23593deb3ec6SMatthias Ringwald                     break;
23603deb3ec6SMatthias Ringwald             }
23613deb3ec6SMatthias Ringwald             break;
23623deb3ec6SMatthias Ringwald 
23633deb3ec6SMatthias Ringwald         default:
23643deb3ec6SMatthias Ringwald             log_info("ATT Handler, unhandled response type 0x%02x", packet[0]);
23653deb3ec6SMatthias Ringwald             break;
23663deb3ec6SMatthias Ringwald     }
2367cf8e5718SMatthias Ringwald }
2368cf8e5718SMatthias Ringwald 
2369cf8e5718SMatthias Ringwald static void gatt_client_att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size) {
2370cf8e5718SMatthias Ringwald     gatt_client_t *gatt_client;
2371180cbe79SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
2372b1da4983SMatthias Ringwald     uint8_t status;
2373b1da4983SMatthias Ringwald     hci_connection_t * hci_connection;
2374180cbe79SMatthias Ringwald     hci_con_handle_t con_handle;
2375180cbe79SMatthias Ringwald #endif
2376180cbe79SMatthias Ringwald 
2377cf8e5718SMatthias Ringwald     if (size < 1u) return;
237859b5e157SMatthias Ringwald     switch (packet_type){
237959b5e157SMatthias Ringwald         case HCI_EVENT_PACKET:
238059b5e157SMatthias Ringwald             switch (hci_event_packet_get_type(packet)) {
238159b5e157SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
238259b5e157SMatthias Ringwald                 case L2CAP_EVENT_CHANNEL_OPENED:
238359b5e157SMatthias Ringwald                     status = l2cap_event_channel_opened_get_status(packet);
238459b5e157SMatthias Ringwald                     gatt_client = gatt_client_get_context_for_l2cap_cid(l2cap_event_channel_opened_get_local_cid(packet));
2385de5a18caSMatthias Ringwald                     if (gatt_client != NULL){
2386180cbe79SMatthias Ringwald                         con_handle = l2cap_event_channel_opened_get_handle(packet);
2387180cbe79SMatthias Ringwald                         hci_connection = hci_connection_for_handle(con_handle);
2388ab6f9d8fSMatthias Ringwald                         if (status == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_RESOURCES){
2389ab6f9d8fSMatthias Ringwald                             if ((hci_connection != NULL) && hci_connection->att_server.incoming_connection_request) {
2390180cbe79SMatthias Ringwald                                 log_info("Collision, retry in 100ms");
2391180cbe79SMatthias Ringwald                                 gatt_client->state = P_W2_L2CAP_CONNECT;
2392180cbe79SMatthias Ringwald                                 // set timer for retry
2393b1da4983SMatthias Ringwald                                 btstack_run_loop_set_timer(&gatt_client->gc_timeout, GATT_CLIENT_COLLISION_BACKOFF_MS);
2394180cbe79SMatthias Ringwald                                 btstack_run_loop_set_timer_handler(&gatt_client->gc_timeout, gatt_client_classic_retry);
2395180cbe79SMatthias Ringwald                                 btstack_run_loop_add_timer(&gatt_client->gc_timeout);
2396180cbe79SMatthias Ringwald                                 break;
2397180cbe79SMatthias Ringwald                             }
2398ab6f9d8fSMatthias Ringwald                         }
239959b5e157SMatthias Ringwald                         // if status != 0, gatt_client will be discarded
2400052dc82aSMatthias Ringwald                         gatt_client->state = P_READY;
240159b5e157SMatthias Ringwald                         gatt_client->con_handle = l2cap_event_channel_opened_get_handle(packet);
240259b5e157SMatthias Ringwald                         gatt_client->mtu = l2cap_event_channel_opened_get_remote_mtu(packet);
240359b5e157SMatthias Ringwald                         gatt_client_classic_handle_connected(gatt_client, status);
2404de5a18caSMatthias Ringwald                     }
240559b5e157SMatthias Ringwald                     break;
240659b5e157SMatthias Ringwald                 case L2CAP_EVENT_CHANNEL_CLOSED:
240759b5e157SMatthias Ringwald                     gatt_client = gatt_client_get_context_for_l2cap_cid(l2cap_event_channel_closed_get_local_cid(packet));
2408de5a18caSMatthias Ringwald                     if (gatt_client != NULL){
240959b5e157SMatthias Ringwald                         // discard gatt client object
241059b5e157SMatthias Ringwald                         gatt_client_classic_handle_disconnected(gatt_client);
2411de5a18caSMatthias Ringwald                     }
241259b5e157SMatthias Ringwald                     break;
241359b5e157SMatthias Ringwald #endif
2414cf8e5718SMatthias Ringwald                 case L2CAP_EVENT_CAN_SEND_NOW:
24153deb3ec6SMatthias Ringwald                     gatt_client_run();
2416cf8e5718SMatthias Ringwald                     break;
2417cf8e5718SMatthias Ringwald                     // att_server has negotiated the mtu for this connection, cache if context exists
2418cf8e5718SMatthias Ringwald                 case ATT_EVENT_MTU_EXCHANGE_COMPLETE:
2419cf8e5718SMatthias Ringwald                     if (size < 6u) break;
2420cf8e5718SMatthias Ringwald                     gatt_client = gatt_client_get_context_for_handle(handle);
2421de5a18caSMatthias Ringwald                     if (gatt_client != NULL) {
2422cf8e5718SMatthias Ringwald                         gatt_client->mtu = little_endian_read_16(packet, 4);
2423de5a18caSMatthias Ringwald                     }
2424cf8e5718SMatthias Ringwald                     break;
2425cf8e5718SMatthias Ringwald                 default:
2426cf8e5718SMatthias Ringwald                     break;
2427cf8e5718SMatthias Ringwald             }
242859b5e157SMatthias Ringwald             break;
2429cf8e5718SMatthias Ringwald 
243059b5e157SMatthias Ringwald         case ATT_DATA_PACKET:
2431cf8e5718SMatthias Ringwald             // special cases: notifications & indications motivate creating context
2432cf8e5718SMatthias Ringwald             switch (packet[0]) {
2433cf8e5718SMatthias Ringwald                 case ATT_HANDLE_VALUE_NOTIFICATION:
2434cf8e5718SMatthias Ringwald                 case ATT_HANDLE_VALUE_INDICATION:
2435cf8e5718SMatthias Ringwald                     gatt_client_provide_context_for_handle(handle, &gatt_client);
2436cf8e5718SMatthias Ringwald                     break;
2437cf8e5718SMatthias Ringwald                 default:
2438cf8e5718SMatthias Ringwald                     gatt_client = gatt_client_get_context_for_handle(handle);
2439cf8e5718SMatthias Ringwald                     break;
2440cf8e5718SMatthias Ringwald             }
2441cf8e5718SMatthias Ringwald 
2442cf8e5718SMatthias Ringwald             if (gatt_client != NULL) {
2443cf8e5718SMatthias Ringwald                 gatt_client_handle_att_response(gatt_client, packet, size);
2444cf8e5718SMatthias Ringwald                 gatt_client_run();
2445cf8e5718SMatthias Ringwald             }
244659b5e157SMatthias Ringwald             break;
244759b5e157SMatthias Ringwald 
244859b5e157SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
244959b5e157SMatthias Ringwald         case L2CAP_DATA_PACKET:
245059b5e157SMatthias Ringwald             gatt_client = gatt_client_get_context_for_l2cap_cid(handle);
2451de5a18caSMatthias Ringwald             if (gatt_client != NULL){
245259b5e157SMatthias Ringwald                 gatt_client_handle_att_response(gatt_client, packet, size);
245359b5e157SMatthias Ringwald                 gatt_client_run();
2454de5a18caSMatthias Ringwald             }
245559b5e157SMatthias Ringwald             break;
245659b5e157SMatthias Ringwald #endif
245759b5e157SMatthias Ringwald 
245859b5e157SMatthias Ringwald         default:
245959b5e157SMatthias Ringwald             break;
246059b5e157SMatthias Ringwald     }
24613deb3ec6SMatthias Ringwald }
24623deb3ec6SMatthias Ringwald 
24637a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
24643deb3ec6SMatthias Ringwald static void att_signed_write_handle_cmac_result(uint8_t hash[8]){
2465665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
2466665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &gatt_client_connections);
2467665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
24685cf1669fSMatthias Ringwald         gatt_client_t * gatt_client = (gatt_client_t *) btstack_linked_list_iterator_next(&it);
2469052dc82aSMatthias Ringwald         if (gatt_client->state == P_W4_CMAC_RESULT){
24703deb3ec6SMatthias Ringwald             // store result
24715cf1669fSMatthias Ringwald             (void)memcpy(gatt_client->cmac, hash, 8);
24725cf1669fSMatthias Ringwald             // reverse_64(hash, gatt_client->cmac);
2473052dc82aSMatthias Ringwald             gatt_client->state = P_W2_SEND_SIGNED_WRITE;
24743deb3ec6SMatthias Ringwald             gatt_client_run();
24753deb3ec6SMatthias Ringwald             return;
24763deb3ec6SMatthias Ringwald         }
24773deb3ec6SMatthias Ringwald     }
24783deb3ec6SMatthias Ringwald }
24793deb3ec6SMatthias Ringwald 
2480b45b7749SMilanka Ringwald uint8_t gatt_client_signed_write_without_response(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t value_handle, uint16_t message_len, uint8_t * message){
248140faeb84SMilanka Ringwald     gatt_client_t * gatt_client;
248240faeb84SMilanka Ringwald     uint8_t status = gatt_client_provide_context_for_handle(con_handle, &gatt_client);
248340faeb84SMilanka Ringwald     if (status != ERROR_CODE_SUCCESS){
248440faeb84SMilanka Ringwald         return status;
248540faeb84SMilanka Ringwald     }
248640faeb84SMilanka Ringwald     if (is_ready(gatt_client) == 0){
248740faeb84SMilanka Ringwald         return GATT_CLIENT_IN_WRONG_STATE;
248840faeb84SMilanka Ringwald     }
24893deb3ec6SMatthias Ringwald 
24905cf1669fSMatthias Ringwald     gatt_client->callback = callback;
2491b45b7749SMilanka Ringwald     gatt_client->attribute_handle = value_handle;
24925cf1669fSMatthias Ringwald     gatt_client->attribute_length = message_len;
24935cf1669fSMatthias Ringwald     gatt_client->attribute_value = message;
2494052dc82aSMatthias Ringwald     gatt_client->state = P_W4_IDENTITY_RESOLVING;
24953deb3ec6SMatthias Ringwald     gatt_client_run();
249625b7c058SMilanka Ringwald     return ERROR_CODE_SUCCESS;
24973deb3ec6SMatthias Ringwald }
24987a766ebfSMatthias Ringwald #endif
24993deb3ec6SMatthias Ringwald 
2500711e6c80SMatthias Ringwald uint8_t gatt_client_discover_primary_services(btstack_packet_handler_t callback, hci_con_handle_t con_handle){
250140faeb84SMilanka Ringwald     gatt_client_t * gatt_client;
2502de27733dSMatthias Ringwald     uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
250340faeb84SMilanka Ringwald     if (status != ERROR_CODE_SUCCESS){
250440faeb84SMilanka Ringwald         return status;
250540faeb84SMilanka Ringwald     }
25063deb3ec6SMatthias Ringwald 
25075cf1669fSMatthias Ringwald     gatt_client->callback = callback;
25085cf1669fSMatthias Ringwald     gatt_client->start_group_handle = 0x0001;
25095cf1669fSMatthias Ringwald     gatt_client->end_group_handle   = 0xffff;
2510052dc82aSMatthias Ringwald     gatt_client->state = P_W2_SEND_SERVICE_QUERY;
25118d1f34d3SMatheus Eduardo Garbelini     gatt_client->uuid16 = GATT_PRIMARY_SERVICE_UUID;
25123deb3ec6SMatthias Ringwald     gatt_client_run();
251325b7c058SMilanka Ringwald     return ERROR_CODE_SUCCESS;
25143deb3ec6SMatthias Ringwald }
25153deb3ec6SMatthias Ringwald 
25168d1f34d3SMatheus Eduardo Garbelini uint8_t gatt_client_discover_secondary_services(btstack_packet_handler_t callback, hci_con_handle_t con_handle){
251740faeb84SMilanka Ringwald     gatt_client_t * gatt_client;
2518de27733dSMatthias Ringwald     uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
251940faeb84SMilanka Ringwald     if (status != ERROR_CODE_SUCCESS){
252040faeb84SMilanka Ringwald         return status;
252140faeb84SMilanka Ringwald     }
25228d1f34d3SMatheus Eduardo Garbelini 
25238d1f34d3SMatheus Eduardo Garbelini     gatt_client->callback = callback;
25248d1f34d3SMatheus Eduardo Garbelini     gatt_client->start_group_handle = 0x0001;
25258d1f34d3SMatheus Eduardo Garbelini     gatt_client->end_group_handle   = 0xffff;
2526052dc82aSMatthias Ringwald     gatt_client->state = P_W2_SEND_SERVICE_QUERY;
25278d1f34d3SMatheus Eduardo Garbelini     gatt_client->uuid16 = GATT_SECONDARY_SERVICE_UUID;
25288d1f34d3SMatheus Eduardo Garbelini     gatt_client_run();
25298d1f34d3SMatheus Eduardo Garbelini     return ERROR_CODE_SUCCESS;
25308d1f34d3SMatheus Eduardo Garbelini }
25313deb3ec6SMatthias Ringwald 
253268ced5c9SMatthias Ringwald uint8_t gatt_client_discover_primary_services_by_uuid16_with_context(btstack_packet_handler_t callback, hci_con_handle_t con_handle,
253368ced5c9SMatthias Ringwald                                                                      uint16_t uuid16, uint16_t service_id, uint16_t connection_id){
253440faeb84SMilanka Ringwald     gatt_client_t * gatt_client;
2535de27733dSMatthias Ringwald     uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
253640faeb84SMilanka Ringwald     if (status != ERROR_CODE_SUCCESS){
253740faeb84SMilanka Ringwald         return status;
253840faeb84SMilanka Ringwald     }
25393deb3ec6SMatthias Ringwald 
25405cf1669fSMatthias Ringwald     gatt_client->callback = callback;
254168ced5c9SMatthias Ringwald     gatt_client->service_id = service_id;
254268ced5c9SMatthias Ringwald     gatt_client->connection_id = connection_id;
25435cf1669fSMatthias Ringwald     gatt_client->start_group_handle = 0x0001;
25445cf1669fSMatthias Ringwald     gatt_client->end_group_handle   = 0xffff;
2545052dc82aSMatthias Ringwald     gatt_client->state = P_W2_SEND_SERVICE_WITH_UUID_QUERY;
25465cf1669fSMatthias Ringwald     gatt_client->uuid16 = uuid16;
25475cf1669fSMatthias Ringwald     uuid_add_bluetooth_prefix((uint8_t*) &(gatt_client->uuid128), gatt_client->uuid16);
25483deb3ec6SMatthias Ringwald     gatt_client_run();
254925b7c058SMilanka Ringwald     return ERROR_CODE_SUCCESS;
25503deb3ec6SMatthias Ringwald }
25513deb3ec6SMatthias Ringwald 
255268ced5c9SMatthias Ringwald uint8_t gatt_client_discover_primary_services_by_uuid16(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t uuid16){
255368ced5c9SMatthias Ringwald     return gatt_client_discover_primary_services_by_uuid16_with_context(callback, con_handle, uuid16, 0, 0);
255468ced5c9SMatthias Ringwald }
255568ced5c9SMatthias Ringwald 
2556711e6c80SMatthias 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){
255740faeb84SMilanka Ringwald     gatt_client_t * gatt_client;
2558de27733dSMatthias Ringwald     uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
255940faeb84SMilanka Ringwald     if (status != ERROR_CODE_SUCCESS){
256040faeb84SMilanka Ringwald         return status;
256140faeb84SMilanka Ringwald     }
25623deb3ec6SMatthias Ringwald 
25635cf1669fSMatthias Ringwald     gatt_client->callback = callback;
25645cf1669fSMatthias Ringwald     gatt_client->start_group_handle = 0x0001;
25655cf1669fSMatthias Ringwald     gatt_client->end_group_handle   = 0xffff;
25665cf1669fSMatthias Ringwald     gatt_client->uuid16 = 0;
25675cf1669fSMatthias Ringwald     (void)memcpy(gatt_client->uuid128, uuid128, 16);
2568052dc82aSMatthias Ringwald     gatt_client->state = P_W2_SEND_SERVICE_WITH_UUID_QUERY;
25693deb3ec6SMatthias Ringwald     gatt_client_run();
257025b7c058SMilanka Ringwald     return ERROR_CODE_SUCCESS;
25713deb3ec6SMatthias Ringwald }
25723deb3ec6SMatthias Ringwald 
25738183ac30SMatthias Ringwald uint8_t gatt_client_discover_characteristics_for_service_with_context(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_service_t * service,
25748183ac30SMatthias Ringwald                                                                       uint16_t service_id, uint16_t connection_id){
257540faeb84SMilanka Ringwald     gatt_client_t * gatt_client;
2576de27733dSMatthias Ringwald     uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
257740faeb84SMilanka Ringwald     if (status != ERROR_CODE_SUCCESS){
257840faeb84SMilanka Ringwald         return status;
257940faeb84SMilanka Ringwald     }
25803deb3ec6SMatthias Ringwald 
25815cf1669fSMatthias Ringwald     gatt_client->callback = callback;
25828183ac30SMatthias Ringwald     gatt_client->service_id = service_id;
25838183ac30SMatthias Ringwald     gatt_client->connection_id = connection_id;
25845cf1669fSMatthias Ringwald     gatt_client->start_group_handle = service->start_group_handle;
25855cf1669fSMatthias Ringwald     gatt_client->end_group_handle   = service->end_group_handle;
2586ab415e75SMatthias Ringwald     gatt_client->filter_with_uuid = false;
25875cf1669fSMatthias Ringwald     gatt_client->characteristic_start_handle = 0;
2588052dc82aSMatthias Ringwald     gatt_client->state = P_W2_SEND_ALL_CHARACTERISTICS_OF_SERVICE_QUERY;
25893deb3ec6SMatthias Ringwald     gatt_client_run();
259025b7c058SMilanka Ringwald     return ERROR_CODE_SUCCESS;
25913deb3ec6SMatthias Ringwald }
25928183ac30SMatthias Ringwald 
25938183ac30SMatthias 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){
25948183ac30SMatthias Ringwald     return gatt_client_discover_characteristics_for_service_with_context(callback, con_handle, service, 0, 0);
25958183ac30SMatthias Ringwald }
25968183ac30SMatthias Ringwald 
2597aa43543aSMatthias Ringwald uint8_t gatt_client_find_included_services_for_service_with_context(btstack_packet_handler_t callback, hci_con_handle_t con_handle,
2598aa43543aSMatthias Ringwald                                                                     gatt_client_service_t * service, uint16_t service_id, uint16_t connection_id){
259940faeb84SMilanka Ringwald     gatt_client_t * gatt_client;
2600de27733dSMatthias Ringwald     uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
260140faeb84SMilanka Ringwald     if (status != ERROR_CODE_SUCCESS){
260240faeb84SMilanka Ringwald         return status;
260340faeb84SMilanka Ringwald     }
2604de27733dSMatthias Ringwald 
26055cf1669fSMatthias Ringwald     gatt_client->callback = callback;
2606aa43543aSMatthias Ringwald     gatt_client->service_id = service_id;
2607aa43543aSMatthias Ringwald     gatt_client->connection_id = connection_id;
26085cf1669fSMatthias Ringwald     gatt_client->start_group_handle = service->start_group_handle;
26095cf1669fSMatthias Ringwald     gatt_client->end_group_handle   = service->end_group_handle;
2610052dc82aSMatthias Ringwald     gatt_client->state = P_W2_SEND_INCLUDED_SERVICE_QUERY;
26113deb3ec6SMatthias Ringwald 
26123deb3ec6SMatthias Ringwald     gatt_client_run();
261325b7c058SMilanka Ringwald     return ERROR_CODE_SUCCESS;
26143deb3ec6SMatthias Ringwald }
26153deb3ec6SMatthias Ringwald 
2616aa43543aSMatthias 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) {
2617aa43543aSMatthias Ringwald     return gatt_client_find_included_services_for_service_with_context(callback, con_handle, service, 0, 0);
2618aa43543aSMatthias Ringwald }
2619aa43543aSMatthias Ringwald 
2620711e6c80SMatthias 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){
262140faeb84SMilanka Ringwald     gatt_client_t * gatt_client;
2622de27733dSMatthias Ringwald     uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
262340faeb84SMilanka Ringwald     if (status != ERROR_CODE_SUCCESS){
262440faeb84SMilanka Ringwald         return status;
262540faeb84SMilanka Ringwald     }
26263deb3ec6SMatthias Ringwald 
26275cf1669fSMatthias Ringwald     gatt_client->callback = callback;
26285cf1669fSMatthias Ringwald     gatt_client->start_group_handle = start_handle;
26295cf1669fSMatthias Ringwald     gatt_client->end_group_handle   = end_handle;
2630ab415e75SMatthias Ringwald     gatt_client->filter_with_uuid = true;
26315cf1669fSMatthias Ringwald     gatt_client->uuid16 = uuid16;
26325cf1669fSMatthias Ringwald     uuid_add_bluetooth_prefix((uint8_t*) &(gatt_client->uuid128), uuid16);
26335cf1669fSMatthias Ringwald     gatt_client->characteristic_start_handle = 0;
2634052dc82aSMatthias Ringwald     gatt_client->state = P_W2_SEND_CHARACTERISTIC_WITH_UUID_QUERY;
26353deb3ec6SMatthias Ringwald     gatt_client_run();
263625b7c058SMilanka Ringwald     return ERROR_CODE_SUCCESS;
26373deb3ec6SMatthias Ringwald }
26383deb3ec6SMatthias Ringwald 
2639045d700dSDavid Lechner 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, const uint8_t * uuid128){
264040faeb84SMilanka Ringwald     gatt_client_t * gatt_client;
2641de27733dSMatthias Ringwald     uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
264240faeb84SMilanka Ringwald     if (status != ERROR_CODE_SUCCESS){
264340faeb84SMilanka Ringwald         return status;
264440faeb84SMilanka Ringwald     }
26453deb3ec6SMatthias Ringwald 
26465cf1669fSMatthias Ringwald     gatt_client->callback = callback;
26475cf1669fSMatthias Ringwald     gatt_client->start_group_handle = start_handle;
26485cf1669fSMatthias Ringwald     gatt_client->end_group_handle   = end_handle;
2649ab415e75SMatthias Ringwald     gatt_client->filter_with_uuid = true;
26505cf1669fSMatthias Ringwald     gatt_client->uuid16 = 0;
26515cf1669fSMatthias Ringwald     (void)memcpy(gatt_client->uuid128, uuid128, 16);
26525cf1669fSMatthias Ringwald     gatt_client->characteristic_start_handle = 0;
2653052dc82aSMatthias Ringwald     gatt_client->state = P_W2_SEND_CHARACTERISTIC_WITH_UUID_QUERY;
26543deb3ec6SMatthias Ringwald     gatt_client_run();
265525b7c058SMilanka Ringwald     return ERROR_CODE_SUCCESS;
26563deb3ec6SMatthias Ringwald }
26573deb3ec6SMatthias Ringwald 
26583deb3ec6SMatthias Ringwald 
2659b45b7749SMilanka Ringwald uint8_t gatt_client_discover_characteristics_for_service_by_uuid16(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_service_t * service, uint16_t uuid16){
2660b45b7749SMilanka Ringwald     return gatt_client_discover_characteristics_for_handle_range_by_uuid16(callback, con_handle, service->start_group_handle, service->end_group_handle, uuid16);
26613deb3ec6SMatthias Ringwald }
26623deb3ec6SMatthias Ringwald 
2663b45b7749SMilanka Ringwald uint8_t gatt_client_discover_characteristics_for_service_by_uuid128(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_service_t * service, const uint8_t * uuid128){
2664b45b7749SMilanka Ringwald     return gatt_client_discover_characteristics_for_handle_range_by_uuid128(callback, con_handle, service->start_group_handle, service->end_group_handle, uuid128);
26653deb3ec6SMatthias Ringwald }
26663deb3ec6SMatthias Ringwald 
2667a94d23eaSMatthias Ringwald uint8_t gatt_client_discover_characteristic_descriptors_with_context(btstack_packet_handler_t callback, hci_con_handle_t con_handle,
2668a94d23eaSMatthias Ringwald                                                                      gatt_client_characteristic_t * characteristic,  uint16_t service_id, uint16_t connection_id){
266940faeb84SMilanka Ringwald     gatt_client_t * gatt_client;
2670de27733dSMatthias Ringwald     uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
267140faeb84SMilanka Ringwald     if (status != ERROR_CODE_SUCCESS){
267240faeb84SMilanka Ringwald         return status;
267340faeb84SMilanka Ringwald     }
26743deb3ec6SMatthias Ringwald 
2675a94d23eaSMatthias Ringwald     gatt_client->service_id = service_id;
2676a94d23eaSMatthias Ringwald     gatt_client->connection_id = connection_id;
2677a94d23eaSMatthias Ringwald 
2678544a5845SMatthias Ringwald     // check if there is space for characteristics descriptors
2679544a5845SMatthias Ringwald     if (characteristic->end_handle > characteristic->value_handle){
26805cf1669fSMatthias Ringwald         gatt_client->callback = callback;
26815cf1669fSMatthias Ringwald         gatt_client->start_group_handle = characteristic->value_handle + 1u;
26825cf1669fSMatthias Ringwald         gatt_client->end_group_handle   = characteristic->end_handle;
2683052dc82aSMatthias Ringwald         gatt_client->state = P_W2_SEND_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY;
26843deb3ec6SMatthias Ringwald         gatt_client_run();
2685544a5845SMatthias Ringwald     } else {
2686544a5845SMatthias Ringwald         // schedule gatt complete event on next run loop iteration otherwise
2687544a5845SMatthias Ringwald         gatt_client->state = P_W2_EMIT_QUERY_COMPLETE_EVENT;
2688544a5845SMatthias Ringwald         gatt_client_deferred_event_emit.callback = gatt_client_emit_events;
2689544a5845SMatthias Ringwald         btstack_run_loop_execute_on_main_thread(&gatt_client_deferred_event_emit);
2690544a5845SMatthias Ringwald     }
269125b7c058SMilanka Ringwald     return ERROR_CODE_SUCCESS;
26923deb3ec6SMatthias Ringwald }
26933deb3ec6SMatthias Ringwald 
2694a94d23eaSMatthias Ringwald uint8_t gatt_client_discover_characteristic_descriptors(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_characteristic_t * characteristic){
2695a94d23eaSMatthias Ringwald     return gatt_client_discover_characteristic_descriptors_with_context(callback, con_handle, characteristic, 0, 0);
2696a94d23eaSMatthias Ringwald }
2697a94d23eaSMatthias Ringwald 
2698e38d764eSMatthias Ringwald uint8_t gatt_client_read_value_of_characteristic_using_value_handle_with_context(btstack_packet_handler_t callback,
2699e38d764eSMatthias Ringwald                                                                                  hci_con_handle_t con_handle,
2700e38d764eSMatthias Ringwald                                                                                  uint16_t value_handle,
2701e38d764eSMatthias Ringwald                                                                                  uint16_t service_id,
2702e38d764eSMatthias Ringwald                                                                                  uint16_t connection_id) {
270340faeb84SMilanka Ringwald     gatt_client_t * gatt_client;
2704de27733dSMatthias Ringwald     uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
270540faeb84SMilanka Ringwald     if (status != ERROR_CODE_SUCCESS){
270640faeb84SMilanka Ringwald         return status;
270740faeb84SMilanka Ringwald     }
27083deb3ec6SMatthias Ringwald 
27095cf1669fSMatthias Ringwald     gatt_client->callback = callback;
2710e38d764eSMatthias Ringwald     gatt_client->service_id = service_id;
2711e38d764eSMatthias Ringwald     gatt_client->connection_id = connection_id;
27125cf1669fSMatthias Ringwald     gatt_client->attribute_handle = value_handle;
27135cf1669fSMatthias Ringwald     gatt_client->attribute_offset = 0;
2714052dc82aSMatthias Ringwald     gatt_client->state = P_W2_SEND_READ_CHARACTERISTIC_VALUE_QUERY;
27153deb3ec6SMatthias Ringwald     gatt_client_run();
271625b7c058SMilanka Ringwald     return ERROR_CODE_SUCCESS;
27173deb3ec6SMatthias Ringwald }
27183deb3ec6SMatthias Ringwald 
2719e38d764eSMatthias 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){
2720e38d764eSMatthias Ringwald     return gatt_client_read_value_of_characteristic_using_value_handle_with_context(callback, con_handle, value_handle, 0, 0);
2721e38d764eSMatthias Ringwald 
2722e38d764eSMatthias Ringwald }
2723e38d764eSMatthias Ringwald 
2724711e6c80SMatthias 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){
272540faeb84SMilanka Ringwald     gatt_client_t * gatt_client;
2726de27733dSMatthias Ringwald     uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
272740faeb84SMilanka Ringwald     if (status != ERROR_CODE_SUCCESS){
272840faeb84SMilanka Ringwald         return status;
272940faeb84SMilanka Ringwald     }
27303deb3ec6SMatthias Ringwald 
27315cf1669fSMatthias Ringwald     gatt_client->callback = callback;
27325cf1669fSMatthias Ringwald     gatt_client->start_group_handle = start_handle;
27335cf1669fSMatthias Ringwald     gatt_client->end_group_handle = end_handle;
27345cf1669fSMatthias Ringwald     gatt_client->query_start_handle = start_handle;
27355cf1669fSMatthias Ringwald     gatt_client->query_end_handle = end_handle;
27365cf1669fSMatthias Ringwald     gatt_client->uuid16 = uuid16;
27375cf1669fSMatthias Ringwald     uuid_add_bluetooth_prefix((uint8_t*) &(gatt_client->uuid128), uuid16);
2738052dc82aSMatthias Ringwald     gatt_client->state = P_W2_SEND_READ_BY_TYPE_REQUEST;
27393deb3ec6SMatthias Ringwald     gatt_client_run();
274025b7c058SMilanka Ringwald     return ERROR_CODE_SUCCESS;
27413deb3ec6SMatthias Ringwald }
27423deb3ec6SMatthias Ringwald 
2743045d700dSDavid Lechner 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, const uint8_t * uuid128){
274440faeb84SMilanka Ringwald     gatt_client_t * gatt_client;
2745de27733dSMatthias Ringwald     uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
274640faeb84SMilanka Ringwald     if (status != ERROR_CODE_SUCCESS){
274740faeb84SMilanka Ringwald         return status;
274840faeb84SMilanka Ringwald     }
27493deb3ec6SMatthias Ringwald 
27505cf1669fSMatthias Ringwald     gatt_client->callback = callback;
27515cf1669fSMatthias Ringwald     gatt_client->start_group_handle = start_handle;
27525cf1669fSMatthias Ringwald     gatt_client->end_group_handle = end_handle;
27535cf1669fSMatthias Ringwald     gatt_client->query_start_handle = start_handle;
27545cf1669fSMatthias Ringwald     gatt_client->query_end_handle = end_handle;
27555cf1669fSMatthias Ringwald     gatt_client->uuid16 = 0;
27565cf1669fSMatthias Ringwald     (void)memcpy(gatt_client->uuid128, uuid128, 16);
2757052dc82aSMatthias Ringwald     gatt_client->state = P_W2_SEND_READ_BY_TYPE_REQUEST;
27583deb3ec6SMatthias Ringwald     gatt_client_run();
275925b7c058SMilanka Ringwald     return ERROR_CODE_SUCCESS;
27603deb3ec6SMatthias Ringwald }
27613deb3ec6SMatthias Ringwald 
27623deb3ec6SMatthias Ringwald 
2763b45b7749SMilanka Ringwald uint8_t gatt_client_read_value_of_characteristic(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_characteristic_t * characteristic){
2764b45b7749SMilanka Ringwald     return gatt_client_read_value_of_characteristic_using_value_handle(callback, con_handle, characteristic->value_handle);
27653deb3ec6SMatthias Ringwald }
27663deb3ec6SMatthias Ringwald 
2767b45b7749SMilanka 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 value_handle, uint16_t offset){
276840faeb84SMilanka Ringwald     gatt_client_t * gatt_client;
2769de27733dSMatthias Ringwald     uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
277040faeb84SMilanka Ringwald     if (status != ERROR_CODE_SUCCESS){
277140faeb84SMilanka Ringwald         return status;
277240faeb84SMilanka Ringwald     }
27733deb3ec6SMatthias Ringwald 
27745cf1669fSMatthias Ringwald     gatt_client->callback = callback;
2775b45b7749SMilanka Ringwald     gatt_client->attribute_handle = value_handle;
27765cf1669fSMatthias Ringwald     gatt_client->attribute_offset = offset;
2777052dc82aSMatthias Ringwald     gatt_client->state = P_W2_SEND_READ_BLOB_QUERY;
27783deb3ec6SMatthias Ringwald     gatt_client_run();
277925b7c058SMilanka Ringwald     return ERROR_CODE_SUCCESS;
27803deb3ec6SMatthias Ringwald }
2781691e02c2SMatthias Ringwald uint8_t gatt_client_read_long_value_of_characteristic_using_value_handle_with_context(btstack_packet_handler_t callback,
2782691e02c2SMatthias Ringwald                                                                                       hci_con_handle_t con_handle, uint16_t value_handle,
2783691e02c2SMatthias Ringwald                                                                                       uint16_t service_id, uint16_t connection_id){
2784691e02c2SMatthias Ringwald     // TODO: move into gatt_client_read_long_value_of_characteristic_using_value_handle_with_offset once
2785691e02c2SMatthias Ringwald     //       gatt_client_read_long_value_of_characteristic_using_value_handle_with_offset_and_context exists
2786691e02c2SMatthias Ringwald     gatt_client_t * gatt_client;
2787691e02c2SMatthias Ringwald     uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
2788691e02c2SMatthias Ringwald     if (status != ERROR_CODE_SUCCESS){
2789691e02c2SMatthias Ringwald         return status;
2790691e02c2SMatthias Ringwald     }
2791691e02c2SMatthias Ringwald     gatt_client->service_id = service_id;
2792691e02c2SMatthias Ringwald     gatt_client->connection_id = connection_id;
2793691e02c2SMatthias Ringwald     return gatt_client_read_long_value_of_characteristic_using_value_handle_with_offset(callback, con_handle, value_handle, 0);
2794691e02c2SMatthias Ringwald }
27953deb3ec6SMatthias Ringwald 
2796b45b7749SMilanka 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 value_handle){
2797691e02c2SMatthias Ringwald     return gatt_client_read_long_value_of_characteristic_using_value_handle_with_context(callback, con_handle, value_handle, 0, 0);
27983deb3ec6SMatthias Ringwald }
27993deb3ec6SMatthias Ringwald 
2800b45b7749SMilanka Ringwald uint8_t gatt_client_read_long_value_of_characteristic(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_characteristic_t * characteristic){
2801b45b7749SMilanka Ringwald     return gatt_client_read_long_value_of_characteristic_using_value_handle(callback, con_handle, characteristic->value_handle);
28023deb3ec6SMatthias Ringwald }
28033deb3ec6SMatthias Ringwald 
2804f125a8efSMatthias Ringwald static uint8_t gatt_client_read_multiple_characteristic_values_with_state(btstack_packet_handler_t callback, hci_con_handle_t con_handle, int num_value_handles, uint16_t * value_handles, gatt_client_state_t state){
280540faeb84SMilanka Ringwald     gatt_client_t * gatt_client;
2806de27733dSMatthias Ringwald     uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
280740faeb84SMilanka Ringwald     if (status != ERROR_CODE_SUCCESS){
280840faeb84SMilanka Ringwald         return status;
280940faeb84SMilanka Ringwald     }
28103deb3ec6SMatthias Ringwald 
2811f125a8efSMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT
2812f125a8efSMatthias Ringwald     if (state == P_W2_SEND_READ_MULTIPLE_VARIABLE_REQUEST){
2813f125a8efSMatthias Ringwald         if (gatt_client->bearer_type != ATT_BEARER_ENHANCED_LE){
2814f125a8efSMatthias Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
2815f125a8efSMatthias Ringwald         }
2816f125a8efSMatthias Ringwald     }
2817f125a8efSMatthias Ringwald #endif
2818f125a8efSMatthias Ringwald 
28195cf1669fSMatthias Ringwald     gatt_client->callback = callback;
28205cf1669fSMatthias Ringwald     gatt_client->read_multiple_handle_count = num_value_handles;
28215cf1669fSMatthias Ringwald     gatt_client->read_multiple_handles = value_handles;
2822052dc82aSMatthias Ringwald     gatt_client->state = state;
28233deb3ec6SMatthias Ringwald     gatt_client_run();
282425b7c058SMilanka Ringwald     return ERROR_CODE_SUCCESS;
28253deb3ec6SMatthias Ringwald }
28263deb3ec6SMatthias Ringwald 
2827f125a8efSMatthias 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){
2828f125a8efSMatthias Ringwald     return gatt_client_read_multiple_characteristic_values_with_state(callback, con_handle, num_value_handles, value_handles, P_W2_SEND_READ_MULTIPLE_REQUEST);
2829f125a8efSMatthias Ringwald }
2830f125a8efSMatthias Ringwald 
2831f125a8efSMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT
2832f125a8efSMatthias Ringwald uint8_t gatt_client_read_multiple_variable_characteristic_values(btstack_packet_handler_t callback, hci_con_handle_t con_handle, int num_value_handles, uint16_t * value_handles){
2833f125a8efSMatthias Ringwald     return gatt_client_read_multiple_characteristic_values_with_state(callback, con_handle, num_value_handles, value_handles, P_W2_SEND_READ_MULTIPLE_VARIABLE_REQUEST);
2834f125a8efSMatthias Ringwald }
2835f125a8efSMatthias Ringwald #endif
2836f125a8efSMatthias Ringwald 
2837de9f8e94SMatthias 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){
283840faeb84SMilanka Ringwald     gatt_client_t * gatt_client;
283940faeb84SMilanka Ringwald     uint8_t status = gatt_client_provide_context_for_handle(con_handle, &gatt_client);
284040faeb84SMilanka Ringwald     if (status != ERROR_CODE_SUCCESS){
284140faeb84SMilanka Ringwald         return status;
284240faeb84SMilanka Ringwald     }
28433deb3ec6SMatthias Ringwald 
2844dda77937SMatthias Ringwald     if (value_length > (gatt_client->mtu - 3u)) return GATT_CLIENT_VALUE_TOO_LONG;
28455cf1669fSMatthias Ringwald     if (!att_dispatch_client_can_send_now(gatt_client->con_handle)) return GATT_CLIENT_BUSY;
28463deb3ec6SMatthias Ringwald 
28476e7b444cSMatthias Ringwald     return att_write_request(gatt_client, ATT_WRITE_COMMAND, value_handle, value_length, value);
28483deb3ec6SMatthias Ringwald }
2849b444aa75SMatthias Ringwald uint8_t gatt_client_write_value_of_characteristic_with_context(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t value_handle,
2850b444aa75SMatthias Ringwald                                                                uint16_t value_length, uint8_t * value, uint16_t service_id, uint16_t connection_id){
285140faeb84SMilanka Ringwald     gatt_client_t * gatt_client;
2852de27733dSMatthias Ringwald     uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
285340faeb84SMilanka Ringwald     if (status != ERROR_CODE_SUCCESS){
285440faeb84SMilanka Ringwald         return status;
285540faeb84SMilanka Ringwald     }
28563deb3ec6SMatthias Ringwald 
28575cf1669fSMatthias Ringwald     gatt_client->callback = callback;
2858b444aa75SMatthias Ringwald     gatt_client->service_id = service_id;
2859b444aa75SMatthias Ringwald     gatt_client->connection_id = connection_id;
28605cf1669fSMatthias Ringwald     gatt_client->attribute_handle = value_handle;
28615cf1669fSMatthias Ringwald     gatt_client->attribute_length = value_length;
2862b45b7749SMilanka Ringwald     gatt_client->attribute_value = value;
2863052dc82aSMatthias Ringwald     gatt_client->state = P_W2_SEND_WRITE_CHARACTERISTIC_VALUE;
28643deb3ec6SMatthias Ringwald     gatt_client_run();
286525b7c058SMilanka Ringwald     return ERROR_CODE_SUCCESS;
28663deb3ec6SMatthias Ringwald }
2867b444aa75SMatthias 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 * value) {
2868b444aa75SMatthias Ringwald     return gatt_client_write_value_of_characteristic_with_context(callback, con_handle, value_handle, value_length, value, 0, 0);
2869b444aa75SMatthias Ringwald }
28703deb3ec6SMatthias Ringwald 
2871b45b7749SMilanka 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 * value){
287240faeb84SMilanka Ringwald     gatt_client_t * gatt_client;
2873de27733dSMatthias Ringwald     uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
287440faeb84SMilanka Ringwald     if (status != ERROR_CODE_SUCCESS){
287540faeb84SMilanka Ringwald         return status;
287640faeb84SMilanka Ringwald     }
28773deb3ec6SMatthias Ringwald 
28785cf1669fSMatthias Ringwald     gatt_client->callback = callback;
28795cf1669fSMatthias Ringwald     gatt_client->attribute_handle = value_handle;
28805cf1669fSMatthias Ringwald     gatt_client->attribute_length = value_length;
28815cf1669fSMatthias Ringwald     gatt_client->attribute_offset = offset;
2882b45b7749SMilanka Ringwald     gatt_client->attribute_value = value;
2883052dc82aSMatthias Ringwald     gatt_client->state = P_W2_PREPARE_WRITE;
28843deb3ec6SMatthias Ringwald     gatt_client_run();
288525b7c058SMilanka Ringwald     return ERROR_CODE_SUCCESS;
28863deb3ec6SMatthias Ringwald }
28873deb3ec6SMatthias Ringwald 
2888b538fb89SMatthias Ringwald uint8_t gatt_client_write_long_value_of_characteristic_with_context(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t value_handle, uint16_t value_length, uint8_t * value, uint16_t service_id, uint16_t connection_id){
2889b538fb89SMatthias Ringwald     // TODO: move into gatt_client_write_long_value_of_characteristic_with_offset once gatt_client_write_long_value_of_characteristic_with_offset_with_context exists
2890b538fb89SMatthias Ringwald     gatt_client_t * gatt_client;
2891b538fb89SMatthias Ringwald     uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
2892b538fb89SMatthias Ringwald     if (status != ERROR_CODE_SUCCESS){
2893b538fb89SMatthias Ringwald         return status;
2894b538fb89SMatthias Ringwald     }
2895b538fb89SMatthias Ringwald     gatt_client->service_id = service_id;
2896b538fb89SMatthias Ringwald     gatt_client->connection_id = connection_id;
28979c662c9bSMatthias Ringwald     return gatt_client_write_long_value_of_characteristic_with_offset(callback, con_handle, value_handle, 0, value_length, value);
28983deb3ec6SMatthias Ringwald }
28993deb3ec6SMatthias Ringwald 
2900b538fb89SMatthias 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){
2901b538fb89SMatthias Ringwald     return gatt_client_write_long_value_of_characteristic_with_context(callback, con_handle, value_handle, value_length, value, 0, 0);
2902b538fb89SMatthias Ringwald }
2903b538fb89SMatthias Ringwald 
2904711e6c80SMatthias 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){
290540faeb84SMilanka Ringwald     gatt_client_t * gatt_client;
2906de27733dSMatthias Ringwald     uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
290740faeb84SMilanka Ringwald     if (status != ERROR_CODE_SUCCESS){
290840faeb84SMilanka Ringwald         return status;
290940faeb84SMilanka Ringwald     }
29103deb3ec6SMatthias Ringwald 
29115cf1669fSMatthias Ringwald     gatt_client->callback = callback;
29125cf1669fSMatthias Ringwald     gatt_client->attribute_handle = value_handle;
29135cf1669fSMatthias Ringwald     gatt_client->attribute_length = value_length;
29145cf1669fSMatthias Ringwald     gatt_client->attribute_offset = 0;
29155cf1669fSMatthias Ringwald     gatt_client->attribute_value = value;
2916052dc82aSMatthias Ringwald     gatt_client->state = P_W2_PREPARE_RELIABLE_WRITE;
29173deb3ec6SMatthias Ringwald     gatt_client_run();
291825b7c058SMilanka Ringwald     return ERROR_CODE_SUCCESS;
29193deb3ec6SMatthias Ringwald }
29203deb3ec6SMatthias Ringwald 
2921cb5b2b64SMatthias Ringwald uint8_t gatt_client_write_client_characteristic_configuration_with_context(btstack_packet_handler_t callback, hci_con_handle_t con_handle,
2922cb5b2b64SMatthias Ringwald                                                               gatt_client_characteristic_t * characteristic, uint16_t configuration, uint16_t service_id, uint16_t connection_id){
292340faeb84SMilanka Ringwald     gatt_client_t * gatt_client;
2924de27733dSMatthias Ringwald     uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
292540faeb84SMilanka Ringwald     if (status != ERROR_CODE_SUCCESS){
292640faeb84SMilanka Ringwald         return status;
292740faeb84SMilanka Ringwald     }
29283deb3ec6SMatthias Ringwald 
29298915696fSMilanka Ringwald     if (configuration > 3){
29308915696fSMilanka Ringwald         return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
29318915696fSMilanka Ringwald     }
29328915696fSMilanka Ringwald 
29333deb3ec6SMatthias Ringwald     if ( (configuration & GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION) &&
29344ea43905SMatthias Ringwald         ((characteristic->properties & ATT_PROPERTY_NOTIFY) == 0u)) {
2935d8e8f12aSMatthias Ringwald         log_info("gatt_client_write_client_characteristic_configuration: GATT_CLIENT_CHARACTERISTIC_NOTIFICATION_NOT_SUPPORTED");
2936616edd56SMatthias Ringwald         return GATT_CLIENT_CHARACTERISTIC_NOTIFICATION_NOT_SUPPORTED;
29373deb3ec6SMatthias Ringwald     } else if ( (configuration & GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_INDICATION) &&
29384ea43905SMatthias Ringwald                ((characteristic->properties & ATT_PROPERTY_INDICATE) == 0u)){
2939d8e8f12aSMatthias Ringwald         log_info("gatt_client_write_client_characteristic_configuration: GATT_CLIENT_CHARACTERISTIC_INDICATION_NOT_SUPPORTED");
2940616edd56SMatthias Ringwald         return GATT_CLIENT_CHARACTERISTIC_INDICATION_NOT_SUPPORTED;
29413deb3ec6SMatthias Ringwald     }
29423deb3ec6SMatthias Ringwald 
29435cf1669fSMatthias Ringwald     gatt_client->callback = callback;
2944cb5b2b64SMatthias Ringwald     gatt_client->service_id = service_id;
2945cb5b2b64SMatthias Ringwald     gatt_client->connection_id = connection_id;
29465cf1669fSMatthias Ringwald     gatt_client->start_group_handle = characteristic->value_handle;
29475cf1669fSMatthias Ringwald     gatt_client->end_group_handle = characteristic->end_handle;
29485cf1669fSMatthias Ringwald     little_endian_store_16(gatt_client->client_characteristic_configuration_value, 0, configuration);
29493deb3ec6SMatthias Ringwald 
2950abdc9fb5SMatthias Ringwald #ifdef ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERY
29515cf1669fSMatthias Ringwald     gatt_client->gatt_client_state = P_W2_SEND_FIND_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY;
2952abdc9fb5SMatthias Ringwald #else
2953052dc82aSMatthias Ringwald     gatt_client->state = P_W2_SEND_READ_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY;
2954abdc9fb5SMatthias Ringwald #endif
29553deb3ec6SMatthias Ringwald     gatt_client_run();
29569cb80b17SMilanka Ringwald     return ERROR_CODE_SUCCESS;
29573deb3ec6SMatthias Ringwald }
29583deb3ec6SMatthias Ringwald 
2959cb5b2b64SMatthias 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){
2960cb5b2b64SMatthias Ringwald     return gatt_client_write_client_characteristic_configuration_with_context(callback, con_handle, characteristic, configuration, 0, 0);
2961cb5b2b64SMatthias Ringwald }
2962cb5b2b64SMatthias Ringwald 
2963711e6c80SMatthias 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){
296440faeb84SMilanka Ringwald     gatt_client_t * gatt_client;
2965de27733dSMatthias Ringwald     uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
296640faeb84SMilanka Ringwald     if (status != ERROR_CODE_SUCCESS){
296740faeb84SMilanka Ringwald         return status;
296840faeb84SMilanka Ringwald     }
29693deb3ec6SMatthias Ringwald 
29705cf1669fSMatthias Ringwald     gatt_client->callback = callback;
29715cf1669fSMatthias Ringwald     gatt_client->attribute_handle = descriptor_handle;
29723deb3ec6SMatthias Ringwald 
2973052dc82aSMatthias Ringwald     gatt_client->state = P_W2_SEND_READ_CHARACTERISTIC_DESCRIPTOR_QUERY;
29743deb3ec6SMatthias Ringwald     gatt_client_run();
297525b7c058SMilanka Ringwald     return ERROR_CODE_SUCCESS;
29763deb3ec6SMatthias Ringwald }
29773deb3ec6SMatthias Ringwald 
2978711e6c80SMatthias 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){
29799c662c9bSMatthias Ringwald     return gatt_client_read_characteristic_descriptor_using_descriptor_handle(callback, con_handle, descriptor->handle);
29803deb3ec6SMatthias Ringwald }
29813deb3ec6SMatthias Ringwald 
2982711e6c80SMatthias 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){
298340faeb84SMilanka Ringwald     gatt_client_t * gatt_client;
2984de27733dSMatthias Ringwald     uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
298540faeb84SMilanka Ringwald     if (status != ERROR_CODE_SUCCESS){
298640faeb84SMilanka Ringwald         return status;
298740faeb84SMilanka Ringwald     }
29883deb3ec6SMatthias Ringwald 
29895cf1669fSMatthias Ringwald     gatt_client->callback = callback;
29905cf1669fSMatthias Ringwald     gatt_client->attribute_handle = descriptor_handle;
29915cf1669fSMatthias Ringwald     gatt_client->attribute_offset = offset;
2992052dc82aSMatthias Ringwald     gatt_client->state = P_W2_SEND_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_QUERY;
29933deb3ec6SMatthias Ringwald     gatt_client_run();
299425b7c058SMilanka Ringwald     return ERROR_CODE_SUCCESS;
29953deb3ec6SMatthias Ringwald }
29963deb3ec6SMatthias Ringwald 
2997711e6c80SMatthias 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){
29989c662c9bSMatthias Ringwald     return gatt_client_read_long_characteristic_descriptor_using_descriptor_handle_with_offset(callback, con_handle, descriptor_handle, 0);
29993deb3ec6SMatthias Ringwald }
30003deb3ec6SMatthias Ringwald 
3001711e6c80SMatthias 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){
30029c662c9bSMatthias Ringwald     return gatt_client_read_long_characteristic_descriptor_using_descriptor_handle(callback, con_handle, descriptor->handle);
30033deb3ec6SMatthias Ringwald }
30043deb3ec6SMatthias Ringwald 
3005b45b7749SMilanka 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 value_length, uint8_t * value){
300640faeb84SMilanka Ringwald     gatt_client_t * gatt_client;
3007de27733dSMatthias Ringwald     uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
300840faeb84SMilanka Ringwald     if (status != ERROR_CODE_SUCCESS){
300940faeb84SMilanka Ringwald         return status;
301040faeb84SMilanka Ringwald     }
30113deb3ec6SMatthias Ringwald 
30125cf1669fSMatthias Ringwald     gatt_client->callback = callback;
30135cf1669fSMatthias Ringwald     gatt_client->attribute_handle = descriptor_handle;
3014b45b7749SMilanka Ringwald     gatt_client->attribute_length = value_length;
30155cf1669fSMatthias Ringwald     gatt_client->attribute_offset = 0;
3016b45b7749SMilanka Ringwald     gatt_client->attribute_value = value;
3017052dc82aSMatthias Ringwald     gatt_client->state = P_W2_SEND_WRITE_CHARACTERISTIC_DESCRIPTOR;
30183deb3ec6SMatthias Ringwald     gatt_client_run();
301925b7c058SMilanka Ringwald     return ERROR_CODE_SUCCESS;
30203deb3ec6SMatthias Ringwald }
30213deb3ec6SMatthias Ringwald 
302248cdff9cSMilanka 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 value_length, uint8_t * value){
302348cdff9cSMilanka Ringwald     return gatt_client_write_characteristic_descriptor_using_descriptor_handle(callback, con_handle, descriptor->handle, value_length, value);
30243deb3ec6SMatthias Ringwald }
30253deb3ec6SMatthias Ringwald 
3026b45b7749SMilanka 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 value_length, uint8_t * value){
302740faeb84SMilanka Ringwald     gatt_client_t * gatt_client;
3028de27733dSMatthias Ringwald     uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
302940faeb84SMilanka Ringwald     if (status != ERROR_CODE_SUCCESS){
303040faeb84SMilanka Ringwald         return status;
303140faeb84SMilanka Ringwald     }
30323deb3ec6SMatthias Ringwald 
30335cf1669fSMatthias Ringwald     gatt_client->callback = callback;
30345cf1669fSMatthias Ringwald     gatt_client->attribute_handle = descriptor_handle;
3035b45b7749SMilanka Ringwald     gatt_client->attribute_length = value_length;
30365cf1669fSMatthias Ringwald     gatt_client->attribute_offset = offset;
3037b45b7749SMilanka Ringwald     gatt_client->attribute_value = value;
3038052dc82aSMatthias Ringwald     gatt_client->state = P_W2_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR;
30393deb3ec6SMatthias Ringwald     gatt_client_run();
304025b7c058SMilanka Ringwald     return ERROR_CODE_SUCCESS;
30413deb3ec6SMatthias Ringwald }
30423deb3ec6SMatthias Ringwald 
3043b45b7749SMilanka 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 value_length, uint8_t * value){
3044b45b7749SMilanka Ringwald     return gatt_client_write_long_characteristic_descriptor_using_descriptor_handle_with_offset(callback, con_handle, descriptor_handle, 0, value_length, value);
30453deb3ec6SMatthias Ringwald }
30463deb3ec6SMatthias Ringwald 
3047b45b7749SMilanka 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 value_length, uint8_t * value){
3048b45b7749SMilanka Ringwald     return gatt_client_write_long_characteristic_descriptor_using_descriptor_handle(callback, con_handle, descriptor->handle, value_length, value);
30493deb3ec6SMatthias Ringwald }
30503deb3ec6SMatthias Ringwald 
30513deb3ec6SMatthias Ringwald /**
30523deb3ec6SMatthias Ringwald  * @brief -> gatt complete event
30533deb3ec6SMatthias Ringwald  */
3054b45b7749SMilanka 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 value_length, uint8_t * value){
305540faeb84SMilanka Ringwald     gatt_client_t * gatt_client;
3056de27733dSMatthias Ringwald     uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
305740faeb84SMilanka Ringwald     if (status != ERROR_CODE_SUCCESS){
305840faeb84SMilanka Ringwald         return status;
305940faeb84SMilanka Ringwald     }
30603deb3ec6SMatthias Ringwald 
30615cf1669fSMatthias Ringwald     gatt_client->callback = callback;
30625cf1669fSMatthias Ringwald     gatt_client->attribute_handle = attribute_handle;
3063b45b7749SMilanka Ringwald     gatt_client->attribute_length = value_length;
30645cf1669fSMatthias Ringwald     gatt_client->attribute_offset = offset;
3065b45b7749SMilanka Ringwald     gatt_client->attribute_value = value;
3066052dc82aSMatthias Ringwald     gatt_client->state = P_W2_PREPARE_WRITE_SINGLE;
30673deb3ec6SMatthias Ringwald     gatt_client_run();
306825b7c058SMilanka Ringwald     return ERROR_CODE_SUCCESS;
30693deb3ec6SMatthias Ringwald }
30703deb3ec6SMatthias Ringwald 
30713deb3ec6SMatthias Ringwald /**
30723deb3ec6SMatthias Ringwald  * @brief -> gatt complete event
30733deb3ec6SMatthias Ringwald  */
3074711e6c80SMatthias Ringwald uint8_t gatt_client_execute_write(btstack_packet_handler_t callback, hci_con_handle_t con_handle){
307540faeb84SMilanka Ringwald     gatt_client_t * gatt_client;
3076de27733dSMatthias Ringwald     uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
307740faeb84SMilanka Ringwald     if (status != ERROR_CODE_SUCCESS){
307840faeb84SMilanka Ringwald         return status;
307940faeb84SMilanka Ringwald     }
30803deb3ec6SMatthias Ringwald 
30815cf1669fSMatthias Ringwald     gatt_client->callback = callback;
3082052dc82aSMatthias Ringwald     gatt_client->state = P_W2_EXECUTE_PREPARED_WRITE;
30833deb3ec6SMatthias Ringwald     gatt_client_run();
308425b7c058SMilanka Ringwald     return ERROR_CODE_SUCCESS;
30853deb3ec6SMatthias Ringwald }
30863deb3ec6SMatthias Ringwald 
30873deb3ec6SMatthias Ringwald /**
30883deb3ec6SMatthias Ringwald  * @brief -> gatt complete event
30893deb3ec6SMatthias Ringwald  */
3090711e6c80SMatthias Ringwald uint8_t gatt_client_cancel_write(btstack_packet_handler_t callback, hci_con_handle_t con_handle){
309140faeb84SMilanka Ringwald     gatt_client_t * gatt_client;
3092de27733dSMatthias Ringwald     uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
309340faeb84SMilanka Ringwald     if (status != ERROR_CODE_SUCCESS){
309440faeb84SMilanka Ringwald         return status;
309540faeb84SMilanka Ringwald     }
30963deb3ec6SMatthias Ringwald 
30975cf1669fSMatthias Ringwald     gatt_client->callback = callback;
3098052dc82aSMatthias Ringwald     gatt_client->state = P_W2_CANCEL_PREPARED_WRITE;
30993deb3ec6SMatthias Ringwald     gatt_client_run();
31007d2258b3SMilanka Ringwald     return ERROR_CODE_SUCCESS;
31013deb3ec6SMatthias Ringwald }
31023deb3ec6SMatthias Ringwald 
3103313e337bSMatthias Ringwald void gatt_client_deserialize_service(const uint8_t *packet, int offset, gatt_client_service_t * service){
31046ba2ad22SMatthias Ringwald     service->start_group_handle = little_endian_read_16(packet, offset);
31056ba2ad22SMatthias Ringwald     service->end_group_handle = little_endian_read_16(packet, offset + 2);
31066ba2ad22SMatthias Ringwald     reverse_128(&packet[offset + 4], service->uuid128);
31076ba2ad22SMatthias Ringwald     if (uuid_has_bluetooth_prefix(service->uuid128)){
31086ba2ad22SMatthias Ringwald         service->uuid16 = big_endian_read_32(service->uuid128, 0);
3109c839c6f9SMatthias Ringwald     } else {
3110c839c6f9SMatthias Ringwald         service->uuid16 = 0;
31116ba2ad22SMatthias Ringwald     }
31126ba2ad22SMatthias Ringwald }
31136ba2ad22SMatthias Ringwald 
3114313e337bSMatthias Ringwald void gatt_client_deserialize_characteristic(const uint8_t * packet, int offset, gatt_client_characteristic_t * characteristic){
31156ba2ad22SMatthias Ringwald     characteristic->start_handle = little_endian_read_16(packet, offset);
31166ba2ad22SMatthias Ringwald     characteristic->value_handle = little_endian_read_16(packet, offset + 2);
31176ba2ad22SMatthias Ringwald     characteristic->end_handle = little_endian_read_16(packet, offset + 4);
31186ba2ad22SMatthias Ringwald     characteristic->properties = little_endian_read_16(packet, offset + 6);
311986c38559SMatthias Ringwald     reverse_128(&packet[offset+8], characteristic->uuid128);
31206ba2ad22SMatthias Ringwald     if (uuid_has_bluetooth_prefix(characteristic->uuid128)){
31216ba2ad22SMatthias Ringwald         characteristic->uuid16 = big_endian_read_32(characteristic->uuid128, 0);
3122c839c6f9SMatthias Ringwald     } else {
3123c839c6f9SMatthias Ringwald         characteristic->uuid16 = 0;
31246ba2ad22SMatthias Ringwald     }
31256ba2ad22SMatthias Ringwald }
31266ba2ad22SMatthias Ringwald 
3127313e337bSMatthias Ringwald void gatt_client_deserialize_characteristic_descriptor(const uint8_t * packet, int offset, gatt_client_characteristic_descriptor_t * descriptor){
31286ba2ad22SMatthias Ringwald     descriptor->handle = little_endian_read_16(packet, offset);
31296ba2ad22SMatthias Ringwald     reverse_128(&packet[offset+2], descriptor->uuid128);
3130b4895529SJakob Krantz     if (uuid_has_bluetooth_prefix(descriptor->uuid128)){
3131b4895529SJakob Krantz         descriptor->uuid16 = big_endian_read_32(descriptor->uuid128, 0);
3132c839c6f9SMatthias Ringwald     } else {
3133c839c6f9SMatthias Ringwald         descriptor->uuid16 = 0;
3134b4895529SJakob Krantz     }
31356ba2ad22SMatthias Ringwald }
31365cf6c434SJakob Krantz 
31375cf6c434SJakob Krantz void gatt_client_send_mtu_negotiation(btstack_packet_handler_t callback, hci_con_handle_t con_handle){
313840faeb84SMilanka Ringwald     gatt_client_t * gatt_client;
313940faeb84SMilanka Ringwald     uint8_t status = gatt_client_provide_context_for_handle(con_handle, &gatt_client);
314040faeb84SMilanka Ringwald     if (status != ERROR_CODE_SUCCESS){
314140faeb84SMilanka Ringwald         return;
314240faeb84SMilanka Ringwald     }
314340faeb84SMilanka Ringwald     if (gatt_client->mtu_state == MTU_AUTO_EXCHANGE_DISABLED){
314440faeb84SMilanka Ringwald         gatt_client->callback = callback;
314540faeb84SMilanka Ringwald         gatt_client->mtu_state = SEND_MTU_EXCHANGE;
31465cf6c434SJakob Krantz         gatt_client_run();
31475cf6c434SJakob Krantz     }
31485cf6c434SJakob Krantz }
314947181045SMatthias Ringwald 
315059d34cd2SMatthias Ringwald uint8_t gatt_client_request_to_write_without_response(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
315159d34cd2SMatthias Ringwald     gatt_client_t * gatt_client;
315259d34cd2SMatthias Ringwald     uint8_t status = gatt_client_provide_context_for_handle(con_handle, &gatt_client);
315359d34cd2SMatthias Ringwald     if (status != ERROR_CODE_SUCCESS){
315459d34cd2SMatthias Ringwald         return status;
315559d34cd2SMatthias Ringwald     }
315659d34cd2SMatthias Ringwald     bool added = btstack_linked_list_add_tail(&gatt_client->write_without_response_requests, (btstack_linked_item_t*) callback_registration);
31572832a98aSMatthias Ringwald     if (added == false){
315859d34cd2SMatthias Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
31592832a98aSMatthias Ringwald     } else {
31602832a98aSMatthias Ringwald         att_dispatch_client_request_can_send_now_event(gatt_client->con_handle);
31612832a98aSMatthias Ringwald         return ERROR_CODE_SUCCESS;
316259d34cd2SMatthias Ringwald     }
316359d34cd2SMatthias Ringwald }
316459d34cd2SMatthias Ringwald 
316553e9c18fSMatthias Ringwald uint8_t gatt_client_request_to_send_gatt_query(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
316653e9c18fSMatthias Ringwald     gatt_client_t * gatt_client;
316753e9c18fSMatthias Ringwald     uint8_t status = gatt_client_provide_context_for_handle(con_handle, &gatt_client);
316853e9c18fSMatthias Ringwald     if (status != ERROR_CODE_SUCCESS){
316953e9c18fSMatthias Ringwald         return status;
317053e9c18fSMatthias Ringwald     }
317153e9c18fSMatthias Ringwald     bool added = btstack_linked_list_add_tail(&gatt_client->query_requests, (btstack_linked_item_t*) callback_registration);
31722832a98aSMatthias Ringwald     if (added == false){
317353e9c18fSMatthias Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
31742832a98aSMatthias Ringwald     } else {
31752832a98aSMatthias Ringwald         gatt_client_notify_can_send_query(gatt_client);
31762832a98aSMatthias Ringwald         return ERROR_CODE_SUCCESS;
317753e9c18fSMatthias Ringwald     }
317853e9c18fSMatthias Ringwald }
317953e9c18fSMatthias Ringwald 
318048553f67SDirk Helbig uint8_t gatt_client_remove_gatt_query(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
318148553f67SDirk Helbig     gatt_client_t * gatt_client;
318248553f67SDirk Helbig     uint8_t status = gatt_client_provide_context_for_handle(con_handle, &gatt_client);
318348553f67SDirk Helbig     if (status != ERROR_CODE_SUCCESS){
318448553f67SDirk Helbig         return status;
318548553f67SDirk Helbig     }
318648553f67SDirk Helbig     (void)btstack_linked_list_remove(&gatt_client->query_requests, (btstack_linked_item_t*) callback_registration);
318748553f67SDirk Helbig     return ERROR_CODE_SUCCESS;
318848553f67SDirk Helbig }
318948553f67SDirk Helbig 
319047181045SMatthias Ringwald uint8_t gatt_client_request_can_write_without_response_event(btstack_packet_handler_t callback, hci_con_handle_t con_handle){
319140faeb84SMilanka Ringwald     gatt_client_t * gatt_client;
319240faeb84SMilanka Ringwald     uint8_t status = gatt_client_provide_context_for_handle(con_handle, &gatt_client);
319340faeb84SMilanka Ringwald     if (status != ERROR_CODE_SUCCESS){
319440faeb84SMilanka Ringwald         return status;
319540faeb84SMilanka Ringwald     }
319640faeb84SMilanka Ringwald     if (gatt_client->write_without_response_callback != NULL){
319740faeb84SMilanka Ringwald         return GATT_CLIENT_IN_WRONG_STATE;
319840faeb84SMilanka Ringwald     }
319940faeb84SMilanka Ringwald     gatt_client->write_without_response_callback = callback;
320040faeb84SMilanka Ringwald     att_dispatch_client_request_can_send_now_event(gatt_client->con_handle);
32017d2258b3SMilanka Ringwald     return ERROR_CODE_SUCCESS;
320247181045SMatthias Ringwald }
3203842492f0SMatthias Ringwald 
3204842492f0SMatthias Ringwald #ifdef ENABLE_GATT_CLIENT_SERVICE_CHANGED
320558e8c9f5SMatthias Ringwald void gatt_client_add_service_changed_handler(btstack_packet_callback_registration_t * callback) {
320658e8c9f5SMatthias Ringwald     btstack_linked_list_add_tail(&gatt_client_service_changed_handler, (btstack_linked_item_t*) callback);
320758e8c9f5SMatthias Ringwald }
3208a6121b51SMilanka Ringwald 
320958e8c9f5SMatthias Ringwald void gatt_client_remove_service_changed_handler(btstack_packet_callback_registration_t * callback){
321058e8c9f5SMatthias Ringwald     btstack_linked_list_remove(&gatt_client_service_changed_handler, (btstack_linked_item_t*) callback);
321158e8c9f5SMatthias Ringwald }
3212842492f0SMatthias Ringwald #endif
32137627a0deSMatthias Ringwald 
32147627a0deSMatthias Ringwald #if defined(ENABLE_GATT_OVER_CLASSIC) || defined(ENABLE_GATT_OVER_EATT)
32151450cdc6SMatthias Ringwald 
32161450cdc6SMatthias Ringwald #include "hci_event.h"
32171450cdc6SMatthias Ringwald 
32181450cdc6SMatthias Ringwald static const hci_event_t gatt_client_connected = {
3219b2d70d58SMatthias Ringwald         GATT_EVENT_CONNECTED, 0, "11BH"
32201450cdc6SMatthias Ringwald };
32211450cdc6SMatthias Ringwald 
32226b4a68c3SMatthias Ringwald static const hci_event_t gatt_client_disconnected = {
32236b4a68c3SMatthias Ringwald         GATT_EVENT_DISCONNECTED, 0, "H"
32246b4a68c3SMatthias Ringwald };
32251c7a19feSMatthias Ringwald 
3226b2d70d58SMatthias Ringwald static void
3227b2d70d58SMatthias Ringwald gatt_client_emit_connected(btstack_packet_handler_t callback, uint8_t status, bd_addr_type_t addr_type, bd_addr_t addr,
32281c7a19feSMatthias Ringwald                            hci_con_handle_t con_handle) {
32291c7a19feSMatthias Ringwald     uint8_t buffer[20];
32301c7a19feSMatthias Ringwald     uint16_t len = hci_event_create_from_template_and_arguments(buffer, sizeof(buffer), &gatt_client_connected, status, addr, con_handle);
32311c7a19feSMatthias Ringwald     (*callback)(HCI_EVENT_PACKET, 0, buffer, len);
32321c7a19feSMatthias Ringwald }
32331c7a19feSMatthias Ringwald 
32347627a0deSMatthias Ringwald #endif
32357627a0deSMatthias Ringwald 
32367627a0deSMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
32377627a0deSMatthias Ringwald 
32387627a0deSMatthias Ringwald #include "bluetooth_psm.h"
32397627a0deSMatthias Ringwald 
32407627a0deSMatthias Ringwald // single active SDP query
32417627a0deSMatthias Ringwald static gatt_client_t * gatt_client_classic_active_sdp_query;
32427627a0deSMatthias Ringwald 
32437627a0deSMatthias Ringwald // macos protocol descriptor list requires 16 bytes
32447627a0deSMatthias Ringwald static uint8_t gatt_client_classic_sdp_buffer[32];
32457627a0deSMatthias Ringwald 
32466b4a68c3SMatthias Ringwald 
32471450cdc6SMatthias Ringwald static gatt_client_t * gatt_client_get_context_for_classic_addr(bd_addr_t addr){
32481450cdc6SMatthias Ringwald     btstack_linked_item_t *it;
32491450cdc6SMatthias Ringwald     for (it = (btstack_linked_item_t *) gatt_client_connections; it != NULL; it = it->next){
32501450cdc6SMatthias Ringwald         gatt_client_t * gatt_client = (gatt_client_t *) it;
32511450cdc6SMatthias Ringwald         if (memcmp(gatt_client->addr, addr, 6) == 0){
32521450cdc6SMatthias Ringwald             return gatt_client;
32531450cdc6SMatthias Ringwald         }
32541450cdc6SMatthias Ringwald     }
32551450cdc6SMatthias Ringwald     return NULL;
32561450cdc6SMatthias Ringwald }
32571450cdc6SMatthias Ringwald 
32581450cdc6SMatthias Ringwald static gatt_client_t * gatt_client_get_context_for_l2cap_cid(uint16_t l2cap_cid){
32591450cdc6SMatthias Ringwald     btstack_linked_item_t *it;
32601450cdc6SMatthias Ringwald     for (it = (btstack_linked_item_t *) gatt_client_connections; it != NULL; it = it->next){
32611450cdc6SMatthias Ringwald         gatt_client_t * gatt_client = (gatt_client_t *) it;
32621450cdc6SMatthias Ringwald         if (gatt_client->l2cap_cid == l2cap_cid){
32631450cdc6SMatthias Ringwald             return gatt_client;
32641450cdc6SMatthias Ringwald         }
32651450cdc6SMatthias Ringwald     }
32661450cdc6SMatthias Ringwald     return NULL;
32671450cdc6SMatthias Ringwald }
32681450cdc6SMatthias Ringwald 
32691450cdc6SMatthias Ringwald static void gatt_client_classic_handle_connected(gatt_client_t * gatt_client, uint8_t status){
3270b2d70d58SMatthias Ringwald     // cache peer information
32711450cdc6SMatthias Ringwald     bd_addr_t addr;
3272d5529700SMatthias Ringwald     // cppcheck-suppress uninitvar ; addr is reported as uninitialized although it's the destination of the memcpy
32731450cdc6SMatthias Ringwald     memcpy(addr, gatt_client->addr, 6);
3274b2d70d58SMatthias Ringwald     bd_addr_type_t addr_type = gatt_client->addr_type;
3275b2d70d58SMatthias Ringwald     gatt_client->addr_type = BD_ADDR_TYPE_ACL;
32761450cdc6SMatthias Ringwald     hci_con_handle_t con_handle = gatt_client->con_handle;
32771450cdc6SMatthias Ringwald     btstack_packet_handler_t callback = gatt_client->callback;
3278b2d70d58SMatthias Ringwald 
32799ed6f53cSMatthias Ringwald     if (status != ERROR_CODE_SUCCESS){
32803b0e0cc5SMatthias Ringwald         btstack_linked_list_remove(&gatt_client_connections, (btstack_linked_item_t *) gatt_client);
32813b0e0cc5SMatthias Ringwald         btstack_memory_gatt_client_free(gatt_client);
32823b0e0cc5SMatthias Ringwald     }
3283b2d70d58SMatthias Ringwald 
3284b2d70d58SMatthias Ringwald     gatt_client_emit_connected(callback, status, addr_type, addr, con_handle);
32851450cdc6SMatthias Ringwald }
32861450cdc6SMatthias Ringwald 
3287180cbe79SMatthias Ringwald static void gatt_client_classic_retry(btstack_timer_source_t * ts){
3288180cbe79SMatthias Ringwald     gatt_client_t * gatt_client = gatt_client_for_timer(ts);
3289180cbe79SMatthias Ringwald     if (gatt_client != NULL){
3290180cbe79SMatthias Ringwald         gatt_client->state = P_W4_L2CAP_CONNECTION;
3291180cbe79SMatthias Ringwald         att_dispatch_classic_connect(gatt_client->addr, gatt_client->l2cap_psm, &gatt_client->l2cap_cid);
3292180cbe79SMatthias Ringwald     }
3293180cbe79SMatthias Ringwald }
3294180cbe79SMatthias Ringwald 
32956b4a68c3SMatthias Ringwald static void gatt_client_classic_handle_disconnected(gatt_client_t * gatt_client){
32966b4a68c3SMatthias Ringwald 
32976b4a68c3SMatthias Ringwald     gatt_client_report_error_if_pending(gatt_client, ATT_ERROR_HCI_DISCONNECT_RECEIVED);
32986b4a68c3SMatthias Ringwald     gatt_client_timeout_stop(gatt_client);
32996b4a68c3SMatthias Ringwald 
33006b4a68c3SMatthias Ringwald     hci_con_handle_t con_handle = gatt_client->con_handle;
33016b4a68c3SMatthias Ringwald     btstack_packet_handler_t callback = gatt_client->callback;
33026b4a68c3SMatthias Ringwald     btstack_linked_list_remove(&gatt_client_connections, (btstack_linked_item_t *) gatt_client);
33036b4a68c3SMatthias Ringwald     btstack_memory_gatt_client_free(gatt_client);
33046b4a68c3SMatthias Ringwald 
33056b4a68c3SMatthias Ringwald     uint8_t buffer[20];
33066b4a68c3SMatthias Ringwald     uint16_t len = hci_event_create_from_template_and_arguments(buffer, sizeof(buffer), &gatt_client_disconnected, con_handle);
33076b4a68c3SMatthias Ringwald     (*callback)(HCI_EVENT_PACKET, 0, buffer, len);
33086b4a68c3SMatthias Ringwald }
33096b4a68c3SMatthias Ringwald 
33101450cdc6SMatthias Ringwald static void gatt_client_handle_sdp_client_query_attribute_value(gatt_client_t * connection, uint8_t *packet){
33111450cdc6SMatthias Ringwald     des_iterator_t des_list_it;
33121450cdc6SMatthias Ringwald     des_iterator_t prot_it;
33131450cdc6SMatthias Ringwald 
33141450cdc6SMatthias Ringwald     if (sdp_event_query_attribute_byte_get_attribute_length(packet) <= sizeof(gatt_client_classic_sdp_buffer)) {
33151450cdc6SMatthias Ringwald         gatt_client_classic_sdp_buffer[sdp_event_query_attribute_byte_get_data_offset(packet)] = sdp_event_query_attribute_byte_get_data(packet);
33161450cdc6SMatthias Ringwald         if ((uint16_t)(sdp_event_query_attribute_byte_get_data_offset(packet)+1) == sdp_event_query_attribute_byte_get_attribute_length(packet)) {
33171450cdc6SMatthias Ringwald             switch(sdp_event_query_attribute_byte_get_attribute_id(packet)) {
33181450cdc6SMatthias Ringwald                 case BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST:
33191450cdc6SMatthias Ringwald                     for (des_iterator_init(&des_list_it, gatt_client_classic_sdp_buffer); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)) {
33201450cdc6SMatthias Ringwald                         uint8_t       *des_element;
33211450cdc6SMatthias Ringwald                         uint8_t       *element;
33221450cdc6SMatthias Ringwald                         uint32_t       uuid;
33231450cdc6SMatthias Ringwald 
33241450cdc6SMatthias Ringwald                         if (des_iterator_get_type(&des_list_it) != DE_DES) continue;
33251450cdc6SMatthias Ringwald 
33261450cdc6SMatthias Ringwald                         des_element = des_iterator_get_element(&des_list_it);
33271450cdc6SMatthias Ringwald                         des_iterator_init(&prot_it, des_element);
33281450cdc6SMatthias Ringwald                         element = des_iterator_get_element(&prot_it);
33291450cdc6SMatthias Ringwald 
33301450cdc6SMatthias Ringwald                         if (de_get_element_type(element) != DE_UUID) continue;
33311450cdc6SMatthias Ringwald 
33321450cdc6SMatthias Ringwald                         uuid = de_get_uuid32(element);
33331450cdc6SMatthias Ringwald                         des_iterator_next(&prot_it);
33341450cdc6SMatthias Ringwald                         // we assume that the even if there are both roles supported, remote device uses the same psm and avdtp version for both
33351450cdc6SMatthias Ringwald                         switch (uuid){
33361450cdc6SMatthias Ringwald                             case BLUETOOTH_PROTOCOL_L2CAP:
33371450cdc6SMatthias Ringwald                                 if (!des_iterator_has_more(&prot_it)) continue;
33381450cdc6SMatthias Ringwald                                 de_element_get_uint16(des_iterator_get_element(&prot_it), &connection->l2cap_psm);
33391450cdc6SMatthias Ringwald                                 break;
33401450cdc6SMatthias Ringwald                             default:
33411450cdc6SMatthias Ringwald                                 break;
33421450cdc6SMatthias Ringwald                         }
33431450cdc6SMatthias Ringwald                     }
33441450cdc6SMatthias Ringwald                     break;
33451450cdc6SMatthias Ringwald 
33461450cdc6SMatthias Ringwald                 default:
33471450cdc6SMatthias Ringwald                     break;
33481450cdc6SMatthias Ringwald             }
33491450cdc6SMatthias Ringwald         }
33501450cdc6SMatthias Ringwald     }
33511450cdc6SMatthias Ringwald }
33521450cdc6SMatthias Ringwald 
33531450cdc6SMatthias Ringwald static void gatt_client_classic_sdp_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){
33541450cdc6SMatthias Ringwald     gatt_client_t * gatt_client = gatt_client_classic_active_sdp_query;
33551450cdc6SMatthias Ringwald     btstack_assert(gatt_client != NULL);
33561450cdc6SMatthias Ringwald     uint8_t status;
33571450cdc6SMatthias Ringwald 
33581450cdc6SMatthias Ringwald     // TODO: handle sdp events, get l2cap psm
33591450cdc6SMatthias Ringwald     switch (hci_event_packet_get_type(packet)){
33601450cdc6SMatthias Ringwald         case SDP_EVENT_QUERY_ATTRIBUTE_VALUE:
33611450cdc6SMatthias Ringwald             gatt_client_handle_sdp_client_query_attribute_value(gatt_client, packet);
33621450cdc6SMatthias Ringwald             // TODO:
33631450cdc6SMatthias Ringwald             return;
33641450cdc6SMatthias Ringwald         case SDP_EVENT_QUERY_COMPLETE:
33651450cdc6SMatthias Ringwald             status = sdp_event_query_complete_get_status(packet);
33661450cdc6SMatthias Ringwald             gatt_client_classic_active_sdp_query = NULL;
33671450cdc6SMatthias Ringwald             log_info("l2cap psm: %0x, status %02x", gatt_client->l2cap_psm, status);
33681450cdc6SMatthias Ringwald             if (status != ERROR_CODE_SUCCESS) break;
33691450cdc6SMatthias Ringwald             if (gatt_client->l2cap_psm == 0) {
33701450cdc6SMatthias Ringwald                 status = SDP_SERVICE_NOT_FOUND;
33711450cdc6SMatthias Ringwald                 break;
33721450cdc6SMatthias Ringwald             }
33731450cdc6SMatthias Ringwald             break;
33741450cdc6SMatthias Ringwald         default:
33751450cdc6SMatthias Ringwald             btstack_assert(false);
33761450cdc6SMatthias Ringwald             return;
33771450cdc6SMatthias Ringwald     }
33781450cdc6SMatthias Ringwald 
33791450cdc6SMatthias Ringwald     // done
33801450cdc6SMatthias Ringwald     if (status == ERROR_CODE_SUCCESS){
3381052dc82aSMatthias Ringwald         gatt_client->state = P_W4_L2CAP_CONNECTION;
338259b5e157SMatthias Ringwald         status = att_dispatch_classic_connect(gatt_client->addr, gatt_client->l2cap_psm, &gatt_client->l2cap_cid);
33831450cdc6SMatthias Ringwald     }
33841450cdc6SMatthias Ringwald     if (status != ERROR_CODE_SUCCESS) {
33851450cdc6SMatthias Ringwald         gatt_client_classic_handle_connected(gatt_client, status);
33861450cdc6SMatthias Ringwald     }
33871450cdc6SMatthias Ringwald }
33881450cdc6SMatthias Ringwald 
33891450cdc6SMatthias Ringwald static void gatt_client_classic_sdp_start(void * context){
33901450cdc6SMatthias Ringwald     gatt_client_classic_active_sdp_query = (gatt_client_t *) context;
3391052dc82aSMatthias Ringwald     gatt_client_classic_active_sdp_query->state = P_W4_SDP_QUERY;
33921450cdc6SMatthias Ringwald     sdp_client_query_uuid16(gatt_client_classic_sdp_handler, gatt_client_classic_active_sdp_query->addr, ORG_BLUETOOTH_SERVICE_GENERIC_ATTRIBUTE);
33931450cdc6SMatthias Ringwald }
33941450cdc6SMatthias Ringwald 
339574daebdeSMatthias Ringwald static void gatt_client_classic_emit_connected(void * context){
339674daebdeSMatthias Ringwald     gatt_client_t * gatt_client = (gatt_client_t *) context;
3397052dc82aSMatthias Ringwald     gatt_client->state = P_READY;
3398b2d70d58SMatthias Ringwald     gatt_client_emit_connected(gatt_client->callback, ERROR_CODE_SUCCESS, gatt_client->addr_type, gatt_client->addr, gatt_client->con_handle);
339974daebdeSMatthias Ringwald }
340074daebdeSMatthias Ringwald 
34011450cdc6SMatthias Ringwald uint8_t gatt_client_classic_connect(btstack_packet_handler_t callback, bd_addr_t addr){
34021450cdc6SMatthias Ringwald     gatt_client_t * gatt_client = gatt_client_get_context_for_classic_addr(addr);
34031450cdc6SMatthias Ringwald     if (gatt_client != NULL){
34041450cdc6SMatthias Ringwald         return ERROR_CODE_ACL_CONNECTION_ALREADY_EXISTS;
34051450cdc6SMatthias Ringwald     }
34061450cdc6SMatthias Ringwald     gatt_client = btstack_memory_gatt_client_get();
34071450cdc6SMatthias Ringwald     if (gatt_client == NULL){
34081450cdc6SMatthias Ringwald         return ERROR_CODE_MEMORY_CAPACITY_EXCEEDED;
34091450cdc6SMatthias Ringwald     }
34101450cdc6SMatthias Ringwald     // init state
3411e9cdf30bSMatthias Ringwald     gatt_client->bearer_type = ATT_BEARER_UNENHANCED_CLASSIC;
34121450cdc6SMatthias Ringwald     gatt_client->con_handle = HCI_CON_HANDLE_INVALID;
34131450cdc6SMatthias Ringwald     memcpy(gatt_client->addr, addr, 6);
3414b2d70d58SMatthias Ringwald     gatt_client->addr_type = BD_ADDR_TYPE_ACL;
34151450cdc6SMatthias Ringwald     gatt_client->mtu = ATT_DEFAULT_MTU;
34161450cdc6SMatthias Ringwald     gatt_client->security_level = LEVEL_0;
34171450cdc6SMatthias Ringwald     gatt_client->mtu_state = MTU_AUTO_EXCHANGE_DISABLED;
34181450cdc6SMatthias Ringwald     gatt_client->callback = callback;
3419cbd76cecSMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT
3420cbd76cecSMatthias Ringwald     gatt_client->eatt_state = GATT_CLIENT_EATT_IDLE;
3421cbd76cecSMatthias Ringwald #endif
34221450cdc6SMatthias Ringwald     btstack_linked_list_add(&gatt_client_connections, (btstack_linked_item_t*)gatt_client);
342374daebdeSMatthias Ringwald 
342474daebdeSMatthias Ringwald     // schedule emitted event if already connected, otherwise
342574daebdeSMatthias Ringwald     bool already_connected = false;
342674daebdeSMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL);
342774daebdeSMatthias Ringwald     if (hci_connection != NULL){
342874daebdeSMatthias Ringwald         if (hci_connection->att_server.l2cap_cid != 0){
342974daebdeSMatthias Ringwald             already_connected = true;
343074daebdeSMatthias Ringwald         }
343174daebdeSMatthias Ringwald     }
3432052dc82aSMatthias Ringwald     gatt_client->callback_request.context = gatt_client;
343374daebdeSMatthias Ringwald     if (already_connected){
343474daebdeSMatthias Ringwald         gatt_client->con_handle = hci_connection->con_handle;
3435052dc82aSMatthias Ringwald         gatt_client->callback_request.callback = &gatt_client_classic_emit_connected;
3436052dc82aSMatthias Ringwald         gatt_client->state = P_W2_EMIT_CONNECTED;
3437052dc82aSMatthias Ringwald         btstack_run_loop_execute_on_main_thread(&gatt_client->callback_request);
343874daebdeSMatthias Ringwald     } else {
3439052dc82aSMatthias Ringwald         gatt_client->callback_request.callback = &gatt_client_classic_sdp_start;
3440052dc82aSMatthias Ringwald         gatt_client->state = P_W2_SDP_QUERY;
3441052dc82aSMatthias Ringwald         sdp_client_register_query_callback(&gatt_client->callback_request);
344274daebdeSMatthias Ringwald     }
3443b7b03a30SMatthias Ringwald     return ERROR_CODE_SUCCESS;
34441450cdc6SMatthias Ringwald }
34451450cdc6SMatthias Ringwald 
34461450cdc6SMatthias Ringwald uint8_t gatt_client_classic_disconnect(btstack_packet_handler_t callback, hci_con_handle_t con_handle){
34471450cdc6SMatthias Ringwald     gatt_client_t * gatt_client = gatt_client_get_context_for_handle(con_handle);
34481450cdc6SMatthias Ringwald     if (gatt_client == NULL){
34491450cdc6SMatthias Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
34501450cdc6SMatthias Ringwald     }
34511450cdc6SMatthias Ringwald     gatt_client->callback = callback;
34521450cdc6SMatthias Ringwald     return l2cap_disconnect(gatt_client->l2cap_cid);
34531450cdc6SMatthias Ringwald }
34541450cdc6SMatthias Ringwald #endif
34551450cdc6SMatthias Ringwald 
345626166ecfSMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT
345726166ecfSMatthias Ringwald 
345826166ecfSMatthias Ringwald #define MAX_NR_EATT_CHANNELS 5
34597627a0deSMatthias Ringwald 
34607627a0deSMatthias Ringwald static void gatt_client_le_enhanced_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
34617627a0deSMatthias Ringwald 
3462df0a5c68SMatthias Ringwald static uint8_t gatt_client_le_enhanced_num_eatt_clients_in_state(gatt_client_t * gatt_client, gatt_client_state_t state){
3463df0a5c68SMatthias Ringwald     uint8_t num_clients = 0;
3464df0a5c68SMatthias Ringwald     btstack_linked_list_iterator_t it;
3465df0a5c68SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &gatt_client->eatt_clients);
3466df0a5c68SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
3467df0a5c68SMatthias Ringwald         gatt_client_t * eatt_client = (gatt_client_t *) btstack_linked_list_iterator_next(&it);
3468df0a5c68SMatthias Ringwald         if (eatt_client->state == state){
3469df0a5c68SMatthias Ringwald             num_clients++;
3470df0a5c68SMatthias Ringwald         }
3471df0a5c68SMatthias Ringwald     }
3472df0a5c68SMatthias Ringwald     return num_clients;
3473df0a5c68SMatthias Ringwald }
3474df0a5c68SMatthias Ringwald 
34757627a0deSMatthias Ringwald static void gatt_client_eatt_finalize(gatt_client_t * gatt_client) {
34767627a0deSMatthias Ringwald     // free eatt clients
34777627a0deSMatthias Ringwald     btstack_linked_list_iterator_t it;
34787627a0deSMatthias Ringwald     btstack_linked_list_iterator_init(&it, &gatt_client_connections);
34797627a0deSMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)) {
34807627a0deSMatthias Ringwald         gatt_client_t *eatt_client = (gatt_client_t *) btstack_linked_list_iterator_next(&it);
34817627a0deSMatthias Ringwald         btstack_linked_list_iterator_remove(&it);
34827627a0deSMatthias Ringwald         btstack_memory_gatt_client_free(eatt_client);
34837627a0deSMatthias Ringwald     }
34847627a0deSMatthias Ringwald }
34857627a0deSMatthias Ringwald 
34867627a0deSMatthias Ringwald // all channels connected
34877627a0deSMatthias Ringwald static void gatt_client_le_enhanced_handle_connected(gatt_client_t * gatt_client, uint8_t status) {
34887627a0deSMatthias Ringwald     if (status == ERROR_CODE_SUCCESS){
3489df0a5c68SMatthias Ringwald         uint8_t num_ready = gatt_client_le_enhanced_num_eatt_clients_in_state(gatt_client, P_READY);
3490df0a5c68SMatthias Ringwald         if (num_ready > 0){
34917627a0deSMatthias Ringwald             gatt_client->eatt_state = GATT_CLIENT_EATT_READY;
3492df0a5c68SMatthias Ringwald             // free unused channels
3493df0a5c68SMatthias Ringwald             btstack_linked_list_iterator_t it;
3494df0a5c68SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &gatt_client_connections);
3495df0a5c68SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)) {
3496df0a5c68SMatthias Ringwald                 gatt_client_t *eatt_client = (gatt_client_t *) btstack_linked_list_iterator_next(&it);
3497df0a5c68SMatthias Ringwald                 if (eatt_client->state == P_L2CAP_CLOSED){
3498df0a5c68SMatthias Ringwald                     btstack_linked_list_iterator_remove(&it);
3499df0a5c68SMatthias Ringwald                     btstack_memory_gatt_client_free(eatt_client);
3500df0a5c68SMatthias Ringwald                 }
3501df0a5c68SMatthias Ringwald             }
3502df0a5c68SMatthias Ringwald         } else {
35036d0f6f49SMatthias Ringwald             hci_connection_t * hci_connection = hci_connection_for_handle(gatt_client->con_handle);
35046d0f6f49SMatthias Ringwald             btstack_assert(hci_connection != NULL);
35056d0f6f49SMatthias Ringwald             if (hci_connection->att_server.incoming_connection_request){
35066d0f6f49SMatthias Ringwald                 hci_connection->att_server.incoming_connection_request = false;
35076d0f6f49SMatthias Ringwald                 log_info("Collision, retry in 100ms");
35086d0f6f49SMatthias Ringwald                 gatt_client->state = P_W2_L2CAP_CONNECT;
35096d0f6f49SMatthias Ringwald                 // set timer for retry
3510b1da4983SMatthias Ringwald                 btstack_run_loop_set_timer(&gatt_client->gc_timeout, GATT_CLIENT_COLLISION_BACKOFF_MS);
35116d0f6f49SMatthias Ringwald                 btstack_run_loop_set_timer_handler(&gatt_client->gc_timeout, gatt_client_le_enhanced_retry);
35126d0f6f49SMatthias Ringwald                 btstack_run_loop_add_timer(&gatt_client->gc_timeout);
35136d0f6f49SMatthias Ringwald                 return;
35146d0f6f49SMatthias Ringwald             } else {
3515df0a5c68SMatthias Ringwald                 gatt_client->eatt_state = GATT_CLIENT_EATT_IDLE;
3516df0a5c68SMatthias Ringwald                 status = ERROR_CODE_CONNECTION_REJECTED_DUE_TO_LIMITED_RESOURCES;
3517df0a5c68SMatthias Ringwald             }
35186d0f6f49SMatthias Ringwald         }
35197627a0deSMatthias Ringwald     } else {
35207627a0deSMatthias Ringwald         gatt_client_eatt_finalize(gatt_client);
35217627a0deSMatthias Ringwald         gatt_client->eatt_state = GATT_CLIENT_EATT_IDLE;
35227627a0deSMatthias Ringwald     }
35237627a0deSMatthias Ringwald 
3524b2d70d58SMatthias Ringwald     gatt_client_emit_connected(gatt_client->callback, status, gatt_client->addr_type, gatt_client->addr, gatt_client->con_handle);
35257627a0deSMatthias Ringwald }
35267627a0deSMatthias Ringwald 
35277627a0deSMatthias Ringwald // single channel disconnected
35287627a0deSMatthias Ringwald static void gatt_client_le_enhanced_handle_ecbm_disconnected(gatt_client_t * gatt_client, gatt_client_t * eatt_client) {
35297627a0deSMatthias Ringwald 
35307627a0deSMatthias Ringwald     // report error
35317627a0deSMatthias Ringwald     gatt_client_report_error_if_pending(eatt_client, ATT_ERROR_HCI_DISCONNECT_RECEIVED);
35327627a0deSMatthias Ringwald 
35337627a0deSMatthias Ringwald     // free memory
35347627a0deSMatthias Ringwald     btstack_linked_list_remove(&gatt_client->eatt_clients, (btstack_linked_item_t *) eatt_client);
35357627a0deSMatthias Ringwald     btstack_memory_gatt_client_free(eatt_client);
35367627a0deSMatthias Ringwald 
3537570bdd2dSMatthias Ringwald     // last channel
35387627a0deSMatthias Ringwald     if (btstack_linked_list_empty(&gatt_client->eatt_clients)){
3539570bdd2dSMatthias Ringwald         hci_connection_t * hci_connection = hci_connection_for_handle(gatt_client->con_handle);
3540570bdd2dSMatthias Ringwald         hci_connection->att_server.eatt_outgoing_active = false;
3541570bdd2dSMatthias Ringwald 
3542570bdd2dSMatthias Ringwald         if (gatt_client->eatt_state == GATT_CLIENT_EATT_READY) {
3543570bdd2dSMatthias Ringwald             // report disconnected if last channel closed
35447627a0deSMatthias Ringwald             uint8_t buffer[20];
35457627a0deSMatthias Ringwald             uint16_t len = hci_event_create_from_template_and_arguments(buffer, sizeof(buffer), &gatt_client_disconnected, gatt_client->con_handle);
35467627a0deSMatthias Ringwald             (*gatt_client->callback)(HCI_EVENT_PACKET, 0, buffer, len);
35477627a0deSMatthias Ringwald         }
35487627a0deSMatthias Ringwald     }
35497627a0deSMatthias Ringwald }
35507627a0deSMatthias Ringwald 
35517627a0deSMatthias Ringwald static gatt_client_t * gatt_client_le_enhanced_get_context_for_l2cap_cid(uint16_t l2cap_cid, gatt_client_t ** out_eatt_client){
35527627a0deSMatthias Ringwald     btstack_linked_list_iterator_t it;
35537627a0deSMatthias Ringwald     btstack_linked_list_iterator_init(&it, &gatt_client_connections);
35547627a0deSMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)) {
35557627a0deSMatthias Ringwald         gatt_client_t * gatt_client = (gatt_client_t *) btstack_linked_list_iterator_next(&it);
35567627a0deSMatthias Ringwald         btstack_linked_list_iterator_t it2;
35577627a0deSMatthias Ringwald         btstack_linked_list_iterator_init(&it2, &gatt_client->eatt_clients);
35587627a0deSMatthias Ringwald         while (btstack_linked_list_iterator_has_next(&it2)) {
35597627a0deSMatthias Ringwald             gatt_client_t * eatt_client = (gatt_client_t *) btstack_linked_list_iterator_next(&it2);
35607627a0deSMatthias Ringwald             if (eatt_client->l2cap_cid == l2cap_cid){
35617627a0deSMatthias Ringwald                 *out_eatt_client = eatt_client;
35627627a0deSMatthias Ringwald                 return gatt_client;
35637627a0deSMatthias Ringwald             }
35647627a0deSMatthias Ringwald         }
35657627a0deSMatthias Ringwald     }
35667627a0deSMatthias Ringwald     return NULL;
35677627a0deSMatthias Ringwald }
35687627a0deSMatthias Ringwald 
35697627a0deSMatthias Ringwald static void gatt_client_le_enhanced_setup_l2cap_channel(gatt_client_t * gatt_client){
35707627a0deSMatthias Ringwald     uint8_t num_channels = gatt_client->eatt_num_clients;
35717627a0deSMatthias Ringwald 
35727627a0deSMatthias Ringwald     // setup channels
35737627a0deSMatthias Ringwald     uint16_t buffer_size_per_client = gatt_client->eatt_storage_size / num_channels;
3574f7a42e72SMatthias Ringwald     uint16_t max_mtu = (buffer_size_per_client - REPORT_PREBUFFER_HEADER) / 2;
35757627a0deSMatthias Ringwald     uint8_t * receive_buffers[MAX_NR_EATT_CHANNELS];
35767627a0deSMatthias Ringwald     uint16_t  new_cids[MAX_NR_EATT_CHANNELS];
35777627a0deSMatthias Ringwald     memset(gatt_client->eatt_storage_buffer, 0, gatt_client->eatt_storage_size);
35787627a0deSMatthias Ringwald     uint8_t i;
35797627a0deSMatthias Ringwald     for (i=0;i<gatt_client->eatt_num_clients; i++){
3580f7a42e72SMatthias Ringwald         receive_buffers[i] = &gatt_client->eatt_storage_buffer[REPORT_PREBUFFER_HEADER];
3581f7a42e72SMatthias Ringwald         gatt_client->eatt_storage_buffer += REPORT_PREBUFFER_HEADER + max_mtu;
35827627a0deSMatthias Ringwald     }
35837627a0deSMatthias Ringwald 
35847627a0deSMatthias Ringwald     log_info("%u EATT clients with receive buffer size %u", gatt_client->eatt_num_clients, buffer_size_per_client);
35857627a0deSMatthias Ringwald 
3586f7a42e72SMatthias Ringwald     uint8_t status = l2cap_ecbm_create_channels(&gatt_client_le_enhanced_packet_handler,
3587f7a42e72SMatthias Ringwald                                                 gatt_client->con_handle,
35887627a0deSMatthias Ringwald                                                 gatt_client->security_level,
3589f7a42e72SMatthias Ringwald                                                 BLUETOOTH_PSM_EATT, num_channels,
3590f7a42e72SMatthias Ringwald                                                 L2CAP_LE_AUTOMATIC_CREDITS,
35917627a0deSMatthias Ringwald                                                 buffer_size_per_client,
3592f7a42e72SMatthias Ringwald                                                 receive_buffers,
3593f7a42e72SMatthias Ringwald                                                 new_cids);
35947627a0deSMatthias Ringwald 
35957627a0deSMatthias Ringwald     if (status == ERROR_CODE_SUCCESS){
35967627a0deSMatthias Ringwald         i = 0;
35977627a0deSMatthias Ringwald         btstack_linked_list_iterator_t it;
35987627a0deSMatthias Ringwald         btstack_linked_list_iterator_init(&it, &gatt_client->eatt_clients);
35997627a0deSMatthias Ringwald         while (btstack_linked_list_iterator_has_next(&it)) {
36007627a0deSMatthias Ringwald             gatt_client_t *new_eatt_client = (gatt_client_t *) btstack_linked_list_iterator_next(&it);
36017627a0deSMatthias Ringwald 
36027627a0deSMatthias Ringwald             // init state with new cid and transmit buffer
36037627a0deSMatthias Ringwald             new_eatt_client->bearer_type = ATT_BEARER_ENHANCED_LE;
36047627a0deSMatthias Ringwald             new_eatt_client->con_handle = gatt_client->con_handle;
36057627a0deSMatthias Ringwald             new_eatt_client->mtu = 64;
36067627a0deSMatthias Ringwald             new_eatt_client->security_level = LEVEL_0;
36077627a0deSMatthias Ringwald             new_eatt_client->mtu_state = MTU_AUTO_EXCHANGE_DISABLED;
3608052dc82aSMatthias Ringwald             new_eatt_client->state = P_W4_L2CAP_CONNECTION;
36097627a0deSMatthias Ringwald             new_eatt_client->l2cap_cid = new_cids[i];
36107627a0deSMatthias Ringwald             new_eatt_client->eatt_storage_buffer = gatt_client->eatt_storage_buffer;
3611f7a42e72SMatthias Ringwald             gatt_client->eatt_storage_buffer += max_mtu;
36127627a0deSMatthias Ringwald             i++;
36137627a0deSMatthias Ringwald         }
36147627a0deSMatthias Ringwald         gatt_client->eatt_state = GATT_CLIENT_EATT_L2CAP_SETUP;
36157627a0deSMatthias Ringwald     } else {
36167627a0deSMatthias Ringwald         gatt_client_le_enhanced_handle_connected(gatt_client, status);
36177627a0deSMatthias Ringwald     }
361826166ecfSMatthias Ringwald }
361926166ecfSMatthias Ringwald 
36206d0f6f49SMatthias Ringwald static void gatt_client_le_enhanced_retry(btstack_timer_source_t * ts){
36216d0f6f49SMatthias Ringwald     gatt_client_t * gatt_client = gatt_client_for_timer(ts);
36226d0f6f49SMatthias Ringwald     if (gatt_client != NULL){
36236d0f6f49SMatthias Ringwald         gatt_client->state = P_W4_L2CAP_CONNECTION;
36246d0f6f49SMatthias Ringwald         gatt_client_le_enhanced_setup_l2cap_channel(gatt_client);
36256d0f6f49SMatthias Ringwald     }
36266d0f6f49SMatthias Ringwald }
36276d0f6f49SMatthias Ringwald 
362826166ecfSMatthias Ringwald static void gatt_client_le_enhanced_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) {
36297627a0deSMatthias Ringwald     gatt_client_t *gatt_client;
36307627a0deSMatthias Ringwald     gatt_client_t *eatt_client;
363126166ecfSMatthias Ringwald     hci_con_handle_t con_handle;
36327627a0deSMatthias Ringwald     uint16_t l2cap_cid;
363326166ecfSMatthias Ringwald     uint8_t status;
363426166ecfSMatthias Ringwald     gatt_client_characteristic_t characteristic;
36357627a0deSMatthias Ringwald     gatt_client_service_t service;
363626166ecfSMatthias Ringwald     switch (packet_type) {
363726166ecfSMatthias Ringwald         case HCI_EVENT_PACKET:
363826166ecfSMatthias Ringwald             switch (hci_event_packet_get_type(packet)) {
36397627a0deSMatthias Ringwald                 case GATT_EVENT_SERVICE_QUERY_RESULT:
36407627a0deSMatthias Ringwald                     con_handle = gatt_event_service_query_result_get_handle(packet);
36417627a0deSMatthias Ringwald                     gatt_client = gatt_client_get_context_for_handle(con_handle);
36427627a0deSMatthias Ringwald                     btstack_assert(gatt_client != NULL);
36437627a0deSMatthias Ringwald                     btstack_assert(gatt_client->eatt_state == GATT_CLIENT_EATT_DISCOVER_GATT_SERVICE_W4_DONE);
36447627a0deSMatthias Ringwald                     gatt_event_service_query_result_get_service(packet, &service);
36457627a0deSMatthias Ringwald                     gatt_client->gatt_service_start_group_handle = service.start_group_handle;
36467627a0deSMatthias Ringwald                     gatt_client->gatt_service_end_group_handle = service.end_group_handle;
36477627a0deSMatthias Ringwald                     break;
364826166ecfSMatthias Ringwald                 case GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT:
364926166ecfSMatthias Ringwald                     con_handle = gatt_event_characteristic_value_query_result_get_handle(packet);
365026166ecfSMatthias Ringwald                     gatt_client = gatt_client_get_context_for_handle(con_handle);
365126166ecfSMatthias Ringwald                     btstack_assert(gatt_client != NULL);
36527627a0deSMatthias Ringwald                     btstack_assert(gatt_client->eatt_state == GATT_CLIENT_EATT_READ_SERVER_SUPPORTED_FEATURES_W4_DONE);
365326166ecfSMatthias Ringwald                     if (gatt_event_characteristic_value_query_result_get_value_length(packet) >= 1) {
36547627a0deSMatthias Ringwald                         gatt_client->gatt_server_supported_features = gatt_event_characteristic_value_query_result_get_value(packet)[0];
365526166ecfSMatthias Ringwald                     }
365626166ecfSMatthias Ringwald                     break;
365726166ecfSMatthias Ringwald                 case GATT_EVENT_CHARACTERISTIC_QUERY_RESULT:
365826166ecfSMatthias Ringwald                     con_handle = gatt_event_characteristic_query_result_get_handle(packet);
365926166ecfSMatthias Ringwald                     gatt_client = gatt_client_get_context_for_handle(con_handle);
366026166ecfSMatthias Ringwald                     btstack_assert(gatt_client != NULL);
36617627a0deSMatthias Ringwald                     btstack_assert(gatt_client->eatt_state == GATT_CLIENT_EATT_FIND_CLIENT_SUPPORTED_FEATURES_W4_DONE);
36627627a0deSMatthias Ringwald                     gatt_event_characteristic_query_result_get_characteristic(packet, &characteristic);
366326166ecfSMatthias Ringwald                     gatt_client->gatt_client_supported_features_handle = characteristic.value_handle;
366426166ecfSMatthias Ringwald                     break;
366526166ecfSMatthias Ringwald                 case GATT_EVENT_QUERY_COMPLETE:
366626166ecfSMatthias Ringwald                     con_handle = gatt_event_query_complete_get_handle(packet);
366726166ecfSMatthias Ringwald                     gatt_client = gatt_client_get_context_for_handle(con_handle);
366826166ecfSMatthias Ringwald                     btstack_assert(gatt_client != NULL);
36697627a0deSMatthias Ringwald                     switch (gatt_client->eatt_state){
36707627a0deSMatthias Ringwald                         case GATT_CLIENT_EATT_DISCOVER_GATT_SERVICE_W4_DONE:
36717627a0deSMatthias Ringwald                             if (gatt_client->gatt_service_start_group_handle == 0){
36727627a0deSMatthias Ringwald                                 gatt_client_le_enhanced_handle_connected(gatt_client, ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE);
36737627a0deSMatthias Ringwald                             } else {
36747627a0deSMatthias Ringwald                                 gatt_client->eatt_state = GATT_CLIENT_EATT_READ_SERVER_SUPPORTED_FEATURES_W2_SEND;
36757627a0deSMatthias Ringwald                             }
36767627a0deSMatthias Ringwald                             break;
36777627a0deSMatthias Ringwald                         case GATT_CLIENT_EATT_READ_SERVER_SUPPORTED_FEATURES_W4_DONE:
36787627a0deSMatthias Ringwald                             if ((gatt_client->gatt_server_supported_features & 1) == 0) {
36797627a0deSMatthias Ringwald                                 gatt_client_le_enhanced_handle_connected(gatt_client, ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE);
36807627a0deSMatthias Ringwald                             } else {
36817627a0deSMatthias Ringwald                                 gatt_client->eatt_state = GATT_CLIENT_EATT_FIND_CLIENT_SUPPORTED_FEATURES_W2_SEND;
36827627a0deSMatthias Ringwald                             }
36837627a0deSMatthias Ringwald                             break;
36847627a0deSMatthias Ringwald                         case GATT_CLIENT_EATT_FIND_CLIENT_SUPPORTED_FEATURES_W4_DONE:
36857627a0deSMatthias Ringwald                             if (gatt_client->gatt_client_supported_features_handle == 0){
36867627a0deSMatthias Ringwald                                 gatt_client_le_enhanced_handle_connected(gatt_client, ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE);
36877627a0deSMatthias Ringwald                             } else {
36887627a0deSMatthias Ringwald                                 gatt_client->eatt_state = GATT_CLIENT_EATT_WRITE_ClIENT_SUPPORTED_FEATURES_W2_SEND;
36897627a0deSMatthias Ringwald                             }
36907627a0deSMatthias Ringwald                             break;
36917627a0deSMatthias Ringwald                         case GATT_CLIENT_EATT_WRITE_ClIENT_SUPPORTED_FEATURES_W4_DONE:
36927627a0deSMatthias Ringwald                             gatt_client_le_enhanced_setup_l2cap_channel(gatt_client);
369326166ecfSMatthias Ringwald                             break;
369426166ecfSMatthias Ringwald                         default:
369526166ecfSMatthias Ringwald                             break;
369626166ecfSMatthias Ringwald                     }
369726166ecfSMatthias Ringwald                     break;
36987627a0deSMatthias Ringwald                 case L2CAP_EVENT_ECBM_CHANNEL_OPENED:
36997627a0deSMatthias Ringwald                     l2cap_cid = l2cap_event_ecbm_channel_opened_get_local_cid(packet);
37007627a0deSMatthias Ringwald                     gatt_client = gatt_client_le_enhanced_get_context_for_l2cap_cid(l2cap_cid, &eatt_client);
37017627a0deSMatthias Ringwald 
37027627a0deSMatthias Ringwald                     btstack_assert(gatt_client != NULL);
37037627a0deSMatthias Ringwald                     btstack_assert(eatt_client != NULL);
3704052dc82aSMatthias Ringwald                     btstack_assert(eatt_client->state == P_W4_L2CAP_CONNECTION);
37057627a0deSMatthias Ringwald 
37067627a0deSMatthias Ringwald                     status = l2cap_event_channel_opened_get_status(packet);
37077627a0deSMatthias Ringwald                     if (status == ERROR_CODE_SUCCESS){
3708052dc82aSMatthias Ringwald                         eatt_client->state = P_READY;
37097627a0deSMatthias Ringwald                         eatt_client->mtu = l2cap_event_channel_opened_get_remote_mtu(packet);
37107627a0deSMatthias Ringwald                     } else {
3711df0a5c68SMatthias Ringwald                         eatt_client->state = P_L2CAP_CLOSED;
37127627a0deSMatthias Ringwald                     }
3713df0a5c68SMatthias Ringwald                     // connected if opened event for all channels received
3714df0a5c68SMatthias Ringwald                     if (gatt_client_le_enhanced_num_eatt_clients_in_state(gatt_client, P_W4_L2CAP_CONNECTION) == 0){
37157627a0deSMatthias Ringwald                         gatt_client_le_enhanced_handle_connected(gatt_client, ERROR_CODE_SUCCESS);
37167627a0deSMatthias Ringwald                     }
37177627a0deSMatthias Ringwald                     break;
37187627a0deSMatthias Ringwald                 case L2CAP_EVENT_CHANNEL_CLOSED:
37197627a0deSMatthias Ringwald                     l2cap_cid = l2cap_event_channel_closed_get_local_cid(packet);
37207627a0deSMatthias Ringwald                     gatt_client = gatt_client_le_enhanced_get_context_for_l2cap_cid(l2cap_cid, &eatt_client);
37217627a0deSMatthias Ringwald                     btstack_assert(gatt_client != NULL);
37227627a0deSMatthias Ringwald                     btstack_assert(eatt_client != NULL);
37237627a0deSMatthias Ringwald                     gatt_client_le_enhanced_handle_ecbm_disconnected(gatt_client, eatt_client);
37247627a0deSMatthias Ringwald                     break;
37257627a0deSMatthias Ringwald                 default:
37267627a0deSMatthias Ringwald                     break;
37277627a0deSMatthias Ringwald             }
37287627a0deSMatthias Ringwald             break;
37297627a0deSMatthias Ringwald         case L2CAP_DATA_PACKET:
37307627a0deSMatthias Ringwald             gatt_client = gatt_client_le_enhanced_get_context_for_l2cap_cid(channel, &eatt_client);
37317627a0deSMatthias Ringwald             btstack_assert(gatt_client != NULL);
3732ce82ac7fSMatthias Ringwald             btstack_assert(eatt_client != NULL);
3733ce82ac7fSMatthias Ringwald             gatt_client_handle_att_response(eatt_client, packet, size);
37347627a0deSMatthias Ringwald             gatt_client_run();
37357627a0deSMatthias Ringwald             break;
373626166ecfSMatthias Ringwald         default:
373726166ecfSMatthias Ringwald             break;
373826166ecfSMatthias Ringwald     }
373926166ecfSMatthias Ringwald }
374026166ecfSMatthias Ringwald 
37417627a0deSMatthias Ringwald static bool gatt_client_le_enhanced_handle_can_send_query(gatt_client_t * gatt_client){
374226166ecfSMatthias Ringwald     uint8_t status = ERROR_CODE_SUCCESS;
374326166ecfSMatthias Ringwald     uint8_t gatt_client_supported_features = 0x06; // eatt + multiple value notifications
374426166ecfSMatthias Ringwald     switch (gatt_client->eatt_state){
37457627a0deSMatthias Ringwald         case GATT_CLIENT_EATT_DISCOVER_GATT_SERVICE_W2_SEND:
37467627a0deSMatthias Ringwald             gatt_client->gatt_service_start_group_handle = 0;
37477627a0deSMatthias Ringwald             gatt_client->eatt_state = GATT_CLIENT_EATT_DISCOVER_GATT_SERVICE_W4_DONE;
37487627a0deSMatthias Ringwald             status = gatt_client_discover_primary_services_by_uuid16(&gatt_client_le_enhanced_packet_handler,
37497627a0deSMatthias Ringwald                                                                      gatt_client->con_handle,
37507627a0deSMatthias Ringwald                                                                      ORG_BLUETOOTH_SERVICE_GENERIC_ATTRIBUTE);
37517627a0deSMatthias Ringwald             break;
375226166ecfSMatthias Ringwald         case GATT_CLIENT_EATT_READ_SERVER_SUPPORTED_FEATURES_W2_SEND:
37537627a0deSMatthias Ringwald             gatt_client->gatt_server_supported_features = 0;
37547627a0deSMatthias Ringwald             gatt_client->eatt_state = GATT_CLIENT_EATT_READ_SERVER_SUPPORTED_FEATURES_W4_DONE;
375526166ecfSMatthias Ringwald             status = gatt_client_read_value_of_characteristics_by_uuid16(&gatt_client_le_enhanced_packet_handler,
37567627a0deSMatthias Ringwald                                                                          gatt_client->con_handle,
37577627a0deSMatthias Ringwald                                                                          gatt_client->gatt_service_start_group_handle,
37587627a0deSMatthias Ringwald                                                                          gatt_client->gatt_service_end_group_handle,
375926166ecfSMatthias Ringwald                                                                          ORG_BLUETOOTH_CHARACTERISTIC_SERVER_SUPPORTED_FEATURES);
376026166ecfSMatthias Ringwald             return true;
376126166ecfSMatthias Ringwald         case GATT_CLIENT_EATT_FIND_CLIENT_SUPPORTED_FEATURES_W2_SEND:
37627627a0deSMatthias Ringwald             gatt_client->gatt_client_supported_features_handle = 0;
37637627a0deSMatthias Ringwald             gatt_client->eatt_state = GATT_CLIENT_EATT_FIND_CLIENT_SUPPORTED_FEATURES_W4_DONE;
376426166ecfSMatthias Ringwald             status = gatt_client_discover_characteristics_for_handle_range_by_uuid16(&gatt_client_le_enhanced_packet_handler,
37657627a0deSMatthias Ringwald                                                                                      gatt_client->con_handle,
37667627a0deSMatthias Ringwald                                                                                      gatt_client->gatt_service_start_group_handle,
37677627a0deSMatthias Ringwald                                                                                      gatt_client->gatt_service_end_group_handle,
37687627a0deSMatthias Ringwald                                                                                      ORG_BLUETOOTH_CHARACTERISTIC_CLIENT_SUPPORTED_FEATURES);
376926166ecfSMatthias Ringwald             return true;
377026166ecfSMatthias Ringwald         case GATT_CLIENT_EATT_WRITE_ClIENT_SUPPORTED_FEATURES_W2_SEND:
377126166ecfSMatthias Ringwald             gatt_client->eatt_state = GATT_CLIENT_EATT_WRITE_ClIENT_SUPPORTED_FEATURES_W4_DONE;
377226166ecfSMatthias Ringwald             status = gatt_client_write_value_of_characteristic(&gatt_client_le_enhanced_packet_handler, gatt_client->con_handle,
377326166ecfSMatthias Ringwald                                                                gatt_client->gatt_client_supported_features_handle, 1,
377426166ecfSMatthias Ringwald                                                                &gatt_client_supported_features);
377526166ecfSMatthias Ringwald             return true;
377626166ecfSMatthias Ringwald         default:
377726166ecfSMatthias Ringwald             break;
377826166ecfSMatthias Ringwald     }
37797627a0deSMatthias Ringwald     btstack_assert(status == ERROR_CODE_SUCCESS);
37807627a0deSMatthias Ringwald     UNUSED(status);
378126166ecfSMatthias Ringwald     return false;
378226166ecfSMatthias Ringwald }
378326166ecfSMatthias Ringwald 
378426166ecfSMatthias Ringwald uint8_t gatt_client_le_enhanced_connect(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint8_t num_channels, uint8_t * storage_buffer, uint16_t storage_size) {
378526166ecfSMatthias Ringwald     gatt_client_t * gatt_client;
378626166ecfSMatthias Ringwald     uint8_t status = gatt_client_provide_context_for_handle(con_handle, &gatt_client);
378726166ecfSMatthias Ringwald     if (status != ERROR_CODE_SUCCESS){
378826166ecfSMatthias Ringwald         return status;
378926166ecfSMatthias Ringwald     }
379026166ecfSMatthias Ringwald 
379126166ecfSMatthias Ringwald     if (gatt_client->eatt_state != GATT_CLIENT_EATT_IDLE){
379226166ecfSMatthias Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
379326166ecfSMatthias Ringwald     }
379426166ecfSMatthias Ringwald 
3795f7a42e72SMatthias Ringwald     // need one buffer for sending and one for receiving. Receiving includes pre-buffer for reports
37967627a0deSMatthias Ringwald     uint16_t buffer_size_per_client = storage_size / num_channels;
3797f7a42e72SMatthias Ringwald     uint16_t max_mtu = (buffer_size_per_client - REPORT_PREBUFFER_HEADER) / 2;
3798f7a42e72SMatthias Ringwald     if (max_mtu < 64) {
37997627a0deSMatthias Ringwald         return ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS;
38007627a0deSMatthias Ringwald     }
38017627a0deSMatthias Ringwald 
38027627a0deSMatthias Ringwald     if ((num_channels == 0) || (num_channels > MAX_NR_EATT_CHANNELS)){
38037627a0deSMatthias Ringwald         return ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS;
38047627a0deSMatthias Ringwald     }
38057627a0deSMatthias Ringwald 
38067627a0deSMatthias Ringwald     // create max num_channel eatt clients
38077627a0deSMatthias Ringwald     uint8_t i;
38087627a0deSMatthias Ringwald     btstack_linked_list_t eatt_clients = NULL;
38097627a0deSMatthias Ringwald     for (i=0;i<num_channels;i++) {
38107627a0deSMatthias Ringwald         gatt_client_t * new_gatt_client = btstack_memory_gatt_client_get();
38117627a0deSMatthias Ringwald         if (new_gatt_client == NULL) {
38127627a0deSMatthias Ringwald             break;
38137627a0deSMatthias Ringwald         }
38147627a0deSMatthias Ringwald         btstack_linked_list_add(&eatt_clients, (btstack_linked_item_t*)new_gatt_client);
38157627a0deSMatthias Ringwald     }
38167627a0deSMatthias Ringwald 
38177627a0deSMatthias Ringwald     if (i != num_channels){
38187627a0deSMatthias Ringwald         while (true){
38197627a0deSMatthias Ringwald             gatt_client = (gatt_client_t *) btstack_linked_list_pop(&eatt_clients);
38207627a0deSMatthias Ringwald             if (gatt_client == NULL) {
38217627a0deSMatthias Ringwald                 break;
38227627a0deSMatthias Ringwald             }
38237627a0deSMatthias Ringwald             btstack_memory_gatt_client_free(gatt_client);
38247627a0deSMatthias Ringwald         }
38257627a0deSMatthias Ringwald         return ERROR_CODE_MEMORY_CAPACITY_EXCEEDED;
38267627a0deSMatthias Ringwald     }
38277627a0deSMatthias Ringwald 
3828570bdd2dSMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
3829570bdd2dSMatthias Ringwald     hci_connection->att_server.eatt_outgoing_active = true;
3830570bdd2dSMatthias Ringwald 
38317627a0deSMatthias Ringwald     gatt_client->callback = callback;
38327627a0deSMatthias Ringwald     gatt_client->eatt_num_clients   = num_channels;
38337627a0deSMatthias Ringwald     gatt_client->eatt_storage_buffer = storage_buffer;
38347627a0deSMatthias Ringwald     gatt_client->eatt_storage_size   = storage_size;
38357627a0deSMatthias Ringwald     gatt_client->eatt_clients = eatt_clients;
38367627a0deSMatthias Ringwald     gatt_client->eatt_state = GATT_CLIENT_EATT_DISCOVER_GATT_SERVICE_W2_SEND;
383726166ecfSMatthias Ringwald     gatt_client_notify_can_send_query(gatt_client);
383826166ecfSMatthias Ringwald 
383926166ecfSMatthias Ringwald     return ERROR_CODE_SUCCESS;
384026166ecfSMatthias Ringwald }
384126166ecfSMatthias Ringwald 
384226166ecfSMatthias Ringwald #endif
384326166ecfSMatthias Ringwald 
3844a6121b51SMilanka Ringwald #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
3845a6121b51SMilanka Ringwald void gatt_client_att_packet_handler_fuzz(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){
3846a6121b51SMilanka Ringwald     gatt_client_att_packet_handler(packet_type, handle, packet, size);
3847a6121b51SMilanka Ringwald }
3848ae1ee62dSMilanka Ringwald 
384940faeb84SMilanka Ringwald uint8_t gatt_client_get_client(hci_con_handle_t con_handle, gatt_client_t ** out_gatt_client){
385040faeb84SMilanka Ringwald     uint8_t status = gatt_client_provide_context_for_handle(con_handle, out_gatt_client);
385140faeb84SMilanka Ringwald     return status;
3852ae1ee62dSMilanka Ringwald }
3853a6121b51SMilanka Ringwald #endif
3854