for TwitterMbed
Fork of GSwifi_old by
GSwifi.h
- Committer:
- gsfan
- Date:
- 2012-10-18
- Revision:
- 16:aea56cce3bf5
- Parent:
- 15:5febfc399099
- Child:
- 17:6828b084e74b
File content as of revision 16:aea56cce3bf5:
/** * Gainspan wi-fi module library for mbed * Copyright (c) 2012 gsfan * Released under the MIT License: http://mbed.org/license/mit */ /** @file * @brief Gainspan wi-fi module library for mbed * GS1011MIC, GS1011MIP, GainSpan WiFi Breakout, etc. * module configuration: ATB=115200 */ #ifndef _GSWIFI_H_ #define _GSWIFI_H_ #include "mbed.h" #include "RingBuffer.h" #include "host.h" #include "ipaddr.h" #define GS_BAUD 115200 #define GS_UART_DIRECT #define GS_BULK #define GS_TIMEOUT 10000 // ms #define GS_TIMEOUT2 30000 // ms #define GS_CMD_SIZE 100 #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368) #define GS_DATA_SIZE 1500 #elif defined(TARGET_LPC11U24) #define GS_DATA_SIZE 500 #endif /** * Wi-Fi security */ enum GSSECURITY { GSSEC_AUTO = 0, GSSEC_NONE = 0, GSSEC_OPEN = 1, GSSEC_WEP = 2, GSSEC_WPA_PSK = 4, GSSEC_WPA2_PSK = 8, GSSEC_WPA_ENT = 16, GSSEC_WPA2_ENT = 32, GSSEC_WPS_BUTTON = 64, }; /** * TCP/IP protocol */ enum GSPROTOCOL { GSPROT_UDP = 0, GSPROT_TCP = 1, GSPROT_HTTPGET, GSPROT_HTTPPOST, }; /** * Client/Server */ enum GSTYPE { GSTYPE_CLIENT = 0, GSTYPE_SERVER = 1, }; enum GSRESPONCE { GSRES_NORMAL, GSRES_CONNECT, GSRES_WPS, GSRES_MACADDRESS, GSRES_DHCP, GSRES_DNSLOOKUP, GSRES_HTTP, GSRES_RSSI, GSRES_TIME, }; enum GSMODE { GSMODE_COMMAND, GSMODE_DATA_RX, GSMODE_DATA_RXUDP, GSMODE_DATA_RX_BULK, GSMODE_DATA_RXUDP_BULK, GSMODE_DATA_RXHTTP, }; enum GSSTATUS { GSSTAT_READY, GSSTAT_STANDBY, GSSTAT_WAKEUP, GSSTAT_DEEPSLEEP, }; /** * data receive callback function */ typedef void (*onGsReceiveFunc)(int cid, int len); struct GS_Socket { GSTYPE type; GSPROTOCOL protocol; bool connect; Host host; RingBuffer *data; int lcid; int received; onGsReceiveFunc onGsReceive; }; /** * GSwifi class */ class GSwifi { public: /** * default constructor */ GSwifi (PinName p_tx, PinName p_rx, int baud = GS_BAUD); /** * Default constructor (with hardware fllow controll) */ GSwifi (PinName p_tx, PinName p_rx, PinName p_cts, PinName p_rts, int baud = GS_BAUD); /** * send command */ int command (const char *cmd, GSRESPONCE res, int timeout = GS_TIMEOUT); /** * recv responce */ int cmdResponse (GSRESPONCE res, int ms); /** * associate infrastructure * @param sec GSSEC_OPEN, GSSEC_WEP, GSSEC_WPA_PSK, GSSEC_WPA2_PSK, GSSEC_WPS_BUTTON * @param ssid SSID * @param pass pass phrase * @param dhcp 0:static ip, 1:dhcp * @retval 0 success * @retval -1 failure */ int connect (GSSECURITY sec, char *ssid, char *pass, int dhcp = 1); /** * adhock * @param sec GSSEC_OPEN or GSSEC_WEP * @param ssid SSID * @param pass 10 or 26 hexadecimal digits * @param ipaddr my ip address * @param netmask subnet mask * @retval 0 success * @retval -1 failure */ int adhock (GSSECURITY sec, char *ssid, char *pass, IpAddr ipaddr, IpAddr netmask); /** * limited AP * @param sec GSSEC_OPEN or GSSEC_WEP * @param ssid SSID * @param pass 10 or 26 hexadecimal digits * @param ipaddr my ip address (dhcp start address) * @param netmask subnet mask * @retval 0 success * @retval -1 failure * firmware: s2w-secureweb, s2w-web, s2w-wpsweb */ int limitedap (GSSECURITY sec, char *ssid, char *pass, IpAddr ipaddr, IpAddr netmask); /** * unassociate */ int disconnect (); /** * use DHCP */ int setAddress (); /** * use static ip address */ int setAddress (IpAddr ipaddr, IpAddr netmask, IpAddr gateway, IpAddr nameserver); /** * get ip address */ int getAddress (IpAddr &ipaddr, IpAddr &netmask, IpAddr &gateway, IpAddr &nameserver); /** * resolv hostname * @param name hostname * @param addr resolved ip address */ int getHostByName (const char* name, IpAddr &addr); /** * resolv hostname * @param host.name hostname * @param host.ipaddr resolved ip address */ int getHostByName (Host &host); /** * RF power * @param power 0(high)-7(low) */ int setRFPower (int power); /** * 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); /** * standby mode * @param msec wakeup after * wakeup after msec or alarm1/2 * core off, save to RTC ram */ int standby (int msec); /** * restore standby */ int wakeup (); /** * deep sleep mode */ int deepSleep (); /** * wifi connected */ bool isConnected (); /** * status * @return GSSTATUS */ GSSTATUS getStatus (); /** * RSSI * @return RSSI (dBm) */ int getRssi (); /** * set NTP server * @param host SNTP server * @param sec time sync interval, 0:one time */ int ntpdate (Host host, int sec = 0); /** * set system time * @param time date time (UTC) */ int setTime (time_t time); /** * get RTC time * @return date time (UTC) */ time_t getTime (); /** * GPIO output * @param port 10,11,30,31 * @param out 0:set(high), 1:reset(low) */ int gpioOut (int port, int out); /** * main polling */ void poll(); /** * tcp/udp client * @return CID, -1:failure */ int open (Host &host, GSPROTOCOL pro, onGsReceiveFunc ponGsReceive = NULL); /** * tcp/udp server * @return CID, -1:failure */ int listen (int port, GSPROTOCOL pro, onGsReceiveFunc ponGsReceive = NULL); /** * close client/server */ int close (int cid); /** * send data tcp(s/c), udp(c) */ int send (int cid, char *buf, int len); /** * send data udp(s) */ int send (int cid, char *buf, int len, Host &host); /** * recv data tcp(s/c), udp(c) * @return length */ int recv (int cid, char *buf, int len); /** * recv data udp(s) * @return length */ int recv (int cid, char *buf, int len, Host &host); /** * tcp/udp connected */ bool isConnected (int cid); /** * http request * @return CID, -1:failure * If you use ssl, please set system time. */ int httpGet (Host &host, char *uri, int ssl, char *user, char *pwd, onGsReceiveFunc ponGsReceive = NULL); int httpGet (Host &host, char *uri, int ssl = 0, onGsReceiveFunc ponGsReceive = NULL); /** * http request * @return CID, -1:failure * If you use ssl, please set system time. */ int httpPost (Host &host, char *uri, char *body, int ssl, char *user, char *pwd, onGsReceiveFunc ponGsReceive = NULL); int httpPost (Host &host, char *uri, char *body, int ssl = 0, onGsReceiveFunc ponGsReceive = NULL); /** * certificate */ int certAdd (char *name, char *cert, int len); int base64encode (const char *input, int length, char *output, int len); int urlencode (char *str, char *buf, int len); void test (); int getc(); void putc(char c); int readable(); protected: int x2i (char c); char i2x (int i); void isr_recv (); void newSock (int cid, GSTYPE type, GSPROTOCOL pro, onGsReceiveFunc ponGsReceive); private: Serial _gs; bool _rts; volatile bool _connect; volatile GSSTATUS _status; volatile int _gs_ok, _gs_failure, _gs_enter; GSMODE _gs_mode; int _escape; int _cid, _rssi; IpAddr _ipaddr, _netmask, _gateway, _nameserver, _resolv; Host _from, _to; char _mac[6]; RingBuffer _buf_cmd; struct GS_Socket _gs_sock[16]; time_t _time; }; #endif