move rust to root, make a separate main example, start using iterator
This commit is contained in:
parent
44849f5b66
commit
9b2f8d9377
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -1,4 +1,4 @@
|
||||||
/venv
|
/venv
|
||||||
/frames
|
/frames
|
||||||
**/*.png
|
**/*.png
|
||||||
thermaldecoder/target
|
target
|
||||||
|
|
0
thermaldecoder/Cargo.lock → Cargo.lock
generated
0
thermaldecoder/Cargo.lock → Cargo.lock
generated
|
@ -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
11
examples/main.rs
Normal 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(())
|
||||||
|
}
|
|
@ -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![];
|
|
@ -1,4 +0,0 @@
|
||||||
//fn main() -> anyhow::Result<()> {
|
|
||||||
// decode_raw("dump.bin")?;
|
|
||||||
// Ok(())
|
|
||||||
//}
|
|
Loading…
Reference in New Issue
Block a user