Prejmenovani promennych pro zbaveni se this->

This commit is contained in:
Pavel Brychta 2023-09-03 12:22:16 +02:00
parent 5bccbd1113
commit 78bd8a981d
4 changed files with 259 additions and 259 deletions

View File

@ -1,114 +1,114 @@
#include "DTE.h" #include "DTE.h"
DTE::DTE(Stream & stream, unsigned int size) DTE::DTE(Stream & stream, unsigned int size)
: stream(stream), bufferSize(size), result(EXPECT_RESULT) : mStream(stream), mBufferSize(size), mResult(EXPECT_RESULT)
{ {
buffer.reserve(size); mBuffer.reserve(size);
} }
void DTE::SendCommand(const char * command, unsigned long timeout, const char * response1, const char * response2, const char * response3) void DTE::SendCommand(const char * command, unsigned long timeout, const char * response1, const char * response2, const char * response3)
{ {
match = 0; mMatch = 0;
result = EXPECT_BUSY; mResult = EXPECT_BUSY;
response[0] = response1; mResponse[0] = response1;
response[1] = response2; mResponse[1] = response2;
response[2] = response3; mResponse[2] = response3;
this->timeout = timeout; mTimeout = timeout;
flush(); flush();
stream.print(command); mStream.print(command);
buffer = ""; mBuffer = "";
tick = millis(); mTick = millis();
process(); process();
} }
void DTE::SendCommand(const __FlashStringHelper * command, unsigned long timeout, const char * response1, const char * response2, const char * response3) void DTE::SendCommand(const __FlashStringHelper * command, unsigned long timeout, const char * response1, const char * response2, const char * response3)
{ {
match = 0; mMatch = 0;
result = EXPECT_BUSY; mResult = EXPECT_BUSY;
response[0] = response1; mResponse[0] = response1;
response[1] = response2; mResponse[1] = response2;
response[2] = response3; mResponse[2] = response3;
this->timeout = timeout; mTimeout = timeout;
// clear rx buffer // clear rx mBuffer
flush(); flush();
// send command // send command
stream.print((const __FlashStringHelper *) command); mStream.print((const __FlashStringHelper *) command);
buffer = ""; mBuffer = "";
tick = millis(); mTick = millis();
process(); process();
} }
void DTE::Delay(unsigned long delay) void DTE::Delay(unsigned long delay)
{ {
timeout = delay; mTimeout = delay;
result = EXPECT_DELAY; mResult = EXPECT_DELAY;
tick = millis(); mTick = millis();
process(); process();
} }
bool DTE::getIsBusy() bool DTE::getIsBusy()
{ {
process(); process();
return (result == EXPECT_BUSY) || (result == EXPECT_DELAY); return (mResult == EXPECT_BUSY) || (mResult == EXPECT_DELAY);
} }
DTE::CommandResult DTE::getResult() DTE::CommandResult DTE::getResult()
{ {
return result; return mResult;
} }
unsigned int DTE::getMatch() const unsigned int DTE::getMatch() const
{ {
return match; return mMatch;
} }
String & DTE::getBuffer() String & DTE::getBuffer()
{ {
return buffer; return mBuffer;
} }
void DTE::flush() void DTE::flush()
{ {
// clear rx buffer // clear rx mBuffer
while (stream.available()) { while (mStream.available()) {
stream.read(); mStream.read();
} }
} }
void DTE::process() void DTE::process()
{ {
if (result == EXPECT_DELAY) { if (mResult == EXPECT_DELAY) {
if (millis() - tick >= timeout) if (millis() - mTick >= mTimeout)
result = EXPECT_RESULT; mResult = EXPECT_RESULT;
return; return;
} }
if (result != EXPECT_BUSY) if (mResult != EXPECT_BUSY)
return; return;
char c; char c;
unsigned long now = millis(); unsigned long now = millis();
while (millis() - tick < timeout) { while (millis() - mTick < mTimeout) {
while (stream.available() && (buffer.length() < bufferSize)) { while (mStream.available() && (mBuffer.length() < mBufferSize)) {
c = stream.read(); c = mStream.read();
buffer.concat(c); mBuffer.concat(c);
if (buffer.endsWith(response[0])) { if (mBuffer.endsWith(mResponse[0])) {
match = 0; mMatch = 0;
result = EXPECT_RESULT; mResult = EXPECT_RESULT;
return; return;
} else if (response[1].length() != 0) { } else if (mResponse[1].length() != 0) {
if (buffer.endsWith(response[1])) { if (mBuffer.endsWith(mResponse[1])) {
match = 1; mMatch = 1;
result = EXPECT_RESULT; mResult = EXPECT_RESULT;
return; return;
} }
} else if (response[2].length() != 0) { } else if (mResponse[2].length() != 0) {
if (buffer.endsWith(response[2])) { if (mBuffer.endsWith(mResponse[2])) {
match = 2; mMatch = 2;
result = EXPECT_RESULT; mResult = EXPECT_RESULT;
return; return;
} }
} }
@ -118,5 +118,5 @@ void DTE::process()
} }
// time out // time out
result = EXPECT_TIMEOUT; mResult = EXPECT_TIMEOUT;
} }

View File

@ -18,14 +18,14 @@ class DTE {
}; };
protected: protected:
String buffer; String mBuffer;
Stream & stream; Stream & mStream;
unsigned int bufferSize; unsigned int mBufferSize;
String response[3]; String mResponse[3];
unsigned long timeout; unsigned long mTimeout;
unsigned long tick; unsigned long mTick;
unsigned int match; unsigned int mMatch;
CommandResult result; CommandResult mResult;
public: public:
DTE(Stream & stream, unsigned int size); DTE(Stream & stream, unsigned int size);

View File

@ -9,29 +9,28 @@
#endif #endif
ThreadedGSM::ThreadedGSM(Stream & stream) ThreadedGSM::ThreadedGSM(Stream & stream)
: stream(stream), dte(stream, THREADEDGSM_DTE_BUFFER_SIZE), ringState(RING_WAIT) : mStream(stream), mDte(stream, THREADEDGSM_DTE_BUFFER_SIZE), mRingState(RING_WAIT)
{ {
for (unsigned long & Interval : Intervals)
Interval = 0;
job = state = requests = 0; mSMSo.Text.reserve(150);
// SMSo.Text.reserve(150); mSMSi.Text.reserve(150);
// SMSi.Text.reserve(150);
} }
void ThreadedGSM::nextJob() void ThreadedGSM::nextJob()
{ {
job = 0;
mJob = 0;
} }
void ThreadedGSM::setHandlers(conf config) void ThreadedGSM::setHandlers(conf config)
{ {
this->configuration = config; mConfiguration = config;
} }
void ThreadedGSM::setInterval(IntervalSourceE source, unsigned long interval) void ThreadedGSM::setInterval(IntervalSourceE source, unsigned long interval)
{ {
Intervals[source] = interval; Intervals[source] = interval;
tickSync[source] = millis(); tickSync[source] = millis();
} }
@ -39,14 +38,15 @@ void ThreadedGSM::setInterval(IntervalSourceE source, unsigned long interval)
// Initialization // Initialization
void ThreadedGSM::begin() void ThreadedGSM::begin()
{ {
requests = (REQ_STARTUP);
mRequests = (REQ_STARTUP);
TimeBase.attachLoop(this); TimeBase.attachLoop(this);
} }
// Call this function for executing thread // Call this function for executing thread
void ThreadedGSM::exec() void ThreadedGSM::exec()
{ {
if (dte.getIsBusy()) if (mDte.getIsBusy())
return; return;
// intervals // intervals
@ -55,19 +55,19 @@ void ThreadedGSM::exec()
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; mRequests |= REQ_CLOCK;
break; break;
case INTERVAL_INBOX: case INTERVAL_INBOX:
requests |= REQ_INBOX; mRequests |= REQ_INBOX;
break; break;
case INTERVAL_SIGNAL: case INTERVAL_SIGNAL:
requests |= REQ_SIG; mRequests |= REQ_SIG;
break; break;
case INTERVAL_BATTERY: case INTERVAL_BATTERY:
requests |= REQ_BATTERY; mRequests |= REQ_BATTERY;
break; break;
} }
tickSync[i] = millis(); tickSync[i] = millis();
@ -75,40 +75,40 @@ void ThreadedGSM::exec()
} }
} }
if (job == 0) { if (mJob == 0) {
// no assigned job, assign it // no assigned mJob, assign it
if (requests & REQ_CLOCK) if (mRequests & REQ_CLOCK)
job = REQ_CLOCK; mJob = REQ_CLOCK;
else if (requests & REQ_SIG) else if (mRequests & REQ_SIG)
job = REQ_SIG; mJob = REQ_SIG;
else if (requests & REQ_INBOX) else if (mRequests & REQ_INBOX)
job = REQ_INBOX; mJob = REQ_INBOX;
else if (requests & REQ_OUTBOX) else if (mRequests & REQ_OUTBOX)
job = REQ_OUTBOX; mJob = REQ_OUTBOX;
else if (requests & REQ_STARTUP) else if (mRequests & REQ_STARTUP)
job = REQ_STARTUP; mJob = REQ_STARTUP;
else if (requests & REQ_BATTERY) else if (mRequests & REQ_BATTERY)
job = REQ_BATTERY; mJob = REQ_BATTERY;
if (job) { if (mJob) {
state = 0; mState = 0;
DEBUG_PRINT(F("Job ID: ")); DEBUG_PRINT(F("Job ID: "));
DEBUG_PRINTLN(job); DEBUG_PRINTLN(mJob);
} }
} }
// execute current job // execute current mJob
if (job == REQ_STARTUP) if (mJob == REQ_STARTUP)
Startup(); Startup();
else if (job == REQ_CLOCK) else if (mJob == REQ_CLOCK)
Clock(); Clock();
else if (job == REQ_SIG) else if (mJob == REQ_SIG)
Signal(); Signal();
else if (job == REQ_INBOX) else if (mJob == REQ_INBOX)
Inbox(); Inbox();
else if (job == REQ_OUTBOX) else if (mJob == REQ_OUTBOX)
Outbox(); Outbox();
else if (job == REQ_BATTERY) else if (mJob == REQ_BATTERY)
Battery(); Battery();
else else
CheckRing(); CheckRing();
@ -117,9 +117,9 @@ void ThreadedGSM::exec()
// Requests // Requests
void ThreadedGSM::sendSMS(String & Number, String & Text) void ThreadedGSM::sendSMS(String & Number, String & Text)
{ {
requests |= (REQ_OUTBOX); mRequests |= (REQ_OUTBOX);
SMSo.Number = Number; mSMSo.Number = Number;
SMSo.Text = Text; mSMSo.Text = Text;
} }
void ThreadedGSM::sendSMS(String & Number, const char * Text) void ThreadedGSM::sendSMS(String & Number, const char * Text)
@ -132,90 +132,90 @@ void ThreadedGSM::sendSMS(String & Number, const char * Text)
// States // States
void ThreadedGSM::Startup() void ThreadedGSM::Startup()
{ {
int lastState = state; int lastState = mState;
switch (state) { switch (mState) {
case STARTUP_POWER_OFF: case STARTUP_POWER_OFF:
if (this->configuration.power != nullptr) if (mConfiguration.power != nullptr)
this->configuration.power(*this, false); mConfiguration.power(*this, false);
tick = millis(); mTick = millis();
state = STARTUP_POWER_OFF_DELAY; mState = 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() - mTick >= THREADEDGSM_STARTUP_POWER_OFF_DELAY)
state = STARTUP_POWER_ON; mState = STARTUP_POWER_ON;
break; break;
case STARTUP_POWER_ON: case STARTUP_POWER_ON:
if (this->configuration.power != nullptr) if (mConfiguration.power != nullptr)
this->configuration.power(*this, true); mConfiguration.power(*this, true);
// begin delay // begin delay
tick = millis(); mTick = millis();
state = STARTUP_DELAY; mState = STARTUP_DELAY;
break; break;
case STARTUP_DELAY: case STARTUP_DELAY:
if (millis() - tick >= THREADEDGSM_STARTUP_DELAY) { if (millis() - mTick >= THREADEDGSM_STARTUP_DELAY) {
dte.SendCommand(F("AT\r"), THREADEDGSM_AT_TIMEOUT, "OK\r"); mDte.SendCommand(F("AT\r"), THREADEDGSM_AT_TIMEOUT, "OK\r");
state = STARTUP_ENTER_AT; mState = STARTUP_ENTER_AT;
} }
break; break;
case STARTUP_ENTER_AT: case STARTUP_ENTER_AT:
if (dte.getResult() == DTE::EXPECT_RESULT) { if (mDte.getResult() == DTE::EXPECT_RESULT) {
dte.SendCommand(F("AT+CPIN?\r"), 10000, "OK\r"); mDte.SendCommand(F("AT+CPIN?\r"), 10000, "OK\r");
state = STARTUP_CHK_CPIN; mState = STARTUP_CHK_CPIN;
} else { } else {
state = STARTUP_POWER_OFF; mState = STARTUP_POWER_OFF;
} }
break; break;
case STARTUP_CHK_CPIN: case STARTUP_CHK_CPIN:
if (dte.getResult() == DTE::EXPECT_RESULT) { if (mDte.getResult() == DTE::EXPECT_RESULT) {
if (dte.getBuffer().indexOf(F("+CPIN: READY")) != -1) { if (mDte.getBuffer().indexOf(F("+CPIN: READY")) != -1) {
dte.SendCommand(F("AT+CREG?\r"), THREADEDGSM_AT_TIMEOUT, "OK\r"); mDte.SendCommand(F("AT+CREG?\r"), THREADEDGSM_AT_TIMEOUT, "OK\r");
state = STARTUP_CHK_CREG; mState = STARTUP_CHK_CREG;
} else { } else {
state = STARTUP_POWER_OFF; mState = STARTUP_POWER_OFF;
} }
} else } else
state = STARTUP_POWER_OFF; mState = STARTUP_POWER_OFF;
break; break;
case STARTUP_CHK_CREG: case STARTUP_CHK_CREG:
if (dte.getResult() == DTE::EXPECT_RESULT) { if (mDte.getResult() == DTE::EXPECT_RESULT) {
if ((dte.getBuffer().indexOf(F(",1")) >= 0) || (dte.getBuffer().indexOf(F(",5")) >= 0)) { if ((mDte.getBuffer().indexOf(F(",1")) >= 0) || (mDte.getBuffer().indexOf(F(",5")) >= 0)) {
dte.SendCommand(F("AT+CLTS=1\r"), THREADEDGSM_AT_TIMEOUT, "OK\r"); mDte.SendCommand(F("AT+CLTS=1\r"), THREADEDGSM_AT_TIMEOUT, "OK\r");
state = STARTUP_CHK_CLTS; mState = STARTUP_CHK_CLTS;
} else } else
state = STARTUP_POWER_OFF; mState = STARTUP_POWER_OFF;
} else } else
state = STARTUP_POWER_OFF; mState = STARTUP_POWER_OFF;
break; break;
case STARTUP_CHK_CLTS: case STARTUP_CHK_CLTS:
if (dte.getResult() == DTE::EXPECT_RESULT) { if (mDte.getResult() == DTE::EXPECT_RESULT) {
dte.SendCommand(F("AT+CENG=3\r"), THREADEDGSM_AT_TIMEOUT, "OK\r"); mDte.SendCommand(F("AT+CENG=3\r"), THREADEDGSM_AT_TIMEOUT, "OK\r");
state = STARTUP_CHK_CENG; mState = STARTUP_CHK_CENG;
} else } else
state = STARTUP_POWER_OFF; mState = STARTUP_POWER_OFF;
break; break;
case STARTUP_CHK_CENG: case STARTUP_CHK_CENG:
if (dte.getResult() == DTE::EXPECT_RESULT) { if (mDte.getResult() == DTE::EXPECT_RESULT) {
requests |= ((REQ_CLOCK) | (REQ_SIG) | (REQ_BATTERY)); mRequests |= ((REQ_CLOCK) | (REQ_SIG) | (REQ_BATTERY));
clearReq(REQ_STARTUP); clearReq(REQ_STARTUP);
for (int i = 0; i < THREADEDGSM_INTERVAL_COUNT; i++) for (unsigned long & i : tickSync)
tickSync[i] = millis(); i = millis();
if (this->configuration.ready != nullptr) if (mConfiguration.ready != nullptr)
this->configuration.ready(*this); mConfiguration.ready(*this);
} else } else
state = STARTUP_POWER_OFF; mState = STARTUP_POWER_OFF;
break; break;
} }
if (state != lastState) { if (mState != lastState) {
DEBUG_PRINT(F("STARTUP_STATE: ")); DEBUG_PRINT(F("STARTUP_STATE: "));
DEBUG_PRINTLN(state); DEBUG_PRINTLN(mState);
} }
} }
@ -223,26 +223,26 @@ void ThreadedGSM::Startup()
void ThreadedGSM::Clock() void ThreadedGSM::Clock()
{ {
String clockTime; String clockTime;
int lastState = state; int lastState = mState;
switch (state) { switch (mState) {
case CLOCK_REQ: case CLOCK_REQ:
dte.SendCommand(F("AT+CCLK?\r"), THREADEDGSM_AT_TIMEOUT, "OK\r"); mDte.SendCommand(F("AT+CCLK?\r"), THREADEDGSM_AT_TIMEOUT, "OK\r");
state = CLOCK_VERIFY; mState = CLOCK_VERIFY;
break; break;
case CLOCK_VERIFY: case CLOCK_VERIFY:
int index = dte.getBuffer().indexOf(F("+CCLK: ")); int index = mDte.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 = mDte.getBuffer().indexOf(F("+"), index);
if (endindex >= 0) if (endindex >= 0)
clockTime = dte.getBuffer().substring(index, endindex); clockTime = mDte.getBuffer().substring(index, endindex);
else { else {
endindex = dte.getBuffer().indexOf(F("-"), index); endindex = mDte.getBuffer().indexOf(F("-"), index);
if (endindex >= 0) if (endindex >= 0)
clockTime = dte.getBuffer().substring(index, endindex); clockTime = mDte.getBuffer().substring(index, endindex);
} }
if (endindex >= 0) { if (endindex >= 0) {
@ -254,192 +254,192 @@ void ThreadedGSM::Clock()
ClockTime.hour = clockTime.substring(9, 11).toInt(); ClockTime.hour = clockTime.substring(9, 11).toInt();
ClockTime.minute = clockTime.substring(12, 14).toInt(); ClockTime.minute = clockTime.substring(12, 14).toInt();
ClockTime.second = clockTime.substring(15, 17).toInt(); ClockTime.second = clockTime.substring(15, 17).toInt();
if (this->configuration.clock != nullptr) if (mConfiguration.clock != nullptr)
this->configuration.clock(*this, ClockTime); mConfiguration.clock(*this, ClockTime);
} }
} }
clearReq(REQ_CLOCK); clearReq(REQ_CLOCK);
break; break;
} }
if (state != lastState) { if (mState != lastState) {
DEBUG_PRINT(F("CLOCK_STATE: ")); DEBUG_PRINT(F("CLOCK_STATE: "));
DEBUG_PRINTLN(state); DEBUG_PRINTLN(mState);
} }
} }
void ThreadedGSM::Signal() void ThreadedGSM::Signal()
{ {
int lastState = state; int lastState = mState;
switch (state) { switch (mState) {
case SIGNAL_REQ: case SIGNAL_REQ:
dte.SendCommand(F("AT+CSQ\r"), THREADEDGSM_AT_TIMEOUT, "OK\r"); mDte.SendCommand(F("AT+CSQ\r"), THREADEDGSM_AT_TIMEOUT, "OK\r");
state = SIGNAL_VERIFY; mState = SIGNAL_VERIFY;
break; break;
case SIGNAL_VERIFY: case SIGNAL_VERIFY:
int index = dte.getBuffer().indexOf(F("+CSQ: ")); int index = mDte.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 = mDte.getBuffer().substring(index, index + 2).toInt();
GsmSignal.Dbm = dte.getBuffer().substring(index + 3, index + 5).toInt(); GsmSignal.Dbm = mDte.getBuffer().substring(index + 3, index + 5).toInt();
if (GsmSignal.Value != 0) { if (GsmSignal.Value != 0) {
if (this->configuration.signal != nullptr) if (mConfiguration.signal != nullptr)
this->configuration.signal(*this, GsmSignal); mConfiguration.signal(*this, GsmSignal);
} }
} }
clearReq(REQ_SIG); clearReq(REQ_SIG);
break; break;
} }
if (state != lastState) { if (mState != lastState) {
DEBUG_PRINT(F("SIGNAL_STATE: ")); DEBUG_PRINT(F("SIGNAL_STATE: "));
DEBUG_PRINTLN(state); DEBUG_PRINTLN(mState);
} }
} }
void ThreadedGSM::Battery() void ThreadedGSM::Battery()
{ {
int lastState = state; int lastState = mState;
switch (state) { switch (mState) {
case BATTERY_REQ: case BATTERY_REQ:
dte.SendCommand(F("AT+CBC\r"), THREADEDGSM_AT_TIMEOUT, "OK\r"); mDte.SendCommand(F("AT+CBC\r"), THREADEDGSM_AT_TIMEOUT, "OK\r");
state = BATTERY_VERIFY; mState = BATTERY_VERIFY;
break; break;
case BATTERY_VERIFY: case BATTERY_VERIFY:
int index = dte.getBuffer().indexOf(F("+CBC:")); int index = mDte.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 = mDte.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 mResult 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 mResult to interger
if (this->configuration.battery != nullptr) if (mConfiguration.battery != nullptr)
this->configuration.battery(*this, BattInfo); mConfiguration.battery(*this, BattInfo);
} }
clearReq(REQ_BATTERY); clearReq(REQ_BATTERY);
break; break;
} }
if (state != lastState) { if (mState != lastState) {
DEBUG_PRINT(F("BATTERY_STATE: ")); DEBUG_PRINT(F("BATTERY_STATE: "));
DEBUG_PRINTLN(state); DEBUG_PRINTLN(mState);
} }
} }
void ThreadedGSM::clearReq(int req) void ThreadedGSM::clearReq(int req)
{ {
requests &= ~(req); mRequests &= ~(req);
nextJob(); nextJob();
} }
void ThreadedGSM::Inbox() void ThreadedGSM::Inbox()
{ {
String CMD; String CMD;
int lastState = state; int lastState = mState;
switch (state) { switch (mState) {
case READ_REQ: case READ_REQ:
SMSi.Text = ""; mSMSi.Text = "";
SMSi.Number = ""; mSMSi.Number = "";
dte.SendCommand(F("AT+CMGF=0\r"), THREADEDGSM_AT_TIMEOUT, "OK\r"); // SMS Message format set as PDU mDte.SendCommand(F("AT+CMGF=0\r"), THREADEDGSM_AT_TIMEOUT, "OK\r"); // SMS Message format set as PDU
state = READ_CHK_CMGF; mState = READ_CHK_CMGF;
break; break;
case READ_CHK_CMGF: case READ_CHK_CMGF:
if (dte.getResult() == DTE::EXPECT_RESULT) { if (mDte.getResult() == DTE::EXPECT_RESULT) {
dte.SendCommand(F("AT+CPMS=\"SM\"\r"), THREADEDGSM_AT_TIMEOUT, "OK\r"); // SIM Message storage mDte.SendCommand(F("AT+CPMS=\"SM\"\r"), THREADEDGSM_AT_TIMEOUT, "OK\r"); // SIM Message storage
state = READ_CHK_CPMS; mState = 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 (mDte.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, ","); mDte.SendCommand(CMD.c_str(), THREADEDGSM_AT_TIMEOUT, ",");
state = READ_CHK_CMGL; mState = 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 (mDte.getResult() == DTE::EXPECT_RESULT) {
// fetch index // fetch index
int indexStart = dte.getBuffer().indexOf(F("+CMGL: ")); int indexStart = mDte.getBuffer().indexOf(F("+CMGL: "));
if (indexStart >= 0) { if (indexStart >= 0) {
Message.Index = dte.getBuffer().substring(indexStart + 7, dte.getBuffer().indexOf(F(","))).toInt(); Message.Index = mDte.getBuffer().substring(indexStart + 7, mDte.getBuffer().indexOf(F(","))).toInt();
if (Message.Index != 0) { if (Message.Index != 0) {
dte.Delay(2000); mDte.Delay(2000);
state = READ_DELAY_CLEAR_BUFF; mState = READ_DELAY_CLEAR_BUFF;
} }
} }
} }
if (state != READ_DELAY_CLEAR_BUFF) if (mState != 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 mDte.SendCommand(F("AT+CMGF=1\r"), THREADEDGSM_AT_TIMEOUT, "OK\r"); // SMS Message format set as TEXT
state = READ_TEXT_CMGR; mState = READ_TEXT_CMGR;
break; break;
case READ_TEXT_CMGR: case READ_TEXT_CMGR:
if (dte.getResult() == DTE::EXPECT_RESULT) { if (mDte.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"); mDte.SendCommand(CMD.c_str(), THREADEDGSM_AT_TIMEOUT, "OK\r");
state = READ_CHK_CMGR; mState = 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 (mDte.getResult() == DTE::EXPECT_RESULT) {
int indexStart = dte.getBuffer().indexOf(F("+CMGR: ")); int indexStart = mDte.getBuffer().indexOf(F("+CMGR: "));
if (indexStart >= 0) { if (indexStart >= 0) {
int indexStartPDU = dte.getBuffer().indexOf(F("\r\n"), indexStart); int indexStartPDU = mDte.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 = mDte.getBuffer().indexOf(F("\r"), indexStartPDU);
if (indexEndPDU >= 0) if (indexEndPDU >= 0)
SMSi.Text = dte.getBuffer().substring(indexStartPDU, indexEndPDU); mSMSi.Text = mDte.getBuffer().substring(indexStartPDU, indexEndPDU);
} }
indexStartPDU = dte.getBuffer().indexOf(F(",\""), indexStart); indexStartPDU = mDte.getBuffer().indexOf(F(",\""), indexStart);
if (indexStartPDU >= 0) { if (indexStartPDU >= 0) {
indexStartPDU += 2; indexStartPDU += 2;
int indexEndPDU = dte.getBuffer().indexOf(F("\","), indexStartPDU); int indexEndPDU = mDte.getBuffer().indexOf(F("\","), indexStartPDU);
if (indexEndPDU >= 0) if (indexEndPDU >= 0)
SMSi.Number = dte.getBuffer().substring(indexStartPDU, indexEndPDU); mSMSi.Number = mDte.getBuffer().substring(indexStartPDU, indexEndPDU);
} }
} }
CMD = F("AT+CMGD="); // Delete the SMS message CMD = F("AT+CMGD="); // Delete the SMS message
CMD += Message.Index; CMD += Message.Index;
CMD += "\r"; CMD += "\r";
dte.SendCommand(CMD.c_str(), THREADEDGSM_AT_TIMEOUT, "OK\r"); mDte.SendCommand(CMD.c_str(), THREADEDGSM_AT_TIMEOUT, "OK\r");
state = READ_CHK_CMGD; mState = READ_CHK_CMGD;
} else } else
clearReq(REQ_INBOX); clearReq(REQ_INBOX);
break; break;
case READ_CHK_CMGD: case READ_CHK_CMGD:
// if( (dte.getResult() == DTE::EXPECT_RESULT) && (SMS.InboxMsgContents != "")) // if( (mDte.getResult() == DTE::EXPECT_RESULT) && (SMS.InboxMsgContents != ""))
if (dte.getResult() == DTE::EXPECT_RESULT) { if (mDte.getResult() == DTE::EXPECT_RESULT) {
if (this->configuration.incoming != nullptr) if (mConfiguration.incoming != nullptr)
this->configuration.incoming(*this, SMSi); mConfiguration.incoming(*this, mSMSi);
} }
clearReq(REQ_INBOX); clearReq(REQ_INBOX);
break; break;
} }
if (state != lastState) { if (mState != lastState) {
DEBUG_PRINT(F("INBOX_STATE: ")); DEBUG_PRINT(F("INBOX_STATE: "));
DEBUG_PRINTLN(state); DEBUG_PRINTLN(mState);
} }
} }
@ -448,65 +448,65 @@ void ThreadedGSM::Outbox()
String CMD; String CMD;
// CMD.reserve(200); // CMD.reserve(200);
int lastState = state; int lastState = mState;
switch (state) { switch (mState) {
case SEND_REQ: case SEND_REQ:
dte.SendCommand(F("AT+CMGF=1\r"), THREADEDGSM_AT_TIMEOUT, "OK\r"); // SMS Text mode mDte.SendCommand(F("AT+CMGF=1\r"), THREADEDGSM_AT_TIMEOUT, "OK\r"); // SMS Text mode
state = SEND_CHK_CMGF; mState = SEND_CHK_CMGF;
break; break;
case SEND_CHK_CMGF: case SEND_CHK_CMGF:
if (dte.getResult() == DTE::EXPECT_RESULT) { if (mDte.getResult() == DTE::EXPECT_RESULT) {
CMD = F("AT+CMGS=\""); CMD = F("AT+CMGS=\"");
CMD += SMSo.Number; CMD += mSMSo.Number;
CMD += "\"\r"; CMD += "\"\r";
dte.SendCommand(CMD.c_str(), 15000, "> "); mDte.SendCommand(CMD.c_str(), 15000, "> ");
state = SEND_CHK_RDY; mState = 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 (mDte.getResult() == DTE::EXPECT_RESULT) {
CMD = SMSo.Text; CMD = mSMSo.Text;
CMD += (char) 26; CMD += (char) 26;
dte.SendCommand(CMD.c_str(), 10000, "OK\r"); mDte.SendCommand(CMD.c_str(), 10000, "OK\r");
state = SEND_CHK_OK; mState = SEND_CHK_OK;
} else } else
clearReq(REQ_OUTBOX); clearReq(REQ_OUTBOX);
break; break;
case SEND_CHK_OK: case SEND_CHK_OK:
if (dte.getResult() == DTE::EXPECT_RESULT) { if (mDte.getResult() == DTE::EXPECT_RESULT) {
if (this->configuration.outgoing != nullptr) if (mConfiguration.outgoing != nullptr)
this->configuration.outgoing(*this); mConfiguration.outgoing(*this);
} }
clearReq(REQ_OUTBOX); clearReq(REQ_OUTBOX);
break; break;
} }
if (state != lastState) { if (mState != lastState) {
DEBUG_PRINT(F("OUTBOX_STATE: ")); DEBUG_PRINT(F("OUTBOX_STATE: "));
DEBUG_PRINTLN(state); DEBUG_PRINTLN(mState);
} }
} }
void ThreadedGSM::CheckRing() void ThreadedGSM::CheckRing()
{ {
int lastState = ringState; int lastState = mRingState;
switch (ringState) { switch (mRingState) {
case RING_WAIT: case RING_WAIT:
break; break;
case RING_CHK: case RING_CHK:
if (this->configuration.ring != nullptr) { if (mConfiguration.ring != nullptr) {
String s = "Ring"; String s = "Ring";
this->configuration.ring(*this, s); mConfiguration.ring(*this, s);
} }
ringState = RING_WAIT; mRingState = RING_WAIT;
break; break;
} }
if (ringState != lastState) { if (mRingState != lastState) {
DEBUG_PRINT(F("RING_STATE: ")); DEBUG_PRINT(F("RING_STATE: "));
DEBUG_PRINTLN(ringState); DEBUG_PRINTLN(mRingState);
} }
} }

View File

@ -144,25 +144,25 @@ class ThreadedGSM : public Executable
RING_CHK, RING_CHK,
}; };
unsigned long tick; unsigned long mTick = 0;
struct struct
{ {
int Index; // Index of readed message int Index; // Index of read message
} Message; } Message{};
SMSInfo SMSi; // Inbox SMS (incoming) SMSInfo mSMSi; // Inbox SMS (incoming)
SMSInfo SMSo; // Outbox SMS (outgoing) SMSInfo mSMSo; // Outbox SMS (outgoing)
Stream & stream; Stream & mStream;
DTE dte; DTE mDte;
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 mConfiguration = {nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr};
conf configuration{0}; conf mConfiguration{};
enum ReqTypes { enum ReqTypes {
REQ_CLOCK = 1, REQ_CLOCK = 1,
@ -172,10 +172,10 @@ class ThreadedGSM : public Executable
REQ_STARTUP = 16, REQ_STARTUP = 16,
REQ_BATTERY = 32, REQ_BATTERY = 32,
}; };
int requests; int mRequests = 0;
int state; int mState = 0;
int job; int mJob = 0;
int ringState; int mRingState;
// functions // functions
public: public:
explicit ThreadedGSM(Stream & stream); explicit ThreadedGSM(Stream & stream);