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

Dependents:   mtsas mtsas mtsas mtsas

Revision:
56:43205bd2752a
Parent:
54:a6c738bfc391
Child:
57:7eb2eb536d4a
--- a/Cellular/EasyIP.cpp	Mon Aug 11 21:01:50 2014 +0000
+++ b/Cellular/EasyIP.cpp	Wed Aug 13 16:46:10 2014 +0000
@@ -43,7 +43,7 @@
 
     logDebug("radio type: %s", Cellular::getRadioNames(type).c_str());
     //Turns on the HW flow control
-    if(sendBasicCommand("AT&K3", 2000) != MTS_SUCCESS) {
+    if(sendBasicCommand("AT+IFC=2,2", 2000) != MTS_SUCCESS) {
         logWarning("Failed to enable serial flow control");
     }
     return true;
@@ -74,7 +74,7 @@
     tmr.start();
     do {
         Registration registration = getRegistration();
-        if(registration != REGISTERED) {
+        if(registration != REGISTERED && registration != ROAMING) {
             logTrace("Not Registered [%d] ... waiting", (int)registration);
             wait(1);
         } else {
@@ -201,7 +201,7 @@
         regist = false;
     }
     
-    string reply = sendCommand("AT#SGACT?", 500);
+    string reply = sendCommand("AT#SGACT?", 1000);
     if (reply.find("1,1") != std::string::npos) {
         active = true;
     } else {
@@ -211,18 +211,14 @@
     RadioState state;
     bool ppp = pppConnected;
     if (signal && regist && active) {
-        //Radio connected
         state = CONNECTED;
         pppConnected = true;
     } else if (signal && !regist && !active) {
-        //Radio idle
         state = IDLE;
         pppConnected = false;
     } else if (active) {
-        //Radio Connecting
         state = CONNECTING;
     } else {
-        //Radio Disconnected
         state = DISCONNECTED;
         pppConnected = false;
     }
@@ -230,7 +226,25 @@
     if (!ppp && state == CONNECTED) {
         logWarning("Internal PPP state tracking differs from radio (DISCONNECTED:CONNECTED)");
     } else if (ppp && state != CONNECTED) {
-        logWarning("Internal PPP state tracking differs from radio (CONNECTED:%s)", state);
+        string stateStr;
+        switch (state) {
+            case IDLE:
+                stateStr = "IDLE";
+                break;
+            case CONNECTING:
+                stateStr = "CONNECTING";
+                break;
+            case DISCONNECTED:
+                stateStr = "DISCONNECTED";
+                break;
+            case CONNECTED:
+                stateStr = "CONNECTED";
+                break;
+            default:
+                stateStr = "UKNOWN";
+                break;
+        }
+        logWarning("Internal PPP state tracking differs from radio (CONNECTED:%s)", stateStr.c_str());
     }
     
     return pppConnected;
@@ -248,21 +262,6 @@
     }
 }
 
-//Binds the socket to a specific port if able
-bool EasyIP::bind(unsigned int port)
-{
-    if(socketOpened) {
-        logError("socket is open. Can not set local port");
-        return false;
-    }
-    if(port > 65535) {
-        logError("port out of range (0-65535)");
-        return false;
-    }
-    local_port = port;
-    return true;
-}
-
 bool EasyIP::open(const std::string& address, unsigned int port, Mode mode)
 {
     char sOpenSocketCmd[256] = {0}; //String for AT command
@@ -310,7 +309,7 @@
         }
     }
     
-    //4) Set escape sequence to not be transmitted
+    //4) Set escape sequence to not be transmitted through socket
     if(sendBasicCommand("AT#SKIPESC=1", 2000) != MTS_SUCCESS) {
         logWarning("Failed to disable escape sequence transmission on data mode suspension");
     }
@@ -347,15 +346,6 @@
     return socketOpened;
 }
 
-bool EasyIP::isOpen()
-{
-    if(io->readable()) {
-        logDebug("Assuming open, data available to read.");
-        return true;
-    }
-    return socketOpened;
-}
-
 bool EasyIP::close()
 {
     
@@ -498,78 +488,22 @@
     return bytesWritten;
 }
 
-unsigned int EasyIP::readable()
-{
-    if(io == NULL) {
-        logWarning("MTSBufferedIO not set");
-        return 0;
-    }
-    if(!socketOpened && !io->readable()) {
-        logWarning("Socket is not open");
-        return 0;
-    }
-    return io->readable();
-}
-
-unsigned int EasyIP::writeable()
-{
-    if(io == NULL) {
-        logWarning("MTSBufferedIO not set");
-        return 0;
-    }
-    if(!socketOpened) {
-        logWarning("Socket is not open");
-        return 0;
-    }
-
-    return io->writeable();
-}
-
-bool EasyIP::setDeviceIP(std::string address)
-{
-    if (address.compare("DHCP") == 0) {
-        return true;
-    } else {
-        logWarning("Radio does not support static IPs, using DHCP.");
-        return false;
-    }
-}
-
 Code EasyIP::setApn(const std::string& apn)
 {
     if (type == MTSMC_H5 || type == MTSMC_G3) {
-         //Set IP,PPP,IPv6
-        Code code = sendBasicCommand("AT+CGDCONT=1,PPP," + apn, 1000);
+         //CGDCONT has options: IP,PPP,IPv6
+        Code code = sendBasicCommand("AT+CGDCONT=1,IP," + apn, 1000);
         if (code != MTS_SUCCESS) {
-            return code; //This will return whatever is not MTS_SUCCESS
+            return code;
         }
         this->apn = apn;
-        return code; //This will return MTS_SUCCESS
+        return code;
     } else {
         logInfo("CDMA radios don't need an APN");
         return MTS_SUCCESS;
     }
 }
 
-std::string EasyIP::getDeviceIP()
-{
-    return local_address;
-}
-
-//Turns off echo when it receives a true, turns on when it receives false
-Code EasyIP::echo(bool state)
-{
-    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};
@@ -604,24 +538,6 @@
     return false;
 }
 
-//Pass 1 to enable socket closeable
-//Pass 0 to disable socket closeable
-Code EasyIP::setSocketCloseable(bool enabled)
-{
-    if(socketCloseable == enabled) {
-        return MTS_SUCCESS;
-    }
-
-    if(socketOpened) {
-        logError("socket is already opened. Can not set closeable");
-        return MTS_ERROR;
-    }
-
-    socketCloseable = enabled;
-
-    return MTS_SUCCESS;
-}
-
 bool EasyIP::sendEscapeCommand()
 {
     //string Cellular::sendCommand(const std::string& command, unsigned int timeoutMillis, char esc)
@@ -690,8 +606,8 @@
 
 bool EasyIP::socketCheck() {
     bool status = false;
-    int socketInfo = 9; //error
-    enum SocketStatus {SOCKETCLOSED, SOCKETACTIVEDATA, SOCKETSUSPEND, SOCKETSUSPENDDATA, SOCKETLISTEN, SOCKETINCOMING, ERROR = 9};
+    int socketInfo = ERROR;
+    enum SocketStatus {SOCKETCLOSED = 0, SOCKETACTIVEDATA = 1, SOCKETSUSPEND = 2, SOCKETSUSPENDDATA = 3, SOCKETLISTEN = 4, SOCKETINCOMING = 5, ERROR = 9};
     std::vector<std::string> params;
     
     //Goes from data mode to command mode
@@ -704,14 +620,14 @@
             socketInfo = atoi(params[1].c_str());
         } else {
             logError("Could not determine socket status[%d]",socketInfo);
-            socketInfo = 9; //9 is unrecognized
+            socketInfo = ERROR;
         }
     } else {
         status = false; //Return value of socketOpened when checking
     }
     
     //Check socket status query
-    if(socketInfo < 5 && socketInfo > 0) { //Socket opened responses
+    if(socketInfo == SOCKETINCOMING || socketInfo == SOCKETACTIVEDATA || socketInfo == SOCKETSUSPEND || socketInfo == SOCKETSUSPENDDATA || socketInfo == SOCKETLISTEN) { //Socket opened responses
         status = true;
     } else if(socketInfo == SOCKETCLOSED || socketInfo == SOCKETINCOMING) {
         status = false;