Fix fp_get_pollfds()
[libfprint.git] / examples / verify_live.c
blob27acb3ba978e1c24f8766452cb509113ead91780
1 /*
2 * Example fingerprint verification program
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 <stdio.h>
21 #include <stdlib.h>
22 #include <unistd.h>
24 #include <libfprint/fprint.h>
26 struct fp_dscv_dev *discover_device(struct fp_dscv_dev **discovered_devs)
28 struct fp_dscv_dev *ddev = discovered_devs[0];
29 struct fp_driver *drv;
30 if (!ddev)
31 return NULL;
33 drv = fp_dscv_dev_get_driver(ddev);
34 printf("Found device claimed by %s driver\n", fp_driver_get_full_name(drv));
35 return ddev;
38 struct fp_print_data *enroll(struct fp_dev *dev) {
39 struct fp_print_data *enrolled_print = NULL;
40 int r;
42 printf("You will need to successfully scan your finger %d times to "
43 "complete the process.\n", fp_dev_get_nr_enroll_stages(dev));
45 do {
46 sleep(1);
47 printf("\nScan your finger now.\n");
48 r = fp_enroll_finger(dev, &enrolled_print);
49 if (r < 0) {
50 printf("Enroll failed with error %d\n", r);
51 return NULL;
53 switch (r) {
54 case FP_ENROLL_COMPLETE:
55 printf("Enroll complete!\n");
56 break;
57 case FP_ENROLL_FAIL:
58 printf("Enroll failed, something wen't wrong :(\n");
59 return NULL;
60 case FP_ENROLL_PASS:
61 printf("Enroll stage passed. Yay!\n");
62 break;
63 case FP_ENROLL_RETRY:
64 printf("Didn't quite catch that. Please try again.\n");
65 break;
66 case FP_ENROLL_RETRY_TOO_SHORT:
67 printf("Your swipe was too short, please try again.\n");
68 break;
69 case FP_ENROLL_RETRY_CENTER_FINGER:
70 printf("Didn't catch that, please center your finger on the "
71 "sensor and try again.\n");
72 break;
73 case FP_ENROLL_RETRY_REMOVE_FINGER:
74 printf("Scan failed, please remove your finger and then try "
75 "again.\n");
76 break;
78 } while (r != FP_ENROLL_COMPLETE);
80 if (!enrolled_print) {
81 fprintf(stderr, "Enroll complete but no print?\n");
82 return NULL;
85 printf("Enrollment completed!\n\n");
86 return enrolled_print;
89 int verify(struct fp_dev *dev, struct fp_print_data *data)
91 int r;
93 do {
94 sleep(1);
95 printf("\nScan your finger now.\n");
96 r = fp_verify_finger(dev, data);
97 if (r < 0) {
98 printf("verification failed with error %d :(\n", r);
99 return r;
101 switch (r) {
102 case FP_VERIFY_NO_MATCH:
103 printf("NO MATCH!\n");
104 return 0;
105 case FP_VERIFY_MATCH:
106 printf("MATCH!\n");
107 return 0;
108 case FP_VERIFY_RETRY:
109 printf("Scan didn't quite work. Please try again.\n");
110 break;
111 case FP_VERIFY_RETRY_TOO_SHORT:
112 printf("Swipe was too short, please try again.\n");
113 break;
114 case FP_VERIFY_RETRY_CENTER_FINGER:
115 printf("Please center your finger on the sensor and try again.\n");
116 break;
117 case FP_VERIFY_RETRY_REMOVE_FINGER:
118 printf("Please remove finger from the sensor and try again.\n");
119 break;
121 } while (1);
124 int main(void)
126 int r = 1;
127 struct fp_dscv_dev *ddev;
128 struct fp_dscv_dev **discovered_devs;
129 struct fp_dev *dev;
130 struct fp_print_data *data;
132 r = fp_init();
133 if (r < 0) {
134 fprintf(stderr, "Failed to initialize libfprint\n");
135 exit(1);
137 fp_set_debug(3);
139 discovered_devs = fp_discover_devs();
140 if (!discovered_devs) {
141 fprintf(stderr, "Could not discover devices\n");
142 goto out;
145 ddev = discover_device(discovered_devs);
146 if (!ddev) {
147 fprintf(stderr, "No devices detected.\n");
148 goto out;
151 dev = fp_dev_open(ddev);
152 fp_dscv_devs_free(discovered_devs);
153 if (!dev) {
154 fprintf(stderr, "Could not open device.\n");
155 goto out;
158 printf("Opened device. It's now time to enroll your finger.\n\n");
159 data = enroll(dev);
160 if (!data)
161 goto out_close;
164 printf("Normally we'd save that print to disk, and recall it at some "
165 "point later when we want to authenticate the user who just "
166 "enrolled. In the interests of demonstration, we'll authenticate "
167 "that user immediately.\n");
169 do {
170 char buffer[20];
172 verify(dev, data);
173 printf("Verify again? [Y/n]? ");
174 fgets(buffer, sizeof(buffer), stdin);
175 if (buffer[0] != '\n' && buffer[0] != 'y' && buffer[0] != 'Y')
176 break;
177 } while (1);
179 fp_print_data_free(data);
180 out_close:
181 fp_dev_close(dev);
182 out:
183 fp_exit();
184 return r;