This is library for using WizFi250

Dependents:   WebSocket_WizFi250_HelloWorld IFTTT_WizFi250 AxedaGo-WizFi250 FANARM_AP_udp_server ... more

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 
00220 int WizFi250::cmdWSTATUS()
00221 {
00222     if( sendCommand("AT+WSTATUS", RES_WSTATUS, DEFAULT_WAIT_RESP_TIMEOUT) )
00223     {
00224         WIZ_ERR("cmdWSTATUS");
00225         return -1;
00226     }
00227 
00228     WIZ_INFO("IP      : %s", _state.ip);
00229     WIZ_INFO("Gateway : %s", _state.gateway);
00230 
00231     return 0;
00232 }
00233 
00234 int WizFi250::cmdSCON ( const char *openType, const char *socketType, int localPort, const char *dataMode)
00235 {
00236     char cmd[CFG_CMD_SIZE];
00237 
00238     sprintf(cmd,"AT+SCON=%s,%s,,,%d,%s",openType, socketType, localPort, dataMode);
00239     return sendCommand(cmd);
00240 }
00241 
00242 int WizFi250::cmdSCON ( const char *openType, const char *socketType, const char *remoteIp, int remotePort, int localPort, const char *dataMode)
00243 {
00244     int resp;
00245     char cmd[CFG_CMD_SIZE];
00246 
00247     if(localPort == 0)
00248         sprintf(cmd,"AT+SCON=%s,%s,%s,%d,%s,%s",openType, socketType, remoteIp, remotePort, "", dataMode);
00249     else
00250         sprintf(cmd,"AT+SCON=%s,%s,%s,%d,%d,%s",openType, socketType, remoteIp, remotePort, localPort, dataMode);
00251 
00252     resp = sendCommand(cmd, RES_CONNECT, 15000 );
00253 
00254     return resp;
00255 }
00256 
00257 int WizFi250::cmdSSEND ( const char *data, int cid, int sendSize, const char *remoteIp, int remotePort, int Timeout )
00258 {
00259     int i, resp;
00260     Timer t;
00261     char cmd[CFG_CMD_SIZE];
00262 
00263     if (lockUart(Timeout))    return -1;
00264 
00265     clearFlags();
00266     if(remoteIp == NULL)
00267     {
00268         sprintf(cmd,"AT+SSEND=%d,,,%d",cid, sendSize);
00269     }
00270     else
00271     {
00272         sprintf(cmd,"AT+SSEND=%d,%s,%d,%d",cid, remoteIp, remotePort, sendSize);
00273     }
00274 
00275     _con[cid].send_length = sendSize;
00276 
00277     resp = sendCommand(cmd, RES_SSEND, 2000, 1);
00278 
00279     unlockUart();
00280     if(resp){
00281         WIZ_DBG("Fail cmdSSEND")
00282        return -1;
00283     }
00284 
00285     for(i=0; i<sendSize; i++)
00286     {
00287         putUart(data[i]);
00288     }
00289     unlockUart();
00290 
00291     if(Timeout)
00292     {
00293         t.start();
00294         for(;;)
00295         {
00296             if (_state.ok) break;
00297             if (_state.failure || t.read_ms() > Timeout)
00298             {
00299                 WIZ_WARN("failure or timeout\r\n");
00300                 return -1;
00301             }
00302         }
00303         t.stop();
00304     }
00305 
00306     wait(0.05);
00307     WIZ_INFO("%s\r\n",data);
00308 
00309     return i;
00310 }
00311 
00312 
00313 int WizFi250::cmdCLOSE ( int cid )
00314 {
00315     char cmd[CFG_CMD_SIZE];
00316 
00317     sprintf(cmd,"AT+SMGMT=%d",cid);
00318     return sendCommand(cmd);
00319 }
00320 
00321 
00322 int WizFi250::cmdFDNS (const char *host)
00323 {
00324     char cmd[CFG_CMD_SIZE];
00325     int resp;
00326 
00327     sprintf(cmd,"AT+FDNS=%s,3000",host);
00328     resp = sendCommand(cmd, RES_FDNS);
00329 
00330     WIZ_DBG("%s",_state.resolv);
00331     return resp;
00332 }
00333 
00334 int WizFi250::cmdSMGMT ( int cid )
00335 {
00336     int resp;
00337 
00338     resp = sendCommand("AT+SMGMT=?", RES_SMGMT);
00339     return resp;
00340 }