Freedman v2

Dependents:   Freedman_v2

Fork of WizFi250Interface by DongEun Koak

Committer:
DongEun Koak
Date:
Fri Nov 14 15:10:33 2014 +0900
Revision:
4:085e7efeae47
Parent:
3:bb87855a8277
Child:
5:8a0702aa91e3
Modified source code for removing warning message from compiler.

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