source: main/pyfprint/trunk/src/fprint.py @ 16536

Last change on this file since 16536 was 11534, checked in by uli, 11 years ago

Add wrapper for detection function.

File size: 5.3 KB
RevLine 
[11526]1# A ctypes wrapper around libfprint, a C lib for fingerprint scanners.
2import ctypes
3
[11529]4lib = ctypes.cdll.LoadLibrary("libfprint.so.0")
[11526]5
6#
7# Enums...
8#
9
10# enum fp_driver_type
11(
12    DRIVER_PRIMITIVE,
13    DRIVER_IMAGING,
14) = map(ctypes.c_int, xrange(0, 2))
15
16# enum fp_scan_type
17(
18    FP_SCAN_TYPE_PRESS,  # press
19    FP_SCAN_TYPE_SWIPE,  # swipe
20) = map(ctypes.c_int, xrange(0, 2))
21
22
23class usb_id(ctypes.Structure):
24    _fields_ = [
25        ('vendor', ctypes.c_uint16),
26        ('product', ctypes.c_uint16),
27        ('driver_data', ctypes.c_ulong),
28        ]
29
[11529]30class libusb_device_descriptor(ctypes.Structure):
31    # from libusb.h
32    _fields_ = [
33        ('bLength', ctypes.c_uint8),
34        ('bDescriptorType', ctypes.c_uint8),
35        ('bcdUSB', ctypes.c_uint16),
36        ('bDeviceClass', ctypes.c_uint8),
37        ('bDeviceSubClass', ctypes.c_uint8),
38        ('bDeviceProtocol', ctypes.c_uint8),
39        ('bMaxPacketSize0', ctypes.c_uint8),
40        ('idVendor', ctypes.c_uint16),
41        ('idProduct', ctypes.c_uint16),
42        ('bcdDevice', ctypes.c_uint16),
43        ('iManufacturer', ctypes.c_uint8),
44        ('iProduct', ctypes.c_uint8),
45        ('iSerialNumber', ctypes.c_uint8),
46        ('bNumConfigurations', ctypes.c_uint8),
47        ]
[11526]48
49
50#
51# 'Hidden' types, i.e. types that should not be accessed internally by apps
52# (defined in fprint_internal.h)
53#
54fp_dscv_dev = None
55fp_dscv_print = None
56fp_dev = None
57class fp_driver(ctypes.Structure):
58    _fields_ = [
59        ('id', ctypes.c_uint16),
60        ('name', ctypes.c_char_p),
61        ('full_name', ctypes.c_char_p),
62        ('id_table', usb_id),
63        ('type', ctypes.c_int),       # fp_driver_type
64        ('scan_type', ctypes.c_int),  # fp_scan_type
65        ('priv', ctypes.c_void_p),
66        #  Device operations
[11530]67        # int (*discover)(struct libusb_device_descriptor *dsc, uint32_t *devtype);
68        ('discover',
69         ctypes.CFUNCTYPE(
70                ctypes.c_int,  # Returntype?
71                ctypes.POINTER(libusb_device_descriptor),
72                ctypes.POINTER(ctypes.c_uint32)),
[11529]73         ),
[11526]74        # int (*open)(struct fp_dev *dev, unsigned long driver_data);
[11530]75        ('open',
76         ctypes.CFUNCTYPE(
77                ctypes.c_int,  # Returntype?
78                ctypes.POINTER(fp_dev),       # dev
79                ctypes.c_ulong),              # driver_data
80         ),
[11526]81        # void (*close)(struct fp_dev *dev);
[11530]82        ('close',
83         ctypes.CFUNCTYPE(
84                ctypes.c_void_p,  # Returntype?
85                ctypes.POINTER(fp_dev),       # dev
86                ),
87         ),
[11526]88        # int (*enroll_start)(struct fp_dev *dev);
[11530]89        ('enroll_start',
90         ctypes.CFUNCTYPE(
91                ctypes.c_int,  # Returntype?
92                ctypes.POINTER(fp_dev)        # dev
93                ),
94         ),
[11526]95        # int (*enroll_stop)(struct fp_dev *dev);
[11530]96        ('enroll_stop',
97         ctypes.CFUNCTYPE(
98                ctypes.c_int,  # Returntype?
99                ctypes.POINTER(fp_dev),       # dev
100                ),
101         ),
[11526]102        # int (*verify_start)(struct fp_dev *dev);
[11530]103        ('verify_start',
104         ctypes.CFUNCTYPE(
105                ctypes.c_int,  # Returntype?
106                ctypes.POINTER(fp_dev),       # dev
107                ),
108         ),
[11526]109        # int (*verify_stop)(struct fp_dev *dev, gboolean iterating);
[11530]110        ('verify_stop',
111         ctypes.CFUNCTYPE(
112                ctypes.c_int,  # Returntype?
113                ctypes.POINTER(fp_dev),  # dev
114                ctypes.c_int,            # iterating (glib.h gboolean=gint=int)
115                ),
116         ),
[11526]117        # int (*identify_start)(struct fp_dev *dev);
[11530]118        ('identify_start',
119         ctypes.CFUNCTYPE(
120                ctypes.c_int,  # Returntype?
121                ctypes.POINTER(fp_dev),  # dev
122                ),
123         ),
[11526]124        # int (*identify_stop)(struct fp_dev *dev, gboolean iterating);
[11530]125        ('identify_stop',
126         ctypes.CFUNCTYPE(
127                ctypes.c_int,  # Returntype?
128                ctypes.POINTER(fp_dev),  # dev
129                ctypes.c_int,            # iterating (glib.h gboolean=gint=int)
130                ),
131         ),
[11526]132        ]
[11530]133
[11526]134fp_print_data = None
135fp_img = None
136
137# enum fp_finger   Some enum...
138(
139    LEFT_THUMB, # thumb (left hand)
140    LEFT_INDEX,  # index finger (left hand)
141    LEFT_MIDDLE,  # middle finger (left hand)
142    LEFT_RING,  # ring finger (left hand)
143    LEFT_LITTLE,  # little finger (left hand)
144    RIGHT_THUMB,  # thumb (right hand)
145    RIGHT_INDEX,  # index finger (right hand)
146    RIGHT_MIDDLE,  # middle finger (right hand)
147    RIGHT_RING,  # ring finger (right hand)
148    RIGHT_LITTLE,  # little finger (right hand)
149) = map(ctypes.c_int, xrange(1, 11))
150
151
152
153# Drivers
154
155lib.fp_driver_get_name.restype = ctypes.POINTER(ctypes.c_char)
156lib.fp_driver_get_name.argtypes = [ctypes.POINTER(fp_driver)]
157driver_get_name = lib.fp_driver_get_name
158
159
160discover_devs = lib.fp_discover_devs
[11534]161discover_devs.restype = ctypes.POINTER(
162    ctypes.POINTER(fp_dscv_dev))
[11526]163
[11534]164# Library
165fp_init = lib.fp_init
166fp_exit = lib.fp_exit
[11526]167
[11534]168lib.fp_set_debug.argtypes = [ctypes.c_int,]
169fp_set_debug = lib.fp_set_debug
170
171
[11526]172if __name__ == '__main__':
[11534]173    fp_init()
174    devs = discover_devs()
175
176    if isinstance(devs, ctypes.POINTER(ctypes.c_void_p)):
177        print "No device detected!"
178    else:
179        print "Something found: "
180        print devs
[11526]181    import pdb; pdb.set_trace()
[11534]182    fp_exit()
Note: See TracBrowser for help on using the repository browser.