Simple detection for LE910-NA1 modules
Fork of MTS-Cellular by
Cellular/Cellular.cpp@26:2b769ed8de4f, 2014-06-26 (annotated)
- Committer:
- Vanger
- Date:
- Thu Jun 26 21:12:37 2014 +0000
- Revision:
- 26:2b769ed8de4f
- Parent:
- 23:8333593dd86f
- Child:
- 35:257eb41405e1
Made setApn() a pure virtual in Cellular.; Implemented setApn in both UIP and EasyIP; Implemented connect(), disconnect(), and ping() in EasyIP; Uncommented cell instantiation for EasyIP type models in CellularFactory
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 | } |
Mike Fiore |
10:c188cc05aed5 | 200 | |
Mike Fiore |
10:c188cc05aed5 | 201 | int timer = 0; |
Mike Fiore |
10:c188cc05aed5 | 202 | size_t previous = 0; |
Mike Fiore |
10:c188cc05aed5 | 203 | char tmp[256]; |
Mike Fiore |
10:c188cc05aed5 | 204 | tmp[255] = 0; |
Mike Fiore |
10:c188cc05aed5 | 205 | bool started = !echoMode; |
Mike Fiore |
10:c188cc05aed5 | 206 | bool done = false; |
Mike Fiore |
10:c188cc05aed5 | 207 | do { |
Mike Fiore |
10:c188cc05aed5 | 208 | wait(0.1); |
Mike Fiore |
10:c188cc05aed5 | 209 | timer += 100; |
Mike Fiore |
10:c188cc05aed5 | 210 | |
Mike Fiore |
10:c188cc05aed5 | 211 | previous = result.size(); |
Mike Fiore |
10:c188cc05aed5 | 212 | //Make a non-blocking read call by passing timeout of zero |
Mike Fiore |
10:c188cc05aed5 | 213 | int size = io->read(tmp,255,0); //1 less than allocated (timeout is instant) |
Mike Fiore |
10:c188cc05aed5 | 214 | if(size > 0) { |
Mike Fiore |
10:c188cc05aed5 | 215 | result.append(tmp, size); |
Mike Fiore |
10:c188cc05aed5 | 216 | } |
Mike Fiore |
10:c188cc05aed5 | 217 | if(!started) { |
Mike Fiore |
10:c188cc05aed5 | 218 | //In Echo Mode (Command will have echo'd + 2 characters for \r\n) |
Mike Fiore |
10:c188cc05aed5 | 219 | if(result.size() > command.size() + 2) { |
Mike Fiore |
10:c188cc05aed5 | 220 | started = true; |
Mike Fiore |
10:c188cc05aed5 | 221 | } |
Mike Fiore |
10:c188cc05aed5 | 222 | } else { |
Mike Fiore |
10:c188cc05aed5 | 223 | done = (result.size() == previous); |
Mike Fiore |
10:c188cc05aed5 | 224 | } |
Mike Fiore |
10:c188cc05aed5 | 225 | if(timer >= timeoutMillis) { |
Mike Fiore |
10:c188cc05aed5 | 226 | if (command != "AT" && command != "at") { |
Mike Fiore |
10:c188cc05aed5 | 227 | logWarning("sendCommand [%s] timed out after %d milliseconds", command.c_str(), timeoutMillis); |
Mike Fiore |
10:c188cc05aed5 | 228 | } |
Mike Fiore |
10:c188cc05aed5 | 229 | done = true; |
Mike Fiore |
10:c188cc05aed5 | 230 | } |
Mike Fiore |
10:c188cc05aed5 | 231 | } while (!done); |
Mike Fiore |
10:c188cc05aed5 | 232 | |
Mike Fiore |
10:c188cc05aed5 | 233 | return result; |
Mike Fiore |
10:c188cc05aed5 | 234 | } |
Mike Fiore |
10:c188cc05aed5 | 235 | |
Mike Fiore |
7:0ee8e69a3e9c | 236 | Code Cellular::sendSMS(const std::string& phoneNumber, const std::string& message) |
Mike Fiore |
7:0ee8e69a3e9c | 237 | { |
mfiore | 16:1bc3e44d4746 | 238 | string csmp; |
mfiore | 16:1bc3e44d4746 | 239 | |
mfiore | 16:1bc3e44d4746 | 240 | if (type == MTSMC_H5_IP || type == MTSMC_H5) { |
mfiore | 16:1bc3e44d4746 | 241 | csmp = "AT+CSMP=17,167,0,0"; |
mfiore | 16:1bc3e44d4746 | 242 | } else if (type == MTSMC_EV3_IP || type == MTSMC_EV3 || type == MTSMC_C2_IP || type == MTSMC_C2) { |
mfiore | 16:1bc3e44d4746 | 243 | csmp = "AT+CSMP=,4098,0,2"; |
mfiore | 16:1bc3e44d4746 | 244 | } else if (type == MTSMC_G3) { |
mfiore | 16:1bc3e44d4746 | 245 | } else { |
mfiore | 16:1bc3e44d4746 | 246 | logError("unknown radio type [%d]", type); |
mfiore | 18:fa0d8120f81f | 247 | return MTS_FAILURE; |
mfiore | 16:1bc3e44d4746 | 248 | } |
mfiore | 16:1bc3e44d4746 | 249 | |
Mike Fiore |
7:0ee8e69a3e9c | 250 | Code code = sendBasicCommand("AT+CMGF=1", 1000); |
mfiore | 18:fa0d8120f81f | 251 | if (code != MTS_SUCCESS) { |
mfiore | 16:1bc3e44d4746 | 252 | logError("CMGF failed"); |
mfiore | 16:1bc3e44d4746 | 253 | return code; |
mfiore | 16:1bc3e44d4746 | 254 | } |
mfiore | 16:1bc3e44d4746 | 255 | code = sendBasicCommand(csmp, 1000); |
mfiore | 18:fa0d8120f81f | 256 | if (code != MTS_SUCCESS) { |
mfiore | 16:1bc3e44d4746 | 257 | logError("CSMP failed [%s]", getRadioNames(type).c_str()); |
Mike Fiore |
7:0ee8e69a3e9c | 258 | return code; |
Mike Fiore |
7:0ee8e69a3e9c | 259 | } |
Mike Fiore |
7:0ee8e69a3e9c | 260 | string cmd = "AT+CMGS=\"+"; |
Mike Fiore |
7:0ee8e69a3e9c | 261 | cmd.append(phoneNumber); |
mfiore | 16:1bc3e44d4746 | 262 | cmd.append("\",145"); |
mfiore | 23:8333593dd86f | 263 | for (int i = 0; i < 5; i++) { |
mfiore | 23:8333593dd86f | 264 | string response1 = sendCommand(cmd, 1000); |
mfiore | 23:8333593dd86f | 265 | if (response1.find('>') != string::npos) { |
mfiore | 23:8333593dd86f | 266 | break; |
mfiore | 23:8333593dd86f | 267 | } |
mfiore | 23:8333593dd86f | 268 | if (i >= 5) { |
mfiore | 23:8333593dd86f | 269 | logError("CMGS phone number failed"); |
mfiore | 23:8333593dd86f | 270 | return MTS_NO_RESPONSE; |
mfiore | 23:8333593dd86f | 271 | } |
mfiore | 23:8333593dd86f | 272 | |
mfiore | 23:8333593dd86f | 273 | wait(1); |
Mike Fiore |
7:0ee8e69a3e9c | 274 | } |
Mike Fiore |
7:0ee8e69a3e9c | 275 | wait(.2); |
Mike Fiore |
7:0ee8e69a3e9c | 276 | string response2 = sendCommand(message, 4000, CTRL_Z); |
mfiore | 16:1bc3e44d4746 | 277 | logInfo("SMS Response: [%s]", response2.c_str()); |
Mike Fiore |
7:0ee8e69a3e9c | 278 | if (response2.find("+CMGS:") == string::npos) { |
mfiore | 16:1bc3e44d4746 | 279 | logError("CMGS message failed"); |
mfiore | 18:fa0d8120f81f | 280 | return MTS_FAILURE; |
Mike Fiore |
7:0ee8e69a3e9c | 281 | } |
mfiore | 18:fa0d8120f81f | 282 | return MTS_SUCCESS; |
Mike Fiore |
7:0ee8e69a3e9c | 283 | } |
Mike Fiore |
7:0ee8e69a3e9c | 284 | |
Mike Fiore |
7:0ee8e69a3e9c | 285 | std::vector<Cellular::Sms> Cellular::getReceivedSms() |
Mike Fiore |
7:0ee8e69a3e9c | 286 | { |
Mike Fiore |
7:0ee8e69a3e9c | 287 | int smsNumber = 0; |
Mike Fiore |
7:0ee8e69a3e9c | 288 | std::vector<Sms> vSms; |
mfiore | 16:1bc3e44d4746 | 289 | std::string received; |
mfiore | 16:1bc3e44d4746 | 290 | size_t pos; |
mfiore | 16:1bc3e44d4746 | 291 | |
mfiore | 16:1bc3e44d4746 | 292 | Code code = sendBasicCommand("AT+CMGF=1", 1000); |
mfiore | 18:fa0d8120f81f | 293 | if (code != MTS_SUCCESS) { |
mfiore | 16:1bc3e44d4746 | 294 | logError("CMGF failed"); |
mfiore | 16:1bc3e44d4746 | 295 | return vSms; |
mfiore | 16:1bc3e44d4746 | 296 | } |
mfiore | 16:1bc3e44d4746 | 297 | |
mfiore | 16:1bc3e44d4746 | 298 | received = sendCommand("AT+CMGL=\"ALL\"", 4000); |
mfiore | 16:1bc3e44d4746 | 299 | pos = received.find("+CMGL: "); |
Mike Fiore |
7:0ee8e69a3e9c | 300 | |
Mike Fiore |
7:0ee8e69a3e9c | 301 | while (pos != std::string::npos) { |
Mike Fiore |
7:0ee8e69a3e9c | 302 | Cellular::Sms sms; |
Mike Fiore |
7:0ee8e69a3e9c | 303 | std::string line(Text::getLine(received, pos, pos)); |
Mike Fiore |
7:0ee8e69a3e9c | 304 | if(line.find("+CMGL: ") == std::string::npos) { |
Mike Fiore |
7:0ee8e69a3e9c | 305 | continue; |
Mike Fiore |
7:0ee8e69a3e9c | 306 | } |
Mike Fiore |
7:0ee8e69a3e9c | 307 | |
Mike Fiore |
7:0ee8e69a3e9c | 308 | //Start of SMS message |
Mike Fiore |
7:0ee8e69a3e9c | 309 | std::vector<std::string> vSmsParts = Text::split(line, ','); |
mfiore | 16:1bc3e44d4746 | 310 | if (type == MTSMC_H5_IP || type == MTSMC_H5) { |
mfiore | 16:1bc3e44d4746 | 311 | /* format for H5 and H5-IP radios |
mfiore | 16:1bc3e44d4746 | 312 | * <index>, <status>, <oa>, <alpha>, <scts> |
mfiore | 16:1bc3e44d4746 | 313 | * scts contains a comma, so splitting on commas should give us 6 items |
mfiore | 16:1bc3e44d4746 | 314 | */ |
mfiore | 16:1bc3e44d4746 | 315 | if(vSmsParts.size() != 6) { |
mfiore | 16:1bc3e44d4746 | 316 | logWarning("Expected 5 commas. SMS[%d] DATA[%s]. Continuing ...", smsNumber, line.c_str()); |
mfiore | 16:1bc3e44d4746 | 317 | continue; |
mfiore | 16:1bc3e44d4746 | 318 | } |
mfiore | 16:1bc3e44d4746 | 319 | |
mfiore | 16:1bc3e44d4746 | 320 | sms.phoneNumber = vSmsParts[2]; |
mfiore | 16:1bc3e44d4746 | 321 | sms.timestamp = vSmsParts[4] + ", " + vSmsParts[5]; |
mfiore | 16:1bc3e44d4746 | 322 | } else if (type == MTSMC_EV3_IP || type == MTSMC_EV3 || type == MTSMC_C2_IP || type == MTSMC_C2) { |
mfiore | 16:1bc3e44d4746 | 323 | /* format for EV3 and EV3-IP radios |
mfiore | 16:1bc3e44d4746 | 324 | * <index>, <status>, <oa>, <callback>, <date> |
mfiore | 16:1bc3e44d4746 | 325 | * splitting on commas should give us 5 items |
mfiore | 16:1bc3e44d4746 | 326 | */ |
mfiore | 16:1bc3e44d4746 | 327 | if(vSmsParts.size() != 5) { |
mfiore | 16:1bc3e44d4746 | 328 | logWarning("Expected 4 commas. SMS[%d] DATA[%s]. Continuing ...", smsNumber, line.c_str()); |
mfiore | 16:1bc3e44d4746 | 329 | continue; |
mfiore | 16:1bc3e44d4746 | 330 | } |
mfiore | 16:1bc3e44d4746 | 331 | |
mfiore | 16:1bc3e44d4746 | 332 | sms.phoneNumber = vSmsParts[2]; |
mfiore | 16:1bc3e44d4746 | 333 | /* timestamp is in a nasty format |
mfiore | 16:1bc3e44d4746 | 334 | * YYYYMMDDHHMMSS |
mfiore | 16:1bc3e44d4746 | 335 | * nobody wants to try and decipher that, so format it nicely |
mfiore | 16:1bc3e44d4746 | 336 | * YY/MM/DD,HH:MM:SS |
mfiore | 16:1bc3e44d4746 | 337 | */ |
mfiore | 16:1bc3e44d4746 | 338 | string s = vSmsParts[4]; |
mfiore | 16:1bc3e44d4746 | 339 | 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 | 340 | } |
Mike Fiore |
7:0ee8e69a3e9c | 341 | |
Mike Fiore |
7:0ee8e69a3e9c | 342 | if(pos == std::string::npos) { |
Mike Fiore |
7:0ee8e69a3e9c | 343 | logWarning("Expected SMS body. SMS[%d]. Leaving ...", smsNumber); |
Mike Fiore |
7:0ee8e69a3e9c | 344 | break; |
Mike Fiore |
7:0ee8e69a3e9c | 345 | } |
Mike Fiore |
7:0ee8e69a3e9c | 346 | //Check for the start of the next SMS message |
Mike Fiore |
7:0ee8e69a3e9c | 347 | size_t bodyEnd = received.find("\r\n+CMGL: ", pos); |
Mike Fiore |
7:0ee8e69a3e9c | 348 | if(bodyEnd == std::string::npos) { |
Mike Fiore |
7:0ee8e69a3e9c | 349 | //This must be the last SMS message |
Mike Fiore |
7:0ee8e69a3e9c | 350 | bodyEnd = received.find("\r\n\r\nOK", pos); |
Mike Fiore |
7:0ee8e69a3e9c | 351 | } |
Mike Fiore |
7:0ee8e69a3e9c | 352 | |
Mike Fiore |
7:0ee8e69a3e9c | 353 | //Safety check that we found the boundary of this current SMS message |
Mike Fiore |
7:0ee8e69a3e9c | 354 | if(bodyEnd != std::string::npos) { |
Mike Fiore |
7:0ee8e69a3e9c | 355 | sms.message = received.substr(pos, bodyEnd - pos); |
Mike Fiore |
7:0ee8e69a3e9c | 356 | } else { |
Mike Fiore |
7:0ee8e69a3e9c | 357 | sms.message = received.substr(pos); |
Mike Fiore |
7:0ee8e69a3e9c | 358 | logWarning("Expected to find end of SMS list. SMS[%d] DATA[%s].", smsNumber, sms.message.c_str()); |
Mike Fiore |
7:0ee8e69a3e9c | 359 | } |
Mike Fiore |
7:0ee8e69a3e9c | 360 | vSms.push_back(sms); |
Mike Fiore |
7:0ee8e69a3e9c | 361 | pos = bodyEnd; |
Mike Fiore |
7:0ee8e69a3e9c | 362 | smsNumber++; |
Mike Fiore |
7:0ee8e69a3e9c | 363 | } |
Mike Fiore |
7:0ee8e69a3e9c | 364 | logInfo("Received %d SMS", smsNumber); |
Mike Fiore |
7:0ee8e69a3e9c | 365 | return vSms; |
Mike Fiore |
7:0ee8e69a3e9c | 366 | } |
Mike Fiore |
7:0ee8e69a3e9c | 367 | |
Mike Fiore |
7:0ee8e69a3e9c | 368 | Code Cellular::deleteOnlyReceivedReadSms() |
Mike Fiore |
7:0ee8e69a3e9c | 369 | { |
Mike Fiore |
7:0ee8e69a3e9c | 370 | return sendBasicCommand("AT+CMGD=1,1", 1000); |
Mike Fiore |
7:0ee8e69a3e9c | 371 | } |
Mike Fiore |
7:0ee8e69a3e9c | 372 | |
Mike Fiore |
7:0ee8e69a3e9c | 373 | Code Cellular::deleteAllReceivedSms() |
Mike Fiore |
7:0ee8e69a3e9c | 374 | { |
Mike Fiore |
7:0ee8e69a3e9c | 375 | return sendBasicCommand("AT+CMGD=1,4", 1000); |
Mike Fiore |
7:0ee8e69a3e9c | 376 | } |