Akshay Tom / WizFi310Interface_Legacynew

Dependencies:   WizFi310Interface_Legacynew

Dependents:   w7500-mqtt-wizfi310 w7500-mqtt-wizfi310 w7500-mqtt-wizfi310

Fork of WizFi310Interface_Legacynew by ajeet prajapati

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers WizFi310_at.cpp Source File

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 WizFi250
00021  */
00022 /* Copyright (C) 2017 Wiznet, MIT License
00023  *  port to the Wiznet Module WizFi310
00024  */
00025 
00026 #include "WizFi310.h"
00027 
00028 
00029 
00030 void WizFi310::clearFlags()
00031 {
00032     _state.ok = false;
00033     _state.failure = false;
00034     _state.res = RES_NULL;
00035     _state.n = 0;
00036 }
00037 
00038 
00039 int WizFi310::sendCommand(const char * cmd, Response res, int timeout, int opt)
00040 {
00041     unsigned int i;
00042     Timer t;
00043 
00044     if (lockUart(timeout)) return -1;
00045 
00046     clearFlags();
00047     _state.res = res;
00048 
00049     WIZ_INFO("%s", cmd)
00050 
00051     for (i=0; i< strlen(cmd); i++)
00052     {
00053         putUart(cmd[i]);
00054     }
00055 
00056     if(opt == 1)
00057     {
00058         putUart('\r');
00059     }
00060     else if(opt == 2)
00061     {
00062         putUart('\r');
00063         putUart('\n');
00064     }
00065     unlockUart();
00066 
00067     if(timeout)
00068     {
00069         t.start();
00070         for(;;)
00071         {
00072             if (_state.ok && _state.res == RES_NULL){
00073                 break;
00074             }
00075             
00076             if (_state.failure || t.read_ms() > timeout)
00077             {
00078                 WIZ_WARN("failure of timeout[%d]ms\r\n",t.read_ms());
00079                 _state.res = RES_NULL;
00080                 t.stop();
00081                 return -1;
00082             }
00083         }
00084 
00085         t.stop();
00086     }
00087 
00088     WIZ_INFO("[OK]\r\n");
00089     _state.res = RES_NULL;
00090 
00091     return 0;
00092 }
00093 
00094 int WizFi310::cmdAT()
00095 {
00096     int resp;
00097 
00098     resp = sendCommand("AT");
00099 
00100     return resp;
00101 }
00102 int WizFi310::cmdTEST()
00103 {
00104   return sendCommand("AT");
00105 }
00106 
00107 int WizFi310::cmdMECHO(bool flg)
00108 {
00109     int status;
00110     char cmd[CFG_CMD_SIZE];
00111 
00112     sprintf(cmd,"AT+MECHO=%d",flg);
00113     status = sendCommand(cmd);
00114 
00115     return status;
00116 }
00117 
00118 int WizFi310::cmdUSET(int baud, char *flow)
00119 {
00120     int status;
00121     char cmd[CFG_CMD_SIZE];
00122 
00123     sprintf(cmd,"AT+USET=%d,N,8,1,%s",baud, flow);
00124     status = sendCommand(cmd);
00125 
00126     if(status == 0)
00127     {
00128         wait(1);
00129         _state.buf->flush();
00130     }
00131 
00132     return status;
00133 }
00134 
00135 int WizFi310::cmdMMAC(const char *mac)
00136 {
00137     int resp;
00138     char cmd[CFG_CMD_SIZE];
00139 
00140     if (mac)
00141     {
00142         sprintf(cmd, "AT+MMAC=%s",mac);
00143         resp = sendCommand(cmd);
00144     }
00145     else
00146     {
00147         sprintf(cmd, "AT+MMAC=?");
00148         resp = sendCommand(cmd, RES_MACADDRESS);
00149     }
00150 
00151     return resp;
00152 }
00153 
00154 int WizFi310::cmdWSET(WiFiMode mode, const char *ssid, const char *bssid, int channel)
00155 {
00156     char cmd[CFG_CMD_SIZE];
00157 
00158     if(*bssid == NULL)
00159     {
00160         sprintf(cmd, "AT+WSET=%d,%s",mode, ssid);
00161     }
00162     else
00163     {
00164         sprintf(cmd, "AT+WSET=%d,%s,%s,%d",mode, ssid, bssid, channel);
00165     }
00166 
00167     return sendCommand(cmd);
00168 }
00169 
00170 int WizFi310::cmdWANT(AntennaMode mode)
00171 {
00172     char cmd[CFG_CMD_SIZE];
00173     sprintf(cmd, "AT+WANT=%d",mode); 
00174     
00175     return sendCommand(cmd);
00176 }
00177 
00178 int WizFi310::cmdWNET(bool is_dhcp)
00179 {
00180     char cmd[CFG_CMD_SIZE];
00181 
00182     if(is_dhcp == true)
00183     {
00184         sprintf(cmd, "AT+WNET=1");
00185     }
00186     else
00187     {
00188         sprintf(cmd, "AT+WNET=0,%s,%s,%s",_state.ip,_state.netmask,_state.gateway);
00189     }
00190 
00191     return sendCommand(cmd);
00192 }
00193 
00194 int WizFi310::cmdWSEC(WiFiMode mode, const char *key, const char *sec)
00195 {
00196     char cmd[CFG_CMD_SIZE];
00197 
00198     if(*sec == NULL)
00199     {
00200         sprintf(cmd, "AT+WSEC=%d,,%s",mode, key);
00201     }
00202     else
00203     {
00204         sprintf(cmd, "AT+WSEC=%d,%s,%s",mode, sec, key);
00205     }
00206 
00207     return sendCommand(cmd);
00208 }
00209 
00210 int WizFi310::cmdWJOIN()
00211 {
00212     //if( sendCommand("AT+WJOIN", RES_WJOIN, CFG_JOIN_TIMEOUT) )
00213     if( sendCommand("AT+WJOIN", RES_NULL, CFG_JOIN_TIMEOUT) )
00214     {
00215         WIZ_ERR("cmdWJOIN");
00216         return -1;
00217     }
00218 
00219     if( cmdWSTATUS() )
00220         return -1;
00221 
00222     WIZ_INFO("WizFi310 is successfully join to AP");
00223 
00224     return 0;
00225 }
00226 
00227 int WizFi310::cmdWLEAVE()
00228 {
00229     return sendCommand("AT+WLEAVE");
00230 }
00231 
00232 
00233 int WizFi310::cmdWSTATUS()
00234 {
00235     if( sendCommand("AT+WSTATUS", RES_WSTATUS, DEFAULT_WAIT_RESP_TIMEOUT) )
00236     {
00237         WIZ_ERR("cmdWSTATUS");
00238         return -1;
00239     }
00240 
00241     WIZ_INFO("IP      : %s", _state.ip);
00242     WIZ_INFO("Gateway : %s", _state.gateway);
00243 
00244     return 0;
00245 }
00246 
00247 int WizFi310::cmdSCON ( const char *openType, const char *socketType, int localPort, const char *dataMode)
00248 {
00249     char cmd[CFG_CMD_SIZE];
00250 
00251     sprintf(cmd,"AT+SCON=%s,%s,,,%d,%s",openType, socketType, localPort, dataMode);
00252     return sendCommand(cmd);
00253 }
00254 
00255 int WizFi310::cmdSCON ( const char *openType, const char *socketType, const char *remoteIp, int remotePort, int localPort, const char *dataMode)
00256 {
00257     int resp;
00258     char cmd[CFG_CMD_SIZE];
00259 
00260     if(localPort == 0)
00261         sprintf(cmd,"AT+SCON=%s,%s,%s,%d,%s,%s",openType, socketType, remoteIp, remotePort, "", dataMode);
00262     else
00263         sprintf(cmd,"AT+SCON=%s,%s,%s,%d,%d,%s",openType, socketType, remoteIp, remotePort, localPort, dataMode);
00264 
00265     resp = sendCommand(cmd, RES_CONNECT, 30000 );
00266 
00267     return resp;
00268 }
00269 
00270 int WizFi310::cmdSSEND ( const char *data, int cid, int sendSize, const char *remoteIp, int remotePort, int Timeout )
00271 {
00272     int i, resp;
00273     Timer t;
00274     char cmd[CFG_CMD_SIZE];
00275 
00276     if (lockUart(Timeout))    return -1;
00277 
00278     clearFlags();
00279     if(remoteIp == NULL)
00280     {
00281         sprintf(cmd,"AT+SSEND=%d,,,%d",cid, sendSize);
00282     }
00283     else
00284     {
00285         sprintf(cmd,"AT+SSEND=%d,%s,%d,%d",cid, remoteIp, remotePort, sendSize);
00286     }
00287 
00288     _con[cid].send_length = sendSize;
00289 
00290     resp = sendCommand(cmd, RES_SSEND, 2000, 1);
00291 
00292     unlockUart();
00293     if(resp){
00294         WIZ_DBG("Fail cmdSSEND")
00295        return -1;
00296     }
00297 
00298     for(i=0; i<sendSize; i++)
00299     {
00300         putUart(data[i]);
00301     }
00302     unlockUart();
00303 
00304     if(Timeout)
00305     {
00306         t.start();
00307         for(;;)
00308         {
00309             if (_state.ok) break;
00310             if (_state.failure || t.read_ms() > Timeout)
00311             {
00312                 WIZ_WARN("failure or timeout\r\n");
00313                 return -1;
00314             }
00315         }
00316         t.stop();
00317     }
00318 
00319     wait(0.05);
00320     WIZ_INFO("%s\r\n",data);
00321 
00322     return i;
00323 }
00324 
00325 
00326 int WizFi310::cmdCLOSE ( int cid )
00327 {
00328     char cmd[CFG_CMD_SIZE];
00329 
00330     sprintf(cmd,"AT+SMGMT=%d",cid);
00331     return sendCommand(cmd);
00332 }
00333 
00334 
00335 int WizFi310::cmdFDNS (const char *host)
00336 {
00337     char cmd[CFG_CMD_SIZE];
00338     int resp;
00339 
00340     sprintf(cmd,"AT+FDNS=%s,3000",host);
00341     resp = sendCommand(cmd, RES_FDNS);
00342 
00343     WIZ_DBG("%s",_state.resolv);
00344     return resp;
00345 }
00346 
00347 int WizFi310::cmdSMGMT ( int cid )
00348 {
00349     int resp;
00350 
00351     resp = sendCommand("AT+SMGMT=?", RES_SMGMT);
00352     return resp;
00353 }