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::getHostByName(const char * host, char * ip)
okini3939 4:9a2415f2ab07 22 {
okini3939 4:9a2415f2ab07 23 int i, flg = 0;
okini3939 4:9a2415f2ab07 24
okini3939 4:9a2415f2ab07 25 if (!isAssociated() || _state.status != STAT_READY) return -1;
okini3939 4:9a2415f2ab07 26
okini3939 4:9a2415f2ab07 27 for (i = 0; i < strlen(host); i ++) {
okini3939 4:9a2415f2ab07 28 if ((host[i] < '0' || host[i] > '9') && host[i] != '.') {
okini3939 4:9a2415f2ab07 29 flg = 1;
okini3939 4:9a2415f2ab07 30 break;
okini3939 4:9a2415f2ab07 31 }
okini3939 4:9a2415f2ab07 32 }
okini3939 4:9a2415f2ab07 33 if (!flg) {
okini3939 4:9a2415f2ab07 34 strncpy(ip, host, 16);
okini3939 4:9a2415f2ab07 35 return 0;
okini3939 4:9a2415f2ab07 36 }
okini3939 4:9a2415f2ab07 37
okini3939 4:9a2415f2ab07 38 if (cmdDNSLOOKUP(host)) {
okini3939 4:9a2415f2ab07 39 // retry
okini3939 4:9a2415f2ab07 40 wait_ms(1000);
okini3939 4:9a2415f2ab07 41 if (cmdDNSLOOKUP(host)) return -1;
okini3939 4:9a2415f2ab07 42 }
okini3939 4:9a2415f2ab07 43 strncpy(ip, _state.resolv, 16);
okini3939 4:9a2415f2ab07 44 return 0;
okini3939 4:9a2415f2ab07 45 }
okini3939 4:9a2415f2ab07 46
okini3939 4:9a2415f2ab07 47 int GSwifi::open (Protocol proto, const char *ip, int port, int src, void(*func)(int)) {
okini3939 4:9a2415f2ab07 48 int cid;
okini3939 4:9a2415f2ab07 49
okini3939 4:9a2415f2ab07 50 if (!isAssociated() || _state.status != STAT_READY) return -1;
okini3939 4:9a2415f2ab07 51
okini3939 4:9a2415f2ab07 52 _state.cid = -1;
okini3939 4:9a2415f2ab07 53 if (proto == PROTO_TCP) {
okini3939 4:9a2415f2ab07 54 if (cmdNCTCP(ip, port)) return -1;
okini3939 4:9a2415f2ab07 55 } else {
okini3939 4:9a2415f2ab07 56 if (cmdNCUDP(ip, port, src)) return -1;
okini3939 4:9a2415f2ab07 57 }
okini3939 4:9a2415f2ab07 58 if (_state.cid < 0) return -1;
okini3939 4:9a2415f2ab07 59 cid = _state.cid;
okini3939 4:9a2415f2ab07 60 _con[cid].protocol = proto;
okini3939 4:9a2415f2ab07 61 _con[cid].type = TYPE_CLIENT;
okini3939 4:9a2415f2ab07 62 _con[cid].func = func;
okini3939 4:9a2415f2ab07 63 return cid;
okini3939 4:9a2415f2ab07 64 }
okini3939 4:9a2415f2ab07 65
okini3939 4:9a2415f2ab07 66 int GSwifi::listen (Protocol proto, int port, void(*func)(int)) {
okini3939 4:9a2415f2ab07 67 int cid;
okini3939 4:9a2415f2ab07 68
okini3939 4:9a2415f2ab07 69 if (!isAssociated() || _state.status != STAT_READY) return -1;
okini3939 4:9a2415f2ab07 70
okini3939 4:9a2415f2ab07 71 _state.cid = -1;
okini3939 4:9a2415f2ab07 72 if (proto == PROTO_TCP) {
okini3939 4:9a2415f2ab07 73 if (cmdNSTCP(port)) return -1;
okini3939 4:9a2415f2ab07 74 } else {
okini3939 4:9a2415f2ab07 75 if (cmdNSUDP(port)) return -1;
okini3939 4:9a2415f2ab07 76 }
okini3939 4:9a2415f2ab07 77 if (_state.cid < 0) return -1;
okini3939 4:9a2415f2ab07 78 cid = _state.cid;
okini3939 4:9a2415f2ab07 79 _con[cid].protocol = proto;
okini3939 4:9a2415f2ab07 80 _con[cid].type = TYPE_SERVER;
okini3939 4:9a2415f2ab07 81 _con[cid].func = func;
okini3939 4:9a2415f2ab07 82 return cid;
okini3939 4:9a2415f2ab07 83 }
okini3939 4:9a2415f2ab07 84
okini3939 4:9a2415f2ab07 85 int GSwifi::close (int cid) {
okini3939 4:9a2415f2ab07 86
okini3939 4:9a2415f2ab07 87 if (!isConnected(cid)) return -1;
okini3939 4:9a2415f2ab07 88
okini3939 4:9a2415f2ab07 89 _con[cid].connected = false;
okini3939 4:9a2415f2ab07 90 return cmdNCLOSE(cid);
okini3939 4:9a2415f2ab07 91 }
okini3939 4:9a2415f2ab07 92
okini3939 4:9a2415f2ab07 93 int GSwifi::send (int cid, const char *buf, int len) {
okini3939 4:9a2415f2ab07 94 char cmd[CFG_CMD_SIZE];
okini3939 4:9a2415f2ab07 95
okini3939 4:9a2415f2ab07 96 if (!isConnected(cid)) return -1;
okini3939 4:9a2415f2ab07 97
okini3939 4:9a2415f2ab07 98 if ((_con[cid].protocol == PROTO_TCP) ||
okini3939 4:9a2415f2ab07 99 (_con[cid].protocol == PROTO_UDP && _con[cid].type == TYPE_CLIENT) ||
okini3939 4:9a2415f2ab07 100 (_con[cid].protocol == PROTO_HTTPD)) {
okini3939 4:9a2415f2ab07 101 if (len > CFG_DATA_SIZE) len = CFG_DATA_SIZE;
okini3939 4:9a2415f2ab07 102 sprintf(cmd, "\x1bZ%X%04d", cid, len);
okini3939 4:9a2415f2ab07 103 return sendData(buf, len, CFG_TIMEOUT, cmd);
okini3939 4:9a2415f2ab07 104 } else {
okini3939 4:9a2415f2ab07 105 return -1;
okini3939 4:9a2415f2ab07 106 }
okini3939 4:9a2415f2ab07 107 }
okini3939 4:9a2415f2ab07 108
okini3939 4:9a2415f2ab07 109 int GSwifi::sendto (int cid, const char *buf, int len, const char *ip, int port) {
okini3939 4:9a2415f2ab07 110 char cmd[CFG_CMD_SIZE];
okini3939 4:9a2415f2ab07 111
okini3939 4:9a2415f2ab07 112 if (!isConnected(cid)) return -1;
okini3939 4:9a2415f2ab07 113
okini3939 4:9a2415f2ab07 114 if ((_con[cid].protocol == PROTO_UDP && _con[cid].type == TYPE_SERVER)) {
okini3939 4:9a2415f2ab07 115 if (len > CFG_DATA_SIZE) len = CFG_DATA_SIZE;
okini3939 4:9a2415f2ab07 116 sprintf(cmd, "\x1bY%X%s:%d:%04d", cid, ip, port, len);
okini3939 4:9a2415f2ab07 117 return sendData(buf, len, CFG_TIMEOUT, cmd);
okini3939 4:9a2415f2ab07 118 } else {
okini3939 4:9a2415f2ab07 119 return -1;
okini3939 4:9a2415f2ab07 120 }
okini3939 4:9a2415f2ab07 121 }
okini3939 4:9a2415f2ab07 122
okini3939 4:9a2415f2ab07 123 int GSwifi::recv (int cid, char *buf, int len) {
okini3939 4:9a2415f2ab07 124 int i;
okini3939 4:9a2415f2ab07 125
okini3939 4:9a2415f2ab07 126 if (!isConnected(cid)) return -1;
okini3939 4:9a2415f2ab07 127
okini3939 4:9a2415f2ab07 128 if (_con[cid].buf == NULL) return 0;
okini3939 4:9a2415f2ab07 129 while (!_con[cid].received && _state.mode != MODE_COMMAND);
okini3939 4:9a2415f2ab07 130 _con[cid].received = false;
okini3939 4:9a2415f2ab07 131 for (i = 0; i < len; i ++) {
okini3939 4:9a2415f2ab07 132 if (_con[cid].buf->dequeue(&buf[i]) == false) break;
okini3939 4:9a2415f2ab07 133 }
okini3939 4:9a2415f2ab07 134 setRts(true);
okini3939 4:9a2415f2ab07 135 return i;
okini3939 4:9a2415f2ab07 136 }
okini3939 4:9a2415f2ab07 137
okini3939 4:9a2415f2ab07 138 int GSwifi::recvfrom (int cid, char *buf, int len, char *ip, int *port) {
okini3939 4:9a2415f2ab07 139 int i;
okini3939 4:9a2415f2ab07 140
okini3939 4:9a2415f2ab07 141 if (!isConnected(cid)) return -1;
okini3939 4:9a2415f2ab07 142
okini3939 4:9a2415f2ab07 143 if (_con[cid].buf == NULL) return 0;
okini3939 4:9a2415f2ab07 144 while (!_con[cid].received && _state.mode != MODE_COMMAND);
okini3939 4:9a2415f2ab07 145 _con[cid].received = false;
okini3939 4:9a2415f2ab07 146 for (i = 0; i < len; i ++) {
okini3939 4:9a2415f2ab07 147 if (_con[cid].buf->dequeue(&buf[i]) == false) break;
okini3939 4:9a2415f2ab07 148 }
okini3939 4:9a2415f2ab07 149 strncpy(ip, _con[cid].ip, 16);
okini3939 4:9a2415f2ab07 150 *port = _con[cid].port;
okini3939 4:9a2415f2ab07 151 setRts(true);
okini3939 4:9a2415f2ab07 152 return i;
okini3939 4:9a2415f2ab07 153 }
okini3939 4:9a2415f2ab07 154
okini3939 4:9a2415f2ab07 155 int GSwifi::readable (int cid) {
okini3939 4:9a2415f2ab07 156 if (!isConnected(cid)) return -1;
okini3939 4:9a2415f2ab07 157
okini3939 4:9a2415f2ab07 158 if (_con[cid].buf == NULL) return -1;
okini3939 4:9a2415f2ab07 159 return _con[cid].buf->available();
okini3939 4:9a2415f2ab07 160 }
okini3939 4:9a2415f2ab07 161
okini3939 4:9a2415f2ab07 162 bool GSwifi::isConnected (int cid) {
okini3939 4:9a2415f2ab07 163 if (cid < 0 || cid >= 16) return false;
okini3939 4:9a2415f2ab07 164
okini3939 4:9a2415f2ab07 165 return _con[cid].connected;
okini3939 4:9a2415f2ab07 166 }
okini3939 4:9a2415f2ab07 167
okini3939 4:9a2415f2ab07 168 int GSwifi::accept (int cid) {
okini3939 4:9a2415f2ab07 169 int i;
okini3939 4:9a2415f2ab07 170
okini3939 4:9a2415f2ab07 171 if (!isConnected(cid)) return -1;
okini3939 4:9a2415f2ab07 172
okini3939 4:9a2415f2ab07 173 for (i = 0; i < 16; i ++) {
okini3939 4:9a2415f2ab07 174 if (_con[i].connected && _con[i].accept && _con[i].parent == cid) {
okini3939 4:9a2415f2ab07 175 _con[i].accept = false;
okini3939 4:9a2415f2ab07 176 return i;
okini3939 4:9a2415f2ab07 177 }
okini3939 4:9a2415f2ab07 178 }
okini3939 4:9a2415f2ab07 179 return -1;
okini3939 4:9a2415f2ab07 180 }
okini3939 4:9a2415f2ab07 181
okini3939 4:9a2415f2ab07 182 int GSwifi::getRemote(int cid, char **ip, int *port) {
okini3939 4:9a2415f2ab07 183
okini3939 4:9a2415f2ab07 184 if (!isConnected(cid)) return -1;
okini3939 4:9a2415f2ab07 185
okini3939 4:9a2415f2ab07 186 *ip = _con[cid].ip;
okini3939 4:9a2415f2ab07 187 *port = _con[cid].port;
okini3939 4:9a2415f2ab07 188 return 0;
okini3939 4:9a2415f2ab07 189 }
okini3939 4:9a2415f2ab07 190
okini3939 4:9a2415f2ab07 191 void GSwifi::initCon (int cid, bool connected) {
okini3939 4:9a2415f2ab07 192 _con[cid].parent = -1;
okini3939 4:9a2415f2ab07 193 _con[cid].func = NULL;
okini3939 4:9a2415f2ab07 194 _con[cid].accept = false;
okini3939 4:9a2415f2ab07 195 if (_con[cid].buf == NULL) {
okini3939 4:9a2415f2ab07 196 _con[cid].buf = new CircBuffer<char>(CFG_DATA_SIZE);
okini3939 4:9a2415f2ab07 197 if (_con[cid].buf == NULL) error("Can't allocate memory");
okini3939 4:9a2415f2ab07 198 }
okini3939 4:9a2415f2ab07 199 _con[cid].buf->flush();
okini3939 4:9a2415f2ab07 200 _con[cid].connected = connected;
okini3939 4:9a2415f2ab07 201 }