diff --git a/src/DTE.cpp b/src/DTE.cpp index a394160..b372bd1 100644 --- a/src/DTE.cpp +++ b/src/DTE.cpp @@ -16,7 +16,7 @@ void DTE::SendCommand(const char * command, unsigned long timeout, const char * mTimeout = timeout; flush(); mStream.print(command); - mBuffer = ""; + clearBuffer(); mTick = millis(); process(); } @@ -34,7 +34,7 @@ void DTE::SendCommand(const __FlashStringHelper * command, unsigned long timeout flush(); // send command mStream.print((const __FlashStringHelper *) command); - mBuffer = ""; + clearBuffer(); mTick = millis(); process(); } @@ -120,3 +120,25 @@ void DTE::process() // time out 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 = ""; +} diff --git a/src/DTE.h b/src/DTE.h index 57afdab..64c6be9 100644 --- a/src/DTE.h +++ b/src/DTE.h @@ -46,6 +46,10 @@ class DTE { String & getBuffer(); + bool buffer(); + + void clearBuffer(); + protected: void flush(); diff --git a/src/ThreadedGSM.cpp b/src/ThreadedGSM.cpp index 3d5e5f9..dcba7ee 100644 --- a/src/ThreadedGSM.cpp +++ b/src/ThreadedGSM.cpp @@ -1,4 +1,5 @@ #include "ThreadedGSM.h" +#include #ifdef THREADEDGSM_DEBUG #define DEBUG_PRINT(x) THREADEDGSM_DEBUG.print(x) @@ -203,15 +204,19 @@ void ThreadedGSM::Startup() case STARTUP_CHK_CENG: if (mDte.getResult() == DTE::EXPECT_RESULT) { - mRequests |= ((REQ_CLOCK) | (REQ_SIG) | (REQ_BATTERY)); - clearReq(REQ_STARTUP); - for (unsigned long & i : tickSync) - i = millis(); - if (mConfiguration.ready != nullptr) - mConfiguration.ready(*this); + mDte.SendCommand(F("AT+CLIP=1\r"), THREADEDGSM_AT_TIMEOUT, "OK\r"); + mState = STARTUP_CHK_CLIP; } else mState = STARTUP_POWER_OFF; break; + + case STARTUP_CHK_CLIP: + if (mDte.getResult() == DTE::EXPECT_RESULT) { + startupDone(); + } else { + mState = STARTUP_POWER_OFF; + } + break; } if (mState != lastState) { DEBUG_PRINT(F("STARTUP_STATE: ")); @@ -333,6 +338,7 @@ void ThreadedGSM::Battery() void ThreadedGSM::clearReq(int req) { mRequests &= ~(req); + mDte.clearBuffer(); nextJob(); } @@ -370,9 +376,10 @@ void ThreadedGSM::Inbox() case READ_CHK_CMGL: if (mDte.getResult() == DTE::EXPECT_RESULT) { // fetch index - int indexStart = mDte.getBuffer().indexOf(F("+CMGL: ")); + String & buffer = mDte.getBuffer(); + int indexStart = buffer.indexOf(F("+CMGL: ")); 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) { mDte.Delay(2000); mState = READ_DELAY_CLEAR_BUFF; @@ -381,7 +388,6 @@ void ThreadedGSM::Inbox() } if (mState != READ_DELAY_CLEAR_BUFF) clearReq(REQ_INBOX); - break; case READ_DELAY_CLEAR_BUFF: @@ -493,8 +499,18 @@ void ThreadedGSM::Outbox() void ThreadedGSM::CheckRing() { int lastState = mRingState; + switch (mRingState) { 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; case RING_CHK: @@ -510,3 +526,13 @@ void ThreadedGSM::CheckRing() 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); +} diff --git a/src/ThreadedGSM.h b/src/ThreadedGSM.h index 26f0c72..57cd150 100644 --- a/src/ThreadedGSM.h +++ b/src/ThreadedGSM.h @@ -103,7 +103,8 @@ class ThreadedGSM : public Executable STARTUP_CHK_CPIN, STARTUP_CHK_CREG, STARTUP_CHK_CLTS, - STARTUP_CHK_CENG + STARTUP_CHK_CENG, + STARTUP_CHK_CLIP }; enum StatesClock { @@ -200,6 +201,8 @@ class ThreadedGSM : public Executable void sendSMS(String & Number, const char * Text); protected: + void startupDone(); + // States void Startup();