ajeet prajapati / WizFi310Interface_Legacy45566

Fork of WizFi310Interface_Legacy by WIZnet

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 
00103 int WizFi310::cmdMECHO(bool flg)
00104 {
00105     int status;
00106     char cmd[CFG_CMD_SIZE];
00107 
00108     sprintf(cmd,"AT+MECHO=%d",flg);
00109     status = sendCommand(cmd);
00110 
00111     return status;
00112 }
00113 
00114 int WizFi310::cmdUSET(int baud, char *flow)
00115 {
00116     int status;
00117     char cmd[CFG_CMD_SIZE];
00118 
00119     sprintf(cmd,"AT+USET=%d,N,8,1,%s",baud, flow);
00120     status = sendCommand(cmd);
00121 
00122     if(status == 0)
00123     {
00124         wait(1);
00125         _state.buf->flush();
00126     }
00127 
00128     return status;
00129 }
00130 
00131 int WizFi310::cmdMMAC(const char *mac)
00132 {
00133     int resp;
00134     char cmd[CFG_CMD_SIZE];
00135 
00136     if (mac)
00137     {
00138         sprintf(cmd, "AT+MMAC=%s",mac);
00139         resp = sendCommand(cmd);
00140     }
00141     else
00142     {
00143         sprintf(cmd, "AT+MMAC=?");
00144         resp = sendCommand(cmd, RES_MACADDRESS);
00145     }
00146 
00147     return resp;
00148 }
00149 
00150 int WizFi310::cmdWSET(WiFiMode mode, const char *ssid, const char *bssid, int channel)
00151 {
00152     char cmd[CFG_CMD_SIZE];
00153 
00154     if(*bssid == NULL)
00155     {
00156         sprintf(cmd, "AT+WSET=%d,%s",mode, ssid);
00157     }
00158     else
00159     {
00160         sprintf(cmd, "AT+WSET=%d,%s,%s,%d",mode, ssid, bssid, channel);
00161     }
00162 
00163     return sendCommand(cmd);
00164 }
00165 
00166 int WizFi310::cmdWANT(AntennaMode mode)
00167 {
00168     char cmd[CFG_CMD_SIZE];
00169     sprintf(cmd, "AT+WANT=%d",mode); 
00170     
00171     return sendCommand(cmd);
00172 }
00173 
00174 int WizFi310::cmdWNET(bool is_dhcp)
00175 {
00176     char cmd[CFG_CMD_SIZE];
00177 
00178     if(is_dhcp == true)
00179     {
00180         sprintf(cmd, "AT+WNET=1");
00181     }
00182     else
00183     {
00184         sprintf(cmd, "AT+WNET=0,%s,%s,%s",_state.ip,_state.netmask,_state.gateway);
00185     }
00186 
00187     return sendCommand(cmd);
00188 }
00189 
00190 int WizFi310::cmdWSEC(WiFiMode mode, const char *key, const char *sec)
00191 {
00192     char cmd[CFG_CMD_SIZE];
00193 
00194     if(*sec == NULL)
00195     {
00196         sprintf(cmd, "AT+WSEC=%d,,%s",mode, key);
00197     }
00198     else
00199     {
00200         sprintf(cmd, "AT+WSEC=%d,%s,%s",mode, sec, key);
00201     }
00202 
00203     return sendCommand(cmd);
00204 }
00205 
00206 int WizFi310::cmdWJOIN()
00207 {
00208     //if( sendCommand("AT+WJOIN", RES_WJOIN, CFG_JOIN_TIMEOUT) )
00209     if( sendCommand("AT+WJOIN", RES_NULL, CFG_JOIN_TIMEOUT) )
00210     {
00211         WIZ_ERR("cmdWJOIN");
00212         return -1;
00213     }
00214 
00215     if( cmdWSTATUS() )
00216         return -1;
00217 
00218     //WIZ_INFO("WizFi310 is successfully join to AP");
00219 
00220     return 0;
00221 }
00222 
00223 int WizFi310::cmdWLEAVE()
00224 {
00225     return sendCommand("AT+WLEAVE");
00226 }
00227 
00228 
00229 int WizFi310::cmdWSTATUS()
00230 {
00231     if( sendCommand("AT+WSTATUS", RES_WSTATUS, DEFAULT_WAIT_RESP_TIMEOUT) )
00232     {
00233         WIZ_ERR("cmdWSTATUS");
00234         return -1;
00235     }
00236 
00237     WIZ_INFO("IP      : %s", _state.ip);
00238     WIZ_INFO("Gateway : %s", _state.gateway);
00239 
00240     return 0;
00241 }
00242 
00243 int WizFi310::cmdSCON ( const char *openType, const char *socketType, int localPort, const char *dataMode)
00244 {
00245     char cmd[CFG_CMD_SIZE];
00246 
00247     sprintf(cmd,"AT+SCON=%s,%s,,,%d,%s",openType, socketType, localPort, dataMode);
00248     return sendCommand(cmd);
00249 }
00250 
00251 int WizFi310::cmdSCON ( const char *openType, const char *socketType, const char *remoteIp, int remotePort, int localPort, const char *dataMode)
00252 {
00253     int resp;
00254     char cmd[CFG_CMD_SIZE];
00255 
00256     if(localPort == 0)
00257         sprintf(cmd,"AT+SCON=%s,%s,%s,%d,%s,%s",openType, socketType, remoteIp, remotePort, "", dataMode);
00258     else
00259         sprintf(cmd,"AT+SCON=%s,%s,%s,%d,%d,%s",openType, socketType, remoteIp, remotePort, localPort, dataMode);
00260 
00261     resp = sendCommand(cmd, RES_CONNECT, 30000 );
00262 
00263     return resp;
00264 }
00265 
00266 int WizFi310::cmdSSEND ( const char *data, int cid, int sendSize, const char *remoteIp, int remotePort, int Timeout )
00267 {
00268     int i, resp;
00269     Timer t;
00270     char cmd[CFG_CMD_SIZE];
00271 
00272     if (lockUart(Timeout))    return -1;
00273 
00274     clearFlags();
00275     if(remoteIp == NULL)
00276     {
00277         sprintf(cmd,"AT+SSEND=%d,,,%d",cid, sendSize);
00278     }
00279     else
00280     {
00281         sprintf(cmd,"AT+SSEND=%d,%s,%d,%d",cid, remoteIp, remotePort, sendSize);
00282     }
00283 
00284     _con[cid].send_length = sendSize;
00285 
00286     resp = sendCommand(cmd, RES_SSEND, 2000, 1);
00287 
00288     unlockUart();
00289     if(resp){
00290         WIZ_DBG("Fail cmdSSEND")
00291        return -1;
00292     }
00293 
00294     for(i=0; i<sendSize; i++)
00295     {
00296         putUart(data[i]);
00297     }
00298     unlockUart();
00299 
00300     if(Timeout)
00301     {
00302         t.start();
00303         for(;;)
00304         {
00305             if (_state.ok) break;
00306             if (_state.failure || t.read_ms() > Timeout)
00307             {
00308                 WIZ_WARN("failure or timeout\r\n");
00309                 return -1;
00310             }
00311         }
00312         t.stop();
00313     }
00314 
00315     wait(0.05);
00316     WIZ_INFO("%s\r\n",data);
00317 
00318     return i;
00319 }
00320 
00321 
00322 int WizFi310::cmdCLOSE ( int cid )
00323 {
00324     char cmd[CFG_CMD_SIZE];
00325 
00326     sprintf(cmd,"AT+SMGMT=%d",cid);
00327     return sendCommand(cmd);
00328 }
00329 
00330 
00331 int WizFi310::cmdFDNS (const char *host)
00332 {
00333     char cmd[CFG_CMD_SIZE];
00334     int resp;
00335 
00336     sprintf(cmd,"AT+FDNS=%s,3000",host);
00337     resp = sendCommand(cmd, RES_FDNS);
00338 
00339     WIZ_DBG("%s",_state.resolv);
00340     return resp;
00341 }
00342 
00343 int WizFi310::cmdSMGMT ( int cid )
00344 {
00345     int resp;
00346 
00347     resp = sendCommand("AT+SMGMT=?", RES_SMGMT);
00348     return resp;
00349 }