Own fork of C027_Support

Dependents:   MbedSmartRestMain MbedSmartRestMain

Fork of C027_Support by u-blox

Files at this revision

API Documentation at this revision

Comitter:
mazgch
Date:
Mon May 19 13:05:41 2014 +0000
Parent:
75:ce6e12067d0c
Child:
77:55788e619858
Commit message:
- properly manage power in con/destructors ; - disable the stdout/stderr/stdin if the modem shares the same port (class DevNull)

Changed in this revision

GPS.cpp Show annotated file Show diff for this revision Revisions of this file
GPS.h Show annotated file Show diff for this revision Revisions of this file
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/GPS.cpp	Fri May 16 14:13:00 2014 +0000
+++ b/GPS.cpp	Mon May 19 13:05:41 2014 +0000
@@ -6,15 +6,6 @@
  #include "C027_api.h"
 #endif
 
-GPSParser::~GPSParser(void) 
-{ 
-    powerOff(); 
-#ifdef TARGET_UBLOX_C027
-    if (_onboard)
-         c027_gps_powerOff();
-#endif
-}
-    
 void GPSParser::powerOff(void)
 {
     // set the gps into backup mode using the command RMX-LPREQ
@@ -258,6 +249,15 @@
 #endif
 }
 
+GPSSerial::~GPSSerial(void)
+{
+    powerOff();
+#ifdef TARGET_UBLOX_C027
+    if (_onboard)
+         c027_gps_powerOff();
+#endif
+}
+
 bool GPSSerial::init(PinName pn)
 {
     // send a byte to wakup the device again
@@ -301,6 +301,15 @@
 #endif
 }
 
+GPSI2C::~GPSI2C(void)
+{
+    powerOff();
+#ifdef TARGET_UBLOX_C027
+    if (_onboard)
+         c027_gps_powerOff();
+#endif
+}
+
 bool GPSI2C::init(PinName pn)
 {
     if (pn != NC) {
--- a/GPS.h	Fri May 16 14:13:00 2014 +0000
+++ b/GPS.h	Mon May 19 13:05:41 2014 +0000
@@ -15,10 +15,6 @@
 class GPSParser
 {
 public:
-
-    //! Constructor
-    ~GPSParser(void);
-   
     /** Power on / Wake up the gps 
     */
     virtual bool init(PinName pn) = 0;
@@ -184,6 +180,9 @@
               int rxSize    = 256 , 
               int txSize    = 128 );
               
+    //! Destructor
+    ~GPSSerial(void);
+    
     virtual bool init(PinName pn = NC);
     
     /** Get a line from the physical interface. 
@@ -219,6 +218,8 @@
            PinName scl          GPS_IF( = GPSSCL, = D15 ),
            unsigned char i2cAdr GPS_IF( = GPSADR, = (66<<1) ), 
            int rxSize           = 256 );
+    //! Destructor
+    ~GPSI2C(void);
     
     /** helper function to probe the i2c device
         \return true if successfully detected the gps. 
--- 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
--- a/MDM.h	Fri May 16 14:13:00 2014 +0000
+++ b/MDM.h	Mon May 19 13:05:41 2014 +0000
@@ -23,9 +23,7 @@
 {
 public:
     //! Constructor 
-    MDMParser();
-    //! Destructor 
-    ~MDMParser();
+    MDMParser(void);
     //! get static instance
     static MDMParser* getInstance() { return inst; };
     
@@ -37,7 +35,7 @@
     //! SIM Status
     typedef enum { SIM_UNKNOWN, SIM_MISSING, SIM_PIN, SIM_READY } Sim;
     //! SIM Status
-    typedef enum { LPM_DISABLED, LPM_ENABLED, LPM_ACTIVE, LPM_SLEEP } Lpm; 
+    typedef enum { LPM_DISABLED, LPM_ENABLED, LPM_ACTIVE } Lpm; 
     //! Device status
     typedef struct { 
         Dev dev;            //!< Device Type
@@ -505,6 +503,7 @@
     // LISA-U and SARA-G have 7 sockets starting at index 1
     SockCtrl _sockets[32];
     static MDMParser* inst;
+    bool _init;
 #ifdef TARGET_UBLOX_C027
     bool _onboard;
 #endif
@@ -543,6 +542,9 @@
  #endif
               int rxSize    = 256 , 
               int txSize    = 128 );
+    //! Destructor          
+    ~MDMSerial(void);
+    
     /** Get a line from the physical interface. 
         \param buf the buffer to store it
         \param buf size of the buffer
@@ -568,7 +570,10 @@
 class MDMUsb :  /*public UsbSerial,*/ public MDMParser
 {
 public: 
+    //! Constructor          
     MDMUsb(void);
+    //! Destructor          
+    ~MDMUsb(void);
     virtual int getLine(char* buffer, int length);
 protected:
     virtual int _send(const void* buf, int len);