From 01faf4b8c6dccd9133f937e4494616943d9de624 Mon Sep 17 00:00:00 2001 From: Sam Willcocks Date: Thu, 21 Mar 2019 00:59:32 +0000 Subject: [PATCH] Add initial atem testing --- Cargo.lock | 1 + Cargo.toml | 3 ++- src/bin/atem.rs | 57 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 src/bin/atem.rs diff --git a/Cargo.lock b/Cargo.lock index e576ec4..79643fa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -789,6 +789,7 @@ dependencies = [ "actix 0.7.9 (registry+https://github.com/rust-lang/crates.io-index)", "actix-net 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", "actix-web 0.7.18 (registry+https://github.com/rust-lang/crates.io-index)", + "byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "crossbeam-channel 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", "ctrlc 3.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index 1582452..f574471 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,4 +15,5 @@ edition = "2018" crossbeam-channel = "0.3.8" ctrlc = "3.1.1" serde = { version = "1.0", features = ["derive"]} - serde_json = "1.0" \ No newline at end of file + serde_json = "1.0" + byteorder = "1.3.1" \ No newline at end of file diff --git a/src/bin/atem.rs b/src/bin/atem.rs new file mode 100644 index 0000000..9803010 --- /dev/null +++ b/src/bin/atem.rs @@ -0,0 +1,57 @@ +use byteorder::{BigEndian, WriteBytesExt}; +use std::net::UdpSocket; + +enum Command { + NoCommand = 0x0, + AckRequest = 0x1, + HelloPacket = 0x2, + Resend = 0x4, + Undefined = 0x8, + Ack = 0x10, +} + +// much thanks to https://github.com/petersimonsson/libqatemcontrol/blob/master/qatemconnection.cpp +struct ATEMPacket { + raw: Vec, +} + +impl ATEMPacket { + const HEADER_SIZE: u16 = 12; + fn new(cmd: Command, uid: u16, ack_id: u16, payload: Vec) -> ATEMPacket { + let mut a = ATEMPacket { raw: vec![] }; + for word in vec![ + ((cmd as u16) << 11) | ((payload.len() as u16 * 2) + ATEMPacket::HEADER_SIZE), + uid, + ack_id, + 0, + 0, + 0, // TODO: packageid + ] + .into_iter() + .chain(payload.into_iter()) + { + a.raw.write_u16::(word).unwrap(); + } + a + } +} + +fn main() { + let mut sock = UdpSocket::bind("0.0.0.0:0").expect("Couldn't bind"); + let a = ATEMPacket::new( + Command::HelloPacket, + 0x1337, + 0x0, + vec![0x100, 0x0, 0x0, 0x0], + ); + sock.send_to(&a.raw[..], "192.168.0.240:9910") + .expect("Couldn't send data"); +} + +#[test] +fn test_newpacket() { + assert_eq!( + ATEMPacket::new(Command::Undefined, 0x1234, 0x5678, vec![0x1337, 0x0420]).raw, + vec![0x10, 0x40, 0x34, 0x12, 0x78, 0x56, 0x0, 0x0, 0x0, 0x0, 0x37, 0x13, 0x20, 0x04] + ); +}