A brief analysis of one of the important features of Openharmony: distributed soft bus

OH distributed soft bus

I. Overview

1.1 Introduction

​ What can a distributed soft bus do?
​ 1.1 Devices can be discovered by sharing a LAN (wifi or Bluetooth).
All devices in the shared network can be found and communicated through keywords such as device names. The key is that there is no need to know the IP address and port of the other party's device in advance.
​ 1.2 Supports data transmission capabilities of messages, bytes, streams, and files.
​ In practical applications, text, pictures, files or audio and video streams can be transmitted.
​ 1.3 Support rich devices, small devices, and lightweight devices.
In practical applications, it can be used across devices. The author passed the test between L0 and L2, L2 and L2, etc.
In reality, there are various communication methods between multiple devices (wifi, NFC, Bluetooth, etc.), and the use of different communication methods is very different, resulting in many communication problems; at the same time, it also faces challenges such as the integration and sharing of communication links between devices and the inability to handle conflicts. The distributed soft bus realizes unified distributed communication management capabilities between near-field devices, and provides link-independent discovery, connection, networking and transmission capabilities between devices. The main functions are as follows:

  • Discovery connection: Provides device discovery and connection capabilities based on wifi, Bluetooth and other communication methods
  • Device networking: Provides unified device networking and topology management capabilities, and provides networked device information for data transmission
typedef enum {
    /* Passive 主动*/
    DISCOVER_MODE_PASSIVE = 0x55,
    //lite device support the passive mode only.
    /* Proactive 被动*/
    DISCOVER_MODE_ACTIVE = 0xAA
} DiscoverMode;
//媒介
typedef enum {
    /* Automatic medium selection */
    AUTO = 0,
    /* Bluetooth*/
    BLE = 1,
    /* wifi*/
    COAP = 2,
    /* USB*/
    USB = 3,
    /* HiLink*/
    COAP1 = 4,
    MEDIUM_BUTT
} ExchangeMedium;
  • Data transmission: Provides data transmission channels to support message, byte data transmission and other capabilities

    Business parties use distributed soft buses to provide APIs to achieve high-speed communication between devices without having to worry about communication details, thereby achieving efficient deployment and operation capabilities of the business platform.

//设备类型 DeviceType
typedef enum {
    /* Smart speaker */
    SMART_SPEAKER = 0x00,
    /* PC */
    DESKTOP_PC,
    /* LAPTOP */
    LAPTOP,
    /* Mobile phone */
    SMART_PHONE,
    /* Table */
    SMART_PAD,
    /* Smart watch */
    SMART_WATCH,
    /* Smart car */
    SMART_CAR,
    /* Kids' watch */
    CHILDREN_WATCH,
    /* Smart TV */
    SMART_TV,
} DeviceType;
typedef enum {
    CONNECT_ADDR_WLAN = 0, /**< WLAN wifi(无线局域网)*/
    CONNECT_ADDR_BR= 0, /**< BR 基础速率*/
    CONNECT_ADDR_BLE = 0, /**< BLE 低功耗蓝牙*/
    CONNECT_ADDR_ETH = 0, /**< Ethernet 以太网*/
    CONNECT_ADDR_MAX = 0, /**< Invalid type*/
} ConnectionAddrType;

1.2 Characteristics of distributed soft bus

  • Auto Discovery/Connect and Play
  • high bandwidth
  • Low latency
  • High reliability
  • open/standard

The soft bus proposes the concept of automatic discovery to achieve a zero-wait self-discovery experience for users. Devices with the same accessory account are automatically discovered without waiting.

Group technology after multi-device interconnection: soft bus networking-heterogeneous network networking, such as Bluetooth-wifi.

After the device goes online, it will register with the network layer. At the same time, the network layer will establish a channel connection with the device and detect device changes in real time.

The network layer is responsible for managing the online and offline transitions of devices. Devices can monitor the devices they are interested in. After the device comes online, it can immediately establish a connection with it, achieving zero waiting.

The soft bus can automatically build a logical fully connected network, and users or business developers do not need to care about the networking method and physical protocol. For software developers, soft bus heterogeneous networking can greatly reduce development costs.

1.3 Official description

//foundation/communication/dsoftbus/README_zh.md

1.4 Directory structure and code description

Compare code description

2. Use of distributed soft bus

2.1 Instructions for use

  • When using cross-device communication, you must add the permission ohos.permission.DISTRIBUTED_DATASYNC, which is of type * dangerous* _ .

L2 and L1 devices determine the application's transmission permissions by checking the softbus_trans_permission.json file. L0 does not need to set permissions.

  • When the device actively discovers the mobile phone, the mobile phone must turn on the Hyper Terminal's Allow to be discovered by "nearby devices" switch (Settings-Hyper Terminal-My Device-Allow Discovery-Nearby Devices) before it can be discovered by the device.
  • include header files
# C头文件
//foundation/communication/dsoftbus/interfaces/kits/discovery/discovery_service.h
#C++头文件
//foundation/communication/dsoftbus/sdk/discovery/ipc/include/disc_server_proxy.h
  • Contains dependency packages
# C依赖包
deps = [
    "//foundation/communication/dsoftbus/adapter:softbus_adapter",
    "//foundation/communication/dsoftbus/core/common:softbus_utils",
]
# C++依赖包
deps = [
    "//foundation/communication/dsoftbus/sdk:softbus_client",
    "//foundation/communication/dsoftbus/core/common:softbus_server",
]

2.2 Interface description

The distributed soft bus interface is divided into three categories

  • Discover
  • Networking
  • transmission

2.3 Discovery

2.3.1 Discovery interface

  1. C interface
//foundation/communication/dsoftbus/interfaces/kits/discovery/discovery_service.h
// 发布回调
typedef struct {
    void (*OnPublishSuccess)(int publishId); //发布成功时回调
    void (*OnPublishFail)(int publishId, PublishFailReason reason);//发布失败时回调
} IPublishCallback;
// 发布服务
int PublishService(const char *pkgName, const PublishInfo *info, const IPublishCallback *cb);
// 注销服务
int UnPublishService(const char *pkgName, int publishId);
// 发现回调
typedef struct {
    void (*OnDeviceFound)(const DeviceInfo *device); //发现设备回调
    void (*OnDiscoverFailed)(int subscribeId, DiscoveryFailReason failReason); //启动发现失败回调
    void (*O

Guess you like

Origin blog.csdn.net/procedurecode/article/details/132110743