storage: add save/load functionality
[libfprint.git] / examples / enroll.c
blob3ed5c8a0103efb3541dadb6b63b571d271ca18c1
1 /*
2 * Example fingerprint enrollment program
3 * Enrolls your right index finger and saves the print 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 const 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 struct fp_print_data *enroll(struct fp_dev *dev) {
43 struct fp_print_data *enrolled_print = NULL;
44 int r;
46 printf("You will need to successfully scan your finger %d times to "
47 "complete the process.\n", fp_dev_get_nr_enroll_stages(dev));
49 do {
50 sleep(1);
51 printf("\nScan your finger now.\n");
52 r = fp_enroll_finger(dev, &enrolled_print);
53 if (r < 0) {
54 printf("Enroll failed with error %d\n", r);
55 return NULL;
57 switch (r) {
58 case FP_ENROLL_COMPLETE:
59 printf("Enroll complete!\n");
60 break;
61 case FP_ENROLL_FAIL:
62 printf("Enroll failed, something wen't wrong :(\n");
63 return NULL;
64 case FP_ENROLL_PASS:
65 printf("Enroll stage passed. Yay!\n");
66 break;
67 case FP_ENROLL_RETRY:
68 printf("Didn't quite catch that. Please try again.\n");
69 break;
70 case FP_ENROLL_RETRY_TOO_SHORT:
71 printf("Your swipe was too short, please try again.\n");
72 break;
73 case FP_ENROLL_RETRY_CENTER_FINGER:
74 printf("Didn't catch that, please center your finger on the "
75 "sensor and try again.\n");
76 break;
77 case FP_ENROLL_RETRY_REMOVE_FINGER:
78 printf("Scan failed, please remove your finger and then try "
79 "again.\n");
80 break;
82 } while (r != FP_ENROLL_COMPLETE);
84 if (!enrolled_print) {
85 fprintf(stderr, "Enroll complete but no print?\n");
86 return NULL;
89 printf("Enrollment completed!\n\n");
90 return enrolled_print;
93 int main(void)
95 int r;
96 struct fp_dscv_dev *ddev;
97 struct fp_dscv_dev **discovered_devs;
98 struct fp_dev *dev;
99 struct fp_print_data *data;
101 printf("This program will enroll your right index finger, "
102 "unconditionally overwriting any right-index print that was enrolled "
103 "previously. If you want to continue, press enter, otherwise hit "
104 "Ctrl+C\n");
105 getchar();
107 r = fp_init();
108 if (r < 0) {
109 fprintf(stderr, "Failed to initialize libfprint\n");
110 exit(1);
113 discovered_devs = fp_discover_devs();
114 if (!discovered_devs) {
115 fprintf(stderr, "Could not discover devices\n");
116 exit(1);
119 ddev = discover_device(discovered_devs);
120 if (!ddev) {
121 fprintf(stderr, "No devices detected.\n");
122 exit(1);
125 dev = fp_dev_open(ddev);
126 fp_dscv_devs_free(discovered_devs);
127 if (!dev) {
128 fprintf(stderr, "Could not open device.\n");
129 exit(1);
132 printf("Opened device. It's now time to enroll your finger.\n\n");
133 data = enroll(dev);
134 if (!data)
135 goto out_close;
137 r = fp_print_data_save(data, RIGHT_INDEX);
138 if (r < 0)
139 fprintf(stderr, "Data save failed, code %d\n", r);
141 fp_print_data_free(data);
142 out_close:
143 fp_dev_close(dev);
144 return r;