Simple detection for LE910-NA1 modules
Fork of MTS-Cellular by
Cellular/Cellular.cpp@50:144099030d9b, 2014-08-05 (annotated)
- Committer:
- Vanger
- Date:
- Tue Aug 05 15:02:00 2014 +0000
- Revision:
- 50:144099030d9b
- Parent:
- 48:3f9d82915e83
- Child:
- 51:ffc556ba33f7
Removed comments to simplify reading code for sendCommand() under Cellular.cpp
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 | |
Vanger | 26:2b769ed8de4f | 135 | //Removed setAPN to be implemented in the individual cellular classes, |
Vanger | 26:2b769ed8de4f | 136 | //as UIP and EasyIP implement it in different ways. |
Mike Fiore |
8:2d7259d244d1 | 137 | |
Mike Fiore |
8:2d7259d244d1 | 138 | Code Cellular::setDns(const std::string& primary, const std::string& secondary) |
Mike Fiore |
8:2d7259d244d1 | 139 | { |
Mike Fiore |
8:2d7259d244d1 | 140 | return sendBasicCommand("AT#DNS=1," + primary + "," + secondary, 1000); |
Mike Fiore |
8:2d7259d244d1 | 141 | } |
Mike Fiore |
8:2d7259d244d1 | 142 | |
Mike Fiore |
7:0ee8e69a3e9c | 143 | Code Cellular::sendSMS(const Sms& sms) |
Mike Fiore |
7:0ee8e69a3e9c | 144 | { |
Mike Fiore |
7:0ee8e69a3e9c | 145 | return sendSMS(sms.phoneNumber, sms.message); |
Mike Fiore |
7:0ee8e69a3e9c | 146 | } |
Mike Fiore |
7:0ee8e69a3e9c | 147 | |
Mike Fiore |
10:c188cc05aed5 | 148 | Code Cellular::sendBasicCommand(const std::string& command, unsigned int timeoutMillis, char esc) |
Mike Fiore |
10:c188cc05aed5 | 149 | { |
Mike Fiore |
10:c188cc05aed5 | 150 | if(socketOpened) { |
Mike Fiore |
10:c188cc05aed5 | 151 | logError("socket is open. Can not send AT commands"); |
mfiore | 18:fa0d8120f81f | 152 | return MTS_ERROR; |
Mike Fiore |
10:c188cc05aed5 | 153 | } |
Mike Fiore |
10:c188cc05aed5 | 154 | |
Mike Fiore |
10:c188cc05aed5 | 155 | string response = sendCommand(command, timeoutMillis, esc); |
Mike Fiore |
10:c188cc05aed5 | 156 | if (response.size() == 0) { |
mfiore | 18:fa0d8120f81f | 157 | return MTS_NO_RESPONSE; |
Mike Fiore |
10:c188cc05aed5 | 158 | } else if (response.find("OK") != string::npos) { |
mfiore | 18:fa0d8120f81f | 159 | return MTS_SUCCESS; |
Mike Fiore |
10:c188cc05aed5 | 160 | } else if (response.find("ERROR") != string::npos) { |
mfiore | 18:fa0d8120f81f | 161 | return MTS_ERROR; |
Mike Fiore |
10:c188cc05aed5 | 162 | } else { |
mfiore | 18:fa0d8120f81f | 163 | return MTS_FAILURE; |
Mike Fiore |
10:c188cc05aed5 | 164 | } |
Mike Fiore |
10:c188cc05aed5 | 165 | } |
Mike Fiore |
10:c188cc05aed5 | 166 | |
Mike Fiore |
10:c188cc05aed5 | 167 | string Cellular::sendCommand(const std::string& command, unsigned int timeoutMillis, char esc) |
Mike Fiore |
10:c188cc05aed5 | 168 | { |
Mike Fiore |
10:c188cc05aed5 | 169 | if(io == NULL) { |
Mike Fiore |
10:c188cc05aed5 | 170 | logError("MTSBufferedIO not set"); |
Mike Fiore |
10:c188cc05aed5 | 171 | return ""; |
Mike Fiore |
10:c188cc05aed5 | 172 | } |
Mike Fiore |
10:c188cc05aed5 | 173 | if(socketOpened) { |
Mike Fiore |
10:c188cc05aed5 | 174 | logError("socket is open. Can not send AT commands"); |
Mike Fiore |
10:c188cc05aed5 | 175 | return ""; |
Mike Fiore |
10:c188cc05aed5 | 176 | } |
Mike Fiore |
10:c188cc05aed5 | 177 | |
Mike Fiore |
10:c188cc05aed5 | 178 | io->rxClear(); |
Mike Fiore |
10:c188cc05aed5 | 179 | io->txClear(); |
Mike Fiore |
10:c188cc05aed5 | 180 | std::string result; |
Mike Fiore |
10:c188cc05aed5 | 181 | |
Mike Fiore |
10:c188cc05aed5 | 182 | //Attempt to write command |
Mike Fiore |
10:c188cc05aed5 | 183 | if(io->write(command.data(), command.size(), timeoutMillis) != command.size()) { |
Mike Fiore |
10:c188cc05aed5 | 184 | //Failed to write command |
Mike Fiore |
10:c188cc05aed5 | 185 | if (command != "AT" && command != "at") { |
Mike Fiore |
10:c188cc05aed5 | 186 | logError("failed to send command to radio within %d milliseconds", timeoutMillis); |
Mike Fiore |
10:c188cc05aed5 | 187 | } |
Mike Fiore |
10:c188cc05aed5 | 188 | return ""; |
Mike Fiore |
10:c188cc05aed5 | 189 | } |
Mike Fiore |
10:c188cc05aed5 | 190 | |
Mike Fiore |
10:c188cc05aed5 | 191 | //Send Escape Character |
Mike Fiore |
10:c188cc05aed5 | 192 | if (esc != 0x00) { |
Mike Fiore |
10:c188cc05aed5 | 193 | if(io->write(esc, timeoutMillis) != 1) { |
Mike Fiore |
10:c188cc05aed5 | 194 | if (command != "AT" && command != "at") { |
Mike Fiore |
10:c188cc05aed5 | 195 | logError("failed to send character '%c' (0x%02X) to radio within %d milliseconds", esc, esc, timeoutMillis); |
Mike Fiore |
10:c188cc05aed5 | 196 | } |
Mike Fiore |
10:c188cc05aed5 | 197 | return ""; |
Mike Fiore |
10:c188cc05aed5 | 198 | } |
Mike Fiore |
10:c188cc05aed5 | 199 | } |
Vanger | 41:8b9b5098696f | 200 | mbed::Timer tmr; |
Mike Fiore |
10:c188cc05aed5 | 201 | char tmp[256]; |
Mike Fiore |
10:c188cc05aed5 | 202 | tmp[255] = 0; |
Mike Fiore |
10:c188cc05aed5 | 203 | bool done = false; |
Vanger | 41:8b9b5098696f | 204 | tmr.start(); |
Mike Fiore |
10:c188cc05aed5 | 205 | do { |
Vanger | 41:8b9b5098696f | 206 | wait(0.05); |
Vanger | 35:257eb41405e1 | 207 | |
Mike Fiore |
10:c188cc05aed5 | 208 | //Make a non-blocking read call by passing timeout of zero |
Mike Fiore |
10:c188cc05aed5 | 209 | int size = io->read(tmp,255,0); //1 less than allocated (timeout is instant) |
Mike Fiore |
10:c188cc05aed5 | 210 | if(size > 0) { |
Mike Fiore |
10:c188cc05aed5 | 211 | result.append(tmp, size); |
Mike Fiore |
10:c188cc05aed5 | 212 | } |
Vanger | 41:8b9b5098696f | 213 | |
Vanger | 45:b9ead235ab7c | 214 | //Check for a response to signify the completion of the AT command |
Vanger | 41:8b9b5098696f | 215 | //OK, ERROR, CONNECT are the 3 most likely responses |
Vanger | 41:8b9b5098696f | 216 | if(result.size() > (command.size() + 2)) { |
Vanger | 45:b9ead235ab7c | 217 | if(result.find("OK") != std::string::npos) { |
Vanger | 45:b9ead235ab7c | 218 | done = true; |
Vanger | 45:b9ead235ab7c | 219 | } else if (result.find("ERROR") != std::string::npos) { |
Vanger | 45:b9ead235ab7c | 220 | done = true; |
Vanger | 45:b9ead235ab7c | 221 | } else if (result.find("NO CARRIER") != std::string::npos) { |
Vanger | 45:b9ead235ab7c | 222 | done = true; |
Vanger | 45:b9ead235ab7c | 223 | } else if (result.find("RING") != std::string::npos) { |
Vanger | 45:b9ead235ab7c | 224 | done = true; |
Vanger | 45:b9ead235ab7c | 225 | } else if (result.find("BUSY") != std::string::npos) { |
Vanger | 45:b9ead235ab7c | 226 | done = true; |
Vanger | 45:b9ead235ab7c | 227 | } else if (result.find("NO ANSWER") != std::string::npos) { |
Vanger | 45:b9ead235ab7c | 228 | done = true; |
Vanger | 45:b9ead235ab7c | 229 | } |
Vanger | 45:b9ead235ab7c | 230 | |
Vanger | 48:3f9d82915e83 | 231 | if (command.find("AT+CMGS=") != string::npos) { |
Vanger | 48:3f9d82915e83 | 232 | if (result.find('>') != string::npos) { |
Vanger | 48:3f9d82915e83 | 233 | done = true; |
Vanger | 48:3f9d82915e83 | 234 | } |
Vanger | 48:3f9d82915e83 | 235 | } |
Vanger | 48:3f9d82915e83 | 236 | |
Vanger | 45:b9ead235ab7c | 237 | if(type == MTSMC_H5 || type == MTSMC_G3 || type == MTSMC_EV3 || type == MTSMC_C2) { |
Vanger | 45:b9ead235ab7c | 238 | if (result.find("CONNECT") != std::string::npos) { |
Vanger | 45:b9ead235ab7c | 239 | //Could add socketOpened flag check here if CONNECT is found |
Vanger | 45:b9ead235ab7c | 240 | done = true; |
Vanger | 45:b9ead235ab7c | 241 | } else if(result.find("NO DIALTONE") != std::string::npos) { |
Vanger | 45:b9ead235ab7c | 242 | done = true; |
Vanger | 45:b9ead235ab7c | 243 | } |
Vanger | 45:b9ead235ab7c | 244 | } else if (type == MTSMC_H5_IP || type == MTSMC_EV3_IP || type == MTSMC_C2_IP) { |
Vanger | 48:3f9d82915e83 | 245 | if (result.find("Ok_Info_") != std::string::npos) { |
Vanger | 45:b9ead235ab7c | 246 | done = true; |
Vanger | 45:b9ead235ab7c | 247 | } |
Vanger | 45:b9ead235ab7c | 248 | } |
Mike Fiore |
10:c188cc05aed5 | 249 | } |
Vanger | 41:8b9b5098696f | 250 | |
Vanger | 41:8b9b5098696f | 251 | if(tmr.read_ms() >= timeoutMillis) { |
Mike Fiore |
10:c188cc05aed5 | 252 | if (command != "AT" && command != "at") { |
Mike Fiore |
10:c188cc05aed5 | 253 | logWarning("sendCommand [%s] timed out after %d milliseconds", command.c_str(), timeoutMillis); |
Mike Fiore |
10:c188cc05aed5 | 254 | } |
Mike Fiore |
10:c188cc05aed5 | 255 | done = true; |
Mike Fiore |
10:c188cc05aed5 | 256 | } |
Mike Fiore |
10:c188cc05aed5 | 257 | } while (!done); |
Mike Fiore |
10:c188cc05aed5 | 258 | |
Mike Fiore |
10:c188cc05aed5 | 259 | return result; |
Mike Fiore |
10:c188cc05aed5 | 260 | } |
Mike Fiore |
10:c188cc05aed5 | 261 | |
Mike Fiore |
7:0ee8e69a3e9c | 262 | Code Cellular::sendSMS(const std::string& phoneNumber, const std::string& message) |
Mike Fiore |
7:0ee8e69a3e9c | 263 | { |
mfiore | 16:1bc3e44d4746 | 264 | string csmp; |
mfiore | 16:1bc3e44d4746 | 265 | |
mfiore | 16:1bc3e44d4746 | 266 | if (type == MTSMC_H5_IP || type == MTSMC_H5) { |
mfiore | 16:1bc3e44d4746 | 267 | csmp = "AT+CSMP=17,167,0,0"; |
mfiore | 16:1bc3e44d4746 | 268 | } else if (type == MTSMC_EV3_IP || type == MTSMC_EV3 || type == MTSMC_C2_IP || type == MTSMC_C2) { |
mfiore | 16:1bc3e44d4746 | 269 | csmp = "AT+CSMP=,4098,0,2"; |
mfiore | 16:1bc3e44d4746 | 270 | } else if (type == MTSMC_G3) { |
mfiore | 16:1bc3e44d4746 | 271 | } else { |
mfiore | 16:1bc3e44d4746 | 272 | logError("unknown radio type [%d]", type); |
mfiore | 18:fa0d8120f81f | 273 | return MTS_FAILURE; |
mfiore | 16:1bc3e44d4746 | 274 | } |
mfiore | 16:1bc3e44d4746 | 275 | |
Mike Fiore |
7:0ee8e69a3e9c | 276 | Code code = sendBasicCommand("AT+CMGF=1", 1000); |
mfiore | 18:fa0d8120f81f | 277 | if (code != MTS_SUCCESS) { |
mfiore | 16:1bc3e44d4746 | 278 | logError("CMGF failed"); |
mfiore | 16:1bc3e44d4746 | 279 | return code; |
mfiore | 16:1bc3e44d4746 | 280 | } |
mfiore | 16:1bc3e44d4746 | 281 | code = sendBasicCommand(csmp, 1000); |
mfiore | 18:fa0d8120f81f | 282 | if (code != MTS_SUCCESS) { |
mfiore | 16:1bc3e44d4746 | 283 | logError("CSMP failed [%s]", getRadioNames(type).c_str()); |
Mike Fiore |
7:0ee8e69a3e9c | 284 | return code; |
Mike Fiore |
7:0ee8e69a3e9c | 285 | } |
Vanger | 35:257eb41405e1 | 286 | string cmd = "AT+CMGS=\""; |
Vanger | 38:b2088faa8bfd | 287 | cmd.append("+"); |
Mike Fiore |
7:0ee8e69a3e9c | 288 | cmd.append(phoneNumber); |
Vanger | 36:948d06b3e23c | 289 | cmd.append("\",145"); |
mfiore | 23:8333593dd86f | 290 | for (int i = 0; i < 5; i++) { |
Vanger | 48:3f9d82915e83 | 291 | string response1 = sendCommand(cmd, 2000); |
mfiore | 23:8333593dd86f | 292 | if (response1.find('>') != string::npos) { |
mfiore | 23:8333593dd86f | 293 | break; |
mfiore | 23:8333593dd86f | 294 | } |
mfiore | 23:8333593dd86f | 295 | if (i >= 5) { |
mfiore | 23:8333593dd86f | 296 | logError("CMGS phone number failed"); |
mfiore | 23:8333593dd86f | 297 | return MTS_NO_RESPONSE; |
mfiore | 23:8333593dd86f | 298 | } |
mfiore | 23:8333593dd86f | 299 | |
mfiore | 23:8333593dd86f | 300 | wait(1); |
Mike Fiore |
7:0ee8e69a3e9c | 301 | } |
Mike Fiore |
7:0ee8e69a3e9c | 302 | wait(.2); |
Mike Fiore |
7:0ee8e69a3e9c | 303 | string response2 = sendCommand(message, 4000, CTRL_Z); |
mfiore | 16:1bc3e44d4746 | 304 | logInfo("SMS Response: [%s]", response2.c_str()); |
Vanger | 36:948d06b3e23c | 305 | if ((response2.find("+CMGS:") == string::npos) || (response2.find("ERROR") != std::string::npos)) { |
mfiore | 16:1bc3e44d4746 | 306 | logError("CMGS message failed"); |
mfiore | 18:fa0d8120f81f | 307 | return MTS_FAILURE; |
Mike Fiore |
7:0ee8e69a3e9c | 308 | } |
mfiore | 18:fa0d8120f81f | 309 | return MTS_SUCCESS; |
Mike Fiore |
7:0ee8e69a3e9c | 310 | } |
Mike Fiore |
7:0ee8e69a3e9c | 311 | |
Mike Fiore |
7:0ee8e69a3e9c | 312 | std::vector<Cellular::Sms> Cellular::getReceivedSms() |
Mike Fiore |
7:0ee8e69a3e9c | 313 | { |
Mike Fiore |
7:0ee8e69a3e9c | 314 | int smsNumber = 0; |
Mike Fiore |
7:0ee8e69a3e9c | 315 | std::vector<Sms> vSms; |
mfiore | 16:1bc3e44d4746 | 316 | std::string received; |
mfiore | 16:1bc3e44d4746 | 317 | size_t pos; |
mfiore | 16:1bc3e44d4746 | 318 | |
mfiore | 16:1bc3e44d4746 | 319 | Code code = sendBasicCommand("AT+CMGF=1", 1000); |
mfiore | 18:fa0d8120f81f | 320 | if (code != MTS_SUCCESS) { |
mfiore | 16:1bc3e44d4746 | 321 | logError("CMGF failed"); |
mfiore | 16:1bc3e44d4746 | 322 | return vSms; |
mfiore | 16:1bc3e44d4746 | 323 | } |
mfiore | 16:1bc3e44d4746 | 324 | |
Vanger | 35:257eb41405e1 | 325 | received = sendCommand("AT+CMGL=\"ALL\"", 5000); |
mfiore | 16:1bc3e44d4746 | 326 | pos = received.find("+CMGL: "); |
Mike Fiore |
7:0ee8e69a3e9c | 327 | |
Mike Fiore |
7:0ee8e69a3e9c | 328 | while (pos != std::string::npos) { |
Mike Fiore |
7:0ee8e69a3e9c | 329 | Cellular::Sms sms; |
Mike Fiore |
7:0ee8e69a3e9c | 330 | std::string line(Text::getLine(received, pos, pos)); |
Mike Fiore |
7:0ee8e69a3e9c | 331 | if(line.find("+CMGL: ") == std::string::npos) { |
Mike Fiore |
7:0ee8e69a3e9c | 332 | continue; |
Mike Fiore |
7:0ee8e69a3e9c | 333 | } |
Mike Fiore |
7:0ee8e69a3e9c | 334 | //Start of SMS message |
Mike Fiore |
7:0ee8e69a3e9c | 335 | std::vector<std::string> vSmsParts = Text::split(line, ','); |
mfiore | 16:1bc3e44d4746 | 336 | if (type == MTSMC_H5_IP || type == MTSMC_H5) { |
mfiore | 16:1bc3e44d4746 | 337 | /* format for H5 and H5-IP radios |
mfiore | 16:1bc3e44d4746 | 338 | * <index>, <status>, <oa>, <alpha>, <scts> |
mfiore | 16:1bc3e44d4746 | 339 | * scts contains a comma, so splitting on commas should give us 6 items |
mfiore | 16:1bc3e44d4746 | 340 | */ |
mfiore | 16:1bc3e44d4746 | 341 | if(vSmsParts.size() != 6) { |
mfiore | 16:1bc3e44d4746 | 342 | logWarning("Expected 5 commas. SMS[%d] DATA[%s]. Continuing ...", smsNumber, line.c_str()); |
mfiore | 16:1bc3e44d4746 | 343 | continue; |
mfiore | 16:1bc3e44d4746 | 344 | } |
mfiore | 16:1bc3e44d4746 | 345 | |
mfiore | 16:1bc3e44d4746 | 346 | sms.phoneNumber = vSmsParts[2]; |
mfiore | 16:1bc3e44d4746 | 347 | sms.timestamp = vSmsParts[4] + ", " + vSmsParts[5]; |
mfiore | 16:1bc3e44d4746 | 348 | } else if (type == MTSMC_EV3_IP || type == MTSMC_EV3 || type == MTSMC_C2_IP || type == MTSMC_C2) { |
mfiore | 16:1bc3e44d4746 | 349 | /* format for EV3 and EV3-IP radios |
mfiore | 16:1bc3e44d4746 | 350 | * <index>, <status>, <oa>, <callback>, <date> |
mfiore | 16:1bc3e44d4746 | 351 | * splitting on commas should give us 5 items |
mfiore | 16:1bc3e44d4746 | 352 | */ |
mfiore | 16:1bc3e44d4746 | 353 | if(vSmsParts.size() != 5) { |
mfiore | 16:1bc3e44d4746 | 354 | logWarning("Expected 4 commas. SMS[%d] DATA[%s]. Continuing ...", smsNumber, line.c_str()); |
mfiore | 16:1bc3e44d4746 | 355 | continue; |
mfiore | 16:1bc3e44d4746 | 356 | } |
mfiore | 16:1bc3e44d4746 | 357 | |
mfiore | 16:1bc3e44d4746 | 358 | sms.phoneNumber = vSmsParts[2]; |
mfiore | 16:1bc3e44d4746 | 359 | /* timestamp is in a nasty format |
mfiore | 16:1bc3e44d4746 | 360 | * YYYYMMDDHHMMSS |
mfiore | 16:1bc3e44d4746 | 361 | * nobody wants to try and decipher that, so format it nicely |
mfiore | 16:1bc3e44d4746 | 362 | * YY/MM/DD,HH:MM:SS |
mfiore | 16:1bc3e44d4746 | 363 | */ |
mfiore | 16:1bc3e44d4746 | 364 | string s = vSmsParts[4]; |
mfiore | 16:1bc3e44d4746 | 365 | 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 | 366 | } |
Mike Fiore |
7:0ee8e69a3e9c | 367 | |
Mike Fiore |
7:0ee8e69a3e9c | 368 | if(pos == std::string::npos) { |
Mike Fiore |
7:0ee8e69a3e9c | 369 | logWarning("Expected SMS body. SMS[%d]. Leaving ...", smsNumber); |
Mike Fiore |
7:0ee8e69a3e9c | 370 | break; |
Mike Fiore |
7:0ee8e69a3e9c | 371 | } |
Mike Fiore |
7:0ee8e69a3e9c | 372 | //Check for the start of the next SMS message |
Vanger | 48:3f9d82915e83 | 373 | size_t bodyEnd = received.find("\r\n+CMGL:", pos); |
Mike Fiore |
7:0ee8e69a3e9c | 374 | if(bodyEnd == std::string::npos) { |
Mike Fiore |
7:0ee8e69a3e9c | 375 | //This must be the last SMS message |
Mike Fiore |
7:0ee8e69a3e9c | 376 | bodyEnd = received.find("\r\n\r\nOK", pos); |
Mike Fiore |
7:0ee8e69a3e9c | 377 | } |
Mike Fiore |
7:0ee8e69a3e9c | 378 | //Safety check that we found the boundary of this current SMS message |
Mike Fiore |
7:0ee8e69a3e9c | 379 | if(bodyEnd != std::string::npos) { |
Mike Fiore |
7:0ee8e69a3e9c | 380 | sms.message = received.substr(pos, bodyEnd - pos); |
Mike Fiore |
7:0ee8e69a3e9c | 381 | } else { |
Mike Fiore |
7:0ee8e69a3e9c | 382 | sms.message = received.substr(pos); |
Mike Fiore |
7:0ee8e69a3e9c | 383 | logWarning("Expected to find end of SMS list. SMS[%d] DATA[%s].", smsNumber, sms.message.c_str()); |
Mike Fiore |
7:0ee8e69a3e9c | 384 | } |
Mike Fiore |
7:0ee8e69a3e9c | 385 | vSms.push_back(sms); |
Mike Fiore |
7:0ee8e69a3e9c | 386 | pos = bodyEnd; |
Mike Fiore |
7:0ee8e69a3e9c | 387 | smsNumber++; |
Mike Fiore |
7:0ee8e69a3e9c | 388 | } |
Mike Fiore |
7:0ee8e69a3e9c | 389 | logInfo("Received %d SMS", smsNumber); |
Mike Fiore |
7:0ee8e69a3e9c | 390 | return vSms; |
Mike Fiore |
7:0ee8e69a3e9c | 391 | } |
Mike Fiore |
7:0ee8e69a3e9c | 392 | |
Mike Fiore |
7:0ee8e69a3e9c | 393 | Code Cellular::deleteOnlyReceivedReadSms() |
Mike Fiore |
7:0ee8e69a3e9c | 394 | { |
Mike Fiore |
7:0ee8e69a3e9c | 395 | return sendBasicCommand("AT+CMGD=1,1", 1000); |
Mike Fiore |
7:0ee8e69a3e9c | 396 | } |
Mike Fiore |
7:0ee8e69a3e9c | 397 | |
Mike Fiore |
7:0ee8e69a3e9c | 398 | Code Cellular::deleteAllReceivedSms() |
Mike Fiore |
7:0ee8e69a3e9c | 399 | { |
Mike Fiore |
7:0ee8e69a3e9c | 400 | return sendBasicCommand("AT+CMGD=1,4", 1000); |
Mike Fiore |
7:0ee8e69a3e9c | 401 | } |