Simple detection for LE910-NA1 modules

Fork of MTS-Cellular by MultiTech

Revision:
35:257eb41405e1
Parent:
33:3b6f3904dde0
Child:
38:b2088faa8bfd
Child:
39:629fc8475af1
--- a/Cellular/EasyIP.cpp	Wed Jul 16 15:05:10 2014 +0000
+++ b/Cellular/EasyIP.cpp	Mon Jul 21 20:14:18 2014 +0000
@@ -45,6 +45,9 @@
 
     logDebug("radio type: %s", Cellular::getRadioNames(type).c_str());
     //Turns on the HW flow control, equiv. to AT&K
+    if(sendBasicCommand("AT&K=3", 2000) != MTS_SUCCESS) {
+        logWarning("Failed to set flow control to radio");
+    }
     if(sendBasicCommand("AT+IFC=2,2", 2000) != MTS_SUCCESS) {
         logWarning("Failed to set flow control to radio");
     }
@@ -77,6 +80,7 @@
     //Check Registration: AT+CREG? == 0,1
     //(Does the AT command inside Cellular class)
     tmr.start();
+    
     do {
         Registration registration = getRegistration();
         if(registration != REGISTERED) {
@@ -108,7 +112,7 @@
     }
     //The main thing going on; Sends the AT command to start a connection
     //Assuming context is already stored in the modem
-    std::string pppResult = sendCommand("AT#SGACT=1,1", 2000);
+    std::string pppResult = sendCommand("AT#SGACT=1,1", 5000);
     std::vector<std::string> parts;
     if(pppResult.find("OK") != std::string::npos) {
         parts = Text::split(pppResult, "\r\n");
@@ -125,6 +129,7 @@
             pppConnected = false;
         } else {
             if(pppResult.find("1,1") != std::string::npos) {
+                logDebug("Radio is already connected");
                 pppConnected = true;
             } else {
                 pppConnected = false;
@@ -146,12 +151,32 @@
                     //(and thus need socket closed)
         }
     }
+    std::string result;
+    Timer tmr;
     //Sends AT#SGACT=1,0 command
-    if(sendBasicCommand("AT#SGACT=1,0", 1000) == MTS_SUCCESS) {
+    if (sendBasicCommand("AT#SGACT=1,0", 1000) == MTS_SUCCESS) {
         pppConnected = false;
         logDebug("Successfully closed PPP Connection");
     }
-    pppConnected = false; //Cell will drop connection if we go silent
+    
+    tmr.start();
+    while(tmr.read() < 30) {
+        result = sendCommand("AT#SGACT?", 1000);
+        if(result.find("1,0") != std::string::npos) {
+            pppConnected = false;
+            break;
+        } else if(result.find("ERROR") != std::string::npos) {
+            break;
+        } else {
+            wait(1);
+        }
+    }
+    
+    if(pppConnected) {
+        wait(30);
+        pppConnected = false;
+    }
+    
     return;
 }
 
@@ -252,7 +277,6 @@
             if(active) {
                 if(ping()) {
                     pppConnected = true;
-                    logWarning("Internal PPP state tracking differs from radio (DISCONNECTED:CONNECTED)");
                     stateString = "CONNECTED";
                 } else {
                     stateString = "CONNECTING";
@@ -263,14 +287,15 @@
         }
     } else if(regist != signal) {
         stateString = "CHECKING";
-        pppConnected = false;
+        return false;
     } else if(!regist && !signal) {
         stateString = "DISCONNECTED";
         pppConnected = false;
     }
-    //Log results if necessary
-    if(stateString != "CONNECTED") {
-        logWarning("Internal PPP state tracking differs from radio (CONNECTED:%s)",stateString.c_str());
+    
+    std::string pppStatus = pppConnected ? "CONNECTED" : "DISCONNECTED";
+    if(stateString != pppStatus) {
+        logDebug("Internal PPP state[%s], radio state[%s])",pppStatus.c_str() ,stateString.c_str());
     }
     return pppConnected;
 }
@@ -341,11 +366,6 @@
             logDebug("PPP connection established");
         }
     }
-    //No way to "set" port except on socket call;
-    //Going to need to warn if local_port was not set.
-    if(!local_port) {
-        logDebug("No local port was set: 0");
-    }
     
     //4) Set escape sequence to not be transmitted
     if(sendBasicCommand("AT#SKIPESC=1", 2000) != MTS_SUCCESS) {
@@ -367,7 +387,7 @@
     }
     //5) Open Socket  
     sprintf(sOpenSocketCmd, "AT#SD=1,%d,%d,%s,%d,%d,0", typeSocket, port, address.c_str(),closeType , local_port);
-    std::string response = sendCommand(sOpenSocketCmd, 5000);
+    std::string response = sendCommand(sOpenSocketCmd, 140000);
     
     if(response.find("CONNECT") != std::string::npos) {
         host_address = address;
@@ -460,9 +480,10 @@
     } else {
         bytesRead = io->read(data, max);
     }
+    
+    //Scan for socket closed message
     if(bytesRead > 0 && socketCloseable) {
-        //Scan for socket closed message
-        for(size_t i = 0; i < bytesRead; i++) {
+        for(int i = 0; i < bytesRead; i++) {
             if(data[i] == 'N') {
                 if(strstr(&data[i], "NO CARRIER")) {
                     logTrace("Found socket closed message. Checking validity");
@@ -618,7 +639,9 @@
     sprintf(buffer, "AT#PING=%s,1,32,%d", address.c_str(), (PINGDELAY*10));
     
     for(int pngs=0; pngs<PINGNUM; pngs++) {
-        std::string response = sendCommand(buffer, (PINGDELAY*1010)); //Send 1 ping
+        std::string response = sendCommand(buffer, (PINGDELAY*2000)); //Send 1 ping
+        wait(0.5); //Radio seems to get stuck if no wait is incurred between issuing ping commands
+                   //leads to unknown registration state eventually :(
         if(response.empty()) {
             continue; //Skip current loop if send command fails
         }
@@ -673,8 +696,7 @@
         return false;
     }
     if(!socketOpened) {
-        logError("Socket is not open. Can not send AT escape sequence (+++)");
-        return false;
+        logError("Socket is not open. +++ Escape sequence should fail");
     }
     
     if(!socketCloseable) {
@@ -686,10 +708,10 @@
     io->txClear();
     
     std::string result;
-    unsigned int timeoutMillis = 2000;
+    unsigned int timeoutMillis = 20000; //20s
     const int size_cmd = 3;
     //Attempt to write command
-    wait(1); //Format for +++ command is 1 second wait, send +++, then another second wait
+    wait(1.2); //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) {