• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 /**
17  * @file l2cap_def.h
18  *
19  * @brief Interface of bluetooth l2cap protocol BR/EDR part
20  *
21  */
22 
23 #ifndef L2CAP_DEF_H
24 #define L2CAP_DEF_H
25 
26 #include <stdint.h>
27 #include "packet.h"
28 #include "btstack.h"
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif  // __cplusplus
33 
34 // -------------------- Below is for BR/EDR ------------------------------
35 
36 #define L2CAP_INFORMATION_TYPE_CONNECTIONLESS_MTU 0x0001  // not supported in this implementation
37 #define L2CAP_INFORMATION_TYPE_EXTENDED_FEATURE 0x0002
38 #define L2CAP_INFORMATION_TYPE_FIXED_CHANNEL 0x0003
39 
40 // the first octet for extended feature mask
41 #define L2CAP_FEATURE_FLOW_CONTROL_MODE 0x01
42 #define L2CAP_FEATURE_RETRANSMISSION_MODE 0x02
43 #define L2CAP_FEATURE_BIDIRECTIONAL_QOS 0x04
44 #define L2CAP_FEATURE_ENHANCED_RETRANSMISSION_MODE 0x08
45 #define L2CAP_FEATURE_STREAMING_MODE 0x10
46 #define L2CAP_FEATURE_FCS_OPTION 0x20
47 #define L2CAP_FEATURE_EXTENDED_FLOW_SPECIFICATION 0x40
48 #define L2CAP_FEATURE_FIXED_CHANNELS 0x80
49 
50 // the second octet for extended feature mask
51 #define L2CAP_FEATURE_EXTENDED_WINDOW_SIZE 0x01
52 #define L2CAP_FEATURE_UNICAST_CONNECTIONLESS_DATA 0x02
53 
54 // the first octet for fixed channels supported
55 #define L2CAP_FEATURE_SIGNALING_CHANNEl 0x02
56 #define L2CAP_FEATURE_CONNECTIONLESS_RECEPTION 0x04
57 #define L2CAP_FEATURE_AMP_MANAGER_PROTOCOL 0x08
58 #define L2CAP_FEATURE_BREDR_SECURITY_MANAGER 0x80
59 
60 // the eighth octet for fixed channels supported
61 #define L2CAP_FEATURE_AMP_TEST_MANAGER 0x80
62 
63 // l2cap mode
64 #define L2CAP_BASIC_MODE 0x00
65 #define L2CAP_ENHANCED_RETRANSMISSION_MODE 0x03
66 #define L2CAP_STREAM_MODE 0x04
67 
68 #define L2CAP_DEFAULT_MTU 672
69 
70 // L2cap connection response result
71 #define L2CAP_CONNECTION_SUCCESSFUL 0x0000
72 #define L2CAP_CONNECTION_PENDING 0x0001
73 #define L2CAP_PSM_NOT_SUPPORTED 0x0002
74 #define L2CAP_SECURITY_BLOCK 0x0003
75 #define L2CAP_NO_RESOURCES_AVAILABLE 0x0004
76 #define L2CAP_INVALID_SOURCE_CID 0x0006
77 #define L2CAP_SOURCE_CID_ALREADY_ALLOCATED 0x0007
78 
79 // L2cap connection response status
80 #define L2CAP_NO_FURTHER_INFORMATION_AVAILABLE 0x0000
81 #define L2CAP_AUTHENTICATION_PENDING 0x0001
82 #define L2CAP_AUTHORIZATION_PENDING 0x0002
83 
84 // L2cap config response result
85 #define L2CAP_SUCCESS 0x0000
86 #define L2CAP_UNACCEPTABLE_PARAMETERS 0x0001
87 #define L2CAP_REJECTED 0x0002
88 #define L2CAP_UNKNOWN_OPTIONS 0x0003
89 #define L2CAP_PENDING 0x0004
90 #define L2CAP_FLOW_SPEC_REJECTED 0x0005
91 
92 #define L2CAP_STATE_COLLISION 0xFF
93 
94 typedef struct {
95     // The requested mode of the link.
96     // Possible values are,
97     // 0x00(Default) - Basic Mode
98     // 0x03 - Enhanced Retransmission mode
99     // 0x04 - Streaming mode
100     uint8_t mode;
101 
102     // Valid in Enhanced Retransmission mode, the value should be set to 0.
103     uint8_t maxTransmit;
104 
105     // Valid in Enhanced Retransmission mode, the value should be set to 0.
106     uint8_t txWindowSize;
107 
108     // Valid in Enhanced Retransmission mode, refer to the size of transmission window.
109     // The value range is 1 to 63
110     // If the value is set to 0, then l2cap will determine the real value.
111     uint8_t rxWindowSize;
112 
113     // Valid in Enhanced Retransmission mode, the value should be set to 0.
114     uint16_t retransmissionTimeout;
115 
116     // Valid in Enhanced Retransmission mode, the value should be set to 0.
117     uint16_t monitorTimeout;
118 
119     // Valid in Enhanced Retransmission mode or Streaming mode, the value should be set to 0.
120     uint16_t mps;
121 } L2capOptionRfc;
122 
123 typedef struct {
124     // The maximum SDU size the sender of this option is capable of accepting for a channel.
125     // The minimum value is 48.
126     // The default value is 672.
127     uint16_t mtu;
128 
129     // The Flush Timeout the sender is going to use.
130     // It should be set to default value if profile/protocol does not specially refer.
131     // Possible values are,
132     // 0x0001 - no retransmissions at the baseband level should be performed.
133     // 0x0002 to 0xFFFE - Flush Timeout in milliseconds used by the baseband.
134     // 0xFFFF(Default) - an infinite amount of retransmissions.
135     uint16_t flushTimeout;
136 
137     // Retransmission and flow control option
138     L2capOptionRfc rfc;
139 
140     // Valid in Enhanced Retransmission mode or Streaming mode, specify the type of Frame Check Sequence (FCS).
141     // It should be set to default value if profile/protocol does not specially refer.
142     // Possible values are,
143     // 0x00 - No FCS
144     // 0x01(Default) - 16-bit FCS
145     uint8_t fcs;
146 } L2capConfigInfo;
147 
148 typedef struct {
149     BtAddr addr;
150     uint16_t handle;
151 } L2capConnectionInfo;
152 
153 typedef struct {
154     // Connection Request packets received
155     void (*recvConnectionReq)(uint16_t lcid, uint8_t id, const L2capConnectionInfo *info, uint16_t lpsm, void *ctx);
156 
157     // Connection Response packet received
158     void (*recvConnectionRsp)(
159         uint16_t lcid, const L2capConnectionInfo *info, uint16_t result, uint16_t status, void *ctx);
160 
161     // Configuration Request packet received
162     void (*recvConfigReq)(uint16_t lcid, uint8_t id, const L2capConfigInfo *cfg, void *ctx);
163 
164     // Configuration Response packet received
165     void (*recvConfigRsp)(uint16_t lcid, const L2capConfigInfo *cfg, uint16_t result, void *ctx);
166 
167     // Disconnection Request packet received
168     void (*recvDisconnectionReq)(uint16_t lcid, uint8_t id, void *ctx);
169 
170     // Disconnection Response packet received
171     void (*recvDisconnectionRsp)(uint16_t lcid, void *ctx);
172 
173     // Disconnected abnormal, such as acl disconnected or link loss
174     void (*disconnectAbnormal)(uint16_t lcid, uint8_t reason, void *ctx);
175 
176     // L2cap data packet received
177     void (*recvData)(uint16_t lcid, Packet *pkt, void *ctx);
178 
179     // In Enhanced Retransmission mode, when RNR received or tx_window overflow, this callback is generated
180     void (*remoteBusy)(uint16_t lcid, uint8_t isBusy, void *ctx);
181 } L2capService;
182 
183 typedef struct {
184     // Echo Request packet received
185     void (*recvEchoReq)(uint16_t aclHandle, uint8_t id, const uint8_t *data, uint16_t dataLen, void *ctx);
186     // Echo Response packet received
187     void (*recvEchoRsp)(uint16_t aclHandle, const uint8_t *data, uint16_t dataLen, void *ctx);
188 } L2capEcho;
189 
190 // -------------------- Below is for LE ------------------------------
191 
192 #define L2CAP_LE_ROLE_MASTER 0x00
193 #define L2CAP_LE_ROLE_SLAVE 0x01
194 
195 #define L2CAP_LE_ATT_CHANNEL 0x0004
196 #define L2CAP_LE_SMP_CHANNEL 0x0006
197 
198 // L2CAP CONNECTION PARAMETER UPDATE RESPONSE result
199 #define L2CAP_LE_CONNECTION_PARAMETERS_ACCEPTED 0x0000
200 #define L2CAP_LE_CONNECTION_PARAMETERS_REJECTED 0x0001
201 
202 // L2cap Credit Based Connection response result
203 #define L2CAP_LE_CONNECTION_SUCCESSFUL 0x0000
204 #define L2CAP_LE_PSM_NOT_SUPPORTED 0x0002
205 #define L2CAP_LE_NO_RESOURCES_AVAILABLE 0x0004
206 #define L2CAP_LE_INSUFFICIENT_AUTHENTICATION 0x0005
207 #define L2CAP_LE_INSUFFICIENT_AUTHORIZATION 0x0006
208 #define L2CAP_LE_INSUFFICIENT_ENCRYPTION_KEY_SIZE 0x0007
209 #define L2CAP_LE_INSUFFICIENT_ENCRYPTION 0x0008
210 #define L2CAP_LE_INVALID_SOURCE_CID 0x0009
211 #define L2CAP_LE_SOURCE_CID_ALREADY_ALLOCATED 0x000A
212 #define L2CAP_LE_UNACCEPTABLE_PARAMETERS 0x000B
213 
214 typedef struct {
215     uint16_t mtu;
216     uint16_t mps;
217     uint16_t credit;
218 } L2capLeConfigInfo;
219 
220 typedef struct {
221     // LE Credit Based Connection Request packet received
222     // Refer to charter 4.22 of Core 5.0
223     void (*recvLeCreditBasedConnectionReq)(
224         uint16_t lcid, uint8_t id, const L2capConnectionInfo *info, const L2capLeConfigInfo *cfg, void *ctx);
225     // LE Credit Based Connection Response packet received
226     // Refer to charter 4.23 of Core 5.0
227     void (*recvLeCreditBasedConnectionRsp)(
228         uint16_t lcid, const L2capConnectionInfo *info, const L2capLeConfigInfo *cfg, uint16_t result, void *ctx);
229 
230     // Disconnection Request packet received
231     // Refer to charter 4.6 of Core 5.0
232     void (*recvLeDisconnectionReq)(uint16_t lcid, uint8_t id, void *ctx);
233     // Disconnection Response packet received
234     // Refer to charter 4.7 of Core 5.0
235     void (*recvLeDisconnectionRsp)(uint16_t lcid, void *ctx);
236 
237     // Disconnected abnormal, such as le acl disconnected or link loss
238     // Refer to HCI specification charter 7.7.5 of Core 5.0
239     void (*leDisconnectAbnormal)(uint16_t lcid, uint8_t reason, void *ctx);
240 
241     // LE data packet received
242     void (*recvLeData)(uint16_t lcid, Packet *pkt, void *ctx);
243 
244     void (*leRemoteBusy)(uint16_t lcid, uint8_t busy, void *ctx);
245 } L2capLeService;
246 
247 typedef struct {
248     uint16_t cid;
249 
250     // LE ACL connected
251     void (*leConnected)(const BtAddr *addr, uint16_t aclHandle, uint8_t role, uint8_t status);
252 
253     // LE ACL disconnected
254     void (*leDisconnected)(uint16_t aclHandle, uint8_t status, uint8_t reason);
255 
256     // LE Fix Channel data received
257     void (*recvLeData)(uint16_t aclHandle, const Packet *pkt);
258 } L2capLeFixChannel;
259 
260 typedef struct {
261     uint16_t connIntervalMin;     // Range: 0x0006 to 0x0C80, Time = N * 1.25 ms, Time Range: 7.5 ms to 4 s.
262     uint16_t connIntervalMax;     // Range: 0x0006 to 0x0C80, Time = N * 1.25 ms, Time Range: 7.5 ms to 4 s.
263     uint16_t connLatency;         // the range 0 to ((supervisionTimeout / (connIntervalMax * 2)) - 1) less than 500.
264     uint16_t supervisionTimeout;  // Range: 0x000A to 0x0C80, Time = N * 10 ms, Time Range: 100 ms to 32 s
265 } L2capLeConnectionParameter;
266 
267 typedef struct {
268     // Connection Parameter Update Request packet received
269     void (*recvLeConnectionParameterUpdateReq)(
270         uint16_t aclHandle, uint8_t id, const L2capLeConnectionParameter *param, void *ctx);
271 
272     // Connection Parameter Update Response packet received
273     void (*recvLeConnectionParameterUpdateRsp)(uint16_t aclHandle, uint16_t result, void *ctx);
274 } L2capLeConnectionParameterUpdate;
275 
276 #ifdef __cplusplus
277 }
278 #endif  // __cplusplus
279 
280 #endif  // L2CAP_DEF_H