Library to use a wifly module: RN 131 C/G

Dependents:   RN-XV_simple_server

Revision:
17:56f3c4da1ea8
Parent:
16:f524a28790b1
Child:
18:88a26cd00ba3
diff -r f524a28790b1 -r 56f3c4da1ea8 Wifly.cpp
--- a/Wifly.cpp	Sun Nov 06 12:15:50 2011 +0000
+++ b/Wifly.cpp	Mon Jan 30 10:53:56 2012 +0000
@@ -2,16 +2,12 @@
 #include "Wifly.h"
 #include <string>
 
-
-
-
-
+//#define DEBUG
 
 Wifly::Wifly(   PinName tx, PinName rx, PinName _reset, char * ssid, char * phrase,
                 bool wpa, char * ip, char * netmask, bool dhcp, int baudrate):
         wifi(tx, rx), reset_pin(_reset) {
     wifi.baud(baudrate);
-    reset_pin = 1;
     wifi.format(8, Serial::None, 1);
     this->wpa = wpa;
     this->dhcp = dhcp;
@@ -20,43 +16,38 @@
     strcpy(this->ip, ip);
     strcpy(this->netmask, netmask);
     adhoc = false;
-    length = 0;
-    new_msg = false;
+
+    reset();
 }
 
 
 
 
 
-Wifly::Wifly(   PinName tx, PinName rx, PinName _reset, char * ssid, char * ip, char * netmask, int channel,
-                int baudrate):
+Wifly::Wifly(   PinName tx, PinName rx, PinName _reset, char * ssid, char * ip, char * netmask, int channel, int baudrate):
         wifi(tx, rx), reset_pin(_reset) {
     wifi.baud(baudrate);
-    reset_pin = 1;
     wifi.format(8, Serial::None, 1);
     adhoc = true;
     strcpy(this->ssid, ssid);
     strcpy(this->ip, ip);
     strcpy(this->netmask, netmask);
     this->channel = channel;
-    length = 0;
-    new_msg = false;
+
+    reset();
 }
 
 
 void Wifly::handler_rx(void) {
+
+    //read the character
     char read = wifi.getc();
-    if (read != '\r' && read != '\n') {
-        tmp_buf_rx[length] = read;
-        length++;
-        new_msg = false;
-    }
-    if (read == 0xff || read == '\n') {
-        new_msg = true;
-        tmp_buf_rx[length] = 0;
-        memcpy(buf_rx, tmp_buf_rx, length);
-        length = 0;
-    }
+
+    //queue it
+    buf_wifly.queue(read);
+
+    //call user callback
+    rx.call();
 }
 
 void Wifly::attach_rx(bool null) {
@@ -69,32 +60,31 @@
 
 
 bool Wifly::send(char * str, char * ACK, char * res) {
+    char read;
+    size_t found = string::npos;
+    Timer tmr;
 
-    char read;
 
     attach_rx(false);
-
-    size_t found = string::npos;
-
-    if (!strcmp(ACK, "NO"))
+    if (!strcmp(ACK, "NO")) {
         wifi.printf("%s", str);
-    else {
-
+    } else {
         //We flush the buffer
         while (wifi.readable())
             wifi.getc();
 
-        Timer tmr;
         tmr.start();
         wifi.printf("%s", str);
         string checking;
 
         while (1) {
-            if (tmr.read() > 5) {
+            if (tmr.read() > 2) {
                 //We flush the buffer
                 while (wifi.readable())
                     wifi.getc();
-
+#ifdef DEBUG
+                printf("check: %s\r\n", checking.c_str());
+#endif
                 attach_rx(true);
                 return false;
             } else if (wifi.readable()) {
@@ -103,6 +93,7 @@
                     checking += read;
                     found = checking.find(ACK);
                     if (found != string::npos) {
+                        wait(0.01);
 
                         //We flush the buffer
                         while (wifi.readable())
@@ -113,17 +104,35 @@
                 }
             }
         }
+#ifdef DEBUG
+        printf("check: %s\r\n", checking.c_str());
+#endif
         attach_rx(true);
-        return (found != string::npos);
+        return true;
     }
+
+    //the user wants the result from the command (ACK == "NO", res != NULL)
     if ( res != NULL) {
         int i = 0;
-        while (!wifi.readable());
-        wait(0.1);
-        while (wifi.readable()) {
-            read = wifi.getc();
-            if ( read != '\r' && read != '\n') {
-                res[i++] = read;
+        Timer timeout;
+        timeout.start();
+        while (1) {
+            if (timeout.read() > 3) {
+                read = NULL;
+                return false;
+            } else {
+                if (tmr.read_ms() > 500) {
+                    res[i] = 0;
+                    attach_rx(true);
+                    return false;
+                }
+                if (wifi.readable()) {
+                    tmr.start();
+                    read = wifi.getc();
+                    if ( read != '\r' && read != '\n') {
+                        res[i++] = read;
+                    }
+                }
             }
         }
     }
@@ -132,102 +141,132 @@
 }
 
 bool Wifly::join() {
-    char cmd[30];
-
-    exit();
-
-    if (!cmdMode()) {
-#ifdef DEBUG
-        printf("join: cannot enter in cmd mode\r\n");
-#endif
-        exit();
-        return false;
-    }
-
-    //auth
-    if (!send("set w a 3\r\n", "AOK")) {
-#ifdef DEBUG
-        printf("join: cannot set auth\r\n");
-#endif
-        exit();
-        return false;
-    }
+    char cmd[60];
 
-    //dhcp
-    sprintf(cmd, "set i d %d\r\n", (dhcp) ? 1 : 0);
-    if (!send(cmd, "AOK")) {
+    if (!adhoc) {
+        //enter in command mode
+        if (!cmdMode()) {
 #ifdef DEBUG
-        printf("join: cannot set dhcp\r\n");
+            printf("join: cannot enter in cmd mode\r\n");
 #endif
-        exit();
-        return false;
-    }
+            send("a\r\n");
+            wait(0.2);
+            exit();
+            if (!cmdMode())
+                return false;
+        }
 
-    //no echo
-    if (!send("set u m 1\r\n", "AOK")) {
-#ifdef DEBUG
-        printf("join: cannot set no echo\r\n");
-#endif
-        exit();
-        return false;
-    }
-
-    if (!dhcp) {
-#ifdef DEBUG
-        printf("not dhcp\r\n");
-#endif
-        sprintf(cmd, "set i a %s\r\n", ip);
+        //auth
+        sprintf(cmd, "set w a %d\r\n", (wpa) ? 3 : 1);
         if (!send(cmd, "AOK")) {
 #ifdef DEBUG
-            printf("Wifly::join: cannot set ip address\r\n");
+            printf("join: cannot set auth\r\n");
+#endif
+            exit();
+            return false;
+        }
+
+        //dhcp
+        sprintf(cmd, "set i d %d\r\n", (dhcp) ? 1 : 0);
+        if (!send(cmd, "AOK")) {
+#ifdef DEBUG
+            printf("join: cannot set dhcp\r\n");
+#endif
+            exit();
+            return false;
+        }
+
+        //no echo
+        if (!send("set u m 1\r\n", "AOK")) {
+#ifdef DEBUG
+            printf("join: cannot set no echo\r\n");
 #endif
             exit();
             return false;
         }
 
-        sprintf(cmd, "set i n %s\r\n", netmask);
+        // if no dhcp, set ip and netmask
+        if (!dhcp) {
+#ifdef DEBUG
+            printf("not dhcp\r\n");
+#endif
+            sprintf(cmd, "set i a %s\r\n", ip);
+            if (!send(cmd, "AOK")) {
+#ifdef DEBUG
+                printf("Wifly::join: cannot set ip address\r\n");
+#endif
+                exit();
+                return false;
+            }
+
+            sprintf(cmd, "set i n %s\r\n", netmask);
+            if (!send(cmd, "AOK")) {
+#ifdef DEBUG
+                printf("Wifly::join: cannot set netmask\r\n");
+#endif
+                exit();
+                return false;
+            }
+
+        }
+
+        //key step
+        if (wpa)
+            sprintf(cmd, "set w p %s\r\n", phrase);
+        else
+            sprintf(cmd, "set w k %s\r\n", phrase);
+
         if (!send(cmd, "AOK")) {
 #ifdef DEBUG
-            printf("Wifly::join: cannot set netmask\r\n");
+            printf("join: cannot set phrase\r\n");
 #endif
             exit();
             return false;
         }
 
-    }
+        if (!send("set w j 1\r\n", "AOK")) {
+#ifdef DEBUG
+            printf("Wifly::join: cannot set join 1\r\n");
+#endif
+            exit();
+            return false;
+        }
+
+        if (!send("save\r\n", "Stor")) {
+#ifdef DEBUG
+            printf("Wifly::join: cannot save\r\n");
+#endif
+            exit();
+            return false;
+        }
+
+        //join the network
+        sprintf(cmd, "join %s\r\n", ssid);
 
-    //key step
-    if (wpa)
-        sprintf(cmd, "set w p %s\r\n", phrase);
-    else
-        sprintf(cmd, "set w k %s\r\n", phrase);
+        if (!send(cmd, "IP=")) {
+#ifdef DEBUG
+            printf("join: cannot join %s\r\n", ssid);
+#endif
+            exit();
+            return false;
+        }
+        exit();
 
-    if (!send(cmd, "AOK")) {
+        wait(0.1);
+        flush();
+
 #ifdef DEBUG
-        printf("join: cannot set phrase\r\n");
+        printf("\r\nssid: %s\r\nphrase: %s\r\nsecurity: %s\r\n\r\n", this->ssid, this->phrase, (wpa) ? "WPA" : "WEP");
 #endif
-        exit();
+        return true;
+    } else {
+#ifdef DEBUG
+        printf("Wifly::join: You didn't choose the right constructor to join a network!\r\n");
+#endif
         return false;
     }
 
 
-
-    //join the network
-    sprintf(cmd, "join %s\r\n", ssid);
-
-    if (!send(cmd, "IP=")) {
-#ifdef DEBUG
-        printf("join: cannot join %s\r\n", ssid);
-#endif
-        exit();
-        return false;
-    }
-    exit();
-#ifdef DEBUG
-    printf("\r\nssid: %s\r\nphrase: %s\r\nsecurity: %s\r\n\r\n", this->ssid, this->phrase, (wpa) ? "WPA" : "WEP");
-#endif
-    return true;
-
 }
 
 
@@ -236,8 +275,6 @@
     if (adhoc) {
         char cmd[50];
 
-        exit();
-
         if (!cmdMode()) {
 #ifdef DEBUG
             printf("Wifly::CreateAdhocNetwork: cannot enter in cmd mode\r\n");
@@ -257,7 +294,7 @@
         //no echo
         if (!send("set u m 1\r\n", "AOK")) {
 #ifdef DEBUG
-            printf("join: cannot set no echo\r\n");
+            printf("Wifly::CreateAdhocNetwork: cannot set no echo\r\n");
 #endif
             exit();
             return false;
@@ -316,19 +353,29 @@
             return false;
         }
 
+        flush();
+
         send("reboot\r\n", "NO");
 #ifdef DEBUG
         printf("\r\ncreating an adhoc\r\nnetwork: %s\r\nip: %s\r\nnetmask: %s\r\nchannel: %d\r\n\r\n", ssid, ip, netmask, channel);
 #endif
+
+        wait(0.2);
+        flush();
         return true;
     } else {
 #ifdef DEBUG
-        printf("Wifly::join: You don't chose the right constructor for creating an adhoc mode!\r\n");
+        printf("Wifly::CreateAdhocNetwork: You didn't choose the right constructor for creating an adhoc mode!\r\n");
 #endif
         return false;
     }
 }
 
+void Wifly::flush() {
+    while (readable())
+        getc();
+}
+
 bool Wifly::cmdMode() {
     if (!send("$$$", "CMD")) {
 #ifdef DEBUG
@@ -344,10 +391,8 @@
 
 void Wifly::reset() {
     reset_pin = 0;
-    memset(buf_rx, 0, 100);
     wait(0.2);
     reset_pin = 1;
-    memset(tmp_buf_rx, 0, 100);
     wait(0.2);
 }
 
@@ -364,12 +409,13 @@
 
 
 bool Wifly::read(char * str) {
-    if (new_msg && str != NULL) {
-        memcpy(str, buf_rx, strlen(buf_rx + 1) + 1);
-        new_msg = false;
-        return true;
-    }
-    return false;
+    int length = buf_wifly.available();
+    if (length == 0)
+        return false;
+    for (int i = 0; i < length; i++)
+        buf_wifly.dequeue((uint8_t *)&str[i]);
+    str[length] = 0;
+    return true;
 }
 
 
@@ -381,8 +427,8 @@
 
 
 
-bool Wifly::readable() {
-    return(wifi.readable());
+int Wifly::readable() {
+    return buf_wifly.available();
 }
 
 bool Wifly::changeBaudrate(int baudrate) {
@@ -417,5 +463,8 @@
 
 
 char Wifly::getc() {
-    return(wifi.getc());
+    char c;
+    while (!buf_wifly.available());
+    buf_wifly.dequeue((uint8_t *)&c);
+    return c;
 }
\ No newline at end of file