Skip to content

Commit

Permalink
make BateryStats::updateAvailable wrap-around-safe
Browse files Browse the repository at this point in the history
  • Loading branch information
schlimmchen committed Mar 5, 2024
1 parent fe7e622 commit e432f0e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion include/BatteryStats.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class BatteryStats {

// the last time *any* datum was updated
uint32_t getAgeSeconds() const { return (millis() - _lastUpdate) / 1000; }
bool updateAvailable(uint32_t since) const { return _lastUpdate > since; }
bool updateAvailable(uint32_t since) const;

uint8_t getSoC() const { return _soc; }
uint32_t getSoCAgeSeconds() const { return (millis() - _lastUpdateSoC) / 1000; }
Expand Down
6 changes: 6 additions & 0 deletions src/BatteryStats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ static void addLiveViewAlarm(JsonVariant& root, std::string const& name,
root["issues"][name] = 2;
}

bool BatteryStats::updateAvailable(uint32_t since) const
{
auto constexpr halfOfAllMillis = std::numeric_limits<uint32_t>::max() / 2;
return (_lastUpdate - since) < halfOfAllMillis;
}

void BatteryStats::getLiveViewData(JsonVariant& root) const
{
root[F("manufacturer")] = _manufacturer;
Expand Down

0 comments on commit e432f0e

Please sign in to comment.