Debian Codegraph @

addons/iot_drivers/iot_handlers/interfaces/usb_interface_L.py

raw
1 # Part of Odoo. See LICENSE file for full copyright and licensing details.
2
3 from usb import core
4
5 from odoo.addons.iot_drivers.interface import Interface
6
7
8 class USBInterface(Interface):
9 connection_type = 'usb'
10 allow_unsupported = True
11
12 @staticmethod
13 def usb_matcher(dev):
14 # USB Class codes documentation: https://www.usb.org/defined-class-codes
15 # Ignore USB hubs (9) and printers (7)
16 if dev.bDeviceClass in [7, 9]:
17 return False
18 # If the device has generic base class (0) check its interface descriptor
19 elif dev.bDeviceClass == 0:
20 for conf in dev:
21 for interface in conf:
22 if interface.bInterfaceClass == 7: # 7 = printer
23 return False
24
25 # Ignore serial adapters
26 try:
27 return dev.product != "USB2.0-Ser!"
28 except ValueError:
29 return True
30
31 def get_devices(self):
32 """
33 USB devices are identified by a combination of their `idVendor` and
34 `idProduct`. We can't be sure this combination in unique per equipment.
35 To still allow connecting multiple similar equipments, we complete the
36 identifier by a counter. The drawbacks are we can't be sure the equipments
37 will get the same identifiers after a reboot or a disconnect/reconnect.
38 """
39 usb_devices = {}
40 devs = core.find(find_all=True, custom_match=self.usb_matcher)
41 cpt = 2
42 for dev in devs:
43 identifier = "usb_%04x:%04x" % (dev.idVendor, dev.idProduct)
44 if identifier in usb_devices:
45 identifier += '_%s' % cpt
46 cpt += 1
47 usb_devices[identifier] = dev
48 return usb_devices