1 /******************************************************************************
2 *
3 * Copyright 1999-2012 Broadcom Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19 #ifndef L2CDEFS_H
20 #define L2CDEFS_H
21
22 #include <cstdint>
23 /* L2CAP command codes
24 */
25 #define L2CAP_CMD_REJECT 0x01
26 #define L2CAP_CMD_CONN_REQ 0x02
27 #define L2CAP_CMD_CONN_RSP 0x03
28 #define L2CAP_CMD_CONFIG_REQ 0x04
29 #define L2CAP_CMD_CONFIG_RSP 0x05
30 #define L2CAP_CMD_DISC_REQ 0x06
31 #define L2CAP_CMD_DISC_RSP 0x07
32 #define L2CAP_CMD_ECHO_REQ 0x08
33 #define L2CAP_CMD_ECHO_RSP 0x09
34 #define L2CAP_CMD_INFO_REQ 0x0A
35 #define L2CAP_CMD_INFO_RSP 0x0B
36 #define L2CAP_CMD_AMP_CONN_REQ 0x0C
37 #define L2CAP_CMD_AMP_MOVE_REQ 0x0E
38 #define L2CAP_CMD_BLE_UPDATE_REQ 0x12
39 #define L2CAP_CMD_BLE_UPDATE_RSP 0x13
40 #define L2CAP_CMD_BLE_CREDIT_BASED_CONN_REQ 0x14
41 #define L2CAP_CMD_BLE_CREDIT_BASED_CONN_RES 0x15
42 #define L2CAP_CMD_BLE_FLOW_CTRL_CREDIT 0x16
43 /* Enhanced CoC */
44 #define L2CAP_CMD_CREDIT_BASED_CONN_REQ 0x17
45 #define L2CAP_CMD_CREDIT_BASED_CONN_RES 0x18
46 #define L2CAP_CMD_CREDIT_BASED_RECONFIG_REQ 0x19
47 #define L2CAP_CMD_CREDIT_BASED_RECONFIG_RES 0x1A
48
49 /* Define some packet and header lengths
50 */
51 /* Length and CID */
52 #define L2CAP_PKT_OVERHEAD 4
53 /* Cmd code, Id and length */
54 #define L2CAP_CMD_OVERHEAD 4
55 /* Reason (data is optional) */
56 #define L2CAP_CMD_REJECT_LEN 2
57 /* PSM and source CID */
58 #define L2CAP_CONN_REQ_LEN 4
59 /* Dest CID, source CID, reason, status */
60 #define L2CAP_CONN_RSP_LEN 8
61 /* Dest CID, flags (data is optional) */
62 #define L2CAP_CONFIG_REQ_LEN 4
63 /* Dest CID, flags, result,data optional*/
64 #define L2CAP_CONFIG_RSP_LEN 6
65 /* Dest CID, source CID */
66 #define L2CAP_DISC_REQ_LEN 4
67 /* Dest CID, source CID */
68 #define L2CAP_DISC_RSP_LEN 4
69 /* Data is optional */
70 #define L2CAP_ECHO_REQ_LEN 0
71 /* Data is optional */
72 #define L2CAP_ECHO_RSP_LEN 0
73 /* Info type, result (data is optional) */
74 #define L2CAP_INFO_RSP_LEN 4
75
76 /* Min and max interval, latency, tout */
77 #define L2CAP_CMD_BLE_UPD_REQ_LEN 8
78 /* Result */
79 #define L2CAP_CMD_BLE_UPD_RSP_LEN 2
80
81 /* LE_PSM, SCID, MTU, MPS, Init Credit */
82 #define L2CAP_CMD_BLE_CREDIT_BASED_CONN_REQ_LEN 10
83 /* DCID, MTU, MPS, Init credit, Result */
84 #define L2CAP_CMD_BLE_CREDIT_BASED_CONN_RES_LEN 10
85 /* CID, Credit */
86 #define L2CAP_CMD_BLE_FLOW_CTRL_CREDIT_LEN 4
87
88 /* LE PSM, MTU, MPS, Initial Credits, SCIDS[] */
89 #define L2CAP_CMD_CREDIT_BASED_CONN_REQ_MIN_LEN 8
90 /* MTU, MPS, Initial Credits, Result, DCIDS[] */
91 #define L2CAP_CMD_CREDIT_BASED_CONN_RES_MIN_LEN 8
92
93 /* MTU, MPS, DCIDS[] */
94 #define L2CAP_CMD_CREDIT_BASED_RECONFIG_REQ_MIN_LEN 4
95 /* Result */
96 #define L2CAP_CMD_CREDIT_BASED_RECONFIG_RES_LEN 2
97
98 /* Define the packet boundary flags
99 */
100 #define L2CAP_PKT_START_NON_FLUSHABLE 0
101 #define L2CAP_PKT_START 2
102 #define L2CAP_PKT_CONTINUE 1
103 #define L2CAP_PKT_TYPE_SHIFT 12
104
105 /* Define the L2CAP connection result codes
106 */
107 typedef enum : uint16_t {
108 L2CAP_CONN_OK = 0,
109 L2CAP_CONN_PENDING = 1,
110 L2CAP_CONN_NO_PSM = 2,
111 L2CAP_CONN_SECURITY_BLOCK = 3,
112 L2CAP_CONN_NO_RESOURCES = 4,
113 L2CAP_CONN_OTHER_ERROR = 5,
114 L2CAP_CONN_TIMEOUT = 0xEEEE,
115 /* Add a couple of our own for internal use */
116 L2CAP_CONN_NO_LINK = 255,
117 L2CAP_CONN_CANCEL = 256, /* L2CAP connection cancelled */
118 } tL2CAP_CONN;
119
120 /* Define the LE L2CAP Connection Response Result codes
121 */
122 typedef enum : uint8_t {
123 L2CAP_LE_RESULT_CONN_OK = 0,
124 L2CAP_LE_RESULT_NO_PSM = 2,
125 L2CAP_LE_RESULT_NO_RESOURCES = 4,
126 L2CAP_LE_RESULT_INSUFFICIENT_AUTHENTICATION = 5,
127 L2CAP_LE_RESULT_INSUFFICIENT_AUTHORIZATION = 6,
128 L2CAP_LE_RESULT_INSUFFICIENT_ENCRYP_KEY_SIZE = 7,
129 L2CAP_LE_RESULT_INSUFFICIENT_ENCRYP = 8,
130 /* We don't like peer device response */
131 L2CAP_LE_RESULT_INVALID_SOURCE_CID = 9,
132 L2CAP_LE_RESULT_SOURCE_CID_ALREADY_ALLOCATED = 0x0A,
133 L2CAP_LE_RESULT_UNACCEPTABLE_PARAMETERS = 0x0B,
134 L2CAP_LE_RESULT_INVALID_PARAMETERS = 0x0C
135 } tL2CAP_LE_RESULT_CODE;
136
l2cap_le_result_code_text(const tL2CAP_LE_RESULT_CODE & code)137 inline std::string l2cap_le_result_code_text(
138 const tL2CAP_LE_RESULT_CODE& code) {
139 switch (code) {
140 case L2CAP_LE_RESULT_CONN_OK:
141 return std::string("le connection success");
142 case L2CAP_LE_RESULT_NO_PSM:
143 return std::string("le no psm service");
144 case L2CAP_LE_RESULT_NO_RESOURCES:
145 return std::string("le no resources");
146 case L2CAP_LE_RESULT_INSUFFICIENT_AUTHENTICATION:
147 return std::string("le authentication failed");
148 case L2CAP_LE_RESULT_INSUFFICIENT_AUTHORIZATION:
149 return std::string("le authorization failed");
150 case L2CAP_LE_RESULT_INSUFFICIENT_ENCRYP_KEY_SIZE:
151 return std::string("le encryption key size failed");
152 case L2CAP_LE_RESULT_INSUFFICIENT_ENCRYP:
153 return std::string("le encryption failed");
154 case L2CAP_LE_RESULT_INVALID_SOURCE_CID:
155 return std::string("le invalid source channel identifier");
156 case L2CAP_LE_RESULT_SOURCE_CID_ALREADY_ALLOCATED:
157 return std::string("le source channel identifier busy");
158 case L2CAP_LE_RESULT_UNACCEPTABLE_PARAMETERS:
159 return std::string("le unacceptable parameters");
160 case L2CAP_LE_RESULT_INVALID_PARAMETERS:
161 return std::string("invalid parameters");
162 }
163 }
164
165 /* Credit based reconfig results code */
166 #define L2CAP_RECONFIG_SUCCEED 0
167 #define L2CAP_RECONFIG_REDUCTION_MTU_NO_ALLOWED 1
168 #define L2CAP_RECONFIG_REDUCTION_MPS_NO_ALLOWED 2
169 #define L2CAP_RECONFIG_INVALID_DCID 3
170 #define L2CAP_RECONFIG_UNACCAPTED_PARAM 4
171
172 /* Define the L2CAP command reject reason codes
173 */
174 #define L2CAP_CMD_REJ_NOT_UNDERSTOOD 0
175 #define L2CAP_CMD_REJ_MTU_EXCEEDED 1
176 #define L2CAP_CMD_REJ_INVALID_CID 2
177
178 /* L2CAP Predefined CIDs
179 */
180 enum : uint16_t {
181 L2CAP_SIGNALLING_CID = 1,
182 L2CAP_CONNECTIONLESS_CID = 2,
183 L2CAP_AMP_CID = 3,
184 L2CAP_ATT_CID = 4,
185 L2CAP_BLE_SIGNALLING_CID = 5,
186 L2CAP_SMP_CID = 6,
187 L2CAP_SMP_BR_CID = 7,
188 L2CAP_BASE_APPL_CID = 0x0040,
189 };
190
191 /* Fixed Channels mask bits */
192
193 /* Signal channel supported (Mandatory) */
194 #define L2CAP_FIXED_CHNL_SIG_BIT (1 << L2CAP_SIGNALLING_CID)
195
196 /* Connectionless reception */
197 #define L2CAP_FIXED_CHNL_CNCTLESS_BIT (1 << L2CAP_CONNECTIONLESS_CID)
198
199 /* Attribute protocol supported */
200 #define L2CAP_FIXED_CHNL_ATT_BIT (1 << L2CAP_ATT_CID)
201
202 /* BLE Signalling supported */
203 #define L2CAP_FIXED_CHNL_BLE_SIG_BIT (1 << L2CAP_BLE_SIGNALLING_CID)
204
205 /* BLE Security Mgr supported */
206 #define L2CAP_FIXED_CHNL_SMP_BIT (1 << L2CAP_SMP_CID)
207
208 /* Security Mgr over BR supported */
209 #define L2CAP_FIXED_CHNL_SMP_BR_BIT (1 << L2CAP_SMP_BR_CID)
210
211 /* Define the L2CAP configuration result codes
212 */
213 #define L2CAP_CFG_OK 0
214 #define L2CAP_CFG_UNACCEPTABLE_PARAMS 1
215 #define L2CAP_CFG_FAILED_NO_REASON 2
216 #define L2CAP_CFG_UNKNOWN_OPTIONS 3
217 #define L2CAP_CFG_PENDING 4
218
219 static_assert(L2CAP_CONN_OTHER_ERROR != L2CAP_CFG_FAILED_NO_REASON,
220 "Different error code should be provided for Connect error and "
221 "Config error");
222
223 /* Define the L2CAP configuration option types
224 */
225 #define L2CAP_CFG_TYPE_MTU 0x01
226 #define L2CAP_CFG_TYPE_FLUSH_TOUT 0x02
227 #define L2CAP_CFG_TYPE_QOS 0x03
228 #define L2CAP_CFG_TYPE_FCR 0x04
229 #define L2CAP_CFG_TYPE_FCS 0x05
230 #define L2CAP_CFG_TYPE_EXT_FLOW 0x06
231
232 #define L2CAP_CFG_MTU_OPTION_LEN 2 /* MTU option length */
233 #define L2CAP_CFG_FLUSH_OPTION_LEN 2 /* Flush option len */
234 #define L2CAP_CFG_QOS_OPTION_LEN 22 /* QOS option length */
235 #define L2CAP_CFG_FCR_OPTION_LEN 9 /* FCR option length */
236 #define L2CAP_CFG_FCS_OPTION_LEN 1 /* FCR option length */
237 #define L2CAP_CFG_EXT_FLOW_OPTION_LEN 16 /* Extended Flow Spec */
238 #define L2CAP_CFG_OPTION_OVERHEAD 2 /* Type and length */
239
240 /* Configuration Cmd/Rsp Flags mask
241 */
242 #define L2CAP_CFG_FLAGS_MASK_CONT 0x0001 /* Flags mask: Continuation */
243
244 /* FCS Check Option values
245 */
246 #define L2CAP_CFG_FCS_BYPASS 0 /* Bypass the FCS in streaming or ERTM modes */
247 #define L2CAP_CFG_FCS_USE \
248 1 /* Use the FCS in streaming or ERTM modes [default] */
249
250 /* Default values for configuration
251 */
252 #define L2CAP_NO_AUTOMATIC_FLUSH 0xFFFF
253
254 #define L2CAP_DEFAULT_MTU (672)
255 #define L2CAP_DEFAULT_SERV_TYPE 1
256 #define L2CAP_DEFAULT_TOKEN_RATE 0
257 #define L2CAP_DEFAULT_BUCKET_SIZE 0
258 #define L2CAP_DEFAULT_PEAK_BANDWIDTH 0
259 #define L2CAP_DEFAULT_LATENCY 0xFFFFFFFF
260 #define L2CAP_DEFAULT_DELAY 0xFFFFFFFF
261
262 /* Define the L2CAP disconnect result codes
263 */
264 #define L2CAP_DISC_OK 0
265 #define L2CAP_DISC_TIMEOUT 0xEEEE
266
267 /* Define the L2CAP info resp result codes
268 */
269 #define L2CAP_INFO_RESP_RESULT_SUCCESS 0
270 #define L2CAP_INFO_RESP_RESULT_NOT_SUPPORTED 1
271
272 /* Define the info-type fields of information request & response
273 */
274 #define L2CAP_CONNLESS_MTU_INFO_TYPE 0x0001
275 /* Used in Information Req/Response */
276 #define L2CAP_EXTENDED_FEATURES_INFO_TYPE 0x0002
277 /* Used in AMP */
278 #define L2CAP_FIXED_CHANNELS_INFO_TYPE 0x0003
279
280 /* Connectionless MTU size */
281 #define L2CAP_CONNLESS_MTU_INFO_SIZE 2
282 /* Extended features array size */
283 #define L2CAP_EXTENDED_FEATURES_ARRAY_SIZE 4
284 /* Fixed channel array size */
285 #define L2CAP_FIXED_CHNL_ARRAY_SIZE 8
286
287 /* Extended features mask bits
288 */
289 /* Enhanced retransmission mode */
290 #define L2CAP_EXTFEA_ENH_RETRANS 0x00000008
291 /* Streaming Mode */
292 #define L2CAP_EXTFEA_STREAM_MODE 0x00000010
293 /* Optional FCS (if set No FCS desired) */
294 #define L2CAP_EXTFEA_NO_CRC 0x00000020
295 /* Extended flow spec */
296 #define L2CAP_EXTFEA_EXT_FLOW_SPEC 0x00000040
297 /* Fixed channels */
298 #define L2CAP_EXTFEA_FIXED_CHNLS 0x00000080
299 /* Extended Window Size */
300 #define L2CAP_EXTFEA_EXT_WINDOW 0x00000100
301 /* Unicast Connectionless Data Reception */
302 #define L2CAP_EXTFEA_UCD_RECEPTION 0x00000200
303
304 /* Mask for locally supported features used in Information Response
305 * (default to none) */
306 #ifndef L2CAP_EXTFEA_SUPPORTED_MASK
307 #define L2CAP_EXTFEA_SUPPORTED_MASK 0
308 #endif
309
310 /* Mask for LE supported features used in Information Response
311 * (default to none) */
312 #ifndef L2CAP_BLE_EXTFEA_MASK
313 #define L2CAP_BLE_EXTFEA_MASK 0
314 #endif
315
316 /* Define a value that tells L2CAP to use the default HCI ACL buffer size */
317 #define L2CAP_INVALID_ERM_BUF_SIZE 0
318 /* Define a value that tells L2CAP to use the default MPS */
319 #define L2CAP_DEFAULT_ERM_MPS 0x0000
320
321 #define L2CAP_FCR_OVERHEAD 2 /* Control word */
322 #define L2CAP_FCS_LEN 2 /* FCS takes 2 bytes */
323 #define L2CAP_SDU_LEN_OVERHEAD 2 /* SDU length field is 2 bytes */
324 #define L2CAP_SDU_LEN_OFFSET 2 /* SDU length offset is 2 bytes */
325 #define L2CAP_EXT_CONTROL_OVERHEAD 4 /* Extended Control Field */
326 /* length(2), channel(2), control(4), SDU length(2) FCS(2) */
327 #define L2CAP_MAX_HEADER_FCS \
328 (L2CAP_PKT_OVERHEAD + L2CAP_EXT_CONTROL_OVERHEAD + L2CAP_SDU_LEN_OVERHEAD + \
329 L2CAP_FCS_LEN)
330
331 /* TODO: This value can probably be optimized per transport, and per L2CAP
332 * socket type, but this should not bring any big performance improvements. For
333 * LE CoC, it should be biggest multiple of "PDU length" smaller than 0xffff (so
334 * depend on controller buffer size), for Classic, making it multiple of PDU
335 * length and also of the 3DH5 air including the l2cap headers in each packet.
336 */
337 #define L2CAP_SDU_LENGTH_MAX (8080 + 26 - (L2CAP_MIN_OFFSET + 6))
338 constexpr uint16_t L2CAP_SDU_LENGTH_LE_MAX = 0xffff;
339
340 /* SAR bits in the control word
341 */
342 /* Control word to begin with for unsegmented PDU*/
343 #define L2CAP_FCR_UNSEG_SDU 0x0000
344 /* ...for Starting PDU of a semented SDU */
345 #define L2CAP_FCR_START_SDU 0x4000
346 /* ...for ending PDU of a segmented SDU */
347 #define L2CAP_FCR_END_SDU 0x8000
348 /* ...for continuation PDU of a segmented SDU */
349 #define L2CAP_FCR_CONT_SDU 0xc000
350
351 /* Supervisory frame types */
352 /* Supervisory frame - RR */
353 #define L2CAP_FCR_SUP_RR 0x0000
354 /* Supervisory frame - REJ */
355 #define L2CAP_FCR_SUP_REJ 0x0001
356 /* Supervisory frame - RNR */
357 #define L2CAP_FCR_SUP_RNR 0x0002
358 /* Supervisory frame - SREJ */
359 #define L2CAP_FCR_SUP_SREJ 0x0003
360
361 /* Mask to get the SAR bits from control word */
362 #define L2CAP_FCR_SAR_BITS 0xC000
363 /* Bits to shift right to get the SAR bits from ctrl-word */
364 #define L2CAP_FCR_SAR_BITS_SHIFT 14
365
366 /* Mask to check if a PDU is S-frame */
367 #define L2CAP_FCR_S_FRAME_BIT 0x0001
368 /* Mask to get the req-seq from control word */
369 #define L2CAP_FCR_REQ_SEQ_BITS 0x3F00
370 /* Bits to shift right to get the req-seq from ctrl-word */
371 #define L2CAP_FCR_REQ_SEQ_BITS_SHIFT 8
372 /* Mask on get the tx-seq from control word */
373 #define L2CAP_FCR_TX_SEQ_BITS 0x007E
374 /* Bits to shift right to get the tx-seq from ctrl-word */
375 #define L2CAP_FCR_TX_SEQ_BITS_SHIFT 1
376
377 /* F-bit in the control word (Sup and I frames) */
378 #define L2CAP_FCR_F_BIT 0x0080
379 /* P-bit in the control word (Sup frames only) */
380 #define L2CAP_FCR_P_BIT 0x0010
381
382 #define L2CAP_FCR_F_BIT_SHIFT 7
383 #define L2CAP_FCR_P_BIT_SHIFT 4
384
385 /* Mask to get the segmentation bits from ctrl-word */
386 #define L2CAP_FCR_SEG_BITS 0xC000
387 /* Bits to shift right to get the S-bits from ctrl-word */
388 #define L2CAP_FCR_SUP_SHIFT 2
389 /* Mask to get the supervisory bits from ctrl-word */
390 #define L2CAP_FCR_SUP_BITS 0x000C
391
392 /* Initial state of the CRC register */
393 #define L2CAP_FCR_INIT_CRC 0
394 /* Mask for sequence numbers (range 0 - 63) */
395 #define L2CAP_FCR_SEQ_MODULO 0x3F
396
397 #endif
398