Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add proper mdns support #295

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions include/MDNSAnnouncer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

namespace OpenShock::MDNSAnnouncer {
bool Init();
bool IsEnabled();
void SetEnabled(bool enabled);
} // namespace OpenShock::MDNSAnnouncer
22 changes: 0 additions & 22 deletions src/CaptivePortal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ const char* const TAG = "CaptivePortal";
#include <WebSocketsServer.h>
#include <WiFi.h>

#include <mdns.h>

#include <memory>

using namespace OpenShock;
Expand Down Expand Up @@ -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<CaptivePortalInstance>();

return true;
Expand Down
48 changes: 48 additions & 0 deletions src/MDNSAnnouncer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include "MDNSAnnouncer.h"

#include "config/Config.h"

#include <mdns.h>

#include <string>

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,
});
}
1 change: 0 additions & 1 deletion src/wifi/WiFiManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Loading