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

Dependents:   GSwifi_httpd GSwifi_websocket GSwifi_tcpclient GSwifi_tcpserver ... more

Fork of GSwifi 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/

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

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

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

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

Files at this revision

API Documentation at this revision

Comitter:
gsfan
Date:
Tue Oct 09 16:05:40 2012 +0000
Parent:
14:9e89b922ace1
Child:
16:aea56cce3bf5
Commit message:
add httpPost, http auth

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
--- a/GSwifi.cpp	Fri Oct 05 16:42:50 2012 +0000
+++ b/GSwifi.cpp	Tue Oct 09 16:05:40 2012 +0000
@@ -13,6 +13,7 @@
 #include "dbg.h"
 #include "mbed.h"
 #include "GSwifi.h"
+#include <ctype.h>
 
 #ifdef GS_UART_DIRECT
 #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368)
@@ -1107,7 +1108,7 @@
     return _gs_sock[cid].connect;
 }
 
-int GSwifi::httpGet (Host &host, char *uri, int ssl, onGsReceiveFunc ponGsReceive) {
+int GSwifi::httpGet (Host &host, char *uri, int ssl, char *user, char *pwd, onGsReceiveFunc ponGsReceive) {
     char cmd[GS_CMD_SIZE];
 
     if (! _connect || _status != GSSTAT_READY) return -1;
@@ -1128,20 +1129,94 @@
     command("AT+HTTPCONF=3,close", GSRES_NORMAL); // Connection:
     sprintf(cmd, "AT+HTTPCONF=11,%s", host.getName());  // Host:
     command(cmd, GSRES_NORMAL);
+    if (user && pwd) {
+        char tmp[GS_CMD_SIZE], tmp2[GS_CMD_SIZE];
+        snprintf(tmp, sizeof(tmp), "%s:%s", user, pwd);
+        base64encode(tmp, strlen(tmp), tmp2, sizeof(tmp2));
+        sprintf(cmd, "AT+HTTPCONF=2,Basic %s", tmp2);  // Authorization:
+        command(cmd, GSRES_NORMAL);
+    } else {
+        command("AT+HTTPCONFDEL=2", GSRES_NORMAL);
+    }
+    command("AT+HTTPCONFDEL=5", GSRES_NORMAL);
+    command("AT+HTTPCONFDEL=7", GSRES_NORMAL);
+
     sprintf(cmd, "AT+HTTPOPEN=%d.%d.%d.%d,%d", host.getIp()[0], host.getIp()[1], host.getIp()[2], host.getIp()[3], host.getPort());
     if (ssl) {
         strcat(cmd, ",1");
     }
     if (command(cmd, GSRES_HTTP)) return -1;
-
     newSock(_cid, GSTYPE_CLIENT, GSPROT_HTTPGET, ponGsReceive);
 
-    sprintf(cmd, "AT+HTTPSEND=%d,1,%d,%s", _cid, GS_TIMEOUT / 1000, uri);  // Get:
+    sprintf(cmd, "AT+HTTPSEND=%d,1,%d,%s", _cid, GS_TIMEOUT / 1000, uri);  // GET
     command(cmd, GSRES_NORMAL);
 
     return _cid;
 }
 
+int GSwifi::httpGet (Host &host, char *uri, int ssl, onGsReceiveFunc ponGsReceive) {
+    return httpGet (host, uri, ssl, NULL, NULL, ponGsReceive);
+}
+
+int GSwifi::httpPost (Host &host, char *uri, char *body, int ssl, char *user, char *pwd, onGsReceiveFunc ponGsReceive) {
+    char cmd[GS_CMD_SIZE];
+    int i, len;
+
+    if (! _connect || _status != GSSTAT_READY) return -1;
+
+    if (host.getIp().isNull()) {
+        if (getHostByName(host)) {
+            if (getHostByName(host)) return -1;
+        }
+    }
+    if (host.getPort() == 0) {
+        if (ssl) {
+            host.setPort(443);
+        } else {
+            host.setPort(80);
+        }
+    }
+    len = strlen(body);
+
+    command("AT+HTTPCONF=3,close", GSRES_NORMAL); // Connection:
+    sprintf(cmd, "AT+HTTPCONF=11,%s", host.getName());  // Host:
+    command(cmd, GSRES_NORMAL);
+    sprintf(cmd, "AT+HTTPCONF=5,%d", len);  // Content-Length:
+    command(cmd, GSRES_NORMAL);
+    command("AT+HTTPCONF=7,application/x-www-form-urlencoded", GSRES_NORMAL); // Content-type:
+    if (user && pwd) {
+        char tmp[GS_CMD_SIZE], tmp2[GS_CMD_SIZE];
+        snprintf(tmp, sizeof(tmp), "%s:%s", user, pwd);
+        base64encode(tmp, strlen(tmp), tmp2, sizeof(tmp2));
+        sprintf(cmd, "AT+HTTPCONF=2,Basic %s", tmp2);  // Authorization:
+        command(cmd, GSRES_NORMAL);
+    } else {
+        command("AT+HTTPCONFDEL=2", GSRES_NORMAL);
+    }
+
+    sprintf(cmd, "AT+HTTPOPEN=%d.%d.%d.%d,%d", host.getIp()[0], host.getIp()[1], host.getIp()[2], host.getIp()[3], host.getPort());
+    if (ssl) {
+        strcat(cmd, ",1");
+    }
+    if (command(cmd, GSRES_HTTP)) return -1;
+    newSock(_cid, GSTYPE_CLIENT, GSPROT_HTTPPOST, ponGsReceive);
+
+    sprintf(cmd, "AT+HTTPSEND=%d,3,%d,%s,%d", _cid, GS_TIMEOUT / 1000, uri, len);  // POST
+    command(cmd, GSRES_NORMAL);
+
+    _gs.printf("\x1bH%X", _cid);
+    for (i = 0; i < len; i ++) {
+        _gs_putc(body[i]);
+        DBG("%c", body[i]);
+    }
+
+    return _cid;
+}
+
+int GSwifi::httpPost (Host &host, char *uri, char *body, int ssl, onGsReceiveFunc ponGsReceive) {
+    return httpPost (host, uri, body, ssl, NULL, NULL, ponGsReceive);
+}
+
 int GSwifi::certAdd (char *name, char *cert, int len) {
     int i;
     char cmd[GS_CMD_SIZE];
@@ -1159,6 +1234,55 @@
     return cmdResponse(GSRES_NORMAL, GS_TIMEOUT);
 }
 
+int GSwifi::base64encode (const char *input, int length, char *output, int len) {
+    // code from 
+    // Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
+    static const char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+    unsigned int c, c1, c2, c3;
+
+    if (len < ((((length-1)/3)+1)<<2)) return -1;
+    for(unsigned int i = 0, j = 0; i<length; i+=3,j+=4) {
+        c1 = ((((unsigned char)*((unsigned char *)&input[i]))));
+        c2 = (length>i+1)?((((unsigned char)*((unsigned char *)&input[i+1])))):0;
+        c3 = (length>i+2)?((((unsigned char)*((unsigned char *)&input[i+2])))):0;
+
+        c = ((c1 & 0xFC) >> 2);
+        output[j+0] = base64[c];
+        c = ((c1 & 0x03) << 4) | ((c2 & 0xF0) >> 4);
+        output[j+1] = base64[c];
+        c = ((c2 & 0x0F) << 2) | ((c3 & 0xC0) >> 6);
+        output[j+2] = (length>i+1)?base64[c]:'=';
+        c = (c3 & 0x3F);
+        output[j+3] = (length>i+2)?base64[c]:'=';
+    }
+    output[(((length-1)/3)+1)<<2] = '\0';
+    return 0;
+}
+
+int GSwifi::urlencode (char *str, char *buf, int len) {
+    // code from 
+    // Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
+    static const char to_hex[] = "0123456789ABCDEF";
+//    char *pstr = str, *buf = (char*)malloc(strlen(str) * 3 + 1), *pbuf = buf;
+    char *pstr = str, *pbuf = buf;
+
+    if (len < (strlen(str) * 3 + 1)) return -1;
+    while (*pstr) {
+        if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~') {
+            *pbuf++ = *pstr;
+        } else if (*pstr == ' ') {
+            *pbuf++ = '+';
+        } else { 
+            *pbuf++ = '%';
+            *pbuf++ = to_hex[(*pstr >> 4) & 0x0f];
+            *pbuf++ = to_hex[*pstr & 0x0f];
+        }
+        pstr++;
+    }
+    *pbuf = '\0';
+    return 0;
+}
+
 int GSwifi::x2i (char c) {
     if (c >= '0' && c <= '9') {
         return c - '0';
--- a/GSwifi.h	Fri Oct 05 16:42:50 2012 +0000
+++ b/GSwifi.h	Tue Oct 09 16:05:40 2012 +0000
@@ -302,12 +302,23 @@
      * @return CID, -1:failure
      * If you use ssl, please set system time.
      */
+    int httpGet (Host &host, char *uri, int ssl, char *user, char *pwd, onGsReceiveFunc ponGsReceive = NULL);
     int httpGet (Host &host, char *uri, int ssl = 0, onGsReceiveFunc ponGsReceive = NULL);
     /**
+     * http request
+     * @return CID, -1:failure
+     * If you use ssl, please set system time.
+     */
+    int httpPost (Host &host, char *uri, char *body, int ssl, char *user, char *pwd, onGsReceiveFunc ponGsReceive = NULL);
+    int httpPost (Host &host, char *uri, char *body, int ssl = 0, onGsReceiveFunc ponGsReceive = NULL);
+    /**
      * certificate 
      */
     int certAdd (char *name, char *cert, int len);
 
+    int base64encode (const char *input, int length, char *output, int len);
+    int urlencode (char *str, char *buf, int len);
+
     void test ();
     int getc();
     void putc(char c);