This is library for using WizFi250

Dependents:   WebSocket_WizFi250_HelloWorld IFTTT_WizFi250 AxedaGo-WizFi250 FANARM_AP_udp_server ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers WizFi250.h Source File

WizFi250.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 /* Copyright (C) 2014 Wiznet, MIT License
00019  *  port to the Wiznet Module WizFi250
00020  */
00021 
00022 #ifndef WIZFI250_H_
00023 #define WIZFI250_H_
00024 
00025 #include "WizFi250_conf.h"
00026 
00027 #include "mbed.h"
00028 #include "RawSerial.h"
00029 #include "Serial.h"
00030 #include "CBuffer.h"
00031 #include <ctype.h>
00032 #include <stdlib.h>
00033 #include <string>
00034 
00035 using namespace std;
00036 
00037 #define BUF_SIZE 1600
00038 
00039 //Debug is disabled by default
00040 #if 1
00041 #define WIZ_DBG(x, ...) std::printf("[WizFi250: DBG]" x "\r\n", ##__VA_ARGS__);
00042 #define WIZ_WARN(x, ...) std::printf("[WizFi250: WARN]" x "\r\n", ##__VA_ARGS__);
00043 #define WIZ_ERR(x, ...) std::printf("[WizFi250: ERR]" x "\r\n", ##__VA_ARGS__);
00044 #define WIZ_INFO(x, ...) std::printf("[WizFi250: INFO]" x "\r\n", ##__VA_ARGS__);
00045 #else
00046 #define WIZ_DBG(x, ...)
00047 #define WIZ_WARN(x, ...)
00048 #define WIZ_ERR(x, ...)
00049 #define WIZ_INFO(x, ...)
00050 #endif
00051 
00052 
00053 class WizFi250
00054 {
00055 public:
00056 
00057     enum AntennaMode{
00058         PCB = 0,
00059         UFL = 1,
00060         AUTO = 3,
00061     };
00062 
00063     enum WiFiMode {
00064         WM_STATION = 0,
00065         WM_AP = 1,
00066     };
00067 
00068     /** Wi-Fi security
00069      */
00070     enum Security {
00071         // kaizen need to change
00072         SEC_AUTO        = 0,
00073         SEC_OPEN        = 1,
00074         SEC_WEP         = 2,
00075         SEC_WPA_TKIP    = 3,
00076         SEC_WPA_AES     = 4,
00077         SEC_WPA2_AES    = 5,
00078         SEC_WPA2_TKIP   = 6,
00079         SEC_WPA2_MIXED  = 7,
00080     };
00081 
00082     /** TCP/IP protocol
00083      */
00084     enum Protocol {
00085         PROTO_UDP = 0,
00086         PROTO_TCP = 1,
00087     };
00088 
00089     /** Client/Server
00090      */
00091     enum Type {
00092         TYPE_CLIENT = 0,
00093         TYPE_SERVER = 1,
00094     };
00095 
00096     enum Response {
00097         RES_NULL,
00098         RES_MACADDRESS,
00099         RES_WJOIN,
00100         RES_CONNECT,
00101         RES_SSEND,
00102         RES_FDNS,
00103         RES_SMGMT,
00104         RES_WSTATUS,
00105     };
00106 
00107     enum Mode {
00108         MODE_COMMAND,
00109         MODE_CMDRESP,
00110         MODE_DATA_RX,
00111         MODE_DATA_RXUDP,
00112         MODE_DATA_RXUDP_BULK,
00113     };
00114 
00115     enum Status {
00116         STAT_NONE,
00117         STAT_READY,
00118         STAT_STANDBY,
00119         STAT_WAKEUP,
00120         STAT_DEEPSLEEP,
00121     };
00122 
00123 
00124     WizFi250 (PinName tx, PinName rx, PinName cts, PinName rts, PinName reset, PinName alarm = NC, int baud = 115200);
00125 
00126     // --------- WizFi250_at.cpp ---------
00127     void clearFlags     ();
00128     int  sendCommand    (const char * cmd, Response res = RES_NULL, int timeout = DEFAULT_WAIT_RESP_TIMEOUT, int opt = 2);
00129 
00130     int cmdAT       ();
00131     int cmdMECHO    (bool flg);
00132     int cmdUSET     (int baud, char *flow);
00133     int cmdMMAC     (const char *mac = NULL);
00134     int cmdWSET     (WiFiMode mode, const char *ssid, const char *bssid = NULL, int channel = 1);
00135     int cmdWANT     (AntennaMode mode);
00136     int cmdWNET     (bool is_dhcp);
00137     int cmdWSEC     (WiFiMode mode, const char *key, const char *sec = NULL);
00138     int cmdWJOIN    ();
00139     int cmdWSTATUS  ();
00140     int cmdSCON     ( const char *openType, const char *socketType, int localPort, const char *dataMode = "0");
00141     int cmdSCON     ( const char *openType, const char *socketType, const char *remoteIp, int remotePort, int localPort = 0, const char *dataMode = "0");
00142     int cmdSSEND    ( const char *data, int cid, int sendSize, const char *remoteIp = NULL, int remotePort = 0, int Timeout = 2000 );
00143     int cmdCLOSE    ( int cid );
00144     int cmdFDNS     (const char *host);
00145     int cmdSMGMT    ( int cid );
00146 
00147 
00148     static WizFi250 * getInstance() {
00149         return _inst;
00150     };
00151 
00152 
00153     // --------- WizFi2550_sock.cpp ---------
00154     int getHostByName   (const char * host, char *ip);
00155     int open            (Protocol proto, const char *ip, int remotePort, int localPort = 0, void(*func)(int) = NULL);
00156     int listen          (Protocol proto, int port, void(*func)(int)=NULL);
00157     int close           (int cid);
00158     void initCon        (int cid, bool connected);
00159     int send            (int cid, const char *buf, int len);
00160     int sendto          (int cid, const char *buf, int len, const char *ip, int port);
00161     int recv            (int cid, char *buf, int len);
00162     int recvfrom        (int cid, char *buf, int len, char *ip, int *port);
00163     int readable        (int cid);
00164     bool isConnected    (int cid);
00165     int accept          (int cid);
00166     int getRemote       (int cid, char **ip, int *port);
00167 
00168 
00169 
00170 protected:
00171     static WizFi250 * _inst;
00172 
00173 
00174     // Serial _wizfi
00175     RawSerial _wizfi;
00176     int _baud;
00177     DigitalIn *_cts;
00178     DigitalOut *_rts;
00179     int _flow;
00180 
00181     DigitalInOut _reset;
00182 
00183     struct STATE {
00184         WiFiMode wm;
00185         Security sec;
00186         char ssid[35];
00187         char pass[66];
00188         char ip[16];
00189         char netmask[16];
00190         char gateway[16];
00191         char nameserver[16];
00192         char mac[18];
00193         char resolv[16];
00194         char name[32];
00195         int rssi;
00196         bool dhcp;
00197         time_t time;
00198 
00199         bool initialized;
00200         bool associated;
00201         volatile Mode mode;
00202         volatile Status status;
00203         bool escape;
00204         volatile bool ok, failure;
00205         volatile Response res;
00206         int cid;
00207         int n;
00208         CircBuffer<char> *buf;
00209         char dbgRespBuf[BUF_SIZE];
00210         //char dummyBuf[BUF_SIZE];
00211     } _state;
00212 
00213 
00214 public:
00215     struct CONNECTION {
00216         Protocol protocol;
00217         Type type;
00218         bool connected;
00219         char ip[16];
00220         int port;
00221         int send_length;
00222         int recv_length;
00223         CircBuffer<char> *buf;
00224         volatile bool received;
00225         volatile int parent;
00226         volatile bool accept;
00227         void(*func)(int);
00228     } _con[8];
00229 
00230     // --------- WizFi250.cpp ---------
00231 
00232     int join(WiFiMode mode);
00233     bool isAssociated();
00234 
00235     int limitedap ();
00236     int dissociate ();
00237     int disconnect () {
00238         return dissociate();
00239     }
00240 
00241     Status getStatus ();
00242 
00243     int setMacAddress   (const char *mac);
00244     int getMacAddress   (char *mac);
00245     int setAddress      (const char *name = NULL);
00246     int setAddress      (const char *ip, const char *netmask, const char *gateway, const char *dns = NULL, const char *name = NULL);
00247     int getAddress      (char *ip, char *netmask, char *gateway);
00248     int setSsid         (const char *ssid);
00249     int setSec          (Security sec, const char *phrase);
00250 
00251 
00252     // --------- WizFi250_msg.cpp ---------
00253     void recvData       ( char c );
00254     int  parseMessage   ();
00255     void msgOk          (const char *buf);
00256     void msgError       (const char *buf);
00257     void msgConnect     (const char *buf);
00258     void msgDisconnect  (const char *buf);
00259     void msgListen      (const char *buf);
00260 
00261     void resMacAddress  (const char *buf);
00262     void resWJOIN       (const char *buf);
00263     void resConnect     (const char *buf);
00264     void resSSEND       (const char *buf);
00265     void resFDNS        (const char *buf);
00266     void resSMGMT       (const char *buf);
00267     void resWSTATUS     (const char *buf);
00268 
00269 
00270     // --------- WizFi250_hal.cpp ---------
00271     void setReset   (bool flg);
00272     void isrUart    ();
00273     int  getUart    ();
00274     void putUart    (char c);
00275     void setRts     (bool flg);
00276     int  lockUart   (int ms);
00277     void unlockUart ();
00278     void initUart   (PinName cts, PinName rts, PinName alarm, int baud);
00279 
00280 
00281     // --------- WizFi2550_util.cpp ---------
00282     int x2i     (char c);
00283     int i2x     (int i);
00284 
00285 
00286 
00287 };
00288 
00289 #endif /* WIZFI250_H_ */