xref: /btstack/src/l2cap.c (revision 2289ec97a0546e0f840f830dcfd663d876d304e9)
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"
4243625864Smatthias.ringwald 
4343625864Smatthias.ringwald #include <stdarg.h>
4443625864Smatthias.ringwald #include <string.h>
4543625864Smatthias.ringwald 
4643625864Smatthias.ringwald #include <stdio.h>
4743625864Smatthias.ringwald 
486f60b3f4Smatthias.ringwald // size of HCI ACL + L2CAP Header for regular data packets
496f60b3f4Smatthias.ringwald #define COMPLETE_L2CAP_HEADER 8
506f60b3f4Smatthias.ringwald 
51fcadd0caSmatthias.ringwald static void null_event_handler(uint8_t *packet, uint16_t size);
52fcadd0caSmatthias.ringwald static void null_data_handler(uint16_t source_cid, uint8_t *packet, uint16_t size);
53fcadd0caSmatthias.ringwald 
541e6aba47Smatthias.ringwald static uint8_t * sig_buffer = NULL;
551e6aba47Smatthias.ringwald static linked_list_t l2cap_channels = NULL;
569d9bbc01Smatthias.ringwald static linked_list_t l2cap_services = NULL;
571e6aba47Smatthias.ringwald static uint8_t * acl_buffer = NULL;
58fcadd0caSmatthias.ringwald static void (*event_packet_handler) (uint8_t *packet, uint16_t size) = null_event_handler;
59fcadd0caSmatthias.ringwald static void (*data_packet_handler)  (uint16_t source_cid, uint8_t *packet, uint16_t size) = null_data_handler;
609edc8742Smatthias.ringwald static connection_t * capture_connection = NULL;
611e6aba47Smatthias.ringwald 
62169f8b28Smatthias.ringwald static uint8_t config_options[] = { 1, 2, 150, 0}; // mtu = 48
63169f8b28Smatthias.ringwald 
641e6aba47Smatthias.ringwald void l2cap_init(){
651e6aba47Smatthias.ringwald     sig_buffer = malloc( 48 );
661e6aba47Smatthias.ringwald     acl_buffer = malloc( 255 + 8 );
67fcadd0caSmatthias.ringwald 
68fcadd0caSmatthias.ringwald     //
69fcadd0caSmatthias.ringwald     // register callbacks with HCI
70fcadd0caSmatthias.ringwald     //
71fcadd0caSmatthias.ringwald     hci_register_event_packet_handler(&l2cap_event_handler);
72fcadd0caSmatthias.ringwald     hci_register_acl_packet_handler(&l2cap_acl_handler);
73fcadd0caSmatthias.ringwald }
74fcadd0caSmatthias.ringwald 
75fcadd0caSmatthias.ringwald 
76fcadd0caSmatthias.ringwald /** Register L2CAP packet handlers */
77fcadd0caSmatthias.ringwald static void null_event_handler(uint8_t *packet, uint16_t size){
78fcadd0caSmatthias.ringwald }
79fcadd0caSmatthias.ringwald static void null_data_handler(uint16_t  source_cid, uint8_t *packet, uint16_t size){
80fcadd0caSmatthias.ringwald }
81fcadd0caSmatthias.ringwald void l2cap_register_event_packet_handler(void (*handler)(uint8_t *packet, uint16_t size)){
82fcadd0caSmatthias.ringwald     event_packet_handler = handler;
83fcadd0caSmatthias.ringwald }
84fcadd0caSmatthias.ringwald void l2cap_register_data_packet_handler  (void (*handler)(uint16_t source_cid, uint8_t *packet, uint16_t size)){
85fcadd0caSmatthias.ringwald     data_packet_handler = handler;
861e6aba47Smatthias.ringwald }
871e6aba47Smatthias.ringwald 
880af41d30Smatthias.ringwald int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){
890af41d30Smatthias.ringwald     va_list argptr;
900af41d30Smatthias.ringwald     va_start(argptr, identifier);
910af41d30Smatthias.ringwald     uint16_t len = l2cap_create_signaling_internal(sig_buffer, handle, cmd, identifier, argptr);
921e6aba47Smatthias.ringwald     va_end(argptr);
930af41d30Smatthias.ringwald     return hci_send_acl_packet(sig_buffer, len);
940af41d30Smatthias.ringwald }
950af41d30Smatthias.ringwald 
96f62db1e3Smatthias.ringwald l2cap_channel_t * l2cap_get_channel_for_source_cid(uint16_t source_cid){
97f62db1e3Smatthias.ringwald     linked_item_t *it;
98f62db1e3Smatthias.ringwald     l2cap_channel_t * channel;
99f62db1e3Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
100f62db1e3Smatthias.ringwald         channel = (l2cap_channel_t *) it;
101f62db1e3Smatthias.ringwald         if ( channel->source_cid == source_cid) {
102f62db1e3Smatthias.ringwald             return channel;
103f62db1e3Smatthias.ringwald         }
104f62db1e3Smatthias.ringwald     }
105f62db1e3Smatthias.ringwald     return NULL;
106f62db1e3Smatthias.ringwald }
107f62db1e3Smatthias.ringwald 
1081e6aba47Smatthias.ringwald // open outgoing L2CAP channel
1091e6aba47Smatthias.ringwald void l2cap_create_channel_internal(connection_t * connection, bd_addr_t address, uint16_t psm){
1101e6aba47Smatthias.ringwald 
1111e6aba47Smatthias.ringwald     // alloc structure
1121e6aba47Smatthias.ringwald     l2cap_channel_t * chan = malloc(sizeof(l2cap_channel_t));
1131e6aba47Smatthias.ringwald     // TODO: emit error event
1141e6aba47Smatthias.ringwald     if (!chan) return;
1151e6aba47Smatthias.ringwald 
1161e6aba47Smatthias.ringwald     // fill in
1171e6aba47Smatthias.ringwald     BD_ADDR_COPY(chan->address, address);
1181e6aba47Smatthias.ringwald     chan->psm = psm;
1191e6aba47Smatthias.ringwald     chan->handle = 0;
1201e6aba47Smatthias.ringwald     chan->connection = connection;
1211e6aba47Smatthias.ringwald 
1221e6aba47Smatthias.ringwald     // set initial state
1231e6aba47Smatthias.ringwald     chan->state = L2CAP_STATE_CLOSED;
1241e6aba47Smatthias.ringwald     chan->sig_id = L2CAP_SIG_ID_INVALID;
1251e6aba47Smatthias.ringwald 
1261e6aba47Smatthias.ringwald     // add to connections list
1271e6aba47Smatthias.ringwald     linked_list_add(&l2cap_channels, (linked_item_t *) chan);
1281e6aba47Smatthias.ringwald 
1291e6aba47Smatthias.ringwald     // send connection request
1301e6aba47Smatthias.ringwald     // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch
1311e6aba47Smatthias.ringwald     hci_send_cmd(&hci_create_connection, address, 0x18, 0, 0, 0, 0);
13243625864Smatthias.ringwald }
13343625864Smatthias.ringwald 
1341e6aba47Smatthias.ringwald void l2cap_disconnect_internal(uint16_t source_cid, uint8_t reason){
135f62db1e3Smatthias.ringwald     // find channel for source_cid
136f62db1e3Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_source_cid(source_cid);
137f62db1e3Smatthias.ringwald     if (channel) {
138f62db1e3Smatthias.ringwald         channel->sig_id = l2cap_next_sig_id();
139f62db1e3Smatthias.ringwald         l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->sig_id, channel->dest_cid, channel->source_cid);
140f62db1e3Smatthias.ringwald         channel->state = L2CAP_STATE_WAIT_DISCONNECT;
141f62db1e3Smatthias.ringwald     }
14243625864Smatthias.ringwald }
1431e6aba47Smatthias.ringwald 
144afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){
1451e6aba47Smatthias.ringwald     linked_item_t *it;
1461e6aba47Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
147b448a0e7Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
148b448a0e7Smatthias.ringwald         if ( ! BD_ADDR_CMP( channel->address, address) ){
149b448a0e7Smatthias.ringwald             if (channel->state == L2CAP_STATE_CLOSED) {
150afde0c52Smatthias.ringwald                 // failure, forward error code
151afde0c52Smatthias.ringwald                 l2cap_emit_channel_opened(channel, status);
152afde0c52Smatthias.ringwald                 // discard channel
153afde0c52Smatthias.ringwald                 linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
154afde0c52Smatthias.ringwald                 free (channel);
155afde0c52Smatthias.ringwald             }
156afde0c52Smatthias.ringwald         }
157afde0c52Smatthias.ringwald     }
158afde0c52Smatthias.ringwald }
159afde0c52Smatthias.ringwald 
160afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){
161afde0c52Smatthias.ringwald     linked_item_t *it;
162afde0c52Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
163afde0c52Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
164afde0c52Smatthias.ringwald         if ( ! BD_ADDR_CMP( channel->address, address) ){
165afde0c52Smatthias.ringwald             if (channel->state == L2CAP_STATE_CLOSED) {
166b448a0e7Smatthias.ringwald                 // success, start l2cap handshake
167afde0c52Smatthias.ringwald                 channel->handle = handle;
168b448a0e7Smatthias.ringwald                 channel->sig_id = l2cap_next_sig_id();
169b448a0e7Smatthias.ringwald                 channel->source_cid = l2cap_next_source_cid();
170b448a0e7Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECT_RSP;
171b448a0e7Smatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, CONNECTION_REQUEST, channel->sig_id, channel->psm, channel->source_cid);
172afde0c52Smatthias.ringwald             }
173afde0c52Smatthias.ringwald         }
174afde0c52Smatthias.ringwald     }
175afde0c52Smatthias.ringwald }
176b448a0e7Smatthias.ringwald 
177afde0c52Smatthias.ringwald void l2cap_event_handler( uint8_t *packet, uint16_t size ){
178afde0c52Smatthias.ringwald 
179afde0c52Smatthias.ringwald     bd_addr_t address;
180afde0c52Smatthias.ringwald     hci_con_handle_t handle;
181afde0c52Smatthias.ringwald 
182afde0c52Smatthias.ringwald     switch(packet[0]){
183afde0c52Smatthias.ringwald 
184afde0c52Smatthias.ringwald         // handle connection complete events
185afde0c52Smatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
186afde0c52Smatthias.ringwald             bt_flip_addr(address, &packet[5]);
187afde0c52Smatthias.ringwald             if (packet[2] == 0){
188afde0c52Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
189afde0c52Smatthias.ringwald                 l2cap_handle_connection_success_for_addr(address, handle);
190afde0c52Smatthias.ringwald             } else {
191afde0c52Smatthias.ringwald                 l2cap_handle_connection_failed_for_addr(address, packet[2]);
192afde0c52Smatthias.ringwald             }
193afde0c52Smatthias.ringwald             break;
194afde0c52Smatthias.ringwald 
195afde0c52Smatthias.ringwald         // handle successful create connection cancel command
196afde0c52Smatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
197afde0c52Smatthias.ringwald             if ( COMMAND_COMPLETE_EVENT(packet, hci_create_connection_cancel) ) {
198afde0c52Smatthias.ringwald                 if (packet[5] == 0){
199afde0c52Smatthias.ringwald                     bt_flip_addr(address, &packet[6]);
200afde0c52Smatthias.ringwald                     // CONNECTION TERMINATED BY LOCAL HOST (0X16)
201afde0c52Smatthias.ringwald                     l2cap_handle_connection_failed_for_addr(address, 0x16);
20203cfbabcSmatthias.ringwald                 }
2031e6aba47Smatthias.ringwald             }
204afde0c52Smatthias.ringwald             break;
20527a923d0Smatthias.ringwald 
2061e6aba47Smatthias.ringwald         // handle disconnection complete events
207afde0c52Smatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
20827a923d0Smatthias.ringwald             // send l2cap disconnect events for all channels on this handle
209afde0c52Smatthias.ringwald             handle = READ_BT_16(packet, 3);
21027a923d0Smatthias.ringwald             linked_item_t *it;
21127a923d0Smatthias.ringwald             for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
21227a923d0Smatthias.ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) it;
21327a923d0Smatthias.ringwald                 if ( channel->handle == handle ){
21427a923d0Smatthias.ringwald                     l2cap_finialize_channel_close(channel);
21527a923d0Smatthias.ringwald                 }
21627a923d0Smatthias.ringwald             }
217afde0c52Smatthias.ringwald             break;
218fcadd0caSmatthias.ringwald 
219ee091cf1Smatthias.ringwald         // HCI Connection Timeouts
220afde0c52Smatthias.ringwald         case L2CAP_EVENT_TIMEOUT_CHECK:
221afde0c52Smatthias.ringwald             if (!capture_connection){
222ee091cf1Smatthias.ringwald                 hci_con_handle_t handle = READ_BT_16(packet, 2);
223ee091cf1Smatthias.ringwald                 linked_item_t *it;
224ee091cf1Smatthias.ringwald                 l2cap_channel_t * channel;
225ee091cf1Smatthias.ringwald                 int used = 0;
226ee091cf1Smatthias.ringwald                 for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
227ee091cf1Smatthias.ringwald                     channel = (l2cap_channel_t *) it;
228ee091cf1Smatthias.ringwald                     if (channel->handle == handle) {
229ee091cf1Smatthias.ringwald                         used = 1;
230ee091cf1Smatthias.ringwald                     }
231ee091cf1Smatthias.ringwald                 }
232ee091cf1Smatthias.ringwald                 if (!used) {
2339edc8742Smatthias.ringwald                     hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection
234ee091cf1Smatthias.ringwald                 }
235ee091cf1Smatthias.ringwald             }
236afde0c52Smatthias.ringwald             break;
237ee091cf1Smatthias.ringwald 
238afde0c52Smatthias.ringwald         default:
239afde0c52Smatthias.ringwald             break;
240afde0c52Smatthias.ringwald     }
241afde0c52Smatthias.ringwald 
242afde0c52Smatthias.ringwald     // pass on
243fcadd0caSmatthias.ringwald     (*event_packet_handler)(packet, size);
2441e6aba47Smatthias.ringwald }
2451e6aba47Smatthias.ringwald 
246afde0c52Smatthias.ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){
24784836b65Smatthias.ringwald     l2cap_send_signaling_packet( channel->handle, DISCONNECTION_RESPONSE, identifier, channel->dest_cid, channel->source_cid);
24884836b65Smatthias.ringwald     l2cap_finialize_channel_close(channel);
24984836b65Smatthias.ringwald }
25084836b65Smatthias.ringwald 
251645658c9Smatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t dest_cid){
252645658c9Smatthias.ringwald 
253169f8b28Smatthias.ringwald     // printf("l2cap_handle_connection_request for handle %u, psm %u cid %u\n", handle, psm, dest_cid);
254645658c9Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
255645658c9Smatthias.ringwald     if (!service) {
256645658c9Smatthias.ringwald         // 0x0002 PSM not supported
257169f8b28Smatthias.ringwald         // printf("l2cap_handle_connection_request no PSM for psm %u/n", psm);
258645658c9Smatthias.ringwald         l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, 0x0002, 0);
259645658c9Smatthias.ringwald         return;
260645658c9Smatthias.ringwald     }
261645658c9Smatthias.ringwald 
262645658c9Smatthias.ringwald     hci_connection_t * hci_connection = connection_for_handle( handle );
263645658c9Smatthias.ringwald     if (!hci_connection) {
264169f8b28Smatthias.ringwald         fprintf(stderr, "no hci_connection for handle %u\n", handle);
265645658c9Smatthias.ringwald         // TODO: emit error
266645658c9Smatthias.ringwald         return;
267645658c9Smatthias.ringwald     }
268645658c9Smatthias.ringwald     // alloc structure
269169f8b28Smatthias.ringwald     // printf("l2cap_handle_connection_request register channel\n");
270169f8b28Smatthias.ringwald     l2cap_channel_t * channel = malloc(sizeof(l2cap_channel_t));
271645658c9Smatthias.ringwald     // TODO: emit error event
272169f8b28Smatthias.ringwald     if (!channel) return;
273645658c9Smatthias.ringwald 
274645658c9Smatthias.ringwald     // fill in
275169f8b28Smatthias.ringwald     BD_ADDR_COPY(channel->address, hci_connection->address);
276169f8b28Smatthias.ringwald     channel->psm = psm;
277169f8b28Smatthias.ringwald     channel->handle = handle;
278169f8b28Smatthias.ringwald     channel->connection = service->connection;
279169f8b28Smatthias.ringwald     channel->source_cid = l2cap_next_source_cid();
280169f8b28Smatthias.ringwald     channel->dest_cid   = dest_cid;
281645658c9Smatthias.ringwald 
282645658c9Smatthias.ringwald     // set initial state
283e405ae81Smatthias.ringwald     channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT;
284e405ae81Smatthias.ringwald 
285e405ae81Smatthias.ringwald     // temp. store req sig id
286e405ae81Smatthias.ringwald     channel->sig_id = sig_id;
287645658c9Smatthias.ringwald 
288645658c9Smatthias.ringwald     // add to connections list
289169f8b28Smatthias.ringwald     linked_list_add(&l2cap_channels, (linked_item_t *) channel);
290645658c9Smatthias.ringwald 
291e405ae81Smatthias.ringwald     // emit incoming connection request
292e405ae81Smatthias.ringwald     l2cap_emit_connection_request(channel);
293e405ae81Smatthias.ringwald }
294645658c9Smatthias.ringwald 
295e405ae81Smatthias.ringwald void l2cap_accept_connection_internal(uint16_t source_cid){
296e405ae81Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_source_cid(source_cid);
297e405ae81Smatthias.ringwald     if (!channel) {
298e405ae81Smatthias.ringwald         fprintf(stderr, "l2cap_accept_connection_internal called but source_cid 0x%x not found", source_cid);
299e405ae81Smatthias.ringwald         return;
300e405ae81Smatthias.ringwald     }
301e405ae81Smatthias.ringwald 
302e405ae81Smatthias.ringwald     // accept connection
303e405ae81Smatthias.ringwald     l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->sig_id, channel->dest_cid, channel->source_cid, 0, 0);
304e405ae81Smatthias.ringwald 
305e405ae81Smatthias.ringwald     // set real sig and state and start config
306e405ae81Smatthias.ringwald     channel->sig_id = l2cap_next_sig_id();
307e405ae81Smatthias.ringwald     channel->state  = L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ;
308169f8b28Smatthias.ringwald     l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->sig_id, channel->dest_cid, 0, 4, &config_options);
309e405ae81Smatthias.ringwald }
310645658c9Smatthias.ringwald 
311e405ae81Smatthias.ringwald void l2cap_decline_connection_internal(uint16_t source_cid, uint8_t reason){
312e405ae81Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_source_cid( source_cid);
313e405ae81Smatthias.ringwald     if (!channel) {
314e405ae81Smatthias.ringwald         fprintf(stderr, "l2cap_decline_connection_internal called but source_cid 0x%x not found", source_cid,reason);
315e405ae81Smatthias.ringwald         return;
316e405ae81Smatthias.ringwald     }
317e405ae81Smatthias.ringwald     l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->sig_id, 0, 0, reason, 0);
318e405ae81Smatthias.ringwald 
319e405ae81Smatthias.ringwald     // discard channel
320e405ae81Smatthias.ringwald     linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
321e405ae81Smatthias.ringwald     free (channel);
322645658c9Smatthias.ringwald }
323645658c9Smatthias.ringwald 
3241e6aba47Smatthias.ringwald void l2cap_signaling_handler(l2cap_channel_t *channel, uint8_t *packet, uint16_t size){
3251e6aba47Smatthias.ringwald 
3261e6aba47Smatthias.ringwald     uint8_t  code       = READ_L2CAP_SIGNALING_CODE( packet );
3271e6aba47Smatthias.ringwald     uint8_t  identifier = READ_L2CAP_SIGNALING_IDENTIFIER( packet );
32838e5900eSmatthias.ringwald     uint16_t result = 0;
3291e6aba47Smatthias.ringwald 
3301e6aba47Smatthias.ringwald     switch (channel->state) {
3311e6aba47Smatthias.ringwald 
3321e6aba47Smatthias.ringwald         case L2CAP_STATE_WAIT_CONNECT_RSP:
3331e6aba47Smatthias.ringwald             switch (code){
3341e6aba47Smatthias.ringwald                 case CONNECTION_RESPONSE:
335141679a4Smatthias.ringwald                     result = READ_BT_16 (packet, L2CAP_SIGNALING_DATA_OFFSET+4);
33638e5900eSmatthias.ringwald                     switch (result) {
33738e5900eSmatthias.ringwald                         case 0:
338169f8b28Smatthias.ringwald                             // successful connection
339cd56f931Smatthias.ringwald                             channel->dest_cid = READ_BT_16(packet, L2CAP_SIGNALING_DATA_OFFSET);
3401e6aba47Smatthias.ringwald                             channel->sig_id = l2cap_next_sig_id();
3411e6aba47Smatthias.ringwald                             l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->sig_id, channel->dest_cid, 0, 4, &config_options);
3425a67bd4aSmatthias.ringwald                             channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ;
34338e5900eSmatthias.ringwald                             break;
34438e5900eSmatthias.ringwald                         case 1:
34538e5900eSmatthias.ringwald                             // connection pending. get some coffee
34638e5900eSmatthias.ringwald                             break;
34738e5900eSmatthias.ringwald                         default:
348f32b992eSmatthias.ringwald                             // map l2cap connection response result to BTstack status enumeration
34938e5900eSmatthias.ringwald                             l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result);
35038e5900eSmatthias.ringwald                             break;
3511e6aba47Smatthias.ringwald                     }
3521e6aba47Smatthias.ringwald                     break;
35338e5900eSmatthias.ringwald 
35484836b65Smatthias.ringwald                 case DISCONNECTION_REQUEST:
35584836b65Smatthias.ringwald                     l2cap_handle_disconnect_request(channel, identifier);
35684836b65Smatthias.ringwald                     break;
35784836b65Smatthias.ringwald 
35838e5900eSmatthias.ringwald                 default:
3591e6aba47Smatthias.ringwald                     //@TODO: implement other signaling packets
36038e5900eSmatthias.ringwald                     break;
3611e6aba47Smatthias.ringwald             }
3621e6aba47Smatthias.ringwald             break;
3631e6aba47Smatthias.ringwald 
3645a67bd4aSmatthias.ringwald         case L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ:
3651e6aba47Smatthias.ringwald             switch (code) {
3661e6aba47Smatthias.ringwald                 case CONFIGURE_RESPONSE:
3671e6aba47Smatthias.ringwald                     channel->state = L2CAP_STATE_WAIT_CONFIG_REQ;
3681e6aba47Smatthias.ringwald                     break;
3695a67bd4aSmatthias.ringwald                 case CONFIGURE_REQUEST:
3705a67bd4aSmatthias.ringwald                     // accept the other's configuration options
3715a67bd4aSmatthias.ringwald                     l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, identifier, channel->dest_cid, 0, 0, size - 16, &packet[16]);
3725a67bd4aSmatthias.ringwald                     channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP;
3735a67bd4aSmatthias.ringwald                     break;
37484836b65Smatthias.ringwald                 case DISCONNECTION_REQUEST:
37584836b65Smatthias.ringwald                     l2cap_handle_disconnect_request(channel, identifier);
37684836b65Smatthias.ringwald                     break;
3775a67bd4aSmatthias.ringwald                 default:
3785a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
3795a67bd4aSmatthias.ringwald                     break;
3801e6aba47Smatthias.ringwald             }
3811e6aba47Smatthias.ringwald             break;
3821e6aba47Smatthias.ringwald 
3831e6aba47Smatthias.ringwald         case L2CAP_STATE_WAIT_CONFIG_REQ:
3841e6aba47Smatthias.ringwald             switch (code) {
3851e6aba47Smatthias.ringwald                 case CONFIGURE_REQUEST:
3861e6aba47Smatthias.ringwald                     // accept the other's configuration options
3871e6aba47Smatthias.ringwald                     l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, identifier, channel->dest_cid, 0, 0, size - 16, &packet[16]);
3881e6aba47Smatthias.ringwald                     channel->state = L2CAP_STATE_OPEN;
38903cfbabcSmatthias.ringwald                     l2cap_emit_channel_opened(channel, 0);  // success
390c8e4258aSmatthias.ringwald                     break;
39184836b65Smatthias.ringwald                 case DISCONNECTION_REQUEST:
39284836b65Smatthias.ringwald                     l2cap_handle_disconnect_request(channel, identifier);
39384836b65Smatthias.ringwald                     break;
3945a67bd4aSmatthias.ringwald                 default:
3955a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
3965a67bd4aSmatthias.ringwald                     break;
3975a67bd4aSmatthias.ringwald             }
3985a67bd4aSmatthias.ringwald             break;
3995a67bd4aSmatthias.ringwald 
4005a67bd4aSmatthias.ringwald         case L2CAP_STATE_WAIT_CONFIG_REQ_RSP:
4015a67bd4aSmatthias.ringwald             switch (code) {
4025a67bd4aSmatthias.ringwald                 case CONFIGURE_RESPONSE:
4035a67bd4aSmatthias.ringwald                     channel->state = L2CAP_STATE_OPEN;
4045a67bd4aSmatthias.ringwald                     l2cap_emit_channel_opened(channel, 0);  // success
4055a67bd4aSmatthias.ringwald                     break;
40684836b65Smatthias.ringwald                 case DISCONNECTION_REQUEST:
40784836b65Smatthias.ringwald                     l2cap_handle_disconnect_request(channel, identifier);
40884836b65Smatthias.ringwald                     break;
4095a67bd4aSmatthias.ringwald                 default:
4105a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
4115a67bd4aSmatthias.ringwald                     break;
412c8e4258aSmatthias.ringwald             }
413c8e4258aSmatthias.ringwald             break;
414f62db1e3Smatthias.ringwald 
415f62db1e3Smatthias.ringwald         case L2CAP_STATE_WAIT_DISCONNECT:
416f62db1e3Smatthias.ringwald             switch (code) {
417f62db1e3Smatthias.ringwald                 case DISCONNECTION_RESPONSE:
41827a923d0Smatthias.ringwald                     l2cap_finialize_channel_close(channel);
41927a923d0Smatthias.ringwald                     break;
42084836b65Smatthias.ringwald                 case DISCONNECTION_REQUEST:
42184836b65Smatthias.ringwald                     l2cap_handle_disconnect_request(channel, identifier);
42284836b65Smatthias.ringwald                     break;
4235a67bd4aSmatthias.ringwald                 default:
4245a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
4255a67bd4aSmatthias.ringwald                     break;
42627a923d0Smatthias.ringwald             }
42727a923d0Smatthias.ringwald             break;
42884836b65Smatthias.ringwald 
42984836b65Smatthias.ringwald         case L2CAP_STATE_CLOSED:
43084836b65Smatthias.ringwald             // @TODO handle incoming requests
43184836b65Smatthias.ringwald             break;
43284836b65Smatthias.ringwald 
43384836b65Smatthias.ringwald         case L2CAP_STATE_OPEN:
43484836b65Smatthias.ringwald             switch (code) {
43584836b65Smatthias.ringwald                 case DISCONNECTION_REQUEST:
43684836b65Smatthias.ringwald                     l2cap_handle_disconnect_request(channel, identifier);
43784836b65Smatthias.ringwald                     break;
4385a67bd4aSmatthias.ringwald                 default:
43984836b65Smatthias.ringwald                     //@TODO: implement other signaling packets, e.g. re-configure
44084836b65Smatthias.ringwald                     break;
44184836b65Smatthias.ringwald             }
4425a67bd4aSmatthias.ringwald             break;
44327a923d0Smatthias.ringwald     }
44427a923d0Smatthias.ringwald }
44527a923d0Smatthias.ringwald 
44627a923d0Smatthias.ringwald // finalize closed channel
44727a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t *channel){
448f62db1e3Smatthias.ringwald     channel->state = L2CAP_STATE_CLOSED;
449f62db1e3Smatthias.ringwald     l2cap_emit_channel_closed(channel);
450f62db1e3Smatthias.ringwald 
451f62db1e3Smatthias.ringwald     // discard channel
452f62db1e3Smatthias.ringwald     linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
453f62db1e3Smatthias.ringwald     free (channel);
454c8e4258aSmatthias.ringwald }
4551e6aba47Smatthias.ringwald 
4569d9bbc01Smatthias.ringwald l2cap_service_t * l2cap_get_service(uint16_t psm){
457c52bf64dSmatthias.ringwald     linked_item_t *it;
4589d9bbc01Smatthias.ringwald 
4599d9bbc01Smatthias.ringwald     // close open channels
4609d9bbc01Smatthias.ringwald     for (it = (linked_item_t *) l2cap_services; it ; it = it->next){
4619d9bbc01Smatthias.ringwald         l2cap_service_t * service = ((l2cap_service_t *) it);
4629d9bbc01Smatthias.ringwald         if ( service->psm == psm){
4639d9bbc01Smatthias.ringwald             return service;
4649d9bbc01Smatthias.ringwald         };
4659d9bbc01Smatthias.ringwald     }
4669d9bbc01Smatthias.ringwald     return NULL;
4679d9bbc01Smatthias.ringwald }
4689d9bbc01Smatthias.ringwald 
469116ee617Smatthias.ringwald void l2cap_register_service_internal(connection_t *connection, uint16_t psm, uint16_t mtu){
4709d9bbc01Smatthias.ringwald     // check for alread registered psm // TODO: emit error event
4719d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
4729d9bbc01Smatthias.ringwald     if (service) return;
4739d9bbc01Smatthias.ringwald 
4749d9bbc01Smatthias.ringwald     // alloc structure     // TODO: emit error event
4759d9bbc01Smatthias.ringwald     service = malloc(sizeof(l2cap_service_t));
4769d9bbc01Smatthias.ringwald     if (!service) return;
4779d9bbc01Smatthias.ringwald 
4789d9bbc01Smatthias.ringwald     // fill in
4799d9bbc01Smatthias.ringwald     service->psm = psm;
4809d9bbc01Smatthias.ringwald     service->mtu = mtu;
4819d9bbc01Smatthias.ringwald     service->connection = connection;
4829d9bbc01Smatthias.ringwald 
4839d9bbc01Smatthias.ringwald     // add to services list
4849d9bbc01Smatthias.ringwald     linked_list_add(&l2cap_services, (linked_item_t *) service);
4859d9bbc01Smatthias.ringwald }
4869d9bbc01Smatthias.ringwald 
487116ee617Smatthias.ringwald void l2cap_unregister_service_internal(connection_t *connection, uint16_t psm){
4889d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
4899d9bbc01Smatthias.ringwald     if (service) return;
4909d9bbc01Smatthias.ringwald     linked_list_remove(&l2cap_services, (linked_item_t *) service);
4919d9bbc01Smatthias.ringwald     free( service );
4929d9bbc01Smatthias.ringwald }
4939d9bbc01Smatthias.ringwald 
4949d9bbc01Smatthias.ringwald //
4959d9bbc01Smatthias.ringwald void l2cap_close_connection(connection_t *connection){
4969d9bbc01Smatthias.ringwald     linked_item_t *it;
4979d9bbc01Smatthias.ringwald 
4989d9bbc01Smatthias.ringwald     // close open channels
499c52bf64dSmatthias.ringwald     l2cap_channel_t * channel;
500c52bf64dSmatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
501c52bf64dSmatthias.ringwald         channel = (l2cap_channel_t *) it;
502c52bf64dSmatthias.ringwald         if (channel->connection == connection) {
503c52bf64dSmatthias.ringwald             channel->sig_id = l2cap_next_sig_id();
504c52bf64dSmatthias.ringwald             l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->sig_id, channel->dest_cid, channel->source_cid);
505c52bf64dSmatthias.ringwald             channel->state = L2CAP_STATE_WAIT_DISCONNECT;
506c52bf64dSmatthias.ringwald         }
507c52bf64dSmatthias.ringwald     }
5089d9bbc01Smatthias.ringwald 
509645658c9Smatthias.ringwald     // unregister services
510645658c9Smatthias.ringwald     l2cap_service_t *service;
511645658c9Smatthias.ringwald     for (it = (linked_item_t *) &l2cap_services; it ; it = it->next){
512645658c9Smatthias.ringwald         channel = (l2cap_channel_t *) it->next;
513645658c9Smatthias.ringwald         if (service->connection == connection){
514645658c9Smatthias.ringwald             it->next = it->next->next;
515645658c9Smatthias.ringwald             return;
516645658c9Smatthias.ringwald         }
517645658c9Smatthias.ringwald     }
518c52bf64dSmatthias.ringwald }
519c52bf64dSmatthias.ringwald 
5201e6aba47Smatthias.ringwald //  notify client
52103cfbabcSmatthias.ringwald void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) {
52203cfbabcSmatthias.ringwald     uint8_t event[17];
52380d52d6bSmatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_OPENED;
524c8e4258aSmatthias.ringwald     event[1] = sizeof(event) - 2;
52503cfbabcSmatthias.ringwald     event[2] = status;
52603cfbabcSmatthias.ringwald     bt_flip_addr(&event[3], channel->address);
52703cfbabcSmatthias.ringwald     bt_store_16(event,  9, channel->handle);
52803cfbabcSmatthias.ringwald     bt_store_16(event, 11, channel->psm);
52903cfbabcSmatthias.ringwald     bt_store_16(event, 13, channel->source_cid);
53003cfbabcSmatthias.ringwald     bt_store_16(event, 15, channel->dest_cid);
531*2289ec97Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
5321e6aba47Smatthias.ringwald     socket_connection_send_packet(channel->connection, HCI_EVENT_PACKET, 0, event, sizeof(event));
5331e6aba47Smatthias.ringwald }
5341e6aba47Smatthias.ringwald 
535f62db1e3Smatthias.ringwald void l2cap_emit_channel_closed(l2cap_channel_t *channel) {
536f62db1e3Smatthias.ringwald     uint8_t event[4];
53780d52d6bSmatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_CLOSED;
538f62db1e3Smatthias.ringwald     event[1] = sizeof(event) - 2;
539f62db1e3Smatthias.ringwald     bt_store_16(event, 2, channel->source_cid);
540*2289ec97Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
541f62db1e3Smatthias.ringwald     socket_connection_send_packet(channel->connection, HCI_EVENT_PACKET, 0, event, sizeof(event));
542f62db1e3Smatthias.ringwald }
543f62db1e3Smatthias.ringwald 
544e405ae81Smatthias.ringwald void l2cap_emit_connection_request(l2cap_channel_t *channel) {
545e405ae81Smatthias.ringwald     uint8_t event[16];
546e405ae81Smatthias.ringwald     event[0] = L2CAP_EVENT_INCOMING_CONNECTION;
547e405ae81Smatthias.ringwald     event[1] = sizeof(event) - 2;
548e405ae81Smatthias.ringwald     bt_flip_addr(&event[2], channel->address);
549e405ae81Smatthias.ringwald     bt_store_16(event,  8, channel->handle);
550e405ae81Smatthias.ringwald     bt_store_16(event, 10, channel->psm);
551e405ae81Smatthias.ringwald     bt_store_16(event, 12, channel->source_cid);
552e405ae81Smatthias.ringwald     bt_store_16(event, 14, channel->dest_cid);
553*2289ec97Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
554e405ae81Smatthias.ringwald     socket_connection_send_packet(channel->connection, HCI_EVENT_PACKET, 0, event, sizeof(event));
555e405ae81Smatthias.ringwald }
556*2289ec97Smatthias.ringwald 
5571e6aba47Smatthias.ringwald void l2cap_acl_handler( uint8_t *packet, uint16_t size ){
5581e6aba47Smatthias.ringwald 
5599edc8742Smatthias.ringwald     // Capturing?
5609edc8742Smatthias.ringwald     if (capture_connection) {
5619edc8742Smatthias.ringwald         socket_connection_send_packet(capture_connection, HCI_ACL_DATA_PACKET, 0, packet, size);
562d6f03c5dSmatthias.ringwald         return;
5639edc8742Smatthias.ringwald     }
5649edc8742Smatthias.ringwald 
5659edc8742Smatthias.ringwald     // forward to higher layers - not needed yet
5669edc8742Smatthias.ringwald     // (*data_packet_handler)(channel_id, packet, size);
5679edc8742Smatthias.ringwald 
5681e6aba47Smatthias.ringwald     // Get Channel ID and command code
5691e6aba47Smatthias.ringwald     uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet);
5701e6aba47Smatthias.ringwald     uint8_t  code       = READ_L2CAP_SIGNALING_CODE( packet );
5711e6aba47Smatthias.ringwald 
5721e6aba47Smatthias.ringwald     // Get Connection
5731e6aba47Smatthias.ringwald     hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet);
5741e6aba47Smatthias.ringwald 
575169f8b28Smatthias.ringwald     // printf("l2cap_acl_handler channel %u, code %u\n", channel_id, code);
576169f8b28Smatthias.ringwald 
5771e6aba47Smatthias.ringwald     // Signaling Packet?
5781e6aba47Smatthias.ringwald     if (channel_id == 1) {
5791e6aba47Smatthias.ringwald 
580169f8b28Smatthias.ringwald         if (code < 1 || code >= 8){
581169f8b28Smatthias.ringwald             // not for a particular channel, and not CONNECTION_REQUEST
5821e6aba47Smatthias.ringwald             return;
5831e6aba47Smatthias.ringwald         }
5841e6aba47Smatthias.ringwald 
585645658c9Smatthias.ringwald         // Get Signaling Identifier
5861e6aba47Smatthias.ringwald         uint8_t sig_id    = READ_L2CAP_SIGNALING_IDENTIFIER(packet);
587645658c9Smatthias.ringwald 
588645658c9Smatthias.ringwald         // CONNECTION_REQUEST
589645658c9Smatthias.ringwald         if (code == CONNECTION_REQUEST){
590645658c9Smatthias.ringwald             uint16_t psm =      READ_BT_16(packet, L2CAP_SIGNALING_DATA_OFFSET);
591645658c9Smatthias.ringwald             uint16_t dest_cid = READ_BT_16(packet, L2CAP_SIGNALING_DATA_OFFSET+2);
592645658c9Smatthias.ringwald             l2cap_handle_connection_request(handle, sig_id, psm, dest_cid);
593645658c9Smatthias.ringwald             return;
594645658c9Smatthias.ringwald         }
595645658c9Smatthias.ringwald 
596645658c9Smatthias.ringwald         // Get potential destination CID
5971e6aba47Smatthias.ringwald         uint16_t dest_cid = READ_BT_16(packet, L2CAP_SIGNALING_DATA_OFFSET);
5981e6aba47Smatthias.ringwald 
5991e6aba47Smatthias.ringwald         // Find channel for this sig_id and connection handle
6001e6aba47Smatthias.ringwald         linked_item_t *it;
6011e6aba47Smatthias.ringwald         for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
602b448a0e7Smatthias.ringwald             l2cap_channel_t * channel = (l2cap_channel_t *) it;
603b448a0e7Smatthias.ringwald             if (channel->handle == handle) {
6041e6aba47Smatthias.ringwald                 if (code & 1) {
6051e6aba47Smatthias.ringwald                     // match odd commands by previous signaling identifier
606b448a0e7Smatthias.ringwald                     if (channel->sig_id == sig_id) {
607b448a0e7Smatthias.ringwald                         l2cap_signaling_handler( channel, packet, size);
6081e6aba47Smatthias.ringwald                     }
6091e6aba47Smatthias.ringwald                 } else {
6101e6aba47Smatthias.ringwald                     // match even commands by source channel id
611b448a0e7Smatthias.ringwald                     if (channel->source_cid == dest_cid) {
612b448a0e7Smatthias.ringwald                         l2cap_signaling_handler( channel, packet, size);
6131e6aba47Smatthias.ringwald                     }
6141e6aba47Smatthias.ringwald                 }
6151e6aba47Smatthias.ringwald             }
6161e6aba47Smatthias.ringwald         }
6171e6aba47Smatthias.ringwald         return;
6181e6aba47Smatthias.ringwald     }
6191e6aba47Smatthias.ringwald 
6201e6aba47Smatthias.ringwald     // Find channel for this channel_id and connection handle
621f62db1e3Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_source_cid(channel_id);
622f62db1e3Smatthias.ringwald     if (channel) {
6236f60b3f4Smatthias.ringwald         socket_connection_send_packet(channel->connection, L2CAP_DATA_PACKET, channel_id,
6246f60b3f4Smatthias.ringwald                                       &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
6251e6aba47Smatthias.ringwald     }
6261e6aba47Smatthias.ringwald }
6271e6aba47Smatthias.ringwald 
628f62db1e3Smatthias.ringwald 
6291e6aba47Smatthias.ringwald void l2cap_send_internal(uint16_t source_cid, uint8_t *data, uint16_t len){
6301e6aba47Smatthias.ringwald     // find channel for source_cid, construct l2cap packet and send
631f62db1e3Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_source_cid(source_cid);
632fcadd0caSmatthias.ringwald     if (channel) {
6331e6aba47Smatthias.ringwald          // 0 - Connection handle : PB=10 : BC=00
6341e6aba47Smatthias.ringwald          bt_store_16(acl_buffer, 0, channel->handle | (2 << 12) | (0 << 14));
6351e6aba47Smatthias.ringwald          // 2 - ACL length
6361e6aba47Smatthias.ringwald          bt_store_16(acl_buffer, 2,  len + 4);
6371e6aba47Smatthias.ringwald          // 4 - L2CAP packet length
6381e6aba47Smatthias.ringwald          bt_store_16(acl_buffer, 4,  len + 0);
6391e6aba47Smatthias.ringwald          // 6 - L2CAP channel DEST
6401e6aba47Smatthias.ringwald          bt_store_16(acl_buffer, 6, channel->dest_cid);
6411e6aba47Smatthias.ringwald          // 8 - data
6421e6aba47Smatthias.ringwald          memcpy(&acl_buffer[8], data, len);
6431e6aba47Smatthias.ringwald          // send
6441e6aba47Smatthias.ringwald          hci_send_acl_packet(acl_buffer, len+8);
6451e6aba47Smatthias.ringwald      }
6461e6aba47Smatthias.ringwald }
6471e6aba47Smatthias.ringwald 
6489edc8742Smatthias.ringwald void l2cap_set_capture_connection(connection_t * connection){
6499edc8742Smatthias.ringwald     capture_connection = connection;
6509edc8742Smatthias.ringwald }
6511e6aba47Smatthias.ringwald 
652b448a0e7Smatthias.ringwald