Struct libfprint_rs::FpDevice
source · pub struct FpDevice<'a> { /* private fields */ }
Expand description
Fingerpint device routines. You can interact with fingerprint devices using this struct.
Examples:
use libfprint_rs::FpContext;
let context = FpContext::new();
let devices = context.get_devices();
let device = devices.iter().next().unwrap();
device.open().unwrap();
let name = device.get_name().unwrap();
println!("Device name: {}", name);
Implementations§
source§impl<'a> FpDevice<'a>
impl<'a> FpDevice<'a>
sourcepub fn identify<T>(
&self,
prints: Vec<FpPrint<'_>>,
callback_fn: Option<FpMatchCb<T>>,
user_data: Option<T>,
new_print: Option<&mut FpPrint<'_>>
) -> Result<Option<FpPrint<'a>>, GError<'static>>
pub fn identify<T>( &self, prints: Vec<FpPrint<'_>>, callback_fn: Option<FpMatchCb<T>>, user_data: Option<T>, new_print: Option<&mut FpPrint<'_>> ) -> Result<Option<FpPrint<'a>>, GError<'static>>
Identify a print from a given vector of prints.
Example:
use libfprint_rs::{device::FpDevice, error::GError, print::FpPrint, context::FpContext};
let ctx = FpContext::new();
let dev = ctx.get_devices().iter().next().unwrap();
fn callback_function(
device: &FpDevice, // The fingerprint scanner device
matched_print: Option<FpPrint>, // The matched print, if any.
new_print: FpPrint, // New print scanned.
error: Option<GError>, // Optinal error in case of an error.
match_data: &Option<()> // User data can be any data type.
)
{
if matched_print.is_some() {
println!("Found matched print!");
}
}
let prints: Vec<FpPrint> = function_to_get_prints(&dev);
let matched_print = dev.identify(prints, Some(callback_function), None::<()>, None).unwrap();
if matched_print.is_some() {
println!("Found matched print");
}
Example with mutation
If you want to mutate the data inside match_data
you can use Rc<RefCell<T>>
:
use std::{cell::RefCell, rc::Rc};
use libfprint_rs::{device::FpDevice, error::GError, print::FpPrint, context::FpContext};
let ctx = FpContext::new();
let dev = ctx.get_devices().iter().next().unwrap();
struct UserData {
count: u32,
name: String,
}
fn callback_fn(
device: &FpDevice, // The fingerprint scanner device
matched_print: Option<FpPrint>, // The matched print, if any.
new_print: FpPrint, // New print scanned.
error: Option<GError>, // Optinal error in case of an error.
match_data: &Option<Rc<RefCell<UserData>>>, // User data can be any data type.
)
{
if let Some(user_data) = match_data {
user_data.borrow_mut().count += 1;
// Mutate the user data
}
println!("Found matched print!");
}
let user_data = Rc::new(RefCell::new(UserData { count: 304, name: "Donda".into() }));
let matched_print = dev.identify(prints, Some(callback_fn), Some(user_data), None).unwrap();
if matched_print.is_some() {
println!("Found matched print");
}
println!("{}", user_data.borrow().count);
sourcepub fn open(&self) -> Result<(), GError<'static>>
pub fn open(&self) -> Result<(), GError<'static>>
Open the device.
Examples found in repository?
More examples
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
fn verify<'a>(dev: FpDevice<'a>) -> Result<bool, GError<'a>> {
if !dev.is_open() {
dev.open()?;
};
let mut buf = Vec::new();
let mut raw_print = File::open("examples/print").unwrap();
raw_print.read_to_end(&mut buf).unwrap();
let enrolled_print = FpPrint::deserialize(&buf).unwrap();
let mut scanned_print = FpPrint::new(&dev);
let matched = dev
.verify(
enrolled_print,
Some(verify_callback),
None,
Some(&mut scanned_print),
)
.unwrap();
Ok(matched)
}
sourcepub fn enroll<T>(
&self,
template_print: FpPrint<'_>,
callback_fn: Option<FpEnrollProgress<T>>,
user_data: Option<T>
) -> Result<FpPrint<'static>, GError<'static>>
pub fn enroll<T>( &self, template_print: FpPrint<'_>, callback_fn: Option<FpEnrollProgress<T>>, user_data: Option<T> ) -> Result<FpPrint<'static>, GError<'static>>
Enroll a new print. Template print is a print with relevant metadata filled in.
Example:
use libfprint_rs::{device::FpDevice, error::GError, print::FpPrint, context::FpContext};
let ctx = FpContext::new();
let dev = ctx.get_devices().iter().next().unwrap();
fn callback_fn(
device: &FpDevice,
completed_stages: i32,
print: FpPrint,
error: Option<GError>,
user_data: &Option<()>
) {
if error.is_none() {
println!("Enrolling: {} of {}", completed_stages, device.get_nr_enroll_stages() );
}
}
dev.open().unwrap();
let template_print = FpPrint::new(&dev);
dev.enroll(template_print, Some(callback_fn), None::<()>);
Example with mutation
If you want to mutate the data inside user_data
you can use Rc<RefCell<T>>
:
use std::{cell::RefCell, rc::Rc};
use libfprint_rs::{device::FpDevice, error::GError, print::FpPrint, context::FpContext};
let ctx = FpContext::new();
let dev = ctx.get_devices().iter().next().unwrap();
struct UserData {
count: u32,
name: String,
}
fn callback_fn(
device: &FpDevice,
completed_stages: i32,
print: FpPrint,
error: Option<GError>,
user_data: &Option<Rc<RefCell<UserData>>>
) {
if let Some(user_data) = user_data {
user_data.borrow_mut().count += 1;
// Mutate the user data
}
println!("Enrolling: {} of {}", completed_stages, device.get_nr_enroll_stages() );
}
let user_data = Rc::new(RefCell::new(UserData { count: 304, name: "Donda".into() }));
let template_print = FpPrint::new(&dev);
dev.enroll(template_print, Some(callback_fn), Some(user_data));
println!("{}", user_data.borrow().count);
sourcepub fn verify<T>(
&self,
enrolled_print: FpPrint<'_>,
callback_fn: Option<FpMatchCb<T>>,
match_data: Option<T>,
scanned_print: Option<&mut FpPrint<'_>>
) -> Result<bool, GError<'static>>
pub fn verify<T>( &self, enrolled_print: FpPrint<'_>, callback_fn: Option<FpMatchCb<T>>, match_data: Option<T>, scanned_print: Option<&mut FpPrint<'_>> ) -> Result<bool, GError<'static>>
Verify a given print.
Example:
use libfprint_rs::{device::FpDevice, error::GError, print::FpPrint, context::FpContext};
let ctx = FpContext::new();
let dev = ctx.get_devices().iter().next().unwrap();
fn match_callback_fn(
device: &FpDevice, // The fingerprint scanner device
matched_print: Option<FpPrint>, // The matched print, if any.
new_print: FpPrint, // New print scanned.
error: Option<GError>, // Error, if any.
match_data: &Option<()> // User data to pass to this function.
)
{
if matched_print.is_some() {
println!("Found matched print!");
}
}
// This dummy fn gets an existing print
let enrolled_print = get_enrolled_print();
// Where we saved the new scanned print
let mut scanned_print = FpPrint::new(&dev);
let matched_res = dev.verify(enrolled_print, Some(match_callback_fn), None::<()>, Some(&mut scanned_print)).unwrap();
if matched_res {
println!("Found matched print");
}
Examples found in repository?
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
fn verify<'a>(dev: FpDevice<'a>) -> Result<bool, GError<'a>> {
if !dev.is_open() {
dev.open()?;
};
let mut buf = Vec::new();
let mut raw_print = File::open("examples/print").unwrap();
raw_print.read_to_end(&mut buf).unwrap();
let enrolled_print = FpPrint::deserialize(&buf).unwrap();
let mut scanned_print = FpPrint::new(&dev);
let matched = dev
.verify(
enrolled_print,
Some(verify_callback),
None,
Some(&mut scanned_print),
)
.unwrap();
Ok(matched)
}
sourcepub fn get_nr_enroll_stages(&self) -> i32
pub fn get_nr_enroll_stages(&self) -> i32
Retrieves the number of enroll stages for this device.
Examples found in repository?
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
fn enroll_callback(
device: &FpDevice,
completed_stages: i32,
_print: FpPrint,
error: Option<GError>,
_user_data: &Option<()>,
) {
println!(
"Enrolling ... {} of {}",
completed_stages,
device.get_nr_enroll_stages()
);
if let Some(err) = error {
eprintln!("Error: {}", err);
}
}
sourcepub fn get_finger_status(&self) -> FpFingerStatusFlags
pub fn get_finger_status(&self) -> FpFingerStatusFlags
Retrieves the finger status flags for the device. This can be used by the UI to present the relevant feedback, although it is not guaranteed to be a relevant value when not performing any action.
pub fn get_features(&self) -> Vec<FpDeviceFeature>
pub fn get_driver(&self) -> &str
pub fn get_device_id(&self) -> &str
sourcepub fn get_scan_type(&self) -> FpScanType
pub fn get_scan_type(&self) -> FpScanType
Retrieves the scan type of the device (FpScanType
)
sourcepub fn has_feature(&self, features: FpDeviceFeature) -> bool
pub fn has_feature(&self, features: FpDeviceFeature) -> bool
Checks if the FpDevice
supports the requested FpDeviceFeature
.
sourcepub fn is_open(&self) -> bool
pub fn is_open(&self) -> bool
Examples found in repository?
More examples
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
fn verify<'a>(dev: FpDevice<'a>) -> Result<bool, GError<'a>> {
if !dev.is_open() {
dev.open()?;
};
let mut buf = Vec::new();
let mut raw_print = File::open("examples/print").unwrap();
raw_print.read_to_end(&mut buf).unwrap();
let enrolled_print = FpPrint::deserialize(&buf).unwrap();
let mut scanned_print = FpPrint::new(&dev);
let matched = dev
.verify(
enrolled_print,
Some(verify_callback),
None,
Some(&mut scanned_print),
)
.unwrap();
Ok(matched)
}
sourcepub fn delete_print(&self, print: FpPrint<'_>) -> Result<(), GError<'_>>
pub fn delete_print(&self, print: FpPrint<'_>) -> Result<(), GError<'_>>
Delete a print from the device. This only makes sense on devices that store prints on-chip, but is safe to always call.
sourcepub fn clear_storage(&self) -> Result<(), GError<'_>>
pub fn clear_storage(&self) -> Result<(), GError<'_>>
Deletes all prints from the device. This only makes sense on devices that store prints on-chip, but is safe to always call.