Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows support #20

Merged
merged 2 commits into from
Jun 5, 2020
Merged
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
20 changes: 14 additions & 6 deletions cargo-hf2/src/main.rs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,7 @@ fn main() {
.unwrap();

if !status.success() {
use std::os::unix::process::ExitStatusExt;
let status = status
.code()
.or_else(|| if cfg!(unix) { status.signal() } else { None })
.unwrap_or(1);
std::process::exit(status);
exit_with_process_status(status)
}

let api = HidApi::new().expect("Couldn't find system usb");
Expand Down Expand Up @@ -145,6 +140,19 @@ fn main() {
);
}

#[cfg(unix)]
fn exit_with_process_status(status: std::process::ExitStatus) -> ! {
use std::os::unix::process::ExitStatusExt;
let status = status.code().or_else(|| status.signal()).unwrap_or(1);
std::process::exit(status)
}

#[cfg(not(unix))]
fn exit_with_process_status(status: std::process::ExitStatus) -> ! {
let status = status.code().unwrap_or(1);
std::process::exit(status)
}

pub trait MemoryRange {
fn contains_range(&self, range: &std::ops::Range<u32>) -> bool;
fn intersects_range(&self, range: &std::ops::Range<u32>) -> bool;
Expand Down
27 changes: 17 additions & 10 deletions hf2/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,12 @@ pub(crate) fn xmit<T: HidMockable>(cmd: Command, d: &T) -> Result<(), Error> {
log::debug!("{:?}", cmd);

//Packets are up to 64 bytes long
let buffer = &mut [0_u8; 64];
let buffer = &mut [0_u8; 65];

buffer[0] = 0; // Report ID

// header is at 0 so start at 1
let mut offset = 1;
// header is at 1 so start at 2
let mut offset = 2;

//command struct is 8 bytes
buffer.gwrite_with(cmd.id, &mut offset, LE)?;
Expand All @@ -170,13 +172,13 @@ pub(crate) fn xmit<T: HidMockable>(cmd: Command, d: &T) -> Result<(), Error> {

//subtract header from offset for packet size
if count == cmd.data.len() {
buffer[0] = (PacketType::Final as u8) << 6 | (offset - 1) as u8;
buffer[1] = (PacketType::Final as u8) << 6 | (offset - 2) as u8;
log::debug!("tx: {:02X?}", &buffer[..offset]);

d.my_write(&buffer[..offset])?;
return Ok(());
} else {
buffer[0] = (PacketType::Inner as u8) << 6 | (offset - 1) as u8;
buffer[1] = (PacketType::Inner as u8) << 6 | (offset - 2) as u8;
log::debug!("tx: {:02X?}", &buffer[..offset]);

d.my_write(&buffer[..offset])?;
Expand All @@ -187,17 +189,17 @@ pub(crate) fn xmit<T: HidMockable>(cmd: Command, d: &T) -> Result<(), Error> {
count += chunk.len();

if count == cmd.data.len() {
buffer[0] = (PacketType::Final as u8) << 6 | chunk.len() as u8;
buffer[1] = (PacketType::Final as u8) << 6 | chunk.len() as u8;
} else {
buffer[0] = (PacketType::Inner as u8) << 6 | chunk.len() as u8;
buffer[1] = (PacketType::Inner as u8) << 6 | chunk.len() as u8;
}

for (i, val) in chunk.iter().enumerate() {
buffer[i + 1] = *val
buffer[i + 2] = *val
}

log::debug!("tx: {:02X?}", &buffer[..=chunk.len()]);
d.my_write(&buffer[..=chunk.len()])?;
log::debug!("tx: {:02X?}", &buffer[..(chunk.len()+2)]);
d.my_write(&buffer[..(chunk.len()+2)])?;
}
Ok(())
}
Expand Down Expand Up @@ -299,34 +301,39 @@ mod tests {
fn send_fragmented() {
let data: Vec<Vec<u8>> = vec![
vec![
0x00,
0x3f, 0x06, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
0x00, 0x03, 0x20, 0xd7, 0x5e, 0x00, 0x00, 0x4d, 0x5f, 0x00, 0x00, 0x51, 0x5f, 0x00,
0x00, 0x4d, 0x5f, 0x00, 0x00, 0x4d, 0x5f, 0x00, 0x00, 0x4d, 0x5f, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x4d, 0x5f, 0x00, 0x00, 0x4d, 0x5f, 0x00,
],
vec![
0x00,
0x3f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4d, 0x5f, 0x00, 0x00, 0x4d, 0x5f, 0x00, 0x00,
0x4d, 0x5f, 0x00, 0x00, 0x4d, 0x5f, 0x00, 0x00, 0x4d, 0x5f, 0x00, 0x00, 0x4d, 0x5f,
0x00, 0x00, 0x4d, 0x5f, 0x00, 0x00, 0x4d, 0x5f, 0x00, 0x00, 0x4d, 0x5f, 0x00, 0x00,
0x4d, 0x5f, 0x00, 0x00, 0x4d, 0x5f, 0x00, 0x00, 0x4d, 0x5f, 0x00, 0x00, 0x4d, 0x5f,
0x00, 0x00, 0x4d, 0x5f, 0x00, 0x00, 0x4d, 0x5f,
],
vec![
0x00,
0x3f, 0x00, 0x00, 0x4d, 0x5f, 0x00, 0x00, 0x4d, 0x5f, 0x00, 0x00, 0x4d, 0x5f, 0x00,
0x00, 0x4d, 0x5f, 0x00, 0x00, 0x4d, 0x5f, 0x00, 0x00, 0x4d, 0x5f, 0x00, 0x00, 0x4d,
0x5f, 0x00, 0x00, 0x4d, 0x5f, 0x00, 0x00, 0x4d, 0x5f, 0x00, 0x00, 0x4d, 0x5f, 0x00,
0x00, 0x4d, 0x5f, 0x00, 0x00, 0x4d, 0x5f, 0x00, 0x00, 0x4d, 0x5f, 0x00, 0x00, 0x4d,
0x5f, 0x00, 0x00, 0x4d, 0x5f, 0x00, 0x00, 0x4d,
],
vec![
0x00,
0x3f, 0x5f, 0x00, 0x00, 0x4d, 0x5f, 0x00, 0x00, 0x4d, 0x5f, 0x00, 0x00, 0x4d, 0x5f,
0x00, 0x00, 0x4d, 0x5f, 0x00, 0x00, 0x4d, 0x5f, 0x00, 0x00, 0x4d, 0x5f, 0x00, 0x00,
0x4d, 0x5f, 0x00, 0x00, 0x4d, 0x5f, 0x00, 0x00, 0x4d, 0x5f, 0x00, 0x00, 0x4d, 0x5f,
0x00, 0x00, 0x4d, 0x5f, 0x00, 0x00, 0x4d, 0x5f, 0x00, 0x00, 0x4d, 0x5f, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
],
vec![
0x00,
0x50, 0x00, 0x00, 0x00, 0x00, 0x4d, 0x5f, 0x00, 0x00, 0x4d, 0x5f, 0x00, 0x00, 0x4d,
0x5f, 0x00, 0x00,
],
Expand Down