1 /*
2  * Copyright (c) 2021-2024 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 /**
17  * @addtogroup SoftBus
18  * @{
19  *
20  * @brief Provides high-speed, secure communication between devices.
21  *
22  * This module implements unified distributed communication capability management between nearby devices, and provides
23  * link-independent device discovery and transmission interfaces to support service publishing and data transmission.
24  *
25  * @since 1.0
26  * @version 1.0
27  */
28 /** @} */
29 
30 /**
31  * @file softbus_common.h
32  *
33  * @brief Declares common APIs for the Intelligent Soft Bus.
34  *
35  * This file provides common functions and constants for each submodule of the Intelligent Soft Bus, including: \n
36  *
37  * <ul>
38  * <li>Constants such as the network ID length</li>
39  * <li>Functions such as that for initializing the Intelligent Soft Bus client</li>
40  * </ul>
41  *
42  * @since 1.0
43  * @version 1.0
44  */
45 
46 #ifndef SOFTBUS_CLIENT_COMMON_H
47 #define SOFTBUS_CLIENT_COMMON_H
48 
49 #include <stdbool.h>
50 #include <stdint.h>
51 
52 #ifdef __cplusplus
53 extern "C" {
54 #endif
55 
56 /**
57  * @brief Permission of softbus component
58  *
59  * @since 3.0
60  * @version 3.0
61 */
62 #define OHOS_PERMISSION_DISTRIBUTED_DATASYNC "ohos.permission.DISTRIBUTED_DATASYNC"
63 #define OHOS_PERMISSION_DISTRIBUTED_SOFTBUS_CENTER "ohos.permission.DISTRIBUTED_SOFTBUS_CENTER"
64 
65 /**
66  * @brief Indicates the length of the Bluetooth device MAC address in string format,
67  * including the terminating null character <b>\0</b>.
68  *
69  * @since 1.0
70  * @version 1.0
71  */
72 #define BT_MAC_LEN 18
73 
74 /**
75  * @brief Indicates the length of the network ID string, including the terminating null character <b>\0</b>.
76  *
77  * @since 1.0
78  * @version 1.0
79  */
80 #define NETWORK_ID_BUF_LEN 65
81 
82 /**
83  * @brief Indicates the length of the UDID string, including the terminating null character <b>\0</b>.
84  *
85  * @since 1.0
86  * @version 1.0
87  */
88 #define UDID_BUF_LEN 65
89 
90 /**
91  * @brief Indicates the length of the UDID hash value.
92  *
93  * @since 1.0
94  * @version 1.0
95  */
96 #define UDID_HASH_LEN 32
97 
98 /**
99  * @brief Indicates the length of the UUID string, including the terminating null character <b>\0</b>.
100  *
101  * @since 1.0
102  * @version 1.0
103  */
104 #define UUID_BUF_LEN 65
105 
106 /**
107  * @brief Indicates the maximum length of an IP address in string format,
108  * including the terminating null character <b>\0</b>. IPv6 addresses are supported.
109  *
110  * @since 1.0
111  * @version 1.0
112  */
113 #define IP_STR_MAX_LEN 46
114 
115 /**
116  * @brief Indicates the maximum length of the account hash code in <b>IDiscoveryCallback</b>.
117  *
118  */
119 #define MAX_ACCOUNT_HASH_LEN 96
120 
121 /**
122  * @brief Indicates the maximum length of the hash code in HEX calculated by SHA-256.
123  *
124  */
125 #define SHA_256_HASH_LEN 32
126 
127 /**
128  * @brief Indicates the maximum length of the hash code in string format calculated by SHA-256,
129  * including the terminating null character <b>\0</b>.
130  *
131  */
132 #define SHA_256_HEX_HASH_LEN 65
133 
134 /**
135  * @brief Indicates the maximum length of the capability data in <b>PublishInfo</b> and <b>SubscribeInfo</b>.
136  *
137  */
138 #define MAX_CAPABILITYDATA_LEN 513
139 
140 /**
141  * @brief Indicates the maximum length of the custom data in <b>IDiscoveryCallback</b>.
142  *
143  */
144 #define DISC_MAX_CUST_DATA_LEN 513
145 
146 /**
147  * @brief Indicates the maximum number of capabilities contained in the bitmap in <b>IDiscoveryCallback</b>.
148  *
149  */
150 #define DISC_MAX_CAPABILITY_NUM 2
151 
152 /**
153  * @brief Indicates the maximum length of the device name in <b>IDiscoveryCallback</b>.
154  *
155  */
156 #define DISC_MAX_DEVICE_NAME_LEN 65
157 
158 /**
159  * @brief Indicates the maximum length of the device ID in <b>IDiscoveryCallback</b>.
160  *
161  */
162 #define DISC_MAX_DEVICE_ID_LEN 96
163 
164 /**
165  * @brief Indicates the maximum length of the network commmon length <b>IDiscoveryCallback</b>.
166  *
167  */
168 #define LNN_COMMON_LEN 4
169 
170 /**
171  * @brief Indicates the short hash length of the networkId.
172  *
173  */
174 #define NODEID_SHORT_HASH_LEN 6
175 
176 /**
177  * @brief Indicates the short hash length of the udid.
178  *
179  */
180 #define UDID_SHORT_HASH_LEN 6
181 
182 /**
183  * @brief Indicates the maximum length of the device database status in <b>INodeStateCb</b>.
184  *
185  */
186 #define DATA_CHANGE_FLAG_BUF_LEN 2
187 
188 /**
189  * @brief Indicates the maximum length of the database dynamic level in <b>IDataLevelCb</b>.
190  *
191  */
192 #define DATA_DYNAMIC_LEVEL_BUF_LEN 2
193 
194 /**
195  * @brief Indicates the maximum length of the database static level in <b>IDataLevelCb</b>.
196  *
197  */
198 #define DATA_STATIC_LEVEL_BUF_LEN 2
199 
200 /**
201  * @brief Indicates the maximum length of the database switch level in <b>IDataLevelCb</b>.
202  *
203  */
204 #define DATA_SWITCH_LEVEL_BUF_LEN 4
205 
206 /**
207  * @brief Device screen on/off bool data.
208  *
209  */
210 #define DATA_DEVICE_SCREEN_STATUS_LEN 1
211 
212 /**
213  * @brief Indicates the maximum length of the database switch length in <b>IDataLevelCb</b>.
214  *
215  */
216 #define DATA_SWITCH_LENGTH_BUF_LEN 2
217 
218 /**
219  * @brief Indicates the maximum length of the node address.
220  *
221  */
222 #define SHORT_ADDRESS_MAX_LEN 20
223 
224 /**
225  * @brief Indicates the maximum num of the node status.
226  *
227  */
228 #define NODE_STATUS_MAX_NUM 32
229 
230 /**
231  * @brief Indicates the maximum num of the device type size.
232  *
233  */
234 #define DEVICE_TYPE_MAX_SIZE 3
235 
236 /**
237  * @brief Indicates the length of the node screen status.
238  *
239  */
240 #define NODE_SCREEN_STATUS_LEN 1
241 
242 /**
243  * @brief Enumerates {@link ConnectionAddrType} types of a device that is added to a LNN.
244  *
245  * @since 1.0
246  * @version 1.0
247  */
248 typedef enum {
249     CONNECTION_ADDR_WLAN = 0, /**< WLAN */
250     CONNECTION_ADDR_BR,       /**< BR */
251     CONNECTION_ADDR_BLE,      /**< BLE */
252     CONNECTION_ADDR_ETH,      /**< Ethernet */
253     CONNECTION_ADDR_SESSION,  /**< SESSION */
254     CONNECTION_ADDR_MAX       /**< Invalid type */
255 } ConnectionAddrType;
256 
257 /**
258  * @brief Enumerates {@link BleProtocolType} types of ble connection type
259  *
260  */
261 typedef enum  {
262     BLE_PROTOCOL_ANY = -1,
263     BLE_GATT = 0,
264     BLE_COC,
265     BLE_PROTOCOL_MAX
266 } BleProtocolType;
267 
268 /**
269  * @brief Defines the address of a device that is added to a LNN.
270  * For details, see {@link ConnectionAddr}.
271  *
272  * @since 1.0
273  * @version 1.0
274  */
275 typedef struct {
276     /**< Address type. This field is used to explain the <b>info</b> field. */
277     ConnectionAddrType type;
278     /**< Connection address information */
279     union {
280         /**< BR address */
281         struct BrAddr {
282             char brMac[BT_MAC_LEN];   /**< BR MAC address in string format */
283         } br;
284         /**< BLE address */
285         struct BleAddr {
286             BleProtocolType protocol;
287             char bleMac[BT_MAC_LEN];  /**< BLE MAC address in string format */
288             uint8_t udidHash[UDID_HASH_LEN];  /**< udid hash value */
289             uint32_t psm;
290         } ble;
291         /**< IPv4 or IPv6 address */
292         struct IpAddr {
293             /**
294              * IP address in string format. It can be an IPv4 address written in dotted decimal notation
295              * or an IPv6 address written in hexadecimal colon-separated notation.
296              */
297             char ip[IP_STR_MAX_LEN];
298             uint16_t port;            /**< Port number represented by the host byte order */
299             uint8_t udidHash[UDID_HASH_LEN]; /**< udid hash value */
300         } ip;
301         /**< Session address */
302         struct SessionAddr {
303             int32_t sessionId;  /**< Session Id in int format */
304             int32_t channelId;  /**< Channel Id in int format */
305             int32_t type;   /**< Session type in int format */
306         } session;
307     } info;
308     char peerUid[MAX_ACCOUNT_HASH_LEN];
309 } ConnectionAddr;
310 
311 /**
312  * @brief Enumerates the modes in which services are published.
313  *
314  */
315 typedef enum  {
316     /* Passive */
317     DISCOVER_MODE_PASSIVE = 0x55,
318     /* Proactive */
319     DISCOVER_MODE_ACTIVE  = 0xAA
320 } DiscoverMode;
321 
322 /**
323  * @brief Enumerates media, such as Bluetooth, Wi-Fi, and USB, used for publishing services.
324  *
325  * Currently, only <b>COAP</b> is supported.
326  * When <b>AUTO</b> is selected, all the supported media will be called automatically.
327  */
328 typedef enum {
329     /** Automatic medium selection */
330     AUTO = 0,
331     /** Bluetooth */
332     BLE = 1,
333     /** Wi-Fi */
334     COAP = 2,
335     /** USB */
336     USB = 3,
337     /** HiLink */
338     COAP1 = 4,
339     MEDIUM_BUTT
340 } ExchangeMedium;
341 
342 /**
343  * @brief Enumerates frequencies for publishing services.
344  *
345  * This enumeration applies only to Bluetooth and is not supported currently.
346  */
347 typedef enum {
348     /** Low */
349     LOW = 0,
350     /** Medium */
351     MID = 1,
352     /** High */
353     HIGH = 2,
354     /** Super-high */
355     SUPER_HIGH = 3,
356     /** Extreme-high */
357     EXTREME_HIGH = 4,
358     FREQ_BUTT
359 } ExchangeFreq;
360 
361 /**
362  * @brief Enumerates supported capabilities published by a device.
363  *
364  */
365 typedef enum {
366     /** MeeTime */
367     HICALL_CAPABILITY_BITMAP = 0,
368     /** Video reverse connection in the smart domain */
369     PROFILE_CAPABILITY_BITMAP = 1,
370     /** Gallery in Vision */
371     HOMEVISIONPIC_CAPABILITY_BITMAP = 2,
372     /** cast+ */
373     CASTPLUS_CAPABILITY_BITMAP,
374     /** Input method in Vision */
375     AA_CAPABILITY_BITMAP,
376     /** Device virtualization tool package */
377     DVKIT_CAPABILITY_BITMAP,
378     /** Distributed middleware */
379     DDMP_CAPABILITY_BITMAP,
380     /** Osd capability */
381     OSD_CAPABILITY_BITMAP,
382     /**Share capability */
383     SHARE_CAPABILITY_BITMAP,
384     /**Approach capability */
385     APPROACH_CAPABILITY_BITMAP,
386     /**virtual link capability */
387     VLINK_CAPABILITY_BITMAP,
388     /**Touch capability */
389     TOUCH_CAPABILITY_BITMAP
390 } DataBitMap;
391 
392 typedef struct {
393     int64_t authId;
394     uint32_t type;
395 } AuthHandle;
396 
397 /**
398  * @brief Defines the mapping between supported capabilities and bitmaps.
399  *
400  */
401 typedef struct {
402     /** Bitmaps. For details, see {@link DataBitMap}. */
403     DataBitMap bitmap;
404     /** Capability. For details, see {@link g_capabilityMap}. */
405     char *capability;
406 } CapabilityMap;
407 
408 /**
409  * @brief Defines the mapping between supported capabilities and bitmaps.
410  *
411  */
412 static const CapabilityMap g_capabilityMap[] = {
413     {HICALL_CAPABILITY_BITMAP, (char *)"hicall"},
414     {PROFILE_CAPABILITY_BITMAP, (char *)"profile"},
415     {HOMEVISIONPIC_CAPABILITY_BITMAP, (char *)"homevisionPic"},
416     {CASTPLUS_CAPABILITY_BITMAP, (char *)"castPlus"},
417     {AA_CAPABILITY_BITMAP, (char *)"aaCapability"},
418     {DVKIT_CAPABILITY_BITMAP, (char *)"dvKit"},
419     {DDMP_CAPABILITY_BITMAP, (char *)"ddmpCapability"},
420     {OSD_CAPABILITY_BITMAP, (char *)"osdCapability"},
421     {SHARE_CAPABILITY_BITMAP, (char *)"share"},
422     {APPROACH_CAPABILITY_BITMAP, (char *)"approach"},
423     {VLINK_CAPABILITY_BITMAP, (char *)"virtualLink"},
424     {TOUCH_CAPABILITY_BITMAP, (char *)"touch"}
425 };
426 
427 /**
428  * @brief Defines service publishing information.
429  *
430  */
431 typedef struct {
432     /** Service ID */
433     int publishId;
434     /** Discovery mode for service publishing. For details, see {@link Discovermode}. */
435     DiscoverMode mode;
436     /** Service publishing medium. For details, see {@link ExchangeMedium}. */
437     ExchangeMedium medium;
438     /** Service publishing frequency. For details, see {@link ExchangeFreq}. */
439     ExchangeFreq freq;
440     /** Service publishing capabilities. For details, see {@link g_capabilityMap}. */
441     const char *capability;
442     /** Capability data for service publishing, MUST be c-string format. */
443     unsigned char *capabilityData;
444     /** Maximum length of the capability data for service publishing (512 bytes) */
445     unsigned int dataLen;
446     /** Whether the device should be ranged  by discoverers.*/
447     bool ranging;
448 } PublishInfo;
449 
450 /**
451  * @brief Defines service subscription information.
452  *
453  */
454 typedef struct {
455     /** Service ID */
456     int subscribeId;
457     /** Discovery mode for service subscription. For details, see {@link Discovermode}. */
458     DiscoverMode mode;
459     /** Service subscription medium. For details, see {@link ExchangeMedium}. */
460     ExchangeMedium medium;
461     /** Service subscription frequency. For details, see {@link ExchangeFreq}. */
462     ExchangeFreq freq;
463     /** only find the device with the same account */
464     bool isSameAccount;
465     /** find the sleeping devices */
466     bool isWakeRemote;
467     /** Service subscription capability. For details, see {@link g_capabilityMap}. */
468     const char *capability;
469     /** Capability data for service subscription, MUST be c-string format. */
470     unsigned char *capabilityData;
471     /** Maximum length of the capability data for service subscription (512 bytes) */
472     unsigned int dataLen;
473 } SubscribeInfo;
474 
475 /**
476  * @brief Enumerates single heartbeat cycle parameter.
477  *
478  * @since 1.0
479  * @version 1.0
480  */
481 typedef enum {
482     /**< Heartbeat interval 30 sec */
483     HIGH_FREQ_CYCLE = 30,
484     /**< Heartbeat interval 60 sec */
485     MID_FREQ_CYCLE = 60,
486     /**< Heartbeat interval 5 * 60 sec */
487     LOW_FREQ_CYCLE = 5 * 60,
488     /**< Heartbeat interval 10 * 60 sec */
489     DEFAULT_FREQ_CYCLE = 10 * 60,
490 } ModeCycle;
491 
492 /**
493  * @brief Enumerates duration of heartbeat keeping alive parameter.
494  *
495  * @since 1.0
496  * @version 1.0
497  */
498 typedef enum {
499     /**< Heartbeat continues for 60 sec */
500     DEFAULT_DURATION = 60,
501     /**< Heartbeat continues for 10 * 60 sec. */
502     NORMAL_DURATION = 10 * 60,
503     /**< Heartbeat continues for 30 * 60 sec. */
504     LONG_DURATION = 30 * 60,
505 } ModeDuration;
506 
507 /**
508  * @brief Enumerates device types.
509  *
510  */
511 typedef enum {
512     /* Smart speaker */
513     SMART_SPEAKER = 0x00,
514     /* PC */
515     DESKTOP_PC,
516     /* Laptop */
517     LAPTOP,
518     /* Mobile phone */
519     SMART_PHONE,
520     /* Tablet */
521     SMART_PAD,
522     /* Smart watch */
523     SMART_WATCH,
524     /* Smart car */
525     SMART_CAR,
526     /* Kids' watch */
527     CHILDREN_WATCH,
528     /* Smart TV */
529     SMART_TV,
530 } DeviceType;
531 
532 /**
533  * @brief Defines the device information returned by <b>IDiscoveryCallback</b>.
534  *
535  */
536 typedef struct {
537     /** Device ID. Its maximum length is specified by {@link DISC_MAX_DEVICE_ID_LEN}. */
538     char devId[DISC_MAX_DEVICE_ID_LEN];
539     /** Account hash code. Its maximum length is specified by {@link MAX_ACCOUNT_HASH_LEN}. */
540     char accountHash[MAX_ACCOUNT_HASH_LEN];
541     /** Device type. For details, see {@link DeviceType}. */
542     DeviceType devType;
543     /** Device name. Its maximum length is specified by {@link DISC_MAX_DEVICE_NAME_LEN}. */
544     char devName[DISC_MAX_DEVICE_NAME_LEN];
545     /** Device Online Status **/
546     bool isOnline;
547     /** Number of available connections */
548     unsigned int addrNum;
549     /** Connection information. For details, see {@link ConnectionAddr}. */
550     ConnectionAddr addr[CONNECTION_ADDR_MAX];
551     /** Number of capabilities */
552     unsigned int capabilityBitmapNum;
553     /** Device capability bitmap.
554      * The maximum number of capabilities in the bitmap is specified by {@link DISC_MAX_CAPABILITY_NUM}.
555      */
556     unsigned int capabilityBitmap[DISC_MAX_CAPABILITY_NUM];
557     /** Custom data. Its length is specified by {@link DISC_MAX_CUST_DATA_LEN}. */
558     char custData[DISC_MAX_CUST_DATA_LEN];
559     /** The distance of discovered device, in centimeters(cm)*/
560     int32_t range;
561 } DeviceInfo;
562 
563 /**
564  * @brief Defines device additional info used by inner
565  *
566  */
567 typedef struct {
568     /** medium which describe the device found by. */
569     ExchangeMedium medium;
570 } InnerDeviceInfoAddtions;
571 
572 /**
573  * @brief Defines the capability enumeration of suppressing and restoring ble.
574  * the value same as lnn_heartbeat_utils.h
575  *
576  */
577 typedef enum {
578     /* Suppress ble */
579     REQUEST_DISABLE_BLE_DISCOVERY = 100,
580     /* Restore ble */
581     REQUEST_ENABLE_BLE_DISCOVERY,
582 } StrategyForBle;
583 
584 #ifdef __cplusplus
585 }
586 #endif
587 #endif
588 /** @} */
589