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

Committer:
sgodinez
Date:
Fri Dec 13 20:23:40 2013 +0000
Revision:
12:40ac31a09132
Parent:
11:134435d8a2d5
Child:
13:0af863114629
merged

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jengbrecht 0:563b70517320 1 #ifndef CELLULAR_CPP
jengbrecht 0:563b70517320 2 #define CELLULAR_CPP
jengbrecht 0:563b70517320 3
jengbrecht 0:563b70517320 4 #include "Cellular.h"
sgodinez 4:6561c9128c6f 5 #include "MTSText.h"
sgodinez 11:134435d8a2d5 6 #include <sstream>
jengbrecht 0:563b70517320 7
sgodinez 11:134435d8a2d5 8 Cellular::Cellular(MTSBufferedIO& io)
sgodinez 11:134435d8a2d5 9 : io(io)
sgodinez 11:134435d8a2d5 10 , code(OK)
sgodinez 11:134435d8a2d5 11 , pppConnected(false)
sgodinez 11:134435d8a2d5 12 , mode(TCP)
sgodinez 11:134435d8a2d5 13 , socketOpened(false)
sgodinez 11:134435d8a2d5 14 , local_port(0)
sgodinez 11:134435d8a2d5 15 , host_port(0)
jengbrecht 0:563b70517320 16 {
jengbrecht 0:563b70517320 17 }
jengbrecht 0:563b70517320 18
jengbrecht 0:563b70517320 19 Cellular::~Cellular()
jengbrecht 0:563b70517320 20 {
jengbrecht 0:563b70517320 21 }
jengbrecht 0:563b70517320 22
sgodinez 11:134435d8a2d5 23 bool Cellular::connect() {
sgodinez 11:134435d8a2d5 24 //Run Test first to validate a good state
sgodinez 11:134435d8a2d5 25 //Check RSSI: AT+CSQ
sgodinez 11:134435d8a2d5 26 //Check Registration: AT+CREG? == 0,1
sgodinez 11:134435d8a2d5 27
sgodinez 11:134435d8a2d5 28
sgodinez 11:134435d8a2d5 29 //AT#CONNECTIONSTART: Make a PPP connection
sgodinez 11:134435d8a2d5 30 std::string pppResult = sendCommand("AT#CONNECTIONSTART", 30000);
sgodinez 11:134435d8a2d5 31
sgodinez 11:134435d8a2d5 32 size_t pos = 0;
sgodinez 11:134435d8a2d5 33 std::string ip = Text::getLine(pppResult, pos, pos);
sgodinez 11:134435d8a2d5 34 std::string status = Text::getLine(pppResult, pos, pos);
sgodinez 11:134435d8a2d5 35
sgodinez 11:134435d8a2d5 36 return false;
sgodinez 11:134435d8a2d5 37 }
sgodinez 11:134435d8a2d5 38
sgodinez 11:134435d8a2d5 39 void Cellular::disconnect() {
sgodinez 11:134435d8a2d5 40
sgodinez 11:134435d8a2d5 41 }
sgodinez 11:134435d8a2d5 42
sgodinez 11:134435d8a2d5 43 bool Cellular::isConnected() {
sgodinez 11:134435d8a2d5 44 //1) Check if APN was set
sgodinez 11:134435d8a2d5 45 if(apn.size() == 0) {
sgodinez 11:134435d8a2d5 46 printf("[DEBUG] APN is not set\n");
sgodinez 11:134435d8a2d5 47 return false;
sgodinez 11:134435d8a2d5 48 }
sgodinez 11:134435d8a2d5 49
sgodinez 11:134435d8a2d5 50 //1) Check that we do not have a live connection up
sgodinez 11:134435d8a2d5 51 if(socketOpened) {
sgodinez 11:134435d8a2d5 52 printf("[DEBUG] Socket is opened\n");
sgodinez 11:134435d8a2d5 53 return true;
sgodinez 11:134435d8a2d5 54 }
sgodinez 11:134435d8a2d5 55 //2) Query the radio
sgodinez 11:134435d8a2d5 56 pppConnected = false;
sgodinez 11:134435d8a2d5 57 std::string result = sendCommand("AT#VSTATE", 1000);
sgodinez 11:134435d8a2d5 58 if(result.find("CONNECTED") != std::string::npos) {
sgodinez 11:134435d8a2d5 59 pppConnected = true;
sgodinez 11:134435d8a2d5 60 }
sgodinez 11:134435d8a2d5 61
sgodinez 11:134435d8a2d5 62 return pppConnected;
sgodinez 11:134435d8a2d5 63 }
sgodinez 11:134435d8a2d5 64
sgodinez 11:134435d8a2d5 65 bool Cellular::bind(unsigned int port) {
sgodinez 11:134435d8a2d5 66 return false;
sgodinez 11:134435d8a2d5 67 }
sgodinez 11:134435d8a2d5 68
sgodinez 11:134435d8a2d5 69 bool Cellular::open(const std::string& address, unsigned int port, Mode mode) {
sgodinez 11:134435d8a2d5 70 Code portCode, addressCode;
sgodinez 11:134435d8a2d5 71
sgodinez 11:134435d8a2d5 72 //1) Check that we do not have a live connection up
sgodinez 11:134435d8a2d5 73 if(socketOpened) {
sgodinez 11:134435d8a2d5 74 printf("[DEBUG] Socket already opened\n");
sgodinez 11:134435d8a2d5 75 return true;
sgodinez 11:134435d8a2d5 76 }
sgodinez 11:134435d8a2d5 77
sgodinez 11:134435d8a2d5 78 //2) Check PPP connection
sgodinez 11:134435d8a2d5 79 if(!isConnected()) {
sgodinez 11:134435d8a2d5 80 printf("[ERROR] PPP not established. Attempting to connect\n");
sgodinez 11:134435d8a2d5 81 if(!connect()) {
sgodinez 11:134435d8a2d5 82 printf("[ERROR] PPP connection failed\n");
sgodinez 11:134435d8a2d5 83 return false;
sgodinez 11:134435d8a2d5 84 } else {
sgodinez 11:134435d8a2d5 85 printf("[DEBUG] PPP connection established\n");
sgodinez 11:134435d8a2d5 86 }
sgodinez 11:134435d8a2d5 87 }
sgodinez 11:134435d8a2d5 88
sgodinez 11:134435d8a2d5 89 //Setup IP Connection
sgodinez 11:134435d8a2d5 90 std::stringstream ss;
sgodinez 11:134435d8a2d5 91 if(mode == TCP) {
sgodinez 11:134435d8a2d5 92 ss << "AT#TCPPORT=1," << port;
sgodinez 11:134435d8a2d5 93 portCode = sendBasicCommand(ss.str(), 1000);
sgodinez 11:134435d8a2d5 94 addressCode = sendBasicCommand("AT#TCPSERV=1," + address, 1000);
sgodinez 11:134435d8a2d5 95 } else {
sgodinez 11:134435d8a2d5 96 ss << "AT#UDPPORT=1," << port;
sgodinez 11:134435d8a2d5 97 portCode = sendBasicCommand(ss.str(), 1000);
sgodinez 11:134435d8a2d5 98 addressCode = sendBasicCommand("AT#UDPSERV=1," + address, 1000);
sgodinez 11:134435d8a2d5 99 }
sgodinez 11:134435d8a2d5 100
sgodinez 11:134435d8a2d5 101 if(portCode == OK) {
sgodinez 11:134435d8a2d5 102 host_port = port;
sgodinez 11:134435d8a2d5 103 } else {
sgodinez 11:134435d8a2d5 104 printf("[ERROR] Host port could not be set\n");
sgodinez 11:134435d8a2d5 105 }
sgodinez 11:134435d8a2d5 106
sgodinez 11:134435d8a2d5 107 if(addressCode == OK) {
sgodinez 11:134435d8a2d5 108 host_address = address;
sgodinez 11:134435d8a2d5 109 } else {
sgodinez 11:134435d8a2d5 110 printf("[ERROR] Host address could not be set\n");
sgodinez 11:134435d8a2d5 111 }
sgodinez 11:134435d8a2d5 112
sgodinez 11:134435d8a2d5 113
sgodinez 11:134435d8a2d5 114
sgodinez 11:134435d8a2d5 115 return false;
sgodinez 11:134435d8a2d5 116 }
sgodinez 11:134435d8a2d5 117
sgodinez 11:134435d8a2d5 118 bool Cellular::isOpen() {
sgodinez 11:134435d8a2d5 119 return false;
sgodinez 11:134435d8a2d5 120 }
sgodinez 11:134435d8a2d5 121
sgodinez 11:134435d8a2d5 122 void Cellular::close() {
sgodinez 11:134435d8a2d5 123
sgodinez 11:134435d8a2d5 124 }
sgodinez 11:134435d8a2d5 125
sgodinez 11:134435d8a2d5 126 int Cellular::read(char* data, int max, int timeout) {
sgodinez 11:134435d8a2d5 127 return -1;
sgodinez 11:134435d8a2d5 128 }
sgodinez 11:134435d8a2d5 129
sgodinez 11:134435d8a2d5 130 int Cellular::write(char* data, int length, int timeout) {
sgodinez 11:134435d8a2d5 131 return -1;
sgodinez 11:134435d8a2d5 132 }
sgodinez 11:134435d8a2d5 133
sgodinez 11:134435d8a2d5 134 unsigned int Cellular::readable() {
sgodinez 11:134435d8a2d5 135 return 0;
sgodinez 11:134435d8a2d5 136 }
sgodinez 11:134435d8a2d5 137
sgodinez 11:134435d8a2d5 138 unsigned int Cellular::writeable() {
sgodinez 11:134435d8a2d5 139 return 0;
sgodinez 11:134435d8a2d5 140 }
sgodinez 11:134435d8a2d5 141
sgodinez 11:134435d8a2d5 142 void Cellular::reset() {
sgodinez 11:134435d8a2d5 143
sgodinez 11:134435d8a2d5 144 }
sgodinez 11:134435d8a2d5 145
sgodinez 11:134435d8a2d5 146 Cellular::Code Cellular::test()
jengbrecht 0:563b70517320 147 {
sgodinez 11:134435d8a2d5 148 Code code = sendBasicCommand("AT", 1000);
sgodinez 11:134435d8a2d5 149
sgodinez 11:134435d8a2d5 150 if(code != OK) {
sgodinez 11:134435d8a2d5 151 printf("[Error] Failed basic AT command");
sgodinez 11:134435d8a2d5 152 return code;
sgodinez 11:134435d8a2d5 153 }
sgodinez 11:134435d8a2d5 154
sgodinez 11:134435d8a2d5 155 //AT#VSTATE != "CHECKING"
sgodinez 11:134435d8a2d5 156
sgodinez 11:134435d8a2d5 157 //AT#GPRSMODE ==
sgodinez 12:40ac31a09132 158 return OK;
jengbrecht 0:563b70517320 159 }
jengbrecht 0:563b70517320 160
jengbrecht 0:563b70517320 161 Cellular::Code Cellular::echoOff(bool state)
jengbrecht 0:563b70517320 162 {
jengbrecht 0:563b70517320 163 if (state) {
jengbrecht 0:563b70517320 164 return sendBasicCommand("ATE0", 1000);
jengbrecht 0:563b70517320 165 } else {
jengbrecht 0:563b70517320 166 return sendBasicCommand("ATE1", 1000);
jengbrecht 0:563b70517320 167 }
jengbrecht 0:563b70517320 168 }
jengbrecht 0:563b70517320 169
jengbrecht 0:563b70517320 170 int Cellular::getSignalStrength()
jengbrecht 0:563b70517320 171 {
jengbrecht 0:563b70517320 172 string response = sendCommand("AT+CSQ", 1000);
jengbrecht 0:563b70517320 173 if (response.find("OK") == string::npos) {
jengbrecht 0:563b70517320 174 return -1;
jengbrecht 0:563b70517320 175 }
jengbrecht 0:563b70517320 176 int start = response.find(':');
jengbrecht 0:563b70517320 177 int stop = response.find(',', start);
jengbrecht 0:563b70517320 178 string signal = response.substr(start + 2, stop - start - 2);
jengbrecht 0:563b70517320 179 int value;
jengbrecht 0:563b70517320 180 sscanf(signal.c_str(), "%d", &value);
jengbrecht 0:563b70517320 181 return value;
jengbrecht 0:563b70517320 182 }
jengbrecht 0:563b70517320 183
sgodinez 5:93e889a5abc6 184 std::string Cellular::getPhoneNumber() {
sgodinez 5:93e889a5abc6 185 return "unknown";
sgodinez 5:93e889a5abc6 186 }
sgodinez 5:93e889a5abc6 187
jengbrecht 0:563b70517320 188 Cellular::Registration Cellular::getRegistration()
jengbrecht 0:563b70517320 189 {
jengbrecht 0:563b70517320 190 string response = sendCommand("AT+CREG?", 1000);
jengbrecht 0:563b70517320 191 if (response.find("OK") == string::npos) {
jengbrecht 0:563b70517320 192 return UNKNOWN;
jengbrecht 0:563b70517320 193 }
jengbrecht 0:563b70517320 194 int start = response.find(',');
jengbrecht 0:563b70517320 195 int stop = response.find(' ', start);
jengbrecht 0:563b70517320 196 string regStat = response.substr(start + 1, stop - start - 1);
jengbrecht 0:563b70517320 197 int value;
jengbrecht 0:563b70517320 198 sscanf(regStat.c_str(), "%d", &value);
jengbrecht 0:563b70517320 199 switch (value) {
jengbrecht 0:563b70517320 200 case 0:
jengbrecht 0:563b70517320 201 return NOT_REGISTERED;
jengbrecht 0:563b70517320 202 case 1:
jengbrecht 0:563b70517320 203 return REGISTERED;
jengbrecht 0:563b70517320 204 case 2:
jengbrecht 0:563b70517320 205 return SEARCHING;
jengbrecht 0:563b70517320 206 case 3:
jengbrecht 0:563b70517320 207 return DENIED;
jengbrecht 0:563b70517320 208 case 4:
jengbrecht 0:563b70517320 209 return UNKNOWN;
jengbrecht 0:563b70517320 210 case 5:
jengbrecht 0:563b70517320 211 return ROAMING;
jengbrecht 0:563b70517320 212 }
sgodinez 4:6561c9128c6f 213 return UNKNOWN;
jengbrecht 0:563b70517320 214 }
jengbrecht 0:563b70517320 215
sgodinez 11:134435d8a2d5 216 //int Cellular::connect(string host, int port)
sgodinez 11:134435d8a2d5 217 //{
sgodinez 11:134435d8a2d5 218 // // Set the Server Address
sgodinez 11:134435d8a2d5 219 // string hostCmd = "AT#TCPSERV=1,\"";
sgodinez 11:134435d8a2d5 220 // hostCmd.append(host);
sgodinez 11:134435d8a2d5 221 // hostCmd.append("\"");
sgodinez 11:134435d8a2d5 222 // if (sendBasicCommand(hostCmd, 1000) != OK) {
sgodinez 11:134435d8a2d5 223 // return -1;
sgodinez 11:134435d8a2d5 224 // }
sgodinez 11:134435d8a2d5 225 //
sgodinez 11:134435d8a2d5 226 // // Set the Server Port
sgodinez 11:134435d8a2d5 227 // string portCmd = "AT#TCPPORT=1,\"";
sgodinez 11:134435d8a2d5 228 // char tmp[7];
sgodinez 11:134435d8a2d5 229 // if (sprintf(tmp, "%d", port) < 0) {
sgodinez 11:134435d8a2d5 230 // return -1;
sgodinez 11:134435d8a2d5 231 // }
sgodinez 11:134435d8a2d5 232 // portCmd.append(string(tmp));
sgodinez 11:134435d8a2d5 233 // portCmd.append("\"");
sgodinez 11:134435d8a2d5 234 // if (sendBasicCommand(portCmd, 1000) != OK) {
sgodinez 11:134435d8a2d5 235 // return -1;
sgodinez 11:134435d8a2d5 236 // }
sgodinez 11:134435d8a2d5 237 //
sgodinez 11:134435d8a2d5 238 // // Try and Connect
sgodinez 11:134435d8a2d5 239 // string response = sendCommand("AT#OTCP=1", 2000);
sgodinez 11:134435d8a2d5 240 // if (response.find("Ok_Info_WaitingForData") != string::npos) {
sgodinez 11:134435d8a2d5 241 // return 0;
sgodinez 11:134435d8a2d5 242 // } else {
sgodinez 11:134435d8a2d5 243 // return -1;
sgodinez 11:134435d8a2d5 244 // }
sgodinez 11:134435d8a2d5 245 //}
jengbrecht 0:563b70517320 246
jengbrecht 0:563b70517320 247 Cellular::Code Cellular::sendBasicCommand(string command, int timeoutMillis, ESC_CHAR esc)
jengbrecht 0:563b70517320 248 {
jengbrecht 0:563b70517320 249 string response = sendCommand(command, timeoutMillis, esc);
jengbrecht 0:563b70517320 250 if (response.size() == 0) {
jengbrecht 0:563b70517320 251 return NO_RESPONSE;
jengbrecht 0:563b70517320 252 } else if (response.find("OK") != string::npos) {
jengbrecht 0:563b70517320 253 return OK;
jengbrecht 0:563b70517320 254 } else if (response.find("ERROR") != string::npos) {
jengbrecht 0:563b70517320 255 return ERROR;
jengbrecht 0:563b70517320 256 } else {
jengbrecht 0:563b70517320 257 return FAILURE;
jengbrecht 0:563b70517320 258 }
jengbrecht 0:563b70517320 259 }
jengbrecht 0:563b70517320 260
sgodinez 11:134435d8a2d5 261 Cellular::Code Cellular::setApn(const std::string& apn) {
sgodinez 11:134435d8a2d5 262 Code code = sendBasicCommand("AT#APNSERV=\"" + apn + "\"", 1000);
sgodinez 11:134435d8a2d5 263 if (code != OK) {
sgodinez 11:134435d8a2d5 264 return code;
sgodinez 11:134435d8a2d5 265 }
sgodinez 11:134435d8a2d5 266 this->apn = apn;
sgodinez 11:134435d8a2d5 267 return code;
sgodinez 11:134435d8a2d5 268 }
sgodinez 11:134435d8a2d5 269
sgodinez 11:134435d8a2d5 270 Cellular::Code Cellular::setDns(const std::string& apn) {
sgodinez 12:40ac31a09132 271 return FAILURE;
sgodinez 11:134435d8a2d5 272 }
sgodinez 11:134435d8a2d5 273
sgodinez 4:6561c9128c6f 274 Cellular::Code Cellular::sendSMS(const Sms& sms) {
sgodinez 4:6561c9128c6f 275 return sendSMS(sms.phoneNumber, sms.message);
sgodinez 4:6561c9128c6f 276 }
sgodinez 4:6561c9128c6f 277
sgodinez 4:6561c9128c6f 278 Cellular::Code Cellular::sendSMS(const std::string& phoneNumber, const std::string& message)
jengbrecht 0:563b70517320 279 {
jengbrecht 0:563b70517320 280 Code code = sendBasicCommand("AT+CMGF=1", 1000);
jengbrecht 0:563b70517320 281 if (code != OK) {
jengbrecht 0:563b70517320 282 return code;
jengbrecht 0:563b70517320 283 }
jengbrecht 0:563b70517320 284 string cmd = "AT+CMGS=\"+";
jengbrecht 0:563b70517320 285 cmd.append(phoneNumber);
jengbrecht 0:563b70517320 286 cmd.append("\"");
jengbrecht 0:563b70517320 287 string response1 = sendCommand(cmd, 1000);
jengbrecht 0:563b70517320 288 if (response1.find('>') == string::npos) {
jengbrecht 0:563b70517320 289 return NO_RESPONSE;
jengbrecht 0:563b70517320 290 }
jengbrecht 0:563b70517320 291 wait(.2);
jengbrecht 0:563b70517320 292 string response2 = sendCommand(message, 4000, CTRL_Z);
sgodinez 4:6561c9128c6f 293 printf("SMS Response: %s\n", response2.c_str());
jengbrecht 0:563b70517320 294 if (response2.find("+CMGS:") == string::npos) {
jengbrecht 0:563b70517320 295 return FAILURE;
jengbrecht 0:563b70517320 296 }
jengbrecht 0:563b70517320 297 return OK;
jengbrecht 0:563b70517320 298 }
jengbrecht 0:563b70517320 299
sgodinez 4:6561c9128c6f 300 std::vector<Cellular::Sms> Cellular::getReceivedSms() {
sgodinez 9:5b12c5a8dde4 301 int smsNumber = 0;
sgodinez 4:6561c9128c6f 302 std::vector<Sms> vSms;
sgodinez 4:6561c9128c6f 303 std::string received = sendCommand("AT+CMGL=\"ALL\"", 4000);
sgodinez 5:93e889a5abc6 304 size_t pos = received.find("+CMGL: ");
sgodinez 4:6561c9128c6f 305
sgodinez 5:93e889a5abc6 306 while (pos != std::string::npos) {
sgodinez 4:6561c9128c6f 307 Cellular::Sms sms;
sgodinez 4:6561c9128c6f 308 std::string line(Text::getLine(received, pos, pos));
sgodinez 9:5b12c5a8dde4 309 //printf("[DEBUG] Top of SMS Parse Loop. LINE[%s]\n", line.c_str());
sgodinez 9:5b12c5a8dde4 310 if(line.find("+CMGL: ") == std::string::npos) {
sgodinez 4:6561c9128c6f 311 continue;
sgodinez 4:6561c9128c6f 312 }
sgodinez 4:6561c9128c6f 313
sgodinez 4:6561c9128c6f 314 //Start of SMS message
sgodinez 4:6561c9128c6f 315 std::vector<std::string> vSmsParts = Text::split(line, ',');
sgodinez 4:6561c9128c6f 316 if(vSmsParts.size() != 6) {
sgodinez 9:5b12c5a8dde4 317 printf("[WARNING] Expected 6 commas. SMS[%d] DATA[%s]. Continuing ...\n", smsNumber, line.c_str());
sgodinez 4:6561c9128c6f 318 continue;
sgodinez 4:6561c9128c6f 319 }
sgodinez 4:6561c9128c6f 320
sgodinez 4:6561c9128c6f 321 sms.phoneNumber = vSmsParts[2];
sgodinez 4:6561c9128c6f 322 sms.timestamp = vSmsParts[4] + ", " + vSmsParts[5];
sgodinez 4:6561c9128c6f 323
sgodinez 8:3fe68d6130a8 324 if(pos == std::string::npos) {
sgodinez 9:5b12c5a8dde4 325 printf("[WARNING] Expected SMS body. SMS[%d]. Leaving ...\n", smsNumber);
sgodinez 8:3fe68d6130a8 326 break;
sgodinez 8:3fe68d6130a8 327 }
sgodinez 9:5b12c5a8dde4 328 //Check for the start of the next SMS message
sgodinez 9:5b12c5a8dde4 329 size_t bodyEnd = received.find("\r\n+CMGL: ", pos);
sgodinez 8:3fe68d6130a8 330 if(bodyEnd == std::string::npos) {
sgodinez 9:5b12c5a8dde4 331 //printf("[DEBUG] Parsing Last SMS. SMS[%d]\n", smsNumber);
sgodinez 9:5b12c5a8dde4 332 //This must be the last SMS message
sgodinez 9:5b12c5a8dde4 333 bodyEnd = received.find("\r\n\r\nOK", pos);
sgodinez 8:3fe68d6130a8 334 }
sgodinez 9:5b12c5a8dde4 335
sgodinez 9:5b12c5a8dde4 336 //Safety check that we found the boundary of this current SMS message
sgodinez 9:5b12c5a8dde4 337 if(bodyEnd != std::string::npos) {
sgodinez 9:5b12c5a8dde4 338 sms.message = received.substr(pos, bodyEnd - pos);
sgodinez 9:5b12c5a8dde4 339 } else {
sgodinez 8:3fe68d6130a8 340 sms.message = received.substr(pos);
sgodinez 9:5b12c5a8dde4 341 printf("[WARNING] Expected to find end of SMS list. SMS[%d] DATA[%s].\n", smsNumber, sms.message.c_str());
sgodinez 8:3fe68d6130a8 342 }
sgodinez 5:93e889a5abc6 343 vSms.push_back(sms);
sgodinez 4:6561c9128c6f 344 pos = bodyEnd;
sgodinez 9:5b12c5a8dde4 345 //printf("[DEBUG] Parsed SMS[%d]. Starting Next at position [%d]\n", smsNumber, pos);
sgodinez 9:5b12c5a8dde4 346 smsNumber++;
sgodinez 4:6561c9128c6f 347 }
sgodinez 9:5b12c5a8dde4 348 printf("Received %d SMS\n", smsNumber);
sgodinez 4:6561c9128c6f 349 return vSms;
sgodinez 4:6561c9128c6f 350 }
sgodinez 4:6561c9128c6f 351
sgodinez 4:6561c9128c6f 352 Cellular::Code Cellular::deleteOnlyReceivedReadSms() {
sgodinez 4:6561c9128c6f 353 return sendBasicCommand("AT+CMGD=1,1", 1000);
sgodinez 4:6561c9128c6f 354 }
sgodinez 4:6561c9128c6f 355
sgodinez 4:6561c9128c6f 356 Cellular::Code Cellular::deleteAllReceivedSms() {
sgodinez 4:6561c9128c6f 357 return sendBasicCommand("AT+CMGD=1,4", 1000);
sgodinez 4:6561c9128c6f 358 }
sgodinez 4:6561c9128c6f 359
sgodinez 4:6561c9128c6f 360
jengbrecht 0:563b70517320 361 string Cellular::sendCommand(string command, int timeoutMillis, ESC_CHAR esc)
jengbrecht 0:563b70517320 362 {
jengbrecht 0:563b70517320 363 int size = command.size() + 1;
jengbrecht 0:563b70517320 364 char cmd[size];
jengbrecht 0:563b70517320 365 strcpy(cmd, command.c_str());
jengbrecht 0:563b70517320 366 if (esc == CR) {
jengbrecht 0:563b70517320 367 cmd[size -1] = '\r';
jengbrecht 0:563b70517320 368 } else if (esc == CTRL_Z) {
jengbrecht 0:563b70517320 369 cmd[size -1] = 0x1A;
jengbrecht 0:563b70517320 370 }
jengbrecht 0:563b70517320 371
sgodinez 4:6561c9128c6f 372 io.rxClear();
sgodinez 4:6561c9128c6f 373 io.txClear();
sgodinez 8:3fe68d6130a8 374 std::string result;
sgodinez 4:6561c9128c6f 375 int status = io.write(cmd, size);
sgodinez 4:6561c9128c6f 376 int available = io.rxAvailable();
jengbrecht 0:563b70517320 377 int previous = -1;
jengbrecht 0:563b70517320 378 int timer = 0;
sgodinez 8:3fe68d6130a8 379 char tmp[256];
sgodinez 8:3fe68d6130a8 380 tmp[255] = 0;
sgodinez 8:3fe68d6130a8 381
sgodinez 8:3fe68d6130a8 382 do {
jengbrecht 0:563b70517320 383 wait(.1);
jengbrecht 0:563b70517320 384 timer = timer + 100;
jengbrecht 0:563b70517320 385 previous = available;
sgodinez 4:6561c9128c6f 386 available = io.rxAvailable();
sgodinez 8:3fe68d6130a8 387
sgodinez 8:3fe68d6130a8 388 int size = io.read(tmp,255); //1 less than allocated
sgodinez 8:3fe68d6130a8 389 if(size > 0) {
sgodinez 8:3fe68d6130a8 390 result.append(tmp, size);
sgodinez 8:3fe68d6130a8 391 }
sgodinez 8:3fe68d6130a8 392 } while (available != previous && timer < timeoutMillis);
sgodinez 8:3fe68d6130a8 393
sgodinez 8:3fe68d6130a8 394 return result;
jengbrecht 0:563b70517320 395 }
jengbrecht 0:563b70517320 396
jengbrecht 0:563b70517320 397 #endif /* CELLULAR_CPP */