ThingPlug Test

Dependents:   WizFi310_ThingPlug_Test WizFi310_ThingPlug_Test_P

Fork of WizFi310Interface by WIZnet

Committer:
jehoon
Date:
Mon Jun 26 00:17:10 2017 +0000
Revision:
5:72212beb817c
Parent:
1:16e57103a7dd
modify message parsing in isr

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 5:72212beb817c 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 1:16e57103a7dd 106
jehoon 0:df571f8f8c03 107 };
jehoon 0:df571f8f8c03 108
jehoon 0:df571f8f8c03 109 enum Mode {
jehoon 0:df571f8f8c03 110 MODE_COMMAND,
jehoon 0:df571f8f8c03 111 MODE_CMDRESP,
jehoon 0:df571f8f8c03 112 MODE_DATA_RX,
jehoon 0:df571f8f8c03 113 MODE_DATA_RXUDP,
jehoon 0:df571f8f8c03 114 MODE_DATA_RXUDP_BULK,
jehoon 0:df571f8f8c03 115 };
jehoon 0:df571f8f8c03 116
jehoon 0:df571f8f8c03 117 enum Status {
jehoon 0:df571f8f8c03 118 STAT_NONE,
jehoon 0:df571f8f8c03 119 STAT_READY,
jehoon 0:df571f8f8c03 120 STAT_STANDBY,
jehoon 0:df571f8f8c03 121 STAT_WAKEUP,
jehoon 0:df571f8f8c03 122 STAT_DEEPSLEEP,
jehoon 0:df571f8f8c03 123 };
jehoon 0:df571f8f8c03 124
jehoon 0:df571f8f8c03 125
jehoon 0:df571f8f8c03 126 WizFi310 (PinName tx, PinName rx, PinName cts, PinName rts, PinName reset, PinName alarm = NC, int baud = 115200);
jehoon 0:df571f8f8c03 127
jehoon 0:df571f8f8c03 128 // --------- WizFi250_at.cpp ---------
jehoon 0:df571f8f8c03 129 void clearFlags ();
jehoon 0:df571f8f8c03 130 int sendCommand (const char * cmd, Response res = RES_NULL, int timeout = DEFAULT_WAIT_RESP_TIMEOUT, int opt = 2);
jehoon 0:df571f8f8c03 131
jehoon 0:df571f8f8c03 132 int cmdAT ();
jehoon 0:df571f8f8c03 133 int cmdMECHO (bool flg);
jehoon 0:df571f8f8c03 134 int cmdUSET (int baud, char *flow);
jehoon 0:df571f8f8c03 135 int cmdMMAC (const char *mac = NULL);
jehoon 0:df571f8f8c03 136 int cmdWSET (WiFiMode mode, const char *ssid, const char *bssid = NULL, int channel = 1);
jehoon 0:df571f8f8c03 137 int cmdWANT (AntennaMode mode);
jehoon 0:df571f8f8c03 138 int cmdWNET (bool is_dhcp);
jehoon 0:df571f8f8c03 139 int cmdWSEC (WiFiMode mode, const char *key, const char *sec = NULL);
jehoon 0:df571f8f8c03 140 int cmdWJOIN ();
jehoon 0:df571f8f8c03 141 int cmdWLEAVE ();
jehoon 0:df571f8f8c03 142 int cmdWSTATUS ();
jehoon 0:df571f8f8c03 143 int cmdSCON ( const char *openType, const char *socketType, int localPort, const char *dataMode = "0");
jehoon 0:df571f8f8c03 144 int cmdSCON ( const char *openType, const char *socketType, const char *remoteIp, int remotePort, int localPort = 0, const char *dataMode = "0");
jehoon 0:df571f8f8c03 145 int cmdSSEND ( const char *data, int cid, int sendSize, const char *remoteIp = NULL, int remotePort = 0, int Timeout = 2000 );
jehoon 0:df571f8f8c03 146 int cmdCLOSE ( int cid );
jehoon 0:df571f8f8c03 147 int cmdFDNS (const char *host);
jehoon 0:df571f8f8c03 148 int cmdSMGMT ( int cid );
jehoon 0:df571f8f8c03 149
jehoon 0:df571f8f8c03 150
jehoon 0:df571f8f8c03 151 static WizFi310 * getInstance() {
jehoon 0:df571f8f8c03 152 return _inst;
jehoon 0:df571f8f8c03 153 };
jehoon 0:df571f8f8c03 154
jehoon 0:df571f8f8c03 155
jehoon 0:df571f8f8c03 156 // --------- WizFi2550_sock.cpp ---------
jehoon 0:df571f8f8c03 157 int getHostByName (const char * host, char *ip);
jehoon 0:df571f8f8c03 158 int open (Protocol proto, const char *ip, int remotePort, int localPort = 0, void(*func)(int) = NULL);
jehoon 0:df571f8f8c03 159 int listen (Protocol proto, int port, void(*func)(int)=NULL);
jehoon 0:df571f8f8c03 160 int close (int cid);
jehoon 0:df571f8f8c03 161 void initCon (int cid, bool connected);
jehoon 0:df571f8f8c03 162 int send (int cid, const char *buf, int len);
jehoon 0:df571f8f8c03 163 int sendto (int cid, const char *buf, int len, const char *ip, int port);
jehoon 0:df571f8f8c03 164 int recv (int cid, char *buf, int len);
jehoon 0:df571f8f8c03 165 int recvfrom (int cid, char *buf, int len, char *ip, int *port);
jehoon 0:df571f8f8c03 166 int readable (int cid);
jehoon 0:df571f8f8c03 167 bool isConnected (int cid);
jehoon 0:df571f8f8c03 168 int accept (int cid);
jehoon 0:df571f8f8c03 169 int getRemote (int cid, char **ip, int *port);
jehoon 0:df571f8f8c03 170
jehoon 0:df571f8f8c03 171
jehoon 0:df571f8f8c03 172 protected:
jehoon 0:df571f8f8c03 173 static WizFi310 * _inst;
jehoon 0:df571f8f8c03 174
jehoon 0:df571f8f8c03 175 // Serial _wizfi
jehoon 0:df571f8f8c03 176 RawSerial _wizfi;
jehoon 5:72212beb817c 177
jehoon 0:df571f8f8c03 178 int _baud;
jehoon 0:df571f8f8c03 179 DigitalIn *_cts;
jehoon 0:df571f8f8c03 180 DigitalOut *_rts;
jehoon 0:df571f8f8c03 181 int _flow;
jehoon 0:df571f8f8c03 182
jehoon 0:df571f8f8c03 183 DigitalInOut _reset;
jehoon 0:df571f8f8c03 184
jehoon 0:df571f8f8c03 185 struct STATE {
jehoon 0:df571f8f8c03 186 WiFiMode wm;
jehoon 0:df571f8f8c03 187
jehoon 0:df571f8f8c03 188 //daniel
jehoon 0:df571f8f8c03 189 //Security sec;
jehoon 0:df571f8f8c03 190 nsapi_security_t sec;
jehoon 0:df571f8f8c03 191 char ssid[35];
jehoon 0:df571f8f8c03 192 char pass[66];
jehoon 0:df571f8f8c03 193 char ip[16];
jehoon 0:df571f8f8c03 194 char netmask[16];
jehoon 0:df571f8f8c03 195 char gateway[16];
jehoon 0:df571f8f8c03 196 char nameserver[16];
jehoon 0:df571f8f8c03 197 char mac[18];
jehoon 0:df571f8f8c03 198 char resolv[16];
jehoon 0:df571f8f8c03 199 char name[32];
jehoon 0:df571f8f8c03 200 int rssi;
jehoon 0:df571f8f8c03 201 bool dhcp;
jehoon 0:df571f8f8c03 202 time_t time;
jehoon 0:df571f8f8c03 203
jehoon 0:df571f8f8c03 204 bool initialized;
jehoon 0:df571f8f8c03 205 bool associated;
jehoon 0:df571f8f8c03 206 volatile Mode mode;
jehoon 0:df571f8f8c03 207 volatile Status status;
jehoon 0:df571f8f8c03 208 bool escape;
jehoon 0:df571f8f8c03 209 volatile bool ok, failure;
jehoon 0:df571f8f8c03 210 volatile Response res;
jehoon 0:df571f8f8c03 211 int cid;
jehoon 0:df571f8f8c03 212 int n;
jehoon 0:df571f8f8c03 213 CircBuffer<char> *buf;
jehoon 0:df571f8f8c03 214 } _state;
jehoon 0:df571f8f8c03 215
jehoon 0:df571f8f8c03 216
jehoon 0:df571f8f8c03 217 public:
jehoon 0:df571f8f8c03 218 struct CONNECTION {
jehoon 0:df571f8f8c03 219 Protocol protocol;
jehoon 0:df571f8f8c03 220 Type type;
jehoon 0:df571f8f8c03 221 bool connected;
jehoon 0:df571f8f8c03 222 char ip[16];
jehoon 0:df571f8f8c03 223 int port;
jehoon 0:df571f8f8c03 224 int send_length;
jehoon 0:df571f8f8c03 225 int recv_length;
jehoon 0:df571f8f8c03 226 CircBuffer<char> *buf;
jehoon 0:df571f8f8c03 227 volatile bool received;
jehoon 0:df571f8f8c03 228 volatile int parent;
jehoon 0:df571f8f8c03 229 volatile bool accept;
jehoon 0:df571f8f8c03 230 void(*func)(int);
jehoon 0:df571f8f8c03 231 } _con[8];
jehoon 0:df571f8f8c03 232
jehoon 0:df571f8f8c03 233 // --------- WizFi310.cpp ---------
jehoon 0:df571f8f8c03 234
jehoon 0:df571f8f8c03 235 int join(WiFiMode mode);
jehoon 0:df571f8f8c03 236 bool isAssociated();
jehoon 0:df571f8f8c03 237
jehoon 0:df571f8f8c03 238 //int limitedap ();
jehoon 0:df571f8f8c03 239 //int dissociate ();
jehoon 0:df571f8f8c03 240 /*
jehoon 0:df571f8f8c03 241 int disconnect () {
jehoon 0:df571f8f8c03 242 return dissociate();
jehoon 0:df571f8f8c03 243 }
jehoon 0:df571f8f8c03 244 */
jehoon 0:df571f8f8c03 245
jehoon 0:df571f8f8c03 246 Status getStatus ();
jehoon 0:df571f8f8c03 247
jehoon 0:df571f8f8c03 248 int setMacAddress (const char *mac);
jehoon 0:df571f8f8c03 249 int getMacAddress (char *mac);
jehoon 0:df571f8f8c03 250 int setAddress (const char *name = NULL);
jehoon 0:df571f8f8c03 251 int setAddress (const char *ip, const char *netmask, const char *gateway, const char *dns = NULL, const char *name = NULL);
jehoon 0:df571f8f8c03 252 int getAddress (char *ip, char *netmask, char *gateway);
jehoon 0:df571f8f8c03 253 int setSsid (const char *ssid);
jehoon 0:df571f8f8c03 254 //daniel
jehoon 0:df571f8f8c03 255 // int setSec (Security sec, const char *phrase);
jehoon 0:df571f8f8c03 256 int setSec (nsapi_security_t sec, const char *phrase);
jehoon 0:df571f8f8c03 257
jehoon 0:df571f8f8c03 258 const char* getIPAddress (void);
jehoon 0:df571f8f8c03 259 const char* getMACAddress (void);
jehoon 0:df571f8f8c03 260
jehoon 0:df571f8f8c03 261 // --------- WizFi250_msg.cpp ---------
jehoon 0:df571f8f8c03 262 void recvData ( char c );
jehoon 0:df571f8f8c03 263 int parseMessage ();
jehoon 0:df571f8f8c03 264 void msgOk (const char *buf);
jehoon 0:df571f8f8c03 265 void msgError (const char *buf);
jehoon 0:df571f8f8c03 266 void msgConnect (const char *buf);
jehoon 0:df571f8f8c03 267 void msgDisconnect (const char *buf);
jehoon 0:df571f8f8c03 268 void msgListen (const char *buf);
jehoon 1:16e57103a7dd 269 //daniel
jehoon 1:16e57103a7dd 270 void msgMQTTConnect (const char *buf);
jehoon 1:16e57103a7dd 271 void msgMQTTDisconnect (const char *buf);
jehoon 0:df571f8f8c03 272
jehoon 0:df571f8f8c03 273 void resMacAddress (const char *buf);
jehoon 0:df571f8f8c03 274 void resWJOIN (const char *buf);
jehoon 0:df571f8f8c03 275 void resConnect (const char *buf);
jehoon 0:df571f8f8c03 276 void resSSEND (const char *buf);
jehoon 0:df571f8f8c03 277 void resFDNS (const char *buf);
jehoon 0:df571f8f8c03 278 void resSMGMT (const char *buf);
jehoon 0:df571f8f8c03 279 void resWSTATUS (const char *buf);
jehoon 0:df571f8f8c03 280
jehoon 0:df571f8f8c03 281
jehoon 0:df571f8f8c03 282 // --------- WizFi250_hal.cpp ---------
jehoon 0:df571f8f8c03 283 void setReset (bool flg);
jehoon 0:df571f8f8c03 284 void isrUart ();
jehoon 0:df571f8f8c03 285 int getUart ();
jehoon 0:df571f8f8c03 286 void putUart (char c);
jehoon 0:df571f8f8c03 287 void setRts (bool flg);
jehoon 0:df571f8f8c03 288 int lockUart (int ms);
jehoon 0:df571f8f8c03 289 void unlockUart ();
jehoon 0:df571f8f8c03 290 void initUart (PinName cts, PinName rts, PinName alarm, int baud);
jehoon 0:df571f8f8c03 291
jehoon 0:df571f8f8c03 292
jehoon 0:df571f8f8c03 293 // --------- WizFi2550_util.cpp ---------
jehoon 5:72212beb817c 294 int x2i (char c);
jehoon 5:72212beb817c 295 int i2x (int i);
jehoon 0:df571f8f8c03 296
jehoon 0:df571f8f8c03 297 // --------- WizFi250_ifc.cpp (For NetworkSocketAPI) ---------
jehoon 0:df571f8f8c03 298 /**
jehoon 0:df571f8f8c03 299 * Startup the WizFi310
jehoon 0:df571f8f8c03 300 *
jehoon 0:df571f8f8c03 301 * @return true only if WizFi310 was setup correctly
jehoon 0:df571f8f8c03 302 */
jehoon 0:df571f8f8c03 303 bool startup(void);
jehoon 0:df571f8f8c03 304
jehoon 0:df571f8f8c03 305 /**
jehoon 0:df571f8f8c03 306 * Reset WizFi310
jehoon 0:df571f8f8c03 307 *
jehoon 0:df571f8f8c03 308 * @return true only if WizFi310 resets successfully
jehoon 0:df571f8f8c03 309 */
jehoon 0:df571f8f8c03 310 bool reset(void);
jehoon 0:df571f8f8c03 311
jehoon 0:df571f8f8c03 312 /**
jehoon 0:df571f8f8c03 313 * Disconnect WizFi310 from AP
jehoon 0:df571f8f8c03 314 *
jehoon 0:df571f8f8c03 315 * @return true only if WizFi310 is disconnected successfully
jehoon 0:df571f8f8c03 316 */
jehoon 0:df571f8f8c03 317 bool disconnect(void);
jehoon 0:df571f8f8c03 318
jehoon 0:df571f8f8c03 319 /**
jehoon 0:df571f8f8c03 320 * Check if WizFi310 is conenected
jehoon 0:df571f8f8c03 321 *
jehoon 0:df571f8f8c03 322 * @return true only if the chip has an IP address
jehoon 0:df571f8f8c03 323 */
jehoon 0:df571f8f8c03 324 bool isConnected(void);
jehoon 1:16e57103a7dd 325
jehoon 1:16e57103a7dd 326
jehoon 1:16e57103a7dd 327 //daniel for mqtt
jehoon 1:16e57103a7dd 328 char rcvd_mqtt_topic[128];
jehoon 0:df571f8f8c03 329
jehoon 0:df571f8f8c03 330 };
jehoon 0:df571f8f8c03 331
jehoon 0:df571f8f8c03 332 #endif /* WIZFI250_H_ */