GainSpan Wi-Fi library see: http://mbed.org/users/gsfan/notebook/gainspan_wifi/
Fork of GSwifi_old by
GSwifi.h
00001 /** 00002 * Gainspan wi-fi module library for mbed 00003 * Copyright (c) 2012 gsfan 00004 * Released under the MIT License: http://mbed.org/license/mit 00005 */ 00006 00007 /** @file 00008 * @brief Gainspan wi-fi module library for mbed 00009 * GS1011MIC, GS1011MIP, GainSpan WiFi Breakout, etc. 00010 * module configuration: ATB=115200 00011 */ 00012 00013 #ifndef _GSWIFI_H_ 00014 #define _GSWIFI_H_ 00015 00016 #include "mbed.h" 00017 #include "GSwifi_net.h" 00018 #include "RingBuffer.h" 00019 #include "host.h" 00020 #include "ipaddr.h" 00021 00022 #define GS_BAUD 115200 00023 00024 #define GS_UART_DIRECT 00025 #define GS_BULK 00026 00027 #define GS_DNSNAME "setup.local" 00028 #define GS_WREGDOMAIN "2" // 0:FCC, 1:ETSI, 2:TELEC 00029 00030 #define GS_TIMEOUT 10000 // ms 00031 #define GS_TIMEOUT2 30000 // ms 00032 #define GS_CMD_SIZE 100 00033 #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368) 00034 //#define GS_DATA_SIZE 1500 00035 #define GS_DATA_SIZE 1000 00036 #elif defined(TARGET_LPC11U24) 00037 #define GS_DATA_SIZE 500 00038 #endif 00039 00040 #ifdef GS_UART_DIRECT 00041 #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368) 00042 #define _gs_getc() LPC_UART1->RBR 00043 #define _gs_putc(c) while(!(LPC_UART1->LSR & (1<<5))); LPC_UART1->THR = c 00044 #elif defined(TARGET_LPC11U24) 00045 #define _gs_getc() LPC_USART->RBR 00046 #define _gs_putc(c) while(!(LPC_USART->LSR & (1<<5))); LPC_USART->THR = c 00047 #endif 00048 #else 00049 #define _gs_getc() _gs.getc() 00050 #define _gs_putc(c) _gs.putc(c) 00051 #endif 00052 00053 /** 00054 * Wi-Fi security 00055 */ 00056 enum GSSECURITY { 00057 GSSEC_AUTO = 0, 00058 GSSEC_NONE = 0, 00059 GSSEC_OPEN = 1, 00060 GSSEC_WEP = 2, 00061 GSSEC_WPA_PSK = 4, 00062 GSSEC_WPA2_PSK = 8, 00063 GSSEC_WPA_ENT = 16, 00064 GSSEC_WPA2_ENT = 32, 00065 GSSEC_WPS_BUTTON = 64, 00066 }; 00067 00068 /** 00069 * TCP/IP protocol 00070 */ 00071 enum GSPROTOCOL { 00072 GSPROT_UDP = 0, 00073 GSPROT_TCP = 1, 00074 GSPROT_HTTPGET, 00075 GSPROT_HTTPPOST, 00076 GSPROT_HTTPD, 00077 }; 00078 00079 /** 00080 * Client/Server 00081 */ 00082 enum GSTYPE { 00083 GSTYPE_CLIENT = 0, 00084 GSTYPE_SERVER = 1, 00085 }; 00086 00087 enum GSRESPONCE { 00088 GSRES_NONE, 00089 GSRES_NORMAL, 00090 GSRES_CONNECT, 00091 GSRES_WPS, 00092 GSRES_MACADDRESS, 00093 GSRES_DHCP, 00094 GSRES_DNSLOOKUP, 00095 GSRES_HTTP, 00096 GSRES_RSSI, 00097 GSRES_TIME, 00098 }; 00099 00100 enum GSMODE { 00101 GSMODE_COMMAND, 00102 GSMODE_DATA_RX, 00103 GSMODE_DATA_RXUDP, 00104 GSMODE_DATA_RX_BULK, 00105 GSMODE_DATA_RXUDP_BULK, 00106 GSMODE_DATA_RXHTTP, 00107 }; 00108 00109 enum GSSTATUS { 00110 GSSTAT_READY, 00111 GSSTAT_STANDBY, 00112 GSSTAT_WAKEUP, 00113 GSSTAT_DEEPSLEEP, 00114 }; 00115 00116 /** 00117 * data receive callback function 00118 */ 00119 typedef void (*onGsReceiveFunc)(int cid, int len); 00120 00121 struct GS_Socket { 00122 GSTYPE type; 00123 GSPROTOCOL protocol; 00124 bool connect; 00125 Host host; 00126 RingBuffer *data; 00127 int lcid; 00128 int received; 00129 onGsReceiveFunc onGsReceive; 00130 }; 00131 00132 /** 00133 * GSwifi class 00134 */ 00135 class GSwifi { 00136 public: 00137 /** 00138 * default constructor 00139 */ 00140 GSwifi (PinName p_tx, PinName p_rx, int baud = GS_BAUD); 00141 /** 00142 * Default constructor (with hardware fllow controll) 00143 */ 00144 GSwifi (PinName p_tx, PinName p_rx, PinName p_cts, PinName p_rts, int baud = GS_BAUD); 00145 /** 00146 * send command 00147 */ 00148 int command (const char *cmd, GSRESPONCE res, int timeout = GS_TIMEOUT); 00149 /** 00150 * recv responce 00151 */ 00152 int cmdResponse (GSRESPONCE res, int ms); 00153 /** 00154 * associate infrastructure 00155 * @param sec GSSEC_OPEN, GSSEC_WEP, GSSEC_WPA_PSK, GSSEC_WPA2_PSK, GSSEC_WPS_BUTTON 00156 * @param ssid SSID 00157 * @param pass pass phrase 00158 * @param dhcp 0:static ip, 1:dhcp 00159 * @param reconnect auto re-connect 00160 * @retval 0 success 00161 * @retval -1 failure 00162 */ 00163 int connect (GSSECURITY sec, const char *ssid, const char *pass, int dhcp = 1, int reconnect = 0); 00164 /** 00165 * adhock 00166 * @param sec GSSEC_OPEN or GSSEC_WEP 00167 * @param ssid SSID 00168 * @param pass 10 or 26 hexadecimal digits 00169 * @param ipaddr my ip address 00170 * @param netmask subnet mask 00171 * @retval 0 success 00172 * @retval -1 failure 00173 */ 00174 int adhock (GSSECURITY sec, const char *ssid, const char *pass, IpAddr ipaddr, IpAddr netmask); 00175 /** 00176 * limited AP 00177 * @param sec GSSEC_OPEN or GSSEC_WEP 00178 * @param ssid SSID 00179 * @param pass 10 or 26 hexadecimal digits 00180 * @param ipaddr my ip address (dhcp start address) 00181 * @param netmask subnet mask 00182 * @retval 0 success 00183 * @retval -1 failure 00184 * firmware: s2w-secureweb, s2w-web, s2w-wpsweb 00185 */ 00186 int limitedap (GSSECURITY sec, const char *ssid, const char *pass, IpAddr ipaddr, IpAddr netmask, char *dns = NULL); 00187 /** 00188 * unassociate 00189 */ 00190 int disconnect (); 00191 00192 /** 00193 * use DHCP 00194 */ 00195 int setAddress (); 00196 /** 00197 * use static ip address 00198 */ 00199 int setAddress (IpAddr ipaddr, IpAddr netmask, IpAddr gateway, IpAddr nameserver); 00200 /** 00201 * get ip address 00202 */ 00203 int getAddress (IpAddr &ipaddr, IpAddr &netmask, IpAddr &gateway, IpAddr &nameserver); 00204 /** 00205 * resolv hostname 00206 * @param name hostname 00207 * @param addr resolved ip address 00208 */ 00209 int getHostByName (const char* name, IpAddr &addr); 00210 /** 00211 * resolv hostname 00212 * @param host.name hostname 00213 * @param host.ipaddr resolved ip address 00214 */ 00215 int getHostByName (Host &host); 00216 /** 00217 * RF power 00218 * @param power 0(high)-7(low) 00219 */ 00220 int setRFPower (int power); 00221 /** 00222 * power save mode 00223 * @param active rx radio 0:switched off, 1:always on 00224 * @param save power save 0:disable, 1:enable 00225 */ 00226 int powerSave (int active, int save); 00227 /** 00228 * standby mode 00229 * @param msec wakeup after 00230 * wakeup after msec or alarm1/2 00231 * core off, save to RTC ram 00232 */ 00233 int standby (int msec); 00234 /** 00235 * restore standby 00236 */ 00237 int wakeup (); 00238 /** 00239 * deep sleep mode 00240 */ 00241 int deepSleep (); 00242 /** 00243 * wifi connected 00244 */ 00245 bool isConnected (); 00246 /** 00247 * status 00248 * @return GSSTATUS 00249 */ 00250 GSSTATUS getStatus (); 00251 /** 00252 * RSSI 00253 * @return RSSI (dBm) 00254 */ 00255 int getRssi (); 00256 /** 00257 * set NTP server 00258 * @param host SNTP server 00259 * @param sec time sync interval, 0:one time 00260 */ 00261 int ntpdate (Host host, int sec = 0); 00262 /** 00263 * set system time 00264 * @param time date time (UTC) 00265 */ 00266 int setTime (time_t time); 00267 /** 00268 * get RTC time 00269 * @return date time (UTC) 00270 */ 00271 time_t getTime (); 00272 /** 00273 * GPIO output 00274 * @param port 10,11,30,31 00275 * @param out 0:set(high), 1:reset(low) 00276 */ 00277 int gpioOut (int port, int out); 00278 00279 /** 00280 * main polling 00281 */ 00282 void poll(); 00283 00284 /** 00285 * tcp/udp client 00286 * @return CID, -1:failure 00287 */ 00288 int open (Host &host, GSPROTOCOL pro, onGsReceiveFunc ponGsReceive = NULL); 00289 /** 00290 * tcp/udp server 00291 * @return CID, -1:failure 00292 */ 00293 int listen (int port, GSPROTOCOL pro, onGsReceiveFunc ponGsReceive = NULL); 00294 /* 00295 template<typename T> 00296 int listen2 (T* tptr, int port, GSPROTOCOL pro, void (T::*mptr)(int,int)) { 00297 if((mptr != NULL) && (tptr != NULL)) { 00298 return listen(port, pro, reinterpret_cast<onGsReceiveFunc>(mptr)); 00299 } 00300 } 00301 */ 00302 /** 00303 * close client/server 00304 */ 00305 int close (int cid); 00306 /** 00307 * send data tcp(s/c), udp(c) 00308 */ 00309 int send (int cid, const char *buf, int len); 00310 /** 00311 * send data udp(s) 00312 */ 00313 int send (int cid, const char *buf, int len, Host &host); 00314 /** 00315 * recv data tcp(s/c), udp(c) 00316 * @return length 00317 */ 00318 int recv (int cid, char *buf, int len); 00319 /** 00320 * recv data udp(s) 00321 * @return length 00322 */ 00323 int recv (int cid, char *buf, int len, Host &host); 00324 /** 00325 * tcp/udp connected 00326 */ 00327 bool isConnected (int cid); 00328 00329 /** 00330 * http request (GET method) 00331 */ 00332 int httpGet (Host &host, const char *uri, const char *user, const char *pwd, int ssl = 0, onGsReceiveFunc ponGsReceive = NULL); 00333 int httpGet (Host &host, const char *uri, int ssl = 0, onGsReceiveFunc ponGsReceive = NULL); 00334 /** 00335 * http request (POST method) 00336 */ 00337 int httpPost (Host &host, const char *uri, const char *body, const char *user, const char *pwd, int ssl = 0, onGsReceiveFunc ponGsReceive = NULL); 00338 int httpPost (Host &host, const char *uri, const char *body, int ssl = 0, onGsReceiveFunc ponGsReceive = NULL); 00339 00340 /** 00341 * certificate 00342 */ 00343 int certAdd (const char *name, const char *cert, int len); 00344 00345 #ifdef GS_USE_SMTP 00346 /** 00347 * send mail (smtp) 00348 * @param host SMTP server 00349 * @param to To address 00350 * @param from From address 00351 * @param subject Subject 00352 * @param mesg Message 00353 * @param user username (SMTP Auth) 00354 * @param pwd password (SMTP Auth) 00355 * @retval 0 success 00356 * @retval -1 failure 00357 */ 00358 int mail (Host &host, const char *to, const char *from, const char *subject, const char *mesg, const char *user = NULL, const char *pwd = NULL); 00359 #endif 00360 00361 #ifdef GS_USE_HTTPD 00362 /** 00363 * start http server 00364 * @param port 00365 */ 00366 int httpd (int port = 80); 00367 /** 00368 * attach uri to dirctory handler 00369 */ 00370 void send_httpd_error (int cid, int err); 00371 /** 00372 * attach uri to dirctory handler 00373 */ 00374 int attach_httpd (const char *uri, const char *dir); 00375 /** 00376 * attach uri to cgi handler 00377 */ 00378 int attach_httpd (const char *uri, onHttpdCgiFunc ponHttpCgi); 00379 #ifdef GS_USE_WEBSOCKET 00380 int send_websocket (int cid, const char *buf, int len); 00381 #endif 00382 #endif 00383 00384 /** 00385 * Web server 00386 */ 00387 int provisioning (char *user, char *pass); 00388 /** 00389 * change baud rate 00390 */ 00391 int setBaud (int baud); 00392 /** 00393 * base64 encode 00394 */ 00395 int base64encode (char *input, int length, char *output, int len); 00396 /** 00397 * url encode 00398 */ 00399 int urlencode (char *str, char *buf, int len); 00400 /** 00401 * url decode 00402 */ 00403 int urldecode (char *str, char *buf, int len); 00404 00405 #ifdef DEBUG 00406 void dump (); 00407 void test (); 00408 int getc(); 00409 void putc(char c); 00410 int readable(); 00411 #endif 00412 00413 protected: 00414 void poll_cmd (); 00415 int x2i (char c); 00416 char i2x (int i); 00417 void isr_recv (); 00418 void newSock (int cid, GSTYPE type, GSPROTOCOL pro, onGsReceiveFunc ponGsReceive); 00419 int from_hex (int ch); 00420 int to_hex (int code); 00421 00422 #ifdef GS_USE_SMTP 00423 int wait_smtp (int cid, int code); 00424 #endif 00425 00426 #ifdef GS_USE_HTTPD 00427 int get_handler (char *uri); 00428 int httpd_request (int cid, GS_httpd *gshttpd, char *dir); 00429 char *mimetype (char *file); 00430 int strnicmp (char *p1, char *p2, int n); 00431 #endif 00432 00433 private: 00434 Serial _gs; 00435 bool _rts; 00436 volatile bool _connect; 00437 volatile GSSTATUS _status; 00438 volatile int _gs_ok, _gs_failure, _gs_enter; 00439 volatile GSRESPONCE _response; 00440 GSMODE _gs_mode; 00441 int _escape; 00442 int _cid, _rssi; 00443 IpAddr _ipaddr, _netmask, _gateway, _nameserver, _resolv; 00444 Host _from, _to; 00445 char _mac[6]; 00446 RingBuffer _buf_cmd; 00447 struct GS_Socket _gs_sock[16]; 00448 time_t _time; 00449 char *_ssid; 00450 int _reconnect, _reconnect_count; 00451 00452 #ifdef GS_USE_HTTPD 00453 struct GS_httpd _httpd[16]; 00454 struct GS_httpd_handler _handler[10]; 00455 int _handler_count; 00456 00457 void poll_httpd (int cid, int len); 00458 #ifdef GS_USE_WEBSOCKET 00459 void poll_websocket (int cid, int len); 00460 void send_websocket_accept (int cid); 00461 #endif 00462 #endif 00463 }; 00464 00465 #endif
Generated on Wed Jul 13 2022 01:52:46 by
1.7.2
