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
GainSpan Wi-Fi library
The GS1011/GS2100 is an ultra low power 802.11b wireless module from GainSpan.
mbed RTOS supported.
- about this library: http://mbed.org/users/gsfan/notebook/GSwifiInterface/
- about Wi-Fi module: http://mbed.org/users/gsfan/notebook/gainspan_wifi/
ゲインスパン Wi-Fi モジュール ライブラリ
ゲインスパン社の低電力 Wi-Fiモジュール(無線LAN) GS1011/GS2100 シリーズ用のライブラリです。
mbed RTOS に対応しています。(mbed2.0)
- このライブラリについて: http://mbed.org/users/gsfan/notebook/gainspan_wifi/
- Wi-FIモジュールについて: http://mbed.org/users/gsfan/notebook/gainspan_wifi/
- UARTコマンド、SPIデータインターフェースに対応しました。(2019/09)
Diff: GSwifi/GSwifi.h
- Revision:
- 8:64184a968e3b
- Parent:
- 6:6a6396b56405
- Child:
- 9:4565d3d3d13b
--- a/GSwifi/GSwifi.h Wed Jan 30 05:52:14 2013 +0000 +++ b/GSwifi/GSwifi.h Thu Oct 31 06:41:45 2013 +0000 @@ -24,47 +24,77 @@ /* 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 10000 // ms -#define CFG_TIMEOUT2 30000 // ms +#define CFG_TIMEOUT 30000 // ms #define CFG_CMD_SIZE 100 -#define CFG_DATA_SIZE 1000 +#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_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 (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__); +#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, ...) -#endif - -#if (0 && !defined(TARGET_LPC11U24)) -#define INFO(x, ...) printf("[GSwifi : INFO]\r\n"x"\r\n", ##__VA_ARGS__); -#else #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, @@ -75,8 +105,11 @@ 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, @@ -85,9 +118,15 @@ PROTO_HTTPD, }; + /** Client/Server + */ + enum Type { + TYPE_CLIENT = 0, + TYPE_SERVER = 1, + }; + enum Response { RES_NULL, - RES_NORMAL, RES_CONNECT, RES_WPS, RES_MACADDRESS, @@ -96,189 +135,377 @@ RES_HTTP, RES_RSSI, RES_TIME, - RES_OK, - RES_FAILURE, + 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, }; - /* - * 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(); + // ----- 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); - /* - * 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); + /** 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); - /* - * 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); + /** 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); - /* - * 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); + /** 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); - /* - * 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); + /** Disconnect the wifi module from the access point + * @returns 0 if successful + */ + int disconnect (); - /* - * Return true if the module is using dhcp - * - * @returns true if the module is using dhcp - */ - bool isDHCP() { - return _state.dhcp; - } + /** Check if a tcp link is active + * @returns true if successful + */ + bool isConnected(); - bool gethostbyname(const char * host, char * ip); + int setSsid (Security sec, const char *ssid, const char *phrase); + + void poll (); static GSwifi * getInstance() { return _inst; }; - bool readRemote(int cid, char **ip, int *port); - int readCID (); - int readACID (); + // ----- 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: - 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); +#ifdef RTOS_H + Thread *_threadPoll; + Mutex _mutexUart; +#endif - int x2i (char c); - char i2x (int i); - void parseResponse (); - void parseCmdResponse (char *buf); +// 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; - typedef struct STATE { - bool associated; + 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; - Security sec; - Mode mode; - Status status; + time_t time; + + bool associated; + volatile Mode mode; + volatile Status status; bool escape; + volatile bool ok, failure; + Response res; int cid, acid; - volatile Response cmdres, retres; int n; - } State; + CircBuffer<char> *buf; + } _state; - State _state; - - typedef struct CONNECTION { + struct CONNECTION { + Protocol protocol; + Type type; bool connected; char ip[16]; int port; CircBuffer<char> *buf; - int parent; - } Connection; + 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*); - Connection _con[16]; + // ----- 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 \ No newline at end of file