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