ESPixelStick Firmware
Firmware for the ESPixelStick
Loading...
Searching...
No Matches
EthernetDriver.hpp
Go to the documentation of this file.
1#pragma once
2/*
3* EthernetDriver.hpp - Output Management class
4*
5* Project: ESPixelStick - An ESP8266 / ESP32 and E1.31 based pixel driver
6* Copyright (c) 2021, 2022 Shelby Merrick
7* http://www.forkineye.com
8*
9* This program is provided free for you to use in any way that you wish,
10* subject to the laws and regulations where you are using it. Due diligence
11* is strongly suggested before using this code. Please give credit where due.
12*
13* The Author makes no warranty of any kind, express or implied, with regard
14* to this program or the documentation contained in this document. The
15* Author shall not be liable in any event for incidental or consequential
16* damages in connection with, or arising out of, the furnishing, performance
17* or use of these programs.
18*
19*/
20
21#include "ESPixelStick.h"
22#ifdef SUPPORT_ETHERNET
23
24// forward declaration
25class c_EthernetDriver;
26
27/*****************************************************************************/
28class fsm_Eth_state
29{
30protected:
31 c_EthernetDriver * pEthernetDriver = nullptr;
32public:
33 fsm_Eth_state() {}
34 virtual ~fsm_Eth_state() {}
35
36 virtual void Poll (void) = 0;
37 virtual void Init (void) = 0;
38 virtual void GetStateName (String& sName) = 0;
39 virtual void OnConnect (void) = 0;
40 virtual void OnGotIp (void) = 0;
41 virtual void OnDisconnect (void) = 0;
42 void SetParent (c_EthernetDriver* parent) { pEthernetDriver = parent; }
43 void GetDriverName (String & value) { value = CN_EthDrv; }
44
45}; // fsm_Eth_state
46
47class c_EthernetDriver
48{
49public:
50 c_EthernetDriver ();
51 virtual ~c_EthernetDriver ();
52
53 void Begin ();
54 void GetConfig (JsonObject & json);
55 bool SetConfig (JsonObject & json);
56 void GetDriverName (String & value) { value = CN_EthDrv; }
57
58 int ValidateConfig ();
59 IPAddress GetIpAddress ();
60 IPAddress GetIpSubNetMask ();
61 IPAddress GetIpGateway ();
62 String GetMacAddress ();
63 void GetHostname (String & Name);
64 void SetHostname (String & Name) {}
65 void GetStatus (JsonObject & jsonStatus);
66 void SetEthHostname ();
67 void reset ();
68 void Poll ();
69 bool IsConnected ();
70 inline void SetFsmState (fsm_Eth_state * NewState) { pCurrentFsmState = NewState; }
71 void AnnounceState ();
72 FastTimer & GetFsmTimer () { return FsmTimer; }
73 void NetworkStateChanged (bool NetworkState);
74 void StartEth ();
75 void InitPowerPin ();
76 inline uint32_t GetPowerPinActiveDelayMs () { return powerPinActiveDelayMs; }
77
78private:
79
80 void SetUpIp ();
81
82 void onEventHandler (const WiFiEvent_t event, const WiFiEventInfo_t info);
83
84 FastTimer NextPollTimer;
85 uint32_t PollInterval = 1000;
86 bool HasBeenPreviouslyConfigured = false;
87
88 IPAddress ip = INADDR_NONE;
89 IPAddress netmask = INADDR_NONE;
90 IPAddress gateway = INADDR_NONE;
91 IPAddress primaryDns = INADDR_NONE;
92 IPAddress secondaryDns = INADDR_NONE;
93 bool UseDhcp = true;
94 uint32_t phy_addr = DEFAULT_ETH_ADDR;
95 gpio_num_t power_pin = DEFAULT_ETH_POWER_PIN;
96 gpio_num_t mdc_pin = DEFAULT_ETH_MDC_PIN;
97 gpio_num_t mdio_pin = DEFAULT_ETH_MDIO_PIN;
98 eth_phy_type_t phy_type = DEFAULT_ETH_TYPE;
99 eth_clock_mode_t clk_mode = DEFAULT_ETH_CLK_MODE;
100 bool powerPinActiveValue = (HIGH == DEFAULT_ETH_POWER_PIN_ACTIVE);
101 uint32_t powerPinActiveDelayMs = 1000;
102
103protected:
104 friend class fsm_Eth_state_Boot;
105 friend class fsm_Eth_state_ConnectingToEth;
106 friend class fsm_Eth_state_WaitForIP;
107 friend class fsm_Eth_state_GotIp;
108 friend class fsm_Eth_state_DeviceInitFailed;
109 friend class fsm_Eth_state;
110 fsm_Eth_state * pCurrentFsmState = nullptr;
111 FastTimer FsmTimer;
112
113}; // c_EthernetDriver
114
115/*****************************************************************************/
116/*
117* Generic fsm base class.
118*/
119/*****************************************************************************/
120/*****************************************************************************/
121// Wait for polling to start.
122class fsm_Eth_state_Boot : public fsm_Eth_state
123{
124public:
125 fsm_Eth_state_Boot() {}
126 virtual ~fsm_Eth_state_Boot() {}
127
128 virtual void Poll (void);
129 virtual void Init (void);
130 virtual void GetStateName (String& sName) { sName = F ("Boot"); }
131 virtual void OnConnect (void) { /* ignore */ }
132 virtual void OnGotIp (void) { /* ignore */ }
133 virtual void OnDisconnect (void) { /* ignore */ }
134
135}; // fsm_Eth_state_Boot
136
137/*****************************************************************************/
138class fsm_Eth_state_PoweringUp : public fsm_Eth_state
139{
140public:
141 fsm_Eth_state_PoweringUp() {}
142 virtual ~fsm_Eth_state_PoweringUp() {}
143
144 virtual void Poll (void);
145 virtual void Init (void);
146 virtual void GetStateName (String& sName) { sName = F ("Powering Up"); }
147 virtual void OnConnect (void) {}
148 virtual void OnGotIp (void) {}
149 virtual void OnDisconnect (void) {}
150
151}; // fsm_Eth_state_PoweringUp
152
153/*****************************************************************************/
154class fsm_Eth_state_ConnectingToEth : public fsm_Eth_state
155{
156public:
157 fsm_Eth_state_ConnectingToEth() {}
158 virtual ~fsm_Eth_state_ConnectingToEth() {}
159
160 virtual void Poll(void);
161 virtual void Init(void);
162 virtual void GetStateName (String& sName) { sName = F ("Connecting"); }
163 virtual void OnConnect (void);
164 virtual void OnGotIp (void);
165 virtual void OnDisconnect (void) {}
166
167}; // fsm_Eth_state_ConnectingToEth
168
169/*****************************************************************************/
170class fsm_Eth_state_WaitForIP : public fsm_Eth_state
171{
172public:
173 fsm_Eth_state_WaitForIP() {}
174 virtual ~fsm_Eth_state_WaitForIP() {}
175
176 virtual void Poll (void) {}
177 virtual void Init (void);
178 virtual void GetStateName (String& sName) { sName = F ("Wait for IP Address"); }
179 virtual void OnConnect (void) {}
180 virtual void OnGotIp (void);
181 virtual void OnDisconnect (void);
182
183}; // fsm_Eth_state_WaitForIP
184
185/*****************************************************************************/
186class fsm_Eth_state_GotIp : public fsm_Eth_state
187{
188public:
189 fsm_Eth_state_GotIp() {}
190 virtual ~fsm_Eth_state_GotIp() {}
191
192 virtual void Poll (void) {}
193 virtual void Init (void);
194 virtual void GetStateName (String& sName) { sName = F ("Got IP"); }
195 virtual void OnConnect (void) {}
196 virtual void OnGotIp (void) {}
197 virtual void OnDisconnect (void);
198
199}; // fsm_Eth_state_GotIp
200
201/*****************************************************************************/
202class fsm_Eth_state_DeviceInitFailed : public fsm_Eth_state
203{
204public:
205 fsm_Eth_state_DeviceInitFailed() {}
206 virtual ~fsm_Eth_state_DeviceInitFailed() {}
207
208 virtual void Poll (void) {}
209 virtual void Init (void);
210 virtual void GetStateName (String& sName) { sName = F ("Device Init Failed"); }
211 virtual void OnConnect (void) {}
212 virtual void OnGotIp (void) {}
213 virtual void OnDisconnect (void) {}
214
215}; // fsm_Eth_state_DeviceInitFailed
216
217extern c_EthernetDriver EthernetDriver;
218
219#endif // def SUPPORT_ETHERNET
const CN_PROGMEM char CN_EthDrv[]
Definition ConstNames.cpp:92
#define DEFAULT_ETH_POWER_PIN_ACTIVE
Definition GPIO_Defs_ESP32_Bong69.hpp:61
#define DEFAULT_ETH_POWER_PIN
Definition GPIO_Defs_ESP32_Bong69.hpp:59
#define DEFAULT_ETH_CLK_MODE
Definition GPIO_Defs_ESP32_Bong69.hpp:56
#define DEFAULT_ETH_ADDR
Definition GPIO_Defs_ESP32_Bong69.hpp:69
#define DEFAULT_ETH_TYPE
Definition GPIO_Defs_ESP32_Bong69.hpp:64
#define DEFAULT_ETH_MDC_PIN
Definition GPIO_Defs_ESP32_Bong69.hpp:76
#define DEFAULT_ETH_MDIO_PIN
Definition GPIO_Defs_ESP32_Bong69.hpp:77
Definition FastTimer.hpp:24
void GetDriverName(String &Name)
Definition main.cpp:115
void GetConfig(JsonObject &json)
Definition main.cpp:440
void SetConfig(const char *DataString)
Definition main.cpp:286