GSwifiInterface library (interface for GainSpan Wi-Fi GS1011 modules) Please see https://mbed.org/users/gsfan/notebook/GSwifiInterface/
Dependents: GSwifiInterface_HelloWorld GSwifiInterface_HelloServo GSwifiInterface_UDPEchoServer GSwifiInterface_UDPEchoClient ... more
Fork of WiflyInterface by
GSwifi_at.cpp
00001 /* Copyright (C) 2013 gsfan, MIT License 00002 * 00003 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00004 * and associated documentation files (the "Software"), to deal in the Software without restriction, 00005 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 00006 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 00007 * furnished to do so, subject to the following conditions: 00008 * 00009 * The above copyright notice and this permission notice shall be included in all copies or 00010 * substantial portions of the Software. 00011 * 00012 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 00013 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00014 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00015 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00016 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00017 */ 00018 00019 #include "GSwifi.h" 00020 00021 void GSwifi::clearFlags () { 00022 _state.ok = false; 00023 _state.failure = false; 00024 _state.res = RES_NULL; 00025 _state.n = 0; 00026 } 00027 00028 int GSwifi::sendCommand(const char * cmd, Response res, int timeout) { 00029 int i; 00030 Timer t; 00031 00032 if (lockUart(timeout)) return -1; 00033 00034 clearFlags(); 00035 _state.res = res; 00036 for (i = 0; i < strlen(cmd); i ++) { 00037 putUart(cmd[i]); 00038 } 00039 putUart('\r'); 00040 putUart('\n'); 00041 unlockUart(); 00042 INFO("command: '%s'\r\n", cmd); 00043 00044 if (timeout) { 00045 t.start(); 00046 for (;;) { 00047 if (_state.ok && _state.res == RES_NULL) break; 00048 if (_state.failure) { 00049 WARN("failure\r\n"); 00050 _state.res = RES_NULL; 00051 return -1; 00052 } 00053 if (t.read_ms() > timeout) { 00054 WARN("timeout\r\n"); 00055 _state.res = RES_NULL; 00056 return -1; 00057 } 00058 } 00059 t.stop(); 00060 } 00061 INFO("ok\r\n"); 00062 _state.res = RES_NULL; 00063 00064 return 0; 00065 } 00066 00067 int GSwifi::sendData(const char * data, int len, int timeout, const char * cmd) { 00068 int i; 00069 Timer t; 00070 00071 #ifdef CFG_SPI_DATAINTERFACE 00072 if (_state.datainterface) { 00073 if (lockSpi(timeout)) return -1; 00074 00075 clearFlags(); 00076 for (i = 0; i < strlen(cmd); i ++) { 00077 putSpi(cmd[i]); 00078 } 00079 for (i = 0; i < len; i ++) { 00080 switch (data[i]) { 00081 case 0: // encode 00082 case 0xf3: 00083 case 0xf5: 00084 case 0xfa: 00085 case 0xfb: 00086 case 0xfd: 00087 case 0xff: 00088 putSpi(0xfb); 00089 putSpi(data[i] ^ 0x20); // xor 00090 break; 00091 default: 00092 putSpi(data[i]); 00093 break; 00094 } 00095 } 00096 unlockSpi(); 00097 } else { 00098 #endif 00099 if (lockUart(timeout)) return -1; 00100 00101 clearFlags(); 00102 for (i = 0; i < strlen(cmd); i ++) { 00103 putUart(cmd[i]); 00104 } 00105 for (i = 0; i < len; i ++) { 00106 putUart(data[i]); 00107 } 00108 unlockUart(); 00109 #ifdef CFG_SPI_DATAINTERFACE 00110 } // _state.datainterface 00111 #endif 00112 INFO("data: '%s' %d %d\r\n", cmd, len, _state.datainterface); 00113 00114 if (timeout) { 00115 t.start(); 00116 for (;;) { 00117 if (_state.ok) break; 00118 if (_state.failure) { 00119 WARN("failure\r\n"); 00120 return -1; 00121 } 00122 if (t.read_ms() > timeout) { 00123 WARN("timeout\r\n"); 00124 return -1; 00125 } 00126 } 00127 t.stop(); 00128 } 00129 00130 return i; 00131 } 00132 00133 00134 int GSwifi::cmdAT () { 00135 return sendCommand("AT"); 00136 } 00137 00138 int GSwifi::cmdE (bool n) { 00139 char cmd[CFG_CMD_SIZE]; 00140 sprintf(cmd, "ATE%d", n ? 1 : 0); 00141 return sendCommand(cmd); 00142 } 00143 00144 int GSwifi::cmdR (bool n) { 00145 char cmd[CFG_CMD_SIZE]; 00146 sprintf(cmd, "AT&R%d", n ? 1 : 0); 00147 return sendCommand(cmd); 00148 } 00149 00150 int GSwifi::cmdNMAC (const char *s) { 00151 int r; 00152 char cmd[CFG_CMD_SIZE]; 00153 const char xmac[] = "00:1D:C9:01:99:99"; 00154 if (s) { 00155 sprintf(cmd, "AT+NMAC2=%s", s); 00156 r = sendCommand(cmd); 00157 } else { 00158 sprintf(cmd, "AT+NMAC=?"); 00159 r = sendCommand(cmd, RES_MACADDRESS); 00160 if (!r && strncmp(_state.mac, xmac, 17) == 0) { 00161 r = -1; 00162 } 00163 } 00164 return r; 00165 } 00166 00167 int GSwifi::cmdWREGDOMAIN (int n) { 00168 char cmd[CFG_CMD_SIZE]; 00169 sprintf(cmd, "AT+WREGDOMAIN=%d", n); 00170 return sendCommand(cmd); 00171 } 00172 00173 int GSwifi::cmdWS () { 00174 return sendCommand("AT+WS"); 00175 } 00176 00177 int GSwifi::cmdWM (int n) { 00178 char cmd[CFG_CMD_SIZE]; 00179 sprintf(cmd, "AT+WM=%d", n); 00180 return sendCommand(cmd); 00181 } 00182 00183 int GSwifi::cmdWA (const char *s) { 00184 char cmd[CFG_CMD_SIZE]; 00185 sprintf(cmd, "AT+WA=%s", s); 00186 return sendCommand(cmd, RES_DHCP, CFG_TIMEOUT); 00187 } 00188 00189 int GSwifi::cmdWD () { 00190 return sendCommand("AT+WD"); 00191 } 00192 00193 int GSwifi::cmdWWPS (bool n, const char *p) { 00194 char cmd[CFG_CMD_SIZE]; 00195 if (p) { 00196 sprintf(cmd, "AT+WWPS=2,%s", p); 00197 } else { 00198 sprintf(cmd, "AT+WWPS=%d", n ? 1 : 0); 00199 } 00200 return sendCommand(cmd, RES_WPS, CFG_TIMEOUT); 00201 } 00202 00203 int GSwifi::cmdNSTAT () { 00204 return sendCommand("AT+NSTAT=?"); 00205 } 00206 00207 int GSwifi::cmdWSTATUS () { 00208 return sendCommand("AT+WSTATUS", RES_STATUS); 00209 } 00210 00211 int GSwifi::cmdWRSSI () { 00212 return sendCommand("AT+WRSSI=?", RES_RSSI); 00213 } 00214 00215 int GSwifi::cmdWAUTH (int n) { 00216 char cmd[CFG_CMD_SIZE]; 00217 sprintf(cmd, "AT+WAUTH=%d", n); 00218 return sendCommand(cmd); 00219 } 00220 00221 int GSwifi::cmdWWEP (int n, const char *s) { 00222 char cmd[CFG_CMD_SIZE]; 00223 sprintf(cmd, "AT+WWEP%d=%s", n, s); 00224 return sendCommand(cmd); 00225 } 00226 00227 int GSwifi::cmdWPAPSK (const char *s, const char *p) { 00228 char cmd[CFG_CMD_SIZE]; 00229 sprintf(cmd, "AT+WPAPSK=%s,%s", s, p); 00230 return sendCommand(cmd, RES_WPAPSK, CFG_TIMEOUT); 00231 } 00232 00233 int GSwifi::cmdWRXACTIVE (bool n) { 00234 char cmd[CFG_CMD_SIZE]; 00235 sprintf(cmd, "AT+WRXACTIVE=%d", n ? 1 : 0); 00236 return sendCommand(cmd); 00237 } 00238 00239 int GSwifi::cmdWRXPS (bool n) { 00240 char cmd[CFG_CMD_SIZE]; 00241 sprintf(cmd, "AT+WRXPS=%d", n ? 1 : 0); 00242 return sendCommand(cmd); 00243 } 00244 00245 int GSwifi::cmdWP (int n) { 00246 char cmd[CFG_CMD_SIZE]; 00247 sprintf(cmd, "AT+WP=%d", n); 00248 return sendCommand(cmd); 00249 } 00250 00251 int GSwifi::cmdNDHCP (bool n, const char *s, int m) { 00252 char cmd[CFG_CMD_SIZE]; 00253 if (n) { 00254 if (s) { 00255 sprintf(cmd, "AT+NDHCP=1,%s", s); 00256 } else { 00257 sprintf(cmd, "AT+NDHCP=1"); 00258 } 00259 return sendCommand(cmd, RES_DHCP, m); 00260 } else { 00261 return sendCommand("AT+NDHCP=0"); 00262 } 00263 } 00264 00265 int GSwifi::cmdDHCPSRVR (bool n) { 00266 char cmd[CFG_CMD_SIZE]; 00267 sprintf(cmd, "AT+DHCPSRVR=%d", n ? 1 : 0); 00268 return sendCommand(cmd); 00269 } 00270 00271 int GSwifi::cmdNSET (const char *s, const char *t, const char *u) { 00272 char cmd[CFG_CMD_SIZE]; 00273 sprintf(cmd, "AT+NSET=%s,%s,%s", s, t, u); 00274 return sendCommand(cmd); 00275 } 00276 00277 int GSwifi::cmdDNS (bool n, const char *s) { 00278 char cmd[CFG_CMD_SIZE]; 00279 if (n) { 00280 if (s) { 00281 sprintf(cmd, "AT+DNS=1,%s", s); 00282 } else { 00283 sprintf(cmd, "AT+DNS=1," CFG_DNSNAME); 00284 } 00285 } else { 00286 sprintf(cmd, "AT+DNS=0"); 00287 } 00288 return sendCommand(cmd); 00289 } 00290 00291 int GSwifi::cmdDNSLOOKUP (const char *s) { 00292 char cmd[CFG_CMD_SIZE]; 00293 sprintf(cmd, "AT+DNSLOOKUP=%s", s); 00294 return sendCommand(cmd, RES_DNSLOOKUP, CFG_TIMEOUT); 00295 } 00296 00297 int GSwifi::cmdDNSSET (const char *s) { 00298 char cmd[CFG_CMD_SIZE]; 00299 sprintf(cmd, "AT+DNSSET=%s", s); 00300 return sendCommand(cmd); 00301 } 00302 00303 int GSwifi::cmdSTORENWCONN () { 00304 return sendCommand("AT+STORENWCONN"); 00305 } 00306 00307 int GSwifi::cmdRESTORENWCONN () { 00308 return sendCommand("AT+RESTORENWCONN"); 00309 } 00310 00311 int GSwifi::cmdBDATA (bool n) { 00312 char cmd[CFG_CMD_SIZE]; 00313 sprintf(cmd, "AT+BDATA=%d", n ? 1 : 0); 00314 return sendCommand(cmd); 00315 } 00316 00317 int GSwifi::cmdNCTCP (const char *s, int n) { 00318 char cmd[CFG_CMD_SIZE]; 00319 sprintf(cmd, "AT+NCTCP=%s,%d", s, n); 00320 return sendCommand(cmd, RES_CONNECT, CFG_TIMEOUT); 00321 } 00322 00323 int GSwifi::cmdNCUDP (const char *s, int n, int m) { 00324 char cmd[CFG_CMD_SIZE]; 00325 if (m) { 00326 sprintf(cmd, "AT+NCUDP=%s,%d,%d", s, n, m); 00327 } else { 00328 sprintf(cmd, "AT+NCUDP=%s,%d", s, n); 00329 } 00330 return sendCommand(cmd, RES_CONNECT, CFG_TIMEOUT); 00331 } 00332 00333 int GSwifi::cmdNSTCP (int n) { 00334 char cmd[CFG_CMD_SIZE]; 00335 sprintf(cmd, "AT+NSTCP=%d", n); 00336 return sendCommand(cmd, RES_CONNECT); 00337 } 00338 00339 int GSwifi::cmdNSUDP (int n) { 00340 char cmd[CFG_CMD_SIZE]; 00341 sprintf(cmd, "AT+NSUDP=%d", n); 00342 return sendCommand(cmd, RES_CONNECT); 00343 } 00344 00345 int GSwifi::cmdNCLOSE (int n) { 00346 char cmd[CFG_CMD_SIZE]; 00347 sprintf(cmd, "AT+NCLOSE=%X", n); 00348 return sendCommand(cmd, RES_NULL, CFG_TIMEOUT); 00349 } 00350 00351 int GSwifi::cmdNCLOSEALL () { 00352 return sendCommand("AT+NCLOSEALL", RES_NULL, CFG_TIMEOUT); 00353 } 00354 00355 int GSwifi::cmdHTTPCONF (int n, const char *s) { 00356 char cmd[CFG_CMD_SIZE]; 00357 sprintf(cmd, "AT+HTTPCONF=%d,%s", n, s); 00358 return sendCommand(cmd); 00359 } 00360 00361 int GSwifi::cmdHTTPCONFDEL (int n) { 00362 char cmd[CFG_CMD_SIZE]; 00363 sprintf(cmd, "AT+HTTPCONFDEL=%d", n); 00364 return sendCommand(cmd); 00365 } 00366 00367 int GSwifi::cmdHTTPOPEN (const char *s, int n, bool m) { 00368 char cmd[CFG_CMD_SIZE]; 00369 if (m) { 00370 sprintf(cmd, "AT+HTTPOPEN=%s,%d,1", s, n); 00371 } else { 00372 sprintf(cmd, "AT+HTTPOPEN=%s,%d", s, n); 00373 } 00374 return sendCommand(cmd, RES_HTTP, CFG_TIMEOUT); 00375 } 00376 00377 int GSwifi::cmdHTTPSEND (int n, bool m, const char *s, int t) { 00378 char cmd[CFG_CMD_SIZE]; 00379 if (m) { 00380 sprintf(cmd, "AT+HTTPSEND=%X,3,%d,%s,%d", n, CFG_TIMEOUT / 1000, s, t); 00381 } else { 00382 sprintf(cmd, "AT+HTTPSEND=%X,1,%d,%s", n, CFG_TIMEOUT / 1000, s); 00383 } 00384 return sendCommand(cmd); 00385 } 00386 00387 int GSwifi::cmdHTTPCLOSE (int n) { 00388 char cmd[CFG_CMD_SIZE]; 00389 sprintf(cmd, "AT+HTTPCLOSE=%X", n); 00390 return sendCommand(cmd, RES_NULL, CFG_TIMEOUT); 00391 } 00392 00393 int GSwifi::cmdPSDPSLEEP (int n) { 00394 char cmd[CFG_CMD_SIZE]; 00395 if (n) { 00396 sprintf(cmd, "AT+PSDPSLEEP=%d", n); 00397 } else { 00398 sprintf(cmd, "AT+PSDPSLEEP"); 00399 } 00400 return sendCommand(cmd, RES_NULL, 0); 00401 } 00402 00403 int GSwifi::cmdPSSTBY (int n, int m) { 00404 char cmd[CFG_CMD_SIZE]; 00405 sprintf(cmd, "AT+PSSTBY=%d,0,%d,%d", n, m, m); 00406 return sendCommand(cmd, RES_NULL, 0); 00407 } 00408 00409 int GSwifi::cmdWEBPROV (const char *s, const char *p) { 00410 char cmd[CFG_CMD_SIZE]; 00411 sprintf(cmd, "AT+WEBPROV=%s,%s", s, p); 00412 return sendCommand(cmd); 00413 } 00414 00415 int GSwifi::cmdSETTIME (const char *s, const char *t) { 00416 char cmd[CFG_CMD_SIZE]; 00417 sprintf(cmd, "AT+SETTIME=%s,%s", s, t); 00418 return sendCommand(cmd); 00419 } 00420 00421 int GSwifi::cmdGETTIME () { 00422 return sendCommand("AT+GETTIME=?", RES_TIME); 00423 } 00424 00425 int GSwifi::cmdNTIMESYNC (bool n, const char *s, int m) { 00426 char cmd[CFG_CMD_SIZE]; 00427 if (n) { 00428 if (m) { 00429 sprintf(cmd, "AT+NTIMESYNC=1,%s,%d,1,%d", s, CFG_TIMEOUT / 1000, m); 00430 } else { 00431 sprintf(cmd, "AT+NTIMESYNC=1,%s,%d,0", s, CFG_TIMEOUT / 1000); 00432 } 00433 } else { 00434 sprintf(cmd, "AT+NTIMESYNC=0"); 00435 } 00436 return sendCommand(cmd, RES_NULL, CFG_TIMEOUT); 00437 } 00438 00439 int GSwifi::cmdDGPIO (int n, int m) { 00440 char cmd[CFG_CMD_SIZE]; 00441 sprintf(cmd, "AT+DGPIO=%d,%d", n, m); 00442 return sendCommand(cmd); 00443 }
Generated on Thu Jul 14 2022 07:53:37 by 1.7.2