1 /*
2 * Copyright (C) 2023 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 //! rust dsoftbus binding sys
17
18 #![allow(dead_code)]
19 #![allow(missing_docs)]
20
21 use std::ffi::{ c_void, c_char };
22
23 ///Constant for dsoftbus
24 pub const INTERCEPT_STRING_LENGTH: usize = 20;
25 pub const DINPUT_LINK_TYPE_MAX: i32 = 4;
26 pub const DEVICE_ID_SIZE_MAX: usize = 65;
27 pub const SESSION_SIDE_SERVER: i32 = 0;
28 pub const LINK_TYPE_WIFI_WLAN_5G: i32 = 1;
29 pub const LINK_TYPE_WIFI_WLAN_2G: i32 = 2;
30 pub const LINK_TYPE_WIFI_P2P: i32 = 3;
31 pub const LINK_TYPE_BR: i32 = 4;
32 pub const LINK_TYPE_MAX: usize = 9;
33 pub const TYPE_MESSAGE: i32 = 1;
34 pub const TYPE_BYTES: i32 = 2;
35 pub const TYPE_FILE: i32 = 3;
36 pub const TYPE_STREAM: i32 = 4;
37 pub const TYPE_BUTT: i32 = 5;
38 pub const NETWORK_ID_BUF_LEN: usize = 65;
39 pub const DEVICE_NAME_BUF_LEN: usize = 128;
40 pub const RET_OK: i32 = 0;
41 pub const RET_ERROR: i32 = -1;
42 pub const C_CHAR_SIZE: usize = std::mem::size_of::<c_char>();
43
44 /// NodeBasicInfo is a structure that represents basic information about a node.
45 #[repr(C)]
46 pub struct NodeBasicInfo {
47 /// The network ID of the device.
48 pub network_id: [i8; NETWORK_ID_BUF_LEN],
49 /// The device name of the device.
50 pub device_name: [i8; DEVICE_NAME_BUF_LEN],
51 /// The device type ID of the device.
52 pub device_type_id: u16,
53 }
54
55 /// SessionAttribute is a structure that represents session attributes.
56 #[repr(C)]
57 pub struct SessionAttribute {
58 /// The data type of the session attribute.
59 pub data_type: i32,
60 /// The number of link types in the session attribute.
61 pub link_type_num: i32,
62 /// The array of link types in the session attribute.
63 pub link_type: [i32; LINK_TYPE_MAX],
64 /// The stream attribute of the session.
65 pub stream_attr: i32,
66 /// The pointer to the fast transmission data.
67 pub fast_trans_data: *const u8,
68 /// The size of the fast transmission data.
69 pub fast_trans_data_size: u16,
70 }
71
72 /// StreamData is a structure that represents stream data.
73 #[repr(C)]
74 pub struct StreamData {
75 /// The content of the buffer.
76 pub buf_data: c_char,
77 /// The length of the buffer.
78 pub buf_len: i32,
79 }
80
81 /// StreamFrameInfo is a structure that contains information about a stream frame.
82 #[repr(C)]
83 pub struct StreamFrameInfo {
84 /// The type of the frame.
85 pub frame_type: i32,
86 /// The time stamp of the frame.
87 pub time_stamp: i64,
88 /// The sequence number of the frame.
89 pub seq_num: i32,
90 /// The sub-sequence number of the frame.
91 pub seq_sub_num: i32,
92 /// The level number of the frame.
93 pub level_num: i32,
94 /// The bit map of the frame.
95 pub bit_map: i32,
96 /// The number of TV (Television) in the frame.
97 pub tv_count: i32,
98 /// The list of TV (Television) in the frame.
99 pub tv_list: i32,
100 }
101
102 // Callback function type for OnSessionOpened() from native, this callback will be called after listening
103 // that a session has been opened.
104 pub type OnSessionOpened = extern "C" fn (session_id: i32, resultValue: i32) -> i32;
105 // Callback function type for OnSessionClosed() from native, this callback will be called after listening
106 // that a session has been opened.
107 pub type OnSessionClosed = extern "C" fn (session_id: i32);
108 // Callback function type for OnBytesReceived() from native, this callback will be called after listening
109 // to receive some byte messages.
110 pub type OnBytesReceived = extern "C" fn (session_id: i32, byteData: *const c_void, data_len: u32);
111 // Callback function type for OnMessageReceived() from native, this callback will be called after listening
112 // to receive some string messages.
113 pub type OnMessageReceived = extern "C" fn (session_id: i32, byteData: *const c_void, data_len: u32);
114 // Callback function type for OnstreamReceived() from native, this callback will be called after listening
115 // to receive some stream messages.
116 pub type OnstreamReceived = extern "C" fn (session_id: i32, byteData: *const StreamData,
117 extData: *const StreamData, paramData: *const StreamFrameInfo);
118
119 /// ISessionListener is a structure that defines the callbacks for session events.
120 #[repr(C)]
121 pub struct ISessionListener {
122 /// The callback function is used to do something after listening that a session has been opened.
123 pub on_session_opened: OnSessionOpened,
124 /// The callback function is used to do something after listening that a session has been closed.
125 pub on_session_closed: OnSessionClosed,
126 /// The callback function is used to do something when listening to receive some byte messages.
127 pub on_bytes_received: OnBytesReceived,
128 /// The callback function is used to do something when listening to receive some string messages.
129 pub on_message_received: OnMessageReceived,
130 /// The callback function is used to do something when listening to receive some stream messages.
131 pub on_stream_received: OnstreamReceived,
132 }
133
134 /// Provide for C lib to call
on_session_opened_c(_session_id: i32, _result: i32) -> i32135 extern "C" fn on_session_opened_c(_session_id: i32, _result: i32) -> i32 {
136 0
137 }
138
139 /// Provide for C lib to call
on_session_closed_c(_session_id: i32)140 extern "C" fn on_session_closed_c(_session_id: i32) {
141 }
142
143 /// Provide for C lib to call
on_bytes_received_c(_session_id: i32, _data: *const c_void, _data_len: u32)144 extern "C" fn on_bytes_received_c(_session_id: i32, _data: *const c_void, _data_len: u32) {
145 }
146
147 /// Provide for C lib to call
on_message_received_c(_session_id: i32, _byte_data: *const c_void, _data_len: u32)148 extern "C" fn on_message_received_c(_session_id: i32, _byte_data: *const c_void, _data_len: u32) {
149 }
150
151 /// Provide for C lib to call
on_stream_received_c(_session_id: i32, _byte_data: *const StreamData, _ext_data: *const StreamData, _param_data: *const StreamFrameInfo)152 extern "C" fn on_stream_received_c(_session_id: i32, _byte_data: *const StreamData,
153 _ext_data: *const StreamData, _param_data: *const StreamFrameInfo) {
154 }
155
156 impl Default for ISessionListener {
default() -> Self157 fn default() -> Self {
158 ISessionListener {
159 on_session_opened: on_session_opened_c,
160 on_session_closed: on_session_closed_c,
161 on_bytes_received: on_bytes_received_c,
162 on_message_received: on_message_received_c,
163 on_stream_received: on_stream_received_c,
164 }
165 }
166 }
167
168 // These C interfaces are defined in lib: dsoftbus:softbus_client
169 extern "C" {
170 /// Creates a session server.
171 ///
172 /// # Arguments
173 ///
174 /// * `pkg_name` - The package name of the server.
175 /// * `session_name` - The name of the session.
176 /// * `session_listener` - A pointer to the session listener.
177 ///
178 /// # Returns
179 ///
180 /// The session ID if successful, otherwise an error code.
CreateSessionServer(pkg_name: *const c_char, session_name: *const c_char, session_listener: *const ISessionListener) -> i32181 pub fn CreateSessionServer(pkg_name: *const c_char, session_name: *const c_char,
182 session_listener: *const ISessionListener) -> i32;
183
184 /// Removes a session server.
185 ///
186 /// # Arguments
187 ///
188 /// * `pkg_name` - The package name of the server.
189 /// * `session_name` - The name of the session.
190 ///
191 /// # Returns
192 ///
193 /// An error code indicating the result of the operation.
RemoveSessionServer(pkg_name: *const c_char, session_name: *const c_char) -> i32194 pub fn RemoveSessionServer(pkg_name: *const c_char, session_name: *const c_char) -> i32;
195
196 /// Closes a session.
197 ///
198 /// # Arguments
199 ///
200 /// * `session_id` - The ID of the session to be closed.
CloseSession(session_id: i32)201 pub fn CloseSession(session_id: i32);
202
203 /// Opens a session.
204 ///
205 /// # Arguments
206 ///
207 /// * `my_session_name` - The name of the local session.
208 /// * `peer_session_name` - The name of the remote session.
209 /// * `peer_device_id` - The ID of the remote device.
210 /// * `groupId` - The group ID of the session.
211 /// * `attr` - A pointer to the session attribute.
212 ///
213 /// # Returns
214 ///
215 /// The session ID if successful, otherwise an error code.
OpenSession(my_session_name: *const c_char, peer_session_name: *const c_char, peer_device_id: *const c_char, groupId: *const c_char, attr: *const SessionAttribute) -> i32216 pub fn OpenSession(my_session_name: *const c_char, peer_session_name: *const c_char, peer_device_id: *const c_char,
217 groupId: *const c_char, attr: *const SessionAttribute) -> i32;
218
219 /// Gets the ID of the remote device for a given session.
220 ///
221 /// # Arguments
222 ///
223 /// * `session_id` - The ID of the session.
224 /// * `peer_dev_id` - A mutable buffer to store the remote device ID.
225 /// * `len` - The length of the buffer.
226 ///
227 /// # Returns
228 ///
229 /// An error code indicating the result of the operation.
GetPeerDeviceId(session_id: i32, peer_dev_id: *mut c_char, len: u32) -> i32230 pub fn GetPeerDeviceId(session_id: i32, peer_dev_id: *mut c_char, len: u32) -> i32;
231
232 /// Gets the session side (server or client) for a given session.
233 ///
234 /// # Arguments
235 ///
236 /// * `session_id` - The ID of the session.
237 ///
238 /// # Returns
239 ///
240 /// The session side if successful, otherwise an error code.
GetSessionSide(session_id: i32) -> i32241 pub fn GetSessionSide(session_id: i32) -> i32;
242
243 /// Gets the local node's device information.
244 ///
245 /// # Arguments
246 ///
247 /// * `pkg_name` - The package name of the local node.
248 /// * `info` - A mutable pointer to the node's basic information structure.
249 ///
250 /// # Returns
251 ///
252 /// An error code indicating the result of the operation.
GetLocalNodeDeviceInfo(pkg_name: *const c_char, info: *mut NodeBasicInfo) -> i32253 pub fn GetLocalNodeDeviceInfo(pkg_name: *const c_char, info: *mut NodeBasicInfo) -> i32;
254
255 /// Sends bytes over a session.
256 ///
257 /// # Arguments
258 ///
259 /// * `session_id` - The ID of the session.
260 /// * `data` - A pointer to the data to be sent.
261 /// * `len` - The length of the data.
262 ///
263 /// # Returns
264 ///
265 /// An error code indicating the result of the operation.
SendBytes(session_id: i32, data: *const c_void, len: u32) -> i32266 pub fn SendBytes(session_id: i32, data: *const c_void, len: u32) -> i32;
267 }