move rust to root, make a separate main example, start using iterator

This commit is contained in:
Alon Levy 2023-12-31 00:19:24 +02:00
parent 44849f5b66
commit 9b2f8d9377
6 changed files with 93 additions and 51 deletions

2
.gitignore vendored
View File

@ -1,4 +1,4 @@
/venv /venv
/frames /frames
**/*.png **/*.png
thermaldecoder/target target

View File

@ -7,7 +7,7 @@ edition = "2021"
[lib] [lib]
name = "thermaldecoder" name = "thermaldecoder"
crate-type = ["cdylib"] crate-type = ["rlib", "cdylib"]
[dependencies] [dependencies]
anyhow = "1.0.77" anyhow = "1.0.77"

11
examples/main.rs Normal file
View File

@ -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(())
}

View File

@ -10,58 +10,72 @@ use std::path::Path;
use pcap_parser::traits::PcapReaderIterator; use pcap_parser::traits::PcapReaderIterator;
use pcap_parser::*; use pcap_parser::*;
fn get_packets_without_udp_header(filename: &str) -> anyhow::Result<(Vec<Vec<u8>>, usize, usize)> { struct PacketsIterator {
let file = File::open(filename)?; cap: PcapNGReader<File>,
let mut cap = PcapNGReader::new(65535, file)?; i: usize,
let mut i = 0; size: usize,
let mut size = 0; }
let mut data = vec![];
loop { impl Iterator for PacketsIterator {
match cap.next() { type Item = Vec<u8>;
Ok((offset, packet)) => {
i += 1; fn next(&mut self) -> Option<Self::Item> {
size += offset; let mut ret = None;
match packet { while ret.is_none() {
PcapBlockOwned::Legacy(_block) => { match self.cap.next() {
println!("dunno"); Ok((offset, packet)) => {
} self.i += 1;
PcapBlockOwned::LegacyHeader(_block) => { self.size += offset;
println!("dunnoheader"); match packet {
} PcapBlockOwned::Legacy(_block) => {
PcapBlockOwned::NG(block) => { println!("dunno");
if block.is_data_block() { }
match block { PcapBlockOwned::LegacyHeader(_block) => {
Block::EnhancedPacket(ref epb) => { println!("dunnoheader");
if epb.origlen == 6972 { }
// remove udp header PcapBlockOwned::NG(block) => {
data.push(epb.data[0x2a..].to_vec()); 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) => {
Block::SimplePacket(ref ep) => { if ep.origlen == 6972 {
if ep.origlen == 6972 { println!("found one regular");
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<Self> {
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)] #[repr(C, packed)]
@ -111,10 +125,31 @@ fn decode(filename: &str, frames_root: &str) -> PyResult<Vec<Vec<u16>>> {
Ok(res.into()) Ok(res.into())
} }
fn decode_raw(filename: &str, frames_root: &str) -> anyhow::Result<Vec<Vec<u16>>> { struct Frame {
let (data, i, size) = get_packets_without_udp_header(filename)?; header: Header,
pixels: Vec<u16>,
}
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<Self::Item> {
todo!()
}
}
pub fn decode_raw(filename: &str, frames_root: &str) -> anyhow::Result<Vec<Vec<u16>>> {
let packiter = PacketsIterator::new(filename)?;
let data = packiter.collect::<Vec<_>>();
// let (i, size) = (packiter.i, packiter.size);
//println!("found {} packets, saved {}, {} size", i, data.len(), size);
let dump_filename = format!("{}/dump.bin", filename); let dump_filename = format!("{}/dump.bin", filename);
let mut dump = File::create_new(dump_filename); let mut dump = File::create_new(dump_filename);
let mut frames = vec![]; let mut frames = vec![];

View File

@ -1,4 +0,0 @@
//fn main() -> anyhow::Result<()> {
// decode_raw("dump.bin")?;
// Ok(())
//}