| 1 |
|
| 2 |
|
| 3 |
package gotasks |
| 4 |
|
| 5 |
import ( |
| 6 |
"archive/zip" |
| 7 |
"encoding/xml" |
| 8 |
"fmt" |
| 9 |
"io" |
| 10 |
"log" |
| 11 |
"net/http" |
| 12 |
"os" |
| 13 |
"path" |
| 14 |
"path/filepath" |
| 15 |
"regexp" |
| 16 |
"strings" |
| 17 |
"text/template" |
| 18 |
|
| 19 |
"github.com/huin/goupnp" |
| 20 |
"github.com/huin/goupnp/scpd" |
| 21 |
"github.com/huin/goutil/codegen" |
| 22 |
"github.com/jingweno/gotask/tasking" |
| 23 |
) |
| 24 |
|
| 25 |
var ( |
| 26 |
deviceURNPrefix = "urn:schemas-upnp-org:device:" |
| 27 |
serviceURNPrefix = "urn:schemas-upnp-org:service:" |
| 28 |
) |
| 29 |
|
| 30 |
|
| 31 |
type DCPMetadata struct { |
| 32 |
Name string |
| 33 |
OfficialName string |
| 34 |
DocURL string |
| 35 |
XMLSpecURL string |
| 36 |
|
| 37 |
Hacks []DCPHackFn |
| 38 |
} |
| 39 |
|
| 40 |
var dcpMetadata = []DCPMetadata{ |
| 41 |
{ |
| 42 |
Name: "internetgateway1", |
| 43 |
OfficialName: "Internet Gateway Device v1", |
| 44 |
DocURL: "http://upnp.org/specs/gw/UPnP-gw-InternetGatewayDevice-v1-Device.pdf", |
| 45 |
XMLSpecURL: "http://upnp.org/specs/gw/UPnP-gw-IGD-TestFiles-20010921.zip", |
| 46 |
Hacks: []DCPHackFn{ |
| 47 |
func(dcp *DCP) error { |
| 48 |
for _, service := range dcp.Services { |
| 49 |
if service.URN == "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1" { |
| 50 |
variables := service.SCPD.StateVariables |
| 51 |
for key, variable := range variables { |
| 52 |
varName := variable.Name |
| 53 |
if varName == "TotalBytesSent" || varName == "TotalBytesReceived" { |
| 54 |
|
| 55 |
variable.DataType.Name = "ui8" |
| 56 |
variables[key] = variable |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
break |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
return nil |
| 65 |
}, |
| 66 |
}, |
| 67 |
}, |
| 68 |
{ |
| 69 |
Name: "internetgateway2", |
| 70 |
OfficialName: "Internet Gateway Device v2", |
| 71 |
DocURL: "http://upnp.org/specs/gw/UPnP-gw-InternetGatewayDevice-v2-Device.pdf", |
| 72 |
XMLSpecURL: "http://upnp.org/specs/gw/UPnP-gw-IGD-Testfiles-20110224.zip", |
| 73 |
Hacks: []DCPHackFn{ |
| 74 |
func(dcp *DCP) error { |
| 75 |
missingURN := "urn:schemas-upnp-org:service:WANIPv6FirewallControl:1" |
| 76 |
if _, ok := dcp.ServiceTypes[missingURN]; ok { |
| 77 |
return nil |
| 78 |
} |
| 79 |
urnParts, err := extractURNParts(missingURN, serviceURNPrefix) |
| 80 |
if err != nil { |
| 81 |
return err |
| 82 |
} |
| 83 |
dcp.ServiceTypes[missingURN] = urnParts |
| 84 |
return nil |
| 85 |
}, |
| 86 |
}, |
| 87 |
}, |
| 88 |
{ |
| 89 |
Name: "av1", |
| 90 |
OfficialName: "MediaServer v1 and MediaRenderer v1", |
| 91 |
DocURL: "http://upnp.org/specs/av/av1/", |
| 92 |
XMLSpecURL: "http://upnp.org/specs/av/UPnP-av-TestFiles-20070927.zip", |
| 93 |
}, |
| 94 |
} |
| 95 |
|
| 96 |
type DCPHackFn func(*DCP) error |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
func TaskSpecgen(t *tasking.T) { |
| 112 |
specsDir := fallbackStrValue("specs", t.Flags.String("specs_dir"), t.Flags.String("s")) |
| 113 |
if err := os.MkdirAll(specsDir, os.ModePerm); err != nil { |
| 114 |
t.Fatalf("Could not create specs-dir %q: %v\n", specsDir, err) |
| 115 |
} |
| 116 |
outDir := fallbackStrValue("../dcps", t.Flags.String("out_dir"), t.Flags.String("o")) |
| 117 |
useGofmt := !t.Flags.Bool("nogofmt") |
| 118 |
|
| 119 |
NEXT_DCP: |
| 120 |
for _, d := range dcpMetadata { |
| 121 |
specFilename := filepath.Join(specsDir, d.Name+".zip") |
| 122 |
err := acquireFile(specFilename, d.XMLSpecURL) |
| 123 |
if err != nil { |
| 124 |
t.Logf("Could not acquire spec for %s, skipping: %v\n", d.Name, err) |
| 125 |
continue NEXT_DCP |
| 126 |
} |
| 127 |
dcp := newDCP(d) |
| 128 |
if err := dcp.processZipFile(specFilename); err != nil { |
| 129 |
log.Printf("Error processing spec for %s in file %q: %v", d.Name, specFilename, err) |
| 130 |
continue NEXT_DCP |
| 131 |
} |
| 132 |
for i, hack := range d.Hacks { |
| 133 |
if err := hack(dcp); err != nil { |
| 134 |
log.Printf("Error with Hack[%d] for %s: %v", i, d.Name, err) |
| 135 |
continue NEXT_DCP |
| 136 |
} |
| 137 |
} |
| 138 |
dcp.writePackage(outDir, useGofmt) |
| 139 |
if err := dcp.writePackage(outDir, useGofmt); err != nil { |
| 140 |
log.Printf("Error writing package %q: %v", dcp.Metadata.Name, err) |
| 141 |
continue NEXT_DCP |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
func fallbackStrValue(defaultValue string, values ...string) string { |
| 147 |
for _, v := range values { |
| 148 |
if v != "" { |
| 149 |
return v |
| 150 |
} |
| 151 |
} |
| 152 |
return defaultValue |
| 153 |
} |
| 154 |
|
| 155 |
func acquireFile(specFilename string, xmlSpecURL string) error { |
| 156 |
if f, err := os.Open(specFilename); err != nil { |
| 157 |
if !os.IsNotExist(err) { |
| 158 |
return err |
| 159 |
} |
| 160 |
} else { |
| 161 |
f.Close() |
| 162 |
return nil |
| 163 |
} |
| 164 |
|
| 165 |
resp, err := http.Get(xmlSpecURL) |
| 166 |
if err != nil { |
| 167 |
return err |
| 168 |
} |
| 169 |
defer resp.Body.Close() |
| 170 |
|
| 171 |
if resp.StatusCode != http.StatusOK { |
| 172 |
return fmt.Errorf("could not download spec %q from %q: ", |
| 173 |
specFilename, xmlSpecURL, resp.Status) |
| 174 |
} |
| 175 |
|
| 176 |
tmpFilename := specFilename + ".download" |
| 177 |
w, err := os.Create(tmpFilename) |
| 178 |
if err != nil { |
| 179 |
return err |
| 180 |
} |
| 181 |
defer w.Close() |
| 182 |
|
| 183 |
_, err = io.Copy(w, resp.Body) |
| 184 |
if err != nil { |
| 185 |
return err |
| 186 |
} |
| 187 |
|
| 188 |
return os.Rename(tmpFilename, specFilename) |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
type DCP struct { |
| 193 |
Metadata DCPMetadata |
| 194 |
DeviceTypes map[string]*URNParts |
| 195 |
ServiceTypes map[string]*URNParts |
| 196 |
Services []SCPDWithURN |
| 197 |
} |
| 198 |
|
| 199 |
func newDCP(metadata DCPMetadata) *DCP { |
| 200 |
return &DCP{ |
| 201 |
Metadata: metadata, |
| 202 |
DeviceTypes: make(map[string]*URNParts), |
| 203 |
ServiceTypes: make(map[string]*URNParts), |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
func (dcp *DCP) processZipFile(filename string) error { |
| 208 |
archive, err := zip.OpenReader(filename) |
| 209 |
if err != nil { |
| 210 |
return fmt.Errorf("error reading zip file %q: %v", filename, err) |
| 211 |
} |
| 212 |
defer archive.Close() |
| 213 |
for _, deviceFile := range globFiles("*/device/*.xml", archive) { |
| 214 |
if err := dcp.processDeviceFile(deviceFile); err != nil { |
| 215 |
return err |
| 216 |
} |
| 217 |
} |
| 218 |
for _, scpdFile := range globFiles("*/service/*.xml", archive) { |
| 219 |
if err := dcp.processSCPDFile(scpdFile); err != nil { |
| 220 |
return err |
| 221 |
} |
| 222 |
} |
| 223 |
return nil |
| 224 |
} |
| 225 |
|
| 226 |
func (dcp *DCP) processDeviceFile(file *zip.File) error { |
| 227 |
var device goupnp.Device |
| 228 |
if err := unmarshalXmlFile(file, &device); err != nil { |
| 229 |
return fmt.Errorf("error decoding device XML from file %q: %v", file.Name, err) |
| 230 |
} |
| 231 |
var mainErr error |
| 232 |
device.VisitDevices(func(d *goupnp.Device) { |
| 233 |
t := strings.TrimSpace(d.DeviceType) |
| 234 |
if t != "" { |
| 235 |
u, err := extractURNParts(t, deviceURNPrefix) |
| 236 |
if err != nil { |
| 237 |
mainErr = err |
| 238 |
} |
| 239 |
dcp.DeviceTypes[t] = u |
| 240 |
} |
| 241 |
}) |
| 242 |
device.VisitServices(func(s *goupnp.Service) { |
| 243 |
u, err := extractURNParts(s.ServiceType, serviceURNPrefix) |
| 244 |
if err != nil { |
| 245 |
mainErr = err |
| 246 |
} |
| 247 |
dcp.ServiceTypes[s.ServiceType] = u |
| 248 |
}) |
| 249 |
return mainErr |
| 250 |
} |
| 251 |
|
| 252 |
func (dcp *DCP) writePackage(outDir string, useGofmt bool) error { |
| 253 |
packageDirname := filepath.Join(outDir, dcp.Metadata.Name) |
| 254 |
err := os.MkdirAll(packageDirname, os.ModePerm) |
| 255 |
if err != nil && !os.IsExist(err) { |
| 256 |
return err |
| 257 |
} |
| 258 |
packageFilename := filepath.Join(packageDirname, dcp.Metadata.Name+".go") |
| 259 |
packageFile, err := os.Create(packageFilename) |
| 260 |
if err != nil { |
| 261 |
return err |
| 262 |
} |
| 263 |
var output io.WriteCloser = packageFile |
| 264 |
if useGofmt { |
| 265 |
if output, err = codegen.NewGofmtWriteCloser(output); err != nil { |
| 266 |
packageFile.Close() |
| 267 |
return err |
| 268 |
} |
| 269 |
} |
| 270 |
if err = packageTmpl.Execute(output, dcp); err != nil { |
| 271 |
output.Close() |
| 272 |
return err |
| 273 |
} |
| 274 |
return output.Close() |
| 275 |
} |
| 276 |
|
| 277 |
func (dcp *DCP) processSCPDFile(file *zip.File) error { |
| 278 |
scpd := new(scpd.SCPD) |
| 279 |
if err := unmarshalXmlFile(file, scpd); err != nil { |
| 280 |
return fmt.Errorf("error decoding SCPD XML from file %q: %v", file.Name, err) |
| 281 |
} |
| 282 |
scpd.Clean() |
| 283 |
urnParts, err := urnPartsFromSCPDFilename(file.Name) |
| 284 |
if err != nil { |
| 285 |
return fmt.Errorf("could not recognize SCPD filename %q: %v", file.Name, err) |
| 286 |
} |
| 287 |
dcp.Services = append(dcp.Services, SCPDWithURN{ |
| 288 |
URNParts: urnParts, |
| 289 |
SCPD: scpd, |
| 290 |
}) |
| 291 |
return nil |
| 292 |
} |
| 293 |
|
| 294 |
type SCPDWithURN struct { |
| 295 |
*URNParts |
| 296 |
SCPD *scpd.SCPD |
| 297 |
} |
| 298 |
|
| 299 |
func (s *SCPDWithURN) WrapArguments(args []*scpd.Argument) (argumentWrapperList, error) { |
| 300 |
wrappedArgs := make(argumentWrapperList, len(args)) |
| 301 |
for i, arg := range args { |
| 302 |
wa, err := s.wrapArgument(arg) |
| 303 |
if err != nil { |
| 304 |
return nil, err |
| 305 |
} |
| 306 |
wrappedArgs[i] = wa |
| 307 |
} |
| 308 |
return wrappedArgs, nil |
| 309 |
} |
| 310 |
|
| 311 |
func (s *SCPDWithURN) wrapArgument(arg *scpd.Argument) (*argumentWrapper, error) { |
| 312 |
relVar := s.SCPD.GetStateVariable(arg.RelatedStateVariable) |
| 313 |
if relVar == nil { |
| 314 |
return nil, fmt.Errorf("no such state variable: %q, for argument %q", arg.RelatedStateVariable, arg.Name) |
| 315 |
} |
| 316 |
cnv, ok := typeConvs[relVar.DataType.Name] |
| 317 |
if !ok { |
| 318 |
return nil, fmt.Errorf("unknown data type: %q, for state variable %q, for argument %q", relVar.DataType.Type, arg.RelatedStateVariable, arg.Name) |
| 319 |
} |
| 320 |
return &argumentWrapper{ |
| 321 |
Argument: *arg, |
| 322 |
relVar: relVar, |
| 323 |
conv: cnv, |
| 324 |
}, nil |
| 325 |
} |
| 326 |
|
| 327 |
type argumentWrapper struct { |
| 328 |
scpd.Argument |
| 329 |
relVar *scpd.StateVariable |
| 330 |
conv conv |
| 331 |
} |
| 332 |
|
| 333 |
func (arg *argumentWrapper) AsParameter() string { |
| 334 |
return fmt.Sprintf("%s %s", arg.Name, arg.conv.ExtType) |
| 335 |
} |
| 336 |
|
| 337 |
func (arg *argumentWrapper) HasDoc() bool { |
| 338 |
rng := arg.relVar.AllowedValueRange |
| 339 |
return ((rng != nil && (rng.Minimum != "" || rng.Maximum != "" || rng.Step != "")) || |
| 340 |
len(arg.relVar.AllowedValues) > 0) |
| 341 |
} |
| 342 |
|
| 343 |
func (arg *argumentWrapper) Document() string { |
| 344 |
relVar := arg.relVar |
| 345 |
if rng := relVar.AllowedValueRange; rng != nil { |
| 346 |
var parts []string |
| 347 |
if rng.Minimum != "" { |
| 348 |
parts = append(parts, fmt.Sprintf("minimum=%s", rng.Minimum)) |
| 349 |
} |
| 350 |
if rng.Maximum != "" { |
| 351 |
parts = append(parts, fmt.Sprintf("maximum=%s", rng.Maximum)) |
| 352 |
} |
| 353 |
if rng.Step != "" { |
| 354 |
parts = append(parts, fmt.Sprintf("step=%s", rng.Step)) |
| 355 |
} |
| 356 |
return "allowed value range: " + strings.Join(parts, ", ") |
| 357 |
} |
| 358 |
if len(relVar.AllowedValues) != 0 { |
| 359 |
return "allowed values: " + strings.Join(relVar.AllowedValues, ", ") |
| 360 |
} |
| 361 |
return "" |
| 362 |
} |
| 363 |
|
| 364 |
func (arg *argumentWrapper) Marshal() string { |
| 365 |
return fmt.Sprintf("soap.Marshal%s(%s)", arg.conv.FuncSuffix, arg.Name) |
| 366 |
} |
| 367 |
|
| 368 |
func (arg *argumentWrapper) Unmarshal(objVar string) string { |
| 369 |
return fmt.Sprintf("soap.Unmarshal%s(%s.%s)", arg.conv.FuncSuffix, objVar, arg.Name) |
| 370 |
} |
| 371 |
|
| 372 |
type argumentWrapperList []*argumentWrapper |
| 373 |
|
| 374 |
func (args argumentWrapperList) HasDoc() bool { |
| 375 |
for _, arg := range args { |
| 376 |
if arg.HasDoc() { |
| 377 |
return true |
| 378 |
} |
| 379 |
} |
| 380 |
return false |
| 381 |
} |
| 382 |
|
| 383 |
type conv struct { |
| 384 |
FuncSuffix string |
| 385 |
ExtType string |
| 386 |
} |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
var typeConvs = map[string]conv{ |
| 391 |
"ui1": conv{"Ui1", "uint8"}, |
| 392 |
"ui2": conv{"Ui2", "uint16"}, |
| 393 |
"ui4": conv{"Ui4", "uint32"}, |
| 394 |
"ui8": conv{"Ui8", "uint64"}, |
| 395 |
"i1": conv{"I1", "int8"}, |
| 396 |
"i2": conv{"I2", "int16"}, |
| 397 |
"i4": conv{"I4", "int32"}, |
| 398 |
"int": conv{"Int", "int64"}, |
| 399 |
"r4": conv{"R4", "float32"}, |
| 400 |
"r8": conv{"R8", "float64"}, |
| 401 |
"number": conv{"R8", "float64"}, |
| 402 |
"fixed.14.4": conv{"Fixed14_4", "float64"}, |
| 403 |
"float": conv{"R8", "float64"}, |
| 404 |
"char": conv{"Char", "rune"}, |
| 405 |
"string": conv{"String", "string"}, |
| 406 |
"date": conv{"Date", "time.Time"}, |
| 407 |
"dateTime": conv{"DateTime", "time.Time"}, |
| 408 |
"dateTime.tz": conv{"DateTimeTz", "time.Time"}, |
| 409 |
"time": conv{"TimeOfDay", "soap.TimeOfDay"}, |
| 410 |
"time.tz": conv{"TimeOfDayTz", "soap.TimeOfDay"}, |
| 411 |
"boolean": conv{"Boolean", "bool"}, |
| 412 |
"bin.base64": conv{"BinBase64", "[]byte"}, |
| 413 |
"bin.hex": conv{"BinHex", "[]byte"}, |
| 414 |
"uri": conv{"URI", "*url.URL"}, |
| 415 |
} |
| 416 |
|
| 417 |
func globFiles(pattern string, archive *zip.ReadCloser) []*zip.File { |
| 418 |
var files []*zip.File |
| 419 |
for _, f := range archive.File { |
| 420 |
if matched, err := path.Match(pattern, f.Name); err != nil { |
| 421 |
|
| 422 |
|
| 423 |
panic(err) |
| 424 |
} else if matched { |
| 425 |
files = append(files, f) |
| 426 |
} |
| 427 |
} |
| 428 |
return files |
| 429 |
} |
| 430 |
|
| 431 |
func unmarshalXmlFile(file *zip.File, data interface{}) error { |
| 432 |
r, err := file.Open() |
| 433 |
if err != nil { |
| 434 |
return err |
| 435 |
} |
| 436 |
decoder := xml.NewDecoder(r) |
| 437 |
defer r.Close() |
| 438 |
return decoder.Decode(data) |
| 439 |
} |
| 440 |
|
| 441 |
type URNParts struct { |
| 442 |
URN string |
| 443 |
Name string |
| 444 |
Version string |
| 445 |
} |
| 446 |
|
| 447 |
func (u *URNParts) Const() string { |
| 448 |
return fmt.Sprintf("URN_%s_%s", u.Name, u.Version) |
| 449 |
} |
| 450 |
|
| 451 |
|
| 452 |
func extractURNParts(urn, expectedPrefix string) (*URNParts, error) { |
| 453 |
if !strings.HasPrefix(urn, expectedPrefix) { |
| 454 |
return nil, fmt.Errorf("%q does not have expected prefix %q", urn, expectedPrefix) |
| 455 |
} |
| 456 |
parts := strings.SplitN(strings.TrimPrefix(urn, expectedPrefix), ":", 2) |
| 457 |
if len(parts) != 2 { |
| 458 |
return nil, fmt.Errorf("%q does not have a name and version", urn) |
| 459 |
} |
| 460 |
name, version := parts[0], parts[1] |
| 461 |
return &URNParts{urn, name, version}, nil |
| 462 |
} |
| 463 |
|
| 464 |
var scpdFilenameRe = regexp.MustCompile( |
| 465 |
`.*/([a-zA-Z0-9]+)([0-9]+)\.xml`) |
| 466 |
|
| 467 |
func urnPartsFromSCPDFilename(filename string) (*URNParts, error) { |
| 468 |
parts := scpdFilenameRe.FindStringSubmatch(filename) |
| 469 |
if len(parts) != 3 { |
| 470 |
return nil, fmt.Errorf("SCPD filename %q does not have expected number of parts", filename) |
| 471 |
} |
| 472 |
name, version := parts[1], parts[2] |
| 473 |
return &URNParts{ |
| 474 |
URN: serviceURNPrefix + name + ":" + version, |
| 475 |
Name: name, |
| 476 |
Version: version, |
| 477 |
}, nil |
| 478 |
} |
| 479 |
|
| 480 |
var packageTmpl = template.Must(template.New("package").Parse(`{{$name := .Metadata.Name}} |
| 481 |
// Client for UPnP Device Control Protocol {{.Metadata.OfficialName}}. |
| 482 |
// {{if .Metadata.DocURL}} |
| 483 |
// This DCP is documented in detail at: {{.Metadata.DocURL}}{{end}} |
| 484 |
// |
| 485 |
// Typically, use one of the New* functions to create clients for services. |
| 486 |
package {{$name}} |
| 487 |
|
| 488 |
// Generated file - do not edit by hand. See README.md |
| 489 |
|
| 490 |
|
| 491 |
import ( |
| 492 |
"net/url" |
| 493 |
"time" |
| 494 |
|
| 495 |
"github.com/huin/goupnp" |
| 496 |
"github.com/huin/goupnp/soap" |
| 497 |
) |
| 498 |
|
| 499 |
// Hack to avoid Go complaining if time isn't used. |
| 500 |
var _ time.Time |
| 501 |
|
| 502 |
// Device URNs: |
| 503 |
const ({{range .DeviceTypes}} |
| 504 |
{{.Const}} = "{{.URN}}"{{end}} |
| 505 |
) |
| 506 |
|
| 507 |
// Service URNs: |
| 508 |
const ({{range .ServiceTypes}} |
| 509 |
{{.Const}} = "{{.URN}}"{{end}} |
| 510 |
) |
| 511 |
|
| 512 |
{{range .Services}} |
| 513 |
{{$srv := .}} |
| 514 |
{{$srvIdent := printf "%s%s" .Name .Version}} |
| 515 |
|
| 516 |
// {{$srvIdent}} is a client for UPnP SOAP service with URN "{{.URN}}". See |
| 517 |
// goupnp.ServiceClient, which contains RootDevice and Service attributes which |
| 518 |
// are provided for informational value. |
| 519 |
type {{$srvIdent}} struct { |
| 520 |
goupnp.ServiceClient |
| 521 |
} |
| 522 |
|
| 523 |
// New{{$srvIdent}}Clients discovers instances of the service on the network, |
| 524 |
// and returns clients to any that are found. errors will contain an error for |
| 525 |
// any devices that replied but which could not be queried, and err will be set |
| 526 |
// if the discovery process failed outright. |
| 527 |
// |
| 528 |
// This is a typical entry calling point into this package. |
| 529 |
func New{{$srvIdent}}Clients() (clients []*{{$srvIdent}}, errors []error, err error) { |
| 530 |
var genericClients []goupnp.ServiceClient |
| 531 |
if genericClients, errors, err = goupnp.NewServiceClients({{$srv.Const}}); err != nil { |
| 532 |
return |
| 533 |
} |
| 534 |
clients = new{{$srvIdent}}ClientsFromGenericClients(genericClients) |
| 535 |
return |
| 536 |
} |
| 537 |
|
| 538 |
// New{{$srvIdent}}ClientsByURL discovers instances of the service at the given |
| 539 |
// URL, and returns clients to any that are found. An error is returned if |
| 540 |
// there was an error probing the service. |
| 541 |
// |
| 542 |
// This is a typical entry calling point into this package when reusing an |
| 543 |
// previously discovered service URL. |
| 544 |
func New{{$srvIdent}}ClientsByURL(loc *url.URL) ([]*{{$srvIdent}}, error) { |
| 545 |
genericClients, err := goupnp.NewServiceClientsByURL(loc, {{$srv.Const}}) |
| 546 |
if err != nil { |
| 547 |
return nil, err |
| 548 |
} |
| 549 |
return new{{$srvIdent}}ClientsFromGenericClients(genericClients), nil |
| 550 |
} |
| 551 |
|
| 552 |
// New{{$srvIdent}}ClientsFromRootDevice discovers instances of the service in |
| 553 |
// a given root device, and returns clients to any that are found. An error is |
| 554 |
// returned if there was not at least one instance of the service within the |
| 555 |
// device. The location parameter is simply assigned to the Location attribute |
| 556 |
// of the wrapped ServiceClient(s). |
| 557 |
// |
| 558 |
// This is a typical entry calling point into this package when reusing an |
| 559 |
// previously discovered root device. |
| 560 |
func New{{$srvIdent}}ClientsFromRootDevice(rootDevice *goupnp.RootDevice, loc *url.URL) ([]*{{$srvIdent}}, error) { |
| 561 |
genericClients, err := goupnp.NewServiceClientsFromRootDevice(rootDevice, loc, {{$srv.Const}}) |
| 562 |
if err != nil { |
| 563 |
return nil, err |
| 564 |
} |
| 565 |
return new{{$srvIdent}}ClientsFromGenericClients(genericClients), nil |
| 566 |
} |
| 567 |
|
| 568 |
func new{{$srvIdent}}ClientsFromGenericClients(genericClients []goupnp.ServiceClient) []*{{$srvIdent}} { |
| 569 |
clients := make([]*{{$srvIdent}}, len(genericClients)) |
| 570 |
for i := range genericClients { |
| 571 |
clients[i] = &{{$srvIdent}}{genericClients[i]} |
| 572 |
} |
| 573 |
return clients |
| 574 |
} |
| 575 |
|
| 576 |
{{range .SCPD.Actions}}{{/* loops over *SCPDWithURN values */}} |
| 577 |
|
| 578 |
{{$winargs := $srv.WrapArguments .InputArguments}} |
| 579 |
{{$woutargs := $srv.WrapArguments .OutputArguments}} |
| 580 |
{{if $winargs.HasDoc}} |
| 581 |
// |
| 582 |
// Arguments:{{range $winargs}}{{if .HasDoc}} |
| 583 |
// |
| 584 |
// * {{.Name}}: {{.Document}}{{end}}{{end}}{{end}} |
| 585 |
{{if $woutargs.HasDoc}} |
| 586 |
// |
| 587 |
// Return values:{{range $woutargs}}{{if .HasDoc}} |
| 588 |
// |
| 589 |
// * {{.Name}}: {{.Document}}{{end}}{{end}}{{end}} |
| 590 |
func (client *{{$srvIdent}}) {{.Name}}({{range $winargs}}{{/* |
| 591 |
*/}}{{.AsParameter}}, {{end}}{{/* |
| 592 |
*/}}) ({{range $woutargs}}{{/* |
| 593 |
*/}}{{.AsParameter}}, {{end}} err error) { |
| 594 |
// Request structure. |
| 595 |
request := {{if $winargs}}&{{template "argstruct" $winargs}}{{"{}"}}{{else}}{{"interface{}(nil)"}}{{end}} |
| 596 |
// BEGIN Marshal arguments into request. |
| 597 |
{{range $winargs}} |
| 598 |
if request.{{.Name}}, err = {{.Marshal}}; err != nil { |
| 599 |
return |
| 600 |
}{{end}} |
| 601 |
// END Marshal arguments into request. |
| 602 |
|
| 603 |
// Response structure. |
| 604 |
response := {{if $woutargs}}&{{template "argstruct" $woutargs}}{{"{}"}}{{else}}{{"interface{}(nil)"}}{{end}} |
| 605 |
|
| 606 |
// Perform the SOAP call. |
| 607 |
if err = client.SOAPClient.PerformAction({{$srv.URNParts.Const}}, "{{.Name}}", request, response); err != nil { |
| 608 |
return |
| 609 |
} |
| 610 |
|
| 611 |
// BEGIN Unmarshal arguments from response. |
| 612 |
{{range $woutargs}} |
| 613 |
if {{.Name}}, err = {{.Unmarshal "response"}}; err != nil { |
| 614 |
return |
| 615 |
}{{end}} |
| 616 |
// END Unmarshal arguments from response. |
| 617 |
return |
| 618 |
} |
| 619 |
{{end}}{{/* range .SCPD.Actions */}} |
| 620 |
{{end}}{{/* range .Services */}} |
| 621 |
|
| 622 |
{{define "argstruct"}}struct {{"{"}}{{range .}} |
| 623 |
{{.Name}} string |
| 624 |
{{end}}{{"}"}}{{end}} |
| 625 |
`)) |