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
Diff: cellular/Cellular.cpp
- Revision:
- 4:6561c9128c6f
- Parent:
- 0:563b70517320
- Child:
- 5:93e889a5abc6
--- a/cellular/Cellular.cpp Mon Dec 09 16:10:26 2013 +0000 +++ b/cellular/Cellular.cpp Wed Dec 11 16:49:21 2013 +0000 @@ -2,8 +2,9 @@ #define CELLULAR_CPP #include "Cellular.h" +#include "MTSText.h" -Cellular::Cellular(MTSBufferedIO* io) : io(io) +Cellular::Cellular(MTSBufferedIO& io) : io(io) { } @@ -64,6 +65,7 @@ case 5: return ROAMING; } + return UNKNOWN; } int Cellular::connect(string host, int port) @@ -111,7 +113,11 @@ } } -Cellular::Code Cellular::sendSMS(string phoneNumber, string message) +Cellular::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 code = sendBasicCommand("AT+CMGF=1", 1000); if (code != OK) { @@ -126,13 +132,55 @@ } wait(.2); string response2 = sendCommand(message, 4000, CTRL_Z); - printf("SMS Response: %s\n", response2); + printf("SMS Response: %s\n", response2.c_str()); if (response2.find("+CMGS:") == string::npos) { return FAILURE; } return OK; } +std::vector<Cellular::Sms> Cellular::getReceivedSms() { + int lineNumber = 0; + size_t pos = 0; + bool done = false; + std::vector<Sms> vSms; + std::string received = sendCommand("AT+CMGL=\"ALL\"", 4000); + + while (pos != std::string::npos && !done) { + Cellular::Sms sms; + std::string line(Text::getLine(received, pos, pos)); + if(line.find("+CMGL: ") == std::string::npos) { + //Searching for SMS start + printf("[WARNING] Expected +CMGL. LINE[%d] DATA[%s]. Continuing ...", lineNumber, line.c_str()); + continue; + } + + //Start of SMS message + std::vector<std::string> vSmsParts = Text::split(line, ','); + if(vSmsParts.size() != 6) { + printf("[WARNING] Expected 6 commas. LINE[%d] DATA[%s]. Continuing ...", lineNumber, line.c_str()); + continue; + } + + sms.phoneNumber = vSmsParts[2]; + sms.timestamp = vSmsParts[4] + ", " + vSmsParts[5]; + + size_t bodyEnd = received.find("+CMGL: ", pos); + sms.message = received.substr(pos, bodyEnd); + pos = bodyEnd; + } + return vSms; +} + +Cellular::Code Cellular::deleteOnlyReceivedReadSms() { + return sendBasicCommand("AT+CMGD=1,1", 1000); +} + +Cellular::Code Cellular::deleteAllReceivedSms() { + return sendBasicCommand("AT+CMGD=1,4", 1000); +} + + string Cellular::sendCommand(string command, int timeoutMillis, ESC_CHAR esc) { int size = command.size() + 1; @@ -144,20 +192,20 @@ cmd[size -1] = 0x1A; } - io->rxClear(); - io->txClear(); - int status = io->write(cmd, size); - int available = io->rxAvailable(); + io.rxClear(); + io.txClear(); + int status = io.write(cmd, size); + int available = io.rxAvailable(); int previous = -1; int timer = 0; while (available != previous && timer < timeoutMillis) { wait(.1); timer = timer + 100; previous = available; - available = io->rxAvailable(); + available = io.rxAvailable(); } char tmp[available]; - io->read(tmp, available); + io.read(tmp, available); return string(tmp); }