-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmds.py
89 lines (63 loc) · 2.17 KB
/
cmds.py
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
77
78
79
80
81
82
83
84
85
86
87
88
89
# Copyright (C) 2022-2024 - Tillitis AB
# SPDX-License-Identifier: GPL-2.0-only
import getpass
import logging
from tkeyclient.hw import find_device
from tkeyclient.error import TKeyError
from tkeyclient.tkey import TKey
logger = logging.getLogger('root')
def create_handler(func):
"""
Create wrapper for a client subcommand handler
"""
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except TKeyError as e:
logger.error('[{0}] {1}: {2}'.format(func.__name__, type(e).__name__, e))
return False
return wrapper
def test_connection(args):
"""
Check if serial connection can be opened to the given device
"""
port = get_device(args)
logger.info('Attempting to open serial port: %s' % port)
with TKey(port, connect=True) as tk:
if tk.test() is True:
logger.info('Serial port is open!')
def get_name_version(args):
"""
Retrieve the name and version of the given device
"""
with TKey(get_device(args), connect=True) as tk:
name0, name1, version = tk.get_name_version()
logger.info('Firmware name0:%s name1:%s version:%d' % (name0, name1, version))
def get_udi(args):
"""
Retrieve unique device identifier (UDI) of the given device
"""
with TKey(get_device(args), connect=True) as tk:
logger.info('Got UDI: %s' % tk.get_udi_string())
def load_app(args):
"""
Load an application onto the given device
"""
# Prompt for user-supplied secret if requested
secret = None
if args.secret:
logger.debug('Asking for user-supplied secret (USS)')
secret = getpass.getpass(prompt='Enter secret: ')
with TKey(get_device(args), connect=True) as tk:
tk.load_app(args.file, secret)
logger.info('Application loaded: %s' % args.file)
def get_device(args):
"""
Return device path to use (automatically or manually)
"""
if args.auto:
logger.debug('Scanning for connected TKey devices...')
d = find_device()
logger.debug('Found device: %s (%s %s)' % (d.device, d.manufacturer, d.product))
return d.device
return args.device