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:
106:358972176b89
Parent:
103:da58d27c15d7
Child:
108:554585370b4a
--- a/wifi/Wifi.cpp	Tue Dec 31 15:11:04 2013 +0000
+++ b/wifi/Wifi.cpp	Tue Dec 31 16:34:22 2013 +0000
@@ -45,6 +45,13 @@
         printf("[ERROR] Failed to set remote transmit mode\n\r");
         return false;
     }
+
+    //Set device into DHCP mode by default
+    if (sendBasicCommand("set ip dhcp 1", 1000) != SUCCESS) {
+        printf("[ERROR] Failed to set to default DHCP mode\n\r");
+        return false;
+    }
+
     return true;
 }
 
@@ -56,6 +63,7 @@
     , socketOpened(false)
     , socketCloseable(true)
     , local_port(0)
+    , local_address("")
     , host_port(0)
     , cmdOn(false)
 {
@@ -89,11 +97,6 @@
 
     //Possibly add a scan command here and look for the network....
 
-    //Set device into DHCP mode
-    if (sendBasicCommand("set ip dhcp 1", 1000) != SUCCESS) {
-        return false;
-    }
-
     //join my_network
     printf("[DEBUG] Making SSID Connection Attempt. SSID[%s]\r\n", _ssid.c_str());
     std::string result = sendCommand("join " + _ssid, 15000, "Listen");
@@ -101,12 +104,14 @@
 
     //Check whether connection was successful
     if(result.find("Associated!") != string::npos) {
-        int start = result.find("IP=");
-        int stop = result.find(":", start);
-        local_address = result.substr(start + 3, stop - start - 3);
+        if(result.find("Static") == string::npos) {
+            int start = result.find("IP=");
+            int stop = result.find(":", start);
+            local_address = result.substr(start + 3, stop - start - 3);
+        }
         printf("[INFO] WiFi Connection Established: IP[%s]\r\n", local_address.c_str());
         wifiConnected = true;
-        
+
         //Report Signal Strength of new connection
         wait(1); //Needed for signal strength to be available
         int rssi = getSignalStrength();
@@ -188,8 +193,6 @@
 
 bool Wifi::open(const std::string& address, unsigned int port, Mode mode)
 {
-    //set comm size??? are advanced Socket settings
-    //set comm time??? are advanced Socket settings
     char buffer[256] = {0};
     printf("[DEBUG] Attempting to Open Socket\r\n");
 
@@ -406,6 +409,31 @@
 {
 }
 
+Code Wifi::setDeviceIP(std::string address)
+{
+    //Set to DHCP mode
+    if(address.compare("DHCP") == 0) {
+        return sendBasicCommand("set ip dhcp 1", 1000);
+    }
+
+    //Set to static mode and set address
+    Code code = sendBasicCommand("set ip address " + address, 1000);
+    if(code != SUCCESS) {
+        return code;
+    }
+    code = sendBasicCommand("set ip dhcp 0", 1000);
+    if(code != SUCCESS) {
+        return code;
+    }
+    local_address = address;
+    return SUCCESS;
+}
+
+std::string Wifi::getDeviceIP()
+{
+    return local_address;
+}
+
 Code Wifi::setNetwork(const std::string& ssid, SecurityType type, const std::string& key)
 {
     //Check the command mode
@@ -549,10 +577,10 @@
         printf("[ERROR] MTSBufferedIO not set\r\n");
         return "";
     }
-    if(socketOpened && command.compare("$$") != 0 && command.compare("exit") != 0 && command.compare("close") != 0) {
-        printf("[ERROR] socket is open. Can not send AT commands\r\n");
-        return "";
-    }
+    //if(socketOpened && command.compare("$$") != 0 && command.compare("exit") != 0 && command.compare("close") != 0) {
+    //    printf("[ERROR] socket is open. Can not send AT commands\r\n");
+    //    return "";
+    //}
 
     io->rxClear();
     io->txClear();