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:
74:9f87bd22c222
Parent:
69:f3e696bbb0d5
Child:
75:7ad32827b39e
--- a/wifi/Wifi.cpp	Thu Dec 26 14:47:52 2013 +0000
+++ b/wifi/Wifi.cpp	Thu Dec 26 16:17:30 2013 +0000
@@ -16,8 +16,11 @@
         return false;
     }
     instance->io = io;
-    //sendCommand("$$$", 1000, Cellular::NONE);
-    return true;
+    if(cmdMode()) {
+        return true;
+    }
+    printf("[ERROR] Failed to enter command mode\r\n");
+    return false;
 }
 
 Wifi::Wifi(MTSBufferedIO* io)
@@ -196,7 +199,7 @@
     Cellular::Code code;
 
     //Set the appropraite SSID
-    code = sendBasicCommand("set wlan ssid " + ssid, 1000);
+    code = sendBasicCommand("set wlan ssid " + ssid, 2000);
     if (code != Cellular::CELL_OK) {
         return code;
     }
@@ -204,13 +207,13 @@
     //Set the security key
     if (type == WEP64 || type == WEP128) {
         //Set the WEP key if using WEP encryption
-        code = sendBasicCommand("set wlan key " + key, 1000);
+        code = sendBasicCommand("set wlan key " + key, 2000);
         if (code != Cellular::CELL_OK) {
             return code;
         }
     } else if (type == WPA || type == WPA2) {
         //Set the WPA key if using WPA encryption
-        code = sendBasicCommand("set wlan phrase " + key, 1000);
+        code = sendBasicCommand("set wlan phrase " + key, 2000);
         if (code != Cellular::CELL_OK) {
             return code;
         }
@@ -222,11 +225,27 @@
 
 int Wifi::getSignalStrength()
 {
-    //show rssi
-    return 0;
+    string response = sendCommand("show rssi", 2000);
+    if (response.find("RSSI") == string::npos) {
+        return -1;
+    }
+    int start = response.find('(');
+    int stop = response.find(')', start);
+    string signal = response.substr(start + 1, stop - start - 1);
+    int value;
+    sscanf(signal.c_str(), "%d", &value);
+    return value;
 }
 
-
+bool Wifi::cmdMode()
+{
+    wait(.5);
+    std::string response = sendCommand("$$$", 2000, Cellular::NONE);
+    if (response.find("CMD") != string::npos) {
+        return true;
+    }
+    return false;
+}
 
 Cellular::Code Wifi::sendBasicCommand(string command, int timeoutMillis, Cellular::ESC_CHAR esc)
 {
@@ -267,7 +286,7 @@
     } else if (esc == Cellular::CTRL_Z) {
         cmd[size -1] = 0x1A;
     } else if(esc == NONE) {
-        cmd[size -1] = '\0';
+        size = size - 1;
     }
 
     io->rxClear();
@@ -282,8 +301,8 @@
     bool started = !echoMode;
     bool done = false;
     do {
-        wait(.1);
-        timer = timer + 100;
+        wait(.2);
+        timer = timer + 200;
         previous = available;
         available = io->readable();