|
| 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 | + // KH mod to work with new func void MACAddress(uint8_t *mac_address); and SinricPro v2.5.1+ |
| 24 | + // Now store to private var _mac_address |
| 25 | + //byte mac_address[6] ={0,}; |
| 26 | + ////// |
| 27 | + |
| 28 | + if (_dhcp != NULL) { |
| 29 | + delete _dhcp; |
| 30 | + } |
| 31 | + _dhcp = new DhcpClass(); |
| 32 | + |
| 33 | + // Initialise the basic info |
| 34 | + w5500.init(w5500_cspin); |
| 35 | + w5500.setIPAddress(IPAddress(0,0,0,0).raw_address()); |
| 36 | + |
| 37 | + // KH mod |
| 38 | + w5500.getMACAddress(_mac_address); |
| 39 | + |
| 40 | + // Now try to get our config info from a DHCP server |
| 41 | + int ret = _dhcp->beginWithDHCP(_mac_address); |
| 42 | + ////// |
| 43 | + |
| 44 | + if(ret == 1) |
| 45 | + { |
| 46 | + // We've successfully found a DHCP server and got our configuration info, so set things |
| 47 | + // accordingly |
| 48 | + w5500.setIPAddress(_dhcp->getLocalIp().raw_address()); |
| 49 | + w5500.setGatewayIp(_dhcp->getGatewayIp().raw_address()); |
| 50 | + w5500.setSubnetMask(_dhcp->getSubnetMask().raw_address()); |
| 51 | + _dnsServerAddress = _dhcp->getDnsServerIp(); |
| 52 | + _dnsDomainName = _dhcp->getDnsDomainName(); |
| 53 | + _hostName = _dhcp->getHostName(); |
| 54 | + } |
| 55 | + |
| 56 | + return ret; |
| 57 | +} |
| 58 | + |
| 59 | +void EthernetClass::begin(IPAddress local_ip) |
| 60 | +{ |
| 61 | + // Assume the DNS server will be the machine on the same network as the local IP |
| 62 | + // but with last octet being '1' |
| 63 | + IPAddress dns_server = local_ip; |
| 64 | + dns_server[3] = 1; |
| 65 | + begin(local_ip, dns_server); |
| 66 | +} |
| 67 | + |
| 68 | +void EthernetClass::begin(IPAddress local_ip, IPAddress dns_server) |
| 69 | +{ |
| 70 | + // Assume the gateway will be the machine on the same network as the local IP |
| 71 | + // but with last octet being '1' |
| 72 | + IPAddress gateway = local_ip; |
| 73 | + gateway[3] = 1; |
| 74 | + begin(local_ip, dns_server, gateway); |
| 75 | +} |
| 76 | + |
| 77 | +void EthernetClass::begin(IPAddress local_ip, IPAddress dns_server, IPAddress gateway) |
| 78 | +{ |
| 79 | + IPAddress subnet(255, 255, 255, 0); |
| 80 | + begin(local_ip, dns_server, gateway, subnet); |
| 81 | +} |
| 82 | + |
| 83 | +void EthernetClass::begin(IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet) |
| 84 | +{ |
| 85 | + w5500.init(w5500_cspin); |
| 86 | + w5500.setIPAddress(local_ip.raw_address()); |
| 87 | + w5500.setGatewayIp(gateway.raw_address()); |
| 88 | + w5500.setSubnetMask(subnet.raw_address()); |
| 89 | + _dnsServerAddress = dns_server; |
| 90 | +} |
| 91 | +#else |
| 92 | +int EthernetClass::begin(uint8_t *mac_address) |
| 93 | +{ |
| 94 | + // KH mod to work with new func void MACAddress(uint8_t *mac_address); and SinricPro v2.5.1+ |
| 95 | + // Now store to private var |
| 96 | + //uint8_t _mac_address[6] ={0,}; |
| 97 | + memcpy(_mac_address, mac_address, sizeof(_mac_address)); |
| 98 | + ////// |
| 99 | + |
| 100 | + if (_dhcp != NULL) { |
| 101 | + delete _dhcp; |
| 102 | + } |
| 103 | + _dhcp = new DhcpClass(); |
| 104 | + // Initialise the basic info |
| 105 | + w5500.init(w5500_cspin); |
| 106 | + w5500.setMACAddress(mac_address); |
| 107 | + w5500.setIPAddress(IPAddress(0,0,0,0).raw_address()); |
| 108 | + |
| 109 | + // Now try to get our config info from a DHCP server |
| 110 | + int ret = _dhcp->beginWithDHCP(mac_address); |
| 111 | + if(ret == 1) |
| 112 | + { |
| 113 | + // We've successfully found a DHCP server and got our configuration info, so set things |
| 114 | + // accordingly |
| 115 | + w5500.setIPAddress(_dhcp->getLocalIp().raw_address()); |
| 116 | + w5500.setGatewayIp(_dhcp->getGatewayIp().raw_address()); |
| 117 | + w5500.setSubnetMask(_dhcp->getSubnetMask().raw_address()); |
| 118 | + _dnsServerAddress = _dhcp->getDnsServerIp(); |
| 119 | + _dnsDomainName = _dhcp->getDnsDomainName(); |
| 120 | + _hostName = _dhcp->getHostName(); |
| 121 | + } |
| 122 | + |
| 123 | + return ret; |
| 124 | +} |
| 125 | + |
| 126 | +void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip) |
| 127 | +{ |
| 128 | + // KH mod to work with new func void MACAddress(uint8_t *mac_address); and SinricPro v2.5.1+ |
| 129 | + // Now store to private var |
| 130 | + //uint8_t _mac_address[6] ={0,}; |
| 131 | + memcpy(_mac_address, mac_address, sizeof(_mac_address)); |
| 132 | + ////// |
| 133 | + |
| 134 | + // Assume the DNS server will be the machine on the same network as the local IP |
| 135 | + // but with last octet being '1' |
| 136 | + IPAddress dns_server = local_ip; |
| 137 | + dns_server[3] = 1; |
| 138 | + begin(mac_address, local_ip, dns_server); |
| 139 | +} |
| 140 | + |
| 141 | +void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server) |
| 142 | +{ |
| 143 | + // KH mod to work with new func void MACAddress(uint8_t *mac_address); and SinricPro v2.5.1+ |
| 144 | + // Now store to private var |
| 145 | + //uint8_t _mac_address[6] ={0,}; |
| 146 | + memcpy(_mac_address, mac_address, sizeof(_mac_address)); |
| 147 | + ////// |
| 148 | + |
| 149 | + // Assume the gateway will be the machine on the same network as the local IP |
| 150 | + // but with last octet being '1' |
| 151 | + IPAddress gateway = local_ip; |
| 152 | + gateway[3] = 1; |
| 153 | + begin(mac_address, local_ip, dns_server, gateway); |
| 154 | +} |
| 155 | + |
| 156 | +void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server, IPAddress gateway) |
| 157 | +{ |
| 158 | + // KH mod to work with new func void MACAddress(uint8_t *mac_address); and SinricPro v2.5.1+ |
| 159 | + // Now store to private var |
| 160 | + //uint8_t _mac_address[6] ={0,}; |
| 161 | + memcpy(_mac_address, mac_address, sizeof(_mac_address)); |
| 162 | + ////// |
| 163 | + |
| 164 | + IPAddress subnet(255, 255, 255, 0); |
| 165 | + begin(mac_address, local_ip, dns_server, gateway, subnet); |
| 166 | +} |
| 167 | + |
| 168 | +void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet) |
| 169 | +{ |
| 170 | + // KH mod to work with new func void MACAddress(uint8_t *mac_address); and SinricPro v2.5.1+ |
| 171 | + // Now store to private var |
| 172 | + //uint8_t _mac_address[6] ={0,}; |
| 173 | + memcpy(_mac_address, mac_address, sizeof(_mac_address)); |
| 174 | + ////// |
| 175 | + |
| 176 | + w5500.init(w5500_cspin); |
| 177 | + w5500.setMACAddress(mac_address); |
| 178 | + w5500.setIPAddress(local_ip.raw_address()); |
| 179 | + w5500.setGatewayIp(gateway.raw_address()); |
| 180 | + w5500.setSubnetMask(subnet.raw_address()); |
| 181 | + _dnsServerAddress = dns_server; |
| 182 | +} |
| 183 | + |
| 184 | +#endif |
| 185 | + |
| 186 | +int EthernetClass::maintain(){ |
| 187 | + int rc = DHCP_CHECK_NONE; |
| 188 | + if(_dhcp != NULL){ |
| 189 | + //we have a pointer to dhcp, use it |
| 190 | + rc = _dhcp->checkLease(); |
| 191 | + switch ( rc ){ |
| 192 | + case DHCP_CHECK_NONE: |
| 193 | + //nothing done |
| 194 | + break; |
| 195 | + case DHCP_CHECK_RENEW_OK: |
| 196 | + case DHCP_CHECK_REBIND_OK: |
| 197 | + //we might have got a new IP. |
| 198 | + w5500.setIPAddress(_dhcp->getLocalIp().raw_address()); |
| 199 | + w5500.setGatewayIp(_dhcp->getGatewayIp().raw_address()); |
| 200 | + w5500.setSubnetMask(_dhcp->getSubnetMask().raw_address()); |
| 201 | + _dnsServerAddress = _dhcp->getDnsServerIp(); |
| 202 | + _dnsDomainName = _dhcp->getDnsDomainName(); |
| 203 | + _hostName = _dhcp->getHostName(); |
| 204 | + break; |
| 205 | + default: |
| 206 | + //this is actually a error, it will retry though |
| 207 | + break; |
| 208 | + } |
| 209 | + } |
| 210 | + return rc; |
| 211 | +} |
| 212 | + |
| 213 | +IPAddress EthernetClass::localIP() |
| 214 | +{ |
| 215 | + IPAddress ret; |
| 216 | + w5500.getIPAddress(ret.raw_address()); |
| 217 | + return ret; |
| 218 | +} |
| 219 | + |
| 220 | +IPAddress EthernetClass::subnetMask() |
| 221 | +{ |
| 222 | + IPAddress ret; |
| 223 | + w5500.getSubnetMask(ret.raw_address()); |
| 224 | + return ret; |
| 225 | +} |
| 226 | + |
| 227 | +IPAddress EthernetClass::gatewayIP() |
| 228 | +{ |
| 229 | + IPAddress ret; |
| 230 | + w5500.getGatewayIp(ret.raw_address()); |
| 231 | + return ret; |
| 232 | +} |
| 233 | + |
| 234 | +IPAddress EthernetClass::dnsServerIP() |
| 235 | +{ |
| 236 | + return _dnsServerAddress; |
| 237 | +} |
| 238 | + |
| 239 | +char* EthernetClass::dnsDomainName(){ |
| 240 | + return _dnsDomainName; |
| 241 | +} |
| 242 | + |
| 243 | +char* EthernetClass::hostName(){ |
| 244 | + return _hostName; |
| 245 | +} |
| 246 | + |
| 247 | +EthernetClass Ethernet; |
0 commit comments