device.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // This file contains XML structures for communicating with UPnP devices.
  2. package goupnp
  3. import (
  4. "context"
  5. "encoding/xml"
  6. "errors"
  7. "fmt"
  8. "net/http"
  9. "net/url"
  10. "github.com/tailscale/goupnp/scpd"
  11. "github.com/tailscale/goupnp/soap"
  12. )
  13. const (
  14. DeviceXMLNamespace = "urn:schemas-upnp-org:device-1-0"
  15. )
  16. // RootDevice is the device description as described by section 2.3 "Device
  17. // description" in
  18. // http://upnp.org/specs/arch/UPnP-arch-DeviceArchitecture-v1.1.pdf
  19. type RootDevice struct {
  20. XMLName xml.Name `xml:"root"`
  21. SpecVersion SpecVersion `xml:"specVersion"`
  22. URLBase url.URL `xml:"-"`
  23. URLBaseStr string `xml:"URLBase"`
  24. Device Device `xml:"device"`
  25. }
  26. // SetURLBase sets the URLBase for the RootDevice and its underlying components.
  27. func (root *RootDevice) SetURLBase(urlBase *url.URL) {
  28. root.URLBase = *urlBase
  29. root.URLBaseStr = urlBase.String()
  30. root.Device.SetURLBase(urlBase)
  31. }
  32. // SpecVersion is part of a RootDevice, describes the version of the
  33. // specification that the data adheres to.
  34. type SpecVersion struct {
  35. Major int32 `xml:"major"`
  36. Minor int32 `xml:"minor"`
  37. }
  38. // Device is a UPnP device. It can have child devices.
  39. type Device struct {
  40. DeviceType string `xml:"deviceType"`
  41. FriendlyName string `xml:"friendlyName"`
  42. Manufacturer string `xml:"manufacturer"`
  43. ManufacturerURL URLField `xml:"manufacturerURL"`
  44. ModelDescription string `xml:"modelDescription"`
  45. ModelName string `xml:"modelName"`
  46. ModelNumber string `xml:"modelNumber"`
  47. ModelURL URLField `xml:"modelURL"`
  48. SerialNumber string `xml:"serialNumber"`
  49. UDN string `xml:"UDN"`
  50. UPC string `xml:"UPC,omitempty"`
  51. Icons []Icon `xml:"iconList>icon,omitempty"`
  52. Services []Service `xml:"serviceList>service,omitempty"`
  53. Devices []Device `xml:"deviceList>device,omitempty"`
  54. // Extra observed elements:
  55. PresentationURL URLField `xml:"presentationURL"`
  56. }
  57. // VisitDevices calls visitor for the device, and all its descendent devices.
  58. func (device *Device) VisitDevices(visitor func(*Device)) {
  59. visitor(device)
  60. for i := range device.Devices {
  61. device.Devices[i].VisitDevices(visitor)
  62. }
  63. }
  64. // VisitServices calls visitor for all Services under the device and all its
  65. // descendent devices.
  66. func (device *Device) VisitServices(visitor func(*Service)) {
  67. device.VisitDevices(func(d *Device) {
  68. for i := range d.Services {
  69. visitor(&d.Services[i])
  70. }
  71. })
  72. }
  73. // FindService finds all (if any) Services under the device and its descendents
  74. // that have the given ServiceType.
  75. func (device *Device) FindService(ctx context.Context, serviceType string) []*Service {
  76. var services []*Service
  77. device.VisitServices(func(s *Service) {
  78. if s.ServiceType == serviceType {
  79. services = append(services, s)
  80. }
  81. })
  82. return services
  83. }
  84. // SetURLBase sets the URLBase for the Device and its underlying components.
  85. func (device *Device) SetURLBase(urlBase *url.URL) {
  86. device.ManufacturerURL.SetURLBase(urlBase)
  87. device.ModelURL.SetURLBase(urlBase)
  88. device.PresentationURL.SetURLBase(urlBase)
  89. for i := range device.Icons {
  90. device.Icons[i].SetURLBase(urlBase)
  91. }
  92. for i := range device.Services {
  93. device.Services[i].SetURLBase(urlBase)
  94. }
  95. for i := range device.Devices {
  96. device.Devices[i].SetURLBase(urlBase)
  97. }
  98. }
  99. func (device *Device) String() string {
  100. return fmt.Sprintf("Device ID %s : %s (%s)", device.UDN, device.DeviceType, device.FriendlyName)
  101. }
  102. // Icon is a representative image that a device might include in its
  103. // description.
  104. type Icon struct {
  105. Mimetype string `xml:"mimetype"`
  106. Width int32 `xml:"width"`
  107. Height int32 `xml:"height"`
  108. Depth int32 `xml:"depth"`
  109. URL URLField `xml:"url"`
  110. }
  111. // SetURLBase sets the URLBase for the Icon.
  112. func (icon *Icon) SetURLBase(url *url.URL) {
  113. icon.URL.SetURLBase(url)
  114. }
  115. // Service is a service provided by a UPnP Device.
  116. type Service struct {
  117. ServiceType string `xml:"serviceType"`
  118. ServiceId string `xml:"serviceId"`
  119. SCPDURL URLField `xml:"SCPDURL"`
  120. ControlURL URLField `xml:"controlURL"`
  121. EventSubURL URLField `xml:"eventSubURL"`
  122. }
  123. // SetURLBase sets the URLBase for the Service.
  124. func (srv *Service) SetURLBase(urlBase *url.URL) {
  125. srv.SCPDURL.SetURLBase(urlBase)
  126. srv.ControlURL.SetURLBase(urlBase)
  127. srv.EventSubURL.SetURLBase(urlBase)
  128. }
  129. func (srv *Service) String() string {
  130. return fmt.Sprintf("Service ID %s : %s", srv.ServiceId, srv.ServiceType)
  131. }
  132. // RequestSCPD requests the SCPD (soap actions and state variables description)
  133. // for the service.
  134. func (srv *Service) RequestSCPD(ctx context.Context) (*scpd.SCPD, error) {
  135. if !srv.SCPDURL.Ok {
  136. return nil, errors.New("bad/missing SCPD URL, or no URLBase has been set")
  137. }
  138. s := new(scpd.SCPD)
  139. if err := requestXml(ctx, srv.SCPDURL.URL.String(), scpd.SCPDXMLNamespace, s); err != nil {
  140. return nil, err
  141. }
  142. return s, nil
  143. }
  144. // NewSOAPClient returns a new SOAP client to the service's control
  145. // URL, using the provided http.Client.
  146. // If httpc is nil, http.DefaultClient is used.
  147. func (srv *Service) NewSOAPClient(httpc *http.Client) *soap.SOAPClient {
  148. if httpc == nil {
  149. httpc = http.DefaultClient
  150. }
  151. return soap.NewSOAPClient(srv.ControlURL.URL, httpc)
  152. }
  153. // URLField is a URL that is part of a device description.
  154. type URLField struct {
  155. URL url.URL `xml:"-"`
  156. Ok bool `xml:"-"`
  157. Str string `xml:",chardata"`
  158. }
  159. func (uf *URLField) SetURLBase(urlBase *url.URL) {
  160. refUrl, err := url.Parse(uf.Str)
  161. if err != nil {
  162. uf.URL = url.URL{}
  163. uf.Ok = false
  164. return
  165. }
  166. uf.URL = *urlBase.ResolveReference(refUrl)
  167. uf.Ok = true
  168. }