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 BLUEKITCHEN
24 * GMBH 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 #define BTSTACK_FILE__ "l2cap_signaling.c"
39
40 /*
41 * l2cap_signaling.h
42 */
43
44 #include "l2cap_signaling.h"
45 #include "btstack_config.h"
46 #include "btstack_debug.h"
47 #include "hci.h"
48
49 #include <string.h>
50
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)51 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){
52
53 static const char *l2cap_signaling_commands_format[] = {
54 "2D", // 0x01 command reject: reason {cmd not understood (0), sig MTU exceeded (2:max sig MTU), invalid CID (4:req CID)}, data len, data
55 "22", // 0x02 connection request: psm, source cid
56 "2222", // 0x03 connection response: destination cid, source cid, result, status
57 "22D", // 0x04 config request: destination cid, flags, configuration options
58 "222D", // 0x05 config response: source cid, flags, result, configuration options
59 "22", // 0x06 disconnection request: destination cid, source cid
60 "22", // 0x07 disconnection response: destination cid, source CID
61 "D", // 0x08 echo request: data
62 "D", // 0x09 echo response: data
63 "2", // 0x0a information request: info type {1=Connectionless MTU, 2=Extended features supported}
64 "22D", // 0x0b information response: info type, Result, Data
65 NULL, // 0x0c non-supported AMP command
66 NULL, // 0x0d non-supported AMP command
67 NULL, // 0x0e non-supported AMP command
68 NULL, // 0x0f non-supported AMP command
69 NULL, // 0x10 non-supported AMP command
70 NULL, // 0x11 non-supported AMP command
71 "2222", // 0x12 connection parameter update request: interval min, interval max, slave latency, timeout multiplier
72 "2", // 0x13 connection parameter update response: result
73 "22222", // 0X14 le credit-based connection request: simplified psm, source cid, mtu, mps, initial credits
74 "22222", // 0x15 le credit-based connection response: dest cid, mtu, mps, initial credits, result
75 "22", // 0x16 le flow control credit indication: source cid, credits
76 "2222C", // 0x17 l2cap credit-based connection request: simplified psm, mtu, mps, initial credits, source cid[0]
77 "2222C", // 0x18 l2cap credit-based connection request: mtu, mps, initial credits, result, destinations cid[0]
78 "22C", // 0x19 l2cap credit-based reconfigure request: mu, mps, destination cid[0]
79 "2", // 0x1a l2cap credit-based reconfigure response: result
80 #ifdef UNIT_TEST
81 "M", // invalid format for unit testing
82 #endif
83 };
84
85 btstack_assert(0 < cmd);
86 uint8_t cmd_index = (uint8_t) cmd;
87 static const uint8_t num_l2cap_commands = (uint8_t) (sizeof(l2cap_signaling_commands_format) / sizeof(const char *));
88 btstack_assert(cmd_index <= num_l2cap_commands);
89 UNUSED(num_l2cap_commands);
90
91 const char *format = l2cap_signaling_commands_format[cmd_index -1u];
92 btstack_assert(format != NULL);
93
94 // 0 - Connection handle : PB=pb : BC=00
95 little_endian_store_16(acl_buffer, 0u, handle | (pb_flags << 12u) | (0u << 14u));
96 // 6 - L2CAP channel = 1
97 little_endian_store_16(acl_buffer, 6, cid);
98 // 8 - Code
99 acl_buffer[8] = cmd_index;
100 // 9 - id (!= 0 sequentially)
101 acl_buffer[9] = identifier;
102
103 // 12 - L2CAP signaling parameters
104 uint16_t pos = 12;
105 uint16_t word;
106 uint8_t * ptr_u8;
107 uint16_t * ptr_u16;
108 while (*format) {
109 switch(*format) {
110 case '2': // 16 bit value
111 word = va_arg(argptr, int); // LCOV_EXCL_BR_LINE
112 // minimal va_arg is int: 2 bytes on 8+16 bit CPUs
113 acl_buffer[pos++] = word & 0xffu;
114 acl_buffer[pos++] = word >> 8;
115 break;
116 case 'C': // list of cids != zero, last one is 0xffff
117 ptr_u16 = va_arg(argptr, uint16_t *); // LCOV_EXCL_BR_LINE
118 while (*ptr_u16 != 0xffff){
119 little_endian_store_16(acl_buffer, pos, *ptr_u16);
120 ptr_u16++;
121 pos += 2;
122 }
123 break;
124 case 'D': // variable data. passed: len, ptr
125 word = va_arg(argptr, int); // LCOV_EXCL_BR_LINE
126 ptr_u8 = va_arg(argptr, uint8_t *); // LCOV_EXCL_BR_LINE
127 (void)memcpy(&acl_buffer[pos], ptr_u8, word);
128 pos += word;
129 break;
130 default:
131 btstack_unreachable();
132 break;
133 }
134 format++;
135 };
136 va_end(argptr);
137
138 // Fill in various length fields: it's the number of bytes following for ACL length and l2cap parameter length
139 // - the l2cap payload length is counted after the following channel id (only payload)
140
141 // 2 - ACL length
142 little_endian_store_16(acl_buffer, 2u, pos - 4u);
143 // 4 - L2CAP packet length
144 little_endian_store_16(acl_buffer, 4u, pos - 6u - 2u);
145 // 10 - L2CAP signaling parameter length
146 little_endian_store_16(acl_buffer, 10u, pos - 12u);
147
148 return pos;
149 }
150