From ec264247b265c2e93e5dfdc21d7500a34e934baf Mon Sep 17 00:00:00 2001 From: WanqQixiang Date: Mon, 21 Aug 2023 15:27:02 +0800 Subject: [PATCH] ESP32: Set the InterfaceId to Wi-Fi Station netif by default for platform mDNS --- src/platform/ESP32/WiFiDnssdImpl.cpp | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/platform/ESP32/WiFiDnssdImpl.cpp b/src/platform/ESP32/WiFiDnssdImpl.cpp index 79c839b27ecc2f..44f641f75e6bb6 100644 --- a/src/platform/ESP32/WiFiDnssdImpl.cpp +++ b/src/platform/ESP32/WiFiDnssdImpl.cpp @@ -19,6 +19,7 @@ #include "lib/dnssd/platform/Dnssd.h" #include +#include #include #include @@ -323,12 +324,22 @@ static CHIP_ERROR OnBrowseDone(BrowseContext * ctx) ctx->mService[servicesIndex].mAddressType = MapAddressType(currentResult->ip_protocol); ctx->mService[servicesIndex].mTransportType = ctx->mAddressType; ctx->mService[servicesIndex].mPort = currentResult->port; - ctx->mService[servicesIndex].mInterface = ctx->mInterfaceId; ctx->mService[servicesIndex].mTextEntries = GetTextEntry(currentResult->txt, currentResult->txt_value_len, currentResult->txt_count); ctx->mService[servicesIndex].mTextEntrySize = currentResult->txt_count; ctx->mService[servicesIndex].mSubTypes = NULL; ctx->mService[servicesIndex].mSubTypeSize = 0; + if (ctx->mInterfaceId == chip::Inet::InterfaceId::Null()) + { + // If the InterfaceId in the context is Null, we will use the Station netif by default. + struct netif * lwip_netif = + reinterpret_cast(esp_netif_get_netif_impl(esp_netif_get_handle_from_ifkey("WIFI_STA_DEF"))); + ctx->mService[servicesIndex].mInterface = chip::Inet::InterfaceId(lwip_netif); + } + else + { + ctx->mService[servicesIndex].mInterface = ctx->mInterfaceId; + } if (currentResult->addr) { Inet::IPAddress IPAddr; @@ -403,9 +414,20 @@ static CHIP_ERROR ParseSrvResult(ResolveContext * ctx) ctx->mService->mAddressType = MapAddressType(ctx->mSrvQueryResult->ip_protocol); ctx->mService->mTransportType = ctx->mService->mAddressType; ctx->mService->mPort = ctx->mSrvQueryResult->port; - ctx->mService->mInterface = ctx->mInterfaceId; ctx->mService->mSubTypes = nullptr; ctx->mService->mSubTypeSize = 0; + if (ctx->mInterfaceId == chip::Inet::InterfaceId::Null()) + { + // If the InterfaceId in the context is Null, we will use the Station netif by default. + struct netif * lwip_netif = + reinterpret_cast(esp_netif_get_netif_impl(esp_netif_get_handle_from_ifkey("WIFI_STA_DEF"))); + ctx->mService->mInterface = chip::Inet::InterfaceId(lwip_netif); + } + else + { + ctx->mService->mInterface = ctx->mInterfaceId; + } + return CHIP_NO_ERROR; } else