From 2ad187e529d0757ecda13c2665405562c6656d7e Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Mon, 16 Aug 2021 04:29:57 +0300 Subject: [PATCH] mdns: do not use espurna.h include in the header --- code/espurna/mdns.cpp | 87 +++++++++++++++++++++++-------------------- code/espurna/mdns.h | 6 ++- 2 files changed, 51 insertions(+), 42 deletions(-) diff --git a/code/espurna/mdns.cpp b/code/espurna/mdns.cpp index f7d1d14a..14000e72 100644 --- a/code/espurna/mdns.cpp +++ b/code/espurna/mdns.cpp @@ -10,49 +10,30 @@ Copyright (C) 2017-2019 by Xose Pérez // mDNS Server // ----------------------------------------------------------------------------- +#include "espurna.h" + +#if MDNS_SERVER_SUPPORT + #include "mdns.h" -#include "web.h" #include "telnet.h" #include "utils.h" - -#if MDNS_SERVER_SUPPORT +#include "web.h" #include // ----------------------------------------------------------------------------- -// As of right now, this needs to be request -> response operation in the same block, -// so we don't end up using someone else's query results. -// `queryService()` 3rd arg is timeout, by default it blocks for MDNS_QUERYSERVICES_WAIT_TIME (1000) - -// TODO: esp8266 allows async pyzeroconf-like API to have this running independently. -// In case something other than MQTT needs this, consider extending the API -// (and notice that RTOS SDK alternative would need to use mdns_query_* ESP-IDF API) -// TODO: both implementations also have separate queries for A and AAAA records :/ - -bool mdnsServiceQuery(const String& service, const String& protocol, MdnsServerQueryCallback callback) { - bool result { false }; - - auto found = MDNS.queryService(service, protocol); - for (decltype(found) n = 0; n < found; ++n) { - if (callback(MDNS.IP(n).toString(), MDNS.port(n))) { - result = true; - break; - } - } - - MDNS.removeQuery(); - - return result; -} +namespace mdns { +namespace { +namespace settings { -bool mdnsRunning() { - return MDNS.isRunning(); +String hostname() { + return getSetting("hostname", getIdentifier()); } -namespace { +} // namespace settings -void _mdnsRegisterServices() { +void addServices() { #if WEB_SUPPORT MDNS.addService("http", "tcp", webPort()); #endif @@ -79,15 +60,11 @@ void _mdnsRegisterServices() { #endif } -String _mdnsHostname() { - return getSetting("hostname", getIdentifier()); -} - -void _mdnsServerStart() { - auto hostname = _mdnsHostname(); +void start() { + auto hostname = settings::hostname(); if (MDNS.begin(hostname)) { DEBUG_MSG_P(PSTR("[MDNS] Started with hostname %s\n"), hostname.c_str()); - _mdnsRegisterServices(); + addServices(); espurnaRegisterLoop([]() { MDNS.update(); }); @@ -98,6 +75,36 @@ void _mdnsServerStart() { } } // namespace +} // namespace mdsn + +// As of right now, this needs to be request -> response operation in the same block, +// so we don't end up using someone else's query results. +// `queryService()` 3rd arg is timeout, by default it blocks for MDNS_QUERYSERVICES_WAIT_TIME (1000) + +// TODO: esp8266 allows async pyzeroconf-like API to have this running independently. +// In case something other than MQTT needs this, consider extending the API +// (and notice that RTOS SDK alternative would need to use mdns_query_* ESP-IDF API) +// TODO: both implementations also have separate queries for A and AAAA records :/ + +bool mdnsServiceQuery(const String& service, const String& protocol, MdnsServerQueryCallback callback) { + bool result { false }; + + auto found = MDNS.queryService(service, protocol); + for (decltype(found) n = 0; n < found; ++n) { + if (callback(MDNS.IP(n).toString(), MDNS.port(n))) { + result = true; + break; + } + } + + MDNS.removeQuery(); + + return result; +} + +bool mdnsRunning() { + return MDNS.isRunning(); +} void mdnsServerSetup() { // 2.7.x and older require MDNS.begin() when interface is UP @@ -114,11 +121,11 @@ void mdnsServerSetup() { wifiRegister([](wifi::Event event) { if ((event == wifi::Event::StationConnected) && !MDNS.isRunning()) { - _mdnsServerStart(); + mdns::start(); } }); #else - _mdnsServerStart(); + mdns::start(); #endif } diff --git a/code/espurna/mdns.h b/code/espurna/mdns.h index e7f02eb5..8c82d380 100644 --- a/code/espurna/mdns.h +++ b/code/espurna/mdns.h @@ -8,10 +8,12 @@ Copyright (C) 2017-2019 by Xose Pérez #pragma once -#include "espurna.h" +#include + +#include using MdnsServerQueryCallback = bool(*)(String&& server, uint16_t port); +bool mdnsServiceQuery(const String& service, const String& protocol, MdnsServerQueryCallback callback); bool mdnsRunning(); -bool mdnsServiceQuery(const String& service, const String& protocol, MdnsServerQueryCallback callback); void mdnsServerSetup();