diff --git a/src/ThreadedGSM.cpp b/src/ThreadedGSM.cpp index dcba7ee..047e631 100644 --- a/src/ThreadedGSM.cpp +++ b/src/ThreadedGSM.cpp @@ -90,6 +90,8 @@ void ThreadedGSM::exec() mJob = REQ_STARTUP; else if (mRequests & REQ_BATTERY) mJob = REQ_BATTERY; + else if (mRequests & REQ_HANGUP) + mJob = REQ_HANGUP; if (mJob) { mState = 0; @@ -111,6 +113,8 @@ void ThreadedGSM::exec() Outbox(); else if (mJob == REQ_BATTERY) Battery(); + else if (mJob == REQ_HANGUP) + Hangup(); else CheckRing(); } @@ -335,6 +339,29 @@ void ThreadedGSM::Battery() } } +void ThreadedGSM::Hangup() +{ + int lastState = mState; + + switch (mState) { + case HANGUP_REQ: + mDte.SendCommand(F("ATH\r"), THREADEDGSM_AT_TIMEOUT, "OK\r"); + mState = HANGUP_CHK; + break; + + case HANGUP_CHK: + if (mDte.getResult() == DTE::EXPECT_RESULT) { + clearReq(REQ_HANGUP); + } else + clearReq(REQ_HANGUP); + break; + } + if (mState != lastState) { + DEBUG_PRINT(F("HANGUP_STATE: ")); + DEBUG_PRINTLN(mState); + } +} + void ThreadedGSM::clearReq(int req) { mRequests &= ~(req); @@ -498,41 +525,75 @@ void ThreadedGSM::Outbox() void ThreadedGSM::CheckRing() { - int lastState = mRingState; - switch (mRingState) { - case RING_WAIT: - if (mDte.buffer()) { - String & buffer = mDte.getBuffer(); + if (mDte.buffer()) { + String & buffer = mDte.getBuffer(); - if (buffer.length() > 0) { + if (buffer.length() > 0) { + bool dump = true; - TRACE(TRACE_INFO, F("Gsm: %s"), buffer.c_str()); - mDte.clearBuffer(); + if (buffer.startsWith(F("+CLIP: \""))) { + // CLIP (RING) detected + if (mConfiguration.ring) { + int index = 8; + int endindex; + + endindex = buffer.indexOf(F("\""), index); + if (endindex >= 0) { + String num = buffer.substring(index, endindex); + + mConfiguration.ring(*this, num); + dump = false; + } + } + } else if (buffer.startsWith(F("NO CARRIER"))) { + // end of call detected + if (mConfiguration.ring) { + String num = F("end"); + + mConfiguration.ring(*this, num); + dump = false; + } + } else if (buffer.startsWith(F("+CMTI: \"SM\""))) { + // SMS receive detected + if (mConfiguration.incoming) { + mRequests |= (REQ_INBOX); + dump = false; } } - break; - - case RING_CHK: - if (mConfiguration.ring != nullptr) { - String s = "Ring"; - mConfiguration.ring(*this, s); + if (dump) { + TRACE(TRACE_DEBUG, F("Gsm: %s"), buffer.c_str()); } - mRingState = RING_WAIT; - break; - } - if (mRingState != lastState) { - DEBUG_PRINT(F("RING_STATE: ")); - DEBUG_PRINTLN(mRingState); + mDte.clearBuffer(); + } } } void ThreadedGSM::startupDone() { - mRequests |= ((REQ_CLOCK) | (REQ_SIG) | (REQ_BATTERY)); + if (mConfiguration.clock) + mRequests |= (REQ_CLOCK); + if (mConfiguration.battery) + mRequests |= (REQ_BATTERY); + if (mConfiguration.signal) + mRequests |= (REQ_SIG); + if (mConfiguration.incoming) + mRequests |= (REQ_INBOX); clearReq(REQ_STARTUP); for (unsigned long & i : tickSync) i = millis(); if (mConfiguration.ready != nullptr) mConfiguration.ready(*this); } + +void ThreadedGSM::hangup() +{ + + mRequests |= (REQ_HANGUP); +} + +void ThreadedGSM::restartModem() +{ + + mRequests |= (REQ_STARTUP); +} diff --git a/src/ThreadedGSM.h b/src/ThreadedGSM.h index 57cd150..53b8a86 100644 --- a/src/ThreadedGSM.h +++ b/src/ThreadedGSM.h @@ -140,6 +140,11 @@ class ThreadedGSM : public Executable SEND_CHK_OK }; + enum StateHangup { + HANGUP_REQ, + HANGUP_CHK + }; + enum StatesCheckRing { RING_WAIT, RING_CHK, @@ -152,8 +157,8 @@ class ThreadedGSM : public Executable int Index; // Index of read message } Message{}; - SMSInfo mSMSi; // Inbox SMS (incoming) - SMSInfo mSMSo; // Outbox SMS (outgoing) + SMSInfo mSMSi{}; // Inbox SMS (incoming) + SMSInfo mSMSo{}; // Outbox SMS (outgoing) Stream & mStream; DTE mDte; @@ -172,6 +177,7 @@ class ThreadedGSM : public Executable REQ_OUTBOX = 8, REQ_STARTUP = 16, REQ_BATTERY = 32, + REQ_HANGUP = 64, }; int mRequests = 0; int mState = 0; @@ -200,6 +206,10 @@ class ThreadedGSM : public Executable void sendSMS(String & Number, const char * Text); + void hangup(); + + void restartModem(); + protected: void startupDone(); @@ -219,5 +229,7 @@ class ThreadedGSM : public Executable void Outbox(); + void Hangup(); + void CheckRing(); }; // ThreadedGSM