WIZnet / WizFi310Interface

Dependents:   KT_IoTMakers_WizFi310_Example WizFi310_STATION_HelloWorld WizFi310_DNS_TCP_HelloWorld WizFi310_Ubidots ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers WizFi310.h Source File

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