Skip to content

Commit 389e8bf

Browse files
committed
add discover_one(), and filtering helpers; refactor discovery.rs to return structured ListIdentity results
1 parent c65a5ca commit 389e8bf

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

src/client/discovery.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,23 @@ impl Discovery for DiscoveryImpl {
2222
let socket = UdpSocket::bind("0.0.0.0:0").await?;
2323
socket.set_broadcast(true)?;
2424

25-
// Send ListIdentity request
25+
// Join CIP multicast group (239.192.1.1)
26+
let _ = socket.join_multicast_v4(
27+
std::net::Ipv4Addr::new(239, 192, 1, 1),
28+
std::net::Ipv4Addr::new(0, 0, 0, 0),
29+
);
30+
31+
// Send ListIdentity request via broadcast
2632
let msg = EncapsulationHeader::new(COMMAND_LIST_IDENTITY, 0, 0).to_bytes();
2733
socket
2834
.send_to(&msg, SocketAddr::from(([255, 255, 255, 255], ENIP_PORT)))
2935
.await?;
3036

37+
// Send ListIdentity request via multicast
38+
let _ = socket
39+
.send_to(&msg, SocketAddr::from(([239, 192, 1, 1], ENIP_PORT)))
40+
.await;
41+
3142
let mut results = Vec::new();
3243
let mut buf = [0u8; 2048];
3344
let start = Instant::now();
@@ -90,6 +101,14 @@ impl Discovery for DiscoveryImpl {
90101
}
91102
}
92103

104+
impl DiscoveryImpl {
105+
/// Return the first discovered device, if any.
106+
pub async fn discover_one() -> io::Result<Option<DiscoveredIdentity>> {
107+
let all = Self::discover().await?;
108+
Ok(all.into_iter().next())
109+
}
110+
}
111+
93112
/// Parse the Identity Item (0x000C) returned by ListIdentity (0x63).
94113
///
95114
/// This is *not* the same as the Identity Object (Class 0x01).

0 commit comments

Comments
 (0)