Crate binder_ndk

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 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.
  • Super-trait for Binder parcelables.

Functions

  • Register a new service with the default service manager.
  • 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
  • Retrieve an existing service for a particular interface, blocking for a few seconds if it doesn’t yet exist.
  • 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 Definitions

  • 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.