- Auto reconnect - Added is_associated method

Fork of WiflyInterface by mbed official

Files at this revision

API Documentation at this revision

Comitter:
kingkingyyk
Date:
Fri Dec 16 02:50:56 2016 +0000
Parent:
9:c77799a03294
Commit message:
- Added auto-reconnect; - Added is_associated method

Changed in this revision

Socket/TCPSocketServer.cpp Show annotated file Show diff for this revision Revisions of this file
Socket/TCPSocketServer.h Show annotated file Show diff for this revision Revisions of this file
Wifly/Wifly.cpp Show annotated file Show diff for this revision Revisions of this file
Wifly/Wifly.h Show annotated file Show diff for this revision Revisions of this file
WiflyInterface.cpp Show annotated file Show diff for this revision Revisions of this file
WiflyInterface.h Show annotated file Show diff for this revision Revisions of this file
--- a/Socket/TCPSocketServer.cpp	Tue Jan 28 11:00:48 2014 +0200
+++ b/Socket/TCPSocketServer.cpp	Fri Dec 16 02:50:56 2016 +0000
@@ -87,4 +87,34 @@
             }
         }
     }
+}
+
+int TCPSocketServer::acceptWithTimeout(TCPSocketConnection& connection, int timeout) {
+    int nb_available = 0, pos = 0;
+    char c;
+    string str;
+    bool o_find = false;
+    time_t currTime = time(NULL);
+    while (1) {
+        while(!wifi->readable() && time(NULL)-currTime<=timeout);
+        if (time(NULL)-currTime>timeout) {
+            wifi->flush();
+            return -1;
+        }
+        nb_available = wifi->readable();
+        for (int i = 0; i < nb_available; i++) {
+            c = wifi->getc();
+            if (c == '*') {
+                o_find = true;
+            }
+            if (o_find && c != '\r' && c != '\n') {
+                str += c;
+                pos = str.find("*OPEN*");
+                if (pos != string::npos) {
+                    wifi->flush();
+                    return 0;
+                }
+            }
+        }
+    }
 }
\ No newline at end of file
--- a/Socket/TCPSocketServer.h	Tue Jan 28 11:00:48 2014 +0200
+++ b/Socket/TCPSocketServer.h	Fri Dec 16 02:50:56 2016 +0000
@@ -47,6 +47,12 @@
     \return 0 on success, -1 on failure.
     */
     int accept(TCPSocketConnection& connection);
+    
+    /** Accept a new connection.
+    \param connection A TCPSocketConnection instance that will handle the incoming connection.
+    \return 0 on success, -1 on failure.
+    */
+    int acceptWithTimeout(TCPSocketConnection& connection, int timeout);
 };
 
 #endif
--- a/Wifly/Wifly.cpp	Tue Jan 28 11:00:48 2014 +0200
+++ b/Wifly/Wifly.cpp	Fri Dec 16 02:50:56 2016 +0000
@@ -71,8 +71,8 @@
 
     for (int i= 0; i < MAX_TRY_JOIN; i++) {
 
-        // no auto join
-        if (!sendCommand("set w j 0\r", "AOK"))
+        // force auto join
+        if (!sendCommand("set w j 1\r", "AOK"))
             continue;
 
         //no echo
@@ -121,6 +121,10 @@
         sprintf(cmd, "set w a %d\r", state.sec);
         if (!sendCommand(cmd, "AOK"))
             continue;
+            
+        //tcp
+        sprintf(cmd, "set comm idle 0%d\r", state.sec);
+        if (!sendCommand(cmd, "AOK"))
 
         // if no dhcp, set ip, netmask and gateway
         if (!state.dhcp) {
@@ -390,6 +394,12 @@
     return wifi.putc(c);
 }
 
+bool Wifly::associated()
+{
+    bool flag=sendCommand("show c\r", "8630", NULL, 1000);
+    exit();
+    return flag;
+}
 
 bool Wifly::exit()
 {
--- a/Wifly/Wifly.h	Tue Jan 28 11:00:48 2014 +0200
+++ b/Wifly/Wifly.h	Fri Dec 16 02:50:56 2016 +0000
@@ -77,7 +77,7 @@
     * @return true if successful
     */
     bool disconnect();
-
+    bool associated();
     /**
     * Open a tcp connection with the specified host on the specified port
     *
--- a/WiflyInterface.cpp	Tue Jan 28 11:00:48 2014 +0200
+++ b/WiflyInterface.cpp	Fri Dec 16 02:50:56 2016 +0000
@@ -37,6 +37,11 @@
     return Wifly::disconnect();
 }
 
+bool WiflyInterface::is_associated()
+{
+    return Wifly::associated();
+}
+
 char * WiflyInterface::getIPAddress()
 {
     char * match = 0;
--- a/WiflyInterface.h	Tue Jan 28 11:00:48 2014 +0200
+++ b/WiflyInterface.h	Fri Dec 16 02:50:56 2016 +0000
@@ -73,6 +73,7 @@
   * \return ip address
   */
   char* getIPAddress();
+  bool is_associated();
   
 private:
     char ip_string[20];