Simple detection for LE910-NA1 modules
Fork of MTS-Cellular by
Cellular/Cellular.cpp@7:0ee8e69a3e9c, 2014-05-21 (annotated)
- Committer:
- Mike Fiore
- Date:
- Wed May 21 15:21:25 2014 -0500
- Revision:
- 7:0ee8e69a3e9c
- Parent:
- 4:1f63354b8d1b
- Child:
- 8:2d7259d244d1
more restructuring
updated radio types in Cellular.h
moved SMS code to Cellular class
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 |
1:f155d94d6f3a | 8 | std::string Cellular::getRegistrationNames(Registration registration) |
Mike Fiore |
1:f155d94d6f3a | 9 | { |
Mike Fiore |
1:f155d94d6f3a | 10 | switch(registration) { |
Mike Fiore |
1:f155d94d6f3a | 11 | case NOT_REGISTERED: |
Mike Fiore |
1:f155d94d6f3a | 12 | return "NOT_REGISTERED"; |
Mike Fiore |
1:f155d94d6f3a | 13 | case REGISTERED: |
Mike Fiore |
1:f155d94d6f3a | 14 | return "REGISTERED"; |
Mike Fiore |
1:f155d94d6f3a | 15 | case SEARCHING: |
Mike Fiore |
1:f155d94d6f3a | 16 | return "SEARCHING"; |
Mike Fiore |
1:f155d94d6f3a | 17 | case DENIED: |
Mike Fiore |
1:f155d94d6f3a | 18 | return "DENIED"; |
Mike Fiore |
1:f155d94d6f3a | 19 | case UNKNOWN: |
Mike Fiore |
1:f155d94d6f3a | 20 | return "UNKNOWN"; |
Mike Fiore |
1:f155d94d6f3a | 21 | case ROAMING: |
Mike Fiore |
1:f155d94d6f3a | 22 | return "ROAMING"; |
Mike Fiore |
1:f155d94d6f3a | 23 | default: |
Mike Fiore |
1:f155d94d6f3a | 24 | return "UNKNOWN ENUM"; |
Mike Fiore |
1:f155d94d6f3a | 25 | } |
Mike Fiore |
1:f155d94d6f3a | 26 | } |
Mike Fiore |
1:f155d94d6f3a | 27 | |
Mike Fiore |
1:f155d94d6f3a | 28 | Code Cellular::test() |
Mike Fiore |
1:f155d94d6f3a | 29 | { |
Mike Fiore |
1:f155d94d6f3a | 30 | int i = 0; |
Mike Fiore |
1:f155d94d6f3a | 31 | while (sendBasicCommand("AT", 1000) != SUCCESS) { |
Mike Fiore |
1:f155d94d6f3a | 32 | i++; |
Mike Fiore |
1:f155d94d6f3a | 33 | if (i >= 30) { |
Mike Fiore |
4:1f63354b8d1b | 34 | logError("Could not talk to radio after 30 tries"); |
Mike Fiore |
1:f155d94d6f3a | 35 | i = 0; |
Mike Fiore |
1:f155d94d6f3a | 36 | } |
Mike Fiore |
1:f155d94d6f3a | 37 | wait(1); |
Mike Fiore |
1:f155d94d6f3a | 38 | } |
Mike Fiore |
1:f155d94d6f3a | 39 | return SUCCESS; |
Mike Fiore |
1:f155d94d6f3a | 40 | } |
Mike Fiore |
1:f155d94d6f3a | 41 | |
Mike Fiore |
1:f155d94d6f3a | 42 | int Cellular::getSignalStrength() |
Mike Fiore |
1:f155d94d6f3a | 43 | { |
Mike Fiore |
1:f155d94d6f3a | 44 | string response = sendCommand("AT+CSQ", 1000); |
Mike Fiore |
1:f155d94d6f3a | 45 | if (response.find("OK") == string::npos) { |
Mike Fiore |
1:f155d94d6f3a | 46 | return -1; |
Mike Fiore |
1:f155d94d6f3a | 47 | } |
Mike Fiore |
1:f155d94d6f3a | 48 | int start = response.find(':'); |
Mike Fiore |
1:f155d94d6f3a | 49 | int stop = response.find(',', start); |
Mike Fiore |
1:f155d94d6f3a | 50 | string signal = response.substr(start + 2, stop - start - 2); |
Mike Fiore |
1:f155d94d6f3a | 51 | int value; |
Mike Fiore |
1:f155d94d6f3a | 52 | sscanf(signal.c_str(), "%d", &value); |
Mike Fiore |
1:f155d94d6f3a | 53 | return value; |
Mike Fiore |
1:f155d94d6f3a | 54 | } |
Mike Fiore |
1:f155d94d6f3a | 55 | |
Mike Fiore |
1:f155d94d6f3a | 56 | Cellular::Registration Cellular::getRegistration() |
Mike Fiore |
1:f155d94d6f3a | 57 | { |
Mike Fiore |
1:f155d94d6f3a | 58 | string response = sendCommand("AT+CREG?", 5000); |
Mike Fiore |
1:f155d94d6f3a | 59 | if (response.find("OK") == string::npos) { |
Mike Fiore |
1:f155d94d6f3a | 60 | return UNKNOWN; |
Mike Fiore |
1:f155d94d6f3a | 61 | } |
Mike Fiore |
1:f155d94d6f3a | 62 | int start = response.find(','); |
Mike Fiore |
1:f155d94d6f3a | 63 | int stop = response.find(' ', start); |
Mike Fiore |
1:f155d94d6f3a | 64 | string regStat = response.substr(start + 1, stop - start - 1); |
Mike Fiore |
1:f155d94d6f3a | 65 | int value; |
Mike Fiore |
1:f155d94d6f3a | 66 | sscanf(regStat.c_str(), "%d", &value); |
Mike Fiore |
1:f155d94d6f3a | 67 | switch (value) { |
Mike Fiore |
1:f155d94d6f3a | 68 | case 0: |
Mike Fiore |
1:f155d94d6f3a | 69 | return NOT_REGISTERED; |
Mike Fiore |
1:f155d94d6f3a | 70 | case 1: |
Mike Fiore |
1:f155d94d6f3a | 71 | return REGISTERED; |
Mike Fiore |
1:f155d94d6f3a | 72 | case 2: |
Mike Fiore |
1:f155d94d6f3a | 73 | return SEARCHING; |
Mike Fiore |
1:f155d94d6f3a | 74 | case 3: |
Mike Fiore |
1:f155d94d6f3a | 75 | return DENIED; |
Mike Fiore |
1:f155d94d6f3a | 76 | case 4: |
Mike Fiore |
1:f155d94d6f3a | 77 | return UNKNOWN; |
Mike Fiore |
1:f155d94d6f3a | 78 | case 5: |
Mike Fiore |
1:f155d94d6f3a | 79 | return ROAMING; |
Mike Fiore |
1:f155d94d6f3a | 80 | } |
Mike Fiore |
1:f155d94d6f3a | 81 | return UNKNOWN; |
Mike Fiore |
1:f155d94d6f3a | 82 | } |
Mike Fiore |
7:0ee8e69a3e9c | 83 | |
Mike Fiore |
7:0ee8e69a3e9c | 84 | Code Cellular::sendSMS(const Sms& sms) |
Mike Fiore |
7:0ee8e69a3e9c | 85 | { |
Mike Fiore |
7:0ee8e69a3e9c | 86 | return sendSMS(sms.phoneNumber, sms.message); |
Mike Fiore |
7:0ee8e69a3e9c | 87 | } |
Mike Fiore |
7:0ee8e69a3e9c | 88 | |
Mike Fiore |
7:0ee8e69a3e9c | 89 | Code Cellular::sendSMS(const std::string& phoneNumber, const std::string& message) |
Mike Fiore |
7:0ee8e69a3e9c | 90 | { |
Mike Fiore |
7:0ee8e69a3e9c | 91 | Code code = sendBasicCommand("AT+CMGF=1", 1000); |
Mike Fiore |
7:0ee8e69a3e9c | 92 | if (code != SUCCESS) { |
Mike Fiore |
7:0ee8e69a3e9c | 93 | return code; |
Mike Fiore |
7:0ee8e69a3e9c | 94 | } |
Mike Fiore |
7:0ee8e69a3e9c | 95 | string cmd = "AT+CMGS=\"+"; |
Mike Fiore |
7:0ee8e69a3e9c | 96 | cmd.append(phoneNumber); |
Mike Fiore |
7:0ee8e69a3e9c | 97 | cmd.append("\""); |
Mike Fiore |
7:0ee8e69a3e9c | 98 | string response1 = sendCommand(cmd, 1000); |
Mike Fiore |
7:0ee8e69a3e9c | 99 | if (response1.find('>') == string::npos) { |
Mike Fiore |
7:0ee8e69a3e9c | 100 | return NO_RESPONSE; |
Mike Fiore |
7:0ee8e69a3e9c | 101 | } |
Mike Fiore |
7:0ee8e69a3e9c | 102 | wait(.2); |
Mike Fiore |
7:0ee8e69a3e9c | 103 | string response2 = sendCommand(message, 4000, CTRL_Z); |
Mike Fiore |
7:0ee8e69a3e9c | 104 | logInfo("SMS Response: %s", response2.c_str()); |
Mike Fiore |
7:0ee8e69a3e9c | 105 | if (response2.find("+CMGS:") == string::npos) { |
Mike Fiore |
7:0ee8e69a3e9c | 106 | return FAILURE; |
Mike Fiore |
7:0ee8e69a3e9c | 107 | } |
Mike Fiore |
7:0ee8e69a3e9c | 108 | return SUCCESS; |
Mike Fiore |
7:0ee8e69a3e9c | 109 | } |
Mike Fiore |
7:0ee8e69a3e9c | 110 | |
Mike Fiore |
7:0ee8e69a3e9c | 111 | std::vector<Cellular::Sms> Cellular::getReceivedSms() |
Mike Fiore |
7:0ee8e69a3e9c | 112 | { |
Mike Fiore |
7:0ee8e69a3e9c | 113 | int smsNumber = 0; |
Mike Fiore |
7:0ee8e69a3e9c | 114 | std::vector<Sms> vSms; |
Mike Fiore |
7:0ee8e69a3e9c | 115 | std::string received = sendCommand("AT+CMGL=\"ALL\"", 4000); |
Mike Fiore |
7:0ee8e69a3e9c | 116 | size_t pos = received.find("+CMGL: "); |
Mike Fiore |
7:0ee8e69a3e9c | 117 | |
Mike Fiore |
7:0ee8e69a3e9c | 118 | while (pos != std::string::npos) { |
Mike Fiore |
7:0ee8e69a3e9c | 119 | Cellular::Sms sms; |
Mike Fiore |
7:0ee8e69a3e9c | 120 | std::string line(Text::getLine(received, pos, pos)); |
Mike Fiore |
7:0ee8e69a3e9c | 121 | if(line.find("+CMGL: ") == std::string::npos) { |
Mike Fiore |
7:0ee8e69a3e9c | 122 | continue; |
Mike Fiore |
7:0ee8e69a3e9c | 123 | } |
Mike Fiore |
7:0ee8e69a3e9c | 124 | |
Mike Fiore |
7:0ee8e69a3e9c | 125 | //Start of SMS message |
Mike Fiore |
7:0ee8e69a3e9c | 126 | std::vector<std::string> vSmsParts = Text::split(line, ','); |
Mike Fiore |
7:0ee8e69a3e9c | 127 | if(vSmsParts.size() != 6) { |
Mike Fiore |
7:0ee8e69a3e9c | 128 | logWarning("Expected 6 commas. SMS[%d] DATA[%s]. Continuing ...", smsNumber, line.c_str()); |
Mike Fiore |
7:0ee8e69a3e9c | 129 | continue; |
Mike Fiore |
7:0ee8e69a3e9c | 130 | } |
Mike Fiore |
7:0ee8e69a3e9c | 131 | |
Mike Fiore |
7:0ee8e69a3e9c | 132 | sms.phoneNumber = vSmsParts[2]; |
Mike Fiore |
7:0ee8e69a3e9c | 133 | sms.timestamp = vSmsParts[4] + ", " + vSmsParts[5]; |
Mike Fiore |
7:0ee8e69a3e9c | 134 | |
Mike Fiore |
7:0ee8e69a3e9c | 135 | if(pos == std::string::npos) { |
Mike Fiore |
7:0ee8e69a3e9c | 136 | logWarning("Expected SMS body. SMS[%d]. Leaving ...", smsNumber); |
Mike Fiore |
7:0ee8e69a3e9c | 137 | break; |
Mike Fiore |
7:0ee8e69a3e9c | 138 | } |
Mike Fiore |
7:0ee8e69a3e9c | 139 | //Check for the start of the next SMS message |
Mike Fiore |
7:0ee8e69a3e9c | 140 | size_t bodyEnd = received.find("\r\n+CMGL: ", pos); |
Mike Fiore |
7:0ee8e69a3e9c | 141 | if(bodyEnd == std::string::npos) { |
Mike Fiore |
7:0ee8e69a3e9c | 142 | //This must be the last SMS message |
Mike Fiore |
7:0ee8e69a3e9c | 143 | bodyEnd = received.find("\r\n\r\nOK", pos); |
Mike Fiore |
7:0ee8e69a3e9c | 144 | } |
Mike Fiore |
7:0ee8e69a3e9c | 145 | |
Mike Fiore |
7:0ee8e69a3e9c | 146 | //Safety check that we found the boundary of this current SMS message |
Mike Fiore |
7:0ee8e69a3e9c | 147 | if(bodyEnd != std::string::npos) { |
Mike Fiore |
7:0ee8e69a3e9c | 148 | sms.message = received.substr(pos, bodyEnd - pos); |
Mike Fiore |
7:0ee8e69a3e9c | 149 | } else { |
Mike Fiore |
7:0ee8e69a3e9c | 150 | sms.message = received.substr(pos); |
Mike Fiore |
7:0ee8e69a3e9c | 151 | logWarning("Expected to find end of SMS list. SMS[%d] DATA[%s].", smsNumber, sms.message.c_str()); |
Mike Fiore |
7:0ee8e69a3e9c | 152 | } |
Mike Fiore |
7:0ee8e69a3e9c | 153 | vSms.push_back(sms); |
Mike Fiore |
7:0ee8e69a3e9c | 154 | pos = bodyEnd; |
Mike Fiore |
7:0ee8e69a3e9c | 155 | smsNumber++; |
Mike Fiore |
7:0ee8e69a3e9c | 156 | } |
Mike Fiore |
7:0ee8e69a3e9c | 157 | logInfo("Received %d SMS", smsNumber); |
Mike Fiore |
7:0ee8e69a3e9c | 158 | return vSms; |
Mike Fiore |
7:0ee8e69a3e9c | 159 | } |
Mike Fiore |
7:0ee8e69a3e9c | 160 | |
Mike Fiore |
7:0ee8e69a3e9c | 161 | Code Cellular::deleteOnlyReceivedReadSms() |
Mike Fiore |
7:0ee8e69a3e9c | 162 | { |
Mike Fiore |
7:0ee8e69a3e9c | 163 | return sendBasicCommand("AT+CMGD=1,1", 1000); |
Mike Fiore |
7:0ee8e69a3e9c | 164 | } |
Mike Fiore |
7:0ee8e69a3e9c | 165 | |
Mike Fiore |
7:0ee8e69a3e9c | 166 | Code Cellular::deleteAllReceivedSms() |
Mike Fiore |
7:0ee8e69a3e9c | 167 | { |
Mike Fiore |
7:0ee8e69a3e9c | 168 | return sendBasicCommand("AT+CMGD=1,4", 1000); |
Mike Fiore |
7:0ee8e69a3e9c | 169 | } |