xref: /btstack/src/l2cap.c (revision 2289ec97a0546e0f840f830dcfd663d876d304e9)
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 
43 #include <stdarg.h>
44 #include <string.h>
45 
46 #include <stdio.h>
47 
48 // size of HCI ACL + L2CAP Header for regular data packets
49 #define COMPLETE_L2CAP_HEADER 8
50 
51 static void null_event_handler(uint8_t *packet, uint16_t size);
52 static void null_data_handler(uint16_t source_cid, uint8_t *packet, uint16_t size);
53 
54 static uint8_t * sig_buffer = NULL;
55 static linked_list_t l2cap_channels = NULL;
56 static linked_list_t l2cap_services = NULL;
57 static uint8_t * acl_buffer = NULL;
58 static void (*event_packet_handler) (uint8_t *packet, uint16_t size) = null_event_handler;
59 static void (*data_packet_handler)  (uint16_t source_cid, uint8_t *packet, uint16_t size) = null_data_handler;
60 static connection_t * capture_connection = NULL;
61 
62 static uint8_t config_options[] = { 1, 2, 150, 0}; // mtu = 48
63 
64 void l2cap_init(){
65     sig_buffer = malloc( 48 );
66     acl_buffer = malloc( 255 + 8 );
67 
68     //
69     // register callbacks with HCI
70     //
71     hci_register_event_packet_handler(&l2cap_event_handler);
72     hci_register_acl_packet_handler(&l2cap_acl_handler);
73 }
74 
75 
76 /** Register L2CAP packet handlers */
77 static void null_event_handler(uint8_t *packet, uint16_t size){
78 }
79 static void null_data_handler(uint16_t  source_cid, uint8_t *packet, uint16_t size){
80 }
81 void l2cap_register_event_packet_handler(void (*handler)(uint8_t *packet, uint16_t size)){
82     event_packet_handler = handler;
83 }
84 void l2cap_register_data_packet_handler  (void (*handler)(uint16_t source_cid, uint8_t *packet, uint16_t size)){
85     data_packet_handler = handler;
86 }
87 
88 int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){
89     va_list argptr;
90     va_start(argptr, identifier);
91     uint16_t len = l2cap_create_signaling_internal(sig_buffer, handle, cmd, identifier, argptr);
92     va_end(argptr);
93     return hci_send_acl_packet(sig_buffer, len);
94 }
95 
96 l2cap_channel_t * l2cap_get_channel_for_source_cid(uint16_t source_cid){
97     linked_item_t *it;
98     l2cap_channel_t * channel;
99     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
100         channel = (l2cap_channel_t *) it;
101         if ( channel->source_cid == source_cid) {
102             return channel;
103         }
104     }
105     return NULL;
106 }
107 
108 // open outgoing L2CAP channel
109 void l2cap_create_channel_internal(connection_t * connection, bd_addr_t address, uint16_t psm){
110 
111     // alloc structure
112     l2cap_channel_t * chan = malloc(sizeof(l2cap_channel_t));
113     // TODO: emit error event
114     if (!chan) return;
115 
116     // fill in
117     BD_ADDR_COPY(chan->address, address);
118     chan->psm = psm;
119     chan->handle = 0;
120     chan->connection = connection;
121 
122     // set initial state
123     chan->state = L2CAP_STATE_CLOSED;
124     chan->sig_id = L2CAP_SIG_ID_INVALID;
125 
126     // add to connections list
127     linked_list_add(&l2cap_channels, (linked_item_t *) chan);
128 
129     // send connection request
130     // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch
131     hci_send_cmd(&hci_create_connection, address, 0x18, 0, 0, 0, 0);
132 }
133 
134 void l2cap_disconnect_internal(uint16_t source_cid, uint8_t reason){
135     // find channel for source_cid
136     l2cap_channel_t * channel = l2cap_get_channel_for_source_cid(source_cid);
137     if (channel) {
138         channel->sig_id = l2cap_next_sig_id();
139         l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->sig_id, channel->dest_cid, channel->source_cid);
140         channel->state = L2CAP_STATE_WAIT_DISCONNECT;
141     }
142 }
143 
144 static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){
145     linked_item_t *it;
146     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
147         l2cap_channel_t * channel = (l2cap_channel_t *) it;
148         if ( ! BD_ADDR_CMP( channel->address, address) ){
149             if (channel->state == L2CAP_STATE_CLOSED) {
150                 // failure, forward error code
151                 l2cap_emit_channel_opened(channel, status);
152                 // discard channel
153                 linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
154                 free (channel);
155             }
156         }
157     }
158 }
159 
160 static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){
161     linked_item_t *it;
162     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
163         l2cap_channel_t * channel = (l2cap_channel_t *) it;
164         if ( ! BD_ADDR_CMP( channel->address, address) ){
165             if (channel->state == L2CAP_STATE_CLOSED) {
166                 // success, start l2cap handshake
167                 channel->handle = handle;
168                 channel->sig_id = l2cap_next_sig_id();
169                 channel->source_cid = l2cap_next_source_cid();
170                 channel->state = L2CAP_STATE_WAIT_CONNECT_RSP;
171                 l2cap_send_signaling_packet( channel->handle, CONNECTION_REQUEST, channel->sig_id, channel->psm, channel->source_cid);
172             }
173         }
174     }
175 }
176 
177 void l2cap_event_handler( uint8_t *packet, uint16_t size ){
178 
179     bd_addr_t address;
180     hci_con_handle_t handle;
181 
182     switch(packet[0]){
183 
184         // handle connection complete events
185         case HCI_EVENT_CONNECTION_COMPLETE:
186             bt_flip_addr(address, &packet[5]);
187             if (packet[2] == 0){
188                 handle = READ_BT_16(packet, 3);
189                 l2cap_handle_connection_success_for_addr(address, handle);
190             } else {
191                 l2cap_handle_connection_failed_for_addr(address, packet[2]);
192             }
193             break;
194 
195         // handle successful create connection cancel command
196         case HCI_EVENT_COMMAND_COMPLETE:
197             if ( COMMAND_COMPLETE_EVENT(packet, hci_create_connection_cancel) ) {
198                 if (packet[5] == 0){
199                     bt_flip_addr(address, &packet[6]);
200                     // CONNECTION TERMINATED BY LOCAL HOST (0X16)
201                     l2cap_handle_connection_failed_for_addr(address, 0x16);
202                 }
203             }
204             break;
205 
206         // handle disconnection complete events
207         case HCI_EVENT_DISCONNECTION_COMPLETE:
208             // send l2cap disconnect events for all channels on this handle
209             handle = READ_BT_16(packet, 3);
210             linked_item_t *it;
211             for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
212                 l2cap_channel_t * channel = (l2cap_channel_t *) it;
213                 if ( channel->handle == handle ){
214                     l2cap_finialize_channel_close(channel);
215                 }
216             }
217             break;
218 
219         // HCI Connection Timeouts
220         case L2CAP_EVENT_TIMEOUT_CHECK:
221             if (!capture_connection){
222                 hci_con_handle_t handle = READ_BT_16(packet, 2);
223                 linked_item_t *it;
224                 l2cap_channel_t * channel;
225                 int used = 0;
226                 for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
227                     channel = (l2cap_channel_t *) it;
228                     if (channel->handle == handle) {
229                         used = 1;
230                     }
231                 }
232                 if (!used) {
233                     hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection
234                 }
235             }
236             break;
237 
238         default:
239             break;
240     }
241 
242     // pass on
243     (*event_packet_handler)(packet, size);
244 }
245 
246 static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){
247     l2cap_send_signaling_packet( channel->handle, DISCONNECTION_RESPONSE, identifier, channel->dest_cid, channel->source_cid);
248     l2cap_finialize_channel_close(channel);
249 }
250 
251 static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t dest_cid){
252 
253     // printf("l2cap_handle_connection_request for handle %u, psm %u cid %u\n", handle, psm, dest_cid);
254     l2cap_service_t *service = l2cap_get_service(psm);
255     if (!service) {
256         // 0x0002 PSM not supported
257         // printf("l2cap_handle_connection_request no PSM for psm %u/n", psm);
258         l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, 0x0002, 0);
259         return;
260     }
261 
262     hci_connection_t * hci_connection = connection_for_handle( handle );
263     if (!hci_connection) {
264         fprintf(stderr, "no hci_connection for handle %u\n", handle);
265         // TODO: emit error
266         return;
267     }
268     // alloc structure
269     // printf("l2cap_handle_connection_request register channel\n");
270     l2cap_channel_t * channel = malloc(sizeof(l2cap_channel_t));
271     // TODO: emit error event
272     if (!channel) return;
273 
274     // fill in
275     BD_ADDR_COPY(channel->address, hci_connection->address);
276     channel->psm = psm;
277     channel->handle = handle;
278     channel->connection = service->connection;
279     channel->source_cid = l2cap_next_source_cid();
280     channel->dest_cid   = dest_cid;
281 
282     // set initial state
283     channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT;
284 
285     // temp. store req sig id
286     channel->sig_id = sig_id;
287 
288     // add to connections list
289     linked_list_add(&l2cap_channels, (linked_item_t *) channel);
290 
291     // emit incoming connection request
292     l2cap_emit_connection_request(channel);
293 }
294 
295 void l2cap_accept_connection_internal(uint16_t source_cid){
296     l2cap_channel_t * channel = l2cap_get_channel_for_source_cid(source_cid);
297     if (!channel) {
298         fprintf(stderr, "l2cap_accept_connection_internal called but source_cid 0x%x not found", source_cid);
299         return;
300     }
301 
302     // accept connection
303     l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->sig_id, channel->dest_cid, channel->source_cid, 0, 0);
304 
305     // set real sig and state and start config
306     channel->sig_id = l2cap_next_sig_id();
307     channel->state  = L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ;
308     l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->sig_id, channel->dest_cid, 0, 4, &config_options);
309 }
310 
311 void l2cap_decline_connection_internal(uint16_t source_cid, uint8_t reason){
312     l2cap_channel_t * channel = l2cap_get_channel_for_source_cid( source_cid);
313     if (!channel) {
314         fprintf(stderr, "l2cap_decline_connection_internal called but source_cid 0x%x not found", source_cid,reason);
315         return;
316     }
317     l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->sig_id, 0, 0, reason, 0);
318 
319     // discard channel
320     linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
321     free (channel);
322 }
323 
324 void l2cap_signaling_handler(l2cap_channel_t *channel, uint8_t *packet, uint16_t size){
325 
326     uint8_t  code       = READ_L2CAP_SIGNALING_CODE( packet );
327     uint8_t  identifier = READ_L2CAP_SIGNALING_IDENTIFIER( packet );
328     uint16_t result = 0;
329 
330     switch (channel->state) {
331 
332         case L2CAP_STATE_WAIT_CONNECT_RSP:
333             switch (code){
334                 case CONNECTION_RESPONSE:
335                     result = READ_BT_16 (packet, L2CAP_SIGNALING_DATA_OFFSET+4);
336                     switch (result) {
337                         case 0:
338                             // successful connection
339                             channel->dest_cid = READ_BT_16(packet, L2CAP_SIGNALING_DATA_OFFSET);
340                             channel->sig_id = l2cap_next_sig_id();
341                             l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->sig_id, channel->dest_cid, 0, 4, &config_options);
342                             channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ;
343                             break;
344                         case 1:
345                             // connection pending. get some coffee
346                             break;
347                         default:
348                             // map l2cap connection response result to BTstack status enumeration
349                             l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result);
350                             break;
351                     }
352                     break;
353 
354                 case DISCONNECTION_REQUEST:
355                     l2cap_handle_disconnect_request(channel, identifier);
356                     break;
357 
358                 default:
359                     //@TODO: implement other signaling packets
360                     break;
361             }
362             break;
363 
364         case L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ:
365             switch (code) {
366                 case CONFIGURE_RESPONSE:
367                     channel->state = L2CAP_STATE_WAIT_CONFIG_REQ;
368                     break;
369                 case CONFIGURE_REQUEST:
370                     // accept the other's configuration options
371                     l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, identifier, channel->dest_cid, 0, 0, size - 16, &packet[16]);
372                     channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP;
373                     break;
374                 case DISCONNECTION_REQUEST:
375                     l2cap_handle_disconnect_request(channel, identifier);
376                     break;
377                 default:
378                     //@TODO: implement other signaling packets
379                     break;
380             }
381             break;
382 
383         case L2CAP_STATE_WAIT_CONFIG_REQ:
384             switch (code) {
385                 case CONFIGURE_REQUEST:
386                     // accept the other's configuration options
387                     l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, identifier, channel->dest_cid, 0, 0, size - 16, &packet[16]);
388                     channel->state = L2CAP_STATE_OPEN;
389                     l2cap_emit_channel_opened(channel, 0);  // success
390                     break;
391                 case DISCONNECTION_REQUEST:
392                     l2cap_handle_disconnect_request(channel, identifier);
393                     break;
394                 default:
395                     //@TODO: implement other signaling packets
396                     break;
397             }
398             break;
399 
400         case L2CAP_STATE_WAIT_CONFIG_REQ_RSP:
401             switch (code) {
402                 case CONFIGURE_RESPONSE:
403                     channel->state = L2CAP_STATE_OPEN;
404                     l2cap_emit_channel_opened(channel, 0);  // success
405                     break;
406                 case DISCONNECTION_REQUEST:
407                     l2cap_handle_disconnect_request(channel, identifier);
408                     break;
409                 default:
410                     //@TODO: implement other signaling packets
411                     break;
412             }
413             break;
414 
415         case L2CAP_STATE_WAIT_DISCONNECT:
416             switch (code) {
417                 case DISCONNECTION_RESPONSE:
418                     l2cap_finialize_channel_close(channel);
419                     break;
420                 case DISCONNECTION_REQUEST:
421                     l2cap_handle_disconnect_request(channel, identifier);
422                     break;
423                 default:
424                     //@TODO: implement other signaling packets
425                     break;
426             }
427             break;
428 
429         case L2CAP_STATE_CLOSED:
430             // @TODO handle incoming requests
431             break;
432 
433         case L2CAP_STATE_OPEN:
434             switch (code) {
435                 case DISCONNECTION_REQUEST:
436                     l2cap_handle_disconnect_request(channel, identifier);
437                     break;
438                 default:
439                     //@TODO: implement other signaling packets, e.g. re-configure
440                     break;
441             }
442             break;
443     }
444 }
445 
446 // finalize closed channel
447 void l2cap_finialize_channel_close(l2cap_channel_t *channel){
448     channel->state = L2CAP_STATE_CLOSED;
449     l2cap_emit_channel_closed(channel);
450 
451     // discard channel
452     linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
453     free (channel);
454 }
455 
456 l2cap_service_t * l2cap_get_service(uint16_t psm){
457     linked_item_t *it;
458 
459     // close open channels
460     for (it = (linked_item_t *) l2cap_services; it ; it = it->next){
461         l2cap_service_t * service = ((l2cap_service_t *) it);
462         if ( service->psm == psm){
463             return service;
464         };
465     }
466     return NULL;
467 }
468 
469 void l2cap_register_service_internal(connection_t *connection, uint16_t psm, uint16_t mtu){
470     // check for alread registered psm // TODO: emit error event
471     l2cap_service_t *service = l2cap_get_service(psm);
472     if (service) return;
473 
474     // alloc structure     // TODO: emit error event
475     service = malloc(sizeof(l2cap_service_t));
476     if (!service) return;
477 
478     // fill in
479     service->psm = psm;
480     service->mtu = mtu;
481     service->connection = connection;
482 
483     // add to services list
484     linked_list_add(&l2cap_services, (linked_item_t *) service);
485 }
486 
487 void l2cap_unregister_service_internal(connection_t *connection, uint16_t psm){
488     l2cap_service_t *service = l2cap_get_service(psm);
489     if (service) return;
490     linked_list_remove(&l2cap_services, (linked_item_t *) service);
491     free( service );
492 }
493 
494 //
495 void l2cap_close_connection(connection_t *connection){
496     linked_item_t *it;
497 
498     // close open channels
499     l2cap_channel_t * channel;
500     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
501         channel = (l2cap_channel_t *) it;
502         if (channel->connection == connection) {
503             channel->sig_id = l2cap_next_sig_id();
504             l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->sig_id, channel->dest_cid, channel->source_cid);
505             channel->state = L2CAP_STATE_WAIT_DISCONNECT;
506         }
507     }
508 
509     // unregister services
510     l2cap_service_t *service;
511     for (it = (linked_item_t *) &l2cap_services; it ; it = it->next){
512         channel = (l2cap_channel_t *) it->next;
513         if (service->connection == connection){
514             it->next = it->next->next;
515             return;
516         }
517     }
518 }
519 
520 //  notify client
521 void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) {
522     uint8_t event[17];
523     event[0] = L2CAP_EVENT_CHANNEL_OPENED;
524     event[1] = sizeof(event) - 2;
525     event[2] = status;
526     bt_flip_addr(&event[3], channel->address);
527     bt_store_16(event,  9, channel->handle);
528     bt_store_16(event, 11, channel->psm);
529     bt_store_16(event, 13, channel->source_cid);
530     bt_store_16(event, 15, channel->dest_cid);
531     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
532     socket_connection_send_packet(channel->connection, HCI_EVENT_PACKET, 0, event, sizeof(event));
533 }
534 
535 void l2cap_emit_channel_closed(l2cap_channel_t *channel) {
536     uint8_t event[4];
537     event[0] = L2CAP_EVENT_CHANNEL_CLOSED;
538     event[1] = sizeof(event) - 2;
539     bt_store_16(event, 2, channel->source_cid);
540     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
541     socket_connection_send_packet(channel->connection, HCI_EVENT_PACKET, 0, event, sizeof(event));
542 }
543 
544 void l2cap_emit_connection_request(l2cap_channel_t *channel) {
545     uint8_t event[16];
546     event[0] = L2CAP_EVENT_INCOMING_CONNECTION;
547     event[1] = sizeof(event) - 2;
548     bt_flip_addr(&event[2], channel->address);
549     bt_store_16(event,  8, channel->handle);
550     bt_store_16(event, 10, channel->psm);
551     bt_store_16(event, 12, channel->source_cid);
552     bt_store_16(event, 14, channel->dest_cid);
553     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
554     socket_connection_send_packet(channel->connection, HCI_EVENT_PACKET, 0, event, sizeof(event));
555 }
556 
557 void l2cap_acl_handler( uint8_t *packet, uint16_t size ){
558 
559     // Capturing?
560     if (capture_connection) {
561         socket_connection_send_packet(capture_connection, HCI_ACL_DATA_PACKET, 0, packet, size);
562         return;
563     }
564 
565     // forward to higher layers - not needed yet
566     // (*data_packet_handler)(channel_id, packet, size);
567 
568     // Get Channel ID and command code
569     uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet);
570     uint8_t  code       = READ_L2CAP_SIGNALING_CODE( packet );
571 
572     // Get Connection
573     hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet);
574 
575     // printf("l2cap_acl_handler channel %u, code %u\n", channel_id, code);
576 
577     // Signaling Packet?
578     if (channel_id == 1) {
579 
580         if (code < 1 || code >= 8){
581             // not for a particular channel, and not CONNECTION_REQUEST
582             return;
583         }
584 
585         // Get Signaling Identifier
586         uint8_t sig_id    = READ_L2CAP_SIGNALING_IDENTIFIER(packet);
587 
588         // CONNECTION_REQUEST
589         if (code == CONNECTION_REQUEST){
590             uint16_t psm =      READ_BT_16(packet, L2CAP_SIGNALING_DATA_OFFSET);
591             uint16_t dest_cid = READ_BT_16(packet, L2CAP_SIGNALING_DATA_OFFSET+2);
592             l2cap_handle_connection_request(handle, sig_id, psm, dest_cid);
593             return;
594         }
595 
596         // Get potential destination CID
597         uint16_t dest_cid = READ_BT_16(packet, L2CAP_SIGNALING_DATA_OFFSET);
598 
599         // Find channel for this sig_id and connection handle
600         linked_item_t *it;
601         for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
602             l2cap_channel_t * channel = (l2cap_channel_t *) it;
603             if (channel->handle == handle) {
604                 if (code & 1) {
605                     // match odd commands by previous signaling identifier
606                     if (channel->sig_id == sig_id) {
607                         l2cap_signaling_handler( channel, packet, size);
608                     }
609                 } else {
610                     // match even commands by source channel id
611                     if (channel->source_cid == dest_cid) {
612                         l2cap_signaling_handler( channel, packet, size);
613                     }
614                 }
615             }
616         }
617         return;
618     }
619 
620     // Find channel for this channel_id and connection handle
621     l2cap_channel_t * channel = l2cap_get_channel_for_source_cid(channel_id);
622     if (channel) {
623         socket_connection_send_packet(channel->connection, L2CAP_DATA_PACKET, channel_id,
624                                       &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
625     }
626 }
627 
628 
629 void l2cap_send_internal(uint16_t source_cid, uint8_t *data, uint16_t len){
630     // find channel for source_cid, construct l2cap packet and send
631     l2cap_channel_t * channel = l2cap_get_channel_for_source_cid(source_cid);
632     if (channel) {
633          // 0 - Connection handle : PB=10 : BC=00
634          bt_store_16(acl_buffer, 0, channel->handle | (2 << 12) | (0 << 14));
635          // 2 - ACL length
636          bt_store_16(acl_buffer, 2,  len + 4);
637          // 4 - L2CAP packet length
638          bt_store_16(acl_buffer, 4,  len + 0);
639          // 6 - L2CAP channel DEST
640          bt_store_16(acl_buffer, 6, channel->dest_cid);
641          // 8 - data
642          memcpy(&acl_buffer[8], data, len);
643          // send
644          hci_send_acl_packet(acl_buffer, len+8);
645      }
646 }
647 
648 void l2cap_set_capture_connection(connection_t * connection){
649     capture_connection = connection;
650 }
651 
652