195cbd947Smatthias.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 /* 3395cbd947Smatthias.ringwald * l2cap_signaling.h 3495cbd947Smatthias.ringwald * 3595cbd947Smatthias.ringwald * Created by Matthias Ringwald on 7/23/09. 3695cbd947Smatthias.ringwald */ 3795cbd947Smatthias.ringwald 3895cbd947Smatthias.ringwald #include "l2cap_signaling.h" 3995cbd947Smatthias.ringwald 40b48a17ffSmatthias.ringwald #include <string.h> 41b48a17ffSmatthias.ringwald 4295cbd947Smatthias.ringwald static char *l2cap_signaling_commands_format[] = { 4395cbd947Smatthias.ringwald "D", // 0x01 command reject: reason {cmd not understood (0), sig MTU exceeded (2:max sig MTU), invalid CID (4:req CID)}, data len, data 4495cbd947Smatthias.ringwald "22", // 0x02 connection request: PSM, Source CID 4595cbd947Smatthias.ringwald "2222", // 0x03 connection response: Dest CID, Source CID, Result, Status 4695cbd947Smatthias.ringwald "22D", // 0x04 config request: Dest CID, Flags, Configuration options 4795cbd947Smatthias.ringwald "222D", // 0x05 config response: Source CID, Flags, Result, Configuration options 4895cbd947Smatthias.ringwald "22", // 0x06 disconection request: Dest CID, Source CID 4995cbd947Smatthias.ringwald "22", // 0x07 disconection response: Dest CID, Source CID 5095cbd947Smatthias.ringwald "D", // 0x08 echo request: Data 5195cbd947Smatthias.ringwald "D", // 0x09 echo response: Data 5295cbd947Smatthias.ringwald "2", // 0x0a information request: InfoType {1=Connectionless MTU, 2=Extended features supported} 5395cbd947Smatthias.ringwald "22D", // 0x0b information response: InfoType, Result, Data 5495cbd947Smatthias.ringwald }; 5595cbd947Smatthias.ringwald 56da269baaSmatthias.ringwald uint8_t sig_seq_nr = 0xff; 57bb53b291Smatthias.ringwald uint16_t source_cid = 0x40; 5895cbd947Smatthias.ringwald 59da269baaSmatthias.ringwald uint8_t l2cap_next_sig_id(void){ 60da269baaSmatthias.ringwald if (sig_seq_nr == 0xff) { 61da269baaSmatthias.ringwald sig_seq_nr = 1; 62da269baaSmatthias.ringwald } else { 63da269baaSmatthias.ringwald sig_seq_nr++; 64da269baaSmatthias.ringwald } 65da269baaSmatthias.ringwald return sig_seq_nr; 66da269baaSmatthias.ringwald } 67da269baaSmatthias.ringwald 68*b35f641cSmatthias.ringwald uint16_t l2cap_next_local_cid(void){ 69bb53b291Smatthias.ringwald return source_cid++; 70da269baaSmatthias.ringwald } 71da269baaSmatthias.ringwald 7295cbd947Smatthias.ringwald uint16_t l2cap_create_signaling_internal(uint8_t * acl_buffer, hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, va_list argptr){ 7395cbd947Smatthias.ringwald 7495cbd947Smatthias.ringwald // 0 - Connection handle : PB=10 : BC=00 7595cbd947Smatthias.ringwald bt_store_16(acl_buffer, 0, handle | (2 << 12) | (0 << 14)); 7695cbd947Smatthias.ringwald // 6 - L2CAP channel = 1 7795cbd947Smatthias.ringwald bt_store_16(acl_buffer, 6, 1); 7895cbd947Smatthias.ringwald // 8 - Code 7995cbd947Smatthias.ringwald acl_buffer[8] = cmd; 8095cbd947Smatthias.ringwald // 9 - id (!= 0 sequentially) 8195cbd947Smatthias.ringwald acl_buffer[9] = identifier; 8295cbd947Smatthias.ringwald 8395cbd947Smatthias.ringwald // 12 - L2CAP signaling parameters 8495cbd947Smatthias.ringwald uint16_t pos = 12; 8595cbd947Smatthias.ringwald const char *format = l2cap_signaling_commands_format[cmd-1]; 8695cbd947Smatthias.ringwald uint16_t word; 8795cbd947Smatthias.ringwald uint8_t * ptr; 8895cbd947Smatthias.ringwald while (*format) { 8995cbd947Smatthias.ringwald switch(*format) { 9095cbd947Smatthias.ringwald case '1': // 8 bit value 9195cbd947Smatthias.ringwald case '2': // 16 bit value 9295cbd947Smatthias.ringwald word = va_arg(argptr, int); 9395cbd947Smatthias.ringwald // minimal va_arg is int: 2 bytes on 8+16 bit CPUs 9495cbd947Smatthias.ringwald acl_buffer[pos++] = word & 0xff; 9595cbd947Smatthias.ringwald if (*format == '2') { 9695cbd947Smatthias.ringwald acl_buffer[pos++] = word >> 8; 9795cbd947Smatthias.ringwald } 9895cbd947Smatthias.ringwald break; 9995cbd947Smatthias.ringwald case 'D': // variable data. passed: len, ptr 10095cbd947Smatthias.ringwald word = va_arg(argptr, int); 10195cbd947Smatthias.ringwald ptr = va_arg(argptr, uint8_t *); 10295cbd947Smatthias.ringwald memcpy(&acl_buffer[pos], ptr, word); 10395cbd947Smatthias.ringwald pos += word; 10495cbd947Smatthias.ringwald break; 10595cbd947Smatthias.ringwald default: 10695cbd947Smatthias.ringwald break; 10795cbd947Smatthias.ringwald } 10895cbd947Smatthias.ringwald format++; 10995cbd947Smatthias.ringwald }; 11095cbd947Smatthias.ringwald va_end(argptr); 11195cbd947Smatthias.ringwald 11295cbd947Smatthias.ringwald // Fill in various length fields: it's the number of bytes following for ACL lenght and l2cap parameter length 11395cbd947Smatthias.ringwald // - the l2cap payload length is counted after the following channel id (only payload) 11495cbd947Smatthias.ringwald 11595cbd947Smatthias.ringwald // 2 - ACL length 11695cbd947Smatthias.ringwald bt_store_16(acl_buffer, 2, pos - 4); 11795cbd947Smatthias.ringwald // 4 - L2CAP packet length 11895cbd947Smatthias.ringwald bt_store_16(acl_buffer, 4, pos - 6 - 2); 11995cbd947Smatthias.ringwald // 10 - L2CAP signaling parameter length 12095cbd947Smatthias.ringwald bt_store_16(acl_buffer, 10, pos - 12); 12195cbd947Smatthias.ringwald 12295cbd947Smatthias.ringwald return pos; 12395cbd947Smatthias.ringwald } 124