Cellular library for MTS Socket Modem Arduino Shield devices from Multi-Tech Systems

Dependents:   mtsas mtsas mtsas mtsas

Committer:
mfiore
Date:
Mon Jun 16 13:50:28 2014 +0000
Revision:
18:fa0d8120f81f
Parent:
17:dee902f7d00e
Child:
19:f6261f1c3dd4
update SUCCESS, FAILURE, and friends to MTS_* so they don't conflict with definitions for STMicro boards

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Mike Fiore 3:04046eebaef5 1 #include "mbed.h"
Mike Fiore 3:04046eebaef5 2 #include "UIP.h"
Mike Fiore 1:f155d94d6f3a 3 #include "MTSText.h"
Mike Fiore 4:1f63354b8d1b 4 #include "MTSLog.h"
Mike Fiore 2:10e72dce251d 5 #include "CellUtils.h"
Mike Fiore 1:f155d94d6f3a 6
Mike Fiore 1:f155d94d6f3a 7 using namespace mts;
Mike Fiore 1:f155d94d6f3a 8
Mike Fiore 11:4e428f689069 9 UIP::UIP(Radio type)
Mike Fiore 1:f155d94d6f3a 10 {
Mike Fiore 11:4e428f689069 11 this->type = type;
Mike Fiore 3:04046eebaef5 12 io = NULL;
Mike Fiore 9:1a03e3f3e7fe 13 dcd = NULL;
Mike Fiore 9:1a03e3f3e7fe 14 dtr = NULL;
Mike Fiore 9:1a03e3f3e7fe 15 resetLine = NULL;
Mike Fiore 3:04046eebaef5 16 echoMode = true;
Mike Fiore 3:04046eebaef5 17 pppConnected = false;
Mike Fiore 3:04046eebaef5 18 mode = TCP;
Mike Fiore 3:04046eebaef5 19 socketOpened = false;
Mike Fiore 3:04046eebaef5 20 socketCloseable = true;
Mike Fiore 3:04046eebaef5 21 local_port = 0;
Mike Fiore 3:04046eebaef5 22 local_address = "";
Mike Fiore 3:04046eebaef5 23 host_port = 0;
Mike Fiore 1:f155d94d6f3a 24 }
Mike Fiore 1:f155d94d6f3a 25
Mike Fiore 3:04046eebaef5 26 UIP::~UIP()
Mike Fiore 1:f155d94d6f3a 27 {
Mike Fiore 1:f155d94d6f3a 28 if (dtr != NULL) {
Mike Fiore 1:f155d94d6f3a 29 dtr->write(1);
Mike Fiore 1:f155d94d6f3a 30 }
Mike Fiore 1:f155d94d6f3a 31
Mike Fiore 1:f155d94d6f3a 32 delete dcd;
Mike Fiore 1:f155d94d6f3a 33 delete dtr;
Mike Fiore 1:f155d94d6f3a 34 delete resetLine;
Mike Fiore 1:f155d94d6f3a 35 }
Mike Fiore 1:f155d94d6f3a 36
Mike Fiore 3:04046eebaef5 37 bool UIP::init(MTSBufferedIO* io)
Mike Fiore 1:f155d94d6f3a 38 {
Mike Fiore 8:2d7259d244d1 39 if (! Cellular::init(io)) {
Mike Fiore 1:f155d94d6f3a 40 return false;
Mike Fiore 1:f155d94d6f3a 41 }
Mike Fiore 1:f155d94d6f3a 42
mfiore 17:dee902f7d00e 43 logDebug("radio type: %s", Cellular::getRadioNames(type).c_str());
Mike Fiore 8:2d7259d244d1 44 return true;
Mike Fiore 1:f155d94d6f3a 45 }
Mike Fiore 1:f155d94d6f3a 46
Mike Fiore 3:04046eebaef5 47 bool UIP::connect()
Mike Fiore 1:f155d94d6f3a 48 {
Mike Fiore 1:f155d94d6f3a 49 //Check if socket is open
Mike Fiore 1:f155d94d6f3a 50 if(socketOpened) {
Mike Fiore 1:f155d94d6f3a 51 return true;
Mike Fiore 1:f155d94d6f3a 52 }
Mike Fiore 1:f155d94d6f3a 53
Mike Fiore 1:f155d94d6f3a 54 //Check if already connected
Mike Fiore 1:f155d94d6f3a 55 if(isConnected()) {
Mike Fiore 1:f155d94d6f3a 56 return true;
Mike Fiore 1:f155d94d6f3a 57 }
Mike Fiore 1:f155d94d6f3a 58
Mike Fiore 1:f155d94d6f3a 59 Timer tmr;
Mike Fiore 1:f155d94d6f3a 60
Mike Fiore 1:f155d94d6f3a 61 //Check Registration: AT+CREG? == 0,1
Mike Fiore 1:f155d94d6f3a 62 tmr.start();
Mike Fiore 1:f155d94d6f3a 63 do {
Mike Fiore 1:f155d94d6f3a 64 Registration registration = getRegistration();
Mike Fiore 1:f155d94d6f3a 65 if(registration != REGISTERED) {
mfiore 15:700a7662c52e 66 logTrace("Not Registered [%d] ... waiting", (int)registration);
Mike Fiore 1:f155d94d6f3a 67 wait(1);
Mike Fiore 1:f155d94d6f3a 68 } else {
Mike Fiore 1:f155d94d6f3a 69 break;
Mike Fiore 1:f155d94d6f3a 70 }
Mike Fiore 1:f155d94d6f3a 71 } while(tmr.read() < 30);
Mike Fiore 1:f155d94d6f3a 72
Mike Fiore 1:f155d94d6f3a 73 //Check RSSI: AT+CSQ
Mike Fiore 1:f155d94d6f3a 74 tmr.reset();
Mike Fiore 1:f155d94d6f3a 75 do {
Mike Fiore 1:f155d94d6f3a 76 int rssi = getSignalStrength();
Mike Fiore 4:1f63354b8d1b 77 logDebug("Signal strength: %d", rssi);
Mike Fiore 1:f155d94d6f3a 78 if(rssi == 99) {
mfiore 15:700a7662c52e 79 logTrace("No Signal ... waiting");
Mike Fiore 1:f155d94d6f3a 80 wait(1);
Mike Fiore 1:f155d94d6f3a 81 } else {
Mike Fiore 1:f155d94d6f3a 82 break;
Mike Fiore 1:f155d94d6f3a 83 }
Mike Fiore 1:f155d94d6f3a 84 } while(tmr.read() < 30);
Mike Fiore 1:f155d94d6f3a 85
Mike Fiore 1:f155d94d6f3a 86 //AT#CONNECTIONSTART: Make a PPP connection
Mike Fiore 13:e443e7e2b605 87 if (type == MTSMC_H5_IP) {
Mike Fiore 13:e443e7e2b605 88 logDebug("Making PPP Connection Attempt. APN[%s]", apn.c_str());
Mike Fiore 13:e443e7e2b605 89 } else {
Mike Fiore 13:e443e7e2b605 90 logDebug("Making PPP Connection Attempt");
Mike Fiore 13:e443e7e2b605 91 }
Mike Fiore 1:f155d94d6f3a 92 std::string pppResult = sendCommand("AT#CONNECTIONSTART", 120000);
Mike Fiore 1:f155d94d6f3a 93 std::vector<std::string> parts = Text::split(pppResult, "\r\n");
Mike Fiore 1:f155d94d6f3a 94
Mike Fiore 1:f155d94d6f3a 95 if(pppResult.find("Ok_Info_GprsActivation") != std::string::npos) {
Mike Fiore 1:f155d94d6f3a 96 if(parts.size() >= 2) {
Mike Fiore 1:f155d94d6f3a 97 local_address = parts[1];
Mike Fiore 1:f155d94d6f3a 98 }
Mike Fiore 4:1f63354b8d1b 99 logInfo("PPP Connection Established: IP[%s]", local_address.c_str());
Mike Fiore 1:f155d94d6f3a 100 pppConnected = true;
Mike Fiore 1:f155d94d6f3a 101
Mike Fiore 1:f155d94d6f3a 102 } else {
Mike Fiore 1:f155d94d6f3a 103 pppConnected = false;
Mike Fiore 1:f155d94d6f3a 104 }
Mike Fiore 1:f155d94d6f3a 105
Mike Fiore 1:f155d94d6f3a 106 return pppConnected;
Mike Fiore 1:f155d94d6f3a 107 }
Mike Fiore 1:f155d94d6f3a 108
Mike Fiore 3:04046eebaef5 109 void UIP::disconnect()
Mike Fiore 1:f155d94d6f3a 110 {
Mike Fiore 1:f155d94d6f3a 111 //AT#CONNECTIONSTOP: Close a PPP connection
Mike Fiore 4:1f63354b8d1b 112 logDebug("Closing PPP Connection");
Mike Fiore 1:f155d94d6f3a 113
Mike Fiore 1:f155d94d6f3a 114 if(socketOpened) {
Mike Fiore 1:f155d94d6f3a 115 close();
Mike Fiore 1:f155d94d6f3a 116 }
Mike Fiore 1:f155d94d6f3a 117
Mike Fiore 1:f155d94d6f3a 118 Code code = sendBasicCommand("AT#CONNECTIONSTOP", 10000);
mfiore 18:fa0d8120f81f 119 if(code == MTS_SUCCESS) {
Mike Fiore 4:1f63354b8d1b 120 logDebug("Successfully closed PPP Connection");
Mike Fiore 1:f155d94d6f3a 121 } else {
Mike Fiore 4:1f63354b8d1b 122 logError("Closing PPP Connection [%d]. Continuing ...", (int)code);
Mike Fiore 1:f155d94d6f3a 123 }
Mike Fiore 1:f155d94d6f3a 124
Mike Fiore 1:f155d94d6f3a 125 pppConnected = false;
Mike Fiore 1:f155d94d6f3a 126 }
Mike Fiore 1:f155d94d6f3a 127
Mike Fiore 3:04046eebaef5 128 bool UIP::isConnected()
Mike Fiore 1:f155d94d6f3a 129 {
Mike Fiore 13:e443e7e2b605 130 //1) Check if APN was set if we're on a HSPA radio
Mike Fiore 13:e443e7e2b605 131 if (type == MTSMC_H5_IP) {
Mike Fiore 13:e443e7e2b605 132 if(apn.size() == 0) {
Mike Fiore 13:e443e7e2b605 133 logDebug("APN is not set");
Mike Fiore 13:e443e7e2b605 134 return false;
Mike Fiore 13:e443e7e2b605 135 }
Mike Fiore 1:f155d94d6f3a 136 }
Mike Fiore 1:f155d94d6f3a 137
Mike Fiore 13:e443e7e2b605 138 //2) Check that we do not have a live connection up
Mike Fiore 1:f155d94d6f3a 139 if(socketOpened) {
Mike Fiore 4:1f63354b8d1b 140 logDebug("Socket is opened");
Mike Fiore 1:f155d94d6f3a 141 return true;
Mike Fiore 1:f155d94d6f3a 142 }
Mike Fiore 13:e443e7e2b605 143 //3) Query the radio
Mike Fiore 1:f155d94d6f3a 144 std::string result = sendCommand("AT#VSTATE", 3000);
Mike Fiore 1:f155d94d6f3a 145 if(result.find("CONNECTED") != std::string::npos) {
Mike Fiore 1:f155d94d6f3a 146 if(pppConnected == false) {
Mike Fiore 4:1f63354b8d1b 147 logWarning("Internal PPP state tracking differs from radio (DISCONNECTED:CONNECTED)");
Mike Fiore 1:f155d94d6f3a 148 }
Mike Fiore 1:f155d94d6f3a 149 pppConnected = true;
Mike Fiore 1:f155d94d6f3a 150 } else {
Mike Fiore 1:f155d94d6f3a 151 if(pppConnected == true) {
Mike Fiore 1:f155d94d6f3a 152 //Find out what state is
Mike Fiore 1:f155d94d6f3a 153 size_t pos = result.find("STATE:");
Mike Fiore 1:f155d94d6f3a 154 if(pos != std::string::npos) {
Mike Fiore 1:f155d94d6f3a 155 result = Text::getLine(result, pos + sizeof("STATE:"), pos);
Mike Fiore 4:1f63354b8d1b 156 logWarning("Internal PPP state tracking differs from radio (CONNECTED:%s)", result.c_str());
Mike Fiore 1:f155d94d6f3a 157 } else {
Mike Fiore 4:1f63354b8d1b 158 logError("Unable to parse radio state: [%s]", result.c_str());
Mike Fiore 1:f155d94d6f3a 159 }
Mike Fiore 1:f155d94d6f3a 160
Mike Fiore 1:f155d94d6f3a 161 }
Mike Fiore 1:f155d94d6f3a 162 pppConnected = false;
Mike Fiore 1:f155d94d6f3a 163 }
Mike Fiore 1:f155d94d6f3a 164
Mike Fiore 1:f155d94d6f3a 165 return pppConnected;
Mike Fiore 1:f155d94d6f3a 166 }
Mike Fiore 1:f155d94d6f3a 167
Mike Fiore 3:04046eebaef5 168 bool UIP::bind(unsigned int port)
Mike Fiore 1:f155d94d6f3a 169 {
Mike Fiore 1:f155d94d6f3a 170 if(socketOpened) {
Mike Fiore 4:1f63354b8d1b 171 logError("socket is open. Can not set local port");
Mike Fiore 1:f155d94d6f3a 172 return false;
Mike Fiore 1:f155d94d6f3a 173 }
Mike Fiore 1:f155d94d6f3a 174 if(port > 65535) {
Mike Fiore 4:1f63354b8d1b 175 logError("port out of range (0-65535)");
Mike Fiore 1:f155d94d6f3a 176 return false;
Mike Fiore 1:f155d94d6f3a 177 }
Mike Fiore 1:f155d94d6f3a 178 local_port = port;
Mike Fiore 1:f155d94d6f3a 179 return true;
Mike Fiore 1:f155d94d6f3a 180 }
Mike Fiore 1:f155d94d6f3a 181
Mike Fiore 3:04046eebaef5 182 bool UIP::open(const std::string& address, unsigned int port, Mode mode)
Mike Fiore 1:f155d94d6f3a 183 {
Mike Fiore 1:f155d94d6f3a 184 char buffer[256] = {0};
Mike Fiore 1:f155d94d6f3a 185 Code portCode, addressCode;
Mike Fiore 1:f155d94d6f3a 186
Mike Fiore 1:f155d94d6f3a 187 //1) Check that we do not have a live connection up
Mike Fiore 1:f155d94d6f3a 188 if(socketOpened) {
Mike Fiore 1:f155d94d6f3a 189 //Check that the address, port, and mode match
Mike Fiore 1:f155d94d6f3a 190 if(host_address != address || host_port != port || this->mode != mode) {
Mike Fiore 1:f155d94d6f3a 191 if(this->mode == TCP) {
Mike Fiore 4:1f63354b8d1b 192 logError("TCP socket already opened [%s:%d]", host_address.c_str(), host_port);
Mike Fiore 1:f155d94d6f3a 193 } else {
Mike Fiore 4:1f63354b8d1b 194 logError("UDP socket already opened [%s:%d]", host_address.c_str(), host_port);
Mike Fiore 1:f155d94d6f3a 195 }
Mike Fiore 1:f155d94d6f3a 196 return false;
Mike Fiore 1:f155d94d6f3a 197 }
Mike Fiore 1:f155d94d6f3a 198
Mike Fiore 4:1f63354b8d1b 199 logDebug("Socket already opened");
Mike Fiore 1:f155d94d6f3a 200 return true;
Mike Fiore 1:f155d94d6f3a 201 }
Mike Fiore 1:f155d94d6f3a 202
Mike Fiore 1:f155d94d6f3a 203 //2) Check Parameters
Mike Fiore 1:f155d94d6f3a 204 if(port > 65535) {
Mike Fiore 4:1f63354b8d1b 205 logError("port out of range (0-65535)");
Mike Fiore 1:f155d94d6f3a 206 return false;
Mike Fiore 1:f155d94d6f3a 207 }
Mike Fiore 1:f155d94d6f3a 208
Mike Fiore 1:f155d94d6f3a 209 //3) Check PPP connection
Mike Fiore 1:f155d94d6f3a 210 if(!isConnected()) {
Mike Fiore 4:1f63354b8d1b 211 logError("PPP not established. Attempting to connect");
Mike Fiore 1:f155d94d6f3a 212 if(!connect()) {
Mike Fiore 4:1f63354b8d1b 213 logError("PPP connection failed");
Mike Fiore 1:f155d94d6f3a 214 return false;
Mike Fiore 1:f155d94d6f3a 215 } else {
Mike Fiore 4:1f63354b8d1b 216 logDebug("PPP connection established");
Mike Fiore 1:f155d94d6f3a 217 }
Mike Fiore 1:f155d94d6f3a 218 }
Mike Fiore 1:f155d94d6f3a 219
Mike Fiore 1:f155d94d6f3a 220 //Set Local Port
Mike Fiore 1:f155d94d6f3a 221 if(local_port != 0) {
Mike Fiore 1:f155d94d6f3a 222 //Attempt to set local port
Mike Fiore 1:f155d94d6f3a 223 sprintf(buffer, "AT#OUTPORT=%d", local_port);
Mike Fiore 1:f155d94d6f3a 224 Code code = sendBasicCommand(buffer, 1000);
mfiore 18:fa0d8120f81f 225 if(code != MTS_SUCCESS) {
Mike Fiore 4:1f63354b8d1b 226 logWarning("Unable to set local port (%d) [%d]", local_port, (int) code);
Mike Fiore 1:f155d94d6f3a 227 }
Mike Fiore 1:f155d94d6f3a 228 }
Mike Fiore 1:f155d94d6f3a 229
Mike Fiore 1:f155d94d6f3a 230 //Set TCP/UDP parameters
Mike Fiore 1:f155d94d6f3a 231 if(mode == TCP) {
Mike Fiore 1:f155d94d6f3a 232 if(socketCloseable) {
Mike Fiore 1:f155d94d6f3a 233 Code code = sendBasicCommand("AT#DLEMODE=1,1", 1000);
mfiore 18:fa0d8120f81f 234 if(code != MTS_SUCCESS) {
Mike Fiore 4:1f63354b8d1b 235 logWarning("Unable to set socket closeable [%d]", (int) code);
Mike Fiore 1:f155d94d6f3a 236 }
Mike Fiore 1:f155d94d6f3a 237 }
Mike Fiore 1:f155d94d6f3a 238 sprintf(buffer, "AT#TCPPORT=1,%d", port);
Mike Fiore 1:f155d94d6f3a 239 portCode = sendBasicCommand(buffer, 1000);
Mike Fiore 1:f155d94d6f3a 240 addressCode = sendBasicCommand("AT#TCPSERV=1,\"" + address + "\"", 1000);
Mike Fiore 1:f155d94d6f3a 241 } else {
Mike Fiore 1:f155d94d6f3a 242 if(socketCloseable) {
Mike Fiore 1:f155d94d6f3a 243 Code code = sendBasicCommand("AT#UDPDLEMODE=1", 1000);
mfiore 18:fa0d8120f81f 244 if(code != MTS_SUCCESS) {
Mike Fiore 4:1f63354b8d1b 245 logWarning("Unable to set socket closeable [%d]", (int) code);
Mike Fiore 1:f155d94d6f3a 246 }
Mike Fiore 1:f155d94d6f3a 247 }
Mike Fiore 1:f155d94d6f3a 248 sprintf(buffer, "AT#UDPPORT=%d", port);
Mike Fiore 1:f155d94d6f3a 249 portCode = sendBasicCommand(buffer, 1000);
Mike Fiore 1:f155d94d6f3a 250 addressCode = sendBasicCommand("AT#UDPSERV=\"" + address + "\"", 1000);
Mike Fiore 1:f155d94d6f3a 251 }
Mike Fiore 1:f155d94d6f3a 252
mfiore 18:fa0d8120f81f 253 if(portCode == MTS_SUCCESS) {
Mike Fiore 1:f155d94d6f3a 254 host_port = port;
Mike Fiore 1:f155d94d6f3a 255 } else {
Mike Fiore 4:1f63354b8d1b 256 logError("Host port could not be set");
Mike Fiore 1:f155d94d6f3a 257 }
Mike Fiore 1:f155d94d6f3a 258
mfiore 18:fa0d8120f81f 259 if(addressCode == MTS_SUCCESS) {
Mike Fiore 1:f155d94d6f3a 260 host_address = address;
Mike Fiore 1:f155d94d6f3a 261 } else {
Mike Fiore 4:1f63354b8d1b 262 logError("Host address could not be set");
Mike Fiore 1:f155d94d6f3a 263 }
Mike Fiore 1:f155d94d6f3a 264
Mike Fiore 1:f155d94d6f3a 265 // Try and Connect
Mike Fiore 1:f155d94d6f3a 266 std::string sMode;
Mike Fiore 1:f155d94d6f3a 267 std::string sOpenSocketCmd;
Mike Fiore 1:f155d94d6f3a 268 if(mode == TCP) {
Mike Fiore 1:f155d94d6f3a 269 sOpenSocketCmd = "AT#OTCP=1";
Mike Fiore 1:f155d94d6f3a 270 sMode = "TCP";
Mike Fiore 1:f155d94d6f3a 271 } else {
Mike Fiore 1:f155d94d6f3a 272 sOpenSocketCmd = "AT#OUDP";
Mike Fiore 1:f155d94d6f3a 273 sMode = "UDP";
Mike Fiore 1:f155d94d6f3a 274 }
Mike Fiore 1:f155d94d6f3a 275
Mike Fiore 1:f155d94d6f3a 276 string response = sendCommand(sOpenSocketCmd, 30000);
Mike Fiore 1:f155d94d6f3a 277 if (response.find("Ok_Info_WaitingForData") != string::npos) {
Mike Fiore 4:1f63354b8d1b 278 logInfo("Opened %s Socket [%s:%d]", sMode.c_str(), address.c_str(), port);
Mike Fiore 1:f155d94d6f3a 279 socketOpened = true;
Mike Fiore 1:f155d94d6f3a 280 } else {
Mike Fiore 4:1f63354b8d1b 281 logWarning("Unable to open %s Socket [%s:%d]", sMode.c_str(), address.c_str(), port);
Mike Fiore 1:f155d94d6f3a 282 socketOpened = false;
Mike Fiore 1:f155d94d6f3a 283 }
Mike Fiore 1:f155d94d6f3a 284
Mike Fiore 1:f155d94d6f3a 285 return socketOpened;
Mike Fiore 1:f155d94d6f3a 286 }
Mike Fiore 1:f155d94d6f3a 287
Mike Fiore 3:04046eebaef5 288 bool UIP::isOpen()
Mike Fiore 1:f155d94d6f3a 289 {
Mike Fiore 1:f155d94d6f3a 290 if(io->readable()) {
Mike Fiore 4:1f63354b8d1b 291 logDebug("Assuming open, data available to read.\n\r");
Mike Fiore 1:f155d94d6f3a 292 return true;
Mike Fiore 1:f155d94d6f3a 293 }
Mike Fiore 1:f155d94d6f3a 294 return socketOpened;
Mike Fiore 1:f155d94d6f3a 295 }
Mike Fiore 1:f155d94d6f3a 296
Mike Fiore 3:04046eebaef5 297 bool UIP::close()
Mike Fiore 1:f155d94d6f3a 298 {
Mike Fiore 1:f155d94d6f3a 299 if(io == NULL) {
Mike Fiore 4:1f63354b8d1b 300 logError("MTSBufferedIO not set");
Mike Fiore 1:f155d94d6f3a 301 return false;
Mike Fiore 1:f155d94d6f3a 302 }
Mike Fiore 1:f155d94d6f3a 303
Mike Fiore 1:f155d94d6f3a 304 if(!socketOpened) {
Mike Fiore 4:1f63354b8d1b 305 logWarning("Socket close() called, but socket was not open");
Mike Fiore 1:f155d94d6f3a 306 return true;
Mike Fiore 1:f155d94d6f3a 307 }
Mike Fiore 1:f155d94d6f3a 308
Mike Fiore 1:f155d94d6f3a 309 if(!socketCloseable) {
Mike Fiore 4:1f63354b8d1b 310 logError("Socket is not closeable");
Mike Fiore 1:f155d94d6f3a 311 return false;
Mike Fiore 1:f155d94d6f3a 312 }
Mike Fiore 1:f155d94d6f3a 313
Mike Fiore 1:f155d94d6f3a 314
Mike Fiore 1:f155d94d6f3a 315
Mike Fiore 1:f155d94d6f3a 316 if(io->write(ETX, 1000) != 1) {
Mike Fiore 4:1f63354b8d1b 317 logError("Timed out attempting to close socket");
Mike Fiore 1:f155d94d6f3a 318 return false;
Mike Fiore 1:f155d94d6f3a 319 }
Mike Fiore 1:f155d94d6f3a 320
Mike Fiore 1:f155d94d6f3a 321 Timer tmr;
Mike Fiore 1:f155d94d6f3a 322 int counter = 0;
Mike Fiore 1:f155d94d6f3a 323 char tmp[256];
Mike Fiore 1:f155d94d6f3a 324 tmr.start();
Mike Fiore 1:f155d94d6f3a 325 do {
Mike Fiore 1:f155d94d6f3a 326 if(socketOpened == false) {
Mike Fiore 1:f155d94d6f3a 327 break;
Mike Fiore 1:f155d94d6f3a 328 }
Mike Fiore 1:f155d94d6f3a 329 read(tmp, 256, 1000);
Mike Fiore 1:f155d94d6f3a 330 } while(counter++ < 10);
Mike Fiore 1:f155d94d6f3a 331
Mike Fiore 1:f155d94d6f3a 332 io->rxClear();
Mike Fiore 1:f155d94d6f3a 333 io->txClear();
Mike Fiore 1:f155d94d6f3a 334
Mike Fiore 1:f155d94d6f3a 335 socketOpened = false;
Mike Fiore 1:f155d94d6f3a 336 return true;
Mike Fiore 1:f155d94d6f3a 337 }
Mike Fiore 1:f155d94d6f3a 338
Mike Fiore 3:04046eebaef5 339 int UIP::read(char* data, int max, int timeout)
Mike Fiore 1:f155d94d6f3a 340 {
Mike Fiore 1:f155d94d6f3a 341 if(io == NULL) {
Mike Fiore 4:1f63354b8d1b 342 logError("MTSBufferedIO not set");
Mike Fiore 1:f155d94d6f3a 343 return -1;
Mike Fiore 1:f155d94d6f3a 344 }
Mike Fiore 1:f155d94d6f3a 345
Mike Fiore 1:f155d94d6f3a 346 //Check that nothing is in the rx buffer
Mike Fiore 1:f155d94d6f3a 347 if(!socketOpened && !io->readable()) {
Mike Fiore 4:1f63354b8d1b 348 logError("Socket is not open");
Mike Fiore 1:f155d94d6f3a 349 return -1;
Mike Fiore 1:f155d94d6f3a 350 }
Mike Fiore 1:f155d94d6f3a 351
Mike Fiore 1:f155d94d6f3a 352 int bytesRead = 0;
Mike Fiore 1:f155d94d6f3a 353
Mike Fiore 1:f155d94d6f3a 354 if(timeout >= 0) {
Mike Fiore 1:f155d94d6f3a 355 bytesRead = io->read(data, max, static_cast<unsigned int>(timeout));
Mike Fiore 1:f155d94d6f3a 356 } else {
Mike Fiore 1:f155d94d6f3a 357 bytesRead = io->read(data, max);
Mike Fiore 1:f155d94d6f3a 358 }
Mike Fiore 1:f155d94d6f3a 359
Mike Fiore 1:f155d94d6f3a 360 if(bytesRead > 0 && socketCloseable) {
Mike Fiore 1:f155d94d6f3a 361 //Remove escape characters
Mike Fiore 1:f155d94d6f3a 362 int index = 0;
Mike Fiore 1:f155d94d6f3a 363 bool escapeFlag = false;
Mike Fiore 1:f155d94d6f3a 364 for(int i = 0; i < bytesRead; i++) {
Mike Fiore 1:f155d94d6f3a 365 if(data[i] == DLE || data[i] == ETX) {
Mike Fiore 1:f155d94d6f3a 366 if(escapeFlag == true) {
Mike Fiore 1:f155d94d6f3a 367 //This character has been escaped
Mike Fiore 1:f155d94d6f3a 368 escapeFlag = false;
Mike Fiore 1:f155d94d6f3a 369 } else if(data[bytesRead] == DLE) {
Mike Fiore 1:f155d94d6f3a 370 //Found escape character
Mike Fiore 1:f155d94d6f3a 371 escapeFlag = true;
Mike Fiore 1:f155d94d6f3a 372 continue;
Mike Fiore 1:f155d94d6f3a 373 } else {
Mike Fiore 1:f155d94d6f3a 374 //ETX sent without escape -> Socket closed
Mike Fiore 4:1f63354b8d1b 375 logInfo("Read ETX character without DLE escape. Socket closed");
Mike Fiore 1:f155d94d6f3a 376 socketOpened = false;
Mike Fiore 1:f155d94d6f3a 377 continue;
Mike Fiore 1:f155d94d6f3a 378 }
Mike Fiore 1:f155d94d6f3a 379 }
Mike Fiore 1:f155d94d6f3a 380
Mike Fiore 1:f155d94d6f3a 381 if(index != i) {
Mike Fiore 1:f155d94d6f3a 382 data[index] = data[i];
Mike Fiore 1:f155d94d6f3a 383 }
Mike Fiore 1:f155d94d6f3a 384 index++;
Mike Fiore 1:f155d94d6f3a 385 }
Mike Fiore 1:f155d94d6f3a 386 bytesRead = index;
Mike Fiore 1:f155d94d6f3a 387 }
Mike Fiore 1:f155d94d6f3a 388
Mike Fiore 1:f155d94d6f3a 389 //Scan for socket closed message
Mike Fiore 1:f155d94d6f3a 390 for(size_t i = 0; i < bytesRead; i++) {
Mike Fiore 1:f155d94d6f3a 391 if(data[i] == 'O') {
Mike Fiore 1:f155d94d6f3a 392 if(strstr(&data[i], "Ok_Info_SocketClosed")) {
Mike Fiore 4:1f63354b8d1b 393 logInfo("Found socket closed message. Socket closed");
Mike Fiore 1:f155d94d6f3a 394 //Close socket and Cut Off End of Message
Mike Fiore 1:f155d94d6f3a 395 socketOpened = false;
Mike Fiore 1:f155d94d6f3a 396 data[i] = '\0';
Mike Fiore 1:f155d94d6f3a 397 bytesRead = i;
Mike Fiore 1:f155d94d6f3a 398 break;
Mike Fiore 1:f155d94d6f3a 399 }
Mike Fiore 1:f155d94d6f3a 400 }
Mike Fiore 1:f155d94d6f3a 401 }
Mike Fiore 1:f155d94d6f3a 402 return bytesRead;
Mike Fiore 1:f155d94d6f3a 403 }
Mike Fiore 1:f155d94d6f3a 404
Mike Fiore 3:04046eebaef5 405 int UIP::write(const char* data, int length, int timeout)
Mike Fiore 1:f155d94d6f3a 406 {
Mike Fiore 1:f155d94d6f3a 407 if(io == NULL) {
Mike Fiore 4:1f63354b8d1b 408 logError("MTSBufferedIO not set");
Mike Fiore 1:f155d94d6f3a 409 return -1;
Mike Fiore 1:f155d94d6f3a 410 }
Mike Fiore 1:f155d94d6f3a 411
Mike Fiore 1:f155d94d6f3a 412 if(!socketOpened) {
Mike Fiore 4:1f63354b8d1b 413 logError("Socket is not open");
Mike Fiore 1:f155d94d6f3a 414 return -1;
Mike Fiore 1:f155d94d6f3a 415 }
Mike Fiore 1:f155d94d6f3a 416
Mike Fiore 1:f155d94d6f3a 417 //In order to avoid allocating another buffer, capture indices of
Mike Fiore 1:f155d94d6f3a 418 //characters to escape during write
Mike Fiore 1:f155d94d6f3a 419 int specialWritten = 0;
Mike Fiore 1:f155d94d6f3a 420 std::vector<int> vSpecial;
Mike Fiore 1:f155d94d6f3a 421 if(socketCloseable) {
Mike Fiore 1:f155d94d6f3a 422 for(int i = 0; i < length; i++) {
Mike Fiore 1:f155d94d6f3a 423 if(data[i] == ETX || data[i] == DLE) {
Mike Fiore 1:f155d94d6f3a 424 //Push back index of special characters
Mike Fiore 1:f155d94d6f3a 425 vSpecial.push_back(i);
Mike Fiore 1:f155d94d6f3a 426 }
Mike Fiore 1:f155d94d6f3a 427 }
Mike Fiore 1:f155d94d6f3a 428 }
Mike Fiore 1:f155d94d6f3a 429
Mike Fiore 1:f155d94d6f3a 430 int bytesWritten = 0;
Mike Fiore 1:f155d94d6f3a 431 if(timeout >= 0) {
Mike Fiore 1:f155d94d6f3a 432 Timer tmr;
Mike Fiore 1:f155d94d6f3a 433 tmr.start();
Mike Fiore 1:f155d94d6f3a 434 do {
Mike Fiore 1:f155d94d6f3a 435 int available = io->writeable();
Mike Fiore 1:f155d94d6f3a 436 if (available > 0) {
Mike Fiore 1:f155d94d6f3a 437 if(specialWritten < vSpecial.size()) {
Mike Fiore 1:f155d94d6f3a 438 //Check if current index is at a special character
Mike Fiore 1:f155d94d6f3a 439 if(bytesWritten == vSpecial[specialWritten]) {
Mike Fiore 1:f155d94d6f3a 440 if(available < 2) {
Mike Fiore 1:f155d94d6f3a 441 //Requires at least two bytes of space
Mike Fiore 1:f155d94d6f3a 442 wait(0.05);
Mike Fiore 1:f155d94d6f3a 443 continue;
Mike Fiore 1:f155d94d6f3a 444 }
Mike Fiore 1:f155d94d6f3a 445 //Ready to write special character
Mike Fiore 1:f155d94d6f3a 446 if(io->write(DLE)) {
Mike Fiore 1:f155d94d6f3a 447 specialWritten++;
Mike Fiore 1:f155d94d6f3a 448 if(io->write(data[bytesWritten])) {
Mike Fiore 1:f155d94d6f3a 449 bytesWritten++;
Mike Fiore 1:f155d94d6f3a 450 }
Mike Fiore 1:f155d94d6f3a 451 } else {
Mike Fiore 1:f155d94d6f3a 452 //Unable to write escape character, try again next round
Mike Fiore 1:f155d94d6f3a 453 wait(0.05);
Mike Fiore 1:f155d94d6f3a 454 }
Mike Fiore 1:f155d94d6f3a 455 } else {
Mike Fiore 1:f155d94d6f3a 456 //We want to write all the way up to the next special character
Mike Fiore 1:f155d94d6f3a 457 int relativeIndex = vSpecial[specialWritten] - bytesWritten;
Mike Fiore 1:f155d94d6f3a 458 int size = MIN(available, relativeIndex);
Mike Fiore 1:f155d94d6f3a 459 bytesWritten += io->write(&data[bytesWritten], size);
Mike Fiore 1:f155d94d6f3a 460 }
Mike Fiore 1:f155d94d6f3a 461 } else {
Mike Fiore 1:f155d94d6f3a 462 int size = MIN(available, length - bytesWritten);
Mike Fiore 1:f155d94d6f3a 463 bytesWritten += io->write(&data[bytesWritten], size);
Mike Fiore 1:f155d94d6f3a 464 }
Mike Fiore 1:f155d94d6f3a 465 } else {
Mike Fiore 1:f155d94d6f3a 466 wait(0.05);
Mike Fiore 1:f155d94d6f3a 467 }
Mike Fiore 1:f155d94d6f3a 468 } while (tmr.read_ms() <= timeout && bytesWritten < length);
Mike Fiore 1:f155d94d6f3a 469 } else {
Mike Fiore 1:f155d94d6f3a 470 for(int i = 0; i < vSpecial.size(); i++) {
Mike Fiore 1:f155d94d6f3a 471 //Write up to the special character, then write the special character
Mike Fiore 1:f155d94d6f3a 472 int size = vSpecial[i] - bytesWritten;
Mike Fiore 1:f155d94d6f3a 473 int currentWritten = io->write(&data[bytesWritten], size);
Mike Fiore 1:f155d94d6f3a 474 bytesWritten += currentWritten;
Mike Fiore 1:f155d94d6f3a 475 if(currentWritten != size) {
Mike Fiore 1:f155d94d6f3a 476 //Failed to write up to the special character.
Mike Fiore 1:f155d94d6f3a 477 return bytesWritten;
Mike Fiore 1:f155d94d6f3a 478 }
Mike Fiore 1:f155d94d6f3a 479 if(io->write(DLE) && io->write(data[bytesWritten])) {
Mike Fiore 1:f155d94d6f3a 480 bytesWritten++;
Mike Fiore 1:f155d94d6f3a 481 } else {
Mike Fiore 1:f155d94d6f3a 482 //Failed to write the special character.
Mike Fiore 1:f155d94d6f3a 483 return bytesWritten;
Mike Fiore 1:f155d94d6f3a 484 }
Mike Fiore 1:f155d94d6f3a 485 }
Mike Fiore 1:f155d94d6f3a 486
Mike Fiore 1:f155d94d6f3a 487 bytesWritten = io->write(&data[bytesWritten], length - bytesWritten);
Mike Fiore 1:f155d94d6f3a 488 }
Mike Fiore 1:f155d94d6f3a 489
Mike Fiore 1:f155d94d6f3a 490 return bytesWritten;
Mike Fiore 1:f155d94d6f3a 491 }
Mike Fiore 1:f155d94d6f3a 492
Mike Fiore 3:04046eebaef5 493 unsigned int UIP::readable()
Mike Fiore 1:f155d94d6f3a 494 {
Mike Fiore 1:f155d94d6f3a 495 if(io == NULL) {
Mike Fiore 4:1f63354b8d1b 496 logWarning("MTSBufferedIO not set");
Mike Fiore 1:f155d94d6f3a 497 return 0;
Mike Fiore 1:f155d94d6f3a 498 }
Mike Fiore 1:f155d94d6f3a 499 if(!socketOpened && !io->readable()) {
Mike Fiore 4:1f63354b8d1b 500 logWarning("Socket is not open");
Mike Fiore 1:f155d94d6f3a 501 return 0;
Mike Fiore 1:f155d94d6f3a 502 }
Mike Fiore 1:f155d94d6f3a 503 return io->readable();
Mike Fiore 1:f155d94d6f3a 504 }
Mike Fiore 1:f155d94d6f3a 505
Mike Fiore 3:04046eebaef5 506 unsigned int UIP::writeable()
Mike Fiore 1:f155d94d6f3a 507 {
Mike Fiore 1:f155d94d6f3a 508 if(io == NULL) {
Mike Fiore 4:1f63354b8d1b 509 logWarning("MTSBufferedIO not set");
Mike Fiore 1:f155d94d6f3a 510 return 0;
Mike Fiore 1:f155d94d6f3a 511 }
Mike Fiore 1:f155d94d6f3a 512 if(!socketOpened) {
Mike Fiore 4:1f63354b8d1b 513 logWarning("Socket is not open");
Mike Fiore 1:f155d94d6f3a 514 return 0;
Mike Fiore 1:f155d94d6f3a 515 }
Mike Fiore 1:f155d94d6f3a 516
Mike Fiore 1:f155d94d6f3a 517 return io->writeable();
Mike Fiore 1:f155d94d6f3a 518 }
Mike Fiore 1:f155d94d6f3a 519
Mike Fiore 3:04046eebaef5 520 bool UIP::setDeviceIP(std::string address)
Mike Fiore 1:f155d94d6f3a 521 {
Mike Fiore 1:f155d94d6f3a 522 if (address.compare("DHCP") == 0) {
Mike Fiore 1:f155d94d6f3a 523 return true;
Mike Fiore 1:f155d94d6f3a 524 } else {
Mike Fiore 4:1f63354b8d1b 525 logWarning("Radio does not support static IPs, using DHCP.\n\r");
Mike Fiore 1:f155d94d6f3a 526 return false;
Mike Fiore 1:f155d94d6f3a 527 }
Mike Fiore 1:f155d94d6f3a 528 }
Mike Fiore 1:f155d94d6f3a 529
Mike Fiore 3:04046eebaef5 530 void UIP::reset()
Mike Fiore 1:f155d94d6f3a 531 {
Mike Fiore 1:f155d94d6f3a 532 disconnect();
Mike Fiore 1:f155d94d6f3a 533 Code code = sendBasicCommand("AT#RESET=0", 10000);
mfiore 18:fa0d8120f81f 534 if(code != MTS_SUCCESS) {
Mike Fiore 4:1f63354b8d1b 535 logError("Socket Modem did not accept RESET command\n\r");
Mike Fiore 1:f155d94d6f3a 536 } else {
Mike Fiore 4:1f63354b8d1b 537 logWarning("Socket Modem is resetting, allow 30 seconds for it to come back\n\r");
Mike Fiore 1:f155d94d6f3a 538 }
Mike Fiore 1:f155d94d6f3a 539 }
Mike Fiore 1:f155d94d6f3a 540
Mike Fiore 3:04046eebaef5 541 std::string UIP::getDeviceIP()
Mike Fiore 1:f155d94d6f3a 542 {
Mike Fiore 1:f155d94d6f3a 543 return local_address;
Mike Fiore 1:f155d94d6f3a 544 }
Mike Fiore 1:f155d94d6f3a 545
Mike Fiore 3:04046eebaef5 546 Code UIP::echo(bool state)
Mike Fiore 1:f155d94d6f3a 547 {
Mike Fiore 1:f155d94d6f3a 548 Code code;
Mike Fiore 1:f155d94d6f3a 549 if (state) {
Mike Fiore 1:f155d94d6f3a 550 code = sendBasicCommand("ATE0", 1000);
mfiore 18:fa0d8120f81f 551 echoMode = (code == MTS_SUCCESS) ? false : echoMode;
Mike Fiore 1:f155d94d6f3a 552 } else {
Mike Fiore 1:f155d94d6f3a 553 code = sendBasicCommand("ATE1", 1000);
mfiore 18:fa0d8120f81f 554 echoMode = (code == MTS_SUCCESS) ? true : echoMode;
Mike Fiore 1:f155d94d6f3a 555 }
Mike Fiore 1:f155d94d6f3a 556 return code;
Mike Fiore 1:f155d94d6f3a 557 }
Mike Fiore 1:f155d94d6f3a 558
Mike Fiore 3:04046eebaef5 559 bool UIP::ping(const std::string& address)
Mike Fiore 1:f155d94d6f3a 560 {
Mike Fiore 1:f155d94d6f3a 561 char buffer[256] = {0};
Mike Fiore 1:f155d94d6f3a 562 Code code;
Mike Fiore 1:f155d94d6f3a 563
Mike Fiore 1:f155d94d6f3a 564 code = sendBasicCommand("AT#PINGREMOTE=\"" + address + "\"", 1000);
mfiore 18:fa0d8120f81f 565 if (code != MTS_SUCCESS) {
Mike Fiore 1:f155d94d6f3a 566 return false;
Mike Fiore 1:f155d94d6f3a 567 }
Mike Fiore 1:f155d94d6f3a 568
Mike Fiore 1:f155d94d6f3a 569 sprintf(buffer, "AT#PINGNUM=%d", 1);
Mike Fiore 1:f155d94d6f3a 570 code = sendBasicCommand(buffer , 1000);
mfiore 18:fa0d8120f81f 571 if (code != MTS_SUCCESS) {
Mike Fiore 1:f155d94d6f3a 572 return false;
Mike Fiore 1:f155d94d6f3a 573 }
Mike Fiore 1:f155d94d6f3a 574
Mike Fiore 1:f155d94d6f3a 575 sprintf(buffer, "AT#PINGDELAY=%d", PINGDELAY);
Mike Fiore 1:f155d94d6f3a 576 code = sendBasicCommand(buffer , 1000);
mfiore 18:fa0d8120f81f 577 if (code != MTS_SUCCESS) {
Mike Fiore 1:f155d94d6f3a 578 return false;
Mike Fiore 1:f155d94d6f3a 579 }
Mike Fiore 1:f155d94d6f3a 580
Mike Fiore 1:f155d94d6f3a 581 std::string response;
Mike Fiore 1:f155d94d6f3a 582 for (int i = 0; i < PINGNUM; i++) {
Mike Fiore 1:f155d94d6f3a 583 response = sendCommand("AT#PING", PINGDELAY * 1000);
Mike Fiore 1:f155d94d6f3a 584 if (response.find("alive") != std::string::npos) {
Mike Fiore 1:f155d94d6f3a 585 return true;
Mike Fiore 1:f155d94d6f3a 586 }
Mike Fiore 1:f155d94d6f3a 587 }
Mike Fiore 1:f155d94d6f3a 588 return false;
Mike Fiore 1:f155d94d6f3a 589 }
Mike Fiore 1:f155d94d6f3a 590
Mike Fiore 3:04046eebaef5 591 Code UIP::setSocketCloseable(bool enabled)
Mike Fiore 1:f155d94d6f3a 592 {
Mike Fiore 1:f155d94d6f3a 593 if(socketCloseable == enabled) {
mfiore 18:fa0d8120f81f 594 return MTS_SUCCESS;
Mike Fiore 1:f155d94d6f3a 595 }
Mike Fiore 1:f155d94d6f3a 596
Mike Fiore 1:f155d94d6f3a 597 if(socketOpened) {
Mike Fiore 4:1f63354b8d1b 598 logError("socket is already opened. Can not set closeable");
mfiore 18:fa0d8120f81f 599 return MTS_ERROR;
Mike Fiore 1:f155d94d6f3a 600 }
Mike Fiore 1:f155d94d6f3a 601
Mike Fiore 1:f155d94d6f3a 602 socketCloseable = enabled;
Mike Fiore 1:f155d94d6f3a 603
mfiore 18:fa0d8120f81f 604 return MTS_SUCCESS;
Mike Fiore 1:f155d94d6f3a 605 }