WIZ820io(W5200) network interface, EthernetInterface compatible.

Dependents:   Seeed_Ethernet_Shield_V2_HelloWorld Seeed_Ethernet_Shield Cayenne-WIZ820ioInterface Seeed_Ethernet_Shield

Fork of WiflyInterface by mbed official

WIZ820io

Files at this revision

API Documentation at this revision

Comitter:
va009039
Date:
Tue Aug 27 12:50:11 2013 +0000
Parent:
4:0bcec6272784
Commit message:
WIZ820ioInterface?first commit

Changed in this revision

DHCPClient/DHCPClient.cpp Show annotated file Show diff for this revision Revisions of this file
DHCPClient/DHCPClient.h Show annotated file Show diff for this revision Revisions of this file
DNSClient/DNSClient.cpp Show annotated file Show diff for this revision Revisions of this file
DNSClient/DNSClient.h Show annotated file Show diff for this revision Revisions of this file
DNSClient/dnsname.h Show annotated file Show diff for this revision Revisions of this file
Helper/def.h Show diff for this revision Revisions of this file
Socket/Endpoint.cpp Show annotated file Show diff for this revision Revisions of this file
Socket/Endpoint.h Show annotated file Show diff for this revision Revisions of this file
Socket/Socket.cpp Show annotated file Show diff for this revision Revisions of this file
Socket/Socket.h Show annotated file Show diff for this revision Revisions of this file
Socket/TCPSocketConnection.cpp Show annotated file Show diff for this revision Revisions of this file
Socket/TCPSocketConnection.h Show annotated file Show diff for this revision Revisions of this file
Socket/TCPSocketServer.cpp Show annotated file Show diff for this revision Revisions of this file
Socket/UDPSocket.cpp Show annotated file Show diff for this revision Revisions of this file
Socket/UDPSocket.h Show annotated file Show diff for this revision Revisions of this file
WIZ820io/WIZ820io.cpp Show annotated file Show diff for this revision Revisions of this file
WIZ820io/WIZ820io.h Show annotated file Show diff for this revision Revisions of this file
WIZ820ioInterface.cpp Show annotated file Show diff for this revision Revisions of this file
WIZ820ioInterface.h Show annotated file Show diff for this revision Revisions of this file
Wifly/CBuffer.h Show diff for this revision Revisions of this file
Wifly/Wifly.cpp Show diff for this revision Revisions of this file
Wifly/Wifly.h Show diff for this revision Revisions of this file
WiflyInterface.cpp Show diff for this revision Revisions of this file
WiflyInterface.h Show diff for this revision Revisions of this file
pico_string.h Show annotated file Show diff for this revision Revisions of this file
diff -r 0bcec6272784 -r fb15c35d1e28 DHCPClient/DHCPClient.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DHCPClient/DHCPClient.cpp	Tue Aug 27 12:50:11 2013 +0000
@@ -0,0 +1,211 @@
+// DHCPClient.cpp 2013/4/10
+#include "mbed.h"
+#include "mbed_debug.h"
+#include "UDPSocket.h"
+#include "DHCPClient.h"
+
+#define DBG_DHCP 0
+
+#if DBG_DHCP
+#define DBG(...) do{debug("[%s:%d]", __PRETTY_FUNCTION__,__LINE__);debug(__VA_ARGS__);} while(0);
+#define DBG_HEX(A,B) do{debug("[%s:%d]\r\n", __PRETTY_FUNCTION__,__LINE__);debug_hex(A,B);} while(0);
+#else
+#define DBG(...) while(0);
+#define DBG_HEX(A,B) while(0);
+#endif
+
+int DHCPClient::discover()
+{
+    m_pos = 0;
+    const uint8_t header[] = {0x01,0x01,0x06,0x00};
+    add_buf((uint8_t*)header, sizeof(header));
+    uint32_t x = time(NULL) + rand();
+    xid[0] = x>>24; xid[1] = x>>16; xid[2] = x>>8; xid[3] = x;
+    add_buf(xid, 4);
+    fill_buf(20, 0x00);
+    add_buf(chaddr, 6);
+    fill_buf(10+192, 0x00);
+    const uint8_t options[] = {0x63,0x82,0x53,0x63, // magic cookie
+                               53,1,DHCPDISCOVER,   // DHCP option 53: DHCP Discover
+                               55,4,1,3,15,6,
+                               255}; 
+    add_buf((uint8_t*)options, sizeof(options));
+    return m_pos;
+}
+
+int DHCPClient::request()
+{
+    m_pos = 0;
+    const uint8_t header[] = {0x01,0x01,0x06,0x00};
+    add_buf((uint8_t*)header, sizeof(header));
+    add_buf(xid, 4);
+    fill_buf(12, 0x00);
+    add_buf(siaddr, 4);
+    fill_buf(4, 0x00); // giaddr
+    add_buf(chaddr, 6);
+    fill_buf(10+192, 0x00);
+    const uint8_t options[] = {0x63,0x82,0x53,0x63, // magic cookie
+                               53,1,DHCPREQUEST,    // DHCP option 53: DHCP Request
+                               55,4,1,3,15,6,       // DHCP option 55:
+                               };
+    add_buf((uint8_t*)options, sizeof(options));
+    add_option(50, yiaddr, 4);
+    add_option(54, siaddr, 4);
+    add_option(255);
+    return m_pos;
+}
+
+int DHCPClient::offer(uint8_t buf[], int size) {
+    memcpy(yiaddr, buf+DHCP_OFFSET_YIADDR, 4);   
+    memcpy(siaddr, buf+DHCP_OFFSET_SIADDR, 4);   
+    uint8_t *p;
+    int msg_type = -1;
+    p = buf + DHCP_OFFSET_OPTIONS;
+    while(*p != 255 && p < (buf+size)) {
+        uint8_t code = *p++;
+        if (code == 0) { // Pad Option
+            continue;
+        }
+        int len = *p++;
+ 
+        DBG("DHCP option: %d\r\n", code);
+        DBG_HEX(p, len);
+
+        switch(code) {
+            case 53:
+                msg_type = *p;
+                break;
+            case 1:
+                memcpy(netmask, p, 4); // Subnet mask address
+                break;
+            case 3:
+                memcpy(gateway, p, 4); // Gateway IP address
+                break; 
+            case 6:  // DNS server
+                memcpy(dnsaddr, p, 4);
+                break;
+            case 51: // IP lease time 
+                break;
+            case 54: // DHCP server
+                memcpy(siaddr, p, 4);
+                break;
+        }
+        p += len;
+    }
+    return msg_type;
+}
+
+bool DHCPClient::verify(uint8_t buf[], int len) {
+    if (len < DHCP_OFFSET_OPTIONS) {
+        return false;
+    }
+    if (buf[DHCP_OFFSET_OP] != 0x02) {
+        return false;
+    }
+    if (memcmp(buf+DHCP_OFFSET_XID, xid, 4) != 0) {
+        return false;
+    }
+    return true;
+}
+
+void DHCPClient::callback()
+{
+    Endpoint host;
+    int recv_len = m_udp->receiveFrom(host, (char*)m_buf, sizeof(m_buf));
+    if (recv_len < 0) {
+        return;
+    }
+    if (!verify(m_buf, recv_len)) {
+        return;
+    }
+    int r = offer(m_buf, recv_len);
+    if (r == DHCPOFFER) {
+        int send_size = request();
+        m_udp->sendTo(m_server, (char*)m_buf, send_size);
+    } else if (r == DHCPACK) {
+        exit_flag = true;
+    }
+}
+
+void  DHCPClient::add_buf(uint8_t c)
+{
+    m_buf[m_pos++] = c;
+}
+
+void  DHCPClient::add_buf(uint8_t* buf, int len)
+{
+    for(int i = 0; i < len; i++) {
+        add_buf(buf[i]);
+    }
+}
+
+void DHCPClient::fill_buf(int len, uint8_t data)
+{
+    while(len-- > 0) {
+        add_buf(data);
+    }
+}
+
+void  DHCPClient::add_option(uint8_t code, uint8_t* buf, int len)
+{
+    add_buf(code);
+    if (len > 0) {
+        add_buf((uint8_t)len);
+        add_buf(buf, len);
+    }
+}
+
+int DHCPClient::setup(int timeout_ms)
+{
+    eth = WIZ820io::getInstance();
+    if (eth == NULL) {
+        return -1;
+    }    
+    eth->reg_rd_mac(SHAR, chaddr);
+    int interval_ms = 5*1000; // 5000msec
+    if (timeout_ms < interval_ms) {
+        interval_ms = timeout_ms;
+    }
+    m_udp = new UDPSocket;
+    m_udp->init();
+    m_udp->set_blocking(false);
+    eth->reg_wr<uint32_t>(SIPR, 0x00000000); // local ip "0.0.0.0"
+    m_udp->bind(68); // local port
+    m_server.set_address("255.255.255.255", 67); // DHCP broadcast
+    exit_flag = false;
+    int err = 0;
+    int seq = 0;
+    int send_size;
+    while(!exit_flag) {
+        switch(seq) {
+            case 0:
+                m_retry = 0;
+                seq++;
+                break;
+            case 1:
+                send_size = discover();
+                m_udp->sendTo(m_server, (char*)m_buf, send_size);
+                m_interval.reset();
+                m_interval.start();
+                seq++;
+                break;
+            case 2:
+                callback();
+                if (m_interval.read_ms() > interval_ms) {
+                    DBG("m_retry: %d\n", m_retry);
+                    if (++m_retry >= (timeout_ms/interval_ms)) {
+                        err = -1;
+                        exit_flag = true;
+                    }
+                    seq--;
+                }
+                break;
+        }
+    }
+    DBG("m_retry: %d, m_interval: %d\n", m_retry, m_interval.read_ms());
+    delete m_udp;
+    return err;
+}
+
+DHCPClient::DHCPClient() {
+}
diff -r 0bcec6272784 -r fb15c35d1e28 DHCPClient/DHCPClient.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DHCPClient/DHCPClient.h	Tue Aug 27 12:50:11 2013 +0000
@@ -0,0 +1,54 @@
+// DHCPClient.h 2013/4/10
+#ifndef DHCPCLIENT_H
+#define DHCPCLIENT_H
+#include "WIZ820io.h"
+#include "UDPSocket.h"
+
+#define DHCP_OFFSET_OP 0
+#define DHCP_OFFSET_XID 4
+#define DHCP_OFFSET_YIADDR 16
+#define DHCP_OFFSET_SIADDR 20
+#define DHCP_OFFSET_OPTIONS 240
+#define DHCP_MAX_PACKET_SIZE 600
+
+// DHCP Message Type
+#define DHCPDISCOVER 1
+#define DHCPOFFER    2
+#define DHCPREQUEST  3
+#define DHCPDECLINE  4
+#define DHCPACK      5
+#define DHCPNAK      6
+#define DHCPRELEASE  7
+#define DHCPINFORM   8
+
+class DHCPClient {
+public:
+    DHCPClient();
+    int setup(int timeout_ms = 15*1000);
+    uint8_t chaddr[6]; // MAC
+    uint8_t yiaddr[4]; // IP
+    uint8_t dnsaddr[4]; // DNS
+    uint8_t gateway[4];
+    uint8_t netmask[4];
+    uint8_t siaddr[4];
+private:
+    int discover();
+    int request();
+    int offer(uint8_t buf[], int size);
+    void add_buf(uint8_t* buf, int len);
+    void fill_buf(int len, uint8_t data = 0x00);
+    void add_buf(uint8_t c);
+    void add_option(uint8_t code, uint8_t* buf = NULL, int len = 0);
+    bool verify(uint8_t buf[], int len);
+    void callback();
+    UDPSocket* m_udp;
+    Endpoint m_server;
+    uint8_t xid[4];
+    bool exit_flag;
+    Timer m_interval;
+    int m_retry;
+    uint8_t m_buf[DHCP_MAX_PACKET_SIZE];
+    int m_pos;
+    WIZ820io* eth;
+};
+#endif //DHCPCLIENT_H
diff -r 0bcec6272784 -r fb15c35d1e28 DNSClient/DNSClient.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DNSClient/DNSClient.cpp	Tue Aug 27 12:50:11 2013 +0000
@@ -0,0 +1,173 @@
+// DNSClient.cpp 2013/8/27
+#include "mbed.h"
+#include "mbed_debug.h"
+#include "DNSClient.h"
+#include "UDPSocket.h"
+#include "dnsname.h"
+#include "WIZ820io.h"
+
+#define DBG_DNS 0
+
+#if DBG_DNS
+#define DBG2(...) do{debug("[DNS]%p %d %s ", this,__LINE__,__PRETTY_FUNCTION__); debug(__VA_ARGS__); } while(0);
+#else
+#define DBG2(...) while(0);
+#endif
+
+DNSClient::DNSClient(const char* hostname) : m_state(MYNETDNS_START), m_udp(NULL) {
+    m_hostname = hostname;
+}
+
+DNSClient::DNSClient(Endpoint* pHost) : m_state(MYNETDNS_START), m_udp(NULL) {
+}
+
+DNSClient::~DNSClient() {
+    if (m_udp) {
+        delete m_udp;
+    }
+}
+
+void DNSClient::callback()
+{
+    uint8_t buf[512];
+    Endpoint host;
+    int len = m_udp->receiveFrom(host, (char*)buf, sizeof(buf));
+    if (len < 0) {
+        return;
+    }
+    if (memcmp(buf+0, m_id, 2) != 0) { //verify
+        return;
+    }
+    int rcode = response(buf, len);
+    if (rcode == 0) {
+        m_state = MYNETDNS_OK;
+    } else {
+        m_state = MYNETDNS_NOTFOUND;
+    }
+}
+
+int DNSClient::response(uint8_t buf[], int size) {
+    int rcode = buf[3] & 0x0f;
+    if (rcode != 0) {
+        return rcode;
+    }
+    int qdcount = buf[4]<<8|buf[5];
+    int ancount = buf[6]<<8|buf[7];
+    int pos = 12;
+    while(qdcount-- > 0) {
+        dnsname qname(buf);
+        pos = qname.decode(pos); // qname
+        pos += 4; // qtype qclass
+    }
+    while(ancount-- > 0) {
+        dnsname name(buf);
+        pos = name.decode(pos); // name
+        int type = buf[pos]<<8|buf[pos+1];
+        pos += 8; // type class TTL  
+        int rdlength = buf[pos]<<8|buf[pos+1]; pos += 2;
+        int rdata_pos = pos;
+        pos += rdlength;
+        if (type == 1) { // A record
+            ip = (buf[rdata_pos]<<24) | (buf[rdata_pos+1]<<16) | (buf[rdata_pos+2]<<8) | buf[rdata_pos+3];
+        }
+#if DBG_DNS
+        printf("%s", name.str.c_str());
+        if (type == 1) {
+            printf(" A %d.%d.%d.%d\n", 
+                buf[rdata_pos],buf[rdata_pos+1],buf[rdata_pos+2],buf[rdata_pos+3]);
+        } else if (type == 5) {
+            dnsname rdname(buf);
+            rdname.decode(rdata_pos);
+            printf(" CNAME %s\n", rdname.str.c_str());
+        } else {
+            printf(" TYPE:%d", type);
+            printfBytes(" RDATA:", &buf[rdata_pos], rdlength);
+        }
+#endif
+    }
+    return rcode;
+}
+
+int DNSClient::query(uint8_t buf[], int size, const char* hostname) {
+    const uint8_t header[] = {
+        0x00,0x00,0x01,0x00, // id=0x0000 QR=0 rd=1 opcode=0 rcode=0
+        0x00,0x01,0x00,0x00, // qdcount=1 ancount=0
+        0x00,0x00,0x00,0x00};// nscount=0 arcount=0 
+    const uint8_t tail[] = {0x00,0x01,0x00,0x01}; // qtype=A qclass=IN
+    memcpy(buf, header, sizeof(header));
+    int t = rand();
+    m_id[0] = t>>8;
+    m_id[1] = t;
+    memcpy(buf, m_id, 2); 
+    dnsname qname(buf);
+    int pos = qname.encode(sizeof(header), (char*)hostname);
+    memcpy(buf+pos, tail, sizeof(tail));
+    pos += sizeof(tail);
+    return pos;
+}
+
+void DNSClient::resolve(const char* hostname) {
+    if (m_udp == NULL) {
+        m_udp = new UDPSocket;
+    }
+    m_udp->init();
+    m_udp->set_blocking(false);
+    Endpoint server;
+    server.set_address("8.8.8.8", 53); // DNS
+    m_udp->bind(rand()&0x7fff);
+    uint8_t buf[256];                
+    int size = query(buf, sizeof(buf), hostname);
+#if DBG_DNS
+    printf("hostname:[%s]\n", hostname);
+    printHex(buf, size);
+#endif
+    m_udp->sendTo(server, (char*)buf, size);
+    m_interval.reset();
+    m_interval.start();
+}
+
+void DNSClient::poll() {
+#if DBG_DNS
+    printf("%p m_state: %d, m_udp: %p\n", this, m_state, m_udp);
+    wait_ms(400);
+#endif
+    switch(m_state) {
+        case MYNETDNS_START:
+            m_retry = 0;
+            resolve(m_hostname);
+            m_state = MYNETDNS_PROCESSING;
+            break;
+        case MYNETDNS_PROCESSING: 
+            break;
+        case MYNETDNS_NOTFOUND: 
+            break;
+        case MYNETDNS_ERROR: 
+            break;
+        case MYNETDNS_OK:
+            DBG2("m_retry=%d, m_interval=%d\n", m_retry, m_interval.read_ms());
+            break;
+    }
+    if (m_interval.read_ms() > 1000) {
+        m_interval.stop();
+        DBG2("timeout m_retry=%d\n", m_retry);
+        if (++m_retry >= 2) {
+            m_state = MYNETDNS_ERROR;
+        } else {
+            resolve(m_hostname);
+            m_state = MYNETDNS_PROCESSING;
+        }
+    }
+}
+
+bool DNSClient::lookup(const char* hostname) {
+    m_hostname = hostname;
+    m_state = MYNETDNS_START;
+    while(1) {
+        poll();
+        callback();
+        if (m_state != MYNETDNS_PROCESSING) {
+            break;
+        } 
+    }
+    return m_state == MYNETDNS_OK;
+}
diff -r 0bcec6272784 -r fb15c35d1e28 DNSClient/DNSClient.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DNSClient/DNSClient.h	Tue Aug 27 12:50:11 2013 +0000
@@ -0,0 +1,34 @@
+// DNSClient.h 2013/4/5
+#pragma once
+
+#include "UDPSocket.h"
+ 
+class DNSClient {
+public:
+    DNSClient(const char* hostname = NULL);
+    DNSClient(Endpoint* pHost);
+    virtual ~DNSClient();
+    bool lookup(const char* hostname = NULL);
+    uint32_t ip;
+protected:
+    void poll();
+    void callback();
+    int response(uint8_t buf[], int size);
+    int query(uint8_t buf[], int size, const char* hostname);
+    void resolve(const char* hostname);
+    uint8_t m_id[2];
+    Timer m_interval;
+    int m_retry;
+    const char* m_hostname;
+private:
+    enum MyNetDnsState
+    {
+        MYNETDNS_START,
+        MYNETDNS_PROCESSING, //Req has not completed
+        MYNETDNS_NOTFOUND,
+        MYNETDNS_ERROR,
+        MYNETDNS_OK
+    };
+    MyNetDnsState m_state;
+    UDPSocket *m_udp;
+};
diff -r 0bcec6272784 -r fb15c35d1e28 DNSClient/dnsname.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DNSClient/dnsname.h	Tue Aug 27 12:50:11 2013 +0000
@@ -0,0 +1,51 @@
+// dnsname.h 2013/8/27
+#pragma once
+//#include <string>
+#include "pico_string.h"
+class dnsname {
+public:
+    uint8_t *buf;
+    pico_string str;
+    dnsname(uint8_t *s) {
+        buf = s;
+    }
+    int decode(int pos) {
+        while(1) {
+            int len = buf[pos++];
+            if (len == 0x00) {
+                break;
+            }
+            if ((len&0xc0) == 0xc0) { //compress
+                int offset = (len&0x3f)<<8|buf[pos];
+                decode(offset);
+                return pos+1;
+            }
+            if (!str.empty()) {
+                str.append(".");
+            }
+            str.append((const char*)(buf+pos), len);
+            pos += len;
+        }
+        return pos;
+    }
+
+    int encode(int pos, char* s) {
+        while(*s) {  
+            char *f = strchr(s, '.');
+            if (f == NULL) {
+                int len = strlen(s);
+                buf[pos++] = len;
+                memcpy(buf+pos, s, len);
+                pos += len;
+                break;
+            }
+            int len = f - s;
+            buf[pos++] = len;
+            memcpy(buf+pos, s, len);
+            s = f+1;
+            pos += len;
+        }
+        buf[pos++] = 0x00;
+        return pos;
+    }
+};
diff -r 0bcec6272784 -r fb15c35d1e28 Helper/def.h
--- a/Helper/def.h	Thu Dec 20 15:08:58 2012 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,28 +0,0 @@
-/* Copyright (C) 2012 mbed.org, MIT License
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
- * and associated documentation files (the "Software"), to deal in the Software without restriction,
- * including without limitation the rights to use, copy, modify, merge, publish, distribute,
- * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all copies or
- * substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
- * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
- * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- */
- 
-#ifndef DEF_H
-#define DEF_H
-
-#include "cmsis.h"
-#define htons(x)      __REV16(x)
-#define ntohs(x)      __REV16(x)
-#define htonl(x)      __REV(x)
-#define ntohl(x)      __REV(x)
-
-#endif
\ No newline at end of file
diff -r 0bcec6272784 -r fb15c35d1e28 Socket/Endpoint.cpp
--- a/Socket/Endpoint.cpp	Thu Dec 20 15:08:58 2012 +0000
+++ b/Socket/Endpoint.cpp	Tue Aug 27 12:50:11 2013 +0000
@@ -17,14 +17,8 @@
  */
 #include "Socket/Socket.h"
 #include "Socket/Endpoint.h"
-#include <cstring>
-
-using std::memset;
 
 Endpoint::Endpoint()  {
-    wifly = Wifly::getInstance();
-    if (wifly == NULL)
-        error("Endpoint constructor error: no wifly instance available!\r\n");
     reset_address();
 }
 Endpoint::~Endpoint() {}
@@ -36,7 +30,15 @@
 
 int Endpoint::set_address(const char* host, const int port) {
     //Resolve DNS address or populate hard-coded IP address
-    wifly->gethostbyname(host, _ipAddress);
+    WIZ820io* eth = WIZ820io::getInstance();
+    if (eth == NULL) {
+        return -1;
+    }
+    uint32_t addr;
+    if (!eth->gethostbyname(host, &addr)) {
+        return -1;
+    }
+    snprintf(_ipAddress, sizeof(_ipAddress), "%d.%d.%d.%d", (addr>>24)&0xff, (addr>>16)&0xff, (addr>>8)&0xff, addr&0xff);
     _port = port;
     return 0;
 }
diff -r 0bcec6272784 -r fb15c35d1e28 Socket/Endpoint.h
--- a/Socket/Endpoint.h	Thu Dec 20 15:08:58 2012 +0000
+++ b/Socket/Endpoint.h	Tue Aug 27 12:50:11 2013 +0000
@@ -18,7 +18,7 @@
 #ifndef ENDPOINT_H
 #define ENDPOINT_H
 
-#include "Wifly.h"
+#include "WIZ820io.h"
 
 class UDPSocket;
 
@@ -59,7 +59,6 @@
 protected:
     char _ipAddress[16];
     int _port;
-    Wifly * wifly;
 };
 
 #endif
diff -r 0bcec6272784 -r fb15c35d1e28 Socket/Socket.cpp
--- a/Socket/Socket.cpp	Thu Dec 20 15:08:58 2012 +0000
+++ b/Socket/Socket.cpp	Tue Aug 27 12:50:11 2013 +0000
@@ -17,12 +17,12 @@
  */
  
 #include "Socket.h"
-#include <cstring>
 
-Socket::Socket() : _blocking(true), _timeout(1500) {
-    wifi = Wifly::getInstance();
-    if (wifi == NULL)
-        error("Socket constructor error: no wifly instance available!\r\n");
+Socket::Socket() : _sock_fd(-1),_blocking(true), _timeout(1500) {
+    eth = WIZ820io::getInstance();
+    if (eth == NULL) {
+        error("Socket constructor error: no WIZ8820io instance available!\r\n");
+    }       
 }
 
 void Socket::set_blocking(bool blocking, unsigned int timeout) {
@@ -31,7 +31,7 @@
 }
 
 int Socket::close() {
-    return (wifi->close()) ? 0 : -1;
+    return (eth->close(_sock_fd)) ? 0 : -1;
 }
 
 Socket::~Socket() {
diff -r 0bcec6272784 -r fb15c35d1e28 Socket/Socket.h
--- a/Socket/Socket.h	Thu Dec 20 15:08:58 2012 +0000
+++ b/Socket/Socket.h	Tue Aug 27 12:50:11 2013 +0000
@@ -18,7 +18,7 @@
 #ifndef SOCKET_H_
 #define SOCKET_H_
 
-#include "Wifly.h"
+#include "WIZ820io.h"
 
 /** Socket file descriptor and select wrapper
   */
@@ -42,9 +42,10 @@
     ~Socket();
     
 protected:
+    int _sock_fd;
     bool _blocking;
     int _timeout;
-    Wifly * wifi;
+    WIZ820io* eth;
 };
 
 
diff -r 0bcec6272784 -r fb15c35d1e28 Socket/TCPSocketConnection.cpp
--- a/Socket/TCPSocketConnection.cpp	Thu Dec 20 15:08:58 2012 +0000
+++ b/Socket/TCPSocketConnection.cpp	Tue Aug 27 12:50:11 2013 +0000
@@ -17,109 +17,96 @@
  */
 
 #include "TCPSocketConnection.h"
-#include <algorithm>
 
-TCPSocketConnection::TCPSocketConnection() {}
+TCPSocketConnection::TCPSocketConnection()
+{
+}
 
 int TCPSocketConnection::connect(const char* host, const int port)
 {  
-    if (!wifi->connect(host, port))
+    if (_sock_fd < 0) {
+        _sock_fd = eth->new_socket();
+        if (_sock_fd < 0) {
+            return -1;
+        }
+    }
+    if (set_address(host, port) != 0) {
         return -1;
-    wifi->flush();
+    }    
+    if (!eth->connect(_sock_fd, get_address(), port)) {
+        return -1;
+    }
     return 0;
 }
 
 bool TCPSocketConnection::is_connected(void)
 {
-    return wifi->is_connected();
+    return eth->is_connected(_sock_fd);
 }
 
 int TCPSocketConnection::send(char* data, int length)
 {
-    Timer tmr;
-
-    if (!_blocking) {
-        tmr.start();
-        while (tmr.read_ms() < _timeout) {
-            if (wifi->writeable())
-                break;
-        }
-        if (tmr.read_ms() >= _timeout) {
-            return -1;
-        }
+    int size = eth->wait_writeable(_sock_fd, _blocking ? -1 : _timeout);
+    if (size < 0) {
+        return -1;
     }
-    return wifi->send(data, length);
+    if (size > length) {
+        size = length;
+    }
+    return eth->send(_sock_fd, data, size);
 }
 
 // -1 if unsuccessful, else number of bytes written
 int TCPSocketConnection::send_all(char* data, int length)
 {
-    Timer tmr;
-    int idx = 0;
-    tmr.start();
-
-    while ((tmr.read_ms() < _timeout) || _blocking) {
-
-        idx += wifi->send(data, length);
-
-        if (idx == length)
-            return idx;
+    int writtenLen = 0;
+    while (writtenLen < length) {
+        int size = eth->wait_writeable(_sock_fd, _blocking ? -1 : _timeout);
+        if (size < 0) {
+            return -1;
+        }
+        if (size > (length-writtenLen)) {
+            size = (length-writtenLen);
+        }
+        int ret = eth->send(_sock_fd, data + writtenLen, size);
+        if (ret < 0) {
+            return -1;
+        }
+        writtenLen += ret;
     }
-    return (idx == 0) ? -1 : idx;
+    return writtenLen;
 }
 
 // -1 if unsuccessful, else number of bytes received
 int TCPSocketConnection::receive(char* data, int length)
 {
-    Timer tmr;
-    int time = -1;
-    
-    
-    if (!_blocking) {
-        tmr.start();
-        while (time < _timeout + 20) {
-            if (wifi->readable()) {
-                break;
-            }
-            time = tmr.read_ms();
-        }
-        if (time >= _timeout + 20) {
-            return -1;
-        }
+    int size = eth->wait_readable(_sock_fd, _blocking ? -1 : _timeout);
+    if (size < 0) {
+        return -1;
     }
-
-
-    while(!wifi->readable());
-    int nb_available = wifi->readable();
-    for (int i = 0; i < min(nb_available, length); i++) {
-        data[i] = wifi->getc();
+    if (size > length) {
+        size = length;
     }
-
-    return min(nb_available, length);
+    return eth->recv(_sock_fd, data, size);
 }
 
-
 // -1 if unsuccessful, else number of bytes received
 int TCPSocketConnection::receive_all(char* data, int length)
 {
-    Timer tmr;
-    int idx = 0;
-    int time = -1;
-
-    tmr.start();
-    
-    while (time < _timeout || _blocking) {
-
-        int nb_available = wifi->readable();
-        for (int i = 0; i < min(nb_available, length); i++) {
-            data[idx++] = wifi->getc();
+    int readLen = 0;
+    while (readLen < length) {
+        int size = eth->wait_readable(_sock_fd, _blocking ? -1 :_timeout);
+        if (size <= 0) {
+            break;
+        }
+        if (size > (length - readLen)) {
+            size = length - readLen;
         }
-
-        if (idx == length)
-            break;
-
-        time = tmr.read_ms();
+        int ret = eth->recv(_sock_fd, data + readLen, size);
+        if (ret < 0) {
+            return -1;
+        }
+        readLen += ret;
     }
-
-    return (idx == 0) ? -1 : idx;
+    return readLen;
 }
diff -r 0bcec6272784 -r fb15c35d1e28 Socket/TCPSocketConnection.h
--- a/Socket/TCPSocketConnection.h	Thu Dec 20 15:08:58 2012 +0000
+++ b/Socket/TCPSocketConnection.h	Tue Aug 27 12:50:11 2013 +0000
@@ -26,6 +26,7 @@
 TCP socket connection
 */
 class TCPSocketConnection: public Socket, public Endpoint {
+    friend class TCPSocketServer;
     
 public:
     /** TCP socket connection
diff -r 0bcec6272784 -r fb15c35d1e28 Socket/TCPSocketServer.cpp
--- a/Socket/TCPSocketServer.cpp	Thu Dec 20 15:08:58 2012 +0000
+++ b/Socket/TCPSocketServer.cpp	Tue Aug 27 12:50:11 2013 +0000
@@ -17,74 +17,58 @@
  */
 
 #include "TCPSocketServer.h"
-#include <string>
 
 TCPSocketServer::TCPSocketServer() {}
 
 // Server initialization
 int TCPSocketServer::bind(int port) {
-    char cmd[20];
-    
+    if (_sock_fd < 0) {
+        _sock_fd = eth->new_socket();
+        if (_sock_fd < 0) {
+            return -1;
+        }
+    }
     // set TCP protocol
-    wifi->setProtocol(TCP);
-    
+    eth->setProtocol(_sock_fd, TCP);
     // set local port
-    sprintf(cmd, "set i l %d\r", port);
-    if (!wifi->sendCommand(cmd, "AOK"))
-        return -1;
-    
-    // save
-    if (!wifi->sendCommand("save\r", "Stor"))
-        return -1;
-    
-    // reboot
-    wifi->reboot();
-    
+    eth->sreg<uint16_t>(_sock_fd, Sn_PORT, port);
     // connect the network
-    if (wifi->isDHCP()) {
-        if (!wifi->sendCommand("join\r", "DHCP=ON", NULL, 10000))
-            return -1;
-    } else {
-        if (!wifi->sendCommand("join\r", "Associated", NULL, 10000))
-            return -1;
-    }
-        
-    // exit
-    wifi->exit();
-    
-    wait(0.2);
-    wifi->flush();
+    eth->scmd(_sock_fd, OPEN);
     return 0;
 }
 
 int TCPSocketServer::listen(int backlog) {
-    if (backlog != 1)
+    if (_sock_fd < 0) {
         return -1;
+    }
+    if (backlog != 1) {
+        return -1;
+    }
+    eth->scmd(_sock_fd, LISTEN);
     return 0;
 }
 
 
 int TCPSocketServer::accept(TCPSocketConnection& connection) {
-    int nb_available = 0, pos = 0;
-    char c;
-    string str;
-    bool o_find = false;
-    while (1) {
-        while(!wifi->readable());
-        nb_available = wifi->readable();
-        for (int i = 0; i < nb_available; i++) {
-            c = wifi->getc();
-            if (c == '*') {
-                o_find = true;
-            }
-            if (o_find && c != '\r' && c != '\n') {
-                str += c;
-                pos = str.find("*OPEN*");
-                if (pos != string::npos) {
-                    wifi->flush();
-                    return 0;
-                }
-            }
+    if (_sock_fd < 0) {
+        return -1;
+    }
+    Timer t;
+    t.reset();
+    t.start();
+    while(1) {
+        if (t.read_ms() > _timeout && _blocking == false) {
+            return -1;
+        }
+        if (eth->sreg<uint8_t>(_sock_fd, Sn_SR) == SOCK_ESTABLISHED) {
+            break;
         }
     }
-}
\ No newline at end of file
+    uint32_t ip = eth->sreg<uint32_t>(_sock_fd, Sn_DIPR);
+    char host[16];
+    snprintf(host, sizeof(host), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff);
+    uint16_t port = eth->sreg<uint16_t>(_sock_fd, Sn_DPORT);
+    connection._sock_fd = _sock_fd;
+    connection.set_address(host, port);
+    return 0;
+}
diff -r 0bcec6272784 -r fb15c35d1e28 Socket/UDPSocket.cpp
--- a/Socket/UDPSocket.cpp	Thu Dec 20 15:08:58 2012 +0000
+++ b/Socket/UDPSocket.cpp	Tue Aug 27 12:50:11 2013 +0000
@@ -18,168 +18,79 @@
 
 #include "UDPSocket.h"
 
-#include <string>
-#include <algorithm>
-
 UDPSocket::UDPSocket()
 {
-    endpoint_configured = false;
-    endpoint_read = false;
 }
 
 int UDPSocket::init(void)
 {
-    wifi->setProtocol(UDP);
-    wifi->exit();
+    if (_sock_fd < 0) {
+        _sock_fd = eth->new_socket();
+    }
+    eth->setProtocol(_sock_fd, UDP);
     return 0;
 }
 
 // Server initialization
 int UDPSocket::bind(int port)
 {
-    char cmd[17];
-    
+    if (_sock_fd < 0) {
+        _sock_fd = eth->new_socket();
+        if (_sock_fd < 0) {
+            return -1;
+        }
+    }
     // set local port
-    sprintf(cmd, "set i l %d\r", port);
-    if (!wifi->sendCommand(cmd, "AOK"))
-        return -1;
-        
-    // save
-    if (!wifi->sendCommand("save\r", "Stor"))
-        return -1;
-    
-    // reboot
-    wifi->reboot();
-    
+    eth->sreg<uint16_t>(_sock_fd, Sn_PORT, port);
     // set udp protocol
-    wifi->setProtocol(UDP);
-    
-    // connect the network
-    if (wifi->isDHCP()) {
-        if (!wifi->sendCommand("join\r", "DHCP=ON", NULL, 10000))
-            return -1;
-    } else {
-        if (!wifi->sendCommand("join\r", "Associated", NULL, 10000))
-            return -1;
-    }
-        
-    // exit
-    wifi->exit();
-    wifi->flush();
+    eth->setProtocol(_sock_fd, UDP);
+    eth->scmd(_sock_fd, OPEN);
     return 0;
 }
 
 // -1 if unsuccessful, else number of bytes written
 int UDPSocket::sendTo(Endpoint &remote, char *packet, int length)
 {
-    Timer tmr;
-    int idx = 0;
-
+    int size = eth->wait_writeable(_sock_fd, _blocking ? -1 : _timeout, length-1);
+    if (size < 0) {
+        return -1;
+    }
     confEndpoint(remote);
-
-    tmr.start();
-
-    while ((tmr.read_ms() < _timeout) || _blocking) {
-
-        idx += wifi->send(packet, length);
-
-        if (idx == length)
-            return idx;
-    }
-    return (idx == 0) ? -1 : idx;
+    int ret = eth->send(_sock_fd, packet, length);
+    return ret;
 }
 
 // -1 if unsuccessful, else number of bytes received
 int UDPSocket::receiveFrom(Endpoint &remote, char *buffer, int length)
 {
-    Timer tmr;
-    int idx = 0;
-    int nb_available = 0;
-    int time = -1;
-
-    if (_blocking) {
-        while (1) {
-            nb_available = wifi->readable();
-            if (nb_available != 0) {
-                break;
-            }
-        }
+    uint8_t info[8];
+    int size = eth->wait_readable(_sock_fd, _blocking ? -1 : _timeout, sizeof(info));
+    if (size < 0) {
+        return -1;
     }
-
-    tmr.start();
-
-    while (time < _timeout) {
-
-        nb_available = wifi->readable();
-        for (int i = 0; i < min(nb_available, length); i++) {
-            buffer[idx] = wifi->getc();
-            idx++;
-        }
-
-        if (idx == length) {
-            break;
-        }
-        
-        time = tmr.read_ms();
+    eth->recv(_sock_fd, (char*)info, sizeof(info));
+    readEndpoint(remote, info);
+    int udp_size = info[6]<<8|info[7]; 
+    //TEST_ASSERT(udp_size <= (size-sizeof(info)));
+    if (udp_size > (size-sizeof(info))) {
+        return -1;
     }
-
-    readEndpoint(remote);
-    return (idx == 0) ? -1 : idx;
+    return eth->recv(_sock_fd, buffer, udp_size);
 }
 
-bool UDPSocket::confEndpoint(Endpoint & ep)
+void UDPSocket::confEndpoint(Endpoint & ep)
 {
-    char * host;
-    char cmd[30];
-    if (!endpoint_configured) {
-        host = ep.get_address();
-        if (host[0] != '\0') {
-            // set host
-            sprintf(cmd, "set i h %s\r", host);
-            if (!wifi->sendCommand(cmd, "AOK"))
-                return false;
-                
-            // set remote port
-            sprintf(cmd, "set i r %d\r", ep.get_port());
-            if (!wifi->sendCommand(cmd, "AOK"))
-                return false;
-                
-            wifi->exit();
-            endpoint_configured = true;
-            return true;
-        }
-    }
-    return true;
+    char * host = ep.get_address();
+    // set remote host
+    eth->sreg_ip(_sock_fd, Sn_DIPR, host);
+    // set remote port
+    eth->sreg<uint16_t>(_sock_fd, Sn_DPORT, ep.get_port());
 }
 
-bool UDPSocket::readEndpoint(Endpoint & ep)
+void UDPSocket::readEndpoint(Endpoint & ep, uint8_t info[])
 {
-    char recv[256];
-    int begin = 0;
-    int end = 0;
-    string str;
-    string addr;
-    int port;
-    if (!endpoint_read) {
-        if (!wifi->sendCommand("get ip\r", NULL, recv))
-            return false;
-        wifi->exit();
-        str = recv;
-        begin = str.find("HOST=");
-        end = str.find("PROTO=");
-        if (begin != string::npos && end != string::npos) {
-            str = str.substr(begin + 5, end - begin - 5);
-            int pos = str.find(":");
-            if (pos != string::npos) {
-                addr = str.substr(0, pos);
-                port = atoi(str.substr(pos + 1).c_str());
-                ep.set_address(addr.c_str(), port);
-                endpoint_read = true;
-                wifi->flush();
-                return true;
-            }
-        }
-        wifi->flush();
-    }
-    return false;
+    char addr[17];
+    snprintf(addr, sizeof(addr), "%d.%d.%d.%d", info[0], info[1], info[2], info[3]);
+    uint16_t port = info[4]<<8|info[5];
+    ep.set_address(addr, port);
 }
diff -r 0bcec6272784 -r fb15c35d1e28 Socket/UDPSocket.h
--- a/Socket/UDPSocket.h	Thu Dec 20 15:08:58 2012 +0000
+++ b/Socket/UDPSocket.h	Tue Aug 27 12:50:11 2013 +0000
@@ -22,8 +22,6 @@
 #include "Endpoint.h"
 #include "Socket.h"
 
-#include <cstdint>
-
 /**
 UDP Socket
 */
@@ -63,13 +61,8 @@
     int receiveFrom(Endpoint &remote, char *buffer, int length);
     
 private:
-    bool confEndpoint(Endpoint & ep);
-    bool readEndpoint(Endpoint & ep);
-    bool endpoint_configured;
-    bool endpoint_read;
-    
+    void confEndpoint(Endpoint & ep);
+    void readEndpoint(Endpoint & ep, uint8_t info[]);
 };
 
-#include "def.h"
-
 #endif
diff -r 0bcec6272784 -r fb15c35d1e28 WIZ820io/WIZ820io.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WIZ820io/WIZ820io.cpp	Tue Aug 27 12:50:11 2013 +0000
@@ -0,0 +1,374 @@
+/* Copyright (C) 2012 mbed.org, MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#include "mbed.h"
+#include "mbed_debug.h"
+#include "WIZ820io.h"
+#include "DNSClient.h"
+
+//Debug is disabled by default
+#if 0
+#define DBG(...) do{debug("%p %d %s ", this,__LINE__,__PRETTY_FUNCTION__); debug(__VA_ARGS__); } while(0);
+//#define DBG(x, ...) debug("[WIZ820io:DBG]"x"\r\n", ##__VA_ARGS__);
+#define WARN(x, ...) debug("[WIZ820io:WARN]"x"\r\n", ##__VA_ARGS__);
+#define ERR(x, ...) debug("[WIZ820io:ERR]"x"\r\n", ##__VA_ARGS__);
+#else
+#define DBG(x, ...)
+#define WARN(x, ...)
+#define ERR(x, ...)
+#endif
+
+#if 1
+#define INFO(x, ...) debug("[WIZ820io:INFO]"x"\r\n", ##__VA_ARGS__);
+#else
+#define INFO(x, ...)
+#endif
+
+#define DBG_SPI 0
+
+WIZ820io* WIZ820io::inst;
+
+WIZ820io::WIZ820io(PinName mosi, PinName miso, PinName sclk, PinName _cs, PinName _reset):
+    cs(_cs), reset_pin(_reset)
+{
+    spi = new SPI(mosi, miso, sclk);
+    cs = 1;
+    reset_pin = 1;
+    inst = this;
+}
+
+WIZ820io::WIZ820io(SPI* spi, PinName _cs, PinName _reset):
+    cs(_cs), reset_pin(_reset)
+{
+    this->spi = spi;
+    cs = 1;
+    reset_pin = 1;
+    inst = this;
+}
+
+bool WIZ820io::join()
+{
+    reg_wr<uint32_t>(SIPR, ip);
+    reg_wr<uint32_t>(GAR, gateway);
+    reg_wr<uint32_t>(SUBR, netmask);
+    return true;
+}
+
+bool WIZ820io::setProtocol(int socket, Protocol p)
+{
+    if (socket < 0) {
+        return false;
+    }
+    sreg<uint8_t>(socket, Sn_MR, p);
+    return true;
+}
+
+bool WIZ820io::connect(int socket, const char * host, int port, int timeout_ms)
+{
+    if (socket < 0) {
+        return false;
+    }
+    sreg<uint8_t>(socket, Sn_MR, TCP);
+    scmd(socket, OPEN);
+    sreg_ip(socket, Sn_DIPR, host);
+    sreg<uint16_t>(socket, Sn_DPORT, port);
+    sreg<uint16_t>(socket, Sn_PORT, new_port());
+    scmd(socket, CONNECT);
+    Timer t;
+    t.reset();
+    t.start();
+    while(!is_connected(socket)) {
+        if (t.read_ms() > timeout_ms) {
+            return false;
+        }
+    }
+    return true;
+}
+
+bool WIZ820io::gethostbyname(const char* host, uint32_t* ip)
+{
+    uint32_t addr = str_to_ip(host);
+    char buf[17];
+    snprintf(buf, sizeof(buf), "%d.%d.%d.%d", (addr>>24)&0xff, (addr>>16)&0xff, (addr>>8)&0xff, addr&0xff);
+    if (strcmp(buf, host) == 0) {
+        *ip = addr;
+        return true;
+    }
+    DNSClient client;
+    if(client.lookup(host)) {
+        *ip = client.ip;
+        return true;
+    }
+    return false;
+}
+
+bool WIZ820io::disconnect()
+{
+    return true;
+}
+
+bool WIZ820io::is_connected(int socket)
+{
+    if (sreg<uint8_t>(socket, Sn_SR) == SOCK_ESTABLISHED) {
+        return true;
+    }
+    return false;
+}
+
+void WIZ820io::reset()
+{
+    reset_pin = 1;
+    reset_pin = 0;
+    wait_us(2); // 2us
+    reset_pin = 1;
+    wait_ms(150); // 150ms
+    reg_wr<uint8_t>(MR, 1<<7);
+#ifdef TARGET_LPC1114
+    uint8_t mac[6] = {0x00,0x02,0xf7,0xf0,0x00,0x00};
+#else
+    uint8_t mac[6];
+    mbed_mac_address((char*)mac);
+#endif
+    reg_wr_mac(SHAR, mac);
+}
+
+bool WIZ820io::close(int socket)
+{
+    if (socket < 0) {
+        return false;
+    }
+    // if not connected, return
+    if (sreg<uint8_t>(socket, Sn_SR) == SOCK_CLOSED) {
+        return true;
+    }
+    if (sreg<uint8_t>(socket, Sn_MR) == TCP) {
+        scmd(socket, DISCON);
+    }
+    scmd(socket, CLOSE);
+    return true;
+}
+
+int WIZ820io::wait_readable(int socket, int wait_time_ms, int req_size)
+{
+    if (socket < 0) {
+        return -1;
+    }
+    Timer t;
+    t.reset();
+    t.start();
+    while(1) {
+        int size = sreg<uint16_t>(socket, Sn_RX_RSR);
+        if (size > req_size) {
+            return size;
+        }
+        if (wait_time_ms != (-1) && t.read_ms() > wait_time_ms) {
+            break;
+        }
+    }
+    return -1;
+}
+
+int WIZ820io::wait_writeable(int socket, int wait_time_ms, int req_size)
+{
+    if (socket < 0) {
+        return -1;
+    }
+    Timer t;
+    t.reset();
+    t.start();
+    while(1) {
+        int size = sreg<uint16_t>(socket, Sn_TX_FSR);
+        if (size > req_size) {
+            return size;
+        }
+        if (wait_time_ms != (-1) && t.read_ms() > wait_time_ms) {
+            break;
+        }
+    }
+    return -1;
+}
+
+int WIZ820io::send(int socket, const char * str, int len)
+{
+    if (socket < 0) {
+        return -1;
+    }
+    uint16_t base = 0x8000 + socket * 0x800;
+    uint16_t ptr = sreg<uint16_t>(socket, Sn_TX_WR);
+    uint16_t dst = base + (ptr&(0x800-1));    
+    if ((dst + len) > (base+0x800)) {
+        int len2 = base + 0x800 - dst;
+        spi_write(dst, (uint8_t*)str, len2);
+        spi_write(base, (uint8_t*)str+len2, len-len2);
+    } else {
+        spi_write(dst, (uint8_t*)str, len);
+    }
+    sreg<uint16_t>(socket, Sn_TX_WR, ptr + len);
+    scmd(socket, SEND);
+    return len;
+}
+
+int WIZ820io::recv(int socket, char* buf, int len)
+{
+    if (socket < 0) {
+        return -1;
+    }
+    uint16_t base = 0xc000 + socket * 0x800;
+    uint16_t ptr = sreg<uint16_t>(socket, Sn_RX_RD);
+    uint16_t src = base + (ptr&(0x800-1));    
+    if ((src + len) > (base+0x800)) {
+        int len2 = base + 0x800 - src;
+        spi_read(src, (uint8_t*)buf, len2);
+        spi_read(base, (uint8_t*)buf+len2, len-len2);
+    } else {
+        spi_read(src, (uint8_t*)buf, len);
+    }
+    sreg<uint16_t>(socket, Sn_RX_RD, ptr + len);
+    scmd(socket, RECV);
+    return len;
+}
+
+int WIZ820io::new_socket()
+{
+    for(int s = 0; s < 8; s++) {
+        if (sreg<uint8_t>(s, Sn_SR) == SOCK_CLOSED) {
+            return s;
+        }
+    }
+    return -1;
+}
+
+uint16_t WIZ820io::new_port()
+{
+    uint16_t port = rand();
+    port |= 49152;
+    return port;
+}
+
+void WIZ820io::scmd(int socket, Command cmd)
+{
+    sreg<uint8_t>(socket, Sn_CR, cmd);
+    while(sreg<uint8_t>(socket, Sn_CR))
+        ;
+}
+
+void WIZ820io::spi_write(uint16_t addr, const uint8_t *buf, uint16_t len)
+{
+    cs = 0;
+    spi->write(addr >> 8);
+    spi->write(addr & 0xff);
+    spi->write((0x80 | ((len & 0x7f00) >> 8)));
+    spi->write(len & 0xff);
+    for(int i = 0; i < len; i++) {
+        spi->write(buf[i]);
+    }
+    cs = 1;
+
+#if DBG_SPI
+    debug("[SPI]W %04x(%d)", addr, len);
+    for(int i = 0; i < len; i++) {
+        debug(" %02x", buf[i]);
+        if (i > 16) {
+            debug(" ...");
+            break;
+        }
+    }
+    debug("\r\n");
+#endif    
+}
+
+void WIZ820io::spi_read(uint16_t addr, uint8_t *buf, uint16_t len)
+{
+    cs = 0;
+    spi->write(addr >> 8);
+    spi->write(addr & 0xff);
+    spi->write((0x00 | ((len & 0x7f00) >> 8)));
+    spi->write(len & 0xff);
+    for(int i = 0; i < len; i++) {
+        buf[i] = spi->write(0);
+    }
+    cs = 1;
+
+#if DBG_SPI
+    debug("[SPI]R %04x(%d)", addr, len);
+    for(int i = 0; i < len; i++) {
+        debug(" %02x", buf[i]);
+        if (i > 16) {
+            debug(" ...");
+            break;
+        }
+    }
+    debug("\r\n");
+    if ((addr&0xf0ff)==0x4026 || (addr&0xf0ff)==0x4003) {
+        wait_ms(200);
+    }
+#endif    
+}
+
+uint32_t str_to_ip(const char* str)
+{
+    uint32_t ip = 0;
+    char* p = (char*)str;
+    for(int i = 0; i < 4; i++) {
+        ip |= atoi(p);
+        p = strchr(p, '.');
+        if (p == NULL) {
+            break;
+        }
+        ip <<= 8;
+        p++;
+    }
+    return ip;
+}
+
+void printfBytes(char* str, uint8_t* buf, int len)
+{
+    printf("%s %d:", str, len);
+    for(int i = 0; i < len; i++) {
+        printf(" %02x", buf[i]);
+    }
+    printf("\n");  
+}
+
+void printHex(uint8_t* buf, int len)
+{
+    for(int i = 0; i < len; i++) {
+        if ((i%16) == 0) {
+            printf("%p", buf+i);
+        }
+        printf(" %02x", buf[i]);
+        if ((i%16) == 15) {
+            printf("\n");
+        }
+    }
+    printf("\n");
+}
+
+void debug_hex(uint8_t* buf, int len)
+{
+    for(int i = 0; i < len; i++) {
+        if ((i%16) == 0) {
+            debug("%p", buf+i);
+        }
+        debug(" %02x", buf[i]);
+        if ((i%16) == 15) {
+            debug("\n");
+        }
+    }
+    debug("\n");
+}
diff -r 0bcec6272784 -r fb15c35d1e28 WIZ820io/WIZ820io.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WIZ820io/WIZ820io.h	Tue Aug 27 12:50:11 2013 +0000
@@ -0,0 +1,252 @@
+/* Copyright (C) 2012 mbed.org, MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ */
+
+#pragma once
+
+#include "mbed.h"
+#include "mbed_debug.h"
+
+#define TEST_ASSERT(A) while(!(A)){debug("\n\n%s@%d %s ASSERT!\n\n",__PRETTY_FUNCTION__,__LINE__,#A);exit(1);};
+
+#define DEFAULT_WAIT_RESP_TIMEOUT 500
+
+enum Protocol {
+    CLOSED = 0,
+    TCP    = 1,
+    UDP    = 2,
+};
+
+enum Command {
+    OPEN      = 0x01,
+    LISTEN    = 0x02,
+    CONNECT   = 0x04,
+    DISCON    = 0x08,
+    CLOSE     = 0x10,
+    SEND      = 0x20,
+    SEND_MAC  = 0x21, 
+    SEND_KEEP = 0x22,
+    RECV      = 0x40,
+    
+};
+
+enum Status {
+    SOCK_CLOSED      = 0x00,
+    SOCK_INIT        = 0x13,
+    SOCK_LISTEN      = 0x14,
+    SOCK_SYNSENT     = 0x15,
+    SOCK_ESTABLISHED = 0x17,
+    SOCK_CLOSE_WAIT  = 0x1c,
+    SOCK_UDP         = 0x22,
+};
+
+#define MAX_SOCK_NUM 8
+
+#define MR        0x0000
+#define GAR       0x0001
+#define SUBR      0x0005
+#define SHAR      0x0009
+#define SIPR      0x000f
+#define PHYSTATUS 0x0035
+// socket
+#define Sn_MR     0x4000
+#define Sn_CR     0x4001
+#define Sn_SR     0x4003
+#define Sn_PORT   0x4004
+#define Sn_DIPR   0x400c
+#define Sn_DPORT  0x4010
+#define Sn_TX_FSR 0x4020
+#define Sn_TX_WR  0x4024
+#define Sn_RX_RSR 0x4026
+#define Sn_RX_RD  0x4028
+
+class WIZ820io {
+public:
+    /*
+    * Constructor
+    *
+    * @param tx mbed pin to use for tx line of Serial interface
+    * @param rx mbed pin to use for rx line of Serial interface
+    * @param reset reset pin of the WIZ820io module
+    */
+    WIZ820io(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset);
+    WIZ820io(SPI* spi, PinName cs, PinName reset);
+
+    /*
+    * Connect the WIZ820io module to the ssid contained in the constructor.
+    *
+    * @return true if connected, false otherwise
+    */
+    bool join();
+
+    /*
+    * Disconnect the WIZ820io module from the access point
+    *
+    * @ returns true if successful
+    */
+    bool disconnect();
+
+    /*
+    * Open a tcp connection with the specified host on the specified port
+    *
+    * @param host host (can be either an ip address or a name. If a name is provided, a dns request will be established)
+    * @param port port
+    * @ returns true if successful
+    */
+    bool connect(int socket, const char * host, int port, int timeout_ms = 10*1000);
+
+    /*
+    * Set the protocol (UDP or TCP)
+    *
+    * @param p protocol
+    * @ returns true if successful
+    */
+    bool setProtocol(int socket, Protocol p);
+
+    /*
+    * Reset the WIZ820io module
+    */
+    void reset();
+    
+   
+    int wait_readable(int socket, int wait_time_ms, int req_size = 0);
+
+    int wait_writeable(int socket, int wait_time_ms, int req_size = 0);
+
+    /*
+    * Check if a tcp link is active
+    *
+    * @returns true if successful
+    */
+    bool is_connected(int socket);
+
+    /*
+    * Close a tcp connection
+    *
+    * @ returns true if successful
+    */
+    bool close(int socket);
+
+    /*
+    * @param str string to be sent
+    * @param len string length
+    */
+    int send(int socket, const char * str, int len);
+
+    int recv(int socket, char* buf, int len);
+
+    /*
+    * Return true if the module is using dhcp
+    *
+    * @returns true if the module is using dhcp
+    */
+    bool isDHCP() {
+        return dhcp;
+    }
+
+    bool gethostbyname(const char* host, uint32_t* ip);
+
+    static WIZ820io * getInstance() {
+        return inst;
+    };
+
+    int new_socket();
+    uint16_t new_port();
+    void scmd(int socket, Command cmd);
+
+    template<typename T>
+    void sreg(int socket, uint16_t addr, T data) {
+        reg_wr<T>(addr+0x100*socket, data);
+    }
+
+    template<typename T>
+    T sreg(int socket, uint16_t addr) {
+        return reg_rd<T>(addr+0x100*socket);
+    }
+
+    template<typename T>
+    void reg_wr(uint16_t addr, T data) {
+        uint8_t buf[sizeof(T)];
+        *reinterpret_cast<T*>(buf) = data;
+        for(int i = 0; i < sizeof(buf)/2; i++) { //  Little Endian to Big Endian
+            uint8_t t = buf[i];
+            buf[i] = buf[sizeof(buf)-1-i];
+            buf[sizeof(buf)-1-i] = t;
+        }
+        spi_write(addr, buf, sizeof(buf));
+    }
+
+    template<typename T>
+    T reg_rd(uint16_t addr) {
+        uint8_t buf[sizeof(T)];
+        spi_read(addr, buf, sizeof(buf));
+        for(int i = 0; i < sizeof(buf)/2; i++) { // Big Endian to Little Endian
+            uint8_t t = buf[i];
+            buf[i] = buf[sizeof(buf)-1-i];
+            buf[sizeof(buf)-1-i] = t;
+        }
+        return *reinterpret_cast<T*>(buf);
+    }
+
+    void reg_rd_mac(uint16_t addr, uint8_t* data) {
+        spi_read(addr, data, 6);
+    }
+
+    void reg_wr_ip(uint16_t addr, const char* ip) {
+        uint8_t buf[4];
+        char* p = (char*)ip;
+        for(int i = 0; i < 4; i++) {
+            buf[i] = atoi(p);
+            p = strchr(p, '.');
+            if (p == NULL) {
+                break;
+            }
+            p++;
+        }
+        spi_write(addr, buf, sizeof(buf));
+    }
+
+    void sreg_ip(int socket, uint16_t addr, const char* ip) {
+        reg_wr_ip(addr+0x100*socket, ip);
+    }
+
+protected:
+    uint32_t ip;
+    uint32_t netmask;
+    uint32_t gateway;
+    uint32_t dnsaddr;
+    bool dhcp;
+
+    static WIZ820io* inst;
+
+    void reg_wr_mac(uint16_t addr, uint8_t* data) {
+        spi_write(addr, data, 6);
+    }
+
+    void spi_write(uint16_t addr, const uint8_t *buf, uint16_t len);
+    void spi_read(uint16_t addr, uint8_t *buf, uint16_t len);
+    SPI* spi;
+    DigitalOut cs;
+    DigitalOut reset_pin;
+};
+
+// WIZ820io.cpp
+extern uint32_t str_to_ip(const char* str);
+extern void printfBytes(char* str, uint8_t* buf, int len);
+extern void printHex(uint8_t* buf, int len);
+extern void debug_hex(uint8_t* buf, int len);
diff -r 0bcec6272784 -r fb15c35d1e28 WIZ820ioInterface.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WIZ820ioInterface.cpp	Tue Aug 27 12:50:11 2013 +0000
@@ -0,0 +1,75 @@
+// WIZ820ioInterface.cpp 2013/4/5
+#include "WIZ820ioInterface.h"
+#include "DHCPClient.h"
+
+WIZ820ioInterface::WIZ820ioInterface(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset) :
+    WIZ820io(mosi, miso, sclk, cs, reset)
+{
+    ip_set = false;
+}
+
+WIZ820ioInterface::WIZ820ioInterface(SPI* spi, PinName cs, PinName reset) :
+    WIZ820io(spi, cs, reset)
+{
+    ip_set = false;
+}
+
+int WIZ820ioInterface::init()
+{
+    dhcp = true;
+    reset();
+    return 0;
+}
+
+int WIZ820ioInterface::init(const char* ip, const char* mask, const char* gateway)
+{
+    dhcp = false;
+    this->ip = str_to_ip(ip);
+    strcpy(ip_string, ip);
+    ip_set = true;
+    this->netmask = str_to_ip(mask);
+    this->gateway = str_to_ip(gateway);
+    reset();
+    return 0;
+}
+
+int WIZ820ioInterface::connect()
+{
+    if (dhcp) {
+        int r = IPrenew();
+        if (r < 0) {
+            return r;
+        }
+    }
+    return join();
+}
+
+int WIZ820ioInterface::disconnect()
+{
+    return WIZ820io::disconnect();
+}
+
+char* WIZ820ioInterface::getIPAddress()
+{
+    uint32_t ip = reg_rd<uint32_t>(SIPR);
+    snprintf(ip_string, sizeof(ip_string), "%d.%d.%d.%d", (ip>>24)&0xff,(ip>>16)&0xff,(ip>>8)&0xff,ip&0xff); 
+    ip_set = true;
+    return ip_string;
+}
+
+int WIZ820ioInterface::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 -1;
+    }
+    printf("Connected, IP: %d.%d.%d.%d\n", dhcp.yiaddr[0], dhcp.yiaddr[1], dhcp.yiaddr[2], dhcp.yiaddr[3]);
+    ip      = (dhcp.yiaddr[0]<<24) | (dhcp.yiaddr[1]<<16) | (dhcp.yiaddr[2]<<8) | dhcp.yiaddr[3];
+    gateway = (dhcp.gateway[0]<<24) | (dhcp.gateway[1]<<16) | (dhcp.gateway[2]<<8) | dhcp.gateway[3];
+    netmask = (dhcp.netmask[0]<<24) | (dhcp.netmask[1]<<16) | (dhcp.netmask[2]<<8) | dhcp.netmask[3];
+    dnsaddr = (dhcp.dnsaddr[0]<<24) | (dhcp.dnsaddr[1]<<16) | (dhcp.dnsaddr[2]<<8) | dhcp.dnsaddr[3];
+    return 0;
+}
diff -r 0bcec6272784 -r fb15c35d1e28 WIZ820ioInterface.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WIZ820ioInterface.h	Tue Aug 27 12:50:11 2013 +0000
@@ -0,0 +1,84 @@
+// WIZ820ioInterface.h 2013/4/10
+/* Copyright (C) 2012 mbed.org, MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#pragma once
+
+#include "WIZ820io.h"
+
+ /** Interface using WIZ820io to connect to an IP-based network
+ *
+ */
+class WIZ820ioInterface: public WIZ820io {
+public:
+
+    /**
+    * Constructor
+    *
+    * \param mosi mbed pin to use for SPI
+    * \param miso mbed pin to use for SPI
+    * \param sclk mbed pin to use for SPI
+    * \param cs
+    * \param reset reset pin of the WIZ820io module
+    */
+  WIZ820ioInterface(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset);
+  WIZ820ioInterface(SPI* spi, PinName cs, PinName reset);
+
+  /** Initialize the interface with DHCP.
+  * Initialize the interface and configure it to use DHCP (no connection at this point).
+  * \return 0 on success, a negative number on failure
+  */
+  int init(); //With DHCP
+
+  /** Initialize the interface with a static IP address.
+  * Initialize the interface and configure it with the following static configuration (no connection at this point).
+  * \param ip the IP address to use
+  * \param mask the IP address mask
+  * \param gateway the gateway to use
+  * \return 0 on success, a negative number on failure
+  */
+  int init(const char* ip, const char* mask, const char* gateway);
+
+  /** Connect
+  * Bring the interface up, start DHCP if needed.
+  * \return 0 on success, a negative number on failure
+  */
+  int connect();
+  
+  /** Disconnect
+  * Bring the interface down
+  * \return 0 on success, a negative number on failure
+  */
+  int disconnect();
+  
+  /** Get IP address
+  *
+  * @ returns ip address
+  */
+  char* getIPAddress();
+
+  int IPrenew(int timeout_ms = 15*1000);
+    
+private:
+    char ip_string[20];
+    bool ip_set;
+};
+
+#include "TCPSocketConnection.h"
+#include "TCPSocketServer.h"
+#include "UDPSocket.h"
diff -r 0bcec6272784 -r fb15c35d1e28 Wifly/CBuffer.h
--- a/Wifly/CBuffer.h	Thu Dec 20 15:08:58 2012 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,75 +0,0 @@
-/* Copyright (C) 2012 mbed.org, MIT License
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
- * and associated documentation files (the "Software"), to deal in the Software without restriction,
- * including without limitation the rights to use, copy, modify, merge, publish, distribute,
- * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all copies or
- * substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
- * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
- * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- */
-
-#ifndef CIRCBUFFER_H_
-#define CIRCBUFFER_H_
-
-template <class T>
-class CircBuffer {
-public:
-    CircBuffer(int length) {
-        write = 0;
-        read = 0;
-        size = length + 1;
-        buf = (T *)malloc(size * sizeof(T));
-    };
-
-    bool isFull() {
-        return (((write + 1) % size) == read);
-    };
-
-    bool isEmpty() {
-        return (read == write);
-    };
-
-    void queue(T k) {
-        if (isFull()) {
-            read++;
-            read %= size;
-        }
-        buf[write++] = k;
-        write %= size;
-    }
-    
-    void flush() {
-        read = 0;
-        write = 0;
-    }
-    
-
-    uint32_t available() {
-        return (write >= read) ? write - read : size - read + write;
-    };
-
-    bool dequeue(T * c) {
-        bool empty = isEmpty();
-        if (!empty) {
-            *c = buf[read++];
-            read %= size;
-        }
-        return(!empty);
-    };
-
-private:
-    volatile uint32_t write;
-    volatile uint32_t read;
-    uint32_t size;
-    T * buf;
-};
-
-#endif
diff -r 0bcec6272784 -r fb15c35d1e28 Wifly/Wifly.cpp
--- a/Wifly/Wifly.cpp	Thu Dec 20 15:08:58 2012 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,546 +0,0 @@
-/* Copyright (C) 2012 mbed.org, MIT License
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
- * and associated documentation files (the "Software"), to deal in the Software without restriction,
- * including without limitation the rights to use, copy, modify, merge, publish, distribute,
- * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all copies or
- * substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
- * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
- * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- */
-
-#include "mbed.h"
-#include "Wifly.h"
-#include <string>
-#include <algorithm>
-
-//Debug is disabled by default
-#if (0 && !defined(TARGET_LPC11U24))
-#define DBG(x, ...) std::printf("[Wifly : DBG]"x"\r\n", ##__VA_ARGS__);
-#define WARN(x, ...) std::printf("[Wifly : WARN]"x"\r\n", ##__VA_ARGS__);
-#define ERR(x, ...) std::printf("[Wifly : ERR]"x"\r\n", ##__VA_ARGS__);
-#else
-#define DBG(x, ...)
-#define WARN(x, ...)
-#define ERR(x, ...)
-#endif
-
-#if !defined(TARGET_LPC11U24)
-#define INFO(x, ...) printf("[Wifly : INFO]"x"\r\n", ##__VA_ARGS__);
-#else
-#define INFO(x, ...)
-#endif
-
-#define MAX_TRY_JOIN 3
-
-Wifly * Wifly::inst;
-
-Wifly::Wifly(   PinName tx, PinName rx, PinName _reset, PinName tcp_status, const char * ssid, const char * phrase, Security sec):
-    wifi(tx, rx), reset_pin(_reset), tcp_status(tcp_status), buf_wifly(256)
-{
-    memset(&state, 0, sizeof(state));
-    state.sec = sec;
-
-    // change all ' ' in '$' in the ssid and the passphrase
-    strcpy(this->ssid, ssid);
-    for (int i = 0; i < strlen(ssid); i++) {
-        if (this->ssid[i] == ' ')
-            this->ssid[i] = '$';
-    }
-    strcpy(this->phrase, phrase);
-    for (int i = 0; i < strlen(phrase); i++) {
-        if (this->phrase[i] == ' ')
-            this->phrase[i] = '$';
-    }
-
-    inst = this;
-    attach_rx(false);
-    state.cmd_mode = false;
-}
-
-bool Wifly::join()
-{
-    char cmd[20];
-
-    for (int i= 0; i < MAX_TRY_JOIN; i++) {
-
-        // no auto join
-        if (!sendCommand("set w j 0\r", "AOK"))
-            continue;
-
-        //no echo
-        if (!sendCommand("set u m 1\r", "AOK"))
-            continue;
-
-        // set time
-        if (!sendCommand("set c t 30\r", "AOK"))
-            continue;
-
-        // set size
-        if (!sendCommand("set c s 1024\r", "AOK"))
-            continue;
-
-        // red led on when tcp connection active
-        if (!sendCommand("set s i 0x40\r", "AOK"))
-            continue;
-
-        // no string sent to the tcp client
-        if (!sendCommand("set c r 0\r", "AOK"))
-            continue;
-
-        // tcp protocol
-        if (!sendCommand("set i p 2\r", "AOK"))
-            continue;
-
-        // tcp retry
-        if (!sendCommand("set i f 0x7\r", "AOK"))
-            continue;
-            
-        // set dns server
-        if (!sendCommand("set d n rn.microchip.com\r", "AOK"))
-            continue;
-
-        //dhcp
-        sprintf(cmd, "set i d %d\r", (state.dhcp) ? 1 : 0);
-        if (!sendCommand(cmd, "AOK"))
-            continue;
-
-        // ssid
-        sprintf(cmd, "set w s %s\r", ssid);
-        if (!sendCommand(cmd, "AOK"))
-            continue;
-
-        //auth
-        sprintf(cmd, "set w a %d\r", state.sec);
-        if (!sendCommand(cmd, "AOK"))
-            continue;
-
-        // if no dhcp, set ip, netmask and gateway
-        if (!state.dhcp) {
-            DBG("not dhcp\r");
-
-            sprintf(cmd, "set i a %s\r\n", ip);
-            if (!sendCommand(cmd, "AOK"))
-                continue;
-
-            sprintf(cmd, "set i n %s\r", netmask);
-            if (!sendCommand(cmd, "AOK"))
-                continue;
-
-            sprintf(cmd, "set i g %s\r", gateway);
-            if (!sendCommand(cmd, "AOK"))
-                continue;
-        }
-
-        //key step
-        if (state.sec != NONE) {
-            if (state.sec == WPA)
-                sprintf(cmd, "set w p %s\r", phrase);
-            else if (state.sec == WEP_128)
-                sprintf(cmd, "set w k %s\r", phrase);
-
-            if (!sendCommand(cmd, "AOK"))
-                continue;
-        }
-
-        //join the network (10s timeout)
-        if (state.dhcp) {
-            if (!sendCommand("join\r", "DHCP=ON", NULL, 10000))
-                continue;
-        } else {
-            if (!sendCommand("join\r", "Associated", NULL, 10000))
-                continue;
-        }
-
-        if (!sendCommand("save\r", "Stor"))
-            continue;
-
-        exit();
-
-        state.associated = true;
-        INFO("\r\nssid: %s\r\nphrase: %s\r\nsecurity: %s\r\n\r\n", this->ssid, this->phrase, getStringSecurity());
-        return true;
-    }
-    return false;
-}
-
-
-bool Wifly::setProtocol(Protocol p)
-{
-    // use udp auto pairing
-    char cmd[20];
-    sprintf(cmd, "set i p %d\r", p);
-    if (!sendCommand(cmd, "AOK"))
-        return false;
-
-    switch(p) {
-        case TCP:
-            // set ip flags: tcp retry enabled
-            if (!sendCommand("set i f 0x07\r", "AOK"))
-                return false;
-            break;
-        case UDP:
-            // set ip flags: udp auto pairing enabled
-            if (!sendCommand("set i h 0.0.0.0\r", "AOK"))
-                return false;
-            if (!sendCommand("set i f 0x40\r", "AOK"))
-                return false;
-            break;
-    }
-    state.proto = p;
-    return true;
-}
-
-char * Wifly::getStringSecurity()
-{
-    switch(state.sec) {
-        case NONE:
-            return "NONE";
-        case WEP_128:
-            return "WEP_128";
-        case WPA:
-            return "WPA";
-    }
-    return "UNKNOWN";
-}
-
-bool Wifly::connect(const char * host, int port)
-{
-    char rcv[20];
-    char cmd[20];
-
-    // try to open
-    sprintf(cmd, "open %s %d\r", host, port);
-    if (sendCommand(cmd, "OPEN", NULL, 10000)) {
-        state.tcp = true;
-        state.cmd_mode = false;
-        return true;
-    }
-
-    // if failed, retry and parse the response
-    if (sendCommand(cmd, NULL, rcv, 5000)) {
-        if (strstr(rcv, "OPEN") == NULL) {
-            if (strstr(rcv, "Connected") != NULL) {
-                wait(0.25);
-                if (!sendCommand("close\r", "CLOS"))
-                    return false;
-                wait(0.25);
-                if (!sendCommand(cmd, "OPEN", NULL, 10000))
-                    return false;
-            } else {
-                return false;
-            }
-        }
-    } else {
-        return false;
-    }
-
-    state.tcp = true;
-    state.cmd_mode = false;
-
-    return true;
-}
-
-
-bool Wifly::gethostbyname(const char * host, char * ip)
-{
-    string h = host;
-    char cmd[30], rcv[100];
-    int l = 0;
-    char * point;
-    int nb_digits = 0;
-
-    // no dns needed
-    int pos = h.find(".");
-    if (pos != string::npos) {
-        string sub = h.substr(0, h.find("."));
-        nb_digits = atoi(sub.c_str());
-    }
-    //printf("substrL %s\r\n", sub.c_str());
-    if (count(h.begin(), h.end(), '.') == 3 && nb_digits > 0) {
-        strcpy(ip, host);
-    }
-    // dns needed
-    else {
-        nb_digits = 0;
-        sprintf(cmd, "lookup %s\r", host);
-        if (!sendCommand(cmd, NULL, rcv))
-            return false;
-
-        // look for the ip address
-        char * begin = strstr(rcv, "=") + 1;
-        for (int i = 0; i < 3; i++) {
-            point = strstr(begin + l, ".");
-            DBG("str: %s", begin + l);
-            l += point - (begin + l) + 1;
-        }
-        DBG("str: %s", begin + l);
-        while(*(begin + l + nb_digits) >= '0' && *(begin + l + nb_digits) <= '9') {
-            DBG("digit: %c", *(begin + l + nb_digits));
-            nb_digits++;
-        }
-        memcpy(ip, begin, l + nb_digits);
-        ip[l+nb_digits] = 0;
-        DBG("ip from dns: %s", ip);
-    }
-    return true;
-}
-
-
-void Wifly::flush()
-{
-    buf_wifly.flush();
-}
-
-bool Wifly::sendCommand(const char * cmd, const char * ack, char * res, int timeout)
-{
-    if (!state.cmd_mode) {
-        cmdMode();
-    }
-    if (send(cmd, strlen(cmd), ack, res, timeout) == -1) {
-        ERR("sendCommand: cannot %s\r\n", cmd);
-        exit();
-        return false;
-    }
-    return true;
-}
-
-bool Wifly::cmdMode()
-{
-    // if already in cmd mode, return
-    if (state.cmd_mode)
-        return true;
-
-    if (send("$$$", 3, "CMD") == -1) {
-        ERR("cannot enter in cmd mode\r\n");
-        exit();
-        return false;
-    }
-    state.cmd_mode = true;
-    return true;
-}
-
-bool Wifly::disconnect()
-{
-    // if already disconnected, return
-    if (!state.associated)
-        return true;
-
-    if (!sendCommand("leave\r", "DeAuth"))
-        return false;
-    exit();
-
-    state.associated = false;
-    return true;
-
-}
-
-bool Wifly::is_connected()
-{
-    return (tcp_status.read() ==  1) ? true : false;
-}
-
-
-void Wifly::reset()
-{
-    reset_pin = 0;
-    wait(0.2);
-    reset_pin = 1;
-    wait(0.2);
-}
-
-bool Wifly::reboot()
-{
-    // if already in cmd mode, return
-    if (!sendCommand("reboot\r"))
-        return false;
-    
-    wait(0.3);
-
-    state.cmd_mode = false;
-    return true;
-}
-
-bool Wifly::close()
-{
-    // if not connected, return
-    if (!state.tcp)
-        return true;
-
-    wait(0.25);
-    if (!sendCommand("close\r", "CLOS"))
-        return false;
-    exit();
-
-    state.tcp = false;
-    return true;
-}
-
-
-int Wifly::putc(char c)
-{
-    while (!wifi.writeable());
-    return wifi.putc(c);
-}
-
-
-bool Wifly::exit()
-{
-    flush();
-    if (!state.cmd_mode)
-        return true;
-    if (!sendCommand("exit\r", "EXIT"))
-        return false;
-    state.cmd_mode = false;
-    flush();
-    return true;
-}
-
-
-int Wifly::readable()
-{
-    return buf_wifly.available();
-}
-
-int Wifly::writeable()
-{
-    return wifi.writeable();
-}
-
-char Wifly::getc()
-{
-    char c;
-    while (!buf_wifly.available());
-    buf_wifly.dequeue(&c);
-    return c;
-}
-
-void Wifly::handler_rx(void)
-{
-    //read characters
-    while (wifi.readable())
-        buf_wifly.queue(wifi.getc());
-}
-
-void Wifly::attach_rx(bool callback)
-{
-    if (!callback)
-        wifi.attach(NULL);
-    else
-        wifi.attach(this, &Wifly::handler_rx);
-}
-
-
-int Wifly::send(const char * str, int len, const char * ACK, char * res, int timeout)
-{
-    char read;
-    size_t found = string::npos;
-    string checking;
-    Timer tmr;
-    int result = 0;
-
-    DBG("will send: %s\r\n",str);
-
-    attach_rx(false);
-
-    //We flush the buffer
-    while (wifi.readable())
-        wifi.getc();
-
-    if (!ACK || !strcmp(ACK, "NO")) {
-        for (int i = 0; i < len; i++)
-            result = (putc(str[i]) == str[i]) ? result + 1 : result;
-    } else {
-        //We flush the buffer
-        while (wifi.readable())
-            wifi.getc();
-
-        tmr.start();
-        for (int i = 0; i < len; i++)
-            result = (putc(str[i]) == str[i]) ? result + 1 : result;
-
-        while (1) {
-            if (tmr.read_ms() > timeout) {
-                //We flush the buffer
-                while (wifi.readable())
-                    wifi.getc();
-
-                DBG("check: %s\r\n", checking.c_str());
-
-                attach_rx(true);
-                return -1;
-            } else if (wifi.readable()) {
-                read = wifi.getc();
-                if ( read != '\r' && read != '\n') {
-                    checking += read;
-                    found = checking.find(ACK);
-                    if (found != string::npos) {
-                        wait(0.01);
-
-                        //We flush the buffer
-                        while (wifi.readable())
-                            wifi.getc();
-
-                        break;
-                    }
-                }
-            }
-        }
-        DBG("check: %s\r\n", checking.c_str());
-
-        attach_rx(true);
-        return result;
-    }
-
-    //the user wants the result from the command (ACK == NULL, res != NULL)
-    if ( res != NULL) {
-        int i = 0;
-        Timer timeout;
-        timeout.start();
-        tmr.reset();
-        while (1) {
-            if (timeout.read() > 2) {
-                if (i == 0) {
-                    res = NULL;
-                    break;
-                }
-                res[i] = '\0';
-                DBG("user str 1: %s\r\n", res);
-
-                break;
-            } else {
-                if (tmr.read_ms() > 300) {
-                    res[i] = '\0';
-                    DBG("user str: %s\r\n", res);
-
-                    break;
-                }
-                if (wifi.readable()) {
-                    tmr.start();
-                    read = wifi.getc();
-
-                    // we drop \r and \n
-                    if ( read != '\r' && read != '\n') {
-                        res[i++] = read;
-                    }
-                }
-            }
-        }
-        DBG("user str: %s\r\n", res);
-    }
-
-    //We flush the buffer
-    while (wifi.readable())
-        wifi.getc();
-
-    attach_rx(true);
-    DBG("result: %d\r\n", result)
-    return result;
-}
\ No newline at end of file
diff -r 0bcec6272784 -r fb15c35d1e28 Wifly/Wifly.h
--- a/Wifly/Wifly.h	Thu Dec 20 15:08:58 2012 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,238 +0,0 @@
-/* Copyright (C) 2012 mbed.org, MIT License
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
- * and associated documentation files (the "Software"), to deal in the Software without restriction,
- * including without limitation the rights to use, copy, modify, merge, publish, distribute,
- * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all copies or
- * substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
- * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
- * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- *
- * @section DESCRIPTION
- *
- * Wifly RN131-C, wifi module
- *
- * Datasheet:
- *
- * http://dlnmh9ip6v2uc.cloudfront.net/datasheets/Wireless/WiFi/WiFly-RN-UM.pdf
- */
-
-#ifndef WIFLY_H
-#define WIFLY_H
-
-#include "mbed.h"
-#include "CBuffer.h"
-
-#define DEFAULT_WAIT_RESP_TIMEOUT 500
-
-enum Security {
-    NONE = 0,
-    WEP_128 = 1,
-    WPA = 3
-};
-
-enum Protocol {
-    UDP = (1 << 0),
-    TCP = (1 << 1)
-};
-
-class Wifly
-{
-
-public:
-    /*
-    * Constructor
-    *
-    * @param tx mbed pin to use for tx line of Serial interface
-    * @param rx mbed pin to use for rx line of Serial interface
-    * @param reset reset pin of the wifi module ()
-    * @param tcp_status connection status pin of the wifi module (GPIO 6)
-    * @param ssid ssid of the network
-    * @param phrase WEP or WPA key
-    * @param sec Security type (NONE, WEP_128 or WPA)
-    */
-    Wifly(  PinName tx, PinName rx, PinName reset, PinName tcp_status, const char * ssid, const char * phrase, Security sec);
-
-    /*
-    * Connect the wifi module to the ssid contained in the constructor.
-    *
-    * @return true if connected, false otherwise
-    */
-    bool join();
-
-    /*
-    * Disconnect the wifly module from the access point
-    *
-    * @ returns true if successful
-    */
-    bool disconnect();
-
-    /*
-    * Open a tcp connection with the specified host on the specified port
-    *
-    * @param host host (can be either an ip address or a name. If a name is provided, a dns request will be established)
-    * @param port port
-    * @ returns true if successful
-    */
-    bool connect(const char * host, int port);
-
-
-    /*
-    * Set the protocol (UDP or TCP)
-    *
-    * @param p protocol
-    * @ returns true if successful
-    */
-    bool setProtocol(Protocol p);
-
-    /*
-    * Reset the wifi module
-    */
-    void reset();
-    
-    /*
-    * Reboot the wifi module
-    */
-    bool reboot();
-
-    /*
-    * Check if characters are available
-    *
-    * @return number of available characters
-    */
-    int readable();
-
-    /*
-    * Check if characters are available
-    *
-    * @return number of available characters
-    */
-    int writeable();
-
-    /*
-    * Check if a tcp link is active
-    *
-    * @returns true if successful
-    */
-    bool is_connected();
-
-    /*
-    * Read a character
-    *
-    * @return the character read
-    */
-    char getc();
-
-    /*
-    * Flush the buffer
-    */
-    void flush();
-
-    /*
-    * Write a character
-    *
-    * @param the character which will be written
-    */
-    int putc(char c);
-
-
-    /*
-    * To enter in command mode (we can configure the module)
-    *
-    * @return true if successful, false otherwise
-    */
-    bool cmdMode();
-
-    /*
-    * To exit the command mode
-    *
-    * @return true if successful, false otherwise
-    */
-    bool exit();
-
-    /*
-    * Close a tcp connection
-    *
-    * @ returns true if successful
-    */
-    bool close();
-
-    /*
-    * Send a string to the wifi module by serial port. This function desactivates the user interrupt handler when a character is received to analyze the response from the wifi module.
-    * Useful to send a command to the module and wait a response.
-    *
-    *
-    * @param str string to be sent
-    * @param len string length
-    * @param ACK string which must be acknowledge by the wifi module. If ACK == NULL, no string has to be acknoledged. (default: "NO")
-    * @param res this field will contain the response from the wifi module, result of a command sent. This field is available only if ACK = "NO" AND res != NULL (default: NULL)
-    *
-    * @return true if ACK has been found in the response from the wifi module. False otherwise or if there is no response in 5s.
-    */
-    int send(const char * str, int len, const char * ACK = NULL, char * res = NULL, int timeout = DEFAULT_WAIT_RESP_TIMEOUT);
-
-    /*
-    * Send a command to the wify module. Check if the module is in command mode. If not enter in command mode
-    *
-    * @param str string to be sent
-    * @param ACK string which must be acknowledge by the wifi module. If ACK == NULL, no string has to be acknoledged. (default: "NO")
-    * @param res this field will contain the response from the wifi module, result of a command sent. This field is available only if ACK = "NO" AND res != NULL (default: NULL)
-    *
-    * @returns true if successful
-    */
-    bool sendCommand(const char * cmd, const char * ack = NULL, char * res = NULL, int timeout = DEFAULT_WAIT_RESP_TIMEOUT);
-    
-    /*
-    * Return true if the module is using dhcp
-    *
-    * @returns true if the module is using dhcp
-    */
-    bool isDHCP() {
-        return state.dhcp;
-    }
-
-    bool gethostbyname(const char * host, char * ip);
-
-    static Wifly * getInstance() {
-        return inst;
-    };
-
-protected:
-    Serial wifi;
-    DigitalOut reset_pin;
-    DigitalIn tcp_status;
-    char phrase[30];
-    char ssid[30];
-    const char * ip;
-    const char * netmask;
-    const char * gateway;
-    int channel;
-    CircBuffer<char> buf_wifly;
-
-    static Wifly * inst;
-
-    void attach_rx(bool null);
-    void handler_rx(void);
-
-
-    typedef struct STATE {
-        bool associated;
-        bool tcp;
-        bool dhcp;
-        Security sec;
-        Protocol proto;
-        bool cmd_mode;
-    } State;
-
-    State state;
-    char * getStringSecurity();
-};
-
-#endif
\ No newline at end of file
diff -r 0bcec6272784 -r fb15c35d1e28 WiflyInterface.cpp
--- a/WiflyInterface.cpp	Thu Dec 20 15:08:58 2012 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,62 +0,0 @@
-#include "WiflyInterface.h"
-
-WiflyInterface::WiflyInterface( PinName tx, PinName rx, PinName reset, PinName tcp_status,
-                                const char * ssid, const char * phrase, Security sec) :
-    Wifly(tx, rx, reset, tcp_status, ssid, phrase, sec)
-{
-    ip_set = false;
-}
-
-int WiflyInterface::init()
-{
-    state.dhcp = true;
-    reset();
-    return 0;
-}
-
-int WiflyInterface::init(const char* ip, const char* mask, const char* gateway)
-{
-    state.dhcp = false;
-    this->ip = ip;
-    strcpy(ip_string, ip);
-    ip_set = true;
-    this->netmask = mask;
-    this->gateway = gateway;
-    reset();
-
-    return 0;
-}
-
-int WiflyInterface::connect()
-{
-    return join();
-}
-
-int WiflyInterface::disconnect()
-{
-    return Wifly::disconnect();
-}
-
-char * WiflyInterface::getIPAddress()
-{
-    char * match = 0;
-    if (!ip_set) {
-        if (!sendCommand("get ip a\r", NULL, ip_string))
-            return NULL;
-        exit();
-        flush();
-        match = strstr(ip_string, "<");
-        if (match != NULL) {
-            *match = '\0';
-        }
-        if (strlen(ip_string) < 6) {
-            match = strstr(ip_string, ">");
-            if (match != NULL) {
-                int len = strlen(match + 1);
-                memcpy(ip_string, match + 1, len);
-            }
-        }
-        ip_set = true;
-    }
-    return ip_string;
-}
\ No newline at end of file
diff -r 0bcec6272784 -r fb15c35d1e28 WiflyInterface.h
--- a/WiflyInterface.h	Thu Dec 20 15:08:58 2012 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,86 +0,0 @@
-/* WiflyInterface.h */
-/* Copyright (C) 2012 mbed.org, MIT License
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
- * and associated documentation files (the "Software"), to deal in the Software without restriction,
- * including without limitation the rights to use, copy, modify, merge, publish, distribute,
- * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all copies or
- * substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
- * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
- * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- */
- 
-#ifndef WIFLYINTERFACE_H_
-#define WIFLYINTERFACE_H_
-
-#include "Wifly.h"
-
- /** Interface using Wifly to connect to an IP-based network
- *
- */
-class WiflyInterface: public Wifly {
-public:
-
-    /**
-    * Constructor
-    *
-    * \param tx mbed pin to use for tx line of Serial interface
-    * \param rx mbed pin to use for rx line of Serial interface
-    * \param reset reset pin of the wifi module ()
-    * \param tcp_status connection status pin of the wifi module (GPIO 6)
-    * \param ssid ssid of the network
-    * \param phrase WEP or WPA key
-    * \param sec Security type (NONE, WEP_128 or WPA)
-    */
-  WiflyInterface(PinName tx, PinName rx, PinName reset, PinName tcp_status, const char * ssid, const char * phrase, Security sec = NONE);
-
-  /** Initialize the interface with DHCP.
-  * Initialize the interface and configure it to use DHCP (no connection at this point).
-  * \return 0 on success, a negative number on failure
-  */
-  int init(); //With DHCP
-
-  /** Initialize the interface with a static IP address.
-  * Initialize the interface and configure it with the following static configuration (no connection at this point).
-  * \param ip the IP address to use
-  * \param mask the IP address mask
-  * \param gateway the gateway to use
-  * \return 0 on success, a negative number on failure
-  */
-  int init(const char* ip, const char* mask, const char* gateway);
-
-  /** Connect
-  * Bring the interface up, start DHCP if needed.
-  * \return 0 on success, a negative number on failure
-  */
-  int connect();
-  
-  /** Disconnect
-  * Bring the interface down
-  * \return 0 on success, a negative number on failure
-  */
-  int disconnect();
-  
-  /** Get IP address
-  *
-  * @ returns ip address
-  */
-  char* getIPAddress();
-  
-private:
-    char ip_string[20];
-    bool ip_set;
-};
-
-#include "TCPSocketConnection.h"
-#include "TCPSocketServer.h"
-#include "UDPSocket.h"
-
-#endif /* WIFLYINTERFACE_H_ */
diff -r 0bcec6272784 -r fb15c35d1e28 pico_string.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pico_string.h	Tue Aug 27 12:50:11 2013 +0000
@@ -0,0 +1,46 @@
+// pico_tring.h 2013/8/27
+#pragma once
+class pico_string {
+public:
+    pico_string(){
+        _len = 0;
+        _buf = (char*)malloc(1);
+        if (_buf) {
+            _buf[0] = '\0';
+        }
+    }
+    ~pico_string() {
+        if (_buf) {
+            free(_buf);
+        }
+    }
+    bool empty() {
+        return _len == 0;
+    }
+    void append(const char* s, int len) {
+        if (_buf == NULL) {
+            return;
+        }
+        char* p = (char*)malloc(_len+len+1);
+        if (p == NULL) {
+            return;
+        }
+        memcpy(p, _buf, _len);
+        memcpy(p+_len, s, len);
+        p[_len+len] = '\0';
+        free(_buf);
+        _buf = p;
+    }
+    void append(const char* s) {
+        append(s, strlen(s));
+    }
+    char* c_str() {
+        if (_buf) {
+            return _buf;
+        }
+        return "";
+    }
+private:
+    char* _buf;
+    int _len;
+};