-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzcl_types_marshal.go
367 lines (302 loc) · 8.59 KB
/
zcl_types_marshal.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
package zcl
import (
"errors"
"fmt"
"github.com/shimmeringbee/bytecodec"
"github.com/shimmeringbee/bytecodec/bitbuffer"
"github.com/shimmeringbee/zigbee"
"math"
)
func marshalZCLType(bb *bitbuffer.BitBuffer, ctx bytecodec.Context, dt AttributeDataType, v interface{}) error {
switch dt {
case TypeNull:
return nil
case TypeData8:
return marshalData(bb, v, 1)
case TypeData16:
return marshalData(bb, v, 2)
case TypeData24:
return marshalData(bb, v, 3)
case TypeData32:
return marshalData(bb, v, 4)
case TypeData40:
return marshalData(bb, v, 5)
case TypeData48:
return marshalData(bb, v, 6)
case TypeData56:
return marshalData(bb, v, 7)
case TypeData64:
return marshalData(bb, v, 8)
case TypeBoolean:
return marshalBoolean(bb, v)
case TypeBitmap8:
return marshalUint(bb, v, 8)
case TypeBitmap16:
return marshalUint(bb, v, 16)
case TypeBitmap24:
return marshalUint(bb, v, 24)
case TypeBitmap32:
return marshalUint(bb, v, 32)
case TypeBitmap40:
return marshalUint(bb, v, 40)
case TypeBitmap48:
return marshalUint(bb, v, 48)
case TypeBitmap56:
return marshalUint(bb, v, 56)
case TypeBitmap64:
return marshalUint(bb, v, 64)
case TypeUnsignedInt8:
return marshalUint(bb, v, 8)
case TypeUnsignedInt16:
return marshalUint(bb, v, 16)
case TypeUnsignedInt24:
return marshalUint(bb, v, 24)
case TypeUnsignedInt32:
return marshalUint(bb, v, 32)
case TypeUnsignedInt40:
return marshalUint(bb, v, 40)
case TypeUnsignedInt48:
return marshalUint(bb, v, 48)
case TypeUnsignedInt56:
return marshalUint(bb, v, 56)
case TypeUnsignedInt64:
return marshalUint(bb, v, 64)
case TypeSignedInt8:
return marshalInt(bb, v, 8)
case TypeSignedInt16:
return marshalInt(bb, v, 16)
case TypeSignedInt24:
return marshalInt(bb, v, 24)
case TypeSignedInt32:
return marshalInt(bb, v, 32)
case TypeSignedInt40:
return marshalInt(bb, v, 40)
case TypeSignedInt48:
return marshalInt(bb, v, 48)
case TypeSignedInt56:
return marshalInt(bb, v, 56)
case TypeSignedInt64:
return marshalInt(bb, v, 64)
case TypeEnum8:
return marshalUint(bb, v, 8)
case TypeEnum16:
return marshalUint(bb, v, 16)
case TypeStringOctet8:
return marshalString(bb, v, 8)
case TypeStringOctet16:
return marshalString(bb, v, 16)
case TypeStringCharacter8:
return marshalString(bb, v, 8)
case TypeStringCharacter16:
return marshalString(bb, v, 16)
case TypeTimeOfDay:
return marshalTimeOfDay(bb, v)
case TypeDate:
return marshalDate(bb, v)
case TypeUTCTime:
return marshalUTCTime(bb, v)
case TypeClusterID:
return marshalClusterID(bb, v)
case TypeAttributeID:
return marshalAttributeID(bb, v)
case TypeIEEEAddress:
return marshalIEEEAddress(bb, v)
case TypeSecurityKey128:
return marshalSecurityKey(bb, v)
case TypeBACnetOID:
return marshalBACnetOID(bb, v)
case TypeStructure:
return marshalStructure(bb, ctx, v)
case TypeArray, TypeSet, TypeBag:
return marshalSlice(bb, ctx, v)
case TypeFloatSingle:
return marshalFloatSingle(bb, v)
case TypeFloatDouble:
return marshalFloatDouble(bb, v)
default:
return fmt.Errorf("unsupported ZCL type to marshal: %d", dt)
}
}
func marshalData(bb *bitbuffer.BitBuffer, v interface{}, size int) error {
data, ok := v.([]byte)
if !ok {
return errors.New("could not cast value")
}
if len(data) != size {
return fmt.Errorf("data array provided does not match output size")
}
for i := size - 1; i >= 0; i-- {
if err := bb.WriteByte(data[i]); err != nil {
return err
}
}
return nil
}
func marshalBoolean(bb *bitbuffer.BitBuffer, v interface{}) error {
data, ok := v.(bool)
if !ok {
return errors.New("could not cast value")
}
if data {
return bb.WriteByte(0x01)
} else {
return bb.WriteByte(0x00)
}
}
func marshalUint(bb *bitbuffer.BitBuffer, v interface{}, bitsize int) error {
switch v := v.(type) {
case uint:
return bb.WriteUint(uint64(v), bitbuffer.LittleEndian, bitsize)
case uint8:
return bb.WriteUint(uint64(v), bitbuffer.LittleEndian, bitsize)
case uint16:
return bb.WriteUint(uint64(v), bitbuffer.LittleEndian, bitsize)
case uint32:
return bb.WriteUint(uint64(v), bitbuffer.LittleEndian, bitsize)
case uint64:
return bb.WriteUint(v, bitbuffer.LittleEndian, bitsize)
}
return errors.New("marshalling uint to ZCL type received unsupported value")
}
func marshalInt(bb *bitbuffer.BitBuffer, v interface{}, bitsize int) error {
switch v := v.(type) {
case int:
return bb.WriteInt(int64(v), bitbuffer.LittleEndian, bitsize)
case int8:
return bb.WriteInt(int64(v), bitbuffer.LittleEndian, bitsize)
case int16:
return bb.WriteInt(int64(v), bitbuffer.LittleEndian, bitsize)
case int32:
return bb.WriteInt(int64(v), bitbuffer.LittleEndian, bitsize)
case int64:
return bb.WriteInt(v, bitbuffer.LittleEndian, bitsize)
}
return errors.New("marshalling int to ZCL type received unsupported value")
}
func marshalString(bb *bitbuffer.BitBuffer, v interface{}, bitsize int) error {
data, ok := v.(string)
if !ok {
return errors.New("could not cast value")
}
return bb.WriteStringLengthPrefixed(data, bitbuffer.LittleEndian, bitsize)
}
func marshalStringRune(bb *bitbuffer.BitBuffer, v interface{}, bitsize int) error {
data, ok := v.(string)
if !ok {
return errors.New("could not cast value")
}
if err := bb.WriteUint(uint64(len([]rune(data))), bitbuffer.LittleEndian, bitsize); err != nil {
return err
}
for i := 0; i < len(data); i++ {
if err := bb.WriteByte(data[i]); err != nil {
return err
}
}
return nil
}
func marshalTimeOfDay(bb *bitbuffer.BitBuffer, v interface{}) error {
tod, ok := v.(TimeOfDay)
if !ok {
return errors.New("could not cast value")
}
return bytecodec.MarshalToBitBuffer(bb, &tod)
}
func marshalDate(bb *bitbuffer.BitBuffer, v interface{}) error {
date, ok := v.(Date)
if !ok {
return errors.New("could not cast value")
}
return bytecodec.MarshalToBitBuffer(bb, &date)
}
func marshalUTCTime(bb *bitbuffer.BitBuffer, v interface{}) error {
utcTime, ok := v.(UTCTime)
if !ok {
return errors.New("could not cast value")
}
return bb.WriteUint(uint64(utcTime), bitbuffer.LittleEndian, 32)
}
func marshalClusterID(bb *bitbuffer.BitBuffer, v interface{}) error {
clusterID, ok := v.(zigbee.ClusterID)
if !ok {
return errors.New("could not cast value")
}
return bb.WriteUint(uint64(clusterID), bitbuffer.LittleEndian, 16)
}
func marshalAttributeID(bb *bitbuffer.BitBuffer, v interface{}) error {
attributeID, ok := v.(AttributeID)
if !ok {
return errors.New("could not cast value")
}
return bb.WriteUint(uint64(attributeID), bitbuffer.LittleEndian, 16)
}
func marshalIEEEAddress(bb *bitbuffer.BitBuffer, v interface{}) error {
ieeeAddress, ok := v.(zigbee.IEEEAddress)
if !ok {
return errors.New("could not cast value")
}
return bb.WriteUint(uint64(ieeeAddress), bitbuffer.LittleEndian, 64)
}
func marshalSecurityKey(bb *bitbuffer.BitBuffer, v interface{}) error {
networkKey, ok := v.(zigbee.NetworkKey)
if !ok {
return errors.New("could not cast value")
}
return bytecodec.MarshalToBitBuffer(bb, &networkKey)
}
func marshalBACnetOID(bb *bitbuffer.BitBuffer, v interface{}) error {
oid, ok := v.(BACnetOID)
if !ok {
return errors.New("could not cast value")
}
return bb.WriteUint(uint64(oid), bitbuffer.LittleEndian, 32)
}
func marshalStructure(bb *bitbuffer.BitBuffer, ctx bytecodec.Context, v interface{}) error {
values, ok := v.([]AttributeDataTypeValue)
if !ok {
return errors.New("could not cast value")
}
if err := bb.WriteUint(uint64(len(values)), bitbuffer.LittleEndian, 16); err != nil {
return err
}
for _, val := range values {
if err := val.Marshal(bb, ctx); err != nil {
return err
}
}
return nil
}
func marshalSlice(bb *bitbuffer.BitBuffer, ctx bytecodec.Context, v interface{}) error {
slice, ok := v.(AttributeSlice)
if !ok {
return errors.New("could not cast value")
}
if err := bb.WriteByte(byte(slice.DataType)); err != nil {
return err
}
if err := bb.WriteUint(uint64(len(slice.Values)), bitbuffer.LittleEndian, 16); err != nil {
return err
}
for i := 0; i < len(slice.Values); i++ {
if err := marshalZCLType(bb, ctx, slice.DataType, slice.Values[i]); err != nil {
return err
}
}
return nil
}
func marshalFloatSingle(bb *bitbuffer.BitBuffer, v interface{}) error {
value, ok := v.(float32)
if !ok {
return errors.New("could not cast value")
}
bits := math.Float32bits(value)
return bb.WriteUint(uint64(bits), bitbuffer.LittleEndian, 32)
}
func marshalFloatDouble(bb *bitbuffer.BitBuffer, v interface{}) error {
value, ok := v.(float64)
if !ok {
return errors.New("could not cast value")
}
bits := math.Float64bits(value)
return bb.WriteUint(bits, bitbuffer.LittleEndian, 64)
}