From 6461b71799a39a846af6cb9adea59aaf8328ecb7 Mon Sep 17 00:00:00 2001 From: Florian Hoss Date: Sun, 12 Jun 2022 11:07:55 +0200 Subject: [PATCH] Work in Progress on storage --- ESPFirewall/lib/Firewall/src/Storage.cpp | 27 ++++++++++++------------ ESPFirewall/lib/Firewall/src/Storage.hpp | 7 +++--- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/ESPFirewall/lib/Firewall/src/Storage.cpp b/ESPFirewall/lib/Firewall/src/Storage.cpp index 5b7acc0..4ae2849 100644 --- a/ESPFirewall/lib/Firewall/src/Storage.cpp +++ b/ESPFirewall/lib/Firewall/src/Storage.cpp @@ -4,36 +4,35 @@ namespace fw { Storage::Storage() { -#ifdef ESP8266 +#ifdef ESP32 +#elif defined(ESP8266) this->setup_eeprom(); #endif } Storage::~Storage() { +#ifdef ESP32 +#elif defined(ESP8266) +#endif } void Storage::setup_eeprom() { -#ifdef ESP8266 +#ifdef ESP32 +#elif defined(ESP8266) EEPROM.begin(this->eeprom_size); #endif } uint16_t Storage::eeprom_rule_position(uint8_t key) { -#ifdef ESP8266 - firewall_rule_t rule; - uint8_t total_space_needed = 0; - total_space_needed += sizeof(rule.ip); - total_space_needed += sizeof(rule.port_from); - total_space_needed += sizeof(rule.port_to); - total_space_needed += sizeof(rule.target); - total_space_needed += sizeof(rule.protocol); - // key-1 because key will be in range 1-255, but we need 1 less for multiplication - return eeprom_rules_head + (key - 1) * total_space_needed; -#elif defined(ESP32) +#ifdef ESP32 return 0; +#elif defined(ESP8266) + uint8_t total_space_needed = sizeof(firewall_rule_t); + // key will be in range 1-255, but we need 1 less for multiplication to work + return eeprom_rules_head + (key - 1) * total_space_needed; #endif } @@ -48,7 +47,7 @@ namespace fw return value; #elif defined(ESP8266) - if (strncmp("amount_of_rules", key, 16) == 0) + if (strncmp(amount, key, sizeof(amount)) == 0) { uint8_t security_number = EEPROM.read(this->eeprom_settings_head); uint8_t amount_of_rules = EEPROM.read(this->eeprom_amout_of_rules_head); diff --git a/ESPFirewall/lib/Firewall/src/Storage.hpp b/ESPFirewall/lib/Firewall/src/Storage.hpp index 1f44da4..395733b 100644 --- a/ESPFirewall/lib/Firewall/src/Storage.hpp +++ b/ESPFirewall/lib/Firewall/src/Storage.hpp @@ -21,13 +21,14 @@ namespace fw #ifdef ESP32 Preferences memory; #elif defined(ESP8266) - // Storage per firewall rule is 42 Byte - // Space for 15 Rules is 630 Byte plus 10 byte for settings - const uint16_t eeprom_size = 640; + // Space for 15 Rules plus 10 byte for settings + const uint16_t eeprom_size = 15 * sizeof(firewall_rule_t) + 10; + // Ramdon security number to check if settings have been written by this const uint8_t security_number = 93; const uint16_t eeprom_settings_head = 0; const uint16_t eeprom_amout_of_rules_head = eeprom_settings_head + 1; const uint16_t eeprom_rules_head = 10; + const char *amount = "amount_of_rules"; #endif void setup_eeprom(); uint16_t eeprom_rule_position(uint8_t key);