Squash of branch setup
This commit is contained in:
70
main/io/nvs_handler.h
Normal file
70
main/io/nvs_handler.h
Normal file
@@ -0,0 +1,70 @@
|
||||
#pragma once
|
||||
#include "io/io.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "nvs.h"
|
||||
|
||||
struct NVSIteratorGuard {
|
||||
public:
|
||||
~NVSIteratorGuard() {
|
||||
if (iterator) {
|
||||
nvs_release_iterator(iterator);
|
||||
}
|
||||
}
|
||||
|
||||
// accessors to the iterator, the internal state should not be modified directly
|
||||
// The iterator is advanced using advance_iter(), and is changed to nullptr on error or end
|
||||
// Caller MUST NOT release the iterator manually nor call get_iterator after advance_iter
|
||||
const nvs_iterator_t& get_iterator() const {
|
||||
return iterator;
|
||||
}
|
||||
|
||||
void advance_iter() {
|
||||
if (iterator) {
|
||||
// advance the iterator and update the internal state
|
||||
esp_err_t err = nvs_entry_next(&iterator);
|
||||
if (err != ESP_OK) {
|
||||
error = err;
|
||||
iterator = nullptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
esp_err_t get_error() const {
|
||||
return error;
|
||||
}
|
||||
bool is_valid() const {
|
||||
return iterator != nullptr && error == ESP_OK;
|
||||
}
|
||||
friend class NVSStorageHandler;
|
||||
private:
|
||||
NVSIteratorGuard(nvs_iterator_t it
|
||||
, esp_err_t err
|
||||
) : iterator(it), error(err) { }
|
||||
nvs_iterator_t iterator;
|
||||
esp_err_t error;
|
||||
};
|
||||
|
||||
class NVSStorageHandler : public KVStorageHandler {
|
||||
public:
|
||||
NVSStorageHandler(
|
||||
const char* name_space
|
||||
);
|
||||
~NVSStorageHandler() override;
|
||||
|
||||
void init(const EventGroupHandle_t& system_event_group) override;
|
||||
|
||||
void put(const std::string& key, const std::string& value) override;
|
||||
|
||||
std::string get(const std::string& key) const override;
|
||||
esp_err_t process_all(KeyValueProcessor processor, void* arg) const override;
|
||||
esp_err_t process_filtered(const std::string& key_prefix, KeyValueProcessor processor, void* arg) const override;
|
||||
esp_err_t process_filtered(FilterFunc filter_func, KeyValueProcessor processor, void* arg) const override;
|
||||
|
||||
void remove(const std::string& key) override;
|
||||
|
||||
private:
|
||||
NVSIteratorGuard create_iterator() const;
|
||||
|
||||
nvs_handle_t nvsHandle = 0;
|
||||
const char* name_space;
|
||||
};
|
||||
Reference in New Issue
Block a user