supla-device
Loading...
Searching...
No Matches
esp32ethspi.h
1/*
2 Copyright (C) AC SOFTWARE SP. Z O.O.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12 You should have received a copy of the GNU General Public License
13 along with this program; if not, write to the Free Software
14 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15*/
16
17#ifndef SRC_SUPLA_NETWORK_ESP32ETHSPI_H_
18#define SRC_SUPLA_NETWORK_ESP32ETHSPI_H_
19
20#include <Arduino.h>
21#include <ETH.h>
22#include <supla/network/netif_lan.h>
23#include <supla/supla_lib_config.h>
24#include <supla/log_wrapper.h>
25
26namespace Supla {
27class ESPETHSPI;
28} // namespace Supla
29
30static Supla::ESPETHSPI *thisSpiEth = nullptr;
31
32namespace Supla {
33class ESPETHSPI : public Supla::LAN {
34 public:
35 explicit ESPETHSPI(eth_phy_type_t type, int32_t phy_addr,
36 int cs, int irq, int rst, spi_host_device_t spi_host,
37 int sck = -1, int miso = -1, int mosi = -1) {
38 thisSpiEth = this;
39 ethspi_type = type;
40 ethspi_phy_addr = phy_addr;
41 cs_pin = cs;
42 irq_pin = irq;
43 rst_pin = rst;
44 ethspi_spi_host = spi_host;
45 sck_pin = sck;
46 miso_pin = miso;
47 mosi_pin = mosi;
48 }
49
50 ~ESPETHSPI() {
51 if (thisSpiEth == this) {
52 thisSpiEth = nullptr;
53 }
54 }
55
56 static void networkEventHandler(arduino_event_id_t event) {
57 switch (event) {
58 case ARDUINO_EVENT_ETH_GOT_IP: {
59 Serial.print(F("[Ethernet] local IP: "));
60 Serial.println(ETH.localIP());
61 Serial.print(F("subnetMask: "));
62 Serial.println(ETH.subnetMask());
63 Serial.print(F("gatewayIP: "));
64 Serial.println(ETH.gatewayIP());
65 Serial.print(F("ETH MAC: "));
66 Serial.println(ETH.macAddress());
67 if (ETH.fullDuplex()) {
68 Serial.print(F("FULL_DUPLEX , "));
69 }
70 Serial.print(ETH.linkSpeed());
71 Serial.println(F("Mbps"));
72 if (thisSpiEth) {
73 thisSpiEth->setIpv4Addr(ETH.localIP());
74 }
75 break;
76 }
77 case ARDUINO_EVENT_ETH_DISCONNECTED: {
78 Serial.println(F("[Ethernet] Disconnected"));
79 if (thisSpiEth) {
80 thisSpiEth->setIpv4Addr(0);
81 }
82 break;
83 }
84 }
85 }
86
87 void setup() override {
88 allowDisable = true;
89 if (initDone) {
90 return;
91 }
92
93 ::Network.onEvent(Supla::ESPETHSPI::networkEventHandler);
94
95 Serial.println(F("[Ethernet] establishing LAN connection"));
96 ETH.begin(ethspi_type,
97 ethspi_phy_addr,
98 cs_pin,
99 irq_pin,
100 rst_pin,
101 ethspi_spi_host,
102 sck_pin,
103 miso_pin,
104 mosi_pin);
105 initDone = true;
106
107 char newHostname[32] = {};
108 generateHostname(hostname, macSizeForHostname, newHostname);
109 strncpy(hostname, newHostname, sizeof(hostname) - 1);
110 SUPLA_LOG_DEBUG("[%s] Network Lan/hostname: %s", getIntfName(), hostname);
111 ETH.setHostname(hostname);
112 }
113
114 void disable() override {
115 if (!allowDisable) {
116 return;
117 }
118
119 allowDisable = false;
120 SUPLA_LOG_DEBUG("[%s] disabling ETH connection", getIntfName());
121 DisconnectProtocols();
122 // ETH.end();
123 }
124
125 bool getMacAddr(uint8_t *mac) override {
126 if (initDone) {
127 ETH.macAddress(mac);
128 }
129 return true;
130 }
131
132 void setHostname(const char *prefix, int macSize) override {
133 macSizeForHostname = macSize;
134 strncpy(hostname, prefix, sizeof(hostname) - 1);
135 SUPLA_LOG_DEBUG("[%s] Network Lam/hostname: %s", getIntfName(), hostname);
136 }
137
138 protected:
139 eth_phy_type_t ethspi_type;
140 int32_t ethspi_phy_addr;
141 int cs_pin;
142 int irq_pin;
143 int rst_pin;
144 spi_host_device_t ethspi_spi_host;
145 int sck_pin;
146 int miso_pin;
147 int mosi_pin;
148 bool allowDisable = false;
149 int macSizeForHostname = 0;
150 bool initDone = false;
151};
152}; // namespace Supla
153
154
155#endif // SRC_SUPLA_NETWORK_ESP32ETHSPI_H_
Definition esp32ethspi.h:33
Definition netif_lan.h:25
Definition network.h:36