storage on esp8266 will be next

This commit is contained in:
Florian Hoss 2022-04-24 00:21:42 +02:00
parent 59b1a6b15f
commit 2cde69a34a
2 changed files with 26 additions and 1 deletions

View file

@ -4,8 +4,10 @@ namespace fw
{ {
Storage::Storage() Storage::Storage()
{ {
#ifdef ESP32
if (this->mount_spiffs() == ERROR) if (this->mount_spiffs() == ERROR)
endless_loop(); endless_loop();
#endif
} }
Storage::~Storage() Storage::~Storage()
@ -14,19 +16,24 @@ namespace fw
ok_t Storage::mount_spiffs() ok_t Storage::mount_spiffs()
{ {
#ifdef ESP32
if (!SPIFFS.begin(false)) if (!SPIFFS.begin(false))
{ {
if (!SPIFFS.begin(true)) if (!SPIFFS.begin(true))
{ {
log_e("SPIFFS cannot be mounted"); Serial.println("SPIFFS cannot be mounted");
return ERROR; return ERROR;
}; };
} }
return SUCCESS; return SUCCESS;
#elif defined(ESP8266)
return NO_ACTION;
#endif
} }
uint8_t Storage::retrieve_settings_value(const char *key) uint8_t Storage::retrieve_settings_value(const char *key)
{ {
#ifdef ESP32
uint8_t amount_of_rules; uint8_t amount_of_rules;
this->memory.begin("settings", true); this->memory.begin("settings", true);
@ -34,17 +41,23 @@ namespace fw
this->memory.end(); this->memory.end();
return amount_of_rules; return amount_of_rules;
#elif defined(ESP8266)
return 0;
#endif
} }
void Storage::store_settings_value(const char *key, const uint8_t new_amount) void Storage::store_settings_value(const char *key, const uint8_t new_amount)
{ {
#ifdef ESP32
this->memory.begin("settings", false); this->memory.begin("settings", false);
this->memory.putUChar(key, new_amount); this->memory.putUChar(key, new_amount);
this->memory.end(); this->memory.end();
#endif
} }
firewall_rule_t *Storage::retrieve_firewall_rule(const uint8_t key) firewall_rule_t *Storage::retrieve_firewall_rule(const uint8_t key)
{ {
#ifdef ESP32
firewall_rule_t *rule_ptr = (firewall_rule_t *)malloc(sizeof(firewall_rule_t)); firewall_rule_t *rule_ptr = (firewall_rule_t *)malloc(sizeof(firewall_rule_t));
rule_ptr->key = key; rule_ptr->key = key;
@ -59,20 +72,26 @@ namespace fw
this->memory.end(); this->memory.end();
return rule_ptr; return rule_ptr;
#elif defined(ESP8266)
return NULL;
#endif
} }
void Storage::store_all_firewall_rules(firewall_rule_t *head) void Storage::store_all_firewall_rules(firewall_rule_t *head)
{ {
#ifdef ESP32
firewall_rule_t *temp = head; firewall_rule_t *temp = head;
while (temp != NULL) while (temp != NULL)
{ {
store_firewall_rule(temp); store_firewall_rule(temp);
temp = temp->next; temp = temp->next;
} }
#endif
} }
void Storage::store_firewall_rule(firewall_rule_t *rule_ptr) void Storage::store_firewall_rule(firewall_rule_t *rule_ptr)
{ {
#ifdef ESP32
char rulename[9]; // fwRule99\n char rulename[9]; // fwRule99\n
sprintf(rulename, "fwRule%i", rule_ptr->key); sprintf(rulename, "fwRule%i", rule_ptr->key);
@ -82,6 +101,7 @@ namespace fw
this->memory.putUChar("protocol", rule_ptr->protocol); this->memory.putUChar("protocol", rule_ptr->protocol);
this->memory.putUChar("target", rule_ptr->target); this->memory.putUChar("target", rule_ptr->target);
this->memory.end(); this->memory.end();
#endif
} }
// httpsserver::SSLCert *Storage::retrieve_certificate() // httpsserver::SSLCert *Storage::retrieve_certificate()

View file

@ -1,8 +1,11 @@
#ifndef ESP32_STORAGE_HPP #ifndef ESP32_STORAGE_HPP
#define ESP32_STORAGE_HPP #define ESP32_STORAGE_HPP
#ifdef ESP32
#include "Preferences.h" #include "Preferences.h"
#include "SPIFFS.h" #include "SPIFFS.h"
#endif
#include "Utils.hpp" #include "Utils.hpp"
namespace fw namespace fw
@ -10,7 +13,9 @@ namespace fw
class Storage class Storage
{ {
private: private:
#ifdef ESP32
Preferences memory; Preferences memory;
#endif
ok_t mount_spiffs(); ok_t mount_spiffs();
protected: protected: