-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathappdata.go
188 lines (176 loc) · 4.6 KB
/
appdata.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
package encoding
import (
"fmt"
"github.com/NubeDev/bacnet/btypes"
"github.com/NubeDev/bacnet/btypes/null"
)
const (
tagNull uint8 = 0
tagBool uint8 = 1
tagUint uint8 = 2
tagInt uint8 = 3
tagReal uint8 = 4
tagDouble uint8 = 5
tagOctetString uint8 = 6
tagCharacterString uint8 = 7
tagBitString uint8 = 8
tagEnumerated uint8 = 9
tagDate uint8 = 10
tagTime uint8 = 11
tagObjectID uint8 = 12
tagReserve1 uint8 = 13
tagReserve2 uint8 = 14
tagReserve3 uint8 = 15
maxTag uint8 = 16
)
// Other values omitted here can have variable length
const (
realLen uint32 = 4
doubleLen uint32 = 8
dateLen uint32 = 4
timeLen uint32 = 4
objectIDLen uint32 = 4
)
// All app layer is non-context specific
const appLayerContext = false
func (e *Encoder) boolean(x bool) {
// Boolean information is stored into the length field
var length uint32
if x {
length = 1
} else {
length = 0
}
e.tag(tagInfo{ID: tagBool, Context: appLayerContext, Value: length})
}
func (e *Encoder) real(x float32) {
e.write(x)
}
func (d *Decoder) real(x *float32) {
d.decode(x)
}
func (e *Encoder) double(x float64) {
e.write(x)
}
func (d *Decoder) double(x *float64) {
d.decode(x)
}
func (e *Encoder) AppData(i interface{}, typeBVBO bool) error {
//if null used for sending null on point write to prop 87
switch i.(type) {
case null.Null:
e.tag(tagInfo{ID: tagNull, Context: appLayerContext})
return nil
}
//if point type is a BO or BV then set the type to type enum (data type 9)
if typeBVBO {
v := i.(uint32)
v, ok := i.(uint32)
if ok {
length := valueLength(v)
e.tag(tagInfo{ID: tagEnumerated, Context: appLayerContext, Value: uint32(length)})
e.enumerated(v)
return nil
} else {
err := fmt.Errorf("error in type convertion for boolean output or boolean value %T", i)
// Set global error
e.err = err
return err
}
}
switch val := i.(type) {
case float32:
e.tag(tagInfo{ID: tagReal, Context: appLayerContext, Value: realLen})
e.real(val)
case float64:
e.tag(tagInfo{ID: tagDouble, Context: appLayerContext, Value: realLen})
e.double(val)
case bool:
e.boolean(val)
case string:
// Add 1 to length to account for the encoding byte
e.tag(tagInfo{ID: tagCharacterString, Context: appLayerContext, Value: uint32(len(val) + 1)})
e.string(val)
case uint32:
length := valueLength(val)
e.tag(tagInfo{ID: tagUint, Context: appLayerContext, Value: uint32(length)})
e.unsigned(val)
case int32:
v := uint32(val)
length := valueLength(v)
e.tag(tagInfo{ID: tagInt, Context: appLayerContext, Value: uint32(length)})
e.unsigned(v)
// Enumerated is pretty much a wrapper for a uint32 with an enumerated associated with it.
case btypes.Enumerated:
v := uint32(val)
length := valueLength(v)
e.tag(tagInfo{ID: tagEnumerated, Context: appLayerContext, Value: uint32(length)})
e.enumerated(v)
case btypes.ObjectID:
e.tag(tagInfo{ID: tagObjectID, Context: appLayerContext, Value: objectIDLen})
e.objectId(val.Type, val.Instance)
case null.Null:
e.tag(tagInfo{ID: tagNull, Context: appLayerContext})
default:
err := fmt.Errorf("Unknown type %T", i)
// Set global error
e.err = err
return err
}
return nil
}
func (d *Decoder) AppDataOfTag(tag uint8, len int) (interface{}, error) {
switch tag {
case tagNull:
return null.Null{}, nil
case tagBool:
// Originally this was in C so non 0 values are considered
// true
return len > 0, d.Error()
case tagUint:
return d.unsigned(len), d.Error()
case tagInt:
return d.signed(len), d.Error()
case tagReal:
var x float32
d.real(&x)
return x, d.Error()
case tagDouble:
var x float64
d.double(&x)
return x, d.Error()
case tagOctetString:
var b []byte
d.octetstring(&b, len)
return b, d.Error()
case tagCharacterString:
var s string
// Subtract 1 to length to account for the encoding byte
err := d.string(&s, len-1)
return s, err
case tagBitString:
return d.bitString(len), d.Error()
case tagEnumerated:
return d.enumerated(len), d.Error()
case tagDate:
var date btypes.Date
d.date(&date, len)
return date, d.Error()
case tagTime:
var t btypes.Time
d.time(&t, len)
return t, d.Error()
case tagObjectID:
objType, objInstance := d.objectId()
return btypes.ObjectID{
Type: btypes.ObjectType(objType),
Instance: objInstance,
}, d.Error()
default:
return nil, fmt.Errorf("Unsupported tag: %d", tag)
}
}
func (d *Decoder) AppData() (interface{}, error) {
tag, _, lenvalue := d.tagNumberAndValue()
return d.AppDataOfTag(tag, int(lenvalue))
}