diff --git a/SourceCode/arduino/lib/Firewall/Firewall.cpp b/SourceCode/arduino/lib/Firewall/esp32Firewall.cpp similarity index 80% rename from SourceCode/arduino/lib/Firewall/Firewall.cpp rename to SourceCode/arduino/lib/Firewall/esp32Firewall.cpp index d397f8c..e860cd7 100644 --- a/SourceCode/arduino/lib/Firewall/Firewall.cpp +++ b/SourceCode/arduino/lib/Firewall/esp32Firewall.cpp @@ -1,6 +1,6 @@ -#include "Firewall.h" +#include "esp32Firewall.hpp" -ESPFirewall::ESPFirewall(const uint16_t api_port) +esp32Firewall::esp32Firewall(const uint16_t api_port) { this->setup_eeprom(); this->setup_certificate(); @@ -14,12 +14,12 @@ ESPFirewall::ESPFirewall(const uint16_t api_port) } } -void ESPFirewall::handle_firewall_api_clients() +void esp32Firewall::handle_firewall_api_clients() { this->firewall_api->loop(); } -String ESPFirewall::protocol_to_string(firewall_protocol_t &protocol) +String esp32Firewall::protocol_to_string(firewall_protocol_t &protocol) { switch (protocol) { @@ -32,7 +32,7 @@ String ESPFirewall::protocol_to_string(firewall_protocol_t &protocol) } } -firewall_protocol_t ESPFirewall::string_to_protocol(std::string &protocol) +firewall_protocol_t esp32Firewall::string_to_protocol(std::string &protocol) { if (protocol.compare("TCP") == 0) return FW_TCP; @@ -42,7 +42,7 @@ firewall_protocol_t ESPFirewall::string_to_protocol(std::string &protocol) return FW_ALL; } -String ESPFirewall::target_to_string(firewall_target_t &target) +String esp32Firewall::target_to_string(firewall_target_t &target) { switch (target) { @@ -55,7 +55,7 @@ String ESPFirewall::target_to_string(firewall_target_t &target) } } -firewall_target_t ESPFirewall::string_to_target(std::string &target) +firewall_target_t esp32Firewall::string_to_target(std::string &target) { if (target.compare("REJECT") == 0) return FW_REJECT; @@ -65,7 +65,7 @@ firewall_target_t ESPFirewall::string_to_target(std::string &target) return FW_ACCEPT; } -void ESPFirewall::setup_eeprom() +void esp32Firewall::setup_eeprom() { EEPROM.begin(this->eeprom_size); this->amount_of_rules = EEPROM.read(this->eeprom_settings_head); @@ -81,7 +81,7 @@ void ESPFirewall::setup_eeprom() this->eeprom_read_firewall_rules(); } -void ESPFirewall::eeprom_write_firewall_rule(firewall_rule_t *rule_ptr) +void esp32Firewall::eeprom_write_firewall_rule(firewall_rule_t *rule_ptr) { EEPROM.write(this->eeprom_settings_head, this->amount_of_rules); EEPROM.writeString(this->eeprom_rules_head, rule_ptr->source); @@ -95,7 +95,7 @@ void ESPFirewall::eeprom_write_firewall_rule(firewall_rule_t *rule_ptr) EEPROM.commit(); } -void ESPFirewall::eeprom_write_firewall_rules() +void esp32Firewall::eeprom_write_firewall_rules() { this->eeprom_rules_head = eeprom_start_firewall_rules; firewall_rule_t *rule_ptr = this->head; @@ -106,7 +106,7 @@ void ESPFirewall::eeprom_write_firewall_rules() } } -void ESPFirewall::eeprom_read_firewall_rule(uint8_t &eeprom_address, uint8_t &rule_nr) +void esp32Firewall::eeprom_read_firewall_rule(uint8_t &eeprom_address, uint8_t &rule_nr) { firewall_rule_t *rule_ptr = (firewall_rule_t *)malloc(sizeof(firewall_rule_t)); rule_ptr->key = rule_nr; @@ -126,7 +126,7 @@ void ESPFirewall::eeprom_read_firewall_rule(uint8_t &eeprom_address, uint8_t &ru target_to_string(rule_ptr->target)); } -void ESPFirewall::eeprom_read_firewall_rules() +void esp32Firewall::eeprom_read_firewall_rules() { uint8_t eeprom_address = eeprom_start_firewall_rules; for (uint8_t i = 1; i <= this->amount_of_rules; i++) @@ -135,7 +135,7 @@ void ESPFirewall::eeprom_read_firewall_rules() } } -void ESPFirewall::add_rule_to_firewall(firewall_rule_t *rule_ptr) +void esp32Firewall::add_rule_to_firewall(firewall_rule_t *rule_ptr) { firewall_rule_t *temp; if (this->head == NULL) @@ -154,7 +154,7 @@ void ESPFirewall::add_rule_to_firewall(firewall_rule_t *rule_ptr) return; } -firewall_rule_t *ESPFirewall::get_rule_from_firewall(uint8_t key) +firewall_rule_t *esp32Firewall::get_rule_from_firewall(uint8_t key) { firewall_rule_t *rule_ptr = this->head; if (this->head == NULL) @@ -175,7 +175,7 @@ firewall_rule_t *ESPFirewall::get_rule_from_firewall(uint8_t key) return rule_ptr; } -bool ESPFirewall::delete_rule_from_firewall(uint8_t key) +bool esp32Firewall::delete_rule_from_firewall(uint8_t key) { if (this->head == NULL) { @@ -217,7 +217,7 @@ bool ESPFirewall::delete_rule_from_firewall(uint8_t key) return true; } -void ESPFirewall::setup_certificate() +void esp32Firewall::setup_certificate() { log_i("Creating the certificate..."); this->certificate = new SSLCert(); @@ -236,14 +236,14 @@ void ESPFirewall::setup_certificate() log_i("Creating the certificate was successful"); } -void ESPFirewall::setup_routing() +void esp32Firewall::setup_routing() { - ResourceNode *get_firewall_rule = new ResourceNode("/api/v1/firewall/*", "GET", std::bind(&ESPFirewall::get_firewall_rule_handler, this, std::placeholders::_1, std::placeholders::_2)); - ResourceNode *get_firewall_rules = new ResourceNode("/api/v1/firewall", "GET", std::bind(&ESPFirewall::get_firewall_rules_handler, this, std::placeholders::_1, std::placeholders::_2)); - ResourceNode *post_firewall = new ResourceNode("/api/v1/firewall", "POST", std::bind(&ESPFirewall::post_firewall_handler, this, std::placeholders::_1, std::placeholders::_2)); - ResourceNode *delete_firewall = new ResourceNode("/api/v1/firewall/*", "DELETE", std::bind(&ESPFirewall::delete_firewall_handler, this, std::placeholders::_1, std::placeholders::_2)); - ResourceNode *restart_device = new ResourceNode("/api/v1/device/restart", "GET", std::bind(&ESPFirewall::restart_device_handler, this, std::placeholders::_1, std::placeholders::_2)); - ResourceNode *not_found = new ResourceNode("", "GET", std::bind(&ESPFirewall::not_found_handler, this, std::placeholders::_1, std::placeholders::_2)); + ResourceNode *get_firewall_rule = new ResourceNode("/api/v1/firewall/*", "GET", std::bind(&esp32Firewall::get_firewall_rule_handler, this, std::placeholders::_1, std::placeholders::_2)); + ResourceNode *get_firewall_rules = new ResourceNode("/api/v1/firewall", "GET", std::bind(&esp32Firewall::get_firewall_rules_handler, this, std::placeholders::_1, std::placeholders::_2)); + ResourceNode *post_firewall = new ResourceNode("/api/v1/firewall", "POST", std::bind(&esp32Firewall::post_firewall_handler, this, std::placeholders::_1, std::placeholders::_2)); + ResourceNode *delete_firewall = new ResourceNode("/api/v1/firewall/*", "DELETE", std::bind(&esp32Firewall::delete_firewall_handler, this, std::placeholders::_1, std::placeholders::_2)); + ResourceNode *restart_device = new ResourceNode("/api/v1/device/restart", "GET", std::bind(&esp32Firewall::restart_device_handler, this, std::placeholders::_1, std::placeholders::_2)); + ResourceNode *not_found = new ResourceNode("", "GET", std::bind(&esp32Firewall::not_found_handler, this, std::placeholders::_1, std::placeholders::_2)); this->firewall_api->registerNode(get_firewall_rule); this->firewall_api->registerNode(get_firewall_rules); this->firewall_api->registerNode(post_firewall); @@ -252,14 +252,14 @@ void ESPFirewall::setup_routing() this->firewall_api->setDefaultNode(not_found); } -void ESPFirewall::json_generic_response(HTTPResponse *response, String serialized, int response_code) +void esp32Firewall::json_generic_response(HTTPResponse *response, String serialized, int response_code) { response->setHeader("Content-Type", "application/json"); response->setStatusCode(response_code); response->println(serialized); } -void ESPFirewall::json_message_response(HTTPResponse *response, String message, int response_code) +void esp32Firewall::json_message_response(HTTPResponse *response, String message, int response_code) { response->setHeader("Content-Type", "application/json"); response->setStatusCode(response_code); @@ -270,7 +270,7 @@ void ESPFirewall::json_message_response(HTTPResponse *response, String message, response->println(serialized); } -String ESPFirewall::construct_json_firewall_rule(firewall_rule_t *rule_ptr) +String esp32Firewall::construct_json_firewall_rule(firewall_rule_t *rule_ptr) { StaticJsonDocument<256> doc; doc["key"] = rule_ptr->key; @@ -283,7 +283,7 @@ String ESPFirewall::construct_json_firewall_rule(firewall_rule_t *rule_ptr) return response; } -String ESPFirewall::construct_json_firewall() +String esp32Firewall::construct_json_firewall() { firewall_rule_t *rule_ptr = this->head; // Size for approx. 12 Rules @@ -305,19 +305,19 @@ String ESPFirewall::construct_json_firewall() return response; } -void ESPFirewall::not_found_handler(HTTPRequest *request, HTTPResponse *response) +void esp32Firewall::not_found_handler(HTTPRequest *request, HTTPResponse *response) { this->json_message_response(response, "not found", 404); } -void ESPFirewall::restart_device_handler(HTTPRequest *request, HTTPResponse *response) +void esp32Firewall::restart_device_handler(HTTPRequest *request, HTTPResponse *response) { this->json_message_response(response, "restarting device in 2 sec", 200); sleep(2000); esp_restart(); } -void ESPFirewall::get_firewall_rule_handler(HTTPRequest *request, HTTPResponse *response) +void esp32Firewall::get_firewall_rule_handler(HTTPRequest *request, HTTPResponse *response) { ResourceParameters *params = request->getParams(); int rule_number = atoi(params->getPathParameter(0).c_str()); @@ -334,17 +334,17 @@ void ESPFirewall::get_firewall_rule_handler(HTTPRequest *request, HTTPResponse * } } -void ESPFirewall::get_firewall_rules_handler(HTTPRequest *request, HTTPResponse *response) +void esp32Firewall::get_firewall_rules_handler(HTTPRequest *request, HTTPResponse *response) { this->json_generic_response(response, this->construct_json_firewall(), 200); } -bool ESPFirewall::request_has_firewall_parameter(ResourceParameters *params) +bool esp32Firewall::request_has_firewall_parameter(ResourceParameters *params) { return params->isQueryParameterSet("source") || params->isQueryParameterSet("destination") || params->isQueryParameterSet("protocol") || params->isQueryParameterSet("target"); } -void ESPFirewall::post_firewall_handler(HTTPRequest *request, HTTPResponse *response) +void esp32Firewall::post_firewall_handler(HTTPRequest *request, HTTPResponse *response) { ResourceParameters *params = request->getParams(); if (request_has_firewall_parameter(params)) @@ -377,7 +377,7 @@ void ESPFirewall::post_firewall_handler(HTTPRequest *request, HTTPResponse *resp } } -void ESPFirewall::delete_firewall_handler(HTTPRequest *request, HTTPResponse *response) +void esp32Firewall::delete_firewall_handler(HTTPRequest *request, HTTPResponse *response) { ResourceParameters *params = request->getParams(); int rule_number = atoi(params->getPathParameter(0).c_str()); diff --git a/SourceCode/arduino/lib/Firewall/Firewall.h b/SourceCode/arduino/lib/Firewall/esp32Firewall.hpp similarity index 94% rename from SourceCode/arduino/lib/Firewall/Firewall.h rename to SourceCode/arduino/lib/Firewall/esp32Firewall.hpp index 0125967..39b085d 100644 --- a/SourceCode/arduino/lib/Firewall/Firewall.h +++ b/SourceCode/arduino/lib/Firewall/esp32Firewall.hpp @@ -1,5 +1,5 @@ -#ifndef FIREWALL_H -#define FIREWALL_H +#ifndef ESP32_FIREWALL_HPP +#define ESP32_FIREWALL_HPP #include "Arduino.h" #include "ArduinoJson.h" @@ -15,7 +15,7 @@ #define eeprom_start_firewall_rules 4 using namespace httpsserver; -class ESPFirewall +class esp32Firewall { uint16_t eeprom_size = 512; uint8_t amount_of_rules = 0; @@ -61,7 +61,7 @@ class ESPFirewall void delete_firewall_handler(HTTPRequest *, HTTPResponse *); public: - ESPFirewall(const uint16_t = 8080); + esp32Firewall(const uint16_t = 8080); void handle_firewall_api_clients(); }; diff --git a/SourceCode/arduino/src/main.cpp b/SourceCode/arduino/src/main.cpp index af28b3f..014c894 100644 --- a/SourceCode/arduino/src/main.cpp +++ b/SourceCode/arduino/src/main.cpp @@ -1,11 +1,9 @@ -#include "Arduino.h" #include "theSecrets.h" #include "WiFi.h" -#include "Firewall.h" -#include "esp32-hal-log.h" +#include "esp32Firewall.hpp" const char *esp_ip_address; -ESPFirewall *firewall; +esp32Firewall *firewall; void setup_wifi() { @@ -26,7 +24,7 @@ void setup_wifi() void setup() { setup_wifi(); - firewall = new ESPFirewall; + firewall = new esp32Firewall; } void loop()