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
14 changes: 13 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ trait HidDeviceBackendBase {
fn send_feature_report(&self, data: &[u8]) -> HidResult<()>;
fn get_feature_report(&self, buf: &mut [u8]) -> HidResult<usize>;
fn send_output_report(&self, data: &[u8]) -> HidResult<()>;
#[cfg(any(hidapi, target_os = "linux"))]
#[cfg(any(hidapi, target_os = "linux", target_os = "windows"))]
fn get_input_report(&self, data: &mut [u8]) -> HidResult<usize>;
fn set_blocking_mode(&self, blocking: bool) -> HidResult<()>;
fn get_device_info(&self) -> HidResult<DeviceInfo>;
Expand Down Expand Up @@ -611,6 +611,18 @@ impl HidDevice {
self.inner.get_feature_report(buf)
}

/// Get an input report from a HID device.
///
/// Set the first byte of `buf` to the 'Report ID' of the report to be read.
/// Upon return, the first byte will still contain the Report ID, and the
/// report data will start in `buf[1]`.
///
/// If successful, returns the number of bytes read plus one for the report ID (which is still
/// in the first byte).
pub fn get_input_report(&self, buf: &mut [u8]) -> HidResult<usize> {
self.inner.get_input_report(buf)
}

/// Send a Output report to the device.
///
/// Output reports are sent over the Control endpoint as a Set_Report
Expand Down
34 changes: 34 additions & 0 deletions src/windows_native/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,40 @@ impl HidDeviceBackendBase for HidDevice {
Ok(bytes_returned as usize)
}

fn get_input_report(&self, buf: &mut [u8]) -> HidResult<usize> {
#[allow(clippy::identity_op, clippy::double_parens)]
const IOCTL_HID_GET_INPUT_REPORT: u32 = ((0x0000000b) << 16) | ((0) << 14) | ((104) << 2) | (2);
ensure!(!buf.is_empty(), Err(HidError::InvalidZeroSizeData));
let mut state = self.feature_state.borrow_mut();
let mut bytes_returned = 0;

let res = unsafe {
ResetEvent(state.overlapped.event_handle());
DeviceIoControl(
self.device_handle.as_raw(),
IOCTL_HID_GET_INPUT_REPORT,
buf.as_mut_ptr() as _,
buf.len() as u32,
buf.as_mut_ptr() as _,
buf.len() as u32,
&mut bytes_returned,
state.overlapped.as_raw(),
)
};
if res != TRUE {
let err = Win32Error::last();
ensure!(err == Win32Error::IoPending, Err(err.into()))
}

bytes_returned = state.overlapped.get_result(&self.device_handle, None)? as u32;

if buf[0] == 0x0 {
bytes_returned += 1;
}

Ok(bytes_returned as usize)
}

fn send_output_report(&self, data: &[u8]) -> HidResult<()> {
ensure!(!data.is_empty(), Err(HidError::InvalidZeroSizeData));
let mut state = self.feature_state.borrow_mut();
Expand Down