From d05ccef834b4694115eb1a204cfb58719fcb8497 Mon Sep 17 00:00:00 2001 From: Florian Hoss Date: Wed, 20 Apr 2022 15:43:02 +0200 Subject: [PATCH] use preferences instead of eeprom --- .../arduino/lib/Firewall/esp32Storage.cpp | 92 ++++++++----------- .../arduino/lib/Firewall/esp32Storage.hpp | 23 ++--- 2 files changed, 46 insertions(+), 69 deletions(-) diff --git a/SourceCode/arduino/lib/Firewall/esp32Storage.cpp b/SourceCode/arduino/lib/Firewall/esp32Storage.cpp index a4b718f..2474c49 100644 --- a/SourceCode/arduino/lib/Firewall/esp32Storage.cpp +++ b/SourceCode/arduino/lib/Firewall/esp32Storage.cpp @@ -2,69 +2,55 @@ namespace firewall { - Storage::Storage(const uint16_t eeprom_size) + Storage::Storage() { - this->get_eeprom_hash(); - this->eeprom_size = eeprom_size; - EEPROM.begin(this->eeprom_size); - this->amount_of_rules = EEPROM.read(this->settings_head); - log_i("Amount of Rules: %i", this->amount_of_rules); } Storage::~Storage() { } - void Storage::clear_eeprom() + uint8_t Storage::retrieve_amount_of_firewall_rules() { - - for (int i = 0; i < this->eeprom_size; i++) - { - EEPROM.write(i, 0); - } + uint8_t amount_of_rules; + this->preferences.begin("settings", false); + amount_of_rules = preferences.getUChar("amount_of_rules", 0); + this->preferences.end(); + return amount_of_rules; } - void Storage::get_eeprom_hash() + void Storage::store_amount_of_firewall_rules(const uint8_t new_amount) { - char buffer[this->eeprom_size] = {0}; - for (int i = 0; i < this->eeprom_size; i++) - { - buffer[i] = EEPROM.readChar(i); - } - unsigned char *hashedPayload = get_hash(buffer); - for (int i = 0; i < sizeof(hashedPayload); i++) - { - char str[3]; - sprintf(str, "%02x", (int)hashedPayload[i]); - Serial.print(str); - } + this->preferences.begin("settings", false); + this->preferences.putUChar("amount_of_rules", new_amount); + this->preferences.end(); + } + + firewall_rule_t *Storage::retrieve_firewall_rule(const uint8_t key) + { + char rulename[12]; + firewall_rule_t *rule_ptr = (firewall_rule_t *)malloc(sizeof(firewall_rule_t)); + sprintf(rulename, "fw_rule_%i", key); + this->preferences.begin(rulename, false); + rule_ptr->key = key; + strcpy(rule_ptr->source, this->preferences.getString("source").c_str()); + strcpy(rule_ptr->destination, this->preferences.getString("destination").c_str()); + rule_ptr->protocol = static_cast(this->preferences.getUChar("protocol")); + rule_ptr->target = static_cast(this->preferences.getUChar("target")); + this->preferences.end(); + return rule_ptr; + } + + void Storage::store_firewall_rule(const uint8_t &new_amount, firewall_rule_t *rule_ptr) + { + this->store_amount_of_firewall_rules(new_amount); + char rulename[12]; + sprintf(rulename, "fw_rule_%i", rule_ptr->key); + this->preferences.begin(rulename, false); + this->preferences.putString("source", rule_ptr->source); + this->preferences.putString("destination", rule_ptr->destination); + this->preferences.putUChar("protocol", rule_ptr->protocol); + this->preferences.putUChar("target", rule_ptr->target); + this->preferences.end(); } } - -unsigned char *Storage::get_hash(const char *payload) -{ - unsigned char hashedPayload[32]; - mbedtls_md_context_t ctx; - mbedtls_md_type_t md_type = MBEDTLS_MD_MD5; - const size_t payloadLength = strlen(payload); - - mbedtls_md_init(&ctx); - mbedtls_md_setup(&ctx, mbedtls_md_info_from_type(md_type), 0); - mbedtls_md_starts(&ctx); - mbedtls_md_update(&ctx, (const unsigned char *)payload, payloadLength); - mbedtls_md_finish(&ctx, hashedPayload); - mbedtls_md_free(&ctx); - - return hashedPayload; -} - -uint8_t Storage::get_amount_of_firewall_rules() -{ - return this->amount_of_rules; -} - -void Storage::set_amount_of_firewall_rules(const uint8_t new_amount) -{ - this->amount_of_rules = new_amount; -} -} diff --git a/SourceCode/arduino/lib/Firewall/esp32Storage.hpp b/SourceCode/arduino/lib/Firewall/esp32Storage.hpp index 3a92d1f..aa16cfe 100644 --- a/SourceCode/arduino/lib/Firewall/esp32Storage.hpp +++ b/SourceCode/arduino/lib/Firewall/esp32Storage.hpp @@ -1,7 +1,7 @@ #ifndef ESP32_STORAGE_HPP #define ESP32_STORAGE_HPP -#include "EEPROM.h" +#include "Preferences.h" #include "FirewallTypes.h" #include "mbedtls/md.h" @@ -10,25 +10,16 @@ namespace firewall class Storage { private: - uint16_t eeprom_size; - uint16_t settings_start = 0; - uint16_t settings_head = settings_start; - uint16_t rules_start = 100; - uint16_t rules_head = rules_start; - uint16_t certificate_start = 800; - uint16_t certificate_head = certificate_start; - - void clear_eeprom(); - void get_eeprom_hash(); - unsigned char *Storage::get_hash(const char *); + Preferences preferences; protected: - uint8_t amount_of_rules; - uint8_t get_amount_of_firewall_rules(); - void set_amount_of_firewall_rules(const uint8_t); + uint8_t retrieve_amount_of_firewall_rules(); + void store_amount_of_firewall_rules(const uint8_t); + firewall_rule_t *retrieve_firewall_rule(const uint8_t); + void store_firewall_rule(const uint8_t &, firewall_rule_t *); public: - Storage(const uint16_t = 1000); + Storage(); ~Storage(); }; }