Skip to content

Commit

Permalink
Support using only one of --pid and --vid
Browse files Browse the repository at this point in the history
Currently, lxc55-host only evaluates the --pid and --vid options if both
are set.  With this patch, we evaluate the filters independent of each
other so that the user can set only one of them.
  • Loading branch information
robin-nitrokey authored and nickray committed Jun 3, 2021
1 parent e28a491 commit c33560c
Showing 1 changed file with 5 additions and 14 deletions.
19 changes: 5 additions & 14 deletions src/bootloader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,30 +59,21 @@ pub type Result<T> = std::result::Result<T, Error>;


impl Bootloader {
/// Select first available ROM bootloader with the given VID/PID pair.
/// Select first available ROM bootloader with the given VID and PID.
///
/// TODO: Open question is whether this is a good idea.
/// For instance, `write-flash` on the wrong device could wreak havoc.
pub fn try_new(vid: Option<u16>, pid: Option<u16>) -> Option<Self> {
Self::try_find(vid, pid, None)
}

/// Attempt to find a ROM bootloader with the given UUID (and VID/PID pair).
/// Attempt to find a ROM bootloader with the given VID, PID and UUID.
pub fn try_find(vid: Option<u16>, pid: Option<u16>, uuid: Option<Uuid>) -> Option<Self> {
Self::list()
.into_iter()
.filter(|bootloader| {
let mut predicate = true;
if vid.is_some() && pid.is_some() {
predicate = bootloader.vid == vid.unwrap() && bootloader.pid == pid.unwrap();
}
if predicate {
if let Some(uuid) = uuid {
predicate = bootloader.uuid == uuid.as_u128();
}
}
predicate
})
.filter(|bootloader| vid.map_or(true, |vid| vid == bootloader.vid))
.filter(|bootloader| pid.map_or(true, |pid| pid == bootloader.pid))
.filter(|bootloader| uuid.map_or(true, |uuid| uuid.as_u128() == bootloader.uuid))
.next()
}

Expand Down

0 comments on commit c33560c

Please sign in to comment.