mirror of
https://github.com/esp8266/Arduino.git
synced 2025-04-19 23:22:16 +03:00
Functional update, host and service probes (#5653)
* Functional update, host and service probes * Fix ServiceMonitor.ino warnings * Adding MDNSServiceQueryCallback functional ServiceMonitor.ino needs some updates for web page but works on Serial output. * DynamicServices Functional * Fix ServiceMonitor to match latest MDNSServiceInfo * Fix unused variable in LEAdns.h * mDNS_Clock.ino fix * example restyle * Add keyValues and answerInfo * Waring and formattin fix * Change struct MDNSServiceInfo { MDNSServiceInfo(MDNSResponder ... to struct MDNSServiceInfo { MDNSServiceInfo(MDNSResponder& * Make AnswerType user friendly * Update ServiceInfo example * Code cleanup * AnswerType update, Astyle update servicemonitor * Update clock example to webserver * Second typedef for probe callbacks Change String -> const char* at multiple locations * Optimizations * Update callbacks to void * esp32 compatibility * std::map to const char* * Fix emplace_back call * Change Dynamic callback to void(...) * Add WiFi events reset() in close()
This commit is contained in:
parent
848fbf5b4a
commit
82789d201c
@ -34,6 +34,7 @@
|
|||||||
|
|
||||||
#include <ESP8266WiFi.h>
|
#include <ESP8266WiFi.h>
|
||||||
#include <WiFiClient.h>
|
#include <WiFiClient.h>
|
||||||
|
#include <ESP8266WebServer.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -72,9 +73,8 @@ char* pcHostDomain = 0; // Negociated
|
|||||||
bool bHostDomainConfirmed = false; // Flags the confirmation of the host domain
|
bool bHostDomainConfirmed = false; // Flags the confirmation of the host domain
|
||||||
MDNSResponder::hMDNSService hMDNSService = 0; // The handle of the clock service in the MDNS responder
|
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
|
// HTTP server at port 'SERVICE_PORT' will respond to HTTP requests
|
||||||
WiFiServer server(SERVICE_PORT);
|
ESP8266WebServer server(SERVICE_PORT);
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
getTimeString
|
getTimeString
|
||||||
@ -135,18 +135,13 @@ bool setStationHostname(const char* p_pcHostname) {
|
|||||||
This can be triggered by calling MDNS.announce().
|
This can be triggered by calling MDNS.announce().
|
||||||
|
|
||||||
*/
|
*/
|
||||||
bool MDNSDynamicServiceTxtCallback(MDNSResponder* p_pMDNSResponder,
|
void MDNSDynamicServiceTxtCallback(const MDNSResponder::hMDNSService p_hService) {
|
||||||
const MDNSResponder::hMDNSService p_hService,
|
|
||||||
void* p_pUserdata) {
|
|
||||||
Serial.println("MDNSDynamicServiceTxtCallback");
|
Serial.println("MDNSDynamicServiceTxtCallback");
|
||||||
(void) p_pUserdata;
|
|
||||||
|
|
||||||
if ((p_pMDNSResponder) &&
|
if (hMDNSService == p_hService) {
|
||||||
(hMDNSService == p_hService)) {
|
|
||||||
Serial.printf("Updating curtime TXT item to: %s\n", getTimeString());
|
Serial.printf("Updating curtime TXT item to: %s\n", getTimeString());
|
||||||
p_pMDNSResponder->addDynamicServiceTxt(p_hService, "curtime", getTimeString());
|
MDNS.addDynamicServiceTxt(p_hService, "curtime", getTimeString());
|
||||||
}
|
}
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -160,77 +155,47 @@ bool MDNSDynamicServiceTxtCallback(MDNSResponder* p_pMDNSResponder,
|
|||||||
restarted via p_pMDNSResponder->setHostname().
|
restarted via p_pMDNSResponder->setHostname().
|
||||||
|
|
||||||
*/
|
*/
|
||||||
bool MDNSProbeResultCallback(MDNSResponder* p_pMDNSResponder,
|
void hostProbeResult(String p_pcDomainName, bool p_bProbeResult) {
|
||||||
const char* p_pcDomainName,
|
|
||||||
const MDNSResponder::hMDNSService p_hService,
|
|
||||||
bool p_bProbeResult,
|
|
||||||
void* p_pUserdata) {
|
|
||||||
Serial.println("MDNSProbeResultCallback");
|
Serial.println("MDNSProbeResultCallback");
|
||||||
(void) p_pUserdata;
|
Serial.printf("MDNSProbeResultCallback: Host domain '%s.local' is %s\n", p_pcDomainName.c_str(), (p_bProbeResult ? "free" : "already USED!"));
|
||||||
|
if (true == p_bProbeResult) {
|
||||||
|
// Set station hostname
|
||||||
|
setStationHostname(pcHostDomain);
|
||||||
|
|
||||||
if ((p_pMDNSResponder) &&
|
if (!bHostDomainConfirmed) {
|
||||||
(0 == p_hService)) { // Called for host domain
|
// Hostname free -> setup clock service
|
||||||
Serial.printf("MDNSProbeResultCallback: Host domain '%s.local' is %s\n", p_pcDomainName, (p_bProbeResult ? "free" : "already USED!"));
|
bHostDomainConfirmed = true;
|
||||||
if (true == p_bProbeResult) {
|
|
||||||
// Set station hostname
|
|
||||||
setStationHostname(pcHostDomain);
|
|
||||||
|
|
||||||
if (!bHostDomainConfirmed) {
|
if (!hMDNSService) {
|
||||||
// Hostname free -> setup clock service
|
// Add a 'clock.tcp' service to port 'SERVICE_PORT', using the host domain as instance domain
|
||||||
bHostDomainConfirmed = true;
|
hMDNSService = MDNS.addService(0, "espclk", "tcp", SERVICE_PORT);
|
||||||
|
if (hMDNSService) {
|
||||||
if (!hMDNSService) {
|
// Add a simple static MDNS service TXT item
|
||||||
// Add a 'clock.tcp' service to port 'SERVICE_PORT', using the host domain as instance domain
|
MDNS.addServiceTxt(hMDNSService, "port#", SERVICE_PORT);
|
||||||
hMDNSService = p_pMDNSResponder->addService(0, "espclk", "tcp", SERVICE_PORT);
|
// Set the callback function for dynamic service TXTs
|
||||||
if (hMDNSService) {
|
MDNS.setDynamicServiceTxtCallback(MDNSDynamicServiceTxtCallback);
|
||||||
// Add a simple static MDNS service TXT item
|
|
||||||
p_pMDNSResponder->addServiceTxt(hMDNSService, "port#", SERVICE_PORT);
|
|
||||||
// Set the callback function for dynamic service TXTs
|
|
||||||
p_pMDNSResponder->setDynamicServiceTxtCallback(hMDNSService, MDNSDynamicServiceTxtCallback, 0);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Change hostname, use '-' as divider between base name and index
|
||||||
|
if (MDNSResponder::indexDomain(pcHostDomain, "-", 0)) {
|
||||||
|
MDNS.setHostname(pcHostDomain);
|
||||||
} else {
|
} else {
|
||||||
// Change hostname, use '-' as divider between base name and index
|
Serial.println("MDNSProbeResultCallback: FAILED to update hostname!");
|
||||||
if (MDNSResponder::indexDomain(pcHostDomain, "-", 0)) {
|
|
||||||
p_pMDNSResponder->setHostname(pcHostDomain);
|
|
||||||
} else {
|
|
||||||
Serial.println("MDNSProbeResultCallback: FAILED to update hostname!");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
handleHTTPClient
|
handleHTTPClient
|
||||||
*/
|
*/
|
||||||
void handleHTTPClient(WiFiClient& client) {
|
|
||||||
|
void handleHTTPRequest() {
|
||||||
Serial.println("");
|
Serial.println("");
|
||||||
Serial.println("New client");
|
Serial.println("HTTP Request");
|
||||||
|
|
||||||
// Wait for data from client to become available
|
|
||||||
while (client.connected() && !client.available()) {
|
|
||||||
delay(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read the first line of HTTP request
|
|
||||||
String req = client.readStringUntil('\r');
|
|
||||||
|
|
||||||
// First line of HTTP request looks like "GET /path HTTP/1.1"
|
|
||||||
// Retrieve the "/path" part by finding the spaces
|
|
||||||
int addr_start = req.indexOf(' ');
|
|
||||||
int addr_end = req.indexOf(' ', addr_start + 1);
|
|
||||||
if (addr_start == -1 || addr_end == -1) {
|
|
||||||
Serial.print("Invalid request: ");
|
|
||||||
Serial.println(req);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
req = req.substring(addr_start + 1, addr_end);
|
|
||||||
Serial.print("Request: ");
|
|
||||||
Serial.println(req);
|
|
||||||
client.flush();
|
|
||||||
|
|
||||||
// Get current time
|
// Get current time
|
||||||
time_t now = time(nullptr);;
|
time_t now = time(nullptr);;
|
||||||
@ -238,27 +203,18 @@ void handleHTTPClient(WiFiClient& client) {
|
|||||||
gmtime_r(&now, &timeinfo);
|
gmtime_r(&now, &timeinfo);
|
||||||
|
|
||||||
String s;
|
String s;
|
||||||
if (req == "/") {
|
|
||||||
IPAddress ip = WiFi.localIP();
|
|
||||||
String ipStr = String(ip[0]) + '.' + String(ip[1]) + '.' + String(ip[2]) + '.' + String(ip[3]);
|
|
||||||
s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>Hello from ";
|
|
||||||
s += WiFi.hostname() + " at " + ipStr;
|
|
||||||
// Simple addition of the current time
|
|
||||||
s += "\r\nCurrent time is: ";
|
|
||||||
s += getTimeString();
|
|
||||||
// done :-)
|
|
||||||
s += "</html>\r\n\r\n";
|
|
||||||
Serial.println("Sending 200");
|
|
||||||
} else {
|
|
||||||
s = "HTTP/1.1 404 Not Found\r\n\r\n";
|
|
||||||
Serial.println("Sending 404");
|
|
||||||
}
|
|
||||||
client.print(s);
|
|
||||||
|
|
||||||
Serial.println("Done with client");
|
s = "<!DOCTYPE HTML>\r\n<html>Hello from ";
|
||||||
|
s += WiFi.hostname() + " at " + WiFi.localIP().toString();
|
||||||
|
// Simple addition of the current time
|
||||||
|
s += "\r\nCurrent time is: ";
|
||||||
|
s += getTimeString();
|
||||||
|
// done :-)
|
||||||
|
s += "</html>\r\n\r\n";
|
||||||
|
Serial.println("Sending 200");
|
||||||
|
server.send(200, "text/html", s);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
setup
|
setup
|
||||||
*/
|
*/
|
||||||
@ -285,7 +241,7 @@ void setup(void) {
|
|||||||
setClock();
|
setClock();
|
||||||
|
|
||||||
// Setup MDNS responder
|
// Setup MDNS responder
|
||||||
MDNS.setProbeResultCallback(MDNSProbeResultCallback, 0);
|
MDNS.setHostProbeResultCallback(hostProbeResult);
|
||||||
// Init the (currently empty) host domain string with 'esp8266'
|
// Init the (currently empty) host domain string with 'esp8266'
|
||||||
if ((!MDNSResponder::indexDomain(pcHostDomain, 0, "esp8266")) ||
|
if ((!MDNSResponder::indexDomain(pcHostDomain, 0, "esp8266")) ||
|
||||||
(!MDNS.begin(pcHostDomain))) {
|
(!MDNS.begin(pcHostDomain))) {
|
||||||
@ -296,26 +252,22 @@ void setup(void) {
|
|||||||
}
|
}
|
||||||
Serial.println("MDNS responder started");
|
Serial.println("MDNS responder started");
|
||||||
|
|
||||||
// Start TCP (HTTP) server
|
// Setup HTTP server
|
||||||
|
server.on("/", handleHTTPRequest);
|
||||||
server.begin();
|
server.begin();
|
||||||
Serial.println("TCP server started");
|
Serial.println("HTTP server started");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
loop
|
loop
|
||||||
*/
|
*/
|
||||||
void loop(void) {
|
void loop(void) {
|
||||||
// Check if a client has connected
|
|
||||||
WiFiClient client = server.available();
|
|
||||||
if (client) {
|
|
||||||
handleHTTPClient(client);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// Check if a request has come in
|
||||||
|
server.handleClient();
|
||||||
// Allow MDNS processing
|
// Allow MDNS processing
|
||||||
MDNS.update();
|
MDNS.update();
|
||||||
|
|
||||||
// Update time (if needed)
|
|
||||||
static esp8266::polledTimeout::periodic timeout(UPDATE_CYCLE);
|
static esp8266::polledTimeout::periodic timeout(UPDATE_CYCLE);
|
||||||
if (timeout.expired()) {
|
if (timeout.expired()) {
|
||||||
|
|
||||||
@ -326,5 +278,3 @@ void loop(void) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -82,106 +82,55 @@ bool setStationHostname(const char* p_pcHostname) {
|
|||||||
if (p_pcHostname) {
|
if (p_pcHostname) {
|
||||||
WiFi.hostname(p_pcHostname);
|
WiFi.hostname(p_pcHostname);
|
||||||
Serial.printf("setStationHostname: Station hostname is set to '%s'\n", p_pcHostname);
|
Serial.printf("setStationHostname: Station hostname is set to '%s'\n", p_pcHostname);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
return true;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
MDNSServiceQueryCallback
|
MDNSServiceQueryCallback
|
||||||
*/
|
*/
|
||||||
bool MDNSServiceQueryCallback(MDNSResponder* p_pMDNSResponder, // The MDNS responder object
|
|
||||||
const 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) &&
|
void MDNSServiceQueryCallback(MDNSResponder::MDNSServiceInfo serviceInfo, MDNSResponder::AnswerType answerType, bool p_bSetContent) {
|
||||||
(hMDNSServiceQuery == p_hServiceQuery)) {
|
String answerInfo;
|
||||||
|
switch (answerType) {
|
||||||
if (MDNSResponder::ServiceQueryAnswerType_ServiceDomain & p_u32ServiceQueryAnswerMask) {
|
case MDNSResponder::AnswerType::ServiceDomain :
|
||||||
Serial.printf("MDNSServiceQueryCallback: Service domain '%s' %s index %u\n",
|
answerInfo = "ServiceDomain " + String(serviceInfo.serviceDomain());
|
||||||
p_pMDNSResponder->answerServiceDomain(p_hServiceQuery, p_u32AnswerIndex),
|
break;
|
||||||
(p_bSetContent ? "added at" : "removed from"),
|
case MDNSResponder::AnswerType::HostDomainAndPort :
|
||||||
p_u32AnswerIndex);
|
answerInfo = "HostDomainAndPort " + String(serviceInfo.hostDomain()) + ":" + String(serviceInfo.hostPort());
|
||||||
} else if (MDNSResponder::ServiceQueryAnswerType_HostDomainAndPort & p_u32ServiceQueryAnswerMask) {
|
break;
|
||||||
if (p_bSetContent) {
|
case MDNSResponder::AnswerType::IP4Address :
|
||||||
Serial.printf("MDNSServiceQueryCallback: Host domain and port added/updated for service '%s': %s:%u\n",
|
answerInfo = "IP4Address ";
|
||||||
p_pMDNSResponder->answerServiceDomain(p_hServiceQuery, p_u32AnswerIndex),
|
for (IPAddress ip : serviceInfo.IP4Adresses()) {
|
||||||
p_pMDNSResponder->answerHostDomain(p_hServiceQuery, p_u32AnswerIndex),
|
answerInfo += "- " + ip.toString();
|
||||||
p_pMDNSResponder->answerPort(p_hServiceQuery, p_u32AnswerIndex));
|
};
|
||||||
} else {
|
break;
|
||||||
Serial.printf("MDNSServiceQueryCallback: Host domain and port removed from service '%s'\n",
|
case MDNSResponder::AnswerType::Txt :
|
||||||
p_pMDNSResponder->answerServiceDomain(p_hServiceQuery, p_u32AnswerIndex));
|
answerInfo = "TXT " + String(serviceInfo.strKeyValue());
|
||||||
|
for (auto kv : serviceInfo.keyValues()) {
|
||||||
|
answerInfo += "\nkv : " + String(kv.first) + " : " + String(kv.second);
|
||||||
}
|
}
|
||||||
} else if (MDNSResponder::ServiceQueryAnswerType_IP4Address & p_u32ServiceQueryAnswerMask) {
|
break;
|
||||||
if (p_bSetContent) {
|
default :
|
||||||
Serial.printf("MDNSServiceQueryCallback: IP4 address added/updated for service '%s':\n",
|
answerInfo = "Unknown Answertype";
|
||||||
p_pMDNSResponder->answerServiceDomain(p_hServiceQuery, p_u32AnswerIndex));
|
|
||||||
for (uint32_t u = 0; u < p_pMDNSResponder->answerIP4AddressCount(p_hServiceQuery, p_u32AnswerIndex); ++u) {
|
|
||||||
Serial.printf("- %s\n", p_pMDNSResponder->answerIP4Address(p_hServiceQuery, p_u32AnswerIndex, u).toString().c_str());
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Serial.printf("MDNSServiceQueryCallback: IP4 address removed from service '%s'\n",
|
|
||||||
p_pMDNSResponder->answerServiceDomain(p_hServiceQuery, p_u32AnswerIndex));
|
|
||||||
}
|
|
||||||
} else if (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),
|
|
||||||
p_pMDNSResponder->answerTxts(p_hServiceQuery, p_u32AnswerIndex));
|
|
||||||
} else {
|
|
||||||
Serial.printf("MDNSServiceQueryCallback: TXT items removed from service '%s'\n",
|
|
||||||
p_pMDNSResponder->answerServiceDomain(p_hServiceQuery, p_u32AnswerIndex));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// Create the current list of 'http.tcp' services
|
|
||||||
uint32_t u32Answers = p_pMDNSResponder->answerCount(p_hServiceQuery);
|
|
||||||
if (u32Answers) {
|
|
||||||
strHTTPServices = "";
|
|
||||||
for (uint32_t u = 0; u < u32Answers; ++u) {
|
|
||||||
// Index and service domain
|
|
||||||
strHTTPServices += "<li>";
|
|
||||||
strHTTPServices += p_pMDNSResponder->answerServiceDomain(p_hServiceQuery, u);
|
|
||||||
// Host domain and port
|
|
||||||
if ((p_pMDNSResponder->hasAnswerHostDomain(p_hServiceQuery, u)) &&
|
|
||||||
(p_pMDNSResponder->hasAnswerPort(p_hServiceQuery, u))) {
|
|
||||||
strHTTPServices += "<br/>Hostname: ";
|
|
||||||
strHTTPServices += p_pMDNSResponder->answerHostDomain(p_hServiceQuery, u);
|
|
||||||
strHTTPServices += ":";
|
|
||||||
strHTTPServices += p_pMDNSResponder->answerPort(p_hServiceQuery, u);
|
|
||||||
}
|
|
||||||
// IP4 address
|
|
||||||
if (p_pMDNSResponder->hasAnswerIP4Address(p_hServiceQuery, u)) {
|
|
||||||
strHTTPServices += "<br/>IP4: ";
|
|
||||||
for (uint32_t u2 = 0; u2 < p_pMDNSResponder->answerIP4AddressCount(p_hServiceQuery, u); ++u2) {
|
|
||||||
if (0 != u2) {
|
|
||||||
strHTTPServices += ", ";
|
|
||||||
}
|
|
||||||
strHTTPServices += p_pMDNSResponder->answerIP4Address(p_hServiceQuery, u, u2).toString();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// MDNS TXT items
|
|
||||||
if (p_pMDNSResponder->hasAnswerTxts(p_hServiceQuery, u)) {
|
|
||||||
strHTTPServices += "<br/>TXT: ";
|
|
||||||
strHTTPServices += p_pMDNSResponder->answerTxts(p_hServiceQuery, u);
|
|
||||||
}
|
|
||||||
strHTTPServices += "</li>";
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
strHTTPServices = cstrNoHTTPServices;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return true;
|
Serial.printf("Answer %s %s\n", answerInfo.c_str(), p_bSetContent ? "Modified" : "Deleted");
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
MDNSProbeResultCallback
|
MDNSServiceProbeResultCallback
|
||||||
|
Probe result callback for Services
|
||||||
|
*/
|
||||||
|
|
||||||
|
void serviceProbeResult(String p_pcServiceName,
|
||||||
|
const MDNSResponder::hMDNSService p_hMDNSService,
|
||||||
|
bool p_bProbeResult) {
|
||||||
|
(void) p_hMDNSService;
|
||||||
|
Serial.printf("MDNSServiceProbeResultCallback: Service %s probe %s\n", p_pcServiceName.c_str(), (p_bProbeResult ? "succeeded." : "failed!"));
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
MDNSHostProbeResultCallback
|
||||||
|
|
||||||
Probe result callback for the host domain.
|
Probe result callback for the host domain.
|
||||||
If the domain is free, the host domain is set and the http service is
|
If the domain is free, the host domain is set and the http service is
|
||||||
@ -190,58 +139,51 @@ bool MDNSServiceQueryCallback(MDNSResponder* p_pMDNSResponder,
|
|||||||
restarted via p_pMDNSResponder->setHostname().
|
restarted via p_pMDNSResponder->setHostname().
|
||||||
|
|
||||||
*/
|
*/
|
||||||
bool MDNSProbeResultCallback(MDNSResponder* p_pMDNSResponder,
|
|
||||||
const char* p_pcDomainName,
|
|
||||||
const MDNSResponder::hMDNSService p_hService,
|
|
||||||
bool p_bProbeResult,
|
|
||||||
void* p_pUserdata) {
|
|
||||||
(void) p_pUserdata;
|
|
||||||
|
|
||||||
if ((p_pMDNSResponder) &&
|
void hostProbeResult(String p_pcDomainName, bool p_bProbeResult) {
|
||||||
(0 == p_hService)) { // Called for host domain
|
|
||||||
|
|
||||||
Serial.printf("MDNSProbeResultCallback: Host domain '%s.local' is %s\n", p_pcDomainName, (p_bProbeResult ? "free" : "already USED!"));
|
Serial.printf("MDNSHostProbeResultCallback: Host domain '%s.local' is %s\n", p_pcDomainName.c_str(), (p_bProbeResult ? "free" : "already USED!"));
|
||||||
|
|
||||||
if (true == p_bProbeResult) {
|
if (true == p_bProbeResult) {
|
||||||
// Set station hostname
|
// Set station hostname
|
||||||
setStationHostname(pcHostDomain);
|
setStationHostname(pcHostDomain);
|
||||||
|
|
||||||
if (!bHostDomainConfirmed) {
|
if (!bHostDomainConfirmed) {
|
||||||
// Hostname free -> setup clock service
|
// Hostname free -> setup clock service
|
||||||
bHostDomainConfirmed = true;
|
bHostDomainConfirmed = true;
|
||||||
|
|
||||||
if (!hMDNSService) {
|
if (!hMDNSService) {
|
||||||
// Add a 'http.tcp' service to port 'SERVICE_PORT', using the host domain as instance domain
|
// Add a 'http.tcp' service to port 'SERVICE_PORT', using the host domain as instance domain
|
||||||
hMDNSService = p_pMDNSResponder->addService(0, "http", "tcp", SERVICE_PORT);
|
hMDNSService = MDNS.addService(0, "http", "tcp", SERVICE_PORT);
|
||||||
if (hMDNSService) {
|
if (hMDNSService) {
|
||||||
// Add some '_http._tcp' protocol specific MDNS service TXT items
|
MDNS.setServiceProbeResultCallback(hMDNSService, serviceProbeResult);
|
||||||
// See: http://www.dns-sd.org/txtrecords.html#http
|
|
||||||
p_pMDNSResponder->addServiceTxt(hMDNSService, "user", "");
|
|
||||||
p_pMDNSResponder->addServiceTxt(hMDNSService, "password", "");
|
|
||||||
p_pMDNSResponder->addServiceTxt(hMDNSService, "path", "/");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Install dynamic 'http.tcp' service query
|
// Add some '_http._tcp' protocol specific MDNS service TXT items
|
||||||
if (!hMDNSServiceQuery) {
|
// See: http://www.dns-sd.org/txtrecords.html#http
|
||||||
hMDNSServiceQuery = p_pMDNSResponder->installServiceQuery("http", "tcp", MDNSServiceQueryCallback, 0);
|
MDNS.addServiceTxt(hMDNSService, "user", "");
|
||||||
if (hMDNSServiceQuery) {
|
MDNS.addServiceTxt(hMDNSService, "password", "");
|
||||||
Serial.printf("MDNSProbeResultCallback: Service query for 'http.tcp' services installed.\n");
|
MDNS.addServiceTxt(hMDNSService, "path", "/");
|
||||||
} else {
|
}
|
||||||
Serial.printf("MDNSProbeResultCallback: FAILED to install service query for 'http.tcp' services!\n");
|
|
||||||
}
|
// Install dynamic 'http.tcp' service query
|
||||||
|
if (!hMDNSServiceQuery) {
|
||||||
|
hMDNSServiceQuery = MDNS.installServiceQuery("http", "tcp", MDNSServiceQueryCallback);
|
||||||
|
if (hMDNSServiceQuery) {
|
||||||
|
Serial.printf("MDNSProbeResultCallback: Service query for 'http.tcp' services installed.\n");
|
||||||
|
} else {
|
||||||
|
Serial.printf("MDNSProbeResultCallback: FAILED to install service query for 'http.tcp' services!\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Change hostname, use '-' as divider between base name and index
|
||||||
|
if (MDNSResponder::indexDomain(pcHostDomain, "-", 0)) {
|
||||||
|
MDNS.setHostname(pcHostDomain);
|
||||||
} else {
|
} else {
|
||||||
// Change hostname, use '-' as divider between base name and index
|
Serial.println("MDNSProbeResultCallback: FAILED to update hostname!");
|
||||||
if (MDNSResponder::indexDomain(pcHostDomain, "-", 0)) {
|
|
||||||
p_pMDNSResponder->setHostname(pcHostDomain);
|
|
||||||
} else {
|
|
||||||
Serial.println("MDNSProbeResultCallback: FAILED to update hostname!");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -251,12 +193,36 @@ void handleHTTPRequest() {
|
|||||||
Serial.println("");
|
Serial.println("");
|
||||||
Serial.println("HTTP Request");
|
Serial.println("HTTP Request");
|
||||||
|
|
||||||
|
IPAddress ip = WiFi.localIP();
|
||||||
|
String ipStr = ip.toString();
|
||||||
String s = "<!DOCTYPE HTML>\r\n<html><h3><head>Hello from ";
|
String s = "<!DOCTYPE HTML>\r\n<html><h3><head>Hello from ";
|
||||||
s += WiFi.hostname() + ".local at " + WiFi.localIP().toString() + "</h3></head>";
|
s += WiFi.hostname() + ".local at " + WiFi.localIP().toString() + "</h3></head>";
|
||||||
s += "<br/><h4>Local HTTP services are :</h4><ol>";
|
s += "<br/><h4>Local HTTP services are :</h4>";
|
||||||
s += strHTTPServices;
|
s += "<ol>";
|
||||||
// done :-)
|
for (auto info : MDNS.answerInfo(hMDNSServiceQuery)) {
|
||||||
s += "</ol></html>";
|
s += "<li>";
|
||||||
|
s += info.serviceDomain();
|
||||||
|
if (info.hostDomainAvailable()) {
|
||||||
|
s += "<br/>Hostname: ";
|
||||||
|
s += String(info.hostDomain());
|
||||||
|
s += (info.hostPortAvailable()) ? (":" + String(info.hostPort())) : "";
|
||||||
|
}
|
||||||
|
if (info.IP4AddressAvailable()) {
|
||||||
|
s += "<br/>IP4:";
|
||||||
|
for (auto ip : info.IP4Adresses()) {
|
||||||
|
s += " " + ip.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (info.txtAvailable()) {
|
||||||
|
s += "<br/>TXT:<br/>";
|
||||||
|
for (auto kv : info.keyValues()) {
|
||||||
|
s += "\t" + String(kv.first) + " : " + String(kv.second) + "<br/>";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
s += "</li>";
|
||||||
|
}
|
||||||
|
s += "</ol><br/>";
|
||||||
|
|
||||||
Serial.println("Sending 200");
|
Serial.println("Sending 200");
|
||||||
server.send(200, "text/html", s);
|
server.send(200, "text/html", s);
|
||||||
Serial.println("Done with request");
|
Serial.println("Done with request");
|
||||||
@ -288,12 +254,13 @@ void setup(void) {
|
|||||||
// Setup HTTP server
|
// Setup HTTP server
|
||||||
server.on("/", handleHTTPRequest);
|
server.on("/", handleHTTPRequest);
|
||||||
|
|
||||||
// Setup MDNS responder
|
// Setup MDNS responders
|
||||||
MDNS.setProbeResultCallback(MDNSProbeResultCallback, 0);
|
MDNS.setHostProbeResultCallback(hostProbeResult);
|
||||||
|
|
||||||
// Init the (currently empty) host domain string with 'esp8266'
|
// Init the (currently empty) host domain string with 'esp8266'
|
||||||
if ((!MDNSResponder::indexDomain(pcHostDomain, 0, "esp8266")) ||
|
if ((!MDNSResponder::indexDomain(pcHostDomain, 0, "esp8266")) ||
|
||||||
(!MDNS.begin(pcHostDomain))) {
|
(!MDNS.begin(pcHostDomain))) {
|
||||||
Serial.println("Error setting up MDNS responder!");
|
Serial.println(" Error setting up MDNS responder!");
|
||||||
while (1) { // STOP
|
while (1) { // STOP
|
||||||
delay(1000);
|
delay(1000);
|
||||||
}
|
}
|
||||||
@ -305,13 +272,12 @@ void setup(void) {
|
|||||||
Serial.println("HTTP server started");
|
Serial.println("HTTP server started");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
loop
|
|
||||||
*/
|
|
||||||
void loop(void) {
|
void loop(void) {
|
||||||
// Check if a request has come in
|
// Check if a request has come in
|
||||||
server.handleClient();
|
server.handleClient();
|
||||||
// Allow MDNS processing
|
// Allow MDNS processing
|
||||||
MDNS.update();
|
MDNS.update();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -58,7 +58,6 @@ MDNSResponder::MDNSResponder(void)
|
|||||||
m_pcHostname(0),
|
m_pcHostname(0),
|
||||||
m_pServiceQueries(0),
|
m_pServiceQueries(0),
|
||||||
m_fnServiceTxtCallback(0),
|
m_fnServiceTxtCallback(0),
|
||||||
m_pServiceTxtCallbackUserdata(0),
|
|
||||||
#ifdef ENABLE_ESP_MDNS_RESPONDER_PASSIV_MODE
|
#ifdef ENABLE_ESP_MDNS_RESPONDER_PASSIV_MODE
|
||||||
m_bPassivModeEnabled(true) {
|
m_bPassivModeEnabled(true) {
|
||||||
#else
|
#else
|
||||||
@ -138,7 +137,10 @@ bool MDNSResponder::begin(const char* p_pcHostname,
|
|||||||
*/
|
*/
|
||||||
bool MDNSResponder::close(void) {
|
bool MDNSResponder::close(void) {
|
||||||
|
|
||||||
_announce(false, true);
|
m_GotIPHandler.reset(); // reset WiFi event callbacks.
|
||||||
|
m_DisconnectedHandler.reset();
|
||||||
|
|
||||||
|
_announce(false, true);
|
||||||
_resetProbeStatus(false); // Stop probing
|
_resetProbeStatus(false); // Stop probing
|
||||||
|
|
||||||
_releaseServiceQueries();
|
_releaseServiceQueries();
|
||||||
@ -148,6 +150,18 @@ bool MDNSResponder::close(void) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* MDNSResponder::end
|
||||||
|
*
|
||||||
|
* Ends the MDNS responder.
|
||||||
|
* for compatibility with esp32
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
bool MDNSResponder::end(void) {
|
||||||
|
return close();
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* MDNSResponder::setHostname
|
* MDNSResponder::setHostname
|
||||||
*
|
*
|
||||||
@ -464,11 +478,9 @@ bool MDNSResponder::addServiceTxt(String p_strService,
|
|||||||
* service TXT items are needed.
|
* service TXT items are needed.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
bool MDNSResponder::setDynamicServiceTxtCallback(MDNSResponder::MDNSDynamicServiceTxtCallbackFn p_fnCallback,
|
bool MDNSResponder::setDynamicServiceTxtCallback(MDNSResponder::MDNSDynamicServiceTxtCallbackFunc p_fnCallback) {
|
||||||
void* p_pUserdata) {
|
|
||||||
|
|
||||||
m_fnServiceTxtCallback = p_fnCallback;
|
m_fnServiceTxtCallback = p_fnCallback;
|
||||||
m_pServiceTxtCallbackUserdata = p_pUserdata;
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -481,15 +493,13 @@ bool MDNSResponder::setDynamicServiceTxtCallback(MDNSResponder::MDNSDynamicServi
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
bool MDNSResponder::setDynamicServiceTxtCallback(MDNSResponder::hMDNSService p_hService,
|
bool MDNSResponder::setDynamicServiceTxtCallback(MDNSResponder::hMDNSService p_hService,
|
||||||
MDNSResponder::MDNSDynamicServiceTxtCallbackFn p_fnCallback,
|
MDNSResponder::MDNSDynamicServiceTxtCallbackFunc p_fnCallback) {
|
||||||
void* p_pUserdata) {
|
|
||||||
|
|
||||||
bool bResult = false;
|
bool bResult = false;
|
||||||
|
|
||||||
stcMDNSService* pService = _findService(p_hService);
|
stcMDNSService* pService = _findService(p_hService);
|
||||||
if (pService) {
|
if (pService) {
|
||||||
pService->m_fnTxtCallback = p_fnCallback;
|
pService->m_fnTxtCallback = p_fnCallback;
|
||||||
pService->m_pTxtCallbackUserdata = p_pUserdata;
|
|
||||||
|
|
||||||
bResult = true;
|
bResult = true;
|
||||||
}
|
}
|
||||||
@ -766,8 +776,7 @@ uint16_t MDNSResponder::port(const uint32_t p_u32AnswerIndex) {
|
|||||||
*/
|
*/
|
||||||
MDNSResponder::hMDNSServiceQuery MDNSResponder::installServiceQuery(const char* p_pcService,
|
MDNSResponder::hMDNSServiceQuery MDNSResponder::installServiceQuery(const char* p_pcService,
|
||||||
const char* p_pcProtocol,
|
const char* p_pcProtocol,
|
||||||
MDNSResponder::MDNSServiceQueryCallbackFn p_fnCallback,
|
MDNSResponder::MDNSServiceQueryCallbackFunc p_fnCallback) {
|
||||||
void* p_pUserdata) {
|
|
||||||
hMDNSServiceQuery hResult = 0;
|
hMDNSServiceQuery hResult = 0;
|
||||||
|
|
||||||
stcMDNSServiceQuery* pServiceQuery = 0;
|
stcMDNSServiceQuery* pServiceQuery = 0;
|
||||||
@ -780,7 +789,6 @@ MDNSResponder::hMDNSServiceQuery MDNSResponder::installServiceQuery(const char*
|
|||||||
(_buildDomainForService(p_pcService, p_pcProtocol, pServiceQuery->m_ServiceTypeDomain))) {
|
(_buildDomainForService(p_pcService, p_pcProtocol, pServiceQuery->m_ServiceTypeDomain))) {
|
||||||
|
|
||||||
pServiceQuery->m_fnCallback = p_fnCallback;
|
pServiceQuery->m_fnCallback = p_fnCallback;
|
||||||
pServiceQuery->m_pUserdata = p_pUserdata;
|
|
||||||
pServiceQuery->m_bLegacyQuery = false;
|
pServiceQuery->m_bLegacyQuery = false;
|
||||||
|
|
||||||
if (_sendMDNSServiceQuery(*pServiceQuery)) {
|
if (_sendMDNSServiceQuery(*pServiceQuery)) {
|
||||||
@ -822,6 +830,15 @@ uint32_t MDNSResponder::answerCount(const MDNSResponder::hMDNSServiceQuery p_hSe
|
|||||||
return (pServiceQuery ? pServiceQuery->answerCount() : 0);
|
return (pServiceQuery ? pServiceQuery->answerCount() : 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<MDNSResponder::MDNSServiceInfo> MDNSResponder::answerInfo (const MDNSResponder::hMDNSServiceQuery p_hServiceQuery) {
|
||||||
|
std::vector<MDNSResponder::MDNSServiceInfo> tempVector;
|
||||||
|
for (uint32_t i=0;i<answerCount(p_hServiceQuery);i++)
|
||||||
|
{
|
||||||
|
tempVector.emplace_back(*this,p_hServiceQuery,i);
|
||||||
|
}
|
||||||
|
return tempVector;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* MDNSResponder::answerServiceDomain
|
* MDNSResponder::answerServiceDomain
|
||||||
*
|
*
|
||||||
@ -1020,7 +1037,6 @@ const char* MDNSResponder::answerTxts(const MDNSResponder::hMDNSServiceQuery p_h
|
|||||||
return (pSQAnswer ? pSQAnswer->m_pcTxts : 0);
|
return (pSQAnswer ? pSQAnswer->m_pcTxts : 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* PROBING
|
* PROBING
|
||||||
*/
|
*/
|
||||||
@ -1035,15 +1051,18 @@ const char* MDNSResponder::answerTxts(const MDNSResponder::hMDNSServiceQuery p_h
|
|||||||
* When succeeded, the host or service domain will be announced by the MDNS responder.
|
* When succeeded, the host or service domain will be announced by the MDNS responder.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
bool MDNSResponder::setProbeResultCallback(MDNSResponder::MDNSProbeResultCallbackFn p_fnCallback,
|
bool MDNSResponder::setHostProbeResultCallback(MDNSResponder::MDNSHostProbeFn p_fnCallback) {
|
||||||
void* p_pUserdata) {
|
|
||||||
|
|
||||||
m_HostProbeInformation.m_fnProbeResultCallback = p_fnCallback;
|
m_HostProbeInformation.m_fnHostProbeResultCallback = p_fnCallback;
|
||||||
m_HostProbeInformation.m_pProbeResultCallbackUserdata = p_pUserdata;
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool MDNSResponder::setHostProbeResultCallback(MDNSHostProbeFn1 pfn) {
|
||||||
|
using namespace std::placeholders;
|
||||||
|
return setHostProbeResultCallback(std::bind(pfn, *this, _1, _2));
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* MDNSResponder::setServiceProbeResultCallback
|
* MDNSResponder::setServiceProbeResultCallback
|
||||||
*
|
*
|
||||||
@ -1054,20 +1073,25 @@ bool MDNSResponder::setProbeResultCallback(MDNSResponder::MDNSProbeResultCallbac
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
bool MDNSResponder::setServiceProbeResultCallback(const MDNSResponder::hMDNSService p_hService,
|
bool MDNSResponder::setServiceProbeResultCallback(const MDNSResponder::hMDNSService p_hService,
|
||||||
MDNSResponder::MDNSProbeResultCallbackFn p_fnCallback,
|
MDNSResponder::MDNSServiceProbeFn p_fnCallback) {
|
||||||
void* p_pUserdata) {
|
|
||||||
bool bResult = false;
|
bool bResult = false;
|
||||||
|
|
||||||
stcMDNSService* pService = _findService(p_hService);
|
stcMDNSService* pService = _findService(p_hService);
|
||||||
if (pService) {
|
if (pService) {
|
||||||
pService->m_ProbeInformation.m_fnProbeResultCallback = p_fnCallback;
|
pService->m_ProbeInformation.m_fnServiceProbeResultCallback = p_fnCallback;
|
||||||
pService->m_ProbeInformation.m_pProbeResultCallbackUserdata = p_pUserdata;
|
|
||||||
|
|
||||||
bResult = true;
|
bResult = true;
|
||||||
}
|
}
|
||||||
return bResult;
|
return bResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool MDNSResponder::setServiceProbeResultCallback(const MDNSResponder::hMDNSService p_hService,
|
||||||
|
MDNSResponder::MDNSServiceProbeFn1 p_fnCallback) {
|
||||||
|
using namespace std::placeholders;
|
||||||
|
return setServiceProbeResultCallback(p_hService, std::bind(p_fnCallback, *this, _1, _2, _3));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* MISC
|
* MISC
|
||||||
|
@ -109,6 +109,8 @@
|
|||||||
#include "include/UdpContext.h"
|
#include "include/UdpContext.h"
|
||||||
#include <limits>
|
#include <limits>
|
||||||
#include <PolledTimeout.h>
|
#include <PolledTimeout.h>
|
||||||
|
#include <map>
|
||||||
|
|
||||||
|
|
||||||
#include "ESP8266WiFi.h"
|
#include "ESP8266WiFi.h"
|
||||||
|
|
||||||
@ -186,7 +188,8 @@ public:
|
|||||||
}
|
}
|
||||||
// Finish MDNS processing
|
// Finish MDNS processing
|
||||||
bool close(void);
|
bool close(void);
|
||||||
|
// for esp32 compatability
|
||||||
|
bool end(void);
|
||||||
// Change hostname (probing is restarted)
|
// Change hostname (probing is restarted)
|
||||||
bool setHostname(const char* p_pcHostname);
|
bool setHostname(const char* p_pcHostname);
|
||||||
// for compatibility...
|
// for compatibility...
|
||||||
@ -223,6 +226,8 @@ public:
|
|||||||
//Warning: this has the side effect of changing the hostname.
|
//Warning: this has the side effect of changing the hostname.
|
||||||
//TODO: implement instancename different from hostname
|
//TODO: implement instancename different from hostname
|
||||||
void setInstanceName(const char* p_pcHostname) {setHostname(p_pcHostname);}
|
void setInstanceName(const char* p_pcHostname) {setHostname(p_pcHostname);}
|
||||||
|
// for esp32 compatibilty
|
||||||
|
void setInstanceName(const String& s_pcHostname) {setInstanceName(s_pcHostname.c_str());}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* hMDNSTxt (opaque handle to access the TXT items)
|
* hMDNSTxt (opaque handle to access the TXT items)
|
||||||
@ -275,19 +280,16 @@ public:
|
|||||||
* MDNSDynamicServiceTxtCallbackFn
|
* MDNSDynamicServiceTxtCallbackFn
|
||||||
* Callback function for dynamic MDNS TXT items
|
* Callback function for dynamic MDNS TXT items
|
||||||
*/
|
*/
|
||||||
typedef bool (*MDNSDynamicServiceTxtCallbackFn)(MDNSResponder* p_pMDNSResponder,
|
|
||||||
const hMDNSService p_hService,
|
typedef std::function<void(const hMDNSService p_hService)> MDNSDynamicServiceTxtCallbackFunc;
|
||||||
void* p_pUserdata);
|
|
||||||
|
|
||||||
// Set a global callback for dynamic MDNS TXT items. The callback function is called
|
// Set a global callback for dynamic MDNS TXT items. The callback function is called
|
||||||
// every time, a TXT item is needed for one of the installed services.
|
// every time, a TXT item is needed for one of the installed services.
|
||||||
bool setDynamicServiceTxtCallback(MDNSDynamicServiceTxtCallbackFn p_fnCallback,
|
bool setDynamicServiceTxtCallback(MDNSDynamicServiceTxtCallbackFunc p_fnCallback);
|
||||||
void* p_pUserdata);
|
|
||||||
// Set a service specific callback for dynamic MDNS TXT items. The callback function
|
// Set a service specific callback for dynamic MDNS TXT items. The callback function
|
||||||
// is called every time, a TXT item is needed for the given service.
|
// is called every time, a TXT item is needed for the given service.
|
||||||
bool setDynamicServiceTxtCallback(const hMDNSService p_hService,
|
bool setDynamicServiceTxtCallback(const hMDNSService p_hService,
|
||||||
MDNSDynamicServiceTxtCallbackFn p_fnCallback,
|
MDNSDynamicServiceTxtCallbackFunc p_fnCallback);
|
||||||
void* p_pUserdata);
|
|
||||||
|
|
||||||
// Add a (dynamic) MDNS TXT item ('key' = 'value') to the service
|
// Add a (dynamic) MDNS TXT item ('key' = 'value') to the service
|
||||||
// Dynamic TXT items are removed right after one-time use. So they need to be added
|
// Dynamic TXT items are removed right after one-time use. So they need to be added
|
||||||
@ -355,16 +357,28 @@ public:
|
|||||||
#endif
|
#endif
|
||||||
} enuServiceQueryAnswerType;
|
} enuServiceQueryAnswerType;
|
||||||
|
|
||||||
|
enum class AnswerType : uint32_t {
|
||||||
|
Unknown = 0,
|
||||||
|
ServiceDomain = ServiceQueryAnswerType_ServiceDomain,
|
||||||
|
HostDomainAndPort = ServiceQueryAnswerType_HostDomainAndPort,
|
||||||
|
Txt = ServiceQueryAnswerType_Txts,
|
||||||
|
#ifdef MDNS_IP4_SUPPORT
|
||||||
|
IP4Address = ServiceQueryAnswerType_IP4Address,
|
||||||
|
#endif
|
||||||
|
#ifdef MDNS_IP6_SUPPORT
|
||||||
|
IP6Address = ServiceQueryAnswerType_IP6Address,
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* MDNSServiceQueryCallbackFn
|
* MDNSServiceQueryCallbackFn
|
||||||
* Callback function for received answers for dynamic service queries
|
* Callback function for received answers for dynamic service queries
|
||||||
*/
|
*/
|
||||||
typedef bool (*MDNSServiceQueryCallbackFn)(MDNSResponder* p_pMDNSResponder,
|
struct MDNSServiceInfo; // forward declaration
|
||||||
const hMDNSServiceQuery p_hServiceQuery, // dynamic service query handle
|
typedef std::function<void(const MDNSServiceInfo& mdnsServiceInfo,
|
||||||
uint32_t p_u32AnswerIndex, // index of the updated answer
|
AnswerType answerType , // flag for the updated answer item
|
||||||
uint32_t p_u32ServiceQueryAnswerMask, // flag for the updated answer item
|
bool p_bSetContent // true: Answer component set, false: component deleted
|
||||||
bool p_bSetContent, // true: Answer component set, false: component deleted
|
)> MDNSServiceQueryCallbackFunc;
|
||||||
void* p_pUserdata); // pUserdata set via 'installServiceQuery'
|
|
||||||
|
|
||||||
// Install a dynamic service query. For every received answer (part) the given callback
|
// Install a dynamic service query. For every received answer (part) the given callback
|
||||||
// function is called. The query will be updated every time, the TTL for an answer
|
// function is called. The query will be updated every time, the TTL for an answer
|
||||||
@ -379,12 +393,13 @@ public:
|
|||||||
// - hasAnswerTxts/answerTxts
|
// - hasAnswerTxts/answerTxts
|
||||||
hMDNSServiceQuery installServiceQuery(const char* p_pcService,
|
hMDNSServiceQuery installServiceQuery(const char* p_pcService,
|
||||||
const char* p_pcProtocol,
|
const char* p_pcProtocol,
|
||||||
MDNSServiceQueryCallbackFn p_fnCallback,
|
MDNSServiceQueryCallbackFunc p_fnCallback);
|
||||||
void* p_pUserdata);
|
|
||||||
// Remove a dynamic service query
|
// Remove a dynamic service query
|
||||||
bool removeServiceQuery(hMDNSServiceQuery p_hServiceQuery);
|
bool removeServiceQuery(hMDNSServiceQuery p_hServiceQuery);
|
||||||
|
|
||||||
uint32_t answerCount(const hMDNSServiceQuery p_hServiceQuery);
|
uint32_t answerCount(const hMDNSServiceQuery p_hServiceQuery);
|
||||||
|
std::vector<MDNSResponder::MDNSServiceInfo> answerInfo (const MDNSResponder::hMDNSServiceQuery p_hServiceQuery);
|
||||||
|
|
||||||
const char* answerServiceDomain(const hMDNSServiceQuery p_hServiceQuery,
|
const char* answerServiceDomain(const hMDNSServiceQuery p_hServiceQuery,
|
||||||
const uint32_t p_u32AnswerIndex);
|
const uint32_t p_u32AnswerIndex);
|
||||||
bool hasAnswerHostDomain(const hMDNSServiceQuery p_hServiceQuery,
|
bool hasAnswerHostDomain(const hMDNSServiceQuery p_hServiceQuery,
|
||||||
@ -422,24 +437,36 @@ public:
|
|||||||
/**
|
/**
|
||||||
* MDNSProbeResultCallbackFn
|
* MDNSProbeResultCallbackFn
|
||||||
* Callback function for (host and service domain) probe results
|
* Callback function for (host and service domain) probe results
|
||||||
*/
|
*/
|
||||||
typedef bool (*MDNSProbeResultCallbackFn)(MDNSResponder* p_pMDNSResponder,
|
typedef std::function<void(const char* p_pcDomainName,
|
||||||
const char* p_pcDomainName,
|
bool p_bProbeResult)> MDNSHostProbeFn;
|
||||||
const hMDNSService p_hMDNSService, // 0 for host domain
|
|
||||||
bool p_bProbeResult,
|
typedef std::function<void(MDNSResponder& resp,
|
||||||
void* p_pUserdata);
|
const char* p_pcDomainName,
|
||||||
|
bool p_bProbeResult)> MDNSHostProbeFn1;
|
||||||
|
|
||||||
|
typedef std::function<void(const char* p_pcServiceName,
|
||||||
|
const hMDNSService p_hMDNSService,
|
||||||
|
bool p_bProbeResult)> MDNSServiceProbeFn;
|
||||||
|
|
||||||
|
typedef std::function<void(MDNSResponder& resp,
|
||||||
|
const char* p_pcServiceName,
|
||||||
|
const hMDNSService p_hMDNSService,
|
||||||
|
bool p_bProbeResult)> MDNSServiceProbeFn1;
|
||||||
|
|
||||||
// Set a global callback function for host and service probe results
|
// Set a global callback function for host and service probe results
|
||||||
// The callback function is called, when the probeing for the host domain
|
// The callback function is called, when the probing for the host domain
|
||||||
// (or a service domain, which hasn't got a service specific callback)
|
// (or a service domain, which hasn't got a service specific callback)
|
||||||
// Succeededs or fails.
|
// Succeeds or fails.
|
||||||
// In case of failure, the failed domain name should be changed.
|
// In case of failure, the failed domain name should be changed.
|
||||||
bool setProbeResultCallback(MDNSProbeResultCallbackFn p_fnCallback,
|
bool setHostProbeResultCallback(MDNSHostProbeFn p_fnCallback);
|
||||||
void* p_pUserdata);
|
bool setHostProbeResultCallback(MDNSHostProbeFn1 p_fnCallback);
|
||||||
// Set a service specific probe result callcack
|
|
||||||
bool setServiceProbeResultCallback(const hMDNSService p_hService,
|
// Set a service specific probe result callback
|
||||||
MDNSProbeResultCallbackFn p_fnCallback,
|
bool setServiceProbeResultCallback(const MDNSResponder::hMDNSService p_hService,
|
||||||
void* p_pUserdata);
|
MDNSServiceProbeFn p_fnCallback);
|
||||||
|
bool setServiceProbeResultCallback(const MDNSResponder::hMDNSService p_hService,
|
||||||
|
MDNSServiceProbeFn1 p_fnCallback);
|
||||||
|
|
||||||
// Application should call this whenever AP is configured/disabled
|
// Application should call this whenever AP is configured/disabled
|
||||||
bool notifyAPChange(void);
|
bool notifyAPChange(void);
|
||||||
@ -462,6 +489,98 @@ public:
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
/** STRUCTS **/
|
/** STRUCTS **/
|
||||||
|
/**
|
||||||
|
* MDNSServiceInfo, used in application callbacks
|
||||||
|
*/
|
||||||
|
public:
|
||||||
|
struct MDNSServiceInfo
|
||||||
|
{
|
||||||
|
MDNSServiceInfo(MDNSResponder& p_pM,MDNSResponder::hMDNSServiceQuery p_hS,uint32_t p_u32A)
|
||||||
|
: p_pMDNSResponder(p_pM),
|
||||||
|
p_hServiceQuery(p_hS),
|
||||||
|
p_u32AnswerIndex(p_u32A)
|
||||||
|
{};
|
||||||
|
struct CompareKey
|
||||||
|
{
|
||||||
|
bool operator()(char const *a, char const *b) const
|
||||||
|
{
|
||||||
|
return strcmp(a, b) < 0;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
using KeyValueMap = std::map<const char*, const char*, CompareKey>;
|
||||||
|
protected:
|
||||||
|
MDNSResponder& p_pMDNSResponder;
|
||||||
|
MDNSResponder::hMDNSServiceQuery p_hServiceQuery;
|
||||||
|
uint32_t p_u32AnswerIndex;
|
||||||
|
KeyValueMap keyValueMap;
|
||||||
|
public:
|
||||||
|
const char* serviceDomain(){
|
||||||
|
return p_pMDNSResponder.answerServiceDomain(p_hServiceQuery, p_u32AnswerIndex);
|
||||||
|
};
|
||||||
|
bool hostDomainAvailable()
|
||||||
|
{
|
||||||
|
return (p_pMDNSResponder.hasAnswerHostDomain(p_hServiceQuery, p_u32AnswerIndex));
|
||||||
|
}
|
||||||
|
const char* hostDomain(){
|
||||||
|
return (hostDomainAvailable()) ?
|
||||||
|
p_pMDNSResponder.answerHostDomain(p_hServiceQuery, p_u32AnswerIndex) : nullptr;
|
||||||
|
};
|
||||||
|
bool hostPortAvailable()
|
||||||
|
{
|
||||||
|
return (p_pMDNSResponder.hasAnswerPort(p_hServiceQuery, p_u32AnswerIndex));
|
||||||
|
}
|
||||||
|
uint16_t hostPort(){
|
||||||
|
return (hostPortAvailable()) ?
|
||||||
|
p_pMDNSResponder.answerPort(p_hServiceQuery, p_u32AnswerIndex) : 0;
|
||||||
|
};
|
||||||
|
bool IP4AddressAvailable()
|
||||||
|
{
|
||||||
|
return (p_pMDNSResponder.hasAnswerIP4Address(p_hServiceQuery,p_u32AnswerIndex ));
|
||||||
|
}
|
||||||
|
std::vector<IPAddress> IP4Adresses(){
|
||||||
|
std::vector<IPAddress> internalIP;
|
||||||
|
if (IP4AddressAvailable()) {
|
||||||
|
uint16_t cntIP4Adress = p_pMDNSResponder.answerIP4AddressCount(p_hServiceQuery, p_u32AnswerIndex);
|
||||||
|
for (uint32_t u2 = 0; u2 < cntIP4Adress; ++u2) {
|
||||||
|
internalIP.emplace_back(p_pMDNSResponder.answerIP4Address(p_hServiceQuery, p_u32AnswerIndex, u2));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return internalIP;
|
||||||
|
};
|
||||||
|
bool txtAvailable()
|
||||||
|
{
|
||||||
|
return (p_pMDNSResponder.hasAnswerTxts(p_hServiceQuery, p_u32AnswerIndex));
|
||||||
|
}
|
||||||
|
const char* strKeyValue (){
|
||||||
|
return (txtAvailable()) ?
|
||||||
|
p_pMDNSResponder.answerTxts(p_hServiceQuery, p_u32AnswerIndex) : nullptr;
|
||||||
|
};
|
||||||
|
const KeyValueMap& keyValues()
|
||||||
|
{
|
||||||
|
if (txtAvailable() && keyValueMap.size() == 0)
|
||||||
|
{
|
||||||
|
for (auto kv = p_pMDNSResponder._answerKeyValue(p_hServiceQuery, p_u32AnswerIndex);kv != nullptr;kv = kv->m_pNext) {
|
||||||
|
keyValueMap.emplace(std::pair<const char*,const char*>(kv->m_pcKey,kv->m_pcValue));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return keyValueMap;
|
||||||
|
}
|
||||||
|
const char* value(const char* key)
|
||||||
|
{
|
||||||
|
char* result = nullptr;
|
||||||
|
|
||||||
|
for (stcMDNSServiceTxt* pTxt=p_pMDNSResponder._answerKeyValue(p_hServiceQuery, p_u32AnswerIndex); pTxt; pTxt=pTxt->m_pNext) {
|
||||||
|
if ((key) &&
|
||||||
|
(0 == strcmp(pTxt->m_pcKey, key))) {
|
||||||
|
result = pTxt->m_pcValue;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
protected:
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* stcMDNSServiceTxt
|
* stcMDNSServiceTxt
|
||||||
*/
|
*/
|
||||||
@ -792,8 +911,8 @@ protected:
|
|||||||
//clsMDNSTimeFlag m_TimeFlag; // Used for probes and announcements
|
//clsMDNSTimeFlag m_TimeFlag; // Used for probes and announcements
|
||||||
bool m_bConflict;
|
bool m_bConflict;
|
||||||
bool m_bTiebreakNeeded;
|
bool m_bTiebreakNeeded;
|
||||||
MDNSProbeResultCallbackFn m_fnProbeResultCallback;
|
MDNSHostProbeFn m_fnHostProbeResultCallback;
|
||||||
void* m_pProbeResultCallbackUserdata;
|
MDNSServiceProbeFn m_fnServiceProbeResultCallback;
|
||||||
|
|
||||||
stcProbeInformation(void);
|
stcProbeInformation(void);
|
||||||
|
|
||||||
@ -813,8 +932,7 @@ protected:
|
|||||||
uint16_t m_u16Port;
|
uint16_t m_u16Port;
|
||||||
uint8_t m_u8ReplyMask;
|
uint8_t m_u8ReplyMask;
|
||||||
stcMDNSServiceTxts m_Txts;
|
stcMDNSServiceTxts m_Txts;
|
||||||
MDNSDynamicServiceTxtCallbackFn m_fnTxtCallback;
|
MDNSDynamicServiceTxtCallbackFunc m_fnTxtCallback;
|
||||||
void* m_pTxtCallbackUserdata;
|
|
||||||
stcProbeInformation m_ProbeInformation;
|
stcProbeInformation m_ProbeInformation;
|
||||||
|
|
||||||
stcMDNSService(const char* p_pcName = 0,
|
stcMDNSService(const char* p_pcName = 0,
|
||||||
@ -957,8 +1075,7 @@ protected:
|
|||||||
|
|
||||||
stcMDNSServiceQuery* m_pNext;
|
stcMDNSServiceQuery* m_pNext;
|
||||||
stcMDNS_RRDomain m_ServiceTypeDomain; // eg. _http._tcp.local
|
stcMDNS_RRDomain m_ServiceTypeDomain; // eg. _http._tcp.local
|
||||||
MDNSServiceQueryCallbackFn m_fnCallback;
|
MDNSServiceQueryCallbackFunc m_fnCallback;
|
||||||
void* m_pUserdata;
|
|
||||||
bool m_bLegacyQuery;
|
bool m_bLegacyQuery;
|
||||||
uint8_t m_u8SentCount;
|
uint8_t m_u8SentCount;
|
||||||
esp8266::polledTimeout::oneShot m_ResendTimeout;
|
esp8266::polledTimeout::oneShot m_ResendTimeout;
|
||||||
@ -1035,8 +1152,7 @@ protected:
|
|||||||
stcMDNSServiceQuery* m_pServiceQueries;
|
stcMDNSServiceQuery* m_pServiceQueries;
|
||||||
WiFiEventHandler m_DisconnectedHandler;
|
WiFiEventHandler m_DisconnectedHandler;
|
||||||
WiFiEventHandler m_GotIPHandler;
|
WiFiEventHandler m_GotIPHandler;
|
||||||
MDNSDynamicServiceTxtCallbackFn m_fnServiceTxtCallback;
|
MDNSDynamicServiceTxtCallbackFunc m_fnServiceTxtCallback;
|
||||||
void* m_pServiceTxtCallbackUserdata;
|
|
||||||
bool m_bPassivModeEnabled;
|
bool m_bPassivModeEnabled;
|
||||||
stcProbeInformation m_HostProbeInformation;
|
stcProbeInformation m_HostProbeInformation;
|
||||||
|
|
||||||
@ -1271,6 +1387,9 @@ protected:
|
|||||||
const char* p_pcValue,
|
const char* p_pcValue,
|
||||||
bool p_bTemp);
|
bool p_bTemp);
|
||||||
|
|
||||||
|
stcMDNSServiceTxt* _answerKeyValue(const hMDNSServiceQuery p_hServiceQuery,
|
||||||
|
const uint32_t p_u32AnswerIndex);
|
||||||
|
|
||||||
bool _collectServiceTxts(stcMDNSService& p_rService);
|
bool _collectServiceTxts(stcMDNSService& p_rService);
|
||||||
bool _releaseTempServiceTxts(stcMDNSService& p_rService);
|
bool _releaseTempServiceTxts(stcMDNSService& p_rService);
|
||||||
const stcMDNSServiceTxt* _serviceTxts(const char* p_pcName,
|
const stcMDNSServiceTxt* _serviceTxts(const char* p_pcName,
|
||||||
|
@ -776,10 +776,10 @@ bool MDNSResponder::_processPTRAnswer(const MDNSResponder::stcMDNS_RRAnswerPTR*
|
|||||||
pSQAnswer->releaseServiceDomain();
|
pSQAnswer->releaseServiceDomain();
|
||||||
|
|
||||||
bResult = pServiceQuery->addAnswer(pSQAnswer);
|
bResult = pServiceQuery->addAnswer(pSQAnswer);
|
||||||
|
|
||||||
p_rbFoundNewKeyAnswer = true;
|
p_rbFoundNewKeyAnswer = true;
|
||||||
if (pServiceQuery->m_fnCallback) {
|
if (pServiceQuery->m_fnCallback) {
|
||||||
pServiceQuery->m_fnCallback(this, (hMDNSServiceQuery)pServiceQuery, pServiceQuery->indexOfAnswer(pSQAnswer), ServiceQueryAnswerType_ServiceDomain, true, pServiceQuery->m_pUserdata);
|
MDNSServiceInfo serviceInfo(*this,(hMDNSServiceQuery)pServiceQuery, pServiceQuery->indexOfAnswer(pSQAnswer));
|
||||||
|
pServiceQuery->m_fnCallback(serviceInfo, static_cast<AnswerType>(ServiceQueryAnswerType_ServiceDomain), true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -823,7 +823,8 @@ bool MDNSResponder::_processSRVAnswer(const MDNSResponder::stcMDNS_RRAnswerSRV*
|
|||||||
|
|
||||||
p_rbFoundNewKeyAnswer = true;
|
p_rbFoundNewKeyAnswer = true;
|
||||||
if (pServiceQuery->m_fnCallback) {
|
if (pServiceQuery->m_fnCallback) {
|
||||||
pServiceQuery->m_fnCallback(this, (hMDNSServiceQuery)pServiceQuery, pServiceQuery->indexOfAnswer(pSQAnswer), ServiceQueryAnswerType_HostDomainAndPort, true, pServiceQuery->m_pUserdata);
|
MDNSServiceInfo serviceInfo(*this, (hMDNSServiceQuery)pServiceQuery, pServiceQuery->indexOfAnswer(pSQAnswer));
|
||||||
|
pServiceQuery->m_fnCallback(serviceInfo, static_cast<AnswerType>(ServiceQueryAnswerType_HostDomainAndPort), true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -870,7 +871,8 @@ bool MDNSResponder::_processTXTAnswer(const MDNSResponder::stcMDNS_RRAnswerTXT*
|
|||||||
pSQAnswer->releaseTxts();
|
pSQAnswer->releaseTxts();
|
||||||
|
|
||||||
if (pServiceQuery->m_fnCallback) {
|
if (pServiceQuery->m_fnCallback) {
|
||||||
pServiceQuery->m_fnCallback(this, (hMDNSServiceQuery)pServiceQuery, pServiceQuery->indexOfAnswer(pSQAnswer), ServiceQueryAnswerType_Txts, true, pServiceQuery->m_pUserdata);
|
MDNSServiceInfo serviceInfo(*this, (hMDNSServiceQuery)pServiceQuery, pServiceQuery->indexOfAnswer(pSQAnswer));
|
||||||
|
pServiceQuery->m_fnCallback(serviceInfo , static_cast<AnswerType>(ServiceQueryAnswerType_Txts), true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -933,9 +935,9 @@ bool MDNSResponder::_processTXTAnswer(const MDNSResponder::stcMDNS_RRAnswerTXT*
|
|||||||
(pSQAnswer->addIP4Address(pIP4Address))) {
|
(pSQAnswer->addIP4Address(pIP4Address))) {
|
||||||
|
|
||||||
pSQAnswer->m_u32ContentFlags |= ServiceQueryAnswerType_IP4Address;
|
pSQAnswer->m_u32ContentFlags |= ServiceQueryAnswerType_IP4Address;
|
||||||
|
|
||||||
if (pServiceQuery->m_fnCallback) {
|
if (pServiceQuery->m_fnCallback) {
|
||||||
pServiceQuery->m_fnCallback(this, (hMDNSServiceQuery)pServiceQuery, pServiceQuery->indexOfAnswer(pSQAnswer), ServiceQueryAnswerType_IP4Address, true, pServiceQuery->m_pUserdata);
|
MDNSServiceInfo serviceInfo (*this, (hMDNSServiceQuery)pServiceQuery, pServiceQuery->indexOfAnswer(pSQAnswer));
|
||||||
|
pServiceQuery->m_fnCallback(serviceInfo, static_cast<AnswerType>(ServiceQueryAnswerType_IP4Address), true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@ -1060,9 +1062,8 @@ bool MDNSResponder::_updateProbeStatus(void) {
|
|||||||
DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _updateProbeStatus: Done host probing.\n")););
|
DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _updateProbeStatus: Done host probing.\n")););
|
||||||
m_HostProbeInformation.m_ProbingStatus = ProbingStatus_Done;
|
m_HostProbeInformation.m_ProbingStatus = ProbingStatus_Done;
|
||||||
m_HostProbeInformation.m_Timeout.reset(std::numeric_limits<esp8266::polledTimeout::oneShot::timeType>::max());
|
m_HostProbeInformation.m_Timeout.reset(std::numeric_limits<esp8266::polledTimeout::oneShot::timeType>::max());
|
||||||
|
if (m_HostProbeInformation.m_fnHostProbeResultCallback) {
|
||||||
if (m_HostProbeInformation.m_fnProbeResultCallback) {
|
m_HostProbeInformation.m_fnHostProbeResultCallback(m_pcHostname, true);
|
||||||
m_HostProbeInformation.m_fnProbeResultCallback(this, m_pcHostname, 0, true, m_HostProbeInformation.m_pProbeResultCallbackUserdata);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prepare to announce host
|
// Prepare to announce host
|
||||||
@ -1110,21 +1111,9 @@ bool MDNSResponder::_updateProbeStatus(void) {
|
|||||||
DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _updateProbeStatus: Done service probing %s.%s.%s\n\n"), (pService->m_pcName ?: m_pcHostname), pService->m_pcService, pService->m_pcProtocol););
|
DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _updateProbeStatus: Done service probing %s.%s.%s\n\n"), (pService->m_pcName ?: m_pcHostname), pService->m_pcService, pService->m_pcProtocol););
|
||||||
pService->m_ProbeInformation.m_ProbingStatus = ProbingStatus_Done;
|
pService->m_ProbeInformation.m_ProbingStatus = ProbingStatus_Done;
|
||||||
pService->m_ProbeInformation.m_Timeout.reset(std::numeric_limits<esp8266::polledTimeout::oneShot::timeType>::max());
|
pService->m_ProbeInformation.m_Timeout.reset(std::numeric_limits<esp8266::polledTimeout::oneShot::timeType>::max());
|
||||||
|
if (pService->m_ProbeInformation.m_fnServiceProbeResultCallback) {
|
||||||
MDNSProbeResultCallbackFn fnProbeResultCallback = 0;
|
pService->m_ProbeInformation.m_fnServiceProbeResultCallback(pService->m_pcName, pService, true);
|
||||||
void* pProbeResultCallbackUserdata = 0;
|
|
||||||
if (pService->m_ProbeInformation.m_fnProbeResultCallback) {
|
|
||||||
fnProbeResultCallback = pService->m_ProbeInformation.m_fnProbeResultCallback;
|
|
||||||
pProbeResultCallbackUserdata = pService->m_ProbeInformation.m_pProbeResultCallbackUserdata;
|
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
fnProbeResultCallback = m_HostProbeInformation.m_fnProbeResultCallback;
|
|
||||||
pProbeResultCallbackUserdata = m_HostProbeInformation.m_pProbeResultCallbackUserdata;
|
|
||||||
}
|
|
||||||
if (fnProbeResultCallback) {
|
|
||||||
fnProbeResultCallback(this, (pService->m_pcName ?: m_pcHostname), pService, true, pProbeResultCallbackUserdata);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Prepare to announce service
|
// Prepare to announce service
|
||||||
pService->m_ProbeInformation.m_u8SentCount = 0;
|
pService->m_ProbeInformation.m_u8SentCount = 0;
|
||||||
pService->m_ProbeInformation.m_Timeout.reset(MDNS_ANNOUNCE_DELAY);
|
pService->m_ProbeInformation.m_Timeout.reset(MDNS_ANNOUNCE_DELAY);
|
||||||
@ -1287,10 +1276,9 @@ bool MDNSResponder::_cancelProbingForHost(void) {
|
|||||||
bool bResult = false;
|
bool bResult = false;
|
||||||
|
|
||||||
m_HostProbeInformation.clear(false);
|
m_HostProbeInformation.clear(false);
|
||||||
|
|
||||||
// Send host notification
|
// Send host notification
|
||||||
if (m_HostProbeInformation.m_fnProbeResultCallback) {
|
if (m_HostProbeInformation.m_fnHostProbeResultCallback) {
|
||||||
m_HostProbeInformation.m_fnProbeResultCallback(this, m_pcHostname, 0, false, m_HostProbeInformation.m_pProbeResultCallbackUserdata);
|
m_HostProbeInformation.m_fnHostProbeResultCallback(m_pcHostname, false);
|
||||||
|
|
||||||
bResult = true;
|
bResult = true;
|
||||||
}
|
}
|
||||||
@ -1309,21 +1297,10 @@ bool MDNSResponder::_cancelProbingForService(stcMDNSService& p_rService) {
|
|||||||
bool bResult = false;
|
bool bResult = false;
|
||||||
|
|
||||||
p_rService.m_ProbeInformation.clear(false);
|
p_rService.m_ProbeInformation.clear(false);
|
||||||
|
|
||||||
// Send notification
|
// Send notification
|
||||||
MDNSProbeResultCallbackFn fnProbeResultCallback = 0;
|
if (p_rService.m_ProbeInformation.m_fnServiceProbeResultCallback) {
|
||||||
void* pProbeResultCallbackUserdata = 0;
|
p_rService.m_ProbeInformation.m_fnServiceProbeResultCallback(p_rService.m_pcName,&p_rService,false);
|
||||||
if (p_rService.m_ProbeInformation.m_fnProbeResultCallback) {
|
bResult = true;
|
||||||
fnProbeResultCallback = p_rService.m_ProbeInformation.m_fnProbeResultCallback;
|
|
||||||
pProbeResultCallbackUserdata = p_rService.m_ProbeInformation.m_pProbeResultCallbackUserdata;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
fnProbeResultCallback = m_HostProbeInformation.m_fnProbeResultCallback;
|
|
||||||
pProbeResultCallbackUserdata = m_HostProbeInformation.m_pProbeResultCallbackUserdata;
|
|
||||||
}
|
|
||||||
if (fnProbeResultCallback) {
|
|
||||||
fnProbeResultCallback(this, (p_rService.m_pcName ?: m_pcHostname), &p_rService, false, pProbeResultCallbackUserdata);
|
|
||||||
bResult = true;
|
|
||||||
}
|
}
|
||||||
return bResult;
|
return bResult;
|
||||||
}
|
}
|
||||||
@ -1506,7 +1483,8 @@ bool MDNSResponder::_checkServiceQueryCache(void) {
|
|||||||
else {
|
else {
|
||||||
// Timed out! -> Delete
|
// Timed out! -> Delete
|
||||||
if (pServiceQuery->m_fnCallback) {
|
if (pServiceQuery->m_fnCallback) {
|
||||||
pServiceQuery->m_fnCallback(this, (hMDNSServiceQuery)pServiceQuery, pServiceQuery->indexOfAnswer(pSQAnswer), ServiceQueryAnswerType_ServiceDomain, false, pServiceQuery->m_pUserdata);
|
MDNSServiceInfo serviceInfo(*this, (hMDNSServiceQuery)pServiceQuery, pServiceQuery->indexOfAnswer(pSQAnswer));
|
||||||
|
pServiceQuery->m_fnCallback(serviceInfo, static_cast<AnswerType>(ServiceQueryAnswerType_ServiceDomain), false);
|
||||||
}
|
}
|
||||||
DEBUG_EX_INFO(
|
DEBUG_EX_INFO(
|
||||||
DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _checkServiceQueryCache: Will remove PTR answer for "));
|
DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _checkServiceQueryCache: Will remove PTR answer for "));
|
||||||
@ -1563,9 +1541,9 @@ bool MDNSResponder::_checkServiceQueryCache(void) {
|
|||||||
|
|
||||||
// Remove content flags for deleted answer parts
|
// Remove content flags for deleted answer parts
|
||||||
pSQAnswer->m_u32ContentFlags &= ~u32ContentFlags;
|
pSQAnswer->m_u32ContentFlags &= ~u32ContentFlags;
|
||||||
|
|
||||||
if (pServiceQuery->m_fnCallback) {
|
if (pServiceQuery->m_fnCallback) {
|
||||||
pServiceQuery->m_fnCallback(this, (hMDNSServiceQuery)pServiceQuery, pServiceQuery->indexOfAnswer(pSQAnswer), u32ContentFlags, false, pServiceQuery->m_pUserdata);
|
MDNSServiceInfo serviceInfo(*this, (hMDNSServiceQuery)pServiceQuery, pServiceQuery->indexOfAnswer(pSQAnswer));
|
||||||
|
pServiceQuery->m_fnCallback(serviceInfo,static_cast<AnswerType>(u32ContentFlags), false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} // HostDomainAndPort flagged
|
} // HostDomainAndPort flagged
|
||||||
@ -1601,7 +1579,8 @@ bool MDNSResponder::_checkServiceQueryCache(void) {
|
|||||||
pSQAnswer->m_u32ContentFlags &= ~ServiceQueryAnswerType_Txts;
|
pSQAnswer->m_u32ContentFlags &= ~ServiceQueryAnswerType_Txts;
|
||||||
|
|
||||||
if (pServiceQuery->m_fnCallback) {
|
if (pServiceQuery->m_fnCallback) {
|
||||||
pServiceQuery->m_fnCallback(this, (hMDNSServiceQuery)pServiceQuery, pServiceQuery->indexOfAnswer(pSQAnswer), ServiceQueryAnswerType_Txts, false, pServiceQuery->m_pUserdata);
|
MDNSServiceInfo serviceInfo(*this, (hMDNSServiceQuery)pServiceQuery, pServiceQuery->indexOfAnswer(pSQAnswer));
|
||||||
|
pServiceQuery->m_fnCallback(serviceInfo, static_cast<AnswerType>(ServiceQueryAnswerType_Txts), false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} // TXTs flagged
|
} // TXTs flagged
|
||||||
@ -1648,7 +1627,8 @@ bool MDNSResponder::_checkServiceQueryCache(void) {
|
|||||||
}
|
}
|
||||||
// Notify client
|
// Notify client
|
||||||
if (pServiceQuery->m_fnCallback) {
|
if (pServiceQuery->m_fnCallback) {
|
||||||
pServiceQuery->m_fnCallback(this, (hMDNSServiceQuery)pServiceQuery, pServiceQuery->indexOfAnswer(pSQAnswer), ServiceQueryAnswerType_IP4Address, false, pServiceQuery->m_pUserdata);
|
MDNSServiceInfo serviceInfo(*this, (hMDNSServiceQuery)pServiceQuery, pServiceQuery->indexOfAnswer(pSQAnswer));
|
||||||
|
pServiceQuery->m_fnCallback(serviceInfo, static_cast<AnswerType>(ServiceQueryAnswerType_IP4Address), false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} // IP4 flagged
|
} // IP4 flagged
|
||||||
|
@ -629,20 +629,27 @@ MDNSResponder::stcMDNSServiceTxt* MDNSResponder::_addServiceTxt(MDNSResponder::s
|
|||||||
return pResult;
|
return pResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
MDNSResponder::stcMDNSServiceTxt* MDNSResponder::_answerKeyValue(const hMDNSServiceQuery p_hServiceQuery,
|
||||||
|
const uint32_t p_u32AnswerIndex) {
|
||||||
|
stcMDNSServiceQuery* pServiceQuery = _findServiceQuery(p_hServiceQuery);
|
||||||
|
stcMDNSServiceQuery::stcAnswer* pSQAnswer = (pServiceQuery ? pServiceQuery->answerAtIndex(p_u32AnswerIndex) : 0);
|
||||||
|
// Fill m_pcTxts (if not already done)
|
||||||
|
return (pSQAnswer) ? pSQAnswer->m_Txts.m_pTxts : 0;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* MDNSResponder::_collectServiceTxts
|
* MDNSResponder::_collectServiceTxts
|
||||||
*/
|
*/
|
||||||
bool MDNSResponder::_collectServiceTxts(MDNSResponder::stcMDNSService& p_rService) {
|
bool MDNSResponder::_collectServiceTxts(MDNSResponder::stcMDNSService& p_rService) {
|
||||||
|
|
||||||
bool bResult = (m_fnServiceTxtCallback
|
// Call Dynamic service callbacks
|
||||||
? m_fnServiceTxtCallback(this, (hMDNSService)&p_rService, m_pServiceTxtCallbackUserdata)
|
if (m_fnServiceTxtCallback) {
|
||||||
: true);
|
m_fnServiceTxtCallback((hMDNSService)&p_rService);
|
||||||
|
|
||||||
if ((bResult) &&
|
|
||||||
(p_rService.m_fnTxtCallback)) {
|
|
||||||
bResult = p_rService.m_fnTxtCallback(this, (hMDNSService)&p_rService, p_rService.m_pTxtCallbackUserdata);
|
|
||||||
}
|
}
|
||||||
return bResult;
|
if (p_rService.m_fnTxtCallback) {
|
||||||
|
p_rService.m_fnTxtCallback((hMDNSService)&p_rService);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -1162,8 +1162,8 @@ MDNSResponder::stcProbeInformation::stcProbeInformation(void)
|
|||||||
m_Timeout(std::numeric_limits<esp8266::polledTimeout::oneShot::timeType>::max()),
|
m_Timeout(std::numeric_limits<esp8266::polledTimeout::oneShot::timeType>::max()),
|
||||||
m_bConflict(false),
|
m_bConflict(false),
|
||||||
m_bTiebreakNeeded(false),
|
m_bTiebreakNeeded(false),
|
||||||
m_fnProbeResultCallback(0),
|
m_fnHostProbeResultCallback(0),
|
||||||
m_pProbeResultCallbackUserdata(0) {
|
m_fnServiceProbeResultCallback(0) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -1177,8 +1177,8 @@ bool MDNSResponder::stcProbeInformation::clear(bool p_bClearUserdata /*= false*/
|
|||||||
m_bConflict = false;
|
m_bConflict = false;
|
||||||
m_bTiebreakNeeded = false;
|
m_bTiebreakNeeded = false;
|
||||||
if (p_bClearUserdata) {
|
if (p_bClearUserdata) {
|
||||||
m_fnProbeResultCallback = 0;
|
m_fnHostProbeResultCallback = 0;
|
||||||
m_pProbeResultCallbackUserdata = 0;
|
m_fnServiceProbeResultCallback = 0;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -1207,8 +1207,7 @@ MDNSResponder::stcMDNSService::stcMDNSService(const char* p_pcName /*= 0*/,
|
|||||||
m_pcProtocol(0),
|
m_pcProtocol(0),
|
||||||
m_u16Port(0),
|
m_u16Port(0),
|
||||||
m_u8ReplyMask(0),
|
m_u8ReplyMask(0),
|
||||||
m_fnTxtCallback(0),
|
m_fnTxtCallback(0) {
|
||||||
m_pTxtCallbackUserdata(0) {
|
|
||||||
|
|
||||||
setName(p_pcName);
|
setName(p_pcName);
|
||||||
setService(p_pcService);
|
setService(p_pcService);
|
||||||
@ -1921,7 +1920,6 @@ MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcIP6Address* MDNSResponder::stc
|
|||||||
MDNSResponder::stcMDNSServiceQuery::stcMDNSServiceQuery(void)
|
MDNSResponder::stcMDNSServiceQuery::stcMDNSServiceQuery(void)
|
||||||
: m_pNext(0),
|
: m_pNext(0),
|
||||||
m_fnCallback(0),
|
m_fnCallback(0),
|
||||||
m_pUserdata(0),
|
|
||||||
m_bLegacyQuery(false),
|
m_bLegacyQuery(false),
|
||||||
m_u8SentCount(0),
|
m_u8SentCount(0),
|
||||||
m_ResendTimeout(std::numeric_limits<esp8266::polledTimeout::oneShot::timeType>::max()),
|
m_ResendTimeout(std::numeric_limits<esp8266::polledTimeout::oneShot::timeType>::max()),
|
||||||
@ -1945,7 +1943,6 @@ MDNSResponder::stcMDNSServiceQuery::~stcMDNSServiceQuery(void) {
|
|||||||
bool MDNSResponder::stcMDNSServiceQuery::clear(void) {
|
bool MDNSResponder::stcMDNSServiceQuery::clear(void) {
|
||||||
|
|
||||||
m_fnCallback = 0;
|
m_fnCallback = 0;
|
||||||
m_pUserdata = 0;
|
|
||||||
m_bLegacyQuery = false;
|
m_bLegacyQuery = false;
|
||||||
m_u8SentCount = 0;
|
m_u8SentCount = 0;
|
||||||
m_ResendTimeout.reset(std::numeric_limits<esp8266::polledTimeout::oneShot::timeType>::max());
|
m_ResendTimeout.reset(std::numeric_limits<esp8266::polledTimeout::oneShot::timeType>::max());
|
||||||
|
Loading…
x
Reference in New Issue
Block a user