wifly/socket interface for wifly modules

Dependents:   WiFi neurGAI_WIFI thingspeak thingspeak2

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Wifly.h Source File

Wifly.h

00001 /* Copyright (C) 2012 mbed.org, MIT License
00002  *
00003  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00004  * and associated documentation files (the "Software"), to deal in the Software without restriction,
00005  * including without limitation the rights to use, copy, modify, merge, publish, distribute,
00006  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
00007  * furnished to do so, subject to the following conditions:
00008  *
00009  * The above copyright notice and this permission notice shall be included in all copies or
00010  * substantial portions of the Software.
00011  *
00012  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00013  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00014  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00015  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00016  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017  *
00018  * @section DESCRIPTION
00019  *
00020  * Wifly RN131-C, wifi module
00021  *
00022  * Datasheet:
00023  *
00024  * http://dlnmh9ip6v2uc.cloudfront.net/datasheets/Wireless/WiFi/WiFly-RN-UM.pdf
00025  */
00026 
00027 #ifndef WIFLY_H
00028 #define WIFLY_H
00029 
00030 #include "mbed.h"
00031 #include "CBuffer.h"
00032 
00033 #define DEFAULT_WAIT_RESP_TIMEOUT 500
00034 
00035 enum Security {
00036     NONE = 0,
00037     WEP_128 = 1,
00038     WPA = 3
00039 };
00040 
00041 enum Protocol {
00042     UDP = (1 << 0),
00043     TCP = (1 << 1)
00044 };
00045 
00046 class Wifly
00047 {
00048 
00049 public:
00050     /*
00051     * Constructor
00052     *
00053     * @param tx mbed pin to use for tx line of Serial interface
00054     * @param rx mbed pin to use for rx line of Serial interface
00055     * @param reset reset pin of the wifi module ()
00056     * @param tcp_status connection status pin of the wifi module (GPIO 6)
00057     * @param ssid ssid of the network
00058     * @param phrase WEP or WPA key
00059     * @param sec Security type (NONE, WEP_128 or WPA)
00060     */
00061     Wifly(  PinName tx, PinName rx, PinName reset, PinName tcp_status, const char * ssid, const char * phrase, Security sec);
00062 
00063     /*
00064     * Connect the wifi module to the ssid contained in the constructor.
00065     *
00066     * @return true if connected, false otherwise
00067     */
00068     bool join();
00069 
00070     /*
00071     * Disconnect the wifly module from the access point
00072     *
00073     * @ returns true if successful
00074     */
00075     bool disconnect();
00076 
00077     /*
00078     * Open a tcp connection with the specified host on the specified port
00079     *
00080     * @param host host (can be either an ip address or a name. If a name is provided, a dns request will be established)
00081     * @param port port
00082     * @ returns true if successful
00083     */
00084     bool connect(const char * host, int port);
00085 
00086 
00087     /*
00088     * Set the protocol (UDP or TCP)
00089     *
00090     * @param p protocol
00091     * @ returns true if successful
00092     */
00093     bool setProtocol(Protocol p);
00094 
00095     /*
00096     * Reset the wifi module
00097     */
00098     void reset();
00099 
00100     /*
00101     * Check if characters are available
00102     *
00103     * @return number of available characters
00104     */
00105     int readable();
00106 
00107     /*
00108     * Check if characters are available
00109     *
00110     * @return number of available characters
00111     */
00112     int writeable();
00113 
00114     /*
00115     * Check if a tcp link is active
00116     *
00117     * @returns true if successful
00118     */
00119     bool is_connected();
00120 
00121     /*
00122     * Read a character
00123     *
00124     * @return the character read
00125     */
00126     char getc();
00127 
00128     /*
00129     * Flush the buffer
00130     */
00131     void flush();
00132 
00133     /*
00134     * Write a character
00135     *
00136     * @param the character which will be written
00137     */
00138     int putc(char c);
00139 
00140 
00141     /*
00142     * To enter in command mode (we can configure the module)
00143     *
00144     * @return true if successful, false otherwise
00145     */
00146     bool cmdMode();
00147 
00148     /*
00149     * To exit the command mode
00150     *
00151     * @return true if successful, false otherwise
00152     */
00153     bool exit();
00154 
00155     /*
00156     * Close a tcp connection
00157     *
00158     * @ returns true if successful
00159     */
00160     bool close();
00161 
00162     /*
00163     * 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.
00164     * Useful to send a command to the module and wait a response.
00165     *
00166     *
00167     * @param str string to be sent
00168     * @param len string length
00169     * @param ACK string which must be acknowledge by the wifi module. If ACK == NULL, no string has to be acknoledged. (default: "NO")
00170     * @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)
00171     *
00172     * @return true if ACK has been found in the response from the wifi module. False otherwise or if there is no response in 5s.
00173     */
00174     int send(const char * str, int len, const char * ACK = NULL, char * res = NULL, int timeout = DEFAULT_WAIT_RESP_TIMEOUT);
00175 
00176     /*
00177     * Send a command to the wify module. Check if the module is in command mode. If not enter in command mode
00178     *
00179     * @param str string to be sent
00180     * @param ACK string which must be acknowledge by the wifi module. If ACK == NULL, no string has to be acknoledged. (default: "NO")
00181     * @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)
00182     *
00183     * @returns true if successful
00184     */
00185     bool sendCommand(const char * cmd, const char * ack = NULL, char * res = NULL, int timeout = DEFAULT_WAIT_RESP_TIMEOUT);
00186 
00187     bool gethostbyname(const char * host, char * ip);
00188 
00189     static Wifly * getInstance() {
00190         return inst;
00191     };
00192 
00193 protected:
00194     Serial wifi;
00195     DigitalOut reset_pin;
00196     DigitalIn tcp_status;
00197     char phrase[30];
00198     char ssid[30];
00199     const char * ip;
00200     const char * netmask;
00201     const char * gateway;
00202     int channel;
00203     CircBuffer<char> buf_wifly;
00204 
00205     static Wifly * inst;
00206 
00207     void attach_rx(bool null);
00208     void handler_rx(void);
00209 
00210 
00211     typedef struct STATE {
00212         bool associated;
00213         bool tcp;
00214         bool dhcp;
00215         Security sec;
00216         Protocol proto;
00217         bool cmd_mode;
00218     } State;
00219 
00220     State state;
00221     char * getStringSecurity();
00222 };
00223 
00224 #endif