-
Notifications
You must be signed in to change notification settings - Fork 3
/
options.go
269 lines (240 loc) · 5.77 KB
/
options.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package coalago
import (
"strconv"
)
// Represents an Option for a CoAP Message
type CoAPMessageOption struct {
Code OptionCode
Value interface{}
}
// Determines if an option is elective
func (o *CoAPMessageOption) IsElective() bool {
if (int(o.Code) % 2) != 0 {
return false
}
return true
}
// Determines if an option is critical
func (o *CoAPMessageOption) IsCritical() bool {
if (int(o.Code) % 2) != 0 {
return true
}
return false
}
// Returns the string value of an option
func (o *CoAPMessageOption) StringValue() string {
if str, ok := o.Value.(string); ok {
return str
}
return ""
}
func (o *CoAPMessageOption) IntValue() int {
if o.Value == nil {
return 0
}
switch o.Value.(type) {
case int:
return o.Value.(int)
case int8:
return int(o.Value.(int8))
case int16:
return int(o.Value.(int16))
case int32:
return int(o.Value.(int32))
case uint:
return int(o.Value.(uint))
case uint8:
return int(o.Value.(uint8))
case uint16:
return int(o.Value.(uint16))
case uint32:
return int(o.Value.(uint32))
case string:
intVal, err := strconv.Atoi(o.Value.(string))
if err != nil {
return 0
}
return intVal
default:
return 0
}
}
func (o *CoAPMessageOption) Uint32Value() uint32 {
if o.Value == nil {
return 0
}
switch o.Value.(type) {
case int:
return uint32(o.Value.(int))
case int8:
return uint32(o.Value.(int8))
case int16:
return uint32(o.Value.(int16))
case int32:
return uint32(o.Value.(int32))
case uint:
return uint32(o.Value.(uint))
case uint8:
return uint32(o.Value.(uint8))
case uint16:
return uint32(o.Value.(uint16))
case uint32:
return o.Value.(uint32)
case string:
intVal, err := strconv.Atoi(o.Value.(string))
if err != nil {
return 0
}
return uint32(intVal)
default:
return 0
}
}
func (o *CoAPMessageOption) Uint16Value() uint16 {
if o.Value == nil {
return 0
}
switch o.Value.(type) {
case int:
return uint16(o.Value.(int))
case int8:
return uint16(o.Value.(int8))
case int16:
return uint16(o.Value.(int16))
case int32:
return uint16(o.Value.(int32))
case uint:
return uint16(o.Value.(uint))
case uint8:
return uint16(o.Value.(uint8))
case uint16:
return o.Value.(uint16)
case uint32:
return uint16(o.Value.(uint32))
case string:
intVal, err := strconv.Atoi(o.Value.(string))
if err != nil {
return 0
}
return uint16(intVal)
default:
return 0
}
}
// Instantiates a New Option
func NewOption(optionNumber OptionCode, optionValue interface{}) *CoAPMessageOption {
return &CoAPMessageOption{
Code: optionNumber,
Value: optionValue,
}
}
// Checks if an option is repeatable
func (opt *CoAPMessageOption) IsRepeatableOption() bool {
switch opt.Code {
case OptionIfMatch, OptionEtag, OptionLocationPath, OptionURIPath, OptionURIQuery, OptionLocationQuery:
return true
default:
return false
}
}
// Checks if an option/option code is recognizable/valid
func (opt *CoAPMessageOption) IsValidOption() bool {
switch opt.Code {
case OptionIfNoneMatch, OptionURIScheme, OptionURIHost,
OptionEtag, OptionIfMatch, OptionObserve, OptionURIPort, OptionLocationPath,
OptionURIPath, OptionContentFormat, OptionMaxAge, OptionURIQuery, OptionAccept,
OptionLocationQuery, OptionBlock2, OptionBlock1, OptionProxyURI, OptionProxySecurityID, OptionProxyScheme, OptionSize1,
OptionHandshakeType, OptionSessionNotFound, OptionSessionExpired, OptionSelectiveRepeatWindowSize:
// OptionWindowtOffset
return true
default:
return false
}
}
// Returns an array of options given an option code
func (m *CoAPMessage) GetOptions(id OptionCode) []*CoAPMessageOption {
var opts []*CoAPMessageOption
for _, val := range m.Options {
if val.Code == id {
opts = append(opts, val)
}
}
return opts
}
// Returns the first option found for a given option code
func (m *CoAPMessage) GetOption(id OptionCode) *CoAPMessageOption {
for _, val := range m.Options {
if val.Code == id {
return val
}
}
return nil
}
func (m *CoAPMessage) GetOptionAsString(id OptionCode) (str string) {
if opt := m.GetOption(id); opt != nil {
return opt.StringValue()
}
return
}
// Attempts to return the string value of an Option
func (m *CoAPMessage) GetOptionsAsString(id OptionCode) (str []string) {
opts := m.GetOptions(id)
for _, o := range opts {
str = append(str, o.StringValue())
}
return
}
func (m *CoAPMessage) GetOptionProxyURIasString() string {
return m.GetOptionAsString(OptionProxyURI)
}
func (m *CoAPMessage) GetOptionProxySchemeAsString() string {
s := m.GetOptionProxyScheme()
switch s {
case COAP_SCHEME:
return "coap"
case COAPS_SCHEME:
return "coaps"
}
return ""
}
func (m *CoAPMessage) GetOptionProxyScheme() int {
opt := m.GetOption(OptionProxyScheme)
if opt == nil {
return -1
}
return opt.IntValue()
}
// Add an Option to the message. If an option is not repeatable, it will replace
// any existing defined Option of the same type
func (m *CoAPMessage) AddOption(code OptionCode, value interface{}) {
opt := NewOption(code, value)
if opt.IsRepeatableOption() {
m.Options = append(m.Options, opt)
} else {
m.RemoveOptions(code)
m.Options = append(m.Options, opt)
}
}
// Add an array of Options to the message. If an option is not repeatable, it will replace
// any existing defined Option of the same type
func (m *CoAPMessage) AddOptions(opts []*CoAPMessageOption) {
for _, opt := range opts {
m.AddOption(opt.Code, opt.Value)
}
}
// Copies the given list of options from another message to this one
func (m *CoAPMessage) CloneOptions(cm *CoAPMessage, opts ...OptionCode) {
for _, opt := range opts {
m.AddOptions(cm.GetOptions(opt))
}
}
// Removes an Option
func (m *CoAPMessage) RemoveOptions(id OptionCode) {
var opts []*CoAPMessageOption
for _, opt := range m.Options {
if opt.Code != id {
opts = append(opts, opt)
}
}
m.Options = opts
}