fp_internal.h 13.6 KB
Newer Older
1 2
/*
 * Internal/private definitions for libfprint
3
 * Copyright (C) 2007-2008 Daniel Drake <dsd@gentoo.org>
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#ifndef __FPRINT_INTERNAL_H__
#define __FPRINT_INTERNAL_H__

Daniel Drake's avatar
Daniel Drake committed
23
#include <config.h>
24 25
#include <stdint.h>

Daniel Drake's avatar
Daniel Drake committed
26
#include <glib.h>
27
#include <libusb.h>
28 29 30

#include <fprint.h>

31 32
#define array_n_elements(array) (sizeof(array) / sizeof(array[0]))

33 34 35 36
#define container_of(ptr, type, member) ({                      \
        const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
        (type *)( (char *)__mptr - offsetof(type,member) );})

Daniel Drake's avatar
Daniel Drake committed
37
enum fpi_log_level {
38 39 40 41
	FPRINT_LOG_LEVEL_DEBUG,
	FPRINT_LOG_LEVEL_INFO,
	FPRINT_LOG_LEVEL_WARNING,
	FPRINT_LOG_LEVEL_ERROR,
Daniel Drake's avatar
Daniel Drake committed
42 43 44 45 46 47 48 49 50
};

void fpi_log(enum fpi_log_level, const char *component, const char *function,
	const char *format, ...);

#ifndef FP_COMPONENT
#define FP_COMPONENT NULL
#endif

Daniel Drake's avatar
Daniel Drake committed
51
#ifdef ENABLE_LOGGING
Daniel Drake's avatar
Daniel Drake committed
52
#define _fpi_log(level, fmt...) fpi_log(level, FP_COMPONENT, __FUNCTION__, fmt)
Daniel Drake's avatar
Daniel Drake committed
53 54 55
#else
#define _fpi_log(level, fmt...)
#endif
Daniel Drake's avatar
Daniel Drake committed
56

Daniel Drake's avatar
Daniel Drake committed
57
#ifdef ENABLE_DEBUG_LOGGING
58
#define fp_dbg(fmt...) _fpi_log(FPRINT_LOG_LEVEL_DEBUG, fmt)
Daniel Drake's avatar
Daniel Drake committed
59 60 61 62
#else
#define fp_dbg(fmt...)
#endif

63 64 65
#define fp_info(fmt...) _fpi_log(FPRINT_LOG_LEVEL_INFO, fmt)
#define fp_warn(fmt...) _fpi_log(FPRINT_LOG_LEVEL_WARNING, fmt)
#define fp_err(fmt...) _fpi_log(FPRINT_LOG_LEVEL_ERROR, fmt)
Daniel Drake's avatar
Daniel Drake committed
66

67
#ifndef NDEBUG
68 69 70 71 72 73
#define BUG_ON(condition) \
	if ((condition)) fp_err("BUG at %s:%d", __FILE__, __LINE__)
#else
#define BUG_ON(condition)
#endif

74 75
#define BUG() BUG_ON(1)

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
enum fp_dev_state {
	DEV_STATE_INITIAL = 0,
	DEV_STATE_ERROR,
	DEV_STATE_INITIALIZING,
	DEV_STATE_INITIALIZED,
	DEV_STATE_DEINITIALIZING,
	DEV_STATE_DEINITIALIZED,
	DEV_STATE_ENROLL_STARTING,
	DEV_STATE_ENROLLING,
	DEV_STATE_ENROLL_STOPPING,
	DEV_STATE_VERIFY_STARTING,
	DEV_STATE_VERIFYING,
	DEV_STATE_VERIFY_DONE,
	DEV_STATE_VERIFY_STOPPING,
	DEV_STATE_IDENTIFY_STARTING,
	DEV_STATE_IDENTIFYING,
	DEV_STATE_IDENTIFY_DONE,
	DEV_STATE_IDENTIFY_STOPPING,
94 95 96 97
	DEV_STATE_CAPTURE_STARTING,
	DEV_STATE_CAPTURING,
	DEV_STATE_CAPTURE_DONE,
	DEV_STATE_CAPTURE_STOPPING,
98 99
};

100 101
struct fp_driver **fprint_get_drivers (void);

Daniel Drake's avatar
Daniel Drake committed
102
struct fp_dev {
103
	struct fp_driver *drv;
Daniel Drake's avatar
Daniel Drake committed
104
	libusb_device_handle *udev;
105
	uint32_t devtype;
Daniel Drake's avatar
Daniel Drake committed
106
	void *priv;
Daniel Drake's avatar
Daniel Drake committed
107 108

	int nr_enroll_stages;
Daniel Drake's avatar
Daniel Drake committed
109

110 111 112 113 114
	/* read-only to drivers */
	struct fp_print_data *verify_data;

	/* drivers should not mess with any of the below */
	enum fp_dev_state state;
Daniel Drake's avatar
Daniel Drake committed
115
	int __enroll_stage;
116
	int unconditional_capture;
117 118 119 120 121 122 123 124 125 126 127

	/* async I/O callbacks and data */
	/* FIXME: convert this to generic state operational data mechanism? */
	fp_dev_open_cb open_cb;
	void *open_cb_data;
	fp_dev_close_cb close_cb;
	void *close_cb_data;
	fp_enroll_stage_cb enroll_stage_cb;
	void *enroll_stage_cb_data;
	fp_enroll_stop_cb enroll_stop_cb;
	void *enroll_stop_cb_data;
128
	fp_verify_cb verify_cb;
129 130 131
	void *verify_cb_data;
	fp_verify_stop_cb verify_stop_cb;
	void *verify_stop_cb_data;
132
	fp_identify_cb identify_cb;
133 134 135
	void *identify_cb_data;
	fp_identify_stop_cb identify_stop_cb;
	void *identify_stop_cb_data;
136 137 138 139
	fp_capture_cb capture_cb;
	void *capture_cb_data;
	fp_capture_stop_cb capture_stop_cb;
	void *capture_stop_cb_data;
140 141 142

	/* FIXME: better place to put this? */
	struct fp_print_data **identify_gallery;
Daniel Drake's avatar
Daniel Drake committed
143 144
};

145 146 147 148 149 150 151 152 153 154 155 156
enum fp_imgdev_state {
	IMGDEV_STATE_INACTIVE,
	IMGDEV_STATE_AWAIT_FINGER_ON,
	IMGDEV_STATE_CAPTURE,
	IMGDEV_STATE_AWAIT_FINGER_OFF,
};

enum fp_imgdev_action {
	IMG_ACTION_NONE = 0,
	IMG_ACTION_ENROLL,
	IMG_ACTION_VERIFY,
	IMG_ACTION_IDENTIFY,
157
	IMG_ACTION_CAPTURE,
158 159 160 161 162 163 164 165 166 167 168 169 170 171
};

enum fp_imgdev_enroll_state {
	IMG_ACQUIRE_STATE_NONE = 0,
	IMG_ACQUIRE_STATE_ACTIVATING,
	IMG_ACQUIRE_STATE_AWAIT_FINGER_ON,
	IMG_ACQUIRE_STATE_AWAIT_IMAGE,
	IMG_ACQUIRE_STATE_AWAIT_FINGER_OFF,
	IMG_ACQUIRE_STATE_DONE,
	IMG_ACQUIRE_STATE_DEACTIVATING,
};

enum fp_imgdev_verify_state {
	IMG_VERIFY_STATE_NONE = 0,
Bastien Nocera's avatar
Bastien Nocera committed
172
	IMG_VERIFY_STATE_ACTIVATING
173 174
};

175 176
struct fp_img_dev {
	struct fp_dev *dev;
Daniel Drake's avatar
Daniel Drake committed
177
	libusb_device_handle *udev;
178 179 180 181
	enum fp_imgdev_action action;
	int action_state;

	struct fp_print_data *acquire_data;
182
	struct fp_print_data *enroll_data;
183
	struct fp_img *acquire_img;
184
	int enroll_stage;
185 186 187 188 189
	int action_result;

	/* FIXME: better place to put this? */
	size_t identify_match_offset;

190 191 192
	void *priv;
};

193 194 195
int fpi_imgdev_get_img_width(struct fp_img_dev *imgdev);
int fpi_imgdev_get_img_height(struct fp_img_dev *imgdev);

196 197 198 199 200 201
struct usb_id {
	uint16_t vendor;
	uint16_t product;
	unsigned long driver_data;
};

202 203 204 205 206
enum fp_driver_type {
	DRIVER_PRIMITIVE = 0,
	DRIVER_IMAGING = 1,
};

207
struct fp_driver {
208
	const uint16_t id;
209 210 211
	const char *name;
	const char *full_name;
	const struct usb_id * const id_table;
212
	enum fp_driver_type type;
213
	enum fp_scan_type scan_type;
214 215

	void *priv;
Daniel Drake's avatar
Daniel Drake committed
216 217

	/* Device operations */
218
	int (*discover)(struct libusb_device_descriptor *dsc, uint32_t *devtype);
219 220
	int (*open)(struct fp_dev *dev, unsigned long driver_data);
	void (*close)(struct fp_dev *dev);
221 222 223 224 225 226
	int (*enroll_start)(struct fp_dev *dev);
	int (*enroll_stop)(struct fp_dev *dev);
	int (*verify_start)(struct fp_dev *dev);
	int (*verify_stop)(struct fp_dev *dev, gboolean iterating);
	int (*identify_start)(struct fp_dev *dev);
	int (*identify_stop)(struct fp_dev *dev, gboolean iterating);
227 228
	int (*capture_start)(struct fp_dev *dev);
	int (*capture_stop)(struct fp_dev *dev);
229 230
};

Daniel Drake's avatar
Daniel Drake committed
231 232
enum fp_print_data_type fpi_driver_get_data_type(struct fp_driver *drv);

233 234 235
/* flags for fp_img_driver.flags */
#define FP_IMGDRV_SUPPORTS_UNCONDITIONAL_CAPTURE (1 << 0)

236 237
struct fp_img_driver {
	struct fp_driver driver;
238
	uint16_t flags;
239 240
	int img_width;
	int img_height;
241
	int bz3_threshold;
242 243

	/* Device operations */
244 245
	int (*open)(struct fp_img_dev *dev, unsigned long driver_data);
	void (*close)(struct fp_img_dev *dev);
246 247 248
	int (*activate)(struct fp_img_dev *dev, enum fp_imgdev_state state);
	int (*change_state)(struct fp_img_dev *dev, enum fp_imgdev_state state);
	void (*deactivate)(struct fp_img_dev *dev);
249 250
};

251
#ifdef ENABLE_UPEKTS
252
extern struct fp_driver upekts_driver;
253 254
#endif
#ifdef ENABLE_UPEKTC
255
extern struct fp_img_driver upektc_driver;
256 257
#endif
#ifdef ENABLE_UPEKSONLY
258
extern struct fp_img_driver upeksonly_driver;
259 260
#endif
#ifdef ENABLE_URU4000
261
extern struct fp_img_driver uru4000_driver;
262 263
#endif
#ifdef ENABLE_AES1610
Daniel Drake's avatar
Daniel Drake committed
264
extern struct fp_img_driver aes1610_driver;
265
#endif
Vasily Khoruzhick's avatar
Vasily Khoruzhick committed
266 267 268
#ifdef ENABLE_AES1660
extern struct fp_img_driver aes1660_driver;
#endif
269
#ifdef ENABLE_AES2501
Daniel Drake's avatar
Daniel Drake committed
270
extern struct fp_img_driver aes2501_driver;
271
#endif
272 273 274
#ifdef ENABLE_AES2550
extern struct fp_img_driver aes2550_driver;
#endif
Vasily Khoruzhick's avatar
Vasily Khoruzhick committed
275 276 277
#ifdef ENABLE_AES2660
extern struct fp_img_driver aes2660_driver;
#endif
Juvenn Woo's avatar
Juvenn Woo committed
278 279 280
#ifdef ENABLE_AES3500
extern struct fp_img_driver aes3500_driver;
#endif
281
#ifdef ENABLE_AES4000
Daniel Drake's avatar
Daniel Drake committed
282
extern struct fp_img_driver aes4000_driver;
283 284
#endif
#ifdef ENABLE_FDU2000
Gustavo Chain's avatar
Gustavo Chain committed
285
extern struct fp_img_driver fdu2000_driver;
286 287
#endif
#ifdef ENABLE_VCOM5S
288
extern struct fp_img_driver vcom5s_driver;
289
#endif
290 291 292
#ifdef ENABLE_VFS101
extern struct fp_img_driver vfs101_driver;
#endif
293 294 295
#ifdef ENABLE_VFS301
extern struct fp_img_driver vfs301_driver;
#endif
Arseniy Lartsev's avatar
Arseniy Lartsev committed
296 297 298
#ifdef ENABLE_VFS5011
extern struct fp_img_driver vfs5011_driver;
#endif
Vasily Khoruzhick's avatar
Vasily Khoruzhick committed
299 300 301
#ifdef ENABLE_UPEKTC_IMG
extern struct fp_img_driver upektc_img_driver;
#endif
Patrick Marlier's avatar
Patrick Marlier committed
302 303 304
#ifdef ENABLE_ETES603
extern struct fp_img_driver etes603_driver;
#endif
Konstantin Semenov's avatar
Konstantin Semenov committed
305 306 307
#ifdef ENABLE_VFS0050
extern struct fp_img_driver vfs0050_driver;
#endif
Igor Filatov's avatar
Igor Filatov committed
308 309 310
#ifdef ENABLE_ELAN
extern struct fp_img_driver elan_driver;
#endif
311

Daniel Drake's avatar
Daniel Drake committed
312
extern libusb_context *fpi_usb_ctx;
313 314
extern GSList *opened_devices;

315
void fpi_img_driver_setup(struct fp_img_driver *idriver);
316

317 318 319
#define fpi_driver_to_img_driver(drv) \
	container_of((drv), struct fp_img_driver, driver)

320
struct fp_dscv_dev {
Daniel Drake's avatar
Daniel Drake committed
321
	struct libusb_device *udev;
322
	struct fp_driver *drv;
323
	unsigned long driver_data;
324
	uint32_t devtype;
325 326
};

Daniel Drake's avatar
Daniel Drake committed
327 328 329 330 331 332 333
struct fp_dscv_print {
	uint16_t driver_id;
	uint32_t devtype;
	enum fp_finger finger;
	char *path;
};

334 335 336 337 338
enum fp_print_data_type {
	PRINT_DATA_RAW = 0, /* memset-imposed default */
	PRINT_DATA_NBIS_MINUTIAE,
};

339 340 341 342 343
struct fp_print_data_item {
	size_t length;
	unsigned char data[0];
};

344
struct fp_print_data {
345 346 347
	uint16_t driver_id;
	uint32_t devtype;
	enum fp_print_data_type type;
348
	GSList *prints;
349 350
};

351
struct fpi_print_data_fp2 {
352 353 354 355 356 357 358
	char prefix[3];
	uint16_t driver_id;
	uint32_t devtype;
	unsigned char data_type;
	unsigned char data[0];
} __attribute__((__packed__));

359 360 361 362 363
struct fpi_print_data_item_fp2 {
	uint32_t length;
	unsigned char data[0];
} __attribute__((__packed__));

364
void fpi_data_exit(void);
365 366
struct fp_print_data *fpi_print_data_new(struct fp_dev *dev);
struct fp_print_data_item *fpi_print_data_item_new(size_t length);
Daniel Drake's avatar
Daniel Drake committed
367 368 369
gboolean fpi_print_data_compatible(uint16_t driver_id1, uint32_t devtype1,
	enum fp_print_data_type type1, uint16_t driver_id2, uint32_t devtype2,
	enum fp_print_data_type type2);
370

371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
struct fp_minutia {
	int x;
	int y;
	int ex;
	int ey;
	int direction;
	double reliability;
	int type;
	int appearing;
	int feature_id;
	int *nbrs;
	int *ridge_counts;
	int num_nbrs;
};

386 387 388 389 390 391
struct fp_minutiae {
	int alloc;
	int num;
	struct fp_minutia **list;
};

Daniel Drake's avatar
Daniel Drake committed
392
/* bit values for fp_img.flags */
Bastien Nocera's avatar
Bastien Nocera committed
393 394
#define FP_IMG_V_FLIPPED	(1<<0)
#define FP_IMG_H_FLIPPED	(1<<1)
Daniel Drake's avatar
Daniel Drake committed
395
#define FP_IMG_COLORS_INVERTED	(1<<2)
396
#define FP_IMG_BINARIZED_FORM	(1<<3)
397
#define FP_IMG_PARTIAL		(1<<4)
398 399 400

#define FP_IMG_STANDARDIZATION_FLAGS (FP_IMG_V_FLIPPED | FP_IMG_H_FLIPPED \
	| FP_IMG_COLORS_INVERTED)
Daniel Drake's avatar
Daniel Drake committed
401

402 403 404 405
struct fp_img {
	int width;
	int height;
	size_t length;
Daniel Drake's avatar
Daniel Drake committed
406
	uint16_t flags;
407 408
	struct fp_minutiae *minutiae;
	unsigned char *binarized;
409 410 411 412
	unsigned char data[0];
};

struct fp_img *fpi_img_new(size_t length);
413
struct fp_img *fpi_img_new_for_imgdev(struct fp_img_dev *dev);
414 415
struct fp_img *fpi_img_resize(struct fp_img *img, size_t newsize);
gboolean fpi_img_is_sane(struct fp_img *img);
416
int fpi_img_to_print_data(struct fp_img_dev *imgdev, struct fp_img *img,
417
	struct fp_print_data **ret);
418 419
int fpi_img_compare_print_data(struct fp_print_data *enrolled_print,
	struct fp_print_data *new_print);
420
int fpi_img_compare_print_data_to_gallery(struct fp_print_data *print,
421
	struct fp_print_data **gallery, int match_threshold, size_t *match_offset);
422
struct fp_img *fpi_im_resize(struct fp_img *img, unsigned int w_factor, unsigned int h_factor);
423

424 425
/* polling and timeouts */

Daniel Drake's avatar
Daniel Drake committed
426
void fpi_poll_init(void);
427 428 429 430
void fpi_poll_exit(void);

typedef void (*fpi_timeout_fn)(void *data);

431 432 433 434
struct fpi_timeout;
struct fpi_timeout *fpi_timeout_add(unsigned int msec, fpi_timeout_fn callback,
	void *data);
void fpi_timeout_cancel(struct fpi_timeout *timeout);
435

436 437 438 439 440 441 442 443 444 445 446
/* async drv <--> lib comms */

struct fpi_ssm;
typedef void (*ssm_completed_fn)(struct fpi_ssm *ssm);
typedef void (*ssm_handler_fn)(struct fpi_ssm *ssm);

/* sequential state machine: state machine that iterates sequentially over
 * a predefined series of states. can be aborted by either completion or
 * abortion error conditions. */
struct fpi_ssm {
	struct fp_dev *dev;
Daniel Drake's avatar
Daniel Drake committed
447
	struct fpi_ssm *parentsm;
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
	void *priv;
	int nr_states;
	int cur_state;
	gboolean completed;
	int error;
	ssm_completed_fn callback;
	ssm_handler_fn handler;
};


/* for library and drivers */
struct fpi_ssm *fpi_ssm_new(struct fp_dev *dev, ssm_handler_fn handler,
	int nr_states);
void fpi_ssm_free(struct fpi_ssm *machine);
void fpi_ssm_start(struct fpi_ssm *machine, ssm_completed_fn callback);
Daniel Drake's avatar
Daniel Drake committed
463
void fpi_ssm_start_subsm(struct fpi_ssm *parent, struct fpi_ssm *child);
464 465 466 467
int fpi_ssm_has_completed(struct fpi_ssm *machine);

/* for drivers */
void fpi_ssm_next_state(struct fpi_ssm *machine);
468
void fpi_ssm_jump_to_state(struct fpi_ssm *machine, int state);
469 470 471
void fpi_ssm_mark_completed(struct fpi_ssm *machine);
void fpi_ssm_mark_aborted(struct fpi_ssm *machine, int error);

472 473
void fpi_drvcb_open_complete(struct fp_dev *dev, int status);
void fpi_drvcb_close_complete(struct fp_dev *dev);
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489

void fpi_drvcb_enroll_started(struct fp_dev *dev, int status);
void fpi_drvcb_enroll_stage_completed(struct fp_dev *dev, int result,
	struct fp_print_data *data, struct fp_img *img);
void fpi_drvcb_enroll_stopped(struct fp_dev *dev);

void fpi_drvcb_verify_started(struct fp_dev *dev, int status);
void fpi_drvcb_report_verify_result(struct fp_dev *dev, int result,
	struct fp_img *img);
void fpi_drvcb_verify_stopped(struct fp_dev *dev);

void fpi_drvcb_identify_started(struct fp_dev *dev, int status);
void fpi_drvcb_report_identify_result(struct fp_dev *dev, int result,
	size_t match_offset, struct fp_img *img);
void fpi_drvcb_identify_stopped(struct fp_dev *dev);

490 491 492 493 494
void fpi_drvcb_capture_started(struct fp_dev *dev, int status);
void fpi_drvcb_report_capture_result(struct fp_dev *dev, int result,
	struct fp_img *img);
void fpi_drvcb_capture_stopped(struct fp_dev *dev);

495
/* for image drivers */
496 497
void fpi_imgdev_open_complete(struct fp_img_dev *imgdev, int status);
void fpi_imgdev_close_complete(struct fp_img_dev *imgdev);
498 499 500 501 502
void fpi_imgdev_activate_complete(struct fp_img_dev *imgdev, int status);
void fpi_imgdev_deactivate_complete(struct fp_img_dev *imgdev);
void fpi_imgdev_report_finger_status(struct fp_img_dev *imgdev,
	gboolean present);
void fpi_imgdev_image_captured(struct fp_img_dev *imgdev, struct fp_img *img);
503
void fpi_imgdev_abort_scan(struct fp_img_dev *imgdev, int result);
504 505
void fpi_imgdev_session_error(struct fp_img_dev *imgdev, int error);

506 507 508 509
/* utils */
int fpi_std_sq_dev(const unsigned char *buf, int size);
int fpi_mean_sq_diff_norm(unsigned char *buf1, unsigned char *buf2, int size);

510 511
#endif