mirror of
https://github.com/vshymanskyy/TinyGSM.git
synced 2026-05-15 04:06:10 +00:00
Update SARA-R5 to use URCs
Signed-off-by: Sara Damiano <sdamiano@stroudcenter.org>
This commit is contained in:
@@ -14,6 +14,10 @@
|
||||
|
||||
#define TINY_GSM_MUX_COUNT 7
|
||||
#define TINY_GSM_BUFFER_READ_AND_CHECK_SIZE
|
||||
#ifdef AT_NL
|
||||
#undef AT_NL
|
||||
#endif
|
||||
#define AT_NL "\r\n" // NOTE: define before including TinyGsmModem!
|
||||
|
||||
#include "TinyGsmBattery.tpp"
|
||||
#include "TinyGsmCalling.tpp"
|
||||
@@ -28,15 +32,7 @@
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#define GSM_NL "\r\n"
|
||||
static const char GSM_OK[] TINY_GSM_PROGMEM = "OK" GSM_NL;
|
||||
static const char GSM_ERROR[] TINY_GSM_PROGMEM = "ERROR" GSM_NL;
|
||||
#if defined TINY_GSM_DEBUG
|
||||
static const char GSM_CME_ERROR[] TINY_GSM_PROGMEM = GSM_NL "+CME ERROR:";
|
||||
static const char GSM_CMS_ERROR[] TINY_GSM_PROGMEM = GSM_NL "+CMS ERROR:";
|
||||
#endif
|
||||
|
||||
enum RegStatus {
|
||||
enum SaraR5RegStatus {
|
||||
REG_NO_RESULT = -1,
|
||||
REG_UNREGISTERED = 0,
|
||||
REG_SEARCHING = 2,
|
||||
@@ -123,7 +119,7 @@ class TinyGsmSaraR5 : public TinyGsmModem<TinyGsmSaraR5>,
|
||||
sock_connected = at->modemConnect(host, port, &mux, false, timeout_s);
|
||||
if (mux != oldMux) {
|
||||
DBG("WARNING: Mux number changed from", oldMux, "to", mux);
|
||||
at->sockets[oldMux] = NULL;
|
||||
at->sockets[oldMux] = nullptr;
|
||||
}
|
||||
at->sockets[mux] = this;
|
||||
at->maintain();
|
||||
@@ -169,7 +165,7 @@ class TinyGsmSaraR5 : public TinyGsmModem<TinyGsmSaraR5>,
|
||||
sock_connected = at->modemConnect(host, port, &mux, true, timeout_s);
|
||||
if (mux != oldMux) {
|
||||
DBG("WARNING: Mux number changed from", oldMux, "to", mux);
|
||||
at->sockets[oldMux] = NULL;
|
||||
at->sockets[oldMux] = nullptr;
|
||||
}
|
||||
at->sockets[mux] = this;
|
||||
at->maintain();
|
||||
@@ -190,7 +186,7 @@ class TinyGsmSaraR5 : public TinyGsmModem<TinyGsmSaraR5>,
|
||||
* Basic functions
|
||||
*/
|
||||
protected:
|
||||
bool initImpl(const char* pin = NULL) {
|
||||
bool initImpl(const char* pin = nullptr) {
|
||||
DBG(GF("### TinyGSM Version:"), TINYGSM_VERSION);
|
||||
DBG(GF("### TinyGSM Compiled Module: TinyGsmClientSaraR5"));
|
||||
|
||||
@@ -216,7 +212,7 @@ class TinyGsmSaraR5 : public TinyGsmModem<TinyGsmSaraR5>,
|
||||
|
||||
SimStatus ret = getSimStatus();
|
||||
// if the sim isn't ready and a pin has been provided, try to unlock the sim
|
||||
if (ret != SIM_READY && pin != NULL && strlen(pin) > 0) {
|
||||
if (ret != SIM_READY && pin != nullptr && strlen(pin) > 0) {
|
||||
simUnlock(pin);
|
||||
return (getSimStatus() == SIM_READY);
|
||||
} else {
|
||||
@@ -231,13 +227,13 @@ class TinyGsmSaraR5 : public TinyGsmModem<TinyGsmSaraR5>,
|
||||
sendAT(GF("+CGMI"));
|
||||
String res1;
|
||||
if (waitResponse(1000L, res1) != 1) { return "u-blox Cellular Modem"; }
|
||||
res1.replace(GSM_NL "OK" GSM_NL, "");
|
||||
res1.replace(AT_NL "OK" AT_NL, "");
|
||||
res1.trim();
|
||||
|
||||
sendAT(GF("+GMM"));
|
||||
String res2;
|
||||
if (waitResponse(1000L, res2) != 1) { return "u-blox Cellular Modem"; }
|
||||
res2.replace(GSM_NL "OK" GSM_NL, "");
|
||||
res2.replace(AT_NL "OK" AT_NL, "");
|
||||
res2.trim();
|
||||
|
||||
String name = res1 + String(' ') + res2;
|
||||
@@ -261,7 +257,7 @@ class TinyGsmSaraR5 : public TinyGsmModem<TinyGsmSaraR5>,
|
||||
* Power functions
|
||||
*/
|
||||
protected:
|
||||
bool restartImpl(const char* pin = NULL) {
|
||||
bool restartImpl(const char* pin = nullptr) {
|
||||
if (!testAT()) { return false; }
|
||||
if (!setPhoneFunctionality(16)) { return false; }
|
||||
delay(3000); // TODO(?): Verify delay timing here
|
||||
@@ -284,16 +280,17 @@ class TinyGsmSaraR5 : public TinyGsmModem<TinyGsmSaraR5>,
|
||||
* Generic network functions
|
||||
*/
|
||||
public:
|
||||
RegStatus getRegistrationStatus() {
|
||||
SaraR5RegStatus getRegistrationStatus() {
|
||||
// Check first for EPS registration
|
||||
RegStatus epsStatus = (RegStatus)getRegistrationStatusXREG("CEREG");
|
||||
SaraR5RegStatus epsStatus =
|
||||
(SaraR5RegStatus)getRegistrationStatusXREG("CEREG");
|
||||
|
||||
// If we're connected on EPS, great!
|
||||
if (epsStatus == REG_OK_HOME || epsStatus == REG_OK_ROAMING) {
|
||||
return epsStatus;
|
||||
} else {
|
||||
// Otherwise, check generic network status
|
||||
return (RegStatus)getRegistrationStatusXREG("CREG");
|
||||
return (SaraR5RegStatus)getRegistrationStatusXREG("CREG");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -325,7 +322,7 @@ class TinyGsmSaraR5 : public TinyGsmModem<TinyGsmSaraR5>,
|
||||
|
||||
protected:
|
||||
bool isNetworkConnectedImpl() {
|
||||
RegStatus s = getRegistrationStatus();
|
||||
SaraR5RegStatus s = getRegistrationStatus();
|
||||
if (s == REG_OK_HOME || s == REG_OK_ROAMING || s == REG_SMS_ONLY_ROAMING ||
|
||||
s == REG_SMS_ONLY_HOME)
|
||||
return true;
|
||||
@@ -337,7 +334,7 @@ class TinyGsmSaraR5 : public TinyGsmModem<TinyGsmSaraR5>,
|
||||
|
||||
String getLocalIPImpl() {
|
||||
sendAT(GF("+UPSND=0,0"));
|
||||
if (waitResponse(GF(GSM_NL "+UPSND:")) != 1) { return ""; }
|
||||
if (waitResponse(GF(AT_NL "+UPSND:")) != 1) { return ""; }
|
||||
streamSkipUntil(','); // Skip PSD profile
|
||||
streamSkipUntil('\"'); // Skip request type
|
||||
String res = stream.readStringUntil('\"');
|
||||
@@ -349,8 +346,8 @@ class TinyGsmSaraR5 : public TinyGsmModem<TinyGsmSaraR5>,
|
||||
* GPRS functions
|
||||
*/
|
||||
protected:
|
||||
bool gprsConnectImpl(const char* apn, const char* user = NULL,
|
||||
const char* pwd = NULL) {
|
||||
bool gprsConnectImpl(const char* apn, const char* user = nullptr,
|
||||
const char* pwd = nullptr) {
|
||||
sendAT(GF("+CGATT=1")); // attach to GPRS
|
||||
if (waitResponse(360000L) != 1) { return false; }
|
||||
|
||||
@@ -453,7 +450,7 @@ class TinyGsmSaraR5 : public TinyGsmModem<TinyGsmSaraR5>,
|
||||
// This uses "CGSN" instead of "GSN"
|
||||
String getIMEIImpl() {
|
||||
sendAT(GF("+CGSN"));
|
||||
if (waitResponse(GF(GSM_NL)) != 1) { return ""; }
|
||||
if (waitResponse(GF(AT_NL)) != 1) { return ""; }
|
||||
String res = stream.readStringUntil('\n');
|
||||
waitResponse();
|
||||
res.trim();
|
||||
@@ -499,7 +496,7 @@ class TinyGsmSaraR5 : public TinyGsmModem<TinyGsmSaraR5>,
|
||||
// waitResponse(10000L) ;
|
||||
if (waitResponse(10000L) != 1) { return ""; }
|
||||
// wait for the final result - wait full timeout time
|
||||
if (waitResponse(120000L, GF(GSM_NL "+UULOC:")) != 1) { return ""; }
|
||||
if (waitResponse(120000L, GF(AT_NL "+UULOC:")) != 1) { return ""; }
|
||||
String res = stream.readStringUntil('\n');
|
||||
waitResponse();
|
||||
res.trim();
|
||||
@@ -513,7 +510,6 @@ class TinyGsmSaraR5 : public TinyGsmModem<TinyGsmSaraR5>,
|
||||
return getUbloxLocationRaw(1);
|
||||
}
|
||||
|
||||
|
||||
inline bool getUbloxLocation(int8_t sensor, float* lat, float* lon,
|
||||
float* speed = 0, float* alt = 0, int* vsat = 0,
|
||||
int* usat = 0, float* accuracy = 0,
|
||||
@@ -539,7 +535,7 @@ class TinyGsmSaraR5 : public TinyGsmModem<TinyGsmSaraR5>,
|
||||
// wait for first "OK"
|
||||
if (waitResponse(10000L) != 1) { return false; }
|
||||
// wait for the final result - wait full timeout time
|
||||
if (waitResponse(120000L, GF(GSM_NL "+UULOC: ")) != 1) { return false; }
|
||||
if (waitResponse(120000L, GF(AT_NL "+UULOC: ")) != 1) { return false; }
|
||||
|
||||
// +UULOC: <date>, <time>, <lat>, <long>, <alt>, <uncertainty>, <speed>,
|
||||
// <direction>, <vertical_acc>, <sensor_used>, <SV_used>, <antenna_status>,
|
||||
@@ -588,27 +584,26 @@ class TinyGsmSaraR5 : public TinyGsmModem<TinyGsmSaraR5>,
|
||||
}
|
||||
|
||||
// Set pointers
|
||||
if (lat != NULL) *lat = ilat;
|
||||
if (lon != NULL) *lon = ilon;
|
||||
if (speed != NULL) *speed = ispeed;
|
||||
if (alt != NULL) *alt = ialt;
|
||||
if (vsat != NULL) *vsat = 0; // Number of satellites viewed not reported;
|
||||
if (usat != NULL) *usat = iusat;
|
||||
if (accuracy != NULL) *accuracy = iaccuracy;
|
||||
if (lat != nullptr) *lat = ilat;
|
||||
if (lon != nullptr) *lon = ilon;
|
||||
if (speed != nullptr) *speed = ispeed;
|
||||
if (alt != nullptr) *alt = ialt;
|
||||
if (vsat != nullptr)
|
||||
*vsat = 0; // Number of satellites viewed not reported;
|
||||
if (usat != nullptr) *usat = iusat;
|
||||
if (accuracy != nullptr) *accuracy = iaccuracy;
|
||||
if (iyear < 2000) iyear += 2000;
|
||||
if (year != NULL) *year = iyear;
|
||||
if (month != NULL) *month = imonth;
|
||||
if (day != NULL) *day = iday;
|
||||
if (hour != NULL) *hour = ihour;
|
||||
if (minute != NULL) *minute = imin;
|
||||
if (second != NULL) *second = static_cast<int>(secondWithSS);
|
||||
if (year != nullptr) *year = iyear;
|
||||
if (month != nullptr) *month = imonth;
|
||||
if (day != nullptr) *day = iday;
|
||||
if (hour != nullptr) *hour = ihour;
|
||||
if (minute != nullptr) *minute = imin;
|
||||
if (second != nullptr) *second = static_cast<int>(secondWithSS);
|
||||
|
||||
// final ok
|
||||
waitResponse();
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool getGsmLocationImpl(float* lat, float* lon, float* accuracy = 0,
|
||||
int* year = 0, int* month = 0, int* day = 0,
|
||||
int* hour = 0, int* minute = 0, int* second = 0) {
|
||||
@@ -637,7 +632,7 @@ class TinyGsmSaraR5 : public TinyGsmModem<TinyGsmSaraR5>,
|
||||
|
||||
int8_t getBattPercentImpl() {
|
||||
sendAT(GF("+CIND?"));
|
||||
if (waitResponse(GF(GSM_NL "+CIND:")) != 1) { return 0; }
|
||||
if (waitResponse(GF(AT_NL "+CIND:")) != 1) { return 0; }
|
||||
|
||||
int8_t res = streamGetIntBefore(',');
|
||||
int8_t percent = res * 20; // return is 0-5
|
||||
@@ -676,7 +671,7 @@ class TinyGsmSaraR5 : public TinyGsmModem<TinyGsmSaraR5>,
|
||||
// create a socket
|
||||
sendAT(GF("+USOCR=6"));
|
||||
// reply is +USOCR: ## of socket created
|
||||
if (waitResponse(GF(GSM_NL "+USOCR:")) != 1) { return false; }
|
||||
if (waitResponse(GF(AT_NL "+USOCR:")) != 1) { return false; }
|
||||
*mux = streamGetIntBefore('\n');
|
||||
waitResponse();
|
||||
|
||||
@@ -710,7 +705,7 @@ class TinyGsmSaraR5 : public TinyGsmModem<TinyGsmSaraR5>,
|
||||
delay(50);
|
||||
stream.write(reinterpret_cast<const uint8_t*>(buff), len);
|
||||
stream.flush();
|
||||
if (waitResponse(GF(GSM_NL "+USOWR:")) != 1) { return 0; }
|
||||
if (waitResponse(GF(AT_NL "+USOWR:")) != 1) { return 0; }
|
||||
streamSkipUntil(','); // Skip mux
|
||||
int16_t sent = streamGetIntBefore('\n');
|
||||
waitResponse(); // sends back OK after the confirmation of number sent
|
||||
@@ -720,7 +715,7 @@ class TinyGsmSaraR5 : public TinyGsmModem<TinyGsmSaraR5>,
|
||||
size_t modemRead(size_t size, uint8_t mux) {
|
||||
if (!sockets[mux]) return 0;
|
||||
sendAT(GF("+USORD="), mux, ',', (uint16_t)size);
|
||||
if (waitResponse(GF(GSM_NL "+USORD:")) != 1) { return 0; }
|
||||
if (waitResponse(GF(AT_NL "+USORD:")) != 1) { return 0; }
|
||||
streamSkipUntil(','); // Skip mux
|
||||
int16_t len = streamGetIntBefore(',');
|
||||
streamSkipUntil('\"');
|
||||
@@ -738,7 +733,7 @@ class TinyGsmSaraR5 : public TinyGsmModem<TinyGsmSaraR5>,
|
||||
// NOTE: Querying a closed socket gives an error "operation not allowed"
|
||||
sendAT(GF("+USORD="), mux, ",0");
|
||||
size_t result = 0;
|
||||
uint8_t res = waitResponse(GF(GSM_NL "+USORD:"));
|
||||
uint8_t res = waitResponse(GF(AT_NL "+USORD:"));
|
||||
// Will give error "operation not allowed" when attempting to read a socket
|
||||
// that you have already told to close
|
||||
if (res == 1) {
|
||||
@@ -748,14 +743,14 @@ class TinyGsmSaraR5 : public TinyGsmModem<TinyGsmSaraR5>,
|
||||
waitResponse();
|
||||
}
|
||||
if (!result) { sockets[mux]->sock_connected = modemGetConnected(mux); }
|
||||
// DBG("### AvailablE:", result, "on", mux);
|
||||
// DBG("### Available:", result, "on", mux);
|
||||
return result;
|
||||
}
|
||||
|
||||
bool modemGetConnected(uint8_t mux) {
|
||||
// NOTE: Querying a closed socket gives an error "operation not allowed"
|
||||
sendAT(GF("+USOCTL="), mux, ",10");
|
||||
uint8_t res = waitResponse(GF(GSM_NL "+USOCTL:"));
|
||||
uint8_t res = waitResponse(GF(AT_NL "+USOCTL:"));
|
||||
if (res != 1) { return false; }
|
||||
|
||||
streamSkipUntil(','); // Skip mux
|
||||
@@ -781,108 +776,28 @@ class TinyGsmSaraR5 : public TinyGsmModem<TinyGsmSaraR5>,
|
||||
* Utilities
|
||||
*/
|
||||
public:
|
||||
// TODO(vshymanskyy): Optimize this!
|
||||
int8_t waitResponse(uint32_t timeout_ms, String& data,
|
||||
GsmConstStr r1 = GFP(GSM_OK),
|
||||
GsmConstStr r2 = GFP(GSM_ERROR),
|
||||
#if defined TINY_GSM_DEBUG
|
||||
GsmConstStr r3 = GFP(GSM_CME_ERROR),
|
||||
GsmConstStr r4 = GFP(GSM_CMS_ERROR),
|
||||
#else
|
||||
GsmConstStr r3 = NULL, GsmConstStr r4 = NULL,
|
||||
#endif
|
||||
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);
|
||||
uint8_t index = 0;
|
||||
uint32_t startMillis = millis();
|
||||
do {
|
||||
TINY_GSM_YIELD();
|
||||
while (stream.available() > 0) {
|
||||
TINY_GSM_YIELD();
|
||||
int8_t a = stream.read();
|
||||
|
||||
if (a <= 0) continue; // Skip 0x00 bytes, just in case
|
||||
data += static_cast<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)) {
|
||||
#if defined TINY_GSM_DEBUG
|
||||
if (r3 == GFP(GSM_CME_ERROR)) {
|
||||
streamSkipUntil('\n'); // Read out the error
|
||||
}
|
||||
#endif
|
||||
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("+UUSORD:"))) {
|
||||
int8_t mux = streamGetIntBefore(',');
|
||||
int16_t len = streamGetIntBefore('\n');
|
||||
if (mux >= 0 && mux < TINY_GSM_MUX_COUNT && sockets[mux]) {
|
||||
sockets[mux]->got_data = true;
|
||||
// max size is 1024
|
||||
if (len >= 0 && len <= 1024) { sockets[mux]->sock_available = len; }
|
||||
}
|
||||
data = "";
|
||||
// DBG("### URC Data Received:", len, "on", mux);
|
||||
} else if (data.endsWith(GF("+UUSOCL:"))) {
|
||||
int8_t mux = streamGetIntBefore('\n');
|
||||
if (mux >= 0 && mux < TINY_GSM_MUX_COUNT && sockets[mux]) {
|
||||
sockets[mux]->sock_connected = false;
|
||||
}
|
||||
data = "";
|
||||
DBG("### URC Sock Closed: ", mux);
|
||||
}
|
||||
bool handleURCs(String& data) {
|
||||
if (data.endsWith(GF("+UUSORD:"))) {
|
||||
int8_t mux = streamGetIntBefore(',');
|
||||
int16_t len = streamGetIntBefore('\n');
|
||||
if (mux >= 0 && mux < TINY_GSM_MUX_COUNT && sockets[mux]) {
|
||||
sockets[mux]->got_data = true;
|
||||
// max size is 1024
|
||||
if (len >= 0 && len <= 1024) { sockets[mux]->sock_available = len; }
|
||||
}
|
||||
} while (millis() - startMillis < timeout_ms);
|
||||
finish:
|
||||
if (!index) {
|
||||
data.trim();
|
||||
if (data.length()) { DBG("### Unhandled:", data); }
|
||||
data = "";
|
||||
// DBG("### URC Data Received:", len, "on", mux);
|
||||
return true;
|
||||
} else if (data.endsWith(GF("+UUSOCL:"))) {
|
||||
int8_t mux = streamGetIntBefore('\n');
|
||||
if (mux >= 0 && mux < TINY_GSM_MUX_COUNT && sockets[mux]) {
|
||||
sockets[mux]->sock_connected = false;
|
||||
}
|
||||
data = "";
|
||||
DBG("### URC Sock Closed: ", mux);
|
||||
return true;
|
||||
}
|
||||
// data.replace(GSM_NL, "/");
|
||||
// DBG('<', index, '>', data);
|
||||
return index;
|
||||
}
|
||||
|
||||
int8_t waitResponse(uint32_t timeout_ms, GsmConstStr r1 = GFP(GSM_OK),
|
||||
GsmConstStr r2 = GFP(GSM_ERROR),
|
||||
#if defined TINY_GSM_DEBUG
|
||||
GsmConstStr r3 = GFP(GSM_CME_ERROR),
|
||||
GsmConstStr r4 = GFP(GSM_CMS_ERROR),
|
||||
#else
|
||||
GsmConstStr r3 = NULL, GsmConstStr r4 = NULL,
|
||||
#endif
|
||||
GsmConstStr r5 = NULL) {
|
||||
String data;
|
||||
return waitResponse(timeout_ms, data, r1, r2, r3, r4, r5);
|
||||
}
|
||||
|
||||
int8_t waitResponse(GsmConstStr r1 = GFP(GSM_OK),
|
||||
GsmConstStr r2 = GFP(GSM_ERROR),
|
||||
#if defined TINY_GSM_DEBUG
|
||||
GsmConstStr r3 = GFP(GSM_CME_ERROR),
|
||||
GsmConstStr r4 = GFP(GSM_CMS_ERROR),
|
||||
#else
|
||||
GsmConstStr r3 = NULL, GsmConstStr r4 = NULL,
|
||||
#endif
|
||||
GsmConstStr r5 = NULL) {
|
||||
return waitResponse(1000, r1, r2, r3, r4, r5);
|
||||
return false;
|
||||
}
|
||||
|
||||
private: // basically the same as waitResponse but without preemptive exiting
|
||||
@@ -910,8 +825,6 @@ class TinyGsmSaraR5 : public TinyGsmModem<TinyGsmSaraR5>,
|
||||
|
||||
protected:
|
||||
GsmClientSaraR5* sockets[TINY_GSM_MUX_COUNT];
|
||||
const char* gsmNL = GSM_NL;
|
||||
};
|
||||
|
||||
|
||||
#endif // SRC_TINYGSMCLIENTSARAR5_H_
|
||||
|
||||
Reference in New Issue
Block a user