1
2
3
4
5
6
7
8
9
10
11 """Parse the usb.ids file for quick access to the information contained therein"""
12
13 import re
14
15 vendor_re = re.compile(r"^([0-9A-Fa-f]{4,4})\s+(.*)$")
16 device_re = re.compile(r"^\t([0-9A-Fa-f]{4,4})\s+(.*)$")
17 iface_re = re.compile(r"^\t\t([0-9A-Fa-f]{2,2})\s+(.*)$")
18 usbclass_re = re.compile(r"^C\s([0-9A-Fa-f]{2,2})\s+(.*)$")
19 usbsubclass_re = re.compile(r"^\t([0-9A-Fa-f]{2,2})\s+(.*)$")
20 usbprotocol_re = re.compile(r"^\t\t([0-9A-Fa-f]{2,2})\s+(.*)$")
21
22
23
24
26 """ Super class for all types of USB vendor/device/interface/class/sublcass/protocol
27 classes which will be descendants of this. I chose to make and use various children
28 of this class, as it's possible they may have unique information of their own some
29 time later, as well as allowing a more natural name/syntax based on object use.
30 """
32 """ Set our ID code, description, and prepare to accept children """
33 self.id = id
34 self.description = description
35 self.children = {};
36
38 """ Return the description for this object """
39 return self.description
40
42 """ Return our ID code """
43 return self.id
44
46 """ Add a child to our list """
47 self.children[int(child.id, 16)] = child
48
50 """ If we have a child matching the request, return it """
51 if child in self.children:
52 return self.children[child]
53 else:
54 return None
55
57 """ Return a list of all our children """
58 return self.children.values()
59
60
61
62
64 """ Class that represents the data in the usb.ids file
65 It reads/parses the file and creates the objects to match
66 """
67
69 """ Initialize the class. This includes reading the supplied file
70 and creating/populating as many related objects as needed.
71 """
72 self.vendorlist = VendorList()
73 self.usbclasslist = USBClassList()
74 self.inVendor = 0
75 self.inClass = 0
76 if fname is not None:
77 self.add_data(fname)
78
80 try:
81 ufile = open(fname, "rt")
82 try:
83 aline = ufile.readline()
84 while aline != "":
85
86 while (aline[:-1] in ["\r", "\n"]):
87 aline = aline[:-1]
88
89
90 m = vendor_re.match(aline)
91 if (m):
92 self.inVendor = 1
93 self.inClass = 0
94 self.curr_vendor = VendorID(m.group(1), m.group(2))
95 self.vendorlist.addVendor(m.group(1), self.curr_vendor)
96
97 if (self.inVendor):
98
99 m = device_re.match(aline)
100 if (m):
101 self.curr_device = DeviceID(m.group(1), m.group(2))
102 self.curr_vendor.addDevice(self.curr_device)
103
104
105 m = iface_re.match(aline)
106 if (m):
107 self.curr_device.addInterface(InterfaceID(m.group(1), m.group(2)))
108
109
110 m = usbclass_re.match(aline)
111 if (m):
112 self.inClass = 1
113 self.inVendor = 0
114 self.curr_usbclass = USBClass(m.group(1), m.group(2))
115 self.usbclasslist.addClass(m.group(1), self.curr_usbclass)
116
117 if (self.inClass):
118
119 m = usbsubclass_re.match(aline)
120 if (m):
121 self.curr_usbsubclass = USBClassSubclass(m.group(1), m.group(2))
122 self.curr_usbclass.addSubclass(self.curr_usbsubclass)
123
124
125 m = usbprotocol_re.match(aline)
126 if (m):
127 self.curr_usbsubclass.addProtocol(USBClassProtocol(m.group(1), m.group(2)))
128
129
130 aline = ufile.readline()
131 except IOError:
132
133 pass
134
135 except IOError:
136 print ("Cannot open the USB ID file: %s" % fname)
137 raise
138
139 if (ufile):
140 ufile.close()
141
142 - def lookupdevice(self, vendor, product=None, interface=None):
144
145 - def lookupclass(self, klass, subclass=None, protocol=None):
146 return self.usbclasslist.getClassInfo(klass, subclass, protocol)
147
149 """ Return the object representing the list of vendors """
150 return self.vendorlist
151
153 """ Return the object representing the list of USB Classes """
154 return self.usbclasslist
155
156
157
158
160 """ This class abstracts USB Vendor ID information
161 It holds the description, and a list of Device ID's
162 """
164 """ Put this device on our list """
165 self.addChild(device)
166
168 """ Return the requested device by ID, if we have it """
169 return self.getChild(device)
170
172 """ Return a list of our devices """
173 return self.getChildren()
174
176 """ This class abstracts USB Device ID information
177 It holds the description and a list of the Interface ID's
178 """
180 """ Put this interface on our list """
181 self.addChild(interface)
182
184 """ Return the requested interface by ID, if we have it """
185 return self.getChild(interface)
186
188 """ Return a list of our interfaces """
189 return self.getChildren()
190
192 """ This class abstracts USB Interface information
193 It holds the description
194 """
195 pass
196
198 """ This class is responsible for the collection of vendor data
199 It allows you to ask for:
200 - vendor info by VendorID
201 - device info by VendorID/DeviceID
202 - interface """
204 """ Prepare a dict to handle all of our children vendor objects """
205 self.vendorlist = {}
206
208 """ Put this vendor into our dictionary """
209 self.vendorlist[int(vID,16)] = vDesc
210
212 """ Lookup info for vendor, device, interface - last two are optional """
213
214 self.vendor = self.device = self.iface = None
215 self.vDesc = self.dDesc = self.iDesc = None
216
217
218 if vID in self.vendorlist:
219 self.vendor = self.vendorlist[vID]
220 self.vDesc = self.vendor.description
221
222
223 if self.vendor:
224 self.device = self.vendor.getDevice(dID)
225 if self.device:
226 self.dDesc = self.device.description
227
228
229 if self.device:
230 self.iface = self.device.getInterface(iID)
231 if self.iface:
232 self.iDesc = self.iface.description
233
234
235 if ((dID is None) and (iID is None)):
236 return (self.vDesc,)
237 elif (iID is None):
238 return (self.vDesc, self.dDesc)
239 else:
240 return (self.vDesc, self.dDesc, self.iDesc)
241
243 return self.vendorlist.values()
244
245
246
247
248
250 """ This class abstracts USB Class information
251 It holds the description, and a list of Subclasses
252 """
254 """ Put this subclass on our list """
255 self.addChild(subclass)
256
258 """ Return subclass by ID, if we have it """
259 return self.getChild(subclass)
260
262 """ Return a list of our subclasses """
263 return self.getChildren()
264
265
267 """ This class abstracts USB Device SubClass information
268 It holds the description and a list of the protocols
269 """
271 """ Put this protocol on our list """
272 self.addChild(protocol)
273
275 """ Return protocol as ID, if we have it """
276 return self.getChild(protocol)
277
279 """ Return a list of our protocols """
280 return self.getChildren()
281
283 """ This class abstracts USB Interface information
284 It holds the description
285 """
286 pass
287
288
290 """ This class is responsible for the collection of USB Class data
291 It allows you to ask for:
292 - USB Class info by Class ID
293 - USB SubClass info by ClassID/SubclassID
294 - USB Protocol info by ClassID/SubclassID/ProtocolID
295 """
298
300 self.classlist[int(cID, 16)] = cDesc
301
303 """ Lookup info for class, subclass, protocol - last two are optional """
304
305 self.usbclass = self.subclass = self.protocol = None
306 self.cDesc = self.sDesc = self.pDesc = None
307
308
309 if cID in self.classlist:
310 self.usbclass = self.classlist[cID]
311 self.cDesc = self.usbclass.description
312
313
314 if self.usbclass:
315 self.subclass = self.usbclass.getSubclass(sID)
316 if self.subclass:
317 self.sDesc = self.subclass.description
318
319
320 if self.subclass:
321 self.protocol = self.subclass.getProtocol(pID)
322 if self.protocol:
323 self.pDesc = self.protocol.description
324
325
326 if ((sID is None) and (pID is None)):
327 return (self.cDesc,)
328 elif (pID is None):
329 return (self.cDesc, self.sDesc)
330 else:
331 return (self.cDesc, self.sDesc, self.pDesc)
332
334 return self.classlist.values()
335
336
337
338
339
340
341 if (__name__ == "__main__"):
342 import os, guihelper
352
362
363 myUSBids = usb_ids(os.path.join(guihelper.resourcedirectory,
364 "usb.ids"))
365
366
367
368
369
370
371
372
373
374 print "Inquire on Vendor 1452, Device 518, Iface 1 using various levels of detail:"
375 vlist = myUSBids.getVendorList()
376 print vlist.getVendorInfo(1452)
377 print vlist.getVendorInfo(1452, 518)
378 print vlist.getVendorInfo(1452, 518, 1)
379 print
380
381
382 print "Inquire on Class 8, Subclass 4, Protocol 0 using various levels of detail:"
383 clist = myUSBids.getUSBClassList()
384 print clist.getClassInfo(8)
385 print clist.getClassInfo(8, 4)
386 print clist.getClassInfo(8, 4, 0)
387 print
388
389
390
391
392
393
394
395
396
397
398
399
400
401 print "Look up something with little detail in Internet file... 1004/6000/(00..02)"
402 print vlist.getVendorInfo(int("1004", 16), int("6000", 16), int("00", 16))
403 print vlist.getVendorInfo(int("1004", 16), int("6000", 16), int("01", 16))
404 print vlist.getVendorInfo(int("1004", 16), int("6000", 16), int("02", 16))
405 print
406
407
408 print "Add in our test data to override internet data"
409 myUSBids.add_data(os.path.join(guihelper.resourcedirectory,
410 "test.ids"))
411 print
412
413
414 print "Check it again..."
415 print vlist.getVendorInfo(int("1004", 16), int("6000", 16), int("00", 16))
416 print vlist.getVendorInfo(int("1004", 16), int("6000", 16), int("01", 16))
417 print vlist.getVendorInfo(int("1004", 16), int("6000", 16), int("02", 16))
418