From c6f2b523312320f1ab2f6b2396c507d1e76d4a5b Mon Sep 17 00:00:00 2001 From: jacqueline Date: Mon, 6 Nov 2023 08:19:28 +1100 Subject: Improve bt scanning responsiveness --- src/drivers/include/bluetooth.hpp | 50 ++++++++++++++++++++++++++++++++++----- 1 file changed, 44 insertions(+), 6 deletions(-) (limited to 'src/drivers/include/bluetooth.hpp') diff --git a/src/drivers/include/bluetooth.hpp b/src/drivers/include/bluetooth.hpp index 1489b790..f3623fb8 100644 --- a/src/drivers/include/bluetooth.hpp +++ b/src/drivers/include/bluetooth.hpp @@ -32,6 +32,12 @@ class Bluetooth { auto Disable() -> void; auto IsEnabled() -> bool; + /* + * Sets whether or not the bluetooth stack is allowed to actively scan for + * new devices. + */ + auto SetDeviceDiscovery(bool) -> void; + auto KnownDevices() -> std::vector; auto SetPreferredDevice(const bluetooth::mac_addr_t& mac) -> void; @@ -47,6 +53,10 @@ struct Disable : public tinyfsm::Event {}; struct PreferredDeviceChanged : public tinyfsm::Event {}; struct SourceChanged : public tinyfsm::Event {}; +struct DiscoveryChanged : public tinyfsm::Event {}; +struct DeviceDiscovered : public tinyfsm::Event { + const Device& device; +}; namespace internal { struct Gap : public tinyfsm::Event { @@ -64,13 +74,37 @@ struct Avrc : public tinyfsm::Event { } // namespace internal } // namespace events +/* + * Utility for managing scanning, independent of the current connection state. + */ +class Scanner { + public: + Scanner(); + + auto ScanContinuously() -> void; + auto ScanOnce() -> void; + auto StopScanning() -> void; + auto StopScanningNow() -> void; + + auto HandleGapEvent(const events::internal::Gap&) -> void; + + private: + bool enabled_; + bool is_discovering_; + + auto HandleDeviceDiscovery(const esp_bt_gap_cb_param_t& param) -> void; +}; + class BluetoothState : public tinyfsm::Fsm { public: static auto Init(NvsStorage& storage) -> void; static auto devices() -> std::vector; static auto preferred_device() -> std::optional; - static auto preferred_device(const mac_addr_t&) -> void; + static auto preferred_device(std::optional) -> void; + + static auto discovery() -> bool; + static auto discovery(bool) -> void; static auto source() -> StreamBufferHandle_t; static auto source(StreamBufferHandle_t) -> void; @@ -86,6 +120,9 @@ class BluetoothState : public tinyfsm::Fsm { virtual void react(const events::Disable& ev) = 0; virtual void react(const events::PreferredDeviceChanged& ev){}; virtual void react(const events::SourceChanged& ev){}; + virtual void react(const events::DiscoveryChanged&); + + virtual void react(const events::DeviceDiscovered&); virtual void react(const events::internal::Gap& ev) = 0; virtual void react(const events::internal::A2dp& ev){}; @@ -93,11 +130,13 @@ class BluetoothState : public tinyfsm::Fsm { protected: static NvsStorage* sStorage_; + static Scanner* sScanner_; static std::mutex sDevicesMutex_; static std::map sDevices_; static std::optional sPreferredDevice_; static mac_addr_t sCurrentDevice_; + static bool sIsDiscoveryAllowed_; static std::atomic sSource_; static std::function sEventHandler_; @@ -106,19 +145,21 @@ class BluetoothState : public tinyfsm::Fsm { class Disabled : public BluetoothState { public: void entry() override; + void exit() override; void react(const events::Enable& ev) override; void react(const events::Disable& ev) override{}; void react(const events::internal::Gap& ev) override {} void react(const events::internal::A2dp& ev) override {} + void react(const events::DiscoveryChanged& ev) override{}; + using BluetoothState::react; }; -class Scanning : public BluetoothState { +class Idle : public BluetoothState { public: void entry() override; - void exit() override; void react(const events::Disable& ev) override; void react(const events::PreferredDeviceChanged& ev) override; @@ -126,9 +167,6 @@ class Scanning : public BluetoothState { void react(const events::internal::Gap& ev) override; using BluetoothState::react; - - private: - auto OnDeviceDiscovered(esp_bt_gap_cb_param_t*) -> void; }; class Connecting : public BluetoothState { -- cgit v1.2.3