Rozhrani pro detekci RING a CLIP z vetsi casti funkcni

This commit is contained in:
Pavel Brychta 2023-09-03 19:06:43 +02:00
parent 78bd8a981d
commit c2d0707100
4 changed files with 67 additions and 12 deletions

View File

@ -16,7 +16,7 @@ void DTE::SendCommand(const char * command, unsigned long timeout, const char *
mTimeout = timeout; mTimeout = timeout;
flush(); flush();
mStream.print(command); mStream.print(command);
mBuffer = ""; clearBuffer();
mTick = millis(); mTick = millis();
process(); process();
} }
@ -34,7 +34,7 @@ void DTE::SendCommand(const __FlashStringHelper * command, unsigned long timeout
flush(); flush();
// send command // send command
mStream.print((const __FlashStringHelper *) command); mStream.print((const __FlashStringHelper *) command);
mBuffer = ""; clearBuffer();
mTick = millis(); mTick = millis();
process(); process();
} }
@ -120,3 +120,25 @@ void DTE::process()
// time out // time out
mResult = EXPECT_TIMEOUT; mResult = EXPECT_TIMEOUT;
} }
bool DTE::buffer()
{
while (mStream.available() > 0 && mBuffer.length() < mBufferSize) {
char c = mStream.read();
if (c >= 0x20) {
mBuffer.concat(c);
} else if (c == '\r')
return true;
}
if (mBuffer.length() == mBufferSize)
clearBuffer();
return false;
}
void DTE::clearBuffer()
{
mBuffer = "";
}

View File

@ -46,6 +46,10 @@ class DTE {
String & getBuffer(); String & getBuffer();
bool buffer();
void clearBuffer();
protected: protected:
void flush(); void flush();

View File

@ -1,4 +1,5 @@
#include "ThreadedGSM.h" #include "ThreadedGSM.h"
#include <trace.h>
#ifdef THREADEDGSM_DEBUG #ifdef THREADEDGSM_DEBUG
#define DEBUG_PRINT(x) THREADEDGSM_DEBUG.print(x) #define DEBUG_PRINT(x) THREADEDGSM_DEBUG.print(x)
@ -203,15 +204,19 @@ void ThreadedGSM::Startup()
case STARTUP_CHK_CENG: case STARTUP_CHK_CENG:
if (mDte.getResult() == DTE::EXPECT_RESULT) { if (mDte.getResult() == DTE::EXPECT_RESULT) {
mRequests |= ((REQ_CLOCK) | (REQ_SIG) | (REQ_BATTERY)); mDte.SendCommand(F("AT+CLIP=1\r"), THREADEDGSM_AT_TIMEOUT, "OK\r");
clearReq(REQ_STARTUP); mState = STARTUP_CHK_CLIP;
for (unsigned long & i : tickSync)
i = millis();
if (mConfiguration.ready != nullptr)
mConfiguration.ready(*this);
} else } else
mState = STARTUP_POWER_OFF; mState = STARTUP_POWER_OFF;
break; break;
case STARTUP_CHK_CLIP:
if (mDte.getResult() == DTE::EXPECT_RESULT) {
startupDone();
} else {
mState = STARTUP_POWER_OFF;
}
break;
} }
if (mState != lastState) { if (mState != lastState) {
DEBUG_PRINT(F("STARTUP_STATE: ")); DEBUG_PRINT(F("STARTUP_STATE: "));
@ -333,6 +338,7 @@ void ThreadedGSM::Battery()
void ThreadedGSM::clearReq(int req) void ThreadedGSM::clearReq(int req)
{ {
mRequests &= ~(req); mRequests &= ~(req);
mDte.clearBuffer();
nextJob(); nextJob();
} }
@ -370,9 +376,10 @@ void ThreadedGSM::Inbox()
case READ_CHK_CMGL: case READ_CHK_CMGL:
if (mDte.getResult() == DTE::EXPECT_RESULT) { if (mDte.getResult() == DTE::EXPECT_RESULT) {
// fetch index // fetch index
int indexStart = mDte.getBuffer().indexOf(F("+CMGL: ")); String & buffer = mDte.getBuffer();
int indexStart = buffer.indexOf(F("+CMGL: "));
if (indexStart >= 0) { if (indexStart >= 0) {
Message.Index = mDte.getBuffer().substring(indexStart + 7, mDte.getBuffer().indexOf(F(","))).toInt(); Message.Index = buffer.substring(indexStart + 7, buffer.indexOf(F(","))).toInt();
if (Message.Index != 0) { if (Message.Index != 0) {
mDte.Delay(2000); mDte.Delay(2000);
mState = READ_DELAY_CLEAR_BUFF; mState = READ_DELAY_CLEAR_BUFF;
@ -381,7 +388,6 @@ void ThreadedGSM::Inbox()
} }
if (mState != 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:
@ -493,8 +499,18 @@ void ThreadedGSM::Outbox()
void ThreadedGSM::CheckRing() void ThreadedGSM::CheckRing()
{ {
int lastState = mRingState; int lastState = mRingState;
switch (mRingState) { switch (mRingState) {
case RING_WAIT: case RING_WAIT:
if (mDte.buffer()) {
String & buffer = mDte.getBuffer();
if (buffer.length() > 0) {
TRACE(TRACE_INFO, F("Gsm: %s"), buffer.c_str());
mDte.clearBuffer();
}
}
break; break;
case RING_CHK: case RING_CHK:
@ -510,3 +526,13 @@ void ThreadedGSM::CheckRing()
DEBUG_PRINTLN(mRingState); DEBUG_PRINTLN(mRingState);
} }
} }
void ThreadedGSM::startupDone()
{
mRequests |= ((REQ_CLOCK) | (REQ_SIG) | (REQ_BATTERY));
clearReq(REQ_STARTUP);
for (unsigned long & i : tickSync)
i = millis();
if (mConfiguration.ready != nullptr)
mConfiguration.ready(*this);
}

View File

@ -103,7 +103,8 @@ class ThreadedGSM : public Executable
STARTUP_CHK_CPIN, STARTUP_CHK_CPIN,
STARTUP_CHK_CREG, STARTUP_CHK_CREG,
STARTUP_CHK_CLTS, STARTUP_CHK_CLTS,
STARTUP_CHK_CENG STARTUP_CHK_CENG,
STARTUP_CHK_CLIP
}; };
enum StatesClock { enum StatesClock {
@ -200,6 +201,8 @@ class ThreadedGSM : public Executable
void sendSMS(String & Number, const char * Text); void sendSMS(String & Number, const char * Text);
protected: protected:
void startupDone();
// States // States
void Startup(); void Startup();