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 | //! Rust API for interacting with a remote binder service. |
| 18 | |
| 19 | use crate::binder::{ |
Andrew Walbran | 12400d8 | 2021-03-04 17:04:34 +0000 | [diff] [blame] | 20 | AsNative, FromIBinder, IBinder, IBinderInternal, Interface, InterfaceClass, Strong, |
| 21 | TransactionCode, TransactionFlags, |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 22 | }; |
| 23 | use crate::error::{status_result, Result, StatusCode}; |
| 24 | use crate::parcel::{ |
Alice Ryhl | 268458c | 2021-09-15 12:56:10 +0000 | [diff] [blame^] | 25 | Deserialize, DeserializeArray, DeserializeOption, OwnedParcel, Parcel, Serialize, SerializeArray, |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 26 | SerializeOption, |
| 27 | }; |
| 28 | use crate::sys; |
| 29 | |
Stephen Crane | ddb3e6d | 2020-12-18 13:27:22 -0800 | [diff] [blame] | 30 | use std::cmp::Ordering; |
Andrew Walbran | 12400d8 | 2021-03-04 17:04:34 +0000 | [diff] [blame] | 31 | use std::convert::TryInto; |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 32 | use std::ffi::{c_void, CString}; |
Andrei Homescu | 2e3c147 | 2020-08-11 16:35:40 -0700 | [diff] [blame] | 33 | use std::fmt; |
Alice Ryhl | ea9d9d2 | 2021-08-27 07:51:30 +0000 | [diff] [blame] | 34 | use std::mem; |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 35 | use std::os::unix::io::AsRawFd; |
| 36 | use std::ptr; |
Alice Ryhl | ea9d9d2 | 2021-08-27 07:51:30 +0000 | [diff] [blame] | 37 | use std::sync::Arc; |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 38 | |
| 39 | /// A strong reference to a Binder remote object. |
| 40 | /// |
| 41 | /// This struct encapsulates the generic C++ `sp<IBinder>` class. This wrapper |
| 42 | /// is untyped; typed interface access is implemented by the AIDL compiler. |
Alice Ryhl | 8dde9bc | 2021-08-16 16:57:10 +0000 | [diff] [blame] | 43 | pub struct SpIBinder(ptr::NonNull<sys::AIBinder>); |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 44 | |
Andrei Homescu | 2e3c147 | 2020-08-11 16:35:40 -0700 | [diff] [blame] | 45 | impl fmt::Debug for SpIBinder { |
| 46 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 47 | f.pad("SpIBinder") |
| 48 | } |
| 49 | } |
| 50 | |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 51 | /// # Safety |
| 52 | /// |
Stephen Crane | f03fe3d | 2021-06-25 15:05:00 -0700 | [diff] [blame] | 53 | /// An `SpIBinder` is an immutable handle to a C++ IBinder, which is thread-safe |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 54 | unsafe impl Send for SpIBinder {} |
| 55 | |
Stephen Crane | f03fe3d | 2021-06-25 15:05:00 -0700 | [diff] [blame] | 56 | /// # Safety |
| 57 | /// |
| 58 | /// An `SpIBinder` is an immutable handle to a C++ IBinder, which is thread-safe |
| 59 | unsafe impl Sync for SpIBinder {} |
| 60 | |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 61 | impl SpIBinder { |
| 62 | /// Create an `SpIBinder` wrapper object from a raw `AIBinder` pointer. |
| 63 | /// |
| 64 | /// # Safety |
| 65 | /// |
| 66 | /// This constructor is safe iff `ptr` is a null pointer or a valid pointer |
| 67 | /// to an `AIBinder`. |
| 68 | /// |
| 69 | /// In the non-null case, this method conceptually takes ownership of a strong |
| 70 | /// reference to the object, so `AIBinder_incStrong` must have been called |
| 71 | /// on the pointer before passing it to this constructor. This is generally |
| 72 | /// done by Binder NDK methods that return an `AIBinder`, but care should be |
| 73 | /// taken to ensure this invariant. |
| 74 | /// |
| 75 | /// All `SpIBinder` objects that are constructed will hold a valid pointer |
| 76 | /// to an `AIBinder`, which will remain valid for the entire lifetime of the |
| 77 | /// `SpIBinder` (we keep a strong reference, and only decrement on drop). |
| 78 | pub(crate) unsafe fn from_raw(ptr: *mut sys::AIBinder) -> Option<Self> { |
Alice Ryhl | 8dde9bc | 2021-08-16 16:57:10 +0000 | [diff] [blame] | 79 | ptr::NonNull::new(ptr).map(Self) |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Stephen Crane | d58bce0 | 2020-07-07 12:26:02 -0700 | [diff] [blame] | 82 | /// Extract a raw `AIBinder` pointer from this wrapper. |
| 83 | /// |
| 84 | /// This method should _only_ be used for testing. Do not try to use the NDK |
| 85 | /// interface directly for anything else. |
| 86 | /// |
| 87 | /// # Safety |
| 88 | /// |
| 89 | /// The resulting pointer is valid only as long as the SpIBinder is alive. |
| 90 | /// The SpIBinder object retains ownership of the AIBinder and the caller |
| 91 | /// should not attempt to free the returned pointer. |
| 92 | pub unsafe fn as_raw(&self) -> *mut sys::AIBinder { |
Alice Ryhl | 8dde9bc | 2021-08-16 16:57:10 +0000 | [diff] [blame] | 93 | self.0.as_ptr() |
Stephen Crane | d58bce0 | 2020-07-07 12:26:02 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 96 | /// Return true if this binder object is hosted in a different process than |
| 97 | /// the current one. |
| 98 | pub fn is_remote(&self) -> bool { |
| 99 | unsafe { |
| 100 | // Safety: `SpIBinder` guarantees that it always contains a valid |
| 101 | // `AIBinder` pointer. |
| 102 | sys::AIBinder_isRemote(self.as_native()) |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /// Try to convert this Binder object into a trait object for the given |
| 107 | /// Binder interface. |
| 108 | /// |
| 109 | /// If this object does not implement the expected interface, the error |
| 110 | /// `StatusCode::BAD_TYPE` is returned. |
Stephen Crane | ddb3e6d | 2020-12-18 13:27:22 -0800 | [diff] [blame] | 111 | pub fn into_interface<I: FromIBinder + Interface + ?Sized>(self) -> Result<Strong<I>> { |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 112 | FromIBinder::try_from(self) |
| 113 | } |
| 114 | |
| 115 | /// Return the interface class of this binder object, if associated with |
| 116 | /// one. |
Stephen Crane | 669deb6 | 2020-09-10 17:31:39 -0700 | [diff] [blame] | 117 | pub fn get_class(&mut self) -> Option<InterfaceClass> { |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 118 | unsafe { |
| 119 | // Safety: `SpIBinder` guarantees that it always contains a valid |
| 120 | // `AIBinder` pointer. `AIBinder_getClass` returns either a null |
| 121 | // pointer or a valid pointer to an `AIBinder_Class`. After mapping |
| 122 | // null to None, we can safely construct an `InterfaceClass` if the |
| 123 | // pointer was non-null. |
| 124 | let class = sys::AIBinder_getClass(self.as_native_mut()); |
| 125 | class.as_ref().map(|p| InterfaceClass::from_ptr(p)) |
| 126 | } |
| 127 | } |
Andrew Walbran | 8fe3ecc | 2020-12-15 11:29:58 +0000 | [diff] [blame] | 128 | |
| 129 | /// Creates a new weak reference to this binder object. |
| 130 | pub fn downgrade(&mut self) -> WpIBinder { |
| 131 | WpIBinder::new(self) |
| 132 | } |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Victor Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 135 | pub mod unstable_api { |
| 136 | use super::{sys, SpIBinder}; |
| 137 | |
| 138 | /// A temporary API to allow the client to create a `SpIBinder` from a `sys::AIBinder`. This is |
| 139 | /// needed to bridge RPC binder, which doesn't have Rust API yet. |
| 140 | /// TODO(b/184872979): remove once the Rust API is created. |
| 141 | /// |
| 142 | /// # Safety |
| 143 | /// |
| 144 | /// See `SpIBinder::from_raw`. |
| 145 | pub unsafe fn new_spibinder(ptr: *mut sys::AIBinder) -> Option<SpIBinder> { |
| 146 | SpIBinder::from_raw(ptr) |
| 147 | } |
| 148 | } |
| 149 | |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 150 | /// An object that can be associate with an [`InterfaceClass`]. |
| 151 | pub trait AssociateClass { |
| 152 | /// Check if this object is a valid object for the given interface class |
| 153 | /// `I`. |
| 154 | /// |
| 155 | /// Returns `Some(self)` if this is a valid instance of the interface, and |
| 156 | /// `None` otherwise. |
| 157 | /// |
| 158 | /// Classes constructed by `InterfaceClass` are unique per type, so |
| 159 | /// repeatedly calling this method for the same `InterfaceClass` is allowed. |
| 160 | fn associate_class(&mut self, class: InterfaceClass) -> bool; |
| 161 | } |
| 162 | |
| 163 | impl AssociateClass for SpIBinder { |
| 164 | fn associate_class(&mut self, class: InterfaceClass) -> bool { |
| 165 | unsafe { |
| 166 | // Safety: `SpIBinder` guarantees that it always contains a valid |
| 167 | // `AIBinder` pointer. An `InterfaceClass` can always be converted |
| 168 | // into a valid `AIBinder_Class` pointer, so these parameters are |
| 169 | // always safe. |
| 170 | sys::AIBinder_associateClass(self.as_native_mut(), class.into()) |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
Stephen Crane | ddb3e6d | 2020-12-18 13:27:22 -0800 | [diff] [blame] | 175 | impl Ord for SpIBinder { |
| 176 | fn cmp(&self, other: &Self) -> Ordering { |
| 177 | let less_than = unsafe { |
| 178 | // Safety: SpIBinder always holds a valid `AIBinder` pointer, so |
| 179 | // this pointer is always safe to pass to `AIBinder_lt` (null is |
| 180 | // also safe to pass to this function, but we should never do that). |
Alice Ryhl | 8dde9bc | 2021-08-16 16:57:10 +0000 | [diff] [blame] | 181 | sys::AIBinder_lt(self.0.as_ptr(), other.0.as_ptr()) |
Stephen Crane | ddb3e6d | 2020-12-18 13:27:22 -0800 | [diff] [blame] | 182 | }; |
| 183 | let greater_than = unsafe { |
| 184 | // Safety: SpIBinder always holds a valid `AIBinder` pointer, so |
| 185 | // this pointer is always safe to pass to `AIBinder_lt` (null is |
| 186 | // also safe to pass to this function, but we should never do that). |
Alice Ryhl | 8dde9bc | 2021-08-16 16:57:10 +0000 | [diff] [blame] | 187 | sys::AIBinder_lt(other.0.as_ptr(), self.0.as_ptr()) |
Stephen Crane | ddb3e6d | 2020-12-18 13:27:22 -0800 | [diff] [blame] | 188 | }; |
| 189 | if !less_than && !greater_than { |
| 190 | Ordering::Equal |
| 191 | } else if less_than { |
| 192 | Ordering::Less |
| 193 | } else { |
| 194 | Ordering::Greater |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | impl PartialOrd for SpIBinder { |
| 200 | fn partial_cmp(&self, other: &Self) -> Option<Ordering> { |
| 201 | Some(self.cmp(other)) |
| 202 | } |
| 203 | } |
| 204 | |
Stephen Crane | 994a0f0 | 2020-08-11 14:47:29 -0700 | [diff] [blame] | 205 | impl PartialEq for SpIBinder { |
| 206 | fn eq(&self, other: &Self) -> bool { |
Alice Ryhl | 8dde9bc | 2021-08-16 16:57:10 +0000 | [diff] [blame] | 207 | ptr::eq(self.0.as_ptr(), other.0.as_ptr()) |
Stephen Crane | 994a0f0 | 2020-08-11 14:47:29 -0700 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | |
| 211 | impl Eq for SpIBinder {} |
| 212 | |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 213 | impl Clone for SpIBinder { |
| 214 | fn clone(&self) -> Self { |
| 215 | unsafe { |
| 216 | // Safety: Cloning a strong reference must increment the reference |
| 217 | // count. We are guaranteed by the `SpIBinder` constructor |
| 218 | // invariants that `self.0` is always a valid `AIBinder` pointer. |
Alice Ryhl | 8dde9bc | 2021-08-16 16:57:10 +0000 | [diff] [blame] | 219 | sys::AIBinder_incStrong(self.0.as_ptr()); |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 220 | } |
| 221 | Self(self.0) |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | impl Drop for SpIBinder { |
| 226 | // We hold a strong reference to the IBinder in SpIBinder and need to give up |
| 227 | // this reference on drop. |
| 228 | fn drop(&mut self) { |
| 229 | unsafe { |
| 230 | // Safety: SpIBinder always holds a valid `AIBinder` pointer, so we |
| 231 | // know this pointer is safe to pass to `AIBinder_decStrong` here. |
| 232 | sys::AIBinder_decStrong(self.as_native_mut()); |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
Andrew Walbran | 12400d8 | 2021-03-04 17:04:34 +0000 | [diff] [blame] | 237 | impl<T: AsNative<sys::AIBinder>> IBinderInternal for T { |
Alice Ryhl | 268458c | 2021-09-15 12:56:10 +0000 | [diff] [blame^] | 238 | fn prepare_transact(&self) -> Result<OwnedParcel> { |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 239 | let mut input = ptr::null_mut(); |
| 240 | let status = unsafe { |
| 241 | // Safety: `SpIBinder` guarantees that `self` always contains a |
| 242 | // valid pointer to an `AIBinder`. It is safe to cast from an |
| 243 | // immutable pointer to a mutable pointer here, because |
| 244 | // `AIBinder_prepareTransaction` only calls immutable `AIBinder` |
| 245 | // methods but the parameter is unfortunately not marked as const. |
| 246 | // |
| 247 | // After the call, input will be either a valid, owned `AParcel` |
| 248 | // pointer, or null. |
| 249 | sys::AIBinder_prepareTransaction(self.as_native() as *mut sys::AIBinder, &mut input) |
| 250 | }; |
Alice Ryhl | feba6ca | 2021-08-19 10:47:04 +0000 | [diff] [blame] | 251 | |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 252 | status_result(status)?; |
Alice Ryhl | feba6ca | 2021-08-19 10:47:04 +0000 | [diff] [blame] | 253 | |
| 254 | unsafe { |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 255 | // Safety: At this point, `input` is either a valid, owned `AParcel` |
Alice Ryhl | 268458c | 2021-09-15 12:56:10 +0000 | [diff] [blame^] | 256 | // pointer, or null. `OwnedParcel::from_raw` safely handles both cases, |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 257 | // taking ownership of the parcel. |
Alice Ryhl | 268458c | 2021-09-15 12:56:10 +0000 | [diff] [blame^] | 258 | OwnedParcel::from_raw(input).ok_or(StatusCode::UNEXPECTED_NULL) |
Alice Ryhl | feba6ca | 2021-08-19 10:47:04 +0000 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | |
| 262 | fn submit_transact( |
| 263 | &self, |
| 264 | code: TransactionCode, |
Alice Ryhl | 268458c | 2021-09-15 12:56:10 +0000 | [diff] [blame^] | 265 | data: OwnedParcel, |
Alice Ryhl | feba6ca | 2021-08-19 10:47:04 +0000 | [diff] [blame] | 266 | flags: TransactionFlags, |
Alice Ryhl | 268458c | 2021-09-15 12:56:10 +0000 | [diff] [blame^] | 267 | ) -> Result<OwnedParcel> { |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 268 | let mut reply = ptr::null_mut(); |
| 269 | let status = unsafe { |
| 270 | // Safety: `SpIBinder` guarantees that `self` always contains a |
| 271 | // valid pointer to an `AIBinder`. Although `IBinder::transact` is |
| 272 | // not a const method, it is still safe to cast our immutable |
| 273 | // pointer to mutable for the call. First, `IBinder::transact` is |
| 274 | // thread-safe, so concurrency is not an issue. The only way that |
| 275 | // `transact` can affect any visible, mutable state in the current |
| 276 | // process is by calling `onTransact` for a local service. However, |
| 277 | // in order for transactions to be thread-safe, this method must |
| 278 | // dynamically lock its data before modifying it. We enforce this |
| 279 | // property in Rust by requiring `Sync` for remotable objects and |
| 280 | // only providing `on_transact` with an immutable reference to |
| 281 | // `self`. |
| 282 | // |
Alice Ryhl | feba6ca | 2021-08-19 10:47:04 +0000 | [diff] [blame] | 283 | // This call takes ownership of the `data` parcel pointer, and |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 284 | // passes ownership of the `reply` out parameter to its caller. It |
| 285 | // does not affect ownership of the `binder` parameter. |
| 286 | sys::AIBinder_transact( |
| 287 | self.as_native() as *mut sys::AIBinder, |
| 288 | code, |
Alice Ryhl | feba6ca | 2021-08-19 10:47:04 +0000 | [diff] [blame] | 289 | &mut data.into_raw(), |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 290 | &mut reply, |
| 291 | flags, |
| 292 | ) |
| 293 | }; |
| 294 | status_result(status)?; |
| 295 | |
| 296 | unsafe { |
| 297 | // Safety: `reply` is either a valid `AParcel` pointer or null |
| 298 | // after the call to `AIBinder_transact` above, so we can |
| 299 | // construct a `Parcel` out of it. `AIBinder_transact` passes |
| 300 | // ownership of the `reply` parcel to Rust, so we need to |
Alice Ryhl | 268458c | 2021-09-15 12:56:10 +0000 | [diff] [blame^] | 301 | // construct an owned variant. |
| 302 | OwnedParcel::from_raw(reply).ok_or(StatusCode::UNEXPECTED_NULL) |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 303 | } |
| 304 | } |
| 305 | |
| 306 | fn is_binder_alive(&self) -> bool { |
| 307 | unsafe { |
| 308 | // Safety: `SpIBinder` guarantees that `self` always contains a |
| 309 | // valid pointer to an `AIBinder`. |
| 310 | // |
| 311 | // This call does not affect ownership of its pointer parameter. |
| 312 | sys::AIBinder_isAlive(self.as_native()) |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | fn ping_binder(&mut self) -> Result<()> { |
| 317 | let status = unsafe { |
| 318 | // Safety: `SpIBinder` guarantees that `self` always contains a |
| 319 | // valid pointer to an `AIBinder`. |
| 320 | // |
| 321 | // This call does not affect ownership of its pointer parameter. |
| 322 | sys::AIBinder_ping(self.as_native_mut()) |
| 323 | }; |
| 324 | status_result(status) |
| 325 | } |
| 326 | |
Janis Danisevskis | 798a09a | 2020-08-18 08:35:38 -0700 | [diff] [blame] | 327 | fn set_requesting_sid(&mut self, enable: bool) { |
Andrew Walbran | 12400d8 | 2021-03-04 17:04:34 +0000 | [diff] [blame] | 328 | unsafe { sys::AIBinder_setRequestingSid(self.as_native_mut(), enable) }; |
Janis Danisevskis | 798a09a | 2020-08-18 08:35:38 -0700 | [diff] [blame] | 329 | } |
| 330 | |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 331 | fn dump<F: AsRawFd>(&mut self, fp: &F, args: &[&str]) -> Result<()> { |
| 332 | let args: Vec<_> = args.iter().map(|a| CString::new(*a).unwrap()).collect(); |
| 333 | let mut arg_ptrs: Vec<_> = args.iter().map(|a| a.as_ptr()).collect(); |
| 334 | let status = unsafe { |
| 335 | // Safety: `SpIBinder` guarantees that `self` always contains a |
| 336 | // valid pointer to an `AIBinder`. `AsRawFd` guarantees that the |
| 337 | // file descriptor parameter is always be a valid open file. The |
| 338 | // `args` pointer parameter is a valid pointer to an array of C |
| 339 | // strings that will outlive the call since `args` lives for the |
| 340 | // whole function scope. |
| 341 | // |
| 342 | // This call does not affect ownership of its binder pointer |
| 343 | // parameter and does not take ownership of the file or args array |
| 344 | // parameters. |
| 345 | sys::AIBinder_dump( |
| 346 | self.as_native_mut(), |
| 347 | fp.as_raw_fd(), |
| 348 | arg_ptrs.as_mut_ptr(), |
| 349 | arg_ptrs.len().try_into().unwrap(), |
| 350 | ) |
| 351 | }; |
| 352 | status_result(status) |
| 353 | } |
| 354 | |
| 355 | fn get_extension(&mut self) -> Result<Option<SpIBinder>> { |
| 356 | let mut out = ptr::null_mut(); |
| 357 | let status = unsafe { |
| 358 | // Safety: `SpIBinder` guarantees that `self` always contains a |
| 359 | // valid pointer to an `AIBinder`. After this call, the `out` |
| 360 | // parameter will be either null, or a valid pointer to an |
| 361 | // `AIBinder`. |
| 362 | // |
| 363 | // This call passes ownership of the out pointer to its caller |
| 364 | // (assuming it is set to a non-null value). |
| 365 | sys::AIBinder_getExtension(self.as_native_mut(), &mut out) |
| 366 | }; |
| 367 | let ibinder = unsafe { |
| 368 | // Safety: The call above guarantees that `out` is either null or a |
| 369 | // valid, owned pointer to an `AIBinder`, both of which are safe to |
| 370 | // pass to `SpIBinder::from_raw`. |
| 371 | SpIBinder::from_raw(out) |
| 372 | }; |
| 373 | |
| 374 | status_result(status)?; |
| 375 | Ok(ibinder) |
| 376 | } |
Andrew Walbran | 12400d8 | 2021-03-04 17:04:34 +0000 | [diff] [blame] | 377 | } |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 378 | |
Andrew Walbran | 12400d8 | 2021-03-04 17:04:34 +0000 | [diff] [blame] | 379 | impl<T: AsNative<sys::AIBinder>> IBinder for T { |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 380 | fn link_to_death(&mut self, recipient: &mut DeathRecipient) -> Result<()> { |
| 381 | status_result(unsafe { |
| 382 | // Safety: `SpIBinder` guarantees that `self` always contains a |
| 383 | // valid pointer to an `AIBinder`. `recipient` can always be |
| 384 | // converted into a valid pointer to an |
Alice Ryhl | ea9d9d2 | 2021-08-27 07:51:30 +0000 | [diff] [blame] | 385 | // `AIBinder_DeathRecipient`. |
| 386 | // |
| 387 | // The cookie is also the correct pointer, and by calling new_cookie, |
| 388 | // we have created a new ref-count to the cookie, which linkToDeath |
| 389 | // takes ownership of. Once the DeathRecipient is unlinked for any |
| 390 | // reason (including if this call fails), the onUnlinked callback |
| 391 | // will consume that ref-count. |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 392 | sys::AIBinder_linkToDeath( |
| 393 | self.as_native_mut(), |
| 394 | recipient.as_native_mut(), |
Alice Ryhl | ea9d9d2 | 2021-08-27 07:51:30 +0000 | [diff] [blame] | 395 | recipient.new_cookie(), |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 396 | ) |
| 397 | }) |
| 398 | } |
| 399 | |
| 400 | fn unlink_to_death(&mut self, recipient: &mut DeathRecipient) -> Result<()> { |
| 401 | status_result(unsafe { |
| 402 | // Safety: `SpIBinder` guarantees that `self` always contains a |
| 403 | // valid pointer to an `AIBinder`. `recipient` can always be |
| 404 | // converted into a valid pointer to an |
Stephen Crane | ddb3e6d | 2020-12-18 13:27:22 -0800 | [diff] [blame] | 405 | // `AIBinder_DeathRecipient`. Any value is safe to pass as the |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 406 | // cookie, although we depend on this value being set by |
| 407 | // `get_cookie` when the death recipient callback is called. |
| 408 | sys::AIBinder_unlinkToDeath( |
| 409 | self.as_native_mut(), |
| 410 | recipient.as_native_mut(), |
| 411 | recipient.get_cookie(), |
| 412 | ) |
| 413 | }) |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | impl Serialize for SpIBinder { |
| 418 | fn serialize(&self, parcel: &mut Parcel) -> Result<()> { |
| 419 | parcel.write_binder(Some(self)) |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | impl SerializeOption for SpIBinder { |
| 424 | fn serialize_option(this: Option<&Self>, parcel: &mut Parcel) -> Result<()> { |
| 425 | parcel.write_binder(this) |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | impl SerializeArray for SpIBinder {} |
| 430 | impl SerializeArray for Option<&SpIBinder> {} |
| 431 | |
| 432 | impl Deserialize for SpIBinder { |
| 433 | fn deserialize(parcel: &Parcel) -> Result<SpIBinder> { |
Andrei Homescu | 3281437 | 2020-08-20 15:36:08 -0700 | [diff] [blame] | 434 | parcel |
| 435 | .read_binder() |
| 436 | .transpose() |
| 437 | .unwrap_or(Err(StatusCode::UNEXPECTED_NULL)) |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 438 | } |
| 439 | } |
| 440 | |
| 441 | impl DeserializeOption for SpIBinder { |
| 442 | fn deserialize_option(parcel: &Parcel) -> Result<Option<SpIBinder>> { |
| 443 | parcel.read_binder() |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | impl DeserializeArray for SpIBinder {} |
| 448 | impl DeserializeArray for Option<SpIBinder> {} |
| 449 | |
| 450 | /// A weak reference to a Binder remote object. |
| 451 | /// |
Andrew Walbran | 8fe3ecc | 2020-12-15 11:29:58 +0000 | [diff] [blame] | 452 | /// This struct encapsulates the generic C++ `wp<IBinder>` class. This wrapper |
| 453 | /// is untyped; typed interface access is implemented by the AIDL compiler. |
Alice Ryhl | 8dde9bc | 2021-08-16 16:57:10 +0000 | [diff] [blame] | 454 | pub struct WpIBinder(ptr::NonNull<sys::AIBinder_Weak>); |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 455 | |
Andrew Walbran | 8fe3ecc | 2020-12-15 11:29:58 +0000 | [diff] [blame] | 456 | impl fmt::Debug for WpIBinder { |
| 457 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 458 | f.pad("WpIBinder") |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | /// # Safety |
| 463 | /// |
Stephen Crane | f03fe3d | 2021-06-25 15:05:00 -0700 | [diff] [blame] | 464 | /// A `WpIBinder` is an immutable handle to a C++ IBinder, which is thread-safe. |
Andrew Walbran | 8fe3ecc | 2020-12-15 11:29:58 +0000 | [diff] [blame] | 465 | unsafe impl Send for WpIBinder {} |
| 466 | |
Stephen Crane | f03fe3d | 2021-06-25 15:05:00 -0700 | [diff] [blame] | 467 | /// # Safety |
| 468 | /// |
| 469 | /// A `WpIBinder` is an immutable handle to a C++ IBinder, which is thread-safe. |
| 470 | unsafe impl Sync for WpIBinder {} |
| 471 | |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 472 | impl WpIBinder { |
| 473 | /// Create a new weak reference from an object that can be converted into a |
| 474 | /// raw `AIBinder` pointer. |
Andrew Walbran | 8fe3ecc | 2020-12-15 11:29:58 +0000 | [diff] [blame] | 475 | fn new<B: AsNative<sys::AIBinder>>(binder: &mut B) -> WpIBinder { |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 476 | let ptr = unsafe { |
| 477 | // Safety: `SpIBinder` guarantees that `binder` always contains a |
| 478 | // valid pointer to an `AIBinder`. |
| 479 | sys::AIBinder_Weak_new(binder.as_native_mut()) |
| 480 | }; |
Alice Ryhl | 8dde9bc | 2021-08-16 16:57:10 +0000 | [diff] [blame] | 481 | Self(ptr::NonNull::new(ptr).expect("Unexpected null pointer from AIBinder_Weak_new")) |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 482 | } |
Stephen Crane | 994a0f0 | 2020-08-11 14:47:29 -0700 | [diff] [blame] | 483 | |
| 484 | /// Promote this weak reference to a strong reference to the binder object. |
| 485 | pub fn promote(&self) -> Option<SpIBinder> { |
| 486 | unsafe { |
| 487 | // Safety: `WpIBinder` always contains a valid weak reference, so we |
| 488 | // can pass this pointer to `AIBinder_Weak_promote`. Returns either |
| 489 | // null or an AIBinder owned by the caller, both of which are valid |
| 490 | // to pass to `SpIBinder::from_raw`. |
Alice Ryhl | 8dde9bc | 2021-08-16 16:57:10 +0000 | [diff] [blame] | 491 | let ptr = sys::AIBinder_Weak_promote(self.0.as_ptr()); |
Stephen Crane | 994a0f0 | 2020-08-11 14:47:29 -0700 | [diff] [blame] | 492 | SpIBinder::from_raw(ptr) |
| 493 | } |
| 494 | } |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 495 | } |
| 496 | |
Stephen Crane | ddb3e6d | 2020-12-18 13:27:22 -0800 | [diff] [blame] | 497 | impl Clone for WpIBinder { |
| 498 | fn clone(&self) -> Self { |
| 499 | let ptr = unsafe { |
| 500 | // Safety: WpIBinder always holds a valid `AIBinder_Weak` pointer, |
| 501 | // so this pointer is always safe to pass to `AIBinder_Weak_clone` |
| 502 | // (although null is also a safe value to pass to this API). |
| 503 | // |
| 504 | // We get ownership of the returned pointer, so can construct a new |
| 505 | // WpIBinder object from it. |
Alice Ryhl | 8dde9bc | 2021-08-16 16:57:10 +0000 | [diff] [blame] | 506 | sys::AIBinder_Weak_clone(self.0.as_ptr()) |
Stephen Crane | ddb3e6d | 2020-12-18 13:27:22 -0800 | [diff] [blame] | 507 | }; |
Alice Ryhl | 8dde9bc | 2021-08-16 16:57:10 +0000 | [diff] [blame] | 508 | Self(ptr::NonNull::new(ptr).expect("Unexpected null pointer from AIBinder_Weak_clone")) |
Stephen Crane | ddb3e6d | 2020-12-18 13:27:22 -0800 | [diff] [blame] | 509 | } |
| 510 | } |
| 511 | |
| 512 | impl Ord for WpIBinder { |
| 513 | fn cmp(&self, other: &Self) -> Ordering { |
| 514 | let less_than = unsafe { |
| 515 | // Safety: WpIBinder always holds a valid `AIBinder_Weak` pointer, |
| 516 | // so this pointer is always safe to pass to `AIBinder_Weak_lt` |
| 517 | // (null is also safe to pass to this function, but we should never |
| 518 | // do that). |
Alice Ryhl | 8dde9bc | 2021-08-16 16:57:10 +0000 | [diff] [blame] | 519 | sys::AIBinder_Weak_lt(self.0.as_ptr(), other.0.as_ptr()) |
Stephen Crane | ddb3e6d | 2020-12-18 13:27:22 -0800 | [diff] [blame] | 520 | }; |
| 521 | let greater_than = unsafe { |
| 522 | // Safety: WpIBinder always holds a valid `AIBinder_Weak` pointer, |
| 523 | // so this pointer is always safe to pass to `AIBinder_Weak_lt` |
| 524 | // (null is also safe to pass to this function, but we should never |
| 525 | // do that). |
Alice Ryhl | 8dde9bc | 2021-08-16 16:57:10 +0000 | [diff] [blame] | 526 | sys::AIBinder_Weak_lt(other.0.as_ptr(), self.0.as_ptr()) |
Stephen Crane | ddb3e6d | 2020-12-18 13:27:22 -0800 | [diff] [blame] | 527 | }; |
| 528 | if !less_than && !greater_than { |
| 529 | Ordering::Equal |
| 530 | } else if less_than { |
| 531 | Ordering::Less |
| 532 | } else { |
| 533 | Ordering::Greater |
| 534 | } |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | impl PartialOrd for WpIBinder { |
| 539 | fn partial_cmp(&self, other: &Self) -> Option<Ordering> { |
| 540 | Some(self.cmp(other)) |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | impl PartialEq for WpIBinder { |
| 545 | fn eq(&self, other: &Self) -> bool { |
| 546 | self.cmp(other) == Ordering::Equal |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | impl Eq for WpIBinder {} |
| 551 | |
Andrew Walbran | 5e8dfa3 | 2020-12-16 12:50:06 +0000 | [diff] [blame] | 552 | impl Drop for WpIBinder { |
| 553 | fn drop(&mut self) { |
| 554 | unsafe { |
| 555 | // Safety: WpIBinder always holds a valid `AIBinder_Weak` pointer, so we |
| 556 | // know this pointer is safe to pass to `AIBinder_Weak_delete` here. |
Alice Ryhl | 8dde9bc | 2021-08-16 16:57:10 +0000 | [diff] [blame] | 557 | sys::AIBinder_Weak_delete(self.0.as_ptr()); |
Andrew Walbran | 5e8dfa3 | 2020-12-16 12:50:06 +0000 | [diff] [blame] | 558 | } |
| 559 | } |
| 560 | } |
| 561 | |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 562 | /// Rust wrapper around DeathRecipient objects. |
Alice Ryhl | ea9d9d2 | 2021-08-27 07:51:30 +0000 | [diff] [blame] | 563 | /// |
| 564 | /// The cookie in this struct represents an Arc<F> for the owned callback. |
| 565 | /// This struct owns a ref-count of it, and so does every binder that we |
| 566 | /// have been linked with. |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 567 | #[repr(C)] |
| 568 | pub struct DeathRecipient { |
| 569 | recipient: *mut sys::AIBinder_DeathRecipient, |
Alice Ryhl | ea9d9d2 | 2021-08-27 07:51:30 +0000 | [diff] [blame] | 570 | cookie: *mut c_void, |
| 571 | vtable: &'static DeathRecipientVtable, |
| 572 | } |
| 573 | |
| 574 | struct DeathRecipientVtable { |
| 575 | cookie_incr_refcount: unsafe extern "C" fn(*mut c_void), |
| 576 | cookie_decr_refcount: unsafe extern "C" fn(*mut c_void), |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | impl DeathRecipient { |
| 580 | /// Create a new death recipient that will call the given callback when its |
| 581 | /// associated object dies. |
| 582 | pub fn new<F>(callback: F) -> DeathRecipient |
| 583 | where |
Alice Ryhl | ea9d9d2 | 2021-08-27 07:51:30 +0000 | [diff] [blame] | 584 | F: Fn() + Send + Sync + 'static, |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 585 | { |
Alice Ryhl | ea9d9d2 | 2021-08-27 07:51:30 +0000 | [diff] [blame] | 586 | let callback: *const F = Arc::into_raw(Arc::new(callback)); |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 587 | let recipient = unsafe { |
| 588 | // Safety: The function pointer is a valid death recipient callback. |
| 589 | // |
| 590 | // This call returns an owned `AIBinder_DeathRecipient` pointer |
| 591 | // which must be destroyed via `AIBinder_DeathRecipient_delete` when |
| 592 | // no longer needed. |
| 593 | sys::AIBinder_DeathRecipient_new(Some(Self::binder_died::<F>)) |
| 594 | }; |
Alice Ryhl | ea9d9d2 | 2021-08-27 07:51:30 +0000 | [diff] [blame] | 595 | unsafe { |
| 596 | // Safety: The function pointer is a valid onUnlinked callback. |
| 597 | // |
| 598 | // All uses of linkToDeath in this file correctly increment the |
| 599 | // ref-count that this onUnlinked callback will decrement. |
| 600 | sys::AIBinder_DeathRecipient_setOnUnlinked(recipient, Some(Self::cookie_decr_refcount::<F>)); |
| 601 | } |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 602 | DeathRecipient { |
| 603 | recipient, |
Alice Ryhl | ea9d9d2 | 2021-08-27 07:51:30 +0000 | [diff] [blame] | 604 | cookie: callback as *mut c_void, |
| 605 | vtable: &DeathRecipientVtable { |
| 606 | cookie_incr_refcount: Self::cookie_incr_refcount::<F>, |
| 607 | cookie_decr_refcount: Self::cookie_decr_refcount::<F>, |
| 608 | }, |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 609 | } |
| 610 | } |
| 611 | |
Alice Ryhl | ea9d9d2 | 2021-08-27 07:51:30 +0000 | [diff] [blame] | 612 | /// Increment the ref-count for the cookie and return it. |
| 613 | /// |
| 614 | /// # Safety |
| 615 | /// |
| 616 | /// The caller must handle the returned ref-count correctly. |
| 617 | unsafe fn new_cookie(&self) -> *mut c_void { |
| 618 | (self.vtable.cookie_incr_refcount)(self.cookie); |
| 619 | |
| 620 | // Return a raw pointer with ownership of a ref-count |
| 621 | self.cookie |
| 622 | } |
| 623 | |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 624 | /// Get the opaque cookie that identifies this death recipient. |
| 625 | /// |
| 626 | /// This cookie will be used to link and unlink this death recipient to a |
| 627 | /// binder object and will be passed to the `binder_died` callback as an |
| 628 | /// opaque userdata pointer. |
| 629 | fn get_cookie(&self) -> *mut c_void { |
Alice Ryhl | ea9d9d2 | 2021-08-27 07:51:30 +0000 | [diff] [blame] | 630 | self.cookie |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | /// Callback invoked from C++ when the binder object dies. |
| 634 | /// |
| 635 | /// # Safety |
| 636 | /// |
Alice Ryhl | ea9d9d2 | 2021-08-27 07:51:30 +0000 | [diff] [blame] | 637 | /// The `cookie` parameter must be the cookie for an Arc<F> and |
| 638 | /// the caller must hold a ref-count to it. |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 639 | unsafe extern "C" fn binder_died<F>(cookie: *mut c_void) |
| 640 | where |
Alice Ryhl | ea9d9d2 | 2021-08-27 07:51:30 +0000 | [diff] [blame] | 641 | F: Fn() + Send + Sync + 'static, |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 642 | { |
Alice Ryhl | ea9d9d2 | 2021-08-27 07:51:30 +0000 | [diff] [blame] | 643 | let callback = (cookie as *const F).as_ref().unwrap(); |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 644 | callback(); |
| 645 | } |
Alice Ryhl | ea9d9d2 | 2021-08-27 07:51:30 +0000 | [diff] [blame] | 646 | |
| 647 | /// Callback that decrements the ref-count. |
| 648 | /// This is invoked from C++ when a binder is unlinked. |
| 649 | /// |
| 650 | /// # Safety |
| 651 | /// |
| 652 | /// The `cookie` parameter must be the cookie for an Arc<F> and |
| 653 | /// the owner must give up a ref-count to it. |
| 654 | unsafe extern "C" fn cookie_decr_refcount<F>(cookie: *mut c_void) |
| 655 | where |
| 656 | F: Fn() + Send + Sync + 'static, |
| 657 | { |
| 658 | drop(Arc::from_raw(cookie as *const F)); |
| 659 | } |
| 660 | |
| 661 | /// Callback that increments the ref-count. |
| 662 | /// |
| 663 | /// # Safety |
| 664 | /// |
| 665 | /// The `cookie` parameter must be the cookie for an Arc<F> and |
| 666 | /// the owner must handle the created ref-count properly. |
| 667 | unsafe extern "C" fn cookie_incr_refcount<F>(cookie: *mut c_void) |
| 668 | where |
| 669 | F: Fn() + Send + Sync + 'static, |
| 670 | { |
| 671 | let arc = mem::ManuallyDrop::new(Arc::from_raw(cookie as *const F)); |
| 672 | mem::forget(Arc::clone(&arc)); |
| 673 | } |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | /// # Safety |
| 677 | /// |
| 678 | /// A `DeathRecipient` is always constructed with a valid raw pointer to an |
| 679 | /// `AIBinder_DeathRecipient`, so it is always type-safe to extract this |
| 680 | /// pointer. |
| 681 | unsafe impl AsNative<sys::AIBinder_DeathRecipient> for DeathRecipient { |
| 682 | fn as_native(&self) -> *const sys::AIBinder_DeathRecipient { |
| 683 | self.recipient |
| 684 | } |
| 685 | |
| 686 | fn as_native_mut(&mut self) -> *mut sys::AIBinder_DeathRecipient { |
| 687 | self.recipient |
| 688 | } |
| 689 | } |
| 690 | |
| 691 | impl Drop for DeathRecipient { |
| 692 | fn drop(&mut self) { |
| 693 | unsafe { |
| 694 | // Safety: `self.recipient` is always a valid, owned |
| 695 | // `AIBinder_DeathRecipient` pointer returned by |
| 696 | // `AIBinder_DeathRecipient_new` when `self` was created. This |
| 697 | // delete method can only be called once when `self` is dropped. |
| 698 | sys::AIBinder_DeathRecipient_delete(self.recipient); |
Alice Ryhl | ea9d9d2 | 2021-08-27 07:51:30 +0000 | [diff] [blame] | 699 | |
| 700 | // Safety: We own a ref-count to the cookie, and so does every |
| 701 | // linked binder. This call gives up our ref-count. The linked |
| 702 | // binders should already have given up their ref-count, or should |
| 703 | // do so shortly. |
| 704 | (self.vtable.cookie_decr_refcount)(self.cookie) |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 705 | } |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | /// Generic interface to remote binder objects. |
| 710 | /// |
| 711 | /// Corresponds to the C++ `BpInterface` class. |
| 712 | pub trait Proxy: Sized + Interface { |
| 713 | /// The Binder interface descriptor string. |
| 714 | /// |
| 715 | /// This string is a unique identifier for a Binder interface, and should be |
| 716 | /// the same between all implementations of that interface. |
| 717 | fn get_descriptor() -> &'static str; |
| 718 | |
| 719 | /// Create a new interface from the given proxy, if it matches the expected |
| 720 | /// type of this interface. |
| 721 | fn from_binder(binder: SpIBinder) -> Result<Self>; |
| 722 | } |
| 723 | |
| 724 | /// # Safety |
| 725 | /// |
| 726 | /// This is a convenience method that wraps `AsNative` for `SpIBinder` to allow |
| 727 | /// invocation of `IBinder` methods directly from `Interface` objects. It shares |
| 728 | /// the same safety as the implementation for `SpIBinder`. |
| 729 | unsafe impl<T: Proxy> AsNative<sys::AIBinder> for T { |
| 730 | fn as_native(&self) -> *const sys::AIBinder { |
| 731 | self.as_binder().as_native() |
| 732 | } |
| 733 | |
| 734 | fn as_native_mut(&mut self) -> *mut sys::AIBinder { |
| 735 | self.as_binder().as_native_mut() |
| 736 | } |
| 737 | } |
| 738 | |
| 739 | /// Retrieve an existing service, blocking for a few seconds if it doesn't yet |
| 740 | /// exist. |
| 741 | pub fn get_service(name: &str) -> Option<SpIBinder> { |
| 742 | let name = CString::new(name).ok()?; |
| 743 | unsafe { |
| 744 | // Safety: `AServiceManager_getService` returns either a null pointer or |
| 745 | // a valid pointer to an owned `AIBinder`. Either of these values is |
| 746 | // safe to pass to `SpIBinder::from_raw`. |
| 747 | SpIBinder::from_raw(sys::AServiceManager_getService(name.as_ptr())) |
| 748 | } |
| 749 | } |
| 750 | |
Andrew Walbran | c3ce5c3 | 2021-06-03 16:15:56 +0000 | [diff] [blame] | 751 | /// Retrieve an existing service, or start it if it is configured as a dynamic |
| 752 | /// service and isn't yet started. |
| 753 | pub fn wait_for_service(name: &str) -> Option<SpIBinder> { |
| 754 | let name = CString::new(name).ok()?; |
| 755 | unsafe { |
| 756 | // Safety: `AServiceManager_waitforService` returns either a null |
| 757 | // pointer or a valid pointer to an owned `AIBinder`. Either of these |
| 758 | // values is safe to pass to `SpIBinder::from_raw`. |
| 759 | SpIBinder::from_raw(sys::AServiceManager_waitForService(name.as_ptr())) |
| 760 | } |
| 761 | } |
| 762 | |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 763 | /// Retrieve an existing service for a particular interface, blocking for a few |
| 764 | /// seconds if it doesn't yet exist. |
Stephen Crane | ddb3e6d | 2020-12-18 13:27:22 -0800 | [diff] [blame] | 765 | pub fn get_interface<T: FromIBinder + ?Sized>(name: &str) -> Result<Strong<T>> { |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 766 | let service = get_service(name); |
| 767 | match service { |
| 768 | Some(service) => FromIBinder::try_from(service), |
| 769 | None => Err(StatusCode::NAME_NOT_FOUND), |
| 770 | } |
| 771 | } |
| 772 | |
Andrew Walbran | c3ce5c3 | 2021-06-03 16:15:56 +0000 | [diff] [blame] | 773 | /// Retrieve an existing service for a particular interface, or start it if it |
| 774 | /// is configured as a dynamic service and isn't yet started. |
| 775 | pub fn wait_for_interface<T: FromIBinder + ?Sized>(name: &str) -> Result<Strong<T>> { |
| 776 | let service = wait_for_service(name); |
| 777 | match service { |
| 778 | Some(service) => FromIBinder::try_from(service), |
| 779 | None => Err(StatusCode::NAME_NOT_FOUND), |
| 780 | } |
| 781 | } |
| 782 | |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 783 | /// # Safety |
| 784 | /// |
| 785 | /// `SpIBinder` guarantees that `binder` always contains a valid pointer to an |
| 786 | /// `AIBinder`, so we can trivially extract this pointer here. |
| 787 | unsafe impl AsNative<sys::AIBinder> for SpIBinder { |
| 788 | fn as_native(&self) -> *const sys::AIBinder { |
Alice Ryhl | 8dde9bc | 2021-08-16 16:57:10 +0000 | [diff] [blame] | 789 | self.0.as_ptr() |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | fn as_native_mut(&mut self) -> *mut sys::AIBinder { |
Alice Ryhl | 8dde9bc | 2021-08-16 16:57:10 +0000 | [diff] [blame] | 793 | self.0.as_ptr() |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 794 | } |
| 795 | } |