Skip to content

Commit 570b9a6

Browse files
LaborEtArsdevyte
authored andcommitted
LEAmDNS Fixes 1.1 (#5619)
* Fixes 1.1 - Better separation of ESP wifi thread code from user thread code - Added a flag for 'update()'-less use (disabled by default) - The too fast updates for service queries are fixed - Switched fully to PolledTimeout; LEATimeFlag not needed anymore (BTW: a const 'expired()' method would be helpful) - The device should stay visible now even after the first TTL timeout - Improved service querying (queries five times now) * Update mDNS_Clock.ino Removed references to LEATimeFlag.h * Update mDNS_Clock.ino Styling
1 parent 6883bee commit 570b9a6

File tree

9 files changed

+533
-331
lines changed

9 files changed

+533
-331
lines changed

Diff for: libraries/ESP8266mDNS/examples/LEAmDNS/mDNS_Clock/mDNS_Clock.ino

+2-5
Original file line numberDiff line numberDiff line change
@@ -316,17 +316,14 @@ void loop(void) {
316316
MDNS.update();
317317

318318
// Update time (if needed)
319-
//static unsigned long ulNextTimeUpdate = UPDATE_CYCLE;
320-
static clsMDNSTimeFlag timeFlag(UPDATE_CYCLE);
321-
if (timeFlag.flagged()/*ulNextTimeUpdate < millis()*/) {
319+
static esp8266::polledTimeout::periodic timeout(UPDATE_CYCLE);
320+
if (timeout.expired()) {
322321

323322
if (hMDNSService) {
324323
// Just trigger a new MDNS announcement, this will lead to a call to
325324
// 'MDNSDynamicServiceTxtCallback', which will update the time TXT item
326325
MDNS.announce();
327326
}
328-
//ulNextTimeUpdate = (millis() + UPDATE_CYCLE); // Set update 'timer'
329-
timeFlag.restart();
330327
}
331328
}
332329

Diff for: libraries/ESP8266mDNS/src/LEATimeFlag.h

-60
This file was deleted.

Diff for: libraries/ESP8266mDNS/src/LEAmDNS.cpp

+17-4
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,12 @@ MDNSResponder::MDNSResponder(void)
5555
m_pcHostname(0),
5656
m_pServiceQueries(0),
5757
m_fnServiceTxtCallback(0),
58-
m_pServiceTxtCallbackUserdata(0) {
58+
m_pServiceTxtCallbackUserdata(0),
59+
#ifdef ENABLE_ESP_MDNS_RESPONDER_PASSIV_MODE
60+
m_bPassivModeEnabled(true) {
61+
#else
62+
m_bPassivModeEnabled(false) {
63+
#endif
5964

6065
}
6166

@@ -123,7 +128,7 @@ bool MDNSResponder::begin(const char* p_pcHostname,
123128
*/
124129
bool MDNSResponder::close(void) {
125130

126-
_announce(false);
131+
_announce(false, true);
127132
_resetProbeStatus(false); // Stop probing
128133

129134
_releaseServiceQueries();
@@ -205,6 +210,7 @@ MDNSResponder::hMDNSService MDNSResponder::addService(const char* p_pcName,
205210
}
206211
}
207212
} // else: bad arguments
213+
DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] addService: %s to add '%s.%s.%s'!\n"), (hResult ? "Succeeded" : "FAILED"), (p_pcName ?: "-"), p_pcService, p_pcProtocol); );
208214
DEBUG_EX_ERR(if (!hResult) { DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] addService: FAILED to add '%s.%s.%s'!\n"), (p_pcName ?: "-"), p_pcService, p_pcProtocol); } );
209215
return hResult;
210216
}
@@ -768,13 +774,17 @@ MDNSResponder::hMDNSServiceQuery MDNSResponder::installServiceQuery(const char*
768774
pServiceQuery->m_bLegacyQuery = false;
769775

770776
if (_sendMDNSServiceQuery(*pServiceQuery)) {
777+
pServiceQuery->m_u8SentCount = 1;
778+
pServiceQuery->m_ResendTimeout.reset(MDNS_DYNAMIC_QUERY_RESEND_DELAY);
779+
771780
hResult = (hMDNSServiceQuery)pServiceQuery;
772781
}
773782
else {
774783
_removeServiceQuery(pServiceQuery);
775784
}
776785
}
777-
DEBUG_EX_ERR(if (!hResult) { DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] installServiceQuery: FAILED for '%s.%s'!\n"), (p_pcService ?: "-"), (p_pcProtocol ?: "-")); } );
786+
DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] installServiceQuery: %s for '%s.%s'!\n\n"), (hResult ? "Succeeded" : "FAILED"), (p_pcService ?: "-"), (p_pcProtocol ?: "-")););
787+
DEBUG_EX_ERR(if (!hResult) { DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] installServiceQuery: FAILED for '%s.%s'!\n\n"), (p_pcService ?: "-"), (p_pcProtocol ?: "-")); } );
778788
return hResult;
779789
}
780790

@@ -1073,6 +1083,9 @@ bool MDNSResponder::notifyAPChange(void) {
10731083
*/
10741084
bool MDNSResponder::update(void) {
10751085

1086+
if (m_bPassivModeEnabled) {
1087+
m_bPassivModeEnabled = false;
1088+
}
10761089
return _process(true);
10771090
}
10781091

@@ -1083,7 +1096,7 @@ bool MDNSResponder::update(void) {
10831096
*/
10841097
bool MDNSResponder::announce(void) {
10851098

1086-
return (_announce());
1099+
return (_announce(true, true));
10871100
}
10881101

10891102
/*

Diff for: libraries/ESP8266mDNS/src/LEAmDNS.h

+47-19
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
* Reference:
6666
* Used mDNS messages:
6767
* A (0x01): eg. esp8266.local A OP TTL 123.456.789.012
68-
* AAAA (01Cx): eg. esp8266.local AAAA OP TTL 1234:5678::90
68+
* AAAA (0x1C): eg. esp8266.local AAAA OP TTL 1234:5678::90
6969
* PTR (0x0C, srv name): eg. _http._tcp.local PTR OP TTL MyESP._http._tcp.local
7070
* PTR (0x0C, srv type): eg. _services._dns-sd._udp.local PTR OP TTL _http._tcp.local
7171
* PTR (0x0C, IP4): eg. 012.789.456.123.in-addr.arpa PTR OP TTL esp8266.local
@@ -107,7 +107,8 @@
107107
#include "lwip/udp.h"
108108
#include "debug.h"
109109
#include "include/UdpContext.h"
110-
#include "LEATimeFlag.h"
110+
#include <limits>
111+
#include <PolledTimeout.h>
111112

112113
#include "ESP8266WiFi.h"
113114

@@ -786,8 +787,9 @@ class MDNSResponder {
786787
*/
787788
struct stcProbeInformation {
788789
enuProbingStatus m_ProbingStatus;
789-
uint8_t m_u8ProbesSent;
790-
clsMDNSTimeFlag m_NextProbeTimeFlag;
790+
uint8_t m_u8SentCount; // Used for probes and announcements
791+
esp8266::polledTimeout::oneShot m_Timeout; // Used for probes and announcements
792+
//clsMDNSTimeFlag m_TimeFlag; // Used for probes and announcements
791793
bool m_bConflict;
792794
bool m_bTiebreakNeeded;
793795
MDNSProbeResultCallbackFn m_fnProbeResultCallback;
@@ -842,14 +844,32 @@ class MDNSResponder {
842844
* stcTTL
843845
*/
844846
struct stcTTL {
845-
clsMDNSTimeFlag m_TTLTimeFlag;
846-
bool m_bUpdateScheduled;
847+
/**
848+
* timeoutLevel_t
849+
*/
850+
typedef uint8_t timeoutLevel_t;
851+
/**
852+
* TIMEOUTLEVELs
853+
*/
854+
const timeoutLevel_t TIMEOUTLEVEL_UNSET = 0;
855+
const timeoutLevel_t TIMEOUTLEVEL_BASE = 80;
856+
const timeoutLevel_t TIMEOUTLEVEL_INTERVAL = 5;
857+
const timeoutLevel_t TIMEOUTLEVEL_FINAL = 100;
847858

848-
stcTTL(uint32_t p_u32TTL = 0);
859+
uint32_t m_u32TTL;
860+
esp8266::polledTimeout::oneShot m_TTLTimeout;
861+
timeoutLevel_t m_timeoutLevel;
862+
863+
stcTTL(void);
849864
bool set(uint32_t p_u32TTL);
850865

851-
bool has80Percent(void) const;
852-
bool isOutdated(void) const;
866+
bool flagged(void) const;
867+
bool restart(void);
868+
869+
bool prepareDeletion(void);
870+
bool finalTimeoutLevel(void) const;
871+
872+
unsigned long timeout(void) const;
853873
};
854874
#ifdef MDNS_IP4_SUPPORT
855875
/**
@@ -861,7 +881,7 @@ class MDNSResponder {
861881
stcTTL m_TTL;
862882

863883
stcIP4Address(IPAddress p_IPAddress,
864-
uint32_t p_u32TTL = 0);
884+
uint32_t p_u32TTL = 0);
865885
};
866886
#endif
867887
#ifdef MDNS_IP6_SUPPORT
@@ -872,6 +892,9 @@ class MDNSResponder {
872892
stcIP6Address* m_pNext;
873893
IP6Address m_IPAddress;
874894
stcTTL m_TTL;
895+
896+
stcIP6Address(IPAddress p_IPAddress,
897+
uint32_t p_u32TTL = 0);
875898
};
876899
#endif
877900

@@ -932,13 +955,15 @@ class MDNSResponder {
932955
#endif
933956
};
934957

935-
stcMDNSServiceQuery* m_pNext;
936-
stcMDNS_RRDomain m_ServiceTypeDomain; // eg. _http._tcp.local
937-
MDNSServiceQueryCallbackFn m_fnCallback;
938-
void* m_pUserdata;
939-
bool m_bLegacyQuery;
940-
bool m_bAwaitingAnswers;
941-
stcAnswer* m_pAnswers;
958+
stcMDNSServiceQuery* m_pNext;
959+
stcMDNS_RRDomain m_ServiceTypeDomain; // eg. _http._tcp.local
960+
MDNSServiceQueryCallbackFn m_fnCallback;
961+
void* m_pUserdata;
962+
bool m_bLegacyQuery;
963+
uint8_t m_u8SentCount;
964+
esp8266::polledTimeout::oneShot m_ResendTimeout;
965+
bool m_bAwaitingAnswers;
966+
stcAnswer* m_pAnswers;
942967

943968
stcMDNSServiceQuery(void);
944969
~stcMDNSServiceQuery(void);
@@ -1012,6 +1037,7 @@ class MDNSResponder {
10121037
WiFiEventHandler m_GotIPHandler;
10131038
MDNSDynamicServiceTxtCallbackFn m_fnServiceTxtCallback;
10141039
void* m_pServiceTxtCallbackUserdata;
1040+
bool m_bPassivModeEnabled;
10151041
stcProbeInformation m_HostProbeInformation;
10161042

10171043
/** CONTROL **/
@@ -1047,7 +1073,8 @@ class MDNSResponder {
10471073
bool _cancelProbingForService(stcMDNSService& p_rService);
10481074

10491075
/* ANNOUNCE */
1050-
bool _announce(bool p_bAnnounce = true);
1076+
bool _announce(bool p_bAnnounce,
1077+
bool p_bIncludeServices);
10511078
bool _announceService(stcMDNSService& p_rService,
10521079
bool p_bAnnounce = true);
10531080

@@ -1064,7 +1091,8 @@ class MDNSResponder {
10641091
IPAddress p_IPAddress);
10651092
bool _sendMDNSServiceQuery(const stcMDNSServiceQuery& p_ServiceQuery);
10661093
bool _sendMDNSQuery(const stcMDNS_RRDomain& p_QueryDomain,
1067-
uint16_t p_u16QueryType);
1094+
uint16_t p_u16QueryType,
1095+
stcMDNSServiceQuery::stcAnswer* p_pKnownAnswers = 0);
10681096

10691097
IPAddress _getResponseMulticastInterface(int p_iWiFiOpModes) const;
10701098

0 commit comments

Comments
 (0)