A library for talking to Multi-Tech's Cellular SocketModem Devices.
Dependents: M2X_dev axeda_wrapper_dev MTS_M2x_Example1 MTS_Cellular_Connect_Example ... more
Revision 125:850c51cbeff7, committed 2014-01-02
- Comitter:
- mfiore
- Date:
- Thu Jan 02 18:12:21 2014 +0000
- Parent:
- 124:6d964b4343c8
- Parent:
- 123:a84f6b89132b
- Child:
- 126:7a8ca7478e0b
- Commit message:
- merged
Changed in this revision
--- a/cellular/Cellular.cpp Thu Jan 02 18:11:25 2014 +0000 +++ b/cellular/Cellular.cpp Thu Jan 02 18:12:21 2014 +0000 @@ -22,6 +22,7 @@ , socketOpened(false) , socketCloseable(true) , local_port(0) + , local_address("") , host_port(0) , dcd(NULL) , dtr(NULL) @@ -33,7 +34,7 @@ if (dtr != NULL) { dtr->write(1); } - + delete dcd; delete dtr; } @@ -43,16 +44,16 @@ if (io == NULL) { return false; } - + if(dcd) { delete dcd; - dcd = NULL; + dcd = NULL; } if(dtr) { delete dtr; dtr = NULL; } - + if (DCD != NC) { // the radio will raise and lower this line dcd = new DigitalIn(DCD); //PTA4 - KL46 @@ -65,7 +66,7 @@ dtr->write(0); } instance->io = io; - + return (test() == SUCCESS); } @@ -81,9 +82,9 @@ if(isConnected()) { return true; } - + Timer tmr; - + //Check Registration: AT+CREG? == 0,1 tmr.start(); do { @@ -102,12 +103,12 @@ int rssi = getSignalStrength(); printf("[DEBUG] Signal strength: %d\r\n", rssi); if(rssi == 99) { - printf("[WARNING] No Signal ... waiting\r\n"); - wait(1); + printf("[WARNING] No Signal ... waiting\r\n"); + wait(1); } else { - break; + break; } - } while(tmr.read() < 30); + } while(tmr.read() < 30); //AT#CONNECTIONSTART: Make a PPP connection printf("[DEBUG] Making PPP Connection Attempt. APN[%s]\r\n", apn.c_str()); @@ -173,11 +174,11 @@ 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()); + printf("[WARNING] Internal PPP state tracking differs from radio (CONNECTED:%s)\r\n", result.c_str()); } else { - printf("[ERROR] Unable to parse radio state: [%s]\r\n", result.c_str()); + printf("[ERROR] Unable to parse radio state: [%s]\r\n", result.c_str()); } - + } pppConnected = false; } @@ -236,7 +237,7 @@ printf("[DEBUG] PPP connection established\r\n"); } } - + //Set Local Port if(local_port != 0) { //Attempt to set local port @@ -292,7 +293,7 @@ sOpenSocketCmd = "AT#OUDP"; sMode = "UDP"; } - + 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); @@ -307,6 +308,10 @@ bool Cellular::isOpen() { + if(io->readable()) { + printf("[DEBUG] Assuming open, data available to read.\n\r"); + return true; + } return socketOpened; } @@ -327,27 +332,27 @@ return false; } - - + + if(io->write(ETX, 1000) != 1) { printf("[ERROR] Timed out attempting to close socket\r\n"); return false; } - + Timer tmr; int counter = 0; char tmp[256]; tmr.start(); do { if(socketOpened == false) { - break; + break; } read(tmp, 256, 1000); } while(counter++ < 10); - + io->rxClear(); io->txClear(); - + socketOpened = false; return true; } @@ -544,6 +549,11 @@ } } +std::string Cellular::getDeviceIP() +{ + return local_address; +} + Code Cellular::test() { bool basicRadioComms = false; @@ -555,9 +565,9 @@ code = sendBasicCommand("AT", 1000); if(code == SUCCESS) { basicRadioComms = true; - break; + break; } else { - wait(1); + wait(1); } } while(tmr.read() < 15); @@ -565,7 +575,7 @@ printf("[ERROR] Unable to communicate with the radio\r\n"); return FAILURE; } - + return SUCCESS; } @@ -836,7 +846,7 @@ do { wait(0.1); timer += 100; - + previous = result.size(); //Make a non-blocking read call by passing timeout of zero int size = io->read(tmp,255,0); //1 less than allocated (timeout is instant)
--- a/cellular/Cellular.h Thu Jan 02 18:11:25 2014 +0000 +++ b/cellular/Cellular.h Thu Jan 02 18:12:21 2014 +0000 @@ -44,7 +44,7 @@ * for (int i = 30; i >= 0; i = i - 5) { * wait(5); * printf("Waiting %d seconds...\n\r", i); -* } +* } * * //Setup serial interface to radio * MTSSerialFlowControl* serial = new MTSSerialFlowControl(PTD3, PTD2, PTA12, PTC8); @@ -94,7 +94,7 @@ * for (int i = 30; i >= 0; i = i - 5) { * wait(5); * printf("Waiting %d seconds...\n\r", i); -* } +* } * * //Setup serial interface to radio * MTSSerialFlowControl* serial = new MTSSerialFlowControl(PTD3, PTD2, PTA12, PTC8); @@ -161,7 +161,7 @@ * for (int i = 30; i >= 0; i = i - 5) { * wait(5); * printf("Waiting %d seconds...\n\r", i); -* } +* } * * //Setup serial interface to radio * MTSSerialFlowControl* serial = new MTSSerialFlowControl(PTD3, PTD2, PTA12, PTC8); @@ -358,6 +358,13 @@ */ Code sendBasicCommand(const std::string& command, unsigned int timeoutMillis, char esc = CR); + /** This method is used to get the IP address of the device, which is determined + * via DHCP by the cellular carrier. + * + * @returns the devices IP address. + */ + std::string getDeviceIP(); + /** A method for testing command access to the radio. This method sends the * command "AT" to the radio, which is a standard radio test to see if you * have command access to the radio.
--- a/wifi/Wifi.cpp Thu Jan 02 18:11:25 2014 +0000 +++ b/wifi/Wifi.cpp Thu Jan 02 18:12:21 2014 +0000 @@ -289,6 +289,10 @@ bool Wifi::isOpen() { + if(io->readable()) { + printf("[DEBUG] Assuming open, data available to read.\n\r"); + return true; + } if(!setCmdMode(true)) { printf("[ERROR] Failed to properly check if TCP connection is open.\r\n"); return socketOpened;