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
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/
ゲインスパン Wi-Fi モジュール ライブラリ
ゲインスパン社の低電力 Wi-Fiモジュール(無線LAN) GS1011 シリーズ用のライブラリです。
解説: http://mbed.org/users/gsfan/notebook/gainspan_wifi/
Diff: GSwifi_http.cpp
- Revision:
- 29:1c4419512941
- Parent:
- 25:f6e5622d2930
- Child:
- 30:f3e9053d9b46
--- a/GSwifi_http.cpp Fri Feb 22 03:07:43 2013 +0000 +++ b/GSwifi_http.cpp Tue Feb 26 02:48:31 2013 +0000 @@ -23,6 +23,7 @@ #include "dbg.h" #include "mbed.h" #include "GSwifi.h" +#include <string.h> int GSwifi::httpGet (Host &host, const char *uri, const char *user, const char *pwd, int ssl, onGsReceiveFunc ponGsReceive) { @@ -71,10 +72,6 @@ return _cid; } -int GSwifi::httpGet (Host &host, const char *uri, int ssl, onGsReceiveFunc ponGsReceive) { - return httpGet (host, uri, NULL, NULL, ssl, ponGsReceive); -} - int GSwifi::httpPost (Host &host, const char *uri, const char *body, const char *user, const char *pwd, int ssl, onGsReceiveFunc ponGsReceive) { char cmd[GS_CMD_SIZE]; int i, len; @@ -130,8 +127,144 @@ return _cid; } -int GSwifi::httpPost (Host &host, const char *uri, const char *body, int ssl, onGsReceiveFunc ponGsReceive) { - return httpPost (host, uri, body, NULL, NULL, ssl, ponGsReceive); +int GSwifi::wsOpen (Host &host, const char *uri, const char *user, const char *pwd, onGsReceiveFunc ponGsReceive) { + int cid; + char cmd[GS_CMD_SIZE], tmp[GS_CMD_SIZE]; + + if (! _connect || _status != GSSTAT_READY) return -1; + + if (host.getIp().isNull()) { + if (getHostByName(host)) { + if (getHostByName(host)) return -1; + } + } + if (host.getPort() == 0) { + host.setPort(80); + } + + cid = open(host, GSPROT_TCP); + if (cid < 0) return -1; + DBG("cid %d\r\n", cid); + + // send request + send(cid, "GET ", 4); + send(cid, uri, strlen(uri)); + send(cid, " HTTP/1.1\r\n", 11); + if (host.getName() && host.getName()[0] != 0) { + send(cid, "Host: ", 5); + send(cid, host.getName(), strlen(host.getName())); + send(cid, "\r\n", 2); + } + if (user && pwd) { + snprintf(tmp, sizeof(tmp), "%s:%s", user, pwd); + base64encode(tmp, strlen(tmp), cmd, sizeof(cmd)); + send(cid, "Authorization: Basic ", 21); + send(cid, cmd, strlen(cmd)); + send(cid, "\r\n", 2); + } + send(cid, "Upgrade: websocket\r\n", 20); + send(cid, "Connection: Upgrade\r\n", 21); + send(cid, "Sec-WebSocket-Key: ", 19); + getMacAddress(tmp); + memcpy(&tmp[6], host.getName(), 10); + base64encode(tmp, 16, cmd, sizeof(cmd)); + send(cid, cmd, strlen(cmd)); + send(cid, "\r\n", 2); + send(cid, "Sec-WebSocket-Version: 13\r\n", 27); + send(cid, "\r\n", 2); + + if (wait_ws(cid, 101)) { + close(cid); + return -1; + } + wait_ws(cid, 0); + + _gs_sock[cid].onGsReceive.attach(ponGsReceive); + return cid; +} + +int GSwifi::wait_ws (int cid, int code) { + Timer timeout; + int i, n, len; + char buf[200], data[100]; + + if (code == 0) { + timeout.start(); + while (timeout.read_ms() < GS_TIMEOUT) { + wait_ms(10); + if (_gs_sock[cid].data->isEmpty()) break; + poll(); + n = recv(cid, buf, sizeof(buf)); + if (n <= 0) break; + } + timeout.stop(); + return 0; + } + + // wait responce + len = 0; + timeout.start(); + while (timeout.read_ms() < GS_TIMEOUT) { + wait_ms(10); + poll(); + n = recv(cid, buf, sizeof(buf)); + for (i = 0; i < n; i ++) { + if (buf[i] == '\r') continue; + if (buf[i] == '\n') { + if (len == 0) continue; + goto next; + } else + if (len < sizeof(data) - 1) { + data[len] = buf[i]; + len ++; + } + } + } +next: + data[len] = 0; + DBG("ws: %s\r\n", data); + timeout.stop(); + + // check return code + if (strncmp(data, "HTTP/1.1 ", 9) != 0) return -1; + i = atoi(&data[9]); + DBG("ws status %d\r\n", i); + if (i == code) return 0; + + return -1; +} + +int GSwifi::wsSend (int cid, const char *buf, int len, const char *mask) { + int r; + char tmp[10]; + + tmp[0] = 0x82; // single, binary frame + tmp[1] = (mask == NULL) ? 0 : 0x80; + + if (len < 126) { + tmp[1] |= len; + r = send(cid, tmp, 2); + } else { + tmp[1] |= 126; + tmp[2] = (len >> 8) & 0xff; + tmp[3] = len & 0xff; + r = send(cid, tmp, 4); + } + + if (r == 0) { + if (mask) { + int i; + char tmp2[len]; + send(cid, mask, 4); + for (i = 0; i < len; i ++) { + tmp2[i] = buf[i] ^ mask[i & 0x03]; + } + r = send(cid, tmp2, len); + } else { + r = send(cid, buf, len); + } + } + return r; }