ajeet prajapati / WizFi310Interface_Legacy45566

Fork of WizFi310Interface_Legacy by WIZnet

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers WizFi310.h Source File

WizFi310.h

00001 /*
00002  * Copyright (C) 2013 gsfan, MIT License
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00005  * and associated documentation files (the "Software"), to deal in the Software without restriction,
00006  * including without limitation the rights to use, copy, modify, merge, publish, distribute,
00007  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
00008  * furnished to do so, subject to the following conditions:
00009  *
00010  * The above copyright notice and this permission notice shall be included in all copies or
00011  * substantial portions of the Software.
00012  *
00013  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00014  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00015  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00016  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00017  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00018  */
00019 /* Copyright (C) 2014 Wiznet, MIT License
00020  *  port to the Wiznet Module WizFi250
00021  */
00022 /* Copyright (C) 2017 Wiznet, MIT License
00023  *  port to the Wiznet Module WizFi310
00024  */
00025 
00026 #ifndef WIZFI310_H_
00027 #define WIZFI310_H_
00028 
00029 #include "WizFi310_conf.h"
00030 
00031 #include "mbed.h"
00032 #include "RawSerial.h"
00033 #include "Serial.h"
00034 #include "CBuffer.h"
00035 #include <ctype.h>
00036 #include <stdlib.h>
00037 #include <string>
00038 
00039 using namespace std;
00040 
00041 #define BUF_SIZE 1600
00042 
00043 //Debug is disabled by default
00044 #if 1
00045 #define WIZ_DBG(x, ...) std::printf("[WizFi310: DBG]" x "\r\n", ##__VA_ARGS__);
00046 #define WIZ_WARN(x, ...) std::printf("[WizFi310: WARN]" x "\r\n", ##__VA_ARGS__);
00047 #define WIZ_ERR(x, ...) std::printf("[WizFi310: ERR]" x "\r\n", ##__VA_ARGS__);
00048 #define WIZ_INFO(x, ...) std::printf("[WizFi310: INFO]" x "\r\n", ##__VA_ARGS__);
00049 #else
00050 #define WIZ_DBG(x, ...)
00051 #define WIZ_WARN(x, ...)
00052 #define WIZ_ERR(x, ...)
00053 #define WIZ_INFO(x, ...)
00054 #endif
00055 
00056 
00057 class WizFi310
00058 {
00059 public:
00060 
00061     enum AntennaMode{
00062         PCB = 0,
00063         UFL = 1,
00064         AUTO = 3,
00065     };
00066 
00067     enum WiFiMode {
00068         WM_STATION = 0,
00069         WM_AP = 1,
00070     };
00071 
00072     /** Wi-Fi security
00073      */
00074     enum Security {
00075         // kaizen need to change
00076         SEC_AUTO        = 0,
00077         SEC_OPEN        = 1,
00078         SEC_WEP         = 2,
00079         SEC_WPA_TKIP    = 3,
00080         SEC_WPA_AES     = 4,
00081         SEC_WPA2_AES    = 5,
00082         SEC_WPA2_TKIP   = 6,
00083         SEC_WPA2_MIXED  = 7,
00084     };
00085 
00086     /** TCP/IP protocol
00087      */
00088     enum Protocol {
00089         PROTO_UDP = 0,
00090         PROTO_TCP = 1,
00091     };
00092 
00093     /** Client/Server
00094      */
00095     enum Type {
00096         TYPE_CLIENT = 0,
00097         TYPE_SERVER = 1,
00098     };
00099 
00100     enum Response {
00101         RES_NULL,
00102         RES_MACADDRESS,
00103         RES_WJOIN,
00104         RES_CONNECT,
00105         RES_SSEND,
00106         RES_FDNS,
00107         RES_SMGMT,
00108         RES_WSTATUS,
00109         
00110     };
00111 
00112     enum Mode {
00113         MODE_COMMAND,
00114         MODE_CMDRESP,
00115         MODE_DATA_RX,
00116         MODE_DATA_RXUDP,
00117         MODE_DATA_RXUDP_BULK,
00118     };
00119 
00120     enum Status {
00121         STAT_NONE,
00122         STAT_READY,
00123         STAT_STANDBY,
00124         STAT_WAKEUP,
00125         STAT_DEEPSLEEP,
00126     };
00127 
00128 
00129     WizFi310 (PinName tx, PinName rx, PinName cts, PinName rts, PinName reset, PinName alarm = NC, int baud = 115200);
00130 
00131     // --------- WizFi250_at.cpp ---------
00132     void clearFlags     ();
00133     int  sendCommand    (const char * cmd, Response res = RES_NULL, int timeout = DEFAULT_WAIT_RESP_TIMEOUT, int opt = 1);
00134 
00135     int cmdAT       ();
00136     int cmdMECHO    (bool flg);
00137     int cmdUSET     (int baud, char *flow);
00138     int cmdMMAC     (const char *mac = NULL);
00139     int cmdWSET     (WiFiMode mode, const char *ssid, const char *bssid = NULL, int channel = 1);
00140     int cmdWANT     (AntennaMode mode);
00141     int cmdWNET     (bool is_dhcp);
00142     int cmdWSEC     (WiFiMode mode, const char *key, const char *sec = NULL);
00143     int cmdWJOIN    ();
00144     int cmdWLEAVE   ();
00145     int cmdWSTATUS  ();
00146     int cmdSCON     ( const char *openType, const char *socketType, int localPort, const char *dataMode = "0");
00147     int cmdSCON     ( const char *openType, const char *socketType, const char *remoteIp, int remotePort, int localPort = 0, const char *dataMode = "0");
00148     int cmdSSEND    ( const char *data, int cid, int sendSize, const char *remoteIp = NULL, int remotePort = 0, int Timeout = 2000 );
00149     int cmdCLOSE    ( int cid );
00150     int cmdFDNS     (const char *host);
00151     int cmdSMGMT    ( int cid );
00152 
00153 
00154     static WizFi310 * getInstance() {
00155         return _inst;
00156     };
00157 
00158 
00159     // --------- WizFi2550_sock.cpp ---------
00160     int getHostByName   (const char * host, char *ip);
00161     int open            (Protocol proto, const char *ip, int remotePort, int localPort = 0, void(*func)(int) = NULL);
00162     int listen          (Protocol proto, int port, void(*func)(int)=NULL);
00163     int close           (int cid);
00164     void initCon        (int cid, bool connected);
00165     int send            (int cid, const char *buf, int len);
00166     int sendto          (int cid, const char *buf, int len, const char *ip, int port);
00167     int recv            (int cid, char *buf, int len);
00168     int recvfrom        (int cid, char *buf, int len, char *ip, int *port);
00169     int readable        (int cid);
00170     bool isConnected    (int cid);
00171     int accept          (int cid);
00172     int getRemote       (int cid, char **ip, int *port);
00173         
00174 
00175 protected:
00176     static WizFi310 * _inst;
00177 
00178 
00179     // Serial _wizfi
00180     RawSerial _wizfi;
00181     int _baud;
00182     DigitalIn *_cts;
00183     DigitalOut *_rts;
00184     int _flow;
00185 
00186     DigitalInOut _reset;
00187 
00188     struct STATE {
00189         WiFiMode wm;
00190         
00191         Security sec;
00192         char ssid[35];
00193         char pass[66];
00194         char ip[16];
00195         char netmask[16];
00196         char gateway[16];
00197         char nameserver[16];
00198         char mac[18];
00199         char resolv[16];
00200         char name[32];
00201         int rssi;
00202         bool dhcp;
00203         time_t time;
00204 
00205         bool initialized;
00206         bool associated;
00207         volatile Mode mode;
00208         volatile Status status;
00209         bool escape;
00210         volatile bool ok, failure;
00211         volatile Response res;
00212         int cid;
00213         int n;
00214         CircBuffer<char> *buf;
00215         char dbgRespBuf[BUF_SIZE];
00216         //char dummyBuf[BUF_SIZE];
00217     } _state;
00218 
00219 
00220 public:
00221     struct CONNECTION {
00222         Protocol protocol;
00223         Type type;
00224         bool connected;
00225         char ip[16];
00226         int port;
00227         int send_length;
00228         int recv_length;
00229         CircBuffer<char> *buf;
00230         volatile bool received;
00231         volatile int parent;
00232         volatile bool accept;
00233         void(*func)(int);
00234     } _con[8];
00235 
00236     // --------- WizFi310.cpp ---------
00237 
00238     int join(WiFiMode mode);
00239     bool isAssociated();
00240 
00241     //int limitedap ();
00242     //int dissociate ();
00243     /*
00244     int disconnect () {
00245         return dissociate();
00246     }
00247     */
00248 
00249     Status getStatus ();
00250 
00251     int setMacAddress   (const char *mac);
00252     int getMacAddress   (char *mac);
00253     int setAddress      (const char *name = NULL);
00254     int setAddress      (const char *ip, const char *netmask, const char *gateway, const char *dns = NULL, const char *name = NULL);
00255     int getAddress      (char *ip, char *netmask, char *gateway);
00256     int setSsid         (const char *ssid);
00257     int setSec          (Security sec, const char *phrase);
00258 
00259     const char* getIPAddress    (void);
00260     const char* getMACAddress   (void);
00261 
00262     // --------- WizFi250_msg.cpp ---------
00263     void recvData       ( char c );
00264     int  parseMessage   ();
00265     void msgOk          (const char *buf);
00266     void msgError       (const char *buf);
00267     void msgConnect     (const char *buf);
00268     void msgDisconnect  (const char *buf);
00269     void msgListen      (const char *buf);
00270     //daniel
00271     void msgMQTTConnect (const char *buf);
00272     void msgMQTTDisconnect (const char *buf);
00273 
00274     void resMacAddress  (const char *buf);
00275     void resWJOIN       (const char *buf);
00276     void resConnect     (const char *buf);
00277     void resSSEND       (const char *buf);
00278     void resFDNS        (const char *buf);
00279     void resSMGMT       (const char *buf);
00280     void resWSTATUS     (const char *buf);
00281 
00282 
00283     // --------- WizFi250_hal.cpp ---------
00284     void setReset   (bool flg);
00285     void isrUart    ();
00286     int  getUart    ();
00287     void putUart    (char c);
00288     void setRts     (bool flg);
00289     int  lockUart   (int ms);
00290     void unlockUart ();
00291     void initUart   (PinName cts, PinName rts, PinName alarm, int baud);
00292 
00293 
00294     // --------- WizFi2550_util.cpp ---------
00295     int x2i     (char c);
00296     int i2x     (int i);
00297 
00298     // --------- WizFi250_ifc.cpp (For NetworkSocketAPI) ---------
00299     /**
00300     * Startup the WizFi310
00301     *
00302     * @return true only if WizFi310 was setup correctly
00303     */
00304     bool startup(void);
00305 
00306     /**
00307     * Reset WizFi310
00308     *
00309     * @return true only if WizFi310 resets successfully
00310     */
00311     bool reset(void);
00312 
00313     /**
00314     * Disconnect WizFi310 from AP
00315     *
00316     * @return true only if WizFi310 is disconnected successfully
00317     */
00318     bool disconnect(void);
00319 
00320     /**
00321     * Check if WizFi310 is conenected
00322     *
00323     * @return true only if the chip has an IP address
00324     */
00325     bool isConnected(void);
00326     
00327     
00328     //daniel for mqtt
00329     char rcvd_mqtt_topic[128];
00330     
00331      bool test(void);
00332 
00333 };
00334 
00335 #endif /* WIZFI250_H_ */