mirror of
https://github.com/fossasia/badgemagic-rs
synced 2025-08-14 11:17:59 +00:00
Merge 9e66a19c8c
into 5d745ab8fd
This commit is contained in:
commit
09d4601bb3
2 changed files with 35 additions and 5 deletions
|
@ -103,7 +103,7 @@ fn main() -> Result<()> {
|
||||||
return list_devices(&args.transport);
|
return list_devices(&args.transport);
|
||||||
}
|
}
|
||||||
|
|
||||||
let payload = gnerate_payload(&mut args)?;
|
let payload = generate_payload(&mut args)?;
|
||||||
|
|
||||||
write_payload(&args.transport, payload)
|
write_payload(&args.transport, payload)
|
||||||
}
|
}
|
||||||
|
@ -129,7 +129,7 @@ fn list_devices(transport: &TransportProtocol) -> Result<()> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn gnerate_payload(args: &mut Args) -> Result<PayloadBuffer> {
|
fn generate_payload(args: &mut Args) -> Result<PayloadBuffer> {
|
||||||
let config_path = args.config.take().unwrap_or_default();
|
let config_path = args.config.take().unwrap_or_default();
|
||||||
let config = fs::read_to_string(&config_path)
|
let config = fs::read_to_string(&config_path)
|
||||||
.with_context(|| format!("load config: {config_path:?}"))?;
|
.with_context(|| format!("load config: {config_path:?}"))?;
|
||||||
|
|
|
@ -98,13 +98,43 @@ fn write_raw(device: &HidDevice, data: &[u8]) -> Result<()> {
|
||||||
// just to be sure
|
// just to be sure
|
||||||
assert!(data.len() <= 8192);
|
assert!(data.len() <= 8192);
|
||||||
|
|
||||||
let n = device.write(data).context("write payload")?;
|
let mut written: usize = 0;
|
||||||
|
|
||||||
|
#[cfg(windows)]
|
||||||
|
{
|
||||||
|
written = 0;
|
||||||
|
|
||||||
|
while written < data.len() {
|
||||||
|
let new_data: &[u8] = &prepend_byte_and_offset(data, written);
|
||||||
|
let n = device.write(new_data).context("write payload")?;
|
||||||
|
written = written + n - 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(not(windows))]
|
||||||
|
{
|
||||||
|
written = device.write(data).context("write payload")?;
|
||||||
|
}
|
||||||
|
|
||||||
anyhow::ensure!(
|
anyhow::ensure!(
|
||||||
n == data.len(),
|
written == data.len(),
|
||||||
"incomplete write: {n} of {} bytes",
|
"incomplete write: {written} of {} bytes",
|
||||||
data.len()
|
data.len()
|
||||||
);
|
);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn prepend_byte_and_offset(data: &[u8], offset: usize) -> [u8; 65] {
|
||||||
|
let mut result: [u8; 65] = [0u8; 65];
|
||||||
|
result[1] = 0x0;
|
||||||
|
if offset > data.len() {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
if data.len() - offset < 64 {
|
||||||
|
result[1..].copy_from_slice(&data[offset..]);
|
||||||
|
} else {
|
||||||
|
result[1..].copy_from_slice(&data[offset..offset + 64]);
|
||||||
|
}
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue