1 /*
2  * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "lnn_bus_center_ipc.h"
17 
18 #include <cstring>
19 #include <mutex>
20 #include <securec.h>
21 #include <vector>
22 
23 #include "bus_center_client_proxy.h"
24 #include "bus_center_manager.h"
25 #include "lnn_connection_addr_utils.h"
26 #include "lnn_distributed_net_ledger.h"
27 #include "lnn_heartbeat_ctrl.h"
28 #include "lnn_log.h"
29 #include "lnn_meta_node_ledger.h"
30 #include "lnn_time_sync_manager.h"
31 #include "lnn_meta_node_interface.h"
32 #include "softbus_def.h"
33 #include "softbus_errcode.h"
34 #include "softbus_permission.h"
35 
36 struct JoinLnnRequestInfo {
37     char pkgName[PKG_NAME_SIZE_MAX];
38     int32_t pid;
39     ConnectionAddr addr;
40 };
41 
42 struct RefreshLnnRequestInfo {
43     char pkgName[PKG_NAME_SIZE_MAX];
44     int32_t pid;
45     int32_t subscribeId;
46 };
47 
48 struct LeaveLnnRequestInfo {
49     char pkgName[PKG_NAME_SIZE_MAX];
50     int32_t pid;
51     char networkId[NETWORK_ID_BUF_LEN];
52 };
53 
54 struct DataLevelChangeReqInfo {
55     char pkgName[PKG_NAME_SIZE_MAX];
56     int32_t pid;
57 };
58 
59 static std::mutex g_lock;
60 static std::vector<JoinLnnRequestInfo *> g_joinLNNRequestInfo;
61 static std::vector<LeaveLnnRequestInfo *> g_leaveLNNRequestInfo;
62 static std::vector<RefreshLnnRequestInfo *> g_refreshLnnRequestInfo;
63 static std::vector<DataLevelChangeReqInfo *> g_dataLevelChangeRequestInfo;
64 
65 static int32_t OnRefreshDeviceFound(const char *pkgName, const DeviceInfo *device,
66     const InnerDeviceInfoAddtions *additions);
67 
68 static IServerDiscInnerCallback g_discInnerCb = {
69     .OnServerDeviceFound = OnRefreshDeviceFound,
70 };
71 
72 static int32_t OnDataLevelChanged(const char *networkId, const DataLevelInfo *dataLevelInfo);
73 
74 static IDataLevelChangeCallback g_dataLevelChangeCb = {
75     .onDataLevelChanged = OnDataLevelChanged,
76 };
77 
IsRepeatJoinLNNRequest(const char * pkgName,int32_t callingPid,const ConnectionAddr * addr)78 static bool IsRepeatJoinLNNRequest(const char *pkgName, int32_t callingPid, const ConnectionAddr *addr)
79 {
80     for (const auto &iter : g_joinLNNRequestInfo) {
81         if (strncmp(pkgName, (*iter).pkgName, strlen(pkgName)) != 0 || (*iter).pid != callingPid) {
82             continue;
83         }
84         if (LnnIsSameConnectionAddr(addr, &(*iter).addr, false)) {
85             return true;
86         }
87     }
88     return false;
89 }
90 
AddJoinLNNInfo(const char * pkgName,int32_t callingPid,const ConnectionAddr * addr)91 static int32_t AddJoinLNNInfo(const char *pkgName, int32_t callingPid, const ConnectionAddr *addr)
92 {
93     JoinLnnRequestInfo *info = new (std::nothrow) JoinLnnRequestInfo();
94     if (info == nullptr) {
95         return SOFTBUS_MEM_ERR;
96     }
97     if (strncpy_s(info->pkgName, PKG_NAME_SIZE_MAX, pkgName, strlen(pkgName)) != EOK) {
98         LNN_LOGE(LNN_EVENT, "copy pkgName fail");
99         delete info;
100         return SOFTBUS_MEM_ERR;
101     }
102     info->pid = callingPid;
103     info->addr = *addr;
104     g_joinLNNRequestInfo.push_back(info);
105     return SOFTBUS_OK;
106 }
107 
IsRepeatLeaveLNNRequest(const char * pkgName,int32_t callingPid,const char * networkId)108 static bool IsRepeatLeaveLNNRequest(const char *pkgName, int32_t callingPid, const char *networkId)
109 {
110     for (const auto &iter : g_leaveLNNRequestInfo) {
111         if (strncmp(pkgName, (*iter).pkgName, strlen(pkgName)) != 0 || (*iter).pid != callingPid) {
112             continue;
113         }
114         if (strncmp(networkId, (*iter).networkId, strlen(networkId)) == 0) {
115             return true;
116         }
117     }
118     return false;
119 }
120 
AddLeaveLNNInfo(const char * pkgName,int32_t callingPid,const char * networkId)121 static int32_t AddLeaveLNNInfo(const char *pkgName, int32_t callingPid, const char *networkId)
122 {
123     LeaveLnnRequestInfo *info = new (std::nothrow) LeaveLnnRequestInfo();
124     if (info == nullptr) {
125         return SOFTBUS_MEM_ERR;
126     }
127     if (strncpy_s(info->pkgName, PKG_NAME_SIZE_MAX, pkgName, strlen(pkgName)) != EOK) {
128         LNN_LOGE(LNN_EVENT, "copy pkgName fail");
129         delete info;
130         return SOFTBUS_MEM_ERR;
131     }
132     if (strncpy_s(info->networkId, NETWORK_ID_BUF_LEN, networkId, strlen(networkId)) != EOK) {
133         LNN_LOGE(LNN_EVENT, "copy networkId fail");
134         delete info;
135         return SOFTBUS_MEM_ERR;
136     }
137     info->pid = callingPid;
138     g_leaveLNNRequestInfo.push_back(info);
139     return SOFTBUS_OK;
140 }
141 
OnRefreshDeviceFound(const char * pkgName,const DeviceInfo * device,const InnerDeviceInfoAddtions * additions)142 static int32_t OnRefreshDeviceFound(const char *pkgName, const DeviceInfo *device,
143     const InnerDeviceInfoAddtions *additions)
144 {
145     LNN_CHECK_AND_RETURN_RET_LOGE(additions != nullptr, SOFTBUS_INVALID_PARAM, LNN_EVENT, "additions is null");
146     LNN_CHECK_AND_RETURN_RET_LOGE(device != nullptr, SOFTBUS_INVALID_PARAM, LNN_EVENT, "device is null");
147     LNN_CHECK_AND_RETURN_RET_LOGE(pkgName != nullptr, SOFTBUS_INVALID_PARAM, LNN_EVENT, "pkgName is null");
148     uint32_t pkgNameLen = strnlen(pkgName, PKG_NAME_SIZE_MAX);
149     LNN_CHECK_AND_RETURN_RET_LOGE(pkgNameLen < PKG_NAME_SIZE_MAX, SOFTBUS_INVALID_PKGNAME, LNN_EVENT,
150         "pkgName invalid");
151 
152     DeviceInfo newDevice;
153     auto ret = memcpy_s(&newDevice, sizeof(DeviceInfo), device, sizeof(DeviceInfo));
154     LNN_CHECK_AND_RETURN_RET_LOGE(ret == EOK, SOFTBUS_MEM_ERR, LNN_EVENT, "copy device info failed");
155 
156     std::lock_guard<std::mutex> autoLock(g_lock);
157     for (const auto &iter : g_refreshLnnRequestInfo) {
158         if (strncmp(pkgName, iter->pkgName, pkgNameLen) != 0) {
159             continue;
160         }
161         LnnRefreshDeviceOnlineStateAndDevIdInfo(pkgName, &newDevice, additions);
162         (void)ClientOnRefreshDeviceFound(pkgName, (*iter).pid, &newDevice, sizeof(DeviceInfo));
163     }
164     return SOFTBUS_OK;
165 }
166 
OnDataLevelChanged(const char * networkId,const DataLevelInfo * dataLevelInfo)167 static int32_t OnDataLevelChanged(const char *networkId, const DataLevelInfo *dataLevelInfo)
168 {
169     std::lock_guard<std::mutex> autoLock(g_lock);
170     const char *dbPkgName = "distributeddata-default";
171     for (const auto &iter : g_dataLevelChangeRequestInfo) {
172         if (strcmp(dbPkgName, iter->pkgName) != 0) {
173             continue;
174         }
175         (void)ClientOnDataLevelChanged(dbPkgName, iter->pid, networkId, dataLevelInfo);
176     }
177     return SOFTBUS_OK;
178 }
179 
LnnIpcServerJoin(const char * pkgName,int32_t callingPid,void * addr,uint32_t addrTypeLen)180 int32_t LnnIpcServerJoin(const char *pkgName, int32_t callingPid, void *addr, uint32_t addrTypeLen)
181 {
182     ConnectionAddr *connAddr = reinterpret_cast<ConnectionAddr *>(addr);
183 
184     if (pkgName == nullptr || connAddr == nullptr) {
185         LNN_LOGE(LNN_EVENT, "parameters are nullptr");
186         return SOFTBUS_INVALID_PARAM;
187     }
188     if (addrTypeLen != sizeof(ConnectionAddr)) {
189         LNN_LOGE(LNN_EVENT, "addr is invalid");
190         return SOFTBUS_INVALID_PARAM;
191     }
192     std::lock_guard<std::mutex> autoLock(g_lock);
193     if (IsRepeatJoinLNNRequest(pkgName, callingPid, connAddr)) {
194         LNN_LOGE(LNN_EVENT, "repeat join lnn request pkgName=%{public}s", pkgName);
195         return SOFTBUS_ALREADY_EXISTED;
196     }
197     int32_t ret = LnnServerJoin(connAddr, pkgName);
198     if (ret == SOFTBUS_OK) {
199         ret = AddJoinLNNInfo(pkgName, callingPid, connAddr);
200     }
201     return ret;
202 }
203 
LnnIpcServerLeave(const char * pkgName,int32_t callingPid,const char * networkId)204 int32_t LnnIpcServerLeave(const char *pkgName, int32_t callingPid, const char *networkId)
205 {
206     if (pkgName == nullptr || networkId == nullptr) {
207         LNN_LOGE(LNN_EVENT, "parameters are nullptr");
208         return SOFTBUS_INVALID_PARAM;
209     }
210     std::lock_guard<std::mutex> autoLock(g_lock);
211     if (IsRepeatLeaveLNNRequest(pkgName, callingPid, networkId)) {
212         LNN_LOGE(LNN_EVENT, "repeat leave lnn request pkgName=%{public}s", pkgName);
213         return SOFTBUS_ALREADY_EXISTED;
214     }
215     int32_t ret = LnnServerLeave(networkId, pkgName);
216     if (ret == SOFTBUS_OK) {
217         ret = AddLeaveLNNInfo(pkgName, callingPid, networkId);
218     }
219     return ret;
220 }
221 
LnnIpcGetAllOnlineNodeInfo(const char * pkgName,void ** info,uint32_t infoTypeLen,int32_t * infoNum)222 int32_t LnnIpcGetAllOnlineNodeInfo(const char *pkgName, void **info, uint32_t infoTypeLen,
223     int32_t *infoNum)
224 {
225     if (infoTypeLen != sizeof(NodeBasicInfo)) {
226         LNN_LOGE(LNN_EVENT, "infoTypeLen is invalid, infoTypeLen=%{public}d", infoTypeLen);
227         return SOFTBUS_INVALID_PARAM;
228     }
229     return LnnGetAllOnlineNodeInfo(reinterpret_cast<NodeBasicInfo **>(info), infoNum);
230 }
231 
LnnIpcGetLocalDeviceInfo(const char * pkgName,void * info,uint32_t infoTypeLen)232 int32_t LnnIpcGetLocalDeviceInfo(const char *pkgName, void *info, uint32_t infoTypeLen)
233 {
234     (void)infoTypeLen;
235     return LnnGetLocalDeviceInfo(reinterpret_cast<NodeBasicInfo *>(info));
236 }
237 
LnnIpcGetNodeKeyInfo(const char * pkgName,const char * networkId,int32_t key,unsigned char * buf,uint32_t len)238 int32_t LnnIpcGetNodeKeyInfo(const char *pkgName, const char *networkId, int32_t key, unsigned char *buf,
239     uint32_t len)
240 {
241     if (key == NODE_KEY_BLE_OFFLINE_CODE) {
242         LNN_LOGE(LNN_EVENT, "the process has been abandoned");
243         return SOFTBUS_INVALID_PARAM;
244     }
245     return LnnGetNodeKeyInfo(networkId, key, buf, len);
246 }
247 
LnnIpcSetNodeDataChangeFlag(const char * pkgName,const char * networkId,uint16_t dataChangeFlag)248 int32_t LnnIpcSetNodeDataChangeFlag(const char *pkgName, const char *networkId,
249     uint16_t dataChangeFlag)
250 {
251     (void)pkgName;
252     return LnnSetNodeDataChangeFlag(networkId, dataChangeFlag);
253 }
254 
LnnIpcRegDataLevelChangeCb(const char * pkgName,int32_t callingPid)255 int32_t LnnIpcRegDataLevelChangeCb(const char *pkgName, int32_t callingPid)
256 {
257     // register data level change callback to heartbeat
258     std::lock_guard<std::mutex> autoLock(g_lock);
259     DataLevelChangeReqInfo *info = new (std::nothrow) DataLevelChangeReqInfo();
260     if (info == nullptr) {
261         COMM_LOGE(COMM_SVC, "DataLevelChangeReqInfo object is nullptr");
262         return SOFTBUS_ERR;
263     }
264     if (strcpy_s(info->pkgName, PKG_NAME_SIZE_MAX, pkgName) != EOK) {
265         LNN_LOGE(LNN_EVENT, "copy pkgName fail");
266         delete info;
267         return SOFTBUS_MEM_ERR;
268     }
269     info->pid = callingPid;
270     g_dataLevelChangeRequestInfo.push_back(info);
271     LnnRegDataLevelChangeCb(&g_dataLevelChangeCb);
272     return SOFTBUS_OK;
273 }
274 
LnnIpcUnregDataLevelChangeCb(const char * pkgName,int32_t callingPid)275 int32_t LnnIpcUnregDataLevelChangeCb(const char *pkgName, int32_t callingPid)
276 {
277     // unregister data level chagne callback to heartbeta
278     std::lock_guard<std::mutex> autoLock(g_lock);
279     std::vector<DataLevelChangeReqInfo *>::iterator iter;
280     for (iter = g_dataLevelChangeRequestInfo.begin(); iter != g_dataLevelChangeRequestInfo.end();) {
281         if (strcmp(pkgName, (*iter)->pkgName) == 0 && callingPid == (*iter)->pid) {
282             delete *iter;
283             g_dataLevelChangeRequestInfo.erase(iter);
284             break;
285         }
286     }
287     LnnUnregDataLevelChangeCb();
288     return SOFTBUS_OK;
289 }
290 
LnnIpcSetDataLevel(const DataLevel * dataLevel)291 int32_t LnnIpcSetDataLevel(const DataLevel *dataLevel)
292 {
293     bool isSwitchLevelChanged = false;
294     int32_t ret = LnnSetDataLevel(dataLevel, &isSwitchLevelChanged);
295     if (ret != SOFTBUS_OK) {
296         LNN_LOGE(LNN_EVENT, "Set Data Level failed, ret=%{public}d", ret);
297         return ret;
298     }
299     if (!isSwitchLevelChanged) {
300         return SOFTBUS_OK;
301     }
302     ret = LnnTriggerDataLevelHeartbeat();
303     if (ret != SOFTBUS_OK) {
304         LNN_LOGE(LNN_EVENT, "Set Data Level but trigger heartbeat failed, ret=%{public}d", ret);
305         return ret;
306     }
307     return SOFTBUS_OK;
308 }
309 
LnnIpcStartTimeSync(const char * pkgName,int32_t callingPid,const char * targetNetworkId,int32_t accuracy,int32_t period)310 int32_t LnnIpcStartTimeSync(const char *pkgName,  int32_t callingPid, const char *targetNetworkId,
311     int32_t accuracy, int32_t period)
312 {
313     return LnnStartTimeSync(pkgName, callingPid, targetNetworkId, (TimeSyncAccuracy)accuracy, (TimeSyncPeriod)period);
314 }
315 
LnnIpcStopTimeSync(const char * pkgName,const char * targetNetworkId,int32_t callingPid)316 int32_t LnnIpcStopTimeSync(const char *pkgName, const char *targetNetworkId, int32_t callingPid)
317 {
318     return LnnStopTimeSync(pkgName, targetNetworkId, callingPid);
319 }
320 
LnnIpcPublishLNN(const char * pkgName,const PublishInfo * info)321 int32_t LnnIpcPublishLNN(const char *pkgName, const PublishInfo *info)
322 {
323     return LnnPublishService(pkgName, info, false);
324 }
325 
LnnIpcStopPublishLNN(const char * pkgName,int32_t publishId)326 int32_t LnnIpcStopPublishLNN(const char *pkgName, int32_t publishId)
327 {
328     return LnnUnPublishService(pkgName, publishId, false);
329 }
330 
IsRepeatRefreshLnnRequest(const char * pkgName,int32_t callingPid,int32_t subscribeId)331 static bool IsRepeatRefreshLnnRequest(const char *pkgName, int32_t callingPid, int32_t subscribeId)
332 {
333     uint32_t pkgNameLen = strlen(pkgName);
334     std::lock_guard<std::mutex> autoLock(g_lock);
335     for (const auto &iter : g_refreshLnnRequestInfo) {
336         if (strncmp(pkgName, iter->pkgName, pkgNameLen) == 0 && iter->pid == callingPid &&
337             iter->subscribeId == subscribeId) {
338             return true;
339         }
340     }
341     return false;
342 }
343 
AddRefreshLnnInfo(const char * pkgName,int32_t callingPid,int32_t subscribeId)344 static int32_t AddRefreshLnnInfo(const char *pkgName, int32_t callingPid, int32_t subscribeId)
345 {
346     RefreshLnnRequestInfo *info = new (std::nothrow) RefreshLnnRequestInfo();
347     LNN_CHECK_AND_RETURN_RET_LOGE(info != nullptr, SOFTBUS_MALLOC_ERR, LNN_EVENT, "malloc failed");
348     if (strncpy_s(info->pkgName, PKG_NAME_SIZE_MAX, pkgName, strlen(pkgName)) != EOK) {
349         LNN_LOGE(LNN_EVENT, "copy pkgName fail");
350         delete info;
351         return SOFTBUS_STRCPY_ERR;
352     }
353     info->pid = callingPid;
354     info->subscribeId = subscribeId;
355     std::lock_guard<std::mutex> autoLock(g_lock);
356     g_refreshLnnRequestInfo.push_back(info);
357     return SOFTBUS_OK;
358 }
359 
DeleteRefreshLnnInfo(const char * pkgName,int32_t callingPid,int32_t subscribeId)360 static int32_t DeleteRefreshLnnInfo(const char *pkgName, int32_t callingPid, int32_t subscribeId)
361 {
362     uint32_t pkgNameLen = strlen(pkgName);
363     std::lock_guard<std::mutex> autoLock(g_lock);
364     std::vector<RefreshLnnRequestInfo *>::iterator iter;
365     for (iter = g_refreshLnnRequestInfo.begin(); iter != g_refreshLnnRequestInfo.end();) {
366         if (strncmp(pkgName, (*iter)->pkgName, pkgNameLen) != 0 || (*iter)->pid != callingPid ||
367             (*iter)->subscribeId != subscribeId) {
368             ++iter;
369             continue;
370         }
371         delete *iter;
372         iter = g_refreshLnnRequestInfo.erase(iter);
373     }
374     return SOFTBUS_OK;
375 }
376 
LnnIpcRefreshLNN(const char * pkgName,int32_t callingPid,const SubscribeInfo * info)377 int32_t LnnIpcRefreshLNN(const char *pkgName, int32_t callingPid, const SubscribeInfo *info)
378 {
379     LNN_CHECK_AND_RETURN_RET_LOGE(info != nullptr, SOFTBUS_INVALID_PARAM, LNN_EVENT, "info is null");
380     LNN_CHECK_AND_RETURN_RET_LOGE(pkgName != nullptr, SOFTBUS_INVALID_PARAM, LNN_EVENT, "pkgName is null");
381     LNN_CHECK_AND_RETURN_RET_LOGE(strnlen(pkgName, PKG_NAME_SIZE_MAX) < PKG_NAME_SIZE_MAX, SOFTBUS_INVALID_PKGNAME,
382         LNN_EVENT, "pkgName invalid");
383 
384     if (IsRepeatRefreshLnnRequest(pkgName, callingPid, info->subscribeId)) {
385         LNN_LOGD(LNN_EVENT, "repeat refresh lnn request pkgName=%{public}s, subscribeId=%{public}d",
386             pkgName, info->subscribeId);
387     } else {
388         int32_t ret = AddRefreshLnnInfo(pkgName, callingPid, info->subscribeId);
389         LNN_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, LNN_EVENT,
390             "add refresh info failed, ret=%{public}d", ret);
391     }
392     InnerCallback callback = {
393         .serverCb = g_discInnerCb,
394     };
395     return LnnStartDiscDevice(pkgName, info, &callback, false);
396 }
397 
LnnIpcStopRefreshLNN(const char * pkgName,int32_t callingPid,int32_t subscribeId)398 int32_t LnnIpcStopRefreshLNN(const char *pkgName, int32_t callingPid, int32_t subscribeId)
399 {
400     LNN_CHECK_AND_RETURN_RET_LOGE(pkgName != nullptr, SOFTBUS_INVALID_PARAM, LNN_EVENT, "pkgName is null");
401     LNN_CHECK_AND_RETURN_RET_LOGE(strnlen(pkgName, PKG_NAME_SIZE_MAX) < PKG_NAME_SIZE_MAX, SOFTBUS_INVALID_PKGNAME,
402         LNN_EVENT, "pkgName invalid");
403 
404     if (IsRepeatRefreshLnnRequest(pkgName, callingPid, subscribeId) &&
405         DeleteRefreshLnnInfo(pkgName, callingPid, subscribeId) != SOFTBUS_OK) {
406         LNN_LOGE(LNN_EVENT, "stop refresh lnn, clean info fail");
407         return SOFTBUS_ERR;
408     }
409     return LnnStopDiscDevice(pkgName, subscribeId, false);
410 }
411 
LnnIpcActiveMetaNode(const MetaNodeConfigInfo * info,char * metaNodeId)412 int32_t LnnIpcActiveMetaNode(const MetaNodeConfigInfo *info, char *metaNodeId)
413 {
414     return LnnActiveMetaNode(info, metaNodeId);
415 }
416 
LnnIpcDeactiveMetaNode(const char * metaNodeId)417 int32_t LnnIpcDeactiveMetaNode(const char *metaNodeId)
418 {
419     return LnnDeactiveMetaNode(metaNodeId);
420 }
421 
LnnIpcGetAllMetaNodeInfo(MetaNodeInfo * infos,int32_t * infoNum)422 int32_t LnnIpcGetAllMetaNodeInfo(MetaNodeInfo *infos, int32_t *infoNum)
423 {
424     return LnnGetAllMetaNodeInfo(infos, infoNum);
425 }
426 
LnnIpcShiftLNNGear(const char * pkgName,const char * callerId,const char * targetNetworkId,const GearMode * mode)427 int32_t LnnIpcShiftLNNGear(const char *pkgName, const char *callerId, const char *targetNetworkId,
428     const GearMode *mode)
429 {
430     return LnnShiftLNNGear(pkgName, callerId, targetNetworkId, mode);
431 }
432 
LnnIpcNotifyJoinResult(void * addr,uint32_t addrTypeLen,const char * networkId,int32_t retCode)433 int32_t LnnIpcNotifyJoinResult(void *addr, uint32_t addrTypeLen, const char *networkId,
434     int32_t retCode)
435 {
436     if (addr == nullptr) {
437         return SOFTBUS_INVALID_PARAM;
438     }
439     ConnectionAddr *connAddr = reinterpret_cast<ConnectionAddr *>(addr);
440     std::lock_guard<std::mutex> autoLock(g_lock);
441     std::vector<JoinLnnRequestInfo *>::iterator iter;
442     for (iter = g_joinLNNRequestInfo.begin(); iter != g_joinLNNRequestInfo.end();) {
443         if (!LnnIsSameConnectionAddr(connAddr, &(*iter)->addr, false)) {
444             ++iter;
445             continue;
446         }
447         PkgNameAndPidInfo info;
448         info.pid = (*iter)->pid;
449         if (strcpy_s(info.pkgName, PKG_NAME_SIZE_MAX, (*iter)->pkgName) != EOK) {
450             LNN_LOGE(LNN_EVENT, "strcpy_s fail");
451             continue;
452         }
453         ClientOnJoinLNNResult(&info, addr, addrTypeLen, networkId, retCode);
454         delete *iter;
455         iter = g_joinLNNRequestInfo.erase(iter);
456     }
457     return SOFTBUS_OK;
458 }
459 
LnnIpcNotifyLeaveResult(const char * networkId,int32_t retCode)460 int32_t LnnIpcNotifyLeaveResult(const char *networkId, int32_t retCode)
461 {
462     if (networkId == nullptr) {
463         return SOFTBUS_INVALID_PARAM;
464     }
465     std::lock_guard<std::mutex> autoLock(g_lock);
466     std::vector<LeaveLnnRequestInfo *>::iterator iter;
467     for (iter = g_leaveLNNRequestInfo.begin(); iter != g_leaveLNNRequestInfo.end();) {
468         if (strncmp(networkId, (*iter)->networkId, strlen(networkId))) {
469             ++iter;
470             continue;
471         }
472         ClientOnLeaveLNNResult((*iter)->pkgName, (*iter)->pid, networkId, retCode);
473         delete *iter;
474         iter = g_leaveLNNRequestInfo.erase(iter);
475     }
476     return SOFTBUS_OK;
477 }
478 
LnnIpcNotifyOnlineState(bool isOnline,void * info,uint32_t infoTypeLen)479 int32_t LnnIpcNotifyOnlineState(bool isOnline, void *info, uint32_t infoTypeLen)
480 {
481     return ClinetOnNodeOnlineStateChanged(isOnline, info, infoTypeLen);
482 }
483 
LnnIpcNotifyBasicInfoChanged(void * info,uint32_t infoTypeLen,int32_t type)484 int32_t LnnIpcNotifyBasicInfoChanged(void *info, uint32_t infoTypeLen, int32_t type)
485 {
486     return ClinetOnNodeBasicInfoChanged(info, infoTypeLen, type);
487 }
488 
LnnIpcNotifyNodeStatusChanged(void * info,uint32_t infoTypeLen,int32_t type)489 int32_t LnnIpcNotifyNodeStatusChanged(void *info, uint32_t infoTypeLen, int32_t type)
490 {
491     return ClientOnNodeStatusChanged(info, infoTypeLen, type);
492 }
493 
LnnIpcLocalNetworkIdChanged(void)494 int32_t LnnIpcLocalNetworkIdChanged(void)
495 {
496     return ClinetOnLocalNetworkIdChanged();
497 }
498 
LnnIpcNotifyDeviceNotTrusted(const char * msg)499 int32_t LnnIpcNotifyDeviceNotTrusted(const char *msg)
500 {
501     return ClinetNotifyDeviceNotTrusted(msg);
502 }
503 
LnnIpcNotifyHichainProofException(const char * proofInfo,uint32_t proofLen,uint16_t deviceTypeId,int32_t errCode)504 int32_t LnnIpcNotifyHichainProofException(
505     const char *proofInfo, uint32_t proofLen, uint16_t deviceTypeId, int32_t errCode)
506 {
507     return ClientNotifyHichainProofException(proofInfo, proofLen, deviceTypeId, errCode);
508 }
509 
LnnIpcNotifyTimeSyncResult(const char * pkgName,int32_t pid,const void * info,uint32_t infoTypeLen,int32_t retCode)510 int32_t LnnIpcNotifyTimeSyncResult(const char *pkgName, int32_t pid, const void *info,
511     uint32_t infoTypeLen, int32_t retCode)
512 {
513     return ClientOnTimeSyncResult(pkgName, pid, info, infoTypeLen, retCode);
514 }
515 
RemoveJoinRequestInfoByPkgName(const char * pkgName)516 static void RemoveJoinRequestInfoByPkgName(const char *pkgName)
517 {
518     std::lock_guard<std::mutex> autoLock(g_lock);
519     std::vector<JoinLnnRequestInfo *>::iterator iter;
520     for (iter = g_joinLNNRequestInfo.begin(); iter != g_joinLNNRequestInfo.end();) {
521         if (strncmp(pkgName, (*iter)->pkgName, strlen(pkgName))) {
522             ++iter;
523             continue;
524         }
525         delete *iter;
526         iter = g_joinLNNRequestInfo.erase(iter);
527     }
528 }
529 
RemoveLeaveRequestInfoByPkgName(const char * pkgName)530 static void RemoveLeaveRequestInfoByPkgName(const char *pkgName)
531 {
532     std::lock_guard<std::mutex> autoLock(g_lock);
533     std::vector<LeaveLnnRequestInfo *>::iterator iter;
534     for (iter = g_leaveLNNRequestInfo.begin(); iter != g_leaveLNNRequestInfo.end();) {
535         if (strncmp(pkgName, (*iter)->pkgName, strlen(pkgName))) {
536             ++iter;
537             continue;
538         }
539         delete *iter;
540         iter = g_leaveLNNRequestInfo.erase(iter);
541     }
542 }
543 
RemoveRefreshRequestInfoByPkgName(const char * pkgName)544 static void RemoveRefreshRequestInfoByPkgName(const char *pkgName)
545 {
546     std::lock_guard<std::mutex> autoLock(g_lock);
547     std::vector<RefreshLnnRequestInfo *>::iterator iter;
548     for (iter = g_refreshLnnRequestInfo.begin(); iter != g_refreshLnnRequestInfo.end();) {
549         if (strncmp(pkgName, (*iter)->pkgName, strlen(pkgName)) != 0) {
550             ++iter;
551             continue;
552         }
553         delete *iter;
554         iter = g_refreshLnnRequestInfo.erase(iter);
555     }
556 }
557 
BusCenterServerDeathCallback(const char * pkgName)558 void BusCenterServerDeathCallback(const char *pkgName)
559 {
560     if (pkgName == nullptr) {
561         return;
562     }
563     RemoveJoinRequestInfoByPkgName(pkgName);
564     RemoveLeaveRequestInfoByPkgName(pkgName);
565     RemoveRefreshRequestInfoByPkgName(pkgName);
566 }
567