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 May 02 00:10:34 2014 +0000
Revision:
69:a4064f7e3529
Parent:
68:d9a3920cc407
Child:
70:83e420b0d3e1
Child:
71:59936f711b80
Revised the ssid and passcode to be simple char arrays, simplifying the storage and management, but also meeting the required size.

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 /**
samux 1:fb4494783863 252 * Send a string to the wifi module by serial port. This function desactivates the user interrupt handler when a character is received to analyze the response from the wifi module.
samux 1:fb4494783863 253 * Useful to send a command to the module and wait a response.
samux 1:fb4494783863 254 *
samux 1:fb4494783863 255 *
samux 1:fb4494783863 256 * @param str string to be sent
samux 1:fb4494783863 257 * @param len string length
samux 1:fb4494783863 258 * @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 259 * @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 260 * @param timeout is the time in msec to wait for the acknowledge
samux 1:fb4494783863 261 *
samux 1:fb4494783863 262 * @return true if ACK has been found in the response from the wifi module. False otherwise or if there is no response in 5s.
samux 1:fb4494783863 263 */
samux 1:fb4494783863 264 int send(const char * str, int len, const char * ACK = NULL, char * res = NULL, int timeout = DEFAULT_WAIT_RESP_TIMEOUT);
samux 1:fb4494783863 265
WiredHome 15:121f473dae3f 266 /**
WiredHome 58:f49aab8072ec 267 * Send a command to the wify module. Check if the module is in command mode. If not enter in command mode
samux 1:fb4494783863 268 *
samux 1:fb4494783863 269 * @param str string to be sent
samux 1:fb4494783863 270 * @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 271 * @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 272 * @param timeout is the time in msec to wait for the acknowledge
samux 1:fb4494783863 273 *
WiredHome 51:a4831b1cb9a4 274 * @return true if successful
samux 1:fb4494783863 275 */
samux 1:fb4494783863 276 bool sendCommand(const char * cmd, const char * ack = NULL, char * res = NULL, int timeout = DEFAULT_WAIT_RESP_TIMEOUT);
samux 4:0bcec6272784 277
WiredHome 15:121f473dae3f 278 /**
samux 4:0bcec6272784 279 * Return true if the module is using dhcp
samux 4:0bcec6272784 280 *
WiredHome 51:a4831b1cb9a4 281 * @return true if the module is using dhcp
samux 4:0bcec6272784 282 */
samux 4:0bcec6272784 283 bool isDHCP() {
samux 4:0bcec6272784 284 return state.dhcp;
samux 4:0bcec6272784 285 }
samux 1:fb4494783863 286
WiredHome 15:121f473dae3f 287 /**
WiredHome 15:121f473dae3f 288 * gets the host ip address
WiredHome 15:121f473dae3f 289 *
WiredHome 15:121f473dae3f 290 * @param host is a pointer to the host string to look up
WiredHome 15:121f473dae3f 291 * @param ip contains the host IP in a string after the lookup.
WiredHome 15:121f473dae3f 292 * @param sizeof_ip is the size of the buffer pointed to by ip
WiredHome 51:a4831b1cb9a4 293 * @return true if successful
WiredHome 15:121f473dae3f 294 */
samux 1:fb4494783863 295 bool gethostbyname(const char * host, char * ip);
samux 1:fb4494783863 296
WiredHome 20:906b0f951bc3 297 /**
WiredHome 20:906b0f951bc3 298 * Set the baud rate between the ARM and the WiFly.
WiredHome 20:906b0f951bc3 299 *
WiredHome 20:906b0f951bc3 300 * This will set the WiFly module baud rate first and then
WiredHome 20:906b0f951bc3 301 * set the ARM interface to match it. If it cannot get the proper
WiredHome 20:906b0f951bc3 302 * acknowledge response, it will go on a hunt through the range
WiredHome 20:906b0f951bc3 303 * of standard baud rates.
WiredHome 20:906b0f951bc3 304 *
WiredHome 20:906b0f951bc3 305 * @note Baud rate must be one of 2400, 4800, 9600, 19200, 38400, 57600,
WiredHome 20:906b0f951bc3 306 * 115200, 230400, 460800, or 921600. (See Wifly manual 2.3.64)
WiredHome 20:906b0f951bc3 307 * @note Baud rate of 230400 has been seen to be marginal w/o flow control.
WiredHome 20:906b0f951bc3 308 * @note Setting a baud rate to a 460800 or above may be unrecoverable
WiredHome 20:906b0f951bc3 309 * without resetting the Wifly module.
WiredHome 20:906b0f951bc3 310 *
WiredHome 20:906b0f951bc3 311 * @param baudrate is the desired baudrate.
WiredHome 20:906b0f951bc3 312 *
WiredHome 51:a4831b1cb9a4 313 * @return true if it succeeded, which means that communications can continue,
WiredHome 51:a4831b1cb9a4 314 * @return false if it failed to establish a communication link.
WiredHome 20:906b0f951bc3 315 */
WiredHome 20:906b0f951bc3 316 bool baud(int baudrate);
WiredHome 20:906b0f951bc3 317
WiredHome 25:a740eb74a86a 318
WiredHome 25:a740eb74a86a 319 /**
WiredHome 25:a740eb74a86a 320 * Sets the connection state.
WiredHome 25:a740eb74a86a 321 *
WiredHome 25:a740eb74a86a 322 * Typically used by external apps that detect an incoming connection.
WiredHome 25:a740eb74a86a 323 *
WiredHome 25:a740eb74a86a 324 * @param state sets the connection state to true or false
WiredHome 25:a740eb74a86a 325 */
WiredHome 25:a740eb74a86a 326 void setConnectionState(bool state);
WiredHome 25:a740eb74a86a 327
WiredHome 26:78a1a09afdc9 328
WiredHome 25:a740eb74a86a 329 /**
WiredHome 25:a740eb74a86a 330 * Get the version information from the Wifly module.
WiredHome 25:a740eb74a86a 331 *
WiredHome 51:a4831b1cb9a4 332 * @return the version information as a string, or NULL
WiredHome 25:a740eb74a86a 333 */
WiredHome 25:a740eb74a86a 334 char * getWiflyVersionString();
WiredHome 25:a740eb74a86a 335
WiredHome 26:78a1a09afdc9 336
WiredHome 25:a740eb74a86a 337 /**
WiredHome 25:a740eb74a86a 338 * Get the software version from the Wifly module.
WiredHome 25:a740eb74a86a 339 *
WiredHome 25:a740eb74a86a 340 * This extracts the basic version number (e.g. 2.38, 4.00)
WiredHome 25:a740eb74a86a 341 * as a float.
WiredHome 25:a740eb74a86a 342 *
WiredHome 51:a4831b1cb9a4 343 * @return the software version number as a float.
WiredHome 25:a740eb74a86a 344 */
WiredHome 25:a740eb74a86a 345 float getWiflyVersion();
WiredHome 25:a740eb74a86a 346
WiredHome 58:f49aab8072ec 347 /**
WiredHome 58:f49aab8072ec 348 * Update the software in the Wifly module.
WiredHome 58:f49aab8072ec 349 *
WiredHome 58:f49aab8072ec 350 * This attempts to connect to the microchip site, download
WiredHome 58:f49aab8072ec 351 * a software update, install it as the primary image, and
WiredHome 58:f49aab8072ec 352 * reboot to activate that image. It is compile-time defined
WiredHome 58:f49aab8072ec 353 * to try for 30 seconds.
WiredHome 58:f49aab8072ec 354 *
WiredHome 58:f49aab8072ec 355 * @return true if success or false for failure.
WiredHome 58:f49aab8072ec 356 */
WiredHome 58:f49aab8072ec 357 bool SWUpdateWifly();
WiredHome 58:f49aab8072ec 358
WiredHome 26:78a1a09afdc9 359 /**
WiredHome 26:78a1a09afdc9 360 * determine if the module is in command mode
WiredHome 26:78a1a09afdc9 361 *
WiredHome 26:78a1a09afdc9 362 * @return true if in command mode, false otherwise
WiredHome 26:78a1a09afdc9 363 */
WiredHome 26:78a1a09afdc9 364 bool isCmdMode() {
WiredHome 26:78a1a09afdc9 365 return state.cmd_mode;
WiredHome 26:78a1a09afdc9 366 }
WiredHome 26:78a1a09afdc9 367
samux 1:fb4494783863 368 static Wifly * getInstance() {
samux 1:fb4494783863 369 return inst;
samux 1:fb4494783863 370 };
samux 1:fb4494783863 371
samux 1:fb4494783863 372 protected:
WiredHome 51:a4831b1cb9a4 373 RawSerial wifi;
samux 1:fb4494783863 374 DigitalOut reset_pin;
samux 1:fb4494783863 375 DigitalIn tcp_status;
WiredHome 20:906b0f951bc3 376 int baudrate;
WiredHome 69:a4064f7e3529 377 char phrase[65];
WiredHome 69:a4064f7e3529 378 char ssid[33];
WiredHome 25:a740eb74a86a 379 char * wiflyVersionString;
WiredHome 25:a740eb74a86a 380 float swVersion;
samux 1:fb4494783863 381 const char * ip;
samux 1:fb4494783863 382 const char * netmask;
samux 1:fb4494783863 383 const char * gateway;
samux 1:fb4494783863 384 CircBuffer<char> buf_wifly;
samux 1:fb4494783863 385
samux 1:fb4494783863 386 static Wifly * inst;
samux 1:fb4494783863 387
samux 1:fb4494783863 388 void attach_rx(bool null);
samux 1:fb4494783863 389 void handler_rx(void);
WiredHome 29:49876a8c445b 390 void flushIn(int timeout_ms = -1);
WiredHome 64:6202428f37ec 391 uint16_t hextoi(char * p);
samux 1:fb4494783863 392
samux 1:fb4494783863 393 typedef struct STATE {
samux 1:fb4494783863 394 bool associated;
samux 1:fb4494783863 395 bool tcp;
samux 1:fb4494783863 396 bool dhcp;
samux 1:fb4494783863 397 Security sec;
samux 1:fb4494783863 398 Protocol proto;
samux 1:fb4494783863 399 bool cmd_mode;
samux 1:fb4494783863 400 } State;
samux 1:fb4494783863 401
samux 1:fb4494783863 402 State state;
samux 1:fb4494783863 403 char * getStringSecurity();
WiredHome 20:906b0f951bc3 404
WiredHome 24:5012a535b1d5 405 /**
WiredHome 24:5012a535b1d5 406 * Allocate space for the parameter (ssid or passphrase)
WiredHome 24:5012a535b1d5 407 * and then fix it (change ' ' to '$').
WiredHome 24:5012a535b1d5 408 *
WiredHome 24:5012a535b1d5 409 * @param dst is a reference to the private member pointer.
WiredHome 69:a4064f7e3529 410 * @param dstLen is the size of the destination buffer
WiredHome 69:a4064f7e3529 411 * @param src is a pointer to a passed in string.
WiredHome 69:a4064f7e3529 412 * @returns true if the src phrase was placed in the dst buffer.
WiredHome 24:5012a535b1d5 413 */
WiredHome 69:a4064f7e3529 414 bool FixPhrase(char * dst, size_t dstLen, const char * src);
WiredHome 24:5012a535b1d5 415
WiredHome 25:a740eb74a86a 416 /**
WiredHome 25:a740eb74a86a 417 * Gather the Wifly module version information for later queries
WiredHome 25:a740eb74a86a 418 */
WiredHome 25:a740eb74a86a 419 void GatherLogonInfo();
WiredHome 25:a740eb74a86a 420
samux 1:fb4494783863 421 };
samux 1:fb4494783863 422
samux 1:fb4494783863 423 #endif