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

Dependents:   mtsas mtsas mtsas mtsas

Revision:
16:1bc3e44d4746
Parent:
11:4e428f689069
Child:
18:fa0d8120f81f
--- a/Cellular/Cellular.cpp	Wed Jun 04 12:20:37 2014 +0000
+++ b/Cellular/Cellular.cpp	Thu Jun 05 14:52:59 2014 +0000
@@ -134,12 +134,17 @@
 
 Code Cellular::setApn(const std::string& apn)
 {
-    Code code = sendBasicCommand("AT#APNSERV=\"" + apn + "\"", 1000);
-    if (code != SUCCESS) {
+    if (type == MTSMC_H5_IP || type == MTSMC_H5 || type == MTSMC_G3) {
+        Code code = sendBasicCommand("AT#APNSERV=\"" + apn + "\"", 1000);
+        if (code != SUCCESS) {
+            return code;
+        }
+        this->apn = apn;
         return code;
+    } else {
+        logInfo("CDMA radios don't need an APN");
+        return SUCCESS;
     }
-    this->apn = apn;
-    return code;
 }
 
 
@@ -243,21 +248,41 @@
 
 Code Cellular::sendSMS(const std::string& phoneNumber, const std::string& message)
 {
+    string csmp;
+    
+    if (type == MTSMC_H5_IP || type == MTSMC_H5) {
+        csmp = "AT+CSMP=17,167,0,0";
+    } else if (type == MTSMC_EV3_IP || type == MTSMC_EV3 || type == MTSMC_C2_IP || type == MTSMC_C2) {
+        csmp = "AT+CSMP=,4098,0,2";
+    } else if (type == MTSMC_G3) {
+    } else {
+        logError("unknown radio type [%d]", type);
+        return FAILURE;
+    }
+    
     Code code = sendBasicCommand("AT+CMGF=1", 1000);
     if (code != SUCCESS) {
+        logError("CMGF failed");
+        return code;
+    }
+    code = sendBasicCommand(csmp, 1000);
+    if (code != SUCCESS) {
+        logError("CSMP failed [%s]", getRadioNames(type).c_str());
         return code;
     }
     string cmd = "AT+CMGS=\"+";
     cmd.append(phoneNumber);
-    cmd.append("\"");
+    cmd.append("\",145");
     string response1 = sendCommand(cmd, 1000);
     if (response1.find('>') == string::npos) {
+        logError("CMGS phone number failed");
         return NO_RESPONSE;
     }
     wait(.2);
     string  response2 = sendCommand(message, 4000, CTRL_Z);
-    logInfo("SMS Response: %s", response2.c_str());
+    logInfo("SMS Response: [%s]", response2.c_str());
     if (response2.find("+CMGS:") == string::npos) {
+        logError("CMGS message failed");
         return FAILURE;
     }
     return SUCCESS;
@@ -267,8 +292,17 @@
 {
     int smsNumber = 0;
     std::vector<Sms> vSms;
-    std::string received = sendCommand("AT+CMGL=\"ALL\"", 4000);
-    size_t pos = received.find("+CMGL: ");
+    std::string received;
+    size_t pos;
+    
+    Code code = sendBasicCommand("AT+CMGF=1", 1000);
+    if (code != SUCCESS) {
+        logError("CMGF failed");
+        return vSms;
+    }
+    
+    received = sendCommand("AT+CMGL=\"ALL\"", 4000);
+    pos = received.find("+CMGL: ");
 
     while (pos != std::string::npos) {
         Cellular::Sms sms;
@@ -279,14 +313,38 @@
 
         //Start of SMS message
         std::vector<std::string> vSmsParts = Text::split(line, ',');
-        if(vSmsParts.size() != 6) {
-            logWarning("Expected 6 commas. SMS[%d] DATA[%s]. Continuing ...", smsNumber, line.c_str());
-            continue;
+        if (type == MTSMC_H5_IP || type == MTSMC_H5) {
+            /* format for H5 and H5-IP radios
+             * <index>, <status>, <oa>, <alpha>, <scts>
+             * scts contains a comma, so splitting on commas should give us 6 items
+             */
+            if(vSmsParts.size() != 6) {
+                logWarning("Expected 5 commas. SMS[%d] DATA[%s]. Continuing ...", smsNumber, line.c_str());
+                continue;
+            }
+
+            sms.phoneNumber = vSmsParts[2];
+            sms.timestamp = vSmsParts[4] + ", " + vSmsParts[5];
+        } else if (type == MTSMC_EV3_IP || type == MTSMC_EV3 || type == MTSMC_C2_IP || type == MTSMC_C2) {
+            /* format for EV3 and EV3-IP radios
+             * <index>, <status>, <oa>, <callback>, <date>
+             * splitting on commas should give us 5 items
+             */
+            if(vSmsParts.size() != 5) {
+                logWarning("Expected 4 commas. SMS[%d] DATA[%s]. Continuing ...", smsNumber, line.c_str());
+                continue;
+            }
+            
+            sms.phoneNumber = vSmsParts[2];
+            /* timestamp is in a nasty format
+             * YYYYMMDDHHMMSS
+             * nobody wants to try and decipher that, so format it nicely
+             * YY/MM/DD,HH:MM:SS
+             */
+             string s = vSmsParts[4];
+             sms.timestamp = s.substr(2,2) + "/" + s.substr(4,2) + "/" + s.substr(6,2) + ", " + s.substr(8,2) + ":" + s.substr(10,2) + ":" + s.substr(12,2);
         }
 
-        sms.phoneNumber = vSmsParts[2];
-        sms.timestamp = vSmsParts[4] + ", " + vSmsParts[5];
-
         if(pos == std::string::npos) {
             logWarning("Expected SMS body. SMS[%d]. Leaving ...", smsNumber);
             break;