-
Notifications
You must be signed in to change notification settings - Fork 58
/
fw-update.py
167 lines (134 loc) · 6.51 KB
/
fw-update.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env python3
"""
License: Apache 2.0. See LICENSE file in root directory.
Copyright(c) 2020-2024 Intel Corporation. All Rights Reserved.
"""
import argparse
import pathlib
import threading
from argparse import SUPPRESS, ArgumentParser
import rsid_py
try:
from tqdm import tqdm
tqdm_installed = True
except ImportError:
print('tqdm not installed - progress bar will not be available. pip install tqdm to enable.')
tqdm_installed = False
def build_arg_parser():
class MultiFormatter(argparse.RawTextHelpFormatter,
argparse.ArgumentDefaultsHelpFormatter):
pass
arg_parser = ArgumentParser(prog='fw-update-py', add_help=False,
formatter_class=MultiFormatter)
options = arg_parser.add_argument_group('Options')
options.add_argument('-h', '--help', action='help', default=SUPPRESS,
help='Show this help message and exit.')
options.add_argument("-p", "--port", help="Device port", required=True, type=str)
options.add_argument("-f", "--file", help="Firmware binary file path", required=True, type=pathlib.Path)
options.add_argument("--dry-run", help="Show summary report and exit", required=False, default=False,
action='store_true')
options.add_argument("--skip-online", help="Skip checking for latest version online",
required=False, default=False,
action='store_true')
options.add_argument("--force-version", help="Force update even if host version mismatch",
action='store_true',
default=False)
options.add_argument("--force-full", help="Force update of modules even if they already exist \n"
"in the current device \nfirmware. This will update all modules. \n",
action='store_true')
return arg_parser
report_template = """
Summary Report
* Device
────────
* Serial number: {serial_umber}
* Serial port: {serial_port}
* Firmware version: {firmware_version}
* Recognition module version: {recognition_module_version}
* Firmware File
───────────────
* Firmware file path: {bin_file_path}
* Firmware version: {bin_firmware_version}
* Recognition module version: {bin_recognition_module_version}
* Compatibility Matrix
──────────────────────
* SKU: {sku_compat}
{sku_msg}
* Host: {host_compat}
{host_msg}
* Update Policy: {policy_compat}
{policy_msg}
"""
update_remote_template = """
Update Checker Report: {update_note}
* Current Software
──────────────────
You are currently running
* Firmware: {local_fw_version}
* SDK/Host: {local_sw_version}
* Remote Software
──────────────────
The following software is available online:
* Firmware: {remote_fw_version}
* SDK/Host: {remote_sw_version}
* Download URL: {release_url}
* Release notes URL: {release_notes_url}
"""
COMPATIBLE = "✓ Compatible"
NOT_COMPATIBLE = "𐄂 Not Compatible"
RUNNING_LATEST = "✓ You are running the latest software"
NEWER_AVAILABLE = "𐄂 Newer software is available online"
if __name__ == '__main__':
args = build_arg_parser().parse_args()
update_progress: float = 0
pbar = None
if not args.skip_online:
update_available, local_info, remote_info = rsid_py.UpdateChecker.is_update_available(args.port)
print(update_remote_template.format(local_fw_version=local_info.fw_version_str,
local_sw_version=local_info.sw_version_str,
remote_fw_version=remote_info.fw_version_str,
remote_sw_version=remote_info.sw_version_str,
release_url=remote_info.release_url,
release_notes_url=remote_info.release_notes_url,
update_note=NEWER_AVAILABLE if update_available else RUNNING_LATEST))
def progress_callback(progress: float) -> None:
global update_progress
update_progress = progress
percent = int(100 * progress)
if pbar is not None:
pbar.update(percent)
else:
print(f"Progress: {percent}%")
with rsid_py.FWUpdater(str(args.file), args.port) as updater:
fw_file_info = updater.get_firmware_bin_info()
device_fw_info = updater.get_device_firmware_info()
sku_compat, sku_msg = updater.is_sku_compatible()
host_compat, host_msg = updater.is_host_compatible()
policy_compat, policy_msg = updater.is_policy_compatible()
print(report_template.format(serial_umber=device_fw_info.serial_number,
serial_port=args.port,
firmware_version=device_fw_info.fw_version,
bin_file_path=args.file,
recognition_module_version=device_fw_info.recognition_version,
bin_firmware_version=fw_file_info.fw_version,
bin_recognition_module_version=fw_file_info.recognition_version,
sku_compat=COMPATIBLE if sku_compat else NOT_COMPATIBLE, sku_msg=sku_msg,
host_compat=COMPATIBLE if host_compat else NOT_COMPATIBLE, host_msg=host_msg,
policy_compat=COMPATIBLE if policy_compat else NOT_COMPATIBLE,
policy_msg=policy_msg))
if args.dry_run:
exit(0)
print("Started update process...")
if tqdm_installed:
pbar = tqdm(total=100, desc="Updating firmware")
status = updater.update(force_version=args.force_version,
force_full=args.force_full,
progress_callback=progress_callback)
if status == rsid_py.Status.Ok:
condition = threading.Condition()
with condition:
condition.wait_for(lambda: update_progress == 1)
print("Done!")
else:
print(f"Failed to start update with status {status}")
exit(status.value)