Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | //! Trait definitions for binder objects |
| 18 | |
Stephen Crane | ddb3e6d | 2020-12-18 13:27:22 -0800 | [diff] [blame] | 19 | use crate::error::{status_t, Result, StatusCode}; |
Matthew Maurer | e268a9f | 2022-07-26 09:31:30 -0700 | [diff] [blame] | 20 | use crate::parcel::{BorrowedParcel, Parcel}; |
Stephen Crane | ddb3e6d | 2020-12-18 13:27:22 -0800 | [diff] [blame] | 21 | use crate::proxy::{DeathRecipient, SpIBinder, WpIBinder}; |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 22 | use crate::sys; |
| 23 | |
Stephen Crane | ddb3e6d | 2020-12-18 13:27:22 -0800 | [diff] [blame] | 24 | use std::borrow::Borrow; |
| 25 | use std::cmp::Ordering; |
Andrei Homescu | ee132fa | 2021-09-03 02:36:17 +0000 | [diff] [blame] | 26 | use std::convert::TryFrom; |
Stephen Crane | 669deb6 | 2020-09-10 17:31:39 -0700 | [diff] [blame] | 27 | use std::ffi::{c_void, CStr, CString}; |
Stephen Crane | ddb3e6d | 2020-12-18 13:27:22 -0800 | [diff] [blame] | 28 | use std::fmt; |
Stephen Crane | 2a3297f | 2021-06-11 16:48:10 -0700 | [diff] [blame] | 29 | use std::fs::File; |
Stephen Crane | ddb3e6d | 2020-12-18 13:27:22 -0800 | [diff] [blame] | 30 | use std::marker::PhantomData; |
| 31 | use std::ops::Deref; |
Stephen Crane | 669deb6 | 2020-09-10 17:31:39 -0700 | [diff] [blame] | 32 | use std::os::raw::c_char; |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 33 | use std::os::unix::io::AsRawFd; |
| 34 | use std::ptr; |
| 35 | |
| 36 | /// Binder action to perform. |
| 37 | /// |
Andrew Walbran | 12400d8 | 2021-03-04 17:04:34 +0000 | [diff] [blame] | 38 | /// This must be a number between [`FIRST_CALL_TRANSACTION`] and |
| 39 | /// [`LAST_CALL_TRANSACTION`]. |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 40 | pub type TransactionCode = u32; |
| 41 | |
| 42 | /// Additional operation flags. |
| 43 | /// |
Andrew Walbran | 12400d8 | 2021-03-04 17:04:34 +0000 | [diff] [blame] | 44 | /// `FLAG_*` values. |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 45 | pub type TransactionFlags = u32; |
| 46 | |
| 47 | /// Super-trait for Binder interfaces. |
| 48 | /// |
| 49 | /// This trait allows conversion of a Binder interface trait object into an |
| 50 | /// IBinder object for IPC calls. All Binder remotable interface (i.e. AIDL |
| 51 | /// interfaces) must implement this trait. |
| 52 | /// |
| 53 | /// This is equivalent `IInterface` in C++. |
Stephen Crane | f03fe3d | 2021-06-25 15:05:00 -0700 | [diff] [blame] | 54 | pub trait Interface: Send + Sync { |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 55 | /// Convert this binder object into a generic [`SpIBinder`] reference. |
| 56 | fn as_binder(&self) -> SpIBinder { |
| 57 | panic!("This object was not a Binder object and cannot be converted into an SpIBinder.") |
| 58 | } |
Stephen Crane | 2a3297f | 2021-06-11 16:48:10 -0700 | [diff] [blame] | 59 | |
| 60 | /// Dump transaction handler for this Binder object. |
| 61 | /// |
| 62 | /// This handler is a no-op by default and should be implemented for each |
| 63 | /// Binder service struct that wishes to respond to dump transactions. |
| 64 | fn dump(&self, _file: &File, _args: &[&CStr]) -> Result<()> { |
| 65 | Ok(()) |
| 66 | } |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Alice Ryhl | c173684 | 2021-11-23 12:38:51 +0000 | [diff] [blame] | 69 | /// Implemented by sync interfaces to specify what the associated async interface is. |
| 70 | /// Generic to handle the fact that async interfaces are generic over a thread pool. |
| 71 | /// |
| 72 | /// The binder in any object implementing this trait should be compatible with the |
| 73 | /// `Target` associated type, and using `FromIBinder` to convert it to the target |
| 74 | /// should not fail. |
| 75 | pub trait ToAsyncInterface<P> |
| 76 | where |
| 77 | Self: Interface, |
| 78 | Self::Target: FromIBinder, |
| 79 | { |
| 80 | /// The async interface associated with this sync interface. |
| 81 | type Target: ?Sized; |
| 82 | } |
| 83 | |
| 84 | /// Implemented by async interfaces to specify what the associated sync interface is. |
| 85 | /// |
| 86 | /// The binder in any object implementing this trait should be compatible with the |
| 87 | /// `Target` associated type, and using `FromIBinder` to convert it to the target |
| 88 | /// should not fail. |
| 89 | pub trait ToSyncInterface |
| 90 | where |
| 91 | Self: Interface, |
| 92 | Self::Target: FromIBinder, |
| 93 | { |
| 94 | /// The sync interface associated with this async interface. |
| 95 | type Target: ?Sized; |
| 96 | } |
| 97 | |
Stephen Crane | ff7f03a | 2021-02-25 16:04:22 -0800 | [diff] [blame] | 98 | /// Interface stability promise |
| 99 | /// |
| 100 | /// An interface can promise to be a stable vendor interface ([`Vintf`]), or |
| 101 | /// makes no stability guarantees ([`Local`]). [`Local`] is |
| 102 | /// currently the default stability. |
Charisee | ab53d0a | 2023-03-03 02:08:34 +0000 | [diff] [blame] | 103 | #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Default)] |
Stephen Crane | ff7f03a | 2021-02-25 16:04:22 -0800 | [diff] [blame] | 104 | pub enum Stability { |
| 105 | /// Default stability, visible to other modules in the same compilation |
| 106 | /// context (e.g. modules on system.img) |
Charisee | ab53d0a | 2023-03-03 02:08:34 +0000 | [diff] [blame] | 107 | #[default] |
Stephen Crane | ff7f03a | 2021-02-25 16:04:22 -0800 | [diff] [blame] | 108 | Local, |
| 109 | |
| 110 | /// A Vendor Interface Object, which promises to be stable |
| 111 | Vintf, |
| 112 | } |
| 113 | |
Andrei Homescu | ee132fa | 2021-09-03 02:36:17 +0000 | [diff] [blame] | 114 | impl From<Stability> for i32 { |
| 115 | fn from(stability: Stability) -> i32 { |
| 116 | use Stability::*; |
| 117 | match stability { |
| 118 | Local => 0, |
| 119 | Vintf => 1, |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | impl TryFrom<i32> for Stability { |
| 125 | type Error = StatusCode; |
| 126 | fn try_from(stability: i32) -> Result<Stability> { |
| 127 | use Stability::*; |
| 128 | match stability { |
| 129 | 0 => Ok(Local), |
| 130 | 1 => Ok(Vintf), |
Matthew Maurer | e268a9f | 2022-07-26 09:31:30 -0700 | [diff] [blame] | 131 | _ => Err(StatusCode::BAD_VALUE), |
Andrei Homescu | ee132fa | 2021-09-03 02:36:17 +0000 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 136 | /// A local service that can be remotable via Binder. |
| 137 | /// |
| 138 | /// An object that implement this interface made be made into a Binder service |
| 139 | /// via `Binder::new(object)`. |
| 140 | /// |
| 141 | /// This is a low-level interface that should normally be automatically |
| 142 | /// generated from AIDL via the [`declare_binder_interface!`] macro. When using |
| 143 | /// the AIDL backend, users need only implement the high-level AIDL-defined |
| 144 | /// interface. The AIDL compiler then generates a container struct that wraps |
| 145 | /// the user-defined service and implements `Remotable`. |
Andrei Homescu | 2c674b0 | 2020-08-07 22:12:27 -0700 | [diff] [blame] | 146 | pub trait Remotable: Send + Sync { |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 147 | /// The Binder interface descriptor string. |
| 148 | /// |
| 149 | /// This string is a unique identifier for a Binder interface, and should be |
| 150 | /// the same between all implementations of that interface. |
| 151 | fn get_descriptor() -> &'static str; |
| 152 | |
| 153 | /// Handle and reply to a request to invoke a transaction on this object. |
| 154 | /// |
| 155 | /// `reply` may be [`None`] if the sender does not expect a reply. |
Matthew Maurer | e268a9f | 2022-07-26 09:31:30 -0700 | [diff] [blame] | 156 | fn on_transact( |
| 157 | &self, |
| 158 | code: TransactionCode, |
| 159 | data: &BorrowedParcel<'_>, |
| 160 | reply: &mut BorrowedParcel<'_>, |
| 161 | ) -> Result<()>; |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 162 | |
Stephen Crane | 2a3297f | 2021-06-11 16:48:10 -0700 | [diff] [blame] | 163 | /// Handle a request to invoke the dump transaction on this |
| 164 | /// object. |
| 165 | fn on_dump(&self, file: &File, args: &[&CStr]) -> Result<()>; |
| 166 | |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 167 | /// Retrieve the class of this remote object. |
| 168 | /// |
| 169 | /// This method should always return the same InterfaceClass for the same |
| 170 | /// type. |
| 171 | fn get_class() -> InterfaceClass; |
| 172 | } |
| 173 | |
Andrew Walbran | 12400d8 | 2021-03-04 17:04:34 +0000 | [diff] [blame] | 174 | /// First transaction code available for user commands (inclusive) |
| 175 | pub const FIRST_CALL_TRANSACTION: TransactionCode = sys::FIRST_CALL_TRANSACTION; |
| 176 | /// Last transaction code available for user commands (inclusive) |
| 177 | pub const LAST_CALL_TRANSACTION: TransactionCode = sys::LAST_CALL_TRANSACTION; |
| 178 | |
| 179 | /// Corresponds to TF_ONE_WAY -- an asynchronous call. |
| 180 | pub const FLAG_ONEWAY: TransactionFlags = sys::FLAG_ONEWAY; |
| 181 | /// Corresponds to TF_CLEAR_BUF -- clear transaction buffers after call is made. |
| 182 | pub const FLAG_CLEAR_BUF: TransactionFlags = sys::FLAG_CLEAR_BUF; |
Stephen Crane | ff7f03a | 2021-02-25 16:04:22 -0800 | [diff] [blame] | 183 | /// Set to the vendor flag if we are building for the VNDK, 0 otherwise |
| 184 | pub const FLAG_PRIVATE_LOCAL: TransactionFlags = sys::FLAG_PRIVATE_LOCAL; |
Andrew Walbran | 12400d8 | 2021-03-04 17:04:34 +0000 | [diff] [blame] | 185 | |
| 186 | /// Internal interface of binder local or remote objects for making |
| 187 | /// transactions. |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 188 | /// |
Andrew Walbran | 12400d8 | 2021-03-04 17:04:34 +0000 | [diff] [blame] | 189 | /// This trait corresponds to the parts of the interface of the C++ `IBinder` |
| 190 | /// class which are internal implementation details. |
| 191 | pub trait IBinderInternal: IBinder { |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 192 | /// Is this object still alive? |
| 193 | fn is_binder_alive(&self) -> bool; |
| 194 | |
Janis Danisevskis | 798a09a | 2020-08-18 08:35:38 -0700 | [diff] [blame] | 195 | /// Indicate that the service intends to receive caller security contexts. |
Janis Danisevskis | 1323d51 | 2021-11-09 07:48:08 -0800 | [diff] [blame] | 196 | #[cfg(not(android_vndk))] |
Janis Danisevskis | 798a09a | 2020-08-18 08:35:38 -0700 | [diff] [blame] | 197 | fn set_requesting_sid(&mut self, enable: bool); |
| 198 | |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 199 | /// Dump this object to the given file handle |
| 200 | fn dump<F: AsRawFd>(&mut self, fp: &F, args: &[&str]) -> Result<()>; |
| 201 | |
| 202 | /// Get a new interface that exposes additional extension functionality, if |
| 203 | /// available. |
| 204 | fn get_extension(&mut self) -> Result<Option<SpIBinder>>; |
| 205 | |
Alice Ryhl | feba6ca | 2021-08-19 10:47:04 +0000 | [diff] [blame] | 206 | /// Create a Parcel that can be used with `submit_transact`. |
Alice Ryhl | 8618c48 | 2021-11-09 15:35:35 +0000 | [diff] [blame] | 207 | fn prepare_transact(&self) -> Result<Parcel>; |
Alice Ryhl | feba6ca | 2021-08-19 10:47:04 +0000 | [diff] [blame] | 208 | |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 209 | /// Perform a generic operation with the object. |
| 210 | /// |
Alice Ryhl | 8618c48 | 2021-11-09 15:35:35 +0000 | [diff] [blame] | 211 | /// The provided [`Parcel`] must have been created by a call to |
Alice Ryhl | feba6ca | 2021-08-19 10:47:04 +0000 | [diff] [blame] | 212 | /// `prepare_transact` on the same binder. |
| 213 | /// |
| 214 | /// # Arguments |
| 215 | /// |
| 216 | /// * `code` - Transaction code for the operation. |
Alice Ryhl | 8618c48 | 2021-11-09 15:35:35 +0000 | [diff] [blame] | 217 | /// * `data` - [`Parcel`] with input data. |
Alice Ryhl | feba6ca | 2021-08-19 10:47:04 +0000 | [diff] [blame] | 218 | /// * `flags` - Transaction flags, e.g. marking the transaction as |
| 219 | /// asynchronous ([`FLAG_ONEWAY`](FLAG_ONEWAY)). |
| 220 | fn submit_transact( |
| 221 | &self, |
| 222 | code: TransactionCode, |
Alice Ryhl | 8618c48 | 2021-11-09 15:35:35 +0000 | [diff] [blame] | 223 | data: Parcel, |
Alice Ryhl | feba6ca | 2021-08-19 10:47:04 +0000 | [diff] [blame] | 224 | flags: TransactionFlags, |
Alice Ryhl | 8618c48 | 2021-11-09 15:35:35 +0000 | [diff] [blame] | 225 | ) -> Result<Parcel>; |
Alice Ryhl | feba6ca | 2021-08-19 10:47:04 +0000 | [diff] [blame] | 226 | |
| 227 | /// Perform a generic operation with the object. This is a convenience |
| 228 | /// method that internally calls `prepare_transact` followed by |
| 229 | /// `submit_transact. |
| 230 | /// |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 231 | /// # Arguments |
| 232 | /// * `code` - Transaction code for the operation |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 233 | /// * `flags` - Transaction flags, e.g. marking the transaction as |
Andrew Walbran | 12400d8 | 2021-03-04 17:04:34 +0000 | [diff] [blame] | 234 | /// asynchronous ([`FLAG_ONEWAY`](FLAG_ONEWAY)) |
Alice Ryhl | feba6ca | 2021-08-19 10:47:04 +0000 | [diff] [blame] | 235 | /// * `input_callback` A callback for building the `Parcel`. |
Alice Ryhl | 8618c48 | 2021-11-09 15:35:35 +0000 | [diff] [blame] | 236 | fn transact<F: FnOnce(BorrowedParcel<'_>) -> Result<()>>( |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 237 | &self, |
| 238 | code: TransactionCode, |
| 239 | flags: TransactionFlags, |
| 240 | input_callback: F, |
Alice Ryhl | feba6ca | 2021-08-19 10:47:04 +0000 | [diff] [blame] | 241 | ) -> Result<Parcel> { |
| 242 | let mut parcel = self.prepare_transact()?; |
Alice Ryhl | 8618c48 | 2021-11-09 15:35:35 +0000 | [diff] [blame] | 243 | input_callback(parcel.borrowed())?; |
| 244 | self.submit_transact(code, parcel, flags) |
Alice Ryhl | feba6ca | 2021-08-19 10:47:04 +0000 | [diff] [blame] | 245 | } |
Andrew Walbran | 12400d8 | 2021-03-04 17:04:34 +0000 | [diff] [blame] | 246 | } |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 247 | |
Andrew Walbran | 12400d8 | 2021-03-04 17:04:34 +0000 | [diff] [blame] | 248 | /// Interface of binder local or remote objects. |
| 249 | /// |
| 250 | /// This trait corresponds to the parts of the interface of the C++ `IBinder` |
| 251 | /// class which are public. |
| 252 | pub trait IBinder { |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 253 | /// Register the recipient for a notification if this binder |
| 254 | /// goes away. If this binder object unexpectedly goes away |
| 255 | /// (typically because its hosting process has been killed), |
Andrew Walbran | 12400d8 | 2021-03-04 17:04:34 +0000 | [diff] [blame] | 256 | /// then the `DeathRecipient`'s callback will be called. |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 257 | /// |
| 258 | /// You will only receive death notifications for remote binders, |
| 259 | /// as local binders by definition can't die without you dying as well. |
| 260 | /// Trying to use this function on a local binder will result in an |
| 261 | /// INVALID_OPERATION code being returned and nothing happening. |
| 262 | /// |
Andrew Walbran | fee5863 | 2023-06-22 17:36:48 +0000 | [diff] [blame] | 263 | /// This link only holds a weak reference to its recipient. If the |
| 264 | /// `DeathRecipient` is dropped then it will be unlinked. |
| 265 | /// |
| 266 | /// Note that the notifications won't work if you don't first start at least |
| 267 | /// one Binder thread by calling |
| 268 | /// [`ProcessState::start_thread_pool`](crate::ProcessState::start_thread_pool) |
| 269 | /// or |
| 270 | /// [`ProcessState::join_thread_pool`](crate::ProcessState::join_thread_pool). |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 271 | fn link_to_death(&mut self, recipient: &mut DeathRecipient) -> Result<()>; |
| 272 | |
| 273 | /// Remove a previously registered death notification. |
| 274 | /// The recipient will no longer be called if this object |
| 275 | /// dies. |
| 276 | fn unlink_to_death(&mut self, recipient: &mut DeathRecipient) -> Result<()>; |
Stephen Crane | 61366d4 | 2022-01-20 17:45:34 -0800 | [diff] [blame] | 277 | |
| 278 | /// Send a ping transaction to this object |
| 279 | fn ping_binder(&mut self) -> Result<()>; |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | /// Opaque reference to the type of a Binder interface. |
| 283 | /// |
| 284 | /// This object encapsulates the Binder interface descriptor string, along with |
| 285 | /// the binder transaction callback, if the class describes a local service. |
| 286 | /// |
| 287 | /// A Binder remotable object may only have a single interface class, and any |
| 288 | /// given object can only be associated with one class. Two objects with |
| 289 | /// different classes are incompatible, even if both classes have the same |
| 290 | /// interface descriptor. |
| 291 | #[derive(Copy, Clone, PartialEq, Eq)] |
| 292 | pub struct InterfaceClass(*const sys::AIBinder_Class); |
| 293 | |
| 294 | impl InterfaceClass { |
| 295 | /// Get a Binder NDK `AIBinder_Class` pointer for this object type. |
| 296 | /// |
| 297 | /// Note: the returned pointer will not be constant. Calling this method |
| 298 | /// multiple times for the same type will result in distinct class |
| 299 | /// pointers. A static getter for this value is implemented in |
| 300 | /// [`declare_binder_interface!`]. |
| 301 | pub fn new<I: InterfaceClassMethods>() -> InterfaceClass { |
| 302 | let descriptor = CString::new(I::get_descriptor()).unwrap(); |
Andrew Walbran | 2f3ff9f | 2023-07-07 16:58:13 +0100 | [diff] [blame^] | 303 | // Safety: `AIBinder_Class_define` expects a valid C string, and three |
| 304 | // valid callback functions, all non-null pointers. The C string is |
| 305 | // copied and need not be valid for longer than the call, so we can drop |
| 306 | // it after the call. We can safely assign null to the onDump and |
| 307 | // handleShellCommand callbacks as long as the class pointer was |
| 308 | // non-null. Rust None for a Option<fn> is guaranteed to be a NULL |
| 309 | // pointer. Rust retains ownership of the pointer after it is defined. |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 310 | let ptr = unsafe { |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 311 | let class = sys::AIBinder_Class_define( |
| 312 | descriptor.as_ptr(), |
| 313 | Some(I::on_create), |
| 314 | Some(I::on_destroy), |
| 315 | Some(I::on_transact), |
| 316 | ); |
| 317 | if class.is_null() { |
| 318 | panic!("Expected non-null class pointer from AIBinder_Class_define!"); |
| 319 | } |
Stephen Crane | 2a3297f | 2021-06-11 16:48:10 -0700 | [diff] [blame] | 320 | sys::AIBinder_Class_setOnDump(class, Some(I::on_dump)); |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 321 | sys::AIBinder_Class_setHandleShellCommand(class, None); |
| 322 | class |
| 323 | }; |
| 324 | InterfaceClass(ptr) |
| 325 | } |
| 326 | |
| 327 | /// Construct an `InterfaceClass` out of a raw, non-null `AIBinder_Class` |
| 328 | /// pointer. |
| 329 | /// |
| 330 | /// # Safety |
| 331 | /// |
| 332 | /// This function is safe iff `ptr` is a valid, non-null pointer to an |
| 333 | /// `AIBinder_Class`. |
| 334 | pub(crate) unsafe fn from_ptr(ptr: *const sys::AIBinder_Class) -> InterfaceClass { |
| 335 | InterfaceClass(ptr) |
| 336 | } |
Stephen Crane | 669deb6 | 2020-09-10 17:31:39 -0700 | [diff] [blame] | 337 | |
| 338 | /// Get the interface descriptor string of this class. |
| 339 | pub fn get_descriptor(&self) -> String { |
Andrew Walbran | 2f3ff9f | 2023-07-07 16:58:13 +0100 | [diff] [blame^] | 340 | // SAFETY: The descriptor returned by AIBinder_Class_getDescriptor is |
| 341 | // always a two-byte null terminated sequence of u16s. Thus, we can |
| 342 | // continue reading from the pointer until we hit a null value, and this |
| 343 | // pointer can be a valid slice if the slice length is <= the number of |
| 344 | // u16 elements before the null terminator. |
Stephen Crane | 669deb6 | 2020-09-10 17:31:39 -0700 | [diff] [blame] | 345 | unsafe { |
Stephen Crane | 669deb6 | 2020-09-10 17:31:39 -0700 | [diff] [blame] | 346 | let raw_descriptor: *const c_char = sys::AIBinder_Class_getDescriptor(self.0); |
Andrew Walbran | 12400d8 | 2021-03-04 17:04:34 +0000 | [diff] [blame] | 347 | CStr::from_ptr(raw_descriptor) |
| 348 | .to_str() |
Stephen Crane | 669deb6 | 2020-09-10 17:31:39 -0700 | [diff] [blame] | 349 | .expect("Expected valid UTF-8 string from AIBinder_Class_getDescriptor") |
| 350 | .into() |
| 351 | } |
| 352 | } |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | impl From<InterfaceClass> for *const sys::AIBinder_Class { |
| 356 | fn from(class: InterfaceClass) -> *const sys::AIBinder_Class { |
| 357 | class.0 |
| 358 | } |
| 359 | } |
| 360 | |
Stephen Crane | ddb3e6d | 2020-12-18 13:27:22 -0800 | [diff] [blame] | 361 | /// Strong reference to a binder object |
| 362 | pub struct Strong<I: FromIBinder + ?Sized>(Box<I>); |
| 363 | |
| 364 | impl<I: FromIBinder + ?Sized> Strong<I> { |
| 365 | /// Create a new strong reference to the provided binder object |
| 366 | pub fn new(binder: Box<I>) -> Self { |
| 367 | Self(binder) |
| 368 | } |
| 369 | |
| 370 | /// Construct a new weak reference to this binder |
| 371 | pub fn downgrade(this: &Strong<I>) -> Weak<I> { |
| 372 | Weak::new(this) |
| 373 | } |
Alice Ryhl | c173684 | 2021-11-23 12:38:51 +0000 | [diff] [blame] | 374 | |
| 375 | /// Convert this synchronous binder handle into an asynchronous one. |
| 376 | pub fn into_async<P>(self) -> Strong<<I as ToAsyncInterface<P>>::Target> |
| 377 | where |
| 378 | I: ToAsyncInterface<P>, |
| 379 | { |
| 380 | // By implementing the ToAsyncInterface trait, it is guaranteed that the binder |
| 381 | // object is also valid for the target type. |
| 382 | FromIBinder::try_from(self.0.as_binder()).unwrap() |
| 383 | } |
| 384 | |
| 385 | /// Convert this asynchronous binder handle into a synchronous one. |
| 386 | pub fn into_sync(self) -> Strong<<I as ToSyncInterface>::Target> |
| 387 | where |
| 388 | I: ToSyncInterface, |
| 389 | { |
| 390 | // By implementing the ToSyncInterface trait, it is guaranteed that the binder |
| 391 | // object is also valid for the target type. |
| 392 | FromIBinder::try_from(self.0.as_binder()).unwrap() |
| 393 | } |
Stephen Crane | ddb3e6d | 2020-12-18 13:27:22 -0800 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | impl<I: FromIBinder + ?Sized> Clone for Strong<I> { |
| 397 | fn clone(&self) -> Self { |
| 398 | // Since we hold a strong reference, we should always be able to create |
| 399 | // a new strong reference to the same interface type, so try_from() |
| 400 | // should never fail here. |
| 401 | FromIBinder::try_from(self.0.as_binder()).unwrap() |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | impl<I: FromIBinder + ?Sized> Borrow<I> for Strong<I> { |
| 406 | fn borrow(&self) -> &I { |
| 407 | &self.0 |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | impl<I: FromIBinder + ?Sized> AsRef<I> for Strong<I> { |
| 412 | fn as_ref(&self) -> &I { |
| 413 | &self.0 |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | impl<I: FromIBinder + ?Sized> Deref for Strong<I> { |
| 418 | type Target = I; |
| 419 | |
| 420 | fn deref(&self) -> &Self::Target { |
| 421 | &self.0 |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | impl<I: FromIBinder + fmt::Debug + ?Sized> fmt::Debug for Strong<I> { |
| 426 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 427 | fmt::Debug::fmt(&**self, f) |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | impl<I: FromIBinder + ?Sized> Ord for Strong<I> { |
| 432 | fn cmp(&self, other: &Self) -> Ordering { |
| 433 | self.0.as_binder().cmp(&other.0.as_binder()) |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | impl<I: FromIBinder + ?Sized> PartialOrd for Strong<I> { |
| 438 | fn partial_cmp(&self, other: &Self) -> Option<Ordering> { |
| 439 | self.0.as_binder().partial_cmp(&other.0.as_binder()) |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | impl<I: FromIBinder + ?Sized> PartialEq for Strong<I> { |
| 444 | fn eq(&self, other: &Self) -> bool { |
| 445 | self.0.as_binder().eq(&other.0.as_binder()) |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | impl<I: FromIBinder + ?Sized> Eq for Strong<I> {} |
| 450 | |
| 451 | /// Weak reference to a binder object |
| 452 | #[derive(Debug)] |
| 453 | pub struct Weak<I: FromIBinder + ?Sized> { |
| 454 | weak_binder: WpIBinder, |
| 455 | interface_type: PhantomData<I>, |
| 456 | } |
| 457 | |
| 458 | impl<I: FromIBinder + ?Sized> Weak<I> { |
| 459 | /// Construct a new weak reference from a strong reference |
| 460 | fn new(binder: &Strong<I>) -> Self { |
| 461 | let weak_binder = binder.as_binder().downgrade(); |
Matthew Maurer | e268a9f | 2022-07-26 09:31:30 -0700 | [diff] [blame] | 462 | Weak { weak_binder, interface_type: PhantomData } |
Stephen Crane | ddb3e6d | 2020-12-18 13:27:22 -0800 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | /// Upgrade this weak reference to a strong reference if the binder object |
| 466 | /// is still alive |
| 467 | pub fn upgrade(&self) -> Result<Strong<I>> { |
Matthew Maurer | e268a9f | 2022-07-26 09:31:30 -0700 | [diff] [blame] | 468 | self.weak_binder.promote().ok_or(StatusCode::DEAD_OBJECT).and_then(FromIBinder::try_from) |
Stephen Crane | ddb3e6d | 2020-12-18 13:27:22 -0800 | [diff] [blame] | 469 | } |
| 470 | } |
| 471 | |
| 472 | impl<I: FromIBinder + ?Sized> Clone for Weak<I> { |
| 473 | fn clone(&self) -> Self { |
Matthew Maurer | e268a9f | 2022-07-26 09:31:30 -0700 | [diff] [blame] | 474 | Self { weak_binder: self.weak_binder.clone(), interface_type: PhantomData } |
Stephen Crane | ddb3e6d | 2020-12-18 13:27:22 -0800 | [diff] [blame] | 475 | } |
| 476 | } |
| 477 | |
| 478 | impl<I: FromIBinder + ?Sized> Ord for Weak<I> { |
| 479 | fn cmp(&self, other: &Self) -> Ordering { |
| 480 | self.weak_binder.cmp(&other.weak_binder) |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | impl<I: FromIBinder + ?Sized> PartialOrd for Weak<I> { |
| 485 | fn partial_cmp(&self, other: &Self) -> Option<Ordering> { |
| 486 | self.weak_binder.partial_cmp(&other.weak_binder) |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | impl<I: FromIBinder + ?Sized> PartialEq for Weak<I> { |
| 491 | fn eq(&self, other: &Self) -> bool { |
| 492 | self.weak_binder == other.weak_binder |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | impl<I: FromIBinder + ?Sized> Eq for Weak<I> {} |
| 497 | |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 498 | /// Create a function implementing a static getter for an interface class. |
| 499 | /// |
| 500 | /// Each binder interface (i.e. local [`Remotable`] service or remote proxy |
| 501 | /// [`Interface`]) must have global, static class that uniquely identifies |
| 502 | /// it. This macro implements an [`InterfaceClass`] getter to simplify these |
| 503 | /// implementations. |
| 504 | /// |
| 505 | /// The type of a structure that implements [`InterfaceClassMethods`] must be |
| 506 | /// passed to this macro. For local services, this should be `Binder<Self>` |
| 507 | /// since [`Binder`] implements [`InterfaceClassMethods`]. |
| 508 | /// |
| 509 | /// # Examples |
| 510 | /// |
| 511 | /// When implementing a local [`Remotable`] service `ExampleService`, the |
| 512 | /// `get_class` method is required in the [`Remotable`] impl block. This macro |
| 513 | /// should be used as follows to implement this functionality: |
| 514 | /// |
| 515 | /// ```rust |
| 516 | /// impl Remotable for ExampleService { |
| 517 | /// fn get_descriptor() -> &'static str { |
| 518 | /// "android.os.IExampleInterface" |
| 519 | /// } |
| 520 | /// |
| 521 | /// fn on_transact( |
| 522 | /// &self, |
| 523 | /// code: TransactionCode, |
Alice Ryhl | 8618c48 | 2021-11-09 15:35:35 +0000 | [diff] [blame] | 524 | /// data: &BorrowedParcel, |
| 525 | /// reply: &mut BorrowedParcel, |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 526 | /// ) -> Result<()> { |
| 527 | /// // ... |
| 528 | /// } |
| 529 | /// |
| 530 | /// binder_fn_get_class!(Binder<Self>); |
| 531 | /// } |
| 532 | /// ``` |
| 533 | macro_rules! binder_fn_get_class { |
| 534 | ($class:ty) => { |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 535 | binder_fn_get_class!($crate::binder_impl::InterfaceClass::new::<$class>()); |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 536 | }; |
| 537 | |
| 538 | ($constructor:expr) => { |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 539 | fn get_class() -> $crate::binder_impl::InterfaceClass { |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 540 | static CLASS_INIT: std::sync::Once = std::sync::Once::new(); |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 541 | static mut CLASS: Option<$crate::binder_impl::InterfaceClass> = None; |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 542 | |
Andrew Walbran | 2f3ff9f | 2023-07-07 16:58:13 +0100 | [diff] [blame^] | 543 | // Safety: This assignment is guarded by the `CLASS_INIT` `Once` |
| 544 | // variable, and therefore is thread-safe, as it can only occur |
| 545 | // once. |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 546 | CLASS_INIT.call_once(|| unsafe { |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 547 | CLASS = Some($constructor); |
| 548 | }); |
Andrew Walbran | 2f3ff9f | 2023-07-07 16:58:13 +0100 | [diff] [blame^] | 549 | // Safety: The `CLASS` variable can only be mutated once, above, and |
| 550 | // is subsequently safe to read from any thread. |
| 551 | unsafe { CLASS.unwrap() } |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 552 | } |
| 553 | }; |
| 554 | } |
| 555 | |
| 556 | pub trait InterfaceClassMethods { |
| 557 | /// Get the interface descriptor string for this object type. |
| 558 | fn get_descriptor() -> &'static str |
| 559 | where |
| 560 | Self: Sized; |
| 561 | |
| 562 | /// Called during construction of a new `AIBinder` object of this interface |
| 563 | /// class. |
| 564 | /// |
| 565 | /// The opaque pointer parameter will be the parameter provided to |
| 566 | /// `AIBinder_new`. Returns an opaque userdata to be associated with the new |
| 567 | /// `AIBinder` object. |
| 568 | /// |
| 569 | /// # Safety |
| 570 | /// |
| 571 | /// Callback called from C++. The parameter argument provided to |
| 572 | /// `AIBinder_new` must match the type expected here. The `AIBinder` object |
| 573 | /// will take ownership of the returned pointer, which it will free via |
| 574 | /// `on_destroy`. |
| 575 | unsafe extern "C" fn on_create(args: *mut c_void) -> *mut c_void; |
| 576 | |
| 577 | /// Called when a transaction needs to be processed by the local service |
| 578 | /// implementation. |
| 579 | /// |
| 580 | /// # Safety |
| 581 | /// |
| 582 | /// Callback called from C++. The `binder` parameter must be a valid pointer |
| 583 | /// to a binder object of this class with userdata initialized via this |
| 584 | /// class's `on_create`. The parcel parameters must be valid pointers to |
| 585 | /// parcel objects. |
| 586 | unsafe extern "C" fn on_transact( |
| 587 | binder: *mut sys::AIBinder, |
| 588 | code: u32, |
| 589 | data: *const sys::AParcel, |
| 590 | reply: *mut sys::AParcel, |
| 591 | ) -> status_t; |
| 592 | |
| 593 | /// Called whenever an `AIBinder` object is no longer referenced and needs |
| 594 | /// to be destroyed. |
| 595 | /// |
| 596 | /// # Safety |
| 597 | /// |
| 598 | /// Callback called from C++. The opaque pointer parameter must be the value |
| 599 | /// returned by `on_create` for this class. This function takes ownership of |
| 600 | /// the provided pointer and destroys it. |
| 601 | unsafe extern "C" fn on_destroy(object: *mut c_void); |
Stephen Crane | 2a3297f | 2021-06-11 16:48:10 -0700 | [diff] [blame] | 602 | |
| 603 | /// Called to handle the `dump` transaction. |
| 604 | /// |
| 605 | /// # Safety |
| 606 | /// |
| 607 | /// Must be called with a non-null, valid pointer to a local `AIBinder` that |
| 608 | /// contains a `T` pointer in its user data. fd should be a non-owned file |
| 609 | /// descriptor, and args must be an array of null-terminated string |
| 610 | /// poiinters with length num_args. |
Matthew Maurer | e268a9f | 2022-07-26 09:31:30 -0700 | [diff] [blame] | 611 | unsafe extern "C" fn on_dump( |
| 612 | binder: *mut sys::AIBinder, |
| 613 | fd: i32, |
| 614 | args: *mut *const c_char, |
| 615 | num_args: u32, |
| 616 | ) -> status_t; |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | /// Interface for transforming a generic SpIBinder into a specific remote |
| 620 | /// interface trait. |
| 621 | /// |
| 622 | /// # Example |
| 623 | /// |
| 624 | /// For Binder interface `IFoo`, the following implementation should be made: |
| 625 | /// ```no_run |
| 626 | /// # use binder::{FromIBinder, SpIBinder, Result}; |
| 627 | /// # trait IFoo {} |
| 628 | /// impl FromIBinder for dyn IFoo { |
| 629 | /// fn try_from(ibinder: SpIBinder) -> Result<Box<Self>> { |
| 630 | /// // ... |
| 631 | /// # Err(binder::StatusCode::OK) |
| 632 | /// } |
| 633 | /// } |
| 634 | /// ``` |
Stephen Crane | ddb3e6d | 2020-12-18 13:27:22 -0800 | [diff] [blame] | 635 | pub trait FromIBinder: Interface { |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 636 | /// Try to interpret a generic Binder object as this interface. |
| 637 | /// |
| 638 | /// Returns a trait object for the `Self` interface if this object |
| 639 | /// implements that interface. |
Stephen Crane | ddb3e6d | 2020-12-18 13:27:22 -0800 | [diff] [blame] | 640 | fn try_from(ibinder: SpIBinder) -> Result<Strong<Self>>; |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | /// Trait for transparent Rust wrappers around android C++ native types. |
| 644 | /// |
| 645 | /// The pointer return by this trait's methods should be immediately passed to |
| 646 | /// C++ and not stored by Rust. The pointer is valid only as long as the |
| 647 | /// underlying C++ object is alive, so users must be careful to take this into |
| 648 | /// account, as Rust cannot enforce this. |
| 649 | /// |
| 650 | /// # Safety |
| 651 | /// |
| 652 | /// For this trait to be a correct implementation, `T` must be a valid android |
| 653 | /// C++ type. Since we cannot constrain this via the type system, this trait is |
| 654 | /// marked as unsafe. |
| 655 | pub unsafe trait AsNative<T> { |
| 656 | /// Return a pointer to the native version of `self` |
| 657 | fn as_native(&self) -> *const T; |
| 658 | |
| 659 | /// Return a mutable pointer to the native version of `self` |
| 660 | fn as_native_mut(&mut self) -> *mut T; |
| 661 | } |
| 662 | |
Andrew Walbran | 2f3ff9f | 2023-07-07 16:58:13 +0100 | [diff] [blame^] | 663 | // Safety: If V is a valid Android C++ type then we can either use that or a |
| 664 | // null pointer. |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 665 | unsafe impl<T, V: AsNative<T>> AsNative<T> for Option<V> { |
| 666 | fn as_native(&self) -> *const T { |
| 667 | self.as_ref().map_or(ptr::null(), |v| v.as_native()) |
| 668 | } |
| 669 | |
| 670 | fn as_native_mut(&mut self) -> *mut T { |
| 671 | self.as_mut().map_or(ptr::null_mut(), |v| v.as_native_mut()) |
| 672 | } |
| 673 | } |
| 674 | |
Andrew Walbran | 88eca4f | 2021-04-13 14:26:01 +0000 | [diff] [blame] | 675 | /// The features to enable when creating a native Binder. |
| 676 | /// |
| 677 | /// This should always be initialised with a default value, e.g.: |
| 678 | /// ``` |
| 679 | /// # use binder::BinderFeatures; |
| 680 | /// BinderFeatures { |
| 681 | /// set_requesting_sid: true, |
| 682 | /// ..BinderFeatures::default(), |
| 683 | /// } |
| 684 | /// ``` |
| 685 | #[derive(Clone, Debug, Default, Eq, PartialEq)] |
| 686 | pub struct BinderFeatures { |
| 687 | /// Indicates that the service intends to receive caller security contexts. This must be true |
| 688 | /// for `ThreadState::with_calling_sid` to work. |
Janis Danisevskis | 1323d51 | 2021-11-09 07:48:08 -0800 | [diff] [blame] | 689 | #[cfg(not(android_vndk))] |
Andrew Walbran | 88eca4f | 2021-04-13 14:26:01 +0000 | [diff] [blame] | 690 | pub set_requesting_sid: bool, |
| 691 | // Ensure that clients include a ..BinderFeatures::default() to preserve backwards compatibility |
| 692 | // when new fields are added. #[non_exhaustive] doesn't work because it prevents struct |
| 693 | // expressions entirely. |
| 694 | #[doc(hidden)] |
| 695 | pub _non_exhaustive: (), |
| 696 | } |
| 697 | |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 698 | /// Declare typed interfaces for a binder object. |
| 699 | /// |
| 700 | /// Given an interface trait and descriptor string, create a native and remote |
| 701 | /// proxy wrapper for this interface. The native service object (`$native`) |
| 702 | /// implements `Remotable` and will dispatch to the function `$on_transact` to |
| 703 | /// handle transactions. The typed proxy object (`$proxy`) wraps remote binder |
| 704 | /// objects for this interface and can optionally contain additional fields. |
| 705 | /// |
| 706 | /// Assuming the interface trait is `Interface`, `$on_transact` function must |
| 707 | /// have the following type: |
| 708 | /// |
| 709 | /// ``` |
Alice Ryhl | 8618c48 | 2021-11-09 15:35:35 +0000 | [diff] [blame] | 710 | /// # use binder::{Interface, TransactionCode, BorrowedParcel}; |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 711 | /// # trait Placeholder { |
| 712 | /// fn on_transact( |
| 713 | /// service: &dyn Interface, |
| 714 | /// code: TransactionCode, |
Alice Ryhl | 8618c48 | 2021-11-09 15:35:35 +0000 | [diff] [blame] | 715 | /// data: &BorrowedParcel, |
| 716 | /// reply: &mut BorrowedParcel, |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 717 | /// ) -> binder::Result<()>; |
| 718 | /// # } |
| 719 | /// ``` |
| 720 | /// |
| 721 | /// # Examples |
| 722 | /// |
| 723 | /// The following example declares the local service type `BnServiceManager` and |
| 724 | /// a remote proxy type `BpServiceManager` (the `n` and `p` stand for native and |
| 725 | /// proxy respectively) for the `IServiceManager` Binder interface. The |
| 726 | /// interfaces will be identified by the descriptor string |
| 727 | /// "android.os.IServiceManager". The local service will dispatch transactions |
| 728 | /// using the provided function, `on_transact`. |
| 729 | /// |
| 730 | /// ``` |
Alice Ryhl | 8618c48 | 2021-11-09 15:35:35 +0000 | [diff] [blame] | 731 | /// use binder::{declare_binder_interface, Binder, Interface, TransactionCode, BorrowedParcel}; |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 732 | /// |
| 733 | /// pub trait IServiceManager: Interface { |
| 734 | /// // remote methods... |
| 735 | /// } |
| 736 | /// |
| 737 | /// declare_binder_interface! { |
| 738 | /// IServiceManager["android.os.IServiceManager"] { |
| 739 | /// native: BnServiceManager(on_transact), |
| 740 | /// proxy: BpServiceManager, |
| 741 | /// } |
| 742 | /// } |
| 743 | /// |
| 744 | /// fn on_transact( |
| 745 | /// service: &dyn IServiceManager, |
| 746 | /// code: TransactionCode, |
Alice Ryhl | 8618c48 | 2021-11-09 15:35:35 +0000 | [diff] [blame] | 747 | /// data: &BorrowedParcel, |
| 748 | /// reply: &mut BorrowedParcel, |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 749 | /// ) -> binder::Result<()> { |
| 750 | /// // ... |
| 751 | /// Ok(()) |
| 752 | /// } |
| 753 | /// |
| 754 | /// impl IServiceManager for BpServiceManager { |
| 755 | /// // parceling/unparceling code for the IServiceManager emitted here |
| 756 | /// } |
| 757 | /// |
| 758 | /// impl IServiceManager for Binder<BnServiceManager> { |
| 759 | /// // Forward calls to local implementation |
| 760 | /// } |
| 761 | /// ``` |
| 762 | #[macro_export] |
| 763 | macro_rules! declare_binder_interface { |
| 764 | { |
| 765 | $interface:path[$descriptor:expr] { |
| 766 | native: $native:ident($on_transact:path), |
| 767 | proxy: $proxy:ident, |
Alice Ryhl | 05f5a2c | 2021-09-15 12:56:10 +0000 | [diff] [blame] | 768 | $(async: $async_interface:ident,)? |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 769 | } |
| 770 | } => { |
| 771 | $crate::declare_binder_interface! { |
| 772 | $interface[$descriptor] { |
| 773 | native: $native($on_transact), |
| 774 | proxy: $proxy {}, |
Alice Ryhl | 05f5a2c | 2021-09-15 12:56:10 +0000 | [diff] [blame] | 775 | $(async: $async_interface,)? |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 776 | stability: $crate::binder_impl::Stability::default(), |
Stephen Crane | ff7f03a | 2021-02-25 16:04:22 -0800 | [diff] [blame] | 777 | } |
| 778 | } |
| 779 | }; |
| 780 | |
| 781 | { |
| 782 | $interface:path[$descriptor:expr] { |
| 783 | native: $native:ident($on_transact:path), |
| 784 | proxy: $proxy:ident, |
Alice Ryhl | 05f5a2c | 2021-09-15 12:56:10 +0000 | [diff] [blame] | 785 | $(async: $async_interface:ident,)? |
Stephen Crane | ff7f03a | 2021-02-25 16:04:22 -0800 | [diff] [blame] | 786 | stability: $stability:expr, |
| 787 | } |
| 788 | } => { |
| 789 | $crate::declare_binder_interface! { |
| 790 | $interface[$descriptor] { |
| 791 | native: $native($on_transact), |
| 792 | proxy: $proxy {}, |
Alice Ryhl | 05f5a2c | 2021-09-15 12:56:10 +0000 | [diff] [blame] | 793 | $(async: $async_interface,)? |
Stephen Crane | ff7f03a | 2021-02-25 16:04:22 -0800 | [diff] [blame] | 794 | stability: $stability, |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 795 | } |
| 796 | } |
| 797 | }; |
| 798 | |
| 799 | { |
| 800 | $interface:path[$descriptor:expr] { |
| 801 | native: $native:ident($on_transact:path), |
| 802 | proxy: $proxy:ident { |
| 803 | $($fname:ident: $fty:ty = $finit:expr),* |
| 804 | }, |
Alice Ryhl | 05f5a2c | 2021-09-15 12:56:10 +0000 | [diff] [blame] | 805 | $(async: $async_interface:ident,)? |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 806 | } |
| 807 | } => { |
| 808 | $crate::declare_binder_interface! { |
| 809 | $interface[$descriptor] { |
Stephen Crane | ff7f03a | 2021-02-25 16:04:22 -0800 | [diff] [blame] | 810 | native: $native($on_transact), |
| 811 | proxy: $proxy { |
| 812 | $($fname: $fty = $finit),* |
| 813 | }, |
Alice Ryhl | 05f5a2c | 2021-09-15 12:56:10 +0000 | [diff] [blame] | 814 | $(async: $async_interface,)? |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 815 | stability: $crate::binder_impl::Stability::default(), |
Stephen Crane | ff7f03a | 2021-02-25 16:04:22 -0800 | [diff] [blame] | 816 | } |
| 817 | } |
| 818 | }; |
| 819 | |
| 820 | { |
| 821 | $interface:path[$descriptor:expr] { |
| 822 | native: $native:ident($on_transact:path), |
| 823 | proxy: $proxy:ident { |
| 824 | $($fname:ident: $fty:ty = $finit:expr),* |
| 825 | }, |
Alice Ryhl | 05f5a2c | 2021-09-15 12:56:10 +0000 | [diff] [blame] | 826 | $(async: $async_interface:ident,)? |
Stephen Crane | ff7f03a | 2021-02-25 16:04:22 -0800 | [diff] [blame] | 827 | stability: $stability:expr, |
| 828 | } |
| 829 | } => { |
| 830 | $crate::declare_binder_interface! { |
| 831 | $interface[$descriptor] { |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 832 | @doc[concat!("A binder [`Remotable`]($crate::binder_impl::Remotable) that holds an [`", stringify!($interface), "`] object.")] |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 833 | native: $native($on_transact), |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 834 | @doc[concat!("A binder [`Proxy`]($crate::binder_impl::Proxy) that holds an [`", stringify!($interface), "`] remote interface.")] |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 835 | proxy: $proxy { |
| 836 | $($fname: $fty = $finit),* |
| 837 | }, |
Alice Ryhl | 05f5a2c | 2021-09-15 12:56:10 +0000 | [diff] [blame] | 838 | $(async: $async_interface,)? |
Stephen Crane | ff7f03a | 2021-02-25 16:04:22 -0800 | [diff] [blame] | 839 | stability: $stability, |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 840 | } |
| 841 | } |
| 842 | }; |
| 843 | |
| 844 | { |
| 845 | $interface:path[$descriptor:expr] { |
| 846 | @doc[$native_doc:expr] |
| 847 | native: $native:ident($on_transact:path), |
| 848 | |
| 849 | @doc[$proxy_doc:expr] |
| 850 | proxy: $proxy:ident { |
| 851 | $($fname:ident: $fty:ty = $finit:expr),* |
| 852 | }, |
Stephen Crane | ff7f03a | 2021-02-25 16:04:22 -0800 | [diff] [blame] | 853 | |
Alice Ryhl | 05f5a2c | 2021-09-15 12:56:10 +0000 | [diff] [blame] | 854 | $( async: $async_interface:ident, )? |
| 855 | |
Stephen Crane | ff7f03a | 2021-02-25 16:04:22 -0800 | [diff] [blame] | 856 | stability: $stability:expr, |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 857 | } |
| 858 | } => { |
| 859 | #[doc = $proxy_doc] |
| 860 | pub struct $proxy { |
| 861 | binder: $crate::SpIBinder, |
| 862 | $($fname: $fty,)* |
| 863 | } |
| 864 | |
| 865 | impl $crate::Interface for $proxy { |
| 866 | fn as_binder(&self) -> $crate::SpIBinder { |
| 867 | self.binder.clone() |
| 868 | } |
| 869 | } |
| 870 | |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 871 | impl $crate::binder_impl::Proxy for $proxy |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 872 | where |
| 873 | $proxy: $interface, |
| 874 | { |
| 875 | fn get_descriptor() -> &'static str { |
| 876 | $descriptor |
| 877 | } |
| 878 | |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 879 | fn from_binder(mut binder: $crate::SpIBinder) -> std::result::Result<Self, $crate::StatusCode> { |
Stephen Crane | 669deb6 | 2020-09-10 17:31:39 -0700 | [diff] [blame] | 880 | Ok(Self { binder, $($fname: $finit),* }) |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 881 | } |
| 882 | } |
| 883 | |
| 884 | #[doc = $native_doc] |
| 885 | #[repr(transparent)] |
| 886 | pub struct $native(Box<dyn $interface + Sync + Send + 'static>); |
| 887 | |
| 888 | impl $native { |
| 889 | /// Create a new binder service. |
Andrew Walbran | 88eca4f | 2021-04-13 14:26:01 +0000 | [diff] [blame] | 890 | pub fn new_binder<T: $interface + Sync + Send + 'static>(inner: T, features: $crate::BinderFeatures) -> $crate::Strong<dyn $interface> { |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 891 | let mut binder = $crate::binder_impl::Binder::new_with_stability($native(Box::new(inner)), $stability); |
Janis Danisevskis | 1323d51 | 2021-11-09 07:48:08 -0800 | [diff] [blame] | 892 | #[cfg(not(android_vndk))] |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 893 | $crate::binder_impl::IBinderInternal::set_requesting_sid(&mut binder, features.set_requesting_sid); |
Stephen Crane | ddb3e6d | 2020-12-18 13:27:22 -0800 | [diff] [blame] | 894 | $crate::Strong::new(Box::new(binder)) |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 895 | } |
| 896 | } |
| 897 | |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 898 | impl $crate::binder_impl::Remotable for $native { |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 899 | fn get_descriptor() -> &'static str { |
| 900 | $descriptor |
| 901 | } |
| 902 | |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 903 | fn on_transact(&self, code: $crate::binder_impl::TransactionCode, data: &$crate::binder_impl::BorrowedParcel<'_>, reply: &mut $crate::binder_impl::BorrowedParcel<'_>) -> std::result::Result<(), $crate::StatusCode> { |
Andrei Homescu | 3281437 | 2020-08-20 15:36:08 -0700 | [diff] [blame] | 904 | match $on_transact(&*self.0, code, data, reply) { |
| 905 | // The C++ backend converts UNEXPECTED_NULL into an exception |
| 906 | Err($crate::StatusCode::UNEXPECTED_NULL) => { |
| 907 | let status = $crate::Status::new_exception( |
| 908 | $crate::ExceptionCode::NULL_POINTER, |
| 909 | None, |
| 910 | ); |
| 911 | reply.write(&status) |
| 912 | }, |
| 913 | result => result |
| 914 | } |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 915 | } |
| 916 | |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 917 | fn on_dump(&self, file: &std::fs::File, args: &[&std::ffi::CStr]) -> std::result::Result<(), $crate::StatusCode> { |
Stephen Crane | 2a3297f | 2021-06-11 16:48:10 -0700 | [diff] [blame] | 918 | self.0.dump(file, args) |
| 919 | } |
| 920 | |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 921 | fn get_class() -> $crate::binder_impl::InterfaceClass { |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 922 | static CLASS_INIT: std::sync::Once = std::sync::Once::new(); |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 923 | static mut CLASS: Option<$crate::binder_impl::InterfaceClass> = None; |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 924 | |
Andrew Walbran | 2f3ff9f | 2023-07-07 16:58:13 +0100 | [diff] [blame^] | 925 | // Safety: This assignment is guarded by the `CLASS_INIT` `Once` |
| 926 | // variable, and therefore is thread-safe, as it can only occur |
| 927 | // once. |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 928 | CLASS_INIT.call_once(|| unsafe { |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 929 | CLASS = Some($crate::binder_impl::InterfaceClass::new::<$crate::binder_impl::Binder<$native>>()); |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 930 | }); |
Andrew Walbran | 2f3ff9f | 2023-07-07 16:58:13 +0100 | [diff] [blame^] | 931 | // Safety: The `CLASS` variable can only be mutated once, above, |
| 932 | // and is subsequently safe to read from any thread. |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 933 | unsafe { |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 934 | CLASS.unwrap() |
| 935 | } |
| 936 | } |
| 937 | } |
| 938 | |
| 939 | impl $crate::FromIBinder for dyn $interface { |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 940 | fn try_from(mut ibinder: $crate::SpIBinder) -> std::result::Result<$crate::Strong<dyn $interface>, $crate::StatusCode> { |
| 941 | use $crate::binder_impl::AssociateClass; |
Stephen Crane | 669deb6 | 2020-09-10 17:31:39 -0700 | [diff] [blame] | 942 | |
| 943 | let existing_class = ibinder.get_class(); |
| 944 | if let Some(class) = existing_class { |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 945 | if class != <$native as $crate::binder_impl::Remotable>::get_class() && |
| 946 | class.get_descriptor() == <$native as $crate::binder_impl::Remotable>::get_descriptor() |
Stephen Crane | 669deb6 | 2020-09-10 17:31:39 -0700 | [diff] [blame] | 947 | { |
| 948 | // The binder object's descriptor string matches what we |
| 949 | // expect. We still need to treat this local or already |
| 950 | // associated object as remote, because we can't cast it |
| 951 | // into a Rust service object without a matching class |
| 952 | // pointer. |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 953 | return Ok($crate::Strong::new(Box::new(<$proxy as $crate::binder_impl::Proxy>::from_binder(ibinder)?))); |
Stephen Crane | 669deb6 | 2020-09-10 17:31:39 -0700 | [diff] [blame] | 954 | } |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 955 | } |
| 956 | |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 957 | if ibinder.associate_class(<$native as $crate::binder_impl::Remotable>::get_class()) { |
| 958 | let service: std::result::Result<$crate::binder_impl::Binder<$native>, $crate::StatusCode> = |
Stephen Crane | 669deb6 | 2020-09-10 17:31:39 -0700 | [diff] [blame] | 959 | std::convert::TryFrom::try_from(ibinder.clone()); |
| 960 | if let Ok(service) = service { |
| 961 | // We were able to associate with our expected class and |
| 962 | // the service is local. |
Stephen Crane | ddb3e6d | 2020-12-18 13:27:22 -0800 | [diff] [blame] | 963 | return Ok($crate::Strong::new(Box::new(service))); |
Stephen Crane | 669deb6 | 2020-09-10 17:31:39 -0700 | [diff] [blame] | 964 | } else { |
| 965 | // Service is remote |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 966 | return Ok($crate::Strong::new(Box::new(<$proxy as $crate::binder_impl::Proxy>::from_binder(ibinder)?))); |
Stephen Crane | 669deb6 | 2020-09-10 17:31:39 -0700 | [diff] [blame] | 967 | } |
Matthew Maurer | f6b9ad9 | 2020-12-03 19:27:25 +0000 | [diff] [blame] | 968 | } |
Stephen Crane | 669deb6 | 2020-09-10 17:31:39 -0700 | [diff] [blame] | 969 | |
| 970 | Err($crate::StatusCode::BAD_TYPE.into()) |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 971 | } |
| 972 | } |
| 973 | |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 974 | impl $crate::binder_impl::Serialize for dyn $interface + '_ |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 975 | where |
Stephen Crane | d58bce0 | 2020-07-07 12:26:02 -0700 | [diff] [blame] | 976 | dyn $interface: $crate::Interface |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 977 | { |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 978 | fn serialize(&self, parcel: &mut $crate::binder_impl::BorrowedParcel<'_>) -> std::result::Result<(), $crate::StatusCode> { |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 979 | let binder = $crate::Interface::as_binder(self); |
| 980 | parcel.write(&binder) |
| 981 | } |
| 982 | } |
| 983 | |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 984 | impl $crate::binder_impl::SerializeOption for dyn $interface + '_ { |
| 985 | fn serialize_option(this: Option<&Self>, parcel: &mut $crate::binder_impl::BorrowedParcel<'_>) -> std::result::Result<(), $crate::StatusCode> { |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 986 | parcel.write(&this.map($crate::Interface::as_binder)) |
| 987 | } |
| 988 | } |
Andrei Homescu | 2e3c147 | 2020-08-11 16:35:40 -0700 | [diff] [blame] | 989 | |
Alice Ryhl | 05f5a2c | 2021-09-15 12:56:10 +0000 | [diff] [blame] | 990 | impl std::fmt::Debug for dyn $interface + '_ { |
Andrei Homescu | 2e3c147 | 2020-08-11 16:35:40 -0700 | [diff] [blame] | 991 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 992 | f.pad(stringify!($interface)) |
| 993 | } |
| 994 | } |
Andrei Homescu | 64ebd13 | 2020-08-07 22:12:48 -0700 | [diff] [blame] | 995 | |
Stephen Crane | ddb3e6d | 2020-12-18 13:27:22 -0800 | [diff] [blame] | 996 | /// Convert a &dyn $interface to Strong<dyn $interface> |
Andrei Homescu | 64ebd13 | 2020-08-07 22:12:48 -0700 | [diff] [blame] | 997 | impl std::borrow::ToOwned for dyn $interface { |
Stephen Crane | ddb3e6d | 2020-12-18 13:27:22 -0800 | [diff] [blame] | 998 | type Owned = $crate::Strong<dyn $interface>; |
Andrei Homescu | 64ebd13 | 2020-08-07 22:12:48 -0700 | [diff] [blame] | 999 | fn to_owned(&self) -> Self::Owned { |
| 1000 | self.as_binder().into_interface() |
| 1001 | .expect(concat!("Error cloning interface ", stringify!($interface))) |
| 1002 | } |
| 1003 | } |
Alice Ryhl | 05f5a2c | 2021-09-15 12:56:10 +0000 | [diff] [blame] | 1004 | |
| 1005 | $( |
| 1006 | // Async interface trait implementations. |
| 1007 | impl<P: $crate::BinderAsyncPool> $crate::FromIBinder for dyn $async_interface<P> { |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 1008 | fn try_from(mut ibinder: $crate::SpIBinder) -> std::result::Result<$crate::Strong<dyn $async_interface<P>>, $crate::StatusCode> { |
| 1009 | use $crate::binder_impl::AssociateClass; |
Alice Ryhl | 05f5a2c | 2021-09-15 12:56:10 +0000 | [diff] [blame] | 1010 | |
| 1011 | let existing_class = ibinder.get_class(); |
| 1012 | if let Some(class) = existing_class { |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 1013 | if class != <$native as $crate::binder_impl::Remotable>::get_class() && |
| 1014 | class.get_descriptor() == <$native as $crate::binder_impl::Remotable>::get_descriptor() |
Alice Ryhl | 05f5a2c | 2021-09-15 12:56:10 +0000 | [diff] [blame] | 1015 | { |
| 1016 | // The binder object's descriptor string matches what we |
| 1017 | // expect. We still need to treat this local or already |
| 1018 | // associated object as remote, because we can't cast it |
| 1019 | // into a Rust service object without a matching class |
| 1020 | // pointer. |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 1021 | return Ok($crate::Strong::new(Box::new(<$proxy as $crate::binder_impl::Proxy>::from_binder(ibinder)?))); |
Alice Ryhl | 05f5a2c | 2021-09-15 12:56:10 +0000 | [diff] [blame] | 1022 | } |
| 1023 | } |
| 1024 | |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 1025 | if ibinder.associate_class(<$native as $crate::binder_impl::Remotable>::get_class()) { |
| 1026 | let service: std::result::Result<$crate::binder_impl::Binder<$native>, $crate::StatusCode> = |
Alice Ryhl | 05f5a2c | 2021-09-15 12:56:10 +0000 | [diff] [blame] | 1027 | std::convert::TryFrom::try_from(ibinder.clone()); |
| 1028 | if let Ok(service) = service { |
| 1029 | // We were able to associate with our expected class and |
| 1030 | // the service is local. |
| 1031 | todo!() |
| 1032 | //return Ok($crate::Strong::new(Box::new(service))); |
| 1033 | } else { |
| 1034 | // Service is remote |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 1035 | return Ok($crate::Strong::new(Box::new(<$proxy as $crate::binder_impl::Proxy>::from_binder(ibinder)?))); |
Alice Ryhl | 05f5a2c | 2021-09-15 12:56:10 +0000 | [diff] [blame] | 1036 | } |
| 1037 | } |
| 1038 | |
| 1039 | Err($crate::StatusCode::BAD_TYPE.into()) |
| 1040 | } |
| 1041 | } |
| 1042 | |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 1043 | impl<P: $crate::BinderAsyncPool> $crate::binder_impl::Serialize for dyn $async_interface<P> + '_ { |
| 1044 | fn serialize(&self, parcel: &mut $crate::binder_impl::BorrowedParcel<'_>) -> std::result::Result<(), $crate::StatusCode> { |
Alice Ryhl | 05f5a2c | 2021-09-15 12:56:10 +0000 | [diff] [blame] | 1045 | let binder = $crate::Interface::as_binder(self); |
| 1046 | parcel.write(&binder) |
| 1047 | } |
| 1048 | } |
| 1049 | |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 1050 | impl<P: $crate::BinderAsyncPool> $crate::binder_impl::SerializeOption for dyn $async_interface<P> + '_ { |
| 1051 | fn serialize_option(this: Option<&Self>, parcel: &mut $crate::binder_impl::BorrowedParcel<'_>) -> std::result::Result<(), $crate::StatusCode> { |
Alice Ryhl | 05f5a2c | 2021-09-15 12:56:10 +0000 | [diff] [blame] | 1052 | parcel.write(&this.map($crate::Interface::as_binder)) |
| 1053 | } |
| 1054 | } |
| 1055 | |
| 1056 | impl<P: $crate::BinderAsyncPool> std::fmt::Debug for dyn $async_interface<P> + '_ { |
| 1057 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 1058 | f.pad(stringify!($async_interface)) |
| 1059 | } |
| 1060 | } |
| 1061 | |
| 1062 | /// Convert a &dyn $async_interface to Strong<dyn $async_interface> |
| 1063 | impl<P: $crate::BinderAsyncPool> std::borrow::ToOwned for dyn $async_interface<P> { |
| 1064 | type Owned = $crate::Strong<dyn $async_interface<P>>; |
| 1065 | fn to_owned(&self) -> Self::Owned { |
| 1066 | self.as_binder().into_interface() |
| 1067 | .expect(concat!("Error cloning interface ", stringify!($async_interface))) |
| 1068 | } |
| 1069 | } |
Alice Ryhl | c173684 | 2021-11-23 12:38:51 +0000 | [diff] [blame] | 1070 | |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 1071 | impl<P: $crate::BinderAsyncPool> $crate::binder_impl::ToAsyncInterface<P> for dyn $interface { |
Alice Ryhl | c173684 | 2021-11-23 12:38:51 +0000 | [diff] [blame] | 1072 | type Target = dyn $async_interface<P>; |
| 1073 | } |
| 1074 | |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 1075 | impl<P: $crate::BinderAsyncPool> $crate::binder_impl::ToSyncInterface for dyn $async_interface<P> { |
Alice Ryhl | c173684 | 2021-11-23 12:38:51 +0000 | [diff] [blame] | 1076 | type Target = dyn $interface; |
| 1077 | } |
Alice Ryhl | 05f5a2c | 2021-09-15 12:56:10 +0000 | [diff] [blame] | 1078 | )? |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 1079 | }; |
| 1080 | } |
Andrei Homescu | 00eca71 | 2020-09-09 18:57:40 -0700 | [diff] [blame] | 1081 | |
| 1082 | /// Declare an AIDL enumeration. |
| 1083 | /// |
| 1084 | /// This is mainly used internally by the AIDL compiler. |
| 1085 | #[macro_export] |
| 1086 | macro_rules! declare_binder_enum { |
| 1087 | { |
Stephen Crane | 7bca105 | 2021-10-25 17:52:51 -0700 | [diff] [blame] | 1088 | $( #[$attr:meta] )* |
Andrei Homescu | 7f38cf9 | 2021-06-29 23:55:43 +0000 | [diff] [blame] | 1089 | $enum:ident : [$backing:ty; $size:expr] { |
Jooyung Han | 70d9281 | 2022-03-18 15:29:54 +0900 | [diff] [blame] | 1090 | $( $( #[$value_attr:meta] )* $name:ident = $value:expr, )* |
Andrei Homescu | 00eca71 | 2020-09-09 18:57:40 -0700 | [diff] [blame] | 1091 | } |
| 1092 | } => { |
Stephen Crane | 7bca105 | 2021-10-25 17:52:51 -0700 | [diff] [blame] | 1093 | $( #[$attr] )* |
Andrei Homescu | c06cfc3 | 2022-09-30 02:46:27 +0000 | [diff] [blame] | 1094 | #[derive(Default, Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash)] |
Stephen Crane | 7bca105 | 2021-10-25 17:52:51 -0700 | [diff] [blame] | 1095 | #[allow(missing_docs)] |
Andrei Homescu | 00eca71 | 2020-09-09 18:57:40 -0700 | [diff] [blame] | 1096 | pub struct $enum(pub $backing); |
| 1097 | impl $enum { |
Jooyung Han | 70d9281 | 2022-03-18 15:29:54 +0900 | [diff] [blame] | 1098 | $( $( #[$value_attr] )* #[allow(missing_docs)] pub const $name: Self = Self($value); )* |
Andrei Homescu | 7f38cf9 | 2021-06-29 23:55:43 +0000 | [diff] [blame] | 1099 | |
| 1100 | #[inline(always)] |
Stephen Crane | 7bca105 | 2021-10-25 17:52:51 -0700 | [diff] [blame] | 1101 | #[allow(missing_docs)] |
Andrei Homescu | 7f38cf9 | 2021-06-29 23:55:43 +0000 | [diff] [blame] | 1102 | pub const fn enum_values() -> [Self; $size] { |
| 1103 | [$(Self::$name),*] |
| 1104 | } |
Andrei Homescu | 00eca71 | 2020-09-09 18:57:40 -0700 | [diff] [blame] | 1105 | } |
| 1106 | |
Andrei Homescu | c06cfc3 | 2022-09-30 02:46:27 +0000 | [diff] [blame] | 1107 | impl std::fmt::Debug for $enum { |
| 1108 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 1109 | match self.0 { |
| 1110 | $($value => f.write_str(stringify!($name)),)* |
| 1111 | _ => f.write_fmt(format_args!("{}", self.0)) |
| 1112 | } |
| 1113 | } |
| 1114 | } |
| 1115 | |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 1116 | impl $crate::binder_impl::Serialize for $enum { |
| 1117 | fn serialize(&self, parcel: &mut $crate::binder_impl::BorrowedParcel<'_>) -> std::result::Result<(), $crate::StatusCode> { |
Andrei Homescu | 00eca71 | 2020-09-09 18:57:40 -0700 | [diff] [blame] | 1118 | parcel.write(&self.0) |
| 1119 | } |
| 1120 | } |
| 1121 | |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 1122 | impl $crate::binder_impl::SerializeArray for $enum { |
| 1123 | fn serialize_array(slice: &[Self], parcel: &mut $crate::binder_impl::BorrowedParcel<'_>) -> std::result::Result<(), $crate::StatusCode> { |
Andrei Homescu | 00eca71 | 2020-09-09 18:57:40 -0700 | [diff] [blame] | 1124 | let v: Vec<$backing> = slice.iter().map(|x| x.0).collect(); |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 1125 | <$backing as $crate::binder_impl::SerializeArray>::serialize_array(&v[..], parcel) |
Andrei Homescu | 00eca71 | 2020-09-09 18:57:40 -0700 | [diff] [blame] | 1126 | } |
| 1127 | } |
| 1128 | |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 1129 | impl $crate::binder_impl::Deserialize for $enum { |
Andrei Homescu | 2b802f7 | 2023-05-05 03:21:43 +0000 | [diff] [blame] | 1130 | type UninitType = Self; |
| 1131 | fn uninit() -> Self::UninitType { Self::UninitType::default() } |
| 1132 | fn from_init(value: Self) -> Self::UninitType { value } |
| 1133 | |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 1134 | fn deserialize(parcel: &$crate::binder_impl::BorrowedParcel<'_>) -> std::result::Result<Self, $crate::StatusCode> { |
Andrei Homescu | 00eca71 | 2020-09-09 18:57:40 -0700 | [diff] [blame] | 1135 | parcel.read().map(Self) |
| 1136 | } |
| 1137 | } |
| 1138 | |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 1139 | impl $crate::binder_impl::DeserializeArray for $enum { |
| 1140 | fn deserialize_array(parcel: &$crate::binder_impl::BorrowedParcel<'_>) -> std::result::Result<Option<Vec<Self>>, $crate::StatusCode> { |
Andrei Homescu | 00eca71 | 2020-09-09 18:57:40 -0700 | [diff] [blame] | 1141 | let v: Option<Vec<$backing>> = |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 1142 | <$backing as $crate::binder_impl::DeserializeArray>::deserialize_array(parcel)?; |
Andrei Homescu | 00eca71 | 2020-09-09 18:57:40 -0700 | [diff] [blame] | 1143 | Ok(v.map(|v| v.into_iter().map(Self).collect())) |
| 1144 | } |
| 1145 | } |
| 1146 | }; |
| 1147 | } |