1 /*
2  * Copyright (c) 2021 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 <securec.h>
19 #include <string.h>
20 
21 #include "bus_center_client_proxy.h"
22 #include "bus_center_manager.h"
23 #include "lnn_connection_addr_utils.h"
24 #include "lnn_distributed_net_ledger.h"
25 #include "lnn_heartbeat_ctrl.h"
26 #include "lnn_log.h"
27 #include "lnn_meta_node_ledger.h"
28 #include "lnn_time_sync_manager.h"
29 #include "softbus_adapter_mem.h"
30 #include "softbus_def.h"
31 #include "softbus_errcode.h"
32 #include "softbus_utils.h"
33 
34 typedef struct {
35     ListNode node;
36     char pkgName[PKG_NAME_SIZE_MAX];
37     ConnectionAddr addr;
38 } JoinLnnRequestInfo;
39 
40 typedef struct {
41     ListNode node;
42     char pkgName[PKG_NAME_SIZE_MAX];
43     char networkId[NETWORK_ID_BUF_LEN];
44 } LeaveLnnRequestInfo;
45 
46 typedef struct {
47     SoftBusList *joinLNNRequestInfo;
48     SoftBusList *leaveLNNRequestInfo;
49     SoftBusMutex lock;
50 } LNNRequestInfo;
51 
52 static LNNRequestInfo g_lnnRequestInfo;
53 
LnnIpcInit(void)54 int32_t LnnIpcInit(void)
55 {
56     g_lnnRequestInfo.joinLNNRequestInfo = NULL;
57     g_lnnRequestInfo.leaveLNNRequestInfo = NULL;
58     if (SoftBusMutexInit(&g_lnnRequestInfo.lock, NULL) != SOFTBUS_OK) {
59         LNN_LOGE(LNN_EVENT, "lock init fail");
60         return SOFTBUS_LOCK_ERR;
61     }
62 
63     return SOFTBUS_OK;
64 }
65 
LnnIpcDeinit(void)66 void LnnIpcDeinit(void)
67 {
68     (void)SoftBusMutexDestroy(&g_lnnRequestInfo.lock);
69 }
70 
71 static int32_t OnRefreshDeviceFound(const char *pkgName, const DeviceInfo *device,
72     const InnerDeviceInfoAddtions *additions);
73 
74 static IServerDiscInnerCallback g_discInnerCb = {
75     .OnServerDeviceFound = OnRefreshDeviceFound,
76 };
77 
FindJoinLNNRequest(ConnectionAddr * addr)78 static JoinLnnRequestInfo *FindJoinLNNRequest(ConnectionAddr *addr)
79 {
80     JoinLnnRequestInfo *info = NULL;
81     SoftBusList *list = g_lnnRequestInfo.joinLNNRequestInfo;
82 
83     if (list == NULL) {
84         LNN_LOGE(LNN_EVENT, "request info list empty");
85         return NULL;
86     }
87     LIST_FOR_EACH_ENTRY(info, &list->list, JoinLnnRequestInfo, node) {
88         if (LnnIsSameConnectionAddr(addr, &info->addr, false)) {
89             return info;
90         }
91     }
92     return NULL;
93 }
94 
FindLeaveLNNRequest(const char * networkId)95 static LeaveLnnRequestInfo *FindLeaveLNNRequest(const char *networkId)
96 {
97     LeaveLnnRequestInfo *info = NULL;
98     SoftBusList *list = g_lnnRequestInfo.leaveLNNRequestInfo;
99 
100     if (list == NULL) {
101         LNN_LOGE(LNN_EVENT, "request info list empty");
102         return NULL;
103     }
104     LIST_FOR_EACH_ENTRY(info, &list->list, LeaveLnnRequestInfo, node) {
105         if (strncmp(networkId, info->networkId, strlen(networkId)) == 0) {
106             return info;
107         }
108     }
109     return NULL;
110 }
111 
IsRepeatJoinLNNRequest(const char * pkgName,const ConnectionAddr * addr)112 static bool IsRepeatJoinLNNRequest(const char *pkgName, const ConnectionAddr *addr)
113 {
114     JoinLnnRequestInfo *info = NULL;
115     SoftBusList *list = g_lnnRequestInfo.joinLNNRequestInfo;
116 
117     if (list == NULL) {
118         LNN_LOGE(LNN_EVENT, "request info list empty");
119         return SOFTBUS_ERR;
120     }
121     LIST_FOR_EACH_ENTRY(info, &list->list, JoinLnnRequestInfo, node) {
122         if (strncmp(pkgName, info->pkgName, strlen(pkgName)) != 0) {
123             continue;
124         }
125         if (LnnIsSameConnectionAddr(addr, &info->addr, false)) {
126             return true;
127         }
128     }
129     return false;
130 }
131 
AddJoinLNNInfo(const char * pkgName,const ConnectionAddr * addr)132 static int32_t AddJoinLNNInfo(const char *pkgName, const ConnectionAddr *addr)
133 {
134     SoftBusList *list = g_lnnRequestInfo.joinLNNRequestInfo;
135     JoinLnnRequestInfo *info = (JoinLnnRequestInfo *)SoftBusMalloc(sizeof(JoinLnnRequestInfo));
136     if (info == NULL) {
137         LNN_LOGE(LNN_EVENT, "request info malloc fail");
138         return SOFTBUS_MALLOC_ERR;
139     }
140     ListInit(&info->node);
141     if (strncpy_s(info->pkgName, PKG_NAME_SIZE_MAX, pkgName, strlen(pkgName)) != EOK) {
142         LNN_LOGE(LNN_EVENT, "copy pkgName fail");
143         SoftBusFree(info);
144         return SOFTBUS_MEM_ERR;
145     }
146     info->addr = *addr;
147     ListAdd(&list->list, &info->node);
148     list->cnt++;
149     return SOFTBUS_OK;
150 }
151 
IsRepeatLeaveLNNRequest(const char * pkgName,const char * networkId)152 static bool IsRepeatLeaveLNNRequest(const char *pkgName, const char *networkId)
153 {
154     LeaveLnnRequestInfo *info = NULL;
155     SoftBusList *list = g_lnnRequestInfo.leaveLNNRequestInfo;
156     LIST_FOR_EACH_ENTRY(info, &list->list, LeaveLnnRequestInfo, node) {
157         if (strncmp(pkgName, info->pkgName, strlen(pkgName)) != 0) {
158             continue;
159         }
160         if (strncmp(networkId, info->networkId, strlen(networkId)) == 0) {
161             return true;
162         }
163     }
164     return false;
165 }
166 
AddLeaveLNNInfo(const char * pkgName,const char * networkId)167 static int32_t AddLeaveLNNInfo(const char *pkgName, const char *networkId)
168 {
169     SoftBusList *list = g_lnnRequestInfo.leaveLNNRequestInfo;
170     LeaveLnnRequestInfo *info = (LeaveLnnRequestInfo *)SoftBusMalloc(sizeof(LeaveLnnRequestInfo));
171     if (info == NULL) {
172         LNN_LOGE(LNN_EVENT, "malloc request info fail");
173         return SOFTBUS_MALLOC_ERR;
174     }
175     ListInit(&info->node);
176     if (strncpy_s(info->pkgName, PKG_NAME_SIZE_MAX, pkgName, strlen(pkgName)) != EOK) {
177         LNN_LOGE(LNN_EVENT, "copy pkgName fail");
178         SoftBusFree(info);
179         return SOFTBUS_STRCPY_ERR;
180     }
181     if (strncpy_s(info->networkId, NETWORK_ID_BUF_LEN, networkId, strlen(networkId)) != EOK) {
182         LNN_LOGE(LNN_EVENT, "copy networkId fail");
183         SoftBusFree(info);
184         return SOFTBUS_STRCPY_ERR;
185     }
186     ListAdd(&list->list, &info->node);
187     list->cnt++;
188     return SOFTBUS_OK;
189 }
190 
OnRefreshDeviceFound(const char * pkgName,const DeviceInfo * device,const InnerDeviceInfoAddtions * additions)191 static int32_t OnRefreshDeviceFound(const char *pkgName, const DeviceInfo *device,
192     const InnerDeviceInfoAddtions *additions)
193 {
194     DeviceInfo newDevice;
195     if (memcpy_s(&newDevice, sizeof(DeviceInfo), device, sizeof(DeviceInfo)) != EOK) {
196         LNN_LOGE(LNN_EVENT, "copy new device info error");
197         return SOFTBUS_MEM_ERR;
198     }
199     LnnRefreshDeviceOnlineStateAndDevIdInfo(pkgName, &newDevice, additions);
200     return ClientOnRefreshDeviceFound(pkgName, 0, &newDevice, sizeof(DeviceInfo));
201 }
202 
LnnIpcServerJoin(const char * pkgName,int32_t callingPid,void * addr,uint32_t addrTypeLen)203 int32_t LnnIpcServerJoin(const char *pkgName, int32_t callingPid, void *addr, uint32_t addrTypeLen)
204 {
205     (void)callingPid;
206     ConnectionAddr *connAddr = (ConnectionAddr *)addr;
207 
208     if (pkgName == NULL || connAddr == NULL) {
209         LNN_LOGE(LNN_EVENT, "parameter is null");
210         return SOFTBUS_INVALID_PARAM;
211     }
212     if (addrTypeLen != sizeof(ConnectionAddr)) {
213         LNN_LOGE(LNN_EVENT, "addr is invalid");
214         return SOFTBUS_INVALID_PARAM;
215     }
216     if (SoftBusMutexLock(&g_lnnRequestInfo.lock) != 0) {
217         LNN_LOGE(LNN_EVENT, "get lock fail");
218     }
219     if (g_lnnRequestInfo.joinLNNRequestInfo == NULL) {
220         g_lnnRequestInfo.joinLNNRequestInfo = CreateSoftBusList();
221         if (g_lnnRequestInfo.joinLNNRequestInfo == NULL) {
222             LNN_LOGE(LNN_EVENT, "lnn request Info is null");
223             (void)SoftBusMutexUnlock(&g_lnnRequestInfo.lock);
224             return SOFTBUS_MALLOC_ERR;
225         }
226     }
227     if (IsRepeatJoinLNNRequest(pkgName, connAddr)) {
228         LNN_LOGE(LNN_EVENT, "repeat join lnn request pkgName=%{public}s", pkgName);
229         (void)SoftBusMutexUnlock(&g_lnnRequestInfo.lock);
230         return SOFTBUS_ALREADY_EXISTED;
231     }
232     int32_t ret = LnnServerJoin(connAddr, pkgName);
233     if (ret == SOFTBUS_OK) {
234         ret = AddJoinLNNInfo(pkgName, connAddr);
235     }
236     if (SoftBusMutexUnlock(&g_lnnRequestInfo.lock) != 0) {
237         LNN_LOGE(LNN_EVENT, "release lock fail");
238     }
239     return ret;
240 }
241 
LnnIpcServerLeave(const char * pkgName,int32_t callingPid,const char * networkId)242 int32_t LnnIpcServerLeave(const char *pkgName, int32_t callingPid, const char *networkId)
243 {
244     (void)callingPid;
245     if (pkgName == NULL || networkId == NULL) {
246         LNN_LOGE(LNN_EVENT, "parameter is NULL");
247         return SOFTBUS_INVALID_PARAM;
248     }
249     if (SoftBusMutexLock(&g_lnnRequestInfo.lock) != 0) {
250         LNN_LOGE(LNN_EVENT, "get lock fail");
251     }
252     if (g_lnnRequestInfo.leaveLNNRequestInfo == NULL) {
253         g_lnnRequestInfo.leaveLNNRequestInfo = CreateSoftBusList();
254         if (g_lnnRequestInfo.leaveLNNRequestInfo == NULL) {
255             LNN_LOGE(LNN_EVENT, "request info is null");
256             (void)SoftBusMutexUnlock(&g_lnnRequestInfo.lock);
257             return SOFTBUS_INVALID_PARAM;
258         }
259     }
260     if (IsRepeatLeaveLNNRequest(pkgName, networkId)) {
261         LNN_LOGE(LNN_EVENT, "repeat leave lnn request pkgName=%{public}s", pkgName);
262         (void)SoftBusMutexUnlock(&g_lnnRequestInfo.lock);
263         return SOFTBUS_ALREADY_EXISTED;
264     }
265     int32_t ret = LnnServerLeave(networkId, pkgName);
266     if (ret == SOFTBUS_OK) {
267         ret = AddLeaveLNNInfo(pkgName, networkId);
268     }
269     if (SoftBusMutexUnlock(&g_lnnRequestInfo.lock) != 0) {
270         LNN_LOGE(LNN_EVENT, "release lock fail");
271     }
272     return ret;
273 }
274 
LnnIpcGetAllOnlineNodeInfo(const char * pkgName,void ** info,uint32_t infoTypeLen,int32_t * infoNum)275 int32_t LnnIpcGetAllOnlineNodeInfo(const char *pkgName, void **info, uint32_t infoTypeLen,
276     int32_t *infoNum)
277 {
278     (void)pkgName;
279     if (infoTypeLen != sizeof(NodeBasicInfo)) {
280         LNN_LOGE(LNN_EVENT, "infoTypeLen is invalid, infoTypeLen=%{public}d", infoTypeLen);
281         return SOFTBUS_INVALID_PARAM;
282     }
283     return LnnGetAllOnlineNodeInfo((NodeBasicInfo **)info, infoNum);
284 }
285 
LnnIpcGetLocalDeviceInfo(const char * pkgName,void * info,uint32_t infoTypeLen)286 int32_t LnnIpcGetLocalDeviceInfo(const char *pkgName, void *info, uint32_t infoTypeLen)
287 {
288     (void)pkgName;
289     (void)infoTypeLen;
290     return LnnGetLocalDeviceInfo((NodeBasicInfo *)info);
291 }
292 
LnnIpcGetNodeKeyInfo(const char * pkgName,const char * networkId,int32_t key,unsigned char * buf,uint32_t len)293 int32_t LnnIpcGetNodeKeyInfo(const char *pkgName, const char *networkId, int32_t key, unsigned char *buf,
294     uint32_t len)
295 {
296     if (key == NODE_KEY_BLE_OFFLINE_CODE) {
297         LNN_LOGE(LNN_EVENT, "the process has been abandoned");
298         return SOFTBUS_INVALID_PARAM;
299     }
300     (void)pkgName;
301     return LnnGetNodeKeyInfo(networkId, key, buf, len);
302 }
303 
LnnIpcSetNodeDataChangeFlag(const char * pkgName,const char * networkId,uint16_t dataChangeFlag)304 int32_t LnnIpcSetNodeDataChangeFlag(const char *pkgName, const char *networkId,
305     uint16_t dataChangeFlag)
306 {
307     (void)pkgName;
308     return LnnSetNodeDataChangeFlag(networkId, dataChangeFlag);
309 }
310 
LnnIpcGetNodeKeyInfoLen(int32_t key)311 int32_t LnnIpcGetNodeKeyInfoLen(int32_t key)
312 {
313     return LnnGetNodeKeyInfoLen(key);
314 }
315 
LnnIpcStartTimeSync(const char * pkgName,int32_t callingPid,const char * targetNetworkId,int32_t accuracy,int32_t period)316 int32_t LnnIpcStartTimeSync(const char *pkgName,  int32_t callingPid, const char *targetNetworkId,
317     int32_t accuracy, int32_t period)
318 {
319     return LnnStartTimeSync(pkgName, callingPid, targetNetworkId, (TimeSyncAccuracy)accuracy, (TimeSyncPeriod)period);
320 }
321 
LnnIpcStopTimeSync(const char * pkgName,const char * targetNetworkId,int32_t callingPid)322 int32_t LnnIpcStopTimeSync(const char *pkgName, const char *targetNetworkId, int32_t callingPid)
323 {
324     return LnnStopTimeSync(pkgName, targetNetworkId, callingPid);
325 }
326 
LnnIpcPublishLNN(const char * pkgName,const PublishInfo * info)327 int32_t LnnIpcPublishLNN(const char *pkgName, const PublishInfo *info)
328 {
329     return LnnPublishService(pkgName, info, false);
330 }
331 
LnnIpcStopPublishLNN(const char * pkgName,int32_t publishId)332 int32_t LnnIpcStopPublishLNN(const char *pkgName, int32_t publishId)
333 {
334     return LnnUnPublishService(pkgName, publishId, false);
335 }
336 
LnnIpcRefreshLNN(const char * pkgName,int32_t callingPid,const SubscribeInfo * info)337 int32_t LnnIpcRefreshLNN(const char *pkgName, int32_t callingPid, const SubscribeInfo *info)
338 {
339     (void)callingPid;
340     InnerCallback callback = {
341         .serverCb = g_discInnerCb,
342     };
343     return LnnStartDiscDevice(pkgName, info, &callback, false);
344 }
345 
LnnIpcStopRefreshLNN(const char * pkgName,int32_t callingPid,int32_t refreshId)346 int32_t LnnIpcStopRefreshLNN(const char *pkgName, int32_t callingPid, int32_t refreshId)
347 {
348     (void)callingPid;
349     return LnnStopDiscDevice(pkgName, refreshId, false);
350 }
351 
LnnIpcActiveMetaNode(const MetaNodeConfigInfo * info,char * metaNodeId)352 int32_t LnnIpcActiveMetaNode(const MetaNodeConfigInfo *info, char *metaNodeId)
353 {
354     return LnnActiveMetaNode(info, metaNodeId);
355 }
356 
LnnIpcDeactiveMetaNode(const char * metaNodeId)357 int32_t LnnIpcDeactiveMetaNode(const char *metaNodeId)
358 {
359     return LnnDeactiveMetaNode(metaNodeId);
360 }
361 
LnnIpcGetAllMetaNodeInfo(MetaNodeInfo * infos,int32_t * infoNum)362 int32_t LnnIpcGetAllMetaNodeInfo(MetaNodeInfo *infos, int32_t *infoNum)
363 {
364     return LnnGetAllMetaNodeInfo(infos, infoNum);
365 }
366 
LnnIpcShiftLNNGear(const char * pkgName,const char * callerId,const char * targetNetworkId,const GearMode * mode)367 int32_t LnnIpcShiftLNNGear(const char *pkgName, const char *callerId, const char *targetNetworkId,
368     const GearMode *mode)
369 {
370     return LnnShiftLNNGear(pkgName, callerId, targetNetworkId, mode);
371 }
372 
LnnIpcNotifyJoinResult(void * addr,uint32_t addrTypeLen,const char * networkId,int32_t retCode)373 int32_t LnnIpcNotifyJoinResult(void *addr, uint32_t addrTypeLen, const char *networkId,
374     int32_t retCode)
375 {
376     if (addr == NULL) {
377         return SOFTBUS_INVALID_PARAM;
378     }
379     ConnectionAddr *connAddr = (ConnectionAddr *)addr;
380     JoinLnnRequestInfo *info = NULL;
381     SoftBusList *list = g_lnnRequestInfo.joinLNNRequestInfo;
382     if (list == NULL) {
383         LNN_LOGE(LNN_EVENT, "request info is null");
384         return SOFTBUS_ERR;
385     }
386     if (SoftBusMutexLock(&g_lnnRequestInfo.lock) != 0) {
387         LNN_LOGE(LNN_EVENT, "get lock fail");
388     }
389     while ((info = FindJoinLNNRequest(connAddr)) != NULL) {
390         ListDelete(&info->node);
391         ClientOnJoinLNNResult(info->pkgName, connAddr, addrTypeLen, networkId, retCode);
392         --list->cnt;
393         SoftBusFree(info);
394     }
395     if (SoftBusMutexUnlock(&g_lnnRequestInfo.lock) != 0) {
396         LNN_LOGE(LNN_EVENT, "release lock fail");
397     }
398     return SOFTBUS_OK;
399 }
400 
LnnIpcNotifyLeaveResult(const char * networkId,int32_t retCode)401 int32_t LnnIpcNotifyLeaveResult(const char *networkId, int32_t retCode)
402 {
403     if (networkId == NULL) {
404         return SOFTBUS_INVALID_PARAM;
405     }
406     LeaveLnnRequestInfo *info = NULL;
407     SoftBusList *list = g_lnnRequestInfo.leaveLNNRequestInfo;
408     if (list == NULL) {
409         LNN_LOGE(LNN_EVENT, "request info is null");
410         return SOFTBUS_ERR;
411     }
412     if (SoftBusMutexLock(&g_lnnRequestInfo.lock) != 0) {
413         LNN_LOGE(LNN_EVENT, "get lock fail");
414     }
415 
416     while ((info = FindLeaveLNNRequest(networkId)) != NULL) {
417         ListDelete(&info->node);
418         ClientOnLeaveLNNResult(info->pkgName, networkId, retCode);
419         --list->cnt;
420         SoftBusFree(info);
421     }
422     if (SoftBusMutexUnlock(&g_lnnRequestInfo.lock) != 0) {
423         LNN_LOGE(LNN_EVENT, "release lock fail");
424     }
425     return SOFTBUS_OK;
426 }
427 
LnnIpcNotifyOnlineState(bool isOnline,void * info,uint32_t infoTypeLen)428 int32_t LnnIpcNotifyOnlineState(bool isOnline, void *info, uint32_t infoTypeLen)
429 {
430     return ClinetOnNodeOnlineStateChanged(isOnline, info, infoTypeLen);
431 }
432 
LnnIpcNotifyBasicInfoChanged(void * info,uint32_t infoTypeLen,int32_t type)433 int32_t LnnIpcNotifyBasicInfoChanged(void *info, uint32_t infoTypeLen, int32_t type)
434 {
435     return ClinetOnNodeBasicInfoChanged(info, infoTypeLen, type);
436 }
437 
LnnIpcNotifyNodeStatusChanged(void * info,uint32_t infoTypeLen,int32_t type)438 int32_t LnnIpcNotifyNodeStatusChanged(void *info, uint32_t infoTypeLen, int32_t type)
439 {
440     (void)info;
441     (void)infoTypeLen;
442     (void)type;
443     LNN_LOGI(LNN_EVENT, "not implement");
444     return SOFTBUS_OK;
445 }
446 
LnnIpcLocalNetworkIdChanged(void)447 int32_t LnnIpcLocalNetworkIdChanged(void)
448 {
449     LNN_LOGI(LNN_EVENT, "not implement");
450     return SOFTBUS_OK;
451 }
452 
LnnIpcNotifyDeviceNotTrusted(const char * msg)453 int32_t LnnIpcNotifyDeviceNotTrusted(const char *msg)
454 {
455     LNN_LOGI(LNN_EVENT, "not implement");
456     return SOFTBUS_OK;
457 }
458 
LnnIpcNotifyHichainProofException(const char * proofInfo,uint32_t proofLen,uint16_t deviceTypeId,int32_t errCode)459 int32_t LnnIpcNotifyHichainProofException(
460     const char *proofInfo, uint32_t proofLen, uint16_t deviceTypeId, int32_t errCode)
461 {
462     (void)proofInfo;
463     (void)proofLen;
464     (void)deviceTypeId;
465     (void)errCode;
466     LNN_LOGI(LNN_EVENT, "not implement");
467     return SOFTBUS_OK;
468 }
469 
LnnIpcNotifyTimeSyncResult(const char * pkgName,int32_t pid,const void * info,uint32_t infoTypeLen,int32_t retCode)470 int32_t LnnIpcNotifyTimeSyncResult(const char *pkgName, int32_t pid, const void *info,
471     uint32_t infoTypeLen, int32_t retCode)
472 {
473     return ClientOnTimeSyncResult(pkgName, pid, info, infoTypeLen, retCode);
474 }
475 
BusCenterServerDeathCallback(const char * pkgName)476 void BusCenterServerDeathCallback(const char *pkgName)
477 {
478     (void)pkgName;
479 }
480