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);