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

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

Add wrapper for detection function.

File size: 5.3 KB
Line 
1# A ctypes wrapper around libfprint, a C lib for fingerprint scanners.
2import ctypes
3
4lib = ctypes.cdll.LoadLibrary("libfprint.so.0")
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
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        ]
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
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)),
73         ),
74        # int (*open)(struct fp_dev *dev, unsigned long driver_data);
75        ('open',
76         ctypes.CFUNCTYPE(
77                ctypes.c_int,  # Returntype?
78                ctypes.POINTER(fp_dev),       # dev
79                ctypes.c_ulong),              # driver_data
80         ),
81        # void (*close)(struct fp_dev *dev);
82        ('close',
83         ctypes.CFUNCTYPE(
84                ctypes.c_void_p,  # Returntype?
85                ctypes.POINTER(fp_dev),       # dev
86                ),
87         ),
88        # int (*enroll_start)(struct fp_dev *dev);
89        ('enroll_start',
90         ctypes.CFUNCTYPE(
91                ctypes.c_int,  # Returntype?
92                ctypes.POINTER(fp_dev)        # dev
93                ),
94         ),
95        # int (*enroll_stop)(struct fp_dev *dev);
96        ('enroll_stop',
97         ctypes.CFUNCTYPE(
98                ctypes.c_int,  # Returntype?
99                ctypes.POINTER(fp_dev),       # dev
100                ),
101         ),
102        # int (*verify_start)(struct fp_dev *dev);
103        ('verify_start',
104         ctypes.CFUNCTYPE(
105                ctypes.c_int,  # Returntype?
106                ctypes.POINTER(fp_dev),       # dev
107                ),
108         ),
109        # int (*verify_stop)(struct fp_dev *dev, gboolean iterating);
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         ),
117        # int (*identify_start)(struct fp_dev *dev);
118        ('identify_start',
119         ctypes.CFUNCTYPE(
120                ctypes.c_int,  # Returntype?
121                ctypes.POINTER(fp_dev),  # dev
122                ),
123         ),
124        # int (*identify_stop)(struct fp_dev *dev, gboolean iterating);
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         ),
132        ]
133
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
161discover_devs.restype = ctypes.POINTER(
162    ctypes.POINTER(fp_dscv_dev))
163
164# Library
165fp_init = lib.fp_init
166fp_exit = lib.fp_exit
167
168lib.fp_set_debug.argtypes = [ctypes.c_int,]
169fp_set_debug = lib.fp_set_debug
170
171
172if __name__ == '__main__':
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
181    import pdb; pdb.set_trace()
182    fp_exit()
Note: See TracBrowser for help on using the repository browser.