• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "client_bus_center_manager.h"
17 
18 #include <pthread.h>
19 #include <securec.h>
20 
21 #include "bus_center_server_proxy.h"
22 #include "common_list.h"
23 #include "softbus_adapter_mem.h"
24 #include "softbus_adapter_thread.h"
25 #include "softbus_errcode.h"
26 #include "softbus_feature_config.h"
27 #include "softbus_log.h"
28 
29 #define DEFAULT_NODE_STATE_CB_CNT 10
30 #define MAX_IPC_LEN 512
31 
32 static int32_t g_maxNodeStateCbCount;
33 
34 typedef struct {
35     ListNode node;
36     ConnectionAddr addr;
37     OnJoinLNNResult cb;
38 } JoinLNNCbListItem;
39 
40 typedef struct {
41     ListNode node;
42     char networkId[NETWORK_ID_BUF_LEN];
43     OnLeaveLNNResult cb;
44 } LeaveLNNCbListItem;
45 
46 typedef struct {
47     ListNode node;
48     char networkId[NETWORK_ID_BUF_LEN];
49     ITimeSyncCb cb;
50 } TimeSyncCallbackItem;
51 
52 typedef struct {
53     ListNode node;
54     INodeStateCb cb;
55 } NodeStateCallbackItem;
56 
57 typedef struct {
58     ListNode joinLNNCbList;
59     ListNode leaveLNNCbList;
60     ListNode nodeStateCbList;
61     ListNode timeSyncCbList;
62     int32_t nodeStateCbListCnt;
63     IPublishCb publishCb;
64     IRefreshCallback refreshCb;
65     bool isInit;
66     SoftBusMutex lock;
67 } BusCenterClient;
68 
69 static BusCenterClient g_busCenterClient = {
70     .nodeStateCbListCnt = 0,
71     .publishCb.OnPublishResult = NULL,
72     .refreshCb.OnDeviceFound = NULL,
73     .refreshCb.OnDiscoverResult = NULL,
74     .isInit = false,
75 };
76 
IsSameConnectionAddr(const ConnectionAddr * addr1,const ConnectionAddr * addr2)77 static bool IsSameConnectionAddr(const ConnectionAddr *addr1, const ConnectionAddr *addr2)
78 {
79     if (addr1->type != addr2->type) {
80         return false;
81     }
82     if (addr1->type == CONNECTION_ADDR_BR) {
83         return strncmp(addr1->info.br.brMac, addr2->info.br.brMac, BT_MAC_LEN) == 0;
84     }
85     if (addr1->type == CONNECTION_ADDR_BLE) {
86         return strncmp(addr1->info.ble.bleMac, addr2->info.ble.bleMac, BT_MAC_LEN) == 0;
87     }
88     if (addr1->type == CONNECTION_ADDR_WLAN || addr1->type == CONNECTION_ADDR_ETH) {
89         return (strncmp(addr1->info.ip.ip, addr2->info.ip.ip, strlen(addr1->info.ip.ip)) == 0)
90             && (addr1->info.ip.port == addr2->info.ip.port);
91     }
92     return false;
93 }
94 
FindJoinLNNCbItem(ConnectionAddr * addr,OnJoinLNNResult cb)95 static JoinLNNCbListItem *FindJoinLNNCbItem(ConnectionAddr *addr, OnJoinLNNResult cb)
96 {
97     JoinLNNCbListItem *item = NULL;
98 
99     LIST_FOR_EACH_ENTRY(item, &g_busCenterClient.joinLNNCbList, JoinLNNCbListItem, node) {
100         if (IsSameConnectionAddr(&item->addr, addr) &&
101             (cb == NULL || cb == item->cb)) {
102             return item;
103         }
104     }
105     return NULL;
106 }
107 
AddJoinLNNCbItem(ConnectionAddr * target,OnJoinLNNResult cb)108 static int32_t AddJoinLNNCbItem(ConnectionAddr *target, OnJoinLNNResult cb)
109 {
110     JoinLNNCbListItem *item = NULL;
111 
112     item = (JoinLNNCbListItem *)SoftBusMalloc(sizeof(*item));
113     if (item == NULL) {
114         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fail: malloc join LNN cb list item");
115         return SOFTBUS_MALLOC_ERR;
116     }
117     ListInit(&item->node);
118     item->addr = *target;
119     item->cb = cb;
120     ListAdd(&g_busCenterClient.joinLNNCbList, &item->node);
121     return SOFTBUS_OK;
122 }
123 
FindLeaveLNNCbItem(const char * networkId,OnLeaveLNNResult cb)124 static LeaveLNNCbListItem *FindLeaveLNNCbItem(const char *networkId, OnLeaveLNNResult cb)
125 {
126     LeaveLNNCbListItem *item = NULL;
127 
128     LIST_FOR_EACH_ENTRY(item, &g_busCenterClient.leaveLNNCbList, LeaveLNNCbListItem, node) {
129         if (strcmp(item->networkId, networkId) == 0 &&
130             (cb == NULL || cb == item->cb)) {
131             return item;
132         }
133     }
134     return NULL;
135 }
136 
AddLeaveLNNCbItem(const char * networkId,OnLeaveLNNResult cb)137 static int32_t AddLeaveLNNCbItem(const char *networkId, OnLeaveLNNResult cb)
138 {
139     LeaveLNNCbListItem *item = NULL;
140 
141     item = (LeaveLNNCbListItem *)SoftBusMalloc(sizeof(*item));
142     if (item == NULL) {
143         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fail: malloc join LNN cb list item");
144         return SOFTBUS_MALLOC_ERR;
145     }
146     ListInit(&item->node);
147     if (strncpy_s(item->networkId, NETWORK_ID_BUF_LEN, networkId, strlen(networkId)) != EOK) {
148         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "strcpy network id fail");
149         SoftBusFree(item);
150         return SOFTBUS_ERR;
151     }
152     item->cb = cb;
153     ListAdd(&g_busCenterClient.leaveLNNCbList, &item->node);
154     return SOFTBUS_OK;
155 }
156 
FindTimeSyncCbItem(const char * networkId,ITimeSyncCb * cb)157 static TimeSyncCallbackItem *FindTimeSyncCbItem(const char *networkId, ITimeSyncCb *cb)
158 {
159     TimeSyncCallbackItem *item = NULL;
160 
161     LIST_FOR_EACH_ENTRY(item, &g_busCenterClient.timeSyncCbList, TimeSyncCallbackItem, node) {
162         if (strcmp(item->networkId, networkId) == 0 &&
163             (cb == NULL || cb->onTimeSyncResult == item->cb.onTimeSyncResult)) {
164             return item;
165         }
166     }
167     return NULL;
168 }
169 
AddTimeSyncCbItem(const char * networkId,ITimeSyncCb * cb)170 static int32_t AddTimeSyncCbItem(const char *networkId, ITimeSyncCb *cb)
171 {
172     TimeSyncCallbackItem *item = NULL;
173 
174     item = (TimeSyncCallbackItem *)SoftBusMalloc(sizeof(*item));
175     if (item == NULL) {
176         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fail: malloc time sync cb list item");
177         return SOFTBUS_MALLOC_ERR;
178     }
179     ListInit(&item->node);
180     if (strncpy_s(item->networkId, NETWORK_ID_BUF_LEN, networkId, strlen(networkId)) != EOK) {
181         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "strcpy network id fail");
182         SoftBusFree(item);
183         return SOFTBUS_ERR;
184     }
185     item->cb = *cb;
186     ListAdd(&g_busCenterClient.timeSyncCbList, &item->node);
187     return SOFTBUS_OK;
188 }
189 
ClearJoinLNNList(void)190 static void ClearJoinLNNList(void)
191 {
192     JoinLNNCbListItem *item = NULL;
193     JoinLNNCbListItem *next = NULL;
194 
195     LIST_FOR_EACH_ENTRY_SAFE(item, next, &g_busCenterClient.joinLNNCbList, JoinLNNCbListItem, node) {
196         ListDelete(&item->node);
197         SoftBusFree(item);
198     }
199 }
200 
ClearLeaveLNNList(void)201 static void ClearLeaveLNNList(void)
202 {
203     LeaveLNNCbListItem *item = NULL;
204     LeaveLNNCbListItem *next = NULL;
205 
206     LIST_FOR_EACH_ENTRY_SAFE(item, next, &g_busCenterClient.leaveLNNCbList, LeaveLNNCbListItem, node) {
207         ListDelete(&item->node);
208         SoftBusFree(item);
209     }
210 }
211 
ClearTimeSyncList(ListNode * list)212 static void ClearTimeSyncList(ListNode *list)
213 {
214     TimeSyncCallbackItem *item = NULL;
215     TimeSyncCallbackItem *next = NULL;
216 
217     LIST_FOR_EACH_ENTRY_SAFE(item, next, list, TimeSyncCallbackItem, node) {
218         ListDelete(&item->node);
219         SoftBusFree(item);
220     }
221 }
222 
ClearNodeStateCbList(ListNode * list)223 static void ClearNodeStateCbList(ListNode *list)
224 {
225     NodeStateCallbackItem *item = NULL;
226     NodeStateCallbackItem *next = NULL;
227 
228     LIST_FOR_EACH_ENTRY_SAFE(item, next, list, NodeStateCallbackItem, node) {
229         ListDelete(&item->node);
230         SoftBusFree(item);
231     }
232 }
233 
DuplicateNodeStateCbList(ListNode * list)234 static void DuplicateNodeStateCbList(ListNode *list)
235 {
236     NodeStateCallbackItem *item = NULL;
237     NodeStateCallbackItem *copyItem = NULL;
238 
239     LIST_FOR_EACH_ENTRY(item, &g_busCenterClient.nodeStateCbList, NodeStateCallbackItem, node) {
240         copyItem = SoftBusMalloc(sizeof(NodeStateCallbackItem));
241         if (copyItem == NULL) {
242             SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "malloc node state callback item fail");
243             continue;
244         }
245         ListInit(&copyItem->node);
246         copyItem->cb = item->cb;
247         ListAdd(list, &copyItem->node);
248     }
249 }
250 
DuplicateTimeSyncResultCbList(ListNode * list,const char * networkId)251 static void DuplicateTimeSyncResultCbList(ListNode *list, const char *networkId)
252 {
253     TimeSyncCallbackItem *item = NULL;
254     TimeSyncCallbackItem *copyItem = NULL;
255 
256     LIST_FOR_EACH_ENTRY(item, &g_busCenterClient.timeSyncCbList, TimeSyncCallbackItem, node) {
257         if (strcmp(item->networkId, networkId) != 0) {
258             continue;
259         }
260         copyItem = SoftBusMalloc(sizeof(TimeSyncCallbackItem));
261         if (copyItem == NULL) {
262             SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "malloc time sync callback item fail");
263             continue;
264         }
265         copyItem->cb = item->cb;
266         ListInit(&copyItem->node);
267         if (strncpy_s(copyItem->networkId, NETWORK_ID_BUF_LEN, item->networkId, strlen(item->networkId)) != EOK) {
268             SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "copy networkId fail");
269             SoftBusFree(copyItem);
270             continue;
271         }
272         ListAdd(list, &copyItem->node);
273     }
274 }
275 
BusCenterClientDeinit(void)276 void BusCenterClientDeinit(void)
277 {
278     if (SoftBusMutexLock(&g_busCenterClient.lock) != 0) {
279         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fail: lock in deinit");
280     }
281     ClearJoinLNNList();
282     ClearLeaveLNNList();
283     ClearTimeSyncList(&g_busCenterClient.timeSyncCbList);
284     ClearNodeStateCbList(&g_busCenterClient.nodeStateCbList);
285     g_busCenterClient.nodeStateCbListCnt = 0;
286     if (SoftBusMutexUnlock(&g_busCenterClient.lock) != 0) {
287         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fail: unlock in deinit");
288     }
289     BusCenterServerProxyDeInit();
290 }
291 
BusCenterClientInit(void)292 int BusCenterClientInit(void)
293 {
294     if (SoftbusGetConfig(SOFTBUS_INT_MAX_NODE_STATE_CB_CNT,
295         (unsigned char*)&g_maxNodeStateCbCount, sizeof(g_maxNodeStateCbCount)) != SOFTBUS_OK) {
296         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "Cannot get NodeStateCbCount from config file");
297         g_maxNodeStateCbCount = DEFAULT_NODE_STATE_CB_CNT;
298     }
299     SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "NodeStateCbCount is %u", g_maxNodeStateCbCount);
300 
301     if (SoftBusMutexInit(&g_busCenterClient.lock, NULL) != SOFTBUS_OK) {
302         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "g_busCenterClient.lock init failed.");
303         return SOFTBUS_ERR;
304     }
305 
306     ListInit(&g_busCenterClient.joinLNNCbList);
307     ListInit(&g_busCenterClient.leaveLNNCbList);
308     ListInit(&g_busCenterClient.nodeStateCbList);
309     ListInit(&g_busCenterClient.timeSyncCbList);
310     g_busCenterClient.isInit = true;
311     if (BusCenterServerProxyInit() != SOFTBUS_OK) {
312         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "bus center server proxy init failed.");
313         BusCenterClientDeinit();
314         return SOFTBUS_ERR;
315     }
316     SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "BusCenterClientInit init OK!");
317     return SOFTBUS_OK;
318 }
319 
GetAllNodeDeviceInfoInner(const char * pkgName,NodeBasicInfo ** info,int32_t * infoNum)320 int32_t GetAllNodeDeviceInfoInner(const char *pkgName, NodeBasicInfo **info, int32_t *infoNum)
321 {
322     int ret = ServerIpcGetAllOnlineNodeInfo(pkgName, (void **)info, sizeof(NodeBasicInfo), infoNum);
323     if (ret != SOFTBUS_OK) {
324         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "Server GetAllOnlineNodeInfo failed, ret = %d", ret);
325     }
326     return ret;
327 }
328 
GetLocalNodeDeviceInfoInner(const char * pkgName,NodeBasicInfo * info)329 int32_t GetLocalNodeDeviceInfoInner(const char *pkgName, NodeBasicInfo *info)
330 {
331     int ret = ServerIpcGetLocalDeviceInfo(pkgName, info, sizeof(*info));
332     if (ret != SOFTBUS_OK) {
333         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "Server GetLocalNodeDeviceInfo failed, ret = %d", ret);
334     }
335     return ret;
336 }
337 
GetNodeKeyInfoInner(const char * pkgName,const char * networkId,NodeDeviceInfoKey key,uint8_t * info,int32_t infoLen)338 int32_t GetNodeKeyInfoInner(const char *pkgName, const char *networkId, NodeDeviceInfoKey key,
339     uint8_t *info, int32_t infoLen)
340 {
341     int ret = ServerIpcGetNodeKeyInfo(pkgName, networkId, key, info, infoLen);
342     if (ret != SOFTBUS_OK) {
343         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "Server GetNodeKeyInfo failed, ret = %d", ret);
344     }
345     return ret;
346 }
347 
JoinLNNInner(const char * pkgName,ConnectionAddr * target,OnJoinLNNResult cb)348 int32_t JoinLNNInner(const char *pkgName, ConnectionAddr *target, OnJoinLNNResult cb)
349 {
350     int32_t rc;
351 
352     if (!g_busCenterClient.isInit) {
353         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fail : join lnn not init");
354         return SOFTBUS_NO_INIT;
355     }
356     if (SoftBusMutexLock(&g_busCenterClient.lock) != 0) {
357         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fail: lock join lnn cb list in join");
358     }
359     rc = SOFTBUS_ERR;
360     do {
361         if (FindJoinLNNCbItem(target, cb) != NULL) {
362             SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fail : join request already exist");
363             rc = SOFTBUS_ALREADY_EXISTED;
364             break;
365         }
366         rc = ServerIpcJoinLNN(pkgName, target, sizeof(*target));
367         if (rc != SOFTBUS_OK) {
368             SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fail : request join lnn");
369         } else {
370             rc = AddJoinLNNCbItem(target, cb);
371         }
372     } while (false);
373     if (SoftBusMutexUnlock(&g_busCenterClient.lock) != 0) {
374         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fail: unlock join lnn cb list in join");
375     }
376     return rc;
377 }
378 
LeaveLNNInner(const char * pkgName,const char * networkId,OnLeaveLNNResult cb)379 int32_t LeaveLNNInner(const char *pkgName, const char *networkId, OnLeaveLNNResult cb)
380 {
381     int32_t rc;
382 
383     if (!g_busCenterClient.isInit) {
384         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fail : leave lnn not init");
385         return SOFTBUS_NO_INIT;
386     }
387     if (SoftBusMutexLock(&g_busCenterClient.lock) != 0) {
388         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fail: lock leave lnn cb list in leave");
389     }
390     rc = SOFTBUS_ERR;
391     do {
392         if (FindLeaveLNNCbItem(networkId, cb) != NULL) {
393             SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fail : leave request already exist");
394             break;
395         }
396         rc = ServerIpcLeaveLNN(pkgName, networkId);
397         if (rc != SOFTBUS_OK) {
398             SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fail : request leave lnn");
399         } else {
400             rc = AddLeaveLNNCbItem(networkId, cb);
401         }
402     } while (false);
403     if (SoftBusMutexUnlock(&g_busCenterClient.lock) != 0) {
404         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fail: unlock leave lnn cb list in leave");
405     }
406     return rc;
407 }
408 
IsSameNodeStateCb(const INodeStateCb * callback1,const INodeStateCb * callback2)409 static bool IsSameNodeStateCb(const INodeStateCb *callback1, const INodeStateCb *callback2)
410 {
411     if (callback1->events != callback2->events) {
412         return false;
413     }
414     if ((callback1->events & EVENT_NODE_STATE_ONLINE) &&
415         callback1->onNodeOnline != callback2->onNodeOnline) {
416         return false;
417     }
418     if ((callback1->events & EVENT_NODE_STATE_OFFLINE) &&
419         callback1->onNodeOffline != callback2->onNodeOffline) {
420         return false;
421     }
422     if ((callback1->events & EVENT_NODE_STATE_INFO_CHANGED) &&
423         callback1->onNodeBasicInfoChanged != callback2->onNodeBasicInfoChanged) {
424         return false;
425     }
426     return true;
427 }
428 
RegNodeDeviceStateCbInner(const char * pkgName,INodeStateCb * callback)429 int32_t RegNodeDeviceStateCbInner(const char *pkgName, INodeStateCb *callback)
430 {
431     NodeStateCallbackItem *item = NULL;
432     int32_t rc = SOFTBUS_ERR;
433 
434     if (!g_busCenterClient.isInit) {
435         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fail: reg node state cb not init");
436         return SOFTBUS_ERR;
437     }
438     if (SoftBusMutexLock(&g_busCenterClient.lock) != 0) {
439         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fail: lock node state cb list in reg");
440     }
441     LIST_FOR_EACH_ENTRY(item, &g_busCenterClient.nodeStateCbList, NodeStateCallbackItem, node) {
442         if (IsSameNodeStateCb(&item->cb, callback)) {
443             (void)SoftBusMutexUnlock(&g_busCenterClient.lock);
444             SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "warn: reg node state callback repeatedly");
445             return SOFTBUS_OK;
446         }
447     }
448     do {
449         if (g_busCenterClient.nodeStateCbListCnt >= g_maxNodeStateCbCount) {
450             break;
451         }
452         item = (NodeStateCallbackItem *)SoftBusMalloc(sizeof(*item));
453         if (item == NULL) {
454             rc = SOFTBUS_MALLOC_ERR;
455             break;
456         }
457         ListInit(&item->node);
458         item->cb = *callback;
459         ListAdd(&g_busCenterClient.nodeStateCbList, &item->node);
460         g_busCenterClient.nodeStateCbListCnt++;
461         rc = SOFTBUS_OK;
462     } while (false);
463     if (SoftBusMutexUnlock(&g_busCenterClient.lock) != 0) {
464         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fail: unlock node state cb list");
465     }
466     return rc;
467 }
468 
UnregNodeDeviceStateCbInner(INodeStateCb * callback)469 int32_t UnregNodeDeviceStateCbInner(INodeStateCb *callback)
470 {
471     NodeStateCallbackItem *item = NULL;
472     NodeStateCallbackItem *next = NULL;
473 
474     if (!g_busCenterClient.isInit) {
475         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fail: unreg node state cb not init");
476         return SOFTBUS_ERR;
477     }
478     if (SoftBusMutexLock(&g_busCenterClient.lock) != 0) {
479         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fail: lock node state cb list in unreg");
480     }
481     LIST_FOR_EACH_ENTRY_SAFE(item, next, &g_busCenterClient.nodeStateCbList, NodeStateCallbackItem, node) {
482         if (IsSameNodeStateCb(&item->cb, callback)) {
483             ListDelete(&item->node);
484             SoftBusFree(item);
485             g_busCenterClient.nodeStateCbListCnt--;
486             break;
487         }
488     }
489     if (SoftBusMutexUnlock(&g_busCenterClient.lock) != 0) {
490         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fail: unlock node state cb list in unreg");
491     }
492     return SOFTBUS_OK;
493 }
494 
StartTimeSyncInner(const char * pkgName,const char * targetNetworkId,TimeSyncAccuracy accuracy,TimeSyncPeriod period,ITimeSyncCb * cb)495 int32_t StartTimeSyncInner(const char *pkgName, const char *targetNetworkId, TimeSyncAccuracy accuracy,
496     TimeSyncPeriod period, ITimeSyncCb *cb)
497 {
498     int32_t rc;
499 
500     if (!g_busCenterClient.isInit) {
501         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fail : start time sync not init");
502         return SOFTBUS_ERR;
503     }
504     if (SoftBusMutexLock(&g_busCenterClient.lock) != 0) {
505         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fail: lock time sync cb list");
506     }
507     rc = SOFTBUS_ERR;
508     do {
509         if (FindTimeSyncCbItem(targetNetworkId, cb) != NULL) {
510             SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "repeat request from %s, StopTimeSync first!", pkgName);
511             break;
512         }
513         rc = ServerIpcStartTimeSync(pkgName, targetNetworkId, accuracy, period);
514         if (rc != SOFTBUS_OK) {
515             SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fail : start time sync");
516         } else {
517             rc = AddTimeSyncCbItem(targetNetworkId, cb);
518         }
519     } while (false);
520     if (SoftBusMutexUnlock(&g_busCenterClient.lock) != 0) {
521         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fail: unlock time sync cb list");
522     }
523     return rc;
524 }
525 
StopTimeSyncInner(const char * pkgName,const char * targetNetworkId)526 int32_t StopTimeSyncInner(const char *pkgName, const char *targetNetworkId)
527 {
528     int32_t rc;
529     TimeSyncCallbackItem *item = NULL;
530 
531     if (!g_busCenterClient.isInit) {
532         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fail : stop time sync cb list not init");
533         return SOFTBUS_ERR;
534     }
535     if (SoftBusMutexLock(&g_busCenterClient.lock) != 0) {
536         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fail: lock time sync cb list");
537     }
538     rc = SOFTBUS_ERR;
539     while ((item = FindTimeSyncCbItem(targetNetworkId, NULL)) != NULL) {
540         rc = ServerIpcStopTimeSync(pkgName, targetNetworkId);
541         if (rc != SOFTBUS_OK) {
542             SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fail : stop time sync");
543         } else {
544             ListDelete(&item->node);
545             SoftBusFree(item);
546         }
547     }
548 
549     if (SoftBusMutexUnlock(&g_busCenterClient.lock) != 0) {
550         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fail: unlock time sync cb list");
551     }
552     return rc;
553 }
554 
PublishLNNInner(const char * pkgName,const PublishInfo * info,const IPublishCb * cb)555 int32_t PublishLNNInner(const char *pkgName, const PublishInfo *info, const IPublishCb *cb)
556 {
557     g_busCenterClient.publishCb = *cb;
558     int32_t ret = ServerIpcPublishLNN(pkgName, info);
559     if (ret != SOFTBUS_OK) {
560         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "Server PublishLNNInner failed, ret = %d", ret);
561     }
562     return ret;
563 }
564 
StopPublishLNNInner(const char * pkgName,int32_t publishId)565 int32_t StopPublishLNNInner(const char *pkgName, int32_t publishId)
566 {
567     int32_t ret = ServerIpcStopPublishLNN(pkgName, publishId);
568     if (ret != SOFTBUS_OK) {
569         SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, "Server StopPublishLNNInner failed, ret = %d", ret);
570     }
571     return ret;
572 }
573 
RefreshLNNInner(const char * pkgName,const SubscribeInfo * info,const IRefreshCallback * cb)574 int32_t RefreshLNNInner(const char *pkgName, const SubscribeInfo *info, const IRefreshCallback *cb)
575 {
576     g_busCenterClient.refreshCb = *cb;
577     int32_t ret = ServerIpcRefreshLNN(pkgName, info);
578     if (ret != SOFTBUS_OK) {
579         SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, "Server RefreshLNNInner failed, ret = %d", ret);
580     }
581     return ret;
582 }
583 
StopRefreshLNNInner(const char * pkgName,int32_t refreshId)584 int32_t StopRefreshLNNInner(const char *pkgName, int32_t refreshId)
585 {
586     int32_t ret = ServerIpcStopRefreshLNN(pkgName, refreshId);
587     if (ret != SOFTBUS_OK) {
588         SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, "Server StopRefreshLNNInner failed, ret = %d", ret);
589     }
590     return ret;
591 }
592 
ActiveMetaNodeInner(const char * pkgName,const MetaNodeConfigInfo * info,char * metaNodeId)593 int32_t ActiveMetaNodeInner(const char *pkgName, const MetaNodeConfigInfo *info, char *metaNodeId)
594 {
595     return ServerIpcActiveMetaNode(pkgName, info, metaNodeId);
596 }
597 
DeactiveMetaNodeInner(const char * pkgName,const char * metaNodeId)598 int32_t DeactiveMetaNodeInner(const char *pkgName, const char *metaNodeId)
599 {
600     return ServerIpcDeactiveMetaNode(pkgName, metaNodeId);
601 }
602 
GetAllMetaNodeInfoInner(const char * pkgName,MetaNodeInfo * infos,int32_t * infoNum)603 int32_t GetAllMetaNodeInfoInner(const char *pkgName, MetaNodeInfo *infos, int32_t *infoNum)
604 {
605     return ServerIpcGetAllMetaNodeInfo(pkgName, infos, infoNum);
606 }
607 
LnnOnJoinResult(void * addr,const char * networkId,int32_t retCode)608 int32_t LnnOnJoinResult(void *addr, const char *networkId, int32_t retCode)
609 {
610     JoinLNNCbListItem *item = NULL;
611     ConnectionAddr *connAddr = (ConnectionAddr *)addr;
612 
613     if (connAddr == NULL) {
614         return SOFTBUS_INVALID_PARAM;
615     }
616     if (!g_busCenterClient.isInit) {
617         return SOFTBUS_ERR;
618     }
619 
620     if (SoftBusMutexLock(&g_busCenterClient.lock) != 0) {
621         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fail: lock join lnn cb list in join result");
622     }
623     while ((item = FindJoinLNNCbItem(addr, NULL)) != NULL) {
624         ListDelete(&item->node);
625         if (SoftBusMutexUnlock(&g_busCenterClient.lock) != 0) {
626             SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fail: unlock join lnn cb list in join result");
627         }
628         if (item->cb != NULL) {
629             item->cb(connAddr, networkId, retCode);
630         }
631         SoftBusFree(item);
632         if (SoftBusMutexLock(&g_busCenterClient.lock) != 0) {
633             SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fail: lock join lnn cb list in join result");
634         }
635     }
636     if (SoftBusMutexUnlock(&g_busCenterClient.lock) != 0) {
637         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fail: unlock join lnn cb list in join result");
638     }
639     return SOFTBUS_OK;
640 }
641 
LnnOnLeaveResult(const char * networkId,int32_t retCode)642 int32_t LnnOnLeaveResult(const char *networkId, int32_t retCode)
643 {
644     LeaveLNNCbListItem *item = NULL;
645 
646     if (networkId == NULL) {
647         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fail: networkId is null");
648         return SOFTBUS_INVALID_PARAM;
649     }
650     if (!g_busCenterClient.isInit) {
651         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fail: leave cb not init");
652         return SOFTBUS_ERR;
653     }
654 
655     if (SoftBusMutexLock(&g_busCenterClient.lock) != 0) {
656         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fail: lock leave lnn cb list in leave result");
657     }
658     while ((item = FindLeaveLNNCbItem(networkId, NULL)) != NULL) {
659         ListDelete(&item->node);
660         if (SoftBusMutexUnlock(&g_busCenterClient.lock) != 0) {
661             SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fail: unlock leave lnn cb list in leave result");
662         }
663         if (item->cb != NULL) {
664             item->cb(networkId, retCode);
665         }
666         SoftBusFree(item);
667         if (SoftBusMutexLock(&g_busCenterClient.lock) != 0) {
668             SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fail: lock leave lnn cb list in leave result");
669         }
670     }
671     if (SoftBusMutexUnlock(&g_busCenterClient.lock) != 0) {
672         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fail: unlock leave lnn cb list in leave result");
673     }
674     return SOFTBUS_OK;
675 }
676 
LnnOnNodeOnlineStateChanged(bool isOnline,void * info)677 int32_t LnnOnNodeOnlineStateChanged(bool isOnline, void *info)
678 {
679     NodeStateCallbackItem *item = NULL;
680     NodeBasicInfo *basicInfo = (NodeBasicInfo *)info;
681     ListNode dupList;
682 
683     if (basicInfo == NULL) {
684         return SOFTBUS_INVALID_PARAM;
685     }
686     if (!g_busCenterClient.isInit) {
687         return SOFTBUS_ERR;
688     }
689 
690     if (SoftBusMutexLock(&g_busCenterClient.lock) != 0) {
691         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fail: lock node state cb list in notify");
692     }
693     ListInit(&dupList);
694     DuplicateNodeStateCbList(&dupList);
695     if (SoftBusMutexUnlock(&g_busCenterClient.lock) != 0) {
696         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fail: unlock node state cb list in notify");
697     }
698     LIST_FOR_EACH_ENTRY(item, &dupList, NodeStateCallbackItem, node) {
699         if (isOnline == true) {
700             if ((item->cb.events & EVENT_NODE_STATE_ONLINE) != 0) {
701                 item->cb.onNodeOnline(basicInfo);
702             }
703         } else {
704             if ((item->cb.events & EVENT_NODE_STATE_OFFLINE) != 0) {
705                 item->cb.onNodeOffline(basicInfo);
706             }
707         }
708     }
709     ClearNodeStateCbList(&dupList);
710     return SOFTBUS_OK;
711 }
712 
LnnOnNodeBasicInfoChanged(void * info,int32_t type)713 int32_t LnnOnNodeBasicInfoChanged(void *info, int32_t type)
714 {
715     NodeStateCallbackItem *item = NULL;
716     NodeBasicInfo *basicInfo = (NodeBasicInfo *)info;
717     ListNode dupList;
718 
719     if (basicInfo == NULL) {
720         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "info or list is null");
721         return SOFTBUS_INVALID_PARAM;
722     }
723     if (!g_busCenterClient.isInit) {
724         return SOFTBUS_ERR;
725     }
726 
727     if ((type < 0) || (type > TYPE_DEVICE_NAME)) {
728         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "OnNodeBasicInfoChanged invalid type: %d", type);
729         return SOFTBUS_INVALID_PARAM;
730     }
731 
732     if (SoftBusMutexLock(&g_busCenterClient.lock) != 0) {
733         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fail: lock node basic info cb list in notify");
734     }
735     ListInit(&dupList);
736     DuplicateNodeStateCbList(&dupList);
737     if (SoftBusMutexUnlock(&g_busCenterClient.lock) != 0) {
738         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fail: unlock node basic info cb list in notify");
739     }
740     LIST_FOR_EACH_ENTRY(item, &dupList, NodeStateCallbackItem, node) {
741         if ((item->cb.events & EVENT_NODE_STATE_INFO_CHANGED) != 0) {
742             item->cb.onNodeBasicInfoChanged(type, basicInfo);
743         }
744     }
745     ClearNodeStateCbList(&dupList);
746     return SOFTBUS_OK;
747 }
748 
LnnOnTimeSyncResult(const void * info,int retCode)749 int32_t LnnOnTimeSyncResult(const void *info, int retCode)
750 {
751     TimeSyncCallbackItem *item = NULL;
752     TimeSyncResultInfo *basicInfo = (TimeSyncResultInfo *)info;
753     ListNode dupList;
754 
755     if (info == NULL) {
756         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "info or list is null");
757         return SOFTBUS_INVALID_PARAM;
758     }
759     if (!g_busCenterClient.isInit) {
760         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fail: time sync cb not init");
761         return SOFTBUS_ERR;
762     }
763 
764     if (SoftBusMutexLock(&g_busCenterClient.lock) != 0) {
765         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fail: lock time sync cb list in time sync result");
766     }
767     ListInit(&dupList);
768     DuplicateTimeSyncResultCbList(&dupList, basicInfo->target.targetNetworkId);
769     if (SoftBusMutexUnlock(&g_busCenterClient.lock) != 0) {
770         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fail: unlock time sync cb list in time sync result");
771     }
772     LIST_FOR_EACH_ENTRY(item, &dupList, TimeSyncCallbackItem, node) {
773         if (item->cb.onTimeSyncResult != NULL) {
774             item->cb.onTimeSyncResult(info, retCode);
775         }
776     }
777     ClearTimeSyncList(&dupList);
778     return SOFTBUS_OK;
779 }
780 
LnnOnPublishLNNResult(int32_t publishId,int32_t reason)781 void LnnOnPublishLNNResult(int32_t publishId, int32_t reason)
782 {
783     if (g_busCenterClient.publishCb.OnPublishResult != NULL) {
784         g_busCenterClient.publishCb.OnPublishResult(publishId, reason);
785     }
786 }
787 
LnnOnRefreshLNNResult(int32_t refreshId,int32_t reason)788 void LnnOnRefreshLNNResult(int32_t refreshId, int32_t reason)
789 {
790     if (g_busCenterClient.refreshCb.OnDiscoverResult != NULL) {
791         g_busCenterClient.refreshCb.OnDiscoverResult(refreshId, reason);
792     }
793 }
794 
LnnOnRefreshDeviceFound(const void * device)795 void LnnOnRefreshDeviceFound(const void *device)
796 {
797     if (g_busCenterClient.refreshCb.OnDeviceFound != NULL) {
798         g_busCenterClient.refreshCb.OnDeviceFound(device);
799     }
800 }
801