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

Dependents:   mtsas mtsas mtsas mtsas

Files at this revision

API Documentation at this revision

Comitter:
Vanger
Date:
Wed Jul 02 15:06:03 2014 +0000
Parent:
28:f93d7b3f7c2e
Child:
30:1326b623919a
Commit message:
Added APN check for connect() under EasyIP.; Implemented isConnected() under EasyIP. ; Note for isConnected(): some states from UIP will never be reached in the EasyIP version, and main method of checking connection is a ping to google.com

Changed in this revision

Cellular/EasyIP.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/Cellular/EasyIP.cpp	Tue Jul 01 19:50:39 2014 +0000
+++ b/Cellular/EasyIP.cpp	Wed Jul 02 15:06:03 2014 +0000
@@ -57,6 +57,14 @@
 
 bool EasyIP::connect()
 {
+    //Check if APN is not set, if so, connect will not work.
+    if (type == MTSMC_H5_IP || type == MTSMC_H5 || type == MTSMC_G3) {
+        if(apn.size() == 0) {
+            logDebug("APN is not set");
+            return false;
+        }
+    }
+    
     //Check if socket is open
     //flag stored in Cellular.h
     if(socketOpened) {
@@ -149,9 +157,106 @@
 
 bool EasyIP::isConnected()
 {
+    std::string stateString;
+    std::vector<std::string> pieces;
+    bool signal = false, regist = false, active = false, ping = false;
     
+    //1) Check if APN was set if we're on an HSPA radio
+    if (type == MTSMC_H5_IP || type == MTSMC_H5 || type == MTSMC_G3) {
+        if(apn.size() == 0) {
+            logDebug("APN is not set");
+            return false;
+        }
+    }
+    
+    //2) Check that we do not have a live connection up
+    if(socketOpened) {
+        logDebug("Socket is opened");
+        return true;
+    }
+    
+    //3) Query the radio
+    //Check antenna signal
+    std::string reply = sendCommand("AT+CSQ", 1000);
+    if(reply.empty() || (reply.find("ERROR") != std::string::npos)) {
+        signal = false;
+    } else {
+        pieces = Text::split(reply, "\r\n");
+        if(pieces.size() >= 2) {
+            pieces = Text::split(pieces[1], " ");
+            if(pieces.size() >= 2) {
+                if((pieces[1].find("0,0") != std::string::npos) || (pieces[1].find("99,99") != std::string::npos)) {
+                    signal = false;
+                } else {
+                    signal = true;
+                }
+            }
+        }
+    }
     
+    //Check cell tower registration
+    reply = sendCommand("AT+CREG?", 1000);
+    if(reply.empty() || (reply.find("ERROR") != std::string::npos)) {
+        regist = false;
+    } else {
+        pieces = Text::split(reply, "\r\n");
+        if(pieces.size() >= 2) {
+            pieces = Text::split(pieces[1], " ");
+            if(pieces.size() >= 2) {
+                if((pieces[1].find("0,1") != std::string::npos) || (pieces[1].find("0,5") != std::string::npos)) {
+                    regist = true; //1 for connected, 5 for roaming connected
+                } else {
+                    regist = false; //Cell tower not registered
+                }
+            }
+        }
+    }
     
+    //Check internet connection through ping, admittedly uses data over cell network
+    ping = EasyIP::ping("www.google.com");
+    
+    //Check active mode (SGACT = 1,1)
+    reply = sendCommand("AT#SGACT?", 1000);
+    if(reply.empty() || (reply.find("ERROR") != std::string::npos)) {
+        active = false;
+    } else {
+        pieces = Text::split(reply, "\r\n");
+        if(pieces.size() >= 2) {
+            pieces = Text::split(pieces[1], " ");
+            if(pieces.size() >= 2) {
+                if(pieces[1].find("1,1") != std::string::npos) {
+                    active = true; //1 for an active connection mode
+                } else {
+                    active = false; //0, or unknown value, is an inactive connection mode
+                }
+            }
+        }
+    }
+    
+    if(ping) {
+        if(!pppConnected) {
+            logWarning("Internal PPP state tracking differs from radio (DISCONNECTED:CONNECTED)");
+        }
+        pppConnected = true;
+    } else {
+        std::string stateStr;
+        if(pppConnected) {
+                //New code for state from boolean values
+                if(regist && signal) {
+                    if(active) {
+                        stateString = "AUTHENTICATING";
+                    } else {
+                        stateString = "IDLE";
+                    }
+                } else if(regist != signal) {
+                    stateString = "CHECKING";
+                } else {
+                    stateString = "DISCONNECTED";
+                }
+                logWarning("Internal PPP state tracking differs from radio (CONNECTED:%s)", stateString.c_str());
+        }
+        pppConnected = false;
+    }
     return pppConnected;
 }
 //Binds the socket to a specific port if able