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/nvs.cpp | |
| parent | d6b83fcf4a1a3039c06e0b1d1a1f7e2af2351efb (diff) | |
| download | tangara-fw-544b0013b104a6584660724ccd502adcccd7ca6c.tar.gz | |
persist preferred bluetooth device in nvs
Diffstat (limited to 'src/drivers/nvs.cpp')
| -rw-r--r-- | src/drivers/nvs.cpp | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/src/drivers/nvs.cpp b/src/drivers/nvs.cpp index a2de9518..8c7e54a8 100644 --- a/src/drivers/nvs.cpp +++ b/src/drivers/nvs.cpp @@ -10,6 +10,7 @@ #include <cstdint> #include <memory> +#include "bluetooth.hpp" #include "esp_log.h" #include "nvs.h" #include "nvs_flash.h" @@ -20,6 +21,7 @@ static constexpr char kTag[] = "nvm"; static constexpr uint8_t kSchemaVersion = 1; static constexpr char kKeyVersion[] = "ver"; +static constexpr char kKeyBluetooth[] = "bt"; auto NvsStorage::Open() -> NvsStorage* { esp_err_t err = nvs_flash_init(); @@ -70,4 +72,24 @@ auto NvsStorage::SchemaVersion() -> uint8_t { return ret; } +auto NvsStorage::PreferredBluetoothDevice() + -> std::optional<bluetooth::mac_addr_t> { + bluetooth::mac_addr_t out{0}; + size_t size = out.size(); + if (nvs_get_blob(handle_, kKeyBluetooth, out.data(), &size) != ESP_OK) { + return {}; + } + return out; +} +auto NvsStorage::PreferredBluetoothDevice( + std::optional<bluetooth::mac_addr_t> addr) -> void { + if (!addr) { + nvs_erase_key(handle_, kKeyBluetooth); + } else { + nvs_set_blob(handle_, kKeyBluetooth, addr.value().data(), + addr.value().size()); + } + nvs_commit(handle_); +} + } // namespace drivers |
