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

Dependents:   mtsas mtsas mtsas mtsas

Revision:
1:f155d94d6f3a
Child:
3:04046eebaef5
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Cellular/Cellular.cpp	Mon May 19 12:34:32 2014 -0500
@@ -0,0 +1,170 @@
+#include "Cellular.h"
+#include "MTSText.h"
+
+using namespace mts;
+
+std::string Cellular::getRegistrationNames(Registration registration)
+{
+    switch(registration) {
+        case NOT_REGISTERED:
+            return "NOT_REGISTERED";
+        case REGISTERED:
+            return "REGISTERED";
+        case SEARCHING:
+            return "SEARCHING";
+        case DENIED:
+            return "DENIED";
+        case UNKNOWN:
+            return "UNKNOWN";
+        case ROAMING:
+            return "ROAMING";
+        default:
+            return "UNKNOWN ENUM";
+    }
+}
+
+Code Cellular::test()
+{
+    int i = 0;
+    while (sendBasicCommand("AT", 1000) != SUCCESS) {
+        i++;
+        if (i >= 30) {
+            printf("[ERROR] Could not talk to radio after 30 tries\r\n");
+            i = 0;
+        }
+        wait(1);
+    }
+    return SUCCESS;
+}
+
+int Cellular::getSignalStrength()
+{
+    string response = sendCommand("AT+CSQ", 1000);
+    if (response.find("OK") == string::npos) {
+        return -1;
+    }
+    int start = response.find(':');
+    int stop = response.find(',', start);
+    string signal = response.substr(start + 2, stop - start - 2);
+    int value;
+    sscanf(signal.c_str(), "%d", &value);
+    return value;
+}
+
+Cellular::Registration Cellular::getRegistration()
+{
+    string response = sendCommand("AT+CREG?", 5000);
+    if (response.find("OK") == string::npos) {
+        return UNKNOWN;
+    }
+    int start = response.find(',');
+    int stop = response.find(' ', start);
+    string regStat = response.substr(start + 1, stop - start - 1);
+    int value;
+    sscanf(regStat.c_str(), "%d", &value);
+    switch (value) {
+        case 0:
+            return NOT_REGISTERED;
+        case 1:
+            return REGISTERED;
+        case 2:
+            return SEARCHING;
+        case 3:
+            return DENIED;
+        case 4:
+            return UNKNOWN;
+        case 5:
+            return ROAMING;
+    }
+    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);
+    printf("SMS Response: %s\r\n", 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));
+        //printf("[DEBUG] Top of SMS Parse Loop. LINE[%s]\r\n", line.c_str());
+        if(line.find("+CMGL: ") == std::string::npos) {
+            continue;
+        }
+
+        //Start of SMS message
+        std::vector<std::string> vSmsParts = Text::split(line, ',');
+        if(vSmsParts.size() != 6) {
+            printf("[WARNING] Expected 6 commas. SMS[%d] DATA[%s]. Continuing ...\r\n", smsNumber, line.c_str());
+            continue;
+        }
+
+        sms.phoneNumber = vSmsParts[2];
+        sms.timestamp = vSmsParts[4] + ", " + vSmsParts[5];
+
+        if(pos == std::string::npos) {
+            printf("[WARNING] Expected SMS body. SMS[%d]. Leaving ...\r\n", 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) {
+            //printf("[DEBUG] Parsing Last SMS. SMS[%d]\r\n", smsNumber);
+            //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);
+            printf("[WARNING] Expected to find end of SMS list. SMS[%d] DATA[%s].\r\n", smsNumber, sms.message.c_str());
+        }
+        vSms.push_back(sms);
+        pos = bodyEnd;
+        //printf("[DEBUG] Parsed SMS[%d].  Starting Next at position [%d]\r\n", smsNumber, pos);
+        smsNumber++;
+    }
+    printf("Received %d SMS\r\n", smsNumber);
+    return vSms;
+}
+
+Code Cellular::deleteOnlyReceivedReadSms()
+{
+    return sendBasicCommand("AT+CMGD=1,1", 1000);
+}
+
+Code Cellular::deleteAllReceivedSms()
+{
+    return sendBasicCommand("AT+CMGD=1,4", 1000);
+}