143625864Smatthias.ringwald /* 243625864Smatthias.ringwald * l2cap.c 343625864Smatthias.ringwald * 443625864Smatthias.ringwald * Logical Link Control and Adaption Protocl (L2CAP) 543625864Smatthias.ringwald * 643625864Smatthias.ringwald * Created by Matthias Ringwald on 5/16/09. 743625864Smatthias.ringwald */ 843625864Smatthias.ringwald 943625864Smatthias.ringwald #include "l2cap.h" 1043625864Smatthias.ringwald 1143625864Smatthias.ringwald #include <stdarg.h> 1243625864Smatthias.ringwald #include <string.h> 1343625864Smatthias.ringwald 1443625864Smatthias.ringwald #include <stdio.h> 1543625864Smatthias.ringwald 16*1e6aba47Smatthias.ringwald static uint8_t * sig_buffer = NULL; 17*1e6aba47Smatthias.ringwald static linked_list_t l2cap_channels = NULL; 18*1e6aba47Smatthias.ringwald static uint8_t * acl_buffer = NULL; 19*1e6aba47Smatthias.ringwald 20*1e6aba47Smatthias.ringwald void l2cap_init(){ 21*1e6aba47Smatthias.ringwald sig_buffer = malloc( 48 ); 22*1e6aba47Smatthias.ringwald acl_buffer = malloc( 255 + 8 ); 23*1e6aba47Smatthias.ringwald } 24*1e6aba47Smatthias.ringwald 2543625864Smatthias.ringwald 260af41d30Smatthias.ringwald int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){ 270af41d30Smatthias.ringwald va_list argptr; 280af41d30Smatthias.ringwald va_start(argptr, identifier); 290af41d30Smatthias.ringwald uint16_t len = l2cap_create_signaling_internal(sig_buffer, handle, cmd, identifier, argptr); 30*1e6aba47Smatthias.ringwald va_end(argptr); 310af41d30Smatthias.ringwald return hci_send_acl_packet(sig_buffer, len); 320af41d30Smatthias.ringwald } 330af41d30Smatthias.ringwald 34*1e6aba47Smatthias.ringwald // open outgoing L2CAP channel 35*1e6aba47Smatthias.ringwald void l2cap_create_channel_internal(connection_t * connection, bd_addr_t address, uint16_t psm){ 36*1e6aba47Smatthias.ringwald 37*1e6aba47Smatthias.ringwald // alloc structure 38*1e6aba47Smatthias.ringwald l2cap_channel_t * chan = malloc(sizeof(l2cap_channel_t)); 39*1e6aba47Smatthias.ringwald // TODO: emit error event 40*1e6aba47Smatthias.ringwald if (!chan) return; 41*1e6aba47Smatthias.ringwald 42*1e6aba47Smatthias.ringwald // fill in 43*1e6aba47Smatthias.ringwald BD_ADDR_COPY(chan->address, address); 44*1e6aba47Smatthias.ringwald chan->psm = psm; 45*1e6aba47Smatthias.ringwald chan->handle = 0; 46*1e6aba47Smatthias.ringwald chan->connection = connection; 47*1e6aba47Smatthias.ringwald 48*1e6aba47Smatthias.ringwald // set initial state 49*1e6aba47Smatthias.ringwald chan->state = L2CAP_STATE_CLOSED; 50*1e6aba47Smatthias.ringwald chan->sig_id = L2CAP_SIG_ID_INVALID; 51*1e6aba47Smatthias.ringwald 52*1e6aba47Smatthias.ringwald // add to connections list 53*1e6aba47Smatthias.ringwald linked_list_add(&l2cap_channels, (linked_item_t *) chan); 54*1e6aba47Smatthias.ringwald 55*1e6aba47Smatthias.ringwald // send connection request 56*1e6aba47Smatthias.ringwald // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch 57*1e6aba47Smatthias.ringwald hci_send_cmd(&hci_create_connection, address, 0x18, 0, 0, 0, 0); 5843625864Smatthias.ringwald } 5943625864Smatthias.ringwald 60*1e6aba47Smatthias.ringwald void l2cap_disconnect_internal(uint16_t source_cid, uint8_t reason){ 61*1e6aba47Smatthias.ringwald // TODO: implement 6243625864Smatthias.ringwald } 63*1e6aba47Smatthias.ringwald 64*1e6aba47Smatthias.ringwald 65*1e6aba47Smatthias.ringwald void l2cap_event_handler( uint8_t *packet, uint16_t size ){ 66*1e6aba47Smatthias.ringwald // handle connection complete events 67*1e6aba47Smatthias.ringwald if (packet[0] == HCI_EVENT_CONNECTION_COMPLETE && packet[2] == 0){ 68*1e6aba47Smatthias.ringwald bd_addr_t address; 69*1e6aba47Smatthias.ringwald bt_flip_addr(address, &packet[5]); 70*1e6aba47Smatthias.ringwald 71*1e6aba47Smatthias.ringwald linked_item_t *it; 72*1e6aba47Smatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 73*1e6aba47Smatthias.ringwald l2cap_channel_t * chan = (l2cap_channel_t *) it; 74*1e6aba47Smatthias.ringwald if ( ! BD_ADDR_CMP( chan->address, address) ){ 75*1e6aba47Smatthias.ringwald if (chan->state == L2CAP_STATE_CLOSED) { 76*1e6aba47Smatthias.ringwald chan->handle = READ_BT_16(packet, 3); 77*1e6aba47Smatthias.ringwald chan->sig_id = l2cap_next_sig_id(); 78*1e6aba47Smatthias.ringwald chan->source_cid = l2cap_next_source_cid(); 79*1e6aba47Smatthias.ringwald 80*1e6aba47Smatthias.ringwald l2cap_send_signaling_packet( chan->handle, CONNECTION_REQUEST, chan->sig_id, chan->psm, chan->source_cid); 81*1e6aba47Smatthias.ringwald 82*1e6aba47Smatthias.ringwald chan->state = L2CAP_STATE_WAIT_CONNECT_RSP; 83*1e6aba47Smatthias.ringwald } 84*1e6aba47Smatthias.ringwald } 85*1e6aba47Smatthias.ringwald } 86*1e6aba47Smatthias.ringwald } 87*1e6aba47Smatthias.ringwald // handle disconnection complete events 88*1e6aba47Smatthias.ringwald //@TODO:... 89*1e6aba47Smatthias.ringwald } 90*1e6aba47Smatthias.ringwald 91*1e6aba47Smatthias.ringwald void l2cap_signaling_handler(l2cap_channel_t *channel, uint8_t *packet, uint16_t size){ 92*1e6aba47Smatthias.ringwald 93*1e6aba47Smatthias.ringwald static uint8_t config_options[] = { 1, 2, 150, 0}; // mtu = 48 94*1e6aba47Smatthias.ringwald 95*1e6aba47Smatthias.ringwald uint8_t code = READ_L2CAP_SIGNALING_CODE( packet ); 96*1e6aba47Smatthias.ringwald uint8_t identifier = READ_L2CAP_SIGNALING_IDENTIFIER( packet ); 97*1e6aba47Smatthias.ringwald 98*1e6aba47Smatthias.ringwald switch (channel->state) { 99*1e6aba47Smatthias.ringwald 100*1e6aba47Smatthias.ringwald case L2CAP_STATE_WAIT_CONNECT_RSP: 101*1e6aba47Smatthias.ringwald switch (code){ 102*1e6aba47Smatthias.ringwald case CONNECTION_RESPONSE: 103*1e6aba47Smatthias.ringwald if ( READ_BT_16 (packet, L2CAP_SIGNALING_DATA_OFFSET+3) == 0){ 104*1e6aba47Smatthias.ringwald // successfull connection 105*1e6aba47Smatthias.ringwald channel->dest_cid = READ_BT_16(packet, L2CAP_SIGNALING_DATA_OFFSET + 0); 106*1e6aba47Smatthias.ringwald channel->sig_id = l2cap_next_sig_id(); 107*1e6aba47Smatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->sig_id, channel->dest_cid, 0, 4, &config_options); 108*1e6aba47Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP; 109*1e6aba47Smatthias.ringwald } else { 110*1e6aba47Smatthias.ringwald //@TODO: implement failed 111*1e6aba47Smatthias.ringwald } 112*1e6aba47Smatthias.ringwald break; 113*1e6aba47Smatthias.ringwald //@TODO: implement other signaling packets 114*1e6aba47Smatthias.ringwald } 115*1e6aba47Smatthias.ringwald break; 116*1e6aba47Smatthias.ringwald 117*1e6aba47Smatthias.ringwald case L2CAP_STATE_WAIT_CONFIG_REQ_RSP: 118*1e6aba47Smatthias.ringwald switch (code) { 119*1e6aba47Smatthias.ringwald case CONFIGURE_RESPONSE: 120*1e6aba47Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONFIG_REQ; 121*1e6aba47Smatthias.ringwald break; 122*1e6aba47Smatthias.ringwald } 123*1e6aba47Smatthias.ringwald break; 124*1e6aba47Smatthias.ringwald 125*1e6aba47Smatthias.ringwald case L2CAP_STATE_WAIT_CONFIG_REQ: 126*1e6aba47Smatthias.ringwald switch (code) { 127*1e6aba47Smatthias.ringwald case CONFIGURE_REQUEST: 128*1e6aba47Smatthias.ringwald 129*1e6aba47Smatthias.ringwald // accept the other's configuration options 130*1e6aba47Smatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, identifier, channel->dest_cid, 0, 0, size - 16, &packet[16]); 131*1e6aba47Smatthias.ringwald 132*1e6aba47Smatthias.ringwald channel->state = L2CAP_STATE_OPEN; 133*1e6aba47Smatthias.ringwald 134*1e6aba47Smatthias.ringwald // notify client 135*1e6aba47Smatthias.ringwald uint8_t event[8]; 136*1e6aba47Smatthias.ringwald event[0] = HCI_EVENT_L2CAP_CHANNEL_OPENED; 137*1e6aba47Smatthias.ringwald event[1] = 6; 138*1e6aba47Smatthias.ringwald bt_store_16(event, 2, channel->handle); 139*1e6aba47Smatthias.ringwald bt_store_16(event, 4, channel->source_cid); 140*1e6aba47Smatthias.ringwald bt_store_16(event, 6, channel->dest_cid); 141*1e6aba47Smatthias.ringwald socket_connection_send_packet(channel->connection, HCI_EVENT_PACKET, 0, event, sizeof(event)); 142*1e6aba47Smatthias.ringwald break; 143*1e6aba47Smatthias.ringwald } 144*1e6aba47Smatthias.ringwald break; 145*1e6aba47Smatthias.ringwald } 146*1e6aba47Smatthias.ringwald } 147*1e6aba47Smatthias.ringwald 148*1e6aba47Smatthias.ringwald void l2cap_acl_handler( uint8_t *packet, uint16_t size ){ 149*1e6aba47Smatthias.ringwald 150*1e6aba47Smatthias.ringwald // Get Channel ID and command code 151*1e6aba47Smatthias.ringwald uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet); 152*1e6aba47Smatthias.ringwald uint8_t code = READ_L2CAP_SIGNALING_CODE( packet ); 153*1e6aba47Smatthias.ringwald 154*1e6aba47Smatthias.ringwald // Get Connection 155*1e6aba47Smatthias.ringwald hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet); 156*1e6aba47Smatthias.ringwald 157*1e6aba47Smatthias.ringwald // Signaling Packet? 158*1e6aba47Smatthias.ringwald if (channel_id == 1) { 159*1e6aba47Smatthias.ringwald 160*1e6aba47Smatthias.ringwald if (code < 1 || code == 2 || code >= 8){ 161*1e6aba47Smatthias.ringwald // not for a particular channel 162*1e6aba47Smatthias.ringwald return; 163*1e6aba47Smatthias.ringwald } 164*1e6aba47Smatthias.ringwald 165*1e6aba47Smatthias.ringwald // Get Signaling Identifier and potential destination CID 166*1e6aba47Smatthias.ringwald uint8_t sig_id = READ_L2CAP_SIGNALING_IDENTIFIER(packet); 167*1e6aba47Smatthias.ringwald uint16_t dest_cid = READ_BT_16(packet, L2CAP_SIGNALING_DATA_OFFSET); 168*1e6aba47Smatthias.ringwald 169*1e6aba47Smatthias.ringwald // Find channel for this sig_id and connection handle 170*1e6aba47Smatthias.ringwald linked_item_t *it; 171*1e6aba47Smatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 172*1e6aba47Smatthias.ringwald l2cap_channel_t * chan = (l2cap_channel_t *) it; 173*1e6aba47Smatthias.ringwald if (chan->handle == handle) { 174*1e6aba47Smatthias.ringwald if (code & 1) { 175*1e6aba47Smatthias.ringwald // match odd commands by previous signaling identifier 176*1e6aba47Smatthias.ringwald if (chan->sig_id == sig_id) { 177*1e6aba47Smatthias.ringwald l2cap_signaling_handler( chan, packet, size); 178*1e6aba47Smatthias.ringwald } 179*1e6aba47Smatthias.ringwald } else { 180*1e6aba47Smatthias.ringwald // match even commands by source channel id 181*1e6aba47Smatthias.ringwald if (chan->source_cid == dest_cid) { 182*1e6aba47Smatthias.ringwald l2cap_signaling_handler( chan, packet, size); 183*1e6aba47Smatthias.ringwald } 184*1e6aba47Smatthias.ringwald } 185*1e6aba47Smatthias.ringwald } 186*1e6aba47Smatthias.ringwald } 187*1e6aba47Smatthias.ringwald return; 188*1e6aba47Smatthias.ringwald } 189*1e6aba47Smatthias.ringwald 190*1e6aba47Smatthias.ringwald // Find channel for this channel_id and connection handle 191*1e6aba47Smatthias.ringwald linked_item_t *it; 192*1e6aba47Smatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 193*1e6aba47Smatthias.ringwald l2cap_channel_t * channel = (l2cap_channel_t *) it; 194*1e6aba47Smatthias.ringwald if ( channel->source_cid == channel_id && channel->handle == handle) { 195*1e6aba47Smatthias.ringwald // send data packet back 196*1e6aba47Smatthias.ringwald socket_connection_send_packet(channel->connection, HCI_ACL_DATA_PACKET, 0, packet, size); 197*1e6aba47Smatthias.ringwald } 198*1e6aba47Smatthias.ringwald } 199*1e6aba47Smatthias.ringwald } 200*1e6aba47Smatthias.ringwald 201*1e6aba47Smatthias.ringwald void l2cap_send_internal(uint16_t source_cid, uint8_t *data, uint16_t len){ 202*1e6aba47Smatthias.ringwald // find channel for source_cid, construct l2cap packet and send 203*1e6aba47Smatthias.ringwald linked_item_t *it; 204*1e6aba47Smatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 205*1e6aba47Smatthias.ringwald l2cap_channel_t * channel = (l2cap_channel_t *) it; 206*1e6aba47Smatthias.ringwald if ( channel->source_cid == source_cid) { 207*1e6aba47Smatthias.ringwald 208*1e6aba47Smatthias.ringwald // use hci_cmd_buffer for now 209*1e6aba47Smatthias.ringwald 210*1e6aba47Smatthias.ringwald // 0 - Connection handle : PB=10 : BC=00 211*1e6aba47Smatthias.ringwald bt_store_16(acl_buffer, 0, channel->handle | (2 << 12) | (0 << 14)); 212*1e6aba47Smatthias.ringwald // 2 - ACL length 213*1e6aba47Smatthias.ringwald bt_store_16(acl_buffer, 2, len + 4); 214*1e6aba47Smatthias.ringwald // 4 - L2CAP packet length 215*1e6aba47Smatthias.ringwald bt_store_16(acl_buffer, 4, len + 0); 216*1e6aba47Smatthias.ringwald // 6 - L2CAP channel DEST 217*1e6aba47Smatthias.ringwald bt_store_16(acl_buffer, 6, channel->dest_cid); 218*1e6aba47Smatthias.ringwald // 8 - data 219*1e6aba47Smatthias.ringwald memcpy(&acl_buffer[8], data, len); 220*1e6aba47Smatthias.ringwald // send 221*1e6aba47Smatthias.ringwald hci_send_acl_packet(acl_buffer, len+8); 222*1e6aba47Smatthias.ringwald 223*1e6aba47Smatthias.ringwald return; 224*1e6aba47Smatthias.ringwald } 225*1e6aba47Smatthias.ringwald } 226*1e6aba47Smatthias.ringwald } 227*1e6aba47Smatthias.ringwald 228*1e6aba47Smatthias.ringwald 229