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:
95:4fdf968b5b37
Parent:
94:1baa587e89ae
Child:
97:5e65d8f334d5
Child:
98:dbeac735109d
--- a/wifi/Wifi.cpp	Mon Dec 30 16:18:49 2013 +0000
+++ b/wifi/Wifi.cpp	Mon Dec 30 17:46:11 2013 +0000
@@ -189,16 +189,134 @@
 {
     //set comm size??? are advanced Socket settings
     //set comm time??? are advanced Socket settings
-    return true;
+    char buffer[256] = {0};
+    printf("[DEBUG] Attempting to Open Socket\r\n");
+
+    //1) Check that we do not have a live connection up
+    if(socketOpened) {
+        //Check that the address, port, and mode match
+        if(host_address != address || host_port != port || this->mode != mode) {
+            if(this->mode == TCP) {
+                printf("[ERROR] TCP socket already opened (%s:%d)\r\n", host_address.c_str(), host_port);
+            } else {
+                printf("[ERROR] UDP socket already opened (%s:%d)\r\n", host_address.c_str(), host_port);
+            }
+            return false;
+        }
+
+        printf("[DEBUG] Socket already opened\r\n");
+        return true;
+    }
+
+    //2) Check Parameters
+    if(port > 65535) {
+        printf("[ERROR] port out of range (0-65535)\r\n");
+        return false;
+    }
+
+
+    //3) Check Wifi network connection
+    if(!isConnected()) {
+        printf("[ERROR] Wifi network not connected.  Attempting to connect\r\n");
+        if(!connect()) {
+            printf("[ERROR] Wifi network connection failed\r\n");
+            return false;
+        } else {
+            printf("[DEBUG] Wifi connection established\r\n");
+        }
+    }
+
+    //Check command mode
+    if (!cmdOn) {
+        if(!setCmdMode(true)) {
+            printf("[ERROR] Failed to enter command mode\n\r");
+            return false;
+        }
+    }
+
+    //Set Local Port
+    if(local_port != 0) {
+        //Attempt to set local port
+        sprintf(buffer, "set ip localport %d", local_port);
+        Code code = sendBasicCommand(buffer, 1000);
+        if(code != SUCCESS) {
+            printf("[WARNING] Unable to set local port (%d) [%d]. Continuing...\r\n", local_port, (int) code);
+        }
+    }
+
+    //Set TCP/UDP parameters
+    sprintf(buffer, "set ip remote %d", port);
+    if(sendBasicCommand(buffer, 1000) == SUCCESS) {
+        host_port = port;
+    } else {
+        printf("[ERROR] Host port could not be set\r\n");
+    }
+
+    if(sendBasicCommand("set ip host " + address, 1000) == SUCCESS) {
+        host_address = address;
+    } else {
+        printf("[ERROR] Host address could not be set\r\n");
+    }
+
+    if(sendBasicCommand("set ip host " + address, 1000) == SUCCESS) {
+        host_address = address;
+    } else {
+        printf("[ERROR] Host address could not be set\r\n");
+    }
+
+    // Try and Connect
+    std::string sMode;
+    std::string sOpenSocketCmd;
+    if(mode == TCP) {
+        sOpenSocketCmd = "open";
+        sMode = "TCP";
+    } else {
+        sOpenSocketCmd = "AT#OUDP";
+        sMode = "UDP";
+    }
+    string response = sendCommand(sOpenSocketCmd, 20000, "OPEN");
+    printf("Open Response: %s\n\r", response.c_str());
+    if (response.find("OPEN") != string::npos) {
+        printf("[INFO] Opened %s Socket [%s:%d]\r\n", sMode.c_str(), address.c_str(), port);
+        socketOpened = true;
+    } else {
+        printf("[WARNING] Unable to open %s Socket [%s:%d]\r\n", sMode.c_str(),  address.c_str(), port);
+        socketOpened = false;
+    }
+
+    return socketOpened;
 }
 
 bool Wifi::isOpen()
 {
-    return true;
+    return socketOpened;
 }
 
 bool Wifi::close()
 {
+    if(io == NULL) {
+        printf("[ERROR] MTSBufferedIO not set\r\n");
+        return false;
+    }
+
+    if(!socketOpened) {
+        printf("[WARNING] Socket close() called, but socket was not open\r\n");
+        return true;
+    }
+
+    if (!cmdOn) {
+        if(!setCmdMode(true)) {
+            printf("[ERROR] Failed to enter command mode\n\r");
+        }
+    }
+
+    std::string response = sendCommand("close", 10000, "CLOS");
+    if(response.find("CLOS" == string::npos)) {
+        printf("[ERROR] Timed out attempting to close socket\r\n");
+        return false;
+    }
+
+    socketOpened = false;
     return true;
 }
 
@@ -291,6 +409,18 @@
     return value;
 }
 
+bool Wifi::ping(const std::string& address)
+{
+    std::string response;
+    for (int i = 0; i < PINGNUM; i++) {
+        response = sendCommand("ping " + address, PINGDELAY * 1000, "reply");
+        if (response.find("reply") != std::string::npos) {
+            return true;
+        }
+    }
+    return false;
+}
+
 bool Wifi::setCmdMode(bool on)
 {
     if (on) {