Own fork of C027_Support

Dependents:   MbedSmartRestMain MbedSmartRestMain

Fork of C027_Support by u-blox

Revision:
35:9275215a3a5b
Parent:
33:fb8fb5021b09
Child:
36:c4df7bcf9b6e
--- a/MDM.cpp	Wed Apr 09 14:25:41 2014 +0000
+++ b/MDM.cpp	Thu Apr 10 13:05:45 2014 +0000
@@ -4,7 +4,7 @@
 #include "MDM.h"
 
 #define TRACE           (0)?:printf
-//#define DEBUG         // enable this for AT command debugging
+#define DEBUG         // enable this for AT command debugging
 #define PROFILE         "0"   // this is the psd profile used
 #define MAX_SIZE        256  // max expected messages
 // some helper 
@@ -40,7 +40,7 @@
 {
     memset(&_dev, 0, sizeof(_dev));
     memset(&_net, 0, sizeof(_net));
-    _ip      = NOIP;
+    _ip        = NOIP;
     memset(_sockets, 0, sizeof(_sockets));
 }
 
@@ -78,6 +78,7 @@
             int len = LENGTH(ret);
             int type = TYPE(ret);
             const char* s = (type == TYPE_UNKNOWN)? YEL("UNK") : 
+                            (type == TYPE_TEXT)   ? MAG("TXT") : 
                             (type == TYPE_OK   )  ? GRE("OK ") : 
                             (type == TYPE_ERROR)  ? RED("ERR") : 
                             (type == TYPE_PLUS)   ? CYA(" + ") : 
@@ -179,7 +180,7 @@
 
 int MDMParser::_cbString(int type, const char* buf, int len, char* str)
 {
-    if (str && (type == TYPE_UNKNOWN)) {
+    if (str && (type == TYPE_TEXT)) {
         if (sscanf(buf, "\r\n%s\r\n", str) == 1)
             /*nothing*/;
     }
@@ -188,7 +189,7 @@
 
 int MDMParser::_cbInt(int type, const char* buf, int len, int* val)
 {
-    if (val && (type == TYPE_UNKNOWN)) {
+    if (val && (type == TYPE_TEXT)) {
         if (sscanf(buf, "\r\n%d\r\n", val) == 1)
             /*nothing*/;
     }
@@ -199,12 +200,15 @@
 
 bool MDMParser::init(const char* pin, DevStatus* status)
 {
-    for(int i = 0; i < 5; i++) {
+    int i = 5;
+    while (i--) {
         // check interface and disable local echo
         sendFormated("AT\r\n");
         if(OK == waitFinalResp())
             break;
     }
+    if (i == 0)
+        return false;
     // echo off
     sendFormated("AT E0\r\n");
     if(OK != waitFinalResp())
@@ -226,10 +230,6 @@
         return false;
     // device specific init
     if (_dev.dev == DEV_LISA_C200) {
-        // disable flow control
-        sendFormated("AT+IFC=0,0\r\n");
-        if (OK != waitFinalResp())
-            return false;
         // get the manufacturer
         sendFormated("AT+GMI\r\n");
         if (OK != waitFinalResp(_cbString, _dev.manu))
@@ -247,20 +247,13 @@
         if (OK != waitFinalResp(_cbString, _dev.meid))
             return false;
     } else {
-        // disable flow control
-        sendFormated("AT&K0\r\n");
-        if (OK != waitFinalResp())
-            return false;
-        // enable power saving
-        sendFormated("AT+UPSV=1\r\n");
-        if (OK != waitFinalResp())
-            return false;
-        // enable the network identification feature 
         if (_dev.dev == DEV_LISA_U200) {
+            // enable the network identification feature 
             sendFormated("AT+UGPIOC=20,2\r\n");
             if (OK != waitFinalResp())
                 return false;
         } else {
+            // enable the network identification feature 
             sendFormated("AT+UGPIOC=16,2\r\n");
             if (OK != waitFinalResp())
                 return false;
@@ -324,6 +317,14 @@
     sendFormated("AT+CIMI\r\n");
     if (OK != waitFinalResp(_cbString, _dev.imsi))
         return false;
+    // enable power saving
+    if (_dev.lpm != LPM_DISABLED) {
+         // enable power saving (requires flow control, cts at least)
+        sendFormated("AT+UPSV=1\r\n");
+        if (OK != waitFinalResp())
+            return false;  
+        _dev.lpm = LPM_ACTIVE;
+    }
     if (status)
         memcpy(status, &_dev, sizeof(DevStatus));
     return true; 
@@ -331,7 +332,7 @@
 
 int MDMParser::_cbATI(int type, const char* buf, int len, Dev* dev)
 {
-    if ((type == TYPE_UNKNOWN) && dev) {
+    if ((type == TYPE_TEXT) && dev) {
         if (strstr(buf, "SARA-G350")) {
             *dev = DEV_SARA_G350;
             /*TRACE("Identified Device: SARA-G350 2G\\n")*/;
@@ -857,7 +858,7 @@
         if (type == TYPE_PLUS) {
             if (sscanf(buf, "\r\n+CMGR: \"%*[^\"]\",\"%[^\"]", param->num) == 1) {
             }
-        } else if ((type == TYPE_UNKNOWN) && (buf[len-2] == '\r') && (buf[len-1] == '\n')) {
+        } else if ((type == TYPE_TEXT) && (buf[len-2] == '\r') && (buf[len-1] == '\n')) {
             memcpy(param->buf, buf, len-2);
             param->buf[len-2] = '\0';
         }
@@ -912,6 +913,10 @@
         }
     }
     if (!end)                               return o; // no termination
+    // at least any char
+    if (++o > len)                      return WAIT;
+    pipe->next();
+    // check the end     
     int x = 0;
     while (end[x]) {
         if (++o > len)                      return WAIT;
@@ -988,6 +993,7 @@
             { "\r\n+",                  "\r\n",             TYPE_PLUS       },
             { "\r\n@",                  NULL,               TYPE_PROMPT     }, // Sockets
             { "\r\n>",                  NULL,               TYPE_PROMPT     }, // SMS
+            { "\r\n",                   "\r\n",             TYPE_TEXT       },
         };
         for (int i = 0; i < sizeof(lutF)/sizeof(*lutF); i ++) {
             pipe->set(unkn);
@@ -1022,22 +1028,26 @@
 
 MDMSerial::MDMSerial(PinName tx /*= MDMTXD*/, PinName rx /*= MDMRXD*/, 
             int baudrate /*= MDMBAUD*/,
-#if DEVICE_SERIAL_FC
             PinName rts /*= MDMRTS*/, PinName cts /*= MDMCTS*/, 
-#endif
             int rxSize /*= 256*/, int txSize /*= 128*/) : 
-#if DEVICE_SERIAL_FC
-            SerialPipe(tx, rx, rts, cts, rxSize, txSize)
-#else
-            SerialPipe(tx, rx, rxSize, txSize)
-#endif
+            SerialPipe(tx, rx, rxSize, txSize) 
 {
     baud(baudrate);
+    // have it fix low 
+#if DEVICE_SERIAL_FC
+    if ((rts != NC) || (cts != NC))
+    {
+        Flow flow = (cts == NC) ? RTS :
+                    (rts == NC) ? CTS : RTSCTS ;
+        set_flow_control(flow, rts, cts);
+        if (cts != NC) _dev.lpm = LPM_ENABLED;
+    }
+#endif
 }
 
 int MDMSerial::_send(const void* buf, int len)   
 { 
-    return put((const char*)buf, len, true/*=blocking*/); 
+    return put((const char*)buf, len, true/*=blocking*/);
 }
 
 int MDMSerial::getLine(char* buffer, int length)
@@ -1054,4 +1064,4 @@
 MDMUsb::MDMUsb(void)                             { }
 int MDMUsb::_send(const void* buf, int len)      { return len; }
 int MDMUsb::getLine(char* buffer, int length)    { return NOT_FOUND; }
-#endif
\ No newline at end of file
+#endif