This is a fork specifically for seeed wifi shield

Dependents:   seeed_Wifi_HelloWorld Wifly_Xively_HelloWorld

Fork of WiflyInterface by mbed official

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     WPA2 = 4,
00040 };
00041 
00042 enum Protocol {
00043     UDP = (1 << 0),
00044     TCP = (1 << 1)
00045 };
00046 
00047 class Wifly
00048 {
00049 
00050 public:
00051     /*
00052     * Constructor
00053     *
00054     * @param tx mbed pin to use for tx line of Serial interface
00055     * @param rx mbed pin to use for rx line of Serial interface
00056     * @param reset reset pin of the wifi module ()
00057     * @param tcp_status connection status pin of the wifi module (GPIO 6)
00058     * @param ssid ssid of the network
00059     * @param phrase WEP or WPA key
00060     * @param sec Security type (NONE, WEP_128 or WPA)
00061     */
00062     Wifly(  PinName tx, PinName rx, PinName reset, PinName tcp_status, const char * ssid, const char * phrase, Security sec);
00063 
00064     /*
00065     * Connect the wifi module to the ssid contained in the constructor.
00066     *
00067     * @return true if connected, false otherwise
00068     */
00069     bool join();
00070 
00071     /*
00072     * Disconnect the wifly module from the access point
00073     *
00074     * @ returns true if successful
00075     */
00076     bool disconnect();
00077 
00078     /*
00079     * Open a tcp connection with the specified host on the specified port
00080     *
00081     * @param host host (can be either an ip address or a name. If a name is provided, a dns request will be established)
00082     * @param port port
00083     * @ returns true if successful
00084     */
00085     bool connect(const char * host, int port);
00086 
00087 
00088     /*
00089     * Set the protocol (UDP or TCP)
00090     *
00091     * @param p protocol
00092     * @ returns true if successful
00093     */
00094     bool setProtocol(Protocol p);
00095 
00096     /*
00097     * Reset the wifi module
00098     */
00099     void reset();
00100 
00101     /*
00102     * Check if characters are available
00103     *
00104     * @return number of available characters
00105     */
00106     int readable();
00107 
00108     /*
00109     * Check if characters are available
00110     *
00111     * @return number of available characters
00112     */
00113     int writeable();
00114 
00115     /*
00116     * Check if a tcp link is active
00117     *
00118     * @returns true if successful
00119     */
00120     bool is_connected();
00121 
00122     /*
00123     * Read a character
00124     *
00125     * @return the character read
00126     */
00127     char getc();
00128 
00129     /*
00130     * Flush the buffer
00131     */
00132     void flush();
00133 
00134     /*
00135     * Write a character
00136     *
00137     * @param the character which will be written
00138     */
00139     int putc(char c);
00140 
00141 
00142     /*
00143     * To enter in command mode (we can configure the module)
00144     *
00145     * @return true if successful, false otherwise
00146     */
00147     bool cmdMode();
00148 
00149     /*
00150     * To exit the command mode
00151     *
00152     * @return true if successful, false otherwise
00153     */
00154     bool exit();
00155 
00156     /*
00157     * Close a tcp connection
00158     *
00159     * @ returns true if successful
00160     */
00161     bool close();
00162 
00163     /*
00164     * 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.
00165     * Useful to send a command to the module and wait a response.
00166     *
00167     *
00168     * @param str string to be sent
00169     * @param len string length
00170     * @param ACK string which must be acknowledge by the wifi module. If ACK == NULL, no string has to be acknoledged. (default: "NO")
00171     * @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)
00172     *
00173     * @return true if ACK has been found in the response from the wifi module. False otherwise or if there is no response in 5s.
00174     */
00175     int send(const char * str, int len, const char * ACK = NULL, char * res = NULL, int timeout = DEFAULT_WAIT_RESP_TIMEOUT);
00176 
00177     /*
00178     * Send a command to the wify module. Check if the module is in command mode. If not enter in command mode
00179     *
00180     * @param str string to be sent
00181     * @param ACK string which must be acknowledge by the wifi module. If ACK == NULL, no string has to be acknoledged. (default: "NO")
00182     * @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)
00183     *
00184     * @returns true if successful
00185     */
00186     bool sendCommand(const char * cmd, const char * ack = NULL, char * res = NULL, int timeout = DEFAULT_WAIT_RESP_TIMEOUT);
00187 
00188     bool gethostbyname(const char * host, char * ip);
00189 
00190     static Wifly * getInstance() {
00191         return inst;
00192     };
00193 
00194 protected:
00195     Serial wifi;
00196     DigitalOut reset_pin;
00197     DigitalIn tcp_status;
00198     char phrase[30];
00199     char ssid[30];
00200     const char * ip;
00201     const char * netmask;
00202     const char * gateway;
00203     int channel;
00204     CircBuffer<char> buf_wifly;
00205 
00206     static Wifly * inst;
00207 
00208     void attach_rx(bool null);
00209     void handler_rx(void);
00210 
00211 
00212     typedef struct STATE {
00213         bool associated;
00214         bool tcp;
00215         bool dhcp;
00216         Security sec;
00217         Protocol proto;
00218         bool cmd_mode;
00219     } State;
00220 
00221     State state;
00222     char * getStringSecurity();
00223 };
00224 
00225 #endif