-
Notifications
You must be signed in to change notification settings - Fork 13.3k
/
Copy pathEthernetCompat.h
127 lines (110 loc) · 3.24 KB
/
EthernetCompat.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#pragma once
#include <LwipEthernet.h>
#include <WiFiUdp.h>
#include <WiFiClient.h>
#include <ArduinoWiFiServer.h>
using EthernetUDP = WiFiUDP;
using EthernetClient = WiFiClient;
using EthernetServer = ArduinoWiFiServer;
enum
{
DHCP_CHECK_NONE = 0,
DHCP_CHECK_RENEW_FAIL = 1,
DHCP_CHECK_RENEW_OK = 2,
DHCP_CHECK_REBIND_FAIL = 3,
DHCP_CHECK_REBIND_OK = 4,
};
enum HardwareStatus
{
EthernetNoHardware,
EthernetHardwareFound,
};
template<class RawDev>
class ArduinoEthernet: public LwipIntfDev<RawDev>
{
public:
ArduinoEthernet(int8_t cs = SS, SPIClass& spi = SPI, int8_t intr = -1) :
LwipIntfDev<RawDev>(cs, spi, intr)
{
_hardwareStatus = EthernetNoHardware;
}
// Arduino-Ethernet API compatibility, order can be either:
// mac, ip, gateway, netmask, dns (esp8266 or natural order)
// mac, ip, dns, gateway, netmask (Arduino legacy)
boolean begin(const uint8_t* macAddress, IPAddress local_ip = IPADDR_NONE,
IPAddress arg1 = IPADDR_NONE, IPAddress arg2 = IPADDR_NONE,
IPAddress arg3 = IPADDR_NONE)
{
if (local_ip.isSet() && local_ip.isV4())
{
// setting auto values using arduino ordering of parameters
if (arg1 == IPADDR_NONE) // else dns or gw
{
arg1 = local_ip;
arg1[3] = 1;
}
if (arg2 == IPADDR_NONE) // else gw or mask
{
arg2 = local_ip;
arg2[3] = 1;
}
// if arg2 is mask (esp ordering), let DNS IP unconfigured
if (arg3 == IPADDR_NONE && arg2[0] != 255) // else mask or dns
{
arg3 = IPAddress(255, 255, 255, 0);
}
}
SPI4EthInit(); // Arduino Ethernet self-initializes SPI
bool ret = true;
if (local_ip.isSet())
ret = LwipIntfDev<RawDev>::config(local_ip, arg1, arg2, arg3);
if (ret)
{
ret = LwipIntfDev<RawDev>::begin(macAddress);
if (!local_ip.isSet())
{
// Arduino API waits for DHCP answer
while (!LwipIntfDev<RawDev>::connected())
{
delay(100);
}
}
}
if (ret)
{
_hardwareStatus = EthernetHardwareFound;
}
return ret;
}
void end()
{
ip_addr_copy(LwipIntfDev<RawDev>::_netif.ip_addr,
ip_addr_any); // to allow DHCP at next begin
LwipIntfDev<RawDev>::end();
}
HardwareStatus hardwareStatus() const
{
return _hardwareStatus;
}
int maintain() const
{
return DHCP_CHECK_NONE;
}
void MACAddress(uint8_t* mac)
{
LwipIntfDev<RawDev>::macAddress(mac);
}
IPAddress dnsServerIP() const
{
return LwipIntfDev<RawDev>::dnsIP(0);
}
void setDnsServerIP(const IPAddress dnsIP)
{
LwipIntfDev<RawDev>::setDNS(dnsIP);
}
protected:
HardwareStatus _hardwareStatus;
};
using ArduinoWiznet5500lwIP = ArduinoEthernet<Wiznet5500>;
using ArduinoWiznet5100lwIP = ArduinoEthernet<Wiznet5100>;
using ArduinoENC28J60lwIP = ArduinoEthernet<ENC28J60>;