Skip to content

Commit

Permalink
to squash later
Browse files Browse the repository at this point in the history
  • Loading branch information
mh-cbon committed Mar 21, 2022
1 parent 9f6b64e commit 7204c5d
Show file tree
Hide file tree
Showing 14 changed files with 13,788 additions and 8,264 deletions.
9 changes: 7 additions & 2 deletions cmd/goupnpdcpgen/codetemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@ package main

import (
"html/template"
"path/filepath"
)

var packageTmpl = template.Must(template.New("package").Parse(`{{$name := .Metadata.Name}}
var templateFuncs = template.FuncMap{
"base": filepath.Base,
}

var packageTmpl = template.Must(template.New("package").Funcs(templateFuncs).Parse(`{{$name := .Metadata.Name}}
// Client for UPnP Device Control Protocol {{.Metadata.OfficialName}}.
// {{if .DocURLs}}
// This DCP is documented in detail at: {{range .DocURLs}}
// - {{.}}{{end}}{{end}}
//
// Typically, use one of the New* functions to create clients for services.
package {{$name}}
package {{$name | base}}
// ***********************************************************
// GENERATED FILE - DO NOT EDIT BY HAND. See README.md
Expand Down
3 changes: 2 additions & 1 deletion cmd/goupnpdcpgen/goupnpdcpgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"log"
"os"
"path/filepath"
)

var (
Expand Down Expand Up @@ -46,7 +47,7 @@ func run(dcpName, specsDir string, useGofmt bool) error {
return fmt.Errorf("could not process spec for %s: %v", metadata.Name, err)
}

if err := dcp.writeCode(metadata.Name+".go", useGofmt); err != nil {
if err := dcp.writeCode(filepath.Base(metadata.Name)+".go", useGofmt); err != nil {
return fmt.Errorf("error writing package %q: %v", dcp.Metadata.Name, err)
}

Expand Down
113 changes: 69 additions & 44 deletions cmd/goupnpdcpgen/metadata.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import "strings"
import (
"strings"
)

// DCP contains extra metadata to use when generating DCP source files.
type DCPMetadata struct {
Expand All @@ -13,25 +15,24 @@ var dcpMetadata = []DCPMetadata{
{
Name: "internetgateway1",
OfficialName: "Internet Gateway Device v1",
Src: openconnectivitydotorg{
SpecsURL: allSpecsURL,
DocPath: "*/DeviceProtection_1/UPnP-gw-*v1*.pdf",
XMLSpecZipPath: "*/DeviceProtection_1/UPnP-gw-IGD-TestFiles-*.zip",
XMLServicePath: []string{"*/service/*1.xml"},
XMLDevicePath: []string{"*/device/*1.xml"},
Hacks: []DCPHackFn{fixMissingURN, totalBytesHack},
Src: upnpdotorg{
DocURL: "http://upnp.org/specs/gw/UPnP-gw-InternetGatewayDevice-v1-Device.pdf",
XMLSpecURL: "http://upnp.org/specs/gw/UPnP-gw-IGD-TestFiles-20010921.zip",
Hacks: []DCPHackFn{
totalBytesHack("urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1"),
},
},
},
{
Name: "internetgateway2",
OfficialName: "Internet Gateway Device v2",
Src: openconnectivitydotorg{
SpecsURL: allSpecsURL,
DocPath: "*/Internet Gateway_2/UPnP-gw-*.pdf",
XMLSpecZipPath: "*/Internet Gateway_2/UPnP-gw-IGD-TestFiles-*.zip",
XMLServicePath: []string{"*/service/*1.xml", "*/service/*2.xml"},
XMLDevicePath: []string{"*/device/*1.xml", "*/device/*2.xml"},
Hacks: []DCPHackFn{fixMissingURN, totalBytesHack},
Src: upnpdotorg{
DocURL: "http://upnp.org/specs/gw/UPnP-gw-InternetGatewayDevice-v2-Device.pdf",
XMLSpecURL: "http://upnp.org/specs/gw/UPnP-gw-IGD-Testfiles-20110224.zip",
Hacks: []DCPHackFn{
fixMissingURN("urn:schemas-upnp-org:service:WANIPv6FirewallControl:1"),
totalBytesHack("urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1"),
},
},
},
{
Expand All @@ -43,49 +44,73 @@ var dcpMetadata = []DCPMetadata{
},
},
{
Name: "av3",
OfficialName: "MediaServer v3 and MediaRenderer v4",
Name: "ocf/internetgateway1",
OfficialName: "Internet Gateway Device v1 - Open Connectivity Foundation",
Src: openconnectivitydotorg{
SpecsURL: allSpecsURL,
DocPath: "standardizeddcps/MediaServer_4 and MediaRenderer_3/UPnP-av-*v{3,4}*.pdf",
XMLSpecZipPath: "standardizeddcps/MediaServer_4 and MediaRenderer_3/UPnP-av-TestFiles-*.zip",
XMLServicePath: []string{"*/*/service/*3.xml", "*/*/service/*4.xml"},
XMLDevicePath: []string{"*/*/device/*3.xml", "*/*/device/*4.xml"},
DocPath: "*/DeviceProtection_1/UPnP-gw-*v1*.pdf",
XMLSpecZipPath: "*/DeviceProtection_1/UPnP-gw-IGD-TestFiles-*.zip",
XMLServicePath: []string{"*/service/*1.xml"},
XMLDevicePath: []string{"*/device/*1.xml"},
Hacks: []DCPHackFn{
fixMissingURN("urn:schemas-upnp-org:service:DeviceProtection:1"),
fixMissingURN("urn:schemas-upnp-org:service:WANIPv6FirewallControl:1"),
totalBytesHack(""),
},
},
},
{
Name: "ocf/internetgateway2",
OfficialName: "Internet Gateway Device v2 - Open Connectivity Foundation",
Src: openconnectivitydotorg{
SpecsURL: allSpecsURL,
DocPath: "*/Internet Gateway_2/UPnP-gw-*.pdf",
XMLSpecZipPath: "*/Internet Gateway_2/UPnP-gw-IGD-TestFiles-*.zip",
XMLServicePath: []string{"*/service/*1.xml", "*/service/*2.xml"},
XMLDevicePath: []string{"*/device/*1.xml", "*/device/*2.xml"},
Hacks: []DCPHackFn{
fixMissingURN("urn:schemas-upnp-org:service:DeviceProtection:1"),
totalBytesHack(""),
},
},
},
}

func totalBytesHack(dcp *DCP) error {
for _, service := range dcp.Services {
if service.URN == "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1" {
variables := service.SCPD.StateVariables
for key, variable := range variables {
varName := variable.Name
if strings.HasSuffix(varName, "TotalBytesSent") || strings.HasSuffix(varName, "TotalBytesReceived") {
// Fix size of total bytes which is by default ui4 or maximum 4 GiB.
variable.DataType.Name = "ui8"
variables[key] = variable
func totalBytesHack(missingURN string) func(dcp *DCP) error {
return func(dcp *DCP) error {
for _, service := range dcp.Services {
if service.URN == missingURN || missingURN == "" {
variables := service.SCPD.StateVariables
for key, variable := range variables {
varName := variable.Name
if strings.HasSuffix(varName, "TotalBytesSent") || strings.HasSuffix(varName, "TotalBytesReceived") {
// Fix size of total bytes which is by default ui4 or maximum 4 GiB.
variable.DataType.Name = "ui8"
variables[key] = variable
}
}
}

break
break
}
}
}

return nil
return nil
}
}

func fixMissingURN(dcp *DCP) error {
missingURN := "urn:schemas-upnp-org:service:WANIPv6FirewallControl:1"
if _, ok := dcp.ServiceTypes[missingURN]; ok {
func fixMissingURN(missingURN string) func(dcp *DCP) error {
return func(dcp *DCP) error {
// missingURN := "urn:schemas-upnp-org:service:WANIPv6FirewallControl:1"
if _, ok := dcp.ServiceTypes[missingURN]; ok {
return nil
}
urnParts, err := extractURNParts(missingURN, serviceURNPrefix)
if err != nil {
return err
}
dcp.ServiceTypes[missingURN] = urnParts
return nil
}
urnParts, err := extractURNParts(missingURN, serviceURNPrefix)
if err != nil {
return err
}
dcp.ServiceTypes[missingURN] = urnParts
return nil
}

type DCPHackFn func(*DCP) error
5 changes: 3 additions & 2 deletions cmd/goupnpdcpgen/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ type openconnectivitydotorg struct {
}

func (o openconnectivitydotorg) process(tmpdir, name string, dcp *DCP) error {
allSpecsFilename := filepath.Join(tmpdir, "openconnectivitydotorg_"+name+".zip")
fname := filepath.Base(name)
allSpecsFilename := filepath.Join(tmpdir, "openconnectivitydotorg_"+fname+".zip")
err := acquireFile(allSpecsFilename, o.SpecsURL)
if err != nil {
return fmt.Errorf("could not acquire specs for %s: %v", name, err)
return fmt.Errorf("could not acquire specs %s: %v", name, err)
}
allSpecsArchive, err := zip.OpenReader(allSpecsFilename)
if err != nil {
Expand Down
Loading

0 comments on commit 7204c5d

Please sign in to comment.