Aktualizace varovani z clang-tidy, prace na RING (zatim neuspesna...)

This commit is contained in:
Pavel Brychta 2023-09-02 12:02:37 +02:00
parent e2c5e5dcd9
commit e6ba94d9e3
2 changed files with 479 additions and 465 deletions

231
src/DTE.h
View File

@ -1,120 +1,125 @@
/* /*
* DTE.h * DTE.h
* *
* Created: 20/09/2016 15:40:51 * Created: 20/09/2016 15:40:51
* Author: Neta Yahav * Author: Neta Yahav
*/ */
#pragma once #pragma once
#include <Arduino.h> #include <Arduino.h>
class DTE class DTE {
{ public:
public: enum CommandResult {
enum CommandResult EXPECT_BUSY,
{ EXPECT_TIMEOUT,
EXPECT_BUSY, EXPECT_DELAY,
EXPECT_TIMEOUT, EXPECT_RESULT,
EXPECT_DELAY,
EXPECT_RESULT,
EXPECT_RING EXPECT_RING
}; };
private: private:
String buffer; String buffer;
Stream& stream; Stream & stream;
unsigned int bufferIndex; unsigned int bufferIndex;
unsigned int bufferSize; unsigned int bufferSize;
String response[3]; String response[3];
unsigned long timeout; unsigned long timeout;
unsigned long tick; unsigned long tick;
unsigned int match; unsigned int match;
CommandResult result; CommandResult result;
public: public:
DTE(Stream& stream, unsigned int size) DTE(Stream & stream, unsigned int size)
: stream(stream) : stream(stream), bufferSize(size), result(EXPECT_RESULT)
, bufferSize(size) {
, result(EXPECT_RESULT) { buffer.reserve(size);
buffer.reserve(size); }
}
~DTE() {}; ~DTE() = default;
void SendCommand(const char* command, unsigned long timeout, const char* response1, const char* response2 = 0, const char* response3 = 0) { void SendCommand(const char * command, unsigned long timeout, const char * response1, const char * response2 = 0, const char * response3 = 0)
match = 0; {
result = EXPECT_BUSY; match = 0;
response[0] = response1; result = EXPECT_BUSY;
response[1] = response2; response[0] = response1;
response[2] = response3; response[1] = response2;
this->timeout = timeout; response[2] = response3;
this->timeout = timeout;
flush(); flush();
stream.print(command); stream.print(command);
buffer = ""; buffer = "";
tick = millis(); tick = millis();
proccess(); proccess();
} }
void SendCommand(const __FlashStringHelper* command, unsigned long timeout, const char* response1, const char* response2 = 0, const char* response3 = 0) { void SendCommand(const __FlashStringHelper * command, unsigned long timeout, const char * response1, const char * response2 = 0, const char * response3 = 0)
{
match = 0; match = 0;
result = EXPECT_BUSY; result = EXPECT_BUSY;
response[0] = response1; response[0] = response1;
response[1] = response2; response[1] = response2;
response[2] = response3; response[2] = response3;
this->timeout = timeout; this->timeout = timeout;
// clear rx buffer // clear rx buffer
flush(); flush();
// send command // send command
stream.print((const __FlashStringHelper *)command); stream.print((const __FlashStringHelper *) command);
buffer = ""; buffer = "";
tick = millis(); tick = millis();
proccess(); proccess();
} }
void Delay(unsigned long delay) { void Delay(unsigned long delay)
timeout = delay; {
result = EXPECT_DELAY; timeout = delay;
tick = millis(); result = EXPECT_DELAY;
proccess(); tick = millis();
} proccess();
}
bool getIsBusy(void) { bool getIsBusy()
proccess(); {
return (result == EXPECT_BUSY) || (result == EXPECT_DELAY); proccess();
} return (result == EXPECT_BUSY) || (result == EXPECT_DELAY);
}
CommandResult getResult(void) { CommandResult getResult()
{
return result; return result;
} }
unsigned int getMatch(void) { unsigned int getMatch() const
{
return match; return match;
} }
String& getBuffer(void) { String & getBuffer()
{
return buffer; return buffer;
} }
private: private:
void flush()
void flush(void) { {
// clear rx buffer // clear rx buffer
while (stream.available()) { while (stream.available()) {
stream.read(); stream.read();
} }
} }
void proccess(void) { void proccess()
if (result == EXPECT_DELAY) { {
if(millis() - tick >= timeout) if (result == EXPECT_DELAY) {
result = EXPECT_RESULT; if (millis() - tick >= timeout)
result = EXPECT_RESULT;
return; return;
} }
if (result != EXPECT_BUSY) { if (result != EXPECT_BUSY) {
if (stream.available()) { if (stream.available()) {
char c; char c;
@ -130,36 +135,36 @@ private:
return; return;
} }
char c; char c;
unsigned long now = millis(); unsigned long now = millis();
while (millis() - tick < timeout) { while (millis() - tick < timeout) {
while (stream.available() && (buffer.length() < bufferSize)) { while (stream.available() && (buffer.length() < bufferSize)) {
c = stream.read(); c = stream.read();
buffer += c; buffer += c;
if (buffer.endsWith(response[0])) { if (buffer.endsWith(response[0])) {
match = 0; match = 0;
result = EXPECT_RESULT; result = EXPECT_RESULT;
return; return;
} else if (response[1].length() != 0) { } else if (response[1].length() != 0) {
if (buffer.endsWith(response[1])) { if (buffer.endsWith(response[1])) {
match = 1; match = 1;
result = EXPECT_RESULT; result = EXPECT_RESULT;
return; return;
} }
} else if (response[2].length() != 0) { } else if (response[2].length() != 0) {
if (buffer.endsWith(response[2])) { if (buffer.endsWith(response[2])) {
match = 2; match = 2;
result = EXPECT_RESULT; result = EXPECT_RESULT;
return; return;
} }
} }
} }
if (millis() - now > 5) if (millis() - now > 5)
return; return;
} }
// time out // time out
result = EXPECT_TIMEOUT; result = EXPECT_TIMEOUT;
} }
}; //DTE }; // DTE

View File

@ -1,51 +1,49 @@
/* /*
* ThreadedGSM.h * ThreadedGSM.h
* *
* Created: 20/09/2016 11:14:02 * Created: 20/09/2016 11:14:02
* Author: Neta Yahav * Author: Neta Yahav
*/ */
#pragma once #pragma once
#include <Arduino.h>
#include "DTE.h" #include "DTE.h"
#include <Arduino.h>
// Defaults // Defaults
#define THREADEDGSM_DEF_DTE_BUF_SIZ 512 #define THREADEDGSM_DEF_DTE_BUF_SIZ 512
#define THREADEDGSM_DEF_AT_TIMEOUT 5000 #define THREADEDGSM_DEF_AT_TIMEOUT 5000
#define THREADEDGSM_DEF_STA_PON 10000 #define THREADEDGSM_DEF_STA_PON 10000
#define THREADEDGSM_DEF_STA_POF 1000 #define THREADEDGSM_DEF_STA_POF 1000
// Use custom values or default ones // Use custom values or default ones
#ifndef THREADEDGSM_DTE_BUFFER_SIZE #ifndef THREADEDGSM_DTE_BUFFER_SIZE
# define THREADEDGSM_DTE_BUFFER_SIZE THREADEDGSM_DEF_DTE_BUF_SIZ #define THREADEDGSM_DTE_BUFFER_SIZE THREADEDGSM_DEF_DTE_BUF_SIZ
#endif #endif
#ifndef THREADEDGSM_AT_TIMEOUT #ifndef THREADEDGSM_AT_TIMEOUT
# define THREADEDGSM_AT_TIMEOUT THREADEDGSM_DEF_AT_TIMEOUT #define THREADEDGSM_AT_TIMEOUT THREADEDGSM_DEF_AT_TIMEOUT
#endif #endif
#ifndef THREADEDGSM_STARTUP_DELAY #ifndef THREADEDGSM_STARTUP_DELAY
# define THREADEDGSM_STARTUP_DELAY THREADEDGSM_DEF_STA_PON #define THREADEDGSM_STARTUP_DELAY THREADEDGSM_DEF_STA_PON
#endif #endif
#ifndef THREADEDGSM_STARTUP_POWER_OFF_DELAY #ifndef THREADEDGSM_STARTUP_POWER_OFF_DELAY
# define THREADEDGSM_STARTUP_POWER_OFF_DELAY THREADEDGSM_DEF_STA_POF #define THREADEDGSM_STARTUP_POWER_OFF_DELAY THREADEDGSM_DEF_STA_POF
#endif #endif
#define THREADEDGSM_INTERVAL_COUNT 4 #define THREADEDGSM_INTERVAL_COUNT 4
#ifdef THREADEDGSM_DEBUG #ifdef THREADEDGSM_DEBUG
# define DEBUG_PRINT(x) THREADEDGSM_DEBUG.print (x) #define DEBUG_PRINT(x) THREADEDGSM_DEBUG.print(x)
# define DEBUG_PRINTLN(x) THREADEDGSM_DEBUG.println (x) #define DEBUG_PRINTLN(x) THREADEDGSM_DEBUG.println(x)
#else #else
# define DEBUG_PRINT(x) #define DEBUG_PRINT(x)
# define DEBUG_PRINTLN(x) #define DEBUG_PRINTLN(x)
#endif #endif
class ThreadedGSM class ThreadedGSM {
{ // variables
//variables public:
public: struct NetworkTime {
struct NetworkTime
{
int year; int year;
int month; int month;
int day; int day;
@ -62,41 +60,36 @@ public:
READ_TYPE_ALL = 4 READ_TYPE_ALL = 4
}; };
enum IntervalSourceE enum IntervalSourceE {
{
INTERVAL_CLOCK, INTERVAL_CLOCK,
INTERVAL_INBOX, INTERVAL_INBOX,
INTERVAL_SIGNAL, INTERVAL_SIGNAL,
INTERVAL_BATTERY INTERVAL_BATTERY
}; };
struct SignalLevel struct SignalLevel {
{
int Dbm; int Dbm;
int Value; int Value;
}; };
struct BatteryInfo struct BatteryInfo {
{
int Percent; int Percent;
int Voltage; int Voltage;
}; };
struct SMSInfo struct SMSInfo {
{
String Number; String Number;
String Text; String Text;
}; };
typedef void (*ThreadedGSMCallbackSignal)(ThreadedGSM&, SignalLevel&); typedef void (*ThreadedGSMCallbackSignal)(ThreadedGSM &, SignalLevel &);
typedef void (*ThreadedGSMCallbackClock)(ThreadedGSM&, NetworkTime&); typedef void (*ThreadedGSMCallbackClock)(ThreadedGSM &, NetworkTime &);
typedef void (*ThreadedGSMCallbackBattery)(ThreadedGSM&, BatteryInfo&); typedef void (*ThreadedGSMCallbackBattery)(ThreadedGSM &, BatteryInfo &);
typedef void (*ThreadedGSMCallbackIncomingSMS)(ThreadedGSM&, SMSInfo&); typedef void (*ThreadedGSMCallbackIncomingSMS)(ThreadedGSM &, SMSInfo &);
typedef void (*ThreadedGSMCallbackBool)(ThreadedGSM&, bool); typedef void (*ThreadedGSMCallbackBool)(ThreadedGSM &, bool);
typedef void (*ThreadedGSMCallback)(ThreadedGSM&); typedef void (*ThreadedGSMCallback)(ThreadedGSM &);
typedef void (*ThreadedGSMCallbackRing)(ThreadedGSM&, String&); typedef void (*ThreadedGSMCallbackRing)(ThreadedGSM &, String &);
struct conf struct conf {
{
ThreadedGSMCallbackSignal signal; ThreadedGSMCallbackSignal signal;
ThreadedGSMCallbackClock clock; ThreadedGSMCallbackClock clock;
ThreadedGSMCallbackIncomingSMS incoming; ThreadedGSMCallbackIncomingSMS incoming;
@ -106,10 +99,10 @@ public:
ThreadedGSMCallbackBattery battery; ThreadedGSMCallbackBattery battery;
ThreadedGSMCallbackRing ring; ThreadedGSMCallbackRing ring;
}; };
protected:
private: protected:
enum StatesStartup private:
{ enum StatesStartup {
STARTUP_POWER_OFF, STARTUP_POWER_OFF,
STARTUP_POWER_OFF_DELAY, STARTUP_POWER_OFF_DELAY,
STARTUP_POWER_ON, STARTUP_POWER_ON,
@ -121,26 +114,22 @@ private:
STARTUP_CHK_CENG STARTUP_CHK_CENG
}; };
enum StatesClock enum StatesClock {
{
CLOCK_REQ, CLOCK_REQ,
CLOCK_VERIFY CLOCK_VERIFY
}; };
enum StatesSignal enum StatesSignal {
{
SIGNAL_REQ, SIGNAL_REQ,
SIGNAL_VERIFY SIGNAL_VERIFY
}; };
enum StatesBattry enum StatesBattry {
{
BATTERY_REQ, BATTERY_REQ,
BATTERY_VERIFY BATTERY_VERIFY
}; };
enum StatesInbox enum StatesInbox {
{
READ_REQ, READ_REQ,
READ_CHK_CMGF, READ_CHK_CMGF,
READ_CHK_CPMS, READ_CHK_CPMS,
@ -151,16 +140,14 @@ private:
READ_CHK_CMGD READ_CHK_CMGD
}; };
enum StatesOutbox enum StatesOutbox {
{
SEND_REQ, SEND_REQ,
SEND_CHK_CMGF, SEND_CHK_CMGF,
SEND_CHK_RDY, SEND_CHK_RDY,
SEND_CHK_OK SEND_CHK_OK
}; };
enum StatesCheckRing enum StatesCheckRing {
{
RING_WAIT, RING_WAIT,
RING_CHK, RING_CHK,
}; };
@ -175,18 +162,17 @@ private:
SMSInfo SMSi; // Inbox SMS (incoming) SMSInfo SMSi; // Inbox SMS (incoming)
SMSInfo SMSo; // Outbox SMS (outgoing) SMSInfo SMSo; // Outbox SMS (outgoing)
Stream& stream; Stream & stream;
DTE dte; DTE dte;
unsigned long tickSync[THREADEDGSM_INTERVAL_COUNT]; unsigned long tickSync[THREADEDGSM_INTERVAL_COUNT];
unsigned long Intervals[THREADEDGSM_INTERVAL_COUNT]; unsigned long Intervals[THREADEDGSM_INTERVAL_COUNT];
// callbacks // callbacks
// conf configuration = {nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr}; // conf configuration = {nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr};
conf configuration{0}; conf configuration{0};
enum ReqTypes enum ReqTypes {
{
REQ_CLOCK = 1, REQ_CLOCK = 1,
REQ_SIG = 2, REQ_SIG = 2,
REQ_INBOX = 4, REQ_INBOX = 4,
@ -198,44 +184,47 @@ private:
int state; int state;
int job; int job;
int ringState; int ringState;
//functions // functions
public: public:
ThreadedGSM(Stream& stream) ThreadedGSM(Stream & stream)
: stream(stream) : stream(stream), dte(stream, THREADEDGSM_DTE_BUFFER_SIZE), ringState(RING_WAIT)
, dte(stream, THREADEDGSM_DTE_BUFFER_SIZE)
, ringState(RING_WAIT)
{ {
for (int i = 0; i < THREADEDGSM_INTERVAL_COUNT; i++) for (unsigned long & Interval : Intervals)
Intervals[i] = 0; Interval = 0;
job = state = requests = 0; job = state = requests = 0;
//SMSo.Text.reserve(150); // SMSo.Text.reserve(150);
//SMSi.Text.reserve(150); // SMSi.Text.reserve(150);
} }
~ThreadedGSM() {}; ~ThreadedGSM() = default;
void nextJob(void) { void nextJob()
{
job = 0; job = 0;
} }
void setHandlers(conf config) { void setHandlers(conf config)
{
this->configuration = config; this->configuration = config;
} }
void setInterval(IntervalSourceE source, unsigned long interval) { void setInterval(IntervalSourceE source, unsigned long interval)
{
Intervals[source] = interval; Intervals[source] = interval;
tickSync[source] = millis(); tickSync[source] = millis();
} }
// Initialization // Initialization
void begin(void) { void begin()
{
requests = (REQ_STARTUP); requests = (REQ_STARTUP);
} }
// Call this function for executing thread // Call this function for executing thread
void loop(void) { void loop()
{
if (dte.getIsBusy()) if (dte.getIsBusy())
return; return;
@ -244,21 +233,21 @@ public:
if (Intervals[i]) { if (Intervals[i]) {
if (millis() - tickSync[i] >= Intervals[i]) { if (millis() - tickSync[i] >= Intervals[i]) {
switch (i) { switch (i) {
case INTERVAL_CLOCK: case INTERVAL_CLOCK:
requests |= REQ_CLOCK; requests |= REQ_CLOCK;
break; break;
case INTERVAL_INBOX: case INTERVAL_INBOX:
requests |= REQ_INBOX; requests |= REQ_INBOX;
break; break;
case INTERVAL_SIGNAL: case INTERVAL_SIGNAL:
requests |= REQ_SIG; requests |= REQ_SIG;
break; break;
case INTERVAL_BATTERY: case INTERVAL_BATTERY:
requests |= REQ_BATTERY; requests |= REQ_BATTERY;
break; break;
} }
tickSync[i] = millis(); tickSync[i] = millis();
} }
@ -300,107 +289,110 @@ public:
Outbox(); Outbox();
else if (job == REQ_BATTERY) else if (job == REQ_BATTERY)
Battery(); Battery();
else CheckRing(); else
CheckRing();
} }
// Requests // Requests
void sendSMS(String& Number, String& Text) { void sendSMS(String & Number, String & Text)
{
requests |= (REQ_OUTBOX); requests |= (REQ_OUTBOX);
SMSo.Number = Number; SMSo.Number = Number;
SMSo.Text = Text; SMSo.Text = Text;
} }
void sendSMS(String& Number, char *Text) { void sendSMS(String & Number, const char * Text)
{
String t = Text; String t = Text;
sendSMS(Number, t); sendSMS(Number, t);
} }
protected: protected:
private: private:
// States // States
void Startup(void) { void Startup()
{
int lastState = state; int lastState = state;
switch (state) { switch (state) {
case STARTUP_POWER_OFF: case STARTUP_POWER_OFF:
if (this->configuration.power != NULL) if (this->configuration.power != nullptr)
this->configuration.power(*this, false); this->configuration.power(*this, false);
tick = millis(); tick = millis();
state = STARTUP_POWER_OFF_DELAY; state = STARTUP_POWER_OFF_DELAY;
break; break;
case STARTUP_POWER_OFF_DELAY: case STARTUP_POWER_OFF_DELAY:
if (millis() - tick >= THREADEDGSM_STARTUP_POWER_OFF_DELAY) if (millis() - tick >= THREADEDGSM_STARTUP_POWER_OFF_DELAY)
state = STARTUP_POWER_ON; state = STARTUP_POWER_ON;
break; break;
case STARTUP_POWER_ON: case STARTUP_POWER_ON:
if (this->configuration.power != NULL) if (this->configuration.power != nullptr)
this->configuration.power(*this, true); this->configuration.power(*this, true);
// begin delay // begin delay
tick = millis(); tick = millis();
state = STARTUP_DELAY; state = STARTUP_DELAY;
break; break;
case STARTUP_DELAY: case STARTUP_DELAY:
if (millis() - tick >= THREADEDGSM_STARTUP_DELAY) { if (millis() - tick >= THREADEDGSM_STARTUP_DELAY) {
dte.SendCommand(F("AT\r"), THREADEDGSM_AT_TIMEOUT, "OK\r"); dte.SendCommand(F("AT\r"), THREADEDGSM_AT_TIMEOUT, "OK\r");
state = STARTUP_ENTER_AT; state = STARTUP_ENTER_AT;
} }
break; break;
case STARTUP_ENTER_AT: case STARTUP_ENTER_AT:
if (dte.getResult() == DTE::EXPECT_RESULT) { if (dte.getResult() == DTE::EXPECT_RESULT) {
dte.SendCommand(F("AT+CPIN?\r"), 10000, "OK\r"); dte.SendCommand(F("AT+CPIN?\r"), 10000, "OK\r");
state = STARTUP_CHK_CPIN; state = STARTUP_CHK_CPIN;
} else {
state = STARTUP_POWER_OFF;
}
break;
case STARTUP_CHK_CPIN:
if (dte.getResult() == DTE::EXPECT_RESULT) {
if (dte.getBuffer().indexOf(F("+CPIN: READY")) != -1) {
dte.SendCommand(F("AT+CREG?\r"), THREADEDGSM_AT_TIMEOUT, "OK\r");
state = STARTUP_CHK_CREG;
} else { } else {
state = STARTUP_POWER_OFF; state = STARTUP_POWER_OFF;
} }
} else break;
state = STARTUP_POWER_OFF;
break;
case STARTUP_CHK_CREG: case STARTUP_CHK_CPIN:
if (dte.getResult() == DTE::EXPECT_RESULT) { if (dte.getResult() == DTE::EXPECT_RESULT) {
if ((dte.getBuffer().indexOf(F(",1")) >= 0) || (dte.getBuffer().indexOf(F(",5")) >= 0)) { if (dte.getBuffer().indexOf(F("+CPIN: READY")) != -1) {
dte.SendCommand(F("AT+CLTS=1\r"), THREADEDGSM_AT_TIMEOUT, "OK\r"); dte.SendCommand(F("AT+CREG?\r"), THREADEDGSM_AT_TIMEOUT, "OK\r");
state = STARTUP_CHK_CLTS; state = STARTUP_CHK_CREG;
} else {
state = STARTUP_POWER_OFF;
}
} else } else
state = STARTUP_POWER_OFF; state = STARTUP_POWER_OFF;
} else break;
state = STARTUP_POWER_OFF;
break;
case STARTUP_CHK_CLTS: case STARTUP_CHK_CREG:
if (dte.getResult() == DTE::EXPECT_RESULT) { if (dte.getResult() == DTE::EXPECT_RESULT) {
dte.SendCommand(F("AT+CENG=3\r"), THREADEDGSM_AT_TIMEOUT, "OK\r"); if ((dte.getBuffer().indexOf(F(",1")) >= 0) || (dte.getBuffer().indexOf(F(",5")) >= 0)) {
state = STARTUP_CHK_CENG; dte.SendCommand(F("AT+CLTS=1\r"), THREADEDGSM_AT_TIMEOUT, "OK\r");
} else state = STARTUP_CHK_CLTS;
state = STARTUP_POWER_OFF; } else
break; state = STARTUP_POWER_OFF;
} else
state = STARTUP_POWER_OFF;
break;
case STARTUP_CHK_CENG: case STARTUP_CHK_CLTS:
if (dte.getResult() == DTE::EXPECT_RESULT) { if (dte.getResult() == DTE::EXPECT_RESULT) {
requests |= ((REQ_CLOCK) | (REQ_SIG) | (REQ_BATTERY)); dte.SendCommand(F("AT+CENG=3\r"), THREADEDGSM_AT_TIMEOUT, "OK\r");
clearReq(REQ_STARTUP); state = STARTUP_CHK_CENG;
for (int i = 0; i < THREADEDGSM_INTERVAL_COUNT; i++) } else
tickSync[i] = millis(); state = STARTUP_POWER_OFF;
if (this->configuration.ready != NULL) break;
this->configuration.ready(*this);
} else case STARTUP_CHK_CENG:
state = STARTUP_POWER_OFF; if (dte.getResult() == DTE::EXPECT_RESULT) {
break; requests |= ((REQ_CLOCK) | (REQ_SIG) | (REQ_BATTERY));
clearReq(REQ_STARTUP);
for (int i = 0; i < THREADEDGSM_INTERVAL_COUNT; i++)
tickSync[i] = millis();
if (this->configuration.ready != nullptr)
this->configuration.ready(*this);
} else
state = STARTUP_POWER_OFF;
break;
} }
if (state != lastState) { if (state != lastState) {
DEBUG_PRINT(F("STARTUP_STATE: ")); DEBUG_PRINT(F("STARTUP_STATE: "));
@ -409,44 +401,46 @@ private:
} }
// Threads // Threads
void Clock(void) { void Clock(void)
{
String clockTime; String clockTime;
int lastState = state; int lastState = state;
switch (state) { switch (state) {
case CLOCK_REQ: case CLOCK_REQ:
dte.SendCommand(F("AT+CCLK?\r"), THREADEDGSM_AT_TIMEOUT, "OK\r"); dte.SendCommand(F("AT+CCLK?\r"), THREADEDGSM_AT_TIMEOUT, "OK\r");
state = CLOCK_VERIFY; state = CLOCK_VERIFY;
break; break;
case CLOCK_VERIFY: case CLOCK_VERIFY:
int index = dte.getBuffer().indexOf(F("+CCLK: ")); int index = dte.getBuffer().indexOf(F("+CCLK: "));
if (index >= 0) { if (index >= 0) {
// parse clock // parse clock
index += 8; index += 8;
int endindex; int endindex;
endindex = dte.getBuffer().indexOf(F("+"), index); endindex = dte.getBuffer().indexOf(F("+"), index);
if (endindex >= 0)
clockTime = dte.getBuffer().substring(index, endindex);
else {
endindex = dte.getBuffer().indexOf(F("-"), index);
if (endindex >= 0) if (endindex >= 0)
clockTime = dte.getBuffer().substring(index, endindex); clockTime = dte.getBuffer().substring(index, endindex);
} else {
endindex = dte.getBuffer().indexOf(F("-"), index);
if (endindex >= 0)
clockTime = dte.getBuffer().substring(index, endindex);
}
if (endindex >= 0) { if (endindex >= 0) {
NetworkTime ClockTime; NetworkTime ClockTime{};
ClockTime.year = 2000 + clockTime.substring(0, 2).toInt();
ClockTime.month = clockTime.substring(3, 5).toInt(); ClockTime.year = 2000 + clockTime.substring(0, 2).toInt();
ClockTime.day = clockTime.substring(6, 8).toInt(); ClockTime.month = clockTime.substring(3, 5).toInt();
ClockTime.hour = clockTime.substring(9, 11).toInt(); ClockTime.day = clockTime.substring(6, 8).toInt();
ClockTime.minute = clockTime.substring(12, 14).toInt(); ClockTime.hour = clockTime.substring(9, 11).toInt();
ClockTime.second = clockTime.substring(15, 17).toInt(); ClockTime.minute = clockTime.substring(12, 14).toInt();
if (this->configuration.clock != NULL) ClockTime.second = clockTime.substring(15, 17).toInt();
this->configuration.clock(*this, ClockTime); if (this->configuration.clock != nullptr)
this->configuration.clock(*this, ClockTime);
}
} }
} clearReq(REQ_CLOCK);
clearReq(REQ_CLOCK); break;
break;
} }
if (state != lastState) { if (state != lastState) {
DEBUG_PRINT(F("CLOCK_STATE: ")); DEBUG_PRINT(F("CLOCK_STATE: "));
@ -454,29 +448,30 @@ private:
} }
} }
void Signal(void) { void Signal()
{
int lastState = state; int lastState = state;
switch (state) { switch (state) {
case SIGNAL_REQ: case SIGNAL_REQ:
dte.SendCommand(F("AT+CSQ\r"), THREADEDGSM_AT_TIMEOUT, "OK\r"); dte.SendCommand(F("AT+CSQ\r"), THREADEDGSM_AT_TIMEOUT, "OK\r");
state = SIGNAL_VERIFY; state = SIGNAL_VERIFY;
break; break;
case SIGNAL_VERIFY: case SIGNAL_VERIFY:
int index = dte.getBuffer().indexOf(F("+CSQ: ")); int index = dte.getBuffer().indexOf(F("+CSQ: "));
if (index >= 0) { if (index >= 0) {
// parse signal // parse signal
index += 6; index += 6;
SignalLevel GsmSignal; SignalLevel GsmSignal{};
GsmSignal.Value = dte.getBuffer().substring(index, index + 2).toInt(); GsmSignal.Value = dte.getBuffer().substring(index, index + 2).toInt();
GsmSignal.Dbm = dte.getBuffer().substring(index + 3, index + 5).toInt(); GsmSignal.Dbm = dte.getBuffer().substring(index + 3, index + 5).toInt();
if (GsmSignal.Value != 0) { if (GsmSignal.Value != 0) {
if (this->configuration.signal != NULL) if (this->configuration.signal != nullptr)
this->configuration.signal(*this, GsmSignal); this->configuration.signal(*this, GsmSignal);
}
} }
} clearReq(REQ_SIG);
clearReq(REQ_SIG); break;
break;
} }
if (state != lastState) { if (state != lastState) {
DEBUG_PRINT(F("SIGNAL_STATE: ")); DEBUG_PRINT(F("SIGNAL_STATE: "));
@ -484,30 +479,31 @@ private:
} }
} }
void Battery(void) { void Battery()
{
int lastState = state; int lastState = state;
switch (state) { switch (state) {
case BATTERY_REQ: case BATTERY_REQ:
dte.SendCommand(F("AT+CBC\r"), THREADEDGSM_AT_TIMEOUT, "OK\r"); dte.SendCommand(F("AT+CBC\r"), THREADEDGSM_AT_TIMEOUT, "OK\r");
state = BATTERY_VERIFY; state = BATTERY_VERIFY;
break; break;
case BATTERY_VERIFY: case BATTERY_VERIFY:
int index = dte.getBuffer().indexOf(F("+CBC:")); int index = dte.getBuffer().indexOf(F("+CBC:"));
if (index >= 0) { if (index >= 0) {
BatteryInfo BattInfo; BatteryInfo BattInfo{};
// parse battery level // parse battery level
String buffer = dte.getBuffer().substring(index); String buffer = dte.getBuffer().substring(index);
String buffer2 = buffer.substring(buffer.indexOf(F(",")) + 1); String buffer2 = buffer.substring(buffer.indexOf(F(",")) + 1);
buffer = buffer2; buffer = buffer2;
BattInfo.Percent = buffer2.substring(0, buffer2.indexOf(F(","))).toInt(); // converts the result to interger BattInfo.Percent = buffer2.substring(0, buffer2.indexOf(F(","))).toInt(); // converts the result to interger
buffer2 = buffer.substring(buffer.indexOf(F(",")) + 1); buffer2 = buffer.substring(buffer.indexOf(F(",")) + 1);
BattInfo.Voltage = buffer2.substring(0, buffer2.indexOf(F("\r"))).toInt(); // converts the result to interger BattInfo.Voltage = buffer2.substring(0, buffer2.indexOf(F("\r"))).toInt(); // converts the result to interger
if (this->configuration.battery != NULL) if (this->configuration.battery != nullptr)
this->configuration.battery(*this, BattInfo); this->configuration.battery(*this, BattInfo);
} }
clearReq(REQ_BATTERY); clearReq(REQ_BATTERY);
break; break;
} }
if (state != lastState) { if (state != lastState) {
DEBUG_PRINT(F("BATTERY_STATE: ")); DEBUG_PRINT(F("BATTERY_STATE: "));
@ -515,110 +511,112 @@ private:
} }
} }
void clearReq(int req) { void clearReq(int req)
{
requests &= ~(req); requests &= ~(req);
nextJob(); nextJob();
} }
void Inbox(void) { void Inbox()
{
String CMD; String CMD;
int lastState = state; int lastState = state;
switch (state) { switch (state) {
case READ_REQ: case READ_REQ:
SMSi.Text = ""; SMSi.Text = "";
SMSi.Number = ""; SMSi.Number = "";
dte.SendCommand(F("AT+CMGF=0\r"), THREADEDGSM_AT_TIMEOUT, "OK\r"); // SMS Message format set as PDU dte.SendCommand(F("AT+CMGF=0\r"), THREADEDGSM_AT_TIMEOUT, "OK\r"); // SMS Message format set as PDU
state = READ_CHK_CMGF; state = READ_CHK_CMGF;
break; break;
case READ_CHK_CMGF: case READ_CHK_CMGF:
if (dte.getResult() == DTE::EXPECT_RESULT) { if (dte.getResult() == DTE::EXPECT_RESULT) {
dte.SendCommand(F("AT+CPMS=\"SM\"\r"), THREADEDGSM_AT_TIMEOUT, "OK\r"); // SIM Message storage dte.SendCommand(F("AT+CPMS=\"SM\"\r"), THREADEDGSM_AT_TIMEOUT, "OK\r"); // SIM Message storage
state = READ_CHK_CPMS; state = READ_CHK_CPMS;
} } else
else clearReq(REQ_INBOX); clearReq(REQ_INBOX);
break; break;
case READ_CHK_CPMS: case READ_CHK_CPMS:
if (dte.getResult() == DTE::EXPECT_RESULT) { if (dte.getResult() == DTE::EXPECT_RESULT) {
CMD = F("AT+CMGL="); // Read all SMS messages CMD = F("AT+CMGL="); // Read all SMS messages
CMD += (int) READ_TYPE_ALL; CMD += (int) READ_TYPE_ALL;
CMD += "\r"; CMD += "\r";
dte.SendCommand(CMD.c_str(), THREADEDGSM_AT_TIMEOUT, ","); dte.SendCommand(CMD.c_str(), THREADEDGSM_AT_TIMEOUT, ",");
state = READ_CHK_CMGL; state = READ_CHK_CMGL;
} } else
else clearReq(REQ_INBOX); clearReq(REQ_INBOX);
break; break;
case READ_CHK_CMGL: case READ_CHK_CMGL:
if (dte.getResult() == DTE::EXPECT_RESULT) { if (dte.getResult() == DTE::EXPECT_RESULT) {
//fetch index // fetch index
int indexStart = dte.getBuffer().indexOf(F("+CMGL: ")); int indexStart = dte.getBuffer().indexOf(F("+CMGL: "));
if (indexStart >= 0) { if (indexStart >= 0) {
Message.Index = dte.getBuffer().substring(indexStart + 7, dte.getBuffer().indexOf(F(","))).toInt(); Message.Index = dte.getBuffer().substring(indexStart + 7, dte.getBuffer().indexOf(F(","))).toInt();
if (Message.Index != 0) { if (Message.Index != 0) {
dte.Delay(2000); dte.Delay(2000);
state = READ_DELAY_CLEAR_BUFF; state = READ_DELAY_CLEAR_BUFF;
}
} }
} }
} if (state != READ_DELAY_CLEAR_BUFF)
if (state != READ_DELAY_CLEAR_BUFF) clearReq(REQ_INBOX);
clearReq(REQ_INBOX);
break; break;
case READ_DELAY_CLEAR_BUFF: case READ_DELAY_CLEAR_BUFF:
dte.SendCommand(F("AT+CMGF=1\r"), THREADEDGSM_AT_TIMEOUT, "OK\r"); // SMS Message format set as TEXT dte.SendCommand(F("AT+CMGF=1\r"), THREADEDGSM_AT_TIMEOUT, "OK\r"); // SMS Message format set as TEXT
state = READ_TEXT_CMGR; state = READ_TEXT_CMGR;
break; break;
case READ_TEXT_CMGR: case READ_TEXT_CMGR:
if (dte.getResult() == DTE::EXPECT_RESULT) { if (dte.getResult() == DTE::EXPECT_RESULT) {
CMD = F("AT+CMGR="); // Read the SMS message CMD = F("AT+CMGR="); // Read the SMS message
CMD += Message.Index; CMD += Message.Index;
CMD += "\r"; CMD += "\r";
dte.SendCommand(CMD.c_str(), THREADEDGSM_AT_TIMEOUT, "OK\r"); dte.SendCommand(CMD.c_str(), THREADEDGSM_AT_TIMEOUT, "OK\r");
state = READ_CHK_CMGR; state = READ_CHK_CMGR;
} else } else
clearReq(REQ_INBOX); clearReq(REQ_INBOX);
break; break;
case READ_CHK_CMGR: case READ_CHK_CMGR:
if (dte.getResult() == DTE::EXPECT_RESULT) { if (dte.getResult() == DTE::EXPECT_RESULT) {
int indexStart = dte.getBuffer().indexOf(F("+CMGR: ")); int indexStart = dte.getBuffer().indexOf(F("+CMGR: "));
if (indexStart >= 0) { if (indexStart >= 0) {
int indexStartPDU = dte.getBuffer().indexOf(F("\r\n"), indexStart); int indexStartPDU = dte.getBuffer().indexOf(F("\r\n"), indexStart);
if (indexStartPDU >= 0) { if (indexStartPDU >= 0) {
indexStartPDU += 2; indexStartPDU += 2;
int indexEndPDU = dte.getBuffer().indexOf(F("\r"), indexStartPDU); int indexEndPDU = dte.getBuffer().indexOf(F("\r"), indexStartPDU);
if (indexEndPDU >= 0) if (indexEndPDU >= 0)
SMSi.Text = dte.getBuffer().substring(indexStartPDU, indexEndPDU); SMSi.Text = dte.getBuffer().substring(indexStartPDU, indexEndPDU);
} }
indexStartPDU = dte.getBuffer().indexOf(F(",\""), indexStart); indexStartPDU = dte.getBuffer().indexOf(F(",\""), indexStart);
if (indexStartPDU >= 0) { if (indexStartPDU >= 0) {
indexStartPDU += 2; indexStartPDU += 2;
int indexEndPDU = dte.getBuffer().indexOf(F("\","), indexStartPDU); int indexEndPDU = dte.getBuffer().indexOf(F("\","), indexStartPDU);
if (indexEndPDU >= 0) if (indexEndPDU >= 0)
SMSi.Number = dte.getBuffer().substring(indexStartPDU, indexEndPDU); SMSi.Number = dte.getBuffer().substring(indexStartPDU, indexEndPDU);
}
} }
CMD = F("AT+CMGD="); // Delete the SMS message
CMD += Message.Index;
CMD += "\r";
dte.SendCommand(CMD.c_str(), THREADEDGSM_AT_TIMEOUT, "OK\r");
state = READ_CHK_CMGD;
} else
clearReq(REQ_INBOX);
break;
case READ_CHK_CMGD:
// if( (dte.getResult() == DTE::EXPECT_RESULT) && (SMS.InboxMsgContents != ""))
if (dte.getResult() == DTE::EXPECT_RESULT) {
if (this->configuration.incoming != nullptr)
this->configuration.incoming(*this, SMSi);
} }
CMD = F("AT+CMGD="); // Delete the SMS message
CMD += Message.Index;
CMD += "\r";
dte.SendCommand(CMD.c_str(), THREADEDGSM_AT_TIMEOUT, "OK\r");
state = READ_CHK_CMGD;
} else
clearReq(REQ_INBOX); clearReq(REQ_INBOX);
break; break;
case READ_CHK_CMGD:
//if( (dte.getResult() == DTE::EXPECT_RESULT) && (SMS.InboxMsgContents != ""))
if (dte.getResult() == DTE::EXPECT_RESULT) {
if (this->configuration.incoming != NULL)
this->configuration.incoming(*this, SMSi);
}
clearReq(REQ_INBOX);
break;
} }
if (state != lastState) { if (state != lastState) {
DEBUG_PRINT(F("INBOX_STATE: ")); DEBUG_PRINT(F("INBOX_STATE: "));
@ -626,44 +624,46 @@ private:
} }
} }
void Outbox(void) { void Outbox()
{
String CMD; String CMD;
//CMD.reserve(200); // CMD.reserve(200);
int lastState = state; int lastState = state;
switch (state) { switch (state) {
case SEND_REQ: case SEND_REQ:
dte.SendCommand(F("AT+CMGF=1\r"), THREADEDGSM_AT_TIMEOUT, "OK\r"); // SMS Text mode dte.SendCommand(F("AT+CMGF=1\r"), THREADEDGSM_AT_TIMEOUT, "OK\r"); // SMS Text mode
state = SEND_CHK_CMGF; state = SEND_CHK_CMGF;
break; break;
case SEND_CHK_CMGF: case SEND_CHK_CMGF:
if (dte.getResult() == DTE::EXPECT_RESULT) { if (dte.getResult() == DTE::EXPECT_RESULT) {
CMD = F("AT+CMGS=\""); CMD = F("AT+CMGS=\"");
CMD += SMSo.Number; CMD += SMSo.Number;
CMD += "\"\r"; CMD += "\"\r";
dte.SendCommand(CMD.c_str(), 15000, "> "); dte.SendCommand(CMD.c_str(), 15000, "> ");
state = SEND_CHK_RDY; state = SEND_CHK_RDY;
} } else
else clearReq(REQ_OUTBOX); clearReq(REQ_OUTBOX);
break; break;
case SEND_CHK_RDY: case SEND_CHK_RDY:
if (dte.getResult() == DTE::EXPECT_RESULT) { if (dte.getResult() == DTE::EXPECT_RESULT) {
CMD = SMSo.Text; CMD = SMSo.Text;
CMD += (char)26; CMD += (char) 26;
dte.SendCommand(CMD.c_str(), 10000, "OK\r"); dte.SendCommand(CMD.c_str(), 10000, "OK\r");
state = SEND_CHK_OK; state = SEND_CHK_OK;
} else clearReq(REQ_OUTBOX); } else
break; clearReq(REQ_OUTBOX);
break;
case SEND_CHK_OK: case SEND_CHK_OK:
if (dte.getResult() == DTE::EXPECT_RESULT) { if (dte.getResult() == DTE::EXPECT_RESULT) {
if (this->configuration.outgoing != NULL) if (this->configuration.outgoing != nullptr)
this->configuration.outgoing(*this); this->configuration.outgoing(*this);
} }
clearReq(REQ_OUTBOX); clearReq(REQ_OUTBOX);
break; break;
} }
if (state != lastState) { if (state != lastState) {
DEBUG_PRINT(F("OUTBOX_STATE: ")); DEBUG_PRINT(F("OUTBOX_STATE: "));
@ -671,7 +671,8 @@ private:
} }
} }
void CheckRing(void) { void CheckRing()
{
int lastState = ringState; int lastState = ringState;
switch (ringState) { switch (ringState) {
case RING_WAIT: case RING_WAIT:
@ -679,11 +680,19 @@ private:
ringState = RING_CHK; ringState = RING_CHK;
} }
break; break;
case RING_CHK:
if (this->configuration.ring != nullptr) {
String s = "Ring";
this->configuration.ring(*this, s);
}
ringState = RING_WAIT;
break;
} }
if (ringState != lastState) { if (ringState != lastState) {
DEBUG_PRINT(F("RING_STATE: ")); DEBUG_PRINT(F("RING_STATE: "));
DEBUG_PRINTLN(ringState); DEBUG_PRINTLN(ringState);
} }
} }
}; //ThreadedGSM }; // ThreadedGSM