mbed official WiflyInterface (interface for Roving Networks Wifly modules)

Dependents:   Wifly_HelloWorld Websocket_Wifly_HelloWorld RPC_Wifly_HelloWorld HTTPClient_Wifly_HelloWorld ... more

Legacy Networking Libraries

This is an mbed 2 WiFI library. For mbed 5, the networking libraries have been revised to better support additional network stacks and thread safety. View information about WiFi interfaces in mbed OS 5 here.

Committer:
Bogdan Marinescu
Date:
Tue Jan 28 11:00:48 2014 +0200
Revision:
9:c77799a03294
Parent:
8:e387c1d18663
Address various potential buffer overflow issues

https://mbed.org/comments/cr/93/4779/#c8499

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"
bogdanm 7:9d18ac47d27f 31 #include "RawSerial.h"
samux 1:fb4494783863 32 #include "CBuffer.h"
samux 1:fb4494783863 33
sam_grove 8:e387c1d18663 34 #define DEFAULT_WAIT_RESP_TIMEOUT 10000
samux 1:fb4494783863 35
samux 1:fb4494783863 36 enum Security {
samux 1:fb4494783863 37 NONE = 0,
samux 1:fb4494783863 38 WEP_128 = 1,
samux 1:fb4494783863 39 WPA = 3
samux 1:fb4494783863 40 };
samux 1:fb4494783863 41
samux 1:fb4494783863 42 enum Protocol {
samux 1:fb4494783863 43 UDP = (1 << 0),
samux 1:fb4494783863 44 TCP = (1 << 1)
samux 1:fb4494783863 45 };
samux 1:fb4494783863 46
screamer 5:81e264dbbf03 47 /**
screamer 5:81e264dbbf03 48 * The Wifly class
screamer 5:81e264dbbf03 49 */
samux 1:fb4494783863 50 class Wifly
samux 1:fb4494783863 51 {
samux 1:fb4494783863 52
samux 1:fb4494783863 53 public:
screamer 5:81e264dbbf03 54 /**
samux 1:fb4494783863 55 * Constructor
samux 1:fb4494783863 56 *
samux 1:fb4494783863 57 * @param tx mbed pin to use for tx line of Serial interface
samux 1:fb4494783863 58 * @param rx mbed pin to use for rx line of Serial interface
samux 1:fb4494783863 59 * @param reset reset pin of the wifi module ()
samux 1:fb4494783863 60 * @param tcp_status connection status pin of the wifi module (GPIO 6)
samux 1:fb4494783863 61 * @param ssid ssid of the network
samux 1:fb4494783863 62 * @param phrase WEP or WPA key
samux 1:fb4494783863 63 * @param sec Security type (NONE, WEP_128 or WPA)
samux 1:fb4494783863 64 */
samux 1:fb4494783863 65 Wifly( PinName tx, PinName rx, PinName reset, PinName tcp_status, const char * ssid, const char * phrase, Security sec);
samux 1:fb4494783863 66
screamer 5:81e264dbbf03 67 /**
samux 1:fb4494783863 68 * Connect the wifi module to the ssid contained in the constructor.
samux 1:fb4494783863 69 *
samux 1:fb4494783863 70 * @return true if connected, false otherwise
samux 1:fb4494783863 71 */
samux 1:fb4494783863 72 bool join();
samux 1:fb4494783863 73
screamer 5:81e264dbbf03 74 /**
samux 1:fb4494783863 75 * Disconnect the wifly module from the access point
samux 1:fb4494783863 76 *
screamer 6:5176e0864078 77 * @return true if successful
samux 1:fb4494783863 78 */
samux 1:fb4494783863 79 bool disconnect();
samux 1:fb4494783863 80
screamer 5:81e264dbbf03 81 /**
samux 1:fb4494783863 82 * Open a tcp connection with the specified host on the specified port
samux 1:fb4494783863 83 *
samux 1:fb4494783863 84 * @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 85 * @param port port
screamer 6:5176e0864078 86 * @return true if successful
samux 1:fb4494783863 87 */
samux 1:fb4494783863 88 bool connect(const char * host, int port);
samux 1:fb4494783863 89
samux 1:fb4494783863 90
screamer 5:81e264dbbf03 91 /**
samux 1:fb4494783863 92 * Set the protocol (UDP or TCP)
samux 1:fb4494783863 93 *
samux 1:fb4494783863 94 * @param p protocol
screamer 6:5176e0864078 95 * @return true if successful
samux 1:fb4494783863 96 */
samux 1:fb4494783863 97 bool setProtocol(Protocol p);
samux 1:fb4494783863 98
screamer 5:81e264dbbf03 99 /**
samux 1:fb4494783863 100 * Reset the wifi module
samux 1:fb4494783863 101 */
samux 1:fb4494783863 102 void reset();
samux 3:9aa05e19c62e 103
screamer 5:81e264dbbf03 104 /**
samux 3:9aa05e19c62e 105 * Reboot the wifi module
samux 3:9aa05e19c62e 106 */
samux 3:9aa05e19c62e 107 bool reboot();
samux 1:fb4494783863 108
screamer 5:81e264dbbf03 109 /**
samux 1:fb4494783863 110 * Check if characters are available
samux 1:fb4494783863 111 *
samux 1:fb4494783863 112 * @return number of available characters
samux 1:fb4494783863 113 */
samux 1:fb4494783863 114 int readable();
samux 1:fb4494783863 115
screamer 5:81e264dbbf03 116 /**
samux 1:fb4494783863 117 * Check if characters are available
samux 1:fb4494783863 118 *
samux 1:fb4494783863 119 * @return number of available characters
samux 1:fb4494783863 120 */
samux 1:fb4494783863 121 int writeable();
samux 1:fb4494783863 122
screamer 5:81e264dbbf03 123 /**
samux 1:fb4494783863 124 * Check if a tcp link is active
samux 1:fb4494783863 125 *
screamer 6:5176e0864078 126 * @return true if successful
samux 1:fb4494783863 127 */
samux 1:fb4494783863 128 bool is_connected();
samux 1:fb4494783863 129
screamer 5:81e264dbbf03 130 /**
samux 1:fb4494783863 131 * Read a character
samux 1:fb4494783863 132 *
samux 1:fb4494783863 133 * @return the character read
samux 1:fb4494783863 134 */
samux 1:fb4494783863 135 char getc();
samux 1:fb4494783863 136
screamer 5:81e264dbbf03 137 /**
samux 1:fb4494783863 138 * Flush the buffer
samux 1:fb4494783863 139 */
samux 1:fb4494783863 140 void flush();
samux 1:fb4494783863 141
screamer 5:81e264dbbf03 142 /**
samux 1:fb4494783863 143 * Write a character
samux 1:fb4494783863 144 *
samux 1:fb4494783863 145 * @param the character which will be written
samux 1:fb4494783863 146 */
samux 1:fb4494783863 147 int putc(char c);
samux 1:fb4494783863 148
samux 1:fb4494783863 149
screamer 5:81e264dbbf03 150 /**
samux 1:fb4494783863 151 * To enter in command mode (we can configure the module)
samux 1:fb4494783863 152 *
samux 1:fb4494783863 153 * @return true if successful, false otherwise
samux 1:fb4494783863 154 */
samux 1:fb4494783863 155 bool cmdMode();
samux 1:fb4494783863 156
screamer 5:81e264dbbf03 157 /**
samux 1:fb4494783863 158 * To exit the command mode
samux 1:fb4494783863 159 *
samux 1:fb4494783863 160 * @return true if successful, false otherwise
samux 1:fb4494783863 161 */
samux 1:fb4494783863 162 bool exit();
samux 1:fb4494783863 163
screamer 5:81e264dbbf03 164 /**
samux 1:fb4494783863 165 * Close a tcp connection
samux 1:fb4494783863 166 *
screamer 6:5176e0864078 167 * @return true if successful
samux 1:fb4494783863 168 */
samux 1:fb4494783863 169 bool close();
samux 1:fb4494783863 170
screamer 5:81e264dbbf03 171 /**
samux 1:fb4494783863 172 * 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 173 * Useful to send a command to the module and wait a response.
samux 1:fb4494783863 174 *
samux 1:fb4494783863 175 *
samux 1:fb4494783863 176 * @param str string to be sent
samux 1:fb4494783863 177 * @param len string length
samux 1:fb4494783863 178 * @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 179 * @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 180 *
samux 1:fb4494783863 181 * @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 182 */
samux 1:fb4494783863 183 int send(const char * str, int len, const char * ACK = NULL, char * res = NULL, int timeout = DEFAULT_WAIT_RESP_TIMEOUT);
samux 1:fb4494783863 184
screamer 5:81e264dbbf03 185 /**
samux 1:fb4494783863 186 * Send a command to the wify module. Check if the module is in command mode. If not enter in command mode
samux 1:fb4494783863 187 *
samux 1:fb4494783863 188 * @param str string to be sent
samux 1:fb4494783863 189 * @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 190 * @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 191 *
screamer 6:5176e0864078 192 * @return true if successful
samux 1:fb4494783863 193 */
samux 1:fb4494783863 194 bool sendCommand(const char * cmd, const char * ack = NULL, char * res = NULL, int timeout = DEFAULT_WAIT_RESP_TIMEOUT);
samux 4:0bcec6272784 195
screamer 5:81e264dbbf03 196 /**
samux 4:0bcec6272784 197 * Return true if the module is using dhcp
samux 4:0bcec6272784 198 *
screamer 6:5176e0864078 199 * @return true if the module is using dhcp
samux 4:0bcec6272784 200 */
samux 4:0bcec6272784 201 bool isDHCP() {
samux 4:0bcec6272784 202 return state.dhcp;
samux 4:0bcec6272784 203 }
samux 1:fb4494783863 204
samux 1:fb4494783863 205 bool gethostbyname(const char * host, char * ip);
samux 1:fb4494783863 206
samux 1:fb4494783863 207 static Wifly * getInstance() {
samux 1:fb4494783863 208 return inst;
samux 1:fb4494783863 209 };
samux 1:fb4494783863 210
samux 1:fb4494783863 211 protected:
bogdanm 7:9d18ac47d27f 212 RawSerial wifi;
samux 1:fb4494783863 213 DigitalOut reset_pin;
samux 1:fb4494783863 214 DigitalIn tcp_status;
Bogdan Marinescu 9:c77799a03294 215 char phrase[65];
Bogdan Marinescu 9:c77799a03294 216 char ssid[33];
samux 1:fb4494783863 217 const char * ip;
samux 1:fb4494783863 218 const char * netmask;
samux 1:fb4494783863 219 const char * gateway;
samux 1:fb4494783863 220 int channel;
samux 1:fb4494783863 221 CircBuffer<char> buf_wifly;
samux 1:fb4494783863 222
samux 1:fb4494783863 223 static Wifly * inst;
samux 1:fb4494783863 224
samux 1:fb4494783863 225 void attach_rx(bool null);
samux 1:fb4494783863 226 void handler_rx(void);
samux 1:fb4494783863 227
samux 1:fb4494783863 228
samux 1:fb4494783863 229 typedef struct STATE {
samux 1:fb4494783863 230 bool associated;
samux 1:fb4494783863 231 bool tcp;
samux 1:fb4494783863 232 bool dhcp;
samux 1:fb4494783863 233 Security sec;
samux 1:fb4494783863 234 Protocol proto;
samux 1:fb4494783863 235 bool cmd_mode;
samux 1:fb4494783863 236 } State;
samux 1:fb4494783863 237
samux 1:fb4494783863 238 State state;
samux 1:fb4494783863 239 char * getStringSecurity();
samux 1:fb4494783863 240 };
samux 1:fb4494783863 241
Bogdan Marinescu 9:c77799a03294 242 #endif