Skip to content

Commit 4d15590

Browse files
authored
formalization of LEA's mdns rewrite (#5450)
* formalization of LEA's mdns rewrite (code), minor changes to polledTimeout * fix typo * Fix mdns examples
1 parent e4c6030 commit 4d15590

15 files changed

+588
-560
lines changed

cores/esp8266/PolledTimeout.h

+10-4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2424
*/
2525

26-
26+
#include <Arduino.h>
2727

2828
namespace esp8266
2929
{
@@ -71,7 +71,7 @@ class timeoutTemplate
7171
return expired();
7272
}
7373

74-
void reset(timeType newTimeout)
74+
void reset(const timeType newTimeout)
7575
{
7676
_timeout = newTimeout;
7777
reset();
@@ -81,12 +81,18 @@ class timeoutTemplate
8181
{
8282
_start = millis();
8383
}
84+
85+
timeType getTimeout() const
86+
{
87+
return _timeout;
88+
}
8489

85-
protected:
86-
bool checkExpired(timeType t) const
90+
bool checkExpired(const timeType t) const
8791
{
8892
return (t - _start) >= _timeout;
8993
}
94+
95+
protected:
9096

9197
bool expiredRetrigger()
9298
{

libraries/ESP8266mDNS/examples/LEAmDNS/mDNS_Clock/mDNS_Clock.ino

+19-20
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@
4949
5050
*/
5151
#include <ESP8266mDNS.h>
52-
#include <LEATimeFlag.h>
53-
52+
#include <PolledTimeout.h>
5453
/*
5554
Global defines and vars
5655
*/
@@ -66,15 +65,15 @@
6665
#define STAPSK "your-password"
6766
#endif
6867

69-
const char* ssid = STASSID;
70-
const char* password = STAPSK;
68+
const char* ssid = STASSID;
69+
const char* password = STAPSK;
7170

72-
char* pcHostDomain = 0; // Negociated host domain
73-
bool bHostDomainConfirmed = false; // Flags the confirmation of the host domain
74-
LEAmDNS::MDNSResponder::hMDNSService hMDNSService = 0; // The handle of the clock service in the MDNS responder
71+
char* pcHostDomain = 0; // Negociated host domain
72+
bool bHostDomainConfirmed = false; // Flags the confirmation of the host domain
73+
MDNSResponder::hMDNSService hMDNSService = 0; // The handle of the clock service in the MDNS responder
7574

7675
// TCP server at port 'SERVICE_PORT' will respond to HTTP requests
77-
WiFiServer server(SERVICE_PORT);
76+
WiFiServer server(SERVICE_PORT);
7877

7978

8079
/*
@@ -86,7 +85,7 @@ const char* getTimeString(void) {
8685
time_t now = time(nullptr);
8786
ctime_r(&now, acTimeString);
8887
size_t stLength;
89-
while (((stLength = os_strlen(acTimeString))) &&
88+
while (((stLength = strlen(acTimeString))) &&
9089
('\n' == acTimeString[stLength - 1])) {
9190
acTimeString[stLength - 1] = 0; // Remove trailing line break...
9291
}
@@ -136,8 +135,8 @@ bool setStationHostname(const char* p_pcHostname) {
136135
This can be triggered by calling MDNS.announce().
137136
138137
*/
139-
bool MDNSDynamicServiceTxtCallback(LEAmDNS::MDNSResponder* p_pMDNSResponder,
140-
const LEAmDNS::MDNSResponder::hMDNSService p_hService,
138+
bool MDNSDynamicServiceTxtCallback(MDNSResponder* p_pMDNSResponder,
139+
const MDNSResponder::hMDNSService p_hService,
141140
void* p_pUserdata) {
142141
Serial.println("MDNSDynamicServiceTxtCallback");
143142
(void) p_pUserdata;
@@ -161,9 +160,9 @@ bool MDNSDynamicServiceTxtCallback(LEAmDNS::MDNSResponder* p_pMDNSResponder,
161160
restarted via p_pMDNSResponder->setHostname().
162161
163162
*/
164-
bool MDNSProbeResultCallback(LEAmDNS::MDNSResponder* p_pMDNSResponder,
163+
bool MDNSProbeResultCallback(MDNSResponder* p_pMDNSResponder,
165164
const char* p_pcDomainName,
166-
const LEAmDNS::MDNSResponder::hMDNSService p_hService,
165+
const MDNSResponder::hMDNSService p_hService,
167166
bool p_bProbeResult,
168167
void* p_pUserdata) {
169168
Serial.println("MDNSProbeResultCallback");
@@ -192,7 +191,7 @@ bool MDNSProbeResultCallback(LEAmDNS::MDNSResponder* p_pMDNSResponder,
192191
}
193192
} else {
194193
// Change hostname, use '-' as divider between base name and index
195-
if (LEAmDNS::MDNSResponder::indexDomain(pcHostDomain, "-", 0)) {
194+
if (MDNSResponder::indexDomain(pcHostDomain, "-", 0)) {
196195
p_pMDNSResponder->setHostname(pcHostDomain);
197196
} else {
198197
Serial.println("MDNSProbeResultCallback: FAILED to update hostname!");
@@ -286,10 +285,10 @@ void setup(void) {
286285
setClock();
287286

288287
// Setup MDNS responder
289-
LEAmDNS::MDNS.setProbeResultCallback(MDNSProbeResultCallback, 0);
288+
MDNS.setProbeResultCallback(MDNSProbeResultCallback, 0);
290289
// Init the (currently empty) host domain string with 'esp8266'
291-
if ((!LEAmDNS::MDNSResponder::indexDomain(pcHostDomain, 0, "esp8266")) ||
292-
(!LEAmDNS::MDNS.begin(pcHostDomain))) {
290+
if ((!MDNSResponder::indexDomain(pcHostDomain, 0, "esp8266")) ||
291+
(!MDNS.begin(pcHostDomain))) {
293292
Serial.println("Error setting up MDNS responder!");
294293
while (1) { // STOP
295294
delay(1000);
@@ -314,17 +313,17 @@ void loop(void) {
314313
}
315314

316315
// Allow MDNS processing
317-
LEAmDNS::MDNS.update();
316+
MDNS.update();
318317

319318
// Update time (if needed)
320319
//static unsigned long ulNextTimeUpdate = UPDATE_CYCLE;
321-
static clsLEATimeFlag timeFlag(UPDATE_CYCLE);
320+
static clsMDNSTimeFlag timeFlag(UPDATE_CYCLE);
322321
if (timeFlag.flagged()/*ulNextTimeUpdate < millis()*/) {
323322

324323
if (hMDNSService) {
325324
// Just trigger a new MDNS announcement, this will lead to a call to
326325
// 'MDNSDynamicServiceTxtCallback', which will update the time TXT item
327-
LEAmDNS::MDNS.announce();
326+
MDNS.announce();
328327
}
329328
//ulNextTimeUpdate = (millis() + UPDATE_CYCLE); // Set update 'timer'
330329
timeFlag.restart();

libraries/ESP8266mDNS/examples/LEAmDNS/mDNS_ServiceMonitor/mDNS_ServiceMonitor.ino

+26-26
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,19 @@
5858
#define STAPSK "your-password"
5959
#endif
6060

61-
const char* ssid = STASSID;
62-
const char* password = STAPSK;
61+
const char* ssid = STASSID;
62+
const char* password = STAPSK;
6363

64-
char* pcHostDomain = 0; // Negociated host domain
65-
bool bHostDomainConfirmed = false; // Flags the confirmation of the host domain
66-
LEAmDNS::MDNSResponder::hMDNSService hMDNSService = 0; // The handle of the http service in the MDNS responder
67-
LEAmDNS::MDNSResponder::hMDNSServiceQuery hMDNSServiceQuery = 0; // The handle of the 'http.tcp' service query in the MDNS responder
64+
char* pcHostDomain = 0; // Negociated host domain
65+
bool bHostDomainConfirmed = false; // Flags the confirmation of the host domain
66+
MDNSResponder::hMDNSService hMDNSService = 0; // The handle of the http service in the MDNS responder
67+
MDNSResponder::hMDNSServiceQuery hMDNSServiceQuery = 0; // The handle of the 'http.tcp' service query in the MDNS responder
6868

69-
const String cstrNoHTTPServices = "Currently no 'http.tcp' services in the local network!<br/>";
70-
String strHTTPServices = cstrNoHTTPServices;
69+
const String cstrNoHTTPServices = "Currently no 'http.tcp' services in the local network!<br/>";
70+
String strHTTPServices = cstrNoHTTPServices;
7171

7272
// TCP server at port 'SERVICE_PORT' will respond to HTTP requests
73-
WiFiServer server(SERVICE_PORT);
73+
WiFiServer server(SERVICE_PORT);
7474

7575

7676
/*
@@ -89,24 +89,24 @@ bool setStationHostname(const char* p_pcHostname) {
8989
/*
9090
MDNSServiceQueryCallback
9191
*/
92-
bool MDNSServiceQueryCallback(LEAmDNS::MDNSResponder* p_pMDNSResponder, // The MDNS responder object
93-
const LEAmDNS::MDNSResponder::hMDNSServiceQuery p_hServiceQuery, // Handle to the service query
94-
uint32_t p_u32AnswerIndex, // Index of the updated answer
95-
uint32_t p_u32ServiceQueryAnswerMask, // Mask for the updated component
96-
bool p_bSetContent, // true: Component set, false: component deleted
97-
void* p_pUserdata) { // pUserdata; here '0', as none set via 'installServiceQuery'
92+
bool MDNSServiceQueryCallback(MDNSResponder* p_pMDNSResponder, // The MDNS responder object
93+
const MDNSResponder::hMDNSServiceQuery p_hServiceQuery, // Handle to the service query
94+
uint32_t p_u32AnswerIndex, // Index of the updated answer
95+
uint32_t p_u32ServiceQueryAnswerMask, // Mask for the updated component
96+
bool p_bSetContent, // true: Component set, false: component deleted
97+
void* p_pUserdata) { // pUserdata; here '0', as none set via 'installServiceQuery'
9898
(void) p_pUserdata;
9999
Serial.printf("MDNSServiceQueryCallback\n");
100100

101101
if ((p_pMDNSResponder) &&
102102
(hMDNSServiceQuery == p_hServiceQuery)) {
103103

104-
if (LEAmDNS::MDNSResponder::ServiceQueryAnswerType_ServiceDomain & p_u32ServiceQueryAnswerMask) {
104+
if (MDNSResponder::ServiceQueryAnswerType_ServiceDomain & p_u32ServiceQueryAnswerMask) {
105105
Serial.printf("MDNSServiceQueryCallback: Service domain '%s' %s index %u\n",
106106
p_pMDNSResponder->answerServiceDomain(p_hServiceQuery, p_u32AnswerIndex),
107107
(p_bSetContent ? "added at" : "removed from"),
108108
p_u32AnswerIndex);
109-
} else if (LEAmDNS::MDNSResponder::ServiceQueryAnswerType_HostDomainAndPort & p_u32ServiceQueryAnswerMask) {
109+
} else if (MDNSResponder::ServiceQueryAnswerType_HostDomainAndPort & p_u32ServiceQueryAnswerMask) {
110110
if (p_bSetContent) {
111111
Serial.printf("MDNSServiceQueryCallback: Host domain and port added/updated for service '%s': %s:%u\n",
112112
p_pMDNSResponder->answerServiceDomain(p_hServiceQuery, p_u32AnswerIndex),
@@ -116,7 +116,7 @@ bool MDNSServiceQueryCallback(LEAmDNS::MDNSResponder* p_pMDNSResponder,
116116
Serial.printf("MDNSServiceQueryCallback: Host domain and port removed from service '%s'\n",
117117
p_pMDNSResponder->answerServiceDomain(p_hServiceQuery, p_u32AnswerIndex));
118118
}
119-
} else if (LEAmDNS::MDNSResponder::ServiceQueryAnswerType_IP4Address & p_u32ServiceQueryAnswerMask) {
119+
} else if (MDNSResponder::ServiceQueryAnswerType_IP4Address & p_u32ServiceQueryAnswerMask) {
120120
if (p_bSetContent) {
121121
Serial.printf("MDNSServiceQueryCallback: IP4 address added/updated for service '%s':\n",
122122
p_pMDNSResponder->answerServiceDomain(p_hServiceQuery, p_u32AnswerIndex));
@@ -127,7 +127,7 @@ bool MDNSServiceQueryCallback(LEAmDNS::MDNSResponder* p_pMDNSResponder,
127127
Serial.printf("MDNSServiceQueryCallback: IP4 address removed from service '%s'\n",
128128
p_pMDNSResponder->answerServiceDomain(p_hServiceQuery, p_u32AnswerIndex));
129129
}
130-
} else if (LEAmDNS::MDNSResponder::ServiceQueryAnswerType_Txts & p_u32ServiceQueryAnswerMask) {
130+
} else if (MDNSResponder::ServiceQueryAnswerType_Txts & p_u32ServiceQueryAnswerMask) {
131131
if (p_bSetContent) {
132132
Serial.printf("MDNSServiceQueryCallback: TXT items added/updated for service '%s': %s\n",
133133
p_pMDNSResponder->answerServiceDomain(p_hServiceQuery, p_u32AnswerIndex),
@@ -192,9 +192,9 @@ bool MDNSServiceQueryCallback(LEAmDNS::MDNSResponder* p_pMDNSResponder,
192192
restarted via p_pMDNSResponder->setHostname().
193193
194194
*/
195-
bool MDNSProbeResultCallback(LEAmDNS::MDNSResponder* p_pMDNSResponder,
195+
bool MDNSProbeResultCallback(MDNSResponder* p_pMDNSResponder,
196196
const char* p_pcDomainName,
197-
const LEAmDNS::MDNSResponder::hMDNSService p_hService,
197+
const MDNSResponder::hMDNSService p_hService,
198198
bool p_bProbeResult,
199199
void* p_pUserdata) {
200200
(void) p_pUserdata;
@@ -235,7 +235,7 @@ bool MDNSProbeResultCallback(LEAmDNS::MDNSResponder* p_pMDNSResponder,
235235
}
236236
} else {
237237
// Change hostname, use '-' as divider between base name and index
238-
if (LEAmDNS::MDNSResponder::indexDomain(pcHostDomain, "-", 0)) {
238+
if (MDNSResponder::indexDomain(pcHostDomain, "-", 0)) {
239239
p_pMDNSResponder->setHostname(pcHostDomain);
240240
} else {
241241
Serial.println("MDNSProbeResultCallback: FAILED to update hostname!");
@@ -322,10 +322,10 @@ void setup(void) {
322322
Serial.println(WiFi.localIP());
323323

324324
// Setup MDNS responder
325-
LEAmDNS::MDNS.setProbeResultCallback(MDNSProbeResultCallback, 0);
325+
MDNS.setProbeResultCallback(MDNSProbeResultCallback, 0);
326326
// Init the (currently empty) host domain string with 'esp8266'
327-
if ((!LEAmDNS::MDNSResponder::indexDomain(pcHostDomain, 0, "esp8266")) ||
328-
(!LEAmDNS::MDNS.begin(pcHostDomain))) {
327+
if ((!MDNSResponder::indexDomain(pcHostDomain, 0, "esp8266")) ||
328+
(!MDNS.begin(pcHostDomain))) {
329329
Serial.println("Error setting up MDNS responder!");
330330
while (1) { // STOP
331331
delay(1000);
@@ -350,7 +350,7 @@ void loop(void) {
350350
}
351351

352352
// Allow MDNS processing
353-
LEAmDNS::MDNS.update();
353+
MDNS.update();
354354
}
355355

356356

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <ESP8266mDNS.h>
2+
3+
/*
4+
* MDNS responder global instance
5+
*
6+
* Class type that is instantiated depends on the type mapping in ESP8266mDNS.h
7+
*/
8+
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_MDNS)
9+
MDNSResponder MDNS;
10+
#endif
11+

libraries/ESP8266mDNS/src/ESP8266mDNS.h

+9-2
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,12 @@
4545
#include "ESP8266mDNS_Legacy.h"
4646
#include "LEAmDNS.h"
4747

48-
using namespace Legacy_MDNSResponder;
49-
//using namespace LEAmDNS;
48+
49+
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_MDNS)
50+
// Maps the implementation to use to the global namespace type
51+
//using MDNSResponder = Legacy_MDNSResponder::MDNSResponder; //legacy
52+
using MDNSResponder = esp8266::MDNSImplementation::MDNSResponder; //new
53+
54+
extern MDNSResponder MDNS;
55+
#endif
56+

libraries/ESP8266mDNS/src/ESP8266mDNS_Legacy.h

+2-6
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ License (MIT license):
4040
THE SOFTWARE.
4141
4242
*/
43-
#ifndef ESP8266MDNS_H
44-
#define ESP8266MDNS_H
43+
#ifndef ESP8266MDNS_LEGACY_H
44+
#define ESP8266MDNS_LEGACY_H
4545

4646
#include "ESP8266WiFi.h"
4747
#include "WiFiUdp.h"
@@ -139,10 +139,6 @@ class MDNSResponder {
139139
void _restart();
140140
};
141141

142-
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_MDNS)
143-
extern MDNSResponder MDNS;
144-
#endif
145-
146142
} // namespace Legacy_MDNSResponder
147143

148144
#endif //ESP8266MDNS_H

0 commit comments

Comments
 (0)