feat(#773): fake radio radio settings (cmd 11, 12, 38, 61)

SET_RADIO_PARAMS validates like firmware (150-2500 MHz in kHz, 7-500 kHz
bandwidth in Hz, SF 5-12, CR 5-8) and allows client repeat only on the
default repeat frequencies (433.000, 869.495, 918.000); SET_TX_POWER
takes -9 to max; SET_PATH_HASH_MODE takes modes 0-2 and only with a
zero second byte. SELF_INFO reports frequency and bandwidth through the
firmware's float round trip. The client's own frame builders drive the
tests under both profiles (stock matches Offband here). 62 tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
feat/757-fake-radio-behaviors
diffwireauto 2 months ago
parent aff4b58a65
commit fa509dcf90

@ -0,0 +1,121 @@
import 'dart:typed_data';
import 'package:flutter_test/flutter_test.dart';
import 'package:meshcore_open/connector/meshcore_protocol.dart';
import '../support/fake_radio/fake_radio.dart';
import '../support/fake_radio/fake_radio_codes.dart';
import '../support/fake_radio/fake_radio_profile.dart';
// #773 (B2 of #755): the client's own settings frames against the fake.
int _u32(List<int> b, int at) => ByteData.sublistView(
Uint8List.fromList(b),
at,
at + 4,
).getUint32(0, Endian.little);
Uint8List _selfInfo(FakeRadio r) =>
r.handle(Uint8List.fromList([fwCmdAppStart, 0, 0, 0, 0, 0, 0, 0])).single;
Uint8List _deviceInfo(FakeRadio r) =>
r.handle(Uint8List.fromList([fwCmdDeviceQuery, 3])).single;
void main() {
for (final profile in [FakeRadioProfile.offband, FakeRadioProfile.stock]) {
group('${profile().name} radio settings', () {
test('cmd 11 changes what SELF_INFO reports', () {
final r = FakeRadio(profile: profile());
expect(r.handle(buildSetRadioParamsFrame(909750, 500000, 10, 5)), [
[fwRespOk],
]);
final s = _selfInfo(r);
expect(_u32(s, 48), 909750);
expect(_u32(s, 52), 500000);
expect(s[56], 10);
expect(s[57], 5);
});
test('cmd 11 refuses out-of-range values and changes nothing', () {
final r = FakeRadio(profile: profile());
for (final bad in [
buildSetRadioParamsFrame(909750, 600000, 10, 5), // bw > 500 kHz
buildSetRadioParamsFrame(100000, 62500, 10, 5), // freq < 150 MHz
buildSetRadioParamsFrame(909750, 62500, 13, 5), // sf > 12
buildSetRadioParamsFrame(909750, 62500, 10, 4), // cr < 5
]) {
expect(r.handle(bad), [
[fwRespErr, fwErrIllegalArg],
]);
}
expect(_u32(_selfInfo(r), 48), 910525);
});
test('client repeat only on an allowed frequency', () {
final r = FakeRadio(profile: profile());
expect(
r.handle(
buildSetRadioParamsFrame(910525, 62500, 7, 5, clientRepeat: true),
),
[
[fwRespErr, fwErrIllegalArg],
],
);
expect(
r.handle(
buildSetRadioParamsFrame(918000, 250000, 11, 8, clientRepeat: true),
),
[
[fwRespOk],
],
);
expect(_deviceInfo(r)[80], 1);
});
test('cmd 12 accepts -9 to max TX power', () {
final r = FakeRadio(profile: profile());
expect(r.handle(buildSetRadioTxPowerFrame(-9)), [
[fwRespOk],
]);
expect(_selfInfo(r)[2], (-9) & 0xFF);
expect(r.handle(buildSetRadioTxPowerFrame(-10)), [
[fwRespErr, fwErrIllegalArg],
]);
expect(r.handle(buildSetRadioTxPowerFrame(23)), [
[fwRespErr, fwErrIllegalArg],
]);
expect(r.handle(buildSetRadioTxPowerFrame(22)), [
[fwRespOk],
]);
});
test('cmd 61 sets path hash mode 0-2, shown in DEVICE_INFO', () {
final r = FakeRadio(profile: profile());
expect(r.handle(buildSetPathHashModeFrame(1)), [
[fwRespOk],
]);
expect(_deviceInfo(r)[81], 1);
// The client clamps; a raw mode 3 is refused by the radio.
expect(r.handle(Uint8List.fromList([fwCmdSetPathHashMode, 0, 3])), [
[fwRespErr, fwErrIllegalArg],
]);
// A non-zero second byte isn't this command at all.
expect(r.handle(Uint8List.fromList([fwCmdSetPathHashMode, 1, 1])), [
[fwRespErr, fwErrUnsupportedCmd],
]);
});
test('cmd 38 sets the other params SELF_INFO reports', () {
final r = FakeRadio(profile: profile());
expect(r.handle(buildSetOtherParamsFrame(0x15, 2, 1)), [
[fwRespOk],
]);
final s = _selfInfo(r);
expect(s[44], 1); // multi acks
expect(s[45], 2); // advert location policy
expect(s[46], 0x15); // telemetry modes
expect(s[47], 1); // manual add contacts
});
});
}
}

@ -173,6 +173,21 @@ class FakeRadio {
autoAddConfig = cmd[1];
if (len >= 3) autoAddMaxHops = cmd[2] > 64 ? 64 : cmd[2];
return [okFrame()];
case fwCmdSetRadioParams when len >= 11:
return [_setRadioParams(cmd)];
case fwCmdSetRadioTxPower when len >= 2:
// MyMesh.cpp:2946-2955: int8, -9 to MAX_LORA_TX_POWER.
final power = cmd[1] >= 128 ? cmd[1] - 256 : cmd[1];
if (power < -9 || power > seed.maxTxPowerDbm) {
return [errFrame(fwErrIllegalArg)];
}
txPowerDbm = power;
return [okFrame()];
case fwCmdSetPathHashMode when len >= 3 && cmd[1] == 0:
// MyMesh.cpp:2990-2997: modes 0-2 (1-3 byte hashes).
if (cmd[2] >= 3) return [errFrame(fwErrIllegalArg)];
pathHashMode = cmd[2];
return [okFrame()];
case fwCmdSetOtherParams when len >= 2:
// MyMesh.cpp:2974.
manualAddContacts = cmd[1];
@ -262,6 +277,54 @@ class FakeRadio {
return [errFrame(fwErrIllegalArg)];
}
/// The bands where client repeat is allowed (`repeat_freq_ranges`,
/// MyMesh.cpp:1720-1727, default build), in kHz.
static const List<(int, int)> repeatFreqRanges = [
(433000, 433000),
(869495, 869495),
(918000, 918000),
];
/// CMD_SET_RADIO_PARAMS (MyMesh.cpp:2910-2945). Frequency in kHz, bandwidth
/// in Hz; an optional trailing byte turns client repeat on, which only some
/// frequencies allow.
Uint8List _setRadioParams(Uint8List cmd) {
final freq = _u32At(cmd, 1);
final bw = _u32At(cmd, 5);
final newSf = cmd[9];
final newCr = cmd[10];
final repeat = cmd.length > 11 ? cmd[11] : 0;
if (repeat != 0 &&
!repeatFreqRanges.any((r) => freq >= r.$1 && freq <= r.$2)) {
return errFrame(fwErrIllegalArg);
}
if (freq < 150000 ||
freq > 2500000 ||
newSf < 5 ||
newSf > 12 ||
newCr < 5 ||
newCr > 8 ||
bw < 7000 ||
bw > 500000) {
return errFrame(fwErrIllegalArg);
}
freqKhz = freq;
bwHz = bw;
sf = newSf;
cr = newCr;
clientRepeat = repeat != 0;
return okFrame();
}
/// Firmware keeps frequency and bandwidth as `float` thousands
/// (`_prefs.freq = freq / 1000.0`) and reports `(uint32_t)(_prefs.freq *
/// 1000)` (MyMesh.cpp:2574,2931); this reproduces that single-precision round
/// trip rather than echoing the integer.
static int _floatRoundTrip(int value) {
final stored = Float32List.fromList([value / 1000.0])[0];
return Float32List.fromList([stored * 1000])[0].truncate();
}
/// CMD_SEND_TXT_MSG (MyMesh.cpp:2587-2632). A plain message gets
/// RESP_CODE_SENT with the expected ACK, and the ACK arrives as
/// PUSH_CODE_SEND_CONFIRMED after [ackDelay]. A CLI command expects no ACK.
@ -542,8 +605,8 @@ class FakeRadio {
..addByte(advertLocPolicy)
..addByte(telemetryModes)
..addByte(manualAddContacts)
..add(_u32(freqKhz))
..add(_u32(bwHz))
..add(_u32(_floatRoundTrip(freqKhz)))
..add(_u32(_floatRoundTrip(bwHz)))
..addByte(sf)
..addByte(cr)
..add(utf8.encode(name));

Loading…
Cancel
Save

Powered by TurnKey Linux.