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

Dependents:   mtsas mtsas mtsas mtsas

Revision:
7:0ee8e69a3e9c
Parent:
4:1f63354b8d1b
Child:
8:2d7259d244d1
--- a/Cellular/Cellular.cpp	Wed May 21 08:34:43 2014 -0500
+++ b/Cellular/Cellular.cpp	Wed May 21 15:21:25 2014 -0500
@@ -80,3 +80,90 @@
     }
     return UNKNOWN;
 }
+
+Code Cellular::sendSMS(const Sms& sms)
+{
+    return sendSMS(sms.phoneNumber, sms.message);
+}
+
+Code Cellular::sendSMS(const std::string& phoneNumber, const std::string& message)
+{
+    Code code = sendBasicCommand("AT+CMGF=1", 1000);
+    if (code != SUCCESS) {
+        return code;
+    }
+    string cmd = "AT+CMGS=\"+";
+    cmd.append(phoneNumber);
+    cmd.append("\"");
+    string response1 = sendCommand(cmd, 1000);
+    if (response1.find('>') == string::npos) {
+        return NO_RESPONSE;
+    }
+    wait(.2);
+    string  response2 = sendCommand(message, 4000, CTRL_Z);
+    logInfo("SMS Response: %s", response2.c_str());
+    if (response2.find("+CMGS:") == string::npos) {
+        return FAILURE;
+    }
+    return SUCCESS;
+}
+
+std::vector<Cellular::Sms> Cellular::getReceivedSms()
+{
+    int smsNumber = 0;
+    std::vector<Sms> vSms;
+    std::string received = sendCommand("AT+CMGL=\"ALL\"", 4000);
+    size_t pos = received.find("+CMGL: ");
+
+    while (pos != std::string::npos) {
+        Cellular::Sms sms;
+        std::string line(Text::getLine(received, pos, pos));
+        if(line.find("+CMGL: ") == std::string::npos) {
+            continue;
+        }
+
+        //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;
+        }
+
+        sms.phoneNumber = vSmsParts[2];
+        sms.timestamp = vSmsParts[4] + ", " + vSmsParts[5];
+
+        if(pos == std::string::npos) {
+            logWarning("Expected SMS body. SMS[%d]. Leaving ...", smsNumber);
+            break;
+        }
+        //Check for the start of the next SMS message
+        size_t bodyEnd = received.find("\r\n+CMGL: ", pos);
+        if(bodyEnd == std::string::npos) {
+            //This must be the last SMS message
+            bodyEnd = received.find("\r\n\r\nOK", pos);
+        }
+
+        //Safety check that we found the boundary of this current SMS message
+        if(bodyEnd != std::string::npos) {
+            sms.message = received.substr(pos, bodyEnd - pos);
+        } else {
+            sms.message = received.substr(pos);
+            logWarning("Expected to find end of SMS list. SMS[%d] DATA[%s].", smsNumber, sms.message.c_str());
+        }
+        vSms.push_back(sms);
+        pos = bodyEnd;
+        smsNumber++;
+    }
+    logInfo("Received %d SMS", smsNumber);
+    return vSms;
+}
+
+Code Cellular::deleteOnlyReceivedReadSms()
+{
+    return sendBasicCommand("AT+CMGD=1,1", 1000);
+}
+
+Code Cellular::deleteAllReceivedSms()
+{
+    return sendBasicCommand("AT+CMGD=1,4", 1000);
+}