Add AES2501 driver
[libfprint.git] / libfprint / core.c
blob928b2851a99dede1fdf4de7a0e557b63ee686dc0
1 /*
2 * Core functions for libfprint
3 * Copyright (C) 2007 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 <usb.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 * libfprint supports enrollment and verification as described above. Although
85 * libfprint does not yet support identification (it is planned), it is worth
86 * introducing the concept to give you a complete picture.
88 * Identification is the process of comparing a freshly scanned fingerprint
89 * to a <em>collection</em> of previously enrolled fingerprints. For example,
90 * imagine there are 100 people in an organisation, and they all have enrolled
91 * their fingerprints. One user walks up to a fingerprint scanner and scans
92 * their finger. With <em>no other knowledge</em> of who that user might be,
93 * the system examines their fingerprint, looks in the database, and determines
94 * that the user is user number #61.
96 * In other words, verification might be seen as a one-to-one fingerprint
97 * comparison where you know the identity of the user that you wish to
98 * authenticate, whereas identification is a one-to-many comparison where you
99 * do not know the identity of the user that you wish to authenticate.
101 * \section compat_general Device and print compatibility
102 * Moving off generic conceptual ideas and onto libfprint-specific
103 * implementation details, here are some introductory notes regarding how
104 * libfprint copes with compatibility of fingerprints.
106 * libfprint deals with a whole variety of different fingerprint readers and
107 * the design includes considerations of compatibility and interoperability
108 * between multiple devices. Your application should also be prepared to
109 * work with more than one type of fingerprint reader and should consider that
110 * enrolled fingerprint X may not be compatible with the device the user has
111 * plugged in today.
113 * libfprint implements the principle that fingerprints from different devices
114 * are not necessarily compatible. For example, different devices may see
115 * significantly different areas of fingerprint surface, and comparing images
116 * between the devices would be unreliable. Also, devices can stretch and
117 * distort images in different ways.
119 * libfprint also implements the principle that in some cases, fingerprints
120 * <em>are</em> compatible between different devices. If you go and buy two
121 * identical fingerprint readers, it seems logical that you should be able
122 * to enroll on one and verify on another without problems.
124 * libfprint takes a fairly simplistic approach to these issues. Internally,
125 * fingerprint hardware is driven by individual drivers. libfprint enforces
126 * that a fingerprint that came from a device backed by driver X is never
127 * compared to a fingerprint that came from a device backed by driver Y.
129 * Additionally, libfprint is designed for the situation where a single driver
130 * may support a range of devices which differ in imaging or scanning
131 * properties. For example, a driver may support two ranges of devices which
132 * even though are programmed over the same interface, one device sees
133 * substantially less of the finger flesh, therefore images from the two
134 * device types should be incompatible despite being from the same driver. To
135 * implement this, each driver assigns a <em>device type</em> to each device
136 * that it detects based on its imaging characteristics. libfprint ensures that
137 * two prints being compared have the same device type.
139 * In summary, libfprint represents fingerprints in several internal structures
140 * and each representation will offer you a way of determining the
141 * \ref driver_id "driver ID" and \ref devtype "devtype" of the print in
142 * question. Prints are only compatible if the driver ID <b>and</b> devtypes
143 * match. libfprint does offer you some "is this print compatible?" helper
144 * functions, so you don't have to worry about these details too much.
146 * \section sync Synchronity/asynchronity
148 * Currently, all data acquisition operations are synchronous and can
149 * potentially block for extended periods of time. For example, the enroll
150 * function will block for an unpredictable amount of time until the user
151 * scans their finger.
153 * Alternative asynchronous/non-blocking functionality will be offered in
154 * future but has not been implemented yet.
156 * \section getting_started Getting started
158 * libfprint includes several simple functional examples under the examples/
159 * directory in the libfprint source distribution. Those are good starting
160 * points.
162 * Usually the first thing you want to do is determine which fingerprint
163 * devices are present. This is done through \ref dscv_dev "device discovery".
165 * Once you have found a device you would like to operate, you should open it.
166 * Refer to \ref dev "device operations". This section also details enrollment,
167 * image capture, and verification.
170 * That should be enough to get you started, but do remember there are
171 * documentation pages on other aspects of libfprint's API (see the modules
172 * page).
175 /** @defgroup core Core library operations */
178 * @defgroup dev Device operations
179 * In order to interact with fingerprint scanners, your software will
180 * interface primarily with libfprint's representation of devices, detailed
181 * on this page.
183 * \section enrolling Enrolling
184 * Enrolling is represented within libfprint as a multi-stage process. This
185 * slightly complicates things for application developers, but is required
186 * for a smooth process.
188 * Some devices require the user to scan their finger multiple times in
189 * order to complete the enrollment process. libfprint must return control
190 * to your application inbetween each scan in order for your application to
191 * instruct the user to swipe their finger again. Each scan is referred to
192 * as a stage, so a device that requires 3 scans for enrollment corresponds
193 * to you running 3 enrollment stages using libfprint.
195 * The fp_dev_get_nr_enroll_stages() function can be used to find out how
196 * many enroll stages are needed.
198 * In order to complete an enroll stage, you call an enroll function such
199 * as fp_enroll_finger(). The return of this function does not necessarily
200 * indicate that a stage has completed though, as the user may not have
201 * produced a good enough scan. Each stage may have to be retried several
202 * times.
204 * The exact semantics of the enroll functions are described in the
205 * fp_enroll_finger() documentation. You should pay careful attention to the
206 * details.
208 * \section imaging Imaging
209 * libfprint provides you with some ways to retrieve images of scanned
210 * fingers, such as the fp_dev_img_capture() function, or some enroll/verify
211 * function variants which provide images. You may wish to do something with
212 * such images in your application.
214 * However, you must be aware that not all hardware supported by libfprint
215 * operates like this. Most hardware does operate simply by sending
216 * fingerprint images to the host computer for further processing, but some
217 * devices do all fingerprint processing in hardware and do not present images
218 * to the host computer.
220 * You can use fp_dev_supports_imaging() to see if image capture is possible
221 * on a particular device. Your application must be able to cope with the
222 * fact that libfprint does support regular operations (e.g. enrolling and
223 * verification) on some devices which do not provide images.
225 * \section devtype Devtypes
226 * Internally, the \ref drv "driver" behind a device assigns a 32-bit
227 * <em>devtype</em> identifier to the device. This cannot be used as a unique
228 * ID for a specific device as many devices under the same range may share
229 * the same devtype. The devtype may even be 0 in all cases.
231 * The only reason you may be interested in retrieving the devtype for a
232 * device is for the purpose of checking if some print data is compatible
233 * with a device. libfprint uses the devtype as one way of checking that the
234 * print you are verifying is compatible with the device in question - the
235 * devtypes must be equal. This effectively allows drivers to support more
236 * than one type of device where the data from each one is not compatible with
237 * the other. Note that libfprint does provide you with helper functions to
238 * determine whether a print is compatible with a device, so under most
239 * circumstances, you don't have to worry about devtypes at all.
242 /** @defgroup dscv_dev Device discovery
243 * These functions allow you to scan the system for supported fingerprint
244 * scanning hardware. This is your starting point when integrating libfprint
245 * into your software.
247 * When you've identified a discovered device that you would like to control,
248 * you can open it with fp_dev_open(). Note that discovered devices may no
249 * longer be available at the time when you want to open them, for example
250 * the user may have unplugged the device.
253 /** @defgroup drv Driver operations
254 * Internally, libfprint is abstracted into various drivers to communicate
255 * with the different types of supported fingerprint readers. libfprint works
256 * hard so that you don't have to care about these internal abstractions,
257 * however there are some situations where you may be interested in a little
258 * behind-the-scenes driver info.
260 * You can obtain the driver for a device using fp_dev_get_driver(), which
261 * you can pass to the functions documented on this page.
263 * \section driver_id Driver IDs
264 * Each driver is assigned a unique ID by the project maintainer. These
265 * assignments are
266 * <a href="http://www.reactivated.net/fprint/Driver_ID_assignments">
267 * documented on the wiki</a> and will never change.
269 * The only reason you may be interested in retrieving the driver ID for a
270 * driver is for the purpose of checking if some print data is compatible
271 * with a device. libfprint uses the driver ID as one way of checking that
272 * the print you are trying to verify is compatible with the device in
273 * question - it ensures that enrollment data from one driver is never fed to
274 * another. Note that libfprint does provide you with helper functions to
275 * determine whether a print is compatible with a device, so under most
276 * circumstances, you don't have to worry about driver IDs at all.
279 static GSList *registered_drivers = NULL;
280 static GSList *opened_devices = NULL;
282 void fpi_log(enum fpi_log_level level, const char *component,
283 const char *function, const char *format, ...)
285 va_list args;
286 FILE *stream = stdout;
287 const char *prefix;
289 switch (level) {
290 case LOG_LEVEL_INFO:
291 prefix = "info";
292 break;
293 case LOG_LEVEL_WARNING:
294 stream = stderr;
295 prefix = "warning";
296 break;
297 case LOG_LEVEL_ERROR:
298 stream = stderr;
299 prefix = "error";
300 break;
301 case LOG_LEVEL_DEBUG:
302 stream = stderr;
303 prefix = "debug";
304 break;
305 default:
306 stream = stderr;
307 prefix = "unknown";
308 break;
311 fprintf(stream, "%s:%s [%s] ", component ? component : "fp", prefix,
312 function);
314 va_start (args, format);
315 vfprintf(stream, format, args);
316 va_end (args);
318 fprintf(stream, "\n");
321 static void register_driver(struct fp_driver *drv)
323 if (drv->id == 0) {
324 fp_err("not registering driver %s: driver ID is 0");
325 return;
327 registered_drivers = g_slist_prepend(registered_drivers, (gpointer) drv);
328 fp_dbg("registered driver %s", drv->name);
331 static struct fp_driver * const primitive_drivers[] = {
332 &upekts_driver,
335 static struct fp_img_driver * const img_drivers[] = {
336 &uru4000_driver,
337 &aes2501_driver,
338 &aes4000_driver,
341 static void register_drivers(void)
343 unsigned int i;
345 for (i = 0; i < ARRAY_SIZE(primitive_drivers); i++)
346 register_driver(primitive_drivers[i]);
348 for (i = 0; i < ARRAY_SIZE(img_drivers); i++) {
349 struct fp_img_driver *imgdriver = img_drivers[i];
350 fpi_img_driver_setup(imgdriver);
351 register_driver(&imgdriver->driver);
355 static struct fp_driver *find_supporting_driver(struct usb_device *udev,
356 const struct usb_id **usb_id)
358 GSList *elem = registered_drivers;
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 (udev->descriptor.idVendor == id->vendor &&
366 udev->descriptor.idProduct == id->product) {
367 fp_dbg("driver %s supports USB device %04x:%04x",
368 drv->name, id->vendor, id->product);
369 *usb_id = id;
370 return drv;
372 } while (elem = g_slist_next(elem));
373 return NULL;
376 static struct fp_dscv_dev *discover_dev(struct usb_device *udev)
378 const struct usb_id *usb_id;
379 struct fp_driver *drv = find_supporting_driver(udev, &usb_id);
380 struct fp_dscv_dev *ddev;
381 uint32_t devtype = 0;
383 if (!drv)
384 return NULL;
386 if (drv->discover) {
387 int r = drv->discover(usb_id, &devtype);
388 if (r < 0)
389 fp_err("%s discover failed, code %d", drv->name, r);
390 if (r <= 0)
391 return NULL;
394 ddev = g_malloc0(sizeof(*ddev));
395 ddev->drv = drv;
396 ddev->udev = udev;
397 ddev->driver_data = usb_id->driver_data;
398 ddev->devtype = devtype;
399 return ddev;
402 /** \ingroup dscv_dev
403 * Scans the system and returns a list of discovered devices. This is your
404 * entry point into finding a fingerprint reader to operate.
405 * \returns a NULL-terminated list of discovered devices. Must be freed with
406 * fp_dscv_devs_free() after use.
408 API_EXPORTED struct fp_dscv_dev **fp_discover_devs(void)
410 GSList *tmplist = NULL;
411 struct fp_dscv_dev **list;
412 struct usb_device *udev;
413 struct usb_bus *bus;
414 int dscv_count = 0;
416 if (registered_drivers == NULL)
417 return NULL;
419 usb_find_busses();
420 usb_find_devices();
422 /* Check each device against each driver, temporarily storing successfully
423 * discovered devices in a GSList.
425 * Quite inefficient but excusable as we'll only be dealing with small
426 * sets of drivers against small sets of USB devices */
427 for (bus = usb_get_busses(); bus; bus = bus->next)
428 for (udev = bus->devices; udev; udev = udev->next) {
429 struct fp_dscv_dev *ddev = discover_dev(udev);
430 if (!ddev)
431 continue;
432 tmplist = g_slist_prepend(tmplist, (gpointer) ddev);
433 dscv_count++;
436 /* Convert our temporary GSList into a standard NULL-terminated pointer
437 * array. */
438 list = g_malloc(sizeof(*list) * (dscv_count + 1));
439 if (dscv_count > 0) {
440 GSList *elem = tmplist;
441 int i = 0;
442 do {
443 list[i++] = elem->data;
444 } while (elem = g_slist_next(elem));
446 list[dscv_count] = NULL; /* NULL-terminate */
448 g_slist_free(tmplist);
449 return list;
452 /** \ingroup dscv_dev
453 * Free a list of discovered devices. This function destroys the list and all
454 * discovered devices that it included, so make sure you have opened your
455 * discovered device <b>before</b> freeing the list.
456 * \param devs the list of discovered devices
458 API_EXPORTED void fp_dscv_devs_free(struct fp_dscv_dev **devs)
460 int i;
461 if (!devs)
462 return;
464 for (i = 0; devs[i]; i++)
465 g_free(devs[i]);
466 g_free(devs);
469 /** \ingroup dscv_dev
470 * Gets the \ref drv "driver" for a discovered device.
471 * \param dev the discovered device
472 * \returns the driver backing the device
474 API_EXPORTED struct fp_driver *fp_dscv_dev_get_driver(struct fp_dscv_dev *dev)
476 return dev->drv;
479 /** \ingroup dscv_dev
480 * Gets the \ref devtype "devtype" for a discovered device.
481 * \param dev the discovered device
482 * \returns the devtype of the device
484 API_EXPORTED uint32_t fp_dscv_dev_get_devtype(struct fp_dscv_dev *dev)
486 return dev->devtype;
489 enum fp_print_data_type fpi_driver_get_data_type(struct fp_driver *drv)
491 switch (drv->type) {
492 case DRIVER_PRIMITIVE:
493 return PRINT_DATA_RAW;
494 case DRIVER_IMAGING:
495 return PRINT_DATA_NBIS_MINUTIAE;
496 default:
497 fp_err("unrecognised drv type %d", drv->type);
498 return PRINT_DATA_RAW;
502 /** \ingroup dscv_dev
503 * Determines if a specific \ref print_data "stored print" appears to be
504 * compatible with a discovered device.
505 * \param dev the discovered device
506 * \param data the print for compatibility checking
507 * \returns 1 if the print is compatible with the device, 0 otherwise
509 API_EXPORTED int fp_dscv_dev_supports_print_data(struct fp_dscv_dev *dev,
510 struct fp_print_data *data)
512 return fpi_print_data_compatible(dev->drv->id, dev->devtype,
513 fpi_driver_get_data_type(dev->drv), data->driver_id, data->devtype,
514 data->type);
517 /** \ingroup dscv_dev
518 * Determines if a specific \ref dscv_print "discovered print" appears to be
519 * compatible with a discovered device.
520 * \param dev the discovered device
521 * \param data the discovered print for compatibility checking
522 * \returns 1 if the print is compatible with the device, 0 otherwise
524 API_EXPORTED int fp_dscv_dev_supports_dscv_print(struct fp_dscv_dev *dev,
525 struct fp_dscv_print *data)
527 return fpi_print_data_compatible(dev->drv->id, dev->devtype, 0,
528 data->driver_id, data->devtype, 0);
531 /** \ingroup dscv_dev
532 * Searches a list of discovered devices for a device that appears to be
533 * compatible with a \ref print_data "stored print".
534 * \param devs a list of discovered devices
535 * \param data the print under inspection
536 * \returns the first discovered device that appears to support the print, or
537 * NULL if no apparently compatible devices could be found
539 API_EXPORTED struct fp_dscv_dev *fp_dscv_dev_for_print_data(struct fp_dscv_dev **devs,
540 struct fp_print_data *data)
542 struct fp_dscv_dev *ddev;
543 int i;
545 for (i = 0; ddev = devs[i]; i++)
546 if (fp_dscv_dev_supports_print_data(ddev, data))
547 return ddev;
548 return NULL;
551 /** \ingroup dscv_dev
552 * Searches a list of discovered devices for a device that appears to be
553 * compatible with a \ref dscv_print "discovered print".
554 * \param devs a list of discovered devices
555 * \param print the print under inspection
556 * \returns the first discovered device that appears to support the print, or
557 * NULL if no apparently compatible devices could be found
559 API_EXPORTED struct fp_dscv_dev *fp_dscv_dev_for_dscv_print(struct fp_dscv_dev **devs,
560 struct fp_dscv_print *print)
562 struct fp_dscv_dev *ddev;
563 int i;
565 for (i = 0; ddev = devs[i]; i++)
566 if (fp_dscv_dev_supports_dscv_print(ddev, print))
567 return ddev;
568 return NULL;
571 /** \ingroup dev
572 * Opens and initialises a device. This is the function you call in order
573 * to convert a \ref dscv_dev "discovered device" into an actual device handle
574 * that you can perform operations with.
575 * \param ddev the discovered device to open
576 * \returns the opened device handle, or NULL on error
578 API_EXPORTED struct fp_dev *fp_dev_open(struct fp_dscv_dev *ddev)
580 struct fp_dev *dev;
581 struct fp_driver *drv = ddev->drv;
582 int r;
584 usb_dev_handle *udevh = usb_open(ddev->udev);
585 if (!udevh) {
586 fp_err("usb_open failed");
587 return NULL;
590 dev = g_malloc0(sizeof(*dev));
591 dev->drv = drv;
592 dev->udev = udevh;
593 dev->__enroll_stage = -1;
595 if (drv->init) {
596 r = drv->init(dev, ddev->driver_data);
597 if (r) {
598 fp_err("device initialisation failed, driver=%s", drv->name);
599 usb_close(udevh);
600 g_free(dev);
601 return NULL;
605 fp_dbg("");
606 opened_devices = g_slist_prepend(opened_devices, (gpointer) dev);
607 return dev;
610 /* performs close operation without modifying opened_devices list */
611 static void do_close(struct fp_dev *dev)
613 if (dev->drv->exit)
614 dev->drv->exit(dev);
615 usb_close(dev->udev);
616 g_free(dev);
619 /** \ingroup dev
620 * Close a device. You must call this function when you are finished using
621 * a fingerprint device.
622 * \param dev the device to close
624 API_EXPORTED void fp_dev_close(struct fp_dev *dev)
626 fp_dbg("");
628 if (g_slist_index(opened_devices, (gconstpointer) dev) == -1)
629 fp_err("device %p not in opened list!", dev);
630 opened_devices = g_slist_remove(opened_devices, (gconstpointer) dev);
631 do_close(dev);
634 /** \ingroup dev
635 * Get the \ref drv "driver" for a fingerprint device.
636 * \param dev the device
637 * \returns the driver controlling the device
639 API_EXPORTED struct fp_driver *fp_dev_get_driver(struct fp_dev *dev)
641 return dev->drv;
644 /** \ingroup dev
645 * Gets the number of \ref enrolling "enroll stages" required to enroll a
646 * fingerprint with the device.
647 * \param dev the device
648 * \returns the number of enroll stages
650 API_EXPORTED int fp_dev_get_nr_enroll_stages(struct fp_dev *dev)
652 return dev->nr_enroll_stages;
655 /** \ingroup dev
656 * Gets the \ref devtype "devtype" for a device.
657 * \param dev the device
658 * \returns the devtype
660 API_EXPORTED uint32_t fp_dev_get_devtype(struct fp_dev *dev)
662 return dev->devtype;
665 /** \ingroup dev
666 * Determines if a stored print is compatible with a certain device.
667 * \param dev the device
668 * \param data the stored print
669 * \returns 1 if the print is compatible with the device, 0 if not
671 API_EXPORTED int fp_dev_supports_print_data(struct fp_dev *dev,
672 struct fp_print_data *data)
674 return fpi_print_data_compatible(dev->drv->id, dev->devtype,
675 fpi_driver_get_data_type(dev->drv), data->driver_id, data->devtype,
676 data->type);
679 /** \ingroup dev
680 * Determines if a \ref dscv_print "discovered print" appears to be compatible
681 * with a certain device.
682 * \param dev the device
683 * \param data the discovered print
684 * \returns 1 if the print is compatible with the device, 0 if not
686 API_EXPORTED int fp_dev_supports_dscv_print(struct fp_dev *dev,
687 struct fp_dscv_print *data)
689 return fpi_print_data_compatible(dev->drv->id, dev->devtype,
690 0, data->driver_id, data->devtype, 0);
693 /** \ingroup drv
694 * Retrieves the name of the driver. For example: "upekts"
695 * \param drv the driver
696 * \returns the driver name. Must not be modified or freed.
698 API_EXPORTED const char *fp_driver_get_name(struct fp_driver *drv)
700 return drv->name;
703 /** \ingroup drv
704 * Retrieves a descriptive name of the driver. For example: "UPEK TouchStrip"
705 * \param drv the driver
706 * \returns the descriptive name. Must not be modified or freed.
708 API_EXPORTED const char *fp_driver_get_full_name(struct fp_driver *drv)
710 return drv->full_name;
713 /** \ingroup drv
714 * Retrieves the driver ID code for a driver.
715 * \param drv the driver
716 * \returns the driver ID
718 API_EXPORTED uint16_t fp_driver_get_driver_id(struct fp_driver *drv)
720 return drv->id;
723 static struct fp_img_dev *dev_to_img_dev(struct fp_dev *dev)
725 if (dev->drv->type != DRIVER_IMAGING)
726 return NULL;
727 return dev->priv;
730 /** \ingroup dev
731 * Determines if a device has imaging capabilities. If a device has imaging
732 * capabilities you are able to perform imaging operations such as retrieving
733 * scan images using fp_dev_img_capture(). However, not all devices are
734 * imaging devices - some do all processing in hardware. This function will
735 * indicate which class a device in question falls into.
736 * \param dev the fingerprint device
737 * \returns 1 if the device is an imaging device, 0 if the device does not
738 * provide images to the host computer
740 API_EXPORTED int fp_dev_supports_imaging(struct fp_dev *dev)
742 return dev->drv->type == DRIVER_IMAGING;
745 /** \ingroup dev
746 * Captures an \ref img "image" from a device. The returned image is the raw
747 * image provided by the device, you may wish to \ref img_std "standardize" it.
749 * If set, the <tt>unconditional</tt> flag indicates that the device should
750 * capture an image unconditionally, regardless of whether a finger is there
751 * or not. If unset, this function will block until a finger is detected on
752 * the sensor.
754 * \param dev the device
755 * \param unconditional whether to unconditionally capture an image, or to only capture when a finger is detected
756 * \param image a location to return the captured image. Must be freed with
757 * fp_img_free() after use.
758 * \return 0 on success, non-zero on error. -ENOTSUP indicates that either the
759 * unconditional flag was set but the device does not support this, or that the
760 * device does not support imaging.
761 * \sa fp_dev_supports_imaging()
763 API_EXPORTED int fp_dev_img_capture(struct fp_dev *dev, int unconditional,
764 struct fp_img **image)
766 struct fp_img_dev *imgdev = dev_to_img_dev(dev);
767 if (!imgdev) {
768 fp_dbg("image capture on non-imaging device");
769 return -ENOTSUP;
772 return fpi_imgdev_capture(imgdev, unconditional, image);
775 /** \ingroup dev
776 * Gets the expected width of images that will be captured from the device.
777 * This function will return -1 for devices that are not
778 * \ref imaging "imaging devices". If the width of images from this device
779 * can vary, 0 will be returned.
780 * \param dev the device
781 * \returns the expected image width, or 0 for variable, or -1 for non-imaging
782 * devices.
784 API_EXPORTED int fp_dev_get_img_width(struct fp_dev *dev)
786 struct fp_img_dev *imgdev = dev_to_img_dev(dev);
787 if (!imgdev) {
788 fp_dbg("get image width for non-imaging device");
789 return -1;
792 return fpi_imgdev_get_img_width(imgdev);
795 /** \ingroup dev
796 * Gets the expected height of images that will be captured from the device.
797 * This function will return -1 for devices that are not
798 * \ref imaging "imaging devices". If the height of images from this device
799 * can vary, 0 will be returned.
800 * \param dev the device
801 * \returns the expected image height, or 0 for variable, or -1 for non-imaging
802 * devices.
804 API_EXPORTED int fp_dev_get_img_height(struct fp_dev *dev)
806 struct fp_img_dev *imgdev = dev_to_img_dev(dev);
807 if (!imgdev) {
808 fp_dbg("get image height for non-imaging device");
809 return -1;
812 return fpi_imgdev_get_img_height(imgdev);
815 /** \ingroup dev
816 * Performs an enroll stage. See \ref enrolling for an explanation of enroll
817 * stages.
819 * If no enrollment is in process, this kicks of the process and runs the
820 * first stage. If an enrollment is already in progress, calling this
821 * function runs the next stage, which may well be the last.
823 * A negative error code may be returned from any stage. When this occurs,
824 * further calls to the enroll function will start a new enrollment process,
825 * i.e. a negative error code indicates that the enrollment process has been
826 * aborted. These error codes only ever indicate unexpected internal errors
827 * or I/O problems.
829 * The RETRY codes from #fp_enroll_result may be returned from any enroll
830 * stage. These codes indicate that the scan was not succesful in that the
831 * user did not position their finger correctly or similar. When a RETRY code
832 * is returned, the enrollment stage is <b>not</b> advanced, so the next call
833 * into this function will retry the current stage again. The current stage may
834 * need to be retried several times.
836 * The fp_enroll_result#FP_ENROLL_FAIL code may be returned from any enroll
837 * stage. This code indicates that even though the scans themselves have been
838 * acceptable, data processing applied to these scans produces incomprehensible
839 * results. In other words, the user may have been scanning a different finger
840 * for each stage or something like that. Like negative error codes, this
841 * return code indicates that the enrollment process has been aborted.
843 * The fp_enroll_result#FP_ENROLL_PASS code will only ever be returned for
844 * non-final stages. This return code indicates that the scan was acceptable
845 * and the next call into this function will advance onto the next enroll
846 * stage.
848 * The fp_enroll_result#FP_ENROLL_COMPLETE code will only ever be returned
849 * from the final enroll stage. It indicates that enrollment completed
850 * successfully, and that print_data has been assigned to point to the
851 * resultant enrollment data. The print_data parameter will not be modified
852 * during any other enrollment stages, hence it is actually legal to pass NULL
853 * as this argument for all but the final stage.
855 * If the device is an imaging device, it can also return the image from
856 * the scan, even when the enroll fails with a RETRY or FAIL code. It is legal
857 * to call this function even on non-imaging devices, just don't expect them to
858 * provide images.
860 * \param dev the device
861 * \param print_data a location to return the resultant enrollment data from
862 * the final stage. Must be freed with fp_print_data_free() after use.
863 * \param img location to store the scan image. accepts NULL for no image
864 * storage. If an image is returned, it must be freed with fp_img_free() after
865 * use.
866 * \return negative code on error, otherwise a code from #fp_enroll_result
868 API_EXPORTED int fp_enroll_finger_img(struct fp_dev *dev,
869 struct fp_print_data **print_data, struct fp_img **img)
871 struct fp_driver *drv = dev->drv;
872 struct fp_img *_img = NULL;
873 int ret;
874 int stage = dev->__enroll_stage;
875 gboolean initial = FALSE;
877 if (!dev->nr_enroll_stages || !drv->enroll) {
878 fp_err("driver %s has 0 enroll stages or no enroll func",
879 drv->name);
880 return -ENOTSUP;
883 if (stage == -1) {
884 initial = TRUE;
885 dev->__enroll_stage = ++stage;
888 if (stage >= dev->nr_enroll_stages) {
889 fp_err("exceeding number of enroll stages for device claimed by "
890 "driver %s (%d stages)", drv->name, dev->nr_enroll_stages);
891 dev->__enroll_stage = -1;
892 return -EINVAL;
894 fp_dbg("%s will handle enroll stage %d/%d%s", drv->name, stage,
895 dev->nr_enroll_stages - 1, initial ? " (initial)" : "");
897 ret = drv->enroll(dev, initial, stage, print_data, &_img);
898 if (ret < 0) {
899 fp_err("enroll failed with code %d", ret);
900 dev->__enroll_stage = -1;
901 return ret;
904 if (img)
905 *img = _img;
906 else
907 fp_img_free(_img);
909 switch (ret) {
910 case FP_ENROLL_PASS:
911 fp_dbg("enroll stage passed");
912 dev->__enroll_stage = stage + 1;
913 break;
914 case FP_ENROLL_COMPLETE:
915 fp_dbg("enroll complete");
916 dev->__enroll_stage = -1;
917 break;
918 case FP_ENROLL_RETRY:
919 fp_dbg("enroll should retry");
920 break;
921 case FP_ENROLL_RETRY_TOO_SHORT:
922 fp_dbg("swipe was too short, enroll should retry");
923 break;
924 case FP_ENROLL_RETRY_CENTER_FINGER:
925 fp_dbg("finger was not centered, enroll should retry");
926 break;
927 case FP_ENROLL_RETRY_REMOVE_FINGER:
928 fp_dbg("scan failed, remove finger and retry");
929 break;
930 case FP_ENROLL_FAIL:
931 fp_err("enroll failed");
932 dev->__enroll_stage = -1;
933 break;
934 default:
935 fp_err("unrecognised return code %d", ret);
936 dev->__enroll_stage = -1;
937 return -EINVAL;
939 return ret;
942 /** \ingroup dev
943 * Performs a new scan and verify it against a previously enrolled print.
944 * If the device is an imaging device, it can also return the image from
945 * the scan, even when the verify fails with a RETRY code. It is legal to
946 * call this function even on non-imaging devices, just don't expect them to
947 * provide images.
949 * \param dev the device to perform the scan.
950 * \param enrolled_print the print to verify against. Must have been previously
951 * enrolled with a device compatible to the device selected to perform the scan.
952 * \param img location to store the scan image. accepts NULL for no image
953 * storage. If an image is returned, it must be freed with fp_img_free() after
954 * use.
955 * \return negative code on error, otherwise a code from #fp_verify_result
957 API_EXPORTED int fp_verify_finger_img(struct fp_dev *dev,
958 struct fp_print_data *enrolled_print, struct fp_img **img)
960 struct fp_driver *drv = dev->drv;
961 struct fp_img *_img = NULL;
962 int r;
964 if (!enrolled_print) {
965 fp_err("no print given");
966 return -EINVAL;
969 if (!drv->verify) {
970 fp_err("driver %s has no verify func", drv->name);
971 return -EINVAL;
974 if (!fp_dev_supports_print_data(dev, enrolled_print)) {
975 fp_err("print is not compatible with device");
976 return -EINVAL;
979 fp_dbg("to be handled by %s", drv->name);
980 r = drv->verify(dev, enrolled_print, &_img);
981 if (r < 0) {
982 fp_dbg("verify error %d", r);
983 return r;
986 if (img)
987 *img = _img;
988 else
989 fp_img_free(_img);
991 switch (r) {
992 case FP_VERIFY_NO_MATCH:
993 fp_dbg("result: no match");
994 break;
995 case FP_VERIFY_MATCH:
996 fp_dbg("result: match");
997 break;
998 case FP_VERIFY_RETRY:
999 fp_dbg("verify should retry");
1000 break;
1001 case FP_VERIFY_RETRY_TOO_SHORT:
1002 fp_dbg("swipe was too short, verify should retry");
1003 break;
1004 case FP_VERIFY_RETRY_CENTER_FINGER:
1005 fp_dbg("finger was not centered, verify should retry");
1006 break;
1007 case FP_VERIFY_RETRY_REMOVE_FINGER:
1008 fp_dbg("scan failed, remove finger and retry");
1009 break;
1010 default:
1011 fp_err("unrecognised return code %d", r);
1012 return -EINVAL;
1015 return r;
1018 /** \ingroup core
1019 * Initialise libfprint. This function must be called before you attempt to
1020 * use the library in any way.
1021 * \return 0 on success, non-zero on error.
1023 API_EXPORTED int fp_init(void)
1025 fp_dbg("");
1026 usb_init();
1027 register_drivers();
1028 return 0;
1031 /** \ingroup core
1032 * Deinitialise libfprint. This function should be called during your program
1033 * exit sequence. You must not use any libfprint functions after calling this
1034 * function, unless you call fp_init() again.
1036 API_EXPORTED void fp_exit(void)
1038 GSList *elem = opened_devices;
1039 fp_dbg("");
1041 if (elem != NULL) {
1042 do {
1043 fp_dbg("naughty app left a device open on exit!");
1044 do_close((struct fp_dev *) elem->data);
1045 } while (elem = g_slist_next(elem));
1046 g_slist_free(opened_devices);
1047 opened_devices = NULL;
1050 fpi_data_exit();
1051 g_slist_free(registered_drivers);
1052 registered_drivers = NULL;