• 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 BR_CONNECTION_MANAGER_H
17 #define BR_CONNECTION_MANAGER_H
18 
19 #include "common_list.h"
20 #include "softbus_conn_interface.h"
21 
22 #ifdef __cplusplus
23 #if __cplusplus
24 extern "C" {
25 #endif
26 #endif
27 
28 typedef struct {
29     ListNode node;
30     uint32_t requestId;
31     ConnectResult callback;
32 } RequestInfo;
33 
34 #define BT_RFCOM_CONGEST_ON 0
35 #define BT_RFCOM_CONGEST_OFF 1
36 #define BR_CLIENT_TYPE 0
37 #define BR_SERVICE_TYPE 1
38 
39 #define METHOD_NOTIFY_REQUEST 1
40 #define METHOD_NOTIFY_RESPONSE 2
41 #define METHOD_NOTIFY_ACK 4
42 #define METHOD_ACK_RESPONSE 5
43 
44 #define MAGIC_NUMBER 0xBABEFACE
45 
46 #define MIN_WINDOWS 10
47 #define MAX_WINDOWS 80
48 #define DEFAULT_WINDOWS 20
49 
50 typedef enum {
51     ADD_CONN_BR_INVALID,
52     ADD_CONN_BR_CLIENT_CONNECTED_MSG,
53     ADD_CONN_BR_CLIENT_DISCONNECTED_MSG,
54     ADD_CONN_BR_SERVICE_CONNECTED_MSG,
55     ADD_CONN_BR_SERVICE_DISCONNECTED_MSG,
56     ADD_CONN_BR_CONGEST_MSG,
57     ADD_CONN_BR_RECV_MSG,
58     ADD_CONN_BR_CLOSING_TIMEOUT_MSG,
59     ADD_CONN_BR_MAX
60 } BrConnLoopMsgType;
61 
62 enum BRConnectionState {
63     BR_CONNECTION_STATE_CONNECTING = 0,
64     BR_CONNECTION_STATE_CONNECTED,
65     BR_CONNECTION_STATE_CLOSING,
66     BR_CONNECTION_STATE_CLOSED
67 };
68 
69 typedef struct BrConnectionInfo {
70     ListNode node;
71     uint32_t connectionId;
72     int32_t socketFd;
73     int32_t sideType;
74     char mac[BT_MAC_LEN];
75     int32_t connectQueueState;
76     int32_t state;
77     int32_t refCount;
78     int32_t refCountRemote;
79     int32_t infoObjRefCount;
80     char *recvBuf;
81     int32_t recvSize;
82     int32_t recvPos;
83     int32_t conGestState;
84     ListNode requestList;
85     pthread_mutex_t lock;
86     pthread_cond_t congestCond;
87     uint64_t seq;
88     uint64_t waitSeq;
89     uint32_t windows;
90     uint32_t ackTimeoutCount;
91     ListNode pendingRequestList;
92 } BrConnectionInfo;
93 
94 void InitBrConnectionManager(int32_t brBuffSize);
95 
96 uint32_t GetLocalWindowsByConnId(uint32_t connId);
97 
98 int32_t GetBrConnectionCount(void);
99 
100 bool IsExitConnectionById(uint32_t connId);
101 
102 bool IsExitBrConnectByFd(int32_t socketFd);
103 
104 BrConnectionInfo *GetConnectionRef(uint32_t connId);
105 
106 void ReleaseBrconnectionNode(BrConnectionInfo *conn);
107 
108 void ReleaseConnectionRef(BrConnectionInfo *connInfo);
109 
110 void ReleaseConnectionRefByConnId(uint32_t connId);
111 
112 BrConnectionInfo* CreateBrconnectionNode(bool clientFlag);
113 
114 int32_t GetConnectionInfo(uint32_t connectionId, ConnectionInfo *info);
115 
116 int32_t SetRefCountByConnId(int32_t delta, int32_t *refCount, uint32_t connectionId);
117 
118 void SetBrConnStateByConnId(uint32_t connId, int32_t state);
119 
120 uint32_t SetBrConnStateBySocket(int32_t socket, int32_t state, int32_t *perState);
121 
122 int32_t AddRequestByConnId(uint32_t connId, RequestInfo *requestInfo);
123 
124 int32_t AddPendingRequestByConnId(uint32_t connId, RequestInfo *requestInfo);
125 
126 int32_t AddConnectionList(BrConnectionInfo *newConnInfo);
127 
128 void RfcomCongestEvent(int32_t socketFd, int32_t value);
129 
130 int32_t GetBrRequestListByConnId(uint32_t connId, ListNode *notifyList,
131     ConnectionInfo *connectionInfo, int32_t *sideType);
132 
133 int32_t GetAndRemovePendingRequestByConnId(uint32_t connId, ListNode *pendings);
134 
135 bool HasDiffMacDeviceExit(const ConnectOption *option);
136 
137 int32_t GetBrConnStateByConnOption(const ConnectOption *option, uint32_t *outCountId, uint32_t *connectingReqId);
138 
139 int32_t GetBrConnStateByConnectionId(uint32_t connId);
140 
141 int32_t BrClosingByConnOption(const ConnectOption *option, int32_t *socketFd, int32_t *sideType);
142 
143 bool BrCheckActiveConnection(const ConnectOption *option);
144 
145 int32_t ResumeConnection(uint32_t connId, ListNode *pendings);
146 
147 #ifdef __cplusplus
148 #if __cplusplus
149 }
150 #endif /* __cplusplus */
151 #endif /* __cplusplus */
152 #endif /* BR_CONNECTION_MANAGER_H */
153