Implement waypoint mission upload (MSP_SET_WP) and Fly-mode send button

Fills in MspFlightControllerLink.uploadMission(), which previously just
threw UnimplementedError, plus a new verifyMission() (both now on the
generic FlightControllerLink interface, protocol-neutral by signature -
MAVLink/ArduPilot get their own implementation later without touching
callers).

All iNAV-specific encoding lives in the new msp_waypoint_codec.dart:
- encodeMspSetWaypoint(): the 21-byte MSP_SET_WP payload. Action/P1/P2/P3
  byte layout was checked against the actual iNAV 9.1.0 source
  (navigation.c/navigation.h), not guessed - notably our generic `loiter`
  action has no configurable duration, so it maps to
  NAV_WP_ACTION_HOLD_TIME with the max representable p1 (int16 max, not
  0xFFFF - that would read as -1 and end the hold immediately instead of
  never).
- parseMspWpGetInfo(): decodes MSP_WP_GETINFO's validity/count fields,
  used by verifyMission() to confirm the FC actually accepted the full
  mission (Doku 2.2/4.5 Ready-to-Fly-Gate: "upload + verified").

uploadMission() sends one MSP_SET_WP per waypoint in order (iNAV has no
batch command - WP#1 resets the FC's mission list, every next number must
follow immediately, only the last carries NAV_WP_FLAG_LAST) and rejects
missions above NAV_MAX_WAYPOINTS upfront instead of silently truncating.
MissionSyncService now calls the real verifyMission() instead of always
confirming, throwing MissionVerificationException when the FC doesn't
confirm the mission.

FlyScreen's footer swaps the warnings button for a send button (Doku:
"ersetze den warnings button mit einem wp send button", pink horizontal
PaperPlaneIcon, matching the existing paper-plane drone iconography) -
warnings/event log stay reachable via the drone status pill's Warnings
tab. BottomStatsBar/MissionFooterBar gained onSendTap/sending in place of
the old forceShowWarningsButton.

Verified end to end on the Pixel_10a emulator: tapping send with WLAN as
the active connection type triggers the real WifiNetworkSpecifier flow
through MspFlightControllerLink (correctly reports "no devices found" -
expected, no real mLRS bridge on the emulator); the actual MSP_SET_WP/
MSP_WP_GETINFO wire behavior is covered by tests against a fake FC
responder over LoopbackTransport instead.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Constantin Leue
2026-08-06 09:10:32 +02:00
co-authored by Claude Sonnet 5
parent 5fa55ac67d
commit 13475cc4f3
12 changed files with 737 additions and 48 deletions
@@ -3,25 +3,38 @@ import 'dart:typed_data';
import 'package:flutter_test/flutter_test.dart';
import 'package:dmc_app/domain/waypoint/flat_waypoint_list.dart';
import 'package:dmc_app/transport/loopback/loopback_transport.dart';
import 'package:dmc_app/transport/msp/msp_commands.dart';
import 'package:dmc_app/transport/msp/msp_flight_controller_link.dart';
import 'package:dmc_app/transport/msp/msp_frame.dart';
import 'package:dmc_app/transport/msp/msp_frame_decoder.dart';
/// Beantwortet jede ueber [transport] gesendete MSP-Anfrage sofort mit einer
/// synthetischen Antwort - simuliert den Flightcontroller, ohne echte
/// Hardware (Doku Kommunikationsschicht Abschnitt 9).
Timer _startFakeFlightController(
LoopbackTransport transport, {
int fixType = 3,
}) {
var answered = 0;
return Timer.periodic(const Duration(milliseconds: 5), (_) {
while (answered < transport.sentData.length) {
/// Simuliert den Flightcontroller ohne echte Hardware (Doku Kommunikations-
/// schicht Abschnitt 9): beantwortet jede ueber [transport] gesendete
/// MSP-Anfrage sofort mit einer synthetischen Antwort. Zeichnet ausserdem
/// jede empfangene MSP_SET_WP-Payload auf und haelt einen einstellbaren
/// MSP_WP_GETINFO-Zustand (fuer Upload-/Verifikations-Tests), analog
/// `posControl.waypointListValid`/`getWaypointCount()` auf dem echten FC.
class _FakeFlightController {
_FakeFlightController(this.transport, {this.fixType = 3}) {
_timer = Timer.periodic(const Duration(milliseconds: 5), _tick);
}
final LoopbackTransport transport;
final int fixType;
late final Timer _timer;
var _answered = 0;
final receivedSetWpPayloads = <Uint8List>[];
bool wpGetInfoValid = false;
int wpGetInfoCount = 0;
void _tick(Timer _) {
while (_answered < transport.sentData.length) {
final request =
MspFrameDecoder().addBytes(transport.sentData[answered]).single;
answered++;
MspFrameDecoder().addBytes(transport.sentData[_answered]).single;
_answered++;
final Uint8List payload;
switch (request.function) {
@@ -57,6 +70,12 @@ Timer _startFakeFlightController(
d.setUint8(1, 88); // Linkqualitaet %
d.setInt8(2, 6); // SNR 6 dB
payload = d.buffer.asUint8List();
case MspCommands.setWp:
receivedSetWpPayloads.add(request.payload);
payload = Uint8List(0);
case MspCommands.wpGetInfo:
payload = Uint8List.fromList(
[0, 15, wpGetInfoValid ? 1 : 0, wpGetInfoCount]);
default:
payload = Uint8List(0);
}
@@ -65,9 +84,17 @@ Timer _startFakeFlightController(
encodeMspV2Frame(MspDirection.response, request.function, payload),
);
}
});
}
void cancel() => _timer.cancel();
}
_FakeFlightController _startFakeFlightController(
LoopbackTransport transport, {
int fixType = 3,
}) =>
_FakeFlightController(transport, fixType: fixType);
void main() {
test(
'connect() + subscribeTelemetry() liefern echte, ueber MSP dekodierte '
@@ -147,4 +174,125 @@ void main() {
final link = MspFlightControllerLink(transport: LoopbackTransport());
expect(() => link.subscribeTelemetry(), throwsStateError);
});
group('uploadMission', () {
const waypoints = [
Waypoint(lat: 52.5, lon: 13.4, altitudeM: 100, speedMs: 15, catchRadiusM: 60),
Waypoint(lat: 52.51, lon: 13.41, altitudeM: 110, speedMs: 15, catchRadiusM: 60),
Waypoint(lat: 52.52, lon: 13.42, altitudeM: 120, speedMs: 15, catchRadiusM: 60),
];
test('sendet einen MSP_SET_WP pro Wegpunkt, WP#1-basiert, nur der '
'letzte traegt NAV_WP_FLAG_LAST', () async {
final transport = LoopbackTransport();
final responder = _startFakeFlightController(transport);
final link = MspFlightControllerLink(transport: transport);
await link.connect();
await link.uploadMission(const FlatWaypointList(waypoints));
expect(responder.receivedSetWpPayloads, hasLength(3));
for (var i = 0; i < 3; i++) {
final d = ByteData.sublistView(responder.receivedSetWpPayloads[i]);
expect(d.getUint8(0), i + 1, reason: 'WP# ist 1-basiert');
expect(d.getUint8(20), i == 2 ? 0xA5 : 0, reason: 'nur der letzte WP traegt LAST');
}
await link.disconnect();
responder.cancel();
}, timeout: const Timeout(Duration(seconds: 5)));
test('wirft ArgumentError bei leerer Mission', () async {
final transport = LoopbackTransport();
final responder = _startFakeFlightController(transport);
final link = MspFlightControllerLink(transport: transport);
await link.connect();
expect(
() => link.uploadMission(const FlatWaypointList([])),
throwsArgumentError,
);
await link.disconnect();
responder.cancel();
}, timeout: const Timeout(Duration(seconds: 5)));
test('wirft ArgumentError, wenn mehr Wegpunkte als NAV_MAX_WAYPOINTS '
'(15) vorliegen - ohne dabei etwas zu senden', () async {
final transport = LoopbackTransport();
final responder = _startFakeFlightController(transport);
final link = MspFlightControllerLink(transport: transport);
await link.connect();
final tooMany = List.generate(
16,
(_) => const Waypoint(lat: 0, lon: 0, altitudeM: 60, speedMs: 15, catchRadiusM: 60),
);
await expectLater(
() => link.uploadMission(FlatWaypointList(tooMany)),
throwsArgumentError,
);
expect(responder.receivedSetWpPayloads, isEmpty);
await link.disconnect();
responder.cancel();
}, timeout: const Timeout(Duration(seconds: 5)));
});
group('verifyMission', () {
test('liefert true, wenn der FC die Mission als gueltig mit passender '
'Wegpunktzahl meldet', () async {
final transport = LoopbackTransport();
final responder = _startFakeFlightController(transport)
..wpGetInfoValid = true
..wpGetInfoCount = 2;
final link = MspFlightControllerLink(transport: transport);
await link.connect();
const mission = FlatWaypointList([
Waypoint(lat: 0, lon: 0, altitudeM: 60, speedMs: 15, catchRadiusM: 60),
Waypoint(lat: 0.01, lon: 0, altitudeM: 60, speedMs: 15, catchRadiusM: 60),
]);
expect(await link.verifyMission(mission), isTrue);
await link.disconnect();
responder.cancel();
}, timeout: const Timeout(Duration(seconds: 5)));
test('liefert false, wenn der FC die Mission als ungueltig meldet', () async {
final transport = LoopbackTransport();
final responder = _startFakeFlightController(transport)
..wpGetInfoValid = false
..wpGetInfoCount = 0;
final link = MspFlightControllerLink(transport: transport);
await link.connect();
const mission = FlatWaypointList([
Waypoint(lat: 0, lon: 0, altitudeM: 60, speedMs: 15, catchRadiusM: 60),
]);
expect(await link.verifyMission(mission), isFalse);
await link.disconnect();
responder.cancel();
}, timeout: const Timeout(Duration(seconds: 5)));
test('liefert false, wenn die vom FC gezaehlte Wegpunktzahl nicht zur '
'erwarteten Mission passt (z.B. veraltete/vorherige Mission)', () async {
final transport = LoopbackTransport();
final responder = _startFakeFlightController(transport)
..wpGetInfoValid = true
..wpGetInfoCount = 1;
final link = MspFlightControllerLink(transport: transport);
await link.connect();
const mission = FlatWaypointList([
Waypoint(lat: 0, lon: 0, altitudeM: 60, speedMs: 15, catchRadiusM: 60),
Waypoint(lat: 0.01, lon: 0, altitudeM: 60, speedMs: 15, catchRadiusM: 60),
]);
expect(await link.verifyMission(mission), isFalse);
await link.disconnect();
responder.cancel();
}, timeout: const Timeout(Duration(seconds: 5)));
});
}
@@ -0,0 +1,155 @@
import 'dart:typed_data';
import 'package:flutter_test/flutter_test.dart';
import 'package:dmc_app/domain/waypoint/flat_waypoint_list.dart';
import 'package:dmc_app/transport/msp/msp_waypoint_codec.dart';
Waypoint _wp({
double lat = 52.5,
double lon = 13.4,
double alt = 120,
double speed = 15,
WaypointAction action = WaypointAction.none,
int? jumpTargetIndex,
int? jumpRepeatCount,
}) =>
Waypoint(
lat: lat,
lon: lon,
altitudeM: alt,
speedMs: speed,
catchRadiusM: 60,
action: action,
jumpTargetIndex: jumpTargetIndex,
jumpRepeatCount: jumpRepeatCount,
);
/// Liest die 21-Byte MSP_SET_WP-Payload wie der echte FC (`fc_msp.c`,
/// `case MSP_SET_WP`) - dient nur der Testverifikation, dupliziert bewusst
/// nicht encodeMspSetWaypoint().
({
int wpNo,
int action,
int lat,
int lon,
int alt,
int p1,
int p2,
int p3,
int flag,
}) _decode(Uint8List payload) {
final d = ByteData.sublistView(payload);
return (
wpNo: d.getUint8(0),
action: d.getUint8(1),
lat: d.getInt32(2, Endian.little),
lon: d.getInt32(6, Endian.little),
alt: d.getInt32(10, Endian.little),
p1: d.getInt16(14, Endian.little),
p2: d.getInt16(16, Endian.little),
p3: d.getInt16(18, Endian.little),
flag: d.getUint8(20),
);
}
void main() {
group('encodeMspSetWaypoint', () {
test('Payload ist immer genau 21 Byte lang (dataSize-Check des FC)', () {
final payload = encodeMspSetWaypoint(_wp(), wireIndex: 1, isLast: true);
expect(payload.length, 21);
});
test('wp_no, lat/lon (1e7), Hoehe (cm) und flag werden korrekt kodiert', () {
final payload = encodeMspSetWaypoint(
_wp(lat: 52.5, lon: 13.4, alt: 120),
wireIndex: 3,
isLast: false,
);
final d = _decode(payload);
expect(d.wpNo, 3);
expect(d.lat, 525000000);
expect(d.lon, 134000000);
expect(d.alt, 12000);
expect(d.flag, 0);
});
test('isLast setzt NAV_WP_FLAG_LAST (0xA5)', () {
final payload = encodeMspSetWaypoint(_wp(), wireIndex: 1, isLast: true);
expect(_decode(payload).flag, 0xA5);
});
test('WaypointAction.none -> NAV_WP_ACTION_WAYPOINT, p1 = Speed in cm/s', () {
final d = _decode(encodeMspSetWaypoint(_wp(speed: 15), wireIndex: 1, isLast: false));
expect(d.action, 0x01);
expect(d.p1, 1500);
expect(d.p2, 0);
expect(d.p3, 0);
});
test('WaypointAction.landing -> NAV_WP_ACTION_LAND, p1 = Speed in cm/s', () {
final d = _decode(encodeMspSetWaypoint(
_wp(speed: 12, action: WaypointAction.landing),
wireIndex: 1,
isLast: false,
));
expect(d.action, 0x08);
expect(d.p1, 1200);
expect(d.p2, 0);
});
test(
'WaypointAction.loiter -> NAV_WP_ACTION_HOLD_TIME mit maximalem p1 '
'(int16-Max statt 0xFFFF, sonst wuerde der FC das Halten sofort statt '
'dauerhaft beenden), p2 = Speed in cm/s', () {
final d = _decode(encodeMspSetWaypoint(
_wp(speed: 10, action: WaypointAction.loiter),
wireIndex: 1,
isLast: false,
));
expect(d.action, 0x03);
expect(d.p1, 0x7FFF);
expect(d.p2, 1000);
});
test(
'WaypointAction.jump -> NAV_WP_ACTION_JUMP, p1 = 1-basierte Ziel-WP#, '
'p2/p3 = Wiederholzahl', () {
final d = _decode(encodeMspSetWaypoint(
_wp(action: WaypointAction.jump, jumpTargetIndex: 2, jumpRepeatCount: 5),
wireIndex: 4,
isLast: false,
));
expect(d.action, 0x06);
// 0-basierter App-Index 2 -> 1-basierte FC-WP# 3.
expect(d.p1, 3);
expect(d.p2, 5);
expect(d.p3, 5);
});
test('WaypointAction.jump ohne jumpRepeatCount kodiert -1 (endlos)', () {
final d = _decode(encodeMspSetWaypoint(
_wp(action: WaypointAction.jump, jumpTargetIndex: 0),
wireIndex: 2,
isLast: false,
));
expect(d.p2, -1);
expect(d.p3, -1);
});
});
group('parseMspWpGetInfo', () {
test('dekodiert maxWaypoints/isValid/waypointCount', () {
final info = parseMspWpGetInfo(Uint8List.fromList([0, 15, 1, 5]));
expect(info.maxWaypoints, 15);
expect(info.isValid, isTrue);
expect(info.waypointCount, 5);
});
test('isValid == false bei 0', () {
final info = parseMspWpGetInfo(Uint8List.fromList([0, 15, 0, 0]));
expect(info.isValid, isFalse);
});
});
}