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" 41645658c9Smatthias.ringwald #include "hci.h" 422b3c6c9bSmatthias.ringwald #include "hci_dump.h" 4343625864Smatthias.ringwald 4443625864Smatthias.ringwald #include <stdarg.h> 4543625864Smatthias.ringwald #include <string.h> 4643625864Smatthias.ringwald 4743625864Smatthias.ringwald #include <stdio.h> 4843625864Smatthias.ringwald 496f60b3f4Smatthias.ringwald // size of HCI ACL + L2CAP Header for regular data packets 506f60b3f4Smatthias.ringwald #define COMPLETE_L2CAP_HEADER 8 516f60b3f4Smatthias.ringwald 52*00d93d79Smatthias.ringwald // offsets for L2CAP SIGNALING COMMANDS 53*00d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_CODE_OFFSET 0 54*00d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_SIGID_OFFSET 1 55*00d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET 2 56*00d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_DATA_OFFSET 4 57*00d93d79Smatthias.ringwald 580d5a78dbSmatthias.ringwald static void null_event_handler(connection_t * connection, uint8_t *packet, uint16_t size); 590d5a78dbSmatthias.ringwald static void null_data_handler(connection_t * connection, uint16_t local_cid, uint8_t *packet, uint16_t size); 60fcadd0caSmatthias.ringwald 611e6aba47Smatthias.ringwald static uint8_t * sig_buffer = NULL; 621e6aba47Smatthias.ringwald static linked_list_t l2cap_channels = NULL; 639d9bbc01Smatthias.ringwald static linked_list_t l2cap_services = NULL; 641e6aba47Smatthias.ringwald static uint8_t * acl_buffer = NULL; 650d5a78dbSmatthias.ringwald static void (*event_packet_handler) (connection_t * connection, uint8_t *packet, uint16_t size) = null_event_handler; 660d5a78dbSmatthias.ringwald static void (*data_packet_handler) (connection_t * connection, uint16_t local_cid, uint8_t *packet, uint16_t size) = null_data_handler; 679edc8742Smatthias.ringwald static connection_t * capture_connection = NULL; 681e6aba47Smatthias.ringwald 69169f8b28Smatthias.ringwald static uint8_t config_options[] = { 1, 2, 150, 0}; // mtu = 48 70169f8b28Smatthias.ringwald 711e6aba47Smatthias.ringwald void l2cap_init(){ 721e6aba47Smatthias.ringwald sig_buffer = malloc( 48 ); 731e6aba47Smatthias.ringwald acl_buffer = malloc( 255 + 8 ); 74fcadd0caSmatthias.ringwald 75fcadd0caSmatthias.ringwald // 76fcadd0caSmatthias.ringwald // register callbacks with HCI 77fcadd0caSmatthias.ringwald // 78fcadd0caSmatthias.ringwald hci_register_event_packet_handler(&l2cap_event_handler); 79fcadd0caSmatthias.ringwald hci_register_acl_packet_handler(&l2cap_acl_handler); 80fcadd0caSmatthias.ringwald } 81fcadd0caSmatthias.ringwald 82fcadd0caSmatthias.ringwald 83fcadd0caSmatthias.ringwald /** Register L2CAP packet handlers */ 840d5a78dbSmatthias.ringwald static void null_event_handler(connection_t * connection, uint8_t *packet, uint16_t size){ 85fcadd0caSmatthias.ringwald } 860d5a78dbSmatthias.ringwald static void null_data_handler(connection_t * connection, uint16_t local_cid, uint8_t *packet, uint16_t size){ 87fcadd0caSmatthias.ringwald } 880d5a78dbSmatthias.ringwald void l2cap_register_event_packet_handler(void (*handler)(connection_t * connection, uint8_t *packet, uint16_t size)){ 89fcadd0caSmatthias.ringwald event_packet_handler = handler; 90fcadd0caSmatthias.ringwald } 910d5a78dbSmatthias.ringwald void l2cap_register_data_packet_handler (void (*handler)(connection_t * connection, uint16_t local_cid, uint8_t *packet, uint16_t size)){ 92fcadd0caSmatthias.ringwald data_packet_handler = handler; 931e6aba47Smatthias.ringwald } 941e6aba47Smatthias.ringwald 950af41d30Smatthias.ringwald int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){ 96b35f641cSmatthias.ringwald // printf("l2cap_send_signaling_packet type %u\n", cmd); 970af41d30Smatthias.ringwald va_list argptr; 980af41d30Smatthias.ringwald va_start(argptr, identifier); 990af41d30Smatthias.ringwald uint16_t len = l2cap_create_signaling_internal(sig_buffer, handle, cmd, identifier, argptr); 1001e6aba47Smatthias.ringwald va_end(argptr); 1010af41d30Smatthias.ringwald return hci_send_acl_packet(sig_buffer, len); 1020af41d30Smatthias.ringwald } 1030af41d30Smatthias.ringwald 104b35f641cSmatthias.ringwald l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid){ 105f62db1e3Smatthias.ringwald linked_item_t *it; 106f62db1e3Smatthias.ringwald l2cap_channel_t * channel; 107f62db1e3Smatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 108f62db1e3Smatthias.ringwald channel = (l2cap_channel_t *) it; 109b35f641cSmatthias.ringwald if ( channel->local_cid == local_cid) { 110f62db1e3Smatthias.ringwald return channel; 111f62db1e3Smatthias.ringwald } 112f62db1e3Smatthias.ringwald } 113f62db1e3Smatthias.ringwald return NULL; 114f62db1e3Smatthias.ringwald } 115f62db1e3Smatthias.ringwald 1161e6aba47Smatthias.ringwald // open outgoing L2CAP channel 1171e6aba47Smatthias.ringwald void l2cap_create_channel_internal(connection_t * connection, bd_addr_t address, uint16_t psm){ 1181e6aba47Smatthias.ringwald 1191e6aba47Smatthias.ringwald // alloc structure 1201e6aba47Smatthias.ringwald l2cap_channel_t * chan = malloc(sizeof(l2cap_channel_t)); 1211e6aba47Smatthias.ringwald // TODO: emit error event 1221e6aba47Smatthias.ringwald if (!chan) return; 1231e6aba47Smatthias.ringwald 1241e6aba47Smatthias.ringwald // fill in 1251e6aba47Smatthias.ringwald BD_ADDR_COPY(chan->address, address); 1261e6aba47Smatthias.ringwald chan->psm = psm; 1271e6aba47Smatthias.ringwald chan->handle = 0; 1281e6aba47Smatthias.ringwald chan->connection = connection; 1291e6aba47Smatthias.ringwald 1301e6aba47Smatthias.ringwald // set initial state 1311e6aba47Smatthias.ringwald chan->state = L2CAP_STATE_CLOSED; 1321e6aba47Smatthias.ringwald chan->sig_id = L2CAP_SIG_ID_INVALID; 1331e6aba47Smatthias.ringwald 1341e6aba47Smatthias.ringwald // add to connections list 1351e6aba47Smatthias.ringwald linked_list_add(&l2cap_channels, (linked_item_t *) chan); 1361e6aba47Smatthias.ringwald 1371e6aba47Smatthias.ringwald // send connection request 1381e6aba47Smatthias.ringwald // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch 1391e6aba47Smatthias.ringwald hci_send_cmd(&hci_create_connection, address, 0x18, 0, 0, 0, 0); 14043625864Smatthias.ringwald } 14143625864Smatthias.ringwald 142b35f641cSmatthias.ringwald void l2cap_disconnect_internal(uint16_t local_cid, uint8_t reason){ 143b35f641cSmatthias.ringwald // find channel for local_cid 144b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 145f62db1e3Smatthias.ringwald if (channel) { 146f62db1e3Smatthias.ringwald channel->sig_id = l2cap_next_sig_id(); 147b35f641cSmatthias.ringwald l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->sig_id, channel->remote_cid, channel->local_cid); 148f62db1e3Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_DISCONNECT; 149f62db1e3Smatthias.ringwald } 15043625864Smatthias.ringwald } 1511e6aba47Smatthias.ringwald 152afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){ 1531e6aba47Smatthias.ringwald linked_item_t *it; 1541e6aba47Smatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 155b448a0e7Smatthias.ringwald l2cap_channel_t * channel = (l2cap_channel_t *) it; 156b448a0e7Smatthias.ringwald if ( ! BD_ADDR_CMP( channel->address, address) ){ 157b448a0e7Smatthias.ringwald if (channel->state == L2CAP_STATE_CLOSED) { 158afde0c52Smatthias.ringwald // failure, forward error code 159afde0c52Smatthias.ringwald l2cap_emit_channel_opened(channel, status); 160afde0c52Smatthias.ringwald // discard channel 161afde0c52Smatthias.ringwald linked_list_remove(&l2cap_channels, (linked_item_t *) channel); 162afde0c52Smatthias.ringwald free (channel); 163afde0c52Smatthias.ringwald } 164afde0c52Smatthias.ringwald } 165afde0c52Smatthias.ringwald } 166afde0c52Smatthias.ringwald } 167afde0c52Smatthias.ringwald 168afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){ 169afde0c52Smatthias.ringwald linked_item_t *it; 170afde0c52Smatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 171afde0c52Smatthias.ringwald l2cap_channel_t * channel = (l2cap_channel_t *) it; 172afde0c52Smatthias.ringwald if ( ! BD_ADDR_CMP( channel->address, address) ){ 173afde0c52Smatthias.ringwald if (channel->state == L2CAP_STATE_CLOSED) { 174b448a0e7Smatthias.ringwald // success, start l2cap handshake 175afde0c52Smatthias.ringwald channel->handle = handle; 176b448a0e7Smatthias.ringwald channel->sig_id = l2cap_next_sig_id(); 177b35f641cSmatthias.ringwald channel->local_cid = l2cap_next_local_cid(); 178b448a0e7Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONNECT_RSP; 179b35f641cSmatthias.ringwald l2cap_send_signaling_packet( channel->handle, CONNECTION_REQUEST, channel->sig_id, channel->psm, channel->local_cid); 180afde0c52Smatthias.ringwald } 181afde0c52Smatthias.ringwald } 182afde0c52Smatthias.ringwald } 183afde0c52Smatthias.ringwald } 184b448a0e7Smatthias.ringwald 185afde0c52Smatthias.ringwald void l2cap_event_handler( uint8_t *packet, uint16_t size ){ 186afde0c52Smatthias.ringwald 187afde0c52Smatthias.ringwald bd_addr_t address; 188afde0c52Smatthias.ringwald hci_con_handle_t handle; 189afde0c52Smatthias.ringwald 190afde0c52Smatthias.ringwald switch(packet[0]){ 191afde0c52Smatthias.ringwald 192afde0c52Smatthias.ringwald // handle connection complete events 193afde0c52Smatthias.ringwald case HCI_EVENT_CONNECTION_COMPLETE: 194afde0c52Smatthias.ringwald bt_flip_addr(address, &packet[5]); 195afde0c52Smatthias.ringwald if (packet[2] == 0){ 196afde0c52Smatthias.ringwald handle = READ_BT_16(packet, 3); 197afde0c52Smatthias.ringwald l2cap_handle_connection_success_for_addr(address, handle); 198afde0c52Smatthias.ringwald } else { 199afde0c52Smatthias.ringwald l2cap_handle_connection_failed_for_addr(address, packet[2]); 200afde0c52Smatthias.ringwald } 201afde0c52Smatthias.ringwald break; 202afde0c52Smatthias.ringwald 203afde0c52Smatthias.ringwald // handle successful create connection cancel command 204afde0c52Smatthias.ringwald case HCI_EVENT_COMMAND_COMPLETE: 205afde0c52Smatthias.ringwald if ( COMMAND_COMPLETE_EVENT(packet, hci_create_connection_cancel) ) { 206afde0c52Smatthias.ringwald if (packet[5] == 0){ 207afde0c52Smatthias.ringwald bt_flip_addr(address, &packet[6]); 208afde0c52Smatthias.ringwald // CONNECTION TERMINATED BY LOCAL HOST (0X16) 209afde0c52Smatthias.ringwald l2cap_handle_connection_failed_for_addr(address, 0x16); 21003cfbabcSmatthias.ringwald } 2111e6aba47Smatthias.ringwald } 212afde0c52Smatthias.ringwald break; 21327a923d0Smatthias.ringwald 2141e6aba47Smatthias.ringwald // handle disconnection complete events 215afde0c52Smatthias.ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 21627a923d0Smatthias.ringwald // send l2cap disconnect events for all channels on this handle 217afde0c52Smatthias.ringwald handle = READ_BT_16(packet, 3); 21827a923d0Smatthias.ringwald linked_item_t *it; 21927a923d0Smatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 22027a923d0Smatthias.ringwald l2cap_channel_t * channel = (l2cap_channel_t *) it; 22127a923d0Smatthias.ringwald if ( channel->handle == handle ){ 22227a923d0Smatthias.ringwald l2cap_finialize_channel_close(channel); 22327a923d0Smatthias.ringwald } 22427a923d0Smatthias.ringwald } 225afde0c52Smatthias.ringwald break; 226fcadd0caSmatthias.ringwald 227ee091cf1Smatthias.ringwald // HCI Connection Timeouts 228afde0c52Smatthias.ringwald case L2CAP_EVENT_TIMEOUT_CHECK: 229afde0c52Smatthias.ringwald if (!capture_connection){ 230ee091cf1Smatthias.ringwald hci_con_handle_t handle = READ_BT_16(packet, 2); 231ee091cf1Smatthias.ringwald linked_item_t *it; 232ee091cf1Smatthias.ringwald l2cap_channel_t * channel; 233ee091cf1Smatthias.ringwald int used = 0; 234ee091cf1Smatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 235ee091cf1Smatthias.ringwald channel = (l2cap_channel_t *) it; 236ee091cf1Smatthias.ringwald if (channel->handle == handle) { 237ee091cf1Smatthias.ringwald used = 1; 238ee091cf1Smatthias.ringwald } 239ee091cf1Smatthias.ringwald } 240ee091cf1Smatthias.ringwald if (!used) { 2419edc8742Smatthias.ringwald hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection 242ee091cf1Smatthias.ringwald } 243ee091cf1Smatthias.ringwald } 244afde0c52Smatthias.ringwald break; 245ee091cf1Smatthias.ringwald 246afde0c52Smatthias.ringwald default: 247afde0c52Smatthias.ringwald break; 248afde0c52Smatthias.ringwald } 249afde0c52Smatthias.ringwald 250afde0c52Smatthias.ringwald // pass on 2510d5a78dbSmatthias.ringwald (*event_packet_handler)(NULL, packet, size); 2521e6aba47Smatthias.ringwald } 2531e6aba47Smatthias.ringwald 254afde0c52Smatthias.ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){ 255b35f641cSmatthias.ringwald l2cap_send_signaling_packet( channel->handle, DISCONNECTION_RESPONSE, identifier, channel->remote_cid, channel->local_cid); 25684836b65Smatthias.ringwald l2cap_finialize_channel_close(channel); 25784836b65Smatthias.ringwald } 25884836b65Smatthias.ringwald 259b35f641cSmatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){ 260645658c9Smatthias.ringwald 261b35f641cSmatthias.ringwald // printf("l2cap_handle_connection_request for handle %u, psm %u cid %u\n", handle, psm, source_cid); 262645658c9Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 263645658c9Smatthias.ringwald if (!service) { 264645658c9Smatthias.ringwald // 0x0002 PSM not supported 265169f8b28Smatthias.ringwald // printf("l2cap_handle_connection_request no PSM for psm %u/n", psm); 266645658c9Smatthias.ringwald l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, 0x0002, 0); 267645658c9Smatthias.ringwald return; 268645658c9Smatthias.ringwald } 269645658c9Smatthias.ringwald 270645658c9Smatthias.ringwald hci_connection_t * hci_connection = connection_for_handle( handle ); 271645658c9Smatthias.ringwald if (!hci_connection) { 272169f8b28Smatthias.ringwald fprintf(stderr, "no hci_connection for handle %u\n", handle); 273645658c9Smatthias.ringwald // TODO: emit error 274645658c9Smatthias.ringwald return; 275645658c9Smatthias.ringwald } 276645658c9Smatthias.ringwald // alloc structure 277169f8b28Smatthias.ringwald // printf("l2cap_handle_connection_request register channel\n"); 278169f8b28Smatthias.ringwald l2cap_channel_t * channel = malloc(sizeof(l2cap_channel_t)); 279645658c9Smatthias.ringwald // TODO: emit error event 280169f8b28Smatthias.ringwald if (!channel) return; 281645658c9Smatthias.ringwald 282645658c9Smatthias.ringwald // fill in 283169f8b28Smatthias.ringwald BD_ADDR_COPY(channel->address, hci_connection->address); 284169f8b28Smatthias.ringwald channel->psm = psm; 285169f8b28Smatthias.ringwald channel->handle = handle; 286169f8b28Smatthias.ringwald channel->connection = service->connection; 287b35f641cSmatthias.ringwald channel->local_cid = l2cap_next_local_cid(); 288b35f641cSmatthias.ringwald channel->remote_cid = source_cid; 289645658c9Smatthias.ringwald 290645658c9Smatthias.ringwald // set initial state 291e405ae81Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT; 292e405ae81Smatthias.ringwald 293e405ae81Smatthias.ringwald // temp. store req sig id 294e405ae81Smatthias.ringwald channel->sig_id = sig_id; 295645658c9Smatthias.ringwald 296645658c9Smatthias.ringwald // add to connections list 297169f8b28Smatthias.ringwald linked_list_add(&l2cap_channels, (linked_item_t *) channel); 298645658c9Smatthias.ringwald 299e405ae81Smatthias.ringwald // emit incoming connection request 300e405ae81Smatthias.ringwald l2cap_emit_connection_request(channel); 301e405ae81Smatthias.ringwald } 302645658c9Smatthias.ringwald 303b35f641cSmatthias.ringwald void l2cap_accept_connection_internal(uint16_t local_cid){ 304b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 305e405ae81Smatthias.ringwald if (!channel) { 306b35f641cSmatthias.ringwald fprintf(stderr, "l2cap_accept_connection_internal called but local_cid 0x%x not found", local_cid); 307e405ae81Smatthias.ringwald return; 308e405ae81Smatthias.ringwald } 309e405ae81Smatthias.ringwald 310e405ae81Smatthias.ringwald // accept connection 311b35f641cSmatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->sig_id, channel->local_cid, channel->remote_cid, 0, 0); 312e405ae81Smatthias.ringwald 313e405ae81Smatthias.ringwald // set real sig and state and start config 314e405ae81Smatthias.ringwald channel->sig_id = l2cap_next_sig_id(); 315e405ae81Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ; 316b35f641cSmatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->sig_id, channel->remote_cid, 0, 4, &config_options); 317b35f641cSmatthias.ringwald 318b35f641cSmatthias.ringwald // printf("new state %u\n", channel->state); 319e405ae81Smatthias.ringwald } 320645658c9Smatthias.ringwald 321b35f641cSmatthias.ringwald void l2cap_decline_connection_internal(uint16_t local_cid, uint8_t reason){ 322b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid); 323e405ae81Smatthias.ringwald if (!channel) { 324b35f641cSmatthias.ringwald fprintf(stderr, "l2cap_decline_connection_internal called but local_cid 0x%x not found", local_cid,reason); 325e405ae81Smatthias.ringwald return; 326e405ae81Smatthias.ringwald } 327e405ae81Smatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->sig_id, 0, 0, reason, 0); 328e405ae81Smatthias.ringwald 329e405ae81Smatthias.ringwald // discard channel 330e405ae81Smatthias.ringwald linked_list_remove(&l2cap_channels, (linked_item_t *) channel); 331e405ae81Smatthias.ringwald free (channel); 332645658c9Smatthias.ringwald } 333645658c9Smatthias.ringwald 334*00d93d79Smatthias.ringwald void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){ 3351e6aba47Smatthias.ringwald 336*00d93d79Smatthias.ringwald uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 337*00d93d79Smatthias.ringwald uint8_t identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 338*00d93d79Smatthias.ringwald uint16_t len = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 33938e5900eSmatthias.ringwald uint16_t result = 0; 3401e6aba47Smatthias.ringwald 341b35f641cSmatthias.ringwald // printf("signaling handler code %u\n", code); 342b35f641cSmatthias.ringwald 3431e6aba47Smatthias.ringwald switch (channel->state) { 3441e6aba47Smatthias.ringwald 3451e6aba47Smatthias.ringwald case L2CAP_STATE_WAIT_CONNECT_RSP: 3461e6aba47Smatthias.ringwald switch (code){ 3471e6aba47Smatthias.ringwald case CONNECTION_RESPONSE: 348*00d93d79Smatthias.ringwald result = READ_BT_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4); 34938e5900eSmatthias.ringwald switch (result) { 35038e5900eSmatthias.ringwald case 0: 351169f8b28Smatthias.ringwald // successful connection 352*00d93d79Smatthias.ringwald channel->remote_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 3531e6aba47Smatthias.ringwald channel->sig_id = l2cap_next_sig_id(); 354b35f641cSmatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->sig_id, channel->remote_cid, 0, 4, &config_options); 3555a67bd4aSmatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ; 35638e5900eSmatthias.ringwald break; 35738e5900eSmatthias.ringwald case 1: 35838e5900eSmatthias.ringwald // connection pending. get some coffee 35938e5900eSmatthias.ringwald break; 36038e5900eSmatthias.ringwald default: 361f32b992eSmatthias.ringwald // map l2cap connection response result to BTstack status enumeration 36238e5900eSmatthias.ringwald l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result); 36338e5900eSmatthias.ringwald break; 3641e6aba47Smatthias.ringwald } 3651e6aba47Smatthias.ringwald break; 36638e5900eSmatthias.ringwald 36784836b65Smatthias.ringwald case DISCONNECTION_REQUEST: 36884836b65Smatthias.ringwald l2cap_handle_disconnect_request(channel, identifier); 36984836b65Smatthias.ringwald break; 37084836b65Smatthias.ringwald 37138e5900eSmatthias.ringwald default: 3721e6aba47Smatthias.ringwald //@TODO: implement other signaling packets 37338e5900eSmatthias.ringwald break; 3741e6aba47Smatthias.ringwald } 3751e6aba47Smatthias.ringwald break; 3761e6aba47Smatthias.ringwald 3775a67bd4aSmatthias.ringwald case L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ: 3781e6aba47Smatthias.ringwald switch (code) { 3791e6aba47Smatthias.ringwald case CONFIGURE_RESPONSE: 3801e6aba47Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONFIG_REQ; 3811e6aba47Smatthias.ringwald break; 3825a67bd4aSmatthias.ringwald case CONFIGURE_REQUEST: 3835a67bd4aSmatthias.ringwald // accept the other's configuration options 384*00d93d79Smatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, identifier, channel->remote_cid, 0, 0, len-4, &command[8]); 3855a67bd4aSmatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP; 3865a67bd4aSmatthias.ringwald break; 38784836b65Smatthias.ringwald case DISCONNECTION_REQUEST: 38884836b65Smatthias.ringwald l2cap_handle_disconnect_request(channel, identifier); 38984836b65Smatthias.ringwald break; 3905a67bd4aSmatthias.ringwald default: 3915a67bd4aSmatthias.ringwald //@TODO: implement other signaling packets 3925a67bd4aSmatthias.ringwald break; 3931e6aba47Smatthias.ringwald } 3941e6aba47Smatthias.ringwald break; 3951e6aba47Smatthias.ringwald 3961e6aba47Smatthias.ringwald case L2CAP_STATE_WAIT_CONFIG_REQ: 3971e6aba47Smatthias.ringwald switch (code) { 3981e6aba47Smatthias.ringwald case CONFIGURE_REQUEST: 3991e6aba47Smatthias.ringwald // accept the other's configuration options 400*00d93d79Smatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, identifier, channel->remote_cid, 0, 0, len-4, &command[8]); 4011e6aba47Smatthias.ringwald channel->state = L2CAP_STATE_OPEN; 40203cfbabcSmatthias.ringwald l2cap_emit_channel_opened(channel, 0); // success 403c8e4258aSmatthias.ringwald break; 40484836b65Smatthias.ringwald case DISCONNECTION_REQUEST: 40584836b65Smatthias.ringwald l2cap_handle_disconnect_request(channel, identifier); 40684836b65Smatthias.ringwald break; 4075a67bd4aSmatthias.ringwald default: 4085a67bd4aSmatthias.ringwald //@TODO: implement other signaling packets 4095a67bd4aSmatthias.ringwald break; 4105a67bd4aSmatthias.ringwald } 4115a67bd4aSmatthias.ringwald break; 4125a67bd4aSmatthias.ringwald 4135a67bd4aSmatthias.ringwald case L2CAP_STATE_WAIT_CONFIG_REQ_RSP: 4145a67bd4aSmatthias.ringwald switch (code) { 4155a67bd4aSmatthias.ringwald case CONFIGURE_RESPONSE: 4165a67bd4aSmatthias.ringwald channel->state = L2CAP_STATE_OPEN; 4175a67bd4aSmatthias.ringwald l2cap_emit_channel_opened(channel, 0); // success 4185a67bd4aSmatthias.ringwald break; 41984836b65Smatthias.ringwald case DISCONNECTION_REQUEST: 42084836b65Smatthias.ringwald l2cap_handle_disconnect_request(channel, identifier); 42184836b65Smatthias.ringwald break; 4225a67bd4aSmatthias.ringwald default: 4235a67bd4aSmatthias.ringwald //@TODO: implement other signaling packets 4245a67bd4aSmatthias.ringwald break; 425c8e4258aSmatthias.ringwald } 426c8e4258aSmatthias.ringwald break; 427f62db1e3Smatthias.ringwald 428f62db1e3Smatthias.ringwald case L2CAP_STATE_WAIT_DISCONNECT: 429f62db1e3Smatthias.ringwald switch (code) { 430f62db1e3Smatthias.ringwald case DISCONNECTION_RESPONSE: 43127a923d0Smatthias.ringwald l2cap_finialize_channel_close(channel); 43227a923d0Smatthias.ringwald break; 43384836b65Smatthias.ringwald case DISCONNECTION_REQUEST: 43484836b65Smatthias.ringwald l2cap_handle_disconnect_request(channel, identifier); 43584836b65Smatthias.ringwald break; 4365a67bd4aSmatthias.ringwald default: 4375a67bd4aSmatthias.ringwald //@TODO: implement other signaling packets 4385a67bd4aSmatthias.ringwald break; 43927a923d0Smatthias.ringwald } 44027a923d0Smatthias.ringwald break; 44184836b65Smatthias.ringwald 44284836b65Smatthias.ringwald case L2CAP_STATE_CLOSED: 44384836b65Smatthias.ringwald // @TODO handle incoming requests 44484836b65Smatthias.ringwald break; 44584836b65Smatthias.ringwald 44684836b65Smatthias.ringwald case L2CAP_STATE_OPEN: 44784836b65Smatthias.ringwald switch (code) { 44884836b65Smatthias.ringwald case DISCONNECTION_REQUEST: 44984836b65Smatthias.ringwald l2cap_handle_disconnect_request(channel, identifier); 45084836b65Smatthias.ringwald break; 4515a67bd4aSmatthias.ringwald default: 45284836b65Smatthias.ringwald //@TODO: implement other signaling packets, e.g. re-configure 45384836b65Smatthias.ringwald break; 45484836b65Smatthias.ringwald } 4555a67bd4aSmatthias.ringwald break; 45627a923d0Smatthias.ringwald } 457b35f641cSmatthias.ringwald // printf("new state %u\n", channel->state); 45827a923d0Smatthias.ringwald } 45927a923d0Smatthias.ringwald 460*00d93d79Smatthias.ringwald 461*00d93d79Smatthias.ringwald void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){ 462*00d93d79Smatthias.ringwald 463*00d93d79Smatthias.ringwald // get code, signalind identifier and command len 464*00d93d79Smatthias.ringwald uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 465*00d93d79Smatthias.ringwald uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 466*00d93d79Smatthias.ringwald uint16_t len = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 467*00d93d79Smatthias.ringwald 468*00d93d79Smatthias.ringwald // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST 469*00d93d79Smatthias.ringwald if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){ 470*00d93d79Smatthias.ringwald return; 471*00d93d79Smatthias.ringwald } 472*00d93d79Smatthias.ringwald 473*00d93d79Smatthias.ringwald // general commands without an assigned channel 474*00d93d79Smatthias.ringwald switch(code) { 475*00d93d79Smatthias.ringwald 476*00d93d79Smatthias.ringwald case CONNECTION_REQUEST: { 477*00d93d79Smatthias.ringwald uint16_t psm = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 478*00d93d79Smatthias.ringwald uint16_t source_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2); 479*00d93d79Smatthias.ringwald l2cap_handle_connection_request(handle, sig_id, psm, source_cid); 480*00d93d79Smatthias.ringwald break; 481*00d93d79Smatthias.ringwald } 482*00d93d79Smatthias.ringwald 483*00d93d79Smatthias.ringwald case ECHO_REQUEST: { 484*00d93d79Smatthias.ringwald // send back packet 485*00d93d79Smatthias.ringwald l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, len, &command[L2CAP_SIGNALING_COMMAND_DATA_OFFSET]); 486*00d93d79Smatthias.ringwald break; 487*00d93d79Smatthias.ringwald } 488*00d93d79Smatthias.ringwald 489*00d93d79Smatthias.ringwald case INFORMATION_REQUEST: { 490*00d93d79Smatthias.ringwald // we neither support connectionless L2CAP data nor support any flow control modes yet 491*00d93d79Smatthias.ringwald uint16_t infoType = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 492*00d93d79Smatthias.ringwald if (infoType == 2) { 493*00d93d79Smatthias.ringwald uint32_t features = 0; 494*00d93d79Smatthias.ringwald // extended features request supported, however no features present 495*00d93d79Smatthias.ringwald l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, 4, &features); 496*00d93d79Smatthias.ringwald } else { 497*00d93d79Smatthias.ringwald // all other types are not supported 498*00d93d79Smatthias.ringwald l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL); 499*00d93d79Smatthias.ringwald } 500*00d93d79Smatthias.ringwald break;; 501*00d93d79Smatthias.ringwald } 502*00d93d79Smatthias.ringwald 503*00d93d79Smatthias.ringwald default: 504*00d93d79Smatthias.ringwald break; 505*00d93d79Smatthias.ringwald } 506*00d93d79Smatthias.ringwald 507*00d93d79Smatthias.ringwald 508*00d93d79Smatthias.ringwald // Get potential destination CID 509*00d93d79Smatthias.ringwald uint16_t dest_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 510*00d93d79Smatthias.ringwald 511*00d93d79Smatthias.ringwald // Find channel for this sig_id and connection handle 512*00d93d79Smatthias.ringwald linked_item_t *it; 513*00d93d79Smatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 514*00d93d79Smatthias.ringwald l2cap_channel_t * channel = (l2cap_channel_t *) it; 515*00d93d79Smatthias.ringwald if (channel->handle == handle) { 516*00d93d79Smatthias.ringwald if (code & 1) { 517*00d93d79Smatthias.ringwald // match odd commands by previous signaling identifier 518*00d93d79Smatthias.ringwald if (channel->sig_id == sig_id) { 519*00d93d79Smatthias.ringwald l2cap_signaling_handler_channel(channel, command); 520*00d93d79Smatthias.ringwald } 521*00d93d79Smatthias.ringwald } else { 522*00d93d79Smatthias.ringwald // match even commands by local channel id 523*00d93d79Smatthias.ringwald if (channel->local_cid == dest_cid) { 524*00d93d79Smatthias.ringwald l2cap_signaling_handler_channel(channel, command); 525*00d93d79Smatthias.ringwald } 526*00d93d79Smatthias.ringwald } 527*00d93d79Smatthias.ringwald } 528*00d93d79Smatthias.ringwald } 529*00d93d79Smatthias.ringwald } 530*00d93d79Smatthias.ringwald 531*00d93d79Smatthias.ringwald void l2cap_acl_handler( uint8_t *packet, uint16_t size ){ 532*00d93d79Smatthias.ringwald 533*00d93d79Smatthias.ringwald // Capturing? 534*00d93d79Smatthias.ringwald if (capture_connection) { 535*00d93d79Smatthias.ringwald (*data_packet_handler)(capture_connection, 0, packet, size); 536*00d93d79Smatthias.ringwald return; 537*00d93d79Smatthias.ringwald } 538*00d93d79Smatthias.ringwald 539*00d93d79Smatthias.ringwald // Get Channel ID 540*00d93d79Smatthias.ringwald uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet); 541*00d93d79Smatthias.ringwald 542*00d93d79Smatthias.ringwald // Signaling Packet? 543*00d93d79Smatthias.ringwald if (channel_id == 1) { 544*00d93d79Smatthias.ringwald 545*00d93d79Smatthias.ringwald // Get Connection 546*00d93d79Smatthias.ringwald hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet); 547*00d93d79Smatthias.ringwald 548*00d93d79Smatthias.ringwald uint16_t command_offset = 8; 549*00d93d79Smatthias.ringwald while (command_offset < size) { 550*00d93d79Smatthias.ringwald 551*00d93d79Smatthias.ringwald // handle signaling commands 552*00d93d79Smatthias.ringwald l2cap_signaling_handler_dispatch(handle, &packet[command_offset]); 553*00d93d79Smatthias.ringwald 554*00d93d79Smatthias.ringwald // increment command_offset 555*00d93d79Smatthias.ringwald command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + READ_BT_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 556*00d93d79Smatthias.ringwald } 557*00d93d79Smatthias.ringwald return; 558*00d93d79Smatthias.ringwald } 559*00d93d79Smatthias.ringwald 560*00d93d79Smatthias.ringwald // Find channel for this channel_id and connection handle 561*00d93d79Smatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(channel_id); 562*00d93d79Smatthias.ringwald if (channel) { 563*00d93d79Smatthias.ringwald (*data_packet_handler)(channel->connection, channel_id, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 564*00d93d79Smatthias.ringwald } 565*00d93d79Smatthias.ringwald } 566*00d93d79Smatthias.ringwald 567*00d93d79Smatthias.ringwald 56827a923d0Smatthias.ringwald // finalize closed channel 56927a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t *channel){ 570f62db1e3Smatthias.ringwald channel->state = L2CAP_STATE_CLOSED; 571f62db1e3Smatthias.ringwald l2cap_emit_channel_closed(channel); 572f62db1e3Smatthias.ringwald 573f62db1e3Smatthias.ringwald // discard channel 574f62db1e3Smatthias.ringwald linked_list_remove(&l2cap_channels, (linked_item_t *) channel); 575f62db1e3Smatthias.ringwald free (channel); 576c8e4258aSmatthias.ringwald } 5771e6aba47Smatthias.ringwald 5789d9bbc01Smatthias.ringwald l2cap_service_t * l2cap_get_service(uint16_t psm){ 579c52bf64dSmatthias.ringwald linked_item_t *it; 5809d9bbc01Smatthias.ringwald 5819d9bbc01Smatthias.ringwald // close open channels 5829d9bbc01Smatthias.ringwald for (it = (linked_item_t *) l2cap_services; it ; it = it->next){ 5839d9bbc01Smatthias.ringwald l2cap_service_t * service = ((l2cap_service_t *) it); 5849d9bbc01Smatthias.ringwald if ( service->psm == psm){ 5859d9bbc01Smatthias.ringwald return service; 5869d9bbc01Smatthias.ringwald }; 5879d9bbc01Smatthias.ringwald } 5889d9bbc01Smatthias.ringwald return NULL; 5899d9bbc01Smatthias.ringwald } 5909d9bbc01Smatthias.ringwald 591116ee617Smatthias.ringwald void l2cap_register_service_internal(connection_t *connection, uint16_t psm, uint16_t mtu){ 5929d9bbc01Smatthias.ringwald // check for alread registered psm // TODO: emit error event 5939d9bbc01Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 5949d9bbc01Smatthias.ringwald if (service) return; 5959d9bbc01Smatthias.ringwald 5969d9bbc01Smatthias.ringwald // alloc structure // TODO: emit error event 5979d9bbc01Smatthias.ringwald service = malloc(sizeof(l2cap_service_t)); 5989d9bbc01Smatthias.ringwald if (!service) return; 5999d9bbc01Smatthias.ringwald 6009d9bbc01Smatthias.ringwald // fill in 6019d9bbc01Smatthias.ringwald service->psm = psm; 6029d9bbc01Smatthias.ringwald service->mtu = mtu; 6039d9bbc01Smatthias.ringwald service->connection = connection; 6049d9bbc01Smatthias.ringwald 6059d9bbc01Smatthias.ringwald // add to services list 6069d9bbc01Smatthias.ringwald linked_list_add(&l2cap_services, (linked_item_t *) service); 6079d9bbc01Smatthias.ringwald } 6089d9bbc01Smatthias.ringwald 609116ee617Smatthias.ringwald void l2cap_unregister_service_internal(connection_t *connection, uint16_t psm){ 6109d9bbc01Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 611037d6e48Smatthias.ringwald if (!service) return; 6129d9bbc01Smatthias.ringwald linked_list_remove(&l2cap_services, (linked_item_t *) service); 6139d9bbc01Smatthias.ringwald free(service); 6149d9bbc01Smatthias.ringwald } 6159d9bbc01Smatthias.ringwald 6169d9bbc01Smatthias.ringwald // 6179d9bbc01Smatthias.ringwald void l2cap_close_connection(connection_t *connection){ 6189d9bbc01Smatthias.ringwald linked_item_t *it; 6199d9bbc01Smatthias.ringwald 6209d9bbc01Smatthias.ringwald // close open channels 621c52bf64dSmatthias.ringwald l2cap_channel_t * channel; 622c52bf64dSmatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 623c52bf64dSmatthias.ringwald channel = (l2cap_channel_t *) it; 624c52bf64dSmatthias.ringwald if (channel->connection == connection) { 625c52bf64dSmatthias.ringwald channel->sig_id = l2cap_next_sig_id(); 626b35f641cSmatthias.ringwald l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->sig_id, channel->remote_cid, channel->local_cid); 627c52bf64dSmatthias.ringwald channel->state = L2CAP_STATE_WAIT_DISCONNECT; 628c52bf64dSmatthias.ringwald } 629c52bf64dSmatthias.ringwald } 6309d9bbc01Smatthias.ringwald 631645658c9Smatthias.ringwald // unregister services 632645658c9Smatthias.ringwald l2cap_service_t *service; 6338149d2c7Smatthias.ringwald for (it = (linked_item_t *) l2cap_services; it ; ){ 6348149d2c7Smatthias.ringwald service = (l2cap_service_t *) it->next; 635645658c9Smatthias.ringwald if (service->connection == connection){ 6368149d2c7Smatthias.ringwald it->next = service->item.next; 6378149d2c7Smatthias.ringwald free(service); 6388149d2c7Smatthias.ringwald } else { 6398149d2c7Smatthias.ringwald it = it->next; 640645658c9Smatthias.ringwald } 641645658c9Smatthias.ringwald } 642c52bf64dSmatthias.ringwald } 643c52bf64dSmatthias.ringwald 6441e6aba47Smatthias.ringwald // notify client 64503cfbabcSmatthias.ringwald void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) { 64603cfbabcSmatthias.ringwald uint8_t event[17]; 64780d52d6bSmatthias.ringwald event[0] = L2CAP_EVENT_CHANNEL_OPENED; 648c8e4258aSmatthias.ringwald event[1] = sizeof(event) - 2; 64903cfbabcSmatthias.ringwald event[2] = status; 65003cfbabcSmatthias.ringwald bt_flip_addr(&event[3], channel->address); 65103cfbabcSmatthias.ringwald bt_store_16(event, 9, channel->handle); 65203cfbabcSmatthias.ringwald bt_store_16(event, 11, channel->psm); 653b35f641cSmatthias.ringwald bt_store_16(event, 13, channel->local_cid); 654b35f641cSmatthias.ringwald bt_store_16(event, 15, channel->remote_cid); 6552289ec97Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 6560d5a78dbSmatthias.ringwald (*event_packet_handler)(channel->connection, event, sizeof(event)); 6571e6aba47Smatthias.ringwald } 6581e6aba47Smatthias.ringwald 659f62db1e3Smatthias.ringwald void l2cap_emit_channel_closed(l2cap_channel_t *channel) { 660f62db1e3Smatthias.ringwald uint8_t event[4]; 66180d52d6bSmatthias.ringwald event[0] = L2CAP_EVENT_CHANNEL_CLOSED; 662f62db1e3Smatthias.ringwald event[1] = sizeof(event) - 2; 663b35f641cSmatthias.ringwald bt_store_16(event, 2, channel->local_cid); 6642289ec97Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 6650d5a78dbSmatthias.ringwald (*event_packet_handler)(channel->connection, event, sizeof(event)); 666f62db1e3Smatthias.ringwald } 667f62db1e3Smatthias.ringwald 668e405ae81Smatthias.ringwald void l2cap_emit_connection_request(l2cap_channel_t *channel) { 669e405ae81Smatthias.ringwald uint8_t event[16]; 670e405ae81Smatthias.ringwald event[0] = L2CAP_EVENT_INCOMING_CONNECTION; 671e405ae81Smatthias.ringwald event[1] = sizeof(event) - 2; 672e405ae81Smatthias.ringwald bt_flip_addr(&event[2], channel->address); 673e405ae81Smatthias.ringwald bt_store_16(event, 8, channel->handle); 674e405ae81Smatthias.ringwald bt_store_16(event, 10, channel->psm); 675b35f641cSmatthias.ringwald bt_store_16(event, 12, channel->local_cid); 676b35f641cSmatthias.ringwald bt_store_16(event, 14, channel->remote_cid); 6772289ec97Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 6780d5a78dbSmatthias.ringwald (*event_packet_handler)(channel->connection, event, sizeof(event)); 679e405ae81Smatthias.ringwald } 6802289ec97Smatthias.ringwald 681b35f641cSmatthias.ringwald void l2cap_send_internal(uint16_t local_cid, uint8_t *data, uint16_t len){ 682b35f641cSmatthias.ringwald // find channel for local_cid, construct l2cap packet and send 683b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 684fcadd0caSmatthias.ringwald if (channel) { 6851e6aba47Smatthias.ringwald // 0 - Connection handle : PB=10 : BC=00 6861e6aba47Smatthias.ringwald bt_store_16(acl_buffer, 0, channel->handle | (2 << 12) | (0 << 14)); 6871e6aba47Smatthias.ringwald // 2 - ACL length 6881e6aba47Smatthias.ringwald bt_store_16(acl_buffer, 2, len + 4); 6891e6aba47Smatthias.ringwald // 4 - L2CAP packet length 6901e6aba47Smatthias.ringwald bt_store_16(acl_buffer, 4, len + 0); 6911e6aba47Smatthias.ringwald // 6 - L2CAP channel DEST 692b35f641cSmatthias.ringwald bt_store_16(acl_buffer, 6, channel->remote_cid); 6931e6aba47Smatthias.ringwald // 8 - data 6941e6aba47Smatthias.ringwald memcpy(&acl_buffer[8], data, len); 6951e6aba47Smatthias.ringwald // send 6961e6aba47Smatthias.ringwald hci_send_acl_packet(acl_buffer, len+8); 6971e6aba47Smatthias.ringwald } 6981e6aba47Smatthias.ringwald } 6991e6aba47Smatthias.ringwald 7009edc8742Smatthias.ringwald void l2cap_set_capture_connection(connection_t * connection){ 7019edc8742Smatthias.ringwald capture_connection = connection; 7029edc8742Smatthias.ringwald } 7031e6aba47Smatthias.ringwald 704b448a0e7Smatthias.ringwald