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 <bluetooth/log.h>
23 
24 #include <cstdint>
25 #include <string>
26 
27 #include "internal_include/bt_target.h"  // L2CAP_EXTFEA_SUPPORTED_MASK
28 #include "macros.h"
29 
30 /* L2CAP command codes
31 */
32 #define L2CAP_CMD_REJECT 0x01
33 #define L2CAP_CMD_CONN_REQ 0x02
34 #define L2CAP_CMD_CONN_RSP 0x03
35 #define L2CAP_CMD_CONFIG_REQ 0x04
36 #define L2CAP_CMD_CONFIG_RSP 0x05
37 #define L2CAP_CMD_DISC_REQ 0x06
38 #define L2CAP_CMD_DISC_RSP 0x07
39 #define L2CAP_CMD_ECHO_REQ 0x08
40 #define L2CAP_CMD_ECHO_RSP 0x09
41 #define L2CAP_CMD_INFO_REQ 0x0A
42 #define L2CAP_CMD_INFO_RSP 0x0B
43 #define L2CAP_CMD_AMP_CONN_REQ 0x0C
44 #define L2CAP_CMD_AMP_MOVE_REQ 0x0E
45 #define L2CAP_CMD_BLE_UPDATE_REQ 0x12
46 #define L2CAP_CMD_BLE_UPDATE_RSP 0x13
47 #define L2CAP_CMD_BLE_CREDIT_BASED_CONN_REQ 0x14
48 #define L2CAP_CMD_BLE_CREDIT_BASED_CONN_RES 0x15
49 #define L2CAP_CMD_BLE_FLOW_CTRL_CREDIT 0x16
50 /* Enhanced CoC */
51 #define L2CAP_CMD_CREDIT_BASED_CONN_REQ 0x17
52 #define L2CAP_CMD_CREDIT_BASED_CONN_RES 0x18
53 #define L2CAP_CMD_CREDIT_BASED_RECONFIG_REQ 0x19
54 #define L2CAP_CMD_CREDIT_BASED_RECONFIG_RES 0x1A
55 
56 /* Define some packet and header lengths
57 */
58 /* Length and CID                       */
59 #define L2CAP_PKT_OVERHEAD 4
60 /* Cmd code, Id and length              */
61 #define L2CAP_CMD_OVERHEAD 4
62 /* Reason (data is optional)            */
63 #define L2CAP_CMD_REJECT_LEN 2
64 /* PSM and source CID                   */
65 #define L2CAP_CONN_REQ_LEN 4
66 /* Dest CID, source CID, reason, status */
67 #define L2CAP_CONN_RSP_LEN 8
68 /* Dest CID, flags (data is optional)   */
69 #define L2CAP_CONFIG_REQ_LEN 4
70 /* Dest CID, flags, result,data optional*/
71 #define L2CAP_CONFIG_RSP_LEN 6
72 /* Dest CID, source CID                 */
73 #define L2CAP_DISC_REQ_LEN 4
74 /* Dest CID, source CID                 */
75 #define L2CAP_DISC_RSP_LEN 4
76 /* Data is optional                     */
77 #define L2CAP_ECHO_REQ_LEN 0
78 /* Data is optional                     */
79 #define L2CAP_ECHO_RSP_LEN 0
80 /* Info type, result (data is optional) */
81 #define L2CAP_INFO_RSP_LEN 4
82 
83 /* Min and max interval, latency, tout  */
84 #define L2CAP_CMD_BLE_UPD_REQ_LEN 8
85 /* Result                               */
86 #define L2CAP_CMD_BLE_UPD_RSP_LEN 2
87 
88 /* LE_PSM, SCID, MTU, MPS, Init Credit */
89 #define L2CAP_CMD_BLE_CREDIT_BASED_CONN_REQ_LEN 10
90 /* DCID, MTU, MPS, Init credit, Result */
91 #define L2CAP_CMD_BLE_CREDIT_BASED_CONN_RES_LEN 10
92 /* CID, Credit */
93 #define L2CAP_CMD_BLE_FLOW_CTRL_CREDIT_LEN 4
94 
95 /* LE PSM, MTU, MPS, Initial Credits, SCIDS[] */
96 #define L2CAP_CMD_CREDIT_BASED_CONN_REQ_MIN_LEN 8
97 /* MTU, MPS, Initial Credits, Result, DCIDS[] */
98 #define L2CAP_CMD_CREDIT_BASED_CONN_RES_MIN_LEN 8
99 
100 /* MTU, MPS, DCIDS[] */
101 #define L2CAP_CMD_CREDIT_BASED_RECONFIG_REQ_MIN_LEN 4
102 /* Result */
103 #define L2CAP_CMD_CREDIT_BASED_RECONFIG_RES_LEN 2
104 
105 /* Define the packet boundary flags
106 */
107 #define L2CAP_PKT_START_NON_FLUSHABLE 0
108 #define L2CAP_PKT_START 2
109 #define L2CAP_PKT_CONTINUE 1
110 #define L2CAP_PKT_TYPE_SHIFT 12
111 
112 #define L2CAP_CONN_INTERNAL_MASK 0xF000
113 #define L2CAP_CONN_LE_MASK 0xFF00
114 
115 /* Define the LE L2CAP Connection Response Result codes
116  */
117 typedef enum : uint8_t {
118   L2CAP_LE_RESULT_CONN_OK = 0,
119   L2CAP_LE_RESULT_NO_PSM = 2,
120   L2CAP_LE_RESULT_NO_RESOURCES = 4,
121   L2CAP_LE_RESULT_INSUFFICIENT_AUTHENTICATION = 5,
122   L2CAP_LE_RESULT_INSUFFICIENT_AUTHORIZATION = 6,
123   L2CAP_LE_RESULT_INSUFFICIENT_ENCRYP_KEY_SIZE = 7,
124   L2CAP_LE_RESULT_INSUFFICIENT_ENCRYP = 8,
125   /* We don't like peer device response */
126   L2CAP_LE_RESULT_INVALID_SOURCE_CID = 9,
127   L2CAP_LE_RESULT_SOURCE_CID_ALREADY_ALLOCATED = 0x0A,
128   L2CAP_LE_RESULT_UNACCEPTABLE_PARAMETERS = 0x0B,
129   L2CAP_LE_RESULT_INVALID_PARAMETERS = 0x0C
130 } tL2CAP_LE_RESULT_CODE;
131 
132 /* Define the L2CAP connection result codes */
133 typedef enum : uint16_t {
134   L2CAP_CONN_OK = 0,
135   L2CAP_CONN_PENDING = 1,
136   L2CAP_CONN_NO_PSM = 2,
137   L2CAP_CONN_SECURITY_BLOCK = 3,
138   L2CAP_CONN_NO_RESOURCES = 4,
139   L2CAP_CONN_TIMEOUT = 0xEEEE,
140   /* Generic L2CAP conn failure reasons */
141   L2CAP_CONN_OTHER_ERROR = L2CAP_CONN_INTERNAL_MASK,
142   L2CAP_CONN_ACL_CONNECTION_FAILED = L2CAP_CONN_INTERNAL_MASK + 1,
143   L2CAP_CONN_CLIENT_SECURITY_CLEARANCE_FAILED = L2CAP_CONN_INTERNAL_MASK + 2,
144   L2CAP_CONN_NO_LINK = L2CAP_CONN_INTERNAL_MASK + 3,
145   L2CAP_CONN_CANCEL =
146       L2CAP_CONN_INTERNAL_MASK + 4, /* L2CAP connection cancelled */
147   /* For LE result codes converted to L2CAP conn failure code */
148   L2CAP_CONN_INSUFFICIENT_AUTHENTICATION =
149       L2CAP_CONN_LE_MASK + L2CAP_LE_RESULT_INSUFFICIENT_AUTHENTICATION,
150   L2CAP_CONN_INSUFFICIENT_AUTHORIZATION =
151       L2CAP_CONN_LE_MASK + L2CAP_LE_RESULT_INSUFFICIENT_AUTHORIZATION,
152   L2CAP_CONN_INSUFFICIENT_ENCRYP_KEY_SIZE =
153       L2CAP_CONN_LE_MASK + L2CAP_LE_RESULT_INSUFFICIENT_ENCRYP_KEY_SIZE,
154   L2CAP_CONN_INSUFFICIENT_ENCRYP =
155       L2CAP_CONN_LE_MASK + L2CAP_LE_RESULT_INSUFFICIENT_ENCRYP,
156   L2CAP_CONN_INVALID_SOURCE_CID =
157       L2CAP_CONN_LE_MASK + L2CAP_LE_RESULT_INVALID_SOURCE_CID,
158   L2CAP_CONN_SOURCE_CID_ALREADY_ALLOCATED =
159       L2CAP_CONN_LE_MASK + L2CAP_LE_RESULT_SOURCE_CID_ALREADY_ALLOCATED,
160   L2CAP_CONN_UNACCEPTABLE_PARAMETERS =
161       L2CAP_CONN_LE_MASK + L2CAP_LE_RESULT_UNACCEPTABLE_PARAMETERS,
162   L2CAP_CONN_INVALID_PARAMETERS =
163       L2CAP_CONN_LE_MASK + L2CAP_LE_RESULT_INVALID_PARAMETERS,
164 } tL2CAP_CONN;
165 
l2cap_result_code_text(const tL2CAP_CONN & result)166 inline std::string l2cap_result_code_text(const tL2CAP_CONN& result) {
167   switch (result) {
168     CASE_RETURN_TEXT(L2CAP_CONN_OK);
169     CASE_RETURN_TEXT(L2CAP_CONN_PENDING);
170     CASE_RETURN_TEXT(L2CAP_CONN_NO_PSM);
171     CASE_RETURN_TEXT(L2CAP_CONN_SECURITY_BLOCK);
172     CASE_RETURN_TEXT(L2CAP_CONN_NO_RESOURCES);
173     CASE_RETURN_TEXT(L2CAP_CONN_TIMEOUT);
174     CASE_RETURN_TEXT(L2CAP_CONN_OTHER_ERROR);
175     CASE_RETURN_TEXT(L2CAP_CONN_ACL_CONNECTION_FAILED);
176     CASE_RETURN_TEXT(L2CAP_CONN_CLIENT_SECURITY_CLEARANCE_FAILED);
177     CASE_RETURN_TEXT(L2CAP_CONN_NO_LINK);
178     CASE_RETURN_TEXT(L2CAP_CONN_CANCEL);
179     CASE_RETURN_TEXT(L2CAP_CONN_INSUFFICIENT_AUTHENTICATION);
180     CASE_RETURN_TEXT(L2CAP_CONN_INSUFFICIENT_AUTHORIZATION);
181     CASE_RETURN_TEXT(L2CAP_CONN_INSUFFICIENT_ENCRYP_KEY_SIZE);
182     CASE_RETURN_TEXT(L2CAP_CONN_INSUFFICIENT_ENCRYP);
183     CASE_RETURN_TEXT(L2CAP_CONN_INVALID_SOURCE_CID);
184     CASE_RETURN_TEXT(L2CAP_CONN_SOURCE_CID_ALREADY_ALLOCATED);
185     CASE_RETURN_TEXT(L2CAP_CONN_UNACCEPTABLE_PARAMETERS);
186     CASE_RETURN_TEXT(L2CAP_CONN_INVALID_PARAMETERS);
187     default:
188       return std::string("UNKNOWN[") + std::to_string(result) +
189              std::string("]");
190   }
191 }
192 
l2cap_command_code_text(uint8_t cmd)193 static inline std::string l2cap_command_code_text(uint8_t cmd) {
194   switch (cmd) {
195     CASE_RETURN_TEXT(L2CAP_CMD_REJECT);
196     CASE_RETURN_TEXT(L2CAP_CMD_CONN_REQ);
197     CASE_RETURN_TEXT(L2CAP_CMD_CONN_RSP);
198     CASE_RETURN_TEXT(L2CAP_CMD_CONFIG_REQ);
199     CASE_RETURN_TEXT(L2CAP_CMD_CONFIG_RSP);
200     CASE_RETURN_TEXT(L2CAP_CMD_DISC_REQ);
201     CASE_RETURN_TEXT(L2CAP_CMD_DISC_RSP);
202     CASE_RETURN_TEXT(L2CAP_CMD_ECHO_REQ);
203     CASE_RETURN_TEXT(L2CAP_CMD_ECHO_RSP);
204     CASE_RETURN_TEXT(L2CAP_CMD_INFO_REQ);
205     CASE_RETURN_TEXT(L2CAP_CMD_INFO_RSP);
206     CASE_RETURN_TEXT(L2CAP_CMD_AMP_CONN_REQ);
207     CASE_RETURN_TEXT(L2CAP_CMD_AMP_MOVE_REQ);
208     CASE_RETURN_TEXT(L2CAP_CMD_BLE_UPDATE_REQ);
209     CASE_RETURN_TEXT(L2CAP_CMD_BLE_UPDATE_RSP);
210     CASE_RETURN_TEXT(L2CAP_CMD_BLE_CREDIT_BASED_CONN_REQ);
211     CASE_RETURN_TEXT(L2CAP_CMD_BLE_CREDIT_BASED_CONN_RES);
212     CASE_RETURN_TEXT(L2CAP_CMD_BLE_FLOW_CTRL_CREDIT);
213     CASE_RETURN_TEXT(L2CAP_CMD_CREDIT_BASED_CONN_REQ);
214     CASE_RETURN_TEXT(L2CAP_CMD_CREDIT_BASED_CONN_RES);
215     CASE_RETURN_TEXT(L2CAP_CMD_CREDIT_BASED_RECONFIG_REQ);
216     CASE_RETURN_TEXT(L2CAP_CMD_CREDIT_BASED_RECONFIG_RES);
217     default:
218       return std::string("UNKNOWN L2CAP CMD[") + std::to_string(cmd) +
219              std::string("]");
220   }
221 }
222 
to_l2cap_result_code(uint16_t result)223 inline tL2CAP_CONN to_l2cap_result_code(uint16_t result) {
224   return static_cast<tL2CAP_CONN>(result);
225 }
226 
l2cap_le_result_code_text(const tL2CAP_LE_RESULT_CODE & code)227 inline std::string l2cap_le_result_code_text(
228     const tL2CAP_LE_RESULT_CODE& code) {
229   switch (code) {
230     case L2CAP_LE_RESULT_CONN_OK:
231       return std::string("le connection success");
232     case L2CAP_LE_RESULT_NO_PSM:
233       return std::string("le no psm service");
234     case L2CAP_LE_RESULT_NO_RESOURCES:
235       return std::string("le no resources");
236     case L2CAP_LE_RESULT_INSUFFICIENT_AUTHENTICATION:
237       return std::string("le authentication failed");
238     case L2CAP_LE_RESULT_INSUFFICIENT_AUTHORIZATION:
239       return std::string("le authorization failed");
240     case L2CAP_LE_RESULT_INSUFFICIENT_ENCRYP_KEY_SIZE:
241       return std::string("le encryption key size failed");
242     case L2CAP_LE_RESULT_INSUFFICIENT_ENCRYP:
243       return std::string("le encryption failed");
244     case L2CAP_LE_RESULT_INVALID_SOURCE_CID:
245       return std::string("le invalid source channel identifier");
246     case L2CAP_LE_RESULT_SOURCE_CID_ALREADY_ALLOCATED:
247       return std::string("le source channel identifier busy");
248     case L2CAP_LE_RESULT_UNACCEPTABLE_PARAMETERS:
249       return std::string("le unacceptable parameters");
250     case L2CAP_LE_RESULT_INVALID_PARAMETERS:
251       return std::string("invalid parameters");
252   }
253 }
254 
255 /* Credit based reconfig results code */
256 #define L2CAP_RECONFIG_SUCCEED 0
257 #define L2CAP_RECONFIG_REDUCTION_MTU_NO_ALLOWED 1
258 #define L2CAP_RECONFIG_REDUCTION_MPS_NO_ALLOWED 2
259 #define L2CAP_RECONFIG_INVALID_DCID 3
260 #define L2CAP_RECONFIG_UNACCAPTED_PARAM 4
261 
262 /* Define the L2CAP command reject reason codes
263 */
264 #define L2CAP_CMD_REJ_NOT_UNDERSTOOD 0
265 #define L2CAP_CMD_REJ_MTU_EXCEEDED 1
266 #define L2CAP_CMD_REJ_INVALID_CID 2
267 
268 /* L2CAP Predefined CIDs
269 */
270 enum tL2CAP_CID_FIXED : uint16_t {
271   L2CAP_SIGNALLING_CID = 1,
272   L2CAP_CONNECTIONLESS_CID = 2,
273   L2CAP_AMP_CID = 3,
274   L2CAP_ATT_CID = 4,
275   L2CAP_BLE_SIGNALLING_CID = 5,
276   L2CAP_SMP_CID = 6,
277   L2CAP_SMP_BR_CID = 7,
278   L2CAP_BASE_APPL_CID = 0x0040,
279 };
280 
l2cap_cid_fixed_text(const tL2CAP_CID_FIXED & cid)281 inline std::string l2cap_cid_fixed_text(const tL2CAP_CID_FIXED& cid) {
282   switch (cid) {
283     CASE_RETURN_STRING_HEX04(L2CAP_SIGNALLING_CID);
284     CASE_RETURN_STRING_HEX04(L2CAP_CONNECTIONLESS_CID);
285     CASE_RETURN_STRING_HEX04(L2CAP_AMP_CID);
286     CASE_RETURN_STRING_HEX04(L2CAP_ATT_CID);
287     CASE_RETURN_STRING_HEX04(L2CAP_BLE_SIGNALLING_CID);
288     CASE_RETURN_STRING_HEX04(L2CAP_SMP_CID);
289     CASE_RETURN_STRING_HEX04(L2CAP_SMP_BR_CID);
290     CASE_RETURN_STRING_HEX04(L2CAP_BASE_APPL_CID);
291   };
292   RETURN_UNKNOWN_TYPE_STRING(type, cid);
293 }
294 
295 /* Fixed Channels mask bits */
296 
297 /* Signal channel supported (Mandatory) */
298 #define L2CAP_FIXED_CHNL_SIG_BIT (1 << L2CAP_SIGNALLING_CID)
299 
300 /* Connectionless reception */
301 #define L2CAP_FIXED_CHNL_CNCTLESS_BIT (1 << L2CAP_CONNECTIONLESS_CID)
302 
303 /* Attribute protocol supported */
304 #define L2CAP_FIXED_CHNL_ATT_BIT (1 << L2CAP_ATT_CID)
305 
306 /* BLE Signalling supported */
307 #define L2CAP_FIXED_CHNL_BLE_SIG_BIT (1 << L2CAP_BLE_SIGNALLING_CID)
308 
309 /* BLE Security Mgr supported */
310 #define L2CAP_FIXED_CHNL_SMP_BIT (1 << L2CAP_SMP_CID)
311 
312 /* Security Mgr over BR supported */
313 #define L2CAP_FIXED_CHNL_SMP_BR_BIT (1 << L2CAP_SMP_BR_CID)
314 
315 /* Define the L2CAP configuration result codes
316 */
317 typedef enum : uint16_t {
318   L2CAP_CFG_OK = 0,
319   L2CAP_CFG_UNACCEPTABLE_PARAMS = 1,
320   L2CAP_CFG_FAILED_NO_REASON = 2,
321   L2CAP_CFG_UNKNOWN_OPTIONS = 3,
322   L2CAP_CFG_PENDING = 4,
323 } tL2CAP_CFG_RESULT;
324 
325 /* Define the L2CAP configuration option types
326 */
327 #define L2CAP_CFG_TYPE_MTU 0x01
328 #define L2CAP_CFG_TYPE_FLUSH_TOUT 0x02
329 #define L2CAP_CFG_TYPE_QOS 0x03
330 #define L2CAP_CFG_TYPE_FCR 0x04
331 #define L2CAP_CFG_TYPE_FCS 0x05
332 #define L2CAP_CFG_TYPE_EXT_FLOW 0x06
333 
334 #define L2CAP_CFG_MTU_OPTION_LEN 2       /* MTU option length    */
335 #define L2CAP_CFG_FLUSH_OPTION_LEN 2     /* Flush option len     */
336 #define L2CAP_CFG_QOS_OPTION_LEN 22      /* QOS option length    */
337 #define L2CAP_CFG_FCR_OPTION_LEN 9       /* FCR option length    */
338 #define L2CAP_CFG_FCS_OPTION_LEN 1       /* FCR option length    */
339 #define L2CAP_CFG_EXT_FLOW_OPTION_LEN 16 /* Extended Flow Spec   */
340 #define L2CAP_CFG_OPTION_OVERHEAD 2      /* Type and length      */
341 
342 /* Configuration Cmd/Rsp Flags mask
343 */
344 #define L2CAP_CFG_FLAGS_MASK_CONT 0x0001 /* Flags mask: Continuation */
345 
346 /* FCS Check Option values
347 */
348 #define L2CAP_CFG_FCS_BYPASS 0 /* Bypass the FCS in streaming or ERTM modes */
349 #define L2CAP_CFG_FCS_USE \
350   1 /* Use the FCS in streaming or ERTM modes [default] */
351 
352 /* Default values for configuration
353 */
354 #define L2CAP_NO_AUTOMATIC_FLUSH 0xFFFF
355 
356 #define L2CAP_DEFAULT_MTU (672)
357 #define L2CAP_DEFAULT_SERV_TYPE 1
358 #define L2CAP_DEFAULT_TOKEN_RATE 0
359 #define L2CAP_DEFAULT_BUCKET_SIZE 0
360 #define L2CAP_DEFAULT_PEAK_BANDWIDTH 0
361 #define L2CAP_DEFAULT_LATENCY 0xFFFFFFFF
362 #define L2CAP_DEFAULT_DELAY 0xFFFFFFFF
363 
364 /* Define the L2CAP disconnect result codes
365 */
366 #define L2CAP_DISC_OK 0
367 #define L2CAP_DISC_TIMEOUT 0xEEEE
368 
369 /* Define the L2CAP info resp result codes
370 */
371 #define L2CAP_INFO_RESP_RESULT_SUCCESS 0
372 #define L2CAP_INFO_RESP_RESULT_NOT_SUPPORTED 1
373 
374 /* Define the info-type fields of information request & response
375 */
376 #define L2CAP_CONNLESS_MTU_INFO_TYPE 0x0001
377 /* Used in Information Req/Response */
378 #define L2CAP_EXTENDED_FEATURES_INFO_TYPE 0x0002
379 /* Used in AMP                      */
380 #define L2CAP_FIXED_CHANNELS_INFO_TYPE 0x0003
381 
382 /* Connectionless MTU size          */
383 #define L2CAP_CONNLESS_MTU_INFO_SIZE 2
384 /* Extended features array size     */
385 #define L2CAP_EXTENDED_FEATURES_ARRAY_SIZE 4
386 /* Fixed channel array size         */
387 #define L2CAP_FIXED_CHNL_ARRAY_SIZE 8
388 
389 /* Extended features mask bits
390 */
391 /* Enhanced retransmission mode           */
392 #define L2CAP_EXTFEA_ENH_RETRANS 0x00000008
393 /* Streaming Mode                         */
394 #define L2CAP_EXTFEA_STREAM_MODE 0x00000010
395 /* Optional FCS (if set No FCS desired)   */
396 #define L2CAP_EXTFEA_NO_CRC 0x00000020
397 /* Extended flow spec                     */
398 #define L2CAP_EXTFEA_EXT_FLOW_SPEC 0x00000040
399 /* Fixed channels                         */
400 #define L2CAP_EXTFEA_FIXED_CHNLS 0x00000080
401 /* Extended Window Size                   */
402 #define L2CAP_EXTFEA_EXT_WINDOW 0x00000100
403 /* Unicast Connectionless Data Reception  */
404 #define L2CAP_EXTFEA_UCD_RECEPTION 0x00000200
405 
406 /* Mask for locally supported features used in Information Response
407  * (default to none) */
408 #ifndef L2CAP_EXTFEA_SUPPORTED_MASK
409 #define L2CAP_EXTFEA_SUPPORTED_MASK 0
410 #endif
411 
412 /* Mask for LE supported features used in Information Response
413  * (default to none) */
414 #ifndef L2CAP_BLE_EXTFEA_MASK
415 #define L2CAP_BLE_EXTFEA_MASK 0
416 #endif
417 
418 /* Define a value that tells L2CAP to use the default HCI ACL buffer size */
419 #define L2CAP_INVALID_ERM_BUF_SIZE 0
420 /* Define a value that tells L2CAP to use the default MPS */
421 #define L2CAP_DEFAULT_ERM_MPS 0x0000
422 
423 #define L2CAP_FCR_OVERHEAD 2         /* Control word                 */
424 #define L2CAP_FCS_LEN 2              /* FCS takes 2 bytes */
425 #define L2CAP_SDU_LEN_OVERHEAD 2     /* SDU length field is 2 bytes */
426 #define L2CAP_SDU_LEN_OFFSET 2       /* SDU length offset is 2 bytes */
427 #define L2CAP_EXT_CONTROL_OVERHEAD 4 /* Extended Control Field       */
428 /* length(2), channel(2), control(4), SDU length(2) FCS(2) */
429 #define L2CAP_MAX_HEADER_FCS                                                  \
430   (L2CAP_PKT_OVERHEAD + L2CAP_EXT_CONTROL_OVERHEAD + L2CAP_SDU_LEN_OVERHEAD + \
431    L2CAP_FCS_LEN)
432 
433 /* TODO: This value can probably be optimized per transport, and per L2CAP
434  * socket type, but this should not bring any big performance improvements. For
435  * LE CoC, it should be biggest multiple of "PDU length" smaller than 0xffff (so
436  * depend on controller buffer size), for Classic, making it multiple of PDU
437  * length and also of the 3DH5 air including the l2cap headers in each packet.
438  */
439 #define L2CAP_SDU_LENGTH_MAX (8080 + 26 - (L2CAP_MIN_OFFSET + 6))
440 constexpr uint16_t L2CAP_SDU_LENGTH_LE_MAX = 0xffff;
441 
442 /* SAR bits in the control word
443 */
444 /* Control word to begin with for unsegmented PDU*/
445 #define L2CAP_FCR_UNSEG_SDU 0x0000
446 /* ...for Starting PDU of a semented SDU */
447 #define L2CAP_FCR_START_SDU 0x4000
448 /* ...for ending PDU of a segmented SDU */
449 #define L2CAP_FCR_END_SDU 0x8000
450 /* ...for continuation PDU of a segmented SDU */
451 #define L2CAP_FCR_CONT_SDU 0xc000
452 
453 /* Supervisory frame types */
454 /* Supervisory frame - RR                          */
455 #define L2CAP_FCR_SUP_RR 0x0000
456 /* Supervisory frame - REJ                         */
457 #define L2CAP_FCR_SUP_REJ 0x0001
458 /* Supervisory frame - RNR                         */
459 #define L2CAP_FCR_SUP_RNR 0x0002
460 /* Supervisory frame - SREJ                        */
461 #define L2CAP_FCR_SUP_SREJ 0x0003
462 
463 /* Mask to get the SAR bits from control word */
464 #define L2CAP_FCR_SAR_BITS 0xC000
465 /* Bits to shift right to get the SAR bits from ctrl-word */
466 #define L2CAP_FCR_SAR_BITS_SHIFT 14
467 
468 /* Mask to check if a PDU is S-frame */
469 #define L2CAP_FCR_S_FRAME_BIT 0x0001
470 /* Mask to get the req-seq from control word */
471 #define L2CAP_FCR_REQ_SEQ_BITS 0x3F00
472 /* Bits to shift right to get the req-seq from ctrl-word */
473 #define L2CAP_FCR_REQ_SEQ_BITS_SHIFT 8
474 /* Mask on get the tx-seq from control word */
475 #define L2CAP_FCR_TX_SEQ_BITS 0x007E
476 /* Bits to shift right to get the tx-seq from ctrl-word */
477 #define L2CAP_FCR_TX_SEQ_BITS_SHIFT 1
478 
479 /* F-bit in the control word (Sup and I frames)  */
480 #define L2CAP_FCR_F_BIT 0x0080
481 /* P-bit in the control word (Sup frames only)   */
482 #define L2CAP_FCR_P_BIT 0x0010
483 
484 #define L2CAP_FCR_F_BIT_SHIFT 7
485 #define L2CAP_FCR_P_BIT_SHIFT 4
486 
487 /* Mask to get the segmentation bits from ctrl-word */
488 #define L2CAP_FCR_SEG_BITS 0xC000
489 /* Bits to shift right to get the S-bits from ctrl-word */
490 #define L2CAP_FCR_SUP_SHIFT 2
491 /* Mask to get the supervisory bits from ctrl-word */
492 #define L2CAP_FCR_SUP_BITS 0x000C
493 
494 /* Initial state of the CRC register */
495 #define L2CAP_FCR_INIT_CRC 0
496 /* Mask for sequence numbers (range 0 - 63) */
497 #define L2CAP_FCR_SEQ_MODULO 0x3F
498 
499 namespace fmt {
500 template <>
501 struct formatter<tL2CAP_CONN> : enum_formatter<tL2CAP_CONN> {};
502 template <>
503 struct formatter<tL2CAP_CID_FIXED> : enum_formatter<tL2CAP_CID_FIXED> {};
504 }  // namespace fmt
505 
506 #endif
507