Add full Companion OTA source builds

This commit is contained in:
mikecarper committed 2026-08-10 12:39:07 -07:00
1 parent 1bceda54a6
commit 88ba5b26cb
19 files changed
+1212 -86

No files matched your search

+98 -30
View File
@@ -25,6 +25,7 @@ import secrets
import shlex
import shutil
import signal
import socket
import struct
import subprocess
import sys
@@ -57,6 +58,8 @@ TEMP_RADIO_SWITCH_DELAY_SECONDS = 3
TEMP_RADIO_RETURN_MINUTES = 1
TEMP_RADIO_RETURN_MARGIN_SECONDS = 15
INSTALL_TARGET_WINDOW_MINUTES = 3
COMPANION_TERMINAL_START = "+++MESHCORE-TERM-START"
COMPANION_TERMINAL_STOP = "+++MESHCORE-TERM-STOP"
# Firmware may hold the apply reboot for up to 15 seconds while its reply
# drains. Do not interpret "still ready" as a failed install before that cap.
INSTALL_RECONCILE_WAIT_SECONDS = 20
@@ -1239,32 +1242,71 @@ def parse_temp_radio(value: str) -> tuple[float, float, int, int, int]:
def source_cli_command(args: argparse.Namespace, command_text: str, check: bool = True) -> str:
port = args.source_cli_serial or args.source_serial
if not port:
serial_port = args.source_cli_serial or args.source_serial
tcp_console = args.source_cli_tcp
if not serial_port and not tcp_console:
if check:
raise OtaError(
"a source CLI serial port is required to enable TempRadio (or use --source-already-temp)"
"a source CLI connection is required to enable TempRadio (or use --source-already-temp)"
)
return ""
command = [
args.meshcli,
"-r",
"-c", "off",
"-s", port,
"-b", str(args.source_baud),
command_text,
]
def run_once() -> str:
try:
result = run_checked(
command,
label=f"source command {command_text.split()[0]}",
timeout=30,
)
except OtaError as exc:
raise TransmissionError(f"source CLI link failed: {exc}") from exc
output = f"{result.stdout}\n{result.stderr}".strip()
if tcp_console:
host, port = split_host_port(tcp_console, 5002)
try:
with socket.create_connection((host, port), timeout=10) as connection:
connection.settimeout(10)
greeting = bytearray()
while b"\r\n> " not in greeting and len(greeting) < 4096:
chunk = connection.recv(512)
if not chunk:
break
greeting.extend(chunk)
connection.sendall(command_text.encode("utf-8") + b"\r\n")
response = bytearray()
while b"\r\n> " not in response and len(response) < 4096:
chunk = connection.recv(512)
if not chunk:
break
response.extend(chunk)
except (OSError, UnicodeError) as exc:
raise TransmissionError(f"source TCP console failed: {exc}") from exc
text = response.decode("utf-8", "replace")
match = re.search(r"(?:^|\r?\n)\s*->\s*(.*?)\r?\n>\s*$", text, re.DOTALL)
if not match:
raise TransmissionError(
f"source TCP console returned no command reply: {text.strip() or 'no output'}"
)
output = match.group(1).strip()
else:
wire_command = command_text
if getattr(args, "source_companion_terminal", False):
# meshcli raw mode keeps one serial open while writing this
# compound command. The full Companion consumes the start/stop
# tokens locally and runs the middle command in ASCII mode.
wire_command = (
f"{COMPANION_TERMINAL_START}\r"
f"{command_text}\r"
f"{COMPANION_TERMINAL_STOP}"
)
command = [
args.meshcli,
"-r",
"-c", "off",
"-s", serial_port,
"-b", str(args.source_baud),
wire_command,
]
try:
result = run_checked(
command,
label=f"source command {command_text.split()[0]}",
timeout=30,
)
except OtaError as exc:
raise TransmissionError(f"source CLI link failed: {exc}") from exc
output = f"{result.stdout}\n{result.stderr}".strip()
lowered = output.lower()
if "error" in lowered or "unknown command" in lowered or "err " in lowered:
raise OtaError(f"source rejected {command_text!r}: {output}")
@@ -1288,14 +1330,31 @@ def source_cli_command(args: argparse.Namespace, command_text: str, check: bool
def preflight_source_cli(args: argparse.Namespace) -> None:
if not (args.source_serial or args.source_cli_serial):
if not (args.source_serial or args.source_cli_serial or args.source_cli_tcp):
return
output = source_cli_command(args, "ota status")
if "OTA |" not in output or "target:" not in output:
def valid_status(value: str) -> bool:
full_seeder = "OTA seeder" in value and "install:disabled" in value
return full_seeder or ("OTA |" in value and "target:" in value)
serial_port = args.source_cli_serial or args.source_serial
if serial_port:
# Ordinary repeaters start in raw ASCII. A full Companion starts in
# Binary mode, so retry an invalid raw probe through its terminal
# control tokens and remember that transport for later TempRadio calls.
args.source_companion_terminal = False
output = source_cli_command(args, "ota status", check=False)
if not valid_status(output):
args.source_companion_terminal = True
output = source_cli_command(args, "ota status")
else:
output = source_cli_command(args, "ota status")
if not valid_status(output):
raise OtaError(
"OTA source did not return a valid `ota status`. Use an OTA-enabled "
"repeater/FULL raw text CLI; Companion USB is a binary API port and "
"cannot be the serial seeder."
"repeater raw text CLI, nRF52 full Companion USB port, or "
"companion_radio_full TCP console."
)
@@ -1793,10 +1852,15 @@ def build_parser() -> argparse.ArgumentParser:
source = parser.add_mutually_exclusive_group()
source.add_argument("--source-serial", metavar="PORT")
source.add_argument("--source-tcp", metavar="HOST[:PORT]")
parser.add_argument(
source_cli = parser.add_mutually_exclusive_group()
source_cli.add_argument(
"--source-cli-serial", metavar="PORT",
help="local text-CLI port for a TCP seeder source",
)
source_cli.add_argument(
"--source-cli-tcp", metavar="HOST[:PORT]",
help="companion_radio_full text console for a TCP seeder source (default port 5002)",
)
parser.add_argument("--controller-baud", type=int, default=115200)
parser.add_argument("--source-baud", type=int, default=115200)
parser.add_argument(
@@ -1890,10 +1954,14 @@ def validate_args(args: argparse.Namespace, parser: argparse.ArgumentParser) ->
parser.error("a controller connection is required")
if not any((args.source_serial, args.source_tcp)):
parser.error("a source seeder connection is required")
if args.source_serial and args.source_cli_serial:
parser.error("--source-cli-serial is only used with --source-tcp")
if args.source_tcp and not (args.source_cli_serial or args.source_already_temp):
parser.error("--source-tcp also needs --source-cli-serial or --source-already-temp")
if args.source_serial and (args.source_cli_serial or args.source_cli_tcp):
parser.error("--source-cli-serial/--source-cli-tcp are only used with --source-tcp")
if args.source_tcp and not (
args.source_cli_serial or args.source_cli_tcp or args.source_already_temp
):
parser.error(
"--source-tcp also needs --source-cli-serial, --source-cli-tcp, or --source-already-temp"
)
if args.controller_serial and args.source_serial:
if os.path.abspath(args.controller_serial) == os.path.abspath(args.source_serial):
parser.error("controller and source must be separate nodes/serial ports")
+103
View File
@@ -193,6 +193,109 @@ class FormatTests(unittest.TestCase):
ota.read_bounded_file(path, 3, "mOTA file")
class SourceCliTests(unittest.TestCase):
def test_full_companion_tcp_console_command(self) -> None:
connection = mock.MagicMock()
connection.__enter__.return_value = connection
connection.recv.side_effect = [
b"OTA console - type `ota ...`\r\n> ",
b" -> OTA seeder | install:disabled | serving:1\r\n> ",
]
args = argparse.Namespace(
source_cli_serial=None,
source_serial=None,
source_cli_tcp="192.0.2.10",
meshcli="meshcli",
source_baud=115200,
)
with mock.patch.object(
ota.socket, "create_connection", return_value=connection
) as create_connection:
output = ota.source_cli_command(args, "ota status")
create_connection.assert_called_once_with(("192.0.2.10", 5002), timeout=10)
connection.sendall.assert_called_once_with(b"ota status\r\n")
self.assertEqual(output, "OTA seeder | install:disabled | serving:1")
def test_full_companion_tcp_source_arguments_validate(self) -> None:
parser = ota.build_parser()
args = parser.parse_args([
"release.mota", "remote",
"--controller-serial", "/dev/controller",
"--source-tcp", "192.0.2.10:5001",
"--source-cli-tcp", "192.0.2.10:5002",
])
ota.validate_args(args, parser)
def test_source_preflight_accepts_seeder_only_status(self) -> None:
args = argparse.Namespace(
source_serial=None,
source_cli_serial=None,
source_cli_tcp="192.0.2.10:5002",
)
with mock.patch.object(
ota,
"source_cli_command",
return_value="OTA seeder | install:disabled | target:00000000",
) as source_command:
ota.preflight_source_cli(args)
source_command.assert_called_once_with(args, "ota status")
def test_serial_preflight_falls_back_to_companion_terminal(self) -> None:
args = argparse.Namespace(
source_serial="/dev/source",
source_cli_serial=None,
source_cli_tcp=None,
)
with mock.patch.object(
ota,
"source_cli_command",
side_effect=(
"",
"OTA seeder | install:disabled | target:00000000",
),
) as source_command:
ota.preflight_source_cli(args)
self.assertTrue(args.source_companion_terminal)
self.assertEqual(
source_command.call_args_list,
[
mock.call(args, "ota status", check=False),
mock.call(args, "ota status"),
],
)
def test_serial_companion_command_is_wrapped_in_terminal_tokens(self) -> None:
args = argparse.Namespace(
source_cli_serial=None,
source_serial="/dev/source",
source_cli_tcp=None,
source_companion_terminal=True,
meshcli="meshcli",
source_baud=115200,
)
completed = subprocess.CompletedProcess(
args=[], returncode=0,
stdout="OK - temp params for 120 mins",
stderr="",
)
with mock.patch.object(ota, "run_checked", return_value=completed) as run:
output = ota.source_cli_command(
args, "tempradio 909.95,250,7,5,120"
)
wire_command = run.call_args.args[0][-1]
self.assertEqual(
wire_command,
"+++MESHCORE-TERM-START\r"
"tempradio 909.95,250,7,5,120\r"
"+++MESHCORE-TERM-STOP",
)
self.assertIn("OK - temp params", output)
class CompatibilityTests(unittest.TestCase):
def setUp(self) -> None:
self.base_image = firmware(b"old" * 2000, VERSION_OLD)