Skip to content

Instantly share code, notes, and snippets.

@gr4y
Last active October 21, 2015 16:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gr4y/b0b7fb18415ada341217 to your computer and use it in GitHub Desktop.
Save gr4y/b0b7fb18415ada341217 to your computer and use it in GitHub Desktop.
Go-Utility to discover UPNP Services over SSDP. Uses: http://github.com/huin/goupnp
package main
import (
"fmt"
"github.com/huin/goupnp"
)
func main() {
devices, err := goupnp.DiscoverDevices("upnp:rootdevice")
if err != nil {
fmt.Println(err)
}
if len(devices) > 0 {
for _, d := range devices {
fmt.Printf("Discovered device %s\n", d.Root.Device.String())
ListServices(d.Root.Device.Services)
ListDevices(d.Root.Device.Devices)
}
}
}
func ListDevices(devices []goupnp.Device) {
if len(devices) > 0 {
for _, sd := range devices {
fmt.Printf("Discovered Device %s\n", sd.String())
ListDevices(sd.Devices)
ListServices(sd.Services)
}
}
}
func ListServices(services []goupnp.Service) {
if len(services) > 0 {
for _, s := range services {
scpd, err := s.RequestSCDP()
fmt.Printf(" Discovered service %s\n", s.ServiceId)
if err != nil {
fmt.Printf(" Error requesting service SCPD: %v\n", err)
} else {
fmt.Println(" Available actions:")
for _, action := range scpd.Actions {
fmt.Printf(" * %s\n", action.Name)
for _, arg := range action.Arguments {
var varDesc string
if stateVar := scpd.GetStateVariable(arg.RelatedStateVariable); stateVar != nil {
varDesc = fmt.Sprintf(" (%s)", stateVar.DataType.Name)
}
fmt.Printf(" * [%s] %s%s\n", arg.Direction, arg.Name, varDesc)
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment