uitgy
Dependents: App_Pese_Ruche_SYSBEE
Fork of C027_Support by
Diff: MDM.cpp
- Revision:
- 44:9d12223b78ff
- Parent:
- 43:a89a7a505991
- Child:
- 45:ebc2fd8dcf21
diff -r a89a7a505991 -r 9d12223b78ff MDM.cpp --- a/MDM.cpp Fri Apr 11 20:10:28 2014 +0000 +++ b/MDM.cpp Thu Apr 17 20:41:30 2014 +0000 @@ -36,8 +36,11 @@ #define WHY(t) COL("37m",t) #endif +MDMParser* MDMParser::inst; + MDMParser::MDMParser(void) { + inst = this; memset(&_dev, 0, sizeof(_dev)); memset(&_net, 0, sizeof(_net)); _ip = NOIP; @@ -635,6 +638,7 @@ // successfull _sockets[socket].state = SOCK_CREATED; _sockets[socket].pending = 0; + _sockets[socket].timeout_ms = 0; // non blocking return socket; } @@ -653,6 +657,19 @@ return true; } +bool MDMParser::socketIsConnected(int socket) +{ + return (ISSOCKET(socket) && (_sockets[socket].state == SOCK_CONNECTED)); +} + +bool MDMParser::socketSetBlocking(int socket, unsigned int timeout_ms) +{ + if (!ISSOCKET(socket)) + return false; + _sockets[socket].timeout_ms = timeout_ms; + return true; +} + bool MDMParser::socketClose(int socket) { if (!ISSOCKET(socket) || (_sockets[socket].state != SOCK_CONNECTED)) @@ -660,6 +677,7 @@ sendFormated("AT+USOCL=%d\r\n", socket); if (OK != waitFinalResp()) return false; + _sockets[socket].state = SOCK_CREATED; return true; } @@ -729,27 +747,33 @@ if (!ISSOCKET(socket)) return SOCKET_ERROR; memset(buf, '\0', len); + Timer timer; + timer.start(); while (len) { int blk = MAX_SIZE - 64; // still need space for headers and unsolicited commands - if (_sockets[socket].state != SOCK_CONNECTED) - return cnt ? cnt : SOCKET_ERROR; if (_sockets[socket].pending < blk) blk = _sockets[socket].pending; if (len < blk) blk = len; if (blk) { - sendFormated("AT+USORD=%d,%d\r\n",socket, blk); + sendFormated("AT+USORD=%d,%d\r\n",socket, blk); if (OK != waitFinalResp(_cbUSORD, buf)) { - return cnt ? cnt : SOCKET_ERROR; + return SOCKET_ERROR; } len -= blk; cnt += blk; buf += blk; _sockets[socket].pending -= blk; - } else { + } else if ((_sockets[socket].state == SOCK_CONNECTED) && ( + (_sockets[socket].timeout_ms == (unsigned int)-1 /* blocking */) || + (timer.read_ms() < _sockets[socket].timeout_ms))){ // allow to receive unsolicited commands waitFinalResp(NULL, NULL, 10); + } else { + len = 0; // no more data and socket closed or timed-out } } + timer.stop(); + timer.reset(); return cnt; } @@ -774,10 +798,10 @@ if (!ISSOCKET(socket)) return SOCKET_ERROR; memset(buf, '\0', len); + Timer timer; + timer.start(); while (len) { int blk = MAX_SIZE - 64; // still need space for headers and unsolicited commands - if (_sockets[socket].state != SOCK_CONNECTED) - return cnt ? cnt : SOCKET_ERROR; if (_sockets[socket].pending < blk) blk = _sockets[socket].pending; if (len < blk) blk = len; @@ -786,7 +810,7 @@ USORFparam param; param.buf = buf; if (OK != waitFinalResp(_cbUSORF, ¶m)) { - return cnt ? cnt : SOCKET_ERROR; + return SOCKET_ERROR; } *ip = param.ip; //*port = param.port; @@ -794,11 +818,17 @@ cnt += blk; buf += blk; _sockets[socket].pending -= blk; - } else { + } else if ((_sockets[socket].state == SOCK_CONNECTED) && ( + (_sockets[socket].timeout_ms == (unsigned int)-1 /* blocking */) || + (timer.read_ms() < _sockets[socket].timeout_ms))){ // allow to receive unsolicited commands waitFinalResp(NULL, NULL, 10); + } else { + len = 0; // no more data and socket closed or timed-out } } + timer.stop(); + timer.reset(); return cnt; }