From 912165d9ee36b9b5541b722fe46c401ea19c806e Mon Sep 17 00:00:00 2001 From: Florian Hoss Date: Sun, 24 Apr 2022 13:50:30 +0200 Subject: [PATCH] make it clearer to read --- ESPFirewall/lib/Firewall/src/Storage.cpp | 22 ++++++++++++++++++---- ESPFirewall/lib/Firewall/src/Storage.hpp | 9 +++++---- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/ESPFirewall/lib/Firewall/src/Storage.cpp b/ESPFirewall/lib/Firewall/src/Storage.cpp index e06b495..679403f 100644 --- a/ESPFirewall/lib/Firewall/src/Storage.cpp +++ b/ESPFirewall/lib/Firewall/src/Storage.cpp @@ -40,6 +40,12 @@ namespace fw #endif } + uint16_t Storage::eeprom_rule_position(uint8_t key) + { + // key-1 because key will be in range 1-255, but we need 1 less for multiplication + return eeprom_rules_head + key - 1 * sizeof(firewall_rule_t); + } + uint8_t Storage::retrieve_settings_value(const char *key) { #ifdef ESP32 @@ -82,10 +88,9 @@ namespace fw 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)); rule_ptr->key = key; - +#ifdef ESP32 char rulename[9]; // fwRule99\n sprintf(rulename, "fwRule%i", key); @@ -96,10 +101,13 @@ namespace fw rule_ptr->target = static_cast(this->memory.getUChar("target", TARGET_REJECT)); this->memory.end(); - return rule_ptr; #elif defined(ESP8266) - return NULL; + uint16_t eespom_position = eeprom_rule_position(key); + rule_ptr->protocol = static_cast(EEPROM.read(eespom_position)); + eespom_position += sizeof(firewall_protocol_t); + rule_ptr->target = static_cast(EEPROM.read(eespom_position)); #endif + return rule_ptr; } void Storage::store_all_firewall_rules(firewall_rule_t *head) @@ -126,6 +134,12 @@ namespace fw this->memory.putUChar("protocol", rule_ptr->protocol); this->memory.putUChar("target", rule_ptr->target); this->memory.end(); +#elif defined(ESP8266) + uint16_t eespom_position = eeprom_rule_position(rule_ptr->key); + EEPROM.write(this->eeprom_rules_head, rule_ptr->protocol); + eespom_position += sizeof(firewall_protocol_t); + EEPROM.write(this->eeprom_rules_head, rule_ptr->target); + EEPROM.commit(); #endif } diff --git a/ESPFirewall/lib/Firewall/src/Storage.hpp b/ESPFirewall/lib/Firewall/src/Storage.hpp index a86726d..147b090 100644 --- a/ESPFirewall/lib/Firewall/src/Storage.hpp +++ b/ESPFirewall/lib/Firewall/src/Storage.hpp @@ -18,13 +18,14 @@ namespace fw #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; + const uint16_t eeprom_size = 1000; + const uint8_t security_number = 93; + const uint16_t eeprom_settings_head = 0; + const uint16_t eeprom_rules_head = 10; #endif ok_t mount_spiffs(); void setup_eeprom(); + uint16_t eeprom_rule_position(uint8_t); protected: uint8_t retrieve_settings_value(const char *);