-
Notifications
You must be signed in to change notification settings - Fork 3
/
rd_viewer.py
139 lines (116 loc) · 4.18 KB
/
rd_viewer.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
135
136
137
138
139
# USB HID Report Descriptor Viewer
#
# Copyright (C) 2019 Ravikiran Bukkasagara <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# TAB = 4 spaces
# Entry point module for the program
import textwrap
import threading
import argparse
import sys
import usb.core
import usb.util
version = "1.0"
program_desc = "USB HID Report Descriptor Viewer {0}".format(version)
from rd_reader import HIDReportDescriptorReader
HID_CLASS_bInterfaceClass = 0x3
def is_hid(dev):
if dev.bDeviceClass == HID_CLASS_bInterfaceClass:
return True
for cfg in dev:
if usb.util.find_descriptor(cfg, bInterfaceClass=HID_CLASS_bInterfaceClass) is not None:
return True
return False
def parse_user_device_id(dev_id_str):
vid_pid = dev_id_str.strip()
ids = vid_pid.split(':', maxsplit=2)
if len(ids) != 2:
raise Exception("Invalid device {0}".format(dev_id_str))
# raises exception if not valid hex
vid = int(ids[0], base=16)
pid = int(ids[1], base=16)
device = usb.core.find(idVendor=vid, idProduct=pid)
if device is None:
raise Exception("Device {0} not found".format(dev_id_str))
if is_hid(device) == False:
raise Exception("Device {0} has no HID class interface".format(dev_id_str))
return device
def read_rd(hid, ofile_name):
ofile = sys.stdout
if ofile_name != '':
ofile = open(ofile_name, 'w')
try:
cfg = hid.get_active_configuration()
for intf in cfg:
if intf.bInterfaceClass == HID_CLASS_bInterfaceClass:
print("Interface Number: {0}".format(intf.bInterfaceNumber), file=ofile)
print("Report Descriptor:", file=ofile)
print(HIDReportDescriptorReader(hid, intf), file=ofile)
print("", file=ofile)
# exceptions on caller block
finally:
if ofile_name != '':
ofile.close()
try:
cmd_parser = argparse.ArgumentParser(description=program_desc)
cmd_parser.add_argument("-v", "--version", action="store_true",
help="show program version information and exit")
cmd_parser.add_argument("-d", "--device",
help="specific device to read",
metavar="pid:vid",
default='')
cmd_parser.add_argument("-o", "--output",
help="write Report descriptor to given file instead of stdout",
metavar="output_file",
default='')
cmd_args = cmd_parser.parse_args()
if cmd_args.version:
print(program_desc)
exit(0)
if cmd_args.device != '':
hid = parse_user_device_id(cmd_args.device)
read_rd(hid, cmd_args.output)
exit(0)
i = 1
hid_devices = []
for hid in usb.core.find(find_all=True, custom_match = is_hid):
print("{0}) {2} - {1} - ID {3:04x}:{4:04x}".format(i,
hid.product,
hid.manufacturer,
hid.idVendor,
hid.idProduct))
i += 1
hid_devices.append(hid)
if i == 1:
print("No USB HID Class devices found.")
exit(0)
print("q) Quit program")
choice = input("Enter choice: ".format(i-1)).strip().lower()
ret = 0
if choice.isdigit():
index = int(choice)
if index >= 1 and index < i:
hid = hid_devices[index-1]
read_rd(hid, cmd_args.output)
else:
print("Invalid selection")
ret = -1
elif choice != 'q':
print("Invalid selection")
ret = -1
exit(ret)
except Exception as e:
print(e)
exit(-1)