diff options
author | 2025-02-11 18:33:22 -0800 | |
---|---|---|
committer | 2025-02-11 18:33:22 -0800 | |
commit | 8388cbe97c1854014348dd9c70688a3d208acbef (patch) | |
tree | df48172dd9abd4f5e77571874d0705c19bf78a23 | |
parent | 395c0a34fe75a2ad5ec72f31a5c141c4ce948882 (diff) | |
parent | 1208b94178c8c72f345c7fe7e102dd963a49fe24 (diff) |
Merge changes I6d31aa81,Ib8fce44b into main
* changes:
Delete unused modules neighbor::Scan, neighbor::NameDb
Delete libbt_common
-rw-r--r-- | system/gd/Android.bp | 1 | ||||
-rw-r--r-- | system/gd/BUILD.gn | 1 | ||||
-rw-r--r-- | system/gd/neighbor/Android.bp | 16 | ||||
-rw-r--r-- | system/gd/neighbor/BUILD.gn | 28 | ||||
-rw-r--r-- | system/gd/neighbor/name_db.cc | 165 | ||||
-rw-r--r-- | system/gd/neighbor/name_db.h | 61 | ||||
-rw-r--r-- | system/gd/neighbor/scan.cc | 190 | ||||
-rw-r--r-- | system/gd/neighbor/scan.h | 55 | ||||
-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 |
22 files changed, 25 insertions, 816 deletions
diff --git a/system/gd/Android.bp b/system/gd/Android.bp index 662fff2143..4fa7e83349 100644 --- a/system/gd/Android.bp +++ b/system/gd/Android.bp @@ -130,7 +130,6 @@ cc_defaults { ":BluetoothHciSources", ":BluetoothLppOffloadSources", ":BluetoothMetricsSources", - ":BluetoothNeighborSources", ":BluetoothOsSources", ":BluetoothPacketSources", ":BluetoothStorageSources", diff --git a/system/gd/BUILD.gn b/system/gd/BUILD.gn index 2ee45fde74..7689ce3ebc 100644 --- a/system/gd/BUILD.gn +++ b/system/gd/BUILD.gn @@ -65,7 +65,6 @@ static_library("libbluetooth_gd") { "//bt/system/gd/hal:BluetoothHalSources_ranging_host", "//bt/system/gd/hal:BluetoothHalSources_socket_host", "//bt/system/gd/metrics:BluetoothMetricsSources", - "//bt/system/gd/neighbor:BluetoothNeighborSources", "//bt/system/gd/storage:BluetoothStorageSources", "//bt/system/gd/sysprops:BluetoothSyspropsSources", "//bt/system/pdl:BluetoothGeneratedPackets_h", diff --git a/system/gd/neighbor/Android.bp b/system/gd/neighbor/Android.bp deleted file mode 100644 index a8f1b64eb9..0000000000 --- a/system/gd/neighbor/Android.bp +++ /dev/null @@ -1,16 +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"], -} - -filegroup { - name: "BluetoothNeighborSources", - srcs: [ - "name_db.cc", - "scan.cc", - ], -} diff --git a/system/gd/neighbor/BUILD.gn b/system/gd/neighbor/BUILD.gn deleted file mode 100644 index 72d02dec40..0000000000 --- a/system/gd/neighbor/BUILD.gn +++ /dev/null @@ -1,28 +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. - -source_set("BluetoothNeighborSources") { - sources = [ - "name_db.cc", - "scan.cc", - ] - - deps = [ "//bt/system/gd:gd_default_deps" ] - - configs += [ - "//bt/system/gd:gd_defaults", - "//bt/system/log:log_defaults", - ] -} diff --git a/system/gd/neighbor/name_db.cc b/system/gd/neighbor/name_db.cc deleted file mode 100644 index 48cdae4705..0000000000 --- a/system/gd/neighbor/name_db.cc +++ /dev/null @@ -1,165 +0,0 @@ -/* - * Copyright 2020 The Android Open Source Project - * - * 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. - */ -#define LOG_TAG "bt_gd_neigh" - -#include "neighbor/name_db.h" - -#include <bluetooth/log.h> - -#include <memory> -#include <unordered_map> -#include <utility> - -#include "common/bind.h" -#include "hci/hci_packets.h" -#include "hci/remote_name_request.h" -#include "module.h" -#include "os/handler.h" - -namespace bluetooth { -namespace neighbor { - -namespace { -struct PendingRemoteNameRead { - ReadRemoteNameDbCallback callback_; - os::Handler* handler_; -}; -} // namespace - -struct NameDbModule::impl { - void ReadRemoteNameRequest(hci::Address address, ReadRemoteNameDbCallback callback, - os::Handler* handler); - - bool IsNameCached(hci::Address address) const; - RemoteName ReadCachedRemoteName(hci::Address address) const; - - impl(const NameDbModule& module); - - void Start(); - void Stop(); - -private: - std::unordered_map<hci::Address, std::list<PendingRemoteNameRead>> address_to_pending_read_map_; - std::unordered_map<hci::Address, RemoteName> address_to_name_map_; - - void OnRemoteNameResponse(hci::Address address, hci::ErrorCode status, RemoteName name); - - hci::RemoteNameRequestModule* name_module_; - - const NameDbModule& module_; - os::Handler* handler_; -}; - -const ModuleFactory neighbor::NameDbModule::Factory = - ModuleFactory([]() { return new neighbor::NameDbModule(); }); - -neighbor::NameDbModule::impl::impl(const neighbor::NameDbModule& module) : module_(module) {} - -void neighbor::NameDbModule::impl::ReadRemoteNameRequest(hci::Address address, - ReadRemoteNameDbCallback callback, - os::Handler* handler) { - if (address_to_pending_read_map_.find(address) != address_to_pending_read_map_.end()) { - log::warn("Already have remote read db in progress; adding callback to callback list"); - address_to_pending_read_map_[address].push_back({std::move(callback), handler}); - return; - } - - std::list<PendingRemoteNameRead> tmp; - address_to_pending_read_map_[address] = std::move(tmp); - address_to_pending_read_map_[address].push_back({std::move(callback), handler}); - - // TODO(cmanton) Use remote name request defaults for now - hci::PageScanRepetitionMode page_scan_repetition_mode = hci::PageScanRepetitionMode::R1; - uint16_t clock_offset = 0; - hci::ClockOffsetValid clock_offset_valid = hci::ClockOffsetValid::INVALID; - name_module_->StartRemoteNameRequest( - address, - hci::RemoteNameRequestBuilder::Create(address, page_scan_repetition_mode, clock_offset, - clock_offset_valid), - handler_->BindOnce([](hci::ErrorCode /* status */) {}), - handler_->BindOnce([&](uint64_t /* features */) { - log::warn("UNIMPLEMENTED: ignoring host supported features"); - }), - handler_->BindOnceOn(this, &NameDbModule::impl::OnRemoteNameResponse, address)); -} - -void neighbor::NameDbModule::impl::OnRemoteNameResponse(hci::Address address, hci::ErrorCode status, - RemoteName name) { - log::assert_that(address_to_pending_read_map_.find(address) != address_to_pending_read_map_.end(), - "assert failed: address_to_pending_read_map_.find(address) != " - "address_to_pending_read_map_.end()"); - if (status == hci::ErrorCode::SUCCESS) { - address_to_name_map_[address] = name; - } - auto& callback_list = address_to_pending_read_map_.at(address); - for (auto& it : callback_list) { - it.handler_->Call(std::move(it.callback_), address, status == hci::ErrorCode::SUCCESS); - } - address_to_pending_read_map_.erase(address); -} - -bool neighbor::NameDbModule::impl::IsNameCached(hci::Address address) const { - return address_to_name_map_.count(address) == 1; -} - -RemoteName neighbor::NameDbModule::impl::ReadCachedRemoteName(hci::Address address) const { - log::assert_that(IsNameCached(address), "assert failed: IsNameCached(address)"); - return address_to_name_map_.at(address); -} - -/** - * General API here - */ -neighbor::NameDbModule::NameDbModule() : pimpl_(std::make_unique<impl>(*this)) {} - -neighbor::NameDbModule::~NameDbModule() { pimpl_.reset(); } - -void neighbor::NameDbModule::ReadRemoteNameRequest(hci::Address address, - ReadRemoteNameDbCallback callback, - os::Handler* handler) { - GetHandler()->Post(common::BindOnce(&NameDbModule::impl::ReadRemoteNameRequest, - common::Unretained(pimpl_.get()), address, - std::move(callback), handler)); -} - -bool neighbor::NameDbModule::IsNameCached(hci::Address address) const { - return pimpl_->IsNameCached(address); -} - -RemoteName neighbor::NameDbModule::ReadCachedRemoteName(hci::Address address) const { - return pimpl_->ReadCachedRemoteName(address); -} - -void neighbor::NameDbModule::impl::Start() { - name_module_ = module_.GetDependency<hci::RemoteNameRequestModule>(); - handler_ = module_.GetHandler(); -} - -void neighbor::NameDbModule::impl::Stop() {} - -/** - * Module methods here - */ -void neighbor::NameDbModule::ListDependencies(ModuleList* list) const { - list->add<hci::RemoteNameRequestModule>(); -} - -void neighbor::NameDbModule::Start() { pimpl_->Start(); } - -void neighbor::NameDbModule::Stop() { pimpl_->Stop(); } - -} // namespace neighbor -} // namespace bluetooth diff --git a/system/gd/neighbor/name_db.h b/system/gd/neighbor/name_db.h deleted file mode 100644 index f220142079..0000000000 --- a/system/gd/neighbor/name_db.h +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright 2020 The Android Open Source Project - * - * 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. - */ -#pragma once - -#include <array> -#include <cstdint> -#include <memory> - -#include "common/bind.h" -#include "hci/address.h" -#include "hci/hci_packets.h" -#include "module.h" - -namespace bluetooth { -namespace neighbor { - -using RemoteName = std::array<uint8_t, 248>; -using ReadRemoteNameDbCallback = common::OnceCallback<void(hci::Address address, bool success)>; - -class NameDbModule : public bluetooth::Module { -public: - virtual void ReadRemoteNameRequest(hci::Address address, ReadRemoteNameDbCallback callback, - os::Handler* handler); - - bool IsNameCached(hci::Address address) const; - RemoteName ReadCachedRemoteName(hci::Address address) const; - - static const ModuleFactory Factory; - - NameDbModule(); - NameDbModule(const NameDbModule&) = delete; - NameDbModule& operator=(const NameDbModule&) = delete; - - ~NameDbModule(); - -protected: - void ListDependencies(ModuleList* list) const override; - void Start() override; - void Stop() override; - std::string ToString() const override { return std::string("NameDb"); } - -private: - struct impl; - std::unique_ptr<impl> pimpl_; -}; - -} // namespace neighbor -} // namespace bluetooth diff --git a/system/gd/neighbor/scan.cc b/system/gd/neighbor/scan.cc deleted file mode 100644 index 8559f338da..0000000000 --- a/system/gd/neighbor/scan.cc +++ /dev/null @@ -1,190 +0,0 @@ -/* - * Copyright 2019 The Android Open Source Project - * - * 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. - */ -#define LOG_TAG "bt_gd_neigh" - -#include "neighbor/scan.h" - -#include <bluetooth/log.h> - -#include <memory> - -#include "hci/hci_layer.h" -#include "hci/hci_packets.h" -#include "module.h" -#include "os/handler.h" - -namespace bluetooth { -namespace neighbor { - -struct ScanModule::impl { - impl(ScanModule& module); - - void SetInquiryScan(bool enabled); - bool IsInquiryEnabled() const; - - void SetPageScan(bool enabled); - bool IsPageEnabled() const; - - void Start(); - void Stop(); - -private: - ScanModule& module_; - - bool inquiry_scan_enabled_; - bool page_scan_enabled_; - - void WriteScanEnable(); - void ReadScanEnable(hci::ScanEnable); - - void OnCommandComplete(hci::CommandCompleteView status); - - hci::HciLayer* hci_layer_; - os::Handler* handler_; -}; - -const ModuleFactory neighbor::ScanModule::Factory = - ModuleFactory([]() { return new neighbor::ScanModule(); }); - -neighbor::ScanModule::impl::impl(neighbor::ScanModule& module) - : module_(module), inquiry_scan_enabled_(false), page_scan_enabled_(false) {} - -void neighbor::ScanModule::impl::OnCommandComplete(hci::CommandCompleteView view) { - switch (view.GetCommandOpCode()) { - case hci::OpCode::READ_SCAN_ENABLE: { - auto packet = hci::ReadScanEnableCompleteView::Create(view); - log::assert_that(packet.IsValid(), "assert failed: packet.IsValid()"); - log::assert_that(packet.GetStatus() == hci::ErrorCode::SUCCESS, - "assert failed: packet.GetStatus() == hci::ErrorCode::SUCCESS"); - ReadScanEnable(packet.GetScanEnable()); - } break; - - case hci::OpCode::WRITE_SCAN_ENABLE: { - auto packet = hci::WriteScanEnableCompleteView::Create(view); - log::assert_that(packet.IsValid(), "assert failed: packet.IsValid()"); - log::assert_that(packet.GetStatus() == hci::ErrorCode::SUCCESS, - "assert failed: packet.GetStatus() == hci::ErrorCode::SUCCESS"); - } break; - - default: - log::error("Unhandled command {}", hci::OpCodeText(view.GetCommandOpCode())); - break; - } -} - -void neighbor::ScanModule::impl::WriteScanEnable() { - hci::ScanEnable scan_enable; - - if (inquiry_scan_enabled_ && !page_scan_enabled_) { - scan_enable = hci::ScanEnable::INQUIRY_SCAN_ONLY; - } else if (!inquiry_scan_enabled_ && page_scan_enabled_) { - scan_enable = hci::ScanEnable::PAGE_SCAN_ONLY; - } else if (inquiry_scan_enabled_ && page_scan_enabled_) { - scan_enable = hci::ScanEnable::INQUIRY_AND_PAGE_SCAN; - } else { - scan_enable = hci::ScanEnable::NO_SCANS; - } - - { - std::unique_ptr<hci::WriteScanEnableBuilder> packet = - hci::WriteScanEnableBuilder::Create(scan_enable); - hci_layer_->EnqueueCommand(std::move(packet), - handler_->BindOnceOn(this, &impl::OnCommandComplete)); - } - - { - std::unique_ptr<hci::ReadScanEnableBuilder> packet = hci::ReadScanEnableBuilder::Create(); - hci_layer_->EnqueueCommand(std::move(packet), - handler_->BindOnceOn(this, &impl::OnCommandComplete)); - } -} - -void neighbor::ScanModule::impl::ReadScanEnable(hci::ScanEnable scan_enable) { - switch (scan_enable) { - case hci::ScanEnable::INQUIRY_SCAN_ONLY: - inquiry_scan_enabled_ = true; - page_scan_enabled_ = false; - break; - - case hci::ScanEnable::PAGE_SCAN_ONLY: - inquiry_scan_enabled_ = false; - page_scan_enabled_ = true; - break; - - case hci::ScanEnable::INQUIRY_AND_PAGE_SCAN: - inquiry_scan_enabled_ = true; - page_scan_enabled_ = true; - break; - - default: - inquiry_scan_enabled_ = false; - page_scan_enabled_ = false; - break; - } -} - -void neighbor::ScanModule::impl::SetInquiryScan(bool enabled) { - inquiry_scan_enabled_ = enabled; - WriteScanEnable(); -} - -void neighbor::ScanModule::impl::SetPageScan(bool enabled) { - page_scan_enabled_ = enabled; - WriteScanEnable(); -} - -bool neighbor::ScanModule::impl::IsInquiryEnabled() const { return inquiry_scan_enabled_; } - -bool neighbor::ScanModule::impl::IsPageEnabled() const { return page_scan_enabled_; } - -void neighbor::ScanModule::impl::Start() { - hci_layer_ = module_.GetDependency<hci::HciLayer>(); - handler_ = module_.GetHandler(); - - std::unique_ptr<hci::ReadScanEnableBuilder> packet = hci::ReadScanEnableBuilder::Create(); - hci_layer_->EnqueueCommand(std::move(packet), - handler_->BindOnceOn(this, &impl::OnCommandComplete)); -} - -void neighbor::ScanModule::impl::Stop() { - log::info("inquiry scan enabled:{} page scan enabled:{}", inquiry_scan_enabled_, - page_scan_enabled_); -} - -neighbor::ScanModule::ScanModule() : pimpl_(std::make_unique<impl>(*this)) {} - -neighbor::ScanModule::~ScanModule() { pimpl_.reset(); } - -void neighbor::ScanModule::SetInquiryScan() { pimpl_->SetInquiryScan(true); } - -void neighbor::ScanModule::ClearInquiryScan() { pimpl_->SetInquiryScan(false); } - -void neighbor::ScanModule::SetPageScan() { pimpl_->SetPageScan(true); } - -void neighbor::ScanModule::ClearPageScan() { pimpl_->SetPageScan(false); } - -bool neighbor::ScanModule::IsInquiryEnabled() const { return pimpl_->IsInquiryEnabled(); } - -bool neighbor::ScanModule::IsPageEnabled() const { return pimpl_->IsPageEnabled(); } - -void neighbor::ScanModule::ListDependencies(ModuleList* list) const { list->add<hci::HciLayer>(); } - -void neighbor::ScanModule::Start() { pimpl_->Start(); } - -void neighbor::ScanModule::Stop() { pimpl_->Stop(); } - -} // namespace neighbor -} // namespace bluetooth diff --git a/system/gd/neighbor/scan.h b/system/gd/neighbor/scan.h deleted file mode 100644 index 0a5d29b6a7..0000000000 --- a/system/gd/neighbor/scan.h +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright 2019 The Android Open Source Project - * - * 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. - */ -#pragma once - -#include <memory> - -#include "module.h" - -namespace bluetooth { -namespace neighbor { - -class ScanModule : public bluetooth::Module { -public: - ScanModule(); - ScanModule(const ScanModule&) = delete; - ScanModule& operator=(const ScanModule&) = delete; - - ~ScanModule(); - - void SetInquiryScan(); - void ClearInquiryScan(); - bool IsInquiryEnabled() const; - - void SetPageScan(); - void ClearPageScan(); - bool IsPageEnabled() const; - - static const ModuleFactory Factory; - -protected: - void ListDependencies(ModuleList* list) const override; - void Start() override; - void Stop() override; - std::string ToString() const override { return std::string("Scan"); } - -private: - struct impl; - std::unique_ptr<impl> pimpl_; -}; - -} // namespace neighbor -} // namespace bluetooth 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 a815ad29d6..d005b23c77 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; }); |