Browse Source

mdns: do not use espurna.h include in the header

pull/2471/head
Maxim Prokhorov 2 years ago
parent
commit
2ad187e529
2 changed files with 51 additions and 42 deletions
  1. +47
    -40
      code/espurna/mdns.cpp
  2. +4
    -2
      code/espurna/mdns.h

+ 47
- 40
code/espurna/mdns.cpp View File

@ -10,49 +10,30 @@ Copyright (C) 2017-2019 by Xose Pérez <xose dot perez at gmail dot com>
// 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 <ESP8266mDNS.h>
// -----------------------------------------------------------------------------
// 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
}


+ 4
- 2
code/espurna/mdns.h View File

@ -8,10 +8,12 @@ Copyright (C) 2017-2019 by Xose Pérez <xose dot perez at gmail dot com>
#pragma once
#include "espurna.h"
#include <Arduino.h>
#include <cstdint>
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();

Loading…
Cancel
Save