Skip to content

Commit

Permalink
system: rework stability counter
Browse files Browse the repository at this point in the history
- remove public function from the header
- initial log for both states, log counted number
- fixup terminal reset triggering unstable state
  • Loading branch information
mcspr committed Mar 31, 2021
1 parent 5cabdee commit 474f0e9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 40 deletions.
71 changes: 34 additions & 37 deletions code/espurna/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,43 +164,45 @@ void _systemRtcmemResetReason(CustomResetReason reason) {
//
// An unstable system will only have serial access, WiFi in AP mode and OTA

bool _systemStable = true;

void systemCheck(bool stable) {
uint8_t value = 0;

if (stable) {
value = 0;
DEBUG_MSG_P(PSTR("[MAIN] System OK\n"));
} else {
if (!rtcmemStatus()) {
systemStabilityCounter(1);
return;
}

value = systemStabilityCounter();
constexpr unsigned char _systemCheckMin() {
return 0u;
}

if (++value > SYSTEM_CHECK_MAX) {
_systemStable = false;
value = SYSTEM_CHECK_MAX;
DEBUG_MSG_P(PSTR("[MAIN] System UNSTABLE\n"));
}
}
constexpr unsigned char _systemCheckMax() {
return SYSTEM_CHECK_MAX;
}

systemStabilityCounter(value);
constexpr unsigned long _systemCheckTime() {
return SYSTEM_CHECK_TIME;
}

bool systemCheck() {
return _systemStable;
static_assert(_systemCheckMax() > 0, "");

Ticker _system_stable_timer;
bool _system_stable { true };

void _systemStabilityInit() {
auto count = rtcmemStatus() ? systemStabilityCounter() : 1u;

_system_stable = (count < _systemCheckMax());
DEBUG_MSG_P(PSTR("[MAIN] System %s\n"), _system_stable ? "OK" : "UNSTABLE");

_system_stable_timer.once_ms_scheduled(_systemCheckTime(), []() {
DEBUG_MSG_P(PSTR("[MAIN] System stability counter %hhu / %hhu\n"),
_systemCheckMin(), _systemCheckMax());
systemStabilityCounter(_systemCheckMin());
});

auto next = count + 1u;
count = next > _systemCheckMax()
? count
: next;

systemStabilityCounter(count);
}

void _systemCheckLoop() {
static bool checked = false;
if (!checked && (millis() > SYSTEM_CHECK_TIME)) {
// Flag system as stable
systemCheck(true);
checked = true;
}
bool systemCheck() {
return _system_stable;
}

#endif
Expand Down Expand Up @@ -536,10 +538,6 @@ void systemLoop() {
return;
}

#if SYSTEM_CHECK_ENABLED
_systemCheckLoop();
#endif

_systemUpdateLoadAverage();
}

Expand Down Expand Up @@ -573,9 +571,8 @@ void systemSetup() {
SPIFFS.begin();
#endif

// Question system stability
#if SYSTEM_CHECK_ENABLED
systemCheck(false);
_systemStabilityInit();
#endif

#if WEB_SUPPORT
Expand Down
1 change: 0 additions & 1 deletion code/espurna/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ uint32_t systemResetReason();
uint8_t systemStabilityCounter();
void systemStabilityCounter(uint8_t count);

void systemCheck(bool stable);
bool systemCheck();

void customResetReason(CustomResetReason reason);
Expand Down
5 changes: 3 additions & 2 deletions code/espurna/terminal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,10 +348,11 @@ void _terminalInitCommands() {
});

terminalRegisterCommand(F("RESET"), [](const terminal::CommandContext& ctx) {
auto count = 1;
if (ctx.argc == 2) {
auto arg = ctx.argv[1].toInt();
count = ctx.argv[1].toInt();
if (arg < SYSTEM_CHECK_MAX) {
systemStabilityCounter(arg);
systemStabilityCounter(count);
}
}

Expand Down

0 comments on commit 474f0e9

Please sign in to comment.