From d23435fab7a7878d4a82f6b7a113f65c12ec33a7 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Tue, 6 Feb 2024 21:27:43 +1100 Subject: Retry bt device connections i hate this janky-ass protocol --- src/drivers/include/bluetooth.hpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'src/drivers/include/bluetooth.hpp') diff --git a/src/drivers/include/bluetooth.hpp b/src/drivers/include/bluetooth.hpp index 6464c8de..c2962e64 100644 --- a/src/drivers/include/bluetooth.hpp +++ b/src/drivers/include/bluetooth.hpp @@ -17,6 +17,7 @@ #include "esp_avrc_api.h" #include "esp_gap_bt_api.h" #include "nvs.hpp" +#include "tasks.hpp" #include "tinyfsm.hpp" #include "tinyfsm/include/tinyfsm.hpp" @@ -27,7 +28,7 @@ namespace drivers { */ class Bluetooth { public: - Bluetooth(NvsStorage& storage); + Bluetooth(NvsStorage& storage, tasks::WorkerPool&); auto Enable() -> bool; auto Disable() -> void; @@ -53,6 +54,7 @@ namespace events { struct Enable : public tinyfsm::Event {}; struct Disable : public tinyfsm::Event {}; +struct ConnectTimedOut : public tinyfsm::Event {}; struct PreferredDeviceChanged : public tinyfsm::Event {}; struct SourceChanged : public tinyfsm::Event {}; struct DeviceDiscovered : public tinyfsm::Event { @@ -124,6 +126,7 @@ class BluetoothState : public tinyfsm::Fsm { virtual void react(const events::Enable& ev){}; virtual void react(const events::Disable& ev) = 0; + virtual void react(const events::ConnectTimedOut& ev){}; virtual void react(const events::PreferredDeviceChanged& ev){}; virtual void react(const events::SourceChanged& ev){}; virtual void react(const events::ChangeVolume&) {} @@ -141,12 +144,14 @@ class BluetoothState : public tinyfsm::Fsm { static std::mutex sDevicesMutex_; static std::map sDevices_; static std::optional sPreferredDevice_; + static std::optional sConnectingDevice_; + static int sConnectAttemptsRemaining_; static std::atomic sSource_; static std::function sEventHandler_; - auto connect(const bluetooth::MacAndName&) -> void; + auto connect(const bluetooth::MacAndName&) -> bool; }; class Disabled : public BluetoothState { @@ -165,6 +170,7 @@ class Disabled : 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; @@ -181,6 +187,7 @@ class Connecting : public BluetoothState { void react(const events::PreferredDeviceChanged& ev) override; + void react(const events::ConnectTimedOut& 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; -- cgit v1.2.3