RC

Dependents:   WizFi250_AP_HelloWorld

Fork of WizFi250Interface by DongEun Koak

Committer:
DongEun Koak
Date:
Fri Nov 14 16:53:10 2014 +0900
Revision:
8:431172d536ee
Parent:
7:ba28fe711055
Child:
9:12ecb377f63f
Had some refactoring

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 //Debug is disabled by default
DongEun Koak 8:431172d536ee 38 #if 0
DongEun Koak 8:431172d536ee 39 #define WIZ_DBG(x, ...) std::printf("[WizFi250: DBG]" x "\r\n", ##__VA_ARGS__);
DongEun Koak 8:431172d536ee 40 #define WIZ_WARN(x, ...) std::printf("[WizFi250: WARN]" x "\r\n", ##__VA_ARGS__);
DongEun Koak 8:431172d536ee 41 #define WIZ_ERR(x, ...) std::printf("[WizFi250: ERR]" x "\r\n", ##__VA_ARGS__);
DongEun Koak 8:431172d536ee 42 #define WIZ_INFO(x, ...) std::printf("[WizFi250: INFO]" x "\r\n", ##__VA_ARGS__);
kaizen 0:f2039204c8f6 43 #else
DongEun Koak 5:8a0702aa91e3 44 #define WIZ_DBG(x, ...)
DongEun Koak 5:8a0702aa91e3 45 #define WIZ_WARN(x, ...)
DongEun Koak 5:8a0702aa91e3 46 #define WIZ_ERR(x, ...)
DongEun Koak 5:8a0702aa91e3 47 #define WIZ_INFO(x, ...)
kaizen 0:f2039204c8f6 48 #endif
kaizen 0:f2039204c8f6 49
kaizen 0:f2039204c8f6 50
kaizen 0:f2039204c8f6 51 class WizFi250
kaizen 0:f2039204c8f6 52 {
kaizen 0:f2039204c8f6 53 public:
kaizen 0:f2039204c8f6 54
kaizen 0:f2039204c8f6 55 enum WiFiMode {
kaizen 0:f2039204c8f6 56 WM_STATION = 0,
kaizen 0:f2039204c8f6 57 WM_AP = 1,
kaizen 0:f2039204c8f6 58 };
kaizen 0:f2039204c8f6 59
kaizen 0:f2039204c8f6 60 /** Wi-Fi security
kaizen 0:f2039204c8f6 61 */
kaizen 0:f2039204c8f6 62 enum Security {
kaizen 0:f2039204c8f6 63 // kaizen need to change
kaizen 0:f2039204c8f6 64 SEC_AUTO = 0,
kaizen 0:f2039204c8f6 65 SEC_OPEN = 1,
kaizen 0:f2039204c8f6 66 SEC_WEP = 2,
kaizen 0:f2039204c8f6 67 SEC_WPA_TKIP = 3,
kaizen 0:f2039204c8f6 68 SEC_WPA_AES = 4,
kaizen 0:f2039204c8f6 69 SEC_WPA2_AES = 5,
kaizen 0:f2039204c8f6 70 SEC_WPA2_TKIP = 6,
kaizen 0:f2039204c8f6 71 SEC_WPA2_MIXED = 7,
kaizen 0:f2039204c8f6 72 };
kaizen 0:f2039204c8f6 73
kaizen 0:f2039204c8f6 74 /** TCP/IP protocol
kaizen 0:f2039204c8f6 75 */
kaizen 0:f2039204c8f6 76 enum Protocol {
kaizen 0:f2039204c8f6 77 PROTO_UDP = 0,
kaizen 0:f2039204c8f6 78 PROTO_TCP = 1,
kaizen 0:f2039204c8f6 79 };
kaizen 0:f2039204c8f6 80
kaizen 0:f2039204c8f6 81 /** Client/Server
kaizen 0:f2039204c8f6 82 */
kaizen 0:f2039204c8f6 83 enum Type {
kaizen 0:f2039204c8f6 84 TYPE_CLIENT = 0,
kaizen 0:f2039204c8f6 85 TYPE_SERVER = 1,
kaizen 0:f2039204c8f6 86 };
kaizen 0:f2039204c8f6 87
kaizen 0:f2039204c8f6 88 enum Response {
kaizen 0:f2039204c8f6 89 RES_NULL,
kaizen 0:f2039204c8f6 90 RES_MACADDRESS,
kaizen 0:f2039204c8f6 91 RES_WJOIN,
kaizen 0:f2039204c8f6 92 RES_CONNECT,
kaizen 0:f2039204c8f6 93 RES_SSEND,
kaizen 0:f2039204c8f6 94 RES_FDNS,
kaizen 0:f2039204c8f6 95 RES_SMGMT,
kaizen 0:f2039204c8f6 96 };
kaizen 0:f2039204c8f6 97
kaizen 0:f2039204c8f6 98 enum Mode {
kaizen 0:f2039204c8f6 99 MODE_COMMAND,
kaizen 0:f2039204c8f6 100 MODE_CMDRESP,
kaizen 0:f2039204c8f6 101 MODE_DATA_RX,
kaizen 0:f2039204c8f6 102 MODE_DATA_RXUDP,
kaizen 0:f2039204c8f6 103 MODE_DATA_RXUDP_BULK,
kaizen 0:f2039204c8f6 104 };
kaizen 0:f2039204c8f6 105
kaizen 0:f2039204c8f6 106 enum Status {
kaizen 0:f2039204c8f6 107 STAT_NONE,
kaizen 0:f2039204c8f6 108 STAT_READY,
kaizen 0:f2039204c8f6 109 STAT_STANDBY,
kaizen 0:f2039204c8f6 110 STAT_WAKEUP,
kaizen 0:f2039204c8f6 111 STAT_DEEPSLEEP,
kaizen 0:f2039204c8f6 112 };
kaizen 0:f2039204c8f6 113
kaizen 0:f2039204c8f6 114
DongEun Koak 6:e149bf87a1bd 115 WizFi250 (PinName tx, PinName rx, PinName cts, PinName rts, PinName reset, PinName alarm = NC, int baud = 115200, mbed::Serial *debug = NULL);
kaizen 0:f2039204c8f6 116
kaizen 0:f2039204c8f6 117 // --------- WizFi250_at.cpp ---------
kaizen 0:f2039204c8f6 118 void clearFlags ();
kaizen 0:f2039204c8f6 119 int sendCommand (const char * cmd, Response res = RES_NULL, int timeout = DEFAULT_WAIT_RESP_TIMEOUT, int opt = 2);
kaizen 0:f2039204c8f6 120
kaizen 0:f2039204c8f6 121 int cmdAT ();
kaizen 0:f2039204c8f6 122 int cmdMECHO (bool flg);
kaizen 0:f2039204c8f6 123 int cmdUSET (int baud, char *flow);
kaizen 0:f2039204c8f6 124 int cmdMMAC (const char *mac = NULL);
kaizen 0:f2039204c8f6 125 int cmdWSET (WiFiMode mode, const char *ssid, const char *bssid = NULL, int channel = 1);
kaizen 0:f2039204c8f6 126 int cmdWSEC (WiFiMode mode, const char *key, const char *sec = NULL);
kaizen 0:f2039204c8f6 127 int cmdWJOIN ();
kaizen 0:f2039204c8f6 128 int cmdSCON ( const char *openType, const char *socketType, int localPort, const char *dataMode = "0");
DongEun Koak 4:085e7efeae47 129 int cmdSCON ( const char *openType, const char *socketType, const char *remoteIp, int remotePort, int localPort = 0, const char *dataMode = "0");
DongEun Koak 4:085e7efeae47 130 int cmdSSEND ( const char *data, int cid, int sendSize, const char *remoteIp = NULL, int remotePort = 0, int Timeout = 2000 );
kaizen 0:f2039204c8f6 131 int cmdCLOSE ( int cid );
kaizen 0:f2039204c8f6 132 int cmdFDNS (const char *host);
kaizen 0:f2039204c8f6 133 int cmdSMGMT ( int cid );
kaizen 0:f2039204c8f6 134
kaizen 0:f2039204c8f6 135
kaizen 0:f2039204c8f6 136 static WizFi250 * getInstance() {
kaizen 0:f2039204c8f6 137 return _inst;
kaizen 0:f2039204c8f6 138 };
kaizen 0:f2039204c8f6 139
kaizen 0:f2039204c8f6 140
kaizen 0:f2039204c8f6 141 // --------- WizFi2550_sock.cpp ---------
kaizen 0:f2039204c8f6 142 int getHostByName (const char * host, char *ip);
kaizen 0:f2039204c8f6 143 int open (Protocol proto, const char *ip, int remotePort, int localPort = 0, void(*func)(int) = NULL);
kaizen 0:f2039204c8f6 144 int listen (Protocol proto, int port, void(*func)(int)=NULL);
kaizen 0:f2039204c8f6 145 int close (int cid);
kaizen 0:f2039204c8f6 146 void initCon (int cid, bool connected);
kaizen 0:f2039204c8f6 147 int send (int cid, const char *buf, int len);
kaizen 0:f2039204c8f6 148 int sendto (int cid, const char *buf, int len, const char *ip, int port);
kaizen 0:f2039204c8f6 149 int recv (int cid, char *buf, int len);
kaizen 0:f2039204c8f6 150 int recvfrom (int cid, char *buf, int len, char *ip, int *port);
kaizen 0:f2039204c8f6 151 int readable (int cid);
kaizen 0:f2039204c8f6 152 bool isConnected (int cid);
kaizen 0:f2039204c8f6 153 int accept (int cid);
kaizen 0:f2039204c8f6 154 int getRemote (int cid, char **ip, int *port);
kaizen 0:f2039204c8f6 155
kaizen 0:f2039204c8f6 156
kaizen 0:f2039204c8f6 157
kaizen 0:f2039204c8f6 158 protected:
kaizen 0:f2039204c8f6 159 static WizFi250 * _inst;
kaizen 0:f2039204c8f6 160
kaizen 0:f2039204c8f6 161
kaizen 0:f2039204c8f6 162 // Serial _wizfi
kaizen 0:f2039204c8f6 163 RawSerial _wizfi;
kaizen 0:f2039204c8f6 164 int _baud;
kaizen 0:f2039204c8f6 165 DigitalIn *_cts;
kaizen 0:f2039204c8f6 166 DigitalOut *_rts;
kaizen 0:f2039204c8f6 167 int _flow;
DongEun Koak 6:e149bf87a1bd 168 mbed::Serial *_debug;
kaizen 0:f2039204c8f6 169
kaizen 0:f2039204c8f6 170 DigitalInOut _reset;
kaizen 0:f2039204c8f6 171
kaizen 0:f2039204c8f6 172 struct STATE {
kaizen 0:f2039204c8f6 173 WiFiMode wm;
kaizen 0:f2039204c8f6 174 Security sec;
kaizen 0:f2039204c8f6 175 char ssid[35];
kaizen 0:f2039204c8f6 176 char pass[66];
kaizen 0:f2039204c8f6 177 char ip[16];
kaizen 0:f2039204c8f6 178 char netmask[16];
kaizen 0:f2039204c8f6 179 char gateway[16];
kaizen 0:f2039204c8f6 180 char nameserver[16];
kaizen 0:f2039204c8f6 181 char mac[18];
kaizen 0:f2039204c8f6 182 char resolv[16];
kaizen 0:f2039204c8f6 183 char name[32];
kaizen 0:f2039204c8f6 184 int rssi;
kaizen 0:f2039204c8f6 185 bool dhcp;
kaizen 0:f2039204c8f6 186 time_t time;
kaizen 0:f2039204c8f6 187
kaizen 0:f2039204c8f6 188 bool initialized;
kaizen 0:f2039204c8f6 189 bool associated;
kaizen 0:f2039204c8f6 190 volatile Mode mode;
kaizen 0:f2039204c8f6 191 volatile Status status;
kaizen 0:f2039204c8f6 192 bool escape;
kaizen 0:f2039204c8f6 193 volatile bool ok, failure;
kaizen 0:f2039204c8f6 194 volatile Response res;
kaizen 0:f2039204c8f6 195 int cid;
kaizen 0:f2039204c8f6 196 int n;
kaizen 0:f2039204c8f6 197 CircBuffer<char> *buf;
kaizen 0:f2039204c8f6 198 char dbgRespBuf[128];
kaizen 0:f2039204c8f6 199 char dummyBuf[128];
kaizen 0:f2039204c8f6 200 } _state;
kaizen 0:f2039204c8f6 201
kaizen 0:f2039204c8f6 202
kaizen 0:f2039204c8f6 203 public:
kaizen 0:f2039204c8f6 204 struct CONNECTION {
kaizen 0:f2039204c8f6 205 Protocol protocol;
kaizen 0:f2039204c8f6 206 Type type;
kaizen 0:f2039204c8f6 207 bool connected;
kaizen 0:f2039204c8f6 208 char ip[16];
kaizen 0:f2039204c8f6 209 int port;
kaizen 0:f2039204c8f6 210 int send_length;
kaizen 0:f2039204c8f6 211 int recv_length;
kaizen 1:23e6f886c8f1 212 CircBuffer<char> *buf;
kaizen 0:f2039204c8f6 213 volatile bool received;
kaizen 0:f2039204c8f6 214 volatile int parent;
kaizen 0:f2039204c8f6 215 volatile bool accept;
kaizen 0:f2039204c8f6 216 void(*func)(int);
kaizen 0:f2039204c8f6 217 } _con[8];
kaizen 0:f2039204c8f6 218
kaizen 0:f2039204c8f6 219 // --------- WizFi250.cpp ---------
kaizen 0:f2039204c8f6 220
kaizen 0:f2039204c8f6 221 int join ();
kaizen 0:f2039204c8f6 222 bool isAssociated();
kaizen 0:f2039204c8f6 223
kaizen 0:f2039204c8f6 224 int limitedap ();
kaizen 0:f2039204c8f6 225 int dissociate ();
kaizen 0:f2039204c8f6 226 int disconnect () {
kaizen 0:f2039204c8f6 227 return dissociate();
kaizen 0:f2039204c8f6 228 }
kaizen 0:f2039204c8f6 229
kaizen 0:f2039204c8f6 230 Status getStatus ();
kaizen 0:f2039204c8f6 231
kaizen 0:f2039204c8f6 232 int setMacAddress (const char *mac);
kaizen 0:f2039204c8f6 233 int getMacAddress (char *mac);
kaizen 0:f2039204c8f6 234 int setAddress (const char *name = NULL);
kaizen 0:f2039204c8f6 235 int setAddress (const char *ip, const char *netmask, const char *gateway, const char *dns = NULL, const char *name = NULL);
kaizen 0:f2039204c8f6 236 int getAddress (char *ip, char *netmask, char *gateway);
kaizen 0:f2039204c8f6 237 int setSsid (const char *ssid);
kaizen 0:f2039204c8f6 238 int setSec (Security sec, const char *phrase);
kaizen 0:f2039204c8f6 239
kaizen 0:f2039204c8f6 240
kaizen 0:f2039204c8f6 241 // --------- WizFi250_msg.cpp ---------
kaizen 0:f2039204c8f6 242 void recvData ( char c );
kaizen 0:f2039204c8f6 243 int parseMessage ();
kaizen 0:f2039204c8f6 244 void msgOk (const char *buf);
kaizen 0:f2039204c8f6 245 void msgError (const char *buf);
kaizen 0:f2039204c8f6 246 void msgConnect (const char *buf);
kaizen 0:f2039204c8f6 247 void msgDisconnect (const char *buf);
kaizen 0:f2039204c8f6 248 void msgListen (const char *buf);
kaizen 0:f2039204c8f6 249
kaizen 0:f2039204c8f6 250 void resMacAddress (const char *buf);
kaizen 0:f2039204c8f6 251 void resWJOIN (const char *buf);
kaizen 0:f2039204c8f6 252 void resConnect (const char *buf);
kaizen 0:f2039204c8f6 253 void resSSEND (const char *buf);
kaizen 0:f2039204c8f6 254 void resFDNS (const char *buf);
kaizen 0:f2039204c8f6 255 void resSMGMT (const char *buf);
kaizen 0:f2039204c8f6 256
kaizen 0:f2039204c8f6 257
kaizen 0:f2039204c8f6 258
kaizen 0:f2039204c8f6 259 // --------- WizFi250_hal.cpp ---------
kaizen 0:f2039204c8f6 260 void setReset (bool flg);
kaizen 0:f2039204c8f6 261 void isrUart ();
kaizen 0:f2039204c8f6 262 int getUart ();
kaizen 0:f2039204c8f6 263 void putUart (char c);
kaizen 0:f2039204c8f6 264 void setRts (bool flg);
kaizen 0:f2039204c8f6 265 int lockUart (int ms);
kaizen 0:f2039204c8f6 266 void unlockUart ();
kaizen 0:f2039204c8f6 267 void initUart (PinName cts, PinName rts, PinName alarm, int baud);
kaizen 0:f2039204c8f6 268
kaizen 0:f2039204c8f6 269
kaizen 0:f2039204c8f6 270 // --------- WizFi2550_util.cpp ---------
kaizen 0:f2039204c8f6 271 int x2i (char c);
kaizen 0:f2039204c8f6 272 int i2x (int i);
kaizen 0:f2039204c8f6 273
kaizen 0:f2039204c8f6 274
kaizen 0:f2039204c8f6 275
kaizen 0:f2039204c8f6 276 };
kaizen 0:f2039204c8f6 277
kaizen8501@gmail.com 3:bb87855a8277 278 #endif /* WIZFI250_H_ */