Add a command to display all SSDP discoveries on local network.

This commit is contained in:
John Beisley 2020-05-16 11:58:37 +01:00
parent 271feae8ac
commit 0c863b7f0d
2 changed files with 42 additions and 0 deletions

View File

@ -0,0 +1,38 @@
// Example program to display all devices discovered on the local network.
package main
import (
"fmt"
"log"
"github.com/huin/goupnp"
"github.com/huin/goupnp/ssdp"
)
func main() {
if err := run(); err != nil {
log.Fatal(err)
}
}
func run() error {
devices, err := goupnp.DiscoverDevices(ssdp.SSDPAll)
if err != nil {
return err
}
for _, device := range devices {
fmt.Printf("Location: %v\n", device.Location)
fmt.Printf("USN: %v\n", device.USN)
if device.Err != nil {
fmt.Printf(" Error: %v\n", device.Err)
} else {
fmt.Printf(" Root v%d.%d @ %s\n",
device.Root.SpecVersion.Major, device.Root.SpecVersion.Minor,
device.Root.URLBaseStr)
fmt.Printf(" Type: %s\n", device.Root.Device.DeviceType)
fmt.Printf(" Friendly name: %s\n", device.Root.Device.FriendlyName)
fmt.Printf(" Num devices: %d\n", len(device.Root.Device.Devices))
}
}
return nil
}

View File

@ -39,6 +39,9 @@ func (err ContextError) Error() string {
// MaybeRootDevice contains either a RootDevice or an error.
type MaybeRootDevice struct {
// Identifier of the device.
USN string
// Set iff Err == nil.
Root *RootDevice
@ -71,6 +74,7 @@ func DiscoverDevices(searchTarget string) ([]MaybeRootDevice, error) {
results := make([]MaybeRootDevice, len(responses))
for i, response := range responses {
maybe := &results[i]
maybe.USN = response.Header.Get("USN")
loc, err := response.Location()
if err != nil {
maybe.Err = ContextError{"unexpected bad location from search", err}