2013-09-26 22:11:06 +00:00
|
|
|
// This file contains XML structures for communicating with UPnP devices.
|
|
|
|
|
|
|
|
package goupnp
|
|
|
|
|
|
|
|
import (
|
2022-10-01 11:49:02 +00:00
|
|
|
"context"
|
2013-09-26 22:11:06 +00:00
|
|
|
"encoding/xml"
|
2013-09-28 19:05:21 +00:00
|
|
|
"errors"
|
2013-09-28 16:57:51 +00:00
|
|
|
"fmt"
|
|
|
|
"net/url"
|
2021-08-09 17:25:57 +00:00
|
|
|
"strings"
|
2013-10-06 12:23:11 +00:00
|
|
|
|
2022-11-01 19:02:13 +00:00
|
|
|
"git.cyrilix.bzh/cyrilix/goupnp/scpd"
|
|
|
|
"git.cyrilix.bzh/cyrilix/goupnp/soap"
|
2013-09-26 22:11:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2013-09-28 16:57:51 +00:00
|
|
|
DeviceXMLNamespace = "urn:schemas-upnp-org:device-1-0"
|
2013-09-26 22:11:06 +00:00
|
|
|
)
|
|
|
|
|
2013-09-28 19:05:21 +00:00
|
|
|
// 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
|
2013-09-28 16:57:51 +00:00
|
|
|
type RootDevice struct {
|
2013-10-03 19:38:23 +00:00
|
|
|
XMLName xml.Name `xml:"root"`
|
2013-09-28 16:57:51 +00:00
|
|
|
SpecVersion SpecVersion `xml:"specVersion"`
|
2013-09-28 17:47:08 +00:00
|
|
|
URLBase url.URL `xml:"-"`
|
2013-09-28 16:57:51 +00:00
|
|
|
URLBaseStr string `xml:"URLBase"`
|
2013-09-29 14:59:53 +00:00
|
|
|
Device Device `xml:"device"`
|
2013-09-26 22:11:06 +00:00
|
|
|
}
|
|
|
|
|
2013-10-06 12:14:31 +00:00
|
|
|
// SetURLBase sets the URLBase for the RootDevice and its underlying components.
|
2013-09-28 16:57:51 +00:00
|
|
|
func (root *RootDevice) SetURLBase(urlBase *url.URL) {
|
2013-09-28 17:47:08 +00:00
|
|
|
root.URLBase = *urlBase
|
2013-09-28 16:57:51 +00:00
|
|
|
root.URLBaseStr = urlBase.String()
|
|
|
|
root.Device.SetURLBase(urlBase)
|
|
|
|
}
|
|
|
|
|
2013-10-06 12:14:31 +00:00
|
|
|
// SpecVersion is part of a RootDevice, describes the version of the
|
|
|
|
// specification that the data adheres to.
|
2013-09-28 16:57:51 +00:00
|
|
|
type SpecVersion struct {
|
2013-09-26 22:11:06 +00:00
|
|
|
Major int32 `xml:"major"`
|
|
|
|
Minor int32 `xml:"minor"`
|
|
|
|
}
|
|
|
|
|
2013-10-06 12:14:31 +00:00
|
|
|
// Device is a UPnP device. It can have child devices.
|
2013-09-28 16:57:51 +00:00
|
|
|
type Device struct {
|
2013-09-29 14:59:53 +00:00
|
|
|
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"`
|
2022-08-11 21:06:09 +00:00
|
|
|
ModelType string `xml:"modelType"`
|
2013-09-29 14:59:53 +00:00
|
|
|
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"`
|
2013-09-26 22:11:06 +00:00
|
|
|
|
|
|
|
// Extra observed elements:
|
2013-09-28 17:47:08 +00:00
|
|
|
PresentationURL URLField `xml:"presentationURL"`
|
2013-09-26 22:11:06 +00:00
|
|
|
}
|
|
|
|
|
2013-10-06 12:14:31 +00:00
|
|
|
// VisitDevices calls visitor for the device, and all its descendent devices.
|
2013-10-03 19:38:23 +00:00
|
|
|
func (device *Device) VisitDevices(visitor func(*Device)) {
|
|
|
|
visitor(device)
|
|
|
|
for i := range device.Devices {
|
|
|
|
device.Devices[i].VisitDevices(visitor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-06 12:14:31 +00:00
|
|
|
// VisitServices calls visitor for all Services under the device and all its
|
|
|
|
// descendent devices.
|
2013-10-03 19:38:23 +00:00
|
|
|
func (device *Device) VisitServices(visitor func(*Service)) {
|
|
|
|
device.VisitDevices(func(d *Device) {
|
2013-10-06 15:53:53 +00:00
|
|
|
for i := range d.Services {
|
2013-10-03 19:38:23 +00:00
|
|
|
visitor(&d.Services[i])
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2013-10-06 12:14:31 +00:00
|
|
|
// FindService finds all (if any) Services under the device and its descendents
|
|
|
|
// that have the given ServiceType.
|
2013-10-03 19:38:23 +00:00
|
|
|
func (device *Device) FindService(serviceType string) []*Service {
|
|
|
|
var services []*Service
|
|
|
|
device.VisitServices(func(s *Service) {
|
|
|
|
if s.ServiceType == serviceType {
|
|
|
|
services = append(services, s)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return services
|
|
|
|
}
|
|
|
|
|
2013-10-06 12:14:31 +00:00
|
|
|
// SetURLBase sets the URLBase for the Device and its underlying components.
|
2013-09-28 16:57:51 +00:00
|
|
|
func (device *Device) SetURLBase(urlBase *url.URL) {
|
2013-09-28 17:47:08 +00:00
|
|
|
device.ManufacturerURL.SetURLBase(urlBase)
|
|
|
|
device.ModelURL.SetURLBase(urlBase)
|
2013-09-28 17:59:03 +00:00
|
|
|
device.PresentationURL.SetURLBase(urlBase)
|
2013-09-29 15:09:10 +00:00
|
|
|
for i := range device.Icons {
|
|
|
|
device.Icons[i].SetURLBase(urlBase)
|
2013-09-28 17:47:08 +00:00
|
|
|
}
|
2013-09-29 15:09:10 +00:00
|
|
|
for i := range device.Services {
|
|
|
|
device.Services[i].SetURLBase(urlBase)
|
2013-09-28 16:57:51 +00:00
|
|
|
}
|
2013-09-29 15:09:10 +00:00
|
|
|
for i := range device.Devices {
|
|
|
|
device.Devices[i].SetURLBase(urlBase)
|
2013-09-28 16:57:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (device *Device) String() string {
|
|
|
|
return fmt.Sprintf("Device ID %s : %s (%s)", device.UDN, device.DeviceType, device.FriendlyName)
|
|
|
|
}
|
|
|
|
|
2013-10-06 12:14:31 +00:00
|
|
|
// Icon is a representative image that a device might include in its
|
|
|
|
// description.
|
2013-09-28 16:57:51 +00:00
|
|
|
type Icon struct {
|
2013-09-28 17:47:08 +00:00
|
|
|
Mimetype string `xml:"mimetype"`
|
|
|
|
Width int32 `xml:"width"`
|
|
|
|
Height int32 `xml:"height"`
|
|
|
|
Depth int32 `xml:"depth"`
|
|
|
|
URL URLField `xml:"url"`
|
|
|
|
}
|
|
|
|
|
2013-10-06 12:14:31 +00:00
|
|
|
// SetURLBase sets the URLBase for the Icon.
|
2013-09-28 17:47:08 +00:00
|
|
|
func (icon *Icon) SetURLBase(url *url.URL) {
|
|
|
|
icon.URL.SetURLBase(url)
|
2013-09-26 22:11:06 +00:00
|
|
|
}
|
|
|
|
|
2013-10-06 12:14:31 +00:00
|
|
|
// Service is a service provided by a UPnP Device.
|
2013-09-28 16:57:51 +00:00
|
|
|
type Service struct {
|
|
|
|
ServiceType string `xml:"serviceType"`
|
|
|
|
ServiceId string `xml:"serviceId"`
|
2013-09-28 17:54:48 +00:00
|
|
|
SCPDURL URLField `xml:"SCPDURL"`
|
|
|
|
ControlURL URLField `xml:"controlURL"`
|
|
|
|
EventSubURL URLField `xml:"eventSubURL"`
|
2013-09-28 16:57:51 +00:00
|
|
|
}
|
|
|
|
|
2013-10-06 12:14:31 +00:00
|
|
|
// SetURLBase sets the URLBase for the Service.
|
2013-09-28 16:57:51 +00:00
|
|
|
func (srv *Service) SetURLBase(urlBase *url.URL) {
|
2013-09-28 17:54:48 +00:00
|
|
|
srv.SCPDURL.SetURLBase(urlBase)
|
|
|
|
srv.ControlURL.SetURLBase(urlBase)
|
|
|
|
srv.EventSubURL.SetURLBase(urlBase)
|
2013-09-28 16:57:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (srv *Service) String() string {
|
|
|
|
return fmt.Sprintf("Service ID %s : %s", srv.ServiceId, srv.ServiceType)
|
2013-09-26 22:11:06 +00:00
|
|
|
}
|
2013-09-28 17:47:08 +00:00
|
|
|
|
2022-10-01 11:49:02 +00:00
|
|
|
// RequestSCPDCtx requests the SCPD (soap actions and state variables description)
|
2013-10-06 12:14:31 +00:00
|
|
|
// for the service.
|
2022-10-01 11:49:02 +00:00
|
|
|
func (srv *Service) RequestSCPDCtx(ctx context.Context) (*scpd.SCPD, error) {
|
2013-09-28 19:05:21 +00:00
|
|
|
if !srv.SCPDURL.Ok {
|
|
|
|
return nil, errors.New("bad/missing SCPD URL, or no URLBase has been set")
|
|
|
|
}
|
2013-10-06 12:23:11 +00:00
|
|
|
s := new(scpd.SCPD)
|
2022-10-01 11:49:02 +00:00
|
|
|
if err := requestXml(ctx, srv.SCPDURL.URL.String(), scpd.SCPDXMLNamespace, s); err != nil {
|
2013-09-28 19:05:21 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2013-10-06 12:23:11 +00:00
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2022-10-01 11:49:02 +00:00
|
|
|
// RequestSCPD is the legacy version of RequestSCPDCtx, but uses
|
|
|
|
// context.Background() as the context.
|
|
|
|
func (srv *Service) RequestSCPD() (*scpd.SCPD, error) {
|
|
|
|
return srv.RequestSCPDCtx(context.Background())
|
|
|
|
}
|
|
|
|
|
2018-03-04 21:55:55 +00:00
|
|
|
// RequestSCDP is for compatibility only, prefer RequestSCPD. This was a
|
|
|
|
// misspelling of RequestSCDP.
|
|
|
|
func (srv *Service) RequestSCDP() (*scpd.SCPD, error) {
|
|
|
|
return srv.RequestSCPD()
|
|
|
|
}
|
|
|
|
|
2013-10-06 12:23:11 +00:00
|
|
|
func (srv *Service) NewSOAPClient() *soap.SOAPClient {
|
|
|
|
return soap.NewSOAPClient(srv.ControlURL.URL)
|
2013-09-28 19:05:21 +00:00
|
|
|
}
|
|
|
|
|
2013-10-06 12:14:31 +00:00
|
|
|
// URLField is a URL that is part of a device description.
|
2013-09-28 17:47:08 +00:00
|
|
|
type URLField struct {
|
|
|
|
URL url.URL `xml:"-"`
|
|
|
|
Ok bool `xml:"-"`
|
|
|
|
Str string `xml:",chardata"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (uf *URLField) SetURLBase(urlBase *url.URL) {
|
2021-08-09 17:25:57 +00:00
|
|
|
str := uf.Str
|
|
|
|
if !strings.Contains(str, "://") && !strings.HasPrefix(str, "/") {
|
|
|
|
str = "/" + str
|
|
|
|
}
|
|
|
|
|
2021-09-08 03:24:49 +00:00
|
|
|
refUrl, err := url.Parse(str)
|
2013-09-28 17:47:08 +00:00
|
|
|
if err != nil {
|
|
|
|
uf.URL = url.URL{}
|
|
|
|
uf.Ok = false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
uf.URL = *urlBase.ResolveReference(refUrl)
|
|
|
|
uf.Ok = true
|
|
|
|
}
|