summaryrefslogtreecommitdiff
path: root/src/drivers/bluetooth.cpp
diff options
context:
space:
mode:
authorjacqueline <me@jacqueline.id.au>2023-09-22 15:48:41 +1000
committerjacqueline <me@jacqueline.id.au>2023-09-22 21:19:19 +1000
commitb192975cb1eeb4e6b7c7bf53cdf42701c55f034a (patch)
tree39755851543d596f2630704be9efb56be1f39bfc /src/drivers/bluetooth.cpp
parentcbd99b2134c6c471708deb409a4b0fcc4c31516d (diff)
downloadtangara-fw-b192975cb1eeb4e6b7c7bf53cdf42701c55f034a.tar.gz
make bluetooth pairing ui functional
Diffstat (limited to 'src/drivers/bluetooth.cpp')
-rw-r--r--src/drivers/bluetooth.cpp34
1 files changed, 27 insertions, 7 deletions
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 <ostream>
#include <sstream>
+#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<bluetooth::Disabled>();
+}
+
auto Bluetooth::KnownDevices() -> std::vector<bluetooth::Device> {
std::vector<bluetooth::Device> 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<void(bluetooth::Event)> 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<mac_addr_t, Device> BluetoothState::sDevices_;
-std::optional<mac_addr_t> BluetoothState::sPreferredDevice_;
+std::mutex BluetoothState::sDevicesMutex_{};
+std::map<mac_addr_t, Device> BluetoothState::sDevices_{};
+std::optional<mac_addr_t> BluetoothState::sPreferredDevice_{};
mac_addr_t BluetoothState::sCurrentDevice_;
std::atomic<StreamBufferHandle_t> BluetoothState::sSource_;
+std::function<void(Event)> 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<bluetooth::BluetoothState>::start();
}
@@ -153,6 +164,11 @@ auto BluetoothState::source(StreamBufferHandle_t src) -> void {
sSource_.store(src);
}
+auto BluetoothState::event_handler(std::function<void(Event)> 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) {