posilani dat

Dependencies:   FatFileSystemCpp mbed PowerControl USBHostLite

Committer:
PavelKumpan
Date:
Tue May 23 18:42:14 2017 +0000
Revision:
26:5674b8978551
Parent:
11:137108e3403e
Recreated communication protocol.

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 11:137108e3403e 73 void flowCTS(PinName cts);
jkaderka 7:805c16a071d0 74 /**
jkaderka 7:805c16a071d0 75 * Connect the wifi module to the ssid contained in the constructor.
jkaderka 7:805c16a071d0 76 *
jkaderka 7:805c16a071d0 77 * @return true if connected, false otherwise
jkaderka 7:805c16a071d0 78 */
jkaderka 7:805c16a071d0 79 bool join();
jkaderka 7:805c16a071d0 80
jkaderka 7:805c16a071d0 81 /**
jkaderka 7:805c16a071d0 82 * Disconnect the wifly module from the access point
jkaderka 7:805c16a071d0 83 *
jkaderka 7:805c16a071d0 84 * @return true if successful
jkaderka 7:805c16a071d0 85 */
jkaderka 7:805c16a071d0 86 bool disconnect();
jkaderka 7:805c16a071d0 87
jkaderka 7:805c16a071d0 88 /**
jkaderka 7:805c16a071d0 89 * Open a tcp connection with the specified host on the specified port
jkaderka 7:805c16a071d0 90 *
jkaderka 7:805c16a071d0 91 * @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 92 * @param port port
jkaderka 7:805c16a071d0 93 * @return true if successful
jkaderka 7:805c16a071d0 94 */
jkaderka 7:805c16a071d0 95 bool connect(const char * host, int port);
jkaderka 7:805c16a071d0 96
jkaderka 7:805c16a071d0 97
jkaderka 7:805c16a071d0 98 /**
jkaderka 7:805c16a071d0 99 * Set the protocol (UDP or TCP)
jkaderka 7:805c16a071d0 100 *
jkaderka 7:805c16a071d0 101 * @param p protocol
jkaderka 7:805c16a071d0 102 * @return true if successful
jkaderka 7:805c16a071d0 103 */
jkaderka 7:805c16a071d0 104 bool setProtocol(Protocol p);
jkaderka 7:805c16a071d0 105
jkaderka 7:805c16a071d0 106 /**
jkaderka 7:805c16a071d0 107 * Reset the wifi module
jkaderka 7:805c16a071d0 108 */
jkaderka 7:805c16a071d0 109 void reset();
jkaderka 7:805c16a071d0 110
jkaderka 7:805c16a071d0 111 /**
jkaderka 7:805c16a071d0 112 * Reboot the wifi module
jkaderka 7:805c16a071d0 113 */
jkaderka 7:805c16a071d0 114 bool reboot();
jkaderka 7:805c16a071d0 115
jkaderka 7:805c16a071d0 116 /**
jkaderka 7:805c16a071d0 117 * Check if characters are available
jkaderka 7:805c16a071d0 118 *
jkaderka 7:805c16a071d0 119 * @return number of available characters
jkaderka 7:805c16a071d0 120 */
jkaderka 7:805c16a071d0 121 int readable();
jkaderka 7:805c16a071d0 122
jkaderka 7:805c16a071d0 123 /**
jkaderka 7:805c16a071d0 124 * Check if characters are available
jkaderka 7:805c16a071d0 125 *
jkaderka 7:805c16a071d0 126 * @return number of available characters
jkaderka 7:805c16a071d0 127 */
jkaderka 7:805c16a071d0 128 int writeable();
jkaderka 7:805c16a071d0 129
jkaderka 7:805c16a071d0 130 /**
jkaderka 7:805c16a071d0 131 * Check if a tcp link is active
jkaderka 7:805c16a071d0 132 *
jkaderka 7:805c16a071d0 133 * @return true if successful
jkaderka 7:805c16a071d0 134 */
jkaderka 7:805c16a071d0 135 bool is_connected();
jkaderka 7:805c16a071d0 136
jkaderka 7:805c16a071d0 137 /**
jkaderka 7:805c16a071d0 138 * Read a character
jkaderka 7:805c16a071d0 139 *
jkaderka 7:805c16a071d0 140 * @return the character read
jkaderka 7:805c16a071d0 141 */
jkaderka 7:805c16a071d0 142 char getc();
jkaderka 7:805c16a071d0 143
jkaderka 7:805c16a071d0 144 /**
jkaderka 7:805c16a071d0 145 * Flush the buffer
jkaderka 7:805c16a071d0 146 */
jkaderka 7:805c16a071d0 147 void flush();
jkaderka 7:805c16a071d0 148
jkaderka 7:805c16a071d0 149 /**
jkaderka 7:805c16a071d0 150 * Write a character
jkaderka 7:805c16a071d0 151 *
jkaderka 7:805c16a071d0 152 * @param the character which will be written
jkaderka 7:805c16a071d0 153 */
jkaderka 7:805c16a071d0 154 int putc(char c);
jkaderka 7:805c16a071d0 155
jkaderka 7:805c16a071d0 156
jkaderka 7:805c16a071d0 157 /**
jkaderka 7:805c16a071d0 158 * To enter in command mode (we can configure the module)
jkaderka 7:805c16a071d0 159 *
jkaderka 7:805c16a071d0 160 * @return true if successful, false otherwise
jkaderka 7:805c16a071d0 161 */
jkaderka 7:805c16a071d0 162 bool cmdMode();
jkaderka 7:805c16a071d0 163
jkaderka 7:805c16a071d0 164 /**
jkaderka 7:805c16a071d0 165 * To exit the command mode
jkaderka 7:805c16a071d0 166 *
jkaderka 7:805c16a071d0 167 * @return true if successful, false otherwise
jkaderka 7:805c16a071d0 168 */
jkaderka 7:805c16a071d0 169 bool exit();
jkaderka 7:805c16a071d0 170
jkaderka 7:805c16a071d0 171 /**
jkaderka 7:805c16a071d0 172 * Close a tcp connection
jkaderka 7:805c16a071d0 173 *
jkaderka 7:805c16a071d0 174 * @return true if successful
jkaderka 7:805c16a071d0 175 */
jkaderka 7:805c16a071d0 176 bool close();
jkaderka 7:805c16a071d0 177
jkaderka 7:805c16a071d0 178 /**
jkaderka 7:805c16a071d0 179 * 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 180 * Useful to send a command to the module and wait a response.
jkaderka 7:805c16a071d0 181 *
jkaderka 7:805c16a071d0 182 *
jkaderka 7:805c16a071d0 183 * @param str string to be sent
jkaderka 7:805c16a071d0 184 * @param len string length
jkaderka 7:805c16a071d0 185 * @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 186 * @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 187 *
jkaderka 7:805c16a071d0 188 * @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 189 */
jkaderka 7:805c16a071d0 190 int send(const char * str, int len, const char * ACK = NULL, char * res = NULL, int timeout = DEFAULT_WAIT_RESP_TIMEOUT);
jkaderka 7:805c16a071d0 191
jkaderka 7:805c16a071d0 192 /**
jkaderka 7:805c16a071d0 193 * Send a command to the wify module. Check if the module is in command mode. If not enter in command mode
jkaderka 7:805c16a071d0 194 *
jkaderka 7:805c16a071d0 195 * @param str string to be sent
jkaderka 7:805c16a071d0 196 * @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 197 * @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 198 *
jkaderka 7:805c16a071d0 199 * @return true if successful
jkaderka 7:805c16a071d0 200 */
jkaderka 7:805c16a071d0 201 bool sendCommand(const char * cmd, const char * ack = NULL, char * res = NULL, int timeout = DEFAULT_WAIT_RESP_TIMEOUT);
jkaderka 7:805c16a071d0 202
jkaderka 7:805c16a071d0 203 /**
jkaderka 7:805c16a071d0 204 * Return true if the module is using dhcp
jkaderka 7:805c16a071d0 205 *
jkaderka 7:805c16a071d0 206 * @return true if the module is using dhcp
jkaderka 7:805c16a071d0 207 */
jkaderka 7:805c16a071d0 208 bool isDHCP() {
jkaderka 7:805c16a071d0 209 return state.dhcp;
jkaderka 7:805c16a071d0 210 }
jkaderka 7:805c16a071d0 211
jkaderka 7:805c16a071d0 212 bool gethostbyname(const char * host, char * ip);
jkaderka 7:805c16a071d0 213
jkaderka 7:805c16a071d0 214 static Wifly * getInstance() {
jkaderka 7:805c16a071d0 215 return inst;
jkaderka 7:805c16a071d0 216 };
jkaderka 7:805c16a071d0 217
jkaderka 7:805c16a071d0 218 protected:
jkaderka 7:805c16a071d0 219 RawSerial wifi;
jkaderka 7:805c16a071d0 220 DigitalOut reset_pin;
jkaderka 7:805c16a071d0 221 DigitalIn tcp_status;
jkaderka 7:805c16a071d0 222 char phrase[65];
jkaderka 7:805c16a071d0 223 char ssid[33];
jkaderka 7:805c16a071d0 224 const char * ip;
jkaderka 7:805c16a071d0 225 const char * netmask;
jkaderka 7:805c16a071d0 226 const char * gateway;
jkaderka 7:805c16a071d0 227 int channel;
jkaderka 7:805c16a071d0 228 CircBuffer<char> buf_wifly;
jkaderka 7:805c16a071d0 229
jkaderka 7:805c16a071d0 230 static Wifly * inst;
jkaderka 7:805c16a071d0 231
jkaderka 7:805c16a071d0 232 void attach_rx(bool null);
jkaderka 7:805c16a071d0 233 void handler_rx(void);
jkaderka 7:805c16a071d0 234
jkaderka 7:805c16a071d0 235
jkaderka 7:805c16a071d0 236 typedef struct STATE {
jkaderka 7:805c16a071d0 237 bool associated;
jkaderka 7:805c16a071d0 238 bool tcp;
jkaderka 7:805c16a071d0 239 bool dhcp;
jkaderka 7:805c16a071d0 240 Security sec;
jkaderka 7:805c16a071d0 241 Protocol proto;
jkaderka 7:805c16a071d0 242 bool cmd_mode;
jkaderka 7:805c16a071d0 243 } State;
jkaderka 7:805c16a071d0 244
jkaderka 7:805c16a071d0 245 State state;
jkaderka 7:805c16a071d0 246 char * getStringSecurity();
jkaderka 7:805c16a071d0 247 };
jkaderka 7:805c16a071d0 248
jkaderka 7:805c16a071d0 249 #endif