Browse Source

Cleanup, again

v_master
SRGDamia1 6 years ago
parent
commit
e08b48a04e
1 changed files with 79 additions and 56 deletions
  1. +79
    -56
      src/TinyGsmClientG350.h

TinyGsmClientG350.h → src/TinyGsmClientG350.h View File

@ -9,7 +9,7 @@
#ifndef TinyGsmClientG350_h
#define TinyGsmClientG350_h
//#define TINY_GSM_DEBUG Serial
// #define TINY_GSM_DEBUG Serial
#if !defined(TINY_GSM_RX_BUFFER)
#define TINY_GSM_RX_BUFFER 64
@ -38,10 +38,22 @@ enum RegStatus {
REG_UNKNOWN = 4,
};
//============================================================================//
//============================================================================//
// Declaration of the TinyGsmG350 Class
//============================================================================//
//============================================================================//
class TinyGsmG350
{
//============================================================================//
//============================================================================//
// The G350 Client Class
//============================================================================//
//============================================================================//
public:
class GsmClient : public Client
@ -160,14 +172,21 @@ public:
String remoteIP() TINY_GSM_ATTR_NOT_IMPLEMENTED;
private:
TinyGsmG350* at;
uint8_t mux;
uint16_t sock_available;
bool sock_connected;
bool got_data;
RxFifo rx;
TinyGsmG350* at;
uint8_t mux;
uint16_t sock_available;
bool sock_connected;
bool got_data;
RxFifo rx;
};
//============================================================================//
//============================================================================//
// The G350 Secure Client
//============================================================================//
//============================================================================//
class GsmClientSecure : public GsmClient
{
public:
@ -187,6 +206,12 @@ public:
}
};
//============================================================================//
//============================================================================//
// The G350 Modem Functions
//============================================================================//
//============================================================================//
public:
#ifdef GSM_DEFAULT_STREAM
@ -321,26 +346,15 @@ public:
int status = waitResponse(GF("READY"), GF("SIM PIN"), GF("SIM PUK"), GF("NOT INSERTED"));
waitResponse();
switch (status) {
case 2:
case 3: return SIM_LOCKED;
case 1: return SIM_READY;
default: return SIM_ERROR;
case 2:
case 3: return SIM_LOCKED;
case 1: return SIM_READY;
default: return SIM_ERROR;
}
}
return SIM_ERROR;
}
RegStatus getRegistrationStatus() {
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;
}
String getOperator() {
sendAT(GF("+COPS?"));
if (waitResponse(GF(GSM_NL "+COPS:")) != 1) {
@ -356,6 +370,17 @@ public:
* Generic network functions
*/
RegStatus getRegistrationStatus() {
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;
}
int getSignalQuality() {
sendAT(GF("+CSQ"));
if (waitResponse(GF(GSM_NL "+CSQ:")) != 1) {
@ -382,6 +407,24 @@ public:
return false;
}
String getLocalIP() {
sendAT(GF("+CIFSR;E0"));
String res;
if (waitResponse(10000L, res) != 1) {
return "";
}
res.trim();
return res;
}
IPAddress localIP() {
return TinyGsmIpFromString(getLocalIP());
}
/*
* WiFi functions
*/
/*
* GPRS functions
*/
@ -446,32 +489,6 @@ public:
return true;
}
String getLocalIP() {
sendAT(GF("+CIFSR;E0"));
String res;
if (waitResponse(10000L, res) != 1) {
return "";
}
res.trim();
return res;
}
IPAddress localIP() {
return TinyGsmIpFromString(getLocalIP());
}
/*
* Phone Call functions
*/
bool setGsmBusy(bool busy = true) TINY_GSM_ATTR_NOT_IMPLEMENTED;
bool callAnswer() TINY_GSM_ATTR_NOT_IMPLEMENTED;
bool callNumber(const String& number) TINY_GSM_ATTR_NOT_IMPLEMENTED;
bool callHangup() TINY_GSM_ATTR_NOT_IMPLEMENTED;
/*
* Messaging functions
*/
@ -625,9 +642,13 @@ public:
streamWrite(tail...);
}
bool streamSkipUntil(char c) { //TODO: timeout
while (true) {
while (!stream.available()) { TINY_GSM_YIELD(); }
bool streamSkipUntil(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;
}
@ -639,7 +660,7 @@ public:
streamWrite("AT", cmd..., GSM_NL);
stream.flush();
TINY_GSM_YIELD();
//DBG("### AT:", cmd...);
DBG("### AT:", cmd...);
}
// TODO: Optimize this!
@ -647,13 +668,12 @@ public:
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);*/
DBG("### ..:", r1s, ",", r2s, ",", r3s, ",", r4s, ",", r5s);
data.reserve(64);
int index = 0;
unsigned long startMillis = millis();
@ -661,7 +681,7 @@ public:
TINY_GSM_YIELD();
while (stream.available() > 0) {
int a = stream.read();
if (a < 0) continue;
if (a <= 0) continue; // Skip 0x00 bytes, just in case
data += (char)a;
if (r1 && data.endsWith(r1)) {
index = 1;
@ -702,6 +722,7 @@ finish:
}
data = "";
}
DBG('<', index, '>');
return index;
}
@ -719,8 +740,10 @@ finish:
return waitResponse(1000, r1, r2, r3, r4, r5);
}
protected:
public:
Stream& stream;
protected:
GsmClient* sockets[TINY_GSM_MUX_COUNT];
};

Loading…
Cancel
Save