| 1 |
|
| 2 |
|
| 3 |
package goupnp |
| 4 |
|
| 5 |
import ( |
| 6 |
"encoding/xml" |
| 7 |
"errors" |
| 8 |
"fmt" |
| 9 |
"net/url" |
| 10 |
|
| 11 |
"github.com/NebulousLabs/go-upnp/goupnp/scpd" |
| 12 |
"github.com/NebulousLabs/go-upnp/goupnp/soap" |
| 13 |
) |
| 14 |
|
| 15 |
const ( |
| 16 |
DeviceXMLNamespace = "urn:schemas-upnp-org:device-1-0" |
| 17 |
) |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
type RootDevice struct { |
| 23 |
XMLName xml.Name `xml:"root"` |
| 24 |
SpecVersion SpecVersion `xml:"specVersion"` |
| 25 |
URLBase url.URL `xml:"-"` |
| 26 |
URLBaseStr string `xml:"URLBase"` |
| 27 |
Device Device `xml:"device"` |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
func (root *RootDevice) SetURLBase(urlBase *url.URL) { |
| 32 |
root.URLBase = *urlBase |
| 33 |
root.URLBaseStr = urlBase.String() |
| 34 |
root.Device.SetURLBase(urlBase) |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
type SpecVersion struct { |
| 40 |
Major int32 `xml:"major"` |
| 41 |
Minor int32 `xml:"minor"` |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
type Device struct { |
| 46 |
DeviceType string `xml:"deviceType"` |
| 47 |
FriendlyName string `xml:"friendlyName"` |
| 48 |
Manufacturer string `xml:"manufacturer"` |
| 49 |
ManufacturerURL URLField `xml:"manufacturerURL"` |
| 50 |
ModelDescription string `xml:"modelDescription"` |
| 51 |
ModelName string `xml:"modelName"` |
| 52 |
ModelNumber string `xml:"modelNumber"` |
| 53 |
ModelURL URLField `xml:"modelURL"` |
| 54 |
SerialNumber string `xml:"serialNumber"` |
| 55 |
UDN string `xml:"UDN"` |
| 56 |
UPC string `xml:"UPC,omitempty"` |
| 57 |
Icons []Icon `xml:"iconList>icon,omitempty"` |
| 58 |
Services []Service `xml:"serviceList>service,omitempty"` |
| 59 |
Devices []Device `xml:"deviceList>device,omitempty"` |
| 60 |
|
| 61 |
|
| 62 |
PresentationURL URLField `xml:"presentationURL"` |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
func (device *Device) VisitDevices(visitor func(*Device)) { |
| 67 |
visitor(device) |
| 68 |
for i := range device.Devices { |
| 69 |
device.Devices[i].VisitDevices(visitor) |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
func (device *Device) VisitServices(visitor func(*Service)) { |
| 76 |
device.VisitDevices(func(d *Device) { |
| 77 |
for i := range d.Services { |
| 78 |
visitor(&d.Services[i]) |
| 79 |
} |
| 80 |
}) |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
func (device *Device) FindService(serviceType string) []*Service { |
| 86 |
var services []*Service |
| 87 |
device.VisitServices(func(s *Service) { |
| 88 |
if s.ServiceType == serviceType { |
| 89 |
services = append(services, s) |
| 90 |
} |
| 91 |
}) |
| 92 |
return services |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
func (device *Device) SetURLBase(urlBase *url.URL) { |
| 97 |
device.ManufacturerURL.SetURLBase(urlBase) |
| 98 |
device.ModelURL.SetURLBase(urlBase) |
| 99 |
device.PresentationURL.SetURLBase(urlBase) |
| 100 |
for i := range device.Icons { |
| 101 |
device.Icons[i].SetURLBase(urlBase) |
| 102 |
} |
| 103 |
for i := range device.Services { |
| 104 |
device.Services[i].SetURLBase(urlBase) |
| 105 |
} |
| 106 |
for i := range device.Devices { |
| 107 |
device.Devices[i].SetURLBase(urlBase) |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
func (device *Device) String() string { |
| 112 |
return fmt.Sprintf("Device ID %s : %s (%s)", device.UDN, device.DeviceType, device.FriendlyName) |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
type Icon struct { |
| 118 |
Mimetype string `xml:"mimetype"` |
| 119 |
Width int32 `xml:"width"` |
| 120 |
Height int32 `xml:"height"` |
| 121 |
Depth int32 `xml:"depth"` |
| 122 |
URL URLField `xml:"url"` |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
func (icon *Icon) SetURLBase(url *url.URL) { |
| 127 |
icon.URL.SetURLBase(url) |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
type Service struct { |
| 132 |
ServiceType string `xml:"serviceType"` |
| 133 |
ServiceId string `xml:"serviceId"` |
| 134 |
SCPDURL URLField `xml:"SCPDURL"` |
| 135 |
ControlURL URLField `xml:"controlURL"` |
| 136 |
EventSubURL URLField `xml:"eventSubURL"` |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
func (srv *Service) SetURLBase(urlBase *url.URL) { |
| 141 |
srv.SCPDURL.SetURLBase(urlBase) |
| 142 |
srv.ControlURL.SetURLBase(urlBase) |
| 143 |
srv.EventSubURL.SetURLBase(urlBase) |
| 144 |
} |
| 145 |
|
| 146 |
func (srv *Service) String() string { |
| 147 |
return fmt.Sprintf("Service ID %s : %s", srv.ServiceId, srv.ServiceType) |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
func (srv *Service) RequestSCDP() (*scpd.SCPD, error) { |
| 153 |
if !srv.SCPDURL.Ok { |
| 154 |
return nil, errors.New("bad/missing SCPD URL, or no URLBase has been set") |
| 155 |
} |
| 156 |
s := new(scpd.SCPD) |
| 157 |
if err := requestXml(srv.SCPDURL.URL.String(), scpd.SCPDXMLNamespace, s); err != nil { |
| 158 |
return nil, err |
| 159 |
} |
| 160 |
return s, nil |
| 161 |
} |
| 162 |
|
| 163 |
func (srv *Service) NewSOAPClient() *soap.SOAPClient { |
| 164 |
return soap.NewSOAPClient(srv.ControlURL.URL) |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
type URLField struct { |
| 169 |
URL url.URL `xml:"-"` |
| 170 |
Ok bool `xml:"-"` |
| 171 |
Str string `xml:",chardata"` |
| 172 |
} |
| 173 |
|
| 174 |
func (uf *URLField) SetURLBase(urlBase *url.URL) { |
| 175 |
refUrl, err := url.Parse(uf.Str) |
| 176 |
if err != nil { |
| 177 |
uf.URL = url.URL{} |
| 178 |
uf.Ok = false |
| 179 |
return |
| 180 |
} |
| 181 |
|
| 182 |
uf.URL = *urlBase.ResolveReference(refUrl) |
| 183 |
uf.Ok = true |
| 184 |
} |