From 03ef19745bf85286ba3bc107f510d3529949817e Mon Sep 17 00:00:00 2001 From: chendejin Date: Fri, 8 Mar 2024 16:42:47 +0800 Subject: [PATCH] test for thread border router deinit, ethernet and wifi --- config/esp32/components/chip/CMakeLists.txt | 5 +- examples/all-clusters-app/esp32/main/main.cpp | 2 +- .../platform/esp32/common/Esp32AppServer.cpp | 23 +++++--- src/platform/ESP32/BUILD.gn | 29 +++++----- src/platform/ESP32/CHIPDevicePlatformConfig.h | 13 +++-- .../ConnectivityManagerImpl_Ethernet.cpp | 53 ++++++++++++------- src/platform/ESP32/DnssdImpl.cpp | 37 ++++++------- .../{WiFiDnssdImpl.cpp => ESP32DnssdImpl.cpp} | 52 +++++++++++++----- .../{WiFiDnssdImpl.h => ESP32DnssdImpl.h} | 16 +++--- src/platform/ESP32/ESP32Utils.h | 1 + .../NetworkCommissioningDriver_Ethernet.cpp | 49 ++++++++++++++--- 11 files changed, 183 insertions(+), 97 deletions(-) rename src/platform/ESP32/{WiFiDnssdImpl.cpp => ESP32DnssdImpl.cpp} (90%) 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 a3ad2d9c438619..ee50f05da6dac2 100644 --- a/config/esp32/components/chip/CMakeLists.txt +++ b/config/esp32/components/chip/CMakeLists.txt @@ -416,16 +416,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/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 8baafcb458a2d7..7e89c6fcd4cf7f 100644 --- a/examples/platform/esp32/common/Esp32AppServer.cpp +++ b/examples/platform/esp32/common/Esp32AppServer.cpp @@ -50,19 +50,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 }; @@ -72,7 +73,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') @@ -111,7 +112,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) { @@ -168,9 +169,12 @@ void Esp32AppServer::Init(AppDelegate * sAppDelegate) ESP_LOGE(TAG, "Failed to convert the EnableKey string to octstr type value"); memset(sTestEventTriggerEnableKey, 0, sizeof(sTestEventTriggerEnableKey)); } - static OTATestEventTriggerDelegate testEventTriggerDelegate{ ByteSpan(sTestEventTriggerEnableKey) }; - initParams.testEventTriggerDelegate = &testEventTriggerDelegate; -#endif // CONFIG_TEST_EVENT_TRIGGER_ENABLED + static SimpleTestEventTriggerDelegate sTestEventTriggerDelegate{}; + static OTATestEventTriggerHandler sOtaTestEventTriggerHandler{}; + VerifyOrDie(sTestEventTriggerDelegate.Init(ByteSpan(sTestEventTriggerEnableKey)) == CHIP_NO_ERROR); + VerifyOrDie(sTestEventTriggerDelegate.AddHandler(&sOtaTestEventTriggerHandler) == CHIP_NO_ERROR); + initParams.testEventTriggerDelegate = &sTestEventTriggerDelegate; +#endif // CONFIG_TEST_EVENT_TRIGGER_ENABLED && CONFIG_ENABLE_OTA_REQUESTOR (void) initParams.InitializeStaticResourcesBeforeServerInit(); if (sAppDelegate != nullptr) { @@ -185,6 +189,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 c5b59add8e9e34..360a9cc2b9054d 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_enable_route_hook) { @@ -138,17 +152,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..9d219f2e27dada 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,41 @@ 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) { + 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) { - OnEthernetIPv6AddressAvailable(event->Platform.ESPSystemEvent.Data.IpGotIp6); + 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 90% rename from src/platform/ESP32/WiFiDnssdImpl.cpp rename to src/platform/ESP32/ESP32DnssdImpl.cpp index b927dd28fec0f2..df4c402681654d 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,10 +203,11 @@ 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; } @@ -332,9 +333,22 @@ static CHIP_ERROR OnBrowseDone(BrowseContext * ctx) 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()); + if (currentResult->esp_netif) + { + ctx->mService[servicesIndex].mInterface = + Inet::InterfaceId(static_cast(esp_netif_get_netif_impl(currentResult->esp_netif))); + } + else + { + // If the InterfaceId in the context and esp_netif in current result is Null, + // we will use the Station or Ethernet netif by default. + ctx->mService[servicesIndex].mInterface = +#if CHIP_DEVICE_CONFIG_ENABLE_WIFI + Inet::InterfaceId(DeviceLayer::Internal::ESP32Utils::GetStationNetif()); +#elif CHIP_DEVICE_CONFIG_ENABLE_ETHERNET + Inet::InterfaceId(DeviceLayer::Internal::ESP32Utils::GetNetif(DeviceLayer::Internal::ESP32Utils::kDefaultEthernetNetifKey)); +#endif + } } else { @@ -418,8 +432,22 @@ static CHIP_ERROR ParseSrvResult(ResolveContext * ctx) 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()); + if (ctx->mSrvQueryResult->esp_netif) + { + ctx->mService->mInterface = + Inet::InterfaceId(static_cast(esp_netif_get_netif_impl(ctx->mSrvQueryResult->esp_netif))); + } + else + { + // If the InterfaceId in the context and esp_netif in current result is Null, + // we will use the Station or Ethernet netif by default. + ctx->mService->mInterface = +#if CHIP_DEVICE_CONFIG_ENABLE_WIFI + Inet::InterfaceId(DeviceLayer::Internal::ESP32Utils::GetStationNetif()); +#elif CHIP_DEVICE_CONFIG_ENABLE_ETHERNET + Inet::InterfaceId(DeviceLayer::Internal::ESP32Utils::GetNetif(DeviceLayer::Internal::ESP32Utils::kDefaultEthernetNetifKey)); +#endif + } } else { @@ -595,7 +623,7 @@ 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_ERROR EspDnssdBrowse(const char * type, DnssdServiceProtocol protocol, chip::Inet::IPAddressType addressType, chip::Inet::InterfaceId interface, DnssdBrowseCallback callback, void * context, intptr_t * browseIdentifier) { @@ -623,7 +651,7 @@ CHIP_ERROR WiFiDnssdBrowse(const char * type, DnssdServiceProtocol protocol, chi return error; } -CHIP_ERROR WiFiDnssdResolve(DnssdService * service, chip::Inet::InterfaceId interface, DnssdResolveCallback callback, +CHIP_ERROR EspDnssdResolve(DnssdService * service, chip::Inet::InterfaceId interface, DnssdResolveCallback callback, void * context) { CHIP_ERROR error = CHIP_NO_ERROR; 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) \ diff --git a/src/platform/ESP32/NetworkCommissioningDriver_Ethernet.cpp b/src/platform/ESP32/NetworkCommissioningDriver_Ethernet.cpp index f8cce41c1e69ed..6db617a2cfb713 100644 --- a/src/platform/ESP32/NetworkCommissioningDriver_Ethernet.cpp +++ b/src/platform/ESP32/NetworkCommissioningDriver_Ethernet.cpp @@ -17,6 +17,7 @@ #include "esp_eth.h" #include "esp_eth_mac.h" #include "esp_eth_phy.h" +#include "driver/gpio.h" #include using namespace ::chip; @@ -35,6 +36,11 @@ static void on_eth_event(void * esp_netif, esp_event_base_t event_base, int32_t ESP_ERROR_CHECK(esp_netif_create_ip6_linklocal(eth_netif)); } break; + case IP_EVENT_ETH_GOT_IP: { + ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data; + ChipLogProgress(DeviceLayer, "Ethernet Got IPv4 event. address: " IPSTR, IP2STR(&event->ip_info.ip)); + } + break; default: break; } @@ -44,7 +50,7 @@ CHIP_ERROR ESPEthernetDriver::Init(NetworkStatusChangeCallback * networkStatusCh { /* Currently default ethernet board supported is IP101, if you want to use other types of * ethernet board then you can override this function in your application. */ - + printf("esp ethernet started"); esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH(); esp_netif_t * eth_netif = esp_netif_new(&cfg); @@ -53,21 +59,52 @@ CHIP_ERROR ESPEthernetDriver::Init(NetworkStatusChangeCallback * networkStatusCh eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG(); phy_config.phy_addr = CONFIG_ETH_PHY_ADDR; phy_config.reset_gpio_num = CONFIG_ETH_PHY_RST_GPIO; - eth_esp32_emac_config_t esp32_emac_config = ETH_ESP32_EMAC_DEFAULT_CONFIG(); - esp32_emac_config.smi_mdc_gpio_num = CONFIG_ETH_MDC_GPIO; - esp32_emac_config.smi_mdio_gpio_num = CONFIG_ETH_MDIO_GPIO; - esp_eth_mac_t * mac = esp_eth_mac_new_esp32(&esp32_emac_config, &mac_config); - esp_eth_phy_t * phy = esp_eth_phy_new_ip101(&phy_config); + // eth_esp32_emac_config_t esp32_emac_config = ETH_ESP32_EMAC_DEFAULT_CONFIG(); + // esp32_emac_config.smi_mdc_gpio_num = CONFIG_ETH_MDC_GPIO; + // esp32_emac_config.smi_mdio_gpio_num = CONFIG_ETH_MDIO_GPIO; + // esp_eth_mac_t * mac = esp_eth_mac_new_esp32(&esp32_emac_config, &mac_config); + + spi_device_interface_config_t spi_devcfg = { + .mode = 0, + .clock_speed_hz = CONFIG_EXAMPLE_ETH_SPI_CLOCK_MHZ * 1000 * 1000, + .spics_io_num = CONFIG_EXAMPLE_ETH_SPI_CS_GPIO, + .queue_size = 20 + }; + gpio_install_isr_service(0); + spi_bus_config_t buscfg = { + .mosi_io_num = CONFIG_EXAMPLE_ETH_SPI_MOSI_GPIO, + .miso_io_num = CONFIG_EXAMPLE_ETH_SPI_MISO_GPIO, + .sclk_io_num = CONFIG_EXAMPLE_ETH_SPI_SCLK_GPIO, + .quadwp_io_num = -1, + .quadhd_io_num = -1, + }; + + spi_bus_initialize((spi_host_device_t)CONFIG_EXAMPLE_ETH_SPI_HOST, &buscfg, SPI_DMA_CH_AUTO); + + eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG((spi_host_device_t)CONFIG_EXAMPLE_ETH_SPI_HOST, &spi_devcfg); + w5500_config.int_gpio_num = CONFIG_EXAMPLE_ETH_SPI_INT_GPIO; + esp_eth_mac_t * mac = esp_eth_mac_new_w5500(&w5500_config, &mac_config); + phy_config.phy_addr = CONFIG_EXAMPLE_ETH_PHY_ADDR; + phy_config.reset_gpio_num = CONFIG_EXAMPLE_ETH_PHY_RST_GPIO; + esp_eth_phy_t * phy = esp_eth_phy_new_w5500(&phy_config); esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, phy); esp_eth_handle_t eth_handle = NULL; ESP_ERROR_CHECK(esp_eth_driver_install(&config, ð_handle)); + + uint8_t addr[] = { + 0x02, 0x00, 0x00, 0x12, 0x34, 0x56 + }; + esp_eth_ioctl(eth_handle, ETH_CMD_S_MAC_ADDR, addr); + /* attach Ethernet driver to TCP/IP stack */ ESP_ERROR_CHECK(esp_netif_attach(eth_netif, esp_eth_new_netif_glue(eth_handle))); ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_CONNECTED, &on_eth_event, eth_netif)); + // ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &on_eth_event, eth_netif)); ESP_ERROR_CHECK(esp_eth_start(eth_handle)); + printf("esp ethernet started"); return CHIP_NO_ERROR; }