http://mbed.org/users/gsfan/notebook/gainspan_wifi/ porterd from: http://electronics.trev.id.au/2012/02/07/gainspan-wifi-library-for-chipkit-and-arduino/

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
gsfan
Date:
Sun May 27 03:19:54 2012 +0000
Commit message:

Changed in this revision

Wirefree/WifiClient.cpp Show annotated file Show diff for this revision Revisions of this file
Wirefree/WifiClient.h Show annotated file Show diff for this revision Revisions of this file
Wirefree/WifiServer.cpp Show annotated file Show diff for this revision Revisions of this file
Wirefree/WifiServer.h Show annotated file Show diff for this revision Revisions of this file
Wirefree/Wirefree.cpp Show annotated file Show diff for this revision Revisions of this file
Wirefree/Wirefree.h Show annotated file Show diff for this revision Revisions of this file
Wirefree/dbg.h Show annotated file Show diff for this revision Revisions of this file
Wirefree/gs.cpp Show annotated file Show diff for this revision Revisions of this file
Wirefree/gs.h Show annotated file Show diff for this revision Revisions of this file
Wirefree/host.h Show annotated file Show diff for this revision Revisions of this file
Wirefree/ipaddr.cpp Show annotated file Show diff for this revision Revisions of this file
Wirefree/ipaddr.h Show annotated file Show diff for this revision Revisions of this file
Wirefree/socket.cpp Show annotated file Show diff for this revision Revisions of this file
Wirefree/socket.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Wirefree/WifiClient.cpp	Sun May 27 03:19:54 2012 +0000
@@ -0,0 +1,140 @@
+/*
+ * Gainspan Wifi library for mbed
+ * Modified for mbed, 2012 gsfan.
+ *
+
+WifiClient.cpp - network client class 
+
+Copyright (C) 2011 DIYSandbox LLC
+
+This program is free software; you can redistribute it and/or
+modify it under the terms of the GNU General Public License
+as published by the Free Software Foundation; either version 2
+of the License, or (at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+*/
+
+#include "socket.h"
+#include "WifiClient.h"
+
+WifiClient::WifiClient(GSSocket &gss) : _gss(gss) {
+    _srcport = 10240;
+}
+
+WifiClient::WifiClient(GSSocket &gss, int sock) : _sock(sock), _gss(gss) {
+    _srcport = 10240;
+}
+
+WifiClient::WifiClient(GSSocket &gss, Host &host) : _sock(MAX_SOCK_NUM), _gss(gss) {
+    _srcport = 10240;
+    _host = host;
+}
+
+
+int WifiClient::connect() {
+    if (_sock != MAX_SOCK_NUM)
+        return 0;
+
+    for (int i = 0; i < MAX_SOCK_NUM; i++) {
+        if (_gss.readSocketStatus(i) == SOCK_STATUS::CLOSED) {
+            _sock = i;
+            break;
+        }
+    }
+    
+    if (_sock == MAX_SOCK_NUM)
+        return 0;
+    
+    _srcport++;
+    if (_srcport == 0) _srcport = 10240;
+    _gss.socket(_sock, IPPROTO::TCP, _srcport, 0);
+    
+    if (!_gss.connect(_sock, _host)) {
+        _sock = MAX_SOCK_NUM;
+        return 0;
+    }
+    
+    while (status() != SOCK_STATUS::ESTABLISHED) {
+        wait_ms(1);
+        if (status() == SOCK_STATUS::CLOSED) {
+            _sock = MAX_SOCK_NUM;
+            return 0;
+        }
+    }
+
+    return 1;
+}
+
+int WifiClient::status() {
+  if (_sock == MAX_SOCK_NUM) return SOCK_STATUS::CLOSED;
+  return _gss.readSocketStatus(_sock);
+}
+
+int WifiClient::_putc (int b) {
+    if (_sock != MAX_SOCK_NUM)
+        _gss.send(_sock, (uint8_t*)&b, 1);
+    return 0;
+}
+
+int WifiClient::available() {
+    if (_sock != MAX_SOCK_NUM) {
+        if (_gss.isDataOnSock(_sock)) {
+            return 1;
+        } else {
+            _gss.process();
+            return 0;
+        }
+    }
+
+    return 0;
+}
+
+int WifiClient::_getc() {
+  char b = 0;
+
+  if (!available() || (_gss.recv(_sock, (uint8_t*)b, 1) != 1))
+    return -1;
+
+  return b;
+}
+
+int WifiClient::peek() {
+  return 0;
+}
+
+void WifiClient::flush() {
+    while (available())
+        _getc();
+}
+
+WifiClient::operator bool() {
+  return _sock != MAX_SOCK_NUM;
+}
+
+int WifiClient::connected() {
+  if (_sock == MAX_SOCK_NUM) return 0;
+
+  //uint8_t s = status();
+  //return !(s == SOCK_STATUS::LISTEN || s == SOCK_STATUS::CLOSED ||
+  //  (s == SOCK_STATUS::CLOSE_WAIT && !available()));
+
+  return (status() == SOCK_STATUS::ESTABLISHED);
+}
+
+void WifiClient::stop() {
+  if (_sock == MAX_SOCK_NUM)
+    return;
+
+  _gss.disconnect(_sock);
+
+  //EthernetClass::_server_port[_sock] = 0;
+  _sock = MAX_SOCK_NUM;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Wirefree/WifiClient.h	Sun May 27 03:19:54 2012 +0000
@@ -0,0 +1,66 @@
+/*
+ * Gainspan Wifi library for mbed
+ * Modified for mbed, 2012 gsfan.
+ *
+
+WifiClient.h - network client class 
+
+Copyright (C) 2011 DIYSandbox LLC
+
+This program is free software; you can redistribute it and/or
+modify it under the terms of the GNU General Public License
+as published by the Free Software Foundation; either version 2
+of the License, or (at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+*/
+
+#ifndef _wifi_client_h_
+#define _wifi_client_h_
+
+#include "mbed.h"
+#include "socket.h"
+
+class WifiClient : public Stream {
+
+public:
+  WifiClient(GSSocket &);
+  WifiClient(GSSocket &, int);
+  WifiClient(GSSocket &, Host &);
+
+  int status();
+  int connect();
+
+  //uint8_t connect();
+  virtual int _putc (int);
+  virtual int available();
+  virtual int _getc ();
+  virtual int peek();
+  virtual void flush();
+  operator bool();
+  int connected();
+  void stop();
+  void startDataTx();
+  void stopDataTx();
+#if 0
+  uint8_t operator==(int);
+  uint8_t operator!=(int);
+#endif
+
+  friend class WifiServer;
+
+private:
+  int _srcport;
+  int _sock;
+  Host _host;
+  GSSocket _gss;
+};
+
+#endif // _wifi_client_h_
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Wirefree/Wirefree.cpp	Sun May 27 03:19:54 2012 +0000
@@ -0,0 +1,106 @@
+/*
+ * Gainspan Wifi library for mbed
+ * Modified for mbed, 2012 gsfan.
+ *
+
+Wirefree.cpp - interface class to talk with DIYSandbox Arduino devices 
+
+Copyright (C) 2011 DIYSandbox LLC
+
+This program is free software; you can redistribute it and/or
+modify it under the terms of the GNU General Public License
+as published by the Free Software Foundation; either version 2
+of the License, or (at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+*/
+
+#include "dbg.h"
+#include "Wirefree.h"
+#include "gs.h"
+#include <string.h>
+
+void Wirefree::begin(WIFI_PROFILE* w_prof, void (*rxDataHndlr)(char *data, int len))
+{
+    for (int i = 0; i < MAX_SOCK_NUM; i ++) {
+        _server_port[i] = 0;
+    }
+
+    DBG("Wirefree.cpp - begin(WIFI_PROFILE* w_prof, void (*rxDataHndlr)(String data))\r\n");
+    DBG("                  call begin(w_prof, rxDataHndlr, NORMAL_MODE);\r\n");
+    begin(w_prof, rxDataHndlr, NORMAL_MODE);
+    DBG("Wirefree.cpp - begin(WIFI_PROFILE* w_prof, void (*rxDataHndlr)(String data)) - DONE\r\n");
+}
+
+void Wirefree::begin(WIFI_PROFILE* w_prof, void (*rxDataHndlr)(char *data, int len), int m)
+{
+    DBG("Wirefree.cpp - begin(WIFI_PROFILE* w_prof, void (*rxDataHndlr)(String data), uint8_t mode)\r\n");
+
+    mode = m;
+    
+    // initialize device
+    if (!init(rxDataHndlr)) {
+        DBG("error: init\r\n");
+        return;
+    }
+    DBG("      initialize device done\r\n");
+
+    // configure params
+    configure((GS_PROFILE*)w_prof);
+    DBG("      configure params done\r\n");
+
+    // initiate wireless connection
+    while (!GSClass::connect());
+    DBG("      connect done\r\n");
+    
+    DBG("Wirefree.cpp - begin(WIFI_PROFILE* w_prof, void (*rxDataHndlr)(String data), uint8_t mode) - DONE\r\n");
+}
+
+void Wirefree::process()
+{
+    GSClass::process();
+}
+
+int Wirefree::socketOpen(Host &host)
+{
+    // get IP address from URL
+    if (!dns_lookup(host)) {
+        return 0;
+    }
+
+    // open socket connection
+    if (!connect_socket(host)) {
+        return 0;
+    }
+
+    return 1;
+}
+
+int Wirefree::connected()
+{
+    return GSClass::connected();
+}
+
+void Wirefree::sendDeviceID()
+{
+    char buf[100] = "ID:";
+
+    strcat(buf, (char*)get_dev_id());
+//    send_data(buf, strlen(buf));
+}
+
+void Wirefree::sendResponse(char *data)
+{
+    char buf[100];
+
+    sprintf(buf, "TIME:%s:%d", data, 0);
+//    send_data(buf, strlen(buf));
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Wirefree/Wirefree.h	Sun May 27 03:19:54 2012 +0000
@@ -0,0 +1,69 @@
+/*
+ * Gainspan Wifi library for mbed
+ * Modified for mbed, 2012 gsfan.
+ *
+
+Wirefree.h - interface class to talk with DIYSandbox Arduino devices 
+
+Copyright (C) 2011 DIYSandbox LLC
+
+Porting for chipKIT boards Copyright (c) 2012 http://electronics.trev.id.au
+
+This program is free software; you can redistribute it and/or
+modify it under the terms of the GNU General Public License
+as published by the Free Software Foundation; either version 2
+of the License, or (at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+*/
+
+#ifndef _wirefree_h_
+#define _wirefree_h_
+
+#include "socket.h"
+#include "WifiClient.h"
+#include "WifiServer.h"
+
+#define NORMAL_MODE     0
+#define ADHOC_MODE      1
+#define AP_MODE         2
+
+typedef struct _WIFI_PROFILE {
+    char ssid[32];
+    char security_key[32];
+    IpAddr ip;
+    IpAddr subnet;
+    IpAddr gateway;
+} WIFI_PROFILE;
+
+class Wirefree : public GSSocket {
+public:
+  Wirefree(PinName p_tx, PinName p_rx) : GSSocket(p_tx, p_rx) {};
+  Wirefree(PinName p_tx, PinName p_rx, PinName p_cts, PinName p_rts) : GSSocket(p_tx, p_rx, p_cts, p_rts) {};
+
+  void begin(WIFI_PROFILE*, void (*rxDataHndlr)(char *data, int len));
+  void begin(WIFI_PROFILE*, void (*rxDataHndlr)(char *data, int len), int m);
+
+  void process();
+  int connected();
+  int socketOpen(Host &host);
+
+  void sendDeviceID();
+  void sendResponse(char *data);
+
+  friend class WifiClient;
+  friend class WifiServer;
+  
+private:
+  int _server_port[MAX_SOCK_NUM];
+
+};
+
+#endif // _wirefree_h_
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Wirefree/dbg.h	Sun May 27 03:19:54 2012 +0000
@@ -0,0 +1,7 @@
+#define DEBUG
+
+#ifdef DEBUG 
+#define DBG(...) printf("" __VA_ARGS__) 
+#else 
+#define DBG(...) 
+#endif 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Wirefree/gs.cpp	Sun May 27 03:19:54 2012 +0000
@@ -0,0 +1,850 @@
+/*
+ * Gainspan Wifi library for mbed
+ * Modified for mbed, 2012 gsfan.
+ *
+
+gs.cpp - HAL driver to talk with Gainspan GS1011 WiFi module
+
+Copyright (C) 2011 DIYSandbox LLC
+
+Porting for chipKIT boards Copyright (c) 2012 http://electronics.trev.id.au
+
+This program is free software; you can redistribute it and/or
+modify it under the terms of the GNU General Public License
+as published by the Free Software Foundation; either version 2
+of the License, or (at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+*/
+
+#include "dbg.h"
+#include "mbed.h"
+#include "ipaddr.h"
+#include "host.h"
+#include "gs.h"
+
+#include <stdio.h>
+#include <stdint.h>
+
+struct _cmd_tbl {
+    const char *cmd_str;
+} cmd_tbl[] = {
+        {"ATE0"},
+        {"AT+WWPA="},
+        {"AT+WA="},
+        {"AT+NDHCP=0"},
+        {"AT+NDHCP=1"},
+        {"AT+WD"},
+        {"AT+NSTCP=80"},
+        {"AT+NCTCP="},
+        {"AT+NMAC=?"},
+        {"AT+DNSLOOKUP="},
+        {"AT+NCLOSE="},
+        {"AT+NSET="},
+        {"AT+WM=2"},
+        {"AT+DHCPSRVR=1"},
+};
+
+int hex_to_int(char c)
+{
+    int val = 0;
+
+    if (c >= '0' && c <= '9') {
+        val = c - '0';
+    }
+    else if (c >= 'A' && c <= 'F') {
+        val = c - 'A' + 10;
+    }
+    else if (c >= 'a' && c <= 'f') {
+        val = c - 'a' + 10;
+    }
+
+    return val;
+}
+
+char int_to_hex(int c)
+{
+    char val = '0';
+
+    if (c >= 0 && c <= 9) {
+        val = c + '0';
+    }
+    else if (c >= 10 && c <= 15) {
+        val = c + 'A' - 10;
+    }
+
+    return val;
+}
+
+GSClass::GSClass(PinName p_tx, PinName p_rx): _gs(p_tx, p_rx) {
+}
+
+GSClass::GSClass(PinName p_tx, PinName p_rx, PinName p_cts, PinName p_rts): _gs(p_tx, p_rx) {
+#if defined(TARGET_LPC1768) || defined(TARGET_LPC2368)
+    if (p_cts == p12) { // CTS input (P0_17)
+        LPC_UART1->MCR |= (1<<7); // CTSEN
+        LPC_PINCON->PINSEL1 &= ~(3 << 2);
+        LPC_PINCON->PINSEL1 |= (1 << 2); // UART CTS
+    }
+    if (p_rts == P0_22) { // RTS output (P0_22)
+        LPC_UART1->MCR |= (1<<6); // RTSEN
+        LPC_PINCON->PINSEL1 &= ~(3 << 12);
+        LPC_PINCON->PINSEL1 |= (1 << 12); // UART RTS
+        _gs.attach(this, &GSClass::isr_recv, Serial::RxIrq);
+        _rts = true;
+    } else {
+        _rts = false;
+    }
+#elif defined(TARGET_LPC11U24)
+    if (p_cts == p21) { // CTS input (P0_7)
+        LPC_UART->MCR = (1<<7); // CTSEN
+        LPC_IOCON->PIO0_7 &= ~0x07;
+        LPC_IOCON->PIO0_7 |= 0x01; // UART CTS
+    }
+    if (p_rts == p22) { // RTS output (P0_17)
+        LPC_UART->MCR = (1<<6); // RTSEN
+        LPC_IOCON->PIO0_17 &= ~0x07;
+        LPC_IOCON->PIO0_17 |= 0x01; // UART RTS
+        _gs.attach(this, &GSClass::isr_recv, Serial::RxIrq);
+        _rts = true;
+    } else {
+        _rts = false;
+    }
+#endif
+}
+
+int GSClass::init(void (*rx_data_hndlr)(char *data, int len))
+{
+    _gs.baud(9600);
+    wait_ms(1000);
+
+    flush();
+    _gs.printf("\r\n");
+    wait_ms(1000);
+
+    dev_mode = DEV_OP_MODE_COMMAND;
+    connection_state = DEV_CONN_ST_DISCONNECTED;
+    dataOnSock = 255;
+
+    rx_data_handler = rx_data_hndlr;
+
+    for (int i = 0; i < 4; i++) {
+        sock_table[i].cid = 0;
+        sock_table[i].port = 0;
+        sock_table[i].protocol = 0;
+        sock_table[i].status = 0;
+    }
+
+    // disable echo
+    if (!send_cmd_w_resp(CMD_DISABLE_ECHO)) {
+        return 0;
+    }
+
+    // get device ID
+    if (!send_cmd_w_resp(CMD_GET_MAC_ADDR)) {
+        return 0;
+    }
+
+    return 1;
+}
+
+int GSClass::send_cmd(int cmd)
+{
+    flush();
+
+    DBG("send_cmd %d\r\n", cmd);
+    switch(cmd) {
+    case CMD_SET_UART_115200:
+    case CMD_DISABLE_ECHO:
+    case CMD_DISABLE_DHCP:
+    case CMD_DISCONNECT:
+    case CMD_ENABLE_DHCP:
+    case CMD_LISTEN:
+    case CMD_GET_MAC_ADDR:
+    case CMD_WIRELESS_MODE:
+    case CMD_ENABLE_DHCPSVR:
+    {
+        _gs.printf("%s\r\n", cmd_tbl[cmd].cmd_str);
+        break;
+    }
+    case CMD_SET_WPA_PSK:
+    {
+        _gs.printf("%s%s\r\n", cmd_tbl[cmd].cmd_str, security_key);
+        break;
+    }
+    case CMD_SET_SSID:
+    {
+        if (mode == 0) {
+            _gs.printf("%s%s\r\n", cmd_tbl[cmd].cmd_str, ssid);
+        }
+        else if (mode == 2) {
+            _gs.printf("%s%s,,11\r\n", cmd_tbl[cmd].cmd_str, ssid);
+        }
+        break;
+    }
+    case CMD_TCP_CONN:
+    {
+        _gs.printf("%s%d.%d.%d.%d,%d\r\n", cmd_tbl[cmd].cmd_str, ip[0], ip[1], ip[2], ip[3], port);
+        break;
+    }
+    case CMD_NETWORK_SET:
+    {
+        _gs.printf("%s%d.%d.%d.%d,", cmd_tbl[cmd].cmd_str, ip[0], ip[1], ip[2], ip[3]);
+        _gs.printf("%d.%d.%d.%d,", subnet[0], subnet[1], subnet[2], subnet[3]);
+        _gs.printf("%d.%d.%d.%d\r\n", gateway[0], gateway[1], gateway[2], gateway[3]);
+        break;
+    }
+    case CMD_DNS_LOOKUP:
+    {
+        _gs.printf("%s%s\r\n", cmd_tbl[cmd].cmd_str, dns_url_ip.getName());
+        break;
+    }
+    case CMD_CLOSE_CONN:
+    {
+        if (sock_table[socket_num].status != SOCK_STATUS::CLOSED) {
+            _gs.printf("%s%d\r\n", cmd_tbl[cmd].cmd_str, sock_table[socket_num].cid);
+        } else {
+            return 0;
+        }
+        break;
+    }
+    default:
+        break;
+    }
+
+    return 1;
+}
+
+int GSClass::parse_resp(int cmd)
+{
+    int resp_done = 0;
+    int ret = 0;
+    char buf[256];
+    int len;
+
+    while (!resp_done) {
+        len = readline(buf, sizeof(buf));
+
+        switch(cmd) {
+        case CMD_SET_UART_115200:
+        case CMD_DISABLE_ECHO:
+        case CMD_DISABLE_DHCP:
+        case CMD_DISCONNECT:
+        case CMD_SET_WPA_PSK:
+        case CMD_SET_SSID:
+        case CMD_ENABLE_DHCP:
+        case CMD_NETWORK_SET:
+        case CMD_WIRELESS_MODE:
+        case CMD_ENABLE_DHCPSVR:
+        {
+            if (strcmp(buf, "OK") == 0) {
+                /* got OK */
+                ret = 1;
+                resp_done = 1;
+            } else if (strstr(buf, "ERROR")) {
+                /* got ERROR */
+                ret = 0;
+                resp_done = 1;
+            }
+            break;
+        }
+        case CMD_LISTEN:
+        {
+            if (strstr(buf, "CONNECT")) {
+                /* got CONNECT */
+                serv_cid = hex_to_int(buf[8]);
+                sock_table[socket_num].cid = hex_to_int(buf[8]);
+                sock_table[socket_num].status = SOCK_STATUS::LISTEN;
+                
+            } else if (strcmp(buf, "OK") == 0) {
+                /* got OK */
+                ret = 1;
+                resp_done = 1;
+            } else if (strstr(buf, "ERROR")) {
+                /* got ERROR */
+                serv_cid = INVALID_CID;
+                sock_table[socket_num].cid = 0;
+                sock_table[socket_num].status = SOCK_STATUS::CLOSED;
+                ret = 0;
+                resp_done = 1;
+            }
+            break;
+        }
+        case CMD_TCP_CONN:
+        {
+            if (strstr(buf, "CONNECT")) {
+                /* got CONNECT */
+                client_cid = hex_to_int(buf[8]);
+                sock_table[socket_num].cid = hex_to_int(buf[8]);
+                sock_table[socket_num].status = SOCK_STATUS::ESTABLISHED;
+            } else if (strcmp(buf, "OK") == 0) {
+                /* got OK */
+                ret = 1;
+                resp_done = 1;
+            } else if (strstr(buf, "ERROR")) {
+                /* got ERROR */
+                client_cid = INVALID_CID;
+                sock_table[socket_num].cid = 0;
+                sock_table[socket_num].status = SOCK_STATUS::CLOSED;
+                ret = 0;
+                resp_done = 1;
+            }
+            break;
+        }
+        case CMD_GET_MAC_ADDR:
+        {
+            if (strstr(buf, "00")) {
+                /* got MAC addr */
+                int mac1, mac2, mac3, mac4, mac5, mac6;
+                sscanf(buf, "%x:%x:%x:%x:%x:%x", &mac1, &mac2, &mac3, &mac4, &mac5, &mac6);
+                dev_id[0] = mac1;
+                dev_id[1] = mac2;
+                dev_id[2] = mac3;
+                dev_id[3] = mac4;
+                dev_id[4] = mac5;
+                dev_id[5] = mac6;
+            } else if (strcmp(buf, "OK") == 0) {
+                /* got OK */
+                ret = 1;
+                resp_done = 1;
+            } else if (strstr(buf, "ERROR")) {
+                /* got ERROR */
+                for (int i = 0; i < 6; i ++) dev_id[i] = 0xff;
+                ret = 0;
+                resp_done = 1;
+            }
+            break;
+        }
+        case CMD_DNS_LOOKUP:
+        {
+            if (strstr(buf, "IP:")) {
+                /* got IP address */
+                int ip1, ip2, ip3, ip4;
+                sscanf(&buf[3], "%d.%d.%d.%d", &ip1, &ip2, &ip3, &ip4);
+                dns_url_ip.setIp(IpAddr(ip1, ip2, ip3, ip4));
+            } else if (strcmp(buf, "OK") == 0) {
+                /* got OK */
+                ret = 1;
+                resp_done = 1;
+            } else if (strstr(buf, "ERROR")) {
+                /* got ERROR */
+                ret = 0;
+                resp_done = 1;
+            }
+            break;
+        }
+        case CMD_CLOSE_CONN:
+        {
+            if (strcmp(buf, "OK") == 0) {
+                /* got OK */
+                ret = 1;
+                resp_done = 1;
+
+                /* clean up socket */
+                sock_table[socket_num].status = 0;
+                sock_table[socket_num].cid = 0;
+                sock_table[socket_num].port = 0;
+                sock_table[socket_num].protocol = 0;
+                
+                dev_mode = DEV_OP_MODE_COMMAND;
+                
+                /* clear flag */
+                dataOnSock = 255;
+            } else if (strstr(buf, "ERROR")) {
+                /* got ERROR */
+                ret = 0;
+                resp_done = 1;
+            }
+            break;
+        }
+        default:
+            break;
+        }
+    }
+
+    return ret;
+}
+
+int GSClass::send_cmd_w_resp(int cmd)
+{
+    if (send_cmd(cmd)) {
+        return parse_resp(cmd);
+    } else {
+        return 0;
+    }
+}
+
+void GSClass::configure(GS_PROFILE *prof)
+{
+    // configure params
+    strcpy(ssid, prof->ssid);
+    strcpy(security_key, prof->security_key);
+    local_ip     = prof->ip;
+    subnet       = prof->subnet;
+    gateway      = prof->gateway;
+}
+
+int GSClass::connect()
+{
+
+    if (!send_cmd_w_resp(CMD_DISCONNECT)) {
+        return 0;
+    }
+
+    if (!send_cmd_w_resp(CMD_DISABLE_DHCP)) {
+        return 0;
+    }
+
+    if (mode == 0) {
+        if (!send_cmd_w_resp(CMD_SET_WPA_PSK)) {
+            return 0;
+        }
+
+        if (!send_cmd_w_resp(CMD_SET_SSID)) {
+            return 0;
+        }
+
+        if (local_ip.isNull()) {
+            if (!send_cmd_w_resp(CMD_ENABLE_DHCP)) {
+                return 0;
+            }
+        } else {
+            if (!send_cmd_w_resp(CMD_NETWORK_SET)) {
+                return 0;
+            }
+        }
+
+    } else if (mode == 2) {
+        if (!send_cmd_w_resp(CMD_NETWORK_SET)) {
+                    return 0;
+                }
+        if (!send_cmd_w_resp(CMD_WIRELESS_MODE)) {
+                        return 0;
+                }
+        if (!send_cmd_w_resp(CMD_SET_SSID)) {
+                        return 0;
+                }
+        if (!send_cmd_w_resp(CMD_ENABLE_DHCPSVR)) {
+            return 0;
+        }
+        
+    } 
+
+    connection_state = DEV_CONN_ST_CONNECTED;
+
+    return 1;
+}
+
+int GSClass::connected()
+{
+    return connection_state;
+}
+
+int GSClass::readline(char *buf, int size)
+{
+    int len = 0;
+    char inByte;
+
+    bool endDetected = false;
+
+    while (!endDetected)
+    {
+//        if (_gs.readable()) {
+        if ((! _rts && _gs.readable()) || (_rts && bufreadable())) {
+            // valid data in HW UART buffer, so check if it's \r or \n
+            // if so, throw away
+            // if strBuf length greater than 0, then this is a true end of line,
+            // so break out
+//            inByte = _gs.getc();
+            if (_rts) {
+                inByte = getbuf();
+            } else {
+                inByte = _gs.getc();
+            }
+            DBG("%c", inByte);
+
+            if ((inByte == '\r') || (inByte == '\n'))
+            {
+                // throw away
+                if ((len > 0) && (inByte == '\n'))
+                {
+                    endDetected = true;
+                }
+            }
+            else
+            {
+                buf[len] = inByte;
+                len ++;
+                if (len >= size) break;
+            }
+        }
+    }
+    DBG("\r\n");
+
+    buf[len] = 0;
+    return len;
+}
+
+int GSClass::readData(SOCKET s, uint8_t* buf, int len)
+{
+    int dataLen = 0;
+    uint8_t tmp1, tmp2;
+
+//    if (!_gs.readable())
+    if (!((! _rts && _gs.readable()) || (_rts && bufreadable())))
+        return 0;
+
+    while(dataLen < len) {
+//        if (_gs.readable()) {
+//            tmp1 = _gs.getc();
+        if ((! _rts && _gs.readable()) || (_rts && bufreadable())) {
+            if (_rts) {
+                tmp1 = getbuf();
+            } else {
+                tmp1 = _gs.getc();
+            }
+//            DBG("%02x ", tmp1);
+//            DBG("%c", tmp1);
+            if (tmp1 == 0x1b) {
+                // escape seq
+
+                /* read in escape sequence */
+                while(1) {
+//                    if (_gs.readable()) {
+//                        tmp2 = _gs.getc();
+                    if ((! _rts && _gs.readable()) || (_rts && bufreadable())) {
+                        if (_rts) {
+                            tmp2 = getbuf();
+                        } else {
+                            tmp2 = _gs.getc();
+                        }
+//                        DBG("%02x ", tmp2);
+                        break;
+                    }
+                }
+                
+                if (tmp2 == 0x45) {
+                    /* data end, switch to command mode */
+                    dev_mode = DEV_OP_MODE_COMMAND;
+                    /* clear flag */
+                    dataOnSock = 255;
+                    break;
+                } else {
+                    if (dataLen < (len-2)) {
+                        buf[dataLen++] = tmp1;
+                        buf[dataLen++] = tmp2;
+                    } else {
+                        buf[dataLen++] = tmp1;
+
+                        /* FIXME : throw away second byte ? */
+                    }
+                }
+            } else {
+                // data
+                buf[dataLen] = tmp1;
+                dataLen++;
+            }
+        }
+    }
+//    DBG(" (%d)\r\n", dataLen);
+
+//    buf[dataLen] = 0;
+    return dataLen;
+}
+
+int GSClass::writeData(SOCKET s, const uint8_t*  buf, int len)
+{    
+    if ((len == 0) || (buf[0] == '\r')){
+    } else {
+        _gs.putc(0x1b);    // data start
+        _gs.putc(0x53);
+        _gs.putc(int_to_hex(client_cid));
+        if (len == 1){
+            if (buf[0] != '\r' && buf[0] != '\n'){ 
+                _gs.putc(buf[0]);           // data to send
+            } else if (buf[0] == '\n') {
+                _gs.printf("\n\r");           // new line
+            } 
+        } else {
+            for (int i = 0; i < len; i ++) {
+                _gs.putc(buf[i]);
+            }
+        }
+
+        _gs.putc(0x1b);    // data end
+        _gs.putc(0x45);
+    }
+    wait_ms(10);
+
+    return 1;
+}
+
+void GSClass::process()
+{
+    char buf[256];
+    int len = 0;
+    char inByte;
+    int processDone = 0;
+
+//    if (!_gs.readable())
+    if (!((! _rts && _gs.readable()) && (_rts && bufreadable())))
+        return;
+
+    while (!processDone) {
+        if (dev_mode == DEV_OP_MODE_COMMAND) {
+            while (1) {
+//                if (_gs.readable()) {
+//                    inByte = _gs.getc();
+                if ((! _rts && _gs.readable()) || (_rts && bufreadable())) {
+                    if (_rts) {
+                        inByte = getbuf();
+                    } else {
+                        inByte = _gs.getc();
+                    }
+
+                    if (inByte == 0x1b) {
+                        // escape seq
+
+                        // switch mode
+                        dev_mode = DEV_OP_MODE_DATA;
+                        break;
+                    } else {
+                        // command string
+                        if ((inByte == '\r') || (inByte == '\n')) {
+                            // throw away
+                            if ((len > 0) && (inByte == '\n'))
+                            {
+                                // parse command
+                                parse_cmd(buf, len);
+                                processDone = 1;
+                                break;
+                            }
+                        }
+                        else
+                        {
+                            buf[len] = inByte;
+                            len ++;
+                        }
+                    }
+                }
+            }
+        } else if (dev_mode == DEV_OP_MODE_DATA) {
+            /* data mode */
+            while(1) {
+                //digitalWrite(5, LOW);
+//                if (_gs.readable()) {
+//                    inByte = _gs.getc();
+                if ((! _rts && _gs.readable()) || (_rts && bufreadable())) {
+                    if (_rts) {
+                        inByte = getbuf();
+                    } else {
+                        inByte = _gs.getc();
+                    }
+
+                    if (inByte == 0x53) {
+                        /* data start, switch to data RX mode */
+                        dev_mode = DEV_OP_MODE_DATA_RX;
+                        /* read in CID */
+                        while(1) {
+//                            if (_gs.readable()) {
+//                                inByte = _gs.getc();
+                            if ((! _rts && _gs.readable()) || (_rts && bufreadable())) {
+                                if (_rts) {
+                                    inByte = getbuf();
+                                } else {
+                                    inByte = _gs.getc();
+                                }
+                                
+                                break;
+                            }
+                        }
+
+                        // find socket from CID
+                        for (SOCKET new_sock = 0; new_sock < 4; new_sock++) {
+                            if (sock_table[new_sock].cid == hex_to_int(inByte)) {
+                                dataOnSock = new_sock;
+                                break;
+                            }
+                        }
+
+                        break;
+                    } else if (inByte == 0x45) {
+                        /* data end, switch to command mode */
+                        dev_mode = DEV_OP_MODE_COMMAND;
+                        processDone = 1;
+                        break;
+                    } else if (inByte == 0x4f) {
+                        /* data mode ok */
+                        tx_done = 1;
+                        dev_mode = DEV_OP_MODE_COMMAND;
+                        processDone = 1;
+                        break;
+                    } else if (inByte == 0x46) {
+                        /* TX failed */
+                        tx_done = 1;
+                        dev_mode = DEV_OP_MODE_COMMAND;
+                        processDone = 1;
+                        break;
+                    } else {
+                        /* unknown */
+                        dev_mode = DEV_OP_MODE_COMMAND;
+                        processDone = 1;
+                        break;
+                    }
+                }
+            }
+        } else if (dev_mode ==  DEV_OP_MODE_DATA_RX) {
+            //digitalWrite(6, LOW);
+            processDone = 1;
+        }
+    }
+}
+
+void GSClass::parse_cmd(char *buf, int len)
+{
+    if (strstr(buf, "CONNECT")) {
+        /* got CONNECT */
+
+        if (serv_cid == hex_to_int(buf[8])) {
+            /* client connected */
+            client_cid = hex_to_int(buf[10]);
+        }
+
+        for (int sock = 0; sock < 4; sock++) {
+            if ((sock_table[sock].status == SOCK_STATUS::LISTEN) &&
+                (sock_table[sock].cid == hex_to_int(buf[8])))
+            {
+                for (int new_sock = 0; new_sock < 4; new_sock++) {
+                    if (sock_table[new_sock].status == SOCK_STATUS::CLOSED) {
+                        sock_table[new_sock].cid = hex_to_int(buf[10]);
+                        sock_table[new_sock].port = sock_table[sock].port;
+                        sock_table[new_sock].protocol = sock_table[sock].protocol;
+                        sock_table[new_sock].status = SOCK_STATUS::ESTABLISHED;
+                        break;
+                    }
+                }
+            }
+        }
+
+    } else if (strstr(buf, "DISCONNECT")) {
+        /* got disconnect */
+        //digitalWrite(6, LOW);
+        for (int sock = 0; sock < 4; sock++) {
+            if ((sock_table[sock].status == SOCK_STATUS::ESTABLISHED) &&
+                (sock_table[sock].cid == hex_to_int(buf[11])))
+            {
+                sock_table[sock].cid = 0;
+                sock_table[sock].port = 0;
+                sock_table[sock].protocol = 0;
+                sock_table[sock].status = SOCK_STATUS::CLOSED;
+                break;
+            }
+        }
+        // FIXME : need to handle socket disconnection
+    } else if (strcmp(buf, "Disassociation Event")) {
+        /* disconnected from AP */
+        connection_state = DEV_CONN_ST_DISCONNECTED;
+    }
+}
+
+void GSClass::parse_data(char *buf, int len)
+{
+    rx_data_handler(buf, len);
+}
+
+int GSClass::connect_socket(Host &host)
+{
+    ip = host.getIp();
+    port = host.getPort();
+
+    if (!send_cmd_w_resp(CMD_TCP_CONN)) {
+        return 0;
+    }
+
+    return 1;
+}
+
+int GSClass::dns_lookup(Host &url)
+{
+    dns_url_ip.setName(url.getName());
+
+    if (!send_cmd_w_resp(CMD_DNS_LOOKUP)) {
+        return 0;
+    }
+
+    url.setIp(dns_url_ip.getIp());
+    return 1;
+}
+
+uint8_t *GSClass::get_dev_id()
+{
+    return dev_id;
+}
+
+void GSClass::configSocket(SOCKET s, int protocol, int port)
+{
+    sock_table[s].protocol = protocol;
+    sock_table[s].port = port;
+    sock_table[s].status = SOCK_STATUS::INIT;
+}
+
+void GSClass::execSocketCmd(SOCKET s, int cmd)
+{
+    socket_num = s;
+
+    if (!send_cmd_w_resp(cmd)) {
+    }
+}
+
+int GSClass::readSocketStatus(SOCKET s)
+{
+    return sock_table[s].status;
+}
+
+int GSClass::isDataOnSock(SOCKET s)
+{
+    return (s == dataOnSock);
+}
+
+void GSClass::flush()
+{
+    // arduino-1.0 repurposed the Serial.flush() command
+    // to wait for outgoing data to be transmitted, not to
+    // clear the buffer
+    // since we need to clear the buffer, need to create this
+    // workaround
+    _rxaddr_w = _rxaddr_r = 0;
+/*
+    while (_gs.readable())
+    {
+        _gs.getc();
+    }
+*/
+}
+
+void GSClass::isr_recv () {
+    _rxbuf[_rxaddr_w] = _gs.getc();
+    DBG("%02x ", _rxbuf[_rxaddr_w]);
+    _rxaddr_w = (_rxaddr_w + 1) % MAX_RXBUF_SIZE;
+}
+
+int GSClass::getbuf () {
+    int r;
+//    if (_rxaddr_w == _rxaddr_r) return 0;
+    __disable_irq();
+    r = _rxbuf[_rxaddr_r];
+    _rxaddr_r = (_rxaddr_r + 1) % MAX_RXBUF_SIZE;
+    __enable_irq();
+    return r;
+}
+
+int GSClass::bufreadable () {
+    return _rxaddr_w != _rxaddr_r;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Wirefree/gs.h	Sun May 27 03:19:54 2012 +0000
@@ -0,0 +1,171 @@
+/*
+ * Gainspan Wifi library for mbed
+ * Modified for mbed, 2012 gsfan.
+ *
+
+gs.cpp - HAL driver to talk with Gainspan GS1011 WiFi module
+
+Copyright (C) 2011 DIYSandbox LLC
+
+Porting for chipKIT boards Copyright (c) 2012 http://electronics.trev.id.au
+
+This program is free software; you can redistribute it and/or
+modify it under the terms of the GNU General Public License
+as published by the Free Software Foundation; either version 2
+of the License, or (at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+*/
+
+#ifndef    _gs_h_
+#define    _gs_h_
+
+#include "mbed.h"
+#include "ipaddr.h"
+#include "host.h"
+#include <stdint.h>
+
+typedef int SOCKET;
+
+class SOCK_STATUS {
+public:
+  static const uint8_t CLOSED      = 0x00;
+  static const uint8_t INIT        = 0x01;
+  static const uint8_t LISTEN      = 0x02;
+  static const uint8_t ESTABLISHED = 0x03;
+  static const uint8_t CLOSE_WAIT  = 0x04;
+};
+
+class IPPROTO {
+public:
+  static const uint8_t TCP  = 6;
+};
+
+// command identifiers
+// config
+#define CMD_DISABLE_ECHO 0
+#define CMD_SET_UART_115200 14
+// wifi
+#define CMD_SET_WPA_PSK  1
+#define CMD_SET_SSID     2
+#define CMD_DISCONNECT   5
+#define CMD_GET_MAC_ADDR 8
+//network
+#define CMD_DISABLE_DHCP 3
+#define CMD_ENABLE_DHCP  4
+#define CMD_LISTEN       6
+#define CMD_TCP_CONN     7
+#define CMD_DNS_LOOKUP   9
+#define CMD_CLOSE_CONN   10
+#define CMD_NETWORK_SET  11
+#define CMD_WIRELESS_MODE 12
+#define CMD_ENABLE_DHCPSVR 13
+
+// device operation modes
+#define DEV_OP_MODE_COMMAND 0
+#define DEV_OP_MODE_DATA    1
+#define DEV_OP_MODE_DATA_RX 2
+
+// device wireless connection state
+#define DEV_CONN_ST_DISCONNECTED 0
+#define DEV_CONN_ST_CONNECTED    1
+
+// connection ID
+#define INVALID_CID 255
+
+#define SSIZE 2048 // Max Tx buffer siz
+
+#define MAX_RXBUF_SIZE 32
+
+// wireless connection params
+typedef struct _GS_PROFILE {
+    char ssid[32];
+    char security_key[32];
+    IpAddr ip;
+    IpAddr subnet;
+    IpAddr gateway;
+} GS_PROFILE;
+
+typedef struct _SOCK_TABLE {
+    int status;
+    int protocol;
+    int port;
+    int cid;
+} SOCK_TABLE;
+
+class GSClass {
+public:
+    GSClass(PinName p_tx, PinName p_rx);
+    GSClass(PinName p_tx, PinName p_rx, PinName p_cts, PinName p_rts);
+    
+    int mode;
+    int init(void (*rx_data_handler)(char *data, int len));
+    void configure(GS_PROFILE* prof);
+    int connect();
+    int connected();
+    void process();
+    int connect_socket(Host &host);
+    int dns_lookup(Host &url);
+    void send_data(char *data, int len);
+    void esc_seq_start();
+    void esc_seq_stop();
+    uint8_t *get_dev_id();
+
+    void configSocket(SOCKET s, int protocol, int port);
+    void execSocketCmd(SOCKET s, int cmd);
+    int readSocketStatus(SOCKET s);
+    int isDataOnSock(SOCKET s);
+    int readData(SOCKET s, uint8_t* buf, int len);
+    int writeData(SOCKET s, const uint8_t* buf, int len);
+    
+//    static const int SSIZE = 2048; // Max Tx buffer siz
+
+    void isr_recv ();
+    int getbuf ();
+    int bufreadable ();
+
+private:
+    char security_key[32];
+    char ssid[32];
+    IpAddr local_ip;
+    IpAddr subnet;
+    IpAddr gateway;
+    int serv_cid;
+    int client_cid;
+    int dev_mode;
+    IpAddr ip;
+    int port;
+    int connection_state;
+    uint8_t dev_id[6];
+    Host dns_url_ip;
+    int tx_done;
+
+    SOCK_TABLE sock_table[4];
+    int socket_num;
+    SOCKET dataOnSock;
+
+    void (*rx_data_handler)(char *data, int len);
+
+    int readline(char *buf, int size);
+    int send_cmd(int cmd);
+    int parse_resp(int cmd);
+    int send_cmd_w_resp(int cmd);
+    void parse_cmd(char *buf, int len);
+    void parse_data(char *buf, int len);
+
+    void flush();
+
+    Serial _gs;
+    bool _rts;
+    char _rxbuf[MAX_RXBUF_SIZE];
+    volatile int _rxaddr_w, _rxaddr_r;
+};
+
+#endif // _gs_h_
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Wirefree/host.h	Sun May 27 03:19:54 2012 +0000
@@ -0,0 +1,109 @@
+
+/*
+Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
+ 
+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 HOST_H
+#define HOST_H
+
+#include "ipaddr.h"
+#include <string.h>
+
+///Host information container
+/**
+This class is a container for data relative to a connection:
+- IP Address
+- Port number
+- Host Name
+*/
+class Host 
+{
+public:
+  ///Initiliazes host with null values
+  Host() : m_ip(0,0,0,0), m_port(0), m_name(NULL)
+  {
+    
+  }
+  
+  ///Initializes host
+  Host(const IpAddr& ip, const int& port, const char* name="" ) : m_ip(ip), m_port(port), m_name(NULL)
+  {
+    setName(name); 
+  }
+  
+  ~Host()
+  {
+    if(m_name)
+    {
+      delete[] m_name;
+    }
+  }
+  
+  ///Returns IP address
+  const IpAddr& getIp() const
+  {
+    return m_ip;
+  }
+  
+  ///Returns port number
+  const int& getPort() const
+  {
+    return m_port;
+  }
+  
+  ///Returns host name
+  const char* getName() const
+  {
+    return m_name;
+  }
+  
+  ///Sets IP address
+  void setIp(const IpAddr& ip)
+  {
+    m_ip = ip;
+  }
+  
+  ///Sets port number
+  void setPort(int port)
+  {
+    m_port = port;
+  }
+  
+  ///Sets host name
+  void setName(const char* name)
+  {
+    if(m_name)
+      delete[] m_name;
+    int len = strlen(name);
+    if(len)
+    {
+      m_name = new char[len+1];
+      strcpy(m_name, name);
+    }
+  }
+  
+private:
+  IpAddr m_ip;
+  int m_port;
+  char* m_name;
+};
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Wirefree/ipaddr.cpp	Sun May 27 03:19:54 2012 +0000
@@ -0,0 +1,104 @@
+
+/*
+Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
+ 
+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 "ipaddr.h"
+
+//#include "netCfg.h"
+#if NET_LWIP_STACK
+#include "lwip/ip_addr.h"
+#endif
+
+
+#if NET_LWIP_STACK
+IpAddr::IpAddr(ip_addr_t* pIp)
+{
+  *((uint32_t*)m_ip) = pIp->addr;
+}
+#endif
+
+///Initializes IP address with provided values
+IpAddr::IpAddr(uint8_t ip0, uint8_t ip1, uint8_t ip2, uint8_t ip3)
+{
+  //We are in LE
+  m_ip[0] = ip0;
+  m_ip[1] = ip1;
+  m_ip[2] = ip2;
+  m_ip[3] = ip3;
+}
+
+///Initializes IP address with null values 
+IpAddr::IpAddr()
+{
+  m_ip[0] = 0;
+  m_ip[1] = 0;
+  m_ip[2] = 0;
+  m_ip[3] = 0;
+}
+
+
+#if NET_LWIP_STACK
+ip_addr_t IpAddr::getStruct() const
+{
+  ip_addr_t ip_struct;
+  ip_struct.addr = *((uint32_t*)m_ip);
+  return ip_struct;
+}
+#endif
+
+uint8_t IpAddr::operator[](unsigned int i) const
+{
+  uint8_t null = 0;
+  if( i > 3 )
+    return null;
+  return m_ip[i];
+}
+
+bool IpAddr::isEq(const IpAddr& b) const
+{
+  return (*((uint32_t*)m_ip) == *((uint32_t*)(b.m_ip)));
+}
+
+bool IpAddr::operator==(const IpAddr& b) const
+{
+  return isEq(b);
+}
+ 
+bool IpAddr::operator!=(const IpAddr& b) const
+{
+  return !(operator==(b));
+}
+
+bool IpAddr::isNull() const
+{
+  return (*((uint32_t*)m_ip) == 0);
+}
+
+bool IpAddr::isBroadcast() const
+{
+  return (*((uint32_t*)m_ip) == 0xFFFFFFFF);
+}
+
+bool IpAddr::isMulticast() const
+{
+  return ((m_ip[0] & 0xF0) == 0xE0);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Wirefree/ipaddr.h	Sun May 27 03:19:54 2012 +0000
@@ -0,0 +1,98 @@
+
+/*
+Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
+ 
+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 IPADDR_H
+#define IPADDR_H
+
+//#include "netCfg.h"
+#if NET_LWIP_STACK
+typedef struct ip_addr ip_addr_t;
+#endif
+
+#include "stdint.h"
+
+///IP Address container
+/**
+This class is a container for an IPv4 address.
+*/
+class IpAddr //Basically a C++ frontend to ip_addr_t
+{
+public:
+  #if NET_LWIP_STACK
+  IpAddr(ip_addr_t* pIp);
+  #endif
+  
+  ///Initializes IP address with provided values
+  IpAddr(uint8_t ip0, uint8_t ip1, uint8_t ip2, uint8_t ip3);
+  
+  ///Initializes IP address with null values 
+  IpAddr();
+
+  #if NET_LWIP_STACK
+  ip_addr_t getStruct() const;
+  #endif
+  
+  ///Returns IP address byte #
+  uint8_t operator[](unsigned int i) const;
+  
+  ///Compares too addresses
+  /**
+  @return true if the two addresses are equal
+  */
+  bool isEq(const IpAddr& b) const;
+  
+  ///Compares too addresses
+  /**
+  @return true if the two addresses are equal
+  */
+  bool operator==(const IpAddr& b) const;
+ 
+  ///Compares too addresses
+  /**
+  @return true if the two addresses are different
+  */ 
+  bool operator!=(const IpAddr& b) const;
+  
+  ///Checks whether the address is null
+  /**
+  @return true if the address is null
+  */
+  bool isNull() const;
+  
+  ///Checks whether the address is a broadcast address
+  /**
+  @return true if the address is a broadcast address
+  */
+  bool isBroadcast() const;
+
+  ///Checks whether the address is a multicast address
+  /**
+  @return true if the address is a multicast address
+  */
+  bool isMulticast() const;
+  
+private:
+  uint8_t m_ip[4];
+};
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Wirefree/socket.cpp	Sun May 27 03:19:54 2012 +0000
@@ -0,0 +1,133 @@
+/*
+ * Gainspan Wifi library for mbed
+ * Modified for mbed, 2012 gsfan.
+ *
+
+socket.cpp - network sockett class 
+
+Copyright (C) 2011 DIYSandbox LLC
+
+This program is free software; you can redistribute it and/or
+modify it under the terms of the GNU General Public License
+as published by the Free Software Foundation; either version 2
+of the License, or (at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+*/
+
+#include "dbg.h"
+#include "mbed.h"
+#include "socket.h"
+
+/**
+ * @brief    This Socket function initialize the channel in perticular mode, and set the port and wait for W5100 done it.
+ * @return     1 for success else 0.
+ */
+int GSSocket::socket(SOCKET s, int protocol, int port, int flag)
+{
+  if ((protocol == IPPROTO::TCP))
+  {
+    close(s);
+    if (port != 0) {
+      configSocket(s, protocol, port);
+    } 
+    else {
+      local_port++; // if don't set the source port, set local_port number.
+      configSocket(s, protocol, local_port);
+    }
+
+    //W5100.execCmdSn(s, Sock_OPEN);
+    
+    return 1;
+  }
+
+  return 0;
+}
+
+
+/**
+ * @brief    This function close the socket and parameter is "s" which represent the socket number
+ */
+void GSSocket::close(SOCKET s)
+{
+  execSocketCmd(s, CMD_CLOSE_CONN);
+}
+
+
+/**
+ * @brief    This function established  the connection for the channel in passive (server) mode. This function waits for the request from the peer.
+ * @return    1 for success else 0.
+ */
+int GSSocket::listen(SOCKET s)
+{
+  if (readSocketStatus(s) != SOCK_STATUS::INIT)
+    return 0;
+  execSocketCmd(s, CMD_LISTEN);
+  return 1;
+}
+
+int GSSocket::recv(SOCKET s, uint8_t *buf, int len)
+{
+  int ret=0;
+
+  if ( (len > 0) && isDataOnSock(s))
+  {
+    ret = readData(s, buf, len);
+  }
+
+  return ret;
+}
+
+void GSSocket::disconnect(SOCKET s)
+{
+  execSocketCmd(s, CMD_CLOSE_CONN);
+}
+
+int GSSocket::send(SOCKET s, const uint8_t *buf, int len)
+{
+//    int status=0;
+    int ret=0;
+//    int freesize=0;
+    
+    if (len > SSIZE) {
+        ret = SSIZE; // check size not to exceed MAX size.
+    } else { 
+        ret = len;
+    }
+    ret = writeData(s, buf, len);
+    
+    return ret;
+}
+
+/**
+ * @brief    This function established  the connection for the channel in Active (client) mode. 
+ *         This function waits for the untill the connection is established.
+ *         
+ * @return    1 for success else 0.
+ */
+int GSSocket::connect(SOCKET s, Host &host)
+{
+    if (host.getIp().isNull()) {
+        DBG("dns_lookup %s\r\n", host.getName());
+        if (!dns_lookup(host)) {
+            wait_ms(50);
+            if (!dns_lookup(host)) {
+                DBG("error dns\r\n");
+                return 0;
+            }
+        }
+    }
+
+    if (host.getIp().isNull() || host.getPort() == 0)
+        return 0;
+    
+    // set destination IP
+    return connect_socket(host);    
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Wirefree/socket.h	Sun May 27 03:19:54 2012 +0000
@@ -0,0 +1,52 @@
+/*
+ * Gainspan Wifi library for mbed
+ * Modified for mbed, 2012 gsfan.
+ *
+
+socket.h - network socket class 
+
+Copyright (C) 2011 DIYSandbox LLC
+
+This program is free software; you can redistribute it and/or
+modify it under the terms of the GNU General Public License
+as published by the Free Software Foundation; either version 2
+of the License, or (at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+*/
+
+#ifndef    _socket_h_
+#define    _socket_h_
+
+#include "mbed.h"
+#include "gs.h"
+
+#define MAX_SOCK_NUM 4
+
+class GSSocket : public GSClass {
+public:
+    GSSocket(PinName p_tx, PinName p_rx) : GSClass(p_tx, p_rx) {};
+    GSSocket(PinName p_tx, PinName p_rx, PinName p_cts, PinName p_rts) : GSClass(p_tx, p_rx, p_cts, p_rts) {};
+
+    int socket(SOCKET s, int protocol, int port, int flag); // Opens a socket(TCP or UDP or IP_RAW mode)
+    void close(SOCKET s); // Close socket
+    int listen(SOCKET s);    // Establish TCP connection (Passive connection)
+    int recv(SOCKET s, uint8_t *buf, int len);    // Receive data (TCP)
+    void disconnect(SOCKET s);
+    int send(SOCKET s, const uint8_t *buf, int len); // Send data (TCP)
+    int connect(SOCKET s, Host &host); 
+
+private:
+    int local_port;
+
+};
+
+
+#endif // _socket_h_
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sun May 27 03:19:54 2012 +0000
@@ -0,0 +1,61 @@
+#include "mbed.h"
+#include "Wirefree.h"
+
+WIFI_PROFILE w_prof = {
+    "GSWIFI",
+    "PASSWORD",
+    IpAddr(0,0,0,0),
+    IpAddr(0,0,0,0),
+    IpAddr(0,0,0,0)
+};
+Host server = Host(IpAddr(0,0,0,0), 80, "mbed.org");
+
+Wirefree wireless(p13, p14);
+//Wirefree wireless(p13, p14, p12, P0_22); // TX, RX, CTS, RTS
+
+WifiClient client(wireless, server);
+
+Serial pc(USBTX, USBRX);
+DigitalOut myled(LED1);
+
+void parseRxData(char *data, int len)
+{
+}
+
+int main() {
+    char c;
+
+    pc.baud(115200);
+
+    wireless.begin(&w_prof, &parseRxData);
+
+    pc.printf("client connecting...\r\n");
+    if (client.connect()) {
+        myled = 1;
+        pc.printf("connection Success..\r\n");
+    
+        // Make a HTTP request:
+        client.printf("GET / HTTP/1.0\r\n\r\n");
+//        client.flush();
+    } else {
+        pc.printf("connection failed..\r\n");
+        return -1;
+    }
+
+    for (;;) {
+        if (client.available()) {
+            c = client.getc();
+            if (c >= 0x20 && c < 0x7f) {
+                pc.putc(c);
+            } else {
+                pc.printf(" %02x ", c);
+            }
+        }
+        if (!client.connected()) {
+            pc.printf("disconnecting.\r\n");
+            client.stop();
+            myled = 0;
+            break;
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Sun May 27 03:19:54 2012 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/737756e0b479