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/
GSwifi_http.cpp@25:f6e5622d2930, 2013-02-11 (annotated)
- Committer:
- gsfan
- Date:
- Mon Feb 11 06:01:46 2013 +0000
- Revision:
- 25:f6e5622d2930
- Child:
- 29:1c4419512941
split the file
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gsfan | 25:f6e5622d2930 | 1 | /* Copyright (C) 2013 gsfan, MIT License |
gsfan | 25:f6e5622d2930 | 2 | * |
gsfan | 25:f6e5622d2930 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
gsfan | 25:f6e5622d2930 | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
gsfan | 25:f6e5622d2930 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
gsfan | 25:f6e5622d2930 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
gsfan | 25:f6e5622d2930 | 7 | * furnished to do so, subject to the following conditions: |
gsfan | 25:f6e5622d2930 | 8 | * |
gsfan | 25:f6e5622d2930 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
gsfan | 25:f6e5622d2930 | 10 | * substantial portions of the Software. |
gsfan | 25:f6e5622d2930 | 11 | * |
gsfan | 25:f6e5622d2930 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
gsfan | 25:f6e5622d2930 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
gsfan | 25:f6e5622d2930 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
gsfan | 25:f6e5622d2930 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
gsfan | 25:f6e5622d2930 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
gsfan | 25:f6e5622d2930 | 17 | */ |
gsfan | 25:f6e5622d2930 | 18 | /** @file |
gsfan | 25:f6e5622d2930 | 19 | * @brief Gainspan wi-fi module library for mbed |
gsfan | 25:f6e5622d2930 | 20 | * GS1011MIC, GS1011MIP, GainSpan WiFi Breakout, etc. |
gsfan | 25:f6e5622d2930 | 21 | */ |
gsfan | 25:f6e5622d2930 | 22 | |
gsfan | 25:f6e5622d2930 | 23 | #include "dbg.h" |
gsfan | 25:f6e5622d2930 | 24 | #include "mbed.h" |
gsfan | 25:f6e5622d2930 | 25 | #include "GSwifi.h" |
gsfan | 25:f6e5622d2930 | 26 | |
gsfan | 25:f6e5622d2930 | 27 | |
gsfan | 25:f6e5622d2930 | 28 | int GSwifi::httpGet (Host &host, const char *uri, const char *user, const char *pwd, int ssl, onGsReceiveFunc ponGsReceive) { |
gsfan | 25:f6e5622d2930 | 29 | char cmd[GS_CMD_SIZE]; |
gsfan | 25:f6e5622d2930 | 30 | |
gsfan | 25:f6e5622d2930 | 31 | if (! _connect || _status != GSSTAT_READY) return -1; |
gsfan | 25:f6e5622d2930 | 32 | |
gsfan | 25:f6e5622d2930 | 33 | if (host.getIp().isNull()) { |
gsfan | 25:f6e5622d2930 | 34 | if (getHostByName(host)) { |
gsfan | 25:f6e5622d2930 | 35 | if (getHostByName(host)) return -1; |
gsfan | 25:f6e5622d2930 | 36 | } |
gsfan | 25:f6e5622d2930 | 37 | } |
gsfan | 25:f6e5622d2930 | 38 | if (host.getPort() == 0) { |
gsfan | 25:f6e5622d2930 | 39 | if (ssl) { |
gsfan | 25:f6e5622d2930 | 40 | host.setPort(443); |
gsfan | 25:f6e5622d2930 | 41 | } else { |
gsfan | 25:f6e5622d2930 | 42 | host.setPort(80); |
gsfan | 25:f6e5622d2930 | 43 | } |
gsfan | 25:f6e5622d2930 | 44 | } |
gsfan | 25:f6e5622d2930 | 45 | |
gsfan | 25:f6e5622d2930 | 46 | command("AT+HTTPCONF=3,close", GSRES_NORMAL); // Connection: |
gsfan | 25:f6e5622d2930 | 47 | sprintf(cmd, "AT+HTTPCONF=11,%s", host.getName()); // Host: |
gsfan | 25:f6e5622d2930 | 48 | command(cmd, GSRES_NORMAL); |
gsfan | 25:f6e5622d2930 | 49 | if (user && pwd) { |
gsfan | 25:f6e5622d2930 | 50 | char tmp[GS_CMD_SIZE], tmp2[GS_CMD_SIZE]; |
gsfan | 25:f6e5622d2930 | 51 | snprintf(tmp, sizeof(tmp), "%s:%s", user, pwd); |
gsfan | 25:f6e5622d2930 | 52 | base64encode(tmp, strlen(tmp), tmp2, sizeof(tmp2)); |
gsfan | 25:f6e5622d2930 | 53 | sprintf(cmd, "AT+HTTPCONF=2,Basic %s", tmp2); // Authorization: |
gsfan | 25:f6e5622d2930 | 54 | command(cmd, GSRES_NORMAL); |
gsfan | 25:f6e5622d2930 | 55 | } else { |
gsfan | 25:f6e5622d2930 | 56 | command("AT+HTTPCONFDEL=2", GSRES_NORMAL); |
gsfan | 25:f6e5622d2930 | 57 | } |
gsfan | 25:f6e5622d2930 | 58 | command("AT+HTTPCONFDEL=5", GSRES_NORMAL); |
gsfan | 25:f6e5622d2930 | 59 | command("AT+HTTPCONFDEL=7", GSRES_NORMAL); |
gsfan | 25:f6e5622d2930 | 60 | |
gsfan | 25:f6e5622d2930 | 61 | sprintf(cmd, "AT+HTTPOPEN=%d.%d.%d.%d,%d", host.getIp()[0], host.getIp()[1], host.getIp()[2], host.getIp()[3], host.getPort()); |
gsfan | 25:f6e5622d2930 | 62 | if (ssl) { |
gsfan | 25:f6e5622d2930 | 63 | strcat(cmd, ",1"); |
gsfan | 25:f6e5622d2930 | 64 | } |
gsfan | 25:f6e5622d2930 | 65 | if (command(cmd, GSRES_HTTP)) return -1; |
gsfan | 25:f6e5622d2930 | 66 | newSock(_cid, GSTYPE_CLIENT, GSPROT_HTTPGET, ponGsReceive); |
gsfan | 25:f6e5622d2930 | 67 | |
gsfan | 25:f6e5622d2930 | 68 | sprintf(cmd, "AT+HTTPSEND=%d,1,%d,%s", _cid, GS_TIMEOUT / 1000, uri); // GET |
gsfan | 25:f6e5622d2930 | 69 | command(cmd, GSRES_NORMAL); |
gsfan | 25:f6e5622d2930 | 70 | |
gsfan | 25:f6e5622d2930 | 71 | return _cid; |
gsfan | 25:f6e5622d2930 | 72 | } |
gsfan | 25:f6e5622d2930 | 73 | |
gsfan | 25:f6e5622d2930 | 74 | int GSwifi::httpGet (Host &host, const char *uri, int ssl, onGsReceiveFunc ponGsReceive) { |
gsfan | 25:f6e5622d2930 | 75 | return httpGet (host, uri, NULL, NULL, ssl, ponGsReceive); |
gsfan | 25:f6e5622d2930 | 76 | } |
gsfan | 25:f6e5622d2930 | 77 | |
gsfan | 25:f6e5622d2930 | 78 | int GSwifi::httpPost (Host &host, const char *uri, const char *body, const char *user, const char *pwd, int ssl, onGsReceiveFunc ponGsReceive) { |
gsfan | 25:f6e5622d2930 | 79 | char cmd[GS_CMD_SIZE]; |
gsfan | 25:f6e5622d2930 | 80 | int i, len; |
gsfan | 25:f6e5622d2930 | 81 | |
gsfan | 25:f6e5622d2930 | 82 | if (! _connect || _status != GSSTAT_READY) return -1; |
gsfan | 25:f6e5622d2930 | 83 | |
gsfan | 25:f6e5622d2930 | 84 | if (host.getIp().isNull()) { |
gsfan | 25:f6e5622d2930 | 85 | if (getHostByName(host)) { |
gsfan | 25:f6e5622d2930 | 86 | if (getHostByName(host)) return -1; |
gsfan | 25:f6e5622d2930 | 87 | } |
gsfan | 25:f6e5622d2930 | 88 | } |
gsfan | 25:f6e5622d2930 | 89 | if (host.getPort() == 0) { |
gsfan | 25:f6e5622d2930 | 90 | if (ssl) { |
gsfan | 25:f6e5622d2930 | 91 | host.setPort(443); |
gsfan | 25:f6e5622d2930 | 92 | } else { |
gsfan | 25:f6e5622d2930 | 93 | host.setPort(80); |
gsfan | 25:f6e5622d2930 | 94 | } |
gsfan | 25:f6e5622d2930 | 95 | } |
gsfan | 25:f6e5622d2930 | 96 | len = strlen(body); |
gsfan | 25:f6e5622d2930 | 97 | |
gsfan | 25:f6e5622d2930 | 98 | command("AT+HTTPCONF=3,close", GSRES_NORMAL); // Connection: |
gsfan | 25:f6e5622d2930 | 99 | sprintf(cmd, "AT+HTTPCONF=11,%s", host.getName()); // Host: |
gsfan | 25:f6e5622d2930 | 100 | command(cmd, GSRES_NORMAL); |
gsfan | 25:f6e5622d2930 | 101 | sprintf(cmd, "AT+HTTPCONF=5,%d", len); // Content-Length: |
gsfan | 25:f6e5622d2930 | 102 | command(cmd, GSRES_NORMAL); |
gsfan | 25:f6e5622d2930 | 103 | command("AT+HTTPCONF=7,application/x-www-form-urlencoded", GSRES_NORMAL); // Content-type: |
gsfan | 25:f6e5622d2930 | 104 | if (user && pwd) { |
gsfan | 25:f6e5622d2930 | 105 | char tmp[GS_CMD_SIZE], tmp2[GS_CMD_SIZE]; |
gsfan | 25:f6e5622d2930 | 106 | snprintf(tmp, sizeof(tmp), "%s:%s", user, pwd); |
gsfan | 25:f6e5622d2930 | 107 | base64encode(tmp, strlen(tmp), tmp2, sizeof(tmp2)); |
gsfan | 25:f6e5622d2930 | 108 | sprintf(cmd, "AT+HTTPCONF=2,Basic %s", tmp2); // Authorization: |
gsfan | 25:f6e5622d2930 | 109 | command(cmd, GSRES_NORMAL); |
gsfan | 25:f6e5622d2930 | 110 | } else { |
gsfan | 25:f6e5622d2930 | 111 | command("AT+HTTPCONFDEL=2", GSRES_NORMAL); |
gsfan | 25:f6e5622d2930 | 112 | } |
gsfan | 25:f6e5622d2930 | 113 | |
gsfan | 25:f6e5622d2930 | 114 | sprintf(cmd, "AT+HTTPOPEN=%d.%d.%d.%d,%d", host.getIp()[0], host.getIp()[1], host.getIp()[2], host.getIp()[3], host.getPort()); |
gsfan | 25:f6e5622d2930 | 115 | if (ssl) { |
gsfan | 25:f6e5622d2930 | 116 | strcat(cmd, ",1"); |
gsfan | 25:f6e5622d2930 | 117 | } |
gsfan | 25:f6e5622d2930 | 118 | if (command(cmd, GSRES_HTTP)) return -1; |
gsfan | 25:f6e5622d2930 | 119 | newSock(_cid, GSTYPE_CLIENT, GSPROT_HTTPPOST, ponGsReceive); |
gsfan | 25:f6e5622d2930 | 120 | |
gsfan | 25:f6e5622d2930 | 121 | sprintf(cmd, "AT+HTTPSEND=%d,3,%d,%s,%d", _cid, GS_TIMEOUT / 1000, uri, len); // POST |
gsfan | 25:f6e5622d2930 | 122 | command(cmd, GSRES_NORMAL); |
gsfan | 25:f6e5622d2930 | 123 | |
gsfan | 25:f6e5622d2930 | 124 | _gs.printf("\x1bH%X", _cid); |
gsfan | 25:f6e5622d2930 | 125 | for (i = 0; i < len; i ++) { |
gsfan | 25:f6e5622d2930 | 126 | _gs_putc(body[i]); |
gsfan | 25:f6e5622d2930 | 127 | DBG("%c", body[i]); |
gsfan | 25:f6e5622d2930 | 128 | } |
gsfan | 25:f6e5622d2930 | 129 | |
gsfan | 25:f6e5622d2930 | 130 | return _cid; |
gsfan | 25:f6e5622d2930 | 131 | } |
gsfan | 25:f6e5622d2930 | 132 | |
gsfan | 25:f6e5622d2930 | 133 | int GSwifi::httpPost (Host &host, const char *uri, const char *body, int ssl, onGsReceiveFunc ponGsReceive) { |
gsfan | 25:f6e5622d2930 | 134 | return httpPost (host, uri, body, NULL, NULL, ssl, ponGsReceive); |
gsfan | 25:f6e5622d2930 | 135 | } |
gsfan | 25:f6e5622d2930 | 136 | |
gsfan | 25:f6e5622d2930 | 137 | |
gsfan | 25:f6e5622d2930 | 138 | /* base64encode code from |
gsfan | 25:f6e5622d2930 | 139 | * Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) |
gsfan | 25:f6e5622d2930 | 140 | */ |
gsfan | 25:f6e5622d2930 | 141 | int GSwifi::base64encode (char *input, int length, char *output, int len) { |
gsfan | 25:f6e5622d2930 | 142 | static const char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
gsfan | 25:f6e5622d2930 | 143 | unsigned int c, c1, c2, c3; |
gsfan | 25:f6e5622d2930 | 144 | |
gsfan | 25:f6e5622d2930 | 145 | if (len < ((((length-1)/3)+1)<<2)) return -1; |
gsfan | 25:f6e5622d2930 | 146 | for(unsigned int i = 0, j = 0; i<length; i+=3,j+=4) { |
gsfan | 25:f6e5622d2930 | 147 | c1 = ((((unsigned char)*((unsigned char *)&input[i])))); |
gsfan | 25:f6e5622d2930 | 148 | c2 = (length>i+1)?((((unsigned char)*((unsigned char *)&input[i+1])))):0; |
gsfan | 25:f6e5622d2930 | 149 | c3 = (length>i+2)?((((unsigned char)*((unsigned char *)&input[i+2])))):0; |
gsfan | 25:f6e5622d2930 | 150 | |
gsfan | 25:f6e5622d2930 | 151 | c = ((c1 & 0xFC) >> 2); |
gsfan | 25:f6e5622d2930 | 152 | output[j+0] = base64[c]; |
gsfan | 25:f6e5622d2930 | 153 | c = ((c1 & 0x03) << 4) | ((c2 & 0xF0) >> 4); |
gsfan | 25:f6e5622d2930 | 154 | output[j+1] = base64[c]; |
gsfan | 25:f6e5622d2930 | 155 | c = ((c2 & 0x0F) << 2) | ((c3 & 0xC0) >> 6); |
gsfan | 25:f6e5622d2930 | 156 | output[j+2] = (length>i+1)?base64[c]:'='; |
gsfan | 25:f6e5622d2930 | 157 | c = (c3 & 0x3F); |
gsfan | 25:f6e5622d2930 | 158 | output[j+3] = (length>i+2)?base64[c]:'='; |
gsfan | 25:f6e5622d2930 | 159 | } |
gsfan | 25:f6e5622d2930 | 160 | output[(((length-1)/3)+1)<<2] = '\0'; |
gsfan | 25:f6e5622d2930 | 161 | return 0; |
gsfan | 25:f6e5622d2930 | 162 | } |
gsfan | 25:f6e5622d2930 | 163 | |
gsfan | 25:f6e5622d2930 | 164 | /* urlencode code from |
gsfan | 25:f6e5622d2930 | 165 | * Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) |
gsfan | 25:f6e5622d2930 | 166 | */ |
gsfan | 25:f6e5622d2930 | 167 | int GSwifi::urlencode (char *str, char *buf, int len) { |
gsfan | 25:f6e5622d2930 | 168 | // char *pstr = str, *buf = (char*)malloc(strlen(str) * 3 + 1), *pbuf = buf; |
gsfan | 25:f6e5622d2930 | 169 | char *pstr = str, *pbuf = buf; |
gsfan | 25:f6e5622d2930 | 170 | |
gsfan | 25:f6e5622d2930 | 171 | if (len < (strlen(str) * 3 + 1)) return -1; |
gsfan | 25:f6e5622d2930 | 172 | while (*pstr) { |
gsfan | 25:f6e5622d2930 | 173 | if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~') |
gsfan | 25:f6e5622d2930 | 174 | *pbuf++ = *pstr; |
gsfan | 25:f6e5622d2930 | 175 | else if (*pstr == ' ') |
gsfan | 25:f6e5622d2930 | 176 | *pbuf++ = '+'; |
gsfan | 25:f6e5622d2930 | 177 | else |
gsfan | 25:f6e5622d2930 | 178 | *pbuf++ = '%', *pbuf++ = to_hex(*pstr >> 4), *pbuf++ = to_hex(*pstr & 15); |
gsfan | 25:f6e5622d2930 | 179 | pstr++; |
gsfan | 25:f6e5622d2930 | 180 | } |
gsfan | 25:f6e5622d2930 | 181 | *pbuf = '\0'; |
gsfan | 25:f6e5622d2930 | 182 | return 0; |
gsfan | 25:f6e5622d2930 | 183 | } |
gsfan | 25:f6e5622d2930 | 184 | |
gsfan | 25:f6e5622d2930 | 185 | /* urldecode code from |
gsfan | 25:f6e5622d2930 | 186 | * Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) |
gsfan | 25:f6e5622d2930 | 187 | */ |
gsfan | 25:f6e5622d2930 | 188 | int GSwifi::urldecode (char *str, char *buf, int len) { |
gsfan | 25:f6e5622d2930 | 189 | // char *pstr = str, *buf = (char*)malloc(strlen(str) + 1), *pbuf = buf; |
gsfan | 25:f6e5622d2930 | 190 | char *pstr = str, *pbuf = buf; |
gsfan | 25:f6e5622d2930 | 191 | |
gsfan | 25:f6e5622d2930 | 192 | if (len < (strlen(str) / 3 - 1)) return -1; |
gsfan | 25:f6e5622d2930 | 193 | while (*pstr) { |
gsfan | 25:f6e5622d2930 | 194 | if (*pstr == '%') { |
gsfan | 25:f6e5622d2930 | 195 | if (pstr[1] && pstr[2]) { |
gsfan | 25:f6e5622d2930 | 196 | *pbuf++ = from_hex(pstr[1]) << 4 | from_hex(pstr[2]); |
gsfan | 25:f6e5622d2930 | 197 | pstr += 2; |
gsfan | 25:f6e5622d2930 | 198 | } |
gsfan | 25:f6e5622d2930 | 199 | } else if (*pstr == '+') { |
gsfan | 25:f6e5622d2930 | 200 | *pbuf++ = ' '; |
gsfan | 25:f6e5622d2930 | 201 | } else { |
gsfan | 25:f6e5622d2930 | 202 | *pbuf++ = *pstr; |
gsfan | 25:f6e5622d2930 | 203 | } |
gsfan | 25:f6e5622d2930 | 204 | pstr++; |
gsfan | 25:f6e5622d2930 | 205 | } |
gsfan | 25:f6e5622d2930 | 206 | *pbuf = '\0'; |
gsfan | 25:f6e5622d2930 | 207 | return 0; |
gsfan | 25:f6e5622d2930 | 208 | } |