Skip to content

Commit

Permalink
Merge pull request #125 from RavuAlHemio/hid-report-descriptor
Browse files Browse the repository at this point in the history
Add support for get_report_descriptor()
  • Loading branch information
ruabmbua authored Jul 8, 2023
2 parents aad6c7c + 6f12270 commit 7f93bd9
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ extern "C" {
string: *mut wchar_t,
maxlen: size_t,
) -> c_int;
pub fn hid_get_report_descriptor(
hid_device: *mut HidDevice,
buf: *mut c_uchar,
buf_size: size_t,
) -> c_int;
pub fn hid_error(device: *mut HidDevice) -> *const wchar_t;
}

Expand Down
7 changes: 7 additions & 0 deletions src/hidapi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,4 +330,11 @@ impl HidDeviceBackendBase for HidDevice {

unsafe { conv_hid_device_info(raw_device) }
}

fn get_report_descriptor(&self, buf: &mut [u8]) -> HidResult<usize> {
let res = unsafe {
ffi::hid_get_report_descriptor(self._hid_device, buf.as_mut_ptr(), buf.len())
};
self.check_size(res)
}
}
10 changes: 10 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ use crate::hidapi::HidApiBackend;
use linux_native::HidApiBackend;

pub type HidResult<T> = Result<T, HidError>;
pub const MAX_REPORT_DESCRIPTOR_SIZE: usize = 4096;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum InitState {
Expand Down Expand Up @@ -434,6 +435,7 @@ trait HidDeviceBackendBase {
fn get_manufacturer_string(&self) -> HidResult<Option<String>>;
fn get_product_string(&self) -> HidResult<Option<String>>;
fn get_serial_number_string(&self) -> HidResult<Option<String>>;
fn get_report_descriptor(&self, buf: &mut [u8]) -> HidResult<usize>;

fn get_indexed_string(&self, _index: i32) -> HidResult<Option<String>> {
Err(HidError::HidApiError {
Expand Down Expand Up @@ -591,6 +593,14 @@ impl HidDevice {
self.inner.get_indexed_string(index)
}

/// Get a report descriptor from a HID device
///
/// User has to provide a preallocated buffer where the descriptor will be copied to.
/// It is recommended to use a preallocated buffer of [`MAX_REPORT_DESCRIPTOR_SIZE`] size.
pub fn get_report_descriptor(&self, buf: &mut [u8]) -> HidResult<usize> {
self.inner.get_report_descriptor(buf)
}

/// Get [`DeviceInfo`] from a HID device.
pub fn get_device_info(&self) -> HidResult<DeviceInfo> {
self.inner.get_device_info()
Expand Down
10 changes: 10 additions & 0 deletions src/linux_native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,16 @@ impl HidDeviceBackendBase for HidDevice {
}),
}
}

fn get_report_descriptor(&self, buf: &mut [u8]) -> HidResult<usize> {
let devnum = fstat(self.fd.as_raw_fd())?.st_rdev;
let syspath: PathBuf = format!("/sys/dev/char/{}:{}", major(devnum), minor(devnum)).into();

let descriptor = HidrawReportDescriptor::from_syspath(&syspath)?;
let min_size = buf.len().min(descriptor.0.len());
buf[..min_size].copy_from_slice(&descriptor.0[..min_size]);
Ok(min_size)
}
}

#[cfg(test)]
Expand Down

0 comments on commit 7f93bd9

Please sign in to comment.