W5200(WIZ820io) network interface
Revision 2:a8df39b4f3aa, committed 2012-04-19
- Comitter:
- va009039
- Date:
- Thu Apr 19 11:14:43 2012 +0000
- Parent:
- 1:803123933c5a
- Commit message:
Changed in this revision
diff -r 803123933c5a -r a8df39b4f3aa MyNetDnsRequest.cpp --- a/MyNetDnsRequest.cpp Tue Apr 17 12:13:15 2012 +0000 +++ b/MyNetDnsRequest.cpp Thu Apr 19 11:14:43 2012 +0000 @@ -1,10 +1,18 @@ -// MyNetDnsRequest.cpp 2012/4/16 +// MyNetDnsRequest.cpp 2012/4/19 #include "mbed.h" #include "MyNetDnsRequest.h" #include "UDPSocket.h" #include <string> #include "dnsname.h" -#include "w5200NetIf.h" +#include "W5200NetIf.h" +//#define __DEBUG +#include "dbg/dbg.h" + +#ifdef __DEBUG +#define DBG2(...) do{ DebugStream::debug("%p %d %s ", this,__LINE__,__PRETTY_FUNCTION__); DebugStream::debug(__VA_ARGS__); } while(0); +#else +#define DBG2(...) while(0); +#endif //__DEBUG //#define DEBUG @@ -36,10 +44,11 @@ void MyNetDnsRequest::callback(UDPSocketEvent e) { PRINT_FUNC(); + DBG2("m_id[]=%02x:%02x\n", m_id[0], m_id[1]); uint8_t buf[512]; Host host; int len = m_udp->recvfrom((char*)buf, sizeof(buf), &host); - if (memcmp(buf, m_id, 2) != 0) { //verify + if (memcmp(buf+0, m_id, 2) != 0) { //verify return; } int rcode = response(buf, len); @@ -125,7 +134,7 @@ IpAddr dns(8,8,8,8); NetIf* pIf = Net::getDefaultIf(); if (pIf) { - dns = ((w5200NetIf*)pIf)->m_dns; + dns = ((W5200NetIf*)pIf)->m_dns; } Host server(dns, 53); // DNS m_udp->bind(local); @@ -136,8 +145,8 @@ printHex(buf, size); #endif m_udp->sendto((char*)buf, size, &server); - m_timer.reset(); - m_timer.start(); + m_interval.reset(); + m_interval.start(); } void MyNetDnsRequest::poll() { @@ -148,6 +157,7 @@ #endif //DEBUG switch(m_state) { case MYNETDNS_START: + m_retry = 0; resolve(m_hostname); m_state = MYNETDNS_PROCESSING; break; @@ -160,12 +170,19 @@ onReply(NETDNS_ERROR); break; case MYNETDNS_OK: + DBG2("m_retry=%d, m_interval=%d\n", m_retry, m_interval.read_ms()); onReply(NETDNS_FOUND); break; } - if (m_timer.read_ms() > 3000) { - m_timer.stop(); - m_state = MYNETDNS_ERROR; + if (m_interval.read_ms() > 1000) { + m_interval.stop(); + DBG2("timeout m_retry=%d\n", m_retry); + if (++m_retry > 1) { + m_state = MYNETDNS_ERROR; + } else { + resolve(m_hostname); + m_state = MYNETDNS_PROCESSING; + } } if(m_closing && (m_state!=MYNETDNS_PROCESSING)) { NetDnsRequest::close();
diff -r 803123933c5a -r a8df39b4f3aa MyNetDnsRequest.h --- a/MyNetDnsRequest.h Tue Apr 17 12:13:15 2012 +0000 +++ b/MyNetDnsRequest.h Thu Apr 19 11:14:43 2012 +0000 @@ -1,4 +1,4 @@ -// MyNetDnsRequest.h 2012/4/13 +// MyNetDnsRequest.h 2012/4/19 #ifndef MYNETDNSREQUEST_H #define MYNETDNSREQUEST_H #include "if/net/netdnsrequest.h" @@ -17,7 +17,8 @@ int query(uint8_t buf[], int size, const char* hostname); void resolve(const char* hostname); uint8_t m_id[2]; - Timer m_timer; + Timer m_interval; + int m_retry; private: enum MyNetDnsState {
diff -r 803123933c5a -r a8df39b4f3aa MyNetTcpSocket.cpp --- a/MyNetTcpSocket.cpp Tue Apr 17 12:13:15 2012 +0000 +++ b/MyNetTcpSocket.cpp Thu Apr 19 11:14:43 2012 +0000 @@ -1,10 +1,16 @@ -// MyNetTcpSocket.cpp 2012/4/17 +// MyNetTcpSocket.cpp 2012/4/19 #include "mbed.h" #include "w5100.h" #include "MyNetTcpSocket.h" -#define __DEBUG +//#define __DEBUG #include "dbg/dbg.h" +#ifdef __DEBUG +#define DBG2(...) do{ DebugStream::debug("%p %s ", this,__PRETTY_FUNCTION__); DebugStream::debug(__VA_ARGS__); } while(0); +#else +#define DBG2(...) while(0); +#endif //__DEBUG + //#define DEBUG #ifdef DEBUG @@ -28,6 +34,7 @@ MyNetTcpSocket::MyNetTcpSocket(int socket) : NetTcpSocket(),_socket(socket),wait_accept(false) { PRINT_FUNC(); + DBG2("socket: %d\n", socket); if (_socket == (-1)) { _socket = w5200_new_socket(); } @@ -41,6 +48,7 @@ MyNetTcpSocket::~MyNetTcpSocket() { PRINT_FUNC(); + DBG2("socket=%d\n", _socket); close(); if (_socket != (-1)) { W5100.writeSnMR(_socket, SnMR::CLOSE); @@ -116,7 +124,12 @@ pClient->setPort(port); Host me; me.setPort(W5100.readSnPORT(_socket)); - *ppNewNetTcpSocket = new MyNetTcpSocket(_socket); + MyNetTcpSocket* pNewNetTcpSocket = new MyNetTcpSocket(_socket); + if (pNewNetTcpSocket == NULL) { + return NETTCPSOCKET_EMPTY; + } + pNewNetTcpSocket->m_refs++; + *ppNewNetTcpSocket = pNewNetTcpSocket; _socket = w5200_new_socket(); if (_socket != (-1)) { W5100.writeSnMR(_socket, SnMR::TCP); // set TCP mode @@ -163,6 +176,7 @@ NetTcpSocketErr MyNetTcpSocket::close() { PRINT_FUNC(); + DBG2("m_closed=%d m_refs=%d\n", m_closed, m_refs); if(m_closed) { return NETTCPSOCKET_OK; }
diff -r 803123933c5a -r a8df39b4f3aa W5200NetIf.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/W5200NetIf.cpp Thu Apr 19 11:14:43 2012 +0000 @@ -0,0 +1,65 @@ +// W5200NetIf.cpp 2012/4/13 +#include "W5200NetIf.h" +#include "DHCPClient.h" +#include "w5100.h" + +W5200NetIf:: W5200NetIf():MyNetIf(), m_netmask(255,255,255,255), m_gateway(), m_hostname(NULL) { + m_hostname = NULL; + m_useDhcp = true; +} + +W5200NetIf::W5200NetIf(IpAddr ip, IpAddr netmask, IpAddr gateway, IpAddr dns) :MyNetIf(), m_hostname(NULL) { + m_hostname = NULL; + m_ip = ip; + m_netmask = netmask; + m_gateway = gateway; + m_dns = dns; + m_useDhcp = false; +} + +W5200Err W5200NetIf::IPrenew(int timeout_ms) { + printf("DHCP Started, waiting for IP...\n"); + DHCPClient dhcp; + int err = dhcp.setup(timeout_ms); + if (err == (-1)) { + printf("Timeout.\n"); + return W5200_TIMEOUT; + } + printf("Connected, IP: %d.%d.%d.%d\n", dhcp.yiaddr[0], dhcp.yiaddr[1], dhcp.yiaddr[2], dhcp.yiaddr[3]); + m_ip = IpAddr(dhcp.yiaddr[0], dhcp.yiaddr[1], dhcp.yiaddr[2], dhcp.yiaddr[3]); + m_netmask = IpAddr(dhcp.netmask[0],dhcp.netmask[1],dhcp.netmask[2],dhcp.netmask[3]); + m_gateway = IpAddr(dhcp.gateway[0],dhcp.gateway[1],dhcp.gateway[2],dhcp.gateway[3]); + uint8_t t[4]; + t[0] = m_ip[0]; + t[1] = m_ip[1]; + t[2] = m_ip[2]; + t[3] = m_ip[3]; + W5100.writeSIPR(t); + t[0] = m_netmask[0]; + t[1] = m_netmask[1]; + t[2] = m_netmask[2]; + t[3] = m_netmask[3]; + W5100.writeSUBR(t); + t[0] = m_gateway[0]; + t[1] = m_gateway[1]; + t[2] = m_gateway[2]; + t[3] = m_gateway[3]; + W5100.writeGAR(t); + m_dns = IpAddr(dhcp.dnsaddr[0],dhcp.dnsaddr[1],dhcp.dnsaddr[2],dhcp.dnsaddr[3]); + return W5200_OK; +} + +W5200Err W5200NetIf::IPrelease(int timeout_ms) { + return W5200_OK; +} + +W5200Err W5200NetIf::setup(int timeout_ms) { + MyNetIf::init(); + uint8_t mac[6] = {0x00,0x00,0x5e,0x00,0x01,0x01}; + W5100.setMACAddress(mac); + printf("HW Addr is : %02x:%02x:%02x:%02x:%02x:%02x.\n", mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]); + if(! m_useDhcp) { + return W5200_OK; + } + return IPrenew(timeout_ms); +}
diff -r 803123933c5a -r a8df39b4f3aa W5200NetIf.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/W5200NetIf.h Thu Apr 19 11:14:43 2012 +0000 @@ -0,0 +1,43 @@ +// W5200NetIf.h 2012/4/19 +/** \file +W5200 network interface header file +*/ +#ifndef W5200_NETIF_H +#define W5200_NETIF_H +#include "MyNetIf.h" + +///W5200 network interface return codes +enum W5200Err +{ + __W5200_MIN = -0xFFFF, + W5200_TIMEOUT, ///<Timeout during setup + W5200_OK = 0 ///<Success +}; + +///W5200 network interface +class W5200NetIf : public MyNetIf { +public: + ///Instantiates the Interface and register it against the stack, DHCP will be used + W5200NetIf(); //W/ DHCP + ///Instantiates the Interface and register it against the stack, DHCP will not be used + /** + IpAddr is a container class that can be constructed with either 4 bytes or no parameters for a null IP address. + */ + W5200NetIf(IpAddr ip, IpAddr netmask, IpAddr gateway, IpAddr dns); //W/o DHCP + W5200Err IPrenew(int timeout_ms = 15000); + W5200Err IPrelease(int timeout_ms = 15000); + ///Brings the interface up + /** + Uses DHCP if necessary + @param timeout_ms : You can set the timeout parameter in milliseconds, if not it defaults to 15s + @return : W5200_OK on success or W5200_TIMEOUT on timeout + */ + W5200Err setup(int timeout_ms = 15000); + IpAddr m_dns; +private: + bool m_useDhcp; + IpAddr m_netmask; + IpAddr m_gateway; + const char* m_hostname; +}; +#endif //W5200_NETIF_H
diff -r 803123933c5a -r a8df39b4f3aa WIZ820ioNetIf.cpp --- a/WIZ820ioNetIf.cpp Tue Apr 17 12:13:15 2012 +0000 +++ b/WIZ820ioNetIf.cpp Thu Apr 19 11:14:43 2012 +0000 @@ -46,5 +46,5 @@ hardware_reset(); W5100.init(); wait_linkup(); - return w5200NetIf::setup(timeout_ms); + return W5200NetIf::setup(timeout_ms); }
diff -r 803123933c5a -r a8df39b4f3aa WIZ820ioNetIf.h --- a/WIZ820ioNetIf.h Tue Apr 17 12:13:15 2012 +0000 +++ b/WIZ820ioNetIf.h Thu Apr 19 11:14:43 2012 +0000 @@ -1,25 +1,24 @@ -// WIZ820ioNetIf.h 2012/4/15 -/* +// WIZ820ioNetIf.h 2012/4/19 -WIZ820io mbed -J2P1 GND ----- p1 GND -J1P3 MOSI ---- p11 SPI(mosi) -J2P6 MISO ---- p12 SPI(miso) -J1P4 SCK ----- p13 SPI(sck) -J1P5 nSS ----- p14 -J2P5 nRESET -- p15 -J2P3 3V3D ---- p40 VOUT -J2P4 PWDN ---- GND - +/** \file +WIZ820io network interface header file */ #ifndef WIZ820IO_NETIF_H #define WIZ820IO_NETIF_H -#include "w5200NetIf.h" -class WIZ820ioNetIf : public w5200NetIf { +#include "W5200NetIf.h" + +///WIZ820io network interface +class WIZ820ioNetIf : public W5200NetIf { public: void spi(PinName mosi,PinName miso, PinName sclk); void cs(PinName _cs); void reset(PinName _reset); + ///Brings the interface up + /** + Uses DHCP if necessary + @param timeout_ms : You can set the timeout parameter in milliseconds, if not it defaults to 15s + @return : 0 on success or -1 on timeout + */ int setup(int timeout_ms = 15000); }; #endif //WIZ820IO_NETIF_H
diff -r 803123933c5a -r a8df39b4f3aa w5200NetIf.cpp --- a/w5200NetIf.cpp Tue Apr 17 12:13:15 2012 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,65 +0,0 @@ -// w5200NetIf.cpp 2012/4/13 -#include "w5200NetIf.h" -#include "DHCPClient.h" -#include "w5100.h" - -w5200NetIf:: w5200NetIf():MyNetIf(), m_netmask(255,255,255,255), m_gateway(), m_hostname(NULL) { - m_hostname = NULL; - m_useDhcp = true; -} - -w5200NetIf::w5200NetIf(IpAddr ip, IpAddr netmask, IpAddr gateway, IpAddr dns) :MyNetIf(), m_hostname(NULL) { - m_hostname = NULL; - m_ip = ip; - m_netmask = netmask; - m_gateway = gateway; - m_dns = dns; - m_useDhcp = false; -} - -int w5200NetIf::IPrenew(int timeout_ms) { - printf("DHCP Started, waiting for IP...\n"); - DHCPClient dhcp; - int err = dhcp.setup(timeout_ms); - if (err == (-1)) { - printf("Timeout.\n"); - return err; - } - printf("Connected, IP: %d.%d.%d.%d\n", dhcp.yiaddr[0], dhcp.yiaddr[1], dhcp.yiaddr[2], dhcp.yiaddr[3]); - m_ip = IpAddr(dhcp.yiaddr[0], dhcp.yiaddr[1], dhcp.yiaddr[2], dhcp.yiaddr[3]); - m_netmask = IpAddr(dhcp.netmask[0],dhcp.netmask[1],dhcp.netmask[2],dhcp.netmask[3]); - m_gateway = IpAddr(dhcp.gateway[0],dhcp.gateway[1],dhcp.gateway[2],dhcp.gateway[3]); - uint8_t t[4]; - t[0] = m_ip[0]; - t[1] = m_ip[1]; - t[2] = m_ip[2]; - t[3] = m_ip[3]; - W5100.writeSIPR(t); - t[0] = m_netmask[0]; - t[1] = m_netmask[1]; - t[2] = m_netmask[2]; - t[3] = m_netmask[3]; - W5100.writeSUBR(t); - t[0] = m_gateway[0]; - t[1] = m_gateway[1]; - t[2] = m_gateway[2]; - t[3] = m_gateway[3]; - W5100.writeGAR(t); - m_dns = IpAddr(dhcp.dnsaddr[0],dhcp.dnsaddr[1],dhcp.dnsaddr[2],dhcp.dnsaddr[3]); - return err; -} - -int w5200NetIf::IPrelease(int timeout_ms) { - return -1; -} - -int w5200NetIf::setup(int timeout_ms) { - MyNetIf::init(); - uint8_t mac[6] = {0x00,0x00,0x5e,0x00,0x01,0x01}; - W5100.setMACAddress(mac); - printf("HW Addr is : %02x:%02x:%02x:%02x:%02x:%02x.\n", mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]); - if(! m_useDhcp) { - return 0; - } - return IPrenew(timeout_ms); -}
diff -r 803123933c5a -r a8df39b4f3aa w5200NetIf.h --- a/w5200NetIf.h Tue Apr 17 12:13:15 2012 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,20 +0,0 @@ -// w5200NetIf.h 2012/4/13 -#ifndef W5200_NETIF_H -#define W5200_NETIF_H -#include "MyNetIf.h" - -class w5200NetIf : public MyNetIf { -public: - w5200NetIf(); - w5200NetIf(IpAddr ip, IpAddr netmask, IpAddr gateway, IpAddr dns); - int IPrenew(int timeout_ms = 15000); - int IPrelease(int timeout_ms = 15000); - int setup(int timeout_ms = 15000); - IpAddr m_dns; -private: - bool m_useDhcp; - IpAddr m_netmask; - IpAddr m_gateway; - const char* m_hostname; -}; -#endif //W5200_NETIF_H