Norimasa Okamoto / WIZ820ioInterface

Dependents:   Seeed_Ethernet_Shield_V2_HelloWorld Seeed_Ethernet_Shield Cayenne-WIZ820ioInterface Seeed_Ethernet_Shield

Fork of WiflyInterface by mbed official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers WIZ820ioInterface.cpp Source File

WIZ820ioInterface.cpp

00001 // WIZ820ioInterface.cpp 2013/4/5
00002 #include "WIZ820ioInterface.h"
00003 #include "DHCPClient.h"
00004 
00005 WIZ820ioInterface::WIZ820ioInterface(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset) :
00006     WIZ820io(mosi, miso, sclk, cs, reset)
00007 {
00008     ip_set = false;
00009 }
00010 
00011 WIZ820ioInterface::WIZ820ioInterface(SPI* spi, PinName cs, PinName reset) :
00012     WIZ820io(spi, cs, reset)
00013 {
00014     ip_set = false;
00015 }
00016 
00017 int WIZ820ioInterface::init()
00018 {
00019     dhcp = true;
00020     reset();
00021     return 0;
00022 }
00023 
00024 int WIZ820ioInterface::init(const char* ip, const char* mask, const char* gateway)
00025 {
00026     dhcp = false;
00027     this->ip = str_to_ip(ip);
00028     strcpy(ip_string, ip);
00029     ip_set = true;
00030     this->netmask = str_to_ip(mask);
00031     this->gateway = str_to_ip(gateway);
00032     reset();
00033     return 0;
00034 }
00035 
00036 int WIZ820ioInterface::connect()
00037 {
00038     if (dhcp) {
00039         int r = IPrenew();
00040         if (r < 0) {
00041             return r;
00042         }
00043     }
00044     return join();
00045 }
00046 
00047 int WIZ820ioInterface::disconnect()
00048 {
00049     return WIZ820io::disconnect();
00050 }
00051 
00052 char* WIZ820ioInterface::getIPAddress()
00053 {
00054     uint32_t ip = reg_rd<uint32_t>(SIPR);
00055     snprintf(ip_string, sizeof(ip_string), "%d.%d.%d.%d", (ip>>24)&0xff,(ip>>16)&0xff,(ip>>8)&0xff,ip&0xff); 
00056     ip_set = true;
00057     return ip_string;
00058 }
00059 
00060 int WIZ820ioInterface::IPrenew(int timeout_ms)
00061 {
00062     printf("DHCP Started, waiting for IP...\n");  
00063     DHCPClient dhcp;
00064     int err = dhcp.setup(timeout_ms);
00065     if (err == (-1)) {
00066         printf("Timeout.\n");
00067         return -1;
00068     }
00069     printf("Connected, IP: %d.%d.%d.%d\n", dhcp.yiaddr[0], dhcp.yiaddr[1], dhcp.yiaddr[2], dhcp.yiaddr[3]);
00070     ip      = (dhcp.yiaddr[0]<<24) | (dhcp.yiaddr[1]<<16) | (dhcp.yiaddr[2]<<8) | dhcp.yiaddr[3];
00071     gateway = (dhcp.gateway[0]<<24) | (dhcp.gateway[1]<<16) | (dhcp.gateway[2]<<8) | dhcp.gateway[3];
00072     netmask = (dhcp.netmask[0]<<24) | (dhcp.netmask[1]<<16) | (dhcp.netmask[2]<<8) | dhcp.netmask[3];
00073     dnsaddr = (dhcp.dnsaddr[0]<<24) | (dhcp.dnsaddr[1]<<16) | (dhcp.dnsaddr[2]<<8) | dhcp.dnsaddr[3];
00074     return 0;
00075 }