(handle: &mut libhid::Handle, report: &nitrokey::Report
) -> Result<()> where P: AsRef<[u8]>, { let mut retry = SEND_TRY_COUNT; loop { let result = handle.feature().send_to(0, report.as_ref()); retry -= 1; match result { Ok(_) => { return Ok(()); }, Err(err) => { if retry > 0 { thread::sleep(time::Duration::from_millis(SEND_RECV_DELAY_MS)); continue; } else { return Err(Error::HidError(err)); } }, } } } /// Receive a HID feature report from the device represented by the given handle. fn receive
(handle: &mut libhid::Handle) -> Result ::new();
let result = handle.feature().get_from(0, report.as_mut());
retry -= 1;
match result {
Ok(size) => {
if size < mem::size_of_val(&report) {
if retry > 0 {
continue;
} else {
return Err(Error::Error("Failed to receive complete report".to_string()));
}
}
if !report.is_valid() {
if retry > 0 {
continue;
} else {
return Err(Error::Error("Failed to receive report: CRC mismatch".to_string()));
}
}
return Ok(report);
},
Err(err) => {
if retry > 0 {
thread::sleep(time::Duration::from_millis(SEND_RECV_DELAY_MS));
continue;
} else {
return Err(Error::HidError(err));
}
},
}
}
}
/// Transmit a HID feature report to the nitrokey and receive a response.
fn transmit