private fork

Fork of GSwifiInterface by gs fan

Committer:
gsfan
Date:
Wed Jan 30 05:52:14 2013 +0000
Revision:
6:6a6396b56405
Parent:
5:78943b3945b5
Child:
7:f94c59d0c735
Child:
8:64184a968e3b
fix

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_WREGDOMAIN "2" // 0:FCC, 1:ETSI, 2:TELEC
gsfan 6:6a6396b56405 42 #define MAX_TRY_JOIN 3
gsfan 5:78943b3945b5 43
gsfan 5:78943b3945b5 44
gsfan 5:78943b3945b5 45 //Debug is disabled by default
gsfan 5:78943b3945b5 46 #if (0 && !defined(TARGET_LPC11U24))
gsfan 5:78943b3945b5 47 #define DBG(x, ...) std::printf("[GSwifi : DBG]" x "\r\n", ##__VA_ARGS__);
gsfan 5:78943b3945b5 48 #define WARN(x, ...) std::printf("[GSwifi : WARN]" x "\r\n", ##__VA_ARGS__);
gsfan 5:78943b3945b5 49 #define ERR(x, ...) std::printf("[GSwifi : ERR]" x "\r\n", ##__VA_ARGS__);
gsfan 5:78943b3945b5 50 #else
gsfan 5:78943b3945b5 51 #define DBG(x, ...)
gsfan 5:78943b3945b5 52 #define WARN(x, ...)
gsfan 5:78943b3945b5 53 #define ERR(x, ...)
gsfan 5:78943b3945b5 54 #endif
gsfan 5:78943b3945b5 55
gsfan 6:6a6396b56405 56 #if (0 && !defined(TARGET_LPC11U24))
gsfan 5:78943b3945b5 57 #define INFO(x, ...) printf("[GSwifi : INFO]\r\n"x"\r\n", ##__VA_ARGS__);
gsfan 5:78943b3945b5 58 #else
gsfan 5:78943b3945b5 59 #define INFO(x, ...)
gsfan 5:78943b3945b5 60 #endif
gsfan 5:78943b3945b5 61
gsfan 5:78943b3945b5 62
gsfan 5:78943b3945b5 63 class GSwifi
gsfan 5:78943b3945b5 64 {
gsfan 5:78943b3945b5 65
gsfan 5:78943b3945b5 66 public:
gsfan 5:78943b3945b5 67
gsfan 5:78943b3945b5 68 enum Security {
gsfan 5:78943b3945b5 69 SEC_AUTO = 0,
gsfan 5:78943b3945b5 70 SEC_NONE = 0,
gsfan 5:78943b3945b5 71 SEC_OPEN = 1,
gsfan 5:78943b3945b5 72 SEC_WEP = 2,
gsfan 5:78943b3945b5 73 SEC_WPA_PSK = 4,
gsfan 5:78943b3945b5 74 SEC_WPA2_PSK = 8,
gsfan 5:78943b3945b5 75 SEC_WPA_ENT = 16,
gsfan 5:78943b3945b5 76 SEC_WPA2_ENT = 32,
gsfan 5:78943b3945b5 77 SEC_WPS_BUTTON = 64,
gsfan 5:78943b3945b5 78 };
gsfan 5:78943b3945b5 79
gsfan 5:78943b3945b5 80 enum Protocol {
gsfan 5:78943b3945b5 81 PROTO_UDP = 0,
gsfan 5:78943b3945b5 82 PROTO_TCP = 1,
gsfan 5:78943b3945b5 83 PROTO_HTTPGET,
gsfan 5:78943b3945b5 84 PROTO_HTTPPOST,
gsfan 5:78943b3945b5 85 PROTO_HTTPD,
gsfan 5:78943b3945b5 86 };
gsfan 5:78943b3945b5 87
gsfan 5:78943b3945b5 88 enum Response {
gsfan 5:78943b3945b5 89 RES_NULL,
gsfan 5:78943b3945b5 90 RES_NORMAL,
gsfan 5:78943b3945b5 91 RES_CONNECT,
gsfan 5:78943b3945b5 92 RES_WPS,
gsfan 5:78943b3945b5 93 RES_MACADDRESS,
gsfan 5:78943b3945b5 94 RES_DHCP,
gsfan 5:78943b3945b5 95 RES_DNSLOOKUP,
gsfan 5:78943b3945b5 96 RES_HTTP,
gsfan 5:78943b3945b5 97 RES_RSSI,
gsfan 5:78943b3945b5 98 RES_TIME,
gsfan 5:78943b3945b5 99 RES_OK,
gsfan 5:78943b3945b5 100 RES_FAILURE,
gsfan 5:78943b3945b5 101 };
gsfan 5:78943b3945b5 102
gsfan 5:78943b3945b5 103 enum Mode {
gsfan 5:78943b3945b5 104 MODE_COMMAND,
gsfan 5:78943b3945b5 105 MODE_DATA_RX,
gsfan 5:78943b3945b5 106 MODE_DATA_RXUDP,
gsfan 5:78943b3945b5 107 MODE_DATA_RXHTTP,
gsfan 5:78943b3945b5 108 };
gsfan 5:78943b3945b5 109
gsfan 5:78943b3945b5 110 enum Status {
gsfan 5:78943b3945b5 111 STAT_READY,
gsfan 5:78943b3945b5 112 STAT_STANDBY,
gsfan 5:78943b3945b5 113 STAT_WAKEUP,
gsfan 5:78943b3945b5 114 STAT_DEEPSLEEP,
gsfan 5:78943b3945b5 115 };
gsfan 5:78943b3945b5 116
gsfan 5:78943b3945b5 117 /*
gsfan 5:78943b3945b5 118 * Constructor
gsfan 5:78943b3945b5 119 *
gsfan 5:78943b3945b5 120 * @param tx mbed pin to use for tx line of Serial interface
gsfan 5:78943b3945b5 121 * @param rx mbed pin to use for rx line of Serial interface
gsfan 5:78943b3945b5 122 * \param cts mbed pin to use for cts line of Serial interface
gsfan 5:78943b3945b5 123 * \param rts mbed pin to use for rts line of Serial interface
gsfan 5:78943b3945b5 124 * @param reset reset pin of the wifi module ()
gsfan 5:78943b3945b5 125 * @param ssid ssid of the network
gsfan 5:78943b3945b5 126 * @param phrase WEP or WPA key
gsfan 5:78943b3945b5 127 * @param sec Security type (NONE, WEP_128 or WPA)
gsfan 5:78943b3945b5 128 */
gsfan 5:78943b3945b5 129 GSwifi( PinName tx, PinName rx, PinName cts, PinName rts, PinName reset, const char * ssid, const char * phrase, Security sec);
gsfan 5:78943b3945b5 130
gsfan 5:78943b3945b5 131 /*
gsfan 5:78943b3945b5 132 * Connect the wifi module to the ssid contained in the constructor.
gsfan 5:78943b3945b5 133 *
gsfan 5:78943b3945b5 134 * @return true if connected, false otherwise
gsfan 5:78943b3945b5 135 */
gsfan 5:78943b3945b5 136 bool join();
gsfan 5:78943b3945b5 137
gsfan 5:78943b3945b5 138 /*
gsfan 5:78943b3945b5 139 * Disconnect the wifly module from the access point
gsfan 5:78943b3945b5 140 *
gsfan 5:78943b3945b5 141 * @ returns true if successful
gsfan 5:78943b3945b5 142 */
gsfan 5:78943b3945b5 143 bool disconnect();
gsfan 5:78943b3945b5 144
gsfan 5:78943b3945b5 145 /*
gsfan 5:78943b3945b5 146 * Reset the wifi module
gsfan 5:78943b3945b5 147 */
gsfan 5:78943b3945b5 148 void reset();
gsfan 5:78943b3945b5 149
gsfan 5:78943b3945b5 150 /*
gsfan 5:78943b3945b5 151 * Check if characters are available
gsfan 5:78943b3945b5 152 *
gsfan 5:78943b3945b5 153 * @return number of available characters
gsfan 5:78943b3945b5 154 */
gsfan 5:78943b3945b5 155 int readable(int cid);
gsfan 5:78943b3945b5 156
gsfan 5:78943b3945b5 157 /*
gsfan 5:78943b3945b5 158 * Check if characters are available
gsfan 5:78943b3945b5 159 *
gsfan 5:78943b3945b5 160 * @return number of available characters
gsfan 5:78943b3945b5 161 */
gsfan 5:78943b3945b5 162 int writeable();
gsfan 5:78943b3945b5 163
gsfan 5:78943b3945b5 164 /*
gsfan 5:78943b3945b5 165 * Check if a tcp link is active
gsfan 5:78943b3945b5 166 *
gsfan 5:78943b3945b5 167 * @returns true if successful
gsfan 5:78943b3945b5 168 */
gsfan 5:78943b3945b5 169 bool is_connected(int cid);
gsfan 5:78943b3945b5 170
gsfan 5:78943b3945b5 171 /*
gsfan 5:78943b3945b5 172 * Read a character
gsfan 5:78943b3945b5 173 *
gsfan 5:78943b3945b5 174 * @return the character read
gsfan 5:78943b3945b5 175 */
gsfan 5:78943b3945b5 176 char getc(int cid);
gsfan 5:78943b3945b5 177
gsfan 5:78943b3945b5 178 /*
gsfan 5:78943b3945b5 179 * Flush the buffer
gsfan 5:78943b3945b5 180 */
gsfan 5:78943b3945b5 181 void flush(int cid);
gsfan 5:78943b3945b5 182
gsfan 5:78943b3945b5 183 /*
gsfan 5:78943b3945b5 184 * Write a character
gsfan 5:78943b3945b5 185 *
gsfan 5:78943b3945b5 186 * @param the character which will be written
gsfan 5:78943b3945b5 187 */
gsfan 5:78943b3945b5 188 int putc(char c);
gsfan 5:78943b3945b5 189
gsfan 5:78943b3945b5 190 /*
gsfan 5:78943b3945b5 191 * 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 192 * Useful to send a command to the module and wait a response.
gsfan 5:78943b3945b5 193 *
gsfan 5:78943b3945b5 194 *
gsfan 5:78943b3945b5 195 * @param str string to be sent
gsfan 5:78943b3945b5 196 * @param len string length
gsfan 5:78943b3945b5 197 * @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 198 * @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 199 *
gsfan 5:78943b3945b5 200 * @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 201 */
gsfan 5:78943b3945b5 202 int send(const char * str, int len, Response res = RES_NULL, int timeout = DEFAULT_WAIT_RESP_TIMEOUT);
gsfan 5:78943b3945b5 203
gsfan 5:78943b3945b5 204 /*
gsfan 5:78943b3945b5 205 * Send a command to the wify module. Check if the module is in command mode. If not enter in command mode
gsfan 5:78943b3945b5 206 *
gsfan 5:78943b3945b5 207 * @param str string to be sent
gsfan 5:78943b3945b5 208 * @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 209 * @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 210 *
gsfan 5:78943b3945b5 211 * @returns true if successful
gsfan 5:78943b3945b5 212 */
gsfan 5:78943b3945b5 213 bool sendCommand(const char * cmd, Response res = RES_NORMAL, int timeout = DEFAULT_WAIT_RESP_TIMEOUT);
gsfan 5:78943b3945b5 214
gsfan 5:78943b3945b5 215 /*
gsfan 5:78943b3945b5 216 * Return true if the module is using dhcp
gsfan 5:78943b3945b5 217 *
gsfan 5:78943b3945b5 218 * @returns true if the module is using dhcp
gsfan 5:78943b3945b5 219 */
gsfan 5:78943b3945b5 220 bool isDHCP() {
gsfan 5:78943b3945b5 221 return _state.dhcp;
gsfan 5:78943b3945b5 222 }
gsfan 5:78943b3945b5 223
gsfan 5:78943b3945b5 224 bool gethostbyname(const char * host, char * ip);
gsfan 5:78943b3945b5 225
gsfan 5:78943b3945b5 226 static GSwifi * getInstance() {
gsfan 5:78943b3945b5 227 return _inst;
gsfan 5:78943b3945b5 228 };
gsfan 5:78943b3945b5 229
gsfan 5:78943b3945b5 230 bool readRemote(int cid, char **ip, int *port);
gsfan 5:78943b3945b5 231 int readCID ();
gsfan 5:78943b3945b5 232 int readACID ();
gsfan 5:78943b3945b5 233
gsfan 5:78943b3945b5 234 protected:
gsfan 5:78943b3945b5 235 Serial _uart;
gsfan 5:78943b3945b5 236 bool _rts;
gsfan 5:78943b3945b5 237 DigitalOut _reset;
gsfan 5:78943b3945b5 238 char _phrase[30];
gsfan 5:78943b3945b5 239 char _ssid[30];
gsfan 5:78943b3945b5 240 char _ip[16];
gsfan 5:78943b3945b5 241 char _netmask[16];
gsfan 5:78943b3945b5 242 char _gateway[16];
gsfan 5:78943b3945b5 243 char _nameserver[16];
gsfan 5:78943b3945b5 244 char _resolv[16];
gsfan 5:78943b3945b5 245 int _rssi;
gsfan 5:78943b3945b5 246 time_t _time;
gsfan 5:78943b3945b5 247 CircBuffer<char> _buf_gswifi;
gsfan 5:78943b3945b5 248
gsfan 5:78943b3945b5 249 static GSwifi * _inst;
gsfan 5:78943b3945b5 250
gsfan 5:78943b3945b5 251 void attach_rx(bool null);
gsfan 5:78943b3945b5 252 void handler_rx(void);
gsfan 5:78943b3945b5 253
gsfan 5:78943b3945b5 254 int x2i (char c);
gsfan 5:78943b3945b5 255 char i2x (int i);
gsfan 5:78943b3945b5 256 void parseResponse ();
gsfan 5:78943b3945b5 257 void parseCmdResponse (char *buf);
gsfan 5:78943b3945b5 258
gsfan 5:78943b3945b5 259 typedef struct STATE {
gsfan 5:78943b3945b5 260 bool associated;
gsfan 5:78943b3945b5 261 bool dhcp;
gsfan 5:78943b3945b5 262 Security sec;
gsfan 5:78943b3945b5 263 Mode mode;
gsfan 5:78943b3945b5 264 Status status;
gsfan 5:78943b3945b5 265 bool escape;
gsfan 5:78943b3945b5 266 int cid, acid;
gsfan 5:78943b3945b5 267 volatile Response cmdres, retres;
gsfan 5:78943b3945b5 268 int n;
gsfan 5:78943b3945b5 269 } State;
gsfan 5:78943b3945b5 270
gsfan 5:78943b3945b5 271 State _state;
gsfan 5:78943b3945b5 272
gsfan 5:78943b3945b5 273 typedef struct CONNECTION {
gsfan 5:78943b3945b5 274 bool connected;
gsfan 5:78943b3945b5 275 char ip[16];
gsfan 5:78943b3945b5 276 int port;
gsfan 5:78943b3945b5 277 CircBuffer<char> *buf;
gsfan 5:78943b3945b5 278 int parent;
gsfan 5:78943b3945b5 279 } Connection;
gsfan 5:78943b3945b5 280
gsfan 5:78943b3945b5 281 Connection _con[16];
gsfan 5:78943b3945b5 282 };
gsfan 5:78943b3945b5 283
samux 1:fb4494783863 284 #endif