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
Diff: cellular/Cellular.cpp
- Revision:
- 13:0af863114629
- Parent:
- 12:40ac31a09132
- Child:
- 17:2d7c4ea7491b
--- a/cellular/Cellular.cpp Fri Dec 13 20:23:40 2013 +0000 +++ b/cellular/Cellular.cpp Fri Dec 13 23:02:50 2013 +0000 @@ -7,7 +7,7 @@ Cellular::Cellular(MTSBufferedIO& io) : io(io) -, code(OK) +, echoMode(true) , pppConnected(false) , mode(TCP) , socketOpened(false) @@ -21,19 +21,50 @@ } bool Cellular::connect() { + //Check if socket is open + if(socketOpened) { + return true; + } + //Run Test first to validate a good state + if(isConnected()) { + return true; + } + //Check RSSI: AT+CSQ + int rssi = getSignalStrength(); + printf("[DEBUG] Signal strength: %d\n", rssi); + //Check Registration: AT+CREG? == 0,1 - + Registration registration = getRegistration(); + if(registration != REGISTERED) { + printf("[WARNING] Not Registered [%d]\n", (int)registration); + } //AT#CONNECTIONSTART: Make a PPP connection - std::string pppResult = sendCommand("AT#CONNECTIONSTART", 30000); + printf("[DEBUG] Making PPP Connection Attempt. APN[%s]\n", apn.c_str()); + std::string pppResult = sendCommand("AT#CONNECTIONSTART", 120000); + + printf("[DEBUG] PPP CONNECT RESULT [%s]\n", pppResult.c_str()); + + std::vector<std::string> parts = Text::split(pppResult, "\r\n"); + for(uint32_t i = 0; i < parts.size(); i++) { + printf("[%d] [%s]\r\n", i, parts[i].c_str()); + } - size_t pos = 0; - std::string ip = Text::getLine(pppResult, pos, pos); - std::string status = Text::getLine(pppResult, pos, pos); + + if(pppResult.find("Ok_Info_GprsActivation") != std::string::npos) { + if(parts.size() >= 2) { + local_address = parts[1]; + } + printf("[INFO] PPP Connection Established: IP[%s]\n", local_address.c_str()); + pppConnected = true; + + } else { + pppConnected = false; + } - return false; + return pppConnected; } void Cellular::disconnect() { @@ -110,9 +141,17 @@ printf("[ERROR] Host address could not be set\n"); } - + // Try and Connect + string response = sendCommand("AT#OTCP=1", 30000); + if (response.find("Ok_Info_WaitingForData") != string::npos) { + printf("[INFO] Opened TCP Socket [%s:%d]\n", address.c_str(), port); + socketOpened = true; + } else { + printf("[WARNING] Unable to open TCP Socket [%s:%d]\n", address.c_str(), port); + socketOpened = false; + } - return false; + return socketOpened; } bool Cellular::isOpen() { @@ -120,7 +159,18 @@ } void Cellular::close() { + if(!socketOpened) { + return; + } + //Build Escape Message => DLE ETX + char buffer[3] = { 0x10, 0x03, 0x00 }; + Code code = sendBasicCommand(buffer, 1000, NONE); + if(code != OK) { + printf("[WARNING] Radio did not accept close socket command"); + //Handle cleanup + } + socketOpened = false; } int Cellular::read(char* data, int max, int timeout) { @@ -160,11 +210,15 @@ Cellular::Code Cellular::echoOff(bool state) { + Code code; if (state) { - return sendBasicCommand("ATE0", 1000); + code = sendBasicCommand("ATE0", 1000); + echoMode = (code == OK) ? false : echoMode; } else { - return sendBasicCommand("ATE1", 1000); + code = sendBasicCommand("ATE1", 1000); + echoMode = (code == OK) ? true : echoMode; } + return code; } int Cellular::getSignalStrength() @@ -213,37 +267,6 @@ return UNKNOWN; } -//int Cellular::connect(string host, int port) -//{ -// // Set the Server Address -// string hostCmd = "AT#TCPSERV=1,\""; -// hostCmd.append(host); -// hostCmd.append("\""); -// if (sendBasicCommand(hostCmd, 1000) != OK) { -// return -1; -// } -// -// // Set the Server Port -// string portCmd = "AT#TCPPORT=1,\""; -// char tmp[7]; -// if (sprintf(tmp, "%d", port) < 0) { -// return -1; -// } -// portCmd.append(string(tmp)); -// portCmd.append("\""); -// if (sendBasicCommand(portCmd, 1000) != OK) { -// return -1; -// } -// -// // Try and Connect -// string response = sendCommand("AT#OTCP=1", 2000); -// if (response.find("Ok_Info_WaitingForData") != string::npos) { -// return 0; -// } else { -// return -1; -// } -//} - Cellular::Code Cellular::sendBasicCommand(string command, int timeoutMillis, ESC_CHAR esc) { string response = sendCommand(command, timeoutMillis, esc); @@ -367,6 +390,8 @@ cmd[size -1] = '\r'; } else if (esc == CTRL_Z) { cmd[size -1] = 0x1A; + } else if(esc == NONE) { + cmd[size -1] = '\0'; } io.rxClear(); @@ -378,7 +403,8 @@ int timer = 0; char tmp[256]; tmp[255] = 0; - + bool started = !echoMode; + bool done = false; do { wait(.1); timer = timer + 100; @@ -389,7 +415,20 @@ if(size > 0) { result.append(tmp, size); } - } while (available != previous && timer < timeoutMillis); + + if(!started) { + //In Echo Mode (Command will have echo'd + 2 characters for \r\n) + if(result.size() > command.size() + 2) { + started = true; + } + } else { + done = (available == previous); + } + if(timer >= timeoutMillis) { + printf("[WARNING] sendCommand timed out after %d milliseconds\n", timeoutMillis); + done = true; + } + } while (!done); return result; }