wifly/socket interface for wifly modules
Dependents: PROJET_MATTHIEU_2019 PROJET_MATTHIEU_2019
Diff: Wifly/Wifly.cpp
- Revision:
- 4:74bfdd00362a
- Parent:
- 3:10b3866f0ea2
- Child:
- 5:8e253731f76f
diff -r 10b3866f0ea2 -r 74bfdd00362a Wifly/Wifly.cpp --- a/Wifly/Wifly.cpp Wed Aug 22 16:22:32 2012 +0000 +++ b/Wifly/Wifly.cpp Thu Aug 23 10:32:45 2012 +0000 @@ -34,6 +34,8 @@ #define INFO(x, ...) printf("[Wifly : INFO]"x"\r\n", ##__VA_ARGS__); +#define MAX_TRY_JOIN 3 + Wifly * Wifly::inst; Wifly::Wifly( PinName tx, PinName rx, PinName _reset, PinName tcp_status, const char * ssid, const char * phrase, bool wpa): @@ -51,137 +53,124 @@ { char cmd[100]; - if (!sendCommand("set comm time 20\r", "AOK")) - return false; + for (int i= 0; i < MAX_TRY_JOIN; i++) { + if (!sendCommand("set comm time 20\r", "AOK")) + continue; + + if (!sendCommand("set comm size 128\r", "AOK")) + continue; - if (!sendCommand("set comm size 128\r", "AOK")) - return false; + if (!sendCommand("set sys iofunc 0x40\r", "AOK")) + continue; - if (!sendCommand("set sys iofunc 0x40\r", "AOK")) - return false; + // no string sent to the tcp client + if (!sendCommand("set comm remote 0\r", "AOK")) + continue; - // no string sent to the tcp client - if (!sendCommand("set comm remote 0\r", "AOK")) - return false; + // tcp protocol + if (!sendCommand("set ip proto 2\r", "AOK")) + continue; - // tcp protocol - if (!sendCommand("set ip proto 2\r", "AOK")) - return false; + // tcp retry + if (!sendCommand("set ip flags 0x7\r", "AOK")) + continue; - // tcp retry - if (!sendCommand("set ip flags 0x7\r", "AOK")) - return false; + //no echo + if (!sendCommand("set u m 1\r", "AOK")) + continue; - //no echo - if (!sendCommand("set u m 1\r", "AOK")) - return false; + // no auto join + if (!sendCommand("set w j 0\r", "AOK")) + continue; - //dhcp - sprintf(cmd, "set i d %d\r", (dhcp) ? 1 : 0); - if (!sendCommand(cmd, "AOK")) - return false; + //dhcp + sprintf(cmd, "set i d %d\r", (dhcp) ? 1 : 0); + if (!sendCommand(cmd, "AOK")) + continue; - if (dhcp) { - if (!sendCommand("get w\r", NULL, cmd)) - return false; + // ssid + sprintf(cmd, "set w s %s\r", ssid); + if (!sendCommand(cmd, "AOK")) + continue; + + //auth + sprintf(cmd, "set w a %d\r", (wpa) ? 3 : 1); + if (!sendCommand(cmd, "AOK")) + continue; - // if the wlan parameters are already known, we just connect without resending all commands - if ((string(cmd).find(ssid) != string::npos) && (string(cmd).find(phrase) != string::npos) ) { - DBG("ssid found && phrase found\r\n"); - Timer tmr; - tmr.start(); - while (tmr.read() < 2) { - send("show c\r", 7, NULL, cmd); - DBG("join: state of conn: %s\r\n", cmd); - if ((cmd[2] - '0') & 0x01) { - INFO("\r\nssid: %s\r\nphrase: %s\r\nsecurity: %s\r\n\r\n", this->ssid, this->phrase, (wpa) ? "WPA" : "WEP"); - // save - if (!sendCommand("save\r", "Stor")) - return false; - reboot(); - return true; - } - wait(0.2); - } + // if no dhcp, set ip, netmask and gateway + if (!dhcp) { + DBG("not dhcp\r"); + + sprintf(cmd, "set i a %s\r\n", ip); + if (!sendCommand(cmd, "AOK")) + continue; + + sprintf(cmd, "set i n %s\r", netmask); + if (!sendCommand(cmd, "AOK")) + continue; + + sprintf(cmd, "set i g %s\r", gateway); + if (!sendCommand(cmd, "AOK")) + continue; } - DBG("getw: %s\r\n", cmd); - } - - // ssid - sprintf(cmd, "set w s %s\r", ssid); - if (!sendCommand(cmd, "AOK")) - return false; + //key step + if (wpa) + sprintf(cmd, "set w p %s\r", phrase); + else + sprintf(cmd, "set w k %s\r", phrase); - //auth - sprintf(cmd, "set w a %d\r", (wpa) ? 3 : 1); - if (!sendCommand(cmd, "AOK")) - return false; + if (!sendCommand(cmd, "AOK")) + continue; - // if no dhcp, set ip, netmask and gateway - if (!dhcp) { - DBG("not dhcp\r"); - - sprintf(cmd, "set i a %s\r\n", ip); - if (!sendCommand(cmd, "AOK")) - return false; - - sprintf(cmd, "set i n %s\r", netmask); - if (!sendCommand(cmd, "AOK")) + // save + if (!sendCommand("save\r", "Stor")) return false; - sprintf(cmd, "set i g %s\r", gateway); - if (!sendCommand(cmd, "AOK")) - return false; - } - - //key step - if (wpa) - sprintf(cmd, "set w p %s\r", phrase); - else - sprintf(cmd, "set w k %s\r", phrase); - - if (!sendCommand(cmd, "AOK")) - return false; + //join the network + sprintf(cmd, "join\r"); + if (!sendCommand(cmd, "Associated", NULL, 3000)) + continue; - // auto join - if (!sendCommand("set w j 1\r", "AOK")) - return false; - - // save - if (!sendCommand("save\r", "Stor")) - return false; + if (dhcp) { + if (!sendCommand("", "DHCP=ON", NULL, 3000)) + continue; + } - //join the network - sprintf(cmd, "join\r"); - if (!sendCommand(cmd, "Associated")) - return false; + exit(); - reboot(); - - INFO("\r\nssid: %s\r\nphrase: %s\r\nsecurity: %s\r\n\r\n", this->ssid, this->phrase, (wpa) ? "WPA" : "WEP"); - return true; + INFO("\r\nssid: %s\r\nphrase: %s\r\nsecurity: %s\r\n\r\n", this->ssid, this->phrase, (wpa) ? "WPA" : "WEP"); + return true; + } + return false; } bool Wifly::dnsLookup(const char * host, char * ip) { string h = host; - char cmd[30], rcv[30]; + char cmd[30], rcv[100]; int l = 0; char * point; int nb_digits = 0; // no dns needed - if (atoi(host) > 0 && count(h.begin(), h.end(), '.') == 4) { + int pos = h.find("."); + if (pos != string::npos) { + string sub = h.substr(0, h.find(".")); + nb_digits = atoi(sub.c_str()); + } + //printf("substrL %s\r\n", sub.c_str()); + if (count(h.begin(), h.end(), '.') == 3 && nb_digits > 0) { strcpy(ip, host); } // dns needed else { + nb_digits = 0; sprintf(cmd, "lookup %s\r", host); if (!sendCommand(cmd, NULL, rcv)) return false; - DBG("lookup: %s\r\n", rcv); // look for the ip address char * begin = strstr(rcv, "=") + 1; @@ -202,34 +191,18 @@ return true; } -bool Wifly::reboot() -{ - if (!sendCommand("reboot\r", "boot")) - return false; - wait(0.2); - // wait that the module fetches an IP - if (dhcp) { - while(send("", 0, "DHCP=ON") == -1); - } else { - while(send("", 0, "Static") == -1); - } - flush(); - cmd_mode = false; - return true; -} - void Wifly::flush() { buf_wifly.flush(); } -bool Wifly::sendCommand(const char * cmd, const char * ack, char * res) +bool Wifly::sendCommand(const char * cmd, const char * ack, char * res, int timeout) { if (!cmd_mode) { cmdMode(); } - if (send(cmd, strlen(cmd), ack, res) == -1) { + if (send(cmd, strlen(cmd), ack, res, timeout) == -1) { ERR("sendCommand: cannot %s\r\n", cmd); exit(); return false; @@ -304,9 +277,12 @@ bool Wifly::exit() { flush(); + if (!cmd_mode) + return true; if (send("exit\r", 5, "EXIT") == -1) return false; cmd_mode = false; + flush(); return true; } @@ -345,7 +321,7 @@ } -int Wifly::send(const char * str, int len, const char * ACK, char * res) +int Wifly::send(const char * str, int len, const char * ACK, char * res, int timeout) { char read; size_t found = string::npos; @@ -374,7 +350,7 @@ result = (putc(str[i]) == str[i]) ? result + 1 : result; while (1) { - if (tmr.read() > 2) { + if (tmr.read_ms() > timeout) { //We flush the buffer while (wifi.readable()) wifi.getc();