This is library for using WizFi250

Dependents:   WebSocket_WizFi250_HelloWorld IFTTT_WizFi250 AxedaGo-WizFi250 FANARM_AP_udp_server ... more

Committer:
DongEun Koak
Date:
Fri Nov 14 15:22:27 2014 +0900
Revision:
6:e149bf87a1bd
Parent:
5:8a0702aa91e3
Child:
7:ba28fe711055
WizFi250 couldn't use usb serial port in application layer.
So I did add function that you can use serial port in application layer.

Who changed what in which revision?

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