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