feat(#888): role-based CLI txt-type selection for remote CLI

F3/E3.1. When sending a CLI command to a remote peer, the message type
is chosen by the contact's ROLE (owner ruling): a companion (chat) gets
the upstream-1.18 command type (3); a repeater or room server keeps the
universal CLI_DATA type (1) it has always used. Decided by role, not
firmware version: a Contact carries no version, the user types the
commands (passthrough), and a non-1.18 companion silently drops the
command type (surfaced as no reply, #891).

Adds txtTypeCliCommand (3) and cliTxtTypeForContactType(), and
parameterizes buildSendCliCommandFrame with the txt type (default
CLI_DATA, so the existing repeater path stays byte-identical). A later
refinement could pin a peer's version via a `ver` probe on connect.

Tests: the role mapping and the frame's type byte + command passthrough.
Full suite green (1430); Gemini 2.5-pro review: one naming nit taken.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
epic/870-console-gating
diffwireauto 1 month ago committed by Strycher
parent 8017ce4baf
commit 0f0c34b054

@ -259,6 +259,10 @@ const int cmdImportPrivateKey = 24;
const int txtTypePlain = 0;
const int txtTypeCliData = 1;
const int txtTypeSigned = 2;
// Remote CLI command to a companion (upstream 1.18, TXT_TYPE_CLI_COMMAND).
// Repeaters/room servers treat it exactly like CLI_DATA; a pre-1.18 or
// Offband-pre-absorb companion silently drops it (surfaced as no reply).
const int txtTypeCliCommand = 3;
// Repeater request types (for server requests)
const int reqTypeGetStatus = 0x01;
@ -1630,20 +1634,30 @@ int calculateMessageTimeout({
// Build CLI command text message frame (companion_radio format)
// Format: [cmd][txt_type][attempt][timestamp x4][pub_key_prefix x6][text...]\0
/// The CLI txt type for a command sent to a peer of [contactType] (#888).
/// Decided by ROLE, not firmware version: a companion (chat) takes the 1.18
/// command type; a repeater/room server takes the universal CLI_DATA type it
/// has always used. A Contact carries no version, and a non-1.18 companion
/// silently drops the command type, so role is both sufficient and safe. A
/// later refinement could pin a peer's version with a `ver` probe on connect.
int cliTxtTypeForContactType(int contactType) =>
contactType == advTypeChat ? txtTypeCliCommand : txtTypeCliData;
Uint8List buildSendCliCommandFrame(
Uint8List repeaterPubKey,
Uint8List peerPubKey,
String command, {
int attempt = 0,
int? timestampSeconds,
int txtType = txtTypeCliData,
}) {
final timestamp =
timestampSeconds ?? (DateTime.now().millisecondsSinceEpoch ~/ 1000);
final writer = BufferWriter();
writer.writeByte(cmdSendTxtMsg);
writer.writeByte(txtTypeCliData);
writer.writeByte(txtType);
writer.writeByte(attempt.clamp(0, 255));
writer.writeUInt32LE(timestamp);
writer.writeBytes(repeaterPubKey.sublist(0, 6));
writer.writeBytes(peerPubKey.sublist(0, 6));
writer.writeString(command);
writer.writeByte(0);
return writer.toBytes();

@ -0,0 +1,65 @@
// #888: role-based CLI txt-type selection for remote CLI.
//
// A companion (chat) gets the 1.18 command type; a repeater/room server gets
// the universal CLI_DATA type. The user types the command, so the frame is a
// passthrough: the type byte changes, the command does not.
import 'dart:convert';
import 'dart:typed_data';
import 'package:flutter_test/flutter_test.dart';
import 'package:meshcore_open/connector/meshcore_protocol.dart';
void main() {
group('cliTxtTypeForContactType (#888)', () {
test('a companion gets the 1.18 command type', () {
expect(cliTxtTypeForContactType(advTypeChat), txtTypeCliCommand);
expect(txtTypeCliCommand, 3);
});
test('a repeater keeps the universal CLI_DATA type', () {
expect(cliTxtTypeForContactType(advTypeRepeater), txtTypeCliData);
});
test('a room server keeps the universal CLI_DATA type', () {
expect(cliTxtTypeForContactType(advTypeRoom), txtTypeCliData);
});
});
group('buildSendCliCommandFrame carries the chosen type (#888)', () {
final pubKey = Uint8List.fromList(List<int>.generate(32, (i) => i));
// [cmd][type][attempt][ts x4][pubKey x6][command][0]
const int typeOffset = 1;
const int commandOffset = 1 + 1 + 1 + 4 + 6;
test(
'defaults to CLI_DATA, so the existing repeater path is unchanged',
() {
final frame = buildSendCliCommandFrame(pubKey, 'get radio');
expect(frame[typeOffset], txtTypeCliData);
},
);
test('emits the command type when asked (companion path)', () {
final frame = buildSendCliCommandFrame(
pubKey,
'get radio',
txtType: txtTypeCliCommand,
);
expect(frame[typeOffset], txtTypeCliCommand);
});
test('the command is passed through byte-identical regardless of type', () {
const command = '0A|set tx 22';
final frame = buildSendCliCommandFrame(
pubKey,
command,
txtType: txtTypeCliCommand,
);
final written = ascii.decode(
frame.sublist(commandOffset, commandOffset + command.length),
);
expect(written, command);
});
});
}
Loading…
Cancel
Save

Powered by TurnKey Linux.