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