Skip to content
Open
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
6 changes: 3 additions & 3 deletions crates/openlogi-core/src/hid/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
41 changes: 38 additions & 3 deletions crates/openlogi-device/src/write/report_rate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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};

Expand Down Expand Up @@ -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");
Expand All @@ -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 {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P0 A refusal becomes a timeout

Blocking. If a wireless mouse returns InvalidArgument and then stops answering, refusal_of_listed_rate starts a five-second read inside the agent's five-second write deadline. The outer deadline cancels the read before _ => error can return the original refusal. The caller gets RequestTimedOut instead.

This violates the repository requirement that each nested read fit inside its caller's deadline. Give this read a smaller budget and preserve the refusal on expiry. Add a refused_write_with_silent_readback_preserves_refusal test through the agent deadline.

Rule Used: Severity: high. Every nested I/O step has its own timeout that fits inside the caller's deadline, and a test asserts the arithmetic. An advisory read the device may ignore (LED, hosts info) gets a budget of hundreds of milliseconds and reads as unkno... (source)

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;
60 changes: 60 additions & 0 deletions crates/openlogi-device/src/write/report_rate/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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<u8>,
/// Whether a rate read after a write fails with `Busy`.
fails_read_back: bool,
/// Every rate write, as its wire byte.
Expand All @@ -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(),
Expand Down Expand Up @@ -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];
}
Expand All @@ -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];
}
Expand Down Expand Up @@ -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");
Comment on lines +326 to +330

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 New tests lack required deadlines

Blocking. Both new tests await channel setup and device writes without a test-owned timeout or an expiry message. This violates the repository requirement that every async test wait have both. Wrap each test's async work in a bounded wait that names what stalled.

The PR also omits the required counter-proof. State that reverting the refusal conversion makes a_listed_rate_the_device_refuses_is_not_applied fail. No counter-proof was run during this review.

Rule Used: Severity: medium. Every bug fix ships a regression test that fails without the fix, and the PR states the counter-proof (the reverted line or the killed mutant). A test that passes with the fix reverted is not a regression test: ask for inputs where ... (source)

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!


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 {
Expand Down