pub fn listen(
    socket: &CANSocket,
    channel: &str,
    logfile: &Path,
    tx_frame: CANFrame,
) -> std::io::Result<()> {
    fn write(frame: CANFrame, channel: &str, logfile: &Path, note: &str) -> std::io::Result<()> {
        // TODO: Move formatting to seperate fn
        let mut formatted_data = "".to_owned();
        for item in frame.data() {
            formatted_data = format!("{}{:02X?} ", formatted_data, item);
        }
        let buffer: String = format!(
            "{0:<3} {1:<30} {2:<8} {3:<10} {4:<25}\n",
            note,
            Utc::now().naive_local().format("[%a %b %e %H:%M:%S %Y]:"),
            channel,
            format!("0x{:03X?}", frame.id()),
            formatted_data
        );

        let mut file = OpenOptions::new().append(true).create(true).open(logfile)?;
        file.write_all(buffer.as_bytes())?;
        Ok(())
    }

    match socket.read_frame() {
        Ok(frame) => {
            write(tx_frame, channel, logfile, "TX")?;
            write(frame, channel, logfile, "RX")
        }
        _ => Ok(()),
    }
}
/// Listen for a message on the can bus and write that message
/// out to a provided log file
/// Assumes that socket read timeout has already been set appropriately
Listen for a message on the can bus and write that message
out to a provided log file
Assumes that socket read timeout has already been set appropriately
Listen for a message on the can bus and write that message out to a provided log file Assumes that socket read timeout has already been set appropriately
pub fn listen(
    socket: &CANSocket,
    channel: &str,
    logfile: &Path,
    tx_frame: CANFrame,
) -> std::io::Result<()> {
    fn write(frame: CANFrame, channel: &str, logfile: &Path, note: &str) -> std::io::Result<()> {
        // TODO: Move formatting to seperate fn
        let mut formatted_data = "".to_owned();
        for item in frame.data() {
            formatted_data = format!("{}{:02X?} ", formatted_data, item);
        }
        let buffer: String = format!(
            "{0:<3} {1:<30} {2:<8} {3:<10} {4:<25}\n",
            note,
            Utc::now().naive_local().format("[%a %b %e %H:%M:%S %Y]:"),
            channel,
            format!("0x{:03X?}", frame.id()),
            formatted_data
        );
        let mut file = OpenOptions::new().append(true).create(true).open(logfile)?;
        file.write_all(buffer.as_bytes())?;
        Ok(())
    }
    match socket.read_frame() {
        Ok(frame) => {
            write(tx_frame, channel, logfile, "TX")?;
            write(frame, channel, logfile, "RX")
        }
        _ => Ok(()),
    }
}
pub fn listen ( socket : & CANSocket , channel : & str , logfile : & Path , tx_frame : CANFrame , ) -> std :: io :: Result < ( ) > { fn write ( frame : CANFrame , channel : & str , logfile : & Path , note : & str ) -> std :: io :: Result < ( ) > { // TODO: Move formatting to seperate fn let mut formatted_data = " " . to_owned ( ) ; for item in frame . data ( ) { formatted_data = format ! ( " " formatted_data item ) ; } let buffer : String = format ! ( " \n " note Utc now ( ) naive_local ( ) format ( " " ) channel format ( " " frame id ( ) ) formatted_data ) ; let mut file = OpenOptions :: new ( ) . append ( true ) . create ( true ) . open ( logfile ) ? ; file . write_all ( buffer . as_bytes ( ) ) ? ; Ok ( ( ) ) } match socket . read_frame ( ) { Ok ( frame ) => { write ( tx_frame , channel , logfile , " " ) ? ; write ( frame , channel , logfile , " " ) } _ => Ok ( ( ) ) , } }
Listen for a message on the can bus and write that message
out to a provided log file
Assumes that socket read timeout has already been set appropriately
Listen for a message on the can bus and write that message out to a provided log file Assumes that socket read timeout has already been set appropriately
socket
&CANSocket
channel
&str
logfile
&Path
tx_frame
CANFrame
socket
&CANSocket
channel
&str
logfile
&Path
tx_frame
CANFrame