From 4f19e7f99d067d433bdff1120dbf4cf80fadaf1d Mon Sep 17 00:00:00 2001 From: Joel Winarske Date: Mon, 13 Jul 2026 09:02:45 -0700 Subject: [PATCH 1/2] Return std::expected from the ps5 controller feature reads MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Convert InputReader::GetControllerMacAll/Version/CalibrationData from an int 0/1 status (whose result the caller discarded) to std::expected, marked [[nodiscard]]. Their two failure modes map to two error domains: an ioctl failure carries the errno via sys::ioctl, while a well-formed response that fails a protocol check (wrong ReportID / signature bytes) carries a new hid:: error category (src/bluez/hid_error.h) — the textbook way to represent a domain error in a std::error_code. Both compose through the same expected channel. open_and_init() now consumes the results: the feature reads are best-effort (a controller still streams input without calibration), so a failure is logged as a warning with the specific message and initialisation continues. Verified: full build green; a standalone check confirms the hid category (name "hid", message "invalid device response", enum comparison); a ps5 client start/SIGTERM cycle exits cleanly. Decoding a real controller needs hardware. Signed-off-by: Joel Winarske --- src/bluez/hid_error.h | 55 ++++++++++++++++++++++++ src/bluez/ps5_dual_sense/input_reader.cc | 55 +++++++++++++----------- src/bluez/ps5_dual_sense/input_reader.h | 13 ++++-- 3 files changed, 94 insertions(+), 29 deletions(-) create mode 100644 src/bluez/hid_error.h diff --git a/src/bluez/hid_error.h b/src/bluez/hid_error.h new file mode 100644 index 0000000..b55fac9 --- /dev/null +++ b/src/bluez/hid_error.h @@ -0,0 +1,55 @@ +// Copyright (c) 2026 Joel Winarske +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef SRC_BLUEZ_HID_ERROR_H +#define SRC_BLUEZ_HID_ERROR_H + +#include +#include + +/// Domain error category for HID feature-report handling. Failures that are not +/// errno (a well-formed ioctl that returns a response failing a protocol check) +/// carry a std::error_code in this category, so they compose with the errno +/// codes from sys:: through the same std::expected channel. +namespace hid { + +enum class Error { + kInvalidResponse = 1, // ioctl succeeded but the report failed validation +}; + +inline const std::error_category& error_category() noexcept { + static const struct Category final : std::error_category { + [[nodiscard]] const char* name() const noexcept override { return "hid"; } + [[nodiscard]] std::string message(const int condition) const override { + switch (static_cast(condition)) { + case Error::kInvalidResponse: + return "invalid device response"; + default: + return "unknown hid error"; + } + } + } category; + return category; +} + +inline std::error_code make_error_code(const Error e) noexcept { + return {static_cast(e), error_category()}; +} + +} // namespace hid + +template <> +struct std::is_error_code_enum : std::true_type {}; + +#endif // SRC_BLUEZ_HID_ERROR_H diff --git a/src/bluez/ps5_dual_sense/input_reader.cc b/src/bluez/ps5_dual_sense/input_reader.cc index a797848..114fc51 100644 --- a/src/bluez/ps5_dual_sense/input_reader.cc +++ b/src/bluez/ps5_dual_sense/input_reader.cc @@ -23,6 +23,7 @@ #include "../../utils/logging.h" #include "../../utils/sys.h" +#include "../hid_error.h" #include "../hidraw.hpp" #include "input_reader.h" @@ -98,11 +99,19 @@ void InputReader::open_and_init() { os << CustomHexdump<400, false>(std::data(rpt_desc.value), rpt_desc.size); LOG_INFO(os.str()); - // Get Features - GetControllerCalibrationData(fd.get(), - hw_cal_data_); // enables extended report for BT - GetControllerMacAll(fd.get(), controller_and_host_mac_); - GetControllerVersion(fd.get(), version_); + // Feature reports are best-effort: the controller still streams input if they + // fail, so log and continue. GetControllerCalibrationData also enables the + // extended BT report as a side effect. + if (auto r = GetControllerCalibrationData(fd.get(), hw_cal_data_); !r) { + LOG_WARN("controller calibration data unavailable: {}", + r.error().message()); + } + if (auto r = GetControllerMacAll(fd.get(), controller_and_host_mac_); !r) { + LOG_WARN("controller MAC unavailable: {}", r.error().message()); + } + if (auto r = GetControllerVersion(fd.get(), version_); !r) { + LOG_WARN("controller version unavailable: {}", r.error().message()); + } // Initialisation succeeded: keep the fd so the loop can poll it. fd_ = std::move(fd); @@ -154,40 +163,38 @@ void InputReader::dispatch(const short revents) { } } -int InputReader::GetControllerMacAll(const int fd, - ReportFeatureInMacAll& mac_all) { +std::expected InputReader::GetControllerMacAll( + const int fd, + ReportFeatureInMacAll& mac_all) { mac_all.ReportID = 0x09; if (auto r = sys::ioctl(fd, HIDIOCGFEATURE(sizeof(ReportFeatureInMacAll)), &mac_all); !r) { - LOG_ERROR("GetControllerMacAll failed: {}", r.error().message()); - return 1; + return std::unexpected(r.error()); } if (mac_all.ReportID != 0x09 || mac_all.Hard08 != 0x08 || mac_all.Hard25 != 0x25 || mac_all.Hard00 != 0x00) { - LOG_ERROR("GetControllerMacAll invalid response"); - return 1; + return std::unexpected(hid::make_error_code(hid::Error::kInvalidResponse)); } - return 0; + return {}; } -int InputReader::GetControllerVersion(const int fd, - ReportFeatureInVersion& version) { +std::expected InputReader::GetControllerVersion( + const int fd, + ReportFeatureInVersion& version) { version.Data.ReportID = 0x20; if (auto r = sys::ioctl(fd, HIDIOCGFEATURE(sizeof(ReportFeatureInVersion)), &version); !r) { - LOG_ERROR("GetControllerVersion failed: {}", r.error().message()); - return 1; + return std::unexpected(r.error()); } if (version.Data.ReportID != 0x20) { - LOG_ERROR("GetControllerVersion invalid response"); - return 1; + return std::unexpected(hid::make_error_code(hid::Error::kInvalidResponse)); } - return 0; + return {}; } -int InputReader::GetControllerCalibrationData( +std::expected InputReader::GetControllerCalibrationData( const int fd, HardwareCalibrationData& hw_cal_data) { static constexpr auto ACC_RES_PER_G = 8192; @@ -200,12 +207,10 @@ int InputReader::GetControllerCalibrationData( if (auto r = sys::ioctl( fd, HIDIOCGFEATURE(sizeof(ReportFeatureCalibrationData)), &cal_data); !r) { - LOG_ERROR("GetControllerCalibrationData failed: {}", r.error().message()); - return 1; + return std::unexpected(r.error()); } if (cal_data.Data.ReportID != 0x05) { - LOG_ERROR("GetControllerCalibrationData invalid response"); - return 1; + return std::unexpected(hid::make_error_code(hid::Error::kInvalidResponse)); } // Set gyroscope calibration and normalization parameters. @@ -285,7 +290,7 @@ int InputReader::GetControllerCalibrationData( i++; } - return 0; + return {}; } std::string InputReader::dpad_to_string(const Direction dpad) { diff --git a/src/bluez/ps5_dual_sense/input_reader.h b/src/bluez/ps5_dual_sense/input_reader.h index ef54cbb..98cd246 100644 --- a/src/bluez/ps5_dual_sense/input_reader.h +++ b/src/bluez/ps5_dual_sense/input_reader.h @@ -17,7 +17,9 @@ #include #include +#include #include +#include #include "../../utils/event_loop.h" #include "../../utils/unique_fd.h" @@ -68,12 +70,15 @@ class InputReader final : public EventSource { static std::string light_brightness_to_string(LightBrightness state); static std::string light_fade_animation_to_string(LightFadeAnimation state); - static int GetControllerMacAll(int fd, ReportFeatureInMacAll& mac_all); + [[nodiscard]] static std::expected GetControllerMacAll( + int fd, + ReportFeatureInMacAll& mac_all); - static int GetControllerVersion(int fd, ReportFeatureInVersion& version); + [[nodiscard]] static std::expected + GetControllerVersion(int fd, ReportFeatureInVersion& version); - static int GetControllerCalibrationData(int fd, - HardwareCalibrationData& hw_cal_data); + [[nodiscard]] static std::expected + GetControllerCalibrationData(int fd, HardwareCalibrationData& hw_cal_data); static void PrintCalibrationData(HardwareCalibrationData const& hw_cal_data); static void PrintControllerMacAll( From a449d3b7464dde442d26eb07026bc583ee32a4c0 Mon Sep 17 00:00:00 2001 From: Joel Winarske Date: Mon, 13 Jul 2026 09:36:23 -0700 Subject: [PATCH 2/2] hid_error: rewrite the trivial message() switch as an if Addresses the CodeQL "no trivial switch statements" review comment on #28: a single-case switch reads more naturally as an if. Signed-off-by: Joel Winarske --- src/bluez/hid_error.h | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/bluez/hid_error.h b/src/bluez/hid_error.h index b55fac9..8226f36 100644 --- a/src/bluez/hid_error.h +++ b/src/bluez/hid_error.h @@ -32,12 +32,10 @@ inline const std::error_category& error_category() noexcept { static const struct Category final : std::error_category { [[nodiscard]] const char* name() const noexcept override { return "hid"; } [[nodiscard]] std::string message(const int condition) const override { - switch (static_cast(condition)) { - case Error::kInvalidResponse: - return "invalid device response"; - default: - return "unknown hid error"; + if (static_cast(condition) == Error::kInvalidResponse) { + return "invalid device response"; } + return "unknown hid error"; } } category; return category;