xref: /btstack/src/l2cap.c (revision 645658c9de1b07ed00d498d8a09568bcf54a32ff)
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"
41*645658c9Smatthias.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 
621e6aba47Smatthias.ringwald void l2cap_init(){
631e6aba47Smatthias.ringwald     sig_buffer = malloc( 48 );
641e6aba47Smatthias.ringwald     acl_buffer = malloc( 255 + 8 );
65fcadd0caSmatthias.ringwald 
66fcadd0caSmatthias.ringwald     //
67fcadd0caSmatthias.ringwald     // register callbacks with HCI
68fcadd0caSmatthias.ringwald     //
69fcadd0caSmatthias.ringwald     hci_register_event_packet_handler(&l2cap_event_handler);
70fcadd0caSmatthias.ringwald     hci_register_acl_packet_handler(&l2cap_acl_handler);
71fcadd0caSmatthias.ringwald }
72fcadd0caSmatthias.ringwald 
73fcadd0caSmatthias.ringwald 
74fcadd0caSmatthias.ringwald /** Register L2CAP packet handlers */
75fcadd0caSmatthias.ringwald static void null_event_handler(uint8_t *packet, uint16_t size){
76fcadd0caSmatthias.ringwald }
77fcadd0caSmatthias.ringwald static void null_data_handler(uint16_t  source_cid, uint8_t *packet, uint16_t size){
78fcadd0caSmatthias.ringwald }
79fcadd0caSmatthias.ringwald void l2cap_register_event_packet_handler(void (*handler)(uint8_t *packet, uint16_t size)){
80fcadd0caSmatthias.ringwald     event_packet_handler = handler;
81fcadd0caSmatthias.ringwald }
82fcadd0caSmatthias.ringwald void l2cap_register_data_packet_handler  (void (*handler)(uint16_t source_cid, uint8_t *packet, uint16_t size)){
83fcadd0caSmatthias.ringwald     data_packet_handler = handler;
841e6aba47Smatthias.ringwald }
851e6aba47Smatthias.ringwald 
860af41d30Smatthias.ringwald int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){
870af41d30Smatthias.ringwald     va_list argptr;
880af41d30Smatthias.ringwald     va_start(argptr, identifier);
890af41d30Smatthias.ringwald     uint16_t len = l2cap_create_signaling_internal(sig_buffer, handle, cmd, identifier, argptr);
901e6aba47Smatthias.ringwald     va_end(argptr);
910af41d30Smatthias.ringwald     return hci_send_acl_packet(sig_buffer, len);
920af41d30Smatthias.ringwald }
930af41d30Smatthias.ringwald 
94f62db1e3Smatthias.ringwald l2cap_channel_t * l2cap_get_channel_for_source_cid(uint16_t source_cid){
95f62db1e3Smatthias.ringwald     linked_item_t *it;
96f62db1e3Smatthias.ringwald     l2cap_channel_t * channel;
97f62db1e3Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
98f62db1e3Smatthias.ringwald         channel = (l2cap_channel_t *) it;
99f62db1e3Smatthias.ringwald         if ( channel->source_cid == source_cid) {
100f62db1e3Smatthias.ringwald             return channel;
101f62db1e3Smatthias.ringwald         }
102f62db1e3Smatthias.ringwald     }
103f62db1e3Smatthias.ringwald     return NULL;
104f62db1e3Smatthias.ringwald }
105f62db1e3Smatthias.ringwald 
1061e6aba47Smatthias.ringwald // open outgoing L2CAP channel
1071e6aba47Smatthias.ringwald void l2cap_create_channel_internal(connection_t * connection, bd_addr_t address, uint16_t psm){
1081e6aba47Smatthias.ringwald 
1091e6aba47Smatthias.ringwald     // alloc structure
1101e6aba47Smatthias.ringwald     l2cap_channel_t * chan = malloc(sizeof(l2cap_channel_t));
1111e6aba47Smatthias.ringwald     // TODO: emit error event
1121e6aba47Smatthias.ringwald     if (!chan) return;
1131e6aba47Smatthias.ringwald 
1141e6aba47Smatthias.ringwald     // fill in
1151e6aba47Smatthias.ringwald     BD_ADDR_COPY(chan->address, address);
1161e6aba47Smatthias.ringwald     chan->psm = psm;
1171e6aba47Smatthias.ringwald     chan->handle = 0;
1181e6aba47Smatthias.ringwald     chan->connection = connection;
1191e6aba47Smatthias.ringwald 
1201e6aba47Smatthias.ringwald     // set initial state
1211e6aba47Smatthias.ringwald     chan->state = L2CAP_STATE_CLOSED;
1221e6aba47Smatthias.ringwald     chan->sig_id = L2CAP_SIG_ID_INVALID;
1231e6aba47Smatthias.ringwald 
1241e6aba47Smatthias.ringwald     // add to connections list
1251e6aba47Smatthias.ringwald     linked_list_add(&l2cap_channels, (linked_item_t *) chan);
1261e6aba47Smatthias.ringwald 
1271e6aba47Smatthias.ringwald     // send connection request
1281e6aba47Smatthias.ringwald     // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch
1291e6aba47Smatthias.ringwald     hci_send_cmd(&hci_create_connection, address, 0x18, 0, 0, 0, 0);
13043625864Smatthias.ringwald }
13143625864Smatthias.ringwald 
1321e6aba47Smatthias.ringwald void l2cap_disconnect_internal(uint16_t source_cid, uint8_t reason){
133f62db1e3Smatthias.ringwald     // find channel for source_cid
134f62db1e3Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_source_cid(source_cid);
135f62db1e3Smatthias.ringwald     if (channel) {
136f62db1e3Smatthias.ringwald         channel->sig_id = l2cap_next_sig_id();
137f62db1e3Smatthias.ringwald         l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->sig_id, channel->dest_cid, channel->source_cid);
138f62db1e3Smatthias.ringwald         channel->state = L2CAP_STATE_WAIT_DISCONNECT;
139f62db1e3Smatthias.ringwald     }
14043625864Smatthias.ringwald }
1411e6aba47Smatthias.ringwald 
142afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){
1431e6aba47Smatthias.ringwald     linked_item_t *it;
1441e6aba47Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
145b448a0e7Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
146b448a0e7Smatthias.ringwald         if ( ! BD_ADDR_CMP( channel->address, address) ){
147b448a0e7Smatthias.ringwald             if (channel->state == L2CAP_STATE_CLOSED) {
148afde0c52Smatthias.ringwald                 // failure, forward error code
149afde0c52Smatthias.ringwald                 l2cap_emit_channel_opened(channel, status);
150afde0c52Smatthias.ringwald                 // discard channel
151afde0c52Smatthias.ringwald                 linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
152afde0c52Smatthias.ringwald                 free (channel);
153afde0c52Smatthias.ringwald             }
154afde0c52Smatthias.ringwald         }
155afde0c52Smatthias.ringwald     }
156afde0c52Smatthias.ringwald }
157afde0c52Smatthias.ringwald 
158afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){
159afde0c52Smatthias.ringwald     linked_item_t *it;
160afde0c52Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
161afde0c52Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
162afde0c52Smatthias.ringwald         if ( ! BD_ADDR_CMP( channel->address, address) ){
163afde0c52Smatthias.ringwald             if (channel->state == L2CAP_STATE_CLOSED) {
164b448a0e7Smatthias.ringwald                 // success, start l2cap handshake
165afde0c52Smatthias.ringwald                 channel->handle = handle;
166b448a0e7Smatthias.ringwald                 channel->sig_id = l2cap_next_sig_id();
167b448a0e7Smatthias.ringwald                 channel->source_cid = l2cap_next_source_cid();
168b448a0e7Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECT_RSP;
169b448a0e7Smatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, CONNECTION_REQUEST, channel->sig_id, channel->psm, channel->source_cid);
170afde0c52Smatthias.ringwald             }
171afde0c52Smatthias.ringwald         }
172afde0c52Smatthias.ringwald     }
173afde0c52Smatthias.ringwald }
174b448a0e7Smatthias.ringwald 
175afde0c52Smatthias.ringwald void l2cap_event_handler( uint8_t *packet, uint16_t size ){
176afde0c52Smatthias.ringwald 
177afde0c52Smatthias.ringwald     bd_addr_t address;
178afde0c52Smatthias.ringwald     hci_con_handle_t handle;
179afde0c52Smatthias.ringwald 
180afde0c52Smatthias.ringwald     switch(packet[0]){
181afde0c52Smatthias.ringwald 
182afde0c52Smatthias.ringwald         // handle connection complete events
183afde0c52Smatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
184afde0c52Smatthias.ringwald             bt_flip_addr(address, &packet[5]);
185afde0c52Smatthias.ringwald             if (packet[2] == 0){
186afde0c52Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
187afde0c52Smatthias.ringwald                 l2cap_handle_connection_success_for_addr(address, handle);
188afde0c52Smatthias.ringwald             } else {
189afde0c52Smatthias.ringwald                 l2cap_handle_connection_failed_for_addr(address, packet[2]);
190afde0c52Smatthias.ringwald             }
191afde0c52Smatthias.ringwald             break;
192afde0c52Smatthias.ringwald 
193afde0c52Smatthias.ringwald         // handle successful create connection cancel command
194afde0c52Smatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
195afde0c52Smatthias.ringwald             if ( COMMAND_COMPLETE_EVENT(packet, hci_create_connection_cancel) ) {
196afde0c52Smatthias.ringwald                 if (packet[5] == 0){
197afde0c52Smatthias.ringwald                     bt_flip_addr(address, &packet[6]);
198afde0c52Smatthias.ringwald                     // CONNECTION TERMINATED BY LOCAL HOST (0X16)
199afde0c52Smatthias.ringwald                     l2cap_handle_connection_failed_for_addr(address, 0x16);
20003cfbabcSmatthias.ringwald                 }
2011e6aba47Smatthias.ringwald             }
202afde0c52Smatthias.ringwald             break;
20327a923d0Smatthias.ringwald 
2041e6aba47Smatthias.ringwald         // handle disconnection complete events
205afde0c52Smatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
20627a923d0Smatthias.ringwald             // send l2cap disconnect events for all channels on this handle
207afde0c52Smatthias.ringwald             handle = READ_BT_16(packet, 3);
20827a923d0Smatthias.ringwald             linked_item_t *it;
20927a923d0Smatthias.ringwald             for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
21027a923d0Smatthias.ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) it;
21127a923d0Smatthias.ringwald                 if ( channel->handle == handle ){
21227a923d0Smatthias.ringwald                     l2cap_finialize_channel_close(channel);
21327a923d0Smatthias.ringwald                 }
21427a923d0Smatthias.ringwald             }
215afde0c52Smatthias.ringwald             break;
216fcadd0caSmatthias.ringwald 
217ee091cf1Smatthias.ringwald         // HCI Connection Timeouts
218afde0c52Smatthias.ringwald         case L2CAP_EVENT_TIMEOUT_CHECK:
219afde0c52Smatthias.ringwald             if (!capture_connection){
220ee091cf1Smatthias.ringwald                 hci_con_handle_t handle = READ_BT_16(packet, 2);
221ee091cf1Smatthias.ringwald                 linked_item_t *it;
222ee091cf1Smatthias.ringwald                 l2cap_channel_t * channel;
223ee091cf1Smatthias.ringwald                 int used = 0;
224ee091cf1Smatthias.ringwald                 for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
225ee091cf1Smatthias.ringwald                     channel = (l2cap_channel_t *) it;
226ee091cf1Smatthias.ringwald                     if (channel->handle == handle) {
227ee091cf1Smatthias.ringwald                         used = 1;
228ee091cf1Smatthias.ringwald                     }
229ee091cf1Smatthias.ringwald                 }
230ee091cf1Smatthias.ringwald                 if (!used) {
2319edc8742Smatthias.ringwald                     hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection
232ee091cf1Smatthias.ringwald                 }
233ee091cf1Smatthias.ringwald             }
234afde0c52Smatthias.ringwald             break;
235ee091cf1Smatthias.ringwald 
236afde0c52Smatthias.ringwald         default:
237afde0c52Smatthias.ringwald             break;
238afde0c52Smatthias.ringwald     }
239afde0c52Smatthias.ringwald 
240afde0c52Smatthias.ringwald     // pass on
241fcadd0caSmatthias.ringwald     (*event_packet_handler)(packet, size);
2421e6aba47Smatthias.ringwald }
2431e6aba47Smatthias.ringwald 
244afde0c52Smatthias.ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){
24584836b65Smatthias.ringwald     l2cap_send_signaling_packet( channel->handle, DISCONNECTION_RESPONSE, identifier, channel->dest_cid, channel->source_cid);
24684836b65Smatthias.ringwald     l2cap_finialize_channel_close(channel);
24784836b65Smatthias.ringwald }
24884836b65Smatthias.ringwald 
249*645658c9Smatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t dest_cid){
250*645658c9Smatthias.ringwald 
251*645658c9Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
252*645658c9Smatthias.ringwald     if (!service) {
253*645658c9Smatthias.ringwald         // 0x0002 PSM not supported
254*645658c9Smatthias.ringwald         l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, 0x0002, 0);
255*645658c9Smatthias.ringwald         return;
256*645658c9Smatthias.ringwald     }
257*645658c9Smatthias.ringwald 
258*645658c9Smatthias.ringwald     hci_connection_t * hci_connection = connection_for_handle( handle );
259*645658c9Smatthias.ringwald     if (!hci_connection) {
260*645658c9Smatthias.ringwald         printf("no hci_connection for handle %u", handle);
261*645658c9Smatthias.ringwald         // TODO: emit error
262*645658c9Smatthias.ringwald         return;
263*645658c9Smatthias.ringwald     }
264*645658c9Smatthias.ringwald 
265*645658c9Smatthias.ringwald     // alloc structure
266*645658c9Smatthias.ringwald     l2cap_channel_t * chan = malloc(sizeof(l2cap_channel_t));
267*645658c9Smatthias.ringwald     // TODO: emit error event
268*645658c9Smatthias.ringwald     if (!chan) return;
269*645658c9Smatthias.ringwald 
270*645658c9Smatthias.ringwald     // fill in
271*645658c9Smatthias.ringwald     BD_ADDR_COPY(chan->address, hci_connection->address);
272*645658c9Smatthias.ringwald     chan->psm = psm;
273*645658c9Smatthias.ringwald     chan->handle = handle;
274*645658c9Smatthias.ringwald     chan->connection = service->connection;
275*645658c9Smatthias.ringwald     chan->source_cid = l2cap_next_source_cid();
276*645658c9Smatthias.ringwald     chan->dest_cid   = dest_cid;
277*645658c9Smatthias.ringwald 
278*645658c9Smatthias.ringwald     // set initial state
279*645658c9Smatthias.ringwald     chan->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ;
280*645658c9Smatthias.ringwald     chan->sig_id = sig_id;
281*645658c9Smatthias.ringwald 
282*645658c9Smatthias.ringwald     // add to connections list
283*645658c9Smatthias.ringwald     linked_list_add(&l2cap_channels, (linked_item_t *) chan);
284*645658c9Smatthias.ringwald 
285*645658c9Smatthias.ringwald     // TODO: emit incoming connection request instead of answering directly
286*645658c9Smatthias.ringwald 
287*645658c9Smatthias.ringwald     l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, dest_cid, chan->source_cid, 0, 0);
288*645658c9Smatthias.ringwald 
289*645658c9Smatthias.ringwald }
290*645658c9Smatthias.ringwald 
2911e6aba47Smatthias.ringwald void l2cap_signaling_handler(l2cap_channel_t *channel, uint8_t *packet, uint16_t size){
2921e6aba47Smatthias.ringwald 
2931e6aba47Smatthias.ringwald     static uint8_t config_options[] = { 1, 2, 150, 0}; // mtu = 48
2941e6aba47Smatthias.ringwald 
2951e6aba47Smatthias.ringwald     uint8_t  code       = READ_L2CAP_SIGNALING_CODE( packet );
2961e6aba47Smatthias.ringwald     uint8_t  identifier = READ_L2CAP_SIGNALING_IDENTIFIER( packet );
29738e5900eSmatthias.ringwald     uint16_t result = 0;
2981e6aba47Smatthias.ringwald 
2991e6aba47Smatthias.ringwald     switch (channel->state) {
3001e6aba47Smatthias.ringwald 
3011e6aba47Smatthias.ringwald         case L2CAP_STATE_WAIT_CONNECT_RSP:
3021e6aba47Smatthias.ringwald             switch (code){
3031e6aba47Smatthias.ringwald                 case CONNECTION_RESPONSE:
304141679a4Smatthias.ringwald                     result = READ_BT_16 (packet, L2CAP_SIGNALING_DATA_OFFSET+4);
30538e5900eSmatthias.ringwald                     switch (result) {
30638e5900eSmatthias.ringwald                         case 0:
3071e6aba47Smatthias.ringwald                             // successfull connection
308cd56f931Smatthias.ringwald                             channel->dest_cid = READ_BT_16(packet, L2CAP_SIGNALING_DATA_OFFSET);
3091e6aba47Smatthias.ringwald                             channel->sig_id = l2cap_next_sig_id();
3101e6aba47Smatthias.ringwald                             l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->sig_id, channel->dest_cid, 0, 4, &config_options);
3115a67bd4aSmatthias.ringwald                             channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ;
31238e5900eSmatthias.ringwald                             break;
31338e5900eSmatthias.ringwald                         case 1:
31438e5900eSmatthias.ringwald                             // connection pending. get some coffee
31538e5900eSmatthias.ringwald                             break;
31638e5900eSmatthias.ringwald                         default:
317f32b992eSmatthias.ringwald                             // map l2cap connection response result to BTstack status enumeration
31838e5900eSmatthias.ringwald                             l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result);
31938e5900eSmatthias.ringwald                             break;
3201e6aba47Smatthias.ringwald                     }
3211e6aba47Smatthias.ringwald                     break;
32238e5900eSmatthias.ringwald 
32384836b65Smatthias.ringwald                 case DISCONNECTION_REQUEST:
32484836b65Smatthias.ringwald                     l2cap_handle_disconnect_request(channel, identifier);
32584836b65Smatthias.ringwald                     break;
32684836b65Smatthias.ringwald 
32738e5900eSmatthias.ringwald                 default:
3281e6aba47Smatthias.ringwald                     //@TODO: implement other signaling packets
32938e5900eSmatthias.ringwald                     break;
3301e6aba47Smatthias.ringwald             }
3311e6aba47Smatthias.ringwald             break;
3321e6aba47Smatthias.ringwald 
3335a67bd4aSmatthias.ringwald         case L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ:
3341e6aba47Smatthias.ringwald             switch (code) {
3351e6aba47Smatthias.ringwald                 case CONFIGURE_RESPONSE:
3361e6aba47Smatthias.ringwald                     channel->state = L2CAP_STATE_WAIT_CONFIG_REQ;
3371e6aba47Smatthias.ringwald                     break;
3385a67bd4aSmatthias.ringwald                 case CONFIGURE_REQUEST:
3395a67bd4aSmatthias.ringwald                     // accept the other's configuration options
3405a67bd4aSmatthias.ringwald                     l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, identifier, channel->dest_cid, 0, 0, size - 16, &packet[16]);
3415a67bd4aSmatthias.ringwald                     channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP;
3425a67bd4aSmatthias.ringwald                     break;
34384836b65Smatthias.ringwald                 case DISCONNECTION_REQUEST:
34484836b65Smatthias.ringwald                     l2cap_handle_disconnect_request(channel, identifier);
34584836b65Smatthias.ringwald                     break;
3465a67bd4aSmatthias.ringwald                 default:
3475a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
3485a67bd4aSmatthias.ringwald                     break;
3491e6aba47Smatthias.ringwald             }
3501e6aba47Smatthias.ringwald             break;
3511e6aba47Smatthias.ringwald 
3521e6aba47Smatthias.ringwald         case L2CAP_STATE_WAIT_CONFIG_REQ:
3531e6aba47Smatthias.ringwald             switch (code) {
3541e6aba47Smatthias.ringwald                 case CONFIGURE_REQUEST:
3551e6aba47Smatthias.ringwald                     // accept the other's configuration options
3561e6aba47Smatthias.ringwald                     l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, identifier, channel->dest_cid, 0, 0, size - 16, &packet[16]);
3571e6aba47Smatthias.ringwald                     channel->state = L2CAP_STATE_OPEN;
35803cfbabcSmatthias.ringwald                     l2cap_emit_channel_opened(channel, 0);  // success
359c8e4258aSmatthias.ringwald                     break;
36084836b65Smatthias.ringwald                 case DISCONNECTION_REQUEST:
36184836b65Smatthias.ringwald                     l2cap_handle_disconnect_request(channel, identifier);
36284836b65Smatthias.ringwald                     break;
3635a67bd4aSmatthias.ringwald                 default:
3645a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
3655a67bd4aSmatthias.ringwald                     break;
3665a67bd4aSmatthias.ringwald             }
3675a67bd4aSmatthias.ringwald             break;
3685a67bd4aSmatthias.ringwald 
3695a67bd4aSmatthias.ringwald         case L2CAP_STATE_WAIT_CONFIG_REQ_RSP:
3705a67bd4aSmatthias.ringwald             switch (code) {
3715a67bd4aSmatthias.ringwald                 case CONFIGURE_RESPONSE:
3725a67bd4aSmatthias.ringwald                     channel->state = L2CAP_STATE_OPEN;
3735a67bd4aSmatthias.ringwald                     l2cap_emit_channel_opened(channel, 0);  // success
3745a67bd4aSmatthias.ringwald                     break;
37584836b65Smatthias.ringwald                 case DISCONNECTION_REQUEST:
37684836b65Smatthias.ringwald                     l2cap_handle_disconnect_request(channel, identifier);
37784836b65Smatthias.ringwald                     break;
3785a67bd4aSmatthias.ringwald                 default:
3795a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
3805a67bd4aSmatthias.ringwald                     break;
381c8e4258aSmatthias.ringwald             }
382c8e4258aSmatthias.ringwald             break;
383f62db1e3Smatthias.ringwald 
384f62db1e3Smatthias.ringwald         case L2CAP_STATE_WAIT_DISCONNECT:
385f62db1e3Smatthias.ringwald             switch (code) {
386f62db1e3Smatthias.ringwald                 case DISCONNECTION_RESPONSE:
38727a923d0Smatthias.ringwald                     l2cap_finialize_channel_close(channel);
38827a923d0Smatthias.ringwald                     break;
38984836b65Smatthias.ringwald                 case DISCONNECTION_REQUEST:
39084836b65Smatthias.ringwald                     l2cap_handle_disconnect_request(channel, identifier);
39184836b65Smatthias.ringwald                     break;
3925a67bd4aSmatthias.ringwald                 default:
3935a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
3945a67bd4aSmatthias.ringwald                     break;
39527a923d0Smatthias.ringwald             }
39627a923d0Smatthias.ringwald             break;
39784836b65Smatthias.ringwald 
39884836b65Smatthias.ringwald         case L2CAP_STATE_CLOSED:
39984836b65Smatthias.ringwald             // @TODO handle incoming requests
40084836b65Smatthias.ringwald             break;
40184836b65Smatthias.ringwald 
40284836b65Smatthias.ringwald         case L2CAP_STATE_OPEN:
40384836b65Smatthias.ringwald             switch (code) {
40484836b65Smatthias.ringwald                 case DISCONNECTION_REQUEST:
40584836b65Smatthias.ringwald                     l2cap_handle_disconnect_request(channel, identifier);
40684836b65Smatthias.ringwald                     break;
4075a67bd4aSmatthias.ringwald                 default:
40884836b65Smatthias.ringwald                     //@TODO: implement other signaling packets, e.g. re-configure
40984836b65Smatthias.ringwald                     break;
41084836b65Smatthias.ringwald             }
4115a67bd4aSmatthias.ringwald             break;
41227a923d0Smatthias.ringwald     }
41327a923d0Smatthias.ringwald }
41427a923d0Smatthias.ringwald 
41527a923d0Smatthias.ringwald // finalize closed channel
41627a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t *channel){
417f62db1e3Smatthias.ringwald     channel->state = L2CAP_STATE_CLOSED;
418f62db1e3Smatthias.ringwald     l2cap_emit_channel_closed(channel);
419f62db1e3Smatthias.ringwald 
420f62db1e3Smatthias.ringwald     // discard channel
421f62db1e3Smatthias.ringwald     linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
422f62db1e3Smatthias.ringwald     free (channel);
423c8e4258aSmatthias.ringwald }
4241e6aba47Smatthias.ringwald 
4259d9bbc01Smatthias.ringwald l2cap_service_t * l2cap_get_service(uint16_t psm){
426c52bf64dSmatthias.ringwald     linked_item_t *it;
4279d9bbc01Smatthias.ringwald 
4289d9bbc01Smatthias.ringwald     // close open channels
4299d9bbc01Smatthias.ringwald     for (it = (linked_item_t *) l2cap_services; it ; it = it->next){
4309d9bbc01Smatthias.ringwald         l2cap_service_t * service = ((l2cap_service_t *) it);
4319d9bbc01Smatthias.ringwald         if ( service->psm == psm){
4329d9bbc01Smatthias.ringwald             return service;
4339d9bbc01Smatthias.ringwald         };
4349d9bbc01Smatthias.ringwald     }
4359d9bbc01Smatthias.ringwald     return NULL;
4369d9bbc01Smatthias.ringwald }
4379d9bbc01Smatthias.ringwald 
438116ee617Smatthias.ringwald void l2cap_register_service_internal(connection_t *connection, uint16_t psm, uint16_t mtu){
4399d9bbc01Smatthias.ringwald     // check for alread registered psm // TODO: emit error event
4409d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
4419d9bbc01Smatthias.ringwald     if (service) return;
4429d9bbc01Smatthias.ringwald 
4439d9bbc01Smatthias.ringwald     // alloc structure     // TODO: emit error event
4449d9bbc01Smatthias.ringwald     service = malloc(sizeof(l2cap_service_t));
4459d9bbc01Smatthias.ringwald     if (!service) return;
4469d9bbc01Smatthias.ringwald 
4479d9bbc01Smatthias.ringwald     // fill in
4489d9bbc01Smatthias.ringwald     service->psm = psm;
4499d9bbc01Smatthias.ringwald     service->mtu = mtu;
4509d9bbc01Smatthias.ringwald     service->connection = connection;
4519d9bbc01Smatthias.ringwald 
4529d9bbc01Smatthias.ringwald     // add to services list
4539d9bbc01Smatthias.ringwald     linked_list_add(&l2cap_services, (linked_item_t *) service);
4549d9bbc01Smatthias.ringwald }
4559d9bbc01Smatthias.ringwald 
456116ee617Smatthias.ringwald void l2cap_unregister_service_internal(connection_t *connection, uint16_t psm){
4579d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
4589d9bbc01Smatthias.ringwald     if (service) return;
4599d9bbc01Smatthias.ringwald     linked_list_remove(&l2cap_services, (linked_item_t *) service);
4609d9bbc01Smatthias.ringwald     free( service );
4619d9bbc01Smatthias.ringwald }
4629d9bbc01Smatthias.ringwald 
4639d9bbc01Smatthias.ringwald //
4649d9bbc01Smatthias.ringwald void l2cap_close_connection(connection_t *connection){
4659d9bbc01Smatthias.ringwald     linked_item_t *it;
4669d9bbc01Smatthias.ringwald 
4679d9bbc01Smatthias.ringwald     // close open channels
468c52bf64dSmatthias.ringwald     l2cap_channel_t * channel;
469c52bf64dSmatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
470c52bf64dSmatthias.ringwald         channel = (l2cap_channel_t *) it;
471c52bf64dSmatthias.ringwald         if (channel->connection == connection) {
472c52bf64dSmatthias.ringwald             channel->sig_id = l2cap_next_sig_id();
473c52bf64dSmatthias.ringwald             l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->sig_id, channel->dest_cid, channel->source_cid);
474c52bf64dSmatthias.ringwald             channel->state = L2CAP_STATE_WAIT_DISCONNECT;
475c52bf64dSmatthias.ringwald         }
476c52bf64dSmatthias.ringwald     }
4779d9bbc01Smatthias.ringwald 
478*645658c9Smatthias.ringwald     // unregister services
479*645658c9Smatthias.ringwald     l2cap_service_t *service;
480*645658c9Smatthias.ringwald     for (it = (linked_item_t *) &l2cap_services; it ; it = it->next){
481*645658c9Smatthias.ringwald         channel = (l2cap_channel_t *) it->next;
482*645658c9Smatthias.ringwald         if (service->connection == connection){
483*645658c9Smatthias.ringwald             it->next = it->next->next;
484*645658c9Smatthias.ringwald             return;
485*645658c9Smatthias.ringwald         }
486*645658c9Smatthias.ringwald     }
487c52bf64dSmatthias.ringwald }
488c52bf64dSmatthias.ringwald 
4891f7b95a1Smatthias.ringwald void l2cap_accept_connection_internal(hci_con_handle_t handle,  uint16_t dest_cid){
4901f7b95a1Smatthias.ringwald     printf("l2cap_accept_connection_internal called but not implemented yet 0x%x, 0x%x\n", handle, dest_cid);
4911f7b95a1Smatthias.ringwald }
4921f7b95a1Smatthias.ringwald 
4931f7b95a1Smatthias.ringwald void l2cap_decline_connection_internal(hci_con_handle_t handle, uint16_t dest_cid, uint8_t reason){
4941f7b95a1Smatthias.ringwald     printf("l2cap_decline_connection_internal called but not implemented yet 0x%x, 0x%x, %u", handle, dest_cid,reason);
4951f7b95a1Smatthias.ringwald }
4961f7b95a1Smatthias.ringwald 
4971e6aba47Smatthias.ringwald //  notify client
49803cfbabcSmatthias.ringwald void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) {
49903cfbabcSmatthias.ringwald     uint8_t event[17];
50080d52d6bSmatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_OPENED;
501c8e4258aSmatthias.ringwald     event[1] = sizeof(event) - 2;
50203cfbabcSmatthias.ringwald     event[2] = status;
50303cfbabcSmatthias.ringwald     bt_flip_addr(&event[3], channel->address);
50403cfbabcSmatthias.ringwald     bt_store_16(event,  9, channel->handle);
50503cfbabcSmatthias.ringwald     bt_store_16(event, 11, channel->psm);
50603cfbabcSmatthias.ringwald     bt_store_16(event, 13, channel->source_cid);
50703cfbabcSmatthias.ringwald     bt_store_16(event, 15, channel->dest_cid);
5081e6aba47Smatthias.ringwald     socket_connection_send_packet(channel->connection, HCI_EVENT_PACKET, 0, event, sizeof(event));
5091e6aba47Smatthias.ringwald }
5101e6aba47Smatthias.ringwald 
511f62db1e3Smatthias.ringwald void l2cap_emit_channel_closed(l2cap_channel_t *channel) {
512f62db1e3Smatthias.ringwald     uint8_t event[4];
51380d52d6bSmatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_CLOSED;
514f62db1e3Smatthias.ringwald     event[1] = sizeof(event) - 2;
515f62db1e3Smatthias.ringwald     bt_store_16(event, 2, channel->source_cid);
516f62db1e3Smatthias.ringwald     socket_connection_send_packet(channel->connection, HCI_EVENT_PACKET, 0, event, sizeof(event));
517f62db1e3Smatthias.ringwald }
518f62db1e3Smatthias.ringwald 
5191e6aba47Smatthias.ringwald void l2cap_acl_handler( uint8_t *packet, uint16_t size ){
5201e6aba47Smatthias.ringwald 
5219edc8742Smatthias.ringwald     // Capturing?
5229edc8742Smatthias.ringwald     if (capture_connection) {
5239edc8742Smatthias.ringwald         socket_connection_send_packet(capture_connection, HCI_ACL_DATA_PACKET, 0, packet, size);
524d6f03c5dSmatthias.ringwald         return;
5259edc8742Smatthias.ringwald     }
5269edc8742Smatthias.ringwald 
5279edc8742Smatthias.ringwald     // forward to higher layers - not needed yet
5289edc8742Smatthias.ringwald     // (*data_packet_handler)(channel_id, packet, size);
5299edc8742Smatthias.ringwald 
5301e6aba47Smatthias.ringwald     // Get Channel ID and command code
5311e6aba47Smatthias.ringwald     uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet);
5321e6aba47Smatthias.ringwald     uint8_t  code       = READ_L2CAP_SIGNALING_CODE( packet );
5331e6aba47Smatthias.ringwald 
5341e6aba47Smatthias.ringwald     // Get Connection
5351e6aba47Smatthias.ringwald     hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet);
5361e6aba47Smatthias.ringwald 
5371e6aba47Smatthias.ringwald     // Signaling Packet?
5381e6aba47Smatthias.ringwald     if (channel_id == 1) {
5391e6aba47Smatthias.ringwald 
5401e6aba47Smatthias.ringwald         if (code < 1 || code == 2 || code >= 8){
5411e6aba47Smatthias.ringwald             // not for a particular channel
5421e6aba47Smatthias.ringwald             return;
5431e6aba47Smatthias.ringwald         }
5441e6aba47Smatthias.ringwald 
545*645658c9Smatthias.ringwald         // Get Signaling Identifier
5461e6aba47Smatthias.ringwald         uint8_t sig_id    = READ_L2CAP_SIGNALING_IDENTIFIER(packet);
547*645658c9Smatthias.ringwald 
548*645658c9Smatthias.ringwald         // CONNECTION_REQUEST
549*645658c9Smatthias.ringwald         if (code == CONNECTION_REQUEST){
550*645658c9Smatthias.ringwald             uint16_t psm =      READ_BT_16(packet, L2CAP_SIGNALING_DATA_OFFSET);
551*645658c9Smatthias.ringwald             uint16_t dest_cid = READ_BT_16(packet, L2CAP_SIGNALING_DATA_OFFSET+2);
552*645658c9Smatthias.ringwald             l2cap_handle_connection_request(handle, sig_id, psm, dest_cid);
553*645658c9Smatthias.ringwald             return;
554*645658c9Smatthias.ringwald         }
555*645658c9Smatthias.ringwald 
556*645658c9Smatthias.ringwald         // Get potential destination CID
5571e6aba47Smatthias.ringwald         uint16_t dest_cid = READ_BT_16(packet, L2CAP_SIGNALING_DATA_OFFSET);
5581e6aba47Smatthias.ringwald 
5591e6aba47Smatthias.ringwald         // Find channel for this sig_id and connection handle
5601e6aba47Smatthias.ringwald         linked_item_t *it;
5611e6aba47Smatthias.ringwald         for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
562b448a0e7Smatthias.ringwald             l2cap_channel_t * channel = (l2cap_channel_t *) it;
563b448a0e7Smatthias.ringwald             if (channel->handle == handle) {
5641e6aba47Smatthias.ringwald                 if (code & 1) {
5651e6aba47Smatthias.ringwald                     // match odd commands by previous signaling identifier
566b448a0e7Smatthias.ringwald                     if (channel->sig_id == sig_id) {
567b448a0e7Smatthias.ringwald                         l2cap_signaling_handler( channel, packet, size);
5681e6aba47Smatthias.ringwald                     }
5691e6aba47Smatthias.ringwald                 } else {
5701e6aba47Smatthias.ringwald                     // match even commands by source channel id
571b448a0e7Smatthias.ringwald                     if (channel->source_cid == dest_cid) {
572b448a0e7Smatthias.ringwald                         l2cap_signaling_handler( channel, packet, size);
5731e6aba47Smatthias.ringwald                     }
5741e6aba47Smatthias.ringwald                 }
5751e6aba47Smatthias.ringwald             }
5761e6aba47Smatthias.ringwald         }
5771e6aba47Smatthias.ringwald         return;
5781e6aba47Smatthias.ringwald     }
5791e6aba47Smatthias.ringwald 
5801e6aba47Smatthias.ringwald     // Find channel for this channel_id and connection handle
581f62db1e3Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_source_cid(channel_id);
582f62db1e3Smatthias.ringwald     if (channel) {
5836f60b3f4Smatthias.ringwald         socket_connection_send_packet(channel->connection, L2CAP_DATA_PACKET, channel_id,
5846f60b3f4Smatthias.ringwald                                       &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
5851e6aba47Smatthias.ringwald     }
5861e6aba47Smatthias.ringwald }
5871e6aba47Smatthias.ringwald 
588f62db1e3Smatthias.ringwald 
5891e6aba47Smatthias.ringwald void l2cap_send_internal(uint16_t source_cid, uint8_t *data, uint16_t len){
5901e6aba47Smatthias.ringwald     // find channel for source_cid, construct l2cap packet and send
591f62db1e3Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_source_cid(source_cid);
592fcadd0caSmatthias.ringwald     if (channel) {
5931e6aba47Smatthias.ringwald          // 0 - Connection handle : PB=10 : BC=00
5941e6aba47Smatthias.ringwald          bt_store_16(acl_buffer, 0, channel->handle | (2 << 12) | (0 << 14));
5951e6aba47Smatthias.ringwald          // 2 - ACL length
5961e6aba47Smatthias.ringwald          bt_store_16(acl_buffer, 2,  len + 4);
5971e6aba47Smatthias.ringwald          // 4 - L2CAP packet length
5981e6aba47Smatthias.ringwald          bt_store_16(acl_buffer, 4,  len + 0);
5991e6aba47Smatthias.ringwald          // 6 - L2CAP channel DEST
6001e6aba47Smatthias.ringwald          bt_store_16(acl_buffer, 6, channel->dest_cid);
6011e6aba47Smatthias.ringwald          // 8 - data
6021e6aba47Smatthias.ringwald          memcpy(&acl_buffer[8], data, len);
6031e6aba47Smatthias.ringwald          // send
6041e6aba47Smatthias.ringwald          hci_send_acl_packet(acl_buffer, len+8);
6051e6aba47Smatthias.ringwald      }
6061e6aba47Smatthias.ringwald }
6071e6aba47Smatthias.ringwald 
6089edc8742Smatthias.ringwald void l2cap_set_capture_connection(connection_t * connection){
6099edc8742Smatthias.ringwald     capture_connection = connection;
6109edc8742Smatthias.ringwald }
6111e6aba47Smatthias.ringwald 
612b448a0e7Smatthias.ringwald