as

Fork of WIZnetInterface by IOP

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers EthernetInterface.cpp Source File

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.h"
00020 #include "DHCPClient.h"
00021 
00022 
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()
00039 {
00040     dhcp = true;
00041     reset();
00042     
00043     return 0;
00044 }
00045 
00046 int EthernetInterface::init(uint8_t * mac)
00047 {
00048     dhcp = true;
00049     //
00050     for (int i =0; i < 6; i++) this->mac[i] = mac[i];
00051     //
00052     reset();
00053     
00054     return 0;
00055 }
00056 
00057 int EthernetInterface::init(uint8_t * mac, const char* ip, const char* mask, const char* gateway)
00058 {
00059     dhcp = false;
00060     //
00061     for (int i =0; i < 6; i++) this->mac[i] = mac[i];
00062     //
00063     this->ip = str_to_ip(ip);
00064     strcpy(ip_string, ip);
00065     ip_set = true;
00066     this->netmask = str_to_ip(mask);
00067     this->gateway = str_to_ip(gateway);
00068     reset();
00069 
00070     // @Jul. 8. 2014 add code. should be called to write chip.
00071     setmac();
00072     setip();
00073     
00074     return 0;
00075 }
00076 
00077 // Connect Bring the interface up, start DHCP if needed.
00078 int EthernetInterface::connect()
00079 {
00080     if (dhcp) {
00081         int r = IPrenew();
00082         if (r < 0) {
00083             return r;
00084         }
00085     }
00086     
00087     if (WIZnet_Chip::setip() == false) return -1;
00088     return 0;
00089 }
00090 
00091 // Disconnect Bring the interface down.
00092 int EthernetInterface::disconnect()
00093 {
00094     //if (WIZnet_Chip::disconnect() == false) return -1;
00095     return 0;
00096 }
00097 
00098 
00099 char* EthernetInterface::getIPAddress()
00100 {
00101     uint32_t ip = reg_rd<uint32_t>(SIPR);
00102     snprintf(ip_string, sizeof(ip_string), "%d.%d.%d.%d", 
00103                 (uint8_t)((ip>>24)&0xff), 
00104                 (uint8_t)((ip>>16)&0xff), 
00105                 (uint8_t)((ip>>8)&0xff), 
00106                 (uint8_t)(ip&0xff));
00107     return ip_string;
00108 }
00109 
00110 char* EthernetInterface::getNetworkMask()
00111 {
00112     uint32_t ip = reg_rd<uint32_t>(SUBR);
00113     snprintf(mask_string, sizeof(mask_string), "%d.%d.%d.%d", 
00114                 (uint8_t)((ip>>24)&0xff), 
00115                 (uint8_t)((ip>>16)&0xff), 
00116                 (uint8_t)((ip>>8)&0xff), 
00117                 (uint8_t)(ip&0xff));
00118     return mask_string;
00119 }
00120 
00121 char* EthernetInterface::getGateway()
00122 {
00123     uint32_t ip = reg_rd<uint32_t>(GAR);
00124     snprintf(gw_string, sizeof(gw_string), "%d.%d.%d.%d", 
00125                 (uint8_t)((ip>>24)&0xff), 
00126                 (uint8_t)((ip>>16)&0xff), 
00127                 (uint8_t)((ip>>8)&0xff), 
00128                 (uint8_t)(ip&0xff));
00129     return gw_string;
00130 }
00131 
00132 char* EthernetInterface::getMACAddress()
00133 {
00134     uint8_t mac[6];
00135     reg_rd_mac(SHAR, mac);
00136     snprintf(mac_string, sizeof(mac_string), "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
00137     //ethernet_address(mac_string);
00138     return mac_string; 
00139     
00140 }
00141 
00142 int EthernetInterface::IPrenew(int timeout_ms)
00143 {
00144     DHCPClient dhcp;
00145     int err = dhcp.setup(timeout_ms);
00146     if (err == (-1)) {
00147         return -1;
00148     }
00149 //    printf("Connected, IP: %d.%d.%d.%d\n", dhcp.yiaddr[0], dhcp.yiaddr[1], dhcp.yiaddr[2], dhcp.yiaddr[3]);
00150     ip      = (dhcp.yiaddr[0] <<24) | (dhcp.yiaddr[1] <<16) | (dhcp.yiaddr[2] <<8) | dhcp.yiaddr[3];
00151     gateway = (dhcp.gateway[0]<<24) | (dhcp.gateway[1]<<16) | (dhcp.gateway[2]<<8) | dhcp.gateway[3];
00152     netmask = (dhcp.netmask[0]<<24) | (dhcp.netmask[1]<<16) | (dhcp.netmask[2]<<8) | dhcp.netmask[3];
00153     dnsaddr = (dhcp.dnsaddr[0]<<24) | (dhcp.dnsaddr[1]<<16) | (dhcp.dnsaddr[2]<<8) | dhcp.dnsaddr[3];
00154     return 0;
00155 }
00156