#[derive(Clone, Copy, PartialEq, Eq, Default)]
pub struct DeviceFeature(u16);
impl DeviceFeature {
pub const NONE: DeviceFeature = DeviceFeature(0);
pub const CAPTURE: DeviceFeature = DeviceFeature(1 << 0);
pub const IDENTIFY: DeviceFeature = DeviceFeature(1 << 1);
pub const VERIFY: DeviceFeature = DeviceFeature(1 << 2);
pub const STORAGE: DeviceFeature = DeviceFeature(1 << 3);
pub const STORAGE_LIST: DeviceFeature = DeviceFeature(1 << 4);
pub const STORAGE_DELETE: DeviceFeature = DeviceFeature(1 << 5);
pub const STORAGE_CLEAR: DeviceFeature = DeviceFeature(1 << 6);
pub const DUPLICATES_CHECK: DeviceFeature = DeviceFeature(1 << 7);
pub const ALWAYS_ON: DeviceFeature = DeviceFeature(1 << 8);
pub const UPDATE_PRINT: DeviceFeature = DeviceFeature(1 << 9);
#[must_use]
pub const fn bits(self) -> u16 {
self.0
}
#[must_use]
pub const fn from_bits_truncate(bits: u32) -> DeviceFeature {
DeviceFeature((bits & 0x03FF) as u16)
}
#[must_use]
pub const fn contains(self, other: DeviceFeature) -> bool {
self.0 & other.0 == other.0
}
#[must_use]
pub const fn is_empty(self) -> bool {
self.0 == 0
}
#[must_use]
pub const fn intersects(self, other: DeviceFeature) -> bool {
self.0 & other.0 != 0
}
#[must_use]
pub const fn is_match_on_chip(self) -> bool {
self.contains(DeviceFeature::STORAGE)
}
}
pub(crate) const FLAGS: [(DeviceFeature, &str); 10] = [
(DeviceFeature::CAPTURE, "CAPTURE"),
(DeviceFeature::IDENTIFY, "IDENTIFY"),
(DeviceFeature::VERIFY, "VERIFY"),
(DeviceFeature::STORAGE, "STORAGE"),
(DeviceFeature::STORAGE_LIST, "STORAGE_LIST"),
(DeviceFeature::STORAGE_DELETE, "STORAGE_DELETE"),
(DeviceFeature::STORAGE_CLEAR, "STORAGE_CLEAR"),
(DeviceFeature::DUPLICATES_CHECK, "DUPLICATES_CHECK"),
(DeviceFeature::ALWAYS_ON, "ALWAYS_ON"),
(DeviceFeature::UPDATE_PRINT, "UPDATE_PRINT"),
];
const fn defined_bits() -> u16 {
let mut acc = 0u16;
let mut i = 0;
while i < FLAGS.len() {
acc |= FLAGS[i].0.bits();
i += 1;
}
acc
}
const _: () = assert!(DeviceFeature::from_bits_truncate(u32::MAX).bits() == defined_bits());
impl core::ops::BitOr for DeviceFeature {
type Output = DeviceFeature;
fn bitor(self, rhs: DeviceFeature) -> DeviceFeature {
DeviceFeature(self.0 | rhs.0)
}
}
impl core::ops::BitOrAssign for DeviceFeature {
fn bitor_assign(&mut self, rhs: DeviceFeature) {
self.0 |= rhs.0;
}
}
impl core::ops::BitAnd for DeviceFeature {
type Output = DeviceFeature;
fn bitand(self, rhs: DeviceFeature) -> DeviceFeature {
DeviceFeature(self.0 & rhs.0)
}
}
impl core::ops::BitAndAssign for DeviceFeature {
fn bitand_assign(&mut self, rhs: DeviceFeature) {
self.0 &= rhs.0;
}
}
impl core::ops::Sub for DeviceFeature {
type Output = DeviceFeature;
fn sub(self, rhs: DeviceFeature) -> DeviceFeature {
DeviceFeature(self.0 & !rhs.0)
}
}
impl core::ops::SubAssign for DeviceFeature {
fn sub_assign(&mut self, rhs: DeviceFeature) {
self.0 &= !rhs.0;
}
}
impl core::ops::Not for DeviceFeature {
type Output = DeviceFeature;
fn not(self) -> DeviceFeature {
DeviceFeature(defined_bits() & !self.0)
}
}
impl core::fmt::Debug for DeviceFeature {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
if self.0 == 0 {
return f.write_str("NONE");
}
let mut first = true;
for (flag, name) in FLAGS {
if self.contains(flag) {
if !first {
f.write_str(" | ")?;
}
first = false;
f.write_str(name)?;
}
}
Ok(())
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum ScanType {
Swipe,
Press,
}
#[derive(Clone, Copy, PartialEq, Eq, Default, Debug)]
pub struct FingerStatus(u8);
impl FingerStatus {
pub const NONE: FingerStatus = FingerStatus(0);
pub const NEEDED: FingerStatus = FingerStatus(1 << 0);
pub const PRESENT: FingerStatus = FingerStatus(1 << 1);
#[must_use]
pub const fn bits(self) -> u8 {
self.0
}
const DEFINED_BITS: u8 = FingerStatus::NEEDED.0 | FingerStatus::PRESENT.0;
#[must_use]
pub const fn from_bits_truncate(bits: u8) -> FingerStatus {
FingerStatus(bits & FingerStatus::DEFINED_BITS)
}
#[must_use]
pub const fn contains(self, other: FingerStatus) -> bool {
self.0 & other.0 == other.0
}
#[must_use]
pub const fn is_empty(self) -> bool {
self.0 == 0
}
#[must_use]
pub const fn intersects(self, other: FingerStatus) -> bool {
self.0 & other.0 != 0
}
}
impl core::ops::BitOr for FingerStatus {
type Output = FingerStatus;
fn bitor(self, rhs: FingerStatus) -> FingerStatus {
FingerStatus(self.0 | rhs.0)
}
}
impl core::ops::BitOrAssign for FingerStatus {
fn bitor_assign(&mut self, rhs: FingerStatus) {
self.0 |= rhs.0;
}
}
impl core::ops::BitAnd for FingerStatus {
type Output = FingerStatus;
fn bitand(self, rhs: FingerStatus) -> FingerStatus {
FingerStatus(self.0 & rhs.0)
}
}
impl core::ops::BitAndAssign for FingerStatus {
fn bitand_assign(&mut self, rhs: FingerStatus) {
self.0 &= rhs.0;
}
}
impl core::ops::Sub for FingerStatus {
type Output = FingerStatus;
fn sub(self, rhs: FingerStatus) -> FingerStatus {
FingerStatus(self.0 & !rhs.0)
}
}
impl core::ops::SubAssign for FingerStatus {
fn sub_assign(&mut self, rhs: FingerStatus) {
self.0 &= !rhs.0;
}
}
impl core::ops::Not for FingerStatus {
type Output = FingerStatus;
fn not(self) -> FingerStatus {
FingerStatus(FingerStatus::DEFINED_BITS & !self.0)
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[non_exhaustive]
pub enum Temperature {
Cold,
Warm,
Hot,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn feature_bits_match_libfprint() {
assert_eq!(DeviceFeature::CAPTURE.bits(), 1 << 0);
assert_eq!(DeviceFeature::IDENTIFY.bits(), 1 << 1);
assert_eq!(DeviceFeature::VERIFY.bits(), 1 << 2);
assert_eq!(DeviceFeature::UPDATE_PRINT.bits(), 1 << 9);
}
#[test]
fn contains_and_moc_detection() {
let moc = DeviceFeature::VERIFY
| DeviceFeature::IDENTIFY
| DeviceFeature::STORAGE
| DeviceFeature::STORAGE_LIST;
assert!(moc.contains(DeviceFeature::STORAGE));
assert!(moc.is_match_on_chip());
assert!(!DeviceFeature::CAPTURE.is_match_on_chip());
}
const DEFINED: [DeviceFeature; FLAGS.len()] = {
let mut out = [DeviceFeature::NONE; FLAGS.len()];
let mut i = 0;
while i < FLAGS.len() {
out[i] = FLAGS[i].0;
i += 1;
}
out
};
#[test]
fn from_bits_truncate_keeps_exactly_the_defined_bits() {
for b in 0..=u32::from(u16::MAX) {
let kept = DeviceFeature::from_bits_truncate(b).bits();
assert_eq!(
kept,
b as u16 & defined_bits(),
"from_bits_truncate({b:#06x})"
);
}
assert_eq!(DeviceFeature::from_bits_truncate(0x1_0000).bits(), 0);
assert_eq!(
DeviceFeature::from_bits_truncate(u32::MAX).bits(),
defined_bits()
);
}
#[test]
fn bitor_is_associative_commutative_with_none_identity() {
for a in DEFINED {
assert_eq!(a | DeviceFeature::NONE, a, "identity for {a:?}");
for b in DEFINED {
assert_eq!(a | b, b | a, "commutativity for {a:?}, {b:?}");
for c in DEFINED {
assert_eq!(
(a | b) | c,
a | (b | c),
"associativity for {a:?}, {b:?}, {c:?}"
);
}
}
}
}
#[test]
fn contains_none_always_and_moc_is_exactly_storage() {
for bits in 0..=u32::from(defined_bits()) {
let set = DeviceFeature::from_bits_truncate(bits);
assert!(
set.contains(DeviceFeature::NONE),
"{set:?} must contain NONE"
);
assert!(set.contains(set), "{set:?} must contain itself");
assert_eq!(
set.is_match_on_chip(),
set.contains(DeviceFeature::STORAGE),
"{set:?}"
);
for flag in DEFINED {
assert_eq!(
set.contains(flag),
bits & u32::from(flag.bits()) != 0,
"{set:?}.contains({flag:?})"
);
assert!(
(set | flag).contains(flag),
"{set:?} | {flag:?} must contain {flag:?}"
);
}
}
}
#[test]
fn bitand_sub_not_obey_the_set_laws() {
let all = DeviceFeature::from_bits_truncate(u32::from(defined_bits()));
for a_bits in 0..=defined_bits() {
let a = DeviceFeature::from_bits_truncate(u32::from(a_bits));
assert!((a & !a).is_empty(), "a & !a must be empty for {a:?}");
assert_eq!(a | !a, all, "a | !a must be every defined flag for {a:?}");
assert_eq!(!!a, a, "double complement is identity for {a:?}");
assert_eq!(a.is_empty(), a == DeviceFeature::NONE, "is_empty for {a:?}");
for b_bits in 0..=defined_bits() {
let b = DeviceFeature::from_bits_truncate(u32::from(b_bits));
assert_eq!(a & b, b & a, "intersection commutes for {a:?}, {b:?}");
assert_eq!(
a.intersects(b),
!(a & b).is_empty(),
"intersects agrees with & for {a:?}, {b:?}"
);
let diff = a - b;
assert!(!diff.intersects(b), "a - b keeps no b bit for {a:?}, {b:?}");
assert_eq!(diff, a & !b, "a - b == a & !b for {a:?}, {b:?}");
assert_eq!(diff | (a & b), a, "difference and intersection partition a");
let mut and_acc = a;
and_acc &= b;
assert_eq!(and_acc, a & b, "&= agrees for {a:?}, {b:?}");
let mut sub_acc = a;
sub_acc -= b;
assert_eq!(sub_acc, a - b, "-= agrees for {a:?}, {b:?}");
}
}
}
#[test]
fn bitor_assign_agrees_with_bitor() {
for a in DEFINED {
for b in DEFINED {
let mut acc = a;
acc |= b;
assert_eq!(acc, a | b, "{a:?} |= {b:?}");
}
}
}
#[test]
fn finger_status_bitor_is_associative_commutative_with_none_identity() {
const THREE: [FingerStatus; 3] = [
FingerStatus::NONE,
FingerStatus::NEEDED,
FingerStatus::PRESENT,
];
for a in THREE {
assert_eq!(a | FingerStatus::NONE, a, "identity for {a:?}");
assert!(a.contains(FingerStatus::NONE), "{a:?} must contain NONE");
for b in THREE {
assert_eq!(a | b, b | a, "commutativity for {a:?}, {b:?}");
assert!(
(a | b).contains(a) && (a | b).contains(b),
"union for {a:?}, {b:?}"
);
for c in THREE {
assert_eq!(
(a | b) | c,
a | (b | c),
"associativity for {a:?}, {b:?}, {c:?}"
);
}
}
}
let both = FingerStatus::NEEDED | FingerStatus::PRESENT;
assert_eq!(both.bits(), 0b11);
assert!(!FingerStatus::NEEDED.contains(FingerStatus::PRESENT));
}
#[test]
fn finger_status_bitand_sub_not_obey_the_set_laws() {
const THREE: [FingerStatus; 4] = [
FingerStatus::NONE,
FingerStatus::NEEDED,
FingerStatus::PRESENT,
FingerStatus(0b11),
];
let all = FingerStatus::NEEDED | FingerStatus::PRESENT;
assert_eq!(FingerStatus::from_bits_truncate(0xFF), all);
assert_eq!(FingerStatus::from_bits_truncate(0b100), FingerStatus::NONE);
for a in THREE {
assert_eq!(
FingerStatus::from_bits_truncate(a.bits()),
a,
"round-trip {a:?}"
);
assert!((a & !a).is_empty(), "a & !a must be empty for {a:?}");
assert_eq!(a | !a, all, "a | !a must be every defined flag for {a:?}");
assert_eq!(!!a, a, "double complement is identity for {a:?}");
assert_eq!(a.is_empty(), a == FingerStatus::NONE, "is_empty for {a:?}");
for b in THREE {
assert_eq!(a & b, b & a, "intersection commutes for {a:?}, {b:?}");
assert_eq!(
a.intersects(b),
!(a & b).is_empty(),
"intersects for {a:?}, {b:?}"
);
assert_eq!(a - b, a & !b, "a - b == a & !b for {a:?}, {b:?}");
let mut acc = a;
acc &= b;
assert_eq!(acc, a & b, "&= agrees for {a:?}, {b:?}");
acc = a;
acc -= b;
assert_eq!(acc, a - b, "-= agrees for {a:?}, {b:?}");
}
}
}
}