private fork

Fork of GSwifiInterface by gs fan

Revision:
5:78943b3945b5
Parent:
4:0bcec6272784
Child:
6:6a6396b56405
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/GSwifi/GSwifi.h	Sun Jan 27 14:31:19 2013 +0000
@@ -0,0 +1,285 @@
+/* Copyright (C) 2012 mbed.org, MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * @section DESCRIPTION
+ *
+ * GainSpan GS1011, Wi-Fi module
+ *
+ *   http://www.gainspan.com/modules
+ */
+/* Copyright (C) 2013 gsfan, MIT License
+ *  port to the GainSpan Wi-FI module GS1011
+ */
+
+#ifndef GSwifi_H
+#define GSwifi_H
+
+#include "mbed.h"
+#include "CBuffer.h"
+
+#define DEFAULT_WAIT_RESP_TIMEOUT 500
+#define CFG_TIMEOUT 10000 // ms
+#define CFG_TIMEOUT2 30000 // ms
+#define CFG_CMD_SIZE 100
+#define CFG_DATA_SIZE 1000
+
+#define CFG_UART_BAUD 9600
+#define CFG_DNSNAME "setup.local"
+#define CFG_WREGDOMAIN "2" // 0:FCC, 1:ETSI, 2:TELEC
+#define MAX_TRY_JOIN 1
+
+
+//Debug is disabled by default
+#if (0 && !defined(TARGET_LPC11U24))
+#define DBG(x, ...) std::printf("[GSwifi : DBG]" x "\r\n", ##__VA_ARGS__);
+#define WARN(x, ...) std::printf("[GSwifi : WARN]" x "\r\n", ##__VA_ARGS__);
+#define ERR(x, ...) std::printf("[GSwifi : ERR]" x "\r\n", ##__VA_ARGS__);
+#else
+#define DBG(x, ...)
+#define WARN(x, ...)
+#define ERR(x, ...)
+#endif
+
+#if !defined(TARGET_LPC11U24)
+#define INFO(x, ...) printf("[GSwifi : INFO]\r\n"x"\r\n", ##__VA_ARGS__);
+#else
+#define INFO(x, ...)
+#endif
+
+
+class GSwifi
+{
+
+public:
+
+    enum Security {
+        SEC_AUTO = 0,
+        SEC_NONE = 0,
+        SEC_OPEN = 1,
+        SEC_WEP = 2,
+        SEC_WPA_PSK = 4,
+        SEC_WPA2_PSK = 8,
+        SEC_WPA_ENT = 16,
+        SEC_WPA2_ENT = 32,
+        SEC_WPS_BUTTON = 64,
+    };
+
+    enum Protocol {
+        PROTO_UDP = 0,
+        PROTO_TCP = 1,
+        PROTO_HTTPGET,
+        PROTO_HTTPPOST,
+        PROTO_HTTPD,
+    };
+
+    enum Response {
+        RES_NULL,
+        RES_NORMAL,
+        RES_CONNECT,
+        RES_WPS,
+        RES_MACADDRESS,
+        RES_DHCP,
+        RES_DNSLOOKUP,
+        RES_HTTP,
+        RES_RSSI,
+        RES_TIME,
+        RES_OK,
+        RES_FAILURE,
+    };
+
+    enum Mode {
+        MODE_COMMAND,
+        MODE_DATA_RX,
+        MODE_DATA_RXUDP,
+        MODE_DATA_RXHTTP,
+    };
+
+    enum Status {
+        STAT_READY,
+        STAT_STANDBY,
+        STAT_WAKEUP,
+        STAT_DEEPSLEEP,
+    };
+
+    /*
+    * Constructor
+    *
+    * @param tx mbed pin to use for tx line of Serial interface
+    * @param rx mbed pin to use for rx line of Serial interface
+    * \param cts mbed pin to use for cts line of Serial interface
+    * \param rts mbed pin to use for rts line of Serial interface
+    * @param reset reset pin of the wifi module ()
+    * @param ssid ssid of the network
+    * @param phrase WEP or WPA key
+    * @param sec Security type (NONE, WEP_128 or WPA)
+    */
+    GSwifi(  PinName tx, PinName rx, PinName cts, PinName rts, PinName reset, const char * ssid, const char * phrase, Security sec);
+
+    /*
+    * Connect the wifi module to the ssid contained in the constructor.
+    *
+    * @return true if connected, false otherwise
+    */
+    bool join();
+
+    /*
+    * Disconnect the wifly module from the access point
+    *
+    * @ returns true if successful
+    */
+    bool disconnect();
+
+    /*
+    * Reset the wifi module
+    */
+    void reset();
+    
+    /*
+    * Check if characters are available
+    *
+    * @return number of available characters
+    */
+    int readable(int cid);
+
+    /*
+    * Check if characters are available
+    *
+    * @return number of available characters
+    */
+    int writeable();
+
+    /*
+    * Check if a tcp link is active
+    *
+    * @returns true if successful
+    */
+    bool is_connected(int cid);
+
+    /*
+    * Read a character
+    *
+    * @return the character read
+    */
+    char getc(int cid);
+
+    /*
+    * Flush the buffer
+    */
+    void flush(int cid);
+
+    /*
+    * Write a character
+    *
+    * @param the character which will be written
+    */
+    int putc(char c);
+
+    /*
+    * 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.
+    * Useful to send a command to the module and wait a response.
+    *
+    *
+    * @param str string to be sent
+    * @param len string length
+    * @param ACK string which must be acknowledge by the wifi module. If ACK == NULL, no string has to be acknoledged. (default: "NO")
+    * @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)
+    *
+    * @return true if ACK has been found in the response from the wifi module. False otherwise or if there is no response in 5s.
+    */
+    int send(const char * str, int len, Response res = RES_NULL, int timeout = DEFAULT_WAIT_RESP_TIMEOUT);
+
+    /*
+    * Send a command to the wify module. Check if the module is in command mode. If not enter in command mode
+    *
+    * @param str string to be sent
+    * @param ACK string which must be acknowledge by the wifi module. If ACK == NULL, no string has to be acknoledged. (default: "NO")
+    * @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)
+    *
+    * @returns true if successful
+    */
+    bool sendCommand(const char * cmd, Response res = RES_NORMAL, int timeout = DEFAULT_WAIT_RESP_TIMEOUT);
+    
+    /*
+    * Return true if the module is using dhcp
+    *
+    * @returns true if the module is using dhcp
+    */
+    bool isDHCP() {
+        return _state.dhcp;
+    }
+
+    bool gethostbyname(const char * host, char * ip);
+
+    static GSwifi * getInstance() {
+        return _inst;
+    };
+
+    bool readRemote(int cid, char **ip, int *port);
+    int readCID ();
+    int readACID ();
+
+protected:
+    Serial _uart;
+    bool _rts;
+    DigitalOut _reset;
+    char _phrase[30];
+    char _ssid[30];
+    char _ip[16];
+    char _netmask[16];
+    char _gateway[16];
+    char _nameserver[16];
+    char _resolv[16];
+    int _rssi;
+    time_t _time;
+    CircBuffer<char> _buf_gswifi;
+
+    static GSwifi * _inst;
+
+    void attach_rx(bool null);
+    void handler_rx(void);
+
+    int x2i (char c);
+    char i2x (int i);
+    void parseResponse ();
+    void parseCmdResponse (char *buf);
+
+    typedef struct STATE {
+        bool associated;
+        bool dhcp;
+        Security sec;
+        Mode mode;
+        Status status;
+        bool escape;
+        int cid, acid;
+        volatile Response cmdres, retres;
+        int n;
+    } State;
+
+    State _state;
+
+    typedef struct CONNECTION {
+        bool connected;
+        char ip[16];
+        int port;
+        CircBuffer<char> *buf;
+        int parent;
+    } Connection;
+
+    Connection _con[16];
+};
+
+#endif
\ No newline at end of file