goupnp

package module
v1.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 29, 2023 License: BSD-2-Clause Imports: 14 Imported by: 1,083

README

goupnp is a UPnP client library for Go

Installation

Run go get -u github.com/huin/goupnp.

Documentation

See GUIDE.md for a quick start on the most common use case for this library.

Supported DCPs (you probably want to start with one of these):

  • GoDoc av1 - Client for UPnP Device Control Protocol MediaServer v1 and MediaRenderer v1.
  • GoDoc internetgateway1 - Client for UPnP Device Control Protocol Internet Gateway Device v1.
  • GoDoc internetgateway2 - Client for UPnP Device Control Protocol Internet Gateway Device v2.

Core components:

  • GoDoc (goupnp) core library - contains datastructures and utilities typically used by the implemented DCPs.
  • GoDoc httpu HTTPU implementation, underlies SSDP.
  • GoDoc ssdp SSDP client implementation (simple service discovery protocol) - used to discover UPnP services on a network.
  • GoDoc soap SOAP client implementation (simple object access protocol) - used to communicate with discovered services.

Regenerating dcps generated source code:

  1. Build code generator:

    go get -u github.com/huin/goupnp/cmd/goupnpdcpgen

  2. Regenerate the code:

    go generate ./...

Supporting additional UPnP devices and services:

Supporting additional services is, in the trivial case, simply a matter of adding the service to the dcpMetadata whitelist in cmd/goupnpdcpgen/metadata.go, regenerating the source code (see above), and committing that source code.

However, it would be helpful if anyone needing such a service could test the service against the service they have, and then reporting any trouble encountered as an issue on this project. If it just works, then please report at least minimal working functionality as an issue, and optionally contribute the metadata upstream.

Migrating due to Breaking Changes

  • #40 introduced a breaking change to handling non-utf8 encodings, but removes a heavy dependency on golang.org/x/net with charset encodings. If this breaks your usage of this library, you can return to the old behavior by modifying the exported variable and importing the package yourself:
import (
  "golang.org/x/net/html/charset"
  "github.com/huin/goupnp"
)

func init() {
  // should be modified before goupnp libraries are in use.
  goupnp.CharsetReaderFault = charset.NewReaderLabel
}

v2alpha

The v2alpha subdirectory contains experimental work on a version 2 API. The plan is to eventually create a v2 subdirectory with a stable version of the version 2 API. The v1 API will stay where it currently is.

NOTE:

  • v2alpha will be deleted one day, so don't rely on it always existing.
  • v2alpha will have API breaking changes, even with itself.

Documentation

Overview

goupnp is an implementation of a client for various UPnP services.

For most uses, it is recommended to use the code-generated packages under github.com/huin/goupnp/dcps. Example use is shown at http://godoc.org/github.com/huin/goupnp/example

A commonly used client is internetgateway1.WANPPPConnection1: http://godoc.org/github.com/huin/goupnp/dcps/internetgateway1#WANPPPConnection1

Currently only a couple of schemas have code generated for them from the UPnP example XML specifications. Not all methods will work on these clients, because the generated stubs contain the full set of specified methods from the XML specifications, and the discovered services will likely support a subset of those methods.

Index

Constants

View Source
const (
	DeviceXMLNamespace = "urn:schemas-upnp-org:device-1-0"
)

Variables

View Source
var CharsetReaderDefault func(charset string, input io.Reader) (io.Reader, error)

CharsetReaderDefault specifies the charset reader used while decoding the output from a UPnP server. It can be modified in an init function to allow for non-utf8 encodings, but should not be changed after requesting clients.

View Source
var HTTPClientDefault = http.DefaultClient

HTTPClient specifies the http.Client object used when fetching the XML from the UPnP server. HTTPClient defaults the http.DefaultClient. This may be overridden by the importing application.

Functions

This section is empty.

Types

type ContextError

type ContextError struct {
	Context string
	Err     error
}

ContextError is an error that wraps an error with some context information.

func (ContextError) Error

func (err ContextError) Error() string

type Device

type Device struct {
	DeviceType       string    `xml:"deviceType"`
	FriendlyName     string    `xml:"friendlyName"`
	Manufacturer     string    `xml:"manufacturer"`
	ManufacturerURL  URLField  `xml:"manufacturerURL"`
	ModelDescription string    `xml:"modelDescription"`
	ModelName        string    `xml:"modelName"`
	ModelNumber      string    `xml:"modelNumber"`
	ModelType        string    `xml:"modelType"`
	ModelURL         URLField  `xml:"modelURL"`
	SerialNumber     string    `xml:"serialNumber"`
	UDN              string    `xml:"UDN"`
	UPC              string    `xml:"UPC,omitempty"`
	Icons            []Icon    `xml:"iconList>icon,omitempty"`
	Services         []Service `xml:"serviceList>service,omitempty"`
	Devices          []Device  `xml:"deviceList>device,omitempty"`

	// Extra observed elements:
	PresentationURL URLField `xml:"presentationURL"`
}

Device is a UPnP device. It can have child devices.

func (*Device) FindService

func (device *Device) FindService(serviceType string) []*Service

FindService finds all (if any) Services under the device and its descendents that have the given ServiceType.

func (*Device) SetURLBase

func (device *Device) SetURLBase(urlBase *url.URL)

SetURLBase sets the URLBase for the Device and its underlying components.

func (*Device) String

func (device *Device) String() string

func (*Device) VisitDevices

func (device *Device) VisitDevices(visitor func(*Device))

VisitDevices calls visitor for the device, and all its descendent devices.

func (*Device) VisitServices

func (device *Device) VisitServices(visitor func(*Service))

VisitServices calls visitor for all Services under the device and all its descendent devices.

type Icon

type Icon struct {
	Mimetype string   `xml:"mimetype"`
	Width    int32    `xml:"width"`
	Height   int32    `xml:"height"`
	Depth    int32    `xml:"depth"`
	URL      URLField `xml:"url"`
}

Icon is a representative image that a device might include in its description.

func (*Icon) SetURLBase

func (icon *Icon) SetURLBase(url *url.URL)

SetURLBase sets the URLBase for the Icon.

type MaybeRootDevice

type MaybeRootDevice struct {
	// Identifier of the device. Note that this in combination with Location
	// uniquely identifies a result from DiscoverDevices.
	USN string

	// Set iff Err == nil.
	Root *RootDevice

	// The location the device was discovered at. This can be used with
	// DeviceByURL, assuming the device is still present. A location represents
	// the discovery of a device, regardless of if there was an error probing it.
	Location *url.URL

	// The address from which the device was discovered (if known - otherwise nil).
	LocalAddr net.IP

	// Any error encountered probing a discovered device.
	Err error
}

MaybeRootDevice contains either a RootDevice or an error.

func DiscoverDevices

func DiscoverDevices(searchTarget string) ([]MaybeRootDevice, error)

DiscoverDevices is the legacy version of DiscoverDevicesCtx, but uses context.Background() as the context.

func DiscoverDevicesCtx added in v1.1.0

func DiscoverDevicesCtx(ctx context.Context, searchTarget string) ([]MaybeRootDevice, error)

DiscoverDevicesCtx attempts to find targets of the given type. This is typically the entry-point for this package. searchTarget is typically a URN in the form "urn:schemas-upnp-org:device:..." or "urn:schemas-upnp-org:service:...". A single error is returned for errors while attempting to send the query. An error or RootDevice is returned for each discovered RootDevice.

type RootDevice

type RootDevice struct {
	XMLName     xml.Name    `xml:"root"`
	SpecVersion SpecVersion `xml:"specVersion"`
	URLBase     url.URL     `xml:"-"`
	URLBaseStr  string      `xml:"URLBase"`
	Device      Device      `xml:"device"`
}

RootDevice is the device description as described by section 2.3 "Device description" in http://upnp.org/specs/arch/UPnP-arch-DeviceArchitecture-v1.1.pdf

func DeviceByURL

func DeviceByURL(loc *url.URL) (*RootDevice, error)

func DeviceByURLCtx added in v1.1.0

func DeviceByURLCtx(ctx context.Context, loc *url.URL) (*RootDevice, error)

func (*RootDevice) SetURLBase

func (root *RootDevice) SetURLBase(urlBase *url.URL)

SetURLBase sets the URLBase for the RootDevice and its underlying components.

type Service

type Service struct {
	ServiceType string   `xml:"serviceType"`
	ServiceId   string   `xml:"serviceId"`
	SCPDURL     URLField `xml:"SCPDURL"`
	ControlURL  URLField `xml:"controlURL"`
	EventSubURL URLField `xml:"eventSubURL"`
}

Service is a service provided by a UPnP Device.

func (*Service) NewSOAPClient

func (srv *Service) NewSOAPClient() *soap.SOAPClient

func (*Service) RequestSCDP

func (srv *Service) RequestSCDP() (*scpd.SCPD, error)

RequestSCDP is for compatibility only, prefer RequestSCPD. This was a misspelling of RequestSCDP.

func (*Service) RequestSCPD

func (srv *Service) RequestSCPD() (*scpd.SCPD, error)

RequestSCPD is the legacy version of RequestSCPDCtx, but uses context.Background() as the context.

func (*Service) RequestSCPDCtx added in v1.1.0

func (srv *Service) RequestSCPDCtx(ctx context.Context) (*scpd.SCPD, error)

RequestSCPDCtx requests the SCPD (soap actions and state variables description) for the service.

func (*Service) SetURLBase

func (srv *Service) SetURLBase(urlBase *url.URL)

SetURLBase sets the URLBase for the Service.

func (*Service) String

func (srv *Service) String() string

type ServiceClient

type ServiceClient struct {
	SOAPClient *soap.SOAPClient
	RootDevice *RootDevice
	Location   *url.URL
	Service    *Service
	// contains filtered or unexported fields
}

ServiceClient is a SOAP client, root device and the service for the SOAP client rolled into one value. The root device, location, and service are intended to be informational. Location can be used to later recreate a ServiceClient with NewServiceClientByURL if the service is still present; bypassing the discovery process.

func NewServiceClients

func NewServiceClients(searchTarget string) (clients []ServiceClient, errors []error, err error)

NewServiceClients is the legacy version of NewServiceClientsCtx, but uses context.Background() as the context.

func NewServiceClientsByURL

func NewServiceClientsByURL(loc *url.URL, searchTarget string) ([]ServiceClient, error)

NewServiceClientsByURL is the legacy version of NewServiceClientsByURLCtx, but uses context.Background() as the context.

func NewServiceClientsByURLCtx added in v1.1.0

func NewServiceClientsByURLCtx(ctx context.Context, loc *url.URL, searchTarget string) ([]ServiceClient, error)

NewServiceClientsByURLCtx creates client(s) for the given service URN, for a root device at the given URL.

func NewServiceClientsCtx added in v1.1.0

func NewServiceClientsCtx(ctx context.Context, searchTarget string) (clients []ServiceClient, errors []error, err error)

NewServiceClientsCtx discovers services, and returns clients for them. err will report any error with the discovery process (blocking any device/service discovery), errors reports errors on a per-root-device basis.

func NewServiceClientsFromRootDevice

func NewServiceClientsFromRootDevice(rootDevice *RootDevice, loc *url.URL, searchTarget string) ([]ServiceClient, error)

NewServiceClientsFromDevice creates client(s) for the given service URN, in a given root device. The loc parameter is simply assigned to the Location attribute of the returned ServiceClient(s).

func (*ServiceClient) GetServiceClient

func (client *ServiceClient) GetServiceClient() *ServiceClient

GetServiceClient returns the ServiceClient itself. This is provided so that the service client attributes can be accessed via an interface method on a wrapping type.

func (*ServiceClient) LocalAddr added in v1.0.3

func (client *ServiceClient) LocalAddr() net.IP

LocalAddr returns the address from which the device was discovered (if known - otherwise empty).

type SpecVersion

type SpecVersion struct {
	Major int32 `xml:"major"`
	Minor int32 `xml:"minor"`
}

SpecVersion is part of a RootDevice, describes the version of the specification that the data adheres to.

type URLField

type URLField struct {
	URL url.URL `xml:"-"`
	Ok  bool    `xml:"-"`
	Str string  `xml:",chardata"`
}

URLField is a URL that is part of a device description.

func (*URLField) SetURLBase

func (uf *URLField) SetURLBase(urlBase *url.URL)

Directories

Path Synopsis
cmd
discoverall
Example program to display all devices discovered on the local network.
Example program to display all devices discovered on the local network.
goupnpdcpgen
Command to generate DCP package source from the XML specification.
Command to generate DCP package source from the XML specification.
dcps
av1
Client for UPnP Device Control Protocol MediaServer v1 and MediaRenderer v1.
Client for UPnP Device Control Protocol MediaServer v1 and MediaRenderer v1.
internetgateway1
Client for UPnP Device Control Protocol Internet Gateway Device v1.
Client for UPnP Device Control Protocol Internet Gateway Device v1.
internetgateway2
Client for UPnP Device Control Protocol Internet Gateway Device v2.
Client for UPnP Device Control Protocol Internet Gateway Device v2.
ocf/internetgateway2
Client for UPnP Device Control Protocol Internet Gateway Device v2 - Open Connectivity Foundation.
Client for UPnP Device Control Protocol Internet Gateway Device v2 - Open Connectivity Foundation.
Serves as examples of using the goupnp library.
Serves as examples of using the goupnp library.
v2alpha module

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL