EEN for Chris
Fork of MTS-Cellular by
Revision 56:43205bd2752a, committed 2014-08-13
- Comitter:
- Vanger
- Date:
- Wed Aug 13 16:46:10 2014 +0000
- Parent:
- 55:85c04afa939a
- Child:
- 57:7eb2eb536d4a
- Commit message:
- Moved common functions from EasyIP and UIP classes to Cellular class. Also tweaked isConnected() under EasyIP.cpp
Changed in this revision
--- a/Cellular/Cellular.cpp Mon Aug 11 21:01:50 2014 +0000 +++ b/Cellular/Cellular.cpp Wed Aug 13 16:46:10 2014 +0000 @@ -211,7 +211,7 @@ if(result.size() > (command.size() + 2)) { if(result.find("OK\r\n",command.size()) != std::string::npos) { done = true; - } else if (result.find("ERROR\r\n") != std::string::npos) { + } else if (result.find("ERROR") != std::string::npos) { done = true; } else if (result.find("NO CARRIER\r\n") != std::string::npos) { done = true; @@ -260,7 +260,7 @@ return MTS_FAILURE; } - Code code = sendBasicCommand("AT+CMGF=1", 1000); + Code code = sendBasicCommand("AT+CMGF=1", 2000); if (code != MTS_SUCCESS) { logError("CMGF failed"); return code; @@ -289,7 +289,7 @@ } wait(.2); - string response2 = sendCommand(message, 12000, CTRL_Z); + string response2 = sendCommand(message, 15000, CTRL_Z); if (response2.find("+CMGS:") == string::npos) { logError("CMGS message failed"); return MTS_FAILURE; @@ -305,7 +305,7 @@ std::string received; size_t pos; - Code code = sendBasicCommand("AT+CMGF=1", 1000); + Code code = sendBasicCommand("AT+CMGF=1", 2000); if (code != MTS_SUCCESS) { logError("CMGF failed"); return vSms; @@ -322,7 +322,7 @@ } //Start of SMS message std::vector<std::string> vSmsParts = Text::split(line, ','); - if (type == MTSMC_H5_IP || type == MTSMC_H5) { + if (type == MTSMC_H5_IP || type == MTSMC_H5 || type == MTSMC_G3) { /* format for H5 and H5-IP radios * <index>, <status>, <oa>, <alpha>, <scts> * scts contains a comma, so splitting on commas should give us 6 items @@ -388,3 +388,102 @@ { return sendBasicCommand("AT+CMGD=1,4", 1000); } + +unsigned int Cellular::readable() +{ + if(io == NULL) { + logWarning("MTSBufferedIO not set"); + return 0; + } + if(!socketOpened && !io->readable()) { + logWarning("Socket is not open"); + return 0; + } + return io->readable(); +} + +unsigned int Cellular::writeable() +{ + if(io == NULL) { + logWarning("MTSBufferedIO not set"); + return 0; + } + if(!socketOpened) { + logWarning("Socket is not open"); + return 0; + } + + return io->writeable(); +} + +bool Cellular::setDeviceIP(std::string address) +{ + if (address.compare("DHCP") == 0) { + return true; + } else { + logWarning("Radio does not support static IPs, using DHCP."); + return false; + } +} + +std::string Cellular::getDeviceIP() +{ + return local_address; +} + +//Turns off echo when it receives a true, turns on when it receives false +Code Cellular::echo(bool state) +{ + Code code; + if (state) { + code = sendBasicCommand("ATE0", 1000); + echoMode = (code == MTS_SUCCESS) ? false : echoMode; + } else { + code = sendBasicCommand("ATE1", 1000); + echoMode = (code == MTS_SUCCESS) ? true : echoMode; + } + return code; +} + +//Pass 1 to enable socket closeable +//Pass 0 to disable socket closeable +Code Cellular::setSocketCloseable(bool enabled) +{ + if(socketCloseable == enabled) { + return MTS_SUCCESS; + } + + if(socketOpened) { + logError("socket is already opened. Can not set closeable"); + return MTS_ERROR; + } + + socketCloseable = enabled; + + return MTS_SUCCESS; +} + +bool Cellular::isOpen() +{ + if(io->readable()) { + logDebug("Assuming open, data available to read."); + return true; + } + return socketOpened; +} + +//Binds the socket to a specific port if able +bool Cellular::bind(unsigned int port) +{ + if(socketOpened) { + logError("socket is open. Can not set local port"); + return false; + } + if(port > 65535) { + logError("port out of range (0-65535)"); + return false; + } + local_port = port; + return true; +} +
--- a/Cellular/Cellular.h Mon Aug 11 21:01:50 2014 +0000 +++ b/Cellular/Cellular.h Wed Aug 13 16:46:10 2014 +0000 @@ -403,13 +403,65 @@ * @returns the enumeration name as a string. */ static std::string getRadioNames(Radio radio); + /** A method for changing the echo commands from radio. * @param state Echo mode is off (an argument of 1 turns echos off, anything else turns echo on) * @returns standard Code enumeration */ - virtual Code echo(bool state) = 0; //Implemented the same way in both UIP and EasyIP, - //and thus could be moved to cellular class - virtual Code setSocketCloseable(bool enabled) = 0; + virtual Code echo(bool state); + + /** This method can be used to trade socket functionality for performance. + * Can disable checking socket closed messages from the data socket, and thus the socket + * will only be visibly closed to the local side if the radio is explicitly checked, or + * the socket is closed by the local side through the use of physical pin manipulation. + * + * Uses the Hayes escape sequence (1 second pause, "+++", 1 second pause) to exit the socket + * connection to check if a received "NO CARRIER" string is from the radio indicating the socket + * has been closed, or is merely part of the data stream. Should not occur very often, however, if + * data carrying the string "NO CARRIER" is going to be transmitted frequently, then the socket should + * be set closeable and physical-socket-closing-means be used instead to reduce the large amount of + * overhead switching from checking the validity of the "NO CARRIER" message being and indication of + * the socket connection being closed. + * + * @param enabled set to true if you want the socket closeable, otherwise false. The default + * is true. + * @returns the standard AT Code enumeration. + */ + virtual Code setSocketCloseable(bool enabled); + + + /** Binds the socket to a specific port if able + * @param port integer to bind the socket to. + * + * @returns true if successfully bound port, false if bind failed. + */ + virtual bool bind(unsigned int port); + + /** Checks if a socket is open. + * @returns true if socket is open, false if socket is closed + */ + virtual bool isOpen(); + + /** Checks if there is data available from the socket. + * @returns number of bytes of data available to read. + */ + virtual unsigned int readable(); + + /** Checks data to output on the socket + * @returns number of bytes to be written to the socket. + */ + virtual unsigned int writeable(); + + /** Gets the device IP + * @returns string containing the IP address + */ + virtual std::string getDeviceIP(); + + /** Sets the device IP + * (Not implemented, IP address values are assigned by DHCP) + * @returns true if the IP was set, false if IP address assignment failed. + */ + virtual bool setDeviceIP(std::string address = "DHCP"); protected: MTSBufferedIO* io; //IO interface obect that the radio is accessed through.
--- a/Cellular/EasyIP.cpp Mon Aug 11 21:01:50 2014 +0000 +++ b/Cellular/EasyIP.cpp Wed Aug 13 16:46:10 2014 +0000 @@ -43,7 +43,7 @@ logDebug("radio type: %s", Cellular::getRadioNames(type).c_str()); //Turns on the HW flow control - if(sendBasicCommand("AT&K3", 2000) != MTS_SUCCESS) { + if(sendBasicCommand("AT+IFC=2,2", 2000) != MTS_SUCCESS) { logWarning("Failed to enable serial flow control"); } return true; @@ -74,7 +74,7 @@ tmr.start(); do { Registration registration = getRegistration(); - if(registration != REGISTERED) { + if(registration != REGISTERED && registration != ROAMING) { logTrace("Not Registered [%d] ... waiting", (int)registration); wait(1); } else { @@ -201,7 +201,7 @@ regist = false; } - string reply = sendCommand("AT#SGACT?", 500); + string reply = sendCommand("AT#SGACT?", 1000); if (reply.find("1,1") != std::string::npos) { active = true; } else { @@ -211,18 +211,14 @@ RadioState state; bool ppp = pppConnected; if (signal && regist && active) { - //Radio connected state = CONNECTED; pppConnected = true; } else if (signal && !regist && !active) { - //Radio idle state = IDLE; pppConnected = false; } else if (active) { - //Radio Connecting state = CONNECTING; } else { - //Radio Disconnected state = DISCONNECTED; pppConnected = false; } @@ -230,7 +226,25 @@ if (!ppp && state == CONNECTED) { logWarning("Internal PPP state tracking differs from radio (DISCONNECTED:CONNECTED)"); } else if (ppp && state != CONNECTED) { - logWarning("Internal PPP state tracking differs from radio (CONNECTED:%s)", state); + string stateStr; + switch (state) { + case IDLE: + stateStr = "IDLE"; + break; + case CONNECTING: + stateStr = "CONNECTING"; + break; + case DISCONNECTED: + stateStr = "DISCONNECTED"; + break; + case CONNECTED: + stateStr = "CONNECTED"; + break; + default: + stateStr = "UKNOWN"; + break; + } + logWarning("Internal PPP state tracking differs from radio (CONNECTED:%s)", stateStr.c_str()); } return pppConnected; @@ -248,21 +262,6 @@ } } -//Binds the socket to a specific port if able -bool EasyIP::bind(unsigned int port) -{ - if(socketOpened) { - logError("socket is open. Can not set local port"); - return false; - } - if(port > 65535) { - logError("port out of range (0-65535)"); - return false; - } - local_port = port; - return true; -} - bool EasyIP::open(const std::string& address, unsigned int port, Mode mode) { char sOpenSocketCmd[256] = {0}; //String for AT command @@ -310,7 +309,7 @@ } } - //4) Set escape sequence to not be transmitted + //4) Set escape sequence to not be transmitted through socket if(sendBasicCommand("AT#SKIPESC=1", 2000) != MTS_SUCCESS) { logWarning("Failed to disable escape sequence transmission on data mode suspension"); } @@ -347,15 +346,6 @@ return socketOpened; } -bool EasyIP::isOpen() -{ - if(io->readable()) { - logDebug("Assuming open, data available to read."); - return true; - } - return socketOpened; -} - bool EasyIP::close() { @@ -498,78 +488,22 @@ return bytesWritten; } -unsigned int EasyIP::readable() -{ - if(io == NULL) { - logWarning("MTSBufferedIO not set"); - return 0; - } - if(!socketOpened && !io->readable()) { - logWarning("Socket is not open"); - return 0; - } - return io->readable(); -} - -unsigned int EasyIP::writeable() -{ - if(io == NULL) { - logWarning("MTSBufferedIO not set"); - return 0; - } - if(!socketOpened) { - logWarning("Socket is not open"); - return 0; - } - - 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."); - 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); + //CGDCONT has options: IP,PPP,IPv6 + Code code = sendBasicCommand("AT+CGDCONT=1,IP," + apn, 1000); if (code != MTS_SUCCESS) { - return code; //This will return whatever is not MTS_SUCCESS + return code; } this->apn = apn; - return code; //This will return MTS_SUCCESS + return code; } else { logInfo("CDMA radios don't need an APN"); return MTS_SUCCESS; } } -std::string EasyIP::getDeviceIP() -{ - return local_address; -} - -//Turns off echo when it receives a true, turns on when it receives false -Code EasyIP::echo(bool state) -{ - Code code; - if (state) { - code = sendBasicCommand("ATE0", 1000); - echoMode = (code == MTS_SUCCESS) ? false : echoMode; - } else { - code = sendBasicCommand("ATE1", 1000); - echoMode = (code == MTS_SUCCESS) ? true : echoMode; - } - return code; -} - bool EasyIP::ping(const std::string& address) { char buffer[256] = {0}; @@ -604,24 +538,6 @@ return false; } -//Pass 1 to enable socket closeable -//Pass 0 to disable socket closeable -Code EasyIP::setSocketCloseable(bool enabled) -{ - if(socketCloseable == enabled) { - return MTS_SUCCESS; - } - - if(socketOpened) { - logError("socket is already opened. Can not set closeable"); - return MTS_ERROR; - } - - socketCloseable = enabled; - - return MTS_SUCCESS; -} - bool EasyIP::sendEscapeCommand() { //string Cellular::sendCommand(const std::string& command, unsigned int timeoutMillis, char esc) @@ -690,8 +606,8 @@ bool EasyIP::socketCheck() { bool status = false; - int socketInfo = 9; //error - enum SocketStatus {SOCKETCLOSED, SOCKETACTIVEDATA, SOCKETSUSPEND, SOCKETSUSPENDDATA, SOCKETLISTEN, SOCKETINCOMING, ERROR = 9}; + int socketInfo = ERROR; + enum SocketStatus {SOCKETCLOSED = 0, SOCKETACTIVEDATA = 1, SOCKETSUSPEND = 2, SOCKETSUSPENDDATA = 3, SOCKETLISTEN = 4, SOCKETINCOMING = 5, ERROR = 9}; std::vector<std::string> params; //Goes from data mode to command mode @@ -704,14 +620,14 @@ socketInfo = atoi(params[1].c_str()); } else { logError("Could not determine socket status[%d]",socketInfo); - socketInfo = 9; //9 is unrecognized + socketInfo = ERROR; } } else { status = false; //Return value of socketOpened when checking } //Check socket status query - if(socketInfo < 5 && socketInfo > 0) { //Socket opened responses + if(socketInfo == SOCKETINCOMING || socketInfo == SOCKETACTIVEDATA || socketInfo == SOCKETSUSPEND || socketInfo == SOCKETSUSPENDDATA || socketInfo == SOCKETLISTEN) { //Socket opened responses status = true; } else if(socketInfo == SOCKETCLOSED || socketInfo == SOCKETINCOMING) { status = false;
--- a/Cellular/EasyIP.h Mon Aug 11 21:01:50 2014 +0000 +++ b/Cellular/EasyIP.h Wed Aug 13 16:46:10 2014 +0000 @@ -83,22 +83,17 @@ // TCP and UDP Socket related commands // For behavior of the following methods refer to IPStack.h documentation - virtual bool bind(unsigned int port); + virtual bool open(const std::string& address, unsigned int port, Mode mode); - virtual bool isOpen(); virtual bool close(); virtual int read(char* data, int max, int timeout = -1); virtual int write(const char* data, int length, int timeout = -1); - virtual unsigned int readable(); - virtual unsigned int writeable(); /** Pings specified DNS or IP address * Google DNS server used as default ping address * @returns true if ping received alive response else false */ virtual bool ping(const std::string& address = "8.8.8.8"); - virtual std::string getDeviceIP(); - virtual bool setDeviceIP(std::string address = "DHCP"); /** Sets the APN * @@ -109,34 +104,6 @@ */ virtual Code setApn(const std::string& apn); - /** A method for configuring command ehco capability on the radio. This command - * sets whether sent characters are echoed back from the radio, in which case you - * will receive back every command you send. - * - * @param state if true echo will be turned off, otherwise it will be turned on. - * @returns the standard AT Code enumeration. - */ - virtual Code echo(bool state); - - /** This method can be used to trade socket functionality for performance. - * Can disable checking socket closed messages from the data socket, and thus the socket - * will only be visibly closed to the local side if the radio is explicitly checked, or - * the socket is closed by the local side through the use of physical pin manipulation. - * - * Uses the Hayes escape sequence (1 second pause, "+++", 1 second pause) to exit the socket - * connection to check if a received "NO CARRIER" string is from the radio indicating the socket - * has been closed, or is merely part of the data stream. Should not occur very often, however, if - * data carrying the string "NO CARRIER" is going to be transmitted frequently, then the socket should - * be set closeable and physical-socket-closing-means be used instead to reduce the large amount of - * overhead switching from checking the validity of the "NO CARRIER" message being and indication of - * the socket connection being closed. - * - * @param enabled set to true if you want the socket closeable, otherwise false. The default - * is true. - * @returns the standard AT Code enumeration. - */ - virtual Code setSocketCloseable(bool enabled = true); - private: /** Function that sends +++ to the radio to exit data mode * returns true if it successfully exits from online mode, else
--- a/Cellular/UIP.cpp Mon Aug 11 21:01:50 2014 +0000 +++ b/Cellular/UIP.cpp Wed Aug 13 16:46:10 2014 +0000 @@ -173,21 +173,6 @@ return pppConnected; } - -bool UIP::bind(unsigned int port) -{ - if(socketOpened) { - logError("socket is open. Can not set local port"); - return false; - } - if(port > 65535) { - logError("port out of range (0-65535)"); - return false; - } - local_port = port; - return true; -} - bool UIP::open(const std::string& address, unsigned int port, Mode mode) { char buffer[256] = {0}; @@ -295,15 +280,6 @@ return socketOpened; } -bool UIP::isOpen() -{ - if(io->readable()) { - logDebug("Assuming open, data available to read."); - return true; - } - return socketOpened; -} - bool UIP::close() { if(io == NULL) { @@ -498,43 +474,6 @@ return bytesWritten; } -unsigned int UIP::readable() -{ - if(io == NULL) { - logWarning("MTSBufferedIO not set"); - return 0; - } - if(!socketOpened && !io->readable()) { - logWarning("Socket is not open"); - return 0; - } - return io->readable(); -} - -unsigned int UIP::writeable() -{ - if(io == NULL) { - logWarning("MTSBufferedIO not set"); - return 0; - } - if(!socketOpened) { - logWarning("Socket is not open"); - return 0; - } - - return io->writeable(); -} - -bool UIP::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 UIP::setApn(const std::string& apn) { if (type == MTSMC_H5_IP) { @@ -561,24 +500,6 @@ } } -std::string UIP::getDeviceIP() -{ - return local_address; -} - -Code UIP::echo(bool state) -{ - Code code; - if (state) { - code = sendBasicCommand("ATE0", 1000); - echoMode = (code == MTS_SUCCESS) ? false : echoMode; - } else { - code = sendBasicCommand("ATE1", 1000); - echoMode = (code == MTS_SUCCESS) ? true : echoMode; - } - return code; -} - bool UIP::ping(const std::string& address) { char buffer[256] = {0}; @@ -614,20 +535,4 @@ } } return false; -} - -Code UIP::setSocketCloseable(bool enabled) -{ - if(socketCloseable == enabled) { - return MTS_SUCCESS; - } - - if(socketOpened) { - logError("socket is already opened. Can not set closeable"); - return MTS_ERROR; - } - - socketCloseable = enabled; - - return MTS_SUCCESS; -} +} \ No newline at end of file
--- a/Cellular/UIP.h Mon Aug 11 21:01:50 2014 +0000 +++ b/Cellular/UIP.h Wed Aug 13 16:46:10 2014 +0000 @@ -77,17 +77,11 @@ // TCP and UDP Socket related commands // For behavior of the following methods refer to IPStack.h documentation - virtual bool bind(unsigned int port); virtual bool open(const std::string& address, unsigned int port, Mode mode); - virtual bool isOpen(); virtual bool close(); virtual int read(char* data, int max, int timeout = -1); virtual int write(const char* data, int length, int timeout = -1); - virtual unsigned int readable(); - virtual unsigned int writeable(); virtual bool ping(const std::string& address = "8.8.8.8"); - virtual std::string getDeviceIP(); - virtual bool setDeviceIP(std::string address = "DHCP"); /** A method for setting the APN * @@ -96,28 +90,6 @@ */ virtual Code setApn(const std::string& apn); - /** A method for configuring command ehco capability on the radio. This command - * sets whether sent characters are echoed back from the radio, in which case you - * will receive back every command you send. - * - * @param state if true echo will be turned off, otherwise it will be turned on. - * @returns the standard AT Code enumeration. - */ - virtual Code echo(bool state); - - /** This method can be used to trade socket functionality for performance. - * In order to enable a socket connection to be closed by the client side programtically, - * this class must process all read and write data on the socket to guard the special - * escape character used to close an open socket connection. It is recommened that you - * use the default of true unless the overhead of these operations is too significant. - * If set to false, socket must be closed using physical pin signals rather than through - * the use of the ETX data character - * - * @param enabled set to true if you want the socket closeable, otherwise false. The default - * is true. - * @returns the standard AT Code enumeration. - */ - virtual Code setSocketCloseable(bool enabled = true); //ETX closes socket (ETX and DLE in payload are escaped with DLE) }; }
--- a/Test/TestSMS.h Mon Aug 11 21:01:50 2014 +0000 +++ b/Test/TestSMS.h Wed Aug 13 16:46:10 2014 +0000 @@ -57,7 +57,7 @@ } //Wait until the SIM card is ready - while (radio->sendBasicCommand("AT+CMGD=1,4", 1000) != MTS_SUCCESS); + while (radio->sendBasicCommand("AT+CMGD=1,4", 3000) != MTS_SUCCESS); Test::assertTrue(radio->deleteAllReceivedSms() == MTS_SUCCESS); Test::assertTrue(radio->getReceivedSms().size() == 0);