Skip to content

Commit

Permalink
tools: Migrate storage and run-fap binaries to RPC session
Browse files Browse the repository at this point in the history
  • Loading branch information
str4d committed Sep 2, 2023
1 parent aa0f84f commit b990553
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 198 deletions.
49 changes: 26 additions & 23 deletions tools/src/bin/run-fap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ use std::{
};

use clap::Parser;
use flipperzero_tools::{serial, storage};
use flipperzero_tools::{
proto::{pb, pb_app, RpcSession},
serial, storage,
};
use rand::{thread_rng, Rng};

#[derive(Parser)]
Expand Down Expand Up @@ -62,17 +65,10 @@ impl From<io::Error> for Error {
}
}

fn wait_for_idle(serial_cli: &mut serial::SerialCli) -> io::Result<()> {
loop {
serial_cli.send_and_wait_eol("loader info")?;
if serial_cli
.consume_response()?
.contains("No application is running")
{
break Ok(());
}
thread::sleep(Duration::from_millis(200));
}
fn wait_for_idle(session: &mut RpcSession) -> io::Result<()> {
// TODO: need equivalent of "loader info" CLI command.
thread::sleep(Duration::from_millis(2000));
Ok(())
}

fn main() -> Result<(), Error> {
Expand All @@ -95,8 +91,8 @@ fn main() -> Result<(), Error> {
.timeout(Duration::from_secs(30))
.open()
.map_err(Error::FailedToOpenSerialPort)?;
let mut store = storage::FlipperStorage::new(port);
store.start().map_err(Error::FailedToStartSerialInterface)?;
let mut store =
storage::FlipperStorage::new(port).map_err(Error::FailedToStartSerialInterface)?;

// Upload the FAP to a temporary directory.
let dest_dir =
Expand All @@ -109,31 +105,38 @@ fn main() -> Result<(), Error> {
.send_file(&cli.fap, &dest_file)
.map_err(Error::FailedToUploadFap)?;

let serial_cli = store.cli_mut();
let session = store.session_mut();

// Wait for no application to be running.
wait_for_idle(serial_cli)?;
wait_for_idle(session)?;

// Run the FAP.
serial_cli.send_and_wait_eol(&format!("loader open {} {}", dest_file, cli.args.join(" ")))?;
session.request(
0,
pb::main::Content::AppStartRequest(pb_app::StartRequest {
name: dest_file.to_string(),
args: cli.args.join(" "),
}),
|resp| match resp {
pb::main::Content::Empty(_) => Ok(()),
r => Err(r),
},
)?;

// Wait for the FAP to finish.
wait_for_idle(serial_cli)?;
wait_for_idle(session)?;

// Download and print the output file, if present.
let output_file = storage::FlipperPath::from("/ext/flipperzero-rs-stdout");
if store.exist_file(&output_file)? {
let output = store.read_file(&output_file)?;
io::stdout().write_all(output.as_ref())?;
store.remove(&output_file)?;
store.remove(&output_file, false)?;
}

// Remove the FAP and temporary directory.
store
.remove(&dest_file)
.map_err(|e| Error::RemoveFailed(dest_file, e))?;
store
.remove(&dest_dir)
.remove(&dest_dir, true)
.map_err(|e| Error::RemoveFailed(dest_dir, e))?;

Ok(())
Expand Down
13 changes: 6 additions & 7 deletions tools/src/bin/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ enum Commands {
/// Flipper path
flipper_path: FlipperPath,
},
/// Format flash card
Format,
/// Remove file/directory
// /// Format flash card
// Format,
// /// Remove file/directory
Remove {
/// Flipper path
flipper_path: FlipperPath,
Expand Down Expand Up @@ -90,13 +90,12 @@ fn main() {
.open()
.expect("unable to open serial port");

let mut store = storage::FlipperStorage::new(port);
store.start().expect("failed to start storage");
let mut store = storage::FlipperStorage::new(port).unwrap();

let result = match command {
Commands::Mkdir { flipper_path } => store.mkdir(flipper_path),
Commands::Format => store.format_ext(),
Commands::Remove { flipper_path } => store.remove(flipper_path),
// Commands::Format => store.format_ext(),
Commands::Remove { flipper_path } => store.remove(flipper_path, true),
Commands::Read => todo!(),
Commands::Size { flipper_path } => match store.size(flipper_path) {
Err(err) => Err(err),
Expand Down
Loading

0 comments on commit b990553

Please sign in to comment.