Cellular library for MTS Socket Modem Arduino Shield devices from Multi-Tech Systems

Dependents:   mtsas mtsas mtsas mtsas

Revision:
27:ec44d5a9544f
Parent:
26:2b769ed8de4f
Child:
28:f93d7b3f7c2e
--- a/Cellular/EasyIP.cpp	Thu Jun 26 21:12:37 2014 +0000
+++ b/Cellular/EasyIP.cpp	Mon Jun 30 17:13:00 2014 +0000
@@ -98,7 +98,7 @@
         }
     } while(tmr.read() < 30);
 
-    //AT#CONNECTIONSTART: Make a PPP connection
+    //Similar to AT#CONNECTIONSTART: Make a PPP connection
     if (type == MTSMC_H5 || type == MTSMC_G3) {
         logDebug("Making PPP Connection Attempt. APN[%s]", apn.c_str());
     } else {
@@ -140,12 +140,14 @@
     } else {
         logError("Closing PPP Connection. Continuing ...");
     }
-    pppConnected = false; 
+    pppConnected = false; //We can do this since the cell will drop their side after timeout
 }
-//++++++++++++++++++++++++++++++++++++++++++++
+
 bool EasyIP::isConnected()
 {
     
+    
+    
     return pppConnected;
 }
 //Binds the socket to a specific port if able
@@ -155,19 +157,19 @@
    
     return true;
 }
-//++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+
 bool EasyIP::open(const std::string& address, unsigned int port, Mode mode)
 {
    
     return socketOpened;
 }
-//++++++++++++++++++++++++++++++++++++++++++++++++++++++
+
 bool EasyIP::isOpen()
 {
     
     return socketOpened;
 }
-//++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+
 bool EasyIP::close()
 {
     
@@ -224,7 +226,7 @@
         return MTS_SUCCESS;
     }
 }
-//++++++++++++++++++++++++++++++++++++++++++++++++++
+
 void EasyIP::reset()
 {
 }
@@ -237,29 +239,51 @@
 //Turns off echo when it receives a 1, turns on when it receives anything else
 Code EasyIP::echo(bool state)
 {
-    return MTS_SUCCESS;
+    Code code;
+    if (state) {
+        code = sendBasicCommand("ATE0", 1000);
+        echoMode = (code == MTS_SUCCESS) ? false : echoMode;
+    } else {
+        code = sendBasicCommand("ATE1", 1000);
+        echoMode = (code == MTS_SUCCESS) ? true : echoMode;
+    }
+    return code;
 }
 
 bool EasyIP::ping(const std::string& address)
 {
     char buffer[256] = {0};
-    std::string response;
+    std::vector<std::string> parts;
+    int pingsRec=0;
+    int TTL=0;
+    int Timeout=0;
+    
+    //Format parameters for sending to radio
+    sprintf(buffer, "AT#PING=%s,1,32,%d", address.c_str(), (10*PINGDELAY));
     
-    //Sends "PINGNUM" of pings to "address" with a timeout of "PINGDELAY"
-    //AT command takes pingdelay as units of 100ms, so seconds would be 10 times bigger
-    //Concatenate parameters into one string
-    sprintf(buffer, "AT#PING=%s,%d,32,%d", address.c_str(), PINGNUM, (10*PINGDELAY));
-    response = sendCommand(buffer,PINGDELAY*10000);
-    if(response.find("OK") != std::string::npos) {
-        //Not sure if I need a wait here, or if it is included in the sendcommand wait time
-        return true;
-    }
-    
-
+    for(int pngs=0; pngs<PINGNUM; pngs++) {
+        std::string response = sendCommand(buffer, (PINGDELAY*1500)); //Send 1 ping
+        if(response.empty()) continue; //Skip current loop if send command fails
+        parts = Text::split(response, "\r\n");
+        parts = Text::split(parts[1], ",");
+        //Parse TTL and Timeout values
+        Timeout = std::atoi(parts[2].c_str());
+        TTL = std::atoi(parts[3].c_str());
+                
+        if((Timeout < 600) && (TTL < 255)) {
+            pingsRec++;
+        }
+    }   //Success if less than 50% packet loss
+        if( ((pingsRec/PINGNUM)>= 0.5) ) {
+            return true;
+        }
     return false;
 }
 
+//Pass 1 to enable socket closeable
+//Pass 0 to disable socket closeable
 Code EasyIP::setSocketCloseable(bool enabled)
 {
+
     return MTS_SUCCESS;
 }