xref: /btstack/src/l2cap_signaling.c (revision 3486da78f9006fda6b2b39c9ac8a625a6c8b3690)
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
232fca4dadSMilanka Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
242fca4dadSMilanka Ringwald  * GMBH 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 
4495cbd947Smatthias.ringwald #include "l2cap_signaling.h"
457907f069SMatthias Ringwald #include "btstack_config.h"
467224be7eSMatthias Ringwald #include "btstack_debug.h"
47b398e4aaS[email protected] #include "hci.h"
4895cbd947Smatthias.ringwald 
49b48a17ffSmatthias.ringwald #include <string.h>
50b48a17ffSmatthias.ringwald 
l2cap_create_signaling_packet(uint8_t * acl_buffer,hci_con_handle_t handle,uint8_t pb_flags,uint16_t cid,L2CAP_SIGNALING_COMMANDS cmd,uint8_t identifier,va_list argptr)51d5a98629SMatthias Ringwald uint16_t l2cap_create_signaling_packet(uint8_t * acl_buffer, hci_con_handle_t handle, uint8_t pb_flags, uint16_t cid, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, va_list argptr){
528334d3d8SMatthias Ringwald 
5395b6ea6eS[email protected]     static const char *l2cap_signaling_commands_format[] = {
545ca8d57bS[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
5514b592b7SMatthias Ringwald             "22",    // 0x02 connection request: psm, source cid
5614b592b7SMatthias Ringwald             "2222",  // 0x03 connection response: destination cid, source cid, result, status
5714b592b7SMatthias Ringwald             "22D",   // 0x04 config request: destination cid, flags, configuration options
5814b592b7SMatthias Ringwald             "222D",  // 0x05 config response: source cid, flags, result, configuration options
5914b592b7SMatthias Ringwald             "22",    // 0x06 disconnection request: destination cid, source cid
6014b592b7SMatthias Ringwald             "22",    // 0x07 disconnection response: destination cid, source CID
6114b592b7SMatthias Ringwald             "D",     // 0x08 echo request: data
6214b592b7SMatthias Ringwald             "D",     // 0x09 echo response: data
6314b592b7SMatthias Ringwald             "2",     // 0x0a information request: info type {1=Connectionless MTU, 2=Extended features supported}
6414b592b7SMatthias Ringwald             "22D",   // 0x0b information response: info type, Result, Data
657224be7eSMatthias Ringwald             NULL,    // 0x0c non-supported AMP command
667224be7eSMatthias Ringwald             NULL,    // 0x0d non-supported AMP command
677224be7eSMatthias Ringwald             NULL,    // 0x0e non-supported AMP command
687224be7eSMatthias Ringwald             NULL,    // 0x0f non-supported AMP command
697224be7eSMatthias Ringwald             NULL,    // 0x10 non-supported AMP command
707224be7eSMatthias Ringwald             NULL,    // 0x11 non-supported AMP command
7114b592b7SMatthias Ringwald             "2222",  // 0x12 connection parameter update request: interval min, interval max, slave latency, timeout multiplier
729e795b48S[email protected]             "2",     // 0x13 connection parameter update response: result
73f74aded9SMatthias Ringwald             "22222", // 0X14 le credit-based connection request: simplified psm, source cid, mtu, mps, initial credits
7414b592b7SMatthias Ringwald             "22222", // 0x15 le credit-based connection response: dest cid, mtu, mps, initial credits, result
7514b592b7SMatthias Ringwald             "22",    // 0x16 le flow control credit indication: source cid, credits
76f74aded9SMatthias Ringwald             "2222C", // 0x17 l2cap credit-based connection request: simplified psm, mtu, mps, initial credits, source cid[0]
77f74aded9SMatthias Ringwald             "2222C", // 0x18 l2cap credit-based connection request: mtu, mps, initial credits, result, destinations cid[0]
78f74aded9SMatthias Ringwald             "22C",   // 0x19 l2cap credit-based reconfigure request: mu, mps, destination cid[0]
79f74aded9SMatthias Ringwald             "2",     // 0x1a l2cap credit-based reconfigure response: result
8015f09267SMilanka Ringwald #ifdef UNIT_TEST
8115f09267SMilanka Ringwald             "M",     // invalid format for unit testing
8215f09267SMilanka Ringwald #endif
8395cbd947Smatthias.ringwald     };
847224be7eSMatthias Ringwald 
85436279c9SMatthias Ringwald     btstack_assert(0 < cmd);
86*3486da78SMatthias Ringwald     uint8_t cmd_index = (uint8_t) cmd;
87*3486da78SMatthias Ringwald     static const uint8_t num_l2cap_commands = (uint8_t) (sizeof(l2cap_signaling_commands_format) / sizeof(const char *));
88*3486da78SMatthias Ringwald     btstack_assert(cmd_index <= num_l2cap_commands);
89c61d23aeSMatthias Ringwald     UNUSED(num_l2cap_commands);
90436279c9SMatthias Ringwald 
91*3486da78SMatthias Ringwald     const char *format = l2cap_signaling_commands_format[cmd_index -1u];
92436279c9SMatthias Ringwald     btstack_assert(format != NULL);
937224be7eSMatthias Ringwald 
94e9772277S[email protected]     // 0 - Connection handle : PB=pb : BC=00
9506a26579SMatthias Ringwald     little_endian_store_16(acl_buffer, 0u, handle | (pb_flags << 12u) | (0u << 14u));
9695cbd947Smatthias.ringwald     // 6 - L2CAP channel = 1
97f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 6, cid);
9895cbd947Smatthias.ringwald     // 8 - Code
99*3486da78SMatthias Ringwald     acl_buffer[8] = cmd_index;
10095cbd947Smatthias.ringwald     // 9 - id (!= 0 sequentially)
10195cbd947Smatthias.ringwald     acl_buffer[9] = identifier;
10295cbd947Smatthias.ringwald 
10395cbd947Smatthias.ringwald     // 12 - L2CAP signaling parameters
10495cbd947Smatthias.ringwald     uint16_t pos = 12;
10595cbd947Smatthias.ringwald     uint16_t word;
106f74aded9SMatthias Ringwald     uint8_t  * ptr_u8;
107f74aded9SMatthias Ringwald     uint16_t * ptr_u16;
10895cbd947Smatthias.ringwald     while (*format) {
10995cbd947Smatthias.ringwald         switch(*format) {
11095cbd947Smatthias.ringwald             case '2': // 16 bit value
111c0dd2f5cSMilanka Ringwald                 word = va_arg(argptr, int);         // LCOV_EXCL_BR_LINE
11295cbd947Smatthias.ringwald                 // minimal va_arg is int: 2 bytes on 8+16 bit CPUs
1134ea43905SMatthias Ringwald                 acl_buffer[pos++] = word & 0xffu;
11495cbd947Smatthias.ringwald                 acl_buffer[pos++] = word >> 8;
11595cbd947Smatthias.ringwald                 break;
11623d9c721SMatthias Ringwald             case 'C': // list of cids != zero, last one is 0xffff
117f74aded9SMatthias Ringwald                 ptr_u16  = va_arg(argptr, uint16_t *);   // LCOV_EXCL_BR_LINE
11823d9c721SMatthias Ringwald                 while (*ptr_u16 != 0xffff){
119f74aded9SMatthias Ringwald                     little_endian_store_16(acl_buffer, pos, *ptr_u16);
120f74aded9SMatthias Ringwald                     ptr_u16++;
121f74aded9SMatthias Ringwald                     pos += 2;
122f74aded9SMatthias Ringwald                 }
123f74aded9SMatthias Ringwald                 break;
12495cbd947Smatthias.ringwald             case 'D': // variable data. passed: len, ptr
125c0dd2f5cSMilanka Ringwald                 word = va_arg(argptr, int);         // LCOV_EXCL_BR_LINE
126f74aded9SMatthias Ringwald                 ptr_u8  = va_arg(argptr, uint8_t *);   // LCOV_EXCL_BR_LINE
127f74aded9SMatthias Ringwald                 (void)memcpy(&acl_buffer[pos], ptr_u8, word);
12895cbd947Smatthias.ringwald                 pos += word;
12995cbd947Smatthias.ringwald                 break;
13095cbd947Smatthias.ringwald             default:
131d7c9cb2bSMilanka Ringwald                 btstack_unreachable();
13295cbd947Smatthias.ringwald                 break;
13395cbd947Smatthias.ringwald         }
13495cbd947Smatthias.ringwald         format++;
13595cbd947Smatthias.ringwald     };
13695cbd947Smatthias.ringwald     va_end(argptr);
13795cbd947Smatthias.ringwald 
138436279c9SMatthias Ringwald     // Fill in various length fields: it's the number of bytes following for ACL length and l2cap parameter length
13995cbd947Smatthias.ringwald     // - the l2cap payload length is counted after the following channel id (only payload)
14095cbd947Smatthias.ringwald 
14195cbd947Smatthias.ringwald     // 2 - ACL length
1424ea43905SMatthias Ringwald     little_endian_store_16(acl_buffer, 2u,  pos - 4u);
14395cbd947Smatthias.ringwald     // 4 - L2CAP packet length
1444ea43905SMatthias Ringwald     little_endian_store_16(acl_buffer, 4u,  pos - 6u - 2u);
14595cbd947Smatthias.ringwald     // 10 - L2CAP signaling parameter length
1464ea43905SMatthias Ringwald     little_endian_store_16(acl_buffer, 10u, pos - 12u);
14795cbd947Smatthias.ringwald 
14895cbd947Smatthias.ringwald     return pos;
14995cbd947Smatthias.ringwald }
150