feat(#1068): radio per-channel notify contract, caps bit, caps byte 3, tests
Epic A of #544, plus the B1 contract it needed to fail behaviourally rather than as a compile error. Owner directive: app and radio notifications are separate controls, separate notifications, independent settings. So this adds a RADIO-side per-channel mode and touches the app's per-channel setting not at all. - offband_channel_notify.dart: 0x18 GET / 0x19 SET, RadioChannelNotify with the 1-based encoding (0 unset, 1 all, 2 mentions, 3 off), frame builders, and a parser that DROPS an unknown mode byte rather than guessing - offbandCap2ChannelNotify = 0x80, the last bit in caps byte 2 - parseOffbandCaps3 at device-info offset 87, length-gated, never version-gated (#1065). 87 and not 85: 85 is ui_led_enabled and 86 is ui_display_mode, so reading 85 would parse the LED state as a caps mask - connector: per-radio slot-to-mode state cleared in _clearDeviceUiState, request/set shaped on requestChannelTune/setChannelTune so state follows the device's re-read reply and never the request, reply handler, and DeviceUiSurface.channelNotify for refusals 15 tests across three groups. The A2 independence group is the load-bearing one: changing the app mode emits no radio frame, a radio reply saying Off does not mute the phone, and the app setting still works on a radio with no capability at all. That is what stops the two being quietly re-merged later. Proved the checks can fail: flipping the caps-3 offset to 85 fails three tests, including the one asserting the LED and display bytes are not mistaken for byte 3. Reverted. The radio control itself is Epic C; this pane explicitly does not own a channelNotify refusal, recorded at the switch rather than left implicit. Blocked downstream, recorded on #1069: B3's read-on-connect should hang off #1061's confirmed-list barrier, and the slot-to-mode cache belongs in #1061's PSK identity-diff reset set. flutter analyze clean; full suite 1804 passed.pull/1102/head
parent
180c01e670
commit
77f16755e7
@ -0,0 +1,98 @@
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'offband_device_ui.dart';
|
||||
|
||||
/// Per-channel notification mode stored on the RADIO, over `0xC5` 0x18 / 0x19
|
||||
/// (#544, meshcore-firmware#606).
|
||||
///
|
||||
/// This governs the RADIO's buzzer and nothing else. It is deliberately
|
||||
/// independent of the app's own per-channel `ChannelNotifyMode`, which governs
|
||||
/// whether the PHONE notifies: two settings, two outputs, no bridging between
|
||||
/// them (owner, 2026-11-03: "they are separate controls and separate
|
||||
/// notifications... they are independent settings"). Never resolve one from
|
||||
/// the other, and never offer an "inherit the app setting" value here.
|
||||
///
|
||||
/// Firmware is the authority and there is no firmware inherit concept: the
|
||||
/// owner's ruling was "Inherit is on the client side, not on the firmware
|
||||
/// side. How would firmware inherit from firmware?". [unset] is simply the
|
||||
/// absence of a per-channel choice, and the radio's own device-wide scope
|
||||
/// answers for that channel instead.
|
||||
const int offbandUiChannelNotifyGet = 0x18;
|
||||
const int offbandUiChannelNotifySet = 0x19;
|
||||
|
||||
/// A channel's radio notify mode, as the radio stores it.
|
||||
///
|
||||
/// The byte is **1-based** and 0 means "no choice made", because the value
|
||||
/// lives in a field that reads zero on every existing record and every fresh
|
||||
/// channel. If 0 meant a real mode, every untouched channel in the field would
|
||||
/// silently adopt it. The shipped `ChannelTune` byte uses the same convention
|
||||
/// for the same reason, so the two are consistent on the wire.
|
||||
enum RadioChannelNotify {
|
||||
/// No per-channel choice: the radio's device-wide scope answers instead.
|
||||
unset(0x00),
|
||||
|
||||
/// Beep for any message on this channel.
|
||||
all(0x01),
|
||||
|
||||
/// Beep only when the message names us, by the `@[name]` rule.
|
||||
mentions(0x02),
|
||||
|
||||
/// Never beep for this channel.
|
||||
off(0x03);
|
||||
|
||||
const RadioChannelNotify(this.stored);
|
||||
|
||||
/// The wire byte. Never built by hand.
|
||||
final int stored;
|
||||
|
||||
static RadioChannelNotify? fromStored(int stored) {
|
||||
for (final m in RadioChannelNotify.values) {
|
||||
if (m.stored == stored) return m;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// One channel's radio notify mode, as a `0xC5` 0x18 or 0x19 reply reports it.
|
||||
class RadioChannelNotifyReply {
|
||||
const RadioChannelNotifyReply({required this.channel, required this.mode});
|
||||
final int channel;
|
||||
final RadioChannelNotify mode;
|
||||
}
|
||||
|
||||
int _channelByte(int channel) {
|
||||
if (channel < 0 || channel > 0xFF) {
|
||||
throw ArgumentError.value(channel, 'channel', 'must be 0 to 255');
|
||||
}
|
||||
return channel;
|
||||
}
|
||||
|
||||
Uint8List buildChannelNotifyGetFrame(int channel) => Uint8List.fromList([
|
||||
cmdOffbandDeviceUi,
|
||||
offbandUiChannelNotifyGet,
|
||||
_channelByte(channel),
|
||||
]);
|
||||
|
||||
Uint8List buildChannelNotifySetFrame(int channel, RadioChannelNotify mode) =>
|
||||
Uint8List.fromList([
|
||||
cmdOffbandDeviceUi,
|
||||
offbandUiChannelNotifySet,
|
||||
_channelByte(channel),
|
||||
mode.stored,
|
||||
]);
|
||||
|
||||
/// Parse a `0xC5` channel-notify reply (0x18 or 0x19). Null when this is not
|
||||
/// one, when it is short, or when the mode byte is not a value we know: an
|
||||
/// unrecognized mode is dropped rather than guessed at, so a firmware that
|
||||
/// grows a fourth mode cannot have it read as one of ours.
|
||||
RadioChannelNotifyReply? parseChannelNotifyReply(List<int> frame) {
|
||||
if (frame.length < 4) return null;
|
||||
if (frame[0] != respCodeOffbandDeviceUi) return null;
|
||||
if (frame[1] != offbandUiChannelNotifyGet &&
|
||||
frame[1] != offbandUiChannelNotifySet) {
|
||||
return null;
|
||||
}
|
||||
final mode = RadioChannelNotify.fromStored(frame[3]);
|
||||
if (mode == null) return null;
|
||||
return RadioChannelNotifyReply(channel: frame[2], mode: mode);
|
||||
}
|
||||
@ -0,0 +1,272 @@
|
||||
// #544 Epic A (#1068): the red baseline for the RADIO's per-channel notify
|
||||
// mode over 0xC5 0x18 / 0x19.
|
||||
//
|
||||
// The feature this pins is that there are TWO independent settings: the app's
|
||||
// per-channel mode drives the PHONE, the radio's drives the BUZZER, and
|
||||
// neither moves the other (owner, 2026-11-03). The independence group below is
|
||||
// the load-bearing part -- it is what stops the two being quietly re-merged by
|
||||
// a later change.
|
||||
|
||||
import 'dart:convert';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:drift/native.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:meshcore_open/connector/meshcore_connector.dart';
|
||||
import 'package:meshcore_open/connector/meshcore_protocol.dart';
|
||||
import 'package:meshcore_open/connector/offband_channel_notify.dart';
|
||||
import 'package:meshcore_open/connector/offband_device_ui.dart';
|
||||
import 'package:meshcore_open/models/app_settings.dart';
|
||||
import 'package:meshcore_open/services/app_settings_service.dart';
|
||||
import 'package:meshcore_open/storage/drift/blob_store.dart';
|
||||
import 'package:meshcore_open/storage/drift/offband_database.dart';
|
||||
import 'package:meshcore_open/storage/prefs_manager.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
/// RESP_CODE_DEVICE_INFO from an Offband radio (fork length, dual version so
|
||||
/// the population classifier corroborates it). Caps byte 2 at offset 84;
|
||||
/// pass [length] 88 with [caps3] to carry a third caps byte at offset 87.
|
||||
Uint8List deviceInfo({
|
||||
required int caps2,
|
||||
int length = 85,
|
||||
int caps3 = 0,
|
||||
int ledByte = 0,
|
||||
int displayByte = 0,
|
||||
}) {
|
||||
final v = ascii.encode('1.5.0-1.17.0');
|
||||
final f = Uint8List(length);
|
||||
f[0] = 13;
|
||||
f[1] = 22;
|
||||
f.setRange(60, 60 + v.length, v);
|
||||
if (length >= 85) f[84] = caps2;
|
||||
if (length >= 86) f[85] = ledByte;
|
||||
if (length >= 87) f[86] = displayByte;
|
||||
if (length >= 88) f[87] = caps3;
|
||||
return f;
|
||||
}
|
||||
|
||||
void main() {
|
||||
late OffbandDatabase db;
|
||||
late MeshCoreConnector connector;
|
||||
late List<List<int>> sent;
|
||||
|
||||
setUp(() async {
|
||||
SharedPreferences.setMockInitialValues({});
|
||||
PrefsManager.reset();
|
||||
await PrefsManager.initialize();
|
||||
db = OffbandDatabase(NativeDatabase.memory());
|
||||
BlobStore.overrideForTest(BlobStore(db));
|
||||
sent = [];
|
||||
connector = MeshCoreConnector();
|
||||
connector.sendFrameOverrideForTest = (data) => sent.add(data.toList());
|
||||
connector.setConnectedForTest();
|
||||
});
|
||||
|
||||
tearDown(() async {
|
||||
BlobStore.clearTestOverride();
|
||||
await db.close();
|
||||
});
|
||||
|
||||
bool sentChannelNotify() => sent.any(
|
||||
(f) =>
|
||||
f.length >= 2 &&
|
||||
f[0] == cmdOffbandDeviceUi &&
|
||||
(f[1] == offbandUiChannelNotifyGet ||
|
||||
f[1] == offbandUiChannelNotifySet),
|
||||
);
|
||||
|
||||
// ---- A1: the radio's modes ------------------------------------------------
|
||||
|
||||
group('A1 radio channel notify', () {
|
||||
test('the wire bytes are the documented ones', () async {
|
||||
connector.handleFrameForTest(deviceInfo(caps2: offbandCap2ChannelNotify));
|
||||
sent.clear();
|
||||
|
||||
await connector.requestChannelNotify(2);
|
||||
await connector.setChannelNotify(2, RadioChannelNotify.mentions);
|
||||
|
||||
expect(
|
||||
sent,
|
||||
equals([
|
||||
[0xC5, 0x18, 0x02],
|
||||
[0xC5, 0x19, 0x02, 0x02],
|
||||
]),
|
||||
);
|
||||
});
|
||||
|
||||
test('a reply sets that slot, and only that slot', () {
|
||||
connector.handleFrameForTest(deviceInfo(caps2: offbandCap2ChannelNotify));
|
||||
connector.handleFrameForTest([0xC5, 0x18, 0x01, 0x02]);
|
||||
|
||||
expect(connector.channelNotifyFor(1), RadioChannelNotify.mentions);
|
||||
expect(connector.channelNotifyFor(0), isNull);
|
||||
expect(connector.channelNotifyFor(2), isNull);
|
||||
});
|
||||
|
||||
test('mode 0 is unset, and is distinct from Off', () {
|
||||
connector.handleFrameForTest(deviceInfo(caps2: offbandCap2ChannelNotify));
|
||||
connector.handleFrameForTest([0xC5, 0x18, 0x00, 0x00]);
|
||||
connector.handleFrameForTest([0xC5, 0x18, 0x01, 0x03]);
|
||||
|
||||
expect(connector.channelNotifyFor(0), RadioChannelNotify.unset);
|
||||
expect(connector.channelNotifyFor(1), RadioChannelNotify.off);
|
||||
});
|
||||
|
||||
test('a write is not shown until the radio confirms it', () async {
|
||||
connector.handleFrameForTest(deviceInfo(caps2: offbandCap2ChannelNotify));
|
||||
await connector.setChannelNotify(3, RadioChannelNotify.off);
|
||||
|
||||
expect(connector.channelNotifyFor(3), isNull);
|
||||
|
||||
connector.handleFrameForTest([0xC5, 0x19, 0x03, 0x03]);
|
||||
expect(connector.channelNotifyFor(3), RadioChannelNotify.off);
|
||||
});
|
||||
|
||||
test('an unknown mode byte is dropped, never guessed at', () {
|
||||
connector.handleFrameForTest(deviceInfo(caps2: offbandCap2ChannelNotify));
|
||||
connector.handleFrameForTest([0xC5, 0x18, 0x04, 0x09]);
|
||||
|
||||
expect(connector.channelNotifyFor(4), isNull);
|
||||
expect(parseChannelNotifyReply([0xC5, 0x18, 0x04, 0x09]), isNull);
|
||||
});
|
||||
|
||||
test('state does not survive a change of radio', () {
|
||||
connector.handleFrameForTest(deviceInfo(caps2: offbandCap2ChannelNotify));
|
||||
connector.handleFrameForTest([0xC5, 0x18, 0x01, 0x01]);
|
||||
expect(connector.channelNotifyFor(1), RadioChannelNotify.all);
|
||||
|
||||
connector.handleFrameForTest(deviceInfo(caps2: offbandCap2ChannelNotify));
|
||||
expect(connector.channelNotifyFor(1), isNull);
|
||||
});
|
||||
});
|
||||
|
||||
// ---- A2: the two settings are independent ---------------------------------
|
||||
|
||||
group('A2 independence of app and radio settings', () {
|
||||
test('changing the app mode emits no radio frame', () async {
|
||||
connector.handleFrameForTest(deviceInfo(caps2: offbandCap2ChannelNotify));
|
||||
sent.clear();
|
||||
|
||||
final settings = AppSettingsService();
|
||||
await settings.loadSettings();
|
||||
await settings.setChannelNotifyMode(
|
||||
devicePrefix: 'aabbccddee',
|
||||
pskHex: '00112233445566778899aabbccddeeff',
|
||||
channelName: 'skywarn',
|
||||
mode: ChannelNotifyMode.off,
|
||||
);
|
||||
|
||||
expect(sentChannelNotify(), isFalse);
|
||||
expect(sent, isEmpty);
|
||||
});
|
||||
|
||||
test('a radio reply does not touch the app store', () async {
|
||||
final settings = AppSettingsService();
|
||||
await settings.loadSettings();
|
||||
const prefix = 'aabbccddee';
|
||||
const psk = '00112233445566778899aabbccddeeff';
|
||||
await settings.setChannelNotifyMode(
|
||||
devicePrefix: prefix,
|
||||
pskHex: psk,
|
||||
channelName: 'skywarn',
|
||||
mode: ChannelNotifyMode.all,
|
||||
);
|
||||
|
||||
connector.handleFrameForTest(deviceInfo(caps2: offbandCap2ChannelNotify));
|
||||
connector.handleFrameForTest([0xC5, 0x18, 0x00, 0x03]);
|
||||
|
||||
expect(
|
||||
settings.channelNotifyMode(devicePrefix: prefix, pskHex: psk),
|
||||
ChannelNotifyMode.all,
|
||||
reason: 'the radio saying Off must not mute the phone',
|
||||
);
|
||||
});
|
||||
|
||||
test('the app setting works with no radio capability at all', () async {
|
||||
connector.handleFrameForTest(deviceInfo(caps2: 0x00));
|
||||
final settings = AppSettingsService();
|
||||
await settings.loadSettings();
|
||||
const prefix = 'aabbccddee';
|
||||
const psk = '00112233445566778899aabbccddeeff';
|
||||
|
||||
await settings.setChannelNotifyMode(
|
||||
devicePrefix: prefix,
|
||||
pskHex: psk,
|
||||
channelName: 'skywarn',
|
||||
mode: ChannelNotifyMode.mentionsOnly,
|
||||
);
|
||||
|
||||
expect(connector.supportsChannelNotify, isFalse);
|
||||
expect(
|
||||
settings.channelNotifyMode(devicePrefix: prefix, pskHex: psk),
|
||||
ChannelNotifyMode.mentionsOnly,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
// ---- A3: capability and caps byte 3 ---------------------------------------
|
||||
|
||||
group('A3 capability gating and caps byte 3', () {
|
||||
test('without 0x80 no channel-notify frame is ever emitted', () async {
|
||||
connector.handleFrameForTest(deviceInfo(caps2: 0x40));
|
||||
await connector.requestChannelNotify(0);
|
||||
await connector.setChannelNotify(0, RadioChannelNotify.off);
|
||||
|
||||
expect(connector.supportsChannelNotify, isFalse);
|
||||
expect(sentChannelNotify(), isFalse);
|
||||
});
|
||||
|
||||
test('0x80 is the bit, and other bits do not open it', () {
|
||||
connector.handleFrameForTest(deviceInfo(caps2: offbandCap2ChannelNotify));
|
||||
expect(connector.supportsChannelNotify, isTrue);
|
||||
|
||||
connector.handleFrameForTest(deviceInfo(caps2: 0x7F));
|
||||
expect(connector.supportsChannelNotify, isFalse);
|
||||
});
|
||||
|
||||
test('caps byte 3 reads offset 87', () {
|
||||
connector.handleFrameForTest(
|
||||
deviceInfo(caps2: 0x80, length: 88, caps3: 0x05),
|
||||
);
|
||||
expect(connector.offbandCaps3, 0x05);
|
||||
});
|
||||
|
||||
test('caps byte 3 is null on any shorter frame', () {
|
||||
for (final n in [85, 86, 87]) {
|
||||
connector.handleFrameForTest(deviceInfo(caps2: 0x80, length: n));
|
||||
expect(
|
||||
connector.offbandCaps3,
|
||||
isNull,
|
||||
reason: 'a $n-byte frame carries no byte 3',
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
test('85 and 86 are not mistaken for byte 3', () {
|
||||
// Those offsets are ui_led_enabled and ui_display_mode. Reading either
|
||||
// as a capability mask is the failure this pins.
|
||||
connector.handleFrameForTest(
|
||||
deviceInfo(
|
||||
caps2: 0x80,
|
||||
length: 88,
|
||||
ledByte: 0xAA,
|
||||
displayByte: 0xBB,
|
||||
caps3: 0x01,
|
||||
),
|
||||
);
|
||||
expect(connector.offbandCaps3, 0x01);
|
||||
expect(connector.offbandCaps3, isNot(0xAA));
|
||||
expect(connector.offbandCaps3, isNot(0xBB));
|
||||
});
|
||||
|
||||
test('a stock-shaped radio exposes nothing, whatever its bytes', () {
|
||||
final stock = Uint8List(82);
|
||||
stock[0] = 13;
|
||||
stock[1] = 14;
|
||||
connector.handleFrameForTest(stock);
|
||||
|
||||
expect(connector.supportsChannelNotify, isFalse);
|
||||
expect(connector.offbandCaps3, isNull);
|
||||
});
|
||||
});
|
||||
}
|
||||
Loading…
Reference in new issue