rust_rcs_client/
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::{ffi::c_char, ptr::NonNull};
16
17use crate::messaging::cpm::MessagingSessionHandle;
18
19pub type StateChangeCallback = extern "C" fn(state: i32, context: *mut StateChangeCallbackContext);
20
21#[repr(C)]
22pub struct StateChangeCallbackContext {
23    _data: [u8; 0],
24    _marker: core::marker::PhantomData<(*mut u8, core::marker::PhantomPinned)>,
25}
26
27#[cfg(any(
28    all(feature = "android", target_os = "android"),
29    all(feature = "ohos", all(target_os = "linux", target_env = "ohos"))
30))]
31extern "C" {
32    fn state_change_callback_context_release(context: *mut StateChangeCallbackContext);
33}
34
35pub struct StateChangeCallbackContextWrapper(pub NonNull<StateChangeCallbackContext>);
36
37impl Drop for StateChangeCallbackContextWrapper {
38    fn drop(&mut self) {
39        #[cfg(any(
40            all(feature = "android", target_os = "android"),
41            all(feature = "ohos", all(target_os = "linux", target_env = "ohos"))
42        ))]
43        let cb_context = self.0.as_ptr();
44        #[cfg(any(
45            all(feature = "android", target_os = "android"),
46            all(feature = "ohos", all(target_os = "linux", target_env = "ohos"))
47        ))]
48        unsafe {
49            state_change_callback_context_release(cb_context);
50        }
51    }
52}
53
54unsafe impl Send for StateChangeCallbackContextWrapper {}
55
56pub type MessageCallback = extern "C" fn(
57    i32,
58    Option<Box<MessagingSessionHandle>>,
59    *const c_char,
60    *const c_char,
61    *const c_char,
62    *const c_char,
63    *const c_char,
64    *const c_char,
65    context: *mut MessageCallbackContext,
66);
67
68#[repr(C)]
69pub struct MessageCallbackContext {
70    _data: [u8; 0],
71    _marker: core::marker::PhantomData<(*mut u8, core::marker::PhantomPinned)>,
72}
73
74#[cfg(any(
75    all(feature = "android", target_os = "android"),
76    all(feature = "ohos", all(target_os = "linux", target_env = "ohos"))
77))]
78extern "C" {
79    fn message_callback_context_release(context: *mut MessageCallbackContext);
80}
81
82pub struct MessageCallbackContextWrapper(pub NonNull<MessageCallbackContext>);
83
84impl Drop for MessageCallbackContextWrapper {
85    fn drop(&mut self) {
86        #[cfg(any(
87            all(feature = "android", target_os = "android"),
88            all(feature = "ohos", all(target_os = "linux", target_env = "ohos"))
89        ))]
90        let cb_context = self.0.as_ptr();
91        #[cfg(any(
92            all(feature = "android", target_os = "android"),
93            all(feature = "ohos", all(target_os = "linux", target_env = "ohos"))
94        ))]
95        unsafe {
96            message_callback_context_release(cb_context);
97        }
98    }
99}
100
101unsafe impl Send for MessageCallbackContextWrapper {}