private fork

Fork of GSwifiInterface by gs fan

Committer:
gsfan
Date:
Sun Jan 27 14:31:19 2013 +0000
Revision:
5:78943b3945b5
Parent:
Wifly/Wifly.h@4:0bcec6272784
Child:
6:6a6396b56405
1st build

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gsfan 5:78943b3945b5 1 /* Copyright (C) 2012 mbed.org, MIT License
gsfan 5:78943b3945b5 2 *
gsfan 5:78943b3945b5 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
gsfan 5:78943b3945b5 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
gsfan 5:78943b3945b5 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
gsfan 5:78943b3945b5 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
gsfan 5:78943b3945b5 7 * furnished to do so, subject to the following conditions:
gsfan 5:78943b3945b5 8 *
gsfan 5:78943b3945b5 9 * The above copyright notice and this permission notice shall be included in all copies or
gsfan 5:78943b3945b5 10 * substantial portions of the Software.
gsfan 5:78943b3945b5 11 *
gsfan 5:78943b3945b5 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
gsfan 5:78943b3945b5 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
gsfan 5:78943b3945b5 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
gsfan 5:78943b3945b5 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
gsfan 5:78943b3945b5 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
gsfan 5:78943b3945b5 17 *
gsfan 5:78943b3945b5 18 * @section DESCRIPTION
gsfan 5:78943b3945b5 19 *
gsfan 5:78943b3945b5 20 * GainSpan GS1011, Wi-Fi module
gsfan 5:78943b3945b5 21 *
gsfan 5:78943b3945b5 22 * http://www.gainspan.com/modules
gsfan 5:78943b3945b5 23 */
gsfan 5:78943b3945b5 24 /* Copyright (C) 2013 gsfan, MIT License
gsfan 5:78943b3945b5 25 * port to the GainSpan Wi-FI module GS1011
gsfan 5:78943b3945b5 26 */
gsfan 5:78943b3945b5 27
gsfan 5:78943b3945b5 28 #ifndef GSwifi_H
gsfan 5:78943b3945b5 29 #define GSwifi_H
gsfan 5:78943b3945b5 30
gsfan 5:78943b3945b5 31 #include "mbed.h"
gsfan 5:78943b3945b5 32 #include "CBuffer.h"
gsfan 5:78943b3945b5 33
gsfan 5:78943b3945b5 34 #define DEFAULT_WAIT_RESP_TIMEOUT 500
gsfan 5:78943b3945b5 35 #define CFG_TIMEOUT 10000 // ms
gsfan 5:78943b3945b5 36 #define CFG_TIMEOUT2 30000 // ms
gsfan 5:78943b3945b5 37 #define CFG_CMD_SIZE 100
gsfan 5:78943b3945b5 38 #define CFG_DATA_SIZE 1000
gsfan 5:78943b3945b5 39
gsfan 5:78943b3945b5 40 #define CFG_UART_BAUD 9600
gsfan 5:78943b3945b5 41 #define CFG_DNSNAME "setup.local"
gsfan 5:78943b3945b5 42 #define CFG_WREGDOMAIN "2" // 0:FCC, 1:ETSI, 2:TELEC
gsfan 5:78943b3945b5 43 #define MAX_TRY_JOIN 1
gsfan 5:78943b3945b5 44
gsfan 5:78943b3945b5 45
gsfan 5:78943b3945b5 46 //Debug is disabled by default
gsfan 5:78943b3945b5 47 #if (0 && !defined(TARGET_LPC11U24))
gsfan 5:78943b3945b5 48 #define DBG(x, ...) std::printf("[GSwifi : DBG]" x "\r\n", ##__VA_ARGS__);
gsfan 5:78943b3945b5 49 #define WARN(x, ...) std::printf("[GSwifi : WARN]" x "\r\n", ##__VA_ARGS__);
gsfan 5:78943b3945b5 50 #define ERR(x, ...) std::printf("[GSwifi : ERR]" x "\r\n", ##__VA_ARGS__);
gsfan 5:78943b3945b5 51 #else
gsfan 5:78943b3945b5 52 #define DBG(x, ...)
gsfan 5:78943b3945b5 53 #define WARN(x, ...)
gsfan 5:78943b3945b5 54 #define ERR(x, ...)
gsfan 5:78943b3945b5 55 #endif
gsfan 5:78943b3945b5 56
gsfan 5:78943b3945b5 57 #if !defined(TARGET_LPC11U24)
gsfan 5:78943b3945b5 58 #define INFO(x, ...) printf("[GSwifi : INFO]\r\n"x"\r\n", ##__VA_ARGS__);
gsfan 5:78943b3945b5 59 #else
gsfan 5:78943b3945b5 60 #define INFO(x, ...)
gsfan 5:78943b3945b5 61 #endif
gsfan 5:78943b3945b5 62
gsfan 5:78943b3945b5 63
gsfan 5:78943b3945b5 64 class GSwifi
gsfan 5:78943b3945b5 65 {
gsfan 5:78943b3945b5 66
gsfan 5:78943b3945b5 67 public:
gsfan 5:78943b3945b5 68
gsfan 5:78943b3945b5 69 enum Security {
gsfan 5:78943b3945b5 70 SEC_AUTO = 0,
gsfan 5:78943b3945b5 71 SEC_NONE = 0,
gsfan 5:78943b3945b5 72 SEC_OPEN = 1,
gsfan 5:78943b3945b5 73 SEC_WEP = 2,
gsfan 5:78943b3945b5 74 SEC_WPA_PSK = 4,
gsfan 5:78943b3945b5 75 SEC_WPA2_PSK = 8,
gsfan 5:78943b3945b5 76 SEC_WPA_ENT = 16,
gsfan 5:78943b3945b5 77 SEC_WPA2_ENT = 32,
gsfan 5:78943b3945b5 78 SEC_WPS_BUTTON = 64,
gsfan 5:78943b3945b5 79 };
gsfan 5:78943b3945b5 80
gsfan 5:78943b3945b5 81 enum Protocol {
gsfan 5:78943b3945b5 82 PROTO_UDP = 0,
gsfan 5:78943b3945b5 83 PROTO_TCP = 1,
gsfan 5:78943b3945b5 84 PROTO_HTTPGET,
gsfan 5:78943b3945b5 85 PROTO_HTTPPOST,
gsfan 5:78943b3945b5 86 PROTO_HTTPD,
gsfan 5:78943b3945b5 87 };
gsfan 5:78943b3945b5 88
gsfan 5:78943b3945b5 89 enum Response {
gsfan 5:78943b3945b5 90 RES_NULL,
gsfan 5:78943b3945b5 91 RES_NORMAL,
gsfan 5:78943b3945b5 92 RES_CONNECT,
gsfan 5:78943b3945b5 93 RES_WPS,
gsfan 5:78943b3945b5 94 RES_MACADDRESS,
gsfan 5:78943b3945b5 95 RES_DHCP,
gsfan 5:78943b3945b5 96 RES_DNSLOOKUP,
gsfan 5:78943b3945b5 97 RES_HTTP,
gsfan 5:78943b3945b5 98 RES_RSSI,
gsfan 5:78943b3945b5 99 RES_TIME,
gsfan 5:78943b3945b5 100 RES_OK,
gsfan 5:78943b3945b5 101 RES_FAILURE,
gsfan 5:78943b3945b5 102 };
gsfan 5:78943b3945b5 103
gsfan 5:78943b3945b5 104 enum Mode {
gsfan 5:78943b3945b5 105 MODE_COMMAND,
gsfan 5:78943b3945b5 106 MODE_DATA_RX,
gsfan 5:78943b3945b5 107 MODE_DATA_RXUDP,
gsfan 5:78943b3945b5 108 MODE_DATA_RXHTTP,
gsfan 5:78943b3945b5 109 };
gsfan 5:78943b3945b5 110
gsfan 5:78943b3945b5 111 enum Status {
gsfan 5:78943b3945b5 112 STAT_READY,
gsfan 5:78943b3945b5 113 STAT_STANDBY,
gsfan 5:78943b3945b5 114 STAT_WAKEUP,
gsfan 5:78943b3945b5 115 STAT_DEEPSLEEP,
gsfan 5:78943b3945b5 116 };
gsfan 5:78943b3945b5 117
gsfan 5:78943b3945b5 118 /*
gsfan 5:78943b3945b5 119 * Constructor
gsfan 5:78943b3945b5 120 *
gsfan 5:78943b3945b5 121 * @param tx mbed pin to use for tx line of Serial interface
gsfan 5:78943b3945b5 122 * @param rx mbed pin to use for rx line of Serial interface
gsfan 5:78943b3945b5 123 * \param cts mbed pin to use for cts line of Serial interface
gsfan 5:78943b3945b5 124 * \param rts mbed pin to use for rts line of Serial interface
gsfan 5:78943b3945b5 125 * @param reset reset pin of the wifi module ()
gsfan 5:78943b3945b5 126 * @param ssid ssid of the network
gsfan 5:78943b3945b5 127 * @param phrase WEP or WPA key
gsfan 5:78943b3945b5 128 * @param sec Security type (NONE, WEP_128 or WPA)
gsfan 5:78943b3945b5 129 */
gsfan 5:78943b3945b5 130 GSwifi( PinName tx, PinName rx, PinName cts, PinName rts, PinName reset, const char * ssid, const char * phrase, Security sec);
gsfan 5:78943b3945b5 131
gsfan 5:78943b3945b5 132 /*
gsfan 5:78943b3945b5 133 * Connect the wifi module to the ssid contained in the constructor.
gsfan 5:78943b3945b5 134 *
gsfan 5:78943b3945b5 135 * @return true if connected, false otherwise
gsfan 5:78943b3945b5 136 */
gsfan 5:78943b3945b5 137 bool join();
gsfan 5:78943b3945b5 138
gsfan 5:78943b3945b5 139 /*
gsfan 5:78943b3945b5 140 * Disconnect the wifly module from the access point
gsfan 5:78943b3945b5 141 *
gsfan 5:78943b3945b5 142 * @ returns true if successful
gsfan 5:78943b3945b5 143 */
gsfan 5:78943b3945b5 144 bool disconnect();
gsfan 5:78943b3945b5 145
gsfan 5:78943b3945b5 146 /*
gsfan 5:78943b3945b5 147 * Reset the wifi module
gsfan 5:78943b3945b5 148 */
gsfan 5:78943b3945b5 149 void reset();
gsfan 5:78943b3945b5 150
gsfan 5:78943b3945b5 151 /*
gsfan 5:78943b3945b5 152 * Check if characters are available
gsfan 5:78943b3945b5 153 *
gsfan 5:78943b3945b5 154 * @return number of available characters
gsfan 5:78943b3945b5 155 */
gsfan 5:78943b3945b5 156 int readable(int cid);
gsfan 5:78943b3945b5 157
gsfan 5:78943b3945b5 158 /*
gsfan 5:78943b3945b5 159 * Check if characters are available
gsfan 5:78943b3945b5 160 *
gsfan 5:78943b3945b5 161 * @return number of available characters
gsfan 5:78943b3945b5 162 */
gsfan 5:78943b3945b5 163 int writeable();
gsfan 5:78943b3945b5 164
gsfan 5:78943b3945b5 165 /*
gsfan 5:78943b3945b5 166 * Check if a tcp link is active
gsfan 5:78943b3945b5 167 *
gsfan 5:78943b3945b5 168 * @returns true if successful
gsfan 5:78943b3945b5 169 */
gsfan 5:78943b3945b5 170 bool is_connected(int cid);
gsfan 5:78943b3945b5 171
gsfan 5:78943b3945b5 172 /*
gsfan 5:78943b3945b5 173 * Read a character
gsfan 5:78943b3945b5 174 *
gsfan 5:78943b3945b5 175 * @return the character read
gsfan 5:78943b3945b5 176 */
gsfan 5:78943b3945b5 177 char getc(int cid);
gsfan 5:78943b3945b5 178
gsfan 5:78943b3945b5 179 /*
gsfan 5:78943b3945b5 180 * Flush the buffer
gsfan 5:78943b3945b5 181 */
gsfan 5:78943b3945b5 182 void flush(int cid);
gsfan 5:78943b3945b5 183
gsfan 5:78943b3945b5 184 /*
gsfan 5:78943b3945b5 185 * Write a character
gsfan 5:78943b3945b5 186 *
gsfan 5:78943b3945b5 187 * @param the character which will be written
gsfan 5:78943b3945b5 188 */
gsfan 5:78943b3945b5 189 int putc(char c);
gsfan 5:78943b3945b5 190
gsfan 5:78943b3945b5 191 /*
gsfan 5:78943b3945b5 192 * 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.
gsfan 5:78943b3945b5 193 * Useful to send a command to the module and wait a response.
gsfan 5:78943b3945b5 194 *
gsfan 5:78943b3945b5 195 *
gsfan 5:78943b3945b5 196 * @param str string to be sent
gsfan 5:78943b3945b5 197 * @param len string length
gsfan 5:78943b3945b5 198 * @param ACK string which must be acknowledge by the wifi module. If ACK == NULL, no string has to be acknoledged. (default: "NO")
gsfan 5:78943b3945b5 199 * @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)
gsfan 5:78943b3945b5 200 *
gsfan 5:78943b3945b5 201 * @return true if ACK has been found in the response from the wifi module. False otherwise or if there is no response in 5s.
gsfan 5:78943b3945b5 202 */
gsfan 5:78943b3945b5 203 int send(const char * str, int len, Response res = RES_NULL, int timeout = DEFAULT_WAIT_RESP_TIMEOUT);
gsfan 5:78943b3945b5 204
gsfan 5:78943b3945b5 205 /*
gsfan 5:78943b3945b5 206 * Send a command to the wify module. Check if the module is in command mode. If not enter in command mode
gsfan 5:78943b3945b5 207 *
gsfan 5:78943b3945b5 208 * @param str string to be sent
gsfan 5:78943b3945b5 209 * @param ACK string which must be acknowledge by the wifi module. If ACK == NULL, no string has to be acknoledged. (default: "NO")
gsfan 5:78943b3945b5 210 * @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)
gsfan 5:78943b3945b5 211 *
gsfan 5:78943b3945b5 212 * @returns true if successful
gsfan 5:78943b3945b5 213 */
gsfan 5:78943b3945b5 214 bool sendCommand(const char * cmd, Response res = RES_NORMAL, int timeout = DEFAULT_WAIT_RESP_TIMEOUT);
gsfan 5:78943b3945b5 215
gsfan 5:78943b3945b5 216 /*
gsfan 5:78943b3945b5 217 * Return true if the module is using dhcp
gsfan 5:78943b3945b5 218 *
gsfan 5:78943b3945b5 219 * @returns true if the module is using dhcp
gsfan 5:78943b3945b5 220 */
gsfan 5:78943b3945b5 221 bool isDHCP() {
gsfan 5:78943b3945b5 222 return _state.dhcp;
gsfan 5:78943b3945b5 223 }
gsfan 5:78943b3945b5 224
gsfan 5:78943b3945b5 225 bool gethostbyname(const char * host, char * ip);
gsfan 5:78943b3945b5 226
gsfan 5:78943b3945b5 227 static GSwifi * getInstance() {
gsfan 5:78943b3945b5 228 return _inst;
gsfan 5:78943b3945b5 229 };
gsfan 5:78943b3945b5 230
gsfan 5:78943b3945b5 231 bool readRemote(int cid, char **ip, int *port);
gsfan 5:78943b3945b5 232 int readCID ();
gsfan 5:78943b3945b5 233 int readACID ();
gsfan 5:78943b3945b5 234
gsfan 5:78943b3945b5 235 protected:
gsfan 5:78943b3945b5 236 Serial _uart;
gsfan 5:78943b3945b5 237 bool _rts;
gsfan 5:78943b3945b5 238 DigitalOut _reset;
gsfan 5:78943b3945b5 239 char _phrase[30];
gsfan 5:78943b3945b5 240 char _ssid[30];
gsfan 5:78943b3945b5 241 char _ip[16];
gsfan 5:78943b3945b5 242 char _netmask[16];
gsfan 5:78943b3945b5 243 char _gateway[16];
gsfan 5:78943b3945b5 244 char _nameserver[16];
gsfan 5:78943b3945b5 245 char _resolv[16];
gsfan 5:78943b3945b5 246 int _rssi;
gsfan 5:78943b3945b5 247 time_t _time;
gsfan 5:78943b3945b5 248 CircBuffer<char> _buf_gswifi;
gsfan 5:78943b3945b5 249
gsfan 5:78943b3945b5 250 static GSwifi * _inst;
gsfan 5:78943b3945b5 251
gsfan 5:78943b3945b5 252 void attach_rx(bool null);
gsfan 5:78943b3945b5 253 void handler_rx(void);
gsfan 5:78943b3945b5 254
gsfan 5:78943b3945b5 255 int x2i (char c);
gsfan 5:78943b3945b5 256 char i2x (int i);
gsfan 5:78943b3945b5 257 void parseResponse ();
gsfan 5:78943b3945b5 258 void parseCmdResponse (char *buf);
gsfan 5:78943b3945b5 259
gsfan 5:78943b3945b5 260 typedef struct STATE {
gsfan 5:78943b3945b5 261 bool associated;
gsfan 5:78943b3945b5 262 bool dhcp;
gsfan 5:78943b3945b5 263 Security sec;
gsfan 5:78943b3945b5 264 Mode mode;
gsfan 5:78943b3945b5 265 Status status;
gsfan 5:78943b3945b5 266 bool escape;
gsfan 5:78943b3945b5 267 int cid, acid;
gsfan 5:78943b3945b5 268 volatile Response cmdres, retres;
gsfan 5:78943b3945b5 269 int n;
gsfan 5:78943b3945b5 270 } State;
gsfan 5:78943b3945b5 271
gsfan 5:78943b3945b5 272 State _state;
gsfan 5:78943b3945b5 273
gsfan 5:78943b3945b5 274 typedef struct CONNECTION {
gsfan 5:78943b3945b5 275 bool connected;
gsfan 5:78943b3945b5 276 char ip[16];
gsfan 5:78943b3945b5 277 int port;
gsfan 5:78943b3945b5 278 CircBuffer<char> *buf;
gsfan 5:78943b3945b5 279 int parent;
gsfan 5:78943b3945b5 280 } Connection;
gsfan 5:78943b3945b5 281
gsfan 5:78943b3945b5 282 Connection _con[16];
gsfan 5:78943b3945b5 283 };
gsfan 5:78943b3945b5 284
samux 1:fb4494783863 285 #endif