|
Name |
|
Date |
Size |
#Lines |
LOC |
| .. | | 17-Mar-2025 | - |
| adapter/ | H | 17-Mar-2025 | - | 17,239 | 13,315 |
| components/ | H | 17-Mar-2025 | - | 68,696 | 51,472 |
| core/ | H | 17-Mar-2025 | - | 171,145 | 143,459 |
| figures/ | H | 17-Mar-2025 | - | | |
| interfaces/ | H | 17-Mar-2025 | - | 4,706 | 1,916 |
| sdk/ | H | 17-Mar-2025 | - | 34,057 | 28,063 |
| tests/ | H | 17-Mar-2025 | - | 200,228 | 141,464 |
| tools/device_info/ | H | 17-Mar-2025 | - | 119 | 98 |
| .clang-format | H A D | 17-Mar-2025 | 1.2 KiB | 40 | 37 |
| .gitattributes | H A D | 17-Mar-2025 | 631 | 16 | 15 |
| .gitignore | H A D | 17-Mar-2025 | 47 | 3 | 2 |
| BUILD.gn | H A D | 17-Mar-2025 | 743 | 23 | 20 |
| CODEOWNERS | H A D | 17-Mar-2025 | 5.3 KiB | 130 | 111 |
| Kconfig | H A D | 17-Mar-2025 | 2.2 KiB | 81 | 69 |
| LICENSE | H A D | 17-Mar-2025 | 9.9 KiB | 177 | 150 |
| OAT.xml | H A D | 17-Mar-2025 | 4.9 KiB | 75 | 22 |
| README.md | H A D | 17-Mar-2025 | 13.1 KiB | 334 | 259 |
| README_zh.md | H A D | 17-Mar-2025 | 13.4 KiB | 336 | 262 |
| bundle.json | H A D | 17-Mar-2025 | 3.5 KiB | 132 | 131 |
| check_sub_module.py | H A D | 17-Mar-2025 | 987 | 34 | 14 |
| config.py | H A D | 17-Mar-2025 | 2.4 KiB | 84 | 55 |
| dsoftbus.gni | H A D | 17-Mar-2025 | 3 KiB | 100 | 87 |
| hisysevent.yaml | H A D | 17-Mar-2025 | 31.8 KiB | 615 | 573 |
README.md
1# DSoftBus
2
3
4## Introduction
5
6DSoftBus implements unified distributed communications between near-field devices and provides APIs for device discovery, connection, networking, and data transmission, regardless of the link type. It provides the following capabilities:
7
8- Device discovery and connection in various communication modes, such as WLAN and Bluetooth.
9- Unified device networking and topology management, and device information provisioning for data transmission.
10- Channel setup for transmitting messages, bytes, streams, and files.
11
12You can use the APIs provided by DSoftBus to implement fast communication between devices without caring about the communication details, which facilitating deployment and running of services across platforms.
13
14## Architecture
15
16
17
18**Figure 1** DSoftBus architecture
19
20## Directory Structure
21
22The DSoftBus directory structure is as follows:
23
24```text
25//foundation/communication/dsoftbus
26├── adapter # Adaptation code
27├── components # Dependent component code
28├── core # Core code
29│ ├── adapter # Adaptation code
30│ ├── authentication # Authentication code
31│ ├── bus_center # Networking code
32│ ├── common # Common code
33│ ├── connection # Connection code
34│ ├── discovery # Discovery code
35│ ├── frame # Framework code
36│ └── transmission # Transmission code
37├── interfaces # External APIs
38├── sdk # Service process code
39│ ├── bus_center # Networking code
40│ ├── discovery # Discovery code
41│ ├── frame # Framework code
42│ └── transmission # Transmission code
43├── tests # Test code
44└── tools # Tool code
45```
46
47## Constraints
48
49- Connections can be set up only between the devices in the same LAN or between near-field devices.
50- Before setting up a connection between two devices, you must bind the devices. For details about the binding process, see [the Security subsystem](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) readme file.
51- After data transmission is complete, the service needs to close the session to release resources.
52
53## Usage
54
55### Usage Guidelines
56
57>**NOTE**
58>
59>- The permissions ohos.permission.DISTRIBUTED_DATASYNC and ohos.permission.DISTRIBUTED_SOFTBUS_CENTER are required for remote procedure calls (RPCs) across devices.
60
61**1. Discovery**
62
63- **Publishing process**
64
651. Publish a service of your application.
66
67 ```C
68 // Callback for service publishing.
69 typedef struct {
70 /** Callback used to return the publish result. */
71 void (*OnPublishResult)(int publishId, PublishResult reason);
72 } IPublishCb;
73
74 // Publish information.
75 typedef struct {
76 int publishId; // Publish ID.
77 DiscoverMode mode; // Publish mode.
78 ExchangeMedium medium; // Medium used for publishing the service.
79 ExchangeFreq freq; // Service publishing frequency.
80 const char *capability; // Capability of the device that can be discovered.
81 unsigned char *capabilityData; // Custom data for service publishing
82 unsigned int dataLen; // Length of the data.
83 bool ranging; // Whether to measure the distance.
84 } PublishInfo;
85
86 // Publish a service.
87 int32_t PublishLNN(const char *pkgName, const PublishInfo *info, const IPublishCb *cb);
88 ```
89
902. Unpublish a service of your application.
91
92 ```C
93 // Unpublish a service.
94 int32_t StopPublishLNN(const char *pkgName, int32_t publishId);
95 ```
96
97
98- **Discovery process**
99
1001. Discover a device.
101
102 ```C
103 // Callbacks for device discovery.
104 typedef struct {
105 /** Callback invoked when a device is found. */
106 void (*OnDeviceFound)(const DeviceInfo *device);
107 /** Callback invoked to return the device discovery result. */
108 void (*OnDiscoverResult)(int32_t refreshId, RefreshResult reason);
109 } IRefreshCallback;
110
111 // Start device discovery.
112 int32_t RefreshLNN(const char *pkgName, const SubscribeInfo *info, const IRefreshCallback *cb);
113 ```
114
1152. DSoftBus notifies the service of the device information via the callback once a device is found.
1163. Stop device discovery.
117
118 ```C
119 // Stop the discovery.
120 int32_t StopRefreshLNN(const char *pkgName, int32_t refreshId);
121 ```
122
123**2. Networking**
124
1251. Initiate a connection request with the address of the target device and the connection callback.
126
127 ```C
128 // Address 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 invoked 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. If DSoftBus accepts the connection request, a callback is invoked to return the result. In the return value, if **retCode** is **0**, the connection is successful, and the **addr** parameter matches the **target** parameter in **JoinLNN()**. 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.
1643. Transmit data using transmission APIs.
1654. Initiate a disconnection request with the **networkId** and the callback.
166
167 ```C
168 // Callback invoked to return the disconnection result.
169 typedef void (*OnLeaveLNNResult)(const char *networkId, int32_t retCode);
170
171 // Initiate a disconnection request.
172 int32_t LeaveLNN(const char *pkgName, const char *networkId, OnLeaveLNNResult cb);
173 ```
174
1755. Wait until the disconnection is complete. The **networkId** parameter in **OnLeaveLNNResult()** matches **networkId** in **LeaveLNN()**. If **retCode** is **0**, the disconnection is successful; otherwise, the disconnection fails. If the disconnection is successful, **networkId** becomes invalid and can no longer be used.
1766. Register and unregister callbacks for device state changes.
177
178 ```C
179 // Device state events.
180 #define EVENT_NODE_STATE_ONLINE 0x1
181 #define EVENT_NODE_STATE_OFFLINE 0x02
182 #define EVENT_NODE_STATE_INFO_CHANGED 0x04
183 #define EVENT_NODE_STATUS_CHANGED 0x08
184 #define EVENT_NODE_STATE_MASK 0xF
185
186 // Device information.
187 typedef struct {
188 char networkId[NETWORK_ID_BUF_LEN];
189 char deviceName[DEVICE_NAME_BUF_LEN];
190 uint16_t deviceTypeId;
191 } NodeBasicInfo;
192
193 // Device state event callbacks.
194 typedef struct {
195 uint32_t events; // Networking event mask.
196 void (*onNodeOnline)(NodeBasicInfo *info); // Called when the device gets online.
197 void (*onNodeOffline)(NodeBasicInfo *info); // Called when the device gets offline.
198 void (*onNodeBasicInfoChanged)(NodeBasicInfoType type, NodeBasicInfo *info); // Called when the device information changes.
199 void (*onNodeStatusChanged)(NodeStatusType type, NodeStatus *status); // Called when the device running status changes.
200 } INodeStateCb;
201
202 // Register the callback for device state events.
203 int32_t RegNodeDeviceStateCb(const char *pkgName, INodeStateCb *callback);
204
205 // Unregister the callback for device state events.
206 int32_t UnregNodeDeviceStateCb(INodeStateCb *callback);
207 ```
208
209**3. Transmission**
210
2111. Create a **Socket** instance.
212
213 ```C
214 typedef struct {
215 char *name; // Local socket name.
216 char *peerName; // Peer socket name.
217 char *peerNetworkId; // Peer network ID.
218 char *pkgName; // Bundle name of the caller.
219 TransDataType dataType; // Type of the data to be transmitted, which must be the same as that in the sender() method.
220 } SocketInfo;
221
222 // Create sockets.
223 int32_t Socket(SocketInfo info);
224 ```
225
2262. Start listening for the socket on the server, and bind the socket on the client.
227
228 ```C
229 // Callbacks for the socket.
230 typedef struct {
231 void (*OnBind)(int32_t socket, PeerSocketInfo info);
232 void (*OnShutdown)(int32_t socket, ShutdownReason reason);
233 void (*OnBytes)(int32_t socket, const void *data, uint32_t dataLen);
234 void (*OnMessage)(int32_t socket, const void *data, uint32_t dataLen);
235 void (*OnStream)(int32_t socket, const StreamData *data, const StreamData *ext, const StreamFrameInfo *param);
236 void (*OnFile)(int32_t socket, FileEvent *event);
237 void (*OnQos)(int32_t socket, QoSEvent eventId, const QosTV *qos, uint32_t qosCount);
238 void (*OnError)(int32_t socket, int32_t errCode);
239 } ISocketListener;
240
241 typedef enum {
242 QOS_TYPE_MIN_BW, // Minimum bandwidth.
243 QOS_TYPE_MAX_WAIT_TIMEOUT, // Maximum time allowed for the bind operation.
244 QOS_TYPE_MIN_LATENCY, // Minimum latency for link setup.
245 QOS_TYPE_RTT_LEVEL, // Level of the RTT.
246 QOS_TYPE_MAX_BUFFER, // Maximum buffer size (reserved).
247 QOS_TYPE_FIRST_PACKAGE, // Size of the first packet (reserved).
248 QOS_TYPE_MAX_IDLE_TIMEOUT, // Maximum idle time.
249 QOS_TYPE_TRANS_RELIABILITY,// Transmission reliability (reserved).
250 QOS_TYPE_BUTT,
251 } QosType;
252
253 typedef struct {
254 QosType qos;
255 int32_t value;
256 } QosTV;
257
258 // Start listening for the socket on the server.
259 int32_t Listen(int32_t socket, const QosTV qos[], uint32_t qosCount, const ISocketListener *listener);
260
261 // Bind the socket on the client.
262 int32_t Bind(int32_t socket, const QosTV qos[], uint32_t qosCount, const ISocketListener *listener);
263 ```
264
2654. Send data to the peer device through the socket.
266
267 ```C
268 // Send bytes.
269 int32_t SendBytes(int32_t socket, const void *data, uint32_t len);
270 // Send messages.
271 int32_t SendMessage(int32_t socket, const void *data, uint32_t len);
272 // Send streams.
273 int32_t SendStream(int32_t socket, const StreamData *data, const StreamData *ext, const StreamFrameInfo *param);
274 // Send a file.
275 int32_t SendFile(int32_t socket, const char *sFileList[], const char *dFileList[], uint32_t fileCnt);
276 ```
277
2785. Shut down the socket.
279
280 ```C
281 // Shut down the socket.
282 void Shutdown(int32_t socket);
283 ```
284
285**4. Device Management**
286
287- **Choose the Wi-Fi keepalive mode.**
288
2891. Call **ShiftLNNGear** on the DSoftBus client to invoke the server **ShiftLNNGear** through an IPC interface. The policy management module adjusts the keepalive attributes of the long-lived TCP connection based on the policy.
290
291 ```C
292 typedef struct {
293 ModeCycle cycle; // Interval for detecting whether the Wi-Fi connection is alive.
294 ModeDuration duration; // Heartbeat mode duration.
295 bool wakeupFlag; // Whether to wake up the peer device.
296 ModeAction action; // Mode to select.
297 } GearMode;
298
299 typedef enum {
300 /**< The heartbeat interval is 30 seconds. */
301 HIGH_FREQ_CYCLE = 30,
302 /**< The heartbeat interval is 60 seconds. */
303 MID_FREQ_CYCLE = 60,
304 /**< The heartbeat interval is 5 minutes. */
305 LOW_FREQ_CYCLE = 5 * 60,
306 /**< The heartbeat interval is 10 minutes. */
307 DEFAULT_FREQ_CYCLE = 10 * 60,
308 } ModeCycle;
309
310 // Adjust the keepalive parameters of the long-lived TCP connection based on the policy.
311 int32_t ShiftLNNGear(const char *pkgName, const char *callerId, const char *targetNetworkId, const GearMode *mode);
312 ```
313
3142. Set **ModeCycle** for the service, which determines the TCP keepalive duration for the device.
315
316 ```C
317 If HIGH_FREQ_CYCLE is used, the TCP keepalive duration is within 40 seconds.
318 If MID_FREQ_CYCLE is used, the actual TCP keepalive duration is within 70 seconds.
319 If LOW_FREQ_CYCLE is used, the TCP keepalive duration is within 315 seconds.
320 If DEFAULT_FREQ_CYCLE is used, the TCP keepalive duration is within 615 seconds.
321 ```
322
323## Repositories Involved
324
325[DSoftBus](https://gitee.com/openharmony/docs/blob/master/en/readme/dsoftbus.md)
326
327**communication_dsoftbus**
328
329[communication_bluetooth](https://gitee.com/openharmony/communication_bluetooth)
330
331[communication_ipc](https://gitee.com/openharmony/communication_ipc)
332
333[communication_wifi](https://gitee.com/openharmony/communication_wifi)
334
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 } ISocketListener;
246
247 typedef enum {
248 QOS_TYPE_MIN_BW, // 最小带宽
249 QOS_TYPE_MAX_WAIT_TIMEOUT, // Bind超时时间
250 QOS_TYPE_MIN_LATENCY, // 最小建链时延
251 QOS_TYPE_RTT_LEVEL, // 往返时间级别
252 QOS_TYPE_MAX_BUFFER, // 最大缓存(预留)
253 QOS_TYPE_FIRST_PACKAGE, // 首包大小(预留)
254 QOS_TYPE_MAX_IDLE_TIMEOUT, // 最大空闲时间
255 QOS_TYPE_TRANS_RELIABILITY, // 传输可靠性(预留)
256 QOS_TYPE_BUTT,
257 } QosType;
258
259 typedef struct {
260 QosType qos;
261 int32_t value;
262 } QosTV;
263
264 // 监听Socket,由服务端开启。
265 int32_t Listen(int32_t socket, const QosTV qos[], uint32_t qosCount, const ISocketListener *listener);
266
267 // 绑定Socket,由客户端开启。
268 int32_t Bind(int32_t socket, const QosTV qos[], uint32_t qosCount, const ISocketListener *listener);
269 ```
270
2714. 通过Socket向对端设备发送数据。
272
273 ```C
274 // 发送字节数据
275 int32_t SendBytes(int32_t socket, const void *data, uint32_t len);
276 // 发送消息数据
277 int32_t SendMessage(int32_t socket, const void *data, uint32_t len);
278 // 发送流数据
279 int32_t SendStream(int32_t socket, const StreamData *data, const StreamData *ext, const StreamFrameInfo *param);
280 // 发送文件
281 int32_t SendFile(int32_t socket, const char *sFileList[], const char *dFileList[], uint32_t fileCnt);
282 ```
283
2845. 关闭Socket。
285
286 ```C
287 // 关闭Socket
288 void Shutdown(int32_t socket);
289 ```
290
291**4、设备管理相关**
292
293- **选择Wi-Fi保活模式**
294
2951. 业务在软总线客户端调用ShiftLNNGear,通过IPC接口调用到服务端ShiftLNNGear,策略管理模块按照策略对TCP长连接的keepalive属性进行调整。
296
297 ```C
298 typedef struct {
299 ModeCycle cycle; // 保活探测间隔
300 ModeDuration duration; // 心跳模式持续时间
301 bool wakeupFlag; // 是否心跳唤醒对端设备
302 ModeAction action; // 选择模式动作
303 } GearMode;
304
305 typedef enum {
306 HIGH_FREQ_CYCLE = 30, // 心跳间隔30s
307 MID_FREQ_CYCLE = 60, // 心跳间隔60s
308 LOW_FREQ_CYCLE = 5 * 60, // 心跳间隔5min
309 DEFAULT_FREQ_CYCLE = 10 * 60, // 心跳间隔10min
310 } ModeCycle;
311
312 // 按照策略对TCP长连接的keepalive参数进行调整
313 int32_t ShiftLNNGear(const char *pkgName, const char *callerId, const char *targetNetworkId, const GearMode *mode);
314 ```
315
3162. 业务指定不同的保活探测间隔,对应不同的TCP保活时长。
317
318 ```C
319 HIGH_FREQ_CYCLE = 30,代表TCP保活时长在40s以内;
320 MID_FREQ_CYCLE = 60,代表TCP保活时长在70s以内;
321 LOW_FREQ_CYCLE = 5*60,代表TCP保活时长在315s以内;
322 DEFAULT_FREQ_CYCLE = 10*60,代表TCP保活时长在615s以内。
323 ```
324
325## 相关仓<a name="section1371113476307"></a>
326
327[分布式软总线子系统](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)
328
329**communication_dsoftbus**
330
331[communication_bluetooth](https://gitee.com/openharmony/communication_bluetooth)
332
333[communication_ipc](https://gitee.com/openharmony/communication_ipc)
334
335[communication_wifi](https://gitee.com/openharmony/communication_wifi)
336