rust_rcs_client/messaging/
ffi.rs

1// Copyright 2023 宋昊文
2//
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
15use std::ptr::NonNull;
16
17use libc::c_char;
18
19pub enum RecipientType {
20    Contact,
21    Chatbot,
22    Group,
23    ResourceList,
24}
25
26impl From<&RecipientType> for RecipientType {
27    fn from(recipient_type: &RecipientType) -> RecipientType {
28        match recipient_type {
29            RecipientType::Contact => RecipientType::Contact,
30            RecipientType::Chatbot => RecipientType::Chatbot,
31            RecipientType::Group => RecipientType::Group,
32            RecipientType::ResourceList => RecipientType::ResourceList,
33        }
34    }
35}
36
37pub fn get_recipient_type(recipient_type: i8) -> Option<RecipientType> {
38    if recipient_type == 0 {
39        Some(RecipientType::Contact)
40    } else if recipient_type == 1 {
41        Some(RecipientType::Chatbot)
42    } else if recipient_type == 2 {
43        Some(RecipientType::Group)
44    } else if recipient_type == 3 {
45        Some(RecipientType::ResourceList)
46    } else {
47        None
48    }
49}
50
51pub type MessageResultCallback = extern "C" fn(
52    status_code: u16,
53    reason_phrase: *const c_char,
54    context: *mut MessageResultCallbackContext,
55);
56
57#[repr(C)]
58pub struct MessageResultCallbackContext {
59    _data: [u8; 0],
60    _marker: core::marker::PhantomData<(*mut u8, core::marker::PhantomPinned)>,
61}
62
63#[cfg(any(
64    all(feature = "android", target_os = "android"),
65    all(feature = "ohos", all(target_os = "linux", target_env = "ohos"))
66))]
67extern "C" {
68    fn message_result_callback_context_release(context: *mut MessageResultCallbackContext);
69}
70
71pub struct MessageResultCallbackContextWrapper(pub NonNull<MessageResultCallbackContext>);
72
73impl Drop for MessageResultCallbackContextWrapper {
74    fn drop(&mut self) {
75        #[cfg(any(
76            all(feature = "android", target_os = "android"),
77            all(feature = "ohos", all(target_os = "linux", target_env = "ohos"))
78        ))]
79        let cb_context = self.0.as_ptr();
80        #[cfg(any(
81            all(feature = "android", target_os = "android"),
82            all(feature = "ohos", all(target_os = "linux", target_env = "ohos"))
83        ))]
84        unsafe {
85            message_result_callback_context_release(cb_context);
86        }
87    }
88}
89
90unsafe impl Send for MessageResultCallbackContextWrapper {}
91
92pub type SendImdnReportResultCallback = extern "C" fn(
93    status_code: u16,
94    reason_phrase: *const c_char,
95    context: *mut SendImdnReportResultCallbackContext,
96);
97
98#[repr(C)]
99pub struct SendImdnReportResultCallbackContext {
100    _data: [u8; 0],
101    _marker: core::marker::PhantomData<(*mut u8, core::marker::PhantomPinned)>,
102}
103
104#[cfg(any(
105    all(feature = "android", target_os = "android"),
106    all(feature = "ohos", all(target_os = "linux", target_env = "ohos"))
107))]
108extern "C" {
109    fn send_imdn_report_result_callback_context_release(
110        context: *mut SendImdnReportResultCallbackContext,
111    );
112}
113
114pub struct SendImdnReportResultCallbackContextWrapper(
115    pub NonNull<SendImdnReportResultCallbackContext>,
116);
117
118impl Drop for SendImdnReportResultCallbackContextWrapper {
119    fn drop(&mut self) {
120        #[cfg(any(
121            all(feature = "android", target_os = "android"),
122            all(feature = "ohos", all(target_os = "linux", target_env = "ohos"))
123        ))]
124        let cb_context = self.0.as_ptr();
125        #[cfg(any(
126            all(feature = "android", target_os = "android"),
127            all(feature = "ohos", all(target_os = "linux", target_env = "ohos"))
128        ))]
129        unsafe {
130            send_imdn_report_result_callback_context_release(cb_context);
131        }
132    }
133}
134
135unsafe impl Send for SendImdnReportResultCallbackContextWrapper {}
136
137pub type UploadFileProgressCallback =
138    extern "C" fn(current: u32, total: i32, context: *mut UploadFileProgressCallbackContext);
139
140#[repr(C)]
141pub struct UploadFileProgressCallbackContext {
142    _data: [u8; 0],
143    _marker: core::marker::PhantomData<(*mut u8, core::marker::PhantomPinned)>,
144}
145
146#[cfg(any(
147    all(feature = "android", target_os = "android"),
148    all(feature = "ohos", all(target_os = "linux", target_env = "ohos"))
149))]
150extern "C" {
151    fn upload_file_progress_callback_context_release(
152        context: *mut UploadFileProgressCallbackContext,
153    );
154}
155
156pub struct UploadFileProgressCallbackContextWrapper(pub NonNull<UploadFileProgressCallbackContext>);
157
158impl Drop for UploadFileProgressCallbackContextWrapper {
159    fn drop(&mut self) {
160        #[cfg(any(
161            all(feature = "android", target_os = "android"),
162            all(feature = "ohos", all(target_os = "linux", target_env = "ohos"))
163        ))]
164        let cb_context = self.0.as_ptr();
165        #[cfg(any(
166            all(feature = "android", target_os = "android"),
167            all(feature = "ohos", all(target_os = "linux", target_env = "ohos"))
168        ))]
169        unsafe {
170            upload_file_progress_callback_context_release(cb_context);
171        }
172    }
173}
174
175unsafe impl Send for UploadFileProgressCallbackContextWrapper {}
176
177pub type UploadFileResultCallback = extern "C" fn(
178    status_code: u16,
179    reason_phrase: *const c_char,
180    xml_result: *const c_char,
181    context: *mut UploadFileResultCallbackContext,
182);
183
184#[repr(C)]
185pub struct UploadFileResultCallbackContext {
186    _data: [u8; 0],
187    _marker: core::marker::PhantomData<(*mut u8, core::marker::PhantomPinned)>,
188}
189
190#[cfg(any(
191    all(feature = "android", target_os = "android"),
192    all(feature = "ohos", all(target_os = "linux", target_env = "ohos"))
193))]
194extern "C" {
195    fn upload_file_result_callback_context_release(context: *mut UploadFileResultCallbackContext);
196}
197
198pub struct UploadFileResultCallbackContextWrapper(pub NonNull<UploadFileResultCallbackContext>);
199
200impl Drop for UploadFileResultCallbackContextWrapper {
201    fn drop(&mut self) {
202        #[cfg(any(
203            all(feature = "android", target_os = "android"),
204            all(feature = "ohos", all(target_os = "linux", target_env = "ohos"))
205        ))]
206        let cb_context = self.0.as_ptr();
207        #[cfg(any(
208            all(feature = "android", target_os = "android"),
209            all(feature = "ohos", all(target_os = "linux", target_env = "ohos"))
210        ))]
211        unsafe {
212            upload_file_result_callback_context_release(cb_context);
213        }
214    }
215}
216
217unsafe impl Send for UploadFileResultCallbackContextWrapper {}
218
219pub type DownloadFileProgressCallback =
220    extern "C" fn(current: u32, total: i32, context: *mut DownloadFileProgressCallbackContext);
221
222#[repr(C)]
223pub struct DownloadFileProgressCallbackContext {
224    _data: [u8; 0],
225    _marker: core::marker::PhantomData<(*mut u8, core::marker::PhantomPinned)>,
226}
227
228#[cfg(any(
229    all(feature = "android", target_os = "android"),
230    all(feature = "ohos", all(target_os = "linux", target_env = "ohos"))
231))]
232extern "C" {
233    fn download_file_progress_callback_context_release(
234        context: *mut DownloadFileProgressCallbackContext,
235    );
236}
237
238pub struct DownloadileProgressCallbackContextWrapper(
239    pub NonNull<DownloadFileProgressCallbackContext>,
240);
241
242impl Drop for DownloadileProgressCallbackContextWrapper {
243    fn drop(&mut self) {
244        #[cfg(any(
245            all(feature = "android", target_os = "android"),
246            all(feature = "ohos", all(target_os = "linux", target_env = "ohos"))
247        ))]
248        let cb_context = self.0.as_ptr();
249        #[cfg(any(
250            all(feature = "android", target_os = "android"),
251            all(feature = "ohos", all(target_os = "linux", target_env = "ohos"))
252        ))]
253        unsafe {
254            download_file_progress_callback_context_release(cb_context);
255        }
256    }
257}
258
259unsafe impl Send for DownloadileProgressCallbackContextWrapper {}
260
261pub type DownloadFileResultCallback = extern "C" fn(
262    status_code: u16,
263    reason_phrase: *const c_char,
264    context: *mut DownloadFileResultCallbackContext,
265);
266
267#[repr(C)]
268pub struct DownloadFileResultCallbackContext {
269    _data: [u8; 0],
270    _marker: core::marker::PhantomData<(*mut u8, core::marker::PhantomPinned)>,
271}
272
273#[cfg(any(
274    all(feature = "android", target_os = "android"),
275    all(feature = "ohos", all(target_os = "linux", target_env = "ohos"))
276))]
277extern "C" {
278    fn download_file_result_callback_context_release(
279        context: *mut DownloadFileResultCallbackContext,
280    );
281}
282
283pub struct DownloadFileResultCallbackContextWrapper(pub NonNull<DownloadFileResultCallbackContext>);
284
285impl Drop for DownloadFileResultCallbackContextWrapper {
286    fn drop(&mut self) {
287        #[cfg(any(
288            all(feature = "android", target_os = "android"),
289            all(feature = "ohos", all(target_os = "linux", target_env = "ohos"))
290        ))]
291        let cb_context = self.0.as_ptr();
292        #[cfg(any(
293            all(feature = "android", target_os = "android"),
294            all(feature = "ohos", all(target_os = "linux", target_env = "ohos"))
295        ))]
296        unsafe {
297            download_file_result_callback_context_release(cb_context);
298        }
299    }
300}
301
302unsafe impl Send for DownloadFileResultCallbackContextWrapper {}