From 79615516ef8868ebd3c59dffcd39b3755f572159 Mon Sep 17 00:00:00 2001 From: Florian Date: Fri, 28 Apr 2023 15:14:01 -0500 Subject: [PATCH] [BT] BLE Scan watchdog relying on scan count rather than msgs processed (#1622) * [BT] BLE Scan watchdog relying on scan count rather than msgs processed * Condition the scan increment following the scan result --- main/ZgatewayBT.ino | 23 ++++++++++++----------- main/main.ino | 1 + 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/main/ZgatewayBT.ino b/main/ZgatewayBT.ino index 697a932..14f1674 100644 --- a/main/ZgatewayBT.ino +++ b/main/ZgatewayBT.ino @@ -101,20 +101,20 @@ void BTConfig_init() { BTConfig.presenceAwayTimer = PresenceAwayTimer; } -// Watchdog, if there was no change of btQueueLengthSum for 5 minutes, restart ESP +// Watchdog, if there was no change of BLE scanCount, restart ESP void btScanWDG() { - static unsigned long previousbtQueueLengthSum = 0; - static unsigned long lastBtMsgTime = 0; + static unsigned long previousBtScanCount = 0; + static unsigned long lastBtScan = 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(); + previousBtScanCount == scanCount && + scanCount != 0 && + (now - lastBtScan > BTConfig.BLEinterval)) { + Log.error(F("BLE Scan watchdog triggered at : %ds" CR), lastBtScan / 1000); + watchdogReboot(4); } else { - previousbtQueueLengthSum = btQueueLengthSum; - lastBtMsgTime = now; + previousBtScanCount = scanCount; + lastBtScan = now; } } @@ -744,7 +744,8 @@ void BLEscan() { pBLEScan->setInterval(BLEScanInterval); pBLEScan->setWindow(BLEScanWindow); BLEScanResults foundDevices = pBLEScan->start(BTConfig.scanDuration / 1000, false); - scanCount++; + if (foundDevices.getCount()) + scanCount++; Log.notice(F("Found %d devices, scan number %d end" CR), foundDevices.getCount(), scanCount); Log.trace(F("Process BLE stack free: %u" CR), uxTaskGetStackHighWaterMark(xProcBLETaskHandle)); } diff --git a/main/main.ino b/main/main.ino index 5186a33..c21d7aa 100644 --- a/main/main.ino +++ b/main/main.ino @@ -2597,6 +2597,7 @@ String toString(uint32_t input) { 1 - Repeated MQTT Connection Failure 2 - Repeated WiFi Connection Failure 3 - Failed WiFiManager configuration portal + 4 - BLE Scan watchdog */ void watchdogReboot(byte reason) { Log.warning(F("Rebooting for reason code %d" CR), reason);