diff --git a/ESPFirewall/lib/Firewall/src/Storage.cpp b/ESPFirewall/lib/Firewall/src/Storage.cpp index 14bfb89..bbe0030 100644 --- a/ESPFirewall/lib/Firewall/src/Storage.cpp +++ b/ESPFirewall/lib/Firewall/src/Storage.cpp @@ -7,6 +7,8 @@ namespace fw #ifdef ESP32 if (this->mount_spiffs() == ERROR) endless_loop(); +#elif defined(ESP8266) + this->setup_eeprom(); #endif } @@ -26,23 +28,36 @@ namespace fw }; } return SUCCESS; -#elif defined(ESP8266) - return NO_ACTION; +#endif + } + + ok_t Storage::setup_eeprom() + { +#ifdef ESP8266 + EEPROM.begin(this->eeprom_size); #endif } uint8_t Storage::retrieve_settings_value(const char *key) { #ifdef ESP32 - uint8_t amount_of_rules; + uint8_t value; this->memory.begin("settings", true); - amount_of_rules = memory.getUChar(key, 0); + value = memory.getUChar(key, 0); this->memory.end(); - return amount_of_rules; + return value; #elif defined(ESP8266) - return 0; + if (strncmp("amount_of_rules", key, 16)) + { + uint8_t security_number = EEPROM.read(this->eeprom_settings_head); + uint8_t amount_of_rules = EEPROM.read(this->eeprom_settings_head + 1); + + if (amount_of_rules > 50 || security_number != this->security_number) + return amount_of_rules; + return 0; + } #endif } @@ -52,6 +67,13 @@ namespace fw this->memory.begin("settings", false); this->memory.putUChar(key, new_amount); this->memory.end(); +#elif defined(ESP8266) + if (strncmp("amount_of_rules", key, 16)) + { + EEPROM.write(this->eeprom_settings_head, this->security_number); + EEPROM.write(this->eeprom_settings_head + 1, new_amount); + EEPROM.commit(); + } #endif } diff --git a/ESPFirewall/lib/Firewall/src/Storage.hpp b/ESPFirewall/lib/Firewall/src/Storage.hpp index 13f07a4..88ffecf 100644 --- a/ESPFirewall/lib/Firewall/src/Storage.hpp +++ b/ESPFirewall/lib/Firewall/src/Storage.hpp @@ -4,6 +4,9 @@ #ifdef ESP32 #include "Preferences.h" #include "SPIFFS.h" +#elif defined(ESP8266) +#include "EEPROM.h" +#include "spiffs_api.h" #endif #include "Utils.hpp" @@ -15,8 +18,14 @@ namespace fw private: #ifdef ESP32 Preferences memory; +#elif defined(ESP8266) + uint16_t eeprom_size = 1000; + uint8_t security_number = 93; + uint16_t eeprom_settings_head = 0; + uint16_t eeprom_rules_head = 10; #endif ok_t mount_spiffs(); + ok_t setup_eeprom(); protected: uint8_t retrieve_settings_value(const char *);