1 /*
2  * Copyright (c) 2022-2023 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_lane_link.h"
17 
18 #include <securec.h>
19 
20 #include "anonymizer.h"
21 #include "bus_center_info_key.h"
22 #include "bus_center_manager.h"
23 #include "lnn_distributed_net_ledger.h"
24 #include "lnn_lane.h"
25 #include "lnn_lane_common.h"
26 #include "lnn_lane_def.h"
27 #include "lnn_lane_score.h"
28 #include "lnn_lane_link_p2p.h"
29 #include "lnn_parameter_utils.h"
30 #include "lnn_lane_power_control.h"
31 #include "lnn_lane_reliability.h"
32 #include "lnn_lane_vap_info.h"
33 #include "lnn_local_net_ledger.h"
34 #include "lnn_log.h"
35 #include "lnn_net_capability.h"
36 #include "lnn_network_manager.h"
37 #include "lnn_node_info.h"
38 #include "lnn_physical_subnet_manager.h"
39 #include "lnn_trans_lane.h"
40 #include "softbus_adapter_mem.h"
41 #include "softbus_adapter_crypto.h"
42 #include "softbus_conn_ble_connection.h"
43 #include "softbus_conn_ble_manager.h"
44 #include "softbus_def.h"
45 #include "softbus_errcode.h"
46 #include "softbus_network_utils.h"
47 #include "softbus_protocol_def.h"
48 #include "softbus_utils.h"
49 #include "trans_network_statistics.h"
50 #include "wifi_direct_manager.h"
51 
52 #define IF_NAME_BR  "br0"
53 #define IF_NAME_BLE "ble0"
54 #define IF_NAME_P2P "p2p0"
55 #define IF_NAME_HML "chba0"
56 #define TYPE_BUF_LEN 2
57 #define LANE_ID_BUF_LEN (UDID_BUF_LEN + UDID_BUF_LEN + TYPE_BUF_LEN)
58 #define LANE_ID_HASH_LEN 32
59 #define UDID_SHORT_HASH_HEXSTR_LEN_TMP 16
60 #define UDID_SHORT_HASH_LEN_TMP 8
61 
62 static bool g_enabledLowPower = false;
63 
64 typedef int32_t (*LaneLinkByType)(uint32_t reqId, const LinkRequest *reqInfo, const LaneLinkCb *callback);
65 
66 static SoftBusList g_laneResource;
67 
LaneLock(void)68 static int32_t LaneLock(void)
69 {
70     return SoftBusMutexLock(&g_laneResource.lock);
71 }
72 
LaneUnlock(void)73 static void LaneUnlock(void)
74 {
75     (void)SoftBusMutexUnlock(&g_laneResource.lock);
76 }
77 
GenerateLaneId(const char * localUdid,const char * remoteUdid,LaneLinkType linkType)78 uint64_t GenerateLaneId(const char *localUdid, const char *remoteUdid, LaneLinkType linkType)
79 {
80     if (localUdid == NULL || remoteUdid == NULL) {
81         LNN_LOGE(LNN_LANE, "udid is NULL");
82         return SOFTBUS_INVALID_PARAM;
83     }
84     const char *bigUdid = NULL;
85     const char *smallUdid = NULL;
86     if (strcmp(localUdid, remoteUdid) >= 0) {
87         bigUdid = localUdid;
88         smallUdid = remoteUdid;
89     } else {
90         bigUdid = remoteUdid;
91         smallUdid = localUdid;
92     }
93     uint8_t laneIdParamBytes[LANE_ID_BUF_LEN];
94     (void)memset_s(laneIdParamBytes, sizeof(laneIdParamBytes), 0, sizeof(laneIdParamBytes));
95     uint64_t laneId = INVALID_LANE_ID;
96     uint16_t type = (uint16_t)linkType;
97     // sharded copy, LANE_ID_BUF_LEN = UDID_BUF_LEN + UDID_BUF_LEN + TYPE_BUF_LEN
98     if (memcpy_s(laneIdParamBytes, UDID_BUF_LEN, bigUdid, strlen(bigUdid)) == EOK &&
99         memcpy_s(laneIdParamBytes + UDID_BUF_LEN, UDID_BUF_LEN, smallUdid, strlen(smallUdid)) == EOK &&
100         memcpy_s(laneIdParamBytes + UDID_BUF_LEN + UDID_BUF_LEN, TYPE_BUF_LEN, &type, sizeof(type)) == EOK) {
101         uint8_t laneIdHash[LANE_ID_HASH_LEN] = {0};
102         if (SoftBusGenerateStrHash(laneIdParamBytes, LANE_ID_BUF_LEN, laneIdHash) != SOFTBUS_OK) {
103             LNN_LOGE(LNN_LANE, "generate laneId hash fail");
104             return INVALID_LANE_ID;
105         }
106         uint32_t len = sizeof(laneId) <= LANE_ID_HASH_LEN ? sizeof(laneId) : LANE_ID_HASH_LEN;
107         if (memcpy_s(&laneId, sizeof(laneId), laneIdHash, len) != EOK) {
108             LNN_LOGE(LNN_LANE, "memcpy laneId hash fail");
109             return INVALID_LANE_ID;
110         }
111         char *anonyLocalUdid = NULL;
112         char *anonyRemoteUdid = NULL;
113         Anonymize(localUdid, &anonyLocalUdid);
114         Anonymize(remoteUdid, &anonyRemoteUdid);
115         LNN_LOGI(LNN_LANE, "generate laneId=%{public}" PRIu64 " with localUdid=%{public}s,"
116             "remoteUdid=%{public}s, linkType=%{public}d",
117             laneId, AnonymizeWrapper(anonyLocalUdid), AnonymizeWrapper(anonyRemoteUdid), linkType);
118         AnonymizeFree(anonyLocalUdid);
119         AnonymizeFree(anonyRemoteUdid);
120         return laneId;
121     }
122     LNN_LOGE(LNN_LANE, "memcpy laneId param bytes fail");
123     return INVALID_LANE_ID;
124 }
125 
IsValidLinkAddr(const LaneLinkInfo * sourceLink,const LaneLinkInfo * linkInfoItem)126 static bool IsValidLinkAddr(const LaneLinkInfo *sourceLink, const LaneLinkInfo *linkInfoItem)
127 {
128     switch (sourceLink->type) {
129         case LANE_BR:
130             if (strncmp(sourceLink->linkInfo.br.brMac, linkInfoItem->linkInfo.br.brMac, BT_MAC_LEN) != 0) {
131                 break;
132             }
133             return true;
134         case LANE_BLE:
135         case LANE_COC:
136             if (strncmp(sourceLink->linkInfo.ble.bleMac, linkInfoItem->linkInfo.ble.bleMac, BT_MAC_LEN) != 0) {
137                 break;
138             }
139             return true;
140         case LANE_P2P:
141         case LANE_HML:
142             if (strncmp(sourceLink->linkInfo.p2p.connInfo.peerIp,
143                 linkInfoItem->linkInfo.p2p.connInfo.peerIp, IP_LEN) != 0) {
144                 break;
145             }
146             return true;
147         case LANE_BLE_DIRECT:
148         case LANE_COC_DIRECT:
149             if (strncmp(sourceLink->linkInfo.bleDirect.networkId, linkInfoItem->linkInfo.bleDirect.networkId,
150                 NETWORK_ID_BUF_LEN) != 0) {
151                 break;
152             }
153             return true;
154         case LANE_WLAN_5G:
155         case LANE_WLAN_2P4G:
156         case LANE_ETH:
157             if (strncmp(sourceLink->linkInfo.wlan.connInfo.addr,
158                 linkInfoItem->linkInfo.wlan.connInfo.addr, MAX_SOCKET_ADDR_LEN) != 0) {
159                 break;
160             }
161             return true;
162         default:
163             LNN_LOGE(LNN_LANE, "invalid linkType=%{public}d", sourceLink->type);
164             return false;
165     }
166     LNN_LOGE(LNN_LANE, "lane resource is different form input link addr, linkType=%{public}d", sourceLink->type);
167     return false;
168 }
169 
GetValidLaneResource(const LaneLinkInfo * linkInfoItem)170 static LaneResource* GetValidLaneResource(const LaneLinkInfo *linkInfoItem)
171 {
172     LaneResource *item = NULL;
173     LaneResource *next = NULL;
174     LIST_FOR_EACH_ENTRY_SAFE(item, next, &g_laneResource.list, LaneResource, node) {
175         if (linkInfoItem->type == item->link.type &&
176             strncmp(item->link.peerUdid, linkInfoItem->peerUdid, UDID_BUF_LEN) == 0 &&
177             IsValidLinkAddr(&(item->link), linkInfoItem)) {
178             return item;
179         }
180     }
181     return NULL;
182 }
183 
GetVapType(LaneLinkType linkType)184 static LnnVapType GetVapType(LaneLinkType linkType)
185 {
186     switch (linkType) {
187         case LANE_HML:
188             return LNN_VAP_HML;
189         case LANE_P2P:
190             return LNN_VAP_P2P;
191         default:
192             LNN_LOGE(LNN_LANE, "unexcepted linkType=%{public}d", linkType);
193             return LNN_VAP_UNKNOWN;
194     }
195 }
196 
AddVapInfo(const LaneLinkInfo * linkInfo)197 static void AddVapInfo(const LaneLinkInfo *linkInfo)
198 {
199     LnnVapType vapType = GetVapType(linkInfo->type);
200     if (vapType == LNN_VAP_UNKNOWN) {
201         LNN_LOGE(LNN_LANE, "addVap fail, vapType unknown");
202         return;
203     }
204     LnnVapAttr vapAttr;
205     (void)memset_s(&vapAttr, sizeof(vapAttr), 0, sizeof(vapAttr));
206     vapAttr.isEnable = true;
207     vapAttr.channelId = linkInfo->linkInfo.p2p.channel;
208     int32_t ret = LnnAddLocalVapInfo(vapType, (const LnnVapAttr *)&vapAttr);
209     if (ret != SOFTBUS_OK) {
210         LNN_LOGE(LNN_LANE, "add vapInfo err, ret=%{public}d", ret);
211     }
212 }
213 
DeleteVapInfo(LaneLinkType linkType)214 static void DeleteVapInfo(LaneLinkType linkType)
215 {
216     LnnVapType vapType = GetVapType(linkType);
217     if (vapType == LNN_VAP_UNKNOWN) {
218         LNN_LOGE(LNN_LANE, "deleteVap fail, vapType unknown");
219         return;
220     }
221     int32_t ret = LnnDeleteLocalVapInfo(vapType);
222     if (ret != SOFTBUS_OK) {
223         LNN_LOGE(LNN_LANE, "delete vapInfo fail=%{public}d", linkType);
224     }
225 }
226 
GetRemoteMacAddrByLocalIp(const char * localIp,char * macAddr,uint32_t macAddrLen)227 static int32_t GetRemoteMacAddrByLocalIp(const char *localIp, char *macAddr, uint32_t macAddrLen)
228 {
229     struct WifiDirectManager *wifiDirectMgr = GetWifiDirectManager();
230     if (wifiDirectMgr == NULL) {
231         LNN_LOGE(LNN_LANE, "get wifi direct manager fail");
232         return SOFTBUS_NOT_FIND;
233     }
234     char localMac[LNN_MAC_LEN];
235     (void)memset_s(localMac, sizeof(localMac), 0, sizeof(localMac));
236     int32_t ret = wifiDirectMgr->getLocalAndRemoteMacByLocalIp(localIp, localMac, LNN_MAC_LEN,
237         macAddr, macAddrLen);
238     if (ret != SOFTBUS_OK) {
239         LNN_LOGE(LNN_LANE, "get macAddr by localIp fail, ret=%{public}d", ret);
240         return ret;
241     }
242     return SOFTBUS_OK;
243 }
244 
SetWifiDirectLinkInfo(P2pLinkInfo * p2pInfo,WifiDirectLinkInfo * wifiDirectInfo,uint32_t bandWidth)245 static void SetWifiDirectLinkInfo(P2pLinkInfo *p2pInfo, WifiDirectLinkInfo *wifiDirectInfo, uint32_t bandWidth)
246 {
247     wifiDirectInfo->linkType = LANE_HML;
248     wifiDirectInfo->bandWidth = bandWidth;
249     int32_t ret = GetRemoteMacAddrByLocalIp(p2pInfo->connInfo.localIp,
250         wifiDirectInfo->wifiDirectMac, LNN_MAC_LEN);
251     if (ret != SOFTBUS_OK) {
252         LNN_LOGE(LNN_LANE, "get hml macAddr fail=%{public}d", ret);
253         return;
254     }
255 }
256 
SetLanePowerStatus(bool status)257 static void SetLanePowerStatus(bool status)
258 {
259     if (LaneLock() != SOFTBUS_OK) {
260         LNN_LOGE(LNN_LANE, "lane lock fail");
261         return;
262     }
263     g_enabledLowPower = status;
264     LaneUnlock();
265 }
266 
HandleDetectWifiDirectApply(PowerControlInfo * powerInfo,WifiDirectLinkInfo * wifiDirectInfo)267 static void HandleDetectWifiDirectApply(PowerControlInfo *powerInfo,  WifiDirectLinkInfo *wifiDirectInfo)
268 {
269     if (powerInfo->isDisableLowPower) {
270         DisablePowerControl(wifiDirectInfo);
271         SetLanePowerStatus(false);
272     } else if ((powerInfo->activeHml == 1) && (powerInfo->passiveHml == 0) && (powerInfo->rawHml == 0)
273          && (!g_enabledLowPower)) {
274         int32_t ret = EnablePowerControl(wifiDirectInfo);
275         SetLanePowerStatus(true);
276         if (ret != SOFTBUS_OK) {
277             LNN_LOGE(LNN_LANE, "enable fail, ret=%{public}d", ret);
278             SetLanePowerStatus(false);
279         }
280     }
281 }
282 
DetectDisableWifiDirectApply(void)283 void DetectDisableWifiDirectApply(void)
284 {
285     PowerControlInfo powerInfo;
286     (void)memset_s(&powerInfo, sizeof(powerInfo), 0, sizeof(powerInfo));
287     WifiDirectLinkInfo wifiDirectInfo;
288     (void)memset_s(&wifiDirectInfo, sizeof(wifiDirectInfo), 0, sizeof(wifiDirectInfo));
289     if (LaneLock() != SOFTBUS_OK) {
290         LNN_LOGE(LNN_LANE, "lane lock fail");
291         return;
292     }
293     LaneResource *item = NULL;
294     LaneResource *next = NULL;
295     LIST_FOR_EACH_ENTRY_SAFE(item, next, &g_laneResource.list, LaneResource, node) {
296         LNN_LOGI(LNN_LANE, "link.type=%{public}d, link.bw=%{public}d", item->link.type, item->link.linkInfo.p2p.bw);
297         if (item->link.type == LANE_HML) {
298             if (item->clientRef > 0) {
299                 powerInfo.activeHml++;
300             }
301             if (item->isServerSide) {
302                 powerInfo.passiveHml++;
303                 SetWifiDirectLinkInfo(&item->link.linkInfo.p2p, &wifiDirectInfo, item->link.linkInfo.p2p.bw);
304             }
305             if (item->link.linkInfo.p2p.bw == LANE_BW_160M || item->link.linkInfo.p2p.bw == LANE_BW_80P80M) {
306                 SetWifiDirectLinkInfo(&item->link.linkInfo.p2p, &wifiDirectInfo, item->link.linkInfo.p2p.bw);
307             }
308         }
309         if (item->link.type == LANE_HML_RAW) {
310             powerInfo.rawHml++;
311         }
312     }
313     if (powerInfo.activeHml == 1) {
314         powerInfo.isDisableLowPower = true;
315     }
316     if (((powerInfo.activeHml == 0) || (powerInfo.passiveHml > 0) || (powerInfo.rawHml > 0)) && g_enabledLowPower) {
317         powerInfo.isDisableLowPower = true;
318     }
319     LaneUnlock();
320     LNN_LOGI(LNN_LANE, "activeHml=%{public}d, passiveHml=%{public}d, rawHml=%{public}d",
321         powerInfo.activeHml, powerInfo.passiveHml, powerInfo.rawHml);
322     HandleDetectWifiDirectApply(&powerInfo, &wifiDirectInfo);
323 }
324 
DetectEnableWifiDirectApply(void)325 static void DetectEnableWifiDirectApply(void)
326 {
327     PowerControlInfo powerInfo;
328     (void)memset_s(&powerInfo, sizeof(powerInfo), 0, sizeof(powerInfo));
329     WifiDirectLinkInfo wifiDirectInfo;
330     (void)memset_s(&wifiDirectInfo, sizeof(wifiDirectInfo), 0, sizeof(wifiDirectInfo));
331     if (LaneLock() != SOFTBUS_OK) {
332         LNN_LOGE(LNN_LANE, "lane lock fail");
333         return;
334     }
335     LaneResource *item = NULL;
336     LaneResource *next = NULL;
337     LIST_FOR_EACH_ENTRY_SAFE(item, next, &g_laneResource.list, LaneResource, node) {
338         LNN_LOGI(LNN_LANE, "link.type=%{public}d, link.bw=%{public}d", item->link.type, item->link.linkInfo.p2p.bw);
339         if (item->link.type == LANE_HML) {
340             if (item->clientRef > 0) {
341                 powerInfo.activeHml = item->clientRef;
342             }
343             if (item->isServerSide) {
344                 powerInfo.passiveHml++;
345                 SetWifiDirectLinkInfo(&item->link.linkInfo.p2p, &wifiDirectInfo, item->link.linkInfo.p2p.bw);
346             }
347             if (item->link.linkInfo.p2p.bw == LANE_BW_160M || item->link.linkInfo.p2p.bw == LANE_BW_80P80M) {
348                 SetWifiDirectLinkInfo(&item->link.linkInfo.p2p, &wifiDirectInfo, item->link.linkInfo.p2p.bw);
349             }
350         }
351         if (item->link.type == LANE_HML_RAW) {
352             powerInfo.rawHml++;
353         }
354     }
355     if (powerInfo.activeHml > 1) {
356         powerInfo.isDisableLowPower = true;
357     }
358     if (((powerInfo.activeHml == 0) || (powerInfo.passiveHml > 0) || (powerInfo.rawHml > 0)) && g_enabledLowPower) {
359         powerInfo.isDisableLowPower = true;
360     }
361     LaneUnlock();
362     LNN_LOGI(LNN_LANE, "activeHml=%{public}d, passiveHml=%{public}d, rawHml=%{public}d",
363         powerInfo.activeHml, powerInfo.passiveHml, powerInfo.rawHml);
364     HandleDetectWifiDirectApply(&powerInfo, &wifiDirectInfo);
365 }
366 
CreateNewLaneResource(const LaneLinkInfo * linkInfo,uint64_t laneId,bool isServerSide)367 static int32_t CreateNewLaneResource(const LaneLinkInfo *linkInfo, uint64_t laneId, bool isServerSide)
368 {
369     LaneResource* resourceItem = (LaneResource *)SoftBusCalloc(sizeof(LaneResource));
370     if (resourceItem == NULL) {
371         LNN_LOGE(LNN_LANE, "resourceItem malloc fail");
372         return SOFTBUS_MALLOC_ERR;
373     }
374     if (memcpy_s(&(resourceItem->link), sizeof(LaneLinkInfo), linkInfo, sizeof(LaneLinkInfo)) != EOK) {
375         LNN_LOGE(LNN_LANE, "link info convert to lane resource fail");
376         SoftBusFree(resourceItem);
377         return SOFTBUS_MEM_ERR;
378     }
379     resourceItem->laneId = laneId;
380     resourceItem->isServerSide = isServerSide;
381     resourceItem->clientRef = isServerSide ? 0 : 1;
382     if (LaneLock() != SOFTBUS_OK) {
383         LNN_LOGE(LNN_LANE, "lane lock fail");
384         SoftBusFree(resourceItem);
385         return SOFTBUS_LOCK_ERR;
386     }
387     ListAdd(&g_laneResource.list, &resourceItem->node);
388     g_laneResource.cnt++;
389     LaneUnlock();
390     LNN_LOGI(LNN_LANE, "create new laneId=%{public}" PRIu64 " to resource pool succ, isServerSide=%{public}u,"
391         "clientRef=%{public}u", resourceItem->laneId, isServerSide, resourceItem->clientRef);
392     AddVapInfo(linkInfo);
393     return SOFTBUS_OK;
394 }
395 
AddNetworkResourceInner(const LaneLinkInfo * linkInfo,uint64_t laneId)396 static void AddNetworkResourceInner(const LaneLinkInfo *linkInfo, uint64_t laneId)
397 {
398     if (linkInfo == NULL) {
399         LNN_LOGE(LNN_LANE, "invalid param");
400         return;
401     }
402     if (linkInfo->type != LANE_BR && linkInfo->type != LANE_BLE && linkInfo->type != LANE_P2P &&
403         linkInfo->type != LANE_HML) {
404         return;
405     }
406 
407     NetworkResource *networkResource = (NetworkResource *)SoftBusCalloc(sizeof(NetworkResource));
408     if (networkResource == NULL) {
409         LNN_LOGE(LNN_LANE, "malloc network resource fail");
410         return;
411     }
412     LnnGetLocalStrInfo(STRING_KEY_NETWORKID, networkResource->localUdid, NETWORK_ID_BUF_LEN);
413     networkResource->laneId = laneId;
414     networkResource->laneLinkType = linkInfo->type;
415     char *anonyLocalUdid = NULL;
416     char *anonyRemoteUdid = NULL;
417     Anonymize(networkResource->localUdid, &anonyLocalUdid);
418     Anonymize(linkInfo->peerUdid, &anonyRemoteUdid);
419     if (anonyLocalUdid == NULL || strcpy_s(networkResource->localUdid, NETWORK_ID_BUF_LEN, anonyLocalUdid) != EOK) {
420         LNN_LOGE(LNN_LANE, "strcpy localUdid fail");
421     }
422     if (anonyRemoteUdid == NULL || strcpy_s(networkResource->peerUdid, NETWORK_ID_BUF_LEN, anonyRemoteUdid) != EOK) {
423         LNN_LOGE(LNN_LANE, "strcpy peerUdid fail");
424     }
425     AddNetworkResource(networkResource);
426     SoftBusFree(networkResource);
427     AnonymizeFree(anonyLocalUdid);
428     AnonymizeFree(anonyRemoteUdid);
429 }
430 
UpdateExistLaneResource(LaneResource * resourceItem,bool isServerSide)431 static int32_t UpdateExistLaneResource(LaneResource *resourceItem, bool isServerSide)
432 {
433     if (isServerSide) {
434         if (resourceItem->isServerSide) {
435             LNN_LOGE(LNN_LANE, "server laneId=%{public}" PRIu64 " is existed, add server lane resource fail",
436             resourceItem->laneId);
437             return SOFTBUS_LANE_TRIGGER_LINK_FAIL;
438         }
439         resourceItem->isServerSide = true;
440         LNN_LOGI(LNN_LANE, "add server laneId=%{public}" PRIu64 " to resource pool succ",
441             resourceItem->laneId);
442         return SOFTBUS_OK;
443     }
444     resourceItem->clientRef++;
445     LNN_LOGI(LNN_LANE, "add client laneId=%{public}" PRIu64 " to resource pool succ, clientRef=%{public}u",
446         resourceItem->laneId, resourceItem->clientRef);
447     return SOFTBUS_OK;
448 }
449 
AddLaneResourceToPool(const LaneLinkInfo * linkInfo,uint64_t laneId,bool isServerSide)450 int32_t AddLaneResourceToPool(const LaneLinkInfo *linkInfo, uint64_t laneId, bool isServerSide)
451 {
452     if (linkInfo == NULL || laneId == INVALID_LANE_ID) {
453         LNN_LOGE(LNN_LANE, "linkInfo is nullptr or invalid laneId");
454         return SOFTBUS_INVALID_PARAM;
455     }
456     if (LaneLock() != SOFTBUS_OK) {
457         LNN_LOGE(LNN_LANE, "lane lock fail");
458         return SOFTBUS_LOCK_ERR;
459     }
460     int32_t addResult = SOFTBUS_LANE_RESOURCE_EXCEPT;
461     LaneResource* resourceItem = GetValidLaneResource(linkInfo);
462     if (resourceItem != NULL) {
463         addResult = UpdateExistLaneResource(resourceItem, isServerSide);
464         LaneUnlock();
465         return addResult;
466     }
467     LaneUnlock();
468     addResult = CreateNewLaneResource(linkInfo, laneId, isServerSide);
469     if (addResult != SOFTBUS_OK) {
470         LNN_LOGE(LNN_LANE, "create laneResource fail, result=%{public}d", addResult);
471         return addResult;
472     }
473     if (!isServerSide) {
474         AddNetworkResourceInner(linkInfo, laneId);
475     }
476     if (linkInfo->type == LANE_HML && IsPowerControlEnabled()) {
477         DetectEnableWifiDirectApply();
478     }
479     return SOFTBUS_OK;
480 }
481 
IsNeedDelResource(uint64_t laneId,bool isServerSide,LaneResource * item)482 static bool IsNeedDelResource(uint64_t laneId, bool isServerSide, LaneResource *item)
483 {
484     if (item->laneId != laneId) {
485         return false;
486     }
487     uint32_t ref = 0;
488     bool isServer = false;
489     if (isServerSide) {
490         ref = item->clientRef;
491         if (item->clientRef == 0) {
492             ListDelete(&item->node);
493             SoftBusFree(item);
494             if (g_laneResource.cnt != 0) {
495                 g_laneResource.cnt--;
496             }
497         } else {
498             item->isServerSide = false;
499         }
500     } else {
501         isServer = item->isServerSide;
502         ref = item->clientRef;
503         if (item->clientRef != 0) {
504             ref = --item->clientRef;
505         }
506         if (!isServer && ref == 0) {
507             DeleteNetworkResourceByLaneId(laneId);
508             ListDelete(&item->node);
509             SoftBusFree(item);
510             if (g_laneResource.cnt != 0) {
511                 g_laneResource.cnt--;
512             }
513         }
514     }
515     LNN_LOGI(LNN_LANE, "del laneId=%{public}" PRIu64 " resource, isServer=%{public}d, clientRef=%{public}u",
516         laneId, isServer, ref);
517     return true;
518 }
519 
DumpWifiDirectInfo(const LaneLinkInfo * resource)520 static void DumpWifiDirectInfo(const LaneLinkInfo *resource)
521 {
522     char *anonyPeerUdid = NULL;
523     Anonymize(resource->peerUdid, &anonyPeerUdid);
524     LNN_LOGI(LNN_LANE, "peerUdid=%{public}s, linkType=%{public}s, channel=%{public}d", AnonymizeWrapper(anonyPeerUdid),
525         resource->type == LANE_HML ? "hml" : "p2p", resource->linkInfo.p2p.channel);
526     AnonymizeFree(anonyPeerUdid);
527 }
528 
ProcessVapInfo(void)529 static void ProcessVapInfo(void)
530 {
531     bool hasHmlVap = false;
532     bool hasP2pVap = false;
533     if (LaneLock() != SOFTBUS_OK) {
534         LNN_LOGE(LNN_LANE, "lane lock fail");
535         return;
536     }
537     LaneResource *next = NULL;
538     LaneResource *item = NULL;
539     LIST_FOR_EACH_ENTRY_SAFE(item, next, &g_laneResource.list, LaneResource, node) {
540         if ((item->clientRef != 0 || item->isServerSide) && item->link.type == LANE_HML) {
541             hasHmlVap = true;
542             LNN_LOGI(LNN_LANE, "HML use: ref=%{public}d, server=%{public}d", item->clientRef, item->isServerSide);
543             DumpWifiDirectInfo((const LaneLinkInfo *)&item->link);
544         }
545         if (item->clientRef != 0 && item->link.type == LANE_P2P) {
546             hasP2pVap = true;
547             LNN_LOGI(LNN_LANE, "P2p use: ref=%{public}d", item->clientRef);
548             DumpWifiDirectInfo((const LaneLinkInfo *)&item->link);
549         }
550         if (hasHmlVap && hasP2pVap) {
551             break;
552         }
553     }
554     LaneUnlock();
555     if (!hasHmlVap) {
556         LNN_LOGI(LNN_LANE, "delete hml vap info");
557         DeleteVapInfo(LANE_HML);
558     }
559     if (!hasP2pVap) {
560         LNN_LOGI(LNN_LANE, "delete p2p vap info");
561         DeleteVapInfo(LANE_P2P);
562     }
563 }
564 
DelLaneResourceByLaneId(uint64_t laneId,bool isServerSide)565 int32_t DelLaneResourceByLaneId(uint64_t laneId, bool isServerSide)
566 {
567     if (LaneLock() != SOFTBUS_OK) {
568         LNN_LOGE(LNN_LANE, "lane lock fail");
569         return SOFTBUS_LOCK_ERR;
570     }
571     LNN_LOGI(LNN_LANE, "start to del laneId=%{public}" PRIu64 " resource, isServer=%{public}d",
572             laneId, isServerSide);
573     LaneResource *next = NULL;
574     LaneResource *item = NULL;
575     LIST_FOR_EACH_ENTRY_SAFE(item, next, &g_laneResource.list, LaneResource, node) {
576         if (IsNeedDelResource(laneId, isServerSide, item)) {
577             LaneUnlock();
578             ProcessVapInfo();
579             return SOFTBUS_OK;
580         }
581     }
582     LaneUnlock();
583     LNN_LOGI(LNN_LANE, "not found laneId=%{public}" PRIu64 " resource when del", laneId);
584     return SOFTBUS_LANE_RESOURCE_NOT_FOUND;
585 }
586 
ClearLaneResourceByLaneId(uint64_t laneId)587 int32_t ClearLaneResourceByLaneId(uint64_t laneId)
588 {
589     if (LaneLock() != SOFTBUS_OK) {
590         LNN_LOGE(LNN_LANE, "lane lock fail");
591         return SOFTBUS_LOCK_ERR;
592     }
593     LaneResource *next = NULL;
594     LaneResource *item = NULL;
595     LIST_FOR_EACH_ENTRY_SAFE(item, next, &g_laneResource.list, LaneResource, node) {
596         if (item->laneId == laneId) {
597             ListDelete(&item->node);
598             SoftBusFree(item);
599             g_laneResource.cnt--;
600             LaneUnlock();
601             LNN_LOGI(LNN_LANE, "clear laneId=%{public}" PRIu64 " resource succ", laneId);
602             ProcessVapInfo();
603             return SOFTBUS_OK;
604         }
605     }
606     LaneUnlock();
607     LNN_LOGI(LNN_LANE, "not found laneId=%{public}" PRIu64 " resource when clear", laneId);
608     return SOFTBUS_LANE_RESOURCE_NOT_FOUND;
609 }
610 
LinkTypeCheck(LaneLinkType type)611 static bool LinkTypeCheck(LaneLinkType type)
612 {
613     static const LaneLinkType supportList[] = { LANE_P2P, LANE_HML, LANE_WLAN_2P4G, LANE_WLAN_5G, LANE_BR, LANE_BLE,
614         LANE_BLE_DIRECT, LANE_P2P_REUSE, LANE_COC, LANE_COC_DIRECT, LANE_BLE_REUSE, LANE_HML_RAW };
615     uint32_t size = sizeof(supportList) / sizeof(LaneLinkType);
616     for (uint32_t i = 0; i < size; i++) {
617         if (supportList[i] == type) {
618             return true;
619         }
620     }
621     LNN_LOGE(LNN_LANE, "linkType not supported, linkType=%{public}d", type);
622     return false;
623 }
624 
IsLinkRequestValid(const LinkRequest * reqInfo)625 static int32_t IsLinkRequestValid(const LinkRequest *reqInfo)
626 {
627     if (reqInfo == NULL) {
628         LNN_LOGE(LNN_LANE, "reqInfo is nullptr");
629         return SOFTBUS_INVALID_PARAM;
630     }
631     return SOFTBUS_OK;
632 }
633 
FindLaneResourceByLinkType(const char * peerUdid,LaneLinkType type,LaneResource * resource)634 int32_t FindLaneResourceByLinkType(const char *peerUdid, LaneLinkType type, LaneResource *resource)
635 {
636     if (peerUdid == NULL || type >= LANE_LINK_TYPE_BUTT || resource == NULL) {
637         return SOFTBUS_INVALID_PARAM;
638     }
639     if (LaneLock() != SOFTBUS_OK) {
640         LNN_LOGE(LNN_LANE, "lane lock fail");
641         return SOFTBUS_LOCK_ERR;
642     }
643     LaneResource *item = NULL;
644     LaneResource *next = NULL;
645     LIST_FOR_EACH_ENTRY_SAFE(item, next, &g_laneResource.list, LaneResource, node) {
646         if (strcmp(peerUdid, item->link.peerUdid) == 0 && type == item->link.type) {
647             if (memcpy_s(resource, sizeof(LaneResource), item, sizeof(LaneResource)) != EOK) {
648                 LNN_LOGE(LNN_LANE, "memcpy lane resource fail");
649                 LaneUnlock();
650                 return SOFTBUS_MEM_ERR;
651             }
652             LaneUnlock();
653             return SOFTBUS_OK;
654         }
655     }
656     LaneUnlock();
657     char *anonyPeerUdid = NULL;
658     Anonymize(peerUdid, &anonyPeerUdid);
659     LNN_LOGE(LNN_LANE, "no find lane resource by linktype=%{public}d, peerUdid=%{public}s",
660         type, AnonymizeWrapper(anonyPeerUdid));
661     AnonymizeFree(anonyPeerUdid);
662     return SOFTBUS_LANE_RESOURCE_NOT_FOUND;
663 }
664 
FindLaneResourceByLinkAddr(const LaneLinkInfo * info,LaneResource * resource)665 int32_t FindLaneResourceByLinkAddr(const LaneLinkInfo *info, LaneResource *resource)
666 {
667     if (info == NULL || resource == NULL) {
668         return SOFTBUS_INVALID_PARAM;
669     }
670     if (LaneLock() != SOFTBUS_OK) {
671         LNN_LOGE(LNN_LANE, "lane lock fail");
672         return SOFTBUS_LOCK_ERR;
673     }
674     LaneResource* item = GetValidLaneResource(info);
675     if (item == NULL) {
676         LNN_LOGE(LNN_LANE, "no found lane resource by link info");
677         LaneUnlock();
678         return SOFTBUS_LANE_RESOURCE_NOT_FOUND;
679     }
680     if (memcpy_s(resource, sizeof(LaneResource), item, sizeof(LaneResource)) != EOK) {
681         LNN_LOGE(LNN_LANE, "memcpy lane resource fail");
682         LaneUnlock();
683         return SOFTBUS_MEM_ERR;
684     }
685     LaneUnlock();
686     return SOFTBUS_OK;
687 }
688 
FindLaneResourceByLaneId(uint64_t laneId,LaneResource * resource)689 int32_t FindLaneResourceByLaneId(uint64_t laneId, LaneResource *resource)
690 {
691     if (resource == NULL) {
692         return SOFTBUS_INVALID_PARAM;
693     }
694     if (LaneLock() != SOFTBUS_OK) {
695         LNN_LOGE(LNN_LANE, "lane lock fail");
696         return SOFTBUS_LOCK_ERR;
697     }
698     LaneResource *item = NULL;
699     LaneResource *next = NULL;
700     LIST_FOR_EACH_ENTRY_SAFE(item, next, &g_laneResource.list, LaneResource, node) {
701         if (item->laneId == laneId) {
702             if (memcpy_s(resource, sizeof(LaneResource), item, sizeof(LaneResource)) != EOK) {
703                 LNN_LOGE(LNN_LANE, "memcpy lane resource fail");
704                 LaneUnlock();
705                 return SOFTBUS_MEM_ERR;
706             }
707             LaneUnlock();
708             return SOFTBUS_OK;
709         }
710     }
711     LaneUnlock();
712     LNN_LOGE(LNN_LANE, "no found lane resource by laneId=%{public}" PRIu64 "", laneId);
713     return SOFTBUS_LANE_RESOURCE_NOT_FOUND;
714 }
715 
UpdateLaneResourceLaneId(uint64_t oldLaneId,uint64_t newLaneId,const char * peerUdid)716 int32_t UpdateLaneResourceLaneId(uint64_t oldLaneId, uint64_t newLaneId, const char *peerUdid)
717 {
718     if (oldLaneId == INVALID_LANE_ID || newLaneId == INVALID_LANE_ID || peerUdid == NULL) {
719         LNN_LOGE(LNN_LANE, "get invalid laneId");
720         return SOFTBUS_INVALID_PARAM;
721     }
722     if (LaneLock() != SOFTBUS_OK) {
723         LNN_LOGE(LNN_LANE, "lane lock fail");
724         return SOFTBUS_LOCK_ERR;
725     }
726     LaneResource *item = NULL;
727     LaneResource *next = NULL;
728     LIST_FOR_EACH_ENTRY_SAFE(item, next, &g_laneResource.list, LaneResource, node) {
729         if (item->laneId == oldLaneId) {
730             item->laneId = newLaneId;
731             if (strcpy_s(item->link.peerUdid, UDID_BUF_LEN, peerUdid) != EOK) {
732                 LNN_LOGE(LNN_LANE, "stcpy udid fail");
733                 LaneUnlock();
734                 return SOFTBUS_STRCPY_ERR;
735             }
736             LNN_LOGI(LNN_LANE, "find and refresh oldLaneId=%{public}" PRIu64 ", newLaneId=%{public}" PRIu64,
737                 oldLaneId, newLaneId);
738             LaneUnlock();
739             return SOFTBUS_OK;
740         }
741     }
742     LaneUnlock();
743     LNN_LOGE(LNN_LANE, "no found lane resource by laneId=%{public}" PRIu64 "", oldLaneId);
744     return SOFTBUS_NOT_FIND;
745 }
746 
CheckLaneResourceNumByLinkType(const char * peerUdid,LaneLinkType type,int32_t * laneNum)747 int32_t CheckLaneResourceNumByLinkType(const char *peerUdid, LaneLinkType type, int32_t *laneNum)
748 {
749     if (peerUdid == NULL || type >= LANE_LINK_TYPE_BUTT) {
750         return SOFTBUS_INVALID_PARAM;
751     }
752     uint32_t num = 0;
753     if (LaneLock() != SOFTBUS_OK) {
754         LNN_LOGE(LNN_LANE, "lane lock fail");
755         return SOFTBUS_LOCK_ERR;
756     }
757     LaneResource *item = NULL;
758     LaneResource *next = NULL;
759     LIST_FOR_EACH_ENTRY_SAFE(item, next, &g_laneResource.list, LaneResource, node) {
760         if (strcmp(peerUdid, item->link.peerUdid) == 0 && type == item->link.type) {
761             num++;
762             LaneUnlock();
763             *laneNum = num;
764             return SOFTBUS_OK;
765         }
766     }
767     LaneUnlock();
768     char *anonyPeerUdid = NULL;
769     Anonymize(peerUdid, &anonyPeerUdid);
770     LNN_LOGE(LNN_LANE, "no find lane resource by linktype=%{public}d, peerUdid=%{public}s",
771         type, AnonymizeWrapper(anonyPeerUdid));
772     AnonymizeFree(anonyPeerUdid);
773     return SOFTBUS_NOT_FIND;
774 }
775 
CopyAllDevIdWithoutLock(LaneLinkType type,uint8_t resourceNum,char ** devIdList,uint8_t * devIdCnt)776 static int32_t CopyAllDevIdWithoutLock(LaneLinkType type, uint8_t resourceNum, char **devIdList, uint8_t *devIdCnt)
777 {
778     char (*itemList)[NETWORK_ID_BUF_LEN] =
779         (char (*)[NETWORK_ID_BUF_LEN])SoftBusCalloc(resourceNum * NETWORK_ID_BUF_LEN);
780     if (itemList == NULL) {
781         LNN_LOGE(LNN_LANE, "device id list calloc fail");
782         return SOFTBUS_MALLOC_ERR;
783     }
784     char (*tmpList)[NETWORK_ID_BUF_LEN] = itemList;
785     char networkId[NETWORK_ID_BUF_LEN] = {0};
786     uint8_t tmpCnt = 0;
787     LaneResource *item = NULL;
788     LIST_FOR_EACH_ENTRY(item, &g_laneResource.list, LaneResource, node) {
789         if (item->link.type == type) {
790             if (LnnGetNetworkIdByUdid(item->link.peerUdid, networkId, NETWORK_ID_BUF_LEN) != SOFTBUS_OK) {
791                 LNN_LOGE(LNN_LANE, "get networkid fail");
792                 continue;
793             }
794             if (memcpy_s(*tmpList, NETWORK_ID_BUF_LEN, networkId, NETWORK_ID_BUF_LEN) != EOK) {
795                 LNN_LOGE(LNN_LANE, "memcpy networkid fail");
796                 continue;
797             }
798             char *anonyNetworkId = NULL;
799             Anonymize(networkId, &anonyNetworkId);
800             LNN_LOGI(LNN_LANE, "networkId=%{public}s exist link=%{public}d", AnonymizeWrapper(anonyNetworkId), type);
801             AnonymizeFree(anonyNetworkId);
802             tmpList += 1;
803             tmpCnt += 1;
804         }
805     }
806     if (tmpCnt == 0) {
807         SoftBusFree(itemList);
808         return SOFTBUS_LANE_GET_LEDGER_INFO_ERR;
809     }
810     *devIdList = (char *)itemList;
811     *devIdCnt = tmpCnt;
812     return SOFTBUS_OK;
813 }
814 
GetAllDevIdWithLinkType(LaneLinkType type,char ** devIdList,uint8_t * devIdCnt)815 int32_t GetAllDevIdWithLinkType(LaneLinkType type, char **devIdList, uint8_t *devIdCnt)
816 {
817     if (devIdList == NULL || devIdCnt == NULL || type == LANE_LINK_TYPE_BUTT) {
818         LNN_LOGE(LNN_LANE, "invalid parem");
819         return SOFTBUS_INVALID_PARAM;
820     }
821     if (LaneLock() != SOFTBUS_OK) {
822         LNN_LOGE(LNN_LANE, "lane lock fail");
823         return SOFTBUS_LOCK_ERR;
824     }
825     uint8_t resourceNum = 0;
826     LaneResource *item = NULL;
827     LIST_FOR_EACH_ENTRY(item, &g_laneResource.list, LaneResource, node) {
828         if (item->link.type == type) {
829             ++resourceNum;
830         }
831     }
832     if (resourceNum == 0) {
833         LaneUnlock();
834         LNN_LOGE(LNN_LANE, "lane resource no link=%{public}d", type);
835         return SOFTBUS_LANE_RESOURCE_NOT_FOUND;
836     }
837     LNN_LOGE(LNN_LANE, "lane resource exist link=%{public}d, num=%{public}u", type, resourceNum);
838     int32_t ret = CopyAllDevIdWithoutLock(type, resourceNum, devIdList, devIdCnt);
839     if (ret != SOFTBUS_OK) {
840         LNN_LOGE(LNN_LANE, "cpoy all device id fail");
841     }
842     LaneUnlock();
843     return ret;
844 }
845 
ConvertUdidToHexStr(const char * peerUdid,char * udidHashStr,uint32_t hashStrLen)846 static int32_t ConvertUdidToHexStr(const char *peerUdid, char *udidHashStr, uint32_t hashStrLen)
847 {
848     if (peerUdid == NULL || udidHashStr == NULL) {
849         LNN_LOGE(LNN_LANE, "invalid parem");
850         return SOFTBUS_INVALID_PARAM;
851     }
852     uint8_t peerUdidHash[UDID_HASH_LEN] = {0};
853     int32_t ret = SoftBusGenerateStrHash((const unsigned char*)peerUdid, strlen(peerUdid), peerUdidHash);
854     if (ret != SOFTBUS_OK) {
855         LNN_LOGE(LNN_LANE, "generate udidHash fail, ret=%{public}d", ret);
856         return ret;
857     }
858     ret = ConvertBytesToHexString(udidHashStr, hashStrLen, peerUdidHash, UDID_SHORT_HASH_LEN_TMP);
859     if (ret != SOFTBUS_OK) {
860         LNN_LOGE(LNN_LANE, "convert bytes to string fail, ret=%{public}d", ret);
861         return ret;
862     }
863     return SOFTBUS_OK;
864 }
865 
FetchLaneResourceByDevId(const char * peerNetworkId,LaneLinkType type,bool isSameDevice)866 static int32_t FetchLaneResourceByDevId(const char *peerNetworkId, LaneLinkType type, bool isSameDevice)
867 {
868     if (peerNetworkId == NULL) {
869         LNN_LOGE(LNN_LANE, "invalid param");
870         return SOFTBUS_INVALID_PARAM;
871     }
872     char peerUdid[UDID_BUF_LEN] = {0};
873     if (LnnGetRemoteStrInfo(peerNetworkId, STRING_KEY_DEV_UDID, peerUdid, UDID_BUF_LEN) != SOFTBUS_OK) {
874         char *anonyPeerNetworkId = NULL;
875         Anonymize(peerNetworkId, &anonyPeerNetworkId);
876         LNN_LOGI(LNN_LANE, "get peerUdid fail, peerNetworkId=%{public}s", AnonymizeWrapper(anonyPeerNetworkId));
877         AnonymizeFree(anonyPeerNetworkId);
878         return SOFTBUS_LANE_GET_LEDGER_INFO_ERR;
879     }
880     if (LaneLock() != SOFTBUS_OK) {
881         LNN_LOGE(LNN_LANE, "lane lock fail");
882         return SOFTBUS_LOCK_ERR;
883     }
884     LaneResource *item = NULL;
885     LaneResource *next = NULL;
886     LIST_FOR_EACH_ENTRY_SAFE(item, next, &g_laneResource.list, LaneResource, node) {
887         if (type != item->link.type) {
888             continue;
889         }
890         if ((!isSameDevice && strcmp(peerUdid, item->link.peerUdid) != 0) ||
891             (isSameDevice && strcmp(peerUdid, item->link.peerUdid) == 0)) {
892             LaneUnlock();
893             LNN_LOGI(LNN_LANE, "match expected laneLink by networkId, linkType=%{public}d, isSameDevice=%{public}d",
894                 type, isSameDevice);
895             return SOFTBUS_OK;
896         }
897     }
898     LaneUnlock();
899     LNN_LOGI(LNN_LANE, "not found lane resource, linkType=%{public}d, isSameDevice=%{public}d", type, isSameDevice);
900     return SOFTBUS_LANE_RESOURCE_NOT_FOUND;
901 }
902 
FetchLaneResourceByDevIdHash(const char * udidHashStr,LaneLinkType type,bool isSameDevice)903 static int32_t FetchLaneResourceByDevIdHash(const char *udidHashStr, LaneLinkType type, bool isSameDevice)
904 {
905     if (udidHashStr == NULL) {
906         LNN_LOGE(LNN_LANE, "invalid param");
907         return SOFTBUS_INVALID_PARAM;
908     }
909     if (LaneLock() != SOFTBUS_OK) {
910         LNN_LOGE(LNN_LANE, "lane lock fail");
911         return SOFTBUS_LOCK_ERR;
912     }
913     LaneResource *item = NULL;
914     LaneResource *next = NULL;
915     LIST_FOR_EACH_ENTRY_SAFE(item, next, &g_laneResource.list, LaneResource, node) {
916         if (type != item->link.type) {
917             continue;
918         }
919         char hashHexStr[UDID_SHORT_HASH_HEXSTR_LEN_TMP + 1] = {0};
920         if (ConvertUdidToHexStr(item->link.peerUdid, hashHexStr, UDID_SHORT_HASH_HEXSTR_LEN_TMP + 1) != SOFTBUS_OK) {
921             continue;
922         }
923         if ((!isSameDevice && strcmp(hashHexStr, udidHashStr) != 0) ||
924             (isSameDevice && strcmp(hashHexStr, udidHashStr) == 0)) {
925             LaneUnlock();
926             LNN_LOGI(LNN_LANE, "match expected laneLink by udidHashStr, linkType=%{public}d,"
927                 " isSameDevice=%{public}d", type, isSameDevice);
928             return SOFTBUS_OK;
929         }
930     }
931     LaneUnlock();
932     LNN_LOGI(LNN_LANE, "not found lane resource, linkType=%{public}d, isSameDevice=%{public}d", type, isSameDevice);
933     return SOFTBUS_LANE_RESOURCE_NOT_FOUND;
934 }
935 
QueryOtherLaneResource(const DevIdentifyInfo * inputInfo,LaneLinkType type)936 int32_t QueryOtherLaneResource(const DevIdentifyInfo *inputInfo, LaneLinkType type)
937 {
938     if (inputInfo == NULL || type >= LANE_LINK_TYPE_BUTT) {
939         LNN_LOGE(LNN_LANE, "invalid param");
940         return SOFTBUS_INVALID_PARAM;
941     }
942     if (inputInfo->type == IDENTIFY_TYPE_DEV_ID) {
943         return FetchLaneResourceByDevId(inputInfo->devInfo.peerDevId, type, false);
944     } else if (inputInfo->type == IDENTIFY_TYPE_UDID_HASH && strlen(inputInfo->devInfo.udidHash) > 0) {
945         return FetchLaneResourceByDevIdHash(inputInfo->devInfo.udidHash, type, false);
946     }
947     LNN_LOGE(LNN_LANE, "no fetch lane resource");
948     return SOFTBUS_LANE_RESOURCE_NOT_FOUND;
949 }
950 
FindLaneResourceByDevInfo(const DevIdentifyInfo * inputInfo,LaneLinkType type)951 bool FindLaneResourceByDevInfo(const DevIdentifyInfo *inputInfo, LaneLinkType type)
952 {
953     if (inputInfo == NULL || type >= LANE_LINK_TYPE_BUTT) {
954         LNN_LOGE(LNN_LANE, "invalid param");
955         return false;
956     }
957     int32_t ret = SOFTBUS_OK;
958     if (inputInfo->type == IDENTIFY_TYPE_DEV_ID) {
959         ret = FetchLaneResourceByDevId(inputInfo->devInfo.peerDevId, type, true);
960         if (ret != SOFTBUS_OK) {
961             return false;
962         }
963         return true;
964     } else if (inputInfo->type == IDENTIFY_TYPE_UDID_HASH && strlen(inputInfo->devInfo.udidHash) > 0) {
965         ret = FetchLaneResourceByDevIdHash(inputInfo->devInfo.udidHash, type, true);
966         if (ret != SOFTBUS_OK) {
967             return false;
968         }
969         return true;
970     }
971     LNN_LOGE(LNN_LANE, "no fetch lane resource");
972     return false;
973 }
974 
LaneLinkOfBr(uint32_t reqId,const LinkRequest * reqInfo,const LaneLinkCb * callback)975 static int32_t LaneLinkOfBr(uint32_t reqId, const LinkRequest *reqInfo, const LaneLinkCb *callback)
976 {
977     LaneLinkInfo linkInfo;
978     (void)memset_s(&linkInfo, sizeof(LaneLinkInfo), 0, sizeof(LaneLinkInfo));
979     if (LnnGetRemoteStrInfo(reqInfo->peerNetworkId, STRING_KEY_DEV_UDID,
980         linkInfo.peerUdid, UDID_BUF_LEN) != SOFTBUS_OK) {
981         LNN_LOGE(LNN_LANE, "get udid error");
982         return SOFTBUS_LANE_GET_LEDGER_INFO_ERR;
983     }
984     if (LnnGetRemoteStrInfo(reqInfo->peerNetworkId, STRING_KEY_BT_MAC, linkInfo.linkInfo.br.brMac,
985         BT_MAC_LEN) != SOFTBUS_OK || strlen(linkInfo.linkInfo.br.brMac) == 0) {
986         LNN_LOGE(LNN_LANE, "LnnGetRemoteStrInfo brmac is failed");
987         return SOFTBUS_LANE_GET_LEDGER_INFO_ERR;
988     }
989     linkInfo.type = LANE_BR;
990     callback->onLaneLinkSuccess(reqId, linkInfo.type, &linkInfo);
991     return SOFTBUS_OK;
992 }
993 
994 typedef struct P2pAddrNode {
995     ListNode node;
996     char networkId[NETWORK_ID_BUF_LEN];
997     char addr[MAX_SOCKET_ADDR_LEN];
998     uint16_t port;
999     uint16_t cnt;
1000 } P2pAddrNode;
1001 
1002 static SoftBusList g_P2pAddrList;
1003 
LaneInitP2pAddrList()1004 static void LaneInitP2pAddrList()
1005 {
1006     ListInit(&g_P2pAddrList.list);
1007     g_P2pAddrList.cnt = 0;
1008     SoftBusMutexInit(&g_P2pAddrList.lock, NULL);
1009 }
1010 
LaneDeinitP2pAddrList(void)1011 static void LaneDeinitP2pAddrList(void)
1012 {
1013     if (SoftBusMutexLock(&g_P2pAddrList.lock) != SOFTBUS_OK) {
1014         LNN_LOGE(LNN_LANE, "SoftBusMutexLock fail");
1015         return;
1016     }
1017     P2pAddrNode *item = NULL;
1018     P2pAddrNode *nextItem = NULL;
1019     LIST_FOR_EACH_ENTRY_SAFE(item, nextItem, &g_P2pAddrList.list, P2pAddrNode, node) {
1020         ListDelete(&item->node);
1021         SoftBusFree(item);
1022     }
1023     g_P2pAddrList.cnt = 0;
1024     SoftBusMutexUnlock(&g_P2pAddrList.lock);
1025     (void)SoftBusMutexDestroy(&g_P2pAddrList.lock);
1026 }
1027 
LaneDeleteP2pAddress(const char * networkId,bool isDestroy)1028 void LaneDeleteP2pAddress(const char *networkId, bool isDestroy)
1029 {
1030     P2pAddrNode *item = NULL;
1031     P2pAddrNode *nextItem = NULL;
1032 
1033     if (networkId == NULL) {
1034         LNN_LOGE(LNN_LANE, "networkId invalid");
1035         return;
1036     }
1037     if (SoftBusMutexLock(&g_P2pAddrList.lock) != SOFTBUS_OK) {
1038         LNN_LOGE(LNN_LANE, "SoftBusMutexLock fail");
1039         return;
1040     }
1041     LIST_FOR_EACH_ENTRY_SAFE(item, nextItem, &g_P2pAddrList.list, P2pAddrNode, node) {
1042         if (strcmp(item->networkId, networkId) == 0) {
1043             if (isDestroy || (--item->cnt) == 0) {
1044                 ListDelete(&item->node);
1045                 SoftBusFree(item);
1046             }
1047         }
1048     }
1049     SoftBusMutexUnlock(&g_P2pAddrList.lock);
1050 }
1051 
LaneAddP2pAddress(const char * networkId,const char * ipAddr,uint16_t port)1052 void LaneAddP2pAddress(const char *networkId, const char *ipAddr, uint16_t port)
1053 {
1054     if (networkId == NULL || ipAddr == NULL) {
1055         LNN_LOGE(LNN_LANE, "invalid parameter");
1056         return;
1057     }
1058     P2pAddrNode *item = NULL;
1059     P2pAddrNode *nextItem = NULL;
1060     bool find = false;
1061 
1062     if (SoftBusMutexLock(&g_P2pAddrList.lock) != SOFTBUS_OK) {
1063         LNN_LOGE(LNN_LANE, "SoftBusMutexLock fail");
1064         return;
1065     }
1066 
1067     LIST_FOR_EACH_ENTRY_SAFE(item, nextItem, &g_P2pAddrList.list, P2pAddrNode, node) {
1068         if (strcmp(item->networkId, networkId) == 0) {
1069             find = true;
1070             break;
1071         }
1072     }
1073     if (find) {
1074         if (strcpy_s(item->addr, MAX_SOCKET_ADDR_LEN, ipAddr) != EOK) {
1075             SoftBusMutexUnlock(&g_P2pAddrList.lock);
1076             return;
1077         }
1078         item->port = port;
1079         item->cnt++;
1080     } else {
1081         P2pAddrNode *p2pAddrNode = (P2pAddrNode *)SoftBusMalloc(sizeof(P2pAddrNode));
1082         if (p2pAddrNode == NULL) {
1083             SoftBusMutexUnlock(&g_P2pAddrList.lock);
1084             return;
1085         }
1086         if (strcpy_s(p2pAddrNode->networkId, NETWORK_ID_BUF_LEN, networkId) != EOK) {
1087             SoftBusMutexUnlock(&g_P2pAddrList.lock);
1088             SoftBusFree(p2pAddrNode);
1089             return;
1090         }
1091         if (strcpy_s(p2pAddrNode->addr, MAX_SOCKET_ADDR_LEN, ipAddr) != EOK) {
1092             SoftBusMutexUnlock(&g_P2pAddrList.lock);
1093             SoftBusFree(p2pAddrNode);
1094             return;
1095         }
1096         p2pAddrNode->port = port;
1097         p2pAddrNode->cnt--;
1098         ListAdd(&g_P2pAddrList.list, &p2pAddrNode->node);
1099     }
1100 
1101     SoftBusMutexUnlock(&g_P2pAddrList.lock);
1102 }
1103 
LaneAddP2pAddressByIp(const char * ipAddr,uint16_t port)1104 void LaneAddP2pAddressByIp(const char *ipAddr, uint16_t port)
1105 {
1106     if (ipAddr == NULL) {
1107         return;
1108     }
1109     P2pAddrNode *item = NULL;
1110     P2pAddrNode *nextItem = NULL;
1111     bool find = false;
1112 
1113     if (SoftBusMutexLock(&g_P2pAddrList.lock) != SOFTBUS_OK) {
1114         return;
1115     }
1116 
1117     LIST_FOR_EACH_ENTRY_SAFE(item, nextItem, &g_P2pAddrList.list, P2pAddrNode, node) {
1118         if (strcmp(item->addr, ipAddr) == 0) {
1119             find = true;
1120             break;
1121         }
1122     }
1123     if (find) {
1124         item->port = port;
1125         item->cnt++;
1126     } else {
1127         P2pAddrNode *p2pAddrNode = (P2pAddrNode *)SoftBusMalloc(sizeof(P2pAddrNode));
1128         if (p2pAddrNode == NULL) {
1129             SoftBusMutexUnlock(&g_P2pAddrList.lock);
1130             return;
1131         }
1132         if (strcpy_s(p2pAddrNode->addr, MAX_SOCKET_ADDR_LEN, ipAddr) != EOK) {
1133             SoftBusMutexUnlock(&g_P2pAddrList.lock);
1134             SoftBusFree(p2pAddrNode);
1135             return;
1136         }
1137         p2pAddrNode->networkId[0] = 0;
1138         p2pAddrNode->port = port;
1139         p2pAddrNode->cnt--;
1140         ListAdd(&g_P2pAddrList.list, &p2pAddrNode->node);
1141     }
1142 
1143     SoftBusMutexUnlock(&g_P2pAddrList.lock);
1144 }
1145 
LaneUpdateP2pAddressByIp(const char * ipAddr,const char * networkId)1146 void LaneUpdateP2pAddressByIp(const char *ipAddr, const char *networkId)
1147 {
1148     if (ipAddr == NULL || networkId == NULL) {
1149         LNN_LOGE(LNN_LANE, "invalid parameter");
1150         return;
1151     }
1152     P2pAddrNode *item = NULL;
1153     P2pAddrNode *nextItem = NULL;
1154 
1155     if (SoftBusMutexLock(&g_P2pAddrList.lock) != SOFTBUS_OK) {
1156         return;
1157     }
1158 
1159     LIST_FOR_EACH_ENTRY_SAFE(item, nextItem, &g_P2pAddrList.list, P2pAddrNode, node) {
1160         if (strcmp(item->addr, ipAddr) == 0) {
1161             if (strcpy_s(item->networkId, NETWORK_ID_BUF_LEN, networkId) != EOK) {
1162                 SoftBusMutexUnlock(&g_P2pAddrList.lock);
1163                 return;
1164             }
1165         }
1166     }
1167     SoftBusMutexUnlock(&g_P2pAddrList.lock);
1168 }
1169 
LaneGetP2PReuseMac(const char * networkId,char * ipAddr,uint32_t maxLen,uint16_t * port)1170 static bool LaneGetP2PReuseMac(const char *networkId, char *ipAddr, uint32_t maxLen, uint16_t *port)
1171 {
1172     P2pAddrNode *item = NULL;
1173     P2pAddrNode *nextItem = NULL;
1174     if (SoftBusMutexLock(&g_P2pAddrList.lock) != SOFTBUS_OK) {
1175         return false;
1176     }
1177     LIST_FOR_EACH_ENTRY_SAFE(item, nextItem, &g_P2pAddrList.list, P2pAddrNode, node) {
1178         if (strcmp(item->networkId, networkId) == 0) {
1179             if (strcpy_s(ipAddr, maxLen, item->addr) != EOK) {
1180                 SoftBusMutexUnlock(&g_P2pAddrList.lock);
1181                 return false;
1182             }
1183             *port = item->port;
1184             SoftBusMutexUnlock(&g_P2pAddrList.lock);
1185             return true;
1186         }
1187     }
1188     SoftBusMutexUnlock(&g_P2pAddrList.lock);
1189     return false;
1190 }
1191 
LaneLinkOfBleReuseCommon(uint32_t reqId,const LinkRequest * reqInfo,const LaneLinkCb * callback,BleProtocolType type)1192 static int32_t LaneLinkOfBleReuseCommon(uint32_t reqId, const LinkRequest *reqInfo, const LaneLinkCb *callback,
1193     BleProtocolType type)
1194 {
1195     const char *udid = LnnConvertDLidToUdid(reqInfo->peerNetworkId, CATEGORY_NETWORK_ID);
1196     ConnBleConnection *connection = ConnBleGetConnectionByUdid(NULL, udid, type);
1197     if ((connection == NULL) || (connection->state != BLE_CONNECTION_STATE_EXCHANGED_BASIC_INFO)) {
1198         return SOFTBUS_INVALID_PARAM;
1199     }
1200     LaneLinkInfo linkInfo;
1201     (void)memset_s(&linkInfo, sizeof(LaneLinkInfo), 0, sizeof(LaneLinkInfo));
1202     if (LnnGetRemoteStrInfo(reqInfo->peerNetworkId, STRING_KEY_DEV_UDID,
1203         linkInfo.peerUdid, UDID_BUF_LEN) != SOFTBUS_OK) {
1204         LNN_LOGE(LNN_LANE, "get udid error");
1205         return SOFTBUS_LANE_GET_LEDGER_INFO_ERR;
1206     }
1207     (void)memcpy_s(linkInfo.linkInfo.ble.bleMac, BT_MAC_LEN, connection->addr, BT_MAC_LEN);
1208     int32_t ret = SoftBusGenerateStrHash((uint8_t*)connection->udid, strlen(connection->udid),
1209         (uint8_t*)linkInfo.linkInfo.ble.deviceIdHash);
1210     if (ret != SOFTBUS_OK) {
1211         LNN_LOGE(LNN_LANE, "generate deviceId hash err");
1212         ConnBleReturnConnection(&connection);
1213         return ret;
1214     }
1215     linkInfo.linkInfo.ble.protoType = type;
1216     if (type == BLE_COC) {
1217         linkInfo.type = LANE_COC;
1218         linkInfo.linkInfo.ble.psm = (int32_t)connection->psm;
1219     } else if (type == BLE_GATT) {
1220         linkInfo.type = LANE_BLE;
1221     }
1222     ConnBleReturnConnection(&connection);
1223     callback->onLaneLinkSuccess(reqId, linkInfo.type, &linkInfo);
1224     return SOFTBUS_OK;
1225 }
1226 
LaneLinkOfBleReuse(uint32_t reqId,const LinkRequest * reqInfo,const LaneLinkCb * callback)1227 static int32_t LaneLinkOfBleReuse(uint32_t reqId, const LinkRequest *reqInfo, const LaneLinkCb *callback)
1228 {
1229     return LaneLinkOfBleReuseCommon(reqId, reqInfo, callback, BLE_GATT);
1230 }
1231 
LaneLinkSetBleMac(const LinkRequest * reqInfo,LaneLinkInfo * linkInfo)1232 static int32_t LaneLinkSetBleMac(const LinkRequest *reqInfo, LaneLinkInfo *linkInfo)
1233 {
1234     NodeInfo node;
1235     (void)memset_s(&node, sizeof(NodeInfo), 0, sizeof(NodeInfo));
1236     if (LnnGetRemoteNodeInfoById(reqInfo->peerNetworkId, CATEGORY_NETWORK_ID, &node) != SOFTBUS_OK) {
1237         LNN_LOGE(LNN_LANE, "can not find node");
1238         return SOFTBUS_LANE_GET_LEDGER_INFO_ERR;
1239     }
1240     if (node.bleMacRefreshSwitch == 0 && strlen(node.connectInfo.bleMacAddr) > 0) {
1241         if (strcpy_s(linkInfo->linkInfo.ble.bleMac, BT_MAC_LEN, node.connectInfo.bleMacAddr) == EOK) {
1242             return SOFTBUS_OK;
1243         }
1244     }
1245     return SOFTBUS_MEM_ERR;
1246 }
1247 
LaneLinkOfBle(uint32_t reqId,const LinkRequest * reqInfo,const LaneLinkCb * callback)1248 static int32_t LaneLinkOfBle(uint32_t reqId, const LinkRequest *reqInfo, const LaneLinkCb *callback)
1249 {
1250     LaneLinkInfo linkInfo;
1251     (void)memset_s(&linkInfo, sizeof(LaneLinkInfo), 0, sizeof(LaneLinkInfo));
1252     if (LnnGetRemoteStrInfo(reqInfo->peerNetworkId, STRING_KEY_DEV_UDID,
1253         linkInfo.peerUdid, UDID_BUF_LEN) != SOFTBUS_OK) {
1254         LNN_LOGE(LNN_LANE, "get udid error");
1255         return SOFTBUS_LANE_GET_LEDGER_INFO_ERR;
1256     }
1257     if (memcpy_s(linkInfo.linkInfo.ble.bleMac, BT_MAC_LEN, reqInfo->peerBleMac, BT_MAC_LEN) != EOK) {
1258         LNN_LOGE(LNN_LANE, "memcpy peerBleMac error");
1259         return SOFTBUS_MEM_ERR;
1260     }
1261     if (strlen(linkInfo.linkInfo.ble.bleMac) == 0) {
1262         if (LaneLinkSetBleMac(reqInfo, &linkInfo) != SOFTBUS_OK) {
1263             LNN_LOGE(LNN_LANE, "get peerBleMac error");
1264             return SOFTBUS_INVALID_PARAM;
1265         }
1266     }
1267     char peerUdid[UDID_BUF_LEN] = {0};
1268     if (LnnGetRemoteStrInfo(reqInfo->peerNetworkId, STRING_KEY_DEV_UDID, peerUdid, UDID_BUF_LEN) != SOFTBUS_OK) {
1269         LNN_LOGE(LNN_LANE, "get udid error");
1270         return SOFTBUS_LANE_GET_LEDGER_INFO_ERR;
1271     }
1272     int32_t ret = SoftBusGenerateStrHash((uint8_t*)peerUdid, strlen(peerUdid),
1273         (uint8_t*)linkInfo.linkInfo.ble.deviceIdHash);
1274     if (ret != SOFTBUS_OK) {
1275         LNN_LOGE(LNN_LANE, "generate deviceId hash err");
1276         return ret;
1277     }
1278     linkInfo.linkInfo.ble.protoType = BLE_GATT;
1279     linkInfo.linkInfo.ble.psm = 0;
1280     linkInfo.type = LANE_BLE;
1281     callback->onLaneLinkSuccess(reqId, linkInfo.type, &linkInfo);
1282     return SOFTBUS_OK;
1283 }
1284 
LaneLinkOfGattDirect(uint32_t reqId,const LinkRequest * reqInfo,const LaneLinkCb * callback)1285 static int32_t LaneLinkOfGattDirect(uint32_t reqId, const LinkRequest *reqInfo, const LaneLinkCb *callback)
1286 {
1287     LaneLinkInfo linkInfo;
1288     (void)memset_s(&linkInfo, sizeof(LaneLinkInfo), 0, sizeof(LaneLinkInfo));
1289     if (LnnGetRemoteStrInfo(reqInfo->peerNetworkId, STRING_KEY_DEV_UDID,
1290         linkInfo.peerUdid, UDID_BUF_LEN) != SOFTBUS_OK) {
1291         LNN_LOGE(LNN_LANE, "get udid error");
1292         return SOFTBUS_LANE_GET_LEDGER_INFO_ERR;
1293     }
1294     if (strcpy_s(linkInfo.linkInfo.bleDirect.networkId, NETWORK_ID_BUF_LEN, reqInfo->peerNetworkId) != EOK) {
1295         LNN_LOGE(LNN_LANE, "copy networkId fail");
1296         return SOFTBUS_STRCPY_ERR;
1297     }
1298     linkInfo.type = LANE_BLE_DIRECT;
1299     linkInfo.linkInfo.bleDirect.protoType = BLE_GATT;
1300     callback->onLaneLinkSuccess(reqId, linkInfo.type, &linkInfo);
1301     return SOFTBUS_OK;
1302 }
1303 
LaneLinkOfP2p(uint32_t reqId,const LinkRequest * reqInfo,const LaneLinkCb * callback)1304 static int32_t LaneLinkOfP2p(uint32_t reqId, const LinkRequest *reqInfo, const LaneLinkCb *callback)
1305 {
1306     LinkRequest linkInfo;
1307     if (memcpy_s(&linkInfo, sizeof(LinkRequest), reqInfo, sizeof(LinkRequest)) != EOK) {
1308         LNN_LOGE(LNN_LANE, "p2p copy linkreqinfo fail");
1309         return SOFTBUS_MEM_ERR;
1310     }
1311     linkInfo.linkType = LANE_P2P;
1312     return LnnConnectP2p(&linkInfo, reqId, callback);
1313 }
1314 
LaneLinkOfHml(uint32_t reqId,const LinkRequest * reqInfo,const LaneLinkCb * callback)1315 static int32_t LaneLinkOfHml(uint32_t reqId, const LinkRequest *reqInfo, const LaneLinkCb *callback)
1316 {
1317     LinkRequest linkInfo;
1318     if (memcpy_s(&linkInfo, sizeof(LinkRequest), reqInfo, sizeof(LinkRequest)) != EOK) {
1319         LNN_LOGE(LNN_LANE, "hml copy linkreqinfo fail");
1320         return SOFTBUS_MEM_ERR;
1321     }
1322     linkInfo.linkType = LANE_HML;
1323     return LnnConnectP2p(&linkInfo, reqId, callback);
1324 }
1325 
LaneLinkOfHmlRaw(uint32_t reqId,const LinkRequest * reqInfo,const LaneLinkCb * callback)1326 static int32_t LaneLinkOfHmlRaw(uint32_t reqId, const LinkRequest *reqInfo, const LaneLinkCb *callback)
1327 {
1328     LinkRequest linkInfo;
1329     if (memcpy_s(&linkInfo, sizeof(LinkRequest), reqInfo, sizeof(LinkRequest)) != EOK) {
1330         LNN_LOGE(LNN_LANE, "hml copy linkreqinfo fail");
1331         return SOFTBUS_MEM_ERR;
1332     }
1333     linkInfo.linkType = LANE_HML_RAW;
1334     return LnnConnectP2p(&linkInfo, reqId, callback);
1335 }
1336 
LaneLinkOfP2pReuse(uint32_t reqId,const LinkRequest * reqInfo,const LaneLinkCb * callback)1337 static int32_t LaneLinkOfP2pReuse(uint32_t reqId, const LinkRequest *reqInfo, const LaneLinkCb *callback)
1338 {
1339     LaneLinkInfo linkInfo;
1340     if (LnnGetRemoteStrInfo(reqInfo->peerNetworkId, STRING_KEY_DEV_UDID,
1341         linkInfo.peerUdid, UDID_BUF_LEN) != SOFTBUS_OK) {
1342         LNN_LOGE(LNN_LANE, "get udid error");
1343         return SOFTBUS_LANE_GET_LEDGER_INFO_ERR;
1344     }
1345     linkInfo.type = LANE_P2P_REUSE;
1346     char ipAddr[MAX_SOCKET_ADDR_LEN];
1347     uint16_t port;
1348     if (!LaneGetP2PReuseMac(reqInfo->peerNetworkId, ipAddr, MAX_SOCKET_ADDR_LEN, &port)) {
1349         LNN_LOGE(LNN_LANE, "p2p resue get addr failed");
1350         return SOFTBUS_LANE_NOT_FOUND;
1351     }
1352     linkInfo.linkInfo.wlan.connInfo.protocol = LNN_PROTOCOL_IP;
1353     linkInfo.linkInfo.wlan.connInfo.port = port;
1354     if (memcpy_s(linkInfo.linkInfo.wlan.connInfo.addr, MAX_SOCKET_ADDR_LEN, ipAddr, MAX_SOCKET_ADDR_LEN) != EOK) {
1355         return SOFTBUS_MEM_ERR;
1356     }
1357     callback->onLaneLinkSuccess(reqId, linkInfo.type, &linkInfo);
1358     return SOFTBUS_OK;
1359 }
1360 
GetWlanLinkedAttribute(int32_t * channel,bool * is5GBand,bool * isConnected)1361 static int32_t GetWlanLinkedAttribute(int32_t *channel, bool *is5GBand, bool *isConnected)
1362 {
1363     LnnWlanLinkedInfo info;
1364     int32_t ret = LnnGetWlanLinkedInfo(&info);
1365     if (ret != SOFTBUS_OK) {
1366         LNN_LOGE(LNN_LANE, "LnnGetWlanLinkedInfo fail, ret=%{public}d", ret);
1367         return ret;
1368     }
1369     *isConnected = info.isConnected;
1370     *is5GBand = (info.band != 1);
1371 
1372     *channel = SoftBusFrequencyToChannel(info.frequency);
1373     LNN_LOGI(LNN_LANE, "wlan current channel=%{public}d", *channel);
1374     return SOFTBUS_OK;
1375 }
1376 
1377 struct SelectProtocolReq {
1378     LnnNetIfType localIfType;
1379     ProtocolType selectedProtocol;
1380     ProtocolType remoteSupporttedProtocol;
1381     uint8_t currPri;
1382 };
1383 
FindBestProtocol(const LnnPhysicalSubnet * subnet,void * priv)1384 VisitNextChoice FindBestProtocol(const LnnPhysicalSubnet *subnet, void *priv)
1385 {
1386     if (subnet == NULL || priv == NULL || subnet->protocol == NULL) {
1387         return CHOICE_FINISH_VISITING;
1388     }
1389     struct SelectProtocolReq *req = (struct SelectProtocolReq *)priv;
1390     if (subnet->status == LNN_SUBNET_RUNNING && (subnet->protocol->supportedNetif & req->localIfType) != 0 &&
1391         subnet->protocol->pri > req->currPri && (subnet->protocol->id & req->remoteSupporttedProtocol) != 0) {
1392         req->currPri = subnet->protocol->pri;
1393         req->selectedProtocol = subnet->protocol->id;
1394     }
1395 
1396     return CHOICE_VISIT_NEXT;
1397 }
1398 
LnnLaneSelectProtocol(LnnNetIfType ifType,const char * netWorkId,ProtocolType acceptableProtocols)1399 static ProtocolType LnnLaneSelectProtocol(LnnNetIfType ifType, const char *netWorkId, ProtocolType acceptableProtocols)
1400 {
1401     NodeInfo remoteNodeInfo;
1402     (void)memset_s(&remoteNodeInfo, sizeof(NodeInfo), 0, sizeof(NodeInfo));
1403     int ret = LnnGetRemoteNodeInfoById(netWorkId, CATEGORY_NETWORK_ID, &remoteNodeInfo);
1404     if (ret != SOFTBUS_OK) {
1405         LNN_LOGE(LNN_LANE, "no such network id");
1406         return SOFTBUS_LANE_GET_LEDGER_INFO_ERR;
1407     }
1408 
1409     const NodeInfo *localNode = LnnGetLocalNodeInfo();
1410     if (localNode == NULL) {
1411         LNN_LOGE(LNN_LANE, "get local node info failed!");
1412         return SOFTBUS_LANE_GET_LEDGER_INFO_ERR;
1413     }
1414 
1415     struct SelectProtocolReq req = {
1416         .localIfType = ifType,
1417         .remoteSupporttedProtocol = remoteNodeInfo.supportedProtocols & acceptableProtocols,
1418         .selectedProtocol = 0,
1419         .currPri = 0,
1420     };
1421 
1422     if ((req.remoteSupporttedProtocol & LNN_PROTOCOL_NIP) != 0 &&
1423         (strcmp(remoteNodeInfo.nodeAddress, NODE_ADDR_LOOPBACK) == 0 ||
1424             strcmp(localNode->nodeAddress, NODE_ADDR_LOOPBACK) == 0)) {
1425         LNN_LOGW(LNN_LANE, "newip temporarily unavailable!");
1426         req.remoteSupporttedProtocol ^= LNN_PROTOCOL_NIP;
1427     }
1428 
1429     (void)LnnVisitPhysicalSubnet(FindBestProtocol, &req);
1430     char *anonyNetworkId = NULL;
1431     Anonymize(netWorkId, &anonyNetworkId);
1432     LNN_LOGI(LNN_LANE, "networkId=%{public}s select protocol=%{public}d, pri=%{public}u",
1433         AnonymizeWrapper(anonyNetworkId), req.selectedProtocol, req.currPri);
1434     AnonymizeFree(anonyNetworkId);
1435     if (req.selectedProtocol == 0) {
1436         req.selectedProtocol = LNN_PROTOCOL_IP;
1437     }
1438 
1439     return req.selectedProtocol;
1440 }
1441 
FillWlanLinkInfo(ProtocolType protocol,const LinkRequest * reqInfo,LaneLinkInfo * linkInfo)1442 static int32_t FillWlanLinkInfo(ProtocolType protocol, const LinkRequest *reqInfo, LaneLinkInfo *linkInfo)
1443 {
1444     int32_t ret = SOFTBUS_LANE_GET_LEDGER_INFO_ERR;
1445     int32_t port = 0;
1446     if (reqInfo->transType == LANE_T_MSG) {
1447         ret = LnnGetRemoteNumInfo(reqInfo->peerNetworkId, NUM_KEY_PROXY_PORT, &port);
1448         LNN_LOGI(LNN_LANE, "get remote proxy port, port=%{public}d, ret=%{public}d", port, ret);
1449     } else {
1450         ret = LnnGetRemoteNumInfo(reqInfo->peerNetworkId, NUM_KEY_SESSION_PORT, &port);
1451         LNN_LOGI(LNN_LANE, "get remote session port, port=%{public}d, ret=%{public}d", port, ret);
1452     }
1453     if (ret != SOFTBUS_OK) {
1454         return ret;
1455     }
1456     int32_t channel = -1;
1457     bool is5GBand = false;
1458     bool isConnected = false;
1459     if (GetWlanLinkedAttribute(&channel, &is5GBand, &isConnected) != SOFTBUS_OK) {
1460         LNN_LOGE(LNN_LANE, "get wlan attr info fail");
1461     }
1462     if (!isConnected) {
1463         LNN_LOGE(LNN_LANE, "wlan is disconnected");
1464     }
1465     linkInfo->type = reqInfo->linkType;
1466     WlanLinkInfo *wlan = &(linkInfo->linkInfo.wlan);
1467     wlan->channel = channel;
1468     wlan->bw = LANE_BW_RANDOM;
1469     wlan->connInfo.protocol = protocol;
1470     wlan->connInfo.port = port;
1471     return SOFTBUS_OK;
1472 }
1473 
CreateWlanLinkInfo(ProtocolType protocol,const LinkRequest * reqInfo,LaneLinkInfo * linkInfo)1474 static int32_t CreateWlanLinkInfo(ProtocolType protocol, const LinkRequest *reqInfo, LaneLinkInfo *linkInfo)
1475 {
1476     if (LnnGetRemoteStrInfo(reqInfo->peerNetworkId, STRING_KEY_DEV_UDID,
1477         linkInfo->peerUdid, UDID_BUF_LEN) != SOFTBUS_OK) {
1478         LNN_LOGE(LNN_LANE, "get udid error");
1479         return SOFTBUS_LANE_GET_LEDGER_INFO_ERR;
1480     }
1481     LNN_LOGI(LNN_LANE, "get remote wlan ip with protocol=%{public}u", protocol);
1482     if (protocol == LNN_PROTOCOL_IP) {
1483         if (LnnGetRemoteStrInfo(reqInfo->peerNetworkId, STRING_KEY_WLAN_IP, linkInfo->linkInfo.wlan.connInfo.addr,
1484             sizeof(linkInfo->linkInfo.wlan.connInfo.addr)) != SOFTBUS_OK) {
1485             LNN_LOGE(LNN_LANE, "get remote wlan ip fail");
1486             return SOFTBUS_LANE_GET_LEDGER_INFO_ERR;
1487         }
1488         if (strnlen(linkInfo->linkInfo.wlan.connInfo.addr, sizeof(linkInfo->linkInfo.wlan.connInfo.addr)) == 0 ||
1489             strnlen(linkInfo->linkInfo.wlan.connInfo.addr,
1490             sizeof(linkInfo->linkInfo.wlan.connInfo.addr)) == MAX_SOCKET_ADDR_LEN ||
1491             strncmp(linkInfo->linkInfo.wlan.connInfo.addr, "127.0.0.1", strlen("127.0.0.1")) == 0) {
1492             LNN_LOGE(LNN_LANE, "Wlan ip not found");
1493             return SOFTBUS_LANE_GET_LEDGER_INFO_ERR;
1494         }
1495     } else {
1496         if (LnnGetRemoteStrInfo(reqInfo->peerNetworkId, STRING_KEY_NODE_ADDR, linkInfo->linkInfo.wlan.connInfo.addr,
1497             sizeof(linkInfo->linkInfo.wlan.connInfo.addr)) != SOFTBUS_OK) {
1498             LNN_LOGE(LNN_LANE, "get remote wlan ip fail");
1499             return SOFTBUS_LANE_GET_LEDGER_INFO_ERR;
1500         }
1501     }
1502     return FillWlanLinkInfo(protocol, reqInfo, linkInfo);
1503 }
1504 
LaneLinkOfWlan(uint32_t reqId,const LinkRequest * reqInfo,const LaneLinkCb * callback)1505 static int32_t LaneLinkOfWlan(uint32_t reqId, const LinkRequest *reqInfo, const LaneLinkCb *callback)
1506 {
1507     LaneLinkInfo linkInfo;
1508     ProtocolType acceptableProtocols = LNN_PROTOCOL_ALL ^ LNN_PROTOCOL_NIP;
1509     if (reqInfo->transType == LANE_T_MSG || reqInfo->transType == LANE_T_BYTE) {
1510         acceptableProtocols |= LNN_PROTOCOL_NIP;
1511     }
1512     acceptableProtocols = acceptableProtocols & reqInfo->acceptableProtocols;
1513     ProtocolType protocol =
1514         LnnLaneSelectProtocol(LNN_NETIF_TYPE_WLAN | LNN_NETIF_TYPE_ETH, reqInfo->peerNetworkId, acceptableProtocols);
1515     if (protocol == 0) {
1516         LNN_LOGE(LNN_LANE, "protocal is invalid!");
1517         return SOFTBUS_LANE_GET_LEDGER_INFO_ERR;
1518     }
1519     int32_t ret = CreateWlanLinkInfo(protocol, reqInfo, &linkInfo);
1520     if (ret != SOFTBUS_OK) {
1521         LNN_LOGE(LNN_LANE, "CreateWlanLinkInfo fail, laneReqId=%{public}u", reqId);
1522         return ret;
1523     }
1524     ret = LaneDetectReliability(reqId, &linkInfo, callback);
1525     if (ret != SOFTBUS_OK) {
1526         LNN_LOGE(LNN_LANE, "lane detect reliability fail, laneReqId=%{public}u", reqId);
1527         return ret;
1528     }
1529     return SOFTBUS_OK;
1530 }
1531 
LaneLinkOfCoc(uint32_t reqId,const LinkRequest * reqInfo,const LaneLinkCb * callback)1532 static int32_t LaneLinkOfCoc(uint32_t reqId, const LinkRequest *reqInfo, const LaneLinkCb *callback)
1533 {
1534     LaneLinkInfo linkInfo;
1535     (void)memset_s(&linkInfo, sizeof(LaneLinkInfo), 0, sizeof(LaneLinkInfo));
1536     if (LnnGetRemoteStrInfo(reqInfo->peerNetworkId, STRING_KEY_DEV_UDID,
1537         linkInfo.peerUdid, UDID_BUF_LEN) != SOFTBUS_OK) {
1538         LNN_LOGE(LNN_LANE, "get udid error");
1539         return SOFTBUS_LANE_GET_LEDGER_INFO_ERR;
1540     }
1541     if (memcpy_s(linkInfo.linkInfo.ble.bleMac, BT_MAC_LEN, reqInfo->peerBleMac, BT_MAC_LEN) != EOK) {
1542         LNN_LOGE(LNN_LANE, "memcpy peerBleMac error");
1543         return SOFTBUS_MEM_ERR;
1544     }
1545     linkInfo.linkInfo.ble.psm = reqInfo->psm;
1546     if (strlen(linkInfo.linkInfo.ble.bleMac) == 0) {
1547         LNN_LOGE(LNN_LANE, "get peerBleMac error");
1548         return SOFTBUS_INVALID_PARAM;
1549     }
1550     char peerUdid[UDID_BUF_LEN] = {0};
1551     if (LnnGetRemoteStrInfo(reqInfo->peerNetworkId, STRING_KEY_DEV_UDID, peerUdid, UDID_BUF_LEN) != SOFTBUS_OK) {
1552         LNN_LOGE(LNN_LANE, "get udid error");
1553         return SOFTBUS_LANE_GET_LEDGER_INFO_ERR;
1554     }
1555     int32_t ret = SoftBusGenerateStrHash((uint8_t*)peerUdid, strlen(peerUdid),
1556         (uint8_t*)linkInfo.linkInfo.ble.deviceIdHash);
1557     if (ret != SOFTBUS_OK) {
1558         LNN_LOGE(LNN_LANE, "generate deviceId hash err");
1559         return ret;
1560     }
1561     linkInfo.linkInfo.ble.protoType = BLE_COC;
1562     linkInfo.type = LANE_COC;
1563     callback->onLaneLinkSuccess(reqId, linkInfo.type, &linkInfo);
1564     return SOFTBUS_OK;
1565 }
1566 
LaneLinkOfCocDirect(uint32_t reqId,const LinkRequest * reqInfo,const LaneLinkCb * callback)1567 static int32_t LaneLinkOfCocDirect(uint32_t reqId, const LinkRequest *reqInfo, const LaneLinkCb *callback)
1568 {
1569     LaneLinkInfo linkInfo;
1570     (void)memset_s(&linkInfo, sizeof(LaneLinkInfo), 0, sizeof(LaneLinkInfo));
1571     if (LnnGetRemoteStrInfo(reqInfo->peerNetworkId, STRING_KEY_DEV_UDID,
1572         linkInfo.peerUdid, UDID_BUF_LEN) != SOFTBUS_OK) {
1573         LNN_LOGE(LNN_LANE, "get udid error");
1574         return SOFTBUS_LANE_GET_LEDGER_INFO_ERR;
1575     }
1576     if (strcpy_s(linkInfo.linkInfo.bleDirect.networkId, NETWORK_ID_BUF_LEN, reqInfo->peerNetworkId) != EOK) {
1577         LNN_LOGE(LNN_LANE, "copy networkId fail");
1578         return SOFTBUS_STRCPY_ERR;
1579     }
1580     linkInfo.type = LANE_COC_DIRECT;
1581     linkInfo.linkInfo.bleDirect.protoType = BLE_COC;
1582 
1583     callback->onLaneLinkSuccess(reqId, linkInfo.type, &linkInfo);
1584     return SOFTBUS_OK;
1585 }
1586 
1587 static LaneLinkByType g_linkTable[LANE_LINK_TYPE_BUTT] = {
1588     [LANE_BR] = LaneLinkOfBr,
1589     [LANE_BLE] = LaneLinkOfBle,
1590     [LANE_P2P] = LaneLinkOfP2p,
1591     [LANE_WLAN_2P4G] = LaneLinkOfWlan,
1592     [LANE_WLAN_5G] = LaneLinkOfWlan,
1593     [LANE_BLE_REUSE] = LaneLinkOfBleReuse,
1594     [LANE_P2P_REUSE] = LaneLinkOfP2pReuse,
1595     [LANE_BLE_DIRECT] = LaneLinkOfGattDirect,
1596     [LANE_COC] = LaneLinkOfCoc,
1597     [LANE_COC_DIRECT] = LaneLinkOfCocDirect,
1598     [LANE_HML] = LaneLinkOfHml,
1599     [LANE_HML_RAW] = LaneLinkOfHmlRaw,
1600 };
1601 
BuildLink(const LinkRequest * reqInfo,uint32_t reqId,const LaneLinkCb * callback)1602 int32_t BuildLink(const LinkRequest *reqInfo, uint32_t reqId, const LaneLinkCb *callback)
1603 {
1604     if (IsLinkRequestValid(reqInfo) != SOFTBUS_OK || !LinkTypeCheck(reqInfo->linkType)) {
1605         LNN_LOGE(LNN_LANE, "the reqInfo or type is invalid");
1606         return SOFTBUS_INVALID_PARAM;
1607     }
1608     if (callback == NULL || callback->onLaneLinkSuccess == NULL ||
1609         callback->onLaneLinkFail == NULL) {
1610         LNN_LOGE(LNN_LANE, "the callback is invalid");
1611         return SOFTBUS_INVALID_PARAM;
1612     }
1613     char *anonyNetworkId = NULL;
1614     Anonymize(reqInfo->peerNetworkId, &anonyNetworkId);
1615     LNN_LOGI(LNN_LANE, "build link, linktype=%{public}d, laneReqId=%{public}u, peerNetworkId=%{public}s",
1616         reqInfo->linkType, reqId, AnonymizeWrapper(anonyNetworkId));
1617     AnonymizeFree(anonyNetworkId);
1618     int32_t ret = g_linkTable[reqInfo->linkType](reqId, reqInfo, callback);
1619     if (ret != SOFTBUS_OK) {
1620         LNN_LOGE(LNN_LANE, "lane link is failed");
1621         return ret;
1622     }
1623     return SOFTBUS_OK;
1624 }
1625 
DestroyLink(const char * networkId,uint32_t laneReqId,LaneLinkType type)1626 int32_t DestroyLink(const char *networkId, uint32_t laneReqId, LaneLinkType type)
1627 {
1628     LNN_LOGI(LNN_LANE, "destroy link=%{public}d, laneReqId=%{public}u", type, laneReqId);
1629     if (networkId == NULL) {
1630         LNN_LOGE(LNN_LANE, "the networkId is nullptr");
1631         return SOFTBUS_INVALID_PARAM;
1632     }
1633     if (type == LANE_P2P || type == LANE_HML || type == LANE_HML_RAW) {
1634         if (IsPowerControlEnabled()) {
1635             DetectDisableWifiDirectApply();
1636         }
1637         LaneDeleteP2pAddress(networkId, false);
1638         int32_t errCode = LnnDisconnectP2p(networkId, laneReqId);
1639         if (errCode != SOFTBUS_OK) {
1640             return errCode;
1641         }
1642     } else {
1643         LNN_LOGI(LNN_LANE, "ignore destroy linkType=%{public}d, laneReqId=%{public}u", type, laneReqId);
1644         PostDelayDestroyMessage(laneReqId, INVALID_LANE_ID, 0);
1645     }
1646     return SOFTBUS_OK;
1647 }
1648 
InitLaneLink(void)1649 int32_t InitLaneLink(void)
1650 {
1651     LaneInitP2pAddrList();
1652     if (SoftBusMutexInit(&g_laneResource.lock, NULL) != SOFTBUS_OK) {
1653         LNN_LOGI(LNN_LANE, "lane resource mutex init fail");
1654         return SOFTBUS_NO_INIT;
1655     }
1656     ListInit(&g_laneResource.list);
1657     g_laneResource.cnt = 0;
1658     return SOFTBUS_OK;
1659 }
1660 
DeinitLaneLink(void)1661 void DeinitLaneLink(void)
1662 {
1663     LaneDeinitP2pAddrList();
1664     if (LaneLock() != SOFTBUS_OK) {
1665         LNN_LOGE(LNN_LANE, "lane lock fail");
1666         return;
1667     }
1668     LaneResource *item = NULL;
1669     LaneResource *next = NULL;
1670     LIST_FOR_EACH_ENTRY_SAFE(item, next, &g_laneResource.list, LaneResource, node) {
1671         ListDelete(&item->node);
1672         SoftBusFree(item);
1673     }
1674     g_laneResource.cnt = 0;
1675     LaneUnlock();
1676     LnnDestroyP2p();
1677     (void)SoftBusMutexDestroy(&g_laneResource.lock);
1678 }
1679