From 2d8ec7d8dfff50973224b7e45460c638889a0ea9 Mon Sep 17 00:00:00 2001 From: technyon Date: Sun, 18 Aug 2024 09:15:30 +0200 Subject: [PATCH] move code to instantiate network devices to NetworkDeviceInstantiator class --- clion/CMakeLists.txt | 1 + src/NukiNetwork.cpp | 153 +---------------------- src/util/NetworkDeviceInstantiator.cpp | 165 +++++++++++++++++++++++++ src/util/NetworkDeviceInstantiator.h | 12 ++ src/util/NetworkUtil.cpp | 2 + src/util/NetworkUtil.h | 2 + updater/src/CMakeLists.txt | 3 +- 7 files changed, 186 insertions(+), 152 deletions(-) create mode 100644 src/util/NetworkDeviceInstantiator.cpp create mode 100644 src/util/NetworkDeviceInstantiator.h diff --git a/clion/CMakeLists.txt b/clion/CMakeLists.txt index a8225ff..9e044d2 100644 --- a/clion/CMakeLists.txt +++ b/clion/CMakeLists.txt @@ -56,6 +56,7 @@ set(SRCFILES ../lib/AsyncTCP/src/AsyncTCP.cpp ../src/util/NetworkUtil.cpp ../src/enums/NetworkDeviceType.h + ../src/util/NetworkDeviceInstantiator.cpp ) file(GLOB_RECURSE SRCFILESREC diff --git a/src/NukiNetwork.cpp b/src/NukiNetwork.cpp index 94de48a..f192206 100644 --- a/src/NukiNetwork.cpp +++ b/src/NukiNetwork.cpp @@ -5,6 +5,7 @@ #include "RestartReason.h" #include #include +#include "util/NetworkDeviceInstantiator.h" #ifndef CONFIG_IDF_TARGET_ESP32H2 #include "networkDevices/WifiDevice.h" #endif @@ -119,157 +120,7 @@ void NukiNetwork::setupDevice() _networkDeviceType = NetworkUtil::GetDeviceTypeFromPreference(hardwareDetect, _preferences->getInt(preference_network_custom_phy, 0)); } - switch (_networkDeviceType) - { - case NetworkDeviceType::W5500: - _device = new EthernetDevice(_hostname, _preferences, _ipConfiguration, "Generic W5500", - ETH_PHY_ADDR_W5500, - ETH_PHY_CS_GENERIC_W5500, - ETH_PHY_IRQ_GENERIC_W5500, - ETH_PHY_RST_GENERIC_W5500, - ETH_PHY_SPI_SCK_GENERIC_W5500, - ETH_PHY_SPI_MISO_GENERIC_W5500, - ETH_PHY_SPI_MOSI_GENERIC_W5500, - ETH_PHY_W5500); - break; - case NetworkDeviceType::W5500M5: - _device = new EthernetDevice(_hostname, _preferences, _ipConfiguration, "M5Stack Atom POE", - ETH_PHY_ADDR_W5500, - ETH_PHY_CS_M5_W5500, - ETH_PHY_IRQ_M5_W5500, - ETH_PHY_RST_M5_W5500, - ETH_PHY_SPI_SCK_M5_W5500, - ETH_PHY_SPI_MISO_M5_W5500, - ETH_PHY_SPI_MOSI_M5_W5500, - ETH_PHY_W5500); - break; - case NetworkDeviceType::W5500M5S3: - _device = new EthernetDevice(_hostname, _preferences, _ipConfiguration, "M5Stack Atom POE S3", - ETH_PHY_ADDR_W5500, - ETH_PHY_CS_M5_W5500_S3, - ETH_PHY_IRQ_M5_W5500, - ETH_PHY_RST_M5_W5500, - ETH_PHY_SPI_SCK_M5_W5500_S3, - ETH_PHY_SPI_MISO_M5_W5500_S3, - ETH_PHY_SPI_MOSI_M5_W5500_S3, - ETH_PHY_W5500); - break; - case NetworkDeviceType::ETH01_Evo: - _device = new EthernetDevice(_hostname, _preferences, _ipConfiguration, "ETH01-Evo", - ETH_PHY_ADDR_ETH01EVO, - ETH_PHY_CS_ETH01EVO, - ETH_PHY_IRQ_ETH01EVO, - ETH_PHY_RST_ETH01EVO, - ETH_PHY_SPI_SCK_ETH01EVO, - ETH_PHY_SPI_MISO_ETH01EVO, - ETH_PHY_SPI_MOSI_ETH01EVO, - ETH_PHY_TYPE_DM9051); - break; - case NetworkDeviceType::M5STACK_PoESP32_Unit: - _device = new EthernetDevice(_hostname, _preferences, _ipConfiguration, "M5STACK PoESP32 Unit", - ETH_PHY_ADDR_W5500, - ETH_PHY_CS_M5_W5500, - ETH_PHY_IRQ_M5_W5500, - ETH_PHY_RST_M5_W5500, - ETH_PHY_SPI_SCK_M5_W5500, - ETH_PHY_SPI_MISO_M5_W5500, - ETH_PHY_SPI_MOSI_M5_W5500, - ETH_PHY_W5500); - break; - case NetworkDeviceType::CUSTOM: - { - int custPHY = _preferences->getInt(preference_network_custom_phy, 0); - - if(custPHY >= 1 && custPHY <= 3) - { - std::string custName; - eth_phy_type_t custEthtype; - - switch(custPHY) - { - case 1: - custName = "Custom (W5500)"; - custEthtype = ETH_PHY_W5500; - break; - case 2: - custName = "Custom (DN9051)"; - custEthtype = ETH_PHY_DM9051; - break; - case 3: - custName = "Custom (KSZ8851SNL)"; - custEthtype = ETH_PHY_KSZ8851; - break; - default: - custName = "Custom (W5500)"; - custEthtype = ETH_PHY_W5500; - break; - } - - _device = new EthernetDevice(_hostname, _preferences, _ipConfiguration, custName, - _preferences->getInt(preference_network_custom_addr, -1), - _preferences->getInt(preference_network_custom_cs, -1), - _preferences->getInt(preference_network_custom_irq, -1), - _preferences->getInt(preference_network_custom_rst, -1), - _preferences->getInt(preference_network_custom_sck, -1), - _preferences->getInt(preference_network_custom_miso, -1), - _preferences->getInt(preference_network_custom_mosi, -1), - custEthtype); - } - #if defined(CONFIG_IDF_TARGET_ESP32) - else if(custPHY >= 4 && custPHY <= 9) - { - int custCLKpref = _preferences->getInt(preference_network_custom_clk, 0); - - std::string custName = NetworkUtil::GetCustomEthernetDeviceName(custPHY); - eth_phy_type_t custEthtype = NetworkUtil::GetCustomEthernetType(custPHY); - eth_clock_mode_t custCLK = NetworkUtil::GetCustomClock(custCLKpref); - - _device = new EthernetDevice(_hostname, _preferences, _ipConfiguration, custName, _preferences->getInt(preference_network_custom_addr, -1), _preferences->getInt(preference_network_custom_pwr, -1), _preferences->getInt(preference_network_custom_mdc, -1), _preferences->getInt(preference_network_custom_mdio, -1), custEthtype, custCLK); - } - #endif - #ifndef CONFIG_IDF_TARGET_ESP32H2 - else - { - _device = new WifiDevice(_hostname, _preferences, _ipConfiguration); - } - #endif - } - break; - #if defined(CONFIG_IDF_TARGET_ESP32) - case NetworkDeviceType::Olimex_LAN8720: - _device = new EthernetDevice(_hostname, _preferences, _ipConfiguration, "Olimex (LAN8720)", ETH_PHY_ADDR_LAN8720, 12, ETH_PHY_MDC_LAN8720, ETH_PHY_MDIO_LAN8720, ETH_PHY_TYPE_LAN8720, ETH_CLOCK_GPIO17_OUT); - break; - case NetworkDeviceType::WT32_LAN8720: - _device = new EthernetDevice(_hostname, _preferences, _ipConfiguration, "WT32-ETH01", 1, 16); - break; - case NetworkDeviceType::GL_S10: - _device = new EthernetDevice(_hostname, _preferences, _ipConfiguration, "GL-S10", 1, 5, ETH_PHY_MDC_LAN8720, ETH_PHY_MDIO_LAN8720, ETH_PHY_IP101, ETH_CLOCK_GPIO0_IN); - break; - case NetworkDeviceType::LilyGO_T_ETH_POE: - _device = new EthernetDevice(_hostname, _preferences, _ipConfiguration, "LilyGO T-ETH-POE", 0, -1, ETH_PHY_MDC_LAN8720, ETH_PHY_MDIO_LAN8720, ETH_PHY_TYPE_LAN8720, ETH_CLOCK_GPIO17_OUT); - break; - #endif - #ifndef CONFIG_IDF_TARGET_ESP32H2 - case NetworkDeviceType::WiFi: - _device = new WifiDevice(_hostname, _preferences, _ipConfiguration); - break; - default: - _device = new WifiDevice(_hostname, _preferences, _ipConfiguration); - break; - #else - default: - _device = new EthernetDevice(_hostname, _preferences, _ipConfiguration, "Custom (W5500)", - _preferences->getInt(preference_network_custom_addr, -1), - _preferences->getInt(preference_network_custom_cs, -1), - _preferences->getInt(preference_network_custom_irq, -1), - _preferences->getInt(preference_network_custom_rst, -1), - _preferences->getInt(preference_network_custom_sck, -1), - _preferences->getInt(preference_network_custom_miso, -1), - _preferences->getInt(preference_network_custom_mosi, -1), - ETH_PHY_W5500); - break; - #endif - } + _device = NetworkDeviceInstantiator::Create(_networkDeviceType, _hostname, _preferences, _ipConfiguration); Log->print(F("Network device: ")); Log->print(_device->deviceName()); diff --git a/src/util/NetworkDeviceInstantiator.cpp b/src/util/NetworkDeviceInstantiator.cpp new file mode 100644 index 0000000..024ca63 --- /dev/null +++ b/src/util/NetworkDeviceInstantiator.cpp @@ -0,0 +1,165 @@ +#include "NetworkDeviceInstantiator.h" +#include "../networkDevices/EthernetDevice.h" +#include "../networkDevices/WifiDevice.h" +#include "../PreferencesKeys.h" +#include "NetworkUtil.h" +#include "../networkDevices/LAN8720Definitions.h" + +NetworkDevice *NetworkDeviceInstantiator::Create(NetworkDeviceType networkDeviceType, String hostname, Preferences *preferences, IPConfiguration *ipConfiguration) +{ + NetworkDevice* device = nullptr; + + switch (networkDeviceType) + { + case NetworkDeviceType::W5500: + device = new EthernetDevice(hostname, preferences, ipConfiguration, "Generic W5500", + ETH_PHY_ADDR_W5500, + ETH_PHY_CS_GENERIC_W5500, + ETH_PHY_IRQ_GENERIC_W5500, + ETH_PHY_RST_GENERIC_W5500, + ETH_PHY_SPI_SCK_GENERIC_W5500, + ETH_PHY_SPI_MISO_GENERIC_W5500, + ETH_PHY_SPI_MOSI_GENERIC_W5500, + ETH_PHY_W5500); + break; + case NetworkDeviceType::W5500M5: + device = new EthernetDevice(hostname, preferences, ipConfiguration, "M5Stack Atom POE", + ETH_PHY_ADDR_W5500, + ETH_PHY_CS_M5_W5500, + ETH_PHY_IRQ_M5_W5500, + ETH_PHY_RST_M5_W5500, + ETH_PHY_SPI_SCK_M5_W5500, + ETH_PHY_SPI_MISO_M5_W5500, + ETH_PHY_SPI_MOSI_M5_W5500, + ETH_PHY_W5500); + break; + case NetworkDeviceType::W5500M5S3: + device = new EthernetDevice(hostname, preferences, ipConfiguration, "M5Stack Atom POE S3", + ETH_PHY_ADDR_W5500, + ETH_PHY_CS_M5_W5500_S3, + ETH_PHY_IRQ_M5_W5500, + ETH_PHY_RST_M5_W5500, + ETH_PHY_SPI_SCK_M5_W5500_S3, + ETH_PHY_SPI_MISO_M5_W5500_S3, + ETH_PHY_SPI_MOSI_M5_W5500_S3, + ETH_PHY_W5500); + break; + case NetworkDeviceType::ETH01_Evo: + device = new EthernetDevice(hostname, preferences, ipConfiguration, "ETH01-Evo", + ETH_PHY_ADDR_ETH01EVO, + ETH_PHY_CS_ETH01EVO, + ETH_PHY_IRQ_ETH01EVO, + ETH_PHY_RST_ETH01EVO, + ETH_PHY_SPI_SCK_ETH01EVO, + ETH_PHY_SPI_MISO_ETH01EVO, + ETH_PHY_SPI_MOSI_ETH01EVO, + ETH_PHY_TYPE_DM9051); + break; + case NetworkDeviceType::M5STACK_PoESP32_Unit: + device = new EthernetDevice(hostname, preferences, ipConfiguration, "M5STACK PoESP32 Unit", + ETH_PHY_ADDR_W5500, + ETH_PHY_CS_M5_W5500, + ETH_PHY_IRQ_M5_W5500, + ETH_PHY_RST_M5_W5500, + ETH_PHY_SPI_SCK_M5_W5500, + ETH_PHY_SPI_MISO_M5_W5500, + ETH_PHY_SPI_MOSI_M5_W5500, + ETH_PHY_W5500); + break; + case NetworkDeviceType::CUSTOM: + { + int custPHY = preferences->getInt(preference_network_custom_phy, 0); + + if(custPHY >= 1 && custPHY <= 3) + { + std::string custName; + eth_phy_type_t custEthtype; + + switch(custPHY) + { + case 1: + custName = "Custom (W5500)"; + custEthtype = ETH_PHY_W5500; + break; + case 2: + custName = "Custom (DN9051)"; + custEthtype = ETH_PHY_DM9051; + break; + case 3: + custName = "Custom (KSZ8851SNL)"; + custEthtype = ETH_PHY_KSZ8851; + break; + default: + custName = "Custom (W5500)"; + custEthtype = ETH_PHY_W5500; + break; + } + + device = new EthernetDevice(hostname, preferences, ipConfiguration, custName, + preferences->getInt(preference_network_custom_addr, -1), + preferences->getInt(preference_network_custom_cs, -1), + preferences->getInt(preference_network_custom_irq, -1), + preferences->getInt(preference_network_custom_rst, -1), + preferences->getInt(preference_network_custom_sck, -1), + preferences->getInt(preference_network_custom_miso, -1), + preferences->getInt(preference_network_custom_mosi, -1), + custEthtype); + } +#if defined(CONFIG_IDF_TARGET_ESP32) + else if(custPHY >= 4 && custPHY <= 9) + { + int custCLKpref = preferences->getInt(preference_network_custom_clk, 0); + + std::string custName = NetworkUtil::GetCustomEthernetDeviceName(custPHY); + eth_phy_type_t custEthtype = NetworkUtil::GetCustomEthernetType(custPHY); + eth_clock_mode_t custCLK = NetworkUtil::GetCustomClock(custCLKpref); + + device = new EthernetDevice(hostname, preferences, ipConfiguration, custName, preferences->getInt(preference_network_custom_addr, -1), preferences->getInt(preference_network_custom_pwr, -1), preferences->getInt(preference_network_custom_mdc, -1), preferences->getInt(preference_network_custom_mdio, -1), custEthtype, custCLK); + } +#endif +#ifndef CONFIG_IDF_TARGET_ESP32H2 + else + { + device = new WifiDevice(hostname, preferences, ipConfiguration); + } +#endif + } + break; +#if defined(CONFIG_IDF_TARGET_ESP32) + case NetworkDeviceType::Olimex_LAN8720: + device = new EthernetDevice(hostname, preferences, ipConfiguration, "Olimex (LAN8720)", ETH_PHY_ADDR_LAN8720, 12, ETH_PHY_MDC_LAN8720, ETH_PHY_MDIO_LAN8720, ETH_PHY_TYPE_LAN8720, ETH_CLOCK_GPIO17_OUT); + break; + case NetworkDeviceType::WT32_LAN8720: + device = new EthernetDevice(hostname, preferences, ipConfiguration, "WT32-ETH01", 1, 16); + break; + case NetworkDeviceType::GL_S10: + device = new EthernetDevice(hostname, preferences, ipConfiguration, "GL-S10", 1, 5, ETH_PHY_MDC_LAN8720, ETH_PHY_MDIO_LAN8720, ETH_PHY_IP101, ETH_CLOCK_GPIO0_IN); + break; + case NetworkDeviceType::LilyGO_T_ETH_POE: + device = new EthernetDevice(hostname, preferences, ipConfiguration, "LilyGO T-ETH-POE", 0, -1, ETH_PHY_MDC_LAN8720, ETH_PHY_MDIO_LAN8720, ETH_PHY_TYPE_LAN8720, ETH_CLOCK_GPIO17_OUT); + break; +#endif +#ifndef CONFIG_IDF_TARGET_ESP32H2 + case NetworkDeviceType::WiFi: + device = new WifiDevice(hostname, preferences, ipConfiguration); + break; + default: + device = new WifiDevice(hostname, preferences, ipConfiguration); + break; +#else + default: + _device = new EthernetDevice(_hostname, _preferences, _ipConfiguration, "Custom (W5500)", + _preferences->getInt(preference_network_custom_addr, -1), + _preferences->getInt(preference_network_custom_cs, -1), + _preferences->getInt(preference_network_custom_irq, -1), + _preferences->getInt(preference_network_custom_rst, -1), + _preferences->getInt(preference_network_custom_sck, -1), + _preferences->getInt(preference_network_custom_miso, -1), + _preferences->getInt(preference_network_custom_mosi, -1), + ETH_PHY_W5500); + break; +#endif + } + + return device; +} diff --git a/src/util/NetworkDeviceInstantiator.h b/src/util/NetworkDeviceInstantiator.h new file mode 100644 index 0000000..f429852 --- /dev/null +++ b/src/util/NetworkDeviceInstantiator.h @@ -0,0 +1,12 @@ +#pragma once + +#include "../networkDevices/NetworkDevice.h" +#include "../enums/NetworkDeviceType.h" +#include +#include + +class NetworkDeviceInstantiator +{ +public: + static NetworkDevice* Create(NetworkDeviceType networkDeviceType, String hostname, Preferences* preferences, IPConfiguration* ipConfiguration); +}; diff --git a/src/util/NetworkUtil.cpp b/src/util/NetworkUtil.cpp index 79ed0d6..603b4e8 100644 --- a/src/util/NetworkUtil.cpp +++ b/src/util/NetworkUtil.cpp @@ -74,6 +74,7 @@ std::string NetworkUtil::GetCustomEthernetDeviceName(int custPHY) } } +#if defined(CONFIG_IDF_TARGET_ESP32) eth_phy_type_t NetworkUtil::GetCustomEthernetType(int custPHY) { switch(custPHY) @@ -120,3 +121,4 @@ eth_clock_mode_t NetworkUtil::GetCustomClock(int custCLKpref) break; } } +#endif \ No newline at end of file diff --git a/src/util/NetworkUtil.h b/src/util/NetworkUtil.h index 6c6a5c0..9f22552 100644 --- a/src/util/NetworkUtil.h +++ b/src/util/NetworkUtil.h @@ -9,6 +9,8 @@ class NetworkUtil public: static NetworkDeviceType GetDeviceTypeFromPreference(int hardwareDetect, int customPhy); static std::string GetCustomEthernetDeviceName(int custPHY); +#if defined(CONFIG_IDF_TARGET_ESP32) static eth_phy_type_t GetCustomEthernetType(int custPHY); static eth_clock_mode_t GetCustomClock(int custCLKpref); +#endif }; \ No newline at end of file diff --git a/updater/src/CMakeLists.txt b/updater/src/CMakeLists.txt index e05cd21..b418139 100644 --- a/updater/src/CMakeLists.txt +++ b/updater/src/CMakeLists.txt @@ -11,11 +11,12 @@ list(APPEND app_sources ../../src/Logger.cpp) list(APPEND app_sources ../../src/NukiNetwork.cpp) list(APPEND app_sources ../../src/WebCfgServer.cpp) +list(APPEND app_sources ../../src/enums/NetworkDeviceType.h) list(APPEND app_sources ../../src/networkDevices/EthernetDevice.h) list(APPEND app_sources ../../src/networkDevices/IPConfiguration.h) list(APPEND app_sources ../../src/networkDevices/NetworkDevice.h) list(APPEND app_sources ../../src/util/NetworkUtil.cpp) -list(APPEND app_sources ../../src/enums/NetworkDeviceType.h) +list(APPEND app_sources ../../src/util/NetworkDeviceInstantiator.cpp) if(NOT DEFINED NUKI_TARGET_H2) list(APPEND app_sources ../../src/networkDevices/WifiDevice.h)