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: wifi/Wifi.cpp
- Revision:
- 79:f356009dbc12
- Parent:
- 76:371aab9902a4
- Child:
- 93:aa7a48e65974
--- a/wifi/Wifi.cpp Thu Dec 26 16:35:29 2013 +0000 +++ b/wifi/Wifi.cpp Fri Dec 27 14:41:03 2013 +0000 @@ -16,7 +16,7 @@ return false; } instance->io = io; - if(cmdMode()) { + if(setCmdMode(true)) { return true; } printf("[ERROR] Failed to enter command mode\r\n"); @@ -27,12 +27,13 @@ : io(io) , echoMode(true) , wifiConnected(false) + , _ssid("") , mode(TCP) , socketOpened(false) , socketCloseable(true) , local_port(0) , host_port(0) - , _ssid("") + , cmdOn(false) { } @@ -109,11 +110,51 @@ void Wifi::disconnect() { + printf("[DEBUG] Disconnecting from network\r\n"); + + if(socketOpened) { + close(); + } + + bool success = false; + if (setCmdMode(true)) { + std::string response = sendCommand("leave", 10000); + if (response.find("DeAuth") != string::npos) { + success = true; + } + } + + if(success) { + printf("[DEBUG] Successfully disconnected from network\r\n"); + } else { + printf("[ERROR] Failed in disconnecting from network. Continuing ...\r\n"); + } + + wifiConnected = false; } bool Wifi::isConnected() { - return false; + //1) Check if SSID was set + if(_ssid.size() == 0) { + printf("[DEBUG] SSID is not set\r\n"); + return false; + } + + //1) Check that we do not have a live connection up + if(socketOpened) { + printf("[DEBUG] Socket is opened\r\n"); + return true; + } + + //2) Query the wifi module + wifiConnected = false; + std::string result = sendCommand("AT#VSTATE", 1000); + if(result.find("CONNECTED") != std::string::npos) { + wifiConnected = true; + } + + return wifiConnected; } bool Wifi::bind(unsigned int port) @@ -222,14 +263,30 @@ return value; } -bool Wifi::cmdMode() +bool Wifi::setCmdMode(bool on) { - wait(.5); - std::string response = sendCommand("$$$", 2000, NONE); - if (response.find("CMD") != string::npos) { - return true; + if (on) { + if (cmdOn) { + return true; + } + wait(.5); + std::string response = sendCommand("$$", 2000, '$'); + if (response.find("CMD") != string::npos) { + cmdOn = true; + return true; + } + return false; + } else { + if (!cmdOn) { + return true; + } + std::string response = sendCommand("exit", 2000); + if (response.find("EXIT") != string::npos) { + cmdOn = false; + return true; + } + return false; } - return false; } Code Wifi::sendBasicCommand(string command, int timeoutMillis, char esc) @@ -259,21 +316,21 @@ return ""; } if(socketOpened) { - printf("[ERROR] socket is open. Can not send AT commands\r\n"); + printf("[ERROR] socket is open. Can not send AT commands\r\n"); return ""; } io->rxClear(); io->txClear(); std::string result; - + //Attempt to write command if(io->write(command.data(), command.size(), timeoutMillis) != command.size()) { //Failed to write command printf("[ERROR] failed to send command to radio within %d milliseconds\r\n", timeoutMillis); return ""; } - + //Send Escape Character if (esc != 0x00) { if(io->write(esc, timeoutMillis) != 1) { @@ -296,7 +353,7 @@ if(size > 0) { result.append(tmp, size); } - + if(!started) { //In Echo Mode (Command will have echo'd + 2 characters for \r\n) if(result.size() > command.size() + 2) { @@ -310,6 +367,6 @@ done = true; } } while (!done); - + return result; }