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:
Fri Apr 18 19:00:10 2014 +0000
Revision:
66:e52bd50e6d9f
Parent:
64:6202428f37ec
Child:
67:557ea7ad15c1
remove a debug printf.

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