Work in Progress on storage

This commit is contained in:
Florian Hoss 2022-06-12 11:07:55 +02:00
parent 265e0562f8
commit 6461b71799
2 changed files with 17 additions and 17 deletions

View file

@ -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);

View file

@ -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);