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/bluetooth.cpp | |
| parent | d6b83fcf4a1a3039c06e0b1d1a1f7e2af2351efb (diff) | |
| download | tangara-fw-544b0013b104a6584660724ccd502adcccd7ca6c.tar.gz | |
persist preferred bluetooth device in nvs
Diffstat (limited to 'src/drivers/bluetooth.cpp')
| -rw-r--r-- | src/drivers/bluetooth.cpp | 21 |
1 files changed, 18 insertions, 3 deletions
diff --git a/src/drivers/bluetooth.cpp b/src/drivers/bluetooth.cpp index 79999b2c..f6992f05 100644 --- a/src/drivers/bluetooth.cpp +++ b/src/drivers/bluetooth.cpp @@ -20,6 +20,7 @@ #include "esp_wifi.h" #include "esp_wifi_types.h" #include "freertos/portmacro.h" +#include "nvs.hpp" #include "tinyfsm/include/tinyfsm.hpp" namespace drivers { @@ -55,8 +56,8 @@ auto a2dp_data_cb(uint8_t* buf, int32_t buf_size) -> int32_t { return xStreamBufferReceive(stream, buf, buf_size, 0); } -Bluetooth::Bluetooth() { - tinyfsm::FsmList<bluetooth::BluetoothState>::start(); +Bluetooth::Bluetooth(NvsStorage* storage) { + bluetooth::BluetoothState::Init(storage); } auto Bluetooth::Enable() -> bool { @@ -101,12 +102,15 @@ auto DeviceName() -> std::string { uint8_t mac[8]{0}; esp_efuse_mac_get_default(mac); std::ostringstream name; - name << "TANGARA " << std::hex << mac[0] << mac[1]; + name << "TANGARA " << std::hex << static_cast<int>(mac[0]) + << static_cast<int>(mac[1]); return name.str(); } namespace bluetooth { +NvsStorage* BluetoothState::sStorage_; + std::mutex BluetoothState::sDevicesMutex_; std::map<mac_addr_t, Device> BluetoothState::sDevices_; std::optional<mac_addr_t> BluetoothState::sPreferredDevice_; @@ -114,6 +118,12 @@ mac_addr_t BluetoothState::sCurrentDevice_; std::atomic<StreamBufferHandle_t> BluetoothState::sSource_; +auto BluetoothState::Init(NvsStorage* storage) -> void { + sStorage_ = storage; + sPreferredDevice_ = storage->PreferredBluetoothDevice(); + tinyfsm::FsmList<bluetooth::BluetoothState>::start(); +} + auto BluetoothState::devices() -> std::vector<Device> { std::lock_guard lock{sDevicesMutex_}; std::vector<Device> out; @@ -417,6 +427,11 @@ void Connecting::react(const events::internal::A2dp& ev) { void Connected::entry() { ESP_LOGI(kTag, "entering connected state"); + + auto stored_pref = sStorage_->PreferredBluetoothDevice(); + if (stored_pref != sPreferredDevice_) { + sStorage_->PreferredBluetoothDevice(sPreferredDevice_); + } // TODO: if we already have a source, immediately start playing } |
