C027 updated to work with latest mBed libraries

Dependents:   Cellular_HelloMQTT UBLOXModemDriver UBLOXMQTTDriver

Fork of C027_Support by u-blox

Revision:
76:f7c3dd568dae
Parent:
75:ce6e12067d0c
Child:
77:55788e619858
--- a/MDM.cpp	Fri May 16 14:13:00 2014 +0000
+++ b/MDM.cpp	Mon May 19 13:05:41 2014 +0000
@@ -33,7 +33,7 @@
             else if (ch == '\v') ::printf("\\v"); // Vertical Tab (0x0B)
             else if (ch == '\f') ::printf("\\f"); // Formfeed (0x0C)
             else if (ch == '\r') ::printf("\\r"); // Carriage Return (0x0D)
-            else                 ::printf("\\x%02x", ch);
+            else                 ::printf("\\x%02x", (unsigned char)ch);
         }
     }
     ::printf("\"\r\n");
@@ -75,21 +75,14 @@
     _net.lac = 0xFFFF;
     _net.ci = 0xFFFFFFFF;
     _ip        = NOIP;
+    _init      = false;
     memset(_sockets, 0, sizeof(_sockets));
 #ifdef MDM_DEBUG
+    _debugLevel = 1;
     _debugTime.start();
 #endif
 }
 
-MDMParser::~MDMParser(void)
-{
-    powerOff();
-#ifdef TARGET_UBLOX_C027
-    if (_onboard)
-        c027_mdm_powerOff();
-#endif
-}
-
 int MDMParser::send(const char* buf, int len)
 {
 #ifdef MDM_DEBUG
@@ -279,6 +272,7 @@
 bool MDMParser::init(const char* simpin, DevStatus* status, PinName pn)
 {
     int i = 10;
+    memset(&_dev, 0, sizeof(_dev));
     if (pn != NC) {
         INFO("Modem::wakeup\r\n");
         DigitalOut pin(pn, 1);
@@ -297,6 +291,7 @@
             return false;
         }
     }
+    _init = true;
     
     INFO("Modem::init\r\n");
     // echo off
@@ -436,10 +431,13 @@
 
 bool MDMParser::powerOff(void)
 {
-    INFO("Modem::powerOff\r\n");
-    sendFormated("AT+CPWROFF\r\n");
-    if (RESP_OK != waitFinalResp(NULL,NULL,120*1000))
-        return false;
+    if (_init) {
+        INFO("Modem::powerOff\r\n");
+        sendFormated("AT+CPWROFF\r\n");
+        if (RESP_OK != waitFinalResp(NULL,NULL,120*1000))
+            return false;
+        _init = false;
+    }
     return true;
 }
 
@@ -607,15 +605,16 @@
     TRACE("Modem::join\r\n");
     _ip = NOIP;
     if (_dev.dev == DEV_LISA_C200) {
-        // TODO: is there something to do here?
-#if 0
+        // make a dumy dns lookup (which will fail, so ignore the result) 
+        sendFormated("AT+UDNSRN=0,\"u-blox.com\"\r\n");
+        waitFinalResp(); 
+        // This fake lookup will enable the IP connection and we 
+        // should have an IP after this, so we check it
+        
         //Get local IP address
         sendFormated("AT+CMIP?\r\n");
         if (RESP_OK != waitFinalResp(_cbCMIP, &_ip))
             return NOIP;
-#else
-        return 0x01010101; // a fake IP
-#endif
     } else { 
         // check gprs attach status 
         sendFormated("AT+CGATT=1\r\n");
@@ -713,7 +712,7 @@
         return true;
     INFO("Modem::disconnect\r\n");
     if (_dev.dev == DEV_LISA_C200) {
-        // TODO: is there something to do here?
+        // There something to do here
     } else { 
         sendFormated("AT+UPSDA=" PROFILE ",4\r\n");
         if (RESP_OK != waitFinalResp())
@@ -1281,6 +1280,24 @@
 // Serial Implementation 
 // ----------------------------------------------------------------
 
+/*! Helper Dev Null Device 
+    Small helper class used to shut off stderr/stdout. Sometimes stdin/stdout
+    is shared with the serial port of the modem. Having printfs inbetween the 
+    AT commands you cause a failure of the modem.
+*/
+class DevNull : public Stream {
+public: 
+    DevNull() : Stream(_name+1) { }             //!< Constructor
+    void claim(const char* mode, FILE* file) 
+        { freopen(_name, mode, file); }         //!< claim a stream
+protected:
+    virtual int _getc()         { return EOF; } //!< Nothing
+    virtual int _putc(int c)    { return c; }   //!< Discard
+    static const char* _name;                   //!< File name
+};
+const char* DevNull::_name = "/null";  //!< the null device name
+static      DevNull null;              //!< the null device
+
 MDMSerial::MDMSerial(PinName tx /*= MDMTXD*/, PinName rx /*= MDMRXD*/, 
             int baudrate /*= MDMBAUD*/,
 #if DEVICE_SERIAL_FC
@@ -1289,9 +1306,15 @@
             int rxSize /*= 256*/, int txSize /*= 128*/) : 
             SerialPipe(tx, rx, rxSize, txSize) 
 {
+    if (rx == USBRX) 
+        null.claim("r", stdin);
+    if (tx == USBTX) {
+        null.claim("w", stdout);
+        null.claim("w", stderr);
 #ifdef MDM_DEBUG
-    _debugLevel = (tx == USBTX) ? -1 : 1;
-#endif    
+        _debugLevel = -1;
+#endif
+    }
 #ifdef TARGET_UBLOX_C027
     _onboard = (tx == MDMTXD) && (rx == MDMRXD);
     if (_onboard)
@@ -1309,6 +1332,15 @@
 #endif
 }
 
+MDMSerial::~MDMSerial(void)
+{
+    powerOff();
+#ifdef TARGET_UBLOX_C027
+    if (_onboard)
+        c027_mdm_powerOff();
+#endif
+}
+
 int MDMSerial::_send(const void* buf, int len)   
 { 
     return put((const char*)buf, len, true/*=blocking*/);
@@ -1324,7 +1356,7 @@
 // ----------------------------------------------------------------
 
 #ifdef HAVE_MDMUSB
-MDMUsb(void)                             
+MDMUsb::MDMUsb(void)                             
 { 
 #ifdef MDM_DEBUG
     _debugLevel = 1;
@@ -1334,6 +1366,18 @@
     c027_mdm_powerOn(true);
 #endif
 }
-int MDMUsb::_send(const void* buf, int len)      { return len; }
+
+MDMUsb::~MDMUsb(void)
+{
+    powerOff();
+#ifdef TARGET_UBLOX_C027
+    if (_onboard)
+        c027_mdm_powerOff();
+#endif
+}
+
+int MDMUsb::_send(const void* buf, int len)      { return 0; }
+
 int MDMUsb::getLine(char* buffer, int length)    { return NOT_FOUND; }
+
 #endif