Skip to content

Conversation

@jerrysxie
Copy link
Contributor

Wrap all I2C master bus operations with timeouts to prevent indefinite blocking when communicating with HID devices.

  • Add DEVICE_RESPONSE_TIMEOUT_MS (200ms) for descriptor reads and commands
  • Add DATA_READ_TIMEOUT_MS (50ms) for input reports and feature data
  • Return Error::Hid(hid::Error::Timeout) on timeout expiry

Wrap all I2C master bus operations with timeouts to
prevent indefinite blocking when communicating with HID devices.

- Add DEVICE_RESPONSE_TIMEOUT_MS (200ms) for descriptor reads and commands
- Add DATA_READ_TIMEOUT_MS (50ms) for input reports and feature data
- Return Error::Hid(hid::Error::Timeout) on timeout expiry
@jerrysxie jerrysxie self-assigned this Jan 28, 2026
Copilot AI review requested due to automatic review settings January 28, 2026 23:24
@jerrysxie jerrysxie added the enhancement New feature or request label Jan 28, 2026

use crate::Error;

const DEVICE_RESPONSE_TIMEOUT_MS: u64 = 200;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@RobertZ2011 I copied these timeouts from the host.rs. Do these timeouts make sense communication with HID passthrough device?

Copy link
Contributor

Choose a reason for hiding this comment

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

200 ms is a good default that should be good for most use cases, but long-term I think these should be configuration options passed into the device. The spec uses 16 bits for length and that would take ~5 seconds at 100 KHz.

@jerrysxie jerrysxie requested a review from RobertZ2011 January 28, 2026 23:26
@jerrysxie jerrysxie linked an issue Jan 28, 2026 that may be closed by this pull request
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds bounded timeouts around all I2C master operations performed by the HID I2C device side, preventing indefinite blocking when devices misbehave or stop responding.

Changes:

  • Introduced DEVICE_RESPONSE_TIMEOUT_MS (200 ms) and DATA_READ_TIMEOUT_MS (50 ms) in the I2C device implementation.
  • Wrapped all bus.write_read, bus.read, and bus.write calls in hid-service/src/i2c/device.rs with embassy_time::with_timeout, mapping timeout expiries to Error::Hid(hid::Error::Timeout).
  • Added per-call logging for timeout vs. underlying bus errors for descriptor reads, report descriptor reads, input reports, and command/host-data transfers.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 11 to +14

const DEVICE_RESPONSE_TIMEOUT_MS: u64 = 200;
const DATA_READ_TIMEOUT_MS: u64 = 50;

Copy link

Copilot AI Jan 28, 2026

Choose a reason for hiding this comment

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

DEVICE_RESPONSE_TIMEOUT_MS and DATA_READ_TIMEOUT_MS are defined here with the same values as in i2c/host.rs, which duplicates configuration and could get out of sync if one side is tuned independently. Consider defining these timeouts in a shared module (e.g. the i2c mod or crate root) and reusing them in both host and device to keep the behavior consistent and easier to adjust.

Suggested change
const DEVICE_RESPONSE_TIMEOUT_MS: u64 = 200;
const DATA_READ_TIMEOUT_MS: u64 = 50;
use crate::{DEVICE_RESPONSE_TIMEOUT_MS, DATA_READ_TIMEOUT_MS};

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

Add timeouts around I2C operations in hid-service

2 participants