From fd333d1c79079cdcd68e5975cf0bcedb1567d96f Mon Sep 17 00:00:00 2001 From: PSONALl <77670766+PSONALl@users.noreply.github.com> Date: Fri, 26 May 2023 15:41:46 +0530 Subject: [PATCH] Add support for implementing different ethernet boards for esp32 (#26687) --- config/esp32/components/chip/CMakeLists.txt | 4 ++ docs/guides/esp32/build_app_and_commission.md | 5 +- src/platform/ESP32/BUILD.gn | 9 ++- .../ConnectivityManagerImpl_Ethernet.cpp | 23 -------- .../ESP32/NetworkCommissioningDriver.h | 8 +-- .../NetworkCommissioningDriver_Ethernet.cpp | 56 +++++++++++++++++++ 6 files changed, 73 insertions(+), 32 deletions(-) create mode 100644 src/platform/ESP32/NetworkCommissioningDriver_Ethernet.cpp diff --git a/config/esp32/components/chip/CMakeLists.txt b/config/esp32/components/chip/CMakeLists.txt index 14a02232bde429..2412e825d6c98d 100644 --- a/config/esp32/components/chip/CMakeLists.txt +++ b/config/esp32/components/chip/CMakeLists.txt @@ -178,6 +178,10 @@ if (CONFIG_ENABLE_ESP32_BLE_CONTROLLER) chip_gn_arg_append("chip_enable_ble_controller" "true") endif() +if (CONFIG_ENABLE_ETHERNET_TELEMETRY) + chip_gn_arg_append("chip_enable_ethernet" "true") +endif() + if (CONFIG_ENABLE_MATTER_OVER_THREAD) chip_gn_arg_append("chip_enable_openthread" "true") else() diff --git a/docs/guides/esp32/build_app_and_commission.md b/docs/guides/esp32/build_app_and_commission.md index a9145a2efe7d21..d49cdd3e7c89e0 100644 --- a/docs/guides/esp32/build_app_and_commission.md +++ b/docs/guides/esp32/build_app_and_commission.md @@ -200,8 +200,9 @@ 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`. 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` +ethernet board then you can extend the `ESPEthernetDriver` class in your +application and override the current implementation under +`ESPEthernetDriver::Init` #### Commissioning Parameters diff --git a/src/platform/ESP32/BUILD.gn b/src/platform/ESP32/BUILD.gn index d583a3a027cb1b..97be4f09587c48 100644 --- a/src/platform/ESP32/BUILD.gn +++ b/src/platform/ESP32/BUILD.gn @@ -30,6 +30,7 @@ declare_args() { chip_enable_ble_controller = false chip_use_secure_cert_dac_provider = false chip_use_esp32_ecdsa_peripheral = false + chip_enable_ethernet = false } defines = [ @@ -45,7 +46,6 @@ static_library("ESP32") { "ConfigurationManagerImpl.h", "ConnectivityManagerImpl.cpp", "ConnectivityManagerImpl.h", - "ConnectivityManagerImpl_Ethernet.cpp", "DiagnosticDataProviderImpl.cpp", "DiagnosticDataProviderImpl.h", "ESP32Config.cpp", @@ -136,6 +136,13 @@ static_library("ESP32") { 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/ConnectivityManagerImpl_Ethernet.cpp b/src/platform/ESP32/ConnectivityManagerImpl_Ethernet.cpp index 10ce294134e6d3..bd4ba1b3806f7a 100644 --- a/src/platform/ESP32/ConnectivityManagerImpl_Ethernet.cpp +++ b/src/platform/ESP32/ConnectivityManagerImpl_Ethernet.cpp @@ -39,8 +39,6 @@ #include #include -#if CHIP_DEVICE_CONFIG_ENABLE_ETHERNET - using namespace ::chip; using namespace ::chip::Inet; using namespace ::chip::System; @@ -53,26 +51,6 @@ 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); - - // Init MAC and PHY configs to default - eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG(); - 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; - mac_config.smi_mdc_gpio_num = CONFIG_ETH_MDC_GPIO; - mac_config.smi_mdio_gpio_num = CONFIG_ETH_MDIO_GPIO; - esp_eth_mac_t * mac = esp_eth_mac_new_esp32(&mac_config); - esp_eth_phy_t * phy = esp_eth_phy_new_ip101(&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)); - /* 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_eth_start(eth_handle)); return CHIP_NO_ERROR; } @@ -99,4 +77,3 @@ void ConnectivityManagerImpl::OnEthernetPlatformEvent(const ChipDeviceEvent * ev } // namespace DeviceLayer } // namespace chip -#endif // CHIP_DEVICE_CONFIG_ENABLE_ETHERNET diff --git a/src/platform/ESP32/NetworkCommissioningDriver.h b/src/platform/ESP32/NetworkCommissioningDriver.h index 960a79c423edf9..ed5fac3414a3b4 100644 --- a/src/platform/ESP32/NetworkCommissioningDriver.h +++ b/src/platform/ESP32/NetworkCommissioningDriver.h @@ -136,7 +136,7 @@ class ESPWiFiDriver final : public WiFiDriver uint16_t mLastDisconnectedReason; }; -class ESPEthernetDriver final : public EthernetDriver +class ESPEthernetDriver : public EthernetDriver { public: class EthernetNetworkIterator final : public NetworkIterator @@ -170,11 +170,7 @@ class ESPEthernetDriver final : public EthernetDriver // 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; - } + CHIP_ERROR Init(NetworkStatusChangeCallback * networkStatusChangeCallback) override; void Shutdown() { // TODO: This method can be implemented if Ethernet is used along with Wifi/Thread. diff --git a/src/platform/ESP32/NetworkCommissioningDriver_Ethernet.cpp b/src/platform/ESP32/NetworkCommissioningDriver_Ethernet.cpp new file mode 100644 index 00000000000000..bd31a9e6835011 --- /dev/null +++ b/src/platform/ESP32/NetworkCommissioningDriver_Ethernet.cpp @@ -0,0 +1,56 @@ +/* + * + * Copyright (c) 2021 Project CHIP Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include + +using namespace ::chip; +using namespace ::chip::DeviceLayer::Internal; +namespace chip { +namespace DeviceLayer { +namespace NetworkCommissioning { + +CHIP_ERROR ESPEthernetDriver::Init(NetworkStatusChangeCallback * networkStatusChangeCallback) +{ + /* 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. */ + + esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH(); + esp_netif_t * eth_netif = esp_netif_new(&cfg); + + // Init MAC and PHY configs to default + eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG(); + 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; + mac_config.smi_mdc_gpio_num = CONFIG_ETH_MDC_GPIO; + mac_config.smi_mdio_gpio_num = CONFIG_ETH_MDIO_GPIO; + esp_eth_mac_t * mac = esp_eth_mac_new_esp32(&mac_config); + esp_eth_phy_t * phy = esp_eth_phy_new_ip101(&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)); + /* 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_eth_start(eth_handle)); + + return CHIP_NO_ERROR; +} + +} // namespace NetworkCommissioning +} // namespace DeviceLayer +} // namespace chip