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_INST_H
17 #define L2CAP_INST_H
18 
19 #include <stdint.h>
20 
21 #include "l2cap_def.h"
22 
23 #include "alarm.h"
24 #include "list.h"
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif  // __cplusplus
29 
30 #define L2CAP_DEFAULT_RETRANSMISSION_TIMEOUT 2000  // 2s
31 #define L2CAP_DEFAULT_MONITOR_TIMEOUT 12000        // 12s
32 #define L2CAP_DEFAULT_TX_WINDOW 0x08
33 #define L2CAP_DEFAULT_MAX_TRANSMIT 0x03
34 
35 // information_state
36 #define L2CAP_INFO_STATE_NONE 0x00
37 #define L2CAP_INFO_STATE_PROCESSING 0x01
38 #define L2CAP_INFO_STATE_DONE 0x02
39 
40 // configuration state
41 #define L2CAP_CONFIG_STATE_IN_DONE 0x01
42 #define L2CAP_CONFIG_STATE_OUT_DONE 0x02
43 
44 // busy state, bit used
45 #define L2CAP_BUSY_LOCAL_BUSY 0x01
46 #define L2CAP_BUSY_REMOTE_BUSY 0x02
47 #define L2CAP_BUSY_WAIT_F 0x40
48 #define L2CAP_BUSY_REMOTE_RNR 0x80
49 
50 typedef struct {
51     uint8_t state;
52     uint8_t extendedFeature[4];
53     uint8_t fixedChannel[8];
54 } L2capInformation;
55 
56 typedef struct {
57     uint16_t lpsm;
58     L2capService service;
59 
60     void *ctx;
61 } L2capPsm;
62 
63 typedef struct {
64     uint8_t *options;
65     uint16_t length;
66 } L2capOptions;
67 
68 typedef struct {
69     Packet *pkt;
70     uint8_t retryCount;
71 } L2capErfcTxPacket;
72 
73 typedef struct {
74     // I-frames contain TxSeq, the send sequence number of the I-frame
75     uint16_t txSeq;
76 
77     // the Sequence number to be used in the next new I-frame transmitted.
78     uint16_t nextTxSeq;
79 
80     // the Sequence number of the next I-frame expected to be acknowledged by the receiving peer.
81     uint16_t expectedAckSeq;
82 
83     // the value of TxSeq expected in the next I-frame.
84     uint16_t expectedTxSeq;
85 
86     // When segmented I-frames are buffered this is used to delay acknowledgment of received
87     // I-frame so that new I-frame transmissions do not cause buffer overflow.
88     uint16_t bufferSeq;
89 
90     uint8_t busyState;
91     uint8_t rejState;
92 
93     Alarm *retransmissionTimer;
94     Alarm *monitorTimer;
95     uint8_t retryCount;
96 
97     List *txList;
98     Packet *rxSarPacket;
99 } L2capErfc;
100 
101 typedef struct {
102     // first octet
103     uint8_t type : 1;
104     uint8_t txSeq : 6;
105     uint8_t fBit : 1;
106 
107     // second octet
108     uint8_t reqSeq : 6;
109     uint8_t sar : 2;
110 } L2capErfcIControl;
111 
112 typedef struct {
113     // first octet
114     uint8_t type : 1;
115     uint8_t reserved1 : 1;
116     uint8_t sBit : 2;
117     uint8_t pBit : 1;
118     uint8_t reserved2 : 2;
119     uint8_t fBit : 1;
120 
121     // second octet
122     uint8_t reqSeq : 6;
123     uint8_t reserved3 : 2;
124 } L2capErfcSControl;
125 
126 typedef struct {
127     uint16_t lcid;
128     uint16_t rcid;
129 
130     uint16_t lpsm;
131     uint16_t rpsm;
132 
133     uint8_t connIdentifier;
134     uint8_t state;
135 
136     L2capOptions part;
137 
138     uint8_t cfgState;
139     L2capConfigInfo lcfg;
140     L2capConfigInfo rcfg;
141 
142     L2capErfc erfc;
143 } L2capChannel;
144 
145 typedef struct {
146     uint16_t aclHandle;
147     BtAddr addr;
148 
149     uint8_t state;
150 
151     uint8_t nextIdentifier;
152 
153     L2capInformation info;
154 
155     Alarm *discTimer;
156 
157     List *chanList;  // Pack struct L2capChannel
158 
159     List *pendingList;  // Pack struct L2capPendingRequest
160 } L2capConnection;
161 
162 typedef struct {
163     L2capEcho cb;
164     void *ctx;
165 } L2capEchoContext;
166 
167 typedef struct {
168     uint16_t nextLcid;
169 
170     List *psmList;   // Pack struct L2capPsm
171     List *connList;  // Pack struct L2capConnection
172 
173     L2capEchoContext echo;
174 } L2capInstance;
175 
176 int L2capInitialized();
177 L2capInstance *L2capGetInstance();
178 
179 L2capPsm *L2capGetPsm(uint16_t lpsm);
180 L2capConnection *L2capGetConnection(uint16_t aclHandle);
181 L2capConnection *L2capGetConnection2(const BtAddr *addr);
182 L2capConnection *L2capGetConnection3(const L2capChannel *chan);
183 
184 L2capChannel *L2capGetChannel(const L2capConnection *conn, int16_t lcid);
185 void L2capGetChannel2(uint16_t lcid, L2capConnection **conn, L2capChannel **chan);
186 void L2capGetChannel3(uint16_t aclHandle, uint16_t lcid, L2capConnection **conn, L2capChannel **chan);
187 
188 L2capChannel *L2capNewChannel(L2capConnection *conn, uint16_t lpsm, uint16_t rpsm);
189 void L2capDestroyChannel(L2capChannel *chan);
190 void L2capDeleteChannel(L2capConnection *conn, L2capChannel *chan, uint16_t removeAcl);
191 L2capConnection *L2capNewConnection(const BtAddr *addr, uint16_t aclHandle);
192 void L2capDeleteConnection(L2capConnection *conn);
193 
194 #ifdef __cplusplus
195 }
196 #endif  // __cplusplus
197 
198 #endif  // L2CAP_INST_H
199