Cellular library for MTS Socket Modem Arduino Shield devices from Multi-Tech Systems
Dependents: mtsas mtsas mtsas mtsas
Diff: Cellular/UIP.cpp
- Revision:
- 4:1f63354b8d1b
- Parent:
- 3:04046eebaef5
- Child:
- 7:0ee8e69a3e9c
diff -r 04046eebaef5 -r 1f63354b8d1b Cellular/UIP.cpp --- a/Cellular/UIP.cpp Tue May 20 15:21:06 2014 -0500 +++ b/Cellular/UIP.cpp Tue May 20 16:11:38 2014 -0500 @@ -1,6 +1,7 @@ #include "mbed.h" #include "UIP.h" #include "MTSText.h" +#include "MTSLog.h" #include "CellUtils.h" using namespace mts; @@ -86,7 +87,7 @@ do { Registration registration = getRegistration(); if(registration != REGISTERED) { - printf("[WARNING] Not Registered [%d] ... waiting\r\n", (int)registration); + logWarning("Not Registered [%d] ... waiting", (int)registration); wait(1); } else { break; @@ -97,9 +98,9 @@ tmr.reset(); do { int rssi = getSignalStrength(); - printf("[DEBUG] Signal strength: %d\r\n", rssi); + logDebug("Signal strength: %d", rssi); if(rssi == 99) { - printf("[WARNING] No Signal ... waiting\r\n"); + logWarning("No Signal ... waiting"); wait(1); } else { break; @@ -107,7 +108,7 @@ } while(tmr.read() < 30); //AT#CONNECTIONSTART: Make a PPP connection - printf("[DEBUG] Making PPP Connection Attempt. APN[%s]\r\n", apn.c_str()); + logDebug("Making PPP Connection Attempt. APN[%s]", apn.c_str()); std::string pppResult = sendCommand("AT#CONNECTIONSTART", 120000); std::vector<std::string> parts = Text::split(pppResult, "\r\n"); @@ -115,7 +116,7 @@ if(parts.size() >= 2) { local_address = parts[1]; } - printf("[INFO] PPP Connection Established: IP[%s]\r\n", local_address.c_str()); + logInfo("PPP Connection Established: IP[%s]", local_address.c_str()); pppConnected = true; } else { @@ -128,7 +129,7 @@ void UIP::disconnect() { //AT#CONNECTIONSTOP: Close a PPP connection - printf("[DEBUG] Closing PPP Connection\r\n"); + logDebug("Closing PPP Connection"); if(socketOpened) { close(); @@ -136,9 +137,9 @@ Code code = sendBasicCommand("AT#CONNECTIONSTOP", 10000); if(code == SUCCESS) { - printf("[DEBUG] Successfully closed PPP Connection\r\n"); + logDebug("Successfully closed PPP Connection"); } else { - printf("[ERROR] Closing PPP Connection [%d]. Continuing ...\r\n", (int)code); + logError("Closing PPP Connection [%d]. Continuing ...", (int)code); } pppConnected = false; @@ -148,20 +149,20 @@ { //1) Check if APN was set if(apn.size() == 0) { - printf("[DEBUG] APN is not set\r\n"); + logDebug("APN is not set"); return false; } //1) Check that we do not have a live connection up if(socketOpened) { - printf("[DEBUG] Socket is opened\r\n"); + logDebug("Socket is opened"); return true; } //2) Query the radio std::string result = sendCommand("AT#VSTATE", 3000); if(result.find("CONNECTED") != std::string::npos) { if(pppConnected == false) { - printf("[WARNING] Internal PPP state tracking differs from radio (DISCONNECTED:CONNECTED)\r\n"); + logWarning("Internal PPP state tracking differs from radio (DISCONNECTED:CONNECTED)"); } pppConnected = true; } else { @@ -170,9 +171,9 @@ size_t pos = result.find("STATE:"); if(pos != std::string::npos) { result = Text::getLine(result, pos + sizeof("STATE:"), pos); - printf("[WARNING] Internal PPP state tracking differs from radio (CONNECTED:%s)\r\n", result.c_str()); + logWarning("Internal PPP state tracking differs from radio (CONNECTED:%s)", result.c_str()); } else { - printf("[ERROR] Unable to parse radio state: [%s]\r\n", result.c_str()); + logError("Unable to parse radio state: [%s]", result.c_str()); } } @@ -185,11 +186,11 @@ bool UIP::bind(unsigned int port) { if(socketOpened) { - printf("[ERROR] socket is open. Can not set local port\r\n"); + logError("socket is open. Can not set local port"); return false; } if(port > 65535) { - printf("[ERROR] port out of range (0-65535)\r\n"); + logError("port out of range (0-65535)"); return false; } local_port = port; @@ -206,31 +207,31 @@ //Check that the address, port, and mode match if(host_address != address || host_port != port || this->mode != mode) { if(this->mode == TCP) { - printf("[ERROR] TCP socket already opened [%s:%d]\r\n", host_address.c_str(), host_port); + logError("TCP socket already opened [%s:%d]", host_address.c_str(), host_port); } else { - printf("[ERROR] UDP socket already opened [%s:%d]\r\n", host_address.c_str(), host_port); + logError("UDP socket already opened [%s:%d]", host_address.c_str(), host_port); } return false; } - printf("[DEBUG] Socket already opened\r\n"); + logDebug("Socket already opened"); return true; } //2) Check Parameters if(port > 65535) { - printf("[ERROR] port out of range (0-65535)\r\n"); + logError("port out of range (0-65535)"); return false; } //3) Check PPP connection if(!isConnected()) { - printf("[ERROR] PPP not established. Attempting to connect\r\n"); + logError("PPP not established. Attempting to connect"); if(!connect()) { - printf("[ERROR] PPP connection failed\r\n"); + logError("PPP connection failed"); return false; } else { - printf("[DEBUG] PPP connection established\r\n"); + logDebug("PPP connection established"); } } @@ -240,7 +241,7 @@ sprintf(buffer, "AT#OUTPORT=%d", local_port); Code code = sendBasicCommand(buffer, 1000); if(code != SUCCESS) { - printf("[WARNING] Unable to set local port (%d) [%d]\r\n", local_port, (int) code); + logWarning("Unable to set local port (%d) [%d]", local_port, (int) code); } } @@ -249,7 +250,7 @@ if(socketCloseable) { Code code = sendBasicCommand("AT#DLEMODE=1,1", 1000); if(code != SUCCESS) { - printf("[WARNING] Unable to set socket closeable [%d]\r\n", (int) code); + logWarning("Unable to set socket closeable [%d]", (int) code); } } sprintf(buffer, "AT#TCPPORT=1,%d", port); @@ -259,7 +260,7 @@ if(socketCloseable) { Code code = sendBasicCommand("AT#UDPDLEMODE=1", 1000); if(code != SUCCESS) { - printf("[WARNING] Unable to set socket closeable [%d]\r\n", (int) code); + logWarning("Unable to set socket closeable [%d]", (int) code); } } sprintf(buffer, "AT#UDPPORT=%d", port); @@ -270,13 +271,13 @@ if(portCode == SUCCESS) { host_port = port; } else { - printf("[ERROR] Host port could not be set\r\n"); + logError("Host port could not be set"); } if(addressCode == SUCCESS) { host_address = address; } else { - printf("[ERROR] Host address could not be set\r\n"); + logError("Host address could not be set"); } // Try and Connect @@ -292,10 +293,10 @@ string response = sendCommand(sOpenSocketCmd, 30000); if (response.find("Ok_Info_WaitingForData") != string::npos) { - printf("[INFO] Opened %s Socket [%s:%d]\r\n", sMode.c_str(), address.c_str(), port); + logInfo("Opened %s Socket [%s:%d]", sMode.c_str(), address.c_str(), port); socketOpened = true; } else { - printf("[WARNING] Unable to open %s Socket [%s:%d]\r\n", sMode.c_str(), address.c_str(), port); + logWarning("Unable to open %s Socket [%s:%d]", sMode.c_str(), address.c_str(), port); socketOpened = false; } @@ -305,7 +306,7 @@ bool UIP::isOpen() { if(io->readable()) { - printf("[DEBUG] Assuming open, data available to read.\n\r"); + logDebug("Assuming open, data available to read.\n\r"); return true; } return socketOpened; @@ -314,24 +315,24 @@ bool UIP::close() { if(io == NULL) { - printf("[ERROR] MTSBufferedIO not set\r\n"); + logError("MTSBufferedIO not set"); return false; } if(!socketOpened) { - printf("[WARNING] Socket close() called, but socket was not open\r\n"); + logWarning("Socket close() called, but socket was not open"); return true; } if(!socketCloseable) { - printf("[ERROR] Socket is not closeable\r\n"); + logError("Socket is not closeable"); return false; } if(io->write(ETX, 1000) != 1) { - printf("[ERROR] Timed out attempting to close socket\r\n"); + logError("Timed out attempting to close socket"); return false; } @@ -356,13 +357,13 @@ int UIP::read(char* data, int max, int timeout) { if(io == NULL) { - printf("[ERROR] MTSBufferedIO not set\r\n"); + logError("MTSBufferedIO not set"); return -1; } //Check that nothing is in the rx buffer if(!socketOpened && !io->readable()) { - printf("[ERROR] Socket is not open\r\n"); + logError("Socket is not open"); return -1; } @@ -389,7 +390,7 @@ continue; } else { //ETX sent without escape -> Socket closed - printf("[INFO] Read ETX character without DLE escape. Socket closed\r\n"); + logInfo("Read ETX character without DLE escape. Socket closed"); socketOpened = false; continue; } @@ -407,7 +408,7 @@ for(size_t i = 0; i < bytesRead; i++) { if(data[i] == 'O') { if(strstr(&data[i], "Ok_Info_SocketClosed")) { - printf("[INFO] Found socket closed message. Socket closed\r\n"); + logInfo("Found socket closed message. Socket closed"); //Close socket and Cut Off End of Message socketOpened = false; data[i] = '\0'; @@ -422,12 +423,12 @@ int UIP::write(const char* data, int length, int timeout) { if(io == NULL) { - printf("[ERROR] MTSBufferedIO not set\r\n"); + logError("MTSBufferedIO not set"); return -1; } if(!socketOpened) { - printf("[ERROR] Socket is not open\r\n"); + logError("Socket is not open"); return -1; } @@ -510,11 +511,11 @@ unsigned int UIP::readable() { if(io == NULL) { - printf("[WARNING] MTSBufferedIO not set\r\n"); + logWarning("MTSBufferedIO not set"); return 0; } if(!socketOpened && !io->readable()) { - printf("[WARNING] Socket is not open\r\n"); + logWarning("Socket is not open"); return 0; } return io->readable(); @@ -523,11 +524,11 @@ unsigned int UIP::writeable() { if(io == NULL) { - printf("[WARNING] MTSBufferedIO not set\r\n"); + logWarning("MTSBufferedIO not set"); return 0; } if(!socketOpened) { - printf("[WARNING] Socket is not open\r\n"); + logWarning("Socket is not open"); return 0; } @@ -539,7 +540,7 @@ if (address.compare("DHCP") == 0) { return true; } else { - printf("[WARNING] Radio does not support static IPs, using DHCP.\n\r"); + logWarning("Radio does not support static IPs, using DHCP.\n\r"); return false; } } @@ -549,9 +550,9 @@ disconnect(); Code code = sendBasicCommand("AT#RESET=0", 10000); if(code != SUCCESS) { - printf("[ERROR] Socket Modem did not accept RESET command\n\r"); + logError("Socket Modem did not accept RESET command\n\r"); } else { - printf("[WARNING] Socket Modem is resetting, allow 30 seconds for it to come back\n\r"); + logWarning("Socket Modem is resetting, allow 30 seconds for it to come back\n\r"); } } @@ -628,7 +629,7 @@ } if(socketOpened) { - printf("[ERROR] socket is already opened. Can not set closeable\r\n"); + logError("socket is already opened. Can not set closeable"); return ERROR; } @@ -640,7 +641,7 @@ Code UIP::sendBasicCommand(const std::string& command, unsigned int timeoutMillis, char esc) { if(socketOpened) { - printf("[ERROR] socket is open. Can not send AT commands\r\n"); + logError("socket is open. Can not send AT commands"); return ERROR; } @@ -659,11 +660,11 @@ string UIP::sendCommand(const std::string& command, unsigned int timeoutMillis, char esc) { if(io == NULL) { - printf("[ERROR] MTSBufferedIO not set\r\n"); + logError("MTSBufferedIO not set"); return ""; } if(socketOpened) { - printf("[ERROR] socket is open. Can not send AT commands\r\n"); + logError("socket is open. Can not send AT commands"); return ""; } @@ -675,7 +676,7 @@ if(io->write(command.data(), command.size(), timeoutMillis) != command.size()) { //Failed to write command if (command != "AT" && command != "at") { - printf("[ERROR] failed to send command to radio within %d milliseconds\r\n", timeoutMillis); + logError("failed to send command to radio within %d milliseconds", timeoutMillis); } return ""; } @@ -684,7 +685,7 @@ if (esc != 0x00) { if(io->write(esc, timeoutMillis) != 1) { if (command != "AT" && command != "at") { - printf("[ERROR] failed to send character '%c' (0x%02X) to radio within %d milliseconds\r\n", esc, esc, timeoutMillis); + logError("failed to send character '%c' (0x%02X) to radio within %d milliseconds", esc, esc, timeoutMillis); } return ""; } @@ -716,7 +717,7 @@ } if(timer >= timeoutMillis) { if (command != "AT" && command != "at") { - printf("[WARNING] sendCommand [%s] timed out after %d milliseconds\r\n", command.c_str(), timeoutMillis); + logWarning("sendCommand [%s] timed out after %d milliseconds", command.c_str(), timeoutMillis); } done = true; } @@ -745,7 +746,7 @@ } wait(.2); string response2 = sendCommand(message, 4000, CTRL_Z); - printf("SMS Response: %s\r\n", response2.c_str()); + logInfo("SMS Response: %s", response2.c_str()); if (response2.find("+CMGS:") == string::npos) { return FAILURE; } @@ -762,7 +763,6 @@ while (pos != std::string::npos) { Cellular::Sms sms; std::string line(Text::getLine(received, pos, pos)); - //printf("[DEBUG] Top of SMS Parse Loop. LINE[%s]\r\n", line.c_str()); if(line.find("+CMGL: ") == std::string::npos) { continue; } @@ -770,7 +770,7 @@ //Start of SMS message std::vector<std::string> vSmsParts = Text::split(line, ','); if(vSmsParts.size() != 6) { - printf("[WARNING] Expected 6 commas. SMS[%d] DATA[%s]. Continuing ...\r\n", smsNumber, line.c_str()); + logWarning("Expected 6 commas. SMS[%d] DATA[%s]. Continuing ...", smsNumber, line.c_str()); continue; } @@ -778,13 +778,12 @@ sms.timestamp = vSmsParts[4] + ", " + vSmsParts[5]; if(pos == std::string::npos) { - printf("[WARNING] Expected SMS body. SMS[%d]. Leaving ...\r\n", smsNumber); + logWarning("Expected SMS body. SMS[%d]. Leaving ...", smsNumber); break; } //Check for the start of the next SMS message size_t bodyEnd = received.find("\r\n+CMGL: ", pos); if(bodyEnd == std::string::npos) { - //printf("[DEBUG] Parsing Last SMS. SMS[%d]\r\n", smsNumber); //This must be the last SMS message bodyEnd = received.find("\r\n\r\nOK", pos); } @@ -794,14 +793,13 @@ sms.message = received.substr(pos, bodyEnd - pos); } else { sms.message = received.substr(pos); - printf("[WARNING] Expected to find end of SMS list. SMS[%d] DATA[%s].\r\n", smsNumber, sms.message.c_str()); + logWarning("Expected to find end of SMS list. SMS[%d] DATA[%s].", smsNumber, sms.message.c_str()); } vSms.push_back(sms); pos = bodyEnd; - //printf("[DEBUG] Parsed SMS[%d]. Starting Next at position [%d]\r\n", smsNumber, pos); smsNumber++; } - printf("Received %d SMS\r\n", smsNumber); + logInfo("Received %d SMS", smsNumber); return vSms; }