for TwitterMbed
Fork of GSwifi_old by
Diff: GSwifi.cpp
- Revision:
- 5:6def1d0df519
- Parent:
- 4:a8d38857f3fd
- Child:
- 6:a423f0d197de
--- a/GSwifi.cpp Thu Jul 12 14:40:02 2012 +0000 +++ b/GSwifi.cpp Fri Jul 13 04:14:14 2012 +0000 @@ -214,7 +214,7 @@ case GSMODE_DATA_RX_BULK: case GSMODE_DATA_RXUDP_BULK: - DBG("%c", dat); +// DBG("%c", dat); if (mode == 0) { // cid _cid = x2i(dat); @@ -264,7 +264,9 @@ } else if (mode == 4) { // data - _gs_sock[_cid].data->put(dat); + if (_gs_sock[_cid].data != NULL) { + _gs_sock[_cid].data->put(dat); + } len --; if (len == 0) { DBG("recv binary %d\r\n", _cid); @@ -291,7 +293,7 @@ _gs.printf("\r\n"); wait_ms(100); while (_gs.readable()) { - _gs_getc(); // dummy read + i = _gs_getc(); // dummy read } return 0; } @@ -425,6 +427,21 @@ flg = 1; } break; + case GSRES_TIME: + if (buf[0] >= '0' && buf[0] <= '9') { + int year, month, day, hour, min, sec; + struct tm t; + sscanf(buf, "%d/%d/%d,%d:%d,%d", &day, &month, &year, &hour, &min, &sec); + t.tm_sec = sec; + t.tm_min = min; + t.tm_hour = hour; + t.tm_mday = day; + t.tm_mon = month - 1; + t.tm_year = year - 1900; + _time = mktime(&t); + flg = 1; + } + break; } if ((flg && _gs_ok) || _gs_failure) break; @@ -770,6 +787,46 @@ return _rssi; } +int GSwifi::ntpdate (Host host, int sec) { + char cmd[GS_CMD_SIZE]; + + if (! _connect || _status != GSSTAT_READY) return -1; + + if (host.getIp().isNull()) { + if (getHostByName(host)) { + if (getHostByName(host)) return -1; + } + } + + if (sec) { + sprintf(cmd, "AT+NTIMESYNC=1,%d.%d.%d.%d,%d,1,%d", host.getIp()[0], host.getIp()[1], host.getIp()[2], host.getIp()[3], + GS_TIMEOUT / 1000, sec); + } else { + sprintf(cmd, "AT+NTIMESYNC=1,%d.%d.%d.%d,%d,0", host.getIp()[0], host.getIp()[1], host.getIp()[2], host.getIp()[3], + GS_TIMEOUT / 1000); + } + return command(cmd, GSRES_NORMAL); +} + +int GSwifi::setTime (time_t time) { + char cmd[GS_CMD_SIZE]; + struct tm *t; + + if (_status != GSSTAT_READY) return -1; + + t = localtime(&time); + sprintf(cmd, "AT+SETTIME=%d/%d/%d,%d:%d:%d", t->tm_mday, t->tm_mon + 1, t->tm_year + 1900, t->tm_hour, t->tm_min, t->tm_sec); + return command(cmd, GSRES_NORMAL); +} + +time_t GSwifi::getTime () { + + if (command("AT+GETTIME=?", GSRES_TIME)) { + return 0; + } + return _time; +} + void GSwifi::poll() { int i;