From c23c78674995079a1c49ceae3b1e6ab9fdafb454 Mon Sep 17 00:00:00 2001 From: stefan-sherwood Date: Tue, 7 Jan 2025 16:53:43 -0500 Subject: [PATCH] ESPmDNS: Add method for querying discovered service name (#10819) * Add method for querying service name * Added implementation for getting service name * Update mDNS-SD_Extended example to include service name * ci(pre-commit): Apply automatic fixes --------- Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> --- .../examples/mDNS-SD_Extended/mDNS-SD_Extended.ino | 2 ++ libraries/ESPmDNS/src/ESPmDNS.cpp | 9 +++++++++ libraries/ESPmDNS/src/ESPmDNS.h | 1 + 3 files changed, 12 insertions(+) diff --git a/libraries/ESPmDNS/examples/mDNS-SD_Extended/mDNS-SD_Extended.ino b/libraries/ESPmDNS/examples/mDNS-SD_Extended/mDNS-SD_Extended.ino index d2e48c0580c..e630d668cea 100644 --- a/libraries/ESPmDNS/examples/mDNS-SD_Extended/mDNS-SD_Extended.ino +++ b/libraries/ESPmDNS/examples/mDNS-SD_Extended/mDNS-SD_Extended.ino @@ -68,6 +68,8 @@ void browseService(const char *service, const char *proto) { Serial.print(" "); Serial.print(i + 1); Serial.print(": "); + Serial.print(MDNS.instanceName(i)); + Serial.print(" - "); Serial.print(MDNS.hostname(i)); Serial.print(" ("); Serial.print(MDNS.address(i)); diff --git a/libraries/ESPmDNS/src/ESPmDNS.cpp b/libraries/ESPmDNS/src/ESPmDNS.cpp index 4c15ed3a5dd..5d422513bbd 100644 --- a/libraries/ESPmDNS/src/ESPmDNS.cpp +++ b/libraries/ESPmDNS/src/ESPmDNS.cpp @@ -299,6 +299,15 @@ String MDNSResponder::hostname(int idx) { return String(result->hostname); } +String MDNSResponder::instanceName(int idx) { + mdns_result_t *result = _getResult(idx); + if (!result) { + log_e("Result %d not found", idx); + return String(); + } + return String(result->instance_name); +} + IPAddress MDNSResponder::address(int idx) { mdns_result_t *result = _getResult(idx); if (!result) { diff --git a/libraries/ESPmDNS/src/ESPmDNS.h b/libraries/ESPmDNS/src/ESPmDNS.h index 0336f476efe..1fb90bbf454 100644 --- a/libraries/ESPmDNS/src/ESPmDNS.h +++ b/libraries/ESPmDNS/src/ESPmDNS.h @@ -110,6 +110,7 @@ class MDNSResponder { } String hostname(int idx); + String instanceName(int idx); IPAddress address(int idx); IPAddress addressV6(int idx); uint16_t port(int idx);