• 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 "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}d", 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,int ret,IpcIo * reply)161 static int32_t ClientBusCenterResultCb(Reply *info, int 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,int key,unsigned char * buf,uint32_t len)304 int32_t ServerIpcGetNodeKeyInfo(const char *pkgName, const char *networkId, int key, unsigned char *buf, uint32_t len)
305 {
306     if (networkId == NULL || buf == NULL) {
307         LNN_LOGW(LNN_EVENT, "params are nullptr");
308         return SOFTBUS_ERR;
309     }
310     if (g_serverProxy == NULL) {
311         LNN_LOGW(LNN_EVENT, "g_serverProxy is nullptr");
312         return SOFTBUS_ERR;
313     }
314 
315     uint8_t data[MAX_SOFT_BUS_IPC_LEN] = {0};
316     IpcIo request = {0};
317     IpcIoInit(&request, data, MAX_SOFT_BUS_IPC_LEN, 0);
318     WriteString(&request, pkgName);
319     WriteString(&request, networkId);
320     WriteInt32(&request, key);
321     WriteInt32(&request, len);
322     Reply reply = {0};
323     reply.id = GET_NODE_KEY_INFO;
324     /* asynchronous invocation */
325     int32_t ans = g_serverProxy->Invoke(g_serverProxy, SERVER_GET_NODE_KEY_INFO, &request, &reply,
326         ClientBusCenterResultCb);
327     if (ans != SOFTBUS_OK) {
328         LNN_LOGE(LNN_EVENT, "GetNodeKeyInfo invoke failed=%{public}d", ans);
329         return SOFTBUS_ERR;
330     }
331     if (reply.data == NULL || reply.dataLen > len) {
332         LNN_LOGE(LNN_EVENT,
333             "GetNodeKeyInfo read retBuf failed, inlen=%{public}d, reply.dataLen=%{public}d", len, reply.dataLen);
334         return SOFTBUS_ERR;
335     }
336     if (memcpy_s(buf, len, reply.data, reply.dataLen) != EOK) {
337         LNN_LOGE(LNN_EVENT, "GetNodeKeyInfo copy node key info failed");
338         return SOFTBUS_ERR;
339     }
340     return SOFTBUS_OK;
341 }
342 
ServerIpcSetNodeDataChangeFlag(const char * pkgName,const char * networkId,uint16_t dataChangeFlag)343 int32_t ServerIpcSetNodeDataChangeFlag(const char *pkgName, const char *networkId, uint16_t dataChangeFlag)
344 {
345     if (networkId == NULL) {
346         LNN_LOGE(LNN_EVENT, "params are nullptr");
347         return SOFTBUS_ERR;
348     }
349     if (g_serverProxy == NULL) {
350         LNN_LOGE(LNN_EVENT, "g_serverProxy is nullptr");
351         return SOFTBUS_ERR;
352     }
353 
354     uint8_t data[MAX_SOFT_BUS_IPC_LEN] = {0};
355     IpcIo request = {0};
356     IpcIoInit(&request, data, MAX_SOFT_BUS_IPC_LEN, 0);
357     WriteString(&request, pkgName);
358     WriteString(&request, networkId);
359     WriteInt16(&request, dataChangeFlag);
360     int32_t ans = g_serverProxy->Invoke(g_serverProxy, SERVER_SET_NODE_DATA_CHANGE_FLAG, &request, NULL, NULL);
361     if (ans != SOFTBUS_OK) {
362         LNN_LOGE(LNN_EVENT, "invoke failed=%{public}d", ans);
363         return SOFTBUS_ERR;
364     }
365     return SOFTBUS_OK;
366 }
367 
ServerIpcJoinLNN(const char * pkgName,void * addr,unsigned int addrTypeLen)368 int ServerIpcJoinLNN(const char *pkgName, void *addr, unsigned int addrTypeLen)
369 {
370     LNN_LOGD(LNN_EVENT, "join Lnn ipc client push");
371     if (addr == NULL || pkgName == NULL) {
372         LNN_LOGW(LNN_EVENT, "Invalid param");
373         return SOFTBUS_INVALID_PARAM;
374     }
375     if (g_serverProxy == NULL) {
376         LNN_LOGE(LNN_EVENT, "g_serverProxy is nullptr");
377         return SOFTBUS_ERR;
378     }
379     uint8_t data[MAX_SOFT_BUS_IPC_LEN] = {0};
380     IpcIo request = {0};
381     IpcIoInit(&request, data, MAX_SOFT_BUS_IPC_LEN, 0);
382     WriteString(&request, pkgName);
383     WriteUint32(&request, addrTypeLen);
384     WriteBuffer(&request, addr, addrTypeLen);
385     /* asynchronous invocation */
386     int32_t ans = g_serverProxy->Invoke(g_serverProxy, SERVER_JOIN_LNN, &request, NULL, NULL);
387     if (ans != SOFTBUS_OK) {
388         LNN_LOGE(LNN_EVENT, "join Lnn invoke failed=%{public}d", ans);
389         return SOFTBUS_ERR;
390     }
391     return SOFTBUS_OK;
392 }
393 
ServerIpcLeaveLNN(const char * pkgName,const char * networkId)394 int ServerIpcLeaveLNN(const char *pkgName, const char *networkId)
395 {
396     LNN_LOGD(LNN_EVENT, "leave Lnn ipc client push");
397     if (pkgName == NULL || networkId == NULL) {
398         LNN_LOGW(LNN_EVENT, "Invalid param");
399         return SOFTBUS_INVALID_PARAM;
400     }
401     if (g_serverProxy == NULL) {
402         LNN_LOGE(LNN_EVENT, "ServerIpcLeaveLNN g_serverProxy is nullptr!\n");
403         return SOFTBUS_ERR;
404     }
405     uint8_t data[MAX_SOFT_BUS_IPC_LEN] = {0};
406     IpcIo request = {0};
407     IpcIoInit(&request, data, MAX_SOFT_BUS_IPC_LEN, 0);
408     WriteString(&request, pkgName);
409     WriteString(&request, networkId);
410     /* asynchronous invocation */
411     int32_t ans = g_serverProxy->Invoke(g_serverProxy, SERVER_LEAVE_LNN, &request, NULL, NULL);
412     if (ans != SOFTBUS_OK) {
413         LNN_LOGE(LNN_EVENT, "leave Lnn invoke failed=%{public}d", ans);
414         return SOFTBUS_ERR;
415     }
416     return SOFTBUS_OK;
417 }
418 
ServerIpcStartTimeSync(const char * pkgName,const char * targetNetworkId,int32_t accuracy,int32_t period)419 int32_t ServerIpcStartTimeSync(const char *pkgName, const char *targetNetworkId, int32_t accuracy, int32_t period)
420 {
421     LNN_LOGD(LNN_EVENT, "start time sync ipc client push");
422     if (targetNetworkId == NULL || pkgName == NULL) {
423         LNN_LOGW(LNN_EVENT, "Invalid param");
424         return SOFTBUS_ERR;
425     }
426     if (g_serverProxy == NULL) {
427         LNN_LOGE(LNN_EVENT, "g_serverProxy is nullptr");
428         return SOFTBUS_ERR;
429     }
430 
431     uint8_t data[MAX_SOFT_BUS_IPC_LEN] = {0};
432     IpcIo request = {0};
433     IpcIoInit(&request, data, MAX_SOFT_BUS_IPC_LEN, 0);
434     WriteString(&request, pkgName);
435     WriteString(&request, targetNetworkId);
436     WriteInt32(&request, accuracy);
437     WriteInt32(&request, period);
438     /* asynchronous invocation */
439     int32_t ans = g_serverProxy->Invoke(g_serverProxy, SERVER_START_TIME_SYNC, &request, NULL, NULL);
440     if (ans != SOFTBUS_OK) {
441         LNN_LOGE(LNN_EVENT, "StartTimeSync invoke failed=%{public}d", ans);
442         return SOFTBUS_ERR;
443     }
444     return SOFTBUS_OK;
445 }
446 
ServerIpcStopTimeSync(const char * pkgName,const char * targetNetworkId)447 int32_t ServerIpcStopTimeSync(const char *pkgName, const char *targetNetworkId)
448 {
449     LNN_LOGD(LNN_EVENT, "stop time sync ipc client push");
450     if (targetNetworkId == NULL || pkgName == NULL) {
451         LNN_LOGW(LNN_EVENT, "Invalid param");
452         return SOFTBUS_ERR;
453     }
454     if (g_serverProxy == NULL) {
455         LNN_LOGE(LNN_EVENT, "g_serverProxy is nullptr");
456         return SOFTBUS_ERR;
457     }
458 
459     uint8_t data[MAX_SOFT_BUS_IPC_LEN] = {0};
460     IpcIo request = {0};
461     IpcIoInit(&request, data, MAX_SOFT_BUS_IPC_LEN, 0);
462     WriteString(&request, pkgName);
463     WriteString(&request, targetNetworkId);
464     /* asynchronous invocation */
465     int32_t ans = g_serverProxy->Invoke(g_serverProxy, SERVER_STOP_TIME_SYNC, &request, NULL, NULL);
466     if (ans != SOFTBUS_OK) {
467         LNN_LOGE(LNN_EVENT, "StopTimeSync invoke failed=%{public}d", ans);
468         return SOFTBUS_ERR;
469     }
470     return SOFTBUS_OK;
471 }
472 
ServerIpcPublishLNN(const char * pkgName,const PublishInfo * info)473 int32_t ServerIpcPublishLNN(const char *pkgName, const PublishInfo *info)
474 {
475     LNN_LOGD(LNN_EVENT, "publish Lnn ipc client push");
476     if (info == NULL || pkgName == NULL) {
477         LNN_LOGW(LNN_EVENT, "Invalid param");
478         return SOFTBUS_INVALID_PARAM;
479     }
480     if (g_serverProxy == NULL) {
481         LNN_LOGE(LNN_EVENT, "g_serverProxy is nullptr");
482         return SOFTBUS_ERR;
483     }
484     uint8_t data[MAX_SOFT_BUS_IPC_LEN_EX] = {0};
485     IpcIo request = {0};
486     Reply reply = {0};
487     reply.id = START_PUBLISH_LNN;
488     IpcIoInit(&request, data, MAX_SOFT_BUS_IPC_LEN_EX, 0);
489     WriteString(&request, pkgName);
490     WriteInt32(&request, info->publishId);
491     WriteInt32(&request, info->mode);
492     WriteInt32(&request, info->medium);
493     WriteInt32(&request, info->freq);
494     WriteString(&request, info->capability);
495     WriteUint32(&request, info->dataLen);
496     if (info->dataLen != 0) {
497         WriteString(&request, info->capabilityData);
498     }
499     WriteBool(&request, info->ranging);
500     /* asynchronous invocation */
501     int32_t ans = g_serverProxy->Invoke(g_serverProxy, SERVER_PUBLISH_LNN, &request, &reply, ClientBusCenterResultCb);
502     if (ans != SOFTBUS_OK || reply.retCode != SOFTBUS_OK) {
503         LNN_LOGE(LNN_EVENT, "publish Lnn invoke failed. ans=%{public}d, retCode=%{public}d", ans, reply.retCode);
504         return SOFTBUS_ERR;
505     }
506     return SOFTBUS_OK;
507 }
508 
ServerIpcStopPublishLNN(const char * pkgName,int32_t publishId)509 int32_t ServerIpcStopPublishLNN(const char *pkgName, int32_t publishId)
510 {
511     LNN_LOGD(LNN_EVENT, "stop publish lnn ipc client push");
512     if (pkgName == NULL) {
513         LNN_LOGW(LNN_EVENT, "Invalid param");
514         return SOFTBUS_ERR;
515     }
516     if (g_serverProxy == NULL) {
517         LNN_LOGE(LNN_EVENT, "g_serverProxy is nullptr");
518         return SOFTBUS_ERR;
519     }
520 
521     uint8_t data[MAX_SOFT_BUS_IPC_LEN] = {0};
522     IpcIo request = {0};
523     IpcIoInit(&request, data, MAX_SOFT_BUS_IPC_LEN, 0);
524     WriteString(&request, pkgName);
525     WriteInt32(&request, publishId);
526     /* asynchronous invocation */
527     int32_t ans = g_serverProxy->Invoke(g_serverProxy, SERVER_STOP_PUBLISH_LNN, &request, NULL, NULL);
528     if (ans != SOFTBUS_OK) {
529         LNN_LOGE(LNN_EVENT, "ServerIpcStopPublishLNN invoke failed. ans=%{public}d", ans);
530         return SOFTBUS_ERR;
531     }
532     return SOFTBUS_OK;
533 }
534 
ServerIpcRefreshLNN(const char * pkgName,const SubscribeInfo * info)535 int32_t ServerIpcRefreshLNN(const char *pkgName, const SubscribeInfo *info)
536 {
537     LNN_LOGD(LNN_EVENT, "refresh Lnn ipc client push");
538     if (info == NULL || pkgName == NULL) {
539         LNN_LOGW(LNN_EVENT, "Invalid param");
540         return SOFTBUS_INVALID_PARAM;
541     }
542     if (g_serverProxy == NULL) {
543         LNN_LOGE(LNN_EVENT, "g_serverProxy is nullptr!");
544         return SOFTBUS_ERR;
545     }
546     uint8_t data[MAX_SOFT_BUS_IPC_LEN_EX] = {0};
547     IpcIo request = {0};
548     Reply reply = {0};
549     reply.id = START_REFRESH_LNN;
550     IpcIoInit(&request, data, MAX_SOFT_BUS_IPC_LEN_EX, 0);
551     WriteString(&request, pkgName);
552     WriteInt32(&request, info->subscribeId);
553     WriteInt32(&request, info->mode);
554     WriteInt32(&request, info->medium);
555     WriteInt32(&request, info->freq);
556     WriteBool(&request, info->isSameAccount);
557     WriteBool(&request, info->isWakeRemote);
558     WriteString(&request, info->capability);
559     WriteUint32(&request, info->dataLen);
560     if (info->dataLen != 0) {
561         WriteString(&request, info->capabilityData);
562     }
563     /* asynchronous invocation */
564     int32_t ans = g_serverProxy->Invoke(g_serverProxy, SERVER_REFRESH_LNN, &request, &reply, ClientBusCenterResultCb);
565     if (ans != SOFTBUS_OK || reply.retCode != SOFTBUS_OK) {
566         LNN_LOGE(LNN_EVENT, "refresh Lnn invoke failed. ans=%{public}d, retCode=%{public}d", ans, reply.retCode);
567         return SOFTBUS_ERR;
568     }
569     return SOFTBUS_OK;
570 }
571 
ServerIpcStopRefreshLNN(const char * pkgName,int32_t refreshId)572 int32_t ServerIpcStopRefreshLNN(const char *pkgName, int32_t refreshId)
573 {
574     LNN_LOGD(LNN_EVENT, "stop refresh lnn ipc client push");
575     if (pkgName == NULL) {
576         LNN_LOGW(LNN_EVENT, "Invalid param");
577         return SOFTBUS_ERR;
578     }
579     if (g_serverProxy == NULL) {
580         LNN_LOGE(LNN_EVENT, "g_serverProxy is nullptr");
581         return SOFTBUS_ERR;
582     }
583 
584     uint8_t data[MAX_SOFT_BUS_IPC_LEN] = {0};
585     IpcIo request = {0};
586     IpcIoInit(&request, data, MAX_SOFT_BUS_IPC_LEN, 0);
587     WriteString(&request, pkgName);
588     WriteInt32(&request, refreshId);
589     /* asynchronous invocation */
590     int32_t ans = g_serverProxy->Invoke(g_serverProxy, SERVER_STOP_REFRESH_LNN, &request, NULL, NULL);
591     if (ans != SOFTBUS_OK) {
592         LNN_LOGE(LNN_EVENT, "invoke failed=%{public}d", ans);
593         return SOFTBUS_ERR;
594     }
595     return SOFTBUS_OK;
596 }
597 
ServerIpcActiveMetaNode(const char * pkgName,const MetaNodeConfigInfo * info,char * metaNodeId)598 int32_t ServerIpcActiveMetaNode(const char *pkgName, const MetaNodeConfigInfo *info, char *metaNodeId)
599 {
600     if (g_serverProxy == NULL) {
601         LNN_LOGE(LNN_EVENT, "g_serverProxy is nullptr");
602         return SOFTBUS_ERR;
603     }
604 
605     uint8_t data[MAX_SOFT_BUS_IPC_LEN_EX] = {0};
606     IpcIo request = {0};
607     IpcIoInit(&request, data, MAX_SOFT_BUS_IPC_LEN_EX, 0);
608     WriteString(&request, pkgName);
609     bool ret = WriteRawData(&request, info, sizeof(MetaNodeConfigInfo));
610     if (!ret) {
611         return SOFTBUS_ERR;
612     }
613     Reply reply = {0};
614     reply.id = ACTIVE_META_NODE;
615     /* asynchronous invocation */
616     int32_t ans = g_serverProxy->Invoke(g_serverProxy, SERVER_ACTIVE_META_NODE, &request, &reply,
617         ClientBusCenterResultCb);
618     if (ans != SOFTBUS_OK || reply.retCode != SOFTBUS_OK) {
619         LNN_LOGE(LNN_EVENT, "invoke failed. ans=%{public}d, retCode=%{public}d", ans, reply.retCode);
620         return SOFTBUS_ERR;
621     }
622     if (reply.data == NULL) {
623         LNN_LOGE(LNN_EVENT, "read data failed");
624         return SOFTBUS_ERR;
625     }
626     if (strncpy_s(metaNodeId, NETWORK_ID_BUF_LEN, (char *)reply.data, strlen((char *)reply.data)) != EOK) {
627         LNN_LOGE(LNN_EVENT, "copy meta node id failed");
628         return SOFTBUS_ERR;
629     }
630     return SOFTBUS_OK;
631 }
632 
ServerIpcDeactiveMetaNode(const char * pkgName,const char * metaNodeId)633 int32_t ServerIpcDeactiveMetaNode(const char *pkgName, const char *metaNodeId)
634 {
635     if (g_serverProxy == NULL) {
636         LNN_LOGE(LNN_EVENT, "g_serverProxy is nullptr");
637         return SOFTBUS_ERR;
638     }
639 
640     uint8_t data[MAX_SOFT_BUS_IPC_LEN] = {0};
641     IpcIo request = {0};
642     IpcIoInit(&request, data, MAX_SOFT_BUS_IPC_LEN, 0);
643     WriteString(&request, pkgName);
644     WriteString(&request, metaNodeId);
645     Reply reply = {0};
646     reply.id = DEACTIVE_META_NODE;
647     /* asynchronous invocation */
648     int32_t ans = g_serverProxy->Invoke(g_serverProxy, SERVER_DEACTIVE_META_NODE, &request,
649         &reply, ClientBusCenterResultCb);
650     if (ans != SOFTBUS_OK || reply.retCode != SOFTBUS_OK) {
651         LNN_LOGE(LNN_EVENT, "invoke failed. ans=%{public}d, retCode=%{public}d", ans, reply.retCode);
652         return SOFTBUS_ERR;
653     }
654     return SOFTBUS_OK;
655 }
656 
ServerIpcGetAllMetaNodeInfo(const char * pkgName,MetaNodeInfo * infos,int32_t * infoNum)657 int32_t ServerIpcGetAllMetaNodeInfo(const char *pkgName, MetaNodeInfo *infos, int32_t *infoNum)
658 {
659     if (g_serverProxy == NULL) {
660         LNN_LOGE(LNN_EVENT, "g_serverProxy is nullptr");
661         return SOFTBUS_ERR;
662     }
663 
664     uint8_t data[MAX_SOFT_BUS_IPC_LEN] = {0};
665     IpcIo request = {0};
666     IpcIoInit(&request, data, MAX_SOFT_BUS_IPC_LEN, 0);
667     WriteString(&request, pkgName);
668     WriteInt32(&request, *infoNum);
669     Reply reply = {0};
670     reply.id = GET_ALL_META_NODE;
671     /* asynchronous invocation */
672     int32_t ans = g_serverProxy->Invoke(g_serverProxy, SERVER_GET_ALL_META_NODE_INFO, &request, &reply,
673         ClientBusCenterResultCb);
674     if (ans != SOFTBUS_OK || reply.retCode != SOFTBUS_OK) {
675         LNN_LOGE(LNN_EVENT, "invoke failed. ans=%{public}d, retCode=%{public}d", ans, reply.retCode);
676         return SOFTBUS_ERR;
677     }
678     if (reply.arg1 > 0) {
679         if (reply.data == NULL) {
680             LNN_LOGE(LNN_EVENT, "read meta node info failed");
681             return SOFTBUS_ERR;
682         }
683         if (memcpy_s(infos, *infoNum * sizeof(MetaNodeInfo), reply.data, reply.arg1 * sizeof(MetaNodeInfo)) != EOK) {
684             LNN_LOGE(LNN_EVENT, "copy meta node info failed");
685             return SOFTBUS_ERR;
686         }
687     }
688     *infoNum = reply.arg1;
689     return SOFTBUS_OK;
690 }
691 
ServerIpcShiftLNNGear(const char * pkgName,const char * callerId,const char * targetNetworkId,const GearMode * mode)692 int32_t ServerIpcShiftLNNGear(const char *pkgName, const char *callerId, const char *targetNetworkId,
693     const GearMode *mode)
694 {
695     if (pkgName == NULL || callerId == NULL || mode == NULL) {
696         LNN_LOGW(LNN_EVENT, "Invalid param");
697         return SOFTBUS_ERR;
698     }
699     if (g_serverProxy == NULL) {
700         LNN_LOGE(LNN_EVENT, "g_serverProxy is nullptr");
701         return SOFTBUS_ERR;
702     }
703 
704     uint8_t data[MAX_SOFT_BUS_IPC_LEN] = {0};
705     bool targetNetworkIdIsNull = targetNetworkId == NULL ? true : false;
706     IpcIo request = {0};
707     IpcIoInit(&request, data, MAX_SOFT_BUS_IPC_LEN, 0);
708     WriteString(&request, pkgName);
709     WriteString(&request, callerId);
710     WriteBool(&request, targetNetworkIdIsNull);
711     if (!targetNetworkIdIsNull) {
712         WriteString(&request, targetNetworkId);
713     }
714     WriteRawData(&request, mode, sizeof(GearMode));
715     Reply reply = {0};
716     reply.id = SHIFT_LNN_GEAR;
717     /* asynchronous invocation */
718     int32_t ans = g_serverProxy->Invoke(g_serverProxy, SERVER_SHIFT_LNN_GEAR, &request, &reply,
719         ClientBusCenterResultCb);
720     if (ans != SOFTBUS_OK || reply.retCode != SOFTBUS_OK) {
721         LNN_LOGE(LNN_EVENT, "invoke failed. ans=%{public}d, retCode=%{public}d", ans, reply.retCode);
722         return ans != SOFTBUS_OK ? SOFTBUS_ERR : reply.retCode;
723     }
724     return SOFTBUS_OK;
725 }
726