https://developer.mbed.org/users/ds4279/code/WiflySocket/
Wifly.h@0:374a8a31f262, 2017-02-22 (annotated)
- Committer:
- ds4279
- Date:
- Wed Feb 22 18:49:17 2017 +0000
- Revision:
- 0:374a8a31f262
Added modified library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ds4279 | 0:374a8a31f262 | 1 | /* Copyright (C) 2012 mbed.org, MIT License |
ds4279 | 0:374a8a31f262 | 2 | * |
ds4279 | 0:374a8a31f262 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
ds4279 | 0:374a8a31f262 | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
ds4279 | 0:374a8a31f262 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
ds4279 | 0:374a8a31f262 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
ds4279 | 0:374a8a31f262 | 7 | * furnished to do so, subject to the following conditions: |
ds4279 | 0:374a8a31f262 | 8 | * |
ds4279 | 0:374a8a31f262 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
ds4279 | 0:374a8a31f262 | 10 | * substantial portions of the Software. |
ds4279 | 0:374a8a31f262 | 11 | * |
ds4279 | 0:374a8a31f262 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
ds4279 | 0:374a8a31f262 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
ds4279 | 0:374a8a31f262 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
ds4279 | 0:374a8a31f262 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
ds4279 | 0:374a8a31f262 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
ds4279 | 0:374a8a31f262 | 17 | * |
ds4279 | 0:374a8a31f262 | 18 | * @section DESCRIPTION |
ds4279 | 0:374a8a31f262 | 19 | * |
ds4279 | 0:374a8a31f262 | 20 | * Wifly Seeed Wifi Shield V2.0 RN-171, wifi module |
ds4279 | 0:374a8a31f262 | 21 | * |
ds4279 | 0:374a8a31f262 | 22 | * Datasheet: |
ds4279 | 0:374a8a31f262 | 23 | * |
ds4279 | 0:374a8a31f262 | 24 | * http://dlnmh9ip6v2uc.cloudfront.net/datasheets/Wireless/WiFi/WiFly-RN-UM.pdf |
ds4279 | 0:374a8a31f262 | 25 | * WiFly-EZX-171 LAN Module |
ds4279 | 0:374a8a31f262 | 26 | |
ds4279 | 0:374a8a31f262 | 27 | //On the Seeed Wifi Shield V2.0 there are two solder pads p5 and p6 |
ds4279 | 0:374a8a31f262 | 28 | //p5: will connect D6/PB_10 to the hardware reset pin of RN-171 |
ds4279 | 0:374a8a31f262 | 29 | //p6: will connect D5/PB_4 to GPIO6 of RN-171 to monitor IP Ready/tcpStatus |
ds4279 | 0:374a8a31f262 | 30 | */ |
ds4279 | 0:374a8a31f262 | 31 | |
ds4279 | 0:374a8a31f262 | 32 | #ifndef WIFLY_H |
ds4279 | 0:374a8a31f262 | 33 | #define WIFLY_H |
ds4279 | 0:374a8a31f262 | 34 | |
ds4279 | 0:374a8a31f262 | 35 | #include "mbed.h" |
ds4279 | 0:374a8a31f262 | 36 | |
ds4279 | 0:374a8a31f262 | 37 | #include "BufferedSerial.h" |
ds4279 | 0:374a8a31f262 | 38 | |
ds4279 | 0:374a8a31f262 | 39 | //#include "RawSerial.h" |
ds4279 | 0:374a8a31f262 | 40 | //#include "CBuffer.h" |
ds4279 | 0:374a8a31f262 | 41 | |
ds4279 | 0:374a8a31f262 | 42 | #define DEFAULT_WAIT_RESP_TIMEOUT 10000 |
ds4279 | 0:374a8a31f262 | 43 | #define DEFAULT_WAIT_RESPONSE_TIME 1000 |
ds4279 | 0:374a8a31f262 | 44 | |
ds4279 | 0:374a8a31f262 | 45 | enum Security { |
ds4279 | 0:374a8a31f262 | 46 | NONE = 0, |
ds4279 | 0:374a8a31f262 | 47 | WEP_128 = 1, |
ds4279 | 0:374a8a31f262 | 48 | WPA = 3 |
ds4279 | 0:374a8a31f262 | 49 | }; |
ds4279 | 0:374a8a31f262 | 50 | |
ds4279 | 0:374a8a31f262 | 51 | enum Protocol { |
ds4279 | 0:374a8a31f262 | 52 | UDP = (1 << 0), |
ds4279 | 0:374a8a31f262 | 53 | TCP = (1 << 1) |
ds4279 | 0:374a8a31f262 | 54 | }; |
ds4279 | 0:374a8a31f262 | 55 | |
ds4279 | 0:374a8a31f262 | 56 | #define softwareReset 0 |
ds4279 | 0:374a8a31f262 | 57 | #define hardwareReset 1 |
ds4279 | 0:374a8a31f262 | 58 | #define setDHCP true |
ds4279 | 0:374a8a31f262 | 59 | #define setStatic false |
ds4279 | 0:374a8a31f262 | 60 | |
ds4279 | 0:374a8a31f262 | 61 | /** |
ds4279 | 0:374a8a31f262 | 62 | * The Wifly class |
ds4279 | 0:374a8a31f262 | 63 | */ |
ds4279 | 0:374a8a31f262 | 64 | class Wifly |
ds4279 | 0:374a8a31f262 | 65 | { |
ds4279 | 0:374a8a31f262 | 66 | |
ds4279 | 0:374a8a31f262 | 67 | public: |
ds4279 | 0:374a8a31f262 | 68 | /**join |
ds4279 | 0:374a8a31f262 | 69 | * Constructor |
ds4279 | 0:374a8a31f262 | 70 | * |
ds4279 | 0:374a8a31f262 | 71 | * @param tx mbed pin to use for tx line of Serial interface |
ds4279 | 0:374a8a31f262 | 72 | * @param rx mbed pin to use for rx line of Serial interface |
ds4279 | 0:374a8a31f262 | 73 | * @param reset reset pin of the wifi module () |
ds4279 | 0:374a8a31f262 | 74 | * @param tcp_status connection status pin of the wifi module (GPIO 6) |
ds4279 | 0:374a8a31f262 | 75 | * @param ssid ssid of the network |
ds4279 | 0:374a8a31f262 | 76 | * @param phrase WEP or WPA key |
ds4279 | 0:374a8a31f262 | 77 | * @param sec Security type (NONE, WEP_128 or WPA) |
ds4279 | 0:374a8a31f262 | 78 | |
ds4279 | 0:374a8a31f262 | 79 | //On the Seeed Wifi Shield V2.0 there are two solder pads p5 and p6 |
ds4279 | 0:374a8a31f262 | 80 | //p5: will connect D6/PB_10 to the hardware reset pin of RN-171 |
ds4279 | 0:374a8a31f262 | 81 | //p6: will connect D5/PB_4 to GPIO6 of RN-171 to monitor IP Ready/tcpStatus |
ds4279 | 0:374a8a31f262 | 82 | |
ds4279 | 0:374a8a31f262 | 83 | */ |
ds4279 | 0:374a8a31f262 | 84 | Wifly( PinName tx, PinName rx, PinName reset, PinName tcp_status, const char * ssid, const char * phrase, Security sec); |
ds4279 | 0:374a8a31f262 | 85 | |
ds4279 | 0:374a8a31f262 | 86 | /** |
ds4279 | 0:374a8a31f262 | 87 | * Connect the wifi module to the ssid contained in the constructor. |
ds4279 | 0:374a8a31f262 | 88 | * |
ds4279 | 0:374a8a31f262 | 89 | * @return true if connected, false otherwise |
ds4279 | 0:374a8a31f262 | 90 | */ |
ds4279 | 0:374a8a31f262 | 91 | |
ds4279 | 0:374a8a31f262 | 92 | bool init(); //Setup initial values of the wifi. |
ds4279 | 0:374a8a31f262 | 93 | bool join(); //Uses the base class SSID/Phrase/AUTH |
ds4279 | 0:374a8a31f262 | 94 | bool join(const char *ssid, const char *phrase, int auth); |
ds4279 | 0:374a8a31f262 | 95 | void setConnectionType(bool DHCP_STATIC=true); //Default DHCP |
ds4279 | 0:374a8a31f262 | 96 | |
ds4279 | 0:374a8a31f262 | 97 | /** |
ds4279 | 0:374a8a31f262 | 98 | * Disconnect the wifly module from the access point |
ds4279 | 0:374a8a31f262 | 99 | * |
ds4279 | 0:374a8a31f262 | 100 | * @return true if successful |
ds4279 | 0:374a8a31f262 | 101 | */ |
ds4279 | 0:374a8a31f262 | 102 | bool disconnect(); |
ds4279 | 0:374a8a31f262 | 103 | |
ds4279 | 0:374a8a31f262 | 104 | /** |
ds4279 | 0:374a8a31f262 | 105 | * Open a tcp connection with the specified host on the specified port |
ds4279 | 0:374a8a31f262 | 106 | * |
ds4279 | 0:374a8a31f262 | 107 | * @param host host (can be either an ip address or a name. If a name is provided, a dns request will be established) |
ds4279 | 0:374a8a31f262 | 108 | * @param port port |
ds4279 | 0:374a8a31f262 | 109 | * @return true if successful |
ds4279 | 0:374a8a31f262 | 110 | */ |
ds4279 | 0:374a8a31f262 | 111 | bool connect(const char * host, int port); |
ds4279 | 0:374a8a31f262 | 112 | |
ds4279 | 0:374a8a31f262 | 113 | |
ds4279 | 0:374a8a31f262 | 114 | /** |
ds4279 | 0:374a8a31f262 | 115 | * Set the protocol (UDP or TCP) |
ds4279 | 0:374a8a31f262 | 116 | * |
ds4279 | 0:374a8a31f262 | 117 | * @param p protocol |
ds4279 | 0:374a8a31f262 | 118 | * @return true if successful |
ds4279 | 0:374a8a31f262 | 119 | */ |
ds4279 | 0:374a8a31f262 | 120 | bool setProtocol(Protocol p); |
ds4279 | 0:374a8a31f262 | 121 | |
ds4279 | 0:374a8a31f262 | 122 | /** |
ds4279 | 0:374a8a31f262 | 123 | * Reset the wifi module |
ds4279 | 0:374a8a31f262 | 124 | */ |
ds4279 | 0:374a8a31f262 | 125 | bool reset(bool); //0:softwareReset 1: hardwareReset |
ds4279 | 0:374a8a31f262 | 126 | |
ds4279 | 0:374a8a31f262 | 127 | /** |
ds4279 | 0:374a8a31f262 | 128 | * Reboot the wifi module |
ds4279 | 0:374a8a31f262 | 129 | */ |
ds4279 | 0:374a8a31f262 | 130 | bool reboot(); |
ds4279 | 0:374a8a31f262 | 131 | |
ds4279 | 0:374a8a31f262 | 132 | /** |
ds4279 | 0:374a8a31f262 | 133 | * Check if characters are available |
ds4279 | 0:374a8a31f262 | 134 | * |
ds4279 | 0:374a8a31f262 | 135 | * @return number of available characters |
ds4279 | 0:374a8a31f262 | 136 | */ |
ds4279 | 0:374a8a31f262 | 137 | int readable(); |
ds4279 | 0:374a8a31f262 | 138 | |
ds4279 | 0:374a8a31f262 | 139 | /** |
ds4279 | 0:374a8a31f262 | 140 | * Check if characters are available |
ds4279 | 0:374a8a31f262 | 141 | * |
ds4279 | 0:374a8a31f262 | 142 | * @return number of available characters |
ds4279 | 0:374a8a31f262 | 143 | */ |
ds4279 | 0:374a8a31f262 | 144 | int writeable(); |
ds4279 | 0:374a8a31f262 | 145 | |
ds4279 | 0:374a8a31f262 | 146 | /** |
ds4279 | 0:374a8a31f262 | 147 | * Check if a tcp link is active |
ds4279 | 0:374a8a31f262 | 148 | * |
ds4279 | 0:374a8a31f262 | 149 | * @return true if successful |
ds4279 | 0:374a8a31f262 | 150 | */ |
ds4279 | 0:374a8a31f262 | 151 | bool is_connected(); |
ds4279 | 0:374a8a31f262 | 152 | |
ds4279 | 0:374a8a31f262 | 153 | /** |
ds4279 | 0:374a8a31f262 | 154 | * Read a character |
ds4279 | 0:374a8a31f262 | 155 | * |
ds4279 | 0:374a8a31f262 | 156 | * @return the character read |
ds4279 | 0:374a8a31f262 | 157 | */ |
ds4279 | 0:374a8a31f262 | 158 | char getc(); |
ds4279 | 0:374a8a31f262 | 159 | |
ds4279 | 0:374a8a31f262 | 160 | /** |
ds4279 | 0:374a8a31f262 | 161 | * Flush the buffer |
ds4279 | 0:374a8a31f262 | 162 | */ |
ds4279 | 0:374a8a31f262 | 163 | void flush(); |
ds4279 | 0:374a8a31f262 | 164 | |
ds4279 | 0:374a8a31f262 | 165 | /** |
ds4279 | 0:374a8a31f262 | 166 | * Write a character |
ds4279 | 0:374a8a31f262 | 167 | * |
ds4279 | 0:374a8a31f262 | 168 | * @param the character which will be written |
ds4279 | 0:374a8a31f262 | 169 | */ |
ds4279 | 0:374a8a31f262 | 170 | int putc(char c); |
ds4279 | 0:374a8a31f262 | 171 | |
ds4279 | 0:374a8a31f262 | 172 | |
ds4279 | 0:374a8a31f262 | 173 | /** |
ds4279 | 0:374a8a31f262 | 174 | * To enter in command mode (we can configure the module) |
ds4279 | 0:374a8a31f262 | 175 | * |
ds4279 | 0:374a8a31f262 | 176 | * @return true if successful, false otherwise |
ds4279 | 0:374a8a31f262 | 177 | */ |
ds4279 | 0:374a8a31f262 | 178 | bool cmdMode(); |
ds4279 | 0:374a8a31f262 | 179 | |
ds4279 | 0:374a8a31f262 | 180 | /** |
ds4279 | 0:374a8a31f262 | 181 | * To exit the command mode |
ds4279 | 0:374a8a31f262 | 182 | * |
ds4279 | 0:374a8a31f262 | 183 | * @return true if successful, false otherwise |
ds4279 | 0:374a8a31f262 | 184 | */ |
ds4279 | 0:374a8a31f262 | 185 | bool exitCmdMode(); |
ds4279 | 0:374a8a31f262 | 186 | |
ds4279 | 0:374a8a31f262 | 187 | /** |
ds4279 | 0:374a8a31f262 | 188 | * Close a tcp connection |
ds4279 | 0:374a8a31f262 | 189 | * |
ds4279 | 0:374a8a31f262 | 190 | * @return true if successful |
ds4279 | 0:374a8a31f262 | 191 | */ |
ds4279 | 0:374a8a31f262 | 192 | bool close(); |
ds4279 | 0:374a8a31f262 | 193 | |
ds4279 | 0:374a8a31f262 | 194 | /** |
ds4279 | 0:374a8a31f262 | 195 | * 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. |
ds4279 | 0:374a8a31f262 | 196 | * Useful to send a command to the module and wait a response. |
ds4279 | 0:374a8a31f262 | 197 | * |
ds4279 | 0:374a8a31f262 | 198 | * |
ds4279 | 0:374a8a31f262 | 199 | * @param str string to be sent |
ds4279 | 0:374a8a31f262 | 200 | * @param len string length |
ds4279 | 0:374a8a31f262 | 201 | * @param ACK string which must be acknowledge by the wifi module. If ACK == NULL, no string has to be acknoledged. (default: "NO") |
ds4279 | 0:374a8a31f262 | 202 | * @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) |
ds4279 | 0:374a8a31f262 | 203 | * |
ds4279 | 0:374a8a31f262 | 204 | * @return true if ACK has been found in the response from the wifi module. False otherwise or if there is no response in 5s. |
ds4279 | 0:374a8a31f262 | 205 | */ |
ds4279 | 0:374a8a31f262 | 206 | int sendString(const char * str, int len, const char * ACK = NULL, char * res = NULL, int timeout = DEFAULT_WAIT_RESPONSE_TIME, bool DHCPconn = false); |
ds4279 | 0:374a8a31f262 | 207 | |
ds4279 | 0:374a8a31f262 | 208 | /** |
ds4279 | 0:374a8a31f262 | 209 | * Send a command to the wify module. Check if the module is in command mode. If not enter in command mode |
ds4279 | 0:374a8a31f262 | 210 | * |
ds4279 | 0:374a8a31f262 | 211 | * @param str string to be sent |
ds4279 | 0:374a8a31f262 | 212 | * @param ACK string which must be acknowledge by the wifi module. If ACK == NULL, no string has to be acknoledged. (default: "NO") |
ds4279 | 0:374a8a31f262 | 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) |
ds4279 | 0:374a8a31f262 | 214 | * |
ds4279 | 0:374a8a31f262 | 215 | * @return true if successful |
ds4279 | 0:374a8a31f262 | 216 | */ |
ds4279 | 0:374a8a31f262 | 217 | bool sendCommand(const char * cmd, const char * ack = NULL, char * res = NULL, int timeout = DEFAULT_WAIT_RESP_TIMEOUT, bool DHCPconn = false); |
ds4279 | 0:374a8a31f262 | 218 | |
ds4279 | 0:374a8a31f262 | 219 | bool setBaudRate(int baud); |
ds4279 | 0:374a8a31f262 | 220 | /** |
ds4279 | 0:374a8a31f262 | 221 | * Return true if the module is using dhcp |
ds4279 | 0:374a8a31f262 | 222 | * |
ds4279 | 0:374a8a31f262 | 223 | * @return true if the module is using dhcp |
ds4279 | 0:374a8a31f262 | 224 | */ |
ds4279 | 0:374a8a31f262 | 225 | bool isDHCP() { |
ds4279 | 0:374a8a31f262 | 226 | return state.dhcp; |
ds4279 | 0:374a8a31f262 | 227 | } |
ds4279 | 0:374a8a31f262 | 228 | |
ds4279 | 0:374a8a31f262 | 229 | |
ds4279 | 0:374a8a31f262 | 230 | |
ds4279 | 0:374a8a31f262 | 231 | bool gethostbyname(const char * host, char * ip); |
ds4279 | 0:374a8a31f262 | 232 | void getDHCPhostInfo(char *SSID, char *passPhrase, int &auth); |
ds4279 | 0:374a8a31f262 | 233 | |
ds4279 | 0:374a8a31f262 | 234 | static Wifly * getInstance() { |
ds4279 | 0:374a8a31f262 | 235 | return inst; |
ds4279 | 0:374a8a31f262 | 236 | }; |
ds4279 | 0:374a8a31f262 | 237 | |
ds4279 | 0:374a8a31f262 | 238 | protected: |
ds4279 | 0:374a8a31f262 | 239 | BufferedSerial wifiSer; |
ds4279 | 0:374a8a31f262 | 240 | DigitalOut reset_pin; |
ds4279 | 0:374a8a31f262 | 241 | DigitalIn tcp_status; |
ds4279 | 0:374a8a31f262 | 242 | char phrase[65]; |
ds4279 | 0:374a8a31f262 | 243 | char ssid[33]; |
ds4279 | 0:374a8a31f262 | 244 | char staticIP[30]; |
ds4279 | 0:374a8a31f262 | 245 | const char * ip; |
ds4279 | 0:374a8a31f262 | 246 | const char * netmask; |
ds4279 | 0:374a8a31f262 | 247 | const char * gateway; |
ds4279 | 0:374a8a31f262 | 248 | int channel; |
ds4279 | 0:374a8a31f262 | 249 | // CircBuffer<char> buf_wifly; |
ds4279 | 0:374a8a31f262 | 250 | |
ds4279 | 0:374a8a31f262 | 251 | static Wifly * inst; |
ds4279 | 0:374a8a31f262 | 252 | |
ds4279 | 0:374a8a31f262 | 253 | void attach_rx(bool null); |
ds4279 | 0:374a8a31f262 | 254 | void handler_rx(void); |
ds4279 | 0:374a8a31f262 | 255 | |
ds4279 | 0:374a8a31f262 | 256 | void enableDHCP(bool DHCP_STATIC); |
ds4279 | 0:374a8a31f262 | 257 | void setStaticIP(const char *staticIP); |
ds4279 | 0:374a8a31f262 | 258 | |
ds4279 | 0:374a8a31f262 | 259 | typedef struct STATE { |
ds4279 | 0:374a8a31f262 | 260 | bool associated; |
ds4279 | 0:374a8a31f262 | 261 | bool tcp; |
ds4279 | 0:374a8a31f262 | 262 | bool dhcp; |
ds4279 | 0:374a8a31f262 | 263 | Security sec; |
ds4279 | 0:374a8a31f262 | 264 | Protocol proto; |
ds4279 | 0:374a8a31f262 | 265 | bool cmd_mode; |
ds4279 | 0:374a8a31f262 | 266 | } State; |
ds4279 | 0:374a8a31f262 | 267 | |
ds4279 | 0:374a8a31f262 | 268 | State state; |
ds4279 | 0:374a8a31f262 | 269 | char * getStringSecurity(); |
ds4279 | 0:374a8a31f262 | 270 | }; |
ds4279 | 0:374a8a31f262 | 271 | |
ds4279 | 0:374a8a31f262 | 272 | #endif |
ds4279 | 0:374a8a31f262 | 273 |