Forked mbed official WiflyInterface (interface for Roving Networks Wifly modules) which includes the possibility to use TCPSocketServer::accept as a non-blocking cal.

Dependents:   WiFlyHTTPServerSample MultiThreadingHTTPServer

Fork of WiflyInterface by mbed official

Revision:
11:63a7dc9e45dd
Parent:
10:d84defb718ab
Child:
12:4f95e6a365db
--- a/Wifly/Wifly.cpp	Sat Jun 08 07:07:05 2013 +0000
+++ b/Wifly/Wifly.cpp	Wed Jun 26 21:12:21 2013 +0000
@@ -21,20 +21,18 @@
 #include <string>
 #include <algorithm>
 
+DigitalOut  ledrx(LED3);
+
 //Debug is disabled by default
-#if (0 && !defined(TARGET_LPC11U24))
+#if (1 && !defined(TARGET_LPC11U24))
 #define DBG(x, ...) std::printf("[Wifly : DBG]"x"\r\n", ##__VA_ARGS__);
 #define WARN(x, ...) std::printf("[Wifly : WARN]"x"\r\n", ##__VA_ARGS__);
 #define ERR(x, ...) std::printf("[Wifly : ERR]"x"\r\n", ##__VA_ARGS__);
+#define INFO(x, ...) printf("[Wifly : INFO]"x"\r\n", ##__VA_ARGS__);
 #else
 #define DBG(x, ...)
 #define WARN(x, ...)
 #define ERR(x, ...)
-#endif
-
-#if !defined(TARGET_LPC11U24)
-#define INFO(x, ...) printf("[Wifly : INFO]"x"\r\n", ##__VA_ARGS__);
-#else
 #define INFO(x, ...)
 #endif
 
@@ -83,7 +81,7 @@
             continue;
 
         //no echo
-        if (!sendCommand("set u m 1\r", "AOK"))
+        if (!sendCommand("set u m 17\r", "AOK"))
             continue;
 
         // set time
@@ -458,10 +456,10 @@
 bool Wifly::close()
 {
     // if not connected, return
-    if (!state.tcp)
-        return true;
+//    if (!state.tcp)
+//        return true;
 
-    wait(0.25);
+//    Thread::wait(250);
     if (!sendCommand("close\r", "CLOS"))
         return false;
     exit(false);
@@ -527,6 +525,7 @@
     while (wifi.readable()) {
         char c = LPC_UART3->RBR;
         buf_wifly.queue(c);
+        ledrx = !ledrx;
     }
 }
 
@@ -538,7 +537,6 @@
         wifi.attach(this, &Wifly::handler_rx);
 }
 
-
 int Wifly::send(const char * str, int len, const char * ACK, char * res, int timeout)
 {
     char read;
@@ -576,8 +574,10 @@
         while (1) {
             if (tmr.read_ms() > timeout) {
                 //We flush the buffer
-                while (wifi.readable())
-                    wifi.getc();
+                while (wifi.readable()) {
+                    char c = wifi.getc();
+                    INFO("Flushing : %c",c);
+                }
 
                 DBG("check: %s\r\n", checking.c_str());
 
@@ -591,8 +591,10 @@
                     if (found != string::npos) {
                         wait(0.01);
                         //We flush the buffer
-                        while (wifi.readable())
-                            wifi.getc();
+                        while (wifi.readable()) {
+                            char c = wifi.getc();
+                            INFO("Flushing : %c",c);
+                        }
 
                         break;
                     }
@@ -643,10 +645,32 @@
     }
 
     //We flush the buffer
-    while (wifi.readable())
-        wifi.getc();
+    while (wifi.readable()) {
+        char c = wifi.getc();
+        INFO("Flushing : %c",c);
+    }
 
     attach_rx(true);
     DBG("result: %d\r\n", result)
     return result;
+}
+
+
+int Wifly::sendData(const char* data, int len, int _timeout)
+{
+    int result = 0;
+    Timer tmr;
+
+    tmr.start();
+    while (tmr.read_ms() < _timeout) {
+        if (wifi.writeable())
+            break;
+    }
+    if (tmr.read_ms() >= _timeout) {
+        return -1;
+    }
+    for (int i = 0; i < len; i++)
+        result = (putc(data[i]) == data[i]) ? result + 1 : result;
+
+    return result;
 }
\ No newline at end of file