Forked mbed official WiflyInterface (interface for Roving Networks Wifly modules) which includes the possibility to use TCPSocketServer::accept as a non-blocking cal.

Dependents:   WiFlyHTTPServerSample MultiThreadingHTTPServer

Fork of WiflyInterface by mbed official

Committer:
leihen
Date:
Thu Jun 27 00:31:02 2013 +0000
Revision:
12:4f95e6a365db
Parent:
11:63a7dc9e45dd
Logger working locally
;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samux 1:fb4494783863 1 /* Copyright (C) 2012 mbed.org, MIT License
samux 1:fb4494783863 2 *
samux 1:fb4494783863 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
samux 1:fb4494783863 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
samux 1:fb4494783863 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
samux 1:fb4494783863 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
samux 1:fb4494783863 7 * furnished to do so, subject to the following conditions:
samux 1:fb4494783863 8 *
samux 1:fb4494783863 9 * The above copyright notice and this permission notice shall be included in all copies or
samux 1:fb4494783863 10 * substantial portions of the Software.
samux 1:fb4494783863 11 *
samux 1:fb4494783863 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
samux 1:fb4494783863 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
samux 1:fb4494783863 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
samux 1:fb4494783863 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
samux 1:fb4494783863 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
samux 1:fb4494783863 17 *
samux 1:fb4494783863 18 * @section DESCRIPTION
samux 1:fb4494783863 19 *
samux 1:fb4494783863 20 * Wifly RN131-C, wifi module
samux 1:fb4494783863 21 *
samux 1:fb4494783863 22 * Datasheet:
samux 1:fb4494783863 23 *
samux 1:fb4494783863 24 * http://dlnmh9ip6v2uc.cloudfront.net/datasheets/Wireless/WiFi/WiFly-RN-UM.pdf
samux 1:fb4494783863 25 */
samux 1:fb4494783863 26
samux 1:fb4494783863 27 #ifndef WIFLY_H
samux 1:fb4494783863 28 #define WIFLY_H
samux 1:fb4494783863 29
samux 1:fb4494783863 30 #include "mbed.h"
samux 1:fb4494783863 31 #include "CBuffer.h"
leihen 5:48d55083d2ff 32 #include <string>
leihen 11:63a7dc9e45dd 33 #include "rtos.h"
samux 1:fb4494783863 34
leihen 5:48d55083d2ff 35 #define DEFAULT_WAIT_RESP_TIMEOUT 800
samux 1:fb4494783863 36
leihen 11:63a7dc9e45dd 37
samux 1:fb4494783863 38 enum Security {
samux 1:fb4494783863 39 NONE = 0,
samux 1:fb4494783863 40 WEP_128 = 1,
samux 1:fb4494783863 41 WPA = 3
samux 1:fb4494783863 42 };
samux 1:fb4494783863 43
samux 1:fb4494783863 44 enum Protocol {
samux 1:fb4494783863 45 UDP = (1 << 0),
samux 1:fb4494783863 46 TCP = (1 << 1)
samux 1:fb4494783863 47 };
samux 1:fb4494783863 48
samux 1:fb4494783863 49 class Wifly
samux 1:fb4494783863 50 {
leihen 7:e42b7fa7ef70 51 public:
leihen 7:e42b7fa7ef70 52 typedef enum
leihen 7:e42b7fa7ef70 53 {
leihen 7:e42b7fa7ef70 54 Wifly_2400,
leihen 7:e42b7fa7ef70 55 Wifly_4800,
leihen 7:e42b7fa7ef70 56 Wifly_9600,
leihen 7:e42b7fa7ef70 57 Wifly_19200,
leihen 7:e42b7fa7ef70 58 Wifly_38400,
leihen 7:e42b7fa7ef70 59 Wifly_57600,
leihen 7:e42b7fa7ef70 60 Wifly_115200,
leihen 7:e42b7fa7ef70 61 Wifly_230400,
leihen 7:e42b7fa7ef70 62 Wifly_460800,
leihen 7:e42b7fa7ef70 63 Wifly_921600
leihen 7:e42b7fa7ef70 64 } WiflyBaudrate_t;
samux 1:fb4494783863 65
leihen 9:4f6f2f35a21a 66
samux 1:fb4494783863 67 public:
samux 1:fb4494783863 68 /*
samux 1:fb4494783863 69 * Constructor
samux 1:fb4494783863 70 *
samux 1:fb4494783863 71 * @param tx mbed pin to use for tx line of Serial interface
samux 1:fb4494783863 72 * @param rx mbed pin to use for rx line of Serial interface
samux 1:fb4494783863 73 * @param reset reset pin of the wifi module ()
samux 1:fb4494783863 74 * @param tcp_status connection status pin of the wifi module (GPIO 6)
samux 1:fb4494783863 75 * @param ssid ssid of the network
samux 1:fb4494783863 76 * @param phrase WEP or WPA key
samux 1:fb4494783863 77 * @param sec Security type (NONE, WEP_128 or WPA)
samux 1:fb4494783863 78 */
leihen 7:e42b7fa7ef70 79 Wifly( PinName tx, PinName rx, PinName reset, PinName tcp_status, const char * ssid, const char * phrase, Security sec, WiflyBaudrate_t baud = Wifly_115200);
samux 1:fb4494783863 80
samux 1:fb4494783863 81 /*
samux 1:fb4494783863 82 * Connect the wifi module to the ssid contained in the constructor.
samux 1:fb4494783863 83 *
samux 1:fb4494783863 84 * @return true if connected, false otherwise
samux 1:fb4494783863 85 */
leihen 11:63a7dc9e45dd 86 virtual bool join();
samux 1:fb4494783863 87
samux 1:fb4494783863 88 /*
samux 1:fb4494783863 89 * Disconnect the wifly module from the access point
samux 1:fb4494783863 90 *
samux 1:fb4494783863 91 * @ returns true if successful
samux 1:fb4494783863 92 */
samux 1:fb4494783863 93 bool disconnect();
samux 1:fb4494783863 94
samux 1:fb4494783863 95 /*
samux 1:fb4494783863 96 * Open a tcp connection with the specified host on the specified port
samux 1:fb4494783863 97 *
samux 1:fb4494783863 98 * @param host host (can be either an ip address or a name. If a name is provided, a dns request will be established)
samux 1:fb4494783863 99 * @param port port
samux 1:fb4494783863 100 * @ returns true if successful
samux 1:fb4494783863 101 */
samux 1:fb4494783863 102 bool connect(const char * host, int port);
samux 1:fb4494783863 103
samux 1:fb4494783863 104
samux 1:fb4494783863 105 /*
samux 1:fb4494783863 106 * Set the protocol (UDP or TCP)
samux 1:fb4494783863 107 *
samux 1:fb4494783863 108 * @param p protocol
samux 1:fb4494783863 109 * @ returns true if successful
samux 1:fb4494783863 110 */
samux 1:fb4494783863 111 bool setProtocol(Protocol p);
samux 1:fb4494783863 112
samux 1:fb4494783863 113 /*
samux 1:fb4494783863 114 * Reset the wifi module
samux 1:fb4494783863 115 */
samux 1:fb4494783863 116 void reset();
samux 3:9aa05e19c62e 117
samux 3:9aa05e19c62e 118 /*
samux 3:9aa05e19c62e 119 * Reboot the wifi module
samux 3:9aa05e19c62e 120 */
samux 3:9aa05e19c62e 121 bool reboot();
samux 1:fb4494783863 122
samux 1:fb4494783863 123 /*
samux 1:fb4494783863 124 * Check if characters are available
samux 1:fb4494783863 125 *
samux 1:fb4494783863 126 * @return number of available characters
samux 1:fb4494783863 127 */
samux 1:fb4494783863 128 int readable();
samux 1:fb4494783863 129
samux 1:fb4494783863 130 /*
samux 1:fb4494783863 131 * Check if characters are available
samux 1:fb4494783863 132 *
samux 1:fb4494783863 133 * @return number of available characters
samux 1:fb4494783863 134 */
samux 1:fb4494783863 135 int writeable();
samux 1:fb4494783863 136
samux 1:fb4494783863 137 /*
samux 1:fb4494783863 138 * Check if a tcp link is active
samux 1:fb4494783863 139 *
samux 1:fb4494783863 140 * @returns true if successful
samux 1:fb4494783863 141 */
samux 1:fb4494783863 142 bool is_connected();
samux 1:fb4494783863 143
samux 1:fb4494783863 144 /*
samux 1:fb4494783863 145 * Read a character
samux 1:fb4494783863 146 *
samux 1:fb4494783863 147 * @return the character read
samux 1:fb4494783863 148 */
samux 1:fb4494783863 149 char getc();
samux 1:fb4494783863 150
samux 1:fb4494783863 151 /*
leihen 9:4f6f2f35a21a 152 * Peek a character
leihen 9:4f6f2f35a21a 153 *
leihen 9:4f6f2f35a21a 154 * @return the character but don't remove it from the queue
leihen 9:4f6f2f35a21a 155 */
leihen 9:4f6f2f35a21a 156 char peek();
leihen 9:4f6f2f35a21a 157
leihen 9:4f6f2f35a21a 158 /*
samux 1:fb4494783863 159 * Flush the buffer
samux 1:fb4494783863 160 */
samux 1:fb4494783863 161 void flush();
samux 1:fb4494783863 162
samux 1:fb4494783863 163 /*
samux 1:fb4494783863 164 * Write a character
samux 1:fb4494783863 165 *
samux 1:fb4494783863 166 * @param the character which will be written
samux 1:fb4494783863 167 */
samux 1:fb4494783863 168 int putc(char c);
samux 1:fb4494783863 169
samux 1:fb4494783863 170
samux 1:fb4494783863 171 /*
samux 1:fb4494783863 172 * To enter in command mode (we can configure the module)
samux 1:fb4494783863 173 *
samux 1:fb4494783863 174 * @return true if successful, false otherwise
samux 1:fb4494783863 175 */
samux 1:fb4494783863 176 bool cmdMode();
samux 1:fb4494783863 177
samux 1:fb4494783863 178 /*
samux 1:fb4494783863 179 * To exit the command mode
samux 1:fb4494783863 180 *
samux 1:fb4494783863 181 * @return true if successful, false otherwise
samux 1:fb4494783863 182 */
leihen 9:4f6f2f35a21a 183 bool exit(bool flush = true);
samux 1:fb4494783863 184
samux 1:fb4494783863 185 /*
samux 1:fb4494783863 186 * Close a tcp connection
samux 1:fb4494783863 187 *
samux 1:fb4494783863 188 * @ returns true if successful
samux 1:fb4494783863 189 */
samux 1:fb4494783863 190 bool close();
samux 1:fb4494783863 191
samux 1:fb4494783863 192 /*
samux 1:fb4494783863 193 * 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 1:fb4494783863 194 * Useful to send a command to the module and wait a response.
samux 1:fb4494783863 195 *
samux 1:fb4494783863 196 *
samux 1:fb4494783863 197 * @param str string to be sent
samux 1:fb4494783863 198 * @param len string length
samux 1:fb4494783863 199 * @param ACK string which must be acknowledge by the wifi module. If ACK == NULL, no string has to be acknoledged. (default: "NO")
samux 1:fb4494783863 200 * @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 1:fb4494783863 201 *
samux 1:fb4494783863 202 * @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 1:fb4494783863 203 */
leihen 11:63a7dc9e45dd 204 virtual int send(const char * str, int len, const char * ACK = NULL, char * res = NULL, int timeout = DEFAULT_WAIT_RESP_TIMEOUT);
samux 1:fb4494783863 205
leihen 11:63a7dc9e45dd 206 virtual int sendData(const char* str, int len, int timeout = DEFAULT_WAIT_RESP_TIMEOUT);
leihen 11:63a7dc9e45dd 207
samux 1:fb4494783863 208 /*
samux 1:fb4494783863 209 * Send a command to the wify module. Check if the module is in command mode. If not enter in command mode
samux 1:fb4494783863 210 *
samux 1:fb4494783863 211 * @param str string to be sent
samux 1:fb4494783863 212 * @param ACK string which must be acknowledge by the wifi module. If ACK == NULL, no string has to be acknoledged. (default: "NO")
samux 1:fb4494783863 213 * @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 1:fb4494783863 214 *
samux 1:fb4494783863 215 * @returns true if successful
samux 1:fb4494783863 216 */
samux 1:fb4494783863 217 bool sendCommand(const char * cmd, const char * ack = NULL, char * res = NULL, int timeout = DEFAULT_WAIT_RESP_TIMEOUT);
samux 4:0bcec6272784 218
samux 4:0bcec6272784 219 /*
samux 4:0bcec6272784 220 * Return true if the module is using dhcp
samux 4:0bcec6272784 221 *
samux 4:0bcec6272784 222 * @returns true if the module is using dhcp
samux 4:0bcec6272784 223 */
samux 4:0bcec6272784 224 bool isDHCP() {
samux 4:0bcec6272784 225 return state.dhcp;
samux 4:0bcec6272784 226 }
leihen 5:48d55083d2ff 227
leihen 5:48d55083d2ff 228 /*
leihen 5:48d55083d2ff 229 * Enable Network time and RTC functionality
leihen 5:48d55083d2ff 230 *
leihen 5:48d55083d2ff 231 * @param minutes : frequency in minutes with which to query the NTP, or 0 to disable, or 1 to query just once after power-up
leihen 5:48d55083d2ff 232 *
leihen 5:48d55083d2ff 233 * @returns 0 if successful, -1 otherwise
leihen 5:48d55083d2ff 234 */
leihen 5:48d55083d2ff 235 int enableTime(int minutes, const char* ntp_address = "65.55.56.206");
leihen 5:48d55083d2ff 236
leihen 5:48d55083d2ff 237 /** Get the time from the Real Time Clock
leihen 5:48d55083d2ff 238 *
leihen 5:48d55083d2ff 239 * @ param uptime : if true, returns the uptime (time since last power-up. Returns the Real Time Clock's time otherwise
leihen 5:48d55083d2ff 240 *
leihen 5:48d55083d2ff 241 * @ returns the time from RTC
leihen 5:48d55083d2ff 242 */
leihen 5:48d55083d2ff 243 std::string getTime(bool uptime);
leihen 7:e42b7fa7ef70 244
samux 1:fb4494783863 245 bool gethostbyname(const char * host, char * ip);
samux 1:fb4494783863 246
samux 1:fb4494783863 247 static Wifly * getInstance() {
samux 1:fb4494783863 248 return inst;
samux 1:fb4494783863 249 };
samux 1:fb4494783863 250
samux 1:fb4494783863 251 protected:
leihen 7:e42b7fa7ef70 252
leihen 7:e42b7fa7ef70 253 /** Set the baud rate for communication
leihen 7:e42b7fa7ef70 254 *
leihen 7:e42b7fa7ef70 255 * @param baud : sets the baud rate for communication between wifly and host computer
leihen 7:e42b7fa7ef70 256 *
leihen 7:e42b7fa7ef70 257 * @ returns true if successful, false otherwise
leihen 7:e42b7fa7ef70 258 */
leihen 7:e42b7fa7ef70 259 bool setBaudRate(int baud);
leihen 7:e42b7fa7ef70 260 WiflyBaudrate_t m_baudrate;
leihen 7:e42b7fa7ef70 261
samux 1:fb4494783863 262 Serial wifi;
samux 1:fb4494783863 263 DigitalOut reset_pin;
samux 1:fb4494783863 264 DigitalIn tcp_status;
samux 1:fb4494783863 265 char phrase[30];
samux 1:fb4494783863 266 char ssid[30];
samux 1:fb4494783863 267 const char * ip;
samux 1:fb4494783863 268 const char * netmask;
samux 1:fb4494783863 269 const char * gateway;
samux 1:fb4494783863 270 int channel;
samux 1:fb4494783863 271 CircBuffer<char> buf_wifly;
samux 1:fb4494783863 272
samux 1:fb4494783863 273 static Wifly * inst;
samux 1:fb4494783863 274
leihen 11:63a7dc9e45dd 275 virtual void attach_rx(bool null) = 0 ;
leihen 11:63a7dc9e45dd 276 virtual void handler_rx(void) = 0;
samux 1:fb4494783863 277
samux 1:fb4494783863 278 typedef struct STATE {
samux 1:fb4494783863 279 bool associated;
samux 1:fb4494783863 280 bool tcp;
samux 1:fb4494783863 281 bool dhcp;
samux 1:fb4494783863 282 Security sec;
samux 1:fb4494783863 283 Protocol proto;
samux 1:fb4494783863 284 bool cmd_mode;
samux 1:fb4494783863 285 } State;
samux 1:fb4494783863 286
samux 1:fb4494783863 287 State state;
samux 1:fb4494783863 288 char * getStringSecurity();
samux 1:fb4494783863 289 };
samux 1:fb4494783863 290
samux 1:fb4494783863 291 #endif