diff --git a/src/TinyGsmClientA6.h b/src/TinyGsmClientA6.h index 080af74..9af1038 100644 --- a/src/TinyGsmClientA6.h +++ b/src/TinyGsmClientA6.h @@ -150,22 +150,11 @@ public: return "AI-Thinker A6"; } - void setBaud(unsigned long baud) { - sendAT(GF("+IPR="), baud); - } +TINY_GSM_MODEM_SET_BAUD_IPR() - bool testAT(unsigned long timeout = 10000L) { - for (unsigned long start = millis(); millis() - start < timeout; ) { - sendAT(GF("")); - if (waitResponse(200) == 1) return true; - delay(100); - } - return false; - } +TINY_GSM_MODEM_TEST_AT() - void maintain() { - waitResponse(10, NULL, NULL); - } +TINY_GSM_MODEM_MAINTAIN_LISTEN() bool factoryDefault() { sendAT(GF("&FZE0&W")); // Factory + Reset + Echo Off + Write @@ -174,17 +163,7 @@ public: return waitResponse() == 1; } - String getModemInfo() { - sendAT(GF("I")); - String res; - if (waitResponse(1000L, res) != 1) { - return ""; - } - res.replace(GSM_NL "OK" GSM_NL, ""); - res.replace(GSM_NL, " "); - res.trim(); - return res; - } +TINY_GSM_MODEM_GET_INFO_ATI() bool hasSSL() { return false; @@ -224,10 +203,7 @@ public: * SIM card functions */ - bool simUnlock(const char *pin) { - sendAT(GF("+CPIN=\""), pin, GF("\"")); - return waitResponse() == 1; - } +TINY_GSM_MODEM_SIM_UNLOCK_CPIN() String getSimCCID() { sendAT(GF("+CCID")); @@ -240,16 +216,7 @@ public: return res; } - String getIMEI() { - sendAT(GF("+GSN")); - if (waitResponse(GF(GSM_NL)) != 1) { - return ""; - } - String res = stream.readStringUntil('\n'); - waitResponse(); - res.trim(); - return res; - } +TINY_GSM_MODEM_GET_IMEI_GSN() SimStatus getSimStatus(unsigned long timeout = 10000L) { for (unsigned long start = millis(); millis() - start < timeout; ) { @@ -270,16 +237,7 @@ public: return SIM_ERROR; } - RegStatus getRegistrationStatus() { - sendAT(GF("+CREG?")); - if (waitResponse(GF(GSM_NL "+CREG:")) != 1) { - return REG_UNKNOWN; - } - streamSkipUntil(','); // Skip format (0) - int status = stream.readStringUntil('\n').toInt(); - waitResponse(); - return (RegStatus)status; - } +TINY_GSM_MODEM_GET_REGISTRATION_XREG(CREG) String getOperator() { sendAT(GF("+COPS=3,0")); // Set format @@ -299,30 +257,14 @@ public: * Generic network functions */ - int16_t getSignalQuality() { - sendAT(GF("+CSQ")); - if (waitResponse(GF(GSM_NL "+CSQ:")) != 1) { - return 99; - } - int res = stream.readStringUntil(',').toInt(); - waitResponse(); - return res; - } +TINY_GSP_MODEM_GET_CSQ() bool isNetworkConnected() { RegStatus s = getRegistrationStatus(); return (s == REG_OK_HOME || s == REG_OK_ROAMING); } - bool waitForNetwork(unsigned long timeout = 60000L) { - for (unsigned long start = millis(); millis() - start < timeout; ) { - if (isNetworkConnected()) { - return true; - } - delay(250); - } - return false; - } +TINY_GSM_MODEM_WAIT_FOR_NETWORK() /* * GPRS functions @@ -603,37 +545,7 @@ public: Utilities */ - template - void streamWrite(T last) { - stream.print(last); - } - - template - void streamWrite(T head, Args... tail) { - stream.print(head); - streamWrite(tail...); - } - - template - void sendAT(Args... cmd) { - streamWrite("AT", cmd..., GSM_NL); - stream.flush(); - TINY_GSM_YIELD(); - //DBG("### AT:", cmd...); - } - - bool streamSkipUntil(const char c, const unsigned long timeout = 1000L) { - unsigned long startMillis = millis(); - while (millis() - startMillis < timeout) { - while (millis() - startMillis < timeout && !stream.available()) { - TINY_GSM_YIELD(); - } - if (stream.read() == c) { - return true; - } - } - return false; - } +TINY_GSP_MODEM_STREAM_UTILITIES() // TODO: Optimize this! uint8_t waitResponse(uint32_t timeout, String& data, diff --git a/src/TinyGsmClientBG96.h b/src/TinyGsmClientBG96.h index 3d6c323..5907dea 100644 --- a/src/TinyGsmClientBG96.h +++ b/src/TinyGsmClientBG96.h @@ -174,31 +174,11 @@ public: return "Quectel BG96"; } - void setBaud(unsigned long baud) { - sendAT(GF("+IPR="), baud); - } +TINY_GSM_MODEM_SET_BAUD_IPR() - bool testAT(unsigned long timeout = 10000L) { - for (unsigned long start = millis(); millis() - start < timeout; ) { - sendAT(GF("")); - if (waitResponse(200) == 1) return true; - delay(100); - } - return false; - } +TINY_GSM_MODEM_TEST_AT() - void maintain() { - for (int mux = 0; mux < TINY_GSM_MUX_COUNT; mux++) { - GsmClient* sock = sockets[mux]; - if (sock && sock->got_data) { - sock->got_data = false; - sock->sock_available = modemGetAvailable(mux); - } - } - while (stream.available()) { - waitResponse(10, NULL, NULL); - } - } +TINY_GSM_MODEM_MAINTAIN_CHECK_SOCKS() bool factoryDefault() { sendAT(GF("&FZE0&W")); // Factory + Reset + Echo Off + Write @@ -209,17 +189,7 @@ public: return waitResponse() == 1; } - String getModemInfo() { - sendAT(GF("I")); - String res; - if (waitResponse(1000L, res) != 1) { - return ""; - } - res.replace(GSM_NL "OK" GSM_NL, ""); - res.replace(GSM_NL, " "); - res.trim(); - return res; - } +TINY_GSM_MODEM_GET_INFO_ATI() bool hasSSL() { return false; // TODO: For now @@ -268,14 +238,11 @@ public: * SIM card functions */ - bool simUnlock(const char *pin) { - sendAT(GF("+CPIN=\""), pin, GF("\"")); - return waitResponse() == 1; - } +TINY_GSM_MODEM_SIM_UNLOCK_CPIN() String getSimCCID() { - sendAT(GF("+ICCID")); - if (waitResponse(GF(GSM_NL "+ICCID:")) != 1) { + sendAT(GF("+QCCID")); + if (waitResponse(GF(GSM_NL "+QCCID:")) != 1) { return ""; } String res = stream.readStringUntil('\n'); @@ -284,16 +251,7 @@ public: return res; } - String getIMEI() { - sendAT(GF("+GSN")); - if (waitResponse(GF(GSM_NL)) != 1) { - return ""; - } - String res = stream.readStringUntil('\n'); - waitResponse(); - res.trim(); - return res; - } +TINY_GSM_MODEM_GET_IMEI_GSN() SimStatus getSimStatus(unsigned long timeout = 10000L) { for (unsigned long start = millis(); millis() - start < timeout; ) { @@ -314,56 +272,22 @@ public: return SIM_ERROR; } - RegStatus getRegistrationStatus() { - sendAT(GF("+CREG?")); - if (waitResponse(GF(GSM_NL "+CREG:")) != 1) { - return REG_UNKNOWN; - } - streamSkipUntil(','); // Skip format (0) - int status = stream.readStringUntil('\n').toInt(); - waitResponse(); - return (RegStatus)status; - } +TINY_GSM_MODEM_GET_REGISTRATION_XREG(CREG) - String getOperator() { - sendAT(GF("+COPS?")); - if (waitResponse(GF(GSM_NL "+COPS:")) != 1) { - return ""; - } - streamSkipUntil('"'); // Skip mode and format - String res = stream.readStringUntil('"'); - waitResponse(); - return res; - } +TINY_GSM_MODEM_GET_OPERATOR_COPS() /* * Generic network functions */ - int16_t getSignalQuality() { - sendAT(GF("+CSQ")); - if (waitResponse(GF(GSM_NL "+CSQ:")) != 1) { - return 99; - } - int res = stream.readStringUntil(',').toInt(); - waitResponse(); - return res; - } +TINY_GSP_MODEM_GET_CSQ() bool isNetworkConnected() { RegStatus s = getRegistrationStatus(); return (s == REG_OK_HOME || s == REG_OK_ROAMING); } - bool waitForNetwork(unsigned long timeout = 60000L) { - for (unsigned long start = millis(); millis() - start < timeout; ) { - if (isNetworkConnected()) { - return true; - } - delay(250); - } - return false; - } +TINY_GSM_MODEM_WAIT_FOR_NETWORK() /* * GPRS functions @@ -400,18 +324,7 @@ public: return true; } - bool isGprsConnected() { - sendAT(GF("+CGATT?")); - if (waitResponse(GF(GSM_NL "+CGATT:")) != 1) { - return false; - } - int res = stream.readStringUntil('\n').toInt(); - waitResponse(); - if (res != 1) - return false; - - return localIP() != IPAddress(0,0,0,0); - } +TINY_GSP_MODEM_GET_GPRS_IP_CONNECTED() /* * IP Address functions @@ -646,37 +559,7 @@ public: Utilities */ - template - void streamWrite(T last) { - stream.print(last); - } - - template - void streamWrite(T head, Args... tail) { - stream.print(head); - streamWrite(tail...); - } - - template - void sendAT(Args... cmd) { - streamWrite("AT", cmd..., GSM_NL); - stream.flush(); - TINY_GSM_YIELD(); - //DBG("### AT:", cmd...); - } - - bool streamSkipUntil(const char c, const unsigned long timeout = 1000L) { - unsigned long startMillis = millis(); - while (millis() - startMillis < timeout) { - while (millis() - startMillis < timeout && !stream.available()) { - TINY_GSM_YIELD(); - } - if (stream.read() == c) { - return true; - } - } - return false; - } +TINY_GSP_MODEM_STREAM_UTILITIES() // TODO: Optimize this! uint8_t waitResponse(uint32_t timeout, String& data, diff --git a/src/TinyGsmClientESP8266.h b/src/TinyGsmClientESP8266.h index 5d14ed6..9fb0866 100644 --- a/src/TinyGsmClientESP8266.h +++ b/src/TinyGsmClientESP8266.h @@ -173,18 +173,9 @@ public: sendAT(GF("+UART_CUR="), baud, "8,1,0,0"); } - bool testAT(unsigned long timeout = 10000L) { - for (unsigned long start = millis(); millis() - start < timeout; ) { - sendAT(GF("")); - if (waitResponse(200) == 1) return true; - delay(100); - } - return false; - } +TINY_GSM_MODEM_TEST_AT() - void maintain() { - waitResponse(10, NULL, NULL); - } +TINY_GSM_MODEM_MAINTAIN_LISTEN() bool factoryDefault() { sendAT(GF("+RESTORE")); @@ -378,37 +369,7 @@ public: Utilities */ - template - void streamWrite(T last) { - stream.print(last); - } - - template - void streamWrite(T head, Args... tail) { - stream.print(head); - streamWrite(tail...); - } - - template - void sendAT(Args... cmd) { - streamWrite("AT", cmd..., GSM_NL); - stream.flush(); - TINY_GSM_YIELD(); - //DBG("### AT:", cmd...); - } - - bool streamSkipUntil(const char c, const unsigned long timeout = 1000L) { - unsigned long startMillis = millis(); - while (millis() - startMillis < timeout) { - while (millis() - startMillis < timeout && !stream.available()) { - TINY_GSM_YIELD(); - } - if (stream.read() == c) { - return true; - } - } - return false; - } +TINY_GSP_MODEM_STREAM_UTILITIES() // TODO: Optimize this! uint8_t waitResponse(uint32_t timeout, String& data, diff --git a/src/TinyGsmClientM590.h b/src/TinyGsmClientM590.h index 52a2507..e9e232a 100644 --- a/src/TinyGsmClientM590.h +++ b/src/TinyGsmClientM590.h @@ -143,24 +143,11 @@ public: return "Neoway M590"; } - void setBaud(unsigned long baud) { - sendAT(GF("+IPR="), baud); - } +TINY_GSM_MODEM_SET_BAUD_IPR() - bool testAT(unsigned long timeout = 10000L) { - for (unsigned long start = millis(); millis() - start < timeout; ) { - sendAT(GF("")); - if (waitResponse(200) == 1) return true; - delay(100); - } - return false; - } +TINY_GSM_MODEM_TEST_AT() - void maintain() { - //while (stream.available()) { - waitResponse(10, NULL, NULL); - //} - } +TINY_GSM_MODEM_MAINTAIN_LISTEN() bool factoryDefault() { sendAT(GF("&FZE0&W")); // Factory + Reset + Echo Off + Write @@ -175,17 +162,7 @@ public: return waitResponse() == 1; } - String getModemInfo() { - sendAT(GF("I")); - String res; - if (waitResponse(1000L, res) != 1) { - return ""; - } - res.replace(GSM_NL "OK" GSM_NL, ""); - res.replace(GSM_NL, " "); - res.trim(); - return res; - } +TINY_GSM_MODEM_GET_INFO_ATI() bool hasSSL() { return false; @@ -232,32 +209,11 @@ public: * SIM card functions */ - bool simUnlock(const char *pin) { - sendAT(GF("+CPIN=\""), pin, GF("\"")); - return waitResponse() == 1; - } +TINY_GSM_MODEM_SIM_UNLOCK_CPIN() - String getSimCCID() { - sendAT(GF("+CCID")); - if (waitResponse(GF(GSM_NL "+CCID:")) != 1) { - return ""; - } - String res = stream.readStringUntil('\n'); - waitResponse(); - res.trim(); - return res; - } +TINY_GSM_MODEM_GET_SIMCCID_CCID() - String getIMEI() { - sendAT(GF("+GSN")); - if (waitResponse(GF(GSM_NL)) != 1) { - return ""; - } - String res = stream.readStringUntil('\n'); - waitResponse(); - res.trim(); - return res; - } +TINY_GSM_MODEM_GET_IMEI_GSN() SimStatus getSimStatus(unsigned long timeout = 10000L) { for (unsigned long start = millis(); millis() - start < timeout; ) { @@ -278,56 +234,22 @@ public: return SIM_ERROR; } - RegStatus getRegistrationStatus() { - sendAT(GF("+CREG?")); - if (waitResponse(GF(GSM_NL "+CREG:")) != 1) { - return REG_UNKNOWN; - } - streamSkipUntil(','); // Skip format (0) - int status = stream.readStringUntil('\n').toInt(); - waitResponse(); - return (RegStatus)status; - } +TINY_GSM_MODEM_GET_REGISTRATION_XREG(CREG) - String getOperator() { - sendAT(GF("+COPS?")); - if (waitResponse(GF(GSM_NL "+COPS:")) != 1) { - return ""; - } - streamSkipUntil('"'); // Skip mode and format - String res = stream.readStringUntil('"'); - waitResponse(); - return res; - } +TINY_GSM_MODEM_GET_OPERATOR_COPS() /* * Generic network functions */ - int16_t getSignalQuality() { - sendAT(GF("+CSQ")); - if (waitResponse(GF(GSM_NL "+CSQ:")) != 1) { - return 99; - } - int res = stream.readStringUntil(',').toInt(); - waitResponse(); - return res; - } +TINY_GSP_MODEM_GET_CSQ() bool isNetworkConnected() { RegStatus s = getRegistrationStatus(); return (s == REG_OK_HOME || s == REG_OK_ROAMING); } - bool waitForNetwork(unsigned long timeout = 60000L) { - for (unsigned long start = millis(); millis() - start < timeout; ) { - if (isNetworkConnected()) { - return true; - } - delay(250); - } - return false; - } +TINY_GSM_MODEM_WAIT_FOR_NETWORK() /* * GPRS functions @@ -545,37 +467,7 @@ public: Utilities */ - template - void streamWrite(T last) { - stream.print(last); - } - - template - void streamWrite(T head, Args... tail) { - stream.print(head); - streamWrite(tail...); - } - - template - void sendAT(Args... cmd) { - streamWrite("AT", cmd..., GSM_NL); - stream.flush(); - TINY_GSM_YIELD(); - //DBG("### AT:", cmd...); - } - - bool streamSkipUntil(const char c, const unsigned long timeout = 1000L) { - unsigned long startMillis = millis(); - while (millis() - startMillis < timeout) { - while (millis() - startMillis < timeout && !stream.available()) { - TINY_GSM_YIELD(); - } - if (stream.read() == c) { - return true; - } - } - return false; - } +TINY_GSP_MODEM_STREAM_UTILITIES() // TODO: Optimize this! uint8_t waitResponse(uint32_t timeout, String& data, diff --git a/src/TinyGsmClientM95.h b/src/TinyGsmClientM95.h index 03225cc..d5e6431 100644 --- a/src/TinyGsmClientM95.h +++ b/src/TinyGsmClientM95.h @@ -178,31 +178,11 @@ public: return "Quectel M95"; } - void setBaud(unsigned long baud) { - sendAT(GF("+IPR="), baud); - } +TINY_GSM_MODEM_SET_BAUD_IPR() - bool testAT(unsigned long timeout = 10000L) { - for (unsigned long start = millis(); millis() - start < timeout; ) { - sendAT(GF("")); - if (waitResponse(200) == 1) return true; - delay(100); - } - return false; - } +TINY_GSM_MODEM_TEST_AT() - void maintain() { - for (int mux = 0; mux < TINY_GSM_MUX_COUNT; mux++) { - GsmClient* sock = sockets[mux]; - if (sock && sock->got_data) { - sock->got_data = false; - sock->sock_available = modemGetAvailable(mux); - } - } - while (stream.available()) { - waitResponse(10, NULL, NULL); - } - } +TINY_GSM_MODEM_MAINTAIN_CHECK_SOCKS() bool factoryDefault() { sendAT(GF("&FZE0&W")); // Factory + Reset + Echo Off + Write @@ -213,17 +193,7 @@ public: return waitResponse() == 1; } - String getModemInfo() { - sendAT(GF("I")); - String res; - if (waitResponse(1000L, res) != 1) { - return ""; - } - res.replace(GSM_NL "OK" GSM_NL, ""); - res.replace(GSM_NL, " "); - res.trim(); - return res; - } +TINY_GSM_MODEM_GET_INFO_ATI() bool hasSSL() { return false; // TODO: For now @@ -276,14 +246,11 @@ public: * SIM card functions */ - bool simUnlock(const char *pin) { - sendAT(GF("+CPIN=\""), pin, GF("\"")); - return waitResponse() == 1; - } +TINY_GSM_MODEM_SIM_UNLOCK_CPIN() String getSimCCID() { - sendAT(GF("+ICCID")); - if (waitResponse(GF(GSM_NL "+ICCID:")) != 1) { + sendAT(GF("+QCCID")); + if (waitResponse(GF(GSM_NL "+QCCID:")) != 1) { return ""; } String res = stream.readStringUntil('\n'); @@ -292,16 +259,7 @@ public: return res; } - String getIMEI() { - sendAT(GF("+GSN")); - if (waitResponse(GF(GSM_NL)) != 1) { - return ""; - } - String res = stream.readStringUntil('\n'); - waitResponse(); - res.trim(); - return res; - } +TINY_GSM_MODEM_GET_IMEI_GSN() SimStatus getSimStatus(unsigned long timeout = 10000L) { for (unsigned long start = millis(); millis() - start < timeout; ) { @@ -322,41 +280,15 @@ public: return SIM_ERROR; } - RegStatus getRegistrationStatus() { - sendAT(GF("+CREG?")); - if (waitResponse(GF(GSM_NL "+CREG:")) != 1) { - return REG_UNKNOWN; - } - streamSkipUntil(','); // Skip format (0) - int status = stream.readStringUntil('\n').toInt(); - waitResponse(); - return (RegStatus)status; - } +TINY_GSM_MODEM_GET_REGISTRATION_XREG(CREG) - String getOperator() { - sendAT(GF("+COPS?")); - if (waitResponse(GF(GSM_NL "+COPS:")) != 1) { - return ""; - } - streamSkipUntil('"'); // Skip mode and format - String res = stream.readStringUntil('"'); - waitResponse(); - return res; - } +TINY_GSM_MODEM_GET_OPERATOR_COPS() /* * Generic network functions */ - int16_t getSignalQuality() { - sendAT(GF("+CSQ")); - if (waitResponse(GF(GSM_NL "+CSQ:")) != 1) { - return 99; - } - int res = stream.readStringUntil(',').toInt(); - waitResponse(); - return res; - } +TINY_GSP_MODEM_GET_CSQ() bool isNetworkConnected() { RegStatus s = getRegistrationStatus(); @@ -372,15 +304,7 @@ public: waitResponse(); } - bool waitForNetwork(unsigned long timeout = 60000L) { - for (unsigned long start = millis(); millis() - start < timeout; ) { - if (isNetworkConnected()) { - return true; - } - delay(250); - } - return false; - } +TINY_GSM_MODEM_WAIT_FOR_NETWORK() /* * GPRS functions @@ -438,18 +362,7 @@ public: return waitResponse(60000L, GF("DEACT OK"), GF("ERROR")) == 1; } - bool isGprsConnected() { - sendAT(GF("+CGATT?")); - if (waitResponse(GF(GSM_NL "+CGATT:")) != 1) { - return false; - } - int res = stream.readStringUntil('\n').toInt(); - waitResponse(); - if (res != 1) - return false; - - return localIP() != IPAddress(0,0,0,0); - } +TINY_GSP_MODEM_GET_GPRS_IP_CONNECTED() /* * IP Address functions @@ -701,37 +614,7 @@ public: Utilities */ - template - void streamWrite(T last) { - stream.print(last); - } - - template - void streamWrite(T head, Args... tail) { - stream.print(head); - streamWrite(tail...); - } - - template - void sendAT(Args... cmd) { - streamWrite("AT", cmd..., GSM_NL); - stream.flush(); - TINY_GSM_YIELD(); - //DBG("### AT:", cmd...); - } - - bool streamSkipUntil(const char c, const unsigned long timeout = 1000L) { - unsigned long startMillis = millis(); - while (millis() - startMillis < timeout) { - while (millis() - startMillis < timeout && !stream.available()) { - TINY_GSM_YIELD(); - } - if (stream.read() == c) { - return true; - } - } - return false; - } +TINY_GSP_MODEM_STREAM_UTILITIES() // TODO: Optimize this! uint8_t waitResponse(uint32_t timeout, String& data, diff --git a/src/TinyGsmClientMC60.h b/src/TinyGsmClientMC60.h index b0d3901..47976f2 100644 --- a/src/TinyGsmClientMC60.h +++ b/src/TinyGsmClientMC60.h @@ -185,31 +185,11 @@ public: return "Quectel MC60"; } - void setBaud(unsigned long baud) { - sendAT(GF("+IPR="), baud); - } +TINY_GSM_MODEM_SET_BAUD_IPR() - bool testAT(unsigned long timeout = 10000L) { - for (unsigned long start = millis(); millis() - start < timeout; ) { - sendAT(GF("")); - if (waitResponse(200) == 1) return true; - delay(100); - } - return false; - } +TINY_GSM_MODEM_TEST_AT() - void maintain() { - for (int mux = 0; mux < TINY_GSM_MUX_COUNT; mux++) { - GsmClient* sock = sockets[mux]; - if (sock && sock->got_data) { - sock->got_data = false; - sock->sock_available = modemGetAvailable(mux); - } - } - while (stream.available()) { - waitResponse(10, NULL, NULL); - } - } +TINY_GSM_MODEM_MAINTAIN_CHECK_SOCKS() bool factoryDefault() { sendAT(GF("&FZE0&W")); // Factory + Reset + Echo Off + Write @@ -220,17 +200,7 @@ public: return waitResponse() == 1; } - String getModemInfo() { - sendAT(GF("I")); - String res; - if (waitResponse(1000L, res) != 1) { - return ""; - } - res.replace(GSM_NL "OK" GSM_NL, ""); - res.replace(GSM_NL, " "); - res.trim(); - return res; - } +TINY_GSM_MODEM_GET_INFO_ATI() /* * under development @@ -298,32 +268,11 @@ public: * SIM card functions */ - bool simUnlock(const char *pin) { - sendAT(GF("+CPIN=\""), pin, GF("\"")); - return waitResponse() == 1; - } +TINY_GSM_MODEM_SIM_UNLOCK_CPIN() - String getSimCCID() { - sendAT(GF("+ICCID")); - if (waitResponse(GF(GSM_NL "+ICCID:")) != 1) { - return ""; - } - String res = stream.readStringUntil('\n'); - waitResponse(); - res.trim(); - return res; - } +TINY_GSM_MODEM_GET_SIMCCID_CCID() - String getIMEI() { - sendAT(GF("+GSN")); - if (waitResponse(GF(GSM_NL)) != 1) { - return ""; - } - String res = stream.readStringUntil('\n'); - waitResponse(); - res.trim(); - return res; - } +TINY_GSM_MODEM_GET_IMEI_GSN() SimStatus getSimStatus(unsigned long timeout = 10000L) { for (unsigned long start = millis(); millis() - start < timeout; ) { @@ -346,56 +295,22 @@ public: return SIM_ERROR; } - RegStatus getRegistrationStatus() { - sendAT(GF("+CREG?")); - if (waitResponse(GF(GSM_NL "+CREG:")) != 1) { - return REG_UNKNOWN; - } - streamSkipUntil(','); // Skip format (0) - int status = stream.readStringUntil('\n').toInt(); - waitResponse(); - return (RegStatus)status; - } +TINY_GSM_MODEM_GET_REGISTRATION_XREG(CREG) - String getOperator() { - sendAT(GF("+COPS?")); - if (waitResponse(GF(GSM_NL "+COPS:")) != 1) { - return ""; - } - streamSkipUntil('"'); // Skip mode and format - String res = stream.readStringUntil('"'); - waitResponse(); - return res; - } +TINY_GSM_MODEM_GET_OPERATOR_COPS() /* * Generic network functions */ - int16_t getSignalQuality() { - sendAT(GF("+CSQ")); - if (waitResponse(GF(GSM_NL "+CSQ:")) != 1) { - return 99; - } - int res = stream.readStringUntil(',').toInt(); - waitResponse(); - return res; - } +TINY_GSP_MODEM_GET_CSQ() bool isNetworkConnected() { RegStatus s = getRegistrationStatus(); return (s == REG_OK_HOME || s == REG_OK_ROAMING); } - bool waitForNetwork(unsigned long timeout = 60000L) { - for (unsigned long start = millis(); millis() - start < timeout; ) { - if (isNetworkConnected()) { - return true; - } - delay(250); - } - return false; - } +TINY_GSM_MODEM_WAIT_FOR_NETWORK() /* * GPRS functions @@ -466,18 +381,7 @@ public: return waitResponse(60000L, GF("DEACT OK"), GF("ERROR")) == 1; } - bool isGprsConnected() { - sendAT(GF("+CGATT?")); - if (waitResponse(GF(GSM_NL "+CGATT:")) != 1) { - return false; - } - int res = stream.readStringUntil('\n').toInt(); - waitResponse(); - if (res != 1) - return false; - - return localIP() != IPAddress(0,0,0,0); - } +TINY_GSP_MODEM_GET_GPRS_IP_CONNECTED() /* * IP Address functions @@ -730,37 +634,7 @@ public: Utilities */ - template - void streamWrite(T last) { - stream.print(last); - } - - template - void streamWrite(T head, Args... tail) { - stream.print(head); - streamWrite(tail...); - } - - template - void sendAT(Args... cmd) { - streamWrite("AT", cmd..., GSM_NL); - stream.flush(); - TINY_GSM_YIELD(); - //DBG("### AT:", cmd...); - } - - bool streamSkipUntil(const char c, const unsigned long timeout = 1000L) { - unsigned long startMillis = millis(); - while (millis() - startMillis < timeout) { - while (millis() - startMillis < timeout && !stream.available()) { - TINY_GSM_YIELD(); - } - if (stream.read() == c) { - return true; - } - } - return false; - } +TINY_GSP_MODEM_STREAM_UTILITIES() // TODO: Optimize this! uint8_t waitResponse(uint32_t timeout, String& data, diff --git a/src/TinyGsmClientSIM7000.h b/src/TinyGsmClientSIM7000.h index 9b7d543..8474816 100644 --- a/src/TinyGsmClientSIM7000.h +++ b/src/TinyGsmClientSIM7000.h @@ -187,48 +187,17 @@ public: return "SIMCom SIM7000"; } - void setBaud(unsigned long baud) { - sendAT(GF("+IPR="), baud); - } +TINY_GSM_MODEM_SET_BAUD_IPR() - bool testAT(unsigned long timeout = 10000L) { - //streamWrite(GF("AAAAA" GSM_NL)); // TODO: extra A's to help detect the baud rate - for (unsigned long start = millis(); millis() - start < timeout; ) { - sendAT(GF("")); - if (waitResponse(200) == 1) return true; - delay(100); - } - return false; - } +TINY_GSM_MODEM_TEST_AT() - void maintain() { - for (int mux = 0; mux < TINY_GSM_MUX_COUNT; mux++) { - GsmClient* sock = sockets[mux]; - if (sock && sock->got_data) { - sock->got_data = false; - sock->sock_available = modemGetAvailable(mux); - } - } - while (stream.available()) { - waitResponse(10, NULL, NULL); - } - } +TINY_GSM_MODEM_MAINTAIN_CHECK_SOCKS() bool factoryDefault() { // these commands aren't supported return false; } - String getModemInfo() { - sendAT(GF("I")); - String res; - if (waitResponse(1000L, res) != 1) { - return ""; - } - res.replace(GSM_NL "OK" GSM_NL, ""); - res.replace(GSM_NL, " "); - res.trim(); - return res; - } +TINY_GSM_MODEM_GET_INFO_ATI() bool hasSSL() { return false; // TODO: Module supports SSL, but not yet implemented @@ -297,32 +266,11 @@ public: * SIM card functions */ - bool simUnlock(const char *pin) { - sendAT(GF("+CPIN=\""), pin, GF("\"")); - return waitResponse() == 1; - } +TINY_GSM_MODEM_SIM_UNLOCK_CPIN() - String getSimCCID() { - sendAT(GF("+ICCID")); - if (waitResponse(GF(GSM_NL "+ICCID:")) != 1) { - return ""; - } - String res = stream.readStringUntil('\n'); - waitResponse(); - res.trim(); - return res; - } +TINY_GSM_MODEM_GET_SIMCCID_CCID() - String getIMEI() { - sendAT(GF("+GSN")); - if (waitResponse(GF(GSM_NL)) != 1) { - return ""; - } - String res = stream.readStringUntil('\n'); - waitResponse(); - res.trim(); - return res; - } +TINY_GSM_MODEM_GET_IMEI_GSN() SimStatus getSimStatus(unsigned long timeout = 10000L) { for (unsigned long start = millis(); millis() - start < timeout; ) { @@ -343,42 +291,15 @@ public: return SIM_ERROR; } - RegStatus getRegistrationStatus() { - sendAT(GF("+CGREG?")); - // TODO: Shouldn't this be CEREG for the EPS status of this 4G module? - if (waitResponse(GF(GSM_NL "+CGREG:")) != 1) { - return REG_UNKNOWN; - } - streamSkipUntil(','); // Skip format (0) - int status = stream.readStringUntil('\n').toInt(); - waitResponse(); - return (RegStatus)status; - } +TINY_GSM_MODEM_GET_REGISTRATION_XREG(CGREG) - String getOperator() { - sendAT(GF("+COPS?")); - if (waitResponse(GF(GSM_NL "+COPS:")) != 1) { - return ""; - } - streamSkipUntil('"'); // Skip mode and format - String res = stream.readStringUntil('"'); - waitResponse(); - return res; - } +TINY_GSM_MODEM_GET_OPERATOR_COPS() /* * Generic network functions */ - int16_t getSignalQuality() { - sendAT(GF("+CSQ")); - if (waitResponse(GF(GSM_NL "+CSQ:")) != 1) { - return 99; - } - int res = stream.readStringUntil(',').toInt(); - waitResponse(); - return res; - } +TINY_GSP_MODEM_GET_CSQ() bool isNetworkConnected() { RegStatus s = getRegistrationStatus(); @@ -395,15 +316,7 @@ public: return res; } - bool waitForNetwork(unsigned long timeout = 60000L) { - for (unsigned long start = millis(); millis() - start < timeout; ) { - if (isNetworkConnected()) { - return true; - } - delay(250); - } - return false; - } +TINY_GSM_MODEM_WAIT_FOR_NETWORK() String setNetworkMode(uint8_t mode) { sendAT(GF("+CNMP="), mode); @@ -944,37 +857,7 @@ public: Utilities */ - template - void streamWrite(T last) { - stream.print(last); - } - - template - void streamWrite(T head, Args... tail) { - stream.print(head); - streamWrite(tail...); - } - - template - void sendAT(Args... cmd) { - streamWrite("AT", cmd..., GSM_NL); - stream.flush(); - TINY_GSM_YIELD(); - //DBG("### AT:", cmd...); - } - - bool streamSkipUntil(const char c, const unsigned long timeout = 1000L) { - unsigned long startMillis = millis(); - while (millis() - startMillis < timeout) { - while (millis() - startMillis < timeout && !stream.available()) { - TINY_GSM_YIELD(); - } - if (stream.read() == c) { - return true; - } - } - return false; - } +TINY_GSP_MODEM_STREAM_UTILITIES() // TODO: Optimize this! uint8_t waitResponse(uint32_t timeout, String& data, diff --git a/src/TinyGsmClientSIM800.h b/src/TinyGsmClientSIM800.h index 5746db8..e417bce 100644 --- a/src/TinyGsmClientSIM800.h +++ b/src/TinyGsmClientSIM800.h @@ -195,32 +195,11 @@ public: return "SIMCom SIM800"; } - void setBaud(unsigned long baud) { - sendAT(GF("+IPR="), baud); - } +TINY_GSM_MODEM_SET_BAUD_IPR() - bool testAT(unsigned long timeout = 10000L) { - //streamWrite(GF("AAAAA" GSM_NL)); // TODO: extra A's to help detect the baud rate - for (unsigned long start = millis(); millis() - start < timeout; ) { - sendAT(GF("")); - if (waitResponse(200) == 1) return true; - delay(100); - } - return false; - } +TINY_GSM_MODEM_TEST_AT() - void maintain() { - for (int mux = 0; mux < TINY_GSM_MUX_COUNT; mux++) { - GsmClient* sock = sockets[mux]; - if (sock && sock->got_data) { - sock->got_data = false; - sock->sock_available = modemGetAvailable(mux); - } - } - while (stream.available()) { - waitResponse(10, NULL, NULL); - } - } +TINY_GSM_MODEM_MAINTAIN_CHECK_SOCKS() bool factoryDefault() { sendAT(GF("&FZE0&W")); // Factory + Reset + Echo Off + Write @@ -237,17 +216,7 @@ public: return waitResponse() == 1; } - String getModemInfo() { - sendAT(GF("I")); - String res; - if (waitResponse(1000L, res) != 1) { - return ""; - } - res.replace(GSM_NL "OK" GSM_NL, ""); - res.replace(GSM_NL, " "); - res.trim(); - return res; - } +TINY_GSM_MODEM_GET_INFO_ATI() bool hasSSL() { #if defined(TINY_GSM_MODEM_SIM900) @@ -325,32 +294,11 @@ public: * SIM card functions */ - bool simUnlock(const char *pin) { - sendAT(GF("+CPIN=\""), pin, GF("\"")); - return waitResponse() == 1; - } +TINY_GSM_MODEM_SIM_UNLOCK_CPIN() - String getSimCCID() { - sendAT(GF("+ICCID")); - if (waitResponse(GF(GSM_NL "+ICCID:")) != 1) { - return ""; - } - String res = stream.readStringUntil('\n'); - waitResponse(); - res.trim(); - return res; - } +TINY_GSM_MODEM_GET_SIMCCID_CCID() - String getIMEI() { - sendAT(GF("+GSN")); - if (waitResponse(GF(GSM_NL)) != 1) { - return ""; - } - String res = stream.readStringUntil('\n'); - waitResponse(); - res.trim(); - return res; - } +TINY_GSM_MODEM_GET_IMEI_GSN() SimStatus getSimStatus(unsigned long timeout = 10000L) { for (unsigned long start = millis(); millis() - start < timeout; ) { @@ -371,56 +319,22 @@ public: return SIM_ERROR; } - RegStatus getRegistrationStatus() { - sendAT(GF("+CREG?")); - if (waitResponse(GF(GSM_NL "+CREG:")) != 1) { - return REG_UNKNOWN; - } - streamSkipUntil(','); // Skip format (0) - int status = stream.readStringUntil('\n').toInt(); - waitResponse(); - return (RegStatus)status; - } +TINY_GSM_MODEM_GET_REGISTRATION_XREG(CREG) - String getOperator() { - sendAT(GF("+COPS?")); - if (waitResponse(GF(GSM_NL "+COPS:")) != 1) { - return ""; - } - streamSkipUntil('"'); // Skip mode and format - String res = stream.readStringUntil('"'); - waitResponse(); - return res; - } +TINY_GSM_MODEM_GET_OPERATOR_COPS() /* * Generic network functions */ - int16_t getSignalQuality() { - sendAT(GF("+CSQ")); - if (waitResponse(GF(GSM_NL "+CSQ:")) != 1) { - return 99; - } - int res = stream.readStringUntil(',').toInt(); - waitResponse(); - return res; - } +TINY_GSP_MODEM_GET_CSQ() bool isNetworkConnected() { RegStatus s = getRegistrationStatus(); return (s == REG_OK_HOME || s == REG_OK_ROAMING); } - bool waitForNetwork(unsigned long timeout = 60000L) { - for (unsigned long start = millis(); millis() - start < timeout; ) { - if (isNetworkConnected()) { - return true; - } - delay(250); - } - return false; - } +TINY_GSM_MODEM_WAIT_FOR_NETWORK() /* * GPRS functions @@ -869,37 +783,7 @@ public: Utilities */ - template - void streamWrite(T last) { - stream.print(last); - } - - template - void streamWrite(T head, Args... tail) { - stream.print(head); - streamWrite(tail...); - } - - template - void sendAT(Args... cmd) { - streamWrite("AT", cmd..., GSM_NL); - stream.flush(); - TINY_GSM_YIELD(); - //DBG("### AT:", cmd...); - } - - bool streamSkipUntil(const char c, const unsigned long timeout = 1000L) { - unsigned long startMillis = millis(); - while (millis() - startMillis < timeout) { - while (millis() - startMillis < timeout && !stream.available()) { - TINY_GSM_YIELD(); - } - if (stream.read() == c) { - return true; - } - } - return false; - } +TINY_GSP_MODEM_STREAM_UTILITIES() // TODO: Optimize this! uint8_t waitResponse(uint32_t timeout, String& data, diff --git a/src/TinyGsmClientSaraR4.h b/src/TinyGsmClientSaraR4.h index fd2b797..6220711 100644 --- a/src/TinyGsmClientSaraR4.h +++ b/src/TinyGsmClientSaraR4.h @@ -242,48 +242,18 @@ public: return name; } - void setBaud(unsigned long baud) { - sendAT(GF("+IPR="), baud); - } +TINY_GSM_MODEM_SET_BAUD_IPR() - bool testAT(unsigned long timeout = 10000L) { - for (unsigned long start = millis(); millis() - start < timeout; ) { - sendAT(GF("")); - if (waitResponse(200) == 1) return true; - delay(100); - } - return false; - } +TINY_GSM_MODEM_TEST_AT() - void maintain() { - for (int mux = 0; mux < TINY_GSM_MUX_COUNT; mux++) { - GsmClient* sock = sockets[mux]; - if (sock && sock->got_data) { - sock->got_data = false; - sock->sock_available = modemGetAvailable(mux); - } - } - while (stream.available()) { - waitResponse(15, NULL, NULL); - } - } +TINY_GSM_MODEM_MAINTAIN_CHECK_SOCKS() bool factoryDefault() { sendAT(GF("&F")); // Resets the current profile, other NVM not affected return waitResponse() == 1; } - String getModemInfo() { - sendAT(GF("I")); - String res; - if (waitResponse(1000L, res) != 1) { - return ""; - } - res.replace(GSM_NL "OK" GSM_NL, ""); - res.replace(GSM_NL, " "); - res.trim(); - return res; - } +TINY_GSM_MODEM_GET_INFO_ATI() bool hasSSL() { return true; @@ -333,21 +303,9 @@ public: * SIM card functions */ - bool simUnlock(const char *pin) { - sendAT(GF("+CPIN=\""), pin, GF("\"")); - return waitResponse() == 1; - } +TINY_GSM_MODEM_SIM_UNLOCK_CPIN() - String getSimCCID() { - sendAT(GF("+CCID")); - if (waitResponse(GF(GSM_NL "+CCID:")) != 1) { - return ""; - } - String res = stream.readStringUntil('\n'); - waitResponse(); - res.trim(); - return res; - } +TINY_GSM_MODEM_GET_SIMCCID_CCID() String getIMEI() { sendAT(GF("+CGSN")); @@ -379,42 +337,16 @@ public: return SIM_ERROR; } - RegStatus getRegistrationStatus() { - // Check EPS registration for LTE modules - sendAT(GF("+CEREG?")); - if (waitResponse(GF(GSM_NL "+CEREG:")) != 1) { - return REG_UNKNOWN; - } - streamSkipUntil(','); // Skip format (0) - int status = stream.readStringUntil('\n').toInt(); - waitResponse(); - return (RegStatus)status; - } - String getOperator() { - sendAT(GF("+COPS?")); - if (waitResponse(GF(GSM_NL "+COPS:")) != 1) { - return ""; - } - streamSkipUntil('"'); // Skip mode and format - String res = stream.readStringUntil('"'); - waitResponse(); - return res; - } +TINY_GSM_MODEM_GET_REGISTRATION_XREG(CEREG) + +TINY_GSM_MODEM_GET_OPERATOR_COPS() /* * Generic network functions */ - int16_t getSignalQuality() { - sendAT(GF("+CSQ")); - if (waitResponse(GF(GSM_NL "+CSQ:")) != 1) { - return 99; - } - int res = stream.readStringUntil(',').toInt(); - waitResponse(); - return res; - } +TINY_GSP_MODEM_GET_CSQ() bool isNetworkConnected() { RegStatus s = getRegistrationStatus(); @@ -425,15 +357,7 @@ public: else return false; } - bool waitForNetwork(unsigned long timeout = 60000L) { - for (unsigned long start = millis(); millis() - start < timeout; ) { - if (isNetworkConnected()) { - return true; - } - delay(250); - } - return false; - } +TINY_GSM_MODEM_WAIT_FOR_NETWORK() bool setURAT( uint8_t urat ) { // AT+URAT=[,[,<2ndPreferredAct>]] @@ -500,18 +424,7 @@ public: return true; } - bool isGprsConnected() { - sendAT(GF("+CGATT?")); - if (waitResponse(GF(GSM_NL "+CGATT:")) != 1) { - return false; - } - int res = stream.readStringUntil('\n').toInt(); - waitResponse(); - if (res != 1) - return false; - - return localIP() != IPAddress(0,0,0,0); - } +TINY_GSP_MODEM_GET_GPRS_IP_CONNECTED() /* * IP Address functions @@ -738,37 +651,7 @@ public: Utilities */ - template - void streamWrite(T last) { - stream.print(last); - } - - template - void streamWrite(T head, Args... tail) { - stream.print(head); - streamWrite(tail...); - } - - template - void sendAT(Args... cmd) { - streamWrite("AT", cmd..., GSM_NL); - stream.flush(); - TINY_GSM_YIELD(); - //DBG("### AT:", cmd...); - } - - bool streamSkipUntil(const char c, const unsigned long timeout = 1000L) { - unsigned long startMillis = millis(); - while (millis() - startMillis < timeout) { - while (millis() - startMillis < timeout && !stream.available()) { - TINY_GSM_YIELD(); - } - if (stream.read() == c) { - return true; - } - } - return false; - } +TINY_GSP_MODEM_STREAM_UTILITIES() // TODO: Optimize this! uint8_t waitResponse(uint32_t timeout, String& data, diff --git a/src/TinyGsmClientSequansMonarch.h b/src/TinyGsmClientSequansMonarch.h index 339ca7b..516fb61 100644 --- a/src/TinyGsmClientSequansMonarch.h +++ b/src/TinyGsmClientSequansMonarch.h @@ -215,9 +215,7 @@ public: return "Sequans Monarch"; } - void setBaud(unsigned long baud) { - sendAT(GF("+IPR="), baud); - } +TINY_GSM_MODEM_SET_BAUD_IPR() bool testAT(unsigned long timeout = 10000L) { for (unsigned long start = millis(); millis() - start < timeout; ) { @@ -231,18 +229,7 @@ public: return false; } - void maintain() { - for (int mux = 0; mux < TINY_GSM_MUX_COUNT; mux++) { - GsmClient* sock = sockets[mux]; - if (sock && sock->got_data) { - sock->got_data = false; - sock->sock_available = modemGetAvailable(mux); - } - } - while (stream.available()) { - waitResponse(10, NULL, NULL); - } - } +TINY_GSM_MODEM_MAINTAIN_CHECK_SOCKS() bool factoryDefault() { sendAT(GF("&FZE0&W")); // Factory + Reset + Echo Off + Write @@ -259,17 +246,7 @@ public: return waitResponse() == 1; } - String getModemInfo() { - sendAT(GF("I")); - String res; - if (waitResponse(1000L, res) != 1) { - return ""; - } - res.replace(GSM_NL "OK" GSM_NL, ""); - res.replace(GSM_NL, " "); - res.trim(); - return res; - } +TINY_GSM_MODEM_GET_INFO_ATI() bool hasSSL() { return true; @@ -325,10 +302,7 @@ public: * SIM card functions */ - bool simUnlock(const char *pin) { - sendAT(GF("+CPIN=\""), pin, GF("\"")); - return waitResponse() == 1; - } +TINY_GSM_MODEM_SIM_UNLOCK_CPIN() String getSimCCID() { sendAT(GF("+SQNCCID")); @@ -341,16 +315,7 @@ public: return res; } - String getIMEI() { - sendAT(GF("+GSN")); - if (waitResponse(GF(GSM_NL)) != 1) { - return ""; - } - String res = stream.readStringUntil('\n'); - waitResponse(); - res.trim(); - return res; - } +TINY_GSM_MODEM_GET_IMEI_GSN() SimStatus getSimStatus(unsigned long timeout = 10000L) { for (unsigned long start = millis(); millis() - start < timeout; ) { @@ -371,42 +336,15 @@ public: return SIM_ERROR; } - RegStatus getRegistrationStatus() { - //sendAT(GF("+CREG?")); - sendAT(GF("+CEREG?")); - if (waitResponse(GF(GSM_NL "+CEREG:")) != 1) { - return REG_UNKNOWN; - } - streamSkipUntil(','); // Skip format (0) - int status = stream.readStringUntil(',').toInt(); - waitResponse(); - return (RegStatus)status; - } +TINY_GSM_MODEM_GET_REGISTRATION_XREG(CEREG) - String getOperator() { - sendAT(GF("+COPS?")); - if (waitResponse(GF(GSM_NL "+COPS:")) != 1) { - return ""; - } - streamSkipUntil('"'); // Skip mode and format - String res = stream.readStringUntil('"'); - waitResponse(); - return res; - } +TINY_GSM_MODEM_GET_OPERATOR_COPS() /* * Generic network functions */ - int16_t getSignalQuality() { - sendAT(GF("+CSQ")); - if (waitResponse(GF(GSM_NL "+CSQ:")) != 1) { - return 99; - } - int res = stream.readStringUntil(',').toInt(); - waitResponse(); - return res; - } +TINY_GSP_MODEM_GET_CSQ() bool isNetworkConnected() { RegStatus s = getRegistrationStatus(); @@ -418,15 +356,7 @@ public: } } - bool waitForNetwork(unsigned long timeout = 60000L) { - for (unsigned long start = millis(); millis() - start < timeout; ) { - if (isNetworkConnected()) { - return true; - } - delay(250); - } - return false; - } +TINY_GSM_MODEM_WAIT_FOR_NETWORK() /* * GPRS functions @@ -665,37 +595,7 @@ public: Utilities */ - template - void streamWrite(T last) { - stream.print(last); - } - - template - void streamWrite(T head, Args... tail) { - stream.print(head); - streamWrite(tail...); - } - - template - void sendAT(Args... cmd) { - streamWrite("AT", cmd..., GSM_NL); - stream.flush(); - TINY_GSM_YIELD(); - //DBG("### AT:", cmd...); - } - - bool streamSkipUntil(const char c, const unsigned long timeout = 1000L) { - unsigned long startMillis = millis(); - while (millis() - startMillis < timeout) { - while (millis() - startMillis < timeout && !stream.available()) { - TINY_GSM_YIELD(); - } - if (stream.read() == c) { - return true; - } - } - return false; - } +TINY_GSP_MODEM_STREAM_UTILITIES() // TODO: Optimize this! uint8_t waitResponse(uint32_t timeout, String& data, diff --git a/src/TinyGsmClientUBLOX.h b/src/TinyGsmClientUBLOX.h index 5feb2d7..ec43e8b 100644 --- a/src/TinyGsmClientUBLOX.h +++ b/src/TinyGsmClientUBLOX.h @@ -229,31 +229,11 @@ public: return name; } - void setBaud(unsigned long baud) { - sendAT(GF("+IPR="), baud); - } +TINY_GSM_MODEM_SET_BAUD_IPR() - bool testAT(unsigned long timeout = 10000L) { - for (unsigned long start = millis(); millis() - start < timeout; ) { - sendAT(GF("")); - if (waitResponse(200) == 1) return true; - delay(100); - } - return false; - } +TINY_GSM_MODEM_TEST_AT() - void maintain() { - for (int mux = 0; mux < TINY_GSM_MUX_COUNT; mux++) { - GsmClient* sock = sockets[mux]; - if (sock && sock->got_data) { - sock->got_data = false; - sock->sock_available = modemGetAvailable(mux); - } - } - while (stream.available()) { - waitResponse(15, NULL, NULL); - } - } +TINY_GSM_MODEM_MAINTAIN_CHECK_SOCKS() bool factoryDefault() { sendAT(GF("+UFACTORY=0,1")); // No factory restore, erase NVM @@ -262,17 +242,7 @@ public: return waitResponse() == 1; } - String getModemInfo() { - sendAT(GF("I")); - String res; - if (waitResponse(1000L, res) != 1) { - return ""; - } - res.replace(GSM_NL "OK" GSM_NL, ""); - res.replace(GSM_NL, " "); - res.trim(); - return res; - } +TINY_GSM_MODEM_GET_INFO_ATI() bool hasSSL() { return true; @@ -322,21 +292,9 @@ public: * SIM card functions */ - bool simUnlock(const char *pin) { - sendAT(GF("+CPIN=\""), pin, GF("\"")); - return waitResponse() == 1; - } +TINY_GSM_MODEM_SIM_UNLOCK_CPIN() - String getSimCCID() { - sendAT(GF("+CCID")); - if (waitResponse(GF(GSM_NL "+CCID:")) != 1) { - return ""; - } - String res = stream.readStringUntil('\n'); - waitResponse(); - res.trim(); - return res; - } +TINY_GSM_MODEM_GET_SIMCCID_CCID() String getIMEI() { sendAT(GF("+CGSN")); @@ -368,42 +326,15 @@ public: return SIM_ERROR; } - RegStatus getRegistrationStatus() { - // Check GPRS registration - sendAT(GF("+CGREG?")); - if (waitResponse(GF(GSM_NL "+CGREG:")) != 1) { - return REG_UNKNOWN; - } - streamSkipUntil(','); // Skip format (0) - int status = stream.readStringUntil('\n').toInt(); - waitResponse(); - return (RegStatus)status; - } +TINY_GSM_MODEM_GET_REGISTRATION_XREG(CGREG) - String getOperator() { - sendAT(GF("+COPS?")); - if (waitResponse(GF(GSM_NL "+COPS:")) != 1) { - return ""; - } - streamSkipUntil('"'); // Skip mode and format - String res = stream.readStringUntil('"'); - waitResponse(); - return res; - } +TINY_GSM_MODEM_GET_OPERATOR_COPS() /* * Generic network functions */ - int16_t getSignalQuality() { - sendAT(GF("+CSQ")); - if (waitResponse(GF(GSM_NL "+CSQ:")) != 1) { - return 99; - } - int res = stream.readStringUntil(',').toInt(); - waitResponse(); - return res; - } +TINY_GSP_MODEM_GET_CSQ() bool isNetworkConnected() { RegStatus s = getRegistrationStatus(); @@ -414,15 +345,7 @@ public: else return false; } - bool waitForNetwork(unsigned long timeout = 60000L) { - for (unsigned long start = millis(); millis() - start < timeout; ) { - if (isNetworkConnected()) { - return true; - } - delay(250); - } - return false; - } +TINY_GSM_MODEM_WAIT_FOR_NETWORK() /* * GPRS functions @@ -483,18 +406,7 @@ public: return true; } - bool isGprsConnected() { - sendAT(GF("+CGATT?")); - if (waitResponse(GF(GSM_NL "+CGATT:")) != 1) { - return false; - } - int res = stream.readStringUntil('\n').toInt(); - waitResponse(); - if (res != 1) - return false; - - return localIP() != IPAddress(0,0,0,0); - } +TINY_GSP_MODEM_GET_GPRS_IP_CONNECTED() /* * IP Address functions @@ -724,37 +636,7 @@ public: Utilities */ - template - void streamWrite(T last) { - stream.print(last); - } - - template - void streamWrite(T head, Args... tail) { - stream.print(head); - streamWrite(tail...); - } - - template - void sendAT(Args... cmd) { - streamWrite("AT", cmd..., GSM_NL); - stream.flush(); - TINY_GSM_YIELD(); - //DBG("### AT:", cmd...); - } - - bool streamSkipUntil(const char c, const unsigned long timeout = 1000L) { - unsigned long startMillis = millis(); - while (millis() - startMillis < timeout) { - while (millis() - startMillis < timeout && !stream.available()) { - TINY_GSM_YIELD(); - } - if (stream.read() == c) { - return true; - } - } - return false; - } +TINY_GSP_MODEM_STREAM_UTILITIES() // TODO: Optimize this! uint8_t waitResponse(uint32_t timeout, String& data, diff --git a/src/TinyGsmClientXBee.h b/src/TinyGsmClientXBee.h index 2a536c7..7a1a671 100644 --- a/src/TinyGsmClientXBee.h +++ b/src/TinyGsmClientXBee.h @@ -933,37 +933,7 @@ public: } } - template - void streamWrite(T last) { - stream.print(last); - } - - template - void streamWrite(T head, Args... tail) { - stream.print(head); - streamWrite(tail...); - } - - template - void sendAT(Args... cmd) { - streamWrite("AT", cmd..., GSM_NL); - stream.flush(); - TINY_GSM_YIELD(); - //DBG("### AT:", cmd...); - } - - bool streamSkipUntil(const char c, const unsigned long timeout = 1000L) { - unsigned long startMillis = millis(); - while (millis() - startMillis < timeout) { - while (millis() - startMillis < timeout && !stream.available()) { - TINY_GSM_YIELD(); - } - if (stream.read() == c) { - return true; - } - } - return false; - } +TINY_GSP_MODEM_STREAM_UTILITIES() // TODO: Optimize this! // NOTE: This function is used while INSIDE command mode, so we're only diff --git a/src/TinyGsmCommon.h b/src/TinyGsmCommon.h index 789e976..4a439b4 100644 --- a/src/TinyGsmCommon.h +++ b/src/TinyGsmCommon.h @@ -389,6 +389,7 @@ String TinyGsmDecodeHex16bit(String &instr) { } +// The peek, flush, and connected functions #define TINY_GSM_CLIENT_PEEK_FLUSH_CONNECTED() \ virtual int peek() { return -1; } /* TODO */ \ \ @@ -402,4 +403,209 @@ String TinyGsmDecodeHex16bit(String &instr) { } \ virtual operator bool() { return connected(); } + +// Set baud rate via the V.25TER standard IPR command +#define TINY_GSM_MODEM_SET_BAUD_IPR() \ + void setBaud(unsigned long baud) { \ + sendAT(GF("+IPR="), baud); \ + } + + +// Test response to AT commands +#define TINY_GSM_MODEM_TEST_AT() \ + bool testAT(unsigned long timeout = 10000L) { \ + for (unsigned long start = millis(); millis() - start < timeout; ) { \ + sendAT(GF("")); \ + if (waitResponse(200) == 1) return true; \ + delay(100); \ + } \ + return false; \ + } + + +// Keeps listening for modem URC's and iterates through sockets +// to see if any data is avaiable +#define TINY_GSM_MODEM_MAINTAIN_CHECK_SOCKS() \ + void maintain() { \ + for (int mux = 0; mux < TINY_GSM_MUX_COUNT; mux++) { \ + GsmClient* sock = sockets[mux]; \ + if (sock && sock->got_data) { \ + sock->got_data = false; \ + sock->sock_available = modemGetAvailable(mux); \ + } \ + } \ + while (stream.available()) { \ + waitResponse(15, NULL, NULL); \ + } \ + } + + +// Keeps listening for modem URC's - doesn't check socks because +// modem has no internal fifo +#define TINY_GSM_MODEM_MAINTAIN_LISTEN() \ + void maintain() { \ + waitResponse(10, NULL, NULL); \ + } + + +// Asks for modem information via the V.25TER standard ATI command +// NOTE: The actual value and style of the response is quite varied +#define TINY_GSM_MODEM_GET_INFO_ATI() \ + String getModemInfo() { \ + sendAT(GF("I")); \ + String res; \ + if (waitResponse(1000L, res) != 1) { \ + return ""; \ + } \ + res.replace(GSM_NL "OK" GSM_NL, ""); \ + res.replace(GSM_NL, " "); \ + res.trim(); \ + return res; \ + } + + +// Unlocks a sim via the 3GPP TS command AT+CPIN +#define TINY_GSM_MODEM_SIM_UNLOCK_CPIN() \ + bool simUnlock(const char *pin) { \ + sendAT(GF("+CPIN=\""), pin, GF("\"")); \ + return waitResponse() == 1; \ + } + + +// Gets the CCID of a sim card via AT+CCID +#define TINY_GSM_MODEM_GET_SIMCCID_CCID() \ + String getSimCCID() { \ + sendAT(GF("+CCID")); \ + if (waitResponse(GF(GSM_NL "+CCID:")) != 1) { \ + return ""; \ + } \ + String res = stream.readStringUntil('\n'); \ + waitResponse(); \ + res.trim(); \ + return res; \ + } + + +// Asks for TA Serial Number Identification (IMEI) via the V.25TER standard AT+GSN command +#define TINY_GSM_MODEM_GET_IMEI_GSN() \ + String getIMEI() { \ + sendAT(GF("+GSN")); \ + if (waitResponse(GF(GSM_NL)) != 1) { \ + return ""; \ + } \ + String res = stream.readStringUntil('\n'); \ + waitResponse(); \ + res.trim(); \ + return res; \ + } + + +// Gets the modem's registration status via CREG/CGREG/CEREG +// CREG = Generic network registration +// CGREG = GPRS service registration +// CEREG = EPS registration for LTE modules +#define TINY_GSM_MODEM_GET_REGISTRATION_XREG(regCommand) \ + RegStatus getRegistrationStatus() { \ + sendAT(GF("+" #regCommand "?")); \ + if (waitResponse(GF(GSM_NL "+" #regCommand ":")) != 1) { \ + return REG_UNKNOWN; \ + } \ + streamSkipUntil(','); /* Skip format (0) */ \ + int status = stream.readStringUntil('\n').toInt(); \ + waitResponse(); \ + return (RegStatus)status; \ + } + + +// Gets the current network operator via the 3GPP TS command AT+COPS +#define TINY_GSM_MODEM_GET_OPERATOR_COPS() \ + String getOperator() { \ + sendAT(GF("+COPS?")); \ + if (waitResponse(GF(GSM_NL "+COPS:")) != 1) { \ + return ""; \ + } \ + streamSkipUntil('"'); /* Skip mode and format */ \ + String res = stream.readStringUntil('"'); \ + waitResponse(); \ + return res; \ + } + + +// Waits for network attachment +#define TINY_GSM_MODEM_WAIT_FOR_NETWORK() \ + bool waitForNetwork(unsigned long timeout = 60000L) { \ + for (unsigned long start = millis(); millis() - start < timeout; ) { \ + if (isNetworkConnected()) { \ + return true; \ + } \ + delay(250); \ + } \ + return false; \ + } + + +// Checks if current attached to GPRS/EPS service +#define TINY_GSP_MODEM_GET_GPRS_IP_CONNECTED() \ + bool isGprsConnected() { \ + sendAT(GF("+CGATT?")); \ + if (waitResponse(GF(GSM_NL "+CGATT:")) != 1) { \ + return false; \ + } \ + int res = stream.readStringUntil('\n').toInt(); \ + waitResponse(); \ + if (res != 1) \ + return false; \ + \ + return localIP() != IPAddress(0,0,0,0); \ + } + + +// Gets signal quality report according to 3GPP TS command AT+CSQ +#define TINY_GSP_MODEM_GET_CSQ() \ + int16_t getSignalQuality() { \ + sendAT(GF("+CSQ")); \ + if (waitResponse(GF(GSM_NL "+CSQ:")) != 1) { \ + return 99; \ + } \ + int res = stream.readStringUntil(',').toInt(); \ + waitResponse(); \ + return res; \ + } + + +// Utility templates for writing/skipping characters on a stream +#define TINY_GSP_MODEM_STREAM_UTILITIES() \ + template \ + void streamWrite(T last) { \ + stream.print(last); \ + } \ + \ + template \ + void streamWrite(T head, Args... tail) { \ + stream.print(head); \ + streamWrite(tail...); \ + } \ + \ + template \ + void sendAT(Args... cmd) { \ + streamWrite("AT", cmd..., GSM_NL); \ + stream.flush(); \ + TINY_GSM_YIELD(); \ + /* DBG("### AT:", cmd...); */ \ + } \ + \ + bool streamSkipUntil(const char c, const unsigned long timeout = 1000L) { \ + unsigned long startMillis = millis(); \ + while (millis() - startMillis < timeout) { \ + while (millis() - startMillis < timeout && !stream.available()) { \ + TINY_GSM_YIELD(); \ + } \ + if (stream.read() == c) { \ + return true; \ + } \ + } \ + return false; \ + } + + #endif