see http://mbed.org/users/okini3939/notebook/wattmeter-shield-on-mbed/

Dependencies:   mbed

Fork of GSwifi_xively by gs fan

Committer:
okini3939
Date:
Wed Nov 27 08:18:45 2013 +0000
Revision:
4:9a2415f2ab07
update GSwifiInterface library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
okini3939 4:9a2415f2ab07 1 /* Copyright (C) 2013 gsfan, MIT License
okini3939 4:9a2415f2ab07 2 *
okini3939 4:9a2415f2ab07 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
okini3939 4:9a2415f2ab07 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
okini3939 4:9a2415f2ab07 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
okini3939 4:9a2415f2ab07 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
okini3939 4:9a2415f2ab07 7 * furnished to do so, subject to the following conditions:
okini3939 4:9a2415f2ab07 8 *
okini3939 4:9a2415f2ab07 9 * The above copyright notice and this permission notice shall be included in all copies or
okini3939 4:9a2415f2ab07 10 * substantial portions of the Software.
okini3939 4:9a2415f2ab07 11 *
okini3939 4:9a2415f2ab07 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
okini3939 4:9a2415f2ab07 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
okini3939 4:9a2415f2ab07 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
okini3939 4:9a2415f2ab07 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
okini3939 4:9a2415f2ab07 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
okini3939 4:9a2415f2ab07 17 */
okini3939 4:9a2415f2ab07 18
okini3939 4:9a2415f2ab07 19 #include "GSwifi.h"
okini3939 4:9a2415f2ab07 20
okini3939 4:9a2415f2ab07 21 int GSwifi::httpGet (const char *host, int port, const char *uri, bool ssl, const char *user, const char *pwd, void(*func)(int)) {
okini3939 4:9a2415f2ab07 22 char ip[17];
okini3939 4:9a2415f2ab07 23 int cid;
okini3939 4:9a2415f2ab07 24
okini3939 4:9a2415f2ab07 25 if (!isAssociated() || _state.status != STAT_READY) return -1;
okini3939 4:9a2415f2ab07 26
okini3939 4:9a2415f2ab07 27 if (getHostByName(host, ip)) return -1;
okini3939 4:9a2415f2ab07 28 if (! port) {
okini3939 4:9a2415f2ab07 29 if (ssl) {
okini3939 4:9a2415f2ab07 30 port = 443;
okini3939 4:9a2415f2ab07 31 } else {
okini3939 4:9a2415f2ab07 32 port = 80;
okini3939 4:9a2415f2ab07 33 }
okini3939 4:9a2415f2ab07 34 }
okini3939 4:9a2415f2ab07 35
okini3939 4:9a2415f2ab07 36 if (cmdHTTPCONF(3, "close")) return -1; // Connection
okini3939 4:9a2415f2ab07 37 cmdHTTPCONFDEL(5);
okini3939 4:9a2415f2ab07 38 cmdHTTPCONFDEL(7);
okini3939 4:9a2415f2ab07 39 cmdHTTPCONF(11, host); // Host
okini3939 4:9a2415f2ab07 40 if (user && pwd) {
okini3939 4:9a2415f2ab07 41 char tmp[CFG_CMD_SIZE], tmp2[CFG_CMD_SIZE];
okini3939 4:9a2415f2ab07 42 snprintf(tmp, sizeof(tmp), "%s:%s", user, pwd);
okini3939 4:9a2415f2ab07 43 base64encode(tmp, strlen(tmp), tmp2, sizeof(tmp2));
okini3939 4:9a2415f2ab07 44 sprintf(tmp, "Basic %s", tmp2);
okini3939 4:9a2415f2ab07 45 cmdHTTPCONF(2, tmp); // Authorization
okini3939 4:9a2415f2ab07 46 } else {
okini3939 4:9a2415f2ab07 47 cmdHTTPCONFDEL(2);
okini3939 4:9a2415f2ab07 48 }
okini3939 4:9a2415f2ab07 49
okini3939 4:9a2415f2ab07 50 _state.cid = -1;
okini3939 4:9a2415f2ab07 51 if (cmdHTTPOPEN(ip, port, ssl)) return -1;
okini3939 4:9a2415f2ab07 52 if (_state.cid < 0) return -1;
okini3939 4:9a2415f2ab07 53 cid = _state.cid;
okini3939 4:9a2415f2ab07 54 _con[cid].protocol = PROTO_HTTPGET;
okini3939 4:9a2415f2ab07 55 _con[cid].type = TYPE_CLIENT;
okini3939 4:9a2415f2ab07 56 _con[cid].func = func;
okini3939 4:9a2415f2ab07 57
okini3939 4:9a2415f2ab07 58 cmdHTTPSEND(cid, false, uri); // GET
okini3939 4:9a2415f2ab07 59 return cid;
okini3939 4:9a2415f2ab07 60 }
okini3939 4:9a2415f2ab07 61
okini3939 4:9a2415f2ab07 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)) {
okini3939 4:9a2415f2ab07 63 char cmd[CFG_CMD_SIZE];
okini3939 4:9a2415f2ab07 64 char ip[17];
okini3939 4:9a2415f2ab07 65 int cid, len;
okini3939 4:9a2415f2ab07 66
okini3939 4:9a2415f2ab07 67 if (!isAssociated() || _state.status != STAT_READY) return -1;
okini3939 4:9a2415f2ab07 68
okini3939 4:9a2415f2ab07 69 if (getHostByName(host, ip)) return -1;
okini3939 4:9a2415f2ab07 70 if (! port) {
okini3939 4:9a2415f2ab07 71 if (ssl) {
okini3939 4:9a2415f2ab07 72 port = 443;
okini3939 4:9a2415f2ab07 73 } else {
okini3939 4:9a2415f2ab07 74 port = 80;
okini3939 4:9a2415f2ab07 75 }
okini3939 4:9a2415f2ab07 76 }
okini3939 4:9a2415f2ab07 77 len = strlen(body);
okini3939 4:9a2415f2ab07 78
okini3939 4:9a2415f2ab07 79 if (cmdHTTPCONF(3, "close")) return -1; // Connection
okini3939 4:9a2415f2ab07 80 sprintf(cmd, "%d", len);
okini3939 4:9a2415f2ab07 81 cmdHTTPCONF(5, cmd); // Content-Length
okini3939 4:9a2415f2ab07 82 cmdHTTPCONF(7, "application/x-www-form-urlencoded"); // Content-type
okini3939 4:9a2415f2ab07 83 cmdHTTPCONF(11, host); // Host
okini3939 4:9a2415f2ab07 84 if (user && pwd) {
okini3939 4:9a2415f2ab07 85 char tmp[CFG_CMD_SIZE], tmp2[CFG_CMD_SIZE];
okini3939 4:9a2415f2ab07 86 snprintf(tmp, sizeof(tmp), "%s:%s", user, pwd);
okini3939 4:9a2415f2ab07 87 base64encode(tmp, strlen(tmp), tmp2, sizeof(tmp2));
okini3939 4:9a2415f2ab07 88 sprintf(tmp, "Basic %s", tmp2);
okini3939 4:9a2415f2ab07 89 cmdHTTPCONF(2, tmp); // Authorization
okini3939 4:9a2415f2ab07 90 } else {
okini3939 4:9a2415f2ab07 91 cmdHTTPCONFDEL(2);
okini3939 4:9a2415f2ab07 92 }
okini3939 4:9a2415f2ab07 93
okini3939 4:9a2415f2ab07 94 _state.cid = -1;
okini3939 4:9a2415f2ab07 95 if (cmdHTTPOPEN(ip, port, ssl)) return -1;
okini3939 4:9a2415f2ab07 96 if (_state.cid < 0) return -1;
okini3939 4:9a2415f2ab07 97 cid = _state.cid;
okini3939 4:9a2415f2ab07 98 _con[cid].protocol = PROTO_HTTPPOST;
okini3939 4:9a2415f2ab07 99 _con[cid].type = TYPE_CLIENT;
okini3939 4:9a2415f2ab07 100 _con[cid].func = func;
okini3939 4:9a2415f2ab07 101
okini3939 4:9a2415f2ab07 102 cmdHTTPSEND(cid, true, uri, len); // POST
okini3939 4:9a2415f2ab07 103 sprintf(cmd, "\x1bH%X", cid);
okini3939 4:9a2415f2ab07 104 sendData(body, len, DEFAULT_WAIT_RESP_TIMEOUT, cmd);
okini3939 4:9a2415f2ab07 105 return cid;
okini3939 4:9a2415f2ab07 106 }
okini3939 4:9a2415f2ab07 107
okini3939 4:9a2415f2ab07 108
okini3939 4:9a2415f2ab07 109 /* base64encode code from
okini3939 4:9a2415f2ab07 110 * Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
okini3939 4:9a2415f2ab07 111 */
okini3939 4:9a2415f2ab07 112 int GSwifi::base64encode (const char *input, int length, char *output, int len) {
okini3939 4:9a2415f2ab07 113 static const char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
okini3939 4:9a2415f2ab07 114 unsigned int c, c1, c2, c3;
okini3939 4:9a2415f2ab07 115
okini3939 4:9a2415f2ab07 116 if (len < ((((length-1)/3)+1)<<2)) return -1;
okini3939 4:9a2415f2ab07 117 for(unsigned int i = 0, j = 0; i<length; i+=3,j+=4) {
okini3939 4:9a2415f2ab07 118 c1 = ((((unsigned char)*((unsigned char *)&input[i]))));
okini3939 4:9a2415f2ab07 119 c2 = (length>i+1)?((((unsigned char)*((unsigned char *)&input[i+1])))):0;
okini3939 4:9a2415f2ab07 120 c3 = (length>i+2)?((((unsigned char)*((unsigned char *)&input[i+2])))):0;
okini3939 4:9a2415f2ab07 121
okini3939 4:9a2415f2ab07 122 c = ((c1 & 0xFC) >> 2);
okini3939 4:9a2415f2ab07 123 output[j+0] = base64[c];
okini3939 4:9a2415f2ab07 124 c = ((c1 & 0x03) << 4) | ((c2 & 0xF0) >> 4);
okini3939 4:9a2415f2ab07 125 output[j+1] = base64[c];
okini3939 4:9a2415f2ab07 126 c = ((c2 & 0x0F) << 2) | ((c3 & 0xC0) >> 6);
okini3939 4:9a2415f2ab07 127 output[j+2] = (length>i+1)?base64[c]:'=';
okini3939 4:9a2415f2ab07 128 c = (c3 & 0x3F);
okini3939 4:9a2415f2ab07 129 output[j+3] = (length>i+2)?base64[c]:'=';
okini3939 4:9a2415f2ab07 130 }
okini3939 4:9a2415f2ab07 131 output[(((length-1)/3)+1)<<2] = '\0';
okini3939 4:9a2415f2ab07 132 return 0;
okini3939 4:9a2415f2ab07 133 }
okini3939 4:9a2415f2ab07 134
okini3939 4:9a2415f2ab07 135
okini3939 4:9a2415f2ab07 136 /* urlencode code from
okini3939 4:9a2415f2ab07 137 * Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
okini3939 4:9a2415f2ab07 138 */
okini3939 4:9a2415f2ab07 139 int GSwifi::urlencode (const char *str, char *buf, int len) {
okini3939 4:9a2415f2ab07 140 // char *pstr = str, *buf = (char*)malloc(strlen(str) * 3 + 1), *pbuf = buf;
okini3939 4:9a2415f2ab07 141 const char *pstr = str;
okini3939 4:9a2415f2ab07 142 char *pbuf = buf;
okini3939 4:9a2415f2ab07 143
okini3939 4:9a2415f2ab07 144 if (len < (strlen(str) * 3 + 1)) return -1;
okini3939 4:9a2415f2ab07 145 while (*pstr) {
okini3939 4:9a2415f2ab07 146 if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~')
okini3939 4:9a2415f2ab07 147 *pbuf++ = *pstr;
okini3939 4:9a2415f2ab07 148 else if (*pstr == ' ')
okini3939 4:9a2415f2ab07 149 *pbuf++ = '+';
okini3939 4:9a2415f2ab07 150 else
okini3939 4:9a2415f2ab07 151 *pbuf++ = '%', *pbuf++ = to_hex(*pstr >> 4), *pbuf++ = to_hex(*pstr & 15);
okini3939 4:9a2415f2ab07 152 pstr++;
okini3939 4:9a2415f2ab07 153 }
okini3939 4:9a2415f2ab07 154 *pbuf = '\0';
okini3939 4:9a2415f2ab07 155 return 0;
okini3939 4:9a2415f2ab07 156 }
okini3939 4:9a2415f2ab07 157
okini3939 4:9a2415f2ab07 158 /* urldecode code from
okini3939 4:9a2415f2ab07 159 * Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
okini3939 4:9a2415f2ab07 160 */
okini3939 4:9a2415f2ab07 161 int GSwifi::urldecode (const char *str, char *buf, int len) {
okini3939 4:9a2415f2ab07 162 // char *pstr = str, *buf = (char*)malloc(strlen(str) + 1), *pbuf = buf;
okini3939 4:9a2415f2ab07 163 const char *pstr = str;
okini3939 4:9a2415f2ab07 164 char *pbuf = buf;
okini3939 4:9a2415f2ab07 165
okini3939 4:9a2415f2ab07 166 if (len < (strlen(str) / 3 - 1)) return -1;
okini3939 4:9a2415f2ab07 167 while (*pstr) {
okini3939 4:9a2415f2ab07 168 if (*pstr == '%') {
okini3939 4:9a2415f2ab07 169 if (pstr[1] && pstr[2]) {
okini3939 4:9a2415f2ab07 170 *pbuf++ = from_hex(pstr[1]) << 4 | from_hex(pstr[2]);
okini3939 4:9a2415f2ab07 171 pstr += 2;
okini3939 4:9a2415f2ab07 172 }
okini3939 4:9a2415f2ab07 173 } else if (*pstr == '+') {
okini3939 4:9a2415f2ab07 174 *pbuf++ = ' ';
okini3939 4:9a2415f2ab07 175 } else {
okini3939 4:9a2415f2ab07 176 *pbuf++ = *pstr;
okini3939 4:9a2415f2ab07 177 }
okini3939 4:9a2415f2ab07 178 pstr++;
okini3939 4:9a2415f2ab07 179 }
okini3939 4:9a2415f2ab07 180 *pbuf = '\0';
okini3939 4:9a2415f2ab07 181 return 0;
okini3939 4:9a2415f2ab07 182 }