Giter Club home page Giter Club logo

go-upnp's Introduction

UPnP Golang Library

A library in the Go language that support discovery, description, control, and eventing. The library aims for UPnP 1.1 compliance, but should work with devices supporting 1.0 and 2.0 (without Device Protection)

Discovery

The discovery module supports active unicast and multicast device discovery, as well as passive multicast notification listening. By default the Discovery() method of the library will perform multicast service discovery to the ssdp:all target, which should catch a vast majority of devices on the network. NOTE, some devices don't respond to the ssdp:all target, but do respond to a upnp:rootdevice discovery request. To customize the discovery request, modify the discovery.SearchRequest struct fields.

To passively listen for discovery notifications, run the ListenNotify() method and discovered devices will be enumerated via the channel provided in the method call.

Description

The description module supports retrieval of device and service descriptions, converting the returned SOAP XML into a struct. For device description, call the DescribeDevice() method with the full HTTP url provided by the Location field of a discovered device. A convenience method called DiscoverDeviceDescription() is provided to perform discovery for a given search target and extract the device description. CAVEAT: only the first device is used to perform the description, so you'll want to ensure the search criteria only returns a single device.

For service description, call the DescribeService() method with the full HTTP url for the SCPDURL provided by the device description. This method assumes that device discovery and device description has been performed prior to calling this method. A convenience method called DiscoverServiceDescription() is provided to perform the discovery and device description in order to get the description for the service. Provide the ServiceType as the parameter to the method to discover devices providing the service, and extracting it's description. The same caveat as DiscoverDeviceDescription() applies.

Control

The control module provides a way to send UPnP control messages to devices without having to worry (too much) about SOAP messaging. By creating a struct which inherits the control.Action interface, and marshals to valid XML needed for a UPnP action (see UPnP 1.1 spec, section 3.2.1), you can call the Invoke() method, specifying the device's full http ControlURL and the Action struct. The value returned is a []byte of the response Body inner XML so you are free to handle the data as you see fit, without all of the surrounding SOAP decoration.

Eventing

The eventing module provides a way to subscribe and get notified for state change events from devices. Unicast event subscription and multicast event listening is supported.

Unicast event subscription is handled through a SubscriptionManager, which handles the details of listening for event notifications as well as periodically refreshing the subscription, as required by the UPnP spec. Obtain a new SubscriptionManager instance by calling the NewSubscriptionManager() method, passing in the EventSubURL (obtained from the device description), and the desired subscription lifetime. Calling the EventLoop() method on the SubscriptionManager object will start the process of listenting for events, returning state variables for the event as a map. To cancel a subscription call the Unsubscribe() method on the SubscriptionManager instance.

To receive events published via multicast, call the ListenMulticastEvents() method with a channel to receive the events found. NOTE: this code has not been well tested

Building

The included Makefile should build all of the modules for this library using the default make target. Individual targets are provided for each module directory.

Contributing

The usual github model for forking the repo and creating a pull request is the preferred way to contribute to this tool. Bug fixes, enhancements, doc updates, translations are always welcomed

References

UPnP 1.1 spec

go-upnp's People

Stargazers

Becir Basic avatar  avatar

Watchers

Mike Morris avatar  avatar

go-upnp's Issues

Eventing package users cannot recover from subscription related errors

N/B: I post in part assuming that this library is abandonware (apologies if not), having last been updated in 2018, and thus to inform anyone considering using this library of potential issues they may come across if they want to use the eventing code.

In the case of a HTTP 400/412/500 response from the UPnP device to a subscription request, there is unfortunately no way for the calling code to discover this issue without using something hacky like log sniffing.

The manageSubscription method is called as a go routine, however it returns the server's error response as an error from the function. This means that error is discarded.
Any calling code would have to do something like add an io.Writer via log.SetOutput so that they could detect the SUBSCRIBE request returned HTTP message, and thus then try to deal with this. Without that, the subscription will expire as the error ends the recursive nature of the function, and the calling code will be unaware of the need to initiate a new subscription.

There is a similar situation with the notifyHandler function. Any errors that occur with this are discarded as it's also called via a go routine, and thus the calling code wouldn't have the ability to know whether the http listener was setup correctly.

Simpler fixes for both of these could be implemented via Error Groups, but obviously would involve modifying the module

There is also an issue when the subscription NOTIFY calls provided by the UPnP device to this code don't provide valid XML, these are silently discarded, and the Unmarshal error just logged out.

With the lack of header information provided to the calling code, none of the NOTIFY call headers are provided out of the eventing package, and thus calling code also can't be aware of which subscription SID is providing invalid data.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.