From b192975cb1eeb4e6b7c7bf53cdf42701c55f034a Mon Sep 17 00:00:00 2001 From: jacqueline Date: Fri, 22 Sep 2023 15:48:41 +1000 Subject: make bluetooth pairing ui functional --- src/drivers/bluetooth.cpp | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) (limited to 'src/drivers/bluetooth.cpp') diff --git a/src/drivers/bluetooth.cpp b/src/drivers/bluetooth.cpp index f3373849..924cdf42 100644 --- a/src/drivers/bluetooth.cpp +++ b/src/drivers/bluetooth.cpp @@ -8,6 +8,7 @@ #include #include +#include "bluetooth_types.hpp" #include "esp_a2dp_api.h" #include "esp_avrc_api.h" #include "esp_bt.h" @@ -56,7 +57,7 @@ auto a2dp_data_cb(uint8_t* buf, int32_t buf_size) -> int32_t { return xStreamBufferReceive(stream, buf, buf_size, 0); } -Bluetooth::Bluetooth(NvsStorage* storage) { +Bluetooth::Bluetooth(NvsStorage& storage) { bluetooth::BluetoothState::Init(storage); } @@ -72,6 +73,10 @@ auto Bluetooth::Disable() -> void { bluetooth::events::Disable{}); } +auto Bluetooth::IsEnabled() -> bool { + return !bluetooth::BluetoothState::is_in_state(); +} + auto Bluetooth::KnownDevices() -> std::vector { std::vector out = bluetooth::BluetoothState::devices(); std::sort(out.begin(), out.end(), [](const auto& a, const auto& b) -> bool { @@ -98,6 +103,11 @@ auto Bluetooth::SetSource(StreamBufferHandle_t src) -> void { bluetooth::events::SourceChanged{}); } +auto Bluetooth::SetEventHandler(std::function cb) + -> void { + bluetooth::BluetoothState::event_handler(cb); +} + auto DeviceName() -> std::string { uint8_t mac[8]{0}; esp_efuse_mac_get_default(mac); @@ -111,16 +121,17 @@ namespace bluetooth { NvsStorage* BluetoothState::sStorage_; -std::mutex BluetoothState::sDevicesMutex_; -std::map BluetoothState::sDevices_; -std::optional BluetoothState::sPreferredDevice_; +std::mutex BluetoothState::sDevicesMutex_{}; +std::map BluetoothState::sDevices_{}; +std::optional BluetoothState::sPreferredDevice_{}; mac_addr_t BluetoothState::sCurrentDevice_; std::atomic BluetoothState::sSource_; +std::function BluetoothState::sEventHandler_; -auto BluetoothState::Init(NvsStorage* storage) -> void { - sStorage_ = storage; - sPreferredDevice_ = storage->PreferredBluetoothDevice().get(); +auto BluetoothState::Init(NvsStorage& storage) -> void { + sStorage_ = &storage; + sPreferredDevice_ = storage.PreferredBluetoothDevice().get(); tinyfsm::FsmList::start(); } @@ -153,6 +164,11 @@ auto BluetoothState::source(StreamBufferHandle_t src) -> void { sSource_.store(src); } +auto BluetoothState::event_handler(std::function cb) -> void { + std::lock_guard lock{sDevicesMutex_}; + sEventHandler_ = cb; +} + static bool sIsFirstEntry = true; void Disabled::entry() { @@ -303,6 +319,10 @@ auto Scanning::OnDeviceDiscovered(esp_bt_gap_cb_param_t* param) -> void { sCurrentDevice_ = device.address; is_preferred = true; } + + if (sEventHandler_) { + std::invoke(sEventHandler_, Event::kKnownDevicesChanged); + } } if (is_preferred) { -- cgit v1.2.3