GSwifiInterface library (interface for GainSpan Wi-Fi GS1011 modules) Please see https://mbed.org/users/gsfan/notebook/GSwifiInterface/

Dependents:   GSwifiInterface_HelloWorld GSwifiInterface_HelloServo GSwifiInterface_UDPEchoServer GSwifiInterface_UDPEchoClient ... more

Fork of WiflyInterface by mbed official

GainSpan Wi-Fi library

The GS1011/GS2100 is an ultra low power 802.11b wireless module from GainSpan.

mbed RTOS supported.

/media/uploads/gsfan/gs_im_002.jpg /media/uploads/gsfan/gs1011m_2.jpg

ゲインスパン Wi-Fi モジュール ライブラリ

ゲインスパン社の低電力 Wi-Fiモジュール(無線LAN) GS1011/GS2100 シリーズ用のライブラリです。

mbed RTOS に対応しています。(mbed2.0)

GSwifi/GSwifi.h

Committer:
gsfan
Date:
2013-10-31
Revision:
9:4565d3d3d13b
Parent:
8:64184a968e3b
Child:
10:7d8134e7df01

File content as of revision 9:4565d3d3d13b:

/* 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
 */
/** @file
 * @brief Gainspan wi-fi module library for mbed
 * GS1011MIC, GS1011MIP, GainSpan WiFi Breakout, etc.
 */

#ifndef GSwifi_H
#define GSwifi_H

#include "mbed.h"
#include "rtos.h"
#include "RawSerial.h"
#include "CBuffer.h"
#include <ctype.h>
#include <stdlib.h>
#include <string.h>

//#define DEBUG

#define DEFAULT_WAIT_RESP_TIMEOUT 500
#define CFG_TIMEOUT 30000 // ms
#define CFG_CMD_SIZE 100
#if defined(TARGET_LPC1768) || defined(TARGET_LPC2368)
#define CFG_DATA_SIZE 1024
#elif defined(TARGET_LPC11U24)
#define CFG_DATA_SIZE 128
#elif defined(TARGET_LPC4088)
#define CFG_DATA_SIZE 4096
#endif


//#define CFG_UART_DIRECT
#define CFG_UART_BAUD 9600
#define CFG_WREGDOMAIN 2 // 0:FCC, 1:ETSI, 2:TELEC
#define CFG_DHCPNAME "mbed-gswifi"
#define CFG_DNSNAME "setup.local"
#define MAX_TRY_JOIN 3

#define MSG_TABLE_NUM 15
#define RES_TABLE_NUM 10

//Debug is disabled by default
#if defined(DEBUG) and (!defined(TARGET_LPC11U24))
#define DBG(x, ...) std::printf("[DBG]" x "\r\n", ##__VA_ARGS__);
#define WARN(x, ...) std::printf("[WARN]" x "\r\n", ##__VA_ARGS__);
#define ERR(x, ...) std::printf("[ERR]" x "\r\n", ##__VA_ARGS__);
#define INFO(x, ...) std::printf("[INFO]" x "\r\n", ##__VA_ARGS__);
#else
#define DBG(x, ...)
#define WARN(x, ...)
#define ERR(x, ...)
#define INFO(x, ...)
#endif


/** GSwifi class
 */
class GSwifi
{

public:

    /** Wi-Fi mode
     */
    enum WiFiMode {
        WM_INFRASTRUCTURE,
        WM_ADHOCK,
        WM_LIMITEDAP,
    };

    /** Wi-Fi security
     */
    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,
        SEC_WPS_PIN,
    };

    /** TCP/IP protocol
     */
    enum Protocol {
        PROTO_UDP = 0,
        PROTO_TCP = 1,
        PROTO_HTTPGET,
        PROTO_HTTPPOST,
        PROTO_HTTPD,
    };

    /** Client/Server
     */
    enum Type {
        TYPE_CLIENT = 0,
        TYPE_SERVER = 1,
    };

    enum Response {
        RES_NULL,
        RES_CONNECT,
        RES_WPS,
        RES_MACADDRESS,
        RES_DHCP,
        RES_DNSLOOKUP,
        RES_HTTP,
        RES_RSSI,
        RES_TIME,
        RES_STATUS,
    };

    enum Mode {
        MODE_COMMAND,
        MODE_CMDRESP,
        MODE_ESCAPE,
        MODE_DATA_RX,
        MODE_DATA_RX_BULK,
        MODE_DATA_RXUDP,
        MODE_DATA_RXUDP_BULK,
        MODE_DATA_RXHTTP,
        MODE_DATA_RAW,
    };

    enum Status {
        STAT_NONE,
        STAT_READY,
        STAT_STANDBY,
        STAT_WAKEUP,
        STAT_DEEPSLEEP,
    };

    // ----- GSwifi.cpp -----
    /** 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 alarm alarm pin of the wifi module
     * \param baud baud rate of Serial interface
     */
    GSwifi (PinName tx, PinName rx, PinName cts, PinName rts, PinName reset, PinName alarm, int baud);

    /** Connect the wifi module to the ssid contained in the constructor.
     * @param sec Security type (NONE, WEP_128 or WPA)
     * @param ssid ssid of the network
     * @param phrase WEP or WPA key
     * @return 0 if connected, -1 otherwise
     */
    int join (Security sec, const char *ssid, const char *phrase);

    /** Connect the wifi module to the adhock in the constructor.
     * @param sec Security type (NONE, WEP_128 or WPA)
     * @param ssid ssid of the network
     * @param phrase WEP or WPA key
     * @return 0 if connected, -1 otherwise
     */
    int adhock (Security sec, const char *ssid, const char *phrase);

    /** Connect the wifi module to the limited AP in the constructor.
     * @param sec Security type (NONE, WEP_128 or WPA)
     * @param ssid ssid of the network
     * @param phrase WEP or WPA key
     * @return 0 if connected, -1 otherwise
     */
    int limitedap (Security sec, const char *ssid, const char *phrase);

    /** Disconnect the wifi module from the access point
     * @returns 0 if successful
     */
    int disconnect ();
    
    /** Check if a tcp link is active
     * @returns true if successful
     */
    bool isConnected();

    int setSsid (Security sec, const char *ssid, const char *phrase);

    void poll ();

    static GSwifi * getInstance() {
        return _inst;
    };

    // ----- GSwifi_util.cpp -----
    int setRegion (int reg);

    /** RSSI
     * @return RSSI (dBm)
     */
    int getRssi ();

    /** power save mode
     * @param active rx radio 0:switched off, 1:always on
     * @param save power save 0:disable, 1:enable
     */
    int powerSave (int active, int save);

    /** RF power
     * @param power 0(high)-7(low)
     */
    int setRfPower (int power);

    /** set system time
     * @param time date time (UTC)
     */
    int setTime (time_t time);

    /** get system time
     * @return date time (UTC)
     */
    time_t getTime ();

    /** set NTP server
     * @param host SNTP server
     * @param sec time sync interval, 0:one time
     */
    int ntpdate (char *host, int sec = 0);

    /** GPIO output
     * @param port 10,11,30,31
     * @param out 0:set(high), 1:reset(low)
     */
    int setGpio (int port, int out);

    /** Web server
     */
    int provisioning (char *user, char *pass);

    /** standby mode
     * @param msec wakeup after
     * wakeup after msec or alarm1/2
     * core off, save to RTC ram
     */
    int standby (int msec);

    /** deep sleep mode
     */
    int deepSleep ();

    /** restore standby or deep sleep
     */
    int wakeup ();

    // ----- GSwifi_sock.cpp -----
    /** Resolv hostname
     * @param name hostname
     * @param ip resolved ip address
     */
    int getHostByName(const char * host, char * ip);

    /** TCP/UDP client
     * @return CID, -1:failure
     */
    int open (Protocol proto, const char *ip, int port, int src = 0);

    /** TCP/UDP server
     * @return CID, -1:failure
     */
    int listen (Protocol proto, int port);

    /** close client/server
     */
    int close (int cid);

    /** send data tcp(s/c), udp(c)
     */
    int send (int cid, const char *buf, int len);

    /*
     * send data udp(s)
     */
    int sendto (int cid, const char *buf, int len, const char *ip, int port);

    /** recv data tcp(s/c), udp(c)
     * @return length
     */
    int recv (int cid, char *buf, int len);

    /** recv data udp(s)
     * @return length
     */
    int recvfrom (int cid, char *buf, int len, char *ip, int *port);

    /** readable recv data
     */
    int readable (int cid);

    /** tcp/udp connected
     */
    bool isConnected (int cid);

    int accept (int cid);
    int getRemote(int cid, char **ip, int *port);

    // ----- GSwifi_http.cpp -----
    int httpGet (const char *host, int port, const char *uri, bool ssl = false, const char *user = NULL, const char *pwd = NULL);
    int httpPost (const char *host, int port, const char *uri, const char *body, bool ssl = false, const char *user = NULL, const char *pwd = NULL);
    int base64encode (const char *input, int length, char *output, int len);
    int urlencode (const char *str, char *buf, int len);
    int urldecode (const char *str, char *buf, int len);

    // ----- GSwifi_msg.cpp -----

    // ----- GSwifi_at.cpp -----
    /** Send a command to the wifi module. Check if the module is in command mode. If not enter in command mode
     * @param cmd string to be sent
     * @param res need response
     * @param timeout
     * @returns 0 if successful
     */
    int sendCommand(const char * cmd, Response res = RES_NULL, int timeout = DEFAULT_WAIT_RESP_TIMEOUT);

    /** Send a command to the wifi module. Check if the module is in command mode. If not enter in command mode
     * @param data string to be sent
     * @param res need response
     * @param timeout
     * @param cmd 
     * @returns 0 if successful
     */
    int sendData(const char * data, int len, int timeout = DEFAULT_WAIT_RESP_TIMEOUT, const char * cmd = NULL);


protected:

    static GSwifi * _inst;
#ifdef RTOS_H
    Thread *_threadPoll;
    Mutex _mutexUart;
#endif

//    Serial _gs;
    RawSerial _gs;
    int _baud;
    DigitalIn *_cts;
    DigitalOut *_rts;
    int _flow;
#if defined(TARGET_LPC1768) || defined(TARGET_LPC2368) || defined(TARGET_LPC4088)
    LPC_UART1_TypeDef *_uart;
#elif defined(TARGET_LPC11U24)
    LPC_USART_Type *_uart;
#endif
    DigitalInOut _reset;
    DigitalInOut *_alarm;

    struct STATE {
        WiFiMode wm;
        Security sec;
        char ssid[35];
        char pass[66];
        char ip[16];
        char netmask[16];
        char gateway[16];
        char nameserver[16];
        char mac[18];
        char resolv[16];
        char name[32];
        int rssi;
        bool dhcp;
        time_t time;

        bool associated;
        volatile Mode mode;
        volatile Status status;
        bool escape;
        volatile bool ok, failure;
        Response res;
        int cid, acid;
        int n;
        CircBuffer<char> *buf;
    } _state;

    struct CONNECTION {
        Protocol protocol;
        Type type;
        bool connected;
        char ip[16];
        int port;
        CircBuffer<char> *buf;
        volatile bool received;
        volatile int parent;
        volatile bool accept;
    } _con[16];


    // ----- GSwifi.cpp -----
#ifdef RTOS_H
    static void threadPoll (void const *args);
#endif

    // ----- GSwifi_util.cpp -----
    int x2i (char c);
    char i2x (int i);
    int from_hex (int ch);
    int to_hex (int code);

    // ----- GSwifi_msg.cpp -----
    void recvData (char c);
    int parseMessage ();
    void msgOk (const char*);
    void msgError (const char*);
    void msgConnect (const char*);
    void msgDisconnect (const char*);
    void msgDisassociated (const char*);
    void msgReset (const char*);
    void msgOutofStandby (const char*);
    void msgOutofDeepsleep (const char*);
    void resNormal (const char*);
    void resConnect (const char*);
    void resWps (const char*);
    void resMacAddress (const char*);
    void resIp (const char*);
    void resLookup (const char*);
    void resRssi (const char*);
    void resTime (const char*);
    void resChannel (const char*);
    void resStatus (const char*);

    // ----- GSwifi_at.cpp -----
    void clearFlags ();
    int cmdAT ();
    int cmdE (bool n);
    int cmdR (bool n);
    int cmdNMAC (const char *s = NULL);
    int cmdWREGDOMAIN (int n = CFG_WREGDOMAIN);
    int cmdWS ();
    int cmdWM (int n);
    int cmdWA (const char *s);
    int cmdWD ();
    int cmdWWPS (bool n, const char *p = NULL);
    int cmdNSTAT ();
    int cmdWSTATUS ();
    int cmdWRSSI ();
    int cmdWAUTH (int n);
    int cmdWWEP (int n, const char *s);
    int cmdWPAPSK (const char *s, const char *p);
    int cmdWRXACTIVE (bool n);
    int cmdWRXPS (bool n);
    int cmdWP (int n);
    int cmdNDHCP (bool n, const char *s = NULL, int m = CFG_TIMEOUT);
    int cmdDHCPSRVR (bool n);
    int cmdNSET (const char *s, const char *t, const char *u);
    int cmdDNS (bool n, const char *s);
    int cmdDNSLOOKUP (const char *s);
    int cmdDNSSET (const char *s);
    int cmdSTORENWCONN ();
    int cmdRESTORENWCONN ();
    int cmdBDATA (bool n);
    int cmdNCTCP (const char *s, int n);
    int cmdNCUDP (const char *s, int n, int m = 0);
    int cmdNSTCP (int n);
    int cmdNSUDP (int n);
    int cmdNCLOSE (int n);
    int cmdNCLOSEALL ();
    int cmdHTTPCONF (int n, const char *s);
    int cmdHTTPCONFDEL (int n);
    int cmdHTTPOPEN (const char *s, int n = 80, bool m = false);
    int cmdHTTPSEND (int n, bool m, const char *s, int t = 0);
    int cmdHTTPCLOSE (int n);
    int cmdPSDPSLEEP (int n = 0);
    int cmdPSSTBY (int n, int m = 0);
    int cmdWEBPROV (const char *s, const char *p);
    int cmdSETTIME (const char *s, const char *t);
    int cmdGETTIME ();
    int cmdNTIMESYNC (bool n, const char *s, int m = 0);
    int cmdDGPIO (int n, int m);

    // ----- GSwifi_hal.cpp -----
    void setReset (bool flg);
    void setAlarm (bool flg);
    void isrUart ();
    int getUart ();
    void putUart (char c);
    void setRts (bool flg);
    int lockUart (int ms);
    void unlockUart ();
    void initUart (PinName cts, PinName rts, PinName alarm, int baud);

};

#endif