forked from WyliodrinEmbeddedIoT/tockloader-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rs
More file actions
255 lines (214 loc) · 8.17 KB
/
Copy pathmain.rs
File metadata and controls
255 lines (214 loc) · 8.17 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Licensed under the Apache License, Version 2.0 or the MIT License.
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Copyright OXIDOS AUTOMOTIVE 2024.
mod cli;
mod display;
mod known_boards;
use anyhow::{Context, Result};
use clap::ArgMatches;
use cli::make_cli;
use known_boards::KnownBoardNames;
use tockloader_lib::board_settings::BoardSettings;
use tockloader_lib::connection::{
Connection, ProbeRSConnection, ProbeTargetInfo, SerialConnection, SerialTargetInfo,
TockloaderConnection,
};
use tockloader_lib::known_boards::KnownBoard;
use tockloader_lib::tabs::tab::Tab;
use tockloader_lib::{
list_debug_probes, list_serial_ports, CommandEraseApps, CommandInfo, CommandInstall,
CommandList,
};
fn get_serial_target_info(user_options: &ArgMatches) -> SerialTargetInfo {
let board = get_known_board(user_options);
if let Some(board) = board {
return board.serial_target_info();
}
let mut result = SerialTargetInfo::default();
if let Some(baud_rate) = user_options.get_one::<u32>("baud-rate") {
result.baud_rate = *baud_rate;
}
result
}
fn get_probe_target_info(user_options: &ArgMatches) -> ProbeTargetInfo {
let board = get_known_board(user_options);
if let Some(board) = board {
return board.probe_target_info();
}
let chip = user_options
.get_one::<String>("chip")
.expect("Expected validation to catch missing chip")
.clone();
let mut result = ProbeTargetInfo::default(chip);
if let Some(core) = user_options.get_one::<usize>("core") {
result.core = *core;
}
result
}
fn get_board_settings(user_options: &ArgMatches) -> BoardSettings {
let board = get_known_board(user_options);
if let Some(board) = board {
return board.get_settings();
}
let result = BoardSettings::default();
if let Some(_strat_address_str) = user_options.get_one::<String>("app-address") {
todo!()
}
result
}
fn using_serial(user_options: &ArgMatches) -> bool {
let serial_flag = *user_options.get_one::<bool>("serial").unwrap_or(&false);
if serial_flag {
return true;
}
let is_listen_command = user_options
.try_get_one::<String>("protocol")
.is_ok_and(|option| option.is_some_and(|val| val == "legacy"));
is_listen_command
}
fn get_known_board(user_options: &ArgMatches) -> Option<Box<dyn KnownBoard>> {
user_options.get_one::<String>("board").map(|board| {
match KnownBoardNames::from_str(board).expect("validation to ensure valid board") {
KnownBoardNames::NucleoF4 => {
Box::new(tockloader_lib::known_boards::NucleoF4) as Box<dyn KnownBoard>
}
KnownBoardNames::MicrobitV2 => {
Box::new(tockloader_lib::known_boards::MicrobitV2) as Box<dyn KnownBoard>
}
KnownBoardNames::Nrf52840dk => {
Box::new(tockloader_lib::known_boards::Nrf52840dk) as Box<dyn KnownBoard>
}
KnownBoardNames::NucleoU545ReQ => {
Box::new(tockloader_lib::known_boards::NucleoU545ReQ) as Box<dyn KnownBoard>
}
}
})
}
async fn open_connection(user_options: &ArgMatches) -> Result<TockloaderConnection> {
if using_serial(user_options) {
let path = if let Some(path) = user_options.get_one::<String>("port") {
path.clone()
} else {
let serial_ports = list_serial_ports().context("Failed to list serial ports.")?;
let port_names: Vec<_> = serial_ports.iter().map(|p| p.port_name.clone()).collect();
inquire::Select::new("Which serial port do you want to use?", port_names)
.prompt()
.context("No device is connected.")?
};
let mut conn: TockloaderConnection = SerialConnection::new(
path,
get_serial_target_info(user_options),
get_board_settings(user_options),
)
.into();
conn.open()
.await
.context("Failed to open serial connection.")?;
Ok(conn)
} else {
let ans =
inquire::Select::new("Which debug probe do you want to use?", list_debug_probes())
.prompt()
.context("No debug probe is connected.")?;
let mut conn: TockloaderConnection = ProbeRSConnection::new(
ans,
get_probe_target_info(user_options),
get_board_settings(user_options),
)
.into();
conn.open()
.await
.context("Failed to open probe connection.")?;
Ok(conn)
}
}
#[tokio::main]
async fn main() -> Result<()> {
let mut cmd = cli::make_cli();
let matches = cmd.get_matches_mut();
let user_level = matches
.get_one::<String>("log-level")
.map(String::as_str)
.unwrap();
let mut builder = env_logger::Builder::new();
let cli_level = match user_level {
"error" => log::LevelFilter::Error,
"warn" => log::LevelFilter::Warn,
"info" => log::LevelFilter::Info,
"debug" => log::LevelFilter::Debug,
"trace" => log::LevelFilter::Trace,
level => panic!("Unknown log level: {level}"),
};
builder.filter_level(log::LevelFilter::Off);
builder.filter_module("tockloader-lib", cli_level);
builder.filter_module("tockloader", cli_level);
builder.filter_module("tbf_parser", cli_level);
builder.init();
match matches.subcommand() {
Some(("listen", sub_matches)) => {
cli::validate(&mut cmd, sub_matches);
let protocol = sub_matches
.get_one::<String>("protocol")
.map(String::as_str)
.unwrap();
match protocol {
"legacy" => {
let conn = open_connection(sub_matches).await?;
match conn {
TockloaderConnection::ProbeRS(_) => panic!("Cannot establish connection."),
TockloaderConnection::Serial(serial_connection) => {
tock_process_console::legacy::run(
serial_connection
.into_inner_stream()
.expect("Expected board to be connected."),
)
.await;
}
}
}
"pconsole" => {
tock_process_console::pconsole::run()
.await
.context("Failed to run console.")?;
}
_ => {
panic!("Invalid protocol");
}
}
}
Some(("list", sub_matches)) => {
cli::validate(&mut cmd, sub_matches);
let mut conn = open_connection(sub_matches).await?;
let app_details = conn.list().await.context("Failed to list apps.")?;
display::print_list(&app_details).await;
}
Some(("info", sub_matches)) => {
cli::validate(&mut cmd, sub_matches);
let mut conn = open_connection(sub_matches).await?;
let mut attributes = conn
.info()
.await
.context("Failed to get data from the board.")?;
display::print_info(&mut attributes.apps, &mut attributes.system).await;
}
Some(("install", sub_matches)) => {
cli::validate(&mut cmd, sub_matches);
let tab_file = Tab::open(sub_matches.get_one::<String>("tab").unwrap().to_string())
.context("Failed to use provided tab file.")?;
let mut conn = open_connection(sub_matches).await?;
conn.install_app(tab_file)
.await
.context("Failed to install app.")?;
}
Some(("erase-apps", sub_matches)) => {
cli::validate(&mut cmd, sub_matches);
let mut conn = open_connection(sub_matches).await?;
conn.erase_apps().await.context("Failed to erase apps.")?;
}
_ => {
println!("Could not run the provided subcommand.");
_ = make_cli().print_help();
}
}
Ok(())
}