From 1190c49b64e4760ed88c96710f52015a708c55a2 Mon Sep 17 00:00:00 2001 From: diffwireauto <330573775+diffwireauto@users.noreply.github.com> Date: Thu, 1 Oct 2026 14:54:30 -0400 Subject: [PATCH] fix(#747): match a preset's path hash too, so a USA radio isn't shown as Canada MeshCore's "Canada" and "USA" share 910.525 / 62.5 / SF7 / CR5 and differ only in path hash (Canada 3-byte). Matching ignored path hash, and Canada sorts first, so a USA radio showed "Canada" (D1 screenshot). A preset with a path hash size now matches only a radio on that size; presets without one match as before. Epic: #726 Agent: MagentaHawk (session 5a8922d6) Co-Authored-By: Claude Opus 5.5 --- lib/models/radio_preset.dart | 7 +++++ lib/screens/settings_screen.dart | 4 ++- test/models/radio_preset_test.dart | 47 ++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) diff --git a/lib/models/radio_preset.dart b/lib/models/radio_preset.dart index ee705b04..b04cf33a 100644 --- a/lib/models/radio_preset.dart +++ b/lib/models/radio_preset.dart @@ -66,6 +66,13 @@ class RadioPreset { int? get pathHashMode => pathHashModeForBytes(pathHashBytes); } +/// Whether a radio using [radioPathHashBytes] fits [preset]'s path hash +/// (#747). Presets without a path hash fit any radio; one with a size only +/// fits a radio on that size. Without this, presets that differ only by path +/// hash (MeshCore's "Canada" 3-byte and "USA") are indistinguishable. +bool presetPathHashMatches(RadioPreset preset, int radioPathHashBytes) => + preset.pathHashBytes == null || preset.pathHashBytes == radioPathHashBytes; + /// Zero-based firmware path hash mode for a published size in bytes (1-3). /// Null for null or out-of-range input, so nothing is sent. int? pathHashModeForBytes(int? bytes) { diff --git a/lib/screens/settings_screen.dart b/lib/screens/settings_screen.dart index 46c21ceb..9b6f9c14 100644 --- a/lib/screens/settings_screen.dart +++ b/lib/screens/settings_screen.dart @@ -2157,7 +2157,9 @@ class _RadioSettingsFormState extends State<_RadioSettingsForm> { preset.bandwidth == snapshot.bandwidth && preset.spreadingFactor == snapshot.spreadingFactor && preset.codingRate == snapshot.codingRate && - (preset.txPowerDbm == null || preset.txPowerDbm == snapshot.txPowerDbm); + (preset.txPowerDbm == null || + preset.txPowerDbm == snapshot.txPowerDbm) && + presetPathHashMatches(preset, widget.connector.pathHashByteWidth); } String? _findMatchingPresetIdForSnapshot(_RadioSettingsSnapshot snapshot) { diff --git a/test/models/radio_preset_test.dart b/test/models/radio_preset_test.dart index 852c6bf4..b2dd13d7 100644 --- a/test/models/radio_preset_test.dart +++ b/test/models/radio_preset_test.dart @@ -194,6 +194,53 @@ void main() { }); }); + group('presetPathHashMatches (#747)', () { + final r = parseUpstreamPresets( + upstream([ + { + 'title': 'Canada', + 'frequency': '910.525', + 'spreading_factor': '7', + 'bandwidth': '62.5', + 'coding_rate': '5', + 'network_settings': {'path_hash_size': 3}, + }, + { + 'title': 'USA', + 'frequency': '910.525', + 'spreading_factor': '7', + 'bandwidth': '62.5', + 'coding_rate': '5', + }, + ]), + ); + final canada = r.presets[0]; + final usa = r.presets[1]; + + test('a preset with a path hash only fits a radio on that size', () { + expect(presetPathHashMatches(canada, 3), isTrue); + expect(presetPathHashMatches(canada, 2), isFalse); + expect(presetPathHashMatches(canada, 1), isFalse); + }); + + test('a preset without one fits any radio', () { + for (final bytes in [1, 2, 3]) { + expect(presetPathHashMatches(usa, bytes), isTrue); + } + }); + + test('a USA radio on 1- or 2-byte hashes is not mistaken for Canada', () { + // Same radio values; Canada sorts first by region, so without the + // path hash check it wins (the D1 screenshot). + for (final bytes in [1, 2]) { + final fitting = r.presets + .where((p) => presetPathHashMatches(p, bytes)) + .map((p) => p.title); + expect(fitting, ['USA']); + } + }); + }); + group('path hash bytes to firmware mode (#649)', () { test('published bytes map to the zero-based firmware mode', () { expect(pathHashModeForBytes(1), 0);