Akshay Tom / WizFi310Interface_Legacynew

Dependencies:   WizFi310Interface_Legacynew

Dependents:   w7500-mqtt-wizfi310 w7500-mqtt-wizfi310 w7500-mqtt-wizfi310

Fork of WizFi310Interface_Legacynew by ajeet prajapati

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