xref: /btstack/src/l2cap.c (revision 1dc511dea629126f5f36a569073789740cf0ac23)
1 /*
2  * Copyright (C) 2009 by Matthias Ringwald
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
21  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
24  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  */
31 
32 /*
33  *  l2cap.c
34  *
35  *  Logical Link Control and Adaption Protocl (L2CAP)
36  *
37  *  Created by Matthias Ringwald on 5/16/09.
38  */
39 
40 #include "l2cap.h"
41 #include "hci.h"
42 #include "hci_dump.h"
43 
44 #include <stdarg.h>
45 #include <string.h>
46 
47 #include <stdio.h>
48 
49 // size of HCI ACL + L2CAP Header for regular data packets
50 #define COMPLETE_L2CAP_HEADER 8
51 
52 // minimum signaling MTU
53 #define L2CAP_MINIMAL_MTU 48
54 #define L2CAP_DEFAULT_MTU 672
55 
56 // offsets for L2CAP SIGNALING COMMANDS
57 #define L2CAP_SIGNALING_COMMAND_CODE_OFFSET   0
58 #define L2CAP_SIGNALING_COMMAND_SIGID_OFFSET  1
59 #define L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET 2
60 #define L2CAP_SIGNALING_COMMAND_DATA_OFFSET   4
61 
62 static void null_event_handler(connection_t * connection, uint8_t *packet, uint16_t size);
63 static void null_data_handler(connection_t * connection, uint16_t local_cid, uint8_t *packet, uint16_t size);
64 
65 static uint8_t * sig_buffer = NULL;
66 static linked_list_t l2cap_channels = NULL;
67 static linked_list_t l2cap_services = NULL;
68 static uint8_t * acl_buffer = NULL;
69 static void (*event_packet_handler) (connection_t * connection, uint8_t *packet, uint16_t size) = null_event_handler;
70 static void (*data_packet_handler)  (connection_t * connection, uint16_t local_cid, uint8_t *packet, uint16_t size) = null_data_handler;
71 static connection_t * capture_connection = NULL;
72 
73 static uint8_t config_options[] = { 1, 2, 150, 0}; // mtu = 48
74 
75 void l2cap_init(){
76     sig_buffer = malloc( L2CAP_MINIMAL_MTU );
77     // acl_buffer = malloc( 255 + 8 );
78     acl_buffer = malloc( 400 + 8 );
79 
80     //
81     // register callbacks with HCI
82     //
83     hci_register_event_packet_handler(&l2cap_event_handler);
84     hci_register_acl_packet_handler(&l2cap_acl_handler);
85 }
86 
87 
88 /** Register L2CAP packet handlers */
89 static void null_event_handler(connection_t * connection, uint8_t *packet, uint16_t size){
90 }
91 static void null_data_handler(connection_t * connection, uint16_t  local_cid, uint8_t *packet, uint16_t size){
92 }
93 void l2cap_register_event_packet_handler(void (*handler)(connection_t * connection, uint8_t *packet, uint16_t size)){
94     event_packet_handler = handler;
95 }
96 void l2cap_register_data_packet_handler  (void (*handler)(connection_t * connection, uint16_t local_cid, uint8_t *packet, uint16_t size)){
97     data_packet_handler = handler;
98 }
99 
100 //  notify client/protocol handler
101 void l2cap_dispatch(l2cap_channel_t *channel, uint8_t type, uint8_t * data, uint16_t size){
102     if (channel->packet_handler) {
103         (* (channel->packet_handler))(type, channel->local_cid, data, size);
104     } else {
105         switch (type){
106             case HCI_EVENT_PACKET:
107                 (*event_packet_handler)(channel->connection, data, size);
108                 break;
109             case L2CAP_DATA_PACKET:
110                 (*data_packet_handler)(channel->connection, channel->local_cid, data, size);
111                 break;
112             default:
113                 // ??
114                 break;
115         }
116     }
117 }
118 
119 void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) {
120     uint8_t event[17];
121     event[0] = L2CAP_EVENT_CHANNEL_OPENED;
122     event[1] = sizeof(event) - 2;
123     event[2] = status;
124     bt_flip_addr(&event[3], channel->address);
125     bt_store_16(event,  9, channel->handle);
126     bt_store_16(event, 11, channel->psm);
127     bt_store_16(event, 13, channel->local_cid);
128     bt_store_16(event, 15, channel->remote_cid);
129     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
130     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
131 }
132 
133 void l2cap_emit_channel_closed(l2cap_channel_t *channel) {
134     uint8_t event[4];
135     event[0] = L2CAP_EVENT_CHANNEL_CLOSED;
136     event[1] = sizeof(event) - 2;
137     bt_store_16(event, 2, channel->local_cid);
138     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
139     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
140 }
141 
142 void l2cap_emit_connection_request(l2cap_channel_t *channel) {
143     uint8_t event[16];
144     event[0] = L2CAP_EVENT_INCOMING_CONNECTION;
145     event[1] = sizeof(event) - 2;
146     bt_flip_addr(&event[2], channel->address);
147     bt_store_16(event,  8, channel->handle);
148     bt_store_16(event, 10, channel->psm);
149     bt_store_16(event, 12, channel->local_cid);
150     bt_store_16(event, 14, channel->remote_cid);
151     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
152     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
153 }
154 
155 l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid){
156     linked_item_t *it;
157     l2cap_channel_t * channel;
158     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
159         channel = (l2cap_channel_t *) it;
160         if ( channel->local_cid == local_cid) {
161             return channel;
162         }
163     }
164     return NULL;
165 }
166 
167 int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){
168     // printf("l2cap_send_signaling_packet type %u\n", cmd);
169     va_list argptr;
170     va_start(argptr, identifier);
171     uint16_t len = l2cap_create_signaling_internal(sig_buffer, handle, cmd, identifier, argptr);
172     va_end(argptr);
173     return hci_send_acl_packet(sig_buffer, len);
174 }
175 
176 void l2cap_send_internal(uint16_t local_cid, uint8_t *data, uint16_t len){
177     // find channel for local_cid, construct l2cap packet and send
178     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
179     if (channel) {
180         // 0 - Connection handle : PB=10 : BC=00
181         bt_store_16(acl_buffer, 0, channel->handle | (2 << 12) | (0 << 14));
182         // 2 - ACL length
183         bt_store_16(acl_buffer, 2,  len + 4);
184         // 4 - L2CAP packet length
185         bt_store_16(acl_buffer, 4,  len + 0);
186         // 6 - L2CAP channel DEST
187         bt_store_16(acl_buffer, 6, channel->remote_cid);
188         // 8 - data
189         memcpy(&acl_buffer[8], data, len);
190         // send
191         hci_send_acl_packet(acl_buffer, len+8);
192     }
193 }
194 
195 // open outgoing L2CAP channel
196 void l2cap_create_channel_internal(connection_t * connection, bd_addr_t address, uint16_t psm){
197 
198     // alloc structure
199     l2cap_channel_t * chan = malloc(sizeof(l2cap_channel_t));
200     // TODO: emit error event
201     if (!chan) return;
202 
203     // fill in
204     BD_ADDR_COPY(chan->address, address);
205     chan->psm = psm;
206     chan->handle = 0;
207     chan->connection = connection;
208     chan->packet_handler = NULL;
209     chan->remote_mtu = L2CAP_MINIMAL_MTU;
210 
211     // set initial state
212     chan->state = L2CAP_STATE_CLOSED;
213     chan->sig_id = L2CAP_SIG_ID_INVALID;
214 
215     // add to connections list
216     linked_list_add(&l2cap_channels, (linked_item_t *) chan);
217 
218     // send connection request
219     // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch
220     hci_send_cmd(&hci_create_connection, address, 0x18, 0, 0, 0, 0);
221 }
222 
223 void l2cap_disconnect_internal(uint16_t local_cid, uint8_t reason){
224     // find channel for local_cid
225     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
226     if (channel) {
227         channel->sig_id = l2cap_next_sig_id();
228         l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->sig_id, channel->remote_cid, channel->local_cid);
229         channel->state = L2CAP_STATE_WAIT_DISCONNECT;
230     }
231 }
232 
233 static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){
234     linked_item_t *it;
235     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
236         l2cap_channel_t * channel = (l2cap_channel_t *) it;
237         if ( ! BD_ADDR_CMP( channel->address, address) ){
238             if (channel->state == L2CAP_STATE_CLOSED) {
239                 // failure, forward error code
240                 l2cap_emit_channel_opened(channel, status);
241                 // discard channel
242                 linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
243                 free (channel);
244             }
245         }
246     }
247 }
248 
249 static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){
250     linked_item_t *it;
251     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
252         l2cap_channel_t * channel = (l2cap_channel_t *) it;
253         if ( ! BD_ADDR_CMP( channel->address, address) ){
254             if (channel->state == L2CAP_STATE_CLOSED) {
255                 // success, start l2cap handshake
256                 channel->handle = handle;
257                 channel->sig_id = l2cap_next_sig_id();
258                 channel->local_cid = l2cap_next_local_cid();
259                 channel->state = L2CAP_STATE_WAIT_CONNECT_RSP;
260                 l2cap_send_signaling_packet( channel->handle, CONNECTION_REQUEST, channel->sig_id, channel->psm, channel->local_cid);
261             }
262         }
263     }
264 }
265 
266 void l2cap_event_handler( uint8_t *packet, uint16_t size ){
267 
268     bd_addr_t address;
269     hci_con_handle_t handle;
270 
271     switch(packet[0]){
272 
273         // handle connection complete events
274         case HCI_EVENT_CONNECTION_COMPLETE:
275             bt_flip_addr(address, &packet[5]);
276             if (packet[2] == 0){
277                 handle = READ_BT_16(packet, 3);
278                 l2cap_handle_connection_success_for_addr(address, handle);
279             } else {
280                 l2cap_handle_connection_failed_for_addr(address, packet[2]);
281             }
282             break;
283 
284         // handle successful create connection cancel command
285         case HCI_EVENT_COMMAND_COMPLETE:
286             if ( COMMAND_COMPLETE_EVENT(packet, hci_create_connection_cancel) ) {
287                 if (packet[5] == 0){
288                     bt_flip_addr(address, &packet[6]);
289                     // CONNECTION TERMINATED BY LOCAL HOST (0X16)
290                     l2cap_handle_connection_failed_for_addr(address, 0x16);
291                 }
292             }
293             break;
294 
295         // handle disconnection complete events
296         case HCI_EVENT_DISCONNECTION_COMPLETE:
297             // send l2cap disconnect events for all channels on this handle
298             handle = READ_BT_16(packet, 3);
299             linked_item_t *it;
300             for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
301                 l2cap_channel_t * channel = (l2cap_channel_t *) it;
302                 if ( channel->handle == handle ){
303                     l2cap_finialize_channel_close(channel);
304                 }
305             }
306             break;
307 
308         // HCI Connection Timeouts
309         case L2CAP_EVENT_TIMEOUT_CHECK:
310             if (!capture_connection){
311                 hci_con_handle_t handle = READ_BT_16(packet, 2);
312                 linked_item_t *it;
313                 l2cap_channel_t * channel;
314                 int used = 0;
315                 for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
316                     channel = (l2cap_channel_t *) it;
317                     if (channel->handle == handle) {
318                         used = 1;
319                     }
320                 }
321                 if (!used) {
322                     hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection
323                 }
324             }
325             break;
326 
327         default:
328             break;
329     }
330 
331     // pass on
332     (*event_packet_handler)(NULL, packet, size);
333 }
334 
335 static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){
336     l2cap_send_signaling_packet( channel->handle, DISCONNECTION_RESPONSE, identifier, channel->local_cid, channel->remote_cid);
337     l2cap_finialize_channel_close(channel);
338 }
339 
340 static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){
341 
342     // printf("l2cap_handle_connection_request for handle %u, psm %u cid %u\n", handle, psm, source_cid);
343     l2cap_service_t *service = l2cap_get_service(psm);
344     if (!service) {
345         // 0x0002 PSM not supported
346         // printf("l2cap_handle_connection_request no PSM for psm %u/n", psm);
347         l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, 0x0002, 0);
348         return;
349     }
350 
351     hci_connection_t * hci_connection = connection_for_handle( handle );
352     if (!hci_connection) {
353         fprintf(stderr, "no hci_connection for handle %u\n", handle);
354         // TODO: emit error
355         return;
356     }
357     // alloc structure
358     // printf("l2cap_handle_connection_request register channel\n");
359     l2cap_channel_t * channel = malloc(sizeof(l2cap_channel_t));
360     // TODO: emit error event
361     if (!channel) return;
362 
363     // fill in
364     BD_ADDR_COPY(channel->address, hci_connection->address);
365     channel->psm = psm;
366     channel->handle = handle;
367     channel->connection = service->connection;
368     channel->packet_handler = service->packet_handler;
369     channel->local_cid  = l2cap_next_local_cid();
370     channel->remote_cid = source_cid;
371     channel->remote_mtu = L2CAP_MINIMAL_MTU;
372 
373     // set initial state
374     channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT;
375 
376     // temp. store req sig id
377     channel->sig_id = sig_id;
378 
379     // add to connections list
380     linked_list_add(&l2cap_channels, (linked_item_t *) channel);
381 
382     // emit incoming connection request
383     l2cap_emit_connection_request(channel);
384 }
385 
386 void l2cap_accept_connection_internal(uint16_t local_cid){
387     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
388     if (!channel) {
389         fprintf(stderr, "l2cap_accept_connection_internal called but local_cid 0x%x not found", local_cid);
390         return;
391     }
392 
393     // accept connection
394     l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->sig_id, channel->local_cid, channel->remote_cid, 0, 0);
395 
396     // set real sig and state and start config
397     channel->sig_id = l2cap_next_sig_id();
398     channel->state  = L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ;
399     l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->sig_id, channel->remote_cid, 0, 4, &config_options);
400 
401     // printf("new state %u\n", channel->state);
402 }
403 
404 void l2cap_decline_connection_internal(uint16_t local_cid, uint8_t reason){
405     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid);
406     if (!channel) {
407         fprintf(stderr, "l2cap_decline_connection_internal called but local_cid 0x%x not found", local_cid,reason);
408         return;
409     }
410     l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->sig_id, 0, 0, reason, 0);
411 
412     // discard channel
413     linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
414     free (channel);
415 }
416 
417 void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){
418     // accept the other's configuration options
419     uint16_t len       = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
420     uint16_t pos = 4;
421     while (pos < len){
422         uint8_t type   = command[pos++];
423         uint8_t length = command[pos++];
424         // MTU { type(8): 1, len(8):2, MTU(16) }
425         if ((type & 0x7f) == 1 && length == 2){
426             channel->remote_mtu = READ_BT_16(command, pos);
427         }
428         pos += length;
429     }
430     uint8_t identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
431     // send back received options
432     // l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, identifier, channel->remote_cid, 0, 0, len-4, &command[8]);
433     // send back OK
434     l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, identifier, channel->remote_cid, 0, 0, 0, NULL);
435 }
436 
437 void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){
438 
439     uint8_t code       = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
440     uint8_t identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
441     uint16_t result = 0;
442 
443     // printf("signaling handler code %u\n", code);
444 
445     switch (channel->state) {
446 
447         case L2CAP_STATE_WAIT_CONNECT_RSP:
448             switch (code){
449                 case CONNECTION_RESPONSE:
450                     result = READ_BT_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
451                     switch (result) {
452                         case 0:
453                             // successful connection
454                             channel->remote_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
455                             channel->sig_id = l2cap_next_sig_id();
456                             l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->sig_id, channel->remote_cid, 0, 4, &config_options);
457                             channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ;
458                             break;
459                         case 1:
460                             // connection pending. get some coffee
461                             break;
462                         default:
463                             // map l2cap connection response result to BTstack status enumeration
464                             l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result);
465                             break;
466                     }
467                     break;
468 
469                 case DISCONNECTION_REQUEST:
470                     l2cap_handle_disconnect_request(channel, identifier);
471                     break;
472 
473                 default:
474                     //@TODO: implement other signaling packets
475                     break;
476             }
477             break;
478 
479         case L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ:
480             switch (code) {
481                 case CONFIGURE_RESPONSE:
482                     channel->state = L2CAP_STATE_WAIT_CONFIG_REQ;
483                     break;
484                 case CONFIGURE_REQUEST:
485                     l2cap_signaling_handle_configure_request(channel, command);
486                     channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP;
487                     break;
488                 case DISCONNECTION_REQUEST:
489                     l2cap_handle_disconnect_request(channel, identifier);
490                     break;
491                 default:
492                     //@TODO: implement other signaling packets
493                     break;
494             }
495             break;
496 
497         case L2CAP_STATE_WAIT_CONFIG_REQ:
498             switch (code) {
499                 case CONFIGURE_REQUEST:
500                     l2cap_signaling_handle_configure_request(channel, command);
501                     channel->state = L2CAP_STATE_OPEN;
502                     l2cap_emit_channel_opened(channel, 0);  // success
503                     break;
504                 case DISCONNECTION_REQUEST:
505                     l2cap_handle_disconnect_request(channel, identifier);
506                     break;
507                 default:
508                     //@TODO: implement other signaling packets
509                     break;
510             }
511             break;
512 
513         case L2CAP_STATE_WAIT_CONFIG_REQ_RSP:
514             switch (code) {
515                 case CONFIGURE_RESPONSE:
516                     channel->state = L2CAP_STATE_OPEN;
517                     l2cap_emit_channel_opened(channel, 0);  // success
518                     break;
519                 case DISCONNECTION_REQUEST:
520                     l2cap_handle_disconnect_request(channel, identifier);
521                     break;
522                 default:
523                     //@TODO: implement other signaling packets
524                     break;
525             }
526             break;
527 
528         case L2CAP_STATE_WAIT_DISCONNECT:
529             switch (code) {
530                 case DISCONNECTION_RESPONSE:
531                     l2cap_finialize_channel_close(channel);
532                     break;
533                 case DISCONNECTION_REQUEST:
534                     l2cap_handle_disconnect_request(channel, identifier);
535                     break;
536                 default:
537                     //@TODO: implement other signaling packets
538                     break;
539             }
540             break;
541 
542         case L2CAP_STATE_CLOSED:
543             // @TODO handle incoming requests
544             break;
545 
546         case L2CAP_STATE_OPEN:
547             switch (code) {
548                 case DISCONNECTION_REQUEST:
549                     l2cap_handle_disconnect_request(channel, identifier);
550                     break;
551                 default:
552                     //@TODO: implement other signaling packets, e.g. re-configure
553                     break;
554             }
555             break;
556     }
557     // printf("new state %u\n", channel->state);
558 }
559 
560 
561 void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){
562 
563     // get code, signalind identifier and command len
564     uint8_t code   = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
565     uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
566     uint16_t len   = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
567 
568     // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST
569     if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){
570         return;
571     }
572 
573     // general commands without an assigned channel
574     switch(code) {
575 
576         case CONNECTION_REQUEST: {
577             uint16_t psm =        READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
578             uint16_t source_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2);
579             l2cap_handle_connection_request(handle, sig_id, psm, source_cid);
580             break;
581         }
582 
583         case ECHO_REQUEST: {
584             // send back packet
585             l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, len, &command[L2CAP_SIGNALING_COMMAND_DATA_OFFSET]);
586             break;
587         }
588 
589         case INFORMATION_REQUEST: {
590             // we neither support connectionless L2CAP data nor support any flow control modes yet
591             uint16_t infoType = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
592             if (infoType == 2) {
593                 uint32_t features = 0;
594                 // extended features request supported, however no features present
595                 l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, 4, &features);
596             } else {
597                 // all other types are not supported
598                 l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL);
599             }
600             break;;
601         }
602 
603         default:
604             break;
605     }
606 
607 
608     // Get potential destination CID
609     uint16_t dest_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
610 
611     // Find channel for this sig_id and connection handle
612     linked_item_t *it;
613     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
614         l2cap_channel_t * channel = (l2cap_channel_t *) it;
615         if (channel->handle == handle) {
616             if (code & 1) {
617                 // match odd commands by previous signaling identifier
618                 if (channel->sig_id == sig_id) {
619                     l2cap_signaling_handler_channel(channel, command);
620                 }
621             } else {
622                 // match even commands by local channel id
623                 if (channel->local_cid == dest_cid) {
624                     l2cap_signaling_handler_channel(channel, command);
625                 }
626             }
627         }
628     }
629 }
630 
631 void l2cap_acl_handler( uint8_t *packet, uint16_t size ){
632 
633     // Capturing?
634     if (capture_connection) {
635         (*data_packet_handler)(capture_connection, 0, packet, size);
636         return;
637     }
638 
639     // Get Channel ID
640     uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet);
641 
642     // Signaling Packet?
643     if (channel_id == 1) {
644 
645         // Get Connection
646         hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet);
647 
648         uint16_t command_offset = 8;
649         while (command_offset < size) {
650 
651             // handle signaling commands
652             l2cap_signaling_handler_dispatch(handle, &packet[command_offset]);
653 
654             // increment command_offset
655             command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + READ_BT_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
656         }
657         return;
658     }
659 
660     // Find channel for this channel_id and connection handle
661     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(channel_id);
662     if (channel) {
663         l2cap_dispatch(channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
664     }
665 }
666 
667 
668 // finalize closed channel
669 void l2cap_finialize_channel_close(l2cap_channel_t *channel){
670     channel->state = L2CAP_STATE_CLOSED;
671     l2cap_emit_channel_closed(channel);
672 
673     // discard channel
674     linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
675     free (channel);
676 }
677 
678 l2cap_service_t * l2cap_get_service(uint16_t psm){
679     linked_item_t *it;
680 
681     // close open channels
682     for (it = (linked_item_t *) l2cap_services; it ; it = it->next){
683         l2cap_service_t * service = ((l2cap_service_t *) it);
684         if ( service->psm == psm){
685             return service;
686         };
687     }
688     return NULL;
689 }
690 
691 void l2cap_register_service_internal(connection_t *connection, btstack_packet_handler_t packet_handler, uint16_t psm, uint16_t mtu){
692     // check for alread registered psm // TODO: emit error event
693     l2cap_service_t *service = l2cap_get_service(psm);
694     if (service) return;
695 
696     // alloc structure     // TODO: emit error event
697     service = malloc(sizeof(l2cap_service_t));
698     if (!service) return;
699 
700     // fill in
701     service->psm = psm;
702     service->mtu = mtu;
703     service->connection = connection;
704     service->packet_handler = packet_handler;
705 
706     // add to services list
707     linked_list_add(&l2cap_services, (linked_item_t *) service);
708 }
709 
710 void l2cap_unregister_service_internal(connection_t *connection, uint16_t psm){
711     l2cap_service_t *service = l2cap_get_service(psm);
712     if (!service) return;
713     linked_list_remove(&l2cap_services, (linked_item_t *) service);
714     free(service);
715 }
716 
717 //
718 void l2cap_close_connection(connection_t *connection){
719     linked_item_t *it;
720 
721     // close open channels
722     l2cap_channel_t * channel;
723     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
724         channel = (l2cap_channel_t *) it;
725         if (channel->connection == connection) {
726             channel->sig_id = l2cap_next_sig_id();
727             l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->sig_id, channel->remote_cid, channel->local_cid);
728             channel->state = L2CAP_STATE_WAIT_DISCONNECT;
729         }
730     }
731 
732     // unregister services
733     it = (linked_item_t *) &l2cap_services;
734     while (it->next){
735         l2cap_service_t * service = (l2cap_service_t *) it->next;
736         if (service->connection == connection){
737             it->next = it->next->next;
738             free(service);
739         } else {
740             it = it->next;
741         }
742     }
743 }
744 
745 
746 void l2cap_set_capture_connection(connection_t * connection){
747     capture_connection = connection;
748 }
749 
750