• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2025 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_net_builder.h"
17 
18 #include <securec.h>
19 #include <stdlib.h>
20 #include <inttypes.h>
21 
22 #include "anonymizer.h"
23 #include "auth_common.h"
24 #include "auth_deviceprofile.h"
25 #include "auth_interface.h"
26 #include "auth_manager.h"
27 #include "auth_request.h"
28 #include "auth_request.h"
29 #include "auth_hichain_adapter.h"
30 #include "bus_center_event.h"
31 #include "bus_center_manager.h"
32 #include "common_list.h"
33 #include "g_enhance_lnn_func.h"
34 #include "g_enhance_lnn_func_pack.h"
35 #include "lnn_async_callback_utils.h"
36 #include "lnn_battery_info.h"
37 #include "lnn_connection_addr_utils.h"
38 #include "lnn_connection_fsm.h"
39 #include "lnn_deviceinfo_to_profile.h"
40 #include "lnn_devicename_info.h"
41 #include "lnn_discovery_manager.h"
42 #include "lnn_distributed_net_ledger.h"
43 #include "lnn_heartbeat_utils.h"
44 #include "lnn_kv_adapter_wrapper.h"
45 #include "lnn_local_net_ledger.h"
46 #include "lnn_log.h"
47 #include "lnn_map.h"
48 #include "lnn_network_id.h"
49 #include "lnn_network_info.h"
50 #include "lnn_network_manager.h"
51 #include "lnn_node_info.h"
52 #include "lnn_node_weight.h"
53 #include "lnn_ohos_account.h"
54 #include "lnn_p2p_info.h"
55 #include "lnn_physical_subnet_manager.h"
56 #include "lnn_sa_status_monitor.h"
57 #include "lnn_sle_capability.h"
58 #include "lnn_sync_info_manager.h"
59 #include "lnn_sync_item_info.h"
60 #include "lnn_topo_manager.h"
61 #include "lnn_net_builder_process.h"
62 #include "lnn_init_monitor.h"
63 #include "softbus_adapter_bt_common.h"
64 #include "softbus_adapter_crypto.h"
65 #include "softbus_adapter_json.h"
66 #include "softbus_adapter_mem.h"
67 #include "softbus_error_code.h"
68 #include "softbus_feature_config.h"
69 #include "legacy/softbus_hisysevt_bus_center.h"
70 #include "softbus_json_utils.h"
71 #include "softbus_adapter_json.h"
72 #include "softbus_utils.h"
73 #include "softbus_wifi_api_adapter.h"
74 #include "softbus_init_common.h"
75 #include "trans_channel_manager.h"
76 
77 #define DEFAULT_PKG_NAME                 "com.huawei.nearby"
78 #define DEFAULT_MAX_LNN_CONNECTION_COUNT 10
79 #define NOT_TRUSTED_DEVICE_MSG_DELAY     5000
80 #define DELAY_REG_DP_TIME                1000
81 #define RETRY_TIMES                      5
82 
SetBeginJoinLnnTime(LnnConnectionFsm * connFsm)83 void SetBeginJoinLnnTime(LnnConnectionFsm *connFsm)
84 {
85     connFsm->statisticData.beginJoinLnnTime = LnnUpTimeMs();
86 }
87 
DfxRecordDeviceInfoExchangeEndTime(const NodeInfo * info)88 static void DfxRecordDeviceInfoExchangeEndTime(const NodeInfo *info)
89 {
90     int32_t ret = SOFTBUS_OK;
91     uint64_t timeStamp = 0;
92     LnnEventExtra extra = { 0 };
93     (void)LnnEventExtraInit(&extra);
94     LnnTriggerInfo triggerInfo = { 0 };
95     GetLnnTriggerInfo(&triggerInfo);
96     timeStamp = SoftBusGetSysTimeMs();
97     extra.timeLatency = timeStamp - triggerInfo.triggerTime;
98     uint8_t hash[UDID_HASH_LEN] = { 0 };
99     char *udidHash = (char *)SoftBusCalloc(SHORT_UDID_HASH_HEX_LEN + 1);
100     if (udidHash == NULL) {
101         LNN_LOGE(LNN_BUILDER, "udidHash calloc fail");
102         return;
103     }
104     ret = SoftBusGenerateStrHash((uint8_t *)info->deviceInfo.deviceUdid, strlen(info->deviceInfo.deviceUdid),
105         hash);
106     if (ret != SOFTBUS_OK) {
107         LNN_LOGE(LNN_BUILDER, "generate udidhash fail. ret=%{public}d", ret);
108         SoftBusFree(udidHash);
109         return;
110     }
111     ret = ConvertBytesToHexString(udidHash, HB_SHORT_UDID_HASH_HEX_LEN + 1, hash, HB_SHORT_UDID_HASH_LEN);
112     if (ret != SOFTBUS_OK) {
113         LNN_LOGE(LNN_BUILDER, "convert bytes to string fail. ret=%{public}d", ret);
114         SoftBusFree(udidHash);
115         return;
116     }
117     extra.peerUdidHash = udidHash;
118     LNN_EVENT(EVENT_SCENE_JOIN_LNN, EVENT_STAGE_AUTH_DEVICE_INFO_PROCESS, extra);
119     SoftBusFree(udidHash);
120 }
121 
GetSessionKeyByNodeInfo(const NodeInfo * info,AuthHandle authHandle)122 static void GetSessionKeyByNodeInfo(const NodeInfo *info, AuthHandle authHandle)
123 {
124     NodeInfo nodeInfo;
125     SessionKey sessionKey;
126     (void)memset_s(&sessionKey, sizeof(SessionKey), 0, sizeof(SessionKey));
127     (void)memset_s(&nodeInfo, sizeof(NodeInfo), 0, sizeof(NodeInfo));
128     (void)LnnGetRemoteNodeInfoById(info->deviceInfo.deviceUdid, CATEGORY_UDID, &nodeInfo);
129     UpdateDpAclParams aclParams = {
130         .accountId = nodeInfo.accountId,
131         .deviceId = nodeInfo.deviceInfo.deviceUdid,
132         .peerUserId = nodeInfo.userId
133     };
134     UpdateDpSameAccount(&aclParams, sessionKey, false, info->aclState);
135 }
136 
IsDeviceOnlineByTargetType(const char * networkId,DiscoveryType onlineType)137 static bool IsDeviceOnlineByTargetType(const char *networkId, DiscoveryType onlineType)
138 {
139     NodeInfo node;
140     (void)memset_s(&node, sizeof(NodeInfo), 0, sizeof(NodeInfo));
141     if (LnnGetRemoteNodeInfoById(networkId, CATEGORY_NETWORK_ID, &node) != SOFTBUS_OK) {
142         char *anonyNetworkId = NULL;
143         Anonymize(networkId, &anonyNetworkId);
144         LNN_LOGE(LNN_LANE, "getRemoteInfo fail, networkId=%{public}s", AnonymizeWrapper(anonyNetworkId));
145         AnonymizeFree(anonyNetworkId);
146         return false;
147     }
148     return LnnHasDiscoveryType(&node, onlineType);
149 }
150 
OnReAuthVerifyPassed(uint32_t requestId,AuthHandle authHandle,const NodeInfo * info)151 static void OnReAuthVerifyPassed(uint32_t requestId, AuthHandle authHandle, const NodeInfo *info)
152 {
153     LNN_CHECK_AND_RETURN_LOGE(info != NULL, LNN_BUILDER, "reAuth verify result error");
154     LNN_LOGI(LNN_BUILDER, "reAuth verify passed: requestId=%{public}u, aclState=%{public}d, authId=%{public}" PRId64,
155         requestId, info->aclState, authHandle.authId);
156     AuthRequest authRequest = { 0 };
157     if (GetAuthRequest(requestId, &authRequest) != SOFTBUS_OK) {
158         LNN_LOGE(LNN_BUILDER, "auth request not found");
159         return;
160     }
161     ConnectionAddr addr;
162     (void)memset_s(&addr, sizeof(ConnectionAddr), 0, sizeof(ConnectionAddr));
163     if (!LnnConvertAuthConnInfoToAddr(&addr, &authRequest.connInfo,
164         GetCurrentConnectType(authRequest.connInfo.type))) {
165         LNN_LOGE(LNN_BUILDER, "ConvertToConnectionAddr failed");
166         return;
167     }
168     int32_t ret = SoftBusGenerateStrHash((unsigned char *)info->deviceInfo.deviceUdid,
169         strlen(info->deviceInfo.deviceUdid), (unsigned char *)addr.info.ble.udidHash);
170     if (ret != SOFTBUS_OK) {
171         LNN_LOGE(LNN_BUILDER, "gen udidHash fail");
172         return;
173     }
174     LnnConnectionFsm *connFsm = FindConnectionFsmByAddr(&addr, true);
175     if (connFsm != NULL && !connFsm->isDead && !LnnIsNeedCleanConnectionFsm(info, addr.type)) {
176         if (info != NULL && LnnUpdateGroupType(info) == SOFTBUS_OK && LnnUpdateAccountInfo(info) == SOFTBUS_OK) {
177             if (IsSameAccountId(info->accountId) && IsDeviceOnlineByTargetType(info->networkId, DISCOVERY_TYPE_BLE)) {
178                 TriggerSparkGroupJoinAgainPacked(info->deviceInfo.deviceUdid, SPARK_GROUP_DELAY_TIME_MS);
179             }
180             UpdateProfile(info);
181             LnnUpdateRemoteDeviceName(info);
182             GetSessionKeyByNodeInfo(info, authHandle);
183         }
184     } else {
185         connFsm = StartNewConnectionFsm(&addr, DEFAULT_PKG_NAME, true);
186         if (connFsm == NULL) {
187             return;
188         }
189         connFsm->connInfo.authHandle = authHandle;
190         connFsm->connInfo.nodeInfo = DupNodeInfo(info);
191         connFsm->connInfo.flag |= LNN_CONN_INFO_FLAG_JOIN_AUTO;
192         LNN_LOGI(LNN_BUILDER, "fsmId=%{public}u start a connection fsm, authId=%{public}" PRId64,
193             connFsm->id, authHandle.authId);
194         if (LnnSendAuthResultMsgToConnFsm(connFsm, SOFTBUS_OK) != SOFTBUS_OK) {
195             SoftBusFree(connFsm->connInfo.nodeInfo);
196             connFsm->connInfo.nodeInfo = NULL;
197             StopConnectionFsm(connFsm);
198         }
199     }
200 }
201 
OnReAuthVerifyFailed(uint32_t requestId,int32_t reason)202 static void OnReAuthVerifyFailed(uint32_t requestId, int32_t reason)
203 {
204     LNN_LOGI(LNN_BUILDER, "verify failed. requestId=%{public}u, reason=%{public}d", requestId, reason);
205     AuthRequest authRequest = { 0 };
206     if (GetAuthRequest(requestId, &authRequest) != SOFTBUS_OK) {
207         LNN_LOGE(LNN_BUILDER, "auth request not found");
208         LnnConnectionFsm *connFsm = FindConnectionFsmByRequestId(requestId);
209         if (connFsm == NULL || connFsm->isDead) {
210             LNN_LOGE(LNN_BUILDER, "can not find connection fsm by requestId. requestId=%{public}d", requestId);
211             return;
212         }
213         if ((connFsm->connInfo.flag & LNN_CONN_INFO_FLAG_JOIN_REQUEST) != 0) {
214             LnnNotifyJoinResult(&connFsm->connInfo.addr, connFsm->connInfo.peerNetworkId, reason);
215         }
216         return;
217     }
218     if (authRequest.forceJoinInfo.isForceJoin) {
219         LNN_LOGI(LNN_BUILDER, "notify joinLnn result");
220         LnnNotifyJoinResult(&authRequest.forceJoinInfo.addr, authRequest.forceJoinInfo.networkId, reason);
221     }
222 }
223 
224 static AuthVerifyCallback g_reAuthVerifyCallback = {
225     .onVerifyPassed = OnReAuthVerifyPassed,
226     .onVerifyFailed = OnReAuthVerifyFailed,
227 };
228 
LnnGetReAuthVerifyCallback(void)229 AuthVerifyCallback *LnnGetReAuthVerifyCallback(void)
230 {
231     return &g_reAuthVerifyCallback;
232 }
233 
NotifyStateForSession(const ConnectionAddr * addr)234 void NotifyStateForSession(const ConnectionAddr *addr)
235 {
236     if (addr == NULL) {
237         LNN_LOGE(LNN_BUILDER, "addr is null.");
238         return;
239     }
240     ConnIdCbInfo connIdCbInfo;
241     (void)memset_s(&connIdCbInfo, sizeof(ConnIdCbInfo), 0, sizeof(ConnIdCbInfo));
242     int32_t ret = GetConnIdCbInfoByAddr(addr, &connIdCbInfo);
243     if (ret != SOFTBUS_OK) {
244         LNN_LOGE(LNN_BUILDER, "get connIdCbInfo fail.");
245         return;
246     }
247     LnnNotifyStateForSession(connIdCbInfo.udid, SOFTBUS_NETWORK_JOIN_REQUEST_ERR);
248 }
249 
PostJoinRequestToConnFsm(LnnConnectionFsm * connFsm,const JoinLnnMsgPara * para,bool needReportFailure)250 int32_t PostJoinRequestToConnFsm(LnnConnectionFsm *connFsm, const JoinLnnMsgPara *para, bool needReportFailure)
251 {
252     if (para == NULL) {
253         LNN_LOGE(LNN_BUILDER, "JoinLnnMsgPara is null");
254         return SOFTBUS_INVALID_PARAM;
255     }
256     int32_t rc = SOFTBUS_OK;
257     bool isCreate = false;
258 
259     if (connFsm == NULL) {
260         connFsm = FindConnectionFsmByAddr(&para->addr, false);
261     }
262     if (connFsm == NULL || connFsm->isDead || CheckRemoteBasicInfoChanged(para->dupInfo)) {
263         connFsm = StartNewConnectionFsm(&para->addr, para->pkgName, para->isNeedConnect);
264         if (connFsm != NULL) {
265             connFsm->connInfo.dupInfo = (para->dupInfo == NULL) ? NULL : DupNodeInfo(para->dupInfo);
266         }
267         isCreate = true;
268     }
269     if (connFsm != NULL && para->isSession) {
270         connFsm->isSession = true;
271     }
272 
273     if (connFsm == NULL || LnnSendJoinRequestToConnFsm(connFsm, false) != SOFTBUS_OK) {
274         LNN_LOGE(LNN_BUILDER, "process join lnn request failed");
275         if (needReportFailure) {
276             LnnNotifyJoinResult((ConnectionAddr *)&para->addr, NULL, SOFTBUS_NETWORK_JOIN_REQUEST_ERR);
277             NotifyStateForSession(&para->addr);
278         }
279         if (connFsm != NULL && isCreate) {
280             LnnFsmRemoveMessageByType(&connFsm->fsm, FSM_CTRL_MSG_START);
281             ListDelete(&connFsm->node);
282             --LnnGetNetBuilder()->connCount;
283             LnnDestroyConnectionFsm(connFsm);
284         }
285         rc = SOFTBUS_NETWORK_JOIN_REQUEST_ERR;
286     }
287     if (rc == SOFTBUS_OK) {
288         connFsm->connInfo.infoReport = para->infoReport;
289         connFsm->connInfo.flag |=
290             (needReportFailure ? LNN_CONN_INFO_FLAG_JOIN_REQUEST : LNN_CONN_INFO_FLAG_JOIN_AUTO);
291         LNN_LOGI(LNN_BUILDER, "infoReport.osType=%{public}d, infoReport.type=%{public}X",
292             connFsm->connInfo.infoReport.osType, connFsm->connInfo.infoReport.type);
293     }
294     return rc;
295 }
296 
TryRemovePendingJoinRequest(void)297 void TryRemovePendingJoinRequest(void)
298 {
299     PendingJoinRequestNode *item = NULL;
300     PendingJoinRequestNode *next = NULL;
301     JoinLnnMsgPara para;
302 
303     LIST_FOR_EACH_ENTRY_SAFE(item, next, &LnnGetNetBuilder()->pendingList, PendingJoinRequestNode, node) {
304         if (NeedPendingJoinRequest()) {
305             return;
306         }
307         ListDelete(&item->node);
308         (void)memset_s(&para, sizeof(JoinLnnMsgPara), 0, sizeof(JoinLnnMsgPara));
309         para.isNeedConnect = true;
310         para.addr = item->addr;
311         if (strcpy_s(para.pkgName, PKG_NAME_SIZE_MAX, DEFAULT_PKG_NAME) != EOK) {
312             LNN_LOGE(LNN_BUILDER, "strcpy_s pkgName failed");
313             SoftBusFree(item);
314             continue;
315         }
316         if (PostJoinRequestToConnFsm(NULL, &para, item->needReportFailure) != SOFTBUS_OK) {
317             LNN_LOGE(LNN_BUILDER, "post pending join request failed");
318         }
319         LNN_LOGI(
320             LNN_BUILDER, "remove a pending join request, peerAddr=%{public}s", LnnPrintConnectionAddr(&item->addr));
321         SoftBusFree(item);
322         break;
323     }
324 }
325 
InitiateNewNetworkOnline(ConnectionAddrType addrType,const char * networkId)326 void InitiateNewNetworkOnline(ConnectionAddrType addrType, const char *networkId)
327 {
328     LnnConnectionFsm *item = NULL;
329     int32_t rc;
330 
331     // find target connfsm, then notify it online
332     LIST_FOR_EACH_ENTRY(item, &LnnGetNetBuilder()->fsmList, LnnConnectionFsm, node) {
333         if (strcmp(networkId, item->connInfo.peerNetworkId) != 0) {
334             continue;
335         }
336         if (item->isDead) {
337             continue;
338         }
339         if (addrType != CONNECTION_ADDR_MAX && addrType != item->connInfo.addr.type) {
340             continue;
341         }
342         rc = LnnSendNewNetworkOnlineToConnFsm(item);
343         LNN_LOGI(LNN_INIT, "initiate new network online to connection. fsmId=%{public}u, rc=%{public}d", item->id, rc);
344     }
345 }
346 
TryInitiateNewNetworkOnline(const LnnConnectionFsm * connFsm)347 void TryInitiateNewNetworkOnline(const LnnConnectionFsm *connFsm)
348 {
349     LnnConnectionFsm *item = NULL;
350     LnnInvalidCleanInfo *cleanInfo = connFsm->connInfo.cleanInfo;
351 
352     if ((connFsm->connInfo.flag & LNN_CONN_INFO_FLAG_INITIATE_ONLINE) == 0) {
353         LNN_LOGI(LNN_INIT, "no need initiate new network online. fsmId=%{public}u", connFsm->id);
354         return;
355     }
356     // let last invalid connfsm notify new network online after it clean
357     LIST_FOR_EACH_ENTRY(item, &LnnGetNetBuilder()->fsmList, LnnConnectionFsm, node) {
358         if (strcmp(connFsm->connInfo.peerNetworkId, item->connInfo.peerNetworkId) != 0) {
359             continue;
360         }
361         if ((item->connInfo.flag & LNN_CONN_INFO_FLAG_INITIATE_ONLINE) == 0) {
362             continue;
363         }
364         LNN_LOGI(LNN_INIT, "wait last connfsm clean, then initiate new network online. fsmId=%{public}u", connFsm->id);
365         return;
366     }
367     InitiateNewNetworkOnline(cleanInfo->addrType, cleanInfo->networkId);
368 }
369 
TryDisconnectAllConnection(const LnnConnectionFsm * connFsm)370 void TryDisconnectAllConnection(const LnnConnectionFsm *connFsm)
371 {
372     LnnConnectionFsm *item = NULL;
373     const ConnectionAddr *addr1 = &connFsm->connInfo.addr;
374     const ConnectionAddr *addr2 = NULL;
375     ConnectOption option;
376 
377     // Not really leaving lnn
378     if ((connFsm->connInfo.flag & LNN_CONN_INFO_FLAG_ONLINE) == 0) {
379         return;
380     }
381     LIST_FOR_EACH_ENTRY(item, &LnnGetNetBuilder()->fsmList, LnnConnectionFsm, node) {
382         addr2 = &item->connInfo.addr;
383         if (addr1->type != addr2->type) {
384             continue;
385         }
386         if (addr1->type == CONNECTION_ADDR_BR || addr1->type == CONNECTION_ADDR_BLE) {
387             if (strncmp(item->connInfo.addr.info.br.brMac, addr2->info.br.brMac, BT_MAC_LEN) == 0) {
388                 return;
389             }
390         } else if (addr1->type == CONNECTION_ADDR_WLAN || addr1->type == CONNECTION_ADDR_ETH ||
391             addr1->type == CONNECTION_ADDR_NCM) {
392             if (strncmp(addr1->info.ip.ip, addr2->info.ip.ip, strlen(addr1->info.ip.ip)) == 0) {
393                 return;
394             }
395         }
396     }
397     if (addr1->type == CONNECTION_ADDR_BR) {
398         LNN_LOGI(
399             LNN_BUILDER, "not disconnect all connection. fsmId=%{public}u, type=%{public}d", connFsm->id, addr1->type);
400         return;
401     }
402     LNN_LOGI(LNN_BUILDER, "disconnect all connection. fsmId=%{public}u, type=%{public}d", connFsm->id, addr1->type);
403     if (LnnConvertAddrToOption(addr1, &option) && CheckAuthChannelIsExit(&option) != SOFTBUS_OK) {
404         ConnDisconnectDeviceAllConn(&option);
405     }
406 }
407 
TryNotifyAllTypeOffline(const LnnConnectionFsm * connFsm)408 void TryNotifyAllTypeOffline(const LnnConnectionFsm *connFsm)
409 {
410     LnnConnectionFsm *item = NULL;
411     const ConnectionAddr *addr1 = &connFsm->connInfo.addr;
412     const ConnectionAddr *addr2 = NULL;
413 
414     LIST_FOR_EACH_ENTRY(item, &LnnGetNetBuilder()->fsmList, LnnConnectionFsm, node) {
415         addr2 = &item->connInfo.addr;
416         if (addr1->type == addr2->type) {
417             return;
418         }
419     }
420     LNN_LOGI(LNN_BUILDER, "notify all connection offline. fsmId=%{public}u, type=%{public}d", connFsm->id, addr1->type);
421     (void)LnnNotifyAllTypeOffline(addr1->type);
422 }
423 
FindConnectionFsmByConnFsmId(uint16_t connFsmId)424 LnnConnectionFsm *FindConnectionFsmByConnFsmId(uint16_t connFsmId)
425 {
426     LnnConnectionFsm *item = NULL;
427 
428     LIST_FOR_EACH_ENTRY(item, &LnnGetNetBuilder()->fsmList, LnnConnectionFsm, node) {
429         if (connFsmId == item->id) {
430             return item;
431         }
432     }
433     return NULL;
434 }
435 
TryElectMasterNodeOnline(const LnnConnectionFsm * connFsm)436 int32_t TryElectMasterNodeOnline(const LnnConnectionFsm *connFsm)
437 {
438     char peerMasterUdid[UDID_BUF_LEN] = { 0 };
439     char localMasterUdid[UDID_BUF_LEN] = { 0 };
440     int32_t localMasterWeight;
441     int32_t peerMasterWeight;
442     int32_t rc;
443 
444     // get local master node info
445     if (LnnGetLocalStrInfo(STRING_KEY_MASTER_NODE_UDID, localMasterUdid, UDID_BUF_LEN) != SOFTBUS_OK ||
446         LnnGetLocalNumInfo(NUM_KEY_MASTER_NODE_WEIGHT, &localMasterWeight) != SOFTBUS_OK) {
447         LNN_LOGE(LNN_BUILDER, "get local master node info from ledger failed");
448         return SOFTBUS_NETWORK_GET_NODE_INFO_ERR;
449     }
450     LNN_LOGI(LNN_BUILDER, "local master fsmId=%{public}u, weight=%{public}d", connFsm->id, localMasterWeight);
451     if (LnnGetRemoteStrInfo(connFsm->connInfo.peerNetworkId, STRING_KEY_MASTER_NODE_UDID,
452         peerMasterUdid, UDID_BUF_LEN) != SOFTBUS_OK ||
453         LnnGetRemoteNumInfo(connFsm->connInfo.peerNetworkId, NUM_KEY_MASTER_NODE_WEIGHT,
454             &peerMasterWeight) != SOFTBUS_OK) {
455         LNN_LOGE(LNN_BUILDER, "peer node info is not found. fsmId=%{public}u", connFsm->id);
456         return SOFTBUS_NETWORK_NOT_FOUND;
457     }
458     LNN_LOGI(LNN_BUILDER, "peer master fsmId=%{public}u, weight=%{public}d", connFsm->id, peerMasterWeight);
459     rc = LnnCompareNodeWeight(localMasterWeight, localMasterUdid, peerMasterWeight, peerMasterUdid);
460     if (rc >= 0) {
461         LNN_LOGI(LNN_BUILDER,
462             "online node  weight less than current, no need elect again. fsmId=%{public}u, compareResult=%{public}d",
463             connFsm->id, rc);
464         return SOFTBUS_OK;
465     }
466     UpdateLocalMasterNode(false, peerMasterUdid, peerMasterWeight);
467     SendElectMessageToAll(connFsm->connInfo.peerNetworkId);
468     return SOFTBUS_OK;
469 }
470 
TryElectMasterNodeOffline(const LnnConnectionFsm * connFsm)471 int32_t TryElectMasterNodeOffline(const LnnConnectionFsm *connFsm)
472 {
473     char localUdid[UDID_BUF_LEN] = { 0 };
474     char localMasterUdid[UDID_BUF_LEN] = { 0 };
475 
476     if (LnnGetLocalStrInfo(STRING_KEY_MASTER_NODE_UDID, localMasterUdid, UDID_BUF_LEN) != SOFTBUS_OK) {
477         LNN_LOGE(LNN_BUILDER, "get local master node info from ledger failed");
478         return SOFTBUS_NETWORK_GET_NODE_INFO_ERR;
479     }
480     LnnGetLocalStrInfo(STRING_KEY_DEV_UDID, localUdid, UDID_BUF_LEN);
481     if (strcmp(localMasterUdid, localUdid) == 0) {
482         LNN_LOGI(LNN_BUILDER, "local is master node, no need elect again. fsmId=%{public}u", connFsm->id);
483     } else {
484         LNN_LOGI(LNN_BUILDER, "maybe master node offline, elect again. fsmId=%{public}u", connFsm->id);
485         UpdateLocalMasterNode(true, localUdid, LnnGetLocalWeight());
486         SendElectMessageToAll(connFsm->connInfo.peerNetworkId);
487     }
488     return SOFTBUS_OK;
489 }
490 
IsSupportMasterNodeElect(SoftBusVersion version)491 bool IsSupportMasterNodeElect(SoftBusVersion version)
492 {
493     LNN_LOGD(LNN_BUILDER, "SoftBusVersion=%{public}d", version);
494     return version >= SOFTBUS_NEW_V1;
495 }
496 
TryElectAsMasterState(const char * networkId,bool isOnline)497 void TryElectAsMasterState(const char *networkId, bool isOnline)
498 {
499     if (networkId == NULL) {
500         LNN_LOGW(LNN_BUILDER, "invalid networkId");
501         return;
502     }
503     if (isOnline) {
504         LNN_LOGD(LNN_BUILDER, "restore master state ignore online process");
505         return;
506     }
507     char masterUdid[UDID_BUF_LEN] = { 0 };
508     char localUdid[UDID_BUF_LEN] = { 0 };
509     if (LnnGetLocalStrInfo(STRING_KEY_MASTER_NODE_UDID, masterUdid, UDID_BUF_LEN) != SOFTBUS_OK) {
510         LNN_LOGE(LNN_BUILDER, "get local master node info from ledger failed");
511         return;
512     }
513     const char *peerUdid = LnnConvertDLidToUdid(networkId, CATEGORY_NETWORK_ID);
514     if (peerUdid == NULL) {
515         char *anonyNetworkId = NULL;
516         Anonymize(networkId, &anonyNetworkId);
517         LNN_LOGE(LNN_BUILDER, "get invalid peerUdid, networkId=%{public}s", AnonymizeWrapper(anonyNetworkId));
518         AnonymizeFree(anonyNetworkId);
519         return;
520     }
521     if (strcmp(masterUdid, peerUdid) != 0) {
522         char *anonyPeerUdid = NULL;
523         char *anonyMasterUdid = NULL;
524         Anonymize(peerUdid, &anonyPeerUdid);
525         Anonymize(masterUdid, &anonyMasterUdid);
526         LNN_LOGD(LNN_BUILDER, "offline node is not master node. peerUdid=%{public}s, masterUdid=%{public}s",
527             AnonymizeWrapper(anonyPeerUdid), AnonymizeWrapper(anonyMasterUdid));
528         AnonymizeFree(anonyPeerUdid);
529         AnonymizeFree(anonyMasterUdid);
530         return;
531     }
532     if (LnnGetLocalStrInfo(STRING_KEY_DEV_UDID, localUdid, UDID_BUF_LEN) != SOFTBUS_OK) {
533         LNN_LOGE(LNN_BUILDER, "get local udid failed");
534         return;
535     }
536     UpdateLocalMasterNode(true, localUdid, LnnGetLocalWeight());
537 }
538 
DeinitNodeInfoSync(void)539 static void DeinitNodeInfoSync(void)
540 {
541     LnnDeinitP2p();
542     LnnDeinitNetworkInfo();
543     LnnDeinitDevicename();
544     LnnDeinitOffline();
545     LnnDeinitBatteryInfo();
546     LnnDeinitWifiDirect();
547     LnnDeinitSleInfo();
548 }
549 
NetBuilderConfigInit(void)550 static void NetBuilderConfigInit(void)
551 {
552     if (SoftbusGetConfig(SOFTBUS_INT_MAX_LNN_CONNECTION_CNT, (unsigned char *)&LnnGetNetBuilder()->maxConnCount,
553         sizeof(LnnGetNetBuilder()->maxConnCount)) != SOFTBUS_OK) {
554         LNN_LOGE(LNN_INIT, "get lnn max connection count fail, use default value");
555         LnnGetNetBuilder()->maxConnCount = DEFAULT_MAX_LNN_CONNECTION_COUNT;
556     }
557     if (SoftbusGetConfig(SOFTBUS_INT_LNN_MAX_CONCURRENT_NUM, (unsigned char *)&LnnGetNetBuilder()->maxConcurrentCount,
558         sizeof(LnnGetNetBuilder()->maxConcurrentCount)) != SOFTBUS_OK) {
559         LNN_LOGE(LNN_INIT, "get lnn max conncurent count fail, use default value");
560         LnnGetNetBuilder()->maxConcurrentCount = 0;
561     }
562     LNN_LOGD(LNN_INIT, "lnn config count: maxConnCount=%{public}d, maxConcurrentCount=%{public}d",
563         LnnGetNetBuilder()->maxConnCount, LnnGetNetBuilder()->maxConcurrentCount);
564 }
565 
InitNodeInfoSync(void)566 static int32_t InitNodeInfoSync(void)
567 {
568     int32_t rc = LnnInitP2p();
569     if (rc != SOFTBUS_OK) {
570         LNN_LOGE(LNN_INIT, "init lnn p2p fail, rc=%{public}d", rc);
571         return rc;
572     }
573     rc = LnnInitNetworkInfo();
574     if (rc != SOFTBUS_OK) {
575         LNN_LOGE(LNN_INIT, "LnnInitNetworkInfo fail, rc=%{public}d", rc);
576         return rc;
577     }
578     rc = LnnInitDevicename();
579     if (rc != SOFTBUS_OK) {
580         LNN_LOGE(LNN_INIT, "LnnInitDeviceName fail, rc=%{public}d", rc);
581         return rc;
582     }
583     rc = LnnInitOffline();
584     if (rc != SOFTBUS_OK) {
585         LNN_LOGE(LNN_INIT, "LnnInitOffline fail, rc=%{public}d", rc);
586         return rc;
587     }
588     rc = LnnInitBatteryInfo();
589     if (rc != SOFTBUS_OK) {
590         LNN_LOGE(LNN_INIT, "LnnInitBatteryInfo fail, rc=%{public}d", rc);
591         return rc;
592     }
593     rc = LnnInitCipherKeyManagerPacked();
594     if (rc != SOFTBUS_OK) {
595         LNN_LOGE(LNN_INIT, "LnnInitCipherKeyManager fail, rc=%{public}d", rc);
596         return rc;
597     }
598     rc = LnnInitWifiDirect();
599     if (rc != SOFTBUS_OK) {
600         LNN_LOGE(LNN_INIT, "init lnn wifidirect addr fail, rc=%{public}d", rc);
601         return rc;
602     }
603     rc = LnnInitSleInfo();
604     if (rc != SOFTBUS_OK) {
605         LNN_LOGE(LNN_INIT, "init lnn sle info fail, rx=%{public}d", rc);
606         return rc;
607     }
608     return SOFTBUS_OK;
609 }
610 
UpdatePCInfoWithoutSoftbus(void)611 static void UpdatePCInfoWithoutSoftbus(void)
612 {
613     int32_t onlineNum = 0;
614     NodeBasicInfo *info = NULL;
615     if (LnnGetAllOnlineNodeInfo(&info, &onlineNum) != 0) {
616         LNN_LOGE(LNN_BUILDER, "LnnGetAllOnlineNodeInfo failed!");
617         return;
618     }
619     if (info == NULL || onlineNum == 0) {
620         LNN_LOGW(LNN_BUILDER, "not online node");
621         return;
622     }
623     // mark-- remove pc offline
624     SoftBusFree(info);
625 }
626 
AccountStateChangeHandler(const LnnEventBasicInfo * info)627 static void AccountStateChangeHandler(const LnnEventBasicInfo *info)
628 {
629     if (info == NULL || info->event != LNN_EVENT_ACCOUNT_CHANGED) {
630         LNN_LOGW(LNN_BUILDER, "invalid param");
631         return;
632     }
633     const LnnMonitorHbStateChangedEvent *event = (const LnnMonitorHbStateChangedEvent *)info;
634     SoftBusAccountState accountState = (SoftBusAccountState)event->status;
635     switch (accountState) {
636         case SOFTBUS_ACCOUNT_LOG_IN:
637             LNN_LOGI(LNN_BUILDER, "ignore SOFTBUS_ACCOUNT_LOG_IN");
638             break;
639         case SOFTBUS_ACCOUNT_LOG_OUT:
640             LNN_LOGI(LNN_BUILDER, "handle SOFTBUS_ACCOUNT_LOG_OUT");
641             UpdatePCInfoWithoutSoftbus();
642             break;
643         default:
644             return;
645     }
646 }
647 
OnDeviceVerifyPass(AuthHandle authHandle,const NodeInfo * info)648 static void OnDeviceVerifyPass(AuthHandle authHandle, const NodeInfo *info)
649 {
650     LNN_CHECK_AND_RETURN_LOGE(info != NULL, LNN_BUILDER, "NodeInfo is NULL");
651     AuthConnInfo connInfo;
652     DeviceVerifyPassMsgPara *para = NULL;
653     LNN_LOGI(LNN_BUILDER, "verify passed passively, authId=%{public}" PRId64, authHandle.authId);
654     if (authHandle.type < AUTH_LINK_TYPE_WIFI || authHandle.type >= AUTH_LINK_TYPE_MAX) {
655         LNN_LOGE(LNN_BUILDER, "authHandle type error");
656         return;
657     }
658     if (AuthGetConnInfo(authHandle, &connInfo) != SOFTBUS_OK) {
659         LNN_LOGE(LNN_BUILDER, "get AuthConnInfo fail, authId=%{public}" PRId64, authHandle.authId);
660         return;
661     }
662     para = (DeviceVerifyPassMsgPara *)SoftBusMalloc(sizeof(DeviceVerifyPassMsgPara));
663     if (para == NULL) {
664         LNN_LOGE(LNN_BUILDER, "malloc DeviceVerifyPassMsgPara fail");
665         return;
666     }
667 
668     if (authHandle.type != AUTH_LINK_TYPE_ENHANCED_P2P && authHandle.type != AUTH_LINK_TYPE_SESSION) {
669         if (!LnnConvertAuthConnInfoToAddr(&para->addr, &connInfo, GetCurrentConnectType(connInfo.type))) {
670             LNN_LOGE(LNN_BUILDER, "convert connInfo to addr fail");
671             SoftBusFree(para);
672             return;
673         }
674     } else {
675         para->addr.type = CONNECTION_ADDR_BR;
676         if (strcpy_s(para->addr.info.br.brMac, BT_MAC_LEN, info->connectInfo.macAddr) != EOK) {
677             LNN_LOGE(LNN_BUILDER, "copy br mac to addr fail");
678         }
679     }
680 
681     para->authHandle = authHandle;
682     para->nodeInfo = DupNodeInfo(info);
683     if (para->nodeInfo == NULL) {
684         LNN_LOGE(LNN_BUILDER, "dup NodeInfo fail");
685         SoftBusFree(para);
686         return;
687     }
688     if (PostBuildMessageToHandler(MSG_TYPE_DEVICE_VERIFY_PASS, para) != SOFTBUS_OK) {
689         LNN_LOGE(LNN_BUILDER, "post DEVICE_VERIFY_PASS msg fail");
690         SoftBusFree(para->nodeInfo);
691         SoftBusFree(para);
692     }
693     LnnNotifyDeviceVerified(info->deviceInfo.deviceUdid);
694     DfxRecordDeviceInfoExchangeEndTime(info);
695 }
696 
OnDeviceDisconnect(AuthHandle authHandle)697 static void OnDeviceDisconnect(AuthHandle authHandle)
698 {
699     if (authHandle.type < AUTH_LINK_TYPE_WIFI || authHandle.type >= AUTH_LINK_TYPE_MAX) {
700         LNN_LOGE(LNN_BUILDER, "authHandle type error");
701         return;
702     }
703     AuthHandle *para = NULL;
704     para = (AuthHandle *)SoftBusMalloc(sizeof(AuthHandle));
705     if (para == NULL) {
706         LNN_LOGE(LNN_BUILDER, "malloc DeviceDisconnect para fail");
707         return;
708     }
709     LNN_LOGI(LNN_BUILDER, "auth device disconnect, authId=%{public}" PRId64, authHandle.authId);
710     para->authId = authHandle.authId;
711     para->type = authHandle.type;
712     if (PostBuildMessageToHandler(MSG_TYPE_DEVICE_DISCONNECT, para) != SOFTBUS_OK) {
713         LNN_LOGE(LNN_BUILDER, "post DEVICE_DISCONNECT msg fail");
714         SoftBusFree(para);
715     }
716 }
717 
IsNeedNotifyOfflineByAdv(const char * udid)718 static bool IsNeedNotifyOfflineByAdv(const char *udid)
719 {
720     NodeInfo info;
721     (void)memset_s(&info, sizeof(NodeInfo), 0, sizeof(NodeInfo));
722     if (LnnGetRemoteNodeInfoByKey(udid, &info) != SOFTBUS_OK) {
723         LNN_LOGE(LNN_BUILDER, "get nodeinfo fail");
724         return false;
725     }
726     if (!IsSupportFeatureByCapaBit(info.authCapacity, BIT_SUPPORT_ADV_OFFLINE)) {
727         LNN_LOGI(LNN_BUILDER, "peer device not support notify offline by adv");
728         return false;
729     }
730     if (LnnHasDiscoveryType(&info, DISCOVERY_TYPE_WIFI)) {
731         LNN_LOGI(LNN_BUILDER, "wifi online, no need notify offline by adv");
732         return false;
733     }
734 
735     uint32_t local = 0;
736     int32_t ret = LnnGetLocalNumU32Info(NUM_KEY_NET_CAP, &local);
737     if (ret != SOFTBUS_OK) {
738         LNN_LOGE(LNN_BUILDER, "lnnGetLocalNumInfo err, ret=%{public}d, local=%{public}u", ret, local);
739         return false;
740     }
741     if (((local & (1 << BIT_BLE)) == 0) || ((info.netCapacity & (1 << BIT_BLE)) == 0)) {
742         LNN_LOGE(LNN_BUILDER, "ble capa disable, local=%{public}u, remote=%{public}u", local, info.netCapacity);
743         return false;
744     }
745     LnnRequestLeaveSpecific(info.networkId, CONNECTION_ADDR_MAX);
746     return true;
747 }
748 
OnDeviceNotTrusted(const char * peerUdid)749 static void OnDeviceNotTrusted(const char *peerUdid)
750 {
751     if (peerUdid == NULL) {
752         LNN_LOGE(LNN_BUILDER, "invalid udid");
753         return;
754     }
755     uint32_t udidLen = strlen(peerUdid) + 1;
756     if (udidLen > UDID_BUF_LEN) {
757         LNN_LOGE(LNN_BUILDER, "udid is too long");
758         return;
759     }
760     if (DeletePcNodeInfo(peerUdid)) {
761         LNN_LOGI(LNN_BUILDER, "pc without softbus, handle offline");
762         return;
763     }
764     const char *useUdid = NULL;
765     char udid[UDID_BUF_LEN] = { 0 };
766     if (StringToLowerCase(peerUdid, udid, UDID_BUF_LEN) != SOFTBUS_OK) {
767         return;
768     }
769     useUdid = SelectUseUdid(peerUdid, udid);
770     if (useUdid == NULL) {
771         return;
772     }
773     LNN_CHECK_AND_RETURN_LOGW(!IsNeedNotifyOfflineByAdv(peerUdid), LNN_BUILDER, "need notify offline by adv");
774     NotTrustedDelayInfo *info = (NotTrustedDelayInfo *)SoftBusCalloc(sizeof(NotTrustedDelayInfo));
775     LNN_CHECK_AND_RETURN_LOGE(info != NULL, LNN_BUILDER, "malloc NotTrustedDelayInfo fail");
776     if (AuthGetLatestAuthSeqList(useUdid, info->authSeq, DISCOVERY_TYPE_COUNT) != SOFTBUS_OK) {
777         LNN_LOGE(LNN_BUILDER, "get latest AuthSeq list fail");
778         SoftBusFree(info);
779         return;
780     }
781     if (strcpy_s(info->udid, UDID_BUF_LEN, useUdid) != EOK) {
782         LNN_LOGE(LNN_BUILDER, "copy udid fail");
783         SoftBusFree(info);
784         return;
785     }
786     if (LnnSendNotTrustedInfoPacked(info, DISCOVERY_TYPE_COUNT,
787         LnnProcessCompleteNotTrustedMsg) != SOFTBUS_OK) {
788         LNN_LOGE(LNN_BUILDER, "send NotTrustedInfo fail");
789         OnLnnProcessNotTrustedMsgDelay((void *)info);
790         return;
791     }
792     if (LnnAsyncCallbackDelayHelper(GetLooper(LOOP_TYPE_DEFAULT), OnLnnProcessNotTrustedMsgDelay,
793         (void *)info, NOT_TRUSTED_DEVICE_MSG_DELAY) != SOFTBUS_OK) {
794         LNN_LOGE(LNN_BUILDER, "async not trusted msg delay fail");
795         SoftBusFree(info);
796     }
797 }
798 
799 AuthVerifyListener g_verifyListener = {
800     .onDeviceVerifyPass = OnDeviceVerifyPass,
801     .onDeviceNotTrusted = OnDeviceNotTrusted,
802     .onDeviceDisconnect = OnDeviceDisconnect,
803 };
804 
UserSwitchedHandler(const LnnEventBasicInfo * info)805 static void UserSwitchedHandler(const LnnEventBasicInfo *info)
806 {
807     if (info == NULL || info->event != LNN_EVENT_USER_SWITCHED) {
808         LNN_LOGW(LNN_BUILDER, "invalid param");
809         return;
810     }
811     const LnnMonitorHbStateChangedEvent *event = (const LnnMonitorHbStateChangedEvent *)info;
812     SoftBusUserSwitchState userSwitchState = (SoftBusUserSwitchState)event->status;
813     bool isDataShareInit = true;
814     switch (userSwitchState) {
815         case SOFTBUS_USER_SWITCHED:
816             LNN_LOGI(LNN_BUILDER, "SOFTBUS_USER_SWITCHED");
817             LnnGetDataShareInitResult(&isDataShareInit);
818             if (isDataShareInit) {
819                 RegisterOOBEMonitorPacked(NULL);
820             }
821             LnnSetUnlockState();
822             break;
823         default:
824             return;
825     }
826 }
827 
InitNetBuilderLooper(void)828 static int32_t InitNetBuilderLooper(void)
829 {
830     ListInit(&LnnGetNetBuilder()->fsmList);
831     ListInit(&LnnGetNetBuilder()->pendingList);
832     LnnGetNetBuilder()->nodeType = NODE_TYPE_L;
833     LnnGetNetBuilder()->looper = GetLooper(LOOP_TYPE_DEFAULT);
834     if (LnnGetNetBuilder()->looper == NULL) {
835         LNN_LOGE(LNN_INIT, "get default looper fail");
836         return SOFTBUS_LOOPER_ERR;
837     }
838     LnnGetNetBuilder()->handler.name = (char *)"NetBuilderHandler";
839     LnnGetNetBuilder()->handler.looper = LnnGetNetBuilder()->looper;
840     LnnGetNetBuilder()->handler.HandleMessage = NetBuilderMessageHandler;
841     LnnGetNetBuilder()->isInit = true;
842     LNN_LOGI(LNN_INIT, "init net builder looper success");
843     return SOFTBUS_OK;
844 }
845 
InitSyncInfoReg(void)846 static int32_t InitSyncInfoReg(void)
847 {
848     int32_t rc = LnnRegSyncInfoHandler(LNN_INFO_TYPE_MASTER_ELECT, OnReceiveMasterElectMsg);
849     if (rc != SOFTBUS_OK) {
850         LNN_LOGE(LNN_INIT, "register sync master elect msg fail, rc=%{public}d", rc);
851         return rc;
852     }
853     rc = LnnRegSyncInfoHandler(LNN_INFO_TYPE_NODE_ADDR, OnReceiveNodeAddrChangedMsg);
854     if (rc != SOFTBUS_OK) {
855         LNN_LOGE(LNN_INIT, "register node addr changed msg fail, rc=%{public}d", rc);
856         return rc;
857     }
858     return SOFTBUS_OK;
859 }
860 
LnnInitNetBuilder(void)861 int32_t LnnInitNetBuilder(void)
862 {
863     if (LnnGetNetBuilder()->isInit == true) {
864         LNN_LOGI(LNN_INIT, "init net builder repeatly");
865         return SOFTBUS_OK;
866     }
867     int32_t rc = LnnInitSyncInfoManager();
868     if (rc != SOFTBUS_OK) {
869         LNN_LOGE(LNN_INIT, "init sync info manager fail, rc=%{public}d", rc);
870         return rc;
871     }
872     LnnInitTopoManager();
873     UpdateLocalNetCapability();
874     InitNodeInfoSync();
875     LnnInitConnIdCallbackManager();
876     NetBuilderConfigInit();
877     // link finder init fail will not cause softbus init fail
878     if (LnnLinkFinderInitPacked() != SOFTBUS_OK) {
879         LNN_LOGE(LNN_INIT, "link finder init fail");
880     }
881     rc = RegAuthVerifyListener(&g_verifyListener);
882     if (rc != SOFTBUS_OK) {
883         LNN_LOGE(LNN_INIT, "register auth verify listener fail, rc=%{public}d", rc);
884         return rc;
885     }
886     rc = InitSyncInfoReg();
887     if (rc != SOFTBUS_OK) {
888         return rc;
889     }
890     rc = ConfigLocalLedger();
891     if (rc != SOFTBUS_OK) {
892         LNN_LOGE(LNN_INIT, "config local ledger fail, rc=%{public}d", rc);
893         return rc;
894     }
895     rc = LnnRegisterEventHandler(LNN_EVENT_ACCOUNT_CHANGED, AccountStateChangeHandler);
896     if (rc != SOFTBUS_OK) {
897         LNN_LOGE(LNN_INIT, "regist account change evt handler fail, rc=%{public}d", rc);
898         return rc;
899     }
900     rc = LnnRegisterEventHandler(LNN_EVENT_USER_SWITCHED, UserSwitchedHandler);
901     if (rc != SOFTBUS_OK) {
902         LNN_LOGE(LNN_INIT, "regist user switch evt handler fail, rc=%{public}d", rc);
903         return rc;
904     }
905     if (LnnInitModuleNotifyWithRetrySync(INIT_DEPS_KVSTORE, LnnSubcribeKvStoreService, RETRY_TIMES,
906         DELAY_REG_DP_TIME) != SOFTBUS_OK) {
907         LNN_LOGE(LNN_INIT, "regist kv store service fail!");
908     }
909     LnnInitSaStatusMonitor();
910     return InitNetBuilderLooper();
911 }
912 
LnnInitNetBuilderDelay(void)913 int32_t LnnInitNetBuilderDelay(void)
914 {
915     char udid[UDID_BUF_LEN] = { 0 };
916     // set master weight and master udid
917     int32_t ret = LnnGetLocalStrInfo(STRING_KEY_DEV_UDID, udid, UDID_BUF_LEN);
918     if (ret != SOFTBUS_OK) {
919         LNN_LOGE(LNN_INIT, "get local udid error!");
920         return ret;
921     }
922     LnnSetLocalStrInfo(STRING_KEY_MASTER_NODE_UDID, udid);
923     LnnSetLocalNumInfo(NUM_KEY_MASTER_NODE_WEIGHT, LnnGetLocalWeight());
924     ret = LnnInitFastOfflinePacked();
925     if (ret != SOFTBUS_OK) {
926         LNN_LOGE(LNN_INIT, "fast offline init fail!");
927         return ret;
928     }
929     ret = LnnDeviceCloudConvergenceInitPacked();
930     LNN_LOGW(LNN_INIT, "init device cloud convergence init ret=%{public}d", ret);
931 #ifndef ENABLE_FEATURE_DATAMGR_SERVICE
932     LNN_LOGI(LNN_INIT, "no data manager, release datashare init monitor");
933     LnnNotifyDataShareStateChangeEvent(SOFTBUS_DATA_SHARE_READY);
934 #endif
935     return SOFTBUS_OK;
936 }
937 
LnnDeinitNetBuilder(void)938 void LnnDeinitNetBuilder(void)
939 {
940     LnnConnectionFsm *item = NULL;
941     LnnConnectionFsm *nextItem = NULL;
942 
943     if (!LnnGetNetBuilder()->isInit) {
944         return;
945     }
946     LIST_FOR_EACH_ENTRY_SAFE(item, nextItem, &LnnGetNetBuilder()->fsmList, LnnConnectionFsm, node) {
947         StopConnectionFsm(item);
948     }
949     LnnUnregSyncInfoHandler(LNN_INFO_TYPE_MASTER_ELECT, OnReceiveMasterElectMsg);
950     LnnUnregSyncInfoHandler(LNN_INFO_TYPE_NODE_ADDR, OnReceiveNodeAddrChangedMsg);
951     LnnUnregisterEventHandler(LNN_EVENT_ACCOUNT_CHANGED, AccountStateChangeHandler);
952     LnnUnregisterEventHandler(LNN_EVENT_USER_SWITCHED, UserSwitchedHandler);
953     UnregAuthVerifyListener();
954     LnnDeinitTopoManager();
955     DeinitNodeInfoSync();
956     LnnDeinitFastOfflinePacked();
957     LnnDeinitSyncInfoManager();
958     LnnDeInitSaStatusMonitor();
959     LnnDeinitConnIdCallbackManager();
960     LnnGetNetBuilder()->isInit = false;
961 }