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:18:47 2013 +0000
Revision:
11:134435d8a2d5
Parent:
9:5b12c5a8dde4
Child:
12:40ac31a09132
IPStack.h interface definition

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 ==
jengbrecht 0:563b70517320 158 }
jengbrecht 0:563b70517320 159
jengbrecht 0:563b70517320 160 Cellular::Code Cellular::echoOff(bool state)
jengbrecht 0:563b70517320 161 {
jengbrecht 0:563b70517320 162 if (state) {
jengbrecht 0:563b70517320 163 return sendBasicCommand("ATE0", 1000);
jengbrecht 0:563b70517320 164 } else {
jengbrecht 0:563b70517320 165 return sendBasicCommand("ATE1", 1000);
jengbrecht 0:563b70517320 166 }
jengbrecht 0:563b70517320 167 }
jengbrecht 0:563b70517320 168
jengbrecht 0:563b70517320 169 int Cellular::getSignalStrength()
jengbrecht 0:563b70517320 170 {
jengbrecht 0:563b70517320 171 string response = sendCommand("AT+CSQ", 1000);
jengbrecht 0:563b70517320 172 if (response.find("OK") == string::npos) {
jengbrecht 0:563b70517320 173 return -1;
jengbrecht 0:563b70517320 174 }
jengbrecht 0:563b70517320 175 int start = response.find(':');
jengbrecht 0:563b70517320 176 int stop = response.find(',', start);
jengbrecht 0:563b70517320 177 string signal = response.substr(start + 2, stop - start - 2);
jengbrecht 0:563b70517320 178 int value;
jengbrecht 0:563b70517320 179 sscanf(signal.c_str(), "%d", &value);
jengbrecht 0:563b70517320 180 return value;
jengbrecht 0:563b70517320 181 }
jengbrecht 0:563b70517320 182
sgodinez 5:93e889a5abc6 183 std::string Cellular::getPhoneNumber() {
sgodinez 5:93e889a5abc6 184 return "unknown";
sgodinez 5:93e889a5abc6 185 }
sgodinez 5:93e889a5abc6 186
jengbrecht 0:563b70517320 187 Cellular::Registration Cellular::getRegistration()
jengbrecht 0:563b70517320 188 {
jengbrecht 0:563b70517320 189 string response = sendCommand("AT+CREG?", 1000);
jengbrecht 0:563b70517320 190 if (response.find("OK") == string::npos) {
jengbrecht 0:563b70517320 191 return UNKNOWN;
jengbrecht 0:563b70517320 192 }
jengbrecht 0:563b70517320 193 int start = response.find(',');
jengbrecht 0:563b70517320 194 int stop = response.find(' ', start);
jengbrecht 0:563b70517320 195 string regStat = response.substr(start + 1, stop - start - 1);
jengbrecht 0:563b70517320 196 int value;
jengbrecht 0:563b70517320 197 sscanf(regStat.c_str(), "%d", &value);
jengbrecht 0:563b70517320 198 switch (value) {
jengbrecht 0:563b70517320 199 case 0:
jengbrecht 0:563b70517320 200 return NOT_REGISTERED;
jengbrecht 0:563b70517320 201 case 1:
jengbrecht 0:563b70517320 202 return REGISTERED;
jengbrecht 0:563b70517320 203 case 2:
jengbrecht 0:563b70517320 204 return SEARCHING;
jengbrecht 0:563b70517320 205 case 3:
jengbrecht 0:563b70517320 206 return DENIED;
jengbrecht 0:563b70517320 207 case 4:
jengbrecht 0:563b70517320 208 return UNKNOWN;
jengbrecht 0:563b70517320 209 case 5:
jengbrecht 0:563b70517320 210 return ROAMING;
jengbrecht 0:563b70517320 211 }
sgodinez 4:6561c9128c6f 212 return UNKNOWN;
jengbrecht 0:563b70517320 213 }
jengbrecht 0:563b70517320 214
sgodinez 11:134435d8a2d5 215 //int Cellular::connect(string host, int port)
sgodinez 11:134435d8a2d5 216 //{
sgodinez 11:134435d8a2d5 217 // // Set the Server Address
sgodinez 11:134435d8a2d5 218 // string hostCmd = "AT#TCPSERV=1,\"";
sgodinez 11:134435d8a2d5 219 // hostCmd.append(host);
sgodinez 11:134435d8a2d5 220 // hostCmd.append("\"");
sgodinez 11:134435d8a2d5 221 // if (sendBasicCommand(hostCmd, 1000) != OK) {
sgodinez 11:134435d8a2d5 222 // return -1;
sgodinez 11:134435d8a2d5 223 // }
sgodinez 11:134435d8a2d5 224 //
sgodinez 11:134435d8a2d5 225 // // Set the Server Port
sgodinez 11:134435d8a2d5 226 // string portCmd = "AT#TCPPORT=1,\"";
sgodinez 11:134435d8a2d5 227 // char tmp[7];
sgodinez 11:134435d8a2d5 228 // if (sprintf(tmp, "%d", port) < 0) {
sgodinez 11:134435d8a2d5 229 // return -1;
sgodinez 11:134435d8a2d5 230 // }
sgodinez 11:134435d8a2d5 231 // portCmd.append(string(tmp));
sgodinez 11:134435d8a2d5 232 // portCmd.append("\"");
sgodinez 11:134435d8a2d5 233 // if (sendBasicCommand(portCmd, 1000) != OK) {
sgodinez 11:134435d8a2d5 234 // return -1;
sgodinez 11:134435d8a2d5 235 // }
sgodinez 11:134435d8a2d5 236 //
sgodinez 11:134435d8a2d5 237 // // Try and Connect
sgodinez 11:134435d8a2d5 238 // string response = sendCommand("AT#OTCP=1", 2000);
sgodinez 11:134435d8a2d5 239 // if (response.find("Ok_Info_WaitingForData") != string::npos) {
sgodinez 11:134435d8a2d5 240 // return 0;
sgodinez 11:134435d8a2d5 241 // } else {
sgodinez 11:134435d8a2d5 242 // return -1;
sgodinez 11:134435d8a2d5 243 // }
sgodinez 11:134435d8a2d5 244 //}
jengbrecht 0:563b70517320 245
jengbrecht 0:563b70517320 246 Cellular::Code Cellular::sendBasicCommand(string command, int timeoutMillis, ESC_CHAR esc)
jengbrecht 0:563b70517320 247 {
jengbrecht 0:563b70517320 248 string response = sendCommand(command, timeoutMillis, esc);
jengbrecht 0:563b70517320 249 if (response.size() == 0) {
jengbrecht 0:563b70517320 250 return NO_RESPONSE;
jengbrecht 0:563b70517320 251 } else if (response.find("OK") != string::npos) {
jengbrecht 0:563b70517320 252 return OK;
jengbrecht 0:563b70517320 253 } else if (response.find("ERROR") != string::npos) {
jengbrecht 0:563b70517320 254 return ERROR;
jengbrecht 0:563b70517320 255 } else {
jengbrecht 0:563b70517320 256 return FAILURE;
jengbrecht 0:563b70517320 257 }
jengbrecht 0:563b70517320 258 }
jengbrecht 0:563b70517320 259
sgodinez 11:134435d8a2d5 260 Cellular::Code Cellular::setApn(const std::string& apn) {
sgodinez 11:134435d8a2d5 261 Code code = sendBasicCommand("AT#APNSERV=\"" + apn + "\"", 1000);
sgodinez 11:134435d8a2d5 262 if (code != OK) {
sgodinez 11:134435d8a2d5 263 return code;
sgodinez 11:134435d8a2d5 264 }
sgodinez 11:134435d8a2d5 265 this->apn = apn;
sgodinez 11:134435d8a2d5 266 return code;
sgodinez 11:134435d8a2d5 267 }
sgodinez 11:134435d8a2d5 268
sgodinez 11:134435d8a2d5 269 Cellular::Code Cellular::setDns(const std::string& apn) {
sgodinez 11:134435d8a2d5 270
sgodinez 11:134435d8a2d5 271 }
sgodinez 11:134435d8a2d5 272
sgodinez 4:6561c9128c6f 273 Cellular::Code Cellular::sendSMS(const Sms& sms) {
sgodinez 4:6561c9128c6f 274 return sendSMS(sms.phoneNumber, sms.message);
sgodinez 4:6561c9128c6f 275 }
sgodinez 4:6561c9128c6f 276
sgodinez 4:6561c9128c6f 277 Cellular::Code Cellular::sendSMS(const std::string& phoneNumber, const std::string& message)
jengbrecht 0:563b70517320 278 {
jengbrecht 0:563b70517320 279 Code code = sendBasicCommand("AT+CMGF=1", 1000);
jengbrecht 0:563b70517320 280 if (code != OK) {
jengbrecht 0:563b70517320 281 return code;
jengbrecht 0:563b70517320 282 }
jengbrecht 0:563b70517320 283 string cmd = "AT+CMGS=\"+";
jengbrecht 0:563b70517320 284 cmd.append(phoneNumber);
jengbrecht 0:563b70517320 285 cmd.append("\"");
jengbrecht 0:563b70517320 286 string response1 = sendCommand(cmd, 1000);
jengbrecht 0:563b70517320 287 if (response1.find('>') == string::npos) {
jengbrecht 0:563b70517320 288 return NO_RESPONSE;
jengbrecht 0:563b70517320 289 }
jengbrecht 0:563b70517320 290 wait(.2);
jengbrecht 0:563b70517320 291 string response2 = sendCommand(message, 4000, CTRL_Z);
sgodinez 4:6561c9128c6f 292 printf("SMS Response: %s\n", response2.c_str());
jengbrecht 0:563b70517320 293 if (response2.find("+CMGS:") == string::npos) {
jengbrecht 0:563b70517320 294 return FAILURE;
jengbrecht 0:563b70517320 295 }
jengbrecht 0:563b70517320 296 return OK;
jengbrecht 0:563b70517320 297 }
jengbrecht 0:563b70517320 298
sgodinez 4:6561c9128c6f 299 std::vector<Cellular::Sms> Cellular::getReceivedSms() {
sgodinez 9:5b12c5a8dde4 300 int smsNumber = 0;
sgodinez 4:6561c9128c6f 301 std::vector<Sms> vSms;
sgodinez 4:6561c9128c6f 302 std::string received = sendCommand("AT+CMGL=\"ALL\"", 4000);
sgodinez 5:93e889a5abc6 303 size_t pos = received.find("+CMGL: ");
sgodinez 4:6561c9128c6f 304
sgodinez 5:93e889a5abc6 305 while (pos != std::string::npos) {
sgodinez 4:6561c9128c6f 306 Cellular::Sms sms;
sgodinez 4:6561c9128c6f 307 std::string line(Text::getLine(received, pos, pos));
sgodinez 9:5b12c5a8dde4 308 //printf("[DEBUG] Top of SMS Parse Loop. LINE[%s]\n", line.c_str());
sgodinez 9:5b12c5a8dde4 309 if(line.find("+CMGL: ") == std::string::npos) {
sgodinez 4:6561c9128c6f 310 continue;
sgodinez 4:6561c9128c6f 311 }
sgodinez 4:6561c9128c6f 312
sgodinez 4:6561c9128c6f 313 //Start of SMS message
sgodinez 4:6561c9128c6f 314 std::vector<std::string> vSmsParts = Text::split(line, ',');
sgodinez 4:6561c9128c6f 315 if(vSmsParts.size() != 6) {
sgodinez 9:5b12c5a8dde4 316 printf("[WARNING] Expected 6 commas. SMS[%d] DATA[%s]. Continuing ...\n", smsNumber, line.c_str());
sgodinez 4:6561c9128c6f 317 continue;
sgodinez 4:6561c9128c6f 318 }
sgodinez 4:6561c9128c6f 319
sgodinez 4:6561c9128c6f 320 sms.phoneNumber = vSmsParts[2];
sgodinez 4:6561c9128c6f 321 sms.timestamp = vSmsParts[4] + ", " + vSmsParts[5];
sgodinez 4:6561c9128c6f 322
sgodinez 8:3fe68d6130a8 323 if(pos == std::string::npos) {
sgodinez 9:5b12c5a8dde4 324 printf("[WARNING] Expected SMS body. SMS[%d]. Leaving ...\n", smsNumber);
sgodinez 8:3fe68d6130a8 325 break;
sgodinez 8:3fe68d6130a8 326 }
sgodinez 9:5b12c5a8dde4 327 //Check for the start of the next SMS message
sgodinez 9:5b12c5a8dde4 328 size_t bodyEnd = received.find("\r\n+CMGL: ", pos);
sgodinez 8:3fe68d6130a8 329 if(bodyEnd == std::string::npos) {
sgodinez 9:5b12c5a8dde4 330 //printf("[DEBUG] Parsing Last SMS. SMS[%d]\n", smsNumber);
sgodinez 9:5b12c5a8dde4 331 //This must be the last SMS message
sgodinez 9:5b12c5a8dde4 332 bodyEnd = received.find("\r\n\r\nOK", pos);
sgodinez 8:3fe68d6130a8 333 }
sgodinez 9:5b12c5a8dde4 334
sgodinez 9:5b12c5a8dde4 335 //Safety check that we found the boundary of this current SMS message
sgodinez 9:5b12c5a8dde4 336 if(bodyEnd != std::string::npos) {
sgodinez 9:5b12c5a8dde4 337 sms.message = received.substr(pos, bodyEnd - pos);
sgodinez 9:5b12c5a8dde4 338 } else {
sgodinez 8:3fe68d6130a8 339 sms.message = received.substr(pos);
sgodinez 9:5b12c5a8dde4 340 printf("[WARNING] Expected to find end of SMS list. SMS[%d] DATA[%s].\n", smsNumber, sms.message.c_str());
sgodinez 8:3fe68d6130a8 341 }
sgodinez 5:93e889a5abc6 342 vSms.push_back(sms);
sgodinez 4:6561c9128c6f 343 pos = bodyEnd;
sgodinez 9:5b12c5a8dde4 344 //printf("[DEBUG] Parsed SMS[%d]. Starting Next at position [%d]\n", smsNumber, pos);
sgodinez 9:5b12c5a8dde4 345 smsNumber++;
sgodinez 4:6561c9128c6f 346 }
sgodinez 9:5b12c5a8dde4 347 printf("Received %d SMS\n", smsNumber);
sgodinez 4:6561c9128c6f 348 return vSms;
sgodinez 4:6561c9128c6f 349 }
sgodinez 4:6561c9128c6f 350
sgodinez 4:6561c9128c6f 351 Cellular::Code Cellular::deleteOnlyReceivedReadSms() {
sgodinez 4:6561c9128c6f 352 return sendBasicCommand("AT+CMGD=1,1", 1000);
sgodinez 4:6561c9128c6f 353 }
sgodinez 4:6561c9128c6f 354
sgodinez 4:6561c9128c6f 355 Cellular::Code Cellular::deleteAllReceivedSms() {
sgodinez 4:6561c9128c6f 356 return sendBasicCommand("AT+CMGD=1,4", 1000);
sgodinez 4:6561c9128c6f 357 }
sgodinez 4:6561c9128c6f 358
sgodinez 4:6561c9128c6f 359
jengbrecht 0:563b70517320 360 string Cellular::sendCommand(string command, int timeoutMillis, ESC_CHAR esc)
jengbrecht 0:563b70517320 361 {
jengbrecht 0:563b70517320 362 int size = command.size() + 1;
jengbrecht 0:563b70517320 363 char cmd[size];
jengbrecht 0:563b70517320 364 strcpy(cmd, command.c_str());
jengbrecht 0:563b70517320 365 if (esc == CR) {
jengbrecht 0:563b70517320 366 cmd[size -1] = '\r';
jengbrecht 0:563b70517320 367 } else if (esc == CTRL_Z) {
jengbrecht 0:563b70517320 368 cmd[size -1] = 0x1A;
jengbrecht 0:563b70517320 369 }
jengbrecht 0:563b70517320 370
sgodinez 4:6561c9128c6f 371 io.rxClear();
sgodinez 4:6561c9128c6f 372 io.txClear();
sgodinez 8:3fe68d6130a8 373 std::string result;
sgodinez 4:6561c9128c6f 374 int status = io.write(cmd, size);
sgodinez 4:6561c9128c6f 375 int available = io.rxAvailable();
jengbrecht 0:563b70517320 376 int previous = -1;
jengbrecht 0:563b70517320 377 int timer = 0;
sgodinez 8:3fe68d6130a8 378 char tmp[256];
sgodinez 8:3fe68d6130a8 379 tmp[255] = 0;
sgodinez 8:3fe68d6130a8 380
sgodinez 8:3fe68d6130a8 381 do {
jengbrecht 0:563b70517320 382 wait(.1);
jengbrecht 0:563b70517320 383 timer = timer + 100;
jengbrecht 0:563b70517320 384 previous = available;
sgodinez 4:6561c9128c6f 385 available = io.rxAvailable();
sgodinez 8:3fe68d6130a8 386
sgodinez 8:3fe68d6130a8 387 int size = io.read(tmp,255); //1 less than allocated
sgodinez 8:3fe68d6130a8 388 if(size > 0) {
sgodinez 8:3fe68d6130a8 389 result.append(tmp, size);
sgodinez 8:3fe68d6130a8 390 }
sgodinez 8:3fe68d6130a8 391 } while (available != previous && timer < timeoutMillis);
sgodinez 8:3fe68d6130a8 392
sgodinez 8:3fe68d6130a8 393 return result;
jengbrecht 0:563b70517320 394 }
jengbrecht 0:563b70517320 395
jengbrecht 0:563b70517320 396 #endif /* CELLULAR_CPP */