xref: /btstack/src/l2cap.c (revision 38e5900edab6d6f4185c46c15f3c021bfbd9781c)
143625864Smatthias.ringwald /*
243625864Smatthias.ringwald  *  l2cap.c
343625864Smatthias.ringwald  *
443625864Smatthias.ringwald  *  Logical Link Control and Adaption Protocl (L2CAP)
543625864Smatthias.ringwald  *
643625864Smatthias.ringwald  *  Created by Matthias Ringwald on 5/16/09.
743625864Smatthias.ringwald  */
843625864Smatthias.ringwald 
943625864Smatthias.ringwald #include "l2cap.h"
1043625864Smatthias.ringwald 
1143625864Smatthias.ringwald #include <stdarg.h>
1243625864Smatthias.ringwald #include <string.h>
1343625864Smatthias.ringwald 
1443625864Smatthias.ringwald #include <stdio.h>
1543625864Smatthias.ringwald 
166f60b3f4Smatthias.ringwald // size of HCI ACL + L2CAP Header for regular data packets
176f60b3f4Smatthias.ringwald #define COMPLETE_L2CAP_HEADER 8
186f60b3f4Smatthias.ringwald 
19fcadd0caSmatthias.ringwald static void null_event_handler(uint8_t *packet, uint16_t size);
20fcadd0caSmatthias.ringwald static void null_data_handler(uint16_t source_cid, uint8_t *packet, uint16_t size);
21fcadd0caSmatthias.ringwald 
221e6aba47Smatthias.ringwald static uint8_t * sig_buffer = NULL;
231e6aba47Smatthias.ringwald static linked_list_t l2cap_channels = NULL;
241e6aba47Smatthias.ringwald static uint8_t * acl_buffer = NULL;
25fcadd0caSmatthias.ringwald static void (*event_packet_handler) (uint8_t *packet, uint16_t size) = null_event_handler;
26fcadd0caSmatthias.ringwald static void (*data_packet_handler)  (uint16_t source_cid, uint8_t *packet, uint16_t size) = null_data_handler;
279edc8742Smatthias.ringwald static connection_t * capture_connection = NULL;
281e6aba47Smatthias.ringwald 
291e6aba47Smatthias.ringwald void l2cap_init(){
301e6aba47Smatthias.ringwald     sig_buffer = malloc( 48 );
311e6aba47Smatthias.ringwald     acl_buffer = malloc( 255 + 8 );
32fcadd0caSmatthias.ringwald 
33fcadd0caSmatthias.ringwald     //
34fcadd0caSmatthias.ringwald     // register callbacks with HCI
35fcadd0caSmatthias.ringwald     //
36fcadd0caSmatthias.ringwald     hci_register_event_packet_handler(&l2cap_event_handler);
37fcadd0caSmatthias.ringwald     hci_register_acl_packet_handler(&l2cap_acl_handler);
38fcadd0caSmatthias.ringwald }
39fcadd0caSmatthias.ringwald 
40fcadd0caSmatthias.ringwald 
41fcadd0caSmatthias.ringwald /** Register L2CAP packet handlers */
42fcadd0caSmatthias.ringwald static void null_event_handler(uint8_t *packet, uint16_t size){
43fcadd0caSmatthias.ringwald }
44fcadd0caSmatthias.ringwald static void null_data_handler(uint16_t  source_cid, uint8_t *packet, uint16_t size){
45fcadd0caSmatthias.ringwald }
46fcadd0caSmatthias.ringwald void l2cap_register_event_packet_handler(void (*handler)(uint8_t *packet, uint16_t size)){
47fcadd0caSmatthias.ringwald     event_packet_handler = handler;
48fcadd0caSmatthias.ringwald }
49fcadd0caSmatthias.ringwald void l2cap_register_data_packet_handler  (void (*handler)(uint16_t source_cid, uint8_t *packet, uint16_t size)){
50fcadd0caSmatthias.ringwald     data_packet_handler = handler;
511e6aba47Smatthias.ringwald }
521e6aba47Smatthias.ringwald 
530af41d30Smatthias.ringwald int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){
540af41d30Smatthias.ringwald     va_list argptr;
550af41d30Smatthias.ringwald     va_start(argptr, identifier);
560af41d30Smatthias.ringwald     uint16_t len = l2cap_create_signaling_internal(sig_buffer, handle, cmd, identifier, argptr);
571e6aba47Smatthias.ringwald     va_end(argptr);
580af41d30Smatthias.ringwald     return hci_send_acl_packet(sig_buffer, len);
590af41d30Smatthias.ringwald }
600af41d30Smatthias.ringwald 
61f62db1e3Smatthias.ringwald l2cap_channel_t * l2cap_get_channel_for_source_cid(uint16_t source_cid){
62f62db1e3Smatthias.ringwald     linked_item_t *it;
63f62db1e3Smatthias.ringwald     l2cap_channel_t * channel;
64f62db1e3Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
65f62db1e3Smatthias.ringwald         channel = (l2cap_channel_t *) it;
66f62db1e3Smatthias.ringwald         if ( channel->source_cid == source_cid) {
67f62db1e3Smatthias.ringwald             return channel;
68f62db1e3Smatthias.ringwald         }
69f62db1e3Smatthias.ringwald     }
70f62db1e3Smatthias.ringwald     return NULL;
71f62db1e3Smatthias.ringwald }
72f62db1e3Smatthias.ringwald 
731e6aba47Smatthias.ringwald // open outgoing L2CAP channel
741e6aba47Smatthias.ringwald void l2cap_create_channel_internal(connection_t * connection, bd_addr_t address, uint16_t psm){
751e6aba47Smatthias.ringwald 
761e6aba47Smatthias.ringwald     // alloc structure
771e6aba47Smatthias.ringwald     l2cap_channel_t * chan = malloc(sizeof(l2cap_channel_t));
781e6aba47Smatthias.ringwald     // TODO: emit error event
791e6aba47Smatthias.ringwald     if (!chan) return;
801e6aba47Smatthias.ringwald 
811e6aba47Smatthias.ringwald     // fill in
821e6aba47Smatthias.ringwald     BD_ADDR_COPY(chan->address, address);
831e6aba47Smatthias.ringwald     chan->psm = psm;
841e6aba47Smatthias.ringwald     chan->handle = 0;
851e6aba47Smatthias.ringwald     chan->connection = connection;
861e6aba47Smatthias.ringwald 
871e6aba47Smatthias.ringwald     // set initial state
881e6aba47Smatthias.ringwald     chan->state = L2CAP_STATE_CLOSED;
891e6aba47Smatthias.ringwald     chan->sig_id = L2CAP_SIG_ID_INVALID;
901e6aba47Smatthias.ringwald 
911e6aba47Smatthias.ringwald     // add to connections list
921e6aba47Smatthias.ringwald     linked_list_add(&l2cap_channels, (linked_item_t *) chan);
931e6aba47Smatthias.ringwald 
941e6aba47Smatthias.ringwald     // send connection request
951e6aba47Smatthias.ringwald     // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch
961e6aba47Smatthias.ringwald     hci_send_cmd(&hci_create_connection, address, 0x18, 0, 0, 0, 0);
9743625864Smatthias.ringwald }
9843625864Smatthias.ringwald 
991e6aba47Smatthias.ringwald void l2cap_disconnect_internal(uint16_t source_cid, uint8_t reason){
100f62db1e3Smatthias.ringwald     // find channel for source_cid
101f62db1e3Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_source_cid(source_cid);
102f62db1e3Smatthias.ringwald     if (channel) {
103f62db1e3Smatthias.ringwald         channel->sig_id = l2cap_next_sig_id();
104f62db1e3Smatthias.ringwald         l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->sig_id, channel->dest_cid, channel->source_cid);
105f62db1e3Smatthias.ringwald         channel->state = L2CAP_STATE_WAIT_DISCONNECT;
106f62db1e3Smatthias.ringwald     }
10743625864Smatthias.ringwald }
1081e6aba47Smatthias.ringwald 
1091e6aba47Smatthias.ringwald 
1101e6aba47Smatthias.ringwald void l2cap_event_handler( uint8_t *packet, uint16_t size ){
1111e6aba47Smatthias.ringwald     // handle connection complete events
11203cfbabcSmatthias.ringwald     if (packet[0] == HCI_EVENT_CONNECTION_COMPLETE) {
1131e6aba47Smatthias.ringwald         bd_addr_t address;
1141e6aba47Smatthias.ringwald         bt_flip_addr(address, &packet[5]);
1151e6aba47Smatthias.ringwald 
1161e6aba47Smatthias.ringwald         linked_item_t *it;
1171e6aba47Smatthias.ringwald         for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
1181e6aba47Smatthias.ringwald             l2cap_channel_t * chan = (l2cap_channel_t *) it;
1191e6aba47Smatthias.ringwald             if ( ! BD_ADDR_CMP( chan->address, address) ){
1201e6aba47Smatthias.ringwald                 if (chan->state == L2CAP_STATE_CLOSED) {
12103cfbabcSmatthias.ringwald                     if (packet[2] == 0){
1221e6aba47Smatthias.ringwald                         chan->handle = READ_BT_16(packet, 3);
1231e6aba47Smatthias.ringwald                         chan->sig_id = l2cap_next_sig_id();
1241e6aba47Smatthias.ringwald                         chan->source_cid = l2cap_next_source_cid();
1251e6aba47Smatthias.ringwald 
1261e6aba47Smatthias.ringwald                         l2cap_send_signaling_packet( chan->handle, CONNECTION_REQUEST, chan->sig_id, chan->psm, chan->source_cid);
1271e6aba47Smatthias.ringwald 
1281e6aba47Smatthias.ringwald                         chan->state = L2CAP_STATE_WAIT_CONNECT_RSP;
12903cfbabcSmatthias.ringwald                     } else {
13003cfbabcSmatthias.ringwald                         l2cap_emit_channel_opened(chan, packet[2]);  // failure, forward error code
13103cfbabcSmatthias.ringwald                     }
1321e6aba47Smatthias.ringwald                 }
1331e6aba47Smatthias.ringwald             }
1341e6aba47Smatthias.ringwald         }
1351e6aba47Smatthias.ringwald     }
13627a923d0Smatthias.ringwald 
1371e6aba47Smatthias.ringwald     // handle disconnection complete events
13827a923d0Smatthias.ringwald     if (packet[0] == HCI_EVENT_DISCONNECTION_COMPLETE) {
13927a923d0Smatthias.ringwald         // send l2cap disconnect events for all channels on this handle
14027a923d0Smatthias.ringwald         hci_con_handle_t handle = READ_BT_16(packet, 3);
14127a923d0Smatthias.ringwald         linked_item_t *it;
14227a923d0Smatthias.ringwald         for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
14327a923d0Smatthias.ringwald             l2cap_channel_t * channel = (l2cap_channel_t *) it;
14427a923d0Smatthias.ringwald             if ( channel->handle == handle ){
14527a923d0Smatthias.ringwald                 l2cap_finialize_channel_close(channel);
14627a923d0Smatthias.ringwald             }
14727a923d0Smatthias.ringwald         }
14827a923d0Smatthias.ringwald     }
149fcadd0caSmatthias.ringwald 
150ee091cf1Smatthias.ringwald     // HCI Connection Timeouts
15180d52d6bSmatthias.ringwald     if (packet[0] == L2CAP_EVENT_TIMEOUT_CHECK){
152ee091cf1Smatthias.ringwald         hci_con_handle_t handle = READ_BT_16(packet, 2);
153ee091cf1Smatthias.ringwald         linked_item_t *it;
154ee091cf1Smatthias.ringwald         l2cap_channel_t * channel;
155ee091cf1Smatthias.ringwald         int used = 0;
156ee091cf1Smatthias.ringwald         for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
157ee091cf1Smatthias.ringwald             channel = (l2cap_channel_t *) it;
158ee091cf1Smatthias.ringwald             if (channel->handle == handle) {
159ee091cf1Smatthias.ringwald                 used = 1;
160ee091cf1Smatthias.ringwald             }
161ee091cf1Smatthias.ringwald         }
162ee091cf1Smatthias.ringwald         if (!used) {
1639edc8742Smatthias.ringwald             hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection
164ee091cf1Smatthias.ringwald         }
165ee091cf1Smatthias.ringwald     }
166ee091cf1Smatthias.ringwald 
167fcadd0caSmatthias.ringwald     (*event_packet_handler)(packet, size);
1681e6aba47Smatthias.ringwald }
1691e6aba47Smatthias.ringwald 
1701e6aba47Smatthias.ringwald void l2cap_signaling_handler(l2cap_channel_t *channel, uint8_t *packet, uint16_t size){
1711e6aba47Smatthias.ringwald 
1721e6aba47Smatthias.ringwald     static uint8_t config_options[] = { 1, 2, 150, 0}; // mtu = 48
1731e6aba47Smatthias.ringwald 
1741e6aba47Smatthias.ringwald     uint8_t  code       = READ_L2CAP_SIGNALING_CODE( packet );
1751e6aba47Smatthias.ringwald     uint8_t  identifier = READ_L2CAP_SIGNALING_IDENTIFIER( packet );
176*38e5900eSmatthias.ringwald     uint16_t result = 0;
1771e6aba47Smatthias.ringwald 
1781e6aba47Smatthias.ringwald     switch (channel->state) {
1791e6aba47Smatthias.ringwald 
1801e6aba47Smatthias.ringwald         case L2CAP_STATE_WAIT_CONNECT_RSP:
1811e6aba47Smatthias.ringwald             switch (code){
1821e6aba47Smatthias.ringwald                 case CONNECTION_RESPONSE:
183*38e5900eSmatthias.ringwald                     result = READ_BT_16 (packet, L2CAP_SIGNALING_DATA_OFFSET+3);
184*38e5900eSmatthias.ringwald                     switch (result) {
185*38e5900eSmatthias.ringwald                         case 0:
1861e6aba47Smatthias.ringwald                             // successfull connection
1871e6aba47Smatthias.ringwald                             channel->dest_cid = READ_BT_16(packet, L2CAP_SIGNALING_DATA_OFFSET + 0);
1881e6aba47Smatthias.ringwald                             channel->sig_id = l2cap_next_sig_id();
1891e6aba47Smatthias.ringwald                             l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->sig_id, channel->dest_cid, 0, 4, &config_options);
1901e6aba47Smatthias.ringwald                             channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP;
191*38e5900eSmatthias.ringwald                             break;
192*38e5900eSmatthias.ringwald                         case 1:
193*38e5900eSmatthias.ringwald                             // connection pending. get some coffee
194*38e5900eSmatthias.ringwald                             break;
195*38e5900eSmatthias.ringwald                         default:
196f32b992eSmatthias.ringwald                             // map l2cap connection response result to BTstack status enumeration
197*38e5900eSmatthias.ringwald                             l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result);
198*38e5900eSmatthias.ringwald                             break;
1991e6aba47Smatthias.ringwald                     }
2001e6aba47Smatthias.ringwald                     break;
201*38e5900eSmatthias.ringwald 
202*38e5900eSmatthias.ringwald                 default:
2031e6aba47Smatthias.ringwald                     //@TODO: implement other signaling packets
204*38e5900eSmatthias.ringwald                     break;
2051e6aba47Smatthias.ringwald             }
2061e6aba47Smatthias.ringwald             break;
2071e6aba47Smatthias.ringwald 
2081e6aba47Smatthias.ringwald         case L2CAP_STATE_WAIT_CONFIG_REQ_RSP:
2091e6aba47Smatthias.ringwald             switch (code) {
2101e6aba47Smatthias.ringwald                 case CONFIGURE_RESPONSE:
2111e6aba47Smatthias.ringwald                     channel->state = L2CAP_STATE_WAIT_CONFIG_REQ;
2121e6aba47Smatthias.ringwald                     break;
2131e6aba47Smatthias.ringwald             }
2141e6aba47Smatthias.ringwald             break;
2151e6aba47Smatthias.ringwald 
2161e6aba47Smatthias.ringwald         case L2CAP_STATE_WAIT_CONFIG_REQ:
2171e6aba47Smatthias.ringwald             switch (code) {
2181e6aba47Smatthias.ringwald                 case CONFIGURE_REQUEST:
2191e6aba47Smatthias.ringwald 
2201e6aba47Smatthias.ringwald                     // accept the other's configuration options
2211e6aba47Smatthias.ringwald                     l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, identifier, channel->dest_cid, 0, 0, size - 16, &packet[16]);
2221e6aba47Smatthias.ringwald 
2231e6aba47Smatthias.ringwald                     channel->state = L2CAP_STATE_OPEN;
22403cfbabcSmatthias.ringwald                     l2cap_emit_channel_opened(channel, 0);  // success
225c8e4258aSmatthias.ringwald                     break;
226c8e4258aSmatthias.ringwald             }
227c8e4258aSmatthias.ringwald             break;
228f62db1e3Smatthias.ringwald 
229f62db1e3Smatthias.ringwald         case L2CAP_STATE_WAIT_DISCONNECT:
230f62db1e3Smatthias.ringwald             switch (code) {
231f62db1e3Smatthias.ringwald                 case DISCONNECTION_RESPONSE:
23227a923d0Smatthias.ringwald                     l2cap_finialize_channel_close(channel);
23327a923d0Smatthias.ringwald                     break;
23427a923d0Smatthias.ringwald             }
23527a923d0Smatthias.ringwald             break;
23627a923d0Smatthias.ringwald     }
23727a923d0Smatthias.ringwald }
23827a923d0Smatthias.ringwald 
23927a923d0Smatthias.ringwald // finalize closed channel
24027a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t *channel){
241f62db1e3Smatthias.ringwald     channel->state = L2CAP_STATE_CLOSED;
242f62db1e3Smatthias.ringwald     l2cap_emit_channel_closed(channel);
243f62db1e3Smatthias.ringwald 
244f62db1e3Smatthias.ringwald     // discard channel
245f62db1e3Smatthias.ringwald     linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
246f62db1e3Smatthias.ringwald     free (channel);
247c8e4258aSmatthias.ringwald }
2481e6aba47Smatthias.ringwald 
249c52bf64dSmatthias.ringwald //
250c52bf64dSmatthias.ringwald void l2cap_close_channels_for_connection(connection_t *connection){
251c52bf64dSmatthias.ringwald     linked_item_t *it;
252c52bf64dSmatthias.ringwald     l2cap_channel_t * channel;
253c52bf64dSmatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
254c52bf64dSmatthias.ringwald         channel = (l2cap_channel_t *) it;
255c52bf64dSmatthias.ringwald         if ( channel->connection == connection) {
256c52bf64dSmatthias.ringwald             channel->sig_id = l2cap_next_sig_id();
257c52bf64dSmatthias.ringwald             l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->sig_id, channel->dest_cid, channel->source_cid);
258c52bf64dSmatthias.ringwald             channel->state = L2CAP_STATE_WAIT_DISCONNECT;
259c52bf64dSmatthias.ringwald         }
260c52bf64dSmatthias.ringwald     }
261c52bf64dSmatthias.ringwald }
262c52bf64dSmatthias.ringwald 
2631e6aba47Smatthias.ringwald //  notify client
26403cfbabcSmatthias.ringwald void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) {
26503cfbabcSmatthias.ringwald     uint8_t event[17];
26680d52d6bSmatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_OPENED;
267c8e4258aSmatthias.ringwald     event[1] = sizeof(event) - 2;
26803cfbabcSmatthias.ringwald     event[2] = status;
26903cfbabcSmatthias.ringwald     bt_flip_addr(&event[3], channel->address);
27003cfbabcSmatthias.ringwald     bt_store_16(event,  9, channel->handle);
27103cfbabcSmatthias.ringwald     bt_store_16(event, 11, channel->psm);
27203cfbabcSmatthias.ringwald     bt_store_16(event, 13, channel->source_cid);
27303cfbabcSmatthias.ringwald     bt_store_16(event, 15, channel->dest_cid);
2741e6aba47Smatthias.ringwald     socket_connection_send_packet(channel->connection, HCI_EVENT_PACKET, 0, event, sizeof(event));
2751e6aba47Smatthias.ringwald }
2761e6aba47Smatthias.ringwald 
277f62db1e3Smatthias.ringwald void l2cap_emit_channel_closed(l2cap_channel_t *channel) {
278f62db1e3Smatthias.ringwald     uint8_t event[4];
27980d52d6bSmatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_CLOSED;
280f62db1e3Smatthias.ringwald     event[1] = sizeof(event) - 2;
281f62db1e3Smatthias.ringwald     bt_store_16(event, 2, channel->source_cid);
282f62db1e3Smatthias.ringwald     socket_connection_send_packet(channel->connection, HCI_EVENT_PACKET, 0, event, sizeof(event));
283f62db1e3Smatthias.ringwald }
284f62db1e3Smatthias.ringwald 
2851e6aba47Smatthias.ringwald void l2cap_acl_handler( uint8_t *packet, uint16_t size ){
2861e6aba47Smatthias.ringwald 
2879edc8742Smatthias.ringwald     // Capturing?
2889edc8742Smatthias.ringwald     if (capture_connection) {
2899edc8742Smatthias.ringwald         socket_connection_send_packet(capture_connection, HCI_ACL_DATA_PACKET, 0, packet, size);
2909edc8742Smatthias.ringwald     }
2919edc8742Smatthias.ringwald 
2929edc8742Smatthias.ringwald     // forward to higher layers - not needed yet
2939edc8742Smatthias.ringwald     // (*data_packet_handler)(channel_id, packet, size);
2949edc8742Smatthias.ringwald 
2951e6aba47Smatthias.ringwald     // Get Channel ID and command code
2961e6aba47Smatthias.ringwald     uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet);
2971e6aba47Smatthias.ringwald     uint8_t  code       = READ_L2CAP_SIGNALING_CODE( packet );
2981e6aba47Smatthias.ringwald 
2991e6aba47Smatthias.ringwald     // Get Connection
3001e6aba47Smatthias.ringwald     hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet);
3011e6aba47Smatthias.ringwald 
3021e6aba47Smatthias.ringwald     // Signaling Packet?
3031e6aba47Smatthias.ringwald     if (channel_id == 1) {
3041e6aba47Smatthias.ringwald 
3051e6aba47Smatthias.ringwald         if (code < 1 || code == 2 || code >= 8){
3061e6aba47Smatthias.ringwald             // not for a particular channel
3071e6aba47Smatthias.ringwald             return;
3081e6aba47Smatthias.ringwald         }
3091e6aba47Smatthias.ringwald 
3101e6aba47Smatthias.ringwald         // Get Signaling Identifier and potential destination CID
3111e6aba47Smatthias.ringwald         uint8_t sig_id    = READ_L2CAP_SIGNALING_IDENTIFIER(packet);
3121e6aba47Smatthias.ringwald         uint16_t dest_cid = READ_BT_16(packet, L2CAP_SIGNALING_DATA_OFFSET);
3131e6aba47Smatthias.ringwald 
3141e6aba47Smatthias.ringwald         // Find channel for this sig_id and connection handle
3151e6aba47Smatthias.ringwald         linked_item_t *it;
3161e6aba47Smatthias.ringwald         for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
3171e6aba47Smatthias.ringwald             l2cap_channel_t * chan = (l2cap_channel_t *) it;
3181e6aba47Smatthias.ringwald             if (chan->handle == handle) {
3191e6aba47Smatthias.ringwald                 if (code & 1) {
3201e6aba47Smatthias.ringwald                     // match odd commands by previous signaling identifier
3211e6aba47Smatthias.ringwald                     if (chan->sig_id == sig_id) {
3221e6aba47Smatthias.ringwald                         l2cap_signaling_handler( chan, packet, size);
3231e6aba47Smatthias.ringwald                     }
3241e6aba47Smatthias.ringwald                 } else {
3251e6aba47Smatthias.ringwald                     // match even commands by source channel id
3261e6aba47Smatthias.ringwald                     if (chan->source_cid == dest_cid) {
3271e6aba47Smatthias.ringwald                         l2cap_signaling_handler( chan, packet, size);
3281e6aba47Smatthias.ringwald                     }
3291e6aba47Smatthias.ringwald                 }
3301e6aba47Smatthias.ringwald             }
3311e6aba47Smatthias.ringwald         }
3321e6aba47Smatthias.ringwald         return;
3331e6aba47Smatthias.ringwald     }
3341e6aba47Smatthias.ringwald 
3351e6aba47Smatthias.ringwald     // Find channel for this channel_id and connection handle
336f62db1e3Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_source_cid(channel_id);
337f62db1e3Smatthias.ringwald     if (channel) {
3386f60b3f4Smatthias.ringwald         socket_connection_send_packet(channel->connection, L2CAP_DATA_PACKET, channel_id,
3396f60b3f4Smatthias.ringwald                                       &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
3401e6aba47Smatthias.ringwald     }
3411e6aba47Smatthias.ringwald }
3421e6aba47Smatthias.ringwald 
343f62db1e3Smatthias.ringwald 
3441e6aba47Smatthias.ringwald void l2cap_send_internal(uint16_t source_cid, uint8_t *data, uint16_t len){
3451e6aba47Smatthias.ringwald     // find channel for source_cid, construct l2cap packet and send
346f62db1e3Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_source_cid(source_cid);
347fcadd0caSmatthias.ringwald     if (channel) {
3481e6aba47Smatthias.ringwald          // 0 - Connection handle : PB=10 : BC=00
3491e6aba47Smatthias.ringwald          bt_store_16(acl_buffer, 0, channel->handle | (2 << 12) | (0 << 14));
3501e6aba47Smatthias.ringwald          // 2 - ACL length
3511e6aba47Smatthias.ringwald          bt_store_16(acl_buffer, 2,  len + 4);
3521e6aba47Smatthias.ringwald          // 4 - L2CAP packet length
3531e6aba47Smatthias.ringwald          bt_store_16(acl_buffer, 4,  len + 0);
3541e6aba47Smatthias.ringwald          // 6 - L2CAP channel DEST
3551e6aba47Smatthias.ringwald          bt_store_16(acl_buffer, 6, channel->dest_cid);
3561e6aba47Smatthias.ringwald          // 8 - data
3571e6aba47Smatthias.ringwald          memcpy(&acl_buffer[8], data, len);
3581e6aba47Smatthias.ringwald          // send
3591e6aba47Smatthias.ringwald          hci_send_acl_packet(acl_buffer, len+8);
3601e6aba47Smatthias.ringwald      }
3611e6aba47Smatthias.ringwald }
3621e6aba47Smatthias.ringwald 
3639edc8742Smatthias.ringwald void l2cap_set_capture_connection(connection_t * connection){
3649edc8742Smatthias.ringwald     capture_connection = connection;
3659edc8742Smatthias.ringwald }
3661e6aba47Smatthias.ringwald 
367