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§
- binder_
impl - Advanced Binder APIs needed internally by AIDL or when manually using Binder without AIDL.
Macros§
- declare_
binder_ enum - Declare an AIDL enumeration.
- declare_
binder_ interface - Declare typed interfaces for a binder object.
- impl_
deserialize_ for_ parcelable - Implement
Deserialize
trait and friends for a parcelable - impl_
deserialize_ for_ unstructured_ parcelable - Implement
Deserialize
trait and friends for an unstructured parcelable - impl_
serialize_ for_ parcelable - Implement
Serialize
trait and friends for a parcelable - impl_
serialize_ for_ unstructured_ parcelable - Implements
Serialize
trait and friends for an unstructured parcelable.
Structs§
- Accessor
- Rust wrapper around ABinderRpc_Accessor objects for RPC binder service management.
- Accessor
Provider - Rust wrapper around ABinderRpc_AccessorProvider objects for RPC binder service management.
- Binder
Features - The features to enable when creating a native Binder.
- Death
Recipient - Rust wrapper around DeathRecipient objects.
- Lazy
Service Guard - 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.
- Parcel
File Descriptor - Rust version of the Java class android.os.ParcelFileDescriptor
- Parcelable
Holder - A container that can hold any arbitrary
Parcelable
. - Process
State - Static utility functions to manage Binder process state.
- SpIBinder
- A strong reference to a Binder remote object.
- Status
- High-level binder status object that encapsulates a standard way to keep track of and chain binder errors along with service specific errors.
- Strong
- Strong reference to a binder object
- Thread
State - Static utility functions to manage Binder thread state.
- Weak
- Weak reference to a binder object
- WpIBinder
- A weak reference to a Binder remote object.
Enums§
- Connection
Info - Socket connection info required for libbinder to connect to a service.
- Exception
Code - Status
Code - Low-level status codes from Android
libutils
.
Traits§
- Binder
Async Pool - A thread pool for running binder transactions.
- FromI
Binder - Interface for transforming a generic SpIBinder into a specific remote interface trait.
- IBinder
- Interface of binder local or remote objects.
- Interface
- Super-trait for Binder interfaces.
- Into
Binder Result - 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. - Parcelable
- Super-trait for structured Binder parcelables, i.e. those generated from AIDL.
Functions§
- add_
service - Register a new service with the default service manager.
- check_
interface - Retrieve an existing service for a particular interface. Returns
Err(StatusCode::NAME_NOT_FOUND)
immediately if the service is not available. - check_
service - Retrieve an existing service. Returns
None
immediately if the service is not available. - delegate_
accessor - Register a new service with the default service manager.
- force_
lazy_ services_ persist - Prevent a process which registers lazy services from being shut down even when none of the services is in use.
- get_
declared_ instances - 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.
- is_
declared - Check if a service is declared (e.g. in a VINTF manifest)
- is_
handling_ transaction - Determine whether the current thread is currently executing an incoming transaction.
- register_
lazy_ service - Register a dynamic service via the LazyServiceRegistrar.
- wait_
for_ interface - Retrieve an existing service for a particular interface, or start it if it is configured as a dynamic service and isn’t yet started.
- wait_
for_ service - Retrieve an existing service, or start it if it is configured as a dynamic service and isn’t yet started.