Crate binder

Source
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§

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§

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> to binder::Result<T>. If this type is Ok(T), it’s returned as is. If this type is Err(E), E is converted into Status 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_interfaceDeprecated
    Retrieve an existing service for a particular interface, blocking for a few seconds if it doesn’t yet exist.
  • get_serviceDeprecated
    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.