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@43:0b5e2727e020, 2013-12-18 (annotated)
- Committer:
- gsfan
- Date:
- Wed Dec 18 01:29:43 2013 +0000
- Revision:
- 43:0b5e2727e020
- Parent:
- 37:e61ea8267415
fix reconnect
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 | 35:515ec79792d3 | 23 | #include "GSwifi_conf.h" |
gsfan | 35:515ec79792d3 | 24 | #ifdef GS_ENABLE_HTTP |
gsfan | 35:515ec79792d3 | 25 | |
gsfan | 25:f6e5622d2930 | 26 | #include "dbg.h" |
gsfan | 25:f6e5622d2930 | 27 | #include "mbed.h" |
gsfan | 25:f6e5622d2930 | 28 | #include "GSwifi.h" |
gsfan | 29:1c4419512941 | 29 | #include <string.h> |
gsfan | 25:f6e5622d2930 | 30 | |
gsfan | 25:f6e5622d2930 | 31 | |
gsfan | 25:f6e5622d2930 | 32 | int GSwifi::httpGet (Host &host, const char *uri, const char *user, const char *pwd, int ssl, onGsReceiveFunc ponGsReceive) { |
gsfan | 25:f6e5622d2930 | 33 | char cmd[GS_CMD_SIZE]; |
gsfan | 25:f6e5622d2930 | 34 | |
gsfan | 25:f6e5622d2930 | 35 | if (! _connect || _status != GSSTAT_READY) return -1; |
gsfan | 25:f6e5622d2930 | 36 | |
gsfan | 25:f6e5622d2930 | 37 | if (host.getIp().isNull()) { |
gsfan | 25:f6e5622d2930 | 38 | if (getHostByName(host)) { |
gsfan | 25:f6e5622d2930 | 39 | if (getHostByName(host)) return -1; |
gsfan | 25:f6e5622d2930 | 40 | } |
gsfan | 25:f6e5622d2930 | 41 | } |
gsfan | 25:f6e5622d2930 | 42 | if (host.getPort() == 0) { |
gsfan | 25:f6e5622d2930 | 43 | if (ssl) { |
gsfan | 25:f6e5622d2930 | 44 | host.setPort(443); |
gsfan | 25:f6e5622d2930 | 45 | } else { |
gsfan | 25:f6e5622d2930 | 46 | host.setPort(80); |
gsfan | 25:f6e5622d2930 | 47 | } |
gsfan | 25:f6e5622d2930 | 48 | } |
gsfan | 25:f6e5622d2930 | 49 | |
gsfan | 25:f6e5622d2930 | 50 | command("AT+HTTPCONF=3,close", GSRES_NORMAL); // Connection: |
gsfan | 25:f6e5622d2930 | 51 | sprintf(cmd, "AT+HTTPCONF=11,%s", host.getName()); // Host: |
gsfan | 25:f6e5622d2930 | 52 | command(cmd, GSRES_NORMAL); |
gsfan | 25:f6e5622d2930 | 53 | if (user && pwd) { |
gsfan | 25:f6e5622d2930 | 54 | char tmp[GS_CMD_SIZE], tmp2[GS_CMD_SIZE]; |
gsfan | 25:f6e5622d2930 | 55 | snprintf(tmp, sizeof(tmp), "%s:%s", user, pwd); |
gsfan | 25:f6e5622d2930 | 56 | base64encode(tmp, strlen(tmp), tmp2, sizeof(tmp2)); |
gsfan | 25:f6e5622d2930 | 57 | sprintf(cmd, "AT+HTTPCONF=2,Basic %s", tmp2); // Authorization: |
gsfan | 25:f6e5622d2930 | 58 | command(cmd, GSRES_NORMAL); |
gsfan | 25:f6e5622d2930 | 59 | } else { |
gsfan | 25:f6e5622d2930 | 60 | command("AT+HTTPCONFDEL=2", GSRES_NORMAL); |
gsfan | 25:f6e5622d2930 | 61 | } |
gsfan | 25:f6e5622d2930 | 62 | command("AT+HTTPCONFDEL=5", GSRES_NORMAL); |
gsfan | 25:f6e5622d2930 | 63 | command("AT+HTTPCONFDEL=7", GSRES_NORMAL); |
gsfan | 25:f6e5622d2930 | 64 | |
gsfan | 25:f6e5622d2930 | 65 | 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 | 66 | if (ssl) { |
gsfan | 25:f6e5622d2930 | 67 | strcat(cmd, ",1"); |
gsfan | 25:f6e5622d2930 | 68 | } |
gsfan | 25:f6e5622d2930 | 69 | if (command(cmd, GSRES_HTTP)) return -1; |
gsfan | 25:f6e5622d2930 | 70 | newSock(_cid, GSTYPE_CLIENT, GSPROT_HTTPGET, ponGsReceive); |
gsfan | 25:f6e5622d2930 | 71 | |
gsfan | 25:f6e5622d2930 | 72 | sprintf(cmd, "AT+HTTPSEND=%d,1,%d,%s", _cid, GS_TIMEOUT / 1000, uri); // GET |
gsfan | 25:f6e5622d2930 | 73 | command(cmd, GSRES_NORMAL); |
gsfan | 25:f6e5622d2930 | 74 | |
gsfan | 25:f6e5622d2930 | 75 | return _cid; |
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 | 37:e61ea8267415 | 124 | if (acquireUart()) return -1; |
gsfan | 36:a70b11e1560f | 125 | sprintf(cmd, "\x1bH%X", _cid); |
gsfan | 36:a70b11e1560f | 126 | _gs_puts(cmd); |
gsfan | 25:f6e5622d2930 | 127 | for (i = 0; i < len; i ++) { |
gsfan | 25:f6e5622d2930 | 128 | _gs_putc(body[i]); |
gsfan | 25:f6e5622d2930 | 129 | DBG("%c", body[i]); |
gsfan | 25:f6e5622d2930 | 130 | } |
gsfan | 37:e61ea8267415 | 131 | releaseUart(); |
gsfan | 25:f6e5622d2930 | 132 | |
gsfan | 25:f6e5622d2930 | 133 | return _cid; |
gsfan | 25:f6e5622d2930 | 134 | } |
gsfan | 25:f6e5622d2930 | 135 | |
gsfan | 29:1c4419512941 | 136 | int GSwifi::wsOpen (Host &host, const char *uri, const char *user, const char *pwd, onGsReceiveFunc ponGsReceive) { |
gsfan | 29:1c4419512941 | 137 | int cid; |
gsfan | 29:1c4419512941 | 138 | char cmd[GS_CMD_SIZE], tmp[GS_CMD_SIZE]; |
gsfan | 29:1c4419512941 | 139 | |
gsfan | 29:1c4419512941 | 140 | if (! _connect || _status != GSSTAT_READY) return -1; |
gsfan | 29:1c4419512941 | 141 | |
gsfan | 29:1c4419512941 | 142 | if (host.getIp().isNull()) { |
gsfan | 29:1c4419512941 | 143 | if (getHostByName(host)) { |
gsfan | 29:1c4419512941 | 144 | if (getHostByName(host)) return -1; |
gsfan | 29:1c4419512941 | 145 | } |
gsfan | 29:1c4419512941 | 146 | } |
gsfan | 29:1c4419512941 | 147 | if (host.getPort() == 0) { |
gsfan | 29:1c4419512941 | 148 | host.setPort(80); |
gsfan | 29:1c4419512941 | 149 | } |
gsfan | 29:1c4419512941 | 150 | |
gsfan | 29:1c4419512941 | 151 | cid = open(host, GSPROT_TCP); |
gsfan | 29:1c4419512941 | 152 | if (cid < 0) return -1; |
gsfan | 29:1c4419512941 | 153 | DBG("cid %d\r\n", cid); |
gsfan | 29:1c4419512941 | 154 | |
gsfan | 29:1c4419512941 | 155 | // send request |
gsfan | 29:1c4419512941 | 156 | send(cid, "GET ", 4); |
gsfan | 29:1c4419512941 | 157 | send(cid, uri, strlen(uri)); |
gsfan | 29:1c4419512941 | 158 | send(cid, " HTTP/1.1\r\n", 11); |
gsfan | 29:1c4419512941 | 159 | if (host.getName() && host.getName()[0] != 0) { |
gsfan | 29:1c4419512941 | 160 | send(cid, "Host: ", 5); |
gsfan | 29:1c4419512941 | 161 | send(cid, host.getName(), strlen(host.getName())); |
gsfan | 29:1c4419512941 | 162 | send(cid, "\r\n", 2); |
gsfan | 29:1c4419512941 | 163 | } |
gsfan | 29:1c4419512941 | 164 | if (user && pwd) { |
gsfan | 29:1c4419512941 | 165 | snprintf(tmp, sizeof(tmp), "%s:%s", user, pwd); |
gsfan | 29:1c4419512941 | 166 | base64encode(tmp, strlen(tmp), cmd, sizeof(cmd)); |
gsfan | 29:1c4419512941 | 167 | send(cid, "Authorization: Basic ", 21); |
gsfan | 29:1c4419512941 | 168 | send(cid, cmd, strlen(cmd)); |
gsfan | 29:1c4419512941 | 169 | send(cid, "\r\n", 2); |
gsfan | 29:1c4419512941 | 170 | } |
gsfan | 29:1c4419512941 | 171 | send(cid, "Upgrade: websocket\r\n", 20); |
gsfan | 29:1c4419512941 | 172 | send(cid, "Connection: Upgrade\r\n", 21); |
gsfan | 29:1c4419512941 | 173 | send(cid, "Sec-WebSocket-Key: ", 19); |
gsfan | 29:1c4419512941 | 174 | getMacAddress(tmp); |
gsfan | 29:1c4419512941 | 175 | memcpy(&tmp[6], host.getName(), 10); |
gsfan | 29:1c4419512941 | 176 | base64encode(tmp, 16, cmd, sizeof(cmd)); |
gsfan | 29:1c4419512941 | 177 | send(cid, cmd, strlen(cmd)); |
gsfan | 29:1c4419512941 | 178 | send(cid, "\r\n", 2); |
gsfan | 29:1c4419512941 | 179 | send(cid, "Sec-WebSocket-Version: 13\r\n", 27); |
gsfan | 29:1c4419512941 | 180 | send(cid, "\r\n", 2); |
gsfan | 29:1c4419512941 | 181 | |
gsfan | 29:1c4419512941 | 182 | if (wait_ws(cid, 101)) { |
gsfan | 29:1c4419512941 | 183 | close(cid); |
gsfan | 29:1c4419512941 | 184 | return -1; |
gsfan | 29:1c4419512941 | 185 | } |
gsfan | 29:1c4419512941 | 186 | wait_ws(cid, 0); |
gsfan | 29:1c4419512941 | 187 | |
gsfan | 29:1c4419512941 | 188 | _gs_sock[cid].onGsReceive.attach(ponGsReceive); |
gsfan | 29:1c4419512941 | 189 | return cid; |
gsfan | 29:1c4419512941 | 190 | } |
gsfan | 29:1c4419512941 | 191 | |
gsfan | 29:1c4419512941 | 192 | int GSwifi::wait_ws (int cid, int code) { |
gsfan | 29:1c4419512941 | 193 | Timer timeout; |
gsfan | 29:1c4419512941 | 194 | int i, n, len; |
gsfan | 29:1c4419512941 | 195 | char buf[200], data[100]; |
gsfan | 29:1c4419512941 | 196 | |
gsfan | 29:1c4419512941 | 197 | if (code == 0) { |
gsfan | 32:e19538c1f13d | 198 | // dummy read |
gsfan | 29:1c4419512941 | 199 | timeout.start(); |
gsfan | 29:1c4419512941 | 200 | while (timeout.read_ms() < GS_TIMEOUT) { |
gsfan | 29:1c4419512941 | 201 | wait_ms(10); |
gsfan | 29:1c4419512941 | 202 | if (_gs_sock[cid].data->isEmpty()) break; |
gsfan | 29:1c4419512941 | 203 | poll(); |
gsfan | 29:1c4419512941 | 204 | n = recv(cid, buf, sizeof(buf)); |
gsfan | 29:1c4419512941 | 205 | if (n <= 0) break; |
gsfan | 29:1c4419512941 | 206 | } |
gsfan | 29:1c4419512941 | 207 | timeout.stop(); |
gsfan | 29:1c4419512941 | 208 | return 0; |
gsfan | 29:1c4419512941 | 209 | } |
gsfan | 29:1c4419512941 | 210 | |
gsfan | 29:1c4419512941 | 211 | // wait responce |
gsfan | 29:1c4419512941 | 212 | len = 0; |
gsfan | 29:1c4419512941 | 213 | timeout.start(); |
gsfan | 29:1c4419512941 | 214 | while (timeout.read_ms() < GS_TIMEOUT) { |
gsfan | 29:1c4419512941 | 215 | wait_ms(10); |
gsfan | 29:1c4419512941 | 216 | poll(); |
gsfan | 29:1c4419512941 | 217 | n = recv(cid, buf, sizeof(buf)); |
gsfan | 29:1c4419512941 | 218 | for (i = 0; i < n; i ++) { |
gsfan | 29:1c4419512941 | 219 | if (buf[i] == '\r') continue; |
gsfan | 29:1c4419512941 | 220 | if (buf[i] == '\n') { |
gsfan | 29:1c4419512941 | 221 | if (len == 0) continue; |
gsfan | 29:1c4419512941 | 222 | goto next; |
gsfan | 29:1c4419512941 | 223 | } else |
gsfan | 29:1c4419512941 | 224 | if (len < sizeof(data) - 1) { |
gsfan | 29:1c4419512941 | 225 | data[len] = buf[i]; |
gsfan | 29:1c4419512941 | 226 | len ++; |
gsfan | 29:1c4419512941 | 227 | } |
gsfan | 29:1c4419512941 | 228 | } |
gsfan | 29:1c4419512941 | 229 | } |
gsfan | 29:1c4419512941 | 230 | next: |
gsfan | 29:1c4419512941 | 231 | data[len] = 0; |
gsfan | 29:1c4419512941 | 232 | DBG("ws: %s\r\n", data); |
gsfan | 29:1c4419512941 | 233 | timeout.stop(); |
gsfan | 29:1c4419512941 | 234 | |
gsfan | 29:1c4419512941 | 235 | // check return code |
gsfan | 29:1c4419512941 | 236 | if (strncmp(data, "HTTP/1.1 ", 9) != 0) return -1; |
gsfan | 29:1c4419512941 | 237 | i = atoi(&data[9]); |
gsfan | 29:1c4419512941 | 238 | DBG("ws status %d\r\n", i); |
gsfan | 29:1c4419512941 | 239 | if (i == code) return 0; |
gsfan | 29:1c4419512941 | 240 | |
gsfan | 29:1c4419512941 | 241 | return -1; |
gsfan | 29:1c4419512941 | 242 | } |
gsfan | 29:1c4419512941 | 243 | |
gsfan | 29:1c4419512941 | 244 | int GSwifi::wsSend (int cid, const char *buf, int len, const char *mask) { |
gsfan | 29:1c4419512941 | 245 | int r; |
gsfan | 29:1c4419512941 | 246 | char tmp[10]; |
gsfan | 29:1c4419512941 | 247 | |
gsfan | 30:f3e9053d9b46 | 248 | tmp[0] = 0x81; // single, text frame |
gsfan | 29:1c4419512941 | 249 | tmp[1] = (mask == NULL) ? 0 : 0x80; |
gsfan | 29:1c4419512941 | 250 | |
gsfan | 29:1c4419512941 | 251 | if (len < 126) { |
gsfan | 29:1c4419512941 | 252 | tmp[1] |= len; |
gsfan | 29:1c4419512941 | 253 | r = send(cid, tmp, 2); |
gsfan | 29:1c4419512941 | 254 | } else { |
gsfan | 29:1c4419512941 | 255 | tmp[1] |= 126; |
gsfan | 29:1c4419512941 | 256 | tmp[2] = (len >> 8) & 0xff; |
gsfan | 29:1c4419512941 | 257 | tmp[3] = len & 0xff; |
gsfan | 29:1c4419512941 | 258 | r = send(cid, tmp, 4); |
gsfan | 29:1c4419512941 | 259 | } |
gsfan | 29:1c4419512941 | 260 | |
gsfan | 29:1c4419512941 | 261 | if (r == 0) { |
gsfan | 29:1c4419512941 | 262 | if (mask) { |
gsfan | 29:1c4419512941 | 263 | int i; |
gsfan | 29:1c4419512941 | 264 | char tmp2[len]; |
gsfan | 29:1c4419512941 | 265 | send(cid, mask, 4); |
gsfan | 29:1c4419512941 | 266 | for (i = 0; i < len; i ++) { |
gsfan | 29:1c4419512941 | 267 | tmp2[i] = buf[i] ^ mask[i & 0x03]; |
gsfan | 29:1c4419512941 | 268 | } |
gsfan | 29:1c4419512941 | 269 | r = send(cid, tmp2, len); |
gsfan | 29:1c4419512941 | 270 | } else { |
gsfan | 29:1c4419512941 | 271 | r = send(cid, buf, len); |
gsfan | 29:1c4419512941 | 272 | } |
gsfan | 29:1c4419512941 | 273 | } |
gsfan | 29:1c4419512941 | 274 | return r; |
gsfan | 25:f6e5622d2930 | 275 | } |
gsfan | 25:f6e5622d2930 | 276 | |
gsfan | 25:f6e5622d2930 | 277 | |
gsfan | 25:f6e5622d2930 | 278 | /* base64encode code from |
gsfan | 25:f6e5622d2930 | 279 | * Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) |
gsfan | 25:f6e5622d2930 | 280 | */ |
gsfan | 25:f6e5622d2930 | 281 | int GSwifi::base64encode (char *input, int length, char *output, int len) { |
gsfan | 25:f6e5622d2930 | 282 | static const char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
gsfan | 25:f6e5622d2930 | 283 | unsigned int c, c1, c2, c3; |
gsfan | 25:f6e5622d2930 | 284 | |
gsfan | 25:f6e5622d2930 | 285 | if (len < ((((length-1)/3)+1)<<2)) return -1; |
gsfan | 25:f6e5622d2930 | 286 | for(unsigned int i = 0, j = 0; i<length; i+=3,j+=4) { |
gsfan | 25:f6e5622d2930 | 287 | c1 = ((((unsigned char)*((unsigned char *)&input[i])))); |
gsfan | 25:f6e5622d2930 | 288 | c2 = (length>i+1)?((((unsigned char)*((unsigned char *)&input[i+1])))):0; |
gsfan | 25:f6e5622d2930 | 289 | c3 = (length>i+2)?((((unsigned char)*((unsigned char *)&input[i+2])))):0; |
gsfan | 25:f6e5622d2930 | 290 | |
gsfan | 25:f6e5622d2930 | 291 | c = ((c1 & 0xFC) >> 2); |
gsfan | 25:f6e5622d2930 | 292 | output[j+0] = base64[c]; |
gsfan | 25:f6e5622d2930 | 293 | c = ((c1 & 0x03) << 4) | ((c2 & 0xF0) >> 4); |
gsfan | 25:f6e5622d2930 | 294 | output[j+1] = base64[c]; |
gsfan | 25:f6e5622d2930 | 295 | c = ((c2 & 0x0F) << 2) | ((c3 & 0xC0) >> 6); |
gsfan | 25:f6e5622d2930 | 296 | output[j+2] = (length>i+1)?base64[c]:'='; |
gsfan | 25:f6e5622d2930 | 297 | c = (c3 & 0x3F); |
gsfan | 25:f6e5622d2930 | 298 | output[j+3] = (length>i+2)?base64[c]:'='; |
gsfan | 25:f6e5622d2930 | 299 | } |
gsfan | 25:f6e5622d2930 | 300 | output[(((length-1)/3)+1)<<2] = '\0'; |
gsfan | 25:f6e5622d2930 | 301 | return 0; |
gsfan | 25:f6e5622d2930 | 302 | } |
gsfan | 25:f6e5622d2930 | 303 | |
gsfan | 25:f6e5622d2930 | 304 | /* urlencode code from |
gsfan | 25:f6e5622d2930 | 305 | * Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) |
gsfan | 25:f6e5622d2930 | 306 | */ |
gsfan | 25:f6e5622d2930 | 307 | int GSwifi::urlencode (char *str, char *buf, int len) { |
gsfan | 25:f6e5622d2930 | 308 | // char *pstr = str, *buf = (char*)malloc(strlen(str) * 3 + 1), *pbuf = buf; |
gsfan | 25:f6e5622d2930 | 309 | char *pstr = str, *pbuf = buf; |
gsfan | 25:f6e5622d2930 | 310 | |
gsfan | 25:f6e5622d2930 | 311 | if (len < (strlen(str) * 3 + 1)) return -1; |
gsfan | 25:f6e5622d2930 | 312 | while (*pstr) { |
gsfan | 25:f6e5622d2930 | 313 | if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~') |
gsfan | 25:f6e5622d2930 | 314 | *pbuf++ = *pstr; |
gsfan | 25:f6e5622d2930 | 315 | else if (*pstr == ' ') |
gsfan | 25:f6e5622d2930 | 316 | *pbuf++ = '+'; |
gsfan | 25:f6e5622d2930 | 317 | else |
gsfan | 25:f6e5622d2930 | 318 | *pbuf++ = '%', *pbuf++ = to_hex(*pstr >> 4), *pbuf++ = to_hex(*pstr & 15); |
gsfan | 25:f6e5622d2930 | 319 | pstr++; |
gsfan | 25:f6e5622d2930 | 320 | } |
gsfan | 25:f6e5622d2930 | 321 | *pbuf = '\0'; |
gsfan | 25:f6e5622d2930 | 322 | return 0; |
gsfan | 25:f6e5622d2930 | 323 | } |
gsfan | 25:f6e5622d2930 | 324 | |
gsfan | 25:f6e5622d2930 | 325 | /* urldecode code from |
gsfan | 25:f6e5622d2930 | 326 | * Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) |
gsfan | 25:f6e5622d2930 | 327 | */ |
gsfan | 25:f6e5622d2930 | 328 | int GSwifi::urldecode (char *str, char *buf, int len) { |
gsfan | 25:f6e5622d2930 | 329 | // char *pstr = str, *buf = (char*)malloc(strlen(str) + 1), *pbuf = buf; |
gsfan | 25:f6e5622d2930 | 330 | char *pstr = str, *pbuf = buf; |
gsfan | 25:f6e5622d2930 | 331 | |
gsfan | 25:f6e5622d2930 | 332 | if (len < (strlen(str) / 3 - 1)) return -1; |
gsfan | 25:f6e5622d2930 | 333 | while (*pstr) { |
gsfan | 25:f6e5622d2930 | 334 | if (*pstr == '%') { |
gsfan | 25:f6e5622d2930 | 335 | if (pstr[1] && pstr[2]) { |
gsfan | 25:f6e5622d2930 | 336 | *pbuf++ = from_hex(pstr[1]) << 4 | from_hex(pstr[2]); |
gsfan | 25:f6e5622d2930 | 337 | pstr += 2; |
gsfan | 25:f6e5622d2930 | 338 | } |
gsfan | 25:f6e5622d2930 | 339 | } else if (*pstr == '+') { |
gsfan | 25:f6e5622d2930 | 340 | *pbuf++ = ' '; |
gsfan | 25:f6e5622d2930 | 341 | } else { |
gsfan | 25:f6e5622d2930 | 342 | *pbuf++ = *pstr; |
gsfan | 25:f6e5622d2930 | 343 | } |
gsfan | 25:f6e5622d2930 | 344 | pstr++; |
gsfan | 25:f6e5622d2930 | 345 | } |
gsfan | 25:f6e5622d2930 | 346 | *pbuf = '\0'; |
gsfan | 25:f6e5622d2930 | 347 | return 0; |
gsfan | 25:f6e5622d2930 | 348 | } |
gsfan | 35:515ec79792d3 | 349 | |
gsfan | 35:515ec79792d3 | 350 | #endif |