xref: /btstack/src/l2cap.c (revision 2b3c6c9bc6816fe3face68f8cb63c60732c45041)
143625864Smatthias.ringwald /*
21713bceaSmatthias.ringwald  * Copyright (C) 2009 by Matthias Ringwald
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.
161713bceaSmatthias.ringwald  *
171713bceaSmatthias.ringwald  * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS
181713bceaSmatthias.ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
191713bceaSmatthias.ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
201713bceaSmatthias.ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
211713bceaSmatthias.ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
221713bceaSmatthias.ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
231713bceaSmatthias.ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
241713bceaSmatthias.ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
251713bceaSmatthias.ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
261713bceaSmatthias.ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
271713bceaSmatthias.ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
281713bceaSmatthias.ringwald  * SUCH DAMAGE.
291713bceaSmatthias.ringwald  *
301713bceaSmatthias.ringwald  */
311713bceaSmatthias.ringwald 
321713bceaSmatthias.ringwald /*
3343625864Smatthias.ringwald  *  l2cap.c
3443625864Smatthias.ringwald  *
3543625864Smatthias.ringwald  *  Logical Link Control and Adaption Protocl (L2CAP)
3643625864Smatthias.ringwald  *
3743625864Smatthias.ringwald  *  Created by Matthias Ringwald on 5/16/09.
3843625864Smatthias.ringwald  */
3943625864Smatthias.ringwald 
4043625864Smatthias.ringwald #include "l2cap.h"
41645658c9Smatthias.ringwald #include "hci.h"
42*2b3c6c9bSmatthias.ringwald #include "hci_dump.h"
4343625864Smatthias.ringwald 
4443625864Smatthias.ringwald #include <stdarg.h>
4543625864Smatthias.ringwald #include <string.h>
4643625864Smatthias.ringwald 
4743625864Smatthias.ringwald #include <stdio.h>
4843625864Smatthias.ringwald 
496f60b3f4Smatthias.ringwald // size of HCI ACL + L2CAP Header for regular data packets
506f60b3f4Smatthias.ringwald #define COMPLETE_L2CAP_HEADER 8
516f60b3f4Smatthias.ringwald 
52fcadd0caSmatthias.ringwald static void null_event_handler(uint8_t *packet, uint16_t size);
53fcadd0caSmatthias.ringwald static void null_data_handler(uint16_t source_cid, uint8_t *packet, uint16_t size);
54fcadd0caSmatthias.ringwald 
551e6aba47Smatthias.ringwald static uint8_t * sig_buffer = NULL;
561e6aba47Smatthias.ringwald static linked_list_t l2cap_channels = NULL;
579d9bbc01Smatthias.ringwald static linked_list_t l2cap_services = NULL;
581e6aba47Smatthias.ringwald static uint8_t * acl_buffer = NULL;
59fcadd0caSmatthias.ringwald static void (*event_packet_handler) (uint8_t *packet, uint16_t size) = null_event_handler;
60fcadd0caSmatthias.ringwald static void (*data_packet_handler)  (uint16_t source_cid, uint8_t *packet, uint16_t size) = null_data_handler;
619edc8742Smatthias.ringwald static connection_t * capture_connection = NULL;
621e6aba47Smatthias.ringwald 
63169f8b28Smatthias.ringwald static uint8_t config_options[] = { 1, 2, 150, 0}; // mtu = 48
64169f8b28Smatthias.ringwald 
651e6aba47Smatthias.ringwald void l2cap_init(){
661e6aba47Smatthias.ringwald     sig_buffer = malloc( 48 );
671e6aba47Smatthias.ringwald     acl_buffer = malloc( 255 + 8 );
68fcadd0caSmatthias.ringwald 
69fcadd0caSmatthias.ringwald     //
70fcadd0caSmatthias.ringwald     // register callbacks with HCI
71fcadd0caSmatthias.ringwald     //
72fcadd0caSmatthias.ringwald     hci_register_event_packet_handler(&l2cap_event_handler);
73fcadd0caSmatthias.ringwald     hci_register_acl_packet_handler(&l2cap_acl_handler);
74fcadd0caSmatthias.ringwald }
75fcadd0caSmatthias.ringwald 
76fcadd0caSmatthias.ringwald 
77fcadd0caSmatthias.ringwald /** Register L2CAP packet handlers */
78fcadd0caSmatthias.ringwald static void null_event_handler(uint8_t *packet, uint16_t size){
79fcadd0caSmatthias.ringwald }
80fcadd0caSmatthias.ringwald static void null_data_handler(uint16_t  source_cid, uint8_t *packet, uint16_t size){
81fcadd0caSmatthias.ringwald }
82fcadd0caSmatthias.ringwald void l2cap_register_event_packet_handler(void (*handler)(uint8_t *packet, uint16_t size)){
83fcadd0caSmatthias.ringwald     event_packet_handler = handler;
84fcadd0caSmatthias.ringwald }
85fcadd0caSmatthias.ringwald void l2cap_register_data_packet_handler  (void (*handler)(uint16_t source_cid, uint8_t *packet, uint16_t size)){
86fcadd0caSmatthias.ringwald     data_packet_handler = handler;
871e6aba47Smatthias.ringwald }
881e6aba47Smatthias.ringwald 
890af41d30Smatthias.ringwald int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){
900af41d30Smatthias.ringwald     va_list argptr;
910af41d30Smatthias.ringwald     va_start(argptr, identifier);
920af41d30Smatthias.ringwald     uint16_t len = l2cap_create_signaling_internal(sig_buffer, handle, cmd, identifier, argptr);
931e6aba47Smatthias.ringwald     va_end(argptr);
940af41d30Smatthias.ringwald     return hci_send_acl_packet(sig_buffer, len);
950af41d30Smatthias.ringwald }
960af41d30Smatthias.ringwald 
97f62db1e3Smatthias.ringwald l2cap_channel_t * l2cap_get_channel_for_source_cid(uint16_t source_cid){
98f62db1e3Smatthias.ringwald     linked_item_t *it;
99f62db1e3Smatthias.ringwald     l2cap_channel_t * channel;
100f62db1e3Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
101f62db1e3Smatthias.ringwald         channel = (l2cap_channel_t *) it;
102f62db1e3Smatthias.ringwald         if ( channel->source_cid == source_cid) {
103f62db1e3Smatthias.ringwald             return channel;
104f62db1e3Smatthias.ringwald         }
105f62db1e3Smatthias.ringwald     }
106f62db1e3Smatthias.ringwald     return NULL;
107f62db1e3Smatthias.ringwald }
108f62db1e3Smatthias.ringwald 
1091e6aba47Smatthias.ringwald // open outgoing L2CAP channel
1101e6aba47Smatthias.ringwald void l2cap_create_channel_internal(connection_t * connection, bd_addr_t address, uint16_t psm){
1111e6aba47Smatthias.ringwald 
1121e6aba47Smatthias.ringwald     // alloc structure
1131e6aba47Smatthias.ringwald     l2cap_channel_t * chan = malloc(sizeof(l2cap_channel_t));
1141e6aba47Smatthias.ringwald     // TODO: emit error event
1151e6aba47Smatthias.ringwald     if (!chan) return;
1161e6aba47Smatthias.ringwald 
1171e6aba47Smatthias.ringwald     // fill in
1181e6aba47Smatthias.ringwald     BD_ADDR_COPY(chan->address, address);
1191e6aba47Smatthias.ringwald     chan->psm = psm;
1201e6aba47Smatthias.ringwald     chan->handle = 0;
1211e6aba47Smatthias.ringwald     chan->connection = connection;
1221e6aba47Smatthias.ringwald 
1231e6aba47Smatthias.ringwald     // set initial state
1241e6aba47Smatthias.ringwald     chan->state = L2CAP_STATE_CLOSED;
1251e6aba47Smatthias.ringwald     chan->sig_id = L2CAP_SIG_ID_INVALID;
1261e6aba47Smatthias.ringwald 
1271e6aba47Smatthias.ringwald     // add to connections list
1281e6aba47Smatthias.ringwald     linked_list_add(&l2cap_channels, (linked_item_t *) chan);
1291e6aba47Smatthias.ringwald 
1301e6aba47Smatthias.ringwald     // send connection request
1311e6aba47Smatthias.ringwald     // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch
1321e6aba47Smatthias.ringwald     hci_send_cmd(&hci_create_connection, address, 0x18, 0, 0, 0, 0);
13343625864Smatthias.ringwald }
13443625864Smatthias.ringwald 
1351e6aba47Smatthias.ringwald void l2cap_disconnect_internal(uint16_t source_cid, uint8_t reason){
136f62db1e3Smatthias.ringwald     // find channel for source_cid
137f62db1e3Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_source_cid(source_cid);
138f62db1e3Smatthias.ringwald     if (channel) {
139f62db1e3Smatthias.ringwald         channel->sig_id = l2cap_next_sig_id();
140f62db1e3Smatthias.ringwald         l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->sig_id, channel->dest_cid, channel->source_cid);
141f62db1e3Smatthias.ringwald         channel->state = L2CAP_STATE_WAIT_DISCONNECT;
142f62db1e3Smatthias.ringwald     }
14343625864Smatthias.ringwald }
1441e6aba47Smatthias.ringwald 
145afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){
1461e6aba47Smatthias.ringwald     linked_item_t *it;
1471e6aba47Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
148b448a0e7Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
149b448a0e7Smatthias.ringwald         if ( ! BD_ADDR_CMP( channel->address, address) ){
150b448a0e7Smatthias.ringwald             if (channel->state == L2CAP_STATE_CLOSED) {
151afde0c52Smatthias.ringwald                 // failure, forward error code
152afde0c52Smatthias.ringwald                 l2cap_emit_channel_opened(channel, status);
153afde0c52Smatthias.ringwald                 // discard channel
154afde0c52Smatthias.ringwald                 linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
155afde0c52Smatthias.ringwald                 free (channel);
156afde0c52Smatthias.ringwald             }
157afde0c52Smatthias.ringwald         }
158afde0c52Smatthias.ringwald     }
159afde0c52Smatthias.ringwald }
160afde0c52Smatthias.ringwald 
161afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){
162afde0c52Smatthias.ringwald     linked_item_t *it;
163afde0c52Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
164afde0c52Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
165afde0c52Smatthias.ringwald         if ( ! BD_ADDR_CMP( channel->address, address) ){
166afde0c52Smatthias.ringwald             if (channel->state == L2CAP_STATE_CLOSED) {
167b448a0e7Smatthias.ringwald                 // success, start l2cap handshake
168afde0c52Smatthias.ringwald                 channel->handle = handle;
169b448a0e7Smatthias.ringwald                 channel->sig_id = l2cap_next_sig_id();
170b448a0e7Smatthias.ringwald                 channel->source_cid = l2cap_next_source_cid();
171b448a0e7Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECT_RSP;
172b448a0e7Smatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, CONNECTION_REQUEST, channel->sig_id, channel->psm, channel->source_cid);
173afde0c52Smatthias.ringwald             }
174afde0c52Smatthias.ringwald         }
175afde0c52Smatthias.ringwald     }
176afde0c52Smatthias.ringwald }
177b448a0e7Smatthias.ringwald 
178afde0c52Smatthias.ringwald void l2cap_event_handler( uint8_t *packet, uint16_t size ){
179afde0c52Smatthias.ringwald 
180afde0c52Smatthias.ringwald     bd_addr_t address;
181afde0c52Smatthias.ringwald     hci_con_handle_t handle;
182afde0c52Smatthias.ringwald 
183afde0c52Smatthias.ringwald     switch(packet[0]){
184afde0c52Smatthias.ringwald 
185afde0c52Smatthias.ringwald         // handle connection complete events
186afde0c52Smatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
187afde0c52Smatthias.ringwald             bt_flip_addr(address, &packet[5]);
188afde0c52Smatthias.ringwald             if (packet[2] == 0){
189afde0c52Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
190afde0c52Smatthias.ringwald                 l2cap_handle_connection_success_for_addr(address, handle);
191afde0c52Smatthias.ringwald             } else {
192afde0c52Smatthias.ringwald                 l2cap_handle_connection_failed_for_addr(address, packet[2]);
193afde0c52Smatthias.ringwald             }
194afde0c52Smatthias.ringwald             break;
195afde0c52Smatthias.ringwald 
196afde0c52Smatthias.ringwald         // handle successful create connection cancel command
197afde0c52Smatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
198afde0c52Smatthias.ringwald             if ( COMMAND_COMPLETE_EVENT(packet, hci_create_connection_cancel) ) {
199afde0c52Smatthias.ringwald                 if (packet[5] == 0){
200afde0c52Smatthias.ringwald                     bt_flip_addr(address, &packet[6]);
201afde0c52Smatthias.ringwald                     // CONNECTION TERMINATED BY LOCAL HOST (0X16)
202afde0c52Smatthias.ringwald                     l2cap_handle_connection_failed_for_addr(address, 0x16);
20303cfbabcSmatthias.ringwald                 }
2041e6aba47Smatthias.ringwald             }
205afde0c52Smatthias.ringwald             break;
20627a923d0Smatthias.ringwald 
2071e6aba47Smatthias.ringwald         // handle disconnection complete events
208afde0c52Smatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
20927a923d0Smatthias.ringwald             // send l2cap disconnect events for all channels on this handle
210afde0c52Smatthias.ringwald             handle = READ_BT_16(packet, 3);
21127a923d0Smatthias.ringwald             linked_item_t *it;
21227a923d0Smatthias.ringwald             for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
21327a923d0Smatthias.ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) it;
21427a923d0Smatthias.ringwald                 if ( channel->handle == handle ){
21527a923d0Smatthias.ringwald                     l2cap_finialize_channel_close(channel);
21627a923d0Smatthias.ringwald                 }
21727a923d0Smatthias.ringwald             }
218afde0c52Smatthias.ringwald             break;
219fcadd0caSmatthias.ringwald 
220ee091cf1Smatthias.ringwald         // HCI Connection Timeouts
221afde0c52Smatthias.ringwald         case L2CAP_EVENT_TIMEOUT_CHECK:
222afde0c52Smatthias.ringwald             if (!capture_connection){
223ee091cf1Smatthias.ringwald                 hci_con_handle_t handle = READ_BT_16(packet, 2);
224ee091cf1Smatthias.ringwald                 linked_item_t *it;
225ee091cf1Smatthias.ringwald                 l2cap_channel_t * channel;
226ee091cf1Smatthias.ringwald                 int used = 0;
227ee091cf1Smatthias.ringwald                 for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
228ee091cf1Smatthias.ringwald                     channel = (l2cap_channel_t *) it;
229ee091cf1Smatthias.ringwald                     if (channel->handle == handle) {
230ee091cf1Smatthias.ringwald                         used = 1;
231ee091cf1Smatthias.ringwald                     }
232ee091cf1Smatthias.ringwald                 }
233ee091cf1Smatthias.ringwald                 if (!used) {
2349edc8742Smatthias.ringwald                     hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection
235ee091cf1Smatthias.ringwald                 }
236ee091cf1Smatthias.ringwald             }
237afde0c52Smatthias.ringwald             break;
238ee091cf1Smatthias.ringwald 
239afde0c52Smatthias.ringwald         default:
240afde0c52Smatthias.ringwald             break;
241afde0c52Smatthias.ringwald     }
242afde0c52Smatthias.ringwald 
243afde0c52Smatthias.ringwald     // pass on
244fcadd0caSmatthias.ringwald     (*event_packet_handler)(packet, size);
2451e6aba47Smatthias.ringwald }
2461e6aba47Smatthias.ringwald 
247afde0c52Smatthias.ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){
24884836b65Smatthias.ringwald     l2cap_send_signaling_packet( channel->handle, DISCONNECTION_RESPONSE, identifier, channel->dest_cid, channel->source_cid);
24984836b65Smatthias.ringwald     l2cap_finialize_channel_close(channel);
25084836b65Smatthias.ringwald }
25184836b65Smatthias.ringwald 
252645658c9Smatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t dest_cid){
253645658c9Smatthias.ringwald 
254169f8b28Smatthias.ringwald     // printf("l2cap_handle_connection_request for handle %u, psm %u cid %u\n", handle, psm, dest_cid);
255645658c9Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
256645658c9Smatthias.ringwald     if (!service) {
257645658c9Smatthias.ringwald         // 0x0002 PSM not supported
258169f8b28Smatthias.ringwald         // printf("l2cap_handle_connection_request no PSM for psm %u/n", psm);
259645658c9Smatthias.ringwald         l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, 0x0002, 0);
260645658c9Smatthias.ringwald         return;
261645658c9Smatthias.ringwald     }
262645658c9Smatthias.ringwald 
263645658c9Smatthias.ringwald     hci_connection_t * hci_connection = connection_for_handle( handle );
264645658c9Smatthias.ringwald     if (!hci_connection) {
265169f8b28Smatthias.ringwald         fprintf(stderr, "no hci_connection for handle %u\n", handle);
266645658c9Smatthias.ringwald         // TODO: emit error
267645658c9Smatthias.ringwald         return;
268645658c9Smatthias.ringwald     }
269645658c9Smatthias.ringwald     // alloc structure
270169f8b28Smatthias.ringwald     // printf("l2cap_handle_connection_request register channel\n");
271169f8b28Smatthias.ringwald     l2cap_channel_t * channel = malloc(sizeof(l2cap_channel_t));
272645658c9Smatthias.ringwald     // TODO: emit error event
273169f8b28Smatthias.ringwald     if (!channel) return;
274645658c9Smatthias.ringwald 
275645658c9Smatthias.ringwald     // fill in
276169f8b28Smatthias.ringwald     BD_ADDR_COPY(channel->address, hci_connection->address);
277169f8b28Smatthias.ringwald     channel->psm = psm;
278169f8b28Smatthias.ringwald     channel->handle = handle;
279169f8b28Smatthias.ringwald     channel->connection = service->connection;
280169f8b28Smatthias.ringwald     channel->source_cid = l2cap_next_source_cid();
281169f8b28Smatthias.ringwald     channel->dest_cid   = dest_cid;
282645658c9Smatthias.ringwald 
283645658c9Smatthias.ringwald     // set initial state
284e405ae81Smatthias.ringwald     channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT;
285e405ae81Smatthias.ringwald 
286e405ae81Smatthias.ringwald     // temp. store req sig id
287e405ae81Smatthias.ringwald     channel->sig_id = sig_id;
288645658c9Smatthias.ringwald 
289645658c9Smatthias.ringwald     // add to connections list
290169f8b28Smatthias.ringwald     linked_list_add(&l2cap_channels, (linked_item_t *) channel);
291645658c9Smatthias.ringwald 
292e405ae81Smatthias.ringwald     // emit incoming connection request
293e405ae81Smatthias.ringwald     l2cap_emit_connection_request(channel);
294e405ae81Smatthias.ringwald }
295645658c9Smatthias.ringwald 
296e405ae81Smatthias.ringwald void l2cap_accept_connection_internal(uint16_t source_cid){
297e405ae81Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_source_cid(source_cid);
298e405ae81Smatthias.ringwald     if (!channel) {
299e405ae81Smatthias.ringwald         fprintf(stderr, "l2cap_accept_connection_internal called but source_cid 0x%x not found", source_cid);
300e405ae81Smatthias.ringwald         return;
301e405ae81Smatthias.ringwald     }
302e405ae81Smatthias.ringwald 
303e405ae81Smatthias.ringwald     // accept connection
304e405ae81Smatthias.ringwald     l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->sig_id, channel->dest_cid, channel->source_cid, 0, 0);
305e405ae81Smatthias.ringwald 
306e405ae81Smatthias.ringwald     // set real sig and state and start config
307e405ae81Smatthias.ringwald     channel->sig_id = l2cap_next_sig_id();
308e405ae81Smatthias.ringwald     channel->state  = L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ;
309169f8b28Smatthias.ringwald     l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->sig_id, channel->dest_cid, 0, 4, &config_options);
310e405ae81Smatthias.ringwald }
311645658c9Smatthias.ringwald 
312e405ae81Smatthias.ringwald void l2cap_decline_connection_internal(uint16_t source_cid, uint8_t reason){
313e405ae81Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_source_cid( source_cid);
314e405ae81Smatthias.ringwald     if (!channel) {
315e405ae81Smatthias.ringwald         fprintf(stderr, "l2cap_decline_connection_internal called but source_cid 0x%x not found", source_cid,reason);
316e405ae81Smatthias.ringwald         return;
317e405ae81Smatthias.ringwald     }
318e405ae81Smatthias.ringwald     l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->sig_id, 0, 0, reason, 0);
319e405ae81Smatthias.ringwald 
320e405ae81Smatthias.ringwald     // discard channel
321e405ae81Smatthias.ringwald     linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
322e405ae81Smatthias.ringwald     free (channel);
323645658c9Smatthias.ringwald }
324645658c9Smatthias.ringwald 
3251e6aba47Smatthias.ringwald void l2cap_signaling_handler(l2cap_channel_t *channel, uint8_t *packet, uint16_t size){
3261e6aba47Smatthias.ringwald 
3271e6aba47Smatthias.ringwald     uint8_t  code       = READ_L2CAP_SIGNALING_CODE( packet );
3281e6aba47Smatthias.ringwald     uint8_t  identifier = READ_L2CAP_SIGNALING_IDENTIFIER( packet );
32938e5900eSmatthias.ringwald     uint16_t result = 0;
3301e6aba47Smatthias.ringwald 
3311e6aba47Smatthias.ringwald     switch (channel->state) {
3321e6aba47Smatthias.ringwald 
3331e6aba47Smatthias.ringwald         case L2CAP_STATE_WAIT_CONNECT_RSP:
3341e6aba47Smatthias.ringwald             switch (code){
3351e6aba47Smatthias.ringwald                 case CONNECTION_RESPONSE:
336141679a4Smatthias.ringwald                     result = READ_BT_16 (packet, L2CAP_SIGNALING_DATA_OFFSET+4);
33738e5900eSmatthias.ringwald                     switch (result) {
33838e5900eSmatthias.ringwald                         case 0:
339169f8b28Smatthias.ringwald                             // successful connection
340cd56f931Smatthias.ringwald                             channel->dest_cid = READ_BT_16(packet, L2CAP_SIGNALING_DATA_OFFSET);
3411e6aba47Smatthias.ringwald                             channel->sig_id = l2cap_next_sig_id();
3421e6aba47Smatthias.ringwald                             l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->sig_id, channel->dest_cid, 0, 4, &config_options);
3435a67bd4aSmatthias.ringwald                             channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ;
34438e5900eSmatthias.ringwald                             break;
34538e5900eSmatthias.ringwald                         case 1:
34638e5900eSmatthias.ringwald                             // connection pending. get some coffee
34738e5900eSmatthias.ringwald                             break;
34838e5900eSmatthias.ringwald                         default:
349f32b992eSmatthias.ringwald                             // map l2cap connection response result to BTstack status enumeration
35038e5900eSmatthias.ringwald                             l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result);
35138e5900eSmatthias.ringwald                             break;
3521e6aba47Smatthias.ringwald                     }
3531e6aba47Smatthias.ringwald                     break;
35438e5900eSmatthias.ringwald 
35584836b65Smatthias.ringwald                 case DISCONNECTION_REQUEST:
35684836b65Smatthias.ringwald                     l2cap_handle_disconnect_request(channel, identifier);
35784836b65Smatthias.ringwald                     break;
35884836b65Smatthias.ringwald 
35938e5900eSmatthias.ringwald                 default:
3601e6aba47Smatthias.ringwald                     //@TODO: implement other signaling packets
36138e5900eSmatthias.ringwald                     break;
3621e6aba47Smatthias.ringwald             }
3631e6aba47Smatthias.ringwald             break;
3641e6aba47Smatthias.ringwald 
3655a67bd4aSmatthias.ringwald         case L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ:
3661e6aba47Smatthias.ringwald             switch (code) {
3671e6aba47Smatthias.ringwald                 case CONFIGURE_RESPONSE:
3681e6aba47Smatthias.ringwald                     channel->state = L2CAP_STATE_WAIT_CONFIG_REQ;
3691e6aba47Smatthias.ringwald                     break;
3705a67bd4aSmatthias.ringwald                 case CONFIGURE_REQUEST:
3715a67bd4aSmatthias.ringwald                     // accept the other's configuration options
3725a67bd4aSmatthias.ringwald                     l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, identifier, channel->dest_cid, 0, 0, size - 16, &packet[16]);
3735a67bd4aSmatthias.ringwald                     channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP;
3745a67bd4aSmatthias.ringwald                     break;
37584836b65Smatthias.ringwald                 case DISCONNECTION_REQUEST:
37684836b65Smatthias.ringwald                     l2cap_handle_disconnect_request(channel, identifier);
37784836b65Smatthias.ringwald                     break;
3785a67bd4aSmatthias.ringwald                 default:
3795a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
3805a67bd4aSmatthias.ringwald                     break;
3811e6aba47Smatthias.ringwald             }
3821e6aba47Smatthias.ringwald             break;
3831e6aba47Smatthias.ringwald 
3841e6aba47Smatthias.ringwald         case L2CAP_STATE_WAIT_CONFIG_REQ:
3851e6aba47Smatthias.ringwald             switch (code) {
3861e6aba47Smatthias.ringwald                 case CONFIGURE_REQUEST:
3871e6aba47Smatthias.ringwald                     // accept the other's configuration options
3881e6aba47Smatthias.ringwald                     l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, identifier, channel->dest_cid, 0, 0, size - 16, &packet[16]);
3891e6aba47Smatthias.ringwald                     channel->state = L2CAP_STATE_OPEN;
39003cfbabcSmatthias.ringwald                     l2cap_emit_channel_opened(channel, 0);  // success
391c8e4258aSmatthias.ringwald                     break;
39284836b65Smatthias.ringwald                 case DISCONNECTION_REQUEST:
39384836b65Smatthias.ringwald                     l2cap_handle_disconnect_request(channel, identifier);
39484836b65Smatthias.ringwald                     break;
3955a67bd4aSmatthias.ringwald                 default:
3965a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
3975a67bd4aSmatthias.ringwald                     break;
3985a67bd4aSmatthias.ringwald             }
3995a67bd4aSmatthias.ringwald             break;
4005a67bd4aSmatthias.ringwald 
4015a67bd4aSmatthias.ringwald         case L2CAP_STATE_WAIT_CONFIG_REQ_RSP:
4025a67bd4aSmatthias.ringwald             switch (code) {
4035a67bd4aSmatthias.ringwald                 case CONFIGURE_RESPONSE:
4045a67bd4aSmatthias.ringwald                     channel->state = L2CAP_STATE_OPEN;
4055a67bd4aSmatthias.ringwald                     l2cap_emit_channel_opened(channel, 0);  // success
4065a67bd4aSmatthias.ringwald                     break;
40784836b65Smatthias.ringwald                 case DISCONNECTION_REQUEST:
40884836b65Smatthias.ringwald                     l2cap_handle_disconnect_request(channel, identifier);
40984836b65Smatthias.ringwald                     break;
4105a67bd4aSmatthias.ringwald                 default:
4115a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
4125a67bd4aSmatthias.ringwald                     break;
413c8e4258aSmatthias.ringwald             }
414c8e4258aSmatthias.ringwald             break;
415f62db1e3Smatthias.ringwald 
416f62db1e3Smatthias.ringwald         case L2CAP_STATE_WAIT_DISCONNECT:
417f62db1e3Smatthias.ringwald             switch (code) {
418f62db1e3Smatthias.ringwald                 case DISCONNECTION_RESPONSE:
41927a923d0Smatthias.ringwald                     l2cap_finialize_channel_close(channel);
42027a923d0Smatthias.ringwald                     break;
42184836b65Smatthias.ringwald                 case DISCONNECTION_REQUEST:
42284836b65Smatthias.ringwald                     l2cap_handle_disconnect_request(channel, identifier);
42384836b65Smatthias.ringwald                     break;
4245a67bd4aSmatthias.ringwald                 default:
4255a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
4265a67bd4aSmatthias.ringwald                     break;
42727a923d0Smatthias.ringwald             }
42827a923d0Smatthias.ringwald             break;
42984836b65Smatthias.ringwald 
43084836b65Smatthias.ringwald         case L2CAP_STATE_CLOSED:
43184836b65Smatthias.ringwald             // @TODO handle incoming requests
43284836b65Smatthias.ringwald             break;
43384836b65Smatthias.ringwald 
43484836b65Smatthias.ringwald         case L2CAP_STATE_OPEN:
43584836b65Smatthias.ringwald             switch (code) {
43684836b65Smatthias.ringwald                 case DISCONNECTION_REQUEST:
43784836b65Smatthias.ringwald                     l2cap_handle_disconnect_request(channel, identifier);
43884836b65Smatthias.ringwald                     break;
4395a67bd4aSmatthias.ringwald                 default:
44084836b65Smatthias.ringwald                     //@TODO: implement other signaling packets, e.g. re-configure
44184836b65Smatthias.ringwald                     break;
44284836b65Smatthias.ringwald             }
4435a67bd4aSmatthias.ringwald             break;
44427a923d0Smatthias.ringwald     }
44527a923d0Smatthias.ringwald }
44627a923d0Smatthias.ringwald 
44727a923d0Smatthias.ringwald // finalize closed channel
44827a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t *channel){
449f62db1e3Smatthias.ringwald     channel->state = L2CAP_STATE_CLOSED;
450f62db1e3Smatthias.ringwald     l2cap_emit_channel_closed(channel);
451f62db1e3Smatthias.ringwald 
452f62db1e3Smatthias.ringwald     // discard channel
453f62db1e3Smatthias.ringwald     linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
454f62db1e3Smatthias.ringwald     free (channel);
455c8e4258aSmatthias.ringwald }
4561e6aba47Smatthias.ringwald 
4579d9bbc01Smatthias.ringwald l2cap_service_t * l2cap_get_service(uint16_t psm){
458c52bf64dSmatthias.ringwald     linked_item_t *it;
4599d9bbc01Smatthias.ringwald 
4609d9bbc01Smatthias.ringwald     // close open channels
4619d9bbc01Smatthias.ringwald     for (it = (linked_item_t *) l2cap_services; it ; it = it->next){
4629d9bbc01Smatthias.ringwald         l2cap_service_t * service = ((l2cap_service_t *) it);
4639d9bbc01Smatthias.ringwald         if ( service->psm == psm){
4649d9bbc01Smatthias.ringwald             return service;
4659d9bbc01Smatthias.ringwald         };
4669d9bbc01Smatthias.ringwald     }
4679d9bbc01Smatthias.ringwald     return NULL;
4689d9bbc01Smatthias.ringwald }
4699d9bbc01Smatthias.ringwald 
470116ee617Smatthias.ringwald void l2cap_register_service_internal(connection_t *connection, uint16_t psm, uint16_t mtu){
4719d9bbc01Smatthias.ringwald     // check for alread registered psm // TODO: emit error event
4729d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
4739d9bbc01Smatthias.ringwald     if (service) return;
4749d9bbc01Smatthias.ringwald 
4759d9bbc01Smatthias.ringwald     // alloc structure     // TODO: emit error event
4769d9bbc01Smatthias.ringwald     service = malloc(sizeof(l2cap_service_t));
4779d9bbc01Smatthias.ringwald     if (!service) return;
4789d9bbc01Smatthias.ringwald 
4799d9bbc01Smatthias.ringwald     // fill in
4809d9bbc01Smatthias.ringwald     service->psm = psm;
4819d9bbc01Smatthias.ringwald     service->mtu = mtu;
4829d9bbc01Smatthias.ringwald     service->connection = connection;
4839d9bbc01Smatthias.ringwald 
4849d9bbc01Smatthias.ringwald     // add to services list
4859d9bbc01Smatthias.ringwald     linked_list_add(&l2cap_services, (linked_item_t *) service);
4869d9bbc01Smatthias.ringwald }
4879d9bbc01Smatthias.ringwald 
488116ee617Smatthias.ringwald void l2cap_unregister_service_internal(connection_t *connection, uint16_t psm){
4899d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
4909d9bbc01Smatthias.ringwald     if (service) return;
4919d9bbc01Smatthias.ringwald     linked_list_remove(&l2cap_services, (linked_item_t *) service);
4929d9bbc01Smatthias.ringwald     free( service );
4939d9bbc01Smatthias.ringwald }
4949d9bbc01Smatthias.ringwald 
4959d9bbc01Smatthias.ringwald //
4969d9bbc01Smatthias.ringwald void l2cap_close_connection(connection_t *connection){
4979d9bbc01Smatthias.ringwald     linked_item_t *it;
4989d9bbc01Smatthias.ringwald 
4999d9bbc01Smatthias.ringwald     // close open channels
500c52bf64dSmatthias.ringwald     l2cap_channel_t * channel;
501c52bf64dSmatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
502c52bf64dSmatthias.ringwald         channel = (l2cap_channel_t *) it;
503c52bf64dSmatthias.ringwald         if (channel->connection == connection) {
504c52bf64dSmatthias.ringwald             channel->sig_id = l2cap_next_sig_id();
505c52bf64dSmatthias.ringwald             l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->sig_id, channel->dest_cid, channel->source_cid);
506c52bf64dSmatthias.ringwald             channel->state = L2CAP_STATE_WAIT_DISCONNECT;
507c52bf64dSmatthias.ringwald         }
508c52bf64dSmatthias.ringwald     }
5099d9bbc01Smatthias.ringwald 
510645658c9Smatthias.ringwald     // unregister services
511645658c9Smatthias.ringwald     l2cap_service_t *service;
512645658c9Smatthias.ringwald     for (it = (linked_item_t *) &l2cap_services; it ; it = it->next){
513645658c9Smatthias.ringwald         channel = (l2cap_channel_t *) it->next;
514645658c9Smatthias.ringwald         if (service->connection == connection){
515645658c9Smatthias.ringwald             it->next = it->next->next;
516645658c9Smatthias.ringwald             return;
517645658c9Smatthias.ringwald         }
518645658c9Smatthias.ringwald     }
519c52bf64dSmatthias.ringwald }
520c52bf64dSmatthias.ringwald 
5211e6aba47Smatthias.ringwald //  notify client
52203cfbabcSmatthias.ringwald void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) {
52303cfbabcSmatthias.ringwald     uint8_t event[17];
52480d52d6bSmatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_OPENED;
525c8e4258aSmatthias.ringwald     event[1] = sizeof(event) - 2;
52603cfbabcSmatthias.ringwald     event[2] = status;
52703cfbabcSmatthias.ringwald     bt_flip_addr(&event[3], channel->address);
52803cfbabcSmatthias.ringwald     bt_store_16(event,  9, channel->handle);
52903cfbabcSmatthias.ringwald     bt_store_16(event, 11, channel->psm);
53003cfbabcSmatthias.ringwald     bt_store_16(event, 13, channel->source_cid);
53103cfbabcSmatthias.ringwald     bt_store_16(event, 15, channel->dest_cid);
5322289ec97Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
5331e6aba47Smatthias.ringwald     socket_connection_send_packet(channel->connection, HCI_EVENT_PACKET, 0, event, sizeof(event));
5341e6aba47Smatthias.ringwald }
5351e6aba47Smatthias.ringwald 
536f62db1e3Smatthias.ringwald void l2cap_emit_channel_closed(l2cap_channel_t *channel) {
537f62db1e3Smatthias.ringwald     uint8_t event[4];
53880d52d6bSmatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_CLOSED;
539f62db1e3Smatthias.ringwald     event[1] = sizeof(event) - 2;
540f62db1e3Smatthias.ringwald     bt_store_16(event, 2, channel->source_cid);
5412289ec97Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
542f62db1e3Smatthias.ringwald     socket_connection_send_packet(channel->connection, HCI_EVENT_PACKET, 0, event, sizeof(event));
543f62db1e3Smatthias.ringwald }
544f62db1e3Smatthias.ringwald 
545e405ae81Smatthias.ringwald void l2cap_emit_connection_request(l2cap_channel_t *channel) {
546e405ae81Smatthias.ringwald     uint8_t event[16];
547e405ae81Smatthias.ringwald     event[0] = L2CAP_EVENT_INCOMING_CONNECTION;
548e405ae81Smatthias.ringwald     event[1] = sizeof(event) - 2;
549e405ae81Smatthias.ringwald     bt_flip_addr(&event[2], channel->address);
550e405ae81Smatthias.ringwald     bt_store_16(event,  8, channel->handle);
551e405ae81Smatthias.ringwald     bt_store_16(event, 10, channel->psm);
552e405ae81Smatthias.ringwald     bt_store_16(event, 12, channel->source_cid);
553e405ae81Smatthias.ringwald     bt_store_16(event, 14, channel->dest_cid);
5542289ec97Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
555e405ae81Smatthias.ringwald     socket_connection_send_packet(channel->connection, HCI_EVENT_PACKET, 0, event, sizeof(event));
556e405ae81Smatthias.ringwald }
5572289ec97Smatthias.ringwald 
5581e6aba47Smatthias.ringwald void l2cap_acl_handler( uint8_t *packet, uint16_t size ){
5591e6aba47Smatthias.ringwald 
5609edc8742Smatthias.ringwald     // Capturing?
5619edc8742Smatthias.ringwald     if (capture_connection) {
5629edc8742Smatthias.ringwald         socket_connection_send_packet(capture_connection, HCI_ACL_DATA_PACKET, 0, packet, size);
563d6f03c5dSmatthias.ringwald         return;
5649edc8742Smatthias.ringwald     }
5659edc8742Smatthias.ringwald 
5669edc8742Smatthias.ringwald     // forward to higher layers - not needed yet
5679edc8742Smatthias.ringwald     // (*data_packet_handler)(channel_id, packet, size);
5689edc8742Smatthias.ringwald 
5691e6aba47Smatthias.ringwald     // Get Channel ID and command code
5701e6aba47Smatthias.ringwald     uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet);
5711e6aba47Smatthias.ringwald     uint8_t  code       = READ_L2CAP_SIGNALING_CODE( packet );
5721e6aba47Smatthias.ringwald 
5731e6aba47Smatthias.ringwald     // Get Connection
5741e6aba47Smatthias.ringwald     hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet);
5751e6aba47Smatthias.ringwald 
576169f8b28Smatthias.ringwald     // printf("l2cap_acl_handler channel %u, code %u\n", channel_id, code);
577169f8b28Smatthias.ringwald 
5781e6aba47Smatthias.ringwald     // Signaling Packet?
5791e6aba47Smatthias.ringwald     if (channel_id == 1) {
5801e6aba47Smatthias.ringwald 
581169f8b28Smatthias.ringwald         if (code < 1 || code >= 8){
582169f8b28Smatthias.ringwald             // not for a particular channel, and not CONNECTION_REQUEST
5831e6aba47Smatthias.ringwald             return;
5841e6aba47Smatthias.ringwald         }
5851e6aba47Smatthias.ringwald 
586645658c9Smatthias.ringwald         // Get Signaling Identifier
5871e6aba47Smatthias.ringwald         uint8_t sig_id    = READ_L2CAP_SIGNALING_IDENTIFIER(packet);
588645658c9Smatthias.ringwald 
589645658c9Smatthias.ringwald         // CONNECTION_REQUEST
590645658c9Smatthias.ringwald         if (code == CONNECTION_REQUEST){
591645658c9Smatthias.ringwald             uint16_t psm =      READ_BT_16(packet, L2CAP_SIGNALING_DATA_OFFSET);
592645658c9Smatthias.ringwald             uint16_t dest_cid = READ_BT_16(packet, L2CAP_SIGNALING_DATA_OFFSET+2);
593645658c9Smatthias.ringwald             l2cap_handle_connection_request(handle, sig_id, psm, dest_cid);
594645658c9Smatthias.ringwald             return;
595645658c9Smatthias.ringwald         }
596645658c9Smatthias.ringwald 
597645658c9Smatthias.ringwald         // Get potential destination CID
5981e6aba47Smatthias.ringwald         uint16_t dest_cid = READ_BT_16(packet, L2CAP_SIGNALING_DATA_OFFSET);
5991e6aba47Smatthias.ringwald 
6001e6aba47Smatthias.ringwald         // Find channel for this sig_id and connection handle
6011e6aba47Smatthias.ringwald         linked_item_t *it;
6021e6aba47Smatthias.ringwald         for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
603b448a0e7Smatthias.ringwald             l2cap_channel_t * channel = (l2cap_channel_t *) it;
604b448a0e7Smatthias.ringwald             if (channel->handle == handle) {
6051e6aba47Smatthias.ringwald                 if (code & 1) {
6061e6aba47Smatthias.ringwald                     // match odd commands by previous signaling identifier
607b448a0e7Smatthias.ringwald                     if (channel->sig_id == sig_id) {
608b448a0e7Smatthias.ringwald                         l2cap_signaling_handler( channel, packet, size);
6091e6aba47Smatthias.ringwald                     }
6101e6aba47Smatthias.ringwald                 } else {
6111e6aba47Smatthias.ringwald                     // match even commands by source channel id
612b448a0e7Smatthias.ringwald                     if (channel->source_cid == dest_cid) {
613b448a0e7Smatthias.ringwald                         l2cap_signaling_handler( channel, packet, size);
6141e6aba47Smatthias.ringwald                     }
6151e6aba47Smatthias.ringwald                 }
6161e6aba47Smatthias.ringwald             }
6171e6aba47Smatthias.ringwald         }
6181e6aba47Smatthias.ringwald         return;
6191e6aba47Smatthias.ringwald     }
6201e6aba47Smatthias.ringwald 
6211e6aba47Smatthias.ringwald     // Find channel for this channel_id and connection handle
622f62db1e3Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_source_cid(channel_id);
623f62db1e3Smatthias.ringwald     if (channel) {
6246f60b3f4Smatthias.ringwald         socket_connection_send_packet(channel->connection, L2CAP_DATA_PACKET, channel_id,
6256f60b3f4Smatthias.ringwald                                       &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
6261e6aba47Smatthias.ringwald     }
6271e6aba47Smatthias.ringwald }
6281e6aba47Smatthias.ringwald 
629f62db1e3Smatthias.ringwald 
6301e6aba47Smatthias.ringwald void l2cap_send_internal(uint16_t source_cid, uint8_t *data, uint16_t len){
6311e6aba47Smatthias.ringwald     // find channel for source_cid, construct l2cap packet and send
632f62db1e3Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_source_cid(source_cid);
633fcadd0caSmatthias.ringwald     if (channel) {
6341e6aba47Smatthias.ringwald          // 0 - Connection handle : PB=10 : BC=00
6351e6aba47Smatthias.ringwald          bt_store_16(acl_buffer, 0, channel->handle | (2 << 12) | (0 << 14));
6361e6aba47Smatthias.ringwald          // 2 - ACL length
6371e6aba47Smatthias.ringwald          bt_store_16(acl_buffer, 2,  len + 4);
6381e6aba47Smatthias.ringwald          // 4 - L2CAP packet length
6391e6aba47Smatthias.ringwald          bt_store_16(acl_buffer, 4,  len + 0);
6401e6aba47Smatthias.ringwald          // 6 - L2CAP channel DEST
6411e6aba47Smatthias.ringwald          bt_store_16(acl_buffer, 6, channel->dest_cid);
6421e6aba47Smatthias.ringwald          // 8 - data
6431e6aba47Smatthias.ringwald          memcpy(&acl_buffer[8], data, len);
6441e6aba47Smatthias.ringwald          // send
6451e6aba47Smatthias.ringwald          hci_send_acl_packet(acl_buffer, len+8);
6461e6aba47Smatthias.ringwald      }
6471e6aba47Smatthias.ringwald }
6481e6aba47Smatthias.ringwald 
6499edc8742Smatthias.ringwald void l2cap_set_capture_connection(connection_t * connection){
6509edc8742Smatthias.ringwald     capture_connection = connection;
6519edc8742Smatthias.ringwald }
6521e6aba47Smatthias.ringwald 
653b448a0e7Smatthias.ringwald