Introduce static USB output driver boundary
This commit is contained in:
parent
1837f42e21
commit
31a8cfb9d3
24 changed files with 1292 additions and 534 deletions
36
tests/native_stubs/device/usbd_pvt.h
Normal file
36
tests/native_stubs/device/usbd_pvt.h
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
#pragma once
|
||||
|
||||
#include "tusb.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
char const* name;
|
||||
void (*init)(void);
|
||||
bool (*deinit)(void);
|
||||
void (*reset)(uint8_t rhport);
|
||||
uint16_t (*open)(uint8_t rhport,
|
||||
tusb_desc_interface_t const* interface_descriptor,
|
||||
uint16_t max_length);
|
||||
bool (*control_xfer_cb)(uint8_t rhport, uint8_t stage,
|
||||
tusb_control_request_t const* request);
|
||||
bool (*xfer_cb)(uint8_t rhport, uint8_t endpoint,
|
||||
xfer_result_t result, uint32_t transferred);
|
||||
void (*sof)(uint8_t rhport, uint32_t frame_count);
|
||||
} usbd_class_driver_t;
|
||||
|
||||
usbd_class_driver_t const* usbd_app_driver_get_cb(uint8_t* driver_count);
|
||||
|
||||
bool usbd_edpt_open(uint8_t rhport,
|
||||
tusb_desc_endpoint_t const* endpoint_descriptor);
|
||||
bool usbd_edpt_xfer(uint8_t rhport, uint8_t endpoint, uint8_t* buffer,
|
||||
uint16_t total_bytes);
|
||||
bool usbd_edpt_busy(uint8_t rhport, uint8_t endpoint);
|
||||
bool usbd_edpt_claim(uint8_t rhport, uint8_t endpoint);
|
||||
bool usbd_edpt_release(uint8_t rhport, uint8_t endpoint);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
|
@ -14,6 +15,54 @@ typedef enum {
|
|||
HID_REPORT_TYPE_FEATURE = 3,
|
||||
} hid_report_type_t;
|
||||
|
||||
typedef enum {
|
||||
XFER_RESULT_SUCCESS = 0,
|
||||
XFER_RESULT_FAILED,
|
||||
} xfer_result_t;
|
||||
|
||||
enum {
|
||||
TUSB_DIR_OUT = 0,
|
||||
TUSB_DIR_IN = 1,
|
||||
TUSB_DESC_ENDPOINT = 5,
|
||||
TUSB_DESC_STRING = 3,
|
||||
};
|
||||
|
||||
#pragma pack(push, 1)
|
||||
typedef struct {
|
||||
uint8_t bLength;
|
||||
uint8_t bDescriptorType;
|
||||
uint8_t bInterfaceNumber;
|
||||
uint8_t bAlternateSetting;
|
||||
uint8_t bNumEndpoints;
|
||||
uint8_t bInterfaceClass;
|
||||
uint8_t bInterfaceSubClass;
|
||||
uint8_t bInterfaceProtocol;
|
||||
uint8_t iInterface;
|
||||
} tusb_desc_interface_t;
|
||||
|
||||
typedef struct {
|
||||
uint8_t bLength;
|
||||
uint8_t bDescriptorType;
|
||||
uint8_t bEndpointAddress;
|
||||
uint8_t bmAttributes;
|
||||
uint16_t wMaxPacketSize;
|
||||
uint8_t bInterval;
|
||||
} tusb_desc_endpoint_t;
|
||||
#pragma pack(pop)
|
||||
|
||||
static inline uint8_t const* tu_desc_next(void const* descriptor) {
|
||||
uint8_t const* bytes = (uint8_t const*)descriptor;
|
||||
return bytes + bytes[0];
|
||||
}
|
||||
|
||||
static inline uint8_t tu_desc_type(void const* descriptor) {
|
||||
return ((uint8_t const*)descriptor)[1];
|
||||
}
|
||||
|
||||
static inline uint8_t tu_edpt_dir(uint8_t endpoint) {
|
||||
return (endpoint & 0x80u) != 0 ? TUSB_DIR_IN : TUSB_DIR_OUT;
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
uint8_t bmRequestType;
|
||||
uint8_t bRequest;
|
||||
|
|
@ -27,6 +76,7 @@ bool tud_hid_n_report(uint8_t instance, uint8_t report_id,
|
|||
const void* report, uint16_t length);
|
||||
bool tud_suspended(void);
|
||||
bool tud_remote_wakeup(void);
|
||||
bool tud_ready(void);
|
||||
|
||||
uint16_t tud_hid_get_report_cb(uint8_t instance, uint8_t report_id,
|
||||
hid_report_type_t report_type, uint8_t* buffer,
|
||||
|
|
@ -39,6 +89,14 @@ void tud_hid_report_received_cb(uint8_t instance, uint8_t report_id,
|
|||
uint8_t const* tud_hid_descriptor_report_cb(uint8_t instance);
|
||||
void tud_mount_cb(void);
|
||||
void tud_umount_cb(void);
|
||||
uint8_t const* tud_descriptor_device_cb(void);
|
||||
uint8_t const* tud_descriptor_configuration_cb(uint8_t index);
|
||||
uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid);
|
||||
bool tud_vendor_control_xfer_cb(
|
||||
uint8_t rhport, uint8_t stage,
|
||||
tusb_control_request_t const* request);
|
||||
bool tud_control_request_cb(uint8_t rhport,
|
||||
tusb_control_request_t const* request);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
#include "switch_pro_driver.h"
|
||||
#include "usb_output_driver.h"
|
||||
#include "controller_color_config.h"
|
||||
#include "tusb.h"
|
||||
#include "pico/time.h"
|
||||
|
|
@ -57,8 +58,8 @@ void initialize_contexts() {
|
|||
for (uint8_t instance = 0; instance < kInstanceCount; ++instance) {
|
||||
hid_ready[instance] = true;
|
||||
hid_report_succeeds[instance] = true;
|
||||
switch_pro_init(instance);
|
||||
}
|
||||
usb_output_driver_init(AdapterUsbMode::kSwitchProbe);
|
||||
clear_sent_reports();
|
||||
}
|
||||
|
||||
|
|
@ -500,7 +501,7 @@ void test_rumble_callbacks_and_decoders_are_isolated() {
|
|||
initialize_contexts();
|
||||
rumble_events = {};
|
||||
for (uint8_t instance = 0; instance < kInstanceCount; ++instance) {
|
||||
switch_pro_set_rumble_callback(instance, rumble_callback);
|
||||
usb_output_driver_set_rumble_callback(instance, rumble_callback);
|
||||
}
|
||||
constexpr uint32_t neutral = 0x40400100u;
|
||||
auto full_payload = rumble_payload(type_2(64, 16, 64, 16), neutral);
|
||||
|
|
@ -592,7 +593,7 @@ void test_lifecycle_and_invalid_instances() {
|
|||
switch_pro_set_input(kInvalidInstance, ignored,
|
||||
SWITCH_PRO_DIGITAL_TRIGGER_THRESHOLD,
|
||||
SWITCH_PRO_DIGITAL_TRIGGER_THRESHOLD);
|
||||
switch_pro_set_rumble_callback(kInvalidInstance, rumble_callback);
|
||||
usb_output_driver_set_rumble_callback(kInvalidInstance, rumble_callback);
|
||||
expect(!switch_pro_task(kInvalidInstance),
|
||||
"invalid instance ran a driver task");
|
||||
expect(!switch_pro_is_ready(kInvalidInstance),
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@ def test_switch_pro_driver_four_contexts_native(tmp_path: Path) -> None:
|
|||
f"-I{root / 'tests' / 'native_stubs'}",
|
||||
f"-I{root}",
|
||||
str(root / "switch_pro_driver.cpp"),
|
||||
str(root / "usb_output_driver.cpp"),
|
||||
str(root / "switch_haptics.cpp"),
|
||||
str(root / "tests" / "switch_pro_driver_context_test.cpp"),
|
||||
"-o",
|
||||
|
|
|
|||
54
tests/test_usb_output_driver_native.py
Normal file
54
tests/test_usb_output_driver_native.py
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import shutil
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def host_compiler() -> str:
|
||||
compiler = shutil.which("c++") or shutil.which("g++")
|
||||
assert compiler is not None, "a host C++ compiler is required"
|
||||
return compiler
|
||||
|
||||
|
||||
def test_usb_output_driver_contracts(tmp_path: Path) -> None:
|
||||
root = Path(__file__).resolve().parents[1]
|
||||
compiler = host_compiler()
|
||||
backends = (
|
||||
("uart", []),
|
||||
("bluepad32", ["-DSWITCH_PICO_BLUEPAD32=1"]),
|
||||
)
|
||||
for backend, backend_definitions in backends:
|
||||
for instance_count in range(1, 5):
|
||||
executable = (
|
||||
tmp_path
|
||||
/ f"usb_output_driver_{backend}_{instance_count}_test"
|
||||
)
|
||||
result = subprocess.run(
|
||||
[
|
||||
compiler,
|
||||
"-std=c++17",
|
||||
"-Wall",
|
||||
"-Wextra",
|
||||
"-Werror",
|
||||
"-pedantic",
|
||||
f"-DSWITCH_PICO_HID_INSTANCE_COUNT={instance_count}",
|
||||
"-DSWITCH_PICO_ADAPTER_FEASIBILITY=1",
|
||||
*backend_definitions,
|
||||
f"-I{root / 'tests' / 'native_stubs'}",
|
||||
f"-I{root}",
|
||||
str(root / "tests" / "usb_output_driver_test.cpp"),
|
||||
str(root / "switch_pro_driver.cpp"),
|
||||
str(root / "usb_output_driver.cpp"),
|
||||
str(root / "xinput_driver.cpp"),
|
||||
str(root / "switch_haptics.cpp"),
|
||||
"-o",
|
||||
str(executable),
|
||||
],
|
||||
check=False,
|
||||
cwd=root,
|
||||
text=True,
|
||||
capture_output=True,
|
||||
)
|
||||
assert result.returncode == 0, result.stderr
|
||||
_ = subprocess.run([str(executable)], check=True, cwd=root)
|
||||
|
|
@ -1,39 +0,0 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import shutil
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def host_compiler() -> str:
|
||||
compiler = shutil.which("c++") or shutil.which("g++")
|
||||
assert compiler is not None, "a host C++ compiler is required"
|
||||
return compiler
|
||||
|
||||
|
||||
def test_xinput_feasibility_contracts(tmp_path: Path) -> None:
|
||||
root = Path(__file__).resolve().parents[1]
|
||||
compiler = host_compiler()
|
||||
for instance_count in range(1, 5):
|
||||
executable = tmp_path / f"xinput_feasibility_{instance_count}_test"
|
||||
result = subprocess.run(
|
||||
[
|
||||
compiler,
|
||||
"-std=c++17",
|
||||
"-Wall",
|
||||
"-Wextra",
|
||||
"-Werror",
|
||||
"-pedantic",
|
||||
f"-DSWITCH_PICO_HID_INSTANCE_COUNT={instance_count}",
|
||||
f"-I{root}",
|
||||
str(root / "tests" / "xinput_feasibility_test.cpp"),
|
||||
"-o",
|
||||
str(executable),
|
||||
],
|
||||
check=False,
|
||||
cwd=root,
|
||||
text=True,
|
||||
capture_output=True,
|
||||
)
|
||||
assert result.returncode == 0, result.stderr
|
||||
_ = subprocess.run([str(executable)], check=True, cwd=root)
|
||||
|
|
@ -158,10 +158,10 @@ void perform_out(UsbConfigurationManagement::Operation operation,
|
|||
tusb_control_request_t request = setup_request(
|
||||
operation, TUSB_DIR_OUT,
|
||||
static_cast<uint16_t>(next_out_payload.size()));
|
||||
require(tud_vendor_control_xfer_cb(
|
||||
require(usb_configuration_management_vendor_control(
|
||||
0, CONTROL_STAGE_SETUP, &request),
|
||||
"valid OUT setup was rejected");
|
||||
require(tud_vendor_control_xfer_cb(
|
||||
require(usb_configuration_management_vendor_control(
|
||||
0, CONTROL_STAGE_ACK, &request) == expected_ack,
|
||||
"OUT acknowledgement result was incorrect");
|
||||
}
|
||||
|
|
@ -177,7 +177,7 @@ void test_vendor_requests() {
|
|||
|
||||
tusb_control_request_t request = setup_request(
|
||||
Operation::kPairingRead, TUSB_DIR_IN, kMaximumResponseSize);
|
||||
require(tud_vendor_control_xfer_cb(
|
||||
require(usb_configuration_management_vendor_control(
|
||||
0, CONTROL_STAGE_SETUP, &request) &&
|
||||
control_payload[5] ==
|
||||
static_cast<uint8_t>(Operation::kPairingRead) &&
|
||||
|
|
@ -221,14 +221,14 @@ void test_vendor_requests() {
|
|||
request = setup_request(
|
||||
Operation::kPairingRefresh, TUSB_DIR_OUT,
|
||||
static_cast<uint16_t>(next_out_payload.size()));
|
||||
require(tud_vendor_control_xfer_cb(
|
||||
require(usb_configuration_management_vendor_control(
|
||||
0, CONTROL_STAGE_SETUP, &request) &&
|
||||
!tud_vendor_control_xfer_cb(
|
||||
!usb_configuration_management_vendor_control(
|
||||
0, CONTROL_STAGE_ACK, &request),
|
||||
"bad request CRC was accepted");
|
||||
|
||||
request.wValue = 0;
|
||||
require(!tud_vendor_control_xfer_cb(
|
||||
require(!usb_configuration_management_vendor_control(
|
||||
0, CONTROL_STAGE_SETUP, &request),
|
||||
"request with invalid magic was accepted");
|
||||
}
|
||||
|
|
@ -251,7 +251,7 @@ void test_profile_vendor_requests() {
|
|||
current_profile_list.rows[1].active_profile = 2;
|
||||
tusb_control_request_t request = setup_request(
|
||||
Operation::kProfileList, TUSB_DIR_IN, kMaximumResponseSize);
|
||||
require(tud_vendor_control_xfer_cb(
|
||||
require(usb_configuration_management_vendor_control(
|
||||
0, CONTROL_STAGE_SETUP, &request) &&
|
||||
control_payload.size() == kResponseHeaderSize + 33 &&
|
||||
control_payload[5] ==
|
||||
|
|
@ -274,7 +274,7 @@ void test_profile_vendor_requests() {
|
|||
controller_profile_default(expected_identity, 2);
|
||||
request = setup_request(
|
||||
Operation::kProfileRead, TUSB_DIR_IN, kMaximumResponseSize);
|
||||
require(tud_vendor_control_xfer_cb(
|
||||
require(usb_configuration_management_vendor_control(
|
||||
0, CONTROL_STAGE_SETUP, &request) &&
|
||||
control_payload.size() ==
|
||||
kResponseHeaderSize +
|
||||
|
|
@ -299,7 +299,7 @@ void test_profile_vendor_requests() {
|
|||
request = setup_request(
|
||||
Operation::kProfileTransactionStatus, TUSB_DIR_IN,
|
||||
kMaximumResponseSize);
|
||||
require(tud_vendor_control_xfer_cb(
|
||||
require(usb_configuration_management_vendor_control(
|
||||
0, CONTROL_STAGE_SETUP, &request) &&
|
||||
control_payload.size() == kResponseHeaderSize + 20 &&
|
||||
control_payload[6] ==
|
||||
|
|
@ -313,7 +313,7 @@ void test_profile_vendor_requests() {
|
|||
current_profile_transaction.transaction.stored_generation =
|
||||
0x11223344;
|
||||
current_profile_transaction.transaction.stored_crc = 0xaabbccdd;
|
||||
require(tud_vendor_control_xfer_cb(
|
||||
require(usb_configuration_management_vendor_control(
|
||||
0, CONTROL_STAGE_SETUP, &request) &&
|
||||
control_payload[6] == static_cast<uint8_t>(Status::kOk) &&
|
||||
read_u32(control_payload, kResponseHeaderSize) ==
|
||||
|
|
@ -391,7 +391,7 @@ void test_profile_vendor_requests() {
|
|||
perform_out(Operation::kProfileActivate, mutation, false);
|
||||
request = setup_request(
|
||||
Operation::kProfileReset, TUSB_DIR_OUT, kRequestHeaderSize + 15);
|
||||
require(!tud_vendor_control_xfer_cb(
|
||||
require(!usb_configuration_management_vendor_control(
|
||||
0, CONTROL_STAGE_SETUP, &request),
|
||||
"legacy profile reset payload was accepted");
|
||||
|
||||
|
|
@ -400,7 +400,7 @@ void test_profile_vendor_requests() {
|
|||
request = setup_request(
|
||||
Operation::kProfileSelect, TUSB_DIR_OUT,
|
||||
kRequestHeaderSize + 14);
|
||||
require(!tud_vendor_control_xfer_cb(
|
||||
require(!usb_configuration_management_vendor_control(
|
||||
0, CONTROL_STAGE_SETUP, &request),
|
||||
"short profile selection request was accepted");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -36,9 +36,6 @@ bool tud_control_xfer(uint8_t rhport,
|
|||
void* buffer, uint16_t length);
|
||||
bool tud_control_status(uint8_t rhport,
|
||||
const tusb_control_request_t* request);
|
||||
bool tud_vendor_control_xfer_cb(
|
||||
uint8_t rhport, uint8_t stage,
|
||||
const tusb_control_request_t* request);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
|
|||
677
tests/usb_output_driver_test.cpp
Normal file
677
tests/usb_output_driver_test.cpp
Normal file
|
|
@ -0,0 +1,677 @@
|
|||
#include <array>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
|
||||
#include "adapter_host_probe_state.h"
|
||||
#include "xinput_descriptors.h"
|
||||
#include "xinput_protocol.h"
|
||||
#include "device/usbd_pvt.h"
|
||||
#include "pico/time.h"
|
||||
#include "switch_pro_driver.h"
|
||||
#include "tusb.h"
|
||||
#include "usb_output_driver.h"
|
||||
#include "xinput_driver.h"
|
||||
|
||||
namespace {
|
||||
|
||||
int failures = 0;
|
||||
|
||||
constexpr uint8_t kInstanceCount = SWITCH_PICO_HID_INSTANCE_COUNT;
|
||||
constexpr uint8_t kInvalidInstance = kInstanceCount;
|
||||
|
||||
struct EndpointHarness {
|
||||
bool opened = false;
|
||||
bool busy = false;
|
||||
bool claimed = false;
|
||||
uint8_t* armed_buffer = nullptr;
|
||||
uint16_t armed_length = 0;
|
||||
std::array<uint8_t, 64> last_transfer{};
|
||||
uint16_t last_transfer_length = 0;
|
||||
unsigned transfer_count = 0;
|
||||
};
|
||||
|
||||
struct RumbleEvent {
|
||||
unsigned count = 0;
|
||||
ControllerRumbleOutput output{};
|
||||
};
|
||||
|
||||
std::array<EndpointHarness, 256> endpoint_harness{};
|
||||
std::array<RumbleEvent, kInstanceCount> xinput_rumble_events{};
|
||||
uint64_t now_ms = 0;
|
||||
uint32_t random_value = 1;
|
||||
bool usb_ready = true;
|
||||
unsigned hid_report_count = 0;
|
||||
unsigned switch_inactive_rumble_count = 0;
|
||||
unsigned observed_string_count = 0;
|
||||
uint8_t last_observed_string = 0;
|
||||
|
||||
#ifdef SWITCH_PICO_BLUEPAD32
|
||||
bool management_vendor_control_result = false;
|
||||
unsigned management_vendor_control_count = 0;
|
||||
uint8_t management_vendor_control_rhport = 0;
|
||||
uint8_t management_vendor_control_stage = 0;
|
||||
const tusb_control_request_t* management_vendor_control_request = nullptr;
|
||||
#endif
|
||||
|
||||
void expect(bool condition, const char *message) {
|
||||
if (!condition) {
|
||||
std::cerr << "FAIL: " << message << '\n';
|
||||
++failures;
|
||||
}
|
||||
}
|
||||
|
||||
uint16_t read_le16(const uint8_t *data) {
|
||||
return static_cast<uint16_t>(data[0] | (data[1] << 8));
|
||||
}
|
||||
|
||||
uint32_t read_le32(const uint8_t *data) {
|
||||
return static_cast<uint32_t>(data[0]) |
|
||||
(static_cast<uint32_t>(data[1]) << 8) |
|
||||
(static_cast<uint32_t>(data[2]) << 16) |
|
||||
(static_cast<uint32_t>(data[3]) << 24);
|
||||
}
|
||||
|
||||
void test_device_and_configuration_descriptors() {
|
||||
using namespace XInput;
|
||||
expect(read_le16(&kSwitchProbeDeviceDescriptor[8]) ==
|
||||
kSwitchProbeVendorId,
|
||||
"Switch probe VID mismatch");
|
||||
expect(read_le16(&kSwitchProbeDeviceDescriptor[10]) ==
|
||||
kSwitchProbeProductId,
|
||||
"Switch probe PID mismatch");
|
||||
expect(read_le16(&kSwitchProbeDeviceDescriptor[12]) ==
|
||||
kSwitchProbeDeviceRevision,
|
||||
"Switch probe revision mismatch");
|
||||
expect(kSwitchProbeDeviceRevision != 0x0210,
|
||||
"Switch probe reuses the genuine controller cache identity");
|
||||
expect(read_le16(&kDeviceDescriptor[8]) == kDevelopmentVendorId,
|
||||
"development VID mismatch");
|
||||
expect(read_le16(&kDeviceDescriptor[10]) == kDevelopmentProductId,
|
||||
"development PID mismatch");
|
||||
expect(read_le16(&kDeviceDescriptor[12]) ==
|
||||
kDevelopmentDeviceRevision,
|
||||
"development revision mismatch");
|
||||
expect(kDeviceDescriptor[4] == 0 && kDeviceDescriptor[5] == 0 &&
|
||||
kDeviceDescriptor[6] == 0,
|
||||
"multi-interface development device is not composite");
|
||||
expect(kDevelopmentVendorId != 0x045e,
|
||||
"development device must not impersonate Microsoft's VID");
|
||||
expect(read_le16(&kConfigurationDescriptor[2]) ==
|
||||
sizeof(kConfigurationDescriptor),
|
||||
"configuration total length mismatch");
|
||||
expect(kConfigurationDescriptor[4] == SWITCH_PICO_HID_INSTANCE_COUNT,
|
||||
"configuration interface count mismatch");
|
||||
|
||||
std::array<bool, 16> endpoints{};
|
||||
for (uint8_t instance = 0; instance < SWITCH_PICO_HID_INSTANCE_COUNT;
|
||||
++instance) {
|
||||
const size_t offset = 9 + instance * kInterfaceDescriptorSize;
|
||||
const uint8_t *interface = &kConfigurationDescriptor[offset];
|
||||
expect(interface[0] == 9 && interface[1] == 4,
|
||||
"missing interface descriptor");
|
||||
expect(interface[2] == instance, "interface number mismatch");
|
||||
expect(interface[5] == 0xff && interface[6] == 0x5d &&
|
||||
interface[7] == 0x01,
|
||||
"XInput interface class tuple mismatch");
|
||||
expect(interface[9] == 0x10 && interface[10] == 0x21,
|
||||
"XInput capability descriptor missing");
|
||||
const uint8_t in_endpoint = interface[27];
|
||||
const uint8_t out_endpoint = interface[34];
|
||||
expect(in_endpoint == static_cast<uint8_t>(0x81 + instance),
|
||||
"input endpoint mismatch");
|
||||
expect(out_endpoint == static_cast<uint8_t>(0x01 + instance),
|
||||
"output endpoint mismatch");
|
||||
expect(interface[15] == in_endpoint && interface[21] == out_endpoint,
|
||||
"capability descriptor endpoint mismatch");
|
||||
expect(!endpoints[in_endpoint & 0x0f] &&
|
||||
!endpoints[out_endpoint & 0x0f],
|
||||
"endpoint number reused");
|
||||
endpoints[in_endpoint & 0x0f] = true;
|
||||
}
|
||||
}
|
||||
|
||||
void test_microsoft_compatible_id_descriptor() {
|
||||
using namespace XInput;
|
||||
expect(read_le32(kMsCompatIdDescriptor) == sizeof(kMsCompatIdDescriptor),
|
||||
"Microsoft descriptor total length mismatch");
|
||||
expect(read_le16(&kMsCompatIdDescriptor[4]) == 0x0100,
|
||||
"Microsoft descriptor version mismatch");
|
||||
expect(read_le16(&kMsCompatIdDescriptor[6]) == kMsCompatIdIndex,
|
||||
"Microsoft descriptor index mismatch");
|
||||
expect(kMsCompatIdDescriptor[8] == SWITCH_PICO_HID_INSTANCE_COUNT,
|
||||
"Microsoft function count mismatch");
|
||||
for (uint8_t instance = 0;
|
||||
instance < SWITCH_PICO_HID_INSTANCE_COUNT; ++instance) {
|
||||
const uint8_t *function = &kMsCompatIdDescriptor[16 + instance * 24];
|
||||
expect(function[0] == instance,
|
||||
"Microsoft descriptor interface mismatch");
|
||||
expect(std::memcmp(&function[2], "XUSB10", 6) == 0,
|
||||
"XUSB10 compatible ID missing");
|
||||
}
|
||||
expect(read_le32(kProbeMsCompatIdDescriptor) == 16 &&
|
||||
kProbeMsCompatIdDescriptor[8] == 0,
|
||||
"probe descriptor must expose no compatible functions");
|
||||
}
|
||||
|
||||
void test_input_report_mapping() {
|
||||
ControllerState state{};
|
||||
auto report = XInput::build_input_report(state);
|
||||
expect(report.report_id == 0 && report.report_size == 20,
|
||||
"neutral report header mismatch");
|
||||
expect(report.buttons == 0 && report.left_trigger == 0 &&
|
||||
report.right_trigger == 0,
|
||||
"neutral report controls mismatch");
|
||||
expect(report.left_x == 0 && report.left_y == 0 && report.right_x == 0 &&
|
||||
report.right_y == 0,
|
||||
"neutral axes mismatch");
|
||||
|
||||
state.dpad_up = true;
|
||||
state.button_south = true;
|
||||
state.button_east = true;
|
||||
state.button_west = true;
|
||||
state.button_north = true;
|
||||
state.button_start = true;
|
||||
state.button_select = true;
|
||||
state.button_system = true;
|
||||
state.left_trigger = UINT16_MAX;
|
||||
state.right_trigger = UINT16_MAX;
|
||||
state.left_stick_x = INT16_MIN;
|
||||
state.left_stick_y = INT16_MIN;
|
||||
state.right_stick_x = INT16_MAX;
|
||||
state.right_stick_y = INT16_MAX;
|
||||
report = XInput::build_input_report(state);
|
||||
expect((report.buttons & XInput::kDpadUp) != 0,
|
||||
"D-pad mapping missing");
|
||||
expect((report.buttons & XInput::kButtonA) != 0 &&
|
||||
(report.buttons & XInput::kButtonB) != 0 &&
|
||||
(report.buttons & XInput::kButtonX) != 0 &&
|
||||
(report.buttons & XInput::kButtonY) != 0,
|
||||
"positional face-button mapping mismatch");
|
||||
expect(report.left_trigger == 0xff && report.right_trigger == 0xff,
|
||||
"full analog trigger mapping mismatch");
|
||||
expect(report.left_x == INT16_MIN && report.left_y == INT16_MAX &&
|
||||
report.right_x == INT16_MAX &&
|
||||
report.right_y == -INT16_MAX,
|
||||
"axis endpoint mapping mismatch");
|
||||
|
||||
state.left_trigger = 0x8000;
|
||||
state.right_trigger = 0x7fff;
|
||||
report = XInput::build_input_report(state);
|
||||
expect(report.left_trigger == 0x80 && report.right_trigger == 0x7f,
|
||||
"analog trigger precision was discarded");
|
||||
}
|
||||
|
||||
void test_rumble_report() {
|
||||
const uint8_t packet[8] = {0x00, 0x08, 0x00, 0xa5, 0x5a, 0x00, 0x00, 0x00};
|
||||
ControllerRumbleOutput output{};
|
||||
expect(XInput::parse_rumble_report(packet, sizeof(packet), &output),
|
||||
"valid rumble report rejected");
|
||||
expect(output.low_frequency_magnitude == 0xa5 &&
|
||||
output.high_frequency_magnitude == 0x5a,
|
||||
"rumble magnitudes mapped incorrectly");
|
||||
expect(!XInput::parse_rumble_report(packet, 4, &output),
|
||||
"truncated rumble report accepted");
|
||||
uint8_t wrong_type[8]{};
|
||||
expect(!XInput::parse_rumble_report(wrong_type, sizeof(wrong_type),
|
||||
&output),
|
||||
"wrong rumble report type accepted");
|
||||
}
|
||||
|
||||
void test_host_probe_sequence() {
|
||||
AdapterHostProbeState state;
|
||||
state.note_ms_compat_id_request(10);
|
||||
expect(!state.windows_confirmed(),
|
||||
"compatible-ID request without signature confirmed Windows");
|
||||
state.note_ms_os_string();
|
||||
expect(state.saw_ms_os_string(),
|
||||
"Microsoft OS string observation was not retained");
|
||||
state.note_ms_compat_id_request(20);
|
||||
expect(state.windows_confirmed(),
|
||||
"two-stage Windows signature not confirmed");
|
||||
expect(!state.should_reboot(119), "probe rebooted before delay");
|
||||
expect(state.should_reboot(120), "probe did not reboot at deadline");
|
||||
|
||||
AdapterHostProbeState wrapped;
|
||||
wrapped.note_ms_os_string();
|
||||
wrapped.note_ms_compat_id_request(UINT32_MAX - 50);
|
||||
expect(!wrapped.should_reboot(48),
|
||||
"wrapped timer rebooted before deadline");
|
||||
expect(wrapped.should_reboot(49), "wrapped timer missed deadline");
|
||||
}
|
||||
|
||||
void reset_usb_harness() {
|
||||
endpoint_harness = {};
|
||||
xinput_rumble_events = {};
|
||||
now_ms = 0;
|
||||
usb_ready = true;
|
||||
hid_report_count = 0;
|
||||
switch_inactive_rumble_count = 0;
|
||||
observed_string_count = 0;
|
||||
last_observed_string = 0;
|
||||
}
|
||||
|
||||
void xinput_rumble_callback(uint8_t instance,
|
||||
const ControllerRumbleOutput& output) {
|
||||
expect(instance < xinput_rumble_events.size(),
|
||||
"XInput rumble used an invalid instance");
|
||||
if (instance < xinput_rumble_events.size()) {
|
||||
++xinput_rumble_events[instance].count;
|
||||
xinput_rumble_events[instance].output = output;
|
||||
}
|
||||
}
|
||||
|
||||
void inactive_switch_rumble_callback(
|
||||
uint8_t instance, const ControllerRumbleOutput& output) {
|
||||
(void)instance;
|
||||
(void)output;
|
||||
++switch_inactive_rumble_count;
|
||||
}
|
||||
|
||||
void expect_usb_string(uint8_t index, const char* expected,
|
||||
const char* message) {
|
||||
const uint16_t* descriptor = tud_descriptor_string_cb(index, 0x0409);
|
||||
const size_t length = std::strlen(expected);
|
||||
bool matches = descriptor != nullptr &&
|
||||
(descriptor[0] & 0xffu) == 2u * length + 2u &&
|
||||
(descriptor[0] >> 8u) == TUSB_DESC_STRING;
|
||||
if (matches) {
|
||||
for (size_t i = 0; i < length; ++i) {
|
||||
if (descriptor[i + 1] !=
|
||||
static_cast<uint8_t>(expected[i])) {
|
||||
matches = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
expect(matches, message);
|
||||
}
|
||||
|
||||
void test_switch_boundary_dispatch() {
|
||||
reset_usb_harness();
|
||||
usb_output_driver_init(AdapterUsbMode::kSwitchProbe);
|
||||
expect(usb_output_driver_mode() == AdapterUsbMode::kSwitchProbe &&
|
||||
std::strcmp(usb_output_driver_name(), "SWITCH") == 0 &&
|
||||
std::strcmp(usb_output_driver_mode_name(),
|
||||
"Switch probe") == 0,
|
||||
"Switch boundary mode was not frozen");
|
||||
expect(std::memcmp(tud_descriptor_device_cb(),
|
||||
XInput::kSwitchProbeDeviceDescriptor,
|
||||
sizeof(XInput::kSwitchProbeDeviceDescriptor)) == 0,
|
||||
"Switch probe device descriptor changed at the boundary");
|
||||
expect(std::memcmp(tud_descriptor_configuration_cb(0),
|
||||
switch_pro_configuration_descriptor,
|
||||
sizeof(switch_pro_configuration_descriptor)) == 0,
|
||||
"Switch configuration descriptor changed at the boundary");
|
||||
const uint8_t* hid_descriptor = tud_hid_descriptor_report_cb(0);
|
||||
expect(hid_descriptor != nullptr &&
|
||||
std::memcmp(hid_descriptor, switch_pro_report_descriptor,
|
||||
sizeof(switch_pro_report_descriptor)) == 0,
|
||||
"Switch HID report descriptor changed at the boundary");
|
||||
expect(tud_hid_descriptor_report_cb(kInvalidInstance) == nullptr,
|
||||
"Switch HID callback accepted an invalid instance");
|
||||
|
||||
std::array<uint8_t, SWITCH_PRO_ENDPOINT_SIZE> report{};
|
||||
expect(tud_hid_get_report_cb(0, 0, HID_REPORT_TYPE_INPUT,
|
||||
report.data(), report.size()) ==
|
||||
sizeof(SwitchProReport),
|
||||
"Switch GET_REPORT was not dispatched");
|
||||
expect(tud_hid_get_report_cb(kInvalidInstance, 0,
|
||||
HID_REPORT_TYPE_INPUT, report.data(),
|
||||
report.size()) == 0,
|
||||
"Switch GET_REPORT accepted an invalid instance");
|
||||
|
||||
uint8_t driver_count = 0xff;
|
||||
expect(usbd_app_driver_get_cb(&driver_count) == nullptr &&
|
||||
driver_count == 0,
|
||||
"Switch mode registered the XInput custom class");
|
||||
expect(usbd_app_driver_get_cb(nullptr) == nullptr,
|
||||
"custom class callback accepted a null count");
|
||||
|
||||
ControllerState switch_state{};
|
||||
switch_state.button_south = true;
|
||||
usb_output_driver_set_input(0, switch_state,
|
||||
SWITCH_PRO_DIGITAL_TRIGGER_THRESHOLD,
|
||||
SWITCH_PRO_DIGITAL_TRIGGER_THRESHOLD);
|
||||
now_ms = 15;
|
||||
expect(usb_output_driver_task(0) && hid_report_count == 1,
|
||||
"Switch input task was not dispatched");
|
||||
expect(tud_hid_get_report_cb(0, 0, HID_REPORT_TYPE_INPUT,
|
||||
report.data(), report.size()) ==
|
||||
sizeof(SwitchProReport),
|
||||
"dispatched Switch report was unavailable");
|
||||
SwitchProReport switch_report{};
|
||||
std::memcpy(&switch_report, report.data(), sizeof(switch_report));
|
||||
expect(switch_report.inputs.buttonB,
|
||||
"Switch boundary did not apply input state");
|
||||
hid_report_count = 0;
|
||||
|
||||
expect(usb_output_driver_is_ready(0),
|
||||
"Switch context was not ready after initialization");
|
||||
tud_mount_cb();
|
||||
expect(!usb_output_driver_is_ready(0),
|
||||
"Switch mount did not reset handshake readiness");
|
||||
expect(!usb_output_driver_task(0) && hid_report_count == 1,
|
||||
"Switch startup identify was not dispatched");
|
||||
tud_umount_cb();
|
||||
expect(!usb_output_driver_is_ready(0),
|
||||
"Switch unmount did not reset handshake readiness");
|
||||
expect(!usb_output_driver_task(kInvalidInstance),
|
||||
"Switch task accepted an invalid instance");
|
||||
|
||||
const uint16_t* os_string = tud_descriptor_string_cb(0xee, 0x0409);
|
||||
expect(os_string != nullptr && os_string[0] == 0x0312 &&
|
||||
os_string[1] == 'M' && os_string[2] == 'S' &&
|
||||
os_string[3] == 'F' && os_string[4] == 'T' &&
|
||||
os_string[5] == '1' && os_string[6] == '0' &&
|
||||
os_string[7] == '0' &&
|
||||
os_string[8] == XInput::kMsVendorRequest,
|
||||
"Microsoft OS string changed at the boundary");
|
||||
expect(observed_string_count == 1 && last_observed_string == 0xee,
|
||||
"host probe did not observe the Microsoft OS string");
|
||||
expect(!tud_control_request_cb(0, nullptr),
|
||||
"generic control routing claimed an unhandled request");
|
||||
}
|
||||
|
||||
void open_xinput_interfaces(usbd_class_driver_t const* driver) {
|
||||
endpoint_harness = {};
|
||||
for (uint8_t instance = 0; instance < kInstanceCount; ++instance) {
|
||||
auto const* interface_descriptor =
|
||||
reinterpret_cast<tusb_desc_interface_t const*>(
|
||||
&XInput::kConfigurationDescriptor[
|
||||
9 + instance * XInput::kInterfaceDescriptorSize]);
|
||||
expect(driver->open(0, interface_descriptor,
|
||||
XInput::kInterfaceDescriptorSize) ==
|
||||
XInput::kInterfaceDescriptorSize,
|
||||
"XInput interface did not open");
|
||||
expect(endpoint_harness[static_cast<uint8_t>(0x81 + instance)]
|
||||
.opened &&
|
||||
endpoint_harness[static_cast<uint8_t>(0x01 + instance)]
|
||||
.opened,
|
||||
"XInput interface endpoints were not opened");
|
||||
}
|
||||
}
|
||||
|
||||
void test_xinput_boundary_dispatch() {
|
||||
reset_usb_harness();
|
||||
usb_output_driver_init(AdapterUsbMode::kXInput);
|
||||
|
||||
expect(usb_output_driver_mode() == AdapterUsbMode::kXInput &&
|
||||
std::strcmp(usb_output_driver_name(), "XINPUT") == 0 &&
|
||||
std::strcmp(usb_output_driver_mode_name(), "XInput") == 0,
|
||||
"XInput boundary mode was not frozen");
|
||||
expect(std::memcmp(tud_descriptor_device_cb(),
|
||||
XInput::kDeviceDescriptor,
|
||||
sizeof(XInput::kDeviceDescriptor)) == 0,
|
||||
"XInput device descriptor changed at the boundary");
|
||||
expect(std::memcmp(tud_descriptor_configuration_cb(0),
|
||||
XInput::kConfigurationDescriptor,
|
||||
sizeof(XInput::kConfigurationDescriptor)) == 0,
|
||||
"XInput configuration descriptor changed at the boundary");
|
||||
expect(tud_hid_descriptor_report_cb(0) == nullptr,
|
||||
"inactive HID class claimed an XInput report descriptor");
|
||||
std::array<uint8_t, SWITCH_PRO_ENDPOINT_SIZE> hid_report{};
|
||||
expect(tud_hid_get_report_cb(0, 0, HID_REPORT_TYPE_INPUT,
|
||||
hid_report.data(), hid_report.size()) == 0,
|
||||
"inactive HID GET_REPORT handled XInput mode");
|
||||
|
||||
uint8_t driver_count = 0;
|
||||
usbd_class_driver_t const* driver =
|
||||
usbd_app_driver_get_cb(&driver_count);
|
||||
expect(driver == xinput_class_driver() && driver_count == 1,
|
||||
"XInput custom class was not selected");
|
||||
expect(driver != nullptr && std::strcmp(driver->name, "XINPUT") == 0,
|
||||
"XInput custom class retained a feasibility name");
|
||||
if (driver == nullptr) {
|
||||
return;
|
||||
}
|
||||
driver->init();
|
||||
open_xinput_interfaces(driver);
|
||||
|
||||
for (uint8_t instance = 0; instance < kInstanceCount; ++instance) {
|
||||
usb_output_driver_set_rumble_callback(
|
||||
instance, xinput_rumble_callback);
|
||||
ControllerState state{};
|
||||
state.button_south = (instance & 1u) == 0;
|
||||
state.button_north = (instance & 1u) != 0;
|
||||
state.left_trigger =
|
||||
static_cast<uint16_t>(0x1000u * (instance + 1u));
|
||||
state.right_trigger =
|
||||
static_cast<uint16_t>(0x0800u * (instance + 1u));
|
||||
state.left_stick_x = static_cast<int16_t>(100 + instance);
|
||||
state.right_stick_y = static_cast<int16_t>(-200 - instance);
|
||||
usb_output_driver_set_input(instance, state, 1, 2);
|
||||
expect(usb_output_driver_is_ready(instance),
|
||||
"configured XInput instance was not ready");
|
||||
expect(usb_output_driver_task(instance),
|
||||
"XInput input report was not dispatched");
|
||||
|
||||
const EndpointHarness& input_endpoint =
|
||||
endpoint_harness[static_cast<uint8_t>(0x81 + instance)];
|
||||
const XInput::InputReport expected =
|
||||
XInput::build_input_report(state);
|
||||
expect(input_endpoint.last_transfer_length == sizeof(expected) &&
|
||||
std::memcmp(input_endpoint.last_transfer.data(),
|
||||
&expected, sizeof(expected)) == 0,
|
||||
"XInput boundary changed an input report");
|
||||
}
|
||||
expect(!usb_output_driver_task(kInvalidInstance),
|
||||
"XInput task accepted an invalid instance");
|
||||
|
||||
switch_pro_set_rumble_callback(0,
|
||||
inactive_switch_rumble_callback);
|
||||
std::array<uint8_t, 10> switch_output{};
|
||||
switch_output[0] = REPORT_OUTPUT_10;
|
||||
tud_hid_report_received_cb(0, 0, switch_output.data(),
|
||||
switch_output.size());
|
||||
tud_hid_set_report_cb(0, REPORT_OUTPUT_10, HID_REPORT_TYPE_OUTPUT,
|
||||
switch_output.data() + 1,
|
||||
switch_output.size() - 1);
|
||||
expect(switch_inactive_rumble_count == 0,
|
||||
"inactive Switch HID callbacks handled XInput output");
|
||||
|
||||
tud_mount_cb();
|
||||
tud_umount_cb();
|
||||
for (uint8_t instance = 0; instance < kInstanceCount; ++instance) {
|
||||
expect(usb_output_driver_is_ready(instance),
|
||||
"generic mount callback reset the active XInput class");
|
||||
}
|
||||
|
||||
driver->reset(0);
|
||||
for (uint8_t instance = 0; instance < kInstanceCount; ++instance) {
|
||||
expect(!usb_output_driver_is_ready(instance),
|
||||
"XInput bus reset retained configured state");
|
||||
}
|
||||
open_xinput_interfaces(driver);
|
||||
|
||||
for (uint8_t instance = 0; instance < kInstanceCount; ++instance) {
|
||||
EndpointHarness& output_endpoint =
|
||||
endpoint_harness[static_cast<uint8_t>(0x01 + instance)];
|
||||
expect(output_endpoint.armed_buffer != nullptr &&
|
||||
output_endpoint.armed_length == 32,
|
||||
"XInput output endpoint was not armed");
|
||||
if (output_endpoint.armed_buffer == nullptr) {
|
||||
continue;
|
||||
}
|
||||
std::memset(output_endpoint.armed_buffer, 0,
|
||||
output_endpoint.armed_length);
|
||||
output_endpoint.armed_buffer[0] = 0x00;
|
||||
output_endpoint.armed_buffer[1] = 0x08;
|
||||
output_endpoint.armed_buffer[3] =
|
||||
static_cast<uint8_t>(0x20 + instance);
|
||||
output_endpoint.armed_buffer[4] =
|
||||
static_cast<uint8_t>(0x40 + instance);
|
||||
expect(driver->xfer_cb(
|
||||
0, static_cast<uint8_t>(0x01 + instance),
|
||||
XFER_RESULT_SUCCESS, 8),
|
||||
"XInput output transfer was not rearmed");
|
||||
}
|
||||
for (uint8_t instance = 0; instance < kInstanceCount; ++instance) {
|
||||
expect(xinput_rumble_events[instance].count == 1 &&
|
||||
xinput_rumble_events[instance]
|
||||
.output.low_frequency_magnitude ==
|
||||
static_cast<uint8_t>(0x20 + instance) &&
|
||||
xinput_rumble_events[instance]
|
||||
.output.high_frequency_magnitude ==
|
||||
static_cast<uint8_t>(0x40 + instance),
|
||||
"XInput rumble crossed instance boundaries");
|
||||
}
|
||||
|
||||
expect_usb_string(1, "Switch Pico",
|
||||
"XInput manufacturer string changed");
|
||||
expect_usb_string(2, "XInput Feasibility",
|
||||
"XInput development product string changed");
|
||||
expect_usb_string(3, "XINPUT-PROTOTYPE",
|
||||
"XInput development serial string changed");
|
||||
}
|
||||
|
||||
void test_vendor_control_boundary() {
|
||||
constexpr uint8_t kSetupStage = 0;
|
||||
constexpr uint8_t kRhport = 2;
|
||||
tusb_control_request_t request{};
|
||||
request.bRequest = 0x42;
|
||||
|
||||
#ifdef SWITCH_PICO_BLUEPAD32
|
||||
management_vendor_control_result = true;
|
||||
management_vendor_control_count = 0;
|
||||
expect(tud_vendor_control_xfer_cb(kRhport, kSetupStage, &request),
|
||||
"BLUEPAD32 vendor control was not forwarded");
|
||||
expect(management_vendor_control_count == 1 &&
|
||||
management_vendor_control_rhport == kRhport &&
|
||||
management_vendor_control_stage == kSetupStage &&
|
||||
management_vendor_control_request == &request,
|
||||
"BLUEPAD32 vendor control forwarding changed its arguments");
|
||||
|
||||
management_vendor_control_result = false;
|
||||
expect(!tud_vendor_control_xfer_cb(kRhport, kSetupStage, &request) &&
|
||||
management_vendor_control_count == 2,
|
||||
"inactive BLUEPAD32 vendor control was claimed");
|
||||
#else
|
||||
expect(!tud_vendor_control_xfer_cb(kRhport, kSetupStage, &request),
|
||||
"UART vendor control was claimed");
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
extern "C" absolute_time_t get_absolute_time(void) {
|
||||
return {now_ms};
|
||||
}
|
||||
|
||||
extern "C" uint32_t to_ms_since_boot(absolute_time_t time) {
|
||||
return static_cast<uint32_t>(time.milliseconds);
|
||||
}
|
||||
|
||||
extern "C" uint32_t get_rand_32(void) {
|
||||
return random_value++;
|
||||
}
|
||||
|
||||
extern "C" bool tud_hid_n_ready(uint8_t instance) {
|
||||
return instance < kInstanceCount;
|
||||
}
|
||||
|
||||
extern "C" bool tud_hid_n_report(uint8_t instance, uint8_t report_id,
|
||||
const void* report, uint16_t length) {
|
||||
(void)report_id;
|
||||
if (instance >= kInstanceCount || report == nullptr || length == 0) {
|
||||
return false;
|
||||
}
|
||||
++hid_report_count;
|
||||
return true;
|
||||
}
|
||||
|
||||
extern "C" bool tud_suspended(void) {
|
||||
return false;
|
||||
}
|
||||
|
||||
extern "C" bool tud_remote_wakeup(void) {
|
||||
return true;
|
||||
}
|
||||
|
||||
extern "C" bool tud_ready(void) {
|
||||
return usb_ready;
|
||||
}
|
||||
|
||||
extern "C" bool usbd_edpt_open(
|
||||
uint8_t rhport, tusb_desc_endpoint_t const* endpoint_descriptor) {
|
||||
(void)rhport;
|
||||
if (endpoint_descriptor == nullptr) {
|
||||
return false;
|
||||
}
|
||||
endpoint_harness[endpoint_descriptor->bEndpointAddress].opened = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
extern "C" bool usbd_edpt_xfer(uint8_t rhport, uint8_t endpoint,
|
||||
uint8_t* buffer, uint16_t total_bytes) {
|
||||
(void)rhport;
|
||||
if (buffer == nullptr) {
|
||||
return false;
|
||||
}
|
||||
EndpointHarness& harness = endpoint_harness[endpoint];
|
||||
++harness.transfer_count;
|
||||
harness.claimed = false;
|
||||
if (tu_edpt_dir(endpoint) == TUSB_DIR_IN) {
|
||||
harness.last_transfer_length =
|
||||
total_bytes < harness.last_transfer.size()
|
||||
? total_bytes
|
||||
: static_cast<uint16_t>(harness.last_transfer.size());
|
||||
std::memcpy(harness.last_transfer.data(), buffer,
|
||||
harness.last_transfer_length);
|
||||
} else {
|
||||
harness.armed_buffer = buffer;
|
||||
harness.armed_length = total_bytes;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
extern "C" bool usbd_edpt_busy(uint8_t rhport, uint8_t endpoint) {
|
||||
(void)rhport;
|
||||
return endpoint_harness[endpoint].busy;
|
||||
}
|
||||
|
||||
extern "C" bool usbd_edpt_claim(uint8_t rhport, uint8_t endpoint) {
|
||||
(void)rhport;
|
||||
EndpointHarness& harness = endpoint_harness[endpoint];
|
||||
if (harness.claimed) {
|
||||
return false;
|
||||
}
|
||||
harness.claimed = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
extern "C" bool usbd_edpt_release(uint8_t rhport, uint8_t endpoint) {
|
||||
(void)rhport;
|
||||
endpoint_harness[endpoint].claimed = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
void adapter_host_probe_note_string_descriptor(uint8_t index) {
|
||||
++observed_string_count;
|
||||
last_observed_string = index;
|
||||
}
|
||||
|
||||
#ifdef SWITCH_PICO_BLUEPAD32
|
||||
bool usb_configuration_management_vendor_control(
|
||||
uint8_t rhport, uint8_t stage,
|
||||
tusb_control_request_t const* request) {
|
||||
++management_vendor_control_count;
|
||||
management_vendor_control_rhport = rhport;
|
||||
management_vendor_control_stage = stage;
|
||||
management_vendor_control_request = request;
|
||||
return management_vendor_control_result;
|
||||
}
|
||||
#endif
|
||||
|
||||
int main() {
|
||||
test_device_and_configuration_descriptors();
|
||||
test_microsoft_compatible_id_descriptor();
|
||||
test_input_report_mapping();
|
||||
test_rumble_report();
|
||||
test_host_probe_sequence();
|
||||
test_switch_boundary_dispatch();
|
||||
test_xinput_boundary_dispatch();
|
||||
test_vendor_control_boundary();
|
||||
return failures == 0 ? 0 : 1;
|
||||
}
|
||||
|
|
@ -1,211 +0,0 @@
|
|||
#include <array>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
|
||||
#include "adapter_host_probe_state.h"
|
||||
#include "xinput_feasibility_descriptors.h"
|
||||
#include "xinput_feasibility_protocol.h"
|
||||
|
||||
namespace {
|
||||
|
||||
int failures = 0;
|
||||
|
||||
void expect(bool condition, const char *message) {
|
||||
if (!condition) {
|
||||
std::cerr << "FAIL: " << message << '\n';
|
||||
++failures;
|
||||
}
|
||||
}
|
||||
|
||||
uint16_t read_le16(const uint8_t *data) {
|
||||
return static_cast<uint16_t>(data[0] | (data[1] << 8));
|
||||
}
|
||||
|
||||
uint32_t read_le32(const uint8_t *data) {
|
||||
return static_cast<uint32_t>(data[0]) |
|
||||
(static_cast<uint32_t>(data[1]) << 8) |
|
||||
(static_cast<uint32_t>(data[2]) << 16) |
|
||||
(static_cast<uint32_t>(data[3]) << 24);
|
||||
}
|
||||
|
||||
void test_device_and_configuration_descriptors() {
|
||||
using namespace XInputFeasibility;
|
||||
expect(read_le16(&kSwitchProbeDeviceDescriptor[8]) ==
|
||||
kSwitchProbeVendorId,
|
||||
"Switch probe VID mismatch");
|
||||
expect(read_le16(&kSwitchProbeDeviceDescriptor[10]) ==
|
||||
kSwitchProbeProductId,
|
||||
"Switch probe PID mismatch");
|
||||
expect(read_le16(&kSwitchProbeDeviceDescriptor[12]) ==
|
||||
kSwitchProbeDeviceRevision,
|
||||
"Switch probe revision mismatch");
|
||||
expect(kSwitchProbeDeviceRevision != 0x0210,
|
||||
"Switch probe reuses the genuine controller cache identity");
|
||||
expect(read_le16(&kDeviceDescriptor[8]) == kPrototypeVendorId,
|
||||
"prototype VID mismatch");
|
||||
expect(read_le16(&kDeviceDescriptor[10]) == kPrototypeProductId,
|
||||
"prototype PID mismatch");
|
||||
expect(read_le16(&kDeviceDescriptor[12]) ==
|
||||
kPrototypeDeviceRevision,
|
||||
"prototype revision mismatch");
|
||||
expect(kDeviceDescriptor[4] == 0 && kDeviceDescriptor[5] == 0 &&
|
||||
kDeviceDescriptor[6] == 0,
|
||||
"multi-interface prototype is not a composite USB device");
|
||||
expect(kPrototypeVendorId != 0x045e,
|
||||
"prototype must not impersonate Microsoft's VID");
|
||||
expect(read_le16(&kConfigurationDescriptor[2]) ==
|
||||
sizeof(kConfigurationDescriptor),
|
||||
"configuration total length mismatch");
|
||||
expect(kConfigurationDescriptor[4] == SWITCH_PICO_HID_INSTANCE_COUNT,
|
||||
"configuration interface count mismatch");
|
||||
|
||||
std::array<bool, 16> endpoints{};
|
||||
for (uint8_t instance = 0; instance < SWITCH_PICO_HID_INSTANCE_COUNT;
|
||||
++instance) {
|
||||
const size_t offset = 9 + instance * kInterfaceDescriptorSize;
|
||||
const uint8_t *interface = &kConfigurationDescriptor[offset];
|
||||
expect(interface[0] == 9 && interface[1] == 4,
|
||||
"missing interface descriptor");
|
||||
expect(interface[2] == instance, "interface number mismatch");
|
||||
expect(interface[5] == 0xff && interface[6] == 0x5d &&
|
||||
interface[7] == 0x01,
|
||||
"XInput interface class tuple mismatch");
|
||||
expect(interface[9] == 0x10 && interface[10] == 0x21,
|
||||
"XInput capability descriptor missing");
|
||||
const uint8_t in_endpoint = interface[27];
|
||||
const uint8_t out_endpoint = interface[34];
|
||||
expect(in_endpoint == static_cast<uint8_t>(0x81 + instance),
|
||||
"input endpoint mismatch");
|
||||
expect(out_endpoint == static_cast<uint8_t>(0x01 + instance),
|
||||
"output endpoint mismatch");
|
||||
expect(interface[15] == in_endpoint && interface[21] == out_endpoint,
|
||||
"capability descriptor endpoint mismatch");
|
||||
expect(!endpoints[in_endpoint & 0x0f] &&
|
||||
!endpoints[out_endpoint & 0x0f],
|
||||
"endpoint number reused");
|
||||
endpoints[in_endpoint & 0x0f] = true;
|
||||
}
|
||||
}
|
||||
|
||||
void test_microsoft_compatible_id_descriptor() {
|
||||
using namespace XInputFeasibility;
|
||||
expect(read_le32(kMsCompatIdDescriptor) == sizeof(kMsCompatIdDescriptor),
|
||||
"Microsoft descriptor total length mismatch");
|
||||
expect(read_le16(&kMsCompatIdDescriptor[4]) == 0x0100,
|
||||
"Microsoft descriptor version mismatch");
|
||||
expect(read_le16(&kMsCompatIdDescriptor[6]) == kMsCompatIdIndex,
|
||||
"Microsoft descriptor index mismatch");
|
||||
expect(kMsCompatIdDescriptor[8] == SWITCH_PICO_HID_INSTANCE_COUNT,
|
||||
"Microsoft function count mismatch");
|
||||
for (uint8_t instance = 0;
|
||||
instance < SWITCH_PICO_HID_INSTANCE_COUNT; ++instance) {
|
||||
const uint8_t *function = &kMsCompatIdDescriptor[16 + instance * 24];
|
||||
expect(function[0] == instance,
|
||||
"Microsoft descriptor interface mismatch");
|
||||
expect(std::memcmp(&function[2], "XUSB10", 6) == 0,
|
||||
"XUSB10 compatible ID missing");
|
||||
}
|
||||
expect(read_le32(kProbeMsCompatIdDescriptor) == 16 &&
|
||||
kProbeMsCompatIdDescriptor[8] == 0,
|
||||
"probe descriptor must expose no compatible functions");
|
||||
}
|
||||
|
||||
void test_input_report_mapping() {
|
||||
ControllerState state{};
|
||||
auto report = XInputFeasibility::build_input_report(state);
|
||||
expect(report.report_id == 0 && report.report_size == 20,
|
||||
"neutral report header mismatch");
|
||||
expect(report.buttons == 0 && report.left_trigger == 0 &&
|
||||
report.right_trigger == 0,
|
||||
"neutral report controls mismatch");
|
||||
expect(report.left_x == 0 && report.left_y == 0 && report.right_x == 0 &&
|
||||
report.right_y == 0,
|
||||
"neutral axes mismatch");
|
||||
|
||||
state.dpad_up = true;
|
||||
state.button_south = true;
|
||||
state.button_east = true;
|
||||
state.button_west = true;
|
||||
state.button_north = true;
|
||||
state.button_start = true;
|
||||
state.button_select = true;
|
||||
state.button_system = true;
|
||||
state.left_trigger = UINT16_MAX;
|
||||
state.right_trigger = UINT16_MAX;
|
||||
state.left_stick_x = INT16_MIN;
|
||||
state.left_stick_y = INT16_MIN;
|
||||
state.right_stick_x = INT16_MAX;
|
||||
state.right_stick_y = INT16_MAX;
|
||||
report = XInputFeasibility::build_input_report(state);
|
||||
expect((report.buttons & XInputFeasibility::kDpadUp) != 0,
|
||||
"D-pad mapping missing");
|
||||
expect((report.buttons & XInputFeasibility::kButtonA) != 0 &&
|
||||
(report.buttons & XInputFeasibility::kButtonB) != 0 &&
|
||||
(report.buttons & XInputFeasibility::kButtonX) != 0 &&
|
||||
(report.buttons & XInputFeasibility::kButtonY) != 0,
|
||||
"positional face-button mapping mismatch");
|
||||
expect(report.left_trigger == 0xff && report.right_trigger == 0xff,
|
||||
"full analog trigger mapping mismatch");
|
||||
expect(report.left_x == INT16_MIN && report.left_y == INT16_MAX &&
|
||||
report.right_x == INT16_MAX &&
|
||||
report.right_y == -INT16_MAX,
|
||||
"axis endpoint mapping mismatch");
|
||||
|
||||
state.left_trigger = 0x8000;
|
||||
state.right_trigger = 0x7fff;
|
||||
report = XInputFeasibility::build_input_report(state);
|
||||
expect(report.left_trigger == 0x80 && report.right_trigger == 0x7f,
|
||||
"analog trigger precision was discarded");
|
||||
}
|
||||
|
||||
void test_rumble_report() {
|
||||
const uint8_t packet[8] = {0x00, 0x08, 0x00, 0xa5, 0x5a, 0x00, 0x00, 0x00};
|
||||
ControllerRumbleOutput output{};
|
||||
expect(
|
||||
XInputFeasibility::parse_rumble_report(packet, sizeof(packet), &output),
|
||||
"valid rumble report rejected");
|
||||
expect(output.low_frequency_magnitude == 0xa5 &&
|
||||
output.high_frequency_magnitude == 0x5a,
|
||||
"rumble magnitudes mapped incorrectly");
|
||||
expect(!XInputFeasibility::parse_rumble_report(packet, 4, &output),
|
||||
"truncated rumble report accepted");
|
||||
uint8_t wrong_type[8]{};
|
||||
expect(!XInputFeasibility::parse_rumble_report(wrong_type,
|
||||
sizeof(wrong_type), &output),
|
||||
"wrong rumble report type accepted");
|
||||
}
|
||||
|
||||
void test_host_probe_sequence() {
|
||||
AdapterHostProbeState state;
|
||||
state.note_ms_compat_id_request(10);
|
||||
expect(!state.windows_confirmed(),
|
||||
"compatible-ID request without signature confirmed Windows");
|
||||
state.note_ms_os_string();
|
||||
expect(state.saw_ms_os_string(),
|
||||
"Microsoft OS string observation was not retained");
|
||||
state.note_ms_compat_id_request(20);
|
||||
expect(state.windows_confirmed(),
|
||||
"two-stage Windows signature not confirmed");
|
||||
expect(!state.should_reboot(119), "probe rebooted before delay");
|
||||
expect(state.should_reboot(120), "probe did not reboot at deadline");
|
||||
|
||||
AdapterHostProbeState wrapped;
|
||||
wrapped.note_ms_os_string();
|
||||
wrapped.note_ms_compat_id_request(UINT32_MAX - 50);
|
||||
expect(!wrapped.should_reboot(48),
|
||||
"wrapped timer rebooted before deadline");
|
||||
expect(wrapped.should_reboot(49), "wrapped timer missed deadline");
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main() {
|
||||
test_device_and_configuration_descriptors();
|
||||
test_microsoft_compatible_id_descriptor();
|
||||
test_input_report_mapping();
|
||||
test_rumble_report();
|
||||
test_host_probe_sequence();
|
||||
return failures == 0 ? 0 : 1;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue