diff options
| author | jacqueline <me@jacqueline.id.au> | 2023-08-15 17:32:57 +1000 |
|---|---|---|
| committer | jacqueline <me@jacqueline.id.au> | 2023-08-15 17:32:57 +1000 |
| commit | 544b0013b104a6584660724ccd502adcccd7ca6c (patch) | |
| tree | 46b140ca9d51cdc14f0004ce64c7b89d021d5fce /src/drivers/include | |
| parent | d6b83fcf4a1a3039c06e0b1d1a1f7e2af2351efb (diff) | |
| download | tangara-fw-544b0013b104a6584660724ccd502adcccd7ca6c.tar.gz | |
persist preferred bluetooth device in nvs
Diffstat (limited to 'src/drivers/include')
| -rw-r--r-- | src/drivers/include/bluetooth.hpp | 20 | ||||
| -rw-r--r-- | src/drivers/include/bluetooth_types.hpp | 20 | ||||
| -rw-r--r-- | src/drivers/include/nvs.hpp | 7 |
3 files changed, 34 insertions, 13 deletions
diff --git a/src/drivers/include/bluetooth.hpp b/src/drivers/include/bluetooth.hpp index bdc45910..fc72d393 100644 --- a/src/drivers/include/bluetooth.hpp +++ b/src/drivers/include/bluetooth.hpp @@ -11,32 +11,22 @@ #include <freertos/FreeRTOS.h> #include <freertos/stream_buffer.h> +#include "bluetooth_types.hpp" #include "esp_a2dp_api.h" #include "esp_avrc_api.h" #include "esp_gap_bt_api.h" +#include "nvs.hpp" #include "tinyfsm.hpp" #include "tinyfsm/include/tinyfsm.hpp" namespace drivers { -namespace bluetooth { - -typedef std::array<uint8_t, 6> mac_addr_t; - -struct Device { - mac_addr_t address; - std::string name; - uint32_t class_of_device; - int8_t signal_strength; -}; -} // namespace bluetooth - /* * A handle used to interact with the bluetooth state machine. */ class Bluetooth { public: - Bluetooth(); + Bluetooth(NvsStorage* storage); auto Enable() -> bool; auto Disable() -> void; @@ -74,6 +64,8 @@ struct Avrc : public tinyfsm::Event { class BluetoothState : public tinyfsm::Fsm<BluetoothState> { public: + static auto Init(NvsStorage* storage) -> void; + static auto devices() -> std::vector<Device>; static auto preferred_device() -> std::optional<mac_addr_t>; static auto preferred_device(const mac_addr_t&) -> void; @@ -96,6 +88,8 @@ class BluetoothState : public tinyfsm::Fsm<BluetoothState> { virtual void react(const events::internal::Avrc& ev){}; protected: + static NvsStorage* sStorage_; + static std::mutex sDevicesMutex_; static std::map<mac_addr_t, Device> sDevices_; static std::optional<mac_addr_t> sPreferredDevice_; diff --git a/src/drivers/include/bluetooth_types.hpp b/src/drivers/include/bluetooth_types.hpp new file mode 100644 index 00000000..03100651 --- /dev/null +++ b/src/drivers/include/bluetooth_types.hpp @@ -0,0 +1,20 @@ + +#pragma once + +#include <array> +#include <string> + +namespace drivers { +namespace bluetooth { + +typedef std::array<uint8_t, 6> mac_addr_t; + +struct Device { + mac_addr_t address; + std::string name; + uint32_t class_of_device; + int8_t signal_strength; +}; + +} // namespace bluetooth +} // namespace drivers diff --git a/src/drivers/include/nvs.hpp b/src/drivers/include/nvs.hpp index be783583..32c2ae73 100644 --- a/src/drivers/include/nvs.hpp +++ b/src/drivers/include/nvs.hpp @@ -6,9 +6,13 @@ #pragma once +#include <optional> + #include "esp_err.h" #include "nvs.h" +#include "bluetooth_types.hpp" + namespace drivers { class NvsStorage { @@ -17,6 +21,9 @@ class NvsStorage { auto SchemaVersion() -> uint8_t; + auto PreferredBluetoothDevice() -> std::optional<bluetooth::mac_addr_t>; + auto PreferredBluetoothDevice(std::optional<bluetooth::mac_addr_t>) -> void; + explicit NvsStorage(nvs_handle_t); ~NvsStorage(); |
