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 "bus_center_server_proxy.h"
17 
18 #include "securec.h"
19 
20 #include "iproxy_client.h"
21 #include "lnn_log.h"
22 #include "samgr_lite.h"
23 #include "serializer.h"
24 #include "softbus_adapter_file.h"
25 #include "softbus_adapter_mem.h"
26 #include "softbus_adapter_timer.h"
27 #include "softbus_def.h"
28 #include "softbus_errcode.h"
29 #include "softbus_feature_config.h"
30 #include "softbus_server_ipc_interface_code.h"
31 
32 #define WAIT_SERVER_READY_INTERVAL_COUNT 50
33 
34 typedef enum {
35     GET_ALL_ONLINE_NODE_INFO = 0,
36     GET_LOCAL_DEVICE_INFO,
37     GET_NODE_KEY_INFO,
38     ACTIVE_META_NODE,
39     DEACTIVE_META_NODE,
40     GET_ALL_META_NODE,
41     SHIFT_LNN_GEAR,
42     START_REFRESH_LNN,
43     START_PUBLISH_LNN,
44 } FunID;
45 
46 typedef struct {
47     FunID id;
48     int32_t arg1;
49     int32_t retCode;
50     void* data;
51     int32_t dataLen;
52 } Reply;
53 
54 typedef int32_t (*ClientBusCenterFunIdHandler)(Reply *, IpcIo *, uint32_t);
55 
56 typedef struct {
57     int32_t funIdType;
58     ClientBusCenterFunIdHandler funIdHandler;
59 } ClientBusCenterStateHandler;
60 
61 static int32_t OnOnlineNodeInfo(Reply *info, IpcIo *reply, uint32_t infoSize);
62 static int32_t OnLocalDeviceInfo(Reply *info, IpcIo *reply, uint32_t infoSize);
63 static int32_t OnNodeKeyInfo(Reply *info, IpcIo *reply, uint32_t infoSize);
64 static int32_t OnActiveMetaNode(Reply *info, IpcIo *reply, uint32_t infoSize);
65 static int32_t OnDeactiveMetaNode(Reply *info, IpcIo *reply, uint32_t infoSize);
66 static int32_t OnAllMetaNode(Reply *info, IpcIo *reply, uint32_t infoSize);
67 static int32_t OnShiftLnnGear(Reply *info, IpcIo *reply, uint32_t infoSize);
68 static int32_t OnStartRefreshLnn(Reply *info, IpcIo *reply, uint32_t infoSize);
69 static int32_t OnStartPublishLnn(Reply *info, IpcIo *reply, uint32_t infoSize);
70 
71 static ClientBusCenterStateHandler g_busCenterStateHandler[] = {
72     {GET_ALL_ONLINE_NODE_INFO, OnOnlineNodeInfo  },
73     { GET_LOCAL_DEVICE_INFO,   OnLocalDeviceInfo },
74     { GET_NODE_KEY_INFO,       OnNodeKeyInfo     },
75     { ACTIVE_META_NODE,        OnActiveMetaNode  },
76     { DEACTIVE_META_NODE,      OnDeactiveMetaNode},
77     { GET_ALL_META_NODE,       OnAllMetaNode     },
78     { SHIFT_LNN_GEAR,          OnShiftLnnGear    },
79     { START_REFRESH_LNN,       OnStartRefreshLnn },
80     { START_PUBLISH_LNN,       OnStartPublishLnn },
81 };
82 
83 static IClientProxy *g_serverProxy = NULL;
84 
OnOnlineNodeInfo(Reply * info,IpcIo * reply,uint32_t infoSize)85 static int32_t OnOnlineNodeInfo(Reply *info, IpcIo *reply, uint32_t infoSize)
86 {
87     ReadInt32(reply, &(info->arg1));
88     if (info->arg1 > 0) {
89         ReadUint32(reply, &infoSize);
90         info->data = (void *)ReadBuffer(reply, infoSize);
91     }
92     return SOFTBUS_OK;
93 }
94 
OnLocalDeviceInfo(Reply * info,IpcIo * reply,uint32_t infoSize)95 static int32_t OnLocalDeviceInfo(Reply *info, IpcIo *reply, uint32_t infoSize)
96 {
97     ReadInt32(reply, &infoSize);
98     info->dataLen = infoSize;
99     info->data = (void *)ReadBuffer(reply, infoSize);
100     return SOFTBUS_OK;
101 }
102 
OnNodeKeyInfo(Reply * info,IpcIo * reply,uint32_t infoSize)103 static int32_t OnNodeKeyInfo(Reply *info, IpcIo *reply, uint32_t infoSize)
104 {
105     ReadInt32(reply, &infoSize);
106     info->dataLen = infoSize;
107     info->data = (void *)ReadBuffer(reply, infoSize);
108     return SOFTBUS_OK;
109 }
110 
OnActiveMetaNode(Reply * info,IpcIo * reply,uint32_t infoSize)111 static int32_t OnActiveMetaNode(Reply *info, IpcIo *reply, uint32_t infoSize)
112 {
113     ReadInt32(reply, &(info->retCode));
114     if (info->retCode == SOFTBUS_OK) {
115         info->data = (void *)ReadString(reply, &infoSize);
116         if (infoSize != (NETWORK_ID_BUF_LEN - 1)) {
117             LNN_LOGE(LNN_EVENT, "invalid meta node id length=%{public}u", infoSize);
118             return SOFTBUS_ERR;
119         }
120     }
121     return SOFTBUS_OK;
122 }
123 
OnDeactiveMetaNode(Reply * info,IpcIo * reply,uint32_t infoSize)124 static int32_t OnDeactiveMetaNode(Reply *info, IpcIo *reply, uint32_t infoSize)
125 {
126     ReadInt32(reply, &(info->retCode));
127     return SOFTBUS_OK;
128 }
129 
OnAllMetaNode(Reply * info,IpcIo * reply,uint32_t infoSize)130 static int32_t OnAllMetaNode(Reply *info, IpcIo *reply, uint32_t infoSize)
131 {
132     ReadInt32(reply, &(info->retCode));
133     if (info->retCode == SOFTBUS_OK) {
134         ReadInt32(reply, &(info->arg1));
135         if (info->arg1 > 0) {
136             ReadUint32(reply, &infoSize);
137             info->data = (void *)ReadBuffer(reply, infoSize);
138         }
139     }
140     return SOFTBUS_OK;
141 }
142 
OnShiftLnnGear(Reply * info,IpcIo * reply,uint32_t infoSize)143 static int32_t OnShiftLnnGear(Reply *info, IpcIo *reply, uint32_t infoSize)
144 {
145     ReadInt32(reply, &(info->retCode));
146     return SOFTBUS_OK;
147 }
148 
OnStartRefreshLnn(Reply * info,IpcIo * reply,uint32_t infoSize)149 static int32_t OnStartRefreshLnn(Reply *info, IpcIo *reply, uint32_t infoSize)
150 {
151     ReadInt32(reply, &(info->retCode));
152     return SOFTBUS_OK;
153 }
154 
OnStartPublishLnn(Reply * info,IpcIo * reply,uint32_t infoSize)155 static int32_t OnStartPublishLnn(Reply *info, IpcIo *reply, uint32_t infoSize)
156 {
157     ReadInt32(reply, &(info->retCode));
158     return SOFTBUS_OK;
159 }
160 
ClientBusCenterResultCb(Reply * info,int32_t ret,IpcIo * reply)161 static int32_t ClientBusCenterResultCb(Reply *info, int32_t ret, IpcIo *reply)
162 {
163     if (ret != SOFTBUS_OK) {
164         LNN_LOGE(LNN_EVENT, "ClientBusCenterResultCb failed. ret=%{public}d", ret);
165         return SOFTBUS_ERR;
166     }
167     uint32_t infoSize;
168     uint32_t count = sizeof(g_busCenterStateHandler) / sizeof(ClientBusCenterStateHandler);
169     for (uint32_t i = 0; i < count; i++) {
170         if (g_busCenterStateHandler[i].funIdType == info->id) {
171             return (g_busCenterStateHandler[i].funIdHandler)(info, reply, infoSize);
172         }
173     }
174     LNN_LOGI(LNN_INIT, "funcId not exist");
175     return SOFTBUS_ERR;
176 }
177 
BusCenterServerProxyInit(void)178 int32_t BusCenterServerProxyInit(void)
179 {
180     if (g_serverProxy != NULL) {
181         LNN_LOGI(LNN_INIT, "server proxy has initialized");
182         return SOFTBUS_OK;
183     }
184 
185     LNN_LOGI(LNN_INIT, "bus center start get server proxy");
186     int32_t proxyInitCount = 0;
187     while (g_serverProxy == NULL) {
188         proxyInitCount++;
189         if (proxyInitCount == WAIT_SERVER_READY_INTERVAL_COUNT) {
190             LNN_LOGE(LNN_INIT, "bus center get server proxy error");
191             return SOFTBUS_ERR;
192         }
193 
194         IUnknown *iUnknown = SAMGR_GetInstance()->GetDefaultFeatureApi(SOFTBUS_SERVICE);
195         if (iUnknown == NULL) {
196             SoftBusSleepMs(WAIT_SERVER_READY_INTERVAL);
197             continue;
198         }
199 
200         int32_t ret = iUnknown->QueryInterface(iUnknown, CLIENT_PROXY_VER, (void **)&g_serverProxy);
201         if (ret != EC_SUCCESS || g_serverProxy == NULL) {
202             LNN_LOGE(LNN_INIT, "QueryInterface failed=%{public}d", ret);
203             SoftBusSleepMs(WAIT_SERVER_READY_INTERVAL);
204             continue;
205         }
206     }
207     LNN_LOGI(LNN_INIT, "bus center get server proxy ok");
208     return SOFTBUS_OK;
209 }
210 
BusCenterServerProxyDeInit(void)211 void BusCenterServerProxyDeInit(void)
212 {
213     g_serverProxy = NULL;
214 }
215 
ServerIpcGetAllOnlineNodeInfo(const char * pkgName,void ** info,uint32_t infoTypeLen,int32_t * infoNum)216 int32_t ServerIpcGetAllOnlineNodeInfo(const char *pkgName, void **info, uint32_t infoTypeLen, int32_t *infoNum)
217 {
218     if (info == NULL || infoNum == NULL) {
219         LNN_LOGW(LNN_EVENT, "Invalid param");
220         return SOFTBUS_INVALID_PARAM;
221     }
222     if (g_serverProxy == NULL) {
223         LNN_LOGE(LNN_EVENT, "g_serverProxy is NULL");
224         return SOFTBUS_ERR;
225     }
226     uint8_t data[MAX_SOFT_BUS_IPC_LEN] = {0};
227     IpcIo request = {0};
228     IpcIoInit(&request, data, MAX_SOFT_BUS_IPC_LEN, 0);
229     WriteString(&request, pkgName);
230     WriteUint32(&request, infoTypeLen);
231     Reply reply = {0};
232     reply.id = GET_ALL_ONLINE_NODE_INFO;
233     /* asynchronous invocation */
234     int32_t ans = g_serverProxy->Invoke(g_serverProxy, SERVER_GET_ALL_ONLINE_NODE_INFO, &request, &reply,
235         ClientBusCenterResultCb);
236     if (ans != SOFTBUS_OK) {
237         LNN_LOGE(LNN_EVENT, "invoke failed=%{public}d", ans);
238         return SOFTBUS_ERR;
239     }
240     uint32_t maxConnCount = UINT32_MAX;
241     (void)SoftbusGetConfig(SOFTBUS_INT_MAX_LNN_CONNECTION_CNT, (unsigned char *)&maxConnCount, sizeof(maxConnCount));
242     *infoNum = reply.arg1;
243     if (*infoNum < 0 || (uint32_t)(*infoNum) > maxConnCount) {
244         LNN_LOGE(LNN_EVENT, "invoke failed=%{public}d", *infoNum);
245         return SOFTBUS_ERR;
246     }
247     int32_t infoSize = (*infoNum) * (int32_t)infoTypeLen;
248     *info = NULL;
249     if (infoSize > 0) {
250         if (reply.data == NULL) {
251             LNN_LOGE(LNN_EVENT, "read node info failed");
252             return SOFTBUS_ERR;
253         }
254         *info = SoftBusMalloc(infoSize);
255         if (*info == NULL) {
256             LNN_LOGE(LNN_EVENT, "malloc failed");
257             return SOFTBUS_ERR;
258         }
259         if (memcpy_s(*info, infoSize, reply.data, infoSize) != EOK) {
260             LNN_LOGE(LNN_EVENT, "copy node info failed");
261             SoftBusFree(*info);
262             *info = NULL;
263             return SOFTBUS_ERR;
264         }
265     }
266     return SOFTBUS_OK;
267 }
268 
ServerIpcGetLocalDeviceInfo(const char * pkgName,void * info,uint32_t infoTypeLen)269 int32_t ServerIpcGetLocalDeviceInfo(const char *pkgName, void *info, uint32_t infoTypeLen)
270 {
271     if (info == NULL) {
272         return SOFTBUS_ERR;
273     }
274     if (g_serverProxy == NULL) {
275         LNN_LOGE(LNN_EVENT, "g_serverProxy is nullptr");
276         return SOFTBUS_ERR;
277     }
278 
279     uint8_t data[MAX_SOFT_BUS_IPC_LEN] = {0};
280     IpcIo request = {0};
281     IpcIoInit(&request, data, MAX_SOFT_BUS_IPC_LEN, 0);
282     WriteString(&request, pkgName);
283     WriteUint32(&request, infoTypeLen);
284     Reply reply = {0};
285     reply.id = GET_LOCAL_DEVICE_INFO;
286     /* asynchronous invocation */
287     int32_t ans = g_serverProxy->Invoke(g_serverProxy, SERVER_GET_LOCAL_DEVICE_INFO, &request, &reply,
288         ClientBusCenterResultCb);
289     if (ans != SOFTBUS_OK) {
290         LNN_LOGE(LNN_EVENT, "invoke failed=%{public}d", ans);
291         return SOFTBUS_ERR;
292     }
293     if (reply.data == NULL) {
294         LNN_LOGE(LNN_EVENT, "read node info failed");
295         return SOFTBUS_ERR;
296     }
297     if (memcpy_s(info, infoTypeLen, reply.data, infoTypeLen) != EOK) {
298         LNN_LOGE(LNN_EVENT, "copy node info failed");
299         return SOFTBUS_ERR;
300     }
301     return SOFTBUS_OK;
302 }
303 
ServerIpcGetNodeKeyInfo(const char * pkgName,const char * networkId,int32_t key,unsigned char * buf,uint32_t len)304 int32_t ServerIpcGetNodeKeyInfo(const char *pkgName, const char *networkId, int32_t key, unsigned char *buf,
305     uint32_t len)
306 {
307     if (networkId == NULL || buf == NULL) {
308         LNN_LOGW(LNN_EVENT, "params are nullptr");
309         return SOFTBUS_ERR;
310     }
311     if (g_serverProxy == NULL) {
312         LNN_LOGW(LNN_EVENT, "g_serverProxy is nullptr");
313         return SOFTBUS_ERR;
314     }
315 
316     uint8_t data[MAX_SOFT_BUS_IPC_LEN] = {0};
317     IpcIo request = {0};
318     IpcIoInit(&request, data, MAX_SOFT_BUS_IPC_LEN, 0);
319     WriteString(&request, pkgName);
320     WriteString(&request, networkId);
321     WriteInt32(&request, key);
322     WriteUint32(&request, len);
323     Reply reply = {0};
324     reply.id = GET_NODE_KEY_INFO;
325     /* asynchronous invocation */
326     int32_t ans = g_serverProxy->Invoke(g_serverProxy, SERVER_GET_NODE_KEY_INFO, &request, &reply,
327         ClientBusCenterResultCb);
328     if (ans != SOFTBUS_OK) {
329         LNN_LOGE(LNN_EVENT, "GetNodeKeyInfo invoke failed=%{public}d", ans);
330         return SOFTBUS_ERR;
331     }
332     if (reply.data == NULL || reply.dataLen <= 0 || (uint32_t)reply.dataLen > len) {
333         LNN_LOGE(LNN_EVENT,
334             "GetNodeKeyInfo read retBuf failed, inlen=%{public}u, reply.dataLen=%{public}d", len, reply.dataLen);
335         return SOFTBUS_ERR;
336     }
337     if (memcpy_s(buf, len, reply.data, reply.dataLen) != EOK) {
338         LNN_LOGE(LNN_EVENT, "GetNodeKeyInfo copy node key info failed");
339         return SOFTBUS_ERR;
340     }
341     return SOFTBUS_OK;
342 }
343 
ServerIpcSetNodeDataChangeFlag(const char * pkgName,const char * networkId,uint16_t dataChangeFlag)344 int32_t ServerIpcSetNodeDataChangeFlag(const char *pkgName, const char *networkId, uint16_t dataChangeFlag)
345 {
346     if (networkId == NULL) {
347         LNN_LOGE(LNN_EVENT, "params are nullptr");
348         return SOFTBUS_ERR;
349     }
350     if (g_serverProxy == NULL) {
351         LNN_LOGE(LNN_EVENT, "g_serverProxy is nullptr");
352         return SOFTBUS_ERR;
353     }
354 
355     uint8_t data[MAX_SOFT_BUS_IPC_LEN] = {0};
356     IpcIo request = {0};
357     IpcIoInit(&request, data, MAX_SOFT_BUS_IPC_LEN, 0);
358     WriteString(&request, pkgName);
359     WriteString(&request, networkId);
360     WriteInt16(&request, dataChangeFlag);
361     int32_t ans = g_serverProxy->Invoke(g_serverProxy, SERVER_SET_NODE_DATA_CHANGE_FLAG, &request, NULL, NULL);
362     if (ans != SOFTBUS_OK) {
363         LNN_LOGE(LNN_EVENT, "invoke failed=%{public}d", ans);
364         return SOFTBUS_ERR;
365     }
366     return SOFTBUS_OK;
367 }
368 
ServerIpcRegDataLevelChangeCb(const char * pkgName)369 int32_t ServerIpcRegDataLevelChangeCb(const char *pkgName)
370 {
371     if (pkgName == NULL) {
372         LNN_LOGE(LNN_EVENT, "params are nullptr");
373         return SOFTBUS_ERR;
374     }
375     if (g_serverProxy == NULL) {
376         LNN_LOGE(LNN_EVENT, "g_serverProxy is nullptr");
377         return SOFTBUS_ERR;
378     }
379     IpcIo request = {0};
380     return g_serverProxy->Invoke(g_serverProxy, SERVER_REG_DATA_LEVEL_CHANGE_CB, &request, NULL, NULL);
381 }
382 
ServerIpcUnregDataLevelChangeCb(const char * pkgName)383 int32_t ServerIpcUnregDataLevelChangeCb(const char *pkgName)
384 {
385     if (pkgName == NULL) {
386         LNN_LOGE(LNN_EVENT, "params are nullptr");
387         return SOFTBUS_ERR;
388     }
389     if (g_serverProxy == NULL) {
390         LNN_LOGE(LNN_EVENT, "g_serverProxy is nullptr");
391         return SOFTBUS_ERR;
392     }
393     IpcIo request = {0};
394     return g_serverProxy->Invoke(g_serverProxy, SERVER_UNREG_DATA_LEVEL_CHANGE_CB, &request, NULL, NULL);
395 }
396 
ServerIpcSetDataLevel(const DataLevel * dataLevel)397 int32_t ServerIpcSetDataLevel(const DataLevel *dataLevel)
398 {
399     if (dataLevel == NULL) {
400         LNN_LOGE(LNN_EVENT, "params are nullptr");
401         return SOFTBUS_ERR;
402     }
403     if (g_serverProxy == NULL) {
404         LNN_LOGE(LNN_EVENT, "g_serverProxy is nullptr");
405         return SOFTBUS_ERR;
406     }
407     IpcIo request = {0};
408     return g_serverProxy->Invoke(g_serverProxy, SERVER_SET_DATA_LEVEL, &request, NULL, NULL);
409 }
410 
ServerIpcJoinLNN(const char * pkgName,void * addr,uint32_t addrTypeLen)411 int32_t ServerIpcJoinLNN(const char *pkgName, void *addr, uint32_t addrTypeLen)
412 {
413     LNN_LOGD(LNN_EVENT, "join Lnn ipc client push");
414     if (addr == NULL || pkgName == NULL) {
415         LNN_LOGW(LNN_EVENT, "Invalid param");
416         return SOFTBUS_INVALID_PARAM;
417     }
418     if (g_serverProxy == NULL) {
419         LNN_LOGE(LNN_EVENT, "g_serverProxy is nullptr");
420         return SOFTBUS_ERR;
421     }
422     uint8_t data[MAX_SOFT_BUS_IPC_LEN] = {0};
423     IpcIo request = {0};
424     IpcIoInit(&request, data, MAX_SOFT_BUS_IPC_LEN, 0);
425     WriteString(&request, pkgName);
426     WriteUint32(&request, addrTypeLen);
427     WriteBuffer(&request, addr, addrTypeLen);
428     /* asynchronous invocation */
429     int32_t ans = g_serverProxy->Invoke(g_serverProxy, SERVER_JOIN_LNN, &request, NULL, NULL);
430     if (ans != SOFTBUS_OK) {
431         LNN_LOGE(LNN_EVENT, "join Lnn invoke failed=%{public}d", ans);
432         return SOFTBUS_ERR;
433     }
434     return SOFTBUS_OK;
435 }
436 
ServerIpcLeaveLNN(const char * pkgName,const char * networkId)437 int32_t ServerIpcLeaveLNN(const char *pkgName, const char *networkId)
438 {
439     LNN_LOGD(LNN_EVENT, "leave Lnn ipc client push");
440     if (pkgName == NULL || networkId == NULL) {
441         LNN_LOGW(LNN_EVENT, "Invalid param");
442         return SOFTBUS_INVALID_PARAM;
443     }
444     if (g_serverProxy == NULL) {
445         LNN_LOGE(LNN_EVENT, "ServerIpcLeaveLNN g_serverProxy is nullptr!\n");
446         return SOFTBUS_ERR;
447     }
448     uint8_t data[MAX_SOFT_BUS_IPC_LEN] = {0};
449     IpcIo request = {0};
450     IpcIoInit(&request, data, MAX_SOFT_BUS_IPC_LEN, 0);
451     WriteString(&request, pkgName);
452     WriteString(&request, networkId);
453     /* asynchronous invocation */
454     int32_t ans = g_serverProxy->Invoke(g_serverProxy, SERVER_LEAVE_LNN, &request, NULL, NULL);
455     if (ans != SOFTBUS_OK) {
456         LNN_LOGE(LNN_EVENT, "leave Lnn invoke failed=%{public}d", ans);
457         return SOFTBUS_ERR;
458     }
459     return SOFTBUS_OK;
460 }
461 
ServerIpcStartTimeSync(const char * pkgName,const char * targetNetworkId,int32_t accuracy,int32_t period)462 int32_t ServerIpcStartTimeSync(const char *pkgName, const char *targetNetworkId, int32_t accuracy, int32_t period)
463 {
464     LNN_LOGD(LNN_EVENT, "start time sync ipc client push");
465     if (targetNetworkId == NULL || pkgName == NULL) {
466         LNN_LOGW(LNN_EVENT, "Invalid param");
467         return SOFTBUS_ERR;
468     }
469     if (g_serverProxy == NULL) {
470         LNN_LOGE(LNN_EVENT, "g_serverProxy is nullptr");
471         return SOFTBUS_ERR;
472     }
473 
474     uint8_t data[MAX_SOFT_BUS_IPC_LEN] = {0};
475     IpcIo request = {0};
476     IpcIoInit(&request, data, MAX_SOFT_BUS_IPC_LEN, 0);
477     WriteString(&request, pkgName);
478     WriteString(&request, targetNetworkId);
479     WriteInt32(&request, accuracy);
480     WriteInt32(&request, period);
481     /* asynchronous invocation */
482     int32_t ans = g_serverProxy->Invoke(g_serverProxy, SERVER_START_TIME_SYNC, &request, NULL, NULL);
483     if (ans != SOFTBUS_OK) {
484         LNN_LOGE(LNN_EVENT, "StartTimeSync invoke failed=%{public}d", ans);
485         return SOFTBUS_ERR;
486     }
487     return SOFTBUS_OK;
488 }
489 
ServerIpcStopTimeSync(const char * pkgName,const char * targetNetworkId)490 int32_t ServerIpcStopTimeSync(const char *pkgName, const char *targetNetworkId)
491 {
492     LNN_LOGD(LNN_EVENT, "stop time sync ipc client push");
493     if (targetNetworkId == NULL || pkgName == NULL) {
494         LNN_LOGW(LNN_EVENT, "Invalid param");
495         return SOFTBUS_ERR;
496     }
497     if (g_serverProxy == NULL) {
498         LNN_LOGE(LNN_EVENT, "g_serverProxy is nullptr");
499         return SOFTBUS_ERR;
500     }
501 
502     uint8_t data[MAX_SOFT_BUS_IPC_LEN] = {0};
503     IpcIo request = {0};
504     IpcIoInit(&request, data, MAX_SOFT_BUS_IPC_LEN, 0);
505     WriteString(&request, pkgName);
506     WriteString(&request, targetNetworkId);
507     /* asynchronous invocation */
508     int32_t ans = g_serverProxy->Invoke(g_serverProxy, SERVER_STOP_TIME_SYNC, &request, NULL, NULL);
509     if (ans != SOFTBUS_OK) {
510         LNN_LOGE(LNN_EVENT, "StopTimeSync invoke failed=%{public}d", ans);
511         return SOFTBUS_ERR;
512     }
513     return SOFTBUS_OK;
514 }
515 
ServerIpcPublishLNN(const char * pkgName,const PublishInfo * info)516 int32_t ServerIpcPublishLNN(const char *pkgName, const PublishInfo *info)
517 {
518     LNN_LOGD(LNN_EVENT, "publish Lnn ipc client push");
519     if (info == NULL || pkgName == NULL) {
520         LNN_LOGW(LNN_EVENT, "Invalid param");
521         return SOFTBUS_INVALID_PARAM;
522     }
523     if (g_serverProxy == NULL) {
524         LNN_LOGE(LNN_EVENT, "g_serverProxy is nullptr");
525         return SOFTBUS_ERR;
526     }
527     uint8_t data[MAX_SOFT_BUS_IPC_LEN_EX] = {0};
528     IpcIo request = {0};
529     Reply reply = {0};
530     reply.id = START_PUBLISH_LNN;
531     IpcIoInit(&request, data, MAX_SOFT_BUS_IPC_LEN_EX, 0);
532     WriteString(&request, pkgName);
533     WriteInt32(&request, info->publishId);
534     WriteInt32(&request, info->mode);
535     WriteInt32(&request, info->medium);
536     WriteInt32(&request, info->freq);
537     WriteString(&request, info->capability);
538     WriteUint32(&request, info->dataLen);
539     if (info->dataLen != 0) {
540         WriteString(&request, info->capabilityData);
541     }
542     WriteBool(&request, info->ranging);
543     /* asynchronous invocation */
544     int32_t ans = g_serverProxy->Invoke(g_serverProxy, SERVER_PUBLISH_LNN, &request, &reply, ClientBusCenterResultCb);
545     if (ans != SOFTBUS_OK || reply.retCode != SOFTBUS_OK) {
546         LNN_LOGE(LNN_EVENT, "publish Lnn invoke failed. ans=%{public}d, retCode=%{public}d", ans, reply.retCode);
547         return SOFTBUS_ERR;
548     }
549     return SOFTBUS_OK;
550 }
551 
ServerIpcStopPublishLNN(const char * pkgName,int32_t publishId)552 int32_t ServerIpcStopPublishLNN(const char *pkgName, int32_t publishId)
553 {
554     LNN_LOGD(LNN_EVENT, "stop publish lnn ipc client push");
555     if (pkgName == NULL) {
556         LNN_LOGW(LNN_EVENT, "Invalid param");
557         return SOFTBUS_ERR;
558     }
559     if (g_serverProxy == NULL) {
560         LNN_LOGE(LNN_EVENT, "g_serverProxy is nullptr");
561         return SOFTBUS_ERR;
562     }
563 
564     uint8_t data[MAX_SOFT_BUS_IPC_LEN] = {0};
565     IpcIo request = {0};
566     IpcIoInit(&request, data, MAX_SOFT_BUS_IPC_LEN, 0);
567     WriteString(&request, pkgName);
568     WriteInt32(&request, publishId);
569     /* asynchronous invocation */
570     int32_t ans = g_serverProxy->Invoke(g_serverProxy, SERVER_STOP_PUBLISH_LNN, &request, NULL, NULL);
571     if (ans != SOFTBUS_OK) {
572         LNN_LOGE(LNN_EVENT, "ServerIpcStopPublishLNN invoke failed. ans=%{public}d", ans);
573         return SOFTBUS_ERR;
574     }
575     return SOFTBUS_OK;
576 }
577 
ServerIpcRefreshLNN(const char * pkgName,const SubscribeInfo * info)578 int32_t ServerIpcRefreshLNN(const char *pkgName, const SubscribeInfo *info)
579 {
580     LNN_LOGD(LNN_EVENT, "refresh Lnn ipc client push");
581     if (info == NULL || pkgName == NULL) {
582         LNN_LOGW(LNN_EVENT, "Invalid param");
583         return SOFTBUS_INVALID_PARAM;
584     }
585     if (g_serverProxy == NULL) {
586         LNN_LOGE(LNN_EVENT, "g_serverProxy is nullptr!");
587         return SOFTBUS_ERR;
588     }
589     uint8_t data[MAX_SOFT_BUS_IPC_LEN_EX] = {0};
590     IpcIo request = {0};
591     Reply reply = {0};
592     reply.id = START_REFRESH_LNN;
593     IpcIoInit(&request, data, MAX_SOFT_BUS_IPC_LEN_EX, 0);
594     WriteString(&request, pkgName);
595     WriteInt32(&request, info->subscribeId);
596     WriteInt32(&request, info->mode);
597     WriteInt32(&request, info->medium);
598     WriteInt32(&request, info->freq);
599     WriteBool(&request, info->isSameAccount);
600     WriteBool(&request, info->isWakeRemote);
601     WriteString(&request, info->capability);
602     WriteUint32(&request, info->dataLen);
603     if (info->dataLen != 0) {
604         WriteString(&request, info->capabilityData);
605     }
606     /* asynchronous invocation */
607     int32_t ans = g_serverProxy->Invoke(g_serverProxy, SERVER_REFRESH_LNN, &request, &reply, ClientBusCenterResultCb);
608     if (ans != SOFTBUS_OK || reply.retCode != SOFTBUS_OK) {
609         LNN_LOGE(LNN_EVENT, "refresh Lnn invoke failed. ans=%{public}d, retCode=%{public}d", ans, reply.retCode);
610         return SOFTBUS_ERR;
611     }
612     return SOFTBUS_OK;
613 }
614 
ServerIpcStopRefreshLNN(const char * pkgName,int32_t refreshId)615 int32_t ServerIpcStopRefreshLNN(const char *pkgName, int32_t refreshId)
616 {
617     LNN_LOGD(LNN_EVENT, "stop refresh lnn ipc client push");
618     if (pkgName == NULL) {
619         LNN_LOGW(LNN_EVENT, "Invalid param");
620         return SOFTBUS_ERR;
621     }
622     if (g_serverProxy == NULL) {
623         LNN_LOGE(LNN_EVENT, "g_serverProxy is nullptr");
624         return SOFTBUS_ERR;
625     }
626 
627     uint8_t data[MAX_SOFT_BUS_IPC_LEN] = {0};
628     IpcIo request = {0};
629     IpcIoInit(&request, data, MAX_SOFT_BUS_IPC_LEN, 0);
630     WriteString(&request, pkgName);
631     WriteInt32(&request, refreshId);
632     /* asynchronous invocation */
633     int32_t ans = g_serverProxy->Invoke(g_serverProxy, SERVER_STOP_REFRESH_LNN, &request, NULL, NULL);
634     if (ans != SOFTBUS_OK) {
635         LNN_LOGE(LNN_EVENT, "invoke failed=%{public}d", ans);
636         return SOFTBUS_ERR;
637     }
638     return SOFTBUS_OK;
639 }
640 
ServerIpcActiveMetaNode(const char * pkgName,const MetaNodeConfigInfo * info,char * metaNodeId)641 int32_t ServerIpcActiveMetaNode(const char *pkgName, const MetaNodeConfigInfo *info, char *metaNodeId)
642 {
643     if (g_serverProxy == NULL) {
644         LNN_LOGE(LNN_EVENT, "g_serverProxy is nullptr");
645         return SOFTBUS_ERR;
646     }
647 
648     uint8_t data[MAX_SOFT_BUS_IPC_LEN_EX] = {0};
649     IpcIo request = {0};
650     IpcIoInit(&request, data, MAX_SOFT_BUS_IPC_LEN_EX, 0);
651     WriteString(&request, pkgName);
652     bool ret = WriteRawData(&request, info, sizeof(MetaNodeConfigInfo));
653     if (!ret) {
654         return SOFTBUS_ERR;
655     }
656     Reply reply = {0};
657     reply.id = ACTIVE_META_NODE;
658     /* asynchronous invocation */
659     int32_t ans = g_serverProxy->Invoke(g_serverProxy, SERVER_ACTIVE_META_NODE, &request, &reply,
660         ClientBusCenterResultCb);
661     if (ans != SOFTBUS_OK || reply.retCode != SOFTBUS_OK) {
662         LNN_LOGE(LNN_EVENT, "invoke failed. ans=%{public}d, retCode=%{public}d", ans, reply.retCode);
663         return SOFTBUS_ERR;
664     }
665     if (reply.data == NULL) {
666         LNN_LOGE(LNN_EVENT, "read data failed");
667         return SOFTBUS_ERR;
668     }
669     if (strncpy_s(metaNodeId, NETWORK_ID_BUF_LEN, (char *)reply.data, strlen((char *)reply.data)) != EOK) {
670         LNN_LOGE(LNN_EVENT, "copy meta node id failed");
671         return SOFTBUS_ERR;
672     }
673     return SOFTBUS_OK;
674 }
675 
ServerIpcDeactiveMetaNode(const char * pkgName,const char * metaNodeId)676 int32_t ServerIpcDeactiveMetaNode(const char *pkgName, const char *metaNodeId)
677 {
678     if (g_serverProxy == NULL) {
679         LNN_LOGE(LNN_EVENT, "g_serverProxy is nullptr");
680         return SOFTBUS_ERR;
681     }
682 
683     uint8_t data[MAX_SOFT_BUS_IPC_LEN] = {0};
684     IpcIo request = {0};
685     IpcIoInit(&request, data, MAX_SOFT_BUS_IPC_LEN, 0);
686     WriteString(&request, pkgName);
687     WriteString(&request, metaNodeId);
688     Reply reply = {0};
689     reply.id = DEACTIVE_META_NODE;
690     /* asynchronous invocation */
691     int32_t ans = g_serverProxy->Invoke(g_serverProxy, SERVER_DEACTIVE_META_NODE, &request,
692         &reply, ClientBusCenterResultCb);
693     if (ans != SOFTBUS_OK || reply.retCode != SOFTBUS_OK) {
694         LNN_LOGE(LNN_EVENT, "invoke failed. ans=%{public}d, retCode=%{public}d", ans, reply.retCode);
695         return SOFTBUS_ERR;
696     }
697     return SOFTBUS_OK;
698 }
699 
ServerIpcGetAllMetaNodeInfo(const char * pkgName,MetaNodeInfo * infos,int32_t * infoNum)700 int32_t ServerIpcGetAllMetaNodeInfo(const char *pkgName, MetaNodeInfo *infos, int32_t *infoNum)
701 {
702     if (g_serverProxy == NULL) {
703         LNN_LOGE(LNN_EVENT, "g_serverProxy is nullptr");
704         return SOFTBUS_ERR;
705     }
706 
707     uint8_t data[MAX_SOFT_BUS_IPC_LEN] = {0};
708     IpcIo request = {0};
709     IpcIoInit(&request, data, MAX_SOFT_BUS_IPC_LEN, 0);
710     WriteString(&request, pkgName);
711     WriteInt32(&request, *infoNum);
712     Reply reply = {0};
713     reply.id = GET_ALL_META_NODE;
714     /* asynchronous invocation */
715     int32_t ans = g_serverProxy->Invoke(g_serverProxy, SERVER_GET_ALL_META_NODE_INFO, &request, &reply,
716         ClientBusCenterResultCb);
717     if (ans != SOFTBUS_OK || reply.retCode != SOFTBUS_OK) {
718         LNN_LOGE(LNN_EVENT, "invoke failed. ans=%{public}d, retCode=%{public}d", ans, reply.retCode);
719         return SOFTBUS_ERR;
720     }
721     if (reply.arg1 > 0) {
722         if (reply.data == NULL) {
723             LNN_LOGE(LNN_EVENT, "read meta node info failed");
724             return SOFTBUS_ERR;
725         }
726         if (memcpy_s(infos, *infoNum * sizeof(MetaNodeInfo), reply.data, reply.arg1 * sizeof(MetaNodeInfo)) != EOK) {
727             LNN_LOGE(LNN_EVENT, "copy meta node info failed");
728             return SOFTBUS_ERR;
729         }
730     }
731     *infoNum = reply.arg1;
732     return SOFTBUS_OK;
733 }
734 
ServerIpcShiftLNNGear(const char * pkgName,const char * callerId,const char * targetNetworkId,const GearMode * mode)735 int32_t ServerIpcShiftLNNGear(const char *pkgName, const char *callerId, const char *targetNetworkId,
736     const GearMode *mode)
737 {
738     if (pkgName == NULL || callerId == NULL || mode == NULL) {
739         LNN_LOGW(LNN_EVENT, "Invalid param");
740         return SOFTBUS_ERR;
741     }
742     if (g_serverProxy == NULL) {
743         LNN_LOGE(LNN_EVENT, "g_serverProxy is nullptr");
744         return SOFTBUS_ERR;
745     }
746 
747     uint8_t data[MAX_SOFT_BUS_IPC_LEN] = {0};
748     bool targetNetworkIdIsNull = targetNetworkId == NULL ? true : false;
749     IpcIo request = {0};
750     IpcIoInit(&request, data, MAX_SOFT_BUS_IPC_LEN, 0);
751     WriteString(&request, pkgName);
752     WriteString(&request, callerId);
753     WriteBool(&request, targetNetworkIdIsNull);
754     if (!targetNetworkIdIsNull) {
755         WriteString(&request, targetNetworkId);
756     }
757     WriteRawData(&request, mode, sizeof(GearMode));
758     Reply reply = {0};
759     reply.id = SHIFT_LNN_GEAR;
760     /* asynchronous invocation */
761     int32_t ans = g_serverProxy->Invoke(g_serverProxy, SERVER_SHIFT_LNN_GEAR, &request, &reply,
762         ClientBusCenterResultCb);
763     if (ans != SOFTBUS_OK || reply.retCode != SOFTBUS_OK) {
764         LNN_LOGE(LNN_EVENT, "invoke failed. ans=%{public}d, retCode=%{public}d", ans, reply.retCode);
765         return ans != SOFTBUS_OK ? SOFTBUS_ERR : reply.retCode;
766     }
767     return SOFTBUS_OK;
768 }
769