Repository navigation
Expand file tree
/
Copy pathfw-update.py
More file actions
145 lines (118 loc) · 5.2 KB
/
Copy pathfw-update.py
File metadata and controls
145 lines (118 loc) · 5.2 KB
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
#!/usr/bin/env python3
"""
License: Apache 2.0. See LICENSE file in root directory.
Copyright(c) 2020-2024 RealSense, Inc. All Rights Reserved.
"""
import argparse
import pathlib
import threading
from argparse import SUPPRESS, ArgumentParser
import rsid_py
try:
from tqdm import tqdm
tqdm_installed = False
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 (auto-detected if not specified)", required=False, type=str, default=None)
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("--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
────────
* Type: {device_type}
* Serial number: {serial_number}
* 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
──────────────────────
* Security: {security_compat}
{security_msg}
* Host: {host_compat}
{host_msg}
"""
COMPATIBLE = "✓ Compatible"
NOT_COMPATIBLE = "𐄂 Not Compatible"
if __name__ == '__main__':
args = build_arg_parser().parse_args()
if args.port is None:
devices = rsid_py.discover_devices()
if not devices:
print("Error: No rsid devices found.")
exit(1)
if len(devices) > 1:
print("Error: Multiple devices found. Use --port to specify one.")
for d in devices:
print(f" {d.serial_port}")
exit(1)
args.port = devices[0].serial_port
print(f"Using device on port {args.port}")
update_progress: float = 0
pbar = None
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()
security_compat, security_msg = updater.is_security_compatible()
host_compat, host_msg = updater.is_host_compatible(device_fw_info.device_type)
print(report_template.format(
device_type = device_fw_info.device_type,
serial_number=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,
security_compat=COMPATIBLE if security_compat else NOT_COMPATIBLE, security_msg=security_msg,
host_compat=COMPATIBLE if host_compat else NOT_COMPATIBLE, host_msg=host_msg))
if args.dry_run:
exit(0)
if not security_compat:
print(f"Aborting: {security_msg}")
exit(1)
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)