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:
Mon Jul 01 04:37:55 2013 +0000
Revision:
12:6270ced02019
Parent:
11:44563217d2b9
Child:
13:7197313973a4
Quite a bit of refactoring of the wifly interface. Most of the changes are in the Wifly.cpp file.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
WiredHome 5:cc0506dc3565 1 /* Copyright (C) 2012 mbed.org, MIT License
WiredHome 5:cc0506dc3565 2 *
WiredHome 5:cc0506dc3565 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
WiredHome 5:cc0506dc3565 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
WiredHome 5:cc0506dc3565 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
WiredHome 5:cc0506dc3565 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
WiredHome 5:cc0506dc3565 7 * furnished to do so, subject to the following conditions:
WiredHome 5:cc0506dc3565 8 *
WiredHome 5:cc0506dc3565 9 * The above copyright notice and this permission notice shall be included in all copies or
WiredHome 5:cc0506dc3565 10 * substantial portions of the Software.
WiredHome 5:cc0506dc3565 11 *
WiredHome 5:cc0506dc3565 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
WiredHome 5:cc0506dc3565 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
WiredHome 5:cc0506dc3565 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
WiredHome 5:cc0506dc3565 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
WiredHome 5:cc0506dc3565 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
WiredHome 5:cc0506dc3565 17 */
WiredHome 5:cc0506dc3565 18
WiredHome 5:cc0506dc3565 19 #include "mbed.h"
WiredHome 5:cc0506dc3565 20 #include "Wifly.h"
WiredHome 5:cc0506dc3565 21 #include <string>
WiredHome 5:cc0506dc3565 22 #include <algorithm>
WiredHome 5:cc0506dc3565 23
WiredHome 12:6270ced02019 24 //#define DEBUG //Debug is disabled by default
WiredHome 11:44563217d2b9 25
WiredHome 11:44563217d2b9 26 #if (defined(DEBUG) && !defined(TARGET_LPC11U24))
WiredHome 5:cc0506dc3565 27 #define DBG(x, ...) std::printf("[DBG Wifly%4d] "x"\r\n", __LINE__, ##__VA_ARGS__);
WiredHome 5:cc0506dc3565 28 #define WARN(x, ...) std::printf("[WRN Wifly%4d] "x"\r\n", __LINE__, ##__VA_ARGS__);
WiredHome 5:cc0506dc3565 29 #define ERR(x, ...) std::printf("[ERR Wifly%4d] "x"\r\n", __LINE__, ##__VA_ARGS__);
WiredHome 5:cc0506dc3565 30 #define INFO(x, ...) std::printf("[INF Wifly%4d] "x"\r\n", __LINE__, ##__VA_ARGS__);
WiredHome 5:cc0506dc3565 31 #else
WiredHome 5:cc0506dc3565 32 #define DBG(x, ...)
WiredHome 5:cc0506dc3565 33 #define WARN(x, ...)
WiredHome 5:cc0506dc3565 34 #define ERR(x, ...)
WiredHome 5:cc0506dc3565 35 #define INFO(x, ...)
WiredHome 5:cc0506dc3565 36 #endif
WiredHome 5:cc0506dc3565 37
WiredHome 5:cc0506dc3565 38 #define MAX_TRY_JOIN 3
WiredHome 5:cc0506dc3565 39
WiredHome 11:44563217d2b9 40 // In its default mode, the Wifly module permits remote access and is rather "open"
WiredHome 11:44563217d2b9 41 // defining this will increase the security by restricting what is remote accessible
WiredHome 11:44563217d2b9 42 //#define INCREASE_SECURITY
WiredHome 11:44563217d2b9 43
WiredHome 5:cc0506dc3565 44 Wifly * Wifly::inst;
WiredHome 5:cc0506dc3565 45
WiredHome 10:a594fe035b36 46 Wifly::Wifly( PinName tx, PinName rx, PinName _reset, PinName tcp_status, const char * _ssid, const char * _phrase, Security sec):
WiredHome 5:cc0506dc3565 47 wifi(tx, rx), reset_pin(_reset), tcp_status(tcp_status), buf_wifly(256)
WiredHome 5:cc0506dc3565 48 {
WiredHome 5:cc0506dc3565 49 memset(&state, 0, sizeof(state));
WiredHome 5:cc0506dc3565 50 state.sec = sec;
WiredHome 10:a594fe035b36 51 SetPhrase(&this->ssid, _ssid);
WiredHome 10:a594fe035b36 52 SetPhrase(&this->phrase, _phrase);
WiredHome 5:cc0506dc3565 53 inst = this;
WiredHome 5:cc0506dc3565 54 attach_rx(false);
WiredHome 5:cc0506dc3565 55 state.cmd_mode = false;
WiredHome 10:a594fe035b36 56 wiflyVersionString = NULL;
WiredHome 10:a594fe035b36 57 baudrate = 9600;
WiredHome 10:a594fe035b36 58 }
WiredHome 10:a594fe035b36 59
WiredHome 5:cc0506dc3565 60 bool Wifly::join()
WiredHome 5:cc0506dc3565 61 {
WiredHome 5:cc0506dc3565 62 char cmd[20];
WiredHome 5:cc0506dc3565 63
WiredHome 5:cc0506dc3565 64 for (int i= 0; i < MAX_TRY_JOIN; i++) {
WiredHome 12:6270ced02019 65
WiredHome 5:cc0506dc3565 66 // no auto join
WiredHome 5:cc0506dc3565 67 if (!sendCommand("set w j 0\r", "AOK"))
WiredHome 5:cc0506dc3565 68 continue;
WiredHome 5:cc0506dc3565 69
WiredHome 12:6270ced02019 70 //no echo
WiredHome 5:cc0506dc3565 71 if (!sendCommand("set u m 1\r", "AOK"))
WiredHome 5:cc0506dc3565 72 continue;
WiredHome 5:cc0506dc3565 73
WiredHome 12:6270ced02019 74 // set comm time to flush (ms)
WiredHome 5:cc0506dc3565 75 if (!sendCommand("set c t 30\r", "AOK"))
WiredHome 5:cc0506dc3565 76 continue;
WiredHome 5:cc0506dc3565 77
WiredHome 12:6270ced02019 78 // set idle timeout to close (s)
WiredHome 12:6270ced02019 79 if (!sendCommand("set c i 5\r", "AOK"))
WiredHome 12:6270ced02019 80 continue;
WiredHome 12:6270ced02019 81
WiredHome 5:cc0506dc3565 82 // set size
WiredHome 5:cc0506dc3565 83 if (!sendCommand("set c s 1420\r", "AOK"))
WiredHome 5:cc0506dc3565 84 continue;
WiredHome 5:cc0506dc3565 85
WiredHome 5:cc0506dc3565 86 // red led on when tcp connection active
WiredHome 5:cc0506dc3565 87 if (!sendCommand("set s i 0x40\r", "AOK"))
WiredHome 5:cc0506dc3565 88 continue;
WiredHome 5:cc0506dc3565 89
WiredHome 5:cc0506dc3565 90 // no string sent to the tcp client
WiredHome 5:cc0506dc3565 91 if (!sendCommand("set c r 0\r", "AOK"))
WiredHome 5:cc0506dc3565 92 continue;
WiredHome 5:cc0506dc3565 93
WiredHome 5:cc0506dc3565 94 // tcp protocol
WiredHome 5:cc0506dc3565 95 if (!sendCommand("set i p 2\r", "AOK"))
WiredHome 5:cc0506dc3565 96 continue;
WiredHome 5:cc0506dc3565 97
WiredHome 5:cc0506dc3565 98 // tcp retry
WiredHome 5:cc0506dc3565 99 if (!sendCommand("set i f 0x7\r", "AOK"))
WiredHome 5:cc0506dc3565 100 continue;
WiredHome 5:cc0506dc3565 101
WiredHome 12:6270ced02019 102 #ifdef INCREASE_SECURITY
WiredHome 11:44563217d2b9 103 // tcp-mode 0x10 = disable remote configuration
WiredHome 11:44563217d2b9 104 // only in SW 2.27 and higher (see 2.3.39)
WiredHome 11:44563217d2b9 105 if ((swVersion >= 2.27) && (!sendCommand("set i t 0x10\r", "AOK")))
WiredHome 11:44563217d2b9 106 continue;
WiredHome 12:6270ced02019 107 #endif
WiredHome 11:44563217d2b9 108
WiredHome 5:cc0506dc3565 109 // set dns server
WiredHome 5:cc0506dc3565 110 if (!sendCommand("set d n rn.microchip.com\r", "AOK"))
WiredHome 5:cc0506dc3565 111 continue;
WiredHome 5:cc0506dc3565 112
WiredHome 11:44563217d2b9 113 // dhcp
WiredHome 5:cc0506dc3565 114 sprintf(cmd, "set i d %d\r", (state.dhcp) ? 1 : 0);
WiredHome 5:cc0506dc3565 115 if (!sendCommand(cmd, "AOK"))
WiredHome 5:cc0506dc3565 116 continue;
WiredHome 5:cc0506dc3565 117
WiredHome 5:cc0506dc3565 118 // ssid
WiredHome 5:cc0506dc3565 119 sprintf(cmd, "set w s %s\r", ssid);
WiredHome 5:cc0506dc3565 120 if (!sendCommand(cmd, "AOK"))
WiredHome 5:cc0506dc3565 121 continue;
WiredHome 5:cc0506dc3565 122
WiredHome 11:44563217d2b9 123 // auth
WiredHome 5:cc0506dc3565 124 sprintf(cmd, "set w a %d\r", state.sec);
WiredHome 5:cc0506dc3565 125 if (!sendCommand(cmd, "AOK"))
WiredHome 5:cc0506dc3565 126 continue;
WiredHome 5:cc0506dc3565 127
WiredHome 5:cc0506dc3565 128 // if no dhcp, set ip, netmask and gateway
WiredHome 5:cc0506dc3565 129 if (!state.dhcp) {
WiredHome 5:cc0506dc3565 130 DBG("not dhcp");
WiredHome 5:cc0506dc3565 131
WiredHome 5:cc0506dc3565 132 sprintf(cmd, "set i a %s\r\n", ip);
WiredHome 5:cc0506dc3565 133 if (!sendCommand(cmd, "AOK"))
WiredHome 5:cc0506dc3565 134 continue;
WiredHome 5:cc0506dc3565 135
WiredHome 5:cc0506dc3565 136 sprintf(cmd, "set i n %s\r", netmask);
WiredHome 5:cc0506dc3565 137 if (!sendCommand(cmd, "AOK"))
WiredHome 5:cc0506dc3565 138 continue;
WiredHome 5:cc0506dc3565 139
WiredHome 5:cc0506dc3565 140 sprintf(cmd, "set i g %s\r", gateway);
WiredHome 5:cc0506dc3565 141 if (!sendCommand(cmd, "AOK"))
WiredHome 5:cc0506dc3565 142 continue;
WiredHome 5:cc0506dc3565 143 }
WiredHome 5:cc0506dc3565 144
WiredHome 12:6270ced02019 145 // key step
WiredHome 10:a594fe035b36 146 cmd[0] = '\0';
WiredHome 10:a594fe035b36 147 switch (state.sec) {
WiredHome 10:a594fe035b36 148 case WPE_64: // google searching suggests this is a typo and should be WEP_64
WiredHome 10:a594fe035b36 149 case WEP_128:
WiredHome 10:a594fe035b36 150 sprintf(cmd, "set w k %s\r", phrase);
WiredHome 10:a594fe035b36 151 break;
WiredHome 10:a594fe035b36 152 case WPA1:
WiredHome 10:a594fe035b36 153 case WPA_MIXED: // alias WPA
WiredHome 10:a594fe035b36 154 case WPA2_PSK:
WiredHome 10:a594fe035b36 155 sprintf(cmd, "set w p %s\r", phrase);
WiredHome 10:a594fe035b36 156 break;
WiredHome 10:a594fe035b36 157 case ADHOC:
WiredHome 10:a594fe035b36 158 case NONE:
WiredHome 10:a594fe035b36 159 default:
WiredHome 10:a594fe035b36 160 break;
WiredHome 10:a594fe035b36 161 }
WiredHome 10:a594fe035b36 162 if (cmd[0] && !sendCommand(cmd, "AOK"))
WiredHome 10:a594fe035b36 163 continue;
WiredHome 5:cc0506dc3565 164
WiredHome 5:cc0506dc3565 165 //join the network (10s timeout)
WiredHome 5:cc0506dc3565 166 if (state.dhcp) {
WiredHome 8:79415e982c32 167 if (!sendCommand("join\r", "DHCP=ON", NULL, 0, 10000))
WiredHome 5:cc0506dc3565 168 continue;
WiredHome 5:cc0506dc3565 169 } else {
WiredHome 8:79415e982c32 170 if (!sendCommand("join\r", "Associated", NULL, 0, 10000))
WiredHome 5:cc0506dc3565 171 continue;
WiredHome 5:cc0506dc3565 172 }
WiredHome 5:cc0506dc3565 173
WiredHome 5:cc0506dc3565 174 //do not store, so it is default on every start
WiredHome 5:cc0506dc3565 175 //if (!sendCommand("save\r", "Stor"))
WiredHome 5:cc0506dc3565 176 // continue;
WiredHome 5:cc0506dc3565 177 exit(); // exit command mode
WiredHome 5:cc0506dc3565 178
WiredHome 5:cc0506dc3565 179 state.associated = true;
WiredHome 5:cc0506dc3565 180 //INFO("\r\nssid: %s\r\nphrase: %s\r\nsecurity: %s\r\n\r\n", this->ssid, this->phrase, getStringSecurity());
WiredHome 5:cc0506dc3565 181 return true;
WiredHome 5:cc0506dc3565 182 }
WiredHome 5:cc0506dc3565 183 return false;
WiredHome 5:cc0506dc3565 184 }
WiredHome 5:cc0506dc3565 185
WiredHome 5:cc0506dc3565 186
WiredHome 5:cc0506dc3565 187 bool Wifly::setProtocol(Protocol p)
WiredHome 5:cc0506dc3565 188 {
WiredHome 5:cc0506dc3565 189 // use udp auto pairing
WiredHome 5:cc0506dc3565 190 char cmd[20];
WiredHome 5:cc0506dc3565 191 sprintf(cmd, "set i p %d\r", p);
WiredHome 5:cc0506dc3565 192 if (!sendCommand(cmd, "AOK"))
WiredHome 5:cc0506dc3565 193 return false;
WiredHome 5:cc0506dc3565 194
WiredHome 5:cc0506dc3565 195 switch(p) {
WiredHome 5:cc0506dc3565 196 case TCP:
WiredHome 5:cc0506dc3565 197 // set ip flags: tcp retry enabled
WiredHome 5:cc0506dc3565 198 if (!sendCommand("set i f 0x07\r", "AOK"))
WiredHome 5:cc0506dc3565 199 return false;
WiredHome 5:cc0506dc3565 200 break;
WiredHome 5:cc0506dc3565 201 case UDP:
WiredHome 5:cc0506dc3565 202 // set ip flags: udp auto pairing enabled
WiredHome 5:cc0506dc3565 203 if (!sendCommand("set i h 0.0.0.0\r", "AOK"))
WiredHome 5:cc0506dc3565 204 return false;
WiredHome 5:cc0506dc3565 205 if (!sendCommand("set i f 0x40\r", "AOK"))
WiredHome 5:cc0506dc3565 206 return false;
WiredHome 5:cc0506dc3565 207 break;
WiredHome 5:cc0506dc3565 208 }
WiredHome 5:cc0506dc3565 209 state.proto = p;
WiredHome 5:cc0506dc3565 210 return true;
WiredHome 5:cc0506dc3565 211 }
WiredHome 5:cc0506dc3565 212
WiredHome 5:cc0506dc3565 213 char * Wifly::getStringSecurity()
WiredHome 5:cc0506dc3565 214 {
WiredHome 5:cc0506dc3565 215 switch(state.sec) {
WiredHome 9:86105ba18d96 216 case NONE: // 0
WiredHome 5:cc0506dc3565 217 return "NONE";
WiredHome 9:86105ba18d96 218 case WEP_128: // 1
WiredHome 5:cc0506dc3565 219 return "WEP_128";
WiredHome 9:86105ba18d96 220 case WPA1: // 2
WiredHome 9:86105ba18d96 221 return "WPA1";
WiredHome 9:86105ba18d96 222 case WPA: // 3
WiredHome 5:cc0506dc3565 223 return "WPA";
WiredHome 9:86105ba18d96 224 case WPA2_PSK: // 4
WiredHome 9:86105ba18d96 225 return "WPA2_PSK";
WiredHome 9:86105ba18d96 226 case ADHOC: // 6
WiredHome 9:86105ba18d96 227 return "ADHOC";
WiredHome 9:86105ba18d96 228 case WPE_64: // 8
WiredHome 9:86105ba18d96 229 return "WPE_64";
WiredHome 9:86105ba18d96 230 default: // ?
WiredHome 9:86105ba18d96 231 return "UNKNOWN";
WiredHome 5:cc0506dc3565 232 }
WiredHome 5:cc0506dc3565 233 }
WiredHome 5:cc0506dc3565 234
WiredHome 5:cc0506dc3565 235 bool Wifly::connect(const char * host, int port)
WiredHome 5:cc0506dc3565 236 {
WiredHome 5:cc0506dc3565 237 char rcv[20];
WiredHome 5:cc0506dc3565 238 char cmd[20];
WiredHome 5:cc0506dc3565 239
WiredHome 5:cc0506dc3565 240 // try to open
WiredHome 5:cc0506dc3565 241 sprintf(cmd, "open %s %d\r", host, port);
WiredHome 8:79415e982c32 242 if (sendCommand(cmd, "OPEN", NULL, 0, 10000)) {
WiredHome 5:cc0506dc3565 243 state.tcp = true;
WiredHome 5:cc0506dc3565 244 state.cmd_mode = false;
WiredHome 5:cc0506dc3565 245 return true;
WiredHome 5:cc0506dc3565 246 }
WiredHome 5:cc0506dc3565 247
WiredHome 5:cc0506dc3565 248 // if failed, retry and parse the response
WiredHome 8:79415e982c32 249 if (sendCommand(cmd, NULL, rcv, sizeof(rcv), 5000)) {
WiredHome 5:cc0506dc3565 250 if (strstr(rcv, "OPEN") == NULL) {
WiredHome 5:cc0506dc3565 251 if (strstr(rcv, "Connected") != NULL) {
WiredHome 5:cc0506dc3565 252 if (!sendCommand("close\r", "CLOS"))
WiredHome 5:cc0506dc3565 253 return false;
WiredHome 8:79415e982c32 254 if (!sendCommand(cmd, "OPEN", NULL, 0, 10000))
WiredHome 5:cc0506dc3565 255 return false;
WiredHome 5:cc0506dc3565 256 } else {
WiredHome 5:cc0506dc3565 257 return false;
WiredHome 5:cc0506dc3565 258 }
WiredHome 5:cc0506dc3565 259 }
WiredHome 5:cc0506dc3565 260 } else {
WiredHome 5:cc0506dc3565 261 return false;
WiredHome 5:cc0506dc3565 262 }
WiredHome 5:cc0506dc3565 263 state.tcp = true;
WiredHome 12:6270ced02019 264 exit(); // state.cmd_mode = false;
WiredHome 5:cc0506dc3565 265 return true;
WiredHome 5:cc0506dc3565 266 }
WiredHome 5:cc0506dc3565 267
WiredHome 12:6270ced02019 268 void Wifly::setConnectionState(bool value)
WiredHome 12:6270ced02019 269 {
WiredHome 5:cc0506dc3565 270 state.tcp = value;
WiredHome 5:cc0506dc3565 271 }
WiredHome 5:cc0506dc3565 272
WiredHome 5:cc0506dc3565 273
WiredHome 12:6270ced02019 274 float Wifly::getWiflyVersion()
WiredHome 12:6270ced02019 275 {
WiredHome 10:a594fe035b36 276 return swVersion;
WiredHome 10:a594fe035b36 277 }
WiredHome 5:cc0506dc3565 278
WiredHome 10:a594fe035b36 279
WiredHome 12:6270ced02019 280 char * Wifly::getWiflyVersionString()
WiredHome 12:6270ced02019 281 {
WiredHome 12:6270ced02019 282 return wiflyVersionString;
WiredHome 9:86105ba18d96 283 }
WiredHome 9:86105ba18d96 284
WiredHome 5:cc0506dc3565 285 bool Wifly::gethostbyname(const char * host, char * ip, int sizeof_ip)
WiredHome 5:cc0506dc3565 286 {
WiredHome 5:cc0506dc3565 287 string h = host;
WiredHome 9:86105ba18d96 288 char rcv[100]; // This we hope is generous enough for the "lookup" response
WiredHome 9:86105ba18d96 289 char * cmd;
WiredHome 9:86105ba18d96 290 const char lookup[] = "lookup %s\r";
WiredHome 5:cc0506dc3565 291 int l = 0;
WiredHome 5:cc0506dc3565 292 char * point;
WiredHome 5:cc0506dc3565 293 int nb_digits = 0;
WiredHome 5:cc0506dc3565 294
WiredHome 5:cc0506dc3565 295 // no dns needed
WiredHome 5:cc0506dc3565 296 int pos = h.find(".");
WiredHome 5:cc0506dc3565 297 if (pos != string::npos) {
WiredHome 5:cc0506dc3565 298 string sub = h.substr(0, h.find("."));
WiredHome 5:cc0506dc3565 299 nb_digits = atoi(sub.c_str());
WiredHome 5:cc0506dc3565 300 }
WiredHome 5:cc0506dc3565 301 if (count(h.begin(), h.end(), '.') == 3 && nb_digits > 0) {
WiredHome 5:cc0506dc3565 302 strcpy(ip, host);
WiredHome 5:cc0506dc3565 303 }
WiredHome 5:cc0506dc3565 304 // dns needed
WiredHome 5:cc0506dc3565 305 else {
WiredHome 5:cc0506dc3565 306 nb_digits = 0;
WiredHome 9:86105ba18d96 307 cmd = (char *)malloc(strlen(lookup) + strlen(host));
WiredHome 9:86105ba18d96 308 if (cmd) {
WiredHome 9:86105ba18d96 309 bool ok;
WiredHome 9:86105ba18d96 310 sprintf(cmd, lookup, host);
WiredHome 9:86105ba18d96 311 ok = sendCommand(cmd, NULL, rcv, sizeof(rcv));
WiredHome 9:86105ba18d96 312 free(cmd); // cleanup the allocation when done with the cmd
WiredHome 9:86105ba18d96 313 if (!ok)
WiredHome 9:86105ba18d96 314 return false;
WiredHome 12:6270ced02019 315
WiredHome 9:86105ba18d96 316 // look for the ip address
WiredHome 9:86105ba18d96 317 char * begin = strstr(rcv, "=") + 1;
WiredHome 9:86105ba18d96 318 for (int i = 0; i < 3; i++) {
WiredHome 9:86105ba18d96 319 point = strstr(begin + l, ".");
WiredHome 9:86105ba18d96 320 DBG("str: %s", begin + l);
WiredHome 9:86105ba18d96 321 l += point - (begin + l) + 1;
WiredHome 9:86105ba18d96 322 }
WiredHome 5:cc0506dc3565 323 DBG("str: %s", begin + l);
WiredHome 9:86105ba18d96 324 while(*(begin + l + nb_digits) >= '0' && *(begin + l + nb_digits) <= '9') {
WiredHome 9:86105ba18d96 325 DBG("digit: %c", *(begin + l + nb_digits));
WiredHome 9:86105ba18d96 326 nb_digits++;
WiredHome 9:86105ba18d96 327 }
WiredHome 9:86105ba18d96 328 if (l + nb_digits <= sizeof_ip) {
WiredHome 9:86105ba18d96 329 memcpy(ip, begin, l + nb_digits);
WiredHome 9:86105ba18d96 330 ip[l+nb_digits] = '\0';
WiredHome 9:86105ba18d96 331 DBG("ip from dns: %s", ip);
WiredHome 9:86105ba18d96 332 } else {
WiredHome 9:86105ba18d96 333 return false;
WiredHome 9:86105ba18d96 334 }
WiredHome 5:cc0506dc3565 335 } else {
WiredHome 9:86105ba18d96 336 return false; // malloc failed
WiredHome 5:cc0506dc3565 337 }
WiredHome 5:cc0506dc3565 338 }
WiredHome 5:cc0506dc3565 339 return true;
WiredHome 5:cc0506dc3565 340 }
WiredHome 5:cc0506dc3565 341
WiredHome 5:cc0506dc3565 342
WiredHome 5:cc0506dc3565 343 void Wifly::flush()
WiredHome 5:cc0506dc3565 344 {
WiredHome 12:6270ced02019 345 DBG("Wifly::flush()");
WiredHome 5:cc0506dc3565 346 buf_wifly.flush();
WiredHome 5:cc0506dc3565 347 }
WiredHome 5:cc0506dc3565 348
WiredHome 8:79415e982c32 349 bool Wifly::sendCommand(const char * cmd, const char * ack, char * res, int resSize, int timeout)
WiredHome 5:cc0506dc3565 350 {
WiredHome 10:a594fe035b36 351 int tries = 2; // how many tries
WiredHome 12:6270ced02019 352
WiredHome 5:cc0506dc3565 353 if (!state.cmd_mode) {
WiredHome 5:cc0506dc3565 354 cmdMode();
WiredHome 5:cc0506dc3565 355 }
WiredHome 10:a594fe035b36 356 while (tries--) {
WiredHome 10:a594fe035b36 357 if (send(cmd, strlen(cmd), ack, res, resSize, timeout) >=0 ) {
WiredHome 10:a594fe035b36 358 return true;
WiredHome 10:a594fe035b36 359 }
WiredHome 10:a594fe035b36 360 ERR("sendCommand: cannot '%s'\r\n", cmd);
WiredHome 5:cc0506dc3565 361 }
WiredHome 10:a594fe035b36 362 return false;
WiredHome 5:cc0506dc3565 363 }
WiredHome 5:cc0506dc3565 364
WiredHome 5:cc0506dc3565 365 bool Wifly::cmdMode()
WiredHome 5:cc0506dc3565 366 {
WiredHome 5:cc0506dc3565 367 // if already in cmd mode, return
WiredHome 5:cc0506dc3565 368 if (state.cmd_mode)
WiredHome 5:cc0506dc3565 369 return true;
WiredHome 5:cc0506dc3565 370
WiredHome 5:cc0506dc3565 371 wait_ms(250); // manual 1.2.1 (250 msec before and after)
WiredHome 5:cc0506dc3565 372 if (send("$$$", 3, "CMD") == -1) {
WiredHome 5:cc0506dc3565 373 ERR("cannot enter in cmd mode\r\n");
WiredHome 5:cc0506dc3565 374 exit();
WiredHome 5:cc0506dc3565 375 return false;
WiredHome 5:cc0506dc3565 376 }
WiredHome 5:cc0506dc3565 377 state.cmd_mode = true;
WiredHome 5:cc0506dc3565 378 return true;
WiredHome 5:cc0506dc3565 379 }
WiredHome 5:cc0506dc3565 380
WiredHome 5:cc0506dc3565 381 bool Wifly::disconnect()
WiredHome 5:cc0506dc3565 382 {
WiredHome 5:cc0506dc3565 383 // if already disconnected, return
WiredHome 5:cc0506dc3565 384 if (!state.associated)
WiredHome 5:cc0506dc3565 385 return true;
WiredHome 5:cc0506dc3565 386
WiredHome 5:cc0506dc3565 387 if (!sendCommand("leave\r", "DeAuth"))
WiredHome 5:cc0506dc3565 388 return false;
WiredHome 5:cc0506dc3565 389 exit();
WiredHome 5:cc0506dc3565 390
WiredHome 5:cc0506dc3565 391 state.associated = false;
WiredHome 5:cc0506dc3565 392 return true;
WiredHome 5:cc0506dc3565 393 }
WiredHome 5:cc0506dc3565 394
WiredHome 5:cc0506dc3565 395 bool Wifly::is_connected()
WiredHome 5:cc0506dc3565 396 {
WiredHome 5:cc0506dc3565 397 return (tcp_status.read() == 1) ? true : false;
WiredHome 5:cc0506dc3565 398 }
WiredHome 5:cc0506dc3565 399
WiredHome 5:cc0506dc3565 400
WiredHome 12:6270ced02019 401 void Wifly::reset()
WiredHome 12:6270ced02019 402 {
WiredHome 12:6270ced02019 403 reset_pin = 0;
WiredHome 12:6270ced02019 404 wifi.baud(9600);
WiredHome 12:6270ced02019 405 wait_ms(200);
WiredHome 12:6270ced02019 406 reset_pin = 1;
WiredHome 12:6270ced02019 407 GatherLogonInfo();
WiredHome 12:6270ced02019 408 }
WiredHome 12:6270ced02019 409
WiredHome 12:6270ced02019 410 bool Wifly::reboot()
WiredHome 12:6270ced02019 411 {
WiredHome 12:6270ced02019 412 // if already in cmd mode, return
WiredHome 12:6270ced02019 413 sendCommand("reboot\r");
WiredHome 12:6270ced02019 414 wait_ms(300);
WiredHome 12:6270ced02019 415 wifi.baud(9600); // After a reboot, it is back at 9600 baud because we don't store
WiredHome 12:6270ced02019 416 baud(baudrate); // shift it up to where you want it
WiredHome 12:6270ced02019 417 exit(); // exit command mode
WiredHome 12:6270ced02019 418 return true;
WiredHome 12:6270ced02019 419 }
WiredHome 12:6270ced02019 420
WiredHome 12:6270ced02019 421 #if 1
WiredHome 12:6270ced02019 422 bool Wifly::baud(int _baudrate)
WiredHome 12:6270ced02019 423 {
WiredHome 12:6270ced02019 424 char cmd[20];
WiredHome 12:6270ced02019 425 bool res = false;
WiredHome 12:6270ced02019 426
WiredHome 12:6270ced02019 427 baudrate = _baudrate;
WiredHome 12:6270ced02019 428 sprintf(cmd, "set u i %d\r", baudrate);
WiredHome 12:6270ced02019 429 // set u i # causes it to exit command mode (manual 2.3.64)
WiredHome 12:6270ced02019 430 // but testing indicates that it does not exit command mode.
WiredHome 12:6270ced02019 431 sendCommand(cmd); // may not see the answer (see 2.3.64)
WiredHome 12:6270ced02019 432 wait_ms(10); // more than enough for even 2400 baud
WiredHome 12:6270ced02019 433 flushIn();
WiredHome 12:6270ced02019 434 // state.cmd_mode = false; // see note above why this is disabled
WiredHome 12:6270ced02019 435 wifi.baud(baudrate);
WiredHome 12:6270ced02019 436 res = true;
WiredHome 12:6270ced02019 437 return res;
WiredHome 12:6270ced02019 438 }
WiredHome 12:6270ced02019 439 #else
WiredHome 12:6270ced02019 440 bool Wifly::baud(int _baudrate)
WiredHome 12:6270ced02019 441 {
WiredHome 12:6270ced02019 442 const int baudrates[] = {2400, 4800, 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600};
WiredHome 12:6270ced02019 443 #define BRCOUNT (sizeof(baudrates)/sizeof(baudrates[0]))
WiredHome 12:6270ced02019 444 char cmd[20];
WiredHome 12:6270ced02019 445 int test = 0;
WiredHome 12:6270ced02019 446 bool res = false;
WiredHome 12:6270ced02019 447
WiredHome 12:6270ced02019 448 // set u i # causes it to exit command mode (manual 2.3.64)
WiredHome 12:6270ced02019 449 // but testing indicates that it does not exit command mode.
WiredHome 12:6270ced02019 450 while (test <= BRCOUNT) {
WiredHome 12:6270ced02019 451 DBG("baud(%d) test:%d, count:%d", _baudrate, test, BRCOUNT);
WiredHome 12:6270ced02019 452 flushIn();
WiredHome 12:6270ced02019 453 sprintf(cmd, "set u i %d\r", _baudrate);
WiredHome 12:6270ced02019 454 sendCommand(cmd); // may not see a response (see 2.3.64)
WiredHome 12:6270ced02019 455 flushIn();
WiredHome 12:6270ced02019 456 if (sendCommand("ver\r", "AOK")) {
WiredHome 12:6270ced02019 457 // state.cmd_mode = false; // see note above why this is disabled
WiredHome 12:6270ced02019 458 baudrate = _baudrate;
WiredHome 12:6270ced02019 459 wifi.baud(baudrate);
WiredHome 12:6270ced02019 460 DBG(" baud set to %d", baudrate);
WiredHome 12:6270ced02019 461 res = true;
WiredHome 12:6270ced02019 462 break;
WiredHome 12:6270ced02019 463 }
WiredHome 12:6270ced02019 464 DBG(" next");
WiredHome 12:6270ced02019 465 // keep trying baudrates between ARM and WiFly
WiredHome 12:6270ced02019 466 if (test < BRCOUNT) {
WiredHome 12:6270ced02019 467 DBG("baud test %d", baudrates[test]);
WiredHome 12:6270ced02019 468 wifi.baud(baudrates[test]);
WiredHome 12:6270ced02019 469 }
WiredHome 12:6270ced02019 470 test++;
WiredHome 12:6270ced02019 471 }
WiredHome 12:6270ced02019 472 DBG(" res %d", res);
WiredHome 12:6270ced02019 473 return res;
WiredHome 12:6270ced02019 474 }
WiredHome 12:6270ced02019 475 #endif
WiredHome 12:6270ced02019 476
WiredHome 12:6270ced02019 477
WiredHome 5:cc0506dc3565 478 bool Wifly::close()
WiredHome 5:cc0506dc3565 479 {
WiredHome 5:cc0506dc3565 480 if (!state.tcp)
WiredHome 5:cc0506dc3565 481 return true;
WiredHome 5:cc0506dc3565 482
WiredHome 5:cc0506dc3565 483 if (!sendCommand("close\r", "CLOS"))
WiredHome 5:cc0506dc3565 484 return false;
WiredHome 5:cc0506dc3565 485 exit();
WiredHome 5:cc0506dc3565 486
WiredHome 5:cc0506dc3565 487 state.tcp = false;
WiredHome 5:cc0506dc3565 488 return true;
WiredHome 5:cc0506dc3565 489 }
WiredHome 5:cc0506dc3565 490
WiredHome 5:cc0506dc3565 491
WiredHome 5:cc0506dc3565 492 int Wifly::putc(char c)
WiredHome 5:cc0506dc3565 493 {
WiredHome 5:cc0506dc3565 494 while (!wifi.writeable())
WiredHome 5:cc0506dc3565 495 ;
WiredHome 5:cc0506dc3565 496 return wifi.putc(c);
WiredHome 5:cc0506dc3565 497 }
WiredHome 5:cc0506dc3565 498
WiredHome 5:cc0506dc3565 499
WiredHome 5:cc0506dc3565 500 bool Wifly::exit()
WiredHome 5:cc0506dc3565 501 {
WiredHome 5:cc0506dc3565 502 if (!sendCommand("exit\r", "EXIT"))
WiredHome 5:cc0506dc3565 503 return false;
WiredHome 5:cc0506dc3565 504 state.cmd_mode = false;
WiredHome 5:cc0506dc3565 505 return true;
WiredHome 5:cc0506dc3565 506 }
WiredHome 5:cc0506dc3565 507
WiredHome 5:cc0506dc3565 508
WiredHome 5:cc0506dc3565 509 int Wifly::readable()
WiredHome 5:cc0506dc3565 510 {
WiredHome 5:cc0506dc3565 511 return buf_wifly.available();
WiredHome 5:cc0506dc3565 512 }
WiredHome 5:cc0506dc3565 513
WiredHome 5:cc0506dc3565 514 int Wifly::writeable()
WiredHome 5:cc0506dc3565 515 {
WiredHome 5:cc0506dc3565 516 return wifi.writeable();
WiredHome 5:cc0506dc3565 517 }
WiredHome 5:cc0506dc3565 518
WiredHome 5:cc0506dc3565 519 char Wifly::getc()
WiredHome 5:cc0506dc3565 520 {
WiredHome 5:cc0506dc3565 521 char c;
WiredHome 5:cc0506dc3565 522 while (!buf_wifly.available())
WiredHome 5:cc0506dc3565 523 ;
WiredHome 5:cc0506dc3565 524 buf_wifly.dequeue(&c);
WiredHome 5:cc0506dc3565 525 return c;
WiredHome 5:cc0506dc3565 526 }
WiredHome 5:cc0506dc3565 527
WiredHome 5:cc0506dc3565 528 void Wifly::handler_rx(void)
WiredHome 5:cc0506dc3565 529 {
WiredHome 5:cc0506dc3565 530 //read characters
WiredHome 5:cc0506dc3565 531 while (wifi.readable())
WiredHome 5:cc0506dc3565 532 buf_wifly.queue(wifi.getc());
WiredHome 5:cc0506dc3565 533 }
WiredHome 5:cc0506dc3565 534
WiredHome 5:cc0506dc3565 535 void Wifly::attach_rx(bool callback)
WiredHome 5:cc0506dc3565 536 {
WiredHome 5:cc0506dc3565 537 if (!callback)
WiredHome 5:cc0506dc3565 538 wifi.attach(NULL);
WiredHome 5:cc0506dc3565 539 else
WiredHome 5:cc0506dc3565 540 wifi.attach(this, &Wifly::handler_rx);
WiredHome 5:cc0506dc3565 541 }
WiredHome 5:cc0506dc3565 542
WiredHome 5:cc0506dc3565 543
WiredHome 8:79415e982c32 544 int Wifly::send(const char * str, int len, const char * ACK, char * res, int resSize, int timeout)
WiredHome 5:cc0506dc3565 545 {
WiredHome 5:cc0506dc3565 546 char read;
WiredHome 5:cc0506dc3565 547 size_t found = string::npos;
WiredHome 5:cc0506dc3565 548 string checking;
WiredHome 5:cc0506dc3565 549 Timer tmr;
WiredHome 5:cc0506dc3565 550 int result = 0;
WiredHome 5:cc0506dc3565 551
WiredHome 5:cc0506dc3565 552 attach_rx(false);
WiredHome 11:44563217d2b9 553 flushIn(); //We flush the buffer
WiredHome 5:cc0506dc3565 554
WiredHome 5:cc0506dc3565 555 if (!ACK || !strcmp(ACK, "NO")) {
WiredHome 5:cc0506dc3565 556 for (int i = 0; i < len; i++)
WiredHome 5:cc0506dc3565 557 result = (putc(str[i]) == str[i]) ? result + 1 : result;
WiredHome 5:cc0506dc3565 558 } else {
WiredHome 5:cc0506dc3565 559 tmr.start();
WiredHome 5:cc0506dc3565 560 for (int i = 0; i < len; i++)
WiredHome 5:cc0506dc3565 561 result = (putc(str[i]) == str[i]) ? result + 1 : result;
WiredHome 12:6270ced02019 562
WiredHome 5:cc0506dc3565 563 while (1) {
WiredHome 5:cc0506dc3565 564 if (tmr.read_ms() > timeout) {
WiredHome 5:cc0506dc3565 565 DBG(" timeout");
WiredHome 5:cc0506dc3565 566 flushIn();
WiredHome 5:cc0506dc3565 567 //DBG("check: %s", checking.c_str());
WiredHome 12:6270ced02019 568
WiredHome 12:6270ced02019 569 attach_rx(true);
WiredHome 12:6270ced02019 570 return -1;
WiredHome 5:cc0506dc3565 571 } else if (wifi.readable()) {
WiredHome 5:cc0506dc3565 572 read = wifi.getc();
WiredHome 5:cc0506dc3565 573 if ( read != '\r' && read != '\n') {
WiredHome 5:cc0506dc3565 574 checking += read;
WiredHome 5:cc0506dc3565 575 found = checking.find(ACK);
WiredHome 5:cc0506dc3565 576 if (found != string::npos) {
WiredHome 5:cc0506dc3565 577 wait_ms(10);
WiredHome 5:cc0506dc3565 578 flushIn();
WiredHome 12:6270ced02019 579
WiredHome 5:cc0506dc3565 580 break;
WiredHome 5:cc0506dc3565 581 }
WiredHome 5:cc0506dc3565 582 }
WiredHome 5:cc0506dc3565 583 }
WiredHome 5:cc0506dc3565 584 }
WiredHome 12:6270ced02019 585 DBG("check: %s\r\n", checking.c_str());
WiredHome 5:cc0506dc3565 586 attach_rx(true);
WiredHome 5:cc0506dc3565 587 return result;
WiredHome 5:cc0506dc3565 588 }
WiredHome 5:cc0506dc3565 589
WiredHome 8:79415e982c32 590 //the user wants the result from the command (ACK == NULL, res != NULL, resSize > 0)
WiredHome 8:79415e982c32 591 if ( res != NULL && resSize > 0) {
WiredHome 5:cc0506dc3565 592 int i = 0;
WiredHome 5:cc0506dc3565 593 Timer timeout;
WiredHome 12:6270ced02019 594
WiredHome 5:cc0506dc3565 595 timeout.start();
WiredHome 5:cc0506dc3565 596 tmr.reset();
WiredHome 11:44563217d2b9 597 while (i < resSize-1) {
WiredHome 5:cc0506dc3565 598 if (timeout.read() > 2) {
WiredHome 12:6270ced02019 599 if (i == 0) {
WiredHome 12:6270ced02019 600 res = NULL;
WiredHome 12:6270ced02019 601 break;
WiredHome 12:6270ced02019 602 }
WiredHome 12:6270ced02019 603 res[i] = '\0';
WiredHome 5:cc0506dc3565 604 break;
WiredHome 5:cc0506dc3565 605 } else {
WiredHome 5:cc0506dc3565 606 if (tmr.read_ms() > 300) {
WiredHome 12:6270ced02019 607 res[i] = '\0';
WiredHome 5:cc0506dc3565 608 break;
WiredHome 5:cc0506dc3565 609 }
WiredHome 5:cc0506dc3565 610 if (wifi.readable()) {
WiredHome 5:cc0506dc3565 611 tmr.start();
WiredHome 5:cc0506dc3565 612 read = wifi.getc();
WiredHome 12:6270ced02019 613
WiredHome 12:6270ced02019 614 // we drop \r and \n
WiredHome 12:6270ced02019 615 if ( read != '\r' && read != '\n') {
WiredHome 5:cc0506dc3565 616 res[i++] = read;
WiredHome 5:cc0506dc3565 617 }
WiredHome 5:cc0506dc3565 618 }
WiredHome 5:cc0506dc3565 619 }
WiredHome 5:cc0506dc3565 620 }
WiredHome 12:6270ced02019 621 DBG("user str: %s\r\n", res);
WiredHome 5:cc0506dc3565 622 }
WiredHome 11:44563217d2b9 623 flushIn(); //We flush the buffer
WiredHome 5:cc0506dc3565 624 attach_rx(true);
WiredHome 12:6270ced02019 625 DBG("result: %d\r\n", result)
WiredHome 5:cc0506dc3565 626 return result;
WiredHome 5:cc0506dc3565 627 }
WiredHome 8:79415e982c32 628
WiredHome 12:6270ced02019 629
WiredHome 12:6270ced02019 630 void Wifly::SetPhrase(char ** dst, const char * src)
WiredHome 12:6270ced02019 631 {
WiredHome 11:44563217d2b9 632 // change all ' ' in '$' in the ssid and the passphrase
WiredHome 11:44563217d2b9 633 *dst = (char *)malloc(strlen(src)+1);
WiredHome 11:44563217d2b9 634 if (*dst) {
WiredHome 11:44563217d2b9 635 strcpy(*dst, src);
WiredHome 11:44563217d2b9 636 for (int i = 0; i < strlen(*dst); i++) {
WiredHome 11:44563217d2b9 637 if ((*dst)[i] == ' ')
WiredHome 11:44563217d2b9 638 (*dst)[i] = '$';
WiredHome 11:44563217d2b9 639 }
WiredHome 11:44563217d2b9 640 } else {
WiredHome 11:44563217d2b9 641 *dst = NULL;
WiredHome 11:44563217d2b9 642 }
WiredHome 11:44563217d2b9 643 }
WiredHome 11:44563217d2b9 644
WiredHome 11:44563217d2b9 645
WiredHome 11:44563217d2b9 646 void Wifly::flushIn(int timeout_ms)
WiredHome 11:44563217d2b9 647 {
WiredHome 11:44563217d2b9 648 Timer tmr;
WiredHome 12:6270ced02019 649
WiredHome 12:6270ced02019 650 DBG("Wifly::flushIn()");
WiredHome 11:44563217d2b9 651 tmr.start();
WiredHome 11:44563217d2b9 652 if (timeout_ms == 0) {
WiredHome 11:44563217d2b9 653 timeout_ms = 2 * 10000 / baudrate; // compute minimal timeout
WiredHome 11:44563217d2b9 654 if (timeout_ms == 0)
WiredHome 11:44563217d2b9 655 timeout_ms = 2;
WiredHome 11:44563217d2b9 656 }
WiredHome 11:44563217d2b9 657 while (wifi.readable() || (tmr.read_ms() < timeout_ms)) {
WiredHome 11:44563217d2b9 658 if (wifi.readable()) {
WiredHome 11:44563217d2b9 659 tmr.reset();
WiredHome 11:44563217d2b9 660 tmr.start(); // start should not be necessary
WiredHome 11:44563217d2b9 661 #if 0 && defined(DEBUG)
WiredHome 11:44563217d2b9 662 std::putchar(wifi.getc());
WiredHome 11:44563217d2b9 663 #else
WiredHome 11:44563217d2b9 664 wifi.getc();
WiredHome 11:44563217d2b9 665 #endif
WiredHome 11:44563217d2b9 666 }
WiredHome 11:44563217d2b9 667 }
WiredHome 11:44563217d2b9 668 }
WiredHome 11:44563217d2b9 669
WiredHome 12:6270ced02019 670 void Wifly::GatherLogonInfo()
WiredHome 12:6270ced02019 671 {
WiredHome 11:44563217d2b9 672 Timer timer;
WiredHome 11:44563217d2b9 673 char logonText[200];
WiredHome 11:44563217d2b9 674 int i = 0;
WiredHome 11:44563217d2b9 675 char *p;
WiredHome 11:44563217d2b9 676
WiredHome 11:44563217d2b9 677 timer.start();
WiredHome 11:44563217d2b9 678 if (wiflyVersionString) {
WiredHome 11:44563217d2b9 679 free(wiflyVersionString);
WiredHome 11:44563217d2b9 680 wiflyVersionString = NULL;
WiredHome 11:44563217d2b9 681 }
WiredHome 11:44563217d2b9 682 logonText[i] = '\0';
WiredHome 11:44563217d2b9 683 while (timer.read_ms() < 500) {
WiredHome 11:44563217d2b9 684 while (wifi.readable()) {
WiredHome 11:44563217d2b9 685 logonText[i++] = wifi.getc();
WiredHome 11:44563217d2b9 686 }
WiredHome 11:44563217d2b9 687 }
WiredHome 11:44563217d2b9 688 logonText[i] = '\0';
WiredHome 11:44563217d2b9 689 p = strchr(logonText, '\r');
WiredHome 11:44563217d2b9 690 if (p)
WiredHome 11:44563217d2b9 691 *p = '\0';
WiredHome 11:44563217d2b9 692 wiflyVersionString = (char *)malloc(strlen(logonText)+1);
WiredHome 11:44563217d2b9 693 if (wiflyVersionString)
WiredHome 11:44563217d2b9 694 strcpy(wiflyVersionString, logonText);
WiredHome 11:44563217d2b9 695 p = strstr(logonText, "Ver ");
WiredHome 11:44563217d2b9 696 if (p) {
WiredHome 11:44563217d2b9 697 p += 4;
WiredHome 11:44563217d2b9 698 swVersion = atof(p);
WiredHome 11:44563217d2b9 699 }
WiredHome 11:44563217d2b9 700 }