Fix fp_get_pollfds()
[libfprint.git] / examples / verify.c
blob60a2ccb6b2048f1a4d2c8590aac3489141bf10a5
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 = discovered_devs[0];
30 struct fp_driver *drv;
31 if (!ddev)
32 return NULL;
34 drv = fp_dscv_dev_get_driver(ddev);
35 printf("Found device claimed by %s driver\n", fp_driver_get_full_name(drv));
36 return ddev;
39 int verify(struct fp_dev *dev, struct fp_print_data *data)
41 int r;
43 do {
44 struct fp_img *img = NULL;
46 sleep(1);
47 printf("\nScan your finger now.\n");
48 r = fp_verify_finger_img(dev, data, &img);
49 if (img) {
50 fp_img_save_to_file(img, "verify.pgm");
51 printf("Wrote scanned image to verify.pgm\n");
52 fp_img_free(img);
54 if (r < 0) {
55 printf("verification failed with error %d :(\n", r);
56 return r;
58 switch (r) {
59 case FP_VERIFY_NO_MATCH:
60 printf("NO MATCH!\n");
61 return 0;
62 case FP_VERIFY_MATCH:
63 printf("MATCH!\n");
64 return 0;
65 case FP_VERIFY_RETRY:
66 printf("Scan didn't quite work. Please try again.\n");
67 break;
68 case FP_VERIFY_RETRY_TOO_SHORT:
69 printf("Swipe was too short, please try again.\n");
70 break;
71 case FP_VERIFY_RETRY_CENTER_FINGER:
72 printf("Please center your finger on the sensor and try again.\n");
73 break;
74 case FP_VERIFY_RETRY_REMOVE_FINGER:
75 printf("Please remove finger from the sensor and try again.\n");
76 break;
78 } while (1);
81 int main(void)
83 int r = 1;
84 struct fp_dscv_dev *ddev;
85 struct fp_dscv_dev **discovered_devs;
86 struct fp_dev *dev;
87 struct fp_print_data *data;
89 r = fp_init();
90 if (r < 0) {
91 fprintf(stderr, "Failed to initialize libfprint\n");
92 exit(1);
94 fp_set_debug(3);
96 discovered_devs = fp_discover_devs();
97 if (!discovered_devs) {
98 fprintf(stderr, "Could not discover devices\n");
99 goto out;
102 ddev = discover_device(discovered_devs);
103 if (!ddev) {
104 fprintf(stderr, "No devices detected.\n");
105 goto out;
108 dev = fp_dev_open(ddev);
109 fp_dscv_devs_free(discovered_devs);
110 if (!dev) {
111 fprintf(stderr, "Could not open device.\n");
112 goto out;
115 printf("Opened device. Loading previously enrolled right index finger "
116 "data...\n");
118 r = fp_print_data_load(dev, RIGHT_INDEX, &data);
119 if (r != 0) {
120 fprintf(stderr, "Failed to load fingerprint, error %d\n", r);
121 fprintf(stderr, "Did you remember to enroll your right index finger "
122 "first?\n");
123 goto out_close;
126 printf("Print loaded. Time to verify!\n");
127 do {
128 char buffer[20];
130 verify(dev, data);
131 printf("Verify again? [Y/n]? ");
132 fgets(buffer, sizeof(buffer), stdin);
133 if (buffer[0] != '\n' && buffer[0] != 'y' && buffer[0] != 'Y')
134 break;
135 } while (1);
137 fp_print_data_free(data);
138 out_close:
139 fp_dev_close(dev);
140 out:
141 fp_exit();
142 return r;