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 use crate::remote::obj::ClosureWrapper;
28
29 #[cxx::bridge(namespace = "OHOS::IpcRust")]
30 pub mod ffi {
31
32 extern "Rust" {
33 type RemoteObj;
34 type ClosureWrapper;
35
execute(self: &mut ClosureWrapper, obj: Box<RemoteObj>)36 fn execute(self: &mut ClosureWrapper, obj: Box<RemoteObj>);
37
38 pub type RemoteStubWrapper;
39
on_remote_request( self: &mut RemoteStubWrapper, code: u32, data: Pin<&mut MessageParcel>, reply: Pin<&mut MessageParcel>, ) -> i3240 fn on_remote_request(
41 self: &mut RemoteStubWrapper,
42 code: u32,
43 data: Pin<&mut MessageParcel>,
44 reply: Pin<&mut MessageParcel>,
45 ) -> i32;
46
dump(self: &mut RemoteStubWrapper, fd: i32, args: Vec<String>) -> i3247 fn dump(self: &mut RemoteStubWrapper, fd: i32, args: Vec<String>) -> i32;
48
descriptor(self: &mut RemoteStubWrapper) -> &'static str49 fn descriptor(self: &mut RemoteStubWrapper) -> &'static str;
50
new_remote_obj(wrap: UniquePtr<IRemoteObjectWrapper>) -> Box<RemoteObj>51 fn new_remote_obj(wrap: UniquePtr<IRemoteObjectWrapper>) -> Box<RemoteObj>;
52
53 }
54
55 unsafe extern "C++" {
56 include!("remote_object_wrapper.h");
57 type IRemoteObjectWrapper;
58
59 #[namespace = "OHOS"]
60 type IRemoteObject;
61 #[namespace = "OHOS"]
62 type MessageParcel = crate::parcel::wrapper::MessageParcel;
63 #[namespace = "OHOS"]
64 type MessageOption = crate::parcel::wrapper::MessageOption;
65 type DeathRecipientRemoveHandler;
66
67 #[namespace = "OHOS"]
68 type SptrIRemoteObject;
69
FromSptrRemote(sptr: UniquePtr<SptrIRemoteObject>) -> UniquePtr<IRemoteObjectWrapper>70 fn FromSptrRemote(sptr: UniquePtr<SptrIRemoteObject>) -> UniquePtr<IRemoteObjectWrapper>;
71
FromRemoteStub(stub: Box<RemoteStubWrapper>) -> UniquePtr<IRemoteObjectWrapper>72 fn FromRemoteStub(stub: Box<RemoteStubWrapper>) -> UniquePtr<IRemoteObjectWrapper>;
73
FromCIRemoteObject(remote: *mut IRemoteObject) -> UniquePtr<IRemoteObjectWrapper>74 unsafe fn FromCIRemoteObject(remote: *mut IRemoteObject)
75 -> UniquePtr<IRemoteObjectWrapper>;
76
SendRequest( self: &IRemoteObjectWrapper, code: u32, data: Pin<&mut MessageParcel>, reply: Pin<&mut MessageParcel>, option: Pin<&mut MessageOption>, ) -> i3277 fn SendRequest(
78 self: &IRemoteObjectWrapper,
79 code: u32,
80 data: Pin<&mut MessageParcel>,
81 reply: Pin<&mut MessageParcel>,
82 option: Pin<&mut MessageOption>,
83 ) -> i32;
84
GetInterfaceDescriptor(self: &IRemoteObjectWrapper) -> String85 fn GetInterfaceDescriptor(self: &IRemoteObjectWrapper) -> String;
GetObjectDescriptor(self: &IRemoteObjectWrapper) -> String86 fn GetObjectDescriptor(self: &IRemoteObjectWrapper) -> String;
87
IsProxyObject(self: &IRemoteObjectWrapper) -> bool88 fn IsProxyObject(self: &IRemoteObjectWrapper) -> bool;
IsObjectDead(self: &IRemoteObjectWrapper) -> bool89 fn IsObjectDead(self: &IRemoteObjectWrapper) -> bool;
CheckObjectLegality(self: &IRemoteObjectWrapper) -> bool90 fn CheckObjectLegality(self: &IRemoteObjectWrapper) -> bool;
91
Dump(self: &IRemoteObjectWrapper, fd: i32, args: &[String]) -> i3292 fn Dump(self: &IRemoteObjectWrapper, fd: i32, args: &[String]) -> i32;
AddDeathRecipient( self: &IRemoteObjectWrapper, cb: Box<ClosureWrapper>, ) -> UniquePtr<DeathRecipientRemoveHandler>93 fn AddDeathRecipient(
94 self: &IRemoteObjectWrapper,
95 cb: Box<ClosureWrapper>,
96 ) -> UniquePtr<DeathRecipientRemoveHandler>;
97
remove(self: &DeathRecipientRemoveHandler)98 fn remove(self: &DeathRecipientRemoveHandler);
99
CloneRemoteObj(remote: &IRemoteObjectWrapper) -> UniquePtr<IRemoteObjectWrapper>100 fn CloneRemoteObj(remote: &IRemoteObjectWrapper) -> UniquePtr<IRemoteObjectWrapper>;
101 }
102 impl UniquePtr<IRemoteObjectWrapper> {}
103 }
104
new_remote_obj(wrap: UniquePtr<ffi::IRemoteObjectWrapper>) -> Box<RemoteObj>105 fn new_remote_obj(wrap: UniquePtr<ffi::IRemoteObjectWrapper>) -> Box<RemoteObj> {
106 Box::new(RemoteObj::try_new(wrap).unwrap())
107 }
108
109 pub struct RemoteStubWrapper {
110 inner: Box<dyn RemoteStub>,
111 }
112
113 impl RemoteStubWrapper {
new<A: RemoteStub + 'static>(remote: A) -> Self114 pub fn new<A: RemoteStub + 'static>(remote: A) -> Self {
115 Self {
116 inner: Box::new(remote),
117 }
118 }
119
on_remote_request( &mut self, code: u32, data: Pin<&mut MessageParcel>, reply: Pin<&mut MessageParcel>, ) -> i32120 pub fn on_remote_request(
121 &mut self,
122 code: u32,
123 data: Pin<&mut MessageParcel>,
124 reply: Pin<&mut MessageParcel>,
125 ) -> i32 {
126 unsafe {
127 let mut data = MsgParcel::from_ptr(data.get_unchecked_mut() as *mut MessageParcel);
128 let mut reply = MsgParcel::from_ptr(reply.get_unchecked_mut() as *mut MessageParcel);
129 self.inner.on_remote_request(code, &mut data, &mut reply)
130 }
131 }
132
dump(&mut self, fd: i32, args: Vec<String>) -> i32133 pub fn dump(&mut self, fd: i32, args: Vec<String>) -> i32 {
134 let file = unsafe { File::from_raw_fd(fd) };
135 self.inner.dump(file, args)
136 }
137
descriptor(&self) -> &'static str138 pub fn descriptor(&self) -> &'static str {
139 self.inner.descriptor()
140 }
141
into_remote(self) -> Option<RemoteObj>142 pub fn into_remote(self) -> Option<RemoteObj> {
143 RemoteObj::try_new(FromRemoteStub(Box::new(self)))
144 }
145 }
146