Aktualizace varovani z clang-tidy, prace na RING (zatim neuspesna...)

This commit is contained in:
Pavel Brychta 2023-09-02 12:02:37 +02:00
parent e2c5e5dcd9
commit e6ba94d9e3
2 changed files with 479 additions and 465 deletions

View File

@ -1,19 +1,17 @@
/*
* DTE.h
*
* Created: 20/09/2016 15:40:51
* Author: Neta Yahav
*/
* DTE.h
*
* Created: 20/09/2016 15:40:51
* Author: Neta Yahav
*/
#pragma once
#include <Arduino.h>
class DTE
{
public:
enum CommandResult
{
class DTE {
public:
enum CommandResult {
EXPECT_BUSY,
EXPECT_TIMEOUT,
EXPECT_DELAY,
@ -21,9 +19,9 @@ public:
EXPECT_RING
};
private:
private:
String buffer;
Stream& stream;
Stream & stream;
unsigned int bufferIndex;
unsigned int bufferSize;
String response[3];
@ -32,17 +30,17 @@ private:
unsigned int match;
CommandResult result;
public:
DTE(Stream& stream, unsigned int size)
: stream(stream)
, bufferSize(size)
, result(EXPECT_RESULT) {
public:
DTE(Stream & stream, unsigned int size)
: stream(stream), bufferSize(size), result(EXPECT_RESULT)
{
buffer.reserve(size);
}
~DTE() {};
~DTE() = default;
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;
result = EXPECT_BUSY;
response[0] = response1;
@ -56,7 +54,8 @@ public:
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)
{
match = 0;
result = EXPECT_BUSY;
@ -67,48 +66,54 @@ public:
// clear rx buffer
flush();
// send command
stream.print((const __FlashStringHelper *)command);
stream.print((const __FlashStringHelper *) command);
buffer = "";
tick = millis();
proccess();
}
void Delay(unsigned long delay) {
void Delay(unsigned long delay)
{
timeout = delay;
result = EXPECT_DELAY;
tick = millis();
proccess();
}
bool getIsBusy(void) {
bool getIsBusy()
{
proccess();
return (result == EXPECT_BUSY) || (result == EXPECT_DELAY);
}
CommandResult getResult(void) {
CommandResult getResult()
{
return result;
}
unsigned int getMatch(void) {
unsigned int getMatch() const
{
return match;
}
String& getBuffer(void) {
String & getBuffer()
{
return buffer;
}
private:
void flush(void) {
private:
void flush()
{
// clear rx buffer
while (stream.available()) {
stream.read();
}
}
void proccess(void) {
void proccess()
{
if (result == EXPECT_DELAY) {
if(millis() - tick >= timeout)
if (millis() - tick >= timeout)
result = EXPECT_RESULT;
return;
@ -162,4 +167,4 @@ private:
// time out
result = EXPECT_TIMEOUT;
}
}; //DTE
}; // DTE

View File

@ -1,14 +1,14 @@
/*
* ThreadedGSM.h
*
* Created: 20/09/2016 11:14:02
* Author: Neta Yahav
*/
* ThreadedGSM.h
*
* Created: 20/09/2016 11:14:02
* Author: Neta Yahav
*/
#pragma once
#include <Arduino.h>
#include "DTE.h"
#include <Arduino.h>
// Defaults
#define THREADEDGSM_DEF_DTE_BUF_SIZ 512
@ -18,34 +18,32 @@
// Use custom values or default ones
#ifndef THREADEDGSM_DTE_BUFFER_SIZE
# define THREADEDGSM_DTE_BUFFER_SIZE THREADEDGSM_DEF_DTE_BUF_SIZ
#define THREADEDGSM_DTE_BUFFER_SIZE THREADEDGSM_DEF_DTE_BUF_SIZ
#endif
#ifndef THREADEDGSM_AT_TIMEOUT
# define THREADEDGSM_AT_TIMEOUT THREADEDGSM_DEF_AT_TIMEOUT
#define THREADEDGSM_AT_TIMEOUT THREADEDGSM_DEF_AT_TIMEOUT
#endif
#ifndef THREADEDGSM_STARTUP_DELAY
# define THREADEDGSM_STARTUP_DELAY THREADEDGSM_DEF_STA_PON
#define THREADEDGSM_STARTUP_DELAY THREADEDGSM_DEF_STA_PON
#endif
#ifndef THREADEDGSM_STARTUP_POWER_OFF_DELAY
# define THREADEDGSM_STARTUP_POWER_OFF_DELAY THREADEDGSM_DEF_STA_POF
#define THREADEDGSM_STARTUP_POWER_OFF_DELAY THREADEDGSM_DEF_STA_POF
#endif
#define THREADEDGSM_INTERVAL_COUNT 4
#ifdef THREADEDGSM_DEBUG
# define DEBUG_PRINT(x) THREADEDGSM_DEBUG.print (x)
# define DEBUG_PRINTLN(x) THREADEDGSM_DEBUG.println (x)
#define DEBUG_PRINT(x) THREADEDGSM_DEBUG.print(x)
#define DEBUG_PRINTLN(x) THREADEDGSM_DEBUG.println(x)
#else
# define DEBUG_PRINT(x)
# define DEBUG_PRINTLN(x)
#define DEBUG_PRINT(x)
#define DEBUG_PRINTLN(x)
#endif
class ThreadedGSM
{
//variables
public:
struct NetworkTime
{
class ThreadedGSM {
// variables
public:
struct NetworkTime {
int year;
int month;
int day;
@ -62,41 +60,36 @@ public:
READ_TYPE_ALL = 4
};
enum IntervalSourceE
{
enum IntervalSourceE {
INTERVAL_CLOCK,
INTERVAL_INBOX,
INTERVAL_SIGNAL,
INTERVAL_BATTERY
};
struct SignalLevel
{
struct SignalLevel {
int Dbm;
int Value;
};
struct BatteryInfo
{
struct BatteryInfo {
int Percent;
int Voltage;
};
struct SMSInfo
{
struct SMSInfo {
String Number;
String Text;
};
typedef void (*ThreadedGSMCallbackSignal)(ThreadedGSM&, SignalLevel&);
typedef void (*ThreadedGSMCallbackClock)(ThreadedGSM&, NetworkTime&);
typedef void (*ThreadedGSMCallbackBattery)(ThreadedGSM&, BatteryInfo&);
typedef void (*ThreadedGSMCallbackIncomingSMS)(ThreadedGSM&, SMSInfo&);
typedef void (*ThreadedGSMCallbackBool)(ThreadedGSM&, bool);
typedef void (*ThreadedGSMCallback)(ThreadedGSM&);
typedef void (*ThreadedGSMCallbackRing)(ThreadedGSM&, String&);
struct conf
{
typedef void (*ThreadedGSMCallbackSignal)(ThreadedGSM &, SignalLevel &);
typedef void (*ThreadedGSMCallbackClock)(ThreadedGSM &, NetworkTime &);
typedef void (*ThreadedGSMCallbackBattery)(ThreadedGSM &, BatteryInfo &);
typedef void (*ThreadedGSMCallbackIncomingSMS)(ThreadedGSM &, SMSInfo &);
typedef void (*ThreadedGSMCallbackBool)(ThreadedGSM &, bool);
typedef void (*ThreadedGSMCallback)(ThreadedGSM &);
typedef void (*ThreadedGSMCallbackRing)(ThreadedGSM &, String &);
struct conf {
ThreadedGSMCallbackSignal signal;
ThreadedGSMCallbackClock clock;
ThreadedGSMCallbackIncomingSMS incoming;
@ -106,10 +99,10 @@ public:
ThreadedGSMCallbackBattery battery;
ThreadedGSMCallbackRing ring;
};
protected:
private:
enum StatesStartup
{
protected:
private:
enum StatesStartup {
STARTUP_POWER_OFF,
STARTUP_POWER_OFF_DELAY,
STARTUP_POWER_ON,
@ -121,26 +114,22 @@ private:
STARTUP_CHK_CENG
};
enum StatesClock
{
enum StatesClock {
CLOCK_REQ,
CLOCK_VERIFY
};
enum StatesSignal
{
enum StatesSignal {
SIGNAL_REQ,
SIGNAL_VERIFY
};
enum StatesBattry
{
enum StatesBattry {
BATTERY_REQ,
BATTERY_VERIFY
};
enum StatesInbox
{
enum StatesInbox {
READ_REQ,
READ_CHK_CMGF,
READ_CHK_CPMS,
@ -151,16 +140,14 @@ private:
READ_CHK_CMGD
};
enum StatesOutbox
{
enum StatesOutbox {
SEND_REQ,
SEND_CHK_CMGF,
SEND_CHK_RDY,
SEND_CHK_OK
};
enum StatesCheckRing
{
enum StatesCheckRing {
RING_WAIT,
RING_CHK,
};
@ -175,18 +162,17 @@ private:
SMSInfo SMSi; // Inbox SMS (incoming)
SMSInfo SMSo; // Outbox SMS (outgoing)
Stream& stream;
Stream & stream;
DTE dte;
unsigned long tickSync[THREADEDGSM_INTERVAL_COUNT];
unsigned long Intervals[THREADEDGSM_INTERVAL_COUNT];
// callbacks
// conf configuration = {nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr};
// conf configuration = {nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr};
conf configuration{0};
enum ReqTypes
{
enum ReqTypes {
REQ_CLOCK = 1,
REQ_SIG = 2,
REQ_INBOX = 4,
@ -198,44 +184,47 @@ private:
int state;
int job;
int ringState;
//functions
public:
ThreadedGSM(Stream& stream)
: stream(stream)
, dte(stream, THREADEDGSM_DTE_BUFFER_SIZE)
, ringState(RING_WAIT)
// functions
public:
ThreadedGSM(Stream & stream)
: stream(stream), dte(stream, THREADEDGSM_DTE_BUFFER_SIZE), ringState(RING_WAIT)
{
for (int i = 0; i < THREADEDGSM_INTERVAL_COUNT; i++)
Intervals[i] = 0;
for (unsigned long & Interval : Intervals)
Interval = 0;
job = state = requests = 0;
//SMSo.Text.reserve(150);
//SMSi.Text.reserve(150);
// SMSo.Text.reserve(150);
// SMSi.Text.reserve(150);
}
~ThreadedGSM() {};
~ThreadedGSM() = default;
void nextJob(void) {
void nextJob()
{
job = 0;
}
void setHandlers(conf config) {
void setHandlers(conf config)
{
this->configuration = config;
}
void setInterval(IntervalSourceE source, unsigned long interval) {
void setInterval(IntervalSourceE source, unsigned long interval)
{
Intervals[source] = interval;
tickSync[source] = millis();
}
// Initialization
void begin(void) {
void begin()
{
requests = (REQ_STARTUP);
}
// Call this function for executing thread
void loop(void) {
void loop()
{
if (dte.getIsBusy())
return;
@ -300,31 +289,34 @@ public:
Outbox();
else if (job == REQ_BATTERY)
Battery();
else CheckRing();
else
CheckRing();
}
// Requests
void sendSMS(String& Number, String& Text) {
void sendSMS(String & Number, String & Text)
{
requests |= (REQ_OUTBOX);
SMSo.Number = Number;
SMSo.Text = Text;
}
void sendSMS(String& Number, char *Text) {
void sendSMS(String & Number, const char * Text)
{
String t = Text;
sendSMS(Number, t);
}
protected:
private:
protected:
private:
// States
void Startup(void) {
void Startup()
{
int lastState = state;
switch (state) {
case STARTUP_POWER_OFF:
if (this->configuration.power != NULL)
if (this->configuration.power != nullptr)
this->configuration.power(*this, false);
tick = millis();
state = STARTUP_POWER_OFF_DELAY;
@ -336,7 +328,7 @@ private:
break;
case STARTUP_POWER_ON:
if (this->configuration.power != NULL)
if (this->configuration.power != nullptr)
this->configuration.power(*this, true);
// begin delay
tick = millis();
@ -396,7 +388,7 @@ private:
clearReq(REQ_STARTUP);
for (int i = 0; i < THREADEDGSM_INTERVAL_COUNT; i++)
tickSync[i] = millis();
if (this->configuration.ready != NULL)
if (this->configuration.ready != nullptr)
this->configuration.ready(*this);
} else
state = STARTUP_POWER_OFF;
@ -409,7 +401,8 @@ private:
}
// Threads
void Clock(void) {
void Clock(void)
{
String clockTime;
int lastState = state;
switch (state) {
@ -434,14 +427,15 @@ private:
}
if (endindex >= 0) {
NetworkTime ClockTime;
NetworkTime ClockTime{};
ClockTime.year = 2000 + clockTime.substring(0, 2).toInt();
ClockTime.month = clockTime.substring(3, 5).toInt();
ClockTime.day = clockTime.substring(6, 8).toInt();
ClockTime.hour = clockTime.substring(9, 11).toInt();
ClockTime.minute = clockTime.substring(12, 14).toInt();
ClockTime.second = clockTime.substring(15, 17).toInt();
if (this->configuration.clock != NULL)
if (this->configuration.clock != nullptr)
this->configuration.clock(*this, ClockTime);
}
}
@ -454,7 +448,8 @@ private:
}
}
void Signal(void) {
void Signal()
{
int lastState = state;
switch (state) {
case SIGNAL_REQ:
@ -467,11 +462,11 @@ private:
if (index >= 0) {
// parse signal
index += 6;
SignalLevel GsmSignal;
SignalLevel GsmSignal{};
GsmSignal.Value = dte.getBuffer().substring(index, index + 2).toInt();
GsmSignal.Dbm = dte.getBuffer().substring(index + 3, index + 5).toInt();
if (GsmSignal.Value != 0) {
if (this->configuration.signal != NULL)
if (this->configuration.signal != nullptr)
this->configuration.signal(*this, GsmSignal);
}
}
@ -484,7 +479,8 @@ private:
}
}
void Battery(void) {
void Battery()
{
int lastState = state;
switch (state) {
case BATTERY_REQ:
@ -495,7 +491,7 @@ private:
case BATTERY_VERIFY:
int index = dte.getBuffer().indexOf(F("+CBC:"));
if (index >= 0) {
BatteryInfo BattInfo;
BatteryInfo BattInfo{};
// parse battery level
String buffer = dte.getBuffer().substring(index);
String buffer2 = buffer.substring(buffer.indexOf(F(",")) + 1);
@ -503,7 +499,7 @@ private:
BattInfo.Percent = buffer2.substring(0, buffer2.indexOf(F(","))).toInt(); // converts the result to interger
buffer2 = buffer.substring(buffer.indexOf(F(",")) + 1);
BattInfo.Voltage = buffer2.substring(0, buffer2.indexOf(F("\r"))).toInt(); // converts the result to interger
if (this->configuration.battery != NULL)
if (this->configuration.battery != nullptr)
this->configuration.battery(*this, BattInfo);
}
clearReq(REQ_BATTERY);
@ -515,12 +511,14 @@ private:
}
}
void clearReq(int req) {
void clearReq(int req)
{
requests &= ~(req);
nextJob();
}
void Inbox(void) {
void Inbox()
{
String CMD;
int lastState = state;
switch (state) {
@ -535,8 +533,8 @@ private:
if (dte.getResult() == DTE::EXPECT_RESULT) {
dte.SendCommand(F("AT+CPMS=\"SM\"\r"), THREADEDGSM_AT_TIMEOUT, "OK\r"); // SIM Message storage
state = READ_CHK_CPMS;
}
else clearReq(REQ_INBOX);
} else
clearReq(REQ_INBOX);
break;
case READ_CHK_CPMS:
@ -546,13 +544,13 @@ private:
CMD += "\r";
dte.SendCommand(CMD.c_str(), THREADEDGSM_AT_TIMEOUT, ",");
state = READ_CHK_CMGL;
}
else clearReq(REQ_INBOX);
} else
clearReq(REQ_INBOX);
break;
case READ_CHK_CMGL:
if (dte.getResult() == DTE::EXPECT_RESULT) {
//fetch index
// fetch index
int indexStart = dte.getBuffer().indexOf(F("+CMGL: "));
if (indexStart >= 0) {
Message.Index = dte.getBuffer().substring(indexStart + 7, dte.getBuffer().indexOf(F(","))).toInt();
@ -612,9 +610,9 @@ private:
break;
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 (this->configuration.incoming != NULL)
if (this->configuration.incoming != nullptr)
this->configuration.incoming(*this, SMSi);
}
clearReq(REQ_INBOX);
@ -626,10 +624,11 @@ private:
}
}
void Outbox(void) {
void Outbox()
{
String CMD;
//CMD.reserve(200);
// CMD.reserve(200);
int lastState = state;
switch (state) {
case SEND_REQ:
@ -644,22 +643,23 @@ private:
CMD += "\"\r";
dte.SendCommand(CMD.c_str(), 15000, "> ");
state = SEND_CHK_RDY;
}
else clearReq(REQ_OUTBOX);
} else
clearReq(REQ_OUTBOX);
break;
case SEND_CHK_RDY:
if (dte.getResult() == DTE::EXPECT_RESULT) {
CMD = SMSo.Text;
CMD += (char)26;
CMD += (char) 26;
dte.SendCommand(CMD.c_str(), 10000, "OK\r");
state = SEND_CHK_OK;
} else clearReq(REQ_OUTBOX);
} else
clearReq(REQ_OUTBOX);
break;
case SEND_CHK_OK:
if (dte.getResult() == DTE::EXPECT_RESULT) {
if (this->configuration.outgoing != NULL)
if (this->configuration.outgoing != nullptr)
this->configuration.outgoing(*this);
}
clearReq(REQ_OUTBOX);
@ -671,7 +671,8 @@ private:
}
}
void CheckRing(void) {
void CheckRing()
{
int lastState = ringState;
switch (ringState) {
case RING_WAIT:
@ -680,10 +681,18 @@ private:
ringState = RING_CHK;
}
break;
case RING_CHK:
if (this->configuration.ring != nullptr) {
String s = "Ring";
this->configuration.ring(*this, s);
}
ringState = RING_WAIT;
break;
}
if (ringState != lastState) {
DEBUG_PRINT(F("RING_STATE: "));
DEBUG_PRINTLN(ringState);
}
}
}; //ThreadedGSM
}; // ThreadedGSM