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