From 49415c41732e076910b425acc0df3a7ebc780612 Mon Sep 17 00:00:00 2001 From: Florian Hoss Date: Tue, 19 Jul 2022 13:07:31 +0200 Subject: [PATCH] Register callback for wifi --- ESPFirewall/src/main.cpp | 51 +++++++++++++++++++++++++++++++--------- 1 file changed, 40 insertions(+), 11 deletions(-) diff --git a/ESPFirewall/src/main.cpp b/ESPFirewall/src/main.cpp index 2e5e9c3..f66e1a7 100644 --- a/ESPFirewall/src/main.cpp +++ b/ESPFirewall/src/main.cpp @@ -15,28 +15,52 @@ fw::API *firewallApi; WiFiServer wifiServer(80); -void setup_wifi() +void connectToWifi() { - Serial.begin(115200); - WiFi.mode(WIFI_STA); WiFi.begin(ssid, psk); - Serial.printf("Attempting to connect to WPA SSID: %s\n", ssid); while (WiFi.status() != WL_CONNECTED) { - Serial.print('.'); - delay(1000); + delay(200); } - Serial.println(); - Serial.print("IP Address: "); - Serial.println(WiFi.localIP()); - wifiServer.begin(); +} + +void handle_wifi_events(WiFiEvent_t event, WiFiEventInfo_t info) +{ + Serial.print("[WiFi] "); + switch (event) + { + case ARDUINO_EVENT_WIFI_STA_START: + Serial.printf("Attempting to connect to WPA SSID: %s\n", ssid); + break; + case ARDUINO_EVENT_WIFI_STA_CONNECTED: + Serial.println("connected"); + break; + case ARDUINO_EVENT_WIFI_STA_DISCONNECTED: + Serial.println("disconnected"); + break; + case ARDUINO_EVENT_WIFI_STA_GOT_IP: + Serial.print("IP address: "); + Serial.println(WiFi.localIP()); + break; + default: + Serial.println(event); + } +} + +void setup_wifi() +{ + WiFi.mode(WIFI_STA); + WiFi.onEvent(handle_wifi_events, ARDUINO_EVENT_MAX); + connectToWifi(); } void setup() { + Serial.begin(115200); setup_wifi(); firewall = new fw::Firewall(); firewallApi = new fw::API(firewall, cert, key, username, password); + wifiServer.begin(); } void loop() @@ -46,9 +70,14 @@ void loop() if (client) { if (firewall->is_client_allowed(client)) + { Serial.println("allowed"); + client.stop(); + } else + { Serial.println("rejected"); - client.stop(); + client.flush(); + } } } \ No newline at end of file