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
/frames
**/*.png
thermaldecoder/target
target

View File

@ -7,7 +7,7 @@ edition = "2021"
[lib]
name = "thermaldecoder"
crate-type = ["cdylib"]
crate-type = ["rlib", "cdylib"]
[dependencies]
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,17 +10,22 @@ use std::path::Path;
use pcap_parser::traits::PcapReaderIterator;
use pcap_parser::*;
fn get_packets_without_udp_header(filename: &str) -> anyhow::Result<(Vec<Vec<u8>>, 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() {
struct PacketsIterator {
cap: PcapNGReader<File>,
i: usize,
size: usize,
}
impl Iterator for PacketsIterator {
type Item = Vec<u8>;
fn next(&mut self) -> Option<Self::Item> {
let mut ret = None;
while ret.is_none() {
match self.cap.next() {
Ok((offset, packet)) => {
i += 1;
size += offset;
self.i += 1;
self.size += offset;
match packet {
PcapBlockOwned::Legacy(_block) => {
println!("dunno");
@ -34,7 +39,7 @@ fn get_packets_without_udp_header(filename: &str) -> anyhow::Result<(Vec<Vec<u8>
Block::EnhancedPacket(ref epb) => {
if epb.origlen == 6972 {
// remove udp header
data.push(epb.data[0x2a..].to_vec());
ret = Some(epb.data[0x2a..].to_vec());
}
}
Block::SimplePacket(ref ep) => {
@ -49,11 +54,11 @@ fn get_packets_without_udp_header(filename: &str) -> anyhow::Result<(Vec<Vec<u8>
}
}
}
cap.consume(offset)
self.cap.consume(offset)
}
Err(PcapError::Eof) => break,
Err(PcapError::Incomplete) => {
cap.refill().unwrap();
self.cap.refill().unwrap();
}
Err(_) => {
println!("unexpected error");
@ -61,7 +66,16 @@ fn get_packets_without_udp_header(filename: &str) -> anyhow::Result<(Vec<Vec<u8>
}
}
}
Ok((data, i, size))
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 })
}
}
#[repr(C, packed)]
@ -111,10 +125,31 @@ fn decode(filename: &str, frames_root: &str) -> PyResult<Vec<Vec<u16>>> {
Ok(res.into())
}
fn decode_raw(filename: &str, frames_root: &str) -> anyhow::Result<Vec<Vec<u16>>> {
let (data, i, size) = get_packets_without_udp_header(filename)?;
struct Frame {
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 mut dump = File::create_new(dump_filename);
let mut frames = vec![];

View File

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