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 Dec 30 20:24:38 2013 +0000
Revision:
53:3dd2e7f9edc0
Parent:
52:ffc0b655a23c
Child:
55:0729959263b7
Added a small guard on cmdMode in sendCommand.

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