xref: /btstack/src/l2cap.c (revision b35f641c7f12d716b0e2e970eb512ae5fb5bd3f8)
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"
422b3c6c9bSmatthias.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);
53*b35f641cSmatthias.ringwald static void null_data_handler(uint16_t local_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;
60*b35f641cSmatthias.ringwald static void (*data_packet_handler)  (uint16_t local_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 }
80*b35f641cSmatthias.ringwald static void null_data_handler(uint16_t  local_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 }
85*b35f641cSmatthias.ringwald void l2cap_register_data_packet_handler  (void (*handler)(uint16_t local_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, ...){
90*b35f641cSmatthias.ringwald     // printf("l2cap_send_signaling_packet type %u\n", cmd);
910af41d30Smatthias.ringwald     va_list argptr;
920af41d30Smatthias.ringwald     va_start(argptr, identifier);
930af41d30Smatthias.ringwald     uint16_t len = l2cap_create_signaling_internal(sig_buffer, handle, cmd, identifier, argptr);
941e6aba47Smatthias.ringwald     va_end(argptr);
950af41d30Smatthias.ringwald     return hci_send_acl_packet(sig_buffer, len);
960af41d30Smatthias.ringwald }
970af41d30Smatthias.ringwald 
98*b35f641cSmatthias.ringwald l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid){
99f62db1e3Smatthias.ringwald     linked_item_t *it;
100f62db1e3Smatthias.ringwald     l2cap_channel_t * channel;
101f62db1e3Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
102f62db1e3Smatthias.ringwald         channel = (l2cap_channel_t *) it;
103*b35f641cSmatthias.ringwald         if ( channel->local_cid == local_cid) {
104f62db1e3Smatthias.ringwald             return channel;
105f62db1e3Smatthias.ringwald         }
106f62db1e3Smatthias.ringwald     }
107f62db1e3Smatthias.ringwald     return NULL;
108f62db1e3Smatthias.ringwald }
109f62db1e3Smatthias.ringwald 
1101e6aba47Smatthias.ringwald // open outgoing L2CAP channel
1111e6aba47Smatthias.ringwald void l2cap_create_channel_internal(connection_t * connection, bd_addr_t address, uint16_t psm){
1121e6aba47Smatthias.ringwald 
1131e6aba47Smatthias.ringwald     // alloc structure
1141e6aba47Smatthias.ringwald     l2cap_channel_t * chan = malloc(sizeof(l2cap_channel_t));
1151e6aba47Smatthias.ringwald     // TODO: emit error event
1161e6aba47Smatthias.ringwald     if (!chan) return;
1171e6aba47Smatthias.ringwald 
1181e6aba47Smatthias.ringwald     // fill in
1191e6aba47Smatthias.ringwald     BD_ADDR_COPY(chan->address, address);
1201e6aba47Smatthias.ringwald     chan->psm = psm;
1211e6aba47Smatthias.ringwald     chan->handle = 0;
1221e6aba47Smatthias.ringwald     chan->connection = connection;
1231e6aba47Smatthias.ringwald 
1241e6aba47Smatthias.ringwald     // set initial state
1251e6aba47Smatthias.ringwald     chan->state = L2CAP_STATE_CLOSED;
1261e6aba47Smatthias.ringwald     chan->sig_id = L2CAP_SIG_ID_INVALID;
1271e6aba47Smatthias.ringwald 
1281e6aba47Smatthias.ringwald     // add to connections list
1291e6aba47Smatthias.ringwald     linked_list_add(&l2cap_channels, (linked_item_t *) chan);
1301e6aba47Smatthias.ringwald 
1311e6aba47Smatthias.ringwald     // send connection request
1321e6aba47Smatthias.ringwald     // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch
1331e6aba47Smatthias.ringwald     hci_send_cmd(&hci_create_connection, address, 0x18, 0, 0, 0, 0);
13443625864Smatthias.ringwald }
13543625864Smatthias.ringwald 
136*b35f641cSmatthias.ringwald void l2cap_disconnect_internal(uint16_t local_cid, uint8_t reason){
137*b35f641cSmatthias.ringwald     // find channel for local_cid
138*b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
139f62db1e3Smatthias.ringwald     if (channel) {
140f62db1e3Smatthias.ringwald         channel->sig_id = l2cap_next_sig_id();
141*b35f641cSmatthias.ringwald         l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->sig_id, channel->remote_cid, channel->local_cid);
142f62db1e3Smatthias.ringwald         channel->state = L2CAP_STATE_WAIT_DISCONNECT;
143f62db1e3Smatthias.ringwald     }
14443625864Smatthias.ringwald }
1451e6aba47Smatthias.ringwald 
146afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){
1471e6aba47Smatthias.ringwald     linked_item_t *it;
1481e6aba47Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
149b448a0e7Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
150b448a0e7Smatthias.ringwald         if ( ! BD_ADDR_CMP( channel->address, address) ){
151b448a0e7Smatthias.ringwald             if (channel->state == L2CAP_STATE_CLOSED) {
152afde0c52Smatthias.ringwald                 // failure, forward error code
153afde0c52Smatthias.ringwald                 l2cap_emit_channel_opened(channel, status);
154afde0c52Smatthias.ringwald                 // discard channel
155afde0c52Smatthias.ringwald                 linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
156afde0c52Smatthias.ringwald                 free (channel);
157afde0c52Smatthias.ringwald             }
158afde0c52Smatthias.ringwald         }
159afde0c52Smatthias.ringwald     }
160afde0c52Smatthias.ringwald }
161afde0c52Smatthias.ringwald 
162afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){
163afde0c52Smatthias.ringwald     linked_item_t *it;
164afde0c52Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
165afde0c52Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
166afde0c52Smatthias.ringwald         if ( ! BD_ADDR_CMP( channel->address, address) ){
167afde0c52Smatthias.ringwald             if (channel->state == L2CAP_STATE_CLOSED) {
168b448a0e7Smatthias.ringwald                 // success, start l2cap handshake
169afde0c52Smatthias.ringwald                 channel->handle = handle;
170b448a0e7Smatthias.ringwald                 channel->sig_id = l2cap_next_sig_id();
171*b35f641cSmatthias.ringwald                 channel->local_cid = l2cap_next_local_cid();
172b448a0e7Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECT_RSP;
173*b35f641cSmatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, CONNECTION_REQUEST, channel->sig_id, channel->psm, channel->local_cid);
174afde0c52Smatthias.ringwald             }
175afde0c52Smatthias.ringwald         }
176afde0c52Smatthias.ringwald     }
177afde0c52Smatthias.ringwald }
178b448a0e7Smatthias.ringwald 
179afde0c52Smatthias.ringwald void l2cap_event_handler( uint8_t *packet, uint16_t size ){
180afde0c52Smatthias.ringwald 
181afde0c52Smatthias.ringwald     bd_addr_t address;
182afde0c52Smatthias.ringwald     hci_con_handle_t handle;
183afde0c52Smatthias.ringwald 
184afde0c52Smatthias.ringwald     switch(packet[0]){
185afde0c52Smatthias.ringwald 
186afde0c52Smatthias.ringwald         // handle connection complete events
187afde0c52Smatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
188afde0c52Smatthias.ringwald             bt_flip_addr(address, &packet[5]);
189afde0c52Smatthias.ringwald             if (packet[2] == 0){
190afde0c52Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
191afde0c52Smatthias.ringwald                 l2cap_handle_connection_success_for_addr(address, handle);
192afde0c52Smatthias.ringwald             } else {
193afde0c52Smatthias.ringwald                 l2cap_handle_connection_failed_for_addr(address, packet[2]);
194afde0c52Smatthias.ringwald             }
195afde0c52Smatthias.ringwald             break;
196afde0c52Smatthias.ringwald 
197afde0c52Smatthias.ringwald         // handle successful create connection cancel command
198afde0c52Smatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
199afde0c52Smatthias.ringwald             if ( COMMAND_COMPLETE_EVENT(packet, hci_create_connection_cancel) ) {
200afde0c52Smatthias.ringwald                 if (packet[5] == 0){
201afde0c52Smatthias.ringwald                     bt_flip_addr(address, &packet[6]);
202afde0c52Smatthias.ringwald                     // CONNECTION TERMINATED BY LOCAL HOST (0X16)
203afde0c52Smatthias.ringwald                     l2cap_handle_connection_failed_for_addr(address, 0x16);
20403cfbabcSmatthias.ringwald                 }
2051e6aba47Smatthias.ringwald             }
206afde0c52Smatthias.ringwald             break;
20727a923d0Smatthias.ringwald 
2081e6aba47Smatthias.ringwald         // handle disconnection complete events
209afde0c52Smatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
21027a923d0Smatthias.ringwald             // send l2cap disconnect events for all channels on this handle
211afde0c52Smatthias.ringwald             handle = READ_BT_16(packet, 3);
21227a923d0Smatthias.ringwald             linked_item_t *it;
21327a923d0Smatthias.ringwald             for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
21427a923d0Smatthias.ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) it;
21527a923d0Smatthias.ringwald                 if ( channel->handle == handle ){
21627a923d0Smatthias.ringwald                     l2cap_finialize_channel_close(channel);
21727a923d0Smatthias.ringwald                 }
21827a923d0Smatthias.ringwald             }
219afde0c52Smatthias.ringwald             break;
220fcadd0caSmatthias.ringwald 
221ee091cf1Smatthias.ringwald         // HCI Connection Timeouts
222afde0c52Smatthias.ringwald         case L2CAP_EVENT_TIMEOUT_CHECK:
223afde0c52Smatthias.ringwald             if (!capture_connection){
224ee091cf1Smatthias.ringwald                 hci_con_handle_t handle = READ_BT_16(packet, 2);
225ee091cf1Smatthias.ringwald                 linked_item_t *it;
226ee091cf1Smatthias.ringwald                 l2cap_channel_t * channel;
227ee091cf1Smatthias.ringwald                 int used = 0;
228ee091cf1Smatthias.ringwald                 for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
229ee091cf1Smatthias.ringwald                     channel = (l2cap_channel_t *) it;
230ee091cf1Smatthias.ringwald                     if (channel->handle == handle) {
231ee091cf1Smatthias.ringwald                         used = 1;
232ee091cf1Smatthias.ringwald                     }
233ee091cf1Smatthias.ringwald                 }
234ee091cf1Smatthias.ringwald                 if (!used) {
2359edc8742Smatthias.ringwald                     hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection
236ee091cf1Smatthias.ringwald                 }
237ee091cf1Smatthias.ringwald             }
238afde0c52Smatthias.ringwald             break;
239ee091cf1Smatthias.ringwald 
240afde0c52Smatthias.ringwald         default:
241afde0c52Smatthias.ringwald             break;
242afde0c52Smatthias.ringwald     }
243afde0c52Smatthias.ringwald 
244afde0c52Smatthias.ringwald     // pass on
245fcadd0caSmatthias.ringwald     (*event_packet_handler)(packet, size);
2461e6aba47Smatthias.ringwald }
2471e6aba47Smatthias.ringwald 
248afde0c52Smatthias.ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){
249*b35f641cSmatthias.ringwald     l2cap_send_signaling_packet( channel->handle, DISCONNECTION_RESPONSE, identifier, channel->remote_cid, channel->local_cid);
25084836b65Smatthias.ringwald     l2cap_finialize_channel_close(channel);
25184836b65Smatthias.ringwald }
25284836b65Smatthias.ringwald 
253*b35f641cSmatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){
254645658c9Smatthias.ringwald 
255*b35f641cSmatthias.ringwald     // printf("l2cap_handle_connection_request for handle %u, psm %u cid %u\n", handle, psm, source_cid);
256645658c9Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
257645658c9Smatthias.ringwald     if (!service) {
258645658c9Smatthias.ringwald         // 0x0002 PSM not supported
259169f8b28Smatthias.ringwald         // printf("l2cap_handle_connection_request no PSM for psm %u/n", psm);
260645658c9Smatthias.ringwald         l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, 0x0002, 0);
261645658c9Smatthias.ringwald         return;
262645658c9Smatthias.ringwald     }
263645658c9Smatthias.ringwald 
264645658c9Smatthias.ringwald     hci_connection_t * hci_connection = connection_for_handle( handle );
265645658c9Smatthias.ringwald     if (!hci_connection) {
266169f8b28Smatthias.ringwald         fprintf(stderr, "no hci_connection for handle %u\n", handle);
267645658c9Smatthias.ringwald         // TODO: emit error
268645658c9Smatthias.ringwald         return;
269645658c9Smatthias.ringwald     }
270645658c9Smatthias.ringwald     // alloc structure
271169f8b28Smatthias.ringwald     // printf("l2cap_handle_connection_request register channel\n");
272169f8b28Smatthias.ringwald     l2cap_channel_t * channel = malloc(sizeof(l2cap_channel_t));
273645658c9Smatthias.ringwald     // TODO: emit error event
274169f8b28Smatthias.ringwald     if (!channel) return;
275645658c9Smatthias.ringwald 
276645658c9Smatthias.ringwald     // fill in
277169f8b28Smatthias.ringwald     BD_ADDR_COPY(channel->address, hci_connection->address);
278169f8b28Smatthias.ringwald     channel->psm = psm;
279169f8b28Smatthias.ringwald     channel->handle = handle;
280169f8b28Smatthias.ringwald     channel->connection = service->connection;
281*b35f641cSmatthias.ringwald     channel->local_cid  = l2cap_next_local_cid();
282*b35f641cSmatthias.ringwald     channel->remote_cid = source_cid;
283645658c9Smatthias.ringwald 
284645658c9Smatthias.ringwald     // set initial state
285e405ae81Smatthias.ringwald     channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT;
286e405ae81Smatthias.ringwald 
287e405ae81Smatthias.ringwald     // temp. store req sig id
288e405ae81Smatthias.ringwald     channel->sig_id = sig_id;
289645658c9Smatthias.ringwald 
290645658c9Smatthias.ringwald     // add to connections list
291169f8b28Smatthias.ringwald     linked_list_add(&l2cap_channels, (linked_item_t *) channel);
292645658c9Smatthias.ringwald 
293e405ae81Smatthias.ringwald     // emit incoming connection request
294e405ae81Smatthias.ringwald     l2cap_emit_connection_request(channel);
295e405ae81Smatthias.ringwald }
296645658c9Smatthias.ringwald 
297*b35f641cSmatthias.ringwald void l2cap_accept_connection_internal(uint16_t local_cid){
298*b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
299e405ae81Smatthias.ringwald     if (!channel) {
300*b35f641cSmatthias.ringwald         fprintf(stderr, "l2cap_accept_connection_internal called but local_cid 0x%x not found", local_cid);
301e405ae81Smatthias.ringwald         return;
302e405ae81Smatthias.ringwald     }
303e405ae81Smatthias.ringwald 
304e405ae81Smatthias.ringwald     // accept connection
305*b35f641cSmatthias.ringwald     l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->sig_id, channel->local_cid, channel->remote_cid, 0, 0);
306e405ae81Smatthias.ringwald 
307e405ae81Smatthias.ringwald     // set real sig and state and start config
308e405ae81Smatthias.ringwald     channel->sig_id = l2cap_next_sig_id();
309e405ae81Smatthias.ringwald     channel->state  = L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ;
310*b35f641cSmatthias.ringwald     l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->sig_id, channel->remote_cid, 0, 4, &config_options);
311*b35f641cSmatthias.ringwald 
312*b35f641cSmatthias.ringwald     // printf("new state %u\n", channel->state);
313e405ae81Smatthias.ringwald }
314645658c9Smatthias.ringwald 
315*b35f641cSmatthias.ringwald void l2cap_decline_connection_internal(uint16_t local_cid, uint8_t reason){
316*b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid);
317e405ae81Smatthias.ringwald     if (!channel) {
318*b35f641cSmatthias.ringwald         fprintf(stderr, "l2cap_decline_connection_internal called but local_cid 0x%x not found", local_cid,reason);
319e405ae81Smatthias.ringwald         return;
320e405ae81Smatthias.ringwald     }
321e405ae81Smatthias.ringwald     l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->sig_id, 0, 0, reason, 0);
322e405ae81Smatthias.ringwald 
323e405ae81Smatthias.ringwald     // discard channel
324e405ae81Smatthias.ringwald     linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
325e405ae81Smatthias.ringwald     free (channel);
326645658c9Smatthias.ringwald }
327645658c9Smatthias.ringwald 
3281e6aba47Smatthias.ringwald void l2cap_signaling_handler(l2cap_channel_t *channel, uint8_t *packet, uint16_t size){
3291e6aba47Smatthias.ringwald 
3301e6aba47Smatthias.ringwald     uint8_t  code       = READ_L2CAP_SIGNALING_CODE( packet );
3311e6aba47Smatthias.ringwald     uint8_t  identifier = READ_L2CAP_SIGNALING_IDENTIFIER( packet );
33238e5900eSmatthias.ringwald     uint16_t result = 0;
3331e6aba47Smatthias.ringwald 
334*b35f641cSmatthias.ringwald     // printf("signaling handler code %u\n", code);
335*b35f641cSmatthias.ringwald 
3361e6aba47Smatthias.ringwald     switch (channel->state) {
3371e6aba47Smatthias.ringwald 
3381e6aba47Smatthias.ringwald         case L2CAP_STATE_WAIT_CONNECT_RSP:
3391e6aba47Smatthias.ringwald             switch (code){
3401e6aba47Smatthias.ringwald                 case CONNECTION_RESPONSE:
341141679a4Smatthias.ringwald                     result = READ_BT_16 (packet, L2CAP_SIGNALING_DATA_OFFSET+4);
34238e5900eSmatthias.ringwald                     switch (result) {
34338e5900eSmatthias.ringwald                         case 0:
344169f8b28Smatthias.ringwald                             // successful connection
345*b35f641cSmatthias.ringwald                             channel->remote_cid = READ_BT_16(packet, L2CAP_SIGNALING_DATA_OFFSET);
3461e6aba47Smatthias.ringwald                             channel->sig_id = l2cap_next_sig_id();
347*b35f641cSmatthias.ringwald                             l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->sig_id, channel->remote_cid, 0, 4, &config_options);
3485a67bd4aSmatthias.ringwald                             channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ;
34938e5900eSmatthias.ringwald                             break;
35038e5900eSmatthias.ringwald                         case 1:
35138e5900eSmatthias.ringwald                             // connection pending. get some coffee
35238e5900eSmatthias.ringwald                             break;
35338e5900eSmatthias.ringwald                         default:
354f32b992eSmatthias.ringwald                             // map l2cap connection response result to BTstack status enumeration
35538e5900eSmatthias.ringwald                             l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result);
35638e5900eSmatthias.ringwald                             break;
3571e6aba47Smatthias.ringwald                     }
3581e6aba47Smatthias.ringwald                     break;
35938e5900eSmatthias.ringwald 
36084836b65Smatthias.ringwald                 case DISCONNECTION_REQUEST:
36184836b65Smatthias.ringwald                     l2cap_handle_disconnect_request(channel, identifier);
36284836b65Smatthias.ringwald                     break;
36384836b65Smatthias.ringwald 
36438e5900eSmatthias.ringwald                 default:
3651e6aba47Smatthias.ringwald                     //@TODO: implement other signaling packets
36638e5900eSmatthias.ringwald                     break;
3671e6aba47Smatthias.ringwald             }
3681e6aba47Smatthias.ringwald             break;
3691e6aba47Smatthias.ringwald 
3705a67bd4aSmatthias.ringwald         case L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ:
3711e6aba47Smatthias.ringwald             switch (code) {
3721e6aba47Smatthias.ringwald                 case CONFIGURE_RESPONSE:
3731e6aba47Smatthias.ringwald                     channel->state = L2CAP_STATE_WAIT_CONFIG_REQ;
3741e6aba47Smatthias.ringwald                     break;
3755a67bd4aSmatthias.ringwald                 case CONFIGURE_REQUEST:
3765a67bd4aSmatthias.ringwald                     // accept the other's configuration options
377*b35f641cSmatthias.ringwald                     l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, identifier, channel->remote_cid, 0, 0, size - 16, &packet[16]);
3785a67bd4aSmatthias.ringwald                     channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP;
3795a67bd4aSmatthias.ringwald                     break;
38084836b65Smatthias.ringwald                 case DISCONNECTION_REQUEST:
38184836b65Smatthias.ringwald                     l2cap_handle_disconnect_request(channel, identifier);
38284836b65Smatthias.ringwald                     break;
3835a67bd4aSmatthias.ringwald                 default:
3845a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
3855a67bd4aSmatthias.ringwald                     break;
3861e6aba47Smatthias.ringwald             }
3871e6aba47Smatthias.ringwald             break;
3881e6aba47Smatthias.ringwald 
3891e6aba47Smatthias.ringwald         case L2CAP_STATE_WAIT_CONFIG_REQ:
3901e6aba47Smatthias.ringwald             switch (code) {
3911e6aba47Smatthias.ringwald                 case CONFIGURE_REQUEST:
3921e6aba47Smatthias.ringwald                     // accept the other's configuration options
393*b35f641cSmatthias.ringwald                     l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, identifier, channel->remote_cid, 0, 0, size - 16, &packet[16]);
3941e6aba47Smatthias.ringwald                     channel->state = L2CAP_STATE_OPEN;
39503cfbabcSmatthias.ringwald                     l2cap_emit_channel_opened(channel, 0);  // success
396c8e4258aSmatthias.ringwald                     break;
39784836b65Smatthias.ringwald                 case DISCONNECTION_REQUEST:
39884836b65Smatthias.ringwald                     l2cap_handle_disconnect_request(channel, identifier);
39984836b65Smatthias.ringwald                     break;
4005a67bd4aSmatthias.ringwald                 default:
4015a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
4025a67bd4aSmatthias.ringwald                     break;
4035a67bd4aSmatthias.ringwald             }
4045a67bd4aSmatthias.ringwald             break;
4055a67bd4aSmatthias.ringwald 
4065a67bd4aSmatthias.ringwald         case L2CAP_STATE_WAIT_CONFIG_REQ_RSP:
4075a67bd4aSmatthias.ringwald             switch (code) {
4085a67bd4aSmatthias.ringwald                 case CONFIGURE_RESPONSE:
4095a67bd4aSmatthias.ringwald                     channel->state = L2CAP_STATE_OPEN;
4105a67bd4aSmatthias.ringwald                     l2cap_emit_channel_opened(channel, 0);  // success
4115a67bd4aSmatthias.ringwald                     break;
41284836b65Smatthias.ringwald                 case DISCONNECTION_REQUEST:
41384836b65Smatthias.ringwald                     l2cap_handle_disconnect_request(channel, identifier);
41484836b65Smatthias.ringwald                     break;
4155a67bd4aSmatthias.ringwald                 default:
4165a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
4175a67bd4aSmatthias.ringwald                     break;
418c8e4258aSmatthias.ringwald             }
419c8e4258aSmatthias.ringwald             break;
420f62db1e3Smatthias.ringwald 
421f62db1e3Smatthias.ringwald         case L2CAP_STATE_WAIT_DISCONNECT:
422f62db1e3Smatthias.ringwald             switch (code) {
423f62db1e3Smatthias.ringwald                 case DISCONNECTION_RESPONSE:
42427a923d0Smatthias.ringwald                     l2cap_finialize_channel_close(channel);
42527a923d0Smatthias.ringwald                     break;
42684836b65Smatthias.ringwald                 case DISCONNECTION_REQUEST:
42784836b65Smatthias.ringwald                     l2cap_handle_disconnect_request(channel, identifier);
42884836b65Smatthias.ringwald                     break;
4295a67bd4aSmatthias.ringwald                 default:
4305a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
4315a67bd4aSmatthias.ringwald                     break;
43227a923d0Smatthias.ringwald             }
43327a923d0Smatthias.ringwald             break;
43484836b65Smatthias.ringwald 
43584836b65Smatthias.ringwald         case L2CAP_STATE_CLOSED:
43684836b65Smatthias.ringwald             // @TODO handle incoming requests
43784836b65Smatthias.ringwald             break;
43884836b65Smatthias.ringwald 
43984836b65Smatthias.ringwald         case L2CAP_STATE_OPEN:
44084836b65Smatthias.ringwald             switch (code) {
44184836b65Smatthias.ringwald                 case DISCONNECTION_REQUEST:
44284836b65Smatthias.ringwald                     l2cap_handle_disconnect_request(channel, identifier);
44384836b65Smatthias.ringwald                     break;
4445a67bd4aSmatthias.ringwald                 default:
44584836b65Smatthias.ringwald                     //@TODO: implement other signaling packets, e.g. re-configure
44684836b65Smatthias.ringwald                     break;
44784836b65Smatthias.ringwald             }
4485a67bd4aSmatthias.ringwald             break;
44927a923d0Smatthias.ringwald     }
450*b35f641cSmatthias.ringwald     // printf("new state %u\n", channel->state);
45127a923d0Smatthias.ringwald }
45227a923d0Smatthias.ringwald 
45327a923d0Smatthias.ringwald // finalize closed channel
45427a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t *channel){
455f62db1e3Smatthias.ringwald     channel->state = L2CAP_STATE_CLOSED;
456f62db1e3Smatthias.ringwald     l2cap_emit_channel_closed(channel);
457f62db1e3Smatthias.ringwald 
458f62db1e3Smatthias.ringwald     // discard channel
459f62db1e3Smatthias.ringwald     linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
460f62db1e3Smatthias.ringwald     free (channel);
461c8e4258aSmatthias.ringwald }
4621e6aba47Smatthias.ringwald 
4639d9bbc01Smatthias.ringwald l2cap_service_t * l2cap_get_service(uint16_t psm){
464c52bf64dSmatthias.ringwald     linked_item_t *it;
4659d9bbc01Smatthias.ringwald 
4669d9bbc01Smatthias.ringwald     // close open channels
4679d9bbc01Smatthias.ringwald     for (it = (linked_item_t *) l2cap_services; it ; it = it->next){
4689d9bbc01Smatthias.ringwald         l2cap_service_t * service = ((l2cap_service_t *) it);
4699d9bbc01Smatthias.ringwald         if ( service->psm == psm){
4709d9bbc01Smatthias.ringwald             return service;
4719d9bbc01Smatthias.ringwald         };
4729d9bbc01Smatthias.ringwald     }
4739d9bbc01Smatthias.ringwald     return NULL;
4749d9bbc01Smatthias.ringwald }
4759d9bbc01Smatthias.ringwald 
476116ee617Smatthias.ringwald void l2cap_register_service_internal(connection_t *connection, uint16_t psm, uint16_t mtu){
4779d9bbc01Smatthias.ringwald     // check for alread registered psm // TODO: emit error event
4789d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
4799d9bbc01Smatthias.ringwald     if (service) return;
4809d9bbc01Smatthias.ringwald 
4819d9bbc01Smatthias.ringwald     // alloc structure     // TODO: emit error event
4829d9bbc01Smatthias.ringwald     service = malloc(sizeof(l2cap_service_t));
4839d9bbc01Smatthias.ringwald     if (!service) return;
4849d9bbc01Smatthias.ringwald 
4859d9bbc01Smatthias.ringwald     // fill in
4869d9bbc01Smatthias.ringwald     service->psm = psm;
4879d9bbc01Smatthias.ringwald     service->mtu = mtu;
4889d9bbc01Smatthias.ringwald     service->connection = connection;
4899d9bbc01Smatthias.ringwald 
4909d9bbc01Smatthias.ringwald     // add to services list
4919d9bbc01Smatthias.ringwald     linked_list_add(&l2cap_services, (linked_item_t *) service);
4929d9bbc01Smatthias.ringwald }
4939d9bbc01Smatthias.ringwald 
494116ee617Smatthias.ringwald void l2cap_unregister_service_internal(connection_t *connection, uint16_t psm){
4959d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
4969d9bbc01Smatthias.ringwald     if (service) return;
4979d9bbc01Smatthias.ringwald     linked_list_remove(&l2cap_services, (linked_item_t *) service);
4989d9bbc01Smatthias.ringwald     free( service );
4999d9bbc01Smatthias.ringwald }
5009d9bbc01Smatthias.ringwald 
5019d9bbc01Smatthias.ringwald //
5029d9bbc01Smatthias.ringwald void l2cap_close_connection(connection_t *connection){
5039d9bbc01Smatthias.ringwald     linked_item_t *it;
5049d9bbc01Smatthias.ringwald 
5059d9bbc01Smatthias.ringwald     // close open channels
506c52bf64dSmatthias.ringwald     l2cap_channel_t * channel;
507c52bf64dSmatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
508c52bf64dSmatthias.ringwald         channel = (l2cap_channel_t *) it;
509c52bf64dSmatthias.ringwald         if (channel->connection == connection) {
510c52bf64dSmatthias.ringwald             channel->sig_id = l2cap_next_sig_id();
511*b35f641cSmatthias.ringwald             l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->sig_id, channel->remote_cid, channel->local_cid);
512c52bf64dSmatthias.ringwald             channel->state = L2CAP_STATE_WAIT_DISCONNECT;
513c52bf64dSmatthias.ringwald         }
514c52bf64dSmatthias.ringwald     }
5159d9bbc01Smatthias.ringwald 
516645658c9Smatthias.ringwald     // unregister services
517645658c9Smatthias.ringwald     l2cap_service_t *service;
518645658c9Smatthias.ringwald     for (it = (linked_item_t *) &l2cap_services; it ; it = it->next){
519645658c9Smatthias.ringwald         channel = (l2cap_channel_t *) it->next;
520645658c9Smatthias.ringwald         if (service->connection == connection){
521645658c9Smatthias.ringwald             it->next = it->next->next;
522645658c9Smatthias.ringwald             return;
523645658c9Smatthias.ringwald         }
524645658c9Smatthias.ringwald     }
525c52bf64dSmatthias.ringwald }
526c52bf64dSmatthias.ringwald 
5271e6aba47Smatthias.ringwald //  notify client
52803cfbabcSmatthias.ringwald void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) {
52903cfbabcSmatthias.ringwald     uint8_t event[17];
53080d52d6bSmatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_OPENED;
531c8e4258aSmatthias.ringwald     event[1] = sizeof(event) - 2;
53203cfbabcSmatthias.ringwald     event[2] = status;
53303cfbabcSmatthias.ringwald     bt_flip_addr(&event[3], channel->address);
53403cfbabcSmatthias.ringwald     bt_store_16(event,  9, channel->handle);
53503cfbabcSmatthias.ringwald     bt_store_16(event, 11, channel->psm);
536*b35f641cSmatthias.ringwald     bt_store_16(event, 13, channel->local_cid);
537*b35f641cSmatthias.ringwald     bt_store_16(event, 15, channel->remote_cid);
5382289ec97Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
5391e6aba47Smatthias.ringwald     socket_connection_send_packet(channel->connection, HCI_EVENT_PACKET, 0, event, sizeof(event));
5401e6aba47Smatthias.ringwald }
5411e6aba47Smatthias.ringwald 
542f62db1e3Smatthias.ringwald void l2cap_emit_channel_closed(l2cap_channel_t *channel) {
543f62db1e3Smatthias.ringwald     uint8_t event[4];
54480d52d6bSmatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_CLOSED;
545f62db1e3Smatthias.ringwald     event[1] = sizeof(event) - 2;
546*b35f641cSmatthias.ringwald     bt_store_16(event, 2, channel->local_cid);
5472289ec97Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
548f62db1e3Smatthias.ringwald     socket_connection_send_packet(channel->connection, HCI_EVENT_PACKET, 0, event, sizeof(event));
549f62db1e3Smatthias.ringwald }
550f62db1e3Smatthias.ringwald 
551e405ae81Smatthias.ringwald void l2cap_emit_connection_request(l2cap_channel_t *channel) {
552e405ae81Smatthias.ringwald     uint8_t event[16];
553e405ae81Smatthias.ringwald     event[0] = L2CAP_EVENT_INCOMING_CONNECTION;
554e405ae81Smatthias.ringwald     event[1] = sizeof(event) - 2;
555e405ae81Smatthias.ringwald     bt_flip_addr(&event[2], channel->address);
556e405ae81Smatthias.ringwald     bt_store_16(event,  8, channel->handle);
557e405ae81Smatthias.ringwald     bt_store_16(event, 10, channel->psm);
558*b35f641cSmatthias.ringwald     bt_store_16(event, 12, channel->local_cid);
559*b35f641cSmatthias.ringwald     bt_store_16(event, 14, channel->remote_cid);
5602289ec97Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
561e405ae81Smatthias.ringwald     socket_connection_send_packet(channel->connection, HCI_EVENT_PACKET, 0, event, sizeof(event));
562e405ae81Smatthias.ringwald }
5632289ec97Smatthias.ringwald 
5641e6aba47Smatthias.ringwald void l2cap_acl_handler( uint8_t *packet, uint16_t size ){
5651e6aba47Smatthias.ringwald 
5669edc8742Smatthias.ringwald     // Capturing?
5679edc8742Smatthias.ringwald     if (capture_connection) {
5689edc8742Smatthias.ringwald         socket_connection_send_packet(capture_connection, HCI_ACL_DATA_PACKET, 0, packet, size);
569d6f03c5dSmatthias.ringwald         return;
5709edc8742Smatthias.ringwald     }
5719edc8742Smatthias.ringwald 
5729edc8742Smatthias.ringwald     // forward to higher layers - not needed yet
5739edc8742Smatthias.ringwald     // (*data_packet_handler)(channel_id, packet, size);
5749edc8742Smatthias.ringwald 
5751e6aba47Smatthias.ringwald     // Get Channel ID and command code
5761e6aba47Smatthias.ringwald     uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet);
5771e6aba47Smatthias.ringwald     uint8_t  code       = READ_L2CAP_SIGNALING_CODE( packet );
5781e6aba47Smatthias.ringwald 
5791e6aba47Smatthias.ringwald     // Get Connection
5801e6aba47Smatthias.ringwald     hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet);
5811e6aba47Smatthias.ringwald 
582169f8b28Smatthias.ringwald     // printf("l2cap_acl_handler channel %u, code %u\n", channel_id, code);
583169f8b28Smatthias.ringwald 
5841e6aba47Smatthias.ringwald     // Signaling Packet?
5851e6aba47Smatthias.ringwald     if (channel_id == 1) {
5861e6aba47Smatthias.ringwald 
587169f8b28Smatthias.ringwald         if (code < 1 || code >= 8){
588169f8b28Smatthias.ringwald             // not for a particular channel, and not CONNECTION_REQUEST
5891e6aba47Smatthias.ringwald             return;
5901e6aba47Smatthias.ringwald         }
5911e6aba47Smatthias.ringwald 
592645658c9Smatthias.ringwald         // Get Signaling Identifier
5931e6aba47Smatthias.ringwald         uint8_t sig_id    = READ_L2CAP_SIGNALING_IDENTIFIER(packet);
594645658c9Smatthias.ringwald 
595645658c9Smatthias.ringwald         // CONNECTION_REQUEST
596645658c9Smatthias.ringwald         if (code == CONNECTION_REQUEST){
597645658c9Smatthias.ringwald             uint16_t psm =      READ_BT_16(packet, L2CAP_SIGNALING_DATA_OFFSET);
598*b35f641cSmatthias.ringwald             uint16_t source_cid = READ_BT_16(packet, L2CAP_SIGNALING_DATA_OFFSET+2);
599*b35f641cSmatthias.ringwald             l2cap_handle_connection_request(handle, sig_id, psm, source_cid);
600645658c9Smatthias.ringwald             return;
601645658c9Smatthias.ringwald         }
602645658c9Smatthias.ringwald 
603645658c9Smatthias.ringwald         // Get potential destination CID
6041e6aba47Smatthias.ringwald         uint16_t dest_cid = READ_BT_16(packet, L2CAP_SIGNALING_DATA_OFFSET);
6051e6aba47Smatthias.ringwald 
6061e6aba47Smatthias.ringwald         // Find channel for this sig_id and connection handle
6071e6aba47Smatthias.ringwald         linked_item_t *it;
6081e6aba47Smatthias.ringwald         for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
609b448a0e7Smatthias.ringwald             l2cap_channel_t * channel = (l2cap_channel_t *) it;
610b448a0e7Smatthias.ringwald             if (channel->handle == handle) {
6111e6aba47Smatthias.ringwald                 if (code & 1) {
6121e6aba47Smatthias.ringwald                     // match odd commands by previous signaling identifier
613b448a0e7Smatthias.ringwald                     if (channel->sig_id == sig_id) {
614b448a0e7Smatthias.ringwald                         l2cap_signaling_handler( channel, packet, size);
6151e6aba47Smatthias.ringwald                     }
6161e6aba47Smatthias.ringwald                 } else {
6171e6aba47Smatthias.ringwald                     // match even commands by source channel id
618*b35f641cSmatthias.ringwald                     if (channel->local_cid == dest_cid) {
619b448a0e7Smatthias.ringwald                         l2cap_signaling_handler( channel, packet, size);
6201e6aba47Smatthias.ringwald                     }
6211e6aba47Smatthias.ringwald                 }
6221e6aba47Smatthias.ringwald             }
6231e6aba47Smatthias.ringwald         }
6241e6aba47Smatthias.ringwald         return;
6251e6aba47Smatthias.ringwald     }
6261e6aba47Smatthias.ringwald 
6271e6aba47Smatthias.ringwald     // Find channel for this channel_id and connection handle
628*b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(channel_id);
629f62db1e3Smatthias.ringwald     if (channel) {
6306f60b3f4Smatthias.ringwald         socket_connection_send_packet(channel->connection, L2CAP_DATA_PACKET, channel_id,
6316f60b3f4Smatthias.ringwald                                       &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
6321e6aba47Smatthias.ringwald     }
6331e6aba47Smatthias.ringwald }
6341e6aba47Smatthias.ringwald 
635f62db1e3Smatthias.ringwald 
636*b35f641cSmatthias.ringwald void l2cap_send_internal(uint16_t local_cid, uint8_t *data, uint16_t len){
637*b35f641cSmatthias.ringwald     // find channel for local_cid, construct l2cap packet and send
638*b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
639fcadd0caSmatthias.ringwald     if (channel) {
6401e6aba47Smatthias.ringwald          // 0 - Connection handle : PB=10 : BC=00
6411e6aba47Smatthias.ringwald          bt_store_16(acl_buffer, 0, channel->handle | (2 << 12) | (0 << 14));
6421e6aba47Smatthias.ringwald          // 2 - ACL length
6431e6aba47Smatthias.ringwald          bt_store_16(acl_buffer, 2,  len + 4);
6441e6aba47Smatthias.ringwald          // 4 - L2CAP packet length
6451e6aba47Smatthias.ringwald          bt_store_16(acl_buffer, 4,  len + 0);
6461e6aba47Smatthias.ringwald          // 6 - L2CAP channel DEST
647*b35f641cSmatthias.ringwald          bt_store_16(acl_buffer, 6, channel->remote_cid);
6481e6aba47Smatthias.ringwald          // 8 - data
6491e6aba47Smatthias.ringwald          memcpy(&acl_buffer[8], data, len);
6501e6aba47Smatthias.ringwald          // send
6511e6aba47Smatthias.ringwald          hci_send_acl_packet(acl_buffer, len+8);
6521e6aba47Smatthias.ringwald      }
6531e6aba47Smatthias.ringwald }
6541e6aba47Smatthias.ringwald 
6559edc8742Smatthias.ringwald void l2cap_set_capture_connection(connection_t * connection){
6569edc8742Smatthias.ringwald     capture_connection = connection;
6579edc8742Smatthias.ringwald }
6581e6aba47Smatthias.ringwald 
659b448a0e7Smatthias.ringwald