|
| 1 | +#![allow(clippy::needless_doctest_main)] |
| 2 | +//! External processor support |
| 3 | +//! |
| 4 | +//! Via the command line argument `--processors` you can instruct espflash to run external executables to pre-process |
| 5 | +//! the logs received from the target. Multiple processors are supported by separating them via `,`. Processors are executed in the specified order. |
| 6 | +//! |
| 7 | +//! You can use full-qualified paths or run an executable which is already in the search path. |
| 8 | +//! |
| 9 | +//! A processors reads from stdin and output to stdout. Be aware this runs before further processing by espflash. |
| 10 | +//! i.e. addresses are not resolved and when using `defmt` you will see encoded data. |
| 11 | +//! |
| 12 | +//! Additionally be aware that you might receive chunked data which is not always split at valid UTF character boundaries. |
| 13 | +//! |
| 14 | +//! The executable will get the path of the ELF file as the first argument if available. |
| 15 | +//! |
| 16 | +//! Example processor which turns some letters into uppercase |
| 17 | +//! ```rust,no-run |
| 18 | +//! use std::io::{stdin, stdout, Read, Write}; |
| 19 | +//! |
| 20 | +//! fn main() { |
| 21 | +//! let args: Vec<String> = std::env::args().collect(); |
| 22 | +//! println!("ELF file: {:?}", args[1]); |
| 23 | +//! |
| 24 | +//! let mut buf = [0u8; 1024]; |
| 25 | +//! loop { |
| 26 | +//! if let Ok(len) = stdin().read(&mut buf) { |
| 27 | +//! for b in &mut buf[..len] { |
| 28 | +//! *b = if b"abdfeo".contains(b) { |
| 29 | +//! b.to_ascii_uppercase() |
| 30 | +//! } else { |
| 31 | +//! *b |
| 32 | +//! }; |
| 33 | +//! } |
| 34 | +//! |
| 35 | +//! stdout().write(&buf[..len]).unwrap(); |
| 36 | +//! stdout().flush().unwrap(); |
| 37 | +//! } else { |
| 38 | +//! // ignored |
| 39 | +//! } |
| 40 | +//! } |
| 41 | +//! } |
| 42 | +//! ``` |
| 43 | +
|
| 44 | +use std::{ |
| 45 | + fmt::Display, |
| 46 | + io::{Read, Write}, |
| 47 | + path::PathBuf, |
| 48 | + process::{Child, ChildStdin, Stdio}, |
| 49 | + sync::mpsc, |
| 50 | +}; |
| 51 | + |
| 52 | +use miette::Diagnostic; |
| 53 | + |
| 54 | +#[derive(Debug)] |
| 55 | +pub struct Error { |
| 56 | + executable: String, |
| 57 | +} |
| 58 | + |
| 59 | +impl Display for Error { |
| 60 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 61 | + write!(f, "Failed to launch '{}'", self.executable) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +impl std::error::Error for Error {} |
| 66 | + |
| 67 | +impl Diagnostic for Error {} |
| 68 | + |
| 69 | +struct Processor { |
| 70 | + rx: mpsc::Receiver<u8>, |
| 71 | + stdin: ChildStdin, |
| 72 | + child: Child, |
| 73 | +} |
| 74 | + |
| 75 | +impl Processor { |
| 76 | + pub fn new(child: Child) -> Self { |
| 77 | + let mut child = child; |
| 78 | + let (tx, rx) = mpsc::channel::<u8>(); |
| 79 | + |
| 80 | + let mut stdout = child.stdout.take().unwrap(); |
| 81 | + let stdin = child.stdin.take().unwrap(); |
| 82 | + |
| 83 | + std::thread::spawn(move || { |
| 84 | + let mut buffer = [0u8; 1024]; |
| 85 | + loop { |
| 86 | + if let Ok(len) = stdout.read(&mut buffer) { |
| 87 | + for b in &buffer[..len] { |
| 88 | + if tx.send(*b).is_err() { |
| 89 | + break; |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + }); |
| 95 | + |
| 96 | + Self { rx, stdin, child } |
| 97 | + } |
| 98 | + |
| 99 | + pub fn try_receive(&mut self) -> Vec<u8> { |
| 100 | + let mut res = Vec::new(); |
| 101 | + while let Ok(b) = self.rx.try_recv() { |
| 102 | + res.push(b); |
| 103 | + } |
| 104 | + res |
| 105 | + } |
| 106 | + |
| 107 | + pub fn send(&mut self, data: Vec<u8>) { |
| 108 | + let _ignored = self.stdin.write(&data).ok(); |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +impl Drop for Processor { |
| 113 | + fn drop(&mut self) { |
| 114 | + self.child.kill().unwrap(); |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +pub struct ExternalProcessors { |
| 119 | + processors: Vec<Processor>, |
| 120 | +} |
| 121 | + |
| 122 | +impl ExternalProcessors { |
| 123 | + pub fn new(processors: Option<String>, elf: Option<PathBuf>) -> Result<Self, Error> { |
| 124 | + let mut args = Vec::new(); |
| 125 | + |
| 126 | + if let Some(elf) = elf { |
| 127 | + args.push(elf.as_os_str().to_str().unwrap().to_string()); |
| 128 | + }; |
| 129 | + |
| 130 | + let mut spawned = Vec::new(); |
| 131 | + if let Some(processors) = processors { |
| 132 | + for processor in processors.split(",") { |
| 133 | + let processor = std::process::Command::new(processor) |
| 134 | + .args(args.clone()) |
| 135 | + .stdin(Stdio::piped()) |
| 136 | + .stdout(Stdio::piped()) |
| 137 | + .stderr(Stdio::inherit()) |
| 138 | + .spawn() |
| 139 | + .map_err(|_| Error { |
| 140 | + executable: processor.to_string(), |
| 141 | + })?; |
| 142 | + spawned.push(Processor::new(processor)); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + Ok(Self { |
| 147 | + processors: spawned, |
| 148 | + }) |
| 149 | + } |
| 150 | + |
| 151 | + pub fn process(&mut self, read: &[u8]) -> Vec<u8> { |
| 152 | + let mut buffer = Vec::new(); |
| 153 | + buffer.extend_from_slice(read); |
| 154 | + |
| 155 | + for processor in &mut self.processors { |
| 156 | + processor.send(buffer); |
| 157 | + buffer = processor.try_receive(); |
| 158 | + } |
| 159 | + |
| 160 | + buffer |
| 161 | + } |
| 162 | +} |
0 commit comments