Major cleanup, looking at it after break
This commit is contained in:
parent
6461b71799
commit
f757b06235
8 changed files with 55 additions and 203 deletions
|
@ -4,25 +4,17 @@ namespace fw
|
|||
{
|
||||
Storage::Storage()
|
||||
{
|
||||
#ifdef ESP32
|
||||
#elif defined(ESP8266)
|
||||
this->setup_eeprom();
|
||||
#if defined(ESP8266)
|
||||
this->max_rules = 15;
|
||||
this->eeprom_amount_of_rules = 0;
|
||||
this->eeprom_rules_head = 1;
|
||||
this->eeprom_size = this->max_rules * sizeof(firewall_rule_t) + eeprom_rules_head;
|
||||
EEPROM.begin(this->eeprom_size);
|
||||
#endif
|
||||
}
|
||||
|
||||
Storage::~Storage()
|
||||
{
|
||||
#ifdef ESP32
|
||||
#elif defined(ESP8266)
|
||||
#endif
|
||||
}
|
||||
|
||||
void Storage::setup_eeprom()
|
||||
{
|
||||
#ifdef ESP32
|
||||
#elif defined(ESP8266)
|
||||
EEPROM.begin(this->eeprom_size);
|
||||
#endif
|
||||
}
|
||||
|
||||
uint16_t Storage::eeprom_rule_position(uint8_t key)
|
||||
|
@ -30,49 +22,36 @@ namespace fw
|
|||
#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;
|
||||
return eeprom_rules_head + (key - 1) * sizeof(firewall_rule_t);
|
||||
#endif
|
||||
}
|
||||
|
||||
uint8_t Storage::retrieve_settings_value(const char *key)
|
||||
uint8_t Storage::retrieve_amount_of_rules()
|
||||
{
|
||||
#ifdef ESP32
|
||||
uint8_t value;
|
||||
|
||||
this->memory.begin("settings", true);
|
||||
value = memory.getUChar(key, 0);
|
||||
const uint8_t value = memory.getUChar("amount_of_rules", 0);
|
||||
this->memory.end();
|
||||
|
||||
return value;
|
||||
#elif defined(ESP8266)
|
||||
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);
|
||||
const uint8_t amount_of_rules = EEPROM.read(this->eeprom_amount_of_rules);
|
||||
|
||||
if (amount_of_rules > 50 || security_number != this->security_number)
|
||||
return 0;
|
||||
return amount_of_rules;
|
||||
}
|
||||
return 0;
|
||||
if (amount_of_rules > this->max_rules)
|
||||
return 0;
|
||||
return amount_of_rules;
|
||||
#endif
|
||||
}
|
||||
|
||||
void Storage::store_settings_value(const char *key, const uint8_t new_amount)
|
||||
void Storage::store_amount_of_rules(const uint8_t new_amount)
|
||||
{
|
||||
#ifdef ESP32
|
||||
this->memory.begin("settings", false);
|
||||
this->memory.putUChar(key, new_amount);
|
||||
this->memory.putUChar("amount_of_rules", new_amount);
|
||||
this->memory.end();
|
||||
#elif defined(ESP8266)
|
||||
if (strncmp("amount_of_rules", key, 16) == 0)
|
||||
{
|
||||
EEPROM.write(this->eeprom_settings_head, this->security_number);
|
||||
EEPROM.write(this->eeprom_amout_of_rules_head, new_amount);
|
||||
EEPROM.commit();
|
||||
}
|
||||
EEPROM.put(this->eeprom_amount_of_rules, new_amount);
|
||||
EEPROM.commit();
|
||||
#endif
|
||||
}
|
||||
|
||||
|
@ -94,22 +73,12 @@ namespace fw
|
|||
|
||||
#elif defined(ESP8266)
|
||||
uint16_t eespom_position = eeprom_rule_position(key);
|
||||
const char source[IPV4ADDRESS_LENGTH] = "";
|
||||
|
||||
EEPROM.get(eespom_position, source);
|
||||
strncpy(rule_ptr->ip, source, sizeof(rule_ptr->ip));
|
||||
eespom_position += sizeof(rule_ptr->ip);
|
||||
|
||||
rule_ptr->port_from = EEPROM.read(eespom_position);
|
||||
eespom_position += sizeof(rule_ptr->port_from);
|
||||
|
||||
rule_ptr->port_to = EEPROM.read(eespom_position);
|
||||
eespom_position += sizeof(rule_ptr->port_to);
|
||||
|
||||
rule_ptr->protocol = static_cast<firewall_protocol_t>(EEPROM.read(eespom_position));
|
||||
eespom_position += sizeof(rule_ptr->protocol);
|
||||
|
||||
rule_ptr->target = static_cast<firewall_target_t>(EEPROM.read(eespom_position));
|
||||
EEPROM.get(eespom_position, rule_ptr->ip);
|
||||
EEPROM.get(eespom_position += sizeof(rule_ptr->ip), rule_ptr->port_from);
|
||||
EEPROM.get(eespom_position += sizeof(rule_ptr->port_from), rule_ptr->port_to);
|
||||
EEPROM.get(eespom_position += sizeof(rule_ptr->port_to), rule_ptr->protocol);
|
||||
EEPROM.get(eespom_position += sizeof(rule_ptr->protocol), rule_ptr->target);
|
||||
#endif
|
||||
return rule_ptr;
|
||||
}
|
||||
|
@ -144,14 +113,10 @@ namespace fw
|
|||
uint16_t eespom_position = eeprom_rule_position(rule_ptr->key);
|
||||
|
||||
EEPROM.put(eespom_position, rule_ptr->ip);
|
||||
eespom_position += sizeof(rule_ptr->ip);
|
||||
EEPROM.put(eespom_position, rule_ptr->port_from);
|
||||
eespom_position += sizeof(rule_ptr->port_from);
|
||||
EEPROM.put(eespom_position, rule_ptr->port_to);
|
||||
eespom_position += sizeof(rule_ptr->port_to);
|
||||
EEPROM.put(eespom_position, rule_ptr->protocol);
|
||||
eespom_position += sizeof(rule_ptr->protocol);
|
||||
EEPROM.put(eespom_position, rule_ptr->target);
|
||||
EEPROM.put(eespom_position += sizeof(rule_ptr->ip), rule_ptr->port_from);
|
||||
EEPROM.put(eespom_position += sizeof(rule_ptr->port_from), rule_ptr->port_to);
|
||||
EEPROM.put(eespom_position += sizeof(rule_ptr->port_to), rule_ptr->protocol);
|
||||
EEPROM.put(eespom_position += sizeof(rule_ptr->protocol), rule_ptr->target);
|
||||
|
||||
EEPROM.commit();
|
||||
#endif
|
||||
|
|
Reference in a new issue