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, |
| 53 | //! _data: &Parcel, |
| 54 | //! reply: &mut Parcel, |
| 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] |
| 97 | mod proxy; |
| 98 | |
| 99 | #[macro_use] |
| 100 | mod binder; |
Alice Ryhl | 05f5a2c | 2021-09-15 12:56:10 +0000 | [diff] [blame^] | 101 | mod binder_async; |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 102 | mod error; |
| 103 | mod native; |
| 104 | mod state; |
| 105 | |
Stephen Crane | 994a0f0 | 2020-08-11 14:47:29 -0700 | [diff] [blame] | 106 | use binder_ndk_sys as sys; |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 107 | |
| 108 | pub mod parcel; |
| 109 | |
| 110 | pub use crate::binder::{ |
Andrew Walbran | 88eca4f | 2021-04-13 14:26:01 +0000 | [diff] [blame] | 111 | BinderFeatures, FromIBinder, IBinder, IBinderInternal, Interface, InterfaceClass, Remotable, |
| 112 | Stability, Strong, TransactionCode, TransactionFlags, Weak, FIRST_CALL_TRANSACTION, |
| 113 | FLAG_CLEAR_BUF, FLAG_ONEWAY, FLAG_PRIVATE_LOCAL, LAST_CALL_TRANSACTION, |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 114 | }; |
Alice Ryhl | 05f5a2c | 2021-09-15 12:56:10 +0000 | [diff] [blame^] | 115 | pub use crate::binder_async::{BoxFuture, BinderAsyncPool}; |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 116 | pub use error::{status_t, ExceptionCode, Result, Status, StatusCode}; |
Alan Stokes | 23fdfcd | 2021-09-09 11:19:24 +0100 | [diff] [blame] | 117 | pub use native::{add_service, force_lazy_services_persist, register_lazy_service, Binder}; |
Alice Ryhl | 268458c | 2021-09-15 12:56:10 +0000 | [diff] [blame] | 118 | pub use parcel::{OwnedParcel, Parcel}; |
Andrew Walbran | c3ce5c3 | 2021-06-03 16:15:56 +0000 | [diff] [blame] | 119 | pub use proxy::{get_interface, get_service, wait_for_interface, wait_for_service}; |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 120 | pub use proxy::{AssociateClass, DeathRecipient, Proxy, SpIBinder, WpIBinder}; |
| 121 | pub use state::{ProcessState, ThreadState}; |
| 122 | |
Victor Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 123 | /// Unstable, in-development API that only allowlisted clients are allowed to use. |
| 124 | pub mod unstable_api { |
| 125 | pub use crate::binder::AsNative; |
| 126 | pub use crate::proxy::unstable_api::new_spibinder; |
| 127 | pub use crate::sys::AIBinder; |
| 128 | } |
| 129 | |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 130 | /// The public API usable outside AIDL-generated interface crates. |
| 131 | pub mod public_api { |
Andrei Homescu | ea40621 | 2021-09-03 02:55:00 +0000 | [diff] [blame] | 132 | pub use super::parcel::{ParcelFileDescriptor, ParcelableHolder}; |
Alan Stokes | 23fdfcd | 2021-09-09 11:19:24 +0100 | [diff] [blame] | 133 | pub use super::{ |
| 134 | add_service, force_lazy_services_persist, get_interface, register_lazy_service, |
| 135 | wait_for_interface, |
| 136 | }; |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 137 | pub use super::{ |
Alice Ryhl | 05f5a2c | 2021-09-15 12:56:10 +0000 | [diff] [blame^] | 138 | BinderAsyncPool, BinderFeatures, BoxFuture, DeathRecipient, ExceptionCode, IBinder, |
| 139 | Interface, ProcessState, SpIBinder, Status, StatusCode, Strong, ThreadState, Weak, |
| 140 | WpIBinder, |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | /// Binder result containing a [`Status`] on error. |
| 144 | pub type Result<T> = std::result::Result<T, Status>; |
| 145 | } |