Add a bunch of APNs
Fork of C027_Support by
Diff: MDM.cpp
- Revision:
- 103:197fa7920ad8
- Parent:
- 102:fb982ff9da82
- Child:
- 104:c64ba749a422
--- a/MDM.cpp Thu Jul 03 18:44:32 2014 +0000 +++ b/MDM.cpp Thu Jul 03 21:00:22 2014 +0000 @@ -7,8 +7,10 @@ #define PROFILE "0" //!< this is the psd profile used #define MAX_SIZE 128 //!< max expected messages +// num sockets +#define MAXSOCKET (sizeof(_sockets)/sizeof(*_sockets)) //! test if it is a socket -#define ISSOCKET(s) (((s) >= 0) && ((s) < (sizeof(_sockets)/sizeof(*_sockets)))) +#define ISSOCKET(s) (((s) >= 0) && ((s) < MAXSOCKET) && (_sockets[s].handle != SOCKET_ERROR)) //! check for timeout #define TIMEOUT(t, ms) ((ms != TIMEOUT_BLOCKING) && (ms < t.read_ms())) //! registration ok check helper @@ -86,6 +88,8 @@ _ip = NOIP; _init = false; memset(_sockets, 0, sizeof(_sockets)); + for (int socket = 0; socket < MAXSOCKET; socket ++) + _sockets[socket].handle = SOCKET_ERROR; #ifdef MDM_DEBUG _debugLevel = 1; _debugTime.start(); @@ -152,21 +156,24 @@ TRACE("New SMS at index %d\r\n", a); // Socket Specific Command --------------------------------- // +UUSORD: <socket>,<length> - } else if ((sscanf(cmd, "UUSORD: %d,%d", &a, &b) == 2) && - ISSOCKET(a) /*&& (_sockets[a].state == SOCK_CONNECTED)*/) { - TRACE("Socket %d: %d bytes pending\r\n", a, b); - _sockets[a].pending = b; + } else if ((sscanf(cmd, "UUSORD: %d,%d", &a, &b) == 2)) { + int socket = _findSocket(a); + TRACE("Socket %d: handle %d has %d bytes pending\r\n", socket, a, b); + if (socket != SOCKET_ERROR) + _sockets[socket].pending = b; // +UUSORF: <socket>,<length> - } else if ((sscanf(cmd, "UUSORF: %d,%d", &a, &b) == 2) && - ISSOCKET(a) /*&& (_sockets[a].state == SOCK_CONNECTED)*/) { - TRACE("Socket %d: %d bytes pending\r\n", a, b); - _sockets[a].pending = b; + } else if ((sscanf(cmd, "UUSORF: %d,%d", &a, &b) == 2)) { + int socket = _findSocket(a); + TRACE("Socket %d: handle %d has %d bytes pending\r\n", socket, a, b); + if (socket != SOCKET_ERROR) + _sockets[socket].pending = b; // +UUSOCL: <socket> - } else if ((sscanf(cmd, "UUSOCL: %d", &a) == 1) && - ISSOCKET(a) && (_sockets[a].state == SOCK_CONNECTED)) { - TRACE("Socket %d: closed by remote host\r\n", a); - _sockets[a].state = SOCK_CREATED/*=CLOSED*/; - } + } else if ((sscanf(cmd, "UUSOCL: %d", &a) == 1)) { + int socket = _findSocket(a); + TRACE("Socket %d: handle %d closed by remote host\r\n", socket, a); + if ((socket != SOCKET_ERROR) && _sockets[socket].connected) + _sockets[socket].connected = false; + } if (_dev.dev == DEV_LISA_C200) { // CDMA Specific ------------------------------------------- // +CREG: <n><SID>,<NID>,<stat> @@ -872,37 +879,42 @@ // ---------------------------------------------------------------- // sockets -int MDMParser::_cbUSOCR(int type, const char* buf, int len, int* socket) +int MDMParser::_cbUSOCR(int type, const char* buf, int len, int* handle) { - if ((type == TYPE_PLUS) && socket) { + if ((type == TYPE_PLUS) && handle) { const char* p = strstr(buf,"+USOCR: "); if (p) - *socket = atoi(p+8); + *handle = atoi(p+8); } return WAIT; } int MDMParser::socketSocket(IpProtocol ipproto, int port) { - int socket = SOCKET_ERROR; + int socket; LOCK(); + // find an free socket + socket = _findSocket(); TRACE("socketSocket(%d)\r\n", ipproto); - if ((ipproto == IPPROTO_UDP) && (port == -1)){ - sendFormated("AT+USOCR=17\r\n"); - } else if (ipproto == IPPROTO_UDP){ - sendFormated("AT+USOCR=17,%d\r\n", port); - } else /*(ipproto == IPPROTO_TCP)*/ { - sendFormated("AT+USOCR=6\r\n"); - } - if (RESP_OK != waitFinalResp(_cbUSOCR, &socket)) - socket = SOCKET_ERROR; - else if (!ISSOCKET(socket) || (_sockets[socket].state != SOCK_FREE)) - socket = SOCKET_ERROR; if (socket != SOCKET_ERROR) { - // successfull - _sockets[socket].state = SOCK_CREATED; - _sockets[socket].pending = 0; - _sockets[socket].timeout_ms = TIMEOUT_BLOCKING; + if ((ipproto == IPPROTO_UDP) && (port == -1)){ + sendFormated("AT+USOCR=17\r\n"); + } else if (ipproto == IPPROTO_UDP){ + sendFormated("AT+USOCR=17,%d\r\n", port); + } else /*(ipproto == IPPROTO_TCP)*/ { + sendFormated("AT+USOCR=6\r\n"); + } + int handle = SOCKET_ERROR; + if ((RESP_OK == waitFinalResp(_cbUSOCR, &handle)) && + (handle != SOCKET_ERROR)) { + TRACE("Socket %d: handle %d was created\r\n", socket, handle); + _sockets[socket].handle = handle; + _sockets[socket].timeout_ms = TIMEOUT_BLOCKING; + _sockets[socket].connected = false; + _sockets[socket].pending = 0; + } + else + socket = SOCKET_ERROR; } UNLOCK(); return socket; @@ -916,11 +928,11 @@ // connect to socket bool ok = false; LOCK(); - if (ISSOCKET(socket) && (_sockets[socket].state == SOCK_CREATED)) { + if (ISSOCKET(socket) && (!_sockets[socket].connected)) { TRACE("socketConnect(%d,%s,%d)\r\n", socket,host,port); - sendFormated("AT+USOCO=%d,\"" IPSTR "\",%d\r\n", socket, IPNUM(ip), port); + sendFormated("AT+USOCO=%d,\"" IPSTR "\",%d\r\n", _sockets[socket].handle, IPNUM(ip), port); if (RESP_OK == waitFinalResp()) - ok = _sockets[socket].state = SOCK_CONNECTED; + ok = _sockets[socket].connected = true; } UNLOCK(); return ok; @@ -931,8 +943,7 @@ bool ok = false; LOCK(); TRACE("socketIsConnected(%d)\r\n", socket); - ok = ISSOCKET(socket) && - _sockets[socket].state == SOCK_CONNECTED; + ok = ISSOCKET(socket) && _sockets[socket].connected; UNLOCK(); return ok; } @@ -941,7 +952,7 @@ { bool ok = false; LOCK(); - TRACE("socketSetBlocking(%d,%d)\r\n", socket, timeout_ms); + TRACE("socketSetBlocking(%d,%d)\r\n", socket,timeout_ms); if (ISSOCKET(socket)) { _sockets[socket].timeout_ms = timeout_ms; ok = true; @@ -954,11 +965,11 @@ { bool ok = false; LOCK(); - if (ISSOCKET(socket) && (_sockets[socket].state == SOCK_CONNECTED)) { + if (ISSOCKET(socket) && _sockets[socket].connected) { TRACE("socketClose(%d)\r\n", socket); - sendFormated("AT+USOCL=%d\r\n", socket); + sendFormated("AT+USOCL=%d\r\n", _sockets[socket].handle); if (RESP_OK == waitFinalResp()) { - _sockets[socket].state = SOCK_CREATED; + _sockets[socket].connected = false; ok = true; } } @@ -972,9 +983,12 @@ socketClose(socket); bool ok = true; LOCK(); - if (ISSOCKET(socket) && (_sockets[socket].state == SOCK_CREATED)) { - TRACE("socketFree(%d)\r\n", socket); - _sockets[socket].state = SOCK_FREE; + if (ISSOCKET(socket)) { + TRACE("socketFree(%d)\r\n", socket); + _sockets[socket].handle = SOCKET_ERROR; + _sockets[socket].timeout_ms = TIMEOUT_BLOCKING; + _sockets[socket].connected = false; + _sockets[socket].pending = 0; ok = true; } UNLOCK(); @@ -993,12 +1007,14 @@ blk = cnt; bool ok = false; LOCK(); - sendFormated("AT+USOWR=%d,%d\r\n",socket,blk); - if (RESP_PROMPT == waitFinalResp()) { - wait_ms(50); - send(buf, blk); - if (RESP_OK == waitFinalResp()) - ok = true; + if (ISSOCKET(socket)) { + sendFormated("AT+USOWR=%d,%d\r\n",_sockets[socket].handle,blk); + if (RESP_PROMPT == waitFinalResp()) { + wait_ms(50); + send(buf, blk); + if (RESP_OK == waitFinalResp()) + ok = true; + } } UNLOCK(); if (!ok) @@ -1011,7 +1027,7 @@ int MDMParser::socketSendTo(int socket, IP ip, int port, const char * buf, int len) { - TRACE("socketSendTo(%d," IPSTR ",%d,,%d)\r\n", socket, IPNUM(ip),port,len); + TRACE("socketSendTo(%d," IPSTR ",%d,,%d)\r\n", socket,IPNUM(ip),port,len); int cnt = len; while (cnt > 0) { int blk = USO_MAX_WRITE; @@ -1019,12 +1035,14 @@ blk = cnt; bool ok = false; LOCK(); - sendFormated("AT+USOST=%d,\"" IPSTR "\",%d,%d\r\n",socket,IPNUM(ip),port,blk); - if (RESP_PROMPT == waitFinalResp()) { - wait_ms(50); - send(buf, blk); - if (RESP_OK == waitFinalResp()) - ok = true; + if (ISSOCKET(socket)) { + sendFormated("AT+USOST=%d,\"" IPSTR "\",%d,%d\r\n",_sockets[socket].handle,IPNUM(ip),port,blk); + if (RESP_PROMPT == waitFinalResp()) { + wait_ms(50); + send(buf, blk); + if (RESP_OK == waitFinalResp()) + ok = true; + } } UNLOCK(); if (!ok) @@ -1039,11 +1057,11 @@ { int pending = SOCKET_ERROR; LOCK(); - if (ISSOCKET(socket) && (_sockets[socket].state == SOCK_CONNECTED)) { + if (ISSOCKET(socket) && _sockets[socket].connected) { TRACE("socketReadable(%d)\r\n", socket); // allow to receive unsolicited commands waitFinalResp(NULL, NULL, 0); - if (_sockets[socket].state == SOCK_CONNECTED) + if (_sockets[socket].connected) pending = _sockets[socket].pending; } UNLOCK(); @@ -1077,11 +1095,11 @@ bool ok = false; LOCK(); if (ISSOCKET(socket)) { - if (_sockets[socket].state == SOCK_CONNECTED) { + if (_sockets[socket].connected) { if (_sockets[socket].pending < blk) blk = _sockets[socket].pending; if (blk > 0) { - sendFormated("AT+USORD=%d,%d\r\n",socket, blk); + sendFormated("AT+USORD=%d,%d\r\n",_sockets[socket].handle, blk); if (RESP_OK == waitFinalResp(_cbUSORD, buf)) { _sockets[socket].pending -= blk; len -= blk; @@ -1095,7 +1113,7 @@ len = 0; ok = true; } - } else if (_sockets[socket].state == SOCK_CREATED) { + } else { len = 0; ok = true; } @@ -1140,7 +1158,7 @@ if (_sockets[socket].pending < blk) blk = _sockets[socket].pending; if (blk > 0) { - sendFormated("AT+USORF=%d,%d\r\n",socket, blk); + sendFormated("AT+USORF=%d,%d\r\n",_sockets[socket].handle, blk); USORFparam param; param.buf = buf; if (RESP_OK == waitFinalResp(_cbUSORF, ¶m)) { @@ -1169,6 +1187,14 @@ return cnt; } +int MDMParser::_findSocket(int handle) { + for (int socket = 0; socket < MAXSOCKET; socket ++) { + if (_sockets[socket].handle == handle) + return socket; + } + return SOCKET_ERROR; +} + // ---------------------------------------------------------------- int MDMParser::_cbCMGL(int type, const char* buf, int len, CMGLparam* param)