Expand description
Safe Rust interface to Android libbinder
.
This crate is primarily designed as an target for a Rust AIDL compiler backend, and should generally not be used directly by users. It is built on top of the binder NDK library to be usable by APEX modules, and therefore only exposes functionality available in the NDK interface.
§Example
The following example illustrates how the AIDL backend will use this crate.
use binder::{
declare_binder_interface, Binder, IBinder, Interface, Remotable, Parcel, SpIBinder,
StatusCode, TransactionCode,
};
// Generated by AIDL compiler
pub trait ITest: Interface {
fn test(&self) -> binder::Result<String>;
}
// Creates a new local (native) service object, BnTest, and a remote proxy
// object, BpTest, that are the typed interfaces for their respective ends
// of the binder transaction. Generated by AIDL compiler.
declare_binder_interface! {
ITest["android.os.ITest"] {
native: BnTest(on_transact),
proxy: BpTest,
}
}
// Generated by AIDL compiler
fn on_transact(
service: &dyn ITest,
code: TransactionCode,
_data: &BorrowedParcel,
reply: &mut BorrowedParcel,
) -> binder::Result<()> {
match code {
SpIBinder::FIRST_CALL_TRANSACTION => {
reply.write(&service.test()?)?;
Ok(())
}
_ => Err(StatusCode::UNKNOWN_TRANSACTION),
}
}
// Generated by AIDL compiler
impl ITest for Binder<BnTest> {
fn test(&self) -> binder::Result<String> {
self.0.test()
}
}
// Generated by AIDL compiler
impl ITest for BpTest {
fn test(&self) -> binder::Result<String> {
let reply = self
.as_binder()
.transact(SpIBinder::FIRST_CALL_TRANSACTION, 0, |_| Ok(()))?;
reply.read()
}
}
// User implemented:
// Local implementation of the ITest remotable interface.
struct TestService;
impl Interface for TestService {}
impl ITest for TestService {
fn test(&self) -> binder::Result<String> {
Ok("testing service".to_string())
}
}
Modules§
- Advanced Binder APIs needed internally by AIDL or when manually using Binder without AIDL.
Macros§
- Declare an AIDL enumeration.
- Declare typed interfaces for a binder object.
- Implement
Deserialize
trait and friends for a parcelable - Implement
Deserialize
trait and friends for an unstructured parcelable - Implement
Serialize
trait and friends for a parcelable - Implements
Serialize
trait and friends for an unstructured parcelable.
Structs§
- The features to enable when creating a native Binder.
- Rust wrapper around DeathRecipient objects.
- An RAII object to ensure a process which registers lazy services is not killed. During the lifetime of any of these objects the service manager will not kill the process even if none of its lazy services are in use.
- Rust version of the Java class android.os.ParcelFileDescriptor
- A container that can hold any arbitrary
Parcelable
. - Static utility functions to manage Binder process state.
- A strong reference to a Binder remote object.
- High-level binder status object that encapsulates a standard way to keep track of and chain binder errors along with service specific errors.
- Strong reference to a binder object
- Static utility functions to manage Binder thread state.
- Weak reference to a binder object
- A weak reference to a Binder remote object.
Enums§
- Low-level status codes from Android
libutils
.
Traits§
- A thread pool for running binder transactions.
- Interface for transforming a generic SpIBinder into a specific remote interface trait.
- Interface of binder local or remote objects.
- Super-trait for Binder interfaces.
- A conversion from
std::result::Result<T, E>
tobinder::Result<T>
. If this type isOk(T)
, it’s returned as is. If this type isErr(E)
,E
is converted intoStatus
which can be either a general binder exception, or a service-specific exception. - Super-trait for structured Binder parcelables, i.e. those generated from AIDL.
Functions§
- Register a new service with the default service manager.
- Retrieve an existing service for a particular interface. Returns
Err(StatusCode::NAME_NOT_FOUND)
immediately if the service is not available. - Retrieve an existing service. Returns
None
immediately if the service is not available. - Prevent a process which registers lazy services from being shut down even when none of the services is in use.
- Retrieve all declared instances for a particular interface
- get_
interface Deprecated Retrieve an existing service for a particular interface, blocking for a few seconds if it doesn’t yet exist. - get_
service Deprecated Retrieve an existing service, blocking for a few seconds if it doesn’t yet exist. - Check if a service is declared (e.g. in a VINTF manifest)
- Determine whether the current thread is currently executing an incoming transaction.
- Register a dynamic service via the LazyServiceRegistrar.
- Retrieve an existing service for a particular interface, or start it if it is configured as a dynamic service and isn’t yet started.
- Retrieve an existing service, or start it if it is configured as a dynamic service and isn’t yet started.
Type Aliases§
- A type alias for a pinned, boxed future that lets you write shorter code without littering it with Pin and Send bounds.
- Binder result containing a
Status
on error.