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

Dependents:   RN-XV_simple_server

Revision:
10:d3e3e925f62f
Parent:
9:984c1436dd42
Child:
11:6eb90004d92a
--- a/Wifly.cpp	Mon Aug 15 16:05:21 2011 +0000
+++ b/Wifly.cpp	Tue Sep 06 16:44:24 2011 +0000
@@ -7,10 +7,9 @@
 
 
 
-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)
-{
+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);
@@ -21,16 +20,17 @@
     this->netmask = netmask;
     this->dhcp = dhcp;
     adhoc = false;
+    length = 0;
+    new_msg = false;
 }
 
 
 
 
 
-Wifly::Wifly(   PinName tx, PinName rx, PinName _reset, char * ssid, char * ip, char * netmask, int channel, 
-                int baudrate): 
-                wifi(tx, rx), reset_pin(_reset)
-{
+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);
@@ -39,138 +39,169 @@
     this->ip = ip;
     this->netmask = netmask;
     this->channel = channel;
+    length = 0;
+    new_msg = false;
+}
+
+
+void Wifly::handler_rx(void) {
+    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;
+    }
+}
+
+void Wifly::attach_rx(bool null) {
+    if (!null)
+        wifi.attach(NULL);
+    else
+        wifi.attach(this, &Wifly::handler_rx);
 }
 
 
 
-
+bool Wifly::Send(char * str, char * ACK, char * res) {
 
-
-bool Wifly::Send(char * str, char * ACK) {
+    char read;
 
-    //We flush the buffer
-    while(wifi.readable())
-        //printf("%c",wifi.getc());
-        wifi.getc();
-    
-    char read;
-    
+    attach_rx(false);
+
     size_t found = string::npos;
-    
+
     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() > 3)
-            {
+
+        while (1) {
+            if (tmr.read() > 3) {
+                //We flush the buffer
+                while (wifi.readable())
+                    wifi.getc();
+
                 //printf("checking: %s\r\n", checking);
+                attach_rx(true);
                 return false;
-            }
-            else if(wifi.readable())
-            {
+            } else if (wifi.readable()) {
                 read = wifi.getc();
-                if ( read != '\r' && read != '\n')
-                {
+                if ( read != '\r' && read != '\n') {
                     checking += read;
                     found = checking.find(ACK);
-                    if(found != string::npos)
-                    {
+                    if (found != string::npos) {
+
+                        //We flush the buffer
+                        while (wifi.readable())
+                            wifi.getc();
+
                         //printf("checking: %s\r\n", checking);
                         break;
                     }
                 }
             }
         }
+        attach_rx(true);
         return (found != string::npos);
     }
+    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;
+            }
+        }
+    }
+    attach_rx(true);
     return true;
 }
 
-bool Wifly::Join()
-{
+bool Wifly::Join() {
     char cmd[30];
-    
+
     exit();
-    
-    if(!CmdMode())
-    {
+
+    if (!CmdMode()) {
         printf("join: cannot enter in cmd mode\r\n");
         exit();
-        return false;  
+        return false;
     }
-    
+
     //auth
-    if(!Send("set wlan auth 3\r\n", "AOK"))
-    {
+    if (!Send("set wlan auth 3\r\n", "AOK")) {
         printf("join: cannot set auth\r\n");
         exit();
         return false;
     }
-    
+
     //dhcp
     sprintf(cmd, "set ip dhcp %d\r\n", (dhcp) ? 1 : 0);
-    if(!Send(cmd, "AOK"))
-    {
+    if (!Send(cmd, "AOK")) {
         printf("join: cannot set dhcp\r\n");
         exit();
         return false;
     }
-    
+
     //no echo
-    if(!Send("set uart mode 1\r\n", "AOK"))
-    {
+    if (!Send("set uart mode 1\r\n", "AOK")) {
         printf("join: cannot set no echo\r\n");
         exit();
         return false;
     }
-    
-    if(!dhcp)
-    {
+
+    if (!dhcp) {
         printf("not dhcp\r\n");
         sprintf(cmd, "set ip address %s\r\n", ip);
-        if(!Send(cmd, "AOK"))
-        {
+        if (!Send(cmd, "AOK")) {
             printf("Wifly::Join: cannot set ip address\r\n");
             exit();
             return false;
         }
-    
+
         sprintf(cmd, "set ip netmask %s\r\n", netmask);
-        if(!Send(cmd, "AOK"))
-        {
+        if (!Send(cmd, "AOK")) {
             printf("Wifly::join: cannot set netmask\r\n");
             exit();
             return false;
         }
-    
+
     }
-    
+
     //key step
-    if(wpa)
+    if (wpa)
         sprintf(cmd, "set wlan phrase %s\r\n", phrase);
     else
         sprintf(cmd, "set wlan key %s\r\n", phrase);
-    
-    if(!Send(cmd, "AOK"))
-    {
+
+    if (!Send(cmd, "AOK")) {
         printf("join: cannot set phrase\r\n");
         exit();
         return false;
     }
-    
-        
-    
+
+
+
     //join the network
     sprintf(cmd, "join %s\r\n", ssid);
-    
-    if(!Send(cmd, "IP="))
-    {
+
+    if (!Send(cmd, "IP=")) {
         printf("join: cannot join %s\r\n", ssid);
         exit();
         return false;
@@ -178,102 +209,88 @@
     exit();
     printf("\r\nssid: %s\r\nphrase: %s\r\nsecurity: %s\r\n\r\n", this->ssid, this->phrase, (wpa) ? "WPA" : "WEP");
     return true;
-    
+
 }
 
 
-bool Wifly::CreateAdhocNetwork()
-{
-    if(adhoc)
-    {
+
+bool Wifly::CreateAdhocNetwork() {
+    if (adhoc) {
         char cmd[50];
-        
+
         exit();
-    
-        if(!CmdMode())
-        {
+
+        if (!CmdMode()) {
             printf("Wifly::CreateAdhocNetwork: cannot enter in cmd mode\r\n");
             exit();
-            return false;  
+            return false;
         }
-    
-        if(!Send("set wlan join 4\r\n", "AOK"))
-        {
+
+        if (!Send("set wlan join 4\r\n", "AOK")) {
             printf("Wifly::CreateAdhocNetwork: cannot set join 4\r\n");
             exit();
             return false;
         }
-        
+
         //no echo
-        if(!Send("set uart mode 1\r\n", "AOK"))
-        {
+        if (!Send("set uart mode 1\r\n", "AOK")) {
             printf("join: cannot set no echo\r\n");
             exit();
             return false;
         }
-    
+
         //ssid
         sprintf(cmd, "set wlan ssid %s\r\n", ssid);
-        if(!Send(cmd, "AOK"))
-        {
+        if (!Send(cmd, "AOK")) {
             printf("Wifly::CreateAdhocNetwork: cannot set ssid\r\n");
             exit();
             return false;
         }
-    
+
         sprintf(cmd, "set wlan channel %d\r\n", channel);
-        if(!Send(cmd, "AOK"))
-        {
+        if (!Send(cmd, "AOK")) {
             printf("Wifly::CreateAdhocNetwork: cannot set channel\r\n");
             exit();
             return false;
         }
-    
+
         sprintf(cmd, "set ip address %s\r\n", ip);
-        if(!Send(cmd, "AOK"))
-        {
+        if (!Send(cmd, "AOK")) {
             printf("Wifly::CreateAdhocNetwork: cannot set ip address\r\n");
             exit();
             return false;
         }
-    
+
         sprintf(cmd, "set ip netmask %s\r\n", netmask);
-        if(!Send(cmd, "AOK"))
-        {
+        if (!Send(cmd, "AOK")) {
             printf("Wifly::CreateAdhocNetwork: cannot set netmask\r\n");
             exit();
             return false;
         }
-    
-        if(!Send("set ip dhcp 0\r\n", "AOK"))
-        {
+
+        if (!Send("set ip dhcp 0\r\n", "AOK")) {
             printf("Wifly::CreateAdhocNetwork: cannot set dhcp off\r\n");
             exit();
             return false;
         }
-    
-        if(!Send("save\r\n", "Stor"))
-        {
+
+        if (!Send("save\r\n", "Stor")) {
             printf("Wifly::CreateAdhocNetwork: cannot save\r\n");
             exit();
             return false;
         }
-    
+
         Send("reboot\r\n", "NO");
         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);
         return true;
-    }
-    else
-    {
+    } else {
         printf("Wifly::join: You don't chose the right constructor for creating an adhoc mode!\r\n");
         return false;
     }
 }
 
-bool Wifly::CmdMode() 
-{
-    if(!Send("$$$", "CMD"))
-    {
+bool Wifly::CmdMode() {
+    if (!Send("$$$", "CMD")) {
         printf("Wifly::CmdMode: cannot enter in cmd mode\r\n");
         return false;
     }
@@ -283,11 +300,12 @@
 
 
 
-void Wifly::reset()
-{
+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);
 }
 
@@ -296,64 +314,59 @@
 
 
 
-void Wifly::putc(char c)
-{
+void Wifly::putc(char c) {
     wifi.putc(c);
 }
 
 
 
 
-void Wifly::read(char * str) 
-{
-    wifi.scanf("%s", str);
+void Wifly::read(char * str) {
+    if (new_msg) {
+        memcpy(str, buf_rx, strlen(buf_rx + 1) + 1);
+        new_msg = false;
+    } else
+        str = NULL;
 }
 
 
 
 
-bool Wifly::exit() 
-{
+bool Wifly::exit() {
     return Send("exit\r", "EXIT");
 }
 
 
 
-bool Wifly::readable()
-{
+bool Wifly::readable() {
     return(wifi.readable());
 }
 
-bool Wifly::changeBaudrate(int baudrate)
-{
+bool Wifly::changeBaudrate(int baudrate) {
     char cmd[20];
     exit();
-    if(!CmdMode())
-    {
+    if (!CmdMode()) {
         printf("Wifly::changeBaudrate: cannot enter in cmd mode\r\n");
         return false;
     }
-    
+
     sprintf(cmd, "set u b %d\r\n", baudrate);
-    if(!Send(cmd, "AOK"))
-    {
+    if (!Send(cmd, "AOK")) {
         printf("Wifly::changeBaudrate: cannot set new baudrate\r\n");
         exit();
         return false;
     }
-    
-    if(!Send("save\r\n", "Stor"))
-    {
+
+    if (!Send("save\r\n", "Stor")) {
         printf("Wifly::changeBaudrate: cannot save\r\n");
         exit();
         return false;
     }
-    
+
     return true;
 }
 
 
-char Wifly::getc()
-{
+char Wifly::getc() {
     return(wifi.getc());
 }
\ No newline at end of file