rsbinder/
lib.rs

1// Copyright 2022 Jeff Kim <hiking90@gmail.com>
2// SPDX-License-Identifier: Apache-2.0
3
4//! A library for Binder communication developed purely in Rust.
5//!
6//! # License
7//!
8//! Licensed under Apache License, Version 2.0.
9//!
10//! # References
11//!
12//! * [AIDL](https://source.android.com/docs/core/architecture/aidl)
13//! * [Binder](https://source.android.com/docs/core/architecture/hidl/binder-ipc)
14//!
15
16mod binder;
17#[cfg(feature = "async")]
18pub mod binder_async;
19mod binder_object;
20pub mod binderfs;
21pub mod error;
22pub mod file_descriptor;
23mod macros;
24pub mod native;
25pub mod parcel;
26pub mod parcelable;
27pub mod parcelable_holder;
28mod process_state;
29pub mod proxy;
30mod ref_counter;
31pub mod status;
32mod sys;
33pub mod thread_state;
34
35pub mod hub;
36#[cfg(feature = "async")]
37mod rt;
38
39pub use binder::*;
40#[cfg(feature = "async")]
41pub use binder_async::{BinderAsyncPool, BinderAsyncRuntime, BoxFuture};
42pub use error::{Result, StatusCode};
43pub use file_descriptor::ParcelFileDescriptor;
44pub use native::*;
45pub use parcel::Parcel;
46pub use parcelable::*;
47pub use parcelable_holder::ParcelableHolder;
48pub use process_state::ProcessState;
49pub use proxy::*;
50#[cfg(feature = "tokio")]
51pub use rt::*;
52pub use status::{ExceptionCode, Status};
53
54pub const DEFAULT_BINDER_CONTROL_PATH: &str = "/dev/binderfs/binder-control";
55pub const DEFAULT_BINDER_PATH: &str = "/dev/binderfs/binder";
56pub const DEFAULT_BINDERFS_PATH: &str = "/dev/binderfs";
57
58#[cfg(target_os = "android")]
59static ANDROID_SDK_VERSION: std::sync::OnceLock<u32> = std::sync::OnceLock::new();
60
61/// Get the Android version.
62#[cfg(target_os = "android")]
63pub fn get_android_sdk_version() -> u32 {
64    *ANDROID_SDK_VERSION.get_or_init(|| {
65        match rsproperties::get_with_result("ro.build.version.sdk") {
66            Ok(version) => version.parse::<u32>().unwrap_or(0),
67            Err(_) => {
68                log::warn!("Failed to get Android SDK version, defaulting to 0");
69                0
70            }
71        }
72    })
73}
74
75#[cfg(test)]
76mod tests {
77    #[cfg(target_os = "linux")]
78    use crate::*;
79    #[test]
80    #[cfg(target_os = "linux")]
81    fn process_state() {
82        ProcessState::init("/dev/binderfs/binder", 0);
83    }
84}