xref: /btstack/src/l2cap.c (revision 169f8b289d38a277601a09d9fe9698c85a297ac1)
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 
269     // alloc structure
270     // printf("l2cap_handle_connection_request register channel\n");
271     l2cap_channel_t * channel = malloc(sizeof(l2cap_channel_t));
272     // TODO: emit error event
273     if (!channel) return;
274 
275     // fill in
276     BD_ADDR_COPY(channel->address, hci_connection->address);
277     channel->psm = psm;
278     channel->handle = handle;
279     channel->connection = service->connection;
280     channel->source_cid = l2cap_next_source_cid();
281     channel->dest_cid   = dest_cid;
282 
283     // set initial state
284     channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ;
285     channel->sig_id = l2cap_next_sig_id();
286 
287     // add to connections list
288     linked_list_add(&l2cap_channels, (linked_item_t *) channel);
289 
290     // TODO: emit incoming connection request instead of answering directly
291 
292     l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, dest_cid, channel->source_cid, 0, 0);
293     l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->sig_id, channel->dest_cid, 0, 4, &config_options);
294 
295 }
296 
297 void l2cap_signaling_handler(l2cap_channel_t *channel, uint8_t *packet, uint16_t size){
298 
299     uint8_t  code       = READ_L2CAP_SIGNALING_CODE( packet );
300     uint8_t  identifier = READ_L2CAP_SIGNALING_IDENTIFIER( packet );
301     uint16_t result = 0;
302 
303     switch (channel->state) {
304 
305         case L2CAP_STATE_WAIT_CONNECT_RSP:
306             switch (code){
307                 case CONNECTION_RESPONSE:
308                     result = READ_BT_16 (packet, L2CAP_SIGNALING_DATA_OFFSET+4);
309                     switch (result) {
310                         case 0:
311                             // successful connection
312                             channel->dest_cid = READ_BT_16(packet, L2CAP_SIGNALING_DATA_OFFSET);
313                             channel->sig_id = l2cap_next_sig_id();
314                             l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->sig_id, channel->dest_cid, 0, 4, &config_options);
315                             channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ;
316                             break;
317                         case 1:
318                             // connection pending. get some coffee
319                             break;
320                         default:
321                             // map l2cap connection response result to BTstack status enumeration
322                             l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result);
323                             break;
324                     }
325                     break;
326 
327                 case DISCONNECTION_REQUEST:
328                     l2cap_handle_disconnect_request(channel, identifier);
329                     break;
330 
331                 default:
332                     //@TODO: implement other signaling packets
333                     break;
334             }
335             break;
336 
337         case L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ:
338             switch (code) {
339                 case CONFIGURE_RESPONSE:
340                     channel->state = L2CAP_STATE_WAIT_CONFIG_REQ;
341                     break;
342                 case CONFIGURE_REQUEST:
343                     // accept the other's configuration options
344                     l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, identifier, channel->dest_cid, 0, 0, size - 16, &packet[16]);
345                     channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP;
346                     break;
347                 case DISCONNECTION_REQUEST:
348                     l2cap_handle_disconnect_request(channel, identifier);
349                     break;
350                 default:
351                     //@TODO: implement other signaling packets
352                     break;
353             }
354             break;
355 
356         case L2CAP_STATE_WAIT_CONFIG_REQ:
357             switch (code) {
358                 case CONFIGURE_REQUEST:
359                     // accept the other's configuration options
360                     l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, identifier, channel->dest_cid, 0, 0, size - 16, &packet[16]);
361                     channel->state = L2CAP_STATE_OPEN;
362                     l2cap_emit_channel_opened(channel, 0);  // success
363                     break;
364                 case DISCONNECTION_REQUEST:
365                     l2cap_handle_disconnect_request(channel, identifier);
366                     break;
367                 default:
368                     //@TODO: implement other signaling packets
369                     break;
370             }
371             break;
372 
373         case L2CAP_STATE_WAIT_CONFIG_REQ_RSP:
374             switch (code) {
375                 case CONFIGURE_RESPONSE:
376                     channel->state = L2CAP_STATE_OPEN;
377                     l2cap_emit_channel_opened(channel, 0);  // success
378                     break;
379                 case DISCONNECTION_REQUEST:
380                     l2cap_handle_disconnect_request(channel, identifier);
381                     break;
382                 default:
383                     //@TODO: implement other signaling packets
384                     break;
385             }
386             break;
387 
388         case L2CAP_STATE_WAIT_DISCONNECT:
389             switch (code) {
390                 case DISCONNECTION_RESPONSE:
391                     l2cap_finialize_channel_close(channel);
392                     break;
393                 case DISCONNECTION_REQUEST:
394                     l2cap_handle_disconnect_request(channel, identifier);
395                     break;
396                 default:
397                     //@TODO: implement other signaling packets
398                     break;
399             }
400             break;
401 
402         case L2CAP_STATE_CLOSED:
403             // @TODO handle incoming requests
404             break;
405 
406         case L2CAP_STATE_OPEN:
407             switch (code) {
408                 case DISCONNECTION_REQUEST:
409                     l2cap_handle_disconnect_request(channel, identifier);
410                     break;
411                 default:
412                     //@TODO: implement other signaling packets, e.g. re-configure
413                     break;
414             }
415             break;
416     }
417 }
418 
419 // finalize closed channel
420 void l2cap_finialize_channel_close(l2cap_channel_t *channel){
421     channel->state = L2CAP_STATE_CLOSED;
422     l2cap_emit_channel_closed(channel);
423 
424     // discard channel
425     linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
426     free (channel);
427 }
428 
429 l2cap_service_t * l2cap_get_service(uint16_t psm){
430     linked_item_t *it;
431 
432     // close open channels
433     for (it = (linked_item_t *) l2cap_services; it ; it = it->next){
434         l2cap_service_t * service = ((l2cap_service_t *) it);
435         if ( service->psm == psm){
436             return service;
437         };
438     }
439     return NULL;
440 }
441 
442 void l2cap_register_service_internal(connection_t *connection, uint16_t psm, uint16_t mtu){
443     // check for alread registered psm // TODO: emit error event
444     l2cap_service_t *service = l2cap_get_service(psm);
445     if (service) return;
446 
447     // alloc structure     // TODO: emit error event
448     service = malloc(sizeof(l2cap_service_t));
449     if (!service) return;
450 
451     // fill in
452     service->psm = psm;
453     service->mtu = mtu;
454     service->connection = connection;
455 
456     // add to services list
457     linked_list_add(&l2cap_services, (linked_item_t *) service);
458 }
459 
460 void l2cap_unregister_service_internal(connection_t *connection, uint16_t psm){
461     l2cap_service_t *service = l2cap_get_service(psm);
462     if (service) return;
463     linked_list_remove(&l2cap_services, (linked_item_t *) service);
464     free( service );
465 }
466 
467 //
468 void l2cap_close_connection(connection_t *connection){
469     linked_item_t *it;
470 
471     // close open channels
472     l2cap_channel_t * channel;
473     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
474         channel = (l2cap_channel_t *) it;
475         if (channel->connection == connection) {
476             channel->sig_id = l2cap_next_sig_id();
477             l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->sig_id, channel->dest_cid, channel->source_cid);
478             channel->state = L2CAP_STATE_WAIT_DISCONNECT;
479         }
480     }
481 
482     // unregister services
483     l2cap_service_t *service;
484     for (it = (linked_item_t *) &l2cap_services; it ; it = it->next){
485         channel = (l2cap_channel_t *) it->next;
486         if (service->connection == connection){
487             it->next = it->next->next;
488             return;
489         }
490     }
491 }
492 
493 void l2cap_accept_connection_internal(hci_con_handle_t handle,  uint16_t dest_cid){
494     printf("l2cap_accept_connection_internal called but not implemented yet 0x%x, 0x%x\n", handle, dest_cid);
495 }
496 
497 void l2cap_decline_connection_internal(hci_con_handle_t handle, uint16_t dest_cid, uint8_t reason){
498     printf("l2cap_decline_connection_internal called but not implemented yet 0x%x, 0x%x, %u", handle, dest_cid,reason);
499 }
500 
501 //  notify client
502 void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) {
503     uint8_t event[17];
504     event[0] = L2CAP_EVENT_CHANNEL_OPENED;
505     event[1] = sizeof(event) - 2;
506     event[2] = status;
507     bt_flip_addr(&event[3], channel->address);
508     bt_store_16(event,  9, channel->handle);
509     bt_store_16(event, 11, channel->psm);
510     bt_store_16(event, 13, channel->source_cid);
511     bt_store_16(event, 15, channel->dest_cid);
512     socket_connection_send_packet(channel->connection, HCI_EVENT_PACKET, 0, event, sizeof(event));
513 }
514 
515 void l2cap_emit_channel_closed(l2cap_channel_t *channel) {
516     uint8_t event[4];
517     event[0] = L2CAP_EVENT_CHANNEL_CLOSED;
518     event[1] = sizeof(event) - 2;
519     bt_store_16(event, 2, channel->source_cid);
520     socket_connection_send_packet(channel->connection, HCI_EVENT_PACKET, 0, event, sizeof(event));
521 }
522 
523 void l2cap_acl_handler( uint8_t *packet, uint16_t size ){
524 
525     // Capturing?
526     if (capture_connection) {
527         socket_connection_send_packet(capture_connection, HCI_ACL_DATA_PACKET, 0, packet, size);
528         return;
529     }
530 
531     // forward to higher layers - not needed yet
532     // (*data_packet_handler)(channel_id, packet, size);
533 
534     // Get Channel ID and command code
535     uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet);
536     uint8_t  code       = READ_L2CAP_SIGNALING_CODE( packet );
537 
538     // Get Connection
539     hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet);
540 
541     // printf("l2cap_acl_handler channel %u, code %u\n", channel_id, code);
542 
543     // Signaling Packet?
544     if (channel_id == 1) {
545 
546         if (code < 1 || code >= 8){
547             // not for a particular channel, and not CONNECTION_REQUEST
548             return;
549         }
550 
551         // Get Signaling Identifier
552         uint8_t sig_id    = READ_L2CAP_SIGNALING_IDENTIFIER(packet);
553 
554         // CONNECTION_REQUEST
555         if (code == CONNECTION_REQUEST){
556             uint16_t psm =      READ_BT_16(packet, L2CAP_SIGNALING_DATA_OFFSET);
557             uint16_t dest_cid = READ_BT_16(packet, L2CAP_SIGNALING_DATA_OFFSET+2);
558             l2cap_handle_connection_request(handle, sig_id, psm, dest_cid);
559             return;
560         }
561 
562         // Get potential destination CID
563         uint16_t dest_cid = READ_BT_16(packet, L2CAP_SIGNALING_DATA_OFFSET);
564 
565         // Find channel for this sig_id and connection handle
566         linked_item_t *it;
567         for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
568             l2cap_channel_t * channel = (l2cap_channel_t *) it;
569             if (channel->handle == handle) {
570                 if (code & 1) {
571                     // match odd commands by previous signaling identifier
572                     if (channel->sig_id == sig_id) {
573                         l2cap_signaling_handler( channel, packet, size);
574                     }
575                 } else {
576                     // match even commands by source channel id
577                     if (channel->source_cid == dest_cid) {
578                         l2cap_signaling_handler( channel, packet, size);
579                     }
580                 }
581             }
582         }
583         return;
584     }
585 
586     // Find channel for this channel_id and connection handle
587     l2cap_channel_t * channel = l2cap_get_channel_for_source_cid(channel_id);
588     if (channel) {
589         socket_connection_send_packet(channel->connection, L2CAP_DATA_PACKET, channel_id,
590                                       &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
591     }
592 }
593 
594 
595 void l2cap_send_internal(uint16_t source_cid, uint8_t *data, uint16_t len){
596     // find channel for source_cid, construct l2cap packet and send
597     l2cap_channel_t * channel = l2cap_get_channel_for_source_cid(source_cid);
598     if (channel) {
599          // 0 - Connection handle : PB=10 : BC=00
600          bt_store_16(acl_buffer, 0, channel->handle | (2 << 12) | (0 << 14));
601          // 2 - ACL length
602          bt_store_16(acl_buffer, 2,  len + 4);
603          // 4 - L2CAP packet length
604          bt_store_16(acl_buffer, 4,  len + 0);
605          // 6 - L2CAP channel DEST
606          bt_store_16(acl_buffer, 6, channel->dest_cid);
607          // 8 - data
608          memcpy(&acl_buffer[8], data, len);
609          // send
610          hci_send_acl_packet(acl_buffer, len+8);
611      }
612 }
613 
614 void l2cap_set_capture_connection(connection_t * connection){
615     capture_connection = connection;
616 }
617 
618