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:
138:a233b9775f93
Parent:
136:0937dce3630b
Child:
141:571e0ef6c8dc
--- a/wifi/Wifi.cpp	Fri Jan 03 22:42:06 2014 +0000
+++ b/wifi/Wifi.cpp	Sat Jan 04 00:55:16 2014 +0000
@@ -1,10 +1,10 @@
 #include "Wifi.h"
-#include <string>
+#include "MTSText.h"
 
 #if 0
 //Enable debug
 #include <cstdio>
-#define DBG(x, ...) std::printf("Line: %d %s \t[Wifi : DBG]"x"\r\n", __LINE__, __FILE__, ##__VA_ARGS__);  
+#define DBG(x, ...) std::printf("Line: %d %s \t[Wifi : DBG]"x"\r\n", __LINE__, __FILE__, ##__VA_ARGS__);
 #else
 #define DBG(x, ...)
 #endif
@@ -20,7 +20,7 @@
 }
 
 bool Wifi::sortInterfaceMode(void)
-{   
+{
     //Check initial state of command mode
     std::string response = sendCommand("", 1000, ">");
     if(response.find(">") != string::npos) {
@@ -31,7 +31,7 @@
     if (!setCmdMode(true)) {
         return false;
     }
-    
+
     return true;
 }
 
@@ -41,15 +41,15 @@
         return false;
     }
     instance->io = io;
-    
+
     // start from the same place each time
     reset();
-    
+
     //Secure interface mode
     if(!sortInterfaceMode()) {
         return false;
     }
-    
+
     //Set device to non-echo mode
     while (sendBasicCommand("set uart mode 1", 1000) != SUCCESS) {
         printf("[ERROR] Failed to set to non-echo mode\n\r");
@@ -287,14 +287,29 @@
         printf("[ERROR] Host port could not be set\r\n");
     }
 
-    if(sendBasicCommand("set ip host " + address, 1000) == SUCCESS) {
+    //Check if address of URL
+    std::vector<std::string> tmp = Text::split(address, '.');
+    if(tmp.size() != 4) {
+        std::string ip = getHostByName(address);
+        if(ip.size() != 0) {
+            host_address = ip;
+        } else {
+            return false;
+        }
+    } else {
         host_address = address;
-    } else {
+    }
+
+    //Set Address
+    printf("[DEBUG] Host address: %s\n\r", host_address.c_str());
+    if(sendBasicCommand("set ip host " + host_address, 1000) != SUCCESS) {
         printf("[ERROR] Host address could not be set\r\n");
+        return false;
     }
 
     if(sendBasicCommand("set ip protocol 8", 1000) != SUCCESS) {
         printf("[ERROR] Failed to set TCP mode\r\n");
+        return false;
     }
 
     // Try and Connect
@@ -310,11 +325,11 @@
     }
     string response = sendCommand(sOpenSocketCmd, 10000, "OPEN");
     if (response.find("OPEN") != string::npos) {
-        printf("[INFO] Opened %s Socket [%s:%d]\r\n", sMode.c_str(), address.c_str(), port);
+        printf("[INFO] Opened %s Socket [%s:%d]\r\n", sMode.c_str(), host_address.c_str(), port);
         socketOpened = true;
         cmdOn = false;
     } else {
-        printf("[WARNING] Unable to open %s Socket [%s:%d]\r\n", sMode.c_str(),  address.c_str(), port);
+        printf("[WARNING] Unable to open %s Socket [%s:%d]\r\n", sMode.c_str(),  host_address.c_str(), port);
         socketOpened = false;
     }
 
@@ -469,11 +484,11 @@
     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;
@@ -638,6 +653,20 @@
     }
 }
 
+std::string Wifi::getHostByName(std::string url)
+{
+    std::string response = sendCommand("lookup " + url, 3000, "<4.00>");
+    int start = response.find("=");
+    int stop = response.find("\r");
+    if(start == string::npos || stop == string::npos) {
+        printf("[ERROR] Failed to resolve URL [%s]", response.c_str());
+        return "";
+    }
+    std::string ip = response.substr(start + 1, stop - start - 1);
+    //printf("Data: %s\n\r", ip);
+    return ip;
+}
+
 Code Wifi::sendBasicCommand(string command, int timeoutMillis, char esc)
 {
     if(socketOpened) {
@@ -687,7 +716,7 @@
         }
     }
     DBG("Sending: %s%c", command.data(), esc);
-    
+
     int timer = 0;
     size_t previous = 0;
     char tmp[256];