-
Notifications
You must be signed in to change notification settings - Fork 0
/
statevar.go
252 lines (220 loc) · 7 KB
/
statevar.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package yuppie
import (
"fmt"
"reflect"
"strings"
"sync"
"github.com/pkg/errors"
"gitlab.com/mipimipi/yuppie/desc"
"gitlab.com/mipimipi/yuppie/internal/events"
)
// StateVar represents a SOAP variable (e.g. a SOAP state variable)
type StateVar interface {
Type() string
Init(interface{}) error
Get() interface{}
Set(interface{}) error
SetFromString(string) error
IsNumeric() bool
IsString() bool
IsZero() bool
String() string
Lock()
Unlock()
}
// newStateVar creates a new state variable for the UPnP type typ with the value
// val
func newStateVar(typ, val string) (sv StateVar, err error) {
f, exists := constructors[typ]
if !exists {
err = fmt.Errorf("could not create state variable of type '%s', value '%s'", typ, val)
return
}
return f(val)
}
// StateVar represents a state variable
type stateVar struct {
name string
service *service
toBeEvented bool
toBeMulticasted bool
evented bool
def StateVar
list map[string]bool
rng rng
listener func() chan events.StateVar
StateVar
*sync.Mutex
}
// rng represents a range
type rng struct{ Min, Max StateVar }
// isZero returns true if both, min and max of the range are zero
func (rng rng) isZero() bool {
return rng.Min.IsZero() && rng.Max.IsZero()
}
// stateVarFromDesc creates a new state variable based on a description (i.e. the
// corresponding state variable part of the service description) and for the
// service svc. A listener for multicast eventing is added.
func stateVarFromDesc(sv desc.StateVariable, svc *service, listener func() chan events.StateVar) (*stateVar, error) {
def, err := newStateVar(sv.DataType, sv.DefaultValue)
if err != nil {
err = errors.Wrap(err, "could no create state variable from description")
return nil, err
}
stateVar := stateVar{
name: strings.TrimSpace(sv.Name),
service: svc,
toBeEvented: (sv.SendEvents == "yes"),
toBeMulticasted: (sv.Multicast == "yes"),
listener: listener,
StateVar: def,
}
if sv.DefaultValue != "" {
stateVar.def = def
}
if stateVar.IsNumeric() {
var (
min, max StateVar
err error
)
if sv.AllowedValueRange.IsZero() {
min, err = newStateVar(sv.DataType, "0")
if err != nil {
err = errors.Wrap(err, "could no create state variable for zero minimum value")
return nil, err
}
max, err = newStateVar(sv.DataType, "0")
if err != nil {
err = errors.Wrap(err, "could no create state variable for zero maximum value")
return nil, err
}
} else {
min, err = newStateVar(sv.DataType, sv.AllowedValueRange.Minimum)
if err != nil {
err = errors.Wrap(err, "could no create state variable for minimum value")
return nil, err
}
max, err = newStateVar(sv.DataType, sv.AllowedValueRange.Maximum)
if err != nil {
err = errors.Wrap(err, "could no create state variable for maximum value")
return nil, err
}
}
stateVar.rng = rng{
Min: min,
Max: max,
}
}
if stateVar.IsString() && len(sv.AllowedValueList) > 0 {
stateVar.list = make(map[string]bool)
for _, allowed := range sv.AllowedValueList {
stateVar.list[allowed] = true
}
}
return &stateVar, nil
}
// Name returns the name of the state variable
func (me *stateVar) Name() string {
return me.name
}
// ServiceType returns the type of the service that the state variable belongs
// to
func (me *stateVar) ServiceType() string {
return string(me.service.typ)
}
// ServiceVersion returns the version of the service the state variable belongs
// to
func (me *stateVar) ServiceVersion() string {
return string(me.service.ver)
}
// DeviceUDN returns the UDN of the device that the service provides the state
// variable belongs to
func (me *stateVar) DeviceUDN() string {
return me.service.device.UDN
}
// ServiceID returns the id of the service the state variable belongs to
func (me *stateVar) ServiceID() string {
return string(me.service.id)
}
// SendEvent triggers the sending of a multicast event for the state variable
func (me *stateVar) SendEvent() {
if me.toBeEvented && me.toBeMulticasted {
log.Tracef("sent event for state variable '%s'", me.name)
me.listener() <- me
}
}
// Init initializes the state variable with v.
// Note: No multicast event is sent.
func (me *stateVar) Init(v interface{}) (err error) {
if err = me.Set(v); err != nil {
return errors.Wrapf(err, "could not initialize state variable '%s'", me.name)
}
return
}
// Set sets the value of the state variable to v
// Note: A multicast event is sent.
func (me *stateVar) Set(v interface{}) (err error) {
// nothing to do if value would not change
if reflect.ValueOf(me.Get()) != reflect.ValueOf(v) {
return
}
// new eventing required
me.evented = false
// inform event listener about change
if me.toBeEvented || me.toBeMulticasted {
me.listener() <- me
}
return me.StateVar.Set(v)
}
// IsValid checks if the value of the state variable is valid. I.e. for numeric
// variables it's checked whether their value is in required range (if there is
// a range for that variable), for string variables it's checked whether the
// value is allowed (if there is a list of allowed values for that variable)
func (me *stateVar) IsValid(s string) (bool, UPnPErrorCode) {
v, err := newStateVar(me.Type(), s)
if err != nil {
return false, UPnPErrorInvalidArgs
}
if me.IsNumeric() && !me.rng.isZero() {
switch reflect.ValueOf(v.Get()).Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if reflect.ValueOf(v.Get()).Int() < reflect.ValueOf(me.rng.Min.Get()).Int() || reflect.ValueOf(v.Get()).Int() > reflect.ValueOf(me.rng.Max.Get()).Int() {
log.Errorf("state variable %s is out of range", me.name)
return false, UPnPErrorArgValOutOfRange
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if reflect.ValueOf(v.Get()).Uint() < reflect.ValueOf(me.rng.Min.Get()).Uint() || reflect.ValueOf(v.Get()).Uint() > reflect.ValueOf(me.rng.Max.Get()).Uint() {
log.Errorf("state variable %s is out of range", me.name)
return false, UPnPErrorArgValOutOfRange
}
case reflect.Float32, reflect.Float64:
if reflect.ValueOf(v.Get()).Float() < reflect.ValueOf(me.rng.Min.Get()).Float() || reflect.ValueOf(v.Get()).Float() > reflect.ValueOf(me.rng.Max.Get()).Float() {
log.Errorf("state variable %s is out of range", me.name)
return false, UPnPErrorArgValOutOfRange
}
default:
log.Fatalf("state variable '%s' is not numeric", me.name)
return false, UPnPErrorInvalidArgs
}
}
if me.IsString() && me.list != nil && len(me.list) > 0 {
_, allowed := me.list[s]
if !allowed {
log.Errorf("value '%s' of state variable %s is not allowed", me.Get().(string), me.name)
return false, UPnPErrorArgValInvalid
}
}
return true, 0
}
func (me *stateVar) SetEvented(evented bool) {
me.evented = evented
}
func (me *stateVar) Evented() bool {
return me.evented
}
func (me *stateVar) ToBeEvented() bool {
return me.toBeEvented
}
func (me *stateVar) ToBeMulticasted() bool {
return me.toBeMulticasted
}