diff --git a/crates/openlogi-core/src/hid/error.rs b/crates/openlogi-core/src/hid/error.rs index 6f1e2d3a32..90c133bd4d 100644 --- a/crates/openlogi-core/src/hid/error.rs +++ b/crates/openlogi-core/src/hid/error.rs @@ -137,9 +137,9 @@ pub enum WriteError { /// The rejected rate. requested: ReportRate, }, - /// The device acknowledged a report-rate write but reads back a - /// different rate — for instance a gaming mouse whose onboard profile - /// owns the rate. + /// The device acknowledged or refused a report-rate write of a rate it + /// lists, and reads back a different rate — for instance a gaming mouse + /// whose onboard profile owns the rate. #[error("device reports {actual} Hz after a write of {requested} Hz")] ReportRateNotApplied { /// The rate that was written. diff --git a/crates/openlogi-device/src/write/report_rate.rs b/crates/openlogi-device/src/write/report_rate.rs index cbe2618182..48b5d5d4d9 100644 --- a/crates/openlogi-device/src/write/report_rate.rs +++ b/crates/openlogi-device/src/write/report_rate.rs @@ -17,7 +17,9 @@ //! already reports at it, and is read back afterwards: a device whose onboard //! profile owns the rate acknowledges the write and keeps its own, which //! surfaces as [`WriteError::ReportRateNotApplied`] instead of a success -//! nothing observed. +//! nothing observed. Some refuse it with `InvalidArgument` instead (a G Pro +//! Wireless in onboard mode); a refused rate the device listed, while it +//! still reports another, surfaces the same way. use std::ops::RangeInclusive; use std::sync::Arc; @@ -38,7 +40,7 @@ use tracing::debug; use crate::SharedChannel; use crate::backend::HidBackend; use crate::channel::route::{DIRECT_DEVICE_INDEX, DeviceRoute}; -use openlogi_core::hid::{ReportRate, ReportRateInfo}; +use openlogi_core::hid::{HidppFeatureErrorKind, ReportRate, ReportRateInfo}; use super::{HidppOperation, WriteError, classify_hidpp_error, open_feature, with_route}; @@ -288,7 +290,9 @@ pub(crate) async fn set_report_rate_on_channel( debug!(index, %rate, "report rate already set"); return Ok(before); } - control.write(rate).await?; + if let Err(error) = control.write(rate).await { + return Err(refusal_of_listed_rate(&control, rate, error).await); + } match control.current(HidppOperation::WriteReportRate).await { Ok(actual) if actual == rate => { debug!(index, %rate, "report rate written"); @@ -314,5 +318,36 @@ pub(crate) async fn set_report_rate_on_channel( } } +/// What a refused write of `rate`, a rate the device lists, means. A device +/// whose onboard profile owns the rate may refuse it with `InvalidArgument` +/// rather than acknowledge it and keep its own; when it still reports another +/// rate, that is [`WriteError::ReportRateNotApplied`]. Any other refusal, or a +/// read that fails, keeps the device's own error. +async fn refusal_of_listed_rate( + control: &ReportRateControl, + rate: ReportRate, + error: WriteError, +) -> WriteError { + if !matches!( + error, + WriteError::HidppFeature { + kind: HidppFeatureErrorKind::InvalidArgument, + .. + } + ) { + return error; + } + match control.current(HidppOperation::WriteReportRate).await { + Ok(actual) if actual != rate => { + debug!(%rate, %actual, "listed report rate refused; the device keeps its own"); + WriteError::ReportRateNotApplied { + requested: rate, + actual, + } + } + _ => error, + } +} + #[cfg(test)] mod tests; diff --git a/crates/openlogi-device/src/write/report_rate/tests.rs b/crates/openlogi-device/src/write/report_rate/tests.rs index e08150f963..89bc13994a 100644 --- a/crates/openlogi-device/src/write/report_rate/tests.rs +++ b/crates/openlogi-device/src/write/report_rate/tests.rs @@ -58,6 +58,8 @@ const EXTENDED_INDEX: u8 = 0x0f; const LEGACY_INDEX: u8 = 0x0e; /// HID++ 2.0 `Busy`. const BUSY: u8 = 0x08; +/// HID++ 2.0 `InvalidArgument`. +const INVALID_ARGUMENT: u8 = 0x02; /// Which report-rate feature a scripted mouse lists. #[derive(Clone, Copy, PartialEq, Eq)] @@ -84,6 +86,9 @@ struct Mouse { interval_ms: u8, /// Whether a write changes the rate. takes_writes: bool, + /// The HID++ error a rate write answers with instead of an + /// acknowledgement, if any. + refuses_writes: Option, /// Whether a rate read after a write fails with `Busy`. fails_read_back: bool, /// Every rate write, as its wire byte. @@ -101,6 +106,7 @@ impl Mouse { connection: 0, interval_ms: 1, takes_writes: true, + refuses_writes: None, fails_read_back: false, writes: Vec::new(), rate_reads: Vec::new(), @@ -141,6 +147,9 @@ impl Mouse { } (EXTENDED_INDEX, 0x03) => { self.writes.push(request[4]); + if let Some(code) = self.refuses_writes { + return Some(feature_error(request, code)); + } if self.takes_writes { self.extended_rates[self.connection] = request[4]; } @@ -154,6 +163,9 @@ impl Mouse { } (LEGACY_INDEX, 0x02) => { self.writes.push(request[4]); + if let Some(code) = self.refuses_writes { + return Some(feature_error(request, code)); + } if self.takes_writes { self.interval_ms = request[4]; } @@ -301,6 +313,54 @@ async fn a_rate_the_device_does_not_keep_is_not_applied() { assert_eq!(mouse.lock().expect("mouse lock").writes, [2]); } +/// A G Pro Wireless in onboard mode lists 125–1000 Hz on `0x8060` but +/// refuses a write of any of them with `InvalidArgument`, keeping the rate +/// its profile sets. +#[tokio::test] +async fn a_listed_rate_the_device_refuses_is_not_applied() { + let mouse = Arc::new(Mutex::new(Mouse { + interval_ms: 1, + refuses_writes: Some(INVALID_ARGUMENT), + ..Mouse::legacy() + })); + let shared = mouse_channel(&mouse, behind_receiver()).await; + + let error = set_report_rate_on(&shared, ReportRate::new(500)) + .await + .expect_err("the onboard profile keeps 1 kHz"); + + assert_eq!( + error, + WriteError::ReportRateNotApplied { + requested: ReportRate::new(500), + actual: ReportRate::new(1000), + } + ); + assert_eq!(mouse.lock().expect("mouse lock").writes, [2]); +} + +#[tokio::test] +async fn another_refusal_keeps_the_devices_error() { + let mouse = Arc::new(Mutex::new(Mouse { + refuses_writes: Some(BUSY), + ..Mouse::extended() + })); + let shared = mouse_channel(&mouse, direct()).await; + + let error = set_report_rate_on(&shared, ReportRate::new(500)) + .await + .expect_err("the mouse is busy"); + + assert_matches!( + error, + WriteError::HidppFeature { + operation: HidppOperation::WriteReportRate, + feature_hex: 0x8061, + kind: HidppFeatureErrorKind::Busy, + } + ); +} + #[tokio::test] async fn an_acknowledged_write_whose_read_back_fails_counts_as_written() { let mouse = Arc::new(Mutex::new(Mouse {