feat(#969): GPS mode and Power-save interval over 0xC5
Codec for 0xC5 sub-codes 0x0D/0x0E, gated on caps byte 2 0x20, per the contract on meshcore-firmware#1789: the 14-byte state reply (resolved mode, raw stored byte incl. 0xFF, ps_ok, activity, 32-bit interval and wait), extended in place in offband_device_ui.dart and the existing 0xC5 dispatcher rather than a second codec. Caps bits 0x10 channel tune and 0x20 GPS mode added beside the others. On a 0x20 radio the Location GPS card is the approved one Off / On / Power save control, one status row naming backed off with its wait and last fix, and a sleep row only in Power save where 0 reads as the firmware default. Power save is offered only where ps_ok says the build can run it. Every value shown is the device's reply. Other radios keep the #977 on/off toggle unchanged. Safety: a mode change resends the radio's own interval, so setGpsMode refuses visibly until state has been read rather than send an interval it does not know. The 0x7F error frame names no sub-code, so a refusal is attributed to the outstanding write and each screen shows only its own. A confirmed mode keeps the legacy gps custom-var mirror, and so the 0xC1 poll, in step, but never invents the var. Tests: codec, connector (gate, bytes, attribution, mirror) and screen. Full suite 1499 pass. Agent: MistyLynx (session 6c0d5797) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>pull/994/head
parent
2a6d7383fc
commit
4209b75284
@ -0,0 +1,184 @@
|
||||
// #969: the connector side of GPS mode over 0xC5 0x0D / 0x0E. Pins the gate
|
||||
// (no 0x20, no frame), the bytes a write sends, that a mode change resends the
|
||||
// radio's own interval rather than inventing one, that a refusal lands on the
|
||||
// GPS surface, and that the legacy `gps` custom-var mirror follows the device.
|
||||
|
||||
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/offband_device_ui.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, caps byte 2 at offset 84.
|
||||
///
|
||||
/// The population classifier honors caps bytes only on a fork-length frame
|
||||
/// with the dual version form, so a stock-looking frame would (correctly) have
|
||||
/// its caps ignored. This is the shape a real Offband radio sends.
|
||||
List<int> deviceInfo({required int caps2}) {
|
||||
final v = ascii.encode('1.5.0-1.17.0');
|
||||
final f = Uint8List(85);
|
||||
f[0] = 13;
|
||||
f[1] = 22;
|
||||
f.setRange(60, 60 + v.length, v);
|
||||
f[84] = caps2;
|
||||
return f;
|
||||
}
|
||||
|
||||
/// `[0xC5][sub][mode][stored][ps_ok][activity][interval:4 LE][wait:4 LE]`
|
||||
List<int> gpsReply({
|
||||
int sub = 0x0D,
|
||||
int mode = 1,
|
||||
int stored = 1,
|
||||
int psOk = 1,
|
||||
int activity = 1,
|
||||
int interval = 3600,
|
||||
int wait = 0,
|
||||
}) {
|
||||
final f = Uint8List(14);
|
||||
f[0] = 0xC5;
|
||||
f[1] = sub;
|
||||
f[2] = mode;
|
||||
f[3] = stored;
|
||||
f[4] = psOk;
|
||||
f[5] = activity;
|
||||
final d = ByteData.sublistView(f);
|
||||
d.setUint32(6, interval, Endian.little);
|
||||
d.setUint32(10, wait, Endian.little);
|
||||
return f;
|
||||
}
|
||||
|
||||
/// RESP_CODE_CUSTOM_VARS, `key:value` pairs comma-separated.
|
||||
List<int> customVars(String body) => [21, ...ascii.encode(body), 0];
|
||||
|
||||
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 sentGps(int sub) => sent.any(
|
||||
(f) => f.length >= 2 && f[0] == cmdOffbandDeviceUi && f[1] == sub,
|
||||
);
|
||||
|
||||
test('a radio advertising 0x20 is asked for its GPS state', () async {
|
||||
connector.handleFrameForTest(deviceInfo(caps2: 0x20));
|
||||
await Future<void>.delayed(Duration.zero);
|
||||
|
||||
expect(connector.supportsGpsMode, isTrue);
|
||||
expect(sentGps(offbandUiGpsGet), isTrue);
|
||||
});
|
||||
|
||||
test('a radio without 0x20 never sees a GPS frame', () async {
|
||||
connector.handleFrameForTest(deviceInfo(caps2: 0x01));
|
||||
await connector.requestGpsMode();
|
||||
await connector.setGpsMode(GpsMode.on, intervalSeconds: 0);
|
||||
|
||||
expect(connector.supportsGpsMode, isFalse);
|
||||
expect(sentGps(offbandUiGpsGet), isFalse);
|
||||
expect(sentGps(offbandUiGpsSet), isFalse);
|
||||
});
|
||||
|
||||
test('the reply becomes the state the screen reads', () {
|
||||
connector.handleFrameForTest(deviceInfo(caps2: 0x20));
|
||||
connector.handleFrameForTest(
|
||||
gpsReply(mode: 2, stored: 2, activity: 4, interval: 900, wait: 2880),
|
||||
);
|
||||
|
||||
final g = connector.gpsModeState!;
|
||||
expect(g.mode, GpsMode.powerSave);
|
||||
expect(g.activity, GpsActivity.backedOff);
|
||||
expect(g.intervalSeconds, equals(900));
|
||||
expect(g.waitSeconds, equals(2880));
|
||||
});
|
||||
|
||||
test('a mode change resends the radio\'s current interval', () async {
|
||||
connector.handleFrameForTest(deviceInfo(caps2: 0x20));
|
||||
connector.handleFrameForTest(gpsReply(mode: 1, interval: 3600));
|
||||
sent.clear();
|
||||
|
||||
await connector.setGpsMode(GpsMode.powerSave);
|
||||
|
||||
expect(sent, equals([buildGpsModeSetFrame(GpsMode.powerSave, 3600)]));
|
||||
});
|
||||
|
||||
test('an explicit interval is sent as given', () async {
|
||||
connector.handleFrameForTest(deviceInfo(caps2: 0x20));
|
||||
connector.handleFrameForTest(gpsReply(mode: 2, interval: 3600));
|
||||
sent.clear();
|
||||
|
||||
await connector.setGpsMode(GpsMode.powerSave, intervalSeconds: 120);
|
||||
|
||||
expect(sent, equals([buildGpsModeSetFrame(GpsMode.powerSave, 120)]));
|
||||
});
|
||||
|
||||
test('a write before any read is refused visibly, nothing sent', () async {
|
||||
connector.handleFrameForTest(deviceInfo(caps2: 0x20));
|
||||
sent.clear();
|
||||
|
||||
await connector.setGpsMode(GpsMode.on);
|
||||
|
||||
expect(sentGps(offbandUiGpsSet), isFalse);
|
||||
expect(connector.deviceUiError, isNotNull);
|
||||
expect(connector.deviceUiErrorSurface, DeviceUiSurface.gpsMode);
|
||||
});
|
||||
|
||||
test('a refusal after a GPS write is the GPS screen\'s, in its words', () {
|
||||
connector.handleFrameForTest(deviceInfo(caps2: 0x20));
|
||||
connector.handleFrameForTest(gpsReply(mode: 1, psOk: 0));
|
||||
connector.setGpsMode(GpsMode.powerSave);
|
||||
connector.handleFrameForTest([0xC5, 0x7F, 0x01]);
|
||||
|
||||
expect(connector.deviceUiErrorSurface, DeviceUiSurface.gpsMode);
|
||||
expect(connector.deviceUiError, 'This radio cannot run Power save');
|
||||
// The state is not touched by a refusal: the device did not change.
|
||||
expect(connector.gpsModeState!.mode, GpsMode.on);
|
||||
});
|
||||
|
||||
test('a refusal with no GPS write outstanding is not claimed by GPS', () {
|
||||
connector.handleFrameForTest(deviceInfo(caps2: 0x20));
|
||||
connector.handleFrameForTest([0xC5, 0x7F, 0x04]);
|
||||
|
||||
expect(connector.deviceUiError, isNotNull);
|
||||
expect(connector.deviceUiErrorSurface, isNull);
|
||||
});
|
||||
|
||||
test('a confirmed Off updates the gps custom-var mirror', () {
|
||||
connector.handleFrameForTest(customVars('gps:1,gps_interval:900'));
|
||||
connector.handleFrameForTest(deviceInfo(caps2: 0x20));
|
||||
connector.handleFrameForTest(gpsReply(sub: 0x0E, mode: 0, stored: 2));
|
||||
|
||||
expect(connector.currentCustomVars!['gps'], equals('0'));
|
||||
// Off kept the remembered Power-save byte.
|
||||
expect(connector.gpsModeState!.storedMode, equals(2));
|
||||
});
|
||||
|
||||
test('a radio that reported no gps var does not get one invented', () {
|
||||
connector.handleFrameForTest(customVars('gps_interval:900'));
|
||||
connector.handleFrameForTest(deviceInfo(caps2: 0x20));
|
||||
connector.handleFrameForTest(gpsReply(mode: 1));
|
||||
|
||||
expect(connector.currentCustomVars!.containsKey('gps'), isFalse);
|
||||
});
|
||||
}
|
||||
@ -0,0 +1,301 @@
|
||||
// Pins #969 on the Location screen, against the approved mockups (cards 1 to
|
||||
// 4, docs/initiatives/standalone/975-settings-restructure/): one Off / On /
|
||||
// Power save control, Power save offered only where ps_ok says the build can
|
||||
// run it, one status row naming backed off, and a sleep row that exists only
|
||||
// in Power save and treats 0 as the firmware default.
|
||||
|
||||
import 'package:drift/native.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:meshcore_open/connector/meshcore_connector.dart';
|
||||
import 'package:meshcore_open/connector/offband_device_ui.dart';
|
||||
import 'package:meshcore_open/l10n/app_localizations.dart';
|
||||
import 'package:meshcore_open/models/offband_gps_status.dart';
|
||||
import 'package:meshcore_open/screens/settings_screen.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:provider/provider.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
/// A radio that speaks the 0xC5 GPS sub-codes, with its state set directly.
|
||||
class _GpsRadio extends MeshCoreConnector {
|
||||
GpsModeState? gpsState;
|
||||
String? error;
|
||||
DeviceUiSurface? errorSurface;
|
||||
final List<(GpsMode, int?)> writes = [];
|
||||
|
||||
@override
|
||||
bool get supportsGpsMode => true;
|
||||
|
||||
@override
|
||||
bool get supportsOffbandGps => false;
|
||||
|
||||
@override
|
||||
GpsModeState? get gpsModeState => gpsState;
|
||||
|
||||
@override
|
||||
String? get deviceUiError => error;
|
||||
|
||||
@override
|
||||
DeviceUiSurface? get deviceUiErrorSurface => errorSurface;
|
||||
|
||||
@override
|
||||
Map<String, String>? get currentCustomVars => {'gps': '1'};
|
||||
|
||||
@override
|
||||
Future<void> requestGpsMode() async {}
|
||||
|
||||
@override
|
||||
Future<OffbandGpsStatus?> requestOffbandGps({
|
||||
Duration timeout = const Duration(seconds: 5),
|
||||
}) async => null;
|
||||
|
||||
@override
|
||||
Future<void> setGpsMode(GpsMode mode, {int? intervalSeconds}) async {
|
||||
writes.add((mode, intervalSeconds));
|
||||
}
|
||||
|
||||
@override
|
||||
void clearDeviceUiError() {
|
||||
error = null;
|
||||
errorSurface = null;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
GpsModeState gps({
|
||||
GpsMode mode = GpsMode.on,
|
||||
bool psOk = true,
|
||||
GpsActivity? activity = GpsActivity.on,
|
||||
int interval = 0,
|
||||
int wait = 0,
|
||||
}) => GpsModeState(
|
||||
mode: mode,
|
||||
storedMode: mode.code,
|
||||
powerSaveAvailable: psOk,
|
||||
activity: activity,
|
||||
intervalSeconds: interval,
|
||||
waitSeconds: wait,
|
||||
);
|
||||
|
||||
void main() {
|
||||
late OffbandDatabase db;
|
||||
late _GpsRadio radio;
|
||||
|
||||
setUp(() async {
|
||||
SharedPreferences.setMockInitialValues({});
|
||||
PrefsManager.reset();
|
||||
await PrefsManager.initialize();
|
||||
db = OffbandDatabase(NativeDatabase.memory());
|
||||
BlobStore.overrideForTest(BlobStore(db));
|
||||
radio = _GpsRadio();
|
||||
radio.sendFrameOverrideForTest = (_) {};
|
||||
});
|
||||
|
||||
tearDown(() async {
|
||||
BlobStore.clearTestOverride();
|
||||
await db.close();
|
||||
});
|
||||
|
||||
/// [settle] false is for a pane showing an indeterminate spinner, which
|
||||
/// animates forever and so never lets `pumpAndSettle` finish.
|
||||
Future<void> openLocation(WidgetTester tester, {bool settle = true}) async {
|
||||
tester.view.physicalSize = const Size(400, 1000);
|
||||
tester.view.devicePixelRatio = 1.0;
|
||||
addTearDown(tester.view.resetPhysicalSize);
|
||||
addTearDown(tester.view.resetDevicePixelRatio);
|
||||
await tester.pumpWidget(
|
||||
MultiProvider(
|
||||
providers: [
|
||||
ChangeNotifierProvider<MeshCoreConnector>.value(value: radio),
|
||||
ChangeNotifierProvider<AppSettingsService>(
|
||||
create: (_) => AppSettingsService(),
|
||||
),
|
||||
],
|
||||
child: const MaterialApp(
|
||||
localizationsDelegates: AppLocalizations.localizationsDelegates,
|
||||
supportedLocales: AppLocalizations.supportedLocales,
|
||||
home: SettingsScreen(),
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.text('Location'));
|
||||
if (settle) {
|
||||
await tester.pumpAndSettle();
|
||||
} else {
|
||||
// One frame to push the route, then enough time for its transition.
|
||||
await tester.pump();
|
||||
await tester.pump(const Duration(seconds: 1));
|
||||
}
|
||||
}
|
||||
|
||||
Finder segment(String label) => find.descendant(
|
||||
of: find.byType(SegmentedButton<GpsMode>),
|
||||
matching: find.text(label),
|
||||
);
|
||||
|
||||
testWidgets('one control with three states replaces the toggle', (
|
||||
tester,
|
||||
) async {
|
||||
radio.gpsState = gps();
|
||||
await openLocation(tester);
|
||||
|
||||
expect(segment('Off'), findsOneWidget);
|
||||
expect(segment('On'), findsOneWidget);
|
||||
expect(segment('Power save'), findsOneWidget);
|
||||
// The legacy two-control layout is gone on a 0x20 radio.
|
||||
expect(find.text('GPS Enable'), findsNothing);
|
||||
expect(find.byType(SwitchListTile), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('Power save is absent where the build cannot run it', (
|
||||
tester,
|
||||
) async {
|
||||
radio.gpsState = gps(psOk: false);
|
||||
await openLocation(tester);
|
||||
|
||||
expect(segment('Off'), findsOneWidget);
|
||||
expect(segment('On'), findsOneWidget);
|
||||
expect(segment('Power save'), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('each choice writes its mode and leaves the interval alone', (
|
||||
tester,
|
||||
) async {
|
||||
radio.gpsState = gps();
|
||||
await openLocation(tester);
|
||||
|
||||
await tester.tap(segment('Power save'));
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(segment('Off'));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
// No explicit interval: the connector resends the radio's own.
|
||||
expect(
|
||||
radio.writes,
|
||||
equals([(GpsMode.powerSave, null), (GpsMode.off, null)]),
|
||||
);
|
||||
});
|
||||
|
||||
testWidgets('backed off is named, with the wait to the next try', (
|
||||
tester,
|
||||
) async {
|
||||
radio.gpsState = gps(
|
||||
mode: GpsMode.powerSave,
|
||||
activity: GpsActivity.backedOff,
|
||||
wait: 2880,
|
||||
);
|
||||
await openLocation(tester);
|
||||
|
||||
expect(find.text('Backed off'), findsOneWidget);
|
||||
expect(find.text('Next try in 48 min'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('Off says what is shown instead of a fix', (tester) async {
|
||||
radio.gpsState = gps(mode: GpsMode.off, activity: GpsActivity.off);
|
||||
await openLocation(tester);
|
||||
|
||||
expect(find.text('GPS is off'), findsOneWidget);
|
||||
expect(find.text('Showing the last stored position'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('the sleep row exists only in Power save', (tester) async {
|
||||
radio.gpsState = gps(mode: GpsMode.on);
|
||||
await openLocation(tester);
|
||||
expect(find.text('Sleep between fix attempts'), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('a sleep of 0 reads as the firmware default, not a number', (
|
||||
tester,
|
||||
) async {
|
||||
radio.gpsState = gps(mode: GpsMode.powerSave, interval: 0);
|
||||
await openLocation(tester);
|
||||
|
||||
expect(find.text('Sleep between fix attempts'), findsOneWidget);
|
||||
expect(find.text('Firmware default, minimum 5 minutes'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('the stored sleep is shown, as the radio reported it', (
|
||||
tester,
|
||||
) async {
|
||||
radio.gpsState = gps(mode: GpsMode.powerSave, interval: 300);
|
||||
await openLocation(tester);
|
||||
expect(find.text('Every 5 minutes'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('the sleep editor sends what was typed, including 0', (
|
||||
tester,
|
||||
) async {
|
||||
radio.gpsState = gps(mode: GpsMode.powerSave, interval: 3600);
|
||||
await openLocation(tester);
|
||||
|
||||
await tester.tap(find.text('Sleep between fix attempts'));
|
||||
await tester.pumpAndSettle();
|
||||
await tester.enterText(find.byType(TextField), '0');
|
||||
await tester.tap(find.text('Save'));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(radio.writes, equals([(GpsMode.powerSave, 0)]));
|
||||
});
|
||||
|
||||
testWidgets('a negative or non-numeric sleep is refused, nothing sent', (
|
||||
tester,
|
||||
) async {
|
||||
radio.gpsState = gps(mode: GpsMode.powerSave, interval: 3600);
|
||||
await openLocation(tester);
|
||||
|
||||
for (final bad in ['-5', 'abc']) {
|
||||
await tester.tap(find.text('Sleep between fix attempts'));
|
||||
await tester.pumpAndSettle();
|
||||
await tester.enterText(find.byType(TextField), bad);
|
||||
await tester.tap(find.text('Save'));
|
||||
await tester.pumpAndSettle();
|
||||
final message = find.text('Enter a whole number of seconds, 0 or more.');
|
||||
expect(message, findsOneWidget, reason: 'input "$bad"');
|
||||
// Persistent: still there after the default 4-second toast would end.
|
||||
await tester.pump(const Duration(seconds: 6));
|
||||
expect(message, findsOneWidget, reason: 'input "$bad" after 6 s');
|
||||
await tester.tap(message);
|
||||
await tester.pumpAndSettle();
|
||||
}
|
||||
expect(radio.writes, isEmpty);
|
||||
});
|
||||
|
||||
testWidgets('a GPS refusal is held on the card until dismissed', (
|
||||
tester,
|
||||
) async {
|
||||
radio.gpsState = gps();
|
||||
radio.error = 'This radio cannot run Power save';
|
||||
radio.errorSurface = DeviceUiSurface.gpsMode;
|
||||
await openLocation(tester);
|
||||
|
||||
expect(find.text('This radio cannot run Power save'), findsOneWidget);
|
||||
await tester.tap(find.byTooltip('Close'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('This radio cannot run Power save'), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('another screen\'s refusal is not shown on Location', (
|
||||
tester,
|
||||
) async {
|
||||
radio.gpsState = gps();
|
||||
radio.error = 'This radio has no buzzer';
|
||||
radio.errorSurface = DeviceUiSurface.notifyScope;
|
||||
await openLocation(tester);
|
||||
|
||||
expect(find.text('This radio has no buzzer'), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('before the first reply the card says it is reading', (
|
||||
tester,
|
||||
) async {
|
||||
radio.gpsState = null;
|
||||
await openLocation(tester, settle: false);
|
||||
|
||||
expect(find.text('Reading GPS settings…'), findsOneWidget);
|
||||
expect(find.byType(SegmentedButton<GpsMode>), findsNothing);
|
||||
});
|
||||
}
|
||||
Loading…
Reference in new issue