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:
Thu Sep 05 22:56:33 2013 +0000
Revision:
34:52e4eec8b790
Parent:
32:4543e91ab4bf
Child:
35:2dc12224cbd6
Major change is in how to checks for expected response. Prior code used the C++ string method of concatenation, which could continue to allocate memory to exhaustion, even if it was only interested in a few character response.

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