Struct DeviceDiscovery

Source
pub struct DeviceDiscovery { /* private fields */ }
Expand description

Allows discovery of VBus-over-TCP devices in a local network.

All VBus-over-TCP devices listen for UDPv4 broadcast messages on port 7053. If such a message with the correct payload is received, the device sends a unicast message back to the sender of the broadcast to identify itself.

The DeviceDiscovery type allows to send such broadcasts and collect all associated replies.

Implementations§

Source§

impl DeviceDiscovery

Source

pub fn new() -> DeviceDiscovery

Create a new DeviceDiscovery instance using default values.

§Examples
use async_resol_vbus::DeviceDiscovery;

let discovery = DeviceDiscovery::new();
let addresses = discovery.discover_device_addresses().await?;
Examples found in repository?
examples/discover.rs (line 15)
13fn main() -> Result<()> {
14    async_std::task::block_on(async {
15        let mut discovery = DeviceDiscovery::new();
16        // discovery.set_broadcast_addr("192.168.15.255:7053".parse().unwrap());
17        discovery.set_fetch_port(3000);
18
19        let mut known_devices = Vec::<DeviceInformation>::new();
20        loop {
21            println!("---- Discovering... ----");
22
23            let mut found_devices = discovery.discover_devices().await?;
24            found_devices.sort_by(|l, r| compare_devices(l, r));
25
26            known_devices.sort_by(|l, r| compare_devices(l, r));
27
28            for found_device in found_devices.iter() {
29                let is_known_device = known_devices
30                    .iter()
31                    .any(|known_device| known_device.address == found_device.address);
32
33                if !is_known_device {
34                    let name = match &found_device.name {
35                        Some(name) => name.as_str(),
36                        None => "???",
37                    };
38
39                    println!("FOUND: {} {}", found_device.address, name);
40                    known_devices.push(found_device.clone());
41                }
42            }
43
44            let mut lost_device_addresses = Vec::new();
45
46            for known_device in known_devices.iter() {
47                let is_found_device = found_devices
48                    .iter()
49                    .any(|found_device| known_device.address == found_device.address);
50
51                if !is_found_device {
52                    let name = match &known_device.name {
53                        Some(name) => name.as_str(),
54                        None => "???",
55                    };
56
57                    println!("LOST:  {} {}", known_device.address, name);
58                    lost_device_addresses.push(known_device.address);
59                }
60            }
61
62            for lost_device_address in lost_device_addresses {
63                let pos = known_devices
64                    .iter()
65                    .position(|known_device| known_device.address == lost_device_address);
66
67                if let Some(idx) = pos {
68                    known_devices.remove(idx);
69                }
70            }
71
72            async_std::task::sleep(std::time::Duration::from_secs(10)).await;
73        }
74    })
75}
Source

pub fn set_broadcast_addr(&mut self, addr: SocketAddr)

Set the broadcast address.

Source

pub fn set_rounds(&mut self, rounds: u8)

Set the number of discovery rounds.

Source

pub fn set_broadcast_timeout(&mut self, timeout: Duration)

Set the timeout used to wait for replies after each round’s broadcast.

Source

pub fn set_fetch_port(&mut self, port: u16)

Set the port number used for fetching the device information.

Examples found in repository?
examples/discover.rs (line 17)
13fn main() -> Result<()> {
14    async_std::task::block_on(async {
15        let mut discovery = DeviceDiscovery::new();
16        // discovery.set_broadcast_addr("192.168.15.255:7053".parse().unwrap());
17        discovery.set_fetch_port(3000);
18
19        let mut known_devices = Vec::<DeviceInformation>::new();
20        loop {
21            println!("---- Discovering... ----");
22
23            let mut found_devices = discovery.discover_devices().await?;
24            found_devices.sort_by(|l, r| compare_devices(l, r));
25
26            known_devices.sort_by(|l, r| compare_devices(l, r));
27
28            for found_device in found_devices.iter() {
29                let is_known_device = known_devices
30                    .iter()
31                    .any(|known_device| known_device.address == found_device.address);
32
33                if !is_known_device {
34                    let name = match &found_device.name {
35                        Some(name) => name.as_str(),
36                        None => "???",
37                    };
38
39                    println!("FOUND: {} {}", found_device.address, name);
40                    known_devices.push(found_device.clone());
41                }
42            }
43
44            let mut lost_device_addresses = Vec::new();
45
46            for known_device in known_devices.iter() {
47                let is_found_device = found_devices
48                    .iter()
49                    .any(|found_device| known_device.address == found_device.address);
50
51                if !is_found_device {
52                    let name = match &known_device.name {
53                        Some(name) => name.as_str(),
54                        None => "???",
55                    };
56
57                    println!("LOST:  {} {}", known_device.address, name);
58                    lost_device_addresses.push(known_device.address);
59                }
60            }
61
62            for lost_device_address in lost_device_addresses {
63                let pos = known_devices
64                    .iter()
65                    .position(|known_device| known_device.address == lost_device_address);
66
67                if let Some(idx) = pos {
68                    known_devices.remove(idx);
69                }
70            }
71
72            async_std::task::sleep(std::time::Duration::from_secs(10)).await;
73        }
74    })
75}
Source

pub fn set_fetch_timeout(&mut self, timeout: Duration)

Set the timeout used for fetching the device information.

Source

pub async fn discover_devices(&self) -> Result<Vec<DeviceInformation>>

Discover all VBus-over-TCP devices and return their device information.

§Examples
use async_resol_vbus::DeviceDiscovery;

let discovery = DeviceDiscovery::new();
let devices = discovery.discover_devices().await?;
Examples found in repository?
examples/discover.rs (line 23)
13fn main() -> Result<()> {
14    async_std::task::block_on(async {
15        let mut discovery = DeviceDiscovery::new();
16        // discovery.set_broadcast_addr("192.168.15.255:7053".parse().unwrap());
17        discovery.set_fetch_port(3000);
18
19        let mut known_devices = Vec::<DeviceInformation>::new();
20        loop {
21            println!("---- Discovering... ----");
22
23            let mut found_devices = discovery.discover_devices().await?;
24            found_devices.sort_by(|l, r| compare_devices(l, r));
25
26            known_devices.sort_by(|l, r| compare_devices(l, r));
27
28            for found_device in found_devices.iter() {
29                let is_known_device = known_devices
30                    .iter()
31                    .any(|known_device| known_device.address == found_device.address);
32
33                if !is_known_device {
34                    let name = match &found_device.name {
35                        Some(name) => name.as_str(),
36                        None => "???",
37                    };
38
39                    println!("FOUND: {} {}", found_device.address, name);
40                    known_devices.push(found_device.clone());
41                }
42            }
43
44            let mut lost_device_addresses = Vec::new();
45
46            for known_device in known_devices.iter() {
47                let is_found_device = found_devices
48                    .iter()
49                    .any(|found_device| known_device.address == found_device.address);
50
51                if !is_found_device {
52                    let name = match &known_device.name {
53                        Some(name) => name.as_str(),
54                        None => "???",
55                    };
56
57                    println!("LOST:  {} {}", known_device.address, name);
58                    lost_device_addresses.push(known_device.address);
59                }
60            }
61
62            for lost_device_address in lost_device_addresses {
63                let pos = known_devices
64                    .iter()
65                    .position(|known_device| known_device.address == lost_device_address);
66
67                if let Some(idx) = pos {
68                    known_devices.remove(idx);
69                }
70            }
71
72            async_std::task::sleep(std::time::Duration::from_secs(10)).await;
73        }
74    })
75}
Source

pub async fn discover_device_addresses(&self) -> Result<Vec<SocketAddr>>

Discover all VBus-over-TCP devices and return their addresses.

§Examples
use async_resol_vbus::DeviceDiscovery;

let discovery = DeviceDiscovery::new();
let addresses = discovery.discover_device_addresses().await?;

Trait Implementations§

Source§

impl Debug for DeviceDiscovery

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for DeviceDiscovery

Source§

fn default() -> DeviceDiscovery

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.