diff --git a/src/main.rs b/src/main.rs index 05a95a0..37da8f1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,7 +25,7 @@ mod ser; mod xb; mod xbpacket; mod xbrx; -// mod pipe; +mod pipe; mod ping; use std::path::PathBuf; @@ -62,6 +62,13 @@ enum Command { }, /// Receive ping requests and transmit pongs Pong, + /// Pipe data across radios using the xbnet protocol + Pipe{ + /// The 64-bit destination for the pipe, in hex + #[structopt(long)] + dest: String, + // FIXME: add a paremter to accept data from only that place + }, } fn main() { @@ -85,6 +92,11 @@ fn main() { }, Command::Pong => { ping::pong(&mut xbreframer, &xb.ser, xbeesender).expect("Failure in pong"); - } + }, + Command::Pipe{dest} => { + let dest_u64:u64 = u64::from_str_radix(&dest, 16).expect("Invalid destination"); + thread::spawn(move || pipe::stdin_processor(dest_u64, 1600, xbeesender).expect("Failure in stdin_processor")); + pipe::stdout_processor(&mut xbreframer, &xb.ser).expect("Failure in stdout_processor"); + }, } } diff --git a/src/pipe.rs b/src/pipe.rs index 13c59f6..43bb274 100644 --- a/src/pipe.rs +++ b/src/pipe.rs @@ -17,4 +17,39 @@ use std::io; use std::io::{Read, Write}; +use crate::xb::*; +use crate::xbpacket::*; +use crate::ser::*; +use crate::xbrx::*; use crossbeam_channel; +use std::thread; +use std::time::Duration; +use bytes::*; + +const INTERVAL: u64 = 5; + +pub fn stdin_processor(dest: u64, maxframesize: usize, + sender: crossbeam_channel::Sender<(XBDestAddr, Bytes)>) -> io::Result<()> { + let stdin = io::stdin(); + let mut br = io::BufReader::new(stdin); + let mut buf = vec![0u8; maxframesize - 1]; + + loop { + let res = br.read(&mut buf)?; + if res == 0 { + // EOF + return Ok(()); + } + + sender.send((XBDestAddr::U64(dest), Bytes::copy_from_slice(&buf[0..res]))).unwrap(); + } +} + +pub fn stdout_processor(xbreframer: &mut XBReframer, ser: &XBSer) -> io::Result<()> { + let mut stdout = io::stdout(); + loop { + let (_fromu64, _fromu16, payload) = xbreframer.rxframe(ser); + stdout.write_all(&payload)?; + stdout.flush()?; + } +}