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 mbed official

GainSpan Wi-Fi library

The GS1011/GS2100 is an ultra low power 802.11b wireless module from GainSpan.

mbed RTOS supported.

/media/uploads/gsfan/gs_im_002.jpg /media/uploads/gsfan/gs1011m_2.jpg

ゲインスパン Wi-Fi モジュール ライブラリ

ゲインスパン社の低電力 Wi-Fiモジュール(無線LAN) GS1011/GS2100 シリーズ用のライブラリです。

mbed RTOS に対応しています。(mbed2.0)

Committer:
gsfan
Date:
Tue Mar 04 15:01:38 2014 +0000
Revision:
16:105b56570533
Parent:
15:086d1a33a197
Child:
22:d25a5a0d2497
bug fix (cause of hard fault)

Who changed what in which revision?

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