Restructured to bring similar functions together
This commit is contained in:
@@ -247,9 +247,8 @@ public:
|
||||
if (waitResponse(GF(GSM_NL "+CCID:")) != 1) {
|
||||
return "";
|
||||
}
|
||||
String res = stream.readStringUntil('\n');
|
||||
String res = streamReadUntil('\n');
|
||||
waitResponse();
|
||||
res.trim();
|
||||
return res;
|
||||
}
|
||||
|
||||
@@ -258,9 +257,8 @@ public:
|
||||
if (waitResponse(GF(GSM_NL)) != 1) {
|
||||
return "";
|
||||
}
|
||||
String res = stream.readStringUntil('\n');
|
||||
String res = streamReadUntil('\n');
|
||||
waitResponse();
|
||||
res.trim();
|
||||
return res;
|
||||
}
|
||||
|
||||
@@ -269,38 +267,11 @@ public:
|
||||
if (waitResponse(GF(GSM_NL "+CSQ:")) != 1) {
|
||||
return 99;
|
||||
}
|
||||
int res = stream.readStringUntil(',').toInt();
|
||||
int res = streamReadUntil(',').toInt();
|
||||
waitResponse();
|
||||
return res;
|
||||
}
|
||||
|
||||
bool callAnswer() {
|
||||
sendAT(GF("A"));
|
||||
return waitResponse() == 1;
|
||||
}
|
||||
|
||||
bool callNumber(const String& number) {
|
||||
sendAT(GF("D"), number);
|
||||
return waitResponse() == 1;
|
||||
}
|
||||
|
||||
bool callHangup(const String& number) {
|
||||
sendAT(GF("H"), number);
|
||||
return waitResponse() == 1;
|
||||
}
|
||||
|
||||
bool sendSMS(const String& number, const String& text) {
|
||||
sendAT(GF("+CMGF=1"));
|
||||
waitResponse();
|
||||
sendAT(GF("+CMGS=\""), number, GF("\""));
|
||||
if (waitResponse(GF(">")) != 1) {
|
||||
return false;
|
||||
}
|
||||
stream.print(text);
|
||||
stream.write((char)0x1A);
|
||||
return waitResponse(60000L) == 1;
|
||||
}
|
||||
|
||||
SimStatus getSimStatus(unsigned long timeout = 10000L) {
|
||||
for (unsigned long start = millis(); millis() - start < timeout; ) {
|
||||
sendAT(GF("+CPIN?"));
|
||||
@@ -326,7 +297,7 @@ public:
|
||||
return REG_UNKNOWN;
|
||||
}
|
||||
streamSkipUntil(','); // Skip format (0)
|
||||
int status = stream.readStringUntil('\n').toInt();
|
||||
int status = streamReadUntil('\n').toInt();
|
||||
waitResponse();
|
||||
return (RegStatus)status;
|
||||
}
|
||||
@@ -337,7 +308,7 @@ public:
|
||||
return "";
|
||||
}
|
||||
streamSkipUntil('"'); // Skip mode and format
|
||||
String res = stream.readStringUntil('"');
|
||||
String res = streamReadUntil('"');
|
||||
waitResponse();
|
||||
return res;
|
||||
}
|
||||
@@ -347,8 +318,6 @@ public:
|
||||
RegStatus s = getRegistrationStatus();
|
||||
if (s == REG_OK_HOME || s == REG_OK_ROAMING) {
|
||||
return true;
|
||||
} else if (s == REG_UNREGISTERED) {
|
||||
return false;
|
||||
}
|
||||
delay(1000);
|
||||
}
|
||||
@@ -400,6 +369,21 @@ public:
|
||||
* Phone Call functions
|
||||
*/
|
||||
|
||||
bool callAnswer() {
|
||||
sendAT(GF("A"));
|
||||
return waitResponse() == 1;
|
||||
}
|
||||
|
||||
bool callNumber(const String& number) {
|
||||
sendAT(GF("D"), number);
|
||||
return waitResponse() == 1;
|
||||
}
|
||||
|
||||
bool callHangup(const String& number) {
|
||||
sendAT(GF("H"), number);
|
||||
return waitResponse() == 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Messaging functions
|
||||
*/
|
||||
@@ -410,6 +394,18 @@ public:
|
||||
void sendSMS() {
|
||||
}
|
||||
|
||||
bool sendSMS(const String& number, const String& text) {
|
||||
sendAT(GF("+CMGF=1"));
|
||||
waitResponse();
|
||||
sendAT(GF("+CMGS=\""), number, GF("\""));
|
||||
if (waitResponse(GF(">")) != 1) {
|
||||
return false;
|
||||
}
|
||||
stream.print(text);
|
||||
stream.write((char)0x1A);
|
||||
return waitResponse(60000L) == 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Location functions
|
||||
*/
|
||||
@@ -420,90 +416,13 @@ public:
|
||||
* Battery functions
|
||||
*/
|
||||
|
||||
private:
|
||||
String dnsIpQuery(const char* host) {
|
||||
sendAT(GF("+DNS=\""), host, GF("\""));
|
||||
if (waitResponse(10000L, GF(GSM_NL "+DNS:")) != 1) {
|
||||
return "";
|
||||
}
|
||||
String res = stream.readStringUntil('\n');
|
||||
waitResponse(GF("+DNS:OK" GSM_NL));
|
||||
res.trim();
|
||||
return res;
|
||||
}
|
||||
|
||||
int modemConnect(const char* host, uint16_t port, uint8_t mux) {
|
||||
int rsp = 0;
|
||||
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;
|
||||
}
|
||||
stream.readStringUntil('\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;
|
||||
}
|
||||
|
||||
/* Utilities */
|
||||
template<typename T>
|
||||
void streamWrite(T last) {
|
||||
stream.print(last);
|
||||
}
|
||||
|
||||
template<typename T, typename... Args>
|
||||
void streamWrite(T head, Args... tail) {
|
||||
stream.print(head);
|
||||
streamWrite(tail...);
|
||||
}
|
||||
|
||||
int streamRead() { return stream.read(); }
|
||||
|
||||
bool streamSkipUntil(char c) { //TODO: timeout
|
||||
while (true) {
|
||||
while (!stream.available()) {}
|
||||
if (stream.read() == c)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Public Utilities */
|
||||
template<typename... Args>
|
||||
void sendAT(Args... cmd) {
|
||||
streamWrite("AT", cmd..., GSM_NL);
|
||||
stream.flush();
|
||||
TINY_GSM_YIELD();
|
||||
//DBG("### AT:", cmd...);
|
||||
DBG(GSM_NL, ">>> AT:", cmd...);
|
||||
}
|
||||
|
||||
// TODO: Optimize this!
|
||||
@@ -542,8 +461,8 @@ private:
|
||||
index = 5;
|
||||
goto finish;
|
||||
} else if (data.endsWith(GF("+TCPRECV:"))) {
|
||||
int mux = stream.readStringUntil(',').toInt();
|
||||
int len = stream.readStringUntil(',').toInt();
|
||||
int mux = streamReadUntil(',').toInt();
|
||||
int len = streamReadUntil(',').toInt();
|
||||
if (len > sockets[mux]->rx.free()) {
|
||||
DBG("### Buffer overflow: ", len, "->", sockets[mux]->rx.free());
|
||||
} else {
|
||||
@@ -556,14 +475,14 @@ private:
|
||||
data = "";
|
||||
return index;
|
||||
} else if (data.endsWith(GF("+TCPCLOSE:"))) {
|
||||
int mux = stream.readStringUntil(',').toInt();
|
||||
stream.readStringUntil('\n');
|
||||
int mux = streamReadUntil(',').toInt();
|
||||
streamReadUntil('\n');
|
||||
sockets[mux]->sock_connected = false;
|
||||
data = "";
|
||||
}
|
||||
}
|
||||
} while (millis() - startMillis < timeout);
|
||||
finish:
|
||||
finish:
|
||||
if (!index) {
|
||||
data.trim();
|
||||
if (data.length()) {
|
||||
@@ -571,6 +490,15 @@ finish:
|
||||
}
|
||||
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);
|
||||
}
|
||||
data = "";
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
@@ -588,6 +516,95 @@ finish:
|
||||
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) {
|
||||
int rsp = 0;
|
||||
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<typename T>
|
||||
void streamWrite(T last) {
|
||||
stream.print(last);
|
||||
}
|
||||
|
||||
template<typename T, typename... Args>
|
||||
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) { //TODO: timeout
|
||||
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];
|
||||
|
||||
Reference in New Issue
Block a user