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 att_common.h
18  *
19  * @brief declare common function to be called.
20  *
21  */
22 
23 #ifndef ATT_COMMON_H
24 #define ATT_COMMON_H
25 
26 #include "att.h"
27 
28 #include <stdint.h>
29 #include <string.h>
30 
31 #include "alarm.h"
32 #include "btstack.h"
33 #include "list.h"
34 #include "packet.h"
35 
36 #include "l2cap_if.h"
37 #include "l2cap_le_if.h"
38 
39 #include "gap_if.h"
40 #include "gap_le_if.h"
41 
42 #include "securec.h"
43 
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47 
48 #define BT_PSM_ATT 0x001F
49 
50 #define LE_CID 0x04
51 
52 #define MAXCONNECT 22
53 
54 #define GAPSIGNATURESIZE 12
55 
56 #define ADDRESSLEN 6
57 #define UUID128LEN 16
58 
59 #define DEFAULTBREDRMTU 48
60 #define DEFAULTLEATTMTU 23
61 
62 #define STEP_TWO 2
63 #define STEP_THREE 3
64 #define STEP_FOUR 4
65 #define STEP_FIVE 5
66 #define STEP_SIX 6
67 #define STEP_FIFTEEN 15
68 
69 #define MAXREADBYTYPERESLEN 253
70 #define MAXREADBYGROUPRESLEN 251
71 
72 // initiative connect and passive connect flag
73 #define INITIATIVECONNECT 1
74 #define PASSIVECONNECT 2
75 
76 // timeout
77 #define CONNECTTIMEOUT 40000
78 #define INSTRUCTIONTIMEOUT 30000
79 
80 // attribute type len
81 #define UUID16BITTYPELEN 2
82 #define UUID128BITTYPELEN 16
83 
84 #define HANDLEAND16BITBLUETOOTHUUID 0x01
85 #define HANDLEAND128BITUUID 0x02
86 
87 #define READBYTYPERESPONSEFREE 1
88 #define READBYGROUPTYPERESPONSEFREE 2
89 
90 #define FINDINFORRESINFOR16BITLEN 4
91 #define FINDINFORRESINFOR128BITLEN 18
92 
93 // execute write request
94 #define IMMEDIATELY_WRITE_ALL_PENDING_PREPARED_VALUES 1
95 #define CANCEL_ALL_PREPARED_WRITES 0
96 
97 typedef struct AttConnectInfo {
98     uint16_t aclHandle;
99     union {
100         uint16_t bredrcid;
101         uint16_t lecid;
102     } AttConnectID;
103     uint16_t retGattConnectHandle;
104     uint8_t transportType;
105     BtAddr addr;
106     uint16_t mtu;
107     uint16_t sendMtu;
108     uint16_t receiveMtu;
109     bool mtuFlag;
110     uint8_t initPassConnFlag;
111     List *instruct;
112     Alarm *alarm;
113     bool serverSendFlag;
114 } AttConnectInfo;
115 
116 typedef struct AttConnectingInfo {
117     uint16_t aclHandle;
118     uint16_t cid;
119     uint8_t id;
120     uint16_t mtu;
121     L2capConfigInfo locall2capConfigInfoObj;
122     L2capConfigInfo remotel2capConfigInfoObj;
123     BtAddr addr;
124     uint16_t connectHandle;
125     uint8_t initiativeConnectStatus;
126     uint8_t passiveConnectSatatus;
127     uint8_t initPassConnFlag;  // 1:initiative connect; 2:passive connect
128     Alarm *bredrAlarm;
129     Alarm *leAlarm;
130     uint8_t transportType;
131 } AttConnectingInfo;
132 
133 typedef void (*recvDataFunction)(AttConnectInfo *connect, const Buffer *buffer);
134 
135 typedef struct {
136     attCallback attClientCallback;
137     void *context;
138 } AttClientDataCallback;
139 
140 typedef struct {
141     attCallback attServerCallback;
142     void *context;
143 } AttServerDataCallback;
144 
145 typedef struct {
146     AttConnectCallback attConnect;
147     void *context;
148 } AttConnectedCallback;
149 
150 typedef struct {
151     uint16_t connectHandle;
152     AttError *ATTErrorPtr;
153 } ErrorResponseAsync;
154 
155 typedef struct {
156     uint16_t connectHandle;
157     uint16_t mtu;
158 } ExchangeMTUAsync;
159 
160 typedef struct {
161     uint16_t connectHandle;
162     AttHandleRange attHandleRange;
163 } FindInformationRequestAsync;
164 
165 typedef struct {
166     uint16_t connectHandle;
167     AttFindInformationRsp attFindInformationResContext;
168 } FindInformationResponseAsync;
169 
170 typedef struct {
171     uint16_t connectHandle;
172     AttFindByTypeValueReq *attFindByTypePtreve;
173     Buffer *attValue;
174 } FindByTypeValueRequestAsync;
175 
176 typedef struct {
177     uint16_t connectHandle;
178     AttFindByTypeValueRsp attFindByTypeResContext;
179 } FindByTypeValueResponseAsync;
180 
181 typedef struct {
182     uint16_t connectHandle;
183     AttReadByTypeReq attReadByTypeReqContext;
184 } ReadByTypeRequestAsync;
185 
186 typedef struct {
187     uint16_t connectHandle;
188     AttReadByTypeRsp attReadByTypeRspContext;
189 } ReadByTypeResponseAsync;
190 
191 typedef struct {
192     uint16_t connectHandle;
193     uint16_t attHandle;
194 } ReadRequestAsync;
195 
196 typedef struct {
197     uint16_t connectHandle;
198     Buffer *attValue;
199 } ReadResponseAsync;  // readresponse / readblobresponse / readmultipleresponse / readmultiplerequest
200 
201 typedef struct {
202     uint16_t connectHandle;
203     AttReadBlobReqPrepareWriteValue attReadBlobContext;
204 } ReadBlobRequestAsync;
205 
206 typedef struct {
207     uint16_t connectHandle;
208     AttReadByTypeReq attReadGroupContext;
209 } ReadByGroupTypeRequesAsync;
210 
211 typedef struct {
212     uint16_t connectHandle;
213     AttReadGroupRes attReadGroupResContext;
214 } ReadByGroupTypeResponseAsync;
215 
216 typedef struct {
217     uint16_t connectHandle;
218     uint16_t attHandle;
219     Buffer *attValue;
220 } WriteAsync;  // writerequest / writecommand / signedwritecommand / handlenotification / handleindication
221 
222 typedef struct {
223     uint16_t connectHandle;
224 } WriteResponseAsync;  // writeresponse / executewriterresponse / handleconfirmation
225 
226 typedef struct {
227     uint16_t connectHandle;
228     AttReadBlobReqPrepareWriteValue attReadBlobObj;
229     Buffer *attValue;
230 } PrepareWriteAsync;  // preparewriterequest / preparewriteresponse
231 
232 typedef struct {
233     uint16_t connectHandle;
234     uint8_t flag;
235 } ExecuteWriteRequestAsync;
236 
237 typedef struct SigedWriteCommandGenerationContext {
238     AttConnectInfo *connect;
239     Packet *packet;
240     uint8_t *data;
241     uint16_t bufferSize;
242 } SigedWriteCommandGenerationContext;
243 
244 typedef struct AttGapSignatureGenerationContext {
245     GAP_SignatureResult result;
246     uint8_t *signaturePtr;
247     uint16_t signatureLen;
248     void *context;
249 } AttGapSignatureGenerationContext;
250 
251 /**
252  * @brief get AttConnectInfo information.
253  *
254  * @return Returns the pointer to AttConnectInfo.
255  */
256 AttConnectInfo *AttGetConnectStart();
257 
258 /**
259  * @brief lookup AttConnectInfo info by aclHandle.
260  *
261  * @param1 aclHandle Indicates the aclHandle.
262  * @param2 connect Indicates the second rank pointer to AttConnectInfo.
263  */
264 void AttGetConnectInfoIndexByAclHandle(uint16_t aclHandle, AttConnectInfo **connect);
265 
266 /**
267  * @brief lookup AttConnectInfo info by cid.
268  *
269  * @param1 cid Indicates the cid.
270  * @param2 connect Indicates the second rank pointer to AttConnectInfo.
271  */
272 void AttGetConnectInfoIndexByCid(uint16_t cid, AttConnectInfo **connect);
273 
274 /**
275  * @brief lookup AttConnectInfo info by cid and output parameter index.
276  *
277  * @param1 cid Indicates the cid.
278  * @param2 index Indicates the pointer to index.
279  * @param3 connect Indicates second rank pointer to AttConnectInfo.
280  */
281 void AttGetConnectInfoIndexByCidOutIndex(uint16_t cid, uint16_t *index, AttConnectInfo **connect);
282 
283 /**
284  * @brief lookup AttConnectInfo info by connectHandle and output parameter index.
285  *
286  * @param1 connectHandle Indicates the connectHandle.
287  * @param2 index Indicates the pointer to index.
288  * @param3 connect Indicates second rank pointer to AttConnectInfo.
289  */
290 void AttGetConnectInfoIndexByConnectHandle(uint16_t connectHandle, uint16_t *index, AttConnectInfo **connect);
291 
292 /**
293  * @brief get AttConnectingInfo information.
294  *
295  * @return Returns the pointer to AttConnectingInfo.
296  */
297 AttConnectingInfo *AttGetConnectingStart();
298 
299 /**
300  * @brief lookup AttConnectingInfo info by cid.
301  *
302  * @param1 cid Indicates the cid.
303  * @param2 connecting Indicates the second rank pointer to AttConnectingInfo.
304  */
305 void AttGetConnectingIndexByCid(uint16_t cid, AttConnectingInfo **connecting);
306 
307 /**
308  * @brief lookup AttConnectingInfo info by cid and output parameter index.
309  *
310  * @param1 cid Indicates the cid.
311  * @param2 index Indicates the pointer to index.
312  * @param3 connecting Indicates the second rank pointer to AttConnectingInfo.
313  */
314 void AttGetConnectingIndexByCidOutIndex(uint16_t cid, uint16_t *index, AttConnectingInfo **connecting);
315 
316 /**
317  * @brief lookup AttConnectingInfo info by connectHandle.
318  *
319  * @param1 connectHandle Indicates the connectHandle.
320  * @param2 connecting Indicates the second rank pointer to AttConnectingInfo.
321  */
322 void AttGetConnectingIndexByConnectHandle(uint16_t connectHandle, AttConnectingInfo **connecting);
323 
324 /**
325  * @brief lookup AttConnectingInfo info by cid and connectHandle, result to output parameter index.
326  *
327  * @param1 cid Indicates the cid.
328  * @param2 connectHandle Indicates the connectHandle.
329  * @param3 index Indicates the pointer to index.
330  * @param4 connecting Indicates second rank pointer to AttConnectingInfo.
331  */
332 void AttGetConnectingIndexByCidConnectHandle(
333     uint16_t cid, uint16_t connectHandle, uint16_t *index, AttConnectingInfo **connecting);
334 
335 /**
336  * @brief lookup AttConnectingInfo info by addr.
337  *
338  * @param1 addr Indicates pointer to addr.
339  * @param2 connecting Indicates the second rank pointer to AttConnectingInfo.
340  */
341 void AttGetConnectingIndexByAddr(const BtAddr *addr, AttConnectingInfo **connecting);
342 
343 /**
344  * @brief lookup AttConnectingInfo info by addr uninitialized cid.
345  *
346  * @param1 addr Indicates pointer to addr.
347  * @param2 connecting Indicates the second rank pointer to AttConnectingInfo.
348  */
349 void AttGetConnectingIndexByAddrUninitializedCid(const BtAddr *addr, AttConnectingInfo **connecting);
350 
351 /**
352  * @brief lookup AttConnectingInfo info by addr aclhandle cid.
353  *
354  * @param1 addr Indicates pointer to addr.
355  * @param2 addr Indicates the aclHandle.
356  * @param3 addr Indicates the cid.
357  * @param4 connecting Indicates the second rank pointer to AttConnectingInfo.
358  */
359 void AttGetConnectingIndexByAddrAclhandleCid(
360     const BtAddr *addr, uint16_t aclHandle, uint16_t cid, AttConnectingInfo **connecting);
361 
362 /**
363  * @brief get AttClientDataCallback information.
364  *
365  * @return Returns the pointer to AttClientDataCallback.
366  */
367 AttClientDataCallback *AttGetATTClientCallback();
368 
369 /**
370  * @brief get AttServerDataCallback information.
371  *
372  * @return Returns the pointer to AttServerDataCallback.
373  */
374 AttServerDataCallback *AttGetATTServerCallback();
375 
376 /**
377  * @brief initiative execut instructions by Scheduling.
378  *
379  * @param connect Indicates the pointer to AttConnectInfo.
380  * @return Returns <b>0</b> if the operation is successful; returns <b>!0</b> if the operation fails.
381  */
382 int AttSendSequenceScheduling(const AttConnectInfo *connect);
383 
384 /**
385  * @brief execut instructions by Scheduling after receiving response.
386  *
387  * @param connect Indicates the pointer to AttConnectInfo.
388  */
389 void AttReceiveSequenceScheduling(const AttConnectInfo *connect);
390 
391 /**
392  * @brief client call back copy.
393  *
394  * @param1 attSendDataCB Indicates the pointer to attSendDataCallback.
395  * @param2 context Indicates the pointer to context.
396  */
397 void AttClientCallBackCopyToCommon(attSendDataCallback attSendDataCB, const void *context);
398 
399 /**
400  * @brief server call back copy.
401  *
402  * @param1 attSendDataCB Indicates the pointer to attSendDataCallback.
403  * @param2 context Indicates the pointer to context.
404  */
405 void AttServerCallBackCopyToCommon(attSendDataCallback attSendDataCB, const void *context);
406 /**
407  * @brief receive senddata callback.
408  *
409  * @param1 aclHandle Indicates the aclHandle.
410  * @param2 result Indicates the result.
411  */
412 void LeRecvSendDataCallback(uint16_t aclHandle, int result);
413 
414 /**
415  * @brief bredr receive senddata callback.
416  *
417  * @param1 aclHandle Indicates the aclHandle.
418  * @param2 result Indicates the result.
419  * @param3 cb Indicates the pointer to cb.
420  */
421 void BREDRRecvSendDataCallback(uint16_t lcid, int result);
422 
423 /**
424  * @brief receive delect callback.
425  *
426  */
427 void AttCallBackDelectCopyToCommon();
428 
429 /**
430  * @brief switch thread.
431  *
432  * @param1 callback Indicates the pointer to function pointer.
433  * @param2 destroyCallback Indicates the pointer to function pointer.
434  * @param3 context Indicates the pointer to context.
435  */
436 void AttAsyncProcess(
437     void (*callback)(const void *context), void (*destroyCallback)(const void *context), const void *context);
438 
439 /**
440  * @brief shut down clear connect information.
441  *
442  * @param connectInfo Indicates the pointer to AttConnectInfo.
443  */
444 void AttShutDownClearConnectInfo(AttConnectInfo *connectInfo);
445 
446 /**
447  * @brief client call back btbadparam.
448  *
449  * @param connect Indicates the pointer to AttConnectInfo.
450  */
451 void ClientCallbackBTBADPARAM(const AttConnectInfo *connect);
452 
453 /**
454  * @brief server call back btbadparam.
455  *
456  * @param connect Indicates the pointer to AttConnectInfo.
457  */
458 void ServerCallbackBTBADPARAM(const AttConnectInfo *connect);
459 
460 /**
461  * @brief client call back return value.
462  *
463  * @param1 ret Indicates the ret.
464  * @param2 connect Indicates the pointer to AttConnectInfo.
465  */
466 void ClientCallbackReturnValue(int ret, const AttConnectInfo *connect);
467 
468 /**
469  * @brief server call back return value.
470  *
471  * @param1 ret Indicates the ret.
472  * @param2 connect Indicates the pointer to AttConnectInfo.
473  */
474 void ServerCallbackReturnValue(int ret, const AttConnectInfo *connect);
475 
476 /**
477  * @brief received bredr connect instructions data information.
478  *
479  * @param1 lcid Indicates the lcid.
480  * @param2 packet Indicates the pointer to Packet.
481  * @param3 ctx Indicates the pointer to context.
482  */
483 void AttRecvData(uint16_t lcid, const Packet *packet, const void *ctx);
484 
485 /**
486  * @brief received le connect instructions data information.
487  *
488  * @param1 aclHandle Indicates the aclHandle.
489  * @param2 packet Indicates the pointer to Packet.
490  */
491 void AttRecvLeData(uint16_t aclHandle, const Packet *packet);
492 
493 AttConnectedCallback *AttGetATTConnectCallback();
494 
495 int AttResponseSendData(const AttConnectInfo *connect, const Packet *packet);
496 
497 /**
498  * @brief received error opcode.
499  *
500  * @param1 connect Indicates the AttConnectInfo.
501  * @param2 error opcode.
502  */
503 void AttErrorCode(const AttConnectInfo *connect, uint8_t opcode);
504 
505 /**
506  * @brief get function array dress.
507  *
508  * @return Returns the pointer to recvDataFunction.
509  */
510 recvDataFunction *GetFunctionArrayDress();
511 
512 /**
513  * @brief return minute number
514  *
515  * @param1 number1
516  * @param2 number2
517  */
518 uint16_t Min(uint16_t param1, uint16_t param2);
519 
520 void AttShutDownClearConnectInfo(AttConnectInfo *connectInfo);
521 
522 #ifdef __cplusplus
523 }
524 #endif
525 
526 #endif  // ATT_COMMON_H
527