use std::path::PathBuf;
use tokio::io::{AsyncRead, AsyncWriteExt};
use tokio::net::TcpStream;
#[derive(Debug, Clone)]
pub enum Source {
Tcp(String),
File(PathBuf),
}
impl Source {
pub async fn open(&self) -> std::io::Result<Box<dyn AsyncRead + Unpin + Send>> {
match self {
Source::Tcp(addr) => Ok(Box::new(TcpStream::connect(addr).await?)),
Source::File(path) => Ok(Box::new(tokio::fs::File::open(path).await?)),
}
}
}
pub async fn openocd_enable(
telnet_addr: &str,
trace_port: u16,
cpu_hz: u32,
swo_hz: u32,
) -> std::io::Result<()> {
let mut conn = TcpStream::connect(telnet_addr).await?;
let commands = [
format!("tpiu config internal :{trace_port} uart off {cpu_hz} {swo_hz}"),
"itm ports on".to_string(),
];
for cmd in commands {
conn.write_all(cmd.as_bytes()).await?;
conn.write_all(b"\r\n").await?;
}
conn.flush().await?;
Ok(())
}