WIZNet W5500 with additional enhancements

Fork of WIZnetInterface by WIZnet

Committer:
Helmut Tschemernjak
Date:
Wed Oct 11 11:18:41 2017 +0200
Revision:
36:0ba2e8d5274a
Parent:
35:fe3028eda085
Child:
38:67e763cdde02
More DHCP cleanup, added precise DHCP packet construction
DHCP hostname support (added to the DHCP request) which
allows dynamic DNS updates and routers will show the client name.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Soohwan Kim 0:6f28332c466f 1 /* Copyright (C) 2012 mbed.org, MIT License
Soohwan Kim 0:6f28332c466f 2 *
Soohwan Kim 0:6f28332c466f 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
Soohwan Kim 0:6f28332c466f 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
Soohwan Kim 0:6f28332c466f 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
Soohwan Kim 0:6f28332c466f 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
Soohwan Kim 0:6f28332c466f 7 * furnished to do so, subject to the following conditions:
Soohwan Kim 0:6f28332c466f 8 *
Soohwan Kim 0:6f28332c466f 9 * The above copyright notice and this permission notice shall be included in all copies or
Soohwan Kim 0:6f28332c466f 10 * substantial portions of the Software.
Soohwan Kim 0:6f28332c466f 11 *
Soohwan Kim 0:6f28332c466f 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
Soohwan Kim 0:6f28332c466f 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Soohwan Kim 0:6f28332c466f 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
Soohwan Kim 0:6f28332c466f 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Soohwan Kim 0:6f28332c466f 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Soohwan Kim 0:6f28332c466f 17 */
Soohwan Kim 0:6f28332c466f 18
Soohwan Kim 0:6f28332c466f 19 #include "EthernetInterface.h"
Soohwan Kim 0:6f28332c466f 20 #include "DHCPClient.h"
embeddist 7:da6fcec0f3fe 21
embeddist 28:200e63e513a8 22 #if (not defined TARGET_WIZwiki_W7500) && (not defined TARGET_WIZwiki_W7500P) && (not defined TARGET_WIZwiki_W7500ECO)
embeddist 3:f8c6efc8bf83 23 EthernetInterface::EthernetInterface(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset) :
embeddist 3:f8c6efc8bf83 24 WIZnet_Chip(mosi, miso, sclk, cs, reset)
embeddist 3:f8c6efc8bf83 25 {
embeddist 3:f8c6efc8bf83 26 ip_set = false;
Helmut Tschemernjak 35:fe3028eda085 27 domainName = NULL;
Helmut Tschemernjak 35:fe3028eda085 28 leaseStart = 0;
Helmut Tschemernjak 36:0ba2e8d5274a 29 _hostname = NULL;
embeddist 3:f8c6efc8bf83 30 }
embeddist 3:f8c6efc8bf83 31
embeddist 3:f8c6efc8bf83 32 EthernetInterface::EthernetInterface(SPI* spi, PinName cs, PinName reset) :
embeddist 3:f8c6efc8bf83 33 WIZnet_Chip(spi, cs, reset)
embeddist 3:f8c6efc8bf83 34 {
embeddist 3:f8c6efc8bf83 35 ip_set = false;
Helmut Tschemernjak 35:fe3028eda085 36 domainName = NULL;
Helmut Tschemernjak 35:fe3028eda085 37 leaseStart = 0;
Helmut Tschemernjak 36:0ba2e8d5274a 38 _hostname = NULL;
embeddist 3:f8c6efc8bf83 39 }
embeddist 7:da6fcec0f3fe 40 #endif
Soohwan Kim 0:6f28332c466f 41
Helmut Tschemernjak 35:fe3028eda085 42 EthernetInterface::~EthernetInterface()
Helmut Tschemernjak 35:fe3028eda085 43 {
Helmut Tschemernjak 35:fe3028eda085 44 if (domainName)
Helmut Tschemernjak 35:fe3028eda085 45 free(domainName);
Helmut Tschemernjak 35:fe3028eda085 46 }
Helmut Tschemernjak 35:fe3028eda085 47
kaizen 20:bda61525ac71 48 int EthernetInterface::init()
kaizen 20:bda61525ac71 49 {
kaizen 20:bda61525ac71 50 dhcp = true;
kaizen 20:bda61525ac71 51 reset();
kaizen 20:bda61525ac71 52
kaizen 20:bda61525ac71 53 return 0;
kaizen 20:bda61525ac71 54 }
kaizen 20:bda61525ac71 55
Helmut Tschemernjak 34:7d44648ec5f2 56 int EthernetInterface::init(uint8_t *mac)
Soohwan Kim 0:6f28332c466f 57 {
Soohwan Kim 0:6f28332c466f 58 dhcp = true;
Soohwan Kim 0:6f28332c466f 59 //
Soohwan Kim 0:6f28332c466f 60 for (int i =0; i < 6; i++) this->mac[i] = mac[i];
Soohwan Kim 0:6f28332c466f 61 //
Soohwan Kim 0:6f28332c466f 62 reset();
Helmut Tschemernjak 31:a3fbfa5c8351 63 setmac();
Soohwan Kim 0:6f28332c466f 64 return 0;
Soohwan Kim 0:6f28332c466f 65 }
Soohwan Kim 0:6f28332c466f 66
Helmut Tschemernjak 34:7d44648ec5f2 67 int EthernetInterface::init(uint8_t * mac, const char* ip, const char* mask, const char* gateway, const char* dnsServer)
Soohwan Kim 0:6f28332c466f 68 {
Soohwan Kim 0:6f28332c466f 69 dhcp = false;
Soohwan Kim 0:6f28332c466f 70 //
Soohwan Kim 0:6f28332c466f 71 for (int i =0; i < 6; i++) this->mac[i] = mac[i];
Soohwan Kim 0:6f28332c466f 72 //
Soohwan Kim 0:6f28332c466f 73 this->ip = str_to_ip(ip);
Soohwan Kim 0:6f28332c466f 74 strcpy(ip_string, ip);
Soohwan Kim 0:6f28332c466f 75 ip_set = true;
Soohwan Kim 0:6f28332c466f 76 this->netmask = str_to_ip(mask);
Soohwan Kim 0:6f28332c466f 77 this->gateway = str_to_ip(gateway);
Helmut Tschemernjak 34:7d44648ec5f2 78 if (dnsServer)
Helmut Tschemernjak 34:7d44648ec5f2 79 this->dnsaddr = str_to_ip(dnsServer);
Soohwan Kim 0:6f28332c466f 80 reset();
Soohwan Kim 4:4930f81bbe98 81
Soohwan Kim 4:4930f81bbe98 82 // @Jul. 8. 2014 add code. should be called to write chip.
Soohwan Kim 4:4930f81bbe98 83 setmac();
Soohwan Kim 4:4930f81bbe98 84 setip();
embeddist 3:f8c6efc8bf83 85
Soohwan Kim 0:6f28332c466f 86 return 0;
Soohwan Kim 0:6f28332c466f 87 }
Soohwan Kim 0:6f28332c466f 88
Soohwan Kim 0:6f28332c466f 89 // Connect Bring the interface up, start DHCP if needed.
Helmut Tschemernjak 36:0ba2e8d5274a 90 int EthernetInterface::connect(const char *hostname)
Soohwan Kim 0:6f28332c466f 91 {
Helmut Tschemernjak 36:0ba2e8d5274a 92 if (hostname)
Helmut Tschemernjak 36:0ba2e8d5274a 93 _hostname = hostname;
Soohwan Kim 0:6f28332c466f 94 if (dhcp) {
Soohwan Kim 0:6f28332c466f 95 int r = IPrenew();
Soohwan Kim 0:6f28332c466f 96 if (r < 0) {
Soohwan Kim 0:6f28332c466f 97 return r;
Soohwan Kim 0:6f28332c466f 98 }
Soohwan Kim 0:6f28332c466f 99 }
Soohwan Kim 0:6f28332c466f 100 if (WIZnet_Chip::setip() == false) return -1;
Soohwan Kim 0:6f28332c466f 101 return 0;
Soohwan Kim 0:6f28332c466f 102 }
Soohwan Kim 0:6f28332c466f 103
Soohwan Kim 0:6f28332c466f 104 // Disconnect Bring the interface down.
Soohwan Kim 0:6f28332c466f 105 int EthernetInterface::disconnect()
Soohwan Kim 0:6f28332c466f 106 {
Soohwan Kim 0:6f28332c466f 107 //if (WIZnet_Chip::disconnect() == false) return -1;
Soohwan Kim 0:6f28332c466f 108 return 0;
Soohwan Kim 0:6f28332c466f 109 }
Soohwan Kim 0:6f28332c466f 110
Soohwan Kim 0:6f28332c466f 111
Soohwan Kim 0:6f28332c466f 112 char* EthernetInterface::getIPAddress()
Soohwan Kim 0:6f28332c466f 113 {
Soohwan Kim 0:6f28332c466f 114 uint32_t ip = reg_rd<uint32_t>(SIPR);
Soohwan Kim 0:6f28332c466f 115 snprintf(ip_string, sizeof(ip_string), "%d.%d.%d.%d",
Soohwan Kim 0:6f28332c466f 116 (uint8_t)((ip>>24)&0xff),
Soohwan Kim 0:6f28332c466f 117 (uint8_t)((ip>>16)&0xff),
Soohwan Kim 0:6f28332c466f 118 (uint8_t)((ip>>8)&0xff),
Soohwan Kim 0:6f28332c466f 119 (uint8_t)(ip&0xff));
Soohwan Kim 0:6f28332c466f 120 return ip_string;
Soohwan Kim 0:6f28332c466f 121 }
Soohwan Kim 0:6f28332c466f 122
Soohwan Kim 0:6f28332c466f 123 char* EthernetInterface::getNetworkMask()
Soohwan Kim 0:6f28332c466f 124 {
Soohwan Kim 0:6f28332c466f 125 uint32_t ip = reg_rd<uint32_t>(SUBR);
Soohwan Kim 0:6f28332c466f 126 snprintf(mask_string, sizeof(mask_string), "%d.%d.%d.%d",
Soohwan Kim 0:6f28332c466f 127 (uint8_t)((ip>>24)&0xff),
Soohwan Kim 0:6f28332c466f 128 (uint8_t)((ip>>16)&0xff),
Soohwan Kim 0:6f28332c466f 129 (uint8_t)((ip>>8)&0xff),
Soohwan Kim 0:6f28332c466f 130 (uint8_t)(ip&0xff));
Soohwan Kim 0:6f28332c466f 131 return mask_string;
Soohwan Kim 0:6f28332c466f 132 }
Soohwan Kim 0:6f28332c466f 133
Soohwan Kim 0:6f28332c466f 134 char* EthernetInterface::getGateway()
Soohwan Kim 0:6f28332c466f 135 {
Soohwan Kim 0:6f28332c466f 136 uint32_t ip = reg_rd<uint32_t>(GAR);
Soohwan Kim 0:6f28332c466f 137 snprintf(gw_string, sizeof(gw_string), "%d.%d.%d.%d",
Soohwan Kim 0:6f28332c466f 138 (uint8_t)((ip>>24)&0xff),
Soohwan Kim 0:6f28332c466f 139 (uint8_t)((ip>>16)&0xff),
Soohwan Kim 0:6f28332c466f 140 (uint8_t)((ip>>8)&0xff),
Soohwan Kim 0:6f28332c466f 141 (uint8_t)(ip&0xff));
Soohwan Kim 0:6f28332c466f 142 return gw_string;
Soohwan Kim 0:6f28332c466f 143 }
Soohwan Kim 0:6f28332c466f 144
Helmut Tschemernjak 33:879cfe51e66e 145
Helmut Tschemernjak 33:879cfe51e66e 146 char* EthernetInterface::getDNSServer()
Helmut Tschemernjak 33:879cfe51e66e 147 {
Helmut Tschemernjak 33:879cfe51e66e 148 uint32_t ip = getDNSAddr();
Helmut Tschemernjak 34:7d44648ec5f2 149 snprintf(dns_string, sizeof(gw_string), "%d.%d.%d.%d",
Helmut Tschemernjak 33:879cfe51e66e 150 (uint8_t)((ip>>24)&0xff),
Helmut Tschemernjak 33:879cfe51e66e 151 (uint8_t)((ip>>16)&0xff),
Helmut Tschemernjak 33:879cfe51e66e 152 (uint8_t)((ip>>8)&0xff),
Helmut Tschemernjak 33:879cfe51e66e 153 (uint8_t)(ip&0xff));
Helmut Tschemernjak 34:7d44648ec5f2 154 return dns_string;
Helmut Tschemernjak 33:879cfe51e66e 155 }
Helmut Tschemernjak 33:879cfe51e66e 156
Soohwan Kim 0:6f28332c466f 157 char* EthernetInterface::getMACAddress()
Soohwan Kim 0:6f28332c466f 158 {
Soohwan Kim 0:6f28332c466f 159 uint8_t mac[6];
Soohwan Kim 0:6f28332c466f 160 reg_rd_mac(SHAR, mac);
Soohwan Kim 0:6f28332c466f 161 snprintf(mac_string, sizeof(mac_string), "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
Soohwan Kim 0:6f28332c466f 162 //ethernet_address(mac_string);
Soohwan Kim 0:6f28332c466f 163 return mac_string;
Soohwan Kim 0:6f28332c466f 164
Soohwan Kim 0:6f28332c466f 165 }
Soohwan Kim 0:6f28332c466f 166
Helmut Tschemernjak 35:fe3028eda085 167
Soohwan Kim 0:6f28332c466f 168 int EthernetInterface::IPrenew(int timeout_ms)
Soohwan Kim 0:6f28332c466f 169 {
Soohwan Kim 0:6f28332c466f 170 DHCPClient dhcp;
Helmut Tschemernjak 36:0ba2e8d5274a 171 int err = dhcp.setup(_hostname, timeout_ms);
Soohwan Kim 0:6f28332c466f 172 if (err == (-1)) {
Soohwan Kim 0:6f28332c466f 173 return -1;
Soohwan Kim 0:6f28332c466f 174 }
Soohwan Kim 0:6f28332c466f 175 // printf("Connected, IP: %d.%d.%d.%d\n", dhcp.yiaddr[0], dhcp.yiaddr[1], dhcp.yiaddr[2], dhcp.yiaddr[3]);
Helmut Tschemernjak 35:fe3028eda085 176 /*
Helmut Tschemernjak 35:fe3028eda085 177 * Sync DHCP response variables
Helmut Tschemernjak 35:fe3028eda085 178 */
Soohwan Kim 0:6f28332c466f 179 ip = (dhcp.yiaddr[0] <<24) | (dhcp.yiaddr[1] <<16) | (dhcp.yiaddr[2] <<8) | dhcp.yiaddr[3];
Soohwan Kim 0:6f28332c466f 180 gateway = (dhcp.gateway[0]<<24) | (dhcp.gateway[1]<<16) | (dhcp.gateway[2]<<8) | dhcp.gateway[3];
Soohwan Kim 0:6f28332c466f 181 netmask = (dhcp.netmask[0]<<24) | (dhcp.netmask[1]<<16) | (dhcp.netmask[2]<<8) | dhcp.netmask[3];
Soohwan Kim 0:6f28332c466f 182 dnsaddr = (dhcp.dnsaddr[0]<<24) | (dhcp.dnsaddr[1]<<16) | (dhcp.dnsaddr[2]<<8) | dhcp.dnsaddr[3];
Helmut Tschemernjak 35:fe3028eda085 183 timesrv = (dhcp.timesrv[0]<<24) | (dhcp.timesrv[1]<<16) | (dhcp.timesrv[2]<<8) | dhcp.timesrv[3];
Helmut Tschemernjak 35:fe3028eda085 184 leaseTime = (dhcp.leaseTime[0]<<24) | (dhcp.leaseTime[1]<<16) | (dhcp.leaseTime[2]<<8) | dhcp.leaseTime[3];
Helmut Tschemernjak 35:fe3028eda085 185 leaseStart = time(NULL);
Helmut Tschemernjak 35:fe3028eda085 186 if (domainName) {
Helmut Tschemernjak 35:fe3028eda085 187 free(domainName);
Helmut Tschemernjak 35:fe3028eda085 188 domainName = NULL;
Helmut Tschemernjak 35:fe3028eda085 189 }
Helmut Tschemernjak 35:fe3028eda085 190 if (dhcp.domainName) {
Helmut Tschemernjak 35:fe3028eda085 191 domainName = dhcp.domainName;
Helmut Tschemernjak 35:fe3028eda085 192 dhcp.domainName = NULL;
Helmut Tschemernjak 35:fe3028eda085 193 }
Soohwan Kim 0:6f28332c466f 194 return 0;
Soohwan Kim 0:6f28332c466f 195 }
Soohwan Kim 0:6f28332c466f 196