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 | //! Safe Rust interface to Android `libbinder`. |
| 18 | //! |
| 19 | //! This crate is primarily designed as an target for a Rust AIDL compiler |
| 20 | //! backend, and should generally not be used directly by users. It is built on |
| 21 | //! top of the binder NDK library to be usable by APEX modules, and therefore |
| 22 | //! only exposes functionality available in the NDK interface. |
| 23 | //! |
| 24 | //! # Example |
| 25 | //! |
| 26 | //! The following example illustrates how the AIDL backend will use this crate. |
| 27 | //! |
| 28 | //! ``` |
| 29 | //! use binder::{ |
| 30 | //! declare_binder_interface, Binder, IBinder, Interface, Remotable, Parcel, SpIBinder, |
| 31 | //! StatusCode, TransactionCode, |
| 32 | //! }; |
| 33 | //! |
| 34 | //! // Generated by AIDL compiler |
| 35 | //! pub trait ITest: Interface { |
| 36 | //! fn test(&self) -> binder::Result<String>; |
| 37 | //! } |
| 38 | //! |
| 39 | //! // Creates a new local (native) service object, BnTest, and a remote proxy |
| 40 | //! // object, BpTest, that are the typed interfaces for their respective ends |
| 41 | //! // of the binder transaction. Generated by AIDL compiler. |
| 42 | //! declare_binder_interface! { |
| 43 | //! ITest["android.os.ITest"] { |
| 44 | //! native: BnTest(on_transact), |
| 45 | //! proxy: BpTest, |
| 46 | //! } |
| 47 | //! } |
| 48 | //! |
| 49 | //! // Generated by AIDL compiler |
| 50 | //! fn on_transact( |
| 51 | //! service: &dyn ITest, |
| 52 | //! code: TransactionCode, |
Alice Ryhl | 8618c48 | 2021-11-09 15:35:35 +0000 | [diff] [blame] | 53 | //! _data: &BorrowedParcel, |
| 54 | //! reply: &mut BorrowedParcel, |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 55 | //! ) -> binder::Result<()> { |
| 56 | //! match code { |
| 57 | //! SpIBinder::FIRST_CALL_TRANSACTION => { |
| 58 | //! reply.write(&service.test()?)?; |
| 59 | //! Ok(()) |
| 60 | //! } |
| 61 | //! _ => Err(StatusCode::UNKNOWN_TRANSACTION), |
| 62 | //! } |
| 63 | //! } |
| 64 | //! |
| 65 | //! // Generated by AIDL compiler |
| 66 | //! impl ITest for Binder<BnTest> { |
| 67 | //! fn test(&self) -> binder::Result<String> { |
| 68 | //! self.0.test() |
| 69 | //! } |
| 70 | //! } |
| 71 | //! |
| 72 | //! // Generated by AIDL compiler |
| 73 | //! impl ITest for BpTest { |
| 74 | //! fn test(&self) -> binder::Result<String> { |
| 75 | //! let reply = self |
| 76 | //! .as_binder() |
| 77 | //! .transact(SpIBinder::FIRST_CALL_TRANSACTION, 0, |_| Ok(()))?; |
| 78 | //! reply.read() |
| 79 | //! } |
| 80 | //! } |
| 81 | //! |
| 82 | //! // User implemented: |
| 83 | //! |
| 84 | //! // Local implementation of the ITest remotable interface. |
| 85 | //! struct TestService; |
| 86 | //! |
| 87 | //! impl Interface for TestService {} |
| 88 | //! |
| 89 | //! impl ITest for TestService { |
| 90 | //! fn test(&self) -> binder::Result<String> { |
| 91 | //! Ok("testing service".to_string()) |
| 92 | //! } |
| 93 | //! } |
| 94 | //! ``` |
| 95 | |
| 96 | #[macro_use] |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 97 | mod binder; |
Alice Ryhl | 05f5a2c | 2021-09-15 12:56:10 +0000 | [diff] [blame] | 98 | mod binder_async; |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 99 | mod error; |
| 100 | mod native; |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 101 | mod parcel; |
Andrew Walbran | 6fe5d64 | 2023-01-19 11:27:01 +0000 | [diff] [blame] | 102 | mod proxy; |
Matthew Maurer | 75d7112 | 2024-10-09 22:20:41 +0000 | [diff] [blame] | 103 | #[cfg(not(any(trusty, android_ndk)))] |
Andrei Homescu | c5bf3f8 | 2024-03-29 04:45:59 +0000 | [diff] [blame] | 104 | mod service; |
Matthew Maurer | 75d7112 | 2024-10-09 22:20:41 +0000 | [diff] [blame] | 105 | #[cfg(not(any(trusty, android_ndk)))] |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 106 | mod state; |
Matthew Maurer | 75d7112 | 2024-10-09 22:20:41 +0000 | [diff] [blame] | 107 | #[cfg(not(any(android_vendor, android_ndk, android_vndk)))] |
Devin Moore | bbd71b1 | 2024-08-09 23:20:03 +0000 | [diff] [blame] | 108 | mod system_only; |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 109 | |
Stephen Crane | 994a0f0 | 2020-08-11 14:47:29 -0700 | [diff] [blame] | 110 | use binder_ndk_sys as sys; |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 111 | |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 112 | pub use crate::binder_async::{BinderAsyncPool, BoxFuture}; |
Matthew Maurer | e268a9f | 2022-07-26 09:31:30 -0700 | [diff] [blame] | 113 | pub use binder::{BinderFeatures, FromIBinder, IBinder, Interface, Strong, Weak}; |
Jiyong Park | e731c49 | 2023-08-04 11:36:41 +0900 | [diff] [blame] | 114 | pub use error::{ExceptionCode, IntoBinderResult, Status, StatusCode}; |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 115 | pub use parcel::{ParcelFileDescriptor, Parcelable, ParcelableHolder}; |
Andrei Homescu | c5bf3f8 | 2024-03-29 04:45:59 +0000 | [diff] [blame] | 116 | pub use proxy::{DeathRecipient, SpIBinder, WpIBinder}; |
Matthew Maurer | 75d7112 | 2024-10-09 22:20:41 +0000 | [diff] [blame] | 117 | #[cfg(not(any(trusty, android_ndk)))] |
Andrei Homescu | c5bf3f8 | 2024-03-29 04:45:59 +0000 | [diff] [blame] | 118 | pub use service::{ |
Frederick Mayle | 68f34bf | 2024-05-08 11:04:10 -0700 | [diff] [blame] | 119 | add_service, check_interface, check_service, force_lazy_services_persist, |
Matthew Maurer | 75d7112 | 2024-10-09 22:20:41 +0000 | [diff] [blame] | 120 | get_declared_instances, is_declared, is_handling_transaction, register_lazy_service, |
| 121 | wait_for_interface, wait_for_service, LazyServiceGuard, |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 122 | }; |
Matthew Maurer | 75d7112 | 2024-10-09 22:20:41 +0000 | [diff] [blame] | 123 | #[cfg(not(any(trusty, android_ndk)))] |
| 124 | #[allow(deprecated)] |
| 125 | pub use service::{get_interface, get_service}; |
| 126 | #[cfg(not(any(trusty, android_ndk)))] |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 127 | pub use state::{ProcessState, ThreadState}; |
Matthew Maurer | 75d7112 | 2024-10-09 22:20:41 +0000 | [diff] [blame] | 128 | #[cfg(not(any(android_vendor, android_vndk, android_ndk)))] |
Devin Moore | 0555fbf | 2024-08-29 15:51:50 +0000 | [diff] [blame] | 129 | pub use system_only::{delegate_accessor, Accessor, ConnectionInfo}; |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 130 | |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 131 | /// Binder result containing a [`Status`] on error. |
| 132 | pub type Result<T> = std::result::Result<T, Status>; |
| 133 | |
| 134 | /// Advanced Binder APIs needed internally by AIDL or when manually using Binder |
| 135 | /// without AIDL. |
| 136 | pub mod binder_impl { |
| 137 | pub use crate::binder::{ |
Frederick Mayle | e254323 | 2024-09-16 21:05:49 -0700 | [diff] [blame] | 138 | IBinderInternal, InterfaceClass, LocalStabilityType, Remotable, Stability, StabilityType, |
| 139 | ToAsyncInterface, ToSyncInterface, TransactionCode, TransactionFlags, VintfStabilityType, |
Matthew Maurer | 75d7112 | 2024-10-09 22:20:41 +0000 | [diff] [blame] | 140 | FIRST_CALL_TRANSACTION, FLAG_ONEWAY, LAST_CALL_TRANSACTION, |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 141 | }; |
Matthew Maurer | 75d7112 | 2024-10-09 22:20:41 +0000 | [diff] [blame] | 142 | #[cfg(not(android_ndk))] |
| 143 | pub use crate::binder::{FLAG_CLEAR_BUF, FLAG_PRIVATE_LOCAL}; |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 144 | pub use crate::binder_async::BinderAsyncRuntime; |
| 145 | pub use crate::error::status_t; |
| 146 | pub use crate::native::Binder; |
| 147 | pub use crate::parcel::{ |
| 148 | BorrowedParcel, Deserialize, DeserializeArray, DeserializeOption, Parcel, |
Andrew Walbran | e9573af | 2024-01-11 16:34:16 +0000 | [diff] [blame] | 149 | ParcelableMetadata, Serialize, SerializeArray, SerializeOption, UnstructuredParcelable, |
| 150 | NON_NULL_PARCELABLE_FLAG, NULL_PARCELABLE_FLAG, |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 151 | }; |
| 152 | pub use crate::proxy::{AssociateClass, Proxy}; |
| 153 | } |
| 154 | |
Victor Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 155 | /// Unstable, in-development API that only allowlisted clients are allowed to use. |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 156 | #[doc(hidden)] |
Victor Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 157 | pub mod unstable_api { |
| 158 | pub use crate::binder::AsNative; |
Andrew Walbran | 43bddb6 | 2023-09-01 16:43:09 +0100 | [diff] [blame] | 159 | pub use crate::error::status_result; |
Victor Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 160 | pub use crate::proxy::unstable_api::new_spibinder; |
| 161 | pub use crate::sys::AIBinder; |
Pawan Wagh | 4c8aa33 | 2022-10-12 21:10:41 +0000 | [diff] [blame] | 162 | pub use crate::sys::AParcel; |
Victor Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 163 | } |