diff --git a/libraries/ESP8266mDNS/examples/LEAmDNS/mDNS_Clock/mDNS_Clock.ino b/libraries/ESP8266mDNS/examples/LEAmDNS/mDNS_Clock/mDNS_Clock.ino index 660271b70b..0c6f24ca87 100644 --- a/libraries/ESP8266mDNS/examples/LEAmDNS/mDNS_Clock/mDNS_Clock.ino +++ b/libraries/ESP8266mDNS/examples/LEAmDNS/mDNS_Clock/mDNS_Clock.ino @@ -34,6 +34,7 @@ #include #include +#include #include /* @@ -72,9 +73,8 @@ char* pcHostDomain = 0; // Negociated bool bHostDomainConfirmed = false; // Flags the confirmation of the host domain 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); - +// HTTP server at port 'SERVICE_PORT' will respond to HTTP requests +ESP8266WebServer server(SERVICE_PORT); /* getTimeString @@ -135,18 +135,13 @@ bool setStationHostname(const char* p_pcHostname) { This can be triggered by calling MDNS.announce(). */ -bool MDNSDynamicServiceTxtCallback(MDNSResponder* p_pMDNSResponder, - const MDNSResponder::hMDNSService p_hService, - void* p_pUserdata) { +void MDNSDynamicServiceTxtCallback(const MDNSResponder::hMDNSService p_hService) { Serial.println("MDNSDynamicServiceTxtCallback"); - (void) p_pUserdata; - if ((p_pMDNSResponder) && - (hMDNSService == p_hService)) { + if (hMDNSService == p_hService) { 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(). */ -bool MDNSProbeResultCallback(MDNSResponder* p_pMDNSResponder, - const char* p_pcDomainName, - const MDNSResponder::hMDNSService p_hService, - bool p_bProbeResult, - void* p_pUserdata) { +void hostProbeResult(String p_pcDomainName, bool p_bProbeResult) { + Serial.println("MDNSProbeResultCallback"); - (void) p_pUserdata; - - if ((p_pMDNSResponder) && - (0 == p_hService)) { // Called for host domain - Serial.printf("MDNSProbeResultCallback: Host domain '%s.local' is %s\n", p_pcDomainName, (p_bProbeResult ? "free" : "already USED!")); - if (true == p_bProbeResult) { - // Set station hostname - setStationHostname(pcHostDomain); - - if (!bHostDomainConfirmed) { - // Hostname free -> setup clock service - bHostDomainConfirmed = true; - - if (!hMDNSService) { - // Add a 'clock.tcp' service to port 'SERVICE_PORT', using the host domain as instance domain - hMDNSService = p_pMDNSResponder->addService(0, "espclk", "tcp", SERVICE_PORT); - if (hMDNSService) { - // 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); - } + 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 (!bHostDomainConfirmed) { + // Hostname free -> setup clock service + bHostDomainConfirmed = true; + + if (!hMDNSService) { + // Add a 'clock.tcp' service to port 'SERVICE_PORT', using the host domain as instance domain + hMDNSService = MDNS.addService(0, "espclk", "tcp", SERVICE_PORT); + if (hMDNSService) { + // Add a simple static MDNS service TXT item + MDNS.addServiceTxt(hMDNSService, "port#", SERVICE_PORT); + // Set the callback function for dynamic service TXTs + MDNS.setDynamicServiceTxtCallback(MDNSDynamicServiceTxtCallback); } } + } + } else { + // Change hostname, use '-' as divider between base name and index + if (MDNSResponder::indexDomain(pcHostDomain, "-", 0)) { + MDNS.setHostname(pcHostDomain); } else { - // Change hostname, use '-' as divider between base name and index - if (MDNSResponder::indexDomain(pcHostDomain, "-", 0)) { - p_pMDNSResponder->setHostname(pcHostDomain); - } else { - Serial.println("MDNSProbeResultCallback: FAILED to update hostname!"); - } + Serial.println("MDNSProbeResultCallback: FAILED to update hostname!"); } } - return true; } /* handleHTTPClient */ -void handleHTTPClient(WiFiClient& client) { - Serial.println(""); - Serial.println("New client"); - - // 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(); +void handleHTTPRequest() { + Serial.println(""); + Serial.println("HTTP Request"); // Get current time time_t now = time(nullptr);; @@ -238,27 +203,18 @@ void handleHTTPClient(WiFiClient& client) { gmtime_r(&now, &timeinfo); 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\r\nHello from "; - s += WiFi.hostname() + " at " + ipStr; - // Simple addition of the current time - s += "\r\nCurrent time is: "; - s += getTimeString(); - // done :-) - s += "\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 = "\r\nHello from "; + s += WiFi.hostname() + " at " + WiFi.localIP().toString(); + // Simple addition of the current time + s += "\r\nCurrent time is: "; + s += getTimeString(); + // done :-) + s += "\r\n\r\n"; + Serial.println("Sending 200"); + server.send(200, "text/html", s); } - /* setup */ @@ -285,7 +241,7 @@ void setup(void) { setClock(); // Setup MDNS responder - MDNS.setProbeResultCallback(MDNSProbeResultCallback, 0); + MDNS.setHostProbeResultCallback(hostProbeResult); // Init the (currently empty) host domain string with 'esp8266' if ((!MDNSResponder::indexDomain(pcHostDomain, 0, "esp8266")) || (!MDNS.begin(pcHostDomain))) { @@ -296,26 +252,22 @@ void setup(void) { } Serial.println("MDNS responder started"); - // Start TCP (HTTP) server + // Setup HTTP server + server.on("/", handleHTTPRequest); server.begin(); - Serial.println("TCP server started"); + Serial.println("HTTP server started"); } - /* loop */ 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 MDNS.update(); - // Update time (if needed) static esp8266::polledTimeout::periodic timeout(UPDATE_CYCLE); if (timeout.expired()) { @@ -326,5 +278,3 @@ void loop(void) { } } } - - diff --git a/libraries/ESP8266mDNS/examples/LEAmDNS/mDNS_ServiceMonitor/mDNS_ServiceMonitor.ino b/libraries/ESP8266mDNS/examples/LEAmDNS/mDNS_ServiceMonitor/mDNS_ServiceMonitor.ino index 1bfe7c3802..98269fb48e 100644 --- a/libraries/ESP8266mDNS/examples/LEAmDNS/mDNS_ServiceMonitor/mDNS_ServiceMonitor.ino +++ b/libraries/ESP8266mDNS/examples/LEAmDNS/mDNS_ServiceMonitor/mDNS_ServiceMonitor.ino @@ -82,106 +82,55 @@ bool setStationHostname(const char* p_pcHostname) { if (p_pcHostname) { WiFi.hostname(p_pcHostname); Serial.printf("setStationHostname: Station hostname is set to '%s'\n", p_pcHostname); + return true; } - return true; + return false; } - - /* 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) && - (hMDNSServiceQuery == p_hServiceQuery)) { - - if (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 (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), - p_pMDNSResponder->answerHostDomain(p_hServiceQuery, p_u32AnswerIndex), - p_pMDNSResponder->answerPort(p_hServiceQuery, p_u32AnswerIndex)); - } else { - Serial.printf("MDNSServiceQueryCallback: Host domain and port removed from service '%s'\n", - p_pMDNSResponder->answerServiceDomain(p_hServiceQuery, p_u32AnswerIndex)); - } - } else if (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)); - 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 += "
  • "; - 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 += "
    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 += "
    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 += "
    TXT: "; - strHTTPServices += p_pMDNSResponder->answerTxts(p_hServiceQuery, u); - } - strHTTPServices += "
  • "; +void MDNSServiceQueryCallback(MDNSResponder::MDNSServiceInfo serviceInfo, MDNSResponder::AnswerType answerType, bool p_bSetContent) { + String answerInfo; + switch (answerType) { + case MDNSResponder::AnswerType::ServiceDomain : + answerInfo = "ServiceDomain " + String(serviceInfo.serviceDomain()); + break; + case MDNSResponder::AnswerType::HostDomainAndPort : + answerInfo = "HostDomainAndPort " + String(serviceInfo.hostDomain()) + ":" + String(serviceInfo.hostPort()); + break; + case MDNSResponder::AnswerType::IP4Address : + answerInfo = "IP4Address "; + for (IPAddress ip : serviceInfo.IP4Adresses()) { + answerInfo += "- " + ip.toString(); + }; + break; + case MDNSResponder::AnswerType::Txt : + answerInfo = "TXT " + String(serviceInfo.strKeyValue()); + for (auto kv : serviceInfo.keyValues()) { + answerInfo += "\nkv : " + String(kv.first) + " : " + String(kv.second); } - } else { - strHTTPServices = cstrNoHTTPServices; - } + break; + default : + answerInfo = "Unknown Answertype"; } - return true; + Serial.printf("Answer %s %s\n", answerInfo.c_str(), p_bSetContent ? "Modified" : "Deleted"); +} + +/* + 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!")); } /* - MDNSProbeResultCallback + MDNSHostProbeResultCallback Probe result callback for the host domain. 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(). */ -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) && - (0 == p_hService)) { // Called for host domain - - Serial.printf("MDNSProbeResultCallback: Host domain '%s.local' is %s\n", p_pcDomainName, (p_bProbeResult ? "free" : "already USED!")); - - if (true == p_bProbeResult) { - // Set station hostname - setStationHostname(pcHostDomain); - - if (!bHostDomainConfirmed) { - // Hostname free -> setup clock service - bHostDomainConfirmed = true; - - if (!hMDNSService) { - // 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); - if (hMDNSService) { - // Add some '_http._tcp' protocol specific MDNS service TXT items - // 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 - if (!hMDNSServiceQuery) { - hMDNSServiceQuery = p_pMDNSResponder->installServiceQuery("http", "tcp", MDNSServiceQueryCallback, 0); - 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"); - } +void hostProbeResult(String p_pcDomainName, bool p_bProbeResult) { + + Serial.printf("MDNSHostProbeResultCallback: 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 (!bHostDomainConfirmed) { + // Hostname free -> setup clock service + bHostDomainConfirmed = true; + + if (!hMDNSService) { + // Add a 'http.tcp' service to port 'SERVICE_PORT', using the host domain as instance domain + hMDNSService = MDNS.addService(0, "http", "tcp", SERVICE_PORT); + if (hMDNSService) { + MDNS.setServiceProbeResultCallback(hMDNSService, serviceProbeResult); + + // Add some '_http._tcp' protocol specific MDNS service TXT items + // See: http://www.dns-sd.org/txtrecords.html#http + MDNS.addServiceTxt(hMDNSService, "user", ""); + MDNS.addServiceTxt(hMDNSService, "password", ""); + MDNS.addServiceTxt(hMDNSService, "path", "/"); + } + + // 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 { - // Change hostname, use '-' as divider between base name and index - if (MDNSResponder::indexDomain(pcHostDomain, "-", 0)) { - p_pMDNSResponder->setHostname(pcHostDomain); - } else { - Serial.println("MDNSProbeResultCallback: FAILED to update hostname!"); - } + Serial.println("MDNSProbeResultCallback: FAILED to update hostname!"); } } - return true; } /* @@ -251,12 +193,36 @@ void handleHTTPRequest() { Serial.println(""); Serial.println("HTTP Request"); + IPAddress ip = WiFi.localIP(); + String ipStr = ip.toString(); String s = "\r\n

    Hello from "; s += WiFi.hostname() + ".local at " + WiFi.localIP().toString() + "

    "; - s += "

    Local HTTP services are :

      "; - s += strHTTPServices; - // done :-) - s += "
    "; + s += "

    Local HTTP services are :

    "; + s += "
      "; + for (auto info : MDNS.answerInfo(hMDNSServiceQuery)) { + s += "
    1. "; + s += info.serviceDomain(); + if (info.hostDomainAvailable()) { + s += "
      Hostname: "; + s += String(info.hostDomain()); + s += (info.hostPortAvailable()) ? (":" + String(info.hostPort())) : ""; + } + if (info.IP4AddressAvailable()) { + s += "
      IP4:"; + for (auto ip : info.IP4Adresses()) { + s += " " + ip.toString(); + } + } + if (info.txtAvailable()) { + s += "
      TXT:
      "; + for (auto kv : info.keyValues()) { + s += "\t" + String(kv.first) + " : " + String(kv.second) + "
      "; + } + } + s += "
    2. "; + } + s += "

    "; + Serial.println("Sending 200"); server.send(200, "text/html", s); Serial.println("Done with request"); @@ -288,12 +254,13 @@ void setup(void) { // Setup HTTP server server.on("/", handleHTTPRequest); - // Setup MDNS responder - MDNS.setProbeResultCallback(MDNSProbeResultCallback, 0); + // Setup MDNS responders + MDNS.setHostProbeResultCallback(hostProbeResult); + // Init the (currently empty) host domain string with 'esp8266' if ((!MDNSResponder::indexDomain(pcHostDomain, 0, "esp8266")) || (!MDNS.begin(pcHostDomain))) { - Serial.println("Error setting up MDNS responder!"); + Serial.println(" Error setting up MDNS responder!"); while (1) { // STOP delay(1000); } @@ -305,13 +272,12 @@ void setup(void) { Serial.println("HTTP server started"); } - -/* - loop -*/ void loop(void) { // Check if a request has come in server.handleClient(); // Allow MDNS processing MDNS.update(); } + + + diff --git a/libraries/ESP8266mDNS/src/LEAmDNS.cpp b/libraries/ESP8266mDNS/src/LEAmDNS.cpp index 581e7180a4..1b45b9950f 100644 --- a/libraries/ESP8266mDNS/src/LEAmDNS.cpp +++ b/libraries/ESP8266mDNS/src/LEAmDNS.cpp @@ -58,7 +58,6 @@ MDNSResponder::MDNSResponder(void) m_pcHostname(0), m_pServiceQueries(0), m_fnServiceTxtCallback(0), - m_pServiceTxtCallbackUserdata(0), #ifdef ENABLE_ESP_MDNS_RESPONDER_PASSIV_MODE m_bPassivModeEnabled(true) { #else @@ -138,7 +137,10 @@ bool MDNSResponder::begin(const char* p_pcHostname, */ bool MDNSResponder::close(void) { - _announce(false, true); + m_GotIPHandler.reset(); // reset WiFi event callbacks. + m_DisconnectedHandler.reset(); + + _announce(false, true); _resetProbeStatus(false); // Stop probing _releaseServiceQueries(); @@ -148,6 +150,18 @@ bool MDNSResponder::close(void) { return true; } +/* + * MDNSResponder::end + * + * Ends the MDNS responder. + * for compatibility with esp32 + * + */ + +bool MDNSResponder::end(void) { + return close(); +} + /* * MDNSResponder::setHostname * @@ -464,11 +478,9 @@ bool MDNSResponder::addServiceTxt(String p_strService, * service TXT items are needed. * */ -bool MDNSResponder::setDynamicServiceTxtCallback(MDNSResponder::MDNSDynamicServiceTxtCallbackFn p_fnCallback, - void* p_pUserdata) { +bool MDNSResponder::setDynamicServiceTxtCallback(MDNSResponder::MDNSDynamicServiceTxtCallbackFunc p_fnCallback) { m_fnServiceTxtCallback = p_fnCallback; - m_pServiceTxtCallbackUserdata = p_pUserdata; return true; } @@ -481,15 +493,13 @@ bool MDNSResponder::setDynamicServiceTxtCallback(MDNSResponder::MDNSDynamicServi * */ bool MDNSResponder::setDynamicServiceTxtCallback(MDNSResponder::hMDNSService p_hService, - MDNSResponder::MDNSDynamicServiceTxtCallbackFn p_fnCallback, - void* p_pUserdata) { + MDNSResponder::MDNSDynamicServiceTxtCallbackFunc p_fnCallback) { bool bResult = false; stcMDNSService* pService = _findService(p_hService); if (pService) { pService->m_fnTxtCallback = p_fnCallback; - pService->m_pTxtCallbackUserdata = p_pUserdata; bResult = true; } @@ -766,8 +776,7 @@ uint16_t MDNSResponder::port(const uint32_t p_u32AnswerIndex) { */ MDNSResponder::hMDNSServiceQuery MDNSResponder::installServiceQuery(const char* p_pcService, const char* p_pcProtocol, - MDNSResponder::MDNSServiceQueryCallbackFn p_fnCallback, - void* p_pUserdata) { + MDNSResponder::MDNSServiceQueryCallbackFunc p_fnCallback) { hMDNSServiceQuery hResult = 0; stcMDNSServiceQuery* pServiceQuery = 0; @@ -780,7 +789,6 @@ MDNSResponder::hMDNSServiceQuery MDNSResponder::installServiceQuery(const char* (_buildDomainForService(p_pcService, p_pcProtocol, pServiceQuery->m_ServiceTypeDomain))) { pServiceQuery->m_fnCallback = p_fnCallback; - pServiceQuery->m_pUserdata = p_pUserdata; pServiceQuery->m_bLegacyQuery = false; if (_sendMDNSServiceQuery(*pServiceQuery)) { @@ -822,6 +830,15 @@ uint32_t MDNSResponder::answerCount(const MDNSResponder::hMDNSServiceQuery p_hSe return (pServiceQuery ? pServiceQuery->answerCount() : 0); } +std::vector MDNSResponder::answerInfo (const MDNSResponder::hMDNSServiceQuery p_hServiceQuery) { + std::vector tempVector; + for (uint32_t i=0;im_Txts.m_pTxts) && (!pSQAnswer->m_pcTxts)) { - + pSQAnswer->m_pcTxts = pSQAnswer->allocTxts(pSQAnswer->m_Txts.c_strLength()); if (pSQAnswer->m_pcTxts) { pSQAnswer->m_Txts.c_str(pSQAnswer->m_pcTxts); @@ -1020,7 +1037,6 @@ const char* MDNSResponder::answerTxts(const MDNSResponder::hMDNSServiceQuery p_h return (pSQAnswer ? pSQAnswer->m_pcTxts : 0); } - /* * 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. * */ -bool MDNSResponder::setProbeResultCallback(MDNSResponder::MDNSProbeResultCallbackFn p_fnCallback, - void* p_pUserdata) { - - m_HostProbeInformation.m_fnProbeResultCallback = p_fnCallback; - m_HostProbeInformation.m_pProbeResultCallbackUserdata = p_pUserdata; +bool MDNSResponder::setHostProbeResultCallback(MDNSResponder::MDNSHostProbeFn p_fnCallback) { + + m_HostProbeInformation.m_fnHostProbeResultCallback = p_fnCallback; return true; } +bool MDNSResponder::setHostProbeResultCallback(MDNSHostProbeFn1 pfn) { + using namespace std::placeholders; + return setHostProbeResultCallback(std::bind(pfn, *this, _1, _2)); +} + /* * MDNSResponder::setServiceProbeResultCallback * @@ -1054,20 +1073,25 @@ bool MDNSResponder::setProbeResultCallback(MDNSResponder::MDNSProbeResultCallbac * */ bool MDNSResponder::setServiceProbeResultCallback(const MDNSResponder::hMDNSService p_hService, - MDNSResponder::MDNSProbeResultCallbackFn p_fnCallback, - void* p_pUserdata) { + MDNSResponder::MDNSServiceProbeFn p_fnCallback) { + bool bResult = false; stcMDNSService* pService = _findService(p_hService); if (pService) { - pService->m_ProbeInformation.m_fnProbeResultCallback = p_fnCallback; - pService->m_ProbeInformation.m_pProbeResultCallbackUserdata = p_pUserdata; + pService->m_ProbeInformation.m_fnServiceProbeResultCallback = p_fnCallback; bResult = true; } 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 diff --git a/libraries/ESP8266mDNS/src/LEAmDNS.h b/libraries/ESP8266mDNS/src/LEAmDNS.h index f5a6702707..d35df1fbd3 100644 --- a/libraries/ESP8266mDNS/src/LEAmDNS.h +++ b/libraries/ESP8266mDNS/src/LEAmDNS.h @@ -109,6 +109,8 @@ #include "include/UdpContext.h" #include #include +#include + #include "ESP8266WiFi.h" @@ -186,7 +188,8 @@ class MDNSResponder { } // Finish MDNS processing bool close(void); - + // for esp32 compatability + bool end(void); // Change hostname (probing is restarted) bool setHostname(const char* p_pcHostname); // for compatibility... @@ -223,6 +226,8 @@ class MDNSResponder { //Warning: this has the side effect of changing the hostname. //TODO: implement instancename different from hostname 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) @@ -275,19 +280,16 @@ class MDNSResponder { * MDNSDynamicServiceTxtCallbackFn * Callback function for dynamic MDNS TXT items */ - typedef bool (*MDNSDynamicServiceTxtCallbackFn)(MDNSResponder* p_pMDNSResponder, - const hMDNSService p_hService, - void* p_pUserdata); + + typedef std::function MDNSDynamicServiceTxtCallbackFunc; // 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. - bool setDynamicServiceTxtCallback(MDNSDynamicServiceTxtCallbackFn p_fnCallback, - void* p_pUserdata); + bool setDynamicServiceTxtCallback(MDNSDynamicServiceTxtCallbackFunc p_fnCallback); // 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. bool setDynamicServiceTxtCallback(const hMDNSService p_hService, - MDNSDynamicServiceTxtCallbackFn p_fnCallback, - void* p_pUserdata); + MDNSDynamicServiceTxtCallbackFunc p_fnCallback); // 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 @@ -355,16 +357,28 @@ class MDNSResponder { #endif } 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 * Callback function for received answers for dynamic service queries */ - typedef bool (*MDNSServiceQueryCallbackFn)(MDNSResponder* p_pMDNSResponder, - const hMDNSServiceQuery p_hServiceQuery, // dynamic service query handle - uint32_t p_u32AnswerIndex, // index of the updated answer - uint32_t p_u32ServiceQueryAnswerMask, // flag for the updated answer item - bool p_bSetContent, // true: Answer component set, false: component deleted - void* p_pUserdata); // pUserdata set via 'installServiceQuery' + struct MDNSServiceInfo; // forward declaration + typedef std::function MDNSServiceQueryCallbackFunc; // 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 @@ -379,12 +393,13 @@ class MDNSResponder { // - hasAnswerTxts/answerTxts hMDNSServiceQuery installServiceQuery(const char* p_pcService, const char* p_pcProtocol, - MDNSServiceQueryCallbackFn p_fnCallback, - void* p_pUserdata); + MDNSServiceQueryCallbackFunc p_fnCallback); // Remove a dynamic service query bool removeServiceQuery(hMDNSServiceQuery p_hServiceQuery); uint32_t answerCount(const hMDNSServiceQuery p_hServiceQuery); + std::vector answerInfo (const MDNSResponder::hMDNSServiceQuery p_hServiceQuery); + const char* answerServiceDomain(const hMDNSServiceQuery p_hServiceQuery, const uint32_t p_u32AnswerIndex); bool hasAnswerHostDomain(const hMDNSServiceQuery p_hServiceQuery, @@ -418,28 +433,40 @@ class MDNSResponder { // Get the TXT items as a ';'-separated string const char* answerTxts(const hMDNSServiceQuery p_hServiceQuery, const uint32_t p_u32AnswerIndex); - + /** * MDNSProbeResultCallbackFn * Callback function for (host and service domain) probe results - */ - typedef bool (*MDNSProbeResultCallbackFn)(MDNSResponder* p_pMDNSResponder, - const char* p_pcDomainName, - const hMDNSService p_hMDNSService, // 0 for host domain - bool p_bProbeResult, - void* p_pUserdata); + */ + typedef std::function MDNSHostProbeFn; + + typedef std::function MDNSHostProbeFn1; + + typedef std::function MDNSServiceProbeFn; + + typedef std::function MDNSServiceProbeFn1; // 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) - // Succeededs or fails. + // Succeeds or fails. // In case of failure, the failed domain name should be changed. - bool setProbeResultCallback(MDNSProbeResultCallbackFn p_fnCallback, - void* p_pUserdata); - // Set a service specific probe result callcack - bool setServiceProbeResultCallback(const hMDNSService p_hService, - MDNSProbeResultCallbackFn p_fnCallback, - void* p_pUserdata); + bool setHostProbeResultCallback(MDNSHostProbeFn p_fnCallback); + bool setHostProbeResultCallback(MDNSHostProbeFn1 p_fnCallback); + + // Set a service specific probe result callback + bool setServiceProbeResultCallback(const MDNSResponder::hMDNSService p_hService, + MDNSServiceProbeFn p_fnCallback); + bool setServiceProbeResultCallback(const MDNSResponder::hMDNSService p_hService, + MDNSServiceProbeFn1 p_fnCallback); // Application should call this whenever AP is configured/disabled bool notifyAPChange(void); @@ -462,6 +489,98 @@ class MDNSResponder { protected: /** 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; + 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 IP4Adresses(){ + std::vector 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(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 */ @@ -792,8 +911,8 @@ class MDNSResponder { //clsMDNSTimeFlag m_TimeFlag; // Used for probes and announcements bool m_bConflict; bool m_bTiebreakNeeded; - MDNSProbeResultCallbackFn m_fnProbeResultCallback; - void* m_pProbeResultCallbackUserdata; + MDNSHostProbeFn m_fnHostProbeResultCallback; + MDNSServiceProbeFn m_fnServiceProbeResultCallback; stcProbeInformation(void); @@ -813,8 +932,7 @@ class MDNSResponder { uint16_t m_u16Port; uint8_t m_u8ReplyMask; stcMDNSServiceTxts m_Txts; - MDNSDynamicServiceTxtCallbackFn m_fnTxtCallback; - void* m_pTxtCallbackUserdata; + MDNSDynamicServiceTxtCallbackFunc m_fnTxtCallback; stcProbeInformation m_ProbeInformation; stcMDNSService(const char* p_pcName = 0, @@ -957,8 +1075,7 @@ class MDNSResponder { stcMDNSServiceQuery* m_pNext; stcMDNS_RRDomain m_ServiceTypeDomain; // eg. _http._tcp.local - MDNSServiceQueryCallbackFn m_fnCallback; - void* m_pUserdata; + MDNSServiceQueryCallbackFunc m_fnCallback; bool m_bLegacyQuery; uint8_t m_u8SentCount; esp8266::polledTimeout::oneShot m_ResendTimeout; @@ -1035,8 +1152,7 @@ class MDNSResponder { stcMDNSServiceQuery* m_pServiceQueries; WiFiEventHandler m_DisconnectedHandler; WiFiEventHandler m_GotIPHandler; - MDNSDynamicServiceTxtCallbackFn m_fnServiceTxtCallback; - void* m_pServiceTxtCallbackUserdata; + MDNSDynamicServiceTxtCallbackFunc m_fnServiceTxtCallback; bool m_bPassivModeEnabled; stcProbeInformation m_HostProbeInformation; @@ -1271,6 +1387,9 @@ class MDNSResponder { const char* p_pcValue, bool p_bTemp); + stcMDNSServiceTxt* _answerKeyValue(const hMDNSServiceQuery p_hServiceQuery, + const uint32_t p_u32AnswerIndex); + bool _collectServiceTxts(stcMDNSService& p_rService); bool _releaseTempServiceTxts(stcMDNSService& p_rService); const stcMDNSServiceTxt* _serviceTxts(const char* p_pcName, diff --git a/libraries/ESP8266mDNS/src/LEAmDNS_Control.cpp b/libraries/ESP8266mDNS/src/LEAmDNS_Control.cpp index 61c8e91fcd..c95c8d8234 100644 --- a/libraries/ESP8266mDNS/src/LEAmDNS_Control.cpp +++ b/libraries/ESP8266mDNS/src/LEAmDNS_Control.cpp @@ -776,10 +776,10 @@ bool MDNSResponder::_processPTRAnswer(const MDNSResponder::stcMDNS_RRAnswerPTR* pSQAnswer->releaseServiceDomain(); bResult = pServiceQuery->addAnswer(pSQAnswer); - p_rbFoundNewKeyAnswer = true; 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(ServiceQueryAnswerType_ServiceDomain), true); } } } @@ -823,7 +823,8 @@ bool MDNSResponder::_processSRVAnswer(const MDNSResponder::stcMDNS_RRAnswerSRV* p_rbFoundNewKeyAnswer = true; 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(ServiceQueryAnswerType_HostDomainAndPort), true); } } } @@ -870,7 +871,8 @@ bool MDNSResponder::_processTXTAnswer(const MDNSResponder::stcMDNS_RRAnswerTXT* pSQAnswer->releaseTxts(); 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(ServiceQueryAnswerType_Txts), true); } } } @@ -933,9 +935,9 @@ bool MDNSResponder::_processTXTAnswer(const MDNSResponder::stcMDNS_RRAnswerTXT* (pSQAnswer->addIP4Address(pIP4Address))) { pSQAnswer->m_u32ContentFlags |= ServiceQueryAnswerType_IP4Address; - 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(ServiceQueryAnswerType_IP4Address), true); } } else { @@ -1060,9 +1062,8 @@ bool MDNSResponder::_updateProbeStatus(void) { DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _updateProbeStatus: Done host probing.\n"));); m_HostProbeInformation.m_ProbingStatus = ProbingStatus_Done; m_HostProbeInformation.m_Timeout.reset(std::numeric_limits::max()); - - if (m_HostProbeInformation.m_fnProbeResultCallback) { - m_HostProbeInformation.m_fnProbeResultCallback(this, m_pcHostname, 0, true, m_HostProbeInformation.m_pProbeResultCallbackUserdata); + if (m_HostProbeInformation.m_fnHostProbeResultCallback) { + m_HostProbeInformation.m_fnHostProbeResultCallback(m_pcHostname, true); } // 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);); pService->m_ProbeInformation.m_ProbingStatus = ProbingStatus_Done; pService->m_ProbeInformation.m_Timeout.reset(std::numeric_limits::max()); - - MDNSProbeResultCallbackFn fnProbeResultCallback = 0; - void* pProbeResultCallbackUserdata = 0; - if (pService->m_ProbeInformation.m_fnProbeResultCallback) { - fnProbeResultCallback = pService->m_ProbeInformation.m_fnProbeResultCallback; - pProbeResultCallbackUserdata = pService->m_ProbeInformation.m_pProbeResultCallbackUserdata; + if (pService->m_ProbeInformation.m_fnServiceProbeResultCallback) { + pService->m_ProbeInformation.m_fnServiceProbeResultCallback(pService->m_pcName, pService, true); } - 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 pService->m_ProbeInformation.m_u8SentCount = 0; pService->m_ProbeInformation.m_Timeout.reset(MDNS_ANNOUNCE_DELAY); @@ -1287,10 +1276,9 @@ bool MDNSResponder::_cancelProbingForHost(void) { bool bResult = false; m_HostProbeInformation.clear(false); - // Send host notification - if (m_HostProbeInformation.m_fnProbeResultCallback) { - m_HostProbeInformation.m_fnProbeResultCallback(this, m_pcHostname, 0, false, m_HostProbeInformation.m_pProbeResultCallbackUserdata); + if (m_HostProbeInformation.m_fnHostProbeResultCallback) { + m_HostProbeInformation.m_fnHostProbeResultCallback(m_pcHostname, false); bResult = true; } @@ -1309,21 +1297,10 @@ bool MDNSResponder::_cancelProbingForService(stcMDNSService& p_rService) { bool bResult = false; p_rService.m_ProbeInformation.clear(false); - // Send notification - MDNSProbeResultCallbackFn fnProbeResultCallback = 0; - void* pProbeResultCallbackUserdata = 0; - if (p_rService.m_ProbeInformation.m_fnProbeResultCallback) { - 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; + if (p_rService.m_ProbeInformation.m_fnServiceProbeResultCallback) { + p_rService.m_ProbeInformation.m_fnServiceProbeResultCallback(p_rService.m_pcName,&p_rService,false); + bResult = true; } return bResult; } @@ -1506,7 +1483,8 @@ bool MDNSResponder::_checkServiceQueryCache(void) { else { // Timed out! -> Delete 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(ServiceQueryAnswerType_ServiceDomain), false); } DEBUG_EX_INFO( 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 pSQAnswer->m_u32ContentFlags &= ~u32ContentFlags; - 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(u32ContentFlags), false); } } } // HostDomainAndPort flagged @@ -1601,7 +1579,8 @@ bool MDNSResponder::_checkServiceQueryCache(void) { pSQAnswer->m_u32ContentFlags &= ~ServiceQueryAnswerType_Txts; 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(ServiceQueryAnswerType_Txts), false); } } } // TXTs flagged @@ -1648,7 +1627,8 @@ bool MDNSResponder::_checkServiceQueryCache(void) { } // Notify client 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(ServiceQueryAnswerType_IP4Address), false); } } } // IP4 flagged diff --git a/libraries/ESP8266mDNS/src/LEAmDNS_Helpers.cpp b/libraries/ESP8266mDNS/src/LEAmDNS_Helpers.cpp index f8042ff185..d99a57f24a 100644 --- a/libraries/ESP8266mDNS/src/LEAmDNS_Helpers.cpp +++ b/libraries/ESP8266mDNS/src/LEAmDNS_Helpers.cpp @@ -629,20 +629,27 @@ MDNSResponder::stcMDNSServiceTxt* MDNSResponder::_addServiceTxt(MDNSResponder::s 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 */ bool MDNSResponder::_collectServiceTxts(MDNSResponder::stcMDNSService& p_rService) { - bool bResult = (m_fnServiceTxtCallback - ? m_fnServiceTxtCallback(this, (hMDNSService)&p_rService, m_pServiceTxtCallbackUserdata) - : true); - - if ((bResult) && - (p_rService.m_fnTxtCallback)) { - bResult = p_rService.m_fnTxtCallback(this, (hMDNSService)&p_rService, p_rService.m_pTxtCallbackUserdata); + // Call Dynamic service callbacks + if (m_fnServiceTxtCallback) { + m_fnServiceTxtCallback((hMDNSService)&p_rService); } - return bResult; + if (p_rService.m_fnTxtCallback) { + p_rService.m_fnTxtCallback((hMDNSService)&p_rService); + } + return true; } /* diff --git a/libraries/ESP8266mDNS/src/LEAmDNS_Structs.cpp b/libraries/ESP8266mDNS/src/LEAmDNS_Structs.cpp index e41e4a08ba..41c38fe6b1 100644 --- a/libraries/ESP8266mDNS/src/LEAmDNS_Structs.cpp +++ b/libraries/ESP8266mDNS/src/LEAmDNS_Structs.cpp @@ -1162,8 +1162,8 @@ MDNSResponder::stcProbeInformation::stcProbeInformation(void) m_Timeout(std::numeric_limits::max()), m_bConflict(false), m_bTiebreakNeeded(false), - m_fnProbeResultCallback(0), - m_pProbeResultCallbackUserdata(0) { + m_fnHostProbeResultCallback(0), + m_fnServiceProbeResultCallback(0) { } /* @@ -1177,8 +1177,8 @@ bool MDNSResponder::stcProbeInformation::clear(bool p_bClearUserdata /*= false*/ m_bConflict = false; m_bTiebreakNeeded = false; if (p_bClearUserdata) { - m_fnProbeResultCallback = 0; - m_pProbeResultCallbackUserdata = 0; + m_fnHostProbeResultCallback = 0; + m_fnServiceProbeResultCallback = 0; } return true; } @@ -1207,8 +1207,7 @@ MDNSResponder::stcMDNSService::stcMDNSService(const char* p_pcName /*= 0*/, m_pcProtocol(0), m_u16Port(0), m_u8ReplyMask(0), - m_fnTxtCallback(0), - m_pTxtCallbackUserdata(0) { + m_fnTxtCallback(0) { setName(p_pcName); setService(p_pcService); @@ -1921,7 +1920,6 @@ MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcIP6Address* MDNSResponder::stc MDNSResponder::stcMDNSServiceQuery::stcMDNSServiceQuery(void) : m_pNext(0), m_fnCallback(0), - m_pUserdata(0), m_bLegacyQuery(false), m_u8SentCount(0), m_ResendTimeout(std::numeric_limits::max()), @@ -1945,7 +1943,6 @@ MDNSResponder::stcMDNSServiceQuery::~stcMDNSServiceQuery(void) { bool MDNSResponder::stcMDNSServiceQuery::clear(void) { m_fnCallback = 0; - m_pUserdata = 0; m_bLegacyQuery = false; m_u8SentCount = 0; m_ResendTimeout.reset(std::numeric_limits::max());