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 
45 
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 {
85  int8_t phyAddr = PHY_ADDR_AUTO;
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 
A class to make it easier to handle and pass around IP addresses.
Definition: IpAddress.h:44
Ethernet::PhyInstance PhyInstance
Definition: Ethernet.h:100
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
XX(tag, desc)
The String class.
Definition: WString.h:136
EventDelegate eventCallback
Definition: Ethernet.h:212
A network hardware (MAC) address.
Definition: MacAddress.h:38
Abstract Service class.
Definition: Ethernet.h:127
Speed
Link speed.
Definition: Ethernet.h:66
void onEvent(EventDelegate callback)
Set callback for ethernet events.
Definition: Ethernet.h:198
constexpr int8_t PIN_DEFAULT
Use default pin for platform.
Definition: Ethernet.h:54
String toString(Ethernet::Event event)
void onGotIp(GotIpDelegate callback)
Set callback for &#39;station connected with IP address&#39; event.
Definition: Ethernet.h:206
GotIpDelegate gotIpCallback
Definition: Ethernet.h:213
Virtual class used to construct a specific PHY instance.
Definition: Ethernet.h:97
Event
Ethernet event codes.
Definition: Ethernet.h:34
Definition: Dp83848.h:15
String toLongString(Ethernet::Event event)
#define ETHERNET_EVENT_MAP(XX)
Ethernet event code map.
Definition: Ethernet.h:23
PHY configuration.
Definition: Ethernet.h:84