-
Notifications
You must be signed in to change notification settings - Fork 41
/
options.go
191 lines (144 loc) · 3.41 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
package canopus
import (
"math"
"strings"
)
// Represents an Option for a CoAP Message
type CoapOption struct {
Code OptionCode
Value interface{}
}
func (o *CoapOption) GetValue() interface{} {
return o.Value
}
func (o *CoapOption) GetCode() OptionCode {
return o.Code
}
func (o *CoapOption) Name() string {
return "Name of option"
}
// Determines if an option is elective
func (o *CoapOption) IsElective() bool {
if (int(o.Code) % 2) != 0 {
return false
}
return true
}
// Determines if an option is critical
func (o *CoapOption) IsCritical() bool {
if (int(o.Code) % 2) != 0 {
return true
}
return false
}
// Returns the string value of an option
func (o *CoapOption) StringValue() string {
return o.Value.(string)
}
func (o *CoapOption) IntValue() int {
return o.Value.(int)
}
// Instantiates a New Option
func NewOption(optionNumber OptionCode, optionValue interface{}) *CoapOption {
return &CoapOption{
Code: optionNumber,
Value: optionValue,
}
}
// Creates an array of options decomposed from a given path
func NewPathOptions(path string) []Option {
opts := []Option{}
ps := strings.Split(path, "/")
for _, p := range ps {
if p != "" {
opt := NewOption(OptionURIPath, p)
opts = append(opts, opt)
}
}
return opts
}
// Checks if an option is repeatable
func IsRepeatableOption(opt Option) bool {
switch opt.GetCode() {
case OptionIfMatch, OptionEtag, OptionURIPort, OptionLocationPath, OptionURIPath, OptionURIQuery, OptionLocationQuery,
OptionBlock2, OptionBlock1:
return true
default:
return false
}
}
// Checks if an option/option code is recognizable/valid
func IsValidOption(opt Option) bool {
switch opt.GetCode() {
case OptionIfNoneMatch, OptionURIHost,
OptionEtag, OptionIfMatch, OptionObserve, OptionURIPort, OptionLocationPath,
OptionURIPath, OptionContentFormat, OptionMaxAge, OptionURIQuery, OptionAccept,
OptionLocationQuery, OptionBlock2, OptionBlock1, OptionProxyURI, OptionProxyScheme, OptionSize1:
return true
default:
return false
}
}
// Determines if an option is elective
func IsElectiveOption(opt Option) bool {
i := int(opt.GetCode())
if (i & 1) == 1 {
return false
}
return true
}
// Determines if an option is critical
func IsCriticalOption(opt Option) bool {
return !IsElectiveOption(opt)
}
func NewBlock1Option(bs BlockSizeType, more bool, seq uint32) *Block1Option {
opt := &Block1Option{}
opt.Code = OptionBlock1
val := seq
val = val << 4
if more {
val |= (1 << 3)
}
val |= (uint32(bs) << 0)
opt.Value = val
/*
BLockSize
val := o.Value.(uint)
exp := val & 0x07
return math.Exp2(float64(exp + 4))
More
val := o.Value.(uint)
return ((val >> 3) & 0x01) == 1
*/
return opt
}
func Block1OptionFromOption(opt Option) *Block1Option {
blockOpt := &Block1Option{}
blockOpt.Value = opt.GetValue()
blockOpt.Code = opt.GetCode()
return blockOpt
}
type Block1Option struct {
CoapOption
}
func (o *Block1Option) Sequence() uint32 {
val := o.GetValue().(uint32)
return val >> 4
}
func (o *Block1Option) Exponent() uint32 {
val := o.GetValue().(uint32)
return val & 0x07
}
func (o *Block1Option) BlockSizeLength() uint32 {
sz := uint32(o.Size()) + 4
return sz * sz
}
func (o *Block1Option) Size() BlockSizeType {
val := o.GetValue().(uint32)
exp := val & 0x07
return BlockSizeType(byte(math.Exp2(float64(exp + 4))))
}
func (o *Block1Option) HasMore() bool {
val := o.Value.(uint32)
return ((val >> 3) & 0x01) == 1
}