Enable NFC for Linux and pcscd on Dell E7470 (and others) with ControlVault2
Jacek Kowalski
2020-07-24 f834210391b0549770947019c8bf6b5c5034d04c
commit | author | age
f83421 1 #!/usr/bin/env python3
JK 2
3 import logging
4 import sys
5 import usb.core
6
7 class UsbDeviceMatcher:
8     def __init__(self, properties, handler):
9         self.properties = properties
10         self.handler = handler
11
12     def matches(self, candidate):
13         for prop, value in self.properties.items():
14             if prop not in candidate.__dict__ or candidate.__dict__[prop] != value:
15                 return False
16         return True
17
18 class UsbDeviceFinder:
19     SUPPORTED_DEVICES = [
20         UsbDeviceMatcher({'idVendor': 0x0A5C, 'idProduct': 0x5832}, lambda device: __import__('cv2').ControlVault2(device)),
21         UsbDeviceMatcher({'idVendor': 0x0A5C, 'idProduct': 0x5834}, lambda device: __import__('cv2').ControlVault2(device)),
22         UsbDeviceMatcher({'idVendor': 0x0A5C, 'idProduct': 0x5843}, lambda device: __import__('cv3').ControlVault3(device)),
23     ]
24
25     @classmethod
26     def _dev_matcher(cls, device):
27         for matcher in cls.SUPPORTED_DEVICES:
28             if matcher.matches(device):
29                 return True
30         return False
31
32     @classmethod
33     def _cls_matcher(cls, device):
34         for matcher in cls.SUPPORTED_DEVICES:
35             if matcher.matches(device):
36                 return matcher.handler(device)
37         raise Exception('Cannot find handler for device {:04X}:{:04X}'.format(dev.idVendor, dev.idProduct))
38
39     @classmethod
40     def find(cls):
41         logger = logging.getLogger(__name__)
42         logger.info('Looking for supported device...')
43
44         device = usb.core.find(custom_match=cls._dev_matcher)
45         if device is None:
46             raise Exception('Cannot find BCM device - check list of supported devices')
47         logger.info('Found {:04X}:{:04X}'.format(device.idVendor, device.idProduct))
48
49         handler = cls._cls_matcher(device)
50         logger.info('Handler {} ({})'.format(handler.__class__.__name__, handler.NAME))
51         return handler
52
53
54 if __name__ == "__main__":
55     if len(sys.argv) < 2:
56         print('Usage: {} [on|off|reset]'.format(sys.argv[0]))
57         sys.exit(2)
58
59     logging.basicConfig(level=logging.DEBUG)
60     logger = logging.getLogger(__name__)
61
62     handler = UsbDeviceFinder.find()
63     if sys.argv[1] == 'on':
64         logger.info('Turning NFC on...')
65         handler.turn_on()
66         logger.info('NFC should be turned on now!')
67     elif sys.argv[1] == 'off':
68         logger.info('Turning NFC off...')
69         handler.turn_off()
70         logger.info('NFC should be turned off now!')
71     elif sys.argv[1] == 'reset':
72         logger.info('Resetting device...')
73         handler.reset()
74         logger.info('NFC device has been reset!')
75     else:
76         raise Exception('Unknown option: {}'.format(sys.argv[1]))