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:
73:bb5bbca971ae
Parent:
69:f3e696bbb0d5
Child:
76:371aab9902a4
--- a/wifi/Wifi.cpp	Thu Dec 26 16:15:09 2013 +0000
+++ b/wifi/Wifi.cpp	Thu Dec 26 16:21:11 2013 +0000
@@ -58,23 +58,23 @@
 
     printf("Starting Setup\n\r");
     //Set device to manual infrastructure mode
-    if (sendBasicCommand("set wlan join 0", 1000) != Cellular::CELL_OK) {
+    if (sendBasicCommand("set wlan join 0", 1000) != SUCCESS) {
         return false;
     }
 
     printf("Set Manual Mode\n\r");
     //Set device to channel auto-scanning mode
-    if (sendBasicCommand("set wlan channel 0", 1000) != Cellular::CELL_OK) {
+    if (sendBasicCommand("set wlan channel 0", 1000) != SUCCESS) {
         return false;
     }
 
     //Set device so no data is transmitted immediately following a socket connection
-    if (sendBasicCommand("set comm remote 0", 1000) != Cellular::CELL_OK) {
+    if (sendBasicCommand("set comm remote 0", 1000) != SUCCESS) {
         return false;
     }
 
     //Set device into DHCP mode
-    if (sendBasicCommand("set ip dhcp 1", 1000) != Cellular::CELL_OK) {
+    if (sendBasicCommand("set ip dhcp 1", 1000) != SUCCESS) {
         return false;
     }
 
@@ -176,7 +176,7 @@
 {
 }
 
-Cellular::Code Wifi::echo(bool state)
+Code Wifi::echo(bool state)
 {
     //Code code;
     if (state) {
@@ -188,16 +188,16 @@
         //set uart mode 0 - Enabled
         //echoMode = (code == CELL_OK) ? true : echoMode;
     }
-    return Cellular::CELL_OK;
+    return SUCCESS;
 }
 
-Cellular::Code Wifi::setNetwork(const std::string& ssid, const std::string& key, SecurityType type)
+Code Wifi::setNetwork(const std::string& ssid, const std::string& key, SecurityType type)
 {
-    Cellular::Code code;
+    Code code;
 
     //Set the appropraite SSID
     code = sendBasicCommand("set wlan ssid " + ssid, 1000);
-    if (code != Cellular::CELL_OK) {
+    if (code != SUCCESS) {
         return code;
     }
 
@@ -205,19 +205,19 @@
     if (type == WEP64 || type == WEP128) {
         //Set the WEP key if using WEP encryption
         code = sendBasicCommand("set wlan key " + key, 1000);
-        if (code != Cellular::CELL_OK) {
+        if (code != SUCCESS) {
             return code;
         }
     } else if (type == WPA || type == WPA2) {
         //Set the WPA key if using WPA encryption
         code = sendBasicCommand("set wlan phrase " + key, 1000);
-        if (code != Cellular::CELL_OK) {
+        if (code != SUCCESS) {
             return code;
         }
     }
 
     _ssid = ssid;
-    return Cellular::CELL_OK;
+    return SUCCESS;
 }
 
 int Wifi::getSignalStrength()
@@ -228,55 +228,58 @@
 
 
 
-Cellular::Code Wifi::sendBasicCommand(string command, int timeoutMillis, Cellular::ESC_CHAR esc)
+Code Wifi::sendBasicCommand(string command, int timeoutMillis, char esc)
 {
     if(socketOpened) {
         printf("[ERROR] socket is open. Can not send AT commands\r\n");
-        return Cellular::CELL_ERROR;
+        return ERROR;
     }
 
     string response = sendCommand(command, timeoutMillis, esc);
     //printf("Response: %s\n\r", response.c_str());
     if (response.size() == 0) {
-        return Cellular::CELL_NO_RESPONSE;
+        return NO_RESPONSE;
     } else if (response.find("AOK") != string::npos) {
-        return Cellular::CELL_OK;
+        return SUCCESS;
     } else if (response.find("ERR") != string::npos) {
-        return Cellular::CELL_ERROR;
+        return ERROR;
     } else {
-        return Cellular::CELL_FAILURE;
+        return FAILURE;
     }
 }
 
-string Wifi::sendCommand(string command, int timeoutMillis, Cellular::ESC_CHAR esc)
+string Wifi::sendCommand(string command, int timeoutMillis, char esc)
 {
     if(io == NULL) {
         printf("[ERROR] MTSBufferedIO not set\r\n");
         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 "";
     }
 
-    int size = command.size() + 1;
-    char cmd[size];
-    strcpy(cmd, command.c_str());
-    if (esc == Cellular::CR) {
-        cmd[size -1] = '\r';
-    } else if (esc == Cellular::CTRL_Z) {
-        cmd[size -1] = 0x1A;
-    } else if(esc == NONE) {
-        cmd[size -1] = '\0';
-    }
-
     io->rxClear();
     io->txClear();
     std::string result;
-    int status = io->write(cmd, size);
-    int available = io->readable();
-    int previous = -1;
+    
+    //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) {
+            printf("[ERROR] failed to send '%c' to radio within %d milliseconds\r\n", esc, timeoutMillis);
+            return "";
+        }
+    }
+
     int timer = 0;
+    size_t previous = 0;
     char tmp[256];
     tmp[255] = 0;
     bool started = !echoMode;
@@ -284,27 +287,25 @@
     do {
         wait(.1);
         timer = timer + 100;
-        previous = available;
-        available = io->readable();
-
+        previous = result.size();
         int size = io->read(tmp,255);    //1 less than allocated
         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) {
                 started = true;
             }
         } else {
-            done =  (available == previous);
+            done =  (result.size() == previous);
         }
         if(timer >= timeoutMillis) {
             printf("[WARNING] sendCommand timed out after %d milliseconds\r\n", timeoutMillis);
             done = true;
         }
     } while (!done);
-
+    
     return result;
 }