143625864Smatthias.ringwald /* 21713bceaSmatthias.ringwald * Copyright (C) 2009 by Matthias Ringwald 31713bceaSmatthias.ringwald * 41713bceaSmatthias.ringwald * Redistribution and use in source and binary forms, with or without 51713bceaSmatthias.ringwald * modification, are permitted provided that the following conditions 61713bceaSmatthias.ringwald * are met: 71713bceaSmatthias.ringwald * 81713bceaSmatthias.ringwald * 1. Redistributions of source code must retain the above copyright 91713bceaSmatthias.ringwald * notice, this list of conditions and the following disclaimer. 101713bceaSmatthias.ringwald * 2. Redistributions in binary form must reproduce the above copyright 111713bceaSmatthias.ringwald * notice, this list of conditions and the following disclaimer in the 121713bceaSmatthias.ringwald * documentation and/or other materials provided with the distribution. 131713bceaSmatthias.ringwald * 3. Neither the name of the copyright holders nor the names of 141713bceaSmatthias.ringwald * contributors may be used to endorse or promote products derived 151713bceaSmatthias.ringwald * from this software without specific prior written permission. 161713bceaSmatthias.ringwald * 171713bceaSmatthias.ringwald * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS 181713bceaSmatthias.ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 191713bceaSmatthias.ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 201713bceaSmatthias.ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 211713bceaSmatthias.ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 221713bceaSmatthias.ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 231713bceaSmatthias.ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 241713bceaSmatthias.ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 251713bceaSmatthias.ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 261713bceaSmatthias.ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 271713bceaSmatthias.ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 281713bceaSmatthias.ringwald * SUCH DAMAGE. 291713bceaSmatthias.ringwald * 301713bceaSmatthias.ringwald */ 311713bceaSmatthias.ringwald 321713bceaSmatthias.ringwald /* 3343625864Smatthias.ringwald * l2cap.c 3443625864Smatthias.ringwald * 3543625864Smatthias.ringwald * Logical Link Control and Adaption Protocl (L2CAP) 3643625864Smatthias.ringwald * 3743625864Smatthias.ringwald * Created by Matthias Ringwald on 5/16/09. 3843625864Smatthias.ringwald */ 3943625864Smatthias.ringwald 4043625864Smatthias.ringwald #include "l2cap.h" 4143625864Smatthias.ringwald 4243625864Smatthias.ringwald #include <stdarg.h> 4343625864Smatthias.ringwald #include <string.h> 4443625864Smatthias.ringwald 4543625864Smatthias.ringwald #include <stdio.h> 4643625864Smatthias.ringwald 476f60b3f4Smatthias.ringwald // size of HCI ACL + L2CAP Header for regular data packets 486f60b3f4Smatthias.ringwald #define COMPLETE_L2CAP_HEADER 8 496f60b3f4Smatthias.ringwald 50fcadd0caSmatthias.ringwald static void null_event_handler(uint8_t *packet, uint16_t size); 51fcadd0caSmatthias.ringwald static void null_data_handler(uint16_t source_cid, uint8_t *packet, uint16_t size); 52fcadd0caSmatthias.ringwald 531e6aba47Smatthias.ringwald static uint8_t * sig_buffer = NULL; 541e6aba47Smatthias.ringwald static linked_list_t l2cap_channels = NULL; 551e6aba47Smatthias.ringwald static uint8_t * acl_buffer = NULL; 56fcadd0caSmatthias.ringwald static void (*event_packet_handler) (uint8_t *packet, uint16_t size) = null_event_handler; 57fcadd0caSmatthias.ringwald static void (*data_packet_handler) (uint16_t source_cid, uint8_t *packet, uint16_t size) = null_data_handler; 589edc8742Smatthias.ringwald static connection_t * capture_connection = NULL; 591e6aba47Smatthias.ringwald 601e6aba47Smatthias.ringwald void l2cap_init(){ 611e6aba47Smatthias.ringwald sig_buffer = malloc( 48 ); 621e6aba47Smatthias.ringwald acl_buffer = malloc( 255 + 8 ); 63fcadd0caSmatthias.ringwald 64fcadd0caSmatthias.ringwald // 65fcadd0caSmatthias.ringwald // register callbacks with HCI 66fcadd0caSmatthias.ringwald // 67fcadd0caSmatthias.ringwald hci_register_event_packet_handler(&l2cap_event_handler); 68fcadd0caSmatthias.ringwald hci_register_acl_packet_handler(&l2cap_acl_handler); 69fcadd0caSmatthias.ringwald } 70fcadd0caSmatthias.ringwald 71fcadd0caSmatthias.ringwald 72fcadd0caSmatthias.ringwald /** Register L2CAP packet handlers */ 73fcadd0caSmatthias.ringwald static void null_event_handler(uint8_t *packet, uint16_t size){ 74fcadd0caSmatthias.ringwald } 75fcadd0caSmatthias.ringwald static void null_data_handler(uint16_t source_cid, uint8_t *packet, uint16_t size){ 76fcadd0caSmatthias.ringwald } 77fcadd0caSmatthias.ringwald void l2cap_register_event_packet_handler(void (*handler)(uint8_t *packet, uint16_t size)){ 78fcadd0caSmatthias.ringwald event_packet_handler = handler; 79fcadd0caSmatthias.ringwald } 80fcadd0caSmatthias.ringwald void l2cap_register_data_packet_handler (void (*handler)(uint16_t source_cid, uint8_t *packet, uint16_t size)){ 81fcadd0caSmatthias.ringwald data_packet_handler = handler; 821e6aba47Smatthias.ringwald } 831e6aba47Smatthias.ringwald 840af41d30Smatthias.ringwald int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){ 850af41d30Smatthias.ringwald va_list argptr; 860af41d30Smatthias.ringwald va_start(argptr, identifier); 870af41d30Smatthias.ringwald uint16_t len = l2cap_create_signaling_internal(sig_buffer, handle, cmd, identifier, argptr); 881e6aba47Smatthias.ringwald va_end(argptr); 890af41d30Smatthias.ringwald return hci_send_acl_packet(sig_buffer, len); 900af41d30Smatthias.ringwald } 910af41d30Smatthias.ringwald 92f62db1e3Smatthias.ringwald l2cap_channel_t * l2cap_get_channel_for_source_cid(uint16_t source_cid){ 93f62db1e3Smatthias.ringwald linked_item_t *it; 94f62db1e3Smatthias.ringwald l2cap_channel_t * channel; 95f62db1e3Smatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 96f62db1e3Smatthias.ringwald channel = (l2cap_channel_t *) it; 97f62db1e3Smatthias.ringwald if ( channel->source_cid == source_cid) { 98f62db1e3Smatthias.ringwald return channel; 99f62db1e3Smatthias.ringwald } 100f62db1e3Smatthias.ringwald } 101f62db1e3Smatthias.ringwald return NULL; 102f62db1e3Smatthias.ringwald } 103f62db1e3Smatthias.ringwald 1041e6aba47Smatthias.ringwald // open outgoing L2CAP channel 1051e6aba47Smatthias.ringwald void l2cap_create_channel_internal(connection_t * connection, bd_addr_t address, uint16_t psm){ 1061e6aba47Smatthias.ringwald 1071e6aba47Smatthias.ringwald // alloc structure 1081e6aba47Smatthias.ringwald l2cap_channel_t * chan = malloc(sizeof(l2cap_channel_t)); 1091e6aba47Smatthias.ringwald // TODO: emit error event 1101e6aba47Smatthias.ringwald if (!chan) return; 1111e6aba47Smatthias.ringwald 1121e6aba47Smatthias.ringwald // fill in 1131e6aba47Smatthias.ringwald BD_ADDR_COPY(chan->address, address); 1141e6aba47Smatthias.ringwald chan->psm = psm; 1151e6aba47Smatthias.ringwald chan->handle = 0; 1161e6aba47Smatthias.ringwald chan->connection = connection; 1171e6aba47Smatthias.ringwald 1181e6aba47Smatthias.ringwald // set initial state 1191e6aba47Smatthias.ringwald chan->state = L2CAP_STATE_CLOSED; 1201e6aba47Smatthias.ringwald chan->sig_id = L2CAP_SIG_ID_INVALID; 1211e6aba47Smatthias.ringwald 1221e6aba47Smatthias.ringwald // add to connections list 1231e6aba47Smatthias.ringwald linked_list_add(&l2cap_channels, (linked_item_t *) chan); 1241e6aba47Smatthias.ringwald 1251e6aba47Smatthias.ringwald // send connection request 1261e6aba47Smatthias.ringwald // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch 1271e6aba47Smatthias.ringwald hci_send_cmd(&hci_create_connection, address, 0x18, 0, 0, 0, 0); 12843625864Smatthias.ringwald } 12943625864Smatthias.ringwald 1301e6aba47Smatthias.ringwald void l2cap_disconnect_internal(uint16_t source_cid, uint8_t reason){ 131f62db1e3Smatthias.ringwald // find channel for source_cid 132f62db1e3Smatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_source_cid(source_cid); 133f62db1e3Smatthias.ringwald if (channel) { 134f62db1e3Smatthias.ringwald channel->sig_id = l2cap_next_sig_id(); 135f62db1e3Smatthias.ringwald l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->sig_id, channel->dest_cid, channel->source_cid); 136f62db1e3Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_DISCONNECT; 137f62db1e3Smatthias.ringwald } 13843625864Smatthias.ringwald } 1391e6aba47Smatthias.ringwald 1401e6aba47Smatthias.ringwald 1411e6aba47Smatthias.ringwald void l2cap_event_handler( uint8_t *packet, uint16_t size ){ 1421e6aba47Smatthias.ringwald // handle connection complete events 14303cfbabcSmatthias.ringwald if (packet[0] == HCI_EVENT_CONNECTION_COMPLETE) { 1441e6aba47Smatthias.ringwald bd_addr_t address; 1451e6aba47Smatthias.ringwald bt_flip_addr(address, &packet[5]); 1461e6aba47Smatthias.ringwald 1471e6aba47Smatthias.ringwald linked_item_t *it; 1481e6aba47Smatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 1491e6aba47Smatthias.ringwald l2cap_channel_t * chan = (l2cap_channel_t *) it; 1501e6aba47Smatthias.ringwald if ( ! BD_ADDR_CMP( chan->address, address) ){ 1511e6aba47Smatthias.ringwald if (chan->state == L2CAP_STATE_CLOSED) { 15203cfbabcSmatthias.ringwald if (packet[2] == 0){ 1531e6aba47Smatthias.ringwald chan->handle = READ_BT_16(packet, 3); 1541e6aba47Smatthias.ringwald chan->sig_id = l2cap_next_sig_id(); 1551e6aba47Smatthias.ringwald chan->source_cid = l2cap_next_source_cid(); 1561e6aba47Smatthias.ringwald 1571e6aba47Smatthias.ringwald l2cap_send_signaling_packet( chan->handle, CONNECTION_REQUEST, chan->sig_id, chan->psm, chan->source_cid); 1581e6aba47Smatthias.ringwald 1591e6aba47Smatthias.ringwald chan->state = L2CAP_STATE_WAIT_CONNECT_RSP; 16003cfbabcSmatthias.ringwald } else { 16103cfbabcSmatthias.ringwald l2cap_emit_channel_opened(chan, packet[2]); // failure, forward error code 16203cfbabcSmatthias.ringwald } 1631e6aba47Smatthias.ringwald } 1641e6aba47Smatthias.ringwald } 1651e6aba47Smatthias.ringwald } 1661e6aba47Smatthias.ringwald } 16727a923d0Smatthias.ringwald 1681e6aba47Smatthias.ringwald // handle disconnection complete events 16927a923d0Smatthias.ringwald if (packet[0] == HCI_EVENT_DISCONNECTION_COMPLETE) { 17027a923d0Smatthias.ringwald // send l2cap disconnect events for all channels on this handle 17127a923d0Smatthias.ringwald hci_con_handle_t handle = READ_BT_16(packet, 3); 17227a923d0Smatthias.ringwald linked_item_t *it; 17327a923d0Smatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 17427a923d0Smatthias.ringwald l2cap_channel_t * channel = (l2cap_channel_t *) it; 17527a923d0Smatthias.ringwald if ( channel->handle == handle ){ 17627a923d0Smatthias.ringwald l2cap_finialize_channel_close(channel); 17727a923d0Smatthias.ringwald } 17827a923d0Smatthias.ringwald } 17927a923d0Smatthias.ringwald } 180fcadd0caSmatthias.ringwald 181ee091cf1Smatthias.ringwald // HCI Connection Timeouts 18280d52d6bSmatthias.ringwald if (packet[0] == L2CAP_EVENT_TIMEOUT_CHECK){ 183ee091cf1Smatthias.ringwald hci_con_handle_t handle = READ_BT_16(packet, 2); 184ee091cf1Smatthias.ringwald linked_item_t *it; 185ee091cf1Smatthias.ringwald l2cap_channel_t * channel; 186ee091cf1Smatthias.ringwald int used = 0; 187ee091cf1Smatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 188ee091cf1Smatthias.ringwald channel = (l2cap_channel_t *) it; 189ee091cf1Smatthias.ringwald if (channel->handle == handle) { 190ee091cf1Smatthias.ringwald used = 1; 191ee091cf1Smatthias.ringwald } 192ee091cf1Smatthias.ringwald } 193ee091cf1Smatthias.ringwald if (!used) { 1949edc8742Smatthias.ringwald hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection 195ee091cf1Smatthias.ringwald } 196ee091cf1Smatthias.ringwald } 197ee091cf1Smatthias.ringwald 198fcadd0caSmatthias.ringwald (*event_packet_handler)(packet, size); 1991e6aba47Smatthias.ringwald } 2001e6aba47Smatthias.ringwald 2011e6aba47Smatthias.ringwald void l2cap_signaling_handler(l2cap_channel_t *channel, uint8_t *packet, uint16_t size){ 2021e6aba47Smatthias.ringwald 2031e6aba47Smatthias.ringwald static uint8_t config_options[] = { 1, 2, 150, 0}; // mtu = 48 2041e6aba47Smatthias.ringwald 2051e6aba47Smatthias.ringwald uint8_t code = READ_L2CAP_SIGNALING_CODE( packet ); 2061e6aba47Smatthias.ringwald uint8_t identifier = READ_L2CAP_SIGNALING_IDENTIFIER( packet ); 20738e5900eSmatthias.ringwald uint16_t result = 0; 2081e6aba47Smatthias.ringwald 2091e6aba47Smatthias.ringwald switch (channel->state) { 2101e6aba47Smatthias.ringwald 2111e6aba47Smatthias.ringwald case L2CAP_STATE_WAIT_CONNECT_RSP: 2121e6aba47Smatthias.ringwald switch (code){ 2131e6aba47Smatthias.ringwald case CONNECTION_RESPONSE: 214141679a4Smatthias.ringwald result = READ_BT_16 (packet, L2CAP_SIGNALING_DATA_OFFSET+4); 21538e5900eSmatthias.ringwald switch (result) { 21638e5900eSmatthias.ringwald case 0: 2171e6aba47Smatthias.ringwald // successfull connection 218cd56f931Smatthias.ringwald channel->dest_cid = READ_BT_16(packet, L2CAP_SIGNALING_DATA_OFFSET); 2191e6aba47Smatthias.ringwald channel->sig_id = l2cap_next_sig_id(); 2201e6aba47Smatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->sig_id, channel->dest_cid, 0, 4, &config_options); 221*5a67bd4aSmatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ; 22238e5900eSmatthias.ringwald break; 22338e5900eSmatthias.ringwald case 1: 22438e5900eSmatthias.ringwald // connection pending. get some coffee 22538e5900eSmatthias.ringwald break; 22638e5900eSmatthias.ringwald default: 227f32b992eSmatthias.ringwald // map l2cap connection response result to BTstack status enumeration 22838e5900eSmatthias.ringwald l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result); 22938e5900eSmatthias.ringwald break; 2301e6aba47Smatthias.ringwald } 2311e6aba47Smatthias.ringwald break; 23238e5900eSmatthias.ringwald 23338e5900eSmatthias.ringwald default: 2341e6aba47Smatthias.ringwald //@TODO: implement other signaling packets 23538e5900eSmatthias.ringwald break; 2361e6aba47Smatthias.ringwald } 2371e6aba47Smatthias.ringwald break; 2381e6aba47Smatthias.ringwald 239*5a67bd4aSmatthias.ringwald case L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ: 2401e6aba47Smatthias.ringwald switch (code) { 2411e6aba47Smatthias.ringwald case CONFIGURE_RESPONSE: 2421e6aba47Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONFIG_REQ; 2431e6aba47Smatthias.ringwald break; 244*5a67bd4aSmatthias.ringwald case CONFIGURE_REQUEST: 245*5a67bd4aSmatthias.ringwald // accept the other's configuration options 246*5a67bd4aSmatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, identifier, channel->dest_cid, 0, 0, size - 16, &packet[16]); 247*5a67bd4aSmatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP; 248*5a67bd4aSmatthias.ringwald break; 249*5a67bd4aSmatthias.ringwald default: 250*5a67bd4aSmatthias.ringwald //@TODO: implement other signaling packets 251*5a67bd4aSmatthias.ringwald break; 2521e6aba47Smatthias.ringwald } 2531e6aba47Smatthias.ringwald break; 2541e6aba47Smatthias.ringwald 2551e6aba47Smatthias.ringwald case L2CAP_STATE_WAIT_CONFIG_REQ: 2561e6aba47Smatthias.ringwald switch (code) { 2571e6aba47Smatthias.ringwald case CONFIGURE_REQUEST: 2581e6aba47Smatthias.ringwald // accept the other's configuration options 2591e6aba47Smatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, identifier, channel->dest_cid, 0, 0, size - 16, &packet[16]); 2601e6aba47Smatthias.ringwald channel->state = L2CAP_STATE_OPEN; 26103cfbabcSmatthias.ringwald l2cap_emit_channel_opened(channel, 0); // success 262c8e4258aSmatthias.ringwald break; 263*5a67bd4aSmatthias.ringwald default: 264*5a67bd4aSmatthias.ringwald //@TODO: implement other signaling packets 265*5a67bd4aSmatthias.ringwald break; 266*5a67bd4aSmatthias.ringwald } 267*5a67bd4aSmatthias.ringwald break; 268*5a67bd4aSmatthias.ringwald 269*5a67bd4aSmatthias.ringwald case L2CAP_STATE_WAIT_CONFIG_REQ_RSP: 270*5a67bd4aSmatthias.ringwald switch (code) { 271*5a67bd4aSmatthias.ringwald case CONFIGURE_RESPONSE: 272*5a67bd4aSmatthias.ringwald channel->state = L2CAP_STATE_OPEN; 273*5a67bd4aSmatthias.ringwald l2cap_emit_channel_opened(channel, 0); // success 274*5a67bd4aSmatthias.ringwald break; 275*5a67bd4aSmatthias.ringwald default: 276*5a67bd4aSmatthias.ringwald //@TODO: implement other signaling packets 277*5a67bd4aSmatthias.ringwald break; 278c8e4258aSmatthias.ringwald } 279c8e4258aSmatthias.ringwald break; 280f62db1e3Smatthias.ringwald 281f62db1e3Smatthias.ringwald case L2CAP_STATE_WAIT_DISCONNECT: 282f62db1e3Smatthias.ringwald switch (code) { 283f62db1e3Smatthias.ringwald case DISCONNECTION_RESPONSE: 28427a923d0Smatthias.ringwald l2cap_finialize_channel_close(channel); 28527a923d0Smatthias.ringwald break; 286*5a67bd4aSmatthias.ringwald default: 287*5a67bd4aSmatthias.ringwald //@TODO: implement other signaling packets 288*5a67bd4aSmatthias.ringwald break; 28927a923d0Smatthias.ringwald } 29027a923d0Smatthias.ringwald break; 291*5a67bd4aSmatthias.ringwald default: 292*5a67bd4aSmatthias.ringwald //@TODO: implement other signaling packets 293*5a67bd4aSmatthias.ringwald break; 29427a923d0Smatthias.ringwald } 29527a923d0Smatthias.ringwald } 29627a923d0Smatthias.ringwald 29727a923d0Smatthias.ringwald // finalize closed channel 29827a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t *channel){ 299f62db1e3Smatthias.ringwald channel->state = L2CAP_STATE_CLOSED; 300f62db1e3Smatthias.ringwald l2cap_emit_channel_closed(channel); 301f62db1e3Smatthias.ringwald 302f62db1e3Smatthias.ringwald // discard channel 303f62db1e3Smatthias.ringwald linked_list_remove(&l2cap_channels, (linked_item_t *) channel); 304f62db1e3Smatthias.ringwald free (channel); 305c8e4258aSmatthias.ringwald } 3061e6aba47Smatthias.ringwald 307c52bf64dSmatthias.ringwald // 308c52bf64dSmatthias.ringwald void l2cap_close_channels_for_connection(connection_t *connection){ 309c52bf64dSmatthias.ringwald linked_item_t *it; 310c52bf64dSmatthias.ringwald l2cap_channel_t * channel; 311c52bf64dSmatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 312c52bf64dSmatthias.ringwald channel = (l2cap_channel_t *) it; 313c52bf64dSmatthias.ringwald if ( channel->connection == connection) { 314c52bf64dSmatthias.ringwald channel->sig_id = l2cap_next_sig_id(); 315c52bf64dSmatthias.ringwald l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->sig_id, channel->dest_cid, channel->source_cid); 316c52bf64dSmatthias.ringwald channel->state = L2CAP_STATE_WAIT_DISCONNECT; 317c52bf64dSmatthias.ringwald } 318c52bf64dSmatthias.ringwald } 319c52bf64dSmatthias.ringwald } 320c52bf64dSmatthias.ringwald 3211e6aba47Smatthias.ringwald // notify client 32203cfbabcSmatthias.ringwald void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) { 32303cfbabcSmatthias.ringwald uint8_t event[17]; 32480d52d6bSmatthias.ringwald event[0] = L2CAP_EVENT_CHANNEL_OPENED; 325c8e4258aSmatthias.ringwald event[1] = sizeof(event) - 2; 32603cfbabcSmatthias.ringwald event[2] = status; 32703cfbabcSmatthias.ringwald bt_flip_addr(&event[3], channel->address); 32803cfbabcSmatthias.ringwald bt_store_16(event, 9, channel->handle); 32903cfbabcSmatthias.ringwald bt_store_16(event, 11, channel->psm); 33003cfbabcSmatthias.ringwald bt_store_16(event, 13, channel->source_cid); 33103cfbabcSmatthias.ringwald bt_store_16(event, 15, channel->dest_cid); 3321e6aba47Smatthias.ringwald socket_connection_send_packet(channel->connection, HCI_EVENT_PACKET, 0, event, sizeof(event)); 3331e6aba47Smatthias.ringwald } 3341e6aba47Smatthias.ringwald 335f62db1e3Smatthias.ringwald void l2cap_emit_channel_closed(l2cap_channel_t *channel) { 336f62db1e3Smatthias.ringwald uint8_t event[4]; 33780d52d6bSmatthias.ringwald event[0] = L2CAP_EVENT_CHANNEL_CLOSED; 338f62db1e3Smatthias.ringwald event[1] = sizeof(event) - 2; 339f62db1e3Smatthias.ringwald bt_store_16(event, 2, channel->source_cid); 340f62db1e3Smatthias.ringwald socket_connection_send_packet(channel->connection, HCI_EVENT_PACKET, 0, event, sizeof(event)); 341f62db1e3Smatthias.ringwald } 342f62db1e3Smatthias.ringwald 3431e6aba47Smatthias.ringwald void l2cap_acl_handler( uint8_t *packet, uint16_t size ){ 3441e6aba47Smatthias.ringwald 3459edc8742Smatthias.ringwald // Capturing? 3469edc8742Smatthias.ringwald if (capture_connection) { 3479edc8742Smatthias.ringwald socket_connection_send_packet(capture_connection, HCI_ACL_DATA_PACKET, 0, packet, size); 3489edc8742Smatthias.ringwald } 3499edc8742Smatthias.ringwald 3509edc8742Smatthias.ringwald // forward to higher layers - not needed yet 3519edc8742Smatthias.ringwald // (*data_packet_handler)(channel_id, packet, size); 3529edc8742Smatthias.ringwald 3531e6aba47Smatthias.ringwald // Get Channel ID and command code 3541e6aba47Smatthias.ringwald uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet); 3551e6aba47Smatthias.ringwald uint8_t code = READ_L2CAP_SIGNALING_CODE( packet ); 3561e6aba47Smatthias.ringwald 3571e6aba47Smatthias.ringwald // Get Connection 3581e6aba47Smatthias.ringwald hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet); 3591e6aba47Smatthias.ringwald 3601e6aba47Smatthias.ringwald // Signaling Packet? 3611e6aba47Smatthias.ringwald if (channel_id == 1) { 3621e6aba47Smatthias.ringwald 3631e6aba47Smatthias.ringwald if (code < 1 || code == 2 || code >= 8){ 3641e6aba47Smatthias.ringwald // not for a particular channel 3651e6aba47Smatthias.ringwald return; 3661e6aba47Smatthias.ringwald } 3671e6aba47Smatthias.ringwald 3681e6aba47Smatthias.ringwald // Get Signaling Identifier and potential destination CID 3691e6aba47Smatthias.ringwald uint8_t sig_id = READ_L2CAP_SIGNALING_IDENTIFIER(packet); 3701e6aba47Smatthias.ringwald uint16_t dest_cid = READ_BT_16(packet, L2CAP_SIGNALING_DATA_OFFSET); 3711e6aba47Smatthias.ringwald 3721e6aba47Smatthias.ringwald // Find channel for this sig_id and connection handle 3731e6aba47Smatthias.ringwald linked_item_t *it; 3741e6aba47Smatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 3751e6aba47Smatthias.ringwald l2cap_channel_t * chan = (l2cap_channel_t *) it; 3761e6aba47Smatthias.ringwald if (chan->handle == handle) { 3771e6aba47Smatthias.ringwald if (code & 1) { 3781e6aba47Smatthias.ringwald // match odd commands by previous signaling identifier 3791e6aba47Smatthias.ringwald if (chan->sig_id == sig_id) { 3801e6aba47Smatthias.ringwald l2cap_signaling_handler( chan, packet, size); 3811e6aba47Smatthias.ringwald } 3821e6aba47Smatthias.ringwald } else { 3831e6aba47Smatthias.ringwald // match even commands by source channel id 3841e6aba47Smatthias.ringwald if (chan->source_cid == dest_cid) { 3851e6aba47Smatthias.ringwald l2cap_signaling_handler( chan, packet, size); 3861e6aba47Smatthias.ringwald } 3871e6aba47Smatthias.ringwald } 3881e6aba47Smatthias.ringwald } 3891e6aba47Smatthias.ringwald } 3901e6aba47Smatthias.ringwald return; 3911e6aba47Smatthias.ringwald } 3921e6aba47Smatthias.ringwald 3931e6aba47Smatthias.ringwald // Find channel for this channel_id and connection handle 394f62db1e3Smatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_source_cid(channel_id); 395f62db1e3Smatthias.ringwald if (channel) { 3966f60b3f4Smatthias.ringwald socket_connection_send_packet(channel->connection, L2CAP_DATA_PACKET, channel_id, 3976f60b3f4Smatthias.ringwald &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 3981e6aba47Smatthias.ringwald } 3991e6aba47Smatthias.ringwald } 4001e6aba47Smatthias.ringwald 401f62db1e3Smatthias.ringwald 4021e6aba47Smatthias.ringwald void l2cap_send_internal(uint16_t source_cid, uint8_t *data, uint16_t len){ 4031e6aba47Smatthias.ringwald // find channel for source_cid, construct l2cap packet and send 404f62db1e3Smatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_source_cid(source_cid); 405fcadd0caSmatthias.ringwald if (channel) { 4061e6aba47Smatthias.ringwald // 0 - Connection handle : PB=10 : BC=00 4071e6aba47Smatthias.ringwald bt_store_16(acl_buffer, 0, channel->handle | (2 << 12) | (0 << 14)); 4081e6aba47Smatthias.ringwald // 2 - ACL length 4091e6aba47Smatthias.ringwald bt_store_16(acl_buffer, 2, len + 4); 4101e6aba47Smatthias.ringwald // 4 - L2CAP packet length 4111e6aba47Smatthias.ringwald bt_store_16(acl_buffer, 4, len + 0); 4121e6aba47Smatthias.ringwald // 6 - L2CAP channel DEST 4131e6aba47Smatthias.ringwald bt_store_16(acl_buffer, 6, channel->dest_cid); 4141e6aba47Smatthias.ringwald // 8 - data 4151e6aba47Smatthias.ringwald memcpy(&acl_buffer[8], data, len); 4161e6aba47Smatthias.ringwald // send 4171e6aba47Smatthias.ringwald hci_send_acl_packet(acl_buffer, len+8); 4181e6aba47Smatthias.ringwald } 4191e6aba47Smatthias.ringwald } 4201e6aba47Smatthias.ringwald 4219edc8742Smatthias.ringwald void l2cap_set_capture_connection(connection_t * connection){ 4229edc8742Smatthias.ringwald capture_connection = connection; 4239edc8742Smatthias.ringwald } 4241e6aba47Smatthias.ringwald 425