xref: /btstack/src/l2cap.c (revision cd529728d639d98ee9a02a082934cd206dd68ded)
143625864Smatthias.ringwald /*
2a0c35809S[email protected]  * Copyright (C) 2014 BlueKitchen GmbH
31713bceaSmatthias.ringwald  *
41713bceaSmatthias.ringwald  * Redistribution and use in source and binary forms, with or without
51713bceaSmatthias.ringwald  * modification, are permitted provided that the following conditions
61713bceaSmatthias.ringwald  * are met:
71713bceaSmatthias.ringwald  *
81713bceaSmatthias.ringwald  * 1. Redistributions of source code must retain the above copyright
91713bceaSmatthias.ringwald  *    notice, this list of conditions and the following disclaimer.
101713bceaSmatthias.ringwald  * 2. Redistributions in binary form must reproduce the above copyright
111713bceaSmatthias.ringwald  *    notice, this list of conditions and the following disclaimer in the
121713bceaSmatthias.ringwald  *    documentation and/or other materials provided with the distribution.
131713bceaSmatthias.ringwald  * 3. Neither the name of the copyright holders nor the names of
141713bceaSmatthias.ringwald  *    contributors may be used to endorse or promote products derived
151713bceaSmatthias.ringwald  *    from this software without specific prior written permission.
166b64433eSmatthias.ringwald  * 4. Any redistribution, use, or modification is done solely for
176b64433eSmatthias.ringwald  *    personal benefit and not for any commercial purpose or for
186b64433eSmatthias.ringwald  *    monetary gain.
191713bceaSmatthias.ringwald  *
20a0c35809S[email protected]  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
211713bceaSmatthias.ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
221713bceaSmatthias.ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
231713bceaSmatthias.ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
241713bceaSmatthias.ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
251713bceaSmatthias.ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
261713bceaSmatthias.ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
271713bceaSmatthias.ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
281713bceaSmatthias.ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
291713bceaSmatthias.ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
301713bceaSmatthias.ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311713bceaSmatthias.ringwald  * SUCH DAMAGE.
321713bceaSmatthias.ringwald  *
33a0c35809S[email protected]  * Please inquire about commercial licensing options at
34a0c35809S[email protected]  * [email protected]
356b64433eSmatthias.ringwald  *
361713bceaSmatthias.ringwald  */
371713bceaSmatthias.ringwald 
381713bceaSmatthias.ringwald /*
3943625864Smatthias.ringwald  *  l2cap.c
4043625864Smatthias.ringwald  *
4143625864Smatthias.ringwald  *  Logical Link Control and Adaption Protocl (L2CAP)
4243625864Smatthias.ringwald  *
4343625864Smatthias.ringwald  *  Created by Matthias Ringwald on 5/16/09.
4443625864Smatthias.ringwald  */
4543625864Smatthias.ringwald 
4643625864Smatthias.ringwald #include "l2cap.h"
47645658c9Smatthias.ringwald #include "hci.h"
482b3c6c9bSmatthias.ringwald #include "hci_dump.h"
4916ece135SMatthias Ringwald #include "btstack_debug.h"
500e2df43fSMatthias Ringwald #include "btstack_event.h"
51d3a9df87Smatthias.ringwald #include "btstack_memory.h"
5243625864Smatthias.ringwald 
5343625864Smatthias.ringwald #include <stdarg.h>
5443625864Smatthias.ringwald #include <string.h>
5543625864Smatthias.ringwald 
5643625864Smatthias.ringwald #include <stdio.h>
5743625864Smatthias.ringwald 
584c744e21Smatthias.ringwald // nr of buffered acl packets in outgoing queue to get max performance
594c744e21Smatthias.ringwald #define NR_BUFFERED_ACL_PACKETS 3
604c744e21Smatthias.ringwald 
6139bda6d5Smatthias.ringwald // used to cache l2cap rejects, echo, and informational requests
62e16a9cacSmatthias.ringwald #define NR_PENDING_SIGNALING_RESPONSES 3
6339bda6d5Smatthias.ringwald 
6400d93d79Smatthias.ringwald // offsets for L2CAP SIGNALING COMMANDS
6500d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_CODE_OFFSET   0
6600d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_SIGID_OFFSET  1
6700d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET 2
6800d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_DATA_OFFSET   4
6900d93d79Smatthias.ringwald 
705628cf69SMatthias Ringwald // internal table
715628cf69SMatthias Ringwald #define L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL 0
725628cf69SMatthias Ringwald #define L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL  1
735628cf69SMatthias Ringwald #define L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL 2
745628cf69SMatthias Ringwald #define L2CAP_FIXED_CHANNEL_TABLE_SIZE (L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL+1)
755628cf69SMatthias Ringwald 
7633c40538SMatthias Ringwald // prototypes
7733c40538SMatthias Ringwald static void l2cap_finialize_channel_close(l2cap_channel_t *channel);
7833c40538SMatthias Ringwald static inline l2cap_service_t * l2cap_get_service(uint16_t psm);
7933c40538SMatthias Ringwald static void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status);
8034e7e577SMatthias Ringwald static void l2cap_emit_can_send_now(btstack_packet_handler_t packet_handler, uint16_t channel);
8133c40538SMatthias Ringwald static void l2cap_emit_channel_closed(l2cap_channel_t *channel);
8233c40538SMatthias Ringwald static void l2cap_emit_connection_request(l2cap_channel_t *channel);
8333c40538SMatthias Ringwald static int  l2cap_channel_ready_for_open(l2cap_channel_t *channel);
8433c40538SMatthias Ringwald static void l2cap_hci_event_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
853d50b4baSMatthias Ringwald static void l2cap_acl_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size );
8630725612SMatthias Ringwald static void l2cap_notify_channel_can_send(void);
87e7d0c9aaSMatthias Ringwald #ifdef ENABLE_BLE
88828a7f7aSMatthias Ringwald static void l2cap_le_finialize_channel_close(l2cap_channel_t *channel);
89e7d0c9aaSMatthias Ringwald static inline l2cap_service_t * l2cap_le_get_service(uint16_t psm);
90e7d0c9aaSMatthias Ringwald #endif
9133c40538SMatthias Ringwald 
925628cf69SMatthias Ringwald typedef struct l2cap_fixed_channel {
935628cf69SMatthias Ringwald     btstack_packet_handler_t callback;
942125de09SMatthias Ringwald     uint8_t waiting_for_can_send_now;
955628cf69SMatthias Ringwald } l2cap_fixed_channel_t;
965628cf69SMatthias Ringwald 
975628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_channels;
985628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_services;
995628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_le_channels;
1005628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_le_services;
10139bda6d5Smatthias.ringwald 
10239bda6d5Smatthias.ringwald // used to cache l2cap rejects, echo, and informational requests
1032b83fb7dSmatthias.ringwald static l2cap_signaling_response_t signaling_responses[NR_PENDING_SIGNALING_RESPONSES];
1042b83fb7dSmatthias.ringwald static int signaling_responses_pending;
1052b83fb7dSmatthias.ringwald 
10633c40538SMatthias Ringwald static uint8_t require_security_level2_for_outgoing_sdp;
10733c40538SMatthias Ringwald 
108fb37a842SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
109fb37a842SMatthias Ringwald 
11033c40538SMatthias Ringwald static btstack_packet_handler_t l2cap_event_packet_handler;
1115628cf69SMatthias Ringwald static l2cap_fixed_channel_t fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_SIZE];
11239bda6d5Smatthias.ringwald 
11334e7e577SMatthias Ringwald static uint16_t l2cap_fixed_channel_table_channel_id_for_index(int index){
11434e7e577SMatthias Ringwald     switch (index){
11534e7e577SMatthias Ringwald         case L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL:
11634e7e577SMatthias Ringwald             return L2CAP_CID_ATTRIBUTE_PROTOCOL;
11734e7e577SMatthias Ringwald         case L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL:
11834e7e577SMatthias Ringwald             return L2CAP_CID_SECURITY_MANAGER_PROTOCOL;
11934e7e577SMatthias Ringwald         case L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL:
12034e7e577SMatthias Ringwald             return L2CAP_CID_CONNECTIONLESS_CHANNEL;
12134e7e577SMatthias Ringwald         default:
12234e7e577SMatthias Ringwald             return 0;
12334e7e577SMatthias Ringwald     }
12434e7e577SMatthias Ringwald }
1255628cf69SMatthias Ringwald static int l2cap_fixed_channel_table_index_for_channel_id(uint16_t channel_id){
1265628cf69SMatthias Ringwald     switch (channel_id){
1275628cf69SMatthias Ringwald         case L2CAP_CID_ATTRIBUTE_PROTOCOL:
1285628cf69SMatthias Ringwald             return L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL;
1295628cf69SMatthias Ringwald         case L2CAP_CID_SECURITY_MANAGER_PROTOCOL:
1305628cf69SMatthias Ringwald             return  L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL;
1315628cf69SMatthias Ringwald         case L2CAP_CID_CONNECTIONLESS_CHANNEL:
1325628cf69SMatthias Ringwald             return  L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL;
1335628cf69SMatthias Ringwald         default:
1345628cf69SMatthias Ringwald             return -1;
1355628cf69SMatthias Ringwald         }
1365628cf69SMatthias Ringwald }
13739bda6d5Smatthias.ringwald 
13834e7e577SMatthias Ringwald static int l2cap_fixed_channel_table_index_is_le(int index){
13934e7e577SMatthias Ringwald     if (index == L2CAP_CID_CONNECTIONLESS_CHANNEL) return 0;
14034e7e577SMatthias Ringwald     return 1;
14134e7e577SMatthias Ringwald }
14234e7e577SMatthias Ringwald 
14371de195eSMatthias Ringwald void l2cap_init(void){
1442b83fb7dSmatthias.ringwald     signaling_responses_pending = 0;
145808a48abSmatthias.ringwald 
146f5454fc6Smatthias.ringwald     l2cap_channels = NULL;
147f5454fc6Smatthias.ringwald     l2cap_services = NULL;
1487192e786SMatthias Ringwald     l2cap_le_services = NULL;
1497192e786SMatthias Ringwald     l2cap_le_channels = NULL;
150f5454fc6Smatthias.ringwald 
15133c40538SMatthias Ringwald     l2cap_event_packet_handler = NULL;
1525628cf69SMatthias Ringwald     memset(fixed_channels, 0, sizeof(fixed_channels));
153f5454fc6Smatthias.ringwald 
154ac301f95S[email protected]     require_security_level2_for_outgoing_sdp = 0;
155ac301f95S[email protected] 
156fcadd0caSmatthias.ringwald     //
1572718e2e7Smatthias.ringwald     // register callback with HCI
158fcadd0caSmatthias.ringwald     //
159d9a7306aSMatthias Ringwald     hci_event_callback_registration.callback = &l2cap_hci_event_handler;
160fb37a842SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
161fb37a842SMatthias Ringwald 
162d9a7306aSMatthias Ringwald     hci_register_acl_packet_handler(&l2cap_acl_handler);
163fb37a842SMatthias Ringwald 
16415a95bd5SMatthias Ringwald     gap_connectable_control(0); // no services yet
165fcadd0caSmatthias.ringwald }
166fcadd0caSmatthias.ringwald 
167ffbf8201SMatthias Ringwald void l2cap_register_packet_handler(void (*handler)(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){
16833c40538SMatthias Ringwald     l2cap_event_packet_handler = handler;
1691e6aba47Smatthias.ringwald }
1701e6aba47Smatthias.ringwald 
17117a9b554SMatthias Ringwald static void l2cap_dispatch_to_channel(l2cap_channel_t *channel, uint8_t type, uint8_t * data, uint16_t size){
17258de5610Smatthias.ringwald     (* (channel->packet_handler))(type, channel->local_cid, data, size);
17358de5610Smatthias.ringwald }
17458de5610Smatthias.ringwald 
17558de5610Smatthias.ringwald void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) {
176c9dc710bS[email protected]     log_info("L2CAP_EVENT_CHANNEL_OPENED status 0x%x addr %s handle 0x%x psm 0x%x local_cid 0x%x remote_cid 0x%x local_mtu %u, remote_mtu %u, flush_timeout %u",
177fc64f94aSMatthias Ringwald              status, bd_addr_to_str(channel->address), channel->con_handle, channel->psm,
178c9dc710bS[email protected]              channel->local_cid, channel->remote_cid, channel->local_mtu, channel->remote_mtu, channel->flush_timeout);
179c9dc710bS[email protected]     uint8_t event[23];
18058de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_OPENED;
18158de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
18258de5610Smatthias.ringwald     event[2] = status;
183724d70a2SMatthias Ringwald     reverse_bd_addr(channel->address, &event[3]);
184fc64f94aSMatthias Ringwald     little_endian_store_16(event,  9, channel->con_handle);
185f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 11, channel->psm);
186f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 13, channel->local_cid);
187f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 15, channel->remote_cid);
188f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 17, channel->local_mtu);
189f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 19, channel->remote_mtu);
190f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 21, channel->flush_timeout);
19158de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
19217a9b554SMatthias Ringwald     l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
19358de5610Smatthias.ringwald }
19458de5610Smatthias.ringwald 
19558de5610Smatthias.ringwald void l2cap_emit_channel_closed(l2cap_channel_t *channel) {
196e0abb8e7S[email protected]     log_info("L2CAP_EVENT_CHANNEL_CLOSED local_cid 0x%x", channel->local_cid);
19758de5610Smatthias.ringwald     uint8_t event[4];
19858de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_CLOSED;
19958de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
200f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 2, channel->local_cid);
20158de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
20217a9b554SMatthias Ringwald     l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
20358de5610Smatthias.ringwald }
20458de5610Smatthias.ringwald 
20558de5610Smatthias.ringwald void l2cap_emit_connection_request(l2cap_channel_t *channel) {
206e0abb8e7S[email protected]     log_info("L2CAP_EVENT_INCOMING_CONNECTION addr %s handle 0x%x psm 0x%x local_cid 0x%x remote_cid 0x%x",
207fc64f94aSMatthias Ringwald              bd_addr_to_str(channel->address), channel->con_handle,  channel->psm, channel->local_cid, channel->remote_cid);
20858de5610Smatthias.ringwald     uint8_t event[16];
20958de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_INCOMING_CONNECTION;
21058de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
211724d70a2SMatthias Ringwald     reverse_bd_addr(channel->address, &event[2]);
212fc64f94aSMatthias Ringwald     little_endian_store_16(event,  8, channel->con_handle);
213f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 10, channel->psm);
214f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 12, channel->local_cid);
215f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 14, channel->remote_cid);
21658de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
21717a9b554SMatthias Ringwald     l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
2180af41d30Smatthias.ringwald }
219808a48abSmatthias.ringwald 
22034e7e577SMatthias Ringwald static void l2cap_emit_can_send_now(btstack_packet_handler_t packet_handler, uint16_t channel) {
22134e7e577SMatthias Ringwald     log_info("L2CAP_EVENT_CHANNEL_CAN_SEND_NOW local_cid 0x%x", channel);
22233c40538SMatthias Ringwald     uint8_t event[4];
22333c40538SMatthias Ringwald     event[0] = L2CAP_EVENT_CAN_SEND_NOW;
22433c40538SMatthias Ringwald     event[1] = sizeof(event) - 2;
22534e7e577SMatthias Ringwald     little_endian_store_16(event, 2, channel);
22633c40538SMatthias Ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
22734e7e577SMatthias Ringwald     packet_handler(HCI_EVENT_PACKET, channel, event, sizeof(event));
22833c40538SMatthias Ringwald }
22933c40538SMatthias Ringwald 
230fc64f94aSMatthias Ringwald static void l2cap_emit_connection_parameter_update_response(hci_con_handle_t con_handle, uint16_t result){
231ccf076adS[email protected]     uint8_t event[6];
232ccf076adS[email protected]     event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_RESPONSE;
233ccf076adS[email protected]     event[1] = 4;
234fc64f94aSMatthias Ringwald     little_endian_store_16(event, 2, con_handle);
235f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 4, result);
236ccf076adS[email protected]     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
23733c40538SMatthias Ringwald     if (!l2cap_event_packet_handler) return;
23833c40538SMatthias Ringwald     (*l2cap_event_packet_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event));
239ccf076adS[email protected] }
240ccf076adS[email protected] 
2417f02f414SMatthias Ringwald static l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid){
242665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
243665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
244665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
245665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
246b35f641cSmatthias.ringwald         if ( channel->local_cid == local_cid) {
247f62db1e3Smatthias.ringwald             return channel;
248f62db1e3Smatthias.ringwald         }
249f62db1e3Smatthias.ringwald     }
250f62db1e3Smatthias.ringwald     return NULL;
251f62db1e3Smatthias.ringwald }
252f62db1e3Smatthias.ringwald 
253e7d0c9aaSMatthias Ringwald #ifdef ENABLE_BLE
254e7d0c9aaSMatthias Ringwald static l2cap_channel_t * l2cap_le_get_channel_for_local_cid(uint16_t local_cid){
255e7d0c9aaSMatthias Ringwald     btstack_linked_list_iterator_t it;
256e7d0c9aaSMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_le_channels);
257e7d0c9aaSMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
258e7d0c9aaSMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
259e7d0c9aaSMatthias Ringwald         if ( channel->local_cid == local_cid) {
260e7d0c9aaSMatthias Ringwald             return channel;
261e7d0c9aaSMatthias Ringwald         }
262e7d0c9aaSMatthias Ringwald     }
263e7d0c9aaSMatthias Ringwald     return NULL;
264e7d0c9aaSMatthias Ringwald }
265e7d0c9aaSMatthias Ringwald #endif
266e7d0c9aaSMatthias Ringwald 
2675cb87675SMatthias Ringwald static l2cap_channel_t * l2cap_get_le_channel_for_address_and_addr_type(bd_addr_t address, bd_addr_type_t address_type){
2685cb87675SMatthias Ringwald     btstack_linked_list_iterator_t it;
2695cb87675SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_le_channels);
2705cb87675SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
2715cb87675SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
2725cb87675SMatthias Ringwald         if (channel->address_type != address_type)  continue;
2735cb87675SMatthias Ringwald         if (bd_addr_cmp(channel->address, address)) continue;
2745cb87675SMatthias Ringwald         return channel;
2755cb87675SMatthias Ringwald     }
2765cb87675SMatthias Ringwald     return NULL;
2775cb87675SMatthias Ringwald }
2785cb87675SMatthias Ringwald 
2790b9d7e78SMatthias Ringwald ///
2800b9d7e78SMatthias Ringwald 
28130725612SMatthias Ringwald void l2cap_request_can_send_now_event(uint16_t local_cid){
28230725612SMatthias Ringwald     l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid);
28330725612SMatthias Ringwald     if (!channel) return;
28430725612SMatthias Ringwald     channel->waiting_for_can_send_now = 1;
28530725612SMatthias Ringwald     l2cap_notify_channel_can_send();
28630725612SMatthias Ringwald }
28730725612SMatthias Ringwald 
2880b9d7e78SMatthias Ringwald void l2cap_request_can_send_fix_channel_now_event(hci_con_handle_t con_handle, uint16_t channel_id){
2890b9d7e78SMatthias Ringwald     int index = l2cap_fixed_channel_table_index_for_channel_id(channel_id);
2900b9d7e78SMatthias Ringwald     if (index < 0) return;
2910b9d7e78SMatthias Ringwald     fixed_channels[index].waiting_for_can_send_now = 1;
2920b9d7e78SMatthias Ringwald     l2cap_notify_channel_can_send();
2930b9d7e78SMatthias Ringwald }
2940b9d7e78SMatthias Ringwald 
2950b9d7e78SMatthias Ringwald ///
2960b9d7e78SMatthias Ringwald 
2976b1fde37Smatthias.ringwald int  l2cap_can_send_packet_now(uint16_t local_cid){
2986b1fde37Smatthias.ringwald     l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid);
2996b1fde37Smatthias.ringwald     if (!channel) return 0;
3000b9d7e78SMatthias Ringwald     return hci_can_send_acl_packet_now(channel->con_handle);
3017856fb31S[email protected] }
3027856fb31S[email protected] 
30383e7cdd9SMatthias Ringwald int  l2cap_can_send_prepared_packet_now(uint16_t local_cid){
30483e7cdd9SMatthias Ringwald     l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid);
30583e7cdd9SMatthias Ringwald     if (!channel) return 0;
3060b9d7e78SMatthias Ringwald     return hci_can_send_prepared_acl_packet_now(channel->con_handle);
30783e7cdd9SMatthias Ringwald }
30883e7cdd9SMatthias Ringwald 
309fc64f94aSMatthias Ringwald int  l2cap_can_send_fixed_channel_packet_now(hci_con_handle_t con_handle, uint16_t channel_id){
3100b9d7e78SMatthias Ringwald     return hci_can_send_acl_packet_now(con_handle);
3112125de09SMatthias Ringwald }
3120b9d7e78SMatthias Ringwald 
3130b9d7e78SMatthias Ringwald ///
3143cab4fcaS[email protected] 
31596cbd662Smatthias.ringwald uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid){
31696cbd662Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
31796cbd662Smatthias.ringwald     if (channel) {
31896cbd662Smatthias.ringwald         return channel->remote_mtu;
31996cbd662Smatthias.ringwald     }
32096cbd662Smatthias.ringwald     return 0;
32196cbd662Smatthias.ringwald }
32296cbd662Smatthias.ringwald 
323ec820d77SMatthias Ringwald static l2cap_channel_t * l2cap_channel_for_rtx_timer(btstack_timer_source_t * ts){
324665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
325665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
326665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
327665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
3285932bd7cS[email protected]         if ( &channel->rtx == ts) {
3295932bd7cS[email protected]             return channel;
3305932bd7cS[email protected]         }
3315932bd7cS[email protected]     }
3325932bd7cS[email protected]     return NULL;
3335932bd7cS[email protected] }
3345932bd7cS[email protected] 
335ec820d77SMatthias Ringwald static void l2cap_rtx_timeout(btstack_timer_source_t * ts){
3365932bd7cS[email protected]     l2cap_channel_t * channel = l2cap_channel_for_rtx_timer(ts);
33795d2c8f4SMatthias Ringwald     if (!channel) return;
3385932bd7cS[email protected] 
3395932bd7cS[email protected]     log_info("l2cap_rtx_timeout for local cid 0x%02x", channel->local_cid);
3405932bd7cS[email protected] 
3415932bd7cS[email protected]     // "When terminating the channel, it is not necessary to send a L2CAP_DisconnectReq
3425932bd7cS[email protected]     //  and enter WAIT_DISCONNECT state. Channels can be transitioned directly to the CLOSED state."
3435932bd7cS[email protected]     // notify client
3445932bd7cS[email protected]     l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_RTX_TIMEOUT);
3455932bd7cS[email protected] 
3465932bd7cS[email protected]     // discard channel
3479dcb2fb2S[email protected]     // no need to stop timer here, it is removed from list during timer callback
348665d90f2SMatthias Ringwald     btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
3495932bd7cS[email protected]     btstack_memory_l2cap_channel_free(channel);
3505932bd7cS[email protected] }
3515932bd7cS[email protected] 
3525932bd7cS[email protected] static void l2cap_stop_rtx(l2cap_channel_t * channel){
3535932bd7cS[email protected]     log_info("l2cap_stop_rtx for local cid 0x%02x", channel->local_cid);
354528a4a3bSMatthias Ringwald     btstack_run_loop_remove_timer(&channel->rtx);
3555932bd7cS[email protected] }
3565932bd7cS[email protected] 
3575932bd7cS[email protected] static void l2cap_start_rtx(l2cap_channel_t * channel){
3585932bd7cS[email protected]     l2cap_stop_rtx(channel);
359cb0ff06bS[email protected]     log_info("l2cap_start_rtx for local cid 0x%02x", channel->local_cid);
360528a4a3bSMatthias Ringwald     btstack_run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout);
361528a4a3bSMatthias Ringwald     btstack_run_loop_set_timer(&channel->rtx, L2CAP_RTX_TIMEOUT_MS);
362528a4a3bSMatthias Ringwald     btstack_run_loop_add_timer(&channel->rtx);
3635932bd7cS[email protected] }
3645932bd7cS[email protected] 
3655932bd7cS[email protected] static void l2cap_start_ertx(l2cap_channel_t * channel){
3665932bd7cS[email protected]     log_info("l2cap_start_ertx for local cid 0x%02x", channel->local_cid);
3675932bd7cS[email protected]     l2cap_stop_rtx(channel);
368528a4a3bSMatthias Ringwald     btstack_run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout);
369528a4a3bSMatthias Ringwald     btstack_run_loop_set_timer(&channel->rtx, L2CAP_ERTX_TIMEOUT_MS);
370528a4a3bSMatthias Ringwald     btstack_run_loop_add_timer(&channel->rtx);
3715932bd7cS[email protected] }
3725932bd7cS[email protected] 
37371de195eSMatthias Ringwald void l2cap_require_security_level_2_for_outgoing_sdp(void){
374ac301f95S[email protected]     require_security_level2_for_outgoing_sdp = 1;
375ac301f95S[email protected] }
376ac301f95S[email protected] 
377df3354fcS[email protected] static int l2cap_security_level_0_allowed_for_PSM(uint16_t psm){
378ac301f95S[email protected]     return (psm == PSM_SDP) && (!require_security_level2_for_outgoing_sdp);
379df3354fcS[email protected] }
3805932bd7cS[email protected] 
3817f02f414SMatthias Ringwald static int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){
382b1d43497Smatthias.ringwald 
383a35252c8S[email protected]     if (!hci_can_send_acl_packet_now(handle)){
3849da54300S[email protected]         log_info("l2cap_send_signaling_packet, cannot send");
385b1d43497Smatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
386b1d43497Smatthias.ringwald     }
387b1d43497Smatthias.ringwald 
3889da54300S[email protected]     // log_info("l2cap_send_signaling_packet type %u", cmd);
3892a373862S[email protected]     hci_reserve_packet_buffer();
390facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
39158de5610Smatthias.ringwald     va_list argptr;
39258de5610Smatthias.ringwald     va_start(argptr, identifier);
39370efece1S[email protected]     uint16_t len = l2cap_create_signaling_classic(acl_buffer, handle, cmd, identifier, argptr);
39458de5610Smatthias.ringwald     va_end(argptr);
3959da54300S[email protected]     // log_info("l2cap_send_signaling_packet con %u!", handle);
396826f7347S[email protected]     return hci_send_acl_packet_buffer(len);
39758de5610Smatthias.ringwald }
39858de5610Smatthias.ringwald 
399a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
4007f02f414SMatthias Ringwald static int l2cap_send_le_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){
40170efece1S[email protected] 
402a35252c8S[email protected]     if (!hci_can_send_acl_packet_now(handle)){
4039da54300S[email protected]         log_info("l2cap_send_signaling_packet, cannot send");
40470efece1S[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
40570efece1S[email protected]     }
40670efece1S[email protected] 
4079da54300S[email protected]     // log_info("l2cap_send_signaling_packet type %u", cmd);
4082a373862S[email protected]     hci_reserve_packet_buffer();
409facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
41070efece1S[email protected]     va_list argptr;
41170efece1S[email protected]     va_start(argptr, identifier);
41270efece1S[email protected]     uint16_t len = l2cap_create_signaling_le(acl_buffer, handle, cmd, identifier, argptr);
41370efece1S[email protected]     va_end(argptr);
4149da54300S[email protected]     // log_info("l2cap_send_signaling_packet con %u!", handle);
415826f7347S[email protected]     return hci_send_acl_packet_buffer(len);
41670efece1S[email protected] }
41770efece1S[email protected] #endif
41870efece1S[email protected] 
419b1d43497Smatthias.ringwald uint8_t *l2cap_get_outgoing_buffer(void){
420facf93fdS[email protected]     return hci_get_outgoing_packet_buffer() + COMPLETE_L2CAP_HEADER; // 8 bytes
421b1d43497Smatthias.ringwald }
4226218e6f1Smatthias.ringwald 
4236b4af23dS[email protected] int l2cap_reserve_packet_buffer(void){
4246b4af23dS[email protected]     return hci_reserve_packet_buffer();
4256b4af23dS[email protected] }
4266b4af23dS[email protected] 
42768a0fcf7S[email protected] void l2cap_release_packet_buffer(void){
42868a0fcf7S[email protected]     hci_release_packet_buffer();
42968a0fcf7S[email protected] }
43068a0fcf7S[email protected] 
4317f107edaSMatthias Ringwald static void l2cap_setup_header(uint8_t * acl_buffer, hci_con_handle_t con_handle, uint16_t remote_cid, uint16_t len){
4327f107edaSMatthias Ringwald 
4337f107edaSMatthias Ringwald     int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02;
4347f107edaSMatthias Ringwald 
4357f107edaSMatthias Ringwald     // 0 - Connection handle : PB=pb : BC=00
4367f107edaSMatthias Ringwald     little_endian_store_16(acl_buffer, 0, con_handle | (pb << 12) | (0 << 14));
4377f107edaSMatthias Ringwald     // 2 - ACL length
4387f107edaSMatthias Ringwald     little_endian_store_16(acl_buffer, 2,  len + 4);
4397f107edaSMatthias Ringwald     // 4 - L2CAP packet length
4407f107edaSMatthias Ringwald     little_endian_store_16(acl_buffer, 4,  len + 0);
4417f107edaSMatthias Ringwald     // 6 - L2CAP channel DEST
4427f107edaSMatthias Ringwald     little_endian_store_16(acl_buffer, 6,  remote_cid);
4437f107edaSMatthias Ringwald }
44468a0fcf7S[email protected] 
445b1d43497Smatthias.ringwald int l2cap_send_prepared(uint16_t local_cid, uint16_t len){
446b1d43497Smatthias.ringwald 
447c8b9416aS[email protected]     if (!hci_is_packet_buffer_reserved()){
448c8b9416aS[email protected]         log_error("l2cap_send_prepared called without reserving packet first");
449c8b9416aS[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
450c8b9416aS[email protected]     }
451c8b9416aS[email protected] 
45258de5610Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
453b1d43497Smatthias.ringwald     if (!channel) {
4549da54300S[email protected]         log_error("l2cap_send_prepared no channel for cid 0x%02x", local_cid);
455b1d43497Smatthias.ringwald         return -1;   // TODO: define error
4566218e6f1Smatthias.ringwald     }
4576218e6f1Smatthias.ringwald 
458fc64f94aSMatthias Ringwald     if (!hci_can_send_prepared_acl_packet_now(channel->con_handle)){
4599da54300S[email protected]         log_info("l2cap_send_prepared cid 0x%02x, cannot send", local_cid);
460a35252c8S[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
461a35252c8S[email protected]     }
462a35252c8S[email protected] 
463fc64f94aSMatthias Ringwald     log_debug("l2cap_send_prepared cid 0x%02x, handle %u, 1 credit used", local_cid, channel->con_handle);
464b1d43497Smatthias.ringwald 
465facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
4667f107edaSMatthias Ringwald     l2cap_setup_header(acl_buffer, channel->con_handle, channel->remote_cid, len);
46758de5610Smatthias.ringwald     // send
4687f107edaSMatthias Ringwald     return hci_send_acl_packet_buffer(len+8);
46958de5610Smatthias.ringwald }
47058de5610Smatthias.ringwald 
471fc64f94aSMatthias Ringwald int l2cap_send_prepared_connectionless(hci_con_handle_t con_handle, uint16_t cid, uint16_t len){
4722149f12eSmatthias.ringwald 
473c8b9416aS[email protected]     if (!hci_is_packet_buffer_reserved()){
474c8b9416aS[email protected]         log_error("l2cap_send_prepared_connectionless called without reserving packet first");
4752149f12eSmatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
4762149f12eSmatthias.ringwald     }
4772149f12eSmatthias.ringwald 
478fc64f94aSMatthias Ringwald     if (!hci_can_send_prepared_acl_packet_now(con_handle)){
479fc64f94aSMatthias Ringwald         log_info("l2cap_send_prepared_connectionless handle 0x%02x, cid 0x%02x, cannot send", con_handle, cid);
480c8b9416aS[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
481c8b9416aS[email protected]     }
482c8b9416aS[email protected] 
483fc64f94aSMatthias Ringwald     log_debug("l2cap_send_prepared_connectionless handle %u, cid 0x%02x", con_handle, cid);
4842149f12eSmatthias.ringwald 
485facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
4867f107edaSMatthias Ringwald     l2cap_setup_header(acl_buffer, con_handle, cid, len);
4872149f12eSmatthias.ringwald     // send
4887f107edaSMatthias Ringwald     return hci_send_acl_packet_buffer(len+8);
4892149f12eSmatthias.ringwald }
4902149f12eSmatthias.ringwald 
491ce8f182eSMatthias Ringwald int l2cap_send(uint16_t local_cid, uint8_t *data, uint16_t len){
492b1d43497Smatthias.ringwald 
493a35252c8S[email protected]     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
494a35252c8S[email protected]     if (!channel) {
495ce8f182eSMatthias Ringwald         log_error("l2cap_send no channel for cid 0x%02x", local_cid);
496a35252c8S[email protected]         return -1;   // TODO: define error
497a35252c8S[email protected]     }
498a35252c8S[email protected] 
499f0efaa57S[email protected]     if (len > channel->remote_mtu){
500ce8f182eSMatthias Ringwald         log_error("l2cap_send cid 0x%02x, data length exceeds remote MTU.", local_cid);
501f0efaa57S[email protected]         return L2CAP_DATA_LEN_EXCEEDS_REMOTE_MTU;
502f0efaa57S[email protected]     }
503f0efaa57S[email protected] 
504fc64f94aSMatthias Ringwald     if (!hci_can_send_acl_packet_now(channel->con_handle)){
505ce8f182eSMatthias Ringwald         log_info("l2cap_send cid 0x%02x, cannot send", local_cid);
506b1d43497Smatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
507b1d43497Smatthias.ringwald     }
508b1d43497Smatthias.ringwald 
5092a373862S[email protected]     hci_reserve_packet_buffer();
510facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
511b1d43497Smatthias.ringwald 
512b1d43497Smatthias.ringwald     memcpy(&acl_buffer[8], data, len);
513b1d43497Smatthias.ringwald 
514b1d43497Smatthias.ringwald     return l2cap_send_prepared(local_cid, len);
515b1d43497Smatthias.ringwald }
516b1d43497Smatthias.ringwald 
517fc64f94aSMatthias Ringwald int l2cap_send_connectionless(hci_con_handle_t con_handle, uint16_t cid, uint8_t *data, uint16_t len){
5182149f12eSmatthias.ringwald 
519fc64f94aSMatthias Ringwald     if (!hci_can_send_acl_packet_now(con_handle)){
520ce8f182eSMatthias Ringwald         log_info("l2cap_send cid 0x%02x, cannot send", cid);
5212149f12eSmatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
5222149f12eSmatthias.ringwald     }
5232149f12eSmatthias.ringwald 
5242a373862S[email protected]     hci_reserve_packet_buffer();
525facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
5262149f12eSmatthias.ringwald 
5272149f12eSmatthias.ringwald     memcpy(&acl_buffer[8], data, len);
5282149f12eSmatthias.ringwald 
529fc64f94aSMatthias Ringwald     return l2cap_send_prepared_connectionless(con_handle, cid, len);
5302149f12eSmatthias.ringwald }
5312149f12eSmatthias.ringwald 
532fc64f94aSMatthias Ringwald int l2cap_send_echo_request(hci_con_handle_t con_handle, uint8_t *data, uint16_t len){
533fc64f94aSMatthias Ringwald     return l2cap_send_signaling_packet(con_handle, ECHO_REQUEST, 0x77, len, data);
5340e37e417S[email protected] }
5350e37e417S[email protected] 
53628ca2b46S[email protected] static inline void channelStateVarSetFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){
53728ca2b46S[email protected]     channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var | flag);
53828ca2b46S[email protected] }
53928ca2b46S[email protected] 
54028ca2b46S[email protected] static inline void channelStateVarClearFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){
54128ca2b46S[email protected]     channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var & ~flag);
54228ca2b46S[email protected] }
54328ca2b46S[email protected] 
54428ca2b46S[email protected] 
545b1d43497Smatthias.ringwald 
5468158c421Smatthias.ringwald // MARK: L2CAP_RUN
5472cd0be45Smatthias.ringwald // process outstanding signaling tasks
5487f02f414SMatthias Ringwald static void l2cap_run(void){
5492b83fb7dSmatthias.ringwald 
55022c29ab4SMatthias Ringwald     // log_info("l2cap_run: entered");
55122c29ab4SMatthias Ringwald 
5522b83fb7dSmatthias.ringwald     // check pending signaling responses
5532b83fb7dSmatthias.ringwald     while (signaling_responses_pending){
5542b83fb7dSmatthias.ringwald 
5552b83fb7dSmatthias.ringwald         hci_con_handle_t handle = signaling_responses[0].handle;
556a35252c8S[email protected] 
557a35252c8S[email protected]         if (!hci_can_send_acl_packet_now(handle)) break;
558a35252c8S[email protected] 
5592b83fb7dSmatthias.ringwald         uint8_t  sig_id = signaling_responses[0].sig_id;
5602b360848Smatthias.ringwald         uint16_t infoType = signaling_responses[0].data;    // INFORMATION_REQUEST
56163a7246aSmatthias.ringwald         uint16_t result   = signaling_responses[0].data;    // CONNECTION_REQUEST, COMMAND_REJECT
562f53da564S[email protected]         uint8_t  response_code = signaling_responses[0].code;
5632b83fb7dSmatthias.ringwald 
564f53da564S[email protected]         // remove first item before sending (to avoid sending response mutliple times)
565f53da564S[email protected]         signaling_responses_pending--;
566f53da564S[email protected]         int i;
567f53da564S[email protected]         for (i=0; i < signaling_responses_pending; i++){
568f53da564S[email protected]             memcpy(&signaling_responses[i], &signaling_responses[i+1], sizeof(l2cap_signaling_response_t));
569f53da564S[email protected]         }
570f53da564S[email protected] 
571f53da564S[email protected]         switch (response_code){
5722b360848Smatthias.ringwald             case CONNECTION_REQUEST:
5732b360848Smatthias.ringwald                 l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, result, 0);
5742bd8b7e7S[email protected]                 // also disconnect if result is 0x0003 - security blocked
5754d816277S[email protected]                 if (result == 0x0003){
5762bd8b7e7S[email protected]                     hci_disconnect_security_block(handle);
5774d816277S[email protected]                 }
5782b360848Smatthias.ringwald                 break;
5792b83fb7dSmatthias.ringwald             case ECHO_REQUEST:
5802b83fb7dSmatthias.ringwald                 l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, 0, NULL);
5812b83fb7dSmatthias.ringwald                 break;
5822b83fb7dSmatthias.ringwald             case INFORMATION_REQUEST:
5833b0484b3S[email protected]                 switch (infoType){
5843b0484b3S[email protected]                     case 1: { // Connectionless MTU
5853b0484b3S[email protected]                         uint16_t connectionless_mtu = hci_max_acl_data_packet_length();
5863b0484b3S[email protected]                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(connectionless_mtu), &connectionless_mtu);
5873b0484b3S[email protected]                         break;
5883b0484b3S[email protected]                     }
5893b0484b3S[email protected]                     case 2: { // Extended Features Supported
590462e630dS[email protected]                         // extended features request supported, features: fixed channels, unicast connectionless data reception
591462e630dS[email protected]                         uint32_t features = 0x280;
5923b0484b3S[email protected]                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(features), &features);
5933b0484b3S[email protected]                         break;
5943b0484b3S[email protected]                     }
5953b0484b3S[email protected]                     case 3: { // Fixed Channels Supported
5963b0484b3S[email protected]                         uint8_t map[8];
5973b0484b3S[email protected]                         memset(map, 0, 8);
598288636a2SMatthias Ringwald                         map[0] = 0x06;  // L2CAP Signaling Channel (0x02) + Connectionless reception (0x04)
5993b0484b3S[email protected]                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(map), &map);
6003b0484b3S[email protected]                         break;
6013b0484b3S[email protected]                     }
6023b0484b3S[email protected]                     default:
6032b83fb7dSmatthias.ringwald                         // all other types are not supported
6042b83fb7dSmatthias.ringwald                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL);
6053b0484b3S[email protected]                         break;
6062b83fb7dSmatthias.ringwald                 }
6072b83fb7dSmatthias.ringwald                 break;
60863a7246aSmatthias.ringwald             case COMMAND_REJECT:
6095ca8d57bS[email protected]                 l2cap_send_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL);
610a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
61170efece1S[email protected]             case COMMAND_REJECT_LE:
61270efece1S[email protected]                 l2cap_send_le_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL);
61363a7246aSmatthias.ringwald                 break;
61470efece1S[email protected] #endif
6152b83fb7dSmatthias.ringwald             default:
6162b83fb7dSmatthias.ringwald                 // should not happen
6172b83fb7dSmatthias.ringwald                 break;
6182b83fb7dSmatthias.ringwald         }
6192b83fb7dSmatthias.ringwald     }
6202b83fb7dSmatthias.ringwald 
621ae280e73Smatthias.ringwald     uint8_t  config_options[4];
622665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
623665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
624665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
625baf94f06S[email protected] 
626665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
62722c29ab4SMatthias Ringwald         // log_info("l2cap_run: channel %p, state %u, var 0x%02x", channel, channel->state, channel->state_var);
6282cd0be45Smatthias.ringwald         switch (channel->state){
6292cd0be45Smatthias.ringwald 
630df3354fcS[email protected]             case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE:
631ad671560S[email protected]             case L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT:
632fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
633a00031e2S[email protected]                 if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND) {
634ad671560S[email protected]                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND);
635fc64f94aSMatthias Ringwald                     l2cap_send_signaling_packet(channel->con_handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 1, 0);
636ad671560S[email protected]                 }
637ad671560S[email protected]                 break;
638ad671560S[email protected] 
63902b22dc4Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION:
640baf94f06S[email protected]                 if (!hci_can_send_command_packet_now()) break;
64164472d52Smatthias.ringwald                 // send connection request - set state first
64264472d52Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE;
64302b22dc4Smatthias.ringwald                 // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch
6448f8108aaSmatthias.ringwald                 hci_send_cmd(&hci_create_connection, channel->address, hci_usable_acl_packet_types(), 0, 0, 0, 1);
64502b22dc4Smatthias.ringwald                 break;
64602b22dc4Smatthias.ringwald 
647e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE:
648fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
64922c29ab4SMatthias Ringwald                 channel->state = L2CAP_STATE_INVALID;
650fc64f94aSMatthias Ringwald                 l2cap_send_signaling_packet(channel->con_handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, channel->reason, 0);
651e7ff783cSmatthias.ringwald                 // discard channel - l2cap_finialize_channel_close without sending l2cap close event
6529dcb2fb2S[email protected]                 l2cap_stop_rtx(channel);
653665d90f2SMatthias Ringwald                 btstack_linked_list_iterator_remove(&it);
654d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
655e7ff783cSmatthias.ringwald                 break;
656e7ff783cSmatthias.ringwald 
657552d92a1Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT:
658fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
659fa8473a4Smatthias.ringwald                 channel->state = L2CAP_STATE_CONFIG;
66028ca2b46S[email protected]                 channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
661fc64f94aSMatthias Ringwald                 l2cap_send_signaling_packet(channel->con_handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 0, 0);
662552d92a1Smatthias.ringwald                 break;
663552d92a1Smatthias.ringwald 
6646fdcc387Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST:
665fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
6666fdcc387Smatthias.ringwald                 // success, start l2cap handshake
667b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
6686fdcc387Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECT_RSP;
669fc64f94aSMatthias Ringwald                 l2cap_send_signaling_packet( channel->con_handle, CONNECTION_REQUEST, channel->local_sig_id, channel->psm, channel->local_cid);
6705932bd7cS[email protected]                 l2cap_start_rtx(channel);
6716fdcc387Smatthias.ringwald                 break;
6726fdcc387Smatthias.ringwald 
673fa8473a4Smatthias.ringwald             case L2CAP_STATE_CONFIG:
674fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
67573cf2b3dSmatthias.ringwald                 if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP){
67663a7246aSmatthias.ringwald                     uint16_t flags = 0;
67728ca2b46S[email protected]                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP);
67863a7246aSmatthias.ringwald                     if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT) {
67963a7246aSmatthias.ringwald                         flags = 1;
68063a7246aSmatthias.ringwald                     } else {
68128ca2b46S[email protected]                         channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP);
68263a7246aSmatthias.ringwald                     }
68363a7246aSmatthias.ringwald                     if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID){
684fc64f94aSMatthias Ringwald                         l2cap_send_signaling_packet(channel->con_handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, L2CAP_CONF_RESULT_UNKNOWN_OPTIONS, 0, NULL);
68563a7246aSmatthias.ringwald                     } else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU){
68663a7246aSmatthias.ringwald                         config_options[0] = 1; // MTU
68763a7246aSmatthias.ringwald                         config_options[1] = 2; // len param
688f8fbdce0SMatthias Ringwald                         little_endian_store_16( (uint8_t*)&config_options, 2, channel->remote_mtu);
689fc64f94aSMatthias Ringwald                         l2cap_send_signaling_packet(channel->con_handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 4, &config_options);
69063a7246aSmatthias.ringwald                         channelStateVarClearFlag(channel,L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU);
69163a7246aSmatthias.ringwald                     } else {
692fc64f94aSMatthias Ringwald                         l2cap_send_signaling_packet(channel->con_handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 0, NULL);
69363a7246aSmatthias.ringwald                     }
69463a7246aSmatthias.ringwald                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT);
695fa8473a4Smatthias.ringwald                 }
69673cf2b3dSmatthias.ringwald                 else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ){
69728ca2b46S[email protected]                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
69828ca2b46S[email protected]                     channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_REQ);
699b1988dceSmatthias.ringwald                     channel->local_sig_id = l2cap_next_sig_id();
700ae280e73Smatthias.ringwald                     config_options[0] = 1; // MTU
701ae280e73Smatthias.ringwald                     config_options[1] = 2; // len param
702f8fbdce0SMatthias Ringwald                     little_endian_store_16( (uint8_t*)&config_options, 2, channel->local_mtu);
703fc64f94aSMatthias Ringwald                     l2cap_send_signaling_packet(channel->con_handle, CONFIGURE_REQUEST, channel->local_sig_id, channel->remote_cid, 0, 4, &config_options);
7045932bd7cS[email protected]                     l2cap_start_rtx(channel);
705fa8473a4Smatthias.ringwald                 }
706fa8473a4Smatthias.ringwald                 if (l2cap_channel_ready_for_open(channel)){
707552d92a1Smatthias.ringwald                     channel->state = L2CAP_STATE_OPEN;
708552d92a1Smatthias.ringwald                     l2cap_emit_channel_opened(channel, 0);  // success
709fa8473a4Smatthias.ringwald                 }
710552d92a1Smatthias.ringwald                 break;
711552d92a1Smatthias.ringwald 
712e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE:
713fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
71422c29ab4SMatthias Ringwald                 channel->state = L2CAP_STATE_INVALID;
715fc64f94aSMatthias Ringwald                 l2cap_send_signaling_packet( channel->con_handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid);
7165932bd7cS[email protected]                 // we don't start an RTX timer for a disconnect - there's no point in closing the channel if the other side doesn't respond :)
717756102d3Smatthias.ringwald                 l2cap_finialize_channel_close(channel);  // -- remove from list
718e7ff783cSmatthias.ringwald                 break;
719e7ff783cSmatthias.ringwald 
720e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
721fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
722b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
7232cd0be45Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_DISCONNECT;
724fc64f94aSMatthias Ringwald                 l2cap_send_signaling_packet( channel->con_handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid);
7252cd0be45Smatthias.ringwald                 break;
7262cd0be45Smatthias.ringwald             default:
7272cd0be45Smatthias.ringwald                 break;
7282cd0be45Smatthias.ringwald         }
7292cd0be45Smatthias.ringwald     }
730da886c03S[email protected] 
731a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
732efedfb4cSMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_le_channels);
733efedfb4cSMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
7347f107edaSMatthias Ringwald         uint8_t  * acl_buffer;
7357f107edaSMatthias Ringwald         uint8_t  * l2cap_payload;
7367f107edaSMatthias Ringwald         uint16_t pos;
7377f107edaSMatthias Ringwald         uint16_t payload_size;
738efedfb4cSMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
739efedfb4cSMatthias Ringwald         // log_info("l2cap_run: channel %p, state %u, var 0x%02x", channel, channel->state, channel->state_var);
740efedfb4cSMatthias Ringwald         switch (channel->state){
7415cb87675SMatthias Ringwald             case L2CAP_STATE_WILL_SEND_LE_CONNECTION_REQUEST:
7425cb87675SMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
7435cb87675SMatthias Ringwald                 channel->state = L2CAP_STATE_WAIT_LE_CONNECTION_RESPONSE;
7445cb87675SMatthias Ringwald                 // le psm, source cid, mtu, mps, initial credits
7451b8b8d05SMatthias Ringwald                 channel->local_sig_id = l2cap_next_sig_id();
746*cd529728SMatthias Ringwald                 l2cap_send_le_signaling_packet( channel->con_handle, LE_CREDIT_BASED_CONNECTION_REQUEST, channel->local_sig_id, channel->psm, channel->local_cid, channel->local_mtu, 23, 1);
7475cb87675SMatthias Ringwald                 break;
74823017473SMatthias Ringwald             case L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_ACCEPT:
74923017473SMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
75023017473SMatthias Ringwald                 // TODO: support larger MPS
75123017473SMatthias Ringwald                 channel->state = L2CAP_STATE_OPEN;
75223017473SMatthias Ringwald                 l2cap_send_le_signaling_packet(channel->con_handle, LE_CREDIT_BASED_CONNECTION_RESPONSE, channel->remote_sig_id, channel->remote_cid, channel->local_mtu, 23, channel->credits_incoming, 0);
75364e11ca9SMatthias Ringwald                 // notify client
75464e11ca9SMatthias Ringwald                 l2cap_emit_channel_opened(channel, 0);
75523017473SMatthias Ringwald                 break;
756e7d0c9aaSMatthias Ringwald             case L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_DECLINE:
757e7d0c9aaSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
758e7d0c9aaSMatthias Ringwald                 channel->state = L2CAP_STATE_INVALID;
759e7d0c9aaSMatthias Ringwald                 l2cap_send_le_signaling_packet(channel->con_handle, LE_CREDIT_BASED_CONNECTION_RESPONSE, channel->remote_sig_id, channel->remote_cid, 0, 0, 0, channel->reason);
760e7d0c9aaSMatthias Ringwald                 // discard channel - l2cap_finialize_channel_close without sending l2cap close event
761e7d0c9aaSMatthias Ringwald                 l2cap_stop_rtx(channel);
762e7d0c9aaSMatthias Ringwald                 btstack_linked_list_iterator_remove(&it);
763e7d0c9aaSMatthias Ringwald                 btstack_memory_l2cap_channel_free(channel);
764e7d0c9aaSMatthias Ringwald                 break;
7657f107edaSMatthias Ringwald             case L2CAP_STATE_OPEN:
7667f107edaSMatthias Ringwald                 if (!channel->send_sdu_buffer) break;
7677f107edaSMatthias Ringwald                 if (!channel->credits_outgoing) break;
7687f107edaSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
7697f107edaSMatthias Ringwald                 // send part of SDU
7707f107edaSMatthias Ringwald                 hci_reserve_packet_buffer();
7717f107edaSMatthias Ringwald                 acl_buffer = hci_get_outgoing_packet_buffer();
7727f107edaSMatthias Ringwald                 l2cap_payload = acl_buffer + 8;
7737f107edaSMatthias Ringwald                 pos = 0;
7747f107edaSMatthias Ringwald                 if (!channel->send_sdu_pos){
7757f107edaSMatthias Ringwald                     // store SDU len
7767f107edaSMatthias Ringwald                     channel->send_sdu_pos += 2;
7777f107edaSMatthias Ringwald                     little_endian_store_16(l2cap_payload, pos, channel->send_sdu_len);
7787f107edaSMatthias Ringwald                     pos += 2;
7797f107edaSMatthias Ringwald                 }
7807f107edaSMatthias Ringwald                 payload_size = btstack_min(channel->send_sdu_len + 2 - channel->send_sdu_pos, channel->remote_mps - pos);
7817f107edaSMatthias Ringwald                 log_info("len %u, pos %u => payload %u", channel->send_sdu_len, channel->send_sdu_pos, payload_size);
7827f107edaSMatthias Ringwald                 memcpy(&l2cap_payload[pos], &channel->send_sdu_buffer[channel->send_sdu_pos-2], payload_size); // -2 for virtual SDU len
7837f107edaSMatthias Ringwald                 pos += payload_size;
7847f107edaSMatthias Ringwald                 channel->send_sdu_pos += payload_size;
7857f107edaSMatthias Ringwald                 l2cap_setup_header(acl_buffer, channel->con_handle, channel->remote_cid, payload_size);
7867f107edaSMatthias Ringwald                 // done
7877f107edaSMatthias Ringwald 
788*cd529728SMatthias Ringwald                 // TODO: enable credit counting
7897f107edaSMatthias Ringwald                 // channel->credits_outgoing--;
7907f107edaSMatthias Ringwald 
7917f107edaSMatthias Ringwald                 if (channel->send_sdu_pos >= channel->send_sdu_len + 2){
7927f107edaSMatthias Ringwald                     channel->send_sdu_buffer = NULL;
7937f107edaSMatthias Ringwald                     // TODO: send done event
7947f107edaSMatthias Ringwald                 }
7957f107edaSMatthias Ringwald                 hci_send_acl_packet_buffer(8 + pos);
7967f107edaSMatthias Ringwald                 break;
797828a7f7aSMatthias Ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
798828a7f7aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
799828a7f7aSMatthias Ringwald                 channel->local_sig_id = l2cap_next_sig_id();
800828a7f7aSMatthias Ringwald                 channel->state = L2CAP_STATE_WAIT_DISCONNECT;
801828a7f7aSMatthias Ringwald                 l2cap_send_le_signaling_packet( channel->con_handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid);
802828a7f7aSMatthias Ringwald                 break;
803828a7f7aSMatthias Ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE:
804828a7f7aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
805828a7f7aSMatthias Ringwald                 channel->state = L2CAP_STATE_INVALID;
806828a7f7aSMatthias Ringwald                 l2cap_send_le_signaling_packet( channel->con_handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid);
807828a7f7aSMatthias Ringwald                 l2cap_le_finialize_channel_close(channel);  // -- remove from list
808828a7f7aSMatthias Ringwald                 break;
809efedfb4cSMatthias Ringwald             default:
810efedfb4cSMatthias Ringwald                 break;
811efedfb4cSMatthias Ringwald         }
812efedfb4cSMatthias Ringwald     }
813efedfb4cSMatthias Ringwald 
814da886c03S[email protected]     // send l2cap con paramter update if necessary
815da886c03S[email protected]     hci_connections_get_iterator(&it);
816665d90f2SMatthias Ringwald     while(btstack_linked_list_iterator_has_next(&it)){
817665d90f2SMatthias Ringwald         hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
8185d14fa8fSMatthias Ringwald         if (connection->address_type != BD_ADDR_TYPE_LE_PUBLIC && connection->address_type != BD_ADDR_TYPE_LE_RANDOM) continue;
819b68d7bc3SMatthias Ringwald         if (!hci_can_send_acl_packet_now(connection->con_handle)) continue;
820da886c03S[email protected]         switch (connection->le_con_parameter_update_state){
821b68d7bc3SMatthias Ringwald             case CON_PARAMETER_UPDATE_SEND_REQUEST:
822b68d7bc3SMatthias Ringwald                 connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE;
823b68d7bc3SMatthias Ringwald                 l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_REQUEST, connection->le_con_param_update_identifier,
824b68d7bc3SMatthias Ringwald                                                connection->le_conn_interval_min, connection->le_conn_interval_max, connection->le_conn_latency, connection->le_supervision_timeout);
825b68d7bc3SMatthias Ringwald                 break;
826da886c03S[email protected]             case CON_PARAMETER_UPDATE_SEND_RESPONSE:
827b68d7bc3SMatthias Ringwald                 connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS;
828b68d7bc3SMatthias Ringwald                 l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_RESPONSE, connection->le_con_param_update_identifier, 0);
829da886c03S[email protected]                 break;
830da886c03S[email protected]             case CON_PARAMETER_UPDATE_DENY:
831b68d7bc3SMatthias Ringwald                 connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE;
832b68d7bc3SMatthias Ringwald                 l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_RESPONSE, connection->le_con_param_update_identifier, 1);
833da886c03S[email protected]                 break;
834da886c03S[email protected]             default:
835da886c03S[email protected]                 break;
836da886c03S[email protected]         }
837da886c03S[email protected]     }
8384d7157c3S[email protected] #endif
839da886c03S[email protected] 
84022c29ab4SMatthias Ringwald     // log_info("l2cap_run: exit");
8412cd0be45Smatthias.ringwald }
8422cd0be45Smatthias.ringwald 
8434aa9e837Smatthias.ringwald uint16_t l2cap_max_mtu(void){
8444ff786cfS[email protected]     return HCI_ACL_PAYLOAD_SIZE - L2CAP_HEADER_SIZE;
845fa8c92f6Smatthias.ringwald }
846fa8c92f6Smatthias.ringwald 
84771de195eSMatthias Ringwald uint16_t l2cap_max_le_mtu(void){
8484ff786cfS[email protected]     return l2cap_max_mtu();
849e5e1518dS[email protected] }
850e5e1518dS[email protected] 
851fc64f94aSMatthias Ringwald static void l2cap_handle_connection_complete(hci_con_handle_t con_handle, l2cap_channel_t * channel){
8522df5dadcS[email protected]     if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) {
8535533f01eS[email protected]         log_info("l2cap_handle_connection_complete expected state");
8542df5dadcS[email protected]         // success, start l2cap handshake
855fc64f94aSMatthias Ringwald         channel->con_handle = con_handle;
8562df5dadcS[email protected]         // check remote SSP feature first
8572df5dadcS[email protected]         channel->state = L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES;
8582df5dadcS[email protected]     }
8592df5dadcS[email protected] }
8602df5dadcS[email protected] 
861efedfb4cSMatthias Ringwald #ifdef ENABLE_BLE
8625cb87675SMatthias Ringwald static void l2cap_handle_le_connection_complete(l2cap_channel_t * channel, uint8_t status, hci_con_handle_t con_handle){
863efedfb4cSMatthias Ringwald     if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) {
864efedfb4cSMatthias Ringwald         log_info("l2cap_le_handle_connection_complete expected state");
8655cb87675SMatthias Ringwald         // TODO: bail if status != 0
8665cb87675SMatthias Ringwald 
867efedfb4cSMatthias Ringwald         // success, start l2cap handshake
868efedfb4cSMatthias Ringwald         channel->con_handle = con_handle;
8695cb87675SMatthias Ringwald         channel->state = L2CAP_STATE_WILL_SEND_LE_CONNECTION_REQUEST;
870efedfb4cSMatthias Ringwald     }
871efedfb4cSMatthias Ringwald }
872efedfb4cSMatthias Ringwald #endif
873efedfb4cSMatthias Ringwald 
874efedfb4cSMatthias Ringwald 
8752df5dadcS[email protected] static void l2cap_handle_remote_supported_features_received(l2cap_channel_t * channel){
8762df5dadcS[email protected]     if (channel->state != L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES) return;
8772df5dadcS[email protected] 
8782df5dadcS[email protected]     // we have been waiting for remote supported features, if both support SSP,
879ac301f95S[email protected]     log_info("l2cap received remote supported features, sec_level_0_allowed for psm %u = %u", channel->psm, l2cap_security_level_0_allowed_for_PSM(channel->psm));
880fc64f94aSMatthias Ringwald     if (gap_ssp_supported_on_both_sides(channel->con_handle) && !l2cap_security_level_0_allowed_for_PSM(channel->psm)){
8812df5dadcS[email protected]         // request security level 2
8822df5dadcS[email protected]         channel->state = L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE;
883fc64f94aSMatthias Ringwald         gap_request_security_level(channel->con_handle, LEVEL_2);
8842df5dadcS[email protected]         return;
8852df5dadcS[email protected]     }
8862df5dadcS[email protected]     // fine, go ahead
8872df5dadcS[email protected]     channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST;
8882df5dadcS[email protected] }
8892df5dadcS[email protected] 
890da144af5SMatthias Ringwald static l2cap_channel_t * l2cap_create_channel_entry(btstack_packet_handler_t packet_handler, bd_addr_t address, bd_addr_type_t address_type,
891da144af5SMatthias Ringwald     uint16_t psm, uint16_t local_mtu, gap_security_level_t security_level){
892da144af5SMatthias Ringwald 
893da144af5SMatthias Ringwald     l2cap_channel_t * channel = btstack_memory_l2cap_channel_get();
894da144af5SMatthias Ringwald     if (!channel) {
895da144af5SMatthias Ringwald         return NULL;
896da144af5SMatthias Ringwald     }
897da144af5SMatthias Ringwald 
898da144af5SMatthias Ringwald      // Init memory (make valgrind happy)
899da144af5SMatthias Ringwald     memset(channel, 0, sizeof(l2cap_channel_t));
900da144af5SMatthias Ringwald 
901da144af5SMatthias Ringwald     // fill in
902da144af5SMatthias Ringwald     channel->packet_handler = packet_handler;
903da144af5SMatthias Ringwald     bd_addr_copy(channel->address, address);
904da144af5SMatthias Ringwald     channel->address_type = address_type;
905da144af5SMatthias Ringwald     channel->psm = psm;
906da144af5SMatthias Ringwald     channel->local_mtu  = local_mtu;
907da144af5SMatthias Ringwald     channel->remote_mtu = L2CAP_MINIMAL_MTU;
908da144af5SMatthias Ringwald     channel->required_security_level = security_level;
909da144af5SMatthias Ringwald 
910da144af5SMatthias Ringwald     //
911da144af5SMatthias Ringwald     channel->local_cid = l2cap_next_local_cid();
912da144af5SMatthias Ringwald     channel->con_handle = 0;
913da144af5SMatthias Ringwald 
914da144af5SMatthias Ringwald     // set initial state
915da144af5SMatthias Ringwald     channel->state = L2CAP_STATE_WILL_SEND_CREATE_CONNECTION;
916da144af5SMatthias Ringwald     channel->state_var = L2CAP_CHANNEL_STATE_VAR_NONE;
917da144af5SMatthias Ringwald     channel->remote_sig_id = L2CAP_SIG_ID_INVALID;
918da144af5SMatthias Ringwald     channel->local_sig_id = L2CAP_SIG_ID_INVALID;
919da144af5SMatthias Ringwald     return channel;
920da144af5SMatthias Ringwald }
921da144af5SMatthias Ringwald 
9229077cb15SMatthias Ringwald /**
9239077cb15SMatthias Ringwald  * @brief Creates L2CAP channel to the PSM of a remote device with baseband address. A new baseband connection will be initiated if necessary.
9249077cb15SMatthias Ringwald  * @param packet_handler
9259077cb15SMatthias Ringwald  * @param address
9269077cb15SMatthias Ringwald  * @param psm
9279077cb15SMatthias Ringwald  * @param mtu
9289077cb15SMatthias Ringwald  * @param local_cid
9299077cb15SMatthias Ringwald  */
9309077cb15SMatthias Ringwald 
931da144af5SMatthias Ringwald uint8_t l2cap_create_channel(btstack_packet_handler_t channel_packet_handler, bd_addr_t address, uint16_t psm, uint16_t local_mtu, uint16_t * out_local_cid){
932da144af5SMatthias Ringwald     log_info("L2CAP_CREATE_CHANNEL addr %s psm 0x%x mtu %u", bd_addr_to_str(address), psm, local_mtu);
933da144af5SMatthias Ringwald 
934da144af5SMatthias Ringwald     if (local_mtu > l2cap_max_mtu()) {
935da144af5SMatthias Ringwald         local_mtu = l2cap_max_mtu();
936da144af5SMatthias Ringwald     }
937da144af5SMatthias Ringwald 
938da144af5SMatthias Ringwald     l2cap_channel_t * channel = l2cap_create_channel_entry(channel_packet_handler, address, BD_ADDR_TYPE_CLASSIC, psm, local_mtu, LEVEL_0);
939fc64f94aSMatthias Ringwald     if (!channel) {
9409077cb15SMatthias Ringwald         return BTSTACK_MEMORY_ALLOC_FAILED;
9419077cb15SMatthias Ringwald     }
9429077cb15SMatthias Ringwald 
9439077cb15SMatthias Ringwald     // add to connections list
944fc64f94aSMatthias Ringwald     btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) channel);
9459077cb15SMatthias Ringwald 
9469077cb15SMatthias Ringwald     // store local_cid
9479077cb15SMatthias Ringwald     if (out_local_cid){
948fc64f94aSMatthias Ringwald        *out_local_cid = channel->local_cid;
9499077cb15SMatthias Ringwald     }
9509077cb15SMatthias Ringwald 
9519077cb15SMatthias Ringwald     // check if hci connection is already usable
9529077cb15SMatthias Ringwald     hci_connection_t * conn = hci_connection_for_bd_addr_and_type(address, BD_ADDR_TYPE_CLASSIC);
9539077cb15SMatthias Ringwald     if (conn){
954add0254bSMatthias Ringwald         log_info("l2cap_create_channel, hci connection already exists");
955fc64f94aSMatthias Ringwald         l2cap_handle_connection_complete(conn->con_handle, channel);
9569077cb15SMatthias Ringwald         // check if remote supported fearures are already received
9579077cb15SMatthias Ringwald         if (conn->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) {
958fc64f94aSMatthias Ringwald             l2cap_handle_remote_supported_features_received(channel);
9599077cb15SMatthias Ringwald         }
9609077cb15SMatthias Ringwald     }
9619077cb15SMatthias Ringwald 
9629077cb15SMatthias Ringwald     l2cap_run();
9639077cb15SMatthias Ringwald 
9649077cb15SMatthias Ringwald     return 0;
9659077cb15SMatthias Ringwald }
9669077cb15SMatthias Ringwald 
967ce8f182eSMatthias Ringwald void
968ce8f182eSMatthias Ringwald l2cap_disconnect(uint16_t local_cid, uint8_t reason){
969e0abb8e7S[email protected]     log_info("L2CAP_DISCONNECT local_cid 0x%x reason 0x%x", local_cid, reason);
970b35f641cSmatthias.ringwald     // find channel for local_cid
971b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
972f62db1e3Smatthias.ringwald     if (channel) {
973e7ff783cSmatthias.ringwald         channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
974f62db1e3Smatthias.ringwald     }
9752cd0be45Smatthias.ringwald     // process
9762cd0be45Smatthias.ringwald     l2cap_run();
97743625864Smatthias.ringwald }
9781e6aba47Smatthias.ringwald 
979afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){
980665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
981665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
982665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
983665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
984058e3d6bSMatthias Ringwald         if ( bd_addr_cmp( channel->address, address) != 0) continue;
985c22aecc9S[email protected]         // channel for this address found
986c22aecc9S[email protected]         switch (channel->state){
987c22aecc9S[email protected]             case L2CAP_STATE_WAIT_CONNECTION_COMPLETE:
988c22aecc9S[email protected]             case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION:
989afde0c52Smatthias.ringwald                 // failure, forward error code
990afde0c52Smatthias.ringwald                 l2cap_emit_channel_opened(channel, status);
991afde0c52Smatthias.ringwald                 // discard channel
9929dcb2fb2S[email protected]                 l2cap_stop_rtx(channel);
993665d90f2SMatthias Ringwald                 btstack_linked_list_iterator_remove(&it);
994d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
995c22aecc9S[email protected]                 break;
996c22aecc9S[email protected]             default:
997c22aecc9S[email protected]                 break;
998afde0c52Smatthias.ringwald         }
999afde0c52Smatthias.ringwald     }
1000afde0c52Smatthias.ringwald }
1001afde0c52Smatthias.ringwald 
1002afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){
1003665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1004665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
1005665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1006665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1007058e3d6bSMatthias Ringwald         if ( ! bd_addr_cmp( channel->address, address) ){
10082df5dadcS[email protected]             l2cap_handle_connection_complete(handle, channel);
1009afde0c52Smatthias.ringwald         }
1010afde0c52Smatthias.ringwald     }
10116fdcc387Smatthias.ringwald     // process
10126fdcc387Smatthias.ringwald     l2cap_run();
1013afde0c52Smatthias.ringwald }
1014b448a0e7Smatthias.ringwald 
101533c40538SMatthias Ringwald static void l2cap_notify_channel_can_send(void){
101633c40538SMatthias Ringwald     btstack_linked_list_iterator_t it;
101733c40538SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
101833c40538SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
101933c40538SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
102033c40538SMatthias Ringwald         if (!channel->waiting_for_can_send_now) continue;
1021fc64f94aSMatthias Ringwald         if (!hci_can_send_acl_packet_now(channel->con_handle)) continue;
102233c40538SMatthias Ringwald         channel->waiting_for_can_send_now = 0;
102334e7e577SMatthias Ringwald         l2cap_emit_can_send_now(channel->packet_handler, channel->local_cid);
102433c40538SMatthias Ringwald     }
10252125de09SMatthias Ringwald 
10262125de09SMatthias Ringwald     int i;
10272125de09SMatthias Ringwald     for (i=0;i<L2CAP_FIXED_CHANNEL_TABLE_SIZE;i++){
1028773c4106SMatthias Ringwald         if (!fixed_channels[i].callback) continue;
10292125de09SMatthias Ringwald         if (!fixed_channels[i].waiting_for_can_send_now) continue;
103034e7e577SMatthias Ringwald         int can_send;
103134e7e577SMatthias Ringwald         if (l2cap_fixed_channel_table_index_is_le(i)){
103234e7e577SMatthias Ringwald             can_send = hci_can_send_acl_le_packet_now();
103334e7e577SMatthias Ringwald         } else {
103434e7e577SMatthias Ringwald             can_send = hci_can_send_acl_classic_packet_now();
103534e7e577SMatthias Ringwald         }
103634e7e577SMatthias Ringwald         if (!can_send) continue;
103734e7e577SMatthias Ringwald         fixed_channels[i].waiting_for_can_send_now = 0;
103834e7e577SMatthias Ringwald         l2cap_emit_can_send_now(fixed_channels[i].callback, l2cap_fixed_channel_table_channel_id_for_index(i));
10392125de09SMatthias Ringwald     }
104033c40538SMatthias Ringwald }
104133c40538SMatthias Ringwald 
1042d9a7306aSMatthias Ringwald static void l2cap_hci_event_handler(uint8_t packet_type, uint16_t cid, uint8_t *packet, uint16_t size){
1043afde0c52Smatthias.ringwald 
1044afde0c52Smatthias.ringwald     bd_addr_t address;
1045afde0c52Smatthias.ringwald     hci_con_handle_t handle;
1046665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
10472d00edd4Smatthias.ringwald     int hci_con_used;
1048afde0c52Smatthias.ringwald 
10490e2df43fSMatthias Ringwald     switch(hci_event_packet_get_type(packet)){
1050afde0c52Smatthias.ringwald 
1051afde0c52Smatthias.ringwald         // handle connection complete events
1052afde0c52Smatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
1053724d70a2SMatthias Ringwald             reverse_bd_addr(&packet[5], address);
1054afde0c52Smatthias.ringwald             if (packet[2] == 0){
1055f8fbdce0SMatthias Ringwald                 handle = little_endian_read_16(packet, 3);
1056afde0c52Smatthias.ringwald                 l2cap_handle_connection_success_for_addr(address, handle);
1057afde0c52Smatthias.ringwald             } else {
1058afde0c52Smatthias.ringwald                 l2cap_handle_connection_failed_for_addr(address, packet[2]);
1059afde0c52Smatthias.ringwald             }
1060afde0c52Smatthias.ringwald             break;
1061afde0c52Smatthias.ringwald 
1062afde0c52Smatthias.ringwald         // handle successful create connection cancel command
1063afde0c52Smatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
1064073bd0faSMatthias Ringwald             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_create_connection_cancel)) {
1065afde0c52Smatthias.ringwald                 if (packet[5] == 0){
1066724d70a2SMatthias Ringwald                     reverse_bd_addr(&packet[6], address);
1067afde0c52Smatthias.ringwald                     // CONNECTION TERMINATED BY LOCAL HOST (0X16)
1068afde0c52Smatthias.ringwald                     l2cap_handle_connection_failed_for_addr(address, 0x16);
106903cfbabcSmatthias.ringwald                 }
10701e6aba47Smatthias.ringwald             }
107139d59809Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
107239d59809Smatthias.ringwald             break;
107339d59809Smatthias.ringwald 
107439d59809Smatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
107539d59809Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
1076afde0c52Smatthias.ringwald             break;
107727a923d0Smatthias.ringwald 
10781e6aba47Smatthias.ringwald         // handle disconnection complete events
1079afde0c52Smatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
1080c22aecc9S[email protected]             // send l2cap disconnect events for all channels on this handle and free them
1081f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 3);
1082665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_channels);
1083665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
1084665d90f2SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1085fc64f94aSMatthias Ringwald                 if (channel->con_handle != handle) continue;
108615ec09bbSmatthias.ringwald                 l2cap_emit_channel_closed(channel);
10879dcb2fb2S[email protected]                 l2cap_stop_rtx(channel);
1088665d90f2SMatthias Ringwald                 btstack_linked_list_iterator_remove(&it);
1089d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
109027a923d0Smatthias.ringwald             }
1091991fea48SMatthias Ringwald #ifdef ENABLE_BLE
1092991fea48SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_le_channels);
1093991fea48SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
1094991fea48SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1095991fea48SMatthias Ringwald                 if (channel->con_handle != handle) continue;
1096991fea48SMatthias Ringwald                 l2cap_emit_channel_closed(channel);
1097991fea48SMatthias Ringwald                 btstack_linked_list_iterator_remove(&it);
1098991fea48SMatthias Ringwald                 btstack_memory_l2cap_channel_free(channel);
1099991fea48SMatthias Ringwald             }
1100991fea48SMatthias Ringwald #endif
1101afde0c52Smatthias.ringwald             break;
1102fcadd0caSmatthias.ringwald 
110333c40538SMatthias Ringwald         // Notify channel packet handler if they can send now
110463fa3374SMatthias Ringwald         case HCI_EVENT_TRANSPORT_PACKET_SENT:
11056218e6f1Smatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
110602b22dc4Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
110733c40538SMatthias Ringwald             l2cap_notify_channel_can_send();
11086218e6f1Smatthias.ringwald             break;
11096218e6f1Smatthias.ringwald 
1110ee091cf1Smatthias.ringwald         // HCI Connection Timeouts
1111afde0c52Smatthias.ringwald         case L2CAP_EVENT_TIMEOUT_CHECK:
1112f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 2);
1113bd04d84aSMatthias Ringwald             if (gap_get_connection_type(handle) != GAP_CONNECTION_ACL) break;
111480ca58a0Smatthias.ringwald             if (hci_authentication_active_for_handle(handle)) break;
11152d00edd4Smatthias.ringwald             hci_con_used = 0;
1116665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_channels);
1117665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
1118665d90f2SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1119fc64f94aSMatthias Ringwald                 if (channel->con_handle != handle) continue;
11202d00edd4Smatthias.ringwald                 hci_con_used = 1;
1121c22aecc9S[email protected]                 break;
1122ee091cf1Smatthias.ringwald             }
11232d00edd4Smatthias.ringwald             if (hci_con_used) break;
1124d94d3cafS[email protected]             if (!hci_can_send_command_packet_now()) break;
11259edc8742Smatthias.ringwald             hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection
1126afde0c52Smatthias.ringwald             break;
1127ee091cf1Smatthias.ringwald 
1128df3354fcS[email protected]         case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE:
1129f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 3);
1130665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_channels);
1131665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
1132665d90f2SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1133fc64f94aSMatthias Ringwald                 if (channel->con_handle != handle) continue;
11342df5dadcS[email protected]                 l2cap_handle_remote_supported_features_received(channel);
1135df3354fcS[email protected]                 break;
1136df3354fcS[email protected]             }
1137c22aecc9S[email protected]             break;
1138df3354fcS[email protected] 
11395cb87675SMatthias Ringwald #ifdef ENABLE_BLE
11405cb87675SMatthias Ringwald         case HCI_EVENT_LE_META:
11415cb87675SMatthias Ringwald             switch (packet[2]){
11425cb87675SMatthias Ringwald                 int addr_type;
11435cb87675SMatthias Ringwald                 l2cap_channel_t * channel;
11445cb87675SMatthias Ringwald                 case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
11455cb87675SMatthias Ringwald                     // Connection management
11465cb87675SMatthias Ringwald                     reverse_bd_addr(&packet[8], address);
11475cb87675SMatthias Ringwald                     addr_type = (bd_addr_type_t)packet[7];
11485cb87675SMatthias Ringwald                     log_info("L2CAP - LE Connection_complete (status=%u) type %u, %s", packet[3], addr_type, bd_addr_to_str(address));
11495cb87675SMatthias Ringwald                     // get l2cap_channel
11505cb87675SMatthias Ringwald                     // also pass in status
11515cb87675SMatthias Ringwald                     channel = l2cap_get_le_channel_for_address_and_addr_type(address, addr_type);
11525cb87675SMatthias Ringwald                     if (!channel) break;
11535cb87675SMatthias Ringwald                     l2cap_handle_le_connection_complete(channel, packet[3], little_endian_read_16(packet, 4));
11545cb87675SMatthias Ringwald                     break;
11555cb87675SMatthias Ringwald                 default:
11565cb87675SMatthias Ringwald                     break;
11575cb87675SMatthias Ringwald             }
11585cb87675SMatthias Ringwald             break;
11595cb87675SMatthias Ringwald #endif
11605cb87675SMatthias Ringwald 
11615611a760SMatthias Ringwald         case GAP_EVENT_SECURITY_LEVEL:
1162f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 2);
1163bd63148eS[email protected]             log_info("l2cap - security level update");
1164665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_channels);
1165665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
1166665d90f2SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1167fc64f94aSMatthias Ringwald                 if (channel->con_handle != handle) continue;
11685533f01eS[email protected] 
1169bd63148eS[email protected]                 log_info("l2cap - state %u", channel->state);
1170bd63148eS[email protected] 
1171e569dfd9SMatthias Ringwald                 gap_security_level_t actual_level = (gap_security_level_t) packet[4];
11725533f01eS[email protected]                 gap_security_level_t required_level = channel->required_security_level;
11735533f01eS[email protected] 
1174df3354fcS[email protected]                 switch (channel->state){
1175df3354fcS[email protected]                     case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE:
11765533f01eS[email protected]                         if (actual_level >= required_level){
1177f85a9399S[email protected]                             channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT;
1178f85a9399S[email protected]                             l2cap_emit_connection_request(channel);
11791eb2563eS[email protected]                         } else {
1180775ecc36SMatthias Ringwald                             channel->reason = 0x0003; // security block
11811eb2563eS[email protected]                             channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE;
11821eb2563eS[email protected]                         }
1183df3354fcS[email protected]                         break;
1184df3354fcS[email protected] 
1185df3354fcS[email protected]                     case L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE:
11865533f01eS[email protected]                         if (actual_level >= required_level){
1187df3354fcS[email protected]                             channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST;
1188df3354fcS[email protected]                         } else {
1189df3354fcS[email protected]                             // disconnnect, authentication not good enough
1190df3354fcS[email protected]                             hci_disconnect_security_block(handle);
1191df3354fcS[email protected]                         }
1192df3354fcS[email protected]                         break;
1193df3354fcS[email protected] 
1194df3354fcS[email protected]                     default:
1195df3354fcS[email protected]                         break;
1196df3354fcS[email protected]                 }
1197f85a9399S[email protected]             }
1198f85a9399S[email protected]             break;
1199f85a9399S[email protected] 
1200afde0c52Smatthias.ringwald         default:
1201afde0c52Smatthias.ringwald             break;
1202afde0c52Smatthias.ringwald     }
1203afde0c52Smatthias.ringwald 
1204bd63148eS[email protected]     l2cap_run();
12051e6aba47Smatthias.ringwald }
12061e6aba47Smatthias.ringwald 
1207afde0c52Smatthias.ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){
1208b1988dceSmatthias.ringwald     channel->remote_sig_id = identifier;
1209e7ff783cSmatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE;
1210e7ff783cSmatthias.ringwald     l2cap_run();
121184836b65Smatthias.ringwald }
121284836b65Smatthias.ringwald 
12132b360848Smatthias.ringwald static void l2cap_register_signaling_response(hci_con_handle_t handle, uint8_t code, uint8_t sig_id, uint16_t data){
12144cf56b4aSmatthias.ringwald     // Vol 3, Part A, 4.3: "The DCID and SCID fields shall be ignored when the result field indi- cates the connection was refused."
12152b360848Smatthias.ringwald     if (signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) {
12162b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].handle = handle;
12172b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].code = code;
12182b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].sig_id = sig_id;
12192b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].data = data;
12202b360848Smatthias.ringwald         signaling_responses_pending++;
12212b360848Smatthias.ringwald         l2cap_run();
12222b360848Smatthias.ringwald     }
12232b360848Smatthias.ringwald }
12242b360848Smatthias.ringwald 
1225b35f641cSmatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){
1226645658c9Smatthias.ringwald 
12279da54300S[email protected]     // log_info("l2cap_handle_connection_request for handle %u, psm %u cid 0x%02x", handle, psm, source_cid);
1228645658c9Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
1229645658c9Smatthias.ringwald     if (!service) {
1230645658c9Smatthias.ringwald         // 0x0002 PSM not supported
12312b360848Smatthias.ringwald         l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0002);
1232645658c9Smatthias.ringwald         return;
1233645658c9Smatthias.ringwald     }
1234645658c9Smatthias.ringwald 
12355061f3afS[email protected]     hci_connection_t * hci_connection = hci_connection_for_handle( handle );
1236645658c9Smatthias.ringwald     if (!hci_connection) {
12372b360848Smatthias.ringwald         //
12389da54300S[email protected]         log_error("no hci_connection for handle %u", handle);
1239645658c9Smatthias.ringwald         return;
1240645658c9Smatthias.ringwald     }
12412bd8b7e7S[email protected] 
1242645658c9Smatthias.ringwald     // alloc structure
12439da54300S[email protected]     // log_info("l2cap_handle_connection_request register channel");
1244da144af5SMatthias Ringwald     l2cap_channel_t * channel = l2cap_create_channel_entry(service->packet_handler, hci_connection->address, BD_ADDR_TYPE_CLASSIC,
1245da144af5SMatthias Ringwald     psm, service->mtu, service->required_security_level);
12462b360848Smatthias.ringwald     if (!channel){
12472b360848Smatthias.ringwald         // 0x0004 No resources available
12482b360848Smatthias.ringwald         l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0004);
12492b360848Smatthias.ringwald         return;
12502b360848Smatthias.ringwald     }
1251da144af5SMatthias Ringwald 
1252fc64f94aSMatthias Ringwald     channel->con_handle = handle;
1253b35f641cSmatthias.ringwald     channel->remote_cid = source_cid;
1254b1988dceSmatthias.ringwald     channel->remote_sig_id = sig_id;
1255645658c9Smatthias.ringwald 
1256f53da564S[email protected]     // limit local mtu to max acl packet length - l2cap header
12572985cb84Smatthias.ringwald     if (channel->local_mtu > l2cap_max_mtu()) {
12582985cb84Smatthias.ringwald         channel->local_mtu = l2cap_max_mtu();
12599775e25bSmatthias.ringwald     }
12609775e25bSmatthias.ringwald 
1261645658c9Smatthias.ringwald     // set initial state
1262df3354fcS[email protected]     channel->state =     L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE;
1263ad671560S[email protected]     channel->state_var = L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND;
1264e405ae81Smatthias.ringwald 
1265645658c9Smatthias.ringwald     // add to connections list
1266665d90f2SMatthias Ringwald     btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) channel);
1267645658c9Smatthias.ringwald 
1268f85a9399S[email protected]     // assert security requirements
12691eb2563eS[email protected]     gap_request_security_level(handle, channel->required_security_level);
1270e405ae81Smatthias.ringwald }
1271645658c9Smatthias.ringwald 
1272ce8f182eSMatthias Ringwald void l2cap_accept_connection(uint16_t local_cid){
1273e0abb8e7S[email protected]     log_info("L2CAP_ACCEPT_CONNECTION local_cid 0x%x", local_cid);
1274b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
1275e405ae81Smatthias.ringwald     if (!channel) {
1276ce8f182eSMatthias Ringwald         log_error("l2cap_accept_connection called but local_cid 0x%x not found", local_cid);
1277e405ae81Smatthias.ringwald         return;
1278e405ae81Smatthias.ringwald     }
1279e405ae81Smatthias.ringwald 
1280552d92a1Smatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT;
1281e405ae81Smatthias.ringwald 
1282552d92a1Smatthias.ringwald     // process
1283552d92a1Smatthias.ringwald     l2cap_run();
1284e405ae81Smatthias.ringwald }
1285645658c9Smatthias.ringwald 
12867ef6a7bbSMatthias Ringwald void l2cap_decline_connection(uint16_t local_cid){
12877ef6a7bbSMatthias Ringwald     log_info("L2CAP_DECLINE_CONNECTION local_cid 0x%x", local_cid);
1288b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid);
1289e405ae81Smatthias.ringwald     if (!channel) {
1290ce8f182eSMatthias Ringwald         log_error( "l2cap_decline_connection called but local_cid 0x%x not found", local_cid);
1291e405ae81Smatthias.ringwald         return;
1292e405ae81Smatthias.ringwald     }
1293e7ff783cSmatthias.ringwald     channel->state  = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE;
12947ef6a7bbSMatthias Ringwald     channel->reason = 0x04; // no resources available
1295e7ff783cSmatthias.ringwald     l2cap_run();
1296645658c9Smatthias.ringwald }
1297645658c9Smatthias.ringwald 
12987f02f414SMatthias Ringwald static void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){
1299b1988dceSmatthias.ringwald 
1300b1988dceSmatthias.ringwald     channel->remote_sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
1301b1988dceSmatthias.ringwald 
1302f8fbdce0SMatthias Ringwald     uint16_t flags = little_endian_read_16(command, 6);
130363a7246aSmatthias.ringwald     if (flags & 1) {
130463a7246aSmatthias.ringwald         channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT);
130563a7246aSmatthias.ringwald     }
130663a7246aSmatthias.ringwald 
13072784b77dSmatthias.ringwald     // accept the other's configuration options
1308f8fbdce0SMatthias Ringwald     uint16_t end_pos = 4 + little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
13093de7c0caSmatthias.ringwald     uint16_t pos     = 8;
13103de7c0caSmatthias.ringwald     while (pos < end_pos){
131163a7246aSmatthias.ringwald         uint8_t option_hint = command[pos] >> 7;
131263a7246aSmatthias.ringwald         uint8_t option_type = command[pos] & 0x7f;
131363a7246aSmatthias.ringwald         log_info("l2cap cid %u, hint %u, type %u", channel->local_cid, option_hint, option_type);
131463a7246aSmatthias.ringwald         pos++;
13151dc511deSmatthias.ringwald         uint8_t length = command[pos++];
13161dc511deSmatthias.ringwald         // MTU { type(8): 1, len(8):2, MTU(16) }
131763a7246aSmatthias.ringwald         if (option_type == 1 && length == 2){
1318f8fbdce0SMatthias Ringwald             channel->remote_mtu = little_endian_read_16(command, pos);
13199da54300S[email protected]             // log_info("l2cap cid 0x%02x, remote mtu %u", channel->local_cid, channel->remote_mtu);
132063a7246aSmatthias.ringwald             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU);
132163a7246aSmatthias.ringwald         }
13220fe7a9d0S[email protected]         // Flush timeout { type(8):2, len(8): 2, Flush Timeout(16)}
13230fe7a9d0S[email protected]         if (option_type == 2 && length == 2){
1324f8fbdce0SMatthias Ringwald             channel->flush_timeout = little_endian_read_16(command, pos);
13250fe7a9d0S[email protected]         }
132663a7246aSmatthias.ringwald         // check for unknown options
132763a7246aSmatthias.ringwald         if (option_hint == 0 && (option_type == 0 || option_type >= 0x07)){
1328c177a91cS[email protected]             log_info("l2cap cid %u, unknown options", channel->local_cid);
132963a7246aSmatthias.ringwald             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID);
13301dc511deSmatthias.ringwald         }
13311dc511deSmatthias.ringwald         pos += length;
13321dc511deSmatthias.ringwald     }
13332784b77dSmatthias.ringwald }
13342784b77dSmatthias.ringwald 
1335fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel){
13369da54300S[email protected]     // log_info("l2cap_channel_ready_for_open 0x%02x", channel->state_var);
133773cf2b3dSmatthias.ringwald     if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP) == 0) return 0;
133873cf2b3dSmatthias.ringwald     if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP) == 0) return 0;
1339019f9b43SMatthias Ringwald     // addition check that fixes re-entrance issue causing l2cap event channel opened twice
1340019f9b43SMatthias Ringwald     if (channel->state == L2CAP_STATE_OPEN) return 0;
1341fa8473a4Smatthias.ringwald     return 1;
1342fa8473a4Smatthias.ringwald }
1343fa8473a4Smatthias.ringwald 
1344fa8473a4Smatthias.ringwald 
13457f02f414SMatthias Ringwald static void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){
13461e6aba47Smatthias.ringwald 
134700d93d79Smatthias.ringwald     uint8_t  code       = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
134800d93d79Smatthias.ringwald     uint8_t  identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
134938e5900eSmatthias.ringwald     uint16_t result = 0;
13501e6aba47Smatthias.ringwald 
13519da54300S[email protected]     log_info("L2CAP signaling handler code %u, state %u", code, channel->state);
1352b35f641cSmatthias.ringwald 
13539a011532Smatthias.ringwald     // handle DISCONNECT REQUESTS seperately
13549a011532Smatthias.ringwald     if (code == DISCONNECTION_REQUEST){
13559a011532Smatthias.ringwald         switch (channel->state){
1356fa8473a4Smatthias.ringwald             case L2CAP_STATE_CONFIG:
13579a011532Smatthias.ringwald             case L2CAP_STATE_OPEN:
13582b83fb7dSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
13599a011532Smatthias.ringwald             case L2CAP_STATE_WAIT_DISCONNECT:
13609a011532Smatthias.ringwald                 l2cap_handle_disconnect_request(channel, identifier);
13619a011532Smatthias.ringwald                 break;
13629a011532Smatthias.ringwald 
13639a011532Smatthias.ringwald             default:
13649a011532Smatthias.ringwald                 // ignore in other states
13659a011532Smatthias.ringwald                 break;
13669a011532Smatthias.ringwald         }
13679a011532Smatthias.ringwald         return;
13689a011532Smatthias.ringwald     }
13699a011532Smatthias.ringwald 
137056081214Smatthias.ringwald     // @STATEMACHINE(l2cap)
13711e6aba47Smatthias.ringwald     switch (channel->state) {
13721e6aba47Smatthias.ringwald 
13731e6aba47Smatthias.ringwald         case L2CAP_STATE_WAIT_CONNECT_RSP:
13741e6aba47Smatthias.ringwald             switch (code){
13751e6aba47Smatthias.ringwald                 case CONNECTION_RESPONSE:
13765932bd7cS[email protected]                     l2cap_stop_rtx(channel);
1377f8fbdce0SMatthias Ringwald                     result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
137838e5900eSmatthias.ringwald                     switch (result) {
137938e5900eSmatthias.ringwald                         case 0:
1380169f8b28Smatthias.ringwald                             // successful connection
1381f8fbdce0SMatthias Ringwald                             channel->remote_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
1382fa8473a4Smatthias.ringwald                             channel->state = L2CAP_STATE_CONFIG;
138328ca2b46S[email protected]                             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
138438e5900eSmatthias.ringwald                             break;
138538e5900eSmatthias.ringwald                         case 1:
13865932bd7cS[email protected]                             // connection pending. get some coffee, but start the ERTX
13875932bd7cS[email protected]                             l2cap_start_ertx(channel);
138838e5900eSmatthias.ringwald                             break;
138938e5900eSmatthias.ringwald                         default:
1390eb920dbeSmatthias.ringwald                             // channel closed
1391eb920dbeSmatthias.ringwald                             channel->state = L2CAP_STATE_CLOSED;
1392f32b992eSmatthias.ringwald                             // map l2cap connection response result to BTstack status enumeration
139338e5900eSmatthias.ringwald                             l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result);
1394eb920dbeSmatthias.ringwald 
1395eb920dbeSmatthias.ringwald                             // drop link key if security block
1396eb920dbeSmatthias.ringwald                             if (L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY){
139715a95bd5SMatthias Ringwald                                 gap_drop_link_key_for_bd_addr(channel->address);
1398eb920dbeSmatthias.ringwald                             }
1399eb920dbeSmatthias.ringwald 
1400eb920dbeSmatthias.ringwald                             // discard channel
1401665d90f2SMatthias Ringwald                             btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
1402d3a9df87Smatthias.ringwald                             btstack_memory_l2cap_channel_free(channel);
140338e5900eSmatthias.ringwald                             break;
14041e6aba47Smatthias.ringwald                     }
14051e6aba47Smatthias.ringwald                     break;
140638e5900eSmatthias.ringwald 
140738e5900eSmatthias.ringwald                 default:
14081e6aba47Smatthias.ringwald                     //@TODO: implement other signaling packets
140938e5900eSmatthias.ringwald                     break;
14101e6aba47Smatthias.ringwald             }
14111e6aba47Smatthias.ringwald             break;
14121e6aba47Smatthias.ringwald 
1413fa8473a4Smatthias.ringwald         case L2CAP_STATE_CONFIG:
1414f8fbdce0SMatthias Ringwald             result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
1415ae280e73Smatthias.ringwald             switch (code) {
1416ae280e73Smatthias.ringwald                 case CONFIGURE_REQUEST:
141728ca2b46S[email protected]                     channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP);
1418ae280e73Smatthias.ringwald                     l2cap_signaling_handle_configure_request(channel, command);
141963a7246aSmatthias.ringwald                     if (!(channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT)){
142063a7246aSmatthias.ringwald                         // only done if continuation not set
142163a7246aSmatthias.ringwald                         channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_REQ);
142263a7246aSmatthias.ringwald                     }
1423ae280e73Smatthias.ringwald                     break;
14241e6aba47Smatthias.ringwald                 case CONFIGURE_RESPONSE:
14255932bd7cS[email protected]                     l2cap_stop_rtx(channel);
14265932bd7cS[email protected]                     switch (result){
14275932bd7cS[email protected]                         case 0: // success
14285932bd7cS[email protected]                             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP);
14295932bd7cS[email protected]                             break;
14305932bd7cS[email protected]                         case 4: // pending
14315932bd7cS[email protected]                             l2cap_start_ertx(channel);
14325932bd7cS[email protected]                             break;
14335932bd7cS[email protected]                         default:
1434fe9d8984S[email protected]                             // retry on negative result
1435fe9d8984S[email protected]                             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
1436fe9d8984S[email protected]                             break;
1437fe9d8984S[email protected]                     }
14385a67bd4aSmatthias.ringwald                     break;
14395a67bd4aSmatthias.ringwald                 default:
14405a67bd4aSmatthias.ringwald                     break;
14411e6aba47Smatthias.ringwald             }
1442fa8473a4Smatthias.ringwald             if (l2cap_channel_ready_for_open(channel)){
1443fa8473a4Smatthias.ringwald                 // for open:
14445a67bd4aSmatthias.ringwald                 channel->state = L2CAP_STATE_OPEN;
1445fa8473a4Smatthias.ringwald                 l2cap_emit_channel_opened(channel, 0);
1446c8e4258aSmatthias.ringwald             }
1447c8e4258aSmatthias.ringwald             break;
1448f62db1e3Smatthias.ringwald 
1449f62db1e3Smatthias.ringwald         case L2CAP_STATE_WAIT_DISCONNECT:
1450f62db1e3Smatthias.ringwald             switch (code) {
1451f62db1e3Smatthias.ringwald                 case DISCONNECTION_RESPONSE:
145227a923d0Smatthias.ringwald                     l2cap_finialize_channel_close(channel);
145327a923d0Smatthias.ringwald                     break;
14545a67bd4aSmatthias.ringwald                 default:
14555a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
14565a67bd4aSmatthias.ringwald                     break;
145727a923d0Smatthias.ringwald             }
145827a923d0Smatthias.ringwald             break;
145984836b65Smatthias.ringwald 
146084836b65Smatthias.ringwald         case L2CAP_STATE_CLOSED:
146184836b65Smatthias.ringwald             // @TODO handle incoming requests
146284836b65Smatthias.ringwald             break;
146384836b65Smatthias.ringwald 
146484836b65Smatthias.ringwald         case L2CAP_STATE_OPEN:
146584836b65Smatthias.ringwald             //@TODO: implement other signaling packets, e.g. re-configure
146684836b65Smatthias.ringwald             break;
146710642e45Smatthias.ringwald         default:
146810642e45Smatthias.ringwald             break;
146927a923d0Smatthias.ringwald     }
14709da54300S[email protected]     // log_info("new state %u", channel->state);
147127a923d0Smatthias.ringwald }
147227a923d0Smatthias.ringwald 
147300d93d79Smatthias.ringwald 
14747f02f414SMatthias Ringwald static void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){
147500d93d79Smatthias.ringwald 
147600d93d79Smatthias.ringwald     // get code, signalind identifier and command len
147700d93d79Smatthias.ringwald     uint8_t code   = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
147800d93d79Smatthias.ringwald     uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
147900d93d79Smatthias.ringwald 
148000d93d79Smatthias.ringwald     // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST
148100d93d79Smatthias.ringwald     if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){
148263a7246aSmatthias.ringwald         l2cap_register_signaling_response(handle, COMMAND_REJECT, sig_id, L2CAP_REJ_CMD_UNKNOWN);
148300d93d79Smatthias.ringwald         return;
148400d93d79Smatthias.ringwald     }
148500d93d79Smatthias.ringwald 
148600d93d79Smatthias.ringwald     // general commands without an assigned channel
148700d93d79Smatthias.ringwald     switch(code) {
148800d93d79Smatthias.ringwald 
148900d93d79Smatthias.ringwald         case CONNECTION_REQUEST: {
1490f8fbdce0SMatthias Ringwald             uint16_t psm =        little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
1491f8fbdce0SMatthias Ringwald             uint16_t source_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2);
149200d93d79Smatthias.ringwald             l2cap_handle_connection_request(handle, sig_id, psm, source_cid);
14932b83fb7dSmatthias.ringwald             return;
149400d93d79Smatthias.ringwald         }
149500d93d79Smatthias.ringwald 
14962b360848Smatthias.ringwald         case ECHO_REQUEST:
14972b360848Smatthias.ringwald             l2cap_register_signaling_response(handle, code, sig_id, 0);
14982b83fb7dSmatthias.ringwald             return;
149900d93d79Smatthias.ringwald 
150000d93d79Smatthias.ringwald         case INFORMATION_REQUEST: {
1501f8fbdce0SMatthias Ringwald             uint16_t infoType = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
15022b360848Smatthias.ringwald             l2cap_register_signaling_response(handle, code, sig_id, infoType);
15032b83fb7dSmatthias.ringwald             return;
150400d93d79Smatthias.ringwald         }
150500d93d79Smatthias.ringwald 
150600d93d79Smatthias.ringwald         default:
150700d93d79Smatthias.ringwald             break;
150800d93d79Smatthias.ringwald     }
150900d93d79Smatthias.ringwald 
151000d93d79Smatthias.ringwald 
151100d93d79Smatthias.ringwald     // Get potential destination CID
1512f8fbdce0SMatthias Ringwald     uint16_t dest_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
151300d93d79Smatthias.ringwald 
151400d93d79Smatthias.ringwald     // Find channel for this sig_id and connection handle
1515665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1516665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
1517665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1518665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1519fc64f94aSMatthias Ringwald         if (channel->con_handle != handle) continue;
152000d93d79Smatthias.ringwald         if (code & 1) {
1521b1988dceSmatthias.ringwald             // match odd commands (responses) by previous signaling identifier
1522b1988dceSmatthias.ringwald             if (channel->local_sig_id == sig_id) {
152300d93d79Smatthias.ringwald                 l2cap_signaling_handler_channel(channel, command);
15244e32727eSmatthias.ringwald                 break;
152500d93d79Smatthias.ringwald             }
152600d93d79Smatthias.ringwald         } else {
1527b1988dceSmatthias.ringwald             // match even commands (requests) by local channel id
152800d93d79Smatthias.ringwald             if (channel->local_cid == dest_cid) {
152900d93d79Smatthias.ringwald                 l2cap_signaling_handler_channel(channel, command);
15304e32727eSmatthias.ringwald                 break;
153100d93d79Smatthias.ringwald             }
153200d93d79Smatthias.ringwald         }
153300d93d79Smatthias.ringwald     }
153400d93d79Smatthias.ringwald }
153500d93d79Smatthias.ringwald 
1536e7d0c9aaSMatthias Ringwald #ifdef ENABLE_BLE
1537c48b2a2cSMatthias Ringwald // @returns valid
1538c48b2a2cSMatthias Ringwald static int l2cap_le_signaling_handler_dispatch(hci_con_handle_t handle, uint8_t * command, uint8_t sig_id){
1539e7d0c9aaSMatthias Ringwald     hci_connection_t * connection;
1540c48b2a2cSMatthias Ringwald     uint16_t result;
1541c48b2a2cSMatthias Ringwald     uint8_t  event[10];
1542e7d0c9aaSMatthias Ringwald     uint16_t le_psm;
1543828a7f7aSMatthias Ringwald     uint16_t local_cid;
1544e7d0c9aaSMatthias Ringwald     l2cap_service_t * service;
15451b8b8d05SMatthias Ringwald     l2cap_channel_t * channel;
15461b8b8d05SMatthias Ringwald     btstack_linked_list_iterator_t it;
154700d93d79Smatthias.ringwald 
15481b8b8d05SMatthias Ringwald     uint8_t code   = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
154923017473SMatthias Ringwald     log_info("l2cap_le_signaling_handler_dispatch: command 0x%02x, sig id %u", code, sig_id);
15501b8b8d05SMatthias Ringwald 
15511b8b8d05SMatthias Ringwald     switch (code){
155200d93d79Smatthias.ringwald 
1553c48b2a2cSMatthias Ringwald         case CONNECTION_PARAMETER_UPDATE_RESPONSE:
1554c48b2a2cSMatthias Ringwald             result = little_endian_read_16(command, 4);
1555ccf076adS[email protected]             l2cap_emit_connection_parameter_update_response(handle, result);
1556ccf076adS[email protected]             break;
1557da886c03S[email protected] 
1558c48b2a2cSMatthias Ringwald         case CONNECTION_PARAMETER_UPDATE_REQUEST:
1559e7d0c9aaSMatthias Ringwald             connection = hci_connection_for_handle(handle);
1560da886c03S[email protected]             if (connection){
15616d91fb6cSMatthias Ringwald                 if (connection->role != HCI_ROLE_MASTER){
15626d91fb6cSMatthias Ringwald                     // reject command without notifying upper layer when not in master role
1563c48b2a2cSMatthias Ringwald                     return 0;
15646d91fb6cSMatthias Ringwald                 }
1565da886c03S[email protected]                 int update_parameter = 1;
1566a4c06b28SMatthias Ringwald                 le_connection_parameter_range_t existing_range;
15674ced4e8cSMatthias Ringwald                 gap_get_connection_parameter_range(&existing_range);
1568c48b2a2cSMatthias Ringwald                 uint16_t le_conn_interval_min = little_endian_read_16(command,8);
1569c48b2a2cSMatthias Ringwald                 uint16_t le_conn_interval_max = little_endian_read_16(command,10);
1570c48b2a2cSMatthias Ringwald                 uint16_t le_conn_latency = little_endian_read_16(command,12);
1571c48b2a2cSMatthias Ringwald                 uint16_t le_supervision_timeout = little_endian_read_16(command,14);
1572da886c03S[email protected] 
1573da886c03S[email protected]                 if (le_conn_interval_min < existing_range.le_conn_interval_min) update_parameter = 0;
1574da886c03S[email protected]                 if (le_conn_interval_max > existing_range.le_conn_interval_max) update_parameter = 0;
1575da886c03S[email protected] 
1576da886c03S[email protected]                 if (le_conn_latency < existing_range.le_conn_latency_min) update_parameter = 0;
1577da886c03S[email protected]                 if (le_conn_latency > existing_range.le_conn_latency_max) update_parameter = 0;
1578da886c03S[email protected] 
1579da886c03S[email protected]                 if (le_supervision_timeout < existing_range.le_supervision_timeout_min) update_parameter = 0;
1580da886c03S[email protected]                 if (le_supervision_timeout > existing_range.le_supervision_timeout_max) update_parameter = 0;
1581da886c03S[email protected] 
1582da886c03S[email protected]                 if (update_parameter){
1583da886c03S[email protected]                     connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_SEND_RESPONSE;
1584da886c03S[email protected]                     connection->le_conn_interval_min = le_conn_interval_min;
1585da886c03S[email protected]                     connection->le_conn_interval_max = le_conn_interval_max;
1586da886c03S[email protected]                     connection->le_conn_latency = le_conn_latency;
1587da886c03S[email protected]                     connection->le_supervision_timeout = le_supervision_timeout;
1588da886c03S[email protected]                 } else {
1589da886c03S[email protected]                     connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_DENY;
1590da886c03S[email protected]                 }
1591c48b2a2cSMatthias Ringwald                 connection->le_con_param_update_identifier = sig_id;
1592da886c03S[email protected]             }
1593da886c03S[email protected] 
159433c40538SMatthias Ringwald             if (!l2cap_event_packet_handler) break;
1595c48b2a2cSMatthias Ringwald 
1596c48b2a2cSMatthias Ringwald             event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_REQUEST;
1597c48b2a2cSMatthias Ringwald             event[1] = 8;
1598c48b2a2cSMatthias Ringwald             memcpy(&event[2], &command[4], 8);
1599c48b2a2cSMatthias Ringwald             hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
160033c40538SMatthias Ringwald             (*l2cap_event_packet_handler)( HCI_EVENT_PACKET, 0, event, sizeof(event));
1601ccf076adS[email protected]             break;
1602c48b2a2cSMatthias Ringwald 
1603e7d0c9aaSMatthias Ringwald         case LE_CREDIT_BASED_CONNECTION_REQUEST:
1604e7d0c9aaSMatthias Ringwald 
1605e7d0c9aaSMatthias Ringwald             // get hci connection, bail if not found (must not happen)
1606e7d0c9aaSMatthias Ringwald             connection = hci_connection_for_handle(handle);
1607e7d0c9aaSMatthias Ringwald             if (!connection) return 0;
1608e7d0c9aaSMatthias Ringwald 
1609e7d0c9aaSMatthias Ringwald             // check if service registered
1610e7d0c9aaSMatthias Ringwald             le_psm  = little_endian_read_16(command, 4);
1611e7d0c9aaSMatthias Ringwald             service = l2cap_le_get_service(le_psm);
1612e7d0c9aaSMatthias Ringwald 
1613e7d0c9aaSMatthias Ringwald             if (service){
1614e7d0c9aaSMatthias Ringwald 
1615e7d0c9aaSMatthias Ringwald                 uint16_t source_cid = little_endian_read_16(command, 6);
1616e7d0c9aaSMatthias Ringwald                 if (source_cid < 0x40){
1617e7d0c9aaSMatthias Ringwald                     // 0x0009 Connection refused - Invalid Source CID
1618e7d0c9aaSMatthias Ringwald                     l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, 0x0009);
1619e7d0c9aaSMatthias Ringwald                     return 1;
1620e7d0c9aaSMatthias Ringwald                 }
1621e7d0c9aaSMatthias Ringwald 
1622e7d0c9aaSMatthias Ringwald                 // go through list of channels for this ACL connection and check if we get a match
1623e7d0c9aaSMatthias Ringwald                 btstack_linked_list_iterator_init(&it, &l2cap_le_channels);
1624e7d0c9aaSMatthias Ringwald                 while (btstack_linked_list_iterator_has_next(&it)){
16251b8b8d05SMatthias Ringwald                     l2cap_channel_t * a_channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
16261b8b8d05SMatthias Ringwald                     if (a_channel->con_handle != handle) continue;
16271b8b8d05SMatthias Ringwald                     if (a_channel->remote_cid != source_cid) continue;
1628e7d0c9aaSMatthias Ringwald                     // 0x000a Connection refused - Source CID already allocated
1629e7d0c9aaSMatthias Ringwald                     l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, 0x000a);
1630e7d0c9aaSMatthias Ringwald                     return 1;
1631e7d0c9aaSMatthias Ringwald                 }
1632e7d0c9aaSMatthias Ringwald 
1633e7d0c9aaSMatthias Ringwald                 // TODO: deal with authentication requirements, errors 0x005 - 000x8
1634e7d0c9aaSMatthias Ringwald 
1635e7d0c9aaSMatthias Ringwald                 // allocate channel
16361b8b8d05SMatthias Ringwald                 channel = l2cap_create_channel_entry(service->packet_handler, connection->address,
1637e7d0c9aaSMatthias Ringwald                     BD_ADDR_TYPE_LE_RANDOM, le_psm, service->mtu, service->required_security_level);
1638e7d0c9aaSMatthias Ringwald                 if (!channel){
1639e7d0c9aaSMatthias Ringwald                     // 0x0004 Connection refused – no resources available
1640e7d0c9aaSMatthias Ringwald                     l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, 0x0004);
1641e7d0c9aaSMatthias Ringwald                     return 1;
1642e7d0c9aaSMatthias Ringwald                 }
1643e7d0c9aaSMatthias Ringwald 
1644e7d0c9aaSMatthias Ringwald                 channel->con_handle = handle;
1645e7d0c9aaSMatthias Ringwald                 channel->remote_cid = source_cid;
1646e7d0c9aaSMatthias Ringwald                 channel->remote_sig_id = sig_id;
16477f107edaSMatthias Ringwald                 channel->remote_mtu = little_endian_read_16(command, 8);
16487f107edaSMatthias Ringwald                 channel->remote_mps = little_endian_read_16(command, 10);
16497f107edaSMatthias Ringwald                 channel->credits_outgoing = little_endian_read_16(command, 12);
1650e7d0c9aaSMatthias Ringwald 
1651e7d0c9aaSMatthias Ringwald                 // set initial state
1652e7d0c9aaSMatthias Ringwald                 channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT;
1653e7d0c9aaSMatthias Ringwald 
1654e7d0c9aaSMatthias Ringwald                 // add to connections list
1655e7d0c9aaSMatthias Ringwald                 btstack_linked_list_add(&l2cap_le_channels, (btstack_linked_item_t *) channel);
1656e7d0c9aaSMatthias Ringwald 
1657e7d0c9aaSMatthias Ringwald                 // post connection request event
1658e7d0c9aaSMatthias Ringwald                 l2cap_emit_connection_request(channel);
1659e7d0c9aaSMatthias Ringwald 
1660e7d0c9aaSMatthias Ringwald             } else {
1661e7d0c9aaSMatthias Ringwald                 // Connection refused – LE_PSM not supported
1662e7d0c9aaSMatthias Ringwald                 l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, 0x0002);
1663e7d0c9aaSMatthias Ringwald             }
1664e7d0c9aaSMatthias Ringwald             break;
16651b8b8d05SMatthias Ringwald 
16661b8b8d05SMatthias Ringwald         case LE_CREDIT_BASED_CONNECTION_RESPONSE:
16671b8b8d05SMatthias Ringwald             // Find channel for this sig_id and connection handle
16681b8b8d05SMatthias Ringwald             channel = NULL;
16691b8b8d05SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_le_channels);
16701b8b8d05SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
16711b8b8d05SMatthias Ringwald                 l2cap_channel_t * a_channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
16721b8b8d05SMatthias Ringwald                 if (a_channel->con_handle   != handle) continue;
16731b8b8d05SMatthias Ringwald                 if (a_channel->local_sig_id != sig_id) continue;
16741b8b8d05SMatthias Ringwald                 channel = a_channel;
16751b8b8d05SMatthias Ringwald                 break;
16761b8b8d05SMatthias Ringwald             }
16771b8b8d05SMatthias Ringwald             // TODO: send error
16781b8b8d05SMatthias Ringwald             if (!channel) break;
16791b8b8d05SMatthias Ringwald 
16801b8b8d05SMatthias Ringwald             // cid + 0
16811b8b8d05SMatthias Ringwald             result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+8);
16821b8b8d05SMatthias Ringwald             if (result){
16831b8b8d05SMatthias Ringwald                 channel->state = L2CAP_STATE_CLOSED;
16841b8b8d05SMatthias Ringwald                 // map l2cap connection response result to BTstack status enumeration
16851b8b8d05SMatthias Ringwald                 l2cap_emit_channel_opened(channel, result);
16861b8b8d05SMatthias Ringwald 
16871b8b8d05SMatthias Ringwald                 // discard channel
16881b8b8d05SMatthias Ringwald                 btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
16891b8b8d05SMatthias Ringwald                 btstack_memory_l2cap_channel_free(channel);
16901b8b8d05SMatthias Ringwald                 break;
16911b8b8d05SMatthias Ringwald             }
16921b8b8d05SMatthias Ringwald 
16931b8b8d05SMatthias Ringwald             // success
16941b8b8d05SMatthias Ringwald             channel->remote_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 0);
16951b8b8d05SMatthias Ringwald             channel->remote_mtu = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 2);
16961b8b8d05SMatthias Ringwald             channel->remote_mps = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 4);
16971b8b8d05SMatthias Ringwald             channel->credits_outgoing = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 6);
16981b8b8d05SMatthias Ringwald             channel->state = L2CAP_STATE_OPEN;
16991b8b8d05SMatthias Ringwald             l2cap_emit_channel_opened(channel, result);
17001b8b8d05SMatthias Ringwald             break;
17011b8b8d05SMatthias Ringwald 
1702828a7f7aSMatthias Ringwald         case DISCONNECTION_REQUEST:
1703828a7f7aSMatthias Ringwald             // find channel
1704828a7f7aSMatthias Ringwald             local_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 0);
1705828a7f7aSMatthias Ringwald             channel = l2cap_le_get_channel_for_local_cid(local_cid);
1706828a7f7aSMatthias Ringwald             if (!channel) break;
1707828a7f7aSMatthias Ringwald             channel->remote_sig_id = sig_id;
1708828a7f7aSMatthias Ringwald             channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE;
1709828a7f7aSMatthias Ringwald             break;
1710828a7f7aSMatthias Ringwald 
1711c48b2a2cSMatthias Ringwald         default:
1712c48b2a2cSMatthias Ringwald             // command unknown -> reject command
1713c48b2a2cSMatthias Ringwald             return 0;
1714ccf076adS[email protected]     }
1715c48b2a2cSMatthias Ringwald     return 1;
1716c48b2a2cSMatthias Ringwald }
1717e7d0c9aaSMatthias Ringwald #endif
1718c48b2a2cSMatthias Ringwald 
1719c48b2a2cSMatthias Ringwald static void l2cap_acl_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size ){
1720c48b2a2cSMatthias Ringwald 
1721c48b2a2cSMatthias Ringwald     // Get Channel ID
1722c48b2a2cSMatthias Ringwald     uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet);
1723c48b2a2cSMatthias Ringwald     hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet);
1724c48b2a2cSMatthias Ringwald 
1725c48b2a2cSMatthias Ringwald     switch (channel_id) {
1726c48b2a2cSMatthias Ringwald 
1727c48b2a2cSMatthias Ringwald         case L2CAP_CID_SIGNALING: {
1728c48b2a2cSMatthias Ringwald             uint16_t command_offset = 8;
1729c48b2a2cSMatthias Ringwald             while (command_offset < size) {
1730c48b2a2cSMatthias Ringwald                 // handle signaling commands
1731c48b2a2cSMatthias Ringwald                 l2cap_signaling_handler_dispatch(handle, &packet[command_offset]);
1732c48b2a2cSMatthias Ringwald 
1733c48b2a2cSMatthias Ringwald                 // increment command_offset
1734c48b2a2cSMatthias Ringwald                 command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + little_endian_read_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
1735c48b2a2cSMatthias Ringwald             }
1736c48b2a2cSMatthias Ringwald             break;
1737c48b2a2cSMatthias Ringwald         }
1738c48b2a2cSMatthias Ringwald 
1739e7d0c9aaSMatthias Ringwald #ifdef ENABLE_BLE
1740e7d0c9aaSMatthias Ringwald         case L2CAP_CID_SIGNALING_LE: {
1741e7d0c9aaSMatthias Ringwald             uint16_t sig_id = packet[COMPLETE_L2CAP_HEADER + 1];
1742e7d0c9aaSMatthias Ringwald             int      valid  = l2cap_le_signaling_handler_dispatch(handle, &packet[COMPLETE_L2CAP_HEADER], sig_id);
1743c48b2a2cSMatthias Ringwald             if (!valid){
17441bbc0b23S[email protected]                 l2cap_register_signaling_response(handle, COMMAND_REJECT_LE, sig_id, L2CAP_REJ_CMD_UNKNOWN);
1745ccf076adS[email protected]             }
1746ccf076adS[email protected]             break;
1747e7d0c9aaSMatthias Ringwald         }
1748e7d0c9aaSMatthias Ringwald #endif
1749c48b2a2cSMatthias Ringwald 
1750c48b2a2cSMatthias Ringwald         case L2CAP_CID_ATTRIBUTE_PROTOCOL:
1751c48b2a2cSMatthias Ringwald             if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL].callback) {
1752c48b2a2cSMatthias Ringwald                 (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL].callback)(ATT_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
1753ccf076adS[email protected]             }
1754c48b2a2cSMatthias Ringwald             break;
1755c48b2a2cSMatthias Ringwald 
1756c48b2a2cSMatthias Ringwald         case L2CAP_CID_SECURITY_MANAGER_PROTOCOL:
1757c48b2a2cSMatthias Ringwald             if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL].callback) {
1758c48b2a2cSMatthias Ringwald                 (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL].callback)(SM_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
1759c48b2a2cSMatthias Ringwald             }
1760c48b2a2cSMatthias Ringwald             break;
1761c48b2a2cSMatthias Ringwald 
1762c48b2a2cSMatthias Ringwald         case L2CAP_CID_CONNECTIONLESS_CHANNEL:
1763c48b2a2cSMatthias Ringwald             if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL].callback) {
1764c48b2a2cSMatthias Ringwald                 (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL].callback)(UCD_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
1765c48b2a2cSMatthias Ringwald             }
1766c48b2a2cSMatthias Ringwald             break;
17671bbc0b23S[email protected] 
17685652b5ffS[email protected]         default: {
176900d93d79Smatthias.ringwald             // Find channel for this channel_id and connection handle
177064e11ca9SMatthias Ringwald             l2cap_channel_t * l2cap_channel;
177164e11ca9SMatthias Ringwald             l2cap_channel = l2cap_get_channel_for_local_cid(channel_id);
1772d1fd2a88SMatthias Ringwald             if (l2cap_channel) {
17733d50b4baSMatthias Ringwald                 l2cap_dispatch_to_channel(l2cap_channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
177400d93d79Smatthias.ringwald             }
177564e11ca9SMatthias Ringwald #ifdef ENABLE_BLE
177664e11ca9SMatthias Ringwald             l2cap_channel = l2cap_le_get_channel_for_local_cid(channel_id);
177764e11ca9SMatthias Ringwald             if (l2cap_channel) {
1778*cd529728SMatthias Ringwald                 // first fragment
1779*cd529728SMatthias Ringwald                 uint16_t pos = 0;
1780*cd529728SMatthias Ringwald                 if (!l2cap_channel->receive_sdu_len){
1781*cd529728SMatthias Ringwald                     l2cap_channel->receive_sdu_len = little_endian_read_16(packet, COMPLETE_L2CAP_HEADER);
1782*cd529728SMatthias Ringwald                     l2cap_channel->receive_sdu_pos = 0;
1783*cd529728SMatthias Ringwald                     pos  += 2;
1784*cd529728SMatthias Ringwald                     size -= 2;
1785*cd529728SMatthias Ringwald                 }
1786*cd529728SMatthias Ringwald                 memcpy(&l2cap_channel->receive_sdu_buffer[l2cap_channel->receive_sdu_pos], &packet[COMPLETE_L2CAP_HEADER+pos], size-COMPLETE_L2CAP_HEADER);
1787*cd529728SMatthias Ringwald                 l2cap_channel->receive_sdu_pos += size - COMPLETE_L2CAP_HEADER;
1788*cd529728SMatthias Ringwald                 // done?
1789*cd529728SMatthias Ringwald                 log_debug("le packet pos %u, len %u", l2cap_channel->receive_sdu_pos, l2cap_channel->receive_sdu_len);
1790*cd529728SMatthias Ringwald                 if (l2cap_channel->receive_sdu_pos >= l2cap_channel->receive_sdu_len){
1791*cd529728SMatthias Ringwald                     l2cap_dispatch_to_channel(l2cap_channel, L2CAP_DATA_PACKET, l2cap_channel->receive_sdu_buffer, l2cap_channel->receive_sdu_len);
1792*cd529728SMatthias Ringwald                     l2cap_channel->receive_sdu_len = 0;
1793*cd529728SMatthias Ringwald                 }
179464e11ca9SMatthias Ringwald             }
179564e11ca9SMatthias Ringwald #endif
17965652b5ffS[email protected]             break;
17975652b5ffS[email protected]         }
17985652b5ffS[email protected]     }
179900d93d79Smatthias.ringwald 
18001eb2563eS[email protected]     l2cap_run();
18012718e2e7Smatthias.ringwald }
180200d93d79Smatthias.ringwald 
180315ec09bbSmatthias.ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE
180427a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t * channel){
1805f62db1e3Smatthias.ringwald     channel->state = L2CAP_STATE_CLOSED;
1806f62db1e3Smatthias.ringwald     l2cap_emit_channel_closed(channel);
1807f62db1e3Smatthias.ringwald     // discard channel
18089dcb2fb2S[email protected]     l2cap_stop_rtx(channel);
1809665d90f2SMatthias Ringwald     btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
1810d3a9df87Smatthias.ringwald     btstack_memory_l2cap_channel_free(channel);
1811c8e4258aSmatthias.ringwald }
18121e6aba47Smatthias.ringwald 
1813828a7f7aSMatthias Ringwald #ifdef ENABLE_BLE
1814828a7f7aSMatthias Ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE
1815828a7f7aSMatthias Ringwald void l2cap_le_finialize_channel_close(l2cap_channel_t * channel){
1816828a7f7aSMatthias Ringwald     channel->state = L2CAP_STATE_CLOSED;
1817828a7f7aSMatthias Ringwald     l2cap_emit_channel_closed(channel);
1818828a7f7aSMatthias Ringwald     // discard channel
1819828a7f7aSMatthias Ringwald     l2cap_stop_rtx(channel);
1820828a7f7aSMatthias Ringwald     btstack_linked_list_remove(&l2cap_le_channels, (btstack_linked_item_t *) channel);
1821828a7f7aSMatthias Ringwald     btstack_memory_l2cap_channel_free(channel);
1822828a7f7aSMatthias Ringwald }
1823828a7f7aSMatthias Ringwald #endif
1824828a7f7aSMatthias Ringwald 
18258f2a52f4SMatthias Ringwald static l2cap_service_t * l2cap_get_service_internal(btstack_linked_list_t * services, uint16_t psm){
1826665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1827665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, services);
1828665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1829665d90f2SMatthias Ringwald         l2cap_service_t * service = (l2cap_service_t *) btstack_linked_list_iterator_next(&it);
18309d9bbc01Smatthias.ringwald         if ( service->psm == psm){
18319d9bbc01Smatthias.ringwald             return service;
18329d9bbc01Smatthias.ringwald         };
18339d9bbc01Smatthias.ringwald     }
18349d9bbc01Smatthias.ringwald     return NULL;
18359d9bbc01Smatthias.ringwald }
18369d9bbc01Smatthias.ringwald 
18377192e786SMatthias Ringwald static inline l2cap_service_t * l2cap_get_service(uint16_t psm){
18387192e786SMatthias Ringwald     return l2cap_get_service_internal(&l2cap_services, psm);
18397192e786SMatthias Ringwald }
18407192e786SMatthias Ringwald 
1841e0abb8e7S[email protected] 
1842be2053a6SMatthias Ringwald uint8_t l2cap_register_service(btstack_packet_handler_t service_packet_handler, uint16_t psm, uint16_t mtu, gap_security_level_t security_level){
1843be2053a6SMatthias Ringwald 
1844be2053a6SMatthias Ringwald     log_info("L2CAP_REGISTER_SERVICE psm 0x%x mtu %u", psm, mtu);
1845e0abb8e7S[email protected] 
18464bb582b6Smatthias.ringwald     // check for alread registered psm
18479d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
1848277abc2cSmatthias.ringwald     if (service) {
1849be2053a6SMatthias Ringwald         log_error("l2cap_register_service: PSM %u already registered", psm);
1850be2053a6SMatthias Ringwald         return L2CAP_SERVICE_ALREADY_REGISTERED;
1851277abc2cSmatthias.ringwald     }
18529d9bbc01Smatthias.ringwald 
18534bb582b6Smatthias.ringwald     // alloc structure
1854bb69aaaeS[email protected]     service = btstack_memory_l2cap_service_get();
1855277abc2cSmatthias.ringwald     if (!service) {
1856be2053a6SMatthias Ringwald         log_error("l2cap_register_service: no memory for l2cap_service_t");
1857be2053a6SMatthias Ringwald         return BTSTACK_MEMORY_ALLOC_FAILED;
1858277abc2cSmatthias.ringwald     }
18599d9bbc01Smatthias.ringwald 
18609d9bbc01Smatthias.ringwald     // fill in
18619d9bbc01Smatthias.ringwald     service->psm = psm;
18629d9bbc01Smatthias.ringwald     service->mtu = mtu;
186305ae8de3SMatthias Ringwald     service->packet_handler = service_packet_handler;
1864df3354fcS[email protected]     service->required_security_level = security_level;
18659d9bbc01Smatthias.ringwald 
18669d9bbc01Smatthias.ringwald     // add to services list
1867665d90f2SMatthias Ringwald     btstack_linked_list_add(&l2cap_services, (btstack_linked_item_t *) service);
1868c0e866bfSmatthias.ringwald 
1869c0e866bfSmatthias.ringwald     // enable page scan
187015a95bd5SMatthias Ringwald     gap_connectable_control(1);
18715842b6d9Smatthias.ringwald 
1872be2053a6SMatthias Ringwald     return 0;
18739d9bbc01Smatthias.ringwald }
18749d9bbc01Smatthias.ringwald 
18757e8856ebSMatthias Ringwald uint8_t l2cap_unregister_service(uint16_t psm){
1876e0abb8e7S[email protected] 
1877e0abb8e7S[email protected]     log_info("L2CAP_UNREGISTER_SERVICE psm 0x%x", psm);
1878e0abb8e7S[email protected] 
18799d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
18807e8856ebSMatthias Ringwald     if (!service) return L2CAP_SERVICE_DOES_NOT_EXIST;
1881665d90f2SMatthias Ringwald     btstack_linked_list_remove(&l2cap_services, (btstack_linked_item_t *) service);
1882d3a9df87Smatthias.ringwald     btstack_memory_l2cap_service_free(service);
1883c0e866bfSmatthias.ringwald 
1884c0e866bfSmatthias.ringwald     // disable page scan when no services registered
18857e8856ebSMatthias Ringwald     if (btstack_linked_list_empty(&l2cap_services)) {
188615a95bd5SMatthias Ringwald         gap_connectable_control(0);
18879d9bbc01Smatthias.ringwald     }
18887e8856ebSMatthias Ringwald     return 0;
18897e8856ebSMatthias Ringwald }
18909d9bbc01Smatthias.ringwald 
18915652b5ffS[email protected] // Bluetooth 4.0 - allows to register handler for Attribute Protocol and Security Manager Protocol
189205ae8de3SMatthias Ringwald void l2cap_register_fixed_channel(btstack_packet_handler_t the_packet_handler, uint16_t channel_id) {
18935628cf69SMatthias Ringwald     int index = l2cap_fixed_channel_table_index_for_channel_id(channel_id);
18945628cf69SMatthias Ringwald     if (index < 0) return;
18955628cf69SMatthias Ringwald     fixed_channels[index].callback = the_packet_handler;
18965652b5ffS[email protected] }
18975652b5ffS[email protected] 
1898a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
18997192e786SMatthias Ringwald 
1900e7d0c9aaSMatthias Ringwald static inline l2cap_service_t * l2cap_le_get_service(uint16_t le_psm){
1901e7d0c9aaSMatthias Ringwald     return l2cap_get_service_internal(&l2cap_le_services, le_psm);
19027192e786SMatthias Ringwald }
1903efedfb4cSMatthias Ringwald 
1904da144af5SMatthias Ringwald uint8_t l2cap_le_register_service(btstack_packet_handler_t packet_handler, uint16_t psm, gap_security_level_t security_level){
19057192e786SMatthias Ringwald 
1906da144af5SMatthias Ringwald     log_info("L2CAP_LE_REGISTER_SERVICE psm 0x%x", psm);
19077192e786SMatthias Ringwald 
19087192e786SMatthias Ringwald     // check for alread registered psm
19097192e786SMatthias Ringwald     l2cap_service_t *service = l2cap_le_get_service(psm);
19107192e786SMatthias Ringwald     if (service) {
1911da144af5SMatthias Ringwald         return L2CAP_SERVICE_ALREADY_REGISTERED;
19127192e786SMatthias Ringwald     }
19137192e786SMatthias Ringwald 
19147192e786SMatthias Ringwald     // alloc structure
19157192e786SMatthias Ringwald     service = btstack_memory_l2cap_service_get();
19167192e786SMatthias Ringwald     if (!service) {
19177192e786SMatthias Ringwald         log_error("l2cap_register_service_internal: no memory for l2cap_service_t");
1918da144af5SMatthias Ringwald         return BTSTACK_MEMORY_ALLOC_FAILED;
19197192e786SMatthias Ringwald     }
19207192e786SMatthias Ringwald 
19217192e786SMatthias Ringwald     // fill in
19227192e786SMatthias Ringwald     service->psm = psm;
1923da144af5SMatthias Ringwald     service->mtu = 0;
19247192e786SMatthias Ringwald     service->packet_handler = packet_handler;
19257192e786SMatthias Ringwald     service->required_security_level = security_level;
19267192e786SMatthias Ringwald 
19277192e786SMatthias Ringwald     // add to services list
1928665d90f2SMatthias Ringwald     btstack_linked_list_add(&l2cap_le_services, (btstack_linked_item_t *) service);
19297192e786SMatthias Ringwald 
19307192e786SMatthias Ringwald     // done
1931da144af5SMatthias Ringwald     return 0;
19327192e786SMatthias Ringwald }
19337192e786SMatthias Ringwald 
1934da144af5SMatthias Ringwald uint8_t l2cap_le_unregister_service(uint16_t psm) {
19357192e786SMatthias Ringwald     log_info("L2CAP_LE_UNREGISTER_SERVICE psm 0x%x", psm);
19367192e786SMatthias Ringwald     l2cap_service_t *service = l2cap_le_get_service(psm);
19379367f9b0SMatthias Ringwald     if (!service) return L2CAP_SERVICE_DOES_NOT_EXIST;
1938da144af5SMatthias Ringwald 
1939665d90f2SMatthias Ringwald     btstack_linked_list_remove(&l2cap_le_services, (btstack_linked_item_t *) service);
19407192e786SMatthias Ringwald     btstack_memory_l2cap_service_free(service);
1941da144af5SMatthias Ringwald     return 0;
19427192e786SMatthias Ringwald }
1943da144af5SMatthias Ringwald 
1944da144af5SMatthias Ringwald uint8_t l2cap_le_accept_connection(uint16_t local_cid, uint8_t * receive_sdu_buffer, uint16_t mtu, uint16_t initial_credits){
1945e7d0c9aaSMatthias Ringwald     // get channel
1946e7d0c9aaSMatthias Ringwald     l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid);
1947e7d0c9aaSMatthias Ringwald     if (!channel) return L2CAP_LOCAL_CID_DOES_NOT_EXIST;
1948e7d0c9aaSMatthias Ringwald 
1949e7d0c9aaSMatthias Ringwald     // validate state
1950e7d0c9aaSMatthias Ringwald     if (channel->state != L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT){
1951e7d0c9aaSMatthias Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1952e7d0c9aaSMatthias Ringwald     }
1953e7d0c9aaSMatthias Ringwald 
1954efedfb4cSMatthias Ringwald     // set state accept connection
195523017473SMatthias Ringwald     channel->state = L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_ACCEPT;
195623017473SMatthias Ringwald     channel->receive_sdu_buffer = receive_sdu_buffer;
195723017473SMatthias Ringwald     channel->local_mtu = mtu;
195823017473SMatthias Ringwald     channel->credits_incoming = initial_credits;
1959e7d0c9aaSMatthias Ringwald 
1960e7d0c9aaSMatthias Ringwald     // go
1961e7d0c9aaSMatthias Ringwald     l2cap_run();
1962da144af5SMatthias Ringwald     return 0;
1963da144af5SMatthias Ringwald }
1964da144af5SMatthias Ringwald 
1965da144af5SMatthias Ringwald /**
1966da144af5SMatthias Ringwald  * @brief Deny incoming LE Data Channel connection due to resource constraints
1967da144af5SMatthias Ringwald  * @param local_cid             L2CAP LE Data Channel Identifier
1968da144af5SMatthias Ringwald  */
1969da144af5SMatthias Ringwald 
1970da144af5SMatthias Ringwald uint8_t l2cap_le_decline_connection(uint16_t local_cid){
1971e7d0c9aaSMatthias Ringwald     // get channel
1972e7d0c9aaSMatthias Ringwald     l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid);
1973e7d0c9aaSMatthias Ringwald     if (!channel) return L2CAP_LOCAL_CID_DOES_NOT_EXIST;
1974e7d0c9aaSMatthias Ringwald 
1975e7d0c9aaSMatthias Ringwald     // validate state
1976e7d0c9aaSMatthias Ringwald     if (channel->state != L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT){
1977e7d0c9aaSMatthias Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1978e7d0c9aaSMatthias Ringwald     }
1979e7d0c9aaSMatthias Ringwald 
1980efedfb4cSMatthias Ringwald     // set state decline connection
1981e7d0c9aaSMatthias Ringwald     channel->state  = L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_DECLINE;
1982e7d0c9aaSMatthias Ringwald     channel->reason = 0x04; // no resources available
1983e7d0c9aaSMatthias Ringwald     l2cap_run();
1984da144af5SMatthias Ringwald     return 0;
1985da144af5SMatthias Ringwald }
1986da144af5SMatthias Ringwald 
1987da144af5SMatthias Ringwald uint8_t l2cap_le_create_channel(btstack_packet_handler_t packet_handler, bd_addr_t address, bd_addr_type_t address_type,
1988da144af5SMatthias Ringwald     uint16_t psm, uint8_t * receive_sdu_buffer, uint16_t mtu, uint16_t initial_credits, gap_security_level_t security_level,
1989efedfb4cSMatthias Ringwald     uint16_t * out_local_cid) {
1990efedfb4cSMatthias Ringwald 
1991da144af5SMatthias Ringwald     log_info("L2CAP_LE_CREATE_CHANNEL addr %s psm 0x%x mtu %u", bd_addr_to_str(address), psm, mtu);
1992da144af5SMatthias Ringwald 
1993*cd529728SMatthias Ringwald     l2cap_channel_t * channel = l2cap_create_channel_entry(packet_handler, address, address_type, psm, mtu, security_level);
1994da144af5SMatthias Ringwald     if (!channel) {
1995da144af5SMatthias Ringwald         return BTSTACK_MEMORY_ALLOC_FAILED;
1996da144af5SMatthias Ringwald     }
1997e7d0c9aaSMatthias Ringwald     log_info("l2cap_le_create_channel %p", channel);
1998da144af5SMatthias Ringwald 
1999da144af5SMatthias Ringwald     // store local_cid
2000da144af5SMatthias Ringwald     if (out_local_cid){
2001da144af5SMatthias Ringwald        *out_local_cid = channel->local_cid;
2002da144af5SMatthias Ringwald     }
2003da144af5SMatthias Ringwald 
2004*cd529728SMatthias Ringwald     // provdide buffer
2005*cd529728SMatthias Ringwald     channel->receive_sdu_buffer = receive_sdu_buffer;
2006e7d0c9aaSMatthias Ringwald     channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE;
2007e7d0c9aaSMatthias Ringwald 
2008efedfb4cSMatthias Ringwald     // add to connections list
2009efedfb4cSMatthias Ringwald     btstack_linked_list_add(&l2cap_le_channels, (btstack_linked_item_t *) channel);
2010efedfb4cSMatthias Ringwald 
2011da144af5SMatthias Ringwald     // check if hci connection is already usable
2012da144af5SMatthias Ringwald     hci_connection_t * conn = hci_connection_for_bd_addr_and_type(address, address_type);
2013efedfb4cSMatthias Ringwald 
2014da144af5SMatthias Ringwald     if (conn){
2015da144af5SMatthias Ringwald         log_info("l2cap_le_create_channel, hci connection already exists");
20165cb87675SMatthias Ringwald         l2cap_handle_le_connection_complete(channel, 0, conn->con_handle);
2017efedfb4cSMatthias Ringwald     } else {
2018efedfb4cSMatthias Ringwald 
2019efedfb4cSMatthias Ringwald         //
2020efedfb4cSMatthias Ringwald         // TODO: start timer
2021efedfb4cSMatthias Ringwald         // ..
2022efedfb4cSMatthias Ringwald 
2023efedfb4cSMatthias Ringwald         // start connection
2024efedfb4cSMatthias Ringwald         uint8_t res = gap_auto_connection_start(address_type, address);
202548af0c56SMatthias Ringwald         if (res){
2026efedfb4cSMatthias Ringwald             // discard channel object
2027e7d0c9aaSMatthias Ringwald             log_info("gap_auto_connection_start failed! 0x%02x", res);
2028e7d0c9aaSMatthias Ringwald             btstack_linked_list_remove(&l2cap_le_channels, (btstack_linked_item_t *) channel);
2029efedfb4cSMatthias Ringwald             btstack_memory_l2cap_channel_free(channel);
2030efedfb4cSMatthias Ringwald             return res;
2031da144af5SMatthias Ringwald         }
2032efedfb4cSMatthias Ringwald     }
2033da144af5SMatthias Ringwald     return 0;
2034da144af5SMatthias Ringwald }
2035da144af5SMatthias Ringwald 
2036da144af5SMatthias Ringwald /**
2037da144af5SMatthias Ringwald  * @brief Provide credtis for LE Data Channel
2038da144af5SMatthias Ringwald  * @param local_cid             L2CAP LE Data Channel Identifier
2039da144af5SMatthias Ringwald  * @param credits               Number additional credits for peer
2040da144af5SMatthias Ringwald  */
204164e11ca9SMatthias Ringwald uint8_t l2cap_le_provide_credits(uint16_t local_cid, uint16_t credits){
2042e7d0c9aaSMatthias Ringwald     // get channel
2043efedfb4cSMatthias Ringwald     // bail if missing
2044efedfb4cSMatthias Ringwald     // check state
2045efedfb4cSMatthias Ringwald     // check incoming credits + credits <= 0xffff
2046efedfb4cSMatthias Ringwald     // set credits_granted
2047efedfb4cSMatthias Ringwald     // l2cap_le_run()
2048da144af5SMatthias Ringwald     return 0;
2049da144af5SMatthias Ringwald }
2050da144af5SMatthias Ringwald 
2051da144af5SMatthias Ringwald /**
2052da144af5SMatthias Ringwald  * @brief Check if outgoing buffer is available and that there's space on the Bluetooth module
2053da144af5SMatthias Ringwald  * @param local_cid             L2CAP LE Data Channel Identifier
2054da144af5SMatthias Ringwald  */
205564e11ca9SMatthias Ringwald int l2cap_le_can_send_now(uint16_t local_cid){
2056efedfb4cSMatthias Ringwald     // check if data can be sent via le at all
2057da144af5SMatthias Ringwald     return 0;
2058da144af5SMatthias Ringwald }
2059da144af5SMatthias Ringwald 
2060da144af5SMatthias Ringwald /**
2061da144af5SMatthias Ringwald  * @brief Request emission of L2CAP_EVENT_CAN_SEND_NOW as soon as possible
2062da144af5SMatthias Ringwald  * @note L2CAP_EVENT_CAN_SEND_NOW might be emitted during call to this function
2063da144af5SMatthias Ringwald  *       so packet handler should be ready to handle it
2064da144af5SMatthias Ringwald  * @param local_cid             L2CAP LE Data Channel Identifier
2065da144af5SMatthias Ringwald  */
206664e11ca9SMatthias Ringwald uint8_t l2cap_le_request_can_send_now_event(uint16_t local_cid){
2067efedfb4cSMatthias Ringwald     // same as for non-le
2068da144af5SMatthias Ringwald     return 0;
2069da144af5SMatthias Ringwald }
2070da144af5SMatthias Ringwald 
2071da144af5SMatthias Ringwald /**
2072da144af5SMatthias Ringwald  * @brief Send data via LE Data Channel
2073da144af5SMatthias Ringwald  * @note Since data larger then the maximum PDU needs to be segmented into multiple PDUs, data needs to stay valid until ... event
2074da144af5SMatthias Ringwald  * @param local_cid             L2CAP LE Data Channel Identifier
2075da144af5SMatthias Ringwald  * @param data                  data to send
2076da144af5SMatthias Ringwald  * @param size                  data size
2077da144af5SMatthias Ringwald  */
207864e11ca9SMatthias Ringwald uint8_t l2cap_le_send_data(uint16_t local_cid, uint8_t * data, uint16_t len){
207964e11ca9SMatthias Ringwald 
208064e11ca9SMatthias Ringwald     l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid);
208164e11ca9SMatthias Ringwald     if (!channel) {
208264e11ca9SMatthias Ringwald         log_error("l2cap_send no channel for cid 0x%02x", local_cid);
2083828a7f7aSMatthias Ringwald         return L2CAP_LOCAL_CID_DOES_NOT_EXIST;
208464e11ca9SMatthias Ringwald     }
208564e11ca9SMatthias Ringwald 
208664e11ca9SMatthias Ringwald     if (len > channel->remote_mtu){
208764e11ca9SMatthias Ringwald         log_error("l2cap_send cid 0x%02x, data length exceeds remote MTU.", local_cid);
208864e11ca9SMatthias Ringwald         return L2CAP_DATA_LEN_EXCEEDS_REMOTE_MTU;
208964e11ca9SMatthias Ringwald     }
209064e11ca9SMatthias Ringwald 
20917f107edaSMatthias Ringwald     if (channel->send_sdu_buffer){
209264e11ca9SMatthias Ringwald         log_info("l2cap_send cid 0x%02x, cannot send", local_cid);
209364e11ca9SMatthias Ringwald         return BTSTACK_ACL_BUFFERS_FULL;
209464e11ca9SMatthias Ringwald     }
209564e11ca9SMatthias Ringwald 
20967f107edaSMatthias Ringwald     channel->send_sdu_buffer = data;
20977f107edaSMatthias Ringwald     channel->send_sdu_len    = len;
20987f107edaSMatthias Ringwald     channel->send_sdu_pos    = 0;
209964e11ca9SMatthias Ringwald 
21007f107edaSMatthias Ringwald     l2cap_run();
21017f107edaSMatthias Ringwald     return 0;
2102da144af5SMatthias Ringwald }
2103da144af5SMatthias Ringwald 
2104da144af5SMatthias Ringwald /**
2105da144af5SMatthias Ringwald  * @brief Disconnect from LE Data Channel
2106da144af5SMatthias Ringwald  * @param local_cid             L2CAP LE Data Channel Identifier
2107da144af5SMatthias Ringwald  */
2108828a7f7aSMatthias Ringwald uint8_t l2cap_le_disconnect(uint16_t local_cid)
2109da144af5SMatthias Ringwald {
2110828a7f7aSMatthias Ringwald     l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid);
2111828a7f7aSMatthias Ringwald     if (!channel) {
2112828a7f7aSMatthias Ringwald         log_error("l2cap_send no channel for cid 0x%02x", local_cid);
2113828a7f7aSMatthias Ringwald         return L2CAP_LOCAL_CID_DOES_NOT_EXIST;
2114828a7f7aSMatthias Ringwald     }
2115828a7f7aSMatthias Ringwald 
2116828a7f7aSMatthias Ringwald     channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
2117828a7f7aSMatthias Ringwald     l2cap_run();
2118da144af5SMatthias Ringwald     return 0;
2119da144af5SMatthias Ringwald }
2120da144af5SMatthias Ringwald 
21217f02f414SMatthias Ringwald #endif
2122