feat(#430): client caplog control commands (enable/disable/erase/status)

Extends the 0xC4 caplog protocol with the companion control sub-codes the
firmware exposes (the companion has no CLI; #395 CLI verbs are repeater-
only). Firmware #417/#408.

- meshcore_protocol.dart: request sub-codes ENABLE/DISABLE/ERASE/STATUS +
  builders; ACK / STATUS parsers (CaplogAck, CaplogDeviceStatus).
- meshcore_connector.dart: setDeviceCaplogEnabled / eraseDeviceCaplog /
  getDeviceCaplogStatus; 0xC4 response routing split so ACK (0x10) and
  STATUS (0x11) dispatch to own completers, download stream unchanged.
- 5 unit tests for builders + parsers; analyze clean.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
pull/444/head
Strycher 4 months ago
parent 67eae2feb4
commit 3314350215

@ -288,6 +288,8 @@ class MeshCoreConnector extends ChangeNotifier {
Completer<Uint8List>? _caplogCompleter;
CaplogReassembler? _caplogReassembler;
bool _caplogAwaitingStart = false;
Completer<CaplogAck>? _caplogAckCompleter;
Completer<CaplogDeviceStatus>? _caplogStatusCompleter;
String? _firmwareVersion;
String? _deviceModel;
int? _offbandCaps;
@ -4323,6 +4325,23 @@ class MeshCoreConnector extends ChangeNotifier {
/// START/CHUNK/END are reassembled by [CaplogReassembler]; frames arriving
/// with no download in flight are ignored. (#430)
void _handleOffbandCaplogFrame(Uint8List frame) {
// Control replies (ACK / STATUS) go to their own pending completers,
// independent of any download in flight. (#430)
if (frame.length >= 2) {
if (frame[1] == caplogRespAck) {
final ack = parseCaplogAck(frame);
final c = _caplogAckCompleter;
if (ack != null && c != null && !c.isCompleted) c.complete(ack);
return;
}
if (frame[1] == caplogRespStatus) {
final status = parseCaplogStatus(frame);
final c = _caplogStatusCompleter;
if (status != null && c != null && !c.isCompleted) c.complete(status);
return;
}
}
// Download stream (START/CHUNK/END).
final completer = _caplogCompleter;
final reassembler = _caplogReassembler;
if (completer == null || reassembler == null || completer.isCompleted) {
@ -4359,6 +4378,73 @@ class MeshCoreConnector extends ChangeNotifier {
}
}
/// Enable or disable serial capture on the device (0xC4 control). Returns the
/// device's `ok` result. (#430)
Future<bool> setDeviceCaplogEnabled(
bool enabled, {
Duration timeout = const Duration(seconds: 5),
}) => _caplogControl(
enabled
? buildOffbandCaplogEnableFrame()
: buildOffbandCaplogDisableFrame(),
timeout,
);
/// Erase the device's serial-capture buffer (0xC4 control). Returns `ok`.
/// (#430)
Future<bool> eraseDeviceCaplog({
Duration timeout = const Duration(seconds: 5),
}) => _caplogControl(buildOffbandCaplogEraseFrame(), timeout);
Future<bool> _caplogControl(Uint8List frame, Duration timeout) async {
if (_caplogAckCompleter != null) {
throw StateError('A caplog control op is already in progress');
}
final completer = Completer<CaplogAck>();
_caplogAckCompleter = completer;
final timer = Timer(timeout, () {
if (!completer.isCompleted) {
completer.completeError(
TimeoutException('caplog control timed out', timeout),
);
}
});
try {
await sendFrame(frame);
final ack = await completer.future;
return ack.ok;
} finally {
timer.cancel();
_caplogAckCompleter = null;
}
}
/// Query the device's capture status: enabled / level / used / capacity
/// (0xC4 control). (#430)
Future<CaplogDeviceStatus> getDeviceCaplogStatus({
Duration timeout = const Duration(seconds: 5),
}) async {
if (_caplogStatusCompleter != null) {
throw StateError('A caplog status query is already in progress');
}
final completer = Completer<CaplogDeviceStatus>();
_caplogStatusCompleter = completer;
final timer = Timer(timeout, () {
if (!completer.isCompleted) {
completer.completeError(
TimeoutException('caplog status timed out', timeout),
);
}
});
try {
await sendFrame(buildOffbandCaplogStatusFrame());
return await completer.future;
} finally {
timer.cancel();
_caplogStatusCompleter = null;
}
}
/// Called when a BLOCK_LIST dump ends. A truncated dump (early-END) is
/// re-requested once the link settles; never derive removals from a partial
/// pull. The union reconcile against the local list is wired in B4.

@ -331,11 +331,89 @@ const int caplogSubStart = 0x01;
const int caplogSubChunk = 0x02;
const int caplogSubEnd = 0x03;
// Caplog request sub-codes in cmd_frame[1]. A bare [0xC4] (len 1) is DOWNLOAD
// for back-compat. Firmware #417/#408. (#430)
const int caplogReqDownload = 0x01;
const int caplogReqEnable =
0x02; // [0xC4,0x02,(level)] — omit level for default
const int caplogReqDisable = 0x03;
const int caplogReqErase = 0x04;
const int caplogReqStatus = 0x05;
// Caplog CONTROL response sub-codes in out_frame[1] (download stream sub-codes
// caplogSubStart/Chunk/End are above).
const int caplogRespAck = 0x10; // [0xC4,0x10, req_op, ok(0|1)]
const int caplogRespStatus =
0x11; // [0xC4,0x11, enabled, level, used(4B LE), cap(4B LE)]
/// Request frame to download the device's serial-capture buffer — a bare 1-byte
/// command, no payload. (#430)
/// command, no payload (firmware treats bare [0xC4] as DOWNLOAD). (#430)
Uint8List buildOffbandCaplogRequestFrame() =>
Uint8List.fromList([cmdOffbandCaplog]);
/// Enable capture on the device (default verbosity level). (#430)
Uint8List buildOffbandCaplogEnableFrame() =>
Uint8List.fromList([cmdOffbandCaplog, caplogReqEnable]);
/// Disable (stop) capture on the device. (#430)
Uint8List buildOffbandCaplogDisableFrame() =>
Uint8List.fromList([cmdOffbandCaplog, caplogReqDisable]);
/// Erase the device's capture buffer. (#430)
Uint8List buildOffbandCaplogEraseFrame() =>
Uint8List.fromList([cmdOffbandCaplog, caplogReqErase]);
/// Query capture status (enabled / level / used / capacity). (#430)
Uint8List buildOffbandCaplogStatusFrame() =>
Uint8List.fromList([cmdOffbandCaplog, caplogReqStatus]);
/// Parsed `[0xC4,0x10, req_op, ok]` control acknowledgement. [reqOp] echoes the
/// request sub-code (enable/disable/erase); [ok] is the device's result. (#430)
class CaplogAck {
const CaplogAck(this.reqOp, {required this.ok});
final int reqOp;
final bool ok;
}
/// Parse a caplog control ACK; null if [frame] isn't one. (#430)
CaplogAck? parseCaplogAck(Uint8List frame) {
if (frame.length < 4 ||
frame[0] != respCodeOffbandCaplog ||
frame[1] != caplogRespAck) {
return null;
}
return CaplogAck(frame[2], ok: frame[3] == 1);
}
/// Parsed `[0xC4,0x11, enabled, level, used(4B LE), cap(4B LE)]` status. (#430)
class CaplogDeviceStatus {
const CaplogDeviceStatus({
required this.enabled,
required this.level,
required this.usedBytes,
required this.capacityBytes,
});
final bool enabled;
final int level;
final int usedBytes;
final int capacityBytes;
}
/// Parse a caplog STATUS reply; null if [frame] isn't one. (#430)
CaplogDeviceStatus? parseCaplogStatus(Uint8List frame) {
if (frame.length < 12 ||
frame[0] != respCodeOffbandCaplog ||
frame[1] != caplogRespStatus) {
return null;
}
return CaplogDeviceStatus(
enabled: frame[2] != 0,
level: frame[3],
usedBytes: readUint32LE(frame, 4),
capacityBytes: readUint32LE(frame, 8),
);
}
// --- Offband block command (0xC2) — capability-gated; see
// docs/architecture/block-contract-as-built.md §8. Firmware as-built PR #247. ---
const int cmdOffbandBlock = 0xC2;

@ -0,0 +1,80 @@
import 'dart:typed_data';
import 'package:flutter_test/flutter_test.dart';
import 'package:meshcore_open/connector/meshcore_protocol.dart';
void main() {
group('caplog control frames', () {
test('builders emit the right command + sub-code', () {
expect(buildOffbandCaplogRequestFrame(), Uint8List.fromList([0xC4]));
expect(buildOffbandCaplogEnableFrame(), Uint8List.fromList([0xC4, 0x02]));
expect(
buildOffbandCaplogDisableFrame(),
Uint8List.fromList([0xC4, 0x03]),
);
expect(buildOffbandCaplogEraseFrame(), Uint8List.fromList([0xC4, 0x04]));
expect(buildOffbandCaplogStatusFrame(), Uint8List.fromList([0xC4, 0x05]));
});
test('parseCaplogAck reads req_op + ok', () {
final ack = parseCaplogAck(
Uint8List.fromList([0xC4, caplogRespAck, caplogReqEnable, 1]),
);
expect(ack, isNotNull);
expect(ack!.reqOp, caplogReqEnable);
expect(ack.ok, isTrue);
final fail = parseCaplogAck(
Uint8List.fromList([0xC4, caplogRespAck, caplogReqErase, 0]),
);
expect(fail!.ok, isFalse);
});
test('parseCaplogAck rejects non-ack / short / wrong-code frames', () {
// A download START frame (sub 0x01) is not an ACK.
expect(
parseCaplogAck(Uint8List.fromList([0xC4, caplogSubStart, 0, 0, 0, 0])),
isNull,
);
// Wrong command byte.
expect(
parseCaplogAck(Uint8List.fromList([0x00, caplogRespAck, 0, 1])),
isNull,
);
// Too short (missing ok byte).
expect(
parseCaplogAck(Uint8List.fromList([0xC4, caplogRespAck, 2])),
isNull,
);
});
test('parseCaplogStatus reads enabled/level/used/cap little-endian', () {
final frame = Uint8List.fromList([
0xC4, caplogRespStatus,
1, // enabled
4, // level
0x02, 0x01, 0x00, 0x00, // used = 258
0x00, 0x20, 0x00, 0x00, // capacity = 8192
]);
final s = parseCaplogStatus(frame);
expect(s, isNotNull);
expect(s!.enabled, isTrue);
expect(s.level, 4);
expect(s.usedBytes, 258);
expect(s.capacityBytes, 8192);
});
test('parseCaplogStatus rejects non-status / short frames', () {
expect(
parseCaplogStatus(Uint8List.fromList([0xC4, caplogRespAck, 2, 1])),
isNull,
);
expect(
parseCaplogStatus(
Uint8List.fromList([0xC4, caplogRespStatus, 1, 4, 0, 0]),
),
isNull,
);
});
});
}
Loading…
Cancel
Save

Powered by TurnKey Linux.