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 Jun 30 03:11:26 2013 +0000
Revision:
11:44563217d2b9
Parent:
10:a594fe035b36
Child:
12:6270ced02019
Rearranged functions to compare more easily

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