RC

Dependents:   WizFi250_AP_HelloWorld

Fork of WizFi250Interface by DongEun Koak

Committer:
DongEun Koak
Date:
Fri Nov 14 15:27:47 2014 +0900
Revision:
7:ba28fe711055
Parent:
6:e149bf87a1bd
Child:
8:431172d536ee
Fixed bug in init function of WizFi250.
Removed sendCommand for operating polling in order to wait response.
Fixed some bug.

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, Response res = RES_NULL, int timeout = DEFAULT_WAIT_RESP_TIMEOUT, int opt = 2);
kaizen 0:f2039204c8f6 125
kaizen 0:f2039204c8f6 126 int cmdAT ();
kaizen 0:f2039204c8f6 127 int cmdMECHO (bool flg);
kaizen 0:f2039204c8f6 128 int cmdUSET (int baud, char *flow);
kaizen 0:f2039204c8f6 129 int cmdMMAC (const char *mac = NULL);
kaizen 0:f2039204c8f6 130 int cmdWSET (WiFiMode mode, const char *ssid, const char *bssid = NULL, int channel = 1);
kaizen 0:f2039204c8f6 131 int cmdWSEC (WiFiMode mode, const char *key, const char *sec = NULL);
kaizen 0:f2039204c8f6 132 int cmdWJOIN ();
kaizen 0:f2039204c8f6 133 int cmdSCON ( const char *openType, const char *socketType, int localPort, const char *dataMode = "0");
DongEun Koak 4:085e7efeae47 134 int cmdSCON ( const char *openType, const char *socketType, const char *remoteIp, int remotePort, int localPort = 0, const char *dataMode = "0");
DongEun Koak 4:085e7efeae47 135 int cmdSSEND ( const char *data, int cid, int sendSize, const char *remoteIp = NULL, int remotePort = 0, int Timeout = 2000 );
kaizen 0:f2039204c8f6 136 int cmdCLOSE ( int cid );
kaizen 0:f2039204c8f6 137 int cmdFDNS (const char *host);
kaizen 0:f2039204c8f6 138 int cmdSMGMT ( int cid );
kaizen 0:f2039204c8f6 139
kaizen 0:f2039204c8f6 140
kaizen 0:f2039204c8f6 141 static WizFi250 * getInstance() {
kaizen 0:f2039204c8f6 142 return _inst;
kaizen 0:f2039204c8f6 143 };
kaizen 0:f2039204c8f6 144
kaizen 0:f2039204c8f6 145
kaizen 0:f2039204c8f6 146 // --------- WizFi2550_sock.cpp ---------
kaizen 0:f2039204c8f6 147 int getHostByName (const char * host, char *ip);
kaizen 0:f2039204c8f6 148 int open (Protocol proto, const char *ip, int remotePort, int localPort = 0, void(*func)(int) = NULL);
kaizen 0:f2039204c8f6 149 int listen (Protocol proto, int port, void(*func)(int)=NULL);
kaizen 0:f2039204c8f6 150 int close (int cid);
kaizen 0:f2039204c8f6 151 void initCon (int cid, bool connected);
kaizen 0:f2039204c8f6 152 int send (int cid, const char *buf, int len);
kaizen 0:f2039204c8f6 153 int sendto (int cid, const char *buf, int len, const char *ip, int port);
kaizen 0:f2039204c8f6 154 int recv (int cid, char *buf, int len);
kaizen 0:f2039204c8f6 155 int recvfrom (int cid, char *buf, int len, char *ip, int *port);
kaizen 0:f2039204c8f6 156 int readable (int cid);
kaizen 0:f2039204c8f6 157 bool isConnected (int cid);
kaizen 0:f2039204c8f6 158 int accept (int cid);
kaizen 0:f2039204c8f6 159 int getRemote (int cid, char **ip, int *port);
kaizen 0:f2039204c8f6 160
kaizen 0:f2039204c8f6 161
kaizen 0:f2039204c8f6 162
kaizen 0:f2039204c8f6 163 protected:
kaizen 0:f2039204c8f6 164 static WizFi250 * _inst;
kaizen 0:f2039204c8f6 165
kaizen 0:f2039204c8f6 166
kaizen 0:f2039204c8f6 167 // Serial _wizfi
kaizen 0:f2039204c8f6 168 RawSerial _wizfi;
kaizen 0:f2039204c8f6 169 int _baud;
kaizen 0:f2039204c8f6 170 DigitalIn *_cts;
kaizen 0:f2039204c8f6 171 DigitalOut *_rts;
kaizen 0:f2039204c8f6 172 int _flow;
DongEun Koak 6:e149bf87a1bd 173 mbed::Serial *_debug;
kaizen 0:f2039204c8f6 174
kaizen 0:f2039204c8f6 175 DigitalInOut _reset;
kaizen 0:f2039204c8f6 176
kaizen 0:f2039204c8f6 177 struct STATE {
kaizen 0:f2039204c8f6 178 WiFiMode wm;
kaizen 0:f2039204c8f6 179 Security sec;
kaizen 0:f2039204c8f6 180 char ssid[35];
kaizen 0:f2039204c8f6 181 char pass[66];
kaizen 0:f2039204c8f6 182 char ip[16];
kaizen 0:f2039204c8f6 183 char netmask[16];
kaizen 0:f2039204c8f6 184 char gateway[16];
kaizen 0:f2039204c8f6 185 char nameserver[16];
kaizen 0:f2039204c8f6 186 char mac[18];
kaizen 0:f2039204c8f6 187 char resolv[16];
kaizen 0:f2039204c8f6 188 char name[32];
kaizen 0:f2039204c8f6 189 int rssi;
kaizen 0:f2039204c8f6 190 bool dhcp;
kaizen 0:f2039204c8f6 191 time_t time;
kaizen 0:f2039204c8f6 192
kaizen 0:f2039204c8f6 193 bool initialized;
kaizen 0:f2039204c8f6 194 bool associated;
kaizen 0:f2039204c8f6 195 volatile Mode mode;
kaizen 0:f2039204c8f6 196 volatile Status status;
kaizen 0:f2039204c8f6 197 bool escape;
kaizen 0:f2039204c8f6 198 volatile bool ok, failure;
kaizen 0:f2039204c8f6 199 volatile Response res;
kaizen 0:f2039204c8f6 200 int cid;
kaizen 0:f2039204c8f6 201 int n;
kaizen 0:f2039204c8f6 202 CircBuffer<char> *buf;
kaizen 0:f2039204c8f6 203 char dbgRespBuf[128];
kaizen 0:f2039204c8f6 204 char dummyBuf[128];
kaizen 0:f2039204c8f6 205 } _state;
kaizen 0:f2039204c8f6 206
kaizen 0:f2039204c8f6 207
kaizen 0:f2039204c8f6 208 public:
kaizen 0:f2039204c8f6 209 struct CONNECTION {
kaizen 0:f2039204c8f6 210 Protocol protocol;
kaizen 0:f2039204c8f6 211 Type type;
kaizen 0:f2039204c8f6 212 bool connected;
kaizen 0:f2039204c8f6 213 char ip[16];
kaizen 0:f2039204c8f6 214 int port;
kaizen 0:f2039204c8f6 215 int send_length;
kaizen 0:f2039204c8f6 216 int recv_length;
kaizen 1:23e6f886c8f1 217 CircBuffer<char> *buf;
kaizen 0:f2039204c8f6 218 volatile bool received;
kaizen 0:f2039204c8f6 219 volatile int parent;
kaizen 0:f2039204c8f6 220 volatile bool accept;
kaizen 0:f2039204c8f6 221 void(*func)(int);
kaizen 0:f2039204c8f6 222 } _con[8];
kaizen 0:f2039204c8f6 223
kaizen 0:f2039204c8f6 224 // --------- WizFi250.cpp ---------
kaizen 0:f2039204c8f6 225
kaizen 0:f2039204c8f6 226 int join ();
kaizen 0:f2039204c8f6 227 bool isAssociated();
kaizen 0:f2039204c8f6 228
kaizen 0:f2039204c8f6 229 int limitedap ();
kaizen 0:f2039204c8f6 230 int dissociate ();
kaizen 0:f2039204c8f6 231 int disconnect () {
kaizen 0:f2039204c8f6 232 return dissociate();
kaizen 0:f2039204c8f6 233 }
kaizen 0:f2039204c8f6 234
kaizen 0:f2039204c8f6 235 Status getStatus ();
kaizen 0:f2039204c8f6 236
kaizen 0:f2039204c8f6 237 int setMacAddress (const char *mac);
kaizen 0:f2039204c8f6 238 int getMacAddress (char *mac);
kaizen 0:f2039204c8f6 239 int setAddress (const char *name = NULL);
kaizen 0:f2039204c8f6 240 int setAddress (const char *ip, const char *netmask, const char *gateway, const char *dns = NULL, const char *name = NULL);
kaizen 0:f2039204c8f6 241 int getAddress (char *ip, char *netmask, char *gateway);
kaizen 0:f2039204c8f6 242 int setSsid (const char *ssid);
kaizen 0:f2039204c8f6 243 int setSec (Security sec, const char *phrase);
kaizen 0:f2039204c8f6 244
kaizen 0:f2039204c8f6 245
kaizen 0:f2039204c8f6 246 // --------- WizFi250_msg.cpp ---------
kaizen 0:f2039204c8f6 247 void recvData ( char c );
kaizen 0:f2039204c8f6 248 int parseMessage ();
kaizen 0:f2039204c8f6 249 void msgOk (const char *buf);
kaizen 0:f2039204c8f6 250 void msgError (const char *buf);
kaizen 0:f2039204c8f6 251 void msgConnect (const char *buf);
kaizen 0:f2039204c8f6 252 void msgDisconnect (const char *buf);
kaizen 0:f2039204c8f6 253 void msgListen (const char *buf);
kaizen 0:f2039204c8f6 254
kaizen 0:f2039204c8f6 255 void resMacAddress (const char *buf);
kaizen 0:f2039204c8f6 256 void resWJOIN (const char *buf);
kaizen 0:f2039204c8f6 257 void resConnect (const char *buf);
kaizen 0:f2039204c8f6 258 void resSSEND (const char *buf);
kaizen 0:f2039204c8f6 259 void resFDNS (const char *buf);
kaizen 0:f2039204c8f6 260 void resSMGMT (const char *buf);
kaizen 0:f2039204c8f6 261
kaizen 0:f2039204c8f6 262
kaizen 0:f2039204c8f6 263
kaizen 0:f2039204c8f6 264 // --------- WizFi250_hal.cpp ---------
kaizen 0:f2039204c8f6 265 void setReset (bool flg);
kaizen 0:f2039204c8f6 266 void isrUart ();
kaizen 0:f2039204c8f6 267 int getUart ();
kaizen 0:f2039204c8f6 268 void putUart (char c);
kaizen 0:f2039204c8f6 269 void setRts (bool flg);
kaizen 0:f2039204c8f6 270 int lockUart (int ms);
kaizen 0:f2039204c8f6 271 void unlockUart ();
kaizen 0:f2039204c8f6 272 void initUart (PinName cts, PinName rts, PinName alarm, int baud);
kaizen 0:f2039204c8f6 273
kaizen 0:f2039204c8f6 274
kaizen 0:f2039204c8f6 275 // --------- WizFi2550_util.cpp ---------
kaizen 0:f2039204c8f6 276 int x2i (char c);
kaizen 0:f2039204c8f6 277 int i2x (int i);
kaizen 0:f2039204c8f6 278
kaizen 0:f2039204c8f6 279
kaizen 0:f2039204c8f6 280
kaizen 0:f2039204c8f6 281 };
kaizen 0:f2039204c8f6 282
kaizen8501@gmail.com 3:bb87855a8277 283 #endif /* WIZFI250_H_ */