cliff

Fork of WizFi250Interface by WIZnet

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers WizFi250_at.cpp Source File

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