-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzcl_types_unmarshal.go
373 lines (305 loc) · 7.94 KB
/
zcl_types_unmarshal.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package zcl
import (
"fmt"
"github.com/shimmeringbee/bytecodec"
"github.com/shimmeringbee/bytecodec/bitbuffer"
"github.com/shimmeringbee/zigbee"
"math"
)
func unmarshalZCLType(bb *bitbuffer.BitBuffer, dt AttributeDataType, ctx bytecodec.Context) (interface{}, error) {
switch dt {
case TypeNull:
return nil, nil
case TypeData8:
return unmarshalData(bb, 1)
case TypeData16:
return unmarshalData(bb, 2)
case TypeData24:
return unmarshalData(bb, 3)
case TypeData32:
return unmarshalData(bb, 4)
case TypeData40:
return unmarshalData(bb, 5)
case TypeData48:
return unmarshalData(bb, 6)
case TypeData56:
return unmarshalData(bb, 7)
case TypeData64:
return unmarshalData(bb, 8)
case TypeBoolean:
return unmarshalBoolean(bb)
case TypeBitmap8:
return unmarshalUint(bb, 8)
case TypeBitmap16:
return unmarshalUint(bb, 16)
case TypeBitmap24:
return unmarshalUint(bb, 24)
case TypeBitmap32:
return unmarshalUint(bb, 32)
case TypeBitmap40:
return unmarshalUint(bb, 40)
case TypeBitmap48:
return unmarshalUint(bb, 48)
case TypeBitmap56:
return unmarshalUint(bb, 56)
case TypeBitmap64:
return unmarshalUint(bb, 64)
case TypeUnsignedInt8:
return unmarshalUint(bb, 8)
case TypeUnsignedInt16:
return unmarshalUint(bb, 16)
case TypeUnsignedInt24:
return unmarshalUint(bb, 24)
case TypeUnsignedInt32:
return unmarshalUint(bb, 32)
case TypeUnsignedInt40:
return unmarshalUint(bb, 40)
case TypeUnsignedInt48:
return unmarshalUint(bb, 48)
case TypeUnsignedInt56:
return unmarshalUint(bb, 56)
case TypeUnsignedInt64:
return unmarshalUint(bb, 64)
case TypeSignedInt8:
return unmarshalInt(bb, 8)
case TypeSignedInt16:
return unmarshalInt(bb, 16)
case TypeSignedInt24:
return unmarshalInt(bb, 24)
case TypeSignedInt32:
return unmarshalInt(bb, 32)
case TypeSignedInt40:
return unmarshalInt(bb, 40)
case TypeSignedInt48:
return unmarshalInt(bb, 48)
case TypeSignedInt56:
return unmarshalInt(bb, 56)
case TypeSignedInt64:
return unmarshalInt(bb, 64)
case TypeEnum8:
val, err := unmarshalUint(bb, 8)
if err == nil {
val = uint8(val.(uint64))
}
return val, err
case TypeEnum16:
val, err := unmarshalUint(bb, 16)
if err == nil {
val = uint16(val.(uint64))
}
return val, err
case TypeStringOctet8:
return unmarshalString(bb, 8)
case TypeStringOctet16:
return unmarshalString(bb, 16)
case TypeStringCharacter8:
return unmarshalString(bb, 8)
case TypeStringCharacter16:
return unmarshalString(bb, 16)
case TypeTimeOfDay:
return unmarshalTimeOfDay(bb)
case TypeDate:
return unmarshalDate(bb)
case TypeUTCTime:
return unmarshalUTCTime(bb)
case TypeClusterID:
return unmarshalClusterID(bb)
case TypeAttributeID:
return unmarshalAttributeID(bb)
case TypeIEEEAddress:
return unmarshalIEEEAddress(bb)
case TypeSecurityKey128:
return unmarshalSecurityKey(bb)
case TypeBACnetOID:
return unmarshalBACnetOID(bb)
case TypeStructure:
return unmarshalStructure(bb, ctx)
case TypeArray, TypeSet, TypeBag:
return unmarshalSlice(bb, ctx)
case TypeFloatSingle:
return unmarshalFloatSingle(bb)
case TypeFloatDouble:
return unmarshalFloatDouble(bb)
default:
return nil, fmt.Errorf("unsupported ZCL type to unmarshal: %d", dt)
}
}
func unmarshalData(bb *bitbuffer.BitBuffer, size int) (interface{}, error) {
data := make([]byte, size)
for i := size - 1; i >= 0; i-- {
if b, err := bb.ReadByte(); err != nil {
return nil, err
} else {
data[i] = b
}
}
return data, nil
}
func unmarshalBoolean(bb *bitbuffer.BitBuffer) (interface{}, error) {
if data, err := bb.ReadByte(); err != nil {
return nil, err
} else {
return data != 0x00, nil
}
}
func unmarshalUint(bb *bitbuffer.BitBuffer, bitsize int) (interface{}, error) {
v, err := bb.ReadUint(bitbuffer.LittleEndian, bitsize)
if err != nil {
return nil, err
}
return v, nil
}
func unmarshalInt(bb *bitbuffer.BitBuffer, bitsize int) (interface{}, error) {
v, err := bb.ReadInt(bitbuffer.LittleEndian, bitsize)
if err != nil {
return nil, err
}
return v, nil
}
func unmarshalString(bb *bitbuffer.BitBuffer, bitsize int) (interface{}, error) {
if data, err := bb.ReadStringLengthPrefixed(bitbuffer.LittleEndian, bitsize); err != nil {
return nil, err
} else {
return data, nil
}
}
func unmarshalStringRune(bb *bitbuffer.BitBuffer, bitsize int) (interface{}, error) {
runeCount, err := bb.ReadUint(bitbuffer.LittleEndian, bitsize)
if err != nil {
return nil, err
}
var data []byte
for i := 0; i < int(runeCount); i++ {
b, err := bb.ReadByte()
if err != nil {
return nil, err
}
data = append(data, b)
if b > 0x80 {
moreBytes := 0
if b < 0b1110000 {
moreBytes = 1
} else if b >= 0b1110000 && b < 0b11110000 {
moreBytes = 2
} else {
moreBytes = 3
}
for i := 0; i < moreBytes; i++ {
if b, err := bb.ReadByte(); err != nil {
return nil, err
} else {
data = append(data, b)
}
}
}
}
return string(data), nil
}
func unmarshalTimeOfDay(bb *bitbuffer.BitBuffer) (interface{}, error) {
tod := TimeOfDay{}
if err := bytecodec.UnmarshalFromBitBuffer(bb, &tod); err != nil {
return nil, err
}
return tod, nil
}
func unmarshalDate(bb *bitbuffer.BitBuffer) (interface{}, error) {
date := Date{}
if err := bytecodec.UnmarshalFromBitBuffer(bb, &date); err != nil {
return nil, err
}
return date, nil
}
func unmarshalUTCTime(bb *bitbuffer.BitBuffer) (interface{}, error) {
v, err := bb.ReadInt(bitbuffer.LittleEndian, 32)
if err != nil {
return nil, err
}
return UTCTime(v), nil
}
func unmarshalClusterID(bb *bitbuffer.BitBuffer) (interface{}, error) {
v, err := bb.ReadInt(bitbuffer.LittleEndian, 16)
if err != nil {
return nil, err
}
return zigbee.ClusterID(v), nil
}
func unmarshalAttributeID(bb *bitbuffer.BitBuffer) (interface{}, error) {
v, err := bb.ReadInt(bitbuffer.LittleEndian, 16)
if err != nil {
return nil, err
}
return AttributeID(v), nil
}
func unmarshalIEEEAddress(bb *bitbuffer.BitBuffer) (interface{}, error) {
v, err := bb.ReadInt(bitbuffer.LittleEndian, 64)
if err != nil {
return nil, err
}
return zigbee.IEEEAddress(v), nil
}
func unmarshalSecurityKey(bb *bitbuffer.BitBuffer) (interface{}, error) {
networkKey := zigbee.NetworkKey{}
if err := bytecodec.UnmarshalFromBitBuffer(bb, &networkKey); err != nil {
return nil, err
}
return networkKey, nil
}
func unmarshalBACnetOID(bb *bitbuffer.BitBuffer) (interface{}, error) {
v, err := bb.ReadInt(bitbuffer.LittleEndian, 32)
if err != nil {
return nil, err
}
return BACnetOID(v), nil
}
func unmarshalStructure(bb *bitbuffer.BitBuffer, ctx bytecodec.Context) (interface{}, error) {
itemCount, err := bb.ReadUint(bitbuffer.LittleEndian, 16)
if err != nil {
return nil, err
}
values := []AttributeDataTypeValue{}
for i := 0; i < int(itemCount); i++ {
val := AttributeDataTypeValue{}
if err := val.Unmarshal(bb, ctx); err != nil {
return nil, err
}
values = append(values, val)
}
return values, nil
}
func unmarshalSlice(bb *bitbuffer.BitBuffer, ctx bytecodec.Context) (interface{}, error) {
rawType, err := bb.ReadUint(bitbuffer.LittleEndian, 8)
if err != nil {
return nil, err
}
itemCount, err := bb.ReadUint(bitbuffer.LittleEndian, 16)
if err != nil {
return nil, err
}
itemType := AttributeDataType(rawType)
value := AttributeSlice{
DataType: itemType,
Values: []interface{}{},
}
for i := 0; i < int(itemCount); i++ {
if val, err := unmarshalZCLType(bb, itemType, ctx); err != nil {
return nil, err
} else {
value.Values = append(value.Values, val)
}
}
return value, nil
}
func unmarshalFloatSingle(bb *bitbuffer.BitBuffer) (interface{}, error) {
if bits, err := bb.ReadUint(bitbuffer.LittleEndian, 32); err != nil {
return nil, err
} else {
return math.Float32frombits(uint32(bits)), nil
}
}
func unmarshalFloatDouble(bb *bitbuffer.BitBuffer) (interface{}, error) {
if bits, err := bb.ReadUint(bitbuffer.LittleEndian, 64); err != nil {
return nil, err
} else {
return math.Float64frombits(bits), nil
}
}