From eeecd2cb45f4d42819ff25d8fd44c1b724d926bc Mon Sep 17 00:00:00 2001 From: William Emfinger Date: Sun, 22 Nov 2026 09:17:28 -0700 Subject: [PATCH] fix(hid-rp): gamepad and Xbox rumble reports no longer send uninitialized padding bits GamepadInputReport kept its consumer byte as `struct { uint8_t consumer_record : 1; uint8_t : 7; }` and XboxRumbleOutputReport its first byte as `struct { uint8_t enabled : 4; uint8_t : 4; }`. Unnamed bit-fields cannot be assigned, so reset() only cleared the named bits and the padding bits carried whatever was in memory into every report (hid_rp_gamepad_host_test's `bytes[9] == 0` caught it, depending on the stack's contents). Both are now plain zero-initialized bytes with bit accessors (get_/set_consumer_record(), get_/set_enabled() masking to their bits); the formatters use the getters. Layout and sizes are unchanged. Tests construct the reports in 0xFF-filled storage, so the regression check is deterministic; both fail on the old headers. A new hid_rp_tests.yml workflow runs every hid-rp host test in CI (only the report-map test ran before). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01DFjjnq3XCTRAXENSxsCxJU --- .github/workflows/hid_rp_tests.yml | 54 +++++++++++++++++++ .../include/hid-rp-gamepad-formatters.hpp | 2 +- components/hid-rp/include/hid-rp-gamepad.hpp | 18 ++++--- .../hid-rp/include/hid-rp-xbox-formatters.hpp | 2 +- components/hid-rp/include/hid-rp-xbox.hpp | 14 ++--- .../hid-rp/test/hid_rp_gamepad_host_test.cpp | 20 +++++++ .../hid-rp/test/hid_rp_reports_host_test.cpp | 14 +++++ 7 files changed, 108 insertions(+), 16 deletions(-) create mode 100644 .github/workflows/hid_rp_tests.yml diff --git a/.github/workflows/hid_rp_tests.yml b/.github/workflows/hid_rp_tests.yml new file mode 100644 index 0000000000..4d86b952c7 --- /dev/null +++ b/.github/workflows/hid_rp_tests.yml @@ -0,0 +1,54 @@ +name: hid-rp host tests + +# Builds and runs every host test of the hid-rp component on the host with +# g++ -std=c++23: report layouts / descriptors (gamepad, Xbox, PlayStation, +# Switch Pro, 3Dconnexion) and the runtime report map + decoders. + +# Minimal token scope: build + run tests, never writes. +permissions: + contents: read + +on: + pull_request: + paths: + - "components/hid-rp/**" + - "components/format/**" + - ".github/workflows/hid_rp_tests.yml" + push: + branches: + - main + paths: + - "components/hid-rp/**" + - "components/format/**" + - ".github/workflows/hid_rp_tests.yml" + workflow_dispatch: + +concurrency: + group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} + cancel-in-progress: true + +jobs: + host-test: + name: C++ host tests (hid-rp) + runs-on: ubuntu-latest + timeout-minutes: 15 + steps: + - uses: actions/checkout@v7 + with: + submodules: recursive + - name: Build and run every hid-rp host test + run: | + g++ --version | head -1 + failed=0 + for t in components/hid-rp/test/*_host_test.cpp; do + name=$(basename "$t" .cpp) + echo "::group::$name" + g++ -std=c++23 -Wall -Wextra -Werror -Wno-comment \ + -isystem components/hid-rp/include \ + -isystem components/hid-rp/detail/hid-rp/hid-rp \ + -isystem components/format/include \ + -isystem components/format/detail/fmt/include \ + "$t" -o "$name" && "./$name" || failed=1 + echo "::endgroup::" + done + exit $failed diff --git a/components/hid-rp/include/hid-rp-gamepad-formatters.hpp b/components/hid-rp/include/hid-rp-gamepad-formatters.hpp index 7e9a315034..397744f40a 100644 --- a/components/hid-rp/include/hid-rp-gamepad-formatters.hpp +++ b/components/hid-rp/include/hid-rp-gamepad-formatters.hpp @@ -39,7 +39,7 @@ struct fmt::formatter< buttons.set(i - 1, report.buttons.test(hid::page::button(i))); } fmt::format_to(out, "{}", buttons); - fmt::format_to(out, "], consumer_record: {}", (bool)report.consumer_record); + fmt::format_to(out, "], consumer_record: {}", report.get_consumer_record()); return fmt::format_to(out, "}}"); } }; diff --git a/components/hid-rp/include/hid-rp-gamepad.hpp b/components/hid-rp/include/hid-rp-gamepad.hpp index e87fd2a68d..4e42b70817 100644 --- a/components/hid-rp/include/hid-rp-gamepad.hpp +++ b/components/hid-rp/include/hid-rp-gamepad.hpp @@ -55,10 +55,12 @@ class GamepadInputReport : public hid::report::base buttons; - struct { - std::uint8_t consumer_record : 1; - std::uint8_t : 7; - }; + /// Bit 0: the consumer Record button; bits 1..7: the descriptor's constant + /// padding, always 0. A plain byte rather than a bit-field struct: the unnamed + /// padding bits of `struct { uint8_t consumer_record : 1; uint8_t : 7; }` + /// could never be cleared (reset() can only assign the named field), so they + /// carried whatever was in memory into every report. + std::uint8_t consumer_byte{0}; public: using Hat = espp::gamepad::Hat; @@ -72,7 +74,7 @@ class GamepadInputReport : public hid::report::base struct fmt::formatter class XboxRumbleOutputReport : public hid::report::base { protected: - struct { - std::uint8_t enabled : 4; // which motors are enabled - std::uint8_t : 4; - }; + /// Low nibble: which motors are enabled; high nibble: reserved, always 0 (a + /// plain byte, not a bit-field struct whose unnamed padding bits could + /// never be cleared and went out uninitialized). + std::uint8_t enabled_byte{0}; std::array magnitude{0}; // [0, 100] magnitude of the effect on each motor std::uint8_t duration{0}; // [0, 255] duration of the effect @@ -166,7 +166,7 @@ class XboxRumbleOutputReport : public hid::report::base #include +#include +#include #include #include "hid-rp-gamepad.hpp" @@ -113,6 +115,23 @@ static void test_byte_axes_layout_without_report_id() { check_byte_layout(); } +// The consumer byte's padding bits are 0 whatever the memory held before (they +// used to be an unnamed bit-field that reset() could not clear, so a report +// went out with garbage in them). +static void test_padding_bits_are_zero() { + std::printf("test_padding_bits_are_zero\n"); + alignas(ByteReport) unsigned char storage[sizeof(ByteReport)]; + std::memset(storage, 0xFF, sizeof(storage)); + auto *report = new (storage) ByteReport(); + auto bytes = report->get_report(); + CHECK(bytes[9] == 0); + report->set_consumer_record(true); + CHECK(report->get_report()[9] == 0x01 && report->get_consumer_record()); + report->set_consumer_record(false); + CHECK(report->get_report()[9] == 0 && !report->get_consumer_record()); + report->~ByteReport(); +} + static void test_byte_axes_centered_is_neutral() { std::printf("test_byte_axes_centered_is_neutral\n"); ByteReport report; @@ -164,6 +183,7 @@ int main() { test_byte_axes_layout(); test_byte_axes_layout_without_report_id(); test_byte_axes_centered_is_neutral(); + test_padding_bits_are_zero(); test_set_data_round_trip(); if (g_failures == 0) { diff --git a/components/hid-rp/test/hid_rp_reports_host_test.cpp b/components/hid-rp/test/hid_rp_reports_host_test.cpp index 3b8915b59c..29242c8a29 100644 --- a/components/hid-rp/test/hid_rp_reports_host_test.cpp +++ b/components/hid-rp/test/hid_rp_reports_host_test.cpp @@ -23,6 +23,7 @@ #include #include #include +#include #include #include "hid-rp-3dconnexion.hpp" @@ -157,6 +158,19 @@ static void test_xbox() { check_offset_and_size(rumble0, 8); CHECK(rumble0.get_report() == bytes); + // the reserved high nibble of byte 0 is always 0, whatever the memory held + // before (it used to be an unnamed bit-field that reset() could not clear) + { + alignas(espp::XboxRumbleOutputReport<>) unsigned char + storage[sizeof(espp::XboxRumbleOutputReport<>)]; + std::memset(storage, 0xFF, sizeof(storage)); + auto *dirty = new (storage) espp::XboxRumbleOutputReport<>(); + CHECK(dirty->get_report()[0] == 0x00); + dirty->set_enabled(0xFF); + CHECK(dirty->get_report()[0] == 0x0F && dirty->get_enabled() == 0x0F); + dirty->~XboxRumbleOutputReport(); + } + espp::XboxBatteryInputReport<> batt; batt.set_data({0xA5}); check_offset_and_size(batt, 1);