This repository has been archived by the owner on Jan 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Ethernet2.cpp
262 lines (223 loc) · 7.12 KB
/
Ethernet2.cpp
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
modified 12 Aug 2013
by Soohwan Kim (suhwan@wiznet.co.kr)
- 10 Apr. 2015
Added support for Arduino Ethernet Shield 2
by Arduino.org team
*/
#include "Ethernet2.h"
#include "Dhcp.h"
// XXX: don't make assumptions about the value of MAX_SOCK_NUM.
uint8_t EthernetClass::_state[MAX_SOCK_NUM] = { 0, };
uint16_t EthernetClass::_server_port[MAX_SOCK_NUM] = { 0, };
#if defined(WIZ550io_WITH_MACADDRESS)
int EthernetClass::begin(void)
{
// KH mod to work with new func void MACAddress(uint8_t *mac_address); and SinricPro v2.5.1+
// Now store to private var _mac_address
//byte mac_address[6] ={0,};
//////
if (_dhcp != NULL) {
delete _dhcp;
}
_dhcp = new DhcpClass();
// Initialise the basic info
w5500.init(w5500_cspin);
w5500.setIPAddress(IPAddress(0,0,0,0).raw_address());
// KH mod
w5500.getMACAddress(_mac_address);
// Now try to get our config info from a DHCP server
int ret = _dhcp->beginWithDHCP(_mac_address);
//////
if(ret == 1)
{
// We've successfully found a DHCP server and got our configuration info, so set things
// accordingly
w5500.setIPAddress(_dhcp->getLocalIp().raw_address());
w5500.setGatewayIp(_dhcp->getGatewayIp().raw_address());
w5500.setSubnetMask(_dhcp->getSubnetMask().raw_address());
_dnsServerAddress = _dhcp->getDnsServerIp();
_dnsDomainName = _dhcp->getDnsDomainName();
_hostName = _dhcp->getHostName();
}
return ret;
}
void EthernetClass::begin(IPAddress local_ip)
{
// Assume the DNS server will be the machine on the same network as the local IP
// but with last octet being '1'
IPAddress dns_server = local_ip;
dns_server[3] = 1;
begin(local_ip, dns_server);
}
void EthernetClass::begin(IPAddress local_ip, IPAddress dns_server)
{
// Assume the gateway will be the machine on the same network as the local IP
// but with last octet being '1'
IPAddress gateway = local_ip;
gateway[3] = 1;
begin(local_ip, dns_server, gateway);
}
void EthernetClass::begin(IPAddress local_ip, IPAddress dns_server, IPAddress gateway)
{
IPAddress subnet(255, 255, 255, 0);
begin(local_ip, dns_server, gateway, subnet);
}
void EthernetClass::begin(IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet)
{
w5500.init(w5500_cspin);
w5500.setIPAddress(local_ip.raw_address());
w5500.setGatewayIp(gateway.raw_address());
w5500.setSubnetMask(subnet.raw_address());
_dnsServerAddress = dns_server;
}
#else
int EthernetClass::begin(uint8_t *mac_address)
{
// KH mod to work with new func void MACAddress(uint8_t *mac_address); and SinricPro v2.5.1+
// Now store to private var
//uint8_t _mac_address[6] ={0,};
memcpy(_mac_address, mac_address, sizeof(_mac_address));
//////
if (_dhcp != NULL) {
delete _dhcp;
}
_dhcp = new DhcpClass();
// Initialise the basic info
w5500.init(w5500_cspin);
w5500.setMACAddress(mac_address);
w5500.setIPAddress(IPAddress(0,0,0,0).raw_address());
// Now try to get our config info from a DHCP server
int ret = _dhcp->beginWithDHCP(mac_address);
if(ret == 1)
{
// We've successfully found a DHCP server and got our configuration info, so set things
// accordingly
w5500.setIPAddress(_dhcp->getLocalIp().raw_address());
w5500.setGatewayIp(_dhcp->getGatewayIp().raw_address());
w5500.setSubnetMask(_dhcp->getSubnetMask().raw_address());
_dnsServerAddress = _dhcp->getDnsServerIp();
_dnsDomainName = _dhcp->getDnsDomainName();
_hostName = _dhcp->getHostName();
}
return ret;
}
void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip)
{
// KH mod to work with new func void MACAddress(uint8_t *mac_address); and SinricPro v2.5.1+
// Now store to private var
//uint8_t _mac_address[6] ={0,};
memcpy(_mac_address, mac_address, sizeof(_mac_address));
//////
// Assume the DNS server will be the machine on the same network as the local IP
// but with last octet being '1'
IPAddress dns_server = local_ip;
dns_server[3] = 1;
begin(mac_address, local_ip, dns_server);
}
void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server)
{
// KH mod to work with new func void MACAddress(uint8_t *mac_address); and SinricPro v2.5.1+
// Now store to private var
//uint8_t _mac_address[6] ={0,};
memcpy(_mac_address, mac_address, sizeof(_mac_address));
//////
// Assume the gateway will be the machine on the same network as the local IP
// but with last octet being '1'
IPAddress gateway = local_ip;
gateway[3] = 1;
begin(mac_address, local_ip, dns_server, gateway);
}
void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server, IPAddress gateway)
{
// KH mod to work with new func void MACAddress(uint8_t *mac_address); and SinricPro v2.5.1+
// Now store to private var
//uint8_t _mac_address[6] ={0,};
memcpy(_mac_address, mac_address, sizeof(_mac_address));
//////
IPAddress subnet(255, 255, 255, 0);
begin(mac_address, local_ip, dns_server, gateway, subnet);
}
void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet)
{
// KH mod to work with new func void MACAddress(uint8_t *mac_address); and SinricPro v2.5.1+
// Now store to private var
//uint8_t _mac_address[6] ={0,};
memcpy(_mac_address, mac_address, sizeof(_mac_address));
//////
w5500.init(w5500_cspin);
w5500.setMACAddress(mac_address);
w5500.setIPAddress(local_ip.raw_address());
w5500.setGatewayIp(gateway.raw_address());
w5500.setSubnetMask(subnet.raw_address());
_dnsServerAddress = dns_server;
}
#endif
int EthernetClass::maintain(){
int rc = DHCP_CHECK_NONE;
if(_dhcp != NULL){
//we have a pointer to dhcp, use it
rc = _dhcp->checkLease();
switch ( rc ){
case DHCP_CHECK_NONE:
//nothing done
break;
case DHCP_CHECK_RENEW_OK:
case DHCP_CHECK_REBIND_OK:
//we might have got a new IP.
w5500.setIPAddress(_dhcp->getLocalIp().raw_address());
w5500.setGatewayIp(_dhcp->getGatewayIp().raw_address());
w5500.setSubnetMask(_dhcp->getSubnetMask().raw_address());
_dnsServerAddress = _dhcp->getDnsServerIp();
_dnsDomainName = _dhcp->getDnsDomainName();
_hostName = _dhcp->getHostName();
break;
default:
//this is actually a error, it will retry though
break;
}
}
return rc;
}
// KH add to report link status
uint8_t EthernetClass::link()
{
return bitRead(w5500.getPHYCFGR(), 0);
}
const char* EthernetClass::linkReport()
{
if (bitRead(w5500.getPHYCFGR(), 0) == 1)
return "LINK";
else
return "NO LINK";
}
//////
IPAddress EthernetClass::localIP()
{
IPAddress ret;
w5500.getIPAddress(ret.raw_address());
return ret;
}
IPAddress EthernetClass::subnetMask()
{
IPAddress ret;
w5500.getSubnetMask(ret.raw_address());
return ret;
}
IPAddress EthernetClass::gatewayIP()
{
IPAddress ret;
w5500.getGatewayIp(ret.raw_address());
return ret;
}
IPAddress EthernetClass::dnsServerIP()
{
return _dnsServerAddress;
}
char* EthernetClass::dnsDomainName(){
return _dnsDomainName;
}
char* EthernetClass::hostName(){
return _hostName;
}
EthernetClass Ethernet;