Freedman v2

Dependents:   Freedman_v2

Fork of WizFi250Interface by DongEun Koak

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 0
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 WiFiMode {
00058         WM_STATION = 0,
00059         WM_AP = 1,
00060     };
00061 
00062     /** Wi-Fi security
00063      */
00064     enum Security {
00065         // kaizen need to change
00066         SEC_AUTO        = 0,
00067         SEC_OPEN        = 1,
00068         SEC_WEP         = 2,
00069         SEC_WPA_TKIP    = 3,
00070         SEC_WPA_AES     = 4,
00071         SEC_WPA2_AES    = 5,
00072         SEC_WPA2_TKIP   = 6,
00073         SEC_WPA2_MIXED  = 7,
00074     };
00075 
00076     /** TCP/IP protocol
00077      */
00078     enum Protocol {
00079         PROTO_UDP = 0,
00080         PROTO_TCP = 1,
00081     };
00082 
00083     /** Client/Server
00084      */
00085     enum Type {
00086         TYPE_CLIENT = 0,
00087         TYPE_SERVER = 1,
00088     };
00089 
00090     enum Response {
00091         RES_NULL,
00092         RES_MACADDRESS,
00093         RES_WJOIN,
00094         RES_CONNECT,
00095         RES_SSEND,
00096         RES_FDNS,
00097         RES_SMGMT,
00098         RES_WSTATUS,
00099     };
00100 
00101     enum Mode {
00102         MODE_COMMAND,
00103         MODE_CMDRESP,
00104         MODE_DATA_RX,
00105         MODE_DATA_RXUDP,
00106         MODE_DATA_RXUDP_BULK,
00107     };
00108 
00109     enum Status {
00110         STAT_NONE,
00111         STAT_READY,
00112         STAT_STANDBY,
00113         STAT_WAKEUP,
00114         STAT_DEEPSLEEP,
00115     };
00116 
00117 
00118     WizFi250 (PinName tx, PinName rx, PinName cts, PinName rts, PinName reset, PinName alarm = NC, int baud = 115200);
00119 
00120     // --------- WizFi250_at.cpp ---------
00121     void clearFlags     ();
00122     int  sendCommand    (const char * cmd, Response res = RES_NULL, int timeout = DEFAULT_WAIT_RESP_TIMEOUT, int opt = 2);
00123 
00124     int cmdAT       ();
00125     int cmdMECHO    (bool flg);
00126     int cmdUSET     (int baud, char *flow);
00127     int cmdMMAC     (const char *mac = NULL);
00128     int cmdWSET     (WiFiMode mode, const char *ssid, const char *bssid = NULL, int channel = 1);
00129     int cmdWNET     (bool is_dhcp);
00130     int cmdWSEC     (WiFiMode mode, const char *key, const char *sec = NULL);
00131     int cmdWJOIN    ();
00132     int cmdWSTATUS  ();
00133     int cmdSCON     ( const char *openType, const char *socketType, int localPort, const char *dataMode = "0");
00134     int cmdSCON     ( const char *openType, const char *socketType, const char *remoteIp, int remotePort, int localPort = 0, const char *dataMode = "0");
00135     int cmdSSEND    ( const char *data, int cid, int sendSize, const char *remoteIp = NULL, int remotePort = 0, int Timeout = 2000 );
00136     int cmdCLOSE    ( int cid );
00137     int cmdFDNS     (const char *host);
00138     int cmdSMGMT    ( int cid );
00139 
00140 
00141     static WizFi250 * getInstance() {
00142         return _inst;
00143     };
00144 
00145 
00146     // --------- WizFi2550_sock.cpp ---------
00147     int getHostByName   (const char * host, char *ip);
00148     int open            (Protocol proto, const char *ip, int remotePort, int localPort = 0, void(*func)(int) = NULL);
00149     int listen          (Protocol proto, int port, void(*func)(int)=NULL);
00150     int close           (int cid);
00151     void initCon        (int cid, bool connected);
00152     int send            (int cid, const char *buf, int len);
00153     int sendto          (int cid, const char *buf, int len, const char *ip, int port);
00154     int recv            (int cid, char *buf, int len);
00155     int recvfrom        (int cid, char *buf, int len, char *ip, int *port);
00156     int readable        (int cid);
00157     bool isConnected    (int cid);
00158     int accept          (int cid);
00159     int getRemote       (int cid, char **ip, int *port);
00160 
00161 
00162 
00163 protected:
00164     static WizFi250 * _inst;
00165 
00166 
00167     // Serial _wizfi
00168     RawSerial _wizfi;
00169     int _baud;
00170     DigitalIn *_cts;
00171     DigitalOut *_rts;
00172     int _flow;
00173 
00174     DigitalInOut _reset;
00175 
00176     struct STATE {
00177         WiFiMode wm;
00178         Security sec;
00179         char ssid[35];
00180         char pass[66];
00181         char ip[16];
00182         char netmask[16];
00183         char gateway[16];
00184         char nameserver[16];
00185         char mac[18];
00186         char resolv[16];
00187         char name[32];
00188         int rssi;
00189         bool dhcp;
00190         time_t time;
00191 
00192         bool initialized;
00193         bool associated;
00194         volatile Mode mode;
00195         volatile Status status;
00196         bool escape;
00197         volatile bool ok, failure;
00198         volatile Response res;
00199         int cid;
00200         int n;
00201         CircBuffer<char> *buf;
00202         char dbgRespBuf[BUF_SIZE];
00203         //char dummyBuf[BUF_SIZE];
00204     } _state;
00205 
00206 
00207 public:
00208     struct CONNECTION {
00209         Protocol protocol;
00210         Type type;
00211         bool connected;
00212         char ip[16];
00213         int port;
00214         int send_length;
00215         int recv_length;
00216         CircBuffer<char> *buf;
00217         volatile bool received;
00218         volatile int parent;
00219         volatile bool accept;
00220         void(*func)(int);
00221     } _con[8];
00222 
00223     // --------- WizFi250.cpp ---------
00224 
00225     int join(WiFiMode mode);
00226     bool isAssociated();
00227 
00228     int limitedap ();
00229     int dissociate ();
00230     int disconnect () {
00231         return dissociate();
00232     }
00233 
00234     Status getStatus ();
00235 
00236     int setMacAddress   (const char *mac);
00237     int getMacAddress   (char *mac);
00238     int setAddress      (const char *name = NULL);
00239     int setAddress      (const char *ip, const char *netmask, const char *gateway, const char *dns = NULL, const char *name = NULL);
00240     int getAddress      (char *ip, char *netmask, char *gateway);
00241     int setSsid         (const char *ssid);
00242     int setSec          (Security sec, const char *phrase);
00243 
00244 
00245     // --------- WizFi250_msg.cpp ---------
00246     void recvData       ( char c );
00247     int  parseMessage   ();
00248     void msgOk          (const char *buf);
00249     void msgError       (const char *buf);
00250     void msgConnect     (const char *buf);
00251     void msgDisconnect  (const char *buf);
00252     void msgListen      (const char *buf);
00253 
00254     void resMacAddress  (const char *buf);
00255     void resWJOIN       (const char *buf);
00256     void resConnect     (const char *buf);
00257     void resSSEND       (const char *buf);
00258     void resFDNS        (const char *buf);
00259     void resSMGMT       (const char *buf);
00260     void resWSTATUS     (const char *buf);
00261 
00262 
00263     // --------- WizFi250_hal.cpp ---------
00264     void setReset   (bool flg);
00265     void isrUart    ();
00266     int  getUart    ();
00267     void putUart    (char c);
00268     void setRts     (bool flg);
00269     int  lockUart   (int ms);
00270     void unlockUart ();
00271     void initUart   (PinName cts, PinName rts, PinName alarm, int baud);
00272 
00273 
00274     // --------- WizFi2550_util.cpp ---------
00275     int x2i     (char c);
00276     int i2x     (int i);
00277 
00278 
00279 
00280 };
00281 
00282 #endif /* WIZFI250_H_ */