From fc1811f966d45a2c85521deebd2f713cd1d838d7 Mon Sep 17 00:00:00 2001 From: Aaron Yu Date: Wed, 15 Feb 2023 16:23:45 -0800 Subject: [PATCH] Set g3 bluetooth adapter to be enabled by default and remove Toggle() PiperOrigin-RevId: 509962396 --- .../implementation/mediums/ble_test.cc | 4 +-- .../implementation/mediums/bluetooth_radio.cc | 27 ------------------- .../implementation/mediums/bluetooth_radio.h | 9 +------ .../mediums/bluetooth_radio_test.cc | 12 ++------- fastpair/internal/ble/ble.cc | 27 ------------------- fastpair/internal/ble/ble.h | 8 +----- fastpair/internal/ble/ble_test.cc | 16 +++-------- internal/platform/bluetooth_adapter_test.cc | 6 ++--- .../implementation/g3/bluetooth_adapter.h | 2 +- 9 files changed, 14 insertions(+), 97 deletions(-) diff --git a/connections/implementation/mediums/ble_test.cc b/connections/implementation/mediums/ble_test.cc index 8669d395..9ec605b1 100644 --- a/connections/implementation/mediums/ble_test.cc +++ b/connections/implementation/mediums/ble_test.cc @@ -178,10 +178,10 @@ TEST_F(BleTest, CanConstructValidObject) { EXPECT_TRUE(ble_a.IsMediumValid()); EXPECT_TRUE(ble_a.IsAdapterValid()); - EXPECT_FALSE(ble_a.IsAvailable()); + EXPECT_TRUE(ble_a.IsAvailable()); EXPECT_TRUE(ble_b.IsMediumValid()); EXPECT_TRUE(ble_b.IsAdapterValid()); - EXPECT_FALSE(ble_b.IsAvailable()); + EXPECT_TRUE(ble_b.IsAvailable()); EXPECT_NE(&radio_a.GetBluetoothAdapter(), &radio_b.GetBluetoothAdapter()); env_.Stop(); } diff --git a/connections/implementation/mediums/bluetooth_radio.cc b/connections/implementation/mediums/bluetooth_radio.cc index 92ed161d..2bea0888 100644 --- a/connections/implementation/mediums/bluetooth_radio.cc +++ b/connections/implementation/mediums/bluetooth_radio.cc @@ -14,15 +14,11 @@ #include "connections/implementation/mediums/bluetooth_radio.h" -#include "internal/platform/exception.h" #include "internal/platform/logging.h" -#include "internal/platform/system_clock.h" namespace nearby { namespace connections { -constexpr absl::Duration BluetoothRadio::kPauseBetweenToggle; - BluetoothRadio::BluetoothRadio() { if (!IsAdapterValid()) { NEARBY_LOG(ERROR, "Bluetooth adapter is not valid: BT is not supported"); @@ -62,29 +58,6 @@ bool BluetoothRadio::IsEnabled() const { return IsAdapterValid() && IsInDesiredState(true); } -bool BluetoothRadio::Toggle() { - if (!SaveOriginalState()) { - return false; - } - - if (!SetBluetoothState(false)) { - NEARBY_LOG(INFO, "BT Toggle: Failed to turn BT off."); - return false; - } - - if (SystemClock::Sleep(kPauseBetweenToggle).Raised(Exception::kInterrupted)) { - NEARBY_LOG(INFO, "BT Toggle: interrupted before turing on."); - return false; - } - - if (!SetBluetoothState(true)) { - NEARBY_LOG(INFO, "BT Toggle: Failed to turn BT on."); - return false; - } - - return true; -} - bool BluetoothRadio::SetBluetoothState(bool enable) { return bluetooth_adapter_.SetStatus( enable ? BluetoothAdapter::Status::kEnabled diff --git a/connections/implementation/mediums/bluetooth_radio.h b/connections/implementation/mediums/bluetooth_radio.h index efef647b..3409e068 100644 --- a/connections/implementation/mediums/bluetooth_radio.h +++ b/connections/implementation/mediums/bluetooth_radio.h @@ -17,7 +17,6 @@ #include -#include "absl/time/clock.h" #include "internal/platform/atomic_boolean.h" #include "internal/platform/bluetooth_adapter.h" @@ -50,21 +49,15 @@ class BluetoothRadio { // Returns true if the Bluetooth radio is currently enabled. bool IsEnabled() const; - // Turn BT radio Off, delay for kPauseBetweenToggle and then turn it On. - // This will block calling thread for at least kPauseBetweenToggle duration. - bool Toggle(); - // Returns result of BluetoothAdapter::IsValid() for private adapter instance. bool IsAdapterValid() const { return bluetooth_adapter_.IsValid(); } BluetoothAdapter& GetBluetoothAdapter() { return bluetooth_adapter_; } private: - static constexpr absl::Duration kPauseBetweenToggle = absl::Seconds(3); - bool SetBluetoothState(bool enable); bool IsInDesiredState(bool should_be_enabled) const; - // To be called in enable(), disable(), and toggle(). This will remember the + // To be called in enable() and disable(). This will remember the // original state of the radio before any radio state has been modified. // Returns false if Bluetooth doesn't exist on the device and the state cannot // be obtained. diff --git a/connections/implementation/mediums/bluetooth_radio_test.cc b/connections/implementation/mediums/bluetooth_radio_test.cc index 4db50b1d..cf5241c8 100644 --- a/connections/implementation/mediums/bluetooth_radio_test.cc +++ b/connections/implementation/mediums/bluetooth_radio_test.cc @@ -30,6 +30,8 @@ TEST(BluetoothRadioTest, ConstructorDestructorWorks) { TEST(BluetoothRadioTest, CanEnable) { BluetoothRadio radio; EXPECT_TRUE(radio.IsAdapterValid()); + EXPECT_TRUE(radio.IsEnabled()); + EXPECT_TRUE(radio.Disable()); EXPECT_FALSE(radio.IsEnabled()); EXPECT_TRUE(radio.Enable()); EXPECT_TRUE(radio.IsEnabled()); @@ -38,21 +40,11 @@ TEST(BluetoothRadioTest, CanEnable) { TEST(BluetoothRadioTest, CanDisable) { BluetoothRadio radio; EXPECT_TRUE(radio.IsAdapterValid()); - EXPECT_FALSE(radio.IsEnabled()); - EXPECT_TRUE(radio.Enable()); EXPECT_TRUE(radio.IsEnabled()); EXPECT_TRUE(radio.Disable()); EXPECT_FALSE(radio.IsEnabled()); } -TEST(BluetoothRadioTest, CanToggle) { - BluetoothRadio radio; - EXPECT_TRUE(radio.IsAdapterValid()); - EXPECT_FALSE(radio.IsEnabled()); - EXPECT_TRUE(radio.Toggle()); - EXPECT_TRUE(radio.IsEnabled()); -} - } // namespace } // namespace connections } // namespace nearby diff --git a/fastpair/internal/ble/ble.cc b/fastpair/internal/ble/ble.cc index f9b5e6f5..6e8234b0 100644 --- a/fastpair/internal/ble/ble.cc +++ b/fastpair/internal/ble/ble.cc @@ -18,15 +18,11 @@ #include #include -#include "internal/platform/exception.h" #include "internal/platform/logging.h" -#include "internal/platform/system_clock.h" namespace nearby { namespace fastpair { -constexpr absl::Duration Ble::kPauseBetweenToggle; - Ble::~Ble() { // We never enabled Bluetooth, nothing to do. if (!ever_saved_state_.Get()) { @@ -60,29 +56,6 @@ bool Ble::IsEnabled() const { return IsAdapterValid() && IsInDesiredState(true); } -bool Ble::Toggle() { - if (!SaveOriginalState()) { - return false; - } - - if (!SetBluetoothState(false)) { - NEARBY_LOG(INFO, "BT Toggle: Failed to turn BT off."); - return false; - } - - if (SystemClock::Sleep(kPauseBetweenToggle).Raised(Exception::kInterrupted)) { - NEARBY_LOG(INFO, "BT Toggle: interrupted before turing on."); - return false; - } - - if (!SetBluetoothState(true)) { - NEARBY_LOG(INFO, "BT Toggle: Failed to turn BT on."); - return false; - } - - return true; -} - bool Ble::SetBluetoothState(bool enable) { return adapter_.SetStatus(enable ? BluetoothAdapter::Status::kEnabled : BluetoothAdapter::Status::kDisabled); diff --git a/fastpair/internal/ble/ble.h b/fastpair/internal/ble/ble.h index e6839975..9644140a 100644 --- a/fastpair/internal/ble/ble.h +++ b/fastpair/internal/ble/ble.h @@ -20,7 +20,6 @@ #include "absl/container/flat_hash_map.h" #include "absl/container/flat_hash_set.h" -#include "absl/time/clock.h" #include "internal/platform/atomic_boolean.h" #include "internal/platform/ble.h" #include "internal/platform/bluetooth_adapter.h" @@ -52,10 +51,6 @@ class Ble { // Returns true if the Bluetooth radio is currently enabled. bool IsEnabled() const; - // Turn BT radio Off, delay for kPauseBetweenToggle and then turn it On. - // This will block calling thread for at least kPauseBetweenToggle duration. - bool Toggle(); - // Returns true if Ble communications are supported by a platform. bool IsAvailable() const ABSL_LOCKS_EXCLUDED(mutex_); @@ -95,7 +90,6 @@ class Ble { DiscoveredPeripheralCallback discovered_peripheral_callback_; BleMedium medium_ ABSL_GUARDED_BY(mutex_){adapter_}; bool is_scanning_ = false; - static constexpr absl::Duration kPauseBetweenToggle = absl::Seconds(3); // Same as IsAvailable(), but must be called with mutex_ held. bool IsAvailableLocked() const ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_); @@ -107,7 +101,7 @@ class Ble { bool IsInDesiredState(bool should_be_enabled) const; - // To be called in enable(), disable(), and toggle(). This will remember the + // To be called in enable() and disable(). This will remember the // original state of the ble before any ble state has been modified. // Returns false if Bluetooth doesn't exist on the device and the state cannot // be obtained. diff --git a/fastpair/internal/ble/ble_test.cc b/fastpair/internal/ble/ble_test.cc index 52ce1a0e..63ad9aed 100644 --- a/fastpair/internal/ble/ble_test.cc +++ b/fastpair/internal/ble/ble_test.cc @@ -47,6 +47,8 @@ TEST_F(BleTest, ConstructorDestructorWorks) { TEST_F(BleTest, CanEnable) { Ble ble; EXPECT_TRUE(ble.IsAdapterValid()); + EXPECT_TRUE(ble.IsEnabled()); + EXPECT_TRUE(ble.Disable()); EXPECT_FALSE(ble.IsEnabled()); EXPECT_TRUE(ble.Enable()); EXPECT_TRUE(ble.IsEnabled()); @@ -55,21 +57,11 @@ TEST_F(BleTest, CanEnable) { TEST_F(BleTest, CanDisable) { Ble ble; EXPECT_TRUE(ble.IsAdapterValid()); - EXPECT_FALSE(ble.IsEnabled()); - EXPECT_TRUE(ble.Enable()); EXPECT_TRUE(ble.IsEnabled()); EXPECT_TRUE(ble.Disable()); EXPECT_FALSE(ble.IsEnabled()); } -TEST_F(BleTest, CanToggle) { - Ble ble; - EXPECT_TRUE(ble.IsAdapterValid()); - EXPECT_FALSE(ble.IsEnabled()); - EXPECT_TRUE(ble.Toggle()); - EXPECT_TRUE(ble.IsEnabled()); -} - TEST_F(BleTest, CanConstructValidObject) { env_.Start(); Ble ble_a; @@ -77,10 +69,10 @@ TEST_F(BleTest, CanConstructValidObject) { EXPECT_TRUE(ble_a.IsMediumValid()); EXPECT_TRUE(ble_a.IsAdapterValid()); - EXPECT_FALSE(ble_a.IsAvailable()); + EXPECT_TRUE(ble_a.IsAvailable()); EXPECT_TRUE(ble_b.IsMediumValid()); EXPECT_TRUE(ble_b.IsAdapterValid()); - EXPECT_FALSE(ble_b.IsAvailable()); + EXPECT_TRUE(ble_b.IsAvailable()); EXPECT_NE(&ble_a.GetBluetoothAdapter(), &ble_b.GetBluetoothAdapter()); env_.Stop(); } diff --git a/internal/platform/bluetooth_adapter_test.cc b/internal/platform/bluetooth_adapter_test.cc index 35377c4d..671793c7 100644 --- a/internal/platform/bluetooth_adapter_test.cc +++ b/internal/platform/bluetooth_adapter_test.cc @@ -130,16 +130,16 @@ TEST(BluetoothAdapterTest, ConstructorDestructorWorks) { TEST(BluetoothAdapterTest, CanSetName) { constexpr char kAdapterName[] = "MyBtAdapter"; BluetoothAdapter adapter; - EXPECT_EQ(adapter.GetStatus(), BluetoothAdapter::Status::kDisabled); + EXPECT_EQ(adapter.GetStatus(), BluetoothAdapter::Status::kEnabled); EXPECT_TRUE(adapter.SetName(kAdapterName)); EXPECT_EQ(adapter.GetName(), std::string(kAdapterName)); } TEST(BluetoothAdapterTest, CanSetStatus) { BluetoothAdapter adapter; - EXPECT_EQ(adapter.GetStatus(), BluetoothAdapter::Status::kDisabled); - EXPECT_TRUE(adapter.SetStatus(BluetoothAdapter::Status::kEnabled)); EXPECT_EQ(adapter.GetStatus(), BluetoothAdapter::Status::kEnabled); + EXPECT_TRUE(adapter.SetStatus(BluetoothAdapter::Status::kDisabled)); + EXPECT_EQ(adapter.GetStatus(), BluetoothAdapter::Status::kDisabled); } TEST(BluetoothAdapterTest, CanSetMode) { diff --git a/internal/platform/implementation/g3/bluetooth_adapter.h b/internal/platform/implementation/g3/bluetooth_adapter.h index b34e5eba..e8ef8c61 100644 --- a/internal/platform/implementation/g3/bluetooth_adapter.h +++ b/internal/platform/implementation/g3/bluetooth_adapter.h @@ -155,7 +155,7 @@ class BluetoothAdapter : public api::BluetoothAdapter { std::string mac_address_; ScanMode mode_ ABSL_GUARDED_BY(mutex_) = ScanMode::kNone; std::string name_ ABSL_GUARDED_BY(mutex_) = "unknown G3 BT device"; - bool enabled_ ABSL_GUARDED_BY(mutex_) = false; + bool enabled_ ABSL_GUARDED_BY(mutex_) = true; }; } // namespace g3