C# UPnP Universal Plug & Play Library

ParkSquare.UPnP

BUILD STATUS Continuous Integration Build Status

INSTALLATION

Install-Package ParkSquare.UPnP

DESCRIPTION

Provides comprehensive set of objects to interact with Universal Plug and Play (UPnP) networked devices and services. Supports device, service and action discovery, and basic control features.

Documentation

From Wikipedia: "Universal Plug and Play (UPnP) is a set of networking protocols that permits networked devices, such as computers, printers, Internet gateways, Wi-Fi access points and mobile devices to seamlessly discover each other's presence on the network and establish functional network services"

When writing the Sky HD & Sky Q Box API, we realized that a decent, clean, UPnP library did not exist. The ones that do exist either have zero documentation, or are tied into proprietary Windows o/s calls. Our version is entirely written in C#. UPnP uses SSDP to discover devices on the network. You can use Troy Willmot's RSSDP library for this.


    ISsdpDeviceLocator deviceLocator = new SsdpDeviceLocator("10.10.0.110");
    var foundDevices = deviceLocator.SearchAsync().Result;

This will give you the URLs of endpoints that will describe the device, any child devices, available services and actions. You pass this URL to our DeviceFactory, and that handles the necessary calls to give you a Device object.


    var device = factory.Create(foundDevices.First().DescriptionLocation);

You can then use the Device object to find information and capabilities. The following code writes all available services and actions to the console window.


    foreach (var device in devices)
    {
        Console.WriteLine(device.ToString());
        foreach (var service in device.Services)
        {
            Console.WriteLine($"\t{service.ServiceId} ({service.ServiceType})");

            foreach (var action in service.Actions)
            {
                Console.WriteLine($"\t\t{action.Name}");
            }
        }
    }

Actions may have one or more parameters. You'll be able to see what datatypes they are expecting, whether they are mandatory, and in some cases what constitutes an acceptable range. More information is available from the official spec document.

Here's an example from our Sky HD & Sky Q Box API showing how to invoke an action. The 'action' object is 'SetAVTransportURI' from the 'SkyPlay' service, essentially it is just a question of building a dictionary of argument names and desired values:


    var args = new Dictionary<string, string>
    {
        {"InstanceID", "0"},
        {"CurrentURI", "xsi://66},  // channel Id in hex
        {"CurrentURIMetaData", "NOT_IMPLEMENTED"}
    };

    action.Invoke(new ActionInvoker(), args);