examples: Make possible to select the finger to enroll/verify

Move some common functions to an utilities file.
This commit is contained in:
Marco Trevisan (Treviño) 2019-11-20 14:29:46 +01:00 committed by Benjamin Berg
parent f2b932960e
commit ab804f7f49
6 changed files with 169 additions and 59 deletions

View file

@ -1,6 +1,6 @@
/*
* Example fingerprint enrollment program
* Enrolls your right index finger and saves the print to disk
* Enrolls your choosen finger and saves the print to disk
* Copyright (C) 2007 Daniel Drake <dsd@gentoo.org>
* Copyright (C) 2019 Marco Trevisan <marco.trevisan@canonical.com>
*
@ -23,10 +23,12 @@
#include <libfprint/fprint.h>
#include "storage.h"
#include "utilities.h"
typedef struct _EnrollData
{
GMainLoop *loop;
FpFinger finger;
int ret_value;
} EnrollData;
@ -82,7 +84,7 @@ on_enroll_completed (FpDevice *dev, GAsyncResult *res, void *user_data)
if (!fp_device_has_storage (dev))
{
g_debug ("Device has not storage, saving locally");
int r = print_data_save (print, FP_FINGER_RIGHT_INDEX);
int r = print_data_save (print, enroll_data->finger);
if (r < 0)
{
g_warning ("Data save failed, code %d", r);
@ -139,11 +141,12 @@ on_device_opened (FpDevice *dev, GAsyncResult *res, void *user_data)
}
printf ("Opened device. It's now time to enroll your finger.\n\n");
printf ("You will need to successfully scan your finger %d times to "
"complete the process.\n\n", fp_device_get_nr_enroll_stages (dev));
printf ("You will need to successfully scan your %s finger %d times to "
"complete the process.\n\n", finger_to_string (enroll_data->finger),
fp_device_get_nr_enroll_stages (dev));
printf ("Scan your finger now.\n");
print_template = print_create_template (dev, FP_FINGER_RIGHT_INDEX);
print_template = print_create_template (dev, enroll_data->finger);
fp_device_enroll (dev, print_template, NULL, on_enroll_progress, NULL,
NULL, (GAsyncReadyCallback) on_enroll_completed,
enroll_data);
@ -156,13 +159,23 @@ main (void)
g_autoptr(EnrollData) enroll_data = NULL;
GPtrArray *devices;
FpDevice *dev;
FpFinger finger;
printf ("This program will enroll your right index finger, "
"unconditionally overwriting any right-index print that was enrolled "
g_print ("This program will enroll the selected finger, unconditionally "
"overwriting any print for the same finger that was enrolled "
"previously. If you want to continue, press enter, otherwise hit "
"Ctrl+C\n");
getchar ();
g_print ("Choose the finger to enroll:\n");
finger = finger_chooser ();
if (finger == FP_FINGER_UNKNOWN)
{
g_warning ("Unknown finger selected");
return EXIT_FAILURE;
}
setenv ("G_MESSAGES_DEBUG", "all", 0);
ctx = fp_context_new ();
@ -182,6 +195,7 @@ main (void)
}
enroll_data = g_new0 (EnrollData, 1);
enroll_data->finger = finger;
enroll_data->ret_value = EXIT_FAILURE;
enroll_data->loop = g_main_loop_new (NULL, FALSE);

View file

@ -21,6 +21,7 @@
#include <stdio.h>
#include <libfprint/fprint.h>
#include "utilities.h"
typedef struct _ListData
{
@ -68,46 +69,6 @@ on_device_closed (FpDevice *dev,
g_main_loop_quit (list_data->loop);
}
const char *
finger_to_string (FpFinger finger)
{
switch (finger)
{
case FP_FINGER_LEFT_THUMB:
return "left thumb";
case FP_FINGER_LEFT_INDEX:
return "left index";
case FP_FINGER_LEFT_MIDDLE:
return "left middle";
case FP_FINGER_LEFT_RING:
return "left ring";
case FP_FINGER_LEFT_LITTLE:
return "left little";
case FP_FINGER_RIGHT_THUMB:
return "right thumb";
case FP_FINGER_RIGHT_INDEX:
return "right index";
case FP_FINGER_RIGHT_MIDDLE:
return "right middle";
case FP_FINGER_RIGHT_RING:
return "right ring";
case FP_FINGER_RIGHT_LITTLE:
return "right little";
default:
return "unknown";
}
}
typedef struct _DeleteData
{
ListData *list_data;
@ -218,7 +179,7 @@ on_list_completed (FpDevice *dev,
{
gint64 idx = 0;
g_print ("Want to delete saved print? [<number>/A/n]\n");
g_print ("Want to delete saved print? [<number>/A/n]\n> ");
if (fgets (buf, 3, stdin))
idx = g_ascii_strtoll (buf, NULL, 10);

View file

@ -2,7 +2,7 @@
examples = [ 'enroll', 'verify', 'manage-prints' ]
foreach example: examples
executable(example,
[example + '.c', 'storage.c'],
[example + '.c', 'storage.c', 'utilities.c'],
dependencies: [libfprint_dep, glib_dep],
include_directories: [
root_inc,

94
examples/utilities.c Normal file
View file

@ -0,0 +1,94 @@
/*
* Utilities for example programs
*
* Copyright (C) 2019 Marco Trevisan <marco.trevisan@canonical.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <libfprint/fprint.h>
#include <stdio.h>
#include "utilities.h"
const char *
finger_to_string (FpFinger finger)
{
switch (finger)
{
case FP_FINGER_LEFT_THUMB:
return "left thumb";
case FP_FINGER_LEFT_INDEX:
return "left index";
case FP_FINGER_LEFT_MIDDLE:
return "left middle";
case FP_FINGER_LEFT_RING:
return "left ring";
case FP_FINGER_LEFT_LITTLE:
return "left little";
case FP_FINGER_RIGHT_THUMB:
return "right thumb";
case FP_FINGER_RIGHT_INDEX:
return "right index";
case FP_FINGER_RIGHT_MIDDLE:
return "right middle";
case FP_FINGER_RIGHT_RING:
return "right ring";
case FP_FINGER_RIGHT_LITTLE:
return "right little";
default:
return "unknown";
}
}
FpFinger
finger_chooser (void)
{
int i;
const FpFinger all_fingers[] = {
FP_FINGER_LEFT_THUMB,
FP_FINGER_LEFT_INDEX,
FP_FINGER_LEFT_MIDDLE,
FP_FINGER_LEFT_RING,
FP_FINGER_LEFT_LITTLE,
FP_FINGER_RIGHT_THUMB,
FP_FINGER_RIGHT_INDEX,
FP_FINGER_RIGHT_MIDDLE,
FP_FINGER_RIGHT_RING,
FP_FINGER_RIGHT_LITTLE,
};
for (i = all_fingers[0]; i <= G_N_ELEMENTS (all_fingers); ++i)
g_print(" [%d] %s\n", (i - all_fingers[0]), finger_to_string (i));
g_print ("> ");
if (!scanf ("%d%*c", &i))
return FP_FINGER_UNKNOWN;
if (i < 0 || i >= G_N_ELEMENTS (all_fingers))
return FP_FINGER_UNKNOWN;
return all_fingers[i];
}

27
examples/utilities.h Normal file
View file

@ -0,0 +1,27 @@
/*
* Trivial storage driver for example programs
*
* Copyright (C) 2019 Marco Trevisan <marco.trevisan@canonical.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef __UTILITIES_H
#define __UTILITIES_H
FpFinger finger_chooser (void);
const char * finger_to_string (FpFinger finger);
#endif /* __UTILITIES_H */

View file

@ -1,5 +1,5 @@
/*
* Example fingerprint verification program, which verifies the right index
* Example fingerprint verification program, which verifies the
* finger which has been previously enrolled to disk.
* Copyright (C) 2007 Daniel Drake <dsd@gentoo.org>
* Copyright (C) 2019 Marco Trevisan <marco.trevisan@canonical.com>
@ -23,10 +23,12 @@
#include <libfprint/fprint.h>
#include "storage.h"
#include "utilities.h"
typedef struct _VerifyData
{
GMainLoop *loop;
FpFinger finger;
int ret_value;
} VerifyData;
@ -134,7 +136,7 @@ on_list_completed (FpDevice *dev, GAsyncResult *res, gpointer user_data)
{
FpPrint *print = prints->pdata[i];
if (fp_print_get_finger (print) == FP_FINGER_RIGHT_INDEX &&
if (fp_print_get_finger (print) == verify_data->finger &&
g_strcmp0 (fp_print_get_username (print), g_get_user_name ()) == 0)
{
if (!verify_print ||
@ -146,8 +148,8 @@ on_list_completed (FpDevice *dev, GAsyncResult *res, gpointer user_data)
if (!verify_print)
{
g_warning ("Did you remember to enroll your right index "
"finger first?");
g_warning ("Did you remember to enroll your %s finger first?",
finger_to_string (verify_data->finger));
g_main_loop_quit (verify_data->loop);
return;
}
@ -170,6 +172,17 @@ on_list_completed (FpDevice *dev, GAsyncResult *res, gpointer user_data)
static void
start_verification (FpDevice *dev, VerifyData *verify_data)
{
g_print ("Choose the finger to verify:\n");
verify_data->finger = finger_chooser ();
if (verify_data->finger == FP_FINGER_UNKNOWN)
{
g_warning ("Unknown finger selected");
verify_data->ret_value = EXIT_FAILURE;
g_main_loop_quit (verify_data->loop);
return;
}
if (fp_device_has_storage (dev))
{
g_print ("Creating finger template, using device storage...\n");
@ -179,16 +192,17 @@ start_verification (FpDevice *dev, VerifyData *verify_data)
}
else
{
g_print ("Loading previously enrolled right index finger data...\n");
g_print ("Loading previously enrolled %s finger data...\n",
finger_to_string (verify_data->finger));
g_autoptr(FpPrint) verify_print;
verify_print = print_data_load (dev, FP_FINGER_RIGHT_INDEX);
verify_print = print_data_load (dev, verify_data->finger);
if (!verify_print)
{
g_warning ("Failed to load fingerprint data");
g_warning ("Did you remember to enroll your right index "
"finger first?");
g_warning ("Did you remember to enroll your %s finger first?",
finger_to_string (verify_data->finger));
g_main_loop_quit (verify_data->loop);
return;
}