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