xref: /btstack/src/l2cap.c (revision 96cbd66294eb1ee46cfceebe63823fb8b282dcde)
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 uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid){
168     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
169     if (channel) {
170         return channel->remote_mtu;
171     }
172     return 0;
173 }
174 
175 int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){
176     // printf("l2cap_send_signaling_packet type %u\n", cmd);
177     va_list argptr;
178     va_start(argptr, identifier);
179     uint16_t len = l2cap_create_signaling_internal(sig_buffer, handle, cmd, identifier, argptr);
180     va_end(argptr);
181     return hci_send_acl_packet(sig_buffer, len);
182 }
183 
184 void l2cap_send_internal(uint16_t local_cid, uint8_t *data, uint16_t len){
185     // find channel for local_cid, construct l2cap packet and send
186     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
187     if (channel) {
188         // 0 - Connection handle : PB=10 : BC=00
189         bt_store_16(acl_buffer, 0, channel->handle | (2 << 12) | (0 << 14));
190         // 2 - ACL length
191         bt_store_16(acl_buffer, 2,  len + 4);
192         // 4 - L2CAP packet length
193         bt_store_16(acl_buffer, 4,  len + 0);
194         // 6 - L2CAP channel DEST
195         bt_store_16(acl_buffer, 6, channel->remote_cid);
196         // 8 - data
197         memcpy(&acl_buffer[8], data, len);
198         // send
199         hci_send_acl_packet(acl_buffer, len+8);
200     }
201 }
202 
203 // open outgoing L2CAP channel
204 void l2cap_create_channel_internal(connection_t * connection, bd_addr_t address, uint16_t psm){
205 
206     // alloc structure
207     l2cap_channel_t * chan = malloc(sizeof(l2cap_channel_t));
208     // TODO: emit error event
209     if (!chan) return;
210 
211     // fill in
212     BD_ADDR_COPY(chan->address, address);
213     chan->psm = psm;
214     chan->handle = 0;
215     chan->connection = connection;
216     chan->packet_handler = NULL;
217     chan->remote_mtu = L2CAP_MINIMAL_MTU;
218 
219     // set initial state
220     chan->state = L2CAP_STATE_CLOSED;
221     chan->sig_id = L2CAP_SIG_ID_INVALID;
222 
223     // add to connections list
224     linked_list_add(&l2cap_channels, (linked_item_t *) chan);
225 
226     // send connection request
227     // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch
228     hci_send_cmd(&hci_create_connection, address, 0x18, 0, 0, 0, 0);
229 }
230 
231 void l2cap_disconnect_internal(uint16_t local_cid, uint8_t reason){
232     // find channel for local_cid
233     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
234     if (channel) {
235         channel->sig_id = l2cap_next_sig_id();
236         l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->sig_id, channel->remote_cid, channel->local_cid);
237         channel->state = L2CAP_STATE_WAIT_DISCONNECT;
238     }
239 }
240 
241 static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){
242     linked_item_t *it;
243     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
244         l2cap_channel_t * channel = (l2cap_channel_t *) it;
245         if ( ! BD_ADDR_CMP( channel->address, address) ){
246             if (channel->state == L2CAP_STATE_CLOSED) {
247                 // failure, forward error code
248                 l2cap_emit_channel_opened(channel, status);
249                 // discard channel
250                 linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
251                 free (channel);
252             }
253         }
254     }
255 }
256 
257 static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){
258     linked_item_t *it;
259     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
260         l2cap_channel_t * channel = (l2cap_channel_t *) it;
261         if ( ! BD_ADDR_CMP( channel->address, address) ){
262             if (channel->state == L2CAP_STATE_CLOSED) {
263                 // success, start l2cap handshake
264                 channel->handle = handle;
265                 channel->sig_id = l2cap_next_sig_id();
266                 channel->local_cid = l2cap_next_local_cid();
267                 channel->state = L2CAP_STATE_WAIT_CONNECT_RSP;
268                 l2cap_send_signaling_packet( channel->handle, CONNECTION_REQUEST, channel->sig_id, channel->psm, channel->local_cid);
269             }
270         }
271     }
272 }
273 
274 void l2cap_event_handler( uint8_t *packet, uint16_t size ){
275 
276     bd_addr_t address;
277     hci_con_handle_t handle;
278 
279     switch(packet[0]){
280 
281         // handle connection complete events
282         case HCI_EVENT_CONNECTION_COMPLETE:
283             bt_flip_addr(address, &packet[5]);
284             if (packet[2] == 0){
285                 handle = READ_BT_16(packet, 3);
286                 l2cap_handle_connection_success_for_addr(address, handle);
287             } else {
288                 l2cap_handle_connection_failed_for_addr(address, packet[2]);
289             }
290             break;
291 
292         // handle successful create connection cancel command
293         case HCI_EVENT_COMMAND_COMPLETE:
294             if ( COMMAND_COMPLETE_EVENT(packet, hci_create_connection_cancel) ) {
295                 if (packet[5] == 0){
296                     bt_flip_addr(address, &packet[6]);
297                     // CONNECTION TERMINATED BY LOCAL HOST (0X16)
298                     l2cap_handle_connection_failed_for_addr(address, 0x16);
299                 }
300             }
301             break;
302 
303         // handle disconnection complete events
304         case HCI_EVENT_DISCONNECTION_COMPLETE:
305             // send l2cap disconnect events for all channels on this handle
306             handle = READ_BT_16(packet, 3);
307             linked_item_t *it;
308             for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
309                 l2cap_channel_t * channel = (l2cap_channel_t *) it;
310                 if ( channel->handle == handle ){
311                     l2cap_finialize_channel_close(channel);
312                 }
313             }
314             break;
315 
316         // HCI Connection Timeouts
317         case L2CAP_EVENT_TIMEOUT_CHECK:
318             if (!capture_connection){
319                 hci_con_handle_t handle = READ_BT_16(packet, 2);
320                 linked_item_t *it;
321                 l2cap_channel_t * channel;
322                 int used = 0;
323                 for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
324                     channel = (l2cap_channel_t *) it;
325                     if (channel->handle == handle) {
326                         used = 1;
327                     }
328                 }
329                 if (!used) {
330                     hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection
331                 }
332             }
333             break;
334 
335         default:
336             break;
337     }
338 
339     // pass on
340     (*event_packet_handler)(NULL, packet, size);
341 }
342 
343 static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){
344     l2cap_send_signaling_packet( channel->handle, DISCONNECTION_RESPONSE, identifier, channel->local_cid, channel->remote_cid);
345     l2cap_finialize_channel_close(channel);
346 }
347 
348 static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){
349 
350     // printf("l2cap_handle_connection_request for handle %u, psm %u cid %u\n", handle, psm, source_cid);
351     l2cap_service_t *service = l2cap_get_service(psm);
352     if (!service) {
353         // 0x0002 PSM not supported
354         // printf("l2cap_handle_connection_request no PSM for psm %u/n", psm);
355         l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, 0x0002, 0);
356         return;
357     }
358 
359     hci_connection_t * hci_connection = connection_for_handle( handle );
360     if (!hci_connection) {
361         fprintf(stderr, "no hci_connection for handle %u\n", handle);
362         // TODO: emit error
363         return;
364     }
365     // alloc structure
366     // printf("l2cap_handle_connection_request register channel\n");
367     l2cap_channel_t * channel = malloc(sizeof(l2cap_channel_t));
368     // TODO: emit error event
369     if (!channel) return;
370 
371     // fill in
372     BD_ADDR_COPY(channel->address, hci_connection->address);
373     channel->psm = psm;
374     channel->handle = handle;
375     channel->connection = service->connection;
376     channel->packet_handler = service->packet_handler;
377     channel->local_cid  = l2cap_next_local_cid();
378     channel->remote_cid = source_cid;
379     channel->remote_mtu = L2CAP_MINIMAL_MTU;
380 
381     // set initial state
382     channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT;
383 
384     // temp. store req sig id
385     channel->sig_id = sig_id;
386 
387     // add to connections list
388     linked_list_add(&l2cap_channels, (linked_item_t *) channel);
389 
390     // emit incoming connection request
391     l2cap_emit_connection_request(channel);
392 }
393 
394 void l2cap_accept_connection_internal(uint16_t local_cid){
395     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
396     if (!channel) {
397         fprintf(stderr, "l2cap_accept_connection_internal called but local_cid 0x%x not found", local_cid);
398         return;
399     }
400 
401     // accept connection
402     l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->sig_id, channel->local_cid, channel->remote_cid, 0, 0);
403 
404     // set real sig and state and start config
405     channel->sig_id = l2cap_next_sig_id();
406     channel->state  = L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ;
407     l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->sig_id, channel->remote_cid, 0, 4, &config_options);
408 
409     // printf("new state %u\n", channel->state);
410 }
411 
412 void l2cap_decline_connection_internal(uint16_t local_cid, uint8_t reason){
413     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid);
414     if (!channel) {
415         fprintf(stderr, "l2cap_decline_connection_internal called but local_cid 0x%x not found", local_cid,reason);
416         return;
417     }
418     l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->sig_id, 0, 0, reason, 0);
419 
420     // discard channel
421     linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
422     free (channel);
423 }
424 
425 void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){
426     // accept the other's configuration options
427     uint16_t len       = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
428     uint16_t pos = 4;
429     while (pos < len){
430         uint8_t type   = command[pos++];
431         uint8_t length = command[pos++];
432         // MTU { type(8): 1, len(8):2, MTU(16) }
433         if ((type & 0x7f) == 1 && length == 2){
434             channel->remote_mtu = READ_BT_16(command, pos);
435         }
436         pos += length;
437     }
438     uint8_t identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
439     // send back received options
440     // l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, identifier, channel->remote_cid, 0, 0, len-4, &command[8]);
441     // send back OK
442     l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, identifier, channel->remote_cid, 0, 0, 0, NULL);
443 }
444 
445 void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){
446 
447     uint8_t code       = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
448     uint8_t identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
449     uint16_t result = 0;
450 
451     // printf("signaling handler code %u\n", code);
452 
453     switch (channel->state) {
454 
455         case L2CAP_STATE_WAIT_CONNECT_RSP:
456             switch (code){
457                 case CONNECTION_RESPONSE:
458                     result = READ_BT_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
459                     switch (result) {
460                         case 0:
461                             // successful connection
462                             channel->remote_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
463                             channel->sig_id = l2cap_next_sig_id();
464                             l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->sig_id, channel->remote_cid, 0, 4, &config_options);
465                             channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ;
466                             break;
467                         case 1:
468                             // connection pending. get some coffee
469                             break;
470                         default:
471                             // map l2cap connection response result to BTstack status enumeration
472                             l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result);
473                             break;
474                     }
475                     break;
476 
477                 case DISCONNECTION_REQUEST:
478                     l2cap_handle_disconnect_request(channel, identifier);
479                     break;
480 
481                 default:
482                     //@TODO: implement other signaling packets
483                     break;
484             }
485             break;
486 
487         case L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ:
488             switch (code) {
489                 case CONFIGURE_RESPONSE:
490                     channel->state = L2CAP_STATE_WAIT_CONFIG_REQ;
491                     break;
492                 case CONFIGURE_REQUEST:
493                     l2cap_signaling_handle_configure_request(channel, command);
494                     channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP;
495                     break;
496                 case DISCONNECTION_REQUEST:
497                     l2cap_handle_disconnect_request(channel, identifier);
498                     break;
499                 default:
500                     //@TODO: implement other signaling packets
501                     break;
502             }
503             break;
504 
505         case L2CAP_STATE_WAIT_CONFIG_REQ:
506             switch (code) {
507                 case CONFIGURE_REQUEST:
508                     l2cap_signaling_handle_configure_request(channel, command);
509                     channel->state = L2CAP_STATE_OPEN;
510                     l2cap_emit_channel_opened(channel, 0);  // success
511                     break;
512                 case DISCONNECTION_REQUEST:
513                     l2cap_handle_disconnect_request(channel, identifier);
514                     break;
515                 default:
516                     //@TODO: implement other signaling packets
517                     break;
518             }
519             break;
520 
521         case L2CAP_STATE_WAIT_CONFIG_REQ_RSP:
522             switch (code) {
523                 case CONFIGURE_RESPONSE:
524                     channel->state = L2CAP_STATE_OPEN;
525                     l2cap_emit_channel_opened(channel, 0);  // success
526                     break;
527                 case DISCONNECTION_REQUEST:
528                     l2cap_handle_disconnect_request(channel, identifier);
529                     break;
530                 default:
531                     //@TODO: implement other signaling packets
532                     break;
533             }
534             break;
535 
536         case L2CAP_STATE_WAIT_DISCONNECT:
537             switch (code) {
538                 case DISCONNECTION_RESPONSE:
539                     l2cap_finialize_channel_close(channel);
540                     break;
541                 case DISCONNECTION_REQUEST:
542                     l2cap_handle_disconnect_request(channel, identifier);
543                     break;
544                 default:
545                     //@TODO: implement other signaling packets
546                     break;
547             }
548             break;
549 
550         case L2CAP_STATE_CLOSED:
551             // @TODO handle incoming requests
552             break;
553 
554         case L2CAP_STATE_OPEN:
555             switch (code) {
556                 case DISCONNECTION_REQUEST:
557                     l2cap_handle_disconnect_request(channel, identifier);
558                     break;
559                 default:
560                     //@TODO: implement other signaling packets, e.g. re-configure
561                     break;
562             }
563             break;
564     }
565     // printf("new state %u\n", channel->state);
566 }
567 
568 
569 void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){
570 
571     // get code, signalind identifier and command len
572     uint8_t code   = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
573     uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
574     uint16_t len   = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
575 
576     // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST
577     if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){
578         return;
579     }
580 
581     // general commands without an assigned channel
582     switch(code) {
583 
584         case CONNECTION_REQUEST: {
585             uint16_t psm =        READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
586             uint16_t source_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2);
587             l2cap_handle_connection_request(handle, sig_id, psm, source_cid);
588             break;
589         }
590 
591         case ECHO_REQUEST: {
592             // send back packet
593             l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, len, &command[L2CAP_SIGNALING_COMMAND_DATA_OFFSET]);
594             break;
595         }
596 
597         case INFORMATION_REQUEST: {
598             // we neither support connectionless L2CAP data nor support any flow control modes yet
599             uint16_t infoType = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
600             if (infoType == 2) {
601                 uint32_t features = 0;
602                 // extended features request supported, however no features present
603                 l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, 4, &features);
604             } else {
605                 // all other types are not supported
606                 l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL);
607             }
608             break;;
609         }
610 
611         default:
612             break;
613     }
614 
615 
616     // Get potential destination CID
617     uint16_t dest_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
618 
619     // Find channel for this sig_id and connection handle
620     linked_item_t *it;
621     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
622         l2cap_channel_t * channel = (l2cap_channel_t *) it;
623         if (channel->handle == handle) {
624             if (code & 1) {
625                 // match odd commands by previous signaling identifier
626                 if (channel->sig_id == sig_id) {
627                     l2cap_signaling_handler_channel(channel, command);
628                 }
629             } else {
630                 // match even commands by local channel id
631                 if (channel->local_cid == dest_cid) {
632                     l2cap_signaling_handler_channel(channel, command);
633                 }
634             }
635         }
636     }
637 }
638 
639 void l2cap_acl_handler( uint8_t *packet, uint16_t size ){
640 
641     // Capturing?
642     if (capture_connection) {
643         (*data_packet_handler)(capture_connection, 0, packet, size);
644         return;
645     }
646 
647     // Get Channel ID
648     uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet);
649 
650     // Signaling Packet?
651     if (channel_id == 1) {
652 
653         // Get Connection
654         hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet);
655 
656         uint16_t command_offset = 8;
657         while (command_offset < size) {
658 
659             // handle signaling commands
660             l2cap_signaling_handler_dispatch(handle, &packet[command_offset]);
661 
662             // increment command_offset
663             command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + READ_BT_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
664         }
665         return;
666     }
667 
668     // Find channel for this channel_id and connection handle
669     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(channel_id);
670     if (channel) {
671         l2cap_dispatch(channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
672     }
673 }
674 
675 
676 // finalize closed channel
677 void l2cap_finialize_channel_close(l2cap_channel_t *channel){
678     channel->state = L2CAP_STATE_CLOSED;
679     l2cap_emit_channel_closed(channel);
680 
681     // discard channel
682     linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
683     free (channel);
684 }
685 
686 l2cap_service_t * l2cap_get_service(uint16_t psm){
687     linked_item_t *it;
688 
689     // close open channels
690     for (it = (linked_item_t *) l2cap_services; it ; it = it->next){
691         l2cap_service_t * service = ((l2cap_service_t *) it);
692         if ( service->psm == psm){
693             return service;
694         };
695     }
696     return NULL;
697 }
698 
699 void l2cap_register_service_internal(connection_t *connection, btstack_packet_handler_t packet_handler, uint16_t psm, uint16_t mtu){
700     // check for alread registered psm // TODO: emit error event
701     l2cap_service_t *service = l2cap_get_service(psm);
702     if (service) return;
703 
704     // alloc structure     // TODO: emit error event
705     service = malloc(sizeof(l2cap_service_t));
706     if (!service) return;
707 
708     // fill in
709     service->psm = psm;
710     service->mtu = mtu;
711     service->connection = connection;
712     service->packet_handler = packet_handler;
713 
714     // add to services list
715     linked_list_add(&l2cap_services, (linked_item_t *) service);
716 }
717 
718 void l2cap_unregister_service_internal(connection_t *connection, uint16_t psm){
719     l2cap_service_t *service = l2cap_get_service(psm);
720     if (!service) return;
721     linked_list_remove(&l2cap_services, (linked_item_t *) service);
722     free(service);
723 }
724 
725 //
726 void l2cap_close_connection(connection_t *connection){
727     linked_item_t *it;
728 
729     // close open channels
730     l2cap_channel_t * channel;
731     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
732         channel = (l2cap_channel_t *) it;
733         if (channel->connection == connection) {
734             channel->sig_id = l2cap_next_sig_id();
735             l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->sig_id, channel->remote_cid, channel->local_cid);
736             channel->state = L2CAP_STATE_WAIT_DISCONNECT;
737         }
738     }
739 
740     // unregister services
741     it = (linked_item_t *) &l2cap_services;
742     while (it->next){
743         l2cap_service_t * service = (l2cap_service_t *) it->next;
744         if (service->connection == connection){
745             it->next = it->next->next;
746             free(service);
747         } else {
748             it = it->next;
749         }
750     }
751 }
752 
753 
754 void l2cap_set_capture_connection(connection_t * connection){
755     capture_connection = connection;
756 }
757 
758