1 /* 2 * Copyright (C) 2014 BlueKitchen GmbH 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 BLUEKITCHEN GMBH 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 34 * [email protected] 35 * 36 */ 37 38 /* 39 * l2cap_signaling.h 40 * 41 * Created by Matthias Ringwald on 7/23/09. 42 */ 43 44 #include "l2cap_signaling.h" 45 #include "btstack-config.h" 46 #include "hci.h" 47 48 #include <string.h> 49 50 static const char *l2cap_signaling_commands_format[] = { 51 "2D", // 0x01 command reject: reason {cmd not understood (0), sig MTU exceeded (2:max sig MTU), invalid CID (4:req CID)}, data len, data 52 "22", // 0x02 connection request: PSM, Source CID 53 "2222", // 0x03 connection response: Dest CID, Source CID, Result, Status 54 "22D", // 0x04 config request: Dest CID, Flags, Configuration options 55 "222D", // 0x05 config response: Source CID, Flags, Result, Configuration options 56 "22", // 0x06 disconection request: Dest CID, Source CID 57 "22", // 0x07 disconection response: Dest CID, Source CID 58 "D", // 0x08 echo request: Data 59 "D", // 0x09 echo response: Data 60 "2", // 0x0a information request: InfoType {1=Connectionless MTU, 2=Extended features supported} 61 "22D", // 0x0b information response: InfoType, Result, Data 62 #ifdef HAVE_BLE 63 // skip 6 not supported signaling pdus, see below 64 "2222", // 0x12 connection parameter update request: interval min, interval max, slave latency, timeout multipler 65 "2", // 0x13 connection parameter update response: result 66 #endif 67 }; 68 69 uint8_t sig_seq_nr = 0xff; 70 uint16_t source_cid = 0x40; 71 72 uint8_t l2cap_next_sig_id(void){ 73 if (sig_seq_nr == 0xff) { 74 sig_seq_nr = 1; 75 } else { 76 sig_seq_nr++; 77 } 78 return sig_seq_nr; 79 } 80 81 uint16_t l2cap_next_local_cid(void){ 82 return source_cid++; 83 } 84 85 uint16_t l2cap_create_signaling_internal(uint8_t * acl_buffer, hci_con_handle_t handle, uint16_t cid, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, va_list argptr){ 86 87 int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02; 88 89 // 0 - Connection handle : PB=pb : BC=00 90 bt_store_16(acl_buffer, 0, handle | (pb << 12) | (0 << 14)); 91 // 6 - L2CAP channel = 1 92 bt_store_16(acl_buffer, 6, cid); 93 // 8 - Code 94 acl_buffer[8] = cmd; 95 // 9 - id (!= 0 sequentially) 96 acl_buffer[9] = identifier; 97 98 // 12 - L2CAP signaling parameters 99 uint16_t pos = 12; 100 // skip AMP commands 101 if (cmd >= CONNECTION_PARAMETER_UPDATE_REQUEST){ 102 cmd -= 6; 103 } 104 const char *format = l2cap_signaling_commands_format[cmd-1]; 105 uint16_t word; 106 uint8_t * ptr; 107 while (*format) { 108 switch(*format) { 109 case '1': // 8 bit value 110 case '2': // 16 bit value 111 word = va_arg(argptr, int); 112 // minimal va_arg is int: 2 bytes on 8+16 bit CPUs 113 acl_buffer[pos++] = word & 0xff; 114 if (*format == '2') { 115 acl_buffer[pos++] = word >> 8; 116 } 117 break; 118 case 'D': // variable data. passed: len, ptr 119 word = va_arg(argptr, int); 120 ptr = va_arg(argptr, uint8_t *); 121 memcpy(&acl_buffer[pos], ptr, word); 122 pos += word; 123 break; 124 default: 125 break; 126 } 127 format++; 128 }; 129 va_end(argptr); 130 131 // Fill in various length fields: it's the number of bytes following for ACL lenght and l2cap parameter length 132 // - the l2cap payload length is counted after the following channel id (only payload) 133 134 // 2 - ACL length 135 bt_store_16(acl_buffer, 2, pos - 4); 136 // 4 - L2CAP packet length 137 bt_store_16(acl_buffer, 4, pos - 6 - 2); 138 // 10 - L2CAP signaling parameter length 139 bt_store_16(acl_buffer, 10, pos - 12); 140 141 return pos; 142 } 143 144 uint16_t l2cap_create_signaling_classic(uint8_t * acl_buffer, hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, va_list argptr){ 145 return l2cap_create_signaling_internal(acl_buffer, handle, 1, cmd, identifier, argptr); 146 } 147 148 #ifdef HAVE_BLE 149 150 uint16_t l2cap_create_signaling_le(uint8_t * acl_buffer, hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, va_list argptr){ 151 return l2cap_create_signaling_internal(acl_buffer, handle, 5, cmd, identifier, argptr); 152 } 153 154 uint16_t l2cap_le_create_connection_parameter_update_request(uint8_t * acl_buffer, uint16_t handle, uint8_t identifier, uint16_t interval_min, uint16_t interval_max, uint16_t slave_latency, uint16_t timeout_multiplier){ 155 156 int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02; 157 158 // 0 - Connection handle : PB=pb : BC=00 159 bt_store_16(acl_buffer, 0, handle | (pb << 12) | (0 << 14)); 160 // 6 - L2CAP LE Signaling channel = 5 161 bt_store_16(acl_buffer, 6, 5); 162 // 8 - Code 163 acl_buffer[8] = CONNECTION_PARAMETER_UPDATE_REQUEST; 164 // 9 - id 165 acl_buffer[9] = identifier; 166 uint16_t pos = 12; 167 bt_store_16(acl_buffer, pos, interval_min); 168 pos += 2; 169 bt_store_16(acl_buffer, pos, interval_max); 170 pos += 2; 171 bt_store_16(acl_buffer, pos, slave_latency); 172 pos += 2; 173 bt_store_16(acl_buffer, pos, timeout_multiplier); 174 pos += 2; 175 // 2 - ACL length 176 bt_store_16(acl_buffer, 2, pos - 4); 177 // 4 - L2CAP packet length 178 bt_store_16(acl_buffer, 4, pos - 6 - 2); 179 // 10 - L2CAP signaling parameter length 180 bt_store_16(acl_buffer, 10, pos - 12); 181 return pos; 182 } 183 184 uint16_t l2cap_le_create_connection_parameter_update_response(uint8_t * acl_buffer, uint16_t handle, uint8_t identifier, uint16_t response){ 185 186 int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02; 187 188 // 0 - Connection handle : PB=pb : BC=00 189 bt_store_16(acl_buffer, 0, handle | (pb << 12) | (0 << 14)); 190 // 6 - L2CAP LE Signaling channel = 5 191 bt_store_16(acl_buffer, 6, 5); 192 // 8 - Code 193 acl_buffer[8] = CONNECTION_PARAMETER_UPDATE_RESPONSE; 194 // 9 - id 195 acl_buffer[9] = identifier; 196 uint16_t pos = 12; 197 bt_store_16(acl_buffer, pos, response); 198 pos += 2; 199 // 2 - ACL length 200 bt_store_16(acl_buffer, 2, pos - 4); 201 // 4 - L2CAP packet length 202 bt_store_16(acl_buffer, 4, pos - 6 - 2); 203 // 10 - L2CAP signaling parameter length 204 bt_store_16(acl_buffer, 10, pos - 12); 205 return pos; 206 } 207 208 209 #endif 210