Fix for UIP read of DLE escape character.
Fork of MTS-Cellular by
Diff: Cellular/EasyIP.cpp
- Revision:
- 26:2b769ed8de4f
- Child:
- 27:ec44d5a9544f
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Cellular/EasyIP.cpp Thu Jun 26 21:12:37 2014 +0000 @@ -0,0 +1,265 @@ +// This is a template from UIP.cpp for now, will modify code and implement it as I go + + + +#include "mbed.h" +#include "EasyIP.h" +#include "MTSText.h" +#include "MTSLog.h" +#include "CellUtils.h" + + + +using namespace mts; + +EasyIP::EasyIP(Radio type) +{ + //Not sure how the construction process is done, + //but assuming it works for both EasyIP and UIP the same way. + this->type = type; + io = NULL; + dcd = NULL; + dtr = NULL; + resetLine = NULL; + echoMode = true; + pppConnected = false; + socketMode = TCP; + socketOpened = false; + socketCloseable = true; + local_port = 0; + local_address = ""; + host_port = 0; +} + +EasyIP::~EasyIP() +{ + //Same reasoning for the destructor as the constructor, + //assuming it works for UIP, it will work for EasyIP + if (dtr != NULL) { + dtr->write(1); + } + + delete dcd; + delete dtr; + delete resetLine; +} + +//Initializes the MTS IO Buffer +bool EasyIP::init(MTSBufferedIO* io) +{ + if (! Cellular::init(io)) { + return false; + } + + logDebug("radio type: %s", Cellular::getRadioNames(type).c_str()); + return true; +} + +bool EasyIP::connect() +{ + //Check if socket is open + //flag stored in Cellular.h + if(socketOpened) { + return true; + } + + //Check if already connected + //by calling the function isConnected() in EasyIP.cpp + if(isConnected()) { + return true; + } + //Create an mbed timer object + Timer tmr; + + //Check Registration: AT+CREG? == 0,1 + //(Does the AT command inside Cellular class) + tmr.start(); + do { + Registration registration = getRegistration(); + if(registration != REGISTERED) { + logTrace("Not Registered [%d] ... waiting", (int)registration); + wait(1); + } else { + break; + } + } while(tmr.read() < 30); + + //Check RSSI: AT+CSQ + //Does the command inside Cellular + tmr.reset(); + do { + int rssi = getSignalStrength(); + logDebug("Signal strength: %d", rssi); + if(rssi == 99) { + logTrace("No Signal ... waiting"); + wait(1); + } else { + break; + } + } while(tmr.read() < 30); + + //AT#CONNECTIONSTART: Make a PPP connection + if (type == MTSMC_H5 || type == MTSMC_G3) { + logDebug("Making PPP Connection Attempt. APN[%s]", apn.c_str()); + } else { + logDebug("Making PPP Connection Attempt"); + } + //The main thing going on; Sends the AT command to start a connection + //Assuming context is already stored in the modem...If not, will need to set context from classes/data + std::string pppResult = sendCommand("AT#SGACT=1,1", 120000); + + if(pppResult.find("OK") != std::string::npos) { + std::vector<std::string> parts = Text::split(pppResult, "\r\n"); + if(parts.size() >= 2) { + parts = Text::split(parts[1], " "); + local_address = parts[1]; + } + logInfo("PPP Connection Established: IP[%s]", local_address.c_str()); + pppConnected = true; + + } else { + pppConnected = false; + } + + return pppConnected; +} + +void EasyIP::disconnect() +{ + //AT#SGACT=1,0: Close a PPP connection + logDebug("Closing PPP Connection"); + + if(socketOpened) { + close(); //Calls another EasyIP + //function to close socket before disconnect + } + //Sends AT#SGACT=1,0 command + + if(sendBasicCommand("AT#SGACT=1,0", 10000) == MTS_SUCCESS) { + logDebug("Successfully closed PPP Connection"); + } else { + logError("Closing PPP Connection. Continuing ..."); + } + pppConnected = false; +} +//++++++++++++++++++++++++++++++++++++++++++++ +bool EasyIP::isConnected() +{ + + return pppConnected; +} +//Binds the socket to a specific port if able +bool EasyIP::bind(unsigned int port) +{ + + + return true; +} +//++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +bool EasyIP::open(const std::string& address, unsigned int port, Mode mode) +{ + + return socketOpened; +} +//++++++++++++++++++++++++++++++++++++++++++++++++++++++ +bool EasyIP::isOpen() +{ + + return socketOpened; +} +//++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +bool EasyIP::close() +{ + + return true; +} + +int EasyIP::read(char* data, int max, int timeout) +{ + + return 1; +} + +int EasyIP::write(const char* data, int length, int timeout) +{ + + return 1; +} + +unsigned int EasyIP::readable() +{ + + return io->readable(); +} + +unsigned int EasyIP::writeable() +{ + + + return io->writeable(); +} + +bool EasyIP::setDeviceIP(std::string address) +{ + if (address.compare("DHCP") == 0) { + return true; + } else { + logWarning("Radio does not support static IPs, using DHCP.\n\r"); + return false; + } +} + +Code EasyIP::setApn(const std::string& apn) +{ + if (type == MTSMC_H5 || type == MTSMC_G3) { + //Set IP,PPP,IPv6 + Code code = sendBasicCommand("AT+CGDCONT=1,PPP," + apn, 1000); + if (code != MTS_SUCCESS) { + return code; + } + this->apn = apn; + return code; //This will return MTS_SUCCESS + } else { + logInfo("CDMA radios don't need an APN"); + return MTS_SUCCESS; + } +} +//++++++++++++++++++++++++++++++++++++++++++++++++++ +void EasyIP::reset() +{ +} + +std::string EasyIP::getDeviceIP() +{ + return local_address; +} + +//Turns off echo when it receives a 1, turns on when it receives anything else +Code EasyIP::echo(bool state) +{ + return MTS_SUCCESS; +} + +bool EasyIP::ping(const std::string& address) +{ + char buffer[256] = {0}; + std::string response; + + //Sends "PINGNUM" of pings to "address" with a timeout of "PINGDELAY" + //AT command takes pingdelay as units of 100ms, so seconds would be 10 times bigger + //Concatenate parameters into one string + sprintf(buffer, "AT#PING=%s,%d,32,%d", address.c_str(), PINGNUM, (10*PINGDELAY)); + response = sendCommand(buffer,PINGDELAY*10000); + if(response.find("OK") != std::string::npos) { + //Not sure if I need a wait here, or if it is included in the sendcommand wait time + return true; + } + + + return false; +} + +Code EasyIP::setSocketCloseable(bool enabled) +{ + return MTS_SUCCESS; +}