cliff

Fork of WizFi250Interface by WIZnet

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 cmdWLEAVE   ();
00140     int cmdWSTATUS  ();
00141     int cmdSCON     ( const char *openType, const char *socketType, int localPort, const char *dataMode = "0");
00142     int cmdSCON     ( const char *openType, const char *socketType, const char *remoteIp, int remotePort, int localPort = 0, const char *dataMode = "0");
00143     int cmdSSEND    ( const char *data, int cid, int sendSize, const char *remoteIp = NULL, int remotePort = 0, int Timeout = 2000 );
00144     int cmdCLOSE    ( int cid );
00145     int cmdFDNS     (const char *host);
00146     int cmdSMGMT    ( int cid );
00147 
00148 
00149     static WizFi250 * getInstance() {
00150         return _inst;
00151     };
00152 
00153 
00154     // --------- WizFi2550_sock.cpp ---------
00155     int getHostByName   (const char * host, char *ip);
00156     int open            (Protocol proto, const char *ip, int remotePort, int localPort = 0, void(*func)(int) = NULL);
00157     int listen          (Protocol proto, int port, void(*func)(int)=NULL);
00158     int close           (int cid);
00159     void initCon        (int cid, bool connected);
00160     int send            (int cid, const char *buf, int len);
00161     int sendto          (int cid, const char *buf, int len, const char *ip, int port);
00162     int recv            (int cid, char *buf, int len);
00163     int recvfrom        (int cid, char *buf, int len, char *ip, int *port);
00164     int readable        (int cid);
00165     bool isConnected    (int cid);
00166     int accept          (int cid);
00167     int getRemote       (int cid, char **ip, int *port);
00168 
00169 
00170 
00171 protected:
00172     static WizFi250 * _inst;
00173 
00174 
00175     // Serial _wizfi
00176     RawSerial _wizfi;
00177     int _baud;
00178     DigitalIn *_cts;
00179     DigitalOut *_rts;
00180     int _flow;
00181 
00182     DigitalInOut _reset;
00183 
00184     struct STATE {
00185         WiFiMode wm;
00186         Security sec;
00187         char ssid[35];
00188         char pass[66];
00189         char ip[16];
00190         char netmask[16];
00191         char gateway[16];
00192         char nameserver[16];
00193         char mac[18];
00194         char resolv[16];
00195         char name[32];
00196         int rssi;
00197         bool dhcp;
00198         time_t time;
00199 
00200         bool initialized;
00201         bool associated;
00202         volatile Mode mode;
00203         volatile Status status;
00204         bool escape;
00205         volatile bool ok, failure;
00206         volatile Response res;
00207         int cid;
00208         int n;
00209         CircBuffer<char> *buf;
00210         char dbgRespBuf[BUF_SIZE];
00211         //char dummyBuf[BUF_SIZE];
00212     } _state;
00213 
00214 
00215 public:
00216     struct CONNECTION {
00217         Protocol protocol;
00218         Type type;
00219         bool connected;
00220         char ip[16];
00221         int port;
00222         int send_length;
00223         int recv_length;
00224         CircBuffer<char> *buf;
00225         volatile bool received;
00226         volatile int parent;
00227         volatile bool accept;
00228         void(*func)(int);
00229     } _con[8];
00230 
00231     // --------- WizFi250.cpp ---------
00232 
00233     int join(WiFiMode mode);
00234     bool isAssociated();
00235 
00236     //int limitedap ();
00237     //int dissociate ();
00238     /*
00239     int disconnect () {
00240         return dissociate();
00241     }
00242     */
00243 
00244     Status getStatus ();
00245 
00246     int setMacAddress   (const char *mac);
00247     int getMacAddress   (char *mac);
00248     int setAddress      (const char *name = NULL);
00249     int setAddress      (const char *ip, const char *netmask, const char *gateway, const char *dns = NULL, const char *name = NULL);
00250     int getAddress      (char *ip, char *netmask, char *gateway);
00251     int setSsid         (const char *ssid);
00252     int setSec          (Security sec, const char *phrase);
00253 
00254     const char* getIPAddress    (void);
00255     const char* getMACAddress   (void);
00256 
00257     // --------- WizFi250_msg.cpp ---------
00258     void recvData       ( char c );
00259     int  parseMessage   ();
00260     void msgOk          (const char *buf);
00261     void msgError       (const char *buf);
00262     void msgConnect     (const char *buf);
00263     void msgDisconnect  (const char *buf);
00264     void msgListen      (const char *buf);
00265 
00266     void resMacAddress  (const char *buf);
00267     void resWJOIN       (const char *buf);
00268     void resConnect     (const char *buf);
00269     void resSSEND       (const char *buf);
00270     void resFDNS        (const char *buf);
00271     void resSMGMT       (const char *buf);
00272     void resWSTATUS     (const char *buf);
00273 
00274 
00275     // --------- WizFi250_hal.cpp ---------
00276     void setReset   (bool flg);
00277     void isrUart    ();
00278     int  getUart    ();
00279     void putUart    (char c);
00280     void setRts     (bool flg);
00281     int  lockUart   (int ms);
00282     void unlockUart ();
00283     void initUart   (PinName cts, PinName rts, PinName alarm, int baud);
00284 
00285 
00286     // --------- WizFi2550_util.cpp ---------
00287     int x2i     (char c);
00288     int i2x     (int i);
00289 
00290     // --------- WizFi250_ifc.cpp (For NetworkSocketAPI) ---------
00291     /**
00292     * Startup the WizFi250
00293     *
00294     * @return true only if WizFi250 was setup correctly
00295     */
00296     bool startup(void);
00297 
00298     /**
00299     * Reset WizFi250
00300     *
00301     * @return true only if WizFi250 resets successfully
00302     */
00303     bool reset(void);
00304 
00305     /**
00306     * Disconnect WizFi250 from AP
00307     *
00308     * @return true only if WizFi250 is disconnected successfully
00309     */
00310     bool disconnect(void);
00311 
00312     /**
00313     * Check if WizFi250 is conenected
00314     *
00315     * @return true only if the chip has an IP address
00316     */
00317     bool isConnected(void);
00318 
00319 };
00320 
00321 #endif /* WIZFI250_H_ */