switch-pico/tests/test_switch2_usb_probe_protocol_native.py
Joey Yakimowich-Payne 80b788099b feat(native-usb): add 32-frame HD gameplay rumble and USB flight diagnostics
Preserve native frequency/amplitude timelines for DualSense PCM output,
remove the unsupported 64-frame path, and retain compatibility rumble for
other controllers. Use the validated 300 MHz sampling phase and restrict
non-bondable Classic discovery autoconnect to previously paired peers.

Buffer native-hub UART stdout and release USB IRQs around port-reset
callbacks. Add observer liveness and pre-SETUP root-response observations
without changing recovery behavior. Cover transport and haptics boundaries.

Record the user-accepted 0.108 trial: controls remained responsive and
rumble felt fine. Instrumentation changes timing; the disconnect root
cause and long-term reliability remain unqualified.

Validation: 624 tests, 11 affected firmware/probe builds, and on-device
concurrent USB and HD-auto-start checks. Private captures, generated
images, and unrelated working-tree files are intentionally excluded.
2026-09-19 17:03:46 -06:00

118 lines
3.9 KiB
Python

from __future__ import annotations
import os
import shutil
import subprocess
from pathlib import Path
import pytest
@pytest.mark.parametrize(
("left", "composite", "hub", "count"),
[
(False, False, False, 1),
(True, False, False, 1),
(False, True, False, 2),
(False, False, True, 2),
(False, False, True, 4),
],
ids=["right", "left", "composite", "hub-one-pair", "hub-two-pairs"],
)
@pytest.mark.parametrize(
"imu_mode", [None, "OMIT_NATIVE_IMU", "ZERO_NATIVE_IMU_PAYLOAD"]
)
def test_switch2_usb_probe_protocol(
tmp_path: Path,
left: bool,
composite: bool,
hub: bool,
count: int,
imu_mode: str | None,
) -> None:
root = Path(__file__).resolve().parents[1]
compiler = shutil.which("cc") or shutil.which("gcc")
assert compiler is not None, "a host C compiler is required"
sdk_candidates = [
root / "build" / "_deps" / "pico_sdk-src",
root / "external" / "pico-sdk",
]
if sdk_path := os.environ.get("PICO_SDK_PATH"):
sdk_candidates.insert(0, Path(sdk_path))
mbedtls = next(
(
sdk / "lib" / "mbedtls"
for sdk in sdk_candidates
if (sdk / "lib" / "mbedtls" / "library" / "aes.c").is_file()
),
None,
)
if mbedtls is None:
pytest.skip(
"Pico SDK mbedTLS required; configure firmware or set PICO_SDK_PATH"
)
probe = root / "tools" / "switch2_usb_probe"
sides = (
[bool(instance & 1) for instance in range(count)]
if composite or hub
else [left]
)
factory_rows = []
user_rows = []
for instance, is_left in enumerate(sides):
# A and B must differ even for the same side: detect side-indexed aliases.
pair = instance // 2
factory_center = (
f"{pair * 0x20}, {9 if is_left else 8}, {0x90 if is_left else 0x80}"
)
factory_rows.append(
f"{{[0xa8] = {factory_center}, 0, 3, 0x30, 0, 4, 0x40,"
f" [8191] = {(0xE2 if is_left else 0xE1) + pair * 2}}}"
)
# L deliberately has invalid user calibration despite valid magic.
user_center = "0, 0, 0" if is_left else f"{0x10 + pair * 0x20}, 0x08, 0x81"
user_rows.append(
f"{{[0x40] = 0xb2, 0xa1, {user_center}, 0, 3, 0x30, 0, 4, 0x40,"
f" [4095] = {(0xF2 if is_left else 0xF1) + pair * 2}}}"
)
(tmp_path / "probe_memory_data.h").write_text(
'#include "model.h"\n'
"static const uint8_t probe_factory_memories[PROBE_CONTROLLER_COUNT][8192] = {\n"
+ ",\n".join(factory_rows)
+ "\n};\n"
"static const uint8_t probe_user_calibrations[PROBE_CONTROLLER_COUNT][4096] = {\n"
+ ",\n".join(user_rows)
+ "\n};\n"
)
executable = tmp_path / "switch2_usb_probe_protocol"
subprocess.run(
[
compiler,
"-std=c11",
"-Wall",
"-Wextra",
"-Werror",
"-pedantic",
f'-DMBEDTLS_CONFIG_FILE="{probe / "mbedtls_config.h"}"',
f"-DSWITCH2_PROBE_JOYCON_LEFT={int(left)}",
f"-DSWITCH2_PROBE_COMPOSITE={int(composite)}",
f"-DSWITCH2_PROBE_HUB={int(hub)}",
f"-DPROBE_CONTROLLER_COUNT={count}",
f"-DSWITCH2_PROBE_NEUTRAL_INPUT={int(hub)}",
*([f"-DSWITCH2_PROBE_{imu_mode}=1"] if imu_mode else []),
f"-I{probe}",
f"-I{root / 'src' / 'firmware'}",
f"-I{tmp_path}",
f"-I{mbedtls / 'include'}",
str(probe / "protocol.c"),
str(probe / "memory.c"),
str(mbedtls / "library" / "aes.c"),
str(mbedtls / "library" / "platform_util.c"),
str(root / "tests" / "switch2_usb_probe_protocol_test.c"),
"-o",
str(executable),
],
check=True,
cwd=root,
)
subprocess.run([str(executable)], check=True, cwd=root)