• 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 #include <stdio.h>
17 #include <sys/prctl.h>
18 
19 #include "br_connection_manager.h"
20 
21 #include "common_list.h"
22 #include "message_handler.h"
23 
24 #ifdef __LITEOS_M__
25 #include "ohos_types.h"
26 #endif
27 
28 #include "securec.h"
29 #include "softbus_adapter_mem.h"
30 #include "softbus_conn_manager.h"
31 #include "softbus_def.h"
32 #include "softbus_errcode.h"
33 #include "softbus_log.h"
34 #include "softbus_type_def.h"
35 #include "softbus_utils.h"
36 #include "stdbool.h"
37 #include "string.h"
38 #include "unistd.h"
39 #include "wrapper_br_interface.h"
40 #include "softbus_hidumper_conn.h"
41 
42 #define BR_CONNECTION_INFO "brConnectionInfo"
43 
44 static pthread_mutex_t g_connectionLock = PTHREAD_MUTEX_INITIALIZER;
45 static LIST_HEAD(g_connection_list);
46 static int32_t g_brBuffSize;
47 static uint16_t g_nextConnectionId = 0;
48 static int32_t BrConnectionInfoDump(int fd);
49 
InitBrConnectionManager(int32_t brBuffSize)50 void InitBrConnectionManager(int32_t brBuffSize)
51 {
52     SoftBusRegConnVarDump(BR_CONNECTION_INFO, &BrConnectionInfoDump);
53     g_brBuffSize = brBuffSize + sizeof(ConnPktHead);
54 }
55 
GetLocalWindowsByConnId(uint32_t connId)56 uint32_t GetLocalWindowsByConnId(uint32_t connId)
57 {
58     if (pthread_mutex_lock(&g_connectionLock) != 0) {
59         SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "GetLocalWindowsByConnId mutex failed");
60         return 0;
61     }
62     ListNode *item = NULL;
63     BrConnectionInfo *itemNode = NULL;
64     LIST_FOR_EACH(item, &g_connection_list) {
65         itemNode = LIST_ENTRY(item, BrConnectionInfo, node);
66         if (itemNode->connectionId == connId) {
67             uint32_t windows = itemNode->windows;
68             (void)pthread_mutex_unlock(&g_connectionLock);
69             return windows;
70         }
71     }
72     (void)pthread_mutex_unlock(&g_connectionLock);
73     return 0;
74 }
75 
GetBrConnectionCount(void)76 int32_t GetBrConnectionCount(void)
77 {
78     if (pthread_mutex_lock(&g_connectionLock) != 0) {
79         SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "mutex failed");
80         return SOFTBUS_ERR;
81     }
82     ListNode *item = NULL;
83     int32_t count = 0;
84     LIST_FOR_EACH(item, &g_connection_list) {
85         count++;
86     }
87     (void)pthread_mutex_unlock(&g_connectionLock);
88     return count;
89 }
90 
IsExitConnectionById(uint32_t connId)91 bool IsExitConnectionById(uint32_t connId)
92 {
93     (void)pthread_mutex_lock(&g_connectionLock);
94     ListNode *item = NULL;
95     BrConnectionInfo *itemNode = NULL;
96     LIST_FOR_EACH(item, &g_connection_list) {
97         itemNode = LIST_ENTRY(item, BrConnectionInfo, node);
98         if (itemNode->connectionId != connId) {
99             continue;
100         }
101         (void)pthread_mutex_unlock(&g_connectionLock);
102         return true;
103     }
104     (void)pthread_mutex_unlock(&g_connectionLock);
105     return false;
106 }
107 
IsExitBrConnectByFd(int32_t socketFd)108 bool IsExitBrConnectByFd(int32_t socketFd)
109 {
110     (void)pthread_mutex_lock(&g_connectionLock);
111     ListNode *item = NULL;
112     BrConnectionInfo *itemNode = NULL;
113     LIST_FOR_EACH(item, &g_connection_list) {
114         itemNode = LIST_ENTRY(item, BrConnectionInfo, node);
115         if (socketFd == itemNode->socketFd) {
116             (void)pthread_mutex_unlock(&g_connectionLock);
117             return true;
118         }
119     }
120     (void)pthread_mutex_unlock(&g_connectionLock);
121     return false;
122 }
123 
GetConnectionRef(uint32_t connId)124 BrConnectionInfo *GetConnectionRef(uint32_t connId)
125 {
126     if (pthread_mutex_lock(&g_connectionLock) != 0) {
127         SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "[GetConnectionRef] mutex failed");
128         return NULL;
129     }
130     ListNode *item = NULL;
131     BrConnectionInfo *itemNode = NULL;
132     LIST_FOR_EACH(item, &g_connection_list) {
133         itemNode = LIST_ENTRY(item, BrConnectionInfo, node);
134         if (itemNode->connectionId == connId) {
135             itemNode->infoObjRefCount++;
136             (void)pthread_mutex_unlock(&g_connectionLock);
137             return itemNode;
138         }
139     }
140     (void)pthread_mutex_unlock(&g_connectionLock);
141     return NULL;
142 }
143 
ReleaseBrconnectionNode(BrConnectionInfo * conn)144 void ReleaseBrconnectionNode(BrConnectionInfo *conn)
145 {
146     if (conn == NULL) {
147         return;
148     }
149     ListNode *item = NULL;
150     ListNode *nextItem = NULL;
151     RequestInfo *requestInfo = NULL;
152     LIST_FOR_EACH_SAFE(item, nextItem, &conn->requestList) {
153         requestInfo = LIST_ENTRY(item, RequestInfo, node);
154         ListDelete(&requestInfo->node);
155         SoftBusFree(requestInfo);
156     }
157     LIST_FOR_EACH_SAFE(item, nextItem, &conn->pendingRequestList) {
158         requestInfo = LIST_ENTRY(item, RequestInfo, node);
159         ListDelete(&requestInfo->node);
160         SoftBusFree(requestInfo);
161     }
162     pthread_cond_destroy(&conn->congestCond);
163     pthread_mutex_destroy(&conn->lock);
164     SoftBusFree(conn->recvBuf);
165     SoftBusFree(conn);
166 }
167 
ReleaseConnectionRef(BrConnectionInfo * connInfo)168 void ReleaseConnectionRef(BrConnectionInfo *connInfo)
169 {
170     if (connInfo == NULL) {
171         return;
172     }
173     if (pthread_mutex_lock(&g_connectionLock) != 0) {
174         SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "[ReleaseConnectionRef] lock mutex failed");
175         return;
176     }
177     connInfo->infoObjRefCount--;
178     if (connInfo->infoObjRefCount <= 0) {
179         ListDelete(&connInfo->node);
180         ReleaseBrconnectionNode(connInfo);
181     }
182     (void)pthread_mutex_unlock(&g_connectionLock);
183 }
184 
ReleaseConnectionRefByConnId(uint32_t connId)185 void ReleaseConnectionRefByConnId(uint32_t connId)
186 {
187     if (pthread_mutex_lock(&g_connectionLock) != 0) {
188         SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "[ReleaseConnectionRef] lock mutex failed");
189         return;
190     }
191     ListNode *item = NULL;
192     BrConnectionInfo *connInfo = NULL;
193     LIST_FOR_EACH(item, &g_connection_list) {
194         BrConnectionInfo *itemNode = LIST_ENTRY(item, BrConnectionInfo, node);
195         if (itemNode->connectionId == connId) {
196             connInfo = itemNode;
197             break;
198         }
199     }
200     if (connInfo != NULL) {
201         connInfo->infoObjRefCount--;
202         if (connInfo->infoObjRefCount <= 0) {
203             ListDelete(&connInfo->node);
204             ReleaseBrconnectionNode(connInfo);
205         }
206     }
207     (void)pthread_mutex_unlock(&g_connectionLock);
208 }
209 
AllocNewConnectionIdLocked(void)210 static uint32_t AllocNewConnectionIdLocked(void)
211 {
212     uint32_t tempId;
213     while (1) {
214         g_nextConnectionId++;
215         tempId = (CONNECT_BR << CONNECT_TYPE_SHIFT) + g_nextConnectionId;
216         if (!IsExitConnectionById(tempId)) {
217             break;
218         }
219     }
220     return tempId;
221 }
222 
CreateBrconnectionNode(bool clientFlag)223 BrConnectionInfo* CreateBrconnectionNode(bool clientFlag)
224 {
225     BrConnectionInfo *newConnInfo = (BrConnectionInfo *)SoftBusCalloc(sizeof(BrConnectionInfo));
226     if (newConnInfo == NULL) {
227         SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "[Create BrConnInfo malloc fail.]");
228         return NULL;
229     }
230     if (pthread_mutex_init(&newConnInfo->lock, NULL) != 0) {
231         SoftBusFree(newConnInfo);
232         return NULL;
233     }
234     if (pthread_cond_init(&newConnInfo->congestCond, NULL) != 0) {
235         pthread_mutex_destroy(&newConnInfo->lock);
236         SoftBusFree(newConnInfo);
237         return NULL;
238     }
239     newConnInfo->recvBuf = (char *)SoftBusCalloc(g_brBuffSize);
240     newConnInfo->recvSize = g_brBuffSize;
241     if (newConnInfo->recvBuf == NULL) {
242         SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "[Create BrConnInfo malloc recvBuf fail]");
243         pthread_cond_destroy(&newConnInfo->congestCond);
244         pthread_mutex_destroy(&newConnInfo->lock);
245         SoftBusFree(newConnInfo);
246         return NULL;
247     }
248     ListInit(&newConnInfo->node);
249     ListInit(&newConnInfo->requestList);
250     ListInit(&newConnInfo->pendingRequestList);
251     newConnInfo->connectionId = AllocNewConnectionIdLocked();
252     newConnInfo->recvPos = 0;
253     newConnInfo->seq = 0;
254     newConnInfo->waitSeq = 0;
255     newConnInfo->ackTimeoutCount = 0;
256     newConnInfo->windows = DEFAULT_WINDOWS;
257     newConnInfo->conGestState = BT_RFCOM_CONGEST_OFF;
258     newConnInfo->refCount = 1;
259     newConnInfo->infoObjRefCount = 1;
260     newConnInfo->state = BR_CONNECTION_STATE_CONNECTING;
261     newConnInfo->sideType = clientFlag ? BR_CLIENT_TYPE : BR_SERVICE_TYPE;
262     return newConnInfo;
263 }
264 
GetConnectionInfo(uint32_t connectionId,ConnectionInfo * info)265 int32_t GetConnectionInfo(uint32_t connectionId, ConnectionInfo *info)
266 {
267     int32_t result = SOFTBUS_ERR;
268     if (pthread_mutex_lock(&g_connectionLock) != 0) {
269         SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "lock mutex failed");
270         return SOFTBUS_ERR;
271     }
272     ListNode *item = NULL;
273     LIST_FOR_EACH(item, &g_connection_list) {
274         BrConnectionInfo *itemNode = LIST_ENTRY(item, BrConnectionInfo, node);
275         if (itemNode->connectionId == connectionId) {
276             info->isAvailable = 1;
277             info->isServer = itemNode->sideType;
278             info->type = CONNECT_BR;
279             if (strcpy_s(info->brInfo.brMac, BT_MAC_LEN, itemNode->mac) != EOK) {
280                 SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "GetConnInfo scpy error");
281                 (void)pthread_mutex_unlock(&g_connectionLock);
282                 return SOFTBUS_BRCONNECTION_GETCONNINFO_ERROR;
283             }
284             result = SOFTBUS_OK;
285             break;
286         }
287     }
288     (void)pthread_mutex_unlock(&g_connectionLock);
289     return result;
290 }
291 
SetRefCountByConnId(int32_t delta,int32_t * refCount,uint32_t connectionId)292 int32_t SetRefCountByConnId(int32_t delta, int32_t *refCount, uint32_t connectionId)
293 {
294     int32_t state = BR_CONNECTION_STATE_CLOSED;
295     if (pthread_mutex_lock(&g_connectionLock) != 0) {
296         SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "lock mutex failed");
297         return state;
298     }
299     ListNode *item = NULL;
300     LIST_FOR_EACH(item, &g_connection_list) {
301         BrConnectionInfo *itemNode = LIST_ENTRY(item, BrConnectionInfo, node);
302         if (itemNode->connectionId == connectionId) {
303             itemNode->refCount += delta;
304             (*refCount) = itemNode->refCount;
305             if (itemNode->state == BR_CONNECTION_STATE_CONNECTED && itemNode->refCount <= 0) {
306                 itemNode->state = BR_CONNECTION_STATE_CLOSING;
307             }
308             state = itemNode->state;
309             break;
310         }
311     }
312     (void)pthread_mutex_unlock(&g_connectionLock);
313     return state;
314 }
315 
FreeCongestEvent(BrConnectionInfo * itemNode)316 static void FreeCongestEvent(BrConnectionInfo *itemNode)
317 {
318     itemNode->conGestState = BT_RFCOM_CONGEST_OFF;
319     if (pthread_mutex_lock(&itemNode->lock) != 0) {
320         SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "[FreeCongestEvent] mutex failed");
321         return;
322     }
323     pthread_cond_broadcast(&itemNode->congestCond);
324     (void)pthread_mutex_unlock(&itemNode->lock);
325 }
326 
SetBrConnStateByConnId(uint32_t connId,int32_t state)327 void SetBrConnStateByConnId(uint32_t connId, int32_t state)
328 {
329     if (pthread_mutex_lock(&g_connectionLock) != 0) {
330         SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "SetBrConnStateByConnId lock mutex failed");
331         return;
332     }
333     ListNode *britem = NULL;
334     BrConnectionInfo *itemNode = NULL;
335     LIST_FOR_EACH(britem, &g_connection_list) {
336         itemNode = LIST_ENTRY(britem, BrConnectionInfo, node);
337         if (itemNode->connectionId == connId) {
338             itemNode->state = state;
339             if (state == BR_CONNECTION_STATE_CLOSED) {
340                 FreeCongestEvent(itemNode);
341             }
342             break;
343         }
344     }
345     (void)pthread_mutex_unlock(&g_connectionLock);
346 }
347 
SetBrConnStateBySocket(int32_t socket,int32_t state,int32_t * perState)348 uint32_t SetBrConnStateBySocket(int32_t socket, int32_t state, int32_t *perState)
349 {
350     uint32_t connId = 0;
351     if (pthread_mutex_lock(&g_connectionLock) != 0) {
352         SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "SetBrConnStateByConnId lock mutex failed");
353         return connId;
354     }
355     ListNode *britem = NULL;
356     BrConnectionInfo *itemNode = NULL;
357     LIST_FOR_EACH(britem, &g_connection_list) {
358         itemNode = LIST_ENTRY(britem, BrConnectionInfo, node);
359         if (itemNode->socketFd == socket) {
360             if (perState != NULL) {
361                 (*perState) = itemNode->state;
362             }
363             itemNode->state = state;
364             if (state == BR_CONNECTION_STATE_CLOSED) {
365                 FreeCongestEvent(itemNode);
366             }
367             connId = itemNode->connectionId;
368             break;
369         }
370     }
371     (void)pthread_mutex_unlock(&g_connectionLock);
372     return connId;
373 }
374 
AddRequestByConnId(uint32_t connId,RequestInfo * requestInfo)375 int32_t AddRequestByConnId(uint32_t connId, RequestInfo *requestInfo)
376 {
377     if (pthread_mutex_lock(&g_connectionLock) != 0) {
378         SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "lock mutex failed");
379         return SOFTBUS_ERR;
380     }
381     ListNode *item = NULL;
382     LIST_FOR_EACH(item, &g_connection_list) {
383         BrConnectionInfo *itemNode = LIST_ENTRY(item, BrConnectionInfo, node);
384         if (itemNode->connectionId == connId) {
385             ListAdd(&itemNode->requestList, &requestInfo->node);
386             break;
387         }
388     }
389     (void)pthread_mutex_unlock(&g_connectionLock);
390     return SOFTBUS_OK;
391 }
392 
AddPendingRequestByConnId(uint32_t connId,RequestInfo * requestInfo)393 int32_t AddPendingRequestByConnId(uint32_t connId, RequestInfo *requestInfo)
394 {
395     if (pthread_mutex_lock(&g_connectionLock) != 0) {
396         SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "lock mutex failed");
397         return SOFTBUS_ERR;
398     }
399 
400     BrConnectionInfo *target = NULL;
401     ListNode *item = NULL;
402     LIST_FOR_EACH(item, &g_connection_list) {
403         BrConnectionInfo *itemNode = LIST_ENTRY(item, BrConnectionInfo, node);
404         if (itemNode->connectionId == connId) {
405             target = itemNode;
406             break;
407         }
408     }
409     if (target == NULL) {
410         SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "pending request failed, there is no connection %u", connId);
411         (void)pthread_mutex_unlock(&g_connectionLock);
412         return SOFTBUS_ERR;
413     }
414     ListAdd(&target->pendingRequestList, &requestInfo->node);
415     (void)pthread_mutex_unlock(&g_connectionLock);
416     return SOFTBUS_OK;
417 }
418 
AddConnectionList(BrConnectionInfo * newConnInfo)419 int32_t AddConnectionList(BrConnectionInfo *newConnInfo)
420 {
421     if (pthread_mutex_lock(&g_connectionLock) != 0) {
422         SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "lock mutex failed");
423         return SOFTBUS_ERR;
424     }
425     ListAdd(&g_connection_list, &newConnInfo->node);
426     (void)pthread_mutex_unlock(&g_connectionLock);
427     return SOFTBUS_OK;
428 }
429 
RfcomCongestEvent(int32_t socketFd,int32_t value)430 void RfcomCongestEvent(int32_t socketFd, int32_t value)
431 {
432     if (pthread_mutex_lock(&g_connectionLock) != 0) {
433         SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "[RfcomCongestEvent] lock mutex failed");
434         return;
435     }
436     ListNode *item = NULL;
437     BrConnectionInfo *itemNode = NULL;
438     LIST_FOR_EACH(item, &g_connection_list) {
439         itemNode = LIST_ENTRY(item, BrConnectionInfo, node);
440         if (itemNode->socketFd == socketFd) {
441             itemNode->conGestState = value;
442             if (value == BT_RFCOM_CONGEST_OFF) {
443                 if (pthread_mutex_lock(&itemNode->lock) != 0) {
444                     (void)pthread_mutex_unlock(&g_connectionLock);
445                     SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "CongestEvent lock itemNode failed");
446                     return;
447                 }
448                 pthread_cond_broadcast(&itemNode->congestCond);
449                 (void)pthread_mutex_unlock(&itemNode->lock);
450             }
451             break;
452         }
453     }
454     (void)pthread_mutex_unlock(&g_connectionLock);
455 }
456 
InitConnectionInfo(ConnectionInfo * connectionInfo,const BrConnectionInfo * itemNode)457 static int32_t InitConnectionInfo(ConnectionInfo *connectionInfo, const BrConnectionInfo *itemNode)
458 {
459     (*connectionInfo).isAvailable = 0;
460     (*connectionInfo).isServer = itemNode->sideType;
461     (*connectionInfo).type = CONNECT_BR;
462     if (strcpy_s((*connectionInfo).brInfo.brMac, BT_MAC_LEN, itemNode->mac) != EOK) {
463         SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "InitConnInfo scpy error");
464         return SOFTBUS_BRCONNECTION_STRNCPY_ERROR;
465     }
466     return SOFTBUS_OK;
467 }
468 
GetBrRequestListByConnId(uint32_t connId,ListNode * notifyList,ConnectionInfo * connectionInfo,int32_t * sideType)469 int32_t GetBrRequestListByConnId(uint32_t connId, ListNode *notifyList,
470     ConnectionInfo *connectionInfo, int32_t *sideType)
471 {
472     int32_t packRequestFlag = 0;
473     if (pthread_mutex_lock(&g_connectionLock) != 0) {
474         SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "BrClient lock mutex failed");
475         return packRequestFlag;
476     }
477     ListNode *britem = NULL;
478     ListNode *item = NULL;
479     ListNode *itemNext = NULL;
480     RequestInfo *requestInfo = NULL;
481     LIST_FOR_EACH(britem, &g_connection_list) {
482         BrConnectionInfo *itemNode = LIST_ENTRY(britem, BrConnectionInfo, node);
483         if (itemNode->connectionId == connId) {
484             (void)InitConnectionInfo(connectionInfo, itemNode);
485             (*sideType) = itemNode->sideType;
486             LIST_FOR_EACH_SAFE(item, itemNext, &itemNode->requestList) {
487                 requestInfo = LIST_ENTRY(item, RequestInfo, node);
488                 ListDelete(&requestInfo->node);
489                 ListAdd(notifyList, &requestInfo->node);
490                 packRequestFlag++;
491             }
492             break;
493         }
494     }
495     (void)pthread_mutex_unlock(&g_connectionLock);
496     return packRequestFlag;
497 }
498 
GetAndRemovePendingRequestByConnId(uint32_t connId,ListNode * pendings)499 int32_t GetAndRemovePendingRequestByConnId(uint32_t connId, ListNode *pendings)
500 {
501     if (pthread_mutex_lock(&g_connectionLock) != 0) {
502         SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "BrClient lock mutex failed");
503         return 0;
504     }
505     BrConnectionInfo *target = NULL;
506     ListNode *brItem = NULL;
507     LIST_FOR_EACH(brItem, &g_connection_list) {
508         BrConnectionInfo *itemNode = LIST_ENTRY(brItem, BrConnectionInfo, node);
509         if (itemNode->connectionId == connId) {
510             target = itemNode;
511             break;
512         }
513     }
514 
515     if (target == NULL) {
516         (void)pthread_mutex_unlock(&g_connectionLock);
517         SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_WARN, "get pending request failed, there is no %u conneciton", connId);
518         return 0;
519     }
520 
521     int32_t pendingCnt = 0;
522     ListNode *item = NULL;
523     ListNode *itemNext = NULL;
524     LIST_FOR_EACH_SAFE(item, itemNext, &target->pendingRequestList) {
525         RequestInfo *requestInfo = LIST_ENTRY(item, RequestInfo, node);
526         ListDelete(&requestInfo->node);
527         ListAdd(pendings, &requestInfo->node);
528         pendingCnt++;
529     }
530     (void)pthread_mutex_unlock(&g_connectionLock);
531     return pendingCnt;
532 }
533 
ResumeConnection(uint32_t connId,ListNode * pendings)534 int32_t ResumeConnection(uint32_t connId, ListNode *pendings)
535 {
536     if (pthread_mutex_lock(&g_connectionLock) != 0) {
537         SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "BrClient lock mutex failed");
538         return SOFTBUS_ERR;
539     }
540 
541     BrConnectionInfo *target = NULL;
542     ListNode *brItem = NULL;
543     LIST_FOR_EACH(brItem, &g_connection_list) {
544         BrConnectionInfo *itemNode = LIST_ENTRY(brItem, BrConnectionInfo, node);
545         if (itemNode->connectionId == connId) {
546             target = itemNode;
547             break;
548         }
549     }
550 
551     if (target == NULL) {
552         SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_WARN, "resume connection failed, there is no %u conneciton", connId);
553         (void)pthread_mutex_unlock(&g_connectionLock);
554         return SOFTBUS_ERR;
555     }
556     SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "resume connection %u to 'BR_CONNECTION_STATE_CONNECTED'", connId);
557     target->state = BR_CONNECTION_STATE_CONNECTED;
558     ListNode *item = NULL;
559     ListNode *itemNext = NULL;
560     LIST_FOR_EACH_SAFE(item, itemNext, &target->pendingRequestList) {
561         RequestInfo *requestInfo = LIST_ENTRY(item, RequestInfo, node);
562         ListDelete(&requestInfo->node);
563         ListAdd(pendings, &requestInfo->node);
564     }
565     (void)pthread_mutex_unlock(&g_connectionLock);
566     return SOFTBUS_OK;
567 }
568 
HasDiffMacDeviceExit(const ConnectOption * option)569 bool HasDiffMacDeviceExit(const ConnectOption *option)
570 {
571     if (pthread_mutex_lock(&g_connectionLock) != 0) {
572         SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "lock mutex failed");
573         return true;
574     }
575     if (IsListEmpty(&g_connection_list)) {
576         SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "[g_connection_list is empty, allow to connect device.]");
577         (void)pthread_mutex_unlock(&g_connectionLock);
578         return false;
579     }
580     ListNode *item = NULL;
581     bool res = false;
582     LIST_FOR_EACH(item, &g_connection_list) {
583         BrConnectionInfo *itemNode = LIST_ENTRY(item, BrConnectionInfo, node);
584         if (itemNode->sideType == BR_CLIENT_TYPE) {
585             if (StrCmpIgnoreCase(itemNode->mac, option->brOption.brMac) != 0) {
586                 res = true;
587                 break;
588             }
589         }
590     }
591     (void)pthread_mutex_unlock(&g_connectionLock);
592     return res;
593 }
594 
IsTargetSideType(ConnSideType targetType,int32_t connType)595 static bool IsTargetSideType(ConnSideType targetType, int32_t connType)
596 {
597     SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "br connection: targetType = %d, current connType = %d",
598         targetType, connType);
599     switch (targetType) {
600         case CONN_SIDE_ANY:
601             return true;
602         case CONN_SIDE_CLIENT:
603             return (connType == BR_CLIENT_TYPE);
604         case CONN_SIDE_SERVER:
605             return (connType == BR_SERVICE_TYPE);
606         default:
607             break;
608     }
609     return true;
610 }
611 
GetBrConnStateByConnOption(const ConnectOption * option,uint32_t * outConnId,uint32_t * connectingReqId)612 int32_t GetBrConnStateByConnOption(const ConnectOption *option, uint32_t *outConnId, uint32_t *connectingReqId)
613 {
614     if (pthread_mutex_lock(&g_connectionLock) != 0) {
615         SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "lock mutex failed");
616         return BR_CONNECTION_STATE_CLOSED;
617     }
618     ListNode *item = NULL;
619     LIST_FOR_EACH(item, &g_connection_list) {
620         BrConnectionInfo *itemNode = LIST_ENTRY(item, BrConnectionInfo, node);
621         if (IsTargetSideType(option->brOption.sideType, itemNode->sideType) &&
622             StrCmpIgnoreCase(itemNode->mac, option->brOption.brMac) == 0) {
623             if (outConnId != NULL) {
624                 *outConnId = itemNode->connectionId;
625             }
626             if (connectingReqId != NULL) {
627                 RequestInfo *connectingReq = LIST_ENTRY(itemNode->requestList.next, RequestInfo, node);
628                 *connectingReqId = connectingReq->requestId;
629             }
630             (void)pthread_mutex_unlock(&g_connectionLock);
631             return itemNode->state;
632         }
633     }
634     (void)pthread_mutex_unlock(&g_connectionLock);
635     return BR_CONNECTION_STATE_CLOSED;
636 }
637 
GetBrConnStateByConnectionId(uint32_t connId)638 int32_t GetBrConnStateByConnectionId(uint32_t connId)
639 {
640     (void)pthread_mutex_lock(&g_connectionLock);
641     int32_t state = -1;
642     BrConnectionInfo *target = NULL;
643     ListNode *item = NULL;
644     LIST_FOR_EACH(item, &g_connection_list) {
645         BrConnectionInfo *itemNode = LIST_ENTRY(item, BrConnectionInfo, node);
646         if (itemNode->connectionId == connId) {
647             target = itemNode;
648             break;
649         }
650     }
651     if (target != NULL) {
652         state = target->state;
653     }
654     (void)pthread_mutex_unlock(&g_connectionLock);
655     return state;
656 }
657 
BrClosingByConnOption(const ConnectOption * option,int32_t * socketFd,int32_t * sideType)658 int32_t BrClosingByConnOption(const ConnectOption *option, int32_t *socketFd, int32_t *sideType)
659 {
660     if (pthread_mutex_lock(&g_connectionLock) != 0) {
661         SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "BrClosingByConnOption mutex failed");
662         return SOFTBUS_ERR;
663     }
664 
665     ListNode *item = NULL;
666     BrConnectionInfo *itemNode = NULL;
667     LIST_FOR_EACH(item, &g_connection_list) {
668         itemNode = LIST_ENTRY(item, BrConnectionInfo, node);
669         if (StrCmpIgnoreCase(itemNode->mac, option->brOption.brMac) == 0) {
670             *socketFd = itemNode->socketFd;
671             *sideType = itemNode->sideType;
672             itemNode->state = BR_CONNECTION_STATE_CLOSING;
673             (void)pthread_mutex_unlock(&g_connectionLock);
674             return SOFTBUS_OK;
675         }
676     }
677     (void)pthread_mutex_unlock(&g_connectionLock);
678     SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "BrClosingByConnOption not find mac addr");
679     return SOFTBUS_NOT_FIND;
680 }
681 
BrCheckActiveConnection(const ConnectOption * option)682 bool BrCheckActiveConnection(const ConnectOption *option)
683 {
684     if (option == NULL || option->type != CONNECT_BR) {
685         SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "option check fail");
686         return false;
687     }
688     SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "BrCheckActiveConnection");
689 
690     ListNode *item = NULL;
691     BrConnectionInfo *itemNode = NULL;
692 
693     if (pthread_mutex_lock(&g_connectionLock) != 0) {
694         SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "mutex failed");
695         return false;
696     }
697     LIST_FOR_EACH(item, &g_connection_list) {
698         itemNode = LIST_ENTRY(item, BrConnectionInfo, node);
699         if ((StrCmpIgnoreCase(itemNode->mac, option->brOption.brMac) == 0) &&
700             (itemNode->state == BR_CONNECTION_STATE_CONNECTED)) {
701             SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "BrCheckActiveConnection true");
702             (void)pthread_mutex_unlock(&g_connectionLock);
703             return true;
704         }
705     }
706     (void)pthread_mutex_unlock(&g_connectionLock);
707 
708     SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "BrCheckActiveConnection false");
709     return false;
710 }
711 
BrConnectionInfoDump(int fd)712 static int32_t BrConnectionInfoDump(int fd)
713 {
714     char tempMac[BT_ADDR_LEN] = {0};
715     if (pthread_mutex_lock(&g_connectionLock) != SOFTBUS_OK) {
716         SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "lock mutex failed");
717         return SOFTBUS_LOCK_ERR;
718     }
719     ListNode *item = NULL;
720     SOFTBUS_DPRINTF(fd, "\n-----------------BRConnect Info-------------------\n");
721     LIST_FOR_EACH(item, &g_connection_list) {
722         BrConnectionInfo *itemNode = LIST_ENTRY(item, BrConnectionInfo, node);
723         SOFTBUS_DPRINTF(fd, "connectionId                  : %d\n", itemNode->connectionId);
724         SOFTBUS_DPRINTF(fd, "socketFd                      : %d\n", itemNode->socketFd);
725         SOFTBUS_DPRINTF(fd, "sideType                      : %d\n", itemNode->sideType);
726         DataMasking(itemNode->mac, BT_ADDR_LEN, MAC_DELIMITER, tempMac);
727         SOFTBUS_DPRINTF(fd, "btMac                         : %s\n", tempMac);
728         SOFTBUS_DPRINTF(fd, "connect Queue State           : %d\n", itemNode->connectQueueState);
729         SOFTBUS_DPRINTF(fd, "br state                      : %d\n", itemNode->state);
730         SOFTBUS_DPRINTF(fd, "refCount                      : %d\n", itemNode->refCount);
731         SOFTBUS_DPRINTF(fd, "refCountRemote                : %d\n", itemNode->refCountRemote);
732         SOFTBUS_DPRINTF(fd, "infoObjRefCount               : %d\n", itemNode->infoObjRefCount);
733         SOFTBUS_DPRINTF(fd, "recvBuf                       : %s\n", itemNode->recvBuf);
734         SOFTBUS_DPRINTF(fd, "recvSize                      : %d\n", itemNode->recvSize);
735         SOFTBUS_DPRINTF(fd, "recvPos                       : %d\n", itemNode->recvPos);
736         SOFTBUS_DPRINTF(fd, "conGestState                  : %d\n", itemNode->conGestState);
737         SOFTBUS_DPRINTF(fd, "request Info: \n");
738         LIST_FOR_EACH(item, &(itemNode->requestList)) {
739             RequestInfo *requestNode = LIST_ENTRY(item, RequestInfo, node);
740             SOFTBUS_DPRINTF(fd, "requestId                 : %u\n", requestNode->requestId);
741         }
742         SOFTBUS_DPRINTF(fd, "seq                           : %lu\n", itemNode->seq);
743         SOFTBUS_DPRINTF(fd, "waitSeq                       : %lu\n", itemNode->waitSeq);
744         SOFTBUS_DPRINTF(fd, "windows                       : %u\n", itemNode->windows);
745         SOFTBUS_DPRINTF(fd, "ackTimeoutCount               : %u\n", itemNode->ackTimeoutCount);
746         SOFTBUS_DPRINTF(fd, "pending request Info: \n");
747         LIST_FOR_EACH(item, &(itemNode->pendingRequestList)) {
748             RequestInfo *requestNode = LIST_ENTRY(item, RequestInfo, node);
749             SOFTBUS_DPRINTF(fd, "requestId                 : %u\n", requestNode->requestId);
750         }
751     }
752     (void)pthread_mutex_unlock(&g_connectionLock);
753     return SOFTBUS_OK;
754 }
755