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 * @section DESCRIPTION
samux 1:fb4494783863 19 *
samux 1:fb4494783863 20 * Wifly RN131-C, wifi module
samux 1:fb4494783863 21 *
samux 1:fb4494783863 22 * Datasheet:
WiredHome 68:d9a3920cc407 23 * http://dlnmh9ip6v2uc.cloudfront.net/datasheets/Wireless/WiFi/WiFly-RN-UM.pdf
samux 1:fb4494783863 24 *
WiredHome 68:d9a3920cc407 25 * Changes relative to mbed official version and others as identified
WiredHome 68:d9a3920cc407 26 * in the thread http://mbed.org/forum/team-165-components-community/topic/4844/?page=1#comment-24108
WiredHome 68:d9a3920cc407 27 * Wifly:
WiredHome 68:d9a3920cc407 28 * @li increase default timeout to 2500 msec
WiredHome 68:d9a3920cc407 29 * @li add additional security options
WiredHome 68:d9a3920cc407 30 * @li add ability to set security after constructor
WiredHome 68:d9a3920cc407 31 * @li improve commands on the APIs
WiredHome 68:d9a3920cc407 32 * @li add baud() api
WiredHome 68:d9a3920cc407 33 * @li add setConnectionState() api
WiredHome 68:d9a3920cc407 34 * @li add getWiflyVersionString() api
WiredHome 68:d9a3920cc407 35 * @li add getWiflyVersion() api
WiredHome 68:d9a3920cc407 36 * @li add SWUpdateWifly() api
WiredHome 68:d9a3920cc407 37 * @li update for RawSerial instead of Serial
WiredHome 68:d9a3920cc407 38 * @li revise #define DEBUG interface
WiredHome 68:d9a3920cc407 39 * @li reduced some of the C++ string processing for reduced memory
WiredHome 68:d9a3920cc407 40 * @li changed size of auto-send from 1024 to 1420
WiredHome 68:d9a3920cc407 41 * @li add ability to disable remote (telnet) config
WiredHome 68:d9a3920cc407 42 * @li add retry to to the sendCommnand() api
WiredHome 68:d9a3920cc407 43 * @li correct is_connected() api to return true if associated
WiredHome 68:d9a3920cc407 44 * @li revise reset to gather Wifly version number and version string
WiredHome 68:d9a3920cc407 45 * @li reduce compiler warnings
WiredHome 68:d9a3920cc407 46 * @li derived from 4:0bcec6272784
samux 1:fb4494783863 47 */
samux 1:fb4494783863 48
samux 1:fb4494783863 49 #ifndef WIFLY_H
samux 1:fb4494783863 50 #define WIFLY_H
samux 1:fb4494783863 51
samux 1:fb4494783863 52 #include "mbed.h"
WiredHome 51:a4831b1cb9a4 53 #include "RawSerial.h"
samux 1:fb4494783863 54 #include "CBuffer.h"
samux 1:fb4494783863 55
WiredHome 69:a4064f7e3529 56 #define DEFAULT_WAIT_RESP_TIMEOUT 5000
samux 1:fb4494783863 57
WiredHome 30:f500260463b7 58 enum Security { // See Wifly user manual Table 2-13.
WiredHome 30:f500260463b7 59 NONE = 0,
WiredHome 30:f500260463b7 60 WEP_128 = 1,
WiredHome 30:f500260463b7 61 WPA1 = 2,
WiredHome 30:f500260463b7 62 WPA_MIXED = 3,
WiredHome 30:f500260463b7 63 WPA = 3, // maintained for backward compatibility
WiredHome 30:f500260463b7 64 WPA2_PSK = 4,
WiredHome 30:f500260463b7 65 ADHOC = 6,
WiredHome 30:f500260463b7 66 WPE_64 = 8,
WiredHome 30:f500260463b7 67 WEP_64 = 8 // probably what the last one should have been
samux 1:fb4494783863 68 };
samux 1:fb4494783863 69
samux 1:fb4494783863 70 enum Protocol {
samux 1:fb4494783863 71 UDP = (1 << 0),
samux 1:fb4494783863 72 TCP = (1 << 1)
samux 1:fb4494783863 73 };
samux 1:fb4494783863 74
WiredHome 15:121f473dae3f 75 /** the Wifly object
WiredHome 15:121f473dae3f 76 *
WiredHome 15:121f473dae3f 77 * This object controls the Wifly module.
WiredHome 15:121f473dae3f 78 */
samux 1:fb4494783863 79 class Wifly
samux 1:fb4494783863 80 {
samux 1:fb4494783863 81
samux 1:fb4494783863 82 public:
WiredHome 15:121f473dae3f 83 /**
samux 1:fb4494783863 84 * Constructor
samux 1:fb4494783863 85 *
samux 1:fb4494783863 86 * @param tx mbed pin to use for tx line of Serial interface
samux 1:fb4494783863 87 * @param rx mbed pin to use for rx line of Serial interface
samux 1:fb4494783863 88 * @param reset reset pin of the wifi module ()
samux 1:fb4494783863 89 * @param tcp_status connection status pin of the wifi module (GPIO 6)
samux 1:fb4494783863 90 * @param ssid ssid of the network
samux 1:fb4494783863 91 * @param phrase WEP or WPA key
WiredHome 30:f500260463b7 92 * @param sec Security type (NONE, WEP_128, WPA1, WPA | WPA_MIXED, WPA2_PSK, WEP_64 )
samux 1:fb4494783863 93 */
WiredHome 25:a740eb74a86a 94 Wifly(PinName tx, PinName rx, PinName reset, PinName tcp_status, const char * ssid, const char * phrase, Security sec);
samux 1:fb4494783863 95
WiredHome 15:121f473dae3f 96 /**
WiredHome 25:a740eb74a86a 97 * Destructor to clean up
WiredHome 25:a740eb74a86a 98 */
WiredHome 25:a740eb74a86a 99 ~Wifly();
WiredHome 25:a740eb74a86a 100
WiredHome 45:21b7bbaad62b 101 /** Optional means to set/reset the security
WiredHome 45:21b7bbaad62b 102 *
WiredHome 45:21b7bbaad62b 103 * If join() has not been called, then you can revise the security parameters
WiredHome 45:21b7bbaad62b 104 * from those in the constructor.
WiredHome 45:21b7bbaad62b 105 *
WiredHome 45:21b7bbaad62b 106 * @param ssid ssid of the network
WiredHome 45:21b7bbaad62b 107 * @param phrase WEP or WPA key
WiredHome 45:21b7bbaad62b 108 * @param sec Security type (NONE, WEP_128, WPA1, WPA | WPA_MIXED, WPA2_PSK, WEP_64 )
WiredHome 45:21b7bbaad62b 109 */
WiredHome 45:21b7bbaad62b 110 void SetSecurity(const char * ssid, const char * phrase, Security sec);
WiredHome 45:21b7bbaad62b 111
WiredHome 25:a740eb74a86a 112 /**
WiredHome 67:557ea7ad15c1 113 * Configure the wifi module with the parameters contained in the constructor.
WiredHome 67:557ea7ad15c1 114 *
WiredHome 67:557ea7ad15c1 115 * @return true if successful, false otherwise.
WiredHome 67:557ea7ad15c1 116 */
WiredHome 67:557ea7ad15c1 117 bool configure();
WiredHome 67:557ea7ad15c1 118
WiredHome 67:557ea7ad15c1 119 /**
samux 1:fb4494783863 120 * Connect the wifi module to the ssid contained in the constructor.
samux 1:fb4494783863 121 *
samux 1:fb4494783863 122 * @return true if connected, false otherwise
samux 1:fb4494783863 123 */
samux 1:fb4494783863 124 bool join();
samux 1:fb4494783863 125
WiredHome 15:121f473dae3f 126 /**
samux 1:fb4494783863 127 * Disconnect the wifly module from the access point
samux 1:fb4494783863 128 *
WiredHome 51:a4831b1cb9a4 129 * @return true if successful
samux 1:fb4494783863 130 */
samux 1:fb4494783863 131 bool disconnect();
samux 1:fb4494783863 132
WiredHome 15:121f473dae3f 133 /**
samux 1:fb4494783863 134 * Open a tcp connection with the specified host on the specified port
samux 1:fb4494783863 135 *
samux 1:fb4494783863 136 * @param host host (can be either an ip address or a name. If a name is provided, a dns request will be established)
samux 1:fb4494783863 137 * @param port port
WiredHome 51:a4831b1cb9a4 138 * @return true if successful
samux 1:fb4494783863 139 */
samux 1:fb4494783863 140 bool connect(const char * host, int port);
samux 1:fb4494783863 141
samux 1:fb4494783863 142
WiredHome 15:121f473dae3f 143 /**
samux 1:fb4494783863 144 * Set the protocol (UDP or TCP)
samux 1:fb4494783863 145 *
samux 1:fb4494783863 146 * @param p protocol
WiredHome 51:a4831b1cb9a4 147 * @return true if successful
samux 1:fb4494783863 148 */
samux 1:fb4494783863 149 bool setProtocol(Protocol p);
samux 1:fb4494783863 150
WiredHome 15:121f473dae3f 151 /**
samux 1:fb4494783863 152 * Reset the wifi module
samux 1:fb4494783863 153 */
samux 1:fb4494783863 154 void reset();
samux 3:9aa05e19c62e 155
WiredHome 15:121f473dae3f 156 /**
samux 3:9aa05e19c62e 157 * Reboot the wifi module
WiredHome 51:a4831b1cb9a4 158 *
WiredHome 51:a4831b1cb9a4 159 * @return true if it could send the command, false otherwise
samux 3:9aa05e19c62e 160 */
samux 3:9aa05e19c62e 161 bool reboot();
samux 1:fb4494783863 162
WiredHome 15:121f473dae3f 163 /**
samux 1:fb4494783863 164 * Check if characters are available
samux 1:fb4494783863 165 *
samux 1:fb4494783863 166 * @return number of available characters
samux 1:fb4494783863 167 */
samux 1:fb4494783863 168 int readable();
samux 1:fb4494783863 169
WiredHome 15:121f473dae3f 170 /**
samux 1:fb4494783863 171 * Check if characters are available
samux 1:fb4494783863 172 *
samux 1:fb4494783863 173 * @return number of available characters
samux 1:fb4494783863 174 */
samux 1:fb4494783863 175 int writeable();
samux 1:fb4494783863 176
WiredHome 15:121f473dae3f 177 /**
WiredHome 64:6202428f37ec 178 * Check if associated with an access point (was checking if tcp link is active)
samux 1:fb4494783863 179 *
WiredHome 67:557ea7ad15c1 180 * Follow this example to improve the automatic recovery
WiredHome 67:557ea7ad15c1 181 * after a lost association.
WiredHome 67:557ea7ad15c1 182 *
WiredHome 67:557ea7ad15c1 183 * @code
WiredHome 67:557ea7ad15c1 184 * WiflyInterface eth(p28, p27, p23, p24, "ssid", "pass", WPA);
WiredHome 67:557ea7ad15c1 185 *
WiredHome 67:557ea7ad15c1 186 * if (0 == eth.init()) {
WiredHome 67:557ea7ad15c1 187 * eth.baud(230400); // speed up the interface
WiredHome 67:557ea7ad15c1 188 * do {
WiredHome 67:557ea7ad15c1 189 * if (0 == eth.connect()) {
WiredHome 67:557ea7ad15c1 190 * linkup = true; // led indicator
WiredHome 67:557ea7ad15c1 191 * while (eth.is_connected()) {
WiredHome 67:557ea7ad15c1 192 * wait_ms(1000);
WiredHome 67:557ea7ad15c1 193 * }
WiredHome 67:557ea7ad15c1 194 * linkup = false; // led indicator
WiredHome 67:557ea7ad15c1 195 * eth.disconnect();
WiredHome 67:557ea7ad15c1 196 * wait_ms(1000);
WiredHome 67:557ea7ad15c1 197 * }
WiredHome 67:557ea7ad15c1 198 * } while (1);
WiredHome 67:557ea7ad15c1 199 * } else {
WiredHome 67:557ea7ad15c1 200 * wait(5); // ... failed to initialize, rebooting...
WiredHome 67:557ea7ad15c1 201 * mbed_reset();
WiredHome 67:557ea7ad15c1 202 * }
WiredHome 67:557ea7ad15c1 203 * @endcode
WiredHome 67:557ea7ad15c1 204 *
WiredHome 51:a4831b1cb9a4 205 * @return true if successful
samux 1:fb4494783863 206 */
samux 1:fb4494783863 207 bool is_connected();
samux 1:fb4494783863 208
WiredHome 15:121f473dae3f 209 /**
samux 1:fb4494783863 210 * Read a character
samux 1:fb4494783863 211 *
samux 1:fb4494783863 212 * @return the character read
samux 1:fb4494783863 213 */
samux 1:fb4494783863 214 char getc();
samux 1:fb4494783863 215
WiredHome 15:121f473dae3f 216 /**
samux 1:fb4494783863 217 * Flush the buffer
samux 1:fb4494783863 218 */
samux 1:fb4494783863 219 void flush();
samux 1:fb4494783863 220
WiredHome 15:121f473dae3f 221 /**
samux 1:fb4494783863 222 * Write a character
samux 1:fb4494783863 223 *
samux 1:fb4494783863 224 * @param the character which will be written
WiredHome 51:a4831b1cb9a4 225 * @return the character written
samux 1:fb4494783863 226 */
samux 1:fb4494783863 227 int putc(char c);
samux 1:fb4494783863 228
samux 1:fb4494783863 229
WiredHome 15:121f473dae3f 230 /**
samux 1:fb4494783863 231 * To enter in command mode (we can configure the module)
samux 1:fb4494783863 232 *
samux 1:fb4494783863 233 * @return true if successful, false otherwise
samux 1:fb4494783863 234 */
samux 1:fb4494783863 235 bool cmdMode();
samux 1:fb4494783863 236
WiredHome 15:121f473dae3f 237 /**
samux 1:fb4494783863 238 * To exit the command mode
samux 1:fb4494783863 239 *
samux 1:fb4494783863 240 * @return true if successful, false otherwise
samux 1:fb4494783863 241 */
samux 1:fb4494783863 242 bool exit();
samux 1:fb4494783863 243
WiredHome 15:121f473dae3f 244 /**
WiredHome 34:52e4eec8b790 245 * Close a tcp connection, and exit command mode.
samux 1:fb4494783863 246 *
WiredHome 51:a4831b1cb9a4 247 * @return true if successful
samux 1:fb4494783863 248 */
samux 1:fb4494783863 249 bool close();
samux 1:fb4494783863 250
WiredHome 15:121f473dae3f 251 /**
WiredHome 70:83e420b0d3e1 252 * Send a string to the wifi module by serial port. This function desactivates
WiredHome 70:83e420b0d3e1 253 * the user interrupt handler when a character is received to analyze the response
WiredHome 70:83e420b0d3e1 254 * from the wifi module.
WiredHome 70:83e420b0d3e1 255 *
samux 1:fb4494783863 256 * Useful to send a command to the module and wait a response.
samux 1:fb4494783863 257 *
samux 1:fb4494783863 258 * @param str string to be sent
samux 1:fb4494783863 259 * @param len string length
WiredHome 70:83e420b0d3e1 260 * @param ACK string which must be acknowledge by the wifi module.
WiredHome 70:83e420b0d3e1 261 * If ACK == NULL, no string has to be acknowledged. (default: NULL)
WiredHome 70:83e420b0d3e1 262 * @param res this field will contain the response from the wifi module,
WiredHome 70:83e420b0d3e1 263 * result of a command sent. This field is available only
WiredHome 70:83e420b0d3e1 264 * if ACK == NULL AND res != NULL (default: NULL)
WiredHome 15:121f473dae3f 265 * @param timeout is the time in msec to wait for the acknowledge
samux 1:fb4494783863 266 *
WiredHome 70:83e420b0d3e1 267 * @return true if ACK has been found in the response from the wifi module.
WiredHome 70:83e420b0d3e1 268 * @return false if there is not a correct response in the timeout.
samux 1:fb4494783863 269 */
samux 1:fb4494783863 270 int send(const char * str, int len, const char * ACK = NULL, char * res = NULL, int timeout = DEFAULT_WAIT_RESP_TIMEOUT);
samux 1:fb4494783863 271
WiredHome 15:121f473dae3f 272 /**
WiredHome 70:83e420b0d3e1 273 * Put the device in command mode.
samux 1:fb4494783863 274 *
samux 1:fb4494783863 275 * @param str string to be sent
samux 1:fb4494783863 276 * @param ACK string which must be acknowledge by the wifi module. If ACK == NULL, no string has to be acknoledged. (default: "NO")
samux 1:fb4494783863 277 * @param res this field will contain the response from the wifi module, result of a command sent. This field is available only if ACK = "NO" AND res != NULL (default: NULL)
WiredHome 15:121f473dae3f 278 * @param timeout is the time in msec to wait for the acknowledge
samux 1:fb4494783863 279 *
WiredHome 70:83e420b0d3e1 280 * @return true if successful, false if it failed.
samux 1:fb4494783863 281 */
samux 1:fb4494783863 282 bool sendCommand(const char * cmd, const char * ack = NULL, char * res = NULL, int timeout = DEFAULT_WAIT_RESP_TIMEOUT);
samux 4:0bcec6272784 283
WiredHome 15:121f473dae3f 284 /**
samux 4:0bcec6272784 285 * Return true if the module is using dhcp
samux 4:0bcec6272784 286 *
WiredHome 51:a4831b1cb9a4 287 * @return true if the module is using dhcp
samux 4:0bcec6272784 288 */
samux 4:0bcec6272784 289 bool isDHCP() {
samux 4:0bcec6272784 290 return state.dhcp;
samux 4:0bcec6272784 291 }
samux 1:fb4494783863 292
WiredHome 15:121f473dae3f 293 /**
WiredHome 15:121f473dae3f 294 * gets the host ip address
WiredHome 15:121f473dae3f 295 *
WiredHome 15:121f473dae3f 296 * @param host is a pointer to the host string to look up
WiredHome 15:121f473dae3f 297 * @param ip contains the host IP in a string after the lookup.
WiredHome 15:121f473dae3f 298 * @param sizeof_ip is the size of the buffer pointed to by ip
WiredHome 51:a4831b1cb9a4 299 * @return true if successful
WiredHome 15:121f473dae3f 300 */
samux 1:fb4494783863 301 bool gethostbyname(const char * host, char * ip);
samux 1:fb4494783863 302
WiredHome 20:906b0f951bc3 303 /**
WiredHome 20:906b0f951bc3 304 * Set the baud rate between the ARM and the WiFly.
WiredHome 20:906b0f951bc3 305 *
WiredHome 20:906b0f951bc3 306 * This will set the WiFly module baud rate first and then
WiredHome 20:906b0f951bc3 307 * set the ARM interface to match it. If it cannot get the proper
WiredHome 20:906b0f951bc3 308 * acknowledge response, it will go on a hunt through the range
WiredHome 20:906b0f951bc3 309 * of standard baud rates.
WiredHome 20:906b0f951bc3 310 *
WiredHome 20:906b0f951bc3 311 * @note Baud rate must be one of 2400, 4800, 9600, 19200, 38400, 57600,
WiredHome 20:906b0f951bc3 312 * 115200, 230400, 460800, or 921600. (See Wifly manual 2.3.64)
WiredHome 20:906b0f951bc3 313 * @note Baud rate of 230400 has been seen to be marginal w/o flow control.
WiredHome 20:906b0f951bc3 314 * @note Setting a baud rate to a 460800 or above may be unrecoverable
WiredHome 20:906b0f951bc3 315 * without resetting the Wifly module.
WiredHome 20:906b0f951bc3 316 *
WiredHome 20:906b0f951bc3 317 * @param baudrate is the desired baudrate.
WiredHome 20:906b0f951bc3 318 *
WiredHome 51:a4831b1cb9a4 319 * @return true if it succeeded, which means that communications can continue,
WiredHome 51:a4831b1cb9a4 320 * @return false if it failed to establish a communication link.
WiredHome 20:906b0f951bc3 321 */
WiredHome 20:906b0f951bc3 322 bool baud(int baudrate);
WiredHome 20:906b0f951bc3 323
WiredHome 25:a740eb74a86a 324
WiredHome 25:a740eb74a86a 325 /**
WiredHome 25:a740eb74a86a 326 * Sets the connection state.
WiredHome 25:a740eb74a86a 327 *
WiredHome 25:a740eb74a86a 328 * Typically used by external apps that detect an incoming connection.
WiredHome 25:a740eb74a86a 329 *
WiredHome 25:a740eb74a86a 330 * @param state sets the connection state to true or false
WiredHome 25:a740eb74a86a 331 */
WiredHome 25:a740eb74a86a 332 void setConnectionState(bool state);
WiredHome 25:a740eb74a86a 333
WiredHome 26:78a1a09afdc9 334
WiredHome 25:a740eb74a86a 335 /**
WiredHome 25:a740eb74a86a 336 * Get the version information from the Wifly module.
WiredHome 25:a740eb74a86a 337 *
WiredHome 51:a4831b1cb9a4 338 * @return the version information as a string, or NULL
WiredHome 25:a740eb74a86a 339 */
WiredHome 25:a740eb74a86a 340 char * getWiflyVersionString();
WiredHome 25:a740eb74a86a 341
WiredHome 26:78a1a09afdc9 342
WiredHome 25:a740eb74a86a 343 /**
WiredHome 25:a740eb74a86a 344 * Get the software version from the Wifly module.
WiredHome 25:a740eb74a86a 345 *
WiredHome 25:a740eb74a86a 346 * This extracts the basic version number (e.g. 2.38, 4.00)
WiredHome 25:a740eb74a86a 347 * as a float.
WiredHome 25:a740eb74a86a 348 *
WiredHome 51:a4831b1cb9a4 349 * @return the software version number as a float.
WiredHome 25:a740eb74a86a 350 */
WiredHome 25:a740eb74a86a 351 float getWiflyVersion();
WiredHome 25:a740eb74a86a 352
WiredHome 58:f49aab8072ec 353 /**
WiredHome 58:f49aab8072ec 354 * Update the software in the Wifly module.
WiredHome 58:f49aab8072ec 355 *
WiredHome 58:f49aab8072ec 356 * This attempts to connect to the microchip site, download
WiredHome 58:f49aab8072ec 357 * a software update, install it as the primary image, and
WiredHome 58:f49aab8072ec 358 * reboot to activate that image. It is compile-time defined
WiredHome 58:f49aab8072ec 359 * to try for 30 seconds.
WiredHome 58:f49aab8072ec 360 *
WiredHome 73:59d0bbc4f905 361 * @param file is the name of the file to fetch, to update to.
WiredHome 73:59d0bbc4f905 362 * The strlen(file) cannot exceed 60, and is typically
WiredHome 73:59d0bbc4f905 363 * like this: "Wifly7-475.mif"
WiredHome 58:f49aab8072ec 364 * @return true if success or false for failure.
WiredHome 58:f49aab8072ec 365 */
WiredHome 73:59d0bbc4f905 366 bool SWUpdateWifly(const char * file);
WiredHome 58:f49aab8072ec 367
WiredHome 26:78a1a09afdc9 368 /**
WiredHome 26:78a1a09afdc9 369 * determine if the module is in command mode
WiredHome 26:78a1a09afdc9 370 *
WiredHome 26:78a1a09afdc9 371 * @return true if in command mode, false otherwise
WiredHome 26:78a1a09afdc9 372 */
WiredHome 26:78a1a09afdc9 373 bool isCmdMode() {
WiredHome 26:78a1a09afdc9 374 return state.cmd_mode;
WiredHome 26:78a1a09afdc9 375 }
WiredHome 26:78a1a09afdc9 376
samux 1:fb4494783863 377 static Wifly * getInstance() {
samux 1:fb4494783863 378 return inst;
samux 1:fb4494783863 379 };
samux 1:fb4494783863 380
samux 1:fb4494783863 381 protected:
WiredHome 51:a4831b1cb9a4 382 RawSerial wifi;
samux 1:fb4494783863 383 DigitalOut reset_pin;
samux 1:fb4494783863 384 DigitalIn tcp_status;
WiredHome 20:906b0f951bc3 385 int baudrate;
WiredHome 69:a4064f7e3529 386 char phrase[65];
WiredHome 69:a4064f7e3529 387 char ssid[33];
WiredHome 25:a740eb74a86a 388 char * wiflyVersionString;
WiredHome 25:a740eb74a86a 389 float swVersion;
samux 1:fb4494783863 390 const char * ip;
samux 1:fb4494783863 391 const char * netmask;
samux 1:fb4494783863 392 const char * gateway;
samux 1:fb4494783863 393 CircBuffer<char> buf_wifly;
samux 1:fb4494783863 394
samux 1:fb4494783863 395 static Wifly * inst;
samux 1:fb4494783863 396
samux 1:fb4494783863 397 void attach_rx(bool null);
samux 1:fb4494783863 398 void handler_rx(void);
WiredHome 29:49876a8c445b 399 void flushIn(int timeout_ms = -1);
WiredHome 64:6202428f37ec 400 uint16_t hextoi(char * p);
samux 1:fb4494783863 401
samux 1:fb4494783863 402 typedef struct STATE {
samux 1:fb4494783863 403 bool associated;
samux 1:fb4494783863 404 bool tcp;
samux 1:fb4494783863 405 bool dhcp;
samux 1:fb4494783863 406 Security sec;
samux 1:fb4494783863 407 Protocol proto;
samux 1:fb4494783863 408 bool cmd_mode;
samux 1:fb4494783863 409 } State;
samux 1:fb4494783863 410
samux 1:fb4494783863 411 State state;
samux 1:fb4494783863 412 char * getStringSecurity();
WiredHome 20:906b0f951bc3 413
WiredHome 24:5012a535b1d5 414 /**
WiredHome 24:5012a535b1d5 415 * Allocate space for the parameter (ssid or passphrase)
WiredHome 24:5012a535b1d5 416 * and then fix it (change ' ' to '$').
WiredHome 24:5012a535b1d5 417 *
WiredHome 24:5012a535b1d5 418 * @param dst is a reference to the private member pointer.
WiredHome 69:a4064f7e3529 419 * @param dstLen is the size of the destination buffer
WiredHome 69:a4064f7e3529 420 * @param src is a pointer to a passed in string.
WiredHome 69:a4064f7e3529 421 * @returns true if the src phrase was placed in the dst buffer.
WiredHome 24:5012a535b1d5 422 */
WiredHome 69:a4064f7e3529 423 bool FixPhrase(char * dst, size_t dstLen, const char * src);
WiredHome 24:5012a535b1d5 424
WiredHome 25:a740eb74a86a 425 /**
WiredHome 25:a740eb74a86a 426 * Gather the Wifly module version information for later queries
WiredHome 25:a740eb74a86a 427 */
WiredHome 25:a740eb74a86a 428 void GatherLogonInfo();
WiredHome 25:a740eb74a86a 429
samux 1:fb4494783863 430 };
samux 1:fb4494783863 431
samux 1:fb4494783863 432 #endif