Skip to content

Commit 22747a9

Browse files
committed
tools: Migrate storage and run-fap binaries to RPC session
1 parent f7a5d15 commit 22747a9

File tree

3 files changed

+181
-198
lines changed

3 files changed

+181
-198
lines changed

tools/src/bin/run-fap.rs

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

99
use clap::Parser;
10-
use flipperzero_tools::{serial, storage};
10+
use flipperzero_tools::{
11+
proto::{
12+
gen::{pb, pb_app},
13+
RpcSession,
14+
},
15+
serial, storage,
16+
};
1117
use rand::{thread_rng, Rng};
1218

1319
#[derive(Parser)]
@@ -62,17 +68,10 @@ impl From<io::Error> for Error {
6268
}
6369
}
6470

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-
}
71+
fn wait_for_idle(session: &mut RpcSession) -> io::Result<()> {
72+
// TODO: need equivalent of "loader info" CLI command.
73+
thread::sleep(Duration::from_millis(2000));
74+
Ok(())
7675
}
7776

7877
fn main() -> Result<(), Error> {
@@ -95,8 +94,8 @@ fn main() -> Result<(), Error> {
9594
.timeout(Duration::from_secs(30))
9695
.open()
9796
.map_err(Error::FailedToOpenSerialPort)?;
98-
let mut store = storage::FlipperStorage::new(port);
99-
store.start().map_err(Error::FailedToStartSerialInterface)?;
97+
let mut store =
98+
storage::FlipperStorage::new(port).map_err(Error::FailedToStartSerialInterface)?;
10099

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

112-
let serial_cli = store.cli_mut();
111+
let session = store.session_mut();
113112

114113
// Wait for no application to be running.
115-
wait_for_idle(serial_cli)?;
114+
wait_for_idle(session)?;
116115

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

120129
// Wait for the FAP to finish.
121-
wait_for_idle(serial_cli)?;
130+
wait_for_idle(session)?;
122131

123132
// Download and print the output file, if present.
124133
let output_file = storage::FlipperPath::from("/ext/flipperzero-rs-stdout");
125134
if store.exist_file(&output_file)? {
126135
let output = store.read_file(&output_file)?;
127136
io::stdout().write_all(output.as_ref())?;
128-
store.remove(&output_file)?;
137+
store.remove(&output_file, false)?;
129138
}
130139

131140
// Remove the FAP and temporary directory.
132141
store
133-
.remove(&dest_file)
134-
.map_err(|e| Error::RemoveFailed(dest_file, e))?;
135-
store
136-
.remove(&dest_dir)
142+
.remove(&dest_dir, true)
137143
.map_err(|e| Error::RemoveFailed(dest_dir, e))?;
138144

139145
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)