ThingPlug Test

Dependents:   WizFi310_ThingPlug_Test WizFi310_ThingPlug_Test_P

Fork of WizFi310Interface by WIZnet

Committer:
jehoon
Date:
Wed Oct 05 09:40:30 2016 +0000
Revision:
0:df571f8f8c03
Child:
1:16e57103a7dd
This is a WizFi310 Library. It supports new mbed5 features including NetworkInterfaceAPI.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jehoon 0:df571f8f8c03 1 /*
jehoon 0:df571f8f8c03 2 * Copyright (C) 2013 gsfan, MIT License
jehoon 0:df571f8f8c03 3 *
jehoon 0:df571f8f8c03 4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
jehoon 0:df571f8f8c03 5 * and associated documentation files (the "Software"), to deal in the Software without restriction,
jehoon 0:df571f8f8c03 6 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
jehoon 0:df571f8f8c03 7 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
jehoon 0:df571f8f8c03 8 * furnished to do so, subject to the following conditions:
jehoon 0:df571f8f8c03 9 *
jehoon 0:df571f8f8c03 10 * The above copyright notice and this permission notice shall be included in all copies or
jehoon 0:df571f8f8c03 11 * substantial portions of the Software.
jehoon 0:df571f8f8c03 12 *
jehoon 0:df571f8f8c03 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
jehoon 0:df571f8f8c03 14 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
jehoon 0:df571f8f8c03 15 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
jehoon 0:df571f8f8c03 16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
jehoon 0:df571f8f8c03 17 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
jehoon 0:df571f8f8c03 18 */
jehoon 0:df571f8f8c03 19 /* Copyright (C) 2014 Wiznet, MIT License
jehoon 0:df571f8f8c03 20 * port to the Wiznet Module WizFi310
jehoon 0:df571f8f8c03 21 */
jehoon 0:df571f8f8c03 22
jehoon 0:df571f8f8c03 23
jehoon 0:df571f8f8c03 24 #include "WizFi310.h"
jehoon 0:df571f8f8c03 25
jehoon 0:df571f8f8c03 26
jehoon 0:df571f8f8c03 27
jehoon 0:df571f8f8c03 28 void WizFi310::clearFlags()
jehoon 0:df571f8f8c03 29 {
jehoon 0:df571f8f8c03 30 _state.ok = false;
jehoon 0:df571f8f8c03 31 _state.failure = false;
jehoon 0:df571f8f8c03 32 _state.res = RES_NULL;
jehoon 0:df571f8f8c03 33 _state.n = 0;
jehoon 0:df571f8f8c03 34 }
jehoon 0:df571f8f8c03 35
jehoon 0:df571f8f8c03 36
jehoon 0:df571f8f8c03 37 int WizFi310::sendCommand(const char * cmd, Response res, int timeout, int opt)
jehoon 0:df571f8f8c03 38 {
jehoon 0:df571f8f8c03 39 unsigned int i;
jehoon 0:df571f8f8c03 40 Timer t;
jehoon 0:df571f8f8c03 41
jehoon 0:df571f8f8c03 42 if (lockUart(timeout)) return -1;
jehoon 0:df571f8f8c03 43
jehoon 0:df571f8f8c03 44 clearFlags();
jehoon 0:df571f8f8c03 45 _state.res = res;
jehoon 0:df571f8f8c03 46
jehoon 0:df571f8f8c03 47 WIZ_INFO("%s", cmd)
jehoon 0:df571f8f8c03 48 for (i=0; i< strlen(cmd); i++)
jehoon 0:df571f8f8c03 49 {
jehoon 0:df571f8f8c03 50 putUart(cmd[i]);
jehoon 0:df571f8f8c03 51 }
jehoon 0:df571f8f8c03 52
jehoon 0:df571f8f8c03 53 if(opt == 1)
jehoon 0:df571f8f8c03 54 {
jehoon 0:df571f8f8c03 55 putUart('\r');
jehoon 0:df571f8f8c03 56 }
jehoon 0:df571f8f8c03 57 else if(opt == 2)
jehoon 0:df571f8f8c03 58 {
jehoon 0:df571f8f8c03 59 putUart('\r');
jehoon 0:df571f8f8c03 60 putUart('\n');
jehoon 0:df571f8f8c03 61 }
jehoon 0:df571f8f8c03 62 unlockUart();
jehoon 0:df571f8f8c03 63
jehoon 0:df571f8f8c03 64 if(timeout)
jehoon 0:df571f8f8c03 65 {
jehoon 0:df571f8f8c03 66 t.start();
jehoon 0:df571f8f8c03 67 for(;;)
jehoon 0:df571f8f8c03 68 {
jehoon 0:df571f8f8c03 69 if (_state.ok && _state.res == RES_NULL){
jehoon 0:df571f8f8c03 70 break;
jehoon 0:df571f8f8c03 71 }
jehoon 0:df571f8f8c03 72 if (_state.failure || t.read_ms() > timeout)
jehoon 0:df571f8f8c03 73 {
jehoon 0:df571f8f8c03 74 WIZ_WARN("failure of timeout[%d]ms\r\n",t.read_ms());
jehoon 0:df571f8f8c03 75 _state.res = RES_NULL;
jehoon 0:df571f8f8c03 76 t.stop();
jehoon 0:df571f8f8c03 77 return -1;
jehoon 0:df571f8f8c03 78 }
jehoon 0:df571f8f8c03 79 }
jehoon 0:df571f8f8c03 80
jehoon 0:df571f8f8c03 81 t.stop();
jehoon 0:df571f8f8c03 82 }
jehoon 0:df571f8f8c03 83
jehoon 0:df571f8f8c03 84 WIZ_INFO("[OK]\r\n");
jehoon 0:df571f8f8c03 85 _state.res = RES_NULL;
jehoon 0:df571f8f8c03 86
jehoon 0:df571f8f8c03 87 return 0;
jehoon 0:df571f8f8c03 88 }
jehoon 0:df571f8f8c03 89
jehoon 0:df571f8f8c03 90 int WizFi310::cmdAT()
jehoon 0:df571f8f8c03 91 {
jehoon 0:df571f8f8c03 92 int resp;
jehoon 0:df571f8f8c03 93
jehoon 0:df571f8f8c03 94 resp = sendCommand("AT");
jehoon 0:df571f8f8c03 95
jehoon 0:df571f8f8c03 96 return resp;
jehoon 0:df571f8f8c03 97 }
jehoon 0:df571f8f8c03 98
jehoon 0:df571f8f8c03 99 int WizFi310::cmdMECHO(bool flg)
jehoon 0:df571f8f8c03 100 {
jehoon 0:df571f8f8c03 101 int status;
jehoon 0:df571f8f8c03 102 char cmd[CFG_CMD_SIZE];
jehoon 0:df571f8f8c03 103
jehoon 0:df571f8f8c03 104 sprintf(cmd,"AT+MECHO=%d",flg);
jehoon 0:df571f8f8c03 105 status = sendCommand(cmd);
jehoon 0:df571f8f8c03 106
jehoon 0:df571f8f8c03 107 return status;
jehoon 0:df571f8f8c03 108 }
jehoon 0:df571f8f8c03 109
jehoon 0:df571f8f8c03 110 int WizFi310::cmdUSET(int baud, char *flow)
jehoon 0:df571f8f8c03 111 {
jehoon 0:df571f8f8c03 112 int status;
jehoon 0:df571f8f8c03 113 char cmd[CFG_CMD_SIZE];
jehoon 0:df571f8f8c03 114
jehoon 0:df571f8f8c03 115 sprintf(cmd,"AT+USET=%d,N,8,1,%s",baud, flow);
jehoon 0:df571f8f8c03 116 status = sendCommand(cmd);
jehoon 0:df571f8f8c03 117
jehoon 0:df571f8f8c03 118 if(status == 0)
jehoon 0:df571f8f8c03 119 {
jehoon 0:df571f8f8c03 120 wait(1);
jehoon 0:df571f8f8c03 121 _state.buf->flush();
jehoon 0:df571f8f8c03 122 }
jehoon 0:df571f8f8c03 123
jehoon 0:df571f8f8c03 124 return status;
jehoon 0:df571f8f8c03 125 }
jehoon 0:df571f8f8c03 126
jehoon 0:df571f8f8c03 127 int WizFi310::cmdMMAC(const char *mac)
jehoon 0:df571f8f8c03 128 {
jehoon 0:df571f8f8c03 129 int resp;
jehoon 0:df571f8f8c03 130 char cmd[CFG_CMD_SIZE];
jehoon 0:df571f8f8c03 131
jehoon 0:df571f8f8c03 132 if (mac)
jehoon 0:df571f8f8c03 133 {
jehoon 0:df571f8f8c03 134 sprintf(cmd, "AT+MMAC=%s",mac);
jehoon 0:df571f8f8c03 135 resp = sendCommand(cmd);
jehoon 0:df571f8f8c03 136 }
jehoon 0:df571f8f8c03 137 else
jehoon 0:df571f8f8c03 138 {
jehoon 0:df571f8f8c03 139 sprintf(cmd, "AT+MMAC=?");
jehoon 0:df571f8f8c03 140 resp = sendCommand(cmd, RES_MACADDRESS);
jehoon 0:df571f8f8c03 141 }
jehoon 0:df571f8f8c03 142
jehoon 0:df571f8f8c03 143 return resp;
jehoon 0:df571f8f8c03 144 }
jehoon 0:df571f8f8c03 145
jehoon 0:df571f8f8c03 146 int WizFi310::cmdWSET(WiFiMode mode, const char *ssid, const char *bssid, int channel)
jehoon 0:df571f8f8c03 147 {
jehoon 0:df571f8f8c03 148 char cmd[CFG_CMD_SIZE];
jehoon 0:df571f8f8c03 149
jehoon 0:df571f8f8c03 150 if(*bssid == NULL)
jehoon 0:df571f8f8c03 151 {
jehoon 0:df571f8f8c03 152 sprintf(cmd, "AT+WSET=%d,%s",mode, ssid);
jehoon 0:df571f8f8c03 153 }
jehoon 0:df571f8f8c03 154 else
jehoon 0:df571f8f8c03 155 {
jehoon 0:df571f8f8c03 156 sprintf(cmd, "AT+WSET=%d,%s,%s,%d",mode, ssid, bssid, channel);
jehoon 0:df571f8f8c03 157 }
jehoon 0:df571f8f8c03 158
jehoon 0:df571f8f8c03 159 return sendCommand(cmd);
jehoon 0:df571f8f8c03 160 }
jehoon 0:df571f8f8c03 161
jehoon 0:df571f8f8c03 162 int WizFi310::cmdWANT(AntennaMode mode)
jehoon 0:df571f8f8c03 163 {
jehoon 0:df571f8f8c03 164 char cmd[CFG_CMD_SIZE];
jehoon 0:df571f8f8c03 165 sprintf(cmd, "AT+WANT=%d",mode);
jehoon 0:df571f8f8c03 166
jehoon 0:df571f8f8c03 167 return sendCommand(cmd);
jehoon 0:df571f8f8c03 168 }
jehoon 0:df571f8f8c03 169
jehoon 0:df571f8f8c03 170 int WizFi310::cmdWNET(bool is_dhcp)
jehoon 0:df571f8f8c03 171 {
jehoon 0:df571f8f8c03 172 char cmd[CFG_CMD_SIZE];
jehoon 0:df571f8f8c03 173
jehoon 0:df571f8f8c03 174 if(is_dhcp == true)
jehoon 0:df571f8f8c03 175 {
jehoon 0:df571f8f8c03 176 sprintf(cmd, "AT+WNET=1");
jehoon 0:df571f8f8c03 177 }
jehoon 0:df571f8f8c03 178 else
jehoon 0:df571f8f8c03 179 {
jehoon 0:df571f8f8c03 180 sprintf(cmd, "AT+WNET=0,%s,%s,%s",_state.ip,_state.netmask,_state.gateway);
jehoon 0:df571f8f8c03 181 }
jehoon 0:df571f8f8c03 182
jehoon 0:df571f8f8c03 183 return sendCommand(cmd);
jehoon 0:df571f8f8c03 184 }
jehoon 0:df571f8f8c03 185
jehoon 0:df571f8f8c03 186 int WizFi310::cmdWSEC(WiFiMode mode, const char *key, const char *sec)
jehoon 0:df571f8f8c03 187 {
jehoon 0:df571f8f8c03 188 char cmd[CFG_CMD_SIZE];
jehoon 0:df571f8f8c03 189
jehoon 0:df571f8f8c03 190 if(*sec == NULL)
jehoon 0:df571f8f8c03 191 {
jehoon 0:df571f8f8c03 192 sprintf(cmd, "AT+WSEC=%d,,%s",mode, key);
jehoon 0:df571f8f8c03 193 }
jehoon 0:df571f8f8c03 194 else
jehoon 0:df571f8f8c03 195 {
jehoon 0:df571f8f8c03 196 sprintf(cmd, "AT+WSEC=%d,%s,%s",mode, sec, key);
jehoon 0:df571f8f8c03 197 }
jehoon 0:df571f8f8c03 198
jehoon 0:df571f8f8c03 199 return sendCommand(cmd);
jehoon 0:df571f8f8c03 200 }
jehoon 0:df571f8f8c03 201
jehoon 0:df571f8f8c03 202 int WizFi310::cmdWJOIN()
jehoon 0:df571f8f8c03 203 {
jehoon 0:df571f8f8c03 204 //if( sendCommand("AT+WJOIN", RES_WJOIN, CFG_JOIN_TIMEOUT) )
jehoon 0:df571f8f8c03 205 if( sendCommand("AT+WJOIN", RES_NULL, CFG_JOIN_TIMEOUT) )
jehoon 0:df571f8f8c03 206 {
jehoon 0:df571f8f8c03 207 WIZ_ERR("cmdWJOIN");
jehoon 0:df571f8f8c03 208 return -1;
jehoon 0:df571f8f8c03 209 }
jehoon 0:df571f8f8c03 210
jehoon 0:df571f8f8c03 211 if( cmdWSTATUS() )
jehoon 0:df571f8f8c03 212 return -1;
jehoon 0:df571f8f8c03 213
jehoon 0:df571f8f8c03 214 WIZ_INFO("WizFi310 is successfully join to AP");
jehoon 0:df571f8f8c03 215
jehoon 0:df571f8f8c03 216 return 0;
jehoon 0:df571f8f8c03 217 }
jehoon 0:df571f8f8c03 218
jehoon 0:df571f8f8c03 219 int WizFi310::cmdWLEAVE()
jehoon 0:df571f8f8c03 220 {
jehoon 0:df571f8f8c03 221 return sendCommand("AT+WLEAVE");
jehoon 0:df571f8f8c03 222 }
jehoon 0:df571f8f8c03 223
jehoon 0:df571f8f8c03 224
jehoon 0:df571f8f8c03 225 int WizFi310::cmdWSTATUS()
jehoon 0:df571f8f8c03 226 {
jehoon 0:df571f8f8c03 227 if( sendCommand("AT+WSTATUS", RES_WSTATUS, DEFAULT_WAIT_RESP_TIMEOUT) )
jehoon 0:df571f8f8c03 228 {
jehoon 0:df571f8f8c03 229 WIZ_ERR("cmdWSTATUS");
jehoon 0:df571f8f8c03 230 return -1;
jehoon 0:df571f8f8c03 231 }
jehoon 0:df571f8f8c03 232
jehoon 0:df571f8f8c03 233 WIZ_INFO("IP : %s", _state.ip);
jehoon 0:df571f8f8c03 234 WIZ_INFO("Gateway : %s", _state.gateway);
jehoon 0:df571f8f8c03 235
jehoon 0:df571f8f8c03 236 return 0;
jehoon 0:df571f8f8c03 237 }
jehoon 0:df571f8f8c03 238
jehoon 0:df571f8f8c03 239 int WizFi310::cmdSCON ( const char *openType, const char *socketType, int localPort, const char *dataMode)
jehoon 0:df571f8f8c03 240 {
jehoon 0:df571f8f8c03 241 char cmd[CFG_CMD_SIZE];
jehoon 0:df571f8f8c03 242
jehoon 0:df571f8f8c03 243 sprintf(cmd,"AT+SCON=%s,%s,,,%d,%s",openType, socketType, localPort, dataMode);
jehoon 0:df571f8f8c03 244 return sendCommand(cmd);
jehoon 0:df571f8f8c03 245 }
jehoon 0:df571f8f8c03 246
jehoon 0:df571f8f8c03 247 int WizFi310::cmdSCON ( const char *openType, const char *socketType, const char *remoteIp, int remotePort, int localPort, const char *dataMode)
jehoon 0:df571f8f8c03 248 {
jehoon 0:df571f8f8c03 249 int resp;
jehoon 0:df571f8f8c03 250 char cmd[CFG_CMD_SIZE];
jehoon 0:df571f8f8c03 251
jehoon 0:df571f8f8c03 252 if(localPort == 0)
jehoon 0:df571f8f8c03 253 sprintf(cmd,"AT+SCON=%s,%s,%s,%d,%s,%s",openType, socketType, remoteIp, remotePort, "", dataMode);
jehoon 0:df571f8f8c03 254 else
jehoon 0:df571f8f8c03 255 sprintf(cmd,"AT+SCON=%s,%s,%s,%d,%d,%s",openType, socketType, remoteIp, remotePort, localPort, dataMode);
jehoon 0:df571f8f8c03 256
jehoon 0:df571f8f8c03 257 resp = sendCommand(cmd, RES_CONNECT, 30000 );
jehoon 0:df571f8f8c03 258
jehoon 0:df571f8f8c03 259 return resp;
jehoon 0:df571f8f8c03 260 }
jehoon 0:df571f8f8c03 261
jehoon 0:df571f8f8c03 262 int WizFi310::cmdSSEND ( const char *data, int cid, int sendSize, const char *remoteIp, int remotePort, int Timeout )
jehoon 0:df571f8f8c03 263 {
jehoon 0:df571f8f8c03 264 int i, resp;
jehoon 0:df571f8f8c03 265 Timer t;
jehoon 0:df571f8f8c03 266 char cmd[CFG_CMD_SIZE];
jehoon 0:df571f8f8c03 267
jehoon 0:df571f8f8c03 268 if (lockUart(Timeout)) return -1;
jehoon 0:df571f8f8c03 269
jehoon 0:df571f8f8c03 270 clearFlags();
jehoon 0:df571f8f8c03 271 if(remoteIp == NULL)
jehoon 0:df571f8f8c03 272 {
jehoon 0:df571f8f8c03 273 sprintf(cmd,"AT+SSEND=%d,,,%d",cid, sendSize);
jehoon 0:df571f8f8c03 274 }
jehoon 0:df571f8f8c03 275 else
jehoon 0:df571f8f8c03 276 {
jehoon 0:df571f8f8c03 277 sprintf(cmd,"AT+SSEND=%d,%s,%d,%d",cid, remoteIp, remotePort, sendSize);
jehoon 0:df571f8f8c03 278 }
jehoon 0:df571f8f8c03 279
jehoon 0:df571f8f8c03 280 _con[cid].send_length = sendSize;
jehoon 0:df571f8f8c03 281
jehoon 0:df571f8f8c03 282 resp = sendCommand(cmd, RES_SSEND, 2000, 1);
jehoon 0:df571f8f8c03 283
jehoon 0:df571f8f8c03 284 unlockUart();
jehoon 0:df571f8f8c03 285 if(resp){
jehoon 0:df571f8f8c03 286 WIZ_DBG("Fail cmdSSEND")
jehoon 0:df571f8f8c03 287 return -1;
jehoon 0:df571f8f8c03 288 }
jehoon 0:df571f8f8c03 289
jehoon 0:df571f8f8c03 290 for(i=0; i<sendSize; i++)
jehoon 0:df571f8f8c03 291 {
jehoon 0:df571f8f8c03 292 putUart(data[i]);
jehoon 0:df571f8f8c03 293 }
jehoon 0:df571f8f8c03 294 unlockUart();
jehoon 0:df571f8f8c03 295
jehoon 0:df571f8f8c03 296 if(Timeout)
jehoon 0:df571f8f8c03 297 {
jehoon 0:df571f8f8c03 298 t.start();
jehoon 0:df571f8f8c03 299 for(;;)
jehoon 0:df571f8f8c03 300 {
jehoon 0:df571f8f8c03 301 if (_state.ok) break;
jehoon 0:df571f8f8c03 302 if (_state.failure || t.read_ms() > Timeout)
jehoon 0:df571f8f8c03 303 {
jehoon 0:df571f8f8c03 304 WIZ_WARN("failure or timeout\r\n");
jehoon 0:df571f8f8c03 305 return -1;
jehoon 0:df571f8f8c03 306 }
jehoon 0:df571f8f8c03 307 }
jehoon 0:df571f8f8c03 308 t.stop();
jehoon 0:df571f8f8c03 309 }
jehoon 0:df571f8f8c03 310
jehoon 0:df571f8f8c03 311 wait(0.05);
jehoon 0:df571f8f8c03 312 WIZ_INFO("%s\r\n",data);
jehoon 0:df571f8f8c03 313
jehoon 0:df571f8f8c03 314 return i;
jehoon 0:df571f8f8c03 315 }
jehoon 0:df571f8f8c03 316
jehoon 0:df571f8f8c03 317
jehoon 0:df571f8f8c03 318 int WizFi310::cmdCLOSE ( int cid )
jehoon 0:df571f8f8c03 319 {
jehoon 0:df571f8f8c03 320 char cmd[CFG_CMD_SIZE];
jehoon 0:df571f8f8c03 321
jehoon 0:df571f8f8c03 322 sprintf(cmd,"AT+SMGMT=%d",cid);
jehoon 0:df571f8f8c03 323 return sendCommand(cmd);
jehoon 0:df571f8f8c03 324 }
jehoon 0:df571f8f8c03 325
jehoon 0:df571f8f8c03 326
jehoon 0:df571f8f8c03 327 int WizFi310::cmdFDNS (const char *host)
jehoon 0:df571f8f8c03 328 {
jehoon 0:df571f8f8c03 329 char cmd[CFG_CMD_SIZE];
jehoon 0:df571f8f8c03 330 int resp;
jehoon 0:df571f8f8c03 331
jehoon 0:df571f8f8c03 332 sprintf(cmd,"AT+FDNS=%s,3000",host);
jehoon 0:df571f8f8c03 333 resp = sendCommand(cmd, RES_FDNS);
jehoon 0:df571f8f8c03 334
jehoon 0:df571f8f8c03 335 WIZ_DBG("%s",_state.resolv);
jehoon 0:df571f8f8c03 336 return resp;
jehoon 0:df571f8f8c03 337 }
jehoon 0:df571f8f8c03 338
jehoon 0:df571f8f8c03 339 int WizFi310::cmdSMGMT ( int cid )
jehoon 0:df571f8f8c03 340 {
jehoon 0:df571f8f8c03 341 int resp;
jehoon 0:df571f8f8c03 342
jehoon 0:df571f8f8c03 343 resp = sendCommand("AT+SMGMT=?", RES_SMGMT);
jehoon 0:df571f8f8c03 344 return resp;
jehoon 0:df571f8f8c03 345 }