195cbd947Smatthias.ringwald /* 2a0c35809S[email protected] * Copyright (C) 2014 BlueKitchen GmbH 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. 166b64433eSmatthias.ringwald * 4. Any redistribution, use, or modification is done solely for 176b64433eSmatthias.ringwald * personal benefit and not for any commercial purpose or for 186b64433eSmatthias.ringwald * monetary gain. 191713bceaSmatthias.ringwald * 20a0c35809S[email protected] * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 211713bceaSmatthias.ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 221713bceaSmatthias.ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 231713bceaSmatthias.ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 241713bceaSmatthias.ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 251713bceaSmatthias.ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 261713bceaSmatthias.ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 271713bceaSmatthias.ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 281713bceaSmatthias.ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 291713bceaSmatthias.ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 301713bceaSmatthias.ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311713bceaSmatthias.ringwald * SUCH DAMAGE. 321713bceaSmatthias.ringwald * 33a0c35809S[email protected] * Please inquire about commercial licensing options at 34a0c35809S[email protected] * [email protected] 356b64433eSmatthias.ringwald * 361713bceaSmatthias.ringwald */ 371713bceaSmatthias.ringwald 38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "l2cap_signaling.c" 39ab2c6ae4SMatthias Ringwald 401713bceaSmatthias.ringwald /* 4195cbd947Smatthias.ringwald * l2cap_signaling.h 4295cbd947Smatthias.ringwald * 4395cbd947Smatthias.ringwald * Created by Matthias Ringwald on 7/23/09. 4495cbd947Smatthias.ringwald */ 4595cbd947Smatthias.ringwald 4695cbd947Smatthias.ringwald #include "l2cap_signaling.h" 477907f069SMatthias Ringwald #include "btstack_config.h" 487224be7eSMatthias Ringwald #include "btstack_debug.h" 49b398e4aaS[email protected] #include "hci.h" 5095cbd947Smatthias.ringwald 51b48a17ffSmatthias.ringwald #include <string.h> 52b48a17ffSmatthias.ringwald 532a9f55d6SMatthias Ringwald static uint16_t l2cap_create_signaling_internal(uint8_t * acl_buffer, hci_con_handle_t handle, bool is_classic, uint16_t cid, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, va_list argptr){ 548334d3d8SMatthias Ringwald 5595b6ea6eS[email protected] static const char *l2cap_signaling_commands_format[] = { 565ca8d57bS[email protected] "2D", // 0x01 command reject: reason {cmd not understood (0), sig MTU exceeded (2:max sig MTU), invalid CID (4:req CID)}, data len, data 5795cbd947Smatthias.ringwald "22", // 0x02 connection request: PSM, Source CID 5895cbd947Smatthias.ringwald "2222", // 0x03 connection response: Dest CID, Source CID, Result, Status 5995cbd947Smatthias.ringwald "22D", // 0x04 config request: Dest CID, Flags, Configuration options 6095cbd947Smatthias.ringwald "222D", // 0x05 config response: Source CID, Flags, Result, Configuration options 6195cbd947Smatthias.ringwald "22", // 0x06 disconection request: Dest CID, Source CID 6295cbd947Smatthias.ringwald "22", // 0x07 disconection response: Dest CID, Source CID 6395cbd947Smatthias.ringwald "D", // 0x08 echo request: Data 6495cbd947Smatthias.ringwald "D", // 0x09 echo response: Data 6595cbd947Smatthias.ringwald "2", // 0x0a information request: InfoType {1=Connectionless MTU, 2=Extended features supported} 6695cbd947Smatthias.ringwald "22D", // 0x0b information response: InfoType, Result, Data 67a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 687224be7eSMatthias Ringwald NULL, // 0x0c non-supported AMP command 697224be7eSMatthias Ringwald NULL, // 0x0d non-supported AMP command 707224be7eSMatthias Ringwald NULL, // 0x0e non-supported AMP command 717224be7eSMatthias Ringwald NULL, // 0x0f non-supported AMP command 727224be7eSMatthias Ringwald NULL, // 0x10 non-supported AMP command 737224be7eSMatthias Ringwald NULL, // 0x11 non-supported AMP command 749e795b48S[email protected] "2222", // 0x12 connection parameter update request: interval min, interval max, slave latency, timeout multipler 759e795b48S[email protected] "2", // 0x13 connection parameter update response: result 7699b3db54SMatthias Ringwald "22222", // 0X14 le credit based connection request: le psm, source cid, mtu, mps, initial credits 7799b3db54SMatthias Ringwald "22222", // 0x15 le credit based connection respone: dest cid, mtu, mps, initial credits, result 7899b3db54SMatthias Ringwald "22", // 0x16 le flow control credit: source cid, credits 799e795b48S[email protected] #endif 8015f09267SMilanka Ringwald #ifdef UNIT_TEST 8115f09267SMilanka Ringwald "M", // invalid format for unit testing 8215f09267SMilanka Ringwald #endif 8395cbd947Smatthias.ringwald }; 84fdfcab43SMatthias Ringwald static const unsigned int num_l2cap_commands = sizeof(l2cap_signaling_commands_format) / sizeof(const char *); 857224be7eSMatthias Ringwald 867224be7eSMatthias Ringwald const char *format = NULL; 874ea43905SMatthias Ringwald if ((cmd > 0u) && (cmd <= num_l2cap_commands)) { 884ea43905SMatthias Ringwald format = l2cap_signaling_commands_format[cmd-1u]; 897224be7eSMatthias Ringwald } 907224be7eSMatthias Ringwald if (!format){ 917224be7eSMatthias Ringwald log_error("l2cap_create_signaling_internal: invalid command id 0x%02x", cmd); 927224be7eSMatthias Ringwald return 0; 937224be7eSMatthias Ringwald } 947224be7eSMatthias Ringwald 952a9f55d6SMatthias Ringwald int pb = 0x00; // First non-automatically-flushable packet of a higher layer message 962a9f55d6SMatthias Ringwald #ifdef ENABLE_CLASSIC 972a9f55d6SMatthias Ringwald if (is_classic){ 982a9f55d6SMatthias Ringwald pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02; 992a9f55d6SMatthias Ringwald } 1001c79b5e3SMatthias Ringwald #else 1011c79b5e3SMatthias Ringwald UNUSED(is_classic); 1022a9f55d6SMatthias Ringwald #endif 103e9772277S[email protected] 104e9772277S[email protected] // 0 - Connection handle : PB=pb : BC=00 1054ea43905SMatthias Ringwald little_endian_store_16(acl_buffer, 0u, handle | (pb << 12u) | (0u << 14u)); 10695cbd947Smatthias.ringwald // 6 - L2CAP channel = 1 107f8fbdce0SMatthias Ringwald little_endian_store_16(acl_buffer, 6, cid); 10895cbd947Smatthias.ringwald // 8 - Code 10995cbd947Smatthias.ringwald acl_buffer[8] = cmd; 11095cbd947Smatthias.ringwald // 9 - id (!= 0 sequentially) 11195cbd947Smatthias.ringwald acl_buffer[9] = identifier; 11295cbd947Smatthias.ringwald 11395cbd947Smatthias.ringwald // 12 - L2CAP signaling parameters 11495cbd947Smatthias.ringwald uint16_t pos = 12; 11595cbd947Smatthias.ringwald uint16_t word; 11695cbd947Smatthias.ringwald uint8_t * ptr; 11795cbd947Smatthias.ringwald while (*format) { 11895cbd947Smatthias.ringwald switch(*format) { 11995cbd947Smatthias.ringwald case '2': // 16 bit value 120*c0dd2f5cSMilanka Ringwald word = va_arg(argptr, int); // LCOV_EXCL_BR_LINE 12195cbd947Smatthias.ringwald // minimal va_arg is int: 2 bytes on 8+16 bit CPUs 1224ea43905SMatthias Ringwald acl_buffer[pos++] = word & 0xffu; 12395cbd947Smatthias.ringwald acl_buffer[pos++] = word >> 8; 12495cbd947Smatthias.ringwald break; 12595cbd947Smatthias.ringwald case 'D': // variable data. passed: len, ptr 126*c0dd2f5cSMilanka Ringwald word = va_arg(argptr, int); // LCOV_EXCL_BR_LINE 127*c0dd2f5cSMilanka Ringwald ptr = va_arg(argptr, uint8_t *); // LCOV_EXCL_BR_LINE 1286535961aSMatthias Ringwald (void)memcpy(&acl_buffer[pos], ptr, word); 12995cbd947Smatthias.ringwald pos += word; 13095cbd947Smatthias.ringwald break; 13195cbd947Smatthias.ringwald default: 132d7c9cb2bSMilanka Ringwald btstack_unreachable(); 13395cbd947Smatthias.ringwald break; 13495cbd947Smatthias.ringwald } 13595cbd947Smatthias.ringwald format++; 13695cbd947Smatthias.ringwald }; 13795cbd947Smatthias.ringwald va_end(argptr); 13895cbd947Smatthias.ringwald 13995cbd947Smatthias.ringwald // Fill in various length fields: it's the number of bytes following for ACL lenght and l2cap parameter length 14095cbd947Smatthias.ringwald // - the l2cap payload length is counted after the following channel id (only payload) 14195cbd947Smatthias.ringwald 14295cbd947Smatthias.ringwald // 2 - ACL length 1434ea43905SMatthias Ringwald little_endian_store_16(acl_buffer, 2u, pos - 4u); 14495cbd947Smatthias.ringwald // 4 - L2CAP packet length 1454ea43905SMatthias Ringwald little_endian_store_16(acl_buffer, 4u, pos - 6u - 2u); 14695cbd947Smatthias.ringwald // 10 - L2CAP signaling parameter length 1474ea43905SMatthias Ringwald little_endian_store_16(acl_buffer, 10u, pos - 12u); 14895cbd947Smatthias.ringwald 14995cbd947Smatthias.ringwald return pos; 15095cbd947Smatthias.ringwald } 151ab2b01dcS[email protected] 152905c28a6SMatthias Ringwald #ifdef ENABLE_CLASSIC 153c61af878S[email protected] 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){ 1542a9f55d6SMatthias Ringwald return l2cap_create_signaling_internal(acl_buffer, handle, true, 1, cmd, identifier, argptr); 155c61af878S[email protected] } 156905c28a6SMatthias Ringwald #endif 157c61af878S[email protected] 158a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 159c61af878S[email protected] 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){ 1602a9f55d6SMatthias Ringwald return l2cap_create_signaling_internal(acl_buffer, handle, false, 5, cmd, identifier, argptr); 161c61af878S[email protected] } 162ab2b01dcS[email protected] #endif 163