OLD
OLD
Diff: GSwifi.h
- Revision:
- 0:2f6062c6d018
- Child:
- 1:b127c6c5241d
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/GSwifi.h Mon Jul 09 14:36:06 2012 +0000 @@ -0,0 +1,276 @@ +/** + * 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 + */ + +#include "mbed.h" +#include "RingBuffer.h" +#include "host.h" +#include "ipaddr.h" + +#define GS_BAUD 115200 + +#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 800 +#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, +}; + +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 acid, int len); + +struct GS_Socket { + GSTYPE type; + GSPROTOCOL protocol; + bool connect; + Host host; + RingBuffer *data; + int acid; + int received; + onGsReceiveFunc onGsReceive; +}; + +/** + * GSwifi class + */ +class GSwifi { +public: + /** + * default constructor + */ + GSwifi (PinName p_tx, PinName p_rx); + /** + * Default constructor (with hardware fllow controll) + */ + GSwifi (PinName p_tx, PinName p_rx, PinName p_cts, PinName p_rts); + /** + * send command + */ + + int command (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 + * @param pass + */ + + int connect (GSSECURITY sec, char *ssid, char *pass, int dhcp = 1); + /** + * adhock + * @param sec GSSEC_OPEN or GSSEC_WEP + * @param ssid + * @param pass + */ + int adhock (GSSECURITY sec, char *ssid, char *pass, IpAddr ipaddr, IpAddr netmask); + /** + * limited AP + * @param sec GSSEC_OPEN or GSSEC_WEP + * @param ssid + * @param pass 10 or 26 hexadecimal digits + * 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); + /** + * resolv hostname + */ + int getHostByName (const char* name, IpAddr &addr); + /** + * resolv hostname + */ + int getHostByName (Host &host); + /** + * RF power + * @param power 0(high)-7(low) + */ + int setRFPower (int power); + /** + * power save mode + */ + int powerSave (int beacon = 10, int association = 1); + /** + * 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 + */ + GSSTATUS getStatus (); + + /** + * polling + */ + void poll(); + + /** + * tcp/udp client + */ + int open (Host &host, GSPROTOCOL pro, onGsReceiveFunc ponGsReceive = NULL); + /** + * tcp/udp server + */ + 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) + */ + int recv (int cid, char *buf, int len); + /** + * recv data udp(s) + */ + int recv (int cid, char *buf, int len, Host &host); + /** + * tcp/udp connected + */ + bool isConnected (int cid); + + /** + * http request + */ + int httpGet (Host &host, char *uri, int ssl = 0, onGsReceiveFunc ponGsReceive = NULL); + /** + * certificate + */ + int certAdd (char *name, char *cert, int len); + + void test (); + int getc(); + void putc(char c); + int readable(); + +protected: + int x2i (char c); + char i2x (int i); + void isr_recv (); + +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; + IpAddr _ipaddr, _netmask, _gateway, _nameserver, _resolv; + Host _from, _to; + char mac[6]; + RingBuffer _buf_cmd; + struct GS_Socket _gs_sock[16]; +};