Skip to content

Commit b990553

Browse files
committed
tools: Migrate storage and run-fap binaries to RPC session
1 parent aa0f84f commit b990553

File tree

3 files changed

+175
-198
lines changed

3 files changed

+175
-198
lines changed

tools/src/bin/run-fap.rs

+26-23
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ use std::{
77
};
88

99
use clap::Parser;
10-
use flipperzero_tools::{serial, storage};
10+
use flipperzero_tools::{
11+
proto::{pb, pb_app, RpcSession},
12+
serial, storage,
13+
};
1114
use rand::{thread_rng, Rng};
1215

1316
#[derive(Parser)]
@@ -62,17 +65,10 @@ impl From<io::Error> for Error {
6265
}
6366
}
6467

65-
fn wait_for_idle(serial_cli: &mut serial::SerialCli) -> io::Result<()> {
66-
loop {
67-
serial_cli.send_and_wait_eol("loader info")?;
68-
if serial_cli
69-
.consume_response()?
70-
.contains("No application is running")
71-
{
72-
break Ok(());
73-
}
74-
thread::sleep(Duration::from_millis(200));
75-
}
68+
fn wait_for_idle(session: &mut RpcSession) -> io::Result<()> {
69+
// TODO: need equivalent of "loader info" CLI command.
70+
thread::sleep(Duration::from_millis(2000));
71+
Ok(())
7672
}
7773

7874
fn main() -> Result<(), Error> {
@@ -95,8 +91,8 @@ fn main() -> Result<(), Error> {
9591
.timeout(Duration::from_secs(30))
9692
.open()
9793
.map_err(Error::FailedToOpenSerialPort)?;
98-
let mut store = storage::FlipperStorage::new(port);
99-
store.start().map_err(Error::FailedToStartSerialInterface)?;
94+
let mut store =
95+
storage::FlipperStorage::new(port).map_err(Error::FailedToStartSerialInterface)?;
10096

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

112-
let serial_cli = store.cli_mut();
108+
let session = store.session_mut();
113109

114110
// Wait for no application to be running.
115-
wait_for_idle(serial_cli)?;
111+
wait_for_idle(session)?;
116112

117113
// Run the FAP.
118-
serial_cli.send_and_wait_eol(&format!("loader open {} {}", dest_file, cli.args.join(" ")))?;
114+
session.request(
115+
0,
116+
pb::main::Content::AppStartRequest(pb_app::StartRequest {
117+
name: dest_file.to_string(),
118+
args: cli.args.join(" "),
119+
}),
120+
|resp| match resp {
121+
pb::main::Content::Empty(_) => Ok(()),
122+
r => Err(r),
123+
},
124+
)?;
119125

120126
// Wait for the FAP to finish.
121-
wait_for_idle(serial_cli)?;
127+
wait_for_idle(session)?;
122128

123129
// Download and print the output file, if present.
124130
let output_file = storage::FlipperPath::from("/ext/flipperzero-rs-stdout");
125131
if store.exist_file(&output_file)? {
126132
let output = store.read_file(&output_file)?;
127133
io::stdout().write_all(output.as_ref())?;
128-
store.remove(&output_file)?;
134+
store.remove(&output_file, false)?;
129135
}
130136

131137
// Remove the FAP and temporary directory.
132138
store
133-
.remove(&dest_file)
134-
.map_err(|e| Error::RemoveFailed(dest_file, e))?;
135-
store
136-
.remove(&dest_dir)
139+
.remove(&dest_dir, true)
137140
.map_err(|e| Error::RemoveFailed(dest_dir, e))?;
138141

139142
Ok(())

tools/src/bin/storage.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ enum Commands {
3131
/// Flipper path
3232
flipper_path: FlipperPath,
3333
},
34-
/// Format flash card
35-
Format,
36-
/// Remove file/directory
34+
// /// Format flash card
35+
// Format,
36+
// /// Remove file/directory
3737
Remove {
3838
/// Flipper path
3939
flipper_path: FlipperPath,
@@ -90,13 +90,12 @@ fn main() {
9090
.open()
9191
.expect("unable to open serial port");
9292

93-
let mut store = storage::FlipperStorage::new(port);
94-
store.start().expect("failed to start storage");
93+
let mut store = storage::FlipperStorage::new(port).unwrap();
9594

9695
let result = match command {
9796
Commands::Mkdir { flipper_path } => store.mkdir(flipper_path),
98-
Commands::Format => store.format_ext(),
99-
Commands::Remove { flipper_path } => store.remove(flipper_path),
97+
// Commands::Format => store.format_ext(),
98+
Commands::Remove { flipper_path } => store.remove(flipper_path, true),
10099
Commands::Read => todo!(),
101100
Commands::Size { flipper_path } => match store.size(flipper_path) {
102101
Err(err) => Err(err),

0 commit comments

Comments
 (0)