2024-02-17 15:43:20 +02:00
|
|
|
use dotenv::dotenv;
|
2024-02-16 00:41:39 +02:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io::Write;
|
2024-02-16 00:42:01 +02:00
|
|
|
use thermaldecoder::{Frame, Header, HDR_SIZE};
|
2024-02-16 00:41:39 +02:00
|
|
|
|
|
|
|
// fn main() -> Result<(), eframe::Error> {
|
|
|
|
// env_logger::init(); // Log to stderr (if you run with `RUST_LOG=debug`).
|
|
|
|
// let options = eframe::NativeOptions {
|
|
|
|
// viewport: egui::ViewportBuilder::default().with_inner_size([600.0, 800.0]),
|
|
|
|
// ..Default::default()
|
|
|
|
// };
|
|
|
|
// let (sender, receiver) = crossbeam::channel::unbounded();
|
|
|
|
// eframe::run_native(
|
|
|
|
// "Image Viewer",
|
|
|
|
// options,
|
|
|
|
// Box::new(|cc| {
|
|
|
|
// // This gives us image support:
|
|
|
|
// egui_extras::install_image_loaders(&cc.egui_ctx);
|
|
|
|
// Box::<MyApp>::default()
|
|
|
|
// }),
|
|
|
|
// )
|
|
|
|
// }
|
|
|
|
|
|
|
|
// #[derive(Default)]
|
|
|
|
// struct MyApp {}
|
|
|
|
|
|
|
|
// impl eframe::App for MyApp {
|
|
|
|
// fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
|
|
|
|
// egui::CentralPanel::default().show(ctx, |ui| {
|
|
|
|
// egui::ScrollArea::both().show(ui, |ui| {
|
|
|
|
// ui.add(egui::Image::new().rounding(10.0));
|
|
|
|
|
|
|
|
// ui.image(egui::include_image!("ferris.svg"));
|
|
|
|
// });
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
fn main() -> anyhow::Result<()> {
|
2024-02-17 15:43:20 +02:00
|
|
|
dotenv().ok();
|
|
|
|
let device = match std::env::var("THERMALCAM_IFACE=enp1s0f0") {
|
|
|
|
Ok(d) => {
|
|
|
|
let device = pcap::Device::list()
|
|
|
|
.expect("device list failed")
|
|
|
|
.into_iter()
|
|
|
|
.find(|x| x.name == d)
|
|
|
|
.expect(&format!("could not find device {}", d));
|
|
|
|
device
|
|
|
|
}
|
|
|
|
Err(_) => pcap::Device::lookup()
|
|
|
|
.expect("device lookup failed")
|
|
|
|
.expect("no device available"),
|
|
|
|
};
|
2024-02-16 00:41:39 +02:00
|
|
|
// get the default Device
|
2024-02-17 15:43:20 +02:00
|
|
|
|
2024-02-16 00:41:39 +02:00
|
|
|
println!("Using device {}", device.name);
|
2024-02-17 15:43:20 +02:00
|
|
|
let output = std::env::args()
|
|
|
|
.nth(1)
|
|
|
|
.expect("required output file argument");
|
|
|
|
println!("Using output {}", output);
|
2024-02-16 00:41:39 +02:00
|
|
|
|
|
|
|
// Setup Capture
|
|
|
|
let mut cap = pcap::Capture::from_device(device)
|
|
|
|
.unwrap()
|
|
|
|
.immediate_mode(true)
|
|
|
|
.open()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// get a packet and print its bytes
|
2024-02-17 15:56:05 +02:00
|
|
|
const PACKET_LEN: usize = 6972;
|
|
|
|
const FRAME_LEN: usize = 288 * 384 * 2;
|
|
|
|
let mut frame = [0u8; FRAME_LEN];
|
|
|
|
let mut len = 0;
|
2024-02-17 15:43:20 +02:00
|
|
|
let mut out = File::create(&output)?;
|
2024-02-16 00:41:39 +02:00
|
|
|
while let Ok(p) = cap.next_packet() {
|
|
|
|
let data = p.data;
|
|
|
|
if data.len() != 6972 {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let data = &data[0x2a..];
|
|
|
|
let header = match Header::read(data) {
|
|
|
|
Ok(header) => header,
|
|
|
|
Err(_) => continue,
|
|
|
|
};
|
|
|
|
let data = &data[HDR_SIZE..];
|
2024-02-17 15:56:05 +02:00
|
|
|
if (header.part == 0 && len > 0)
|
|
|
|
// do not write out of bounds - would panic, instead just skip
|
|
|
|
|| (data.len() + len > FRAME_LEN)
|
|
|
|
{
|
|
|
|
if len == FRAME_LEN {
|
|
|
|
out.write_all(&frame[..])?;
|
2024-02-16 00:41:39 +02:00
|
|
|
}
|
2024-02-17 15:56:05 +02:00
|
|
|
len = 0;
|
2024-02-16 00:41:39 +02:00
|
|
|
}
|
2024-02-17 15:56:05 +02:00
|
|
|
frame[len..len + data.len()].copy_from_slice(data);
|
|
|
|
len += data.len();
|
2024-02-16 00:41:39 +02:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|