Freedman v2

Dependents:   Freedman_v2

Fork of WizFi250Interface by DongEun Koak

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::cmdWNET(bool is_dhcp)
00163 {
00164     char cmd[CFG_CMD_SIZE];
00165 
00166     if(is_dhcp == true)
00167     {
00168         sprintf(cmd, "AT+WNET=1");
00169     }
00170     else
00171     {
00172         sprintf(cmd, "AT+WNET=0,%s,%s,%s",_state.ip,_state.netmask,_state.gateway);
00173     }
00174 
00175     return sendCommand(cmd);
00176 }
00177 
00178 int WizFi250::cmdWSEC(WiFiMode mode, const char *key, const char *sec)
00179 {
00180     char cmd[CFG_CMD_SIZE];
00181 
00182     if(*sec == NULL)
00183     {
00184         sprintf(cmd, "AT+WSEC=%d,,%s",mode, key);
00185     }
00186     else
00187     {
00188         sprintf(cmd, "AT+WSEC=%d,%s,%s",mode, sec, key);
00189     }
00190 
00191     return sendCommand(cmd);
00192 }
00193 
00194 int WizFi250::cmdWJOIN()
00195 {
00196     //if( sendCommand("AT+WJOIN", RES_WJOIN, CFG_JOIN_TIMEOUT) )
00197     if( sendCommand("AT+WJOIN", RES_NULL, CFG_JOIN_TIMEOUT) )
00198     {
00199         WIZ_ERR("cmdWJOIN");
00200         return -1;
00201     }
00202 
00203     if( cmdWSTATUS() )
00204         return -1;
00205 
00206     WIZ_INFO("WizFi250 is successfully join to AP");
00207 
00208     return 0;
00209 }
00210 
00211 
00212 int WizFi250::cmdWSTATUS()
00213 {
00214     if( sendCommand("AT+WSTATUS", RES_WSTATUS, DEFAULT_WAIT_RESP_TIMEOUT) )
00215     {
00216         WIZ_ERR("cmdWSTATUS");
00217         return -1;
00218     }
00219 
00220     WIZ_INFO("IP      : %s", _state.ip);
00221     WIZ_INFO("Gateway : %s", _state.gateway);
00222 
00223     return 0;
00224 }
00225 
00226 int WizFi250::cmdSCON ( const char *openType, const char *socketType, int localPort, const char *dataMode)
00227 {
00228     char cmd[CFG_CMD_SIZE];
00229 
00230     sprintf(cmd,"AT+SCON=%s,%s,,,%d,%s",openType, socketType, localPort, dataMode);
00231     return sendCommand(cmd);
00232 }
00233 
00234 int WizFi250::cmdSCON ( const char *openType, const char *socketType, const char *remoteIp, int remotePort, int localPort, const char *dataMode)
00235 {
00236     int resp;
00237     char cmd[CFG_CMD_SIZE];
00238 
00239     if(localPort == 0)
00240         sprintf(cmd,"AT+SCON=%s,%s,%s,%d,%s,%s",openType, socketType, remoteIp, remotePort, "", dataMode);
00241     else
00242         sprintf(cmd,"AT+SCON=%s,%s,%s,%d,%d,%s",openType, socketType, remoteIp, remotePort, localPort, dataMode);
00243 
00244     resp = sendCommand(cmd, RES_CONNECT, 15000 );
00245 
00246     return resp;
00247 }
00248 
00249 int WizFi250::cmdSSEND ( const char *data, int cid, int sendSize, const char *remoteIp, int remotePort, int Timeout )
00250 {
00251     int i, resp;
00252     Timer t;
00253     char cmd[CFG_CMD_SIZE];
00254 
00255     if (lockUart(Timeout))    return -1;
00256 
00257     clearFlags();
00258     if(remoteIp == NULL)
00259     {
00260         sprintf(cmd,"AT+SSEND=%d,,,%d",cid, sendSize);
00261     }
00262     else
00263     {
00264         sprintf(cmd,"AT+SSEND=%d,%s,%d,%d",cid, remoteIp, remotePort, sendSize);
00265     }
00266 
00267     _con[cid].send_length = sendSize;
00268 
00269     resp = sendCommand(cmd, RES_SSEND, 2000, 1);
00270 
00271     unlockUart();
00272     if(resp){
00273         WIZ_DBG("Fail cmdSSEND")
00274        return -1;
00275     }
00276 
00277     for(i=0; i<sendSize; i++)
00278     {
00279         putUart(data[i]);
00280     }
00281     unlockUart();
00282 
00283     if(Timeout)
00284     {
00285         t.start();
00286         for(;;)
00287         {
00288             if (_state.ok) break;
00289             if (_state.failure || t.read_ms() > Timeout)
00290             {
00291                 WIZ_WARN("failure or timeout\r\n");
00292                 return -1;
00293             }
00294         }
00295         t.stop();
00296     }
00297 
00298     wait(0.05);
00299     WIZ_INFO("%s\r\n",data);
00300 
00301     return i;
00302 }
00303 
00304 
00305 int WizFi250::cmdCLOSE ( int cid )
00306 {
00307     char cmd[CFG_CMD_SIZE];
00308 
00309     sprintf(cmd,"AT+SMGMT=%d",cid);
00310     return sendCommand(cmd);
00311 }
00312 
00313 
00314 int WizFi250::cmdFDNS (const char *host)
00315 {
00316     char cmd[CFG_CMD_SIZE];
00317     int resp;
00318 
00319     sprintf(cmd,"AT+FDNS=%s,3000",host);
00320     resp = sendCommand(cmd, RES_FDNS);
00321 
00322     WIZ_DBG("%s",_state.resolv);
00323     return resp;
00324 }
00325 
00326 int WizFi250::cmdSMGMT ( int cid )
00327 {
00328     int resp;
00329 
00330     resp = sendCommand("AT+SMGMT=?", RES_SMGMT);
00331     return resp;
00332 }