diff --git a/Cargo.lock b/Cargo.lock index b2dd2db..dda3fc5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -360,6 +360,7 @@ dependencies = [ "rumqttc", "serde", "serde_json", + "socket2 0.4.10", ] [[package]] @@ -559,6 +560,16 @@ version = "0.4.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "04dc19736151f35336d325007ac991178d504a119863a2fcb3758cdb5e52c50d" +[[package]] +name = "socket2" +version = "0.4.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f7916fc008ca5542385b89a3d3ce689953c143e9304a9bf8beec1de48994c0d" +dependencies = [ + "libc", + "winapi", +] + [[package]] name = "socket2" version = "0.5.10" @@ -628,7 +639,7 @@ dependencies = [ "mio", "pin-project-lite", "slab", - "socket2", + "socket2 0.5.10", "tokio-macros", "windows-sys 0.52.0", ] @@ -678,6 +689,28 @@ version = "0.11.1+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + [[package]] name = "windows-sys" version = "0.52.0" diff --git a/Cargo.toml b/Cargo.toml index 6c5824d..64ab2e8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,3 +8,4 @@ rumqttc = "0.23" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" clap = { version = "4.4", features = ["derive"] } +socket2 = "0.4.7" diff --git a/README.md b/README.md new file mode 100644 index 0000000..784ba5b --- /dev/null +++ b/README.md @@ -0,0 +1,12 @@ +# Position client + +Example client to get Positon data from the bornhack pixelflut touch screen + +``` +./run.sh network_interface_name +``` + + +The positon data is a UDP packet of 4 bytes containing 2 usigned 16bit ints for x and y on multicast group 239.1.1.1 port 1234 + + diff --git a/run.sh b/run.sh new file mode 100755 index 0000000..36781b3 --- /dev/null +++ b/run.sh @@ -0,0 +1,3 @@ +#!/bin/sh +sudo ip addr add 239.1.1.1/32 dev $1 autojoin +cargo run diff --git a/src/main.rs b/src/main.rs index abc8476..daa035a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,8 @@ use std::net::UdpSocket; use std::env; use std::process; use std::convert::TryInto; +use socket2::{Domain, Socket, Type}; +use std::net::{Ipv4Addr, SocketAddr}; /// Unpacks a 4-byte slice into two u16 values (little-endian). fn unpack_coordinates(buffer: &[u8]) -> Option<(u16, u16)> { @@ -21,28 +23,14 @@ fn unpack_coordinates(buffer: &[u8]) -> Option<(u16, u16)> { } fn main() { - let args: Vec = env::args().collect(); - if args.len() != 3 { - eprintln!("Usage: {} ", args[0]); - eprintln!("Example: {} 127.0.0.1 12345", args[0]); - process::exit(1); - } + let socket = Socket::new(Domain::IPV4, Type::DGRAM, None).unwrap(); + socket.set_reuse_address(true).unwrap(); - let listen_addr = &args[1]; - let listen_port = &args[2]; - let bind_address = format!("{}:{}", listen_addr, listen_port); + socket.join_multicast_v4(&Ipv4Addr::new(239, 1, 1, 1), &Ipv4Addr::new(0, 0, 0, 0)).unwrap(); + socket.bind(&"0.0.0.0:1234".parse::().unwrap().into()).unwrap(); - // Bind the UDP socket to the specified address and port. - let socket = match UdpSocket::bind(&bind_address) { - Ok(s) => s, - Err(e) => { - eprintln!("Error: Could not bind to address {}: {}", bind_address, e); - process::exit(1); - } - }; - - println!("Listening for UDP packets on {}", bind_address); + let socket: UdpSocket = socket.into(); // Create a buffer to hold incoming data. 4 bytes for two u16 values. let mut buf = [0u8; 4];