Clean up on exit, and convert to singly-linked-lists
[libfprint.git] / examples / verify.c
blobef66d5e18fe92ba9a66f59ff203f8ba2230058c3
1 /*
2 * Example fingerprint verification program, which verifies the right index
3 * finger which has been previously enrolled to disk.
4 * Copyright (C) 2007 Daniel Drake <dsd@gentoo.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
25 #include <libfprint/fprint.h>
27 struct fp_dscv_dev *discover_device(struct fp_dscv_dev **discovered_devs)
29 struct fp_dscv_dev *ddev = NULL;
30 int i;
32 for (i = 0; ddev = discovered_devs[i]; i++) {
33 struct fp_driver *drv = fp_dscv_dev_get_driver(ddev);
34 printf("Found device claimed by %s driver\n",
35 fp_driver_get_full_name(drv));
36 return ddev;
39 return ddev;
42 int verify(struct fp_dev *dev, struct fp_print_data *data)
44 int r;
46 do {
47 sleep(1);
48 printf("\nScan your finger now.\n");
49 r = fp_verify_finger(dev, data);
50 if (r < 0) {
51 printf("verification failed with error %d :(\n", r);
52 return r;
54 switch (r) {
55 case FP_VERIFY_NO_MATCH:
56 printf("NO MATCH!\n");
57 return 0;
58 case FP_VERIFY_MATCH:
59 printf("MATCH!\n");
60 return 0;
61 case FP_VERIFY_RETRY:
62 printf("Scan didn't quite work. Please try again.\n");
63 break;
64 case FP_VERIFY_RETRY_TOO_SHORT:
65 printf("Swipe was too short, please try again.\n");
66 break;
67 case FP_VERIFY_RETRY_CENTER_FINGER:
68 printf("Please center your finger on the sensor and try again.\n");
69 break;
70 case FP_VERIFY_RETRY_REMOVE_FINGER:
71 printf("Please remove finger from the sensor and try again.\n");
72 break;
74 } while (1);
77 int main(void)
79 int r = 1;
80 struct fp_dscv_dev *ddev;
81 struct fp_dscv_dev **discovered_devs;
82 struct fp_dev *dev;
83 struct fp_print_data *data;
85 r = fp_init();
86 if (r < 0) {
87 fprintf(stderr, "Failed to initialize libfprint\n");
88 exit(1);
91 discovered_devs = fp_discover_devs();
92 if (!discovered_devs) {
93 fprintf(stderr, "Could not discover devices\n");
94 goto out;
97 ddev = discover_device(discovered_devs);
98 if (!ddev) {
99 fprintf(stderr, "No devices detected.\n");
100 goto out;
103 dev = fp_dev_open(ddev);
104 fp_dscv_devs_free(discovered_devs);
105 if (!dev) {
106 fprintf(stderr, "Could not open device.\n");
107 goto out;
110 printf("Opened device. Loading previously enrolled right index finger "
111 "data...\n");
113 r = fp_print_data_load(dev, RIGHT_INDEX, &data);
114 if (r != 0) {
115 fprintf(stderr, "Failed to load fingerprint, error %d\n", r);
116 fprintf(stderr, "Did you remember to enroll your right index finger "
117 "first?\n");
118 goto out_close;
121 printf("Print loaded. Time to verify!\n");
122 do {
123 char buffer[20];
125 verify(dev, data);
126 printf("Verify again? [Y/n]? ");
127 fgets(buffer, sizeof(buffer), stdin);
128 if (buffer[0] != '\n' && buffer[0] != 'y' && buffer[0] != 'Y')
129 break;
130 } while (1);
132 fp_print_data_free(data);
133 out_close:
134 fp_dev_close(dev);
135 out:
136 fp_exit();
137 return r;