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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Wifi.cpp Source File

Wifi.cpp

00001 /* Universal Socket Modem Interface Library
00002 * Copyright (c) 2013 Multi-Tech Systems
00003 *
00004 * Licensed under the Apache License, Version 2.0 (the "License");
00005 * you may not use this file except in compliance with the License.
00006 * You may obtain a copy of the License at
00007 *
00008 *     http://www.apache.org/licenses/LICENSE-2.0
00009 *
00010 * Unless required by applicable law or agreed to in writing, software
00011 * distributed under the License is distributed on an "AS IS" BASIS,
00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013 * See the License for the specific language governing permissions and
00014 * limitations under the License.
00015 */
00016 
00017 #include "Wifi.h"
00018 #include "MTSText.h"
00019 
00020 #if 0
00021 //Enable debug
00022 #include <cstdio>
00023 #define DBG(x, ...) std::printf("Line: %d %s \t[Wifi : DBG]"x"\r\n", __LINE__, __FILE__, ##__VA_ARGS__);
00024 #else
00025 #define DBG(x, ...)
00026 #endif
00027 
00028 Wifi* Wifi::instance = NULL;
00029 
00030 Wifi* Wifi::getInstance()
00031 {
00032     if(instance == NULL) {
00033         instance = new Wifi(NULL);
00034     }
00035     return instance;
00036 }
00037 
00038 bool Wifi::sortInterfaceMode(void)
00039 {
00040     //Check initial state of command mode
00041     std::string response = sendCommand("", 1000, ">");
00042     if(response.find(">") != string::npos) {
00043         cmdOn = true;
00044     }
00045 
00046     //Set device into command mode
00047     if (!setCmdMode(true)) {
00048         return false;
00049     }
00050 
00051     return true;
00052 }
00053 
00054 bool Wifi::init(MTSBufferedIO* io)
00055 {
00056     if (io == NULL) {
00057         return false;
00058     }
00059     instance->io = io;
00060 
00061     // start from the same place each time
00062     reset();
00063 
00064     //Secure interface mode
00065     if(!sortInterfaceMode()) {
00066         return false;
00067     }
00068 
00069     //Set device to non-echo mode
00070     while (sendBasicCommand("set uart mode 1", 1000) != MTS_SUCCESS) {
00071         printf("[ERROR] Failed to set to non-echo mode\n\r");
00072         //return false;
00073     }
00074     // do this twice because the module response doesnt seem to always take
00075     while (sendBasicCommand("set uart mode 1", 1000) != MTS_SUCCESS) {
00076         printf("[ERROR] Failed to set to non-echo mode\n\r");
00077         //return false;
00078     }
00079 
00080     //Set device to manual infrastructure mode
00081     if (sendBasicCommand("set wlan join 0", 1000) != MTS_SUCCESS) {
00082         printf("[ERROR] Failed to set join mode\n\r");
00083         return false;
00084     }
00085 
00086     //Set device to channel auto-scanning mode
00087     if (sendBasicCommand("set wlan channel 0", 1000) != MTS_SUCCESS) {
00088         printf("[ERROR] Failed to set auto-scanning mode\n\r");
00089         return false;
00090     }
00091 
00092     //Set device so no data is transmitted immediately following a socket connection
00093     if (sendBasicCommand("set comm remote 0", 1000) != MTS_SUCCESS) {
00094         printf("[ERROR] Failed to set remote transmit mode\n\r");
00095         return false;
00096     }
00097 
00098     //Set device into DHCP mode by default
00099     if (sendBasicCommand("set ip dhcp 1", 1000) != MTS_SUCCESS) {
00100         printf("[ERROR] Failed to set to default DHCP mode\n\r");
00101         return false;
00102     }
00103 
00104     return true;
00105 }
00106 
00107 Wifi::Wifi(MTSBufferedIO* io)
00108     : io(io)
00109     , wifiConnected(false)
00110     , _ssid("")
00111     , mode(TCP)
00112     , socketOpened(false)
00113     , socketCloseable(true)
00114     , local_port(0)
00115     , local_address("")
00116     , host_port(0)
00117     , cmdOn(false)
00118 {
00119 
00120 }
00121 
00122 Wifi::~Wifi()
00123 {
00124 }
00125 
00126 bool Wifi::connect()
00127 {
00128     //Check if socket is open
00129     if(socketOpened) {
00130         return true;
00131     }
00132 
00133     //Run Test first to validate a good state
00134     if(isConnected()) {
00135         return true;
00136     }
00137 
00138     if (_ssid.size() == 0) {
00139         printf("[ERROR] No SSID has been set\n\r");
00140         return false;
00141     }
00142 
00143     if(!setCmdMode(true)) {
00144         return false;
00145     }
00146 
00147     //Possibly add a scan command here and look for the network....
00148 
00149     //join my_network
00150     printf("[DEBUG] Making SSID Connection Attempt. SSID[%s]\r\n", _ssid.c_str());
00151     std::string result = sendCommand("join " + _ssid, 15000, "GW=");
00152     //printf("Connect Status: %s\n\r", result.c_str());
00153 
00154     //Check whether connection was successful
00155     if(result.find("Associated!") != string::npos) {
00156         if(result.find("Static") == string::npos) {
00157             int start = result.find("IP=");
00158             int stop = result.find(":", start);
00159             local_address = result.substr(start + 3, stop - start - 3);
00160         }
00161         printf("[INFO] WiFi Connection Established: IP[%s]\r\n", local_address.c_str());
00162         wifiConnected = true;
00163 
00164         //Report Signal Strength of new connection
00165         wait(1); //Needed for signal strength to be available
00166         int rssi = getSignalStrength();
00167         printf("[DEBUG] Signal strength (dBm): %d\r\n", rssi);
00168     } else {
00169         wifiConnected = false;
00170     }
00171 
00172     return wifiConnected;
00173 }
00174 
00175 void Wifi::disconnect()
00176 {
00177     wait(5.0f);
00178     printf("[DEBUG] Disconnecting from network\r\n");
00179 
00180     if(socketOpened) {
00181         close();
00182     }
00183 
00184     if(!setCmdMode(true)) {
00185         printf("[ERROR] Failed in disconnecting from network.  Continuing ...\r\n");
00186     }
00187 
00188     std::string response = sendCommand("leave", 10000, "<4.00>");
00189     response = sendCommand("show net", 5000, "Links");
00190     //printf("Response: %s\n\r", response.c_str());
00191     if (response.find("Assoc=FAIL") != string::npos) {
00192         printf("[DEBUG] Successfully disconnected from network\r\n");
00193     } else {
00194         printf("[ERROR] Failed in disconnecting from network.  Continuing ...\r\n");
00195     }
00196 
00197     wifiConnected = false;
00198 }
00199 
00200 bool Wifi::isConnected()
00201 {
00202     //1) Check if SSID was set
00203     if(_ssid.size() == 0) {
00204         printf("[DEBUG] SSID is not set\r\n");
00205         return false;
00206     }
00207 
00208     //1) Check that we do not have a live connection up
00209     if(isOpen()) {
00210         printf("[DEBUG] Socket is opened\r\n");
00211         return true;
00212     }
00213 
00214     //Check command mode.
00215     if(!setCmdMode(true)) {
00216         return false;
00217     }
00218 
00219     //2) Query the wifi module
00220     wifiConnected = false;
00221     std::string result = sendCommand("show net", 5000, "Links");
00222     //printf("netResult: %s\n\r", result);
00223     if(result.find("Assoc=OK") != std::string::npos) {
00224         wifiConnected = true;
00225     }
00226 
00227     return wifiConnected;
00228 }
00229 
00230 bool Wifi::bind(unsigned int port)
00231 {
00232     if(socketOpened) {
00233         printf("[ERROR] socket is open. Can not set local port\r\n");
00234         return false;
00235     }
00236     if(port > 65535) {
00237         printf("[ERROR] port out of range (0-65535)\r\n");
00238         return false;
00239     }
00240     local_port = port;
00241     return true;
00242 }
00243 
00244 bool Wifi::open(const std::string& address, unsigned int port, Mode mode)
00245 {
00246     char buffer[256] = {0};
00247     printf("[DEBUG] Attempting to Open Socket\r\n");
00248 
00249     //1) Check that we do not have a live connection up
00250     if(socketOpened) {
00251         //Check that the address, port, and mode match
00252         if(host_address != address || host_port != port || this->mode != mode) {
00253             if(this->mode == TCP) {
00254                 printf("[ERROR] TCP socket already opened (%s:%d)\r\n", host_address.c_str(), host_port);
00255             } else {
00256                 printf("[ERROR] UDP socket already opened (%s:%d)\r\n", host_address.c_str(), host_port);
00257             }
00258             return false;
00259         }
00260 
00261         printf("[DEBUG] Socket already opened\r\n");
00262         return true;
00263     }
00264 
00265     //2) Check Parameters
00266     if(port > 65535) {
00267         printf("[ERROR] port out of range (0-65535)\r\n");
00268         return false;
00269     }
00270 
00271 
00272     //3) Check Wifi network connection
00273     if(!isConnected()) {
00274         printf("[ERROR] Wifi network not connected.  Attempting to connect\r\n");
00275         if(!connect()) {
00276             printf("[ERROR] Wifi network connection failed\r\n");
00277             return false;
00278         } else {
00279             printf("[DEBUG] Wifi connection established\r\n");
00280         }
00281     }
00282 
00283     //Check command mode
00284     if(!setCmdMode(true)) {
00285         return false;
00286     }
00287 
00288     //Set Local Port
00289     if(local_port != 0) {
00290         //Attempt to set local port
00291         sprintf(buffer, "set ip localport %d", local_port);
00292         Code code = sendBasicCommand(buffer, 1000);
00293         if(code != MTS_SUCCESS) {
00294             printf("[WARNING] Unable to set local port (%d) [%d]. Continuing...\r\n", local_port, (int) code);
00295         }
00296     }
00297 
00298     //Set TCP/UDP parameters
00299     sprintf(buffer, "set ip remote %d", port);
00300     if(sendBasicCommand(buffer, 1000) == MTS_SUCCESS) {
00301         host_port = port;
00302     } else {
00303         printf("[ERROR] Host port could not be set\r\n");
00304     }
00305 
00306     //Check if address of URL
00307     std::vector<std::string> tmp = Text::split(address, '.');
00308     if(tmp.size() != 4) {
00309         std::string ip = getHostByName(address);
00310         if(ip.size() != 0) {
00311             host_address = ip;
00312         } else {
00313             return false;
00314         }
00315     } else {
00316         host_address = address;
00317     }
00318 
00319     //Set Address
00320     printf("[DEBUG] Host address: %s\n\r", host_address.c_str());
00321     if(sendBasicCommand("set ip host " + host_address, 1000) != MTS_SUCCESS) {
00322         printf("[ERROR] Host address could not be set\r\n");
00323         return false;
00324     }
00325 
00326     if(sendBasicCommand("set ip protocol 8", 1000) != MTS_SUCCESS) {
00327         printf("[ERROR] Failed to set TCP mode\r\n");
00328         return false;
00329     }
00330 
00331     // Try and Connect
00332     std::string sMode;
00333     std::string sOpenSocketCmd;
00334     if(mode == TCP) {
00335         sOpenSocketCmd = "open";
00336         sMode = "TCP";
00337     } else {
00338         //TODO
00339         //sOpenSocketCmd = "AT#OUDP";
00340         //sMode = "UDP";
00341     }
00342     string response = sendCommand(sOpenSocketCmd, 10000, "OPEN");
00343     if (response.find("OPEN") != string::npos) {
00344         printf("[INFO] Opened %s Socket [%s:%d]\r\n", sMode.c_str(), host_address.c_str(), port);
00345         socketOpened = true;
00346         cmdOn = false;
00347     } else {
00348         printf("[WARNING] Unable to open %s Socket [%s:%d]\r\n", sMode.c_str(),  host_address.c_str(), port);
00349         socketOpened = false;
00350     }
00351 
00352     return socketOpened;
00353 }
00354 
00355 bool Wifi::isOpen()
00356 {
00357     if(io->readable()) {
00358         printf("[DEBUG] Assuming open, data available to read.\n\r");
00359         return true;
00360     }
00361     if(!setCmdMode(true)) {
00362         printf("[ERROR] Failed to properly check if TCP connection is open.\r\n");
00363         return socketOpened;
00364     }
00365     std::string response = sendCommand("show connection", 2000, "\n");
00366     // response to "show connection" always starts with "8"
00367     // http://ww1.microchip.com/downloads/en/DeviceDoc/rn-wiflycr-ug-v1.2r.pdf
00368     int start = response.find("8");
00369     // but for some reason the old wifi sheilds give back a response starting with "f"
00370     // perhaps a different firmware version
00371     if (start == string::npos) {
00372         start = response.find("f");
00373     }
00374     if(start != string::npos && response.size() >= (start + 3)) {
00375         if(response[start + 3] == '1') {
00376             socketOpened = true;
00377         } else {
00378             socketOpened = false;
00379         }
00380     } else {
00381         printf("[WARNING] Trouble checking TCP Connection status.\n\r");
00382     }
00383     return socketOpened;
00384 }
00385 
00386 bool Wifi::close()
00387 {
00388     wait(1);
00389     if(io == NULL) {
00390         printf("[ERROR] MTSBufferedIO not set\r\n");
00391         return false;
00392     }
00393 
00394     if(!socketOpened) {
00395         printf("[WARNING] Socket close() called, but socket was not open\r\n");
00396         return true;
00397     }
00398 
00399     if(!setCmdMode(true)) {
00400         printf("[ERROR] Failed to close socket\r\n");
00401         return false;
00402     }
00403 
00404     if(isOpen()) {
00405         std::string response = sendCommand("close", 3000, "CLOS");
00406         if(response.find("CLOS") == string::npos) {
00407             printf("[WARNING] Failed to successfully close socket...\r\n");
00408             return false;
00409         }
00410     }
00411 
00412     wait(1); //Wait so the subsequent isOpen calls return correctly.
00413     io->rxClear();
00414     io->txClear();
00415 
00416     return true;
00417 }
00418 
00419 int Wifi::read(char* data, int max, int timeout)
00420 {
00421     if(io == NULL) {
00422         printf("[ERROR] MTSBufferedIO not set\r\n");
00423         return -1;
00424     }
00425 
00426     //Check that nothing is in the rx buffer
00427     if(!socketOpened && !io->readable()) {
00428         printf("[ERROR] Socket is not open\r\n");
00429         return -1;
00430     }
00431 
00432     //Check for data mode
00433     if(!setCmdMode(false)) {
00434         printf("[ERROR] Failed to read data due to mode\r\n");
00435         return -1;
00436     }
00437 
00438     int bytesRead = 0;
00439 
00440     if(timeout >= 0) {
00441         bytesRead = io->read(data, max, static_cast<unsigned int>(timeout));
00442     } else {
00443         bytesRead = io->read(data, max);
00444     }
00445 
00446     return bytesRead;
00447 }
00448 
00449 int Wifi::write(const char* data, int length, int timeout)
00450 {
00451     if(io == NULL) {
00452         printf("[ERROR] MTSBufferedIO not set\r\n");
00453         return -1;
00454     }
00455 
00456     if(!socketOpened) {
00457         printf("[ERROR] Socket is not open\r\n");
00458         return -1;
00459     }
00460 
00461     //Check for data mode
00462     if(!setCmdMode(false)) {
00463         printf("[ERROR] Failed to write data due to mode\r\n");
00464         return -1;
00465     }
00466 
00467     int bytesWritten = 0;
00468 
00469     if(timeout >= 0) {
00470         bytesWritten = io->write(data, length, static_cast<unsigned int>(timeout));
00471     } else {
00472         bytesWritten = io->write(data, length);
00473     }
00474 
00475     return bytesWritten;
00476 }
00477 
00478 unsigned int Wifi::readable()
00479 {
00480     if(io == NULL) {
00481         printf("[ERROR] MTSBufferedIO not set\r\n");
00482         return 0;
00483     }
00484     if(!socketOpened) {
00485         printf("[ERROR] Socket is not open\r\n");
00486         return 0;
00487     }
00488     return io->readable();
00489 }
00490 
00491 unsigned int Wifi::writeable()
00492 {
00493     if(io == NULL) {
00494         printf("[ERROR] MTSBufferedIO not set\r\n");
00495         return 0;
00496     }
00497     if(!socketOpened) {
00498         printf("[ERROR] Socket is not open\r\n");
00499         return 0;
00500     }
00501 
00502     return io->writeable();
00503 }
00504 
00505 void Wifi::reset()
00506 {
00507     if(!sortInterfaceMode()) {
00508         return;
00509     }
00510 
00511     sendCommand("factory RESET", 2000, "Set Factory Default"); // <ver> comes out about 1 sec later
00512     wait(0.5f);
00513     sendCommand("reboot", 2000, "*READY*");
00514 
00515     wifiConnected = false;
00516     _ssid = "";
00517     mode = TCP;
00518     socketOpened = false;
00519     socketCloseable = true;
00520     local_port = 0;
00521     local_address = "";
00522     host_port = 0;
00523     cmdOn = false;
00524     wait(1);
00525 //    if(!init(io)) {
00526 //        printf("[ERROR] Failed to reinitialize after reset.\n\r");
00527 //    }
00528 }
00529 
00530 Code Wifi::setDeviceIP(std::string address)
00531 {
00532     //Check for command mode
00533     if(!setCmdMode(true)) {
00534         printf("[ERROR] Failed to set IP due to mode issue\r\n");
00535         return MTS_FAILURE;
00536     }
00537 
00538     //Set to DHCP mode
00539     if(address.compare("DHCP") == 0) {
00540         return sendBasicCommand("set ip dhcp 1", 1000);
00541     }
00542 
00543     //Set to static mode and set address
00544     Code code = sendBasicCommand("set ip address " + address, 1000);
00545     if(code != MTS_SUCCESS) {
00546         return code;
00547     }
00548     code = sendBasicCommand("set ip dhcp 0", 1000);
00549     if(code != MTS_SUCCESS) {
00550         return code;
00551     }
00552     local_address = address;
00553     return MTS_SUCCESS;
00554 }
00555 
00556 std::string Wifi::getDeviceIP()
00557 {
00558     return local_address;
00559 }
00560 
00561 Code Wifi::setNetwork(const std::string& ssid, SecurityType type, const std::string& key)
00562 {
00563     //Check the command mode
00564     if(!setCmdMode(true)) {
00565         return MTS_FAILURE;
00566     }
00567 
00568     Code code;
00569 
00570     //Set the appropraite SSID
00571     code = sendBasicCommand("set wlan ssid " + ssid, 1000);
00572     if (code != MTS_SUCCESS) {
00573         return code;
00574     }
00575 
00576     //Set the security key
00577     if (type == WEP64 || type == WEP128) {
00578         //Set the WEP key if using WEP encryption
00579         code = sendBasicCommand("set wlan key " + key, 1000);
00580         if (code != MTS_SUCCESS) {
00581             return code;
00582         }
00583     } else if (type == WPA || type == WPA2) {
00584         //Set the WPA key if using WPA encryption
00585         code = sendBasicCommand("set wlan phrase " + key, 1000);
00586         if (code != MTS_SUCCESS) {
00587             return code;
00588         }
00589     }
00590 
00591     _ssid = ssid;
00592     return MTS_SUCCESS;
00593 }
00594 
00595 Code Wifi::setDNS(const std::string& dnsName)
00596 {
00597     //Check the command mode
00598     if(!setCmdMode(true)) {
00599         return MTS_FAILURE;
00600     }
00601 
00602     return sendBasicCommand("set dns name " + dnsName, 1000);
00603 }
00604 
00605 int Wifi::getSignalStrength()
00606 {
00607     //Signal strength does not report correctly if not connected
00608     if(!wifiConnected) {
00609         printf("[ERROR] Could not get RSSI, Wifi network not connected.\n\r");
00610         return 99;
00611     }
00612 
00613     //Check the command mode
00614     if(!setCmdMode(true)) {
00615         printf("[ERROR] Could not get RSSI\n\r");
00616         return 99;
00617     }
00618 
00619     string response = sendCommand("show rssi", 2000, "dBm");
00620     if (response.find("RSSI") == string::npos) {
00621         printf("[ERROR] Could not get RSSI\n\r");
00622         return 99;
00623     }
00624     int start = response.find('(');
00625     int stop = response.find(')', start);
00626     string signal = response.substr(start + 1, stop - start - 1);
00627     int value;
00628     sscanf(signal.c_str(), "%d", &value);
00629     return value;
00630 }
00631 
00632 bool Wifi::ping(const std::string& address)
00633 {
00634     //Check the command mode
00635     if(!setCmdMode(true)) {
00636         printf("[ERROR] Could not send ping command\n\r");
00637         return false;
00638     }
00639 
00640     std::string response;
00641     for (int i = 0; i < PINGNUM; i++) {
00642         response = sendCommand("ping " + address, PINGDELAY * 1000, "reply");
00643         if (response.find("reply") != std::string::npos) {
00644             return true;
00645         }
00646     }
00647     return false;
00648 }
00649 
00650 bool Wifi::setCmdMode(bool on)
00651 {
00652     if (on) {
00653         if (cmdOn) {
00654             return true;
00655         }
00656         wait(.5);
00657         std::string response = sendCommand("$$", 2000, "CMD", '$');
00658         if (response.find("CMD") != string::npos) {
00659             cmdOn = true;
00660             wait(.5);
00661             return true;
00662         }
00663         printf("[ERROR] Failed to enter command mode\n\r");
00664         return false;
00665     } else {
00666         if (!cmdOn) {
00667             return true;
00668         }
00669         std::string response = sendCommand("exit", 2000, "EXIT");
00670         if (response.find("EXIT") != string::npos) {
00671             cmdOn = false;
00672             return true;
00673         }
00674         printf("[ERROR] Failed to exit command mode\n\r");
00675         return false;
00676     }
00677 }
00678 
00679 std::string Wifi::getHostByName(std::string url)
00680 {
00681     std::string response = sendCommand("lookup " + url, 3000, "<4.00>");
00682     int start = response.find("=");
00683     int stop = response.find("\r");
00684     if(start == string::npos || stop == string::npos) {
00685         printf("[ERROR] Failed to resolve URL [%s]", response.c_str());
00686         return "";
00687     }
00688     std::string ip = response.substr(start + 1, stop - start - 1);
00689     //printf("Data: %s\n\r", ip);
00690     return ip;
00691 }
00692 
00693 Code Wifi::sendBasicCommand(string command, int timeoutMillis, char esc)
00694 {
00695     if(socketOpened) {
00696         printf("[ERROR] socket is open. Can not send AT commands\r\n");
00697         return MTS_ERROR;
00698     }
00699 
00700     string response = sendCommand(command, timeoutMillis, "AOK", esc);
00701     //printf("Response: %s\n\r", response.c_str());
00702     if (response.size() == 0) {
00703         return MTS_NO_RESPONSE;
00704     } else if (response.find("AOK") != string::npos) {
00705         return MTS_SUCCESS;
00706     } else if (response.find("ERR") != string::npos) {
00707         return MTS_ERROR;
00708     } else {
00709         return MTS_FAILURE;
00710     }
00711 }
00712 
00713 string Wifi::sendCommand(string command, int timeoutMillis, std::string response, char esc)
00714 {
00715     if(io == NULL) {
00716         printf("[ERROR] MTSBufferedIO not set\r\n");
00717         return "";
00718     }
00719     //if(socketOpened && command.compare("$$") != 0 && command.compare("exit") != 0 && command.compare("close") != 0) {
00720     //    printf("[ERROR] socket is open. Can not send AT commands\r\n");
00721     //    return "";
00722     //}
00723 
00724     io->rxClear();
00725     io->txClear();
00726     std::string result;
00727 
00728     //Attempt to write command
00729     if(io->write(command.data(), command.size(), timeoutMillis) != command.size()) {
00730         //Failed to write command
00731         printf("[ERROR] failed to send command to radio within %d milliseconds\r\n", timeoutMillis);
00732         return "";
00733     }
00734     //Send Escape Character
00735     if (esc != 0x00) {
00736         if(io->write(esc, timeoutMillis) != 1) {
00737             printf("[ERROR] failed to send '%c' to radio within %d milliseconds\r\n", esc, timeoutMillis);
00738             return "";
00739         }
00740     }
00741     DBG("Sending: %s%c", command.data(), esc);
00742 
00743     int timer = 0;
00744     size_t previous = 0;
00745     char tmp[256];
00746     tmp[255] = 0;
00747     bool done = false;
00748     do {
00749         wait(.2);
00750         timer = timer + 200;
00751         previous = result.size();
00752         int size = io->read(tmp, 255, 0);    //1 less than allocated
00753         if(size > 0) {
00754             result.append(tmp, size);
00755             if (response.size() != 0) {
00756                 if (result.find(response) != string::npos) {
00757                     goto exit_func;
00758                     //return result;
00759                 }
00760             } else {
00761                 done =  (result.size() == previous);
00762             }
00763         }
00764         if(timer >= timeoutMillis) {
00765             if(!(command.compare("reboot") == 0 || command.compare("") == 0)) {
00766                 printf("[WARNING] sendCommand [%s] timed out after %d milliseconds\r\n", command.c_str(), timeoutMillis);
00767             }
00768             done = true;
00769         }
00770     } while (!done);
00771 
00772 exit_func:
00773     DBG("Result: %s\n\r", result.c_str());
00774     return result;
00775 }
00776