ThingPlug Test

Dependents:   WizFi310_ThingPlug_Test WizFi310_ThingPlug_Test_P

Fork of WizFi310Interface by WIZnet

Committer:
jehoon
Date:
Wed Oct 05 09:40:30 2016 +0000
Revision:
0:df571f8f8c03
Child:
1:16e57103a7dd
This is a WizFi310 Library. It supports new mbed5 features including NetworkInterfaceAPI.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jehoon 0:df571f8f8c03 1 /* Copyright (C) 2013 gsfan, MIT License
jehoon 0:df571f8f8c03 2 *
jehoon 0:df571f8f8c03 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
jehoon 0:df571f8f8c03 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
jehoon 0:df571f8f8c03 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
jehoon 0:df571f8f8c03 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
jehoon 0:df571f8f8c03 7 * furnished to do so, subject to the following conditions:
jehoon 0:df571f8f8c03 8 *
jehoon 0:df571f8f8c03 9 * The above copyright notice and this permission notice shall be included in all copies or
jehoon 0:df571f8f8c03 10 * substantial portions of the Software.
jehoon 0:df571f8f8c03 11 *
jehoon 0:df571f8f8c03 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
jehoon 0:df571f8f8c03 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
jehoon 0:df571f8f8c03 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
jehoon 0:df571f8f8c03 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
jehoon 0:df571f8f8c03 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
jehoon 0:df571f8f8c03 17 */
jehoon 0:df571f8f8c03 18 /* Copyright (C) 2014 Wiznet, MIT License
jehoon 0:df571f8f8c03 19 * port to the Wiznet Module WizFi310
jehoon 0:df571f8f8c03 20 */
jehoon 0:df571f8f8c03 21
jehoon 0:df571f8f8c03 22 #ifndef WIZFI310_H_
jehoon 0:df571f8f8c03 23 #define WIZFI310_H_
jehoon 0:df571f8f8c03 24
jehoon 0:df571f8f8c03 25 #include "WizFi310_conf.h"
jehoon 0:df571f8f8c03 26
jehoon 0:df571f8f8c03 27 #include "mbed.h"
jehoon 0:df571f8f8c03 28 #include "RawSerial.h"
jehoon 0:df571f8f8c03 29 #include "Serial.h"
jehoon 0:df571f8f8c03 30 #include "CBuffer.h"
jehoon 0:df571f8f8c03 31 #include <ctype.h>
jehoon 0:df571f8f8c03 32 #include <stdlib.h>
jehoon 0:df571f8f8c03 33 #include <string>
jehoon 0:df571f8f8c03 34 #include "WiFiInterface.h"
jehoon 0:df571f8f8c03 35
jehoon 0:df571f8f8c03 36 using namespace std;
jehoon 0:df571f8f8c03 37
jehoon 0:df571f8f8c03 38 #define BUF_SIZE 1600
jehoon 0:df571f8f8c03 39
jehoon 0:df571f8f8c03 40 //Debug is disabled by default
jehoon 0:df571f8f8c03 41 #if 1
jehoon 0:df571f8f8c03 42 #define WIZ_DBG(x, ...) std::printf("[WizFi310: DBG]" x "\r\n", ##__VA_ARGS__);
jehoon 0:df571f8f8c03 43 #define WIZ_WARN(x, ...) std::printf("[WizFi310: WARN]" x "\r\n", ##__VA_ARGS__);
jehoon 0:df571f8f8c03 44 #define WIZ_ERR(x, ...) std::printf("[WizFi310: ERR]" x "\r\n", ##__VA_ARGS__);
jehoon 0:df571f8f8c03 45 #define WIZ_INFO(x, ...) std::printf("[WizFi310: INFO]" x "\r\n", ##__VA_ARGS__);
jehoon 0:df571f8f8c03 46 #else
jehoon 0:df571f8f8c03 47 #define WIZ_DBG(x, ...)
jehoon 0:df571f8f8c03 48 #define WIZ_WARN(x, ...)
jehoon 0:df571f8f8c03 49 #define WIZ_ERR(x, ...)
jehoon 0:df571f8f8c03 50 #define WIZ_INFO(x, ...)
jehoon 0:df571f8f8c03 51 #endif
jehoon 0:df571f8f8c03 52
jehoon 0:df571f8f8c03 53
jehoon 0:df571f8f8c03 54 class WizFi310
jehoon 0:df571f8f8c03 55 {
jehoon 0:df571f8f8c03 56 public:
jehoon 0:df571f8f8c03 57
jehoon 0:df571f8f8c03 58 enum AntennaMode{
jehoon 0:df571f8f8c03 59 PCB = 0,
jehoon 0:df571f8f8c03 60 UFL = 1,
jehoon 0:df571f8f8c03 61 AUTO = 3,
jehoon 0:df571f8f8c03 62 };
jehoon 0:df571f8f8c03 63
jehoon 0:df571f8f8c03 64 enum WiFiMode {
jehoon 0:df571f8f8c03 65 WM_STATION = 0,
jehoon 0:df571f8f8c03 66 WM_AP = 1,
jehoon 0:df571f8f8c03 67 };
jehoon 0:df571f8f8c03 68
jehoon 0:df571f8f8c03 69 /** Wi-Fi security
jehoon 0:df571f8f8c03 70 */
jehoon 0:df571f8f8c03 71 enum Security {
jehoon 0:df571f8f8c03 72 // kaizen need to change
jehoon 0:df571f8f8c03 73 SEC_AUTO = 0,
jehoon 0:df571f8f8c03 74 SEC_OPEN = 1,
jehoon 0:df571f8f8c03 75 SEC_WEP = 2,
jehoon 0:df571f8f8c03 76 SEC_WPA_TKIP = 3,
jehoon 0:df571f8f8c03 77 SEC_WPA_AES = 4,
jehoon 0:df571f8f8c03 78 SEC_WPA2_AES = 5,
jehoon 0:df571f8f8c03 79 SEC_WPA2_TKIP = 6,
jehoon 0:df571f8f8c03 80 SEC_WPA2_MIXED = 7,
jehoon 0:df571f8f8c03 81 };
jehoon 0:df571f8f8c03 82
jehoon 0:df571f8f8c03 83 /** TCP/IP protocol
jehoon 0:df571f8f8c03 84 */
jehoon 0:df571f8f8c03 85 enum Protocol {
jehoon 0:df571f8f8c03 86 PROTO_UDP = 0,
jehoon 0:df571f8f8c03 87 PROTO_TCP = 1,
jehoon 0:df571f8f8c03 88 };
jehoon 0:df571f8f8c03 89
jehoon 0:df571f8f8c03 90 /** Client/Server
jehoon 0:df571f8f8c03 91 */
jehoon 0:df571f8f8c03 92 enum Type {
jehoon 0:df571f8f8c03 93 TYPE_CLIENT = 0,
jehoon 0:df571f8f8c03 94 TYPE_SERVER = 1,
jehoon 0:df571f8f8c03 95 };
jehoon 0:df571f8f8c03 96
jehoon 0:df571f8f8c03 97 enum Response {
jehoon 0:df571f8f8c03 98 RES_NULL,
jehoon 0:df571f8f8c03 99 RES_MACADDRESS,
jehoon 0:df571f8f8c03 100 RES_WJOIN,
jehoon 0:df571f8f8c03 101 RES_CONNECT,
jehoon 0:df571f8f8c03 102 RES_SSEND,
jehoon 0:df571f8f8c03 103 RES_FDNS,
jehoon 0:df571f8f8c03 104 RES_SMGMT,
jehoon 0:df571f8f8c03 105 RES_WSTATUS,
jehoon 0:df571f8f8c03 106 };
jehoon 0:df571f8f8c03 107
jehoon 0:df571f8f8c03 108 enum Mode {
jehoon 0:df571f8f8c03 109 MODE_COMMAND,
jehoon 0:df571f8f8c03 110 MODE_CMDRESP,
jehoon 0:df571f8f8c03 111 MODE_DATA_RX,
jehoon 0:df571f8f8c03 112 MODE_DATA_RXUDP,
jehoon 0:df571f8f8c03 113 MODE_DATA_RXUDP_BULK,
jehoon 0:df571f8f8c03 114 };
jehoon 0:df571f8f8c03 115
jehoon 0:df571f8f8c03 116 enum Status {
jehoon 0:df571f8f8c03 117 STAT_NONE,
jehoon 0:df571f8f8c03 118 STAT_READY,
jehoon 0:df571f8f8c03 119 STAT_STANDBY,
jehoon 0:df571f8f8c03 120 STAT_WAKEUP,
jehoon 0:df571f8f8c03 121 STAT_DEEPSLEEP,
jehoon 0:df571f8f8c03 122 };
jehoon 0:df571f8f8c03 123
jehoon 0:df571f8f8c03 124
jehoon 0:df571f8f8c03 125 WizFi310 (PinName tx, PinName rx, PinName cts, PinName rts, PinName reset, PinName alarm = NC, int baud = 115200);
jehoon 0:df571f8f8c03 126
jehoon 0:df571f8f8c03 127 // --------- WizFi250_at.cpp ---------
jehoon 0:df571f8f8c03 128 void clearFlags ();
jehoon 0:df571f8f8c03 129 int sendCommand (const char * cmd, Response res = RES_NULL, int timeout = DEFAULT_WAIT_RESP_TIMEOUT, int opt = 2);
jehoon 0:df571f8f8c03 130
jehoon 0:df571f8f8c03 131 int cmdAT ();
jehoon 0:df571f8f8c03 132 int cmdMECHO (bool flg);
jehoon 0:df571f8f8c03 133 int cmdUSET (int baud, char *flow);
jehoon 0:df571f8f8c03 134 int cmdMMAC (const char *mac = NULL);
jehoon 0:df571f8f8c03 135 int cmdWSET (WiFiMode mode, const char *ssid, const char *bssid = NULL, int channel = 1);
jehoon 0:df571f8f8c03 136 int cmdWANT (AntennaMode mode);
jehoon 0:df571f8f8c03 137 int cmdWNET (bool is_dhcp);
jehoon 0:df571f8f8c03 138 int cmdWSEC (WiFiMode mode, const char *key, const char *sec = NULL);
jehoon 0:df571f8f8c03 139 int cmdWJOIN ();
jehoon 0:df571f8f8c03 140 int cmdWLEAVE ();
jehoon 0:df571f8f8c03 141 int cmdWSTATUS ();
jehoon 0:df571f8f8c03 142 int cmdSCON ( const char *openType, const char *socketType, int localPort, const char *dataMode = "0");
jehoon 0:df571f8f8c03 143 int cmdSCON ( const char *openType, const char *socketType, const char *remoteIp, int remotePort, int localPort = 0, const char *dataMode = "0");
jehoon 0:df571f8f8c03 144 int cmdSSEND ( const char *data, int cid, int sendSize, const char *remoteIp = NULL, int remotePort = 0, int Timeout = 2000 );
jehoon 0:df571f8f8c03 145 int cmdCLOSE ( int cid );
jehoon 0:df571f8f8c03 146 int cmdFDNS (const char *host);
jehoon 0:df571f8f8c03 147 int cmdSMGMT ( int cid );
jehoon 0:df571f8f8c03 148
jehoon 0:df571f8f8c03 149
jehoon 0:df571f8f8c03 150 static WizFi310 * getInstance() {
jehoon 0:df571f8f8c03 151 return _inst;
jehoon 0:df571f8f8c03 152 };
jehoon 0:df571f8f8c03 153
jehoon 0:df571f8f8c03 154
jehoon 0:df571f8f8c03 155 // --------- WizFi2550_sock.cpp ---------
jehoon 0:df571f8f8c03 156 int getHostByName (const char * host, char *ip);
jehoon 0:df571f8f8c03 157 int open (Protocol proto, const char *ip, int remotePort, int localPort = 0, void(*func)(int) = NULL);
jehoon 0:df571f8f8c03 158 int listen (Protocol proto, int port, void(*func)(int)=NULL);
jehoon 0:df571f8f8c03 159 int close (int cid);
jehoon 0:df571f8f8c03 160 void initCon (int cid, bool connected);
jehoon 0:df571f8f8c03 161 int send (int cid, const char *buf, int len);
jehoon 0:df571f8f8c03 162 int sendto (int cid, const char *buf, int len, const char *ip, int port);
jehoon 0:df571f8f8c03 163 int recv (int cid, char *buf, int len);
jehoon 0:df571f8f8c03 164 int recvfrom (int cid, char *buf, int len, char *ip, int *port);
jehoon 0:df571f8f8c03 165 int readable (int cid);
jehoon 0:df571f8f8c03 166 bool isConnected (int cid);
jehoon 0:df571f8f8c03 167 int accept (int cid);
jehoon 0:df571f8f8c03 168 int getRemote (int cid, char **ip, int *port);
jehoon 0:df571f8f8c03 169
jehoon 0:df571f8f8c03 170
jehoon 0:df571f8f8c03 171 protected:
jehoon 0:df571f8f8c03 172 static WizFi310 * _inst;
jehoon 0:df571f8f8c03 173
jehoon 0:df571f8f8c03 174
jehoon 0:df571f8f8c03 175 // Serial _wizfi
jehoon 0:df571f8f8c03 176 RawSerial _wizfi;
jehoon 0:df571f8f8c03 177 int _baud;
jehoon 0:df571f8f8c03 178 DigitalIn *_cts;
jehoon 0:df571f8f8c03 179 DigitalOut *_rts;
jehoon 0:df571f8f8c03 180 int _flow;
jehoon 0:df571f8f8c03 181
jehoon 0:df571f8f8c03 182 DigitalInOut _reset;
jehoon 0:df571f8f8c03 183
jehoon 0:df571f8f8c03 184 struct STATE {
jehoon 0:df571f8f8c03 185 WiFiMode wm;
jehoon 0:df571f8f8c03 186
jehoon 0:df571f8f8c03 187 //daniel
jehoon 0:df571f8f8c03 188 //Security sec;
jehoon 0:df571f8f8c03 189 nsapi_security_t sec;
jehoon 0:df571f8f8c03 190 char ssid[35];
jehoon 0:df571f8f8c03 191 char pass[66];
jehoon 0:df571f8f8c03 192 char ip[16];
jehoon 0:df571f8f8c03 193 char netmask[16];
jehoon 0:df571f8f8c03 194 char gateway[16];
jehoon 0:df571f8f8c03 195 char nameserver[16];
jehoon 0:df571f8f8c03 196 char mac[18];
jehoon 0:df571f8f8c03 197 char resolv[16];
jehoon 0:df571f8f8c03 198 char name[32];
jehoon 0:df571f8f8c03 199 int rssi;
jehoon 0:df571f8f8c03 200 bool dhcp;
jehoon 0:df571f8f8c03 201 time_t time;
jehoon 0:df571f8f8c03 202
jehoon 0:df571f8f8c03 203 bool initialized;
jehoon 0:df571f8f8c03 204 bool associated;
jehoon 0:df571f8f8c03 205 volatile Mode mode;
jehoon 0:df571f8f8c03 206 volatile Status status;
jehoon 0:df571f8f8c03 207 bool escape;
jehoon 0:df571f8f8c03 208 volatile bool ok, failure;
jehoon 0:df571f8f8c03 209 volatile Response res;
jehoon 0:df571f8f8c03 210 int cid;
jehoon 0:df571f8f8c03 211 int n;
jehoon 0:df571f8f8c03 212 CircBuffer<char> *buf;
jehoon 0:df571f8f8c03 213 char dbgRespBuf[BUF_SIZE];
jehoon 0:df571f8f8c03 214 //char dummyBuf[BUF_SIZE];
jehoon 0:df571f8f8c03 215 } _state;
jehoon 0:df571f8f8c03 216
jehoon 0:df571f8f8c03 217
jehoon 0:df571f8f8c03 218 public:
jehoon 0:df571f8f8c03 219 struct CONNECTION {
jehoon 0:df571f8f8c03 220 Protocol protocol;
jehoon 0:df571f8f8c03 221 Type type;
jehoon 0:df571f8f8c03 222 bool connected;
jehoon 0:df571f8f8c03 223 char ip[16];
jehoon 0:df571f8f8c03 224 int port;
jehoon 0:df571f8f8c03 225 int send_length;
jehoon 0:df571f8f8c03 226 int recv_length;
jehoon 0:df571f8f8c03 227 CircBuffer<char> *buf;
jehoon 0:df571f8f8c03 228 volatile bool received;
jehoon 0:df571f8f8c03 229 volatile int parent;
jehoon 0:df571f8f8c03 230 volatile bool accept;
jehoon 0:df571f8f8c03 231 void(*func)(int);
jehoon 0:df571f8f8c03 232 } _con[8];
jehoon 0:df571f8f8c03 233
jehoon 0:df571f8f8c03 234 // --------- WizFi310.cpp ---------
jehoon 0:df571f8f8c03 235
jehoon 0:df571f8f8c03 236 int join(WiFiMode mode);
jehoon 0:df571f8f8c03 237 bool isAssociated();
jehoon 0:df571f8f8c03 238
jehoon 0:df571f8f8c03 239 //int limitedap ();
jehoon 0:df571f8f8c03 240 //int dissociate ();
jehoon 0:df571f8f8c03 241 /*
jehoon 0:df571f8f8c03 242 int disconnect () {
jehoon 0:df571f8f8c03 243 return dissociate();
jehoon 0:df571f8f8c03 244 }
jehoon 0:df571f8f8c03 245 */
jehoon 0:df571f8f8c03 246
jehoon 0:df571f8f8c03 247 Status getStatus ();
jehoon 0:df571f8f8c03 248
jehoon 0:df571f8f8c03 249 int setMacAddress (const char *mac);
jehoon 0:df571f8f8c03 250 int getMacAddress (char *mac);
jehoon 0:df571f8f8c03 251 int setAddress (const char *name = NULL);
jehoon 0:df571f8f8c03 252 int setAddress (const char *ip, const char *netmask, const char *gateway, const char *dns = NULL, const char *name = NULL);
jehoon 0:df571f8f8c03 253 int getAddress (char *ip, char *netmask, char *gateway);
jehoon 0:df571f8f8c03 254 int setSsid (const char *ssid);
jehoon 0:df571f8f8c03 255 //daniel
jehoon 0:df571f8f8c03 256 // int setSec (Security sec, const char *phrase);
jehoon 0:df571f8f8c03 257 int setSec (nsapi_security_t sec, const char *phrase);
jehoon 0:df571f8f8c03 258
jehoon 0:df571f8f8c03 259 const char* getIPAddress (void);
jehoon 0:df571f8f8c03 260 const char* getMACAddress (void);
jehoon 0:df571f8f8c03 261
jehoon 0:df571f8f8c03 262 // --------- WizFi250_msg.cpp ---------
jehoon 0:df571f8f8c03 263 void recvData ( char c );
jehoon 0:df571f8f8c03 264 int parseMessage ();
jehoon 0:df571f8f8c03 265 void msgOk (const char *buf);
jehoon 0:df571f8f8c03 266 void msgError (const char *buf);
jehoon 0:df571f8f8c03 267 void msgConnect (const char *buf);
jehoon 0:df571f8f8c03 268 void msgDisconnect (const char *buf);
jehoon 0:df571f8f8c03 269 void msgListen (const char *buf);
jehoon 0:df571f8f8c03 270
jehoon 0:df571f8f8c03 271 void resMacAddress (const char *buf);
jehoon 0:df571f8f8c03 272 void resWJOIN (const char *buf);
jehoon 0:df571f8f8c03 273 void resConnect (const char *buf);
jehoon 0:df571f8f8c03 274 void resSSEND (const char *buf);
jehoon 0:df571f8f8c03 275 void resFDNS (const char *buf);
jehoon 0:df571f8f8c03 276 void resSMGMT (const char *buf);
jehoon 0:df571f8f8c03 277 void resWSTATUS (const char *buf);
jehoon 0:df571f8f8c03 278
jehoon 0:df571f8f8c03 279
jehoon 0:df571f8f8c03 280 // --------- WizFi250_hal.cpp ---------
jehoon 0:df571f8f8c03 281 void setReset (bool flg);
jehoon 0:df571f8f8c03 282 void isrUart ();
jehoon 0:df571f8f8c03 283 int getUart ();
jehoon 0:df571f8f8c03 284 void putUart (char c);
jehoon 0:df571f8f8c03 285 void setRts (bool flg);
jehoon 0:df571f8f8c03 286 int lockUart (int ms);
jehoon 0:df571f8f8c03 287 void unlockUart ();
jehoon 0:df571f8f8c03 288 void initUart (PinName cts, PinName rts, PinName alarm, int baud);
jehoon 0:df571f8f8c03 289
jehoon 0:df571f8f8c03 290
jehoon 0:df571f8f8c03 291 // --------- WizFi2550_util.cpp ---------
jehoon 0:df571f8f8c03 292 int x2i (char c);
jehoon 0:df571f8f8c03 293 int i2x (int i);
jehoon 0:df571f8f8c03 294
jehoon 0:df571f8f8c03 295 // --------- WizFi250_ifc.cpp (For NetworkSocketAPI) ---------
jehoon 0:df571f8f8c03 296 /**
jehoon 0:df571f8f8c03 297 * Startup the WizFi310
jehoon 0:df571f8f8c03 298 *
jehoon 0:df571f8f8c03 299 * @return true only if WizFi310 was setup correctly
jehoon 0:df571f8f8c03 300 */
jehoon 0:df571f8f8c03 301 bool startup(void);
jehoon 0:df571f8f8c03 302
jehoon 0:df571f8f8c03 303 /**
jehoon 0:df571f8f8c03 304 * Reset WizFi310
jehoon 0:df571f8f8c03 305 *
jehoon 0:df571f8f8c03 306 * @return true only if WizFi310 resets successfully
jehoon 0:df571f8f8c03 307 */
jehoon 0:df571f8f8c03 308 bool reset(void);
jehoon 0:df571f8f8c03 309
jehoon 0:df571f8f8c03 310 /**
jehoon 0:df571f8f8c03 311 * Disconnect WizFi310 from AP
jehoon 0:df571f8f8c03 312 *
jehoon 0:df571f8f8c03 313 * @return true only if WizFi310 is disconnected successfully
jehoon 0:df571f8f8c03 314 */
jehoon 0:df571f8f8c03 315 bool disconnect(void);
jehoon 0:df571f8f8c03 316
jehoon 0:df571f8f8c03 317 /**
jehoon 0:df571f8f8c03 318 * Check if WizFi310 is conenected
jehoon 0:df571f8f8c03 319 *
jehoon 0:df571f8f8c03 320 * @return true only if the chip has an IP address
jehoon 0:df571f8f8c03 321 */
jehoon 0:df571f8f8c03 322 bool isConnected(void);
jehoon 0:df571f8f8c03 323
jehoon 0:df571f8f8c03 324 };
jehoon 0:df571f8f8c03 325
jehoon 0:df571f8f8c03 326 #endif /* WIZFI250_H_ */