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_request( self: &mut RemoteStubWrapper, code: u32, data: Pin<&mut MessageParcel>, reply: Pin<&mut MessageParcel>, ) -> i3235         fn on_remote_request(
36             self: &mut RemoteStubWrapper,
37             code: u32,
38             data: Pin<&mut MessageParcel>,
39             reply: Pin<&mut MessageParcel>,
40         ) -> i32;
41 
dump(self: &mut RemoteStubWrapper, fd: i32, args: Vec<String>) -> i3242         fn dump(self: &mut RemoteStubWrapper, fd: i32, args: Vec<String>) -> i32;
43 
descriptor(self: &mut RemoteStubWrapper) -> &'static str44         fn descriptor(self: &mut RemoteStubWrapper) -> &'static str;
45 
new_remote_obj(wrap: UniquePtr<IRemoteObjectWrapper>) -> Box<RemoteObj>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 
FromSptrRemote(sptr: UniquePtr<SptrIRemoteObject>) -> UniquePtr<IRemoteObjectWrapper>65         fn FromSptrRemote(sptr: UniquePtr<SptrIRemoteObject>) -> UniquePtr<IRemoteObjectWrapper>;
66 
FromRemoteStub(stub: Box<RemoteStubWrapper>) -> UniquePtr<IRemoteObjectWrapper>67         fn FromRemoteStub(stub: Box<RemoteStubWrapper>) -> UniquePtr<IRemoteObjectWrapper>;
68 
FromCIRemoteObject(remote: *mut IRemoteObject) -> UniquePtr<IRemoteObjectWrapper>69         unsafe fn FromCIRemoteObject(remote: *mut IRemoteObject)
70             -> UniquePtr<IRemoteObjectWrapper>;
71 
SendRequest( self: &IRemoteObjectWrapper, code: u32, data: Pin<&mut MessageParcel>, reply: Pin<&mut MessageParcel>, option: Pin<&mut MessageOption>, ) -> i3272         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 
GetInterfaceDescriptor(self: &IRemoteObjectWrapper) -> String80         fn GetInterfaceDescriptor(self: &IRemoteObjectWrapper) -> String;
GetObjectDescriptor(self: &IRemoteObjectWrapper) -> String81         fn GetObjectDescriptor(self: &IRemoteObjectWrapper) -> String;
82 
IsProxyObject(self: &IRemoteObjectWrapper) -> bool83         fn IsProxyObject(self: &IRemoteObjectWrapper) -> bool;
IsObjectDead(self: &IRemoteObjectWrapper) -> bool84         fn IsObjectDead(self: &IRemoteObjectWrapper) -> bool;
CheckObjectLegality(self: &IRemoteObjectWrapper) -> bool85         fn CheckObjectLegality(self: &IRemoteObjectWrapper) -> bool;
86 
Dump(self: &IRemoteObjectWrapper, fd: i32, args: &[String]) -> i3287         fn Dump(self: &IRemoteObjectWrapper, fd: i32, args: &[String]) -> i32;
AddDeathRecipient( self: &IRemoteObjectWrapper, cb: fn(Box<RemoteObj>), ) -> UniquePtr<DeathRecipientRemoveHandler>88         fn AddDeathRecipient(
89             self: &IRemoteObjectWrapper,
90             cb: fn(Box<RemoteObj>),
91         ) -> UniquePtr<DeathRecipientRemoveHandler>;
92 
remove(self: &DeathRecipientRemoveHandler)93         fn remove(self: &DeathRecipientRemoveHandler);
94 
CloneRemoteObj(remote: &IRemoteObjectWrapper) -> UniquePtr<IRemoteObjectWrapper>95         fn CloneRemoteObj(remote: &IRemoteObjectWrapper) -> UniquePtr<IRemoteObjectWrapper>;
96     }
97     impl UniquePtr<IRemoteObjectWrapper> {}
98 }
99 
new_remote_obj(wrap: UniquePtr<ffi::IRemoteObjectWrapper>) -> Box<RemoteObj>100 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 {
new<A: RemoteStub + 'static>(remote: A) -> Self109     pub fn new<A: RemoteStub + 'static>(remote: A) -> Self {
110         Self {
111             inner: Box::new(remote),
112         }
113     }
114 
on_remote_request( &mut self, code: u32, data: Pin<&mut MessageParcel>, reply: Pin<&mut MessageParcel>, ) -> i32115     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 
dump(&mut self, fd: i32, args: Vec<String>) -> i32128     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 
descriptor(&self) -> &'static str133     pub fn descriptor(&self) -> &'static str {
134         self.inner.descriptor()
135     }
136 
into_remote(self) -> Option<RemoteObj>137     pub fn into_remote(self) -> Option<RemoteObj> {
138         RemoteObj::try_new(FromRemoteStub(Box::new(self)))
139     }
140 }
141