• 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 #ifndef L2CAP_CMN_H
17 #define L2CAP_CMN_H
18 
19 #include <stdint.h>
20 
21 #include "btstack.h"
22 #include "packet.h"
23 #include "securec.h"
24 
25 #include "alarm.h"
26 #include "list.h"
27 #include "log.h"
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif  // __cplusplus
32 
33 #define L2CAP_HEADER_LENGTH 0x04
34 #define L2CAP_SIGNAL_HEADER_LENGTH 0x04
35 
36 // Packet Boundary Flag
37 #define L2CAP_FIRST_NON_AUTOMATICALLY_FLUSHABLE_PACKET 0x00
38 #define L2CAP_CONTINUING_FRAGMENT 0x01
39 #define L2CAP_FIRST_AUTOMATICALLY_FLUSHABLE_PACKET 0x02
40 #define L2CAP_A_COMPLETE_L2CAP_PDU 0x03
41 
42 #define L2CAP_DEFAULT_RTX (30 * 1000)    // 30s
43 #define L2CAP_DEFAULT_ERTX (180 * 1000)  // 180s
44 
45 #define L2CAP_DISCONNECTION_TIME_OUT 4000
46 
47 // l2cap signal
48 #define L2CAP_COMMAND_REJECT 0x01
49 #define L2CAP_CONNECTION_REQUEST 0x02
50 #define L2CAP_CONNECTION_RESPONSE 0x03
51 #define L2CAP_CONFIGURATION_REQUEST 0x04
52 #define L2CAP_CONFIGURATION_RESPONSE 0x05
53 #define L2CAP_DISCONNECTION_REQUEST 0x06
54 #define L2CAP_DISCONNECTION_RESPONSE 0x07
55 #define L2CAP_ECHO_REQUEST 0x08
56 #define L2CAP_ECHO_RESPONSE 0x09
57 #define L2CAP_INFORMATION_REQUEST 0x0A
58 #define L2CAP_INFORMATION_RESPONSE 0x0B
59 #define L2CAP_CONNECTION_PARAMETER_UPDATE_REQUEST 0x12
60 #define L2CAP_CONNECTION_PARAMETER_UPDATE_RESPONSE 0x13
61 #define L2CAP_LE_CREDIT_BASED_CONNECTION_REQUEST 0x14
62 #define L2CAP_LE_CREDIT_BASED_CONNECTION_RESPONSE 0x15
63 #define L2CAP_LE_FLOW_CONTROL_CREDIT 0x16
64 
65 #define L2CAP_MIN_CID 0x0080
66 #define L2CAP_MAX_CID 0xFFFF
67 #define L2CAP_LE_MIN_CID 0x0040
68 #define L2CAP_LE_MAX_CID 0x007F
69 
70 #define L2CAP_MIN_IDENTIFIER 0x01
71 #define L2CAP_MAX_IDENTIFIER 0xFF
72 
73 #define L2CAP_SIGNALING_CHANNEL 0x0001
74 #define L2CAP_CONNECTIONLESS_CHANNEL 0x0002
75 #define L2CAP_AMP_MANAGER_PROTOCOL 0x0003
76 #define L2CAP_LE_ATTRIBUTE_PROTOCOL 0x0004
77 #define L2CAP_LE_SIGNALING_CHANNEL 0x0005
78 #define L2CAP_LE_SECURITY_MANAGER_PROTOCOL 0x0006
79 #define L2CAP_BREDR_SECURITY_MANAGER 0x0007
80 
81 #define L2CAP_SIGNAL_MTU 672
82 
83 #define L2CAP_MIN_MTU 48
84 #define L2CAP_LE_MIN_MTU 23
85 
86 // Reason of Command Reject
87 #define L2CAP_COMMAND_NOT_UNDERSTOOD 0x0000
88 #define L2CAP_SIGNAL_MTU_EXCEEDED 0x0001
89 #define L2CAP_INVALID_CID_IN_REQUEST 0x0002
90 
91 #define L2CAP_NONE_FLUSH_PACKET 0xFFFF
92 
93 // channel state
94 #define L2CAP_CHANNEL_IDLE 0x00
95 #define L2CAP_CHANNEL_CONNECT_OUT_REQ 0x01
96 #define L2CAP_CHANNEL_CONNECT_IN_REQ 0x02
97 #define L2CAP_CHANNEL_CONFIGING 0x03
98 #define L2CAP_CHANNEL_CONNECTED 0x04
99 #define L2CAP_CHANNEL_DISCONNECT_OUT_REQ 0x05
100 #define L2CAP_CHANNEL_DISCONNECT_IN_REQ 0x06
101 
102 // connection state
103 #define L2CAP_CONNECTION_IDLE 0x00
104 #define L2CAP_CONNECTION_CONNECTING 0x01
105 #define L2CAP_CONNECTION_CONNECTED 0x02
106 #define L2CAP_CONNECTION_DISCONNECTING 0x03
107 
108 // offset
109 #define L2CAP_OFFSET_1 0x01
110 #define L2CAP_OFFSET_2 0x02
111 #define L2CAP_OFFSET_3 0x03
112 #define L2CAP_OFFSET_4 0x04
113 #define L2CAP_OFFSET_5 0x05
114 #define L2CAP_OFFSET_6 0x06
115 #define L2CAP_OFFSET_7 0x07
116 #define L2CAP_OFFSET_8 0x08
117 #define L2CAP_OFFSET_9 0x09
118 #define L2CAP_OFFSET_10 0x0A
119 #define L2CAP_OFFSET_12 0x0C
120 
121 // size
122 #define L2CAP_SIZE_1 0x01
123 #define L2CAP_SIZE_2 0x02
124 #define L2CAP_SIZE_3 0x03
125 #define L2CAP_SIZE_4 0x04
126 #define L2CAP_SIZE_5 0x05
127 #define L2CAP_SIZE_6 0x06
128 #define L2CAP_SIZE_7 0x07
129 #define L2CAP_SIZE_8 0x08
130 #define L2CAP_SIZE_9 0x09
131 #define L2CAP_SIZE_10 0x0A
132 #define L2CAP_SIZE_11 0x0B
133 #define L2CAP_SIZE_12 0x0C
134 #define L2CAP_SIZE_22 0x16
135 
136 // hint option
137 #define L2CAP_OPTION_HINT 0x80
138 
139 typedef struct {
140     uint8_t code;
141     uint8_t identifier;
142     uint16_t length;
143 } L2capSignalHeader;
144 
145 typedef struct {
146     uint16_t lcid;
147     uint8_t code;
148     uint8_t identifier;
149 
150     Alarm *timer;
151 } L2capPendingRequest;
152 
153 typedef struct {
154     int (*aclConnected)(const BtAddr *addr, uint16_t handle, uint8_t status);
155     int (*aclDisconnected)(uint16_t handle, uint8_t status, uint8_t reason);
156     int (*recvL2capPacket)(uint16_t handle, uint16_t cid, Packet *pkt);
157 } L2capBdrCallback;
158 
159 typedef struct {
160     int (*aclConnected)(const BtAddr *addr, uint16_t handle, uint8_t role, uint8_t status);
161     int (*aclDisconnected)(uint16_t handle, uint8_t status, uint8_t reason);
162     int (*recvL2capPacket)(uint16_t handle, uint16_t cid, Packet *pkt);
163 } L2capLeCallback;
164 
165 void L2capCpuToLe16(uint8_t *dst, uint16_t src);
166 
167 uint16_t L2capLe16ToCpu(const uint8_t *src);
168 
169 void *L2capAlloc(int size);
170 void L2capFree(void *p);
171 
172 int L2capAsynchronousProcess(void (*task)(const void *context), void (*destroy)(void *context), void *context);
173 
174 Packet *L2capBuildSignalPacket(uint16_t cid, const L2capSignalHeader *signal, const uint8_t *data);
175 int L2capSendCommandReject(uint16_t handle, uint16_t cid, uint8_t ident, uint16_t reason, uint16_t data[2]);
176 
177 void L2capCreatePendingRequest(
178     List *pendingList, uint16_t lcid, const L2capSignalHeader *signal, uint64_t timeo, AlarmCallback timerExpired);
179 void L2capDestroyPendingRequest(List *pendingList, uint8_t identifier);
180 L2capPendingRequest *L2capGetPendingRequest(List *pendingList, uint8_t identifier);
181 L2capPendingRequest *L2capGetPendingRequest2(List *pendingList, const void *request);
182 void L2capClearPendingRequest(List *pendingList);
183 
184 int L2capSendPacket(uint16_t handle, uint16_t flushTimeout, Packet *pkt);
185 int L2capSendPacketNoFree(uint16_t handle, uint16_t flushTimeout, Packet *pkt);
186 int L2capLeSendPacket(uint16_t handle, Packet *pkt);
187 
188 uint16_t L2capGetTxBufferSize();
189 uint16_t L2capGetRxBufferSize();
190 
191 uint16_t L2capLeGetTxBufferSize();
192 
193 int L2capConnectBdr(const BtAddr *addr);
194 int L2capConnectLe(const BtAddr *addr);
195 int L2capConnectLeCancel(const BtAddr *addr);
196 
197 int L2capAddConnectionRef(uint16_t handle);
198 int L2capDisconnect(uint16_t handle, uint8_t reason);
199 
200 int L2capRegisterBdr(const L2capBdrCallback *cb);
201 int L2capRegisterLe(const L2capLeCallback *cb);
202 
203 void L2capCommonStartup();
204 void L2capCommonShutdown();
205 
206 #ifdef __cplusplus
207 }
208 #endif  // __cplusplus
209 
210 #endif  // L2CAP_CMN_H
211