Skip to content

Commit 145cd2b

Browse files
committed
[BT] Application level Watchdog Timer to avoid scan_evt timeout
Implement a BLE scan watchdog to bypass the problem related to the BLE scan hang scan_evt timeout This is a bypass solution to espressif/arduino-esp32#5860 The watchdog will restart the ESP if no new BLE messages has been added to the queue following: checked every 120s if we are after the last BLE message time + the BLE scan interval for passive if the process is not locked by an OTA update or other operation We restart the ESP We also remove the WDT0 enable and disable functions.
1 parent 95896e0 commit 145cd2b

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

main/ZgatewayBT.ino

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,23 @@ void BTConfig_init() {
101101
BTConfig.presenceAwayTimer = PresenceAwayTimer;
102102
}
103103

104+
// Watchdog, if there was no change of btQueueLengthSum for 5 minutes, restart ESP
105+
void btScanWDG() {
106+
static unsigned long previousbtQueueLengthSum = 0;
107+
static unsigned long lastBtMsgTime = 0;
108+
unsigned long now = millis();
109+
if (!ProcessLock &&
110+
previousbtQueueLengthSum == btQueueLengthSum &&
111+
btQueueLengthSum != 0 &&
112+
(now - lastBtMsgTime > BTConfig.BLEinterval)) {
113+
Log.error(F("BLE Scan watchdog triggered at : %ds" CR), lastBtMsgTime / 1000);
114+
ESPRestart();
115+
} else {
116+
previousbtQueueLengthSum = btQueueLengthSum;
117+
lastBtMsgTime = now;
118+
}
119+
}
120+
104121
unsigned long timeBetweenConnect = 0;
105122
unsigned long timeBetweenActive = 0;
106123

@@ -707,7 +724,6 @@ void BLEscan() {
707724
while (uxQueueMessagesWaiting(BLEQueue)) {
708725
yield();
709726
}
710-
disableCore0WDT();
711727
Log.notice(F("Scan begin" CR));
712728
BLEScan* pBLEScan = BLEDevice::getScan();
713729
MyAdvertisedDeviceCallbacks myCallbacks;
@@ -723,7 +739,6 @@ void BLEscan() {
723739
BLEScanResults foundDevices = pBLEScan->start(BTConfig.scanDuration / 1000, false);
724740
scanCount++;
725741
Log.notice(F("Found %d devices, scan number %d end" CR), foundDevices.getCount(), scanCount);
726-
enableCore0WDT();
727742
Log.trace(F("Process BLE stack free: %u" CR), uxTaskGetStackHighWaterMark(xProcBLETaskHandle));
728743
}
729744

main/main.ino

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1617,6 +1617,7 @@ void loop() {
16171617
stateMeasures();
16181618
# ifdef ZgatewayBT
16191619
stateBTMeasures(false);
1620+
btScanWDG();
16201621
# endif
16211622
# ifdef ZactuatorONOFF
16221623
stateONOFFMeasures();

0 commit comments

Comments
 (0)