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

Dependents:   RN-XV_simple_server

Revision:
11:6eb90004d92a
Parent:
10:d3e3e925f62f
Child:
12:fd6874542b28
--- a/Wifly.cpp	Tue Sep 06 16:44:24 2011 +0000
+++ b/Wifly.cpp	Wed Sep 21 09:31:47 2011 +0000
@@ -14,11 +14,11 @@
     reset_pin = 1;
     wifi.format(8, Serial::None, 1);
     this->wpa = wpa;
-    this->phrase = phrase;
-    this->ssid = ssid;
-    this->ip = ip;
-    this->netmask = netmask;
     this->dhcp = dhcp;
+    strcpy(this->phrase, phrase);
+    strcpy(this->ssid, ssid);
+    strcpy(this->ip, ip);
+    strcpy(this->netmask, netmask);
     adhoc = false;
     length = 0;
     new_msg = false;
@@ -35,9 +35,9 @@
     reset_pin = 1;
     wifi.format(8, Serial::None, 1);
     adhoc = true;
-    this->ssid = ssid;
-    this->ip = ip;
-    this->netmask = netmask;
+    strcpy(this->ssid, ssid);
+    strcpy(this->ip, ip);
+    strcpy(this->netmask, netmask);
     this->channel = channel;
     length = 0;
     new_msg = false;
@@ -139,45 +139,59 @@
     exit();
 
     if (!CmdMode()) {
+#ifdef DEBUG
         printf("join: cannot enter in cmd mode\r\n");
+#endif
         exit();
         return false;
     }
 
     //auth
-    if (!Send("set wlan auth 3\r\n", "AOK")) {
+    if (!Send("set w a 3\r\n", "AOK")) {
+#ifdef DEBUG
         printf("join: cannot set auth\r\n");
+#endif
         exit();
         return false;
     }
 
     //dhcp
-    sprintf(cmd, "set ip dhcp %d\r\n", (dhcp) ? 1 : 0);
+    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 uart mode 1\r\n", "AOK")) {
+    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");
-        sprintf(cmd, "set ip address %s\r\n", ip);
+#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 ip netmask %s\r\n", netmask);
+        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;
         }
@@ -186,12 +200,14 @@
 
     //key step
     if (wpa)
-        sprintf(cmd, "set wlan phrase %s\r\n", phrase);
+        sprintf(cmd, "set w p %s\r\n", phrase);
     else
-        sprintf(cmd, "set wlan key %s\r\n", phrase);
+        sprintf(cmd, "set w k %s\r\n", phrase);
 
     if (!Send(cmd, "AOK")) {
+#ifdef DEBUG
         printf("join: cannot set phrase\r\n");
+#endif
         exit();
         return false;
     }
@@ -202,12 +218,16 @@
     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;
 
 }
@@ -221,77 +241,101 @@
         exit();
 
         if (!CmdMode()) {
+#ifdef DEBUG
             printf("Wifly::CreateAdhocNetwork: cannot enter in cmd mode\r\n");
+#endif
             exit();
             return false;
         }
 
-        if (!Send("set wlan join 4\r\n", "AOK")) {
+        if (!Send("set w j 4\r\n", "AOK")) {
+#ifdef DEBUG
             printf("Wifly::CreateAdhocNetwork: cannot set join 4\r\n");
+#endif
             exit();
             return false;
         }
 
         //no echo
-        if (!Send("set uart mode 1\r\n", "AOK")) {
+        if (!Send("set u m 1\r\n", "AOK")) {
+#ifdef DEBUG
             printf("join: cannot set no echo\r\n");
+#endif
             exit();
             return false;
         }
 
         //ssid
-        sprintf(cmd, "set wlan ssid %s\r\n", ssid);
+        sprintf(cmd, "set w s %s\r\n", ssid);
         if (!Send(cmd, "AOK")) {
+#ifdef DEBUG
             printf("Wifly::CreateAdhocNetwork: cannot set ssid\r\n");
+#endif
             exit();
             return false;
         }
 
-        sprintf(cmd, "set wlan channel %d\r\n", channel);
+        sprintf(cmd, "set w c %d\r\n", channel);
         if (!Send(cmd, "AOK")) {
+#ifdef DEBUG
             printf("Wifly::CreateAdhocNetwork: cannot set channel\r\n");
+#endif
             exit();
             return false;
         }
 
-        sprintf(cmd, "set ip address %s\r\n", ip);
+        sprintf(cmd, "set i a %s\r\n", ip);
         if (!Send(cmd, "AOK")) {
+#ifdef DEBUG
             printf("Wifly::CreateAdhocNetwork: cannot set ip address\r\n");
+#endif
             exit();
             return false;
         }
 
-        sprintf(cmd, "set ip netmask %s\r\n", netmask);
+        sprintf(cmd, "set i n %s\r\n", netmask);
         if (!Send(cmd, "AOK")) {
+#ifdef DEBUG
             printf("Wifly::CreateAdhocNetwork: cannot set netmask\r\n");
+#endif
             exit();
             return false;
         }
 
-        if (!Send("set ip dhcp 0\r\n", "AOK")) {
+        if (!Send("set i d 0\r\n", "AOK")) {
+#ifdef DEBUG
             printf("Wifly::CreateAdhocNetwork: cannot set dhcp off\r\n");
+#endif
             exit();
             return false;
         }
 
         if (!Send("save\r\n", "Stor")) {
+#ifdef DEBUG
             printf("Wifly::CreateAdhocNetwork: cannot save\r\n");
+#endif
             exit();
             return false;
         }
 
         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
         return true;
     } else {
+#ifdef DEBUG
         printf("Wifly::join: You don't chose the right constructor for creating an adhoc mode!\r\n");
+#endif
         return false;
     }
 }
 
 bool Wifly::CmdMode() {
     if (!Send("$$$", "CMD")) {
+#ifdef DEBUG
         printf("Wifly::CmdMode: cannot enter in cmd mode\r\n");
+#endif
         return false;
     }
     return true;
@@ -321,12 +365,13 @@
 
 
 
-void Wifly::read(char * str) {
-    if (new_msg) {
+bool Wifly::read(char * str) {
+    if (new_msg && str != NULL) {
         memcpy(str, buf_rx, strlen(buf_rx + 1) + 1);
         new_msg = false;
-    } else
-        str = NULL;
+        return true;
+    }
+    return false;
 }
 
 
@@ -346,19 +391,25 @@
     char cmd[20];
     exit();
     if (!CmdMode()) {
+#ifdef DEBUG
         printf("Wifly::changeBaudrate: cannot enter in cmd mode\r\n");
+#endif
         return false;
     }
 
     sprintf(cmd, "set u b %d\r\n", baudrate);
     if (!Send(cmd, "AOK")) {
+#ifdef DEBUG
         printf("Wifly::changeBaudrate: cannot set new baudrate\r\n");
+#endif
         exit();
         return false;
     }
 
     if (!Send("save\r\n", "Stor")) {
+#ifdef DEBUG
         printf("Wifly::changeBaudrate: cannot save\r\n");
+#endif
         exit();
         return false;
     }