-
Notifications
You must be signed in to change notification settings - Fork 0
/
setparam.py
executable file
·123 lines (99 loc) · 3.8 KB
/
setparam.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env python2
import sys, os, time
import logging
import argparse
import r2p
from helpers import *
def _create_argsparser():
parser = argparse.ArgumentParser(
description='R2P set app parameter'
)
parser.add_argument(
'-v', '--verbose', required=False, action='count', default=0,
help='verbosity level (default %(default)s): 0=critical, 1=error, 2=warning, 3=info, 4=debug',
dest='verbosity'
)
group = parser.add_argument_group('transport setup')
group.add_argument(
'-p', '--transport', required=False, nargs='+',
default=['DebugTransport', 'SerialLineIO', '/dev/ttyUSB0', 115200],
help='transport parameters',
dest='transport', metavar='PARAMS'
)
group.add_argument(
'-t', '--boot-topic', required=True,
help='name of the bootloader topic for the target R2P module; format: "[\\w]{1,%d}"' % r2p.MODULE_NAME_MAX_LENGTH,
dest='boot_topic_name', metavar='BOOT_TOPIC'
)
group.add_argument(
'-e', '--boot-module', required=True,
help='name of the target R2P module; format: "[\\w]{1,%d}"' % r2p.MODULE_NAME_MAX_LENGTH,
dest='boot_module_name', metavar='BOOT_MODULE'
)
group = parser.add_argument_group('parameter setup')
group.add_argument(
'-n', '--app-name', required=True,
help='name of the app (R2P Node); format: "[\\w]{1,%d}"' % r2p.NODE_NAME_MAX_LENGTH,
dest='app_name', metavar='APP_NAME'
)
group.add_argument(
'-o', '--param-offset', required=True,
help='offset of the target parameter, relative to the app config struct',
dest='offset', metavar='OFFSET'
)
group.add_argument(
'-b', '--param-bytes', required=True,
help='parameter value, as hex byte stream; format: "([0-9A-Fa-f]{2})+"',
dest='value', metavar='HEX_BYTES'
)
return parser
def _main():
parser = _create_argsparser()
args = parser.parse_args()
logging.basicConfig(stream=sys.stderr, level=verbosity2level(int(args.verbosity)))
logging.debug('os.chdir(%s)' % repr(os.path.os.getcwd()))
logging.debug('sys.argv = ' + repr(sys.argv))
# TODO: Automate transport construction from "--transport" args
assert args.transport[0] == 'DebugTransport'
assert args.transport[1] == 'SerialLineIO'
lineio = r2p.SerialLineIO(str(args.transport[2]), int(args.transport[3]))
transport = r2p.DebugTransport('dbgtra', lineio)
mw = r2p.Middleware.instance()
node = r2p.Node('LOADER')
pub = r2p.Publisher()
sub = r2p.Subscriber(4)
bootloader = r2p.BootloaderMaster(pub, sub, args.boot_module_name)
try:
exception = None
mw.initialize('R2PY')
transport.open()
node.begin()
node.advertise(pub, args.boot_topic_name, r2p.Time_INFINITE, r2p.BootMsg)
node.subscribe(sub, args.boot_topic_name, r2p.BootMsg)
time.sleep(0.200)
mw.reboot_remote(args.boot_module_name, True)
time.sleep(2.000)
bootloader.initialize()
time.sleep(0.200)
bootloader.set_parameter(app_name = str(args.app_name),
offset = autoint(args.offset),
value = hexb2str(str(args.value)))
except KeyboardInterrupt as exception:
pass
except Exception as exception:
print '~' * 80
raise
finally:
if exception is not None:
logging.exception(exception)
logging.debug('Unwinding script initialization')
node.end()
mw.uninitialize()
transport.close()
if exception is not None:
raise exception
if __name__ == '__main__':
try:
_main()
except Exception as e:
raise