1 | /*
|
---|
2 | modified 12 Aug 2013
|
---|
3 | by Soohwan Kim (suhwan@wiznet.co.kr)
|
---|
4 |
|
---|
5 | - 10 Apr. 2015
|
---|
6 | Added support for Arduino Ethernet Shield 2
|
---|
7 | by Arduino.org team
|
---|
8 |
|
---|
9 | */
|
---|
10 |
|
---|
11 | #include "Ethernet2.h"
|
---|
12 | #include "Dhcp.h"
|
---|
13 |
|
---|
14 | // XXX: don't make assumptions about the value of MAX_SOCK_NUM.
|
---|
15 | uint8_t EthernetClass::_state[MAX_SOCK_NUM] = { 0, };
|
---|
16 | uint16_t EthernetClass::_server_port[MAX_SOCK_NUM] = { 0, };
|
---|
17 |
|
---|
18 |
|
---|
19 |
|
---|
20 | #if defined(WIZ550io_WITH_MACADDRESS)
|
---|
21 | int EthernetClass::begin(void)
|
---|
22 | {
|
---|
23 | byte mac_address[6] ={0,};
|
---|
24 | _dhcp = new DhcpClass();
|
---|
25 |
|
---|
26 | // Initialise the basic info
|
---|
27 | w5500.init();
|
---|
28 | w5500.setIPAddress(IPAddress(0,0,0,0).raw_address());
|
---|
29 | w5500.getMACAddress(mac_address);
|
---|
30 |
|
---|
31 | // Now try to get our config info from a DHCP server
|
---|
32 | int ret = _dhcp->beginWithDHCP(mac_address);
|
---|
33 | if(ret == 1)
|
---|
34 | {
|
---|
35 | // We've successfully found a DHCP server and got our configuration info, so set things
|
---|
36 | // accordingly
|
---|
37 | w5500.setIPAddress(_dhcp->getLocalIp().raw_address());
|
---|
38 | w5500.setGatewayIp(_dhcp->getGatewayIp().raw_address());
|
---|
39 | w5500.setSubnetMask(_dhcp->getSubnetMask().raw_address());
|
---|
40 | _dnsServerAddress = _dhcp->getDnsServerIp();
|
---|
41 | }
|
---|
42 |
|
---|
43 | return ret;
|
---|
44 | }
|
---|
45 |
|
---|
46 | void EthernetClass::begin(IPAddress local_ip)
|
---|
47 | {
|
---|
48 | // Assume the DNS server will be the machine on the same network as the local IP
|
---|
49 | // but with last octet being '1'
|
---|
50 | IPAddress dns_server = local_ip;
|
---|
51 | dns_server[3] = 1;
|
---|
52 | begin(local_ip, dns_server);
|
---|
53 | }
|
---|
54 |
|
---|
55 | void EthernetClass::begin(IPAddress local_ip, IPAddress dns_server)
|
---|
56 | {
|
---|
57 | // Assume the gateway will be the machine on the same network as the local IP
|
---|
58 | // but with last octet being '1'
|
---|
59 | IPAddress gateway = local_ip;
|
---|
60 | gateway[3] = 1;
|
---|
61 | begin(local_ip, dns_server, gateway);
|
---|
62 | }
|
---|
63 |
|
---|
64 | void EthernetClass::begin(IPAddress local_ip, IPAddress dns_server, IPAddress gateway)
|
---|
65 | {
|
---|
66 | IPAddress subnet(255, 255, 255, 0);
|
---|
67 | begin(local_ip, dns_server, gateway, subnet);
|
---|
68 | }
|
---|
69 |
|
---|
70 | void EthernetClass::begin(IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet)
|
---|
71 | {
|
---|
72 | w5500.init();
|
---|
73 | w5500.setIPAddress(local_ip.raw_address());
|
---|
74 | w5500.setGatewayIp(gateway.raw_address());
|
---|
75 | w5500.setSubnetMask(subnet.raw_address());
|
---|
76 | _dnsServerAddress = dns_server;
|
---|
77 | }
|
---|
78 | #else
|
---|
79 | int EthernetClass::begin(uint8_t *mac_address)
|
---|
80 | {
|
---|
81 | _dhcp = new DhcpClass();
|
---|
82 | // Initialise the basic info
|
---|
83 | w5500.init();
|
---|
84 | w5500.setMACAddress(mac_address);
|
---|
85 | w5500.setIPAddress(IPAddress(0,0,0,0).raw_address());
|
---|
86 |
|
---|
87 | // Now try to get our config info from a DHCP server
|
---|
88 | int ret = _dhcp->beginWithDHCP(mac_address);
|
---|
89 | if(ret == 1)
|
---|
90 | {
|
---|
91 | // We've successfully found a DHCP server and got our configuration info, so set things
|
---|
92 | // accordingly
|
---|
93 | w5500.setIPAddress(_dhcp->getLocalIp().raw_address());
|
---|
94 | w5500.setGatewayIp(_dhcp->getGatewayIp().raw_address());
|
---|
95 | w5500.setSubnetMask(_dhcp->getSubnetMask().raw_address());
|
---|
96 | _dnsServerAddress = _dhcp->getDnsServerIp();
|
---|
97 | }
|
---|
98 |
|
---|
99 | return ret;
|
---|
100 | }
|
---|
101 |
|
---|
102 | void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip)
|
---|
103 | {
|
---|
104 | // Assume the DNS server will be the machine on the same network as the local IP
|
---|
105 | // but with last octet being '1'
|
---|
106 | IPAddress dns_server = local_ip;
|
---|
107 | dns_server[3] = 1;
|
---|
108 | begin(mac_address, local_ip, dns_server);
|
---|
109 | }
|
---|
110 |
|
---|
111 | void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server)
|
---|
112 | {
|
---|
113 | // Assume the gateway will be the machine on the same network as the local IP
|
---|
114 | // but with last octet being '1'
|
---|
115 | IPAddress gateway = local_ip;
|
---|
116 | gateway[3] = 1;
|
---|
117 | begin(mac_address, local_ip, dns_server, gateway);
|
---|
118 | }
|
---|
119 |
|
---|
120 | void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server, IPAddress gateway)
|
---|
121 | {
|
---|
122 | IPAddress subnet(255, 255, 255, 0);
|
---|
123 | begin(mac_address, local_ip, dns_server, gateway, subnet);
|
---|
124 | }
|
---|
125 |
|
---|
126 | void EthernetClass::begin(uint8_t *mac, IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet)
|
---|
127 | {
|
---|
128 | w5500.init();
|
---|
129 | w5500.setMACAddress(mac);
|
---|
130 | w5500.setIPAddress(local_ip.raw_address());
|
---|
131 | w5500.setGatewayIp(gateway.raw_address());
|
---|
132 | w5500.setSubnetMask(subnet.raw_address());
|
---|
133 | _dnsServerAddress = dns_server;
|
---|
134 | }
|
---|
135 |
|
---|
136 | #endif
|
---|
137 |
|
---|
138 | int EthernetClass::maintain(){
|
---|
139 | int rc = DHCP_CHECK_NONE;
|
---|
140 | if(_dhcp != NULL){
|
---|
141 | //we have a pointer to dhcp, use it
|
---|
142 | rc = _dhcp->checkLease();
|
---|
143 | switch ( rc ){
|
---|
144 | case DHCP_CHECK_NONE:
|
---|
145 | //nothing done
|
---|
146 | break;
|
---|
147 | case DHCP_CHECK_RENEW_OK:
|
---|
148 | case DHCP_CHECK_REBIND_OK:
|
---|
149 | //we might have got a new IP.
|
---|
150 | w5500.setIPAddress(_dhcp->getLocalIp().raw_address());
|
---|
151 | w5500.setGatewayIp(_dhcp->getGatewayIp().raw_address());
|
---|
152 | w5500.setSubnetMask(_dhcp->getSubnetMask().raw_address());
|
---|
153 | _dnsServerAddress = _dhcp->getDnsServerIp();
|
---|
154 | break;
|
---|
155 | default:
|
---|
156 | //this is actually a error, it will retry though
|
---|
157 | break;
|
---|
158 | }
|
---|
159 | }
|
---|
160 | return rc;
|
---|
161 | }
|
---|
162 |
|
---|
163 | IPAddress EthernetClass::localIP()
|
---|
164 | {
|
---|
165 | IPAddress ret;
|
---|
166 | w5500.getIPAddress(ret.raw_address());
|
---|
167 | return ret;
|
---|
168 | }
|
---|
169 |
|
---|
170 | IPAddress EthernetClass::subnetMask()
|
---|
171 | {
|
---|
172 | IPAddress ret;
|
---|
173 | w5500.getSubnetMask(ret.raw_address());
|
---|
174 | return ret;
|
---|
175 | }
|
---|
176 |
|
---|
177 | IPAddress EthernetClass::gatewayIP()
|
---|
178 | {
|
---|
179 | IPAddress ret;
|
---|
180 | w5500.getGatewayIp(ret.raw_address());
|
---|
181 | return ret;
|
---|
182 | }
|
---|
183 |
|
---|
184 | IPAddress EthernetClass::dnsServerIP()
|
---|
185 | {
|
---|
186 | return _dnsServerAddress;
|
---|
187 | }
|
---|
188 |
|
---|
189 | EthernetClass Ethernet;
|
---|