GainSpan Wi-Fi library see: http://mbed.org/users/gsfan/notebook/gainspan_wifi/
Dependents: GSwifi_httpd GSwifi_websocket GSwifi_tcpclient GSwifi_tcpserver ... more
Fork of GSwifi by
GSwifi.h
00001 /* Copyright (C) 2013 gsfan, MIT License 00002 * 00003 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00004 * and associated documentation files (the "Software"), to deal in the Software without restriction, 00005 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 00006 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 00007 * furnished to do so, subject to the following conditions: 00008 * 00009 * The above copyright notice and this permission notice shall be included in all copies or 00010 * substantial portions of the Software. 00011 * 00012 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 00013 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00014 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00015 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00016 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00017 */ 00018 /** @file 00019 * @brief Gainspan wi-fi module library for mbed 00020 * GS1011MIC, GS1011MIP, GainSpan WiFi Breakout, etc. 00021 */ 00022 00023 #ifndef _GSWIFI_H_ 00024 #define _GSWIFI_H_ 00025 00026 #include "dbg.h" 00027 #include "mbed.h" 00028 #include "CBuffer.h" 00029 #include "GSFunctionPointer.h" 00030 #include "host.h" 00031 #include "ipaddr.h" 00032 #include "GSwifi_conf.h" 00033 #include <ctype.h> 00034 #include <stdlib.h> 00035 #include <string.h> 00036 00037 00038 #ifdef GS_UART_DIRECT 00039 #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368) || defined(TARGET_LPC11U24) 00040 #define _gs_getc() _uart->RBR 00041 #define _gs_putc(c) while(!(_uart->LSR & (1<<5))); _uart->THR = c 00042 #elif defined(TARGET_KL25Z) 00043 #error "no support GS_UART_DIRECT" 00044 #endif 00045 #else // GS_UART_DIRECT 00046 #define _gs_getc() _gs.getc() 00047 #define _gs_putc(c) _gs.putc(c) 00048 #endif 00049 00050 #ifdef GS_SYSLOG 00051 #define LOG(...) printf("" __VA_ARGS__) 00052 #else 00053 #define LOG(...) 00054 #endif 00055 00056 00057 /** 00058 * GSwifi class 00059 */ 00060 class GSwifi { 00061 public: 00062 00063 /** 00064 * Wi-Fi security 00065 */ 00066 enum GSSECURITY { 00067 GSSEC_AUTO = 0, 00068 GSSEC_NONE = 0, 00069 GSSEC_OPEN = 1, 00070 GSSEC_WEP = 2, 00071 GSSEC_WPA_PSK = 4, 00072 GSSEC_WPA2_PSK = 8, 00073 GSSEC_WPA_ENT = 16, 00074 GSSEC_WPA2_ENT = 32, 00075 GSSEC_WPS_BUTTON = 64, 00076 GSSEC_WPS_PIN, 00077 }; 00078 00079 /** 00080 * TCP/IP protocol 00081 */ 00082 enum GSPROTOCOL { 00083 GSPROT_UDP = 0, 00084 GSPROT_TCP = 1, 00085 GSPROT_HTTPGET, 00086 GSPROT_HTTPPOST, 00087 GSPROT_HTTPD, 00088 }; 00089 00090 /** 00091 * Client/Server 00092 */ 00093 enum GSTYPE { 00094 GSTYPE_CLIENT = 0, 00095 GSTYPE_SERVER = 1, 00096 }; 00097 00098 enum GSRESPONCE { 00099 GSRES_NONE, 00100 GSRES_NORMAL, 00101 GSRES_CONNECT, 00102 GSRES_WPS, 00103 GSRES_MACADDRESS, 00104 GSRES_DHCP, 00105 GSRES_DNSLOOKUP, 00106 GSRES_HTTP, 00107 GSRES_RSSI, 00108 GSRES_TIME, 00109 GSRES_STATUS, 00110 }; 00111 00112 enum GSMODE { 00113 GSMODE_COMMAND, 00114 GSMODE_DATA_RX, 00115 GSMODE_DATA_RXUDP, 00116 GSMODE_DATA_RX_BULK, 00117 GSMODE_DATA_RXUDP_BULK, 00118 GSMODE_DATA_RXHTTP, 00119 }; 00120 00121 enum GSSTATUS { 00122 GSSTAT_READY, 00123 GSSTAT_STANDBY, 00124 GSSTAT_WAKEUP, 00125 GSSTAT_DEEPSLEEP, 00126 }; 00127 00128 /** 00129 * data receive callback function 00130 */ 00131 typedef void (*onGsReceiveFunc)(int cid, int len); 00132 00133 struct GS_Socket { 00134 GSTYPE type; 00135 GSPROTOCOL protocol; 00136 bool connect; 00137 Host host; 00138 CircBuffer<char> *data; 00139 int lcid; 00140 bool received; 00141 // onGsReceiveFunc onGsReceive; 00142 GSFunctionPointer onGsReceive; 00143 }; 00144 00145 #ifdef GS_ENABLE_HTTPD 00146 enum GSHTTPDMODE { 00147 GSHTTPDMODE_REQUEST, 00148 GSHTTPDMODE_HEAD, 00149 GSHTTPDMODE_SPACE, 00150 GSHTTPDMODE_BODY, 00151 GSHTTPDMODE_ERROR, 00152 GSHTTPDMODE_WEBSOCKET, 00153 GSHTTPDMODE_WEBSOCKET_MASK, 00154 GSHTTPDMODE_WEBSOCKET_BODY, 00155 }; 00156 00157 struct GS_httpd { 00158 GSHTTPDMODE mode; 00159 int type; 00160 char *buf; // body 00161 int len; // length of buf 00162 char *uri; 00163 char *file; 00164 char *query; 00165 int length; // content-length 00166 int keepalive; 00167 Host host; 00168 #ifdef GS_ENABLE_WEBSOCKET 00169 int websocket; 00170 char *websocket_key; 00171 int websocket_flg; 00172 char websocket_mask[4]; 00173 int websocket_payload; 00174 #endif 00175 }; 00176 00177 typedef void (*onHttpdCgiFunc)(int cid, GS_httpd *gshttpd); 00178 00179 struct GS_httpd_handler { 00180 char *uri; 00181 char *dir; 00182 onHttpdCgiFunc onHttpCgi; 00183 }; 00184 #endif // GS_ENABLE_HTTPD 00185 00186 // ----- GSwifi.cpp ----- 00187 /** 00188 * default constructor (no fllow controll) 00189 * @param p_tx mbed TXD pin (GS RXD) 00190 * @param p_rx mbed RXD pin (GS TXD) 00191 * @param p_reset GS RESET pin 00192 * @param p_alarm GS ALARM pin 00193 * @param baud uart baud rate 00194 */ 00195 GSwifi (PinName p_tx, PinName p_rx, PinName p_reset, PinName p_alarm = NC, int baud = GS_BAUD); 00196 /** 00197 * default constructor (with hardware fllow controll) 00198 * @param p_tx mbed TXD pin (GS RXD) 00199 * @param p_rx mbed RXD pin (GS TXD) 00200 * @param p_cts mbed CTS pin (GS RTS) 00201 * @param p_rts mbed RTS pin (GS CTS) 00202 * @param p_reset GS RESET pin 00203 * @param p_alarm GS ALARM pin 00204 * @param baud uart baud rate 00205 */ 00206 GSwifi (PinName p_tx, PinName p_rx, PinName p_cts, PinName p_rts, PinName p_reset, PinName p_alarm = NC, int baud = GS_BAUD); 00207 /** 00208 * polling 00209 */ 00210 void poll(); 00211 /** 00212 * send command 00213 */ 00214 int command (const char *cmd, GSRESPONCE res, int timeout = GS_TIMEOUT); 00215 /** 00216 * reset recv responce 00217 */ 00218 void resetResponse (GSRESPONCE res); 00219 /** 00220 * wait recv responce 00221 */ 00222 int waitResponse (int ms); 00223 /** 00224 * associate infrastructure 00225 * @param sec GSSEC_OPEN, GSSEC_WEP, GSSEC_WPA_PSK, GSSEC_WPA2_PSK, GSSEC_WPS_BUTTON 00226 * @param ssid SSID 00227 * @param pass pass phrase 00228 * @param dhcp 0:static ip, 1:dhcp 00229 * @param reconnect auto re-connect time 00230 * @param name my host name 00231 * @retval 0 success 00232 * @retval -1 failure 00233 */ 00234 int connect (GSSECURITY sec, const char *ssid, const char *pass, int dhcp = 1, int reconnect = GS_RECONNECT, char *name = NULL); 00235 /** 00236 * adhock 00237 * @param sec GSSEC_OPEN or GSSEC_WEP 00238 * @param ssid SSID 00239 * @param pass 10 or 26 hexadecimal digits 00240 * @param ipaddr my ip address 00241 * @param netmask subnet mask 00242 * @retval 0 success 00243 * @retval -1 failure 00244 */ 00245 int adhock (GSSECURITY sec, const char *ssid, const char *pass, IpAddr ipaddr, IpAddr netmask); 00246 /** 00247 * limited AP 00248 * @param sec GSSEC_OPEN or GSSEC_WEP 00249 * @param ssid SSID 00250 * @param pass 10 or 26 hexadecimal digits 00251 * @param ipaddr my ip address (dhcp start address) 00252 * @param netmask subnet mask 00253 * @param dns my host name 00254 * @retval 0 success 00255 * @retval -1 failure 00256 * firmware: s2w-secureweb, s2w-web, s2w-wpsweb 00257 */ 00258 int limitedap (GSSECURITY sec, const char *ssid, const char *pass, IpAddr ipaddr, IpAddr netmask, char *dns = NULL); 00259 /** 00260 * unassociate 00261 */ 00262 int disconnect (); 00263 /** 00264 * re-connect 00265 */ 00266 int reconnect (); 00267 /** 00268 * main polling 00269 */ 00270 int setBaud (int baud); 00271 /** 00272 * change radio region 00273 */ 00274 int setRegion (int reg = GS_WREGDOMAIN); 00275 00276 /** 00277 * use DHCP 00278 */ 00279 int setAddress (char *name = NULL); 00280 /** 00281 * use static ip address 00282 */ 00283 int setAddress (IpAddr ipaddr, IpAddr netmask, IpAddr gateway, IpAddr nameserver); 00284 /** 00285 * get ip address 00286 */ 00287 int getAddress (IpAddr &ipaddr, IpAddr &netmask, IpAddr &gateway, IpAddr &nameserver); 00288 /** 00289 * get mac address 00290 */ 00291 int getMacAddress (char *mac); 00292 /** 00293 * resolv hostname 00294 * @param name hostname 00295 * @param addr resolved ip address 00296 */ 00297 int getHostByName (const char* name, IpAddr &addr); 00298 /** 00299 * resolv hostname 00300 * @param host.name hostname 00301 * @param host.ipaddr resolved ip address 00302 */ 00303 int getHostByName (Host &host); 00304 /** 00305 * wifi connected 00306 */ 00307 bool isConnected (); 00308 /** 00309 * status 00310 * @return GSSTATUS 00311 */ 00312 GSSTATUS getStatus (); 00313 /** 00314 * RSSI 00315 * @return RSSI (dBm) 00316 */ 00317 int getRssi (); 00318 00319 char *getSsid () { 00320 return _ssid; 00321 } 00322 char *getPass () { 00323 return _pass; 00324 } 00325 #ifndef GS_LIB_TINY 00326 /** 00327 * RF power 00328 * @param power 0(high)-7(low) 00329 */ 00330 int setRFPower (int power); 00331 /** 00332 * power save mode 00333 * @param active rx radio 0:switched off, 1:always on 00334 * @param save power save 0:disable, 1:enable 00335 */ 00336 int powerSave (int active, int save); 00337 /** 00338 * standby mode 00339 * @param msec wakeup after 00340 * wakeup after msec or alarm1/2 00341 * core off, save to RTC ram 00342 */ 00343 int standby (int msec); 00344 /** 00345 * restore standby 00346 */ 00347 int wakeup (); 00348 /** 00349 * deep sleep mode 00350 */ 00351 int deepSleep (); 00352 00353 /** 00354 * set NTP server 00355 * @param host SNTP server 00356 * @param sec time sync interval, 0:one time 00357 */ 00358 int ntpdate (Host host, int sec = 0); 00359 /** 00360 * set system time 00361 * @param time date time (UTC) 00362 */ 00363 int setTime (time_t time); 00364 /** 00365 * get RTC time 00366 * @return date time (UTC) 00367 */ 00368 time_t getTime (); 00369 /** 00370 * GPIO output 00371 * @param port 10,11,30,31 00372 * @param out 0:set(high), 1:reset(low) 00373 */ 00374 int gpioOut (int port, int out); 00375 /** 00376 * Web server 00377 */ 00378 int provisioning (char *user, char *pass); 00379 /** 00380 * certificate 00381 */ 00382 int certAdd (const char *name, const char *cert, int len); 00383 #endif 00384 00385 // ----- GSwifi_sock.cpp ----- 00386 /** 00387 * tcp/udp client 00388 * @return CID, -1:failure 00389 */ 00390 int open (Host &host, GSPROTOCOL pro, int port = 0); 00391 00392 int open (Host &host, GSPROTOCOL pro, onGsReceiveFunc ponGsReceive, int port = 0) { 00393 int cid = open(host, pro, port); 00394 if (cid >= 0) _gs_sock[cid].onGsReceive.attach(ponGsReceive); 00395 return cid; 00396 } 00397 template<typename T> 00398 int open (Host &host, GSPROTOCOL pro, T *object, void (T::*member)(int, int), int port = 0) { 00399 int cid = open(host, pro, port); 00400 if (cid >= 0) _gs_sock[cid].onGsReceive.attach(object, member); 00401 return cid; 00402 } 00403 /** 00404 * tcp/udp server 00405 * @return CID, -1:failure 00406 */ 00407 int listen (int port, GSPROTOCOL pro); 00408 00409 int listen (int port, GSPROTOCOL pro, onGsReceiveFunc ponGsReceive) { 00410 int cid = listen(port, pro); 00411 if (cid >= 0) _gs_sock[cid].onGsReceive.attach(ponGsReceive); 00412 return cid; 00413 } 00414 template<typename T> 00415 int listen (int port, GSPROTOCOL pro, T *object, void (T::*member)(int, int)) { 00416 int cid = listen(port, pro); 00417 if (cid >= 0) _gs_sock[cid].onGsReceive.attach(object, member); 00418 return cid; 00419 } 00420 /** 00421 * close client/server 00422 */ 00423 int close (int cid); 00424 /** 00425 * send data tcp(s/c), udp(c) 00426 */ 00427 int send (int cid, const char *buf, int len); 00428 /** 00429 * send data udp(s) 00430 */ 00431 int send (int cid, const char *buf, int len, Host &host); 00432 /** 00433 * recv data tcp(s/c), udp(c) 00434 * @return length 00435 */ 00436 int recv (int cid, char *buf, int len); 00437 /** 00438 * recv data udp(s) 00439 * @return length 00440 */ 00441 int recv (int cid, char *buf, int len, Host &host); 00442 /** 00443 * tcp/udp connected 00444 */ 00445 bool isConnected (int cid); 00446 00447 #ifdef GS_ENABLE_HTTP 00448 // ----- GSwifi_http.cpp ----- 00449 /** 00450 * http request (GET method) 00451 */ 00452 int httpGet (Host &host, const char *uri, const char *user, const char *pwd, int ssl = 0, onGsReceiveFunc ponGsReceive = NULL); 00453 int httpGet (Host &host, const char *uri, int ssl = 0, onGsReceiveFunc ponGsReceive = NULL) { 00454 return httpGet (host, uri, NULL, NULL, ssl, ponGsReceive); 00455 } 00456 /** 00457 * http request (POST method) 00458 */ 00459 int httpPost (Host &host, const char *uri, const char *body, const char *user, const char *pwd, int ssl = 0, onGsReceiveFunc ponGsReceive = NULL); 00460 int httpPost (Host &host, const char *uri, const char *body, int ssl = 0, onGsReceiveFunc ponGsReceive = NULL) { 00461 return httpPost (host, uri, body, NULL, NULL, ssl, ponGsReceive); 00462 } 00463 /** 00464 * websocket request (Upgrade method) 00465 */ 00466 int wsOpen (Host &host, const char *uri, const char *user, const char *pwd, onGsReceiveFunc ponGsReceive = NULL); 00467 int wsOpen (Host &host, const char *uri, onGsReceiveFunc ponGsReceive = NULL) { 00468 return wsOpen (host, uri, NULL, NULL, ponGsReceive); 00469 } 00470 /** 00471 * send data websocket 00472 */ 00473 int wsSend (int cid, const char *buf, int len, const char *mask = NULL); 00474 00475 /** 00476 * base64 encode 00477 */ 00478 int base64encode (char *input, int length, char *output, int len); 00479 /** 00480 * url encode 00481 */ 00482 int urlencode (char *str, char *buf, int len); 00483 /** 00484 * url decode 00485 */ 00486 int urldecode (char *str, char *buf, int len); 00487 #endif 00488 00489 #ifdef GS_ENABLE_SMTP 00490 // ----- GSwifi_smtp.cpp ----- 00491 /** 00492 * send mail (smtp) 00493 * @param host SMTP server 00494 * @param to To address 00495 * @param from From address 00496 * @param subject Subject 00497 * @param mesg Message 00498 * @param user username (SMTP Auth) 00499 * @param pwd password (SMTP Auth) 00500 * @retval 0 success 00501 * @retval -1 failure 00502 */ 00503 int mail (Host &host, const char *to, const char *from, const char *subject, const char *mesg, const char *user = NULL, const char *pwd = NULL); 00504 #endif 00505 00506 #ifdef GS_ENABLE_HTTPD 00507 // ----- GSwifi_httpd.cpp ----- 00508 /** 00509 * start http server 00510 * @param port 00511 */ 00512 int httpd (int port = 80); 00513 /** 00514 * attach uri to dirctory handler 00515 */ 00516 void send_httpd_error (int cid, int err); 00517 /** 00518 * attach uri to dirctory handler 00519 */ 00520 int attach_httpd (const char *uri, const char *dir); 00521 /** 00522 * attach uri to cgi handler 00523 */ 00524 int attach_httpd (const char *uri, onHttpdCgiFunc ponHttpCgi); 00525 #endif 00526 00527 #ifdef DEBUG 00528 void dump (); 00529 void test (); 00530 int getc(); 00531 void putc(char c); 00532 int readable(); 00533 #endif 00534 00535 protected: 00536 void reset (); 00537 int autobaud (int flg); 00538 int acquireUart (int ms = GS_TIMEOUT); 00539 void releaseUart (); 00540 00541 inline void _gs_puts (char *s) { 00542 int i; 00543 for (i = 0; i < strlen(s); i++) { 00544 _gs_putc(s[i]); 00545 } 00546 } 00547 00548 void parseResponse (); 00549 void parseCmdResponse (char *buf); 00550 00551 int x2i (char c); 00552 char i2x (int i); 00553 void isr_recv (); 00554 int from_hex (int ch); 00555 int to_hex (int code); 00556 00557 void newSock (int cid, GSTYPE type, GSPROTOCOL pro); 00558 00559 void newSock (int cid, GSTYPE type, GSPROTOCOL pro, onGsReceiveFunc ponGsReceive) { 00560 newSock(cid, type, pro); 00561 _gs_sock[cid].onGsReceive.attach(ponGsReceive); 00562 } 00563 template<typename T> 00564 void newSock (int cid, GSTYPE type, GSPROTOCOL pro, T *object, void (T::*member)(int, int)) { 00565 newSock(cid, type, pro); 00566 _gs_sock[cid].onGsReceive.attach(object, member); 00567 } 00568 00569 int wait_ws (int cid, int code); 00570 00571 #ifdef GS_ENABLE_SMTP 00572 int wait_smtp (int cid, int code); 00573 #endif 00574 00575 #ifdef GS_ENABLE_HTTPD 00576 int get_handler (char *uri); 00577 int httpd_request (int cid, GS_httpd *gshttpd, char *dir); 00578 char *mimetype (char *file); 00579 int strnicmp (const char *p1, const char *p2, int n); 00580 #endif 00581 00582 private: 00583 Serial _gs; 00584 int _baud; 00585 bool _rts; 00586 #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368) 00587 LPC_UART1_TypeDef *_uart; 00588 #elif defined(TARGET_LPC11U24) 00589 LPC_USART_Type *_uart; 00590 #endif 00591 DigitalInOut _reset; 00592 DigitalInOut *_alarm; 00593 volatile bool _connect, _dhcp; 00594 volatile GSSTATUS _status; 00595 volatile bool _gs_ok, _gs_failure; 00596 volatile int _gs_flg; 00597 volatile GSRESPONCE _gs_res; 00598 volatile GSMODE _gs_mode; 00599 volatile bool _escape; 00600 volatile int _cid, _rssi; 00601 IpAddr _ipaddr, _netmask, _gateway, _nameserver, _resolv; 00602 Host _from, _to; 00603 char _mac[6]; 00604 CircBuffer<char> _buf_cmd; 00605 struct GS_Socket _gs_sock[16]; 00606 time_t _time; 00607 GSSECURITY _sec; 00608 char *_ssid, *_pass; 00609 int _reconnect; 00610 00611 #ifdef GS_ENABLE_HTTPD 00612 struct GS_httpd _httpd[16]; 00613 struct GS_httpd_handler _handler[10]; 00614 int _handler_count; 00615 00616 void poll_httpd (int cid, int len); 00617 #ifdef GS_ENABLE_WEBSOCKET 00618 void poll_websocket (int cid, int len); 00619 void send_websocket_accept (int cid); 00620 #endif 00621 #endif 00622 }; 00623 00624 #endif
Generated on Tue Jul 12 2022 22:02:57 by 1.7.2