• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2024 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 "trans_lane_manager.h"
17 
18 #include <securec.h>
19 
20 #include "common_list.h"
21 #include "softbus_adapter_mem.h"
22 #include "softbus_adapter_thread.h"
23 #include "softbus_def.h"
24 #include "softbus_error_code.h"
25 #include "softbus_utils.h"
26 #include "trans_channel_manager.h"
27 #include "trans_lane_pending_ctl.h"
28 #include "trans_log.h"
29 #include "legacy/softbus_hidumper_trans.h"
30 #include "trans_session_manager.h"
31 
32 #define CMD_CONCURRENT_SESSION_LIST "concurrent_sessionlist"
33 
34 typedef struct {
35     ListNode node;
36     char sessionName[SESSION_NAME_SIZE_MAX];
37     pid_t pid;
38     int32_t sessionId;
39     int32_t channelId;
40     int32_t channelType;
41     uint32_t laneHandle;
42     CoreSessionState state;
43     bool isAsync;
44     bool isQosLane;
45 } SocketWithChannelInfo;
46 
47 static SoftBusList *g_channelLaneList = NULL;
48 
49 static SoftBusList *g_socketChannelList = NULL;
50 
GetTransSessionInfoByLane(TransLaneInfo * laneItem,AppInfo * appInfo)51 static void GetTransSessionInfoByLane(TransLaneInfo * laneItem, AppInfo *appInfo)
52 {
53     if (TransGetAppInfoByChanId(laneItem->channelId, laneItem->channelType, appInfo) != SOFTBUS_OK) {
54         TRANS_LOGE(TRANS_SVC, "TransGetAppInfoByChanId get appInfo failed");
55     }
56 }
57 
ConvertLaneLinkTypeToDumper(LaneLinkType type)58 static TransDumpLaneLinkType ConvertLaneLinkTypeToDumper(LaneLinkType type)
59 {
60     switch (type) {
61         case LANE_BR:
62             return DUMPER_LANE_BR;
63         case LANE_BLE:
64             return DUMPER_LANE_BLE;
65         case LANE_P2P:
66             return DUMPER_LANE_P2P;
67         case LANE_WLAN_2P4G:
68             return DUMPER_LANE_WLAN;
69         case LANE_WLAN_5G:
70             return DUMPER_LANE_WLAN;
71         case LANE_ETH:
72             return DUMPER_LANE_ETH;
73         default:
74             break;
75     }
76     return DUMPER_LANE_LINK_TYPE_BUTT;
77 }
78 
TransLaneChannelForEachShowInfo(int32_t fd)79 static int32_t TransLaneChannelForEachShowInfo(int32_t fd)
80 {
81     if (g_channelLaneList == NULL) {
82         TRANS_LOGE(TRANS_INIT, "trans lane manager hasn't init.");
83         return SOFTBUS_NO_INIT;
84     }
85     AppInfo *appInfo = (AppInfo *)SoftBusMalloc(sizeof(AppInfo));
86     if (appInfo == NULL) {
87         TRANS_LOGE(TRANS_SVC, "TransSessionInfoForEach malloc appInfo failed");
88         return SOFTBUS_MALLOC_ERR;
89     }
90     if (SoftBusMutexLock(&(g_channelLaneList->lock)) != SOFTBUS_OK) {
91         TRANS_LOGE(TRANS_SVC, "lock failed");
92         SoftBusFree(appInfo);
93         return SOFTBUS_LOCK_ERR;
94     }
95 
96     TransLaneInfo *laneItem = NULL;
97     LIST_FOR_EACH_ENTRY(laneItem, &(g_channelLaneList->list), TransLaneInfo, node) {
98         GetTransSessionInfoByLane(laneItem, appInfo);
99         SoftBusTransDumpRunningSession(fd,
100             ConvertLaneLinkTypeToDumper(laneItem->laneConnInfo.type), appInfo);
101     }
102 
103     (void)memset_s(appInfo->sessionKey, sizeof(appInfo->sessionKey), 0, sizeof(appInfo->sessionKey));
104     (void)SoftBusMutexUnlock(&(g_channelLaneList->lock));
105     SoftBusFree(appInfo);
106     return SOFTBUS_OK;
107 }
108 
TransLaneMgrInit(void)109 int32_t TransLaneMgrInit(void)
110 {
111     if (g_channelLaneList != NULL) {
112         TRANS_LOGI(TRANS_INIT, "trans lane info manager has init.");
113         return SOFTBUS_OK;
114     }
115     g_channelLaneList = CreateSoftBusList();
116     if (g_channelLaneList == NULL) {
117         TRANS_LOGE(TRANS_INIT, "trans lane info manager init failed.");
118         return SOFTBUS_MALLOC_ERR;
119     }
120 
121     return SoftBusRegTransVarDump(CMD_CONCURRENT_SESSION_LIST, TransLaneChannelForEachShowInfo);
122 }
123 
TransSocketLaneMgrInit(void)124 int32_t TransSocketLaneMgrInit(void)
125 {
126     if (g_socketChannelList != NULL) {
127         TRANS_LOGI(TRANS_INIT, "trans lane info manager has init.");
128         return SOFTBUS_OK;
129     }
130     g_socketChannelList = CreateSoftBusList();
131     if (g_socketChannelList == NULL) {
132         TRANS_LOGE(TRANS_INIT, "trans lane info manager init failed.");
133         return SOFTBUS_MALLOC_ERR;
134     }
135     return SOFTBUS_OK;
136 }
137 
TransLaneMgrDeinit(void)138 void TransLaneMgrDeinit(void)
139 {
140     TRANS_LOGI(TRANS_CTRL, "enter.");
141     if (g_channelLaneList == NULL) {
142         return;
143     }
144 
145     if (SoftBusMutexLock(&g_channelLaneList->lock) != SOFTBUS_OK) {
146         TRANS_LOGE(TRANS_SVC, "lock failed");
147         return;
148     }
149     TransLaneInfo *laneItem = NULL;
150     TransLaneInfo *nextLaneItem = NULL;
151     LIST_FOR_EACH_ENTRY_SAFE(laneItem, nextLaneItem, &g_channelLaneList->list, TransLaneInfo, node) {
152         ListDelete(&(laneItem->node));
153         if (laneItem->isQosLane) {
154             TransFreeLaneByLaneHandle(laneItem->laneHandle, true);
155         } else {
156             LnnFreeLane(laneItem->laneHandle);
157         }
158         SoftBusFree(laneItem);
159     }
160     g_channelLaneList->cnt = 0;
161     (void)SoftBusMutexUnlock(&g_channelLaneList->lock);
162     DestroySoftBusList(g_channelLaneList);
163     g_channelLaneList = NULL;
164 }
165 
TransSocketLaneMgrDeinit(void)166 void TransSocketLaneMgrDeinit(void)
167 {
168     TRANS_LOGI(TRANS_CTRL, "enter.");
169     if (g_socketChannelList == NULL) {
170         return;
171     }
172     if (SoftBusMutexLock(&g_socketChannelList->lock) != SOFTBUS_OK) {
173         TRANS_LOGE(TRANS_SVC, "lock failed");
174         return;
175     }
176     SocketWithChannelInfo *socketItem = NULL;
177     SocketWithChannelInfo *nextSocketItem = NULL;
178     LIST_FOR_EACH_ENTRY_SAFE(socketItem, nextSocketItem, &g_socketChannelList->list, SocketWithChannelInfo, node) {
179         ListDelete(&(socketItem->node));
180         SoftBusFree(socketItem);
181     }
182     g_socketChannelList->cnt = 0;
183     (void)SoftBusMutexUnlock(&g_socketChannelList->lock);
184     DestroySoftBusList(g_socketChannelList);
185     g_socketChannelList = NULL;
186 }
187 
TransLaneMgrAddLane(const TransInfo * transInfo,const LaneConnInfo * connInfo,uint32_t laneHandle,bool isQosLane,AppInfoData * myData)188 int32_t TransLaneMgrAddLane(
189     const TransInfo *transInfo, const LaneConnInfo *connInfo, uint32_t laneHandle, bool isQosLane, AppInfoData *myData)
190 {
191     if (transInfo == NULL || g_channelLaneList == NULL || connInfo == NULL || myData == NULL) {
192         return SOFTBUS_INVALID_PARAM;
193     }
194     TransLaneInfo *newLane = (TransLaneInfo *)SoftBusCalloc(sizeof(TransLaneInfo));
195     TRANS_CHECK_AND_RETURN_RET_LOGE(newLane != NULL, SOFTBUS_MALLOC_ERR, TRANS_SVC, "calloc laneInfo failed.");
196     newLane->channelId = transInfo->channelId;
197     newLane->channelType = transInfo->channelType;
198     newLane->laneHandle = laneHandle;
199     newLane->isQosLane = isQosLane;
200     newLane->pid = myData->pid;
201     if (memcpy_s(&(newLane->laneConnInfo), sizeof(LaneConnInfo), connInfo, sizeof(LaneConnInfo)) != EOK) {
202         SoftBusFree(newLane);
203         TRANS_LOGE(TRANS_SVC, "memcpy connInfo failed");
204         return SOFTBUS_MEM_ERR;
205     }
206     if (strcpy_s(newLane->pkgName, sizeof(newLane->pkgName), myData->pkgName) != EOK) {
207         SoftBusFree(newLane);
208         TRANS_LOGE(TRANS_SVC, "strcpy failed.");
209         return SOFTBUS_STRCPY_ERR;
210     }
211     if (SoftBusMutexLock(&(g_channelLaneList->lock)) != SOFTBUS_OK) {
212         SoftBusFree(newLane);
213         TRANS_LOGE(TRANS_SVC, "lock failed");
214         return SOFTBUS_LOCK_ERR;
215     }
216 
217     TransLaneInfo *laneItem = NULL;
218     LIST_FOR_EACH_ENTRY(laneItem, &(g_channelLaneList->list), TransLaneInfo, node) {
219         if (laneItem->channelId == transInfo->channelId && laneItem->channelType == transInfo->channelType) {
220             SoftBusFree(newLane);
221             (void)SoftBusMutexUnlock(&(g_channelLaneList->lock));
222             TRANS_LOGI(TRANS_SVC, "trans lane info has existed. channelId=%{public}d, channelType=%{public}d",
223                 transInfo->channelId, transInfo->channelType);
224             return SOFTBUS_ALREADY_EXISTED;
225         }
226     }
227     ListInit(&(newLane->node));
228     ListAdd(&(g_channelLaneList->list), &(newLane->node));
229     TRANS_LOGI(TRANS_CTRL, "add channelId=%{public}d", newLane->channelId);
230     g_channelLaneList->cnt++;
231     TRANS_LOGI(TRANS_SVC, "lane count is cnt=%{public}d", g_channelLaneList->cnt);
232     (void)SoftBusMutexUnlock(&(g_channelLaneList->lock));
233     return SOFTBUS_OK;
234 }
235 
TransLaneMgrDelLane(int32_t channelId,int32_t channelType,bool isAsync)236 int32_t TransLaneMgrDelLane(int32_t channelId, int32_t channelType, bool isAsync)
237 {
238     if (g_channelLaneList == NULL) {
239         TRANS_LOGE(TRANS_INIT, "trans lane manager hasn't init.");
240         return SOFTBUS_NO_INIT;
241     }
242     if (SoftBusMutexLock(&(g_channelLaneList->lock)) != SOFTBUS_OK) {
243         TRANS_LOGE(TRANS_SVC, "lock failed");
244         return SOFTBUS_LOCK_ERR;
245     }
246     TransLaneInfo *laneItem = NULL;
247     TransLaneInfo *next = NULL;
248     int32_t ret = SOFTBUS_TRANS_NODE_NOT_FOUND;
249     uint32_t laneHandle = 0;
250     bool isQos = false;
251     LIST_FOR_EACH_ENTRY_SAFE(laneItem, next, &(g_channelLaneList->list), TransLaneInfo, node) {
252         if (laneItem->channelId == channelId && laneItem->channelType == channelType) {
253             ListDelete(&(laneItem->node));
254             TRANS_LOGI(TRANS_CTRL, "delete channelId=%{public}d, channelType = %{public}d",
255                 laneItem->channelId, laneItem->channelType);
256             g_channelLaneList->cnt--;
257             laneHandle = laneItem->laneHandle;
258             if (laneItem->isQosLane) {
259                 isQos = true;
260             }
261             SoftBusFree(laneItem);
262             ret = SOFTBUS_OK;
263             break;
264         }
265     }
266     (void)SoftBusMutexUnlock(&(g_channelLaneList->lock));
267     if (ret != SOFTBUS_OK) {
268         TRANS_LOGW(TRANS_SVC, "No lane to be is found. channelId=%{public}d.", channelId);
269         return ret;
270     }
271     if (isQos) {
272         TransFreeLaneByLaneHandle(laneHandle, isAsync);
273     } else {
274         LnnFreeLane(laneHandle);
275     }
276     return ret;
277 }
278 
TransLaneMgrDeathCallback(const char * pkgName,int32_t pid)279 void TransLaneMgrDeathCallback(const char *pkgName, int32_t pid)
280 {
281     (void)TransDeleteSocketChannelInfoByPid(pid);
282     if (pkgName == NULL || g_channelLaneList == NULL) {
283         TRANS_LOGE(TRANS_INIT, "trans lane manager hasn't init.");
284         return;
285     }
286     char *anonymizePkgName = NULL;
287     Anonymize(pkgName, &anonymizePkgName);
288     TRANS_LOGW(TRANS_CTRL, "pkgName=%{public}s, pid=%{public}d", AnonymizeWrapper(anonymizePkgName), pid);
289     AnonymizeFree(anonymizePkgName);
290     if (SoftBusMutexLock(&(g_channelLaneList->lock)) != SOFTBUS_OK) {
291         TRANS_LOGE(TRANS_SVC, "lock failed");
292         return;
293     }
294     TransLaneInfo *laneItem = NULL;
295     TransLaneInfo *next = NULL;
296     LIST_FOR_EACH_ENTRY_SAFE(laneItem, next, &(g_channelLaneList->list), TransLaneInfo, node) {
297         if ((strcmp(laneItem->pkgName, pkgName) == 0) && (laneItem->pid == pid)) {
298             ListDelete(&(laneItem->node));
299             g_channelLaneList->cnt--;
300             TRANS_LOGI(TRANS_SVC, "death del lane. channelId=%{public}d, channelType=%{public}d",
301                 laneItem->channelId, laneItem->channelType);
302             if (laneItem->isQosLane) {
303                 TransFreeLaneByLaneHandle(laneItem->laneHandle, true);
304             } else {
305                 LnnFreeLane(laneItem->laneHandle);
306             }
307             SoftBusFree(laneItem);
308         }
309     }
310     (void)SoftBusMutexUnlock(&(g_channelLaneList->lock));
311     return;
312 }
313 
TransGetLaneHandleByChannelId(int32_t channelId,uint32_t * laneHandle)314 int32_t TransGetLaneHandleByChannelId(int32_t channelId, uint32_t *laneHandle)
315 {
316     if (g_channelLaneList == NULL || laneHandle == NULL) {
317         return SOFTBUS_INVALID_PARAM;
318     }
319     if (SoftBusMutexLock(&(g_channelLaneList->lock)) != SOFTBUS_OK) {
320         return SOFTBUS_LOCK_ERR;
321     }
322     TransLaneInfo *item = NULL;
323     LIST_FOR_EACH_ENTRY(item, &(g_channelLaneList->list), TransLaneInfo, node) {
324         if (item->channelId == channelId) {
325             *laneHandle = item->laneHandle;
326             (void)SoftBusMutexUnlock(&(g_channelLaneList->lock));
327             return SOFTBUS_OK;
328         }
329     }
330     (void)SoftBusMutexUnlock(&(g_channelLaneList->lock));
331     return SOFTBUS_TRANS_NODE_NOT_FOUND;
332 }
333 
TransGetLaneIdByChannelId(int32_t channelId,uint64_t * laneId)334 int32_t TransGetLaneIdByChannelId(int32_t channelId, uint64_t *laneId)
335 {
336     if (g_channelLaneList == NULL || laneId == NULL) {
337         return SOFTBUS_INVALID_PARAM;
338     }
339     if (SoftBusMutexLock(&(g_channelLaneList->lock)) != 0) {
340         return SOFTBUS_LOCK_ERR;
341     }
342     TransLaneInfo *item = NULL;
343     LIST_FOR_EACH_ENTRY(item, &(g_channelLaneList->list), TransLaneInfo, node) {
344         if (item->channelId == channelId) {
345             *laneId = item->laneConnInfo.laneId;
346             (void)SoftBusMutexUnlock(&(g_channelLaneList->lock));
347             return SOFTBUS_OK;
348         }
349     }
350     (void)SoftBusMutexUnlock(&(g_channelLaneList->lock));
351     return SOFTBUS_NOT_FIND;
352 }
353 
TransGetChannelInfoByLaneHandle(uint32_t laneHandle,int32_t * channelId,int32_t * channelType)354 int32_t TransGetChannelInfoByLaneHandle(uint32_t laneHandle, int32_t *channelId, int32_t *channelType)
355 {
356     if (g_channelLaneList == NULL || channelId == NULL || channelType == NULL) {
357         return SOFTBUS_INVALID_PARAM;
358     }
359     if (SoftBusMutexLock(&(g_channelLaneList->lock)) != SOFTBUS_OK) {
360         return SOFTBUS_LOCK_ERR;
361     }
362     TransLaneInfo *item = NULL;
363     LIST_FOR_EACH_ENTRY(item, &(g_channelLaneList->list), TransLaneInfo, node) {
364         if (item->laneHandle == laneHandle) {
365             *channelId = item->channelId;
366             *channelType = item->channelType;
367             (void)SoftBusMutexUnlock(&(g_channelLaneList->lock));
368             return SOFTBUS_OK;
369         }
370     }
371     (void)SoftBusMutexUnlock(&(g_channelLaneList->lock));
372     return SOFTBUS_TRANS_NODE_NOT_FOUND;
373 }
374 
GetSocketWithChannelInfoBySession(const char * sessionName,int32_t sessionId)375 static SocketWithChannelInfo *GetSocketWithChannelInfoBySession(const char *sessionName, int32_t sessionId)
376 {
377     // need get lock before
378     SocketWithChannelInfo *socketItem = NULL;
379     LIST_FOR_EACH_ENTRY(socketItem, &(g_socketChannelList->list), SocketWithChannelInfo, node) {
380         if (strcmp(socketItem->sessionName, sessionName) == 0 && socketItem->sessionId == sessionId) {
381             return socketItem;
382         }
383     }
384     return NULL;
385 }
386 
AnonymizeLogSessionNameWhenNotFound(const char * sessionName,int32_t sessionId)387 static void AnonymizeLogSessionNameWhenNotFound(const char *sessionName, int32_t sessionId)
388 {
389     char *tmpName = NULL;
390     Anonymize(sessionName, &tmpName);
391     TRANS_LOGE(
392         TRANS_SVC, "socket info not found. sessionName=%{public}s, sessionId=%{public}d",
393             AnonymizeWrapper(tmpName), sessionId);
394     AnonymizeFree(tmpName);
395 }
396 
TransAddSocketChannelInfo(const char * sessionName,int32_t sessionId,int32_t channelId,int32_t channelType,CoreSessionState state)397 int32_t TransAddSocketChannelInfo(
398     const char *sessionName, int32_t sessionId, int32_t channelId, int32_t channelType, CoreSessionState state)
399 {
400     if (sessionName == NULL) {
401         TRANS_LOGE(TRANS_SVC, "Invaild param, sessionName is null");
402         return SOFTBUS_TRANS_INVALID_SESSION_NAME;
403     }
404     if (sessionId <= 0) {
405         TRANS_LOGE(TRANS_SVC, "Invaild param, sessionId=%{public}d", sessionId);
406         return SOFTBUS_TRANS_INVALID_SESSION_ID;
407     }
408     TRANS_CHECK_AND_RETURN_RET_LOGE(
409         g_socketChannelList != NULL, SOFTBUS_NO_INIT, TRANS_INIT, "socket info manager hasn't init.");
410 
411     SocketWithChannelInfo *newSocket = (SocketWithChannelInfo *)SoftBusCalloc(sizeof(SocketWithChannelInfo));
412     TRANS_CHECK_AND_RETURN_RET_LOGE(newSocket != NULL, SOFTBUS_MALLOC_ERR, TRANS_SVC, "socket info calloc failed.");
413     if (strcpy_s(newSocket->sessionName, SESSION_NAME_SIZE_MAX, sessionName) != EOK) {
414         SoftBusFree(newSocket);
415         return SOFTBUS_STRCPY_ERR;
416     }
417     newSocket->sessionId = sessionId;
418     newSocket->channelId = channelId;
419     newSocket->channelType = channelType;
420     newSocket->state = state;
421     newSocket->laneHandle = INVALID_LANE_REQ_ID;
422     newSocket->isQosLane = false;
423     newSocket->isAsync = false;
424     int32_t tmpUid;
425     TransGetUidAndPid(sessionName, &tmpUid, &(newSocket->pid));
426 
427     if (SoftBusMutexLock(&(g_socketChannelList->lock)) != SOFTBUS_OK) {
428         TRANS_LOGE(TRANS_SVC, "lock failed");
429         SoftBusFree(newSocket);
430         return SOFTBUS_LOCK_ERR;
431     }
432 
433     SocketWithChannelInfo *socketItem = GetSocketWithChannelInfoBySession(sessionName, sessionId);
434     if (socketItem != NULL) {
435         char *tmpName = NULL;
436         Anonymize(sessionName, &tmpName);
437         TRANS_LOGI(
438             TRANS_SVC, "socket lane info has existed. socketId=%{public}d, sessionName=%{public}s",
439                 sessionId, AnonymizeWrapper(tmpName));
440         AnonymizeFree(tmpName);
441         SoftBusFree(newSocket);
442         (void)SoftBusMutexUnlock(&(g_socketChannelList->lock));
443         return SOFTBUS_OK;
444     }
445 
446     ListInit(&(newSocket->node));
447     ListAdd(&(g_socketChannelList->list), &(newSocket->node));
448     g_socketChannelList->cnt++;
449     TRANS_LOGI(TRANS_SVC, "socket lane count is cnt=%{public}d", g_socketChannelList->cnt);
450     (void)SoftBusMutexUnlock(&(g_socketChannelList->lock));
451     return SOFTBUS_OK;
452 }
453 
CheckParamIsValid(const char * sessionName,int32_t sessionId)454 static int32_t CheckParamIsValid(const char *sessionName, int32_t sessionId)
455 {
456     if (sessionName == NULL) {
457         TRANS_LOGE(TRANS_SVC, "Invaild param, sessionName is null");
458         return SOFTBUS_TRANS_INVALID_SESSION_NAME;
459     }
460     if (sessionId <= 0) {
461         TRANS_LOGE(TRANS_SVC, "Invaild param, sessionId=%{public}d", sessionId);
462         return SOFTBUS_TRANS_INVALID_SESSION_ID;
463     }
464     return SOFTBUS_OK;
465 }
466 
TransUpdateSocketChannelInfoBySession(const char * sessionName,int32_t sessionId,int32_t channelId,int32_t channelType)467 int32_t TransUpdateSocketChannelInfoBySession(
468     const char *sessionName, int32_t sessionId, int32_t channelId, int32_t channelType)
469 {
470     int32_t ret = CheckParamIsValid(sessionName, sessionId);
471     if (ret != SOFTBUS_OK) {
472         return ret;
473     }
474     if (g_socketChannelList == NULL) {
475         TRANS_LOGE(TRANS_INIT, "socket info manager hasn't init.");
476         return SOFTBUS_NO_INIT;
477     }
478     if (SoftBusMutexLock(&(g_socketChannelList->lock)) != SOFTBUS_OK) {
479         TRANS_LOGE(TRANS_SVC, "lock failed");
480         return SOFTBUS_LOCK_ERR;
481     }
482     SocketWithChannelInfo *socketItem = GetSocketWithChannelInfoBySession(sessionName, sessionId);
483     if (socketItem != NULL) {
484         socketItem->channelId = channelId;
485         socketItem->channelType = channelType;
486         (void)SoftBusMutexUnlock(&(g_socketChannelList->lock));
487         return SOFTBUS_OK;
488     }
489     (void)SoftBusMutexUnlock(&(g_socketChannelList->lock));
490     AnonymizeLogSessionNameWhenNotFound(sessionName, sessionId);
491     return SOFTBUS_NOT_FIND;
492 }
493 
TransUpdateSocketChannelLaneInfoBySession(const char * sessionName,int32_t sessionId,uint32_t laneHandle,bool isQosLane,bool isAsync)494 int32_t TransUpdateSocketChannelLaneInfoBySession(
495     const char *sessionName, int32_t sessionId, uint32_t laneHandle, bool isQosLane, bool isAsync)
496 {
497     int32_t ret = CheckParamIsValid(sessionName, sessionId);
498     if (ret != SOFTBUS_OK) {
499         return ret;
500     }
501     if (g_socketChannelList == NULL) {
502         TRANS_LOGE(TRANS_INIT, "socket info manager hasn't init.");
503         return SOFTBUS_NO_INIT;
504     }
505     if (SoftBusMutexLock(&(g_socketChannelList->lock)) != SOFTBUS_OK) {
506         TRANS_LOGE(TRANS_SVC, "lock failed");
507         return SOFTBUS_LOCK_ERR;
508     }
509     SocketWithChannelInfo *socketItem = GetSocketWithChannelInfoBySession(sessionName, sessionId);
510     if (socketItem != NULL) {
511         socketItem->laneHandle = laneHandle;
512         socketItem->isQosLane = isQosLane;
513         socketItem->isAsync = isAsync;
514         (void)SoftBusMutexUnlock(&(g_socketChannelList->lock));
515         return SOFTBUS_OK;
516     }
517     (void)SoftBusMutexUnlock(&(g_socketChannelList->lock));
518     AnonymizeLogSessionNameWhenNotFound(sessionName, sessionId);
519     return SOFTBUS_NOT_FIND;
520 }
521 
TransDeleteSocketChannelInfoBySession(const char * sessionName,int32_t sessionId)522 int32_t TransDeleteSocketChannelInfoBySession(const char *sessionName, int32_t sessionId)
523 {
524     int32_t ret = CheckParamIsValid(sessionName, sessionId);
525     if (ret != SOFTBUS_OK) {
526         return ret;
527     }
528     if (g_socketChannelList == NULL) {
529         TRANS_LOGE(TRANS_INIT, "socket info manager hasn't init.");
530         return SOFTBUS_NO_INIT;
531     }
532     if (SoftBusMutexLock(&(g_socketChannelList->lock)) != SOFTBUS_OK) {
533         TRANS_LOGE(TRANS_SVC, "lock failed");
534         return SOFTBUS_LOCK_ERR;
535     }
536     SocketWithChannelInfo *socketItem = NULL;
537     SocketWithChannelInfo *next = NULL;
538     LIST_FOR_EACH_ENTRY_SAFE(socketItem, next, &(g_socketChannelList->list), SocketWithChannelInfo, node) {
539         if (strcmp(socketItem->sessionName, sessionName) == 0 && socketItem->sessionId == sessionId) {
540             ListDelete(&(socketItem->node));
541             g_socketChannelList->cnt--;
542             SoftBusFree(socketItem);
543             (void)SoftBusMutexUnlock(&(g_socketChannelList->lock));
544             char *tmpName = NULL;
545             Anonymize(sessionName, &tmpName);
546             TRANS_LOGI(TRANS_CTRL, "delete socket channel info, sessionName=%{public}s, sessionId=%{public}d",
547                 AnonymizeWrapper(tmpName), sessionId);
548             AnonymizeFree(tmpName);
549             return SOFTBUS_OK;
550         }
551     }
552     (void)SoftBusMutexUnlock(&(g_socketChannelList->lock));
553     AnonymizeLogSessionNameWhenNotFound(sessionName, sessionId);
554     return SOFTBUS_NOT_FIND;
555 }
556 
TransDeleteSocketChannelInfoByChannel(int32_t channelId,int32_t channelType)557 int32_t TransDeleteSocketChannelInfoByChannel(int32_t channelId, int32_t channelType)
558 {
559     if (g_socketChannelList == NULL) {
560         TRANS_LOGE(TRANS_INIT, "socket info manager hasn't init.");
561         return SOFTBUS_NO_INIT;
562     }
563     if (SoftBusMutexLock(&(g_socketChannelList->lock)) != SOFTBUS_OK) {
564         TRANS_LOGE(TRANS_SVC, "lock failed");
565         return SOFTBUS_LOCK_ERR;
566     }
567     SocketWithChannelInfo *socketItem = NULL;
568     SocketWithChannelInfo *next = NULL;
569     LIST_FOR_EACH_ENTRY_SAFE(socketItem, next, &(g_socketChannelList->list), SocketWithChannelInfo, node) {
570         if (socketItem->channelId == channelId && socketItem->channelType == channelType) {
571             ListDelete(&(socketItem->node));
572             g_socketChannelList->cnt--;
573             SoftBusFree(socketItem);
574             (void)SoftBusMutexUnlock(&(g_socketChannelList->lock));
575             TRANS_LOGI(TRANS_CTRL, "delete socket channel info, channelId=%{public}d, channelType=%{public}d",
576                 channelId, channelType);
577             return SOFTBUS_OK;
578         }
579     }
580     (void)SoftBusMutexUnlock(&(g_socketChannelList->lock));
581     TRANS_LOGD(
582         TRANS_SVC, "socket info not found. channelId=%{public}d, channelType=%{public}d", channelId, channelType);
583     return SOFTBUS_NOT_FIND;
584 }
585 
TransDeleteSocketChannelInfoByPid(int32_t pid)586 int32_t TransDeleteSocketChannelInfoByPid(int32_t pid)
587 {
588     if (g_socketChannelList == NULL) {
589         TRANS_LOGE(TRANS_INIT, "socket info manager hasn't init.");
590         return SOFTBUS_NO_INIT;
591     }
592     if (SoftBusMutexLock(&(g_socketChannelList->lock)) != SOFTBUS_OK) {
593         TRANS_LOGE(TRANS_SVC, "lock failed");
594         return SOFTBUS_LOCK_ERR;
595     }
596     int32_t delCount = 0;
597     SocketWithChannelInfo *socketItem = NULL;
598     SocketWithChannelInfo *next = NULL;
599     LIST_FOR_EACH_ENTRY_SAFE(socketItem, next, &(g_socketChannelList->list), SocketWithChannelInfo, node) {
600         if (socketItem->pid == pid) {
601             ListDelete(&(socketItem->node));
602             g_socketChannelList->cnt--;
603             SoftBusFree(socketItem);
604             delCount++;
605         }
606     }
607     (void)SoftBusMutexUnlock(&(g_socketChannelList->lock));
608     if (delCount > 0) {
609         TRANS_LOGI(TRANS_CTRL, "delete socket channel info, pid=%{public}d delete count=%{public}d",
610             pid, delCount);
611         return SOFTBUS_OK;
612     }
613     return SOFTBUS_NOT_FIND;
614 }
615 
TransSetSocketChannelStateBySession(const char * sessionName,int32_t sessionId,CoreSessionState state)616 int32_t TransSetSocketChannelStateBySession(const char *sessionName, int32_t sessionId, CoreSessionState state)
617 {
618     int32_t ret = CheckParamIsValid(sessionName, sessionId);
619     if (ret != SOFTBUS_OK) {
620         return ret;
621     }
622     if (g_socketChannelList == NULL) {
623         TRANS_LOGE(TRANS_INIT, "socket info manager hasn't init.");
624         return SOFTBUS_NO_INIT;
625     }
626     if (SoftBusMutexLock(&(g_socketChannelList->lock)) != SOFTBUS_OK) {
627         TRANS_LOGE(TRANS_SVC, "lock failed");
628         return SOFTBUS_LOCK_ERR;
629     }
630     SocketWithChannelInfo *socketItem = GetSocketWithChannelInfoBySession(sessionName, sessionId);
631     if (socketItem != NULL) {
632         socketItem->state = state;
633         (void)SoftBusMutexUnlock(&(g_socketChannelList->lock));
634         return SOFTBUS_OK;
635     }
636     (void)SoftBusMutexUnlock(&(g_socketChannelList->lock));
637     AnonymizeLogSessionNameWhenNotFound(sessionName, sessionId);
638     return SOFTBUS_NOT_FIND;
639 }
640 
TransSetSocketChannelStateByChannel(int32_t channelId,int32_t channelType,CoreSessionState state)641 int32_t TransSetSocketChannelStateByChannel(int32_t channelId, int32_t channelType, CoreSessionState state)
642 {
643     if (g_socketChannelList == NULL) {
644         TRANS_LOGE(TRANS_INIT, "socket info manager hasn't init.");
645         return SOFTBUS_NO_INIT;
646     }
647     if (SoftBusMutexLock(&(g_socketChannelList->lock)) != SOFTBUS_OK) {
648         TRANS_LOGE(TRANS_SVC, "lock failed");
649         return SOFTBUS_LOCK_ERR;
650     }
651     SocketWithChannelInfo *socketItem = NULL;
652     LIST_FOR_EACH_ENTRY(socketItem, &(g_socketChannelList->list), SocketWithChannelInfo, node) {
653         if (socketItem->channelId == channelId && socketItem->channelType == channelType) {
654             socketItem->state = state;
655             (void)SoftBusMutexUnlock(&(g_socketChannelList->lock));
656             return SOFTBUS_OK;
657         }
658     }
659     (void)SoftBusMutexUnlock(&(g_socketChannelList->lock));
660     TRANS_LOGE(
661         TRANS_SVC, "socket info not found. channelId=%{public}d, channelType=%{public}d", channelId, channelType);
662     return SOFTBUS_NOT_FIND;
663 }
664 
TransGetSocketChannelStateBySession(const char * sessionName,int32_t sessionId,CoreSessionState * state)665 int32_t TransGetSocketChannelStateBySession(const char *sessionName, int32_t sessionId, CoreSessionState *state)
666 {
667     int32_t ret = CheckParamIsValid(sessionName, sessionId);
668     if (ret != SOFTBUS_OK) {
669         return ret;
670     }
671     if (g_socketChannelList == NULL) {
672         TRANS_LOGE(TRANS_INIT, "socket info manager hasn't init.");
673         return SOFTBUS_NO_INIT;
674     }
675     if (state == NULL) {
676         TRANS_LOGE(TRANS_SVC, "Invaild param, state is null");
677         return SOFTBUS_INVALID_PARAM;
678     }
679     if (SoftBusMutexLock(&(g_socketChannelList->lock)) != SOFTBUS_OK) {
680         TRANS_LOGE(TRANS_SVC, "lock failed");
681         return SOFTBUS_LOCK_ERR;
682     }
683     SocketWithChannelInfo *socketItem = GetSocketWithChannelInfoBySession(sessionName, sessionId);
684     if (socketItem != NULL) {
685         *state = socketItem->state;
686         (void)SoftBusMutexUnlock(&(g_socketChannelList->lock));
687         return SOFTBUS_OK;
688     }
689     (void)SoftBusMutexUnlock(&(g_socketChannelList->lock));
690     AnonymizeLogSessionNameWhenNotFound(sessionName, sessionId);
691     return SOFTBUS_NOT_FIND;
692 }
693 
TransGetSocketChannelLaneInfoBySession(const char * sessionName,int32_t sessionId,uint32_t * laneHandle,bool * isQosLane,bool * isAsync)694 int32_t TransGetSocketChannelLaneInfoBySession(
695     const char *sessionName, int32_t sessionId, uint32_t *laneHandle, bool *isQosLane, bool *isAsync)
696 {
697     int32_t ret = CheckParamIsValid(sessionName, sessionId);
698     if (ret != SOFTBUS_OK) {
699         return ret;
700     }
701     if (g_socketChannelList == NULL) {
702         TRANS_LOGE(TRANS_INIT, "socket info manager hasn't init.");
703         return SOFTBUS_NO_INIT;
704     }
705     if (SoftBusMutexLock(&(g_socketChannelList->lock)) != SOFTBUS_OK) {
706         TRANS_LOGE(TRANS_SVC, "lock failed");
707         return SOFTBUS_LOCK_ERR;
708     }
709     SocketWithChannelInfo *socketItem = GetSocketWithChannelInfoBySession(sessionName, sessionId);
710     if (socketItem != NULL) {
711         if (laneHandle != NULL) {
712             *laneHandle = socketItem->laneHandle;
713         }
714         if (isQosLane != NULL) {
715             *isQosLane = socketItem->isQosLane;
716         }
717         if (isAsync != NULL) {
718             *isAsync = socketItem->isAsync;
719         }
720         (void)SoftBusMutexUnlock(&(g_socketChannelList->lock));
721         return SOFTBUS_OK;
722     }
723     (void)SoftBusMutexUnlock(&(g_socketChannelList->lock));
724     AnonymizeLogSessionNameWhenNotFound(sessionName, sessionId);
725     return SOFTBUS_NOT_FIND;
726 }
727 
TransGetSocketChannelStateByChannel(int32_t channelId,int32_t channelType,CoreSessionState * state)728 int32_t TransGetSocketChannelStateByChannel(int32_t channelId, int32_t channelType, CoreSessionState *state)
729 {
730     if (state == NULL) {
731         TRANS_LOGE(TRANS_SVC, "Invaild param");
732         return SOFTBUS_INVALID_PARAM;
733     }
734 
735     if (g_socketChannelList == NULL) {
736         TRANS_LOGE(TRANS_INIT, "socket info manager hasn't init.");
737         return SOFTBUS_NO_INIT;
738     }
739     if (SoftBusMutexLock(&(g_socketChannelList->lock)) != SOFTBUS_OK) {
740         TRANS_LOGE(TRANS_SVC, "lock failed");
741         return SOFTBUS_LOCK_ERR;
742     }
743     SocketWithChannelInfo *socketItem = NULL;
744     LIST_FOR_EACH_ENTRY(socketItem, &(g_socketChannelList->list), SocketWithChannelInfo, node) {
745         if (socketItem->channelId == channelId && socketItem->channelType == channelType) {
746             *state = socketItem->state;
747             (void)SoftBusMutexUnlock(&(g_socketChannelList->lock));
748             return SOFTBUS_OK;
749         }
750     }
751     (void)SoftBusMutexUnlock(&(g_socketChannelList->lock));
752     TRANS_LOGE(
753         TRANS_SVC, "socket info not found. channelId=%{public}d, channelType=%{public}d", channelId, channelType);
754     return SOFTBUS_NOT_FIND;
755 }
756 
TransGetPidFromSocketChannelInfoBySession(const char * sessionName,int32_t sessionId,int32_t * pid)757 int32_t TransGetPidFromSocketChannelInfoBySession(const char *sessionName, int32_t sessionId, int32_t *pid)
758 {
759     int32_t ret = CheckParamIsValid(sessionName, sessionId);
760     if (ret != SOFTBUS_OK) {
761         return ret;
762     }
763     if (g_socketChannelList == NULL) {
764         TRANS_LOGE(TRANS_INIT, "socket info manager hasn't init.");
765         return SOFTBUS_NO_INIT;
766     }
767     if (pid == NULL) {
768         TRANS_LOGE(TRANS_SVC, "Invaild param, pid is null");
769         return SOFTBUS_INVALID_PARAM;
770     }
771     if (SoftBusMutexLock(&(g_socketChannelList->lock)) != SOFTBUS_OK) {
772         TRANS_LOGE(TRANS_SVC, "lock failed");
773         return SOFTBUS_LOCK_ERR;
774     }
775     SocketWithChannelInfo *socketItem = GetSocketWithChannelInfoBySession(sessionName, sessionId);
776     if (socketItem != NULL) {
777         *pid = socketItem->pid;
778         (void)SoftBusMutexUnlock(&(g_socketChannelList->lock));
779         return SOFTBUS_OK;
780     }
781     (void)SoftBusMutexUnlock(&(g_socketChannelList->lock));
782     AnonymizeLogSessionNameWhenNotFound(sessionName, sessionId);
783     return SOFTBUS_NOT_FIND;
784 }
785 
ConvertLaneLinkTypeToConnectType(LaneLinkType laneLinkType)786 static ConnectType ConvertLaneLinkTypeToConnectType(LaneLinkType laneLinkType)
787 {
788     switch (laneLinkType) {
789         case LANE_BR:
790             return CONNECT_BR;
791         case LANE_BLE:
792         case LANE_COC:
793             return CONNECT_BLE;
794         case LANE_P2P:
795             return CONNECT_P2P;
796         case LANE_WLAN_2P4G:
797         case LANE_WLAN_5G:
798         case LANE_ETH:
799             return CONNECT_TCP;
800         case LANE_P2P_REUSE:
801             return CONNECT_P2P_REUSE;
802         case LANE_BLE_DIRECT:
803         case LANE_COC_DIRECT:
804             return CONNECT_BLE_DIRECT;
805         case LANE_HML:
806             return CONNECT_HML;
807         case LANE_BLE_REUSE:
808             return CONNECT_BLE;
809         default:
810             return CONNECT_TYPE_MAX;
811     }
812 }
813 
TransGetConnectTypeByChannelId(int32_t channelId,ConnectType * connectType)814 int32_t TransGetConnectTypeByChannelId(int32_t channelId, ConnectType *connectType)
815 {
816     if (connectType == NULL) {
817         TRANS_LOGE(TRANS_INIT, "connectType is null");
818         return SOFTBUS_INVALID_PARAM;
819     }
820 
821     if (g_channelLaneList == NULL) {
822         TRANS_LOGE(TRANS_INIT, "trans lane manager hasn't init.");
823         return SOFTBUS_INVALID_PARAM;
824     }
825 
826     if (SoftBusMutexLock(&(g_channelLaneList->lock)) != SOFTBUS_OK) {
827         TRANS_LOGE(TRANS_SVC, "lock failed");
828         return SOFTBUS_LOCK_ERR;
829     }
830 
831     TransLaneInfo *item = NULL;
832     LIST_FOR_EACH_ENTRY(item, &(g_channelLaneList->list), TransLaneInfo, node) {
833         if (item->channelId != channelId) {
834             continue;
835         }
836 
837         ConnectType connType = ConvertLaneLinkTypeToConnectType(item->laneConnInfo.type);
838         if (connType >= CONNECT_TYPE_MAX) {
839             TRANS_LOGE(TRANS_SVC, "invalid connectType=%{public}d. linkType=%{public}d, channelId=%{public}d",
840                 connType, item->laneConnInfo.type, channelId);
841             (void)SoftBusMutexUnlock(&(g_channelLaneList->lock));
842             return SOFTBUS_CONN_INVALID_CONN_TYPE;
843         }
844 
845         *connectType = connType;
846         (void)SoftBusMutexUnlock(&(g_channelLaneList->lock));
847         return SOFTBUS_OK;
848     }
849     (void)SoftBusMutexUnlock(&(g_channelLaneList->lock));
850     TRANS_LOGE(TRANS_SVC, "can not find connectType by channelId=%{public}d", channelId);
851     return SOFTBUS_TRANS_INVALID_CHANNEL_ID;
852 }
853 
TransGetTransLaneInfoByLaneHandle(uint32_t laneHandle,TransLaneInfo * laneInfo)854 int32_t TransGetTransLaneInfoByLaneHandle(uint32_t laneHandle, TransLaneInfo *laneInfo)
855 {
856     if (laneInfo == NULL) {
857         TRANS_LOGE(TRANS_INIT, "laneInfo is null");
858         return SOFTBUS_INVALID_PARAM;
859     }
860     if (g_channelLaneList == NULL) {
861         TRANS_LOGE(TRANS_INIT, "trans lane manager hasn't init.");
862         return SOFTBUS_INVALID_PARAM;
863     }
864 
865     if (SoftBusMutexLock(&(g_channelLaneList->lock)) != SOFTBUS_OK) {
866         TRANS_LOGE(TRANS_SVC, "lock failed");
867         return SOFTBUS_LOCK_ERR;
868     }
869     TransLaneInfo *item = NULL;
870     LIST_FOR_EACH_ENTRY(item, &(g_channelLaneList->list), TransLaneInfo, node) {
871         if (item->laneHandle == laneHandle) {
872             if (memcpy_s(laneInfo, sizeof(TransLaneInfo), item, sizeof(TransLaneInfo)) != EOK) {
873                 (void)SoftBusMutexUnlock(&(g_channelLaneList->lock));
874                 return SOFTBUS_MEM_ERR;
875             }
876             (void)SoftBusMutexUnlock(&(g_channelLaneList->lock));
877             return SOFTBUS_OK;
878         }
879     }
880     (void)SoftBusMutexUnlock(&(g_channelLaneList->lock));
881     TRANS_LOGE(TRANS_SVC, "can not find laneInfo by laneHandle=%{public}u", laneHandle);
882     return SOFTBUS_NOT_FIND;
883 }