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