From 6e8b4632131ff9e6b1771aceb577a7cdcfdaa960 Mon Sep 17 00:00:00 2001 From: Florian Date: Thu, 6 Apr 2023 15:08:40 -0500 Subject: [PATCH] BT Watchdog Timer (#1590) Implement a BLE scan watchdog to bypass the problem related to the BLE scan hang scan_evt timout --- main/ZgatewayBT.ino | 19 +++++++++++++++++-- main/main.ino | 1 + 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/main/ZgatewayBT.ino b/main/ZgatewayBT.ino index 7526798..71386b0 100644 --- a/main/ZgatewayBT.ino +++ b/main/ZgatewayBT.ino @@ -101,6 +101,23 @@ void BTConfig_init() { BTConfig.presenceAwayTimer = PresenceAwayTimer; } +// Watchdog, if there was no change of btQueueLengthSum for 5 minutes, restart ESP +void btScanWDG() { + static unsigned long previousbtQueueLengthSum = 0; + static unsigned long lastBtMsgTime = 0; + unsigned long now = millis(); + if (!ProcessLock && + previousbtQueueLengthSum == btQueueLengthSum && + btQueueLengthSum != 0 && + (now - lastBtMsgTime > BTConfig.BLEinterval)) { + Log.error(F("BLE Scan watchdog triggered at : %ds" CR), lastBtMsgTime / 1000); + ESPRestart(); + } else { + previousbtQueueLengthSum = btQueueLengthSum; + lastBtMsgTime = now; + } +} + unsigned long timeBetweenConnect = 0; unsigned long timeBetweenActive = 0; @@ -714,7 +731,6 @@ void BLEscan() { while (uxQueueMessagesWaiting(BLEQueue)) { yield(); } - disableCore0WDT(); Log.notice(F("Scan begin" CR)); BLEScan* pBLEScan = BLEDevice::getScan(); MyAdvertisedDeviceCallbacks myCallbacks; @@ -730,7 +746,6 @@ void BLEscan() { BLEScanResults foundDevices = pBLEScan->start(BTConfig.scanDuration / 1000, false); scanCount++; Log.notice(F("Found %d devices, scan number %d end" CR), foundDevices.getCount(), scanCount); - enableCore0WDT(); Log.trace(F("Process BLE stack free: %u" CR), uxTaskGetStackHighWaterMark(xProcBLETaskHandle)); } diff --git a/main/main.ino b/main/main.ino index 6fe7e3b..68ba879 100644 --- a/main/main.ino +++ b/main/main.ino @@ -1614,6 +1614,7 @@ void loop() { stateMeasures(); # ifdef ZgatewayBT stateBTMeasures(false); + btScanWDG(); # endif # ifdef ZactuatorONOFF stateONOFFMeasures();