1 /* 2 * Copyright (C) 2009-2012 by Matthias Ringwald 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the copyright holders nor the names of 14 * contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 4. Any redistribution, use, or modification is done solely for 17 * personal benefit and not for any commercial purpose or for 18 * monetary gain. 19 * 20 * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24 * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * Please inquire about commercial licensing options at [email protected] 34 * 35 */ 36 37 /* 38 * l2cap_signaling.h 39 * 40 * Created by Matthias Ringwald on 7/23/09. 41 */ 42 43 #include "l2cap_signaling.h" 44 45 #include <string.h> 46 47 static const char *l2cap_signaling_commands_format[] = { 48 "D", // 0x01 command reject: reason {cmd not understood (0), sig MTU exceeded (2:max sig MTU), invalid CID (4:req CID)}, data len, data 49 "22", // 0x02 connection request: PSM, Source CID 50 "2222", // 0x03 connection response: Dest CID, Source CID, Result, Status 51 "22D", // 0x04 config request: Dest CID, Flags, Configuration options 52 "222D", // 0x05 config response: Source CID, Flags, Result, Configuration options 53 "22", // 0x06 disconection request: Dest CID, Source CID 54 "22", // 0x07 disconection response: Dest CID, Source CID 55 "D", // 0x08 echo request: Data 56 "D", // 0x09 echo response: Data 57 "2", // 0x0a information request: InfoType {1=Connectionless MTU, 2=Extended features supported} 58 "22D", // 0x0b information response: InfoType, Result, Data 59 #ifdef HAVE_BLE 60 // skip 6 not supported signaling pdus, see below 61 "2222", // 0x12 connection parameter update request: interval min, interval max, slave latency, timeout multipler 62 "2", // 0x13 connection parameter update response: result 63 #endif 64 }; 65 66 uint8_t sig_seq_nr = 0xff; 67 uint16_t source_cid = 0x40; 68 69 uint8_t l2cap_next_sig_id(void){ 70 if (sig_seq_nr == 0xff) { 71 sig_seq_nr = 1; 72 } else { 73 sig_seq_nr++; 74 } 75 return sig_seq_nr; 76 } 77 78 uint16_t l2cap_next_local_cid(void){ 79 return source_cid++; 80 } 81 82 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){ 83 84 // 0 - Connection handle : PB=10 : BC=00 85 bt_store_16(acl_buffer, 0, handle | (2 << 12) | (0 << 14)); 86 // 6 - L2CAP channel = 1 87 bt_store_16(acl_buffer, 6, 1); 88 // 8 - Code 89 acl_buffer[8] = cmd; 90 // 9 - id (!= 0 sequentially) 91 acl_buffer[9] = identifier; 92 93 // 12 - L2CAP signaling parameters 94 uint16_t pos = 12; 95 // skip AMP commands 96 if (cmd >= CONNECTION_PARAMETER_UPDATE_REQUEST){ 97 cmd -= 6; 98 } 99 const char *format = l2cap_signaling_commands_format[cmd-1]; 100 uint16_t word; 101 uint8_t * ptr; 102 while (*format) { 103 switch(*format) { 104 case '1': // 8 bit value 105 case '2': // 16 bit value 106 word = va_arg(argptr, int); 107 // minimal va_arg is int: 2 bytes on 8+16 bit CPUs 108 acl_buffer[pos++] = word & 0xff; 109 if (*format == '2') { 110 acl_buffer[pos++] = word >> 8; 111 } 112 break; 113 case 'D': // variable data. passed: len, ptr 114 word = va_arg(argptr, int); 115 ptr = va_arg(argptr, uint8_t *); 116 memcpy(&acl_buffer[pos], ptr, word); 117 pos += word; 118 break; 119 default: 120 break; 121 } 122 format++; 123 }; 124 va_end(argptr); 125 126 // Fill in various length fields: it's the number of bytes following for ACL lenght and l2cap parameter length 127 // - the l2cap payload length is counted after the following channel id (only payload) 128 129 // 2 - ACL length 130 bt_store_16(acl_buffer, 2, pos - 4); 131 // 4 - L2CAP packet length 132 bt_store_16(acl_buffer, 4, pos - 6 - 2); 133 // 10 - L2CAP signaling parameter length 134 bt_store_16(acl_buffer, 10, pos - 12); 135 136 return pos; 137 } 138