A Hardware Hacking Tool with Web-Based CLI That Speaks Every Protocol
C arduinobluetoothcan-busdebuggingeepromesp32flipperzerogpiohardware-hackingi2cjtagopenocdprotocolspwmradiorfidspisubghzuartwifi
adapt ethernet service to work with the esp network stack
Geo14c1f9a522b5
diff --git a/src/Services/EthernetService.cpp b/src/Services/EthernetService.cppindex 20fda04c..a2e62f48 100644--- a/src/Services/EthernetService.cpp+++ b/src/Services/EthernetService.cpp@@ -1,79 +1,351 @@#include "EthernetService.h"-EthernetService::EthernetService() {}+#include <cstdio>+#include "esp_netif_defaults.h"+#include "esp_log.h"+#include "driver/gpio.h"+#include <Arduino.h>-void EthernetService::configure(int8_t pinCS, int8_t pinRST, int8_t pinSCK, int8_t pinMISO, int8_t pinMOSI, uint32_t spiHz) {- _pinCS = pinCS;+// =================================== LOG ================================++// Erase macro to log on serial+#ifdef ESP_LOGE+ #undef ESP_LOGE+#endif++#define _SERIAL_LOG_BASE(levelChar, tag, format, ...) \+ do { Serial.printf("[%c][%s] " format "\r\n", (levelChar), (tag), ##__VA_ARGS__); } while (0)++#define ESP_LOGE(tag, format, ...) _SERIAL_LOG_BASE('E', (tag), format, ##__VA_ARGS__)+static const char* TAG = "EthernetService";++#define LOG_ERR(fn, err) ESP_LOGE(TAG, "%s -> %s", fn, esp_err_to_name(err))++bool EthernetService::s_stackInited = false;++// helpers for IP cast compatibility+static inline const ip4_addr_t* to_lwip(const esp_ip4_addr_t* a) {+ return reinterpret_cast<const ip4_addr_t*>(a);+}++// ==============================================================++EthernetService::EthernetService()+: _spi(nullptr), _eth(nullptr), _netif(nullptr), _glue(nullptr),+ _pinRST(-1), _pinIRQ(-1), _configured(false), _linkUp(false), _gotIP(false) {+ _ip.addr = _gw.addr = _mask.addr = _dns0.addr = 0;+}++void EthernetService::ensureStacksInited() {+ if (s_stackInited) return;++ esp_err_t e = esp_netif_init();+ e = esp_event_loop_create_default();+ s_stackInited = true;++ esp_log_level_set("esp_eth", ESP_LOG_DEBUG);+ esp_log_level_set("ETH", ESP_LOG_DEBUG);+ esp_log_level_set("netif", ESP_LOG_INFO);+}++bool EthernetService::configure(int8_t pinCS, int8_t pinRST, int8_t pinSCK, int8_t pinMISO, int8_t pinMOSI, uint8_t pinIRQ, uint32_t spiHz, const std::array<uint8_t,6>& chosenMac) {_pinRST = pinRST;- _pinSCK = pinSCK;- _pinMISO= pinMISO;- _pinMOSI= pinMOSI;- _spiHz = spiHz;+ _pinIRQ = pinIRQ;++ #ifdef DEVICE_M5STICK+ ESP_LOGE(TAG, "M5Stick not supported");+ return false;+ #else+ ensureStacksInited();++ // Service ISR GPIO+ static bool s_isr = false;+ if (!s_isr) {+ esp_err_t e = gpio_install_isr_service(0);+ if (e != ESP_OK) { LOG_ERR("gpio_install_isr_service", e); return false; }+ }- SPI.end();- SPI.begin(_pinSCK, _pinMISO, _pinMOSI, _pinCS);+ // Bus SPI+ spi_bus_config_t buscfg{};+ buscfg.mosi_io_num = pinMOSI;+ buscfg.miso_io_num = pinMISO;+ buscfg.sclk_io_num = pinSCK;+ buscfg.quadwp_io_num = -1;+ buscfg.quadhd_io_num = -1;+ esp_err_t e1 = spi_bus_initialize(SPI3_HOST, &buscfg, SPI_DMA_CH_AUTO);+ if (e1 != ESP_OK) { LOG_ERR("falied init spi bus", e1); return false; }- Ethernet.init(_pinCS);+ // Device config+ spi_device_interface_config_t devcfg_final{};+ devcfg_final.mode = 0;+ devcfg_final.clock_speed_hz = spiHz;+ devcfg_final.spics_io_num = pinCS;+ devcfg_final.command_bits = 16;+ devcfg_final.address_bits = 8;+ devcfg_final.flags = SPI_DEVICE_NO_DUMMY;+ devcfg_final.queue_size = 4;- hardReset();+ // Add device+ esp_err_t eDev = spi_bus_add_device(SPI3_HOST, &devcfg_final, &_spi);+ if (eDev != ESP_OK) { LOG_ERR("spi_bus_add_device(final)", eDev); return false; }++ // MAC/PHY W5500+ eth_w5500_config_t mac_cfg = ETH_W5500_DEFAULT_CONFIG(_spi);+ mac_cfg.int_gpio_num = _pinIRQ;++ // Mode polling+ #if defined(W5500_HAS_POLLING)+ if (_pinIRQ < 0) {+ mac_cfg.poll_period_ms = 10; // 10 ms+ }+ #else+ if (_pinIRQ < 0) {+ ESP_LOGE(TAG, "IRQ require");+ return false;+ }+ #endif++ eth_mac_config_t mac_common = ETH_MAC_DEFAULT_CONFIG();+ mac_common.rx_task_stack_size = 4096;+ eth_phy_config_t phy_cfg = ETH_PHY_DEFAULT_CONFIG();+ phy_cfg.phy_addr = 0;+ phy_cfg.reset_gpio_num = _pinRST; // let driver do reset if available++ esp_eth_mac_t* mac = esp_eth_mac_new_w5500(&mac_cfg, &mac_common);+ esp_eth_phy_t* phy2 = esp_eth_phy_new_w5500(&phy_cfg);+ if (!mac || !phy2) { ESP_LOGE(TAG, "esp_eth_mac_new_w5500/phy_new_w5500 NULL"); return false; }++ esp_eth_config_t eth_cfg = ETH_DEFAULT_CONFIG(mac, phy2);+ esp_err_t e3 = esp_eth_driver_install(ð_cfg, &_eth);+ if (e3 != ESP_OK) { LOG_ERR("esp_eth_driver_install", e3); return false; }++ // Mac+ _mac = chosenMac;+ esp_err_t eMac = esp_eth_ioctl(_eth, ETH_CMD_S_MAC_ADDR, (void*)_mac.data());+ if (eMac != ESP_OK) {+ ESP_LOGE(TAG, "ETH_CMD_S_MAC_ADDR -> %s", esp_err_to_name(eMac));+ }++ // netif + glue+ esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH();+ _netif = esp_netif_new(&cfg);+ if (!_netif) { ESP_LOGE(TAG, "esp_netif_new failed"); return false; }++ _glue = esp_eth_new_netif_glue(_eth);+ if (!_glue) { ESP_LOGE(TAG, "esp_eth_new_netif_glue failed"); return false; }++ esp_err_t e4 = esp_netif_attach(_netif, _glue);+ if (e4 != ESP_OK) { LOG_ERR("esp_netif_attach", e4); return false; }++ // Hostname+ esp_netif_set_hostname(_netif, "esp32-buspirate-eth");++ // Handlers+ esp_err_t e5 = esp_event_handler_instance_register(ETH_EVENT, ESP_EVENT_ANY_ID, &EthernetService::onEthEvent, this, &_ethHandler);+ if (e5 != ESP_OK) { LOG_ERR("handler ETH_EVENT register", e5); return false; }++ esp_err_t e6 = esp_event_handler_instance_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &EthernetService::onIpEvent, this, &_ipHandler);+ if (e6 != ESP_OK) { LOG_ERR("handler IP_EVENT register", e6); return false; }_configured = true;++ #endif++ return true;}void EthernetService::hardReset() {- if (_pinRST < 0) return;pinMode(_pinRST, OUTPUT);- digitalWrite(_pinRST, LOW);- delay(5);- digitalWrite(_pinRST, HIGH);- delay(200);+ digitalWrite(_pinRST, LOW); delay(5);+ digitalWrite(_pinRST, HIGH); delay(200);}-bool EthernetService::beginDHCP(const std::array<uint8_t,6>& mac, unsigned long timeoutMs) {- if (!_configured) return false;- _mac = mac;+bool EthernetService::beginDHCP(unsigned long timeoutMs) {+ if (!_configured) { ESP_LOGE(TAG, "service non configure"); return false; }- int ret = Ethernet.begin(const_cast<uint8_t*>(_mac.data()),- timeoutMs,- 4000); // responseTimeout-- if (ret != 1) return false;- return true;+ // Launch Ethernet+ esp_err_t eStart = esp_eth_start(_eth);+ if (eStart != ESP_OK && eStart != ESP_ERR_INVALID_STATE) {+ ESP_LOGE(TAG, "esp_eth_start -> %s", esp_err_to_name(eStart));+ return false;+ }+ if (eStart == ESP_ERR_INVALID_STATE) ESP_LOGE(TAG, "esp_eth_start: Eth already started");++ // Launch DHCP+ esp_err_t eDhcp = esp_netif_dhcpc_start(_netif);++ // Wait LINK+ const unsigned long t0 = millis();+ while (millis() - t0 < timeoutMs) {+ if (_linkUp) break;+ delay(25);+ }++ // Wait IP+ const unsigned long t1 = millis();+ while (millis() - t1 < timeoutMs) {+ if (_gotIP) {+ return true;+ }+ delay(25);+ }++ return false;}-bool EthernetService::isConnected() const {- return linkUp() && Ethernet.localIP();+static std::string ip4_to_string_(const ip4_addr_t& a) {+ char buf[16];+ ip4addr_ntoa_r(&a, buf, sizeof(buf));+ return std::string(buf);}-void EthernetService::maintain() {- Ethernet.maintain();+std::string EthernetService::getMac() const {+ uint8_t m[6] = {0};+ if (_eth) {+ esp_err_t e = esp_eth_ioctl(_eth, ETH_CMD_G_MAC_ADDR, m);+ // if (e != ESP_OK) LOG_ERR("ETH_CMD_G_MAC_ADDR", e);+ } else {+ memcpy(m, _mac.data(), 6);+ }+ char buf[18];+ sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", m[0],m[1],m[2],m[3],m[4],m[5]);+ return std::string(buf);}-int EthernetService::hardwareStatusRaw() const {- return (int)Ethernet.hardwareStatus();+bool EthernetService::isConnected() const { return _linkUp && _gotIP; }+int EthernetService::linkStatusRaw() const { return _linkUp ? 1 : 0; }+bool EthernetService::linkUp() const { return _linkUp; }+std::string EthernetService::ip4ToString(const ip4_addr_t& a) const { return ip4_to_string_(a); }+std::string EthernetService::getLocalIP() const { return ip4_to_string_(_ip); }+std::string EthernetService::getSubnetMask() const{ return ip4_to_string_(_mask); }+std::string EthernetService::getGatewayIp() const { return ip4_to_string_(_gw); }+std::string EthernetService::getDns() const { return ip4_to_string_(_dns0); }++//================== Events =============================++void EthernetService::onEthEvent(void* arg, esp_event_base_t, int32_t id, void*) {+ auto* self = static_cast<EthernetService*>(arg);+ if (!self) { ESP_LOGE(TAG, "onEthEvent self=null"); return; }++ switch (id) {+ case ETHERNET_EVENT_CONNECTED:+ self->_linkUp = true;+ break;+ case ETHERNET_EVENT_DISCONNECTED:+ self->_linkUp = false;+ self->_gotIP = false;+ break;+ case ETHERNET_EVENT_START:+ break;+ case ETHERNET_EVENT_STOP:+ break;+ default:+ break;+ }}-int EthernetService::linkStatusRaw() const {- return (int)Ethernet.linkStatus();++void EthernetService::onIpEvent(void* arg, esp_event_base_t, int32_t id, void* data) {+ auto* self = static_cast<EthernetService*>(arg);+ if (!self) { ESP_LOGE(TAG, "onIpEvent self=null"); return; }++ if (id == IP_EVENT_ETH_GOT_IP) {+ auto* e = reinterpret_cast<ip_event_got_ip_t*>(data);+ self->_ip.addr = e->ip_info.ip.addr;+ self->_gw.addr = e->ip_info.gw.addr;+ self->_mask.addr = e->ip_info.netmask.addr;++ esp_netif_dns_info_t dns{};+ if (esp_netif_get_dns_info(self->_netif, ESP_NETIF_DNS_MAIN, &dns) == ESP_OK) {+ self->_dns0.addr = dns.ip.u_addr.ip4.addr;+ } else {+ self->_dns0.addr = 0;+ }+ self->_gotIP = true;++ char ip[16], gw[16], mask[16];+ ip4addr_ntoa_r(to_lwip(&e->ip_info.ip), ip, sizeof(ip));+ ip4addr_ntoa_r(to_lwip(&e->ip_info.gw), gw, sizeof(gw));+ ip4addr_ntoa_r(to_lwip(&e->ip_info.netmask), mask, sizeof(mask));+ }}-bool EthernetService::linkUp() const {- return Ethernet.linkStatus() == LinkON;++// ==================== W5500 Test Helpers ====================++static bool _w5500_spi_read(spi_device_handle_t dev, uint16_t addr, uint8_t bsb, uint8_t& out) {+ if (!dev) return false;+ uint8_t tx[4] = { (uint8_t)(addr >> 8), (uint8_t)(addr & 0xFF), (uint8_t)((bsb << 3) | 0x01), 0x00 };+ uint8_t rx[4] = {0};+ spi_transaction_t t{};+ t.length = 8 * sizeof(tx);+ t.tx_buffer = tx;+ t.rx_buffer = rx;+ esp_err_t e = spi_device_transmit(dev, &t);+ if (e != ESP_OK) return false;+ out = rx[3];+ return true;}-std::string EthernetService::getLocalIP() const {- return Ethernet.localIP().toString().c_str();+static bool _w5500_probe(spi_device_handle_t dev, uint8_t& ver) {+ // VERSIONR @ common regs 0x0039 should be 0x04 on W5500+ if (!_w5500_spi_read(dev, 0x0039, 0x00, ver)) return false;+ return (ver != 0x00 && ver != 0xFF);}-std::string EthernetService::getSubnetMask() const {- return Ethernet.subnetMask().toString().c_str();++// W5500 1-byte FDM helpers (OM=01)+static bool _w5500_spi_write1(spi_device_handle_t dev, uint16_t addr, uint8_t bsb, uint8_t val) {+ if (!dev) return false;+ uint8_t tx[4] = { uint8_t(addr >> 8), uint8_t(addr), uint8_t((bsb << 3) | 0x04 | 0x01), val }; // RWB=1, OM=01+ spi_transaction_t t{}; t.length = 32; t.tx_buffer = tx;+ return spi_device_transmit(dev, &t) == ESP_OK;}-std::string EthernetService::getGatewayIp() const {- return Ethernet.gatewayIP().toString().c_str();++static bool _w5500_spi_read1(spi_device_handle_t dev, uint16_t addr, uint8_t bsb, uint8_t& out) {+ if (!dev) return false;+ uint8_t tx[4] = { uint8_t(addr >> 8), uint8_t(addr), uint8_t((bsb << 3) | 0x00 | 0x01), 0x00 }; // RWB=0, OM=01+ uint8_t rx[4] = {0};+ spi_transaction_t t{}; t.length = 32; t.tx_buffer = tx; t.rx_buffer = rx;+ if (spi_device_transmit(dev, &t) != ESP_OK) return false;+ out = rx[3]; return true;}-std::string EthernetService::getDns() const {- return Ethernet.dnsServerIP().toString().c_str();++static bool _w5500_soft_reset(spi_device_handle_t dev) {+ if (!_w5500_spi_write1(dev, 0x0000, 0x00, 0x80)) return false; // MR bit7=RST+ delay(5);+ for (int i=0;i<50;i++) { uint8_t mr;+ if (!_w5500_spi_read1(dev, 0x0000, 0x00, mr)) return false;+ if ((mr & 0x80)==0) return true;+ delay(2);+ }+ return false;+}++// write+read sur SUBR (0x001A..0x001D) for validation+static bool _w5500_rw_selftest(spi_device_handle_t dev) {+ const uint16_t SUBR=0x001A; const uint8_t BSB=0x00; const uint8_t v[4]={255,255,255,0};+ for (int i=0;i<4;i++) if (!_w5500_spi_write1(dev, SUBR+i, BSB, v[i])) return false;+ for (int i=0;i<4;i++) { uint8_t r=0; if (!_w5500_spi_read1(dev, SUBR+i, BSB, r) || r!=v[i]) return false; }+ return true;+}+static bool _w5500_read_phycfgr(spi_device_handle_t dev, uint8_t& v) { // bit0=LNK, bit1=100M, bit2=FULL+ return _w5500_spi_read1(dev, 0x002E, 0x00, v);+}++static bool _w5500_spi_write(spi_device_handle_t dev, uint16_t addr, uint8_t bsb, uint8_t val) {+ if (!dev) return false;+ uint8_t tx[4] = { (uint8_t)(addr >> 8), (uint8_t)(addr & 0xFF), (uint8_t)((bsb << 3) | 0x04), val }; // RWB=0, OM=00+ spi_transaction_t t{};+ t.length = 8 * sizeof(tx);+ t.tx_buffer = tx;+ return (spi_device_transmit(dev, &t) == ESP_OK);+}++// low level SPI helpers to probe W5500 before driver install+static bool _spi_set_speed(spi_device_handle_t& dev, int csPin, int hz) {+ if (dev) { spi_bus_remove_device(dev); dev = nullptr; }+ spi_device_interface_config_t devcfg{};+ devcfg.mode = 0;+ devcfg.clock_speed_hz = hz;+ devcfg.spics_io_num = csPin;+ devcfg.queue_size = 4;+ return spi_bus_add_device(SPI3_HOST, &devcfg, &dev) == ESP_OK;}-std::string EthernetService::getMac() const {- char buf[18];- sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",- _mac[0],_mac[1],_mac[2],_mac[3],_mac[4],_mac[5]);- return std::string(buf);-}\ No newline at end of filediff --git a/src/Services/EthernetService.h b/src/Services/EthernetService.hindex bcba973e..0a630d0b 100644--- a/src/Services/EthernetService.h+++ b/src/Services/EthernetService.h@@ -1,50 +1,70 @@#pragma once-#include <Arduino.h>-#include <SPI.h>-#include <Ethernet.h>-#include <ESP32Ping.h>-#include <array>#include <string>-#include <memory>+#include <cstring>++#include "esp_event.h"+#include "esp_eth.h"+#include "esp_netif.h"+#include "esp_eth_netif_glue.h"+#include "driver/spi_master.h"+#include "lwip/ip4_addr.h"+#include <Arduino.h>+#include <ESP32Ping.h>class EthernetService {public:EthernetService();- void configure(int8_t pinCS,- int8_t pinRST = -1,- int8_t pinSCK = 18,- int8_t pinMISO = 19,- int8_t pinMOSI = 23,- uint32_t spiHz = 26000000UL);+ // Init the W5500 ethernet stack+ void ensureStacksInited();++ // Configure the W5500+ bool configure(int8_t pinCS, int8_t pinRST, int8_t pinSCK, int8_t pinMISO, int8_t pinMOSI, uint8_t pinIRQ, uint32_t spiHz, const std::array<uint8_t,6>& chosenMac);- bool beginDHCP(const std::array<uint8_t,6>& mac, unsigned long timeoutMs = 10000);+ // Reset the W5500+ void hardReset();++ // Start ETH + DHCP and wait for link then IP.+ bool beginDHCP(unsigned long timeoutMs);++ // Check if the W5500 is connected to a networkbool isConnected() const;- void maintain();- std::string getLocalIP() const;- std::string getSubnetMask()const;+ // Plug/unplug+ int linkStatusRaw() const;+ bool linkUp() const;++ // Helpers+ std::string ip4ToString(const ip4_addr_t& a) const;+ std::string getLocalIP() const;+ std::string getSubnetMask() const;std::string getGatewayIp() const;- std::string getDns() const;- std::string getMac() const;+ std::string getDns() const;+ std::string getMac() const;- int hardwareStatusRaw() const; // Ethernet.hardwareStatus()- int linkStatusRaw() const; // Ethernet.linkStatus()- bool linkUp() const;+ // Handlers for the esp network stack+ static void onEthEvent(void* arg, esp_event_base_t, int32_t id, void* data);+ static void onIpEvent (void* arg, esp_event_base_t, int32_t id, void* data);private:- void hardReset();+ static bool s_stackInited;++ spi_device_handle_t _spi;+ esp_eth_handle_t _eth;+ esp_netif_t* _netif;+ esp_eth_netif_glue_handle_t _glue;++ esp_event_handler_instance_t _ethHandler = nullptr;+ esp_event_handler_instance_t _ipHandler = nullptr;- // pins / conf SPI- int8_t _pinCS = 5;- int8_t _pinRST = -1;- int8_t _pinSCK = 18;- int8_t _pinMISO = 19;- int8_t _pinMOSI = 23;- uint32_t _spiHz = 26000000UL;+ std::array<uint8_t,6> _mac;+ ip4_addr_t _ip{}, _gw{}, _mask{}, _dns0{};- std::array<uint8_t,6> _mac {{0,0,0,0,0,0}};- bool _configured = false;+ int8_t _pinRST;+ int8_t _pinIRQ;+ bool _configured;+ volatile bool _linkUp;+ volatile bool _gotIP;};