From 4928435911a441cb548a9a31ab32ed418adaea38 Mon Sep 17 00:00:00 2001 From: Wang Qixiang <43193572+wqx6@users.noreply.github.com> Date: Thu, 28 Mar 2024 13:09:18 +0800 Subject: [PATCH] ESP: Enable Wi-Fi and Ethernet coexisting and add platform mdns for Ethernet (#32428) * ESP32: Add platform mdns support for Ethernet network interface * Enable Wi-Fi and Ethernet coexisting * Restyled by clang-format * readme file changes * spell check * Restyled by prettier-markdown * review changes * Restyled by clang-format --------- Co-authored-by: Restyled.io --- config/esp32/components/chip/CMakeLists.txt | 5 +- examples/all-clusters-app/esp32/README.md | 19 ++++++ examples/all-clusters-app/esp32/main/main.cpp | 2 +- .../platform/esp32/common/Esp32AppServer.cpp | 16 +++-- src/platform/ESP32/BUILD.gn | 29 +++++---- src/platform/ESP32/CHIPDevicePlatformConfig.h | 13 ++-- .../ConnectivityManagerImpl_Ethernet.cpp | 54 ++++++++++------ src/platform/ESP32/DnssdImpl.cpp | 37 +++++------ .../{WiFiDnssdImpl.cpp => ESP32DnssdImpl.cpp} | 62 ++++++++++--------- .../{WiFiDnssdImpl.h => ESP32DnssdImpl.h} | 16 ++--- src/platform/ESP32/ESP32Utils.h | 1 + 11 files changed, 147 insertions(+), 107 deletions(-) rename src/platform/ESP32/{WiFiDnssdImpl.cpp => ESP32DnssdImpl.cpp} (91%) rename src/platform/ESP32/{WiFiDnssdImpl.h => ESP32DnssdImpl.h} (85%) diff --git a/config/esp32/components/chip/CMakeLists.txt b/config/esp32/components/chip/CMakeLists.txt index 14594a03b74352..672cf849af1244 100644 --- a/config/esp32/components/chip/CMakeLists.txt +++ b/config/esp32/components/chip/CMakeLists.txt @@ -441,16 +441,17 @@ if(CONFIG_OPENTHREAD_ENABLED) endif() endif() -if((NOT CONFIG_USE_MINIMAL_MDNS) AND (CONFIG_ENABLE_WIFI_STATION OR CONFIG_ENABLE_WIFI_AP)) +if(NOT CONFIG_USE_MINIMAL_MDNS) idf_build_get_property(build_components BUILD_COMPONENTS) # For IDF v5.x, the mdns component was moved to idf_managed_components. # We should use 'espressif__mdns' for 'idf_component_get_property'. if("espressif__mdns" IN_LIST build_components) idf_component_get_property(mdns_lib espressif__mdns COMPONENT_LIB) + list(APPEND chip_libraries $) elseif("mdns" IN_LIST build_components) idf_component_get_property(mdns_lib mdns COMPONENT_LIB) + list(APPEND chip_libraries $) endif() - list(APPEND chip_libraries $) endif() if (CONFIG_ENABLE_ENCRYPTED_OTA) diff --git a/examples/all-clusters-app/esp32/README.md b/examples/all-clusters-app/esp32/README.md index d7817027369adf..60ee180c1337fd 100644 --- a/examples/all-clusters-app/esp32/README.md +++ b/examples/all-clusters-app/esp32/README.md @@ -14,6 +14,7 @@ guides to get started. - [Cluster control](#cluster-control) - [Matter OTA guide](../../../docs/guides/esp32/ota.md) - [RPC console and Device Tracing](../../../docs/guides/esp32/rpc_console.md) +- [Multiple Network Interfaces](#multiple-network-interfaces) --- @@ -37,6 +38,24 @@ Usage: $ ./out/debug/chip-tool levelcontrol move-to-level Level=10 TransitionTime=0 OptionMask=0 OptionOverride=0 ``` +### Multiple Network Interfaces + +The data model of this example includes a secondary NetworkCommissioning +Endpoint with another NetworkCommissioning cluster. The Endpoint Id for the +secondary NetworkCommissioning Endpoint is 65534. The secondary +NetworkCommissioning Endpoint can be used to manage the driver of extra network +interface. + +For ESP32-C6 DevKits, if `CHIP_DEVICE_CONFIG_ENABLE_WIFI` and +`CHIP_DEVICE_CONFIG_ENABLE_THREAD` are both enabled, the NetworkCommissioning +cluster in Endpoint 0 will be used for Thread network driver and the same +cluster on Endpoint 65534 will be used for Wi-Fi network driver. + +For ESP32-Ethernet-Kits, if `CHIP_DEVICE_CONFIG_ENABLE_WIFI` and +`CHIP_DEVICE_CONFIG_ENABLE_ETHERNET` are both enabled, the NetworkCommissioning +cluster in Endpoint 0 will be used for Ethernet network driver and the same +cluster on Endpoint 65534 will be used for Wi-Fi network driver. + --- This demo app illustrates controlling OnOff cluster (Server) attributes of an diff --git a/examples/all-clusters-app/esp32/main/main.cpp b/examples/all-clusters-app/esp32/main/main.cpp index 4893cf1b8f1475..a3af54f3a22e06 100644 --- a/examples/all-clusters-app/esp32/main/main.cpp +++ b/examples/all-clusters-app/esp32/main/main.cpp @@ -114,7 +114,7 @@ static void InitServer(intptr_t context) { Esp32AppServer::Init(&sCallbacks); // Init ZCL Data Model and CHIP App Server AND Initialize device attestation config -#if !(CHIP_DEVICE_CONFIG_ENABLE_WIFI && CHIP_DEVICE_CONFIG_ENABLE_THREAD) +#if !(CHIP_DEVICE_CONFIG_ENABLE_WIFI && (CHIP_DEVICE_CONFIG_ENABLE_THREAD || CHIP_DEVICE_CONFIG_ENABLE_ETHERNET)) // We only have network commissioning on endpoint 0. emberAfEndpointEnableDisable(kNetworkCommissioningEndpointSecondary, false); #endif diff --git a/examples/platform/esp32/common/Esp32AppServer.cpp b/examples/platform/esp32/common/Esp32AppServer.cpp index b60685d34debca..038ff055413e64 100644 --- a/examples/platform/esp32/common/Esp32AppServer.cpp +++ b/examples/platform/esp32/common/Esp32AppServer.cpp @@ -51,19 +51,20 @@ static constexpr char TAG[] = "ESP32Appserver"; namespace { #if CHIP_DEVICE_CONFIG_ENABLE_WIFI -#if CHIP_DEVICE_CONFIG_ENABLE_THREAD +#if CHIP_DEVICE_CONFIG_ENABLE_THREAD || CHIP_DEVICE_CONFIG_ENABLE_ETHERNET constexpr chip::EndpointId kNetworkCommissioningEndpointWiFi = 0xFFFE; #else constexpr chip::EndpointId kNetworkCommissioningEndpointWiFi = 0; #endif app::Clusters::NetworkCommissioning::Instance sWiFiNetworkCommissioningInstance(kNetworkCommissioningEndpointWiFi, &(NetworkCommissioning::ESPWiFiDriver::GetInstance())); -#elif CHIP_DEVICE_CONFIG_ENABLE_ETHERNET +#endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI +#if CHIP_DEVICE_CONFIG_ENABLE_ETHERNET static app::Clusters::NetworkCommissioning::Instance sEthernetNetworkCommissioningInstance(0 /* Endpoint Id */, &(NetworkCommissioning::ESPEthernetDriver::GetInstance())); #endif -#if CONFIG_TEST_EVENT_TRIGGER_ENABLED +#if CONFIG_TEST_EVENT_TRIGGER_ENABLED && CONFIG_ENABLE_OTA_REQUESTOR static uint8_t sTestEventTriggerEnableKey[TestEventTriggerDelegate::kEnableKeyLength] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff }; @@ -73,7 +74,7 @@ static ICDSubscriptionCallback sICDSubscriptionHandler; #endif } // namespace -#if CONFIG_TEST_EVENT_TRIGGER_ENABLED +#if CONFIG_TEST_EVENT_TRIGGER_ENABLED && CONFIG_ENABLE_OTA_REQUESTOR static int hex_digit_to_int(char hex) { if ('A' <= hex && hex <= 'F') @@ -112,7 +113,7 @@ static size_t hex_string_to_binary(const char * hex_string, uint8_t * buf, size_ return buf_size; } -#endif // CONFIG_TEST_EVENT_TRIGGER_ENABLED +#endif // CONFIG_TEST_EVENT_TRIGGER_ENABLED && CONFIG_ENABLE_OTA_REQUESTOR void Esp32AppServer::DeInitBLEIfCommissioned(void) { @@ -175,7 +176,7 @@ void Esp32AppServer::Init(AppDelegate * sAppDelegate) VerifyOrDie(sTestEventTriggerDelegate.Init(ByteSpan(sTestEventTriggerEnableKey)) == CHIP_NO_ERROR); VerifyOrDie(sTestEventTriggerDelegate.AddHandler(&sOtaTestEventTriggerHandler) == CHIP_NO_ERROR); initParams.testEventTriggerDelegate = &sTestEventTriggerDelegate; -#endif // CONFIG_TEST_EVENT_TRIGGER_ENABLED +#endif // CONFIG_TEST_EVENT_TRIGGER_ENABLED && CONFIG_ENABLE_OTA_REQUESTOR (void) initParams.InitializeStaticResourcesBeforeServerInit(); if (sAppDelegate != nullptr) { @@ -190,6 +191,9 @@ void Esp32AppServer::Init(AppDelegate * sAppDelegate) #if CHIP_DEVICE_CONFIG_ENABLE_WIFI sWiFiNetworkCommissioningInstance.Init(); #endif +#if CHIP_DEVICE_CONFIG_ENABLE_ETHERNET + sEthernetNetworkCommissioningInstance.Init(); +#endif #if CHIP_DEVICE_CONFIG_ENABLE_THREAD if (chip::DeviceLayer::ConnectivityMgr().IsThreadProvisioned() && (chip::Server::GetInstance().GetFabricTable().FabricCount() != 0)) diff --git a/src/platform/ESP32/BUILD.gn b/src/platform/ESP32/BUILD.gn index 768f6a25ba0e64..b1d3650769c468 100644 --- a/src/platform/ESP32/BUILD.gn +++ b/src/platform/ESP32/BUILD.gn @@ -122,10 +122,24 @@ static_library("ESP32") { "NetworkCommissioningDriver.cpp", "NetworkCommissioningDriver.h", ] + } + + if (chip_mdns == "platform") { + sources += [ "DnssdImpl.cpp" ] + } + + if (chip_enable_ethernet) { + sources += [ + "ConnectivityManagerImpl_Ethernet.cpp", + "NetworkCommissioningDriver_Ethernet.cpp", + ] + } + + if (chip_enable_ethernet || chip_enable_wifi) { if (chip_mdns == "platform") { sources += [ - "WiFiDnssdImpl.cpp", - "WiFiDnssdImpl.h", + "ESP32DnssdImpl.cpp", + "ESP32DnssdImpl.h", ] } if (chip_mdns == "minimal") { @@ -141,17 +155,6 @@ static_library("ESP32") { } } - if (chip_mdns == "platform") { - sources += [ "DnssdImpl.cpp" ] - } - - if (chip_enable_ethernet) { - sources += [ - "ConnectivityManagerImpl_Ethernet.cpp", - "NetworkCommissioningDriver_Ethernet.cpp", - ] - } - if (chip_enable_openthread) { sources += [ "../OpenThread/GenericNetworkCommissioningThreadDriver.cpp", diff --git a/src/platform/ESP32/CHIPDevicePlatformConfig.h b/src/platform/ESP32/CHIPDevicePlatformConfig.h index f0f28408a3ce12..ef6f850b36fb14 100644 --- a/src/platform/ESP32/CHIPDevicePlatformConfig.h +++ b/src/platform/ESP32/CHIPDevicePlatformConfig.h @@ -53,11 +53,12 @@ #define CHIP_DEVICE_CONFIG_ENABLE_THREAD_DNS_CLIENT CONFIG_OPENTHREAD_DNS_CLIENT #if CONFIG_ENABLE_ETHERNET_TELEMETRY -#define CHIP_DEVICE_CONFIG_ENABLE_WIFI 0 #define CHIP_DEVICE_CONFIG_ENABLE_ETHERNET 1 -#elif CONFIG_IDF_TARGET_ESP32H2 -#define CHIP_DEVICE_CONFIG_ENABLE_WIFI 0 -#else +#endif + +#define CHIP_DEVICE_CONFIG_ENABLE_WIFI CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP | CHIP_DEVICE_CONFIG_ENABLE_WIFI_STATION +#if CHIP_DEVICE_CONFIG_ENABLE_WIFI +#define CHIP_DEVICE_CONFIG_ENABLE_WIFI_TELEMETRY CONFIG_ENABLE_WIFI_TELEMETRY #define CHIP_DEVICE_CONFIG_WIFI_STATION_RECONNECT_INTERVAL CONFIG_WIFI_STATION_RECONNECT_INTERVAL #define CHIP_DEVICE_CONFIG_MAX_SCAN_NETWORKS_RESULTS CONFIG_MAX_SCAN_NETWORKS_RESULTS #define CHIP_DEVICE_CONFIG_WIFI_SCAN_COMPLETION_TIMEOUT CONFIG_WIFI_SCAN_COMPLETION_TIMEOUT @@ -69,9 +70,7 @@ #define CHIP_DEVICE_CONFIG_WIFI_AP_BEACON_INTERVAL CONFIG_WIFI_AP_BEACON_INTERVAL #define CHIP_DEVICE_CONFIG_WIFI_AP_IDLE_TIMEOUT CONFIG_WIFI_AP_IDLE_TIMEOUT #endif /* CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP */ -#define CHIP_DEVICE_CONFIG_ENABLE_WIFI_TELEMETRY CONFIG_ENABLE_WIFI_TELEMETRY -#define CHIP_DEVICE_CONFIG_ENABLE_WIFI CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP | CHIP_DEVICE_CONFIG_ENABLE_WIFI_STATION -#endif // CONFIG_IDF_TARGET_ESP32H2 +#endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI #if CONFIG_ENABLE_ICD_SERVER #define CHIP_DEVICE_CONFIG_ICD_SLOW_POLL_INTERVAL chip::System::Clock::Milliseconds32(CONFIG_ICD_SLOW_POLL_INTERVAL_MS) diff --git a/src/platform/ESP32/ConnectivityManagerImpl_Ethernet.cpp b/src/platform/ESP32/ConnectivityManagerImpl_Ethernet.cpp index d8f7137047ebac..b36c4b8cb273d2 100644 --- a/src/platform/ESP32/ConnectivityManagerImpl_Ethernet.cpp +++ b/src/platform/ESP32/ConnectivityManagerImpl_Ethernet.cpp @@ -30,7 +30,7 @@ #include #include -#include "esp_eth_com.h" +#include "esp_eth.h" #include "esp_event.h" #include "esp_netif.h" #include "esp_wifi.h" @@ -88,28 +88,42 @@ void ConnectivityManagerImpl::OnEthernetIPv6AddressAvailable(const ip_event_got_ void ConnectivityManagerImpl::OnEthernetPlatformEvent(const ChipDeviceEvent * event) { - switch (event->Platform.ESPSystemEvent.Id) + if (event->Type == DeviceEventType::kESPSystemEvent) { - case IP_EVENT_ETH_GOT_IP: - OnEthernetIPv4AddressAvailable(event->Platform.ESPSystemEvent.Data.IpGotIp); - break; - case IP_EVENT_ETH_LOST_IP: - OnEthernetIPv4AddressLost(); - break; - case IP_EVENT_GOT_IP6: - if (strcmp(esp_netif_get_ifkey(event->Platform.ESPSystemEvent.Data.IpGotIp6.esp_netif), "ETH_DEF") == 0) + if (event->Platform.ESPSystemEvent.Base == IP_EVENT) { - OnEthernetIPv6AddressAvailable(event->Platform.ESPSystemEvent.Data.IpGotIp6); + switch (event->Platform.ESPSystemEvent.Id) + { + case IP_EVENT_ETH_GOT_IP: + OnEthernetIPv4AddressAvailable(event->Platform.ESPSystemEvent.Data.IpGotIp); + break; + case IP_EVENT_ETH_LOST_IP: + OnEthernetIPv4AddressLost(); + break; + case IP_EVENT_GOT_IP6: + if (strcmp(esp_netif_get_ifkey(event->Platform.ESPSystemEvent.Data.IpGotIp6.esp_netif), "ETH_DEF") == 0) + { + OnEthernetIPv6AddressAvailable(event->Platform.ESPSystemEvent.Data.IpGotIp6); + } + break; + default: + break; + } + } + else if (event->Platform.ESPSystemEvent.Base == ETH_EVENT) + { + switch (event->Platform.ESPSystemEvent.Id) + { + case ETHERNET_EVENT_START: + ChipLogProgress(DeviceLayer, "Ethernet Started"); + break; + case ETHERNET_EVENT_STOP: + ChipLogProgress(DeviceLayer, "Ethernet Stopped"); + break; + default: + break; + } } - break; - case ETHERNET_EVENT_START: - ChipLogProgress(DeviceLayer, "Ethernet Started"); - break; - case ETHERNET_EVENT_STOP: - ChipLogProgress(DeviceLayer, "Ethernet Stopped"); - break; - default: - break; } } diff --git a/src/platform/ESP32/DnssdImpl.cpp b/src/platform/ESP32/DnssdImpl.cpp index 37d33f9d13903a..1ca07213a866c6 100644 --- a/src/platform/ESP32/DnssdImpl.cpp +++ b/src/platform/ESP32/DnssdImpl.cpp @@ -17,6 +17,7 @@ #include "lib/dnssd/platform/Dnssd.h" #include "platform/CHIPDeviceLayer.h" +#include "platform/ESP32/ESP32Utils.h" #include #include @@ -24,8 +25,8 @@ #if CHIP_DEVICE_CONFIG_ENABLE_THREAD #include #endif -#if CHIP_DEVICE_CONFIG_ENABLE_WIFI -#include +#if CHIP_DEVICE_CONFIG_ENABLE_WIFI || CHIP_DEVICE_CONFIG_ENABLE_ETHERNET +#include #endif using namespace ::chip::DeviceLayer; @@ -35,8 +36,8 @@ namespace Dnssd { CHIP_ERROR ChipDnssdInit(DnssdAsyncReturnCallback initCallback, DnssdAsyncReturnCallback errorCallback, void * context) { -#if CHIP_DEVICE_CONFIG_ENABLE_WIFI - ReturnErrorOnFailure(WiFiDnssdInit(initCallback, errorCallback, context)); +#if CHIP_DEVICE_CONFIG_ENABLE_WIFI || CHIP_DEVICE_CONFIG_ENABLE_ETHERNET + ReturnErrorOnFailure(EspDnssdInit(initCallback, errorCallback, context)); #endif #if CHIP_DEVICE_CONFIG_ENABLE_THREAD ReturnErrorOnFailure(OpenThreadDnssdInit(initCallback, errorCallback, context)); @@ -48,11 +49,8 @@ void ChipDnssdShutdown() {} CHIP_ERROR ChipDnssdPublishService(const DnssdService * service, DnssdPublishCallback callback, void * context) { -#if CHIP_DEVICE_CONFIG_ENABLE_WIFI - if (ConnectivityMgr().IsWiFiStationProvisioned()) - { - ReturnErrorOnFailure(WiFiDnssdPublishService(service, callback, context)); - } +#if CHIP_DEVICE_CONFIG_ENABLE_WIFI || CHIP_DEVICE_CONFIG_ENABLE_ETHERNET + ReturnErrorOnFailure(EspDnssdPublishService(service, callback, context)); #endif #if CHIP_DEVICE_CONFIG_ENABLE_THREAD if (ConnectivityMgr().IsThreadProvisioned()) @@ -65,11 +63,8 @@ CHIP_ERROR ChipDnssdPublishService(const DnssdService * service, DnssdPublishCal CHIP_ERROR ChipDnssdRemoveServices() { -#if CHIP_DEVICE_CONFIG_ENABLE_WIFI - if (ConnectivityMgr().IsWiFiStationProvisioned()) - { - ReturnErrorOnFailure(WiFiDnssdRemoveServices()); - } +#if CHIP_DEVICE_CONFIG_ENABLE_WIFI || CHIP_DEVICE_CONFIG_ENABLE_ETHERNET + ReturnErrorOnFailure(EspDnssdRemoveServices()); #endif #if CHIP_DEVICE_CONFIG_ENABLE_THREAD if (ConnectivityMgr().IsThreadProvisioned()) @@ -95,10 +90,11 @@ CHIP_ERROR ChipDnssdBrowse(const char * type, DnssdServiceProtocol protocol, chi chip::Inet::InterfaceId interface, DnssdBrowseCallback callback, void * context, intptr_t * browseIdentifier) { -#if CHIP_DEVICE_CONFIG_ENABLE_WIFI - if (ConnectivityMgr().IsWiFiStationProvisioned()) +#if CHIP_DEVICE_CONFIG_ENABLE_WIFI || CHIP_DEVICE_CONFIG_ENABLE_ETHERNET + if (ConnectivityMgr().IsWiFiStationProvisioned() || + Internal::ESP32Utils::HasIPv6LinkLocalAddress(Internal::ESP32Utils::kDefaultEthernetNetifKey)) { - ReturnErrorOnFailure(WiFiDnssdBrowse(type, protocol, addressType, interface, callback, context, browseIdentifier)); + ReturnErrorOnFailure(EspDnssdBrowse(type, protocol, addressType, interface, callback, context, browseIdentifier)); } #endif #if CHIP_DEVICE_CONFIG_ENABLE_THREAD @@ -118,10 +114,11 @@ CHIP_ERROR ChipDnssdStopBrowse(intptr_t browseIdentifier) CHIP_ERROR ChipDnssdResolve(DnssdService * service, chip::Inet::InterfaceId interface, DnssdResolveCallback callback, void * context) { -#if CHIP_DEVICE_CONFIG_ENABLE_WIFI - if (ConnectivityMgr().IsWiFiStationProvisioned()) +#if CHIP_DEVICE_CONFIG_ENABLE_WIFI || CHIP_DEVICE_CONFIG_ENABLE_ETHERNET + if (ConnectivityMgr().IsWiFiStationProvisioned() || + Internal::ESP32Utils::HasIPv6LinkLocalAddress(Internal::ESP32Utils::kDefaultEthernetNetifKey)) { - ReturnErrorOnFailure(WiFiDnssdResolve(service, interface, callback, context)); + ReturnErrorOnFailure(EspDnssdResolve(service, interface, callback, context)); } #endif #if CHIP_DEVICE_CONFIG_ENABLE_THREAD diff --git a/src/platform/ESP32/WiFiDnssdImpl.cpp b/src/platform/ESP32/ESP32DnssdImpl.cpp similarity index 91% rename from src/platform/ESP32/WiFiDnssdImpl.cpp rename to src/platform/ESP32/ESP32DnssdImpl.cpp index b927dd28fec0f2..2c896a0d5904db 100644 --- a/src/platform/ESP32/WiFiDnssdImpl.cpp +++ b/src/platform/ESP32/ESP32DnssdImpl.cpp @@ -15,7 +15,7 @@ * limitations under the License. */ -#include "WiFiDnssdImpl.h" +#include "ESP32DnssdImpl.h" #include "lib/dnssd/platform/Dnssd.h" #include @@ -130,7 +130,7 @@ static CHIP_ERROR RemoveMdnsQuery(GenericContext * ctx) return CHIP_NO_ERROR; } -CHIP_ERROR WiFiDnssdInit(DnssdAsyncReturnCallback initCallback, DnssdAsyncReturnCallback errorCallback, void * context) +CHIP_ERROR EspDnssdInit(DnssdAsyncReturnCallback initCallback, DnssdAsyncReturnCallback errorCallback, void * context) { CHIP_ERROR error = CHIP_NO_ERROR; esp_err_t espError; @@ -153,7 +153,7 @@ static const char * GetProtocolString(DnssdServiceProtocol protocol) return protocol == DnssdServiceProtocol::kDnssdProtocolTcp ? "_tcp" : "_udp"; } -CHIP_ERROR WiFiDnssdPublishService(const DnssdService * service, DnssdPublishCallback callback, void * context) +CHIP_ERROR EspDnssdPublishService(const DnssdService * service, DnssdPublishCallback callback, void * context) { CHIP_ERROR error = CHIP_NO_ERROR; mdns_txt_item_t * items = nullptr; @@ -165,7 +165,7 @@ CHIP_ERROR WiFiDnssdPublishService(const DnssdService * service, DnssdPublishCal } VerifyOrExit(service->mTextEntrySize <= UINT8_MAX, error = CHIP_ERROR_INVALID_ARGUMENT); - if (service->mTextEntries) + if (service->mTextEntries && service->mTextEntrySize > 0) { items = static_cast(chip::Platform::MemoryCalloc(service->mTextEntrySize, sizeof(mdns_txt_item_t))); VerifyOrExit(items != nullptr, error = CHIP_ERROR_NO_MEMORY); @@ -203,13 +203,30 @@ CHIP_ERROR WiFiDnssdPublishService(const DnssdService * service, DnssdPublishCal return error; } -CHIP_ERROR WiFiDnssdRemoveServices() +CHIP_ERROR EspDnssdRemoveServices() { mdns_service_remove("_matter", "_tcp"); mdns_service_remove("_matterc", "_udp"); + mdns_service_remove("_matterd", "_udp"); return CHIP_NO_ERROR; } +static Inet::InterfaceId GetServiceInterfaceId(esp_netif_t * esp_netif) +{ + if (!esp_netif) + { + // If the InterfaceId in the context and esp_netif in current result is Null, + // we will use the Station or Ethernet netif by default. +#if CHIP_DEVICE_CONFIG_ENABLE_WIFI + return Inet::InterfaceId(DeviceLayer::Internal::ESP32Utils::GetStationNetif()); +#elif CHIP_DEVICE_CONFIG_ENABLE_ETHERNET + return Inet::InterfaceId( + DeviceLayer::Internal::ESP32Utils::GetNetif(DeviceLayer::Internal::ESP32Utils::kDefaultEthernetNetifKey)); +#endif + } + return Inet::InterfaceId(static_cast(esp_netif_get_netif_impl(esp_netif))); +} + static Inet::IPAddressType MapAddressType(mdns_ip_protocol_t ip_protocol) { switch (ip_protocol) @@ -330,16 +347,9 @@ static CHIP_ERROR OnBrowseDone(BrowseContext * ctx) 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. - ctx->mService[servicesIndex].mInterface = - Inet::InterfaceId(DeviceLayer::Internal::ESP32Utils::GetStationNetif()); - } - else - { - ctx->mService[servicesIndex].mInterface = ctx->mInterfaceId; - } + ctx->mService[servicesIndex].mInterface = ctx->mInterfaceId != chip::Inet::InterfaceId::Null() + ? ctx->mInterfaceId + : GetServiceInterfaceId(currentResult->esp_netif); if (currentResult->addr) { Inet::IPAddress IPAddr; @@ -416,16 +426,9 @@ static CHIP_ERROR ParseSrvResult(ResolveContext * ctx) ctx->mService->mPort = ctx->mSrvQueryResult->port; 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. - ctx->mService->mInterface = Inet::InterfaceId(DeviceLayer::Internal::ESP32Utils::GetStationNetif()); - } - else - { - ctx->mService->mInterface = ctx->mInterfaceId; - } - + ctx->mService->mInterface = ctx->mInterfaceId != chip::Inet::InterfaceId::Null() + ? ctx->mInterfaceId + : GetServiceInterfaceId(ctx->mSrvQueryResult->esp_netif); return CHIP_NO_ERROR; } else @@ -595,9 +598,9 @@ static void MdnsQueryNotifier(mdns_search_once_t * searchHandle) chip::DeviceLayer::PlatformMgr().ScheduleWork(MdnsQueryDone, reinterpret_cast(searchHandle)); } -CHIP_ERROR WiFiDnssdBrowse(const char * type, DnssdServiceProtocol protocol, chip::Inet::IPAddressType addressType, - chip::Inet::InterfaceId interface, DnssdBrowseCallback callback, void * context, - intptr_t * browseIdentifier) +CHIP_ERROR EspDnssdBrowse(const char * type, DnssdServiceProtocol protocol, chip::Inet::IPAddressType addressType, + chip::Inet::InterfaceId interface, DnssdBrowseCallback callback, void * context, + intptr_t * browseIdentifier) { CHIP_ERROR error = CHIP_NO_ERROR; mdns_search_once_t * queryHandle = @@ -623,8 +626,7 @@ CHIP_ERROR WiFiDnssdBrowse(const char * type, DnssdServiceProtocol protocol, chi return error; } -CHIP_ERROR WiFiDnssdResolve(DnssdService * service, chip::Inet::InterfaceId interface, DnssdResolveCallback callback, - void * context) +CHIP_ERROR EspDnssdResolve(DnssdService * service, chip::Inet::InterfaceId interface, DnssdResolveCallback callback, void * context) { CHIP_ERROR error = CHIP_NO_ERROR; mdns_search_once_t * querySrv = mdns_query_async_new(service->mName, service->mType, GetProtocolString(service->mProtocol), diff --git a/src/platform/ESP32/WiFiDnssdImpl.h b/src/platform/ESP32/ESP32DnssdImpl.h similarity index 85% rename from src/platform/ESP32/WiFiDnssdImpl.h rename to src/platform/ESP32/ESP32DnssdImpl.h index 1f1c378a414cb2..aa7f0f6737aa89 100644 --- a/src/platform/ESP32/WiFiDnssdImpl.h +++ b/src/platform/ESP32/ESP32DnssdImpl.h @@ -139,18 +139,18 @@ struct ResolveContext : public GenericContext } }; -CHIP_ERROR WiFiDnssdInit(DnssdAsyncReturnCallback initCallback, DnssdAsyncReturnCallback errorCallback, void * context); +CHIP_ERROR EspDnssdInit(DnssdAsyncReturnCallback initCallback, DnssdAsyncReturnCallback errorCallback, void * context); -CHIP_ERROR WiFiDnssdPublishService(const DnssdService * service, DnssdPublishCallback callback, void * context); +CHIP_ERROR EspDnssdPublishService(const DnssdService * service, DnssdPublishCallback callback, void * context); -CHIP_ERROR WiFiDnssdRemoveServices(); +CHIP_ERROR EspDnssdRemoveServices(); -CHIP_ERROR WiFiDnssdBrowse(const char * type, DnssdServiceProtocol protocol, chip::Inet::IPAddressType addressType, - chip::Inet::InterfaceId interface, DnssdBrowseCallback callback, void * context, - intptr_t * browseIdentifier); +CHIP_ERROR EspDnssdBrowse(const char * type, DnssdServiceProtocol protocol, chip::Inet::IPAddressType addressType, + chip::Inet::InterfaceId interface, DnssdBrowseCallback callback, void * context, + intptr_t * browseIdentifier); -CHIP_ERROR WiFiDnssdResolve(DnssdService * service, chip::Inet::InterfaceId interface, DnssdResolveCallback callback, - void * context); +CHIP_ERROR EspDnssdResolve(DnssdService * service, chip::Inet::InterfaceId interface, DnssdResolveCallback callback, + void * context); } // namespace Dnssd } // namespace chip diff --git a/src/platform/ESP32/ESP32Utils.h b/src/platform/ESP32/ESP32Utils.h index 1c052eee6f64b8..b82734c540da20 100644 --- a/src/platform/ESP32/ESP32Utils.h +++ b/src/platform/ESP32/ESP32Utils.h @@ -54,6 +54,7 @@ class ESP32Utils static constexpr char kDefaultWiFiStationNetifKey[] = "WIFI_STA_DEF"; static constexpr char kDefaultWiFiAPNetifKey[] = "WIFI_AP_DEF"; + static constexpr char kDefaultEthernetNetifKey[] = "ETH_DEF"; }; #define ReturnMappedErrorOnFailure(expr) \