Skip to content

Commit

Permalink
feat: Install Command using probe-rs (#32)
Browse files Browse the repository at this point in the history
Signed-off-by: Ana Micu <[email protected]>
Co-authored-by: Ana Micu <[email protected]>
  • Loading branch information
george-cosma and Ana Micu authored Sep 12, 2024
1 parent 7db3faa commit 6851460
Show file tree
Hide file tree
Showing 10 changed files with 528 additions and 118 deletions.
5 changes: 5 additions & 0 deletions tockloader-cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ fn get_subcommands() -> Vec<Command> {
.args(get_app_args())
.args(get_channel_args())
.arg_required_else_help(true),
Command::new("install")
.about("Install apps")
.args(get_app_args())
.args(get_channel_args())
.arg_required_else_help(false),
]
}

Expand Down
110 changes: 79 additions & 31 deletions tockloader-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
mod cli;
mod display;
mod errors;
mod serial;

use cli::make_cli;
use display::{print_info, print_list};
use errors::TockloaderError;
use serial::select_probe;
use tockloader_lib::{info_probe, list_probe};
use inquire::Select;
use tockloader_lib::{
connection::{Connection, ConnectionInfo},
info_probe, install_app_probe_rs, install_app_serial, list_debug_probes, list_probe,
list_serial_ports,
tabs::tab::Tab,
};

#[tokio::main]
async fn main() -> Result<(), TockloaderError> {
Expand Down Expand Up @@ -40,43 +44,87 @@ async fn run() -> Result<(), TockloaderError> {
}
Some(("list", sub_matches)) => {
// TODO(george-cosma):inspect-err
let probe = select_probe();
match probe {
Ok(probe) => {
let mut apps_details = list_probe(
probe,
sub_matches.get_one::<String>("chip").unwrap(),
sub_matches.get_one::<usize>("core").unwrap(),
)
.await;
print_list(&mut apps_details).await;
}
Err(err) => println!("{}", err),
}
// TODO(Micu Ana): Add error handling
let ans =
Select::new("Which debug probe do you want to use?", list_debug_probes()).prompt();
// Open connection
let conn = Connection::open(
tockloader_lib::connection::ConnectionInfo::ProbeInfo(ans.unwrap()),
Some(sub_matches.get_one::<String>("chip").unwrap().to_string()),
);

let mut apps_details =
list_probe(conn.unwrap(), sub_matches.get_one::<usize>("core").unwrap())
.await
.unwrap();
print_list(&mut apps_details).await;
}
Some(("info", sub_matches)) => {
let probe = select_probe();
match probe {
Ok(probe) => {
let mut attributes = info_probe(
probe,
sub_matches.get_one::<String>("chip").unwrap(),
sub_matches.get_one::<usize>("core").unwrap(),
)
.await;
print_info(&mut attributes.apps, &mut attributes.system).await;
// TODO(Micu Ana): Add error handling
let ans =
Select::new("Which debug probe do you want to use?", list_debug_probes()).prompt();
// Open connection
let conn = Connection::open(
tockloader_lib::connection::ConnectionInfo::ProbeInfo(ans.unwrap()),
Some(sub_matches.get_one::<String>("chip").unwrap().to_string()),
);

let mut attributes =
info_probe(conn.unwrap(), sub_matches.get_one::<usize>("core").unwrap())
.await
.unwrap();

print_info(&mut attributes.apps, &mut attributes.system).await;
}
Some(("install", sub_matches)) => {
let tab_file =
Tab::open(sub_matches.get_one::<String>("tab").unwrap().to_string()).unwrap();
// If "--serial" flag is used, we choose the serial connection
if *sub_matches.get_one::<bool>("serial").unwrap() {
let serial_ports = list_serial_ports();
// Let the user choose the port that will be used
let mut port_names = Vec::new();
for port in serial_ports {
port_names.push(port.port_name);
}
Err(err) => println!("{}", err),
// TODO(Micu Ana): Add error handling
let ans = Select::new("Which serial port do you want to use?", port_names)
.prompt()
.unwrap();
// Open connection
let conn = Connection::open(ConnectionInfo::from(ans), None);
// Install app
install_app_serial(
conn.unwrap(),
sub_matches.get_one::<String>("board").unwrap(),
tab_file,
)
.await
.unwrap();
} else {
// TODO(Micu Ana): Add error handling
let ans = Select::new("Which debug probe do you want to use?", list_debug_probes())
.prompt();
// Open connection
let conn = Connection::open(
tockloader_lib::connection::ConnectionInfo::ProbeInfo(ans.unwrap()),
Some(sub_matches.get_one::<String>("chip").unwrap().to_string()),
);
// Install app
install_app_probe_rs(
conn.unwrap(),
sub_matches.get_one::<usize>("core").unwrap(),
tab_file,
)
.await
.unwrap();
}
}

// If only the "--debug" flag is set, then this branch is executed
// Or, more likely at this stage, a subcommand hasn't been implemented yet.
_ => {
println!("Could not run the provided subcommand.");
_ = make_cli().print_help();
}
}

// TODO(Micu Ana): Add error handling
Ok(())
}
13 changes: 0 additions & 13 deletions tockloader-cli/src/serial.rs

This file was deleted.

6 changes: 4 additions & 2 deletions tockloader-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ probe-rs = "0.24.0"
tbf-parser = { path = "../tbf-parser"}
utf8-decode = "1.0.1"
byteorder = "1.5.0"


tar = "0.4.41"
bytes = "1.7.1"
toml = "0.8.19"
serde = { version = "1.0.210", features = ["derive"] }
58 changes: 58 additions & 0 deletions tockloader-lib/src/connection.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use std::time::Duration;

use probe_rs::{probe::DebugProbeInfo, Permissions, Session};
use tokio_serial::{FlowControl, Parity, SerialPort, SerialStream, StopBits};

use crate::errors::TockloaderError;

pub enum ConnectionInfo {
SerialInfo(String),
ProbeInfo(DebugProbeInfo),
}

impl From<String> for ConnectionInfo {
fn from(value: String) -> Self {
ConnectionInfo::SerialInfo(value)
}
}

pub enum Connection {
ProbeRS(Session),
Serial(SerialStream),
}

impl Connection {
pub fn open(info: ConnectionInfo, chip: Option<String>) -> Result<Connection, TockloaderError> {
match info {
ConnectionInfo::SerialInfo(serial_info) => {
let builder = tokio_serial::new(serial_info, 115200);
match SerialStream::open(&builder) {
Ok(mut port) => {
port.set_parity(Parity::None).unwrap();
port.set_stop_bits(StopBits::One).unwrap();
port.set_flow_control(FlowControl::None).unwrap();
port.set_timeout(Duration::from_millis(500)).unwrap();
port.write_request_to_send(false).unwrap();
port.write_data_terminal_ready(false).unwrap();
Ok(Connection::Serial(port))
}
Err(_) => {
//TODO(Micu Ana): Add error handling
Err(TockloaderError::NoPortAvailable)
}
}
}
ConnectionInfo::ProbeInfo(probe_info) => {
//TODO(Micu Ana): Add error handling
let probe = probe_info.open().unwrap();
match probe.attach(chip.unwrap(), Permissions::default()) {
Ok(session) => Ok(Connection::ProbeRS(session)),
Err(_) => {
//TODO(Micu Ana): Add error handling
Err(TockloaderError::NoPortAvailable)
}
}
}
}
}
}
Loading

0 comments on commit 6851460

Please sign in to comment.