Simple detection for LE910-NA1 modules

Fork of MTS-Cellular by MultiTech

Revision:
33:3b6f3904dde0
Parent:
32:7d5581159bed
Child:
35:257eb41405e1
--- a/Cellular/EasyIP.cpp	Tue Jul 15 20:37:08 2014 +0000
+++ b/Cellular/EasyIP.cpp	Wed Jul 16 14:26:10 2014 +0000
@@ -6,121 +6,10 @@
 
 using namespace mts;
 
-bool EasyIP::sendEscapeCommand()
-{
-    //string Cellular::sendCommand(const std::string& command, unsigned int timeoutMillis, char esc)
-    if(io == NULL) {
-        logError("MTSBufferedIO not set");
-        return false;
-    }
-    if(!socketOpened) {
-        logError("Socket is not open. Can not send AT escape sequence (+++)");
-        return false;
-    }
-    
-    if(!socketCloseable) {
-        logError("Socket is not closeable");
-        return false;
-    }
-    
-    io->rxClear();
-    io->txClear();
-    
-    std::string result;
-    unsigned int timeoutMillis = 2000;
-    const int size_cmd = 3;
-    //Attempt to write command
-    wait(1); //Format for +++ command is 1 second wait, send +++, then another second wait
-             //1s wait after command is implemented as a polling function for 2 seconds
-             //Option: Could change wait periods to be longer/shorter (0-255)*50ms
-    if(io->write("+++", size_cmd, timeoutMillis) != size_cmd) {
-        //Failed to write command
-        logError("failed to send command to radio within %d milliseconds", timeoutMillis);
-        return false;
-    }
-    
-    int timer = 0;
-    char tmp[256];
-    tmp[255] = 0;
-    bool done = false;
-    io->read(tmp,255,0);
-    bool exitmode = false;
-    
-    do {
-        wait(0.1);
-        timer += 100;
-        //Make a non-blocking read call by passing timeout of zero
-            int size = io->read(tmp,255,0);    //1 less than allocated (timeout is instant)
-            if(size > 0) {
-                result.append(tmp, size);
-            }
-        if(result.find("OK") != std::string::npos) {
-            exitmode = true;
-            done = true;
-        } else if(result.find("NO CARRIER") != std::string::npos) {
-            exitmode = true;
-            done = true;
-        } else if(result.find("ERROR") != std::string::npos) {
-            exitmode = false;
-            done = true;
-        }
-        if(timer >= timeoutMillis) {
-            logDebug("Escape sequence [+++] timed out after %d milliseconds", timeoutMillis);
-            exitmode = true;
-            done = true;
-        }
-    } while (!done);
-    
-    return exitmode;
-}
 
-bool EasyIP::socketCheck() {
-    bool status = false;
-    std::string socketInfo = "9"; //9 is unrecognized
-    std::vector<std::string> params;
-    
-    if(sendEscapeCommand()) {
-        socketOpened = false;
-        if(sendBasicCommand("AT", 1000) == MTS_SUCCESS) {
-            socketInfo = sendCommand("AT#SS=1", 2000);
-            if(socketInfo.find("OK") != std::string::npos) {
-                //Found valid response
-                params = Text::split(socketInfo, "\r\n");
-                params = Text::split(params[1], ",");
-                socketInfo = params[1];
-                //Check comparison of params[1] to response codes
-            } else {
-                logError("Could not determine socket status[%s]",socketInfo.c_str());
-                socketInfo == "9"; //9 is unrecognized
-            }
-        }
-    } else {
-        status = false; //Return value of socketOpened when checking
-    }
-    //Check socket status query
-    if(socketInfo == "2" || socketInfo == "3" || socketInfo == "1" || socketInfo == "4") {
-        status = true; //2 and 3 are suspended connections
-    } else if(socketInfo == "0" || socketInfo == "5") {
-        status = false; //0 is closed socket, probably won't occur
-    } else {
-        logError("Could not determine socket status");
-        status = false; //anything else is unknown status
-    }
-    
-    if(status) {
-        std::string reconnect = sendCommand("AT#SO=1", 2000);
-        if(reconnect.find("CONNECT") != std::string::npos || reconnect.find("OK") != std::string::npos) {
-        } else {
-            logError("Failed to resume socket connection");
-        }
-    }
-    return status;
-}
 
 EasyIP::EasyIP(Radio type)
 {
-    //Not sure how the construction process is done, 
-    //but assuming it works for both EasyIP and UIP the same way.
     this->type = type;
     io = NULL;
     dcd = NULL;
@@ -138,8 +27,6 @@
 
 EasyIP::~EasyIP()
 {
-    //Same reasoning for the destructor as the constructor,
-    //assuming it works for UIP, it will work for EasyIP
     if (dtr != NULL) {
         dtr->write(1);
     }
@@ -166,7 +53,7 @@
 
 bool EasyIP::connect()
 {
-    //Check if APN is not set, if so, connect will not work.
+    //Check if APN is not set, if it is not, connect will not work.
     if (type == MTSMC_H5_IP || type == MTSMC_H5 || type == MTSMC_G3) {
         if(apn.size() == 0) {
             logDebug("APN is not set");
@@ -327,7 +214,7 @@
         }
     }
     
-    //Check active mode (SGACT = 1,1)
+    //Check active context (SGACT = 1,1)
     reply = sendCommand("AT#SGACT?", 500);
     if(reply.empty() || (reply.find("ERROR") != std::string::npos)) {
         active = false;
@@ -388,6 +275,18 @@
     return pppConnected;
 }
 
+//Resets the radio
+void EasyIP::reset()
+{
+    disconnect();
+    if(sendBasicCommand("AT#REBOOT", 10000) != MTS_SUCCESS) {
+        logError("Socket Modem did not accept RESET command\n\r");
+    } else {
+        logWarning("Socket Modem is resetting, allow 30 seconds for it to come back\n\r");
+        return;
+    }
+}
+
 //Binds the socket to a specific port if able
 bool EasyIP::bind(unsigned int port)
 {
@@ -678,7 +577,7 @@
          //Set IP,PPP,IPv6
         Code code = sendBasicCommand("AT+CGDCONT=1,PPP," + apn, 1000);
         if (code != MTS_SUCCESS) {
-            return code;
+            return code; //This will return whatever is not MTS_SUCCESS
         }
         this->apn = apn;
         return code; //This will return MTS_SUCCESS
@@ -688,17 +587,6 @@
     }
 }
 
-void EasyIP::reset()
-{
-    disconnect();
-    if(sendBasicCommand("AT#REBOOT", 10000) != MTS_SUCCESS) {
-        logError("Socket Modem did not accept RESET command\n\r");
-    } else {
-        logWarning("Socket Modem is resetting, allow 30 seconds for it to come back\n\r");
-        return;
-    }
-}
-
 std::string EasyIP::getDeviceIP()
 {
     return local_address;
@@ -776,3 +664,117 @@
 
     return MTS_SUCCESS;
 }
+
+bool EasyIP::sendEscapeCommand()
+{
+    //string Cellular::sendCommand(const std::string& command, unsigned int timeoutMillis, char esc)
+    if(io == NULL) {
+        logError("MTSBufferedIO not set");
+        return false;
+    }
+    if(!socketOpened) {
+        logError("Socket is not open. Can not send AT escape sequence (+++)");
+        return false;
+    }
+    
+    if(!socketCloseable) {
+        logError("Socket is not closeable");
+        return false;
+    }
+    
+    io->rxClear();
+    io->txClear();
+    
+    std::string result;
+    unsigned int timeoutMillis = 2000;
+    const int size_cmd = 3;
+    //Attempt to write command
+    wait(1); //Format for +++ command is 1 second wait, send +++, then another second wait
+             //1s wait after command is implemented as a polling function for 2 seconds
+             //Option: Could change wait periods to be longer/shorter (0-255)*50ms
+    if(io->write("+++", size_cmd, timeoutMillis) != size_cmd) {
+        //Failed to write command
+        logError("failed to send command to radio within %d milliseconds", timeoutMillis);
+        return false;
+    }
+    
+    int timer = 0;
+    char tmp[256];
+    tmp[255] = 0;
+    bool done = false;
+    io->read(tmp,255,0);
+    bool exitmode = false;
+    
+    do {
+        wait(0.1);
+        timer += 100;
+        //Make a non-blocking read call by passing timeout of zero
+            int size = io->read(tmp,255,0);    //1 less than allocated (timeout is instant)
+            if(size > 0) {
+                result.append(tmp, size);
+            }
+        if(result.find("OK") != std::string::npos) {
+            exitmode = true;
+            done = true;
+        } else if(result.find("NO CARRIER") != std::string::npos) {
+            exitmode = true;
+            done = true;
+        } else if(result.find("ERROR") != std::string::npos) {
+            exitmode = false;
+            done = true;
+        }
+        if(timer >= timeoutMillis) {
+            logDebug("Escape sequence [+++] timed out after %d milliseconds", timeoutMillis);
+            exitmode = true;
+            done = true;
+        }
+    } while (!done);
+    
+    return exitmode;
+}
+
+bool EasyIP::socketCheck() {
+    bool status = false;
+    std::string socketInfo = "9"; //9 is unrecognized
+    std::vector<std::string> params;
+    
+    //Goes from data mode to command mode
+    if(sendEscapeCommand()) {
+        socketOpened = false;
+        if(sendBasicCommand("AT", 1000) == MTS_SUCCESS) {
+            socketInfo = sendCommand("AT#SS=1", 2000);
+            if(socketInfo.find("OK") != std::string::npos) {
+                //Found valid response
+                params = Text::split(socketInfo, "\r\n");
+                params = Text::split(params[1], ",");
+                socketInfo = params[1];
+                //Check comparison of params[1] to response codes
+            } else {
+                logError("Could not determine socket status[%s]",socketInfo.c_str());
+                socketInfo == "9"; //9 is unrecognized
+            }
+        }
+    } else {
+        status = false; //Return value of socketOpened when checking
+    }
+    
+    //Check socket status query
+    if(socketInfo == "2" || socketInfo == "3" || socketInfo == "1" || socketInfo == "4") {
+        status = true; //2 and 3 are suspended connections
+    } else if(socketInfo == "0" || socketInfo == "5") {
+        status = false; //0 is closed socket, probably won't occur
+    } else {
+        logError("Could not determine socket status");
+        status = false; //anything else is unknown status
+    }
+    
+    //Reconnects to active socket if able
+    if(status) {
+        std::string reconnect = sendCommand("AT#SO=1", 2000);
+        if(reconnect.find("CONNECT") != std::string::npos || reconnect.find("OK") != std::string::npos) {
+        } else {
+            logError("Failed to resume socket connection");
+        }
+    }
+    return status;
+}