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
54 changes: 54 additions & 0 deletions .github/workflows/hid_rp_tests.yml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion components/hid-rp/include/hid-rp-gamepad-formatters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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, "}}");
}
};
18 changes: 11 additions & 7 deletions components/hid-rp/include/hid-rp-gamepad.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,12 @@ class GamepadInputReport : public hid::report::base<hid::report::type::INPUT, RE
std::uint8_t hat_switch{0};
hid::report_bitset<hid::page::button, hid::page::button(1), hid::page::button(BUTTON_COUNT)>
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;
Expand All @@ -72,7 +74,7 @@ class GamepadInputReport : public hid::report::base<hid::report::type::INPUT, RE
std::fill(trigger_axes.begin(), trigger_axes.end(), trigger_center);
set_hat(Hat::CENTERED);
buttons.reset();
consumer_record = 0;
consumer_byte = 0;
}

/// Get the left joystick X and Y axis values
Expand Down Expand Up @@ -292,11 +294,13 @@ class GamepadInputReport : public hid::report::base<hid::report::type::INPUT, RE

/// Get the consumer record button value
/// \return The consumer record button value.
constexpr bool get_consumer_record() const { return consumer_record; }
constexpr bool get_consumer_record() const { return consumer_byte & 0x01; }

/// Set the consumer record button value
/// \param value The true/false value you want to se the consumer record button to.
constexpr void set_consumer_record(bool value) { consumer_record = value; }
constexpr void set_consumer_record(bool value) {
consumer_byte = value ? 0x01 : 0x00; // the padding bits stay 0
}

/// Get the input report as a vector of bytes
/// \return The input report as a vector of bytes.
Expand Down
2 changes: 1 addition & 1 deletion components/hid-rp/include/hid-rp-xbox-formatters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ template <uint8_t REPORT_ID> struct fmt::formatter<espp::XboxGamepadInputReport<
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, "}}");
}
};
14 changes: 7 additions & 7 deletions components/hid-rp/include/hid-rp-xbox.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,10 @@ class XboxGamepadInputReport : public espp::GamepadInputReport<15, std::uint16_t
template <uint8_t REPORT_ID = 3>
class XboxRumbleOutputReport : public hid::report::base<hid::report::type::OUTPUT, REPORT_ID> {
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<std::uint8_t, 4> magnitude{0}; // [0, 100] magnitude of the effect on each motor
std::uint8_t duration{0}; // [0, 255] duration of the effect
Expand Down Expand Up @@ -166,7 +166,7 @@ class XboxRumbleOutputReport : public hid::report::base<hid::report::type::OUTPU

/// Reset the rumble effect
constexpr void reset() {
enabled = 0;
enabled_byte = 0;
std::fill(magnitude.begin(), magnitude.end(), 0);
duration = 0;
start_delay = 0;
Expand All @@ -175,11 +175,11 @@ class XboxRumbleOutputReport : public hid::report::base<hid::report::type::OUTPU

/// Get the enabled mask for the rumble motors
/// \return The enabled mask for the rumble motors
constexpr auto get_enabled() const { return enabled; }
constexpr std::uint8_t get_enabled() const { return enabled_byte & 0x0f; }

/// Set the enabled mask for the rumble motors
/// \param new_enabled The enabled mask for the rumble motors
constexpr void set_enabled(std::uint8_t new_enabled) { enabled = new_enabled; }
constexpr void set_enabled(std::uint8_t new_enabled) { enabled_byte = new_enabled & 0x0f; }

/// Get the magnitude of the rumble effect for the specified motor
/// \param motor The motor for which you want to get the magnitude.
Expand Down
20 changes: 20 additions & 0 deletions components/hid-rp/test/hid_rp_gamepad_host_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

#include <cstdint>
#include <cstdio>
#include <cstring>
#include <new>
#include <vector>

#include "hid-rp-gamepad.hpp"
Expand Down Expand Up @@ -113,6 +115,23 @@ static void test_byte_axes_layout_without_report_id() {
check_byte_layout<ByteReportNoId>();
}

// 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;
Expand Down Expand Up @@ -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) {
Expand Down
14 changes: 14 additions & 0 deletions components/hid-rp/test/hid_rp_reports_host_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <new>
#include <vector>

#include "hid-rp-3dconnexion.hpp"
Expand Down Expand Up @@ -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);
Expand Down
Loading