ThingPlug Test
Dependents: WizFi310_ThingPlug_Test WizFi310_ThingPlug_Test_P
Fork of WizFi310Interface by
WizFi310_at.cpp
00001 /* 00002 * Copyright (C) 2013 gsfan, MIT License 00003 * 00004 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00005 * and associated documentation files (the "Software"), to deal in the Software without restriction, 00006 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 00007 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 00008 * furnished to do so, subject to the following conditions: 00009 * 00010 * The above copyright notice and this permission notice shall be included in all copies or 00011 * substantial portions of the Software. 00012 * 00013 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 00014 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00015 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00016 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00017 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00018 */ 00019 /* Copyright (C) 2014 Wiznet, MIT License 00020 * port to the Wiznet Module WizFi310 00021 */ 00022 00023 00024 #include "WizFi310.h" 00025 00026 00027 00028 void WizFi310::clearFlags() 00029 { 00030 _state.ok = false; 00031 _state.failure = false; 00032 _state.res = RES_NULL; 00033 _state.n = 0; 00034 } 00035 00036 00037 int WizFi310::sendCommand(const char * cmd, Response res, int timeout, int opt) 00038 { 00039 unsigned int i; 00040 Timer t; 00041 00042 if (lockUart(timeout)) return -1; 00043 00044 clearFlags(); 00045 _state.res = res; 00046 00047 WIZ_INFO("%s", cmd) 00048 for (i=0; i< strlen(cmd); i++) 00049 { 00050 putUart(cmd[i]); 00051 } 00052 00053 if(opt == 1) 00054 { 00055 putUart('\r'); 00056 } 00057 else if(opt == 2) 00058 { 00059 putUart('\r'); 00060 putUart('\n'); 00061 } 00062 unlockUart(); 00063 00064 if(timeout) 00065 { 00066 t.start(); 00067 for(;;) 00068 { 00069 if (_state.ok && _state.res == RES_NULL){ 00070 break; 00071 } 00072 00073 if (_state.failure || t.read_ms() > timeout) 00074 { 00075 WIZ_WARN("failure of timeout[%d]ms\r\n",t.read_ms()); 00076 _state.res = RES_NULL; 00077 t.stop(); 00078 return -1; 00079 } 00080 } 00081 00082 t.stop(); 00083 } 00084 00085 WIZ_INFO("[OK]\r\n"); 00086 _state.res = RES_NULL; 00087 00088 return 0; 00089 } 00090 00091 int WizFi310::cmdAT() 00092 { 00093 int resp; 00094 00095 resp = sendCommand("AT"); 00096 00097 return resp; 00098 } 00099 00100 int WizFi310::cmdMECHO(bool flg) 00101 { 00102 int status; 00103 char cmd[CFG_CMD_SIZE]; 00104 00105 sprintf(cmd,"AT+MECHO=%d",flg); 00106 status = sendCommand(cmd); 00107 00108 return status; 00109 } 00110 00111 int WizFi310::cmdUSET(int baud, char *flow) 00112 { 00113 int status; 00114 char cmd[CFG_CMD_SIZE]; 00115 00116 sprintf(cmd,"AT+USET=%d,N,8,1,%s",baud, flow); 00117 status = sendCommand(cmd); 00118 00119 if(status == 0) 00120 { 00121 wait(1); 00122 _state.buf->flush(); 00123 } 00124 00125 return status; 00126 } 00127 00128 int WizFi310::cmdMMAC(const char *mac) 00129 { 00130 int resp; 00131 char cmd[CFG_CMD_SIZE]; 00132 00133 if (mac) 00134 { 00135 sprintf(cmd, "AT+MMAC=%s",mac); 00136 resp = sendCommand(cmd); 00137 } 00138 else 00139 { 00140 sprintf(cmd, "AT+MMAC=?"); 00141 resp = sendCommand(cmd, RES_MACADDRESS); 00142 } 00143 00144 return resp; 00145 } 00146 00147 int WizFi310::cmdWSET(WiFiMode mode, const char *ssid, const char *bssid, int channel) 00148 { 00149 char cmd[CFG_CMD_SIZE]; 00150 00151 if(*bssid == NULL) 00152 { 00153 sprintf(cmd, "AT+WSET=%d,%s",mode, ssid); 00154 } 00155 else 00156 { 00157 sprintf(cmd, "AT+WSET=%d,%s,%s,%d",mode, ssid, bssid, channel); 00158 } 00159 00160 return sendCommand(cmd); 00161 } 00162 00163 int WizFi310::cmdWANT(AntennaMode mode) 00164 { 00165 char cmd[CFG_CMD_SIZE]; 00166 sprintf(cmd, "AT+WANT=%d",mode); 00167 00168 return sendCommand(cmd); 00169 } 00170 00171 int WizFi310::cmdWNET(bool is_dhcp) 00172 { 00173 char cmd[CFG_CMD_SIZE]; 00174 00175 if(is_dhcp == true) 00176 { 00177 sprintf(cmd, "AT+WNET=1"); 00178 } 00179 else 00180 { 00181 sprintf(cmd, "AT+WNET=0,%s,%s,%s",_state.ip,_state.netmask,_state.gateway); 00182 } 00183 00184 return sendCommand(cmd); 00185 } 00186 00187 int WizFi310::cmdWSEC(WiFiMode mode, const char *key, const char *sec) 00188 { 00189 char cmd[CFG_CMD_SIZE]; 00190 00191 if(*sec == NULL) 00192 { 00193 sprintf(cmd, "AT+WSEC=%d,,%s",mode, key); 00194 } 00195 else 00196 { 00197 sprintf(cmd, "AT+WSEC=%d,%s,%s",mode, sec, key); 00198 } 00199 00200 return sendCommand(cmd); 00201 } 00202 00203 int WizFi310::cmdWJOIN() 00204 { 00205 //if( sendCommand("AT+WJOIN", RES_WJOIN, CFG_JOIN_TIMEOUT) ) 00206 if( sendCommand("AT+WJOIN", RES_NULL, CFG_JOIN_TIMEOUT) ) 00207 { 00208 WIZ_ERR("cmdWJOIN"); 00209 return -1; 00210 } 00211 00212 if( cmdWSTATUS() ) 00213 return -1; 00214 00215 WIZ_INFO("WizFi310 is successfully join to AP"); 00216 00217 return 0; 00218 } 00219 00220 int WizFi310::cmdWLEAVE() 00221 { 00222 return sendCommand("AT+WLEAVE"); 00223 } 00224 00225 00226 int WizFi310::cmdWSTATUS() 00227 { 00228 if( sendCommand("AT+WSTATUS", RES_WSTATUS, DEFAULT_WAIT_RESP_TIMEOUT) ) 00229 { 00230 WIZ_ERR("cmdWSTATUS"); 00231 return -1; 00232 } 00233 00234 WIZ_INFO("IP : %s", _state.ip); 00235 WIZ_INFO("Gateway : %s", _state.gateway); 00236 00237 return 0; 00238 } 00239 00240 int WizFi310::cmdSCON ( const char *openType, const char *socketType, int localPort, const char *dataMode) 00241 { 00242 char cmd[CFG_CMD_SIZE]; 00243 00244 sprintf(cmd,"AT+SCON=%s,%s,,,%d,%s",openType, socketType, localPort, dataMode); 00245 return sendCommand(cmd); 00246 } 00247 00248 int WizFi310::cmdSCON ( const char *openType, const char *socketType, const char *remoteIp, int remotePort, int localPort, const char *dataMode) 00249 { 00250 int resp; 00251 char cmd[CFG_CMD_SIZE]; 00252 00253 if(localPort == 0) 00254 sprintf(cmd,"AT+SCON=%s,%s,%s,%d,%s,%s",openType, socketType, remoteIp, remotePort, "", dataMode); 00255 else 00256 sprintf(cmd,"AT+SCON=%s,%s,%s,%d,%d,%s",openType, socketType, remoteIp, remotePort, localPort, dataMode); 00257 00258 resp = sendCommand(cmd, RES_CONNECT, 30000 ); 00259 00260 return resp; 00261 } 00262 00263 int WizFi310::cmdSSEND ( const char *data, int cid, int sendSize, const char *remoteIp, int remotePort, int Timeout ) 00264 { 00265 int i, resp; 00266 Timer t; 00267 char cmd[CFG_CMD_SIZE]; 00268 00269 if (lockUart(Timeout)) return -1; 00270 00271 clearFlags(); 00272 if(remoteIp == NULL) 00273 { 00274 sprintf(cmd,"AT+SSEND=%d,,,%d",cid, sendSize); 00275 } 00276 else 00277 { 00278 sprintf(cmd,"AT+SSEND=%d,%s,%d,%d",cid, remoteIp, remotePort, sendSize); 00279 } 00280 00281 _con[cid].send_length = sendSize; 00282 00283 resp = sendCommand(cmd, RES_SSEND, 2000, 1); 00284 00285 unlockUart(); 00286 if(resp){ 00287 WIZ_DBG("Fail cmdSSEND") 00288 return -1; 00289 } 00290 00291 for(i=0; i<sendSize; i++) 00292 { 00293 putUart(data[i]); 00294 } 00295 unlockUart(); 00296 00297 if(Timeout) 00298 { 00299 t.start(); 00300 for(;;) 00301 { 00302 if (_state.ok) break; 00303 if (_state.failure || t.read_ms() > Timeout) 00304 { 00305 WIZ_WARN("failure or timeout\r\n"); 00306 return -1; 00307 } 00308 } 00309 t.stop(); 00310 } 00311 00312 wait(0.05); 00313 WIZ_INFO("%s\r\n",data); 00314 00315 return i; 00316 } 00317 00318 00319 int WizFi310::cmdCLOSE ( int cid ) 00320 { 00321 char cmd[CFG_CMD_SIZE]; 00322 00323 sprintf(cmd,"AT+SMGMT=%d",cid); 00324 return sendCommand(cmd); 00325 } 00326 00327 00328 int WizFi310::cmdFDNS (const char *host) 00329 { 00330 char cmd[CFG_CMD_SIZE]; 00331 int resp; 00332 00333 sprintf(cmd,"AT+FDNS=%s,3000",host); 00334 resp = sendCommand(cmd, RES_FDNS); 00335 00336 WIZ_DBG("%s",_state.resolv); 00337 return resp; 00338 } 00339 00340 int WizFi310::cmdSMGMT ( int cid ) 00341 { 00342 int resp; 00343 00344 resp = sendCommand("AT+SMGMT=?", RES_SMGMT); 00345 return resp; 00346 } 00347 00348 00349 int WizFi310::cmdSKTPCON ( const char *openType ) 00350 { 00351 char cmd[CFG_CMD_SIZE]; 00352 int resp; 00353 00354 sprintf(cmd,"AT+SKTPCON=%s", openType); 00355 resp = sendCommand(cmd); 00356 00357 return resp; 00358 } 00359 00360 int WizFi310::cmdSKTPCON ( const char *openType, const char *clientId, const char *credentialId, const char *serviceId, const char *devId ) 00361 { 00362 char cmd[CFG_CMD_SIZE]; 00363 int resp; 00364 00365 sprintf(cmd,"AT+SKTPCON=%s,mqtt.thingplug.net,1883,300,%s,%s,%s,v1_0,%s", openType, clientId, credentialId, serviceId, devId); 00366 resp = sendCommand(cmd); 00367 00368 return resp; 00369 } 00370 00371 int WizFi310::cmdSKTPDEVICE (const char *openType, const char *devId) 00372 { 00373 char cmd[CFG_CMD_SIZE]; 00374 int resp; 00375 00376 sprintf(cmd,"AT+SKTPDEVICE=%s,%s", openType, devId); 00377 resp = sendCommand(cmd); 00378 00379 return resp; 00380 } 00381 00382 int WizFi310::cmdSKTPCONTAINER ( const char *openType, const char *containerName ) 00383 { 00384 char cmd[CFG_CMD_SIZE]; 00385 int resp; 00386 00387 sprintf(cmd,"AT+SKTPCONTAINER=%s,%s", openType, containerName); 00388 resp = sendCommand(cmd); 00389 return resp; 00390 } 00391 00392 int WizFi310::cmdSKTPCMD ( const char *openType, const char *commandName ) 00393 { 00394 char cmd[CFG_CMD_SIZE]; 00395 int resp; 00396 00397 sprintf(cmd,"AT+SKTPCMD=%s,%s", openType, commandName); 00398 resp = sendCommand(cmd); 00399 return resp; 00400 } 00401 00402 int WizFi310::cmdSKTPRESULT ( const char *commandName, int executeStatus, int executeResult ) 00403 { 00404 char cmd[CFG_CMD_SIZE]; 00405 int resp; 00406 00407 sprintf(cmd,"AT+SKTPRESULT=%s,%d,%d", commandName, executeStatus, executeResult); 00408 resp = sendCommand(cmd); 00409 00410 return resp; 00411 } 00412 00413 int WizFi310::cmdSKTPSEND ( const char *containerName, const char *sendData ) 00414 { 00415 char cmd[CFG_CMD_SIZE]; 00416 int resp; 00417 00418 sprintf(cmd,"AT+SKTPSEND=%s,%s", containerName, sendData); 00419 resp = sendCommand(cmd); 00420 return resp; 00421 }
Generated on Sat Jul 16 2022 03:26:38 by 1.7.2