Compare commits

...

6 Commits

6 changed files with 214 additions and 55 deletions

123
Cargo.lock generated
View File

@ -469,6 +469,29 @@ dependencies = [
"rustc-demangle", "rustc-demangle",
] ]
[[package]]
name = "bindgen"
version = "0.65.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cfdf7b466f9a4903edc73f95d6d2bcd5baf8ae620638762244d3f60143643cc5"
dependencies = [
"bitflags 1.3.2",
"cexpr",
"clang-sys",
"lazy_static",
"lazycell",
"log",
"peeking_take_while",
"prettyplease",
"proc-macro2",
"quote",
"regex",
"rustc-hash",
"shlex",
"syn 2.0.48",
"which",
]
[[package]] [[package]]
name = "bit-set" name = "bit-set"
version = "0.5.3" version = "0.5.3"
@ -645,6 +668,15 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d43a04d8753f35258c91f8ec639f792891f748a1edbd759cf1dcea3382ad83c" checksum = "6d43a04d8753f35258c91f8ec639f792891f748a1edbd759cf1dcea3382ad83c"
[[package]]
name = "cexpr"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6fac387a98bb7c37292057cffc56d62ecb629900026402633ae9160df93a8766"
dependencies = [
"nom",
]
[[package]] [[package]]
name = "cfg-if" name = "cfg-if"
version = "1.0.0" version = "1.0.0"
@ -672,6 +704,17 @@ version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0fc239e0f6cb375d2402d48afb92f76f5404fd1df208a41930ec81eda078bea" checksum = "b0fc239e0f6cb375d2402d48afb92f76f5404fd1df208a41930ec81eda078bea"
[[package]]
name = "clang-sys"
version = "1.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "67523a3b4be3ce1989d607a828d036249522dd9c1c8de7f4dd2dae43a37369d1"
dependencies = [
"glob",
"libc",
"libloading 0.8.1",
]
[[package]] [[package]]
name = "clipboard-win" name = "clipboard-win"
version = "5.1.0" version = "5.1.0"
@ -965,6 +1008,12 @@ dependencies = [
"litrs", "litrs",
] ]
[[package]]
name = "dotenv"
version = "0.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f"
[[package]] [[package]]
name = "downcast-rs" name = "downcast-rs"
version = "1.2.0" version = "1.2.0"
@ -1078,6 +1127,12 @@ dependencies = [
"winit", "winit",
] ]
[[package]]
name = "either"
version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "11157ac094ffbdde99aa67b23417ebdd801842852b500e395a45a9c0aac03e4a"
[[package]] [[package]]
name = "emath" name = "emath"
version = "0.26.2" version = "0.26.2"
@ -1467,6 +1522,12 @@ dependencies = [
"xml-rs", "xml-rs",
] ]
[[package]]
name = "glob"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b"
[[package]] [[package]]
name = "glow" name = "glow"
version = "0.13.1" version = "0.13.1"
@ -1800,6 +1861,12 @@ version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "lazycell"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55"
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.153" version = "0.2.153"
@ -2283,6 +2350,12 @@ dependencies = [
"rusticata-macros", "rusticata-macros",
] ]
[[package]]
name = "peeking_take_while"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099"
[[package]] [[package]]
name = "percent-encoding" name = "percent-encoding"
version = "2.3.1" version = "2.3.1"
@ -2379,6 +2452,16 @@ version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e8cf8e6a8aa66ce33f63993ffc4ea4271eb5b0530a9002db8455ea6050c77bfa" checksum = "e8cf8e6a8aa66ce33f63993ffc4ea4271eb5b0530a9002db8455ea6050c77bfa"
[[package]]
name = "prettyplease"
version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a41cf62165e97c7f814d2221421dbb9afcbcdb0a88068e5ea206e19951c2cbb5"
dependencies = [
"proc-macro2",
"syn 2.0.48",
]
[[package]] [[package]]
name = "proc-macro-crate" name = "proc-macro-crate"
version = "1.3.1" version = "1.3.1"
@ -2711,6 +2794,12 @@ dependencies = [
"digest", "digest",
] ]
[[package]]
name = "shlex"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
[[package]] [[package]]
name = "signal-hook-registry" name = "signal-hook-registry"
version = "1.4.1" version = "1.4.1"
@ -2892,6 +2981,7 @@ dependencies = [
"anyhow", "anyhow",
"crossbeam", "crossbeam",
"crossbeam-channel", "crossbeam-channel",
"dotenv",
"eframe", "eframe",
"egui", "egui",
"indicatif", "indicatif",
@ -2900,6 +2990,7 @@ dependencies = [
"png", "png",
"pyo3", "pyo3",
"serde", "serde",
"v4l",
] ]
[[package]] [[package]]
@ -3136,6 +3227,26 @@ dependencies = [
"percent-encoding", "percent-encoding",
] ]
[[package]]
name = "v4l"
version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d8fbfea44a46799d62c55323f3c55d06df722fbe577851d848d328a1041c3403"
dependencies = [
"bitflags 1.3.2",
"libc",
"v4l2-sys-mit",
]
[[package]]
name = "v4l2-sys-mit"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6779878362b9bacadc7893eac76abe69612e8837ef746573c4a5239daf11990b"
dependencies = [
"bindgen",
]
[[package]] [[package]]
name = "version_check" name = "version_check"
version = "0.9.4" version = "0.9.4"
@ -3477,6 +3588,18 @@ dependencies = [
"web-sys", "web-sys",
] ]
[[package]]
name = "which"
version = "4.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "87ba24419a2078cd2b0f2ede2691b6c66d8e47836da3b6db8265ebad47afbfc7"
dependencies = [
"either",
"home",
"once_cell",
"rustix 0.38.31",
]
[[package]] [[package]]
name = "widestring" name = "widestring"
version = "1.0.2" version = "1.0.2"

View File

@ -13,6 +13,7 @@ crate-type = ["rlib", "cdylib"]
anyhow = "1.0.77" anyhow = "1.0.77"
crossbeam = "0.8.4" crossbeam = "0.8.4"
crossbeam-channel = "0.5.11" crossbeam-channel = "0.5.11"
dotenv = "0.15.0"
eframe = "0.26.2" eframe = "0.26.2"
egui = "0.26.2" egui = "0.26.2"
indicatif = "0.17.7" indicatif = "0.17.7"
@ -21,3 +22,4 @@ pcap-parser = { version = "0.14.1", features = ["data"] }
png = "0.17.10" png = "0.17.10"
pyo3 = { version = "0.20.0", "features" = ["extension-module"] } pyo3 = { version = "0.20.0", "features" = ["extension-module"] }
serde = { version = "1.0.193", features = ["derive", "serde_derive", "alloc"] } serde = { version = "1.0.193", features = ["derive", "serde_derive", "alloc"] }
v4l = { version = "0.14.0", features = ["v4l2"], default-features = false }

View File

@ -1,47 +1,45 @@
use indicatif::ProgressBar; use dotenv::dotenv;
use std::fs::File;
use std::io::Write; use std::io::Write;
use thermaldecoder::{Frame, Header, HDR_SIZE}; use thermaldecoder::{Header, HDR_SIZE};
use v4l::video::Output;
// 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<()> { fn main() -> anyhow::Result<()> {
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"),
};
// get the default Device // get the default Device
let device = pcap::Device::lookup()
.expect("device lookup failed")
.expect("no device available");
println!("Using device {}", device.name); println!("Using device {}", device.name);
let output = std::env::args()
.nth(1)
.expect("required output file argument");
println!("Using output v4l2loopback device {}", output);
const WIDTH: usize = 288;
const HEIGHT: usize = 384;
let fourcc_repr = [
b'Y', // | 0b10000000
b'1', b'6',
b' ', // Note: not using b' ' | 0x80, (V4L2_PIX_FMT_Y16_BE)
// because VID_S_FMT ioctl returns EINVAL, so just swap the bytes here
];
let fourcc = v4l::format::FourCC { repr: fourcc_repr };
let mut out = v4l::Device::with_path(output)?;
// To find the fourcc code, use v4l2-ctl --list-formats-out /dev/video0
// (or read the source :)
let format = v4l::Format::new(WIDTH as u32, HEIGHT as u32, fourcc);
Output::set_format(&out, &format)?;
// Setup Capture // Setup Capture
let mut cap = pcap::Capture::from_device(device) let mut cap = pcap::Capture::from_device(device)
@ -51,12 +49,13 @@ fn main() -> anyhow::Result<()> {
.unwrap(); .unwrap();
// get a packet and print its bytes // get a packet and print its bytes
let mut parts = vec![]; const PACKET_LEN: usize = 6972;
let pb = ProgressBar::new(1024); const FRAME_LEN: usize = WIDTH * HEIGHT * 2;
let mut out = File::create("output.raw")?; let mut frame = [0u8; FRAME_LEN];
let mut len = 0;
while let Ok(p) = cap.next_packet() { while let Ok(p) = cap.next_packet() {
let data = p.data; let data = p.data;
if data.len() != 6972 { if data.len() != PACKET_LEN {
continue; continue;
} }
let data = &data[0x2a..]; let data = &data[0x2a..];
@ -65,18 +64,24 @@ fn main() -> anyhow::Result<()> {
Err(_) => continue, Err(_) => continue,
}; };
let data = &data[HDR_SIZE..]; let data = &data[HDR_SIZE..];
if header.part == 0 && parts.len() > 0 { if (header.part == 0 && len > 0)
let frame = Frame { // do not write out of bounds - would panic, instead just skip
header, || (data.len() + len > FRAME_LEN)
raw: parts.concat(), {
}; if len == FRAME_LEN {
if frame.raw.len() == 384 * 288 * 2 { // swap the bytes, we are using LE, not BE, 16 bit grayscale
out.write_all(&frame.raw)?; // possibly limitation of current v4l2loopback or v4l rust wrapper or libv4l2
pb.inc(1); let mut swapped = [0u8; FRAME_LEN];
for i in 0..FRAME_LEN / 2 {
swapped[i * 2] = frame[i * 2 + 1];
swapped[i * 2 + 1] = frame[i * 2];
}
out.write_all(&swapped[..])?;
} }
parts.clear(); len = 0;
} }
parts.push(data.to_vec()); frame[len..len + data.len()].copy_from_slice(data);
len += data.len();
} }
Ok(()) Ok(())
} }

View File

@ -4,6 +4,10 @@
import base64 import base64
from scapy.all import * from scapy.all import *
from dotenv import load_dotenv
import os
load_dotenv()
# Base64 encoded packet data # Base64 encoded packet data
encoded_packet = "////////AAFsWfAKCABFAAA4KB0AAIARkEfAqAABwKgA/x+bH5wA2QAAASABgBtAACAAAAAAAAAADwAAAAEAAAEAACArAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP//////////////////////////////////////////AAAAAAAAAAIBAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" encoded_packet = "////////AAFsWfAKCABFAAA4KB0AAIARkEfAqAABwKgA/x+bH5wA2QAAASABgBtAACAAAAAAAAAADwAAAAEAAAEAACArAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP//////////////////////////////////////////AAAAAAAAAAIBAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
@ -16,5 +20,8 @@ decoded_packet = base64.b64decode(encoded_packet)
packet = Ether(decoded_packet) packet = Ether(decoded_packet)
#print(packet.show(dump=True)) #print(packet.show(dump=True))
iface = os.environ.get('THERMALCAM_IFACE', 'enp1s0f0')
print(f'using interface {iface}')
# (packet) # (packet)
sendp(packet, iface="enp1s0f0") sendp(packet, iface=iface)

View File

@ -1,3 +1,13 @@
#!/bin/bash #!/bin/bash
cd $(dirname $0) cd $(dirname $0)
sudo ./venv/bin/python ./decode.py --live
set -e
# Python works but stutters
#sudo ./venv/bin/python ./decode.py --live
cargo build --release --example live
TARGET=./target/release/examples/live
# setcap does not work yet (EPERM on socket AF_PACKET)
# sudo setcap cap_net_raw,cap_net_admin=eip $TARGET
#sudo strace -f -o live.strace $TARGET /dev/video0
sudo $TARGET /dev/video0

12
start.sh Executable file
View File

@ -0,0 +1,12 @@
#!/bin/bash
. .env
IFACE=$THERMALCAM_IFACE
echo "using iface $IFACE"
echo "checking mtu"
mtu=$(ip link show ${IFACE} | grep -o 'mtu [0-9]*' | gawk '{print $2}')
echo "mtu = $mtu"
if [ $mtu -lt 9000 ]; then
echo "setting mtu to 9000"
sudo ip link set $IFACE mtu 9000
fi
sudo venv/bin/python ./replay.py