posilani dat

Dependencies:   FatFileSystemCpp mbed PowerControl USBHostLite

Committer:
jkaderka
Date:
Wed Apr 29 08:01:37 2015 +0000
Revision:
9:8839ecc02e0e
Parent:
7:805c16a071d0
Child:
11:137108e3403e
Comments added

Who changed what in which revision?

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