Skip to content

Commit

Permalink
BT Watchdog Timer (#1590)
Browse files Browse the repository at this point in the history
Implement a BLE scan watchdog to bypass the problem related to the BLE scan hang scan_evt timout
  • Loading branch information
1technophile authored Apr 6, 2023
1 parent 8ee3da4 commit 6e8b463
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
19 changes: 17 additions & 2 deletions main/ZgatewayBT.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -714,7 +731,6 @@ void BLEscan() {
while (uxQueueMessagesWaiting(BLEQueue)) {
yield();
}
disableCore0WDT();
Log.notice(F("Scan begin" CR));
BLEScan* pBLEScan = BLEDevice::getScan();
MyAdvertisedDeviceCallbacks myCallbacks;
Expand All @@ -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));
}

Expand Down
1 change: 1 addition & 0 deletions main/main.ino
Original file line number Diff line number Diff line change
Expand Up @@ -1614,6 +1614,7 @@ void loop() {
stateMeasures();
# ifdef ZgatewayBT
stateBTMeasures(false);
btScanWDG();
# endif
# ifdef ZactuatorONOFF
stateONOFFMeasures();
Expand Down

0 comments on commit 6e8b463

Please sign in to comment.