WIZnetInterface using namespace
Dependents: DualNetworkInterface-Basic
Fork of WIZnetInterface by
EthernetInterface.cpp
00001 /* Copyright (C) 2012 mbed.org, MIT License 00002 * 00003 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00004 * and associated documentation files (the "Software"), to deal in the Software without restriction, 00005 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 00006 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 00007 * furnished to do so, subject to the following conditions: 00008 * 00009 * The above copyright notice and this permission notice shall be included in all copies or 00010 * substantial portions of the Software. 00011 * 00012 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 00013 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00014 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00015 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00016 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00017 */ 00018 00019 #include "EthernetInterface.hpp" 00020 #include "DHCPClient.hpp" 00021 00022 using namespace wiznet_space; 00023 00024 #if not defined(TARGET_WIZwiki_W7500) 00025 EthernetInterface::EthernetInterface(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset) : 00026 WIZnet_Chip(mosi, miso, sclk, cs, reset) 00027 { 00028 ip_set = false; 00029 } 00030 00031 EthernetInterface::EthernetInterface(SPI* spi, PinName cs, PinName reset) : 00032 WIZnet_Chip(spi, cs, reset) 00033 { 00034 ip_set = false; 00035 } 00036 #endif 00037 00038 int EthernetInterface::init(uint8_t * mac) 00039 { 00040 dhcp = true; 00041 // 00042 for (int i =0; i < 6; i++) this->mac[i] = mac[i]; 00043 // 00044 reset(); 00045 00046 return 0; 00047 } 00048 00049 int EthernetInterface::init(uint8_t * mac, const char* ip, const char* mask, const char* gateway) 00050 { 00051 dhcp = false; 00052 // 00053 for (int i =0; i < 6; i++) this->mac[i] = mac[i]; 00054 // 00055 this->ip = str_to_ip(ip); 00056 strcpy(ip_string, ip); 00057 ip_set = true; 00058 this->netmask = str_to_ip(mask); 00059 this->gateway = str_to_ip(gateway); 00060 reset(); 00061 00062 // @Jul. 8. 2014 add code. should be called to write chip. 00063 setmac(); 00064 setip(); 00065 00066 return 0; 00067 } 00068 00069 // Connect Bring the interface up, start DHCP if needed. 00070 int EthernetInterface::connect() 00071 { 00072 if (dhcp) { 00073 int r = IPrenew(); 00074 if (r < 0) { 00075 return r; 00076 } 00077 } 00078 00079 if (WIZnet_Chip::setip() == false) return -1; 00080 return 0; 00081 } 00082 00083 // Disconnect Bring the interface down. 00084 int EthernetInterface::disconnect() 00085 { 00086 //if (WIZnet_Chip::disconnect() == false) return -1; 00087 return 0; 00088 } 00089 00090 00091 char* EthernetInterface::getIPAddress() 00092 { 00093 uint32_t ip = reg_rd<uint32_t>(SIPR); 00094 snprintf(ip_string, sizeof(ip_string), "%d.%d.%d.%d", 00095 (uint8_t)((ip>>24)&0xff), 00096 (uint8_t)((ip>>16)&0xff), 00097 (uint8_t)((ip>>8)&0xff), 00098 (uint8_t)(ip&0xff)); 00099 return ip_string; 00100 } 00101 00102 char* EthernetInterface::getNetworkMask() 00103 { 00104 uint32_t ip = reg_rd<uint32_t>(SUBR); 00105 snprintf(mask_string, sizeof(mask_string), "%d.%d.%d.%d", 00106 (uint8_t)((ip>>24)&0xff), 00107 (uint8_t)((ip>>16)&0xff), 00108 (uint8_t)((ip>>8)&0xff), 00109 (uint8_t)(ip&0xff)); 00110 return mask_string; 00111 } 00112 00113 char* EthernetInterface::getGateway() 00114 { 00115 uint32_t ip = reg_rd<uint32_t>(GAR); 00116 snprintf(gw_string, sizeof(gw_string), "%d.%d.%d.%d", 00117 (uint8_t)((ip>>24)&0xff), 00118 (uint8_t)((ip>>16)&0xff), 00119 (uint8_t)((ip>>8)&0xff), 00120 (uint8_t)(ip&0xff)); 00121 return gw_string; 00122 } 00123 00124 char* EthernetInterface::getMACAddress() 00125 { 00126 uint8_t mac[6]; 00127 reg_rd_mac(SHAR, mac); 00128 snprintf(mac_string, sizeof(mac_string), "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); 00129 //ethernet_address(mac_string); 00130 return mac_string; 00131 00132 } 00133 00134 int EthernetInterface::IPrenew(int timeout_ms) 00135 { 00136 DHCPClient dhcp; 00137 int err = dhcp.setup(timeout_ms); 00138 if (err == (-1)) { 00139 return -1; 00140 } 00141 // printf("Connected, IP: %d.%d.%d.%d\n", dhcp.yiaddr[0], dhcp.yiaddr[1], dhcp.yiaddr[2], dhcp.yiaddr[3]); 00142 ip = (dhcp.yiaddr[0] <<24) | (dhcp.yiaddr[1] <<16) | (dhcp.yiaddr[2] <<8) | dhcp.yiaddr[3]; 00143 gateway = (dhcp.gateway[0]<<24) | (dhcp.gateway[1]<<16) | (dhcp.gateway[2]<<8) | dhcp.gateway[3]; 00144 netmask = (dhcp.netmask[0]<<24) | (dhcp.netmask[1]<<16) | (dhcp.netmask[2]<<8) | dhcp.netmask[3]; 00145 dnsaddr = (dhcp.dnsaddr[0]<<24) | (dhcp.dnsaddr[1]<<16) | (dhcp.dnsaddr[2]<<8) | dhcp.dnsaddr[3]; 00146 return 0; 00147 } 00148
Generated on Wed Jul 13 2022 05:21:37 by 1.7.2