131 lines
4 KiB
Rust
131 lines
4 KiB
Rust
|
use std::{
|
||
|
collections::HashMap,
|
||
|
net::{IpAddr, Ipv4Addr, SocketAddr, UdpSocket},
|
||
|
num::ParseIntError,
|
||
|
time::Duration,
|
||
|
};
|
||
|
|
||
|
use anyhow::Result;
|
||
|
|
||
|
pub struct Startup {}
|
||
|
|
||
|
impl Startup {
|
||
|
const BROADCAST_MSG: &'static [u8] = &[
|
||
|
0x5a, 0x5a, 0x01, 0x11, 0x48, 0x00, 0x92, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0x75, 0xbd, 0x6b, 0x3e,
|
||
|
0x4f, 0x8b, 0x76, 0x2e, 0x84, 0x9c, 0x6e, 0x57, 0x8d, 0x65, 0x90, 0x03, 0x6e, 0x9d, 0x43,
|
||
|
0x42, 0xa5, 0x0f, 0x1f, 0x56, 0x9e, 0xb8, 0xec, 0x91, 0x8e, 0x92, 0xe5,
|
||
|
];
|
||
|
|
||
|
const DEVICE_INFO_MSG: &'static [u8] = &[
|
||
|
0x5a, 0x5a, 0x15, 0x00, 0x00, 0x38, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x27, 0x33,
|
||
|
0x05, 0x13, 0x06, 0x14, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xe8, 0x00, 0x00,
|
||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xca, 0x8d, 0x9b, 0xf9, 0xa0,
|
||
|
0x30, 0x1a, 0xe3, 0xb7, 0xe4, 0x2d, 0x53, 0x49, 0x47, 0x62, 0xbe,
|
||
|
];
|
||
|
|
||
|
const NUM_RETRIES: u8 = 5;
|
||
|
|
||
|
pub fn discover() -> Result<Self> {
|
||
|
let socket = UdpSocket::bind("0.0.0.0:0")?;
|
||
|
socket.set_broadcast(true)?;
|
||
|
socket.set_read_timeout(Some(Duration::from_secs(2)))?;
|
||
|
|
||
|
let mut devices = HashMap::new();
|
||
|
|
||
|
for addr in Self::broadcast_addresses() {
|
||
|
for _ in 0..Self::NUM_RETRIES {
|
||
|
socket.send_to(Self::BROADCAST_MSG, (addr, 6445))?;
|
||
|
socket.send_to(Self::BROADCAST_MSG, (addr, 20086))?;
|
||
|
|
||
|
let mut buffer = [0; 1024];
|
||
|
if let Ok((bytes_read, addr)) = socket.recv_from(&mut buffer) {
|
||
|
let mut bytes = buffer[0..bytes_read].to_vec();
|
||
|
|
||
|
let protocol = if bytes[..2] == Self::hex("5a5a")? {
|
||
|
2
|
||
|
} else if bytes[..2] == Self::hex("8370")? {
|
||
|
if bytes[8..10] == Self::hex("5a5a")? {
|
||
|
bytes = bytes[8..(bytes.len() - 16)].to_vec();
|
||
|
}
|
||
|
|
||
|
3
|
||
|
} else {
|
||
|
continue;
|
||
|
};
|
||
|
|
||
|
let mut tmp: Vec<u8> = bytes[20..26].to_vec();
|
||
|
tmp.push(0);
|
||
|
tmp.push(0);
|
||
|
|
||
|
let device_id = u64::from_le_bytes(tmp.try_into().unwrap());
|
||
|
|
||
|
if !devices.contains_key(&device_id) {
|
||
|
devices.insert(device_id, (addr, bytes.to_vec(), protocol));
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
println!("{devices:#?}");
|
||
|
|
||
|
Ok(Self {})
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl Startup {
|
||
|
fn broadcast_addresses() -> Vec<IpAddr> {
|
||
|
use if_addrs::*;
|
||
|
|
||
|
let mut addresses = Vec::new();
|
||
|
|
||
|
if let Some(interfaces) = get_if_addrs().ok() {
|
||
|
for interface in interfaces {
|
||
|
match interface.addr {
|
||
|
IfAddr::V4(v4) => {
|
||
|
let ip = v4.ip;
|
||
|
let octets = ip.octets();
|
||
|
|
||
|
if ip.is_private() {
|
||
|
addresses.push(IpAddr::V4(Ipv4Addr::new(
|
||
|
octets[0],
|
||
|
octets[1],
|
||
|
octets[2],
|
||
|
u8::MAX,
|
||
|
)))
|
||
|
}
|
||
|
}
|
||
|
IfAddr::V6(_v6) => {
|
||
|
// TODO
|
||
|
// addresses.push(IpAddr::V6(v6.ip));
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
addresses
|
||
|
}
|
||
|
|
||
|
fn hex(s: &str) -> Result<Vec<u8>, ParseIntError> {
|
||
|
(0..s.len())
|
||
|
.step_by(2)
|
||
|
.map(|i| u8::from_str_radix(&s[i..i + 2], 16))
|
||
|
.collect()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#[cfg(test)]
|
||
|
mod test {
|
||
|
use anyhow::Result;
|
||
|
|
||
|
use super::Startup;
|
||
|
|
||
|
#[test]
|
||
|
fn discover() -> Result<()> {
|
||
|
Startup::discover()?;
|
||
|
|
||
|
Ok(())
|
||
|
}
|
||
|
}
|