From af21f2148d77f58e52b38f90863b472d0ec8bcbb Mon Sep 17 00:00:00 2001 From: PSONALl Date: Fri, 17 Mar 2023 14:49:44 +0530 Subject: [PATCH] Fix ethernet commissioning for esp32 --- docs/guides/esp32/build_app_and_commission.md | 5 +- .../platform/esp32/common/Esp32AppServer.cpp | 3 ++ .../ConnectivityManagerImpl_Ethernet.cpp | 2 + .../ESP32/NetworkCommissioningDriver.h | 51 +++++++++++++++++++ 4 files changed, 60 insertions(+), 1 deletion(-) diff --git a/docs/guides/esp32/build_app_and_commission.md b/docs/guides/esp32/build_app_and_commission.md index eeb5f1296b49f2..720334652fec89 100644 --- a/docs/guides/esp32/build_app_and_commission.md +++ b/docs/guides/esp32/build_app_and_commission.md @@ -198,7 +198,10 @@ $ out/debug/chip-tool pairing onnetwork 12345 20202021 Note: In order to commission an ethernet device, from all-clusters-app enable these config options: select `ESP32-Ethernet-Kit` under `Demo->Device Type` and -select `On-Network` rendezvous mode under `Demo->Rendezvous Mode` +select `On-Network` rendezvous mode under `Demo->Rendezvous Mode` Currently +default ethernet board supported is IP101, if you want to use other types of +ethernet board then override the current implementation under +`ConnectivityManagerImpl::InitEthernet` #### Commissioning Parameters diff --git a/examples/platform/esp32/common/Esp32AppServer.cpp b/examples/platform/esp32/common/Esp32AppServer.cpp index 448463033c383e..91120707b9504c 100644 --- a/examples/platform/esp32/common/Esp32AppServer.cpp +++ b/examples/platform/esp32/common/Esp32AppServer.cpp @@ -35,6 +35,9 @@ namespace { #if CHIP_DEVICE_CONFIG_ENABLE_WIFI app::Clusters::NetworkCommissioning::Instance sWiFiNetworkCommissioningInstance(0 /* Endpoint Id */, &(NetworkCommissioning::ESPWiFiDriver::GetInstance())); +#elif CHIP_DEVICE_CONFIG_ENABLE_ETHERNET +static app::Clusters::NetworkCommissioning::Instance + sEthernetNetworkCommissioningInstance(0 /* Endpoint Id */, &(NetworkCommissioning::ESPEthernetDriver::GetInstance())); #endif #if CONFIG_TEST_EVENT_TRIGGER_ENABLED diff --git a/src/platform/ESP32/ConnectivityManagerImpl_Ethernet.cpp b/src/platform/ESP32/ConnectivityManagerImpl_Ethernet.cpp index ed0a5f059f4106..10ce294134e6d3 100644 --- a/src/platform/ESP32/ConnectivityManagerImpl_Ethernet.cpp +++ b/src/platform/ESP32/ConnectivityManagerImpl_Ethernet.cpp @@ -51,6 +51,8 @@ namespace DeviceLayer { CHIP_ERROR ConnectivityManagerImpl::InitEthernet() { + // Initialize TCP/IP network interface (should be called for all Ethernet boards) + ESP_ERROR_CHECK(esp_netif_init()); esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH(); esp_netif_t * eth_netif = esp_netif_new(&cfg); diff --git a/src/platform/ESP32/NetworkCommissioningDriver.h b/src/platform/ESP32/NetworkCommissioningDriver.h index c6175938d76478..960a79c423edf9 100644 --- a/src/platform/ESP32/NetworkCommissioningDriver.h +++ b/src/platform/ESP32/NetworkCommissioningDriver.h @@ -136,6 +136,57 @@ class ESPWiFiDriver final : public WiFiDriver uint16_t mLastDisconnectedReason; }; +class ESPEthernetDriver final : public EthernetDriver +{ +public: + class EthernetNetworkIterator final : public NetworkIterator + { + public: + EthernetNetworkIterator(ESPEthernetDriver * aDriver) : mDriver(aDriver) {} + size_t Count() { return 1; } + bool Next(Network & item) override + { + if (exhausted) + { + return false; + } + exhausted = true; + memcpy(item.networkID, interfaceName, interfaceNameLen); + item.networkIDLen = interfaceNameLen; + item.connected = true; + return true; + } + void Release() override { delete this; } + ~EthernetNetworkIterator() = default; + + uint8_t interfaceName[kMaxNetworkIDLen]; + uint8_t interfaceNameLen = 0; + bool exhausted = false; + + private: + ESPEthernetDriver * mDriver; + }; + + // BaseDriver + NetworkIterator * GetNetworks() override { return new EthernetNetworkIterator(this); } + uint8_t GetMaxNetworks() { return 1; } + CHIP_ERROR Init(NetworkStatusChangeCallback * networkStatusChangeCallback) + { + // TODO: This method can be implemented if Ethernet is used along with Wifi/Thread. + return CHIP_NO_ERROR; + } + void Shutdown() + { + // TODO: This method can be implemented if Ethernet is used along with Wifi/Thread. + } + + static ESPEthernetDriver & GetInstance() + { + static ESPEthernetDriver instance; + return instance; + } +}; + } // namespace NetworkCommissioning } // namespace DeviceLayer } // namespace chip