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 23:26:58 2013 +0000
Revision:
24:5012a535b1d5
Parent:
23:6e77c65ced00
Child:
25:a740eb74a86a
Refactored the ssid and passphrase handler to eliminate redundant code.

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
WiredHome 20:906b0f951bc3 24 #define DEBUG //Debug is disabled by default
WiredHome 20:906b0f951bc3 25
WiredHome 20:906b0f951bc3 26 #if (defined(DEBUG) && !defined(TARGET_LPC11U24))
WiredHome 20:906b0f951bc3 27 #define DBG(x, ...) std::printf("[DBG Wifly%4d] "x"\r\n", __LINE__, ##__VA_ARGS__);
WiredHome 20:906b0f951bc3 28 #define WARN(x, ...) std::printf("[WRN Wifly%4d] "x"\r\n", __LINE__, ##__VA_ARGS__);
WiredHome 20:906b0f951bc3 29 #define ERR(x, ...) std::printf("[ERR Wifly%4d] "x"\r\n", __LINE__, ##__VA_ARGS__);
WiredHome 20:906b0f951bc3 30 #define INFO(x, ...) std::printf("[INF Wifly%4d] "x"\r\n", __LINE__, ##__VA_ARGS__);
samux 1:fb4494783863 31 #else
samux 1:fb4494783863 32 #define DBG(x, ...)
samux 1:fb4494783863 33 #define WARN(x, ...)
samux 1:fb4494783863 34 #define ERR(x, ...)
WiredHome 20:906b0f951bc3 35 #define INFO(x, ...)
samux 1:fb4494783863 36 #endif
samux 1:fb4494783863 37
samux 1:fb4494783863 38 #define MAX_TRY_JOIN 3
samux 1:fb4494783863 39
samux 1:fb4494783863 40 Wifly * Wifly::inst;
samux 1:fb4494783863 41
samux 1:fb4494783863 42 Wifly::Wifly( PinName tx, PinName rx, PinName _reset, PinName tcp_status, const char * ssid, const char * phrase, Security sec):
WiredHome 20:906b0f951bc3 43 wifi(tx, rx), reset_pin(_reset), tcp_status(tcp_status), baudrate(9600), buf_wifly(256)
samux 1:fb4494783863 44 {
samux 1:fb4494783863 45 memset(&state, 0, sizeof(state));
samux 1:fb4494783863 46 state.sec = sec;
samux 1:fb4494783863 47
WiredHome 24:5012a535b1d5 48 FixPhrase(&this->ssid, ssid);
WiredHome 24:5012a535b1d5 49 FixPhrase(&this->phrase, phrase);
samux 1:fb4494783863 50
samux 1:fb4494783863 51 inst = this;
samux 1:fb4494783863 52 attach_rx(false);
samux 1:fb4494783863 53 state.cmd_mode = false;
samux 1:fb4494783863 54 }
samux 1:fb4494783863 55
samux 1:fb4494783863 56 bool Wifly::join()
samux 1:fb4494783863 57 {
samux 1:fb4494783863 58 char cmd[20];
samux 1:fb4494783863 59
samux 1:fb4494783863 60 for (int i= 0; i < MAX_TRY_JOIN; i++) {
samux 2:8e54830d0df7 61
samux 2:8e54830d0df7 62 // no auto join
samux 2:8e54830d0df7 63 if (!sendCommand("set w j 0\r", "AOK"))
samux 2:8e54830d0df7 64 continue;
samux 2:8e54830d0df7 65
samux 2:8e54830d0df7 66 //no echo
samux 2:8e54830d0df7 67 if (!sendCommand("set u m 1\r", "AOK"))
samux 2:8e54830d0df7 68 continue;
samux 2:8e54830d0df7 69
samux 1:fb4494783863 70 // set time
samux 4:0bcec6272784 71 if (!sendCommand("set c t 30\r", "AOK"))
samux 1:fb4494783863 72 continue;
samux 1:fb4494783863 73
samux 1:fb4494783863 74 // set size
samux 4:0bcec6272784 75 if (!sendCommand("set c s 1024\r", "AOK"))
samux 1:fb4494783863 76 continue;
samux 1:fb4494783863 77
samux 1:fb4494783863 78 // red led on when tcp connection active
samux 1:fb4494783863 79 if (!sendCommand("set s i 0x40\r", "AOK"))
samux 1:fb4494783863 80 continue;
samux 1:fb4494783863 81
samux 1:fb4494783863 82 // no string sent to the tcp client
samux 1:fb4494783863 83 if (!sendCommand("set c r 0\r", "AOK"))
samux 1:fb4494783863 84 continue;
samux 1:fb4494783863 85
samux 1:fb4494783863 86 // tcp protocol
samux 1:fb4494783863 87 if (!sendCommand("set i p 2\r", "AOK"))
samux 1:fb4494783863 88 continue;
samux 1:fb4494783863 89
samux 1:fb4494783863 90 // tcp retry
samux 1:fb4494783863 91 if (!sendCommand("set i f 0x7\r", "AOK"))
samux 1:fb4494783863 92 continue;
WiredHome 20:906b0f951bc3 93
samux 2:8e54830d0df7 94 // set dns server
samux 2:8e54830d0df7 95 if (!sendCommand("set d n rn.microchip.com\r", "AOK"))
samux 1:fb4494783863 96 continue;
samux 1:fb4494783863 97
samux 1:fb4494783863 98 //dhcp
samux 1:fb4494783863 99 sprintf(cmd, "set i d %d\r", (state.dhcp) ? 1 : 0);
samux 1:fb4494783863 100 if (!sendCommand(cmd, "AOK"))
samux 1:fb4494783863 101 continue;
samux 1:fb4494783863 102
samux 1:fb4494783863 103 // ssid
samux 1:fb4494783863 104 sprintf(cmd, "set w s %s\r", ssid);
samux 1:fb4494783863 105 if (!sendCommand(cmd, "AOK"))
samux 1:fb4494783863 106 continue;
samux 1:fb4494783863 107
samux 1:fb4494783863 108 //auth
samux 1:fb4494783863 109 sprintf(cmd, "set w a %d\r", state.sec);
samux 1:fb4494783863 110 if (!sendCommand(cmd, "AOK"))
samux 1:fb4494783863 111 continue;
samux 1:fb4494783863 112
samux 1:fb4494783863 113 // if no dhcp, set ip, netmask and gateway
samux 1:fb4494783863 114 if (!state.dhcp) {
WiredHome 20:906b0f951bc3 115 DBG("not dhcp");
samux 1:fb4494783863 116
samux 1:fb4494783863 117 sprintf(cmd, "set i a %s\r\n", ip);
samux 1:fb4494783863 118 if (!sendCommand(cmd, "AOK"))
samux 1:fb4494783863 119 continue;
samux 1:fb4494783863 120
samux 1:fb4494783863 121 sprintf(cmd, "set i n %s\r", netmask);
samux 1:fb4494783863 122 if (!sendCommand(cmd, "AOK"))
samux 1:fb4494783863 123 continue;
samux 1:fb4494783863 124
samux 1:fb4494783863 125 sprintf(cmd, "set i g %s\r", gateway);
samux 1:fb4494783863 126 if (!sendCommand(cmd, "AOK"))
samux 1:fb4494783863 127 continue;
samux 1:fb4494783863 128 }
samux 1:fb4494783863 129
samux 1:fb4494783863 130 //key step
samux 1:fb4494783863 131 if (state.sec != NONE) {
samux 1:fb4494783863 132 if (state.sec == WPA)
samux 1:fb4494783863 133 sprintf(cmd, "set w p %s\r", phrase);
samux 1:fb4494783863 134 else if (state.sec == WEP_128)
samux 1:fb4494783863 135 sprintf(cmd, "set w k %s\r", phrase);
samux 1:fb4494783863 136
samux 1:fb4494783863 137 if (!sendCommand(cmd, "AOK"))
samux 1:fb4494783863 138 continue;
samux 1:fb4494783863 139 }
samux 1:fb4494783863 140
samux 2:8e54830d0df7 141 //join the network (10s timeout)
samux 1:fb4494783863 142 if (state.dhcp) {
samux 2:8e54830d0df7 143 if (!sendCommand("join\r", "DHCP=ON", NULL, 10000))
samux 2:8e54830d0df7 144 continue;
samux 2:8e54830d0df7 145 } else {
samux 2:8e54830d0df7 146 if (!sendCommand("join\r", "Associated", NULL, 10000))
samux 1:fb4494783863 147 continue;
samux 1:fb4494783863 148 }
samux 1:fb4494783863 149
samux 2:8e54830d0df7 150 if (!sendCommand("save\r", "Stor"))
samux 2:8e54830d0df7 151 continue;
samux 2:8e54830d0df7 152
samux 1:fb4494783863 153 exit();
samux 1:fb4494783863 154
samux 1:fb4494783863 155 state.associated = true;
samux 1:fb4494783863 156 INFO("\r\nssid: %s\r\nphrase: %s\r\nsecurity: %s\r\n\r\n", this->ssid, this->phrase, getStringSecurity());
samux 1:fb4494783863 157 return true;
samux 1:fb4494783863 158 }
samux 1:fb4494783863 159 return false;
samux 1:fb4494783863 160 }
samux 1:fb4494783863 161
samux 1:fb4494783863 162
samux 1:fb4494783863 163 bool Wifly::setProtocol(Protocol p)
samux 1:fb4494783863 164 {
samux 1:fb4494783863 165 // use udp auto pairing
samux 1:fb4494783863 166 char cmd[20];
samux 1:fb4494783863 167 sprintf(cmd, "set i p %d\r", p);
samux 1:fb4494783863 168 if (!sendCommand(cmd, "AOK"))
samux 1:fb4494783863 169 return false;
samux 1:fb4494783863 170
samux 1:fb4494783863 171 switch(p) {
samux 1:fb4494783863 172 case TCP:
samux 1:fb4494783863 173 // set ip flags: tcp retry enabled
samux 1:fb4494783863 174 if (!sendCommand("set i f 0x07\r", "AOK"))
samux 1:fb4494783863 175 return false;
samux 1:fb4494783863 176 break;
samux 1:fb4494783863 177 case UDP:
samux 1:fb4494783863 178 // set ip flags: udp auto pairing enabled
samux 1:fb4494783863 179 if (!sendCommand("set i h 0.0.0.0\r", "AOK"))
samux 1:fb4494783863 180 return false;
samux 4:0bcec6272784 181 if (!sendCommand("set i f 0x40\r", "AOK"))
samux 1:fb4494783863 182 return false;
samux 1:fb4494783863 183 break;
samux 1:fb4494783863 184 }
samux 1:fb4494783863 185 state.proto = p;
samux 1:fb4494783863 186 return true;
samux 1:fb4494783863 187 }
samux 1:fb4494783863 188
samux 1:fb4494783863 189 char * Wifly::getStringSecurity()
samux 1:fb4494783863 190 {
samux 1:fb4494783863 191 switch(state.sec) {
samux 1:fb4494783863 192 case NONE:
samux 1:fb4494783863 193 return "NONE";
samux 1:fb4494783863 194 case WEP_128:
samux 1:fb4494783863 195 return "WEP_128";
samux 1:fb4494783863 196 case WPA:
samux 1:fb4494783863 197 return "WPA";
samux 1:fb4494783863 198 }
samux 1:fb4494783863 199 return "UNKNOWN";
samux 1:fb4494783863 200 }
samux 1:fb4494783863 201
samux 1:fb4494783863 202 bool Wifly::connect(const char * host, int port)
samux 1:fb4494783863 203 {
samux 1:fb4494783863 204 char rcv[20];
samux 1:fb4494783863 205 char cmd[20];
samux 1:fb4494783863 206
samux 2:8e54830d0df7 207 // try to open
samux 2:8e54830d0df7 208 sprintf(cmd, "open %s %d\r", host, port);
samux 2:8e54830d0df7 209 if (sendCommand(cmd, "OPEN", NULL, 10000)) {
samux 2:8e54830d0df7 210 state.tcp = true;
samux 2:8e54830d0df7 211 state.cmd_mode = false;
samux 2:8e54830d0df7 212 return true;
samux 1:fb4494783863 213 }
samux 1:fb4494783863 214
samux 2:8e54830d0df7 215 // if failed, retry and parse the response
samux 2:8e54830d0df7 216 if (sendCommand(cmd, NULL, rcv, 5000)) {
samux 1:fb4494783863 217 if (strstr(rcv, "OPEN") == NULL) {
samux 1:fb4494783863 218 if (strstr(rcv, "Connected") != NULL) {
samux 2:8e54830d0df7 219 wait(0.25);
samux 1:fb4494783863 220 if (!sendCommand("close\r", "CLOS"))
samux 1:fb4494783863 221 return false;
samux 2:8e54830d0df7 222 wait(0.25);
samux 2:8e54830d0df7 223 if (!sendCommand(cmd, "OPEN", NULL, 10000))
samux 1:fb4494783863 224 return false;
samux 1:fb4494783863 225 } else {
samux 1:fb4494783863 226 return false;
samux 1:fb4494783863 227 }
samux 1:fb4494783863 228 }
samux 1:fb4494783863 229 } else {
samux 1:fb4494783863 230 return false;
samux 1:fb4494783863 231 }
samux 2:8e54830d0df7 232
samux 1:fb4494783863 233 state.tcp = true;
samux 1:fb4494783863 234 state.cmd_mode = false;
samux 1:fb4494783863 235
samux 1:fb4494783863 236 return true;
samux 1:fb4494783863 237 }
samux 1:fb4494783863 238
samux 1:fb4494783863 239
samux 1:fb4494783863 240 bool Wifly::gethostbyname(const char * host, char * ip)
samux 1:fb4494783863 241 {
samux 1:fb4494783863 242 string h = host;
samux 1:fb4494783863 243 char cmd[30], rcv[100];
samux 1:fb4494783863 244 int l = 0;
samux 1:fb4494783863 245 char * point;
samux 1:fb4494783863 246 int nb_digits = 0;
samux 1:fb4494783863 247
samux 1:fb4494783863 248 // no dns needed
samux 1:fb4494783863 249 int pos = h.find(".");
samux 1:fb4494783863 250 if (pos != string::npos) {
samux 1:fb4494783863 251 string sub = h.substr(0, h.find("."));
samux 1:fb4494783863 252 nb_digits = atoi(sub.c_str());
samux 1:fb4494783863 253 }
samux 1:fb4494783863 254 //printf("substrL %s\r\n", sub.c_str());
samux 1:fb4494783863 255 if (count(h.begin(), h.end(), '.') == 3 && nb_digits > 0) {
samux 1:fb4494783863 256 strcpy(ip, host);
samux 1:fb4494783863 257 }
samux 1:fb4494783863 258 // dns needed
samux 1:fb4494783863 259 else {
samux 1:fb4494783863 260 nb_digits = 0;
samux 1:fb4494783863 261 sprintf(cmd, "lookup %s\r", host);
samux 1:fb4494783863 262 if (!sendCommand(cmd, NULL, rcv))
samux 1:fb4494783863 263 return false;
samux 1:fb4494783863 264
samux 1:fb4494783863 265 // look for the ip address
samux 1:fb4494783863 266 char * begin = strstr(rcv, "=") + 1;
samux 1:fb4494783863 267 for (int i = 0; i < 3; i++) {
samux 1:fb4494783863 268 point = strstr(begin + l, ".");
samux 1:fb4494783863 269 DBG("str: %s", begin + l);
samux 1:fb4494783863 270 l += point - (begin + l) + 1;
samux 1:fb4494783863 271 }
samux 1:fb4494783863 272 DBG("str: %s", begin + l);
samux 1:fb4494783863 273 while(*(begin + l + nb_digits) >= '0' && *(begin + l + nb_digits) <= '9') {
samux 1:fb4494783863 274 DBG("digit: %c", *(begin + l + nb_digits));
samux 1:fb4494783863 275 nb_digits++;
samux 1:fb4494783863 276 }
samux 1:fb4494783863 277 memcpy(ip, begin, l + nb_digits);
samux 1:fb4494783863 278 ip[l+nb_digits] = 0;
samux 1:fb4494783863 279 DBG("ip from dns: %s", ip);
samux 1:fb4494783863 280 }
samux 1:fb4494783863 281 return true;
samux 1:fb4494783863 282 }
samux 1:fb4494783863 283
samux 1:fb4494783863 284
samux 1:fb4494783863 285 void Wifly::flush()
samux 1:fb4494783863 286 {
samux 1:fb4494783863 287 buf_wifly.flush();
samux 1:fb4494783863 288 }
samux 1:fb4494783863 289
samux 1:fb4494783863 290 bool Wifly::sendCommand(const char * cmd, const char * ack, char * res, int timeout)
samux 1:fb4494783863 291 {
WiredHome 20:906b0f951bc3 292 int tries = 1;
WiredHome 20:906b0f951bc3 293
samux 1:fb4494783863 294 if (!state.cmd_mode) {
samux 1:fb4494783863 295 cmdMode();
samux 1:fb4494783863 296 }
WiredHome 20:906b0f951bc3 297 while (tries <= 2) {
WiredHome 20:906b0f951bc3 298 DBG("sendCommand() try: %d, this: %s", tries, cmd);
WiredHome 20:906b0f951bc3 299 if (send(cmd, strlen(cmd), ack, res, timeout) >= 0) {
WiredHome 20:906b0f951bc3 300 return true;
WiredHome 20:906b0f951bc3 301 }
WiredHome 20:906b0f951bc3 302 tries++;
samux 1:fb4494783863 303 }
WiredHome 20:906b0f951bc3 304 ERR("sendCommand: failed: %s", cmd);
WiredHome 17:213844a1864f 305 return false;
samux 1:fb4494783863 306 }
samux 1:fb4494783863 307
samux 1:fb4494783863 308 bool Wifly::cmdMode()
samux 1:fb4494783863 309 {
samux 1:fb4494783863 310 // if already in cmd mode, return
samux 1:fb4494783863 311 if (state.cmd_mode)
samux 1:fb4494783863 312 return true;
samux 2:8e54830d0df7 313
samux 1:fb4494783863 314 if (send("$$$", 3, "CMD") == -1) {
samux 1:fb4494783863 315 ERR("cannot enter in cmd mode\r\n");
samux 2:8e54830d0df7 316 exit();
samux 1:fb4494783863 317 return false;
samux 1:fb4494783863 318 }
samux 1:fb4494783863 319 state.cmd_mode = true;
samux 1:fb4494783863 320 return true;
samux 1:fb4494783863 321 }
samux 1:fb4494783863 322
samux 1:fb4494783863 323 bool Wifly::disconnect()
samux 1:fb4494783863 324 {
samux 1:fb4494783863 325 // if already disconnected, return
samux 1:fb4494783863 326 if (!state.associated)
samux 1:fb4494783863 327 return true;
samux 2:8e54830d0df7 328
samux 1:fb4494783863 329 if (!sendCommand("leave\r", "DeAuth"))
samux 1:fb4494783863 330 return false;
samux 1:fb4494783863 331 exit();
samux 2:8e54830d0df7 332
samux 1:fb4494783863 333 state.associated = false;
samux 1:fb4494783863 334 return true;
samux 1:fb4494783863 335
samux 1:fb4494783863 336 }
samux 1:fb4494783863 337
samux 1:fb4494783863 338 bool Wifly::is_connected()
samux 1:fb4494783863 339 {
samux 1:fb4494783863 340 return (tcp_status.read() == 1) ? true : false;
samux 1:fb4494783863 341 }
samux 1:fb4494783863 342
samux 1:fb4494783863 343
samux 1:fb4494783863 344 void Wifly::reset()
samux 1:fb4494783863 345 {
samux 1:fb4494783863 346 reset_pin = 0;
samux 1:fb4494783863 347 wait(0.2);
samux 1:fb4494783863 348 reset_pin = 1;
samux 1:fb4494783863 349 wait(0.2);
samux 1:fb4494783863 350 }
samux 1:fb4494783863 351
samux 3:9aa05e19c62e 352 bool Wifly::reboot()
samux 3:9aa05e19c62e 353 {
samux 3:9aa05e19c62e 354 // if already in cmd mode, return
samux 3:9aa05e19c62e 355 if (!sendCommand("reboot\r"))
samux 3:9aa05e19c62e 356 return false;
WiredHome 20:906b0f951bc3 357
samux 3:9aa05e19c62e 358 wait(0.3);
samux 3:9aa05e19c62e 359
samux 3:9aa05e19c62e 360 state.cmd_mode = false;
samux 3:9aa05e19c62e 361 return true;
samux 3:9aa05e19c62e 362 }
samux 3:9aa05e19c62e 363
samux 1:fb4494783863 364 bool Wifly::close()
samux 1:fb4494783863 365 {
samux 1:fb4494783863 366 // if not connected, return
samux 1:fb4494783863 367 if (!state.tcp)
samux 1:fb4494783863 368 return true;
samux 2:8e54830d0df7 369
samux 1:fb4494783863 370 wait(0.25);
samux 1:fb4494783863 371 if (!sendCommand("close\r", "CLOS"))
samux 1:fb4494783863 372 return false;
samux 1:fb4494783863 373 exit();
samux 2:8e54830d0df7 374
samux 1:fb4494783863 375 state.tcp = false;
samux 1:fb4494783863 376 return true;
samux 1:fb4494783863 377 }
samux 1:fb4494783863 378
samux 1:fb4494783863 379
samux 1:fb4494783863 380 int Wifly::putc(char c)
samux 1:fb4494783863 381 {
samux 1:fb4494783863 382 while (!wifi.writeable());
samux 1:fb4494783863 383 return wifi.putc(c);
samux 1:fb4494783863 384 }
samux 1:fb4494783863 385
samux 1:fb4494783863 386
samux 1:fb4494783863 387 bool Wifly::exit()
samux 1:fb4494783863 388 {
samux 1:fb4494783863 389 flush();
samux 1:fb4494783863 390 if (!state.cmd_mode)
samux 1:fb4494783863 391 return true;
samux 1:fb4494783863 392 if (!sendCommand("exit\r", "EXIT"))
samux 1:fb4494783863 393 return false;
samux 1:fb4494783863 394 state.cmd_mode = false;
samux 1:fb4494783863 395 flush();
samux 1:fb4494783863 396 return true;
samux 1:fb4494783863 397 }
samux 1:fb4494783863 398
samux 1:fb4494783863 399
samux 1:fb4494783863 400 int Wifly::readable()
samux 1:fb4494783863 401 {
samux 1:fb4494783863 402 return buf_wifly.available();
samux 1:fb4494783863 403 }
samux 1:fb4494783863 404
samux 1:fb4494783863 405 int Wifly::writeable()
samux 1:fb4494783863 406 {
samux 1:fb4494783863 407 return wifi.writeable();
samux 1:fb4494783863 408 }
samux 1:fb4494783863 409
samux 1:fb4494783863 410 char Wifly::getc()
samux 1:fb4494783863 411 {
samux 1:fb4494783863 412 char c;
samux 1:fb4494783863 413 while (!buf_wifly.available());
samux 1:fb4494783863 414 buf_wifly.dequeue(&c);
samux 1:fb4494783863 415 return c;
samux 1:fb4494783863 416 }
samux 1:fb4494783863 417
samux 1:fb4494783863 418 void Wifly::handler_rx(void)
samux 1:fb4494783863 419 {
samux 1:fb4494783863 420 //read characters
samux 1:fb4494783863 421 while (wifi.readable())
samux 1:fb4494783863 422 buf_wifly.queue(wifi.getc());
samux 1:fb4494783863 423 }
samux 1:fb4494783863 424
samux 1:fb4494783863 425 void Wifly::attach_rx(bool callback)
samux 1:fb4494783863 426 {
samux 1:fb4494783863 427 if (!callback)
samux 1:fb4494783863 428 wifi.attach(NULL);
samux 1:fb4494783863 429 else
samux 1:fb4494783863 430 wifi.attach(this, &Wifly::handler_rx);
samux 1:fb4494783863 431 }
samux 1:fb4494783863 432
samux 1:fb4494783863 433
samux 1:fb4494783863 434 int Wifly::send(const char * str, int len, const char * ACK, char * res, int timeout)
samux 1:fb4494783863 435 {
samux 1:fb4494783863 436 char read;
samux 1:fb4494783863 437 size_t found = string::npos;
samux 1:fb4494783863 438 string checking;
samux 1:fb4494783863 439 Timer tmr;
samux 1:fb4494783863 440 int result = 0;
samux 1:fb4494783863 441
WiredHome 20:906b0f951bc3 442 DBG("send in %d ms this: %s", timeout, str);
samux 1:fb4494783863 443
samux 1:fb4494783863 444 attach_rx(false);
samux 1:fb4494783863 445
WiredHome 18:18ab3993d00d 446 flushIn();
WiredHome 20:906b0f951bc3 447 tmr.start();
samux 1:fb4494783863 448 if (!ACK || !strcmp(ACK, "NO")) {
samux 1:fb4494783863 449 for (int i = 0; i < len; i++)
samux 1:fb4494783863 450 result = (putc(str[i]) == str[i]) ? result + 1 : result;
samux 1:fb4494783863 451 } else {
samux 1:fb4494783863 452 for (int i = 0; i < len; i++)
samux 1:fb4494783863 453 result = (putc(str[i]) == str[i]) ? result + 1 : result;
samux 1:fb4494783863 454
samux 1:fb4494783863 455 while (1) {
samux 1:fb4494783863 456 if (tmr.read_ms() > timeout) {
WiredHome 18:18ab3993d00d 457 flushIn();
WiredHome 20:906b0f951bc3 458 DBG("check: %s", checking.c_str());
samux 1:fb4494783863 459 attach_rx(true);
samux 1:fb4494783863 460 return -1;
samux 1:fb4494783863 461 } else if (wifi.readable()) {
samux 1:fb4494783863 462 read = wifi.getc();
samux 1:fb4494783863 463 if ( read != '\r' && read != '\n') {
samux 1:fb4494783863 464 checking += read;
samux 1:fb4494783863 465 found = checking.find(ACK);
samux 1:fb4494783863 466 if (found != string::npos) {
WiredHome 18:18ab3993d00d 467 flushIn(10);
samux 1:fb4494783863 468 break;
samux 1:fb4494783863 469 }
samux 1:fb4494783863 470 }
samux 1:fb4494783863 471 }
samux 1:fb4494783863 472 }
WiredHome 20:906b0f951bc3 473 DBG("check: %s", checking.c_str());
samux 1:fb4494783863 474 attach_rx(true);
samux 1:fb4494783863 475 return result;
samux 1:fb4494783863 476 }
samux 1:fb4494783863 477
samux 1:fb4494783863 478 //the user wants the result from the command (ACK == NULL, res != NULL)
samux 1:fb4494783863 479 if ( res != NULL) {
samux 1:fb4494783863 480 int i = 0;
samux 1:fb4494783863 481 while (1) {
WiredHome 19:b0c2488222a3 482 if (tmr.read_ms() > timeout) {
WiredHome 19:b0c2488222a3 483 res[i] = '\0';
samux 1:fb4494783863 484 if (i == 0) {
samux 1:fb4494783863 485 res = NULL;
samux 1:fb4494783863 486 }
WiredHome 23:6e77c65ced00 487 DBG("timed out");
samux 1:fb4494783863 488 break;
samux 1:fb4494783863 489 } else {
samux 1:fb4494783863 490 if (wifi.readable()) {
samux 1:fb4494783863 491 read = wifi.getc();
samux 1:fb4494783863 492 // we drop \r and \n
samux 1:fb4494783863 493 if ( read != '\r' && read != '\n') {
samux 1:fb4494783863 494 res[i++] = read;
samux 1:fb4494783863 495 }
samux 1:fb4494783863 496 }
samux 1:fb4494783863 497 }
samux 1:fb4494783863 498 }
WiredHome 20:906b0f951bc3 499 DBG("user str: %s", res);
samux 1:fb4494783863 500 }
samux 1:fb4494783863 501
WiredHome 18:18ab3993d00d 502 flushIn();
samux 1:fb4494783863 503 attach_rx(true);
WiredHome 20:906b0f951bc3 504 DBG("result: %d", result)
samux 1:fb4494783863 505 return result;
WiredHome 18:18ab3993d00d 506 }
WiredHome 18:18ab3993d00d 507
WiredHome 19:b0c2488222a3 508
WiredHome 18:18ab3993d00d 509 void Wifly::flushIn(int timeout_ms)
WiredHome 18:18ab3993d00d 510 {
WiredHome 18:18ab3993d00d 511 Timer tmr;
WiredHome 18:18ab3993d00d 512 #ifdef DEBUG
WiredHome 18:18ab3993d00d 513 char chatter[500];
WiredHome 18:18ab3993d00d 514 int count = 0;
WiredHome 18:18ab3993d00d 515 int c;
WiredHome 18:18ab3993d00d 516 #endif
WiredHome 18:18ab3993d00d 517
WiredHome 18:18ab3993d00d 518 if (timeout_ms == 0) {
WiredHome 18:18ab3993d00d 519 timeout_ms = 2;
WiredHome 18:18ab3993d00d 520 }
WiredHome 18:18ab3993d00d 521 tmr.start();
WiredHome 18:18ab3993d00d 522 while (wifi.readable() || (tmr.read_ms() < timeout_ms)) {
WiredHome 18:18ab3993d00d 523 if (wifi.readable()) {
WiredHome 18:18ab3993d00d 524 #ifndef DEBUG
WiredHome 18:18ab3993d00d 525 wifi.getc();
WiredHome 18:18ab3993d00d 526 #else
WiredHome 18:18ab3993d00d 527 c = wifi.getc();
WiredHome 18:18ab3993d00d 528 if (c != '\r' && c != '\n')
WiredHome 18:18ab3993d00d 529 chatter[count++] = c;
WiredHome 18:18ab3993d00d 530 #endif
WiredHome 18:18ab3993d00d 531 tmr.reset();
WiredHome 18:18ab3993d00d 532 tmr.start(); // start should not be necessary
WiredHome 18:18ab3993d00d 533 }
WiredHome 18:18ab3993d00d 534 }
WiredHome 18:18ab3993d00d 535 #ifdef DEBUG
WiredHome 18:18ab3993d00d 536 chatter[count] = '\0';
WiredHome 18:18ab3993d00d 537 DBG("Wifly::flushIn(%d) {%s}", timeout_ms, chatter);
WiredHome 18:18ab3993d00d 538 #endif
WiredHome 18:18ab3993d00d 539 }
WiredHome 20:906b0f951bc3 540
WiredHome 20:906b0f951bc3 541
WiredHome 20:906b0f951bc3 542 // The ARM uart and the Wifly uart have to be in sync or we get
WiredHome 20:906b0f951bc3 543 // no meaningful response, so then have to try the possibilities.
WiredHome 20:906b0f951bc3 544 //
WiredHome 20:906b0f951bc3 545 // First try is at the currently configured ARM uart baud, if
WiredHome 20:906b0f951bc3 546 // that fails then it shifts the ARM uart baud through the probable
WiredHome 20:906b0f951bc3 547 // speeds, trying to establish contact with the Wifly module.
WiredHome 20:906b0f951bc3 548 // Once contact is demonstrated (response to the 'ver' command),
WiredHome 20:906b0f951bc3 549 // then it sets the Wifly module and then the ARM uart.
WiredHome 20:906b0f951bc3 550 bool Wifly::baud(int _targetBaud)
WiredHome 20:906b0f951bc3 551 {
WiredHome 20:906b0f951bc3 552 // in testing, 460800 and 921600 may change the Wifly module where you can't
WiredHome 20:906b0f951bc3 553 // change it back w/o a reset. So, we won't even permit those speeds.
WiredHome 20:906b0f951bc3 554 const int baudrates[] = {2400, 4800, 9600, 19200, 38400, 57600, 115200, 230400}; //, 460800, 921600};
WiredHome 20:906b0f951bc3 555 #define BRCOUNT (sizeof(baudrates)/sizeof(baudrates[0]))
WiredHome 20:906b0f951bc3 556 char cmd[20]; // sized for "set u i 460800\r" [15+1], plus margin [4]
WiredHome 20:906b0f951bc3 557 int tryIndex = 0;
WiredHome 20:906b0f951bc3 558 bool res = false;
WiredHome 20:906b0f951bc3 559 int userIndex;
WiredHome 20:906b0f951bc3 560
WiredHome 20:906b0f951bc3 561 sprintf(cmd, "set u i %d\r", _targetBaud);
WiredHome 20:906b0f951bc3 562 // set u i # should cause it to exit command mode (manual 2.3.64),
WiredHome 20:906b0f951bc3 563 // but testing indicates that it does not.
WiredHome 20:906b0f951bc3 564 for (userIndex=0; userIndex < BRCOUNT; userIndex++) {
WiredHome 20:906b0f951bc3 565 if (_targetBaud == baudrates[userIndex]) {
WiredHome 20:906b0f951bc3 566 while (tryIndex <= BRCOUNT) {
WiredHome 20:906b0f951bc3 567 DBG("baud(%d) try: %d", _targetBaud, tryIndex);
WiredHome 20:906b0f951bc3 568 sendCommand(cmd); // shift Wifly to desired speed [it may not respond (see 2.3.64)]
WiredHome 20:906b0f951bc3 569 // state.cmd_mode = false; // see note above why this is disabled
WiredHome 20:906b0f951bc3 570 wifi.baud(_targetBaud); // shift the ARM uart to match
WiredHome 20:906b0f951bc3 571 if (sendCommand("ver\r", "wifly", NULL, timeToRespond(4))) { // use this to verify communications
WiredHome 20:906b0f951bc3 572 baudrate = _targetBaud;
WiredHome 20:906b0f951bc3 573 res = true;
WiredHome 20:906b0f951bc3 574 break; // success
WiredHome 20:906b0f951bc3 575 }
WiredHome 20:906b0f951bc3 576 // keep trying baudrates between ARM and WiFly
WiredHome 20:906b0f951bc3 577 if (tryIndex < BRCOUNT) {
WiredHome 20:906b0f951bc3 578 wifi.baud(baudrates[tryIndex]);
WiredHome 20:906b0f951bc3 579 }
WiredHome 20:906b0f951bc3 580 tryIndex++;
WiredHome 20:906b0f951bc3 581 }
WiredHome 20:906b0f951bc3 582 break; // if they selected a legitimate baud, try no others
WiredHome 20:906b0f951bc3 583 }
WiredHome 20:906b0f951bc3 584 }
WiredHome 20:906b0f951bc3 585 return res;
WiredHome 20:906b0f951bc3 586 }
WiredHome 20:906b0f951bc3 587
WiredHome 20:906b0f951bc3 588 int Wifly::timeToRespond(int stringLen)
WiredHome 20:906b0f951bc3 589 {
WiredHome 20:906b0f951bc3 590 return 150 + (1 | 10000/baudrate) * stringLen;
WiredHome 21:97294686b4a1 591 }
WiredHome 23:6e77c65ced00 592
WiredHome 24:5012a535b1d5 593
WiredHome 24:5012a535b1d5 594 void Wifly::FixPhrase(char ** dst, const char * src)
WiredHome 24:5012a535b1d5 595 {
WiredHome 24:5012a535b1d5 596 // change all ' ' in '$' in the ssid and the passphrase
WiredHome 24:5012a535b1d5 597 *dst = (char *)malloc(strlen(src)+1);
WiredHome 24:5012a535b1d5 598 if (*dst) {
WiredHome 24:5012a535b1d5 599 strcpy(*dst, src);
WiredHome 24:5012a535b1d5 600 for (int i = 0; i < strlen(*dst); i++) {
WiredHome 24:5012a535b1d5 601 if ((*dst)[i] == ' ')
WiredHome 24:5012a535b1d5 602 (*dst)[i] = '$';
WiredHome 24:5012a535b1d5 603 }
WiredHome 24:5012a535b1d5 604 } else {
WiredHome 24:5012a535b1d5 605 *dst = NULL;
WiredHome 24:5012a535b1d5 606 }
WiredHome 24:5012a535b1d5 607 }