2019-06-13 13:09:19 +00:00
|
|
|
/*
|
|
|
|
* Trivial storage driver for example programs
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019 Benjamin Berg <bberg@redhat.com>
|
2019-11-19 19:18:13 +00:00
|
|
|
* Copyright (C) 2019 Marco Trevisan <marco.trevisan@canonical.com>
|
2019-06-13 13:09:19 +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-storage"
|
|
|
|
|
2019-11-19 19:18:13 +00:00
|
|
|
#include <libfprint/fprint.h>
|
2020-01-30 13:59:59 +00:00
|
|
|
#include <libfprint/fpi-compat.h>
|
2019-12-04 12:13:40 +00:00
|
|
|
#include "storage.h"
|
2019-11-19 19:18:13 +00:00
|
|
|
|
|
|
|
#include <errno.h>
|
2019-06-13 13:09:19 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#define STORAGE_FILE "test-storage.variant"
|
|
|
|
|
|
|
|
static char *
|
2019-11-19 19:18:13 +00:00
|
|
|
get_print_data_descriptor (FpPrint *print, FpDevice *dev, FpFinger finger)
|
2019-06-13 13:09:19 +00:00
|
|
|
{
|
2019-11-19 20:13:11 +00:00
|
|
|
const char *driver;
|
|
|
|
const char *dev_id;
|
|
|
|
|
|
|
|
if (print)
|
|
|
|
{
|
|
|
|
driver = fp_print_get_driver (print);
|
|
|
|
dev_id = fp_print_get_device_id (print);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
driver = fp_device_get_driver (dev);
|
|
|
|
dev_id = fp_device_get_device_id (dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
return g_strdup_printf ("%s/%s/%x",
|
|
|
|
driver,
|
|
|
|
dev_id,
|
|
|
|
finger);
|
2019-06-13 13:09:19 +00:00
|
|
|
}
|
|
|
|
|
2019-11-19 19:18:13 +00:00
|
|
|
static GVariantDict *
|
2019-11-19 20:13:11 +00:00
|
|
|
load_data (void)
|
2019-06-13 13:09:19 +00:00
|
|
|
{
|
2019-11-19 20:13:11 +00:00
|
|
|
GVariantDict *res;
|
|
|
|
GVariant *var;
|
2020-01-02 17:38:19 +00:00
|
|
|
gchar *contents = NULL;
|
2019-12-03 18:36:26 +00:00
|
|
|
gsize length = 0;
|
2019-11-19 20:13:11 +00:00
|
|
|
|
|
|
|
if (!g_file_get_contents (STORAGE_FILE, &contents, &length, NULL))
|
|
|
|
{
|
|
|
|
g_warning ("Error loading storage, assuming it is empty");
|
|
|
|
return g_variant_dict_new (NULL);
|
|
|
|
}
|
|
|
|
|
2020-01-02 17:38:19 +00:00
|
|
|
var = g_variant_new_from_data (G_VARIANT_TYPE_VARDICT,
|
|
|
|
contents,
|
|
|
|
length,
|
|
|
|
FALSE,
|
|
|
|
g_free,
|
|
|
|
contents);
|
2019-11-19 20:13:11 +00:00
|
|
|
|
|
|
|
res = g_variant_dict_new (var);
|
|
|
|
g_variant_unref (var);
|
|
|
|
return res;
|
2019-06-13 13:09:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2019-11-19 20:13:11 +00:00
|
|
|
save_data (GVariant *data)
|
2019-06-13 13:09:19 +00:00
|
|
|
{
|
2019-11-19 20:13:11 +00:00
|
|
|
const gchar *contents = NULL;
|
|
|
|
gsize length;
|
2019-06-13 13:09:19 +00:00
|
|
|
|
2019-11-19 20:13:11 +00:00
|
|
|
length = g_variant_get_size (data);
|
|
|
|
contents = (gchar *) g_variant_get_data (data);
|
2019-06-13 13:09:19 +00:00
|
|
|
|
2019-11-19 20:13:11 +00:00
|
|
|
if (!g_file_set_contents (STORAGE_FILE, contents, length, NULL))
|
|
|
|
{
|
|
|
|
g_warning ("Error saving storage,!");
|
|
|
|
return -1;
|
|
|
|
}
|
2019-06-13 13:09:19 +00:00
|
|
|
|
2019-11-19 20:13:11 +00:00
|
|
|
g_variant_ref_sink (data);
|
|
|
|
g_variant_unref (data);
|
2019-06-13 13:09:19 +00:00
|
|
|
|
2019-11-19 20:13:11 +00:00
|
|
|
return 0;
|
2019-06-13 13:09:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2019-11-19 20:13:11 +00:00
|
|
|
print_data_save (FpPrint *print, FpFinger finger)
|
2019-06-13 13:09:19 +00:00
|
|
|
{
|
2019-11-19 20:13:11 +00:00
|
|
|
g_autofree gchar *descr = get_print_data_descriptor (print, NULL, finger);
|
|
|
|
|
|
|
|
g_autoptr(GError) error = NULL;
|
|
|
|
g_autoptr(GVariantDict) dict = NULL;
|
|
|
|
g_autofree guchar *data = NULL;
|
|
|
|
GVariant *val;
|
|
|
|
gsize size;
|
|
|
|
int res;
|
|
|
|
|
|
|
|
dict = load_data ();
|
|
|
|
|
|
|
|
fp_print_serialize (print, &data, &size, &error);
|
|
|
|
if (error)
|
|
|
|
{
|
|
|
|
g_warning ("Error serializing data: %s", error->message);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
val = g_variant_new_fixed_array (G_VARIANT_TYPE ("y"), data, size, 1);
|
|
|
|
g_variant_dict_insert_value (dict, descr, val);
|
|
|
|
|
|
|
|
res = save_data (g_variant_dict_end (dict));
|
|
|
|
|
|
|
|
return res;
|
2019-06-13 13:09:19 +00:00
|
|
|
}
|
|
|
|
|
2019-11-19 19:18:13 +00:00
|
|
|
FpPrint *
|
2019-11-19 20:13:11 +00:00
|
|
|
print_data_load (FpDevice *dev, FpFinger finger)
|
2019-06-13 13:09:19 +00:00
|
|
|
{
|
2019-11-19 20:13:11 +00:00
|
|
|
g_autofree gchar *descr = get_print_data_descriptor (NULL, dev, finger);
|
|
|
|
|
|
|
|
g_autoptr(GVariant) val = NULL;
|
|
|
|
g_autoptr(GVariantDict) dict = NULL;
|
2020-01-02 17:39:57 +00:00
|
|
|
const guchar *stored_data = NULL;
|
2019-11-19 20:13:11 +00:00
|
|
|
gsize stored_len;
|
2019-06-13 13:09:19 +00:00
|
|
|
|
2019-11-19 20:13:11 +00:00
|
|
|
dict = load_data ();
|
|
|
|
val = g_variant_dict_lookup_value (dict, descr, G_VARIANT_TYPE ("ay"));
|
2019-06-13 13:09:19 +00:00
|
|
|
|
2019-11-19 20:13:11 +00:00
|
|
|
if (val)
|
|
|
|
{
|
|
|
|
FpPrint *print;
|
|
|
|
g_autoptr(GError) error = NULL;
|
2019-11-19 19:18:13 +00:00
|
|
|
|
2020-01-02 17:39:57 +00:00
|
|
|
stored_data = (const guchar *) g_variant_get_fixed_array (val, &stored_len, 1);
|
2019-11-19 20:13:11 +00:00
|
|
|
print = fp_print_deserialize (stored_data, stored_len, &error);
|
2019-06-13 13:09:19 +00:00
|
|
|
|
2019-11-19 20:13:11 +00:00
|
|
|
if (error)
|
|
|
|
g_warning ("Error deserializing data: %s", error->message);
|
2019-11-19 19:18:13 +00:00
|
|
|
|
2019-11-19 20:13:11 +00:00
|
|
|
return print;
|
|
|
|
}
|
2019-06-13 13:09:19 +00:00
|
|
|
|
2019-11-19 20:13:11 +00:00
|
|
|
return NULL;
|
2019-11-19 19:18:13 +00:00
|
|
|
}
|
2019-06-13 13:09:19 +00:00
|
|
|
|
2019-11-19 19:18:13 +00:00
|
|
|
FpPrint *
|
|
|
|
print_create_template (FpDevice *dev, FpFinger finger)
|
|
|
|
{
|
2019-11-19 20:13:11 +00:00
|
|
|
g_autoptr(GDateTime) datetime = NULL;
|
2020-01-30 13:59:59 +00:00
|
|
|
g_autoptr(GDate) date = NULL;
|
2019-11-19 20:13:11 +00:00
|
|
|
FpPrint *template = NULL;
|
|
|
|
gint year, month, day;
|
|
|
|
|
|
|
|
template = fp_print_new (dev);
|
|
|
|
fp_print_set_finger (template, finger);
|
|
|
|
fp_print_set_username (template, g_get_user_name ());
|
|
|
|
datetime = g_date_time_new_now_local ();
|
|
|
|
g_date_time_get_ymd (datetime, &year, &month, &day);
|
|
|
|
date = g_date_new_dmy (day, month, year);
|
|
|
|
fp_print_set_enroll_date (template, date);
|
|
|
|
|
|
|
|
return template;
|
2019-11-19 19:18:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-07 06:04:26 +00:00
|
|
|
gboolean
|
2019-11-19 19:18:13 +00:00
|
|
|
save_image_to_pgm (FpImage *img, const char *path)
|
|
|
|
{
|
2019-11-19 20:13:11 +00:00
|
|
|
FILE *fd = fopen (path, "w");
|
|
|
|
size_t write_size;
|
|
|
|
const guchar *data = fp_image_get_data (img, &write_size);
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if (!fd)
|
|
|
|
{
|
|
|
|
g_warning ("could not open '%s' for writing: %d", path, errno);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = fprintf (fd, "P5 %d %d 255\n",
|
|
|
|
fp_image_get_width (img), fp_image_get_height (img));
|
|
|
|
if (r < 0)
|
|
|
|
{
|
|
|
|
fclose (fd);
|
|
|
|
g_critical ("pgm header write failed, error %d", r);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = fwrite (data, 1, write_size, fd);
|
|
|
|
if (r < write_size)
|
|
|
|
{
|
|
|
|
fclose (fd);
|
|
|
|
g_critical ("short write (%d)", r);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose (fd);
|
|
|
|
g_debug ("written to '%s'", path);
|
|
|
|
|
|
|
|
return TRUE;
|
2019-11-19 19:18:13 +00:00
|
|
|
}
|
|
|
|
|
2019-11-19 20:13:11 +00:00
|
|
|
gboolean
|
|
|
|
print_image_save (FpPrint *print, const char *path)
|
2019-11-19 19:18:13 +00:00
|
|
|
{
|
2020-01-02 17:42:18 +00:00
|
|
|
FpImage *img = NULL;
|
2019-11-19 19:18:13 +00:00
|
|
|
|
2019-11-19 20:13:11 +00:00
|
|
|
g_return_val_if_fail (FP_IS_PRINT (print), FALSE);
|
|
|
|
g_return_val_if_fail (path != NULL, FALSE);
|
2019-11-19 19:18:13 +00:00
|
|
|
|
2019-11-19 20:13:11 +00:00
|
|
|
img = fp_print_get_image (print);
|
2019-11-19 19:18:13 +00:00
|
|
|
|
2019-11-19 20:13:11 +00:00
|
|
|
if (img)
|
|
|
|
return save_image_to_pgm (img, path);
|
2019-11-19 19:18:13 +00:00
|
|
|
|
2019-11-19 20:13:11 +00:00
|
|
|
return FALSE;
|
2019-06-13 13:09:19 +00:00
|
|
|
}
|