Merge pull request #746 from OffbandMesh/feat/733-remote-presets
epic(#733): apply radio presets to repeaters, room servers and sensorsdocs/750-contact-naming-plan
commit
8939541f79
@ -0,0 +1,102 @@
|
||||
import '../models/radio_preset.dart';
|
||||
|
||||
// Radio presets on remote nodes (repeater, room server, sensor) through the
|
||||
// stock admin CLI (#733). Kept out of the screen so it can be tested.
|
||||
|
||||
/// Default `tempradio` duration (owner decision on #662).
|
||||
const int kTempRadioDefaultMinutes = 1440;
|
||||
|
||||
/// Longest `tempradio` duration that works. Firmware schedules the revert at
|
||||
/// `futureMillis(2000 + timeout_mins * 60 * 1000)` in a 32-bit int
|
||||
/// (simple_repeater/MyMesh.cpp), which overflows past this.
|
||||
const int kTempRadioMaxMinutes = 35791;
|
||||
|
||||
String _bandwidthKHz(int bandwidthHz) => '${bandwidthHz / 1000}';
|
||||
|
||||
/// Stock CLI: persist radio params (applied after a reboot).
|
||||
String setRadioCommand(
|
||||
String frequencyText,
|
||||
int bandwidthHz,
|
||||
int spreadingFactor,
|
||||
int codingRate,
|
||||
) =>
|
||||
'set radio $frequencyText,${_bandwidthKHz(bandwidthHz)},'
|
||||
'$spreadingFactor,$codingRate';
|
||||
|
||||
/// Stock CLI: apply radio params now, reverting after [minutes]. Nothing is
|
||||
/// saved on the node. Coding rate is the 4/x denominator (4/5 is 5), which
|
||||
/// is what firmware range-checks (5-8).
|
||||
String tempRadioCommand(
|
||||
String frequencyText,
|
||||
int bandwidthHz,
|
||||
int spreadingFactor,
|
||||
int codingRate,
|
||||
int minutes,
|
||||
) =>
|
||||
'tempradio $frequencyText,${_bandwidthKHz(bandwidthHz)},'
|
||||
'$spreadingFactor,$codingRate,$minutes';
|
||||
|
||||
/// A valid `tempradio` duration in minutes (1 to [kTempRadioMaxMinutes]),
|
||||
/// or null.
|
||||
int? parseTempRadioMinutes(String text) {
|
||||
final minutes = int.tryParse(text.trim());
|
||||
if (minutes == null || minutes < 1 || minutes > kTempRadioMaxMinutes) {
|
||||
return null;
|
||||
}
|
||||
return minutes;
|
||||
}
|
||||
|
||||
/// `tempradio` retunes the node 2 s after it replies, so anything sent after
|
||||
/// it in the same save would go out on the old frequency. Keep the original
|
||||
/// order otherwise, with retuning commands moved to the end.
|
||||
List<T> withRetuneLast<T>(List<T> commands, bool Function(T) retunes) => [
|
||||
...commands.where((c) => !retunes(c)),
|
||||
...commands.where(retunes),
|
||||
];
|
||||
|
||||
bool isRetuneCommand(String command) => command.startsWith('tempradio ');
|
||||
|
||||
/// Stock CLI: path hash mode, zero-based (2 bytes = mode 1).
|
||||
String setPathHashModeCommand(int mode) => 'set path.hash.mode $mode';
|
||||
|
||||
/// Whether a path hash that came from a preset goes out in this save (#735).
|
||||
/// Firmware has no temporary path hash, so with a temporary radio change it
|
||||
/// would outlive the revert; it stays pending for a normal save instead. A
|
||||
/// path hash the user set by hand is always sent.
|
||||
bool sendsPresetPathHash({
|
||||
required bool temporaryRadio,
|
||||
required bool pathHashFromPreset,
|
||||
}) => !(temporaryRadio && pathHashFromPreset);
|
||||
|
||||
/// The preset whose radio values match the form's, or null for custom values.
|
||||
/// The form holds frequency as text (the repeater reports e.g. "908.205017"),
|
||||
/// so frequency matches to the nearest kHz. TX power is not compared: the
|
||||
/// remote screen sets it separately. A preset with a path hash only matches
|
||||
/// a node on that mode (#747): MeshCore's "Canada" and "USA" differ only there.
|
||||
String? matchRemotePresetId(
|
||||
List<RadioPreset> presets, {
|
||||
required String frequencyText,
|
||||
required int? bandwidthHz,
|
||||
required int? spreadingFactor,
|
||||
required int? codingRate,
|
||||
required int pathHashMode,
|
||||
}) {
|
||||
final mhz = double.tryParse(frequencyText.trim());
|
||||
if (mhz == null ||
|
||||
bandwidthHz == null ||
|
||||
spreadingFactor == null ||
|
||||
codingRate == null) {
|
||||
return null;
|
||||
}
|
||||
final khz = (mhz * 1000).round();
|
||||
for (final p in presets) {
|
||||
if (p.frequencyKHz == khz &&
|
||||
p.bandwidth.hz == bandwidthHz &&
|
||||
p.spreadingFactor.value == spreadingFactor &&
|
||||
p.codingRate.value == codingRate &&
|
||||
presetPathHashMatches(p, pathHashMode + 1)) {
|
||||
return p.id;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@ -0,0 +1,256 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:meshcore_open/helpers/remote_radio_commands.dart';
|
||||
import 'package:meshcore_open/models/radio_preset.dart';
|
||||
import 'package:meshcore_open/models/radio_settings.dart';
|
||||
|
||||
RadioPreset preset(
|
||||
String id, {
|
||||
double mhz = 908.205,
|
||||
LoRaBandwidth bw = LoRaBandwidth.bw62_5,
|
||||
LoRaSpreadingFactor sf = LoRaSpreadingFactor.sf9,
|
||||
LoRaCodingRate cr = LoRaCodingRate.cr4_8,
|
||||
int? tx,
|
||||
int? hashBytes,
|
||||
}) => RadioPreset(
|
||||
id: id,
|
||||
title: id,
|
||||
region: 'USA',
|
||||
frequencyMHz: mhz,
|
||||
bandwidth: bw,
|
||||
spreadingFactor: sf,
|
||||
codingRate: cr,
|
||||
source: RadioPresetSource.offband,
|
||||
txPowerDbm: tx,
|
||||
pathHashBytes: hashBytes,
|
||||
);
|
||||
|
||||
void main() {
|
||||
group('remote radio commands (#662)', () {
|
||||
test('set radio keeps its existing format', () {
|
||||
expect(
|
||||
setRadioCommand('908.205', 62500, 9, 8),
|
||||
'set radio 908.205,62.5,9,8',
|
||||
);
|
||||
});
|
||||
|
||||
test('tempradio carries the values of the OKI-published command', () {
|
||||
// docs.okimesh.org 500kHz-testing: tempradio 909.750,500,10,5,1440.
|
||||
// "500.0" and "500" parse to the same bandwidth (firmware strtof,
|
||||
// CommonCLI.cpp tempradio handler).
|
||||
expect(
|
||||
tempRadioCommand('909.750', 500000, 10, 5, kTempRadioDefaultMinutes),
|
||||
'tempradio 909.750,500.0,10,5,1440',
|
||||
);
|
||||
});
|
||||
|
||||
test('coding rate goes out as the 4/x denominator', () {
|
||||
expect(tempRadioCommand('910.525', 62500, 7, 5, 60), endsWith(',7,5,60'));
|
||||
});
|
||||
|
||||
test(
|
||||
'the default duration is valid, so no error shows by default (#748)',
|
||||
() {
|
||||
expect(
|
||||
parseTempRadioMinutes('$kTempRadioDefaultMinutes'),
|
||||
kTempRadioDefaultMinutes,
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
test('duration must be 1 to the firmware overflow bound', () {
|
||||
expect(parseTempRadioMinutes('1440'), 1440);
|
||||
expect(parseTempRadioMinutes(' 1 '), 1);
|
||||
expect(parseTempRadioMinutes('$kTempRadioMaxMinutes'), 35791);
|
||||
expect(parseTempRadioMinutes('${kTempRadioMaxMinutes + 1}'), isNull);
|
||||
expect(parseTempRadioMinutes('0'), isNull);
|
||||
expect(parseTempRadioMinutes('-5'), isNull);
|
||||
expect(parseTempRadioMinutes('1.5'), isNull);
|
||||
expect(parseTempRadioMinutes(''), isNull);
|
||||
// 2000 + 35791 min fits a 32-bit int; one more minute does not.
|
||||
expect(2000 + kTempRadioMaxMinutes * 60 * 1000, lessThan(1 << 31));
|
||||
expect(
|
||||
2000 + (kTempRadioMaxMinutes + 1) * 60 * 1000,
|
||||
greaterThan(1 << 31),
|
||||
);
|
||||
});
|
||||
|
||||
test('tempradio is sent after every other command', () {
|
||||
final ordered = withRetuneLast([
|
||||
'set name X',
|
||||
'tempradio 909.75,500.0,10,5,1440',
|
||||
'set tx 20',
|
||||
'set repeat on',
|
||||
], isRetuneCommand);
|
||||
expect(ordered, [
|
||||
'set name X',
|
||||
'set tx 20',
|
||||
'set repeat on',
|
||||
'tempradio 909.75,500.0,10,5,1440',
|
||||
]);
|
||||
});
|
||||
|
||||
test('without tempradio the order is unchanged', () {
|
||||
final commands = [
|
||||
'set name X',
|
||||
'set radio 908.205,62.5,9,8',
|
||||
'set tx 20',
|
||||
];
|
||||
expect(withRetuneLast(commands, isRetuneCommand), commands);
|
||||
});
|
||||
});
|
||||
|
||||
group('preset path hash on remote nodes (#735)', () {
|
||||
test('published bytes become the zero-based CLI mode', () {
|
||||
final philly = preset('philly', hashBytes: 2);
|
||||
final socal = preset('socal', hashBytes: 3);
|
||||
expect(
|
||||
setPathHashModeCommand(philly.pathHashMode!),
|
||||
'set path.hash.mode 1',
|
||||
);
|
||||
expect(
|
||||
setPathHashModeCommand(socal.pathHashMode!),
|
||||
'set path.hash.mode 2',
|
||||
);
|
||||
expect(preset('usa').pathHashMode, isNull);
|
||||
});
|
||||
|
||||
test('a preset path hash is held back from a temporary change', () {
|
||||
expect(
|
||||
sendsPresetPathHash(temporaryRadio: true, pathHashFromPreset: true),
|
||||
isFalse,
|
||||
);
|
||||
expect(
|
||||
sendsPresetPathHash(temporaryRadio: false, pathHashFromPreset: true),
|
||||
isTrue,
|
||||
);
|
||||
});
|
||||
|
||||
test('a path hash set by hand is always sent', () {
|
||||
expect(
|
||||
sendsPresetPathHash(temporaryRadio: true, pathHashFromPreset: false),
|
||||
isTrue,
|
||||
);
|
||||
expect(
|
||||
sendsPresetPathHash(temporaryRadio: false, pathHashFromPreset: false),
|
||||
isTrue,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
group('matchRemotePresetId (#734)', () {
|
||||
final presets = [
|
||||
preset('arizona', tx: 20),
|
||||
preset(
|
||||
'oki',
|
||||
mhz: 909.75,
|
||||
bw: LoRaBandwidth.bw500,
|
||||
sf: LoRaSpreadingFactor.sf10,
|
||||
cr: LoRaCodingRate.cr4_5,
|
||||
),
|
||||
];
|
||||
|
||||
test('matches the frequency a repeater reports, to the nearest kHz', () {
|
||||
expect(
|
||||
matchRemotePresetId(
|
||||
presets,
|
||||
frequencyText: '908.205017',
|
||||
bandwidthHz: 62500,
|
||||
spreadingFactor: 9,
|
||||
codingRate: 8,
|
||||
pathHashMode: 0,
|
||||
),
|
||||
'arizona',
|
||||
);
|
||||
expect(
|
||||
matchRemotePresetId(
|
||||
presets,
|
||||
frequencyText: '909.750',
|
||||
bandwidthHz: 500000,
|
||||
spreadingFactor: 10,
|
||||
codingRate: 5,
|
||||
pathHashMode: 0,
|
||||
),
|
||||
'oki',
|
||||
);
|
||||
});
|
||||
|
||||
test('any differing value means custom (null)', () {
|
||||
expect(
|
||||
matchRemotePresetId(
|
||||
presets,
|
||||
frequencyText: '908.205',
|
||||
bandwidthHz: 62500,
|
||||
spreadingFactor: 9,
|
||||
codingRate: 5,
|
||||
pathHashMode: 0,
|
||||
),
|
||||
isNull,
|
||||
);
|
||||
expect(
|
||||
matchRemotePresetId(
|
||||
presets,
|
||||
frequencyText: '908.300',
|
||||
bandwidthHz: 62500,
|
||||
spreadingFactor: 9,
|
||||
codingRate: 8,
|
||||
pathHashMode: 0,
|
||||
),
|
||||
isNull,
|
||||
);
|
||||
});
|
||||
|
||||
test('unknown or unparsable fields match nothing', () {
|
||||
expect(
|
||||
matchRemotePresetId(
|
||||
presets,
|
||||
frequencyText: '',
|
||||
bandwidthHz: 62500,
|
||||
spreadingFactor: 9,
|
||||
codingRate: 8,
|
||||
pathHashMode: 0,
|
||||
),
|
||||
isNull,
|
||||
);
|
||||
expect(
|
||||
matchRemotePresetId(
|
||||
presets,
|
||||
frequencyText: '908.205',
|
||||
bandwidthHz: null,
|
||||
spreadingFactor: 9,
|
||||
codingRate: 8,
|
||||
pathHashMode: 0,
|
||||
),
|
||||
isNull,
|
||||
);
|
||||
});
|
||||
|
||||
test('a path hash preset only matches a node on that mode (#747)', () {
|
||||
final usaPair = [
|
||||
preset(
|
||||
'canada',
|
||||
mhz: 910.525,
|
||||
sf: LoRaSpreadingFactor.sf7,
|
||||
cr: LoRaCodingRate.cr4_5,
|
||||
hashBytes: 3,
|
||||
),
|
||||
preset(
|
||||
'usa',
|
||||
mhz: 910.525,
|
||||
sf: LoRaSpreadingFactor.sf7,
|
||||
cr: LoRaCodingRate.cr4_5,
|
||||
),
|
||||
];
|
||||
String? match(int mode) => matchRemotePresetId(
|
||||
usaPair,
|
||||
frequencyText: '910.525',
|
||||
bandwidthHz: 62500,
|
||||
spreadingFactor: 7,
|
||||
codingRate: 5,
|
||||
pathHashMode: mode,
|
||||
);
|
||||
expect(match(0), 'usa');
|
||||
expect(match(1), 'usa');
|
||||
expect(match(2), 'canada');
|
||||
});
|
||||
});
|
||||
}
|
||||
Loading…
Reference in new issue