Simple detection for LE910-NA1 modules

Fork of MTS-Cellular by MultiTech

Revision:
78:fc9d2b983744
Parent:
76:6eeffc10739d
diff -r 82c0ec0f73ba -r fc9d2b983744 Cellular/EasyIP.cpp
--- a/Cellular/EasyIP.cpp	Tue Jul 28 16:18:45 2015 +0000
+++ b/Cellular/EasyIP.cpp	Mon Aug 17 21:05:41 2015 +0000
@@ -3,6 +3,7 @@
 #include "MTSText.h"
 #include "MTSLog.h"
 #include "CellUtils.h"
+#include <string>
 
 using namespace mts;
 
@@ -14,6 +15,7 @@
     dtr = NULL;
     resetLine = NULL;
     echoMode = true;
+    gpsEnabled = false;
     pppConnected = false;
     socketMode = TCP;
     socketOpened = false;
@@ -680,3 +682,126 @@
     }
     return status;
 }
+
+bool EasyIP::GPSenable() {
+//The HE910 returns an ERROR if you try to enable when it is already enabled.
+// That's why we need to check if GPS is enabled before enabling it.
+    if(GPSenabled()) {
+        logInfo("GPS was already enabled.");
+        return true;
+    }
+//The LE910-NAG requires AT$GPSSLSR=2,3 to enable GPS but can use AT$GPSP=0 to disable it.    
+    if(type == MTSMC_LAT1){
+        Code code = sendBasicCommand("AT$GPSSLSR=2,3", 2000);
+        if (code == MTS_SUCCESS) {
+            gpsEnabled = true;        
+            logInfo("GPS enabled.");
+            return true;
+        } else {
+            logError("Enable GPS failed!");        
+            return false;
+        }        
+    } else {
+        Code code = sendBasicCommand("AT$GPSP=1", 2000);
+        if (code == MTS_SUCCESS) {
+            gpsEnabled = true;        
+            logInfo("GPS enabled.");
+            return true;
+        } else {
+            logError("Enable GPS failed.");
+            return false;
+        }
+    }
+}
+
+bool EasyIP::GPSdisable() {
+// The HE910 returns an ERROR if you try to disable when it is already disabled.
+// That's why we need to check if GPS is disabled before disabling it.
+    if(!GPSenabled()) {
+        logInfo("GPS was already disabled.");
+        return true;
+    }
+    Code code = sendBasicCommand("AT$GPSP=0", 2000);
+    if (code == MTS_SUCCESS) {
+        gpsEnabled = false;        
+        logInfo("GPS disabled.");
+        return true;
+    } else {
+        logError("Disable GPS failed.");
+        return false;
+    }
+}
+
+bool EasyIP::GPSenabled() {
+    std::string reply = sendCommand("AT$GPSP?", 1000);
+    if(reply.find("1") != std::string::npos) {
+        gpsEnabled = true;
+        return true;
+    } else {
+        gpsEnabled = false;
+        return false;
+    }
+}
+
+Cellular::gpsData EasyIP::GPSgetPosition(){
+    enum gpsFields{time, latitude, longitude, hdop, altitude, fix, cog, kmhr, knots, date, satellites, numOfFields };
+    Cellular::gpsData position;
+    if(!gpsEnabled) {
+        logError("GPS is disabled... can't get position.");
+        position.success = false;
+        return position;
+    }
+    // Get the position information in string format.
+    std::string gps = sendCommand("AT$GPSACP?", 1000);
+    if(gps.find("OK") != std::string::npos) {
+        position.success = true;
+        // Remove echoed AT$GPSACP and leading non position characters.
+        gps.erase(0,22);
+        // Remove trailing CR/LF, CR/LF, OK and CR/LF.
+        gps.erase(gps.end()-8, gps.end());
+        // Split remaining data and load into corresponding structure fields.
+        std::vector<std::string> gpsParts = Text::split(gps, ',');
+        // Check size.
+        if(gpsParts.size() != numOfFields) {
+            logError("Expected %d fields but there are %d fields in \"%s\"", numOfFields, gpsParts.size(), gps.c_str());
+            position.success = false;
+            return position; 
+        }
+        position.latitude = gpsParts[latitude];
+        position.longitude = gpsParts[longitude];
+        position.hdop = atof(gpsParts[hdop].c_str());
+        position.altitude = atof(gpsParts[altitude].c_str());
+        position.fix = atoi(gpsParts[fix].c_str());
+        position.cog = gpsParts[cog];
+        position.kmhr = atof(gpsParts[kmhr].c_str());
+        position.knots = atof(gpsParts[knots].c_str());
+        position.satellites = atoi(gpsParts[satellites].c_str());
+        if((gpsParts[date].size() == 6) && (gpsParts[time].size() == 10)) {
+            position.timestamp = gpsParts[date].substr(4,2) + "/" + gpsParts[date].substr(2,2) + 
+            "/" + gpsParts[date].substr(0,2) + ", " + gpsParts[time].substr(0,2) + 
+            ":" + gpsParts[time].substr(2,2) + ":" + gpsParts[time].substr(4,6);        
+        }
+        return position;     
+    } else {
+        position.success = false;
+        logError("NO \"OK\" returned from GPS position command \"AT$GPSACP?\".");
+        return position;
+    }
+}   
+    
+bool EasyIP::GPSgotFix() {
+    if(!gpsEnabled) {
+        logError("GPS is disabled... can't get fix.");
+        return false;
+    }
+    Cellular::gpsData position = GPSgetPosition();
+    if(!position.success) {
+        return false;
+    } else if(position.fix < 2){
+        logWarning("No GPS fix. GPS fix can take a few minutes. Check GPS antenna attachment and placement.");
+        return false;
+    } else {
+        logInfo("Got GPS fix.");
+        return true;
+    }
+}
\ No newline at end of file