8000 LEA_mDNSResponder by LaborEtArs · Pull Request #5358 · esp8266/Arduino · GitHub
[go: up one dir, main page]

Skip to content

LEA_mDNSResponder #5358

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Finalized integration
  • Loading branch information
LaborEtArs committed Nov 19, 2018
commit 3485532cf2cbe09436007644d543ddb06db0fe81
16 changes: 15 additions & 1 deletion libraries/ESP8266WiFi/src/include/ClientContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,21 @@ class ClientContext
}
_connect_pending = 1;
_op_start_time = millis();
/*LEA*/
//Serial.print("\nClientContext::connect (A)");
uint32_t nextPrint = (_op_start_time + 100);
while ((millis() < (_op_start_time + _timeout_ms)) &&
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't do millis() compare like this. Need to subtract now from the start millis and compare to the timeout value. OTW you'll end up with bad things when millis or millis + timeout rolls over.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohhh... the ClientContext changes wasn‘t supposed to be part of this... sorry!

(state() != ESTABLISHED)) {
yield();
if (millis() > nextPrint) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same millis issue. Plus, I'm not sure what the purpose of this as we're not going to be printing anything anyway...

Would suggest going back to the delay(_timeout_ms); for this whole section.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above, this is just a q&d solution for the connection problems with the existing implementation! Wasn‘t supposed to be committed here, sorry! Just remove if possible!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find this interesting. Does the current implementation have an issue? Does this resolve it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current implementation relies on a break of the delay based on a call to _connected. When experimenting with BearSSL to create an Apple Push Notification Service, I found, that the Client::connect call stalled the whole system (without any timeout) because the supposed break of this delay never happened... My q&d workaround solved this problem for me...

//Serial.printf(".");
nextPrint = (millis() + 100);
}
}
//Serial.println("\nClientContext::connect (B)");
// This delay will be interrupted by esp_schedule in the connect callback
delay(_timeout_ms);
//delay(_timeout_ms);
/*LEA*/
_connect_pending = 0;
if (!_pcb) {
DEBUGV(":cabrt\r\n");
Expand All @@ -139,6 +152,7 @@ class ClientContext
abort();
return 0;
}
//Serial.println("ClientContext::connect (C)");
return 1;
}

Expand Down
F438
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@

/*
* Include the MDNSResponder (the library needs to be included also)
* As LEA_MDNSResponder is experimantal in the ESP8266 environment currently, the
* As LEA MDNSResponder is experimantal in the ESP8266 environment currently, the
* legacy MDNSResponder is defaulted in th include file.
* There are two ways to access LEA_MDNSResponder:
* There are two ways to access LEA MDNSResponder:
* 1. Prepend every declaration and call to global declarations or functions with the namespace, like:
* 'LEA_MDNSResponder::MDNSResponder::hMDNSService hMDNSService;'
* 'LEAmDNS::MDNSResponder::hMDNSService hMDNSService;'
* This way is used in the example. But be careful, if the namespace declaration is missing
* somewhere, the call might go to the legacy implementation...
* 2. Open 'ESP8266mDNS.h' and set LEA_MDNSResponder to default.
* 2. Open 'ESP8266mDNS.h' and set LEAmDNS to default.
*
*/
#include <ESP8266mDNS.h>
Expand All @@ -54,19 +54,19 @@
* Global defines and vars
*/

#define TIMEZONE_OFFSET 1 // CET
#define DST_OFFSET 1 // CEST
#define TIMEZONE_OFFSET 1 // CET
#define DST_OFFSET 1 // CEST
#define UPDATE_CYCLE (1 * 1000) // every second

#define SERVICE_PORT 80 // HTTP port
#define SERVICE_PORT 80 // HTTP port


const char* ssid = "............";
const char* password = "............";
const char* ssid = "............";
const char* password = "............";

char* pcHostDomain = 0; // Negociated host domain
bool bHostDomainConfirmed = false; // Flags the confirmation of the host domain
LEA_MDNSResponder::MDNSResponder::hMDNSService hMDNSService = 0; // The handle of the clock service in the MDNS responder
char* pcHostDomain = 0; // Negociated host domain
bool bHostDomainConfirmed = false; // Flags the confirmation of the host domain
LEAmDNS::MDNSResponder::hMDNSService hMDNSService = 0; // The handle of the clock service in the MDNS responder

// TCP server at port 'SERVICE_PORT' will respond to HTTP requests
WiFiServer server(SERVICE_PORT);
Expand All @@ -77,13 +77,13 @@ WiFiServer server(SERVICE_PORT);
*/
const char* getTimeString(void) {

static char acTimeString[32];
static char acTimeString[32];
time_t now = time(nullptr);
ctime_r(&now, acTimeString);
size_t stLength;
while (((stLength = os_strlen(acTimeString))) &&
('\n' == acTimeString[stLength - 1])) {
acTimeString[stLength - 1] = 0; // Remove trailing line break...
acTimeString[stLength - 1] = 0; // Remove trailing line break...
}
return acTimeString;
}
Expand Down Expand Up @@ -131,8 +131,8 @@ bool setStationHostname(const char* p_pcHostname) {
* This can be triggered by calling MDNS.announce().
*
*/
bool MDNSDynamicServiceTxtCallback(LEA_MDNSResponder::MDNSResponder* p_pMDNSResponder,
const LEA_MDNSResponder::MDNSResponder::hMDNSService p_hService,
bool MDNSDynamicServiceTxtCallback(LEAmDNS::MDNSResponder* p_pMDNSResponder,
const LEAmDNS::MDNSResponder::hMDNSService p_hService,
void* p_pUserdata) {
Serial.println("MDNSDynamicServiceTxtCallback");
(void) p_pUserdata;
Expand All @@ -156,9 +156,9 @@ bool MDNSDynamicServiceTxtCallback(LEA_MDNSResponder::MDNSResponder* p_pMDNSResp
* restarted via p_pMDNSResponder->setHostname().
*
*/
bool MDNSProbeResultCallback(LEA_MDNSResponder::MDNSResponder* p_pMDNSResponder,
bool MDNSProbeResultCallback(LEAmDNS::MDNSResponder* p_pMDNSResponder,
const char* p_pcDomainName,
const LEA_MDNSResponder::MDNSResponder::hMDNSService p_hService,
const LEAmDNS::MDNSResponder::hMDNSService p_hService,
bool p_bProbeResult,
void* p_pUserdata) {
Serial.println("MDNSProbeResultCallback");
Expand Down Expand Up @@ -188,7 +188,7 @@ bool MDNSProbeResultCallback(LEA_MDNSResponder::MDNSResponder* p_pMDNSResponder,
}
else {
// Change hostname, use '-' as divider between base name and index
if (LEA_MDNSResponder::MDNSResponder::indexDomain(pcHostDomain, "-", 0)) {
if (LEAmDNS::MDNSResponder::indexDomain(pcHostDomain, "-", 0)) {
p_pMDNSResponder->setHostname(pcHostDomain);
}
else {
Expand Down Expand Up @@ -283,10 +283,10 @@ void setup(void) {
setClock();

// Setup MDNS responder
LEA_MDNSResponder::MDNS.setProbeResultCallback(MDNSProbeResultCallback, 0);
LEAmDNS::MDNS.setProbeResultCallback(MDNSProbeResultCallback, 0);
// Init the (currently empty) host domain string with 'esp8266'
if ((!LEA_MDNSResponder::MDNSResponder::indexDomain(pcHostDomain, 0, "esp8266")) ||
(!LEA_MDNSResponder::MDNS.begin(pcHostDomain))) {
if ((!LEAmDNS::MDNSResponder::indexDomain(pcHostDomain, 0, "esp8266")) ||
(!LEAmDNS::MDNS.begin(pcHostDomain))) {
Serial.println("Error setting up MDNS responder!");
while (1) { // STOP
delay(1000);
Expand All @@ -311,7 +311,7 @@ void loop(void) {
}

// Allow MDNS processing
LEA_MDNSResponder::MDNS.update();
LEAmDNS::MDNS.update();

// Update time (if needed)
static unsigned long ulNextTimeUpdate = UPDATE_CYCLE;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same issue w/millis wraparound here, I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I‘ve only now learned, that a millis() roll over happens every 72 minutes... Is there any alternative way? Maybe some tick counter??

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m getting confused ... is the millis() rollover really hard-connected to the micros() rollover?? Otherwise it would be a rollover time of about 6 weeks... (haven’t seen an ESP running that long without running out of memory and needs a restart :-( )

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@LaborEtArs millis() does a rollover, so you can't compare the value returned by millis() against a delta. Instead, you have to compare a delta against a value. A delta (i.e.: a millis() - op_start_time) considers the case of rollover, so the result is always correct if you happen to be caught in the middle of a rollover.
Well, "always correct" as long as you don't have 2 rollovers in between 😛 .
This kind of bug, and having to think your way through it every time you need to do a timeout operation, is the motivation behind #5198 : have the details abstracted and simplify coding.
But it's not merged yet.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, got the problem!
I guess, I will incorporate a similar class as your pooledTimeout here.
BTW: There had been a short discussion about the types and type names in that thread. Personally I really don’t like the use of ‘using ... unsigned int’ for standard types, as it extremely reduces the readability of the code. Of course not, if there are just one or two of those ‘using’ declarations, but the more the worse it becomes. I would plead for standard types like uint32_t in cases like this.

Expand All @@ -320,7 +320,7 @@ void loop(void) {
if (hMDNSService) {
// Just trigger a new MDNS announcement, this will lead to a call to
// 'MDNSDynamicServiceTxtCallback', which will update the time TXT item
LEA_MDNSResponder::MDNS.announce();
LEAmDNS::MDNS.announce();
}
ulNextTimeUpdate = (millis() + UPDATE_CYCLE); // Set update 'timer'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@

/*
* Include the MDNSResponder (the library needs to be included also)
* As LEA_MDNSResponder is experimantal in the ESP8266 environment currently, the
* As LEA MDNSResponder is experimantal in the ESP8266 environment currently, the
* legacy MDNSResponder is defaulted in th include file.
* There are two ways to access LEA_MDNSResponder:
* There are two ways to access LEA MDNSResponder:
* 1. Prepend every declaration and call to global declarations or functions with the namespace, like:
* 'LEA_MDNSResponder::MDNSResponder::hMDNSService hMDNSService;'
* 'LEAmDNS:MDNSResponder::hMDNSService hMDNSService;'
* This way is used in the example. But be careful, if the namespace declaration is missing
* somewhere, the call might go to the legacy implementation...
* 2. Open 'ESP8266mDNS.h' and set LEA_MDNSResponder to default.
* 2. Open 'ESP8266mDNS.h' and set LEAmDNS to default.
*
*/
#include <ESP8266mDNS.h>
Expand All @@ -51,16 +51,16 @@
* Global defines and vars
*/

#define SERVICE_PORT 80 // HTTP port
#define SERVICE_PORT 80 // HTTP port


const char* ssid = "............";
const char* password = "............";

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

const String cstrNoHTTPServices = "Currently no 'http.tcp' services in the local network!<br/>";
String strHTTPServices = cstrNoHTTPServices;
Expand All @@ -85,25 +85,25 @@ bool setStationHostname(const char* p_pcHostname) {
/*
* MDNSServiceQueryCallback
*/
bool MDNSServiceQueryCallback(LEA_MDNSResponder::MDNSResponder* p_pMDNSResponder, // The MDNS responder object
const LEA_MDNSResponder::MDNSResponder::hMDNSServiceQuery p_hServiceQuery, // Handle to the service query
uint32_t p_u32AnswerIndex, // Index of the updated answer
uint32_t p_u32ServiceQueryAnswerMask, // Mask for the updated component
bool p_bSetContent, // true: Component set, false: component deleted
void* p_pUserdata) { // pUserdata; here '0', as none set via 'installServiceQuery'
bool MDNSServiceQueryCallback(LEAmDNS::MDNSResponder* p_pMDNSResponder, // The MDNS responder object
const LEAmDNS::MDNSResponder::hMDNSServiceQuery p_hServiceQuery, // Handle to the service query
uint32_t p_u32AnswerIndex, // Index of the updated answer
uint32_t p_u32ServiceQueryAnswerMask, // Mask for the updated component
bool p_bSetContent, // true: Component set, false: component deleted
void* p_pUserdata) { // pUserdata; here '0', as none set via 'installServiceQuery'
(void) p_pUserdata;
Serial.printf("MDNSServiceQueryCallback\n");

if ((p_pMDNSResponder) &&
(hMDNSServiceQuery == p_hServiceQuery)) {

if (LEA_MDNSResponder::MDNSResponder::ServiceQueryAnswerType_ServiceDomain & p_u32ServiceQueryAnswerMask) {
if (LEAmDNS::MDNSResponder::ServiceQueryAnswerType_ServiceDomain & p_u32ServiceQueryAnswerMask) {
Serial.printf("MDNSServiceQueryCallback: Service domain '%s' %s index %u\n",
p_pMDNSResponder->answerServiceDomain(p_hServiceQuery, p_u32AnswerIndex),
(p_bSetContent ? "added at" : "removed from"),
p_u32AnswerIndex);
}
else if (LEA_MDNSResponder::MDNSResponder::ServiceQueryAnswerType_HostDomainAndPort & p_u32ServiceQueryAnswerMask) {
else if (LEAmDNS::MDNSResponder::ServiceQueryAnswerType_HostDomainAndPort & p_u32ServiceQueryAnswerMask) {
if (p_bSetContent) {
Serial.printf("MDNSServiceQueryCallback: Host domain and port added/updated for service '%s': %s:%u\n",
p_pMDNSResponder->answerServiceDomain(p_hServiceQuery, p_u32AnswerIndex),
Expand All @@ -115,7 +115,7 @@ bool MDNSServiceQueryCallback(LEA_MDNSResponder::MDNSResponder* p_pMDNSResponder
p_pMDNSResponder->answerServiceDomain(p_hServiceQuery, p_u32AnswerIndex));
}
}
else if (LEA_MDNSResponder::MDNSResponder::ServiceQueryAnswerType_IP4Address & p_u32ServiceQueryAnswerMask) {
else if (LEAmDNS::MDNSResponder::ServiceQueryAnswerType_IP4Address & p_u32ServiceQueryAnswerMask) {
if (p_bSetContent) {
Serial.printf("MDNSServiceQueryCallback: IP4 address added/updated for service '%s':\n",
p_pMDNSResponder->answerServiceDomain(p_hServiceQuery, p_u32AnswerIndex));
Expand All @@ -128,7 +128,7 @@ bool MDNSServiceQueryCallback(LEA_MDNSResponder::MDNSResponder* p_pMDNSResponder
p_pMDNSResponder->answerServiceDomain(p_hServiceQuery, p_u32AnswerIndex));
}
}
else if (LEA_MDNSResponder::MDNSResponder::ServiceQueryAnswerType_Txts & p_u32ServiceQueryAnswerMask) {
else if (LEAmDNS::MDNSResponder::ServiceQueryAnswerType_Txts & p_u32ServiceQueryAnswerMask) {
if (p_bSetContent) {
Serial.printf("MDNSServiceQueryCallback: TXT items added/updated for service '%s': %s\n",
p_pMDNSResponder->answerServiceDomain(p_hServiceQuery, p_u32AnswerIndex),
Expand Down Expand Up @@ -163,10 +163,10 @@ bool MDNSServiceQueryCallback(LEA_MDNSResponder::MDNSResponder* p_pMDNSResponder
if (p_pMDNSResponder->hasAnswerIP4Address(p_hServiceQuery, u)) {
strHTTPServices += " IP4: ";
for (uint32_t u2=0; u2<p_pMDNSResponder->answerIP4AddressCount(p_hServiceQuery, u); ++u2) {
if (0 != u2) {
if (0 != u2) {
strHTTPServices += ", ";
}
strHTTPServices += p_pMDNSResponder->answerIP4Address(p_hServiceQuery, u, u2).toString();
}
strHTTPServices += p_pMDNSResponder->answerIP4Address(p_hServiceQuery, u, u2).toString();
}
}
// MDNS TXT items
Expand Down Expand Up @@ -195,9 +195,9 @@ bool MDNSServiceQueryCallback(LEA_MDNSResponder::MDNSResponder* p_pMDNSResponder
* restarted via p_pMDNSResponder->setHostname().
*
*/
bool MDNSProbeResultCallback(LEA_MDNSResponder::MDNSResponder* p_pMDNSResponder,
bool MDNSProbeResultCallback(LEAmDNS::MDNSResponder* p_pMDNSResponder,
const char* p_pcDomainName,
const LEA_MDNSResponder::MDNSResponder::hMDNSService p_hService,
const LEAmDNS::MDNSResponder::hMDNSService p_hService,
bool p_bProbeResult,
void* p_pUserdata) {
(void) p_pUserdata;
Expand Down Expand Up @@ -240,7 +240,7 @@ bool MDNSProbeResultCallback(LEA_MDNSResponder::MDNSResponder* p_pMDNSResponder,
}
else {
// Change hostname, use '-' as divider between base name and index
if (LEA_MDNSResponder::MDNSResponder::indexDomain(pcHostDomain, "-", 0)) {
if (LEAmDNS::MDNSResponder::indexDomain(pcHostDomain, "-", 0)) {
p_pMDNSResponder->setHostname(pcHostDomain);
}
else {
Expand Down Expand Up @@ -328,10 +328,10 @@ void setup(void) {
Serial.println(WiFi.localIP());

// Setup MDNS responder
LEA_MDNSResponder::MDNS.setProbeResultCallback(MDNSProbeResultCallback, 0);
LEAmDNS::MDNS.setProbeResultCallback(MDNSProbeResultCallback, 0);
// Init the (currently empty) host domain string with 'esp8266'
if ((!LEA_MDNSResponder::MDNSResponder::indexDomain(pcHostDomain, 0, "esp8266")) ||
(!LEA_MDNSResponder::MDNS.begin(pcHostDomain))) {
if ((!LEAmDNS::MDNSResponder::indexDomain(pcHostDomain, 0, "esp8266")) ||
(!LEAmDNS::MDNS.begin(pcHostDomain))) {
Serial.println("Error setting up MDNS responder!");
while (1) { // STOP
delay(1000);
Expand All @@ -356,7 +356,7 @@ void loop(void) {
}

// Allow MDNS processing
LEA_MDNSResponder::MDNS.update();
LEAmDNS::MDNS.update();
}


9 changes: 9 additions & 0 deletions libraries/ESP8266mDNS/library.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name=ESP8266mDNS
version=1.0
author=multiple, see files
maintainer=LaborEtArs
sentence=Creates a mDNS responder.
paragraph=Creates a mDNS responder to ensure host domain uniqueness in local networks and to allow for mDNS service discovery and announcment.
category=Communication
url=
architectures=esp8266
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@

*/

#include "Legacy_MDNSResponder/ESP8266MDNS.h"
#include "LEA_MDNSResponder/ESP8266MDNS.h"
#include "ESP8266MDNS_Legacy.h"
#include "LEAmDNS.h"

using namespace Legacy_MDNSResponder;
//using namespace LEA_MDNSResponder;
//using namespace LEAmDNS;
Loading
0