From aff28342d94f9140e36a793353575d243cc789b8 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Tue, 6 Feb 2024 17:56:17 +1100 Subject: [PATCH] let the bluetooth stack handle its own discovery state --- src/drivers/bluetooth.cpp | 74 +++++-------------------- src/drivers/include/bluetooth.hpp | 14 +---- src/drivers/include/bluetooth_types.hpp | 1 - src/ui/include/ui_fsm.hpp | 1 - src/ui/ui_fsm.cpp | 6 -- 5 files changed, 17 insertions(+), 79 deletions(-) diff --git a/src/drivers/bluetooth.cpp b/src/drivers/bluetooth.cpp index b6cd40a9..b65d5dea 100644 --- a/src/drivers/bluetooth.cpp +++ b/src/drivers/bluetooth.cpp @@ -102,19 +102,6 @@ auto Bluetooth::ConnectedDevice() -> std::optional { return {}; } -auto Bluetooth::SetDeviceDiscovery(bool allowed) -> void { - if (allowed == bluetooth::BluetoothState::discovery()) { - return; - } - bluetooth::BluetoothState::discovery(allowed); - tinyfsm::FsmList::dispatch( - bluetooth::events::DiscoveryChanged{}); -} - -auto Bluetooth::IsDiscovering() -> bool { - return bluetooth::BluetoothState::discovery(); -} - auto Bluetooth::KnownDevices() -> std::vector { std::vector out = bluetooth::BluetoothState::devices(); std::sort(out.begin(), out.end(), [](const auto& a, const auto& b) -> bool { @@ -308,7 +295,6 @@ std::mutex BluetoothState::sDevicesMutex_{}; std::map BluetoothState::sDevices_{}; std::optional BluetoothState::sPreferredDevice_{}; std::optional BluetoothState::sConnectingDevice_{}; -bool BluetoothState::sIsDiscoveryAllowed_{false}; std::atomic BluetoothState::sSource_; std::function BluetoothState::sEventHandler_; @@ -338,16 +324,6 @@ auto BluetoothState::preferred_device(std::optional addr) -> void { sPreferredDevice_ = addr; } -auto BluetoothState::discovery() -> bool { - std::lock_guard lock{sDevicesMutex_}; - return sIsDiscoveryAllowed_; -} - -auto BluetoothState::discovery(bool en) -> void { - std::lock_guard lock{sDevicesMutex_}; - sIsDiscoveryAllowed_ = en; -} - auto BluetoothState::source() -> StreamBufferHandle_t { std::lock_guard lock{sDevicesMutex_}; return sSource_.load(); @@ -380,17 +356,21 @@ auto BluetoothState::react(const events::DeviceDiscovered& ev) -> void { } } - if (is_preferred && is_in_state()) { - ESP_LOGI(kTag, "new device is preferred. connecting."); - transit(); + if (is_preferred && sPreferredDevice_) { + connect(*sPreferredDevice_); } } -auto BluetoothState::react(const events::DiscoveryChanged& ev) -> void { - if (sIsDiscoveryAllowed_) { - sScanner_->ScanContinuously(); - } else { - sScanner_->StopScanning(); +auto BluetoothState::connect(const MacAndName& dev) -> void { + if (!is_in_state()) { + return; + } + sConnectingDevice_ = dev; + ESP_LOGI(kTag, "connecting to '%s' (%u%u%u%u%u%u)", dev.name.c_str(), + dev.mac[0], dev.mac[1], dev.mac[2], dev.mac[3], dev.mac[4], + dev.mac[5]); + if (esp_a2d_source_connect(sConnectingDevice_->mac.data()) == ESP_OK) { + transit(); } } @@ -415,16 +395,6 @@ void Disabled::entry() { esp_bt_controller_deinit(); } -void Disabled::exit() { - if (sIsDiscoveryAllowed_) { - ESP_LOGI(kTag, "bt enabled, beginning discovery"); - sScanner_->ScanContinuously(); - } else { - ESP_LOGI(kTag, "bt enabled, scanning once"); - sScanner_->ScanOnce(); - } -} - void Disabled::react(const events::Enable&) { esp_bt_controller_config_t config = BT_CONTROLLER_INIT_CONFIG_DEFAULT(); esp_err_t err; @@ -477,8 +447,7 @@ void Disabled::react(const events::Enable&) { esp_bt_gap_set_scan_mode(ESP_BT_NON_CONNECTABLE, ESP_BT_NON_DISCOVERABLE); if (sPreferredDevice_) { - sConnectingDevice_ = sPreferredDevice_; - transit(); + connect(*sPreferredDevice_); } else { transit(); } @@ -501,8 +470,7 @@ void Idle::react(const events::PreferredDeviceChanged& ev) { } } if (is_discovered) { - sConnectingDevice_ = sPreferredDevice_; - transit(); + connect(*sPreferredDevice_); } } @@ -512,20 +480,13 @@ void Idle::react(const events::internal::Gap& ev) { void Connecting::entry() { sScanner_->StopScanning(); - - auto dev = sConnectingDevice_; - ESP_LOGI(kTag, "connecting to '%s' (%u%u%u%u%u%u)", dev->name.c_str(), - dev->mac[0], dev->mac[1], dev->mac[2], dev->mac[3], dev->mac[4], - dev->mac[5]); - esp_a2d_source_connect(sConnectingDevice_->mac.data()); - if (sEventHandler_) { std::invoke(sEventHandler_, Event::kConnectionStateChanged); } } void Connecting::exit() { - ESP_LOGI(kTag, "connecting finished"); + sConnectingDevice_ = {}; if (sEventHandler_) { std::invoke(sEventHandler_, Event::kConnectionStateChanged); } @@ -546,7 +507,6 @@ void Connecting::react(const events::internal::Gap& ev) { case ESP_BT_GAP_AUTH_CMPL_EVT: if (ev.param->auth_cmpl.stat != ESP_BT_STATUS_SUCCESS) { ESP_LOGE(kTag, "auth failed"); - sConnectingDevice_ = {}; transit(); } break; @@ -556,22 +516,18 @@ void Connecting::react(const events::internal::Gap& ev) { break; case ESP_BT_GAP_PIN_REQ_EVT: ESP_LOGW(kTag, "device needs a pin to connect"); - sConnectingDevice_ = {}; transit(); break; case ESP_BT_GAP_CFM_REQ_EVT: ESP_LOGW(kTag, "user needs to do cfm. idk man."); - sConnectingDevice_ = {}; transit(); break; case ESP_BT_GAP_KEY_NOTIF_EVT: ESP_LOGW(kTag, "the device is telling us a password??"); - sConnectingDevice_ = {}; transit(); break; case ESP_BT_GAP_KEY_REQ_EVT: ESP_LOGW(kTag, "the device wants a password!"); - sConnectingDevice_ = {}; transit(); break; case ESP_BT_GAP_MODE_CHG_EVT: diff --git a/src/drivers/include/bluetooth.hpp b/src/drivers/include/bluetooth.hpp index 00ddb0c0..6464c8de 100644 --- a/src/drivers/include/bluetooth.hpp +++ b/src/drivers/include/bluetooth.hpp @@ -36,13 +36,6 @@ class Bluetooth { auto IsConnected() -> bool; auto ConnectedDevice() -> std::optional; - /* - * Sets whether or not the bluetooth stack is allowed to actively scan for - * new devices. - */ - auto SetDeviceDiscovery(bool) -> void; - auto IsDiscovering() -> bool; - auto KnownDevices() -> std::vector; auto SetPreferredDevice(std::optional dev) -> void; @@ -62,7 +55,6 @@ 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; }; @@ -134,7 +126,6 @@ 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::ChangeVolume&) {} virtual void react(const events::DeviceDiscovered&); @@ -151,23 +142,22 @@ class BluetoothState : public tinyfsm::Fsm { static std::map sDevices_; static std::optional sPreferredDevice_; static std::optional sConnectingDevice_; - static bool sIsDiscoveryAllowed_; static std::atomic sSource_; static std::function sEventHandler_; + + auto connect(const bluetooth::MacAndName&) -> void; }; 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; }; diff --git a/src/drivers/include/bluetooth_types.hpp b/src/drivers/include/bluetooth_types.hpp index 87da0ab5..74434182 100644 --- a/src/drivers/include/bluetooth_types.hpp +++ b/src/drivers/include/bluetooth_types.hpp @@ -27,7 +27,6 @@ enum class Event { kKnownDevicesChanged, kConnectionStateChanged, kPreferredDeviceChanged, - kDiscoveryChanged, }; } // namespace bluetooth diff --git a/src/ui/include/ui_fsm.hpp b/src/ui/include/ui_fsm.hpp index 42110bdb..c097e764 100644 --- a/src/ui/include/ui_fsm.hpp +++ b/src/ui/include/ui_fsm.hpp @@ -108,7 +108,6 @@ class UiState : public tinyfsm::Fsm { static lua::Property sBluetoothConnected; static lua::Property sBluetoothPairedDevice; static lua::Property sBluetoothDevices; - static lua::Property sBluetoothScanning; static lua::Property sPlaybackPlaying; diff --git a/src/ui/ui_fsm.cpp b/src/ui/ui_fsm.cpp index 55c8c84b..12584ec7 100644 --- a/src/ui/ui_fsm.cpp +++ b/src/ui/ui_fsm.cpp @@ -85,7 +85,6 @@ lua::Property UiState::sBluetoothEnabled{ if (std::get(val)) { sServices->nvs().OutputMode(drivers::NvsStorage::Output::kBluetooth); sServices->bluetooth().Enable(); - sServices->bluetooth().SetDeviceDiscovery(true); } else { sServices->nvs().OutputMode(drivers::NvsStorage::Output::kHeadphones); sServices->bluetooth().Disable(); @@ -109,7 +108,6 @@ lua::Property UiState::sBluetoothPairedDevice{ }}; lua::Property UiState::sBluetoothDevices{ std::vector{}}; -lua::Property UiState::sBluetoothScanning{false}; lua::Property UiState::sPlaybackPlaying{ false, [](const lua::LuaValue& val) { @@ -340,9 +338,6 @@ void UiState::react(const system_fsm::BluetoothEvent& ev) { sBluetoothPairedDevice.Update(std::monostate{}); } break; - case drivers::bluetooth::Event::kDiscoveryChanged: - sBluetoothScanning.Update(bt.IsDiscovering()); - break; case drivers::bluetooth::Event::kPreferredDeviceChanged: break; } @@ -466,7 +461,6 @@ void Lua::entry() { sBluetoothPairedDevice.Update(bt.ConnectedDevice().value()); } sBluetoothDevices.Update(bt.KnownDevices()); - sBluetoothScanning.Update(bt.IsDiscovering()); sCurrentScreen.reset(); sLua->RunScript("/lua/main.lua");