xref: /btstack/src/ble/att_dispatch.c (revision d21b039a25b4ac3a2d40065d78f85ac3b7e8d564)
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__ "att_dispatch.c"
39ab2c6ae4SMatthias Ringwald 
403deb3ec6SMatthias Ringwald 
413deb3ec6SMatthias Ringwald /**
423deb3ec6SMatthias Ringwald  * Dispatcher for independent implementation of ATT client and server
433deb3ec6SMatthias Ringwald  */
443deb3ec6SMatthias Ringwald 
45296289e3SMatthias Ringwald #include "ble/att_dispatch.h"
4659c6af15SMatthias Ringwald #include "ble/core.h"
4716ece135SMatthias Ringwald #include "btstack_debug.h"
4859c6af15SMatthias Ringwald #include "l2cap.h"
4998926299SMatthias Ringwald #include "btstack_event.h"
503deb3ec6SMatthias Ringwald 
5138b893aaSMilanka Ringwald #define ATT_SERVER 0u
5238b893aaSMilanka Ringwald #define ATT_CLIENT 1u
5338b893aaSMilanka Ringwald #define ATT_MAX    2u
54773c4106SMatthias Ringwald 
5553e41ec1SMatthias Ringwald struct {
5653e41ec1SMatthias Ringwald     btstack_packet_handler_t packet_handler;
57429d695fSMilanka Ringwald     bool                  waiting_for_can_send;
587a705a92SMatthias Ringwald } subscriptions[ATT_MAX];
597a705a92SMatthias Ringwald 
607a705a92SMatthias Ringwald // index of subscription that will get can send now first if waiting for it
617a705a92SMatthias Ringwald static uint8_t att_round_robin;
623deb3ec6SMatthias Ringwald 
63047a7fd3SMatthias Ringwald // track can send now requests
64429d695fSMilanka Ringwald static bool can_send_now_pending;
65047a7fd3SMatthias Ringwald 
6672df296cSMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
att_dispatch_att_server_for_l2cap_cid(uint16_t l2cap_cid)672aed7bd4SMatthias Ringwald static att_server_t * att_dispatch_att_server_for_l2cap_cid(uint16_t l2cap_cid){
6872df296cSMatthias Ringwald     btstack_linked_list_iterator_t it;
6972df296cSMatthias Ringwald     hci_connections_get_iterator(&it);
7072df296cSMatthias Ringwald     while(btstack_linked_list_iterator_has_next(&it)) {
7172df296cSMatthias Ringwald         hci_connection_t *hci_connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
7272df296cSMatthias Ringwald         att_server_t *att_server = &hci_connection->att_server;
7372df296cSMatthias Ringwald         if (att_server->l2cap_cid == l2cap_cid){
742aed7bd4SMatthias Ringwald             return att_server;
752aed7bd4SMatthias Ringwald         }
762aed7bd4SMatthias Ringwald     }
772aed7bd4SMatthias Ringwald     return NULL;
782aed7bd4SMatthias Ringwald }
792aed7bd4SMatthias Ringwald #endif
802aed7bd4SMatthias Ringwald 
att_dispatch_handle_can_send_now(uint8_t * packet,uint16_t size)812aed7bd4SMatthias Ringwald static void att_dispatch_handle_can_send_now(uint8_t *packet, uint16_t size){
822aed7bd4SMatthias Ringwald     uint8_t i;
832aed7bd4SMatthias Ringwald     uint8_t index;
842aed7bd4SMatthias Ringwald     uint16_t l2cap_cid = l2cap_event_can_send_now_get_local_cid(packet);
852aed7bd4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
862aed7bd4SMatthias Ringwald     if (l2cap_cid != L2CAP_CID_ATTRIBUTE_PROTOCOL){
872aed7bd4SMatthias Ringwald         att_server_t * att_server = att_dispatch_att_server_for_l2cap_cid(l2cap_cid);
882aed7bd4SMatthias Ringwald         if (att_server != NULL){
8972df296cSMatthias Ringwald             for (i = 0u; i < ATT_MAX; i++) {
9072df296cSMatthias Ringwald                 index = (att_round_robin + i) & 1u;
9172df296cSMatthias Ringwald                 if (att_server->send_requests[index]) {
9272df296cSMatthias Ringwald                     att_server->send_requests[index] = false;
9372df296cSMatthias Ringwald                     // registered packet handlers from Unenhanced LE
9472df296cSMatthias Ringwald                     subscriptions[index].packet_handler(HCI_EVENT_PACKET, l2cap_cid, packet, size);
9572df296cSMatthias Ringwald                     // fairness: prioritize next service
9672df296cSMatthias Ringwald                     att_round_robin = (index + 1u) % ATT_MAX;
9772df296cSMatthias Ringwald                     // stop if client cannot send anymore
9872df296cSMatthias Ringwald                     if (!l2cap_can_send_packet_now(l2cap_cid)) break;
9972df296cSMatthias Ringwald                 }
10072df296cSMatthias Ringwald             }
10172df296cSMatthias Ringwald             // check if more can send now events are needed
10272df296cSMatthias Ringwald             bool send_request_pending = att_server->send_requests[ATT_CLIENT]
10372df296cSMatthias Ringwald                                         || att_server->send_requests[ATT_SERVER];
10472df296cSMatthias Ringwald             if (send_request_pending){
10572df296cSMatthias Ringwald                 l2cap_request_can_send_now_event(att_server->l2cap_cid);
10672df296cSMatthias Ringwald             }
10772df296cSMatthias Ringwald             return;
10872df296cSMatthias Ringwald         }
10972df296cSMatthias Ringwald     }
11072df296cSMatthias Ringwald #endif
11172df296cSMatthias Ringwald     can_send_now_pending = false;
11238b893aaSMilanka Ringwald     for (i = 0u; i < ATT_MAX; i++){
1134ea43905SMatthias Ringwald         index = (att_round_robin + i) & 1u;
114429d695fSMilanka Ringwald         if ( (subscriptions[index].packet_handler != NULL) && subscriptions[index].waiting_for_can_send){
115429d695fSMilanka Ringwald             subscriptions[index].waiting_for_can_send = false;
1169eba80a6SMatthias Ringwald             subscriptions[index].packet_handler(HCI_EVENT_PACKET, l2cap_cid, packet, size);
1171dab9fd6SMatthias Ringwald             // fairness: prioritize next service
1184ea43905SMatthias Ringwald             att_round_robin = (index + 1u) % ATT_MAX;
119773c4106SMatthias Ringwald             // stop if client cannot send anymore
120b947e684SMatthias Ringwald             if (!hci_can_send_acl_le_packet_now()) break;
121773c4106SMatthias Ringwald         }
122773c4106SMatthias Ringwald     }
123047a7fd3SMatthias Ringwald     // check if more can send now events are needed
124047a7fd3SMatthias Ringwald     if (!can_send_now_pending){
12538b893aaSMilanka Ringwald         for (i = 0u; i < ATT_MAX; i++){
126429d695fSMilanka Ringwald             if ((subscriptions[i].packet_handler != NULL) && subscriptions[i].waiting_for_can_send){
127429d695fSMilanka Ringwald                 can_send_now_pending = true;
128047a7fd3SMatthias Ringwald                 // note: con_handle is not used, so we can pass in anything
129047a7fd3SMatthias Ringwald                 l2cap_request_can_send_fix_channel_now_event(0, L2CAP_CID_ATTRIBUTE_PROTOCOL);
130047a7fd3SMatthias Ringwald                 break;
131047a7fd3SMatthias Ringwald             }
132047a7fd3SMatthias Ringwald         }
133047a7fd3SMatthias Ringwald     }
1349eba80a6SMatthias Ringwald }
1359eba80a6SMatthias Ringwald 
att_dispatch_handle_att_pdu(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)136e6dbc98eSMatthias Ringwald static void att_dispatch_handle_att_pdu(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
137e6dbc98eSMatthias Ringwald     uint8_t index;
1389eba80a6SMatthias Ringwald     uint8_t opcode;
1399eba80a6SMatthias Ringwald     uint8_t method;
1409eba80a6SMatthias Ringwald     bool for_server;
1419eba80a6SMatthias Ringwald     bool invalid;
142e6dbc98eSMatthias Ringwald 
1439eba80a6SMatthias Ringwald     // parse opcode
1449eba80a6SMatthias Ringwald     opcode  = packet[0u];
1459eba80a6SMatthias Ringwald     method  = opcode & 0x03fu;
1469eba80a6SMatthias Ringwald     invalid = method > ATT_MULTIPLE_HANDLE_VALUE_NTF;
1479eba80a6SMatthias Ringwald     // odd PDUs are sent from server to client - even PDUs are sent from client to server, also let server handle invalid ones
1489eba80a6SMatthias Ringwald     for_server = ((method & 1u) == 0u) || invalid;
1499eba80a6SMatthias Ringwald     index = for_server ? ATT_SERVER : ATT_CLIENT;
1509eba80a6SMatthias Ringwald     if (!subscriptions[index].packet_handler) return;
1519eba80a6SMatthias Ringwald     subscriptions[index].packet_handler(packet_type, channel, packet, size);
152e6dbc98eSMatthias Ringwald }
153e6dbc98eSMatthias Ringwald 
att_packet_handler(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)154e6dbc98eSMatthias Ringwald static void att_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
155476bb9c4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
156476bb9c4SMatthias Ringwald     hci_connection_t * hci_connection;
1576737be29SMatthias Ringwald     att_server_t * att_server;
158476bb9c4SMatthias Ringwald     hci_con_handle_t con_handle;
159476bb9c4SMatthias Ringwald     bool outgoing_active;
1609ed6f53cSMatthias Ringwald     uint8_t index;
161e6dbc98eSMatthias Ringwald     bd_addr_t address;
162e6dbc98eSMatthias Ringwald     uint16_t l2cap_cid;
1639ed6f53cSMatthias Ringwald     uint8_t  status;
164e6dbc98eSMatthias Ringwald #endif
165e6dbc98eSMatthias Ringwald     switch (packet_type){
166e6dbc98eSMatthias Ringwald         case ATT_DATA_PACKET:
167e6dbc98eSMatthias Ringwald             att_dispatch_handle_att_pdu(packet_type, channel, packet, size);
1689eba80a6SMatthias Ringwald             break;
169fd3d2f4cSMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
170fd3d2f4cSMatthias Ringwald         case L2CAP_DATA_PACKET:
171fd3d2f4cSMatthias Ringwald             att_dispatch_handle_att_pdu(packet_type, channel, packet, size);
172fd3d2f4cSMatthias Ringwald             break;
173fd3d2f4cSMatthias Ringwald #endif
1749eba80a6SMatthias Ringwald         case HCI_EVENT_PACKET:
1759eba80a6SMatthias Ringwald             switch (hci_event_packet_get_type(packet)) {
1769eba80a6SMatthias Ringwald                 case L2CAP_EVENT_CAN_SEND_NOW:
1779eba80a6SMatthias Ringwald                     att_dispatch_handle_can_send_now(packet, size);
178773c4106SMatthias Ringwald                     break;
179d385c90eSMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
180d385c90eSMatthias Ringwald                 case L2CAP_EVENT_INCOMING_CONNECTION:
181d385c90eSMatthias Ringwald                     l2cap_event_incoming_connection_get_address(packet, address);
182d385c90eSMatthias Ringwald                     l2cap_cid = l2cap_event_incoming_connection_get_local_cid(packet);
183476bb9c4SMatthias Ringwald                     // reject if outgoing l2cap connection active, L2CAP/TIM/BV-01-C
184476bb9c4SMatthias Ringwald                     con_handle = l2cap_event_incoming_connection_get_handle(packet);
185476bb9c4SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
1869ed6f53cSMatthias Ringwald                     btstack_assert(hci_connection != NULL);
187476bb9c4SMatthias Ringwald                     outgoing_active = hci_connection->att_server.l2cap_cid != 0;
188476bb9c4SMatthias Ringwald                     if (outgoing_active) {
1894e39f390SMatthias Ringwald                         hci_connection->att_server.incoming_connection_request = true;
190476bb9c4SMatthias Ringwald                         l2cap_decline_connection(l2cap_cid);
191476bb9c4SMatthias Ringwald                         log_info("Decline incoming connection from %s", bd_addr_to_str(address));
192476bb9c4SMatthias Ringwald                     } else {
193d385c90eSMatthias Ringwald                         l2cap_accept_connection(l2cap_cid);
194d385c90eSMatthias Ringwald                         log_info("Accept incoming connection from %s", bd_addr_to_str(address));
195476bb9c4SMatthias Ringwald                     }
196d385c90eSMatthias Ringwald                     break;
197d385c90eSMatthias Ringwald                 case L2CAP_EVENT_CHANNEL_OPENED:
1989ed6f53cSMatthias Ringwald                     // store l2cap_cid in att_server
1999ed6f53cSMatthias Ringwald                     status = l2cap_event_channel_opened_get_status(packet);
2009ed6f53cSMatthias Ringwald                     con_handle = l2cap_event_channel_opened_get_handle(packet);
2019ed6f53cSMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
2029ed6f53cSMatthias Ringwald                     if (status == ERROR_CODE_SUCCESS){
2039ed6f53cSMatthias Ringwald                         btstack_assert(hci_connection != NULL);
2049ed6f53cSMatthias Ringwald                         hci_connection->att_server.l2cap_cid = l2cap_event_channel_opened_get_local_cid(packet);
2059ed6f53cSMatthias Ringwald                     } else {
2069ed6f53cSMatthias Ringwald                         if (hci_connection != NULL){
2079ed6f53cSMatthias Ringwald                             hci_connection->att_server.l2cap_cid = 0;
2089ed6f53cSMatthias Ringwald                         }
2099ed6f53cSMatthias Ringwald                     }
210d385c90eSMatthias Ringwald                     // dispatch to all roles
211d385c90eSMatthias Ringwald                     for (index = 0; index < ATT_MAX; index++){
212d385c90eSMatthias Ringwald                         if (subscriptions[index].packet_handler != NULL){
213d385c90eSMatthias Ringwald                             subscriptions[index].packet_handler(packet_type, channel, packet, size);
214d385c90eSMatthias Ringwald                         }
215d385c90eSMatthias Ringwald                     }
216d385c90eSMatthias Ringwald                     break;
217434166e3SMatthias Ringwald                 case L2CAP_EVENT_CHANNEL_CLOSED:
2186737be29SMatthias Ringwald                     // clear l2cap_cid in att_server
219*d4c00e3dSMatthias Ringwald                     l2cap_cid = l2cap_event_channel_closed_get_local_cid(packet);
2206737be29SMatthias Ringwald                     att_server = att_dispatch_att_server_for_l2cap_cid(l2cap_cid);
2216737be29SMatthias Ringwald                     att_server->l2cap_cid = 0;
222434166e3SMatthias Ringwald                     // dispatch to all roles
223434166e3SMatthias Ringwald                     for (index = 0; index < ATT_MAX; index++){
224434166e3SMatthias Ringwald                         if (subscriptions[index].packet_handler != NULL){
225434166e3SMatthias Ringwald                             subscriptions[index].packet_handler(packet_type, channel, packet, size);
226434166e3SMatthias Ringwald                         }
227434166e3SMatthias Ringwald                     }
228434166e3SMatthias Ringwald                     break;
229d385c90eSMatthias Ringwald #endif
230773c4106SMatthias Ringwald                 default:
231773c4106SMatthias Ringwald                     break;
232773c4106SMatthias Ringwald             }
23398926299SMatthias Ringwald             break;
23498926299SMatthias Ringwald         default:
23598926299SMatthias Ringwald             break;
23698926299SMatthias Ringwald     }
2373deb3ec6SMatthias Ringwald }
2383deb3ec6SMatthias Ringwald 
att_dispatch_register_client(btstack_packet_handler_t packet_handler)2393deb3ec6SMatthias Ringwald void att_dispatch_register_client(btstack_packet_handler_t packet_handler){
24053e41ec1SMatthias Ringwald     subscriptions[ATT_CLIENT].packet_handler = packet_handler;
2413deb3ec6SMatthias Ringwald     l2cap_register_fixed_channel(att_packet_handler, L2CAP_CID_ATTRIBUTE_PROTOCOL);
2423deb3ec6SMatthias Ringwald }
2433deb3ec6SMatthias Ringwald 
att_dispatch_register_server(btstack_packet_handler_t packet_handler)2443deb3ec6SMatthias Ringwald void att_dispatch_register_server(btstack_packet_handler_t packet_handler){
24553e41ec1SMatthias Ringwald     subscriptions[ATT_SERVER].packet_handler = packet_handler;
2463deb3ec6SMatthias Ringwald     l2cap_register_fixed_channel(att_packet_handler, L2CAP_CID_ATTRIBUTE_PROTOCOL);
2473deb3ec6SMatthias Ringwald }
24819448c0bSMatthias Ringwald 
2490f376572SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
att_dispatch_classic_get_l2cap_cid(hci_con_handle_t con_handle)2500cd55b6cSMatthias Ringwald static uint16_t att_dispatch_classic_get_l2cap_cid(hci_con_handle_t con_handle){
2510f376572SMatthias Ringwald     // get l2cap_cid from att_server in hci_connection
2520f376572SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
2530f376572SMatthias Ringwald     if (hci_connection != NULL) {
2540cd55b6cSMatthias Ringwald         return hci_connection->att_server.l2cap_cid;
2550cd55b6cSMatthias Ringwald     } else {
2560cd55b6cSMatthias Ringwald         return 0;
2570cd55b6cSMatthias Ringwald     }
2580cd55b6cSMatthias Ringwald }
2590cd55b6cSMatthias Ringwald #endif
2600cd55b6cSMatthias Ringwald 
2610cd55b6cSMatthias Ringwald 
att_dispatch_can_send_now(hci_con_handle_t con_handle)2620cd55b6cSMatthias Ringwald static bool att_dispatch_can_send_now(hci_con_handle_t con_handle) {
2630cd55b6cSMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
2640cd55b6cSMatthias Ringwald     uint16_t l2cap_cid = att_dispatch_classic_get_l2cap_cid(con_handle);
2650f376572SMatthias Ringwald     if (l2cap_cid != 0){
2660f376572SMatthias Ringwald         return l2cap_can_send_packet_now(l2cap_cid);
2670f376572SMatthias Ringwald     }
2680f376572SMatthias Ringwald #endif
2690b9d7e78SMatthias Ringwald     return l2cap_can_send_fixed_channel_packet_now(con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL);
27019448c0bSMatthias Ringwald }
27119448c0bSMatthias Ringwald 
att_dispatch_client_can_send_now(hci_con_handle_t con_handle)272e9e0c21eSMatthias Ringwald bool att_dispatch_client_can_send_now(hci_con_handle_t con_handle){
273e9e0c21eSMatthias Ringwald     return att_dispatch_can_send_now(con_handle);
274e9e0c21eSMatthias Ringwald }
275e9e0c21eSMatthias Ringwald 
att_dispatch_server_can_send_now(hci_con_handle_t con_handle)2764a7a258fSMatthias Ringwald bool att_dispatch_server_can_send_now(hci_con_handle_t con_handle){
277e9e0c21eSMatthias Ringwald     return att_dispatch_can_send_now(con_handle);
278773c4106SMatthias Ringwald }
2790b9d7e78SMatthias Ringwald 
att_dispatch_request_can_send_now_event(hci_con_handle_t con_handle,uint8_t type)280eb65899cSMatthias Ringwald static void att_dispatch_request_can_send_now_event(hci_con_handle_t con_handle, uint8_t type) {
2815fd2be40SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
2825fd2be40SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
2835fd2be40SMatthias Ringwald     if (hci_connection != NULL) {
2845fd2be40SMatthias Ringwald         att_server_t * att_server = &hci_connection->att_server;
2855fd2be40SMatthias Ringwald         if (att_server->l2cap_cid != 0){
2865fd2be40SMatthias Ringwald             bool send_request_pending = att_server->send_requests[ATT_CLIENT]
2875fd2be40SMatthias Ringwald                                      || att_server->send_requests[ATT_SERVER];
2885fd2be40SMatthias Ringwald             att_server->send_requests[type] = true;
2895fd2be40SMatthias Ringwald             if (send_request_pending == false){
2905fd2be40SMatthias Ringwald                 l2cap_request_can_send_now_event(att_server->l2cap_cid);
2915fd2be40SMatthias Ringwald             }
2925fd2be40SMatthias Ringwald             return;
2935fd2be40SMatthias Ringwald         }
2945fd2be40SMatthias Ringwald     }
2955fd2be40SMatthias Ringwald #endif
296eb65899cSMatthias Ringwald     subscriptions[type].waiting_for_can_send = true;
2975fd2be40SMatthias Ringwald     if (can_send_now_pending == false) {
298429d695fSMilanka Ringwald         can_send_now_pending = true;
2990b9d7e78SMatthias Ringwald         l2cap_request_can_send_fix_channel_now_event(con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL);
3000b9d7e78SMatthias Ringwald     }
301047a7fd3SMatthias Ringwald }
3020b9d7e78SMatthias Ringwald 
att_dispatch_client_request_can_send_now_event(hci_con_handle_t con_handle)303eb65899cSMatthias Ringwald void att_dispatch_client_request_can_send_now_event(hci_con_handle_t con_handle){
304eb65899cSMatthias Ringwald     att_dispatch_request_can_send_now_event(con_handle, ATT_CLIENT);
30519448c0bSMatthias Ringwald }
306eb65899cSMatthias Ringwald 
att_dispatch_server_request_can_send_now_event(hci_con_handle_t con_handle)307eb65899cSMatthias Ringwald void att_dispatch_server_request_can_send_now_event(hci_con_handle_t con_handle){
308eb65899cSMatthias Ringwald     att_dispatch_request_can_send_now_event(con_handle, ATT_SERVER);
309047a7fd3SMatthias Ringwald }
310b12646c5SJakob Krantz 
emit_mtu_exchange_complete(btstack_packet_handler_t packet_handler,hci_con_handle_t con_handle,uint16_t new_mtu)31125684bfcSMatthias Ringwald static void emit_mtu_exchange_complete(btstack_packet_handler_t packet_handler, hci_con_handle_t con_handle, uint16_t new_mtu){
31225684bfcSMatthias Ringwald     if (!packet_handler) return;
313b12646c5SJakob Krantz     uint8_t packet[6];
314b12646c5SJakob Krantz     packet[0] = ATT_EVENT_MTU_EXCHANGE_COMPLETE;
3154ea43905SMatthias Ringwald     packet[1] = sizeof(packet) - 2u;
316b12646c5SJakob Krantz     little_endian_store_16(packet, 2, con_handle);
317b12646c5SJakob Krantz     little_endian_store_16(packet, 4, new_mtu);
318f38f43d7SMatthias Ringwald     packet_handler(HCI_EVENT_PACKET, con_handle, packet, 6);
31925684bfcSMatthias Ringwald }
32025684bfcSMatthias Ringwald 
att_dispatch_server_mtu_exchanged(hci_con_handle_t con_handle,uint16_t new_mtu)32125684bfcSMatthias Ringwald void att_dispatch_server_mtu_exchanged(hci_con_handle_t con_handle, uint16_t new_mtu){
32253e41ec1SMatthias Ringwald     emit_mtu_exchange_complete(subscriptions[ATT_CLIENT].packet_handler, con_handle, new_mtu);
323b12646c5SJakob Krantz }
324b12646c5SJakob Krantz 
att_dispatch_client_mtu_exchanged(hci_con_handle_t con_handle,uint16_t new_mtu)325b12646c5SJakob Krantz void att_dispatch_client_mtu_exchanged(hci_con_handle_t con_handle, uint16_t new_mtu){
32653e41ec1SMatthias Ringwald     emit_mtu_exchange_complete(subscriptions[ATT_SERVER].packet_handler, con_handle, new_mtu);
327b12646c5SJakob Krantz }
328d385c90eSMatthias Ringwald 
329d385c90eSMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
att_dispatch_classic_register_service(void)330d385c90eSMatthias Ringwald void att_dispatch_classic_register_service(void){
331d385c90eSMatthias Ringwald     l2cap_register_service(&att_packet_handler, PSM_ATT, 0xffff, gap_get_security_level());
332d385c90eSMatthias Ringwald }
att_dispatch_classic_connect(bd_addr_t address,uint16_t l2cap_psm,uint16_t * out_cid)3331f4d0bc6SMatthias Ringwald uint8_t att_dispatch_classic_connect(bd_addr_t address, uint16_t l2cap_psm, uint16_t *out_cid) {
334476bb9c4SMatthias Ringwald     uint16_t l2cap_cid;
335476bb9c4SMatthias Ringwald     uint8_t status = l2cap_create_channel(&att_packet_handler, address, l2cap_psm, 0xffff, &l2cap_cid);
336476bb9c4SMatthias Ringwald     // store l2cap_cid in hci_connection
337476bb9c4SMatthias Ringwald     if (status == ERROR_CODE_SUCCESS){
338476bb9c4SMatthias Ringwald         hci_connection_t * hci_connection = hci_connection_for_bd_addr_and_type(address, BD_ADDR_TYPE_ACL);
339476bb9c4SMatthias Ringwald         if (hci_connection != NULL) {
340476bb9c4SMatthias Ringwald             hci_connection->att_server.l2cap_cid = l2cap_cid;
3414e39f390SMatthias Ringwald             hci_connection->att_server.incoming_connection_request = false;
342476bb9c4SMatthias Ringwald         }
343476bb9c4SMatthias Ringwald     }
344476bb9c4SMatthias Ringwald     *out_cid = l2cap_cid;
345476bb9c4SMatthias Ringwald     return status;
3461f4d0bc6SMatthias Ringwald }
3471f4d0bc6SMatthias Ringwald 
348d385c90eSMatthias Ringwald #endif
349