Ethernet.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * Ethernet.h
8  *
9  ****/
10 
11 #pragma once
12 
13 #include <WString.h>
14 #include <IpAddress.h>
15 #include <MacAddress.h>
16 #include <Delegate.h>
17 #include <memory>
18 
23 #define ETHERNET_EVENT_MAP(XX) \
24  XX(Start, "Ethernet driver Started") \
25  XX(Stop, "Ethernet driver stopped") \
26  XX(Connected, "Ethernet link established") \
27  XX(Disconnected, "Ethernet link lost")
28 
29 namespace Ethernet
30 {
34 enum class Event {
35 #define XX(tag, desc) tag,
37 #undef XX
38 };
39 
44 using EventDelegate = Delegate<void(Ethernet::Event event)>;
45 
49 using GotIpDelegate = Delegate<void(IpAddress ip, IpAddress netmask, IpAddress gateway)>;
50 
54 constexpr int8_t PIN_DEFAULT{-2};
55 
61 constexpr int8_t PIN_UNUSED{-1};
62 
66 enum class Speed {
67  MBPS10,
68  MBPS100,
69 };
70 
74 struct PhyInstance;
75 
79 constexpr int8_t PHY_ADDR_AUTO{-1};
80 
84 struct PhyConfig {
86  int8_t resetPin = PIN_UNUSED;
87  uint16_t resetTimeout = 100;
88  uint16_t autoNegTimeout = 4000;
89 };
90 
98 {
99 public:
100  using PhyInstance = Ethernet::PhyInstance;
101 
105  virtual PhyInstance* create(const PhyConfig& config) = 0;
106 
110  virtual void destroy(PhyInstance* inst) = 0;
111 };
112 
127 class Service
128 {
129 public:
133  virtual void end() = 0;
134 
138  virtual MacAddress getMacAddress() const = 0;
139 
143  virtual bool setMacAddress(const MacAddress& addr) = 0;
144 
148  virtual bool setSpeed(Speed speed) = 0;
149 
153  virtual bool setFullDuplex(bool enable) = 0;
154 
158  virtual bool setLinkState(bool up) = 0;
159 
163  virtual bool setPromiscuous(bool enable) = 0;
164 
168  virtual void setHostname(const String& hostname) = 0;
169 
173  virtual String getHostname() const = 0;
174 
178  virtual IpAddress getIP() const = 0;
179 
183  virtual bool setIP(IpAddress address, IpAddress netmask, IpAddress gateway) = 0;
184 
188  virtual bool isEnabledDHCP() const = 0;
189 
193  virtual bool enableDHCP(bool enable) = 0;
194 
198  void onEvent(EventDelegate callback)
199  {
200  eventCallback = callback;
201  }
202 
206  void onGotIp(GotIpDelegate callback)
207  {
208  gotIpCallback = callback;
209  }
210 
211 protected:
214 };
215 
216 } // namespace Ethernet
217 
#define ETHERNET_EVENT_MAP(XX)
Ethernet event code map.
Definition: Ethernet.h:23
String toLongString(Ethernet::Event event)
String toString(Ethernet::Event event)
Virtual class used to construct a specific PHY instance.
Definition: Ethernet.h:98
Ethernet::PhyInstance PhyInstance
Definition: Ethernet.h:100
virtual PhyInstance * create(const PhyConfig &config)=0
Called by the Service to construct a PHY instance.
virtual void destroy(PhyInstance *inst)=0
Called by the Service to destroy a PHY instance.
Abstract Service class.
Definition: Ethernet.h:128
EventDelegate eventCallback
Definition: Ethernet.h:212
virtual MacAddress getMacAddress() const =0
Get MAC address.
virtual bool setPromiscuous(bool enable)=0
Set MAC promiscuous mode.
virtual void setHostname(const String &hostname)=0
Set DHCP hostname.
void onGotIp(GotIpDelegate callback)
Set callback for 'station connected with IP address' event.
Definition: Ethernet.h:206
virtual bool setFullDuplex(bool enable)=0
Set duplex mode of MAC.
void onEvent(EventDelegate callback)
Set callback for ethernet events.
Definition: Ethernet.h:198
virtual bool isEnabledDHCP() const =0
Determine if DHCP is active for this interface.
virtual bool setLinkState(bool up)=0
Set link status of MAC.
virtual bool setMacAddress(const MacAddress &addr)=0
Set MAC address.
GotIpDelegate gotIpCallback
Definition: Ethernet.h:213
virtual void end()=0
Tear down the ethernet connection.
virtual bool setSpeed(Speed speed)=0
Set speed of MAC.
virtual bool setIP(IpAddress address, IpAddress netmask, IpAddress gateway)=0
Set static IP address.
virtual IpAddress getIP() const =0
Get current IP address.
virtual bool enableDHCP(bool enable)=0
Enable/disable DHCP on this interface.
virtual String getHostname() const =0
Get DHCP hostname.
A class to make it easier to handle and pass around IP addresses.
Definition: IpAddress.h:45
A network hardware (MAC) address.
Definition: MacAddress.h:39
The String class.
Definition: WString.h:137
void enable(Handler &commandHandler, HardwareSerial &serial)
Definition: Dp83848.h:16
Event
Ethernet event codes.
Definition: Ethernet.h:34
XX(tag, desc)
Speed
Link speed.
Definition: Ethernet.h:66
constexpr int8_t PIN_UNUSED
Do not configure this pin.
Definition: Ethernet.h:61
constexpr int8_t PHY_ADDR_AUTO
Automatically detect PHY address during initialization.
Definition: Ethernet.h:79
constexpr int8_t PIN_DEFAULT
Use default pin for platform.
Definition: Ethernet.h:54
PHY configuration.
Definition: Ethernet.h:84
uint16_t autoNegTimeout
Auto-negotiation timeout in milliseconds.
Definition: Ethernet.h:88
int8_t resetPin
Reset GPIO number *‍/.
Definition: Ethernet.h:86
int8_t phyAddr
PHY address.
Definition: Ethernet.h:85
uint16_t resetTimeout
Reset timeout value in milliseconds.
Definition: Ethernet.h:87