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 03 02:06:24 2018 +0000
Revision:
78:a03a5dade5b5
Parent:
73:59d0bbc4f905
Renamed the WiflyInterface header and class name to EthernetInterface to be more readily swapped with the wired version. The TimeInterface then works directly.

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