Debian Codegraph @

goupnp/scpd/scpd.go

raw symbols SCIP
1 package scpd
2
3 import (
4 "encoding/xml"
5 "strings"
6 )
7
8 const (
9 SCPDXMLNamespace = "urn:schemas-upnp-org:service-1-0"
10 )
11
12 func cleanWhitespace(s *string) {
13 *s = strings.TrimSpace(*s)
14 }
15
16 // SCPD is the service description as described by section 2.5 "Service
17 // description" in
18 // http://upnp.org/specs/arch/UPnP-arch-DeviceArchitecture-v1.1.pdf
19 type SCPD struct {
20 XMLName xml.Name `xml:"scpd"`
21 ConfigId string `xml:"configId,attr"`
22 SpecVersion SpecVersion `xml:"specVersion"`
23 Actions []Action `xml:"actionList>action"`
24 StateVariables []StateVariable `xml:"serviceStateTable>stateVariable"`
25 }
26
27 // Clean attempts to remove stray whitespace etc. in the structure. It seems
28 // unfortunately common for stray whitespace to be present in SCPD documents,
29 // this method attempts to make it easy to clean them out.
30 func (scpd *SCPD) Clean() {
31 cleanWhitespace(&scpd.ConfigId)
32 for i := range scpd.Actions {
33 scpd.Actions[i].clean()
34 }
35 for i := range scpd.StateVariables {
36 scpd.StateVariables[i].clean()
37 }
38 }
39
40 func (scpd *SCPD) GetStateVariable(variable string) *StateVariable {
41 for i := range scpd.StateVariables {
42 v := &scpd.StateVariables[i]
43 if v.Name == variable {
44 return v
45 }
46 }
47 return nil
48 }
49
50 func (scpd *SCPD) GetAction(action string) *Action {
51 for i := range scpd.Actions {
52 a := &scpd.Actions[i]
53 if a.Name == action {
54 return a
55 }
56 }
57 return nil
58 }
59
60 // SpecVersion is part of a SCPD document, describes the version of the
61 // specification that the data adheres to.
62 type SpecVersion struct {
63 Major int32 `xml:"major"`
64 Minor int32 `xml:"minor"`
65 }
66
67 type Action struct {
68 Name string `xml:"name"`
69 Arguments []Argument `xml:"argumentList>argument"`
70 }
71
72 func (action *Action) clean() {
73 cleanWhitespace(&action.Name)
74 for i := range action.Arguments {
75 action.Arguments[i].clean()
76 }
77 }
78
79 func (action *Action) InputArguments() []*Argument {
80 var result []*Argument
81 for i := range action.Arguments {
82 arg := &action.Arguments[i]
83 if arg.IsInput() {
84 result = append(result, arg)
85 }
86 }
87 return result
88 }
89
90 func (action *Action) OutputArguments() []*Argument {
91 var result []*Argument
92 for i := range action.Arguments {
93 arg := &action.Arguments[i]
94 if arg.IsOutput() {
95 result = append(result, arg)
96 }
97 }
98 return result
99 }
100
101 type Argument struct {
102 Name string `xml:"name"`
103 Direction string `xml:"direction"` // in|out
104 RelatedStateVariable string `xml:"relatedStateVariable"` // ?
105 Retval string `xml:"retval"` // ?
106 }
107
108 func (arg *Argument) clean() {
109 cleanWhitespace(&arg.Name)
110 cleanWhitespace(&arg.Direction)
111 cleanWhitespace(&arg.RelatedStateVariable)
112 cleanWhitespace(&arg.Retval)
113 }
114
115 func (arg *Argument) IsInput() bool {
116 return arg.Direction == "in"
117 }
118
119 func (arg *Argument) IsOutput() bool {
120 return arg.Direction == "out"
121 }
122
123 type StateVariable struct {
124 Name string `xml:"name"`
125 SendEvents string `xml:"sendEvents,attr"` // yes|no
126 Multicast string `xml:"multicast,attr"` // yes|no
127 DataType DataType `xml:"dataType"`
128 DefaultValue string `xml:"defaultValue"`
129 AllowedValueRange *AllowedValueRange `xml:"allowedValueRange"`
130 AllowedValues []string `xml:"allowedValueList>allowedValue"`
131 }
132
133 func (v *StateVariable) clean() {
134 cleanWhitespace(&v.Name)
135 cleanWhitespace(&v.SendEvents)
136 cleanWhitespace(&v.Multicast)
137 v.DataType.clean()
138 cleanWhitespace(&v.DefaultValue)
139 if v.AllowedValueRange != nil {
140 v.AllowedValueRange.clean()
141 }
142 for i := range v.AllowedValues {
143 cleanWhitespace(&v.AllowedValues[i])
144 }
145 }
146
147 type AllowedValueRange struct {
148 Minimum string `xml:"minimum"`
149 Maximum string `xml:"maximum"`
150 Step string `xml:"step"`
151 }
152
153 func (r *AllowedValueRange) clean() {
154 cleanWhitespace(&r.Minimum)
155 cleanWhitespace(&r.Maximum)
156 cleanWhitespace(&r.Step)
157 }
158
159 type DataType struct {
160 Name string `xml:",chardata"`
161 Type string `xml:"type,attr"`
162 }
163
164 func (dt *DataType) clean() {
165 cleanWhitespace(&dt.Name)
166 cleanWhitespace(&dt.Type)
167 }