supla-device
Loading...
Searching...
No Matches
esp32ethspi.h
1// SPDX-FileCopyrightText: AC SOFTWARE SP. Z O.O.
2// SPDX-License-Identifier: GPL-2.0-or-later
3
4#ifndef SRC_SUPLA_NETWORK_ESP32ETHSPI_H_
5#define SRC_SUPLA_NETWORK_ESP32ETHSPI_H_
6
7#include <Arduino.h>
8#include <ETH.h>
9#include <supla/network/arduino_netif_config.h>
10#include <supla/network/netif_lan.h>
11#include <supla/supla_lib_config.h>
12#include <supla/log_wrapper.h>
13
14namespace Supla {
15class ESPETHSPI;
16} // namespace Supla
17
18static Supla::ESPETHSPI *thisSpiEth = nullptr;
19
20namespace Supla {
21class ESPETHSPI : public Supla::LAN {
22 public:
23 explicit ESPETHSPI(eth_phy_type_t type, int32_t phy_addr,
24 int cs, int irq, int rst, spi_host_device_t spi_host,
25 int sck = -1, int miso = -1, int mosi = -1) {
26 thisSpiEth = this;
27 ethspi_type = type;
28 ethspi_phy_addr = phy_addr;
29 cs_pin = cs;
30 irq_pin = irq;
31 rst_pin = rst;
32 ethspi_spi_host = spi_host;
33 sck_pin = sck;
34 miso_pin = miso;
35 mosi_pin = mosi;
36 }
37
38 ~ESPETHSPI() {
39 if (thisSpiEth == this) {
40 thisSpiEth = nullptr;
41 }
42 }
43
44 static void networkEventHandler(arduino_event_id_t event) {
45 switch (event) {
46 case ARDUINO_EVENT_ETH_GOT_IP: {
47 IPAddress localIP = ETH.localIP();
48 IPAddress subnetMaskIP = ETH.subnetMask();
49 IPAddress gatewayIP = ETH.gatewayIP();
50 uint8_t mac[6] = {};
51 ETH.macAddress(mac);
52 SUPLA_LOG_INFO("localIP: %d.%d.%d.%d",
53 localIP[0],
54 localIP[1],
55 localIP[2],
56 localIP[3]);
57 SUPLA_LOG_INFO("subnetMaskIP: %d.%d.%d.%d",
58 subnetMaskIP[0],
59 subnetMaskIP[1],
60 subnetMaskIP[2],
61 subnetMaskIP[3]);
62 SUPLA_LOG_INFO("gatewayIP: %d.%d.%d.%d",
63 gatewayIP[0],
64 gatewayIP[1],
65 gatewayIP[2],
66 gatewayIP[3]);
67 SUPLA_LOG_INFO("ETH MAC: %02X:%02X:%02X:%02X:%02X:%02X",
68 mac[0],
69 mac[1],
70 mac[2],
71 mac[3],
72 mac[4],
73 mac[5]);
74 SUPLA_LOG_INFO("speed: %d Mbps", ETH.linkSpeed());
75 if (ETH.fullDuplex()) {
76 SUPLA_LOG_INFO("FULL_DUPLEX");
77 }
78 if (thisSpiEth) {
79 thisSpiEth->setIpv4Addr(ETH.localIP());
80 }
81 break;
82 }
83 case ARDUINO_EVENT_ETH_DISCONNECTED: {
84 SUPLA_LOG_INFO("[Ethernet] Disconnected");
85 if (thisSpiEth) {
86 thisSpiEth->setIpv4Addr(0);
87 }
88 break;
89 }
90 }
91 }
92
93 void setup() override {
94 allowDisable = true;
95 if (initDone) {
96 return;
97 }
98
99 ::Network.onEvent(Supla::ESPETHSPI::networkEventHandler);
100
101 SUPLA_LOG_INFO("[Ethernet] establishing LAN connection");
102 ETH.begin(ethspi_type,
103 ethspi_phy_addr,
104 cs_pin,
105 irq_pin,
106 rst_pin,
107 ethspi_spi_host,
108 sck_pin,
109 miso_pin,
110 mosi_pin);
111 if (hasStaticIpConfig()) {
112 const auto &cfg = getNetifConfig();
113 IPAddress localIp = toArduinoIpAddress(cfg.ip);
114 IPAddress gateway = toArduinoIpAddress(cfg.gateway);
115 IPAddress subnet = toArduinoIpAddress(cfg.netmask);
116 IPAddress dns1 = toArduinoIpAddress(cfg.dns1);
117 IPAddress dns2 = toArduinoIpAddress(cfg.dns2);
118 if (!ETH.config(localIp, gateway, subnet, dns1, dns2)) {
119 SUPLA_LOG_WARNING("ETH SPI static IP config failed, continuing");
120 }
121 }
122 initDone = true;
123
124 char newHostname[32] = {};
125 generateHostname(hostname, macSizeForHostname, newHostname);
126 strncpy(hostname, newHostname, sizeof(hostname) - 1);
127 SUPLA_LOG_DEBUG("[%s] Network LAN/hostname: %s", getIntfName(), hostname);
128 ETH.setHostname(hostname);
129 }
130
131 void disable() override {
132 if (!allowDisable) {
133 return;
134 }
135
136 allowDisable = false;
137 SUPLA_LOG_DEBUG("[%s] disabling ETH connection", getIntfName());
138 DisconnectProtocols();
139 // ETH.end();
140 }
141
142 bool getMacAddr(uint8_t *mac) override {
143 if (initDone) {
144 ETH.macAddress(mac);
145 }
146 return true;
147 }
148
149 const char *getIntfName() const override {
150 return "ETH";
151 }
152
153 void setHostname(const char *prefix, int macSize) override {
154 macSizeForHostname = macSize;
155 strncpy(hostname, prefix, sizeof(hostname) - 1);
156 SUPLA_LOG_DEBUG("[%s] Network LAN/hostname: %s", getIntfName(), hostname);
157 }
158
159 protected:
160 eth_phy_type_t ethspi_type;
161 int32_t ethspi_phy_addr;
162 int cs_pin;
163 int irq_pin;
164 int rst_pin;
165 spi_host_device_t ethspi_spi_host;
166 int sck_pin;
167 int miso_pin;
168 int mosi_pin;
169 bool allowDisable = false;
170 int macSizeForHostname = 0;
171 bool initDone = false;
172};
173}; // namespace Supla
174
175
176#endif // SRC_SUPLA_NETWORK_ESP32ETHSPI_H_
Definition ip_address.h:15
Definition esp32ethspi.h:21
Definition netif_lan.h:10