xref: /btstack/src/l2cap.c (revision afde0c529f24312119adc84a44823beda06bf018)
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"
4143625864Smatthias.ringwald 
4243625864Smatthias.ringwald #include <stdarg.h>
4343625864Smatthias.ringwald #include <string.h>
4443625864Smatthias.ringwald 
4543625864Smatthias.ringwald #include <stdio.h>
4643625864Smatthias.ringwald 
476f60b3f4Smatthias.ringwald // size of HCI ACL + L2CAP Header for regular data packets
486f60b3f4Smatthias.ringwald #define COMPLETE_L2CAP_HEADER 8
496f60b3f4Smatthias.ringwald 
50fcadd0caSmatthias.ringwald static void null_event_handler(uint8_t *packet, uint16_t size);
51fcadd0caSmatthias.ringwald static void null_data_handler(uint16_t source_cid, uint8_t *packet, uint16_t size);
52fcadd0caSmatthias.ringwald 
531e6aba47Smatthias.ringwald static uint8_t * sig_buffer = NULL;
541e6aba47Smatthias.ringwald static linked_list_t l2cap_channels = NULL;
551e6aba47Smatthias.ringwald static uint8_t * acl_buffer = NULL;
56fcadd0caSmatthias.ringwald static void (*event_packet_handler) (uint8_t *packet, uint16_t size) = null_event_handler;
57fcadd0caSmatthias.ringwald static void (*data_packet_handler)  (uint16_t source_cid, uint8_t *packet, uint16_t size) = null_data_handler;
589edc8742Smatthias.ringwald static connection_t * capture_connection = NULL;
591e6aba47Smatthias.ringwald 
601e6aba47Smatthias.ringwald void l2cap_init(){
611e6aba47Smatthias.ringwald     sig_buffer = malloc( 48 );
621e6aba47Smatthias.ringwald     acl_buffer = malloc( 255 + 8 );
63fcadd0caSmatthias.ringwald 
64fcadd0caSmatthias.ringwald     //
65fcadd0caSmatthias.ringwald     // register callbacks with HCI
66fcadd0caSmatthias.ringwald     //
67fcadd0caSmatthias.ringwald     hci_register_event_packet_handler(&l2cap_event_handler);
68fcadd0caSmatthias.ringwald     hci_register_acl_packet_handler(&l2cap_acl_handler);
69fcadd0caSmatthias.ringwald }
70fcadd0caSmatthias.ringwald 
71fcadd0caSmatthias.ringwald 
72fcadd0caSmatthias.ringwald /** Register L2CAP packet handlers */
73fcadd0caSmatthias.ringwald static void null_event_handler(uint8_t *packet, uint16_t size){
74fcadd0caSmatthias.ringwald }
75fcadd0caSmatthias.ringwald static void null_data_handler(uint16_t  source_cid, uint8_t *packet, uint16_t size){
76fcadd0caSmatthias.ringwald }
77fcadd0caSmatthias.ringwald void l2cap_register_event_packet_handler(void (*handler)(uint8_t *packet, uint16_t size)){
78fcadd0caSmatthias.ringwald     event_packet_handler = handler;
79fcadd0caSmatthias.ringwald }
80fcadd0caSmatthias.ringwald void l2cap_register_data_packet_handler  (void (*handler)(uint16_t source_cid, uint8_t *packet, uint16_t size)){
81fcadd0caSmatthias.ringwald     data_packet_handler = handler;
821e6aba47Smatthias.ringwald }
831e6aba47Smatthias.ringwald 
840af41d30Smatthias.ringwald int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){
850af41d30Smatthias.ringwald     va_list argptr;
860af41d30Smatthias.ringwald     va_start(argptr, identifier);
870af41d30Smatthias.ringwald     uint16_t len = l2cap_create_signaling_internal(sig_buffer, handle, cmd, identifier, argptr);
881e6aba47Smatthias.ringwald     va_end(argptr);
890af41d30Smatthias.ringwald     return hci_send_acl_packet(sig_buffer, len);
900af41d30Smatthias.ringwald }
910af41d30Smatthias.ringwald 
92f62db1e3Smatthias.ringwald l2cap_channel_t * l2cap_get_channel_for_source_cid(uint16_t source_cid){
93f62db1e3Smatthias.ringwald     linked_item_t *it;
94f62db1e3Smatthias.ringwald     l2cap_channel_t * channel;
95f62db1e3Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
96f62db1e3Smatthias.ringwald         channel = (l2cap_channel_t *) it;
97f62db1e3Smatthias.ringwald         if ( channel->source_cid == source_cid) {
98f62db1e3Smatthias.ringwald             return channel;
99f62db1e3Smatthias.ringwald         }
100f62db1e3Smatthias.ringwald     }
101f62db1e3Smatthias.ringwald     return NULL;
102f62db1e3Smatthias.ringwald }
103f62db1e3Smatthias.ringwald 
1041e6aba47Smatthias.ringwald // open outgoing L2CAP channel
1051e6aba47Smatthias.ringwald void l2cap_create_channel_internal(connection_t * connection, bd_addr_t address, uint16_t psm){
1061e6aba47Smatthias.ringwald 
1071e6aba47Smatthias.ringwald     // alloc structure
1081e6aba47Smatthias.ringwald     l2cap_channel_t * chan = malloc(sizeof(l2cap_channel_t));
1091e6aba47Smatthias.ringwald     // TODO: emit error event
1101e6aba47Smatthias.ringwald     if (!chan) return;
1111e6aba47Smatthias.ringwald 
1121e6aba47Smatthias.ringwald     // fill in
1131e6aba47Smatthias.ringwald     BD_ADDR_COPY(chan->address, address);
1141e6aba47Smatthias.ringwald     chan->psm = psm;
1151e6aba47Smatthias.ringwald     chan->handle = 0;
1161e6aba47Smatthias.ringwald     chan->connection = connection;
1171e6aba47Smatthias.ringwald 
1181e6aba47Smatthias.ringwald     // set initial state
1191e6aba47Smatthias.ringwald     chan->state = L2CAP_STATE_CLOSED;
1201e6aba47Smatthias.ringwald     chan->sig_id = L2CAP_SIG_ID_INVALID;
1211e6aba47Smatthias.ringwald 
1221e6aba47Smatthias.ringwald     // add to connections list
1231e6aba47Smatthias.ringwald     linked_list_add(&l2cap_channels, (linked_item_t *) chan);
1241e6aba47Smatthias.ringwald 
1251e6aba47Smatthias.ringwald     // send connection request
1261e6aba47Smatthias.ringwald     // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch
1271e6aba47Smatthias.ringwald     hci_send_cmd(&hci_create_connection, address, 0x18, 0, 0, 0, 0);
12843625864Smatthias.ringwald }
12943625864Smatthias.ringwald 
1301e6aba47Smatthias.ringwald void l2cap_disconnect_internal(uint16_t source_cid, uint8_t reason){
131f62db1e3Smatthias.ringwald     // find channel for source_cid
132f62db1e3Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_source_cid(source_cid);
133f62db1e3Smatthias.ringwald     if (channel) {
134f62db1e3Smatthias.ringwald         channel->sig_id = l2cap_next_sig_id();
135f62db1e3Smatthias.ringwald         l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->sig_id, channel->dest_cid, channel->source_cid);
136f62db1e3Smatthias.ringwald         channel->state = L2CAP_STATE_WAIT_DISCONNECT;
137f62db1e3Smatthias.ringwald     }
13843625864Smatthias.ringwald }
1391e6aba47Smatthias.ringwald 
140*afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){
1411e6aba47Smatthias.ringwald     linked_item_t *it;
1421e6aba47Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
143b448a0e7Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
144b448a0e7Smatthias.ringwald         if ( ! BD_ADDR_CMP( channel->address, address) ){
145b448a0e7Smatthias.ringwald             if (channel->state == L2CAP_STATE_CLOSED) {
146*afde0c52Smatthias.ringwald                 // failure, forward error code
147*afde0c52Smatthias.ringwald                 l2cap_emit_channel_opened(channel, status);
148*afde0c52Smatthias.ringwald                 // discard channel
149*afde0c52Smatthias.ringwald                 linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
150*afde0c52Smatthias.ringwald                 free (channel);
151*afde0c52Smatthias.ringwald             }
152*afde0c52Smatthias.ringwald         }
153*afde0c52Smatthias.ringwald     }
154*afde0c52Smatthias.ringwald }
155*afde0c52Smatthias.ringwald 
156*afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){
157*afde0c52Smatthias.ringwald     linked_item_t *it;
158*afde0c52Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
159*afde0c52Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
160*afde0c52Smatthias.ringwald         if ( ! BD_ADDR_CMP( channel->address, address) ){
161*afde0c52Smatthias.ringwald             if (channel->state == L2CAP_STATE_CLOSED) {
162b448a0e7Smatthias.ringwald                 // success, start l2cap handshake
163*afde0c52Smatthias.ringwald                 channel->handle = handle;
164b448a0e7Smatthias.ringwald                 channel->sig_id = l2cap_next_sig_id();
165b448a0e7Smatthias.ringwald                 channel->source_cid = l2cap_next_source_cid();
166b448a0e7Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECT_RSP;
167b448a0e7Smatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, CONNECTION_REQUEST, channel->sig_id, channel->psm, channel->source_cid);
168*afde0c52Smatthias.ringwald             }
169*afde0c52Smatthias.ringwald         }
170*afde0c52Smatthias.ringwald     }
171*afde0c52Smatthias.ringwald }
172b448a0e7Smatthias.ringwald 
173*afde0c52Smatthias.ringwald void l2cap_event_handler( uint8_t *packet, uint16_t size ){
174*afde0c52Smatthias.ringwald 
175*afde0c52Smatthias.ringwald     bd_addr_t address;
176*afde0c52Smatthias.ringwald     hci_con_handle_t handle;
177*afde0c52Smatthias.ringwald 
178*afde0c52Smatthias.ringwald     switch(packet[0]){
179*afde0c52Smatthias.ringwald 
180*afde0c52Smatthias.ringwald         // handle connection complete events
181*afde0c52Smatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
182*afde0c52Smatthias.ringwald             bt_flip_addr(address, &packet[5]);
183*afde0c52Smatthias.ringwald             if (packet[2] == 0){
184*afde0c52Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
185*afde0c52Smatthias.ringwald                 l2cap_handle_connection_success_for_addr(address, handle);
186*afde0c52Smatthias.ringwald             } else {
187*afde0c52Smatthias.ringwald                 l2cap_handle_connection_failed_for_addr(address, packet[2]);
188*afde0c52Smatthias.ringwald             }
189*afde0c52Smatthias.ringwald             break;
190*afde0c52Smatthias.ringwald 
191*afde0c52Smatthias.ringwald         // handle successful create connection cancel command
192*afde0c52Smatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
193*afde0c52Smatthias.ringwald             if ( COMMAND_COMPLETE_EVENT(packet, hci_create_connection_cancel) ) {
194*afde0c52Smatthias.ringwald                 if (packet[5] == 0){
195*afde0c52Smatthias.ringwald                     bt_flip_addr(address, &packet[6]);
196*afde0c52Smatthias.ringwald                     // CONNECTION TERMINATED BY LOCAL HOST (0X16)
197*afde0c52Smatthias.ringwald                     l2cap_handle_connection_failed_for_addr(address, 0x16);
19803cfbabcSmatthias.ringwald                 }
1991e6aba47Smatthias.ringwald             }
200*afde0c52Smatthias.ringwald             break;
20127a923d0Smatthias.ringwald 
2021e6aba47Smatthias.ringwald         // handle disconnection complete events
203*afde0c52Smatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
20427a923d0Smatthias.ringwald             // send l2cap disconnect events for all channels on this handle
205*afde0c52Smatthias.ringwald             handle = READ_BT_16(packet, 3);
20627a923d0Smatthias.ringwald             linked_item_t *it;
20727a923d0Smatthias.ringwald             for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
20827a923d0Smatthias.ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) it;
20927a923d0Smatthias.ringwald                 if ( channel->handle == handle ){
21027a923d0Smatthias.ringwald                     l2cap_finialize_channel_close(channel);
21127a923d0Smatthias.ringwald                 }
21227a923d0Smatthias.ringwald             }
213*afde0c52Smatthias.ringwald             break;
214fcadd0caSmatthias.ringwald 
215ee091cf1Smatthias.ringwald         // HCI Connection Timeouts
216*afde0c52Smatthias.ringwald         case L2CAP_EVENT_TIMEOUT_CHECK:
217*afde0c52Smatthias.ringwald             if (!capture_connection){
218ee091cf1Smatthias.ringwald                 hci_con_handle_t handle = READ_BT_16(packet, 2);
219ee091cf1Smatthias.ringwald                 linked_item_t *it;
220ee091cf1Smatthias.ringwald                 l2cap_channel_t * channel;
221ee091cf1Smatthias.ringwald                 int used = 0;
222ee091cf1Smatthias.ringwald                 for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
223ee091cf1Smatthias.ringwald                     channel = (l2cap_channel_t *) it;
224ee091cf1Smatthias.ringwald                     if (channel->handle == handle) {
225ee091cf1Smatthias.ringwald                         used = 1;
226ee091cf1Smatthias.ringwald                     }
227ee091cf1Smatthias.ringwald                 }
228ee091cf1Smatthias.ringwald                 if (!used) {
2299edc8742Smatthias.ringwald                     hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection
230ee091cf1Smatthias.ringwald                 }
231ee091cf1Smatthias.ringwald             }
232*afde0c52Smatthias.ringwald             break;
233ee091cf1Smatthias.ringwald 
234*afde0c52Smatthias.ringwald         default:
235*afde0c52Smatthias.ringwald             break;
236*afde0c52Smatthias.ringwald     }
237*afde0c52Smatthias.ringwald 
238*afde0c52Smatthias.ringwald     // pass on
239fcadd0caSmatthias.ringwald     (*event_packet_handler)(packet, size);
2401e6aba47Smatthias.ringwald }
2411e6aba47Smatthias.ringwald 
242*afde0c52Smatthias.ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){
24384836b65Smatthias.ringwald     l2cap_send_signaling_packet( channel->handle, DISCONNECTION_RESPONSE, identifier, channel->dest_cid, channel->source_cid);
24484836b65Smatthias.ringwald     l2cap_finialize_channel_close(channel);
24584836b65Smatthias.ringwald }
24684836b65Smatthias.ringwald 
2471e6aba47Smatthias.ringwald void l2cap_signaling_handler(l2cap_channel_t *channel, uint8_t *packet, uint16_t size){
2481e6aba47Smatthias.ringwald 
2491e6aba47Smatthias.ringwald     static uint8_t config_options[] = { 1, 2, 150, 0}; // mtu = 48
2501e6aba47Smatthias.ringwald 
2511e6aba47Smatthias.ringwald     uint8_t  code       = READ_L2CAP_SIGNALING_CODE( packet );
2521e6aba47Smatthias.ringwald     uint8_t  identifier = READ_L2CAP_SIGNALING_IDENTIFIER( packet );
25338e5900eSmatthias.ringwald     uint16_t result = 0;
2541e6aba47Smatthias.ringwald 
2551e6aba47Smatthias.ringwald     switch (channel->state) {
2561e6aba47Smatthias.ringwald 
2571e6aba47Smatthias.ringwald         case L2CAP_STATE_WAIT_CONNECT_RSP:
2581e6aba47Smatthias.ringwald             switch (code){
2591e6aba47Smatthias.ringwald                 case CONNECTION_RESPONSE:
260141679a4Smatthias.ringwald                     result = READ_BT_16 (packet, L2CAP_SIGNALING_DATA_OFFSET+4);
26138e5900eSmatthias.ringwald                     switch (result) {
26238e5900eSmatthias.ringwald                         case 0:
2631e6aba47Smatthias.ringwald                             // successfull connection
264cd56f931Smatthias.ringwald                             channel->dest_cid = READ_BT_16(packet, L2CAP_SIGNALING_DATA_OFFSET);
2651e6aba47Smatthias.ringwald                             channel->sig_id = l2cap_next_sig_id();
2661e6aba47Smatthias.ringwald                             l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->sig_id, channel->dest_cid, 0, 4, &config_options);
2675a67bd4aSmatthias.ringwald                             channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ;
26838e5900eSmatthias.ringwald                             break;
26938e5900eSmatthias.ringwald                         case 1:
27038e5900eSmatthias.ringwald                             // connection pending. get some coffee
27138e5900eSmatthias.ringwald                             break;
27238e5900eSmatthias.ringwald                         default:
273f32b992eSmatthias.ringwald                             // map l2cap connection response result to BTstack status enumeration
27438e5900eSmatthias.ringwald                             l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result);
27538e5900eSmatthias.ringwald                             break;
2761e6aba47Smatthias.ringwald                     }
2771e6aba47Smatthias.ringwald                     break;
27838e5900eSmatthias.ringwald 
27984836b65Smatthias.ringwald                 case DISCONNECTION_REQUEST:
28084836b65Smatthias.ringwald                     l2cap_handle_disconnect_request(channel, identifier);
28184836b65Smatthias.ringwald                     break;
28284836b65Smatthias.ringwald 
28338e5900eSmatthias.ringwald                 default:
2841e6aba47Smatthias.ringwald                     //@TODO: implement other signaling packets
28538e5900eSmatthias.ringwald                     break;
2861e6aba47Smatthias.ringwald             }
2871e6aba47Smatthias.ringwald             break;
2881e6aba47Smatthias.ringwald 
2895a67bd4aSmatthias.ringwald         case L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ:
2901e6aba47Smatthias.ringwald             switch (code) {
2911e6aba47Smatthias.ringwald                 case CONFIGURE_RESPONSE:
2921e6aba47Smatthias.ringwald                     channel->state = L2CAP_STATE_WAIT_CONFIG_REQ;
2931e6aba47Smatthias.ringwald                     break;
2945a67bd4aSmatthias.ringwald                 case CONFIGURE_REQUEST:
2955a67bd4aSmatthias.ringwald                     // accept the other's configuration options
2965a67bd4aSmatthias.ringwald                     l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, identifier, channel->dest_cid, 0, 0, size - 16, &packet[16]);
2975a67bd4aSmatthias.ringwald                     channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP;
2985a67bd4aSmatthias.ringwald                     break;
29984836b65Smatthias.ringwald                 case DISCONNECTION_REQUEST:
30084836b65Smatthias.ringwald                     l2cap_handle_disconnect_request(channel, identifier);
30184836b65Smatthias.ringwald                     break;
3025a67bd4aSmatthias.ringwald                 default:
3035a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
3045a67bd4aSmatthias.ringwald                     break;
3051e6aba47Smatthias.ringwald             }
3061e6aba47Smatthias.ringwald             break;
3071e6aba47Smatthias.ringwald 
3081e6aba47Smatthias.ringwald         case L2CAP_STATE_WAIT_CONFIG_REQ:
3091e6aba47Smatthias.ringwald             switch (code) {
3101e6aba47Smatthias.ringwald                 case CONFIGURE_REQUEST:
3111e6aba47Smatthias.ringwald                     // accept the other's configuration options
3121e6aba47Smatthias.ringwald                     l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, identifier, channel->dest_cid, 0, 0, size - 16, &packet[16]);
3131e6aba47Smatthias.ringwald                     channel->state = L2CAP_STATE_OPEN;
31403cfbabcSmatthias.ringwald                     l2cap_emit_channel_opened(channel, 0);  // success
315c8e4258aSmatthias.ringwald                     break;
31684836b65Smatthias.ringwald                 case DISCONNECTION_REQUEST:
31784836b65Smatthias.ringwald                     l2cap_handle_disconnect_request(channel, identifier);
31884836b65Smatthias.ringwald                     break;
3195a67bd4aSmatthias.ringwald                 default:
3205a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
3215a67bd4aSmatthias.ringwald                     break;
3225a67bd4aSmatthias.ringwald             }
3235a67bd4aSmatthias.ringwald             break;
3245a67bd4aSmatthias.ringwald 
3255a67bd4aSmatthias.ringwald         case L2CAP_STATE_WAIT_CONFIG_REQ_RSP:
3265a67bd4aSmatthias.ringwald             switch (code) {
3275a67bd4aSmatthias.ringwald                 case CONFIGURE_RESPONSE:
3285a67bd4aSmatthias.ringwald                     channel->state = L2CAP_STATE_OPEN;
3295a67bd4aSmatthias.ringwald                     l2cap_emit_channel_opened(channel, 0);  // success
3305a67bd4aSmatthias.ringwald                     break;
33184836b65Smatthias.ringwald                 case DISCONNECTION_REQUEST:
33284836b65Smatthias.ringwald                     l2cap_handle_disconnect_request(channel, identifier);
33384836b65Smatthias.ringwald                     break;
3345a67bd4aSmatthias.ringwald                 default:
3355a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
3365a67bd4aSmatthias.ringwald                     break;
337c8e4258aSmatthias.ringwald             }
338c8e4258aSmatthias.ringwald             break;
339f62db1e3Smatthias.ringwald 
340f62db1e3Smatthias.ringwald         case L2CAP_STATE_WAIT_DISCONNECT:
341f62db1e3Smatthias.ringwald             switch (code) {
342f62db1e3Smatthias.ringwald                 case DISCONNECTION_RESPONSE:
34327a923d0Smatthias.ringwald                     l2cap_finialize_channel_close(channel);
34427a923d0Smatthias.ringwald                     break;
34584836b65Smatthias.ringwald                 case DISCONNECTION_REQUEST:
34684836b65Smatthias.ringwald                     l2cap_handle_disconnect_request(channel, identifier);
34784836b65Smatthias.ringwald                     break;
3485a67bd4aSmatthias.ringwald                 default:
3495a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
3505a67bd4aSmatthias.ringwald                     break;
35127a923d0Smatthias.ringwald             }
35227a923d0Smatthias.ringwald             break;
35384836b65Smatthias.ringwald 
35484836b65Smatthias.ringwald         case L2CAP_STATE_CLOSED:
35584836b65Smatthias.ringwald             // @TODO handle incoming requests
35684836b65Smatthias.ringwald             break;
35784836b65Smatthias.ringwald 
35884836b65Smatthias.ringwald         case L2CAP_STATE_OPEN:
35984836b65Smatthias.ringwald             switch (code) {
36084836b65Smatthias.ringwald                 case DISCONNECTION_REQUEST:
36184836b65Smatthias.ringwald                     l2cap_handle_disconnect_request(channel, identifier);
36284836b65Smatthias.ringwald                     break;
3635a67bd4aSmatthias.ringwald                 default:
36484836b65Smatthias.ringwald                     //@TODO: implement other signaling packets, e.g. re-configure
36584836b65Smatthias.ringwald                     break;
36684836b65Smatthias.ringwald             }
3675a67bd4aSmatthias.ringwald             break;
36827a923d0Smatthias.ringwald     }
36927a923d0Smatthias.ringwald }
37027a923d0Smatthias.ringwald 
37127a923d0Smatthias.ringwald // finalize closed channel
37227a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t *channel){
373f62db1e3Smatthias.ringwald     channel->state = L2CAP_STATE_CLOSED;
374f62db1e3Smatthias.ringwald     l2cap_emit_channel_closed(channel);
375f62db1e3Smatthias.ringwald 
376f62db1e3Smatthias.ringwald     // discard channel
377f62db1e3Smatthias.ringwald     linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
378f62db1e3Smatthias.ringwald     free (channel);
379c8e4258aSmatthias.ringwald }
3801e6aba47Smatthias.ringwald 
381c52bf64dSmatthias.ringwald //
382c52bf64dSmatthias.ringwald void l2cap_close_channels_for_connection(connection_t *connection){
383c52bf64dSmatthias.ringwald     linked_item_t *it;
384c52bf64dSmatthias.ringwald     l2cap_channel_t * channel;
385c52bf64dSmatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
386c52bf64dSmatthias.ringwald         channel = (l2cap_channel_t *) it;
387c52bf64dSmatthias.ringwald         if ( channel->connection == connection) {
388c52bf64dSmatthias.ringwald             channel->sig_id = l2cap_next_sig_id();
389c52bf64dSmatthias.ringwald             l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->sig_id, channel->dest_cid, channel->source_cid);
390c52bf64dSmatthias.ringwald             channel->state = L2CAP_STATE_WAIT_DISCONNECT;
391c52bf64dSmatthias.ringwald         }
392c52bf64dSmatthias.ringwald     }
393c52bf64dSmatthias.ringwald }
394c52bf64dSmatthias.ringwald 
3951e6aba47Smatthias.ringwald //  notify client
39603cfbabcSmatthias.ringwald void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) {
39703cfbabcSmatthias.ringwald     uint8_t event[17];
39880d52d6bSmatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_OPENED;
399c8e4258aSmatthias.ringwald     event[1] = sizeof(event) - 2;
40003cfbabcSmatthias.ringwald     event[2] = status;
40103cfbabcSmatthias.ringwald     bt_flip_addr(&event[3], channel->address);
40203cfbabcSmatthias.ringwald     bt_store_16(event,  9, channel->handle);
40303cfbabcSmatthias.ringwald     bt_store_16(event, 11, channel->psm);
40403cfbabcSmatthias.ringwald     bt_store_16(event, 13, channel->source_cid);
40503cfbabcSmatthias.ringwald     bt_store_16(event, 15, channel->dest_cid);
4061e6aba47Smatthias.ringwald     socket_connection_send_packet(channel->connection, HCI_EVENT_PACKET, 0, event, sizeof(event));
4071e6aba47Smatthias.ringwald }
4081e6aba47Smatthias.ringwald 
409f62db1e3Smatthias.ringwald void l2cap_emit_channel_closed(l2cap_channel_t *channel) {
410f62db1e3Smatthias.ringwald     uint8_t event[4];
41180d52d6bSmatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_CLOSED;
412f62db1e3Smatthias.ringwald     event[1] = sizeof(event) - 2;
413f62db1e3Smatthias.ringwald     bt_store_16(event, 2, channel->source_cid);
414f62db1e3Smatthias.ringwald     socket_connection_send_packet(channel->connection, HCI_EVENT_PACKET, 0, event, sizeof(event));
415f62db1e3Smatthias.ringwald }
416f62db1e3Smatthias.ringwald 
4171e6aba47Smatthias.ringwald void l2cap_acl_handler( uint8_t *packet, uint16_t size ){
4181e6aba47Smatthias.ringwald 
4199edc8742Smatthias.ringwald     // Capturing?
4209edc8742Smatthias.ringwald     if (capture_connection) {
4219edc8742Smatthias.ringwald         socket_connection_send_packet(capture_connection, HCI_ACL_DATA_PACKET, 0, packet, size);
422d6f03c5dSmatthias.ringwald         return;
4239edc8742Smatthias.ringwald     }
4249edc8742Smatthias.ringwald 
4259edc8742Smatthias.ringwald     // forward to higher layers - not needed yet
4269edc8742Smatthias.ringwald     // (*data_packet_handler)(channel_id, packet, size);
4279edc8742Smatthias.ringwald 
4281e6aba47Smatthias.ringwald     // Get Channel ID and command code
4291e6aba47Smatthias.ringwald     uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet);
4301e6aba47Smatthias.ringwald     uint8_t  code       = READ_L2CAP_SIGNALING_CODE( packet );
4311e6aba47Smatthias.ringwald 
4321e6aba47Smatthias.ringwald     // Get Connection
4331e6aba47Smatthias.ringwald     hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet);
4341e6aba47Smatthias.ringwald 
4351e6aba47Smatthias.ringwald     // Signaling Packet?
4361e6aba47Smatthias.ringwald     if (channel_id == 1) {
4371e6aba47Smatthias.ringwald 
4381e6aba47Smatthias.ringwald         if (code < 1 || code == 2 || code >= 8){
4391e6aba47Smatthias.ringwald             // not for a particular channel
4401e6aba47Smatthias.ringwald             return;
4411e6aba47Smatthias.ringwald         }
4421e6aba47Smatthias.ringwald 
4431e6aba47Smatthias.ringwald         // Get Signaling Identifier and potential destination CID
4441e6aba47Smatthias.ringwald         uint8_t sig_id    = READ_L2CAP_SIGNALING_IDENTIFIER(packet);
4451e6aba47Smatthias.ringwald         uint16_t dest_cid = READ_BT_16(packet, L2CAP_SIGNALING_DATA_OFFSET);
4461e6aba47Smatthias.ringwald 
4471e6aba47Smatthias.ringwald         // Find channel for this sig_id and connection handle
4481e6aba47Smatthias.ringwald         linked_item_t *it;
4491e6aba47Smatthias.ringwald         for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
450b448a0e7Smatthias.ringwald             l2cap_channel_t * channel = (l2cap_channel_t *) it;
451b448a0e7Smatthias.ringwald             if (channel->handle == handle) {
4521e6aba47Smatthias.ringwald                 if (code & 1) {
4531e6aba47Smatthias.ringwald                     // match odd commands by previous signaling identifier
454b448a0e7Smatthias.ringwald                     if (channel->sig_id == sig_id) {
455b448a0e7Smatthias.ringwald                         l2cap_signaling_handler( channel, packet, size);
4561e6aba47Smatthias.ringwald                     }
4571e6aba47Smatthias.ringwald                 } else {
4581e6aba47Smatthias.ringwald                     // match even commands by source channel id
459b448a0e7Smatthias.ringwald                     if (channel->source_cid == dest_cid) {
460b448a0e7Smatthias.ringwald                         l2cap_signaling_handler( channel, packet, size);
4611e6aba47Smatthias.ringwald                     }
4621e6aba47Smatthias.ringwald                 }
4631e6aba47Smatthias.ringwald             }
4641e6aba47Smatthias.ringwald         }
4651e6aba47Smatthias.ringwald         return;
4661e6aba47Smatthias.ringwald     }
4671e6aba47Smatthias.ringwald 
4681e6aba47Smatthias.ringwald     // Find channel for this channel_id and connection handle
469f62db1e3Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_source_cid(channel_id);
470f62db1e3Smatthias.ringwald     if (channel) {
4716f60b3f4Smatthias.ringwald         socket_connection_send_packet(channel->connection, L2CAP_DATA_PACKET, channel_id,
4726f60b3f4Smatthias.ringwald                                       &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
4731e6aba47Smatthias.ringwald     }
4741e6aba47Smatthias.ringwald }
4751e6aba47Smatthias.ringwald 
476f62db1e3Smatthias.ringwald 
4771e6aba47Smatthias.ringwald void l2cap_send_internal(uint16_t source_cid, uint8_t *data, uint16_t len){
4781e6aba47Smatthias.ringwald     // find channel for source_cid, construct l2cap packet and send
479f62db1e3Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_source_cid(source_cid);
480fcadd0caSmatthias.ringwald     if (channel) {
4811e6aba47Smatthias.ringwald          // 0 - Connection handle : PB=10 : BC=00
4821e6aba47Smatthias.ringwald          bt_store_16(acl_buffer, 0, channel->handle | (2 << 12) | (0 << 14));
4831e6aba47Smatthias.ringwald          // 2 - ACL length
4841e6aba47Smatthias.ringwald          bt_store_16(acl_buffer, 2,  len + 4);
4851e6aba47Smatthias.ringwald          // 4 - L2CAP packet length
4861e6aba47Smatthias.ringwald          bt_store_16(acl_buffer, 4,  len + 0);
4871e6aba47Smatthias.ringwald          // 6 - L2CAP channel DEST
4881e6aba47Smatthias.ringwald          bt_store_16(acl_buffer, 6, channel->dest_cid);
4891e6aba47Smatthias.ringwald          // 8 - data
4901e6aba47Smatthias.ringwald          memcpy(&acl_buffer[8], data, len);
4911e6aba47Smatthias.ringwald          // send
4921e6aba47Smatthias.ringwald          hci_send_acl_packet(acl_buffer, len+8);
4931e6aba47Smatthias.ringwald      }
4941e6aba47Smatthias.ringwald }
4951e6aba47Smatthias.ringwald 
4969edc8742Smatthias.ringwald void l2cap_set_capture_connection(connection_t * connection){
4979edc8742Smatthias.ringwald     capture_connection = connection;
4989edc8742Smatthias.ringwald }
4991e6aba47Smatthias.ringwald 
500b448a0e7Smatthias.ringwald