1 /*
2  * Internal/private definitions for libfprint
3  * Copyright (C) 2007-2008 Daniel Drake <dsd@gentoo.org>
4  *
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.
9  *
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.
14  *
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
18  */
19 
20 #ifndef __FPRINT_INTERNAL_H__
21 #define __FPRINT_INTERNAL_H__
22 
23 #include <config.h>
24 #include <stdint.h>
25 
26 #include <glib.h>
27 #include <libusb.h>
28 
29 #include <fprint.h>
30 
31 #define array_n_elements(array) (sizeof(array) / sizeof(array[0]))
32 
33 #define container_of(ptr, type, member) ({                      \
34         const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
35         (type *)( (char *)__mptr - offsetof(type,member) );})
36 
37 enum fpi_log_level {
38 	FPRINT_LOG_LEVEL_DEBUG,
39 	FPRINT_LOG_LEVEL_INFO,
40 	FPRINT_LOG_LEVEL_WARNING,
41 	FPRINT_LOG_LEVEL_ERROR,
42 };
43 
44 void fpi_log(enum fpi_log_level, const char *component, const char *function,
45 	const char *format, ...);
46 
47 #ifndef FP_COMPONENT
48 #define FP_COMPONENT NULL
49 #endif
50 
51 #ifdef ENABLE_LOGGING
52 #define _fpi_log(level, fmt...) fpi_log(level, FP_COMPONENT, __FUNCTION__, fmt)
53 #else
54 #define _fpi_log(level, fmt...)
55 #endif
56 
57 #ifdef ENABLE_DEBUG_LOGGING
58 #define fp_dbg(fmt...) _fpi_log(FPRINT_LOG_LEVEL_DEBUG, fmt)
59 #else
60 #define fp_dbg(fmt...)
61 #endif
62 
63 #define fp_info(fmt...) _fpi_log(FPRINT_LOG_LEVEL_INFO, fmt)
64 #define fp_warn(fmt...) _fpi_log(FPRINT_LOG_LEVEL_WARNING, fmt)
65 #define fp_err(fmt...) _fpi_log(FPRINT_LOG_LEVEL_ERROR, fmt)
66 
67 #ifndef NDEBUG
68 #define BUG_ON(condition) \
69 	if ((condition)) fp_err("BUG at %s:%d", __FILE__, __LINE__)
70 #else
71 #define BUG_ON(condition)
72 #endif
73 
74 #define BUG() BUG_ON(1)
75 
76 enum fp_dev_state {
77 	DEV_STATE_INITIAL = 0,
78 	DEV_STATE_ERROR,
79 	DEV_STATE_INITIALIZING,
80 	DEV_STATE_INITIALIZED,
81 	DEV_STATE_DEINITIALIZING,
82 	DEV_STATE_DEINITIALIZED,
83 	DEV_STATE_ENROLL_STARTING,
84 	DEV_STATE_ENROLLING,
85 	DEV_STATE_ENROLL_STOPPING,
86 	DEV_STATE_VERIFY_STARTING,
87 	DEV_STATE_VERIFYING,
88 	DEV_STATE_VERIFY_DONE,
89 	DEV_STATE_VERIFY_STOPPING,
90 	DEV_STATE_IDENTIFY_STARTING,
91 	DEV_STATE_IDENTIFYING,
92 	DEV_STATE_IDENTIFY_DONE,
93 	DEV_STATE_IDENTIFY_STOPPING,
94 	DEV_STATE_CAPTURE_STARTING,
95 	DEV_STATE_CAPTURING,
96 	DEV_STATE_CAPTURE_DONE,
97 	DEV_STATE_CAPTURE_STOPPING,
98 };
99 
100 struct fp_driver **fprint_get_drivers (void);
101 
102 struct fp_dev {
103 	struct fp_driver *drv;
104 	libusb_device_handle *udev;
105 	uint32_t devtype;
106 	void *priv;
107 
108 	int nr_enroll_stages;
109 
110 	/* read-only to drivers */
111 	struct fp_print_data *verify_data;
112 
113 	/* drivers should not mess with any of the below */
114 	enum fp_dev_state state;
115 	int __enroll_stage;
116 	int unconditional_capture;
117 
118 	/* async I/O callbacks and data */
119 	/* FIXME: convert this to generic state operational data mechanism? */
120 	fp_dev_open_cb open_cb;
121 	void *open_cb_data;
122 	fp_dev_close_cb close_cb;
123 	void *close_cb_data;
124 	fp_enroll_stage_cb enroll_stage_cb;
125 	void *enroll_stage_cb_data;
126 	fp_enroll_stop_cb enroll_stop_cb;
127 	void *enroll_stop_cb_data;
128 	fp_verify_cb verify_cb;
129 	void *verify_cb_data;
130 	fp_verify_stop_cb verify_stop_cb;
131 	void *verify_stop_cb_data;
132 	fp_identify_cb identify_cb;
133 	void *identify_cb_data;
134 	fp_identify_stop_cb identify_stop_cb;
135 	void *identify_stop_cb_data;
136 	fp_capture_cb capture_cb;
137 	void *capture_cb_data;
138 	fp_capture_stop_cb capture_stop_cb;
139 	void *capture_stop_cb_data;
140 
141 	/* FIXME: better place to put this? */
142 	struct fp_print_data **identify_gallery;
143 };
144 
145 enum fp_imgdev_state {
146 	IMGDEV_STATE_INACTIVE,
147 	IMGDEV_STATE_AWAIT_FINGER_ON,
148 	IMGDEV_STATE_CAPTURE,
149 	IMGDEV_STATE_AWAIT_FINGER_OFF,
150 };
151 
152 enum fp_imgdev_action {
153 	IMG_ACTION_NONE = 0,
154 	IMG_ACTION_ENROLL,
155 	IMG_ACTION_VERIFY,
156 	IMG_ACTION_IDENTIFY,
157 	IMG_ACTION_CAPTURE,
158 };
159 
160 enum fp_imgdev_enroll_state {
161 	IMG_ACQUIRE_STATE_NONE = 0,
162 	IMG_ACQUIRE_STATE_ACTIVATING,
163 	IMG_ACQUIRE_STATE_AWAIT_FINGER_ON,
164 	IMG_ACQUIRE_STATE_AWAIT_IMAGE,
165 	IMG_ACQUIRE_STATE_AWAIT_FINGER_OFF,
166 	IMG_ACQUIRE_STATE_DONE,
167 	IMG_ACQUIRE_STATE_DEACTIVATING,
168 };
169 
170 enum fp_imgdev_verify_state {
171 	IMG_VERIFY_STATE_NONE = 0,
172 	IMG_VERIFY_STATE_ACTIVATING
173 };
174 
175 struct fp_img_dev {
176 	struct fp_dev *dev;
177 	libusb_device_handle *udev;
178 	enum fp_imgdev_action action;
179 	int action_state;
180 
181 	struct fp_print_data *acquire_data;
182 	struct fp_print_data *enroll_data;
183 	struct fp_img *acquire_img;
184 	int enroll_stage;
185 	int action_result;
186 
187 	/* FIXME: better place to put this? */
188 	size_t identify_match_offset;
189 
190 	void *priv;
191 };
192 
193 int fpi_imgdev_get_img_width(struct fp_img_dev *imgdev);
194 int fpi_imgdev_get_img_height(struct fp_img_dev *imgdev);
195 
196 struct usb_id {
197 	uint16_t vendor;
198 	uint16_t product;
199 	unsigned long driver_data;
200 };
201 
202 enum fp_driver_type {
203 	DRIVER_PRIMITIVE = 0,
204 	DRIVER_IMAGING = 1,
205 };
206 
207 struct fp_driver {
208 	const uint16_t id;
209 	const char *name;
210 	const char *full_name;
211 	const struct usb_id * const id_table;
212 	enum fp_driver_type type;
213 	enum fp_scan_type scan_type;
214 
215 	void *priv;
216 
217 	/* Device operations */
218 	int (*discover)(struct libusb_device_descriptor *dsc, uint32_t *devtype);
219 	int (*open)(struct fp_dev *dev, unsigned long driver_data);
220 	void (*close)(struct fp_dev *dev);
221 	int (*enroll_start)(struct fp_dev *dev);
222 	int (*enroll_stop)(struct fp_dev *dev);
223 	int (*verify_start)(struct fp_dev *dev);
224 	int (*verify_stop)(struct fp_dev *dev, gboolean iterating);
225 	int (*identify_start)(struct fp_dev *dev);
226 	int (*identify_stop)(struct fp_dev *dev, gboolean iterating);
227 	int (*capture_start)(struct fp_dev *dev);
228 	int (*capture_stop)(struct fp_dev *dev);
229 };
230 
231 enum fp_print_data_type fpi_driver_get_data_type(struct fp_driver *drv);
232 
233 /* flags for fp_img_driver.flags */
234 #define FP_IMGDRV_SUPPORTS_UNCONDITIONAL_CAPTURE (1 << 0)
235 
236 struct fp_img_driver {
237 	struct fp_driver driver;
238 	uint16_t flags;
239 	int img_width;
240 	int img_height;
241 	int bz3_threshold;
242 
243 	/* Device operations */
244 	int (*open)(struct fp_img_dev *dev, unsigned long driver_data);
245 	void (*close)(struct fp_img_dev *dev);
246 	int (*activate)(struct fp_img_dev *dev, enum fp_imgdev_state state);
247 	int (*change_state)(struct fp_img_dev *dev, enum fp_imgdev_state state);
248 	void (*deactivate)(struct fp_img_dev *dev);
249 };
250 
251 #ifdef ENABLE_UPEKTS
252 extern struct fp_driver upekts_driver;
253 #endif
254 #ifdef ENABLE_UPEKE2
255 extern struct fp_driver upeke2_driver;
256 #endif
257 #ifdef ENABLE_UPEKTC
258 extern struct fp_img_driver upektc_driver;
259 #endif
260 #ifdef ENABLE_UPEKSONLY
261 extern struct fp_img_driver upeksonly_driver;
262 #endif
263 #ifdef ENABLE_URU4000
264 extern struct fp_img_driver uru4000_driver;
265 #endif
266 #ifdef ENABLE_AES1610
267 extern struct fp_img_driver aes1610_driver;
268 #endif
269 #ifdef ENABLE_AES1660
270 extern struct fp_img_driver aes1660_driver;
271 #endif
272 #ifdef ENABLE_AES2501
273 extern struct fp_img_driver aes2501_driver;
274 #endif
275 #ifdef ENABLE_AES2550
276 extern struct fp_img_driver aes2550_driver;
277 #endif
278 #ifdef ENABLE_AES2660
279 extern struct fp_img_driver aes2660_driver;
280 #endif
281 #ifdef ENABLE_AES3500
282 extern struct fp_img_driver aes3500_driver;
283 #endif
284 #ifdef ENABLE_AES4000
285 extern struct fp_img_driver aes4000_driver;
286 #endif
287 #ifdef ENABLE_FDU2000
288 extern struct fp_img_driver fdu2000_driver;
289 #endif
290 #ifdef ENABLE_VCOM5S
291 extern struct fp_img_driver vcom5s_driver;
292 #endif
293 #ifdef ENABLE_VFS101
294 extern struct fp_img_driver vfs101_driver;
295 #endif
296 #ifdef ENABLE_VFS301
297 extern struct fp_img_driver vfs301_driver;
298 #endif
299 #ifdef ENABLE_VFS5011
300 extern struct fp_img_driver vfs5011_driver;
301 #endif
302 #ifdef ENABLE_UPEKTC_IMG
303 extern struct fp_img_driver upektc_img_driver;
304 #endif
305 #ifdef ENABLE_ETES603
306 extern struct fp_img_driver etes603_driver;
307 #endif
308 #ifdef ENABLE_VFS0050
309 extern struct fp_img_driver vfs0050_driver;
310 #endif
311 
312 extern libusb_context *fpi_usb_ctx;
313 extern GSList *opened_devices;
314 
315 void fpi_img_driver_setup(struct fp_img_driver *idriver);
316 
317 #define fpi_driver_to_img_driver(drv) \
318 	container_of((drv), struct fp_img_driver, driver)
319 
320 struct fp_dscv_dev {
321 	struct libusb_device *udev;
322 	struct fp_driver *drv;
323 	unsigned long driver_data;
324 	uint32_t devtype;
325 };
326 
327 struct fp_dscv_print {
328 	uint16_t driver_id;
329 	uint32_t devtype;
330 	enum fp_finger finger;
331 	char *path;
332 };
333 
334 enum fp_print_data_type {
335 	PRINT_DATA_RAW = 0, /* memset-imposed default */
336 	PRINT_DATA_NBIS_MINUTIAE,
337 };
338 
339 struct fp_print_data_item {
340 	size_t length;
341 	unsigned char data[0];
342 };
343 
344 struct fp_print_data {
345 	uint16_t driver_id;
346 	uint32_t devtype;
347 	enum fp_print_data_type type;
348 	GSList *prints;
349 };
350 
351 struct fpi_print_data_fp2 {
352 	char prefix[3];
353 	uint16_t driver_id;
354 	uint32_t devtype;
355 	unsigned char data_type;
356 	unsigned char data[0];
357 } __attribute__((__packed__));
358 
359 struct fpi_print_data_item_fp2 {
360 	uint32_t length;
361 	unsigned char data[0];
362 } __attribute__((__packed__));
363 
364 void fpi_data_exit(void);
365 struct fp_print_data *fpi_print_data_new(struct fp_dev *dev);
366 struct fp_print_data_item *fpi_print_data_item_new(size_t length);
367 gboolean fpi_print_data_compatible(uint16_t driver_id1, uint32_t devtype1,
368 	enum fp_print_data_type type1, uint16_t driver_id2, uint32_t devtype2,
369 	enum fp_print_data_type type2);
370 
371 struct fp_minutiae {
372 	int alloc;
373 	int num;
374 	struct fp_minutia **list;
375 };
376 
377 /* bit values for fp_img.flags */
378 #define FP_IMG_V_FLIPPED	(1<<0)
379 #define FP_IMG_H_FLIPPED	(1<<1)
380 #define FP_IMG_COLORS_INVERTED	(1<<2)
381 #define FP_IMG_BINARIZED_FORM	(1<<3)
382 #define FP_IMG_PARTIAL		(1<<4)
383 
384 #define FP_IMG_STANDARDIZATION_FLAGS (FP_IMG_V_FLIPPED | FP_IMG_H_FLIPPED \
385 	| FP_IMG_COLORS_INVERTED)
386 
387 struct fp_img {
388 	int width;
389 	int height;
390 	size_t length;
391 	uint16_t flags;
392 	struct fp_minutiae *minutiae;
393 	unsigned char *binarized;
394 	unsigned char data[0];
395 };
396 
397 struct fp_img *fpi_img_new(size_t length);
398 struct fp_img *fpi_img_new_for_imgdev(struct fp_img_dev *dev);
399 struct fp_img *fpi_img_resize(struct fp_img *img, size_t newsize);
400 gboolean fpi_img_is_sane(struct fp_img *img);
401 int fpi_img_detect_minutiae(struct fp_img *img);
402 int fpi_img_to_print_data(struct fp_img_dev *imgdev, struct fp_img *img,
403 	struct fp_print_data **ret);
404 int fpi_img_compare_print_data(struct fp_print_data *enrolled_print,
405 	struct fp_print_data *new_print);
406 int fpi_img_compare_print_data_to_gallery(struct fp_print_data *print,
407 	struct fp_print_data **gallery, int match_threshold, size_t *match_offset);
408 struct fp_img *fpi_im_resize(struct fp_img *img, unsigned int w_factor, unsigned int h_factor);
409 
410 /* polling and timeouts */
411 
412 void fpi_poll_init(void);
413 void fpi_poll_exit(void);
414 
415 typedef void (*fpi_timeout_fn)(void *data);
416 
417 struct fpi_timeout;
418 struct fpi_timeout *fpi_timeout_add(unsigned int msec, fpi_timeout_fn callback,
419 	void *data);
420 void fpi_timeout_cancel(struct fpi_timeout *timeout);
421 
422 /* async drv <--> lib comms */
423 
424 struct fpi_ssm;
425 typedef void (*ssm_completed_fn)(struct fpi_ssm *ssm);
426 typedef void (*ssm_handler_fn)(struct fpi_ssm *ssm);
427 
428 /* sequential state machine: state machine that iterates sequentially over
429  * a predefined series of states. can be aborted by either completion or
430  * abortion error conditions. */
431 struct fpi_ssm {
432 	struct fp_dev *dev;
433 	struct fpi_ssm *parentsm;
434 	void *priv;
435 	int nr_states;
436 	int cur_state;
437 	gboolean completed;
438 	int error;
439 	ssm_completed_fn callback;
440 	ssm_handler_fn handler;
441 };
442 
443 
444 /* for library and drivers */
445 struct fpi_ssm *fpi_ssm_new(struct fp_dev *dev, ssm_handler_fn handler,
446 	int nr_states);
447 void fpi_ssm_free(struct fpi_ssm *machine);
448 void fpi_ssm_start(struct fpi_ssm *machine, ssm_completed_fn callback);
449 void fpi_ssm_start_subsm(struct fpi_ssm *parent, struct fpi_ssm *child);
450 int fpi_ssm_has_completed(struct fpi_ssm *machine);
451 
452 /* for drivers */
453 void fpi_ssm_next_state(struct fpi_ssm *machine);
454 void fpi_ssm_jump_to_state(struct fpi_ssm *machine, int state);
455 void fpi_ssm_mark_completed(struct fpi_ssm *machine);
456 void fpi_ssm_mark_aborted(struct fpi_ssm *machine, int error);
457 
458 void fpi_drvcb_open_complete(struct fp_dev *dev, int status);
459 void fpi_drvcb_close_complete(struct fp_dev *dev);
460 
461 void fpi_drvcb_enroll_started(struct fp_dev *dev, int status);
462 void fpi_drvcb_enroll_stage_completed(struct fp_dev *dev, int result,
463 	struct fp_print_data *data, struct fp_img *img);
464 void fpi_drvcb_enroll_stopped(struct fp_dev *dev);
465 
466 void fpi_drvcb_verify_started(struct fp_dev *dev, int status);
467 void fpi_drvcb_report_verify_result(struct fp_dev *dev, int result,
468 	struct fp_img *img);
469 void fpi_drvcb_verify_stopped(struct fp_dev *dev);
470 
471 void fpi_drvcb_identify_started(struct fp_dev *dev, int status);
472 void fpi_drvcb_report_identify_result(struct fp_dev *dev, int result,
473 	size_t match_offset, struct fp_img *img);
474 void fpi_drvcb_identify_stopped(struct fp_dev *dev);
475 
476 void fpi_drvcb_capture_started(struct fp_dev *dev, int status);
477 void fpi_drvcb_report_capture_result(struct fp_dev *dev, int result,
478 	struct fp_img *img);
479 void fpi_drvcb_capture_stopped(struct fp_dev *dev);
480 
481 /* for image drivers */
482 void fpi_imgdev_open_complete(struct fp_img_dev *imgdev, int status);
483 void fpi_imgdev_close_complete(struct fp_img_dev *imgdev);
484 void fpi_imgdev_activate_complete(struct fp_img_dev *imgdev, int status);
485 void fpi_imgdev_deactivate_complete(struct fp_img_dev *imgdev);
486 void fpi_imgdev_report_finger_status(struct fp_img_dev *imgdev,
487 	gboolean present);
488 void fpi_imgdev_image_captured(struct fp_img_dev *imgdev, struct fp_img *img);
489 void fpi_imgdev_abort_scan(struct fp_img_dev *imgdev, int result);
490 void fpi_imgdev_session_error(struct fp_img_dev *imgdev, int error);
491 
492 /* utils */
493 int fpi_std_sq_dev(const unsigned char *buf, int size);
494 int fpi_mean_sq_diff_norm(unsigned char *buf1, unsigned char *buf2, int size);
495 
496 #endif
497 
498