DEV Community

Tsiry Sandratraina
Tsiry Sandratraina

Posted on

Introduction to UPnP Client for Rust

The Universal Plug and Play (UPnP) protocol is a set of networking protocols that allows for seamless discovery and communication between networked devices. UPnP enables devices such as smart home appliances, gaming consoles, and media players to discover each other and exchange information on a network.

This Rust crate provides a UPnP client library, making it easy for developers to build UPnP-enabled applications in Rust. With this library, developers can discover UPnP devices on the network, retrieve information about them, and communicate with them using UPnP services.

Features

  • Discover UPnP devices on the network
  • Retrieve information about UPnP devices such as device type, friendly name, manufacturer, model, and services
  • Communicate with UPnP services using control and event URLs

Getting Started

To start using the UPnP Client crate in your Rust project, add the following to your Cargo.toml file:

[dependencies]
upnp-client = "0.1"
Enter fullscreen mode Exit fullscreen mode

Example Usage

Here's a basic example that demonstrates how to use the UPnP Client library to discover UPnP devices on the network and retrieve information about them. The example will print out a JSON representation of each device found on the network, including its device type, friendly name, location, manufacturer, model, and services.

use colored_json::prelude::*;
use futures_util::StreamExt;

use crate::discovery::discover_pnp_locations;

mod discovery;
mod types;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let devices = discover_pnp_locations();
    tokio::pin!(devices);

    while let Some(device) = devices.next().await {
        let json = serde_json::to_string_pretty(&device)?;
        println!("{}", json.to_colored_json_auto()?);
    }

    Ok(())
}
Enter fullscreen mode Exit fullscreen mode

The example uses the discover_pnp_locations function from the discovery module to initiate UPnP device discovery on the network. The function returns a stream of UPnP devices, which the example loops through and prints out in a JSON format.

Conclusion

The UPnP Client library for Rust provides a convenient and easy-to-use solution for discovering and communicating with UPnP devices on a network. With its powerful features, developers can quickly build UPnP-enabled applications in Rust.
If you're interested in contributing to the upnp-client-rs project, you're more than welcome to do so! This Rust crate is open-source and hosted on GitHub at https://github.com/tsirysndr/upnp-client-rs, so feel free to check it out and make any contributions you feel would be useful. Whether you have bug fixes, improvements, or new features, all contributions are appreciated. Before submitting a pull request, please make sure to review the contribution guidelines in the repository's CONTRIBUTING.md file.

Top comments (0)