Clean up on exit, and convert to singly-linked-lists
[libfprint.git] / examples / verify_live.c
blob0e1db6d71e02751e218999d5832861eed1326013
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 = NULL;
29 int i;
31 for (i = 0; ddev = discovered_devs[i]; i++) {
32 struct fp_driver *drv = fp_dscv_dev_get_driver(ddev);
33 printf("Found device claimed by %s driver\n",
34 fp_driver_get_full_name(drv));
35 return ddev;
38 return ddev;
41 struct fp_print_data *enroll(struct fp_dev *dev) {
42 struct fp_print_data *enrolled_print = NULL;
43 int r;
45 printf("You will need to successfully scan your finger %d times to "
46 "complete the process.\n", fp_dev_get_nr_enroll_stages(dev));
48 do {
49 sleep(1);
50 printf("\nScan your finger now.\n");
51 r = fp_enroll_finger(dev, &enrolled_print);
52 if (r < 0) {
53 printf("Enroll failed with error %d\n", r);
54 return NULL;
56 switch (r) {
57 case FP_ENROLL_COMPLETE:
58 printf("Enroll complete!\n");
59 break;
60 case FP_ENROLL_FAIL:
61 printf("Enroll failed, something wen't wrong :(\n");
62 return NULL;
63 case FP_ENROLL_PASS:
64 printf("Enroll stage passed. Yay!\n");
65 break;
66 case FP_ENROLL_RETRY:
67 printf("Didn't quite catch that. Please try again.\n");
68 break;
69 case FP_ENROLL_RETRY_TOO_SHORT:
70 printf("Your swipe was too short, please try again.\n");
71 break;
72 case FP_ENROLL_RETRY_CENTER_FINGER:
73 printf("Didn't catch that, please center your finger on the "
74 "sensor and try again.\n");
75 break;
76 case FP_ENROLL_RETRY_REMOVE_FINGER:
77 printf("Scan failed, please remove your finger and then try "
78 "again.\n");
79 break;
81 } while (r != FP_ENROLL_COMPLETE);
83 if (!enrolled_print) {
84 fprintf(stderr, "Enroll complete but no print?\n");
85 return NULL;
88 printf("Enrollment completed!\n\n");
89 return enrolled_print;
92 int verify(struct fp_dev *dev, struct fp_print_data *data)
94 int r;
96 do {
97 sleep(1);
98 printf("\nScan your finger now.\n");
99 r = fp_verify_finger(dev, data);
100 if (r < 0) {
101 printf("verification failed with error %d :(\n", r);
102 return r;
104 switch (r) {
105 case FP_VERIFY_NO_MATCH:
106 printf("NO MATCH!\n");
107 return 0;
108 case FP_VERIFY_MATCH:
109 printf("MATCH!\n");
110 return 0;
111 case FP_VERIFY_RETRY:
112 printf("Scan didn't quite work. Please try again.\n");
113 break;
114 case FP_VERIFY_RETRY_TOO_SHORT:
115 printf("Swipe was too short, please try again.\n");
116 break;
117 case FP_VERIFY_RETRY_CENTER_FINGER:
118 printf("Please center your finger on the sensor and try again.\n");
119 break;
120 case FP_VERIFY_RETRY_REMOVE_FINGER:
121 printf("Please remove finger from the sensor and try again.\n");
122 break;
124 } while (1);
127 int main(void)
129 int r = 1;
130 struct fp_dscv_dev *ddev;
131 struct fp_dscv_dev **discovered_devs;
132 struct fp_dev *dev;
133 struct fp_print_data *data;
135 r = fp_init();
136 if (r < 0) {
137 fprintf(stderr, "Failed to initialize libfprint\n");
138 exit(1);
141 discovered_devs = fp_discover_devs();
142 if (!discovered_devs) {
143 fprintf(stderr, "Could not discover devices\n");
144 goto out;
147 ddev = discover_device(discovered_devs);
148 if (!ddev) {
149 fprintf(stderr, "No devices detected.\n");
150 goto out;
153 dev = fp_dev_open(ddev);
154 fp_dscv_devs_free(discovered_devs);
155 if (!dev) {
156 fprintf(stderr, "Could not open device.\n");
157 goto out;
160 printf("Opened device. It's now time to enroll your finger.\n\n");
161 data = enroll(dev);
162 if (!data)
163 goto out_close;
166 printf("Normally we'd save that print to disk, and recall it at some "
167 "point later when we want to authenticate the user who just "
168 "enrolled. In the interests of demonstration, we'll authenticate "
169 "that user immediately.\n");
171 do {
172 char buffer[20];
174 verify(dev, data);
175 printf("Verify again? [Y/n]? ");
176 fgets(buffer, sizeof(buffer), stdin);
177 if (buffer[0] != '\n' && buffer[0] != 'y' && buffer[0] != 'Y')
178 break;
179 } while (1);
181 fp_print_data_free(data);
182 out_close:
183 fp_dev_close(dev);
184 out:
185 fp_exit();
186 return r;