Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion ARCHITECTURE_2_flight_controller_communication.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ This architecture provides:
- **Purpose**: Manages flight controller connection lifecycle
- **Key Classes**:
- `FlightControllerConnection`: Connection establishment and management
- `FakeSerialForTests`: Mock serial class for unit testing
- **Key Methods**:
- `connect()`: Establishes connection with retry logic
- `disconnect()`: Closes connection gracefully
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from time import monotonic
from time import sleep as time_sleep
from time import time as time_time
from typing import TYPE_CHECKING, Any, ClassVar, NoReturn, Optional, Union, cast, no_type_check
from typing import TYPE_CHECKING, Any, ClassVar, Optional, Union, cast, no_type_check

import serial.tools.list_ports
import serial.tools.list_ports_common
Expand Down Expand Up @@ -108,33 +108,6 @@ def _serial_port_autodetect_rank(port: ListPortInfo) -> int:
# pylint: disable=too-many-lines


class FakeSerialForTests:
"""
A mock serial class for unit testing purposes.

This class simulates the behavior of a serial connection for testing purposes,
allowing for the testing of serial communication without needing a physical
serial device. It includes methods for reading, writing, and checking the
number of bytes in the input buffer, as well as closing the connection.
"""

def __init__(self, device: str) -> None:
self.device = device

def read(self, _len) -> str: # noqa: ANN001
return ""

def write(self, _buf) -> NoReturn: # noqa: ANN001
msg = "write always fails"
raise Exception(msg) # pylint: disable=broad-exception-raised

def inWaiting(self) -> int: # noqa: N802, pylint: disable=invalid-name
return 0

def close(self) -> None:
pass


DEFAULT_BAUDRATE: int = 115200
FC_STATUS_TEXT_BUFFER_LIMIT = 100
DEVICE_FC_PARAM_FROM_FILE: str = "file" # Special device name to simulate FC parameters from params.param file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ def update(self, sample: TelemetrySample) -> TelemetryState: # pylint: disable=
and radio.source == cached_radio.source
and radio.rxerrors >= cached_radio.rxerrors
)
old_radio = previous.radio if same_counter else None
if not same_counter:
# Rebaseline the radio independently; preserve byte/message measurement intervals.
previous = replace(previous, radio=radio)
Expand All @@ -131,7 +132,6 @@ def update(self, sample: TelemetrySample) -> TelemetryState: # pylint: disable=
strict=True,
)
)
old_radio = previous.radio if same_counter else None
# Polling the same fresh report must retain its last measured rate.
if radio is None or old_radio is None or radio.time != old_radio.time:
radio_error_rate = None
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ pytest_add_cli_args_test_selection = [
"tests/test_data_model_firmware_upload.py",
"tests/test_frontend_tkinter_usage_popup_windows.py",
"tests/test_frontend_tkinter_log_analysis.py",
"tests/test_frontend_tkinter_log_analysis_rendering.py",
"tests/test_backend_flightcontroller_connection.py",
"tests/test_backend_flightcontroller.py",
"tests/test_backend_filesystem_program_settings.py",
Expand Down
110 changes: 110 additions & 0 deletions tests/acceptance_template_import_from_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@
- Tests skip gracefully when empty templates are unavailable for certain vehicle types
- ArduPlane uses its available empty_4.7.x template; Heli and Rover are skipped
where an empty template is required because none is currently available
- Template setup downloads exact-release metadata when its cache is missing or stale;
project metadata seeding then runs offline from the validated cache

"""

Expand All @@ -136,13 +138,16 @@
import re
import shutil
from pathlib import Path
from tempfile import TemporaryDirectory

import pytest

import ardupilot_methodic_configurator.backend_filesystem as filesystem_module
from ardupilot_methodic_configurator.annotate_params import get_xml_url
from ardupilot_methodic_configurator.backend_filesystem import LocalFilesystem
from ardupilot_methodic_configurator.backend_filesystem_vehicle_components import VehicleComponents
from ardupilot_methodic_configurator.backend_flightcontroller import DEVICE_FC_PARAM_FROM_FILE, FlightController
from ardupilot_methodic_configurator.backend_internet import download_file_from_url
from ardupilot_methodic_configurator.data_model_par_dict import ParDict
from ardupilot_methodic_configurator.data_model_vehicle_components import ComponentDataModel
from ardupilot_methodic_configurator.data_model_vehicle_components_json_schema import (
Expand Down Expand Up @@ -258,6 +263,83 @@ def test_offline_metadata_seed_rejects_incompatible_firmware_metadata(tmp_path:
_seed_offline_parameter_metadata(template_dir, target_dir, "ArduCopter", "4.6.3")


@pytest.mark.parametrize(("vehicle_type", "firmware_version"), [("ArduCopter", "4.6.3"), ("ArduPlane", "4.7.2")])
@pytest.mark.parametrize("cached_release", [None, "4.6.0", "4.6.30", "4.6.3", "4.7.2"])
def test_template_setup_prepares_exact_release_metadata(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
vehicle_type: str,
firmware_version: str,
cached_release: str | None,
) -> None:
"""
Template setup repairs missing or stale caches and works offline with matching metadata.

GIVEN: A template with missing, stale or matching cached metadata
WHEN: Acceptance setup prepares the template and seeds a generated project
THEN: Both use the exact firmware release, downloading only when necessary
"""
metadata_vehicle = vehicle_type.removeprefix("Ardu")
(tmp_path / "vehicle_components.json").write_text(
json.dumps({"Components": {"Flight Controller": {"Firmware": {"Version": firmware_version}}}}), encoding="utf-8"
)
metadata_path = tmp_path / "apm.pdef.xml"
if cached_release:
metadata_path.write_text(f"<!-- Generated from git tag {metadata_vehicle}-{cached_release} -->", encoding="utf-8")
expected_metadata = f"<!-- Generated from git tag {metadata_vehicle}-{firmware_version} -->"
downloaded_urls = []

def download_metadata(url: str, path: str) -> bool:
assert cached_release != firmware_version, "Matching template metadata must work offline"
downloaded_urls.append(url)
Path(path).write_text(expected_metadata, encoding="utf-8")
return True

monkeypatch.setattr(f"{__name__}.download_file_from_url", download_metadata)

_prepare_template_parameter_metadata(tmp_path, vehicle_type)
target_dir = tmp_path / "new_vehicle"
target_dir.mkdir()
_seed_offline_parameter_metadata(tmp_path, target_dir, vehicle_type, firmware_version)

assert (target_dir / "apm.pdef.xml").read_text(encoding="utf-8") == expected_metadata
expected_url = get_xml_url(vehicle_type, firmware_version) + "apm.pdef.xml"
assert downloaded_urls == ([] if cached_release == firmware_version else [expected_url])


@pytest.mark.parametrize("downloaded_release", [None, "Copter-4.6.0", "Plane-4.6.3", "Copter-4.6.30", "unknown"])
def test_template_setup_preserves_cache_when_exact_metadata_is_unavailable(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, downloaded_release: str | None
) -> None:
"""
Failed refreshes cannot replace the template cache with incompatible metadata.

GIVEN: A stale template cache and an unavailable or incompatible exact-release download
WHEN: Acceptance setup attempts to refresh metadata
THEN: Setup fails and preserves the old cache for diagnosis
"""
(tmp_path / "vehicle_components.json").write_text(
json.dumps({"Components": {"Flight Controller": {"Firmware": {"Version": "4.6.3"}}}}), encoding="utf-8"
)
metadata_path = tmp_path / "apm.pdef.xml"
original_metadata = "<!-- Generated from git tag Copter-4.6.0 -->"
metadata_path.write_text(original_metadata, encoding="utf-8")

def download_metadata(_url: str, path: str) -> bool:
if downloaded_release is None:
return False
Path(path).write_text(f"<!-- Generated from git tag {downloaded_release} -->", encoding="utf-8")
return True

monkeypatch.setattr(f"{__name__}.download_file_from_url", download_metadata)

with pytest.raises(ValueError, match=r"Could not prepare|incompatible"):
_prepare_template_parameter_metadata(tmp_path, "ArduCopter")

assert metadata_path.read_text(encoding="utf-8") == original_metadata
assert sorted(path.name for path in tmp_path.iterdir()) == ["apm.pdef.xml", "vehicle_components.json"]


def _seed_offline_parameter_metadata(
metadata_source_dir: Path, new_vehicle_dir: str | Path, vehicle_type: str, firmware_version: str
) -> None:
Expand All @@ -281,6 +363,33 @@ def _seed_offline_parameter_metadata(
shutil.copy2(metadata_source, metadata_target)


def _prepare_template_parameter_metadata(template_dir: Path, vehicle_type: str) -> None:
"""Ensure template setup caches the exact release before running offline project imports."""
with (template_dir / "vehicle_components.json").open(encoding="utf-8") as component_file:
firmware_version = json.load(component_file)["Components"]["Flight Controller"]["Firmware"]["Version"]
metadata_vehicle = {"ArduCopter": "Copter", "ArduPlane": "Plane"}[vehicle_type]
release_tag = f"Generated from git tag {metadata_vehicle}-{firmware_version}"

def matches_release(metadata_path: Path) -> bool:
if not metadata_path.is_file():
return False
return re.search(re.escape(release_tag) + r"(?=\s|\(|$)", metadata_path.read_text(encoding="utf-8")) is not None

metadata_target = template_dir / "apm.pdef.xml"
if matches_release(metadata_target):
return

# Keep the old cache intact unless a download for the exact release succeeds.
metadata_url = get_xml_url(vehicle_type, firmware_version) + "apm.pdef.xml"
with TemporaryDirectory(dir=template_dir) as download_dir:
downloaded_metadata = Path(download_dir) / "apm.pdef.xml"
if not download_file_from_url(metadata_url, str(downloaded_metadata)):
raise ValueError(f"Could not prepare offline parameter metadata from {metadata_url}")
if not matches_release(downloaded_metadata):
raise ValueError(f"Downloaded parameter metadata is incompatible with {vehicle_type} {firmware_version}")
downloaded_metadata.replace(metadata_target)


def create_test_filesystem(vehicle_dir: Path, vehicle_type: str):
"""
Create LocalFilesystem and schema for testing.
Expand Down Expand Up @@ -445,6 +554,7 @@ def get_empty_template_dir(vehicle_type: str) -> Path:
if not templates_base.exists():
raise FileNotFoundError(f"Template directory not found: {templates_base}")

_prepare_template_parameter_metadata(templates_base, vehicle_type)
return templates_base


Expand Down
42 changes: 42 additions & 0 deletions tests/log_analysis/test_data_model_gnss_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,25 @@ def test_status_indication_maps_receiver_status_to_precise_finding(
assert finding.timestamp_us == finding.end_timestamp_us == 1_000_000


def test_status_indications_exclude_invalid_values_from_readable_coverage() -> None:
"""Valid status samples produce findings while healthy and invalid samples affect coverage correctly."""
series = SensorSeries(
2,
{"TimeUS": np.arange(1.0, 6.0), "GSS": np.array([1.0, 0.0, 2.5, 256.0, 3.0])},
)

outcomes = GPSLogAnalysis._status_indications(series, "ILB5", "GSS") # pylint: disable=protected-access
findings = [item for item in outcomes if item.timestamp_us is not None]
coverage = next(item for item in outcomes if item.group == "Coverage")

assert [(item.timestamp_us, _evidence(item)) for item in findings] == [
(2_000_000, {"status": 0.0}),
(5_000_000, {"status": 3.0}),
]
assert [item.severity for item in findings] == ["informational", "warning"]
assert _evidence(coverage) == {"readable": 3.0}


@pytest.mark.parametrize(
("message", "field", "threat", "previous", "current_time", "current_status", "recovered"),
[
Expand Down Expand Up @@ -366,6 +385,29 @@ def test_no_timestamped_capability_is_not_reported_as_completed_analysis() -> No
assert "No supported timestamped GNSS checks" in result.reason


def test_receiver_analysis_sorts_findings_and_reports_available_guidance() -> None:
"""Receiver findings are ordered by time and the completed assessment carries usable guidance."""
log = LogData()
_add(log, "AERG", TimeUS=[2, 1], Jam=[2, 3], Spf=[3, 2])

result = GPSLogAnalysis(log, _context()).analyse()

assert result.available is True
assert result.name == "GPS Analysis"
assert result.reason == (
"GNSS assessment complete. Receiver indications and consistency discrepancies require review; "
"sensor faults can cause similar symptoms, and undetected spoofing remains possible."
)
assert result.related_step == ""
group_order = {"Receiver indications": 0, "GNSS consistency": 1, "Correlated evidence": 2, "Coverage": 3}
actual_groups = [group_order.get(item.group or "", 4) for item in result.outcomes]
assert actual_groups == sorted(actual_groups)
indications = [item for item in result.outcomes if item.group == "Receiver indications"]
assert [(item.timestamp_us, item.source) for item in indications] == sorted(
(item.timestamp_us, item.source) for item in indications
)


def test_ublox_rise_is_receiver_specific_and_constant_levels_are_not_an_attack() -> None:
"""GIVEN interleaved receivers WHEN one indicator rises THEN only that receiver gets an RF finding."""
log = LogData()
Expand Down
Loading
Loading