-
Notifications
You must be signed in to change notification settings - Fork 0
/
usb_test2.py
134 lines (115 loc) · 4.78 KB
/
usb_test2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/python
"""
You must be using the new PyUSB 1.0 branch and not the 0.x branch.
Copyright (c) 2011 - Bernard `Guyzmo` Pratz - guyzmo{at}hackable-devices{dot}org
Forked from: Copyright (c) 2010 - Micah Carrick - http://www.micahcarrick.com/credit-card-reader-pyusb.html
Modified by : meotimdihia
"""
import sys
import usb.core
import usb.util
import time
# VENDOR_ID = 0x0458 #mouse
# PRODUCT_ID = 0x003a #mouse
# VENDOR_ID = 0x10c4 # nhiet do
# PRODUCT_ID = 0xea60
VENDOR_ID = 0x0B38 # kb
PRODUCT_ID = 0x0010
# VENDOR_ID = 0x08ff # id
# PRODUCT_ID = 0x0009
DATA_SIZE = 167
# keycode mapping
key_pages = [
'', '', '', '',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '\n', '^]', '^H',
'^I', ' ', '-', '=', '[', ']', '\\', '>', ';', "'", '`', ',', '.',
'/', 'CapsLock', 'F1', 'F2', 'F3', 'F4', 'F5', 'F6', 'F7', 'F8', 'F9', 'F10', 'F11', 'F12',
'PS', 'SL', 'Pause', 'Ins', 'Home', 'PU', '^D', 'End', 'PD', '->', '<-', '-v', '-^', 'NL',
'KP/', 'KP*', 'KP-', 'KP+', 'KPE', 'KP1', 'KP2', 'KP3', 'KP4', 'KP5', 'KP6', 'KP7', 'KP8',
'KP9', 'KP0', '\\', 'App', 'Pow', 'KP=', 'F13', 'F14' ]
key_pages_shift = [
'', '', '', '',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '\n', '^]', '^H',
'^I', ' ', '_', '+', '{', '}', '|', '<', ':', '"', '~', '<', '>',
'?', 'CapsLock', 'F1', 'F2', 'F3', 'F4', 'F5', 'F6', 'F7', 'F8', 'F9', 'F10', 'F11', 'F12',
'PS', 'SL', 'Pause', 'Ins', 'Home', 'PU', '^D', 'End', 'PD', '->', '<-', '-v', '-^', 'NL',
'KP/', 'KP*', 'KP-', 'KP+', 'KPE', 'KP1', 'KP2', 'KP3', 'KP4', 'KP5', 'KP6', 'KP7', 'KP8',
'KP9', 'KP0', '|', 'App', 'Pow', 'KP=', 'F13', 'F14' ]
def map_character(c):
#return keymap[keycode[c]]
return key_pages[c]
def chunks(l, n):
""" Yield successive n-sized chunks from l.
"""
for i in xrange(0, len(l), n):
print n
yield l[i:i+n]
a = ['B', [1, 0, 6, 0, 0, 0, 0, 0]]
map_keys = lambda c: key_pages_shift[c[1]] if c[0] is 2 else key_pages[c[1]]
# print "".join(map(map_keys, [(d[0], d[2]) for d in chunks(a, 8)]))
class MagSwipe:
def __init__(self):
# find the MagSwipe reader
device = usb.core.find(idVendor=VENDOR_ID, idProduct=PRODUCT_ID)
self._device = device
if device is None:
sys.exit("Could not find MagTek USB HID Swipe Reader.")
#make sure the hiddev kernel driver is not active
# if device.is_kernel_driver_active(0):
# try:
# print "detaching device..."
# device.detach_kernel_driver(0)
# except usb.core.USBError as e:
# sys.exit("Could not detatch kernel driver: %s" % str(e))
#set configuration
try:
print 'setting configuration...'
device.set_configuration()
# device.reset()
except usb.core.USBError as e:
sys.exit("Could not set configuration: %s" % str(e))
self._endpoint = device[0][(0,0)][0]
def wait_for_swipe(self):
# wait for swipe
data = []
swiped = False
print self._device
# print self._device.deviceClass
# print "device name=",_name
print 'endpoint %s' % self._endpoint
print "Please type a key on keyboard..."
while 1:
try:
data = self._device.read(self._endpoint.bEndpointAddress, self._endpoint.wMaxPacketSize, 0, 1024)
print data
if not swiped:
print "Reading..."
swiped = True
print data
print "Data: %s" % ''.join(map(chr, data))
sret = ''.join([chr(x) for x in data])
print sret
map_keys = lambda c: key_pages_shift[c[1]] if c[0] is 2 else key_pages[c[1]]
data = "".join(map(map_keys, [(d[0], d[2]) for d in chunks(data, 8)]))
print "key: " + data
except usb.core.USBError as e:
if e.args[0] == 110 and swiped:
if len(data) < DATA_SIZE:
print "Bad swipe, try again. (%d bytes)" % len(data)
print "Data: %s" % ''.join(map(chr, data))
data = []
swiped = False
continue
else:
break # we got it!
# convert text
map_keys = lambda c: key_pages_shift[c[1]] if c[0] is 2 else key_pages[c[1]]
data = "".join(map(map_keys, [(d[0], d[2]) for d in chunks(data, 8)]))
time.sleep(.01)
return data
if __name__ == "__main__":
MagSwipe().wait_for_swipe()