gs fan / GSwifi_old

Fork of GSwifi_old by gs fan

Files at this revision

API Documentation at this revision

Comitter:
gsfan
Date:
Wed Jan 23 07:41:23 2013 +0000
Parent:
23:a783c62c36d0
Commit message:
auto re-connect

Changed in this revision

GSwifi.cpp Show annotated file Show diff for this revision Revisions of this file
GSwifi.h Show annotated file Show diff for this revision Revisions of this file
GSwifi_httpd.cpp Show annotated file Show diff for this revision Revisions of this file
GSwifi_net.h Show annotated file Show diff for this revision Revisions of this file
--- a/GSwifi.cpp	Mon Jan 21 05:58:28 2013 +0000
+++ b/GSwifi.cpp	Wed Jan 23 07:41:23 2013 +0000
@@ -22,6 +22,9 @@
     _escape = 0;
     _response = GSRES_NONE;
     _gs_mode = GSMODE_COMMAND;
+    _ssid = NULL;
+    _reconnect = 0;
+    _reconnect_count = 0;
 
     _gs.baud(baud);
     _gs.attach(this, &GSwifi::isr_recv, Serial::RxIrq);
@@ -34,6 +37,9 @@
     _escape = 0;
     _response = GSRES_NONE;
     _gs_mode = GSMODE_COMMAND;
+    _ssid = NULL;
+    _reconnect = 0;
+    _reconnect_count = 0;
 
     _gs.baud(baud);
     _gs.attach(this, &GSwifi::isr_recv, Serial::RxIrq);
@@ -141,7 +147,6 @@
                 _buf_cmd.put(dat);
                 if (dat == '\n') {
                     _gs_enter ++;
-                    DBG("* %d %d *", _response, _connect);
                     if (_response == GSRES_NONE && _connect) {
                         DBG("poll_cmd\r\n");
                         poll_cmd();
@@ -390,7 +395,7 @@
             }
             break;
         case GSRES_CONNECT:
-            if (strncmp(buf, "CONNECT", 7) == 0) {
+            if (strncmp(buf, "CONNECT ", 8) == 0) {
                 _cid = x2i(buf[8]);
                 flg = 1;
             }
@@ -476,7 +481,7 @@
     }
 }
 
-int GSwifi::connect (GSSECURITY sec, const char *ssid, const char *pass, int dhcp) {
+int GSwifi::connect (GSSECURITY sec, const char *ssid, const char *pass, int dhcp, int reconnect) {
     int r;
     char cmd[GS_CMD_SIZE];
     
@@ -556,7 +561,13 @@
         command(cmd, GSRES_NORMAL);
     }
 
-    if (r == 0) _connect = true;
+    if (r == 0) {
+        _connect = true;
+        _reconnect = reconnect;
+        _reconnect_count = 0;
+        if (!_ssid) _ssid = new char[sizeof(ssid) + 1];
+        strcpy(_ssid, ssid);
+    }
     return r;
 }
 
@@ -989,6 +1000,20 @@
             }
         }
     }
+    
+    if (!_connect && _reconnect_count < _reconnect && _ssid) {
+        // re-connrct
+        int r;
+        char cmd[GS_CMD_SIZE];
+        _reconnect_count ++;
+        DBG("re-connect %d\r\n", _reconnect_count);
+        sprintf(cmd, "AT+WA=%s", _ssid);
+        r = command(cmd, GSRES_DHCP, GS_TIMEOUT2);
+        if (r == 0) {
+            _connect = true;
+            _reconnect_count = 0;
+        }
+    }
 }
 
 void GSwifi::newSock (int cid, GSTYPE type, GSPROTOCOL pro, onGsReceiveFunc ponGsReceive) {
--- a/GSwifi.h	Mon Jan 21 05:58:28 2013 +0000
+++ b/GSwifi.h	Wed Jan 23 07:41:23 2013 +0000
@@ -156,10 +156,11 @@
      * @param ssid SSID
      * @param pass pass phrase
      * @param dhcp 0:static ip, 1:dhcp
+     * @param reconnect auto re-connect
      * @retval 0 success
      * @retval -1 failure
      */
-    int connect (GSSECURITY sec, const char *ssid, const char *pass, int dhcp = 1);
+    int connect (GSSECURITY sec, const char *ssid, const char *pass, int dhcp = 1, int reconnect = 0);
     /**
      * adhock
      * @param sec GSSEC_OPEN or GSSEC_WEP
@@ -445,6 +446,8 @@
     RingBuffer _buf_cmd;
     struct GS_Socket _gs_sock[16];
     time_t _time;
+    char *_ssid;
+    int _reconnect, _reconnect_count;
     
 #ifdef GS_USE_HTTPD
     struct GS_httpd _httpd[16];
--- a/GSwifi_httpd.cpp	Mon Jan 21 05:58:28 2013 +0000
+++ b/GSwifi_httpd.cpp	Wed Jan 23 07:41:23 2013 +0000
@@ -174,8 +174,13 @@
 #ifdef GS_USE_WEBSOCKET
     if (flg && _httpd[cid].mode == GSHTTPDMODE_WEBSOCKET) {
         // websocket
+        _httpd[cid].host = _gs_sock[cid].host;
+        j = strlen(_handler[i].uri);
+        _httpd[cid].file = &_httpd[cid].uri[j];
+        _httpd[cid].query = NULL;
+
         send_websocket_accept(cid);
-        break; // break while
+        break; // exit while
 
     } else
 #endif
@@ -256,7 +261,7 @@
     strcpy(file, dir);
     strcat(file, gshttpd->file);
     if (file[strlen(file) - 1] == '/') {
-        strcat(file, "index.htm");
+        strcat(file, "index.html");
     }
     DBG("file: %s\r\n", file);
     
@@ -488,16 +493,11 @@
       case 0x02: // binary
         i = get_handler(_httpd[cid].uri);
         if (i >= 0) {
-            _httpd[cid].host = _gs_sock[cid].host;
-            j = strlen(_handler[i].uri);
-            _httpd[cid].file = &_httpd[cid].uri[j];
-            _httpd[cid].query = NULL;
-
             if (_handler[i].onHttpCgi) {
                 // cgi
                 _handler[i].onHttpCgi(cid, &_httpd[cid]);
                 LOG("%d.%d.%d.%d ", _httpd[cid].host.getIp()[0], _httpd[cid].host.getIp()[1], _httpd[cid].host.getIp()[2], _httpd[cid].host.getIp()[3]);
-                LOG("%s %s %d 200 -\r\n", _httpd[cid].type == GSPROT_HTTPGET ? "GET" : "POST", _httpd[cid].uri, _httpd[cid].length);
+                LOG("%s %s %d 200 -\r\n", "WEBSOCKET", _httpd[cid].uri, _httpd[cid].length);
                 flg = 1;
             }
         }
@@ -561,7 +561,6 @@
     base64encode(buf2, 20, buf, sizeof(buf));
     send(cid, buf, strlen(buf));
     send(cid, "\r\n", 2);
-
 //    send(cid, "Sec-WebSocket-Protocol: chat\r\n", 30);
     send(cid, "\r\n", 2);
     LOG("%d.%d.%d.%d ", _httpd[cid].host.getIp()[0], _httpd[cid].host.getIp()[1], _httpd[cid].host.getIp()[2], _httpd[cid].host.getIp()[3]);
--- a/GSwifi_net.h	Mon Jan 21 05:58:28 2013 +0000
+++ b/GSwifi_net.h	Wed Jan 23 07:41:23 2013 +0000
@@ -16,9 +16,9 @@
 #include "mbed.h"
 #include "host.h"
 
-#define GS_USE_HTTPD  // comment out if not use httpd
-#define GS_USE_WEBSOCKET
-#define GS_USE_SMTP  // comment out if not use smtp
+#define GS_USE_HTTPD  // use http server
+//#define GS_USE_WEBSOCKET  // use websocket (need httpd)
+#define GS_USE_SMTP  // use smtp client
 #define GS_SYSLOG // log for stdout