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