forked from huin/goupnp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
device.go
190 lines (165 loc) · 5.39 KB
/
device.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// This file contains XML structures for communicating with UPnP devices.
package goupnp
import (
"encoding/xml"
"errors"
"fmt"
"net/url"
"github.com/huin/goupnp/scpd"
"github.com/huin/goupnp/soap"
)
const (
DeviceXMLNamespace = "urn:schemas-upnp-org:device-1-0"
)
// 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
type RootDevice struct {
XMLName xml.Name `xml:"root"`
SpecVersion SpecVersion `xml:"specVersion"`
URLBase url.URL `xml:"-"`
URLBaseStr string `xml:"URLBase"`
Device Device `xml:"device"`
}
// SetURLBase sets the URLBase for the RootDevice and its underlying components.
func (root *RootDevice) SetURLBase(urlBase *url.URL) {
root.URLBase = *urlBase
root.URLBaseStr = urlBase.String()
root.Device.SetURLBase(urlBase)
}
// SpecVersion is part of a RootDevice, describes the version of the
// specification that the data adheres to.
type SpecVersion struct {
Major int32 `xml:"major"`
Minor int32 `xml:"minor"`
}
// Device is a UPnP device. It can have child devices.
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"`
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"`
}
// VisitDevices calls visitor for the device, and all its descendent devices.
func (device *Device) VisitDevices(visitor func(*Device)) {
visitor(device)
for i := range device.Devices {
device.Devices[i].VisitDevices(visitor)
}
}
// VisitServices calls visitor for all Services under the device and all its
// descendent devices.
func (device *Device) VisitServices(visitor func(*Service)) {
device.VisitDevices(func(d *Device) {
for i := range d.Services {
visitor(&d.Services[i])
}
})
}
// FindService finds all (if any) Services under the device and its descendents
// that have the given ServiceType.
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
}
// SetURLBase sets the URLBase for the Device and its underlying components.
func (device *Device) SetURLBase(urlBase *url.URL) {
device.ManufacturerURL.SetURLBase(urlBase)
device.ModelURL.SetURLBase(urlBase)
device.PresentationURL.SetURLBase(urlBase)
for i := range device.Icons {
device.Icons[i].SetURLBase(urlBase)
}
for i := range device.Services {
device.Services[i].SetURLBase(urlBase)
}
for i := range device.Devices {
device.Devices[i].SetURLBase(urlBase)
}
}
func (device *Device) String() string {
return fmt.Sprintf("Device ID %s : %s (%s)", device.UDN, device.DeviceType, device.FriendlyName)
}
// Icon is a representative image that a device might include in its
// description.
type Icon struct {
Mimetype string `xml:"mimetype"`
Width int32 `xml:"width"`
Height int32 `xml:"height"`
Depth int32 `xml:"depth"`
URL URLField `xml:"url"`
}
// SetURLBase sets the URLBase for the Icon.
func (icon *Icon) SetURLBase(url *url.URL) {
icon.URL.SetURLBase(url)
}
// Service is a service provided by a UPnP Device.
type Service struct {
ServiceType string `xml:"serviceType"`
ServiceId string `xml:"serviceId"`
SCPDURL URLField `xml:"SCPDURL"`
ControlURL URLField `xml:"controlURL"`
EventSubURL URLField `xml:"eventSubURL"`
}
// SetURLBase sets the URLBase for the Service.
func (srv *Service) SetURLBase(urlBase *url.URL) {
srv.SCPDURL.SetURLBase(urlBase)
srv.ControlURL.SetURLBase(urlBase)
srv.EventSubURL.SetURLBase(urlBase)
}
func (srv *Service) String() string {
return fmt.Sprintf("Service ID %s : %s", srv.ServiceId, srv.ServiceType)
}
// RequestSCPD requests the SCPD (soap actions and state variables description)
// for the service.
func (srv *Service) RequestSCPD() (*scpd.SCPD, error) {
if !srv.SCPDURL.Ok {
return nil, errors.New("bad/missing SCPD URL, or no URLBase has been set")
}
s := new(scpd.SCPD)
if err := requestXml(srv.SCPDURL.URL.String(), scpd.SCPDXMLNamespace, s); err != nil {
return nil, err
}
return s, nil
}
// RequestSCDP is for compatibility only, prefer RequestSCPD. This was a
// misspelling of RequestSCDP.
func (srv *Service) RequestSCDP() (*scpd.SCPD, error) {
return srv.RequestSCPD()
}
func (srv *Service) NewSOAPClient() *soap.SOAPClient {
return soap.NewSOAPClient(srv.ControlURL.URL)
}
// URLField is a URL that is part of a device description.
type URLField struct {
URL url.URL `xml:"-"`
Ok bool `xml:"-"`
Str string `xml:",chardata"`
}
func (uf *URLField) SetURLBase(urlBase *url.URL) {
refUrl, err := url.Parse(uf.Str)
if err != nil {
uf.URL = url.URL{}
uf.Ok = false
return
}
uf.URL = *urlBase.ResolveReference(refUrl)
uf.Ok = true
}