A library for talking to Multi-Tech's Cellular SocketModem Devices.

Dependents:   M2X_dev axeda_wrapper_dev MTS_M2x_Example1 MTS_Cellular_Connect_Example ... more

Revision:
71:82205735732b
Parent:
68:c490e4a51778
Child:
82:5aa75004e553
Child:
84:77c5ab16534d
--- a/cellular/Cellular.cpp	Tue Dec 24 01:15:44 2013 +0000
+++ b/cellular/Cellular.cpp	Thu Dec 26 16:12:49 2013 +0000
@@ -104,7 +104,7 @@
     }
     
     Code code = sendBasicCommand("AT#CONNECTIONSTOP", 10000);
-    if(code == CELL_OK) {
+    if(code == SUCCESS) {
         printf("[DEBUG] Successfully closed PPP Connection\r\n");
     } else {
         printf("[ERROR] Closing PPP Connection [%d].  Continuing ...\r\n", (int)code);   
@@ -192,7 +192,7 @@
         //Attempt to set local port
         sprintf(buffer, "AT#OUTPORT=%d", local_port);
         Code code = sendBasicCommand(buffer, 1000);
-        if(code != CELL_OK) {
+        if(code != SUCCESS) {
             printf("[WARNING] Unable to set local port (%d) [%d]\r\n", local_port, (int) code);       
         }
     }
@@ -201,7 +201,7 @@
     if(mode == TCP) {
         if(socketCloseable) {
             Code code = sendBasicCommand("AT#DLEMODE=1,1", 1000);
-            if(code != CELL_OK) {
+            if(code != SUCCESS) {
                 printf("[WARNING] Unable to set socket closeable [%d]\r\n", (int) code);       
             }
         }
@@ -211,7 +211,7 @@
     } else {
         if(socketCloseable) {
             Code code = sendBasicCommand("AT#UDPDLEMODE=1", 1000);
-            if(code != CELL_OK) {
+            if(code != SUCCESS) {
                 printf("[WARNING] Unable to set socket closeable [%d]\r\n", (int) code);       
             }
         }
@@ -220,13 +220,13 @@
         addressCode = sendBasicCommand("AT#UDPSERV=\"" + address + "\"", 1000);
     }
     
-    if(portCode == CELL_OK) {
+    if(portCode == SUCCESS) {
         host_port = port;
     } else {
         printf("[ERROR] Host port could not be set\r\n");
     }
     
-    if(addressCode == CELL_OK) {
+    if(addressCode == SUCCESS) {
         host_address = address;
     } else {
         printf("[ERROR] Host address could not be set\r\n");
@@ -477,18 +477,18 @@
 void Cellular::reset() {
     disconnect();
     Code code = sendBasicCommand("AT#RESET=0", 10000);
-    if(code != CELL_OK) {
+    if(code != SUCCESS) {
         printf("[ERROR] Socket Modem did not accept RESET command\n\r");
     } else {
         printf("[WARNING] Socket Modem is resetting, allow 30 seconds for it to come back\n\r");
     }
 }
 
-Cellular::Code Cellular::test()
+Code Cellular::test()
 {
     Code code = sendBasicCommand("AT", 1000);
     
-    if(code != CELL_OK) {
+    if(code != SUCCESS) {
         printf("[Error] Failed basic AT command");
         return code;
     }
@@ -496,18 +496,18 @@
     //AT#VSTATE != "CHECKING"
     
     //AT#GPRSMODE == 
-    return CELL_OK;
+    return SUCCESS;
 }
 
-Cellular::Code Cellular::echo(bool state)
+Code Cellular::echo(bool state)
 {
     Code code;
     if (state) {
         code = sendBasicCommand("ATE0", 1000);
-        echoMode = (code == CELL_OK) ? false : echoMode;
+        echoMode = (code == SUCCESS) ? false : echoMode;
     } else {
         code = sendBasicCommand("ATE1", 1000);
-        echoMode = (code == CELL_OK) ? true : echoMode;
+        echoMode = (code == SUCCESS) ? true : echoMode;
     }
     return code;
 }
@@ -526,10 +526,6 @@
     return value;
 }
 
-std::string Cellular::getPhoneNumber() {
-    return "unknown";
-}
-
 Cellular::Registration Cellular::getRegistration()
 {
     string response = sendCommand("AT+CREG?", 1000);
@@ -558,28 +554,28 @@
     return UNKNOWN;
 }
 
-Cellular::Code Cellular::sendBasicCommand(const std::string& command, unsigned int timeoutMillis, ESC_CHAR esc)
+Code Cellular::sendBasicCommand(const std::string& command, unsigned int timeoutMillis, char esc)
 {
     if(socketOpened) {
         printf("[ERROR] socket is open. Can not send AT commands\r\n");    
-        return CELL_ERROR;
+        return ERROR;
     }
 
     string response = sendCommand(command, timeoutMillis, esc);
     if (response.size() == 0) {
-        return CELL_NO_RESPONSE;
+        return NO_RESPONSE;
     } else if (response.find("OK") != string::npos) {
-        return CELL_OK;
+        return SUCCESS;
     } else if (response.find("ERROR") != string::npos) {
-        return CELL_ERROR;
+        return ERROR;
     } else {
-        return CELL_FAILURE;
+        return FAILURE;
     }
 }
 
-Cellular::Code Cellular::setApn(const std::string& apn) {
+Code Cellular::setApn(const std::string& apn) {
     Code code = sendBasicCommand("AT#APNSERV=\"" + apn + "\"", 1000);
-    if (code != CELL_OK) {
+    if (code != SUCCESS) {
         return code;
     }
     this->apn = apn;
@@ -587,7 +583,7 @@
 }
 
 
-Cellular::Code Cellular::setDns(const std::string& primary, const std::string& secondary) {
+Code Cellular::setDns(const std::string& primary, const std::string& secondary) {
     return sendBasicCommand("AT#DNS=1," + primary + "," + secondary, 1000);
 }
 
@@ -596,19 +592,19 @@
     Code code;
     
     code = sendBasicCommand("AT#PINGREMOTE=\"" + address + "\"", 1000);
-    if (code != CELL_OK) {
+    if (code != SUCCESS) {
         return false;
     }
     
     sprintf(buffer, "AT#PINGNUM=%d", 1);
     code = sendBasicCommand(buffer , 1000);
-    if (code != CELL_OK) {
+    if (code != SUCCESS) {
         return false;
     }
     
     sprintf(buffer, "AT#PINGDELAY=%d", PINGDELAY);
     code = sendBasicCommand(buffer , 1000);
-    if (code != CELL_OK) {
+    if (code != SUCCESS) {
         return false;
     }
     
@@ -622,29 +618,29 @@
     return false;
 }
 
-Cellular::Code Cellular::setSocketCloseable(bool enabled) {
+Code Cellular::setSocketCloseable(bool enabled) {
     if(socketCloseable == enabled) {
-        return CELL_OK;    
+        return SUCCESS;    
     }
     
     if(socketOpened) {
         printf("[ERROR] socket is already opened. Can not set closeable\r\n");    
-        return CELL_ERROR;
+        return ERROR;
     }
     
     socketCloseable = enabled;
     
-    return CELL_OK;
+    return SUCCESS;
 }
 
-Cellular::Code Cellular::sendSMS(const Sms& sms) {
+Code Cellular::sendSMS(const Sms& sms) {
     return sendSMS(sms.phoneNumber, sms.message);
 }
 
-Cellular::Code Cellular::sendSMS(const std::string& phoneNumber, const std::string& message)
+Code Cellular::sendSMS(const std::string& phoneNumber, const std::string& message)
 {    
     Code code = sendBasicCommand("AT+CMGF=1", 1000);
-    if (code != CELL_OK) {
+    if (code != SUCCESS) {
         return code;
     }
     string cmd = "AT+CMGS=\"+";
@@ -652,15 +648,15 @@
     cmd.append("\"");
     string response1 = sendCommand(cmd, 1000);
     if (response1.find('>') == string::npos) {
-        return CELL_NO_RESPONSE;
+        return NO_RESPONSE;
     }
     wait(.2);
     string  response2 = sendCommand(message, 4000, CTRL_Z);
     printf("SMS Response: %s\r\n", response2.c_str());
     if (response2.find("+CMGS:") == string::npos) {
-        return CELL_FAILURE;
+        return FAILURE;
     }
-    return CELL_OK;
+    return SUCCESS;
 }
 
 std::vector<Cellular::Sms> Cellular::getReceivedSms() {
@@ -715,16 +711,16 @@
     return vSms;
 }
 
-Cellular::Code Cellular::deleteOnlyReceivedReadSms() {
+Code Cellular::deleteOnlyReceivedReadSms() {
     return sendBasicCommand("AT+CMGD=1,1", 1000);
 }
 
-Cellular::Code Cellular::deleteAllReceivedSms() {
+Code Cellular::deleteAllReceivedSms() {
     return sendBasicCommand("AT+CMGD=1,4", 1000);
 }
 
 
-string Cellular::sendCommand(const std::string& command, unsigned int timeoutMillis, ESC_CHAR esc)
+string Cellular::sendCommand(const std::string& command, unsigned int timeoutMillis, char esc)
 {
     if(io == NULL) {
         printf("[ERROR] MTSBufferedIO not set\r\n");
@@ -747,14 +743,9 @@
     }
     
     //Send Escape Character
-    if (esc == CR) {
-        if(io->write('\r', timeoutMillis) != 1) {
-            printf("[ERROR] failed to send '\\r' to radio within %d milliseconds\r\n", timeoutMillis);
-            return "";
-        }
-    } else if (esc == CTRL_Z) {
-        if(io->write(0x1A, timeoutMillis) != 1) {
-            printf("[ERROR] failed to send 'CTRL+Z' to radio within %d milliseconds\r\n", timeoutMillis);
+    if (esc != 0x00) {
+        if(io->write(esc, timeoutMillis) != 1) {
+            printf("[ERROR] failed to send '%c' to radio within %d milliseconds\r\n", esc, timeoutMillis);
             return "";
         }
     }
@@ -794,14 +785,14 @@
 std::string Cellular::getCodeNames(Code code)
 {
     switch(code) {
-        case CELL_OK:
-            return "CELL_OK";
-        case CELL_ERROR:
-            return "CELL_ERROR";
-        case CELL_NO_RESPONSE:
-            return "CELL_NO_RESPONSE";
-        case CELL_FAILURE:
-            return "CELL_FAILURE";
+        case SUCCESS:
+            return "SUCCESS";
+        case ERROR:
+            return "ERROR";
+        case NO_RESPONSE:
+            return "NO_RESPONSE";
+        case FAILURE:
+            return "FAILURE";
         default:
             return "UNKNOWN ENUM";
     }