1 /*
2 * Copyright (c) 2022 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 "lnn_trans_lane.h"
17
18 #include <securec.h>
19
20 #include "anonymizer.h"
21 #include "bus_center_manager.h"
22 #include "common_list.h"
23 #include "g_enhance_lnn_func.h"
24 #include "g_enhance_lnn_func_pack.h"
25 #include "lnn_distributed_net_ledger.h"
26 #include "lnn_event.h"
27 #include "lnn_event_form.h"
28 #include "lnn_heartbeat_utils.h"
29 #include "lnn_lane.h"
30 #include "lnn_lane_common.h"
31 #include "lnn_lane_def.h"
32 #include "lnn_lane_dfx.h"
33 #include "lnn_lane_interface.h"
34 #include "lnn_lane_link_p2p.h"
35 #include "lnn_lane_listener.h"
36 #include "lnn_lane_model.h"
37 #include "lnn_lane_reliability.h"
38 #include "lnn_log.h"
39 #include "lnn_select_rule.h"
40 #include "lnn_trans_free_lane.h"
41 #include "lnn_lane_link.h"
42 #include "message_handler.h"
43 #include "softbus_adapter_mem.h"
44 #include "softbus_def.h"
45 #include "softbus_error_code.h"
46 #include "softbus_protocol_def.h"
47 #include "softbus_utils.h"
48 #include "softbus_init_common.h"
49 #include "wifi_direct_error_code.h"
50 #include "wifi_direct_manager.h"
51
52 #define DEFAULT_LINK_LATENCY 30000
53 #define WIFI_DIRECET_NUM_LIMIT 4
54
55 typedef enum {
56 MSG_TYPE_LANE_TRIGGER_LINK = 0,
57 MSG_TYPE_LANE_LINK_SUCCESS,
58 MSG_TYPE_LANE_LINK_FAIL,
59 MSG_TYPE_LANE_STATE_CHANGE,
60 MSG_TYPE_DELAY_DESTROY_LINK,
61 MSG_TYPE_LANE_DETECT_TIMEOUT,
62 MSG_TYPE_LANE_LINK_TIMEOUT,
63 MSG_TYPE_NOTIFY_FREE_LANE_RESULT,
64 } LaneMsgType;
65
66 typedef struct {
67 uint32_t cnt;
68 ListNode list;
69 } TransLaneList;
70
71 typedef enum {
72 BUILD_LINK_STATUS_BUILDING = 0,
73 BUILD_LINK_STATUS_FAIL,
74 BUILD_LINK_STATUS_SUCC,
75 BUILD_LINK_STATUS_BUTT,
76 } BuildLinkStatus;
77
78 typedef struct {
79 BuildLinkStatus status;
80 int32_t result;
81 LaneLinkInfo linkInfo;
82 } LinkStatusInfo;
83
84 typedef struct {
85 ListNode node;
86 uint32_t laneReqId;
87 int32_t pid;
88 char networkId[NETWORK_ID_BUF_LEN];
89 LanePreferredLinkList *linkList; /* Mem provided by laneSelect module */
90 uint32_t listNum;
91 uint32_t linkRetryIdx;
92 bool networkDelegate;
93 uint32_t bandWidth;
94 uint64_t triggerLinkTime;
95 uint64_t availableLinkTime;
96 char peerBleMac[MAX_MAC_LEN];
97 LaneTransType transType;
98 ProtocolType acceptableProtocols;
99 // OldInfo
100 int32_t psm;
101 bool p2pOnly;
102 LinkStatusInfo statusList[LANE_LINK_TYPE_BUTT];
103 bool isCompleted;
104 uint32_t actionAddr;
105 bool isSupportIpv6;
106 bool isVirtualLink;
107 bool isInnerCalled; // Indicates whether to select a link for TransOpenNetWorkingChannel
108 } LaneLinkNodeInfo;
109
110 typedef struct {
111 LaneState state;
112 char peerUdid[UDID_BUF_LEN];
113 LaneLinkInfo laneLinkInfo;
114 } StateNotifyInfo;
115
116 typedef struct {
117 int32_t reason;
118 LaneLinkType linkType;
119 } LinkFailInfo;
120
121 typedef struct {
122 uint32_t laneReqId;
123 LaneLinkType linkType;
124 } LaneTimeoutInfo;
125
126 static ListNode g_multiLinkList;
127 static SoftBusMutex g_transLaneMutex;
128 static TransLaneList *g_requestList = NULL;
129 static SoftBusHandler g_laneLoopHandler;
130 static ILaneIdStateListener *g_laneIdCallback = NULL;
131
Lock(void)132 static int32_t Lock(void)
133 {
134 return SoftBusMutexLock(&g_transLaneMutex);
135 }
136
Unlock(void)137 static void Unlock(void)
138 {
139 (void)SoftBusMutexUnlock(&g_transLaneMutex);
140 }
141
LnnLanePostMsgToHandler(int32_t msgType,uint64_t param1,uint64_t param2,void * obj,uint64_t delayMillis)142 static int32_t LnnLanePostMsgToHandler(int32_t msgType, uint64_t param1, uint64_t param2,
143 void *obj, uint64_t delayMillis)
144 {
145 SoftBusMessage *msg = (SoftBusMessage *)SoftBusCalloc(sizeof(SoftBusMessage));
146 if (msg == NULL) {
147 LNN_LOGE(LNN_LANE, "[transLane]create handler msg failed");
148 return SOFTBUS_MALLOC_ERR;
149 }
150 msg->what = msgType;
151 msg->arg1 = param1;
152 msg->arg2 = param2;
153 msg->handler = &g_laneLoopHandler;
154 msg->obj = obj;
155 if (delayMillis == 0) {
156 g_laneLoopHandler.looper->PostMessage(g_laneLoopHandler.looper, msg);
157 } else {
158 g_laneLoopHandler.looper->PostMessageDelay(g_laneLoopHandler.looper, msg, delayMillis);
159 }
160 return SOFTBUS_OK;
161 }
162
RemoveLinkTimeout(const SoftBusMessage * msg,void * data)163 static int32_t RemoveLinkTimeout(const SoftBusMessage *msg, void *data)
164 {
165 LaneTimeoutInfo *info = (LaneTimeoutInfo *)data;
166 if (msg->what != MSG_TYPE_LANE_LINK_TIMEOUT || msg->arg1 != info->laneReqId) {
167 return SOFTBUS_INVALID_PARAM;
168 }
169 if (info->linkType == LANE_LINK_TYPE_BUTT) {
170 LNN_LOGI(LNN_LANE, "remove build link timeout message succ. laneReqId=%{public}u, linkType=%{public}d",
171 info->laneReqId, info->linkType);
172 return SOFTBUS_OK;
173 }
174 if (msg->arg2 == info->linkType) {
175 LNN_LOGI(LNN_LANE, "remove build link timeout message succ. laneReqId=%{public}u, linkType=%{public}d",
176 info->laneReqId, info->linkType);
177 return SOFTBUS_OK;
178 }
179 return SOFTBUS_INVALID_PARAM;
180 }
181
RemoveLinkTimeoutMessage(uint32_t laneReqId,LaneLinkType linkType)182 static void RemoveLinkTimeoutMessage(uint32_t laneReqId, LaneLinkType linkType)
183 {
184 LNN_LOGI(LNN_LANE, "remove build link timeout message. laneReqId=%{public}u, linkType=%{public}d",
185 laneReqId, linkType);
186 LaneTimeoutInfo info = {
187 .laneReqId = laneReqId,
188 .linkType = linkType,
189 };
190 g_laneLoopHandler.looper->RemoveMessageCustom(g_laneLoopHandler.looper, &g_laneLoopHandler,
191 RemoveLinkTimeout, &info);
192 }
193
LinkSuccess(uint32_t laneReqId,LaneLinkType linkType,const LaneLinkInfo * linkInfo)194 static void LinkSuccess(uint32_t laneReqId, LaneLinkType linkType, const LaneLinkInfo *linkInfo)
195 {
196 LNN_LOGI(LNN_LANE, "build link succ, laneReqId=%{public}u, link=%{public}d", laneReqId, linkType);
197 if (linkInfo == NULL) {
198 LNN_LOGE(LNN_LANE, "linkSuccess param invalid");
199 return;
200 }
201 RemoveLinkTimeoutMessage(laneReqId, linkType);
202 LaneLinkInfo *linkParam = (LaneLinkInfo *)SoftBusCalloc(sizeof(LaneLinkInfo));
203 if (linkParam == NULL) {
204 LNN_LOGE(LNN_LANE, "linkSuccess info malloc fail");
205 (void)LnnLanePostMsgToHandler(MSG_TYPE_LANE_LINK_FAIL, laneReqId, SOFTBUS_MALLOC_ERR, NULL, 0);
206 return;
207 }
208 if (memcpy_s(linkParam, sizeof(LaneLinkInfo), linkInfo, sizeof(LaneLinkInfo)) != EOK) {
209 LNN_LOGE(LNN_LANE, "linkParam memcpy fail, laneReqId=%{public}u", laneReqId);
210 SoftBusFree(linkParam);
211 (void)LnnLanePostMsgToHandler(MSG_TYPE_LANE_LINK_FAIL, laneReqId, SOFTBUS_MEM_ERR, NULL, 0);
212 return;
213 }
214 if (LnnLanePostMsgToHandler(MSG_TYPE_LANE_LINK_SUCCESS, laneReqId, linkType, linkParam, 0) != SOFTBUS_OK) {
215 LNN_LOGE(LNN_LANE, "post LaneLinkSuccess msg err, laneReqId=%{public}u", laneReqId);
216 SoftBusFree(linkParam);
217 }
218 }
219
LinkFail(uint32_t laneReqId,int32_t reason,LaneLinkType linkType)220 static void LinkFail(uint32_t laneReqId, int32_t reason, LaneLinkType linkType)
221 {
222 LNN_LOGE(LNN_LANE, "build link fail, laneReqId=%{public}u, link=%{public}d, reason=%{public}d",
223 laneReqId, linkType, reason);
224 RemoveLinkTimeoutMessage(laneReqId, linkType);
225 LinkFailInfo *failInfo = (LinkFailInfo *)SoftBusCalloc(sizeof(LinkFailInfo));
226 if (failInfo == NULL) {
227 LNN_LOGE(LNN_LANE, "failInfo malloc fail");
228 (void)LnnLanePostMsgToHandler(MSG_TYPE_LANE_LINK_FAIL, laneReqId, SOFTBUS_MALLOC_ERR, NULL, 0);
229 return;
230 }
231 failInfo->reason = reason;
232 failInfo->linkType = linkType;
233 if (LnnLanePostMsgToHandler(MSG_TYPE_LANE_LINK_FAIL, laneReqId, (uint64_t)reason, failInfo, 0) != SOFTBUS_OK) {
234 LNN_LOGE(LNN_LANE, "post lanelink fail msg err");
235 SoftBusFree(failInfo);
236 }
237 }
238
DeleteLaneLinkNode(uint32_t laneReqId)239 static void DeleteLaneLinkNode(uint32_t laneReqId)
240 {
241 if (Lock() != SOFTBUS_OK) {
242 LNN_LOGE(LNN_LANE, "get lock fail");
243 return;
244 }
245 LaneLinkNodeInfo *item = NULL;
246 LaneLinkNodeInfo *next = NULL;
247 LIST_FOR_EACH_ENTRY_SAFE(item, next, &g_multiLinkList, LaneLinkNodeInfo, node) {
248 if (item->laneReqId == laneReqId) {
249 ListDelete(&item->node);
250 SoftBusFree(item->linkList);
251 SoftBusFree(item);
252 break;
253 }
254 }
255 Unlock();
256 }
257
PostLinkTimeoutMessage(uint32_t laneReqId,LaneLinkType linkType,uint64_t delayMillis)258 static int32_t PostLinkTimeoutMessage(uint32_t laneReqId, LaneLinkType linkType, uint64_t delayMillis)
259 {
260 LNN_LOGI(LNN_LANE, "post build link timeout message, laneReqId=%{public}u, linkType=%{public}d",
261 laneReqId, linkType);
262 return LnnLanePostMsgToHandler(MSG_TYPE_LANE_LINK_TIMEOUT, laneReqId, linkType, NULL, delayMillis);
263 }
264
InitStatusList(LaneLinkNodeInfo * linkNode)265 static void InitStatusList(LaneLinkNodeInfo *linkNode)
266 {
267 for (uint32_t i = 0; i < LANE_LINK_TYPE_BUTT; i++) {
268 linkNode->statusList[i].status = BUILD_LINK_STATUS_BUTT;
269 linkNode->statusList[i].result = SOFTBUS_LANE_BUILD_LINK_FAIL;
270 (void)memset_s(&linkNode->statusList[i].linkInfo, sizeof(LaneLinkInfo), 0, sizeof(LaneLinkInfo));
271 }
272 }
273
TriggerLink(uint32_t laneReqId,TransOption * request,LanePreferredLinkList * recommendLinkList)274 static int32_t TriggerLink(uint32_t laneReqId, TransOption *request,
275 LanePreferredLinkList *recommendLinkList)
276 {
277 LaneLinkNodeInfo *linkNode = (LaneLinkNodeInfo *)SoftBusCalloc(sizeof(LaneLinkNodeInfo));
278 if (linkNode == NULL) {
279 return SOFTBUS_MALLOC_ERR;
280 }
281 if (memcpy_s(linkNode->networkId, NETWORK_ID_BUF_LEN,
282 request->networkId, NETWORK_ID_BUF_LEN) != EOK) {
283 LNN_LOGE(LNN_LANE, "memcpy fail for networkId");
284 SoftBusFree(linkNode);
285 return SOFTBUS_MEM_ERR;
286 }
287 if (memcpy_s(linkNode->peerBleMac, MAX_MAC_LEN, request->peerBleMac, MAX_MAC_LEN) != EOK) {
288 LNN_LOGE(LNN_LANE, "memcpy fail for peerBleMac");
289 SoftBusFree(linkNode);
290 return SOFTBUS_MEM_ERR;
291 }
292 linkNode->psm = request->psm;
293 linkNode->transType = request->transType;
294 linkNode->laneReqId = laneReqId;
295 linkNode->linkRetryIdx = 0;
296 linkNode->listNum = recommendLinkList->linkTypeNum;
297 linkNode->linkList = recommendLinkList;
298 linkNode->pid = request->pid;
299 linkNode->networkDelegate = request->networkDelegate;
300 linkNode->p2pOnly = request->p2pOnly;
301 linkNode->acceptableProtocols = request->acceptableProtocols;
302 linkNode->bandWidth = 0;
303 linkNode->triggerLinkTime = SoftBusGetSysTimeMs();
304 linkNode->availableLinkTime = DEFAULT_LINK_LATENCY;
305 linkNode->isCompleted = false;
306 linkNode->isInnerCalled = request->isInnerCalled;
307 InitStatusList(linkNode);
308 ListInit(&linkNode->node);
309 if (Lock() != SOFTBUS_OK) {
310 LNN_LOGE(LNN_LANE, "get lock fail");
311 SoftBusFree(linkNode);
312 return SOFTBUS_LOCK_ERR;
313 }
314 ListTailInsert(&g_multiLinkList, &linkNode->node);
315 Unlock();
316 int32_t ret = LnnLanePostMsgToHandler(MSG_TYPE_LANE_TRIGGER_LINK, laneReqId, 0, NULL, 0);
317 if (ret != SOFTBUS_OK) {
318 DeleteLaneLinkNode(laneReqId);
319 return ret;
320 }
321 return SOFTBUS_OK;
322 }
323
CreateRequestNode(uint32_t laneReqId,const TransOption * option,const ILaneListener * listener)324 static TransReqInfo *CreateRequestNode(uint32_t laneReqId, const TransOption *option, const ILaneListener *listener)
325 {
326 TransReqInfo *newNode = (TransReqInfo *)SoftBusCalloc(sizeof(TransReqInfo));
327 if (newNode == NULL) {
328 LNN_LOGE(LNN_LANE, "malloc fail");
329 return NULL;
330 }
331 if (memcpy_s(&newNode->extraInfo.listener, sizeof(ILaneListener), listener, sizeof(ILaneListener)) != EOK) {
332 SoftBusFree(newNode);
333 return NULL;
334 }
335 if (memcpy_s(&newNode->extraInfo.info, sizeof(TransOption), option, sizeof(TransOption)) != EOK) {
336 SoftBusFree(newNode);
337 return NULL;
338 }
339 newNode->isWithQos = false;
340 newNode->isCanceled = false;
341 newNode->isNotified = false;
342 newNode->notifyFree = false;
343 newNode->hasNotifiedFree = false;
344 newNode->laneReqId = laneReqId;
345 newNode->extraInfo.isSupportIpv6 = option->isSupportIpv6;
346 ListInit(&newNode->node);
347 return newNode;
348 }
349
DeleteRequestNode(uint32_t laneReqId)350 int32_t DeleteRequestNode(uint32_t laneReqId)
351 {
352 if (laneReqId == INVALID_LANE_REQ_ID) {
353 LNN_LOGE(LNN_LANE, "laneHandle is invalid parameter");
354 return SOFTBUS_INVALID_PARAM;
355 }
356 if (Lock() != SOFTBUS_OK) {
357 LNN_LOGE(LNN_LANE, "get lock fail");
358 return SOFTBUS_LOCK_ERR;
359 }
360 TransReqInfo *item = NULL;
361 TransReqInfo *next = NULL;
362 LIST_FOR_EACH_ENTRY_SAFE(item, next, &g_requestList->list, TransReqInfo, node) {
363 if (item->laneReqId == laneReqId) {
364 ListDelete(&item->node);
365 SoftBusFree(item);
366 g_requestList->cnt--;
367 Unlock();
368 return SOFTBUS_OK;
369 }
370 }
371 Unlock();
372 LNN_LOGE(LNN_LANE, "not found request node, laneReqId=%{public}d", laneReqId);
373 return SOFTBUS_LANE_NOT_FOUND;
374 }
375
CheckVirtualLinkByLaneReqId(uint32_t laneReqId)376 bool CheckVirtualLinkByLaneReqId(uint32_t laneReqId)
377 {
378 if (laneReqId == INVALID_LANE_REQ_ID) {
379 LNN_LOGE(LNN_LANE, "laneHandle is invalid parameter");
380 return false;
381 }
382 if (Lock() != SOFTBUS_OK) {
383 LNN_LOGE(LNN_LANE, "get lock fail");
384 return false;
385 }
386 TransReqInfo *item = NULL;
387 TransReqInfo *next = NULL;
388 LIST_FOR_EACH_ENTRY_SAFE(item, next, &g_requestList->list, TransReqInfo, node) {
389 if (item->laneReqId == laneReqId) {
390 Unlock();
391 LNN_LOGE(LNN_LANE, "found virtual request node, laneReqId=%{public}d", laneReqId);
392 return item->allocInfo.extendInfo.isVirtualLink;
393 }
394 }
395 Unlock();
396 LNN_LOGE(LNN_LANE, "not found request node, laneReqId=%{public}d", laneReqId);
397 return false;
398 }
399
CreateReqNodeWithQos(uint32_t laneReqId,const LaneAllocInfo * allocInfo,const LaneAllocListener * listener)400 static TransReqInfo *CreateReqNodeWithQos(uint32_t laneReqId, const LaneAllocInfo *allocInfo,
401 const LaneAllocListener *listener)
402 {
403 TransReqInfo *newNode = (TransReqInfo *)SoftBusCalloc(sizeof(TransReqInfo));
404 if (newNode == NULL) {
405 LNN_LOGE(LNN_LANE, "malloc fail");
406 return NULL;
407 }
408 if (memcpy_s(&newNode->listener, sizeof(LaneAllocListener), listener, sizeof(LaneAllocListener)) != EOK) {
409 LNN_LOGE(LNN_LANE, "memcpy fail for lane alloc listener");
410 SoftBusFree(newNode);
411 return NULL;
412 }
413 if (memcpy_s(&newNode->allocInfo, sizeof(LaneAllocInfo), allocInfo, sizeof(LaneAllocInfo)) != EOK) {
414 LNN_LOGE(LNN_LANE, "memcpy fail for lane alloc info");
415 SoftBusFree(newNode);
416 return NULL;
417 }
418 newNode->extraInfo.actionAddr = allocInfo->extendInfo.actionAddr;
419 newNode->extraInfo.isSupportIpv6 = allocInfo->extendInfo.isSupportIpv6;
420 newNode->extraInfo.isVirtualLink = allocInfo->extendInfo.isVirtualLink;
421 newNode->laneReqId = laneReqId;
422 newNode->isWithQos = true;
423 newNode->isCanceled = false;
424 newNode->isNotified = false;
425 newNode->notifyFree = false;
426 newNode->hasNotifiedFree = false;
427 ListInit(&newNode->node);
428 return newNode;
429 }
430
TriggerLinkWithQos(uint32_t laneReqId,const LaneAllocInfo * allocInfo,LanePreferredLinkList * recommendLinkList)431 static int32_t TriggerLinkWithQos(uint32_t laneReqId, const LaneAllocInfo *allocInfo,
432 LanePreferredLinkList *recommendLinkList)
433 {
434 LaneLinkNodeInfo *linkNode = (LaneLinkNodeInfo *)SoftBusCalloc(sizeof(LaneLinkNodeInfo));
435 if (linkNode == NULL) {
436 return SOFTBUS_MALLOC_ERR;
437 }
438 if (memcpy_s(linkNode->networkId, NETWORK_ID_BUF_LEN, allocInfo->networkId, NETWORK_ID_BUF_LEN) != EOK) {
439 LNN_LOGE(LNN_LANE, "memcpy fail for networkId");
440 SoftBusFree(linkNode);
441 return SOFTBUS_MEM_ERR;
442 }
443 if (memcpy_s(linkNode->peerBleMac, MAX_MAC_LEN, allocInfo->extendInfo.peerBleMac, MAX_MAC_LEN) != EOK) {
444 LNN_LOGE(LNN_LANE, "memcpy fail for peerBleMac");
445 SoftBusFree(linkNode);
446 return SOFTBUS_MEM_ERR;
447 }
448 linkNode->transType = allocInfo->transType;
449 linkNode->laneReqId = laneReqId;
450 linkNode->linkRetryIdx = 0;
451 linkNode->listNum = recommendLinkList->linkTypeNum;
452 linkNode->linkList = recommendLinkList;
453 linkNode->pid = allocInfo->pid;
454 linkNode->networkDelegate = allocInfo->extendInfo.networkDelegate;
455 linkNode->acceptableProtocols = allocInfo->acceptableProtocols;
456 linkNode->actionAddr = allocInfo->extendInfo.actionAddr;
457 linkNode->isSupportIpv6 = allocInfo->extendInfo.isSupportIpv6;
458 linkNode->isVirtualLink = allocInfo->extendInfo.isVirtualLink;
459 linkNode->bandWidth = allocInfo->qosRequire.minBW;
460 linkNode->triggerLinkTime = SoftBusGetSysTimeMs();
461 linkNode->availableLinkTime = allocInfo->qosRequire.maxLaneLatency != 0 ?
462 allocInfo->qosRequire.maxLaneLatency : DEFAULT_LINK_LATENCY;
463 linkNode->isCompleted = false;
464 InitStatusList(linkNode);
465 ListInit(&linkNode->node);
466 if (Lock() != SOFTBUS_OK) {
467 LNN_LOGE(LNN_LANE, "get lock fail");
468 SoftBusFree(linkNode);
469 return SOFTBUS_LOCK_ERR;
470 }
471 ListTailInsert(&g_multiLinkList, &linkNode->node);
472 Unlock();
473 int32_t ret = LnnLanePostMsgToHandler(MSG_TYPE_LANE_TRIGGER_LINK, laneReqId, 0, NULL, 0);
474 if (ret != SOFTBUS_OK) {
475 DeleteLaneLinkNode(laneReqId);
476 return ret;
477 }
478 return SOFTBUS_OK;
479 }
480
StartTriggerLink(uint32_t laneReqId,const LaneAllocInfo * allocInfo,const LaneAllocListener * listener,LanePreferredLinkList * recommendLinkList)481 static int32_t StartTriggerLink(uint32_t laneReqId, const LaneAllocInfo *allocInfo, const LaneAllocListener *listener,
482 LanePreferredLinkList *recommendLinkList)
483 {
484 TransReqInfo *newItem = CreateReqNodeWithQos(laneReqId, allocInfo, listener);
485 if (newItem == NULL) {
486 return SOFTBUS_MEM_ERR;
487 }
488 if (Lock() != SOFTBUS_OK) {
489 LNN_LOGE(LNN_LANE, "get lock fail");
490 SoftBusFree(newItem);
491 return SOFTBUS_LOCK_ERR;
492 }
493 ListTailInsert(&g_requestList->list, &newItem->node);
494 g_requestList->cnt++;
495 Unlock();
496 int32_t ret = TriggerLinkWithQos(laneReqId, allocInfo, recommendLinkList);
497 if (ret != SOFTBUS_OK) {
498 DeleteRequestNode(laneReqId);
499 return ret;
500 }
501 return SOFTBUS_OK;
502 }
503
UpdateLaneEventWithCap(uint32_t laneHandle,const char * networkId)504 static void UpdateLaneEventWithCap(uint32_t laneHandle, const char *networkId)
505 {
506 uint32_t local = 0;
507 uint32_t remote = 0;
508 if (LnnGetLocalNumU32Info(NUM_KEY_NET_CAP, &local) == SOFTBUS_OK) {
509 LNN_LOGD(LNN_LANE, "local cap=%{public}u", local);
510 UpdateLaneEventInfo(laneHandle, EVENT_LOCAL_CAP,
511 LANE_PROCESS_TYPE_UINT32, (void *)(&local));
512 }
513 if (LnnGetRemoteNumU32Info(networkId, NUM_KEY_NET_CAP, &remote) == SOFTBUS_OK) {
514 LNN_LOGD(LNN_LANE, "remote cap=%{public}u", remote);
515 UpdateLaneEventInfo(laneHandle, EVENT_REMOTE_CAP,
516 LANE_PROCESS_TYPE_UINT32, (void *)(&remote));
517 }
518 }
519
UpdateLaneEventWithOnlineType(uint32_t laneHandle,const char * networkId)520 static void UpdateLaneEventWithOnlineType(uint32_t laneHandle, const char *networkId)
521 {
522 NodeInfo *nodeInfo = LnnGetNodeInfoById(networkId, CATEGORY_NETWORK_ID);
523 if (nodeInfo == NULL) {
524 LNN_LOGE(LNN_LANE, "not found nodeInfo");
525 return;
526 }
527 uint32_t onlineType = nodeInfo->discoveryType;
528 UpdateLaneEventInfo(laneHandle, EVENT_ONLINE_STATE,
529 LANE_PROCESS_TYPE_UINT32, (void *)(&onlineType));
530 }
531
AllocValidLane(uint32_t laneReqId,uint64_t allocLaneId,const LaneAllocInfo * allocInfo,const LaneAllocListener * listener)532 static int32_t AllocValidLane(uint32_t laneReqId, uint64_t allocLaneId, const LaneAllocInfo *allocInfo,
533 const LaneAllocListener *listener)
534 {
535 LaneSelectParam selectParam;
536 (void)memset_s(&selectParam, sizeof(LaneSelectParam), 0, sizeof(LaneSelectParam));
537 selectParam.transType = allocInfo->transType;
538 selectParam.qosRequire = allocInfo->qosRequire;
539 selectParam.allocedLaneId = allocLaneId;
540 LanePreferredLinkList *recommendLinkList = (LanePreferredLinkList *)SoftBusCalloc(sizeof(LanePreferredLinkList));
541 if (recommendLinkList == NULL) {
542 LNN_LOGE(LNN_LANE, "recommendLinkList malloc fail");
543 return SOFTBUS_MALLOC_ERR;
544 }
545 recommendLinkList->linkTypeNum = 0;
546 UpdateLaneEventWithCap(laneReqId, allocInfo->networkId);
547 UpdateLaneEventWithOnlineType(laneReqId, allocInfo->networkId);
548 int32_t ret = SelectExpectLanesByQos((const char *)allocInfo->networkId, &selectParam, recommendLinkList);
549 if (ret != SOFTBUS_OK) {
550 SoftBusFree(recommendLinkList);
551 LNN_LOGE(LNN_LANE, "selectExpectLanesByQos fail, laneReqId=%{public}u", laneReqId);
552 return ret;
553 }
554 if (recommendLinkList->linkTypeNum == 0) {
555 SoftBusFree(recommendLinkList);
556 LNN_LOGE(LNN_LANE, "no available link resources, laneReqId=%{public}u", laneReqId);
557 return SOFTBUS_LANE_NO_AVAILABLE_LINK;
558 }
559 for (uint32_t i = 0; i < recommendLinkList->linkTypeNum; i++) {
560 LNN_LOGI(LNN_LANE, "expect linklist nums=%{public}u, priority=%{public}u, link=%{public}u",
561 recommendLinkList->linkTypeNum, i, recommendLinkList->linkType[i]);
562 }
563 ret = StartTriggerLink(laneReqId, allocInfo, listener, recommendLinkList);
564 if (ret != SOFTBUS_OK) {
565 SoftBusFree(recommendLinkList);
566 LNN_LOGE(LNN_LANE, "trigger link fail, laneReqId=%{public}u", laneReqId);
567 return ret;
568 }
569 return SOFTBUS_OK;
570 }
571
DumpInputLinkList(const LanePreferredLinkList * linkInfo)572 static void DumpInputLinkList(const LanePreferredLinkList *linkInfo)
573 {
574 for (uint32_t i = 0; i < linkInfo->linkTypeNum; i++) {
575 LNN_LOGD(LNN_LANE, "priority=%{public}u, linkType=%{public}d", i, linkInfo->linkType[i]);
576 }
577 }
578
LaneAllocInfoConvert(const LaneAllocInfoExt * allocInfoExt,LaneAllocInfo * allocInfo)579 static int32_t LaneAllocInfoConvert(const LaneAllocInfoExt *allocInfoExt, LaneAllocInfo *allocInfo)
580 {
581 allocInfo->type = allocInfoExt->type;
582 allocInfo->transType = allocInfoExt->commInfo.transType;
583 allocInfo->extendInfo.isSupportIpv6 = allocInfoExt->commInfo.isSupportIpv6;
584 allocInfo->extendInfo.actionAddr = allocInfoExt->commInfo.actionAddr;
585 allocInfo->extendInfo.isVirtualLink = allocInfoExt->commInfo.isVirtualLink;
586 if (strcpy_s(allocInfo->networkId, NETWORK_ID_BUF_LEN, allocInfoExt->commInfo.networkId) != EOK) {
587 return SOFTBUS_STRCPY_ERR;
588 }
589 return SOFTBUS_OK;
590 }
591
BuildTargetLink(uint32_t laneHandle,const LaneAllocInfoExt * allocInfoExt,const LaneAllocListener * listener)592 static int32_t BuildTargetLink(uint32_t laneHandle, const LaneAllocInfoExt *allocInfoExt,
593 const LaneAllocListener *listener)
594 {
595 LanePreferredLinkList *linkList = (LanePreferredLinkList *)SoftBusCalloc(sizeof(LanePreferredLinkList));
596 if (linkList == NULL) {
597 return SOFTBUS_MALLOC_ERR;
598 }
599 if (memcpy_s(linkList, sizeof(LanePreferredLinkList), &allocInfoExt->linkList,
600 sizeof(LanePreferredLinkList)) != EOK) {
601 SoftBusFree(linkList);
602 return SOFTBUS_MEM_ERR;
603 }
604 LaneAllocInfo allocInfo;
605 (void)memset_s(&allocInfo, sizeof(allocInfo), 0, sizeof(allocInfo));
606 int32_t ret = LaneAllocInfoConvert(allocInfoExt, &allocInfo);
607 if (ret != SOFTBUS_OK) {
608 SoftBusFree(linkList);
609 return ret;
610 }
611 TransReqInfo *newItem = CreateReqNodeWithQos(laneHandle, &allocInfo, listener);
612 if (newItem == NULL) {
613 SoftBusFree(linkList);
614 return SOFTBUS_MEM_ERR;
615 }
616 if (Lock() != SOFTBUS_OK) {
617 LNN_LOGE(LNN_LANE, "get lock fail");
618 SoftBusFree(newItem);
619 SoftBusFree(linkList);
620 return SOFTBUS_LOCK_ERR;
621 }
622 ListTailInsert(&g_requestList->list, &newItem->node);
623 g_requestList->cnt++;
624 Unlock();
625 ret = TriggerLinkWithQos(laneHandle, &allocInfo, linkList);
626 if (ret != SOFTBUS_OK) {
627 SoftBusFree(linkList);
628 DeleteRequestNode(laneHandle);
629 return ret;
630 }
631 return SOFTBUS_OK;
632 }
633
AllocTargetLane(uint32_t laneHandle,const LaneAllocInfoExt * allocInfo,const LaneAllocListener * listener)634 static int32_t AllocTargetLane(uint32_t laneHandle, const LaneAllocInfoExt *allocInfo,
635 const LaneAllocListener *listener)
636 {
637 if (laneHandle == INVALID_LANE_REQ_ID || allocInfo == NULL ||
638 allocInfo->type != LANE_TYPE_TRANS || listener == NULL) {
639 LNN_LOGE(LNN_LANE, "alloc targetLane param invalid");
640 return SOFTBUS_INVALID_PARAM;
641 }
642 if (allocInfo->linkList.linkTypeNum >= LANE_LINK_TYPE_BUTT) {
643 return SOFTBUS_INVALID_PARAM;
644 }
645 DumpInputLinkList((const LanePreferredLinkList *)&allocInfo->linkList);
646 return BuildTargetLink(laneHandle, allocInfo, listener);
647 }
648
SpecifiedLinkCheck(const char * networkId,uint32_t linkNum,LaneLinkType * optionalLink,LanePreferredLinkList * preferLink)649 static int32_t SpecifiedLinkCheck(const char *networkId, uint32_t linkNum,
650 LaneLinkType *optionalLink, LanePreferredLinkList *preferLink)
651 {
652 if (linkNum <= 0 || linkNum >= LANE_LINK_TYPE_BUTT || optionalLink == NULL || preferLink == NULL) {
653 LNN_LOGE(LNN_LANE, "invalid link num");
654 return SOFTBUS_INVALID_PARAM;
655 }
656 uint32_t resNum = 0;
657 for (uint32_t i = 0; i < linkNum; i++) {
658 if (LaneCheckLinkValid(networkId, optionalLink[i], LANE_T_BUTT) != SOFTBUS_OK) {
659 LNN_LOGE(LNN_LANE, "SpecifiedLink capcheck fail, linkType=%{public}d", optionalLink[i]);
660 continue;
661 }
662 preferLink->linkType[resNum] = optionalLink[i];
663 resNum++;
664 }
665 if (resNum == 0) {
666 return GetErrCodeOfLink(networkId, optionalLink[0]);
667 }
668 preferLink->linkTypeNum = resNum;
669 return SOFTBUS_OK;
670 }
671
SpecifiedLinkConvert(const char * networkId,LaneSpecifiedLink link,LanePreferredLinkList * preferLink)672 static int32_t SpecifiedLinkConvert(const char *networkId, LaneSpecifiedLink link, LanePreferredLinkList *preferLink)
673 {
674 LaneLinkType optionalLink[LANE_LINK_TYPE_BUTT];
675 (void)memset_s(optionalLink, sizeof(optionalLink), 0, sizeof(optionalLink));
676 uint32_t linkNum = 0;
677 switch (link) {
678 case LANE_LINK_TYPE_WIFI_WLAN:
679 optionalLink[linkNum++] = LANE_WLAN_5G;
680 optionalLink[linkNum++] = LANE_WLAN_2P4G;
681 break;
682 case LANE_LINK_TYPE_WIFI_P2P:
683 optionalLink[linkNum++] = LANE_P2P;
684 break;
685 case LANE_LINK_TYPE_BR:
686 optionalLink[linkNum++] = LANE_BR;
687 break;
688 case LANE_LINK_TYPE_COC_DIRECT:
689 optionalLink[linkNum++] = LANE_COC_DIRECT;
690 break;
691 case LANE_LINK_TYPE_BLE_DIRECT:
692 optionalLink[linkNum++] = LANE_BLE_DIRECT;
693 break;
694 case LANE_LINK_TYPE_HML:
695 optionalLink[linkNum++] = LANE_HML;
696 break;
697 case LANE_LINK_TYPE_USB:
698 optionalLink[linkNum++] = LANE_USB;
699 break;
700 case LANE_LINK_TYPE_SLE_DIRECT:
701 optionalLink[linkNum++] = LANE_SLE_DIRECT;
702 break;
703 default:
704 LNN_LOGE(LNN_LANE, "unexpected link=%{public}d", link);
705 break;
706 }
707 if (linkNum == 0) {
708 return SOFTBUS_LANE_NO_AVAILABLE_LINK;
709 }
710 int32_t ret = SpecifiedLinkCheck(networkId, linkNum, optionalLink, preferLink);
711 if (ret != SOFTBUS_OK) {
712 LNN_LOGE(LNN_LANE, "specofied link check failed");
713 }
714 return ret;
715 }
716
ProcessSpecifiedLink(uint32_t laneHandle,const LaneAllocInfo * allocInfo,const LaneAllocListener * listener)717 static int32_t ProcessSpecifiedLink(uint32_t laneHandle, const LaneAllocInfo *allocInfo,
718 const LaneAllocListener *listener)
719 {
720 LaneAllocInfoExt info;
721 (void)memset_s(&info, sizeof(info), 0, sizeof(info));
722 if (SpecifiedLinkConvert((const char *)allocInfo->networkId, allocInfo->extendInfo.linkType, &info.linkList)
723 != SOFTBUS_OK) {
724 return SOFTBUS_LANE_SELECT_FAIL;
725 }
726 info.type = allocInfo->type;
727 info.commInfo.transType = allocInfo->transType;
728 info.commInfo.isSupportIpv6 = allocInfo->extendInfo.isSupportIpv6;
729 info.commInfo.actionAddr = allocInfo->extendInfo.actionAddr;
730 if (strcpy_s(info.commInfo.networkId, NETWORK_ID_BUF_LEN, allocInfo->networkId) != EOK) {
731 return SOFTBUS_STRCPY_ERR;
732 }
733 return BuildTargetLink(laneHandle, &info, listener);
734 }
735
InitLaneProcess(uint32_t laneReqId,const LaneAllocInfo * allocInfo)736 static void InitLaneProcess(uint32_t laneReqId, const LaneAllocInfo *allocInfo)
737 {
738 LaneProcess processInfo;
739 (void)memset_s(&processInfo, sizeof(LaneProcess), 0, sizeof(LaneProcess));
740 processInfo.laneProcessList32Bit[EVENT_LANE_HANDLE] = laneReqId;
741 processInfo.laneProcessList32Bit[EVENT_LANE_LINK_TYPE] = LANE_LINK_TYPE_BUTT;
742 processInfo.laneProcessList32Bit[EVENT_LANE_MIN_BW] = allocInfo->qosRequire.minBW;
743 processInfo.laneProcessList32Bit[EVENT_LANE_MAX_LANE_LATENCY] = allocInfo->qosRequire.maxLaneLatency;
744 processInfo.laneProcessList32Bit[EVENT_LANE_MIN_LANE_LATENCY] = allocInfo->qosRequire.minLaneLatency;
745 processInfo.laneProcessList32Bit[EVENT_LANE_RTT_LEVEL] = allocInfo->qosRequire.rttLevel;
746 processInfo.laneProcessList32Bit[EVENT_TRANS_TYPE] = allocInfo->transType;
747 processInfo.laneProcessList32Bit[EVENT_GUIDE_TYPE] = LANE_CHANNEL_BUTT;
748 processInfo.laneProcessList32Bit[EVENT_WIFI_DETECT_STATE] = WIFI_DETECT_BUTT;
749 processInfo.laneProcessList64Bit[EVENT_LANE_ID] = INVALID_LANE_ID;
750 if (memcpy_s(processInfo.peerNetWorkId, NETWORK_ID_BUF_LEN, allocInfo->networkId, NETWORK_ID_BUF_LEN) != EOK) {
751 LNN_LOGE(LNN_LANE, "peerNetWorkId memcpy fail");
752 }
753 CreateLaneEventInfo(&processInfo);
754 }
755
ReportLaneEvent(uint32_t laneHandle,LnnEventLaneStage stage,int32_t retCode)756 static void ReportLaneEvent(uint32_t laneHandle, LnnEventLaneStage stage, int32_t retCode)
757 {
758 UpdateLaneEventInfo(laneHandle, EVENT_LANE_STAGE,
759 LANE_PROCESS_TYPE_UINT32, (void *)(&stage));
760 ReportLaneEventInfo(laneHandle, retCode);
761 }
762
AllocLaneByQos(uint32_t laneReqId,const LaneAllocInfo * allocInfo,const LaneAllocListener * listener)763 static int32_t AllocLaneByQos(uint32_t laneReqId, const LaneAllocInfo *allocInfo, const LaneAllocListener *listener)
764 {
765 if (laneReqId == INVALID_LANE_REQ_ID || allocInfo == NULL ||
766 allocInfo->type != LANE_TYPE_TRANS || listener == NULL) {
767 LNN_LOGE(LNN_LANE, "allocLane param invalid");
768 return SOFTBUS_INVALID_PARAM;
769 }
770 if (allocInfo->extendInfo.isSpecifiedLink) {
771 LNN_LOGW(LNN_LANE, "process specifiedLink, linkType=%{public}d", allocInfo->extendInfo.linkType);
772 return ProcessSpecifiedLink(laneReqId, allocInfo, listener);
773 }
774 InitLaneProcess(laneReqId, allocInfo);
775 int32_t ret = AllocValidLane(laneReqId, INVALID_LANE_ID, allocInfo, listener);
776 if (ret != SOFTBUS_OK) {
777 LNN_LOGE(LNN_LANE, "alloc valid lane fail, laneReqId=%{public}u", laneReqId);
778 ReportLaneEvent(laneReqId, EVENT_STAGE_LANE_DECIDE_FAIL, ret);
779 FreeLaneReqId(laneReqId);
780 return ret;
781 }
782 return SOFTBUS_OK;
783 }
784
ReallocLaneByQos(uint32_t laneReqId,uint64_t laneId,const LaneAllocInfo * allocInfo,const LaneAllocListener * listener)785 static int32_t ReallocLaneByQos(uint32_t laneReqId, uint64_t laneId, const LaneAllocInfo *allocInfo,
786 const LaneAllocListener *listener)
787 {
788 if (laneReqId == INVALID_LANE_REQ_ID || allocInfo == NULL || allocInfo->type != LANE_TYPE_TRANS ||
789 listener == NULL || laneId == INVALID_LANE_ID) {
790 LNN_LOGE(LNN_LANE, "reallocLane param invalid");
791 return SOFTBUS_INVALID_PARAM;
792 }
793 int32_t ret = AllocValidLane(laneReqId, laneId, allocInfo, listener);
794 if (ret != SOFTBUS_OK) {
795 LNN_LOGE(LNN_LANE, "alloc valid lane fail, laneReqId=%{public}u", laneReqId);
796 FreeLaneReqId(laneReqId);
797 return ret;
798 }
799 return SOFTBUS_OK;
800 }
801
AllocRawLane(uint32_t laneHandle,const RawLaneAllocInfo * allocInfo,const LaneAllocListener * listener)802 static int32_t AllocRawLane(uint32_t laneHandle, const RawLaneAllocInfo *allocInfo, const LaneAllocListener *listener)
803 {
804 if ((allocInfo == NULL) || (allocInfo->type != LANE_TYPE_TRANS) || (listener == NULL)) {
805 LNN_LOGE(LNN_LANE, "allocLane param invalid");
806 return SOFTBUS_INVALID_PARAM;
807 }
808 LNN_LOGI(LNN_LANE, "get raw lane info, actionAddr=%{public}u", allocInfo->actionAddr);
809 LaneSelectParam selectParam;
810 (void)memset_s(&selectParam, sizeof(LaneSelectParam), 0, sizeof(LaneSelectParam));
811 selectParam.transType = allocInfo->transType;
812 selectParam.qosRequire = allocInfo->qosRequire;
813 selectParam.allocedLaneId = INVALID_LANE_ID;
814 LanePreferredLinkList *recommendLinkList = (LanePreferredLinkList *)SoftBusCalloc(sizeof(LanePreferredLinkList));
815 if (recommendLinkList == NULL) {
816 LNN_LOGE(LNN_LANE, "recommendLinkList malloc fail");
817 return SOFTBUS_MALLOC_ERR;
818 }
819 recommendLinkList->linkTypeNum = 1;
820 recommendLinkList->linkType[0] = LANE_HML_RAW;
821 LaneAllocInfo laneAllocInfo;
822 (void)memset_s(&laneAllocInfo, sizeof(LaneAllocInfo), 0, sizeof(LaneAllocInfo));
823 laneAllocInfo.type = allocInfo->type;
824 laneAllocInfo.transType = allocInfo->transType;
825 laneAllocInfo.qosRequire = allocInfo->qosRequire;
826 laneAllocInfo.extendInfo.actionAddr = allocInfo->actionAddr;
827 laneAllocInfo.extendInfo.isSupportIpv6 = true;
828 int32_t ret = StartTriggerLink(laneHandle, &laneAllocInfo, listener, recommendLinkList);
829 if (ret != SOFTBUS_OK) {
830 SoftBusFree(recommendLinkList);
831 LNN_LOGE(LNN_LANE, "trigger link fail, laneHandle=%{public}u", laneHandle);
832 return ret;
833 }
834 return SOFTBUS_OK;
835 }
836
Alloc(uint32_t laneReqId,const LaneRequestOption * request,const ILaneListener * listener)837 static int32_t Alloc(uint32_t laneReqId, const LaneRequestOption *request, const ILaneListener *listener)
838 {
839 if ((request == NULL) || (request->type != LANE_TYPE_TRANS)) {
840 return SOFTBUS_INVALID_PARAM;
841 }
842 TransOption *transRequest = (TransOption *)&request->requestInfo.trans;
843 LaneSelectParam selectParam;
844 (void)memset_s(&selectParam, sizeof(LaneSelectParam), 0, sizeof(LaneSelectParam));
845 selectParam.transType = transRequest->transType;
846 selectParam.expectedBw = transRequest->expectedBw;
847 if (memcpy_s(&selectParam.list, sizeof(selectParam.list),
848 &transRequest->expectedLink, sizeof(transRequest->expectedLink)) != EOK) {
849 return SOFTBUS_MEM_ERR;
850 }
851 LanePreferredLinkList *recommendLinkList = (LanePreferredLinkList *)SoftBusCalloc(sizeof(LanePreferredLinkList));
852 if (recommendLinkList == NULL) {
853 return SOFTBUS_MALLOC_ERR;
854 }
855 uint32_t listNum = 0;
856 int32_t ret = SelectLane((const char *)transRequest->networkId, &selectParam, recommendLinkList, &listNum);
857 if (ret != SOFTBUS_OK) {
858 SoftBusFree(recommendLinkList);
859 return ret;
860 }
861 if (recommendLinkList->linkTypeNum == 0) {
862 LNN_LOGE(LNN_LANE, "no available link to request, laneReqId=%{public}u", laneReqId);
863 SoftBusFree(recommendLinkList);
864 return SOFTBUS_LANE_NO_AVAILABLE_LINK;
865 }
866 LNN_LOGI(LNN_LANE, "select lane link success, linkNum=%{public}d, laneReqId=%{public}u", listNum, laneReqId);
867 TransReqInfo *newItem = CreateRequestNode(laneReqId, transRequest, listener);
868 if (newItem == NULL) {
869 SoftBusFree(recommendLinkList);
870 return SOFTBUS_MEM_ERR;
871 }
872 if (Lock() != SOFTBUS_OK) {
873 SoftBusFree(newItem);
874 SoftBusFree(recommendLinkList);
875 return SOFTBUS_LOCK_ERR;
876 }
877 ListTailInsert(&g_requestList->list, &newItem->node);
878 g_requestList->cnt++;
879 Unlock();
880 ret = TriggerLink(laneReqId, transRequest, recommendLinkList);
881 if (ret != SOFTBUS_OK) {
882 SoftBusFree(recommendLinkList);
883 DeleteRequestNode(laneReqId);
884 return ret;
885 }
886 return SOFTBUS_OK;
887 }
888
ParseLaneTypeByLaneReqId(uint32_t laneReqId,LaneType * laneType)889 static int32_t ParseLaneTypeByLaneReqId(uint32_t laneReqId, LaneType *laneType)
890 {
891 if (laneReqId == INVALID_LANE_REQ_ID || laneType == NULL) {
892 LNN_LOGE(LNN_LANE, "[ParseLaneType]invalid param");
893 return SOFTBUS_INVALID_PARAM;
894 }
895 *laneType = (LaneType)(laneReqId >> LANE_REQ_ID_TYPE_SHIFT);
896 return SOFTBUS_OK;
897 }
898
PostNotifyFreeLaneResult(uint32_t laneReqId,int32_t errCode,uint64_t delayMillis)899 int32_t PostNotifyFreeLaneResult(uint32_t laneReqId, int32_t errCode, uint64_t delayMillis)
900 {
901 LNN_LOGI(LNN_LANE, "post notify free lane result message, laneReqId=%{public}u, errCode=%{public}d",
902 laneReqId, errCode);
903 return LnnLanePostMsgToHandler(MSG_TYPE_NOTIFY_FREE_LANE_RESULT, laneReqId, errCode, NULL, delayMillis);
904 }
905
GetCostTime(uint64_t triggerLinkTime)906 static uint64_t GetCostTime(uint64_t triggerLinkTime)
907 {
908 uint64_t currentSysTime = SoftBusGetSysTimeMs();
909 if (currentSysTime < triggerLinkTime) {
910 LNN_LOGE(LNN_LANE, "get cost time fail");
911 return 0;
912 }
913 return currentSysTime - triggerLinkTime;
914 }
915
CancelLane(uint32_t laneReqId)916 static int32_t CancelLane(uint32_t laneReqId)
917 {
918 if (Lock() != SOFTBUS_OK) {
919 LNN_LOGE(LNN_LANE, "get lock fail");
920 return SOFTBUS_LOCK_ERR;
921 }
922 TransReqInfo *item = NULL;
923 TransReqInfo *next = NULL;
924 LIST_FOR_EACH_ENTRY_SAFE(item, next, &g_requestList->list, TransReqInfo, node) {
925 if (item->isWithQos && item->laneReqId == laneReqId) {
926 if (item->isNotified) {
927 Unlock();
928 LNN_LOGE(LNN_LANE, "cancel lane fail, lane result has notified, laneReqId=%{public}u", laneReqId);
929 return SOFTBUS_INVALID_PARAM;
930 }
931 item->isCanceled = true;
932 Unlock();
933 LnnCancelWifiDirect(laneReqId);
934 LNN_LOGI(LNN_LANE, "cancel lane succ, laneReqId=%{public}u", laneReqId);
935 return SOFTBUS_OK;
936 }
937 }
938 Unlock();
939 LNN_LOGE(LNN_LANE, "cancel lane fail, lane reqinfo not find, laneReqId=%{public}u", laneReqId);
940 return SOFTBUS_LANE_NOT_FOUND;
941 }
942
UpdateReqListLaneId(uint64_t oldLaneId,uint64_t newLaneId)943 int32_t UpdateReqListLaneId(uint64_t oldLaneId, uint64_t newLaneId)
944 {
945 if (Lock() != SOFTBUS_OK) {
946 LNN_LOGE(LNN_LANE, "get lock fail");
947 return SOFTBUS_LOCK_ERR;
948 }
949 TransReqInfo *item = NULL;
950 LIST_FOR_EACH_ENTRY(item, &g_requestList->list, TransReqInfo, node) {
951 if (item->laneId == oldLaneId) {
952 item->laneId = newLaneId;
953 LNN_LOGI(LNN_LANE, "update newLaneId=%{public}" PRIu64 "oldLaneId=%{public}" PRIu64,
954 newLaneId, oldLaneId);
955 Unlock();
956 return SOFTBUS_OK;
957 }
958 }
959 Unlock();
960 return SOFTBUS_NOT_FIND;
961 }
962
UpdateNotifyInfoBylaneReqId(uint32_t laneReqId,bool isNotifyFree)963 int32_t UpdateNotifyInfoBylaneReqId(uint32_t laneReqId, bool isNotifyFree)
964 {
965 if (Lock() != SOFTBUS_OK) {
966 LNN_LOGE(LNN_LANE, "get lock fail");
967 return SOFTBUS_LOCK_ERR;
968 }
969 TransReqInfo *item = NULL;
970 TransReqInfo *next = NULL;
971 LIST_FOR_EACH_ENTRY_SAFE(item, next, &g_requestList->list, TransReqInfo, node) {
972 if (item->laneReqId == laneReqId) {
973 item->notifyFree = isNotifyFree;
974 Unlock();
975 return SOFTBUS_OK;
976 }
977 }
978 Unlock();
979 LNN_LOGE(LNN_LANE, "not found lane need free, laneReqId=%{public}u", laneReqId);
980 return SOFTBUS_LANE_NOT_FOUND;
981 }
982
UpdateReqInfoWithLaneReqId(uint32_t laneReqId,uint64_t laneId)983 static void UpdateReqInfoWithLaneReqId(uint32_t laneReqId, uint64_t laneId)
984 {
985 if (Lock() != SOFTBUS_OK) {
986 LNN_LOGE(LNN_LANE, "get lock fail");
987 return;
988 }
989 TransReqInfo *item = NULL;
990 LIST_FOR_EACH_ENTRY(item, &g_requestList->list, TransReqInfo, node) {
991 if (item->laneReqId == laneReqId) {
992 item->laneId = laneId;
993 if (item->isWithQos && !item->isCanceled) {
994 item->isNotified = true;
995 }
996 Unlock();
997 return;
998 }
999 }
1000 Unlock();
1001 }
1002
DfxReportLinkResult(uint32_t laneReqId,LaneLinkType linkType,int32_t reason)1003 static void DfxReportLinkResult(uint32_t laneReqId, LaneLinkType linkType, int32_t reason)
1004 {
1005 LnnEventExtra extra = { 0 };
1006 extra.errcode = reason;
1007 extra.laneReqId = laneReqId;
1008 extra.laneLinkType = (int32_t)linkType;
1009 LNN_EVENT(EVENT_SCENE_LNN, EVENT_STAGE_LNN_LANE_SELECT_END, extra);
1010 }
1011
GetLaneLinkNodeWithoutLock(uint32_t laneReqId)1012 static LaneLinkNodeInfo *GetLaneLinkNodeWithoutLock(uint32_t laneReqId)
1013 {
1014 LaneLinkNodeInfo *linkNode = NULL;
1015 LIST_FOR_EACH_ENTRY(linkNode, &g_multiLinkList, LaneLinkNodeInfo, node) {
1016 if (linkNode->laneReqId == laneReqId) {
1017 return linkNode;
1018 }
1019 }
1020 return NULL;
1021 }
1022
ReportLaneEventWithBuildLinkInfo(uint32_t laneHandle,uint64_t laneId,LaneLinkType linkType,int32_t reason)1023 static void ReportLaneEventWithBuildLinkInfo(uint32_t laneHandle, uint64_t laneId,
1024 LaneLinkType linkType, int32_t reason)
1025 {
1026 uint64_t buildLinkTime;
1027 if (Lock() != SOFTBUS_OK) {
1028 LNN_LOGE(LNN_LANE, "get lock fail");
1029 return;
1030 }
1031 LaneLinkNodeInfo *nodeInfo = GetLaneLinkNodeWithoutLock(laneHandle);
1032 if (nodeInfo == NULL || nodeInfo->linkList == NULL) {
1033 LNN_LOGE(LNN_LANE, "get lane link node info fail, laneReqId=%{public}u", laneHandle);
1034 Unlock();
1035 return;
1036 }
1037 buildLinkTime = GetCostTime(nodeInfo->triggerLinkTime);
1038 Unlock();
1039 UpdateLaneEventInfo(laneHandle, EVENT_BUILD_LINK_TIME,
1040 LANE_PROCESS_TYPE_UINT64, (void *)(&buildLinkTime));
1041 if (laneId != INVALID_LANE_ID) {
1042 UpdateLaneEventInfo(laneHandle, EVENT_LANE_ID,
1043 LANE_PROCESS_TYPE_UINT64, (void *)(&laneId));
1044 }
1045 if (linkType != LANE_LINK_TYPE_BUTT) {
1046 UpdateLaneEventInfo(laneHandle, EVENT_LANE_LINK_TYPE,
1047 LANE_PROCESS_TYPE_UINT32, (void *)(&linkType));
1048 }
1049 if (reason == SOFTBUS_OK) {
1050 ReportLaneEvent(laneHandle, EVENT_STAGE_LANE_BUILD_SUCC, SOFTBUS_OK);
1051 } else {
1052 ReportLaneEvent(laneHandle, EVENT_STAGE_LANE_BUILD_FAIL, reason);
1053 }
1054 }
1055
NotifyLaneAllocSuccess(uint32_t laneReqId,uint64_t laneId,const LaneLinkInfo * info)1056 static void NotifyLaneAllocSuccess(uint32_t laneReqId, uint64_t laneId, const LaneLinkInfo *info)
1057 {
1058 UpdateReqInfoWithLaneReqId(laneReqId, laneId);
1059 TransReqInfo reqInfo;
1060 (void)memset_s(&reqInfo, sizeof(TransReqInfo), 0, sizeof(TransReqInfo));
1061 if (GetTransReqInfoByLaneReqId(laneReqId, &reqInfo) != SOFTBUS_OK) {
1062 LNN_LOGE(LNN_LANE, "get lane reqInfo fail");
1063 return;
1064 }
1065 LaneProfile profile;
1066 LaneConnInfo connInfo;
1067 (void)memset_s(&profile, sizeof(LaneProfile), 0, sizeof(LaneProfile));
1068 if (LaneInfoProcess(info, &connInfo, &profile) != SOFTBUS_OK) {
1069 LNN_LOGE(LNN_LANE, "lane alloc success, but laneInfo proc fail");
1070 return;
1071 }
1072 connInfo.isLowLatency = IsSupportLowLatencyPacked(&reqInfo, info);
1073 LNN_LOGI(LNN_LANE, "Notify laneAlloc succ, laneReqId=%{public}u, linkType=%{public}d, isLowLatency=%{public}d, "
1074 "laneId=%{public}" PRIu64 "", laneReqId, info->type, connInfo.isLowLatency, laneId);
1075 if (reqInfo.isWithQos) {
1076 if (reqInfo.isCanceled) {
1077 LNN_LOGE(LNN_LANE, "lane has canceled only notify fail, laneReqId=%{public}u", laneReqId);
1078 reqInfo.listener.onLaneAllocFail(laneReqId, SOFTBUS_LANE_SUCC_AFTER_CANCELED);
1079 (void)FreeLane(laneReqId);
1080 return;
1081 }
1082 connInfo.laneId = laneId;
1083 reqInfo.listener.onLaneAllocSuccess(laneReqId, &connInfo);
1084 } else {
1085 connInfo.laneId = INVALID_LANE_ID;
1086 reqInfo.extraInfo.listener.onLaneRequestSuccess(laneReqId, &connInfo);
1087 }
1088 ReportLaneEventWithBuildLinkInfo(laneReqId, laneId, info->type, SOFTBUS_OK);
1089 LaneType laneType;
1090 if (ParseLaneTypeByLaneReqId(laneReqId, &laneType) != SOFTBUS_OK ||
1091 AddLaneBusinessInfoItem(laneType, laneId) != SOFTBUS_OK) {
1092 LNN_LOGE(LNN_LANE, "create laneBusinessInfo fail, laneReqId=%{public}u", laneReqId);
1093 }
1094 DfxReportLinkResult(laneReqId, info->type, SOFTBUS_OK);
1095 }
1096
NotifyLaneAllocFail(uint32_t laneReqId,int32_t reason)1097 static void NotifyLaneAllocFail(uint32_t laneReqId, int32_t reason)
1098 {
1099 UpdateReqInfoWithLaneReqId(laneReqId, INVALID_LANE_ID);
1100 TransReqInfo reqInfo;
1101 (void)memset_s(&reqInfo, sizeof(TransReqInfo), 0, sizeof(TransReqInfo));
1102 if (GetTransReqInfoByLaneReqId(laneReqId, &reqInfo) != SOFTBUS_OK) {
1103 LNN_LOGE(LNN_LANE, "get lane reqInfo fail");
1104 return;
1105 }
1106 if (reqInfo.isWithQos && reqInfo.isCanceled) {
1107 LNN_LOGE(LNN_LANE, "lane has canceled only notify fail, laneReqId=%{public}u", laneReqId);
1108 }
1109 LNN_LOGE(LNN_LANE, "Notify laneAlloc fail, laneReqId=%{public}u, reason=%{public}d", laneReqId, reason);
1110 if (reqInfo.isWithQos) {
1111 reqInfo.listener.onLaneAllocFail(laneReqId, reason);
1112 FreeLaneReqId(laneReqId);
1113 } else {
1114 reqInfo.extraInfo.listener.onLaneRequestFail(laneReqId, reason);
1115 }
1116 LaneLinkType laneType;
1117 if (Lock() != SOFTBUS_OK) {
1118 LNN_LOGE(LNN_LANE, "get lock fail");
1119 return;
1120 }
1121 LaneLinkNodeInfo *nodeInfo = GetLaneLinkNodeWithoutLock(laneReqId);
1122 if (nodeInfo == NULL || nodeInfo->linkList == NULL) {
1123 LNN_LOGE(LNN_LANE, "get lane link node info fail, laneReqId=%{public}u", laneReqId);
1124 Unlock();
1125 return;
1126 }
1127 laneType = nodeInfo->linkList->linkType[0];
1128 Unlock();
1129 ReportLaneEventWithBuildLinkInfo(laneReqId, INVALID_LANE_ID, LANE_LINK_TYPE_BUTT, reason);
1130 DfxReportLinkResult(laneReqId, laneType, reason);
1131 DeleteRequestNode(laneReqId);
1132 }
1133
GetErrCodeWithLock(uint32_t laneReqId)1134 static int32_t GetErrCodeWithLock(uint32_t laneReqId)
1135 {
1136 if (Lock() != SOFTBUS_OK) {
1137 LNN_LOGE(LNN_LANE, "get lock fail");
1138 return SOFTBUS_LOCK_ERR;
1139 }
1140 LaneLinkNodeInfo *nodeInfo = GetLaneLinkNodeWithoutLock(laneReqId);
1141 if (nodeInfo == NULL || nodeInfo->linkList == NULL) {
1142 LNN_LOGE(LNN_LANE, "get lane link node info fail, laneReqId=%{public}u", laneReqId);
1143 Unlock();
1144 return SOFTBUS_LANE_NOT_FOUND;
1145 }
1146 LaneLinkType linkType;
1147 int32_t result = SOFTBUS_LANE_BUILD_LINK_FAIL;
1148 for (uint32_t i = 0; i < nodeInfo->linkList->linkTypeNum; i++) {
1149 linkType = nodeInfo->linkList->linkType[i];
1150 if (linkType == LANE_HML || linkType == LANE_P2P) {
1151 result = nodeInfo->statusList[linkType].result;
1152 Unlock();
1153 return result;
1154 }
1155 }
1156 linkType = nodeInfo->linkList->linkType[0];
1157 result = nodeInfo->statusList[linkType].result;
1158 Unlock();
1159 return result;
1160 }
1161
CreateLinkRequestNode(const LaneLinkNodeInfo * nodeInfo,LinkRequest * requestInfo)1162 static int32_t CreateLinkRequestNode(const LaneLinkNodeInfo *nodeInfo, LinkRequest *requestInfo)
1163 {
1164 requestInfo->networkDelegate = nodeInfo->networkDelegate;
1165 requestInfo->p2pOnly = nodeInfo->p2pOnly;
1166 requestInfo->linkType = nodeInfo->linkList->linkType[nodeInfo->linkRetryIdx];
1167 requestInfo->pid = nodeInfo->pid;
1168 requestInfo->acceptableProtocols = nodeInfo->acceptableProtocols;
1169 requestInfo->transType = nodeInfo->transType;
1170 requestInfo->psm = nodeInfo->psm;
1171 requestInfo->actionAddr = nodeInfo->actionAddr;
1172 requestInfo->isSupportIpv6 = nodeInfo->isSupportIpv6;
1173 requestInfo->isVirtualLink = nodeInfo->isVirtualLink;
1174 requestInfo->isInnerCalled = nodeInfo->isInnerCalled;
1175 if (memcpy_s(requestInfo->peerNetworkId, NETWORK_ID_BUF_LEN, nodeInfo->networkId, NETWORK_ID_BUF_LEN) != EOK) {
1176 LNN_LOGE(LNN_LANE, "memcpy networkId fail");
1177 return SOFTBUS_MEM_ERR;
1178 }
1179 if (memcpy_s(requestInfo->peerBleMac, MAX_MAC_LEN, nodeInfo->peerBleMac, MAX_MAC_LEN) != EOK) {
1180 LNN_LOGE(LNN_LANE, "memcpy peerBleMac fail");
1181 return SOFTBUS_MEM_ERR;
1182 }
1183 requestInfo->bandWidth = nodeInfo->bandWidth;
1184 requestInfo->triggerLinkTime = nodeInfo->triggerLinkTime;
1185 requestInfo->availableLinkTime = nodeInfo->availableLinkTime;
1186 return SOFTBUS_OK;
1187 }
1188
1189 static int32_t g_laneLatency[LANE_LINK_TYPE_BUTT] = {
1190 [LANE_BR] = BR_LATENCY,
1191 [LANE_BLE] = COC_DIRECT_LATENCY,
1192 [LANE_P2P] = P2P_LATENCY,
1193 [LANE_WLAN_2P4G] = WLAN_LATENCY,
1194 [LANE_WLAN_5G] = WLAN_LATENCY,
1195 [LANE_ETH] = WLAN_LATENCY,
1196 [LANE_P2P_REUSE] = P2P_LATENCY,
1197 [LANE_BLE_DIRECT] = COC_DIRECT_LATENCY,
1198 [LANE_BLE_REUSE] = COC_DIRECT_LATENCY,
1199 [LANE_COC] = COC_DIRECT_LATENCY,
1200 [LANE_COC_DIRECT] = COC_DIRECT_LATENCY,
1201 [LANE_HML] = HML_LATENCY,
1202 [LANE_USB] = USB_LATENCY,
1203 [LANE_SLE] = SLE_LATENCY,
1204 [LANE_SLE_DIRECT] = SLE_DIRECT_LATENCY,
1205 };
1206
LaneTriggerLink(SoftBusMessage * msg)1207 static void LaneTriggerLink(SoftBusMessage *msg)
1208 {
1209 uint32_t laneReqId = (uint32_t)msg->arg1;
1210 LaneLinkCb linkCb = {
1211 .onLaneLinkSuccess = LinkSuccess,
1212 .onLaneLinkFail = LinkFail,
1213 };
1214 LinkRequest requestInfo = {0};
1215 if (Lock() != SOFTBUS_OK) {
1216 LNN_LOGE(LNN_LANE, "get lock fail");
1217 return;
1218 }
1219 LaneLinkNodeInfo *nodeInfo = GetLaneLinkNodeWithoutLock(laneReqId);
1220 if (nodeInfo == NULL) {
1221 LNN_LOGE(LNN_LANE, "get lane link node info fail");
1222 Unlock();
1223 return;
1224 }
1225 int32_t ret = SOFTBUS_LANE_BUILD_LINK_FAIL;
1226 do {
1227 ret = CreateLinkRequestNode(nodeInfo, &requestInfo);
1228 if (ret != SOFTBUS_OK) {
1229 LNN_LOGE(LNN_LANE, "Create LinkRequestNode fail.");
1230 Unlock();
1231 break;
1232 }
1233 nodeInfo->linkRetryIdx++;
1234 nodeInfo->statusList[requestInfo.linkType].status = BUILD_LINK_STATUS_BUILDING;
1235 Unlock();
1236 uint64_t delayMillis = (uint64_t)g_laneLatency[requestInfo.linkType];
1237 (void)PostLinkTimeoutMessage(laneReqId, requestInfo.linkType, delayMillis);
1238 ret = BuildLink(&requestInfo, laneReqId, &linkCb);
1239 if (ret == SOFTBUS_OK) {
1240 return;
1241 }
1242 } while (false);
1243 linkCb.onLaneLinkFail(laneReqId, ret, requestInfo.linkType);
1244 }
1245
UpdateLinkStatus(uint32_t laneReqId,BuildLinkStatus status,LaneLinkType linkType,const LaneLinkInfo * linkInfo,int32_t result)1246 static int32_t UpdateLinkStatus(uint32_t laneReqId, BuildLinkStatus status, LaneLinkType linkType,
1247 const LaneLinkInfo *linkInfo, int32_t result)
1248 {
1249 if (Lock() != SOFTBUS_OK) {
1250 LNN_LOGE(LNN_LANE, "get lock fail");
1251 return SOFTBUS_LOCK_ERR;
1252 }
1253 LaneLinkNodeInfo *nodeInfo = GetLaneLinkNodeWithoutLock(laneReqId);
1254 if (nodeInfo == NULL) {
1255 Unlock();
1256 LNN_LOGI(LNN_LANE, "link result has notified, not need update link status. laneReqId=%{public}u", laneReqId);
1257 if (status == BUILD_LINK_STATUS_SUCC) {
1258 FreeUnusedLink(laneReqId, linkInfo);
1259 }
1260 return SOFTBUS_LANE_NOT_FOUND;
1261 }
1262 if (nodeInfo->isCompleted) {
1263 Unlock();
1264 LNN_LOGI(LNN_LANE, "build link has completed, not need update link status. laneReqId=%{public}u, "
1265 "linkType=%{public}d", laneReqId, linkType);
1266 if (status == BUILD_LINK_STATUS_SUCC) {
1267 FreeUnusedLink(laneReqId, linkInfo);
1268 }
1269 return SOFTBUS_LANE_TRIGGER_LINK_FAIL;
1270 }
1271 LNN_LOGI(LNN_LANE, "update link status, laneReqId=%{public}u, status=%{public}d, linkType=%{public}d",
1272 laneReqId, status, linkType);
1273 nodeInfo->statusList[linkType].status = status;
1274 nodeInfo->statusList[linkType].result = result;
1275 if (status != BUILD_LINK_STATUS_SUCC) {
1276 Unlock();
1277 return SOFTBUS_OK;
1278 }
1279 if (memcpy_s(&(nodeInfo->statusList[linkType].linkInfo), sizeof(LaneLinkInfo), linkInfo,
1280 sizeof(LaneLinkInfo)) != EOK) {
1281 LNN_LOGE(LNN_LANE, "linkParam memcpy fail, laneReqId=%{public}u", laneReqId);
1282 Unlock();
1283 return SOFTBUS_MEM_ERR;
1284 }
1285 Unlock();
1286 return SOFTBUS_OK;
1287 }
1288
IsNeedNotifySucc(uint32_t laneReqId)1289 static bool IsNeedNotifySucc(uint32_t laneReqId)
1290 {
1291 if (Lock() != SOFTBUS_OK) {
1292 LNN_LOGE(LNN_LANE, "get lock fail");
1293 return false;
1294 }
1295 LaneLinkNodeInfo *nodeInfo = GetLaneLinkNodeWithoutLock(laneReqId);
1296 if (nodeInfo == NULL) {
1297 Unlock();
1298 LNN_LOGE(LNN_LANE, "get lane link node info fail, laneReqId=%{public}u", laneReqId);
1299 return false;
1300 }
1301 bool isBuilding = false;
1302 for (uint32_t i = 0; i < nodeInfo->listNum; i++) {
1303 LaneLinkType linkType = nodeInfo->linkList->linkType[i];
1304 if (nodeInfo->statusList[linkType].status == BUILD_LINK_STATUS_BUILDING) {
1305 Unlock();
1306 isBuilding = true;
1307 LNN_LOGE(LNN_LANE, "has exist building link, laneReqId=%{public}u", laneReqId);
1308 return false;
1309 }
1310 if (!isBuilding && nodeInfo->statusList[linkType].status == BUILD_LINK_STATUS_SUCC) {
1311 nodeInfo->isCompleted = true;
1312 Unlock();
1313 return true;
1314 }
1315 }
1316 Unlock();
1317 return false;
1318 }
1319
GetLaneLinkInfo(uint32_t laneReqId,LaneLinkType * type,LaneLinkInfo * info)1320 static int32_t GetLaneLinkInfo(uint32_t laneReqId, LaneLinkType *type, LaneLinkInfo *info)
1321 {
1322 if (Lock() != SOFTBUS_OK) {
1323 LNN_LOGE(LNN_LANE, "get lock fail");
1324 return SOFTBUS_LOCK_ERR;
1325 }
1326 LaneLinkNodeInfo *nodeInfo = GetLaneLinkNodeWithoutLock(laneReqId);
1327 if (nodeInfo == NULL) {
1328 Unlock();
1329 LNN_LOGE(LNN_LANE, "get lane link node info fail, laneReqId=%{public}u", laneReqId);
1330 return SOFTBUS_LANE_NOT_FOUND;
1331 }
1332 for (uint32_t i = 0; i < nodeInfo->listNum; i++) {
1333 LaneLinkType linkType = nodeInfo->linkList->linkType[i];
1334 if (nodeInfo->statusList[linkType].status == BUILD_LINK_STATUS_SUCC) {
1335 if (memcpy_s(info, sizeof(LaneLinkInfo), &(nodeInfo->statusList[linkType].linkInfo),
1336 sizeof(LaneLinkInfo)) != EOK) {
1337 Unlock();
1338 LNN_LOGE(LNN_LANE, "info memcpy fail, laneReqId=%{public}u", laneReqId);
1339 return SOFTBUS_MEM_ERR;
1340 }
1341 *type = linkType;
1342 Unlock();
1343 return SOFTBUS_OK;
1344 }
1345 }
1346 Unlock();
1347 LNN_LOGE(LNN_LANE, "not found LaneLinkInfo, laneReqId=%{public}u", laneReqId);
1348 return SOFTBUS_LANE_NOT_FOUND;
1349 }
1350
FreeLowPriorityLink(uint32_t laneReqId,LaneLinkType linkType)1351 static void FreeLowPriorityLink(uint32_t laneReqId, LaneLinkType linkType)
1352 {
1353 if (Lock() != SOFTBUS_OK) {
1354 LNN_LOGE(LNN_LANE, "get lock fail");
1355 return;
1356 }
1357 LaneLinkNodeInfo *nodeInfo = GetLaneLinkNodeWithoutLock(laneReqId);
1358 if (nodeInfo == NULL) {
1359 Unlock();
1360 LNN_LOGE(LNN_LANE, "get lane link node info fail, laneReqId=%{public}u", laneReqId);
1361 return;
1362 }
1363 LinkStatusInfo statusList[LANE_LINK_TYPE_BUTT];
1364 (void)memset_s(&statusList, sizeof(statusList), 0, sizeof(statusList));
1365 uint32_t listNum = 0;
1366 for (uint32_t i = 0; i < nodeInfo->listNum; i++) {
1367 LaneLinkType type = nodeInfo->linkList->linkType[i];
1368 if (type != linkType && nodeInfo->statusList[type].status == BUILD_LINK_STATUS_SUCC) {
1369 if (memcpy_s(&statusList[listNum++], sizeof(LinkStatusInfo), &nodeInfo->statusList[type],
1370 sizeof(LinkStatusInfo)) != EOK) {
1371 continue;
1372 }
1373 }
1374 }
1375 Unlock();
1376 for (uint32_t i = 0; i < listNum; i++) {
1377 FreeUnusedLink(laneReqId, &statusList[i].linkInfo);
1378 }
1379 }
1380
ProcessPowerControlInfoByLaneReqId(const LaneLinkType linkType,uint32_t laneReqId)1381 static void ProcessPowerControlInfoByLaneReqId(const LaneLinkType linkType, uint32_t laneReqId)
1382 {
1383 LaneTransType transType = LANE_T_BUTT;
1384 if (Lock() != SOFTBUS_OK) {
1385 LNN_LOGE(LNN_LANE, "get lock fail");
1386 return;
1387 }
1388 TransReqInfo *item = NULL;
1389 TransReqInfo *next = NULL;
1390 LIST_FOR_EACH_ENTRY_SAFE(item, next, &g_requestList->list, TransReqInfo, node) {
1391 if (item->laneReqId == laneReqId) {
1392 transType = item->allocInfo.transType;
1393 }
1394 }
1395 Unlock();
1396 if (linkType == LANE_HML && IsPowerControlEnabledPacked()) {
1397 LNN_LOGI(LNN_LANE, "low-power transtype = %{public}d", transType);
1398 if (transType == LANE_T_BYTE || transType == LANE_T_MSG) {
1399 DetectDisableWifiDirectApply();
1400 } else {
1401 DetectEnableWifiDirectApply();
1402 }
1403 }
1404 }
1405
NotifyLinkSucc(uint32_t laneReqId)1406 static void NotifyLinkSucc(uint32_t laneReqId)
1407 {
1408 LaneLinkType linkType;
1409 LaneLinkInfo info;
1410 int32_t ret = SOFTBUS_LANE_RESULT_REPORT_ERR;
1411 (void)memset_s(&info, sizeof(LaneLinkInfo), 0, sizeof(LaneLinkInfo));
1412 ret = GetLaneLinkInfo(laneReqId, &linkType, &info);
1413 if (ret != SOFTBUS_OK) {
1414 LNN_LOGE(LNN_LANE, "get LaneLinkInfo fail, laneReqId=%{public}u", laneReqId);
1415 goto FAIL;
1416 }
1417 char localUdid[UDID_BUF_LEN] = {0};
1418 if (LnnGetLocalStrInfo(STRING_KEY_DEV_UDID, localUdid, UDID_BUF_LEN) != SOFTBUS_OK) {
1419 LNN_LOGE(LNN_LANE, "get udid fail, laneReqId=%{public}u", laneReqId);
1420 ret = SOFTBUS_LANE_GET_LEDGER_INFO_ERR;
1421 goto FAIL;
1422 }
1423 LNN_LOGI(LNN_LANE, "check is need peerIp, udidlen=%{public}zu", strlen(info.peerUdid));
1424 uint64_t laneId = INVALID_LANE_ID;
1425 if (strlen(info.peerUdid) == 0) {
1426 laneId = GenerateLaneId(localUdid, info.linkInfo.rawWifiDirect.peerIp, info.type);
1427 } else {
1428 laneId = GenerateLaneId(localUdid, info.peerUdid, info.type);
1429 }
1430 if (laneId == INVALID_LANE_ID) {
1431 LNN_LOGE(LNN_LANE, "generate laneId fail, laneReqId=%{public}u", laneReqId);
1432 ret = SOFTBUS_LANE_ID_GENERATE_FAIL;
1433 goto FAIL;
1434 }
1435 ret = AddLaneResourceToPool(&info, laneId, false);
1436 if (ret != SOFTBUS_OK) {
1437 LNN_LOGE(LNN_LANE, "add linkInfo item fail, laneReqId=%{public}u", laneReqId);
1438 goto FAIL;
1439 }
1440 ProcessPowerControlInfoByLaneReqId(linkType, laneReqId);
1441 NotifyLaneAllocSuccess(laneReqId, laneId, &info);
1442 (void)HandleLaneQosChange(&info);
1443 FreeLowPriorityLink(laneReqId, linkType);
1444 return;
1445 FAIL:
1446 NotifyLaneAllocFail(laneReqId, ret);
1447 }
1448
LaneLinkSuccess(SoftBusMessage * msg)1449 static void LaneLinkSuccess(SoftBusMessage *msg)
1450 {
1451 if (msg->obj == NULL) {
1452 LNN_LOGE(LNN_LANE, "invalid msg->obj");
1453 return;
1454 }
1455 LaneLinkInfo *info = (LaneLinkInfo *)msg->obj;
1456 uint32_t laneReqId = (uint32_t)msg->arg1;
1457 LaneLinkType linkType = (LaneLinkType)msg->arg2;
1458 if (UpdateLinkStatus(laneReqId, BUILD_LINK_STATUS_SUCC, linkType, info, SOFTBUS_OK) != SOFTBUS_OK) {
1459 LNN_LOGE(LNN_LANE, "update link status fail, laneReqId=%{public}u", laneReqId);
1460 SoftBusFree(info);
1461 return;
1462 }
1463 SoftBusFree(info);
1464 if (IsNeedNotifySucc(laneReqId)) {
1465 RemoveLinkTimeoutMessage(laneReqId, LANE_LINK_TYPE_BUTT);
1466 NotifyLinkSucc(laneReqId);
1467 DeleteLaneLinkNode(laneReqId);
1468 }
1469 }
1470
IsNeedNotifyFail(uint32_t laneReqId)1471 static bool IsNeedNotifyFail(uint32_t laneReqId)
1472 {
1473 bool notifyFail = false;
1474 if (Lock() != SOFTBUS_OK) {
1475 LNN_LOGE(LNN_LANE, "get lock fail");
1476 return true;
1477 }
1478 LaneLinkNodeInfo *nodeInfo = GetLaneLinkNodeWithoutLock(laneReqId);
1479 if (nodeInfo == NULL) {
1480 Unlock();
1481 LNN_LOGE(LNN_LANE, "get lane link node info fail, laneReqId=%{public}u", laneReqId);
1482 return true;
1483 }
1484 uint64_t costTime = GetCostTime(nodeInfo->triggerLinkTime);
1485 if (costTime >= nodeInfo->availableLinkTime || nodeInfo->linkRetryIdx >= nodeInfo->listNum) {
1486 LNN_LOGE(LNN_LANE, "link retry exceed limit, laneReqId=%{public}u", laneReqId);
1487 notifyFail = true;
1488 }
1489 if (!notifyFail) {
1490 nodeInfo->isCompleted = false;
1491 Unlock();
1492 return notifyFail;
1493 }
1494 for (uint32_t i = 0; i < nodeInfo->linkRetryIdx; i++) {
1495 LaneLinkType linkType = nodeInfo->linkList->linkType[i];
1496 if (nodeInfo->statusList[linkType].status != BUILD_LINK_STATUS_FAIL) {
1497 notifyFail = false;
1498 }
1499 }
1500 nodeInfo->isCompleted = notifyFail ? true : false;
1501 Unlock();
1502 return notifyFail;
1503 }
1504
BuildLinkRetry(uint32_t laneReqId)1505 static void BuildLinkRetry(uint32_t laneReqId)
1506 {
1507 bool needRetry = true;
1508 if (Lock() != SOFTBUS_OK) {
1509 LNN_LOGE(LNN_LANE, "get lock fail");
1510 NotifyLaneAllocFail(laneReqId, SOFTBUS_LOCK_ERR);
1511 return;
1512 }
1513 LaneLinkNodeInfo *nodeInfo = GetLaneLinkNodeWithoutLock(laneReqId);
1514 if (nodeInfo == NULL) {
1515 Unlock();
1516 LNN_LOGE(LNN_LANE, "get lane link node info fail, laneReqId=%{public}u", laneReqId);
1517 NotifyLaneAllocFail(laneReqId, SOFTBUS_LANE_NOT_FOUND);
1518 return;
1519 }
1520 uint64_t costTime = GetCostTime(nodeInfo->triggerLinkTime);
1521 if (costTime >= nodeInfo->availableLinkTime || nodeInfo->linkRetryIdx >= nodeInfo->listNum) {
1522 LNN_LOGE(LNN_LANE, "link retry exceed limit, laneReqId=%{public}u", laneReqId);
1523 Unlock();
1524 return;
1525 }
1526 for (uint32_t i = 0; i < nodeInfo->linkRetryIdx; i++) {
1527 LaneLinkType linkType = nodeInfo->linkList->linkType[i];
1528 if (nodeInfo->statusList[linkType].status == BUILD_LINK_STATUS_SUCC) {
1529 LNN_LOGI(LNN_LANE, "has exist high priority succ link, laneReqId=%{public}u", laneReqId);
1530 needRetry = false;
1531 }
1532 if (nodeInfo->linkRetryIdx < nodeInfo->listNum &&
1533 nodeInfo->linkList->linkType[nodeInfo->linkRetryIdx] == LANE_P2P && linkType == LANE_HML &&
1534 nodeInfo->statusList[linkType].status == BUILD_LINK_STATUS_BUILDING) {
1535 LNN_LOGI(LNN_LANE, "refuse same type link repeat build, laneReqId=%{public}u", laneReqId);
1536 needRetry = false;
1537 }
1538 }
1539 Unlock();
1540 if (needRetry) {
1541 LNN_LOGI(LNN_LANE, "continue to build link, laneReqId=%{public}u", laneReqId);
1542 (void)LnnLanePostMsgToHandler(MSG_TYPE_LANE_TRIGGER_LINK, laneReqId, 0, NULL, 0);
1543 }
1544 }
1545
LaneLinkFail(SoftBusMessage * msg)1546 static void LaneLinkFail(SoftBusMessage *msg)
1547 {
1548 uint32_t laneReqId = (uint32_t)msg->arg1;
1549 int32_t failReason = (int32_t)msg->arg2;
1550 if (msg->obj == NULL) {
1551 LNN_LOGE(LNN_LANE, "invalid msg->obj");
1552 NotifyLaneAllocFail(laneReqId, failReason);
1553 return;
1554 }
1555 LinkFailInfo *failInfo = (LinkFailInfo *)msg->obj;
1556 LaneLinkType linkType = failInfo->linkType;
1557 SoftBusFree(failInfo);
1558 LnnDumpLocalBasicInfo();
1559 LnnDumpOnlineDeviceInfo();
1560 if (UpdateLinkStatus(laneReqId, BUILD_LINK_STATUS_FAIL, linkType, NULL, failReason) != SOFTBUS_OK) {
1561 return;
1562 }
1563 if (IsNeedNotifySucc(laneReqId)) {
1564 RemoveLinkTimeoutMessage(laneReqId, LANE_LINK_TYPE_BUTT);
1565 NotifyLinkSucc(laneReqId);
1566 DeleteLaneLinkNode(laneReqId);
1567 } else if (IsNeedNotifyFail(laneReqId)) {
1568 RemoveLinkTimeoutMessage(laneReqId, LANE_LINK_TYPE_BUTT);
1569 NotifyLaneAllocFail(laneReqId, GetErrCodeWithLock(laneReqId));
1570 DeleteLaneLinkNode(laneReqId);
1571 } else {
1572 BuildLinkRetry(laneReqId);
1573 }
1574 }
1575
LaneStateChange(SoftBusMessage * msg)1576 static void LaneStateChange(SoftBusMessage *msg)
1577 {
1578 if (msg->obj == NULL) {
1579 LNN_LOGE(LNN_LANE, "invalid msg->obj");
1580 return;
1581 }
1582 StateNotifyInfo *info = (StateNotifyInfo*)msg->obj;
1583 switch (info->state) {
1584 case LANE_STATE_LINKUP:
1585 if (LaneLinkupNotify(info->peerUdid, &info->laneLinkInfo) != SOFTBUS_OK) {
1586 LNN_LOGE(LNN_LANE, "notify lane linkup fail");
1587 }
1588 break;
1589 case LANE_STATE_LINKDOWN:
1590 if (LaneLinkdownNotify(info->peerUdid, &info->laneLinkInfo) != SOFTBUS_OK) {
1591 LNN_LOGE(LNN_LANE, "notify lane linkdown fail");
1592 }
1593 break;
1594 default:
1595 LNN_LOGE(LNN_LANE, "lane state=%{public}d cannot found", info->state);
1596 }
1597 SoftBusFree(info);
1598 }
1599
UpdateFreeLaneStatus(uint32_t laneReqId)1600 int32_t UpdateFreeLaneStatus(uint32_t laneReqId)
1601 {
1602 if (laneReqId == INVALID_LANE_REQ_ID) {
1603 LNN_LOGE(LNN_LANE, "laneHandle is invalid parameter");
1604 return SOFTBUS_INVALID_PARAM;
1605 }
1606 if (Lock() != SOFTBUS_OK) {
1607 LNN_LOGE(LNN_LANE, "get lock fail");
1608 return SOFTBUS_LOCK_ERR;
1609 }
1610 TransReqInfo *item = NULL;
1611 LIST_FOR_EACH_ENTRY(item, &g_requestList->list, TransReqInfo, node) {
1612 if (item->laneReqId == laneReqId) {
1613 item->hasNotifiedFree = true;
1614 Unlock();
1615 return SOFTBUS_OK;
1616 }
1617 }
1618 Unlock();
1619 LNN_LOGE(LNN_LANE, "Update free lane status fail, laneReqId=%{public}d", laneReqId);
1620 return SOFTBUS_LANE_NOT_FOUND;
1621 }
1622
HandleDetectTimeout(SoftBusMessage * msg)1623 static void HandleDetectTimeout(SoftBusMessage *msg)
1624 {
1625 uint32_t detectId = (uint32_t)msg->arg1;
1626 LNN_LOGI(LNN_LANE, "lane detect timeout. detectId=%{public}u", detectId);
1627 NotifyDetectTimeout(detectId);
1628 }
1629
HandleLinkTimeout(SoftBusMessage * msg)1630 static void HandleLinkTimeout(SoftBusMessage *msg)
1631 {
1632 uint32_t laneReqId = (uint32_t)msg->arg1;
1633 LaneLinkType timeoutLinkType = (LaneLinkType)msg->arg2;
1634 if (Lock() != SOFTBUS_OK) {
1635 LNN_LOGE(LNN_LANE, "get lock fail");
1636 return;
1637 }
1638 LaneLinkNodeInfo *nodeInfo = GetLaneLinkNodeWithoutLock(laneReqId);
1639 if (nodeInfo == NULL) {
1640 Unlock();
1641 LNN_LOGE(LNN_LANE, "get lane link node info fail, laneReqId=%{public}u", laneReqId);
1642 return;
1643 }
1644 uint64_t costTime = GetCostTime(nodeInfo->triggerLinkTime);
1645 if (costTime >= nodeInfo->availableLinkTime || nodeInfo->linkRetryIdx >= nodeInfo->listNum) {
1646 LNN_LOGE(LNN_LANE, "link retry exceed limit, laneReqId=%{public}u", laneReqId);
1647 Unlock();
1648 return;
1649 }
1650 for (uint32_t i = 0; i < nodeInfo->linkRetryIdx; i++) {
1651 LaneLinkType linkType = nodeInfo->linkList->linkType[i];
1652 if (nodeInfo->statusList[linkType].status == BUILD_LINK_STATUS_SUCC) {
1653 LNN_LOGI(LNN_LANE, "a successful link already exist, laneReqId=%{public}u, linkType=%{public}d",
1654 laneReqId, linkType);
1655 Unlock();
1656 return;
1657 }
1658 }
1659 if (nodeInfo->linkList->linkType[nodeInfo->linkRetryIdx] == LANE_P2P) {
1660 for (uint32_t i = 0; i < nodeInfo->linkRetryIdx; i++) {
1661 LaneLinkType type = nodeInfo->linkList->linkType[i];
1662 if (type == LANE_HML && nodeInfo->statusList[type].status == BUILD_LINK_STATUS_BUILDING) {
1663 LNN_LOGI(LNN_LANE, "refuse same type link repeat build, laneReqId=%{public}u", laneReqId);
1664 Unlock();
1665 return;
1666 }
1667 }
1668 }
1669 Unlock();
1670 LNN_LOGI(LNN_LANE, "continue to build link, laneReqId=%{public}u, timeoutLinkType=%{public}d",
1671 laneReqId, timeoutLinkType);
1672 (void)LnnLanePostMsgToHandler(MSG_TYPE_LANE_TRIGGER_LINK, laneReqId, 0, NULL, 0);
1673 }
1674
MsgHandler(SoftBusMessage * msg)1675 static void MsgHandler(SoftBusMessage *msg)
1676 {
1677 if (msg == NULL) {
1678 return;
1679 }
1680 switch (msg->what) {
1681 case MSG_TYPE_LANE_TRIGGER_LINK:
1682 LaneTriggerLink(msg);
1683 break;
1684 case MSG_TYPE_LANE_LINK_SUCCESS:
1685 LaneLinkSuccess(msg);
1686 break;
1687 case MSG_TYPE_LANE_LINK_FAIL:
1688 LaneLinkFail(msg);
1689 break;
1690 case MSG_TYPE_LANE_STATE_CHANGE:
1691 LaneStateChange(msg);
1692 break;
1693 case MSG_TYPE_DELAY_DESTROY_LINK:
1694 HandleDelayDestroyLink(msg);
1695 break;
1696 case MSG_TYPE_LANE_DETECT_TIMEOUT:
1697 HandleDetectTimeout(msg);
1698 break;
1699 case MSG_TYPE_LANE_LINK_TIMEOUT:
1700 HandleLinkTimeout(msg);
1701 break;
1702 case MSG_TYPE_NOTIFY_FREE_LANE_RESULT:
1703 HandelNotifyFreeLaneResult(msg);
1704 break;
1705 default:
1706 LNN_LOGE(LNN_LANE, "msg type=%{public}d cannot found", msg->what);
1707 break;
1708 }
1709 return;
1710 }
1711
InitLooper(void)1712 static int32_t InitLooper(void)
1713 {
1714 g_laneLoopHandler.name = "transLaneLooper";
1715 g_laneLoopHandler.HandleMessage = MsgHandler;
1716 g_laneLoopHandler.looper = GetLooper(LOOP_TYPE_LNN);
1717 if (g_laneLoopHandler.looper == NULL) {
1718 LNN_LOGE(LNN_LANE, "transLane init looper fail");
1719 return SOFTBUS_NO_INIT;
1720 }
1721 return SOFTBUS_OK;
1722 }
1723
Init(const ILaneIdStateListener * listener)1724 static void Init(const ILaneIdStateListener *listener)
1725 {
1726 if (g_requestList != NULL) {
1727 LNN_LOGW(LNN_LANE, "already init");
1728 return;
1729 }
1730 if (InitLooper() != SOFTBUS_OK) {
1731 LNN_LOGE(LNN_LANE, "init looper fail");
1732 return;
1733 }
1734
1735 if (SoftBusMutexInit(&g_transLaneMutex, NULL) != SOFTBUS_OK) {
1736 LNN_LOGE(LNN_LANE, "transLane mutex init fail");
1737 return;
1738 }
1739 g_requestList = (TransLaneList *)SoftBusCalloc(sizeof(TransLaneList));
1740 if (g_requestList == NULL) {
1741 LNN_LOGE(LNN_LANE, "transLane malloc fail");
1742 (void)SoftBusMutexDestroy(&g_transLaneMutex);
1743 return;
1744 }
1745 ListInit(&g_requestList->list);
1746 ListInit(&g_multiLinkList);
1747 g_laneIdCallback = (ILaneIdStateListener *)listener;
1748 if (InitLaneReliability() != SOFTBUS_OK) {
1749 LNN_LOGE(LNN_LANE, "init laneReliability fail");
1750 return;
1751 }
1752 }
1753
Deinit(void)1754 static void Deinit(void)
1755 {
1756 if (g_requestList == NULL) {
1757 return;
1758 }
1759 if (Lock() != SOFTBUS_OK) {
1760 LNN_LOGE(LNN_LANE, "get lock fail");
1761 return;
1762 }
1763 TransReqInfo *item = NULL;
1764 TransReqInfo *nextItem = NULL;
1765 LIST_FOR_EACH_ENTRY_SAFE(item, nextItem, &g_requestList->list, TransReqInfo, node) {
1766 ListDelete(&item->node);
1767 SoftBusFree(item);
1768 g_requestList->cnt--;
1769 }
1770 Unlock();
1771 (void)SoftBusMutexDestroy(&g_transLaneMutex);
1772 SoftBusFree(g_requestList);
1773 g_requestList = NULL;
1774 DeinitLaneReliability();
1775 }
1776
1777 static LaneInterface g_transLaneObject = {
1778 .init = Init,
1779 .deinit = Deinit,
1780 .allocLane = Alloc,
1781 .allocLaneByQos = AllocLaneByQos,
1782 .allocRawLane = AllocRawLane,
1783 .reallocLaneByQos = ReallocLaneByQos,
1784 .allocTargetLane = AllocTargetLane,
1785 .cancelLane = CancelLane,
1786 .freeLane = FreeLane,
1787 };
1788
TransLaneGetInstance(void)1789 LaneInterface *TransLaneGetInstance(void)
1790 {
1791 return &g_transLaneObject;
1792 }
1793
GetTransReqInfoByLaneReqId(uint32_t laneReqId,TransReqInfo * reqInfo)1794 int32_t GetTransReqInfoByLaneReqId(uint32_t laneReqId, TransReqInfo *reqInfo)
1795 {
1796 if (reqInfo == NULL || laneReqId == INVALID_LANE_REQ_ID) {
1797 return SOFTBUS_INVALID_PARAM;
1798 }
1799 if (Lock() != SOFTBUS_OK) {
1800 LNN_LOGE(LNN_LANE, "get lock fail");
1801 return SOFTBUS_LOCK_ERR;
1802 }
1803 TransReqInfo *item = NULL;
1804 LIST_FOR_EACH_ENTRY(item, &g_requestList->list, TransReqInfo, node) {
1805 if (item->laneReqId == laneReqId) {
1806 if (memcpy_s(reqInfo, sizeof(TransReqInfo), item, sizeof(TransReqInfo)) != EOK) {
1807 LNN_LOGE(LNN_LANE, "memcpy TransReqInfo fail");
1808 Unlock();
1809 return SOFTBUS_MEM_ERR;
1810 }
1811 Unlock();
1812 return SOFTBUS_OK;
1813 }
1814 }
1815 Unlock();
1816 return SOFTBUS_LANE_NOT_FOUND;
1817 }
1818
PostDetectTimeoutMessage(uint32_t detectId,uint64_t delayMillis)1819 int32_t PostDetectTimeoutMessage(uint32_t detectId, uint64_t delayMillis)
1820 {
1821 LNN_LOGI(LNN_LANE, "post timeout message, detectId=%{public}u", detectId);
1822 return LnnLanePostMsgToHandler(MSG_TYPE_LANE_DETECT_TIMEOUT, detectId, 0, NULL, delayMillis);
1823 }
1824
RemoveDetectTimeout(const SoftBusMessage * msg,void * data)1825 static int32_t RemoveDetectTimeout(const SoftBusMessage *msg, void *data)
1826 {
1827 uint32_t *detectId = (uint32_t *)data;
1828 if (msg->what != MSG_TYPE_LANE_DETECT_TIMEOUT) {
1829 return SOFTBUS_INVALID_PARAM;
1830 }
1831 if (msg->arg1 == *detectId) {
1832 LNN_LOGE(LNN_LANE, "remove detect timeout message success. detectId=%{public}u", *detectId);
1833 return SOFTBUS_OK;
1834 }
1835 return SOFTBUS_INVALID_PARAM;
1836 }
1837
RemoveDetectTimeoutMessage(uint32_t detectId)1838 void RemoveDetectTimeoutMessage(uint32_t detectId)
1839 {
1840 LNN_LOGI(LNN_LANE, "remove detect timeout message. detectId=%{public}u", detectId);
1841 g_laneLoopHandler.looper->RemoveMessageCustom(g_laneLoopHandler.looper, &g_laneLoopHandler,
1842 RemoveDetectTimeout, &detectId);
1843 }
1844
PostDelayDestroyMessage(uint32_t laneReqId,uint64_t laneId,uint64_t delayMillis)1845 int32_t PostDelayDestroyMessage(uint32_t laneReqId, uint64_t laneId, uint64_t delayMillis)
1846 {
1847 LNN_LOGI(LNN_LANE, "post delay destroy message. laneReqId=%{public}u, laneId=%{public}" PRIu64 "",
1848 laneReqId, laneId);
1849 return LnnLanePostMsgToHandler(MSG_TYPE_DELAY_DESTROY_LINK, laneReqId, laneId, NULL, delayMillis);
1850 }
1851
PostLaneStateChangeMessage(LaneState state,const char * peerUdid,const LaneLinkInfo * laneLinkInfo)1852 int32_t PostLaneStateChangeMessage(LaneState state, const char *peerUdid, const LaneLinkInfo *laneLinkInfo)
1853 {
1854 LNN_LOGI(LNN_LANE, "post lane state change msg, state=%{public}d", state);
1855 if (peerUdid == NULL || laneLinkInfo == NULL) {
1856 LNN_LOGE(LNN_LANE, "invalid param");
1857 return SOFTBUS_INVALID_PARAM;
1858 }
1859 StateNotifyInfo *stateNotifyInfo = (StateNotifyInfo *)SoftBusCalloc(sizeof(StateNotifyInfo));
1860 if (stateNotifyInfo == NULL) {
1861 LNN_LOGE(LNN_LANE, "calloc stateNotifyInfo fail");
1862 return SOFTBUS_MALLOC_ERR;
1863 }
1864 stateNotifyInfo->state = state;
1865 if (strncpy_s(stateNotifyInfo->peerUdid, UDID_BUF_LEN, peerUdid, UDID_BUF_LEN) != EOK) {
1866 SoftBusFree(stateNotifyInfo);
1867 LNN_LOGE(LNN_STATE, "copy peerUdid fail");
1868 return SOFTBUS_STRCPY_ERR;
1869 }
1870 if (memcpy_s(&stateNotifyInfo->laneLinkInfo, sizeof(LaneLinkInfo), laneLinkInfo,
1871 sizeof(LaneLinkInfo)) != EOK) {
1872 SoftBusFree(stateNotifyInfo);
1873 LNN_LOGE(LNN_LANE, "memcpy laneLinkInfo fail");
1874 return SOFTBUS_MEM_ERR;
1875 }
1876 int32_t ret = LnnLanePostMsgToHandler(MSG_TYPE_LANE_STATE_CHANGE, 0, 0, stateNotifyInfo, 0);
1877 if (ret != SOFTBUS_OK) {
1878 SoftBusFree(stateNotifyInfo);
1879 LNN_LOGE(LNN_LANE, "post lane state change msg fail");
1880 return ret;
1881 }
1882 return SOFTBUS_OK;
1883 }
1884
RemoveDelayDestroy(const SoftBusMessage * msg,void * data)1885 static int32_t RemoveDelayDestroy(const SoftBusMessage *msg, void *data)
1886 {
1887 uint64_t *laneId = (uint64_t *)data;
1888 if (msg->what == MSG_TYPE_DELAY_DESTROY_LINK && *laneId == (uint64_t)msg->arg2) {
1889 LNN_LOGI(LNN_LANE, "remove delay destroy message succ, laneId=%{public}" PRIu64 "", *laneId);
1890 return SOFTBUS_OK;
1891 }
1892 return SOFTBUS_INVALID_PARAM;
1893 }
1894
RemoveDelayDestroyMessage(uint64_t laneId)1895 void RemoveDelayDestroyMessage(uint64_t laneId)
1896 {
1897 g_laneLoopHandler.looper->RemoveMessageCustom(g_laneLoopHandler.looper, &g_laneLoopHandler,
1898 RemoveDelayDestroy, &laneId);
1899 }
1900
DelLogicAndLaneRelationship(uint64_t laneId)1901 void DelLogicAndLaneRelationship(uint64_t laneId)
1902 {
1903 if (Lock() != SOFTBUS_OK) {
1904 LNN_LOGE(LNN_LANE, "get lock fail");
1905 return;
1906 }
1907 TransReqInfo *item = NULL;
1908 LIST_FOR_EACH_ENTRY(item, &g_requestList->list, TransReqInfo, node) {
1909 if (item->laneId == laneId) {
1910 item->laneId = INVALID_LANE_ID;
1911 }
1912 }
1913 Unlock();
1914 }
1915
DestroyRequestNodeList(ListNode * reqInfoList)1916 static void DestroyRequestNodeList(ListNode *reqInfoList)
1917 {
1918 TransReqInfo *item = NULL;
1919 TransReqInfo *next = NULL;
1920 LIST_FOR_EACH_ENTRY_SAFE(item, next, reqInfoList, TransReqInfo, node) {
1921 ListDelete(&item->node);
1922 SoftBusFree(item);
1923 }
1924 }
1925
GetNodeToNotifyQosEvent(const char * peerNetworkId,ListNode * reqInfoList)1926 static int32_t GetNodeToNotifyQosEvent(const char *peerNetworkId, ListNode *reqInfoList)
1927 {
1928 if (peerNetworkId == NULL || reqInfoList == NULL) {
1929 return SOFTBUS_INVALID_PARAM;
1930 }
1931 if (Lock() != SOFTBUS_OK) {
1932 return SOFTBUS_LOCK_ERR;
1933 }
1934 int32_t ret = SOFTBUS_OK;
1935 TransReqInfo *item = NULL;
1936 TransReqInfo *next = NULL;
1937 LIST_FOR_EACH_ENTRY_SAFE(item, next, &g_requestList->list, TransReqInfo, node) {
1938 if (strcmp(item->allocInfo.networkId, peerNetworkId) != 0 ||
1939 item->allocInfo.qosRequire.minBW != DB_MAGIC_NUMBER) {
1940 continue;
1941 }
1942 LNN_LOGI(LNN_LANE, "laneReqId=%{public}u, laneId=%{public}" PRIu64 "", item->laneReqId, item->laneId);
1943 TransReqInfo *info = (TransReqInfo *)SoftBusCalloc(sizeof(TransReqInfo));
1944 if (info == NULL) {
1945 ret = SOFTBUS_MALLOC_ERR;
1946 break;
1947 }
1948 ListInit(&info->node);
1949 if (memcpy_s(info, sizeof(TransReqInfo), item, sizeof(TransReqInfo)) != EOK) {
1950 LNN_LOGE(LNN_LANE, "memcpy fail");
1951 SoftBusFree(info);
1952 ret = SOFTBUS_MEM_ERR;
1953 break;
1954 }
1955 ListTailInsert(reqInfoList, &info->node);
1956 }
1957 Unlock();
1958 if (ret != SOFTBUS_OK) {
1959 DestroyRequestNodeList(reqInfoList);
1960 }
1961 return ret;
1962 }
1963
NeedToNotify(const TransReqInfo * info)1964 static bool NeedToNotify(const TransReqInfo *info)
1965 {
1966 if (info == NULL) {
1967 return false;
1968 }
1969 LaneResource laneLinkInfo;
1970 (void)memset_s(&laneLinkInfo, sizeof(LaneResource), 0, sizeof(LaneResource));
1971 int32_t ret = FindLaneResourceByLaneId(info->laneId, &laneLinkInfo);
1972 if (ret != SOFTBUS_OK) {
1973 LNN_LOGE(LNN_LANE, "find laneId=%{public}" PRIu64 " fail, ret=%{public}d", info->laneId, ret);
1974 return false;
1975 }
1976 LNN_LOGI(LNN_LANE, "laneReqId=%{public}u, type=%{public}d", info->laneReqId, laneLinkInfo.link.type);
1977 return laneLinkInfo.link.type == LANE_BR;
1978 }
1979
HandleLaneQosChange(const LaneLinkInfo * laneLinkInfo)1980 int32_t HandleLaneQosChange(const LaneLinkInfo *laneLinkInfo)
1981 {
1982 if (laneLinkInfo == NULL) {
1983 LNN_LOGE(LNN_LANE, "invalid param");
1984 return SOFTBUS_INVALID_PARAM;
1985 }
1986 if (laneLinkInfo->type != LANE_P2P && laneLinkInfo->type != LANE_HML) {
1987 return SOFTBUS_OK;
1988 }
1989 char peerNetworkId[NETWORK_ID_BUF_LEN] = {0};
1990 int32_t ret = LnnGetNetworkIdByUdid(laneLinkInfo->peerUdid, peerNetworkId, sizeof(peerNetworkId));
1991 if (ret != SOFTBUS_OK) {
1992 LNN_LOGE(LNN_LANE, "get networkId by udid fail");
1993 return ret;
1994 }
1995 ListNode reqInfoList;
1996 ListInit(&reqInfoList);
1997 ret = GetNodeToNotifyQosEvent(peerNetworkId, &reqInfoList);
1998 if (ret != SOFTBUS_OK) {
1999 LNN_LOGE(LNN_LANE, "get list fail, ret=%{public}d", ret);
2000 return ret;
2001 }
2002 TransReqInfo *item = NULL;
2003 TransReqInfo *next = NULL;
2004 LIST_FOR_EACH_ENTRY_SAFE(item, next, &reqInfoList, TransReqInfo, node) {
2005 if (item->listener.onLaneQosEvent != NULL && NeedToNotify(item)) {
2006 item->listener.onLaneQosEvent(item->laneReqId, LANE_OWNER_OTHER, LANE_QOS_BW_HIGH);
2007 }
2008 }
2009 DestroyRequestNodeList(&reqInfoList);
2010 return SOFTBUS_OK;
2011 }