diff --git a/.gitignore b/.gitignore index 3357967..6875498 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ /venv /frames **/*.png -thermaldecoder/target +target diff --git a/thermaldecoder/Cargo.lock b/Cargo.lock similarity index 100% rename from thermaldecoder/Cargo.lock rename to Cargo.lock diff --git a/thermaldecoder/Cargo.toml b/Cargo.toml similarity index 93% rename from thermaldecoder/Cargo.toml rename to Cargo.toml index 1a2d8ee..0650980 100644 --- a/thermaldecoder/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,7 @@ edition = "2021" [lib] name = "thermaldecoder" -crate-type = ["cdylib"] +crate-type = ["rlib", "cdylib"] [dependencies] anyhow = "1.0.77" diff --git a/examples/main.rs b/examples/main.rs new file mode 100644 index 0000000..5df7887 --- /dev/null +++ b/examples/main.rs @@ -0,0 +1,11 @@ +use thermaldecoder::decode_raw; + +use std::env; + +fn main() -> anyhow::Result<()> { + let mut arg = env::args(); + let filename = arg.next().ok_or(anyhow::anyhow!("unexpected"))?; + let frames = arg.next().unwrap_or("frames".into()); + decode_raw(&filename, &frames)?; + Ok(()) +} diff --git a/thermaldecoder/src/lib.rs b/src/lib.rs similarity index 62% rename from thermaldecoder/src/lib.rs rename to src/lib.rs index de0a407..f6990b4 100644 --- a/thermaldecoder/src/lib.rs +++ b/src/lib.rs @@ -10,58 +10,72 @@ use std::path::Path; use pcap_parser::traits::PcapReaderIterator; use pcap_parser::*; -fn get_packets_without_udp_header(filename: &str) -> anyhow::Result<(Vec>, usize, usize)> { - let file = File::open(filename)?; - let mut cap = PcapNGReader::new(65535, file)?; - let mut i = 0; - let mut size = 0; - let mut data = vec![]; - loop { - match cap.next() { - Ok((offset, packet)) => { - i += 1; - size += offset; - match packet { - PcapBlockOwned::Legacy(_block) => { - println!("dunno"); - } - PcapBlockOwned::LegacyHeader(_block) => { - println!("dunnoheader"); - } - PcapBlockOwned::NG(block) => { - if block.is_data_block() { - match block { - Block::EnhancedPacket(ref epb) => { - if epb.origlen == 6972 { - // remove udp header - data.push(epb.data[0x2a..].to_vec()); +struct PacketsIterator { + cap: PcapNGReader, + i: usize, + size: usize, +} + +impl Iterator for PacketsIterator { + type Item = Vec; + + fn next(&mut self) -> Option { + let mut ret = None; + while ret.is_none() { + match self.cap.next() { + Ok((offset, packet)) => { + self.i += 1; + self.size += offset; + match packet { + PcapBlockOwned::Legacy(_block) => { + println!("dunno"); + } + PcapBlockOwned::LegacyHeader(_block) => { + println!("dunnoheader"); + } + PcapBlockOwned::NG(block) => { + if block.is_data_block() { + match block { + Block::EnhancedPacket(ref epb) => { + if epb.origlen == 6972 { + // remove udp header + ret = Some(epb.data[0x2a..].to_vec()); + } } - } - Block::SimplePacket(ref ep) => { - if ep.origlen == 6972 { - println!("found one regular"); + Block::SimplePacket(ref ep) => { + if ep.origlen == 6972 { + println!("found one regular"); + } + } + _ => { + println!("unsupported packet"); } - } - _ => { - println!("unsupported packet"); } } } } + self.cap.consume(offset) + } + Err(PcapError::Eof) => break, + Err(PcapError::Incomplete) => { + self.cap.refill().unwrap(); + } + Err(_) => { + println!("unexpected error"); + break; } - cap.consume(offset) - } - Err(PcapError::Eof) => break, - Err(PcapError::Incomplete) => { - cap.refill().unwrap(); - } - Err(_) => { - println!("unexpected error"); - break; } } + ret + } +} + +impl PacketsIterator { + fn new(filename: &str) -> anyhow::Result { + let file = File::open(filename)?; + let cap = PcapNGReader::new(65535, file)?; + Ok(PacketsIterator { cap, i: 0, size: 0 }) } - Ok((data, i, size)) } #[repr(C, packed)] @@ -111,10 +125,31 @@ fn decode(filename: &str, frames_root: &str) -> PyResult>> { Ok(res.into()) } -fn decode_raw(filename: &str, frames_root: &str) -> anyhow::Result>> { - let (data, i, size) = get_packets_without_udp_header(filename)?; +struct Frame { + header: Header, + pixels: Vec, +} - println!("found {} packets, saved {}, {} size", i, data.len(), size); +struct Decoder {} + +impl Decoder { + fn new(filename: &str) {} +} + +impl Iterator for Decoder { + type Item = Frame; + + fn next(&mut self) -> Option { + todo!() + } +} + +pub fn decode_raw(filename: &str, frames_root: &str) -> anyhow::Result>> { + let packiter = PacketsIterator::new(filename)?; + let data = packiter.collect::>(); + // let (i, size) = (packiter.i, packiter.size); + + //println!("found {} packets, saved {}, {} size", i, data.len(), size); let dump_filename = format!("{}/dump.bin", filename); let mut dump = File::create_new(dump_filename); let mut frames = vec![]; diff --git a/thermaldecoder/src/main.rs b/thermaldecoder/src/main.rs deleted file mode 100644 index 196019e..0000000 --- a/thermaldecoder/src/main.rs +++ /dev/null @@ -1,4 +0,0 @@ -//fn main() -> anyhow::Result<()> { -// decode_raw("dump.bin")?; -// Ok(()) -//}