GSwifiInterface library (interface for GainSpan Wi-Fi GS1011 modules) Please see https://mbed.org/users/gsfan/notebook/GSwifiInterface/

Dependents:   GSwifiInterface_HelloWorld GSwifiInterface_HelloServo GSwifiInterface_UDPEchoServer GSwifiInterface_UDPEchoClient ... more

Fork of WiflyInterface by mbed official

GainSpan Wi-Fi library

The GS1011/GS2100 is an ultra low power 802.11b wireless module from GainSpan.

mbed RTOS supported.

/media/uploads/gsfan/gs_im_002.jpg /media/uploads/gsfan/gs1011m_2.jpg

ゲインスパン Wi-Fi モジュール ライブラリ

ゲインスパン社の低電力 Wi-Fiモジュール(無線LAN) GS1011/GS2100 シリーズ用のライブラリです。

mbed RTOS に対応しています。(mbed2.0)

Files at this revision

API Documentation at this revision

Comitter:
gsfan
Date:
Fri Nov 15 04:20:14 2013 +0000
Parent:
10:7d8134e7df01
Child:
12:057089026a20
Commit message:
fix api function, miss spell

Changed in this revision

GSwifi/CBuffer.h Show annotated file Show diff for this revision Revisions of this file
GSwifi/GSwifi.cpp Show annotated file Show diff for this revision Revisions of this file
GSwifi/GSwifi.h Show annotated file Show diff for this revision Revisions of this file
GSwifi/GSwifi_at.cpp Show annotated file Show diff for this revision Revisions of this file
GSwifi/GSwifi_conf.h Show annotated file Show diff for this revision Revisions of this file
GSwifi/GSwifi_hal.cpp Show annotated file Show diff for this revision Revisions of this file
GSwifi/GSwifi_http.cpp Show annotated file Show diff for this revision Revisions of this file
GSwifi/GSwifi_msg.cpp Show annotated file Show diff for this revision Revisions of this file
GSwifi/GSwifi_sock.cpp Show annotated file Show diff for this revision Revisions of this file
GSwifi/GSwifi_util.cpp Show annotated file Show diff for this revision Revisions of this file
GSwifiInterface.cpp 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/TCPSocketServer.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/GSwifi/CBuffer.h	Thu Oct 31 08:34:45 2013 +0000
+++ b/GSwifi/CBuffer.h	Fri Nov 15 04:20:14 2013 +0000
@@ -27,6 +27,8 @@
         read = 0;
         size = length + 1;
         buf = (T *)malloc(size * sizeof(T));
+        if (buf == NULL)
+            error("Can't allocate memory");
     };
 
     bool isFull() {
--- a/GSwifi/GSwifi.cpp	Thu Oct 31 08:34:45 2013 +0000
+++ b/GSwifi/GSwifi.cpp	Fri Nov 15 04:20:14 2013 +0000
@@ -1,4 +1,4 @@
-/* Copyright (C) 2012 mbed.org, MIT License
+/* Copyright (C) 2013 gsfan, 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,
@@ -15,9 +15,6 @@
  * 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.
  */
-/* Copyright (C) 2013 gsfan, MIT License
- *  port to the GainSpan Wi-FI module GS1011
- */
 
 #include "mbed.h"
 #include "GSwifi.h"
@@ -32,12 +29,15 @@
     _inst = this;
     memset(&_state, 0, sizeof(_state));
     memset(&_con, 0, sizeof(_con));
+    _state.initialized = false;
+    _state.status = STAT_READY;
     _state.cid = -1;
-    _state.acid = -1;
     _state.buf = new CircBuffer<char>(CFG_DATA_SIZE);
-#ifdef RTOS_H
+#ifdef CFG_ENABLE_RTOS
     for (int i = 0; i < 16; i ++) {
         _con[i].buf = new CircBuffer<char>(CFG_DATA_SIZE);
+        if (_con[i].buf == NULL)
+            error("CircBuffer failed");
     }
     _threadPoll = NULL;
 #endif
@@ -52,12 +52,13 @@
     wait_ms(100);
 }
 
-int GSwifi::join(Security sec, const char *ssid, const char *phrase)
+int GSwifi::join()
 {
+    int i;
     bool r = -1;
 
     _state.wm = WM_INFRASTRUCTURE;
-    setSsid(sec, ssid, phrase);
+    _state.initialized = true;
     if (!strlen(_state.name)) {
         strncpy(_state.name, CFG_DHCPNAME, sizeof(_state.name));
     }
@@ -72,21 +73,21 @@
     cmdWM(0); // infrastructure
     wait_ms(100);
 
-    switch (sec) {
+    switch (_state.sec) {
     case SEC_NONE:
     case SEC_OPEN:
     case SEC_WEP:
         cmdNDHCP(_state.dhcp, _state.name, 0);
         cmdWAUTH(_state.sec);
-        if (sec != SEC_NONE) {
+        if (_state.sec != SEC_NONE) {
             cmdWWEP(1, _state.pass);
             wait_ms(100);
         }
-        r = cmdWA(_state.ssid);
-        if (r) {
+        for (i = 0; i < CFG_TRYJOIN; i ++) {
+            r = cmdWA(_state.ssid);
+            if (!r) break;
             DBG("retry\r\n");
             wait_ms(1000);
-            r = cmdWA(_state.ssid);
         }
         break;
     case SEC_WPA_PSK:
@@ -95,11 +96,11 @@
         cmdWAUTH(0);
         cmdWPAPSK(_state.ssid, _state.pass);
         wait_ms(100);
-        r = cmdWA(_state.ssid);
-        if (r) {
+        for (i = 0; i < CFG_TRYJOIN; i ++) {
+            r = cmdWA(_state.ssid);
+            if (!r) break;
             DBG("retry\r\n");
             wait_ms(1000);
-            r = cmdWA(_state.ssid);
         }
         break;
     case SEC_WPA_ENT:
@@ -135,11 +136,12 @@
     }
 
     if (!r) {
+        // connected
         if (! _state.dhcp) {
             cmdDNSSET(_state.nameserver);
         }
         _state.associated = true;
-#ifdef RTOS_H
+#ifdef CFG_ENABLE_RTOS
         _threadPoll = new Thread(&threadPoll);
 //        _threadPoll = new Thread(&threadPoll, NULL, osPriorityLow);
 #endif
@@ -147,12 +149,12 @@
     return r;
 }
 
-int GSwifi::adhock (Security sec, const char *ssid, const char *phrase)
+int GSwifi::adhock ()
 {
     bool r = -1;
 
     _state.wm = WM_ADHOCK;
-    setSsid(sec, ssid, phrase);
+    _state.initialized = true;
     clearFlags();
     sendCommand(NULL, RES_NULL, 0);
     if (cmdE(false)) return -1;
@@ -169,12 +171,12 @@
         cmdNSET(_state.ip, _state.netmask, _state.ip);
     }
 
-    switch (sec) {
+    switch (_state.sec) {
     case SEC_NONE:
     case SEC_OPEN:
     case SEC_WEP:
         cmdWAUTH(_state.sec);
-        if (sec != SEC_NONE) {
+        if (_state.sec != SEC_NONE) {
             cmdWWEP(1, _state.pass);
             wait_ms(100);
         }
@@ -186,17 +188,18 @@
     }
 
     if (!r) {
+        // connected
         _state.associated = true;
     }
     return r;
 }
 
-int GSwifi::limitedap (Security sec, const char *ssid, const char *phrase)
+int GSwifi::limitedap ()
 {
     bool r = -1;
 
     _state.wm = WM_LIMITEDAP;
-    setSsid(sec, ssid, phrase);
+    _state.initialized = true;
     if (!strlen(_state.name)) {
         strncpy(_state.name, CFG_DNSNAME, sizeof(_state.name));
     }
@@ -219,12 +222,12 @@
     cmdDHCPSRVR(true);
     cmdDNS(true, _state.name);
 
-    switch (sec) {
+    switch (_state.sec) {
     case SEC_NONE:
     case SEC_OPEN:
     case SEC_WEP:
         cmdWAUTH(_state.sec);
-        if (sec != SEC_NONE) {
+        if (_state.sec != SEC_NONE) {
             cmdWWEP(1, _state.pass);
             wait_ms(100);
         }
@@ -236,6 +239,7 @@
     }
 
     if (!r) {
+        // connected
         _state.associated = true;
     }
     return r;
@@ -249,7 +253,7 @@
     if (!_state.associated)
         return 0;
 
-#ifdef RTOS_H
+#ifdef CFG_ENABLE_RTOS
     if (_threadPoll) {
         _threadPoll->terminate();
         delete _threadPoll;
@@ -276,15 +280,46 @@
 }
 
 void GSwifi::poll () {
+#ifndef CFG_ENABLE_RTOS
+    static int t = 0;
+
+    if (_state.wm == WM_INFRASTRUCTURE && (! isConnected()) && _state.ssid) {
+      if (t && time(NULL) > t) {
+        // Wi-Fi re-associate
+        if (_state.initialized) {
+            if (!cmdWA(_state.ssid)) {
+                _state.associated = true;
+                t = 0;
+            } else {
+                t = time(NULL) + CFG_RECONNECT;
+            }
+        } else {
+            if (join()) {
+                t = 0;
+            } else {
+                t = time(NULL) + CFG_RECONNECT;
+            }
+        }
+      }
+    } else {
+        if (t == 0)
+            t = time(NULL) + CFG_RECONNECT;
+    }
+#else
     if (_state.wm == WM_INFRASTRUCTURE && (! isConnected()) && _state.ssid) {
         // Wi-Fi re-associate
-        if (!cmdWA(_state.ssid)) {
-            _state.associated = true;
+        if (_state.initialized) {
+            if (!cmdWA(_state.ssid)) {
+                _state.associated = true;
+            }
+        } else {
+            join();
         }
     }
+#endif
 }
 
-#ifdef RTOS_H
+#ifdef CFG_ENABLE_RTOS
 void GSwifi::threadPoll (void const *args) {
     GSwifi * _wifi = GSwifi::getInstance();
     if (_wifi == NULL)
@@ -303,17 +338,52 @@
 }
 #endif
 
+int GSwifi::setAddress (const char *name) {
+    _state.dhcp = true;
+    strncpy(_state.name, name, sizeof(_state.name));
+    return 0;
+}
+
+int GSwifi::setAddress (const char *ip, const char *netmask, const char *gateway, const char *name) {
+    _state.dhcp = false;
+    strncpy(_state.ip, ip, sizeof(_state.ip));
+    strncpy(_state.netmask, netmask, sizeof(_state.netmask));
+    strncpy(_state.gateway, gateway, sizeof(_state.gateway));
+    strncpy(_state.name, name, sizeof(_state.name));
+    return 0;
+}
+
+int GSwifi::getAddress (char *ip, char *netmask, char *gateway) {
+    strcpy(ip, _state.ip);
+    strcpy(netmask, _state.netmask);
+    strcpy(gateway, _state.gateway);
+    return 0;
+}
+
 int GSwifi::setSsid (Security sec, const char *ssid, const char *phrase) {
     int i;
 
     _state.sec = sec;
+
     // change all ' ' in '$' in the ssid and the passphrase
     strncpy(_state.ssid, ssid, sizeof(_state.ssid));
     for (i = 0; i < strlen(_state.ssid); i++) {
         if (_state.ssid[i] == ' ')
             _state.ssid[i] = '$';
     }
-    strncpy(_state.pass, phrase, sizeof(_state.pass));
+
+    if (sec == SEC_WEP) {
+        if (strlen(phrase) == 5 || strlen(phrase) == 13) {
+            for (i = 0; i < strlen(phrase); i++) {
+                _state.pass[i * 2] = '0' + ((phrase[i] >> 4) & 0x0f);
+                _state.pass[i * 2 + 1] = '0' + (phrase[i + 1] & 0x0f);
+            }
+        } else {
+            strncpy(_state.pass, phrase, strlen(phrase));
+        }
+    } else {
+        strncpy(_state.pass, phrase, sizeof(_state.pass));
+    }
     for (i = 0; i < strlen(_state.pass); i++) {
         if (_state.pass[i] == ' ')
             _state.pass[i] = '$';
@@ -321,60 +391,3 @@
     return 0;
 }
 
-int GSwifi::standby (int msec) {
-    switch (_state.status) {
-    case STAT_READY:
-        cmdSTORENWCONN();
-        for (int i = 0; i < 16; i ++) {
-            _con[i].connected = false;
-        }
-        break;
-    case STAT_WAKEUP:
-        if (cmdE(false)) return -1;
-        if (_flow) {
-            cmdR(true);
-        }
-        break;
-    default:
-        return -1;
-    }
-
-    _state.status = STAT_STANDBY;
-    return cmdPSSTBY(msec, 0);
-}
-
-int GSwifi::deepSleep () {
-    if (_state.status != STAT_READY) return -1;
-    _state.status = STAT_DEEPSLEEP;
-    return cmdPSDPSLEEP(); // go deep sleep
-}
-
-int GSwifi::wakeup () {
-    if (_state.status == STAT_STANDBY) {
-        Timer timeout;
-        setAlarm(true);
-        timeout.start();
-        while (_state.status != STAT_WAKEUP && timeout.read() < CFG_TIMEOUT);
-        timeout.stop();
-        setAlarm(false);
-    }
-
-    switch (_state.status) {
-    case STAT_WAKEUP:
-        _state.status = STAT_READY;
-        if (cmdE(false)) return -1;
-        if (_flow) {
-            cmdR(true);
-        }
-        cmdBDATA(true);
-        int r = cmdRESTORENWCONN();
-        wait_ms(100);
-//        return cmdWRXACTIVE(true);
-        return r;
-    case STAT_DEEPSLEEP:
-        _state.status = STAT_READY;
-        return cmdAT();
-    }
-    return -1;
-}
-
--- a/GSwifi/GSwifi.h	Thu Oct 31 08:34:45 2013 +0000
+++ b/GSwifi/GSwifi.h	Fri Nov 15 04:20:14 2013 +0000
@@ -1,4 +1,4 @@
-/* Copyright (C) 2012 mbed.org, MIT License
+/* Copyright (C) 2013 gsfan, 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,
@@ -21,9 +21,6 @@
  *
  *   http://www.gainspan.com/modules
  */
-/* Copyright (C) 2013 gsfan, MIT License
- *  port to the GainSpan Wi-FI module GS1011
- */
 /** @file
  * @brief Gainspan wi-fi module library for mbed
  * GS1011MIC, GS1011MIP, GainSpan WiFi Breakout, etc.
@@ -32,35 +29,19 @@
 #ifndef GSwifi_H
 #define GSwifi_H
 
+#include "GSwifi_conf.h"
+
 #include "mbed.h"
-#include "rtos.h"
 #include "RawSerial.h"
 #include "CBuffer.h"
 #include <ctype.h>
 #include <stdlib.h>
 #include <string.h>
 
-//#define DEBUG
-
-#define DEFAULT_WAIT_RESP_TIMEOUT 500
-#define CFG_TIMEOUT 30000 // ms
-#define CFG_CMD_SIZE 100
-#if defined(TARGET_LPC1768) || defined(TARGET_LPC2368)
-#define CFG_DATA_SIZE 1024
-#elif defined(TARGET_LPC11U24)
-#define CFG_DATA_SIZE 128
-#elif defined(TARGET_LPC4088)
-#define CFG_DATA_SIZE 4096
+#ifdef CFG_ENABLE_RTOS
+#include "rtos.h"
 #endif
 
-
-//#define CFG_UART_DIRECT
-#define CFG_UART_BAUD 9600
-#define CFG_WREGDOMAIN 2 // 0:FCC, 1:ETSI, 2:TELEC
-#define CFG_DHCPNAME "mbed-gswifi"
-#define CFG_DNSNAME "setup.local"
-#define MAX_TRY_JOIN 3
-
 #define MSG_TABLE_NUM 15
 #define RES_TABLE_NUM 10
 
@@ -176,7 +157,7 @@
      * @param phrase WEP or WPA key
      * @return 0 if connected, -1 otherwise
      */
-    int join (Security sec, const char *ssid, const char *phrase);
+    int join ();
 
     /** Connect the wifi module to the adhock in the constructor.
      * @param sec Security type (NONE, WEP_128 or WPA)
@@ -184,7 +165,7 @@
      * @param phrase WEP or WPA key
      * @return 0 if connected, -1 otherwise
      */
-    int adhock (Security sec, const char *ssid, const char *phrase);
+    int adhock ();
 
     /** Connect the wifi module to the limited AP in the constructor.
      * @param sec Security type (NONE, WEP_128 or WPA)
@@ -192,7 +173,7 @@
      * @param phrase WEP or WPA key
      * @return 0 if connected, -1 otherwise
      */
-    int limitedap (Security sec, const char *ssid, const char *phrase);
+    int limitedap ();
 
     /** Disconnect the wifi module from the access point
      * @returns 0 if successful
@@ -204,9 +185,12 @@
      */
     bool isConnected();
 
-    int setSsid (Security sec, const char *ssid, const char *phrase);
+    void poll ();
 
-    void poll ();
+    int setAddress (const char *name = NULL);
+    int setAddress (const char *ip, const char *netmask, const char *gateway, const char *name = NULL);
+    int getAddress (char *ip, char *netmask, char *gateway);
+    int setSsid (Security sec, const char *ssid, const char *phrase);
 
     static GSwifi * getInstance() {
         return _inst;
@@ -348,13 +332,13 @@
      * @param cmd 
      * @returns 0 if successful
      */
-    int sendData(const char * data, int len, int timeout = DEFAULT_WAIT_RESP_TIMEOUT, const char * cmd = NULL);
+    int sendData(const char * data, int len, int timeout = CFG_TIMEOUT, const char * cmd = NULL);
 
 
 protected:
 
     static GSwifi * _inst;
-#ifdef RTOS_H
+#ifdef CFG_ENABLE_RTOS
     Thread *_threadPoll;
     Mutex _mutexUart;
 #endif
@@ -389,13 +373,14 @@
         bool dhcp;
         time_t time;
 
+        bool initialized;
         bool associated;
         volatile Mode mode;
         volatile Status status;
         bool escape;
         volatile bool ok, failure;
         volatile Response res;
-        int cid, acid;
+        int cid;
         int n;
         CircBuffer<char> *buf;
     } _state;
@@ -414,7 +399,7 @@
 
 
     // ----- GSwifi.cpp -----
-#ifdef RTOS_H
+#ifdef CFG_ENABLE_RTOS
     static void threadPoll (void const *args);
 #endif
 
--- a/GSwifi/GSwifi_at.cpp	Thu Oct 31 08:34:45 2013 +0000
+++ b/GSwifi/GSwifi_at.cpp	Fri Nov 15 04:20:14 2013 +0000
@@ -357,7 +357,7 @@
     } else {
         sprintf(cmd, "AT+PSDPSLEEP");
     }
-    return sendCommand(cmd);
+    return sendCommand(cmd, RES_NULL, 0);
 }
 
 int GSwifi::cmdPSSTBY (int n, int m) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/GSwifi/GSwifi_conf.h	Fri Nov 15 04:20:14 2013 +0000
@@ -0,0 +1,23 @@
+
+#define DEBUG
+
+#define CFG_ENABLE_RTOS
+//#define CFG_UART_DIRECT
+#define CFG_UART_BAUD 9600
+#define CFG_WREGDOMAIN 2 // 0:FCC, 1:ETSI, 2:TELEC
+#define CFG_DHCPNAME "mbed-gswifi"
+#define CFG_DNSNAME "setup.local"
+#define CFG_TRYJOIN 3
+#define CFG_RECONNECT 60 // sec
+
+#define DEFAULT_WAIT_RESP_TIMEOUT 500
+#define CFG_TIMEOUT 30000 // ms
+
+#define CFG_CMD_SIZE 100
+#if defined(TARGET_LPC1768) || defined(TARGET_LPC2368)
+#define CFG_DATA_SIZE 1024
+#elif defined(TARGET_LPC11U24)
+#define CFG_DATA_SIZE 128
+#elif defined(TARGET_LPC4088)
+#define CFG_DATA_SIZE 4096
+#endif
--- a/GSwifi/GSwifi_hal.cpp	Thu Oct 31 08:34:45 2013 +0000
+++ b/GSwifi/GSwifi_hal.cpp	Fri Nov 15 04:20:14 2013 +0000
@@ -107,7 +107,7 @@
         }
     }
 
-#ifdef RTOS_H
+#ifdef CFG_ENABLE_RTOS
     if (_mutexUart.lock(ms) != osOK) return -1;
 #endif
 
@@ -137,13 +137,14 @@
             }
         }
     }
+
     setRts(false);
     return 0;
 }
 
 void GSwifi::unlockUart () {
     setRts(true);
-#ifdef RTOS_H
+#ifdef CFG_ENABLE_RTOS
     _mutexUart.unlock();
 #endif
 }
--- a/GSwifi/GSwifi_http.cpp	Thu Oct 31 08:34:45 2013 +0000
+++ b/GSwifi/GSwifi_http.cpp	Fri Nov 15 04:20:14 2013 +0000
@@ -22,6 +22,8 @@
     char ip[17];
     int cid;
 
+    if (!isConnected() && _state.status == STAT_READY) return -1;
+
     if (getHostByName(host, ip)) return -1;
     if (! port) {
         if (ssl) {
@@ -45,6 +47,7 @@
         cmdHTTPCONFDEL(2);
     }
 
+    _state.cid = -1;
     if (cmdHTTPOPEN(ip, port, ssl)) return -1;
     if (_state.cid < 0) return -1;
     cid = _state.cid;
@@ -60,6 +63,8 @@
     char ip[17];
     int cid, len;
 
+    if (!isConnected() && _state.status == STAT_READY) return -1;
+
     if (getHostByName(host, ip)) return -1;
     if (! port) {
         if (ssl) {
@@ -85,6 +90,7 @@
         cmdHTTPCONFDEL(2);
     }
 
+    _state.cid = -1;
     if (cmdHTTPOPEN(ip, port, ssl)) return -1;
     if (_state.cid < 0) return -1;
     cid = _state.cid;
--- a/GSwifi/GSwifi_msg.cpp	Thu Oct 31 08:34:45 2013 +0000
+++ b/GSwifi/GSwifi_msg.cpp	Fri Nov 15 04:20:14 2013 +0000
@@ -18,13 +18,13 @@
 
 #include "GSwifi.h"
 
-#ifdef RTOS_H
+#ifdef CFG_ENABLE_RTOS
 #undef DBG
 #define DBG(x, ...)
 #endif
 
 void GSwifi::recvData (char c) {
-    static int sub, len, count;
+    static int cid, sub, len, count;
 
 #ifdef DEBUG_DUMP
     if (c < 0x20) {
@@ -111,7 +111,7 @@
         switch (sub) {
         case 0:
             // cid
-            _state.cid = x2i(c);
+            cid = x2i(c);
             sub ++;
             count = 0;
             if (_state.mode == MODE_DATA_RX) {
@@ -121,18 +121,18 @@
         case 1:
             // ip
             if ((c >= '0' && c <= '9') || c == '.') {
-                _con[_state.cid].ip[count] = c;
+                _con[cid].ip[count] = c;
                 count ++;
             } else {
-                _con[_state.cid].ip[count] = 0;
-                _con[_state.cid].port = 0;
+                _con[cid].ip[count] = 0;
+                _con[cid].port = 0;
                 sub ++;
             }
             break;
         case 2:
             // port
             if (c >= '0' && c <= '9') {
-                _con[_state.cid].port = (_con[_state.cid].port * 10) + (c - '0'); 
+                _con[cid].port = (_con[cid].port * 10) + (c - '0'); 
             } else {
                 sub ++;
                 count = 0;
@@ -142,13 +142,13 @@
             // data
             if (_state.escape) {
                 if (c == 'E') {
-                    DBG("recv ascii %d %d/a\r\n", _state.cid, count);
-                    _con[_state.cid].received = true;
+                    DBG("recv ascii %d %d/a\r\n", cid, count);
+                    _con[cid].received = true;
                     _state.mode = MODE_COMMAND;
                 } else {
-                    if (_con[_state.cid].buf != NULL) {
-                        _con[_state.cid].buf->queue(0x1b);
-                        _con[_state.cid].buf->queue(c);
+                    if (_con[cid].buf != NULL) {
+                        _con[cid].buf->queue(0x1b);
+                        _con[cid].buf->queue(c);
                     }
                     count += 2;
                 }
@@ -157,11 +157,11 @@
             if (c == 0x1b) {
                 _state.escape = true;
             } else {
-                if (_con[_state.cid].buf != NULL) {
-                    _con[_state.cid].buf->queue(c);
-                    if (_con[_state.cid].buf->available() > CFG_DATA_SIZE - 16) {
+                if (_con[cid].buf != NULL) {
+                    _con[cid].buf->queue(c);
+                    if (_con[cid].buf->available() > CFG_DATA_SIZE - 16) {
                         setRts(false);
-                        _con[_state.cid].received = true;
+                        _con[cid].received = true;
                     }
                 }
                 count ++;
@@ -176,7 +176,7 @@
         switch (sub) {
         case 0:
             // cid
-            _state.cid = x2i(c);
+            cid = x2i(c);
             sub ++;
             len = 0;
             count = 0;
@@ -187,18 +187,18 @@
         case 1:
             // ip
             if ((c >= '0' && c <= '9') || c == '.') {
-                _con[_state.cid].ip[count] = c;
+                _con[cid].ip[count] = c;
                 count ++;
             } else {
-                _con[_state.cid].ip[count] = 0;
-                _con[_state.cid].port = 0;
+                _con[cid].ip[count] = 0;
+                _con[cid].port = 0;
                 sub ++;
             }
             break;
         case 2:
             // port
             if (c >= '0' && c <= '9') {
-                _con[_state.cid].port = (_con[_state.cid].port * 10) + (c - '0'); 
+                _con[cid].port = (_con[cid].port * 10) + (c - '0'); 
             } else {
                 sub ++;
                 count = 0;
@@ -215,17 +215,17 @@
             break;
         default:
             // data
-            if (_con[_state.cid].buf != NULL) {
-                _con[_state.cid].buf->queue(c);
-                if (_con[_state.cid].buf->available() > CFG_DATA_SIZE - 16) {
+            if (_con[cid].buf != NULL) {
+                _con[cid].buf->queue(c);
+                if (_con[cid].buf->available() > CFG_DATA_SIZE - 16) {
                     setRts(false);
-                    _con[_state.cid].received = true;
+                    _con[cid].received = true;
                 }
             }
             count ++;
             if (count >= len) {
-                DBG("recv bulk %d %d/%d\r\n", _state.cid, count, len);
-                _con[_state.cid].received = true;
+                DBG("recv bulk %d %d/%d\r\n", cid, count, len);
+                _con[cid].received = true;
                 _state.mode = MODE_COMMAND;
             }
             break;
@@ -304,6 +304,9 @@
 
 void GSwifi::msgOk (const char *buf) {
     _state.ok = true;
+    if (_state.status == STAT_DEEPSLEEP) {
+        _state.status = STAT_READY;
+    }
 }
 
 void GSwifi::msgError (const char *buf) {
@@ -312,42 +315,44 @@
 
 void GSwifi::msgConnect (const char *buf) {
     int i, count;
+    int cid, acid;
 
     if (buf[8] < '0' || buf[8] > 'F' || buf[9] != ' ') return;
 
-    _state.cid = x2i(buf[8]);
-    _state.acid = x2i(buf[10]);
-    DBG("connect %d -> %d\r\n", _state.cid, _state.acid);
+    cid = x2i(buf[8]);
+    acid = x2i(buf[10]);
+    DBG("forked %d -> %d\r\n", cid, acid);
     // ip
     count = 0;
     for (i = 12; i < strlen(buf); i ++) {
         if ((buf[i] >= '0' && buf[i] <= '9') || buf[i] == '.') {
-            _con[_state.acid].ip[count] = buf[i];
+            _con[acid].ip[count] = buf[i];
             count ++;
         } else {
-            _con[_state.acid].ip[count] = 0;
+            _con[acid].ip[count] = 0;
             break;
         }
     }
     // port
-    _con[_state.acid].port = 0;
+    _con[acid].port = 0;
     count = 0;
     for (; i < strlen(buf); i ++) {
         if (buf[i] >= '0' && buf[i] <= '9') {
-            _con[_state.acid].port = (_con[_state.acid].port * 10) + (buf[i] - '0'); 
+            _con[acid].port = (_con[acid].port * 10) + (buf[i] - '0'); 
         } else {
             break;
         }
     }
 
-    _con[_state.acid].protocol = _con[_state.cid].protocol;
-    _con[_state.acid].type = _con[_state.cid].type;
-    _con[_state.acid].parent = _state.cid;
-    _con[_state.acid].accept = true;
-    if (_con[_state.acid].buf == NULL)
-        _con[_state.acid].buf = new CircBuffer<char>(CFG_DATA_SIZE);
-    _con[_state.acid].buf->flush();
-    _con[_state.acid].connected = true;
+    // initalize
+    _con[acid].protocol = _con[cid].protocol;
+    _con[acid].type = _con[cid].type;
+    _con[acid].parent = cid;
+    _con[acid].accept = true;
+    if (_con[acid].buf == NULL)
+        _con[acid].buf = new CircBuffer<char>(CFG_DATA_SIZE);
+    _con[acid].buf->flush();
+    _con[acid].connected = true;
 }
 
 void GSwifi::msgDisconnect (const char *buf) {
@@ -367,36 +372,52 @@
     for (i = 0; i < 16; i ++) {
         _con[i].connected = false;
     }
-#ifdef RTOS_H
+#ifdef CFG_ENABLE_RTOS
     if (_threadPoll)
         _threadPoll->signal_set(1);
 #endif
 }
 
 void GSwifi::msgReset (const char *buf) {
+    DBG("reset\r\n");
+    _state.initialized = false;
     _state.mode = MODE_COMMAND;
     _state.status = STAT_READY;
     msgDisassociated(NULL);
+    clearFlags();
+#ifdef CFG_ENABLE_RTOS
+    if (_threadPoll) {
+        _threadPoll->terminate();
+        delete _threadPoll;
+    }
+#endif
 }
 
 void GSwifi::msgOutofStandby (const char *buf) {
+    DBG("OutofStandby\r\n");
     _state.status = STAT_WAKEUP;
 }
 
 void GSwifi::msgOutofDeepsleep (const char *buf) {
+    DBG("OutofDeepsleep\r\n");
     _state.status = STAT_READY;
 }
 
 void GSwifi::resConnect (const char *buf) {
+    int cid;
+
+    // udp/tcp listen socket
     if (strncmp(buf, "CONNECT ", 8) == 0 && buf[9] == 0) {
-        _state.cid = x2i(buf[8]);
-        DBG("connect %d\r\n", _state.cid);
-        _con[_state.cid].parent = -1;
-        _con[_state.cid].accept = false;
-        if (_con[_state.cid].buf == NULL)
-            _con[_state.cid].buf = new CircBuffer<char>(CFG_DATA_SIZE);
-        _con[_state.cid].buf->flush();
-        _con[_state.cid].connected = true;
+        cid = x2i(buf[8]);
+        DBG("connect %d\r\n", cid);
+        // initalize
+        _con[cid].parent = -1;
+        _con[cid].accept = false;
+        if (_con[cid].buf == NULL)
+            _con[cid].buf = new CircBuffer<char>(CFG_DATA_SIZE);
+        _con[cid].buf->flush();
+        _con[cid].connected = true;
+        _state.cid = cid;
         _state.res = RES_NULL;
         return;
     }
@@ -414,6 +435,7 @@
         strncpy(_state.pass, &buf[11], sizeof(_state.pass));
         _state.n ++;
         _state.res = RES_NULL;
+        DBG("wps %s %s\r\n", _state.ssid, _state.pass);
     }
 }
 
@@ -422,7 +444,7 @@
         strncpy(_state.mac, buf, sizeof(_state.mac));
         _state.mac[17] = 0;
         _state.res = RES_NULL;
-        DBG("mac: %s", _state.mac);
+        DBG("mac %s\r\n", _state.mac);
     }
 }
 
@@ -451,7 +473,7 @@
     if (strncmp(buf, "IP:", 3) == 0) {
         strncpy(_state.resolv, &buf[3], sizeof(_state.resolv));
         _state.res = RES_NULL;
-        DBG("resolv: %s", _state.resolv);
+        DBG("resolv: %s\r\n", _state.resolv);
     }
 }
 
@@ -459,7 +481,7 @@
     if (buf[0] == '-' || (buf[0] >= '0' && buf[0] <= '9')) {
         _state.rssi = atoi(buf);
         _state.res = RES_NULL;
-        DBG("rssi: %d", _state.rssi);
+        DBG("rssi: %d\r\n", _state.rssi);
     }
 }
 
--- a/GSwifi/GSwifi_sock.cpp	Thu Oct 31 08:34:45 2013 +0000
+++ b/GSwifi/GSwifi_sock.cpp	Fri Nov 15 04:20:14 2013 +0000
@@ -22,6 +22,8 @@
 {
     int i, flg = 0;
 
+    if (!isConnected() && _state.status == STAT_READY) return -1;
+
     for (i = 0; i < strlen(host); i ++) {
         if ((host[i] < '0' || host[i] > '9') && host[i] != '.') {
             flg = 1;
@@ -43,35 +45,45 @@
 }
 
 int GSwifi::open (Protocol proto, const char *ip, int port, int src) {
+    int cid;
+
+    if (!isConnected() && _state.status == STAT_READY) return -1;
+
     _state.cid = -1;
-    _state.acid = -1;
     if (proto == PROTO_TCP) {
         if (cmdNCTCP(ip, port)) return -1;
     } else {
         if (cmdNCUDP(ip, port, src)) return -1;
     }
     if (_state.cid < 0) return -1;
-    _con[_state.cid].protocol = proto;
-    _con[_state.cid].type = TYPE_CLIENT;
-    return _state.cid;
+    cid = _state.cid;
+    _con[cid].protocol = proto;
+    _con[cid].type = TYPE_CLIENT;
+    return cid;
 }
 
 int GSwifi::listen (Protocol proto, int port) {
+    int cid;
+
+    if (!isConnected() && _state.status == STAT_READY) return -1;
+
     _state.cid = -1;
-    _state.acid = -1;
     if (proto == PROTO_TCP) {
         if (cmdNSTCP(port)) return -1;
     } else {
         if (cmdNSUDP(port)) return -1;
     }
     if (_state.cid < 0) return -1;
-    _con[_state.cid].protocol = proto;
-    _con[_state.cid].type = TYPE_SERVER;
-    return _state.cid;
+    cid = _state.cid;
+    _con[cid].protocol = proto;
+    _con[cid].type = TYPE_SERVER;
+    return cid;
 }
 
 int GSwifi::close (int cid) {
-    if (cid < 0 || cid >= 16) return -1;
+
+    if (!isConnected(cid)) return -1;
+
     _con[cid].connected = false;
     return cmdNCLOSE(cid);
 }
@@ -79,15 +91,14 @@
 int GSwifi::send (int cid, const char *buf, int len) {
     char cmd[CFG_CMD_SIZE];
 
-    if (cid < 0 || cid >= 16) return -1;
-    if (!_con[cid].connected) return -1;
+    if (!isConnected(cid)) return -1;
 
     if ((_con[cid].protocol == PROTO_TCP) ||
       (_con[cid].protocol == PROTO_UDP && _con[cid].type == TYPE_CLIENT) ||
       (_con[cid].protocol == PROTO_HTTPD)) {
         if (len > CFG_DATA_SIZE) len = CFG_DATA_SIZE;
         sprintf(cmd, "\x1bZ%X%04d", cid, len);
-        return sendData(buf, len, DEFAULT_WAIT_RESP_TIMEOUT, cmd);
+        return sendData(buf, len, CFG_TIMEOUT, cmd);
     } else {
         return -1;
     }
@@ -96,13 +107,12 @@
 int GSwifi::sendto (int cid, const char *buf, int len, const char *ip, int port) {
     char cmd[CFG_CMD_SIZE];
 
-    if (cid < 0 || cid >= 16) return -1;
-    if (!_con[cid].connected) return -1;
+    if (!isConnected(cid)) return -1;
 
     if ((_con[cid].protocol == PROTO_UDP && _con[cid].type == TYPE_SERVER)) {
         if (len > CFG_DATA_SIZE) len = CFG_DATA_SIZE;
         sprintf(cmd, "\x1bY%X%s:%d:%04d", cid, ip, port, len);
-        return sendData(buf, len, DEFAULT_WAIT_RESP_TIMEOUT, cmd);
+        return sendData(buf, len, CFG_TIMEOUT, cmd);
     } else {
         return -1;
     }
@@ -111,8 +121,7 @@
 int GSwifi::recv (int cid, char *buf, int len) {
     int i;
 
-    if (cid < 0 || cid >= 16) return -1;
-    if (!_con[cid].connected) return -1;
+    if (!isConnected(cid)) return -1;
 
     if (_con[cid].buf == NULL) return 0;
     while (!_con[cid].received && _state.mode != MODE_COMMAND);
@@ -127,8 +136,7 @@
 int GSwifi::recvfrom (int cid, char *buf, int len, char *ip, int *port) {
     int i;
 
-    if (cid < 0 || cid >= 16) return -1;
-    if (!_con[cid].connected) return -1;
+    if (!isConnected(cid)) return -1;
 
     if (_con[cid].buf == NULL) return 0;
     while (!_con[cid].received && _state.mode != MODE_COMMAND);
@@ -143,20 +151,23 @@
 }
 
 int GSwifi::readable (int cid) {
-    if (cid < 0 || cid >= 16) return -1;
-    if (!_con[cid].connected) return -1;
+    if (!isConnected(cid)) return -1;
+
     if (_con[cid].buf == NULL) return -1;
     return _con[cid].buf->available();
 }
 
 bool GSwifi::isConnected (int cid) {
     if (cid < 0 || cid >= 16) return false;
+
     return _con[cid].connected;
 }
 
 int GSwifi::accept (int cid) {
     int i;
-    if (cid < 0 || cid >= 16) return -1;
+
+    if (!isConnected(cid)) return -1;
+
     for (i = 0; i < 16; i ++) {
         if (_con[i].connected && _con[i].accept && _con[i].parent == cid) {
             _con[i].accept = false;
@@ -167,7 +178,9 @@
 }
 
 int GSwifi::getRemote(int cid, char **ip, int *port) {
-    if (cid < 0 || cid >= 16) return -1;
+
+    if (!isConnected(cid)) return -1;
+
     *ip = _con[cid].ip;
     *port = _con[cid].port;
     return 0;
--- a/GSwifi/GSwifi_util.cpp	Thu Oct 31 08:34:45 2013 +0000
+++ b/GSwifi/GSwifi_util.cpp	Fri Nov 15 04:20:14 2013 +0000
@@ -87,6 +87,8 @@
 int GSwifi::ntpdate (char *host, int sec) {
     char ip[17];
 
+    if (!isConnected() && _state.status == STAT_READY) return -1;
+
     if (getHostByName(host, ip)) return -1;
     return cmdNTIMESYNC(true, ip, sec);
 }
@@ -96,5 +98,80 @@
 }
 
 int GSwifi::provisioning (char *user, char *pass) {
+
+    if (!isConnected() && _state.status == STAT_READY) return -1;
+
     return cmdWEBPROV(user, pass);
 }
+
+int GSwifi::standby (int msec) {
+    switch (_state.status) {
+    case STAT_READY:
+        cmdNCLOSEALL();
+        for (int i = 0; i < 16; i ++) {
+            _con[i].connected = false;
+        }
+        cmdSTORENWCONN();
+//        cmdWRXACTIVE(false);
+        break;
+    case STAT_WAKEUP:
+        if (cmdE(false)) return -1;
+        if (_flow) {
+            cmdR(true);
+        }
+        break;
+    default:
+        return -1;
+    }
+
+    _state.status = STAT_STANDBY;
+    return cmdPSSTBY(msec, 0);
+}
+
+
+int GSwifi::deepSleep () {
+    if (_state.status != STAT_READY) return -1;
+
+    _state.status = STAT_DEEPSLEEP;
+    return cmdPSDPSLEEP(); // go deep sleep
+}
+
+int GSwifi::wakeup () {
+    int r;
+
+    if (_state.status == STAT_STANDBY) {
+        Timer timeout;
+        setAlarm(true);
+        timeout.start();
+        while (timeout.read() < CFG_TIMEOUT) {
+            if (_state.status == STAT_WAKEUP) break;
+        }
+        timeout.stop();
+        setAlarm(false);
+    }
+
+    switch (_state.status) {
+    case STAT_WAKEUP:
+        if (cmdE(false)) return -1;
+        if (_flow) {
+            cmdR(true);
+        }
+        cmdBDATA(true);
+        r = cmdRESTORENWCONN();
+        if (!r) {
+            wait_ms(100);
+//            cmdWRXACTIVE(true);
+            _state.status = STAT_READY;
+        }
+        return r;
+    case STAT_DEEPSLEEP:
+        r = cmdAT();
+        if (!r) {
+            _state.status = STAT_READY;
+        }
+        return r;
+    default:
+        break;
+    }
+    return -1;
+}
--- a/GSwifiInterface.cpp	Thu Oct 31 08:34:45 2013 +0000
+++ b/GSwifiInterface.cpp	Fri Nov 15 04:20:14 2013 +0000
@@ -11,32 +11,24 @@
 
 int GSwifiInterface::init(const char* name)
 {
-    _state.dhcp = true;
-    strncpy(_state.name, name, sizeof(_state.name));
-
-    return 0;
+    return setAddress(name);
 }
 
-int GSwifiInterface::init(const char* ip, const char* mask, const char* gateway, const char* name)
+int GSwifiInterface::init(const char* ip, const char* netmask, const char* gateway, const char* name)
 {
-    _state.dhcp = false;
-    strncpy(_state.ip, ip, sizeof(_state.ip));
-    strncpy(_state.netmask, mask, sizeof(_state.netmask));
-    strncpy(_state.gateway, gateway, sizeof(_state.gateway));
-    strncpy(_state.name, name, sizeof(_state.name));
-
-    return 0;
+    return setAddress(ip, netmask, gateway, name);
 }
 
 int GSwifiInterface::connect(Security sec, const char* ssid, const char* phrase, WiFiMode mode)
 {
+    setSsid(sec, ssid, phrase);
     switch (mode) {
     case WM_INFRASTRUCTURE:
-        return join(sec, ssid, phrase);
+        return join();
     case WM_ADHOCK:
-        return adhock(sec, ssid, phrase);
+        return adhock();
     case WM_LIMITEDAP:
-        return limitedap(sec, ssid, phrase);
+        return limitedap();
     }
     return -1;
 }
--- a/Socket/TCPSocketConnection.cpp	Thu Oct 31 08:34:45 2013 +0000
+++ b/Socket/TCPSocketConnection.cpp	Fri Nov 15 04:20:14 2013 +0000
@@ -83,12 +83,14 @@
         tmr.start();
         while (time < _timeout + 20) {
             if (_wifi->readable(_cid)) {
+                DBG("receive readable");
                 break;
             }
             time = tmr.read_ms();
         }
         if (time >= _timeout + 20) {
-            return -1;
+            DBG("receive timeout");
+            return 0;
         }
     }
 
--- a/Socket/TCPSocketServer.cpp	Thu Oct 31 08:34:45 2013 +0000
+++ b/Socket/TCPSocketServer.cpp	Fri Nov 15 04:20:14 2013 +0000
@@ -56,4 +56,5 @@
             return 0;
         }
     }
+    return -1;
 }
\ No newline at end of file