Damien Frost / WiflyInterface

Dependents:   IoT_Ex BatteryModelTester BatteryModelTester

Fork of WiflyInterface by Components

Committer:
defrost
Date:
Mon May 09 11:39:08 2016 +0000
Revision:
31:2846355deb7e
Parent:
30:2c704a72d88a
Child:
32:ab206f7f5090
- Fixed checkNetworkStatus() function, works properly

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