From a4bc11970f4538ee6e2a8f31ef930edcdf6cb32a Mon Sep 17 00:00:00 2001 From: Jacek Kowalski <Jacek@jacekk.info> Date: Sat, 03 Aug 2019 15:52:00 +0000 Subject: [PATCH] Merge pull request #2 from xabolcs/branch-dell-5491-dell-5480 --- bcm20795.py | 34 ++++++++++++++++++++++++++-------- 1 files changed, 26 insertions(+), 8 deletions(-) diff --git a/bcm20795.py b/bcm20795.py index c689d18..c683016 100755 --- a/bcm20795.py +++ b/bcm20795.py @@ -14,8 +14,10 @@ import usb.core import usb.util -VENDOR_ID = 0x0A5C -DEVICE_ID = 0x5834 +SUPPORTED_DEVICES = [ + {'idVendor': 0x0A5C, 'idProduct': 0x5834}, + {'idVendor': 0x0A5C, 'idProduct': 0x5832}, +] logging.basicConfig(level=logging.DEBUG) logger = logging.getLogger(__name__) @@ -72,22 +74,38 @@ if data[1] == 0x61: packet = self.recv_packet() + @staticmethod + def _dev_match(template, candidate): + for prop, value in template.items(): + if prop not in candidate.__dict__ or candidate.__dict__[prop] != value: + return False + return True + @classmethod - def find(cls, vendor_id, product_id): + def _dev_matcher(cls, dev): + for device in SUPPORTED_DEVICES: + if cls._dev_match(device, dev): + return True + return False + + @classmethod + def find(cls): logger = logging.getLogger(__name__) - logger.info('Looking for device {:04X}:{:04X}...'.format(vendor_id, product_id)) + logger.info('Looking for BCM device...') - device = usb.core.find(idVendor=vendor_id, idProduct=product_id) + device = usb.core.find(custom_match=cls._dev_matcher) if device is None: - raise Exception('Cannot find device {:04X}:{:04X}'.format(vendor_id, product_id)) + raise Exception('Cannot find BCM device - check list of supported devices') + logger.info('Found {:04X}:{:04X}'.format(device.idVendor, device.idProduct)) logger.debug('Enumerating interfaces...') configuration = device.get_active_configuration() bcm_interface = None for interface in configuration: if interface.bInterfaceClass == 0xff and interface.iInterface == 0x08: + if bcm_interface is not None: + raise Exception('More than one vendor-specific interface found!') bcm_interface = interface - break if bcm_interface is None: raise Exception('Cannot find vendor-specific interface') logger.debug('Interface found: {}'.format(bcm_interface._str())) @@ -158,7 +176,7 @@ print('Usage: {} [on|off]'.format(sys.argv[0])) sys.exit(2) - communicator = BcmCommunicator.find(VENDOR_ID, DEVICE_ID) + communicator = BcmCommunicator.find() if sys.argv[1] == 'on': logger.info('Turning NFC on...') turn_on(communicator) -- Gitblit v1.9.1