From f3fee5f4fbe5546047c8e3ce1acefc5d93ca8aa0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Sat, 22 Oct 2022 16:29:50 +0200 Subject: [PATCH] first murmurs of ethernet support --- arch/esp32/esp32.ini | 2 +- arch/esp32/esp32s3.ini | 2 +- src/RedirectablePrint.cpp | 2 -- src/configuration.h | 3 ++ src/main.cpp | 4 +++ src/mesh/eth/ethClient.cpp | 55 +++++++++++++++++++++++++++++++++ src/mesh/eth/ethClient.h | 8 +++++ src/network-stubs.cpp | 27 ++++++++++++++++ src/wifi-stubs.cpp | 19 ------------ variants/rak4631/platformio.ini | 1 + variants/rak4631/variant.h | 2 ++ 11 files changed, 102 insertions(+), 23 deletions(-) create mode 100644 src/mesh/eth/ethClient.cpp create mode 100644 src/mesh/eth/ethClient.h create mode 100644 src/network-stubs.cpp delete mode 100644 src/wifi-stubs.cpp diff --git a/arch/esp32/esp32.ini b/arch/esp32/esp32.ini index 798fb3d5a..1b57ffb8b 100644 --- a/arch/esp32/esp32.ini +++ b/arch/esp32/esp32.ini @@ -3,7 +3,7 @@ extends = arduino_base platform = platformio/espressif32@^5.2.0 build_src_filter = - ${arduino_base.build_src_filter} - - - + ${arduino_base.build_src_filter} - - - - upload_speed = 921600 debug_init_break = tbreak setup monitor_filters = esp32_exception_decoder diff --git a/arch/esp32/esp32s3.ini b/arch/esp32/esp32s3.ini index 8dc6d0b62..286d9950e 100644 --- a/arch/esp32/esp32s3.ini +++ b/arch/esp32/esp32s3.ini @@ -2,7 +2,7 @@ extends = arduino_base platform = platformio/espressif32@^5.2.0 build_src_filter = - ${arduino_base.build_src_filter} - - - + ${arduino_base.build_src_filter} - - - - upload_speed = 961200 monitor_speed = 115200 debug_init_break = tbreak setup diff --git a/src/RedirectablePrint.cpp b/src/RedirectablePrint.cpp index 0d14b4562..66c83171a 100644 --- a/src/RedirectablePrint.cpp +++ b/src/RedirectablePrint.cpp @@ -47,8 +47,6 @@ size_t RedirectablePrint::vprintf(const char *format, va_list arg) size_t len = vsnprintf(printBuf, sizeof(printBuf), format, copy); va_end(copy); - if (len < 0) return 0; - // If the resulting string is longer than sizeof(printBuf)-1 characters, the remaining characters are still counted for the return value if (len > sizeof(printBuf) - 1) { diff --git a/src/configuration.h b/src/configuration.h index 9374c29d4..6ad5f8ff9 100644 --- a/src/configuration.h +++ b/src/configuration.h @@ -142,6 +142,9 @@ along with this program. If not, see . #ifndef HAS_WIFI #define HAS_WIFI 0 #endif +#ifndef HAS_ETHERNET + #define ETHERNET 0 +#endif #ifndef HAS_SCREEN #define HAS_SCREEN 0 #endif diff --git a/src/main.cpp b/src/main.cpp index 104731ef3..2e231dec3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -30,6 +30,7 @@ // #include #include "mesh/http/WiFiAPClient.h" +#include "mesh/eth/ethClient.h" #ifdef ARCH_ESP32 #include "mesh/http/WebServer.h" @@ -440,6 +441,9 @@ void setup() // Initialize Wifi initWifi(forceSoftAP); + // Initialize Ethernet + initEthernet(); + #ifdef ARCH_ESP32 // Start web server thread. webServerThread = new WebServerThread(); diff --git a/src/mesh/eth/ethClient.cpp b/src/mesh/eth/ethClient.cpp new file mode 100644 index 000000000..92c1997e4 --- /dev/null +++ b/src/mesh/eth/ethClient.cpp @@ -0,0 +1,55 @@ +#include "mesh/eth/ethClient.h" +#include "NodeDB.h" +#include +#include +#include "target_specific.h" + +// Startup Ethernet +bool initEthernet() +{ + if (config.network.eth_enabled) { + + Ethernet.init( SS ); + + uint8_t mac[6]; + + getMacAddr(mac); // FIXME use the BLE MAC for now... + + if (Ethernet.hardwareStatus() == EthernetNoHardware) { + DEBUG_MSG("Ethernet shield was not found.\n"); + } else if (Ethernet.linkStatus() == LinkOFF) { + DEBUG_MSG("Ethernet cable is not connected.\n"); + } else if (config.network.eth_mode == Config_NetworkConfig_EthMode_DHCP) { + DEBUG_MSG("starting Ethernet DHCP\n"); + if (Ethernet.begin(mac) == 0) { + DEBUG_MSG("DHCP failed\n"); + } else{ + DEBUG_MSG("DHCP assigned IP %s\n",Ethernet.localIP()); + } + } else if (config.network.eth_mode == Config_NetworkConfig_EthMode_STATIC) { + DEBUG_MSG("starting Ethernet Static\n"); + Ethernet.begin(mac, config.network.eth_config.ip, config.network.eth_config.dns, config.network.eth_config.subnet); + } else { + DEBUG_MSG("Ethernet Disabled\n"); + return false; + } + return true; + + } else { + + DEBUG_MSG("Not using Ethernet\n"); + return false; + } +} + +bool isEthernetAvailable() { + if (!config.network.eth_enabled) { + return false; + } else if (Ethernet.hardwareStatus() == EthernetNoHardware) { + return false; + } else if (Ethernet.linkStatus() == LinkOFF) { + return false; + } else { + return true; + } +} \ No newline at end of file diff --git a/src/mesh/eth/ethClient.h b/src/mesh/eth/ethClient.h new file mode 100644 index 000000000..9e1745b9f --- /dev/null +++ b/src/mesh/eth/ethClient.h @@ -0,0 +1,8 @@ +#pragma once + +#include "configuration.h" +#include +#include + +bool initEthernet(); +bool isEthernetAvailable(); diff --git a/src/network-stubs.cpp b/src/network-stubs.cpp new file mode 100644 index 000000000..b7b4c146f --- /dev/null +++ b/src/network-stubs.cpp @@ -0,0 +1,27 @@ +#include "configuration.h" + +#if (HAS_WIFI == 0) + +bool initWifi(bool forceSoftAP) { + return false; +} + +void deinitWifi() {} + +bool isWifiAvailable() { + return false; +} + +#endif + +#if (HAS_ETHERNET == 0) + +bool initEthernet() { + return false; +} + +bool isEthernetAvailable() { + return false; +} + +#endif diff --git a/src/wifi-stubs.cpp b/src/wifi-stubs.cpp deleted file mode 100644 index faf58b620..000000000 --- a/src/wifi-stubs.cpp +++ /dev/null @@ -1,19 +0,0 @@ -//#include "mesh/wifi/WebServer.h" -#include "configuration.h" - -#ifndef ARCH_ESP32 - -//#include "mesh/wifi/WiFiAPClient.h" - -bool initWifi(bool forceSoftAP) { - return false; -} - -void deinitWifi() {} - -bool isWifiAvailable() -{ - return false; -} - -#endif diff --git a/variants/rak4631/platformio.ini b/variants/rak4631/platformio.ini index d8e2ba3e9..247cef6f6 100644 --- a/variants/rak4631/platformio.ini +++ b/variants/rak4631/platformio.ini @@ -7,6 +7,7 @@ build_src_filter = ${nrf52_base.build_src_filter} +<../variants/rak4631> lib_deps = ${nrf52840_base.lib_deps} melopero/Melopero RV3028@^1.1.0 + beegee-tokyo/RAK13800-W5100S@^1.0.1 debug_tool = jlink ; If not set we will default to uploading over serial (first it forces bootloader entry by talking 1200bps to cdcacm) ;upload_protocol = jlink \ No newline at end of file diff --git a/variants/rak4631/variant.h b/variants/rak4631/variant.h index 3648b8510..902165333 100644 --- a/variants/rak4631/variant.h +++ b/variants/rak4631/variant.h @@ -226,6 +226,8 @@ static const uint8_t SCK = PIN_SPI_SCK; #define HAS_RTC 1 +#define HAS_ETHERNET 1 + #ifdef __cplusplus } #endif