xref: /btstack/src/l2cap.c (revision 169f8b289d38a277601a09d9fe9698c85a297ac1)
143625864Smatthias.ringwald /*
21713bceaSmatthias.ringwald  * Copyright (C) 2009 by Matthias Ringwald
31713bceaSmatthias.ringwald  *
41713bceaSmatthias.ringwald  * Redistribution and use in source and binary forms, with or without
51713bceaSmatthias.ringwald  * modification, are permitted provided that the following conditions
61713bceaSmatthias.ringwald  * are met:
71713bceaSmatthias.ringwald  *
81713bceaSmatthias.ringwald  * 1. Redistributions of source code must retain the above copyright
91713bceaSmatthias.ringwald  *    notice, this list of conditions and the following disclaimer.
101713bceaSmatthias.ringwald  * 2. Redistributions in binary form must reproduce the above copyright
111713bceaSmatthias.ringwald  *    notice, this list of conditions and the following disclaimer in the
121713bceaSmatthias.ringwald  *    documentation and/or other materials provided with the distribution.
131713bceaSmatthias.ringwald  * 3. Neither the name of the copyright holders nor the names of
141713bceaSmatthias.ringwald  *    contributors may be used to endorse or promote products derived
151713bceaSmatthias.ringwald  *    from this software without specific prior written permission.
161713bceaSmatthias.ringwald  *
171713bceaSmatthias.ringwald  * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS
181713bceaSmatthias.ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
191713bceaSmatthias.ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
201713bceaSmatthias.ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
211713bceaSmatthias.ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
221713bceaSmatthias.ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
231713bceaSmatthias.ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
241713bceaSmatthias.ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
251713bceaSmatthias.ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
261713bceaSmatthias.ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
271713bceaSmatthias.ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
281713bceaSmatthias.ringwald  * SUCH DAMAGE.
291713bceaSmatthias.ringwald  *
301713bceaSmatthias.ringwald  */
311713bceaSmatthias.ringwald 
321713bceaSmatthias.ringwald /*
3343625864Smatthias.ringwald  *  l2cap.c
3443625864Smatthias.ringwald  *
3543625864Smatthias.ringwald  *  Logical Link Control and Adaption Protocl (L2CAP)
3643625864Smatthias.ringwald  *
3743625864Smatthias.ringwald  *  Created by Matthias Ringwald on 5/16/09.
3843625864Smatthias.ringwald  */
3943625864Smatthias.ringwald 
4043625864Smatthias.ringwald #include "l2cap.h"
41645658c9Smatthias.ringwald #include "hci.h"
4243625864Smatthias.ringwald 
4343625864Smatthias.ringwald #include <stdarg.h>
4443625864Smatthias.ringwald #include <string.h>
4543625864Smatthias.ringwald 
4643625864Smatthias.ringwald #include <stdio.h>
4743625864Smatthias.ringwald 
486f60b3f4Smatthias.ringwald // size of HCI ACL + L2CAP Header for regular data packets
496f60b3f4Smatthias.ringwald #define COMPLETE_L2CAP_HEADER 8
506f60b3f4Smatthias.ringwald 
51fcadd0caSmatthias.ringwald static void null_event_handler(uint8_t *packet, uint16_t size);
52fcadd0caSmatthias.ringwald static void null_data_handler(uint16_t source_cid, uint8_t *packet, uint16_t size);
53fcadd0caSmatthias.ringwald 
541e6aba47Smatthias.ringwald static uint8_t * sig_buffer = NULL;
551e6aba47Smatthias.ringwald static linked_list_t l2cap_channels = NULL;
569d9bbc01Smatthias.ringwald static linked_list_t l2cap_services = NULL;
571e6aba47Smatthias.ringwald static uint8_t * acl_buffer = NULL;
58fcadd0caSmatthias.ringwald static void (*event_packet_handler) (uint8_t *packet, uint16_t size) = null_event_handler;
59fcadd0caSmatthias.ringwald static void (*data_packet_handler)  (uint16_t source_cid, uint8_t *packet, uint16_t size) = null_data_handler;
609edc8742Smatthias.ringwald static connection_t * capture_connection = NULL;
611e6aba47Smatthias.ringwald 
62*169f8b28Smatthias.ringwald static uint8_t config_options[] = { 1, 2, 150, 0}; // mtu = 48
63*169f8b28Smatthias.ringwald 
641e6aba47Smatthias.ringwald void l2cap_init(){
651e6aba47Smatthias.ringwald     sig_buffer = malloc( 48 );
661e6aba47Smatthias.ringwald     acl_buffer = malloc( 255 + 8 );
67fcadd0caSmatthias.ringwald 
68fcadd0caSmatthias.ringwald     //
69fcadd0caSmatthias.ringwald     // register callbacks with HCI
70fcadd0caSmatthias.ringwald     //
71fcadd0caSmatthias.ringwald     hci_register_event_packet_handler(&l2cap_event_handler);
72fcadd0caSmatthias.ringwald     hci_register_acl_packet_handler(&l2cap_acl_handler);
73fcadd0caSmatthias.ringwald }
74fcadd0caSmatthias.ringwald 
75fcadd0caSmatthias.ringwald 
76fcadd0caSmatthias.ringwald /** Register L2CAP packet handlers */
77fcadd0caSmatthias.ringwald static void null_event_handler(uint8_t *packet, uint16_t size){
78fcadd0caSmatthias.ringwald }
79fcadd0caSmatthias.ringwald static void null_data_handler(uint16_t  source_cid, uint8_t *packet, uint16_t size){
80fcadd0caSmatthias.ringwald }
81fcadd0caSmatthias.ringwald void l2cap_register_event_packet_handler(void (*handler)(uint8_t *packet, uint16_t size)){
82fcadd0caSmatthias.ringwald     event_packet_handler = handler;
83fcadd0caSmatthias.ringwald }
84fcadd0caSmatthias.ringwald void l2cap_register_data_packet_handler  (void (*handler)(uint16_t source_cid, uint8_t *packet, uint16_t size)){
85fcadd0caSmatthias.ringwald     data_packet_handler = handler;
861e6aba47Smatthias.ringwald }
871e6aba47Smatthias.ringwald 
880af41d30Smatthias.ringwald int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){
890af41d30Smatthias.ringwald     va_list argptr;
900af41d30Smatthias.ringwald     va_start(argptr, identifier);
910af41d30Smatthias.ringwald     uint16_t len = l2cap_create_signaling_internal(sig_buffer, handle, cmd, identifier, argptr);
921e6aba47Smatthias.ringwald     va_end(argptr);
930af41d30Smatthias.ringwald     return hci_send_acl_packet(sig_buffer, len);
940af41d30Smatthias.ringwald }
950af41d30Smatthias.ringwald 
96f62db1e3Smatthias.ringwald l2cap_channel_t * l2cap_get_channel_for_source_cid(uint16_t source_cid){
97f62db1e3Smatthias.ringwald     linked_item_t *it;
98f62db1e3Smatthias.ringwald     l2cap_channel_t * channel;
99f62db1e3Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
100f62db1e3Smatthias.ringwald         channel = (l2cap_channel_t *) it;
101f62db1e3Smatthias.ringwald         if ( channel->source_cid == source_cid) {
102f62db1e3Smatthias.ringwald             return channel;
103f62db1e3Smatthias.ringwald         }
104f62db1e3Smatthias.ringwald     }
105f62db1e3Smatthias.ringwald     return NULL;
106f62db1e3Smatthias.ringwald }
107f62db1e3Smatthias.ringwald 
1081e6aba47Smatthias.ringwald // open outgoing L2CAP channel
1091e6aba47Smatthias.ringwald void l2cap_create_channel_internal(connection_t * connection, bd_addr_t address, uint16_t psm){
1101e6aba47Smatthias.ringwald 
1111e6aba47Smatthias.ringwald     // alloc structure
1121e6aba47Smatthias.ringwald     l2cap_channel_t * chan = malloc(sizeof(l2cap_channel_t));
1131e6aba47Smatthias.ringwald     // TODO: emit error event
1141e6aba47Smatthias.ringwald     if (!chan) return;
1151e6aba47Smatthias.ringwald 
1161e6aba47Smatthias.ringwald     // fill in
1171e6aba47Smatthias.ringwald     BD_ADDR_COPY(chan->address, address);
1181e6aba47Smatthias.ringwald     chan->psm = psm;
1191e6aba47Smatthias.ringwald     chan->handle = 0;
1201e6aba47Smatthias.ringwald     chan->connection = connection;
1211e6aba47Smatthias.ringwald 
1221e6aba47Smatthias.ringwald     // set initial state
1231e6aba47Smatthias.ringwald     chan->state = L2CAP_STATE_CLOSED;
1241e6aba47Smatthias.ringwald     chan->sig_id = L2CAP_SIG_ID_INVALID;
1251e6aba47Smatthias.ringwald 
1261e6aba47Smatthias.ringwald     // add to connections list
1271e6aba47Smatthias.ringwald     linked_list_add(&l2cap_channels, (linked_item_t *) chan);
1281e6aba47Smatthias.ringwald 
1291e6aba47Smatthias.ringwald     // send connection request
1301e6aba47Smatthias.ringwald     // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch
1311e6aba47Smatthias.ringwald     hci_send_cmd(&hci_create_connection, address, 0x18, 0, 0, 0, 0);
13243625864Smatthias.ringwald }
13343625864Smatthias.ringwald 
1341e6aba47Smatthias.ringwald void l2cap_disconnect_internal(uint16_t source_cid, uint8_t reason){
135f62db1e3Smatthias.ringwald     // find channel for source_cid
136f62db1e3Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_source_cid(source_cid);
137f62db1e3Smatthias.ringwald     if (channel) {
138f62db1e3Smatthias.ringwald         channel->sig_id = l2cap_next_sig_id();
139f62db1e3Smatthias.ringwald         l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->sig_id, channel->dest_cid, channel->source_cid);
140f62db1e3Smatthias.ringwald         channel->state = L2CAP_STATE_WAIT_DISCONNECT;
141f62db1e3Smatthias.ringwald     }
14243625864Smatthias.ringwald }
1431e6aba47Smatthias.ringwald 
144afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){
1451e6aba47Smatthias.ringwald     linked_item_t *it;
1461e6aba47Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
147b448a0e7Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
148b448a0e7Smatthias.ringwald         if ( ! BD_ADDR_CMP( channel->address, address) ){
149b448a0e7Smatthias.ringwald             if (channel->state == L2CAP_STATE_CLOSED) {
150afde0c52Smatthias.ringwald                 // failure, forward error code
151afde0c52Smatthias.ringwald                 l2cap_emit_channel_opened(channel, status);
152afde0c52Smatthias.ringwald                 // discard channel
153afde0c52Smatthias.ringwald                 linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
154afde0c52Smatthias.ringwald                 free (channel);
155afde0c52Smatthias.ringwald             }
156afde0c52Smatthias.ringwald         }
157afde0c52Smatthias.ringwald     }
158afde0c52Smatthias.ringwald }
159afde0c52Smatthias.ringwald 
160afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){
161afde0c52Smatthias.ringwald     linked_item_t *it;
162afde0c52Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
163afde0c52Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
164afde0c52Smatthias.ringwald         if ( ! BD_ADDR_CMP( channel->address, address) ){
165afde0c52Smatthias.ringwald             if (channel->state == L2CAP_STATE_CLOSED) {
166b448a0e7Smatthias.ringwald                 // success, start l2cap handshake
167afde0c52Smatthias.ringwald                 channel->handle = handle;
168b448a0e7Smatthias.ringwald                 channel->sig_id = l2cap_next_sig_id();
169b448a0e7Smatthias.ringwald                 channel->source_cid = l2cap_next_source_cid();
170b448a0e7Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECT_RSP;
171b448a0e7Smatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, CONNECTION_REQUEST, channel->sig_id, channel->psm, channel->source_cid);
172afde0c52Smatthias.ringwald             }
173afde0c52Smatthias.ringwald         }
174afde0c52Smatthias.ringwald     }
175afde0c52Smatthias.ringwald }
176b448a0e7Smatthias.ringwald 
177afde0c52Smatthias.ringwald void l2cap_event_handler( uint8_t *packet, uint16_t size ){
178afde0c52Smatthias.ringwald 
179afde0c52Smatthias.ringwald     bd_addr_t address;
180afde0c52Smatthias.ringwald     hci_con_handle_t handle;
181afde0c52Smatthias.ringwald 
182afde0c52Smatthias.ringwald     switch(packet[0]){
183afde0c52Smatthias.ringwald 
184afde0c52Smatthias.ringwald         // handle connection complete events
185afde0c52Smatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
186afde0c52Smatthias.ringwald             bt_flip_addr(address, &packet[5]);
187afde0c52Smatthias.ringwald             if (packet[2] == 0){
188afde0c52Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
189afde0c52Smatthias.ringwald                 l2cap_handle_connection_success_for_addr(address, handle);
190afde0c52Smatthias.ringwald             } else {
191afde0c52Smatthias.ringwald                 l2cap_handle_connection_failed_for_addr(address, packet[2]);
192afde0c52Smatthias.ringwald             }
193afde0c52Smatthias.ringwald             break;
194afde0c52Smatthias.ringwald 
195afde0c52Smatthias.ringwald         // handle successful create connection cancel command
196afde0c52Smatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
197afde0c52Smatthias.ringwald             if ( COMMAND_COMPLETE_EVENT(packet, hci_create_connection_cancel) ) {
198afde0c52Smatthias.ringwald                 if (packet[5] == 0){
199afde0c52Smatthias.ringwald                     bt_flip_addr(address, &packet[6]);
200afde0c52Smatthias.ringwald                     // CONNECTION TERMINATED BY LOCAL HOST (0X16)
201afde0c52Smatthias.ringwald                     l2cap_handle_connection_failed_for_addr(address, 0x16);
20203cfbabcSmatthias.ringwald                 }
2031e6aba47Smatthias.ringwald             }
204afde0c52Smatthias.ringwald             break;
20527a923d0Smatthias.ringwald 
2061e6aba47Smatthias.ringwald         // handle disconnection complete events
207afde0c52Smatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
20827a923d0Smatthias.ringwald             // send l2cap disconnect events for all channels on this handle
209afde0c52Smatthias.ringwald             handle = READ_BT_16(packet, 3);
21027a923d0Smatthias.ringwald             linked_item_t *it;
21127a923d0Smatthias.ringwald             for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
21227a923d0Smatthias.ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) it;
21327a923d0Smatthias.ringwald                 if ( channel->handle == handle ){
21427a923d0Smatthias.ringwald                     l2cap_finialize_channel_close(channel);
21527a923d0Smatthias.ringwald                 }
21627a923d0Smatthias.ringwald             }
217afde0c52Smatthias.ringwald             break;
218fcadd0caSmatthias.ringwald 
219ee091cf1Smatthias.ringwald         // HCI Connection Timeouts
220afde0c52Smatthias.ringwald         case L2CAP_EVENT_TIMEOUT_CHECK:
221afde0c52Smatthias.ringwald             if (!capture_connection){
222ee091cf1Smatthias.ringwald                 hci_con_handle_t handle = READ_BT_16(packet, 2);
223ee091cf1Smatthias.ringwald                 linked_item_t *it;
224ee091cf1Smatthias.ringwald                 l2cap_channel_t * channel;
225ee091cf1Smatthias.ringwald                 int used = 0;
226ee091cf1Smatthias.ringwald                 for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
227ee091cf1Smatthias.ringwald                     channel = (l2cap_channel_t *) it;
228ee091cf1Smatthias.ringwald                     if (channel->handle == handle) {
229ee091cf1Smatthias.ringwald                         used = 1;
230ee091cf1Smatthias.ringwald                     }
231ee091cf1Smatthias.ringwald                 }
232ee091cf1Smatthias.ringwald                 if (!used) {
2339edc8742Smatthias.ringwald                     hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection
234ee091cf1Smatthias.ringwald                 }
235ee091cf1Smatthias.ringwald             }
236afde0c52Smatthias.ringwald             break;
237ee091cf1Smatthias.ringwald 
238afde0c52Smatthias.ringwald         default:
239afde0c52Smatthias.ringwald             break;
240afde0c52Smatthias.ringwald     }
241afde0c52Smatthias.ringwald 
242afde0c52Smatthias.ringwald     // pass on
243fcadd0caSmatthias.ringwald     (*event_packet_handler)(packet, size);
2441e6aba47Smatthias.ringwald }
2451e6aba47Smatthias.ringwald 
246afde0c52Smatthias.ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){
24784836b65Smatthias.ringwald     l2cap_send_signaling_packet( channel->handle, DISCONNECTION_RESPONSE, identifier, channel->dest_cid, channel->source_cid);
24884836b65Smatthias.ringwald     l2cap_finialize_channel_close(channel);
24984836b65Smatthias.ringwald }
25084836b65Smatthias.ringwald 
251645658c9Smatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t dest_cid){
252645658c9Smatthias.ringwald 
253*169f8b28Smatthias.ringwald     // printf("l2cap_handle_connection_request for handle %u, psm %u cid %u\n", handle, psm, dest_cid);
254645658c9Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
255645658c9Smatthias.ringwald     if (!service) {
256645658c9Smatthias.ringwald         // 0x0002 PSM not supported
257*169f8b28Smatthias.ringwald         // printf("l2cap_handle_connection_request no PSM for psm %u/n", psm);
258645658c9Smatthias.ringwald         l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, 0x0002, 0);
259645658c9Smatthias.ringwald         return;
260645658c9Smatthias.ringwald     }
261645658c9Smatthias.ringwald 
262645658c9Smatthias.ringwald     hci_connection_t * hci_connection = connection_for_handle( handle );
263645658c9Smatthias.ringwald     if (!hci_connection) {
264*169f8b28Smatthias.ringwald         fprintf(stderr, "no hci_connection for handle %u\n", handle);
265645658c9Smatthias.ringwald         // TODO: emit error
266645658c9Smatthias.ringwald         return;
267645658c9Smatthias.ringwald     }
268645658c9Smatthias.ringwald 
269645658c9Smatthias.ringwald     // alloc structure
270*169f8b28Smatthias.ringwald     // printf("l2cap_handle_connection_request register channel\n");
271*169f8b28Smatthias.ringwald     l2cap_channel_t * channel = malloc(sizeof(l2cap_channel_t));
272645658c9Smatthias.ringwald     // TODO: emit error event
273*169f8b28Smatthias.ringwald     if (!channel) return;
274645658c9Smatthias.ringwald 
275645658c9Smatthias.ringwald     // fill in
276*169f8b28Smatthias.ringwald     BD_ADDR_COPY(channel->address, hci_connection->address);
277*169f8b28Smatthias.ringwald     channel->psm = psm;
278*169f8b28Smatthias.ringwald     channel->handle = handle;
279*169f8b28Smatthias.ringwald     channel->connection = service->connection;
280*169f8b28Smatthias.ringwald     channel->source_cid = l2cap_next_source_cid();
281*169f8b28Smatthias.ringwald     channel->dest_cid   = dest_cid;
282645658c9Smatthias.ringwald 
283645658c9Smatthias.ringwald     // set initial state
284*169f8b28Smatthias.ringwald     channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ;
285*169f8b28Smatthias.ringwald     channel->sig_id = l2cap_next_sig_id();
286645658c9Smatthias.ringwald 
287645658c9Smatthias.ringwald     // add to connections list
288*169f8b28Smatthias.ringwald     linked_list_add(&l2cap_channels, (linked_item_t *) channel);
289645658c9Smatthias.ringwald 
290645658c9Smatthias.ringwald     // TODO: emit incoming connection request instead of answering directly
291645658c9Smatthias.ringwald 
292*169f8b28Smatthias.ringwald     l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, dest_cid, channel->source_cid, 0, 0);
293*169f8b28Smatthias.ringwald     l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->sig_id, channel->dest_cid, 0, 4, &config_options);
294645658c9Smatthias.ringwald 
295645658c9Smatthias.ringwald }
296645658c9Smatthias.ringwald 
2971e6aba47Smatthias.ringwald void l2cap_signaling_handler(l2cap_channel_t *channel, uint8_t *packet, uint16_t size){
2981e6aba47Smatthias.ringwald 
2991e6aba47Smatthias.ringwald     uint8_t  code       = READ_L2CAP_SIGNALING_CODE( packet );
3001e6aba47Smatthias.ringwald     uint8_t  identifier = READ_L2CAP_SIGNALING_IDENTIFIER( packet );
30138e5900eSmatthias.ringwald     uint16_t result = 0;
3021e6aba47Smatthias.ringwald 
3031e6aba47Smatthias.ringwald     switch (channel->state) {
3041e6aba47Smatthias.ringwald 
3051e6aba47Smatthias.ringwald         case L2CAP_STATE_WAIT_CONNECT_RSP:
3061e6aba47Smatthias.ringwald             switch (code){
3071e6aba47Smatthias.ringwald                 case CONNECTION_RESPONSE:
308141679a4Smatthias.ringwald                     result = READ_BT_16 (packet, L2CAP_SIGNALING_DATA_OFFSET+4);
30938e5900eSmatthias.ringwald                     switch (result) {
31038e5900eSmatthias.ringwald                         case 0:
311*169f8b28Smatthias.ringwald                             // successful connection
312cd56f931Smatthias.ringwald                             channel->dest_cid = READ_BT_16(packet, L2CAP_SIGNALING_DATA_OFFSET);
3131e6aba47Smatthias.ringwald                             channel->sig_id = l2cap_next_sig_id();
3141e6aba47Smatthias.ringwald                             l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->sig_id, channel->dest_cid, 0, 4, &config_options);
3155a67bd4aSmatthias.ringwald                             channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ;
31638e5900eSmatthias.ringwald                             break;
31738e5900eSmatthias.ringwald                         case 1:
31838e5900eSmatthias.ringwald                             // connection pending. get some coffee
31938e5900eSmatthias.ringwald                             break;
32038e5900eSmatthias.ringwald                         default:
321f32b992eSmatthias.ringwald                             // map l2cap connection response result to BTstack status enumeration
32238e5900eSmatthias.ringwald                             l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result);
32338e5900eSmatthias.ringwald                             break;
3241e6aba47Smatthias.ringwald                     }
3251e6aba47Smatthias.ringwald                     break;
32638e5900eSmatthias.ringwald 
32784836b65Smatthias.ringwald                 case DISCONNECTION_REQUEST:
32884836b65Smatthias.ringwald                     l2cap_handle_disconnect_request(channel, identifier);
32984836b65Smatthias.ringwald                     break;
33084836b65Smatthias.ringwald 
33138e5900eSmatthias.ringwald                 default:
3321e6aba47Smatthias.ringwald                     //@TODO: implement other signaling packets
33338e5900eSmatthias.ringwald                     break;
3341e6aba47Smatthias.ringwald             }
3351e6aba47Smatthias.ringwald             break;
3361e6aba47Smatthias.ringwald 
3375a67bd4aSmatthias.ringwald         case L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ:
3381e6aba47Smatthias.ringwald             switch (code) {
3391e6aba47Smatthias.ringwald                 case CONFIGURE_RESPONSE:
3401e6aba47Smatthias.ringwald                     channel->state = L2CAP_STATE_WAIT_CONFIG_REQ;
3411e6aba47Smatthias.ringwald                     break;
3425a67bd4aSmatthias.ringwald                 case CONFIGURE_REQUEST:
3435a67bd4aSmatthias.ringwald                     // accept the other's configuration options
3445a67bd4aSmatthias.ringwald                     l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, identifier, channel->dest_cid, 0, 0, size - 16, &packet[16]);
3455a67bd4aSmatthias.ringwald                     channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP;
3465a67bd4aSmatthias.ringwald                     break;
34784836b65Smatthias.ringwald                 case DISCONNECTION_REQUEST:
34884836b65Smatthias.ringwald                     l2cap_handle_disconnect_request(channel, identifier);
34984836b65Smatthias.ringwald                     break;
3505a67bd4aSmatthias.ringwald                 default:
3515a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
3525a67bd4aSmatthias.ringwald                     break;
3531e6aba47Smatthias.ringwald             }
3541e6aba47Smatthias.ringwald             break;
3551e6aba47Smatthias.ringwald 
3561e6aba47Smatthias.ringwald         case L2CAP_STATE_WAIT_CONFIG_REQ:
3571e6aba47Smatthias.ringwald             switch (code) {
3581e6aba47Smatthias.ringwald                 case CONFIGURE_REQUEST:
3591e6aba47Smatthias.ringwald                     // accept the other's configuration options
3601e6aba47Smatthias.ringwald                     l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, identifier, channel->dest_cid, 0, 0, size - 16, &packet[16]);
3611e6aba47Smatthias.ringwald                     channel->state = L2CAP_STATE_OPEN;
36203cfbabcSmatthias.ringwald                     l2cap_emit_channel_opened(channel, 0);  // success
363c8e4258aSmatthias.ringwald                     break;
36484836b65Smatthias.ringwald                 case DISCONNECTION_REQUEST:
36584836b65Smatthias.ringwald                     l2cap_handle_disconnect_request(channel, identifier);
36684836b65Smatthias.ringwald                     break;
3675a67bd4aSmatthias.ringwald                 default:
3685a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
3695a67bd4aSmatthias.ringwald                     break;
3705a67bd4aSmatthias.ringwald             }
3715a67bd4aSmatthias.ringwald             break;
3725a67bd4aSmatthias.ringwald 
3735a67bd4aSmatthias.ringwald         case L2CAP_STATE_WAIT_CONFIG_REQ_RSP:
3745a67bd4aSmatthias.ringwald             switch (code) {
3755a67bd4aSmatthias.ringwald                 case CONFIGURE_RESPONSE:
3765a67bd4aSmatthias.ringwald                     channel->state = L2CAP_STATE_OPEN;
3775a67bd4aSmatthias.ringwald                     l2cap_emit_channel_opened(channel, 0);  // success
3785a67bd4aSmatthias.ringwald                     break;
37984836b65Smatthias.ringwald                 case DISCONNECTION_REQUEST:
38084836b65Smatthias.ringwald                     l2cap_handle_disconnect_request(channel, identifier);
38184836b65Smatthias.ringwald                     break;
3825a67bd4aSmatthias.ringwald                 default:
3835a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
3845a67bd4aSmatthias.ringwald                     break;
385c8e4258aSmatthias.ringwald             }
386c8e4258aSmatthias.ringwald             break;
387f62db1e3Smatthias.ringwald 
388f62db1e3Smatthias.ringwald         case L2CAP_STATE_WAIT_DISCONNECT:
389f62db1e3Smatthias.ringwald             switch (code) {
390f62db1e3Smatthias.ringwald                 case DISCONNECTION_RESPONSE:
39127a923d0Smatthias.ringwald                     l2cap_finialize_channel_close(channel);
39227a923d0Smatthias.ringwald                     break;
39384836b65Smatthias.ringwald                 case DISCONNECTION_REQUEST:
39484836b65Smatthias.ringwald                     l2cap_handle_disconnect_request(channel, identifier);
39584836b65Smatthias.ringwald                     break;
3965a67bd4aSmatthias.ringwald                 default:
3975a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
3985a67bd4aSmatthias.ringwald                     break;
39927a923d0Smatthias.ringwald             }
40027a923d0Smatthias.ringwald             break;
40184836b65Smatthias.ringwald 
40284836b65Smatthias.ringwald         case L2CAP_STATE_CLOSED:
40384836b65Smatthias.ringwald             // @TODO handle incoming requests
40484836b65Smatthias.ringwald             break;
40584836b65Smatthias.ringwald 
40684836b65Smatthias.ringwald         case L2CAP_STATE_OPEN:
40784836b65Smatthias.ringwald             switch (code) {
40884836b65Smatthias.ringwald                 case DISCONNECTION_REQUEST:
40984836b65Smatthias.ringwald                     l2cap_handle_disconnect_request(channel, identifier);
41084836b65Smatthias.ringwald                     break;
4115a67bd4aSmatthias.ringwald                 default:
41284836b65Smatthias.ringwald                     //@TODO: implement other signaling packets, e.g. re-configure
41384836b65Smatthias.ringwald                     break;
41484836b65Smatthias.ringwald             }
4155a67bd4aSmatthias.ringwald             break;
41627a923d0Smatthias.ringwald     }
41727a923d0Smatthias.ringwald }
41827a923d0Smatthias.ringwald 
41927a923d0Smatthias.ringwald // finalize closed channel
42027a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t *channel){
421f62db1e3Smatthias.ringwald     channel->state = L2CAP_STATE_CLOSED;
422f62db1e3Smatthias.ringwald     l2cap_emit_channel_closed(channel);
423f62db1e3Smatthias.ringwald 
424f62db1e3Smatthias.ringwald     // discard channel
425f62db1e3Smatthias.ringwald     linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
426f62db1e3Smatthias.ringwald     free (channel);
427c8e4258aSmatthias.ringwald }
4281e6aba47Smatthias.ringwald 
4299d9bbc01Smatthias.ringwald l2cap_service_t * l2cap_get_service(uint16_t psm){
430c52bf64dSmatthias.ringwald     linked_item_t *it;
4319d9bbc01Smatthias.ringwald 
4329d9bbc01Smatthias.ringwald     // close open channels
4339d9bbc01Smatthias.ringwald     for (it = (linked_item_t *) l2cap_services; it ; it = it->next){
4349d9bbc01Smatthias.ringwald         l2cap_service_t * service = ((l2cap_service_t *) it);
4359d9bbc01Smatthias.ringwald         if ( service->psm == psm){
4369d9bbc01Smatthias.ringwald             return service;
4379d9bbc01Smatthias.ringwald         };
4389d9bbc01Smatthias.ringwald     }
4399d9bbc01Smatthias.ringwald     return NULL;
4409d9bbc01Smatthias.ringwald }
4419d9bbc01Smatthias.ringwald 
442116ee617Smatthias.ringwald void l2cap_register_service_internal(connection_t *connection, uint16_t psm, uint16_t mtu){
4439d9bbc01Smatthias.ringwald     // check for alread registered psm // TODO: emit error event
4449d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
4459d9bbc01Smatthias.ringwald     if (service) return;
4469d9bbc01Smatthias.ringwald 
4479d9bbc01Smatthias.ringwald     // alloc structure     // TODO: emit error event
4489d9bbc01Smatthias.ringwald     service = malloc(sizeof(l2cap_service_t));
4499d9bbc01Smatthias.ringwald     if (!service) return;
4509d9bbc01Smatthias.ringwald 
4519d9bbc01Smatthias.ringwald     // fill in
4529d9bbc01Smatthias.ringwald     service->psm = psm;
4539d9bbc01Smatthias.ringwald     service->mtu = mtu;
4549d9bbc01Smatthias.ringwald     service->connection = connection;
4559d9bbc01Smatthias.ringwald 
4569d9bbc01Smatthias.ringwald     // add to services list
4579d9bbc01Smatthias.ringwald     linked_list_add(&l2cap_services, (linked_item_t *) service);
4589d9bbc01Smatthias.ringwald }
4599d9bbc01Smatthias.ringwald 
460116ee617Smatthias.ringwald void l2cap_unregister_service_internal(connection_t *connection, uint16_t psm){
4619d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
4629d9bbc01Smatthias.ringwald     if (service) return;
4639d9bbc01Smatthias.ringwald     linked_list_remove(&l2cap_services, (linked_item_t *) service);
4649d9bbc01Smatthias.ringwald     free( service );
4659d9bbc01Smatthias.ringwald }
4669d9bbc01Smatthias.ringwald 
4679d9bbc01Smatthias.ringwald //
4689d9bbc01Smatthias.ringwald void l2cap_close_connection(connection_t *connection){
4699d9bbc01Smatthias.ringwald     linked_item_t *it;
4709d9bbc01Smatthias.ringwald 
4719d9bbc01Smatthias.ringwald     // close open channels
472c52bf64dSmatthias.ringwald     l2cap_channel_t * channel;
473c52bf64dSmatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
474c52bf64dSmatthias.ringwald         channel = (l2cap_channel_t *) it;
475c52bf64dSmatthias.ringwald         if (channel->connection == connection) {
476c52bf64dSmatthias.ringwald             channel->sig_id = l2cap_next_sig_id();
477c52bf64dSmatthias.ringwald             l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->sig_id, channel->dest_cid, channel->source_cid);
478c52bf64dSmatthias.ringwald             channel->state = L2CAP_STATE_WAIT_DISCONNECT;
479c52bf64dSmatthias.ringwald         }
480c52bf64dSmatthias.ringwald     }
4819d9bbc01Smatthias.ringwald 
482645658c9Smatthias.ringwald     // unregister services
483645658c9Smatthias.ringwald     l2cap_service_t *service;
484645658c9Smatthias.ringwald     for (it = (linked_item_t *) &l2cap_services; it ; it = it->next){
485645658c9Smatthias.ringwald         channel = (l2cap_channel_t *) it->next;
486645658c9Smatthias.ringwald         if (service->connection == connection){
487645658c9Smatthias.ringwald             it->next = it->next->next;
488645658c9Smatthias.ringwald             return;
489645658c9Smatthias.ringwald         }
490645658c9Smatthias.ringwald     }
491c52bf64dSmatthias.ringwald }
492c52bf64dSmatthias.ringwald 
4931f7b95a1Smatthias.ringwald void l2cap_accept_connection_internal(hci_con_handle_t handle,  uint16_t dest_cid){
4941f7b95a1Smatthias.ringwald     printf("l2cap_accept_connection_internal called but not implemented yet 0x%x, 0x%x\n", handle, dest_cid);
4951f7b95a1Smatthias.ringwald }
4961f7b95a1Smatthias.ringwald 
4971f7b95a1Smatthias.ringwald void l2cap_decline_connection_internal(hci_con_handle_t handle, uint16_t dest_cid, uint8_t reason){
4981f7b95a1Smatthias.ringwald     printf("l2cap_decline_connection_internal called but not implemented yet 0x%x, 0x%x, %u", handle, dest_cid,reason);
4991f7b95a1Smatthias.ringwald }
5001f7b95a1Smatthias.ringwald 
5011e6aba47Smatthias.ringwald //  notify client
50203cfbabcSmatthias.ringwald void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) {
50303cfbabcSmatthias.ringwald     uint8_t event[17];
50480d52d6bSmatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_OPENED;
505c8e4258aSmatthias.ringwald     event[1] = sizeof(event) - 2;
50603cfbabcSmatthias.ringwald     event[2] = status;
50703cfbabcSmatthias.ringwald     bt_flip_addr(&event[3], channel->address);
50803cfbabcSmatthias.ringwald     bt_store_16(event,  9, channel->handle);
50903cfbabcSmatthias.ringwald     bt_store_16(event, 11, channel->psm);
51003cfbabcSmatthias.ringwald     bt_store_16(event, 13, channel->source_cid);
51103cfbabcSmatthias.ringwald     bt_store_16(event, 15, channel->dest_cid);
5121e6aba47Smatthias.ringwald     socket_connection_send_packet(channel->connection, HCI_EVENT_PACKET, 0, event, sizeof(event));
5131e6aba47Smatthias.ringwald }
5141e6aba47Smatthias.ringwald 
515f62db1e3Smatthias.ringwald void l2cap_emit_channel_closed(l2cap_channel_t *channel) {
516f62db1e3Smatthias.ringwald     uint8_t event[4];
51780d52d6bSmatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_CLOSED;
518f62db1e3Smatthias.ringwald     event[1] = sizeof(event) - 2;
519f62db1e3Smatthias.ringwald     bt_store_16(event, 2, channel->source_cid);
520f62db1e3Smatthias.ringwald     socket_connection_send_packet(channel->connection, HCI_EVENT_PACKET, 0, event, sizeof(event));
521f62db1e3Smatthias.ringwald }
522f62db1e3Smatthias.ringwald 
5231e6aba47Smatthias.ringwald void l2cap_acl_handler( uint8_t *packet, uint16_t size ){
5241e6aba47Smatthias.ringwald 
5259edc8742Smatthias.ringwald     // Capturing?
5269edc8742Smatthias.ringwald     if (capture_connection) {
5279edc8742Smatthias.ringwald         socket_connection_send_packet(capture_connection, HCI_ACL_DATA_PACKET, 0, packet, size);
528d6f03c5dSmatthias.ringwald         return;
5299edc8742Smatthias.ringwald     }
5309edc8742Smatthias.ringwald 
5319edc8742Smatthias.ringwald     // forward to higher layers - not needed yet
5329edc8742Smatthias.ringwald     // (*data_packet_handler)(channel_id, packet, size);
5339edc8742Smatthias.ringwald 
5341e6aba47Smatthias.ringwald     // Get Channel ID and command code
5351e6aba47Smatthias.ringwald     uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet);
5361e6aba47Smatthias.ringwald     uint8_t  code       = READ_L2CAP_SIGNALING_CODE( packet );
5371e6aba47Smatthias.ringwald 
5381e6aba47Smatthias.ringwald     // Get Connection
5391e6aba47Smatthias.ringwald     hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet);
5401e6aba47Smatthias.ringwald 
541*169f8b28Smatthias.ringwald     // printf("l2cap_acl_handler channel %u, code %u\n", channel_id, code);
542*169f8b28Smatthias.ringwald 
5431e6aba47Smatthias.ringwald     // Signaling Packet?
5441e6aba47Smatthias.ringwald     if (channel_id == 1) {
5451e6aba47Smatthias.ringwald 
546*169f8b28Smatthias.ringwald         if (code < 1 || code >= 8){
547*169f8b28Smatthias.ringwald             // not for a particular channel, and not CONNECTION_REQUEST
5481e6aba47Smatthias.ringwald             return;
5491e6aba47Smatthias.ringwald         }
5501e6aba47Smatthias.ringwald 
551645658c9Smatthias.ringwald         // Get Signaling Identifier
5521e6aba47Smatthias.ringwald         uint8_t sig_id    = READ_L2CAP_SIGNALING_IDENTIFIER(packet);
553645658c9Smatthias.ringwald 
554645658c9Smatthias.ringwald         // CONNECTION_REQUEST
555645658c9Smatthias.ringwald         if (code == CONNECTION_REQUEST){
556645658c9Smatthias.ringwald             uint16_t psm =      READ_BT_16(packet, L2CAP_SIGNALING_DATA_OFFSET);
557645658c9Smatthias.ringwald             uint16_t dest_cid = READ_BT_16(packet, L2CAP_SIGNALING_DATA_OFFSET+2);
558645658c9Smatthias.ringwald             l2cap_handle_connection_request(handle, sig_id, psm, dest_cid);
559645658c9Smatthias.ringwald             return;
560645658c9Smatthias.ringwald         }
561645658c9Smatthias.ringwald 
562645658c9Smatthias.ringwald         // Get potential destination CID
5631e6aba47Smatthias.ringwald         uint16_t dest_cid = READ_BT_16(packet, L2CAP_SIGNALING_DATA_OFFSET);
5641e6aba47Smatthias.ringwald 
5651e6aba47Smatthias.ringwald         // Find channel for this sig_id and connection handle
5661e6aba47Smatthias.ringwald         linked_item_t *it;
5671e6aba47Smatthias.ringwald         for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
568b448a0e7Smatthias.ringwald             l2cap_channel_t * channel = (l2cap_channel_t *) it;
569b448a0e7Smatthias.ringwald             if (channel->handle == handle) {
5701e6aba47Smatthias.ringwald                 if (code & 1) {
5711e6aba47Smatthias.ringwald                     // match odd commands by previous signaling identifier
572b448a0e7Smatthias.ringwald                     if (channel->sig_id == sig_id) {
573b448a0e7Smatthias.ringwald                         l2cap_signaling_handler( channel, packet, size);
5741e6aba47Smatthias.ringwald                     }
5751e6aba47Smatthias.ringwald                 } else {
5761e6aba47Smatthias.ringwald                     // match even commands by source channel id
577b448a0e7Smatthias.ringwald                     if (channel->source_cid == dest_cid) {
578b448a0e7Smatthias.ringwald                         l2cap_signaling_handler( channel, packet, size);
5791e6aba47Smatthias.ringwald                     }
5801e6aba47Smatthias.ringwald                 }
5811e6aba47Smatthias.ringwald             }
5821e6aba47Smatthias.ringwald         }
5831e6aba47Smatthias.ringwald         return;
5841e6aba47Smatthias.ringwald     }
5851e6aba47Smatthias.ringwald 
5861e6aba47Smatthias.ringwald     // Find channel for this channel_id and connection handle
587f62db1e3Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_source_cid(channel_id);
588f62db1e3Smatthias.ringwald     if (channel) {
5896f60b3f4Smatthias.ringwald         socket_connection_send_packet(channel->connection, L2CAP_DATA_PACKET, channel_id,
5906f60b3f4Smatthias.ringwald                                       &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
5911e6aba47Smatthias.ringwald     }
5921e6aba47Smatthias.ringwald }
5931e6aba47Smatthias.ringwald 
594f62db1e3Smatthias.ringwald 
5951e6aba47Smatthias.ringwald void l2cap_send_internal(uint16_t source_cid, uint8_t *data, uint16_t len){
5961e6aba47Smatthias.ringwald     // find channel for source_cid, construct l2cap packet and send
597f62db1e3Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_source_cid(source_cid);
598fcadd0caSmatthias.ringwald     if (channel) {
5991e6aba47Smatthias.ringwald          // 0 - Connection handle : PB=10 : BC=00
6001e6aba47Smatthias.ringwald          bt_store_16(acl_buffer, 0, channel->handle | (2 << 12) | (0 << 14));
6011e6aba47Smatthias.ringwald          // 2 - ACL length
6021e6aba47Smatthias.ringwald          bt_store_16(acl_buffer, 2,  len + 4);
6031e6aba47Smatthias.ringwald          // 4 - L2CAP packet length
6041e6aba47Smatthias.ringwald          bt_store_16(acl_buffer, 4,  len + 0);
6051e6aba47Smatthias.ringwald          // 6 - L2CAP channel DEST
6061e6aba47Smatthias.ringwald          bt_store_16(acl_buffer, 6, channel->dest_cid);
6071e6aba47Smatthias.ringwald          // 8 - data
6081e6aba47Smatthias.ringwald          memcpy(&acl_buffer[8], data, len);
6091e6aba47Smatthias.ringwald          // send
6101e6aba47Smatthias.ringwald          hci_send_acl_packet(acl_buffer, len+8);
6111e6aba47Smatthias.ringwald      }
6121e6aba47Smatthias.ringwald }
6131e6aba47Smatthias.ringwald 
6149edc8742Smatthias.ringwald void l2cap_set_capture_connection(connection_t * connection){
6159edc8742Smatthias.ringwald     capture_connection = connection;
6169edc8742Smatthias.ringwald }
6171e6aba47Smatthias.ringwald 
618b448a0e7Smatthias.ringwald