works, needs more work

This commit is contained in:
Florian Hoss 2022-04-10 20:46:54 +02:00
parent f94639c1a0
commit 4d23c1039b
6 changed files with 75 additions and 66 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -1,14 +1,31 @@
#include <Arduino.h>
#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();
}