diff options
-rw-r--r-- | system/gd/rust/common/Android.bp | 69 | ||||
-rw-r--r-- | system/gd/rust/common/Cargo.toml | 36 | ||||
-rw-r--r-- | system/gd/rust/common/src/lib.rs | 4 | ||||
-rw-r--r-- | system/gd/rust/common/src/logging.rs | 11 | ||||
-rw-r--r-- | system/gd/rust/common/src/time.rs | 167 | ||||
-rw-r--r-- | system/gd/rust/linux/mgmt/Cargo.toml | 1 | ||||
-rw-r--r-- | system/gd/rust/linux/stack/Cargo.toml | 1 | ||||
-rw-r--r-- | system/gd/rust/topshim/Android.bp | 2 | ||||
-rw-r--r-- | system/gd/rust/topshim/Cargo.toml | 1 | ||||
-rw-r--r-- | system/rust/Android.bp | 12 | ||||
-rw-r--r-- | system/rust/Cargo.toml | 3 | ||||
-rw-r--r-- | system/rust/src/utils.rs | 12 | ||||
-rw-r--r-- | system/rust/src/utils/task.rs | 3 | ||||
-rw-r--r-- | system/rust/tests/utils/mod.rs | 2 |
14 files changed, 25 insertions, 299 deletions
diff --git a/system/gd/rust/common/Android.bp b/system/gd/rust/common/Android.bp deleted file mode 100644 index 26b724c900..0000000000 --- a/system/gd/rust/common/Android.bp +++ /dev/null @@ -1,69 +0,0 @@ -package { - // See: http://go/android-license-faq - // A large-scale-change added 'default_applicable_licenses' to import - // all of the 'license_kinds' from "system_bt_license" - // to get the below license kinds: - // SPDX-license-identifier-Apache-2.0 - default_applicable_licenses: ["system_bt_license"], -} - -rust_defaults { - name: "gd_rust_defaults", - target: { - darwin: { - enabled: false, - }, - }, - host_supported: true, -} - -rust_library { - name: "libbt_common", - defaults: ["libbt_common_defaults"], - rustlibs: [ - "liblog_rust", - ], - target: { - android: { - rustlibs: [ - "libandroid_logger", - ], - }, - host: { - rustlibs: [ - "libenv_logger", - ], - }, - }, - apex_available: ["com.android.bt"], - min_sdk_version: "Tiramisu", -} - -rust_defaults { - name: "libbt_common_defaults", - defaults: ["gd_rust_defaults"], - crate_name: "bt_common", - srcs: ["src/lib.rs"], - rustlibs: [ - "liblog_rust", - ], - proc_macros: [ - "libpaste", - ], -} - -rust_test_host { - name: "libbt_common_inline_tests", - defaults: ["gd_rust_defaults"], - srcs: ["src/lib.rs"], - test_suites: ["general-tests"], - auto_gen_config: true, - rustlibs: [ - "libbt_common", - "libenv_logger", - "liblog_rust", - ], - proc_macros: [ - "libpaste", - ], -} diff --git a/system/gd/rust/common/Cargo.toml b/system/gd/rust/common/Cargo.toml deleted file mode 100644 index 70fc874baa..0000000000 --- a/system/gd/rust/common/Cargo.toml +++ /dev/null @@ -1,36 +0,0 @@ -# -# Copyright 2021 Google, Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at: -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -[package] -name = "bt_common" -version = "0.0.1" -edition = "2018" - -[dependencies] -cxx = "1.0" -env_logger = "0.8" -futures = "0.3.13" -log = "0.4" -nix = { version = "0.27.1", features = ["time", "user"] } -tokio = { version = "1.0", features = ['bytes', 'macros', 'net', 'rt-multi-thread', 'time'] } - -# Proc Macro dependency -paste = "1.0" - -[dev-dependencies] -tokio = { version = "1.0", features = ['bytes', 'macros', 'net', 'rt-multi-thread', 'time', 'sync'] } - -[lib] -crate-type = ["rlib"] diff --git a/system/gd/rust/common/src/lib.rs b/system/gd/rust/common/src/lib.rs deleted file mode 100644 index 165aaa5063..0000000000 --- a/system/gd/rust/common/src/lib.rs +++ /dev/null @@ -1,4 +0,0 @@ -//! Bluetooth common library - -mod logging; -pub use logging::*; diff --git a/system/gd/rust/common/src/logging.rs b/system/gd/rust/common/src/logging.rs deleted file mode 100644 index 71c60da64b..0000000000 --- a/system/gd/rust/common/src/logging.rs +++ /dev/null @@ -1,11 +0,0 @@ -/// Inits logging for Android -#[cfg(target_os = "android")] -pub fn init_logging() { - android_logger::init_once(android_logger::Config::default().with_tag("bluetooth")); -} - -/// Inits logging for host -#[cfg(not(target_os = "android"))] -pub fn init_logging() { - env_logger::Builder::new().parse_default_env().try_init().ok(); -} diff --git a/system/gd/rust/common/src/time.rs b/system/gd/rust/common/src/time.rs deleted file mode 100644 index 0fd2226a4a..0000000000 --- a/system/gd/rust/common/src/time.rs +++ /dev/null @@ -1,167 +0,0 @@ -//! Waking timers for Bluetooth. Implemented using timerfd, but supposed to feel similar to -///Tokio's time -use nix::sys::time::TimeSpec; -use nix::sys::timerfd::{ClockId, Expiration, TimerFd, TimerFlags, TimerSetTimeFlags}; -use std::os::fd::{AsFd, AsRawFd, RawFd}; -use std::time::Duration; -use tokio::io::unix::AsyncFd; - -/// A wrapper for `TimerFd` which implements `AsRawFd`. -#[derive(Debug)] -struct TimerFdWrapper(TimerFd); - -impl TimerFdWrapper { - fn get(&self) -> nix::Result<Option<Expiration>> { - self.0.get() - } - - fn set(&self, expiration: Expiration, flags: TimerSetTimeFlags) -> nix::Result<()> { - self.0.set(expiration, flags) - } - - fn wait(&self) -> nix::Result<()> { - self.0.wait() - } -} - -impl AsRawFd for TimerFdWrapper { - fn as_raw_fd(&self) -> RawFd { - self.0.as_fd().as_raw_fd() - } -} - -/// A single shot Alarm -pub struct Alarm { - fd: AsyncFd<TimerFdWrapper>, -} - -impl Alarm { - /// Construct a new alarm - pub fn new() -> Self { - let timer = TimerFd::new(get_clock(), TimerFlags::empty()).unwrap(); - Self { fd: AsyncFd::new(TimerFdWrapper(timer)).unwrap() } - } - - /// Reset the alarm to duration, starting from now - pub fn reset(&self, duration: Duration) { - self.fd - .get_ref() - .set(Expiration::OneShot(TimeSpec::from(duration)), TimerSetTimeFlags::empty()) - .unwrap(); - } - - /// Stop the alarm if it is currently started - pub fn cancel(&self) { - self.reset(Duration::from_millis(0)); - } - - /// Completes when the alarm has expired - pub async fn expired(&self) { - let mut read_ready = self.fd.readable().await.unwrap(); - read_ready.clear_ready(); - drop(read_ready); - // Will not block, since we have confirmed it is readable - if self.fd.get_ref().get().unwrap().is_some() { - self.fd.get_ref().wait().unwrap(); - } - } -} - -impl Default for Alarm { - fn default() -> Self { - Alarm::new() - } -} - -/// Similar to tokio's interval, except the first tick does *not* complete immediately -pub fn interval(period: Duration) -> Interval { - let timer = TimerFd::new(get_clock(), TimerFlags::empty()).unwrap(); - timer.set(Expiration::Interval(TimeSpec::from(period)), TimerSetTimeFlags::empty()).unwrap(); - - Interval { fd: AsyncFd::new(TimerFdWrapper(timer)).unwrap() } -} - -/// Future returned by interval() -pub struct Interval { - fd: AsyncFd<TimerFdWrapper>, -} - -impl Interval { - /// Call this to get the future for the next tick of the interval - pub async fn tick(&mut self) { - let mut read_ready = self.fd.readable().await.unwrap(); - read_ready.clear_ready(); - drop(read_ready); - // Will not block, since we have confirmed it is readable - if self.fd.get_ref().get().unwrap().is_some() { - self.fd.get_ref().wait().unwrap(); - } - } -} - -fn get_clock() -> ClockId { - if cfg!(target_os = "android") { - ClockId::CLOCK_BOOTTIME_ALARM - } else { - ClockId::CLOCK_BOOTTIME - } -} - -#[cfg(test)] -mod tests { - use super::interval; - use super::Alarm; - use crate::assert_near; - use std::time::{Duration, Instant}; - - #[test] - fn alarm_cancel_after_expired() { - let runtime = tokio::runtime::Runtime::new().unwrap(); - runtime.block_on(async { - let alarm = Alarm::new(); - alarm.reset(Duration::from_millis(10)); - tokio::time::sleep(Duration::from_millis(30)).await; - alarm.cancel(); - - for _ in 0..10 { - let ready_in_10_ms = async { - tokio::time::sleep(Duration::from_millis(10)).await; - }; - - tokio::select! { - _ = alarm.expired() => (), - _ = ready_in_10_ms => (), - } - } - }); - } - - #[test] - fn alarm_clear_ready_after_expired() { - // After an alarm expired, we need to make sure we clear ready from AsyncFdReadyGuard. - // Otherwise it's still ready and select! won't work. - let runtime = tokio::runtime::Runtime::new().unwrap(); - runtime.block_on(async { - let timer = Instant::now(); - let alarm = Alarm::new(); - alarm.reset(Duration::from_millis(10)); - alarm.expired().await; - let ready_in_10_ms = async { - tokio::time::sleep(Duration::from_millis(10)).await; - }; - tokio::select! { - _ = alarm.expired() => (), - _ = ready_in_10_ms => (), - } - assert_near!(timer.elapsed().as_millis(), 20, 3); - }); - } - - #[test] - fn interval_schedule_and_then_drop() { - let runtime = tokio::runtime::Runtime::new().unwrap(); - runtime.block_on(async { - interval(Duration::from_millis(10)); - }); - } -} diff --git a/system/gd/rust/linux/mgmt/Cargo.toml b/system/gd/rust/linux/mgmt/Cargo.toml index 66c239a4af..d7905e7359 100644 --- a/system/gd/rust/linux/mgmt/Cargo.toml +++ b/system/gd/rust/linux/mgmt/Cargo.toml @@ -7,7 +7,6 @@ build = "build.rs" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -bt_common = { path = "../../common" } bt_topshim = { path = "../../topshim" } bt_utils = { path = "../utils" } btstack = { path = "../stack" } diff --git a/system/gd/rust/linux/stack/Cargo.toml b/system/gd/rust/linux/stack/Cargo.toml index da42d9face..37fafcf9e4 100644 --- a/system/gd/rust/linux/stack/Cargo.toml +++ b/system/gd/rust/linux/stack/Cargo.toml @@ -4,7 +4,6 @@ version = "0.1.0" edition = "2018" [dependencies] -bt_common = { path = "../../common" } bt_topshim = { path = "../../topshim" } bt_utils = { path = "../utils" } diff --git a/system/gd/rust/topshim/Android.bp b/system/gd/rust/topshim/Android.bp index 5d0e9c2102..9e1a5e976a 100644 --- a/system/gd/rust/topshim/Android.bp +++ b/system/gd/rust/topshim/Android.bp @@ -9,7 +9,6 @@ package { rust_library_host_rlib { name: "libbt_topshim", - defaults: ["gd_rust_defaults"], crate_name: "bt_topshim", srcs: [ "src/lib.rs", @@ -18,7 +17,6 @@ rust_library_host_rlib { ], rustlibs: [ "libbitflags", - "libbt_common", "libcxx", "libfutures", "libgrpcio", diff --git a/system/gd/rust/topshim/Cargo.toml b/system/gd/rust/topshim/Cargo.toml index 22829587b8..6ff28859bd 100644 --- a/system/gd/rust/topshim/Cargo.toml +++ b/system/gd/rust/topshim/Cargo.toml @@ -21,7 +21,6 @@ build = "build.rs" [dependencies] # BT dependencies -bt_common = { path = "../common" } topshim_macros = { path = "macros" } cxx = "1.0" diff --git a/system/rust/Android.bp b/system/rust/Android.bp index 59ef358330..c3e98a0022 100644 --- a/system/rust/Android.bp +++ b/system/rust/Android.bp @@ -33,7 +33,6 @@ rust_defaults { rustlibs: [ "libanyhow", "libbitflags", - "libbt_common", "libbytes", "libcxx", "liblog_rust", @@ -56,6 +55,17 @@ rust_defaults { "libstatslog_bt", "libutils", ], + rustlibs: [ + "libandroid_logger", + ], + }, + host: { + rustlibs: [ + "libenv_logger", + ], + }, + darwin: { + enabled: false, }, }, apex_available: ["com.android.bt"], diff --git a/system/rust/Cargo.toml b/system/rust/Cargo.toml index a9ab090891..39ccba97e1 100644 --- a/system/rust/Cargo.toml +++ b/system/rust/Cargo.toml @@ -18,9 +18,6 @@ name = "bluetooth_core" version = "0.0.1" edition = "2021" -[dependencies] -bt_common = { path = "../gd/rust/common", default-features = false } - # External dependencies # Note: source-of-truth is Android.bp, these are mirrored solely for IDE convenience anyhow = "1.0" diff --git a/system/rust/src/utils.rs b/system/rust/src/utils.rs index ed6331a7af..0183d8b514 100644 --- a/system/rust/src/utils.rs +++ b/system/rust/src/utils.rs @@ -4,3 +4,15 @@ pub mod owned_handle; #[cfg(test)] pub mod task; + +/// Inits logging for Android +#[cfg(target_os = "android")] +pub fn init_logging() { + android_logger::init_once(android_logger::Config::default().with_tag("bluetooth")); +} + +/// Inits logging for host +#[cfg(not(target_os = "android"))] +pub fn init_logging() { + env_logger::Builder::new().parse_default_env().try_init().ok(); +} diff --git a/system/rust/src/utils/task.rs b/system/rust/src/utils/task.rs index b020f05a65..4e2753be7c 100644 --- a/system/rust/src/utils/task.rs +++ b/system/rust/src/utils/task.rs @@ -6,7 +6,6 @@ use std::{ time::Duration, }; -use bt_common::init_logging; use tokio::{ runtime::Builder, select, @@ -15,7 +14,7 @@ use tokio::{ /// Run the supplied future on a single-threaded runtime pub fn block_on_locally<T>(f: impl Future<Output = T>) -> T { - init_logging(); + crate::utils::init_logging(); LocalSet::new().block_on( &Builder::new_current_thread().enable_time().start_paused(true).build().unwrap(), async move { diff --git a/system/rust/tests/utils/mod.rs b/system/rust/tests/utils/mod.rs index a187e90058..c0775acb89 100644 --- a/system/rust/tests/utils/mod.rs +++ b/system/rust/tests/utils/mod.rs @@ -4,7 +4,7 @@ use tokio::task::LocalSet; pub fn start_test(f: impl Future<Output = ()>) { tokio_test::block_on(async move { - bt_common::init_logging(); + utils::init_logging(); tokio::time::pause(); LocalSet::new().run_until(f).await; }); |