From 4548d88ff7210c691e4d3b09044e9f7e8f3d8813 Mon Sep 17 00:00:00 2001 From: hhvrc Date: Thu, 3 Oct 2024 23:36:30 +0200 Subject: [PATCH] Initial wip push --- include/MDNSAnnouncer.h | 7 ++++++ src/CaptivePortal.cpp | 22 ------------------ src/MDNSAnnouncer.cpp | 48 ++++++++++++++++++++++++++++++++++++++++ src/wifi/WiFiManager.cpp | 1 - 4 files changed, 55 insertions(+), 23 deletions(-) create mode 100644 include/MDNSAnnouncer.h create mode 100644 src/MDNSAnnouncer.cpp diff --git a/include/MDNSAnnouncer.h b/include/MDNSAnnouncer.h new file mode 100644 index 00000000..96d310e6 --- /dev/null +++ b/include/MDNSAnnouncer.h @@ -0,0 +1,7 @@ +#pragma once + +namespace OpenShock::MDNSAnnouncer { + bool Init(); + bool IsEnabled(); + void SetEnabled(bool enabled); +} // namespace OpenShock::MDNSAnnouncer diff --git a/src/CaptivePortal.cpp b/src/CaptivePortal.cpp index e314c2b6..81117d6d 100644 --- a/src/CaptivePortal.cpp +++ b/src/CaptivePortal.cpp @@ -15,8 +15,6 @@ const char* const TAG = "CaptivePortal"; #include #include -#include - #include using namespace OpenShock; @@ -51,26 +49,6 @@ bool _startCaptive() { return false; } - esp_err_t err = mdns_init(); - if (err != ESP_OK) { - OS_LOGE(TAG, "Failed to initialize mDNS"); - WiFi.softAPdisconnect(true); - return false; - } - - std::string hostname; - if (!Config::GetWiFiHostname(hostname)) { - OS_LOGE(TAG, "Failed to get WiFi hostname, reverting to default"); - hostname = OPENSHOCK_FW_HOSTNAME; - } - - err = mdns_hostname_set(hostname.c_str()); - if (err != ESP_OK) { - OS_LOGE(TAG, "Failed to set mDNS hostname"); - WiFi.softAPdisconnect(true); - return false; - } - s_instance = std::make_unique(); return true; diff --git a/src/MDNSAnnouncer.cpp b/src/MDNSAnnouncer.cpp new file mode 100644 index 00000000..84be8c0a --- /dev/null +++ b/src/MDNSAnnouncer.cpp @@ -0,0 +1,48 @@ +#include "MDNSAnnouncer.h" + +#include "config/Config.h" + +#include + +#include + +static const char* TAG = "MDNSAnnouncer"; + +using namespace OpenShock; + +bool MDNSAnnouncer::Init() { + esp_err_t err = mdns_init(); + if (err != ESP_OK) { + OS_LOGE(TAG, "Failed to initialize mDNS"); + WiFi.softAPdisconnect(true); + return false; + } + + mdns_instance_name_set("OpenShock"); + + std::string hostname; + if (Config::GetWiFiHostname(hostname)) { + OS_LOGE(TAG, "Failed to get WiFi hostname, reverting to default"); + hostname = OPENSHOCK_FW_HOSTNAME; + } + + err = mdns_hostname_set(hostname.c_str()); + if (err != ESP_OK) { + OS_LOGE(TAG, "Failed to set mDNS hostname"); + WiFi.softAPdisconnect(true); + return false; + } + + return true; +} + +bool MDNSAnnouncer::IsEnabled() { + mdns_service_add("http", "_tcp", 80); + return Config::GetCaptivePortalConfig().mdnsEnabled; +} + +void MDNSAnnouncer::SetEnabled(bool enabled) { + Config::SetCaptivePortalConfig({ + .mdnsEnabled = enabled, + }); +} diff --git a/src/wifi/WiFiManager.cpp b/src/wifi/WiFiManager.cpp index 098df494..96aa06e5 100644 --- a/src/wifi/WiFiManager.cpp +++ b/src/wifi/WiFiManager.cpp @@ -325,7 +325,6 @@ bool WiFiManager::Init() { hostname = OPENSHOCK_FW_HOSTNAME; } - WiFi.setAutoConnect(false); WiFi.setAutoReconnect(false); WiFi.enableSTA(true); WiFi.setHostname(hostname.c_str());