Fix verify/identify error handling
[libfprint.git] / libfprint / core.c
blob43d92c99e50dfd0d49f64df63bcac1876a30831f
1 /*
2 * Core functions for libfprint
3 * Copyright (C) 2007-2008 Daniel Drake <dsd@gentoo.org>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 #include <config.h>
21 #include <errno.h>
22 #include <stdio.h>
24 #include <glib.h>
25 #include <libusb.h>
27 #include "fp_internal.h"
29 /**
30 * \mainpage libfprint API Reference
31 * libfprint is an open source library to provide access to fingerprint
32 * scanning devices. For more info, see the
33 * <a href="http://www.reactivated.net/fprint/Libfprint">libfprint project
34 * homepage</a>.
36 * This documentation is aimed at application developers who wish to integrate
37 * fingerprint-related functionality into their software. libfprint has been
38 * designed so that you only have to do this once - by integrating your
39 * software with libfprint, you'll be supporting all the fingerprint readers
40 * that we have got our hands on. As such, the API is rather general (and
41 * therefore hopefully easy to comprehend!), and does it's best to hide the
42 * technical details that required to operate the hardware.
44 * This documentation is not aimed at developers wishing to develop and
45 * contribute fingerprint device drivers to libfprint.
47 * Feedback on this API and it's associated documentation is appreciated. Was
48 * anything unclear? Does anything seem unreasonably complicated? Is anything
49 * missing? Let us know on the
50 * <a href="http://www.reactivated.net/fprint/Mailing_list">mailing list</a>.
52 * \section enrollment Enrollment
54 * Before you dive into the API, it's worth introducing a couple of concepts.
56 * The process of enrolling a finger is where you effectively scan your
57 * finger for the purposes of teaching the system what your finger looks like.
58 * This means that you scan your fingerprint, then the system processes it and
59 * stores some data about your fingerprint to refer to later.
61 * \section verification Verification
63 * Verification is what most people think of when they think about fingerprint
64 * scanning. The process of verification is effectively performing a fresh
65 * fingerprint scan, and then comparing that scan to a finger that was
66 * previously enrolled.
68 * As an example scenario, verification can be used to implement what people
69 * would picture as fingerprint login (i.e. fingerprint replaces password).
70 * For example:
71 * - I enroll my fingerprint through some software that trusts I am who I say
72 * I am. This is a prerequisite before I can perform fingerprint-based
73 * login for my account.
74 * - Some time later, I want to login to my computer. I enter my username,
75 * but instead of prompting me for a password, it asks me to scan my finger.
76 * I scan my finger.
77 * - The system compares the finger I just scanned to the one that was
78 * enrolled earlier. If the system decides that the fingerprints match,
79 * I am successfully logged in. Otherwise, the system informs me that I am
80 * not authorised to login as that user.
82 * \section identification Identification
84 * Identification is the process of comparing a freshly scanned fingerprint
85 * to a <em>collection</em> of previously enrolled fingerprints. For example,
86 * imagine there are 100 people in an organisation, and they all have enrolled
87 * their fingerprints. One user walks up to a fingerprint scanner and scans
88 * their finger. With <em>no other knowledge</em> of who that user might be,
89 * the system examines their fingerprint, looks in the database, and determines
90 * that the user is user number #61.
92 * In other words, verification might be seen as a one-to-one fingerprint
93 * comparison where you know the identity of the user that you wish to
94 * authenticate, whereas identification is a one-to-many comparison where you
95 * do not know the identity of the user that you wish to authenticate.
97 * \section compat_general Device and print compatibility
98 * Moving off generic conceptual ideas and onto libfprint-specific
99 * implementation details, here are some introductory notes regarding how
100 * libfprint copes with compatibility of fingerprints.
102 * libfprint deals with a whole variety of different fingerprint readers and
103 * the design includes considerations of compatibility and interoperability
104 * between multiple devices. Your application should also be prepared to
105 * work with more than one type of fingerprint reader and should consider that
106 * enrolled fingerprint X may not be compatible with the device the user has
107 * plugged in today.
109 * libfprint implements the principle that fingerprints from different devices
110 * are not necessarily compatible. For example, different devices may see
111 * significantly different areas of fingerprint surface, and comparing images
112 * between the devices would be unreliable. Also, devices can stretch and
113 * distort images in different ways.
115 * libfprint also implements the principle that in some cases, fingerprints
116 * <em>are</em> compatible between different devices. If you go and buy two
117 * identical fingerprint readers, it seems logical that you should be able
118 * to enroll on one and verify on another without problems.
120 * libfprint takes a fairly simplistic approach to these issues. Internally,
121 * fingerprint hardware is driven by individual drivers. libfprint enforces
122 * that a fingerprint that came from a device backed by driver X is never
123 * compared to a fingerprint that came from a device backed by driver Y.
125 * Additionally, libfprint is designed for the situation where a single driver
126 * may support a range of devices which differ in imaging or scanning
127 * properties. For example, a driver may support two ranges of devices which
128 * even though are programmed over the same interface, one device sees
129 * substantially less of the finger flesh, therefore images from the two
130 * device types should be incompatible despite being from the same driver. To
131 * implement this, each driver assigns a <em>device type</em> to each device
132 * that it detects based on its imaging characteristics. libfprint ensures that
133 * two prints being compared have the same device type.
135 * In summary, libfprint represents fingerprints in several internal structures
136 * and each representation will offer you a way of determining the
137 * \ref driver_id "driver ID" and \ref devtype "devtype" of the print in
138 * question. Prints are only compatible if the driver ID <b>and</b> devtypes
139 * match. libfprint does offer you some "is this print compatible?" helper
140 * functions, so you don't have to worry about these details too much.
142 * \section sync Synchronity/asynchronity
144 * Currently, all data acquisition operations are synchronous and can
145 * potentially block for extended periods of time. For example, the enroll
146 * function will block for an unpredictable amount of time until the user
147 * scans their finger.
149 * Alternative asynchronous/non-blocking functionality will be offered in
150 * future but has not been implemented yet.
152 * \section getting_started Getting started
154 * libfprint includes several simple functional examples under the examples/
155 * directory in the libfprint source distribution. Those are good starting
156 * points.
158 * Usually the first thing you want to do is determine which fingerprint
159 * devices are present. This is done through \ref dscv_dev "device discovery".
161 * Once you have found a device you would like to operate, you should open it.
162 * Refer to \ref dev "device operations". This section also details enrollment,
163 * image capture, and verification.
166 * That should be enough to get you started, but do remember there are
167 * documentation pages on other aspects of libfprint's API (see the modules
168 * page).
171 /** @defgroup core Core library operations */
174 * @defgroup dev Device operations
175 * In order to interact with fingerprint scanners, your software will
176 * interface primarily with libfprint's representation of devices, detailed
177 * on this page.
179 * \section enrolling Enrolling
180 * Enrolling is represented within libfprint as a multi-stage process. This
181 * slightly complicates things for application developers, but is required
182 * for a smooth process.
184 * Some devices require the user to scan their finger multiple times in
185 * order to complete the enrollment process. libfprint must return control
186 * to your application inbetween each scan in order for your application to
187 * instruct the user to swipe their finger again. Each scan is referred to
188 * as a stage, so a device that requires 3 scans for enrollment corresponds
189 * to you running 3 enrollment stages using libfprint.
191 * The fp_dev_get_nr_enroll_stages() function can be used to find out how
192 * many enroll stages are needed.
194 * In order to complete an enroll stage, you call an enroll function such
195 * as fp_enroll_finger(). The return of this function does not necessarily
196 * indicate that a stage has completed though, as the user may not have
197 * produced a good enough scan. Each stage may have to be retried several
198 * times.
200 * The exact semantics of the enroll functions are described in the
201 * fp_enroll_finger() documentation. You should pay careful attention to the
202 * details.
204 * \section imaging Imaging
205 * libfprint provides you with some ways to retrieve images of scanned
206 * fingers, such as the fp_dev_img_capture() function, or some enroll/verify
207 * function variants which provide images. You may wish to do something with
208 * such images in your application.
210 * However, you must be aware that not all hardware supported by libfprint
211 * operates like this. Most hardware does operate simply by sending
212 * fingerprint images to the host computer for further processing, but some
213 * devices do all fingerprint processing in hardware and do not present images
214 * to the host computer.
216 * You can use fp_dev_supports_imaging() to see if image capture is possible
217 * on a particular device. Your application must be able to cope with the
218 * fact that libfprint does support regular operations (e.g. enrolling and
219 * verification) on some devices which do not provide images.
221 * \section devtype Devtypes
222 * Internally, the \ref drv "driver" behind a device assigns a 32-bit
223 * <em>devtype</em> identifier to the device. This cannot be used as a unique
224 * ID for a specific device as many devices under the same range may share
225 * the same devtype. The devtype may even be 0 in all cases.
227 * The only reason you may be interested in retrieving the devtype for a
228 * device is for the purpose of checking if some print data is compatible
229 * with a device. libfprint uses the devtype as one way of checking that the
230 * print you are verifying is compatible with the device in question - the
231 * devtypes must be equal. This effectively allows drivers to support more
232 * than one type of device where the data from each one is not compatible with
233 * the other. Note that libfprint does provide you with helper functions to
234 * determine whether a print is compatible with a device, so under most
235 * circumstances, you don't have to worry about devtypes at all.
238 /** @defgroup dscv_dev Device discovery
239 * These functions allow you to scan the system for supported fingerprint
240 * scanning hardware. This is your starting point when integrating libfprint
241 * into your software.
243 * When you've identified a discovered device that you would like to control,
244 * you can open it with fp_dev_open(). Note that discovered devices may no
245 * longer be available at the time when you want to open them, for example
246 * the user may have unplugged the device.
249 /** @defgroup drv Driver operations
250 * Internally, libfprint is abstracted into various drivers to communicate
251 * with the different types of supported fingerprint readers. libfprint works
252 * hard so that you don't have to care about these internal abstractions,
253 * however there are some situations where you may be interested in a little
254 * behind-the-scenes driver info.
256 * You can obtain the driver for a device using fp_dev_get_driver(), which
257 * you can pass to the functions documented on this page.
259 * \section driver_id Driver IDs
260 * Each driver is assigned a unique ID by the project maintainer. These
261 * assignments are
262 * <a href="http://www.reactivated.net/fprint/Driver_ID_assignments">
263 * documented on the wiki</a> and will never change.
265 * The only reason you may be interested in retrieving the driver ID for a
266 * driver is for the purpose of checking if some print data is compatible
267 * with a device. libfprint uses the driver ID as one way of checking that
268 * the print you are trying to verify is compatible with the device in
269 * question - it ensures that enrollment data from one driver is never fed to
270 * another. Note that libfprint does provide you with helper functions to
271 * determine whether a print is compatible with a device, so under most
272 * circumstances, you don't have to worry about driver IDs at all.
275 static GSList *registered_drivers = NULL;
276 static GSList *opened_devices = NULL;
278 void fpi_log(enum fpi_log_level level, const char *component,
279 const char *function, const char *format, ...)
281 va_list args;
282 FILE *stream = stdout;
283 const char *prefix;
285 switch (level) {
286 case LOG_LEVEL_INFO:
287 prefix = "info";
288 break;
289 case LOG_LEVEL_WARNING:
290 stream = stderr;
291 prefix = "warning";
292 break;
293 case LOG_LEVEL_ERROR:
294 stream = stderr;
295 prefix = "error";
296 break;
297 case LOG_LEVEL_DEBUG:
298 stream = stderr;
299 prefix = "debug";
300 break;
301 default:
302 stream = stderr;
303 prefix = "unknown";
304 break;
307 fprintf(stream, "%s:%s [%s] ", component ? component : "fp", prefix,
308 function);
310 va_start (args, format);
311 vfprintf(stream, format, args);
312 va_end (args);
314 fprintf(stream, "\n");
317 static void register_driver(struct fp_driver *drv)
319 if (drv->id == 0) {
320 fp_err("not registering driver %s: driver ID is 0");
321 return;
323 registered_drivers = g_slist_prepend(registered_drivers, (gpointer) drv);
324 fp_dbg("registered driver %s", drv->name);
327 static struct fp_driver * const primitive_drivers[] = {
328 &upekts_driver,
331 static struct fp_img_driver * const img_drivers[] = {
332 &aes4000_driver,
333 &aes2501_driver,
334 /* &uru4000_driver,
335 &aes1610_driver,
336 &upektc_driver,
337 &fdu2000_driver, */
340 static void register_drivers(void)
342 unsigned int i;
344 for (i = 0; i < G_N_ELEMENTS(primitive_drivers); i++)
345 register_driver(primitive_drivers[i]);
347 for (i = 0; i < G_N_ELEMENTS(img_drivers); i++) {
348 struct fp_img_driver *imgdriver = img_drivers[i];
349 fpi_img_driver_setup(imgdriver);
350 register_driver(&imgdriver->driver);
354 static struct fp_driver *find_supporting_driver(libusb_dev *udev,
355 const struct usb_id **usb_id)
357 GSList *elem = registered_drivers;
358 struct libusb_dev_descriptor *dsc = libusb_dev_get_descriptor(udev);
360 do {
361 struct fp_driver *drv = elem->data;
362 const struct usb_id *id;
364 for (id = drv->id_table; id->vendor; id++)
365 if (dsc->idVendor == id->vendor && dsc->idProduct == id->product) {
366 fp_dbg("driver %s supports USB device %04x:%04x",
367 drv->name, id->vendor, id->product);
368 *usb_id = id;
369 return drv;
371 } while ((elem = g_slist_next(elem)));
372 return NULL;
375 static struct fp_dscv_dev *discover_dev(libusb_dev *udev)
377 const struct usb_id *usb_id;
378 struct fp_driver *drv = find_supporting_driver(udev, &usb_id);
379 struct fp_dscv_dev *ddev;
380 uint32_t devtype = 0;
382 if (!drv)
383 return NULL;
385 if (drv->discover) {
386 int r = drv->discover(usb_id, &devtype);
387 if (r < 0)
388 fp_err("%s discover failed, code %d", drv->name, r);
389 if (r <= 0)
390 return NULL;
393 ddev = g_malloc0(sizeof(*ddev));
394 ddev->drv = drv;
395 ddev->udev = udev;
396 ddev->driver_data = usb_id->driver_data;
397 ddev->devtype = devtype;
398 return ddev;
401 /** \ingroup dscv_dev
402 * Scans the system and returns a list of discovered devices. This is your
403 * entry point into finding a fingerprint reader to operate.
404 * \returns a NULL-terminated list of discovered devices. Must be freed with
405 * fp_dscv_devs_free() after use.
407 API_EXPORTED struct fp_dscv_dev **fp_discover_devs(void)
409 GSList *tmplist = NULL;
410 struct fp_dscv_dev **list;
411 struct libusb_dev *udev;
412 int dscv_count = 0;
414 if (registered_drivers == NULL)
415 return NULL;
417 libusb_find_devices();
419 /* Check each device against each driver, temporarily storing successfully
420 * discovered devices in a GSList.
422 * Quite inefficient but excusable as we'll only be dealing with small
423 * sets of drivers against small sets of USB devices */
424 for (udev = libusb_get_devices(); udev; udev = libusb_dev_next(udev)) {
425 struct fp_dscv_dev *ddev = discover_dev(udev);
426 if (!ddev)
427 continue;
428 tmplist = g_slist_prepend(tmplist, (gpointer) ddev);
429 dscv_count++;
432 /* Convert our temporary GSList into a standard NULL-terminated pointer
433 * array. */
434 list = g_malloc(sizeof(*list) * (dscv_count + 1));
435 if (dscv_count > 0) {
436 GSList *elem = tmplist;
437 int i = 0;
438 do {
439 list[i++] = elem->data;
440 } while ((elem = g_slist_next(elem)));
442 list[dscv_count] = NULL; /* NULL-terminate */
444 g_slist_free(tmplist);
445 return list;
448 /** \ingroup dscv_dev
449 * Free a list of discovered devices. This function destroys the list and all
450 * discovered devices that it included, so make sure you have opened your
451 * discovered device <b>before</b> freeing the list.
452 * \param devs the list of discovered devices. If NULL, function simply
453 * returns.
455 API_EXPORTED void fp_dscv_devs_free(struct fp_dscv_dev **devs)
457 int i;
458 if (!devs)
459 return;
461 for (i = 0; devs[i]; i++)
462 g_free(devs[i]);
463 g_free(devs);
466 /** \ingroup dscv_dev
467 * Gets the \ref drv "driver" for a discovered device.
468 * \param dev the discovered device
469 * \returns the driver backing the device
471 API_EXPORTED struct fp_driver *fp_dscv_dev_get_driver(struct fp_dscv_dev *dev)
473 return dev->drv;
476 /** \ingroup dscv_dev
477 * Gets the \ref devtype "devtype" for a discovered device.
478 * \param dev the discovered device
479 * \returns the devtype of the device
481 API_EXPORTED uint32_t fp_dscv_dev_get_devtype(struct fp_dscv_dev *dev)
483 return dev->devtype;
486 enum fp_print_data_type fpi_driver_get_data_type(struct fp_driver *drv)
488 switch (drv->type) {
489 case DRIVER_PRIMITIVE:
490 return PRINT_DATA_RAW;
491 case DRIVER_IMAGING:
492 return PRINT_DATA_NBIS_MINUTIAE;
493 default:
494 fp_err("unrecognised drv type %d", drv->type);
495 return PRINT_DATA_RAW;
499 /** \ingroup dscv_dev
500 * Determines if a specific \ref print_data "stored print" appears to be
501 * compatible with a discovered device.
502 * \param dev the discovered device
503 * \param data the print for compatibility checking
504 * \returns 1 if the print is compatible with the device, 0 otherwise
506 API_EXPORTED int fp_dscv_dev_supports_print_data(struct fp_dscv_dev *dev,
507 struct fp_print_data *data)
509 return fpi_print_data_compatible(dev->drv->id, dev->devtype,
510 fpi_driver_get_data_type(dev->drv), data->driver_id, data->devtype,
511 data->type);
514 /** \ingroup dscv_dev
515 * Determines if a specific \ref dscv_print "discovered print" appears to be
516 * compatible with a discovered device.
517 * \param dev the discovered device
518 * \param data the discovered print for compatibility checking
519 * \returns 1 if the print is compatible with the device, 0 otherwise
521 API_EXPORTED int fp_dscv_dev_supports_dscv_print(struct fp_dscv_dev *dev,
522 struct fp_dscv_print *data)
524 return fpi_print_data_compatible(dev->drv->id, dev->devtype, 0,
525 data->driver_id, data->devtype, 0);
528 /** \ingroup dscv_dev
529 * Searches a list of discovered devices for a device that appears to be
530 * compatible with a \ref print_data "stored print".
531 * \param devs a list of discovered devices
532 * \param data the print under inspection
533 * \returns the first discovered device that appears to support the print, or
534 * NULL if no apparently compatible devices could be found
536 API_EXPORTED struct fp_dscv_dev *fp_dscv_dev_for_print_data(struct fp_dscv_dev **devs,
537 struct fp_print_data *data)
539 struct fp_dscv_dev *ddev;
540 int i;
542 for (i = 0; (ddev = devs[i]); i++)
543 if (fp_dscv_dev_supports_print_data(ddev, data))
544 return ddev;
545 return NULL;
548 /** \ingroup dscv_dev
549 * Searches a list of discovered devices for a device that appears to be
550 * compatible with a \ref dscv_print "discovered print".
551 * \param devs a list of discovered devices
552 * \param print the print under inspection
553 * \returns the first discovered device that appears to support the print, or
554 * NULL if no apparently compatible devices could be found
556 API_EXPORTED struct fp_dscv_dev *fp_dscv_dev_for_dscv_print(struct fp_dscv_dev **devs,
557 struct fp_dscv_print *print)
559 struct fp_dscv_dev *ddev;
560 int i;
562 for (i = 0; (ddev = devs[i]); i++)
563 if (fp_dscv_dev_supports_dscv_print(ddev, print))
564 return ddev;
565 return NULL;
568 /** \ingroup dev
569 * Opens and initialises a device. This is the function you call in order
570 * to convert a \ref dscv_dev "discovered device" into an actual device handle
571 * that you can perform operations with.
572 * \param ddev the discovered device to open
573 * \returns the opened device handle, or NULL on error
575 API_EXPORTED struct fp_dev *fp_dev_open(struct fp_dscv_dev *ddev)
577 struct fp_dev *dev;
578 struct fp_driver *drv = ddev->drv;
579 int r;
581 fp_dbg("");
582 libusb_dev_handle *udevh = libusb_open(ddev->udev);
583 if (!udevh) {
584 fp_err("usb_open failed");
585 return NULL;
588 dev = g_malloc0(sizeof(*dev));
589 dev->drv = drv;
590 dev->udev = udevh;
591 dev->__enroll_stage = -1;
592 dev->state = DEV_STATE_INITIALIZING;
594 r = fpi_drv_init(dev, ddev->driver_data);
595 if (r) {
596 fp_err("device initialisation failed, driver=%s", drv->name);
597 goto err;
600 while (dev->state == DEV_STATE_INITIALIZING)
601 if (fp_handle_events() < 0)
602 goto err_deinit;
603 if (dev->state != DEV_STATE_INITIALIZED)
604 goto err_deinit;
606 opened_devices = g_slist_prepend(opened_devices, (gpointer) dev);
607 return dev;
609 err_deinit:
610 fpi_drv_deinit(dev);
611 while (dev->state == DEV_STATE_DEINITIALIZING) {
612 if (fp_handle_events() < 0)
613 break;
615 err:
616 libusb_close(udevh);
617 g_free(dev);
618 return NULL;
621 /* performs close operation without modifying opened_devices list */
622 static void do_close(struct fp_dev *dev)
624 fpi_drv_deinit(dev);
625 while (dev->state == DEV_STATE_DEINITIALIZING)
626 if (fp_handle_events() < 0)
627 break;
629 libusb_close(dev->udev);
630 g_free(dev);
633 /** \ingroup dev
634 * Close a device. You must call this function when you are finished using
635 * a fingerprint device.
636 * \param dev the device to close. If NULL, function simply returns.
638 API_EXPORTED void fp_dev_close(struct fp_dev *dev)
640 if (!dev)
641 return;
643 fp_dbg("");
645 if (g_slist_index(opened_devices, (gconstpointer) dev) == -1)
646 fp_err("device %p not in opened list!", dev);
647 opened_devices = g_slist_remove(opened_devices, (gconstpointer) dev);
648 do_close(dev);
651 /** \ingroup dev
652 * Get the \ref drv "driver" for a fingerprint device.
653 * \param dev the device
654 * \returns the driver controlling the device
656 API_EXPORTED struct fp_driver *fp_dev_get_driver(struct fp_dev *dev)
658 return dev->drv;
661 /** \ingroup dev
662 * Gets the number of \ref enrolling "enroll stages" required to enroll a
663 * fingerprint with the device.
664 * \param dev the device
665 * \returns the number of enroll stages
667 API_EXPORTED int fp_dev_get_nr_enroll_stages(struct fp_dev *dev)
669 return dev->nr_enroll_stages;
672 /** \ingroup dev
673 * Gets the \ref devtype "devtype" for a device.
674 * \param dev the device
675 * \returns the devtype
677 API_EXPORTED uint32_t fp_dev_get_devtype(struct fp_dev *dev)
679 return dev->devtype;
682 /** \ingroup dev
683 * Determines if a stored print is compatible with a certain device.
684 * \param dev the device
685 * \param data the stored print
686 * \returns 1 if the print is compatible with the device, 0 if not
688 API_EXPORTED int fp_dev_supports_print_data(struct fp_dev *dev,
689 struct fp_print_data *data)
691 return fpi_print_data_compatible(dev->drv->id, dev->devtype,
692 fpi_driver_get_data_type(dev->drv), data->driver_id, data->devtype,
693 data->type);
696 /** \ingroup dev
697 * Determines if a \ref dscv_print "discovered print" appears to be compatible
698 * with a certain device.
699 * \param dev the device
700 * \param data the discovered print
701 * \returns 1 if the print is compatible with the device, 0 if not
703 API_EXPORTED int fp_dev_supports_dscv_print(struct fp_dev *dev,
704 struct fp_dscv_print *data)
706 return fpi_print_data_compatible(dev->drv->id, dev->devtype,
707 0, data->driver_id, data->devtype, 0);
710 /** \ingroup drv
711 * Retrieves the name of the driver. For example: "upekts"
712 * \param drv the driver
713 * \returns the driver name. Must not be modified or freed.
715 API_EXPORTED const char *fp_driver_get_name(struct fp_driver *drv)
717 return drv->name;
720 /** \ingroup drv
721 * Retrieves a descriptive name of the driver. For example: "UPEK TouchStrip"
722 * \param drv the driver
723 * \returns the descriptive name. Must not be modified or freed.
725 API_EXPORTED const char *fp_driver_get_full_name(struct fp_driver *drv)
727 return drv->full_name;
730 /** \ingroup drv
731 * Retrieves the driver ID code for a driver.
732 * \param drv the driver
733 * \returns the driver ID
735 API_EXPORTED uint16_t fp_driver_get_driver_id(struct fp_driver *drv)
737 return drv->id;
740 static struct fp_img_dev *dev_to_img_dev(struct fp_dev *dev)
742 if (dev->drv->type != DRIVER_IMAGING)
743 return NULL;
744 return dev->priv;
747 /** \ingroup dev
748 * Determines if a device has imaging capabilities. If a device has imaging
749 * capabilities you are able to perform imaging operations such as retrieving
750 * scan images using fp_dev_img_capture(). However, not all devices are
751 * imaging devices - some do all processing in hardware. This function will
752 * indicate which class a device in question falls into.
753 * \param dev the fingerprint device
754 * \returns 1 if the device is an imaging device, 0 if the device does not
755 * provide images to the host computer
757 API_EXPORTED int fp_dev_supports_imaging(struct fp_dev *dev)
759 return dev->drv->type == DRIVER_IMAGING;
762 /** \ingroup dev
763 * Determines if a device is capable of \ref identification "identification"
764 * through fp_identify_finger() and similar. Not all devices support this
765 * functionality.
766 * \param dev the fingerprint device
767 * \returns 1 if the device is capable of identification, 0 otherwise.
769 API_EXPORTED int fp_dev_supports_identification(struct fp_dev *dev)
771 return dev->drv->identify_start != NULL;
774 /** \ingroup dev
775 * Captures an \ref img "image" from a device. The returned image is the raw
776 * image provided by the device, you may wish to \ref img_std "standardize" it.
778 * If set, the <tt>unconditional</tt> flag indicates that the device should
779 * capture an image unconditionally, regardless of whether a finger is there
780 * or not. If unset, this function will block until a finger is detected on
781 * the sensor.
783 * \param dev the device
784 * \param unconditional whether to unconditionally capture an image, or to only capture when a finger is detected
785 * \param image a location to return the captured image. Must be freed with
786 * fp_img_free() after use.
787 * \return 0 on success, non-zero on error. -ENOTSUP indicates that either the
788 * unconditional flag was set but the device does not support this, or that the
789 * device does not support imaging.
790 * \sa fp_dev_supports_imaging()
792 API_EXPORTED int fp_dev_img_capture(struct fp_dev *dev, int unconditional,
793 struct fp_img **image)
795 struct fp_img_dev *imgdev = dev_to_img_dev(dev);
796 if (!imgdev) {
797 fp_dbg("image capture on non-imaging device");
798 return -ENOTSUP;
801 //return fpi_imgdev_capture(imgdev, unconditional, image);
802 /* FIXME reimplement async */
803 return -ENOTSUP;
806 /** \ingroup dev
807 * Gets the expected width of images that will be captured from the device.
808 * This function will return -1 for devices that are not
809 * \ref imaging "imaging devices". If the width of images from this device
810 * can vary, 0 will be returned.
811 * \param dev the device
812 * \returns the expected image width, or 0 for variable, or -1 for non-imaging
813 * devices.
815 API_EXPORTED int fp_dev_get_img_width(struct fp_dev *dev)
817 struct fp_img_dev *imgdev = dev_to_img_dev(dev);
818 if (!imgdev) {
819 fp_dbg("get image width for non-imaging device");
820 return -1;
823 return fpi_imgdev_get_img_width(imgdev);
826 /** \ingroup dev
827 * Gets the expected height of images that will be captured from the device.
828 * This function will return -1 for devices that are not
829 * \ref imaging "imaging devices". If the height of images from this device
830 * can vary, 0 will be returned.
831 * \param dev the device
832 * \returns the expected image height, or 0 for variable, or -1 for non-imaging
833 * devices.
835 API_EXPORTED int fp_dev_get_img_height(struct fp_dev *dev)
837 struct fp_img_dev *imgdev = dev_to_img_dev(dev);
838 if (!imgdev) {
839 fp_dbg("get image height for non-imaging device");
840 return -1;
843 return fpi_imgdev_get_img_height(imgdev);
846 struct sync_enroll_data {
847 gboolean populated;
848 int result;
849 struct fp_print_data *data;
850 struct fp_img *img;
853 static void sync_enroll_cb(struct fp_dev *dev, int result,
854 struct fp_print_data *data, struct fp_img *img)
856 struct sync_enroll_data *edata = dev->enroll_data;
857 edata->result = result;
858 edata->data = data;
859 edata->img = img;
860 edata->populated = TRUE;
863 /** \ingroup dev
864 * Performs an enroll stage. See \ref enrolling for an explanation of enroll
865 * stages.
867 * If no enrollment is in process, this kicks of the process and runs the
868 * first stage. If an enrollment is already in progress, calling this
869 * function runs the next stage, which may well be the last.
871 * A negative error code may be returned from any stage. When this occurs,
872 * further calls to the enroll function will start a new enrollment process,
873 * i.e. a negative error code indicates that the enrollment process has been
874 * aborted. These error codes only ever indicate unexpected internal errors
875 * or I/O problems.
877 * The RETRY codes from #fp_enroll_result may be returned from any enroll
878 * stage. These codes indicate that the scan was not succesful in that the
879 * user did not position their finger correctly or similar. When a RETRY code
880 * is returned, the enrollment stage is <b>not</b> advanced, so the next call
881 * into this function will retry the current stage again. The current stage may
882 * need to be retried several times.
884 * The fp_enroll_result#FP_ENROLL_FAIL code may be returned from any enroll
885 * stage. This code indicates that even though the scans themselves have been
886 * acceptable, data processing applied to these scans produces incomprehensible
887 * results. In other words, the user may have been scanning a different finger
888 * for each stage or something like that. Like negative error codes, this
889 * return code indicates that the enrollment process has been aborted.
891 * The fp_enroll_result#FP_ENROLL_PASS code will only ever be returned for
892 * non-final stages. This return code indicates that the scan was acceptable
893 * and the next call into this function will advance onto the next enroll
894 * stage.
896 * The fp_enroll_result#FP_ENROLL_COMPLETE code will only ever be returned
897 * from the final enroll stage. It indicates that enrollment completed
898 * successfully, and that print_data has been assigned to point to the
899 * resultant enrollment data. The print_data parameter will not be modified
900 * during any other enrollment stages, hence it is actually legal to pass NULL
901 * as this argument for all but the final stage.
903 * If the device is an imaging device, it can also return the image from
904 * the scan, even when the enroll fails with a RETRY or FAIL code. It is legal
905 * to call this function even on non-imaging devices, just don't expect them to
906 * provide images.
908 * \param dev the device
909 * \param print_data a location to return the resultant enrollment data from
910 * the final stage. Must be freed with fp_print_data_free() after use.
911 * \param img location to store the scan image. accepts NULL for no image
912 * storage. If an image is returned, it must be freed with fp_img_free() after
913 * use.
914 * \return negative code on error, otherwise a code from #fp_enroll_result
916 API_EXPORTED int fp_enroll_finger_img(struct fp_dev *dev,
917 struct fp_print_data **print_data, struct fp_img **img)
919 struct fp_driver *drv = dev->drv;
920 int stage = dev->__enroll_stage;
921 gboolean final = FALSE;
922 struct sync_enroll_data *edata;
923 int r;
925 if (!dev->nr_enroll_stages || !drv->enroll_start) {
926 fp_err("driver %s has 0 enroll stages or no enroll func",
927 drv->name);
928 return -ENOTSUP;
931 if (stage == -1) {
932 fp_dbg("starting enrollment");
933 r = fpi_drv_enroll_start(dev, sync_enroll_cb);
934 if (r < 0) {
935 fp_err("failed to start enrollment");
936 return r;
938 while (dev->state == DEV_STATE_ENROLL_STARTING) {
939 r = fp_handle_events();
940 if (r < 0)
941 goto err;
944 if (dev->state != DEV_STATE_ENROLLING) {
945 r = -EIO;
946 goto err;
949 dev->__enroll_stage = ++stage;
950 dev->enroll_data = g_malloc0(sizeof(struct sync_enroll_data));
951 } else if (stage >= dev->nr_enroll_stages) {
952 fp_err("exceeding number of enroll stages for device claimed by "
953 "driver %s (%d stages)", drv->name, dev->nr_enroll_stages);
954 dev->__enroll_stage = -1;
955 r = -EINVAL;
956 final = TRUE;
957 goto out;
959 fp_dbg("%s will handle enroll stage %d/%d", drv->name, stage,
960 dev->nr_enroll_stages - 1);
962 edata = dev->enroll_data;
963 while (!edata->populated) {
964 r = fp_handle_events();
965 if (r < 0) {
966 g_free(edata);
967 goto err;
971 edata->populated = FALSE;
973 if (img)
974 *img = edata->img;
975 else
976 fp_img_free(edata->img);
978 r = edata->result;
979 switch (r) {
980 case FP_ENROLL_PASS:
981 fp_dbg("enroll stage passed");
982 dev->__enroll_stage = stage + 1;
983 break;
984 case FP_ENROLL_COMPLETE:
985 fp_dbg("enroll complete");
986 dev->__enroll_stage = -1;
987 *print_data = edata->data;
988 final = TRUE;
989 break;
990 case FP_ENROLL_RETRY:
991 fp_dbg("enroll should retry");
992 break;
993 case FP_ENROLL_RETRY_TOO_SHORT:
994 fp_dbg("swipe was too short, enroll should retry");
995 break;
996 case FP_ENROLL_RETRY_CENTER_FINGER:
997 fp_dbg("finger was not centered, enroll should retry");
998 break;
999 case FP_ENROLL_RETRY_REMOVE_FINGER:
1000 fp_dbg("scan failed, remove finger and retry");
1001 break;
1002 case FP_ENROLL_FAIL:
1003 fp_err("enroll failed");
1004 dev->__enroll_stage = -1;
1005 final = TRUE;
1006 break;
1007 default:
1008 fp_err("unrecognised return code %d", r);
1009 dev->__enroll_stage = -1;
1010 r = -EINVAL;
1011 final = TRUE;
1012 break;
1015 out:
1016 if (final) {
1017 fp_dbg("ending enrollment");
1018 if (fpi_drv_enroll_stop(dev) == 0)
1019 while (dev->state == DEV_STATE_ENROLL_STOPPING) {
1020 if (fp_handle_events() < 0)
1021 break;
1023 g_free(dev->enroll_data);
1026 return r;
1028 err:
1029 if (fpi_drv_enroll_stop(dev) == 0)
1030 while (dev->state == DEV_STATE_ENROLL_STOPPING)
1031 if (fp_handle_events() < 0)
1032 break;
1033 return r;
1036 struct sync_verify_data {
1037 gboolean populated;
1038 int result;
1039 struct fp_img *img;
1042 static void sync_verify_cb(struct fp_dev *dev, int result, struct fp_img *img)
1044 struct sync_verify_data *vdata = dev->sync_verify_data;
1045 vdata->result = result;
1046 vdata->img = img;
1047 vdata->populated = TRUE;
1050 /** \ingroup dev
1051 * Performs a new scan and verify it against a previously enrolled print.
1052 * If the device is an imaging device, it can also return the image from
1053 * the scan, even when the verify fails with a RETRY code. It is legal to
1054 * call this function even on non-imaging devices, just don't expect them to
1055 * provide images.
1057 * \param dev the device to perform the scan.
1058 * \param enrolled_print the print to verify against. Must have been previously
1059 * enrolled with a device compatible to the device selected to perform the scan.
1060 * \param img location to store the scan image. accepts NULL for no image
1061 * storage. If an image is returned, it must be freed with fp_img_free() after
1062 * use.
1063 * \return negative code on error, otherwise a code from #fp_verify_result
1065 API_EXPORTED int fp_verify_finger_img(struct fp_dev *dev,
1066 struct fp_print_data *enrolled_print, struct fp_img **img)
1068 struct fp_driver *drv = dev->drv;
1069 struct sync_verify_data *vdata;
1070 int r;
1072 if (!enrolled_print) {
1073 fp_err("no print given");
1074 return -EINVAL;
1077 if (!drv->verify_start) {
1078 fp_err("driver %s has no verify func", drv->name);
1079 return -ENOTSUP;
1082 if (!fp_dev_supports_print_data(dev, enrolled_print)) {
1083 fp_err("print is not compatible with device");
1084 return -EINVAL;
1087 fp_dbg("to be handled by %s", drv->name);
1088 r = fpi_drv_verify_start(dev, sync_verify_cb, enrolled_print);
1089 if (r < 0) {
1090 fp_dbg("verify_start error %d", r);
1091 return r;
1093 while (dev->state == DEV_STATE_VERIFY_STARTING) {
1094 r = fp_handle_events();
1095 if (r < 0)
1096 goto err;
1098 if (dev->state != DEV_STATE_VERIFYING) {
1099 r = -EIO;
1100 goto err;
1103 dev->sync_verify_data = g_malloc0(sizeof(struct sync_verify_data));
1104 vdata = dev->sync_verify_data;
1106 while (!vdata->populated) {
1107 r = fp_handle_events();
1108 if (r < 0) {
1109 g_free(vdata);
1110 goto err;
1114 if (img)
1115 *img = vdata->img;
1116 else
1117 fp_img_free(vdata->img);
1119 r = vdata->result;
1120 g_free(dev->sync_verify_data);
1121 switch (r) {
1122 case FP_VERIFY_NO_MATCH:
1123 fp_dbg("result: no match");
1124 break;
1125 case FP_VERIFY_MATCH:
1126 fp_dbg("result: match");
1127 break;
1128 case FP_VERIFY_RETRY:
1129 fp_dbg("verify should retry");
1130 break;
1131 case FP_VERIFY_RETRY_TOO_SHORT:
1132 fp_dbg("swipe was too short, verify should retry");
1133 break;
1134 case FP_VERIFY_RETRY_CENTER_FINGER:
1135 fp_dbg("finger was not centered, verify should retry");
1136 break;
1137 case FP_VERIFY_RETRY_REMOVE_FINGER:
1138 fp_dbg("scan failed, remove finger and retry");
1139 break;
1140 default:
1141 fp_err("unrecognised return code %d", r);
1142 r = -EINVAL;
1145 err:
1146 fp_dbg("ending verification");
1147 if (fpi_drv_verify_stop(dev) == 0) {
1148 while (dev->state == DEV_STATE_VERIFY_STOPPING) {
1149 if (fp_handle_events() < 0)
1150 break;
1154 return r;
1157 struct sync_identify_data {
1158 gboolean populated;
1159 int result;
1160 size_t match_offset;
1161 struct fp_img *img;
1164 static void sync_identify_cb(struct fp_dev *dev, int result,
1165 size_t match_offset, struct fp_img *img)
1167 struct sync_identify_data *idata = dev->sync_identify_data;
1168 idata->result = result;
1169 idata->match_offset = match_offset;
1170 idata->img = img;
1171 idata->populated = TRUE;
1174 /** \ingroup dev
1175 * Performs a new scan and attempts to identify the scanned finger against
1176 * a collection of previously enrolled fingerprints.
1177 * If the device is an imaging device, it can also return the image from
1178 * the scan, even when identification fails with a RETRY code. It is legal to
1179 * call this function even on non-imaging devices, just don't expect them to
1180 * provide images.
1182 * This function returns codes from #fp_verify_result. The return code
1183 * fp_verify_result#FP_VERIFY_MATCH indicates that the scanned fingerprint
1184 * does appear in the print gallery, and the match_offset output parameter
1185 * will indicate the index into the print gallery array of the matched print.
1187 * This function will not necessarily examine the whole print gallery, it
1188 * will return as soon as it finds a matching print.
1190 * Not all devices support identification. -ENOTSUP will be returned when
1191 * this is the case.
1193 * \param dev the device to perform the scan.
1194 * \param print_gallery NULL-terminated array of pointers to the prints to
1195 * identify against. Each one must have been previously enrolled with a device
1196 * compatible to the device selected to perform the scan.
1197 * \param match_offset output location to store the array index of the matched
1198 * gallery print (if any was found). Only valid if FP_VERIFY_MATCH was
1199 * returned.
1200 * \param img location to store the scan image. accepts NULL for no image
1201 * storage. If an image is returned, it must be freed with fp_img_free() after
1202 * use.
1203 * \return negative code on error, otherwise a code from #fp_verify_result
1205 API_EXPORTED int fp_identify_finger_img(struct fp_dev *dev,
1206 struct fp_print_data **print_gallery, size_t *match_offset,
1207 struct fp_img **img)
1209 struct fp_driver *drv = dev->drv;
1210 struct sync_identify_data *idata;
1211 int r;
1213 if (!drv->identify_start) {
1214 fp_dbg("driver %s has no identify func", drv->name);
1215 return -ENOTSUP;
1217 fp_dbg("to be handled by %s", drv->name);
1219 r = fpi_drv_identify_start(dev, sync_identify_cb, print_gallery);
1220 if (r < 0) {
1221 fp_err("identify_start error %d", r);
1222 return r;
1224 while (dev->state == DEV_STATE_IDENTIFY_STARTING) {
1225 r = fp_handle_events();
1226 if (r < 0)
1227 goto err;
1229 if (dev->state != DEV_STATE_IDENTIFYING) {
1230 r = -EIO;
1231 goto err;
1234 dev->sync_identify_data = g_malloc0(sizeof(struct sync_identify_data));
1235 idata = dev->sync_identify_data;
1237 while (!idata->populated) {
1238 r = fp_handle_events();
1239 if (r < 0) {
1240 g_free(idata);
1241 goto err;
1245 if (img)
1246 *img = idata->img;
1247 else
1248 fp_img_free(idata->img);
1250 r = idata->result;
1251 switch (r) {
1252 case FP_VERIFY_NO_MATCH:
1253 fp_dbg("result: no match");
1254 break;
1255 case FP_VERIFY_MATCH:
1256 fp_dbg("result: match at offset %zd", match_offset);
1257 *match_offset = idata->match_offset;
1258 break;
1259 case FP_VERIFY_RETRY:
1260 fp_dbg("verify should retry");
1261 break;
1262 case FP_VERIFY_RETRY_TOO_SHORT:
1263 fp_dbg("swipe was too short, verify should retry");
1264 break;
1265 case FP_VERIFY_RETRY_CENTER_FINGER:
1266 fp_dbg("finger was not centered, verify should retry");
1267 break;
1268 case FP_VERIFY_RETRY_REMOVE_FINGER:
1269 fp_dbg("scan failed, remove finger and retry");
1270 break;
1271 default:
1272 fp_err("unrecognised return code %d", r);
1273 r = -EINVAL;
1275 g_free(dev->sync_identify_data);
1277 err:
1278 if (fpi_drv_identify_stop(dev) == 0) {
1279 while (dev->state == DEV_STATE_IDENTIFY_STOPPING) {
1280 if (fp_handle_events() < 0)
1281 break;
1285 return r;
1288 /** \ingroup core
1289 * Initialise libfprint. This function must be called before you attempt to
1290 * use the library in any way.
1291 * \return 0 on success, non-zero on error.
1293 API_EXPORTED int fp_init(void)
1295 int r;
1296 fp_dbg("");
1298 r = libusb_init();
1299 if (r < 0)
1300 return r;
1302 register_drivers();
1303 return 0;
1306 /** \ingroup core
1307 * Deinitialise libfprint. This function should be called during your program
1308 * exit sequence. You must not use any libfprint functions after calling this
1309 * function, unless you call fp_init() again.
1311 API_EXPORTED void fp_exit(void)
1313 GSList *elem = opened_devices;
1314 fp_dbg("");
1316 if (elem != NULL) {
1317 do {
1318 fp_dbg("naughty app left a device open on exit!");
1319 do_close((struct fp_dev *) elem->data);
1320 } while ((elem = g_slist_next(elem)));
1321 g_slist_free(opened_devices);
1322 opened_devices = NULL;
1325 fpi_data_exit();
1326 fpi_poll_exit();
1327 g_slist_free(registered_drivers);
1328 registered_drivers = NULL;
1329 libusb_exit();