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>

source

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);
source

pub fn get_name(&self) -> &str

Returns the name of the device.

source

pub fn open(&self) -> Result<(), GError<'static>>

Open the device.

Examples found in repository?
examples/enroll.rs (line 24)
22
23
24
25
26
27
28
29
30
31
fn enroll<'a>(dev: FpDevice<'a>) -> Result<FpPrint<'a>, GError<'static>> {
    if !dev.is_open() {
        dev.open()?;
    };

    let template_print = FpPrint::new(&dev);
    template_print.set_username("MyUsername");

    dev.enroll(template_print, Some(enroll_callback), None)
}
More examples
Hide additional examples
examples/verify.rs (line 27)
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)
}
source

pub fn close(&self) -> Result<(), GError<'static>>

Close the device.

source

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);
Examples found in repository?
examples/enroll.rs (line 30)
22
23
24
25
26
27
28
29
30
31
fn enroll<'a>(dev: FpDevice<'a>) -> Result<FpPrint<'a>, GError<'static>> {
    if !dev.is_open() {
        dev.open()?;
    };

    let template_print = FpPrint::new(&dev);
    template_print.set_username("MyUsername");

    dev.enroll(template_print, Some(enroll_callback), None)
}
source

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?
examples/verify.rs (lines 38-43)
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)
}
source

pub fn capture(&self) -> Result<FpImage, GError<'static>>

Start operation to capture an image.

source

pub fn get_nr_enroll_stages(&self) -> i32

Retrieves the number of enroll stages for this device.

Examples found in repository?
examples/enroll.rs (line 43)
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);
    }
}
source

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.

source

pub fn get_features(&self) -> Vec<FpDeviceFeature>

source

pub fn get_driver(&self) -> &str

source

pub fn get_device_id(&self) -> &str

source

pub fn get_scan_type(&self) -> FpScanType

Retrieves the scan type of the device (FpScanType)

source

pub fn has_feature(&self, features: FpDeviceFeature) -> bool

Checks if the FpDevice supports the requested FpDeviceFeature.

source

pub fn is_open(&self) -> bool

Examples found in repository?
examples/enroll.rs (line 23)
22
23
24
25
26
27
28
29
30
31
fn enroll<'a>(dev: FpDevice<'a>) -> Result<FpPrint<'a>, GError<'static>> {
    if !dev.is_open() {
        dev.open()?;
    };

    let template_print = FpPrint::new(&dev);
    template_print.set_username("MyUsername");

    dev.enroll(template_print, Some(enroll_callback), None)
}
More examples
Hide additional examples
examples/verify.rs (line 26)
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)
}
source

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.

source

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.

source

pub fn suspend(&self) -> Result<(), GError<'_>>

Prepare device for suspend.

source

pub fn resume(&self) -> Result<(), GError<'_>>

Resume device after suspend.

Trait Implementations§

source§

impl<'a> Clone for FpDevice<'a>

source§

fn clone(&self) -> FpDevice<'a>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl<'a> RefUnwindSafe for FpDevice<'a>

§

impl<'a> !Send for FpDevice<'a>

§

impl<'a> !Sync for FpDevice<'a>

§

impl<'a> Unpin for FpDevice<'a>

§

impl<'a> UnwindSafe for FpDevice<'a>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.