Simple detection for LE910-NA1 modules
Fork of MTS-Cellular by
Cellular/Cellular.cpp@18:fa0d8120f81f, 2014-06-16 (annotated)
- Committer:
- mfiore
- Date:
- Mon Jun 16 13:50:28 2014 +0000
- Revision:
- 18:fa0d8120f81f
- Parent:
- 16:1bc3e44d4746
- Child:
- 23:8333593dd86f
update SUCCESS, FAILURE, and friends to MTS_* so they don't conflict with definitions for STMicro boards
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Mike Fiore |
4:1f63354b8d1b | 1 | #include "mbed.h" |
Mike Fiore |
1:f155d94d6f3a | 2 | #include "Cellular.h" |
Mike Fiore |
1:f155d94d6f3a | 3 | #include "MTSText.h" |
Mike Fiore |
4:1f63354b8d1b | 4 | #include "MTSLog.h" |
Mike Fiore |
1:f155d94d6f3a | 5 | |
Mike Fiore |
1:f155d94d6f3a | 6 | using namespace mts; |
Mike Fiore |
1:f155d94d6f3a | 7 | |
Mike Fiore |
8:2d7259d244d1 | 8 | bool Cellular::init(MTSBufferedIO* io) |
Mike Fiore |
8:2d7259d244d1 | 9 | { |
Mike Fiore |
8:2d7259d244d1 | 10 | if (io == NULL) { |
Mike Fiore |
8:2d7259d244d1 | 11 | return false; |
Mike Fiore |
8:2d7259d244d1 | 12 | } |
Mike Fiore |
8:2d7259d244d1 | 13 | this->io = io; |
Mike Fiore |
8:2d7259d244d1 | 14 | |
Mike Fiore |
8:2d7259d244d1 | 15 | return true; |
Mike Fiore |
8:2d7259d244d1 | 16 | } |
Mike Fiore |
8:2d7259d244d1 | 17 | |
Mike Fiore |
9:1a03e3f3e7fe | 18 | bool Cellular::configureSignals(unsigned int DCD, unsigned int DTR, unsigned int RESET) |
Mike Fiore |
9:1a03e3f3e7fe | 19 | { |
Mike Fiore |
9:1a03e3f3e7fe | 20 | //Set DCD - The radio will raise and lower this line |
Mike Fiore |
9:1a03e3f3e7fe | 21 | if (DCD != NC) { |
Mike Fiore |
9:1a03e3f3e7fe | 22 | dcd = new DigitalIn(PinName(DCD)); |
Mike Fiore |
9:1a03e3f3e7fe | 23 | } |
Mike Fiore |
9:1a03e3f3e7fe | 24 | /* Set DTR - This line should be lowered when we want to talk to the radio and raised when we're done |
Mike Fiore |
9:1a03e3f3e7fe | 25 | * for now we will lower it in the constructor and raise it in the destructor. |
Mike Fiore |
9:1a03e3f3e7fe | 26 | */ |
Mike Fiore |
9:1a03e3f3e7fe | 27 | if (DTR != NC) { |
Mike Fiore |
9:1a03e3f3e7fe | 28 | dtr = new DigitalOut(PinName(DTR)); |
Mike Fiore |
9:1a03e3f3e7fe | 29 | dtr->write(0); |
Mike Fiore |
9:1a03e3f3e7fe | 30 | } |
Mike Fiore |
9:1a03e3f3e7fe | 31 | //Set RESET - Set the hardware reset line to the radio |
Mike Fiore |
9:1a03e3f3e7fe | 32 | if (RESET != NC) { |
Mike Fiore |
9:1a03e3f3e7fe | 33 | resetLine = new DigitalOut(PinName(RESET)); |
Mike Fiore |
9:1a03e3f3e7fe | 34 | } |
Mike Fiore |
9:1a03e3f3e7fe | 35 | return true; |
Mike Fiore |
9:1a03e3f3e7fe | 36 | } |
Mike Fiore |
9:1a03e3f3e7fe | 37 | |
Mike Fiore |
1:f155d94d6f3a | 38 | std::string Cellular::getRegistrationNames(Registration registration) |
Mike Fiore |
1:f155d94d6f3a | 39 | { |
Mike Fiore |
1:f155d94d6f3a | 40 | switch(registration) { |
Mike Fiore |
1:f155d94d6f3a | 41 | case NOT_REGISTERED: |
Mike Fiore |
1:f155d94d6f3a | 42 | return "NOT_REGISTERED"; |
Mike Fiore |
1:f155d94d6f3a | 43 | case REGISTERED: |
Mike Fiore |
1:f155d94d6f3a | 44 | return "REGISTERED"; |
Mike Fiore |
1:f155d94d6f3a | 45 | case SEARCHING: |
Mike Fiore |
1:f155d94d6f3a | 46 | return "SEARCHING"; |
Mike Fiore |
1:f155d94d6f3a | 47 | case DENIED: |
Mike Fiore |
1:f155d94d6f3a | 48 | return "DENIED"; |
Mike Fiore |
1:f155d94d6f3a | 49 | case UNKNOWN: |
Mike Fiore |
1:f155d94d6f3a | 50 | return "UNKNOWN"; |
Mike Fiore |
1:f155d94d6f3a | 51 | case ROAMING: |
Mike Fiore |
1:f155d94d6f3a | 52 | return "ROAMING"; |
Mike Fiore |
1:f155d94d6f3a | 53 | default: |
Mike Fiore |
1:f155d94d6f3a | 54 | return "UNKNOWN ENUM"; |
Mike Fiore |
1:f155d94d6f3a | 55 | } |
Mike Fiore |
1:f155d94d6f3a | 56 | } |
Mike Fiore |
1:f155d94d6f3a | 57 | |
Mike Fiore |
11:4e428f689069 | 58 | std::string Cellular::getRadioNames(Radio radio) { |
Mike Fiore |
11:4e428f689069 | 59 | switch(radio) { |
Mike Fiore |
11:4e428f689069 | 60 | case MTSMC_H5: |
Mike Fiore |
11:4e428f689069 | 61 | return "MTSMC-H5"; |
Mike Fiore |
11:4e428f689069 | 62 | case MTSMC_EV3: |
Mike Fiore |
11:4e428f689069 | 63 | return "MTSMC-EV3"; |
Mike Fiore |
11:4e428f689069 | 64 | case MTSMC_G3: |
Mike Fiore |
11:4e428f689069 | 65 | return "MTSMC-G3"; |
Mike Fiore |
11:4e428f689069 | 66 | case MTSMC_C2: |
Mike Fiore |
11:4e428f689069 | 67 | return "MTSMC-C2"; |
Mike Fiore |
11:4e428f689069 | 68 | case MTSMC_H5_IP: |
Mike Fiore |
11:4e428f689069 | 69 | return "MTSMC-H5-IP"; |
Mike Fiore |
11:4e428f689069 | 70 | case MTSMC_EV3_IP: |
Mike Fiore |
11:4e428f689069 | 71 | return "MTSMC-EV3-IP"; |
Mike Fiore |
11:4e428f689069 | 72 | case MTSMC_C2_IP: |
Mike Fiore |
11:4e428f689069 | 73 | return "MTSMC-C2-IP"; |
Mike Fiore |
11:4e428f689069 | 74 | default: |
Mike Fiore |
11:4e428f689069 | 75 | return "UNKNOWN ENUM"; |
Mike Fiore |
11:4e428f689069 | 76 | } |
Mike Fiore |
11:4e428f689069 | 77 | } |
Mike Fiore |
11:4e428f689069 | 78 | |
Mike Fiore |
1:f155d94d6f3a | 79 | Code Cellular::test() |
Mike Fiore |
1:f155d94d6f3a | 80 | { |
Mike Fiore |
1:f155d94d6f3a | 81 | int i = 0; |
mfiore | 18:fa0d8120f81f | 82 | while (sendBasicCommand("AT", 1000) != MTS_SUCCESS) { |
Mike Fiore |
1:f155d94d6f3a | 83 | i++; |
Mike Fiore |
1:f155d94d6f3a | 84 | if (i >= 30) { |
Mike Fiore |
4:1f63354b8d1b | 85 | logError("Could not talk to radio after 30 tries"); |
Mike Fiore |
1:f155d94d6f3a | 86 | i = 0; |
Mike Fiore |
1:f155d94d6f3a | 87 | } |
Mike Fiore |
1:f155d94d6f3a | 88 | wait(1); |
Mike Fiore |
1:f155d94d6f3a | 89 | } |
mfiore | 18:fa0d8120f81f | 90 | return MTS_SUCCESS; |
Mike Fiore |
1:f155d94d6f3a | 91 | } |
Mike Fiore |
1:f155d94d6f3a | 92 | |
Mike Fiore |
1:f155d94d6f3a | 93 | int Cellular::getSignalStrength() |
Mike Fiore |
1:f155d94d6f3a | 94 | { |
Mike Fiore |
1:f155d94d6f3a | 95 | string response = sendCommand("AT+CSQ", 1000); |
Mike Fiore |
1:f155d94d6f3a | 96 | if (response.find("OK") == string::npos) { |
Mike Fiore |
1:f155d94d6f3a | 97 | return -1; |
Mike Fiore |
1:f155d94d6f3a | 98 | } |
Mike Fiore |
1:f155d94d6f3a | 99 | int start = response.find(':'); |
Mike Fiore |
1:f155d94d6f3a | 100 | int stop = response.find(',', start); |
Mike Fiore |
1:f155d94d6f3a | 101 | string signal = response.substr(start + 2, stop - start - 2); |
Mike Fiore |
1:f155d94d6f3a | 102 | int value; |
Mike Fiore |
1:f155d94d6f3a | 103 | sscanf(signal.c_str(), "%d", &value); |
Mike Fiore |
1:f155d94d6f3a | 104 | return value; |
Mike Fiore |
1:f155d94d6f3a | 105 | } |
Mike Fiore |
1:f155d94d6f3a | 106 | |
Mike Fiore |
1:f155d94d6f3a | 107 | Cellular::Registration Cellular::getRegistration() |
Mike Fiore |
1:f155d94d6f3a | 108 | { |
Mike Fiore |
1:f155d94d6f3a | 109 | string response = sendCommand("AT+CREG?", 5000); |
Mike Fiore |
1:f155d94d6f3a | 110 | if (response.find("OK") == string::npos) { |
Mike Fiore |
1:f155d94d6f3a | 111 | return UNKNOWN; |
Mike Fiore |
1:f155d94d6f3a | 112 | } |
Mike Fiore |
1:f155d94d6f3a | 113 | int start = response.find(','); |
Mike Fiore |
1:f155d94d6f3a | 114 | int stop = response.find(' ', start); |
Mike Fiore |
1:f155d94d6f3a | 115 | string regStat = response.substr(start + 1, stop - start - 1); |
Mike Fiore |
1:f155d94d6f3a | 116 | int value; |
Mike Fiore |
1:f155d94d6f3a | 117 | sscanf(regStat.c_str(), "%d", &value); |
Mike Fiore |
1:f155d94d6f3a | 118 | switch (value) { |
Mike Fiore |
1:f155d94d6f3a | 119 | case 0: |
Mike Fiore |
1:f155d94d6f3a | 120 | return NOT_REGISTERED; |
Mike Fiore |
1:f155d94d6f3a | 121 | case 1: |
Mike Fiore |
1:f155d94d6f3a | 122 | return REGISTERED; |
Mike Fiore |
1:f155d94d6f3a | 123 | case 2: |
Mike Fiore |
1:f155d94d6f3a | 124 | return SEARCHING; |
Mike Fiore |
1:f155d94d6f3a | 125 | case 3: |
Mike Fiore |
1:f155d94d6f3a | 126 | return DENIED; |
Mike Fiore |
1:f155d94d6f3a | 127 | case 4: |
Mike Fiore |
1:f155d94d6f3a | 128 | return UNKNOWN; |
Mike Fiore |
1:f155d94d6f3a | 129 | case 5: |
Mike Fiore |
1:f155d94d6f3a | 130 | return ROAMING; |
Mike Fiore |
1:f155d94d6f3a | 131 | } |
Mike Fiore |
1:f155d94d6f3a | 132 | return UNKNOWN; |
Mike Fiore |
1:f155d94d6f3a | 133 | } |
Mike Fiore |
7:0ee8e69a3e9c | 134 | |
Mike Fiore |
8:2d7259d244d1 | 135 | Code Cellular::setApn(const std::string& apn) |
Mike Fiore |
8:2d7259d244d1 | 136 | { |
mfiore | 16:1bc3e44d4746 | 137 | if (type == MTSMC_H5_IP || type == MTSMC_H5 || type == MTSMC_G3) { |
mfiore | 16:1bc3e44d4746 | 138 | Code code = sendBasicCommand("AT#APNSERV=\"" + apn + "\"", 1000); |
mfiore | 18:fa0d8120f81f | 139 | if (code != MTS_SUCCESS) { |
mfiore | 16:1bc3e44d4746 | 140 | return code; |
mfiore | 16:1bc3e44d4746 | 141 | } |
mfiore | 16:1bc3e44d4746 | 142 | this->apn = apn; |
Mike Fiore |
8:2d7259d244d1 | 143 | return code; |
mfiore | 16:1bc3e44d4746 | 144 | } else { |
mfiore | 16:1bc3e44d4746 | 145 | logInfo("CDMA radios don't need an APN"); |
mfiore | 18:fa0d8120f81f | 146 | return MTS_SUCCESS; |
Mike Fiore |
8:2d7259d244d1 | 147 | } |
Mike Fiore |
8:2d7259d244d1 | 148 | } |
Mike Fiore |
8:2d7259d244d1 | 149 | |
Mike Fiore |
8:2d7259d244d1 | 150 | |
Mike Fiore |
8:2d7259d244d1 | 151 | Code Cellular::setDns(const std::string& primary, const std::string& secondary) |
Mike Fiore |
8:2d7259d244d1 | 152 | { |
Mike Fiore |
8:2d7259d244d1 | 153 | return sendBasicCommand("AT#DNS=1," + primary + "," + secondary, 1000); |
Mike Fiore |
8:2d7259d244d1 | 154 | } |
Mike Fiore |
8:2d7259d244d1 | 155 | |
Mike Fiore |
7:0ee8e69a3e9c | 156 | Code Cellular::sendSMS(const Sms& sms) |
Mike Fiore |
7:0ee8e69a3e9c | 157 | { |
Mike Fiore |
7:0ee8e69a3e9c | 158 | return sendSMS(sms.phoneNumber, sms.message); |
Mike Fiore |
7:0ee8e69a3e9c | 159 | } |
Mike Fiore |
7:0ee8e69a3e9c | 160 | |
Mike Fiore |
10:c188cc05aed5 | 161 | Code Cellular::sendBasicCommand(const std::string& command, unsigned int timeoutMillis, char esc) |
Mike Fiore |
10:c188cc05aed5 | 162 | { |
Mike Fiore |
10:c188cc05aed5 | 163 | if(socketOpened) { |
Mike Fiore |
10:c188cc05aed5 | 164 | logError("socket is open. Can not send AT commands"); |
mfiore | 18:fa0d8120f81f | 165 | return MTS_ERROR; |
Mike Fiore |
10:c188cc05aed5 | 166 | } |
Mike Fiore |
10:c188cc05aed5 | 167 | |
Mike Fiore |
10:c188cc05aed5 | 168 | string response = sendCommand(command, timeoutMillis, esc); |
Mike Fiore |
10:c188cc05aed5 | 169 | if (response.size() == 0) { |
mfiore | 18:fa0d8120f81f | 170 | return MTS_NO_RESPONSE; |
Mike Fiore |
10:c188cc05aed5 | 171 | } else if (response.find("OK") != string::npos) { |
mfiore | 18:fa0d8120f81f | 172 | return MTS_SUCCESS; |
Mike Fiore |
10:c188cc05aed5 | 173 | } else if (response.find("ERROR") != string::npos) { |
mfiore | 18:fa0d8120f81f | 174 | return MTS_ERROR; |
Mike Fiore |
10:c188cc05aed5 | 175 | } else { |
mfiore | 18:fa0d8120f81f | 176 | return MTS_FAILURE; |
Mike Fiore |
10:c188cc05aed5 | 177 | } |
Mike Fiore |
10:c188cc05aed5 | 178 | } |
Mike Fiore |
10:c188cc05aed5 | 179 | |
Mike Fiore |
10:c188cc05aed5 | 180 | string Cellular::sendCommand(const std::string& command, unsigned int timeoutMillis, char esc) |
Mike Fiore |
10:c188cc05aed5 | 181 | { |
Mike Fiore |
10:c188cc05aed5 | 182 | if(io == NULL) { |
Mike Fiore |
10:c188cc05aed5 | 183 | logError("MTSBufferedIO not set"); |
Mike Fiore |
10:c188cc05aed5 | 184 | return ""; |
Mike Fiore |
10:c188cc05aed5 | 185 | } |
Mike Fiore |
10:c188cc05aed5 | 186 | if(socketOpened) { |
Mike Fiore |
10:c188cc05aed5 | 187 | logError("socket is open. Can not send AT commands"); |
Mike Fiore |
10:c188cc05aed5 | 188 | return ""; |
Mike Fiore |
10:c188cc05aed5 | 189 | } |
Mike Fiore |
10:c188cc05aed5 | 190 | |
Mike Fiore |
10:c188cc05aed5 | 191 | io->rxClear(); |
Mike Fiore |
10:c188cc05aed5 | 192 | io->txClear(); |
Mike Fiore |
10:c188cc05aed5 | 193 | std::string result; |
Mike Fiore |
10:c188cc05aed5 | 194 | |
Mike Fiore |
10:c188cc05aed5 | 195 | //Attempt to write command |
Mike Fiore |
10:c188cc05aed5 | 196 | if(io->write(command.data(), command.size(), timeoutMillis) != command.size()) { |
Mike Fiore |
10:c188cc05aed5 | 197 | //Failed to write command |
Mike Fiore |
10:c188cc05aed5 | 198 | if (command != "AT" && command != "at") { |
Mike Fiore |
10:c188cc05aed5 | 199 | logError("failed to send command to radio within %d milliseconds", timeoutMillis); |
Mike Fiore |
10:c188cc05aed5 | 200 | } |
Mike Fiore |
10:c188cc05aed5 | 201 | return ""; |
Mike Fiore |
10:c188cc05aed5 | 202 | } |
Mike Fiore |
10:c188cc05aed5 | 203 | |
Mike Fiore |
10:c188cc05aed5 | 204 | //Send Escape Character |
Mike Fiore |
10:c188cc05aed5 | 205 | if (esc != 0x00) { |
Mike Fiore |
10:c188cc05aed5 | 206 | if(io->write(esc, timeoutMillis) != 1) { |
Mike Fiore |
10:c188cc05aed5 | 207 | if (command != "AT" && command != "at") { |
Mike Fiore |
10:c188cc05aed5 | 208 | logError("failed to send character '%c' (0x%02X) to radio within %d milliseconds", esc, esc, timeoutMillis); |
Mike Fiore |
10:c188cc05aed5 | 209 | } |
Mike Fiore |
10:c188cc05aed5 | 210 | return ""; |
Mike Fiore |
10:c188cc05aed5 | 211 | } |
Mike Fiore |
10:c188cc05aed5 | 212 | } |
Mike Fiore |
10:c188cc05aed5 | 213 | |
Mike Fiore |
10:c188cc05aed5 | 214 | int timer = 0; |
Mike Fiore |
10:c188cc05aed5 | 215 | size_t previous = 0; |
Mike Fiore |
10:c188cc05aed5 | 216 | char tmp[256]; |
Mike Fiore |
10:c188cc05aed5 | 217 | tmp[255] = 0; |
Mike Fiore |
10:c188cc05aed5 | 218 | bool started = !echoMode; |
Mike Fiore |
10:c188cc05aed5 | 219 | bool done = false; |
Mike Fiore |
10:c188cc05aed5 | 220 | do { |
Mike Fiore |
10:c188cc05aed5 | 221 | wait(0.1); |
Mike Fiore |
10:c188cc05aed5 | 222 | timer += 100; |
Mike Fiore |
10:c188cc05aed5 | 223 | |
Mike Fiore |
10:c188cc05aed5 | 224 | previous = result.size(); |
Mike Fiore |
10:c188cc05aed5 | 225 | //Make a non-blocking read call by passing timeout of zero |
Mike Fiore |
10:c188cc05aed5 | 226 | int size = io->read(tmp,255,0); //1 less than allocated (timeout is instant) |
Mike Fiore |
10:c188cc05aed5 | 227 | if(size > 0) { |
Mike Fiore |
10:c188cc05aed5 | 228 | result.append(tmp, size); |
Mike Fiore |
10:c188cc05aed5 | 229 | } |
Mike Fiore |
10:c188cc05aed5 | 230 | if(!started) { |
Mike Fiore |
10:c188cc05aed5 | 231 | //In Echo Mode (Command will have echo'd + 2 characters for \r\n) |
Mike Fiore |
10:c188cc05aed5 | 232 | if(result.size() > command.size() + 2) { |
Mike Fiore |
10:c188cc05aed5 | 233 | started = true; |
Mike Fiore |
10:c188cc05aed5 | 234 | } |
Mike Fiore |
10:c188cc05aed5 | 235 | } else { |
Mike Fiore |
10:c188cc05aed5 | 236 | done = (result.size() == previous); |
Mike Fiore |
10:c188cc05aed5 | 237 | } |
Mike Fiore |
10:c188cc05aed5 | 238 | if(timer >= timeoutMillis) { |
Mike Fiore |
10:c188cc05aed5 | 239 | if (command != "AT" && command != "at") { |
Mike Fiore |
10:c188cc05aed5 | 240 | logWarning("sendCommand [%s] timed out after %d milliseconds", command.c_str(), timeoutMillis); |
Mike Fiore |
10:c188cc05aed5 | 241 | } |
Mike Fiore |
10:c188cc05aed5 | 242 | done = true; |
Mike Fiore |
10:c188cc05aed5 | 243 | } |
Mike Fiore |
10:c188cc05aed5 | 244 | } while (!done); |
Mike Fiore |
10:c188cc05aed5 | 245 | |
Mike Fiore |
10:c188cc05aed5 | 246 | return result; |
Mike Fiore |
10:c188cc05aed5 | 247 | } |
Mike Fiore |
10:c188cc05aed5 | 248 | |
Mike Fiore |
7:0ee8e69a3e9c | 249 | Code Cellular::sendSMS(const std::string& phoneNumber, const std::string& message) |
Mike Fiore |
7:0ee8e69a3e9c | 250 | { |
mfiore | 16:1bc3e44d4746 | 251 | string csmp; |
mfiore | 16:1bc3e44d4746 | 252 | |
mfiore | 16:1bc3e44d4746 | 253 | if (type == MTSMC_H5_IP || type == MTSMC_H5) { |
mfiore | 16:1bc3e44d4746 | 254 | csmp = "AT+CSMP=17,167,0,0"; |
mfiore | 16:1bc3e44d4746 | 255 | } else if (type == MTSMC_EV3_IP || type == MTSMC_EV3 || type == MTSMC_C2_IP || type == MTSMC_C2) { |
mfiore | 16:1bc3e44d4746 | 256 | csmp = "AT+CSMP=,4098,0,2"; |
mfiore | 16:1bc3e44d4746 | 257 | } else if (type == MTSMC_G3) { |
mfiore | 16:1bc3e44d4746 | 258 | } else { |
mfiore | 16:1bc3e44d4746 | 259 | logError("unknown radio type [%d]", type); |
mfiore | 18:fa0d8120f81f | 260 | return MTS_FAILURE; |
mfiore | 16:1bc3e44d4746 | 261 | } |
mfiore | 16:1bc3e44d4746 | 262 | |
Mike Fiore |
7:0ee8e69a3e9c | 263 | Code code = sendBasicCommand("AT+CMGF=1", 1000); |
mfiore | 18:fa0d8120f81f | 264 | if (code != MTS_SUCCESS) { |
mfiore | 16:1bc3e44d4746 | 265 | logError("CMGF failed"); |
mfiore | 16:1bc3e44d4746 | 266 | return code; |
mfiore | 16:1bc3e44d4746 | 267 | } |
mfiore | 16:1bc3e44d4746 | 268 | code = sendBasicCommand(csmp, 1000); |
mfiore | 18:fa0d8120f81f | 269 | if (code != MTS_SUCCESS) { |
mfiore | 16:1bc3e44d4746 | 270 | logError("CSMP failed [%s]", getRadioNames(type).c_str()); |
Mike Fiore |
7:0ee8e69a3e9c | 271 | return code; |
Mike Fiore |
7:0ee8e69a3e9c | 272 | } |
Mike Fiore |
7:0ee8e69a3e9c | 273 | string cmd = "AT+CMGS=\"+"; |
Mike Fiore |
7:0ee8e69a3e9c | 274 | cmd.append(phoneNumber); |
mfiore | 16:1bc3e44d4746 | 275 | cmd.append("\",145"); |
Mike Fiore |
7:0ee8e69a3e9c | 276 | string response1 = sendCommand(cmd, 1000); |
Mike Fiore |
7:0ee8e69a3e9c | 277 | if (response1.find('>') == string::npos) { |
mfiore | 16:1bc3e44d4746 | 278 | logError("CMGS phone number failed"); |
mfiore | 18:fa0d8120f81f | 279 | return MTS_NO_RESPONSE; |
Mike Fiore |
7:0ee8e69a3e9c | 280 | } |
Mike Fiore |
7:0ee8e69a3e9c | 281 | wait(.2); |
Mike Fiore |
7:0ee8e69a3e9c | 282 | string response2 = sendCommand(message, 4000, CTRL_Z); |
mfiore | 16:1bc3e44d4746 | 283 | logInfo("SMS Response: [%s]", response2.c_str()); |
Mike Fiore |
7:0ee8e69a3e9c | 284 | if (response2.find("+CMGS:") == string::npos) { |
mfiore | 16:1bc3e44d4746 | 285 | logError("CMGS message failed"); |
mfiore | 18:fa0d8120f81f | 286 | return MTS_FAILURE; |
Mike Fiore |
7:0ee8e69a3e9c | 287 | } |
mfiore | 18:fa0d8120f81f | 288 | return MTS_SUCCESS; |
Mike Fiore |
7:0ee8e69a3e9c | 289 | } |
Mike Fiore |
7:0ee8e69a3e9c | 290 | |
Mike Fiore |
7:0ee8e69a3e9c | 291 | std::vector<Cellular::Sms> Cellular::getReceivedSms() |
Mike Fiore |
7:0ee8e69a3e9c | 292 | { |
Mike Fiore |
7:0ee8e69a3e9c | 293 | int smsNumber = 0; |
Mike Fiore |
7:0ee8e69a3e9c | 294 | std::vector<Sms> vSms; |
mfiore | 16:1bc3e44d4746 | 295 | std::string received; |
mfiore | 16:1bc3e44d4746 | 296 | size_t pos; |
mfiore | 16:1bc3e44d4746 | 297 | |
mfiore | 16:1bc3e44d4746 | 298 | Code code = sendBasicCommand("AT+CMGF=1", 1000); |
mfiore | 18:fa0d8120f81f | 299 | if (code != MTS_SUCCESS) { |
mfiore | 16:1bc3e44d4746 | 300 | logError("CMGF failed"); |
mfiore | 16:1bc3e44d4746 | 301 | return vSms; |
mfiore | 16:1bc3e44d4746 | 302 | } |
mfiore | 16:1bc3e44d4746 | 303 | |
mfiore | 16:1bc3e44d4746 | 304 | received = sendCommand("AT+CMGL=\"ALL\"", 4000); |
mfiore | 16:1bc3e44d4746 | 305 | pos = received.find("+CMGL: "); |
Mike Fiore |
7:0ee8e69a3e9c | 306 | |
Mike Fiore |
7:0ee8e69a3e9c | 307 | while (pos != std::string::npos) { |
Mike Fiore |
7:0ee8e69a3e9c | 308 | Cellular::Sms sms; |
Mike Fiore |
7:0ee8e69a3e9c | 309 | std::string line(Text::getLine(received, pos, pos)); |
Mike Fiore |
7:0ee8e69a3e9c | 310 | if(line.find("+CMGL: ") == std::string::npos) { |
Mike Fiore |
7:0ee8e69a3e9c | 311 | continue; |
Mike Fiore |
7:0ee8e69a3e9c | 312 | } |
Mike Fiore |
7:0ee8e69a3e9c | 313 | |
Mike Fiore |
7:0ee8e69a3e9c | 314 | //Start of SMS message |
Mike Fiore |
7:0ee8e69a3e9c | 315 | std::vector<std::string> vSmsParts = Text::split(line, ','); |
mfiore | 16:1bc3e44d4746 | 316 | if (type == MTSMC_H5_IP || type == MTSMC_H5) { |
mfiore | 16:1bc3e44d4746 | 317 | /* format for H5 and H5-IP radios |
mfiore | 16:1bc3e44d4746 | 318 | * <index>, <status>, <oa>, <alpha>, <scts> |
mfiore | 16:1bc3e44d4746 | 319 | * scts contains a comma, so splitting on commas should give us 6 items |
mfiore | 16:1bc3e44d4746 | 320 | */ |
mfiore | 16:1bc3e44d4746 | 321 | if(vSmsParts.size() != 6) { |
mfiore | 16:1bc3e44d4746 | 322 | logWarning("Expected 5 commas. SMS[%d] DATA[%s]. Continuing ...", smsNumber, line.c_str()); |
mfiore | 16:1bc3e44d4746 | 323 | continue; |
mfiore | 16:1bc3e44d4746 | 324 | } |
mfiore | 16:1bc3e44d4746 | 325 | |
mfiore | 16:1bc3e44d4746 | 326 | sms.phoneNumber = vSmsParts[2]; |
mfiore | 16:1bc3e44d4746 | 327 | sms.timestamp = vSmsParts[4] + ", " + vSmsParts[5]; |
mfiore | 16:1bc3e44d4746 | 328 | } else if (type == MTSMC_EV3_IP || type == MTSMC_EV3 || type == MTSMC_C2_IP || type == MTSMC_C2) { |
mfiore | 16:1bc3e44d4746 | 329 | /* format for EV3 and EV3-IP radios |
mfiore | 16:1bc3e44d4746 | 330 | * <index>, <status>, <oa>, <callback>, <date> |
mfiore | 16:1bc3e44d4746 | 331 | * splitting on commas should give us 5 items |
mfiore | 16:1bc3e44d4746 | 332 | */ |
mfiore | 16:1bc3e44d4746 | 333 | if(vSmsParts.size() != 5) { |
mfiore | 16:1bc3e44d4746 | 334 | logWarning("Expected 4 commas. SMS[%d] DATA[%s]. Continuing ...", smsNumber, line.c_str()); |
mfiore | 16:1bc3e44d4746 | 335 | continue; |
mfiore | 16:1bc3e44d4746 | 336 | } |
mfiore | 16:1bc3e44d4746 | 337 | |
mfiore | 16:1bc3e44d4746 | 338 | sms.phoneNumber = vSmsParts[2]; |
mfiore | 16:1bc3e44d4746 | 339 | /* timestamp is in a nasty format |
mfiore | 16:1bc3e44d4746 | 340 | * YYYYMMDDHHMMSS |
mfiore | 16:1bc3e44d4746 | 341 | * nobody wants to try and decipher that, so format it nicely |
mfiore | 16:1bc3e44d4746 | 342 | * YY/MM/DD,HH:MM:SS |
mfiore | 16:1bc3e44d4746 | 343 | */ |
mfiore | 16:1bc3e44d4746 | 344 | string s = vSmsParts[4]; |
mfiore | 16:1bc3e44d4746 | 345 | sms.timestamp = s.substr(2,2) + "/" + s.substr(4,2) + "/" + s.substr(6,2) + ", " + s.substr(8,2) + ":" + s.substr(10,2) + ":" + s.substr(12,2); |
Mike Fiore |
7:0ee8e69a3e9c | 346 | } |
Mike Fiore |
7:0ee8e69a3e9c | 347 | |
Mike Fiore |
7:0ee8e69a3e9c | 348 | if(pos == std::string::npos) { |
Mike Fiore |
7:0ee8e69a3e9c | 349 | logWarning("Expected SMS body. SMS[%d]. Leaving ...", smsNumber); |
Mike Fiore |
7:0ee8e69a3e9c | 350 | break; |
Mike Fiore |
7:0ee8e69a3e9c | 351 | } |
Mike Fiore |
7:0ee8e69a3e9c | 352 | //Check for the start of the next SMS message |
Mike Fiore |
7:0ee8e69a3e9c | 353 | size_t bodyEnd = received.find("\r\n+CMGL: ", pos); |
Mike Fiore |
7:0ee8e69a3e9c | 354 | if(bodyEnd == std::string::npos) { |
Mike Fiore |
7:0ee8e69a3e9c | 355 | //This must be the last SMS message |
Mike Fiore |
7:0ee8e69a3e9c | 356 | bodyEnd = received.find("\r\n\r\nOK", pos); |
Mike Fiore |
7:0ee8e69a3e9c | 357 | } |
Mike Fiore |
7:0ee8e69a3e9c | 358 | |
Mike Fiore |
7:0ee8e69a3e9c | 359 | //Safety check that we found the boundary of this current SMS message |
Mike Fiore |
7:0ee8e69a3e9c | 360 | if(bodyEnd != std::string::npos) { |
Mike Fiore |
7:0ee8e69a3e9c | 361 | sms.message = received.substr(pos, bodyEnd - pos); |
Mike Fiore |
7:0ee8e69a3e9c | 362 | } else { |
Mike Fiore |
7:0ee8e69a3e9c | 363 | sms.message = received.substr(pos); |
Mike Fiore |
7:0ee8e69a3e9c | 364 | logWarning("Expected to find end of SMS list. SMS[%d] DATA[%s].", smsNumber, sms.message.c_str()); |
Mike Fiore |
7:0ee8e69a3e9c | 365 | } |
Mike Fiore |
7:0ee8e69a3e9c | 366 | vSms.push_back(sms); |
Mike Fiore |
7:0ee8e69a3e9c | 367 | pos = bodyEnd; |
Mike Fiore |
7:0ee8e69a3e9c | 368 | smsNumber++; |
Mike Fiore |
7:0ee8e69a3e9c | 369 | } |
Mike Fiore |
7:0ee8e69a3e9c | 370 | logInfo("Received %d SMS", smsNumber); |
Mike Fiore |
7:0ee8e69a3e9c | 371 | return vSms; |
Mike Fiore |
7:0ee8e69a3e9c | 372 | } |
Mike Fiore |
7:0ee8e69a3e9c | 373 | |
Mike Fiore |
7:0ee8e69a3e9c | 374 | Code Cellular::deleteOnlyReceivedReadSms() |
Mike Fiore |
7:0ee8e69a3e9c | 375 | { |
Mike Fiore |
7:0ee8e69a3e9c | 376 | return sendBasicCommand("AT+CMGD=1,1", 1000); |
Mike Fiore |
7:0ee8e69a3e9c | 377 | } |
Mike Fiore |
7:0ee8e69a3e9c | 378 | |
Mike Fiore |
7:0ee8e69a3e9c | 379 | Code Cellular::deleteAllReceivedSms() |
Mike Fiore |
7:0ee8e69a3e9c | 380 | { |
Mike Fiore |
7:0ee8e69a3e9c | 381 | return sendBasicCommand("AT+CMGD=1,4", 1000); |
Mike Fiore |
7:0ee8e69a3e9c | 382 | } |