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 device profile binding sys
17 
18 #![allow(dead_code)]
19 
20 use std::ffi::{ c_char, c_int };
21 
22 /// Constant for device profile
23 pub const RET_OK: i32 = 0;
24 pub const EMPTY_LENGTH: usize = 0;
25 
26 /// C representation of [`ServiceCharacteristicProfile`].
27 #[repr(C)]
28 pub struct CServiceCharacteristicProfile {
29     // The ID of the service.
30     pub service_id: *const c_char,
31     // The type of the service.
32     pub service_type: *const c_char,
33     // The json description of the service.
34     pub service_profile_json: *const c_char,
35     // The characteristic profile of the service.
36     pub characteristic_profile_json: *const c_char,
37 }
38 
39 /// Type definition for the clone function of the `CIProfileEvents` struct.
40 pub type CIProfileEventsClone = extern "C" fn (profile_events: *mut CIProfileEvents) -> *mut CIProfileEvents;
41 /// Type definition for the destruct function of the `CIProfileEvents` struct.
42 pub type CIProfileEventsDestruct = extern "C" fn (profile_events: *mut CIProfileEvents);
43 
44 /// Represents a CIProfileEvents object that contains profile events information.
45 #[repr(C)]
46 pub struct CIProfileEvents {
47     /// An optional clone function pointer for creating a clone of the `CIProfileEvents` object.
48     pub clone: Option<CIProfileEventsClone>,
49     /// An optional destruct function pointer for cleaning up the resources associated with the `CIProfileEvents` object
50     pub destruct: Option<CIProfileEventsDestruct>,
51     /// A mutable pointer to an array of unsigned 32-bit integers representing the profile events.
52     pub profile_events: *mut u32,
53     /// The number of profile events in the `profile_events` array.
54     pub num_of_profile_events: usize,
55 }
56 
57 /// C representation of [`SubscribeInfo`].
58 #[repr(C)]
59 pub struct CSubscribeInfo {
60     /// The ID of the profile event to which the subscription applies.
61     pub profile_event: u32,
62     /// An optional pointer to extra information about the subscription.
63     pub extra_info: *const c_char,
64 }
65 
66 /// Type definition for the clone function of the `CISubscribeInfos` struct.
67 pub type CISubscribeInfosClone = extern "C" fn (profile_events: *mut CISubscribeInfos) -> *mut CISubscribeInfos;
68 /// Type definition for the destruct function of the `CISubscribeInfos` struct.
69 pub type CISubscribeInfosDestruct = extern "C" fn (profile_events: *mut CISubscribeInfos);
70 
71 /// Struct representing subscription information.
72 #[repr(C)]
73 pub struct CISubscribeInfos {
74     /// An optional clone function for creating a clone of the `CISubscribeInfos` object.
75     pub clone: Option<CISubscribeInfosClone>,
76     /// An optional destruct function for cleaning up the `CISubscribeInfos` object.
77     pub destruct: Option<CISubscribeInfosDestruct>,
78     /// A pointer to an array of `CSubscribeInfo` objects containing subscription details.
79     pub subscribe_infos: *const CSubscribeInfo,
80     /// The number of `CSubscribeInfo` objects in the `subscribe_infos` array.
81     pub num_of_subscribe_infos: usize,
82 }
83 
84 /// Enum representing the type of change that occurred in a profile entry.
85 #[repr(i32)]
86 #[derive(Copy, Clone)]
87 pub enum CProfileChangeType {
88     /// Represents an unknown or uninitialized change type.
89     Unknown,
90     /// Represents an inserted profile entry.
91     Inserted,
92     /// Represents an updated profile entry.
93     Updated,
94     /// Represents a deleted profile entry.
95     Deleted,
96 }
97 
98 /// Struct representing a profile entry.
99 #[repr(C)]
100 pub struct CProfileEntry {
101     /// A pointer to the key of the profile entry.
102     pub key: *const c_char,
103     /// A pointer to the value of the profile entry.
104     pub value: *const c_char,
105     /// The type of change that occurred for this profile entry.
106     pub change_type: CProfileChangeType,
107     /// A pointer to the next profile entry in the linked list.
108     pub next: *const CProfileEntry,
109 }
110 
111 /// C representation of [`ProfileChangeNotification`].
112 #[repr(C)]
113 pub struct CProfileChangeNotification {
114     /// A pointer to an array of profile entries.
115     pub profile_entries: *const CProfileEntry,
116     /// The number of profile entries in the array.
117     pub n_profile_entries: usize,
118     /// A pointer to the device ID associated with the profile change.
119     pub device_id: *const c_char,
120     /// An integer indicating whether the profile change occurred locally.
121     pub local_flag: c_int,
122 }
123 
124 /// Type definition for the clone function of a CIProfileEventCb.
125 pub type CIProfileEventCbClone = extern "C" fn (profile_events: *mut CIProfileEventCb) -> *mut CIProfileEventCb;
126 /// Type definition for the destruct function of a CIProfileEventCb.
127 pub type CIProfileEventCbDestruct = extern "C" fn (profile_events: *mut CIProfileEventCb);
128 /// Type definition for the callback function when the profile changes.
129 pub type OnProfileChanged = extern "C" fn (cb: *mut CIProfileEventCb, notification: *const CProfileChangeNotification);
130 
131 /// Struct representing a callback for profile events.
132 #[repr(C)]
133 pub struct CIProfileEventCb {
134     /// The clone function for the callback.
135     pub clone: Option<CIProfileEventCbClone>,
136     /// The destruct function for the callback.
137     pub destruct: Option<CIProfileEventCbDestruct>,
138     /// The callback function to be executed when the profile changes.
139     pub on_profile_changed: Option<OnProfileChanged>,
140 }
141 
142 /// C representation of [`SyncMode`].
143 #[repr(i32)]
144 #[derive(Copy, Clone)]
145 pub enum CSyncMode {
146     /// The pull synchronization mode, where the local device pulls data from the remote device.
147     Pull,
148     /// The push synchronization mode, where the local device pushes data to the remote device.
149     Push,
150     /// The push-pull synchronization mode, where the local and remote devices can push and pull data
151     /// in either direction.
152     PushPull,
153 }
154 
155 /// C representation of [`SyncOptions`].
156 #[repr(C)]
157 pub struct CSyncOptions {
158     /// The synchronization mode.
159     pub sync_mode: CSyncMode,
160     /// Pointer to an array of device IDs.
161     pub device_ids: *const *const c_char,
162     /// The number of device IDs in the array.
163     pub n_device_ids: usize,
164 }
165 
166 // These C interfaces are defined in lib: device_profile:fusion_device_profile_binding.
167 extern "C" {
168     /// Puts a device profile into the system.
PutDeviceProfilenull169     pub fn PutDeviceProfile(profile: *const CServiceCharacteristicProfile) -> i32;
170     /// Gets a device profile from the system.
GetDeviceProfilenull171     pub fn GetDeviceProfile(ud_id: *const c_char,
172                         service_id: *const c_char,
173                         profile: *mut CServiceCharacteristicProfile) -> i32;
174     /// Subscribes to profile events for the specified subscribe information.
SubscribeProfileEventsnull175     pub fn SubscribeProfileEvents(subscribe_infos: *const CISubscribeInfos,
176                                   event_cb: *mut CIProfileEventCb,
177                                   failed_events: *mut *mut CIProfileEvents) -> i32;
178     /// Unsubscribes from profile events for the specified profile events and callback function.
UnsubscribeProfileEventsnull179     pub fn UnsubscribeProfileEvents(profile_events: *const CIProfileEvents,
180                                     event_cb: *mut CIProfileEventCb,
181                                     failed_events: *mut *mut CIProfileEvents) -> i32;
182 }
183 
184 type CIStringVectorClone = extern "C" fn (*mut CIStringVector) -> *mut CIStringVector;
185 type CIStringVectorDestruct = extern "C" fn (*mut CIStringVector);
186 type CIStringVectorAt = extern "C" fn (*mut CIStringVector, usize) -> *const c_char;
187 type CIStringVectorSize = extern "C" fn (*mut CIStringVector) -> usize;
188 
189 /// Struct representing a CIStringVector.
190 #[repr(C)]
191 pub struct CIStringVector {
192     /// An optional clone function for cloning the CIStringVector.
193     pub clone: Option<CIStringVectorClone>,
194     /// An optional destruct function for cleaning up the CIStringVector.
195     pub destruct: Option<CIStringVectorDestruct>,
196     /// An optional function for accessing an element at a given index.
197     pub get: Option<CIStringVectorAt>,
198     /// An optional function for getting the size of the CIStringVector.
199     pub get_size: Option<CIStringVectorSize>,
200 }
201 
202 type CICrossStateListenerClone = extern "C" fn (listener: *mut CICrossStateListener) -> *mut CICrossStateListener;
203 type CICrossStateListenerDestruct = extern "C" fn (listener: *mut CICrossStateListener);
204 type OnCrossStateUpdate = extern "C" fn (listener: *mut CICrossStateListener, device_id: *const c_char, state: u32);
205 
206 /// A struct representing a cross-state listener.
207 #[repr(C)]
208 pub struct CICrossStateListener {
209     /// An optional function pointer for cloning the cross-state listener.
210     pub clone: Option<CICrossStateListenerClone>,
211     /// An optional function pointer for destructing or cleaning up the cross-state listener.
212     pub destruct: Option<CICrossStateListenerDestruct>,
213     /// An optional callback function for handling cross-state update events.
214     pub on_update: Option<OnCrossStateUpdate>,
215 }
216 
217 // These C interfaces are defined in lib: device_profile:fusion_device_profile_binding.
218 extern "C" {
219     /// Updates the state of the cross switch for all registered devices.
220     ///
221     /// # Arguments
222     ///
223     /// * `state` - An unsigned integer value that represents the cross switch state.
224     ///
225     /// # Returns
226     ///
227     /// Returns an `i32` value representing the result of the update operation.
UpdateCrossSwitchStatenull228     pub fn UpdateCrossSwitchState(state: u32) -> i32;
229     /// Synchronizes the state of the cross switch with multiple devices.
230     ///
231     /// # Arguments
232     ///
233     /// * `state` - The state to synchronize the cross switch to.
234     /// * `device_ids` - A mutable pointer that contains the device IDs to synchronize.
235     ///
236     /// # Returns
237     ///
238     /// Returns an `i32` value representing the result of the synchronization operation.
SyncCrossSwitchStatenull239     pub fn SyncCrossSwitchState(state: u32, device_ids: *mut CIStringVector) -> i32;
240     /// Retrieves the state of the cross switch for a specific device.
241     ///
242     /// # Arguments
243     ///
244     /// * `device_id` - The ID of the device to retrieve the cross switch state for.
245     ///
246     /// # Returns
247     ///
248     /// Returns an `i32` value representing the state of the cross switch for the specified device.
GetCrossSwitchStatenull249     pub fn GetCrossSwitchState(device_id: *const c_char) -> i32;
250     /// Registers a cross state listener for a specific device.
251     ///
252     /// # Arguments
253     ///
254     /// * `device_id` - The ID of the device to register the listener for.
255     /// * `listener` - A mutable pointer to the cross state listener to register.
256     ///
257     /// # Returns
258     ///
259     /// Returns an `i32` value representing the result of the registration operation.
RegisterCrossStateListenernull260     pub fn RegisterCrossStateListener(device_id: *const c_char, listener: *mut CICrossStateListener) -> i32;
261     /// Unregisters a cross state listener for a specific device.
262     ///
263     /// # Arguments
264     ///
265     /// * `device_id` - The ID of the device to unregister the listener from.
266     ///
267     /// # Returns
268     ///
269     /// Returns an `i32` value representing the result of the unregistration operation.
UnregisterCrossStateListenernull270     pub fn UnregisterCrossStateListener(device_id: *const c_char) -> i32;
271 }
272 
273 type CIStringClone = extern "C" fn (*mut CIString) -> *mut CIString;
274 type CIStringDestruct = extern "C" fn (*mut CIString);
275 type CIStringGetData = extern "C" fn (*mut CIString) -> *const c_char;
276 
277 // Struct representing a CIString.
278 #[repr(C)]
279 pub struct CIString {
280     /// Optional function pointer for cloning the string.
281     pub clone: Option<CIStringClone>,
282     /// Optional function pointer for destructing the string.
283     pub destruct: Option<CIStringDestruct>,
284     /// Optional function pointer for retrieving the string data.
285     pub data: Option<CIStringGetData>,
286 }
287 
288 // These C interfaces are defined in lib: device_profile:fusion_device_profile_binding.
289 extern "C" {
290     /// Retrieves an access token from a C++ API.
GetAccessTokennull291     pub fn GetAccessToken();
292     /// Get a mutable pointer to CIString.
293     ///
294     /// # Returns
295     ///
296     /// A mutable pointer to CIString. Please note that the pointer may be null if there is an error or no local network
297     /// ID is available.The caller should handle null pointer values appropriately.
GetLocalNetworkIdnull298     pub fn GetLocalNetworkId() -> *mut CIString;
299 }
300