• 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 #ifndef NSTACKX_H
17 #define NSTACKX_H
18 
19 #include <stdint.h>
20 #include <stdbool.h>
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
26 #define NSTACKX_MAX_DEVICE_NAME_LEN 64
27 #define NSTACKX_MAX_MODULE_NAME_LEN 64
28 #define NSTACKX_MAX_DEVICE_ID_LEN 96
29 #define NSTACKX_MAX_SENDMSG_DATA_LEN 512
30 #define NSTACKX_MAX_MAC_STRING_LEN 18
31 #define NSTACKX_MAX_IP_STRING_LEN 16
32 #define NSTACKX_MAX_CAPABILITY_NUM 2
33 #define NSTACKX_MAX_INTERFACE_NAME_LEN 16
34 #define NSTACKX_MAX_HICOM_VERSION 16
35 #define NSTACKX_MAX_SERVICE_DATA_LEN 64
36 #define NSTACKX_MAX_EXTEND_SERVICE_DATA_LEN 128
37 #ifndef NSTACKX_EXTEND_BUSINESSDATA
38 #define NSTACKX_MAX_BUSINESS_DATA_LEN 1
39 #else
40 #define NSTACKX_MAX_BUSINESS_DATA_LEN 300
41 #endif
42 
43 #ifdef DFINDER_SAVE_DEVICE_LIST
44 #define NSTACKX_MIN_DEVICE_NUM 1
45 #define NSTACKX_DEFAULT_DEVICE_NUM 20
46 #define NSTACKX_MAX_DEVICE_NUM 400
47 #else
48 #define NSTACKX_MAX_DEVICE_NUM 1
49 #endif
50 
51 // expand from 131 to 219 (+88) bytes to hold service data
52 // expand from 219 to 400 (+128 +53) bytes to hold extend service data
53 // expand from 400 to (420 + NSTACKX_MAX_BUSINESS_DATA_LEN) bytes to hold business data and type
54 #define NSTACKX_MAX_RESERVED_INFO_LEN (420 + NSTACKX_MAX_BUSINESS_DATA_LEN)
55 
56 #define DEVICE_HASH_LEN 21
57 enum {
58     DEFAULT_MODE = 0,
59     DISCOVER_MODE = 1,
60     PUBLISH_MODE_UPLINE = 2,
61     PUBLISH_MODE_OFFLINE = 3,
62     PUBLISH_MODE_PROACTIVE = 10
63 }; // discovery mode
64 #define PUBLISH_DEVICE_NUM 1
65 #define INNER_DISCOVERY 1
66 #define PUBLISH_NUM 1
67 #define COUNT_INIT 0
68 
69 enum {
70     NSTACKX_DISCOVERY_TYPE_PASSIVE = 1,
71     NSTACKX_DISCOVERY_TYPE_ACTIVE = 2
72 };
73 
74 #ifndef DFINDER_EXPORT
75 #ifdef _WIN32
76 #define DFINDER_EXPORT __declspec(dllexport)
77 #else
78 #define DFINDER_EXPORT
79 #endif
80 #endif
81 
82 /* Remote device information */
83 typedef struct NSTACKX_DeviceInfo {
84     char deviceId[NSTACKX_MAX_DEVICE_ID_LEN];
85     char deviceName[NSTACKX_MAX_DEVICE_NAME_LEN];
86     uint32_t capabilityBitmapNum;
87     uint32_t capabilityBitmap[NSTACKX_MAX_CAPABILITY_NUM];
88     uint8_t deviceType;
89     uint8_t mode;
90 #ifdef DFINDER_SAVE_DEVICE_LIST
91     uint8_t update : 1;
92     uint8_t reserved : 7;
93     char networkName[NSTACKX_MAX_INTERFACE_NAME_LEN];
94 #endif
95     uint8_t discoveryType;
96     uint8_t businessType;
97     char version[NSTACKX_MAX_HICOM_VERSION];
98     char reservedInfo[NSTACKX_MAX_RESERVED_INFO_LEN];
99 } NSTACKX_DeviceInfo;
100 
101 #ifdef DFINDER_SUPPORT_MULTI_NIF
102 #define NSTACKX_MAX_LISTENED_NIF_NUM 2
103 #else
104 #define NSTACKX_MAX_LISTENED_NIF_NUM 1
105 #endif
106 typedef struct {
107     char networkName[NSTACKX_MAX_INTERFACE_NAME_LEN];
108     char networkIpAddr[NSTACKX_MAX_IP_STRING_LEN];
109 } NSTACKX_InterfaceInfo;
110 
111 
112 /* Local device information */
113 typedef struct {
114     char name[NSTACKX_MAX_DEVICE_NAME_LEN];
115     char deviceId[NSTACKX_MAX_DEVICE_ID_LEN];
116     char btMacAddr[NSTACKX_MAX_MAC_STRING_LEN];
117     char wifiMacAddr[NSTACKX_MAX_MAC_STRING_LEN];
118 
119     /* Configuration for network interface */
120     NSTACKX_InterfaceInfo localIfInfo[NSTACKX_MAX_LISTENED_NIF_NUM];
121     uint8_t ifNums;
122 
123     /* Obsoleted. Use localIfInfo instead. */
124     char networkIpAddr[NSTACKX_MAX_IP_STRING_LEN];
125     /* Obsoleted. Use localIfInfo instead. */
126     char networkName[NSTACKX_MAX_INTERFACE_NAME_LEN];
127     uint8_t is5GHzBandSupported;
128     uint8_t deviceType;
129     char version[NSTACKX_MAX_HICOM_VERSION];
130     uint8_t businessType;
131 } NSTACKX_LocalDeviceInfo;
132 
133 typedef enum {
134     NSTACKX_BUSINESS_TYPE_NULL = 0,
135     NSTACKX_BUSINESS_TYPE_HICOM = 1,
136     NSTACKX_BUSINESS_TYPE_SOFTBUS = 2,
137     NSTACKX_BUSINESS_TYPE_NEARBY = 3
138 } NSTACKX_BusinessType;
139 
140 #define NSTACKX_MIN_ADVERTISE_COUNT 1
141 #define NSTACKX_MAX_ADVERTISE_COUNT 100
142 /* The unit of duration is ms. */
143 #define NSTACKX_MIN_ADVERTISE_DURATION 5000
144 #define NSTACKX_MAX_ADVERTISE_DURATION 50000
145 
146 typedef struct {
147     uint8_t businessType;
148     uint8_t discoveryMode;
149     uint32_t advertiseCount;
150     uint32_t advertiseDuration;
151     char *businessData;
152     uint32_t length;
153 } NSTACKX_DiscoverySettings;
154 
155 /* Register local device information */
156 DFINDER_EXPORT int32_t NSTACKX_RegisterDevice(const NSTACKX_LocalDeviceInfo *localDeviceInfo);
157 
158 /* Register local device name */
159 DFINDER_EXPORT int32_t NSTACKX_RegisterDeviceName(const char *devName);
160 
161 /* Register local device information with deviceHash */
162 DFINDER_EXPORT int32_t NSTACKX_RegisterDeviceAn(const NSTACKX_LocalDeviceInfo *localDeviceInfo, uint64_t deviceHash);
163 
164 /* Device list change callback type */
165 typedef void (*NSTACKX_OnDeviceListChanged)(const NSTACKX_DeviceInfo *deviceList, uint32_t deviceCount);
166 
167 /* Data receive callback type */
168 typedef void (*NSTACKX_OnMsgReceived)(const char *moduleName, const char *deviceId,
169                                       const uint8_t *data, uint32_t len);
170 
171 /* DFinder message type list. */
172 typedef enum {
173     DFINDER_ON_TOO_BUSY = 1,
174     DFINDER_ON_INNER_ERROR,
175 } DFinderMsgType;
176 
177 /* Data receive callback type */
178 typedef void (*NSTACKX_OnDFinderMsgReceived)(DFinderMsgType msgType);
179 
180 /* NSTACKX parameter, which contains callback list */
181 typedef struct {
182     NSTACKX_OnDeviceListChanged onDeviceListChanged;
183     NSTACKX_OnDeviceListChanged onDeviceFound;
184     NSTACKX_OnMsgReceived onMsgReceived;
185     NSTACKX_OnDFinderMsgReceived onDFinderMsgReceived;
186     uint32_t maxDeviceNum; // the size of the device list configured by the caller
187 } NSTACKX_Parameter;
188 
189 /* DFinder log level */
190 enum {
191     DFINDER_LOG_LEVEL_OFF     = 0,
192     DFINDER_LOG_LEVEL_FATAL   = 1,
193     DFINDER_LOG_LEVEL_ERROR   = 2,
194     DFINDER_LOG_LEVEL_WARNING = 3,
195     DFINDER_LOG_LEVEL_INFO    = 4,
196     DFINDER_LOG_LEVEL_DEBUG   = 5,
197     DFINDER_LOG_LEVEL_END,
198 };
199 
200 typedef enum {
201     DFINDER_EVENT_TYPE_FAULT,
202     DFINDER_EVENT_TYPE_STATISTIC,
203     DFINDER_EVENT_TYPE_SECURITY,
204     DFINDER_EVENT_TYPE_BEHAVIOR,
205 } DFinderEventType;
206 
207 typedef enum {
208     DFINDER_EVENT_LEVEL_CRITICAL,
209     DFINDER_EVENT_LEVEL_MINOR,
210 } DFinderEventLevel;
211 
212 typedef enum {
213     DFINDER_PARAM_TYPE_BOOL,
214     DFINDER_PARAM_TYPE_UINT8,
215     DFINDER_PARAM_TYPE_UINT16,
216     DFINDER_PARAM_TYPE_INT32,
217     DFINDER_PARAM_TYPE_UINT32,
218     DFINDER_PARAM_TYPE_UINT64,
219     DFINDER_PARAM_TYPE_FLOAT,
220     DFINDER_PARAM_TYPE_DOUBLE,
221     DFINDER_PARAM_TYPE_STRING,
222 } DFinderEventParamType;
223 
224 #define DFINDER_EVENT_NAME_LEN 33
225 #define DFINDER_EVENT_TAG_LEN 16
226 
227 typedef struct {
228     DFinderEventParamType type;
229     char name[DFINDER_EVENT_NAME_LEN];
230     union {
231         bool b;
232         uint8_t u8v;
233         uint16_t u16v;
234         int32_t i32v;
235         uint32_t u32v;
236         uint64_t u64v;
237         float f;
238         double d;
239         char str[DFINDER_EVENT_NAME_LEN];
240     } value;
241 } DFinderEventParam;
242 
243 typedef struct {
244     char eventName[DFINDER_EVENT_NAME_LEN];
245     DFinderEventType type;
246     DFinderEventLevel level;
247     uint32_t paramNum;
248     DFinderEventParam *params;
249 } DFinderEvent;
250 
251 typedef void (*DFinderEventFunc)(void *softObj, const DFinderEvent *info);
252 
253 DFINDER_EXPORT int NSTACKX_DFinderSetEventFunc(void *softobj, DFinderEventFunc func);
254 
255 typedef void (*DFinderDumpFunc)(void *softObj, const char *data, uint32_t len);
256 DFINDER_EXPORT int NSTACKX_DFinderDump(const char **argv, uint32_t argc, void *softObj, DFinderDumpFunc dump);
257 
258 /*
259  * NSTACKX Initialization
260  * return 0 on success, negative value on failure
261  */
262 DFINDER_EXPORT int32_t NSTACKX_Init(const NSTACKX_Parameter *parameter);
263 
264 /* NSTACKX Destruction */
265 DFINDER_EXPORT void NSTACKX_Deinit(void);
266 
267 /*
268  * Start device discovery
269  * return 0 on success, negative value on failure
270  */
271 DFINDER_EXPORT int32_t NSTACKX_StartDeviceFind(void);
272 
273 /*
274  * Start device discovery by mode
275  * return 0 on success, negative value on failure
276  */
277 DFINDER_EXPORT int32_t NSTACKX_StartDeviceFindAn(uint8_t mode);
278 
279 /*
280  * Stop device discovery
281  * return 0 on success, negative value on failure
282  */
283 DFINDER_EXPORT int32_t NSTACKX_StopDeviceFind(void);
284 
285 /*
286  * subscribe module
287  * return 0 on success, negative value on failure
288  */
289 DFINDER_EXPORT int32_t NSTACKX_SubscribeModule(void);
290 
291 /*
292  * unsubscribe module
293  * return 0 on success, negative value on failure
294  */
295 DFINDER_EXPORT int32_t NSTACKX_UnsubscribeModule(void);
296 
297 /*
298  * Register the capability of local device.
299  * return 0 on success, negative value on failure
300  */
301 DFINDER_EXPORT int32_t NSTACKX_RegisterCapability(uint32_t capabilityBitmapNum, uint32_t capabilityBitmap[]);
302 
303 /*
304  * Set the capability to filter remote devices.
305  * return 0 on success, negative value on failure
306  */
307 DFINDER_EXPORT int32_t NSTACKX_SetFilterCapability(uint32_t capabilityBitmapNum, uint32_t capabilityBitmap[]);
308 
309 /*
310  * Register the serviceData of local device.
311  * return 0 on success, negative value on failure
312  */
313 DFINDER_EXPORT int32_t NSTACKX_RegisterServiceData(const char *serviceData);
314 
315 /*
316  * Register the extendServiceData of local device.
317  * return 0 on success, negative value on failure
318  */
319 DFINDER_EXPORT int32_t NSTACKX_RegisterExtendServiceData(const char *extendServiceData);
320 
321 /*
322  * Send Msg to remote peer
323  * return 0 on success, negative value on failure
324  */
325 DFINDER_EXPORT int32_t NSTACKX_SendMsg(const char *moduleName, const char *deviceId, const uint8_t *data,
326                                        uint32_t len);
327 
328 /*
329  * Send Msg to remote peer
330  * return 0 on success, negative value on failure
331  */
332 DFINDER_EXPORT int32_t NSTACKX_SendMsgDirect(const char *moduleName, const char *deviceId, const uint8_t *data,
333     uint32_t len, const char *ipaddr, uint8_t sendType);
334 
335 /*
336  * Get device list from cache
337  * param: deviceList - Device list return from NSTACKX, user should prepare sufficient buffer to store
338  *                     device list.
339  * param: deviceCountPtr - In/Out parameter. It indicates buffer size (number of elements) in deviceList
340  *                         When returns, it indicates numbers of valid device in deviceList.
341  * return 0 on success, negative value on failure
342  */
343 DFINDER_EXPORT int32_t NSTACKX_GetDeviceList(NSTACKX_DeviceInfo *deviceList, uint32_t *deviceCountPtr);
344 
345 /*
346  * NSTACKX Initialization, only used for restart.
347  * return 0 on success, negative value on failure
348  */
349 DFINDER_EXPORT int32_t NSTACKX_InitRestart(const NSTACKX_Parameter *parameter);
350 
351 /*
352  * NSTACKX Initialization, only used for restart.
353  * return 0 on success, negative value on failure
354  */
355 DFINDER_EXPORT void NSTACKX_StartDeviceFindRestart(void);
356 
357 /*
358  * Start device discovery with settings. If advertiseCount and advertiseDuration both 0, discovery with default
359  * advertise settings.
360  * return 0 on success, negative value on failure
361  */
362 DFINDER_EXPORT int32_t NSTACKX_StartDeviceDiscovery(const NSTACKX_DiscoverySettings *discoverySettings);
363 
364 typedef struct {
365     uint8_t businessType;
366     char localNetworkName[NSTACKX_MAX_INTERFACE_NAME_LEN];
367     char remoteIp[NSTACKX_MAX_IP_STRING_LEN];
368     char *businessData;
369     uint32_t length;
370 } NSTACKX_ResponseSettings;
371 
372 /*
373  * Send discovery response to remote in unicast.
374  * return 0 on success, negative value on failure
375  */
376 DFINDER_EXPORT int32_t NSTACKX_SendDiscoveryRsp(const NSTACKX_ResponseSettings *responseSettings);
377 
378 #ifdef ENABLE_USER_LOG
379 typedef void (*DFinderLogCallback)(const char *moduleName, uint32_t logLevel, const char *format, ...);
380 
381 /*
382  * Set the DFinder log implementation
383  */
384 DFINDER_EXPORT int32_t NSTACKX_DFinderRegisterLog(DFinderLogCallback userLogCallback);
385 #endif
386 
387 #ifdef __cplusplus
388 }
389 #endif
390 
391 #endif /* NSTACKX_H */
392