Enable NFC for Linux and pcscd on Dell E7470 (and others) with ControlVault2
Ben Harris
2021-02-03 f3b864392366cbef862f8cca493c2ff0e72c94db
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)),
f3b864 22         UsbDeviceMatcher({'idVendor': 0x0A5C, 'idProduct': 0x5842}, lambda device: __import__('cv3').ControlVault3(device)),
f83421 23         UsbDeviceMatcher({'idVendor': 0x0A5C, 'idProduct': 0x5843}, lambda device: __import__('cv3').ControlVault3(device)),
JK 24     ]
25
26     @classmethod
27     def _dev_matcher(cls, device):
28         for matcher in cls.SUPPORTED_DEVICES:
29             if matcher.matches(device):
30                 return True
31         return False
32
33     @classmethod
34     def _cls_matcher(cls, device):
35         for matcher in cls.SUPPORTED_DEVICES:
36             if matcher.matches(device):
37                 return matcher.handler(device)
38         raise Exception('Cannot find handler for device {:04X}:{:04X}'.format(dev.idVendor, dev.idProduct))
39
40     @classmethod
41     def find(cls):
42         logger = logging.getLogger(__name__)
43         logger.info('Looking for supported device...')
44
45         device = usb.core.find(custom_match=cls._dev_matcher)
46         if device is None:
47             raise Exception('Cannot find BCM device - check list of supported devices')
48         logger.info('Found {:04X}:{:04X}'.format(device.idVendor, device.idProduct))
49
50         handler = cls._cls_matcher(device)
51         logger.info('Handler {} ({})'.format(handler.__class__.__name__, handler.NAME))
52         return handler
53
54
55 if __name__ == "__main__":
56     if len(sys.argv) < 2:
57         print('Usage: {} [on|off|reset]'.format(sys.argv[0]))
58         sys.exit(2)
59
60     logging.basicConfig(level=logging.DEBUG)
61     logger = logging.getLogger(__name__)
62
63     handler = UsbDeviceFinder.find()
64     if sys.argv[1] == 'on':
65         logger.info('Turning NFC on...')
66         handler.turn_on()
67         logger.info('NFC should be turned on now!')
68     elif sys.argv[1] == 'off':
69         logger.info('Turning NFC off...')
70         handler.turn_off()
71         logger.info('NFC should be turned off now!')
72     elif sys.argv[1] == 'reset':
73         logger.info('Resetting device...')
74         handler.reset()
75         logger.info('NFC device has been reset!')
76     else:
77         raise Exception('Unknown option: {}'.format(sys.argv[1]))