2007-10-16 13:23:30 +00:00
|
|
|
/*
|
|
|
|
* Example fingerprint enrollment program
|
2020-04-14 11:49:17 +00:00
|
|
|
* Enrolls your chosen finger and saves the print to disk
|
2007-10-16 13:23:30 +00:00
|
|
|
* Copyright (C) 2007 Daniel Drake <dsd@gentoo.org>
|
2019-11-19 19:18:13 +00:00
|
|
|
* Copyright (C) 2019 Marco Trevisan <marco.trevisan@canonical.com>
|
2007-10-16 13:23:30 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2020-01-16 13:37:07 +00:00
|
|
|
#define FP_COMPONENT "example-enroll"
|
|
|
|
|
2007-10-16 13:23:30 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <libfprint/fprint.h>
|
2020-04-20 15:45:36 +00:00
|
|
|
#include <glib-unix.h>
|
2007-10-16 13:23:30 +00:00
|
|
|
|
2019-06-13 13:10:59 +00:00
|
|
|
#include "storage.h"
|
2019-11-20 13:29:46 +00:00
|
|
|
#include "utilities.h"
|
2019-06-13 13:10:59 +00:00
|
|
|
|
2019-11-19 20:13:11 +00:00
|
|
|
typedef struct _EnrollData
|
|
|
|
{
|
2020-04-20 15:45:36 +00:00
|
|
|
GMainLoop *loop;
|
|
|
|
GCancellable *cancellable;
|
|
|
|
unsigned int sigint_handler;
|
|
|
|
FpFinger finger;
|
|
|
|
int ret_value;
|
2019-11-19 19:18:13 +00:00
|
|
|
} EnrollData;
|
|
|
|
|
|
|
|
static void
|
|
|
|
enroll_data_free (EnrollData *enroll_data)
|
|
|
|
{
|
2020-04-20 15:45:36 +00:00
|
|
|
g_clear_handle_id (&enroll_data->sigint_handler, g_source_remove);
|
|
|
|
g_clear_object (&enroll_data->cancellable);
|
2019-11-19 20:13:11 +00:00
|
|
|
g_main_loop_unref (enroll_data->loop);
|
|
|
|
g_free (enroll_data);
|
2019-11-19 19:18:13 +00:00
|
|
|
}
|
|
|
|
G_DEFINE_AUTOPTR_CLEANUP_FUNC (EnrollData, enroll_data_free)
|
|
|
|
|
|
|
|
static void
|
2019-11-19 20:13:11 +00:00
|
|
|
on_device_closed (FpDevice *dev, GAsyncResult *res, void *user_data)
|
|
|
|
{
|
|
|
|
EnrollData *enroll_data = user_data;
|
2007-10-16 13:23:30 +00:00
|
|
|
|
2019-11-19 20:13:11 +00:00
|
|
|
g_autoptr(GError) error = NULL;
|
2007-10-16 13:23:30 +00:00
|
|
|
|
2019-11-19 20:13:11 +00:00
|
|
|
fp_device_close_finish (dev, res, &error);
|
2019-05-01 14:04:27 +00:00
|
|
|
|
2019-11-19 20:13:11 +00:00
|
|
|
if (error)
|
2020-09-29 08:42:03 +00:00
|
|
|
g_warning ("Failed closing device %s", error->message);
|
2019-11-19 20:13:11 +00:00
|
|
|
|
|
|
|
g_main_loop_quit (enroll_data->loop);
|
2019-11-19 19:18:13 +00:00
|
|
|
}
|
2007-11-13 20:46:15 +00:00
|
|
|
|
2019-11-19 19:18:13 +00:00
|
|
|
static void
|
2019-11-19 20:13:11 +00:00
|
|
|
on_enroll_completed (FpDevice *dev, GAsyncResult *res, void *user_data)
|
|
|
|
{
|
|
|
|
EnrollData *enroll_data = user_data;
|
|
|
|
|
|
|
|
g_autoptr(FpPrint) print = NULL;
|
|
|
|
g_autoptr(GError) error = NULL;
|
|
|
|
|
|
|
|
print = fp_device_enroll_finish (dev, res, &error);
|
|
|
|
|
|
|
|
if (!error)
|
|
|
|
{
|
|
|
|
enroll_data->ret_value = EXIT_SUCCESS;
|
|
|
|
|
|
|
|
if (!fp_device_has_storage (dev))
|
|
|
|
{
|
|
|
|
g_debug ("Device has not storage, saving locally");
|
2019-11-20 13:29:46 +00:00
|
|
|
int r = print_data_save (print, enroll_data->finger);
|
2019-11-19 20:13:11 +00:00
|
|
|
if (r < 0)
|
|
|
|
{
|
|
|
|
g_warning ("Data save failed, code %d", r);
|
|
|
|
enroll_data->ret_value = EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-09-29 08:42:03 +00:00
|
|
|
g_warning ("Enroll failed with error %s", error->message);
|
2019-11-19 20:13:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fp_device_close (dev, NULL, (GAsyncReadyCallback) on_device_closed,
|
|
|
|
enroll_data);
|
2019-11-19 19:18:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
on_enroll_progress (FpDevice *device,
|
2019-11-19 20:13:11 +00:00
|
|
|
gint completed_stages,
|
|
|
|
FpPrint *print,
|
|
|
|
gpointer user_data,
|
|
|
|
GError *error)
|
2019-11-19 19:18:13 +00:00
|
|
|
{
|
2019-11-19 20:13:11 +00:00
|
|
|
if (error)
|
|
|
|
{
|
|
|
|
g_warning ("Enroll stage %d of %d failed with error %s",
|
|
|
|
completed_stages,
|
|
|
|
fp_device_get_nr_enroll_stages (device),
|
|
|
|
error->message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-17 12:00:19 +00:00
|
|
|
if (print && fp_print_get_image (print) &&
|
2019-11-19 20:13:11 +00:00
|
|
|
print_image_save (print, "enrolled.pgm"))
|
|
|
|
printf ("Wrote scanned image to enrolled.pgm\n");
|
|
|
|
|
|
|
|
printf ("Enroll stage %d of %d passed. Yay!\n", completed_stages,
|
|
|
|
fp_device_get_nr_enroll_stages (device));
|
2019-11-19 19:18:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
on_device_opened (FpDevice *dev, GAsyncResult *res, void *user_data)
|
|
|
|
{
|
2019-11-19 20:13:11 +00:00
|
|
|
EnrollData *enroll_data = user_data;
|
|
|
|
FpPrint *print_template;
|
|
|
|
|
|
|
|
g_autoptr(GError) error = NULL;
|
|
|
|
|
|
|
|
if (!fp_device_open_finish (dev, res, &error))
|
|
|
|
{
|
|
|
|
g_warning ("Failed to open device: %s", error->message);
|
|
|
|
g_main_loop_quit (enroll_data->loop);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf ("Opened device. It's now time to enroll your finger.\n\n");
|
2019-11-20 13:29:46 +00:00
|
|
|
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));
|
2019-11-19 20:13:11 +00:00
|
|
|
printf ("Scan your finger now.\n");
|
|
|
|
|
2019-11-20 13:29:46 +00:00
|
|
|
print_template = print_create_template (dev, enroll_data->finger);
|
2020-04-20 15:45:36 +00:00
|
|
|
fp_device_enroll (dev, print_template, enroll_data->cancellable,
|
|
|
|
on_enroll_progress, NULL, NULL,
|
|
|
|
(GAsyncReadyCallback) on_enroll_completed,
|
2019-11-19 20:13:11 +00:00
|
|
|
enroll_data);
|
2007-10-16 13:23:30 +00:00
|
|
|
}
|
|
|
|
|
2020-04-20 15:45:36 +00:00
|
|
|
static gboolean
|
|
|
|
sigint_cb (void *user_data)
|
|
|
|
{
|
|
|
|
EnrollData *enroll_data = user_data;
|
|
|
|
|
|
|
|
g_cancellable_cancel (enroll_data->cancellable);
|
|
|
|
|
|
|
|
return G_SOURCE_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2019-11-19 20:13:11 +00:00
|
|
|
int
|
|
|
|
main (void)
|
2007-10-16 13:23:30 +00:00
|
|
|
{
|
2019-11-19 20:13:11 +00:00
|
|
|
g_autoptr(FpContext) ctx = NULL;
|
|
|
|
g_autoptr(EnrollData) enroll_data = NULL;
|
|
|
|
GPtrArray *devices;
|
|
|
|
FpDevice *dev;
|
2019-11-20 13:29:46 +00:00
|
|
|
FpFinger finger;
|
2007-10-16 13:23:30 +00:00
|
|
|
|
2019-11-20 13:29:46 +00:00
|
|
|
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");
|
2019-11-19 20:13:11 +00:00
|
|
|
getchar ();
|
2007-10-16 13:23:30 +00:00
|
|
|
|
2019-11-20 13:29:46 +00:00
|
|
|
g_print ("Choose the finger to enroll:\n");
|
|
|
|
finger = finger_chooser ();
|
|
|
|
|
|
|
|
if (finger == FP_FINGER_UNKNOWN)
|
|
|
|
{
|
|
|
|
g_warning ("Unknown finger selected");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2019-11-19 20:13:11 +00:00
|
|
|
setenv ("G_MESSAGES_DEBUG", "all", 0);
|
2007-10-16 13:23:30 +00:00
|
|
|
|
2019-11-19 20:13:11 +00:00
|
|
|
ctx = fp_context_new ();
|
2007-10-16 13:23:30 +00:00
|
|
|
|
2019-11-19 20:13:11 +00:00
|
|
|
devices = fp_context_get_devices (ctx);
|
|
|
|
if (!devices)
|
|
|
|
{
|
|
|
|
g_warning ("Impossible to get devices");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2007-10-16 13:23:30 +00:00
|
|
|
|
2019-11-19 20:13:11 +00:00
|
|
|
dev = discover_device (devices);
|
|
|
|
if (!dev)
|
|
|
|
{
|
|
|
|
g_warning ("No devices detected.");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2007-10-16 13:23:30 +00:00
|
|
|
|
2019-11-19 20:13:11 +00:00
|
|
|
enroll_data = g_new0 (EnrollData, 1);
|
2019-11-20 13:29:46 +00:00
|
|
|
enroll_data->finger = finger;
|
2019-11-19 20:13:11 +00:00
|
|
|
enroll_data->ret_value = EXIT_FAILURE;
|
|
|
|
enroll_data->loop = g_main_loop_new (NULL, FALSE);
|
2020-04-20 15:45:36 +00:00
|
|
|
enroll_data->cancellable = g_cancellable_new ();
|
|
|
|
enroll_data->sigint_handler = g_unix_signal_add_full (G_PRIORITY_HIGH,
|
|
|
|
SIGINT,
|
|
|
|
sigint_cb,
|
|
|
|
enroll_data,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
fp_device_open (dev, enroll_data->cancellable,
|
|
|
|
(GAsyncReadyCallback) on_device_opened,
|
2019-11-19 20:13:11 +00:00
|
|
|
enroll_data);
|
2007-10-16 13:23:30 +00:00
|
|
|
|
2019-11-19 20:13:11 +00:00
|
|
|
g_main_loop_run (enroll_data->loop);
|
2007-10-16 13:23:30 +00:00
|
|
|
|
2019-11-19 20:13:11 +00:00
|
|
|
return enroll_data->ret_value;
|
2019-11-19 19:18:13 +00:00
|
|
|
}
|