From 77e9e97dd15ffe0f7615651b03469585313094e1 Mon Sep 17 00:00:00 2001 From: Denny Sheirer Date: Sat, 21 Nov 2026 02:22:39 -0500 Subject: [PATCH] #2623 DMR decoder resets the detected sync mode after inactivity (#2736) On sparse channels, noise false syncs accumulated across long idle periods and permanently locked the sync mode monitor to the wrong mode (ie DIRECT_ONLY), so the repeater's base station syncs were never detected again until the channel was restarted. - Only sync detections that follow a previous sync within the max sync interval are counted toward the sync mode. - Restore AUTOMATIC sync mode after 5 seconds without a sync detection, unless the mode is locked (ie BASE_ONLY for traffic channels). Co-authored-by: denny --- .../dsp/c4fm/C4FMSymbolProcessor.java | 25 ++++++ .../module/decode/dmr/DMRSymbolProcessor.java | 31 ++++++- .../decode/dmr/sync/DMRSyncModeMonitor.java | 61 +++++++++++-- .../dmr/sync/DMRSyncModeMonitorTest.java | 87 +++++++++++++++++++ 4 files changed, 195 insertions(+), 9 deletions(-) create mode 100644 src/test/java/io/github/dsheirer/module/decode/dmr/sync/DMRSyncModeMonitorTest.java diff --git a/src/main/java/io/github/dsheirer/dsp/c4fm/C4FMSymbolProcessor.java b/src/main/java/io/github/dsheirer/dsp/c4fm/C4FMSymbolProcessor.java index b23cfcaf..45b6cb3c 100644 --- a/src/main/java/io/github/dsheirer/dsp/c4fm/C4FMSymbolProcessor.java +++ b/src/main/java/io/github/dsheirer/dsp/c4fm/C4FMSymbolProcessor.java @@ -271,6 +271,26 @@ public abstract class C4FMSymbolProcessor return Integer.MAX_VALUE; } + /** + * Number of symbols without a sync detection after which syncInactivityTimeout() is invoked. Default is zero, + * which disables the timeout. + * @return timeout as a count of symbols, or zero to disable. + */ + protected int getSyncInactivityTimeoutSymbolCount() + { + return 0; + } + + /** + * Notification that no sync was detected for the sync inactivity timeout period (see + * getSyncInactivityTimeoutSymbolCount()). Invoked once per period of inactivity. Subclasses can override this to + * reset protocol specific state that was learned from the signal. Default implementation is a no-op. + */ + protected void syncInactivityTimeout() + { + //No-op default implementation + } + /** * Number of consecutive sync periods (capped at 3) where a sync was detected within the max message length. */ @@ -406,6 +426,11 @@ public abstract class C4FMSymbolProcessor symbolsSinceLastSync++; + if(symbolsSinceLastSync == getSyncInactivityTimeoutSymbolCount()) + { + syncInactivityTimeout(); + } + //Clear synchronized state when we miss a sync period. if(mSynchronized && symbolsSinceLastSync > getMaxMessageDibitLength()) { diff --git a/src/main/java/io/github/dsheirer/module/decode/dmr/DMRSymbolProcessor.java b/src/main/java/io/github/dsheirer/module/decode/dmr/DMRSymbolProcessor.java index 49c91c51..8472cd66 100644 --- a/src/main/java/io/github/dsheirer/module/decode/dmr/DMRSymbolProcessor.java +++ b/src/main/java/io/github/dsheirer/module/decode/dmr/DMRSymbolProcessor.java @@ -72,6 +72,10 @@ public class DMRSymbolProcessor extends C4FMSymbolProcessor //recordings with injected noise, where valid link control improved. A max of 10 leaks noise false syncs and 6 //costs more multipath data bursts. Applying the max once synchronized cost 3 - 20% of valid bursts. private static final int SYNC_ACQUISITION_MAX_BIT_ERRORS = 8; + //Restores automatic sync mode detection after this many symbols (5 seconds) without a sync detection, so that a + //sync mode that was established on earlier traffic (ie direct mode) doesn't prevent detection of later traffic + //that uses a different sync mode (ie a repeater) on the same channel. + private static final int SYNC_MODE_INACTIVITY_TIMEOUT_SYMBOLS = SYMBOL_RATE * 5; private final DMRSyncModeMonitor mSyncModeMonitor = new DMRSyncModeMonitor(); private final DMRMessageFramer mMessageFramer; @@ -116,6 +120,10 @@ public class DMRSymbolProcessor extends C4FMSymbolProcessor /** * Captures the detected sync pattern to update the sync mode monitor and the message framer. + * + * Only sync detections that follow a previous sync detection within the max sync interval (ie consecutive sync + * periods) are counted toward the sync mode, since noise produces isolated false sync detections while acquiring + * that would otherwise accumulate across long idle periods and lock the sync mode to the wrong mode. * @param syncDetector that triggered the sync detection. */ @Override @@ -124,11 +132,32 @@ public class DMRSymbolProcessor extends C4FMSymbolProcessor if(syncDetector instanceof DMRSoftSyncDetector dmrSyncDetector) { DMRSyncPattern pattern = dmrSyncDetector.getDetectedPattern(); - mSyncModeMonitor.detected(pattern); + + if(getConsecutiveSyncPeriods() >= 2) + { + mSyncModeMonitor.detected(pattern); + } + mMessageFramer.setSyncPattern(pattern); } } + @Override + protected int getSyncInactivityTimeoutSymbolCount() + { + return SYNC_MODE_INACTIVITY_TIMEOUT_SYMBOLS; + } + + /** + * Restores automatic sync mode detection after a period of inactivity, unless the mode was locked (ie BASE_ONLY + * for a traffic channel). + */ + @Override + protected void syncInactivityTimeout() + { + mSyncModeMonitor.reset(); + } + /** * Once synchronized with at least two consecutive sync detections, only allows sync detections within a small * window around each expected burst boundary (multiples of the burst length) to prevent false sync detections diff --git a/src/main/java/io/github/dsheirer/module/decode/dmr/sync/DMRSyncModeMonitor.java b/src/main/java/io/github/dsheirer/module/decode/dmr/sync/DMRSyncModeMonitor.java index ab76373a..6cd2ce88 100644 --- a/src/main/java/io/github/dsheirer/module/decode/dmr/sync/DMRSyncModeMonitor.java +++ b/src/main/java/io/github/dsheirer/module/decode/dmr/sync/DMRSyncModeMonitor.java @@ -26,6 +26,11 @@ import java.util.List; * Monitors sync pattern detections to automatically detect the prevailing or dominant sync mode group used on a channel * and optimizes the DMR sync detectors to reduce the quantity of false detects of unused sync patterns and the overall * sync detection processing workload. + * + * An automatically detected mode is not permanent: reset() restores AUTOMATIC mode so that the dominant mode can be + * re-established, for example after a period of inactivity on a channel that carries sparse or mixed mode (ie + * repeater and direct mode) traffic. A mode that is set via setMode() (ie BASE_ONLY for a traffic channel) is locked + * and is not affected by sync detections or reset(). */ public class DMRSyncModeMonitor { @@ -34,6 +39,7 @@ public class DMRSyncModeMonitor private int mBaseCount; private int mMobileCount; private int mDirectCount; + private DMRSyncDetectMode mLockedMode = DMRSyncDetectMode.AUTOMATIC; private DMRSyncDetectMode mMode = DMRSyncDetectMode.AUTOMATIC; /** @@ -45,17 +51,39 @@ public class DMRSyncModeMonitor /** * Sets (and locks) the sync detection mode. This can be directly invoked when the operating mode is known - * in advance, such as for a repeated traffic channel where the expected mode is BASE_ONLY, otherwise this method - * is automatically invoked by this monitor when operating in ALL mode, once the dominant mode is established. + * in advance, such as for a repeated traffic channel where the expected mode is BASE_ONLY. Setting AUTOMATIC + * unlocks the mode so that this monitor detects the dominant mode from the sync detections. * @param mode to set */ public void setMode(DMRSyncDetectMode mode) { - mMode = mode; + mLockedMode = mode; + clearCounts(); + applyMode(mode); + } - for(DMRSyncDetector detector : mSyncDetectors) + /** + * Current sync detection mode. + */ + public DMRSyncDetectMode getMode() + { + return mMode; + } + + /** + * Restores AUTOMATIC mode and clears the sync detection counts so that the dominant mode is re-established from + * subsequent sync detections. Has no effect when the mode is locked via setMode(). + */ + public void reset() + { + if(mLockedMode == DMRSyncDetectMode.AUTOMATIC) { - detector.setMode(mode); + clearCounts(); + + if(mMode != DMRSyncDetectMode.AUTOMATIC) + { + applyMode(DMRSyncDetectMode.AUTOMATIC); + } } } @@ -92,15 +120,32 @@ public class DMRSyncModeMonitor if(mBaseCount - mMobileCount > DOMINANT_THRESHOLD && mBaseCount - mDirectCount > DOMINANT_THRESHOLD) { - setMode(DMRSyncDetectMode.BASE_ONLY); + applyMode(DMRSyncDetectMode.BASE_ONLY); } else if(mMobileCount - mBaseCount > DOMINANT_THRESHOLD && mMobileCount - mDirectCount > DOMINANT_THRESHOLD) { - setMode(DMRSyncDetectMode.MOBILE_ONLY); + applyMode(DMRSyncDetectMode.MOBILE_ONLY); } else if(mDirectCount - mBaseCount > DOMINANT_THRESHOLD && mDirectCount - mMobileCount > DOMINANT_THRESHOLD) { - setMode(DMRSyncDetectMode.DIRECT_ONLY); + applyMode(DMRSyncDetectMode.DIRECT_ONLY); + } + } + + private void clearCounts() + { + mBaseCount = 0; + mMobileCount = 0; + mDirectCount = 0; + } + + private void applyMode(DMRSyncDetectMode mode) + { + mMode = mode; + + for(DMRSyncDetector detector : mSyncDetectors) + { + detector.setMode(mode); } } diff --git a/src/test/java/io/github/dsheirer/module/decode/dmr/sync/DMRSyncModeMonitorTest.java b/src/test/java/io/github/dsheirer/module/decode/dmr/sync/DMRSyncModeMonitorTest.java new file mode 100644 index 00000000..c98e92c6 --- /dev/null +++ b/src/test/java/io/github/dsheirer/module/decode/dmr/sync/DMRSyncModeMonitorTest.java @@ -0,0 +1,87 @@ +/* + * ***************************************************************************** + * Copyright (C) 2014-2026 Dennis Sheirer + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + * **************************************************************************** + */ + + +package io.github.dsheirer.module.decode.dmr.sync; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +/** + * Unit tests for the DMR sync mode monitor. + */ +public class DMRSyncModeMonitorTest +{ + private static DMRSyncModeMonitor monitor(DMRSyncDetector detector) + { + DMRSyncModeMonitor monitor = new DMRSyncModeMonitor(); + monitor.add(detector); + return monitor; + } + + private static void detect(DMRSyncModeMonitor monitor, DMRSyncPattern pattern, int count) + { + for(int x = 0; x < count; x++) + { + monitor.detected(pattern); + } + } + + @Test + void dominantModeIsDetected() + { + DMRSyncDetector detector = new DMRSyncDetector() {}; + DMRSyncModeMonitor monitor = monitor(detector); + detect(monitor, DMRSyncPattern.DIRECT_DATA_TIMESLOT_1, 10); + assertEquals(DMRSyncDetectMode.AUTOMATIC, monitor.getMode()); + detect(monitor, DMRSyncPattern.DIRECT_DATA_TIMESLOT_1, 1); + assertEquals(DMRSyncDetectMode.DIRECT_ONLY, monitor.getMode()); + assertEquals(DMRSyncDetectMode.DIRECT_ONLY, detector.getMode()); + } + + @Test + void resetRestoresAutomaticAndAllowsNewDominantMode() + { + DMRSyncDetector detector = new DMRSyncDetector() {}; + DMRSyncModeMonitor monitor = monitor(detector); + detect(monitor, DMRSyncPattern.DIRECT_DATA_TIMESLOT_1, 11); + monitor.reset(); + assertEquals(DMRSyncDetectMode.AUTOMATIC, monitor.getMode()); + assertEquals(DMRSyncDetectMode.AUTOMATIC, detector.getMode()); + + //Counts are cleared by the reset, so the base station mode is established without having to outvote the + //direct mode detections from before the reset. + detect(monitor, DMRSyncPattern.BASE_STATION_DATA, 11); + assertEquals(DMRSyncDetectMode.BASE_ONLY, monitor.getMode()); + assertEquals(DMRSyncDetectMode.BASE_ONLY, detector.getMode()); + } + + @Test + void lockedModeIgnoresDetectionsAndReset() + { + DMRSyncDetector detector = new DMRSyncDetector() {}; + DMRSyncModeMonitor monitor = monitor(detector); + monitor.setMode(DMRSyncDetectMode.BASE_ONLY); + detect(monitor, DMRSyncPattern.DIRECT_DATA_TIMESLOT_1, 20); + monitor.reset(); + assertEquals(DMRSyncDetectMode.BASE_ONLY, monitor.getMode()); + assertEquals(DMRSyncDetectMode.BASE_ONLY, detector.getMode()); + } +}