wifly/socket interface for wifly modules

Dependents:   WiFi neurGAI_WIFI thingspeak thingspeak2

Committer:
samux
Date:
Thu Aug 23 14:08:08 2012 +0000
Revision:
8:9a3cd07ed7e8
Parent:
7:a92dbed32890
Child:
11:b912f91e3212
add documentation

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samux 7:a92dbed32890 1 /* Copyright (C) 2012 mbed.org, MIT License
samux 7:a92dbed32890 2 *
samux 7:a92dbed32890 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
samux 7:a92dbed32890 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
samux 7:a92dbed32890 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
samux 7:a92dbed32890 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
samux 7:a92dbed32890 7 * furnished to do so, subject to the following conditions:
samux 7:a92dbed32890 8 *
samux 7:a92dbed32890 9 * The above copyright notice and this permission notice shall be included in all copies or
samux 7:a92dbed32890 10 * substantial portions of the Software.
samux 7:a92dbed32890 11 *
samux 7:a92dbed32890 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
samux 7:a92dbed32890 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
samux 7:a92dbed32890 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
samux 7:a92dbed32890 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
samux 7:a92dbed32890 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
samux 7:a92dbed32890 17 *
samux 7:a92dbed32890 18 * @section DESCRIPTION
samux 7:a92dbed32890 19 *
samux 7:a92dbed32890 20 * Wifly RN131-C, wifi module
samux 7:a92dbed32890 21 *
samux 7:a92dbed32890 22 * Datasheet:
samux 7:a92dbed32890 23 *
samux 7:a92dbed32890 24 * http://dlnmh9ip6v2uc.cloudfront.net/datasheets/Wireless/WiFi/WiFly-RN-UM.pdf
samux 7:a92dbed32890 25 */
samux 7:a92dbed32890 26
samux 7:a92dbed32890 27 #ifndef WIFLY_H
samux 7:a92dbed32890 28 #define WIFLY_H
samux 7:a92dbed32890 29
samux 7:a92dbed32890 30 #include "mbed.h"
samux 7:a92dbed32890 31 #include "CBuffer.h"
samux 7:a92dbed32890 32
samux 7:a92dbed32890 33 #define DEFAULT_WAIT_RESP_TIMEOUT 500
samux 7:a92dbed32890 34
samux 7:a92dbed32890 35 enum Security {
samux 7:a92dbed32890 36 NONE = 0,
samux 7:a92dbed32890 37 WEP_128 = 1,
samux 7:a92dbed32890 38 WPA = 3
samux 7:a92dbed32890 39 };
samux 7:a92dbed32890 40
samux 7:a92dbed32890 41 class Wifly {
samux 7:a92dbed32890 42
samux 7:a92dbed32890 43 public:
samux 7:a92dbed32890 44 /*
samux 8:9a3cd07ed7e8 45 * Constructor
samux 7:a92dbed32890 46 *
samux 7:a92dbed32890 47 * @param tx mbed pin to use for tx line of Serial interface
samux 7:a92dbed32890 48 * @param rx mbed pin to use for rx line of Serial interface
samux 7:a92dbed32890 49 * @param reset reset pin of the wifi module ()
samux 7:a92dbed32890 50 * @param tcp_status connection status pin of the wifi module (GPIO 6)
samux 7:a92dbed32890 51 * @param ssid ssid of the network
samux 7:a92dbed32890 52 * @param phrase WEP or WPA key
samux 8:9a3cd07ed7e8 53 * @param sec Security type (NONE, WEP_128 or WPA)
samux 7:a92dbed32890 54 */
samux 7:a92dbed32890 55 Wifly( PinName tx, PinName rx, PinName reset, PinName tcp_status, const char * ssid, const char * phrase, Security sec);
samux 7:a92dbed32890 56
samux 7:a92dbed32890 57 /*
samux 7:a92dbed32890 58 * 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 7:a92dbed32890 59 * Useful to send a command to the module and wait a response.
samux 7:a92dbed32890 60 *
samux 7:a92dbed32890 61 *
samux 7:a92dbed32890 62 * @param str string to be sent
samux 7:a92dbed32890 63 * @param len string length
samux 7:a92dbed32890 64 * @param ACK string which must be acknowledge by the wifi module. If ACK == NULL, no string has to be acknoledged. (default: "NO")
samux 7:a92dbed32890 65 * @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)
samux 7:a92dbed32890 66 *
samux 7:a92dbed32890 67 * @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 7:a92dbed32890 68 */
samux 7:a92dbed32890 69 int send(const char * str, int len, const char * ACK = NULL, char * res = NULL, int timeout = DEFAULT_WAIT_RESP_TIMEOUT);
samux 7:a92dbed32890 70
samux 7:a92dbed32890 71 /*
samux 7:a92dbed32890 72 * Connect the wifi module to the ssid contained in the constructor.
samux 7:a92dbed32890 73 *
samux 7:a92dbed32890 74 * @return true if connected, false otherwise
samux 7:a92dbed32890 75 */
samux 7:a92dbed32890 76 bool join();
samux 7:a92dbed32890 77
samux 7:a92dbed32890 78 /*
samux 7:a92dbed32890 79 * Close a connection with the access point
samux 7:a92dbed32890 80 *
samux 7:a92dbed32890 81 * @ returns true if successful
samux 7:a92dbed32890 82 */
samux 7:a92dbed32890 83 bool leave();
samux 7:a92dbed32890 84
samux 7:a92dbed32890 85 /*
samux 7:a92dbed32890 86 * Read a string if available
samux 7:a92dbed32890 87 *
samux 7:a92dbed32890 88 *@param str pointer where will be stored the string read
samux 7:a92dbed32890 89 */
samux 7:a92dbed32890 90 bool read(char * str);
samux 7:a92dbed32890 91
samux 7:a92dbed32890 92 /*
samux 7:a92dbed32890 93 * Reset the wifi module
samux 7:a92dbed32890 94 */
samux 7:a92dbed32890 95 void reset();
samux 7:a92dbed32890 96
samux 7:a92dbed32890 97 /*
samux 7:a92dbed32890 98 * Check if characters are available
samux 7:a92dbed32890 99 *
samux 7:a92dbed32890 100 * @return number of available characters
samux 7:a92dbed32890 101 */
samux 7:a92dbed32890 102 int readable();
samux 7:a92dbed32890 103
samux 7:a92dbed32890 104 /*
samux 7:a92dbed32890 105 * Check if characters are available
samux 7:a92dbed32890 106 *
samux 7:a92dbed32890 107 * @return number of available characters
samux 7:a92dbed32890 108 */
samux 7:a92dbed32890 109 int writeable();
samux 7:a92dbed32890 110
samux 7:a92dbed32890 111 /*
samux 7:a92dbed32890 112 * Check if a tcp link is active
samux 7:a92dbed32890 113 *
samux 7:a92dbed32890 114 * @returns true if successful
samux 7:a92dbed32890 115 */
samux 7:a92dbed32890 116 bool is_connected();
samux 7:a92dbed32890 117
samux 7:a92dbed32890 118 /*
samux 7:a92dbed32890 119 * Read a character
samux 7:a92dbed32890 120 *
samux 7:a92dbed32890 121 * @return the character read
samux 7:a92dbed32890 122 */
samux 7:a92dbed32890 123 char getc();
samux 7:a92dbed32890 124
samux 7:a92dbed32890 125 /*
samux 7:a92dbed32890 126 * Flush the buffer
samux 7:a92dbed32890 127 */
samux 7:a92dbed32890 128 void flush();
samux 7:a92dbed32890 129
samux 7:a92dbed32890 130 /*
samux 7:a92dbed32890 131 * Write a character
samux 7:a92dbed32890 132 *
samux 7:a92dbed32890 133 * @param the character which will be written
samux 7:a92dbed32890 134 */
samux 7:a92dbed32890 135 int putc(char c);
samux 7:a92dbed32890 136
samux 7:a92dbed32890 137
samux 7:a92dbed32890 138 /*
samux 7:a92dbed32890 139 * To enter in command mode (we can configure the module)
samux 7:a92dbed32890 140 *
samux 7:a92dbed32890 141 * @return true if successful, false otherwise
samux 7:a92dbed32890 142 */
samux 7:a92dbed32890 143 bool cmdMode();
samux 7:a92dbed32890 144
samux 7:a92dbed32890 145 /*
samux 7:a92dbed32890 146 * To exit the command mode
samux 7:a92dbed32890 147 *
samux 7:a92dbed32890 148 * @return true if successful, false otherwise
samux 7:a92dbed32890 149 */
samux 7:a92dbed32890 150 bool exit();
samux 7:a92dbed32890 151
samux 7:a92dbed32890 152 /*
samux 7:a92dbed32890 153 * Close a tcp connection
samux 7:a92dbed32890 154 *
samux 7:a92dbed32890 155 * @ returns true if successful
samux 7:a92dbed32890 156 */
samux 7:a92dbed32890 157 bool close();
samux 7:a92dbed32890 158
samux 7:a92dbed32890 159 /*
samux 7:a92dbed32890 160 * Send a command to the wify module. Check if the module is in command mode. If not enter in command mode
samux 7:a92dbed32890 161 *
samux 7:a92dbed32890 162 * @param str string to be sent
samux 7:a92dbed32890 163 * @param ACK string which must be acknowledge by the wifi module. If ACK == NULL, no string has to be acknoledged. (default: "NO")
samux 7:a92dbed32890 164 * @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)
samux 7:a92dbed32890 165 *
samux 7:a92dbed32890 166 * @returns true if successful
samux 7:a92dbed32890 167 */
samux 7:a92dbed32890 168 bool sendCommand(const char * cmd, const char * ack = NULL, char * res = NULL, int timeout = DEFAULT_WAIT_RESP_TIMEOUT);
samux 7:a92dbed32890 169
samux 7:a92dbed32890 170 bool dnsLookup(const char * host, char * ip);
samux 7:a92dbed32890 171
samux 7:a92dbed32890 172 static Wifly * getInstance() {return inst;};
samux 7:a92dbed32890 173
samux 7:a92dbed32890 174 protected:
samux 7:a92dbed32890 175 Serial wifi;
samux 7:a92dbed32890 176 DigitalOut reset_pin;
samux 7:a92dbed32890 177 DigitalIn tcp_status;
samux 7:a92dbed32890 178 bool wpa;
samux 7:a92dbed32890 179 bool dhcp;
samux 7:a92dbed32890 180 char phrase[30];
samux 7:a92dbed32890 181 char ssid[30];
samux 7:a92dbed32890 182 const char * ip;
samux 7:a92dbed32890 183 const char * netmask;
samux 7:a92dbed32890 184 const char * gateway;
samux 7:a92dbed32890 185 int channel;
samux 7:a92dbed32890 186 CircBuffer<char> buf_wifly;
samux 7:a92dbed32890 187 Security security;
samux 7:a92dbed32890 188 char * getStringSecurity();
samux 7:a92dbed32890 189
samux 7:a92dbed32890 190 bool cmd_mode;
samux 7:a92dbed32890 191
samux 7:a92dbed32890 192 static Wifly * inst;
samux 7:a92dbed32890 193
samux 7:a92dbed32890 194 void attach_rx(bool null);
samux 7:a92dbed32890 195 void handler_rx(void);
samux 7:a92dbed32890 196 };
samux 7:a92dbed32890 197
samux 0:6ffb0aeb3972 198 #endif