MODIFIED from mbed official WiflyInterface (interface for Roving Networks Wifly modules). Numerous performance and reliability improvements (see the detailed documentation). Also, tracking changes in mbed official version to retain functional parity.

Dependents:   Smart-WiFly-WebServer PUB_WiflyInterface_Demo

Fork of WiflyInterface by mbed official

Resources

Derivative from mbed Official

  • Documentation update, improved consistency, documented parameters that were inadvertently omitted.
  • Avoid c++ string handling, which causes dynamic allocation and free, side effect, fewer CPU cycles spent for same purpose.
  • Fixed socket APIs to support non-blocking mode.
  • Increase communication baud-rate to Wifly module
  • sendCommand - added retries for improved robustness.
  • setConnectionState - method to force the connection state (used by TCPSocketServer)
  • gethostbyname - added a length parameter to the size of the buffer being written
  • flushIn - a private method to flush the input buffer
  • Changed the timeout from 500 to 2500 msec for commands - measured some at 700 to 850 msec.
  • Performance improvements - reduced some unnecessary delays.
  • Added additional security options for the wi-fi connection (that are supported by the WiFly module).
  • Added setSecurity API which permits revising the security when connecting to, or selecting from, one of several access points.
  • Improved DEBUG interface (slightly more consistent printout).
  • gathers information from the Wifly module on reboot (SW version info), which permits customizing behavior based on Wifly capabilities (like the improved security).
  • Avoid potential for recursive crash (if exit fails, it calls sendcommand, which calls exit...)
  • Update to support permissible SSID and PassCode lengths.

Robustness testing

I've had some mixed behavior with the Wifly module, some of which seems to be traceable to the module itself, and some in my derivative code. The result, after running for minutes, hours, sometimes days, it hangs and I have to reset the module.

To test, I created a fairly simple test program -

  • check for Watchdog induced reset and count it.
  • initialize the Watchdog for 60 sec timeout.
  • Init the Wifly interface and connect to my network.
  • Wait 10 seconds and force mbed_reset().

If the Watchdog induces the restart, then it is pretty clear that either:

  • The communications hung with the Wifly module causing the failure.
  • The Wifly module decided to go unresponsive.

If it gets to the end, it typically takes about 4 to 6 seconds for the boot and connect, then the 10 second delay.

But I can't really pin down the root cause easily. My strongest theory is that the Wifly module has rebooted, and since I don't store the high baud rate I configure it for, it resets back to 9600.

Also, one of the objectives for my revised send( ) is to avoid the c++ string, as that can fragment memory, and it wasn't very well bounded in behavior.

Latest tests:

Warm BootsWatchdog EventsNotes
100's30An early version of my derivative WiflyInterface, including my derivative of "send( )" API. Let's call this version 0.1.
26684My derivative WiflyInterface, but with the mbed official "send( )" API. Much improved. This was over the course of about 12 hours.
24003Most recent derivative - incremental change to "send( )", but this relative number does not rule out the Wifly module itself.

I think with these numbers, +/- 1 means that the changes have had no measurable effect. Which is good, since this incremental change eliminates the c++ string handling.

Test Software

This is pieces of a test program, clipped and copied to here. What I have compiled and run for hours and hours is almost exactly what you see. This uses this simple Watchdog library.

#include "mbed.h"
#include "WiflyInterface.h"
#include "Watchdog.h"

Serial pc(USBTX, USBRX);

Watchdog wd;
extern "C" void mbed_reset();

// Pinout for SmartBoard
WiflyInterface wifly(p9, p10, p30, p29, "ssid", "pass", WPA);

int main() {
    pc.baud(460800);                         // I like a snappy terminal
    
    wd.Configure(60.0);                     // Set time limit for the test to 1 minute
    LPC_RTC->GPREG0++;                      // Count boots here
    if (wd.WatchdogCausedReset()) {
        LPC_RTC->GPREG1++;                  // Count Watchdog events here
        pc.printf("\r\n\r\nWatchdog event.\r\n");
    }
    pc.printf("\r\nWifly Test: %d boots, %d watchdogs. %s %s\r\n", LPC_RTC->GPREG0, LPC_RTC->GPREG1, __DATE__, __TIME__);
    
    wifly.init(); // use DHCP
    pc.printf("Connect...  ");
    while (!wifly.connect());               // join the network
    pc.printf("Address is %s.  ", wifly.getIPAddress());
    pc.printf("Disconnect...  ");
    wifly.disconnect();
    pc.printf("OK. Reset in 10 sec...\r\n");
    wait(10);
    if (pc.readable()) {
        if (pc.getc() == 'r') {             // secret 'r'eset of the counters
            LPC_RTC->GPREG0 = 0;
            LPC_RTC->GPREG1 = 0;
            pc.printf("counters reset\r\n");
        }
    }
    mbed_reset();                           // reset here indicates successful communication
}
Committer:
WiredHome
Date:
Sun Jul 21 21:02:59 2013 +0000
Revision:
18:18ab3993d00d
Parent:
17:213844a1864f
Child:
19:b0c2488222a3
Efficiency - add flushIn() and eliminate several inline equivalent changes.; Efficiency - largest effect was on the send() method.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samux 1:fb4494783863 1 /* Copyright (C) 2012 mbed.org, MIT License
samux 1:fb4494783863 2 *
samux 1:fb4494783863 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
samux 1:fb4494783863 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
samux 1:fb4494783863 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
samux 1:fb4494783863 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
samux 1:fb4494783863 7 * furnished to do so, subject to the following conditions:
samux 1:fb4494783863 8 *
samux 1:fb4494783863 9 * The above copyright notice and this permission notice shall be included in all copies or
samux 1:fb4494783863 10 * substantial portions of the Software.
samux 1:fb4494783863 11 *
samux 1:fb4494783863 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
samux 1:fb4494783863 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
samux 1:fb4494783863 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
samux 1:fb4494783863 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
samux 1:fb4494783863 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
samux 1:fb4494783863 17 */
samux 1:fb4494783863 18
samux 1:fb4494783863 19 #include "mbed.h"
samux 1:fb4494783863 20 #include "Wifly.h"
samux 1:fb4494783863 21 #include <string>
samux 1:fb4494783863 22 #include <algorithm>
samux 1:fb4494783863 23
samux 1:fb4494783863 24 //Debug is disabled by default
samux 2:8e54830d0df7 25 #if (0 && !defined(TARGET_LPC11U24))
samux 1:fb4494783863 26 #define DBG(x, ...) std::printf("[Wifly : DBG]"x"\r\n", ##__VA_ARGS__);
samux 1:fb4494783863 27 #define WARN(x, ...) std::printf("[Wifly : WARN]"x"\r\n", ##__VA_ARGS__);
samux 1:fb4494783863 28 #define ERR(x, ...) std::printf("[Wifly : ERR]"x"\r\n", ##__VA_ARGS__);
samux 1:fb4494783863 29 #else
samux 1:fb4494783863 30 #define DBG(x, ...)
samux 1:fb4494783863 31 #define WARN(x, ...)
samux 1:fb4494783863 32 #define ERR(x, ...)
samux 1:fb4494783863 33 #endif
samux 1:fb4494783863 34
samux 2:8e54830d0df7 35 #if !defined(TARGET_LPC11U24)
samux 1:fb4494783863 36 #define INFO(x, ...) printf("[Wifly : INFO]"x"\r\n", ##__VA_ARGS__);
samux 1:fb4494783863 37 #else
samux 1:fb4494783863 38 #define INFO(x, ...)
samux 1:fb4494783863 39 #endif
samux 1:fb4494783863 40
samux 1:fb4494783863 41 #define MAX_TRY_JOIN 3
samux 1:fb4494783863 42
samux 1:fb4494783863 43 Wifly * Wifly::inst;
samux 1:fb4494783863 44
samux 1:fb4494783863 45 Wifly::Wifly( PinName tx, PinName rx, PinName _reset, PinName tcp_status, const char * ssid, const char * phrase, Security sec):
samux 1:fb4494783863 46 wifi(tx, rx), reset_pin(_reset), tcp_status(tcp_status), buf_wifly(256)
samux 1:fb4494783863 47 {
samux 1:fb4494783863 48 memset(&state, 0, sizeof(state));
samux 1:fb4494783863 49 state.sec = sec;
samux 1:fb4494783863 50
samux 1:fb4494783863 51 // change all ' ' in '$' in the ssid and the passphrase
samux 1:fb4494783863 52 strcpy(this->ssid, ssid);
samux 1:fb4494783863 53 for (int i = 0; i < strlen(ssid); i++) {
samux 1:fb4494783863 54 if (this->ssid[i] == ' ')
samux 1:fb4494783863 55 this->ssid[i] = '$';
samux 1:fb4494783863 56 }
samux 1:fb4494783863 57 strcpy(this->phrase, phrase);
samux 1:fb4494783863 58 for (int i = 0; i < strlen(phrase); i++) {
samux 1:fb4494783863 59 if (this->phrase[i] == ' ')
samux 1:fb4494783863 60 this->phrase[i] = '$';
samux 1:fb4494783863 61 }
samux 1:fb4494783863 62
samux 1:fb4494783863 63 inst = this;
samux 1:fb4494783863 64 attach_rx(false);
samux 1:fb4494783863 65 state.cmd_mode = false;
samux 1:fb4494783863 66 }
samux 1:fb4494783863 67
samux 1:fb4494783863 68 bool Wifly::join()
samux 1:fb4494783863 69 {
samux 1:fb4494783863 70 char cmd[20];
samux 1:fb4494783863 71
samux 1:fb4494783863 72 for (int i= 0; i < MAX_TRY_JOIN; i++) {
samux 2:8e54830d0df7 73
samux 2:8e54830d0df7 74 // no auto join
samux 2:8e54830d0df7 75 if (!sendCommand("set w j 0\r", "AOK"))
samux 2:8e54830d0df7 76 continue;
samux 2:8e54830d0df7 77
samux 2:8e54830d0df7 78 //no echo
samux 2:8e54830d0df7 79 if (!sendCommand("set u m 1\r", "AOK"))
samux 2:8e54830d0df7 80 continue;
samux 2:8e54830d0df7 81
samux 1:fb4494783863 82 // set time
samux 4:0bcec6272784 83 if (!sendCommand("set c t 30\r", "AOK"))
samux 1:fb4494783863 84 continue;
samux 1:fb4494783863 85
samux 1:fb4494783863 86 // set size
samux 4:0bcec6272784 87 if (!sendCommand("set c s 1024\r", "AOK"))
samux 1:fb4494783863 88 continue;
samux 1:fb4494783863 89
samux 1:fb4494783863 90 // red led on when tcp connection active
samux 1:fb4494783863 91 if (!sendCommand("set s i 0x40\r", "AOK"))
samux 1:fb4494783863 92 continue;
samux 1:fb4494783863 93
samux 1:fb4494783863 94 // no string sent to the tcp client
samux 1:fb4494783863 95 if (!sendCommand("set c r 0\r", "AOK"))
samux 1:fb4494783863 96 continue;
samux 1:fb4494783863 97
samux 1:fb4494783863 98 // tcp protocol
samux 1:fb4494783863 99 if (!sendCommand("set i p 2\r", "AOK"))
samux 1:fb4494783863 100 continue;
samux 1:fb4494783863 101
samux 1:fb4494783863 102 // tcp retry
samux 1:fb4494783863 103 if (!sendCommand("set i f 0x7\r", "AOK"))
samux 1:fb4494783863 104 continue;
samux 2:8e54830d0df7 105
samux 2:8e54830d0df7 106 // set dns server
samux 2:8e54830d0df7 107 if (!sendCommand("set d n rn.microchip.com\r", "AOK"))
samux 1:fb4494783863 108 continue;
samux 1:fb4494783863 109
samux 1:fb4494783863 110 //dhcp
samux 1:fb4494783863 111 sprintf(cmd, "set i d %d\r", (state.dhcp) ? 1 : 0);
samux 1:fb4494783863 112 if (!sendCommand(cmd, "AOK"))
samux 1:fb4494783863 113 continue;
samux 1:fb4494783863 114
samux 1:fb4494783863 115 // ssid
samux 1:fb4494783863 116 sprintf(cmd, "set w s %s\r", ssid);
samux 1:fb4494783863 117 if (!sendCommand(cmd, "AOK"))
samux 1:fb4494783863 118 continue;
samux 1:fb4494783863 119
samux 1:fb4494783863 120 //auth
samux 1:fb4494783863 121 sprintf(cmd, "set w a %d\r", state.sec);
samux 1:fb4494783863 122 if (!sendCommand(cmd, "AOK"))
samux 1:fb4494783863 123 continue;
samux 1:fb4494783863 124
samux 1:fb4494783863 125 // if no dhcp, set ip, netmask and gateway
samux 1:fb4494783863 126 if (!state.dhcp) {
samux 1:fb4494783863 127 DBG("not dhcp\r");
samux 1:fb4494783863 128
samux 1:fb4494783863 129 sprintf(cmd, "set i a %s\r\n", ip);
samux 1:fb4494783863 130 if (!sendCommand(cmd, "AOK"))
samux 1:fb4494783863 131 continue;
samux 1:fb4494783863 132
samux 1:fb4494783863 133 sprintf(cmd, "set i n %s\r", netmask);
samux 1:fb4494783863 134 if (!sendCommand(cmd, "AOK"))
samux 1:fb4494783863 135 continue;
samux 1:fb4494783863 136
samux 1:fb4494783863 137 sprintf(cmd, "set i g %s\r", gateway);
samux 1:fb4494783863 138 if (!sendCommand(cmd, "AOK"))
samux 1:fb4494783863 139 continue;
samux 1:fb4494783863 140 }
samux 1:fb4494783863 141
samux 1:fb4494783863 142 //key step
samux 1:fb4494783863 143 if (state.sec != NONE) {
samux 1:fb4494783863 144 if (state.sec == WPA)
samux 1:fb4494783863 145 sprintf(cmd, "set w p %s\r", phrase);
samux 1:fb4494783863 146 else if (state.sec == WEP_128)
samux 1:fb4494783863 147 sprintf(cmd, "set w k %s\r", phrase);
samux 1:fb4494783863 148
samux 1:fb4494783863 149 if (!sendCommand(cmd, "AOK"))
samux 1:fb4494783863 150 continue;
samux 1:fb4494783863 151 }
samux 1:fb4494783863 152
samux 2:8e54830d0df7 153 //join the network (10s timeout)
samux 1:fb4494783863 154 if (state.dhcp) {
samux 2:8e54830d0df7 155 if (!sendCommand("join\r", "DHCP=ON", NULL, 10000))
samux 2:8e54830d0df7 156 continue;
samux 2:8e54830d0df7 157 } else {
samux 2:8e54830d0df7 158 if (!sendCommand("join\r", "Associated", NULL, 10000))
samux 1:fb4494783863 159 continue;
samux 1:fb4494783863 160 }
samux 1:fb4494783863 161
samux 2:8e54830d0df7 162 if (!sendCommand("save\r", "Stor"))
samux 2:8e54830d0df7 163 continue;
samux 2:8e54830d0df7 164
samux 1:fb4494783863 165 exit();
samux 1:fb4494783863 166
samux 1:fb4494783863 167 state.associated = true;
samux 1:fb4494783863 168 INFO("\r\nssid: %s\r\nphrase: %s\r\nsecurity: %s\r\n\r\n", this->ssid, this->phrase, getStringSecurity());
samux 1:fb4494783863 169 return true;
samux 1:fb4494783863 170 }
samux 1:fb4494783863 171 return false;
samux 1:fb4494783863 172 }
samux 1:fb4494783863 173
samux 1:fb4494783863 174
samux 1:fb4494783863 175 bool Wifly::setProtocol(Protocol p)
samux 1:fb4494783863 176 {
samux 1:fb4494783863 177 // use udp auto pairing
samux 1:fb4494783863 178 char cmd[20];
samux 1:fb4494783863 179 sprintf(cmd, "set i p %d\r", p);
samux 1:fb4494783863 180 if (!sendCommand(cmd, "AOK"))
samux 1:fb4494783863 181 return false;
samux 1:fb4494783863 182
samux 1:fb4494783863 183 switch(p) {
samux 1:fb4494783863 184 case TCP:
samux 1:fb4494783863 185 // set ip flags: tcp retry enabled
samux 1:fb4494783863 186 if (!sendCommand("set i f 0x07\r", "AOK"))
samux 1:fb4494783863 187 return false;
samux 1:fb4494783863 188 break;
samux 1:fb4494783863 189 case UDP:
samux 1:fb4494783863 190 // set ip flags: udp auto pairing enabled
samux 1:fb4494783863 191 if (!sendCommand("set i h 0.0.0.0\r", "AOK"))
samux 1:fb4494783863 192 return false;
samux 4:0bcec6272784 193 if (!sendCommand("set i f 0x40\r", "AOK"))
samux 1:fb4494783863 194 return false;
samux 1:fb4494783863 195 break;
samux 1:fb4494783863 196 }
samux 1:fb4494783863 197 state.proto = p;
samux 1:fb4494783863 198 return true;
samux 1:fb4494783863 199 }
samux 1:fb4494783863 200
samux 1:fb4494783863 201 char * Wifly::getStringSecurity()
samux 1:fb4494783863 202 {
samux 1:fb4494783863 203 switch(state.sec) {
samux 1:fb4494783863 204 case NONE:
samux 1:fb4494783863 205 return "NONE";
samux 1:fb4494783863 206 case WEP_128:
samux 1:fb4494783863 207 return "WEP_128";
samux 1:fb4494783863 208 case WPA:
samux 1:fb4494783863 209 return "WPA";
samux 1:fb4494783863 210 }
samux 1:fb4494783863 211 return "UNKNOWN";
samux 1:fb4494783863 212 }
samux 1:fb4494783863 213
samux 1:fb4494783863 214 bool Wifly::connect(const char * host, int port)
samux 1:fb4494783863 215 {
samux 1:fb4494783863 216 char rcv[20];
samux 1:fb4494783863 217 char cmd[20];
samux 1:fb4494783863 218
samux 2:8e54830d0df7 219 // try to open
samux 2:8e54830d0df7 220 sprintf(cmd, "open %s %d\r", host, port);
samux 2:8e54830d0df7 221 if (sendCommand(cmd, "OPEN", NULL, 10000)) {
samux 2:8e54830d0df7 222 state.tcp = true;
samux 2:8e54830d0df7 223 state.cmd_mode = false;
samux 2:8e54830d0df7 224 return true;
samux 1:fb4494783863 225 }
samux 1:fb4494783863 226
samux 2:8e54830d0df7 227 // if failed, retry and parse the response
samux 2:8e54830d0df7 228 if (sendCommand(cmd, NULL, rcv, 5000)) {
samux 1:fb4494783863 229 if (strstr(rcv, "OPEN") == NULL) {
samux 1:fb4494783863 230 if (strstr(rcv, "Connected") != NULL) {
samux 2:8e54830d0df7 231 wait(0.25);
samux 1:fb4494783863 232 if (!sendCommand("close\r", "CLOS"))
samux 1:fb4494783863 233 return false;
samux 2:8e54830d0df7 234 wait(0.25);
samux 2:8e54830d0df7 235 if (!sendCommand(cmd, "OPEN", NULL, 10000))
samux 1:fb4494783863 236 return false;
samux 1:fb4494783863 237 } else {
samux 1:fb4494783863 238 return false;
samux 1:fb4494783863 239 }
samux 1:fb4494783863 240 }
samux 1:fb4494783863 241 } else {
samux 1:fb4494783863 242 return false;
samux 1:fb4494783863 243 }
samux 2:8e54830d0df7 244
samux 1:fb4494783863 245 state.tcp = true;
samux 1:fb4494783863 246 state.cmd_mode = false;
samux 1:fb4494783863 247
samux 1:fb4494783863 248 return true;
samux 1:fb4494783863 249 }
samux 1:fb4494783863 250
samux 1:fb4494783863 251
samux 1:fb4494783863 252 bool Wifly::gethostbyname(const char * host, char * ip)
samux 1:fb4494783863 253 {
samux 1:fb4494783863 254 string h = host;
samux 1:fb4494783863 255 char cmd[30], rcv[100];
samux 1:fb4494783863 256 int l = 0;
samux 1:fb4494783863 257 char * point;
samux 1:fb4494783863 258 int nb_digits = 0;
samux 1:fb4494783863 259
samux 1:fb4494783863 260 // no dns needed
samux 1:fb4494783863 261 int pos = h.find(".");
samux 1:fb4494783863 262 if (pos != string::npos) {
samux 1:fb4494783863 263 string sub = h.substr(0, h.find("."));
samux 1:fb4494783863 264 nb_digits = atoi(sub.c_str());
samux 1:fb4494783863 265 }
samux 1:fb4494783863 266 //printf("substrL %s\r\n", sub.c_str());
samux 1:fb4494783863 267 if (count(h.begin(), h.end(), '.') == 3 && nb_digits > 0) {
samux 1:fb4494783863 268 strcpy(ip, host);
samux 1:fb4494783863 269 }
samux 1:fb4494783863 270 // dns needed
samux 1:fb4494783863 271 else {
samux 1:fb4494783863 272 nb_digits = 0;
samux 1:fb4494783863 273 sprintf(cmd, "lookup %s\r", host);
samux 1:fb4494783863 274 if (!sendCommand(cmd, NULL, rcv))
samux 1:fb4494783863 275 return false;
samux 1:fb4494783863 276
samux 1:fb4494783863 277 // look for the ip address
samux 1:fb4494783863 278 char * begin = strstr(rcv, "=") + 1;
samux 1:fb4494783863 279 for (int i = 0; i < 3; i++) {
samux 1:fb4494783863 280 point = strstr(begin + l, ".");
samux 1:fb4494783863 281 DBG("str: %s", begin + l);
samux 1:fb4494783863 282 l += point - (begin + l) + 1;
samux 1:fb4494783863 283 }
samux 1:fb4494783863 284 DBG("str: %s", begin + l);
samux 1:fb4494783863 285 while(*(begin + l + nb_digits) >= '0' && *(begin + l + nb_digits) <= '9') {
samux 1:fb4494783863 286 DBG("digit: %c", *(begin + l + nb_digits));
samux 1:fb4494783863 287 nb_digits++;
samux 1:fb4494783863 288 }
samux 1:fb4494783863 289 memcpy(ip, begin, l + nb_digits);
samux 1:fb4494783863 290 ip[l+nb_digits] = 0;
samux 1:fb4494783863 291 DBG("ip from dns: %s", ip);
samux 1:fb4494783863 292 }
samux 1:fb4494783863 293 return true;
samux 1:fb4494783863 294 }
samux 1:fb4494783863 295
samux 1:fb4494783863 296
samux 1:fb4494783863 297 void Wifly::flush()
samux 1:fb4494783863 298 {
samux 1:fb4494783863 299 buf_wifly.flush();
samux 1:fb4494783863 300 }
samux 1:fb4494783863 301
samux 1:fb4494783863 302 bool Wifly::sendCommand(const char * cmd, const char * ack, char * res, int timeout)
samux 1:fb4494783863 303 {
samux 1:fb4494783863 304 if (!state.cmd_mode) {
samux 1:fb4494783863 305 cmdMode();
samux 1:fb4494783863 306 }
WiredHome 17:213844a1864f 307 if (send(cmd, strlen(cmd), ack, res, timeout) >= 0) {
WiredHome 17:213844a1864f 308 return true;
samux 1:fb4494783863 309 }
WiredHome 17:213844a1864f 310 ERR("sendCommand: cannot %s", cmd);
WiredHome 17:213844a1864f 311 return false;
samux 1:fb4494783863 312 }
samux 1:fb4494783863 313
samux 1:fb4494783863 314 bool Wifly::cmdMode()
samux 1:fb4494783863 315 {
samux 1:fb4494783863 316 // if already in cmd mode, return
samux 1:fb4494783863 317 if (state.cmd_mode)
samux 1:fb4494783863 318 return true;
samux 2:8e54830d0df7 319
samux 1:fb4494783863 320 if (send("$$$", 3, "CMD") == -1) {
samux 1:fb4494783863 321 ERR("cannot enter in cmd mode\r\n");
samux 2:8e54830d0df7 322 exit();
samux 1:fb4494783863 323 return false;
samux 1:fb4494783863 324 }
samux 1:fb4494783863 325 state.cmd_mode = true;
samux 1:fb4494783863 326 return true;
samux 1:fb4494783863 327 }
samux 1:fb4494783863 328
samux 1:fb4494783863 329 bool Wifly::disconnect()
samux 1:fb4494783863 330 {
samux 1:fb4494783863 331 // if already disconnected, return
samux 1:fb4494783863 332 if (!state.associated)
samux 1:fb4494783863 333 return true;
samux 2:8e54830d0df7 334
samux 1:fb4494783863 335 if (!sendCommand("leave\r", "DeAuth"))
samux 1:fb4494783863 336 return false;
samux 1:fb4494783863 337 exit();
samux 2:8e54830d0df7 338
samux 1:fb4494783863 339 state.associated = false;
samux 1:fb4494783863 340 return true;
samux 1:fb4494783863 341
samux 1:fb4494783863 342 }
samux 1:fb4494783863 343
samux 1:fb4494783863 344 bool Wifly::is_connected()
samux 1:fb4494783863 345 {
samux 1:fb4494783863 346 return (tcp_status.read() == 1) ? true : false;
samux 1:fb4494783863 347 }
samux 1:fb4494783863 348
samux 1:fb4494783863 349
samux 1:fb4494783863 350 void Wifly::reset()
samux 1:fb4494783863 351 {
samux 1:fb4494783863 352 reset_pin = 0;
samux 1:fb4494783863 353 wait(0.2);
samux 1:fb4494783863 354 reset_pin = 1;
samux 1:fb4494783863 355 wait(0.2);
samux 1:fb4494783863 356 }
samux 1:fb4494783863 357
samux 3:9aa05e19c62e 358 bool Wifly::reboot()
samux 3:9aa05e19c62e 359 {
samux 3:9aa05e19c62e 360 // if already in cmd mode, return
samux 3:9aa05e19c62e 361 if (!sendCommand("reboot\r"))
samux 3:9aa05e19c62e 362 return false;
samux 3:9aa05e19c62e 363
samux 3:9aa05e19c62e 364 wait(0.3);
samux 3:9aa05e19c62e 365
samux 3:9aa05e19c62e 366 state.cmd_mode = false;
samux 3:9aa05e19c62e 367 return true;
samux 3:9aa05e19c62e 368 }
samux 3:9aa05e19c62e 369
samux 1:fb4494783863 370 bool Wifly::close()
samux 1:fb4494783863 371 {
samux 1:fb4494783863 372 // if not connected, return
samux 1:fb4494783863 373 if (!state.tcp)
samux 1:fb4494783863 374 return true;
samux 2:8e54830d0df7 375
samux 1:fb4494783863 376 wait(0.25);
samux 1:fb4494783863 377 if (!sendCommand("close\r", "CLOS"))
samux 1:fb4494783863 378 return false;
samux 1:fb4494783863 379 exit();
samux 2:8e54830d0df7 380
samux 1:fb4494783863 381 state.tcp = false;
samux 1:fb4494783863 382 return true;
samux 1:fb4494783863 383 }
samux 1:fb4494783863 384
samux 1:fb4494783863 385
samux 1:fb4494783863 386 int Wifly::putc(char c)
samux 1:fb4494783863 387 {
samux 1:fb4494783863 388 while (!wifi.writeable());
samux 1:fb4494783863 389 return wifi.putc(c);
samux 1:fb4494783863 390 }
samux 1:fb4494783863 391
samux 1:fb4494783863 392
samux 1:fb4494783863 393 bool Wifly::exit()
samux 1:fb4494783863 394 {
samux 1:fb4494783863 395 flush();
samux 1:fb4494783863 396 if (!state.cmd_mode)
samux 1:fb4494783863 397 return true;
samux 1:fb4494783863 398 if (!sendCommand("exit\r", "EXIT"))
samux 1:fb4494783863 399 return false;
samux 1:fb4494783863 400 state.cmd_mode = false;
samux 1:fb4494783863 401 flush();
samux 1:fb4494783863 402 return true;
samux 1:fb4494783863 403 }
samux 1:fb4494783863 404
samux 1:fb4494783863 405
samux 1:fb4494783863 406 int Wifly::readable()
samux 1:fb4494783863 407 {
samux 1:fb4494783863 408 return buf_wifly.available();
samux 1:fb4494783863 409 }
samux 1:fb4494783863 410
samux 1:fb4494783863 411 int Wifly::writeable()
samux 1:fb4494783863 412 {
samux 1:fb4494783863 413 return wifi.writeable();
samux 1:fb4494783863 414 }
samux 1:fb4494783863 415
samux 1:fb4494783863 416 char Wifly::getc()
samux 1:fb4494783863 417 {
samux 1:fb4494783863 418 char c;
samux 1:fb4494783863 419 while (!buf_wifly.available());
samux 1:fb4494783863 420 buf_wifly.dequeue(&c);
samux 1:fb4494783863 421 return c;
samux 1:fb4494783863 422 }
samux 1:fb4494783863 423
samux 1:fb4494783863 424 void Wifly::handler_rx(void)
samux 1:fb4494783863 425 {
samux 1:fb4494783863 426 //read characters
samux 1:fb4494783863 427 while (wifi.readable())
samux 1:fb4494783863 428 buf_wifly.queue(wifi.getc());
samux 1:fb4494783863 429 }
samux 1:fb4494783863 430
samux 1:fb4494783863 431 void Wifly::attach_rx(bool callback)
samux 1:fb4494783863 432 {
samux 1:fb4494783863 433 if (!callback)
samux 1:fb4494783863 434 wifi.attach(NULL);
samux 1:fb4494783863 435 else
samux 1:fb4494783863 436 wifi.attach(this, &Wifly::handler_rx);
samux 1:fb4494783863 437 }
samux 1:fb4494783863 438
samux 1:fb4494783863 439
samux 1:fb4494783863 440 int Wifly::send(const char * str, int len, const char * ACK, char * res, int timeout)
samux 1:fb4494783863 441 {
samux 1:fb4494783863 442 char read;
samux 1:fb4494783863 443 size_t found = string::npos;
samux 1:fb4494783863 444 string checking;
samux 1:fb4494783863 445 Timer tmr;
samux 1:fb4494783863 446 int result = 0;
samux 1:fb4494783863 447
samux 1:fb4494783863 448 DBG("will send: %s\r\n",str);
samux 1:fb4494783863 449
samux 1:fb4494783863 450 attach_rx(false);
samux 1:fb4494783863 451
WiredHome 18:18ab3993d00d 452 flushIn();
samux 1:fb4494783863 453
samux 1:fb4494783863 454 if (!ACK || !strcmp(ACK, "NO")) {
samux 1:fb4494783863 455 for (int i = 0; i < len; i++)
samux 1:fb4494783863 456 result = (putc(str[i]) == str[i]) ? result + 1 : result;
samux 1:fb4494783863 457 } else {
samux 1:fb4494783863 458 tmr.start();
samux 1:fb4494783863 459 for (int i = 0; i < len; i++)
samux 1:fb4494783863 460 result = (putc(str[i]) == str[i]) ? result + 1 : result;
samux 1:fb4494783863 461
samux 1:fb4494783863 462 while (1) {
samux 1:fb4494783863 463 if (tmr.read_ms() > timeout) {
WiredHome 18:18ab3993d00d 464 flushIn();
samux 1:fb4494783863 465 DBG("check: %s\r\n", checking.c_str());
samux 1:fb4494783863 466 attach_rx(true);
samux 1:fb4494783863 467 return -1;
samux 1:fb4494783863 468 } else if (wifi.readable()) {
samux 1:fb4494783863 469 read = wifi.getc();
samux 1:fb4494783863 470 if ( read != '\r' && read != '\n') {
samux 1:fb4494783863 471 checking += read;
samux 1:fb4494783863 472 found = checking.find(ACK);
samux 1:fb4494783863 473 if (found != string::npos) {
WiredHome 18:18ab3993d00d 474 flushIn(10);
samux 1:fb4494783863 475 break;
samux 1:fb4494783863 476 }
samux 1:fb4494783863 477 }
samux 1:fb4494783863 478 }
samux 1:fb4494783863 479 }
samux 1:fb4494783863 480 DBG("check: %s\r\n", checking.c_str());
samux 1:fb4494783863 481
samux 1:fb4494783863 482 attach_rx(true);
samux 1:fb4494783863 483 return result;
samux 1:fb4494783863 484 }
samux 1:fb4494783863 485
samux 1:fb4494783863 486 //the user wants the result from the command (ACK == NULL, res != NULL)
samux 1:fb4494783863 487 if ( res != NULL) {
samux 1:fb4494783863 488 int i = 0;
samux 1:fb4494783863 489 Timer timeout;
samux 1:fb4494783863 490 timeout.start();
samux 1:fb4494783863 491 tmr.reset();
samux 1:fb4494783863 492 while (1) {
samux 1:fb4494783863 493 if (timeout.read() > 2) {
samux 1:fb4494783863 494 if (i == 0) {
samux 1:fb4494783863 495 res = NULL;
samux 1:fb4494783863 496 break;
samux 1:fb4494783863 497 }
samux 1:fb4494783863 498 res[i] = '\0';
samux 1:fb4494783863 499 DBG("user str 1: %s\r\n", res);
samux 1:fb4494783863 500
samux 1:fb4494783863 501 break;
samux 1:fb4494783863 502 } else {
samux 1:fb4494783863 503 if (tmr.read_ms() > 300) {
samux 1:fb4494783863 504 res[i] = '\0';
samux 1:fb4494783863 505 DBG("user str: %s\r\n", res);
samux 1:fb4494783863 506
samux 1:fb4494783863 507 break;
samux 1:fb4494783863 508 }
samux 1:fb4494783863 509 if (wifi.readable()) {
samux 1:fb4494783863 510 tmr.start();
samux 1:fb4494783863 511 read = wifi.getc();
samux 1:fb4494783863 512
samux 1:fb4494783863 513 // we drop \r and \n
samux 1:fb4494783863 514 if ( read != '\r' && read != '\n') {
samux 1:fb4494783863 515 res[i++] = read;
samux 1:fb4494783863 516 }
samux 1:fb4494783863 517 }
samux 1:fb4494783863 518 }
samux 1:fb4494783863 519 }
samux 1:fb4494783863 520 DBG("user str: %s\r\n", res);
samux 1:fb4494783863 521 }
samux 1:fb4494783863 522
WiredHome 18:18ab3993d00d 523 flushIn();
samux 1:fb4494783863 524 attach_rx(true);
samux 1:fb4494783863 525 DBG("result: %d\r\n", result)
samux 1:fb4494783863 526 return result;
WiredHome 18:18ab3993d00d 527 }
WiredHome 18:18ab3993d00d 528
WiredHome 18:18ab3993d00d 529 void Wifly::flushIn(int timeout_ms)
WiredHome 18:18ab3993d00d 530 {
WiredHome 18:18ab3993d00d 531 Timer tmr;
WiredHome 18:18ab3993d00d 532 #ifdef DEBUG
WiredHome 18:18ab3993d00d 533 char chatter[500];
WiredHome 18:18ab3993d00d 534 int count = 0;
WiredHome 18:18ab3993d00d 535 int c;
WiredHome 18:18ab3993d00d 536 #endif
WiredHome 18:18ab3993d00d 537
WiredHome 18:18ab3993d00d 538 if (timeout_ms == 0) {
WiredHome 18:18ab3993d00d 539 timeout_ms = 2;
WiredHome 18:18ab3993d00d 540 }
WiredHome 18:18ab3993d00d 541 tmr.start();
WiredHome 18:18ab3993d00d 542 while (wifi.readable() || (tmr.read_ms() < timeout_ms)) {
WiredHome 18:18ab3993d00d 543 if (wifi.readable()) {
WiredHome 18:18ab3993d00d 544 #ifndef DEBUG
WiredHome 18:18ab3993d00d 545 wifi.getc();
WiredHome 18:18ab3993d00d 546 #else
WiredHome 18:18ab3993d00d 547 c = wifi.getc();
WiredHome 18:18ab3993d00d 548 if (c != '\r' && c != '\n')
WiredHome 18:18ab3993d00d 549 chatter[count++] = c;
WiredHome 18:18ab3993d00d 550 #endif
WiredHome 18:18ab3993d00d 551 tmr.reset();
WiredHome 18:18ab3993d00d 552 tmr.start(); // start should not be necessary
WiredHome 18:18ab3993d00d 553 }
WiredHome 18:18ab3993d00d 554 }
WiredHome 18:18ab3993d00d 555 #ifdef DEBUG
WiredHome 18:18ab3993d00d 556 chatter[count] = '\0';
WiredHome 18:18ab3993d00d 557 DBG("Wifly::flushIn(%d) {%s}", timeout_ms, chatter);
WiredHome 18:18ab3993d00d 558 #endif
WiredHome 18:18ab3993d00d 559 }