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 CONN_BR_TRANS_H
17 #define CONN_BR_TRANS_H
18 
19 #include "cJSON.h"
20 
21 #include "softbus_conn_br_manager.h"
22 #include "softbus_conn_common.h"
23 #include "wrapper_br_interface.h"
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 #define KEY_METHOD        "KEY_METHOD"
30 #define KEY_DELTA         "KEY_DELTA"
31 #define KEY_REFERENCE_NUM "KEY_REFERENCE_NUM"
32 #define KEY_WINDOWS       "KEY_WINDOWS"
33 #define KEY_ACK_SEQ_NUM   "KEY_ACK_SEQ_NUM"
34 
35 #define MIN_WINDOW             10
36 #define MAX_WINDOW             80
37 #define DEFAULT_WINDOW         20
38 #define ACK_FAILED_TIMES       3
39 #define WAIT_ACK_TIMEOUT_MILLS 100
40 #define TIMEOUT_TIMES          2
41 
42 enum BrCtlMessageMethod {
43     BR_METHOD_NOTIFY_REQUEST = 1,
44     BR_METHOD_NOTIFY_RESPONSE = 2,
45     BR_METHOD_NOTIFY_ACK = 4,
46     BR_METHOD_ACK_RESPONSE = 5,
47 };
48 
49 typedef struct {
50     SoftBusMutex lock;
51     bool messagePosted;
52     bool sendTaskRunning;
53 } StartBrSendLPInfo;
54 
55 
56 typedef struct {
57     uint32_t connectionId;
58     int32_t flag;
59     enum BrCtlMessageMethod method;
60     union {
61         struct {
62             int32_t delta;
63             int32_t referenceNumber;
64         } referenceRequest;
65         struct {
66             int32_t referenceNumber;
67         } referenceResponse;
68         struct {
69             int32_t window;
70             int64_t seq;
71         } ackRequestResponse;
72     };
73 } BrCtlMessageSerializationContext;
74 
75 typedef struct {
76     void (*onPostByteFinshed)(
77         uint32_t connectionId, uint32_t len, int32_t pid, int32_t flag, int32_t module, int64_t seq, int32_t error);
78 } ConnBrTransEventListener;
79 
80 int32_t ConnBrTransReadOneFrame(uint32_t connectionId, int32_t socketHandle, LimitedBuffer *buffer, uint8_t **outData);
81 int32_t BrTransSend(uint32_t connectionId, int32_t socketHandle, uint32_t mtu, const uint8_t *data, uint32_t dataLen);
82 int64_t ConnBrPackCtlMessage(BrCtlMessageSerializationContext ctx, uint8_t **outData, uint32_t *outLen);
83 int32_t ConnBrPostBytes(
84     uint32_t connectionId, uint8_t *data, uint32_t len, int32_t pid, int32_t flag, int32_t module, int64_t seq);
85 int32_t ConnBrTransConfigPostLimit(const LimitConfiguration *configuration);
86 
87 int32_t ConnBrTransMuduleInit(SppSocketDriver *sppDriver, ConnBrTransEventListener *listener);
88 
89 #ifdef __cplusplus
90 }
91 #endif /* __cplusplus */
92 #endif /* CONN_BR_TRANS_H */