Skip to content

Commit bdb4e14

Browse files
committed
feat: wip - add json output to cli
1 parent bda22b2 commit bdb4e14

File tree

8 files changed

+344
-105
lines changed

8 files changed

+344
-105
lines changed

Cargo.lock

Lines changed: 47 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

stm32cubeprogrammer-cli/Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,15 @@ bpaf = { version = "0.9", features = ["derive"] }
1515
env_logger = "0.11.5"
1616
indicatif = "0.17.9"
1717
indicatif-log-bridge = "0.2.3"
18-
stm32cubeprogrammer = { path = "../stm32cubeprogrammer" }
18+
stm32cubeprogrammer = { path = "../stm32cubeprogrammer", features = [
19+
"serde",
20+
"validations",
21+
] }
1922
dotenvy.workspace = true
2023
log.workspace = true
2124
anyhow = "1.0.94"
25+
serde_json = "1"
26+
serde = { version = "1", features = ["derive"] }
2227

2328
[dev-dependencies]
2429
test-log.workspace = true

stm32cubeprogrammer-cli/src/main.rs

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
//! - Enabling read protection
88
//! - Disabling read protection
99
//! - Resetting target
10-
//!
10+
//!
1111
//! All commands above can be combined in a single command line invocation by chaining them.
12-
//!
12+
//!
1313
//! If you need other commands, feel free to open an issue or a pull request. :smile:
1414
//!
1515
//! # Example usage:
@@ -28,7 +28,7 @@
2828
//! ```sh
2929
//! STM32_CUBE_PROGRAMMER_DIR=`installation_dir` stm32cubeprogrammer-cli unprotect reset flash-hex `path_to_hex_file` protect
3030
//! ```
31-
//!
31+
//!
3232
//! Use the `--list` flag to list available probes.
3333
//! ```sh
3434
//! stm32cubeprogrammer-cli --stm32-cube-programmer-dir `installation_dir` --list
@@ -49,6 +49,7 @@
4949
5050
mod display_handler;
5151
mod parse;
52+
mod output;
5253

5354
use anyhow::Context;
5455
use display_handler::DisplayHandler;
@@ -198,8 +199,14 @@ fn main() -> Result<(), anyhow::Error> {
198199
// Parse command line arguments
199200
let options = parse::options().run();
200201

202+
let verbosity = if options.quiet {
203+
log::LevelFilter::Error
204+
} else {
205+
options.verbose
206+
};
207+
201208
// Init api
202-
let display_handler = init_display_handler(options.verbosity);
209+
let display_handler = init_display_handler(verbosity);
203210
let api = stm32cubeprogrammer::CubeProgrammer::builder()
204211
.cube_programmer_dir(&options.stm32_cube_programmer_dir)
205212
.display_callback(display_handler.clone())
@@ -255,8 +262,10 @@ fn main() -> Result<(), anyhow::Error> {
255262

256263
// Handle commands
257264
for command in options.target_commands {
258-
match command {
265+
let command_output = match command {
259266
parse::TargetCommand::FlashBin(bin_file_info) => {
267+
log::info!("Flash binary file: {}", bin_file_info);
268+
260269
display_handler
261270
.lock()
262271
.unwrap()
@@ -268,6 +277,8 @@ fn main() -> Result<(), anyhow::Error> {
268277
.with_context(|| "Failed to flash binary file")?;
269278
}
270279
parse::TargetCommand::FlashHex { file } => {
280+
log::info!("Flash hex file: `{:?}`", file);
281+
271282
display_handler
272283
.lock()
273284
.unwrap()
@@ -279,6 +290,8 @@ fn main() -> Result<(), anyhow::Error> {
279290
.with_context(|| "Failed to flash hex file")?;
280291
}
281292
parse::TargetCommand::UpdateBleStack(ble_stack_info) => {
293+
log::info!("Update BLE stack: {}", ble_stack_info);
294+
282295
display_handler
283296
.lock()
284297
.unwrap()
@@ -318,7 +331,23 @@ fn main() -> Result<(), anyhow::Error> {
318331
.with_context(|| "Failed to start BLE stack")?;
319332
}
320333
}
334+
parse::TargetCommand::BleStackInfo { compare } => {
335+
let fus_programmer = programmer_connection.fus_connection()?;
336+
let target_version = fus_programmer.fus_info().wireless_stack_version;
337+
338+
log::info!("FUS info: {}", fus_programmer.fus_info());
339+
340+
if let Some(compare) = compare {
341+
log::info!("Comparing BLE stack versions. Given version: {} ; Version on target: {} ; Stack up to date: {}", compare, target_version, compare == target_version);
342+
}
343+
344+
fus_programmer
345+
.start_wireless_stack()
346+
.with_context(|| "Failed to start BLE stack")?;
347+
}
321348
parse::TargetCommand::Reset(reset_mode) => {
349+
log::info!("Resetting target: {:?}", reset_mode);
350+
322351
display_handler
323352
.lock()
324353
.unwrap()
@@ -330,6 +359,8 @@ fn main() -> Result<(), anyhow::Error> {
330359
.with_context(|| "Failed to reset target")?;
331360
}
332361
parse::TargetCommand::MassErase => {
362+
log::info!("Mass erase");
363+
333364
display_handler
334365
.lock()
335366
.unwrap()
@@ -341,6 +372,8 @@ fn main() -> Result<(), anyhow::Error> {
341372
.with_context(|| "Failed to mass erase target")?;
342373
}
343374
parse::TargetCommand::Protect => {
375+
log::info!("Enable read protection");
376+
344377
display_handler
345378
.lock()
346379
.unwrap()
@@ -352,6 +385,8 @@ fn main() -> Result<(), anyhow::Error> {
352385
.with_context(|| "Failed to enable read protection")?;
353386
}
354387
parse::TargetCommand::Unprotect => {
388+
log::info!("Disable read protection");
389+
355390
display_handler
356391
.lock()
357392
.unwrap()

stm32cubeprogrammer-cli/src/output.rs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
use serde::Serialize;
2+
use std::env::ArgsOs;
3+
4+
pub struct Output {
5+
schema_version: String,
6+
args: String,
7+
list: Option<Vec<stm32cubeprogrammer::probe::Serial>>,
8+
selected_probe: Option<stm32cubeprogrammer::probe::Serial>,
9+
target_information: Option<stm32cubeprogrammer::api_types::TargetInformation>,
10+
command_output: Option<Vec<CommandOutput>>,
11+
}
12+
13+
impl Output {
14+
const SCHEMA_VERSION: &str = "1";
15+
16+
pub fn new(args: ArgsOs) -> Self {
17+
let schema_version = Self::SCHEMA_VERSION.to_string();
18+
let args = args.map(|x| x.to_string_lossy().to_string()).collect();
19+
20+
Self {
21+
schema_version,
22+
args,
23+
list: None,
24+
selected_probe: None,
25+
target_information: None,
26+
command_output: None,
27+
}
28+
}
29+
30+
pub fn add_probe_list(&mut self, list: Vec<stm32cubeprogrammer::probe::Serial>) {
31+
self.list = Some(list);
32+
}
33+
34+
pub fn add_command_output(&mut self, command: CommandOutput) {
35+
if let Some(ref mut command_output) = self.command_output {
36+
command_output.push(command);
37+
} else {
38+
self.command_output = Some(vec![command]);
39+
}
40+
}
41+
}
42+
43+
#[derive(Serialize)]
44+
#[serde(tag = "type")]
45+
pub enum CommandOutput {
46+
Info(stm32cubeprogrammer::api_types::TargetInformation),
47+
FlashBin {
48+
file: std::path::PathBuf,
49+
address: u32,
50+
},
51+
FlashHex {
52+
file: std::path::PathBuf,
53+
},
54+
UpdateBleStack {
55+
file: std::path::PathBuf,
56+
address: u32,
57+
},
58+
BleStackInfo(stm32cubeprogrammer::fus::Information),
59+
Reset(stm32cubeprogrammer::api_types::probe::ResetMode),
60+
MassErase,
61+
Protect,
62+
Unprotect,
63+
}
64+
65+
#[derive(Serialize)]
66+
pub enum BleStackUpdated {
67+
NotUpdatet,
68+
Updated(BleStackUpdateReason),
69+
}
70+
71+
#[derive(Serialize)]
72+
pub enum BleStackUpdateReason {
73+
Force,
74+
VersionNotEqual {
75+
expected: stm32cubeprogrammer::fus::Version,
76+
on_target: stm32cubeprogrammer::fus::Version,
77+
},
78+
NoVersionProvided,
79+
}

0 commit comments

Comments
 (0)