for TwitterMbed
Fork of GSwifi_old by
Diff: GSwifi.cpp
- Revision:
- 18:4b97804c37d1
- Parent:
- 17:6828b084e74b
- Child:
- 19:cad912f5a6ba
diff -r 6828b084e74b -r 4b97804c37d1 GSwifi.cpp --- a/GSwifi.cpp Sun Oct 28 14:26:31 2012 +0000 +++ b/GSwifi.cpp Thu Nov 01 02:55:57 2012 +0000 @@ -32,6 +32,7 @@ _connect = false; _status = GSSTAT_READY; _escape = 0; + _response = 1; _gs_mode = GSMODE_COMMAND; _gs.baud(baud); @@ -43,6 +44,7 @@ _connect = false; _status = GSSTAT_READY; _escape = 0; + _response = 1; _gs_mode = GSMODE_COMMAND; _gs.baud(baud); @@ -93,7 +95,7 @@ dat = _gs_getc(); if (flg & ((1 << 7)|(1 << 3)|(1 << 4))) return; -// DBG("%02x ", dat); +// DBG("%02x_", dat); switch (_gs_mode) { case GSMODE_COMMAND: // command responce @@ -141,7 +143,10 @@ if (dat != '\r') { // command _buf_cmd.put(dat); - if (dat == '\n') _gs_enter ++; + if (dat == '\n') { + _gs_enter ++; + if (!_response && _connect) poll_cmd(); + } } } break; @@ -299,19 +304,20 @@ } int GSwifi::command (const char *cmd, GSRESPONCE res, int timeout) { - int i; + int i, r = 0; if (! cmd) { // dummy CR+LF _gs.printf("\r\n"); for (i = 0; i < 10; i ++) { wait_ms(10); - poll(); + poll_cmd(); _buf_cmd.clear(); } return 0; } + _response = 1; _buf_cmd.clear(); _gs_ok = 0; _gs_failure = 0; @@ -323,12 +329,12 @@ _gs_putc('\n'); DBG("command: %s\r\n", cmd); if (strlen(cmd) == 0) return 0; - wait_ms(50); + wait_ms(10); if (timeout) { - return cmdResponse(res, timeout); - } else { - return 0; + r = cmdResponse(res, timeout); } + _response = 0; + return r; } int GSwifi::cmdResponse (GSRESPONCE res, int ms) { @@ -537,6 +543,12 @@ break; } + if (r == 0 && !dhcp) { + sprintf(cmd, "AT+DNSSET=%d.%d.%d.%d", + _gateway[0], _gateway[1], _gateway[2], _gateway[3]); + command(cmd, GSRES_NORMAL); + } + if (r == 0) _connect = true; return r; } @@ -854,8 +866,8 @@ return command(cmd, GSRES_NORMAL); } -void GSwifi::poll() { - int i, j; +void GSwifi::poll_cmd () { + int i; while (_gs_enter) { // received "\n" @@ -877,22 +889,39 @@ if (i == 0) { } else if (strncmp(buf, "CONNECT", 7) == 0 && buf[8] >= '0' && buf[8] <= 'F') { - i = x2i(buf[8]); - if (_gs_sock[i].type == GSTYPE_SERVER) { + int cid = x2i(buf[8]); + if (_gs_sock[cid].type == GSTYPE_SERVER) { int acid, ip1, ip2, ip3, ip4; char *tmp = buf + 12; acid = x2i(buf[10]); - newSock(acid, _gs_sock[i].type, _gs_sock[i].protocol, _gs_sock[i].onGsReceive); - _gs_sock[acid].lcid = i; + DBG("connect %d -> %d\r\n", cid, acid); + newSock(acid, _gs_sock[cid].type, _gs_sock[cid].protocol, _gs_sock[cid].onGsReceive); + _gs_sock[acid].lcid = cid; sscanf(tmp, "%d.%d.%d.%d", &ip1, &ip2, &ip3, &ip4); _gs_sock[acid].host.setIp(IpAddr(ip1, ip2, ip3, ip4)); tmp = strstr(tmp, " ") + 1; _gs_sock[acid].host.setPort(atoi(tmp)); + + if (_gs_sock[acid].protocol == GSPROT_HTTPD) { + poll_httpd(acid, 0); + } else + if (_gs_sock[acid].onGsReceive != NULL) { + _gs_sock[acid].onGsReceive(acid, 0); // event connected + } } } else if (strncmp(buf, "DISCONNECT", 10) == 0) { - _gs_sock[x2i(buf[11])].connect = false; + int cid = x2i(buf[11]); + DBG("disconnect %d\r\n", cid); + _gs_sock[cid].connect = false; + + if (_gs_sock[cid].protocol == GSPROT_HTTPD) { + poll_httpd(cid, 0); + } else + if (_gs_sock[cid].onGsReceive != NULL) { + _gs_sock[cid].onGsReceive(cid, 0); // event disconnected + } } else if (strncmp(buf, "DISASSOCIATED", 13) == 0 || strncmp(buf, "Disassociated", 13) == 0 || @@ -918,15 +947,27 @@ } DBG("status: %d\r\n", _status); } - +} + +void GSwifi::poll () { + int i, j; + + poll_cmd(); + for (i = 0; i < 16; i ++) { // if (_gs_sock[i].connect && _gs_sock[i].received) { if (_gs_sock[i].received && _gs_sock[i].data->use()) { // recv interrupt _gs_sock[i].received = 0; + if (_gs_sock[i].protocol == GSPROT_HTTPD) { + for (j = 0; j < 1500 / GS_DATA_SIZE + 1; j ++) { + if (! _gs_sock[i].connect || ! _gs_sock[i].data->use()) break; + poll_httpd(i, _gs_sock[i].data->use()); + } + } else if (_gs_sock[i].onGsReceive != NULL) { for (j = 0; j < 1500 / GS_DATA_SIZE + 1; j ++) { - if (! _gs_sock[i].data->use()) break; + if (! _gs_sock[i].connect || ! _gs_sock[i].data->use()) break; _gs_sock[i].onGsReceive(i, _gs_sock[i].data->use()); } } @@ -1005,7 +1046,8 @@ if (! _gs_sock[cid].connect) return -1; if ((_gs_sock[cid].protocol == GSPROT_TCP) || - (_gs_sock[cid].protocol == GSPROT_UDP && _gs_sock[cid].type == GSTYPE_CLIENT)) { + (_gs_sock[cid].protocol == GSPROT_UDP && _gs_sock[cid].type == GSTYPE_CLIENT) || + (_gs_sock[cid].protocol == GSPROT_HTTPD)) { // TCP Client, TCP Server, UDP Client _gs_ok = 0; _gs_failure = 0; @@ -1308,6 +1350,7 @@ } +#ifdef DEBUF // for test void GSwifi::test () { @@ -1342,3 +1385,4 @@ return _buf_cmd.use(); // return _buf_cmd.use() || (_gs_sock[0].data != NULL && _gs_sock[0].data->use()); } +#endif