Simple detection for LE910-NA1 modules
Fork of MTS-Cellular by
Cellular/EasyIP.cpp@31:529db15abda7, 2014-07-14 (annotated)
- Committer:
- Vanger
- Date:
- Mon Jul 14 21:11:50 2014 +0000
- Revision:
- 31:529db15abda7
- Parent:
- 30:1326b623919a
- Child:
- 32:7d5581159bed
Pulled MTS-Cellular changes into MTS-Cellular-dev branch.; Also implemented setSocketCloseable(); Modified read(),write(), and sendEscapeCommand() to account for socketCloseable boolean flag.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Vanger | 26:2b769ed8de4f | 1 | // This is a template from UIP.cpp for now, will modify code and implement it as I go |
Vanger | 26:2b769ed8de4f | 2 | |
Vanger | 26:2b769ed8de4f | 3 | |
Vanger | 26:2b769ed8de4f | 4 | |
Vanger | 26:2b769ed8de4f | 5 | #include "mbed.h" |
Vanger | 26:2b769ed8de4f | 6 | #include "EasyIP.h" |
Vanger | 26:2b769ed8de4f | 7 | #include "MTSText.h" |
Vanger | 26:2b769ed8de4f | 8 | #include "MTSLog.h" |
Vanger | 26:2b769ed8de4f | 9 | #include "CellUtils.h" |
Vanger | 26:2b769ed8de4f | 10 | |
Vanger | 26:2b769ed8de4f | 11 | |
Vanger | 26:2b769ed8de4f | 12 | |
Vanger | 26:2b769ed8de4f | 13 | using namespace mts; |
Vanger | 26:2b769ed8de4f | 14 | |
Vanger | 30:1326b623919a | 15 | bool EasyIP::sendEscapeCommand() |
Vanger | 30:1326b623919a | 16 | { |
Vanger | 30:1326b623919a | 17 | //string Cellular::sendCommand(const std::string& command, unsigned int timeoutMillis, char esc) |
Vanger | 30:1326b623919a | 18 | if(io == NULL) { |
Vanger | 30:1326b623919a | 19 | logError("MTSBufferedIO not set"); |
Vanger | 30:1326b623919a | 20 | return false; |
Vanger | 30:1326b623919a | 21 | } |
Vanger | 30:1326b623919a | 22 | if(!socketOpened) { |
Vanger | 30:1326b623919a | 23 | logError("Socket is not open. Can not send AT escape sequence (+++)"); |
Vanger | 30:1326b623919a | 24 | return false; |
Vanger | 30:1326b623919a | 25 | } |
Vanger | 30:1326b623919a | 26 | |
Vanger | 31:529db15abda7 | 27 | if(!socketCloseable) { |
Vanger | 31:529db15abda7 | 28 | logError("Socket is not closeable"); |
Vanger | 31:529db15abda7 | 29 | return false; |
Vanger | 31:529db15abda7 | 30 | } |
Vanger | 31:529db15abda7 | 31 | |
Vanger | 30:1326b623919a | 32 | io->rxClear(); |
Vanger | 30:1326b623919a | 33 | io->txClear(); |
Vanger | 30:1326b623919a | 34 | |
Vanger | 30:1326b623919a | 35 | std::string result; |
Vanger | 30:1326b623919a | 36 | unsigned int timeoutMillis = 2000; |
Vanger | 30:1326b623919a | 37 | const int size_cmd = 3; |
Vanger | 30:1326b623919a | 38 | //Attempt to write command |
Vanger | 30:1326b623919a | 39 | wait(1); //Format for +++ command is 1 second wait, send +++, then another second wait |
Vanger | 30:1326b623919a | 40 | //1s wait after command is implemented as a polling function for 2 seconds |
Vanger | 30:1326b623919a | 41 | //Option: Could change wait periods to be longer/shorter (0-255)*50ms |
Vanger | 30:1326b623919a | 42 | if(io->write("+++", size_cmd, timeoutMillis) != size_cmd) { |
Vanger | 30:1326b623919a | 43 | //Failed to write command |
Vanger | 30:1326b623919a | 44 | logError("failed to send command to radio within %d milliseconds", timeoutMillis); |
Vanger | 30:1326b623919a | 45 | return false; |
Vanger | 30:1326b623919a | 46 | } |
Vanger | 30:1326b623919a | 47 | |
Vanger | 30:1326b623919a | 48 | int timer = 0; |
Vanger | 30:1326b623919a | 49 | char tmp[256]; |
Vanger | 30:1326b623919a | 50 | tmp[255] = 0; |
Vanger | 30:1326b623919a | 51 | bool done = false; |
Vanger | 30:1326b623919a | 52 | io->read(tmp,255,0); |
Vanger | 30:1326b623919a | 53 | bool exitmode = false; |
Vanger | 30:1326b623919a | 54 | |
Vanger | 30:1326b623919a | 55 | do { |
Vanger | 30:1326b623919a | 56 | wait(0.1); |
Vanger | 30:1326b623919a | 57 | timer += 100; |
Vanger | 30:1326b623919a | 58 | //Make a non-blocking read call by passing timeout of zero |
Vanger | 30:1326b623919a | 59 | int size = io->read(tmp,255,0); //1 less than allocated (timeout is instant) |
Vanger | 30:1326b623919a | 60 | if(size > 0) { |
Vanger | 30:1326b623919a | 61 | result.append(tmp, size); |
Vanger | 30:1326b623919a | 62 | } |
Vanger | 30:1326b623919a | 63 | if(result.find("OK") != std::string::npos) { |
Vanger | 30:1326b623919a | 64 | exitmode = true; |
Vanger | 30:1326b623919a | 65 | done = true; |
Vanger | 30:1326b623919a | 66 | } else if(result.find("NO CARRIER") != std::string::npos) { |
Vanger | 30:1326b623919a | 67 | exitmode = true; |
Vanger | 30:1326b623919a | 68 | done = true; |
Vanger | 30:1326b623919a | 69 | } else if(result.find("ERROR") != std::string::npos) { |
Vanger | 30:1326b623919a | 70 | exitmode = false; |
Vanger | 30:1326b623919a | 71 | done = true; |
Vanger | 30:1326b623919a | 72 | } |
Vanger | 30:1326b623919a | 73 | if(timer >= timeoutMillis) { |
Vanger | 30:1326b623919a | 74 | logDebug("Escape sequence [+++] timed out after %d milliseconds", timeoutMillis); |
Vanger | 30:1326b623919a | 75 | exitmode = true; |
Vanger | 30:1326b623919a | 76 | done = true; |
Vanger | 30:1326b623919a | 77 | } |
Vanger | 30:1326b623919a | 78 | } while (!done); |
Vanger | 30:1326b623919a | 79 | |
Vanger | 30:1326b623919a | 80 | return exitmode; |
Vanger | 30:1326b623919a | 81 | } |
Vanger | 30:1326b623919a | 82 | |
Vanger | 30:1326b623919a | 83 | bool EasyIP::socketCheck() { |
Vanger | 30:1326b623919a | 84 | bool status = false; |
Vanger | 30:1326b623919a | 85 | std::string socketInfo = "9"; //9 is unrecognized |
Vanger | 30:1326b623919a | 86 | std::vector<std::string> params; |
Vanger | 30:1326b623919a | 87 | |
Vanger | 30:1326b623919a | 88 | if(sendEscapeCommand()) { |
Vanger | 30:1326b623919a | 89 | socketOpened = false; |
Vanger | 30:1326b623919a | 90 | if(sendBasicCommand("AT", 1000) == MTS_SUCCESS) { |
Vanger | 30:1326b623919a | 91 | socketInfo = sendCommand("AT#SS=1", 2000); |
Vanger | 30:1326b623919a | 92 | if(socketInfo.find("OK") != std::string::npos) { |
Vanger | 30:1326b623919a | 93 | //Found valid response |
Vanger | 30:1326b623919a | 94 | params = Text::split(socketInfo, "\r\n"); |
Vanger | 30:1326b623919a | 95 | params = Text::split(params[1], ","); |
Vanger | 30:1326b623919a | 96 | socketInfo = params[1]; |
Vanger | 30:1326b623919a | 97 | //Check comparison of params[1] to response codes |
Vanger | 30:1326b623919a | 98 | } else { |
Vanger | 30:1326b623919a | 99 | logError("Could not determine socket status[%s]",socketInfo.c_str()); |
Vanger | 30:1326b623919a | 100 | socketInfo == "9"; //9 is unrecognized |
Vanger | 30:1326b623919a | 101 | } |
Vanger | 30:1326b623919a | 102 | } |
Vanger | 30:1326b623919a | 103 | } else { |
Vanger | 30:1326b623919a | 104 | status = false; //Return value of socketOpened when checking |
Vanger | 30:1326b623919a | 105 | } |
Vanger | 30:1326b623919a | 106 | //Check socket status query |
Vanger | 30:1326b623919a | 107 | if(socketInfo == "2" || socketInfo == "3" || socketInfo == "1" || socketInfo == "4") { |
Vanger | 30:1326b623919a | 108 | status = true; //2 and 3 are suspended connections |
Vanger | 30:1326b623919a | 109 | } else if(socketInfo == "0" || socketInfo == "5") { |
Vanger | 30:1326b623919a | 110 | status = false; //0 is closed socket, probably won't occur |
Vanger | 30:1326b623919a | 111 | } else { |
Vanger | 30:1326b623919a | 112 | logError("Could not determine socket status"); |
Vanger | 30:1326b623919a | 113 | status = false; //anything else is unknown status |
Vanger | 30:1326b623919a | 114 | } |
Vanger | 30:1326b623919a | 115 | |
Vanger | 30:1326b623919a | 116 | if(status) { |
Vanger | 30:1326b623919a | 117 | std::string reconnect = sendCommand("AT#SO=1", 2000); |
Vanger | 30:1326b623919a | 118 | if(reconnect.find("CONNECT") != std::string::npos || reconnect.find("OK") != std::string::npos) { |
Vanger | 30:1326b623919a | 119 | } else { |
Vanger | 30:1326b623919a | 120 | logError("Failed to resume socket connection"); |
Vanger | 30:1326b623919a | 121 | } |
Vanger | 30:1326b623919a | 122 | } |
Vanger | 30:1326b623919a | 123 | return status; |
Vanger | 30:1326b623919a | 124 | } |
Vanger | 30:1326b623919a | 125 | |
Vanger | 26:2b769ed8de4f | 126 | EasyIP::EasyIP(Radio type) |
Vanger | 26:2b769ed8de4f | 127 | { |
Vanger | 26:2b769ed8de4f | 128 | //Not sure how the construction process is done, |
Vanger | 26:2b769ed8de4f | 129 | //but assuming it works for both EasyIP and UIP the same way. |
Vanger | 26:2b769ed8de4f | 130 | this->type = type; |
Vanger | 26:2b769ed8de4f | 131 | io = NULL; |
Vanger | 26:2b769ed8de4f | 132 | dcd = NULL; |
Vanger | 26:2b769ed8de4f | 133 | dtr = NULL; |
Vanger | 26:2b769ed8de4f | 134 | resetLine = NULL; |
Vanger | 26:2b769ed8de4f | 135 | echoMode = true; |
Vanger | 26:2b769ed8de4f | 136 | pppConnected = false; |
Vanger | 26:2b769ed8de4f | 137 | socketMode = TCP; |
Vanger | 26:2b769ed8de4f | 138 | socketOpened = false; |
Vanger | 26:2b769ed8de4f | 139 | socketCloseable = true; |
Vanger | 26:2b769ed8de4f | 140 | local_port = 0; |
Vanger | 26:2b769ed8de4f | 141 | local_address = ""; |
Vanger | 26:2b769ed8de4f | 142 | host_port = 0; |
Vanger | 26:2b769ed8de4f | 143 | } |
Vanger | 26:2b769ed8de4f | 144 | |
Vanger | 26:2b769ed8de4f | 145 | EasyIP::~EasyIP() |
Vanger | 26:2b769ed8de4f | 146 | { |
Vanger | 26:2b769ed8de4f | 147 | //Same reasoning for the destructor as the constructor, |
Vanger | 26:2b769ed8de4f | 148 | //assuming it works for UIP, it will work for EasyIP |
Vanger | 26:2b769ed8de4f | 149 | if (dtr != NULL) { |
Vanger | 26:2b769ed8de4f | 150 | dtr->write(1); |
Vanger | 26:2b769ed8de4f | 151 | } |
Vanger | 26:2b769ed8de4f | 152 | |
Vanger | 26:2b769ed8de4f | 153 | delete dcd; |
Vanger | 26:2b769ed8de4f | 154 | delete dtr; |
Vanger | 26:2b769ed8de4f | 155 | delete resetLine; |
Vanger | 26:2b769ed8de4f | 156 | } |
Vanger | 26:2b769ed8de4f | 157 | |
Vanger | 26:2b769ed8de4f | 158 | //Initializes the MTS IO Buffer |
Vanger | 26:2b769ed8de4f | 159 | bool EasyIP::init(MTSBufferedIO* io) |
Vanger | 26:2b769ed8de4f | 160 | { |
Vanger | 26:2b769ed8de4f | 161 | if (! Cellular::init(io)) { |
Vanger | 26:2b769ed8de4f | 162 | return false; |
Vanger | 26:2b769ed8de4f | 163 | } |
Vanger | 26:2b769ed8de4f | 164 | |
Vanger | 26:2b769ed8de4f | 165 | logDebug("radio type: %s", Cellular::getRadioNames(type).c_str()); |
Vanger | 26:2b769ed8de4f | 166 | return true; |
Vanger | 26:2b769ed8de4f | 167 | } |
Vanger | 26:2b769ed8de4f | 168 | |
Vanger | 26:2b769ed8de4f | 169 | bool EasyIP::connect() |
Vanger | 26:2b769ed8de4f | 170 | { |
Vanger | 29:edc613ed3f2e | 171 | //Check if APN is not set, if so, connect will not work. |
Vanger | 29:edc613ed3f2e | 172 | if (type == MTSMC_H5_IP || type == MTSMC_H5 || type == MTSMC_G3) { |
Vanger | 29:edc613ed3f2e | 173 | if(apn.size() == 0) { |
Vanger | 29:edc613ed3f2e | 174 | logDebug("APN is not set"); |
Vanger | 29:edc613ed3f2e | 175 | return false; |
Vanger | 29:edc613ed3f2e | 176 | } |
Vanger | 29:edc613ed3f2e | 177 | } |
Vanger | 29:edc613ed3f2e | 178 | |
Vanger | 26:2b769ed8de4f | 179 | //Check if socket is open |
Vanger | 26:2b769ed8de4f | 180 | //flag stored in Cellular.h |
Vanger | 26:2b769ed8de4f | 181 | if(socketOpened) { |
Vanger | 26:2b769ed8de4f | 182 | return true; |
Vanger | 26:2b769ed8de4f | 183 | } |
Vanger | 26:2b769ed8de4f | 184 | |
Vanger | 26:2b769ed8de4f | 185 | //Check if already connected |
Vanger | 26:2b769ed8de4f | 186 | //by calling the function isConnected() in EasyIP.cpp |
Vanger | 26:2b769ed8de4f | 187 | if(isConnected()) { |
Vanger | 26:2b769ed8de4f | 188 | return true; |
Vanger | 26:2b769ed8de4f | 189 | } |
Vanger | 26:2b769ed8de4f | 190 | //Create an mbed timer object |
Vanger | 26:2b769ed8de4f | 191 | Timer tmr; |
Vanger | 26:2b769ed8de4f | 192 | //Check Registration: AT+CREG? == 0,1 |
Vanger | 26:2b769ed8de4f | 193 | //(Does the AT command inside Cellular class) |
Vanger | 26:2b769ed8de4f | 194 | tmr.start(); |
Vanger | 26:2b769ed8de4f | 195 | do { |
Vanger | 26:2b769ed8de4f | 196 | Registration registration = getRegistration(); |
Vanger | 26:2b769ed8de4f | 197 | if(registration != REGISTERED) { |
Vanger | 26:2b769ed8de4f | 198 | logTrace("Not Registered [%d] ... waiting", (int)registration); |
Vanger | 26:2b769ed8de4f | 199 | wait(1); |
Vanger | 26:2b769ed8de4f | 200 | } else { |
Vanger | 26:2b769ed8de4f | 201 | break; |
Vanger | 26:2b769ed8de4f | 202 | } |
Vanger | 30:1326b623919a | 203 | } while(tmr.read() < 30); |
Vanger | 26:2b769ed8de4f | 204 | //Check RSSI: AT+CSQ |
Vanger | 26:2b769ed8de4f | 205 | //Does the command inside Cellular |
Vanger | 26:2b769ed8de4f | 206 | tmr.reset(); |
Vanger | 26:2b769ed8de4f | 207 | do { |
Vanger | 26:2b769ed8de4f | 208 | int rssi = getSignalStrength(); |
Vanger | 26:2b769ed8de4f | 209 | logDebug("Signal strength: %d", rssi); |
Vanger | 30:1326b623919a | 210 | if((rssi == 99) || (rssi == -1)) { |
Vanger | 26:2b769ed8de4f | 211 | logTrace("No Signal ... waiting"); |
Vanger | 26:2b769ed8de4f | 212 | wait(1); |
Vanger | 26:2b769ed8de4f | 213 | } else { |
Vanger | 26:2b769ed8de4f | 214 | break; |
Vanger | 26:2b769ed8de4f | 215 | } |
Vanger | 26:2b769ed8de4f | 216 | } while(tmr.read() < 30); |
Vanger | 26:2b769ed8de4f | 217 | |
Vanger | 27:ec44d5a9544f | 218 | //Similar to AT#CONNECTIONSTART: Make a PPP connection |
Vanger | 26:2b769ed8de4f | 219 | if (type == MTSMC_H5 || type == MTSMC_G3) { |
Vanger | 26:2b769ed8de4f | 220 | logDebug("Making PPP Connection Attempt. APN[%s]", apn.c_str()); |
Vanger | 26:2b769ed8de4f | 221 | } else { |
Vanger | 26:2b769ed8de4f | 222 | logDebug("Making PPP Connection Attempt"); |
Vanger | 26:2b769ed8de4f | 223 | } |
Vanger | 26:2b769ed8de4f | 224 | //The main thing going on; Sends the AT command to start a connection |
Vanger | 30:1326b623919a | 225 | //Assuming context is already stored in the modem |
Vanger | 30:1326b623919a | 226 | std::string pppResult = sendCommand("AT#SGACT=1,1", 2000); |
Vanger | 30:1326b623919a | 227 | std::vector<std::string> parts; |
Vanger | 26:2b769ed8de4f | 228 | if(pppResult.find("OK") != std::string::npos) { |
Vanger | 30:1326b623919a | 229 | parts = Text::split(pppResult, "\r\n"); |
Vanger | 26:2b769ed8de4f | 230 | if(parts.size() >= 2) { |
Vanger | 26:2b769ed8de4f | 231 | parts = Text::split(parts[1], " "); |
Vanger | 26:2b769ed8de4f | 232 | local_address = parts[1]; |
Vanger | 26:2b769ed8de4f | 233 | } |
Vanger | 26:2b769ed8de4f | 234 | logInfo("PPP Connection Established: IP[%s]", local_address.c_str()); |
Vanger | 26:2b769ed8de4f | 235 | pppConnected = true; |
Vanger | 26:2b769ed8de4f | 236 | |
Vanger | 26:2b769ed8de4f | 237 | } else { |
Vanger | 30:1326b623919a | 238 | pppResult = sendCommand("AT#SGACT?", 2000); |
Vanger | 30:1326b623919a | 239 | if(pppResult.empty() || (pppResult.find("ERROR") != std::string::npos)) { |
Vanger | 30:1326b623919a | 240 | pppConnected = false; |
Vanger | 30:1326b623919a | 241 | } else { |
Vanger | 30:1326b623919a | 242 | if(pppResult.find("1,1") != std::string::npos) { |
Vanger | 30:1326b623919a | 243 | pppConnected = true; |
Vanger | 30:1326b623919a | 244 | } else { |
Vanger | 30:1326b623919a | 245 | pppConnected = false; |
Vanger | 30:1326b623919a | 246 | } |
Vanger | 30:1326b623919a | 247 | } |
Vanger | 26:2b769ed8de4f | 248 | } |
Vanger | 26:2b769ed8de4f | 249 | |
Vanger | 26:2b769ed8de4f | 250 | return pppConnected; |
Vanger | 26:2b769ed8de4f | 251 | } |
Vanger | 26:2b769ed8de4f | 252 | |
Vanger | 26:2b769ed8de4f | 253 | void EasyIP::disconnect() |
Vanger | 26:2b769ed8de4f | 254 | { |
Vanger | 26:2b769ed8de4f | 255 | //AT#SGACT=1,0: Close a PPP connection |
Vanger | 28:f93d7b3f7c2e | 256 | logDebug("Closing PPP Connection"); |
Vanger | 26:2b769ed8de4f | 257 | if(socketOpened) { |
Vanger | 30:1326b623919a | 258 | if(!close()) { //Calls another EasyIP function to close socket before disconnect |
Vanger | 30:1326b623919a | 259 | logDebug("Failed to close socket for disconnect"); |
Vanger | 30:1326b623919a | 260 | return; //Can't close connection without AT commands |
Vanger | 30:1326b623919a | 261 | //(and thus need socket closed) |
Vanger | 30:1326b623919a | 262 | } |
Vanger | 26:2b769ed8de4f | 263 | } |
Vanger | 26:2b769ed8de4f | 264 | //Sends AT#SGACT=1,0 command |
Vanger | 30:1326b623919a | 265 | if(sendBasicCommand("AT#SGACT=1,0", 1000) == MTS_SUCCESS) { |
Vanger | 30:1326b623919a | 266 | pppConnected = false; |
Vanger | 30:1326b623919a | 267 | logDebug("Successfully closed PPP Connection"); |
Vanger | 30:1326b623919a | 268 | } |
Vanger | 28:f93d7b3f7c2e | 269 | pppConnected = false; //Cell will drop connection if we go silent |
Vanger | 30:1326b623919a | 270 | return; |
Vanger | 26:2b769ed8de4f | 271 | } |
Vanger | 27:ec44d5a9544f | 272 | |
Vanger | 26:2b769ed8de4f | 273 | bool EasyIP::isConnected() |
Vanger | 26:2b769ed8de4f | 274 | { |
Vanger | 29:edc613ed3f2e | 275 | std::string stateString; |
Vanger | 29:edc613ed3f2e | 276 | std::vector<std::string> pieces; |
Vanger | 30:1326b623919a | 277 | //state flags for various connection checks |
Vanger | 30:1326b623919a | 278 | bool signal = false, regist = false, active = false; |
Vanger | 26:2b769ed8de4f | 279 | |
Vanger | 29:edc613ed3f2e | 280 | //1) Check if APN was set if we're on an HSPA radio |
Vanger | 29:edc613ed3f2e | 281 | if (type == MTSMC_H5_IP || type == MTSMC_H5 || type == MTSMC_G3) { |
Vanger | 29:edc613ed3f2e | 282 | if(apn.size() == 0) { |
Vanger | 29:edc613ed3f2e | 283 | logDebug("APN is not set"); |
Vanger | 29:edc613ed3f2e | 284 | return false; |
Vanger | 29:edc613ed3f2e | 285 | } |
Vanger | 29:edc613ed3f2e | 286 | } |
Vanger | 29:edc613ed3f2e | 287 | |
Vanger | 29:edc613ed3f2e | 288 | //2) Check that we do not have a live connection up |
Vanger | 29:edc613ed3f2e | 289 | if(socketOpened) { |
Vanger | 29:edc613ed3f2e | 290 | logDebug("Socket is opened"); |
Vanger | 29:edc613ed3f2e | 291 | return true; |
Vanger | 29:edc613ed3f2e | 292 | } |
Vanger | 29:edc613ed3f2e | 293 | |
Vanger | 30:1326b623919a | 294 | |
Vanger | 29:edc613ed3f2e | 295 | //3) Query the radio |
Vanger | 29:edc613ed3f2e | 296 | //Check antenna signal |
Vanger | 30:1326b623919a | 297 | std::string reply = sendCommand("AT+CSQ", 500); |
Vanger | 29:edc613ed3f2e | 298 | if(reply.empty() || (reply.find("ERROR") != std::string::npos)) { |
Vanger | 29:edc613ed3f2e | 299 | signal = false; |
Vanger | 29:edc613ed3f2e | 300 | } else { |
Vanger | 29:edc613ed3f2e | 301 | pieces = Text::split(reply, "\r\n"); |
Vanger | 29:edc613ed3f2e | 302 | if(pieces.size() >= 2) { |
Vanger | 29:edc613ed3f2e | 303 | pieces = Text::split(pieces[1], " "); |
Vanger | 29:edc613ed3f2e | 304 | if(pieces.size() >= 2) { |
Vanger | 29:edc613ed3f2e | 305 | if((pieces[1].find("0,0") != std::string::npos) || (pieces[1].find("99,99") != std::string::npos)) { |
Vanger | 29:edc613ed3f2e | 306 | signal = false; |
Vanger | 29:edc613ed3f2e | 307 | } else { |
Vanger | 29:edc613ed3f2e | 308 | signal = true; |
Vanger | 29:edc613ed3f2e | 309 | } |
Vanger | 29:edc613ed3f2e | 310 | } |
Vanger | 29:edc613ed3f2e | 311 | } |
Vanger | 29:edc613ed3f2e | 312 | } |
Vanger | 27:ec44d5a9544f | 313 | |
Vanger | 29:edc613ed3f2e | 314 | //Check cell tower registration |
Vanger | 30:1326b623919a | 315 | reply = sendCommand("AT+CREG?", 500); |
Vanger | 29:edc613ed3f2e | 316 | if(reply.empty() || (reply.find("ERROR") != std::string::npos)) { |
Vanger | 29:edc613ed3f2e | 317 | regist = false; |
Vanger | 29:edc613ed3f2e | 318 | } else { |
Vanger | 29:edc613ed3f2e | 319 | pieces = Text::split(reply, "\r\n"); |
Vanger | 29:edc613ed3f2e | 320 | if(pieces.size() >= 2) { |
Vanger | 29:edc613ed3f2e | 321 | pieces = Text::split(pieces[1], " "); |
Vanger | 29:edc613ed3f2e | 322 | if(pieces.size() >= 2) { |
Vanger | 29:edc613ed3f2e | 323 | if((pieces[1].find("0,1") != std::string::npos) || (pieces[1].find("0,5") != std::string::npos)) { |
Vanger | 29:edc613ed3f2e | 324 | regist = true; //1 for connected, 5 for roaming connected |
Vanger | 29:edc613ed3f2e | 325 | } else { |
Vanger | 29:edc613ed3f2e | 326 | regist = false; //Cell tower not registered |
Vanger | 29:edc613ed3f2e | 327 | } |
Vanger | 29:edc613ed3f2e | 328 | } |
Vanger | 29:edc613ed3f2e | 329 | } |
Vanger | 29:edc613ed3f2e | 330 | } |
Vanger | 27:ec44d5a9544f | 331 | |
Vanger | 29:edc613ed3f2e | 332 | //Check active mode (SGACT = 1,1) |
Vanger | 30:1326b623919a | 333 | reply = sendCommand("AT#SGACT?", 500); |
Vanger | 29:edc613ed3f2e | 334 | if(reply.empty() || (reply.find("ERROR") != std::string::npos)) { |
Vanger | 29:edc613ed3f2e | 335 | active = false; |
Vanger | 29:edc613ed3f2e | 336 | } else { |
Vanger | 29:edc613ed3f2e | 337 | pieces = Text::split(reply, "\r\n"); |
Vanger | 29:edc613ed3f2e | 338 | if(pieces.size() >= 2) { |
Vanger | 29:edc613ed3f2e | 339 | pieces = Text::split(pieces[1], " "); |
Vanger | 29:edc613ed3f2e | 340 | if(pieces.size() >= 2) { |
Vanger | 29:edc613ed3f2e | 341 | if(pieces[1].find("1,1") != std::string::npos) { |
Vanger | 29:edc613ed3f2e | 342 | active = true; //1 for an active connection mode |
Vanger | 29:edc613ed3f2e | 343 | } else { |
Vanger | 29:edc613ed3f2e | 344 | active = false; //0, or unknown value, is an inactive connection mode |
Vanger | 29:edc613ed3f2e | 345 | } |
Vanger | 29:edc613ed3f2e | 346 | } |
Vanger | 29:edc613ed3f2e | 347 | } |
Vanger | 29:edc613ed3f2e | 348 | } |
Vanger | 30:1326b623919a | 349 | //4) Determine radio state |
Vanger | 30:1326b623919a | 350 | if(regist && signal) { |
Vanger | 29:edc613ed3f2e | 351 | if(pppConnected) { |
Vanger | 30:1326b623919a | 352 | if(active) { |
Vanger | 30:1326b623919a | 353 | if(ping()) { |
Vanger | 30:1326b623919a | 354 | stateString = "CONNECTED"; |
Vanger | 30:1326b623919a | 355 | pppConnected = true; |
Vanger | 29:edc613ed3f2e | 356 | } else { |
Vanger | 30:1326b623919a | 357 | stateString = "AUTHENTICATING"; |
Vanger | 30:1326b623919a | 358 | pppConnected = true; |
Vanger | 30:1326b623919a | 359 | return false; //Return false instead of changing pppConnected due to the fact |
Vanger | 30:1326b623919a | 360 | //that it is connected to ppp, it just can't ping successfully |
Vanger | 29:edc613ed3f2e | 361 | } |
Vanger | 30:1326b623919a | 362 | } else { |
Vanger | 30:1326b623919a | 363 | stateString = "DISCONNECTING"; |
Vanger | 30:1326b623919a | 364 | pppConnected = false; |
Vanger | 30:1326b623919a | 365 | } |
Vanger | 30:1326b623919a | 366 | } else { |
Vanger | 30:1326b623919a | 367 | if(active) { |
Vanger | 30:1326b623919a | 368 | if(ping()) { |
Vanger | 30:1326b623919a | 369 | pppConnected = true; |
Vanger | 30:1326b623919a | 370 | logWarning("Internal PPP state tracking differs from radio (DISCONNECTED:CONNECTED)"); |
Vanger | 30:1326b623919a | 371 | stateString = "CONNECTED"; |
Vanger | 30:1326b623919a | 372 | } else { |
Vanger | 30:1326b623919a | 373 | stateString = "CONNECTING"; |
Vanger | 30:1326b623919a | 374 | } |
Vanger | 30:1326b623919a | 375 | } else { |
Vanger | 30:1326b623919a | 376 | stateString = "IDLE"; |
Vanger | 30:1326b623919a | 377 | } |
Vanger | 29:edc613ed3f2e | 378 | } |
Vanger | 30:1326b623919a | 379 | } else if(regist != signal) { |
Vanger | 30:1326b623919a | 380 | stateString = "CHECKING"; |
Vanger | 29:edc613ed3f2e | 381 | pppConnected = false; |
Vanger | 30:1326b623919a | 382 | } else if(!regist && !signal) { |
Vanger | 30:1326b623919a | 383 | stateString = "DISCONNECTED"; |
Vanger | 30:1326b623919a | 384 | pppConnected = false; |
Vanger | 30:1326b623919a | 385 | } |
Vanger | 30:1326b623919a | 386 | //Log results if necessary |
Vanger | 30:1326b623919a | 387 | if(stateString != "CONNECTED") { |
Vanger | 30:1326b623919a | 388 | logWarning("Internal PPP state tracking differs from radio (CONNECTED:%s)",stateString.c_str()); |
Vanger | 29:edc613ed3f2e | 389 | } |
Vanger | 26:2b769ed8de4f | 390 | return pppConnected; |
Vanger | 26:2b769ed8de4f | 391 | } |
Vanger | 30:1326b623919a | 392 | |
Vanger | 26:2b769ed8de4f | 393 | //Binds the socket to a specific port if able |
Vanger | 26:2b769ed8de4f | 394 | bool EasyIP::bind(unsigned int port) |
Vanger | 26:2b769ed8de4f | 395 | { |
Vanger | 30:1326b623919a | 396 | if(socketOpened) { |
Vanger | 30:1326b623919a | 397 | logError("socket is open. Can not set local port"); |
Vanger | 30:1326b623919a | 398 | return false; |
Vanger | 30:1326b623919a | 399 | } |
Vanger | 30:1326b623919a | 400 | if(port > 65535) { |
Vanger | 30:1326b623919a | 401 | logError("port out of range (0-65535)"); |
Vanger | 30:1326b623919a | 402 | return false; |
Vanger | 30:1326b623919a | 403 | } |
Vanger | 30:1326b623919a | 404 | local_port = port; |
Vanger | 26:2b769ed8de4f | 405 | return true; |
Vanger | 26:2b769ed8de4f | 406 | } |
Vanger | 27:ec44d5a9544f | 407 | |
Vanger | 26:2b769ed8de4f | 408 | bool EasyIP::open(const std::string& address, unsigned int port, Mode mode) |
Vanger | 26:2b769ed8de4f | 409 | { |
Vanger | 30:1326b623919a | 410 | char sOpenSocketCmd[256] = {0}; //String for AT command |
Vanger | 30:1326b623919a | 411 | std::string sMode = ""; |
Vanger | 31:529db15abda7 | 412 | int typeSocket = 0; |
Vanger | 31:529db15abda7 | 413 | int closeType = 0; |
Vanger | 30:1326b623919a | 414 | |
Vanger | 30:1326b623919a | 415 | //1) Check that we do not have a live connection up |
Vanger | 30:1326b623919a | 416 | if(socketOpened) { |
Vanger | 30:1326b623919a | 417 | //Check that the address, port, and mode match |
Vanger | 30:1326b623919a | 418 | if(host_address != address || host_port != port || socketMode != mode) { |
Vanger | 30:1326b623919a | 419 | if(socketMode == TCP) { |
Vanger | 30:1326b623919a | 420 | logError("TCP socket already opened [%s:%d]", host_address.c_str(), host_port); |
Vanger | 30:1326b623919a | 421 | } else { |
Vanger | 30:1326b623919a | 422 | logError("UDP socket already opened [%s:%d]", host_address.c_str(), host_port); |
Vanger | 30:1326b623919a | 423 | } |
Vanger | 30:1326b623919a | 424 | return false; |
Vanger | 30:1326b623919a | 425 | } |
Vanger | 30:1326b623919a | 426 | |
Vanger | 30:1326b623919a | 427 | logDebug("Socket already opened"); |
Vanger | 30:1326b623919a | 428 | return true; |
Vanger | 30:1326b623919a | 429 | } |
Vanger | 30:1326b623919a | 430 | |
Vanger | 30:1326b623919a | 431 | //2) Check Parameters |
Vanger | 30:1326b623919a | 432 | if(port > 65535) { |
Vanger | 30:1326b623919a | 433 | logError("port out of range (0-65535)"); |
Vanger | 30:1326b623919a | 434 | return false; |
Vanger | 30:1326b623919a | 435 | } |
Vanger | 30:1326b623919a | 436 | |
Vanger | 30:1326b623919a | 437 | //3) Check PPP connection |
Vanger | 30:1326b623919a | 438 | if(!isConnected()) { |
Vanger | 30:1326b623919a | 439 | logError("PPP not established. Attempting to connect"); |
Vanger | 30:1326b623919a | 440 | if(!connect()) { |
Vanger | 30:1326b623919a | 441 | logError("PPP connection failed"); |
Vanger | 30:1326b623919a | 442 | return false; |
Vanger | 30:1326b623919a | 443 | } else { |
Vanger | 30:1326b623919a | 444 | logDebug("PPP connection established"); |
Vanger | 30:1326b623919a | 445 | } |
Vanger | 30:1326b623919a | 446 | } |
Vanger | 30:1326b623919a | 447 | //No way to "set" port except on socket call; |
Vanger | 30:1326b623919a | 448 | //Going to need to warn if local_port was not set. |
Vanger | 30:1326b623919a | 449 | if(!local_port) { |
Vanger | 30:1326b623919a | 450 | logDebug("No local port was set: 0"); |
Vanger | 30:1326b623919a | 451 | } |
Vanger | 30:1326b623919a | 452 | |
Vanger | 30:1326b623919a | 453 | //4) Set escape sequence to not be transmitted |
Vanger | 30:1326b623919a | 454 | if(sendBasicCommand("AT#SKIPESC=1", 2000) != MTS_SUCCESS) { |
Vanger | 30:1326b623919a | 455 | logWarning("Failed to disable escape sequence transmission on data mode suspension"); |
Vanger | 30:1326b623919a | 456 | } |
Vanger | 30:1326b623919a | 457 | |
Vanger | 30:1326b623919a | 458 | if(mode == TCP) { |
Vanger | 30:1326b623919a | 459 | typeSocket = 0; |
Vanger | 30:1326b623919a | 460 | sMode = "TCP"; |
Vanger | 30:1326b623919a | 461 | } else { |
Vanger | 30:1326b623919a | 462 | typeSocket = 1; |
Vanger | 30:1326b623919a | 463 | sMode = "UDP"; |
Vanger | 30:1326b623919a | 464 | } |
Vanger | 31:529db15abda7 | 465 | |
Vanger | 31:529db15abda7 | 466 | if(socketCloseable) { |
Vanger | 31:529db15abda7 | 467 | closeType = 0; |
Vanger | 31:529db15abda7 | 468 | } else { |
Vanger | 31:529db15abda7 | 469 | closeType = 255; |
Vanger | 31:529db15abda7 | 470 | } |
Vanger | 31:529db15abda7 | 471 | //5) Open Socket |
Vanger | 31:529db15abda7 | 472 | sprintf(sOpenSocketCmd, "AT#SD=1,%d,%d,%s,%d,%d,0", typeSocket, port, address.c_str(),closeType , local_port); |
Vanger | 30:1326b623919a | 473 | std::string response = sendCommand(sOpenSocketCmd, 5000); |
Vanger | 30:1326b623919a | 474 | |
Vanger | 30:1326b623919a | 475 | if(response.find("CONNECT") != std::string::npos) { |
Vanger | 30:1326b623919a | 476 | host_address = address; |
Vanger | 30:1326b623919a | 477 | host_port = port; |
Vanger | 30:1326b623919a | 478 | |
Vanger | 30:1326b623919a | 479 | logInfo("Opened %s Socket [%s:%d]", sMode.c_str(), address.c_str(), port); |
Vanger | 30:1326b623919a | 480 | socketOpened = true; |
Vanger | 30:1326b623919a | 481 | socketMode = mode; |
Vanger | 30:1326b623919a | 482 | } else { |
Vanger | 30:1326b623919a | 483 | logWarning("Unable to open %s Socket [%s:%d]", sMode.c_str(), address.c_str(), port); |
Vanger | 30:1326b623919a | 484 | socketOpened = false; |
Vanger | 30:1326b623919a | 485 | } |
Vanger | 30:1326b623919a | 486 | |
Vanger | 26:2b769ed8de4f | 487 | return socketOpened; |
Vanger | 26:2b769ed8de4f | 488 | } |
Vanger | 27:ec44d5a9544f | 489 | |
Vanger | 26:2b769ed8de4f | 490 | bool EasyIP::isOpen() |
Vanger | 26:2b769ed8de4f | 491 | { |
Vanger | 30:1326b623919a | 492 | if(io->readable()) { |
Vanger | 30:1326b623919a | 493 | logDebug("Assuming open, data available to read.\n\r"); |
Vanger | 30:1326b623919a | 494 | return true; |
Vanger | 30:1326b623919a | 495 | } |
Vanger | 26:2b769ed8de4f | 496 | return socketOpened; |
Vanger | 26:2b769ed8de4f | 497 | } |
Vanger | 27:ec44d5a9544f | 498 | |
Vanger | 26:2b769ed8de4f | 499 | bool EasyIP::close() |
Vanger | 26:2b769ed8de4f | 500 | { |
Vanger | 26:2b769ed8de4f | 501 | |
Vanger | 30:1326b623919a | 502 | if(io == NULL) { |
Vanger | 30:1326b623919a | 503 | logError("MTSBufferedIO not set"); |
Vanger | 30:1326b623919a | 504 | return false; |
Vanger | 30:1326b623919a | 505 | } |
Vanger | 30:1326b623919a | 506 | |
Vanger | 30:1326b623919a | 507 | if(!socketOpened) { |
Vanger | 30:1326b623919a | 508 | logWarning("Socket close() called, but socket was not open"); |
Vanger | 30:1326b623919a | 509 | return true; |
Vanger | 30:1326b623919a | 510 | } |
Vanger | 30:1326b623919a | 511 | |
Vanger | 30:1326b623919a | 512 | if(!socketCloseable) { |
Vanger | 30:1326b623919a | 513 | logError("Socket is not closeable"); |
Vanger | 30:1326b623919a | 514 | return false; |
Vanger | 30:1326b623919a | 515 | } |
Vanger | 30:1326b623919a | 516 | |
Vanger | 30:1326b623919a | 517 | if(!sendEscapeCommand()) { |
Vanger | 30:1326b623919a | 518 | logError("Failed to exit online mode"); |
Vanger | 30:1326b623919a | 519 | return false; |
Vanger | 30:1326b623919a | 520 | } else { |
Vanger | 30:1326b623919a | 521 | socketOpened = false; |
Vanger | 30:1326b623919a | 522 | } |
Vanger | 30:1326b623919a | 523 | |
Vanger | 30:1326b623919a | 524 | if(sendBasicCommand("AT#SH=1", 2000) != MTS_SUCCESS) { |
Vanger | 30:1326b623919a | 525 | logDebug("Failed to close socket connection"); |
Vanger | 30:1326b623919a | 526 | } |
Vanger | 30:1326b623919a | 527 | |
Vanger | 30:1326b623919a | 528 | Timer tmr; |
Vanger | 30:1326b623919a | 529 | int counter = 0; |
Vanger | 30:1326b623919a | 530 | char tmp[256]; |
Vanger | 30:1326b623919a | 531 | tmr.start(); |
Vanger | 30:1326b623919a | 532 | do { |
Vanger | 30:1326b623919a | 533 | if(socketOpened == false) { |
Vanger | 30:1326b623919a | 534 | break; |
Vanger | 30:1326b623919a | 535 | } |
Vanger | 30:1326b623919a | 536 | read(tmp, 256, 1000); |
Vanger | 30:1326b623919a | 537 | } while(counter++ < 10); |
Vanger | 30:1326b623919a | 538 | |
Vanger | 30:1326b623919a | 539 | io->rxClear(); |
Vanger | 30:1326b623919a | 540 | io->txClear(); |
Vanger | 30:1326b623919a | 541 | |
Vanger | 30:1326b623919a | 542 | return !socketOpened; |
Vanger | 26:2b769ed8de4f | 543 | } |
Vanger | 26:2b769ed8de4f | 544 | |
Vanger | 26:2b769ed8de4f | 545 | int EasyIP::read(char* data, int max, int timeout) |
Vanger | 26:2b769ed8de4f | 546 | { |
Vanger | 30:1326b623919a | 547 | if(io == NULL) { |
Vanger | 30:1326b623919a | 548 | logError("MTSBufferedIO not set"); |
Vanger | 30:1326b623919a | 549 | return -1; |
Vanger | 30:1326b623919a | 550 | } |
Vanger | 30:1326b623919a | 551 | |
Vanger | 30:1326b623919a | 552 | //Check that nothing is in the rx buffer |
Vanger | 30:1326b623919a | 553 | if(!socketOpened && !io->readable()) { |
Vanger | 30:1326b623919a | 554 | logError("Socket is not open"); |
Vanger | 30:1326b623919a | 555 | return -1; |
Vanger | 30:1326b623919a | 556 | } |
Vanger | 30:1326b623919a | 557 | |
Vanger | 30:1326b623919a | 558 | int bytesRead = 0; |
Vanger | 30:1326b623919a | 559 | |
Vanger | 30:1326b623919a | 560 | |
Vanger | 30:1326b623919a | 561 | if(timeout >= 0) { |
Vanger | 30:1326b623919a | 562 | bytesRead = io->read(data, max, static_cast<unsigned int>(timeout)); |
Vanger | 30:1326b623919a | 563 | } else { |
Vanger | 30:1326b623919a | 564 | bytesRead = io->read(data, max); |
Vanger | 30:1326b623919a | 565 | } |
Vanger | 31:529db15abda7 | 566 | if(bytesRead > 0 && socketCloseable) { |
Vanger | 31:529db15abda7 | 567 | //Scan for socket closed message |
Vanger | 31:529db15abda7 | 568 | for(size_t i = 0; i < bytesRead; i++) { |
Vanger | 31:529db15abda7 | 569 | if(data[i] == 'N') { |
Vanger | 31:529db15abda7 | 570 | if(strstr(&data[i], "NO CARRIER")) { |
Vanger | 31:529db15abda7 | 571 | logTrace("Found socket closed message. Checking validity"); |
Vanger | 31:529db15abda7 | 572 | //Close socket and Cut Off End of Message |
Vanger | 31:529db15abda7 | 573 | socketOpened = socketCheck(); //Verifies legitimacy of socket disconnect |
Vanger | 31:529db15abda7 | 574 | if(socketOpened) { |
Vanger | 31:529db15abda7 | 575 | logDebug("Socket still open"); |
Vanger | 31:529db15abda7 | 576 | continue; |
Vanger | 31:529db15abda7 | 577 | } else { |
Vanger | 31:529db15abda7 | 578 | logDebug("Socket closed"); |
Vanger | 31:529db15abda7 | 579 | data[i] = '\0'; |
Vanger | 31:529db15abda7 | 580 | bytesRead = i; |
Vanger | 31:529db15abda7 | 581 | break; |
Vanger | 31:529db15abda7 | 582 | } |
Vanger | 30:1326b623919a | 583 | } |
Vanger | 30:1326b623919a | 584 | } |
Vanger | 30:1326b623919a | 585 | } |
Vanger | 30:1326b623919a | 586 | } |
Vanger | 30:1326b623919a | 587 | return bytesRead; |
Vanger | 26:2b769ed8de4f | 588 | } |
Vanger | 26:2b769ed8de4f | 589 | |
Vanger | 26:2b769ed8de4f | 590 | int EasyIP::write(const char* data, int length, int timeout) |
Vanger | 26:2b769ed8de4f | 591 | { |
Vanger | 30:1326b623919a | 592 | if(io == NULL) { |
Vanger | 30:1326b623919a | 593 | logError("MTSBufferedIO not set"); |
Vanger | 30:1326b623919a | 594 | return -1; |
Vanger | 30:1326b623919a | 595 | } |
Vanger | 30:1326b623919a | 596 | |
Vanger | 30:1326b623919a | 597 | if(!socketOpened) { |
Vanger | 30:1326b623919a | 598 | logError("Socket is not open"); |
Vanger | 30:1326b623919a | 599 | return -1; |
Vanger | 30:1326b623919a | 600 | } |
Vanger | 30:1326b623919a | 601 | |
Vanger | 30:1326b623919a | 602 | int bytesWritten = 0; |
Vanger | 31:529db15abda7 | 603 | int size = length; |
Vanger | 31:529db15abda7 | 604 | int failedWrites = 0; |
Vanger | 30:1326b623919a | 605 | if(timeout >= 0) { |
Vanger | 30:1326b623919a | 606 | Timer tmr; |
Vanger | 30:1326b623919a | 607 | tmr.start(); |
Vanger | 30:1326b623919a | 608 | do { |
Vanger | 30:1326b623919a | 609 | int available = io->writeable(); |
Vanger | 30:1326b623919a | 610 | if (available > 0) { |
Vanger | 31:529db15abda7 | 611 | size = MIN(available, length - bytesWritten); |
Vanger | 31:529db15abda7 | 612 | bytesWritten += io->write(&data[bytesWritten], size); |
Vanger | 30:1326b623919a | 613 | } else { |
Vanger | 30:1326b623919a | 614 | wait(0.05); |
Vanger | 30:1326b623919a | 615 | } |
Vanger | 30:1326b623919a | 616 | } while (tmr.read_ms() <= timeout && bytesWritten < length); |
Vanger | 30:1326b623919a | 617 | } else { |
Vanger | 31:529db15abda7 | 618 | //If timeout is -1: |
Vanger | 31:529db15abda7 | 619 | do { |
Vanger | 31:529db15abda7 | 620 | int available = io->writeable(); |
Vanger | 31:529db15abda7 | 621 | if(available > 0) { |
Vanger | 31:529db15abda7 | 622 | size = MIN(available, length - bytesWritten); |
Vanger | 31:529db15abda7 | 623 | int currentWritten = io->write(&data[bytesWritten], size); |
Vanger | 31:529db15abda7 | 624 | bytesWritten += currentWritten; |
Vanger | 31:529db15abda7 | 625 | if(!currentWritten) { |
Vanger | 31:529db15abda7 | 626 | failedWrites++; |
Vanger | 31:529db15abda7 | 627 | } |
Vanger | 31:529db15abda7 | 628 | if(failedWrites > 10) { |
Vanger | 31:529db15abda7 | 629 | logError("Couldn't write any characters"); |
Vanger | 31:529db15abda7 | 630 | return bytesWritten; |
Vanger | 31:529db15abda7 | 631 | } |
Vanger | 31:529db15abda7 | 632 | } else { |
Vanger | 31:529db15abda7 | 633 | wait(0.05); |
Vanger | 30:1326b623919a | 634 | } |
Vanger | 31:529db15abda7 | 635 | } while (bytesWritten < length); |
Vanger | 30:1326b623919a | 636 | } |
Vanger | 30:1326b623919a | 637 | return bytesWritten; |
Vanger | 26:2b769ed8de4f | 638 | } |
Vanger | 26:2b769ed8de4f | 639 | |
Vanger | 26:2b769ed8de4f | 640 | unsigned int EasyIP::readable() |
Vanger | 26:2b769ed8de4f | 641 | { |
Vanger | 30:1326b623919a | 642 | if(io == NULL) { |
Vanger | 30:1326b623919a | 643 | logWarning("MTSBufferedIO not set"); |
Vanger | 30:1326b623919a | 644 | return 0; |
Vanger | 30:1326b623919a | 645 | } |
Vanger | 30:1326b623919a | 646 | if(!socketOpened && !io->readable()) { |
Vanger | 30:1326b623919a | 647 | logWarning("Socket is not open"); |
Vanger | 30:1326b623919a | 648 | return 0; |
Vanger | 30:1326b623919a | 649 | } |
Vanger | 26:2b769ed8de4f | 650 | return io->readable(); |
Vanger | 26:2b769ed8de4f | 651 | } |
Vanger | 26:2b769ed8de4f | 652 | |
Vanger | 26:2b769ed8de4f | 653 | unsigned int EasyIP::writeable() |
Vanger | 26:2b769ed8de4f | 654 | { |
Vanger | 30:1326b623919a | 655 | if(io == NULL) { |
Vanger | 30:1326b623919a | 656 | logWarning("MTSBufferedIO not set"); |
Vanger | 30:1326b623919a | 657 | return 0; |
Vanger | 30:1326b623919a | 658 | } |
Vanger | 30:1326b623919a | 659 | if(!socketOpened) { |
Vanger | 30:1326b623919a | 660 | logWarning("Socket is not open"); |
Vanger | 30:1326b623919a | 661 | return 0; |
Vanger | 30:1326b623919a | 662 | } |
Vanger | 26:2b769ed8de4f | 663 | |
Vanger | 26:2b769ed8de4f | 664 | return io->writeable(); |
Vanger | 26:2b769ed8de4f | 665 | } |
Vanger | 26:2b769ed8de4f | 666 | |
Vanger | 26:2b769ed8de4f | 667 | bool EasyIP::setDeviceIP(std::string address) |
Vanger | 26:2b769ed8de4f | 668 | { |
Vanger | 26:2b769ed8de4f | 669 | if (address.compare("DHCP") == 0) { |
Vanger | 26:2b769ed8de4f | 670 | return true; |
Vanger | 26:2b769ed8de4f | 671 | } else { |
Vanger | 26:2b769ed8de4f | 672 | logWarning("Radio does not support static IPs, using DHCP.\n\r"); |
Vanger | 26:2b769ed8de4f | 673 | return false; |
Vanger | 26:2b769ed8de4f | 674 | } |
Vanger | 26:2b769ed8de4f | 675 | } |
Vanger | 26:2b769ed8de4f | 676 | |
Vanger | 26:2b769ed8de4f | 677 | Code EasyIP::setApn(const std::string& apn) |
Vanger | 26:2b769ed8de4f | 678 | { |
Vanger | 26:2b769ed8de4f | 679 | if (type == MTSMC_H5 || type == MTSMC_G3) { |
Vanger | 26:2b769ed8de4f | 680 | //Set IP,PPP,IPv6 |
Vanger | 26:2b769ed8de4f | 681 | Code code = sendBasicCommand("AT+CGDCONT=1,PPP," + apn, 1000); |
Vanger | 26:2b769ed8de4f | 682 | if (code != MTS_SUCCESS) { |
Vanger | 26:2b769ed8de4f | 683 | return code; |
Vanger | 26:2b769ed8de4f | 684 | } |
Vanger | 26:2b769ed8de4f | 685 | this->apn = apn; |
Vanger | 26:2b769ed8de4f | 686 | return code; //This will return MTS_SUCCESS |
Vanger | 26:2b769ed8de4f | 687 | } else { |
Vanger | 26:2b769ed8de4f | 688 | logInfo("CDMA radios don't need an APN"); |
Vanger | 26:2b769ed8de4f | 689 | return MTS_SUCCESS; |
Vanger | 26:2b769ed8de4f | 690 | } |
Vanger | 26:2b769ed8de4f | 691 | } |
Vanger | 27:ec44d5a9544f | 692 | |
Vanger | 26:2b769ed8de4f | 693 | void EasyIP::reset() |
Vanger | 26:2b769ed8de4f | 694 | { |
Vanger | 30:1326b623919a | 695 | disconnect(); |
Vanger | 30:1326b623919a | 696 | if(sendBasicCommand("AT#REBOOT", 10000) != MTS_SUCCESS) { |
Vanger | 30:1326b623919a | 697 | logError("Socket Modem did not accept RESET command\n\r"); |
Vanger | 30:1326b623919a | 698 | } else { |
Vanger | 30:1326b623919a | 699 | logWarning("Socket Modem is resetting, allow 30 seconds for it to come back\n\r"); |
Vanger | 30:1326b623919a | 700 | return; |
Vanger | 30:1326b623919a | 701 | } |
Vanger | 26:2b769ed8de4f | 702 | } |
Vanger | 26:2b769ed8de4f | 703 | |
Vanger | 26:2b769ed8de4f | 704 | std::string EasyIP::getDeviceIP() |
Vanger | 26:2b769ed8de4f | 705 | { |
Vanger | 26:2b769ed8de4f | 706 | return local_address; |
Vanger | 26:2b769ed8de4f | 707 | } |
Vanger | 26:2b769ed8de4f | 708 | |
Vanger | 26:2b769ed8de4f | 709 | //Turns off echo when it receives a 1, turns on when it receives anything else |
Vanger | 26:2b769ed8de4f | 710 | Code EasyIP::echo(bool state) |
Vanger | 26:2b769ed8de4f | 711 | { |
Vanger | 27:ec44d5a9544f | 712 | Code code; |
Vanger | 27:ec44d5a9544f | 713 | if (state) { |
Vanger | 27:ec44d5a9544f | 714 | code = sendBasicCommand("ATE0", 1000); |
Vanger | 27:ec44d5a9544f | 715 | echoMode = (code == MTS_SUCCESS) ? false : echoMode; |
Vanger | 27:ec44d5a9544f | 716 | } else { |
Vanger | 27:ec44d5a9544f | 717 | code = sendBasicCommand("ATE1", 1000); |
Vanger | 27:ec44d5a9544f | 718 | echoMode = (code == MTS_SUCCESS) ? true : echoMode; |
Vanger | 27:ec44d5a9544f | 719 | } |
Vanger | 27:ec44d5a9544f | 720 | return code; |
Vanger | 26:2b769ed8de4f | 721 | } |
Vanger | 26:2b769ed8de4f | 722 | |
Vanger | 26:2b769ed8de4f | 723 | bool EasyIP::ping(const std::string& address) |
Vanger | 26:2b769ed8de4f | 724 | { |
Vanger | 26:2b769ed8de4f | 725 | char buffer[256] = {0}; |
Vanger | 27:ec44d5a9544f | 726 | std::vector<std::string> parts; |
Vanger | 27:ec44d5a9544f | 727 | int pingsRec=0; |
Vanger | 27:ec44d5a9544f | 728 | int TTL=0; |
Vanger | 27:ec44d5a9544f | 729 | int Timeout=0; |
Vanger | 27:ec44d5a9544f | 730 | |
Vanger | 27:ec44d5a9544f | 731 | //Format parameters for sending to radio |
Vanger | 30:1326b623919a | 732 | sprintf(buffer, "AT#PING=%s,1,32,%d", address.c_str(), (PINGDELAY*10)); |
Vanger | 26:2b769ed8de4f | 733 | |
Vanger | 27:ec44d5a9544f | 734 | for(int pngs=0; pngs<PINGNUM; pngs++) { |
Vanger | 30:1326b623919a | 735 | std::string response = sendCommand(buffer, (PINGDELAY*1010)); //Send 1 ping |
Vanger | 28:f93d7b3f7c2e | 736 | if(response.empty()) { |
Vanger | 28:f93d7b3f7c2e | 737 | continue; //Skip current loop if send command fails |
Vanger | 28:f93d7b3f7c2e | 738 | } |
Vanger | 28:f93d7b3f7c2e | 739 | if(response.find("ERROR") != std::string::npos) { |
Vanger | 28:f93d7b3f7c2e | 740 | continue; //Skip current loop if send command fails |
Vanger | 28:f93d7b3f7c2e | 741 | } |
Vanger | 27:ec44d5a9544f | 742 | parts = Text::split(response, "\r\n"); |
Vanger | 28:f93d7b3f7c2e | 743 | if(parts.size() < 2) { |
Vanger | 28:f93d7b3f7c2e | 744 | continue; |
Vanger | 28:f93d7b3f7c2e | 745 | } |
Vanger | 27:ec44d5a9544f | 746 | parts = Text::split(parts[1], ","); |
Vanger | 28:f93d7b3f7c2e | 747 | if(parts.size() < 4) { |
Vanger | 28:f93d7b3f7c2e | 748 | continue; |
Vanger | 28:f93d7b3f7c2e | 749 | } |
Vanger | 27:ec44d5a9544f | 750 | //Parse TTL and Timeout values |
Vanger | 27:ec44d5a9544f | 751 | Timeout = std::atoi(parts[2].c_str()); |
Vanger | 27:ec44d5a9544f | 752 | TTL = std::atoi(parts[3].c_str()); |
Vanger | 27:ec44d5a9544f | 753 | |
Vanger | 27:ec44d5a9544f | 754 | if((Timeout < 600) && (TTL < 255)) { |
Vanger | 27:ec44d5a9544f | 755 | pingsRec++; |
Vanger | 27:ec44d5a9544f | 756 | } |
Vanger | 27:ec44d5a9544f | 757 | } //Success if less than 50% packet loss |
Vanger | 27:ec44d5a9544f | 758 | if( ((pingsRec/PINGNUM)>= 0.5) ) { |
Vanger | 27:ec44d5a9544f | 759 | return true; |
Vanger | 27:ec44d5a9544f | 760 | } |
Vanger | 26:2b769ed8de4f | 761 | return false; |
Vanger | 26:2b769ed8de4f | 762 | } |
Vanger | 26:2b769ed8de4f | 763 | |
Vanger | 27:ec44d5a9544f | 764 | //Pass 1 to enable socket closeable |
Vanger | 27:ec44d5a9544f | 765 | //Pass 0 to disable socket closeable |
Vanger | 26:2b769ed8de4f | 766 | Code EasyIP::setSocketCloseable(bool enabled) |
Vanger | 26:2b769ed8de4f | 767 | { |
Vanger | 31:529db15abda7 | 768 | if(socketCloseable == enabled) { |
Vanger | 31:529db15abda7 | 769 | return MTS_SUCCESS; |
Vanger | 31:529db15abda7 | 770 | } |
Vanger | 31:529db15abda7 | 771 | |
Vanger | 31:529db15abda7 | 772 | if(socketOpened) { |
Vanger | 31:529db15abda7 | 773 | logError("socket is already opened. Can not set closeable"); |
Vanger | 31:529db15abda7 | 774 | return MTS_ERROR; |
Vanger | 31:529db15abda7 | 775 | } |
Vanger | 31:529db15abda7 | 776 | |
Vanger | 31:529db15abda7 | 777 | socketCloseable = enabled; |
Vanger | 31:529db15abda7 | 778 | |
Vanger | 26:2b769ed8de4f | 779 | return MTS_SUCCESS; |
Vanger | 26:2b769ed8de4f | 780 | } |