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:
Sat Apr 19 16:36:15 2014 +0000
Revision:
67:557ea7ad15c1
Parent:
66:e52bd50e6d9f
Child:
69:a4064f7e3529
Revise the Wifly interface so that recovery from a lost connection is possible. See the "in_connected()" method for an example.

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