wifly/socket interface for wifly modules

Dependents:   WiFi neurGAI_WIFI thingspeak thingspeak2

Committer:
samux
Date:
Thu Aug 23 10:37:27 2012 +0000
Revision:
5:8e253731f76f
Parent:
4:74bfdd00362a
Child:
7:a92dbed32890
support whitespace in ssid and passphrase

Who changed what in which revision?

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