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