WIP: pridani ring indikace (zatim nefunkcni)
This commit is contained in:
parent
49ffb2109c
commit
e2c5e5dcd9
117
src/DTE.h
117
src/DTE.h
@ -5,24 +5,22 @@
|
|||||||
* Author: Neta Yahav
|
* Author: Neta Yahav
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef __DTE_H__
|
#pragma once
|
||||||
#define __DTE_H__
|
|
||||||
|
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
|
||||||
class DTE
|
class DTE
|
||||||
{
|
{
|
||||||
//variables
|
|
||||||
public:
|
public:
|
||||||
enum CommandResult
|
enum CommandResult
|
||||||
{
|
{
|
||||||
EXPECT_BUSY,
|
EXPECT_BUSY,
|
||||||
EXPECT_TIMEOUT,
|
EXPECT_TIMEOUT,
|
||||||
EXPECT_DELAY,
|
EXPECT_DELAY,
|
||||||
EXPECT_RESULT
|
EXPECT_RESULT,
|
||||||
|
EXPECT_RING
|
||||||
};
|
};
|
||||||
|
|
||||||
protected:
|
|
||||||
private:
|
private:
|
||||||
String buffer;
|
String buffer;
|
||||||
Stream& stream;
|
Stream& stream;
|
||||||
@ -33,33 +31,32 @@ private:
|
|||||||
unsigned long tick;
|
unsigned long tick;
|
||||||
unsigned int match;
|
unsigned int match;
|
||||||
CommandResult result;
|
CommandResult result;
|
||||||
//functions
|
|
||||||
public:
|
public:
|
||||||
DTE(Stream& stream, unsigned int size) : stream(stream), bufferSize(size), result(EXPECT_RESULT) { buffer.reserve(size); }
|
DTE(Stream& stream, unsigned int size)
|
||||||
|
: stream(stream)
|
||||||
|
, bufferSize(size)
|
||||||
|
, result(EXPECT_RESULT) {
|
||||||
|
buffer.reserve(size);
|
||||||
|
}
|
||||||
|
|
||||||
~DTE() {};
|
~DTE() {};
|
||||||
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;
|
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
|
flush();
|
||||||
while (stream.available())
|
|
||||||
{
|
|
||||||
stream.read();
|
|
||||||
}
|
|
||||||
// send command
|
|
||||||
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) {
|
||||||
{
|
|
||||||
// char cmd[32];
|
|
||||||
|
|
||||||
match = 0;
|
match = 0;
|
||||||
result = EXPECT_BUSY;
|
result = EXPECT_BUSY;
|
||||||
@ -68,74 +65,90 @@ public:
|
|||||||
response[2] = response3;
|
response[2] = response3;
|
||||||
this->timeout = timeout;
|
this->timeout = timeout;
|
||||||
// clear rx buffer
|
// clear rx buffer
|
||||||
while (stream.available())
|
flush();
|
||||||
{
|
|
||||||
stream.read();
|
|
||||||
}
|
|
||||||
// send command
|
// send command
|
||||||
// strcpy_P(cmd, command);
|
|
||||||
stream.print((const __FlashStringHelper *)command);
|
stream.print((const __FlashStringHelper *)command);
|
||||||
// stream.print(cmd);
|
|
||||||
buffer = "";
|
buffer = "";
|
||||||
tick = millis();
|
tick = millis();
|
||||||
proccess();
|
proccess();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Delay(unsigned long delay)
|
void Delay(unsigned long delay) {
|
||||||
{
|
|
||||||
timeout = delay;
|
timeout = delay;
|
||||||
result = EXPECT_DELAY;
|
result = EXPECT_DELAY;
|
||||||
tick = millis();
|
tick = millis();
|
||||||
proccess();
|
proccess();
|
||||||
}
|
}
|
||||||
bool getIsBusy()
|
|
||||||
{
|
bool getIsBusy(void) {
|
||||||
proccess();
|
proccess();
|
||||||
return (result == EXPECT_BUSY) || (result == EXPECT_DELAY);
|
return (result == EXPECT_BUSY) || (result == EXPECT_DELAY);
|
||||||
}
|
}
|
||||||
CommandResult getResult() { return result ; }
|
|
||||||
unsigned int getMatch() { return match; }
|
CommandResult getResult(void) {
|
||||||
String& getBuffer() { return buffer; }
|
return result;
|
||||||
protected:
|
}
|
||||||
|
|
||||||
|
unsigned int getMatch(void) {
|
||||||
|
return match;
|
||||||
|
}
|
||||||
|
|
||||||
|
String& getBuffer(void) {
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void proccess()
|
|
||||||
{
|
void flush(void) {
|
||||||
if(result == EXPECT_DELAY)
|
// clear rx buffer
|
||||||
{
|
while (stream.available()) {
|
||||||
|
stream.read();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void proccess(void) {
|
||||||
|
if (result == EXPECT_DELAY) {
|
||||||
if(millis() - tick >= timeout)
|
if(millis() - tick >= timeout)
|
||||||
result = EXPECT_RESULT;
|
result = EXPECT_RESULT;
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(result != EXPECT_BUSY) return;
|
if (result != EXPECT_BUSY) {
|
||||||
|
if (stream.available()) {
|
||||||
|
char c;
|
||||||
|
|
||||||
|
c = stream.read();
|
||||||
|
buffer += c;
|
||||||
|
if (buffer.length() > 4) {
|
||||||
|
if (buffer.endsWith(F("RING\n"))) {
|
||||||
|
result = EXPECT_RING;
|
||||||
|
}
|
||||||
|
buffer.remove(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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;
|
||||||
@ -150,5 +163,3 @@ private:
|
|||||||
result = EXPECT_TIMEOUT;
|
result = EXPECT_TIMEOUT;
|
||||||
}
|
}
|
||||||
}; //DTE
|
}; //DTE
|
||||||
|
|
||||||
#endif //__DTE_H__
|
|
||||||
|
@ -5,9 +5,7 @@
|
|||||||
* Author: Neta Yahav
|
* Author: Neta Yahav
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
#ifndef __THREADEDGSM_H__
|
|
||||||
#define __THREADEDGSM_H__
|
|
||||||
|
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include "DTE.h"
|
#include "DTE.h"
|
||||||
@ -96,6 +94,7 @@ public:
|
|||||||
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&);
|
||||||
struct conf
|
struct conf
|
||||||
{
|
{
|
||||||
ThreadedGSMCallbackSignal signal;
|
ThreadedGSMCallbackSignal signal;
|
||||||
@ -105,6 +104,7 @@ public:
|
|||||||
ThreadedGSMCallback outgoing;
|
ThreadedGSMCallback outgoing;
|
||||||
ThreadedGSMCallbackBool power;
|
ThreadedGSMCallbackBool power;
|
||||||
ThreadedGSMCallbackBattery battery;
|
ThreadedGSMCallbackBattery battery;
|
||||||
|
ThreadedGSMCallbackRing ring;
|
||||||
};
|
};
|
||||||
protected:
|
protected:
|
||||||
private:
|
private:
|
||||||
@ -159,6 +159,12 @@ private:
|
|||||||
SEND_CHK_OK
|
SEND_CHK_OK
|
||||||
};
|
};
|
||||||
|
|
||||||
|
enum StatesCheckRing
|
||||||
|
{
|
||||||
|
RING_WAIT,
|
||||||
|
RING_CHK,
|
||||||
|
};
|
||||||
|
|
||||||
unsigned long tick;
|
unsigned long tick;
|
||||||
|
|
||||||
struct
|
struct
|
||||||
@ -176,7 +182,8 @@ private:
|
|||||||
unsigned long Intervals[THREADEDGSM_INTERVAL_COUNT];
|
unsigned long Intervals[THREADEDGSM_INTERVAL_COUNT];
|
||||||
|
|
||||||
// callbacks
|
// callbacks
|
||||||
conf configuration = {NULL, NULL, NULL, NULL, NULL, NULL, NULL};
|
// conf configuration = {nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr};
|
||||||
|
conf configuration{0};
|
||||||
|
|
||||||
enum ReqTypes
|
enum ReqTypes
|
||||||
{
|
{
|
||||||
@ -190,9 +197,13 @@ private:
|
|||||||
int requests;
|
int requests;
|
||||||
int state;
|
int state;
|
||||||
int job;
|
int job;
|
||||||
|
int ringState;
|
||||||
//functions
|
//functions
|
||||||
public:
|
public:
|
||||||
ThreadedGSM(Stream& stream) : stream(stream), dte(stream, THREADEDGSM_DTE_BUFFER_SIZE)
|
ThreadedGSM(Stream& stream)
|
||||||
|
: stream(stream)
|
||||||
|
, dte(stream, THREADEDGSM_DTE_BUFFER_SIZE)
|
||||||
|
, ringState(RING_WAIT)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < THREADEDGSM_INTERVAL_COUNT; i++)
|
for (int i = 0; i < THREADEDGSM_INTERVAL_COUNT; i++)
|
||||||
Intervals[i] = 0;
|
Intervals[i] = 0;
|
||||||
@ -204,50 +215,47 @@ public:
|
|||||||
|
|
||||||
~ThreadedGSM() {};
|
~ThreadedGSM() {};
|
||||||
|
|
||||||
void nextJob()
|
void nextJob(void) {
|
||||||
{
|
|
||||||
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 begin(void) {
|
||||||
{
|
|
||||||
requests = (REQ_STARTUP);
|
requests = (REQ_STARTUP);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Call this function for executing thread
|
// Call this function for executing thread
|
||||||
void loop()
|
void loop(void) {
|
||||||
{
|
if (dte.getIsBusy())
|
||||||
if(dte.getIsBusy()) return;
|
return;
|
||||||
|
|
||||||
// intervals
|
// intervals
|
||||||
for(int i=0;i<THREADEDGSM_INTERVAL_COUNT;i++)
|
for (int i = 0; i < THREADEDGSM_INTERVAL_COUNT; i++) {
|
||||||
{
|
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;
|
||||||
@ -257,8 +265,7 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(job == 0)
|
if (job == 0) {
|
||||||
{
|
|
||||||
// no assigned job, assign it
|
// no assigned job, assign it
|
||||||
if (requests & REQ_CLOCK)
|
if (requests & REQ_CLOCK)
|
||||||
job = REQ_CLOCK;
|
job = REQ_CLOCK;
|
||||||
@ -273,8 +280,7 @@ public:
|
|||||||
else if (requests & REQ_BATTERY)
|
else if (requests & REQ_BATTERY)
|
||||||
job = REQ_BATTERY;
|
job = REQ_BATTERY;
|
||||||
|
|
||||||
if(job)
|
if (job) {
|
||||||
{
|
|
||||||
state = 0;
|
state = 0;
|
||||||
DEBUG_PRINT(F("Job ID: "));
|
DEBUG_PRINT(F("Job ID: "));
|
||||||
DEBUG_PRINTLN(job);
|
DEBUG_PRINTLN(job);
|
||||||
@ -294,42 +300,41 @@ public:
|
|||||||
Outbox();
|
Outbox();
|
||||||
else if (job == REQ_BATTERY)
|
else if (job == REQ_BATTERY)
|
||||||
Battery();
|
Battery();
|
||||||
|
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, char *Text) {
|
||||||
{
|
String t = Text;
|
||||||
requests |= (REQ_OUTBOX);
|
|
||||||
SMSo.Number = Number;
|
sendSMS(Number, t);
|
||||||
SMSo.Text = Text;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
private:
|
private:
|
||||||
|
|
||||||
// States
|
// States
|
||||||
void Startup()
|
void Startup(void) {
|
||||||
{
|
|
||||||
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 != NULL)
|
||||||
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 != NULL)
|
||||||
this->configuration.power(*this, true);
|
this->configuration.power(*this, true);
|
||||||
@ -337,43 +342,38 @@ private:
|
|||||||
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 {
|
||||||
else
|
|
||||||
{
|
|
||||||
state = STARTUP_POWER_OFF;
|
state = STARTUP_POWER_OFF;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case STARTUP_CHK_CPIN:
|
case STARTUP_CHK_CPIN:
|
||||||
if(dte.getResult() == DTE::EXPECT_RESULT)
|
if (dte.getResult() == DTE::EXPECT_RESULT) {
|
||||||
{
|
if (dte.getBuffer().indexOf(F("+CPIN: READY")) != -1) {
|
||||||
if(dte.getBuffer().indexOf(F("+CPIN: READY")) != -1)
|
|
||||||
{
|
|
||||||
dte.SendCommand(F("AT+CREG?\r"), THREADEDGSM_AT_TIMEOUT, "OK\r");
|
dte.SendCommand(F("AT+CREG?\r"), THREADEDGSM_AT_TIMEOUT, "OK\r");
|
||||||
state = STARTUP_CHK_CREG;
|
state = STARTUP_CHK_CREG;
|
||||||
}else
|
} else {
|
||||||
{
|
|
||||||
state = STARTUP_POWER_OFF;
|
state = STARTUP_POWER_OFF;
|
||||||
}
|
}
|
||||||
} else
|
} else
|
||||||
state = STARTUP_POWER_OFF;
|
state = STARTUP_POWER_OFF;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case STARTUP_CHK_CREG:
|
case STARTUP_CHK_CREG:
|
||||||
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(",1")) >= 0) || (dte.getBuffer().indexOf(F(",5")) >= 0))
|
|
||||||
{
|
|
||||||
dte.SendCommand(F("AT+CLTS=1\r"), THREADEDGSM_AT_TIMEOUT, "OK\r");
|
dte.SendCommand(F("AT+CLTS=1\r"), THREADEDGSM_AT_TIMEOUT, "OK\r");
|
||||||
state = STARTUP_CHK_CLTS;
|
state = STARTUP_CHK_CLTS;
|
||||||
} else
|
} else
|
||||||
@ -381,17 +381,17 @@ private:
|
|||||||
} else
|
} else
|
||||||
state = STARTUP_POWER_OFF;
|
state = STARTUP_POWER_OFF;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case STARTUP_CHK_CLTS:
|
case STARTUP_CHK_CLTS:
|
||||||
if(dte.getResult() == DTE::EXPECT_RESULT)
|
if (dte.getResult() == DTE::EXPECT_RESULT) {
|
||||||
{
|
|
||||||
dte.SendCommand(F("AT+CENG=3\r"), THREADEDGSM_AT_TIMEOUT, "OK\r");
|
dte.SendCommand(F("AT+CENG=3\r"), THREADEDGSM_AT_TIMEOUT, "OK\r");
|
||||||
state = STARTUP_CHK_CENG;
|
state = STARTUP_CHK_CENG;
|
||||||
} else
|
} else
|
||||||
state = STARTUP_POWER_OFF;
|
state = STARTUP_POWER_OFF;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case STARTUP_CHK_CENG:
|
case STARTUP_CHK_CENG:
|
||||||
if(dte.getResult() == DTE::EXPECT_RESULT)
|
if (dte.getResult() == DTE::EXPECT_RESULT) {
|
||||||
{
|
|
||||||
requests |= ((REQ_CLOCK) | (REQ_SIG) | (REQ_BATTERY));
|
requests |= ((REQ_CLOCK) | (REQ_SIG) | (REQ_BATTERY));
|
||||||
clearReq(REQ_STARTUP);
|
clearReq(REQ_STARTUP);
|
||||||
for (int i = 0; i < THREADEDGSM_INTERVAL_COUNT; i++)
|
for (int i = 0; i < THREADEDGSM_INTERVAL_COUNT; i++)
|
||||||
@ -402,43 +402,38 @@ private:
|
|||||||
state = STARTUP_POWER_OFF;
|
state = STARTUP_POWER_OFF;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if(state != lastState)
|
if (state != lastState) {
|
||||||
{
|
|
||||||
DEBUG_PRINT(F("STARTUP_STATE: "));
|
DEBUG_PRINT(F("STARTUP_STATE: "));
|
||||||
DEBUG_PRINTLN(state);
|
DEBUG_PRINTLN(state);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Threads
|
// Threads
|
||||||
void Clock()
|
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)
|
if (endindex >= 0)
|
||||||
clockTime = dte.getBuffer().substring(index, endindex);
|
clockTime = dte.getBuffer().substring(index, endindex);
|
||||||
else
|
else {
|
||||||
{
|
|
||||||
endindex = dte.getBuffer().indexOf(F("-"), index);
|
endindex = dte.getBuffer().indexOf(F("-"), index);
|
||||||
if (endindex >= 0)
|
if (endindex >= 0)
|
||||||
clockTime = dte.getBuffer().substring(index, endindex);
|
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.year = 2000 + clockTime.substring(0, 2).toInt();
|
||||||
ClockTime.month = clockTime.substring(3, 5).toInt();
|
ClockTime.month = clockTime.substring(3, 5).toInt();
|
||||||
@ -453,33 +448,29 @@ private:
|
|||||||
clearReq(REQ_CLOCK);
|
clearReq(REQ_CLOCK);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if(state != lastState)
|
if (state != lastState) {
|
||||||
{
|
|
||||||
DEBUG_PRINT(F("CLOCK_STATE: "));
|
DEBUG_PRINT(F("CLOCK_STATE: "));
|
||||||
DEBUG_PRINTLN(state);
|
DEBUG_PRINTLN(state);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Signal()
|
void Signal(void) {
|
||||||
{
|
|
||||||
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 != NULL)
|
||||||
this->configuration.signal(*this, GsmSignal);
|
this->configuration.signal(*this, GsmSignal);
|
||||||
}
|
}
|
||||||
@ -487,26 +478,23 @@ private:
|
|||||||
clearReq(REQ_SIG);
|
clearReq(REQ_SIG);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if(state != lastState)
|
if (state != lastState) {
|
||||||
{
|
|
||||||
DEBUG_PRINT(F("SIGNAL_STATE: "));
|
DEBUG_PRINT(F("SIGNAL_STATE: "));
|
||||||
DEBUG_PRINTLN(state);
|
DEBUG_PRINTLN(state);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Battery()
|
void Battery(void) {
|
||||||
{
|
|
||||||
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);
|
||||||
@ -521,42 +509,38 @@ private:
|
|||||||
clearReq(REQ_BATTERY);
|
clearReq(REQ_BATTERY);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if(state != lastState)
|
if (state != lastState) {
|
||||||
{
|
|
||||||
DEBUG_PRINT(F("BATTERY_STATE: "));
|
DEBUG_PRINT(F("BATTERY_STATE: "));
|
||||||
DEBUG_PRINTLN(state);
|
DEBUG_PRINTLN(state);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void clearReq(int req)
|
void clearReq(int req) {
|
||||||
{
|
|
||||||
requests &= ~(req);
|
requests &= ~(req);
|
||||||
nextJob();
|
nextJob();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Inbox()
|
void Inbox(void) {
|
||||||
{
|
|
||||||
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 clearReq(REQ_INBOX);
|
else 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";
|
||||||
@ -565,16 +549,14 @@ private:
|
|||||||
}
|
}
|
||||||
else clearReq(REQ_INBOX);
|
else 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;
|
||||||
}
|
}
|
||||||
@ -591,8 +573,7 @@ private:
|
|||||||
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";
|
||||||
@ -603,22 +584,18 @@ private:
|
|||||||
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)
|
||||||
@ -633,39 +610,35 @@ private:
|
|||||||
} 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( (dte.getResult() == DTE::EXPECT_RESULT) && (SMS.InboxMsgContents != ""))
|
||||||
if(dte.getResult() == DTE::EXPECT_RESULT)
|
if (dte.getResult() == DTE::EXPECT_RESULT) {
|
||||||
{
|
|
||||||
if (this->configuration.incoming != NULL)
|
if (this->configuration.incoming != NULL)
|
||||||
this->configuration.incoming(*this, SMSi);
|
this->configuration.incoming(*this, SMSi);
|
||||||
}
|
}
|
||||||
clearReq(REQ_INBOX);
|
clearReq(REQ_INBOX);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if(state != lastState)
|
if (state != lastState) {
|
||||||
{
|
|
||||||
DEBUG_PRINT(F("INBOX_STATE: "));
|
DEBUG_PRINT(F("INBOX_STATE: "));
|
||||||
DEBUG_PRINTLN(state);
|
DEBUG_PRINTLN(state);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Outbox()
|
void Outbox(void) {
|
||||||
{
|
|
||||||
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";
|
||||||
@ -674,30 +647,43 @@ private:
|
|||||||
}
|
}
|
||||||
else clearReq(REQ_OUTBOX);
|
else 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 clearReq(REQ_OUTBOX);
|
||||||
break;
|
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 != NULL)
|
||||||
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: "));
|
||||||
DEBUG_PRINTLN(state);
|
DEBUG_PRINTLN(state);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}; //ThreadedGSM
|
|
||||||
|
|
||||||
#endif //__THREADEDGSM_H__
|
void CheckRing(void) {
|
||||||
|
int lastState = ringState;
|
||||||
|
switch (ringState) {
|
||||||
|
case RING_WAIT:
|
||||||
|
if (dte.getResult() == DTE::EXPECT_RING) {
|
||||||
|
|
||||||
|
ringState = RING_CHK;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (ringState != lastState) {
|
||||||
|
DEBUG_PRINT(F("RING_STATE: "));
|
||||||
|
DEBUG_PRINTLN(ringState);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}; //ThreadedGSM
|
||||||
|
Loading…
Reference in New Issue
Block a user