feat(#649): convert a preset's path hash bytes to the firmware mode

Presets publish path hash size in bytes; firmware takes a zero-based
mode (1 byte = mode 0, 2 = 1, 3 = 2). RadioPreset.pathHashMode does
the conversion and returns null when the preset carries none or an
out-of-range size, so nothing is sent. Tests pin all three sizes and
that a published 2 never becomes mode 2.

Epic: #726
Agent: MagentaHawk (session 5a8922d6)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
pull/744/head
diffwireauto 2 months ago
parent 0ed9bd717a
commit d4a7177a2a

@ -55,6 +55,21 @@ class RadioPreset {
final bool offGrid;
int get frequencyHz => (frequencyMHz * 1000).round();
/// The firmware's path hash mode for [pathHashBytes], or null to leave it.
///
/// Presets publish the size in bytes; firmware takes a zero-based mode
/// (`sendFlood(..., path_hash_mode + 1)`): 1 byte = mode 0, 2 bytes = mode 1,
/// 3 bytes = mode 2. Passing a published 2 straight through would select
/// 3-byte hashes and put the node off its mesh.
int? get pathHashMode => pathHashModeForBytes(pathHashBytes);
}
/// 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) {
if (bytes == null || bytes < 1 || bytes > 3) return null;
return bytes - 1;
}
class RadioPresetFormatException implements Exception {

@ -193,4 +193,46 @@ void main() {
expect(upstreamRegionFor('New Zealand (Gisborne)'), 'New Zealand');
});
});
group('path hash bytes to firmware mode (#649)', () {
test('published bytes map to the zero-based firmware mode', () {
expect(pathHashModeForBytes(1), 0);
expect(pathHashModeForBytes(2), 1);
expect(pathHashModeForBytes(3), 2);
});
test('a published 2 never becomes mode 2 (3-byte hashes)', () {
expect(pathHashModeForBytes(2), isNot(2));
});
test('absent or out-of-range sizes send nothing', () {
expect(pathHashModeForBytes(null), isNull);
expect(pathHashModeForBytes(0), isNull);
expect(pathHashModeForBytes(4), isNull);
});
test('a parsed preset exposes its mode', () {
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',
},
]),
);
expect(r.presets[0].pathHashMode, 2);
expect(r.presets[1].pathHashMode, isNull);
});
});
}

Loading…
Cancel
Save

Powered by TurnKey Linux.