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 #ifdef CFG_ENABLE_RTOS
okini3939 4:9a2415f2ab07 22 #undef DBG
okini3939 4:9a2415f2ab07 23 #define DBG(x, ...)
okini3939 4:9a2415f2ab07 24 #endif
okini3939 4:9a2415f2ab07 25
okini3939 4:9a2415f2ab07 26 void GSwifi::recvData (char c) {
okini3939 4:9a2415f2ab07 27 static int cid, sub, len, count;
okini3939 4:9a2415f2ab07 28
okini3939 4:9a2415f2ab07 29 #ifdef DEBUG_DUMP
okini3939 4:9a2415f2ab07 30 if (c < 0x20) {
okini3939 4:9a2415f2ab07 31 DBG("%02x_", c);
okini3939 4:9a2415f2ab07 32 } else {
okini3939 4:9a2415f2ab07 33 DBG("%c_", c);
okini3939 4:9a2415f2ab07 34 }
okini3939 4:9a2415f2ab07 35 #endif
okini3939 4:9a2415f2ab07 36 switch (_state.mode) {
okini3939 4:9a2415f2ab07 37 case MODE_COMMAND:
okini3939 4:9a2415f2ab07 38 switch (c) {
okini3939 4:9a2415f2ab07 39 case 0:
okini3939 4:9a2415f2ab07 40 case 0x0a: // LF
okini3939 4:9a2415f2ab07 41 case 0x0d: // CR
okini3939 4:9a2415f2ab07 42 break;
okini3939 4:9a2415f2ab07 43 case 0x1b: // ESC
okini3939 4:9a2415f2ab07 44 _state.buf->flush();
okini3939 4:9a2415f2ab07 45 _state.mode = MODE_ESCAPE;
okini3939 4:9a2415f2ab07 46 break;
okini3939 4:9a2415f2ab07 47 default:
okini3939 4:9a2415f2ab07 48 _state.buf->flush();
okini3939 4:9a2415f2ab07 49 _state.buf->queue(c);
okini3939 4:9a2415f2ab07 50 _state.mode = MODE_CMDRESP;
okini3939 4:9a2415f2ab07 51 break;
okini3939 4:9a2415f2ab07 52 }
okini3939 4:9a2415f2ab07 53 break;
okini3939 4:9a2415f2ab07 54
okini3939 4:9a2415f2ab07 55 case MODE_CMDRESP:
okini3939 4:9a2415f2ab07 56 switch (c) {
okini3939 4:9a2415f2ab07 57 case 0:
okini3939 4:9a2415f2ab07 58 break;
okini3939 4:9a2415f2ab07 59 case 0x0a: // LF
okini3939 4:9a2415f2ab07 60 case 0x0d: // CR
okini3939 4:9a2415f2ab07 61 // _state.buf->queue(c);
okini3939 4:9a2415f2ab07 62 parseMessage();
okini3939 4:9a2415f2ab07 63 _state.mode = MODE_COMMAND;
okini3939 4:9a2415f2ab07 64 break;
okini3939 4:9a2415f2ab07 65 case 0x1b: // ESC
okini3939 4:9a2415f2ab07 66 _state.mode = MODE_ESCAPE;
okini3939 4:9a2415f2ab07 67 break;
okini3939 4:9a2415f2ab07 68 default:
okini3939 4:9a2415f2ab07 69 _state.buf->queue(c);
okini3939 4:9a2415f2ab07 70 break;
okini3939 4:9a2415f2ab07 71 }
okini3939 4:9a2415f2ab07 72 break;
okini3939 4:9a2415f2ab07 73
okini3939 4:9a2415f2ab07 74 case MODE_ESCAPE:
okini3939 4:9a2415f2ab07 75 sub = 0;
okini3939 4:9a2415f2ab07 76 switch (c) {
okini3939 4:9a2415f2ab07 77 case 'H':
okini3939 4:9a2415f2ab07 78 _state.mode = MODE_DATA_RXHTTP;
okini3939 4:9a2415f2ab07 79 break;
okini3939 4:9a2415f2ab07 80 case 'u':
okini3939 4:9a2415f2ab07 81 _state.mode = MODE_DATA_RXUDP;
okini3939 4:9a2415f2ab07 82 break;
okini3939 4:9a2415f2ab07 83 case 'y':
okini3939 4:9a2415f2ab07 84 _state.mode = MODE_DATA_RXUDP_BULK;
okini3939 4:9a2415f2ab07 85 break;
okini3939 4:9a2415f2ab07 86 case 'Z':
okini3939 4:9a2415f2ab07 87 _state.mode = MODE_DATA_RX_BULK;
okini3939 4:9a2415f2ab07 88 break;
okini3939 4:9a2415f2ab07 89 case 'S':
okini3939 4:9a2415f2ab07 90 _state.mode = MODE_DATA_RX;
okini3939 4:9a2415f2ab07 91 break;
okini3939 4:9a2415f2ab07 92 case ':':
okini3939 4:9a2415f2ab07 93 _state.mode = MODE_DATA_RAW;
okini3939 4:9a2415f2ab07 94 break;
okini3939 4:9a2415f2ab07 95 case 'O':
okini3939 4:9a2415f2ab07 96 _state.mode = MODE_COMMAND;
okini3939 4:9a2415f2ab07 97 _state.ok = true;
okini3939 4:9a2415f2ab07 98 return;
okini3939 4:9a2415f2ab07 99 case 'F':
okini3939 4:9a2415f2ab07 100 _state.mode = MODE_COMMAND;
okini3939 4:9a2415f2ab07 101 _state.failure = true;
okini3939 4:9a2415f2ab07 102 return;
okini3939 4:9a2415f2ab07 103 default:
okini3939 4:9a2415f2ab07 104 _state.mode = MODE_COMMAND;
okini3939 4:9a2415f2ab07 105 return;
okini3939 4:9a2415f2ab07 106 }
okini3939 4:9a2415f2ab07 107 break;
okini3939 4:9a2415f2ab07 108
okini3939 4:9a2415f2ab07 109 case MODE_DATA_RX:
okini3939 4:9a2415f2ab07 110 case MODE_DATA_RXUDP:
okini3939 4:9a2415f2ab07 111 switch (sub) {
okini3939 4:9a2415f2ab07 112 case 0:
okini3939 4:9a2415f2ab07 113 // cid
okini3939 4:9a2415f2ab07 114 cid = x2i(c);
okini3939 4:9a2415f2ab07 115 sub ++;
okini3939 4:9a2415f2ab07 116 count = 0;
okini3939 4:9a2415f2ab07 117 if (_state.mode == MODE_DATA_RX) {
okini3939 4:9a2415f2ab07 118 sub = 3;
okini3939 4:9a2415f2ab07 119 }
okini3939 4:9a2415f2ab07 120 break;
okini3939 4:9a2415f2ab07 121 case 1:
okini3939 4:9a2415f2ab07 122 // ip
okini3939 4:9a2415f2ab07 123 if ((c >= '0' && c <= '9') || c == '.') {
okini3939 4:9a2415f2ab07 124 _con[cid].ip[count] = c;
okini3939 4:9a2415f2ab07 125 count ++;
okini3939 4:9a2415f2ab07 126 } else {
okini3939 4:9a2415f2ab07 127 _con[cid].ip[count] = 0;
okini3939 4:9a2415f2ab07 128 _con[cid].port = 0;
okini3939 4:9a2415f2ab07 129 sub ++;
okini3939 4:9a2415f2ab07 130 }
okini3939 4:9a2415f2ab07 131 break;
okini3939 4:9a2415f2ab07 132 case 2:
okini3939 4:9a2415f2ab07 133 // port
okini3939 4:9a2415f2ab07 134 if (c >= '0' && c <= '9') {
okini3939 4:9a2415f2ab07 135 _con[cid].port = (_con[cid].port * 10) + (c - '0');
okini3939 4:9a2415f2ab07 136 } else {
okini3939 4:9a2415f2ab07 137 sub ++;
okini3939 4:9a2415f2ab07 138 count = 0;
okini3939 4:9a2415f2ab07 139 }
okini3939 4:9a2415f2ab07 140 break;
okini3939 4:9a2415f2ab07 141 default:
okini3939 4:9a2415f2ab07 142 // data
okini3939 4:9a2415f2ab07 143 if (_state.escape) {
okini3939 4:9a2415f2ab07 144 if (c == 'E') {
okini3939 4:9a2415f2ab07 145 DBG("recv ascii %d %d/a\r\n", cid, count);
okini3939 4:9a2415f2ab07 146 _con[cid].received = true;
okini3939 4:9a2415f2ab07 147 _state.mode = MODE_COMMAND;
okini3939 4:9a2415f2ab07 148 } else {
okini3939 4:9a2415f2ab07 149 if (_con[cid].buf != NULL) {
okini3939 4:9a2415f2ab07 150 _con[cid].buf->queue(0x1b);
okini3939 4:9a2415f2ab07 151 _con[cid].buf->queue(c);
okini3939 4:9a2415f2ab07 152 }
okini3939 4:9a2415f2ab07 153 count += 2;
okini3939 4:9a2415f2ab07 154 }
okini3939 4:9a2415f2ab07 155 _state.escape = false;
okini3939 4:9a2415f2ab07 156 } else
okini3939 4:9a2415f2ab07 157 if (c == 0x1b) {
okini3939 4:9a2415f2ab07 158 _state.escape = true;
okini3939 4:9a2415f2ab07 159 } else {
okini3939 4:9a2415f2ab07 160 if (_con[cid].buf != NULL) {
okini3939 4:9a2415f2ab07 161 _con[cid].buf->queue(c);
okini3939 4:9a2415f2ab07 162 if (_con[cid].buf->available() > CFG_DATA_SIZE - 16) {
okini3939 4:9a2415f2ab07 163 setRts(false);
okini3939 4:9a2415f2ab07 164 _con[cid].received = true;
okini3939 4:9a2415f2ab07 165 WARN("buf full");
okini3939 4:9a2415f2ab07 166 }
okini3939 4:9a2415f2ab07 167 }
okini3939 4:9a2415f2ab07 168 count ++;
okini3939 4:9a2415f2ab07 169 }
okini3939 4:9a2415f2ab07 170 break;
okini3939 4:9a2415f2ab07 171 }
okini3939 4:9a2415f2ab07 172 break;
okini3939 4:9a2415f2ab07 173
okini3939 4:9a2415f2ab07 174 case MODE_DATA_RX_BULK:
okini3939 4:9a2415f2ab07 175 case MODE_DATA_RXUDP_BULK:
okini3939 4:9a2415f2ab07 176 case MODE_DATA_RXHTTP:
okini3939 4:9a2415f2ab07 177 switch (sub) {
okini3939 4:9a2415f2ab07 178 case 0:
okini3939 4:9a2415f2ab07 179 // cid
okini3939 4:9a2415f2ab07 180 cid = x2i(c);
okini3939 4:9a2415f2ab07 181 sub ++;
okini3939 4:9a2415f2ab07 182 len = 0;
okini3939 4:9a2415f2ab07 183 count = 0;
okini3939 4:9a2415f2ab07 184 if (_state.mode != MODE_DATA_RXUDP_BULK) {
okini3939 4:9a2415f2ab07 185 sub = 3;
okini3939 4:9a2415f2ab07 186 }
okini3939 4:9a2415f2ab07 187 break;
okini3939 4:9a2415f2ab07 188 case 1:
okini3939 4:9a2415f2ab07 189 // ip
okini3939 4:9a2415f2ab07 190 if ((c >= '0' && c <= '9') || c == '.') {
okini3939 4:9a2415f2ab07 191 _con[cid].ip[count] = c;
okini3939 4:9a2415f2ab07 192 count ++;
okini3939 4:9a2415f2ab07 193 } else {
okini3939 4:9a2415f2ab07 194 _con[cid].ip[count] = 0;
okini3939 4:9a2415f2ab07 195 _con[cid].port = 0;
okini3939 4:9a2415f2ab07 196 sub ++;
okini3939 4:9a2415f2ab07 197 }
okini3939 4:9a2415f2ab07 198 break;
okini3939 4:9a2415f2ab07 199 case 2:
okini3939 4:9a2415f2ab07 200 // port
okini3939 4:9a2415f2ab07 201 if (c >= '0' && c <= '9') {
okini3939 4:9a2415f2ab07 202 _con[cid].port = (_con[cid].port * 10) + (c - '0');
okini3939 4:9a2415f2ab07 203 } else {
okini3939 4:9a2415f2ab07 204 sub ++;
okini3939 4:9a2415f2ab07 205 count = 0;
okini3939 4:9a2415f2ab07 206 }
okini3939 4:9a2415f2ab07 207 break;
okini3939 4:9a2415f2ab07 208 case 3:
okini3939 4:9a2415f2ab07 209 // length
okini3939 4:9a2415f2ab07 210 len = (len * 10) + (c - '0');
okini3939 4:9a2415f2ab07 211 count ++;
okini3939 4:9a2415f2ab07 212 if (count >= 4) {
okini3939 4:9a2415f2ab07 213 sub ++;
okini3939 4:9a2415f2ab07 214 count = 0;
okini3939 4:9a2415f2ab07 215 }
okini3939 4:9a2415f2ab07 216 break;
okini3939 4:9a2415f2ab07 217 default:
okini3939 4:9a2415f2ab07 218 // data
okini3939 4:9a2415f2ab07 219 #ifdef CFG_ENABLE_HTTPD
okini3939 4:9a2415f2ab07 220 if (_con[cid].protocol == PROTO_HTTPD) {
okini3939 4:9a2415f2ab07 221 httpdRecvData(cid, c);
okini3939 4:9a2415f2ab07 222 } else
okini3939 4:9a2415f2ab07 223 #endif
okini3939 4:9a2415f2ab07 224 if (_con[cid].buf != NULL) {
okini3939 4:9a2415f2ab07 225 _con[cid].buf->queue(c);
okini3939 4:9a2415f2ab07 226 if (_con[cid].buf->available() > CFG_DATA_SIZE - 16) {
okini3939 4:9a2415f2ab07 227 setRts(false);
okini3939 4:9a2415f2ab07 228 _con[cid].received = true;
okini3939 4:9a2415f2ab07 229 WARN("buf full");
okini3939 4:9a2415f2ab07 230 }
okini3939 4:9a2415f2ab07 231 }
okini3939 4:9a2415f2ab07 232 count ++;
okini3939 4:9a2415f2ab07 233 if (count >= len) {
okini3939 4:9a2415f2ab07 234 DBG("recv bulk %d %d/%d\r\n", cid, count, len);
okini3939 4:9a2415f2ab07 235 _con[cid].received = true;
okini3939 4:9a2415f2ab07 236 _state.mode = MODE_COMMAND;
okini3939 4:9a2415f2ab07 237 }
okini3939 4:9a2415f2ab07 238 break;
okini3939 4:9a2415f2ab07 239 }
okini3939 4:9a2415f2ab07 240 break;
okini3939 4:9a2415f2ab07 241 }
okini3939 4:9a2415f2ab07 242 }
okini3939 4:9a2415f2ab07 243
okini3939 4:9a2415f2ab07 244 #define MSG_TABLE_NUM 15
okini3939 4:9a2415f2ab07 245 #define RES_TABLE_NUM 11
okini3939 4:9a2415f2ab07 246 int GSwifi::parseMessage () {
okini3939 4:9a2415f2ab07 247 int i;
okini3939 4:9a2415f2ab07 248 char buf[256];
okini3939 4:9a2415f2ab07 249 static const struct MSG_TABLE {
okini3939 4:9a2415f2ab07 250 const char msg[24];
okini3939 4:9a2415f2ab07 251 void (GSwifi::*func)(const char*);
okini3939 4:9a2415f2ab07 252 } msg_table[MSG_TABLE_NUM] = {
okini3939 4:9a2415f2ab07 253 {"OK", &GSwifi::msgOk},
okini3939 4:9a2415f2ab07 254 {"ERROR", &GSwifi::msgError},
okini3939 4:9a2415f2ab07 255 {"INVALID INPUT", &GSwifi::msgError},
okini3939 4:9a2415f2ab07 256 {"CONNECT ", &GSwifi::msgConnect},
okini3939 4:9a2415f2ab07 257 {"DISCONNECT ", &GSwifi::msgDisconnect},
okini3939 4:9a2415f2ab07 258 {"DISASSOCIATED", &GSwifi::msgDisassociated},
okini3939 4:9a2415f2ab07 259 {"Disassociated", &GSwifi::msgDisassociated},
okini3939 4:9a2415f2ab07 260 {"Disassociation Event", &GSwifi::msgDisassociated},
okini3939 4:9a2415f2ab07 261 {"Serial2WiFi APP", &GSwifi::msgReset},
okini3939 4:9a2415f2ab07 262 {"UnExpected Warm Boot", &GSwifi::msgReset},
okini3939 4:9a2415f2ab07 263 {"APP Reset-APP SW Reset", &GSwifi::msgReset},
okini3939 4:9a2415f2ab07 264 {"APP Reset-Wlan Except", &GSwifi::msgReset},
okini3939 4:9a2415f2ab07 265 {"Out of StandBy-Timer", &GSwifi::msgOutofStandby},
okini3939 4:9a2415f2ab07 266 {"Out of StandBy-Alarm", &GSwifi::msgOutofStandby},
okini3939 4:9a2415f2ab07 267 {"Out of Deep Sleep", &GSwifi::msgOutofDeepsleep},
okini3939 4:9a2415f2ab07 268 };
okini3939 4:9a2415f2ab07 269 static const struct RES_TABLE {
okini3939 4:9a2415f2ab07 270 const Response res;
okini3939 4:9a2415f2ab07 271 void (GSwifi::*func)(const char*);
okini3939 4:9a2415f2ab07 272 } res_table[RES_TABLE_NUM] = {
okini3939 4:9a2415f2ab07 273 {RES_NULL, NULL},
okini3939 4:9a2415f2ab07 274 {RES_CONNECT, &GSwifi::resConnect},
okini3939 4:9a2415f2ab07 275 {RES_WPAPSK, &GSwifi::resWpapsk},
okini3939 4:9a2415f2ab07 276 {RES_WPS, &GSwifi::resWps},
okini3939 4:9a2415f2ab07 277 {RES_MACADDRESS, &GSwifi::resMacAddress},
okini3939 4:9a2415f2ab07 278 {RES_DHCP, &GSwifi::resIp},
okini3939 4:9a2415f2ab07 279 {RES_DNSLOOKUP, &GSwifi::resLookup},
okini3939 4:9a2415f2ab07 280 {RES_HTTP, &GSwifi::resHttp},
okini3939 4:9a2415f2ab07 281 {RES_RSSI, &GSwifi::resRssi},
okini3939 4:9a2415f2ab07 282 {RES_TIME, &GSwifi::resTime},
okini3939 4:9a2415f2ab07 283 {RES_STATUS, &GSwifi::resStatus},
okini3939 4:9a2415f2ab07 284 };
okini3939 4:9a2415f2ab07 285
okini3939 4:9a2415f2ab07 286 for (i = 0; i < sizeof(buf); i++) {
okini3939 4:9a2415f2ab07 287 if (_state.buf->dequeue(&buf[i]) == false) break;
okini3939 4:9a2415f2ab07 288 }
okini3939 4:9a2415f2ab07 289 buf[i] = 0;
okini3939 4:9a2415f2ab07 290
okini3939 4:9a2415f2ab07 291 if (_state.res != RES_NULL) {
okini3939 4:9a2415f2ab07 292 for (i = 0; i < RES_TABLE_NUM; i ++) {
okini3939 4:9a2415f2ab07 293 if (res_table[i].res == _state.res) {
okini3939 4:9a2415f2ab07 294 DBG("parse res %d '%s'\r\n", i, buf);
okini3939 4:9a2415f2ab07 295 if (res_table[i].func != NULL) {
okini3939 4:9a2415f2ab07 296 (this->*(res_table[i].func))(buf);
okini3939 4:9a2415f2ab07 297 }
okini3939 4:9a2415f2ab07 298 }
okini3939 4:9a2415f2ab07 299 }
okini3939 4:9a2415f2ab07 300 }
okini3939 4:9a2415f2ab07 301
okini3939 4:9a2415f2ab07 302 for (i = 0; i < MSG_TABLE_NUM; i ++) {
okini3939 4:9a2415f2ab07 303 if (strncmp(buf, msg_table[i].msg, strlen(msg_table[i].msg)) == 0) {
okini3939 4:9a2415f2ab07 304 DBG("parse msg %d '%s'\r\n", i, buf);
okini3939 4:9a2415f2ab07 305 if (msg_table[i].func != NULL) {
okini3939 4:9a2415f2ab07 306 (this->*(msg_table[i].func))(buf);
okini3939 4:9a2415f2ab07 307 }
okini3939 4:9a2415f2ab07 308 return 0;
okini3939 4:9a2415f2ab07 309 }
okini3939 4:9a2415f2ab07 310 }
okini3939 4:9a2415f2ab07 311
okini3939 4:9a2415f2ab07 312 return -1;
okini3939 4:9a2415f2ab07 313 }
okini3939 4:9a2415f2ab07 314
okini3939 4:9a2415f2ab07 315 void GSwifi::msgOk (const char *buf) {
okini3939 4:9a2415f2ab07 316 _state.ok = true;
okini3939 4:9a2415f2ab07 317 if (_state.status == STAT_DEEPSLEEP) {
okini3939 4:9a2415f2ab07 318 _state.status = STAT_READY;
okini3939 4:9a2415f2ab07 319 }
okini3939 4:9a2415f2ab07 320 }
okini3939 4:9a2415f2ab07 321
okini3939 4:9a2415f2ab07 322 void GSwifi::msgError (const char *buf) {
okini3939 4:9a2415f2ab07 323 _state.failure = true;
okini3939 4:9a2415f2ab07 324 }
okini3939 4:9a2415f2ab07 325
okini3939 4:9a2415f2ab07 326 void GSwifi::msgConnect (const char *buf) {
okini3939 4:9a2415f2ab07 327 int i, count;
okini3939 4:9a2415f2ab07 328 int cid, acid;
okini3939 4:9a2415f2ab07 329
okini3939 4:9a2415f2ab07 330 if (buf[8] < '0' || buf[8] > 'F' || buf[9] != ' ') return;
okini3939 4:9a2415f2ab07 331
okini3939 4:9a2415f2ab07 332 cid = x2i(buf[8]);
okini3939 4:9a2415f2ab07 333 acid = x2i(buf[10]);
okini3939 4:9a2415f2ab07 334 DBG("forked %d -> %d\r\n", cid, acid);
okini3939 4:9a2415f2ab07 335 // ip
okini3939 4:9a2415f2ab07 336 count = 0;
okini3939 4:9a2415f2ab07 337 for (i = 12; i < strlen(buf); i ++) {
okini3939 4:9a2415f2ab07 338 if ((buf[i] >= '0' && buf[i] <= '9') || buf[i] == '.') {
okini3939 4:9a2415f2ab07 339 _con[acid].ip[count] = buf[i];
okini3939 4:9a2415f2ab07 340 count ++;
okini3939 4:9a2415f2ab07 341 } else {
okini3939 4:9a2415f2ab07 342 _con[acid].ip[count] = 0;
okini3939 4:9a2415f2ab07 343 break;
okini3939 4:9a2415f2ab07 344 }
okini3939 4:9a2415f2ab07 345 }
okini3939 4:9a2415f2ab07 346 // port
okini3939 4:9a2415f2ab07 347 _con[acid].port = 0;
okini3939 4:9a2415f2ab07 348 count = 0;
okini3939 4:9a2415f2ab07 349 for (; i < strlen(buf); i ++) {
okini3939 4:9a2415f2ab07 350 if (buf[i] >= '0' && buf[i] <= '9') {
okini3939 4:9a2415f2ab07 351 _con[acid].port = (_con[acid].port * 10) + (buf[i] - '0');
okini3939 4:9a2415f2ab07 352 } else {
okini3939 4:9a2415f2ab07 353 break;
okini3939 4:9a2415f2ab07 354 }
okini3939 4:9a2415f2ab07 355 }
okini3939 4:9a2415f2ab07 356
okini3939 4:9a2415f2ab07 357 // initialize
okini3939 4:9a2415f2ab07 358 initCon(acid, true);
okini3939 4:9a2415f2ab07 359 _con[acid].protocol = _con[cid].protocol;
okini3939 4:9a2415f2ab07 360 _con[acid].type = _con[cid].type;
okini3939 4:9a2415f2ab07 361 _con[acid].parent = cid;
okini3939 4:9a2415f2ab07 362 _con[acid].func = _con[cid].func;
okini3939 4:9a2415f2ab07 363 _con[acid].accept = true;
okini3939 4:9a2415f2ab07 364 }
okini3939 4:9a2415f2ab07 365
okini3939 4:9a2415f2ab07 366 void GSwifi::msgDisconnect (const char *buf) {
okini3939 4:9a2415f2ab07 367 int cid;
okini3939 4:9a2415f2ab07 368
okini3939 4:9a2415f2ab07 369 if (buf[11] < '0' || buf[11] > 'F') return;
okini3939 4:9a2415f2ab07 370
okini3939 4:9a2415f2ab07 371 cid = x2i(buf[11]);
okini3939 4:9a2415f2ab07 372 DBG("disconnect %d\r\n", cid);
okini3939 4:9a2415f2ab07 373 _con[cid].connected = false;
okini3939 4:9a2415f2ab07 374 }
okini3939 4:9a2415f2ab07 375
okini3939 4:9a2415f2ab07 376 void GSwifi::msgDisassociated (const char *buf) {
okini3939 4:9a2415f2ab07 377 int i;
okini3939 4:9a2415f2ab07 378 DBG("disassociate\r\n");
okini3939 4:9a2415f2ab07 379 _state.associated = false;
okini3939 4:9a2415f2ab07 380 for (i = 0; i < 16; i ++) {
okini3939 4:9a2415f2ab07 381 _con[i].connected = false;
okini3939 4:9a2415f2ab07 382 }
okini3939 4:9a2415f2ab07 383 #ifdef CFG_ENABLE_RTOS
okini3939 4:9a2415f2ab07 384 if (_threadPoll)
okini3939 4:9a2415f2ab07 385 _threadPoll->signal_set(1);
okini3939 4:9a2415f2ab07 386 #endif
okini3939 4:9a2415f2ab07 387 }
okini3939 4:9a2415f2ab07 388
okini3939 4:9a2415f2ab07 389 void GSwifi::msgReset (const char *buf) {
okini3939 4:9a2415f2ab07 390 DBG("reset\r\n");
okini3939 4:9a2415f2ab07 391 _state.initialized = false;
okini3939 4:9a2415f2ab07 392 _state.mode = MODE_COMMAND;
okini3939 4:9a2415f2ab07 393 _state.status = STAT_READY;
okini3939 4:9a2415f2ab07 394 msgDisassociated(NULL);
okini3939 4:9a2415f2ab07 395 clearFlags();
okini3939 4:9a2415f2ab07 396 #ifdef CFG_ENABLE_RTOS
okini3939 4:9a2415f2ab07 397 if (_threadPoll) {
okini3939 4:9a2415f2ab07 398 _threadPoll->terminate();
okini3939 4:9a2415f2ab07 399 delete _threadPoll;
okini3939 4:9a2415f2ab07 400 }
okini3939 4:9a2415f2ab07 401 #endif
okini3939 4:9a2415f2ab07 402 }
okini3939 4:9a2415f2ab07 403
okini3939 4:9a2415f2ab07 404 void GSwifi::msgOutofStandby (const char *buf) {
okini3939 4:9a2415f2ab07 405 DBG("OutofStandby\r\n");
okini3939 4:9a2415f2ab07 406 _state.status = STAT_WAKEUP;
okini3939 4:9a2415f2ab07 407 }
okini3939 4:9a2415f2ab07 408
okini3939 4:9a2415f2ab07 409 void GSwifi::msgOutofDeepsleep (const char *buf) {
okini3939 4:9a2415f2ab07 410 DBG("OutofDeepsleep\r\n");
okini3939 4:9a2415f2ab07 411 _state.status = STAT_READY;
okini3939 4:9a2415f2ab07 412 }
okini3939 4:9a2415f2ab07 413
okini3939 4:9a2415f2ab07 414 void GSwifi::resConnect (const char *buf) {
okini3939 4:9a2415f2ab07 415 int cid;
okini3939 4:9a2415f2ab07 416
okini3939 4:9a2415f2ab07 417 // udp/tcp listen socket
okini3939 4:9a2415f2ab07 418 if (strncmp(buf, "CONNECT ", 8) == 0 && buf[9] == 0) {
okini3939 4:9a2415f2ab07 419 cid = x2i(buf[8]);
okini3939 4:9a2415f2ab07 420 DBG("connect %d\r\n", cid);
okini3939 4:9a2415f2ab07 421 // initialize
okini3939 4:9a2415f2ab07 422 initCon(cid, true);
okini3939 4:9a2415f2ab07 423 _state.cid = cid;
okini3939 4:9a2415f2ab07 424 _state.res = RES_NULL;
okini3939 4:9a2415f2ab07 425 }
okini3939 4:9a2415f2ab07 426 }
okini3939 4:9a2415f2ab07 427
okini3939 4:9a2415f2ab07 428 void GSwifi::resWpapsk (const char *buf) {
okini3939 4:9a2415f2ab07 429 if (strncmp(buf, "Computing PSK from SSID and PassPhrase", 38) == 0) {
okini3939 4:9a2415f2ab07 430 _state.res = RES_NULL;
okini3939 4:9a2415f2ab07 431 DBG("wpapsk\r\n");
okini3939 4:9a2415f2ab07 432 }
okini3939 4:9a2415f2ab07 433 }
okini3939 4:9a2415f2ab07 434
okini3939 4:9a2415f2ab07 435 void GSwifi::resWps (const char *buf) {
okini3939 4:9a2415f2ab07 436 if (_state.n == 0 && strncmp(buf, "SSID", 4) == 0) {
okini3939 4:9a2415f2ab07 437 strncpy(_state.ssid, &buf[5], sizeof(_state.ssid));
okini3939 4:9a2415f2ab07 438 _state.n ++;
okini3939 4:9a2415f2ab07 439 } else
okini3939 4:9a2415f2ab07 440 if (_state.n == 1 && strncmp(buf, "CHANNEL", 7) == 0) {
okini3939 4:9a2415f2ab07 441 _state.n ++;
okini3939 4:9a2415f2ab07 442 } else
okini3939 4:9a2415f2ab07 443 if (_state.n == 2 && strncmp(buf, "PASSPHRASE", 10) == 0) {
okini3939 4:9a2415f2ab07 444 strncpy(_state.pass, &buf[11], sizeof(_state.pass));
okini3939 4:9a2415f2ab07 445 _state.n ++;
okini3939 4:9a2415f2ab07 446 _state.res = RES_NULL;
okini3939 4:9a2415f2ab07 447 DBG("wps %s %s\r\n", _state.ssid, _state.pass);
okini3939 4:9a2415f2ab07 448 }
okini3939 4:9a2415f2ab07 449 }
okini3939 4:9a2415f2ab07 450
okini3939 4:9a2415f2ab07 451 void GSwifi::resMacAddress (const char *buf) {
okini3939 4:9a2415f2ab07 452 if (buf[2] == ':' && buf[5] == ':') {
okini3939 4:9a2415f2ab07 453 strncpy(_state.mac, buf, sizeof(_state.mac));
okini3939 4:9a2415f2ab07 454 _state.mac[17] = 0;
okini3939 4:9a2415f2ab07 455 _state.res = RES_NULL;
okini3939 4:9a2415f2ab07 456 DBG("mac %s\r\n", _state.mac);
okini3939 4:9a2415f2ab07 457 }
okini3939 4:9a2415f2ab07 458 }
okini3939 4:9a2415f2ab07 459
okini3939 4:9a2415f2ab07 460 void GSwifi::resIp (const char *buf) {
okini3939 4:9a2415f2ab07 461 const char *tmp, *tmp2;
okini3939 4:9a2415f2ab07 462
okini3939 4:9a2415f2ab07 463 if (_state.n == 0 && strstr(buf, "SubNet") && strstr(buf, "Gateway")) {
okini3939 4:9a2415f2ab07 464 _state.n ++;
okini3939 4:9a2415f2ab07 465 } else
okini3939 4:9a2415f2ab07 466 if (_state.n == 1) {
okini3939 4:9a2415f2ab07 467 tmp = buf + 1;
okini3939 4:9a2415f2ab07 468 tmp2 = strstr(tmp, ":");
okini3939 4:9a2415f2ab07 469 strncpy(_state.ip, tmp, tmp2 - tmp);
okini3939 4:9a2415f2ab07 470 tmp = tmp2 + 2;
okini3939 4:9a2415f2ab07 471 tmp2 = strstr(tmp, ":");
okini3939 4:9a2415f2ab07 472 strncpy(_state.netmask, tmp, tmp2 - tmp);
okini3939 4:9a2415f2ab07 473 tmp = tmp2 + 2;
okini3939 4:9a2415f2ab07 474 strncpy(_state.gateway, tmp, sizeof(_state.gateway));
okini3939 4:9a2415f2ab07 475 _state.n ++;
okini3939 4:9a2415f2ab07 476 _state.res = RES_NULL;
okini3939 4:9a2415f2ab07 477 DBG("ip: %s\r\nnetmask: %s\r\ngateway: %s", _state.ip, _state.netmask, _state.gateway);
okini3939 4:9a2415f2ab07 478 }
okini3939 4:9a2415f2ab07 479 }
okini3939 4:9a2415f2ab07 480
okini3939 4:9a2415f2ab07 481 void GSwifi::resLookup (const char *buf) {
okini3939 4:9a2415f2ab07 482 if (strncmp(buf, "IP:", 3) == 0) {
okini3939 4:9a2415f2ab07 483 strncpy(_state.resolv, &buf[3], sizeof(_state.resolv));
okini3939 4:9a2415f2ab07 484 _state.res = RES_NULL;
okini3939 4:9a2415f2ab07 485 DBG("resolv: %s\r\n", _state.resolv);
okini3939 4:9a2415f2ab07 486 }
okini3939 4:9a2415f2ab07 487 }
okini3939 4:9a2415f2ab07 488
okini3939 4:9a2415f2ab07 489 void GSwifi::resRssi (const char *buf) {
okini3939 4:9a2415f2ab07 490 if (buf[0] == '-' || (buf[0] >= '0' && buf[0] <= '9')) {
okini3939 4:9a2415f2ab07 491 _state.rssi = atoi(buf);
okini3939 4:9a2415f2ab07 492 _state.res = RES_NULL;
okini3939 4:9a2415f2ab07 493 DBG("rssi: %d\r\n", _state.rssi);
okini3939 4:9a2415f2ab07 494 }
okini3939 4:9a2415f2ab07 495 }
okini3939 4:9a2415f2ab07 496
okini3939 4:9a2415f2ab07 497 void GSwifi::resTime (const char *buf) {
okini3939 4:9a2415f2ab07 498 int year, month, day, hour, min, sec;
okini3939 4:9a2415f2ab07 499 struct tm t;
okini3939 4:9a2415f2ab07 500 if (buf[0] >= '0' && buf[0] <= '9') {
okini3939 4:9a2415f2ab07 501 sscanf(buf, "%d/%d/%d,%d:%d:%d", &day, &month, &year, &hour, &min, &sec);
okini3939 4:9a2415f2ab07 502 t.tm_sec = sec;
okini3939 4:9a2415f2ab07 503 t.tm_min = min;
okini3939 4:9a2415f2ab07 504 t.tm_hour = hour;
okini3939 4:9a2415f2ab07 505 t.tm_mday = day;
okini3939 4:9a2415f2ab07 506 t.tm_mon = month - 1;
okini3939 4:9a2415f2ab07 507 t.tm_year = year - 1900;
okini3939 4:9a2415f2ab07 508 _state.time = mktime(&t);
okini3939 4:9a2415f2ab07 509 _state.res = RES_NULL;
okini3939 4:9a2415f2ab07 510 }
okini3939 4:9a2415f2ab07 511 }
okini3939 4:9a2415f2ab07 512
okini3939 4:9a2415f2ab07 513 void GSwifi::resChannel (const char *buf) {
okini3939 4:9a2415f2ab07 514 }
okini3939 4:9a2415f2ab07 515
okini3939 4:9a2415f2ab07 516 void GSwifi::resStatus (const char *buf) {
okini3939 4:9a2415f2ab07 517 if (_state.n == 0 && strncmp(buf, "NOT ASSOCIATED", 14) == 0) {
okini3939 4:9a2415f2ab07 518 msgDisassociated(NULL);
okini3939 4:9a2415f2ab07 519 _state.res = RES_NULL;
okini3939 4:9a2415f2ab07 520 }
okini3939 4:9a2415f2ab07 521
okini3939 4:9a2415f2ab07 522 if (_state.n == 0 && strncmp(buf, "MODE:", 5) == 0) {
okini3939 4:9a2415f2ab07 523 _state.n ++;
okini3939 4:9a2415f2ab07 524 } else
okini3939 4:9a2415f2ab07 525 if (_state.n == 1 && strncmp(buf, "BSSID:", 6) == 0) {
okini3939 4:9a2415f2ab07 526 const char *tmp = strstr(buf, "SECURITY:") + 2;
okini3939 4:9a2415f2ab07 527 if (strncmp(tmp, "WEP (OPEN)", 10) == NULL) {
okini3939 4:9a2415f2ab07 528 _state.sec = SEC_OPEN;
okini3939 4:9a2415f2ab07 529 } else
okini3939 4:9a2415f2ab07 530 if (strncmp(tmp, "WEP (SHARED)", 12) == NULL) {
okini3939 4:9a2415f2ab07 531 _state.sec = SEC_WEP;
okini3939 4:9a2415f2ab07 532 } else
okini3939 4:9a2415f2ab07 533 if (strncmp(tmp, "WPA-PERSONAL", 12) == NULL) {
okini3939 4:9a2415f2ab07 534 _state.sec = SEC_WPA_PSK;
okini3939 4:9a2415f2ab07 535 } else
okini3939 4:9a2415f2ab07 536 if (strncmp(tmp, "WPA2-PERSONAL", 13) == NULL) {
okini3939 4:9a2415f2ab07 537 _state.sec = SEC_WPA2_PSK;
okini3939 4:9a2415f2ab07 538 }
okini3939 4:9a2415f2ab07 539 _state.res = RES_NULL;
okini3939 4:9a2415f2ab07 540 }
okini3939 4:9a2415f2ab07 541 }
okini3939 4:9a2415f2ab07 542
okini3939 4:9a2415f2ab07 543 void GSwifi::resHttp (const char *buf) {
okini3939 4:9a2415f2ab07 544 int cid;
okini3939 4:9a2415f2ab07 545
okini3939 4:9a2415f2ab07 546 // http client socket
okini3939 4:9a2415f2ab07 547 if (buf[0] >= '0' && buf[0] <= 'F' && buf[1] == 0) {
okini3939 4:9a2415f2ab07 548 cid = x2i(buf[0]);
okini3939 4:9a2415f2ab07 549 DBG("connect %d\r\n", cid);
okini3939 4:9a2415f2ab07 550 // initialize
okini3939 4:9a2415f2ab07 551 initCon(cid, true);
okini3939 4:9a2415f2ab07 552 _state.cid = cid;
okini3939 4:9a2415f2ab07 553 _state.res = RES_NULL;
okini3939 4:9a2415f2ab07 554 }
okini3939 4:9a2415f2ab07 555 }