Bender Robotics / Mbed 2 deprecated PLAUCI_full

Dependencies:   FatFileSystemCpp mbed PowerControl USBHostLite

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