-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ea9cf7f
commit f4e2256
Showing
4 changed files
with
277 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
187 changes: 187 additions & 0 deletions
187
firmware/main/general/general_flash_storage/general_flash_storage.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
#include "general_flash_storage.h" | ||
#include <stdio.h> | ||
#include <string.h> | ||
#include "esp_log.h" | ||
#include "nvs.h" | ||
#include "nvs_flash.h" | ||
#include "preferences.h" | ||
|
||
#define FS_TREE_MAIN_COUNT "fsmc" | ||
#define FS_TREE_SUBITEM_COUNT "fsmc" | ||
#define FS_TREE_MAIN_PREFIX "fsm" | ||
#define FS_TREE_SUBITEM_SUFIX "si" | ||
#define MAX_LEN_STRING 1024 | ||
#define MAX_NVS_CHARS 15 | ||
|
||
static const char* TAG = "flash_storage"; | ||
static esp_err_t err; | ||
|
||
static bool flash_storage_exist_main_item(char* base_name) { | ||
char* idx_main_item = malloc(MAX_NVS_CHARS); | ||
char* main_item = malloc(MAX_NVS_CHARS); | ||
bool return_val = false; | ||
esp_err_t err; | ||
|
||
uint16_t main_count = preferences_get_ushort(FS_TREE_MAIN_COUNT, 0); | ||
|
||
for (int i = 0; i < main_count; i++) { | ||
sprintf(idx_main_item, "%d%s", i, FS_TREE_MAIN_PREFIX); | ||
err = preferences_get_string(idx_main_item, main_item, MAX_LEN_STRING); | ||
if (err != ESP_OK) { | ||
ESP_LOGW(TAG, "No item found: %s", esp_err_to_name(err)); | ||
continue; | ||
} | ||
if (strcmp(base_name, main_item) == 0) { | ||
return_val = true; | ||
break; | ||
} | ||
} | ||
free(idx_main_item); | ||
free(main_item); | ||
return return_val; | ||
} | ||
|
||
static esp_err_t flash_storage_save_main_item(char* base_name) { | ||
uint16_t item_count = preferences_get_ushort(FS_TREE_MAIN_COUNT, 0); | ||
char* idx_item = malloc(MAX_NVS_CHARS); | ||
|
||
sprintf(idx_item, "%d%s", item_count, FS_TREE_MAIN_PREFIX); | ||
err = preferences_put_string(idx_item, base_name); | ||
if (err != ESP_OK) { | ||
ESP_LOGW(TAG, "No item saved: %s", esp_err_to_name(err)); | ||
return err; | ||
} | ||
item_count++; | ||
err = preferences_put_ushort(FS_TREE_MAIN_COUNT, item_count); | ||
if (err != ESP_OK) { | ||
ESP_LOGW(TAG, "Error saving count: %s", esp_err_to_name(err)); | ||
return err; | ||
} | ||
free(idx_item); | ||
return err; | ||
} | ||
|
||
static void flash_storage_save_subitem(storage_contex_t* storage_context) { | ||
char* idx_item = malloc(MAX_NVS_CHARS); | ||
char* main_item_str = malloc(MAX_NVS_CHARS); | ||
char* subitem_str = malloc(MAX_NVS_CHARS); | ||
char* idx_subitem_count = malloc(MAX_NVS_CHARS); | ||
char* main_subitem = malloc(MAX_NVS_CHARS); | ||
char* main_subitem_val = malloc(MAX_LEN_STRING); | ||
|
||
// First get the main item for the class | ||
uint16_t main_count = preferences_get_ushort(FS_TREE_MAIN_COUNT, 0); | ||
|
||
for (int i = 0; i < main_count; i++) { | ||
sprintf(idx_item, "%d%s", i, FS_TREE_MAIN_PREFIX); | ||
err = preferences_get_string(idx_item, main_item_str, MAX_LEN_STRING); | ||
if (err != ESP_OK) { | ||
ESP_LOGW(TAG, "No item found: %s", esp_err_to_name(err)); | ||
continue; | ||
} | ||
if (strcmp(main_item_str, storage_context->main_storage_name) == 0) { | ||
break; | ||
} | ||
} | ||
// 0fsm | ||
sprintf(idx_subitem_count, "%sc", main_item_str); | ||
uint16_t subitem_count = preferences_get_ushort(idx_subitem_count, 0); | ||
sprintf(main_subitem, "%d%s", subitem_count, idx_item); | ||
|
||
err = | ||
preferences_put_string(main_subitem, storage_context->item_storage_name); | ||
if (err != ESP_OK) { | ||
ESP_LOGW(TAG, "No item saved: %s", esp_err_to_name(err)); | ||
return; | ||
} | ||
subitem_count++; | ||
err = preferences_put_ushort(idx_subitem_count, subitem_count); | ||
if (err != ESP_OK) { | ||
ESP_LOGW(TAG, "Error saving count: %s", esp_err_to_name(err)); | ||
return; | ||
} | ||
|
||
sprintf(main_subitem_val, "%sv", main_subitem); | ||
err = preferences_put_string(main_subitem_val, | ||
storage_context->items_storage_value); | ||
if (err != ESP_OK) { | ||
ESP_LOGW(TAG, "No item saved: %s", esp_err_to_name(err)); | ||
return; | ||
} | ||
|
||
free(idx_item); | ||
free(main_item_str); | ||
free(subitem_str); | ||
free(idx_subitem_count); | ||
free(main_subitem); | ||
free(main_subitem_val); | ||
} | ||
|
||
void flash_storage_show_list(char* main_tree) { | ||
char* idx_main_item = malloc(MAX_NVS_CHARS); | ||
char* main_item = malloc(MAX_NVS_CHARS); | ||
char* idx_subitem = malloc(MAX_NVS_CHARS); | ||
char* idx_subitem_count = malloc(MAX_NVS_CHARS); | ||
char* tree_subitem_str = malloc(MAX_NVS_CHARS); | ||
char* tree_subitem = malloc(MAX_NVS_CHARS); | ||
char* tree_subitem_val = malloc(MAX_LEN_STRING); | ||
esp_err_t err; | ||
|
||
uint16_t main_count = preferences_get_ushort(FS_TREE_MAIN_COUNT, 0); | ||
|
||
for (int i = 0; i < main_count; i++) { | ||
sprintf(idx_main_item, "%d%s", i, FS_TREE_MAIN_PREFIX); | ||
err = preferences_get_string(idx_main_item, main_item, MAX_LEN_STRING); | ||
if (err != ESP_OK) { | ||
ESP_LOGW(TAG, "No item found: %s", esp_err_to_name(err)); | ||
continue; | ||
} | ||
if (strcmp(main_tree, main_item) == 0) { | ||
break; | ||
} | ||
} | ||
|
||
ESP_LOGI(TAG, "Main item: %s", main_tree); | ||
|
||
sprintf(idx_subitem_count, "%sc", main_item); | ||
uint16_t subitem_count = preferences_get_ushort(idx_subitem_count, 0); | ||
for (int j = 0; j < subitem_count; j++) { | ||
sprintf(idx_subitem, "%d%s", j, idx_main_item); | ||
err = preferences_get_string(idx_subitem, tree_subitem, MAX_LEN_STRING); | ||
if (err != ESP_OK) { | ||
ESP_LOGW(TAG, "No item found: %s", esp_err_to_name(err)); | ||
continue; | ||
} | ||
sprintf(tree_subitem_str, "%sv", idx_subitem); | ||
err = preferences_get_string(tree_subitem_str, tree_subitem_val, | ||
MAX_LEN_STRING); | ||
if (err != ESP_OK) { | ||
ESP_LOGW(TAG, "No item found: %s", esp_err_to_name(err)); | ||
continue; | ||
} | ||
ESP_LOGI(TAG, "Subitem: %s:%s", tree_subitem, tree_subitem_val); | ||
} | ||
|
||
free(idx_main_item); | ||
free(main_item); | ||
free(idx_subitem_count); | ||
free(idx_subitem); | ||
free(tree_subitem); | ||
free(tree_subitem_str); | ||
free(tree_subitem_val); | ||
} | ||
|
||
void flash_storage_save_list_items(storage_contex_t* storage_context) { | ||
uint16_t main_count = preferences_get_ushort(FS_TREE_MAIN_COUNT, 0); | ||
uint16_t subitems_count = preferences_get_ushort(FS_TREE_SUBITEM_COUNT, 0); | ||
|
||
if (!flash_storage_exist_main_item(storage_context->main_storage_name)) { | ||
err = flash_storage_save_main_item(storage_context->main_storage_name); | ||
if (err != ESP_OK) { | ||
ESP_LOGW(TAG, "Error saving: %s", esp_err_to_name(err)); | ||
return; | ||
} | ||
} | ||
|
||
flash_storage_save_subitem(storage_context); | ||
} |
39 changes: 39 additions & 0 deletions
39
firmware/main/general/general_flash_storage/general_flash_storage.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#pragma once | ||
#include "esp_err.h" | ||
|
||
#define GENERAL_FLASH_STORAGE_COUNT_LIMIT 99 | ||
|
||
typedef enum { | ||
GFS_SPAM, | ||
GFS_WIFI, | ||
GFS_GENERALS, | ||
GFS_COUNT | ||
} storage_categories_t; | ||
|
||
typedef struct { | ||
char* main_storage_name; | ||
char* item_storage_name; | ||
char* items_storage_value; | ||
storage_categories_t category; | ||
} storage_contex_t; | ||
|
||
/* @brief Get the value in flash as the form of prefix_basename | ||
@param char base_name - The name of the option | ||
@param char str_value - The array to save the returned value | ||
*/ | ||
esp_err_t flash_storage_get_str_item(char* base_name, char* str_value); | ||
|
||
/* @brief Save the value in flash as the form of prefix_basename | ||
@param char base_name - The name of the option | ||
@param char value - The value | ||
*/ | ||
esp_err_t flash_storage_save_str_item(char* base_name, char* value); | ||
|
||
/* @brief Delete the value in flash as the form of prefix_basename | ||
@param char base_name - The name of the option | ||
*/ | ||
esp_err_t flash_storage_delete_str_item(char* base_name); | ||
|
||
void flash_storage_save_list_items(storage_contex_t* storage_context); | ||
esp_err_t flash_storage_delete_uint32_item(char* base_name); | ||
void flash_storage_show_list(char* main_tree); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters