GSwifiInterface library (interface for GainSpan Wi-Fi GS1011 modules) Please see https://mbed.org/users/gsfan/notebook/GSwifiInterface/
Dependents: GSwifiInterface_HelloWorld GSwifiInterface_HelloServo GSwifiInterface_UDPEchoServer GSwifiInterface_UDPEchoClient ... more
Fork of WiflyInterface by
GainSpan Wi-Fi library
The GS1011/GS2100 is an ultra low power 802.11b wireless module from GainSpan.
mbed RTOS supported.
- about this library: http://mbed.org/users/gsfan/notebook/GSwifiInterface/
- about Wi-Fi module: http://mbed.org/users/gsfan/notebook/gainspan_wifi/
ゲインスパン Wi-Fi モジュール ライブラリ
ゲインスパン社の低電力 Wi-Fiモジュール(無線LAN) GS1011/GS2100 シリーズ用のライブラリです。
mbed RTOS に対応しています。(mbed2.0)
- このライブラリについて: http://mbed.org/users/gsfan/notebook/gainspan_wifi/
- Wi-FIモジュールについて: http://mbed.org/users/gsfan/notebook/gainspan_wifi/
- UARTコマンド、SPIデータインターフェースに対応しました。(2019/09)
GSwifi/GSwifi_http.cpp@22:d25a5a0d2497, 2019-09-24 (annotated)
- Committer:
- gsfan
- Date:
- Tue Sep 24 06:24:37 2019 +0000
- Revision:
- 22:d25a5a0d2497
- Parent:
- 12:057089026a20
UART Command and SPI Data supported.; bug fix.;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gsfan | 8:64184a968e3b | 1 | /* Copyright (C) 2013 gsfan, MIT License |
gsfan | 8:64184a968e3b | 2 | * |
gsfan | 8:64184a968e3b | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
gsfan | 8:64184a968e3b | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
gsfan | 8:64184a968e3b | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
gsfan | 8:64184a968e3b | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
gsfan | 8:64184a968e3b | 7 | * furnished to do so, subject to the following conditions: |
gsfan | 8:64184a968e3b | 8 | * |
gsfan | 8:64184a968e3b | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
gsfan | 8:64184a968e3b | 10 | * substantial portions of the Software. |
gsfan | 8:64184a968e3b | 11 | * |
gsfan | 8:64184a968e3b | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
gsfan | 8:64184a968e3b | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
gsfan | 8:64184a968e3b | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
gsfan | 8:64184a968e3b | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
gsfan | 8:64184a968e3b | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
gsfan | 8:64184a968e3b | 17 | */ |
gsfan | 8:64184a968e3b | 18 | |
gsfan | 8:64184a968e3b | 19 | #include "GSwifi.h" |
gsfan | 8:64184a968e3b | 20 | |
gsfan | 12:057089026a20 | 21 | int GSwifi::httpGet (const char *host, int port, const char *uri, bool ssl, const char *user, const char *pwd, void(*func)(int)) { |
gsfan | 8:64184a968e3b | 22 | char ip[17]; |
gsfan | 8:64184a968e3b | 23 | int cid; |
gsfan | 8:64184a968e3b | 24 | |
gsfan | 12:057089026a20 | 25 | if (!isAssociated() || _state.status != STAT_READY) return -1; |
gsfan | 11:71d67fea5ace | 26 | |
gsfan | 8:64184a968e3b | 27 | if (getHostByName(host, ip)) return -1; |
gsfan | 8:64184a968e3b | 28 | if (! port) { |
gsfan | 8:64184a968e3b | 29 | if (ssl) { |
gsfan | 8:64184a968e3b | 30 | port = 443; |
gsfan | 8:64184a968e3b | 31 | } else { |
gsfan | 8:64184a968e3b | 32 | port = 80; |
gsfan | 8:64184a968e3b | 33 | } |
gsfan | 8:64184a968e3b | 34 | } |
gsfan | 8:64184a968e3b | 35 | |
gsfan | 8:64184a968e3b | 36 | if (cmdHTTPCONF(3, "close")) return -1; // Connection |
gsfan | 8:64184a968e3b | 37 | cmdHTTPCONFDEL(5); |
gsfan | 8:64184a968e3b | 38 | cmdHTTPCONFDEL(7); |
gsfan | 8:64184a968e3b | 39 | cmdHTTPCONF(11, host); // Host |
gsfan | 8:64184a968e3b | 40 | if (user && pwd) { |
gsfan | 8:64184a968e3b | 41 | char tmp[CFG_CMD_SIZE], tmp2[CFG_CMD_SIZE]; |
gsfan | 8:64184a968e3b | 42 | snprintf(tmp, sizeof(tmp), "%s:%s", user, pwd); |
gsfan | 8:64184a968e3b | 43 | base64encode(tmp, strlen(tmp), tmp2, sizeof(tmp2)); |
gsfan | 8:64184a968e3b | 44 | sprintf(tmp, "Basic %s", tmp2); |
gsfan | 8:64184a968e3b | 45 | cmdHTTPCONF(2, tmp); // Authorization |
gsfan | 8:64184a968e3b | 46 | } else { |
gsfan | 8:64184a968e3b | 47 | cmdHTTPCONFDEL(2); |
gsfan | 8:64184a968e3b | 48 | } |
gsfan | 8:64184a968e3b | 49 | |
gsfan | 11:71d67fea5ace | 50 | _state.cid = -1; |
gsfan | 8:64184a968e3b | 51 | if (cmdHTTPOPEN(ip, port, ssl)) return -1; |
gsfan | 8:64184a968e3b | 52 | if (_state.cid < 0) return -1; |
gsfan | 8:64184a968e3b | 53 | cid = _state.cid; |
gsfan | 8:64184a968e3b | 54 | _con[cid].protocol = PROTO_HTTPGET; |
gsfan | 8:64184a968e3b | 55 | _con[cid].type = TYPE_CLIENT; |
gsfan | 12:057089026a20 | 56 | _con[cid].func = func; |
gsfan | 8:64184a968e3b | 57 | |
gsfan | 8:64184a968e3b | 58 | cmdHTTPSEND(cid, false, uri); // GET |
gsfan | 8:64184a968e3b | 59 | return cid; |
gsfan | 8:64184a968e3b | 60 | } |
gsfan | 8:64184a968e3b | 61 | |
gsfan | 12:057089026a20 | 62 | int GSwifi::httpPost (const char *host, int port, const char *uri, const char *body, bool ssl, const char *user, const char *pwd, void(*func)(int)) { |
gsfan | 8:64184a968e3b | 63 | char cmd[CFG_CMD_SIZE]; |
gsfan | 8:64184a968e3b | 64 | char ip[17]; |
gsfan | 8:64184a968e3b | 65 | int cid, len; |
gsfan | 8:64184a968e3b | 66 | |
gsfan | 12:057089026a20 | 67 | if (!isAssociated() || _state.status != STAT_READY) return -1; |
gsfan | 11:71d67fea5ace | 68 | |
gsfan | 8:64184a968e3b | 69 | if (getHostByName(host, ip)) return -1; |
gsfan | 8:64184a968e3b | 70 | if (! port) { |
gsfan | 8:64184a968e3b | 71 | if (ssl) { |
gsfan | 8:64184a968e3b | 72 | port = 443; |
gsfan | 8:64184a968e3b | 73 | } else { |
gsfan | 8:64184a968e3b | 74 | port = 80; |
gsfan | 8:64184a968e3b | 75 | } |
gsfan | 8:64184a968e3b | 76 | } |
gsfan | 8:64184a968e3b | 77 | len = strlen(body); |
gsfan | 8:64184a968e3b | 78 | |
gsfan | 8:64184a968e3b | 79 | if (cmdHTTPCONF(3, "close")) return -1; // Connection |
gsfan | 8:64184a968e3b | 80 | sprintf(cmd, "%d", len); |
gsfan | 8:64184a968e3b | 81 | cmdHTTPCONF(5, cmd); // Content-Length |
gsfan | 8:64184a968e3b | 82 | cmdHTTPCONF(7, "application/x-www-form-urlencoded"); // Content-type |
gsfan | 8:64184a968e3b | 83 | cmdHTTPCONF(11, host); // Host |
gsfan | 8:64184a968e3b | 84 | if (user && pwd) { |
gsfan | 8:64184a968e3b | 85 | char tmp[CFG_CMD_SIZE], tmp2[CFG_CMD_SIZE]; |
gsfan | 8:64184a968e3b | 86 | snprintf(tmp, sizeof(tmp), "%s:%s", user, pwd); |
gsfan | 8:64184a968e3b | 87 | base64encode(tmp, strlen(tmp), tmp2, sizeof(tmp2)); |
gsfan | 8:64184a968e3b | 88 | sprintf(tmp, "Basic %s", tmp2); |
gsfan | 8:64184a968e3b | 89 | cmdHTTPCONF(2, tmp); // Authorization |
gsfan | 8:64184a968e3b | 90 | } else { |
gsfan | 8:64184a968e3b | 91 | cmdHTTPCONFDEL(2); |
gsfan | 8:64184a968e3b | 92 | } |
gsfan | 8:64184a968e3b | 93 | |
gsfan | 11:71d67fea5ace | 94 | _state.cid = -1; |
gsfan | 8:64184a968e3b | 95 | if (cmdHTTPOPEN(ip, port, ssl)) return -1; |
gsfan | 8:64184a968e3b | 96 | if (_state.cid < 0) return -1; |
gsfan | 8:64184a968e3b | 97 | cid = _state.cid; |
gsfan | 8:64184a968e3b | 98 | _con[cid].protocol = PROTO_HTTPPOST; |
gsfan | 8:64184a968e3b | 99 | _con[cid].type = TYPE_CLIENT; |
gsfan | 12:057089026a20 | 100 | _con[cid].func = func; |
gsfan | 8:64184a968e3b | 101 | |
gsfan | 8:64184a968e3b | 102 | cmdHTTPSEND(cid, true, uri, len); // POST |
gsfan | 8:64184a968e3b | 103 | sprintf(cmd, "\x1bH%X", cid); |
gsfan | 8:64184a968e3b | 104 | sendData(body, len, DEFAULT_WAIT_RESP_TIMEOUT, cmd); |
gsfan | 8:64184a968e3b | 105 | return cid; |
gsfan | 8:64184a968e3b | 106 | } |
gsfan | 8:64184a968e3b | 107 | |
gsfan | 8:64184a968e3b | 108 | |
gsfan | 8:64184a968e3b | 109 | /* base64encode code from |
gsfan | 8:64184a968e3b | 110 | * Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) |
gsfan | 8:64184a968e3b | 111 | */ |
gsfan | 8:64184a968e3b | 112 | int GSwifi::base64encode (const char *input, int length, char *output, int len) { |
gsfan | 8:64184a968e3b | 113 | static const char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
gsfan | 8:64184a968e3b | 114 | unsigned int c, c1, c2, c3; |
gsfan | 8:64184a968e3b | 115 | |
gsfan | 8:64184a968e3b | 116 | if (len < ((((length-1)/3)+1)<<2)) return -1; |
gsfan | 8:64184a968e3b | 117 | for(unsigned int i = 0, j = 0; i<length; i+=3,j+=4) { |
gsfan | 8:64184a968e3b | 118 | c1 = ((((unsigned char)*((unsigned char *)&input[i])))); |
gsfan | 8:64184a968e3b | 119 | c2 = (length>i+1)?((((unsigned char)*((unsigned char *)&input[i+1])))):0; |
gsfan | 8:64184a968e3b | 120 | c3 = (length>i+2)?((((unsigned char)*((unsigned char *)&input[i+2])))):0; |
gsfan | 8:64184a968e3b | 121 | |
gsfan | 8:64184a968e3b | 122 | c = ((c1 & 0xFC) >> 2); |
gsfan | 8:64184a968e3b | 123 | output[j+0] = base64[c]; |
gsfan | 8:64184a968e3b | 124 | c = ((c1 & 0x03) << 4) | ((c2 & 0xF0) >> 4); |
gsfan | 8:64184a968e3b | 125 | output[j+1] = base64[c]; |
gsfan | 8:64184a968e3b | 126 | c = ((c2 & 0x0F) << 2) | ((c3 & 0xC0) >> 6); |
gsfan | 8:64184a968e3b | 127 | output[j+2] = (length>i+1)?base64[c]:'='; |
gsfan | 8:64184a968e3b | 128 | c = (c3 & 0x3F); |
gsfan | 8:64184a968e3b | 129 | output[j+3] = (length>i+2)?base64[c]:'='; |
gsfan | 8:64184a968e3b | 130 | } |
gsfan | 8:64184a968e3b | 131 | output[(((length-1)/3)+1)<<2] = '\0'; |
gsfan | 8:64184a968e3b | 132 | return 0; |
gsfan | 8:64184a968e3b | 133 | } |
gsfan | 8:64184a968e3b | 134 | |
gsfan | 8:64184a968e3b | 135 | |
gsfan | 8:64184a968e3b | 136 | /* urlencode code from |
gsfan | 8:64184a968e3b | 137 | * Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) |
gsfan | 8:64184a968e3b | 138 | */ |
gsfan | 8:64184a968e3b | 139 | int GSwifi::urlencode (const char *str, char *buf, int len) { |
gsfan | 8:64184a968e3b | 140 | // char *pstr = str, *buf = (char*)malloc(strlen(str) * 3 + 1), *pbuf = buf; |
gsfan | 8:64184a968e3b | 141 | const char *pstr = str; |
gsfan | 8:64184a968e3b | 142 | char *pbuf = buf; |
gsfan | 8:64184a968e3b | 143 | |
gsfan | 8:64184a968e3b | 144 | if (len < (strlen(str) * 3 + 1)) return -1; |
gsfan | 8:64184a968e3b | 145 | while (*pstr) { |
gsfan | 8:64184a968e3b | 146 | if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~') |
gsfan | 8:64184a968e3b | 147 | *pbuf++ = *pstr; |
gsfan | 8:64184a968e3b | 148 | else if (*pstr == ' ') |
gsfan | 8:64184a968e3b | 149 | *pbuf++ = '+'; |
gsfan | 8:64184a968e3b | 150 | else |
gsfan | 8:64184a968e3b | 151 | *pbuf++ = '%', *pbuf++ = to_hex(*pstr >> 4), *pbuf++ = to_hex(*pstr & 15); |
gsfan | 8:64184a968e3b | 152 | pstr++; |
gsfan | 8:64184a968e3b | 153 | } |
gsfan | 8:64184a968e3b | 154 | *pbuf = '\0'; |
gsfan | 8:64184a968e3b | 155 | return 0; |
gsfan | 8:64184a968e3b | 156 | } |
gsfan | 8:64184a968e3b | 157 | |
gsfan | 8:64184a968e3b | 158 | /* urldecode code from |
gsfan | 8:64184a968e3b | 159 | * Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) |
gsfan | 8:64184a968e3b | 160 | */ |
gsfan | 8:64184a968e3b | 161 | int GSwifi::urldecode (const char *str, char *buf, int len) { |
gsfan | 8:64184a968e3b | 162 | // char *pstr = str, *buf = (char*)malloc(strlen(str) + 1), *pbuf = buf; |
gsfan | 8:64184a968e3b | 163 | const char *pstr = str; |
gsfan | 8:64184a968e3b | 164 | char *pbuf = buf; |
gsfan | 8:64184a968e3b | 165 | |
gsfan | 8:64184a968e3b | 166 | if (len < (strlen(str) / 3 - 1)) return -1; |
gsfan | 8:64184a968e3b | 167 | while (*pstr) { |
gsfan | 8:64184a968e3b | 168 | if (*pstr == '%') { |
gsfan | 8:64184a968e3b | 169 | if (pstr[1] && pstr[2]) { |
gsfan | 8:64184a968e3b | 170 | *pbuf++ = from_hex(pstr[1]) << 4 | from_hex(pstr[2]); |
gsfan | 8:64184a968e3b | 171 | pstr += 2; |
gsfan | 8:64184a968e3b | 172 | } |
gsfan | 8:64184a968e3b | 173 | } else if (*pstr == '+') { |
gsfan | 8:64184a968e3b | 174 | *pbuf++ = ' '; |
gsfan | 8:64184a968e3b | 175 | } else { |
gsfan | 8:64184a968e3b | 176 | *pbuf++ = *pstr; |
gsfan | 8:64184a968e3b | 177 | } |
gsfan | 8:64184a968e3b | 178 | pstr++; |
gsfan | 8:64184a968e3b | 179 | } |
gsfan | 8:64184a968e3b | 180 | *pbuf = '\0'; |
gsfan | 8:64184a968e3b | 181 | return 0; |
gsfan | 8:64184a968e3b | 182 | } |