🖥️ Adds mDNS service

This commit is contained in:
Rune Harlyk
2025-03-23 19:45:55 +01:00
committed by Rune Harlyk
parent c346f7f553
commit 3671610860
10 changed files with 530 additions and 13 deletions
+1
View File
@@ -12,6 +12,7 @@
#define DEVICE_CONFIG_FILE "/config/peripheral.json"
#define WIFI_SETTINGS_FILE "/config/wifiSettings.json"
#define SERVO_SETTINGS_FILE "/config/servoSettings.json"
#define MDNS_SETTINGS_FILE "/config/mdnsSettings.json"
namespace FileSystem {
extern PsychicUploadHandler *uploadHandler;
+101
View File
@@ -0,0 +1,101 @@
#include <mdns_service.h>
static const char *TAG = "MDNSService";
MDNSService::MDNSService()
: endpoint(MDNSSettings::read, MDNSSettings::update, this),
_persistence(MDNSSettings::read, MDNSSettings::update, this, MDNS_SETTINGS_FILE),
_started(false) {
addUpdateHandler([&](const String &originId) { reconfigureMDNS(); }, false);
}
MDNSService::~MDNSService() {
if (_started) {
stopMDNS();
}
}
void MDNSService::begin() {
_persistence.readFromFS();
startMDNS();
}
void MDNSService::reconfigureMDNS() {
if (_started) {
stopMDNS();
}
startMDNS();
}
void MDNSService::startMDNS() {
ESP_LOGV(TAG, "Starting MDNS with hostname: %s", state().hostname.c_str());
if (MDNS.begin(state().hostname.c_str())) {
_started = true;
MDNS.setInstanceName(state().instance.c_str());
addServices();
ESP_LOGI(TAG, "MDNS started successfully with hostname: %s", state().hostname.c_str());
} else {
_started = false;
ESP_LOGE(TAG, "Failed to start MDNS");
}
}
void MDNSService::stopMDNS() {
ESP_LOGV(TAG, "Stopping MDNS");
MDNS.end();
_started = false;
}
void MDNSService::addServices() {
for (const auto &service : state().services) {
MDNS.addService(service.service.c_str(), service.protocol.c_str(), service.port);
for (const auto &txt : service.txtRecords) {
MDNS.addServiceTxt(service.service.c_str(), service.protocol.c_str(), txt.key.c_str(), txt.value.c_str());
}
}
for (const auto &txt : state().globalTxtRecords) {
for (const auto &service : state().services) {
MDNS.addServiceTxt(service.service.c_str(), service.protocol.c_str(), txt.key.c_str(), txt.value.c_str());
}
}
}
esp_err_t MDNSService::getStatus(PsychicRequest *request) {
PsychicJsonResponse response = PsychicJsonResponse(request, false);
JsonObject root = response.getRoot();
getStatus(root);
return response.send();
}
void MDNSService::getStatus(JsonObject &root) {
state().read(state(), root);
root["started"] = _started;
}
esp_err_t MDNSService::queryServices(PsychicRequest *request, JsonVariant &json) {
String service = json["service"].as<String>();
String proto = json["protocol"].as<String>();
PsychicJsonResponse response = PsychicJsonResponse(request, false);
JsonObject root = response.getRoot();
ESP_LOGI(TAG, "Querying for service: %s, protocol: %s", service.c_str(), proto.c_str());
int n = MDNS.queryService(service.c_str(), proto.c_str());
ESP_LOGI(TAG, "Found %d services", n);
JsonArray servicesArray = root["services"].to<JsonArray>();
for (int i = 0; i < n; i++) {
JsonObject serviceObj = servicesArray.add<JsonObject>();
serviceObj["name"] = MDNS.hostname(i);
serviceObj["ip"] = MDNS.IP(i);
serviceObj["port"] = MDNS.port(i);
}
return response.send();
}
+33
View File
@@ -0,0 +1,33 @@
#pragma once
#include <PsychicHttp.h>
#include <ESPmDNS.h>
#include <template/stateful_service.h>
#include <template/stateful_endpoint.h>
#include <template/stateful_persistence.h>
#include <settings/mdns_settings.h>
#include <utils/timing.h>
class MDNSService : public StatefulService<MDNSSettings> {
private:
FSPersistence<MDNSSettings> _persistence;
bool _started;
void reconfigureMDNS();
void startMDNS();
void stopMDNS();
void addServices();
public:
MDNSService();
~MDNSService();
void begin();
esp_err_t getStatus(PsychicRequest *request);
void getStatus(JsonObject &root);
static esp_err_t queryServices(PsychicRequest *request, JsonVariant &json);
StatefulHttpEndpoint<MDNSSettings> endpoint;
};
@@ -0,0 +1,139 @@
#pragma once
#include <ArduinoJson.h>
#include <utils/json_utils.h>
#include <utils/string_utils.h>
#include <template/state_result.h>
#include <filesystem.h>
#ifndef FACTORY_MDNS_HOSTNAME
#define FACTORY_MDNS_HOSTNAME "esp32"
#endif
#ifndef FACTORY_MDNS_INSTANCE
#define FACTORY_MDNS_INSTANCE "ESP32 Device"
#endif
typedef struct {
String key;
String value;
void serialize(JsonObject &json) const {
json["key"] = key;
json["value"] = value;
}
bool deserialize(const JsonObject &json) {
key = json["key"].as<String>();
value = json["value"].as<String>();
return key.length() > 0;
}
} mdns_txt_record_t;
typedef struct {
String service;
String protocol;
uint16_t port;
std::vector<mdns_txt_record_t> txtRecords;
void serialize(JsonObject &json) const {
json["service"] = service;
json["protocol"] = protocol;
json["port"] = port;
if (txtRecords.size() > 0) {
JsonArray txtArray = json["txt_records"].to<JsonArray>();
for (const auto &txt : txtRecords) {
JsonObject txtObj = txtArray.add<JsonObject>();
txt.serialize(txtObj);
}
}
}
bool deserialize(const JsonObject &json) {
service = json["service"].as<String>();
protocol = json["protocol"].as<String>();
port = json["port"] | 0;
txtRecords.clear();
if (json["txt_records"].is<JsonArray>()) {
JsonArray txtArray = json["txt_records"];
for (JsonObject txtObj : txtArray) {
mdns_txt_record_t txt;
if (txt.deserialize(txtObj)) {
txtRecords.push_back(txt);
}
}
}
return service.length() > 0 && protocol.length() > 0 && port > 0;
}
} mdns_service_t;
class MDNSSettings {
public:
String hostname;
String instance;
std::vector<mdns_service_t> services;
std::vector<mdns_txt_record_t> globalTxtRecords;
static void read(MDNSSettings &settings, JsonObject &root) {
root["hostname"] = settings.hostname;
root["instance"] = settings.instance;
JsonArray servicesArray = root["services"].to<JsonArray>();
for (const auto &service : settings.services) {
JsonObject serviceObj = servicesArray.add<JsonObject>();
service.serialize(serviceObj);
}
JsonArray txtArray = root["global_txt_records"].to<JsonArray>();
for (const auto &txt : settings.globalTxtRecords) {
JsonObject txtObj = txtArray.add<JsonObject>();
txt.serialize(txtObj);
}
}
static StateUpdateResult update(JsonObject &root, MDNSSettings &settings) {
settings.hostname = root["hostname"] | FACTORY_MDNS_HOSTNAME;
settings.instance = root["instance"] | FACTORY_MDNS_INSTANCE;
settings.services.clear();
if (root["services"].is<JsonArray>()) {
JsonArray servicesArray = root["services"];
for (JsonObject serviceObj : servicesArray) {
mdns_service_t service;
if (service.deserialize(serviceObj)) {
settings.services.push_back(service);
}
}
}
if (settings.services.empty()) {
mdns_service_t httpService = {.service = "http", .protocol = "tcp", .port = 80};
settings.services.push_back(httpService);
mdns_service_t wsService = {.service = "ws", .protocol = "tcp", .port = 80};
settings.services.push_back(wsService);
}
settings.globalTxtRecords.clear();
if (root["global_txt_records"].is<JsonArray>()) {
JsonArray txtArray = root["global_txt_records"];
for (JsonObject txtObj : txtArray) {
mdns_txt_record_t txt;
if (txt.deserialize(txtObj)) {
settings.globalTxtRecords.push_back(txt);
}
}
}
if (settings.globalTxtRecords.empty()) {
mdns_txt_record_t firmwareVersion = {.key = "Firmware Version", .value = APP_VERSION};
settings.globalTxtRecords.push_back(firmwareVersion);
}
return StateUpdateResult::CHANGED;
}
};
+11 -11
View File
@@ -26,8 +26,6 @@ void Spot::initialize() {
startServices();
setupMDNS();
ESP_LOGV(TAG, "Starting misc loop task");
g_taskManager.createTask(this->_loopImpl, "Spot misc", 4096, this, 2, NULL, APPLICATION_CORE);
}
@@ -108,6 +106,16 @@ void Spot::setupServer() {
});
#endif
// MDNS
_server.on("/api/mdns/status", HTTP_GET,
[this](PsychicRequest *request) { return _mdnsService.getStatus(request); });
_server.on("/api/mdns/settings", HTTP_GET,
[this](PsychicRequest *request) { return _mdnsService.endpoint.getState(request); });
_server.on("/api/mdns/settings", HTTP_POST, [this](PsychicRequest *request, JsonVariant &json) {
return _mdnsService.endpoint.handleStateUpdate(request, json);
});
_server.on("/api/mdns/query", HTTP_POST, MDNSService::queryServices);
#ifdef EMBED_WWW
ESP_LOGV(TAG, "Registering routes from PROGMEM static resources");
WWWData::registerRoutes([&](const String &uri, const String &contentType, const uint8_t *content, size_t len) {
@@ -158,15 +166,6 @@ void Spot::setupServer() {
DefaultHeaders::Instance().addHeader("Server", _appName);
}
void Spot::setupMDNS() {
ESP_LOGV(TAG, "Starting MDNS");
MDNS.begin(_wifiService.getHostname());
MDNS.setInstanceName(_appName);
MDNS.addService("http", "tcp", _port);
MDNS.addService("ws", "tcp", _port);
MDNS.addServiceTxt("http", "tcp", "Firmware Version", APP_VERSION);
}
void Spot::startServices() {
_apService.begin();
#if FT_ENABLED(USE_UPLOAD_FIRMWARE)
@@ -180,6 +179,7 @@ void Spot::startServices() {
#if FT_ENABLED(USE_CAMERA)
_cameraService.begin();
#endif
_mdnsService.begin();
}
void IRAM_ATTR Spot::loop() {