Uptime was not threadsafe
This commit is contained in:
parent
4a89e338de
commit
553d386352
39
src/main.cpp
39
src/main.cpp
|
@ -105,38 +105,9 @@ BleFingerprint *getFingerprint(BLEAdvertisedDevice *advertisedDevice)
|
|||
return created;
|
||||
}
|
||||
|
||||
/**
|
||||
* CalculateUptimeSeconds()
|
||||
*
|
||||
* Handle millis() rollover and calculate the total uptime in seconds.
|
||||
* This function must be called at least once for every 50 days to be
|
||||
* able to see the rollover.
|
||||
*/
|
||||
unsigned long CalculateUptimeSeconds(void)
|
||||
unsigned long getUptimeSeconds(void)
|
||||
{
|
||||
static unsigned int _rolloverCount = 0; // Number of 0xFFFFFFFF rollover we had in millis()
|
||||
static unsigned long _lastMillis = 0; // Value of the last millis()
|
||||
|
||||
// Get the current milliseconds uptime from the system.
|
||||
// Note: This only works as long as no one else did hook up with timer0
|
||||
// because the arduino system uses timer0 to manage delay() and millis().
|
||||
unsigned long currentMilliSeconds = millis();
|
||||
|
||||
// If we had a rollover we count that.
|
||||
if (currentMilliSeconds < _lastMillis)
|
||||
{
|
||||
_rolloverCount++;
|
||||
}
|
||||
|
||||
// Now store the current number of milliseconds for the next round.
|
||||
_lastMillis = currentMilliSeconds;
|
||||
|
||||
// Based on the current milliseconds and the number of rollovers
|
||||
// we had in total we calculate here the uptime in seconds since
|
||||
// poweron or reset.
|
||||
// Caution: Because we shorten millis to seconds we may miss one
|
||||
// second for every rollover (1 second every 50 days).
|
||||
return (0xFFFFFFFF / 1000) * _rolloverCount + (_lastMillis / 1000);
|
||||
return esp_timer_get_time() / 1e6;
|
||||
}
|
||||
|
||||
bool sendTelemetry(int totalSeen = -1, int totalReported = -1, int totalAdverts = -1)
|
||||
|
@ -147,7 +118,7 @@ bool sendTelemetry(int totalSeen = -1, int totalReported = -1, int totalAdverts
|
|||
tele["hostname"] = WiFi.getHostname();
|
||||
tele["scan_dur"] = BLE_SCAN_DURATION;
|
||||
tele["max_dist"] = MAX_DISTANCE;
|
||||
tele["uptime"] = CalculateUptimeSeconds();
|
||||
tele["uptime"] = getUptimeSeconds();
|
||||
tele["firm"] = String(FIRMWARE);
|
||||
|
||||
#ifdef VERSION
|
||||
|
@ -199,7 +170,7 @@ void connectToWifi()
|
|||
return 500; // Delay next function call by 500ms
|
||||
};
|
||||
WiFiSettings.onPortalWaitLoop = []() {
|
||||
if (CalculateUptimeSeconds() > 600)
|
||||
if (getUptimeSeconds() > 600)
|
||||
ESP.restart();
|
||||
};
|
||||
|
||||
|
@ -447,7 +418,7 @@ void firmwareUpdate()
|
|||
{
|
||||
#ifdef VERSION
|
||||
static long lastFirmwareCheck = 0;
|
||||
long uptime = CalculateUptimeSeconds();
|
||||
long uptime = getUptimeSeconds();
|
||||
if (uptime - lastFirmwareCheck < CHECK_FOR_UPDATES_INTERVAL)
|
||||
return;
|
||||
|
||||
|
|
Loading…
Reference in New Issue