1 // Copyright (C) 2024 Huawei Device Co., Ltd.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 //! cxx wrapper
15 #![allow(missing_docs)]
16
17 use std::fs::File;
18 use std::os::fd::FromRawFd;
19 use std::pin::Pin;
20
21 use cxx::UniquePtr;
22 pub use ffi::*;
23
24 pub use super::obj::RemoteObj;
25 use super::stub::RemoteStub;
26 use crate::parcel::MsgParcel;
27
28 #[cxx::bridge(namespace = "OHOS::IpcRust")]
29 pub mod ffi {
30
31 extern "Rust" {
32 type RemoteObj;
33 pub type RemoteStubWrapper;
34
on_remote_requestnull35 fn on_remote_request(
36 self: &mut RemoteStubWrapper,
37 code: u32,
38 data: Pin<&mut MessageParcel>,
39 reply: Pin<&mut MessageParcel>,
40 ) -> i32;
41
dumpnull42 fn dump(self: &mut RemoteStubWrapper, fd: i32, args: Vec<String>) -> i32;
43
descriptornull44 fn descriptor(self: &mut RemoteStubWrapper) -> &'static str;
45
46 fn new_remote_obj(wrap: UniquePtr<IRemoteObjectWrapper>) -> Box<RemoteObj>;
47
48 }
49
50 unsafe extern "C++" {
51 include!("remote_object_wrapper.h");
52 type IRemoteObjectWrapper;
53
54 #[namespace = "OHOS"]
55 type IRemoteObject;
56 #[namespace = "OHOS"]
57 type MessageParcel = crate::parcel::wrapper::MessageParcel;
58 #[namespace = "OHOS"]
59 type MessageOption = crate::parcel::wrapper::MessageOption;
60 type DeathRecipientRemoveHandler;
61
62 #[namespace = "OHOS"]
63 type SptrIRemoteObject;
64
FromSptrRemotenull65 fn FromSptrRemote(sptr: UniquePtr<SptrIRemoteObject>) -> UniquePtr<IRemoteObjectWrapper>;
66
FromRemoteStubnull67 fn FromRemoteStub(stub: Box<RemoteStubWrapper>) -> UniquePtr<IRemoteObjectWrapper>;
68
FromCIRemoteObjectnull69 unsafe fn FromCIRemoteObject(remote: *mut IRemoteObject)
70 -> UniquePtr<IRemoteObjectWrapper>;
71
SendRequestnull72 fn SendRequest(
73 self: &IRemoteObjectWrapper,
74 code: u32,
75 data: Pin<&mut MessageParcel>,
76 reply: Pin<&mut MessageParcel>,
77 option: Pin<&mut MessageOption>,
78 ) -> i32;
79
GetInterfaceDescriptornull80 fn GetInterfaceDescriptor(self: &IRemoteObjectWrapper) -> String;
GetObjectDescriptornull81 fn GetObjectDescriptor(self: &IRemoteObjectWrapper) -> String;
82
IsProxyObjectnull83 fn IsProxyObject(self: &IRemoteObjectWrapper) -> bool;
IsObjectDeadnull84 fn IsObjectDead(self: &IRemoteObjectWrapper) -> bool;
CheckObjectLegalitynull85 fn CheckObjectLegality(self: &IRemoteObjectWrapper) -> bool;
86
Dumpnull87 fn Dump(self: &IRemoteObjectWrapper, fd: i32, args: &[String]) -> i32;
AddDeathRecipientnull88 fn AddDeathRecipient(
89 self: &IRemoteObjectWrapper,
90 cb: fn(Box<RemoteObj>),
91 ) -> UniquePtr<DeathRecipientRemoveHandler>;
92
removenull93 fn remove(self: &DeathRecipientRemoveHandler);
94
CloneRemoteObjnull95 fn CloneRemoteObj(remote: &IRemoteObjectWrapper) -> UniquePtr<IRemoteObjectWrapper>;
96 }
97 impl UniquePtr<IRemoteObjectWrapper> {}
98 }
99
new_remote_objnull100 fn new_remote_obj(wrap: UniquePtr<ffi::IRemoteObjectWrapper>) -> Box<RemoteObj> {
101 Box::new(RemoteObj::try_new(wrap).unwrap())
102 }
103
104 pub struct RemoteStubWrapper {
105 inner: Box<dyn RemoteStub>,
106 }
107
108 impl RemoteStubWrapper {
newnull109 pub fn new<A: RemoteStub + 'static>(remote: A) -> Self {
110 Self {
111 inner: Box::new(remote),
112 }
113 }
114
on_remote_requestnull115 pub fn on_remote_request(
116 &mut self,
117 code: u32,
118 data: Pin<&mut MessageParcel>,
119 reply: Pin<&mut MessageParcel>,
120 ) -> i32 {
121 unsafe {
122 let mut data = MsgParcel::from_ptr(data.get_unchecked_mut() as *mut MessageParcel);
123 let mut reply = MsgParcel::from_ptr(reply.get_unchecked_mut() as *mut MessageParcel);
124 self.inner.on_remote_request(code, &mut data, &mut reply)
125 }
126 }
127
dumpnull128 pub fn dump(&mut self, fd: i32, args: Vec<String>) -> i32 {
129 let file = unsafe { File::from_raw_fd(fd) };
130 self.inner.dump(file, args)
131 }
132
descriptornull133 pub fn descriptor(&self) -> &'static str {
134 self.inner.descriptor()
135 }
136
137 pub fn into_remote(self) -> Option<RemoteObj> {
138 RemoteObj::try_new(FromRemoteStub(Box::new(self)))
139 }
140 }
141