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 5200d93d79Smatthias.ringwald // offsets for L2CAP SIGNALING COMMANDS 5300d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_CODE_OFFSET 0 5400d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_SIGID_OFFSET 1 5500d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET 2 5600d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_DATA_OFFSET 4 5700d93d79Smatthias.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; 129b5a49eb1Smatthias.ringwald chan->packet_handler = NULL; 1301e6aba47Smatthias.ringwald 1311e6aba47Smatthias.ringwald // set initial state 1321e6aba47Smatthias.ringwald chan->state = L2CAP_STATE_CLOSED; 1331e6aba47Smatthias.ringwald chan->sig_id = L2CAP_SIG_ID_INVALID; 1341e6aba47Smatthias.ringwald 1351e6aba47Smatthias.ringwald // add to connections list 1361e6aba47Smatthias.ringwald linked_list_add(&l2cap_channels, (linked_item_t *) chan); 1371e6aba47Smatthias.ringwald 1381e6aba47Smatthias.ringwald // send connection request 1391e6aba47Smatthias.ringwald // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch 1401e6aba47Smatthias.ringwald hci_send_cmd(&hci_create_connection, address, 0x18, 0, 0, 0, 0); 14143625864Smatthias.ringwald } 14243625864Smatthias.ringwald 143b35f641cSmatthias.ringwald void l2cap_disconnect_internal(uint16_t local_cid, uint8_t reason){ 144b35f641cSmatthias.ringwald // find channel for local_cid 145b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 146f62db1e3Smatthias.ringwald if (channel) { 147f62db1e3Smatthias.ringwald channel->sig_id = l2cap_next_sig_id(); 148b35f641cSmatthias.ringwald l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->sig_id, channel->remote_cid, channel->local_cid); 149f62db1e3Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_DISCONNECT; 150f62db1e3Smatthias.ringwald } 15143625864Smatthias.ringwald } 1521e6aba47Smatthias.ringwald 153afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){ 1541e6aba47Smatthias.ringwald linked_item_t *it; 1551e6aba47Smatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 156b448a0e7Smatthias.ringwald l2cap_channel_t * channel = (l2cap_channel_t *) it; 157b448a0e7Smatthias.ringwald if ( ! BD_ADDR_CMP( channel->address, address) ){ 158b448a0e7Smatthias.ringwald if (channel->state == L2CAP_STATE_CLOSED) { 159afde0c52Smatthias.ringwald // failure, forward error code 160afde0c52Smatthias.ringwald l2cap_emit_channel_opened(channel, status); 161afde0c52Smatthias.ringwald // discard channel 162afde0c52Smatthias.ringwald linked_list_remove(&l2cap_channels, (linked_item_t *) channel); 163afde0c52Smatthias.ringwald free (channel); 164afde0c52Smatthias.ringwald } 165afde0c52Smatthias.ringwald } 166afde0c52Smatthias.ringwald } 167afde0c52Smatthias.ringwald } 168afde0c52Smatthias.ringwald 169afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){ 170afde0c52Smatthias.ringwald linked_item_t *it; 171afde0c52Smatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 172afde0c52Smatthias.ringwald l2cap_channel_t * channel = (l2cap_channel_t *) it; 173afde0c52Smatthias.ringwald if ( ! BD_ADDR_CMP( channel->address, address) ){ 174afde0c52Smatthias.ringwald if (channel->state == L2CAP_STATE_CLOSED) { 175b448a0e7Smatthias.ringwald // success, start l2cap handshake 176afde0c52Smatthias.ringwald channel->handle = handle; 177b448a0e7Smatthias.ringwald channel->sig_id = l2cap_next_sig_id(); 178b35f641cSmatthias.ringwald channel->local_cid = l2cap_next_local_cid(); 179b448a0e7Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONNECT_RSP; 180b35f641cSmatthias.ringwald l2cap_send_signaling_packet( channel->handle, CONNECTION_REQUEST, channel->sig_id, channel->psm, channel->local_cid); 181afde0c52Smatthias.ringwald } 182afde0c52Smatthias.ringwald } 183afde0c52Smatthias.ringwald } 184afde0c52Smatthias.ringwald } 185b448a0e7Smatthias.ringwald 186afde0c52Smatthias.ringwald void l2cap_event_handler( uint8_t *packet, uint16_t size ){ 187afde0c52Smatthias.ringwald 188afde0c52Smatthias.ringwald bd_addr_t address; 189afde0c52Smatthias.ringwald hci_con_handle_t handle; 190afde0c52Smatthias.ringwald 191afde0c52Smatthias.ringwald switch(packet[0]){ 192afde0c52Smatthias.ringwald 193afde0c52Smatthias.ringwald // handle connection complete events 194afde0c52Smatthias.ringwald case HCI_EVENT_CONNECTION_COMPLETE: 195afde0c52Smatthias.ringwald bt_flip_addr(address, &packet[5]); 196afde0c52Smatthias.ringwald if (packet[2] == 0){ 197afde0c52Smatthias.ringwald handle = READ_BT_16(packet, 3); 198afde0c52Smatthias.ringwald l2cap_handle_connection_success_for_addr(address, handle); 199afde0c52Smatthias.ringwald } else { 200afde0c52Smatthias.ringwald l2cap_handle_connection_failed_for_addr(address, packet[2]); 201afde0c52Smatthias.ringwald } 202afde0c52Smatthias.ringwald break; 203afde0c52Smatthias.ringwald 204afde0c52Smatthias.ringwald // handle successful create connection cancel command 205afde0c52Smatthias.ringwald case HCI_EVENT_COMMAND_COMPLETE: 206afde0c52Smatthias.ringwald if ( COMMAND_COMPLETE_EVENT(packet, hci_create_connection_cancel) ) { 207afde0c52Smatthias.ringwald if (packet[5] == 0){ 208afde0c52Smatthias.ringwald bt_flip_addr(address, &packet[6]); 209afde0c52Smatthias.ringwald // CONNECTION TERMINATED BY LOCAL HOST (0X16) 210afde0c52Smatthias.ringwald l2cap_handle_connection_failed_for_addr(address, 0x16); 21103cfbabcSmatthias.ringwald } 2121e6aba47Smatthias.ringwald } 213afde0c52Smatthias.ringwald break; 21427a923d0Smatthias.ringwald 2151e6aba47Smatthias.ringwald // handle disconnection complete events 216afde0c52Smatthias.ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 21727a923d0Smatthias.ringwald // send l2cap disconnect events for all channels on this handle 218afde0c52Smatthias.ringwald handle = READ_BT_16(packet, 3); 21927a923d0Smatthias.ringwald linked_item_t *it; 22027a923d0Smatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 22127a923d0Smatthias.ringwald l2cap_channel_t * channel = (l2cap_channel_t *) it; 22227a923d0Smatthias.ringwald if ( channel->handle == handle ){ 22327a923d0Smatthias.ringwald l2cap_finialize_channel_close(channel); 22427a923d0Smatthias.ringwald } 22527a923d0Smatthias.ringwald } 226afde0c52Smatthias.ringwald break; 227fcadd0caSmatthias.ringwald 228ee091cf1Smatthias.ringwald // HCI Connection Timeouts 229afde0c52Smatthias.ringwald case L2CAP_EVENT_TIMEOUT_CHECK: 230afde0c52Smatthias.ringwald if (!capture_connection){ 231ee091cf1Smatthias.ringwald hci_con_handle_t handle = READ_BT_16(packet, 2); 232ee091cf1Smatthias.ringwald linked_item_t *it; 233ee091cf1Smatthias.ringwald l2cap_channel_t * channel; 234ee091cf1Smatthias.ringwald int used = 0; 235ee091cf1Smatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 236ee091cf1Smatthias.ringwald channel = (l2cap_channel_t *) it; 237ee091cf1Smatthias.ringwald if (channel->handle == handle) { 238ee091cf1Smatthias.ringwald used = 1; 239ee091cf1Smatthias.ringwald } 240ee091cf1Smatthias.ringwald } 241ee091cf1Smatthias.ringwald if (!used) { 2429edc8742Smatthias.ringwald hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection 243ee091cf1Smatthias.ringwald } 244ee091cf1Smatthias.ringwald } 245afde0c52Smatthias.ringwald break; 246ee091cf1Smatthias.ringwald 247afde0c52Smatthias.ringwald default: 248afde0c52Smatthias.ringwald break; 249afde0c52Smatthias.ringwald } 250afde0c52Smatthias.ringwald 251afde0c52Smatthias.ringwald // pass on 2520d5a78dbSmatthias.ringwald (*event_packet_handler)(NULL, packet, size); 2531e6aba47Smatthias.ringwald } 2541e6aba47Smatthias.ringwald 255afde0c52Smatthias.ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){ 256b35f641cSmatthias.ringwald l2cap_send_signaling_packet( channel->handle, DISCONNECTION_RESPONSE, identifier, channel->remote_cid, channel->local_cid); 25784836b65Smatthias.ringwald l2cap_finialize_channel_close(channel); 25884836b65Smatthias.ringwald } 25984836b65Smatthias.ringwald 260b35f641cSmatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){ 261645658c9Smatthias.ringwald 262b35f641cSmatthias.ringwald // printf("l2cap_handle_connection_request for handle %u, psm %u cid %u\n", handle, psm, source_cid); 263645658c9Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 264645658c9Smatthias.ringwald if (!service) { 265645658c9Smatthias.ringwald // 0x0002 PSM not supported 266169f8b28Smatthias.ringwald // printf("l2cap_handle_connection_request no PSM for psm %u/n", psm); 267645658c9Smatthias.ringwald l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, 0x0002, 0); 268645658c9Smatthias.ringwald return; 269645658c9Smatthias.ringwald } 270645658c9Smatthias.ringwald 271645658c9Smatthias.ringwald hci_connection_t * hci_connection = connection_for_handle( handle ); 272645658c9Smatthias.ringwald if (!hci_connection) { 273169f8b28Smatthias.ringwald fprintf(stderr, "no hci_connection for handle %u\n", handle); 274645658c9Smatthias.ringwald // TODO: emit error 275645658c9Smatthias.ringwald return; 276645658c9Smatthias.ringwald } 277645658c9Smatthias.ringwald // alloc structure 278169f8b28Smatthias.ringwald // printf("l2cap_handle_connection_request register channel\n"); 279169f8b28Smatthias.ringwald l2cap_channel_t * channel = malloc(sizeof(l2cap_channel_t)); 280645658c9Smatthias.ringwald // TODO: emit error event 281169f8b28Smatthias.ringwald if (!channel) return; 282645658c9Smatthias.ringwald 283645658c9Smatthias.ringwald // fill in 284169f8b28Smatthias.ringwald BD_ADDR_COPY(channel->address, hci_connection->address); 285169f8b28Smatthias.ringwald channel->psm = psm; 286169f8b28Smatthias.ringwald channel->handle = handle; 287169f8b28Smatthias.ringwald channel->connection = service->connection; 288b35f641cSmatthias.ringwald channel->local_cid = l2cap_next_local_cid(); 289b35f641cSmatthias.ringwald channel->remote_cid = source_cid; 290b5a49eb1Smatthias.ringwald channel->packet_handler = NULL; 291645658c9Smatthias.ringwald 292645658c9Smatthias.ringwald // set initial state 293e405ae81Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT; 294e405ae81Smatthias.ringwald 295e405ae81Smatthias.ringwald // temp. store req sig id 296e405ae81Smatthias.ringwald channel->sig_id = sig_id; 297645658c9Smatthias.ringwald 298645658c9Smatthias.ringwald // add to connections list 299169f8b28Smatthias.ringwald linked_list_add(&l2cap_channels, (linked_item_t *) channel); 300645658c9Smatthias.ringwald 301e405ae81Smatthias.ringwald // emit incoming connection request 302e405ae81Smatthias.ringwald l2cap_emit_connection_request(channel); 303e405ae81Smatthias.ringwald } 304645658c9Smatthias.ringwald 305b35f641cSmatthias.ringwald void l2cap_accept_connection_internal(uint16_t local_cid){ 306b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 307e405ae81Smatthias.ringwald if (!channel) { 308b35f641cSmatthias.ringwald fprintf(stderr, "l2cap_accept_connection_internal called but local_cid 0x%x not found", local_cid); 309e405ae81Smatthias.ringwald return; 310e405ae81Smatthias.ringwald } 311e405ae81Smatthias.ringwald 312e405ae81Smatthias.ringwald // accept connection 313b35f641cSmatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->sig_id, channel->local_cid, channel->remote_cid, 0, 0); 314e405ae81Smatthias.ringwald 315e405ae81Smatthias.ringwald // set real sig and state and start config 316e405ae81Smatthias.ringwald channel->sig_id = l2cap_next_sig_id(); 317e405ae81Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ; 318b35f641cSmatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->sig_id, channel->remote_cid, 0, 4, &config_options); 319b35f641cSmatthias.ringwald 320b35f641cSmatthias.ringwald // printf("new state %u\n", channel->state); 321e405ae81Smatthias.ringwald } 322645658c9Smatthias.ringwald 323b35f641cSmatthias.ringwald void l2cap_decline_connection_internal(uint16_t local_cid, uint8_t reason){ 324b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid); 325e405ae81Smatthias.ringwald if (!channel) { 326b35f641cSmatthias.ringwald fprintf(stderr, "l2cap_decline_connection_internal called but local_cid 0x%x not found", local_cid,reason); 327e405ae81Smatthias.ringwald return; 328e405ae81Smatthias.ringwald } 329e405ae81Smatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->sig_id, 0, 0, reason, 0); 330e405ae81Smatthias.ringwald 331e405ae81Smatthias.ringwald // discard channel 332e405ae81Smatthias.ringwald linked_list_remove(&l2cap_channels, (linked_item_t *) channel); 333e405ae81Smatthias.ringwald free (channel); 334645658c9Smatthias.ringwald } 335645658c9Smatthias.ringwald 33600d93d79Smatthias.ringwald void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){ 3371e6aba47Smatthias.ringwald 33800d93d79Smatthias.ringwald uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 33900d93d79Smatthias.ringwald uint8_t identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 34000d93d79Smatthias.ringwald uint16_t len = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 34138e5900eSmatthias.ringwald uint16_t result = 0; 3421e6aba47Smatthias.ringwald 343b35f641cSmatthias.ringwald // printf("signaling handler code %u\n", code); 344b35f641cSmatthias.ringwald 3451e6aba47Smatthias.ringwald switch (channel->state) { 3461e6aba47Smatthias.ringwald 3471e6aba47Smatthias.ringwald case L2CAP_STATE_WAIT_CONNECT_RSP: 3481e6aba47Smatthias.ringwald switch (code){ 3491e6aba47Smatthias.ringwald case CONNECTION_RESPONSE: 35000d93d79Smatthias.ringwald result = READ_BT_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4); 35138e5900eSmatthias.ringwald switch (result) { 35238e5900eSmatthias.ringwald case 0: 353169f8b28Smatthias.ringwald // successful connection 35400d93d79Smatthias.ringwald channel->remote_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 3551e6aba47Smatthias.ringwald channel->sig_id = l2cap_next_sig_id(); 356b35f641cSmatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->sig_id, channel->remote_cid, 0, 4, &config_options); 3575a67bd4aSmatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ; 35838e5900eSmatthias.ringwald break; 35938e5900eSmatthias.ringwald case 1: 36038e5900eSmatthias.ringwald // connection pending. get some coffee 36138e5900eSmatthias.ringwald break; 36238e5900eSmatthias.ringwald default: 363f32b992eSmatthias.ringwald // map l2cap connection response result to BTstack status enumeration 36438e5900eSmatthias.ringwald l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result); 36538e5900eSmatthias.ringwald break; 3661e6aba47Smatthias.ringwald } 3671e6aba47Smatthias.ringwald break; 36838e5900eSmatthias.ringwald 36984836b65Smatthias.ringwald case DISCONNECTION_REQUEST: 37084836b65Smatthias.ringwald l2cap_handle_disconnect_request(channel, identifier); 37184836b65Smatthias.ringwald break; 37284836b65Smatthias.ringwald 37338e5900eSmatthias.ringwald default: 3741e6aba47Smatthias.ringwald //@TODO: implement other signaling packets 37538e5900eSmatthias.ringwald break; 3761e6aba47Smatthias.ringwald } 3771e6aba47Smatthias.ringwald break; 3781e6aba47Smatthias.ringwald 3795a67bd4aSmatthias.ringwald case L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ: 3801e6aba47Smatthias.ringwald switch (code) { 3811e6aba47Smatthias.ringwald case CONFIGURE_RESPONSE: 3821e6aba47Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONFIG_REQ; 3831e6aba47Smatthias.ringwald break; 3845a67bd4aSmatthias.ringwald case CONFIGURE_REQUEST: 3855a67bd4aSmatthias.ringwald // accept the other's configuration options 38600d93d79Smatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, identifier, channel->remote_cid, 0, 0, len-4, &command[8]); 3875a67bd4aSmatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP; 3885a67bd4aSmatthias.ringwald break; 38984836b65Smatthias.ringwald case DISCONNECTION_REQUEST: 39084836b65Smatthias.ringwald l2cap_handle_disconnect_request(channel, identifier); 39184836b65Smatthias.ringwald break; 3925a67bd4aSmatthias.ringwald default: 3935a67bd4aSmatthias.ringwald //@TODO: implement other signaling packets 3945a67bd4aSmatthias.ringwald break; 3951e6aba47Smatthias.ringwald } 3961e6aba47Smatthias.ringwald break; 3971e6aba47Smatthias.ringwald 3981e6aba47Smatthias.ringwald case L2CAP_STATE_WAIT_CONFIG_REQ: 3991e6aba47Smatthias.ringwald switch (code) { 4001e6aba47Smatthias.ringwald case CONFIGURE_REQUEST: 4011e6aba47Smatthias.ringwald // accept the other's configuration options 40200d93d79Smatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, identifier, channel->remote_cid, 0, 0, len-4, &command[8]); 4031e6aba47Smatthias.ringwald channel->state = L2CAP_STATE_OPEN; 40403cfbabcSmatthias.ringwald l2cap_emit_channel_opened(channel, 0); // success 405c8e4258aSmatthias.ringwald break; 40684836b65Smatthias.ringwald case DISCONNECTION_REQUEST: 40784836b65Smatthias.ringwald l2cap_handle_disconnect_request(channel, identifier); 40884836b65Smatthias.ringwald break; 4095a67bd4aSmatthias.ringwald default: 4105a67bd4aSmatthias.ringwald //@TODO: implement other signaling packets 4115a67bd4aSmatthias.ringwald break; 4125a67bd4aSmatthias.ringwald } 4135a67bd4aSmatthias.ringwald break; 4145a67bd4aSmatthias.ringwald 4155a67bd4aSmatthias.ringwald case L2CAP_STATE_WAIT_CONFIG_REQ_RSP: 4165a67bd4aSmatthias.ringwald switch (code) { 4175a67bd4aSmatthias.ringwald case CONFIGURE_RESPONSE: 4185a67bd4aSmatthias.ringwald channel->state = L2CAP_STATE_OPEN; 4195a67bd4aSmatthias.ringwald l2cap_emit_channel_opened(channel, 0); // success 4205a67bd4aSmatthias.ringwald break; 42184836b65Smatthias.ringwald case DISCONNECTION_REQUEST: 42284836b65Smatthias.ringwald l2cap_handle_disconnect_request(channel, identifier); 42384836b65Smatthias.ringwald break; 4245a67bd4aSmatthias.ringwald default: 4255a67bd4aSmatthias.ringwald //@TODO: implement other signaling packets 4265a67bd4aSmatthias.ringwald break; 427c8e4258aSmatthias.ringwald } 428c8e4258aSmatthias.ringwald break; 429f62db1e3Smatthias.ringwald 430f62db1e3Smatthias.ringwald case L2CAP_STATE_WAIT_DISCONNECT: 431f62db1e3Smatthias.ringwald switch (code) { 432f62db1e3Smatthias.ringwald case DISCONNECTION_RESPONSE: 43327a923d0Smatthias.ringwald l2cap_finialize_channel_close(channel); 43427a923d0Smatthias.ringwald break; 43584836b65Smatthias.ringwald case DISCONNECTION_REQUEST: 43684836b65Smatthias.ringwald l2cap_handle_disconnect_request(channel, identifier); 43784836b65Smatthias.ringwald break; 4385a67bd4aSmatthias.ringwald default: 4395a67bd4aSmatthias.ringwald //@TODO: implement other signaling packets 4405a67bd4aSmatthias.ringwald break; 44127a923d0Smatthias.ringwald } 44227a923d0Smatthias.ringwald break; 44384836b65Smatthias.ringwald 44484836b65Smatthias.ringwald case L2CAP_STATE_CLOSED: 44584836b65Smatthias.ringwald // @TODO handle incoming requests 44684836b65Smatthias.ringwald break; 44784836b65Smatthias.ringwald 44884836b65Smatthias.ringwald case L2CAP_STATE_OPEN: 44984836b65Smatthias.ringwald switch (code) { 45084836b65Smatthias.ringwald case DISCONNECTION_REQUEST: 45184836b65Smatthias.ringwald l2cap_handle_disconnect_request(channel, identifier); 45284836b65Smatthias.ringwald break; 4535a67bd4aSmatthias.ringwald default: 45484836b65Smatthias.ringwald //@TODO: implement other signaling packets, e.g. re-configure 45584836b65Smatthias.ringwald break; 45684836b65Smatthias.ringwald } 4575a67bd4aSmatthias.ringwald break; 45827a923d0Smatthias.ringwald } 459b35f641cSmatthias.ringwald // printf("new state %u\n", channel->state); 46027a923d0Smatthias.ringwald } 46127a923d0Smatthias.ringwald 46200d93d79Smatthias.ringwald 46300d93d79Smatthias.ringwald void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){ 46400d93d79Smatthias.ringwald 46500d93d79Smatthias.ringwald // get code, signalind identifier and command len 46600d93d79Smatthias.ringwald uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 46700d93d79Smatthias.ringwald uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 46800d93d79Smatthias.ringwald uint16_t len = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 46900d93d79Smatthias.ringwald 47000d93d79Smatthias.ringwald // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST 47100d93d79Smatthias.ringwald if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){ 47200d93d79Smatthias.ringwald return; 47300d93d79Smatthias.ringwald } 47400d93d79Smatthias.ringwald 47500d93d79Smatthias.ringwald // general commands without an assigned channel 47600d93d79Smatthias.ringwald switch(code) { 47700d93d79Smatthias.ringwald 47800d93d79Smatthias.ringwald case CONNECTION_REQUEST: { 47900d93d79Smatthias.ringwald uint16_t psm = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 48000d93d79Smatthias.ringwald uint16_t source_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2); 48100d93d79Smatthias.ringwald l2cap_handle_connection_request(handle, sig_id, psm, source_cid); 48200d93d79Smatthias.ringwald break; 48300d93d79Smatthias.ringwald } 48400d93d79Smatthias.ringwald 48500d93d79Smatthias.ringwald case ECHO_REQUEST: { 48600d93d79Smatthias.ringwald // send back packet 48700d93d79Smatthias.ringwald l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, len, &command[L2CAP_SIGNALING_COMMAND_DATA_OFFSET]); 48800d93d79Smatthias.ringwald break; 48900d93d79Smatthias.ringwald } 49000d93d79Smatthias.ringwald 49100d93d79Smatthias.ringwald case INFORMATION_REQUEST: { 49200d93d79Smatthias.ringwald // we neither support connectionless L2CAP data nor support any flow control modes yet 49300d93d79Smatthias.ringwald uint16_t infoType = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 49400d93d79Smatthias.ringwald if (infoType == 2) { 49500d93d79Smatthias.ringwald uint32_t features = 0; 49600d93d79Smatthias.ringwald // extended features request supported, however no features present 49700d93d79Smatthias.ringwald l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, 4, &features); 49800d93d79Smatthias.ringwald } else { 49900d93d79Smatthias.ringwald // all other types are not supported 50000d93d79Smatthias.ringwald l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL); 50100d93d79Smatthias.ringwald } 50200d93d79Smatthias.ringwald break;; 50300d93d79Smatthias.ringwald } 50400d93d79Smatthias.ringwald 50500d93d79Smatthias.ringwald default: 50600d93d79Smatthias.ringwald break; 50700d93d79Smatthias.ringwald } 50800d93d79Smatthias.ringwald 50900d93d79Smatthias.ringwald 51000d93d79Smatthias.ringwald // Get potential destination CID 51100d93d79Smatthias.ringwald uint16_t dest_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 51200d93d79Smatthias.ringwald 51300d93d79Smatthias.ringwald // Find channel for this sig_id and connection handle 51400d93d79Smatthias.ringwald linked_item_t *it; 51500d93d79Smatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 51600d93d79Smatthias.ringwald l2cap_channel_t * channel = (l2cap_channel_t *) it; 51700d93d79Smatthias.ringwald if (channel->handle == handle) { 51800d93d79Smatthias.ringwald if (code & 1) { 51900d93d79Smatthias.ringwald // match odd commands by previous signaling identifier 52000d93d79Smatthias.ringwald if (channel->sig_id == sig_id) { 52100d93d79Smatthias.ringwald l2cap_signaling_handler_channel(channel, command); 52200d93d79Smatthias.ringwald } 52300d93d79Smatthias.ringwald } else { 52400d93d79Smatthias.ringwald // match even commands by local channel id 52500d93d79Smatthias.ringwald if (channel->local_cid == dest_cid) { 52600d93d79Smatthias.ringwald l2cap_signaling_handler_channel(channel, command); 52700d93d79Smatthias.ringwald } 52800d93d79Smatthias.ringwald } 52900d93d79Smatthias.ringwald } 53000d93d79Smatthias.ringwald } 53100d93d79Smatthias.ringwald } 53200d93d79Smatthias.ringwald 53300d93d79Smatthias.ringwald void l2cap_acl_handler( uint8_t *packet, uint16_t size ){ 53400d93d79Smatthias.ringwald 53500d93d79Smatthias.ringwald // Capturing? 53600d93d79Smatthias.ringwald if (capture_connection) { 53700d93d79Smatthias.ringwald (*data_packet_handler)(capture_connection, 0, packet, size); 53800d93d79Smatthias.ringwald return; 53900d93d79Smatthias.ringwald } 54000d93d79Smatthias.ringwald 54100d93d79Smatthias.ringwald // Get Channel ID 54200d93d79Smatthias.ringwald uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet); 54300d93d79Smatthias.ringwald 54400d93d79Smatthias.ringwald // Signaling Packet? 54500d93d79Smatthias.ringwald if (channel_id == 1) { 54600d93d79Smatthias.ringwald 54700d93d79Smatthias.ringwald // Get Connection 54800d93d79Smatthias.ringwald hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet); 54900d93d79Smatthias.ringwald 55000d93d79Smatthias.ringwald uint16_t command_offset = 8; 55100d93d79Smatthias.ringwald while (command_offset < size) { 55200d93d79Smatthias.ringwald 55300d93d79Smatthias.ringwald // handle signaling commands 55400d93d79Smatthias.ringwald l2cap_signaling_handler_dispatch(handle, &packet[command_offset]); 55500d93d79Smatthias.ringwald 55600d93d79Smatthias.ringwald // increment command_offset 55700d93d79Smatthias.ringwald command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + READ_BT_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 55800d93d79Smatthias.ringwald } 55900d93d79Smatthias.ringwald return; 56000d93d79Smatthias.ringwald } 56100d93d79Smatthias.ringwald 56200d93d79Smatthias.ringwald // Find channel for this channel_id and connection handle 56300d93d79Smatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(channel_id); 56400d93d79Smatthias.ringwald if (channel) { 56500d93d79Smatthias.ringwald (*data_packet_handler)(channel->connection, channel_id, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 56600d93d79Smatthias.ringwald } 56700d93d79Smatthias.ringwald } 56800d93d79Smatthias.ringwald 56900d93d79Smatthias.ringwald 57027a923d0Smatthias.ringwald // finalize closed channel 57127a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t *channel){ 572f62db1e3Smatthias.ringwald channel->state = L2CAP_STATE_CLOSED; 573f62db1e3Smatthias.ringwald l2cap_emit_channel_closed(channel); 574f62db1e3Smatthias.ringwald 575f62db1e3Smatthias.ringwald // discard channel 576f62db1e3Smatthias.ringwald linked_list_remove(&l2cap_channels, (linked_item_t *) channel); 577f62db1e3Smatthias.ringwald free (channel); 578c8e4258aSmatthias.ringwald } 5791e6aba47Smatthias.ringwald 5809d9bbc01Smatthias.ringwald l2cap_service_t * l2cap_get_service(uint16_t psm){ 581c52bf64dSmatthias.ringwald linked_item_t *it; 5829d9bbc01Smatthias.ringwald 5839d9bbc01Smatthias.ringwald // close open channels 5849d9bbc01Smatthias.ringwald for (it = (linked_item_t *) l2cap_services; it ; it = it->next){ 5859d9bbc01Smatthias.ringwald l2cap_service_t * service = ((l2cap_service_t *) it); 5869d9bbc01Smatthias.ringwald if ( service->psm == psm){ 5879d9bbc01Smatthias.ringwald return service; 5889d9bbc01Smatthias.ringwald }; 5899d9bbc01Smatthias.ringwald } 5909d9bbc01Smatthias.ringwald return NULL; 5919d9bbc01Smatthias.ringwald } 5929d9bbc01Smatthias.ringwald 593*d8497f19Smatthias.ringwald void l2cap_register_service_internal(connection_t *connection, btstack_packet_handler_t packet_handler, uint16_t psm, uint16_t mtu){ 5949d9bbc01Smatthias.ringwald // check for alread registered psm // TODO: emit error event 5959d9bbc01Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 5969d9bbc01Smatthias.ringwald if (service) return; 5979d9bbc01Smatthias.ringwald 5989d9bbc01Smatthias.ringwald // alloc structure // TODO: emit error event 5999d9bbc01Smatthias.ringwald service = malloc(sizeof(l2cap_service_t)); 6009d9bbc01Smatthias.ringwald if (!service) return; 6019d9bbc01Smatthias.ringwald 6029d9bbc01Smatthias.ringwald // fill in 6039d9bbc01Smatthias.ringwald service->psm = psm; 6049d9bbc01Smatthias.ringwald service->mtu = mtu; 6059d9bbc01Smatthias.ringwald service->connection = connection; 606*d8497f19Smatthias.ringwald service->packet_handler = packet_handler; 6079d9bbc01Smatthias.ringwald 6089d9bbc01Smatthias.ringwald // add to services list 6099d9bbc01Smatthias.ringwald linked_list_add(&l2cap_services, (linked_item_t *) service); 6109d9bbc01Smatthias.ringwald } 6119d9bbc01Smatthias.ringwald 612116ee617Smatthias.ringwald void l2cap_unregister_service_internal(connection_t *connection, uint16_t psm){ 6139d9bbc01Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 614037d6e48Smatthias.ringwald if (!service) return; 6159d9bbc01Smatthias.ringwald linked_list_remove(&l2cap_services, (linked_item_t *) service); 6169d9bbc01Smatthias.ringwald free(service); 6179d9bbc01Smatthias.ringwald } 6189d9bbc01Smatthias.ringwald 6199d9bbc01Smatthias.ringwald // 6209d9bbc01Smatthias.ringwald void l2cap_close_connection(connection_t *connection){ 6219d9bbc01Smatthias.ringwald linked_item_t *it; 6229d9bbc01Smatthias.ringwald 6239d9bbc01Smatthias.ringwald // close open channels 624c52bf64dSmatthias.ringwald l2cap_channel_t * channel; 625c52bf64dSmatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 626c52bf64dSmatthias.ringwald channel = (l2cap_channel_t *) it; 627c52bf64dSmatthias.ringwald if (channel->connection == connection) { 628c52bf64dSmatthias.ringwald channel->sig_id = l2cap_next_sig_id(); 629b35f641cSmatthias.ringwald l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->sig_id, channel->remote_cid, channel->local_cid); 630c52bf64dSmatthias.ringwald channel->state = L2CAP_STATE_WAIT_DISCONNECT; 631c52bf64dSmatthias.ringwald } 632c52bf64dSmatthias.ringwald } 6339d9bbc01Smatthias.ringwald 634645658c9Smatthias.ringwald // unregister services 635645658c9Smatthias.ringwald l2cap_service_t *service; 6368149d2c7Smatthias.ringwald for (it = (linked_item_t *) l2cap_services; it ; ){ 6378149d2c7Smatthias.ringwald service = (l2cap_service_t *) it->next; 638645658c9Smatthias.ringwald if (service->connection == connection){ 6398149d2c7Smatthias.ringwald it->next = service->item.next; 6408149d2c7Smatthias.ringwald free(service); 6418149d2c7Smatthias.ringwald } else { 6428149d2c7Smatthias.ringwald it = it->next; 643645658c9Smatthias.ringwald } 644645658c9Smatthias.ringwald } 645c52bf64dSmatthias.ringwald } 646c52bf64dSmatthias.ringwald 647b5a49eb1Smatthias.ringwald // notify client/protocol handler 648b5a49eb1Smatthias.ringwald void l2cap_dispatch(l2cap_channel_t *channel, uint8_t type, uint8_t * data, uint16_t size){ 649b5a49eb1Smatthias.ringwald if (channel->packet_handler) { 6501b0354c0Smatthias.ringwald (* (channel->packet_handler))(type, 0, data, size); 651b5a49eb1Smatthias.ringwald } else { 652b5a49eb1Smatthias.ringwald (*event_packet_handler)(channel->connection, data, size); 653b5a49eb1Smatthias.ringwald } 654b5a49eb1Smatthias.ringwald } 655b5a49eb1Smatthias.ringwald 65603cfbabcSmatthias.ringwald void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) { 65703cfbabcSmatthias.ringwald uint8_t event[17]; 65880d52d6bSmatthias.ringwald event[0] = L2CAP_EVENT_CHANNEL_OPENED; 659c8e4258aSmatthias.ringwald event[1] = sizeof(event) - 2; 66003cfbabcSmatthias.ringwald event[2] = status; 66103cfbabcSmatthias.ringwald bt_flip_addr(&event[3], channel->address); 66203cfbabcSmatthias.ringwald bt_store_16(event, 9, channel->handle); 66303cfbabcSmatthias.ringwald bt_store_16(event, 11, channel->psm); 664b35f641cSmatthias.ringwald bt_store_16(event, 13, channel->local_cid); 665b35f641cSmatthias.ringwald bt_store_16(event, 15, channel->remote_cid); 6662289ec97Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 667b5a49eb1Smatthias.ringwald l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event)); 6681e6aba47Smatthias.ringwald } 6691e6aba47Smatthias.ringwald 670f62db1e3Smatthias.ringwald void l2cap_emit_channel_closed(l2cap_channel_t *channel) { 671f62db1e3Smatthias.ringwald uint8_t event[4]; 67280d52d6bSmatthias.ringwald event[0] = L2CAP_EVENT_CHANNEL_CLOSED; 673f62db1e3Smatthias.ringwald event[1] = sizeof(event) - 2; 674b35f641cSmatthias.ringwald bt_store_16(event, 2, channel->local_cid); 6752289ec97Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 676b5a49eb1Smatthias.ringwald l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event)); 677f62db1e3Smatthias.ringwald } 678f62db1e3Smatthias.ringwald 679e405ae81Smatthias.ringwald void l2cap_emit_connection_request(l2cap_channel_t *channel) { 680e405ae81Smatthias.ringwald uint8_t event[16]; 681e405ae81Smatthias.ringwald event[0] = L2CAP_EVENT_INCOMING_CONNECTION; 682e405ae81Smatthias.ringwald event[1] = sizeof(event) - 2; 683e405ae81Smatthias.ringwald bt_flip_addr(&event[2], channel->address); 684e405ae81Smatthias.ringwald bt_store_16(event, 8, channel->handle); 685e405ae81Smatthias.ringwald bt_store_16(event, 10, channel->psm); 686b35f641cSmatthias.ringwald bt_store_16(event, 12, channel->local_cid); 687b35f641cSmatthias.ringwald bt_store_16(event, 14, channel->remote_cid); 6882289ec97Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 689b5a49eb1Smatthias.ringwald l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event)); 690e405ae81Smatthias.ringwald } 6912289ec97Smatthias.ringwald 692b35f641cSmatthias.ringwald void l2cap_send_internal(uint16_t local_cid, uint8_t *data, uint16_t len){ 693b35f641cSmatthias.ringwald // find channel for local_cid, construct l2cap packet and send 694b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 695fcadd0caSmatthias.ringwald if (channel) { 6961e6aba47Smatthias.ringwald // 0 - Connection handle : PB=10 : BC=00 6971e6aba47Smatthias.ringwald bt_store_16(acl_buffer, 0, channel->handle | (2 << 12) | (0 << 14)); 6981e6aba47Smatthias.ringwald // 2 - ACL length 6991e6aba47Smatthias.ringwald bt_store_16(acl_buffer, 2, len + 4); 7001e6aba47Smatthias.ringwald // 4 - L2CAP packet length 7011e6aba47Smatthias.ringwald bt_store_16(acl_buffer, 4, len + 0); 7021e6aba47Smatthias.ringwald // 6 - L2CAP channel DEST 703b35f641cSmatthias.ringwald bt_store_16(acl_buffer, 6, channel->remote_cid); 7041e6aba47Smatthias.ringwald // 8 - data 7051e6aba47Smatthias.ringwald memcpy(&acl_buffer[8], data, len); 7061e6aba47Smatthias.ringwald // send 7071e6aba47Smatthias.ringwald hci_send_acl_packet(acl_buffer, len+8); 7081e6aba47Smatthias.ringwald } 7091e6aba47Smatthias.ringwald } 7101e6aba47Smatthias.ringwald 7119edc8742Smatthias.ringwald void l2cap_set_capture_connection(connection_t * connection){ 7129edc8742Smatthias.ringwald capture_connection = connection; 7139edc8742Smatthias.ringwald } 7141e6aba47Smatthias.ringwald 715b448a0e7Smatthias.ringwald