From 77ddef76af2687339ba4766b2564e65aee5f151b Mon Sep 17 00:00:00 2001 From: iranl Date: Mon, 15 Jul 2024 17:16:43 +0200 Subject: [PATCH] Replace millis() with esp_timer_get_time() (#423) --- lib/nuki_ble | 2 +- platformio.ini | 15 +++++--- src/Gpio.cpp | 46 ++++++++++++------------- src/Gpio.h | 2 +- src/NukiNetwork.cpp | 41 ++++++++++++++++------ src/NukiNetwork.h | 22 ++++++------ src/NukiNetworkLock.cpp | 5 +++ src/NukiNetworkLock.h | 5 +-- src/NukiNetworkOpener.cpp | 4 +-- src/NukiNetworkOpener.h | 2 +- src/NukiOpenerWrapper.cpp | 22 ++++++------ src/NukiOpenerWrapper.h | 26 +++++++------- src/NukiWrapper.cpp | 40 ++++++++++----------- src/NukiWrapper.h | 26 +++++++------- src/PresenceDetection.cpp | 12 +++---- src/PresenceDetection.h | 4 +-- src/WebCfgServer.cpp | 12 +++---- src/WebCfgServer.h | 2 +- src/main.cpp | 14 ++------ src/networkDevices/EthLan8720Device.cpp | 2 +- src/networkDevices/WifiDevice.cpp | 10 +++--- src/networkDevices/WifiDevice.h | 2 +- 22 files changed, 171 insertions(+), 145 deletions(-) diff --git a/lib/nuki_ble b/lib/nuki_ble index 7079805..9116261 160000 --- a/lib/nuki_ble +++ b/lib/nuki_ble @@ -1 +1 @@ -Subproject commit 7079805486b77ea1584922470c1d53ae24013db9 +Subproject commit 9116261c791d64d77238e093780d64cbeac32256 diff --git a/platformio.ini b/platformio.ini index 4664539..e2a0a68 100644 --- a/platformio.ini +++ b/platformio.ini @@ -66,6 +66,7 @@ build_flags = -DNUKI_USE_LATEST_NIMBLE -DNUKI_NO_WDT_RESET -DNUKI_MUTEX_RECURSIVE + -DNUKI_64BIT_TIME -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_NONE -DCONFIG_NIMBLE_CPP_LOG_LEVEL=0 -DCONFIG_BT_NIMBLE_LOG_LEVEL=0 @@ -93,6 +94,7 @@ build_flags = -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_NONE -DCONFIG_NIMBLE_CPP_LOG_LEVEL=0 -DCONFIG_BT_NIMBLE_LOG_LEVEL=0 + -DNUKI_64BIT_TIME lib_deps = BleScanner=symlink://lib/BleScanner lib_ignore = @@ -100,7 +102,7 @@ lib_ignore = NetworkClientSecure ESPAsyncTCP-esphome AsyncTCP_RP2040W - + [env:esp32dev_dbg] extends = env:esp32dev custom_build = debug @@ -113,6 +115,7 @@ build_flags = -DNUKI_USE_LATEST_NIMBLE -DNUKI_NO_WDT_RESET -DNUKI_MUTEX_RECURSIVE + -DNUKI_64BIT_TIME -DDEBUG_NUKIHUB -DDEBUG_SENSE_NUKI -DDEBUG_NUKI_COMMAND @@ -132,7 +135,8 @@ build_flags = -DBLESCANNER_USE_LATEST_NIMBLE -DNUKI_USE_LATEST_NIMBLE -DNUKI_NO_WDT_RESET - -DNUKI_MUTEX_RECURSIVE + -DNUKI_MUTEX_RECURSIVE + -DNUKI_64BIT_TIME -DDEBUG_NUKIHUB -DDEBUG_SENSE_NUKI -DDEBUG_NUKI_COMMAND @@ -152,7 +156,8 @@ build_flags = -DBLESCANNER_USE_LATEST_NIMBLE -DNUKI_USE_LATEST_NIMBLE -DNUKI_NO_WDT_RESET - -DNUKI_MUTEX_RECURSIVE + -DNUKI_MUTEX_RECURSIVE + -DNUKI_64BIT_TIME -DDEBUG_NUKIHUB -DDEBUG_SENSE_NUKI -DDEBUG_NUKI_COMMAND @@ -172,7 +177,8 @@ build_flags = -DBLESCANNER_USE_LATEST_NIMBLE -DNUKI_USE_LATEST_NIMBLE -DNUKI_NO_WDT_RESET - -DNUKI_MUTEX_RECURSIVE + -DNUKI_MUTEX_RECURSIVE + -DNUKI_64BIT_TIME -DDEBUG_NUKIHUB -DDEBUG_SENSE_NUKI -DDEBUG_NUKI_COMMAND @@ -189,6 +195,7 @@ build_flags = -DFRAMEWORK_ARDUINO_SOLO1 -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_DEBUG -DCONFIG_NIMBLE_CPP_LOG_LEVEL=0 + -DNUKI_64BIT_TIME -DDEBUG_NUKIHUB -DDEBUG_SENSE_NUKI -DDEBUG_NUKI_COMMAND diff --git a/src/Gpio.cpp b/src/Gpio.cpp index 22066c0..4520617 100644 --- a/src/Gpio.cpp +++ b/src/Gpio.cpp @@ -8,7 +8,7 @@ #include "Gpio2Go.h" Gpio* Gpio::_inst = nullptr; -unsigned long Gpio::_debounceTs = 0; +int64_t Gpio::_debounceTs = 0; const uint Gpio::_debounceTime = GPIO_DEBOUNCE_TIME; Gpio::Gpio(Preferences* preferences) @@ -277,79 +277,79 @@ void Gpio::addCallback(std::function callba void Gpio::isrLock() { - if(millis() < _debounceTs) return; + if((esp_timer_get_time() / 1000) < _debounceTs) return; _inst->notify(GpioAction::Lock, -1); - _debounceTs = millis() + _debounceTime; + _debounceTs = (esp_timer_get_time() / 1000) + _debounceTime; } void Gpio::isrUnlock() { - if(millis() < _debounceTs) return; + if((esp_timer_get_time() / 1000) < _debounceTs) return; _inst->notify(GpioAction::Unlock, -1); - _debounceTs = millis() + _debounceTime; + _debounceTs = (esp_timer_get_time() / 1000) + _debounceTime; } void Gpio::isrUnlatch() { - if(millis() < _debounceTs) return; + if((esp_timer_get_time() / 1000) < _debounceTs) return; _inst->notify(GpioAction::Unlatch, -1); - _debounceTs = millis() + _debounceTime; + _debounceTs = (esp_timer_get_time() / 1000) + _debounceTime; } void Gpio::isrLockNgo() { - if(millis() < _debounceTs) return; + if((esp_timer_get_time() / 1000) < _debounceTs) return; _inst->notify(GpioAction::LockNgo, -1); - _debounceTs = millis() + _debounceTime; + _debounceTs = (esp_timer_get_time() / 1000) + _debounceTime; } void Gpio::isrLockNgoUnlatch() { - if(millis() < _debounceTs) return; + if((esp_timer_get_time() / 1000) < _debounceTs) return; _inst->notify(GpioAction::LockNgoUnlatch, -1); - _debounceTs = millis() + _debounceTime; + _debounceTs = (esp_timer_get_time() / 1000) + _debounceTime; } void Gpio::isrElectricStrikeActuation() { - if(millis() < _debounceTs) return; + if((esp_timer_get_time() / 1000) < _debounceTs) return; _inst->notify(GpioAction::ElectricStrikeActuation, -1); - _debounceTs = millis() + _debounceTime; + _debounceTs = (esp_timer_get_time() / 1000) + _debounceTime; } void Gpio::isrActivateRTO() { - if(millis() < _debounceTs) return; + if((esp_timer_get_time() / 1000) < _debounceTs) return; _inst->notify(GpioAction::ActivateRTO, -1); - _debounceTs = millis() + _debounceTime; + _debounceTs = (esp_timer_get_time() / 1000) + _debounceTime; } void Gpio::isrActivateCM() { - if(millis() < _debounceTs) return; + if((esp_timer_get_time() / 1000) < _debounceTs) return; _inst->notify(GpioAction::ActivateCM, -1); - _debounceTs = millis() + _debounceTime; + _debounceTs = (esp_timer_get_time() / 1000) + _debounceTime; } void Gpio::isrDeactivateRtoCm() { - if(millis() < _debounceTs) return; + if((esp_timer_get_time() / 1000) < _debounceTs) return; _inst->notify(GpioAction::DeactivateRtoCm, -1); - _debounceTs = millis() + _debounceTime; + _debounceTs = (esp_timer_get_time() / 1000) + _debounceTime; } void Gpio::isrDeactivateRTO() { - if(millis() < _debounceTs) return; + if((esp_timer_get_time() / 1000) < _debounceTs) return; _inst->notify(GpioAction::DeactivateRTO, -1); - _debounceTs = millis() + _debounceTime; + _debounceTs = (esp_timer_get_time() / 1000) + _debounceTime; } void Gpio::isrDeactivateCM() { - if(millis() < _debounceTs) return; + if((esp_timer_get_time() / 1000) < _debounceTs) return; _inst->notify(GpioAction::DeactivateCM, -1); - _debounceTs = millis() + _debounceTime; + _debounceTs = (esp_timer_get_time() / 1000) + _debounceTime; } void Gpio::setPinOutput(const uint8_t& pin, const uint8_t& state) diff --git a/src/Gpio.h b/src/Gpio.h index a7823da..d04a39c 100644 --- a/src/Gpio.h +++ b/src/Gpio.h @@ -122,7 +122,7 @@ private: std::vector> _callbacks; static Gpio* _inst; - static unsigned long _debounceTs; + static int64_t _debounceTs; Preferences* _preferences = nullptr; }; diff --git a/src/NukiNetwork.cpp b/src/NukiNetwork.cpp index 41ed4b8..dec4d85 100644 --- a/src/NukiNetwork.cpp +++ b/src/NukiNetwork.cpp @@ -349,7 +349,7 @@ void NukiNetwork::initialize() bool NukiNetwork::update() { - unsigned long ts = millis(); + int64_t ts = (esp_timer_get_time() / 1000); _device->update(); @@ -365,7 +365,7 @@ bool NukiNetwork::update() _device->mqttDisconnect(true); } - if(_restartOnDisconnect && millis() > 60000) + if(_restartOnDisconnect && (esp_timer_get_time() / 1000) > 60000) { restartEsp(RestartReason::RestartOnDisconnectWatchdog); } @@ -506,8 +506,8 @@ bool NukiNetwork::update() for(const auto& gpioTs : _gpioTs) { uint8_t pin = gpioTs.first; - unsigned long ts = gpioTs.second; - if(ts != 0 && ((millis() - ts) >= GPIO_DEBOUNCE_TIME)) + int64_t ts = gpioTs.second; + if(ts != 0 && (((esp_timer_get_time() / 1000) - ts) >= GPIO_DEBOUNCE_TIME)) { _gpioTs[pin] = 0; @@ -574,12 +574,12 @@ bool NukiNetwork::reconnect() _mqttConnectionState = 0; int port = _preferences->getInt(preference_mqtt_broker_port); - while (!_device->mqttConnected() && millis() > _nextReconnect) + while (!_device->mqttConnected() && (esp_timer_get_time() / 1000) > _nextReconnect) { if(strcmp(_mqttBrokerAddr, "") == 0) { Log->println(F("MQTT Broker not configured, aborting connection attempt.")); - _nextReconnect = millis() + 5000; + _nextReconnect = (esp_timer_get_time() / 1000) + 5000; return false; } @@ -601,9 +601,9 @@ bool NukiNetwork::reconnect() _device->mqttSetServer(_mqttBrokerAddr, port); _device->mqttConnect(); - unsigned long timeout = millis() + 60000; + int64_t timeout = (esp_timer_get_time() / 1000) + 60000; - while(!_connectReplyReceived && millis() < timeout) + while(!_connectReplyReceived && (esp_timer_get_time() / 1000) < timeout) { delay(50); _device->update(); @@ -649,7 +649,7 @@ bool NukiNetwork::reconnect() Log->print(F("MQTT connect failed, rc=")); _device->printError(); _mqttConnectionState = 0; - _nextReconnect = millis() + 5000; + _nextReconnect = (esp_timer_get_time() / 1000) + 5000; //_device->mqttDisconnect(true); } } @@ -762,7 +762,7 @@ void NukiNetwork::parseGpioTopics(const espMqttClientTypes::MessageProperties &p void NukiNetwork::gpioActionCallback(const GpioAction &action, const int &pin) { - _gpioTs[pin] = millis(); + _gpioTs[pin] = (esp_timer_get_time() / 1000); } #if PRESENCE_DETECTION_ENABLED @@ -837,6 +837,27 @@ void NukiNetwork::publishULong(const char* prefix, const char *topic, const unsi _device->mqttPublish(path, MQTT_QOS_LEVEL, retain, str); } +void NukiNetwork::publishLongLong(const char* prefix, const char *topic, int64_t value, bool retain) +{ + static char result[21] = ""; + memset(&result[0], 0, sizeof(result)); + char temp[21] = ""; + char c; + uint8_t base = 10; + + while (value) + { + int num = value % base; + value /= base; + c = '0' + num; + sprintf(temp, "%c%s", c, result); + strcpy(result, temp); + } + char path[200] = {0}; + buildMqttPath(path, { prefix, topic }); + _device->mqttPublish(path, MQTT_QOS_LEVEL, retain, result); +} + void NukiNetwork::publishBool(const char* prefix, const char *topic, const bool value, bool retain) { char str[2] = {0}; diff --git a/src/NukiNetwork.h b/src/NukiNetwork.h index 99a0ba2..d285a9f 100644 --- a/src/NukiNetwork.h +++ b/src/NukiNetwork.h @@ -62,6 +62,7 @@ public: void publishInt(const char* prefix, const char* topic, const int value, bool retain); void publishUInt(const char* prefix, const char* topic, const unsigned int value, bool retain); void publishULong(const char* prefix, const char* topic, const unsigned long value, bool retain); + void publishLongLong(const char* prefix, const char* topic, int64_t value, bool retain); void publishBool(const char* prefix, const char* topic, const bool value, bool retain); bool publishString(const char* prefix, const char* topic, const char* value, bool retain); @@ -162,7 +163,7 @@ private: bool _connectReplyReceived = false; bool _firstDisconnected = true; - unsigned long _nextReconnect = 0; + int64_t _nextReconnect = 0; char _mqttBrokerAddr[101] = {0}; char _mqttUser[31] = {0}; char _mqttPass[31] = {0}; @@ -176,17 +177,16 @@ private: bool _logIp = true; std::vector _subscribedTopics; std::map _initTopics; - - unsigned long _lastConnectedTs = 0; - unsigned long _lastMaintenanceTs = 0; - unsigned long _lastUpdateCheckTs = 0; -#if PRESENCE_DETECTION_ENABLED - unsigned long _lastPresenceTs = 0; -#endif - unsigned long _lastRssiTs = 0; + int64_t _lastConnectedTs = 0; + int64_t _lastMaintenanceTs = 0; + int64_t _lastUpdateCheckTs = 0; + #if PRESENCE_DETECTION_ENABLED + int64_t _lastPresenceTs = 0; + #endif + int64_t _lastRssiTs = 0; bool _mqttEnabled = true; - long _rssiPublishInterval = 0; - std::map _gpioTs; + int _rssiPublishInterval = 0; + std::map _gpioTs; char* _buffer; const size_t _bufferSize; diff --git a/src/NukiNetworkLock.cpp b/src/NukiNetworkLock.cpp index 2f8fc1d..a32f1a2 100644 --- a/src/NukiNetworkLock.cpp +++ b/src/NukiNetworkLock.cpp @@ -1428,6 +1428,11 @@ void NukiNetworkLock::publishULong(const char *topic, const unsigned long value, return _network->publishULong(_mqttPath, topic, value, retain); } +void NukiNetworkLock::publishLongLong(const char *topic, int64_t value, bool retain) +{ + return _network->publishLongLong(_mqttPath, topic, value, retain); +} + String NukiNetworkLock::concat(String a, String b) { String c = a; diff --git a/src/NukiNetworkLock.h b/src/NukiNetworkLock.h index caea560..0a88058 100644 --- a/src/NukiNetworkLock.h +++ b/src/NukiNetworkLock.h @@ -57,6 +57,7 @@ public: void publishInt(const char* topic, const int value, bool retain); void publishUInt(const char* topic, const unsigned int value, bool retain); void publishULong(const char* topic, const unsigned long value, bool retain); + void publishLongLong(const char* topic, int64_t value, bool retain); void publishBool(const char* topic, const bool value, bool retain); bool publishString(const char* topic, const String& value, bool retain); bool publishString(const char* topic, const std::string& value, bool retain); @@ -81,7 +82,7 @@ public: uint32_t _offCodeId = 0; uint8_t _offContext = 0; uint32_t _authId = 0; - unsigned long _offCommandExecutedTs = 0; + int64_t _offCommandExecutedTs = 0; NukiLock::LockAction _offCommand = (NukiLock::LockAction)0xff; @@ -105,7 +106,7 @@ private: char _offMqttPath[181] = {0}; bool _firstTunerStatePublish = true; - unsigned long _lastMaintenanceTs = 0; + int64_t _lastMaintenanceTs = 0; bool _haEnabled = false; bool _reconnected = false; diff --git a/src/NukiNetworkOpener.cpp b/src/NukiNetworkOpener.cpp index fafc239..2a0c047 100644 --- a/src/NukiNetworkOpener.cpp +++ b/src/NukiNetworkOpener.cpp @@ -122,7 +122,7 @@ void NukiNetworkOpener::initialize() void NukiNetworkOpener::update() { - if(_resetRingStateTs != 0 && millis() >= _resetRingStateTs) + if(_resetRingStateTs != 0 && (esp_timer_get_time() / 1000) >= _resetRingStateTs) { _resetRingStateTs = 0; publishString(mqtt_topic_lock_binary_ring, "standby", true); @@ -396,7 +396,7 @@ void NukiNetworkOpener::publishRing(const bool locked) } publishString(mqtt_topic_lock_binary_ring, "ring", true); - _resetRingStateTs = millis() + 2000; + _resetRingStateTs = (esp_timer_get_time() / 1000) + 2000; } void NukiNetworkOpener::publishState(NukiOpener::OpenerState lockState) diff --git a/src/NukiNetworkOpener.h b/src/NukiNetworkOpener.h index 98b79cc..ccedc6c 100644 --- a/src/NukiNetworkOpener.h +++ b/src/NukiNetworkOpener.h @@ -89,7 +89,7 @@ private: String _keypadCommandCode = ""; uint _keypadCommandId = 0; int _keypadCommandEnabled = 1; - unsigned long _resetRingStateTs = 0; + int64_t _resetRingStateTs = 0; uint8_t _queryCommands = 0; uint32_t _authId = 0; char _authName[33]; diff --git a/src/NukiOpenerWrapper.cpp b/src/NukiOpenerWrapper.cpp index 9a66993..a02b558 100644 --- a/src/NukiOpenerWrapper.cpp +++ b/src/NukiOpenerWrapper.cpp @@ -142,8 +142,8 @@ void NukiOpenerWrapper::update() } } - long ts = millis(); - long lastReceivedBeaconTs = _nukiOpener.getLastReceivedBeaconTs(); + int64_t lastReceivedBeaconTs = _nukiOpener.getLastReceivedBeaconTs(); + int64_t ts = (esp_timer_get_time() / 1000); uint8_t queryCommands = _network->queryCommands(); if(_restartBeaconTimeout > 0 && @@ -256,7 +256,7 @@ void NukiOpenerWrapper::update() _network->publishRetry(std::to_string(_retryCount + 1)); - _nextRetryTs = millis() + _retryDelay; + _nextRetryTs = (esp_timer_get_time() / 1000) + _retryDelay; ++_retryCount; } @@ -367,7 +367,7 @@ void NukiOpenerWrapper::updateKeyTurnerState() postponeBleWatchdog(); if(_retryLockstateCount < _nrOfRetries + 1) { - _nextLockStateUpdateTs = millis() + _retryDelay; + _nextLockStateUpdateTs = (esp_timer_get_time() / 1000) + _retryDelay; } return; } @@ -523,7 +523,7 @@ void NukiOpenerWrapper::updateConfig() } if(!expectedConfig && _retryConfigCount < 11) { - unsigned long ts = millis(); + int64_t ts = (esp_timer_get_time() / 1000); _nextConfigUpdateTs = ts + 60000; } } @@ -557,7 +557,7 @@ void NukiOpenerWrapper::updateAuthData(bool retrieved) printCommandResult(result); if(result == Nuki::CmdResult::Success) { - _waitAuthLogUpdateTs = millis() + 5000; + _waitAuthLogUpdateTs = (esp_timer_get_time() / 1000) + 5000; delay(100); std::list log; @@ -623,7 +623,7 @@ void NukiOpenerWrapper::updateKeypad(bool retrieved) printCommandResult(result); if(result == Nuki::CmdResult::Success) { - _waitKeypadUpdateTs = millis() + 5000; + _waitKeypadUpdateTs = (esp_timer_get_time() / 1000) + 5000; } } else @@ -684,7 +684,7 @@ void NukiOpenerWrapper::updateTimeControl(bool retrieved) printCommandResult(result); if(result == Nuki::CmdResult::Success) { - _waitTimeControlUpdateTs = millis() + 5000; + _waitTimeControlUpdateTs = (esp_timer_get_time() / 1000) + 5000; } } else @@ -724,7 +724,7 @@ void NukiOpenerWrapper::updateTimeControl(bool retrieved) void NukiOpenerWrapper::postponeBleWatchdog() { - _disableBleWatchdogTs = millis() + 15000; + _disableBleWatchdogTs = (esp_timer_get_time() / 1000) + 15000; } NukiOpener::LockAction NukiOpenerWrapper::lockActionToEnum(const char *str) @@ -1411,7 +1411,7 @@ void NukiOpenerWrapper::onConfigUpdateReceived(const char *value) if(basicUpdated || advancedUpdated) jsonResult["general"] = "success"; else jsonResult["general"] = "noChange"; - _nextConfigUpdateTs = millis() + 300; + _nextConfigUpdateTs = (esp_timer_get_time() / 1000) + 300; serializeJson(jsonResult, _resbuf, sizeof(_resbuf)); _network->publishConfigCommandResult(_resbuf); @@ -2266,7 +2266,7 @@ void NukiOpenerWrapper::onTimeControlCommandReceived(const char *value) _network->publishTimeControlCommandResult(resultStr); } - _nextConfigUpdateTs = millis() + 300; + _nextConfigUpdateTs = (esp_timer_get_time() / 1000) + 300; } else { diff --git a/src/NukiOpenerWrapper.h b/src/NukiOpenerWrapper.h index 0c74a0d..fa5f227 100644 --- a/src/NukiOpenerWrapper.h +++ b/src/NukiOpenerWrapper.h @@ -105,7 +105,7 @@ private: int _retryCount = 0; int _retryConfigCount = 0; int _retryLockstateCount = 0; - unsigned long _nextRetryTs = 0; + int64_t _nextRetryTs = 0; std::vector _keypadCodeIds; std::vector _timeControlIds; @@ -129,18 +129,18 @@ private: uint _maxKeypadCodeCount = 0; uint _maxTimeControlEntryCount = 0; bool _configRead = false; - long _rssiPublishInterval = 0; - unsigned long _nextLockStateUpdateTs = 0; - unsigned long _nextBatteryReportTs = 0; - unsigned long _nextConfigUpdateTs = 0; - unsigned long _waitAuthLogUpdateTs = 0; - unsigned long _waitKeypadUpdateTs = 0; - unsigned long _waitTimeControlUpdateTs = 0; - unsigned long _nextKeypadUpdateTs = 0; - unsigned long _nextPairTs = 0; - long _nextRssiTs = 0; - unsigned long _lastRssi = 0; - unsigned long _disableBleWatchdogTs = 0; + int _rssiPublishInterval = 0; + int64_t _nextLockStateUpdateTs = 0; + int64_t _nextBatteryReportTs = 0; + int64_t _nextConfigUpdateTs = 0; + int64_t _waitAuthLogUpdateTs = 0; + int64_t _waitKeypadUpdateTs = 0; + int64_t _waitTimeControlUpdateTs = 0; + int64_t _nextKeypadUpdateTs = 0; + int64_t _nextPairTs = 0; + int64_t _nextRssiTs = 0; + int64_t _lastRssi = 0; + int64_t _disableBleWatchdogTs = 0; std::string _firmwareVersion = ""; std::string _hardwareVersion = ""; NukiOpener::LockAction _nextLockAction = (NukiOpener::LockAction)0xff; diff --git a/src/NukiWrapper.cpp b/src/NukiWrapper.cpp index 167f35b..9771418 100644 --- a/src/NukiWrapper.cpp +++ b/src/NukiWrapper.cpp @@ -213,8 +213,8 @@ void NukiWrapper::update() } } - long ts = millis(); - long lastReceivedBeaconTs = _nukiLock.getLastReceivedBeaconTs(); + int64_t lastReceivedBeaconTs = _nukiLock.getLastReceivedBeaconTs(); + int64_t ts = (esp_timer_get_time() / 1000); uint8_t queryCommands = _network->queryCommands(); if(_restartBeaconTimeout > 0 && @@ -335,7 +335,7 @@ void NukiWrapper::update() _network->publishRetry(std::to_string(_retryCount + 1)); - _nextRetryTs = millis() + _retryDelay; + _nextRetryTs = (esp_timer_get_time() / 1000) + _retryDelay; ++_retryCount; } @@ -449,7 +449,7 @@ void NukiWrapper::updateKeyTurnerState() Log->print(F("Query lock state retrying in ")); Log->print(_retryDelay); Log->println("ms"); - _nextLockStateUpdateTs = millis() + _retryDelay; + _nextLockStateUpdateTs = (esp_timer_get_time() / 1000) + _retryDelay; } return; } @@ -600,7 +600,7 @@ void NukiWrapper::updateConfig() } if(!expectedConfig && _retryConfigCount < 11) { - unsigned long ts = millis(); + int64_t ts = (esp_timer_get_time() / 1000); _nextConfigUpdateTs = ts + 60000; } } @@ -633,7 +633,7 @@ void NukiWrapper::updateAuthData(bool retrieved) printCommandResult(result); if(result == Nuki::CmdResult::Success) { - _waitAuthLogUpdateTs = millis() + 5000; + _waitAuthLogUpdateTs = (esp_timer_get_time() / 1000) + 5000; delay(100); std::list log; @@ -699,7 +699,7 @@ void NukiWrapper::updateKeypad(bool retrieved) printCommandResult(result); if(result == Nuki::CmdResult::Success) { - _waitKeypadUpdateTs = millis() + 5000; + _waitKeypadUpdateTs = (esp_timer_get_time() / 1000) + 5000; } } else @@ -760,7 +760,7 @@ void NukiWrapper::updateTimeControl(bool retrieved) printCommandResult(result); if(result == Nuki::CmdResult::Success) { - _waitTimeControlUpdateTs = millis() + 5000; + _waitTimeControlUpdateTs = (esp_timer_get_time() / 1000) + 5000; } } else @@ -800,7 +800,7 @@ void NukiWrapper::updateTimeControl(bool retrieved) void NukiWrapper::postponeBleWatchdog() { - _disableBleWatchdogTs = millis() + 15000; + _disableBleWatchdogTs = (esp_timer_get_time() / 1000) + 15000; } NukiLock::LockAction NukiWrapper::lockActionToEnum(const char *str) @@ -842,7 +842,7 @@ LockActionResult NukiWrapper::onLockActionReceivedCallback(const char *value) if(!networkInst->_offConnected) nukiInst->_nextLockAction = action; else { - networkInst->_offCommandExecutedTs = millis() + 2000; + networkInst->_offCommandExecutedTs = (esp_timer_get_time() / 1000) + 2000; networkInst->_offCommand = action; networkInst->publishOffAction((int)action); } @@ -973,7 +973,7 @@ void NukiWrapper::onOfficialUpdateReceived(const char *topic, const char *value) Log->println((strcmp(value, "true") == 0 ? 1 : 0)); _network->_offConnected = (strcmp(value, "true") == 0 ? 1 : 0); - if(!_network->_offConnected) _nextHybridLockStateUpdateTs = millis() + _intervalHybridLockstate * 1000; + if(!_network->_offConnected) _nextHybridLockStateUpdateTs = (esp_timer_get_time() / 1000) + _intervalHybridLockstate * 1000; else _nextHybridLockStateUpdateTs = 0; } else if(strcmp(topic, mqtt_topic_official_state) == 0) @@ -1653,7 +1653,7 @@ void NukiWrapper::onConfigUpdateReceived(const char *value) if(basicUpdated || advancedUpdated) jsonResult["general"] = "success"; else jsonResult["general"] = "noChange"; - _nextConfigUpdateTs = millis() + 300; + _nextConfigUpdateTs = (esp_timer_get_time() / 1000) + 300; serializeJson(jsonResult, _resbuf, sizeof(_resbuf)); _network->publishConfigCommandResult(_resbuf); @@ -1687,7 +1687,7 @@ void NukiWrapper::gpioActionCallback(const GpioAction &action, const int& pin) if(!networkInst->_offConnected) nukiInst->lock(); else { - networkInst->_offCommandExecutedTs = millis() + 2000; + networkInst->_offCommandExecutedTs = (esp_timer_get_time() / 1000) + 2000; networkInst->_offCommand = NukiLock::LockAction::Lock; networkInst->publishOffAction(2); } @@ -1696,7 +1696,7 @@ void NukiWrapper::gpioActionCallback(const GpioAction &action, const int& pin) if(!networkInst->_offConnected) nukiInst->unlock(); else { - networkInst->_offCommandExecutedTs = millis() + 2000; + networkInst->_offCommandExecutedTs = (esp_timer_get_time() / 1000) + 2000; networkInst->_offCommand = NukiLock::LockAction::Unlock; networkInst->publishOffAction(1); } @@ -1705,7 +1705,7 @@ void NukiWrapper::gpioActionCallback(const GpioAction &action, const int& pin) if(!networkInst->_offConnected) nukiInst->unlatch(); else { - networkInst->_offCommandExecutedTs = millis() + 2000; + networkInst->_offCommandExecutedTs = (esp_timer_get_time() / 1000) + 2000; networkInst->_offCommand = NukiLock::LockAction::Unlatch; networkInst->publishOffAction(3); } @@ -1714,7 +1714,7 @@ void NukiWrapper::gpioActionCallback(const GpioAction &action, const int& pin) if(!networkInst->_offConnected) nukiInst->lockngo(); else { - networkInst->_offCommandExecutedTs = millis() + 2000; + networkInst->_offCommandExecutedTs = (esp_timer_get_time() / 1000) + 2000; networkInst->_offCommand = NukiLock::LockAction::LockNgo; networkInst->publishOffAction(4); } @@ -1723,7 +1723,7 @@ void NukiWrapper::gpioActionCallback(const GpioAction &action, const int& pin) if(!networkInst->_offConnected) nukiInst->lockngounlatch(); else { - networkInst->_offCommandExecutedTs = millis() + 2000; + networkInst->_offCommandExecutedTs = (esp_timer_get_time() / 1000) + 2000; networkInst->_offCommand = NukiLock::LockAction::LockNgoUnlatch; networkInst->publishOffAction(5); } @@ -2544,7 +2544,7 @@ void NukiWrapper::onTimeControlCommandReceived(const char *value) _network->publishTimeControlCommandResult(resultStr); } - _nextConfigUpdateTs = millis() + 300; + _nextConfigUpdateTs = (esp_timer_get_time() / 1000) + 300; } else { @@ -2572,11 +2572,11 @@ void NukiWrapper::notify(Nuki::EventType eventType) { if(!_network->_offConnected) { - if(_preferences->getBool(preference_official_hybrid, false) && _intervalHybridLockstate > 0 && millis() > (_intervalHybridLockstate * 1000)) + if(_preferences->getBool(preference_official_hybrid, false) && _intervalHybridLockstate > 0 && (esp_timer_get_time() / 1000) > (_intervalHybridLockstate * 1000)) { Log->println("OffKeyTurnerStatusUpdated"); _statusUpdated = true; - _nextHybridLockStateUpdateTs = millis() + _intervalHybridLockstate * 1000; + _nextHybridLockStateUpdateTs = (esp_timer_get_time() / 1000) + _intervalHybridLockstate * 1000; } else { diff --git a/src/NukiWrapper.h b/src/NukiWrapper.h index ea0cb7e..16bc463 100644 --- a/src/NukiWrapper.h +++ b/src/NukiWrapper.h @@ -125,19 +125,19 @@ private: int _retryCount = 0; int _retryConfigCount = 0; int _retryLockstateCount = 0; - long _rssiPublishInterval = 0; - unsigned long _nextRetryTs = 0; - unsigned long _nextLockStateUpdateTs = 0; - unsigned long _nextHybridLockStateUpdateTs = 0; - unsigned long _nextBatteryReportTs = 0; - unsigned long _nextConfigUpdateTs = 0; - unsigned long _waitAuthLogUpdateTs = 0; - unsigned long _waitKeypadUpdateTs = 0; - unsigned long _waitTimeControlUpdateTs = 0; - unsigned long _nextKeypadUpdateTs = 0; - unsigned long _nextRssiTs = 0; - unsigned long _lastRssi = 0; - unsigned long _disableBleWatchdogTs = 0; + int _rssiPublishInterval = 0; + int64_t _nextRetryTs = 0; + int64_t _nextLockStateUpdateTs = 0; + int64_t _nextHybridLockStateUpdateTs = 0; + int64_t _nextBatteryReportTs = 0; + int64_t _nextConfigUpdateTs = 0; + int64_t _waitAuthLogUpdateTs = 0; + int64_t _waitKeypadUpdateTs = 0; + int64_t _waitTimeControlUpdateTs = 0; + int64_t _nextKeypadUpdateTs = 0; + int64_t _nextRssiTs = 0; + int64_t _lastRssi = 0; + int64_t _disableBleWatchdogTs = 0; std::string _firmwareVersion = ""; std::string _hardwareVersion = ""; volatile NukiLock::LockAction _nextLockAction = (NukiLock::LockAction)0xff; diff --git a/src/PresenceDetection.cpp b/src/PresenceDetection.cpp index b766fee..980049a 100644 --- a/src/PresenceDetection.cpp +++ b/src/PresenceDetection.cpp @@ -44,7 +44,7 @@ char* PresenceDetection::generateCsv() memset(_csv, 0, _bufferSize); _csvIndex = 0; - long ts = millis(); + int64_t ts = esp_timer_get_time() / 1000; { std::lock_guard lock(mtx); @@ -133,7 +133,7 @@ void PresenceDetection::onResult(NimBLEAdvertisedDevice *device) addrArrComp[10] = addressStr.at(15); addrArrComp[11] = addressStr.at(16); - long long addr = strtoll(addrArrComp, nullptr, 16); + int64_t addr = strtoll(addrArrComp, nullptr, 16); bool found; { @@ -143,7 +143,7 @@ void PresenceDetection::onResult(NimBLEAdvertisedDevice *device) if(found) { - it->second->timestamp = millis(); + it->second->timestamp = esp_timer_get_time() / 1000; if(device->haveRSSI()) { it->second->hasRssi = true; @@ -183,7 +183,7 @@ void PresenceDetection::onResult(NimBLEAdvertisedDevice *device) ++i; } - pdDevice->timestamp = millis(); + pdDevice->timestamp = esp_timer_get_time() / 1000; { std::lock_guard lock(mtx); @@ -204,7 +204,7 @@ void PresenceDetection::onResult(NimBLEAdvertisedDevice *device) // if(ENDIAN_CHANGE_U16(oBeacon.getMinor()) == 40004) // { - pdDevice->timestamp = millis(); + pdDevice->timestamp = esp_timer_get_time() / 1000; strcpy(pdDevice->name, oBeacon.getProximityUUID().toString().c_str()); { std::lock_guard lock(mtx); @@ -224,7 +224,7 @@ void PresenceDetection::onResult(NimBLEAdvertisedDevice *device) ++i; } - pdDevice->timestamp = millis(); + pdDevice->timestamp = esp_timer_get_time() / 1000; { std::lock_guard lock(mtx); diff --git a/src/PresenceDetection.h b/src/PresenceDetection.h index 5e5889b..2ee70bb 100644 --- a/src/PresenceDetection.h +++ b/src/PresenceDetection.h @@ -10,7 +10,7 @@ struct PdDevice { char address[18] = {0}; char name[37] = {0}; - unsigned long timestamp = 0; + int64_t timestamp = 0; int rssi = 0; bool hasRssi = false; }; @@ -36,7 +36,7 @@ private: BleScanner::Scanner* _bleScanner; char* _csv = {0}; size_t _bufferSize = 0; - std::map> _devices; + std::map> _devices; int _timeout = 20000; int _csvIndex = 0; }; diff --git a/src/WebCfgServer.cpp b/src/WebCfgServer.cpp index c082998..bdfaeae 100644 --- a/src/WebCfgServer.cpp +++ b/src/WebCfgServer.cpp @@ -377,7 +377,7 @@ void WebCfgServer::initialize() void WebCfgServer::update() { - if(_otaStartTs > 0 && (millis() - _otaStartTs) > 120000) + if(_otaStartTs > 0 && ((esp_timer_get_time() / 1000) - _otaStartTs) > 120000) { Log->println(F("OTA time out, restarting")); delay(200); @@ -479,13 +479,13 @@ void WebCfgServer::buildHtmlHeader(String &response, String additionalHeader) response.concat(""); response.concat("Nuki Hub"); - srand(millis()); + srand(esp_timer_get_time() / 1000); } void WebCfgServer::waitAndProcess(const bool blocking, const uint32_t duration) { - unsigned long timeout = millis() + duration; - while(millis() < timeout) + int64_t timeout = esp_timer_get_time() + (duration * 1000); + while(esp_timer_get_time() < timeout) { _server.handleClient(); if(blocking) @@ -532,7 +532,7 @@ void WebCfgServer::handleOtaUpload() { filename = "/" + filename; } - _otaStartTs = millis(); + _otaStartTs = esp_timer_get_time() / 1000; #if (ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0)) esp_task_wdt_init(30, false); #else @@ -2792,7 +2792,7 @@ void WebCfgServer::buildInfoHtml(String &response) } response.concat("Uptime: "); - response.concat(millis() / 1000 / 60); + response.concat(esp_timer_get_time() / 1000 / 1000 / 60); response.concat(" minutes\n"); response.concat("Heap: "); diff --git a/src/WebCfgServer.h b/src/WebCfgServer.h index eddf90d..6510170 100644 --- a/src/WebCfgServer.h +++ b/src/WebCfgServer.h @@ -107,7 +107,7 @@ private: bool _allowRestartToPortal = false; uint8_t _partitionType = 0; uint32_t _transferredSize = 0; - unsigned long _otaStartTs = 0; + int64_t _otaStartTs = 0; String _hostname; bool _enabled = true; }; diff --git a/src/main.cpp b/src/main.cpp index 9de2b72..2faf316 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -38,7 +38,7 @@ bool openerEnabled = false; TaskHandle_t nukiTaskHandle = nullptr; TaskHandle_t presenceDetectionTaskHandle = nullptr; -unsigned long restartTs = (2^32) - 5 * 60000; +int64_t restartTs = ((2^64) - (5 * 1000 * 60000)) / 1000; #else #include "../../src/WebCfgServer.h" @@ -48,7 +48,7 @@ unsigned long restartTs = (2^32) - 5 * 60000; #include "../../src/RestartReason.h" #include "../../src/NukiNetwork.h" -unsigned long restartTs = 10 * 60000; +int64_t restartTs = 10 * 1000 * 60000; #endif @@ -74,7 +74,7 @@ void networkTask(void *pvParameters) { while(true) { - unsigned long ts = millis(); + int64_t ts = (esp_timer_get_time() / 1000); if(ts > 120000 && ts < 125000 && bootloopCounter > 0) { bootloopCounter = (int8_t)0; @@ -97,14 +97,6 @@ void networkTask(void *pvParameters) webCfgServer->update(); #endif - // millis() is about to overflow. Restart device to prevent problems with overflow - if(millis() > restartTs) - { - Log->println(F("Restart timer expired, restarting device.")); - delay(200); - restartEsp(RestartReason::RestartTimer); - } - delay(100); } } diff --git a/src/networkDevices/EthLan8720Device.cpp b/src/networkDevices/EthLan8720Device.cpp index b937d01..ab4763c 100644 --- a/src/networkDevices/EthLan8720Device.cpp +++ b/src/networkDevices/EthLan8720Device.cpp @@ -155,7 +155,7 @@ ReconnectStatus EthLan8720Device::reconnect() void EthLan8720Device::onDisconnected() { - if(_restartOnDisconnect && (millis() > 60000)) restartEsp(RestartReason::RestartOnDisconnectWatchdog); + if(_restartOnDisconnect && ((esp_timer_get_time() / 1000) > 60000)) restartEsp(RestartReason::RestartOnDisconnectWatchdog); reconnect(); } diff --git a/src/networkDevices/WifiDevice.cpp b/src/networkDevices/WifiDevice.cpp index 39b50f1..800d1da 100644 --- a/src/networkDevices/WifiDevice.cpp +++ b/src/networkDevices/WifiDevice.cpp @@ -153,8 +153,8 @@ ReconnectStatus WifiDevice::reconnect() { if(WiFi.getMode() & WIFI_STA){ WiFi.mode(WIFI_OFF); - int timeout = millis()+1200; - while(WiFi.getMode()!= WIFI_OFF && millis() millis() - 120000) _wm.setEnableConfigPortal(_startAp || !_preferences->getBool(preference_network_wifi_fallback_disabled)); + if(!isConnected() && _disconnectTs > (esp_timer_get_time() / 1000) - 120000) _wm.setEnableConfigPortal(_startAp || !_preferences->getBool(preference_network_wifi_fallback_disabled)); return isConnected() ? ReconnectStatus::Success : ReconnectStatus::Failure; } @@ -179,8 +179,8 @@ void WifiDevice::onConnected() void WifiDevice::onDisconnected() { - _disconnectTs = millis(); - if(_restartOnDisconnect && (millis() > 60000)) restartEsp(RestartReason::RestartOnDisconnectWatchdog); + _disconnectTs = (esp_timer_get_time() / 1000); + if(_restartOnDisconnect && ((esp_timer_get_time() / 1000) > 60000)) restartEsp(RestartReason::RestartOnDisconnectWatchdog); _wm.setEnableConfigPortal(false); reconnect(); } diff --git a/src/networkDevices/WifiDevice.h b/src/networkDevices/WifiDevice.h index 986f923..080b53d 100644 --- a/src/networkDevices/WifiDevice.h +++ b/src/networkDevices/WifiDevice.h @@ -46,7 +46,7 @@ private: bool _startAp = false; bool _isReconnecting = false; char* _path; - unsigned long _disconnectTs = 0; + int64_t _disconnectTs = 0; #ifndef NUKI_HUB_UPDATER char _ca[TLS_CA_MAX_SIZE] = {0};