|
Name |
|
Date |
Size |
#Lines |
LOC |
| .. | | - | - |
| adapter/ | | 22-Oct-2025 | - | 18,792 | 15,109 |
| br_proxy/ | | 22-Oct-2025 | - | 4,168 | 3,625 |
| components/ | | 22-Oct-2025 | - | 70,240 | 52,805 |
| core/ | | 22-Oct-2025 | - | 208,819 | 177,619 |
| dfx/ | | 22-Oct-2025 | - | 16,464 | 13,238 |
| figures/ | | 22-Oct-2025 | - | | |
| interfaces/ | | 22-Oct-2025 | - | 17,268 | 9,931 |
| sdk/ | | 22-Oct-2025 | - | 41,582 | 35,003 |
| tests/ | | 22-Oct-2025 | - | 345,973 | 247,290 |
| tools/device_info/ | | 22-Oct-2025 | - | 119 | 98 |
| .clang-format | D | 22-Oct-2025 | 1.2 KiB | 40 | 37 |
| .gitattributes | D | 22-Oct-2025 | 631 | 16 | 15 |
| .gitignore | D | 22-Oct-2025 | 47 | 3 | 2 |
| BUILD.gn | D | 22-Oct-2025 | 743 | 23 | 20 |
| CODEOWNERS | D | 22-Oct-2025 | 8.4 KiB | 169 | 150 |
| Kconfig | D | 22-Oct-2025 | 2.2 KiB | 81 | 69 |
| LICENSE | D | 22-Oct-2025 | 9.9 KiB | 177 | 150 |
| OAT.xml | D | 22-Oct-2025 | 4.9 KiB | 75 | 22 |
| README.md | D | 22-Oct-2025 | 13.5 KiB | 341 | 260 |
| README_zh.md | D | 22-Oct-2025 | 13.6 KiB | 339 | 265 |
| bundle.json | D | 22-Oct-2025 | 14.3 KiB | 361 | 360 |
| check_sub_module.py | D | 22-Oct-2025 | 987 | 34 | 14 |
| config.py | D | 22-Oct-2025 | 2.4 KiB | 84 | 55 |
| dsoftbus.gni | D | 22-Oct-2025 | 6.2 KiB | 194 | 166 |
| hisysevent.yaml | D | 22-Oct-2025 | 33.7 KiB | 649 | 571 |
README.md
1# DSoftBus
2
3## Introduction
4
5There are various ways of communication (such as Wi-Fi and Bluetooth) between devices. The difference in the usage of these communication methods may lead to communication issues. In addition, there are many challenges in converging and sharing communication links between devices and handling conflicts.
6
7DSoftBus implements unified distributed communications between near-field devices and provides APIs for device discovery, connections, networking, and data transmission, regardless of the link type. It stands out with the following capabilities:
8
9- Implements discovery and connections of devices in various communication modes, such as WLAN and Bluetooth.
10- Performs unified device networking and topology management, and provides device information for data transmission.
11- Sets up channels for transmitting messages, bytes, streams, and files.
12
13You can use the APIs provided by DSoftBus to implement fast communication between devices without caring about the communication details, which accelerates deployment and running of services across platforms.
14
15## Architecture
16
17
18
19**Figure 1** DSoftBus architecture
20
21## Directory Structure
22
23The DSoftBus directory structure is as follows:
24
25```text
26//foundation/communication/dsoftbus
27├── adapter # Adaptation code
28├── components # Dependent components
29├── core # Core code
30│ ├── adapter # Adaptation code
31│ ├── authentication # Code for authentication
32│ ├── bus_center # Code for networking
33│ ├── common # Common code
34│ ├── connection # Code for device connections
35│ ├── discovery # Code for device discovery
36│ ├── frame # Framework code
37│ └── transmission # Code for data transmission
38├── interfaces # External APIs
39├── sdk # SDK
40│ ├── bus_center # Networking
41│ ├── discovery # Device discovery
42│ ├── frame # Framework
43│ └── transmission # Transmission
44├── tests # Test code
45└── tools # Tools
46```
47
48## Constraints
49
50- Connections can be set up only between the devices in the same LAN or between near-field devices.
51- Before setting up a connection between two devices, you must bind devices. For details, see [Security](https://gitee.com/openharmony/docs/blob/master/en/readme/Security.md).
52- After data transmission is complete, the service needs to close the session to release resources.
53
54## Usage
55
56>**NOTE**
57>
58>The permissions ohos.permission.DISTRIBUTED_DATASYNC and ohos.permission.DISTRIBUTED_SOFTBUS_CENTER are required for remote procedure calls (RPCs) across devices.
59
60### Device Discovery
61
62- **Publishing process**
63
641. Call **PublishLNN()** to publish application information across a network.
65
66 ```C
67 // Callback used to return the publish result.
68 typedef struct {
69 /** Callback for publish result */
70 void (*OnPublishResult)(int publishId, PublishResult reason);
71 } IPublishCb;
72
73 // Information to publish.
74 typedef struct {
75 int publishId; // Publish ID.
76 DiscoverMode mode; // Discovery mode.
77 ExchangeMedium medium; // Medium to use.
78 ExchangeFreq freq; // Frequency to use.
79 const char *capability; // Capability of the device to discover.
80 unsigned char *capabilityData; // Custom data to publish.
81 unsigned int dataLen; // Data length.
82 bool ranging; // Whether ranging is required.
83 } PublishInfo;
84
85 // Publish information.
86 int32_t PublishLNN(const char *pkgName, const PublishInfo *info, const IPublishCb *cb);
87 ```
88
892. Call **StopPublishLNN** to stop publishing information when it is not required.
90
91 ```C
92 // Stop publishing a service.
93 int32_t StopPublishLNN(const char *pkgName, int32_t publishId);
94 ```
95
96
97- **Discovery process**
98
991. Call **RefreshLNN()** to discover devices with the specified capability.
100
101 ```C
102 // Callbacks for device discovery.
103 typedef struct {
104 /** Callback to be invoked when a device is found. */
105 void (*OnDeviceFound)(const DeviceInfo *device);
106 /** Callback for a subscription result. */
107 void (*OnDiscoverResult)(int32_t refreshId, RefreshResult reason);
108 } IRefreshCallback;
109
110 // Start device discovery.
111 int32_t RefreshLNN(const char *pkgName, const SubscribeInfo *info, const IRefreshCallback *cb);
112 ```
113
114 DSoftBus notifies the service of the device information via a callback once a device is found.
115
1163. Call **StopRefreshLNN()** to stop device discovery.
117
118 ```C
119 // Stop the discovery.
120 int32_t StopRefreshLNN(const char *pkgName, int32_t refreshId);
121 ```
122
123### Networking
124
1251. Initiate a request for connection with the target device address and a callback used to return the connection result.
126
127 ```C
128 // Address of the target device to connect to.
129 typedef struct {
130 ConnectionAddrType type;
131 union {
132 struct BrAddr {
133 char brMac[BT_MAC_LEN];
134 } br;
135 struct BleAddr {
136 char bleMac[BT_MAC_LEN];
137 uint8_t udidHash[UDID_HASH_LEN];
138 } ble;
139 struct IpAddr {
140 char ip[IP_STR_MAX_LEN];
141 uint16_t port;
142 } ip;
143 } info;
144 char peerUid[MAX_ACCOUNT_HASH_LEN];
145 } ConnectionAddr;
146
147 // Address type.
148 typedef enum {
149 CONNECTION_ADDR_WLAN = 0,
150 CONNECTION_ADDR_BR,
151 CONNECTION_ADDR_BLE,
152 CONNECTION_ADDR_ETH,
153 CONNECTION_ADDR_MAX
154 } ConnectionAddrType;
155
156 // Callback used to return the connection result.
157 typedef void (*OnJoinLNNResult)(ConnectionAddr *addr, const char *networkId, int32_t retCode);
158
159 // Initiate a connection request.
160 int32_t JoinLNN(const char *pkgName, ConnectionAddr *target, OnJoinLNNResult cb);
161 ```
162
1632. Wait for the connection result.
164
165 If **JoinLNN()** returns success, DSoftBus accepts the connection request and notifies the service of the connection result through the callback. The **addr** parameter in the callback matches the **target** parameter in **JoinLNN()**. If **retCode** in the callback is **0**, the connection is successful. In this case, the value of **networkId** is valid and will be used in the data transmission and disconnection APIs. If the value of **retCode** is not **0**, the connection fails, and the value of **networkId** is invalid.
166
1673. Transmit data using the related APIs.
168
1694. Initiate a disconnection request with the **networkId** and a callback for returing the result.
170
171 ```C
172 // Callback used to return the disconnection result.
173 typedef void (*OnLeaveLNNResult)(const char *networkId, int32_t retCode);
174
175 // Initiate a disconnection request.
176 int32_t LeaveLNN(const char *pkgName, const char *networkId, OnLeaveLNNResult cb);
177 ```
178
1795. Wait until the disconnection is complete.
180
181 The **networkId** parameter in **OnLeaveLNNResult()** matches **networkId** in **LeaveLNN()**. If **retCode** in the callback is **0**, the disconnection is successful; otherwise, the disconnection fails. If the disconnection is successful, **networkId** becomes invalid and can no longer be used.
182
1836. Register and unregister callbacks for device state changes.
184
185 ```C
186 // Device state events.
187 #define EVENT_NODE_STATE_ONLINE 0x1
188 #define EVENT_NODE_STATE_OFFLINE 0x02
189 #define EVENT_NODE_STATE_INFO_CHANGED 0x04
190 #define EVENT_NODE_STATUS_CHANGED 0x08
191 #define EVENT_NODE_STATE_MASK 0xF
192
193 // Device information.
194 typedef struct {
195 char networkId[NETWORK_ID_BUF_LEN];
196 char deviceName[DEVICE_NAME_BUF_LEN];
197 uint16_t deviceTypeId;
198 } NodeBasicInfo;
199
200 // Device state event callbacks.
201 typedef struct {
202 uint32_t events; // Networking event mask.
203 void (*onNodeOnline)(NodeBasicInfo *info); // Called when the device gets online.
204 void (*onNodeOffline)(NodeBasicInfo *info); // Called when the device gets offline.
205 void (*onNodeBasicInfoChanged)(NodeBasicInfoType type, NodeBasicInfo *info); // Called when the device information changes.
206 void (*onNodeStatusChanged)(NodeStatusType type, NodeStatus *status); // Called when the device running status changes.
207 } INodeStateCb;
208
209 // Register a callback for device state events.
210 int32_t RegNodeDeviceStateCb(const char *pkgName, INodeStateCb *callback);
211
212 // Unregister a callback for device state events.
213 int32_t UnregNodeDeviceStateCb(INodeStateCb *callback);
214 ```
215
216### Transmission
217
2181. Create a **Socket** instance.
219
220 ```C
221 typedef struct {
222 char *name; // Local socket name.
223 char *peerName; // Peer socket name.
224 char *peerNetworkId; // Peer network ID.
225 char *pkgName; // Bundle name of the caller.
226 TransDataType dataType; // Type of the data to be transmitted.
227 } SocketInfo;
228
229 // Create sockets.
230 int32_t Socket(SocketInfo info);
231 ```
232
2332. Start listening for the socket on the server, and bind the socket on the client.
234
235 ```C
236 // Socket callbacks.
237 typedef struct {
238 void (*OnBind)(int32_t socket, PeerSocketInfo info);
239 void (*OnShutdown)(int32_t socket, ShutdownReason reason);
240 void (*OnBytes)(int32_t socket, const void *data, uint32_t dataLen);
241 void (*OnMessage)(int32_t socket, const void *data, uint32_t dataLen);
242 void (*OnStream)(int32_t socket, const StreamData *data, const StreamData *ext, const StreamFrameInfo *param);
243 void (*OnFile)(int32_t socket, FileEvent *event);
244 void (*OnQos)(int32_t socket, QoSEvent eventId, const QosTV *qos, uint32_t qosCount);
245 void (*OnError)(int32_t socket, int32_t errCode);
246 void (*OnBytesSent)(int32_t socket, uint32_t dataSeq, int32_t errCode);
247 } ISocketListener;
248
249 typedef enum {
250 QOS_TYPE_MIN_BW, // Minimum bandwidth.
251 QOS_TYPE_MAX_WAIT_TIMEOUT, // Bind timeout.
252 QOS_TYPE_MIN_LATENCY, // Minimum connection latency.
253 QOS_TYPE_RTT_LEVEL, // Round-trip time (RTT) level.
254 QOS_TYPE_MAX_BUFFER, // Maximum buffer (reserved).
255 QOS_TYPE_FIRST_PACKAGE, // Size of the first packet (reserved).
256 QOS_TYPE_MAX_IDLE_TIMEOUT, // Maximum idle time.
257 QOS_TYPE_TRANS_RELIABILITY, // Transmission reliability (reserved).
258 QOS_TYPE_BUTT,
259 } QosType;
260
261 typedef struct {
262 QosType qos;
263 int32_t value;
264 } QosTV;
265
266 // Start listening for the socket on the server.
267 int32_t Listen(int32_t socket, const QosTV qos[], uint32_t qosCount, const ISocketListener *listener);
268
269 // Bind the socket on the client.
270 int32_t Bind(int32_t socket, const QosTV qos[], uint32_t qosCount, const ISocketListener *listener);
271 ```
272
2734. Send data to the peer device through the socket.
274
275 ```C
276 // Send bytes.
277 int32_t SendBytes(int32_t socket, const void *data, uint32_t len);
278 // Async Send bytes. dataSeq indicates the unique seq number of the packet to be sent, which cannot be zero.
279 int32_t SendBytesAsync(int32_t socket, uint32_t dataSeq, const void *data, uint32_t len);
280 // Send messages.
281 int32_t SendMessage(int32_t socket, const void *data, uint32_t len);
282 // Send a stream.
283 int32_t SendStream(int32_t socket, const StreamData *data, const StreamData *ext, const StreamFrameInfo *param);
284 // Send a file.
285 int32_t SendFile(int32_t socket, const char *sFileList[], const char *dFileList[], uint32_t fileCnt);
286 ```
287
2884. Shut down a socket.
289
290 ```C
291 // Shut down a socket.
292 void Shutdown(int32_t socket);
293 ```
294
295### Device Management
296
297
298- Select Wi-Fi keepalive mode.
299
300Call **ShiftLNNGear** on the DSoftBus client to invoke the server **ShiftLNNGear** through an IPC interface. The policy management module adjusts the **keepalive** property of the TCP persistent connection based on the specified policy.
301
302```C
303typedef struct {
304 ModeCycle cycle; // Keepalive probe interval.
305 ModeDuration duration; // Heartbeat mode duration.
306 bool wakeupFlag; // Whether the heartbeat signal wakes up the peer device.
307 ModeAction action; // Action to be taken during the keepalive process.
308} GearMode;
309
310typedef enum {
311 HIGH_FREQ_CYCLE = 30, // Heartbeat interval 30s.
312 MID_FREQ_CYCLE = 60, // Heartbeat interval 60s.
313 LOW_FREQ_CYCLE = 5 * 60, // Heartbeat interval 5 minutes.
314 DEFAULT_FREQ_CYCLE = 10 * 60, // Heartbeat interval 10 minutes.
315} ModeCycle;
316
317// Adjust the keepalive parameters of a TCP persistent connection based on the specified gear mode.
318int32_t ShiftLNNGear(const char *pkgName, const char *callerId, const char *targetNetworkId, const GearMode *mode);
319```
320
321The TCP keepalive duration varies, depending on **ModeCycle**.
322
323```C
324HIGH_FREQ_CYCLE = 30, // The TCP keepalive duration is within 40s.
325MID_FREQ_CYCLE = 60, // The TCP keepalive duration is within 70s.
326LOW_FREQ_CYCLE = 5 x 60, // The TCP keepalive duration is within 315s.
327DEFAULT_FREQ_CYCLE = 10 x 60, // The TCP keepalive duration is within 615s.
328```
329
330## Repositories Involved
331
332[DSoftBus](https://gitee.com/openharmony/docs/blob/master/en/readme/dsoftbus.md)
333
334**communication_dsoftbus**
335
336[communication_bluetooth](https://gitee.com/openharmony/communication_bluetooth)
337
338[communication_ipc](https://gitee.com/openharmony/communication_ipc)
339
340[communication_wifi](https://gitee.com/openharmony/communication_wifi)
341
README_zh.md
1# 分布式软总线组件<a name="ZH-CN_TOPIC_0000001103650648"></a>
2
3- [分布式软总线组件<a name="ZH-CN_TOPIC_0000001103650648"></a>](#分布式软总线组件)
4 - [简介<a name="section13587125816351"></a>](#简介)
5 - [系统架构<a name="section13587185873516"></a>](#系统架构)
6 - [目录<a name="section161941989596"></a>](#目录)
7 - [约束<a name="section119744591305"></a>](#约束)
8 - [说明<a name="section1312121216216"></a>](#说明)
9 - [使用说明<a name="section1698318421816"></a>](#使用说明)
10 - [相关仓<a name="section1371113476307"></a>](#相关仓)
11
12## 简介<a name="section13587125816351"></a>
13
14现实中多设备间通信方式多种多样\(WIFI、蓝牙等\),不同的通信方式使用差异大,导致通信问题多;同时还面临设备间通信链路的融合共享和冲突无法处理等挑战。分布式软总线实现近场设备间统一的分布式通信管理能力,提供不区分链路的设备间发现连接、组网和传输能力,主要功能如下:
15
16- 发现连接:提供基于Wifi、蓝牙等通信方式的设备发现连接能力。
17- 设备组网:提供统一的设备组网和拓扑管理能力,为数据传输提供已组网设备信息。
18- 数据传输:提供数据传输通道,支持消息、字节、流、文件的数据传输能力。
19
20业务方通过使用分布式软总线提供的API实现设备间的高速通信,不用关心通信细节,进而实现业务平台的高效部署与运行能力。
21
22## 系统架构<a name="section13587185873516"></a>
23
24
25**图 1** 分布式软总线组件架构图<a name="fig4460722185514"></a>
26
27## 目录<a name="section161941989596"></a>
28
29分布式软总线组件主要代码目录结构如下:
30
31```text
32//foundation/communication/dsoftbus
33├── adapter # 适配层代码
34├── components # 依赖组件代码
35├── core # 核心代码
36│ ├── adapter # 适配层代码
37│ ├── authentication # 认证代码
38│ ├── bus_center # 组网代码
39│ ├── common # 通用代码
40│ ├── connection # 连接代码
41│ ├── discovery # 发现代码
42│ ├── frame # 框架代码
43│ └── transmission # 传输代码
44├── interfaces # 对外接口代码
45├── sdk # 运行业务进程代码
46│ ├── bus_center # 组网代码
47│ ├── discovery # 发现代码
48│ ├── frame # 框架代码
49│ └── transmission # 传输代码
50├── tests # 测试代码
51└── tools # 工具代码
52```
53
54## 约束<a name="section119744591305"></a>
55
56- 组网设备需在同一局域网中 或者 距离相近的近场设备间。
57- 组网之前,需先完成设备绑定,绑定流程参见[安全基础能力子系统](https://gitee.com/openharmony/docs/blob/master/zh-cn/readme/%E5%AE%89%E5%85%A8%E5%9F%BA%E7%A1%80%E8%83%BD%E5%8A%9B%E5%AD%90%E7%B3%BB%E7%BB%9F.md)中说明。
58- 传输完成数据收发之后,业务要主动关闭会话,释放资源。
59
60## 说明<a name="section1312121216216"></a>
61
62### 使用说明<a name="section1698318421816"></a>
63
64>**须知:**
65>使用跨设备通信时,必须添加权限`ohos.permission.DISTRIBUTED_DATASYNC`和`ohos.permission.DISTRIBUTED_SOFTBUS_CENTER`,该权限类型为 _**dangerous**_ 。
66
67**1、发现**
68
69- **发布流程**
70
711. 上层应用需要对外发布自身能力时,调用服务发布接口发布自身能力。
72
73 ```C
74 // 发布回调
75 typedef struct {
76 /** Callback for publish result */
77 void (*OnPublishResult)(int publishId, PublishResult reason);
78 } IPublishCb;
79
80 // 发布信息
81 typedef struct {
82 int publishId; // 发布消息Id
83 DiscoverMode mode; // 发布模式
84 ExchangeMedium medium; // 发布媒介
85 ExchangeFreq freq; // 发布频率
86 const char *capability; // 被发现设备需要具备的能力
87 unsigned char *capabilityData; // 业务发布的自定义数据
88 unsigned int dataLen; // 数据长度
89 bool ranging; // 是否测距
90 } PublishInfo;
91
92 // 发布服务
93 int32_t PublishLNN(const char *pkgName, const PublishInfo *info, const IPublishCb *cb);
94 ```
95
962. 上层应用不再需要对外发布自身能力时,调用StopPublishLNN接口注销服务。
97
98 ```C
99 // 注销服务
100 int32_t StopPublishLNN(const char *pkgName, int32_t publishId);
101 ```
102
103
104- **发现流程**
105
1061. 上层应用需要发现特定能力设备时,调用发现接口启动发现。
107
108 ```C
109 // 发现回调
110 typedef struct {
111 /** Callback that is invoked when a device is found */
112 void (*OnDeviceFound)(const DeviceInfo *device);
113 /** Callback for a subscription result */
114 void (*OnDiscoverResult)(int32_t refreshId, RefreshResult reason);
115 } IRefreshCallback;
116
117 // 发现服务
118 int32_t RefreshLNN(const char *pkgName, const SubscribeInfo *info, const IRefreshCallback *cb);
119 ```
120
1212. 当软总线发现到设备时,通过回调接口通知业务所发现的设备信息。
1223. 上层应用不再需要发现时,调用StopRefreshLNN接口停止设备发现。
123
124 ```C
125 // 停止发现
126 int32_t StopRefreshLNN(const char *pkgName, int32_t refreshId);
127 ```
128
129**2、组网**
130
1311. 发起组网请求,携带组网连接地址信息,并且提供组网执行结果回调函数。
132
133 ```C
134 // 组网连接地址
135 typedef struct {
136 ConnectionAddrType type;
137 union {
138 struct BrAddr {
139 char brMac[BT_MAC_LEN];
140 } br;
141 struct BleAddr {
142 char bleMac[BT_MAC_LEN];
143 uint8_t udidHash[UDID_HASH_LEN];
144 } ble;
145 struct IpAddr {
146 char ip[IP_STR_MAX_LEN];
147 uint16_t port;
148 } ip;
149 } info;
150 char peerUid[MAX_ACCOUNT_HASH_LEN];
151 } ConnectionAddr;
152
153 // 组网连接地址类型
154 typedef enum {
155 CONNECTION_ADDR_WLAN = 0,
156 CONNECTION_ADDR_BR,
157 CONNECTION_ADDR_BLE,
158 CONNECTION_ADDR_ETH,
159 CONNECTION_ADDR_MAX
160 } ConnectionAddrType;
161
162 // 组网请求执行结果回调
163 typedef void (*OnJoinLNNResult)(ConnectionAddr *addr, const char *networkId, int32_t retCode);
164
165 // 发起组网请求
166 int32_t JoinLNN(const char *pkgName, ConnectionAddr *target, OnJoinLNNResult cb);
167 ```
168
1692. 等待组网结果,JoinLNN\(\)返回成功表示软总线接受了组网请求,组网结果通过回调函数通知业务;组网回调函数中addr参数内容和JoinLNN\(\)的入参互相匹配;retCode如果为0,表示组网成功,此时networkId为有效值,后续传输、退网等接口均需使用该参数;retCode如果不为0,表示组网失败,此时networkId为无效值。
1703. 使用传输相关接口进行数据传输。
1714. 发送退网请求,携带组网成功后返回的networkId,并且提供退网执行结果回调。
172
173 ```C
174 // 退网执行结果回调
175 typedef void (*OnLeaveLNNResult)(const char *networkId, int32_t retCode);
176
177 // 退网请求
178 int32_t LeaveLNN(const char *pkgName, const char *networkId, OnLeaveLNNResult cb);
179 ```
180
1815. 等待退网完成,OnLeaveLNNResult\(\)的networkId和退网请求接口中的networkId互相匹配;retCode为0表示退网成功,否则退网失败。退网成功后,networkId变为无效值,后续不应该被继续使用。
1826. 使用节点(即设备)注册和注销接口,监听网络中节点状态变化等事件。
183
184 ```C
185 // 事件掩码
186 #define EVENT_NODE_STATE_ONLINE 0x1
187 #define EVENT_NODE_STATE_OFFLINE 0x02
188 #define EVENT_NODE_STATE_INFO_CHANGED 0x04
189 #define EVENT_NODE_STATUS_CHANGED 0x08
190 #define EVENT_NODE_STATE_MASK 0xF
191
192 // 节点信息
193 typedef struct {
194 char networkId[NETWORK_ID_BUF_LEN];
195 char deviceName[DEVICE_NAME_BUF_LEN];
196 uint16_t deviceTypeId;
197 } NodeBasicInfo;
198
199 // 节点状态事件回调
200 typedef struct {
201 uint32_t events; // 组网事件掩码
202 void (*onNodeOnline)(NodeBasicInfo *info); // 节点上线事件回调
203 void (*onNodeOffline)(NodeBasicInfo *info); // 节点下线事件回调
204 void (*onNodeBasicInfoChanged)(NodeBasicInfoType type, NodeBasicInfo *info); // 节点信息变化事件回调
205 void (*onNodeStatusChanged)(NodeStatusType type, NodeStatus *status); // 设备运行状态变化事件回调
206 } INodeStateCb;
207
208 // 注册节点状态事件回调
209 int32_t RegNodeDeviceStateCb(const char *pkgName, INodeStateCb *callback);
210
211 // 注销节点状态事件回调
212 int32_t UnregNodeDeviceStateCb(INodeStateCb *callback);
213 ```
214
215**3、传输**
216
2171. 创建Socket。
218
219 ```C
220 typedef struct {
221 char *name; // 本端Socket名称
222 char *peerName; // 对端Socket名称
223 char *peerNetworkId; // 对端Socket的网络ID
224 char *pkgName; // 调用者包名
225 TransDataType dataType; // 传输的数据类型,需要与发送方法一致
226 } SocketInfo;
227
228 // 创建Socket
229 int32_t Socket(SocketInfo info);
230 ```
231
2322. 服务端启动监听,客户端进行绑定。
233
234 ```C
235 // Socket回调函数
236 typedef struct {
237 void (*OnBind)(int32_t socket, PeerSocketInfo info);
238 void (*OnShutdown)(int32_t socket, ShutdownReason reason);
239 void (*OnBytes)(int32_t socket, const void *data, uint32_t dataLen);
240 void (*OnMessage)(int32_t socket, const void *data, uint32_t dataLen);
241 void (*OnStream)(int32_t socket, const StreamData *data, const StreamData *ext, const StreamFrameInfo *param);
242 void (*OnFile)(int32_t socket, FileEvent *event);
243 void (*OnQos)(int32_t socket, QoSEvent eventId, const QosTV *qos, uint32_t qosCount);
244 void (*OnError)(int32_t socket, int32_t errCode);
245 void (*OnBytesSent)(int32_t socket, uint32_t dataSeq, int32_t errCode);
246 } ISocketListener;
247
248 typedef enum {
249 QOS_TYPE_MIN_BW, // 最小带宽
250 QOS_TYPE_MAX_WAIT_TIMEOUT, // Bind超时时间
251 QOS_TYPE_MIN_LATENCY, // 最小建链时延
252 QOS_TYPE_RTT_LEVEL, // 往返时间级别
253 QOS_TYPE_MAX_BUFFER, // 最大缓存(预留)
254 QOS_TYPE_FIRST_PACKAGE, // 首包大小(预留)
255 QOS_TYPE_MAX_IDLE_TIMEOUT, // 最大空闲时间
256 QOS_TYPE_TRANS_RELIABILITY, // 传输可靠性(预留)
257 QOS_TYPE_BUTT,
258 } QosType;
259
260 typedef struct {
261 QosType qos;
262 int32_t value;
263 } QosTV;
264
265 // 监听Socket,由服务端开启。
266 int32_t Listen(int32_t socket, const QosTV qos[], uint32_t qosCount, const ISocketListener *listener);
267
268 // 绑定Socket,由客户端开启。
269 int32_t Bind(int32_t socket, const QosTV qos[], uint32_t qosCount, const ISocketListener *listener);
270 ```
271
2724. 通过Socket向对端设备发送数据。
273
274 ```C
275 // 发送字节数据
276 int32_t SendBytes(int32_t socket, const void *data, uint32_t len);
277 // 异步发送字节数据, dataSeq是无符号的不为0值的整数
278 int32_t SendBytesAsync(int32_t socket, uint32_t dataSeq, const void *data, uint32_t len);
279 // 发送消息数据
280 int32_t SendMessage(int32_t socket, const void *data, uint32_t len);
281 // 发送流数据
282 int32_t SendStream(int32_t socket, const StreamData *data, const StreamData *ext, const StreamFrameInfo *param);
283 // 发送文件
284 int32_t SendFile(int32_t socket, const char *sFileList[], const char *dFileList[], uint32_t fileCnt);
285 ```
286
2875. 关闭Socket。
288
289 ```C
290 // 关闭Socket
291 void Shutdown(int32_t socket);
292 ```
293
294**4、设备管理相关**
295
296- **选择Wi-Fi保活模式**
297
2981. 业务在软总线客户端调用ShiftLNNGear,通过IPC接口调用到服务端ShiftLNNGear,策略管理模块按照策略对TCP长连接的keepalive属性进行调整。
299
300 ```C
301 typedef struct {
302 ModeCycle cycle; // 保活探测间隔
303 ModeDuration duration; // 心跳模式持续时间
304 bool wakeupFlag; // 是否心跳唤醒对端设备
305 ModeAction action; // 选择模式动作
306 } GearMode;
307
308 typedef enum {
309 HIGH_FREQ_CYCLE = 30, // 心跳间隔30s
310 MID_FREQ_CYCLE = 60, // 心跳间隔60s
311 LOW_FREQ_CYCLE = 5 * 60, // 心跳间隔5min
312 DEFAULT_FREQ_CYCLE = 10 * 60, // 心跳间隔10min
313 } ModeCycle;
314
315 // 按照策略对TCP长连接的keepalive参数进行调整
316 int32_t ShiftLNNGear(const char *pkgName, const char *callerId, const char *targetNetworkId, const GearMode *mode);
317 ```
318
3192. 业务指定不同的保活探测间隔,对应不同的TCP保活时长。
320
321 ```C
322 HIGH_FREQ_CYCLE = 30,代表TCP保活时长在40s以内;
323 MID_FREQ_CYCLE = 60,代表TCP保活时长在70s以内;
324 LOW_FREQ_CYCLE = 5*60,代表TCP保活时长在315s以内;
325 DEFAULT_FREQ_CYCLE = 10*60,代表TCP保活时长在615s以内。
326 ```
327
328## 相关仓<a name="section1371113476307"></a>
329
330[分布式软总线子系统](https://gitee.com/openharmony/docs/blob/master/zh-cn/readme/%E5%88%86%E5%B8%83%E5%BC%8F%E8%BD%AF%E6%80%BB%E7%BA%BF%E5%AD%90%E7%B3%BB%E7%BB%9F.md)
331
332**communication_dsoftbus**
333
334[communication_bluetooth](https://gitee.com/openharmony/communication_bluetooth)
335
336[communication_ipc](https://gitee.com/openharmony/communication_ipc)
337
338[communication_wifi](https://gitee.com/openharmony/communication_wifi)
339