CRTP!!!! Totally untested
Signed-off-by: Sara Damiano <sdamiano@stroudcenter.org>
This commit is contained in:
@@ -6,32 +6,23 @@
|
||||
* @date Nov 2016
|
||||
*/
|
||||
|
||||
#ifndef TinyGsmClientUBLOX_h
|
||||
#define TinyGsmClientUBLOX_h
|
||||
//#pragma message("TinyGSM: TinyGsmClientUBLOX")
|
||||
#ifndef SRC_TINYGSMCLIENTUBLOX_H_
|
||||
#define SRC_TINYGSMCLIENTUBLOX_H_
|
||||
// #pragma message("TinyGSM: TinyGsmClientUBLOX")
|
||||
|
||||
//#define TINY_GSM_DEBUG Serial
|
||||
|
||||
#if !defined(TINY_GSM_RX_BUFFER)
|
||||
#define TINY_GSM_RX_BUFFER 64
|
||||
#endif
|
||||
// #define TINY_GSM_DEBUG Serial
|
||||
|
||||
#define TINY_GSM_MUX_COUNT 7
|
||||
|
||||
#include <TinyGsmCommon.h>
|
||||
#include "TinyGsmCommon.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;
|
||||
static const char GSM_OK[] TINY_GSM_PROGMEM = "OK" GSM_NL;
|
||||
static const char GSM_ERROR[] TINY_GSM_PROGMEM = "ERROR" GSM_NL;
|
||||
static const char GSM_CME_ERROR[] TINY_GSM_PROGMEM = GSM_NL "+CME ERROR:";
|
||||
|
||||
enum SimStatus {
|
||||
SIM_ERROR = 0,
|
||||
SIM_READY = 1,
|
||||
SIM_LOCKED = 2,
|
||||
};
|
||||
|
||||
enum RegStatus {
|
||||
REG_NO_RESULT = -1,
|
||||
REG_UNREGISTERED = 0,
|
||||
REG_SEARCHING = 2,
|
||||
REG_DENIED = 3,
|
||||
@@ -40,127 +31,115 @@ enum RegStatus {
|
||||
REG_UNKNOWN = 4,
|
||||
};
|
||||
|
||||
|
||||
class TinyGsmUBLOX
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
class GsmClient : public Client
|
||||
{
|
||||
friend class TinyGsmUBLOX;
|
||||
typedef TinyGsmFifo<uint8_t, TINY_GSM_RX_BUFFER> RxFifo;
|
||||
|
||||
public:
|
||||
GsmClient() {}
|
||||
|
||||
GsmClient(TinyGsmUBLOX& modem, uint8_t mux = 0) {
|
||||
init(&modem, mux);
|
||||
}
|
||||
|
||||
virtual ~GsmClient(){}
|
||||
|
||||
bool init(TinyGsmUBLOX* modem, uint8_t mux = 0) {
|
||||
this->at = modem;
|
||||
this->mux = mux;
|
||||
sock_available = 0;
|
||||
prev_check = 0;
|
||||
sock_connected = false;
|
||||
got_data = false;
|
||||
|
||||
at->sockets[mux] = this;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public:
|
||||
virtual int connect(const char *host, uint16_t port, int timeout_s) {
|
||||
stop();
|
||||
TINY_GSM_YIELD();
|
||||
rx.clear();
|
||||
|
||||
uint8_t oldMux = mux;
|
||||
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[mux] = this;
|
||||
at->maintain();
|
||||
|
||||
return sock_connected;
|
||||
}
|
||||
|
||||
TINY_GSM_CLIENT_CONNECT_OVERLOADS()
|
||||
|
||||
virtual void stop(uint32_t maxWaitMs) {
|
||||
TINY_GSM_CLIENT_DUMP_MODEM_BUFFER()
|
||||
at->sendAT(GF("+USOCL="), mux);
|
||||
at->waitResponse(); // should return within 1s
|
||||
sock_connected = false;
|
||||
}
|
||||
|
||||
virtual void stop() { stop(15000L); }
|
||||
|
||||
TINY_GSM_CLIENT_WRITE()
|
||||
|
||||
TINY_GSM_CLIENT_AVAILABLE_WITH_BUFFER_CHECK()
|
||||
|
||||
TINY_GSM_CLIENT_READ_WITH_BUFFER_CHECK()
|
||||
|
||||
TINY_GSM_CLIENT_PEEK_FLUSH_CONNECTED()
|
||||
: public TinyGsmModem<TinyGsmUBLOX, READ_AND_CHECK_SIZE, TINY_GSM_MUX_COUNT> {
|
||||
friend class TinyGsmModem<TinyGsmUBLOX, READ_AND_CHECK_SIZE,
|
||||
TINY_GSM_MUX_COUNT>;
|
||||
|
||||
/*
|
||||
* Extended API
|
||||
* Inner Client
|
||||
*/
|
||||
public:
|
||||
class GsmClientUBLOX : public GsmClient {
|
||||
friend class TinyGsmUBLOX;
|
||||
|
||||
String remoteIP() TINY_GSM_ATTR_NOT_IMPLEMENTED;
|
||||
public:
|
||||
GsmClientUBLOX() {}
|
||||
|
||||
private:
|
||||
TinyGsmUBLOX* at;
|
||||
uint8_t mux;
|
||||
uint16_t sock_available;
|
||||
uint32_t prev_check;
|
||||
bool sock_connected;
|
||||
bool got_data;
|
||||
RxFifo rx;
|
||||
};
|
||||
explicit GsmClientUBLOX(TinyGsmUBLOX& modem, uint8_t mux = 0) {
|
||||
init(&modem, mux);
|
||||
}
|
||||
|
||||
bool init(TinyGsmUBLOX* modem, uint8_t mux = 0) {
|
||||
this->at = modem;
|
||||
this->mux = mux;
|
||||
sock_available = 0;
|
||||
prev_check = 0;
|
||||
sock_connected = false;
|
||||
got_data = false;
|
||||
|
||||
class GsmClientSecure : public GsmClient
|
||||
{
|
||||
public:
|
||||
GsmClientSecure() {}
|
||||
at->sockets[mux] = this;
|
||||
|
||||
GsmClientSecure(TinyGsmUBLOX& modem, uint8_t mux = 1)
|
||||
: GsmClient(modem, mux)
|
||||
{}
|
||||
return true;
|
||||
}
|
||||
|
||||
virtual ~GsmClientSecure(){}
|
||||
public:
|
||||
int connect(const char* host, uint16_t port, int timeout_s) {
|
||||
stop();
|
||||
TINY_GSM_YIELD();
|
||||
rx.clear();
|
||||
|
||||
public:
|
||||
virtual int connect(const char *host, uint16_t port, int timeout_s) {
|
||||
stop();
|
||||
TINY_GSM_YIELD();
|
||||
rx.clear();
|
||||
uint8_t oldMux = mux;
|
||||
sock_connected = at->modemConnect(host, port, &mux, true, timeout_s);
|
||||
if (mux != oldMux) {
|
||||
uint8_t oldMux = mux;
|
||||
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[mux] = this;
|
||||
at->maintain();
|
||||
|
||||
return sock_connected;
|
||||
}
|
||||
int connect(IPAddress ip, uint16_t port, int timeout_s) {
|
||||
return connect(TinyGsmStringFromIp(ip).c_str(), port, timeout_s);
|
||||
}
|
||||
int connect(const char* host, uint16_t port) override {
|
||||
return connect(host, port, 75);
|
||||
}
|
||||
int connect(IPAddress ip, uint16_t port) override {
|
||||
return connect(ip, port, 75);
|
||||
}
|
||||
at->sockets[mux] = this;
|
||||
at->maintain();
|
||||
return sock_connected;
|
||||
}
|
||||
};
|
||||
|
||||
void stop(uint32_t maxWaitMs) {
|
||||
dumpModemBuffer(maxWaitMs);
|
||||
at->sendAT(GF("+USOCL="), mux);
|
||||
at->waitResponse(); // should return within 1s
|
||||
sock_connected = false;
|
||||
}
|
||||
void stop() override {
|
||||
stop(15000L);
|
||||
}
|
||||
|
||||
public:
|
||||
/*
|
||||
* Extended API
|
||||
*/
|
||||
|
||||
TinyGsmUBLOX(Stream& stream)
|
||||
: stream(stream)
|
||||
{
|
||||
String remoteIP() TINY_GSM_ATTR_NOT_IMPLEMENTED;
|
||||
};
|
||||
|
||||
/*
|
||||
* Inner Secure Client
|
||||
*/
|
||||
public:
|
||||
class GsmClientSecureUBLOX : public GsmClientUBLOX {
|
||||
public:
|
||||
GsmClientSecureUBLOX() {}
|
||||
|
||||
explicit GsmClientSecureUBLOX(TinyGsmUBLOX& modem, uint8_t mux = 1)
|
||||
: GsmClientUBLOX(modem, mux) {}
|
||||
|
||||
public:
|
||||
int connect(const char* host, uint16_t port, int timeout_s) {
|
||||
stop();
|
||||
TINY_GSM_YIELD();
|
||||
rx.clear();
|
||||
uint8_t oldMux = mux;
|
||||
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[mux] = this;
|
||||
at->maintain();
|
||||
return sock_connected;
|
||||
}
|
||||
};
|
||||
|
||||
/*
|
||||
* Constructor
|
||||
*/
|
||||
public:
|
||||
explicit TinyGsmUBLOX(Stream& stream) : stream(stream) {
|
||||
memset(sockets, 0, sizeof(sockets));
|
||||
}
|
||||
|
||||
@@ -168,21 +147,13 @@ public:
|
||||
* Basic functions
|
||||
*/
|
||||
|
||||
bool begin(const char* pin = NULL) {
|
||||
return init(pin);
|
||||
}
|
||||
|
||||
bool init(const char* pin = NULL) {
|
||||
bool initImpl(const char* pin = NULL) {
|
||||
DBG(GF("### TinyGSM Version:"), TINYGSM_VERSION);
|
||||
|
||||
if (!testAT()) {
|
||||
return false;
|
||||
}
|
||||
if (!testAT()) { return false; }
|
||||
|
||||
sendAT(GF("E0")); // Echo Off
|
||||
if (waitResponse() != 1) {
|
||||
return false;
|
||||
}
|
||||
sendAT(GF("E0")); // Echo Off
|
||||
if (waitResponse() != 1) { return false; }
|
||||
|
||||
#ifdef TINY_GSM_DEBUG
|
||||
sendAT(GF("+CMEE=2")); // turn on verbose error codes
|
||||
@@ -193,176 +164,127 @@ public:
|
||||
|
||||
getModemName();
|
||||
|
||||
// Enable automatic time zome update
|
||||
sendAT(GF("+CTZU=1"));
|
||||
if (waitResponse(10000L) != 1) { return false; }
|
||||
|
||||
int 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) {
|
||||
simUnlock(pin);
|
||||
return (getSimStatus() == SIM_READY);
|
||||
}
|
||||
// if the sim is ready, or it's locked but no pin has been provided, return true
|
||||
else {
|
||||
} else {
|
||||
// if the sim is ready, or it's locked but no pin has been provided,
|
||||
// return true
|
||||
return (ret == SIM_READY || ret == SIM_LOCKED);
|
||||
}
|
||||
}
|
||||
|
||||
String getModemName() {
|
||||
String getModemNameImpl() {
|
||||
sendAT(GF("+CGMI"));
|
||||
String res1;
|
||||
if (waitResponse(1000L, res1) != 1) {
|
||||
return "u-blox Cellular Modem";
|
||||
}
|
||||
if (waitResponse(1000L, res1) != 1) { return "u-blox Cellular Modem"; }
|
||||
res1.replace(GSM_NL "OK" GSM_NL, "");
|
||||
res1.trim();
|
||||
|
||||
sendAT(GF("+GMM"));
|
||||
String res2;
|
||||
if (waitResponse(1000L, res2) != 1) {
|
||||
return "u-blox Cellular Modem";
|
||||
}
|
||||
if (waitResponse(1000L, res2) != 1) { return "u-blox Cellular Modem"; }
|
||||
res2.replace(GSM_NL "OK" GSM_NL, "");
|
||||
res2.trim();
|
||||
|
||||
String name = res1 + String(' ') + res2;
|
||||
DBG("### Modem:", name);
|
||||
if (name.startsWith("u-blox SARA-R4") || name.startsWith("u-blox SARA-N4")) {
|
||||
if (name.startsWith("u-blox SARA-R4") ||
|
||||
name.startsWith("u-blox SARA-N4")) {
|
||||
DBG("### WARNING: You are using the wrong TinyGSM modem!");
|
||||
}
|
||||
else if (name.startsWith("u-blox SARA-N2")) {
|
||||
} else if (name.startsWith("u-blox SARA-N2")) {
|
||||
DBG("### SARA N2 NB-IoT modems not supported!");
|
||||
}
|
||||
|
||||
return name;
|
||||
}
|
||||
|
||||
TINY_GSM_MODEM_SET_BAUD_IPR()
|
||||
|
||||
TINY_GSM_MODEM_TEST_AT()
|
||||
|
||||
TINY_GSM_MODEM_MAINTAIN_CHECK_SOCKS()
|
||||
|
||||
bool factoryDefault() {
|
||||
bool factoryDefaultImpl() {
|
||||
sendAT(GF("+UFACTORY=0,1")); // No factory restore, erase NVM
|
||||
waitResponse();
|
||||
sendAT(GF("+CFUN=16")); // Reset
|
||||
sendAT(GF("+CFUN=16")); // Reset
|
||||
return waitResponse() == 1;
|
||||
}
|
||||
|
||||
TINY_GSM_MODEM_GET_INFO_ATI()
|
||||
|
||||
bool hasSSL() {
|
||||
bool thisHasSSL() {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool hasWifi() {
|
||||
bool thisHasWifi() {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool hasGPRS() {
|
||||
bool thisHasGPRS() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
* Power functions
|
||||
*/
|
||||
|
||||
bool restart() {
|
||||
if (!testAT()) {
|
||||
return false;
|
||||
}
|
||||
protected:
|
||||
bool restartImpl() {
|
||||
if (!testAT()) { return false; }
|
||||
sendAT(GF("+CFUN=16"));
|
||||
if (waitResponse(10000L) != 1) {
|
||||
return false;
|
||||
}
|
||||
delay(3000); // TODO: Verify delay timing here
|
||||
if (waitResponse(10000L) != 1) { return false; }
|
||||
delay(3000); // TODO(?): Verify delay timing here
|
||||
return init();
|
||||
}
|
||||
|
||||
bool poweroff() {
|
||||
bool powerOffImpl() {
|
||||
sendAT(GF("+CPWROFF"));
|
||||
return waitResponse(40000L) == 1;
|
||||
}
|
||||
|
||||
bool radioOff() {
|
||||
sendAT(GF("+CFUN=0"));
|
||||
if (waitResponse(10000L) != 1) {
|
||||
return false;
|
||||
}
|
||||
delay(3000);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool sleepEnable(bool enable = true) TINY_GSM_ATTR_NOT_IMPLEMENTED;
|
||||
bool sleepEnableImpl(bool enable = true) TINY_GSM_ATTR_NOT_IMPLEMENTED;
|
||||
|
||||
/*
|
||||
* SIM card functions
|
||||
*/
|
||||
|
||||
TINY_GSM_MODEM_SIM_UNLOCK_CPIN()
|
||||
|
||||
TINY_GSM_MODEM_GET_SIMCCID_CCID()
|
||||
|
||||
String getIMEI() {
|
||||
protected:
|
||||
String getIMEIImpl() {
|
||||
sendAT(GF("+CGSN"));
|
||||
if (waitResponse(GF(GSM_NL)) != 1) {
|
||||
return "";
|
||||
}
|
||||
if (waitResponse(GF(GSM_NL)) != 1) { return ""; }
|
||||
String res = stream.readStringUntil('\n');
|
||||
waitResponse();
|
||||
res.trim();
|
||||
return res;
|
||||
}
|
||||
|
||||
SimStatus getSimStatus(unsigned long timeout_ms = 10000L) {
|
||||
for (unsigned long start = millis(); millis() - start < timeout_ms; ) {
|
||||
sendAT(GF("+CPIN?"));
|
||||
if (waitResponse(GF(GSM_NL "+CPIN:")) != 1) {
|
||||
delay(1000);
|
||||
continue;
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
return SIM_ERROR;
|
||||
}
|
||||
|
||||
TINY_GSM_MODEM_GET_REGISTRATION_XREG(CGREG)
|
||||
|
||||
TINY_GSM_MODEM_GET_OPERATOR_COPS()
|
||||
|
||||
/*
|
||||
* Generic network functions
|
||||
*/
|
||||
public:
|
||||
RegStatus getRegistrationStatus() {
|
||||
return (RegStatus)getRegistrationStatusXREG("CGREG");
|
||||
}
|
||||
|
||||
TINY_GSM_MODEM_GET_CSQ()
|
||||
|
||||
bool isNetworkConnected() {
|
||||
protected:
|
||||
bool isNetworkConnectedImpl() {
|
||||
RegStatus s = getRegistrationStatus();
|
||||
if (s == REG_OK_HOME || s == REG_OK_ROAMING)
|
||||
return true;
|
||||
else if (s == REG_UNKNOWN) // for some reason, it can hang at unknown..
|
||||
return isGprsConnected();
|
||||
else return false;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
TINY_GSM_MODEM_WAIT_FOR_NETWORK()
|
||||
|
||||
/*
|
||||
* GPRS functions
|
||||
*/
|
||||
|
||||
bool gprsConnect(const char* apn, const char* user = NULL, const char* pwd = NULL) {
|
||||
protected:
|
||||
bool gprsConnectImpl(const char* apn, const char* user = NULL,
|
||||
const char* pwd = NULL) {
|
||||
gprsDisconnect();
|
||||
|
||||
sendAT(GF("+CGATT=1")); // attach to GPRS
|
||||
if (waitResponse(360000L) != 1) {
|
||||
return false;
|
||||
}
|
||||
if (waitResponse(360000L) != 1) { return false; }
|
||||
|
||||
// Setting up the PSD profile/PDP context with the UPSD commands sets up an
|
||||
// "internal" PDP context, i.e. a data connection using the internal IP
|
||||
@@ -388,15 +310,16 @@ TINY_GSM_MODEM_WAIT_FOR_NETWORK()
|
||||
waitResponse();
|
||||
}
|
||||
|
||||
sendAT(GF("+UPSD=0,7,\"0.0.0.0\"")); // Dynamic IP on PSD profile 0
|
||||
sendAT(GF("+UPSD=0,7,\"0.0.0.0\"")); // Dynamic IP on PSD profile 0
|
||||
waitResponse();
|
||||
|
||||
// Packet switched data action
|
||||
// AT+UPSDA=<profile_id>,<action>
|
||||
// profile_id = 0: PSD profile identifier, in range 0-6 (NOT PDP context)
|
||||
// action = 3: activate; it activates a PDP context with the specified profile,
|
||||
// using the current parameters
|
||||
sendAT(GF("+UPSDA=0,3")); // Activate the PDP context associated with profile 0
|
||||
// action = 3: activate; it activates a PDP context with the specified
|
||||
// profile, using the current parameters
|
||||
sendAT(GF(
|
||||
"+UPSDA=0,3")); // Activate the PDP context associated with profile 0
|
||||
if (waitResponse(360000L) != 1) { // Should return ok
|
||||
return false;
|
||||
}
|
||||
@@ -406,12 +329,13 @@ TINY_GSM_MODEM_WAIT_FOR_NETWORK()
|
||||
// for the active PDP context associated with the specified PSD profile.
|
||||
// AT+UPSND=<profile_id>,<param_tag>
|
||||
// profile_id = 0: PSD profile identifier, in range 0-6 (NOT PDP context)
|
||||
// param_tag = 8: PSD profile status: if the profile is active the return value is 1, 0 otherwise
|
||||
sendAT(GF("+UPSND=0,8")); // Check if PSD profile 0 is now active
|
||||
// param_tag = 8: PSD profile status: if the profile is active the return
|
||||
// value is 1, 0 otherwise
|
||||
sendAT(GF("+UPSND=0,8")); // Check if PSD profile 0 is now active
|
||||
int res = waitResponse(GF(",8,1"), GF(",8,0"));
|
||||
waitResponse(); // Should return another OK
|
||||
if (res == 1) {
|
||||
return true; // It's now active
|
||||
return true; // It's now active
|
||||
} else if (res == 2) { // If it's not active yet, wait for the +UUPSDA URC
|
||||
if (waitResponse(180000L, GF("+UUPSDA: 0")) != 1) { // 0=successful
|
||||
return false;
|
||||
@@ -424,138 +348,109 @@ TINY_GSM_MODEM_WAIT_FOR_NETWORK()
|
||||
return true;
|
||||
}
|
||||
|
||||
bool gprsDisconnect() {
|
||||
sendAT(GF("+UPSDA=0,4")); // Deactivate the PDP context associated with profile 0
|
||||
if (waitResponse(360000L) != 1) {
|
||||
return false;
|
||||
}
|
||||
bool gprsDisconnectImpl() {
|
||||
sendAT(GF(
|
||||
"+UPSDA=0,4")); // Deactivate the PDP context associated with profile 0
|
||||
if (waitResponse(360000L) != 1) { return false; }
|
||||
|
||||
sendAT(GF("+CGATT=0")); // detach from GPRS
|
||||
if (waitResponse(360000L) != 1) {
|
||||
return false;
|
||||
}
|
||||
if (waitResponse(360000L) != 1) { return false; }
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
TINY_GSM_MODEM_GET_GPRS_IP_CONNECTED()
|
||||
|
||||
/*
|
||||
* IP Address functions
|
||||
*/
|
||||
|
||||
String getLocalIP() {
|
||||
protected:
|
||||
String getLocalIPImpl() {
|
||||
sendAT(GF("+UPSND=0,0"));
|
||||
if (waitResponse(GF(GSM_NL "+UPSND:")) != 1) {
|
||||
return "";
|
||||
}
|
||||
streamSkipUntil(','); // Skip PSD profile
|
||||
streamSkipUntil('\"'); // Skip request type
|
||||
if (waitResponse(GF(GSM_NL "+UPSND:")) != 1) { return ""; }
|
||||
streamSkipUntil(','); // Skip PSD profile
|
||||
streamSkipUntil('\"'); // Skip request type
|
||||
String res = stream.readStringUntil('\"');
|
||||
if (waitResponse() != 1) {
|
||||
return "";
|
||||
}
|
||||
if (waitResponse() != 1) { return ""; }
|
||||
return res;
|
||||
}
|
||||
|
||||
IPAddress localIP() {
|
||||
return TinyGsmIpFromString(getLocalIP());
|
||||
}
|
||||
|
||||
/*
|
||||
* Phone Call functions
|
||||
*/
|
||||
|
||||
bool setGsmBusy(bool busy = true) TINY_GSM_ATTR_NOT_AVAILABLE;
|
||||
|
||||
bool callAnswer() TINY_GSM_ATTR_NOT_IMPLEMENTED;
|
||||
|
||||
bool callNumber(const String& number) TINY_GSM_ATTR_NOT_IMPLEMENTED;
|
||||
|
||||
bool callHangup() TINY_GSM_ATTR_NOT_IMPLEMENTED;
|
||||
protected:
|
||||
// Can follow all of the phone call functions from the template
|
||||
|
||||
/*
|
||||
* Messaging functions
|
||||
*/
|
||||
|
||||
String sendUSSD(const String& code) TINY_GSM_ATTR_NOT_IMPLEMENTED;
|
||||
|
||||
bool sendSMS(const String& number, const String& text) {
|
||||
sendAT(GF("+CSCS=\"GSM\"")); // Set GSM default alphabet
|
||||
waitResponse();
|
||||
sendAT(GF("+CMGF=1")); // Set preferred message format to text mode
|
||||
waitResponse();
|
||||
sendAT(GF("+CMGS=\""), number, GF("\"")); // set the phone number
|
||||
if (waitResponse(GF(">")) != 1) {
|
||||
return false;
|
||||
}
|
||||
stream.print(text); // Actually send the message
|
||||
stream.write((char)0x1A);
|
||||
stream.flush();
|
||||
return waitResponse(60000L) == 1;
|
||||
}
|
||||
|
||||
bool sendSMS_UTF16(const String& number, const void* text, size_t len) TINY_GSM_ATTR_NOT_IMPLEMENTED;
|
||||
|
||||
protected:
|
||||
// Can follow all template functions
|
||||
|
||||
/*
|
||||
* Location functions
|
||||
*/
|
||||
|
||||
String getGsmLocation() {
|
||||
protected:
|
||||
String getGsmLocationImpl() {
|
||||
sendAT(GF("+ULOC=2,3,0,120,1"));
|
||||
if (waitResponse(30000L, GF(GSM_NL "+UULOC:")) != 1) {
|
||||
return "";
|
||||
}
|
||||
if (waitResponse(30000L, GF(GSM_NL "+UULOC:")) != 1) { return ""; }
|
||||
String res = stream.readStringUntil('\n');
|
||||
waitResponse();
|
||||
res.trim();
|
||||
return res;
|
||||
}
|
||||
|
||||
/*
|
||||
* GPS location functions
|
||||
*/
|
||||
public:
|
||||
// No functions of this type supported
|
||||
|
||||
/*
|
||||
* Time functions
|
||||
*/
|
||||
protected:
|
||||
// Can follow the standard CCLK function in the template
|
||||
|
||||
/*
|
||||
* Battery & temperature functions
|
||||
*/
|
||||
protected:
|
||||
uint16_t getBattVoltageImpl() TINY_GSM_ATTR_NOT_AVAILABLE;
|
||||
|
||||
uint16_t getBattVoltage() TINY_GSM_ATTR_NOT_AVAILABLE;
|
||||
|
||||
int8_t getBattPercent() {
|
||||
int8_t getBattPercentImpl() {
|
||||
sendAT(GF("+CIND?"));
|
||||
if (waitResponse(GF(GSM_NL "+CIND:")) != 1) {
|
||||
return 0;
|
||||
}
|
||||
if (waitResponse(GF(GSM_NL "+CIND:")) != 1) { return 0; }
|
||||
|
||||
int res = stream.readStringUntil(',').toInt();
|
||||
int8_t percent = res*20; // return is 0-5
|
||||
int res = stream.readStringUntil(',').toInt();
|
||||
int8_t percent = res * 20; // return is 0-5
|
||||
// Wait for final OK
|
||||
waitResponse();
|
||||
return percent;
|
||||
}
|
||||
|
||||
uint8_t getBattChargeState() TINY_GSM_ATTR_NOT_AVAILABLE;
|
||||
uint8_t getBattChargeStateImpl() TINY_GSM_ATTR_NOT_AVAILABLE;
|
||||
|
||||
bool getBattStats(uint8_t &chargeState, int8_t &percent, uint16_t &milliVolts) {
|
||||
bool getBattStatsImpl(uint8_t& chargeState, int8_t& percent,
|
||||
uint16_t& milliVolts) {
|
||||
chargeState = 0;
|
||||
percent = getBattPercent();
|
||||
milliVolts = 0;
|
||||
percent = getBattPercent();
|
||||
milliVolts = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
// This would only available for a small number of modules in this group (TOBY-L)
|
||||
float getTemperature() TINY_GSM_ATTR_NOT_IMPLEMENTED;
|
||||
// This would only available for a small number of modules in this group
|
||||
// (TOBY-L)
|
||||
float getTemperatureImpl() TINY_GSM_ATTR_NOT_IMPLEMENTED;
|
||||
|
||||
/*
|
||||
* Client related functions
|
||||
*/
|
||||
|
||||
protected:
|
||||
|
||||
protected:
|
||||
bool modemConnect(const char* host, uint16_t port, uint8_t* mux,
|
||||
bool ssl = false, int timeout_s = 120)
|
||||
{
|
||||
uint32_t timeout_ms = ((uint32_t)timeout_s)*1000;
|
||||
bool ssl = false, int timeout_s = 120) {
|
||||
uint32_t timeout_ms = ((uint32_t)timeout_s) * 1000;
|
||||
sendAT(GF("+USOCR=6")); // create a socket
|
||||
if (waitResponse(GF(GSM_NL "+USOCR:")) != 1) { // reply is +USOCR: ## of socket created
|
||||
if (waitResponse(GF(GSM_NL "+USOCR:")) !=
|
||||
1) { // reply is +USOCR: ## of socket created
|
||||
return false;
|
||||
}
|
||||
*mux = stream.readStringUntil('\n').toInt();
|
||||
@@ -571,8 +466,8 @@ protected:
|
||||
waitResponse();
|
||||
|
||||
// Enable KEEPALIVE, 30 sec
|
||||
//sendAT(GF("+USOSO="), *mux, GF(",6,2,30000"));
|
||||
//waitResponse();
|
||||
// sendAT(GF("+USOSO="), *mux, GF(",6,2,30000"));
|
||||
// waitResponse();
|
||||
|
||||
// connect on the allocated socket
|
||||
sendAT(GF("+USOCO="), *mux, ",\"", host, "\",", port);
|
||||
@@ -582,17 +477,13 @@ protected:
|
||||
|
||||
int16_t modemSend(const void* buff, size_t len, uint8_t mux) {
|
||||
sendAT(GF("+USOWR="), mux, ',', (uint16_t)len);
|
||||
if (waitResponse(GF("@")) != 1) {
|
||||
return 0;
|
||||
}
|
||||
if (waitResponse(GF("@")) != 1) { return 0; }
|
||||
// 50ms delay, see AT manual section 25.10.4
|
||||
delay(50);
|
||||
stream.write((uint8_t*)buff, len);
|
||||
stream.write(reinterpret_cast<const uint8_t*>(buff), len);
|
||||
stream.flush();
|
||||
if (waitResponse(GF(GSM_NL "+USOWR:")) != 1) {
|
||||
return 0;
|
||||
}
|
||||
streamSkipUntil(','); // Skip mux
|
||||
if (waitResponse(GF(GSM_NL "+USOWR:")) != 1) { return 0; }
|
||||
streamSkipUntil(','); // Skip mux
|
||||
int sent = stream.readStringUntil('\n').toInt();
|
||||
waitResponse(); // sends back OK after the confirmation of number sent
|
||||
return sent;
|
||||
@@ -600,16 +491,12 @@ protected:
|
||||
|
||||
size_t modemRead(size_t size, uint8_t mux) {
|
||||
sendAT(GF("+USORD="), mux, ',', (uint16_t)size);
|
||||
if (waitResponse(GF(GSM_NL "+USORD:")) != 1) {
|
||||
return 0;
|
||||
}
|
||||
streamSkipUntil(','); // Skip mux
|
||||
if (waitResponse(GF(GSM_NL "+USORD:")) != 1) { return 0; }
|
||||
streamSkipUntil(','); // Skip mux
|
||||
int len = stream.readStringUntil(',').toInt();
|
||||
streamSkipUntil('\"');
|
||||
|
||||
for (int i=0; i<len; i++) {
|
||||
TINY_GSM_MODEM_STREAM_TO_MUX_FIFO_WITH_DOUBLE_TIMEOUT
|
||||
}
|
||||
for (int i = 0; i < len; i++) { moveCharFromStreamToFifo(mux); }
|
||||
streamSkipUntil('\"');
|
||||
waitResponse();
|
||||
DBG("### READ:", len, "from", mux);
|
||||
@@ -620,19 +507,17 @@ protected:
|
||||
size_t modemGetAvailable(uint8_t mux) {
|
||||
// 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:"));
|
||||
size_t result = 0;
|
||||
uint8_t res = waitResponse(GF(GSM_NL "+USORD:"));
|
||||
// Will give error "operation not allowed" when attempting to read a socket
|
||||
// that you have already told to close
|
||||
if (res == 1) {
|
||||
streamSkipUntil(','); // Skip mux
|
||||
streamSkipUntil(','); // Skip mux
|
||||
result = stream.readStringUntil('\n').toInt();
|
||||
// if (result) DBG("### DATA AVAILABLE:", result, "on", mux);
|
||||
waitResponse();
|
||||
}
|
||||
if (!result) {
|
||||
sockets[mux]->sock_connected = modemGetConnected(mux);
|
||||
}
|
||||
if (!result) { sockets[mux]->sock_connected = modemGetConnected(mux); }
|
||||
DBG("### AVAILABLE:", result, "on", mux);
|
||||
return result;
|
||||
}
|
||||
@@ -641,11 +526,10 @@ protected:
|
||||
// NOTE: Querying a closed socket gives an error "operation not allowed"
|
||||
sendAT(GF("+USOCTL="), mux, ",10");
|
||||
uint8_t res = waitResponse(GF(GSM_NL "+USOCTL:"));
|
||||
if (res != 1)
|
||||
return false;
|
||||
if (res != 1) { return false; }
|
||||
|
||||
streamSkipUntil(','); // Skip mux
|
||||
streamSkipUntil(','); // Skip type
|
||||
streamSkipUntil(','); // Skip mux
|
||||
streamSkipUntil(','); // Skip type
|
||||
int result = stream.readStringUntil('\n').toInt();
|
||||
// 0: the socket is in INACTIVE status (it corresponds to CLOSED status
|
||||
// defined in RFC793 "TCP Protocol Specification" [112])
|
||||
@@ -663,19 +547,16 @@ protected:
|
||||
return (result != 0);
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
/*
|
||||
Utilities
|
||||
* Utilities
|
||||
*/
|
||||
|
||||
TINY_GSM_MODEM_STREAM_UTILITIES()
|
||||
|
||||
// TODO: Optimize this!
|
||||
uint8_t waitResponse(uint32_t timeout_ms, String& data,
|
||||
GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR),
|
||||
GsmConstStr r3=GFP(GSM_CME_ERROR), GsmConstStr r4=NULL, GsmConstStr r5=NULL)
|
||||
{
|
||||
public:
|
||||
// TODO(vshymanskyy): Optimize this!
|
||||
uint8_t
|
||||
waitResponse(uint32_t timeout_ms, String& data, GsmConstStr r1 = GFP(GSM_OK),
|
||||
GsmConstStr r2 = GFP(GSM_ERROR),
|
||||
GsmConstStr r3 = GFP(GSM_CME_ERROR), GsmConstStr r4 = NULL,
|
||||
GsmConstStr r5 = NULL) {
|
||||
/*String r1s(r1); r1s.trim();
|
||||
String r2s(r2); r2s.trim();
|
||||
String r3s(r3); r3s.trim();
|
||||
@@ -683,15 +564,15 @@ TINY_GSM_MODEM_STREAM_UTILITIES()
|
||||
String r5s(r5); r5s.trim();
|
||||
DBG("### ..:", r1s, ",", r2s, ",", r3s, ",", r4s, ",", r5s);*/
|
||||
data.reserve(64);
|
||||
int index = 0;
|
||||
unsigned long startMillis = millis();
|
||||
int index = 0;
|
||||
uint32_t startMillis = millis();
|
||||
do {
|
||||
TINY_GSM_YIELD();
|
||||
while (stream.available() > 0) {
|
||||
TINY_GSM_YIELD();
|
||||
int a = stream.read();
|
||||
if (a <= 0) continue; // Skip 0x00 bytes, just in case
|
||||
data += (char)a;
|
||||
if (a <= 0) continue; // Skip 0x00 bytes, just in case
|
||||
data += static_cast<char>(a);
|
||||
if (r1 && data.endsWith(r1)) {
|
||||
index = 1;
|
||||
goto finish;
|
||||
@@ -714,7 +595,7 @@ TINY_GSM_MODEM_STREAM_UTILITIES()
|
||||
int mux = stream.readStringUntil(',').toInt();
|
||||
int len = stream.readStringUntil('\n').toInt();
|
||||
if (mux >= 0 && mux < TINY_GSM_MUX_COUNT && sockets[mux]) {
|
||||
sockets[mux]->got_data = true;
|
||||
sockets[mux]->got_data = true;
|
||||
sockets[mux]->sock_available = len;
|
||||
}
|
||||
data = "";
|
||||
@@ -729,38 +610,36 @@ TINY_GSM_MODEM_STREAM_UTILITIES()
|
||||
}
|
||||
}
|
||||
} while (millis() - startMillis < timeout_ms);
|
||||
finish:
|
||||
finish:
|
||||
if (!index) {
|
||||
data.trim();
|
||||
if (data.length()) {
|
||||
DBG("### Unhandled:", data);
|
||||
}
|
||||
if (data.length()) { DBG("### Unhandled:", data); }
|
||||
data = "";
|
||||
}
|
||||
//data.replace(GSM_NL, "/");
|
||||
//DBG('<', index, '>', data);
|
||||
// data.replace(GSM_NL, "/");
|
||||
// DBG('<', index, '>', data);
|
||||
return index;
|
||||
}
|
||||
|
||||
uint8_t waitResponse(uint32_t timeout_ms,
|
||||
GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR),
|
||||
GsmConstStr r3=GFP(GSM_CME_ERROR), GsmConstStr r4=NULL, GsmConstStr r5=NULL)
|
||||
{
|
||||
uint8_t waitResponse(uint32_t timeout_ms, GsmConstStr r1 = GFP(GSM_OK),
|
||||
GsmConstStr r2 = GFP(GSM_ERROR),
|
||||
GsmConstStr r3 = GFP(GSM_CME_ERROR),
|
||||
GsmConstStr r4 = NULL, GsmConstStr r5 = NULL) {
|
||||
String data;
|
||||
return waitResponse(timeout_ms, data, r1, r2, r3, r4, r5);
|
||||
}
|
||||
|
||||
uint8_t waitResponse(GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR),
|
||||
GsmConstStr r3=GFP(GSM_CME_ERROR), GsmConstStr r4=NULL, GsmConstStr r5=NULL)
|
||||
{
|
||||
uint8_t
|
||||
waitResponse(GsmConstStr r1 = GFP(GSM_OK), GsmConstStr r2 = GFP(GSM_ERROR),
|
||||
GsmConstStr r3 = GFP(GSM_CME_ERROR), GsmConstStr r4 = NULL,
|
||||
GsmConstStr r5 = NULL) {
|
||||
return waitResponse(1000, r1, r2, r3, r4, r5);
|
||||
}
|
||||
|
||||
public:
|
||||
Stream& stream;
|
||||
|
||||
protected:
|
||||
GsmClient* sockets[TINY_GSM_MUX_COUNT];
|
||||
protected:
|
||||
Stream& stream;
|
||||
GsmClientUBLOX* sockets[TINY_GSM_MUX_COUNT];
|
||||
const char* gsmNL = GSM_NL;
|
||||
};
|
||||
|
||||
#endif
|
||||
#endif // SRC_TINYGSMCLIENTUBLOX_H_
|
||||
|
||||
Reference in New Issue
Block a user