support library for C027 helper functions for Buffer Pipes, Buffered Serial Port (rtos capable) and GPS parsing. It includes modem APIs for USSD, SMS and Sockets.

Dependents:   HTTPClient_Cellular_HelloWorld Cellular_HelloMQTT MbedSmartRestMain Car_Bon_car_module ... more

This library is intended to be used with u-blox products such as the C027 or a shield with u-blox cellular and GPS modules like the cellular and positioning shield from Embedded Artist.

For 2G/GSM and 3G/UMTS you need to:

  • have a SIM card and know its PIN number
  • need to know you network operators APN setting These setting should be passed to the connect or init and join functions. You can also extend the APN database in MDMAPN.h.

For CDMA products you need to make sure that you have provisioned and activated the modem with either Sprint or Verizon.

Files at this revision

API Documentation at this revision

Comitter:
mazgch
Date:
Thu Jul 03 21:00:22 2014 +0000
Parent:
102:fb982ff9da82
Child:
104:c64ba749a422
Commit message:
do not use socket handle from modem as index directly

Changed in this revision

MDM.cpp Show annotated file Show diff for this revision Revisions of this file
MDM.h Show annotated file Show diff for this revision Revisions of this file
--- 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, &param)) {
@@ -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)
--- a/MDM.h	Thu Jul 03 18:44:32 2014 +0000
+++ b/MDM.h	Thu Jul 03 21:00:22 2014 +0000
@@ -526,7 +526,7 @@
     static int _cbUPSND(int type, const char* buf, int len, int* act);
     static int _cbUPSND(int type, const char* buf, int len, IP* ip);
     static int _cbUDNSRN(int type, const char* buf, int len, IP* ip);
-    static int _cbUSOCR(int type, const char* buf, int len, int* socket);
+    static int _cbUSOCR(int type, const char* buf, int len, int* handle);
     static int _cbUSORD(int type, const char* buf, int len, char* out);
     typedef struct { char* buf; IP ip; int port; } USORFparam;
     static int _cbUSORF(int type, const char* buf, int len, USORFparam* param);
@@ -544,11 +544,11 @@
     NetStatus   _net; //!< collected network information 
     IP          _ip;  //!< assigned ip address
     // management struture for sockets
-    typedef enum { SOCK_FREE, SOCK_CREATED, SOCK_CONNECTED } SockState;
-    typedef struct { volatile SockState state; volatile int pending; int timeout_ms; } SockCtrl;
-    // LISA-C has 6 TCP and 6 UDP sockets starting at index 18
-    // LISA-U and SARA-G have 7 sockets starting at index 1
-    SockCtrl _sockets[32];
+    typedef struct { int handle; int timeout_ms; volatile bool connected; volatile int pending; } SockCtrl;
+    // LISA-C has 6 TCP and 6 UDP sockets 
+    // LISA-U and SARA-G have 7 sockets
+    SockCtrl _sockets[12];
+    int _findSocket(int handle = SOCKET_ERROR/* = CREATE*/);
     static MDMParser* inst;
     bool _init;
 #ifdef TARGET_UBLOX_C027