ThingPlug Test

Dependents:   WizFi310_ThingPlug_Test WizFi310_ThingPlug_Test_P

Fork of WizFi310Interface by WIZnet

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