xref: /btstack/src/l2cap.c (revision f62db1e31aee1f37c3f088521ef5f5c8d96e68cc)
143625864Smatthias.ringwald /*
243625864Smatthias.ringwald  *  l2cap.c
343625864Smatthias.ringwald  *
443625864Smatthias.ringwald  *  Logical Link Control and Adaption Protocl (L2CAP)
543625864Smatthias.ringwald  *
643625864Smatthias.ringwald  *  Created by Matthias Ringwald on 5/16/09.
743625864Smatthias.ringwald  */
843625864Smatthias.ringwald 
943625864Smatthias.ringwald #include "l2cap.h"
1043625864Smatthias.ringwald 
1143625864Smatthias.ringwald #include <stdarg.h>
1243625864Smatthias.ringwald #include <string.h>
1343625864Smatthias.ringwald 
1443625864Smatthias.ringwald #include <stdio.h>
1543625864Smatthias.ringwald 
16fcadd0caSmatthias.ringwald static void null_event_handler(uint8_t *packet, uint16_t size);
17fcadd0caSmatthias.ringwald static void null_data_handler(uint16_t source_cid, uint8_t *packet, uint16_t size);
18fcadd0caSmatthias.ringwald 
191e6aba47Smatthias.ringwald static uint8_t * sig_buffer = NULL;
201e6aba47Smatthias.ringwald static linked_list_t l2cap_channels = NULL;
211e6aba47Smatthias.ringwald static uint8_t * acl_buffer = NULL;
22fcadd0caSmatthias.ringwald static void (*event_packet_handler) (uint8_t *packet, uint16_t size) = null_event_handler;
23fcadd0caSmatthias.ringwald static void (*data_packet_handler)  (uint16_t source_cid, uint8_t *packet, uint16_t size) = null_data_handler;
241e6aba47Smatthias.ringwald 
251e6aba47Smatthias.ringwald void l2cap_init(){
261e6aba47Smatthias.ringwald     sig_buffer = malloc( 48 );
271e6aba47Smatthias.ringwald     acl_buffer = malloc( 255 + 8 );
28fcadd0caSmatthias.ringwald 
29fcadd0caSmatthias.ringwald     //
30fcadd0caSmatthias.ringwald     // register callbacks with HCI
31fcadd0caSmatthias.ringwald     //
32fcadd0caSmatthias.ringwald     hci_register_event_packet_handler(&l2cap_event_handler);
33fcadd0caSmatthias.ringwald     hci_register_acl_packet_handler(&l2cap_acl_handler);
34fcadd0caSmatthias.ringwald }
35fcadd0caSmatthias.ringwald 
36fcadd0caSmatthias.ringwald 
37fcadd0caSmatthias.ringwald /** Register L2CAP packet handlers */
38fcadd0caSmatthias.ringwald static void null_event_handler(uint8_t *packet, uint16_t size){
39fcadd0caSmatthias.ringwald }
40fcadd0caSmatthias.ringwald static void null_data_handler(uint16_t  source_cid, uint8_t *packet, uint16_t size){
41fcadd0caSmatthias.ringwald }
42fcadd0caSmatthias.ringwald void l2cap_register_event_packet_handler(void (*handler)(uint8_t *packet, uint16_t size)){
43fcadd0caSmatthias.ringwald     event_packet_handler = handler;
44fcadd0caSmatthias.ringwald }
45fcadd0caSmatthias.ringwald void l2cap_register_data_packet_handler  (void (*handler)(uint16_t source_cid, uint8_t *packet, uint16_t size)){
46fcadd0caSmatthias.ringwald     data_packet_handler = handler;
471e6aba47Smatthias.ringwald }
481e6aba47Smatthias.ringwald 
490af41d30Smatthias.ringwald int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){
500af41d30Smatthias.ringwald     va_list argptr;
510af41d30Smatthias.ringwald     va_start(argptr, identifier);
520af41d30Smatthias.ringwald     uint16_t len = l2cap_create_signaling_internal(sig_buffer, handle, cmd, identifier, argptr);
531e6aba47Smatthias.ringwald     va_end(argptr);
540af41d30Smatthias.ringwald     return hci_send_acl_packet(sig_buffer, len);
550af41d30Smatthias.ringwald }
560af41d30Smatthias.ringwald 
57*f62db1e3Smatthias.ringwald l2cap_channel_t * l2cap_get_channel_for_source_cid(uint16_t source_cid){
58*f62db1e3Smatthias.ringwald     linked_item_t *it;
59*f62db1e3Smatthias.ringwald     l2cap_channel_t * channel;
60*f62db1e3Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
61*f62db1e3Smatthias.ringwald         channel = (l2cap_channel_t *) it;
62*f62db1e3Smatthias.ringwald         if ( channel->source_cid == source_cid) {
63*f62db1e3Smatthias.ringwald             return channel;
64*f62db1e3Smatthias.ringwald         }
65*f62db1e3Smatthias.ringwald     }
66*f62db1e3Smatthias.ringwald     return NULL;
67*f62db1e3Smatthias.ringwald }
68*f62db1e3Smatthias.ringwald 
691e6aba47Smatthias.ringwald // open outgoing L2CAP channel
701e6aba47Smatthias.ringwald void l2cap_create_channel_internal(connection_t * connection, bd_addr_t address, uint16_t psm){
711e6aba47Smatthias.ringwald 
721e6aba47Smatthias.ringwald     // alloc structure
731e6aba47Smatthias.ringwald     l2cap_channel_t * chan = malloc(sizeof(l2cap_channel_t));
741e6aba47Smatthias.ringwald     // TODO: emit error event
751e6aba47Smatthias.ringwald     if (!chan) return;
761e6aba47Smatthias.ringwald 
771e6aba47Smatthias.ringwald     // fill in
781e6aba47Smatthias.ringwald     BD_ADDR_COPY(chan->address, address);
791e6aba47Smatthias.ringwald     chan->psm = psm;
801e6aba47Smatthias.ringwald     chan->handle = 0;
811e6aba47Smatthias.ringwald     chan->connection = connection;
821e6aba47Smatthias.ringwald 
831e6aba47Smatthias.ringwald     // set initial state
841e6aba47Smatthias.ringwald     chan->state = L2CAP_STATE_CLOSED;
851e6aba47Smatthias.ringwald     chan->sig_id = L2CAP_SIG_ID_INVALID;
861e6aba47Smatthias.ringwald 
871e6aba47Smatthias.ringwald     // add to connections list
881e6aba47Smatthias.ringwald     linked_list_add(&l2cap_channels, (linked_item_t *) chan);
891e6aba47Smatthias.ringwald 
901e6aba47Smatthias.ringwald     // send connection request
911e6aba47Smatthias.ringwald     // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch
921e6aba47Smatthias.ringwald     hci_send_cmd(&hci_create_connection, address, 0x18, 0, 0, 0, 0);
9343625864Smatthias.ringwald }
9443625864Smatthias.ringwald 
951e6aba47Smatthias.ringwald void l2cap_disconnect_internal(uint16_t source_cid, uint8_t reason){
96*f62db1e3Smatthias.ringwald     // find channel for source_cid
97*f62db1e3Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_source_cid(source_cid);
98*f62db1e3Smatthias.ringwald     if (channel) {
99*f62db1e3Smatthias.ringwald         channel->sig_id = l2cap_next_sig_id();
100*f62db1e3Smatthias.ringwald         l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->sig_id, channel->dest_cid, channel->source_cid);
101*f62db1e3Smatthias.ringwald         channel->state = L2CAP_STATE_WAIT_DISCONNECT;
102*f62db1e3Smatthias.ringwald     }
10343625864Smatthias.ringwald }
1041e6aba47Smatthias.ringwald 
1051e6aba47Smatthias.ringwald 
1061e6aba47Smatthias.ringwald void l2cap_event_handler( uint8_t *packet, uint16_t size ){
1071e6aba47Smatthias.ringwald     // handle connection complete events
1081e6aba47Smatthias.ringwald     if (packet[0] == HCI_EVENT_CONNECTION_COMPLETE && packet[2] == 0){
1091e6aba47Smatthias.ringwald         bd_addr_t address;
1101e6aba47Smatthias.ringwald         bt_flip_addr(address, &packet[5]);
1111e6aba47Smatthias.ringwald 
1121e6aba47Smatthias.ringwald         linked_item_t *it;
1131e6aba47Smatthias.ringwald         for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
1141e6aba47Smatthias.ringwald             l2cap_channel_t * chan = (l2cap_channel_t *) it;
1151e6aba47Smatthias.ringwald             if ( ! BD_ADDR_CMP( chan->address, address) ){
1161e6aba47Smatthias.ringwald                 if (chan->state == L2CAP_STATE_CLOSED) {
1171e6aba47Smatthias.ringwald                     chan->handle = READ_BT_16(packet, 3);
1181e6aba47Smatthias.ringwald                     chan->sig_id = l2cap_next_sig_id();
1191e6aba47Smatthias.ringwald                     chan->source_cid = l2cap_next_source_cid();
1201e6aba47Smatthias.ringwald 
1211e6aba47Smatthias.ringwald                     l2cap_send_signaling_packet( chan->handle, CONNECTION_REQUEST, chan->sig_id, chan->psm, chan->source_cid);
1221e6aba47Smatthias.ringwald 
1231e6aba47Smatthias.ringwald                     chan->state = L2CAP_STATE_WAIT_CONNECT_RSP;
1241e6aba47Smatthias.ringwald                 }
1251e6aba47Smatthias.ringwald             }
1261e6aba47Smatthias.ringwald         }
1271e6aba47Smatthias.ringwald     }
1281e6aba47Smatthias.ringwald     // handle disconnection complete events
1291e6aba47Smatthias.ringwald     //@TODO:...
130fcadd0caSmatthias.ringwald 
131fcadd0caSmatthias.ringwald     // forward to higher layers
132fcadd0caSmatthias.ringwald     (*event_packet_handler)(packet, size);
133fcadd0caSmatthias.ringwald 
134fcadd0caSmatthias.ringwald     // forward event to clients
135fcadd0caSmatthias.ringwald     socket_connection_send_packet_all(HCI_EVENT_PACKET, 0, packet, size);
1361e6aba47Smatthias.ringwald }
1371e6aba47Smatthias.ringwald 
1381e6aba47Smatthias.ringwald void l2cap_signaling_handler(l2cap_channel_t *channel, uint8_t *packet, uint16_t size){
1391e6aba47Smatthias.ringwald 
1401e6aba47Smatthias.ringwald     static uint8_t config_options[] = { 1, 2, 150, 0}; // mtu = 48
1411e6aba47Smatthias.ringwald 
1421e6aba47Smatthias.ringwald     uint8_t code       = READ_L2CAP_SIGNALING_CODE( packet );
1431e6aba47Smatthias.ringwald     uint8_t identifier = READ_L2CAP_SIGNALING_IDENTIFIER( packet );
1441e6aba47Smatthias.ringwald 
1451e6aba47Smatthias.ringwald     switch (channel->state) {
1461e6aba47Smatthias.ringwald 
1471e6aba47Smatthias.ringwald         case L2CAP_STATE_WAIT_CONNECT_RSP:
1481e6aba47Smatthias.ringwald             switch (code){
1491e6aba47Smatthias.ringwald                 case CONNECTION_RESPONSE:
1501e6aba47Smatthias.ringwald                     if ( READ_BT_16 (packet, L2CAP_SIGNALING_DATA_OFFSET+3) == 0){
1511e6aba47Smatthias.ringwald                         // successfull connection
1521e6aba47Smatthias.ringwald                         channel->dest_cid = READ_BT_16(packet, L2CAP_SIGNALING_DATA_OFFSET + 0);
1531e6aba47Smatthias.ringwald                         channel->sig_id = l2cap_next_sig_id();
1541e6aba47Smatthias.ringwald                         l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->sig_id, channel->dest_cid, 0, 4, &config_options);
1551e6aba47Smatthias.ringwald                         channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP;
1561e6aba47Smatthias.ringwald                     } else {
1571e6aba47Smatthias.ringwald                         //@TODO: implement failed
1581e6aba47Smatthias.ringwald                     }
1591e6aba47Smatthias.ringwald                     break;
1601e6aba47Smatthias.ringwald                     //@TODO: implement other signaling packets
1611e6aba47Smatthias.ringwald             }
1621e6aba47Smatthias.ringwald             break;
1631e6aba47Smatthias.ringwald 
1641e6aba47Smatthias.ringwald         case L2CAP_STATE_WAIT_CONFIG_REQ_RSP:
1651e6aba47Smatthias.ringwald             switch (code) {
1661e6aba47Smatthias.ringwald                 case CONFIGURE_RESPONSE:
1671e6aba47Smatthias.ringwald                     channel->state = L2CAP_STATE_WAIT_CONFIG_REQ;
1681e6aba47Smatthias.ringwald                     break;
1691e6aba47Smatthias.ringwald             }
1701e6aba47Smatthias.ringwald             break;
1711e6aba47Smatthias.ringwald 
1721e6aba47Smatthias.ringwald         case L2CAP_STATE_WAIT_CONFIG_REQ:
1731e6aba47Smatthias.ringwald             switch (code) {
1741e6aba47Smatthias.ringwald                 case CONFIGURE_REQUEST:
1751e6aba47Smatthias.ringwald 
1761e6aba47Smatthias.ringwald                     // accept the other's configuration options
1771e6aba47Smatthias.ringwald                     l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, identifier, channel->dest_cid, 0, 0, size - 16, &packet[16]);
1781e6aba47Smatthias.ringwald 
1791e6aba47Smatthias.ringwald                     channel->state = L2CAP_STATE_OPEN;
180c8e4258aSmatthias.ringwald                     l2cap_emit_channel_opened(channel);
181c8e4258aSmatthias.ringwald                     break;
182c8e4258aSmatthias.ringwald             }
183c8e4258aSmatthias.ringwald             break;
184*f62db1e3Smatthias.ringwald 
185*f62db1e3Smatthias.ringwald         case L2CAP_STATE_WAIT_DISCONNECT:
186*f62db1e3Smatthias.ringwald             switch (code) {
187*f62db1e3Smatthias.ringwald                 case DISCONNECTION_RESPONSE:
188*f62db1e3Smatthias.ringwald                     channel->state = L2CAP_STATE_CLOSED;
189*f62db1e3Smatthias.ringwald                     l2cap_emit_channel_closed(channel);
190*f62db1e3Smatthias.ringwald 
191*f62db1e3Smatthias.ringwald                     // discard channel
192*f62db1e3Smatthias.ringwald                     linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
193*f62db1e3Smatthias.ringwald                     free (channel);
194*f62db1e3Smatthias.ringwald                     break;
195*f62db1e3Smatthias.ringwald             }
196*f62db1e3Smatthias.ringwald             break;
197c8e4258aSmatthias.ringwald     }
198c8e4258aSmatthias.ringwald }
1991e6aba47Smatthias.ringwald 
2001e6aba47Smatthias.ringwald //  notify client
201c8e4258aSmatthias.ringwald void l2cap_emit_channel_opened(l2cap_channel_t *channel) {
202c8e4258aSmatthias.ringwald     uint8_t event[16];
2031e6aba47Smatthias.ringwald     event[0] = HCI_EVENT_L2CAP_CHANNEL_OPENED;
204c8e4258aSmatthias.ringwald     event[1] = sizeof(event) - 2;
205c8e4258aSmatthias.ringwald     bt_flip_addr(&event[2], channel->address);
206c8e4258aSmatthias.ringwald     bt_store_16(event,  8, channel->handle);
207c8e4258aSmatthias.ringwald     bt_store_16(event, 10, channel->psm);
208c8e4258aSmatthias.ringwald     bt_store_16(event, 12, channel->source_cid);
209c8e4258aSmatthias.ringwald     bt_store_16(event, 14, channel->dest_cid);
2101e6aba47Smatthias.ringwald     socket_connection_send_packet(channel->connection, HCI_EVENT_PACKET, 0, event, sizeof(event));
2111e6aba47Smatthias.ringwald }
2121e6aba47Smatthias.ringwald 
213*f62db1e3Smatthias.ringwald void l2cap_emit_channel_closed(l2cap_channel_t *channel) {
214*f62db1e3Smatthias.ringwald     uint8_t event[4];
215*f62db1e3Smatthias.ringwald     event[0] = HCI_EVENT_L2CAP_CHANNEL_CLOSED;
216*f62db1e3Smatthias.ringwald     event[1] = sizeof(event) - 2;
217*f62db1e3Smatthias.ringwald     bt_store_16(event, 2, channel->source_cid);
218*f62db1e3Smatthias.ringwald     socket_connection_send_packet(channel->connection, HCI_EVENT_PACKET, 0, event, sizeof(event));
219*f62db1e3Smatthias.ringwald }
220*f62db1e3Smatthias.ringwald 
2211e6aba47Smatthias.ringwald void l2cap_acl_handler( uint8_t *packet, uint16_t size ){
2221e6aba47Smatthias.ringwald 
2231e6aba47Smatthias.ringwald     // Get Channel ID and command code
2241e6aba47Smatthias.ringwald     uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet);
2251e6aba47Smatthias.ringwald     uint8_t  code       = READ_L2CAP_SIGNALING_CODE( packet );
2261e6aba47Smatthias.ringwald 
2271e6aba47Smatthias.ringwald     // Get Connection
2281e6aba47Smatthias.ringwald     hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet);
2291e6aba47Smatthias.ringwald 
2301e6aba47Smatthias.ringwald     // Signaling Packet?
2311e6aba47Smatthias.ringwald     if (channel_id == 1) {
2321e6aba47Smatthias.ringwald 
2331e6aba47Smatthias.ringwald         if (code < 1 || code == 2 || code >= 8){
2341e6aba47Smatthias.ringwald             // not for a particular channel
2351e6aba47Smatthias.ringwald             return;
2361e6aba47Smatthias.ringwald         }
2371e6aba47Smatthias.ringwald 
2381e6aba47Smatthias.ringwald         // Get Signaling Identifier and potential destination CID
2391e6aba47Smatthias.ringwald         uint8_t sig_id    = READ_L2CAP_SIGNALING_IDENTIFIER(packet);
2401e6aba47Smatthias.ringwald         uint16_t dest_cid = READ_BT_16(packet, L2CAP_SIGNALING_DATA_OFFSET);
2411e6aba47Smatthias.ringwald 
2421e6aba47Smatthias.ringwald         // Find channel for this sig_id and connection handle
2431e6aba47Smatthias.ringwald         linked_item_t *it;
2441e6aba47Smatthias.ringwald         for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
2451e6aba47Smatthias.ringwald             l2cap_channel_t * chan = (l2cap_channel_t *) it;
2461e6aba47Smatthias.ringwald             if (chan->handle == handle) {
2471e6aba47Smatthias.ringwald                 if (code & 1) {
2481e6aba47Smatthias.ringwald                     // match odd commands by previous signaling identifier
2491e6aba47Smatthias.ringwald                     if (chan->sig_id == sig_id) {
2501e6aba47Smatthias.ringwald                         l2cap_signaling_handler( chan, packet, size);
2511e6aba47Smatthias.ringwald                     }
2521e6aba47Smatthias.ringwald                 } else {
2531e6aba47Smatthias.ringwald                     // match even commands by source channel id
2541e6aba47Smatthias.ringwald                     if (chan->source_cid == dest_cid) {
2551e6aba47Smatthias.ringwald                         l2cap_signaling_handler( chan, packet, size);
2561e6aba47Smatthias.ringwald                     }
2571e6aba47Smatthias.ringwald                 }
2581e6aba47Smatthias.ringwald             }
2591e6aba47Smatthias.ringwald         }
2601e6aba47Smatthias.ringwald         return;
2611e6aba47Smatthias.ringwald     }
2621e6aba47Smatthias.ringwald 
2631e6aba47Smatthias.ringwald     // Find channel for this channel_id and connection handle
264*f62db1e3Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_source_cid(channel_id);
265*f62db1e3Smatthias.ringwald     if (channel) {
2661e6aba47Smatthias.ringwald         socket_connection_send_packet(channel->connection, HCI_ACL_DATA_PACKET, 0, packet, size);
2671e6aba47Smatthias.ringwald     }
268fcadd0caSmatthias.ringwald 
269fcadd0caSmatthias.ringwald      // forward to higher layers
270fcadd0caSmatthias.ringwald     (*data_packet_handler)(channel_id, packet, size);
2711e6aba47Smatthias.ringwald }
2721e6aba47Smatthias.ringwald 
273*f62db1e3Smatthias.ringwald 
2741e6aba47Smatthias.ringwald void l2cap_send_internal(uint16_t source_cid, uint8_t *data, uint16_t len){
2751e6aba47Smatthias.ringwald     // find channel for source_cid, construct l2cap packet and send
276*f62db1e3Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_source_cid(source_cid);
277fcadd0caSmatthias.ringwald     if (channel) {
2781e6aba47Smatthias.ringwald          // 0 - Connection handle : PB=10 : BC=00
2791e6aba47Smatthias.ringwald          bt_store_16(acl_buffer, 0, channel->handle | (2 << 12) | (0 << 14));
2801e6aba47Smatthias.ringwald          // 2 - ACL length
2811e6aba47Smatthias.ringwald          bt_store_16(acl_buffer, 2,  len + 4);
2821e6aba47Smatthias.ringwald          // 4 - L2CAP packet length
2831e6aba47Smatthias.ringwald          bt_store_16(acl_buffer, 4,  len + 0);
2841e6aba47Smatthias.ringwald          // 6 - L2CAP channel DEST
2851e6aba47Smatthias.ringwald          bt_store_16(acl_buffer, 6, channel->dest_cid);
2861e6aba47Smatthias.ringwald          // 8 - data
2871e6aba47Smatthias.ringwald          memcpy(&acl_buffer[8], data, len);
2881e6aba47Smatthias.ringwald          // send
2891e6aba47Smatthias.ringwald          hci_send_acl_packet(acl_buffer, len+8);
2901e6aba47Smatthias.ringwald      }
2911e6aba47Smatthias.ringwald }
2921e6aba47Smatthias.ringwald 
2931e6aba47Smatthias.ringwald 
294