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:
131:da8f0e1c264a
Parent:
130:5a8af68a46ec
Child:
132:165b3a1084bd
--- a/wifi/Wifi.cpp	Thu Jan 02 21:46:57 2014 +0000
+++ b/wifi/Wifi.cpp	Fri Jan 03 05:00:54 2014 +0000
@@ -1,6 +1,14 @@
 #include "Wifi.h"
 #include <string>
 
+#if 1
+//Enable debug
+#include <cstdio>
+#define DBG(x, ...) std::printf("Line: %d %s \t[Wifi : DBG]"x"\r\n", __LINE__, __FILE__, ##__VA_ARGS__);  
+#else
+#define DBG(x, ...)
+#endif
+
 Wifi* Wifi::instance = NULL;
 
 Wifi* Wifi::getInstance()
@@ -11,13 +19,8 @@
     return instance;
 }
 
-bool Wifi::init(MTSBufferedIO* io)
-{
-    if (io == NULL) {
-        return false;
-    }
-    instance->io = io;
-
+bool Wifi::sortInterfaceMode(void)
+{   
     //Check initial state of command mode
     std::string response = sendCommand("", 1000, ">");
     if(response.find(">") != string::npos) {
@@ -28,11 +31,33 @@
     if (!setCmdMode(true)) {
         return false;
     }
+    
+    return true;
+}
 
+bool Wifi::init(MTSBufferedIO* io)
+{
+    if (io == NULL) {
+        return false;
+    }
+    instance->io = io;
+    
+    // start from the same place each time
+    reset();
+    
+    //Secure interface mode
+    if(!sortInterfaceMode()) {
+        return false;
+    }
+
+    //cant do as a basic command after factory RESET, echo is on
+    //sendCommand("set uart mode 1", 1000, "set");
+    //wait(2.0f); // just wait a sec for the change
+    
     //Set device to non-echo mode
-    if (sendBasicCommand("set uart mode 1", 1000) != SUCCESS) {
+    while (sendBasicCommand("set uart mode 1", 1000) != SUCCESS) {
         printf("[ERROR] Failed to set to non-echo mode\n\r");
-        return false;
+        //return false;
     }
 
     //Set device to manual infrastructure mode
@@ -132,6 +157,7 @@
 
 void Wifi::disconnect()
 {
+    wait(2.0f);
     printf("[DEBUG] Disconnecting from network\r\n");
 
     if(socketOpened) {
@@ -439,7 +465,14 @@
 
 void Wifi::reset()
 {
-    sendCommand("reboot", 2000);
+    if(!sortInterfaceMode()) {
+        return;
+    }
+    
+    sendCommand("factory RESET", 2000, "Set Factory Default"); // <ver> comes out about 1 sec later
+    wait(0.5f);
+    sendCommand("reboot", 2000, "*READY*");
+   
     wifiConnected = false;
     _ssid = "";
     mode = TCP;
@@ -449,10 +482,10 @@
     local_address = "";
     host_port = 0;
     cmdOn = false;
-    wait(10);
-    if(!init(io)) {
-        printf("[ERROR] Failed to reinitialize after reset.\n\r");
-    }
+    wait(1);
+//    if(!init(io)) {
+//        printf("[ERROR] Failed to reinitialize after reset.\n\r");
+//    }
 }
 
 Code Wifi::setDeviceIP(std::string address)
@@ -645,7 +678,6 @@
         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) {
@@ -653,7 +685,8 @@
             return "";
         }
     }
-
+    DBG("Sending: %s%c", command.data(), esc);
+    
     int timer = 0;
     size_t previous = 0;
     char tmp[256];
@@ -668,7 +701,8 @@
             result.append(tmp, size);
             if (response.size() != 0) {
                 if (result.find(response) != string::npos) {
-                    return result;
+                    goto exit_func;
+                    //return result;
                 }
             } else {
                 done =  (result.size() == previous);
@@ -682,6 +716,7 @@
         }
     } while (!done);
 
-    //printf("Result: %s\n\r", result.c_str());
+exit_func:
+    DBG("Result: %s\n\r", result.c_str());
     return result;
 }