Simple detection for LE910-NA1 modules

Fork of MTS-Cellular by MultiTech

Committer:
Vanger
Date:
Mon Jul 21 20:14:18 2014 +0000
Revision:
35:257eb41405e1
Parent:
33:3b6f3904dde0
Child:
38:b2088faa8bfd
Child:
39:629fc8475af1
Cellular.cpp   Fixed char-delay causing failed SMS.; EasyIP.cpp   Added closed-connection checking.; TestSMS.h   Added SIM-not-ready checking loop before starting SMS test.

Who changed what in which revision?

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