diff --git a/TinyGsmClientA6.h b/TinyGsmClientA6.h index 2eb82b5..2a30076 100644 --- a/TinyGsmClientA6.h +++ b/TinyGsmClientA6.h @@ -420,124 +420,6 @@ public: * Battery functions */ - /* Public Utilities */ - template - void sendAT(Args... cmd) { - streamWrite("AT", cmd..., GSM_NL); - stream.flush(); - TINY_GSM_YIELD(); - DBG(GSM_NL, ">>> AT:", cmd...); - } - - // TODO: Optimize this! - uint8_t waitResponse(uint32_t timeout, String& data, - GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR), - GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL) - { - /*String r1s(r1); r1s.trim(); - String r2s(r2); r2s.trim(); - String r3s(r3); r3s.trim(); - String r4s(r4); r4s.trim(); - String r5s(r5); r5s.trim(); - DBG("### ..:", r1s, ",", r2s, ",", r3s, ",", r4s, ",", r5s);*/ - data.reserve(64); - bool gotData = false; - int mux = -1; - int len = 0; - int index = 0; - unsigned long startMillis = millis(); - do { - TINY_GSM_YIELD(); - while (stream.available() > 0) { - int a = streamRead(); - if (a <= 0) continue; // Skip 0x00 bytes, just in case - data += (char)a; - if (r1 && data.endsWith(r1)) { - index = 1; - goto finish; - } else if (r2 && data.endsWith(r2)) { - index = 2; - goto finish; - } else if (r3 && data.endsWith(r3)) { - index = 3; - goto finish; - } else if (r4 && data.endsWith(r4)) { - index = 4; - goto finish; - } else if (r5 && data.endsWith(r5)) { - index = 5; - goto finish; - } else if (data.endsWith(GF("+CIPRCV:"))) { - mux = stream.readStringUntil(',').toInt(); - data += mux; - data += (','); - len = stream.readStringUntil(',').toInt(); - data += len; - data += (','); - gotData = true; - index = 6; - goto finish; - } else if (data.endsWith(GF("+TCPCLOSED:"))) { - mux = stream.readStringUntil(',').toInt(); - data += mux; - data += (','); - String concl = stream.readStringUntil('\n'); - data += concl; - sockets[mux]->sock_connected = false; - index = 7; - goto finish; - } - } - } while (millis() - startMillis < timeout); - finish: - if (!index) { - data.trim(); - if (data.length()) { - DBG(GSM_NL, "### Unhandled:", data); - } - } - else { - data.trim(); - data.replace(GSM_NL GSM_NL, GSM_NL); - data.replace(GSM_NL, GSM_NL " "); - if (data.length()) { - DBG(GSM_NL, "<<< ", data); - } - } - if (gotData) { - int len_orig = len; - if (len > sockets[mux]->rx.free()) { - DBG(GSM_NL, "### Buffer overflow: ", len, "->", sockets[mux]->rx.free()); - } else { - DBG(GSM_NL, "### Got: ", len, "->", sockets[mux]->rx.free()); - } - while (len--) { - TINY_GSM_YIELD(); - int r = stream.read(); - if (r <= 0) continue; // Skip 0x00 bytes, just in case - sockets[mux]->rx.put((char)r); - } - if (len_orig > sockets[mux]->available()) { - DBG(GSM_NL, "### Fewer characters received than expected: ", sockets[mux]->available(), " vs ", len_orig); - } - } - return index; - } - - uint8_t waitResponse(uint32_t timeout, - GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR), - GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL) - { - String data; - return waitResponse(timeout, data, r1, r2, r3, r4, r5); - } - - uint8_t waitResponse(GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR), - GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL) - { - return waitResponse(1000, r1, r2, r3, r4, r5); - } - private: int modemConnect(const char* host, uint16_t port, uint8_t* mux) { sendAT(GF("+CIPSTART="), GF("\"TCP"), GF("\",\""), host, GF("\","), port); @@ -613,6 +495,123 @@ private: } else return false; } + template + void sendAT(Args... cmd) { + streamWrite("AT", cmd..., GSM_NL); + stream.flush(); + TINY_GSM_YIELD(); + DBG(GSM_NL, ">>> AT:", cmd...); + } + + // TODO: Optimize this! + uint8_t waitResponse(uint32_t timeout, String& data, + GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR), + GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL) + { + /*String r1s(r1); r1s.trim(); + String r2s(r2); r2s.trim(); + String r3s(r3); r3s.trim(); + String r4s(r4); r4s.trim(); + String r5s(r5); r5s.trim(); + DBG("### ..:", r1s, ",", r2s, ",", r3s, ",", r4s, ",", r5s);*/ + data.reserve(64); + bool gotData = false; + int mux = -1; + int len = 0; + int index = 0; + unsigned long startMillis = millis(); + do { + TINY_GSM_YIELD(); + while (stream.available() > 0) { + int a = streamRead(); + if (a <= 0) continue; // Skip 0x00 bytes, just in case + data += (char)a; + if (r1 && data.endsWith(r1)) { + index = 1; + goto finish; + } else if (r2 && data.endsWith(r2)) { + index = 2; + goto finish; + } else if (r3 && data.endsWith(r3)) { + index = 3; + goto finish; + } else if (r4 && data.endsWith(r4)) { + index = 4; + goto finish; + } else if (r5 && data.endsWith(r5)) { + index = 5; + goto finish; + } else if (data.endsWith(GF("+CIPRCV:"))) { + mux = stream.readStringUntil(',').toInt(); + data += mux; + data += (','); + len = stream.readStringUntil(',').toInt(); + data += len; + data += (','); + gotData = true; + index = 6; + goto finish; + } else if (data.endsWith(GF("+TCPCLOSED:"))) { + mux = stream.readStringUntil(',').toInt(); + data += mux; + data += (','); + String concl = stream.readStringUntil('\n'); + data += concl; + sockets[mux]->sock_connected = false; + index = 7; + goto finish; + } + } + } while (millis() - startMillis < timeout); + finish: + if (!index) { + data.trim(); + if (data.length()) { + DBG(GSM_NL, "### Unhandled:", data); + } + } + else { + data.trim(); + data.replace(GSM_NL GSM_NL, GSM_NL); + data.replace(GSM_NL, GSM_NL " "); + if (data.length()) { + DBG(GSM_NL, "<<< ", data); + } + } + if (gotData) { + int len_orig = len; + if (len > sockets[mux]->rx.free()) { + DBG(GSM_NL, "### Buffer overflow: ", len, "->", sockets[mux]->rx.free()); + } else { + DBG(GSM_NL, "### Got: ", len, "->", sockets[mux]->rx.free()); + } + while (len--) { + TINY_GSM_YIELD(); + int r = stream.read(); + if (r <= 0) continue; // Skip 0x00 bytes, just in case + sockets[mux]->rx.put((char)r); + } + if (len_orig > sockets[mux]->available()) { + DBG(GSM_NL, "### Fewer characters received than expected: ", sockets[mux]->available(), " vs ", len_orig); + } + } + return index; + } + + uint8_t waitResponse(uint32_t timeout, + GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR), + GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL) + { + String data; + return waitResponse(timeout, data, r1, r2, r3, r4, r5); + } + + uint8_t waitResponse(GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR), + GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL) + { + return waitResponse(1000, r1, r2, r3, r4, r5); + } + private: Stream& stream; GsmClient* sockets[8]; diff --git a/TinyGsmClientESP8266.h b/TinyGsmClientESP8266.h index 8f3d790..a9f58f3 100644 --- a/TinyGsmClientESP8266.h +++ b/TinyGsmClientESP8266.h @@ -276,7 +276,70 @@ public: return false; } - /* Public Utilities */ +private: + int modemConnect(const char* host, uint16_t port, uint8_t mux) { + sendAT(GF("+CIPSTART="), mux, ',', GF("\"TCP"), GF("\",\""), host, GF("\","), port, GF(","), TCP_KEEP_ALIVE); + int rsp = waitResponse(75000L, + GFP(GSM_OK), + GFP(GSM_ERROR), + GF(GSM_NL "ALREADY CONNECT" GSM_NL)); + waitResponse(100, GF("1,CONNECT")); + return (1 == rsp); + } + + int modemSend(const void* buff, size_t len, uint8_t mux) { + sendAT(GF("+CIPSEND="), mux, ',', len); + if (waitResponse(GF(">")) != 1) { + return -1; + } + stream.write((uint8_t*)buff, len); + if (waitResponse(GF(GSM_NL "SEND OK" GSM_NL)) != 1) { + return -1; + } + return len; + } + + bool modemGetConnected(uint8_t mux) { + sendAT(GF("+CIPSTATUS="), mux); + int res = waitResponse(GF(",\"CONNECTED\""), GF(",\"CLOSED\""), GF(",\"CLOSING\""), GF(",\"INITIAL\"")); + waitResponse(); + return 1 == res; + } + + /* Private Utilities */ + template + void streamWrite(T last) { + stream.print(last); + } + + template + void streamWrite(T head, Args... tail) { + stream.print(head); + streamWrite(tail...); + } + + int streamRead() { return stream.read(); } + + String streamReadUntil(char c) { + String return_string = stream.readStringUntil(c); + return_string.trim(); + if (String(c) == GSM_NL || String(c) == "\n"){ + DBG(return_string, c, " "); + } else DBG(return_string, c); + return return_string; + } + + bool streamSkipUntil(char c) { + String skipped = stream.readStringUntil(c); + skipped.trim(); + if (skipped.length()) { + if (String(c) == GSM_NL || String(c) == "\n"){ + DBG(skipped, c, " "); + } else DBG(skipped, c); + return true; + } else return false; + } + template void sendAT(Args... cmd) { streamWrite("AT", cmd..., GSM_NL); @@ -389,70 +452,6 @@ public: return waitResponse(1000, r1, r2, r3, r4, r5); } -private: - int modemConnect(const char* host, uint16_t port, uint8_t mux) { - sendAT(GF("+CIPSTART="), mux, ',', GF("\"TCP"), GF("\",\""), host, GF("\","), port, GF(","), TCP_KEEP_ALIVE); - int rsp = waitResponse(75000L, - GFP(GSM_OK), - GFP(GSM_ERROR), - GF(GSM_NL "ALREADY CONNECT" GSM_NL)); - waitResponse(100, GF("1,CONNECT")); - return (1 == rsp); - } - - int modemSend(const void* buff, size_t len, uint8_t mux) { - sendAT(GF("+CIPSEND="), mux, ',', len); - if (waitResponse(GF(">")) != 1) { - return -1; - } - stream.write((uint8_t*)buff, len); - if (waitResponse(GF(GSM_NL "SEND OK" GSM_NL)) != 1) { - return -1; - } - return len; - } - - bool modemGetConnected(uint8_t mux) { - sendAT(GF("+CIPSTATUS="), mux); - int res = waitResponse(GF(",\"CONNECTED\""), GF(",\"CLOSED\""), GF(",\"CLOSING\""), GF(",\"INITIAL\"")); - waitResponse(); - return 1 == res; - } - - /* Private Utilities */ - template - void streamWrite(T last) { - stream.print(last); - } - - template - void streamWrite(T head, Args... tail) { - stream.print(head); - streamWrite(tail...); - } - - int streamRead() { return stream.read(); } - - String streamReadUntil(char c) { - String return_string = stream.readStringUntil(c); - return_string.trim(); - if (String(c) == GSM_NL || String(c) == "\n"){ - DBG(return_string, c, " "); - } else DBG(return_string, c); - return return_string; - } - - bool streamSkipUntil(char c) { - String skipped = stream.readStringUntil(c); - skipped.trim(); - if (skipped.length()) { - if (String(c) == GSM_NL || String(c) == "\n"){ - DBG(skipped, c, " "); - } else DBG(skipped, c); - return true; - } else return false; - } - private: Stream& stream; GsmClient* sockets[5]; diff --git a/TinyGsmClientM590.h b/TinyGsmClientM590.h index 9ddc6e1..3d53d0a 100644 --- a/TinyGsmClientM590.h +++ b/TinyGsmClientM590.h @@ -427,7 +427,94 @@ public: * Battery functions */ - /* Public Utilities */ +private: + String dnsIpQuery(const char* host) { + sendAT(GF("+DNS=\""), host, GF("\"")); + if (waitResponse(10000L, GF(GSM_NL "+DNS:")) != 1) { + return ""; + } + String res = streamReadUntil('\n'); + waitResponse(GF("+DNS:OK" GSM_NL)); + res.trim(); + return res; + } + + int modemConnect(const char* host, uint16_t port, uint8_t mux) { + for (int i=0; i<3; i++) { + String ip = dnsIpQuery(host); + + sendAT(GF("+TCPSETUP="), mux, GF(","), ip, GF(","), port); + int rsp = waitResponse(75000L, + GF(",OK" GSM_NL), + GF(",FAIL" GSM_NL), + GF("+TCPSETUP:Error" GSM_NL)); + if (1 == rsp) { + return true; + } else if (3 == rsp) { + sendAT(GF("+TCPCLOSE="), mux); + waitResponse(); + } + delay(1000); + } + return false; + } + + int modemSend(const void* buff, size_t len, uint8_t mux) { + sendAT(GF("+TCPSEND="), mux, ',', len); + if (waitResponse(GF(">")) != 1) { + return 0; + } + stream.write((uint8_t*)buff, len); + stream.write((char)0x0D); + + if (waitResponse(30000L, GF(GSM_NL "+TCPSEND:")) != 1) { + return 0; + } + streamReadUntil('\n'); + return len; + } + + bool modemGetConnected(uint8_t mux) { + sendAT(GF("+CIPSTATUS="), mux); + int res = waitResponse(GF(",\"CONNECTED\""), GF(",\"CLOSED\""), GF(",\"CLOSING\""), GF(",\"INITIAL\"")); + waitResponse(); + return 1 == res; + } + + /* Private Utilities */ + template + void streamWrite(T last) { + stream.print(last); + } + + template + void streamWrite(T head, Args... tail) { + stream.print(head); + streamWrite(tail...); + } + + int streamRead() { return stream.read(); } + + String streamReadUntil(char c) { + String return_string = stream.readStringUntil(c); + return_string.trim(); + if (String(c) == GSM_NL || String(c) == "\n"){ + DBG(return_string, c, " "); + } else DBG(return_string, c); + return return_string; + } + + bool streamSkipUntil(char c) { + String skipped = stream.readStringUntil(c); + skipped.trim(); + if (skipped.length()) { + if (String(c) == GSM_NL || String(c) == "\n"){ + DBG(skipped, c, " "); + } else DBG(skipped, c); + return true; + } else return false; + } + template void sendAT(Args... cmd) { streamWrite("AT", cmd..., GSM_NL); @@ -545,94 +632,6 @@ public: return waitResponse(1000, r1, r2, r3, r4, r5); } -private: - String dnsIpQuery(const char* host) { - sendAT(GF("+DNS=\""), host, GF("\"")); - if (waitResponse(10000L, GF(GSM_NL "+DNS:")) != 1) { - return ""; - } - String res = streamReadUntil('\n'); - waitResponse(GF("+DNS:OK" GSM_NL)); - res.trim(); - return res; - } - - int modemConnect(const char* host, uint16_t port, uint8_t mux) { - for (int i=0; i<3; i++) { - String ip = dnsIpQuery(host); - - sendAT(GF("+TCPSETUP="), mux, GF(","), ip, GF(","), port); - int rsp = waitResponse(75000L, - GF(",OK" GSM_NL), - GF(",FAIL" GSM_NL), - GF("+TCPSETUP:Error" GSM_NL)); - if (1 == rsp) { - return true; - } else if (3 == rsp) { - sendAT(GF("+TCPCLOSE="), mux); - waitResponse(); - } - delay(1000); - } - return false; - } - - int modemSend(const void* buff, size_t len, uint8_t mux) { - sendAT(GF("+TCPSEND="), mux, ',', len); - if (waitResponse(GF(">")) != 1) { - return 0; - } - stream.write((uint8_t*)buff, len); - stream.write((char)0x0D); - - if (waitResponse(30000L, GF(GSM_NL "+TCPSEND:")) != 1) { - return 0; - } - streamReadUntil('\n'); - return len; - } - - bool modemGetConnected(uint8_t mux) { - sendAT(GF("+CIPSTATUS="), mux); - int res = waitResponse(GF(",\"CONNECTED\""), GF(",\"CLOSED\""), GF(",\"CLOSING\""), GF(",\"INITIAL\"")); - waitResponse(); - return 1 == res; - } - - /* Private Utilities */ - template - void streamWrite(T last) { - stream.print(last); - } - - template - void streamWrite(T head, Args... tail) { - stream.print(head); - streamWrite(tail...); - } - - int streamRead() { return stream.read(); } - - String streamReadUntil(char c) { - String return_string = stream.readStringUntil(c); - return_string.trim(); - if (String(c) == GSM_NL || String(c) == "\n"){ - DBG(return_string, c, " "); - } else DBG(return_string, c); - return return_string; - } - - bool streamSkipUntil(char c) { - String skipped = stream.readStringUntil(c); - skipped.trim(); - if (skipped.length()) { - if (String(c) == GSM_NL || String(c) == "\n"){ - DBG(skipped, c, " "); - } else DBG(skipped, c); - return true; - } else return false; - } - private: Stream& stream; GsmClient* sockets[2]; diff --git a/TinyGsmClientSIM800.h b/TinyGsmClientSIM800.h index 97d54fa..840a00c 100644 --- a/TinyGsmClientSIM800.h +++ b/TinyGsmClientSIM800.h @@ -534,107 +534,6 @@ public: return res; } - /* Public Utilities */ - template - void sendAT(Args... cmd) { - streamWrite("AT", cmd..., GSM_NL); - stream.flush(); - TINY_GSM_YIELD(); - DBG(GSM_NL, ">>> AT:", cmd...); - } - - // TODO: Optimize this! - uint8_t waitResponse(uint32_t timeout, String& data, - GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR), - GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL) - { - /*String r1s(r1); r1s.trim(); - String r2s(r2); r2s.trim(); - String r3s(r3); r3s.trim(); - String r4s(r4); r4s.trim(); - String r5s(r5); r5s.trim(); - DBG("### ..:", r1s, ",", r2s, ",", r3s, ",", r4s, ",", r5s);*/ - data.reserve(64); - bool gotData = false; - int mux = -1; - int index = 0; - unsigned long startMillis = millis(); - do { - TINY_GSM_YIELD(); - while (stream.available() > 0) { - int a = streamRead(); - if (a <= 0) continue; // Skip 0x00 bytes, just in case - data += (char)a; - if (r1 && data.endsWith(r1)) { - index = 1; - goto finish; - } else if (r2 && data.endsWith(r2)) { - index = 2; - goto finish; - } else if (r3 && data.endsWith(r3)) { - index = 3; - goto finish; - } else if (r4 && data.endsWith(r4)) { - index = 4; - goto finish; - } else if (r5 && data.endsWith(r5)) { - index = 5; - goto finish; - } else if (data.endsWith(GF(GSM_NL "+CIPRXGET:"))) { - index = 6; - String mode = streamReadUntil(','); - if (mode.toInt() == 1) { - mux = streamReadUntil('\n').toInt(); - gotData = true; - } else { - data += mode; - } - } else if (data.endsWith(GF("CLOSED" GSM_NL))) { - index = 7; - int nl = data.lastIndexOf(GSM_NL, data.length()-8); - int coma = data.indexOf(',', nl+2); - mux = data.substring(nl+2, coma).toInt(); - if (mux) { - sockets[mux]->sock_connected = false; - } - } - } - } while (millis() - startMillis < timeout); - finish: - if (!index) { - data.trim(); - if (data.length()) { - DBG(GSM_NL, "### Unhandled:", data); - } - } - else { - data.trim(); - data.replace(GSM_NL GSM_NL, GSM_NL); - data.replace(GSM_NL, GSM_NL " "); - if (data.length()) { - DBG(GSM_NL, "<<< ", data); - } - } - if (gotData) { - sockets[mux]->sock_available = modemGetAvailable(mux); - } - return index; - } - - uint8_t waitResponse(uint32_t timeout, - GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR), - GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL) - { - String data; - return waitResponse(timeout, data, r1, r2, r3, r4, r5); - } - - uint8_t waitResponse(GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR), - GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL) - { - return waitResponse(1000, r1, r2, r3, r4, r5); - } - private: int modemConnect(const char* host, uint16_t port, uint8_t mux) { sendAT(GF("+CIPSTART="), mux, ',', GF("\"TCP"), GF("\",\""), host, GF("\","), port); @@ -751,6 +650,106 @@ private: } else return false; } + template + void sendAT(Args... cmd) { + streamWrite("AT", cmd..., GSM_NL); + stream.flush(); + TINY_GSM_YIELD(); + DBG(GSM_NL, ">>> AT:", cmd...); + } + + // TODO: Optimize this! + uint8_t waitResponse(uint32_t timeout, String& data, + GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR), + GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL) + { + /*String r1s(r1); r1s.trim(); + String r2s(r2); r2s.trim(); + String r3s(r3); r3s.trim(); + String r4s(r4); r4s.trim(); + String r5s(r5); r5s.trim(); + DBG("### ..:", r1s, ",", r2s, ",", r3s, ",", r4s, ",", r5s);*/ + data.reserve(64); + bool gotData = false; + int mux = -1; + int index = 0; + unsigned long startMillis = millis(); + do { + TINY_GSM_YIELD(); + while (stream.available() > 0) { + int a = streamRead(); + if (a <= 0) continue; // Skip 0x00 bytes, just in case + data += (char)a; + if (r1 && data.endsWith(r1)) { + index = 1; + goto finish; + } else if (r2 && data.endsWith(r2)) { + index = 2; + goto finish; + } else if (r3 && data.endsWith(r3)) { + index = 3; + goto finish; + } else if (r4 && data.endsWith(r4)) { + index = 4; + goto finish; + } else if (r5 && data.endsWith(r5)) { + index = 5; + goto finish; + } else if (data.endsWith(GF(GSM_NL "+CIPRXGET:"))) { + index = 6; + String mode = streamReadUntil(','); + if (mode.toInt() == 1) { + mux = streamReadUntil('\n').toInt(); + gotData = true; + } else { + data += mode; + } + } else if (data.endsWith(GF("CLOSED" GSM_NL))) { + index = 7; + int nl = data.lastIndexOf(GSM_NL, data.length()-8); + int coma = data.indexOf(',', nl+2); + mux = data.substring(nl+2, coma).toInt(); + if (mux) { + sockets[mux]->sock_connected = false; + } + } + } + } while (millis() - startMillis < timeout); + finish: + if (!index) { + data.trim(); + if (data.length()) { + DBG(GSM_NL, "### Unhandled:", data); + } + } + else { + data.trim(); + data.replace(GSM_NL GSM_NL, GSM_NL); + data.replace(GSM_NL, GSM_NL " "); + if (data.length()) { + DBG(GSM_NL, "<<< ", data); + } + } + if (gotData) { + sockets[mux]->sock_available = modemGetAvailable(mux); + } + return index; + } + + uint8_t waitResponse(uint32_t timeout, + GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR), + GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL) + { + String data; + return waitResponse(timeout, data, r1, r2, r3, r4, r5); + } + + uint8_t waitResponse(GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR), + GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL) + { + return waitResponse(1000, r1, r2, r3, r4, r5); + } + private: Stream& stream; GsmClient* sockets[5]; diff --git a/TinyGsmClientXBee.h b/TinyGsmClientXBee.h index aa083e1..91d0347 100644 --- a/TinyGsmClientXBee.h +++ b/TinyGsmClientXBee.h @@ -405,86 +405,6 @@ public: return true; } - /* Public Utilities */ - template - void sendAT(Args... cmd) { - streamWrite("AT", cmd..., GSM_NL); - stream.flush(); - TINY_GSM_YIELD(); - DBG(">>> AT ", cmd..., "\r\n"); - } - - // TODO: Optimize this! - uint8_t waitResponse(uint32_t timeout, String& data, - GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR), - GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL) - { - /*String r1s(r1); r1s.trim(); - String r2s(r2); r2s.trim(); - String r3s(r3); r3s.trim(); - String r4s(r4); r4s.trim(); - String r5s(r5); r5s.trim(); - DBG("### ..:", r1s, ",", r2s, ",", r3s, ",", r4s, ",", r5s);*/ - data.reserve(64); - int index = 0; - unsigned long startMillis = millis(); - do { - TINY_GSM_YIELD(); - while (stream.available() > 0) { - int a = streamRead(); - if (a <= 0) continue; // Skip 0x00 bytes, just in case - data += (char)a; - if (r1 && data.endsWith(r1)) { - index = 1; - goto finish; - } else if (r2 && data.endsWith(r2)) { - index = 2; - goto finish; - } else if (r3 && data.endsWith(r3)) { - index = 3; - goto finish; - } else if (r4 && data.endsWith(r4)) { - index = 4; - goto finish; - } else if (r5 && data.endsWith(r5)) { - index = 5; - goto finish; - } - } - } while (millis() - startMillis < timeout); - finish: - if (!index) { - data.trim(); - data.replace(GSM_NL GSM_NL, GSM_NL); - data.replace(GSM_NL, "\r\n" " "); - if (data.length()) { - DBG("### Unhandled:", data, "\r\n"); - } else DBG("### NO RESPONSE!\r\n"); - } - else { - data.trim(); - data.replace(GSM_NL GSM_NL, GSM_NL); - data.replace(GSM_NL, "\r\n "); - if (data.length()) { - DBG("<<< ", data, "\r\n"); - } - } - return index; - } - - uint8_t waitResponse(uint32_t timeout, - GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR), - GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL) - { - String data; - return waitResponse(timeout, data, r1, r2, r3, r4, r5); - } - - uint8_t waitResponse(GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR), - GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL) - { - return waitResponse(1000, r1, r2, r3, r4, r5); - } private: int modemConnect(const char* host, uint16_t port, uint8_t mux = 1) { @@ -580,6 +500,86 @@ private: waitResponse(); } + template + void sendAT(Args... cmd) { + streamWrite("AT", cmd..., GSM_NL); + stream.flush(); + TINY_GSM_YIELD(); + DBG(">>> AT ", cmd..., "\r\n"); + } + + // TODO: Optimize this! + uint8_t waitResponse(uint32_t timeout, String& data, + GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR), + GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL) + { + /*String r1s(r1); r1s.trim(); + String r2s(r2); r2s.trim(); + String r3s(r3); r3s.trim(); + String r4s(r4); r4s.trim(); + String r5s(r5); r5s.trim(); + DBG("### ..:", r1s, ",", r2s, ",", r3s, ",", r4s, ",", r5s);*/ + data.reserve(64); + int index = 0; + unsigned long startMillis = millis(); + do { + TINY_GSM_YIELD(); + while (stream.available() > 0) { + int a = streamRead(); + if (a <= 0) continue; // Skip 0x00 bytes, just in case + data += (char)a; + if (r1 && data.endsWith(r1)) { + index = 1; + goto finish; + } else if (r2 && data.endsWith(r2)) { + index = 2; + goto finish; + } else if (r3 && data.endsWith(r3)) { + index = 3; + goto finish; + } else if (r4 && data.endsWith(r4)) { + index = 4; + goto finish; + } else if (r5 && data.endsWith(r5)) { + index = 5; + goto finish; + } + } + } while (millis() - startMillis < timeout); + finish: + if (!index) { + data.trim(); + data.replace(GSM_NL GSM_NL, GSM_NL); + data.replace(GSM_NL, "\r\n" " "); + if (data.length()) { + DBG("### Unhandled:", data, "\r\n"); + } else DBG("### NO RESPONSE!\r\n"); + } + else { + data.trim(); + data.replace(GSM_NL GSM_NL, GSM_NL); + data.replace(GSM_NL, "\r\n "); + if (data.length()) { + DBG("<<< ", data, "\r\n"); + } + } + return index; + } + + uint8_t waitResponse(uint32_t timeout, + GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR), + GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL) + { + String data; + return waitResponse(timeout, data, r1, r2, r3, r4, r5); + } + + uint8_t waitResponse(GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR), + GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL) + { + return waitResponse(1000, r1, r2, r3, r4, r5); + } + private: int guardTime; XBeeType beeType;