diff --git a/SourceCode/arduino/include/theServer.h b/SourceCode/arduino/include/theServer.h deleted file mode 100644 index adef3ee..0000000 --- a/SourceCode/arduino/include/theServer.h +++ /dev/null @@ -1,39 +0,0 @@ -#ifndef THESERVER_H -#define THESERVER_H - -#include "WebServer.h" -#include "cJSON.h" - -static WebServer server(8080); - -void custom_message_response(const char *message, int response_code) -{ - cJSON *json_response = cJSON_CreateObject(); - cJSON_AddBoolToObject(json_response, "ok", true); - cJSON_AddStringToObject(json_response, "message", message); - server.send(response_code, "application/json", cJSON_Print(json_response)); - cJSON_Delete(json_response); -} - -static void getFirewallRules() -{ - custom_message_response("firewall rules..", 200); -} - -static void setup_routing() -{ - server.on("/api/v1/firewall", HTTP_GET, getFirewallRules); -} - -void setup_server() -{ - setup_routing(); - server.begin(); -} - -void handle_server_clients() -{ - server.handleClient(); -} - -#endif diff --git a/SourceCode/arduino/include/theWifi.h b/SourceCode/arduino/include/theWifi.h deleted file mode 100644 index 0f4c2b4..0000000 --- a/SourceCode/arduino/include/theWifi.h +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef THEWIFI_H -#define THEWIFI_H - -#include "esp32-hal-log.h" -#include "WiFi.h" -#include "theSecrets.h" - -const char *esp_ip_address; - -void setup_wifi() -{ - log_i("Attempting to connect to WPA SSID: %s", ssid); - WiFi.mode(WIFI_STA); - WiFi.begin(ssid, psk); - while (WiFi.status() != WL_CONNECTED) - { - delay(1000); - } - esp_ip_address = WiFi.localIP().toString().c_str(); - log_i("Connected, IP Address: %s", esp_ip_address); -} - -#endif diff --git a/SourceCode/arduino/lib/Firewall/Firewall.cpp b/SourceCode/arduino/lib/Firewall/Firewall.cpp new file mode 100644 index 0000000..87aabc6 --- /dev/null +++ b/SourceCode/arduino/lib/Firewall/Firewall.cpp @@ -0,0 +1,27 @@ +#include "Firewall.h" + +ESPFirewall::ESPFirewall() +{ + log_i("Firewall!"); + firewall_api->on("/api/v1/firewall", HTTP_GET, std::bind(&ESPFirewall::get_firewall_rules, this)); + firewall_api->begin(); +} + +void ESPFirewall::get_firewall_rules() +{ + this->custom_message_response("Firewall", 200); +} + +void ESPFirewall::custom_message_response(const char *message, int response_code) +{ + cJSON *json_response = cJSON_CreateObject(); + cJSON_AddBoolToObject(json_response, "ok", true); + cJSON_AddStringToObject(json_response, "message", message); + firewall_api->send(response_code, "application/json", cJSON_Print(json_response)); + cJSON_Delete(json_response); +} + +void ESPFirewall::handle_clients() +{ + this->firewall_api->handleClient(); +} \ No newline at end of file diff --git a/SourceCode/arduino/lib/Firewall/Firewall.h b/SourceCode/arduino/lib/Firewall/Firewall.h new file mode 100644 index 0000000..d36dfd5 --- /dev/null +++ b/SourceCode/arduino/lib/Firewall/Firewall.h @@ -0,0 +1,20 @@ +#ifndef FIREWALL_H +#define FIREWALL_H + +#include "WebServer.h" +#include "cJSON.h" +#include "esp32-hal-log.h" + +class ESPFirewall +{ + WebServer *firewall_api = new WebServer(8080); + void get_firewall_rules(); + void setup_routing(); + void custom_message_response(const char *message, int response_code); + +public: + ESPFirewall(); + void handle_clients(); +}; + +#endif diff --git a/SourceCode/arduino/platformio.ini b/SourceCode/arduino/platformio.ini index 31365bd..534eaca 100644 --- a/SourceCode/arduino/platformio.ini +++ b/SourceCode/arduino/platformio.ini @@ -14,3 +14,10 @@ board = esp32-evb framework = arduino monitor_speed = 115200 build_flags = -DCORE_DEBUG_LEVEL=3 + +[env:esp32-dev] +platform = espressif32 +board = az-delivery-devkit-v4 +framework = arduino +monitor_speed = 115200 +build_flags = -DCORE_DEBUG_LEVEL=3 diff --git a/SourceCode/arduino/src/main.cpp b/SourceCode/arduino/src/main.cpp index 348aabb..78df614 100644 --- a/SourceCode/arduino/src/main.cpp +++ b/SourceCode/arduino/src/main.cpp @@ -1,14 +1,31 @@ #include -#include "theWifi.h" -#include "theServer.h" +#include "theSecrets.h" +#include "Firewall.h" +#include "esp32-hal-log.h" + +const char *esp_ip_address; +ESPFirewall *firewall; + +void setup_wifi() +{ + log_i("Attempting to connect to WPA SSID: %s", ssid); + WiFi.mode(WIFI_STA); + WiFi.begin(ssid, psk); + while (WiFi.status() != WL_CONNECTED) + { + delay(1000); + } + esp_ip_address = WiFi.localIP().toString().c_str(); + log_i("Connected, IP Address: %s", esp_ip_address); +} void setup() { setup_wifi(); - setup_server(); + firewall = new ESPFirewall; } void loop() { - handle_server_clients(); + firewall->handle_clients(); } \ No newline at end of file