RC
Dependents: WizFi250_AP_HelloWorld
Fork of WizFi250Interface by
WizFi250/WizFi250.h@19:3a2282277ad0, 2015-07-30 (annotated)
- Committer:
- kaizen
- Date:
- Thu Jul 30 11:13:12 2015 +0000
- Revision:
- 19:3a2282277ad0
- Parent:
- 18:2a20f54f2929
fixed bug about udp server
Who changed what in which revision?
User | Revision | Line number | New 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 | |
kaizen8501 |
9:12ecb377f63f | 37 | #define BUF_SIZE 1600 |
kaizen8501 |
9:12ecb377f63f | 38 | |
kaizen | 0:f2039204c8f6 | 39 | //Debug is disabled by default |
kaizen8501 |
9:12ecb377f63f | 40 | #if 1 |
DongEun Koak | 8:431172d536ee | 41 | #define WIZ_DBG(x, ...) std::printf("[WizFi250: DBG]" x "\r\n", ##__VA_ARGS__); |
DongEun Koak | 8:431172d536ee | 42 | #define WIZ_WARN(x, ...) std::printf("[WizFi250: WARN]" x "\r\n", ##__VA_ARGS__); |
DongEun Koak | 8:431172d536ee | 43 | #define WIZ_ERR(x, ...) std::printf("[WizFi250: ERR]" x "\r\n", ##__VA_ARGS__); |
DongEun Koak | 8:431172d536ee | 44 | #define WIZ_INFO(x, ...) std::printf("[WizFi250: INFO]" x "\r\n", ##__VA_ARGS__); |
kaizen | 0:f2039204c8f6 | 45 | #else |
DongEun Koak | 5:8a0702aa91e3 | 46 | #define WIZ_DBG(x, ...) |
DongEun Koak | 5:8a0702aa91e3 | 47 | #define WIZ_WARN(x, ...) |
DongEun Koak | 5:8a0702aa91e3 | 48 | #define WIZ_ERR(x, ...) |
DongEun Koak | 5:8a0702aa91e3 | 49 | #define WIZ_INFO(x, ...) |
kaizen | 0:f2039204c8f6 | 50 | #endif |
kaizen | 0:f2039204c8f6 | 51 | |
kaizen | 0:f2039204c8f6 | 52 | |
kaizen | 0:f2039204c8f6 | 53 | class WizFi250 |
kaizen | 0:f2039204c8f6 | 54 | { |
kaizen | 0:f2039204c8f6 | 55 | public: |
kaizen | 0:f2039204c8f6 | 56 | |
kaizen | 0:f2039204c8f6 | 57 | enum WiFiMode { |
kaizen | 0:f2039204c8f6 | 58 | WM_STATION = 0, |
kaizen | 0:f2039204c8f6 | 59 | WM_AP = 1, |
kaizen | 0:f2039204c8f6 | 60 | }; |
kaizen | 0:f2039204c8f6 | 61 | |
kaizen | 0:f2039204c8f6 | 62 | /** Wi-Fi security |
kaizen | 0:f2039204c8f6 | 63 | */ |
kaizen | 0:f2039204c8f6 | 64 | enum Security { |
kaizen | 0:f2039204c8f6 | 65 | // kaizen need to change |
kaizen | 0:f2039204c8f6 | 66 | SEC_AUTO = 0, |
kaizen | 0:f2039204c8f6 | 67 | SEC_OPEN = 1, |
kaizen | 0:f2039204c8f6 | 68 | SEC_WEP = 2, |
kaizen | 0:f2039204c8f6 | 69 | SEC_WPA_TKIP = 3, |
kaizen | 0:f2039204c8f6 | 70 | SEC_WPA_AES = 4, |
kaizen | 0:f2039204c8f6 | 71 | SEC_WPA2_AES = 5, |
kaizen | 0:f2039204c8f6 | 72 | SEC_WPA2_TKIP = 6, |
kaizen | 0:f2039204c8f6 | 73 | SEC_WPA2_MIXED = 7, |
kaizen | 0:f2039204c8f6 | 74 | }; |
kaizen | 0:f2039204c8f6 | 75 | |
kaizen | 0:f2039204c8f6 | 76 | /** TCP/IP protocol |
kaizen | 0:f2039204c8f6 | 77 | */ |
kaizen | 0:f2039204c8f6 | 78 | enum Protocol { |
kaizen | 0:f2039204c8f6 | 79 | PROTO_UDP = 0, |
kaizen | 0:f2039204c8f6 | 80 | PROTO_TCP = 1, |
kaizen | 0:f2039204c8f6 | 81 | }; |
kaizen | 0:f2039204c8f6 | 82 | |
kaizen | 0:f2039204c8f6 | 83 | /** Client/Server |
kaizen | 0:f2039204c8f6 | 84 | */ |
kaizen | 0:f2039204c8f6 | 85 | enum Type { |
kaizen | 0:f2039204c8f6 | 86 | TYPE_CLIENT = 0, |
kaizen | 0:f2039204c8f6 | 87 | TYPE_SERVER = 1, |
kaizen | 0:f2039204c8f6 | 88 | }; |
kaizen | 0:f2039204c8f6 | 89 | |
kaizen | 0:f2039204c8f6 | 90 | enum Response { |
kaizen | 0:f2039204c8f6 | 91 | RES_NULL, |
kaizen | 0:f2039204c8f6 | 92 | RES_MACADDRESS, |
kaizen | 0:f2039204c8f6 | 93 | RES_WJOIN, |
kaizen | 0:f2039204c8f6 | 94 | RES_CONNECT, |
kaizen | 0:f2039204c8f6 | 95 | RES_SSEND, |
kaizen | 0:f2039204c8f6 | 96 | RES_FDNS, |
kaizen | 0:f2039204c8f6 | 97 | RES_SMGMT, |
kaizen8501 |
9:12ecb377f63f | 98 | RES_WSTATUS, |
kaizen | 0:f2039204c8f6 | 99 | }; |
kaizen | 0:f2039204c8f6 | 100 | |
kaizen | 0:f2039204c8f6 | 101 | enum Mode { |
kaizen | 0:f2039204c8f6 | 102 | MODE_COMMAND, |
kaizen | 0:f2039204c8f6 | 103 | MODE_CMDRESP, |
kaizen | 0:f2039204c8f6 | 104 | MODE_DATA_RX, |
kaizen | 0:f2039204c8f6 | 105 | MODE_DATA_RXUDP, |
kaizen | 0:f2039204c8f6 | 106 | MODE_DATA_RXUDP_BULK, |
kaizen | 0:f2039204c8f6 | 107 | }; |
kaizen | 0:f2039204c8f6 | 108 | |
kaizen | 0:f2039204c8f6 | 109 | enum Status { |
kaizen | 0:f2039204c8f6 | 110 | STAT_NONE, |
kaizen | 0:f2039204c8f6 | 111 | STAT_READY, |
kaizen | 0:f2039204c8f6 | 112 | STAT_STANDBY, |
kaizen | 0:f2039204c8f6 | 113 | STAT_WAKEUP, |
kaizen | 0:f2039204c8f6 | 114 | STAT_DEEPSLEEP, |
kaizen | 0:f2039204c8f6 | 115 | }; |
kaizen | 0:f2039204c8f6 | 116 | |
kaizen | 0:f2039204c8f6 | 117 | |
kaizen | 13:0b8e51d3c0e8 | 118 | WizFi250 (PinName tx, PinName rx, PinName cts, PinName rts, PinName reset, PinName alarm = NC, int baud = 115200); |
kaizen | 0:f2039204c8f6 | 119 | |
kaizen | 0:f2039204c8f6 | 120 | // --------- WizFi250_at.cpp --------- |
kaizen | 0:f2039204c8f6 | 121 | void clearFlags (); |
kaizen | 0:f2039204c8f6 | 122 | int sendCommand (const char * cmd, Response res = RES_NULL, int timeout = DEFAULT_WAIT_RESP_TIMEOUT, int opt = 2); |
kaizen | 0:f2039204c8f6 | 123 | |
kaizen | 0:f2039204c8f6 | 124 | int cmdAT (); |
kaizen | 0:f2039204c8f6 | 125 | int cmdMECHO (bool flg); |
kaizen | 0:f2039204c8f6 | 126 | int cmdUSET (int baud, char *flow); |
kaizen | 0:f2039204c8f6 | 127 | int cmdMMAC (const char *mac = NULL); |
kaizen | 0:f2039204c8f6 | 128 | int cmdWSET (WiFiMode mode, const char *ssid, const char *bssid = NULL, int channel = 1); |
kaizen | 18:2a20f54f2929 | 129 | int cmdWNET (bool is_dhcp); |
kaizen | 0:f2039204c8f6 | 130 | int cmdWSEC (WiFiMode mode, const char *key, const char *sec = NULL); |
kaizen | 0:f2039204c8f6 | 131 | int cmdWJOIN (); |
kaizen8501 |
9:12ecb377f63f | 132 | int cmdWSTATUS (); |
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; |
kaizen | 0:f2039204c8f6 | 173 | |
kaizen | 0:f2039204c8f6 | 174 | DigitalInOut _reset; |
kaizen | 0:f2039204c8f6 | 175 | |
kaizen | 0:f2039204c8f6 | 176 | struct STATE { |
kaizen | 0:f2039204c8f6 | 177 | WiFiMode wm; |
kaizen | 0:f2039204c8f6 | 178 | Security sec; |
kaizen | 0:f2039204c8f6 | 179 | char ssid[35]; |
kaizen | 0:f2039204c8f6 | 180 | char pass[66]; |
kaizen | 0:f2039204c8f6 | 181 | char ip[16]; |
kaizen | 0:f2039204c8f6 | 182 | char netmask[16]; |
kaizen | 0:f2039204c8f6 | 183 | char gateway[16]; |
kaizen | 0:f2039204c8f6 | 184 | char nameserver[16]; |
kaizen | 0:f2039204c8f6 | 185 | char mac[18]; |
kaizen | 0:f2039204c8f6 | 186 | char resolv[16]; |
kaizen | 0:f2039204c8f6 | 187 | char name[32]; |
kaizen | 0:f2039204c8f6 | 188 | int rssi; |
kaizen | 0:f2039204c8f6 | 189 | bool dhcp; |
kaizen | 0:f2039204c8f6 | 190 | time_t time; |
kaizen | 0:f2039204c8f6 | 191 | |
kaizen | 0:f2039204c8f6 | 192 | bool initialized; |
kaizen | 0:f2039204c8f6 | 193 | bool associated; |
kaizen | 0:f2039204c8f6 | 194 | volatile Mode mode; |
kaizen | 0:f2039204c8f6 | 195 | volatile Status status; |
kaizen | 0:f2039204c8f6 | 196 | bool escape; |
kaizen | 0:f2039204c8f6 | 197 | volatile bool ok, failure; |
kaizen | 0:f2039204c8f6 | 198 | volatile Response res; |
kaizen | 0:f2039204c8f6 | 199 | int cid; |
kaizen | 0:f2039204c8f6 | 200 | int n; |
kaizen | 0:f2039204c8f6 | 201 | CircBuffer<char> *buf; |
kaizen8501 |
9:12ecb377f63f | 202 | char dbgRespBuf[BUF_SIZE]; |
kaizen8501 |
9:12ecb377f63f | 203 | //char dummyBuf[BUF_SIZE]; |
kaizen | 0:f2039204c8f6 | 204 | } _state; |
kaizen | 0:f2039204c8f6 | 205 | |
kaizen | 0:f2039204c8f6 | 206 | |
kaizen | 0:f2039204c8f6 | 207 | public: |
kaizen | 0:f2039204c8f6 | 208 | struct CONNECTION { |
kaizen | 0:f2039204c8f6 | 209 | Protocol protocol; |
kaizen | 0:f2039204c8f6 | 210 | Type type; |
kaizen | 0:f2039204c8f6 | 211 | bool connected; |
kaizen | 0:f2039204c8f6 | 212 | char ip[16]; |
kaizen | 0:f2039204c8f6 | 213 | int port; |
kaizen | 0:f2039204c8f6 | 214 | int send_length; |
kaizen | 0:f2039204c8f6 | 215 | int recv_length; |
kaizen | 10:187e3fd24123 | 216 | CircBuffer<char> *buf; |
kaizen | 0:f2039204c8f6 | 217 | volatile bool received; |
kaizen | 0:f2039204c8f6 | 218 | volatile int parent; |
kaizen | 0:f2039204c8f6 | 219 | volatile bool accept; |
kaizen | 0:f2039204c8f6 | 220 | void(*func)(int); |
kaizen | 0:f2039204c8f6 | 221 | } _con[8]; |
kaizen | 0:f2039204c8f6 | 222 | |
kaizen | 0:f2039204c8f6 | 223 | // --------- WizFi250.cpp --------- |
kaizen | 0:f2039204c8f6 | 224 | |
kaizen | 18:2a20f54f2929 | 225 | int join(WiFiMode mode); |
kaizen | 0:f2039204c8f6 | 226 | bool isAssociated(); |
kaizen | 0:f2039204c8f6 | 227 | |
kaizen | 0:f2039204c8f6 | 228 | int limitedap (); |
kaizen | 0:f2039204c8f6 | 229 | int dissociate (); |
kaizen | 0:f2039204c8f6 | 230 | int disconnect () { |
kaizen | 0:f2039204c8f6 | 231 | return dissociate(); |
kaizen | 0:f2039204c8f6 | 232 | } |
kaizen | 0:f2039204c8f6 | 233 | |
kaizen | 0:f2039204c8f6 | 234 | Status getStatus (); |
kaizen | 0:f2039204c8f6 | 235 | |
kaizen | 0:f2039204c8f6 | 236 | int setMacAddress (const char *mac); |
kaizen | 0:f2039204c8f6 | 237 | int getMacAddress (char *mac); |
kaizen | 0:f2039204c8f6 | 238 | int setAddress (const char *name = NULL); |
kaizen | 0:f2039204c8f6 | 239 | int setAddress (const char *ip, const char *netmask, const char *gateway, const char *dns = NULL, const char *name = NULL); |
kaizen | 0:f2039204c8f6 | 240 | int getAddress (char *ip, char *netmask, char *gateway); |
kaizen | 0:f2039204c8f6 | 241 | int setSsid (const char *ssid); |
kaizen | 0:f2039204c8f6 | 242 | int setSec (Security sec, const char *phrase); |
kaizen | 0:f2039204c8f6 | 243 | |
kaizen | 0:f2039204c8f6 | 244 | |
kaizen | 0:f2039204c8f6 | 245 | // --------- WizFi250_msg.cpp --------- |
kaizen | 0:f2039204c8f6 | 246 | void recvData ( char c ); |
kaizen | 0:f2039204c8f6 | 247 | int parseMessage (); |
kaizen | 0:f2039204c8f6 | 248 | void msgOk (const char *buf); |
kaizen | 0:f2039204c8f6 | 249 | void msgError (const char *buf); |
kaizen | 0:f2039204c8f6 | 250 | void msgConnect (const char *buf); |
kaizen | 0:f2039204c8f6 | 251 | void msgDisconnect (const char *buf); |
kaizen | 0:f2039204c8f6 | 252 | void msgListen (const char *buf); |
kaizen | 0:f2039204c8f6 | 253 | |
kaizen | 0:f2039204c8f6 | 254 | void resMacAddress (const char *buf); |
kaizen | 0:f2039204c8f6 | 255 | void resWJOIN (const char *buf); |
kaizen | 0:f2039204c8f6 | 256 | void resConnect (const char *buf); |
kaizen | 0:f2039204c8f6 | 257 | void resSSEND (const char *buf); |
kaizen | 0:f2039204c8f6 | 258 | void resFDNS (const char *buf); |
kaizen | 0:f2039204c8f6 | 259 | void resSMGMT (const char *buf); |
kaizen8501 |
9:12ecb377f63f | 260 | void resWSTATUS (const char *buf); |
kaizen | 0:f2039204c8f6 | 261 | |
kaizen | 0:f2039204c8f6 | 262 | |
kaizen | 0:f2039204c8f6 | 263 | // --------- WizFi250_hal.cpp --------- |
kaizen | 0:f2039204c8f6 | 264 | void setReset (bool flg); |
kaizen | 0:f2039204c8f6 | 265 | void isrUart (); |
kaizen | 0:f2039204c8f6 | 266 | int getUart (); |
kaizen | 0:f2039204c8f6 | 267 | void putUart (char c); |
kaizen | 0:f2039204c8f6 | 268 | void setRts (bool flg); |
kaizen | 0:f2039204c8f6 | 269 | int lockUart (int ms); |
kaizen | 0:f2039204c8f6 | 270 | void unlockUart (); |
kaizen | 0:f2039204c8f6 | 271 | void initUart (PinName cts, PinName rts, PinName alarm, int baud); |
kaizen | 0:f2039204c8f6 | 272 | |
kaizen | 0:f2039204c8f6 | 273 | |
kaizen | 0:f2039204c8f6 | 274 | // --------- WizFi2550_util.cpp --------- |
kaizen | 0:f2039204c8f6 | 275 | int x2i (char c); |
kaizen | 0:f2039204c8f6 | 276 | int i2x (int i); |
kaizen | 0:f2039204c8f6 | 277 | |
kaizen | 0:f2039204c8f6 | 278 | |
kaizen | 0:f2039204c8f6 | 279 | |
kaizen | 0:f2039204c8f6 | 280 | }; |
kaizen | 0:f2039204c8f6 | 281 | |
kaizen8501@gmail.com | 3:bb87855a8277 | 282 | #endif /* WIZFI250_H_ */ |