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