1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "client_bus_center.h"
17
18 #include <securec.h>
19 #include <string.h>
20
21 #include "softbus_adapter_mem.h"
22 #include "softbus_client_frame_manager.h"
23 #include "softbus_def.h"
24 #include "softbus_errcode.h"
25 #include "softbus_log.h"
26 #include "softbus_type_def.h"
27 #include "softbus_utils.h"
28
CommonInit(const char * pkgName)29 static int32_t CommonInit(const char *pkgName)
30 {
31 if (InitSoftBus(pkgName) != SOFTBUS_OK) {
32 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "init softbus failed");
33 return SOFTBUS_ERR;
34 }
35 if (CheckPackageName(pkgName) != SOFTBUS_OK) {
36 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "check packageName failed");
37 return SOFTBUS_INVALID_PARAM;
38 }
39 return SOFTBUS_OK;
40 }
41
IsValidNodeStateCb(INodeStateCb * callback)42 static bool IsValidNodeStateCb(INodeStateCb *callback)
43 {
44 if (callback == NULL) {
45 return false;
46 }
47 if (callback->events == 0) {
48 return false;
49 }
50 if ((callback->events & EVENT_NODE_STATE_ONLINE) != 0 &&
51 callback->onNodeOnline == NULL) {
52 return false;
53 }
54 if ((callback->events & EVENT_NODE_STATE_OFFLINE) != 0 &&
55 callback->onNodeOffline == NULL) {
56 return false;
57 }
58 if ((callback->events & EVENT_NODE_STATE_INFO_CHANGED) != 0 &&
59 callback->onNodeBasicInfoChanged == NULL) {
60 return false;
61 }
62 return true;
63 }
64
PublishInfoCheck(const PublishInfo * info)65 static int32_t PublishInfoCheck(const PublishInfo *info)
66 {
67 if ((info->mode != DISCOVER_MODE_PASSIVE) && (info->mode != DISCOVER_MODE_ACTIVE)) {
68 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "mode is invalid");
69 return SOFTBUS_INVALID_PARAM;
70 }
71 if ((info->medium < AUTO) || (info->medium > COAP)) {
72 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "medium is invalid");
73 return SOFTBUS_INVALID_PARAM;
74 }
75 if (info->capability == NULL) {
76 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "capability is invalid");
77 return SOFTBUS_INVALID_PARAM;
78 }
79 if ((info->freq < LOW) || (info->freq > SUPER_HIGH)) {
80 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "freq is invalid");
81 return SOFTBUS_INVALID_PARAM;
82 }
83 if ((info->capabilityData == NULL) && (info->dataLen != 0)) {
84 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "data is invalid");
85 return SOFTBUS_INVALID_PARAM;
86 }
87 if (info->dataLen == 0) {
88 return SOFTBUS_OK;
89 }
90
91 if ((info->capabilityData != NULL) &&
92 ((info->dataLen > MAX_CAPABILITYDATA_LEN) ||
93 (strnlen((char *)(info->capabilityData), MAX_CAPABILITY_LEN) == MAX_CAPABILITYDATA_LEN))) {
94 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "data exceeds the maximum length");
95 return SOFTBUS_INVALID_PARAM;
96 }
97 return SOFTBUS_OK;
98 }
99
SubscribeInfoCheck(const SubscribeInfo * info)100 static int32_t SubscribeInfoCheck(const SubscribeInfo *info)
101 {
102 if ((info->mode != DISCOVER_MODE_PASSIVE) && (info->mode != DISCOVER_MODE_ACTIVE)) {
103 SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, "mode is invalid");
104 return SOFTBUS_INVALID_PARAM;
105 }
106 if ((info->medium < AUTO) || (info->medium > COAP)) {
107 SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, "medium is invalid");
108 return SOFTBUS_INVALID_PARAM;
109 }
110 if ((info->freq < LOW) || (info->freq > SUPER_HIGH)) {
111 SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, "freq is invalid");
112 return SOFTBUS_INVALID_PARAM;
113 }
114 if (info->capability == NULL) {
115 SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, "capability is invalid");
116 return SOFTBUS_INVALID_PARAM;
117 }
118 if ((info->capabilityData == NULL) && (info->dataLen != 0)) {
119 SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, "data is invalid");
120 return SOFTBUS_INVALID_PARAM;
121 }
122 if (info->dataLen == 0) {
123 return SOFTBUS_OK;
124 }
125 if ((info->capabilityData != NULL) &&
126 ((info->dataLen > MAX_CAPABILITYDATA_LEN) ||
127 (strnlen((char *)(info->capabilityData), MAX_CAPABILITYDATA_LEN) == MAX_CAPABILITYDATA_LEN))) {
128 SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, "data exceeds the maximum length");
129 return SOFTBUS_INVALID_PARAM;
130 }
131 return SOFTBUS_OK;
132 }
133
GetAllNodeDeviceInfo(const char * pkgName,NodeBasicInfo ** info,int32_t * infoNum)134 int32_t GetAllNodeDeviceInfo(const char *pkgName, NodeBasicInfo **info, int32_t *infoNum)
135 {
136 if (pkgName == NULL || info == NULL || infoNum == NULL) {
137 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fail: params are null");
138 return SOFTBUS_INVALID_PARAM;
139 }
140 if (CommonInit(pkgName) != SOFTBUS_OK) {
141 return SOFTBUS_INVALID_PARAM;
142 }
143 return GetAllNodeDeviceInfoInner(pkgName, info, infoNum);
144 }
145
FreeNodeInfo(NodeBasicInfo * info)146 void FreeNodeInfo(NodeBasicInfo *info)
147 {
148 if (info == NULL) {
149 return;
150 }
151 SoftBusFree(info);
152 }
153
GetLocalNodeDeviceInfo(const char * pkgName,NodeBasicInfo * info)154 int32_t GetLocalNodeDeviceInfo(const char *pkgName, NodeBasicInfo *info)
155 {
156 if (pkgName == NULL || info == NULL) {
157 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fail: params are null");
158 return SOFTBUS_INVALID_PARAM;
159 }
160 if (CommonInit(pkgName) != SOFTBUS_OK) {
161 return SOFTBUS_INVALID_PARAM;
162 }
163 return GetLocalNodeDeviceInfoInner(pkgName, info);
164 }
165
GetNodeKeyInfo(const char * pkgName,const char * networkId,NodeDeviceInfoKey key,uint8_t * info,int32_t infoLen)166 int32_t GetNodeKeyInfo(const char *pkgName, const char *networkId, NodeDeviceInfoKey key,
167 uint8_t *info, int32_t infoLen)
168 {
169 if (pkgName == NULL) {
170 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fail: pkgName is null");
171 return SOFTBUS_INVALID_PARAM;
172 }
173 if (!IsValidString(networkId, NETWORK_ID_BUF_LEN) || info == NULL) {
174 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "invalid params");
175 return SOFTBUS_INVALID_PARAM;
176 }
177 if (CommonInit(pkgName) != SOFTBUS_OK) {
178 return SOFTBUS_INVALID_PARAM;
179 }
180 return GetNodeKeyInfoInner(pkgName, networkId, key, info, infoLen);
181 }
182
JoinLNN(const char * pkgName,ConnectionAddr * target,OnJoinLNNResult cb)183 int32_t JoinLNN(const char *pkgName, ConnectionAddr *target, OnJoinLNNResult cb)
184 {
185 if (pkgName == NULL || target == NULL || cb == NULL) {
186 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fail : params are NULL!");
187 return SOFTBUS_INVALID_PARAM;
188 }
189 if (CommonInit(pkgName) != SOFTBUS_OK) {
190 return SOFTBUS_INVALID_PARAM;
191 }
192 return JoinLNNInner(pkgName, target, cb);
193 }
194
LeaveLNN(const char * pkgName,const char * networkId,OnLeaveLNNResult cb)195 int32_t LeaveLNN(const char *pkgName, const char *networkId, OnLeaveLNNResult cb)
196 {
197 if (!IsValidString(networkId, NETWORK_ID_BUF_LEN) || cb == NULL || !IsValidString(pkgName, PKG_NAME_SIZE_MAX)) {
198 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fail : networkId or cb is NULL!");
199 return SOFTBUS_INVALID_PARAM;
200 }
201 return LeaveLNNInner(pkgName, networkId, cb);
202 }
203
RegNodeDeviceStateCb(const char * pkgName,INodeStateCb * callback)204 int32_t RegNodeDeviceStateCb(const char *pkgName, INodeStateCb *callback)
205 {
206 if (pkgName == NULL || IsValidNodeStateCb(callback) == false) {
207 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fail: invalid parameters");
208 return SOFTBUS_INVALID_PARAM;
209 }
210 if (CommonInit(pkgName) != SOFTBUS_OK) {
211 return SOFTBUS_INVALID_PARAM;
212 }
213 return RegNodeDeviceStateCbInner(pkgName, callback);
214 }
215
UnregNodeDeviceStateCb(INodeStateCb * callback)216 int32_t UnregNodeDeviceStateCb(INodeStateCb *callback)
217 {
218 if (callback == NULL) {
219 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "para callback = null!");
220 return SOFTBUS_INVALID_PARAM;
221 }
222 return UnregNodeDeviceStateCbInner(callback);
223 }
224
StartTimeSync(const char * pkgName,const char * targetNetworkId,TimeSyncAccuracy accuracy,TimeSyncPeriod period,ITimeSyncCb * cb)225 int32_t StartTimeSync(const char *pkgName, const char *targetNetworkId, TimeSyncAccuracy accuracy,
226 TimeSyncPeriod period, ITimeSyncCb *cb)
227 {
228 if (pkgName == NULL || !IsValidString(targetNetworkId, NETWORK_ID_BUF_LEN) ||
229 cb == NULL || cb->onTimeSyncResult == NULL) {
230 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fail: invalid parameters");
231 return SOFTBUS_INVALID_PARAM;
232 }
233 if (CommonInit(pkgName) != SOFTBUS_OK) {
234 return SOFTBUS_INVALID_PARAM;
235 }
236 return StartTimeSyncInner(pkgName, targetNetworkId, accuracy, period, cb);
237 }
238
StopTimeSync(const char * pkgName,const char * targetNetworkId)239 int32_t StopTimeSync(const char *pkgName, const char *targetNetworkId)
240 {
241 if (pkgName == NULL || !IsValidString(targetNetworkId, NETWORK_ID_BUF_LEN)) {
242 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fail: invalid parameters");
243 return SOFTBUS_INVALID_PARAM;
244 }
245 if (CommonInit(pkgName) != SOFTBUS_OK) {
246 return SOFTBUS_INVALID_PARAM;
247 }
248 return StopTimeSyncInner(pkgName, targetNetworkId);
249 }
250
PublishLNN(const char * pkgName,const PublishInfo * info,const IPublishCb * cb)251 int32_t PublishLNN(const char *pkgName, const PublishInfo *info, const IPublishCb *cb)
252 {
253 if (pkgName == NULL || info == NULL || cb == NULL) {
254 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fail: invalid parameters");
255 return SOFTBUS_INVALID_PARAM;
256 }
257 if (CommonInit(pkgName) != SOFTBUS_OK) {
258 return SOFTBUS_INVALID_PARAM;
259 }
260 if (PublishInfoCheck(info) != SOFTBUS_OK) {
261 return SOFTBUS_INVALID_PARAM;
262 }
263 return PublishLNNInner(pkgName, info, cb);
264 }
265
StopPublishLNN(const char * pkgName,int32_t publishId)266 int32_t StopPublishLNN(const char *pkgName, int32_t publishId)
267 {
268 if (pkgName == NULL) {
269 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fail: invalid parameters");
270 return SOFTBUS_INVALID_PARAM;
271 }
272 if (CommonInit(pkgName) != SOFTBUS_OK) {
273 return SOFTBUS_INVALID_PARAM;
274 }
275 return StopPublishLNNInner(pkgName, publishId);
276 }
277
RefreshLNN(const char * pkgName,const SubscribeInfo * info,const IRefreshCallback * cb)278 int32_t RefreshLNN(const char *pkgName, const SubscribeInfo *info, const IRefreshCallback *cb)
279 {
280 if (pkgName == NULL || info == NULL || cb == NULL) {
281 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fail: invalid parameters");
282 return SOFTBUS_INVALID_PARAM;
283 }
284 if (CommonInit(pkgName) != SOFTBUS_OK) {
285 return SOFTBUS_INVALID_PARAM;
286 }
287 if (SubscribeInfoCheck(info) != SOFTBUS_OK) {
288 return SOFTBUS_INVALID_PARAM;
289 }
290 return RefreshLNNInner(pkgName, info, cb);
291 }
292
StopRefreshLNN(const char * pkgName,int32_t refreshId)293 int32_t StopRefreshLNN(const char *pkgName, int32_t refreshId)
294 {
295 if (pkgName == NULL) {
296 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fail: invalid parameters");
297 return SOFTBUS_INVALID_PARAM;
298 }
299 if (CommonInit(pkgName) != SOFTBUS_OK) {
300 return SOFTBUS_INVALID_PARAM;
301 }
302 return StopRefreshLNNInner(pkgName, refreshId);
303 }
304
ActiveMetaNode(const char * pkgName,const MetaNodeConfigInfo * info,char * metaNodeId)305 int32_t ActiveMetaNode(const char *pkgName, const MetaNodeConfigInfo *info, char *metaNodeId)
306 {
307 if (pkgName == NULL || info == NULL || metaNodeId == NULL) {
308 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "invalid active meta node para");
309 return SOFTBUS_INVALID_PARAM;
310 }
311 if (CommonInit(pkgName) != SOFTBUS_OK) {
312 return SOFTBUS_INVALID_PARAM;
313 }
314 return ActiveMetaNodeInner(pkgName, info, metaNodeId);
315 }
316
DeactiveMetaNode(const char * pkgName,const char * metaNodeId)317 int32_t DeactiveMetaNode(const char *pkgName, const char *metaNodeId)
318 {
319 if (pkgName == NULL || metaNodeId == NULL) {
320 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "invalid deactive meta node para");
321 return SOFTBUS_INVALID_PARAM;
322 }
323 if (CommonInit(pkgName) != SOFTBUS_OK) {
324 return SOFTBUS_INVALID_PARAM;
325 }
326 return DeactiveMetaNodeInner(pkgName, metaNodeId);
327 }
328
GetAllMetaNodeInfo(const char * pkgName,MetaNodeInfo * infos,int32_t * infoNum)329 int32_t GetAllMetaNodeInfo(const char *pkgName, MetaNodeInfo *infos, int32_t *infoNum)
330 {
331 if (pkgName == NULL || infos == NULL || infoNum == NULL || *infoNum > MAX_META_NODE_NUM) {
332 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "invalid query meta node info para");
333 return SOFTBUS_INVALID_PARAM;
334 }
335 if (CommonInit(pkgName) != SOFTBUS_OK) {
336 return SOFTBUS_INVALID_PARAM;
337 }
338 return GetAllMetaNodeInfoInner(pkgName, infos, infoNum);
339 }
340