GainSpan Wi-Fi library see: http://mbed.org/users/gsfan/notebook/gainspan_wifi/

Fork of GSwifi_old by gs fan

GainSpan Wi-Fi library

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

see: http://mbed.org/users/gsfan/notebook/gainspan_wifi/

Information

Please change the baud rate in advance.

  • ATB=115200
  • AT&W0

It may be better and sometimes faster.
GSwifi gs(p13, p14, baud);

Heavily modified new library: http://mbed.org/users/gsfan/code/GSwifi

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

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

解説: http://mbed.org/users/gsfan/notebook/gainspan_wifi/

Information

モジュールはあらかじめ次のコマンドでボーレートを変更しておく。

  • ATB=115200
  • AT&W0

場合によってはもっと高速の方がいいかもしれない。クラス宣言時にレート設定をする。
GSwifi gs(p13, p14, baud);

大幅に更新された新しいライブラリ: http://mbed.org/users/gsfan/code/GSwifi

Revision:
19:cad912f5a6ba
Parent:
18:4b97804c37d1
Child:
20:151b5a4fdd29
diff -r 4b97804c37d1 -r cad912f5a6ba GSwifi_httpd.cpp
--- a/GSwifi_httpd.cpp	Thu Nov 01 02:55:57 2012 +0000
+++ b/GSwifi_httpd.cpp	Tue Nov 06 08:54:19 2012 +0000
@@ -8,6 +8,19 @@
 #define HTTPD_BODY 3
 #define HTTPD_ERROR 4
 
+#define MIMETABLE_NUM 6
+struct {
+    char ext[5];
+    char type[24];
+} mimetable[MIMETABLE_NUM] = {
+    {".txt", "text/plain"},
+    {".htm", "text/html"},
+    {".css", "text/css"},
+    {".jpg", "image/jpeg"},
+    {".png", "image/png"},
+    {".gif", "image/gif"},
+};
+
 int GSwifi::httpd (int port) {
     int i;
     char cmd[GS_CMD_SIZE];
@@ -25,6 +38,13 @@
     if (command(cmd, GSRES_CONNECT)) return -1;
 
     newSock(_cid, GSTYPE_SERVER, GSPROT_HTTPD, NULL);
+/*
+//    newSock(_cid, GSTYPE_SERVER, GSPROT_HTTPD, &GSwifi::poll_httpd);
+//    newSock(_cid, GSTYPE_SERVER, GSPROT_HTTPD, reinterpret_cast<onGsReceiveFunc>(&GSwifi::poll_httpd));
+    void (GSwifi::*f1)(int,int) = &GSwifi::poll_httpd;
+    onGsReceiveFunc f2 = reinterpret_cast<onGsReceiveFunc>(f1);
+    newSock(_cid, GSTYPE_SERVER, GSPROT_HTTPD, f2);
+*/
     return _cid;
 }
 
@@ -64,11 +84,11 @@
     switch (_httpd[cid].mode) {
     case HTTPD_REQUEST:
         if (strncmp(_httpd[cid].buf, "GET ", 4) == 0) {
-            _httpd[cid].type = 0;
+            _httpd[cid].type = GSPROT_HTTPGET;
             j = 4;
         } else
         if (strncmp(_httpd[cid].buf, "POST ", 5) == 0) {
-            _httpd[cid].type = 1;
+            _httpd[cid].type = GSPROT_HTTPPOST;
             j = 5;
         } else {
             _httpd[cid].mode = HTTPD_ERROR;
@@ -94,7 +114,7 @@
     case HTTPD_HEAD:
         if (_httpd[cid].len == 0) {
             _httpd[cid].mode = HTTPD_BODY;
-            if (_httpd[cid].length == 0) flg = 1;
+            if (_httpd[cid].length == 0) flg = 1; // no body
         } else
         if (strncmp(_httpd[cid].buf, "Content-length: ", 16) == 0) {
             _httpd[cid].length = atoi(&_httpd[cid].buf[16]);
@@ -122,27 +142,41 @@
         for (i = 0; i < _handler_count; i ++) {
             j = strlen(_handler[i].uri);
             if (strncmp(_httpd[cid].uri, _handler[i].uri, j) == NULL) {
+                // found
+                _httpd[cid].host = _gs_sock[cid].host;
+                _httpd[cid].file = &_httpd[cid].uri[j];
+                _httpd[cid].query = NULL;
+                for (; j < strlen(_httpd[cid].uri); j ++) {
+                    if (_httpd[cid].uri[j] == '?') {
+                        // query_string
+                        _httpd[cid].uri[j] = 0;
+                        _httpd[cid].query = &_httpd[cid].uri[j + 1];
+                        break;
+                    }
+                }
+
                 if (_handler[i].dir) {
                     // file
-                    httpd_request(cid, _handler[i].dir, &_httpd[cid].uri[j], _httpd[cid].keepalive);
+                    httpd_request(cid, &_httpd[cid], _handler[i].dir);
                     flg = 1;
                 } else
                 if (_handler[i].onHttpCgi) {
                     // cgi
-                    _handler[i].onHttpCgi(cid, &_httpd[cid].uri[j], _httpd[cid].buf, _httpd[cid].len);
+                    _handler[i].onHttpCgi(cid, &_httpd[cid]);
+                    _httpd[cid].keepalive = 0;
                     flg = 1;
                 }
                 break;
             }
         }
         if (! flg) {
+            // not found
             send_httpd_error(cid, 403);
         }
         
         if (_httpd[cid].keepalive) {
             _httpd[cid].mode = HTTPD_REQUEST;
             _httpd[cid].len = 0;
-            _httpd[cid].type = 0;
             _httpd[cid].length = 0;
             _httpd[cid].keepalive --;
         } else {
@@ -158,13 +192,13 @@
   }
 }
 
-int GSwifi::httpd_request (int cid, char *dir, char *file, int keep) {
+int GSwifi::httpd_request (int cid, GS_httpd *gshttpd, char *dir) {
     FILE *fp;
     int i, j;
-    char buf[100], tmp[20];
+    char buf[100];
 
     strcpy(buf, dir);
-    strcat(buf, file);
+    strcat(buf, gshttpd->file);
     if (buf[strlen(buf) - 1] == '/') {
         strcat(buf, "index.htm");
     }
@@ -172,9 +206,9 @@
     
     fp = fopen(buf, "r");
     if (fp) {
-        strcpy(buf, "HTTP/1.1 200 OK\r\n");
-        send(cid, buf, strlen(buf));
+        send(cid, "HTTP/1.1 200 OK\r\n", 17);
         {
+            // file size
             j = ftell(fp);
             fseek(fp, 0, SEEK_END);
             i = ftell(fp);
@@ -182,24 +216,9 @@
         }
         sprintf(buf, "Content-Length: %d\r\n", i);
         send(cid, buf, strlen(buf));
-        j = strlen(file);
-        if (j == 0 || strncmp(&file[j - 4], ".htm", 4)) {
-            strcpy(tmp, "text/html");
-        } else
-        if (strncmp(&file[j - 4], ".jpg", 4)) {
-            strcpy(tmp, "image/jpeg");
-        } else
-        if (strncmp(&file[j - 4], ".png", 4)) {
-            strcpy(tmp, "image/png");
-        } else
-        if (strncmp(&file[j - 4], ".gif", 4)) {
-            strcpy(tmp, "image/gif");
-        } else {
-            strcpy(tmp, "text/plain");
-        }
-        sprintf(buf, "Content-Type: %s\r\n", tmp);
+        sprintf(buf, "Content-Type: %s\r\n", mimetype(gshttpd->file));
         send(cid, buf, strlen(buf));
-        if (keep) {
+        if (gshttpd->keepalive) {
             strcpy(buf, "Connection: Keep-Alive\r\n");
         } else {
             strcpy(buf, "Connection: close\r\n");
@@ -223,6 +242,18 @@
     return -1;
 }
 
+char *GSwifi::mimetype (char *file) {
+    int i, j;
+
+    for (i = 0; i < MIMETABLE_NUM; i ++) {
+        j = strlen(mimetable[i].ext);
+        if (strncmp(&file[strlen(file) - j], mimetable[i].ext, j) == NULL) {
+            return mimetable[i].type;
+        }
+    }
+    return mimetable[0].type;
+}
+
 void GSwifi::send_httpd_error (int cid, int err) {
     char buf[100], msg[30];
     
@@ -256,7 +287,7 @@
     close(cid);
 }
 
-int GSwifi::attach_httpd (char *uri, char *dir) {
+int GSwifi::attach_httpd (const char *uri, const char *dir) {
     if (_handler_count < HTTPD_HANDLE) {
         _handler[_handler_count].uri = new char[strlen(uri) + 1];
         strcpy(_handler[_handler_count].uri, uri);
@@ -270,7 +301,7 @@
     }
 }
 
-int GSwifi::attach_httpd (char *uri, onHttpdCgiFunc ponHttpCgi) {
+int GSwifi::attach_httpd (const char *uri, onHttpdCgiFunc ponHttpCgi) {
     if (_handler_count < HTTPD_HANDLE) {
         _handler[_handler_count].uri = new char[strlen(uri) + 1];
         strcpy(_handler[_handler_count].uri, uri);