diff --git a/CMakeLists.txt b/CMakeLists.txt index 243db0d..c84612a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,4 +5,4 @@ cmake_minimum_required(VERSION 3.5) include($ENV{IDF_PATH}/tools/cmake/project.cmake) -project(blackmagick) +project(blackmagic) diff --git a/components/mlib/CMakeLists.txt b/components/mlib/CMakeLists.txt index 641b840..a92b4d7 100644 --- a/components/mlib/CMakeLists.txt +++ b/components/mlib/CMakeLists.txt @@ -1,3 +1,3 @@ -idf_component_register(SRCS "m_string.c" +idf_component_register(SRCS "m-string.c" PRIV_INCLUDE_DIRS "mlib" INCLUDE_DIRS ".") \ No newline at end of file diff --git a/components/mlib/m_string.c b/components/mlib/m-string.c similarity index 83% rename from components/mlib/m_string.c rename to components/mlib/m-string.c index 97c2511..ebb82d6 100644 --- a/components/mlib/m_string.c +++ b/components/mlib/m-string.c @@ -1,5 +1,5 @@ #include "mlib\m-string.h" -#include "m_string.h" +#include "m-string.h" struct mstring_t { string_t str; @@ -27,6 +27,15 @@ void mstring_set(mstring_t* mstr, const char* text) { string_set_str(mstr->str, text); } +void mstring_reset(mstring_t* mstr) { + string_reset(mstr->str); +} + +void mstring_cat(mstring_t* mstr, const char* text) +{ + string_cat_str(mstr->str, text); +} + int mstring_printf(mstring_t* mstr, const char format[], ...) { va_list args; va_start(args, format); diff --git a/components/mlib/m_string.h b/components/mlib/m-string.h similarity index 75% rename from components/mlib/m_string.h rename to components/mlib/m-string.h index 6a211f6..a123f54 100644 --- a/components/mlib/m_string.h +++ b/components/mlib/m-string.h @@ -1,5 +1,5 @@ /** - * @file m_string.h + * @file m-string.h * * M*Lib string wrapper, to prevent inline code pollution * @@ -34,6 +34,20 @@ void mstring_free(mstring_t* mstr); */ void mstring_set(mstring_t* mstr, const char* text); +/** + * Reset string value + * @param mstr + * @param text + */ +void mstring_reset(mstring_t* mstr); + +/** + * Concatenate value + * @param mstr + * @param text + */ +void mstring_cat(mstring_t* mstr, const char* text); + /** * Printf string value * @param mstr diff --git a/main/nvs.c b/main/nvs.c index c8d0325..e5d6248 100644 --- a/main/nvs.c +++ b/main/nvs.c @@ -1,11 +1,63 @@ #include #include +#include "nvs.h" + +#define TAG "nvs" +#define NVS_STORE "nvs_storage" void nvs_init(void) { + ESP_LOGI(TAG, "init"); esp_err_t ret = nvs_flash_init(); if(ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { + ESP_LOGI(TAG, "erasing"); ESP_ERROR_CHECK(nvs_flash_erase()); ret = nvs_flash_init(); } ESP_ERROR_CHECK(ret); + ESP_LOGI(TAG, "done"); } + +esp_err_t nvs_save_string(const char* key, const mstring_t* value) { + nvs_handle_t nvs_handle; + esp_err_t err; + + err = nvs_open(NVS_STORE, NVS_READWRITE, &nvs_handle); + if(err != ESP_OK) return err; + + err = nvs_set_str(nvs_handle, key, mstring_get_cstr(value)); + if(err != ESP_OK) return err; + + err = nvs_commit(nvs_handle); + if(err != ESP_OK) return err; + + nvs_close(nvs_handle); + return ESP_OK; +} + +esp_err_t nvs_load_string(const char* key, mstring_t* value) { + nvs_handle_t nvs_handle; + esp_err_t err; + char* buffer = NULL; + + do { + err = nvs_open(NVS_STORE, NVS_READWRITE, &nvs_handle); + if(err != ESP_OK) return err; + + size_t required_size = 0; + err = nvs_get_str(nvs_handle, key, NULL, &required_size); + if(err != ESP_OK) return err; + + buffer = malloc(required_size + sizeof(uint32_t)); + err = nvs_get_str(nvs_handle, key, buffer, &required_size); + if(err != ESP_OK) return err; + + mstring_set(value, buffer); + + nvs_close(nvs_handle); + err = ESP_OK; + } while(0); + + if(buffer != NULL) free(buffer); + + return err; +} \ No newline at end of file diff --git a/main/nvs.h b/main/nvs.h index caa8010..01b2984 100644 --- a/main/nvs.h +++ b/main/nvs.h @@ -1,3 +1,7 @@ #pragma once +#include +#include -void nvs_init(void); \ No newline at end of file +void nvs_init(void); +esp_err_t nvs_save_string(const char* key, const mstring_t* value); +esp_err_t nvs_load_string(const char* key, mstring_t* value); \ No newline at end of file