-
Notifications
You must be signed in to change notification settings - Fork 2
/
struct.go
545 lines (516 loc) · 9.99 KB
/
struct.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
package zcl
import (
"encoding/hex"
"fmt"
"log"
"strconv"
"strings"
)
type ProfileID Zu16
type CommandID Zu8
type ZdoCmdID Zu16
type TypeID Zu8
func (i ProfileID) MarshalZcl() ([]byte, error) { return Zu16(i).MarshalZcl() }
func (i CommandID) MarshalZcl() ([]byte, error) { return Zu8(i).MarshalZcl() }
func (i ZdoCmdID) MarshalZcl() ([]byte, error) { return Zu16(i).MarshalZcl() }
func (i TypeID) MarshalZcl() ([]byte, error) { return Zu8(i).MarshalZcl() }
func (i TypeID) New() Val { return NewValue(uint8(i)) }
func (i TypeID) Size() int {
v := i.New()
if v == nil {
return -1
}
if _, ok := v.(*Zostring); ok {
return 255
}
if _, ok := v.(*Zcstring); ok {
return 255
}
if _, ok := v.(*Zlostring); ok {
return 255
}
if _, ok := v.(*Zlcstring); ok {
return 255
}
if _, ok := v.(*Zarray); ok {
return 255
}
if _, ok := v.(*Zstruct); ok {
return 255
}
if _, ok := v.(*Zset); ok {
return 255
}
if _, ok := v.(*Zbag); ok {
return 255
}
bytes, err := v.MarshalZcl()
if err != nil {
return 255
}
return len(bytes)
}
const (
ErrNotEnoughData errType = "not enough data"
ErrTooMuchData errType = "too much data"
ErrNotImpl errType = "not implemented"
ErrInvalidArrayLength errType = "invalid length for array"
ErrInvalidType errType = "invalid type"
ErrTimeout errType = "timeout"
ErrInvalidHandler errType = "invalid handler"
ErrInvalidPacket errType = "invalid packet"
ErrCommandClusterSpecific errType = "command is cluster-specific, cannot use as profile-wide"
ErrCommandProfileWide errType = "command is profile-wide, cannot use as cluster-specific"
ErrResponseClusterSpecific errType = "request is profile-wide, cannot respond with cluster-specific command"
ErrResponseProfileWide errType = "request is cluster-specific, cannot respond with profile-wide command"
ErrResponseWrongCluster errType = "response cluster does not match request"
ErrResponseWrongMnfCode errType = "response mnfCode does not match request"
ErrUnknownCommand errType = "unknown command"
)
type Direction bool
type CommandType uint8
type AddressMode uint8
type errType string
type Option struct {
Name string
Value int
}
type ArgDesc struct {
Argument Argument
Name string
}
const (
ServerToClient Direction = true
ClientToServer Direction = false
ProfileWide CommandType = 0
ClusterSpecific CommandType = 1
GroupAddress AddressMode = 0x01
NWKAddress AddressMode = 0x02
IEEEAddress AddressMode = 0x03
FullAddress AddressMode = 0x04
BroadcastAll uint16 = 0xFFFF
BroadcastRxOnWhenIdle uint16 = 0xFFFD
BroadcastRoutersCoords uint16 = 0xFFFC
BroadcastLowPowerRouter uint16 = 0xFFFB
)
func (t CommandType) String() string {
switch t {
case ProfileWide:
return "ProfileWide"
case ClusterSpecific:
return "ClusterSpecific"
default:
return fmt.Sprintf("CommandType(0x%02X)", uint8(t))
}
}
func (d Direction) String() string {
if d == ServerToClient {
return "Server-to-Client"
}
return "Client-to-Server"
}
func (a AddressMode) String() string {
switch a {
case GroupAddress:
return "Group"
case NWKAddress:
return "NWK"
case IEEEAddress:
return "IEEE"
case FullAddress:
return "NWK+IEEE"
}
return fmt.Sprintf("AddrMode%d", int(a))
}
func (i *AttrID) UnmarshalZcl(b []byte) ([]byte, error) {
v := new(Zu16)
b, err := v.UnmarshalZcl(b)
*i = AttrID(*v)
return b, err
}
func (i *ProfileID) UnmarshalZcl(b []byte) ([]byte, error) {
v := new(Zu16)
b, err := v.UnmarshalZcl(b)
*i = ProfileID(*v)
return b, err
}
func (i *CommandID) UnmarshalZcl(b []byte) ([]byte, error) {
v := new(Zu8)
b, err := v.UnmarshalZcl(b)
*i = CommandID(*v)
return b, err
}
func (i *ZdoCmdID) UnmarshalZcl(b []byte) ([]byte, error) {
v := new(Zu16)
b, err := v.UnmarshalZcl(b)
*i = ZdoCmdID(*v)
return b, err
}
func (i *TypeID) UnmarshalZcl(b []byte) ([]byte, error) {
v := new(Zu8)
b, err := v.UnmarshalZcl(b)
*i = TypeID(*v)
return b, err
}
func (e errType) Error() string { return string(e) }
func FromString(dataType uint8, val ...string) Val {
var v string
if len(val) == 0 {
return NewValue(dataType)
}
v = val[0]
// Decode in a few different formats
hs, err := hex.DecodeString(v)
if err != nil {
log.Printf("Cannot parse %s as hex: %v", v, err)
}
ui, err := strconv.ParseUint(v, 10, 64)
if err != nil {
log.Printf("Cannot parse %s as uint: %v", v, err)
}
si, err := strconv.ParseInt(v, 10, 64)
if err != nil {
log.Printf("Cannot parse %s as int: %v", v, err)
}
fl, err := strconv.ParseFloat(v, 64)
if err != nil {
log.Printf("Cannot parse %s as float: %v", v, err)
}
switch dataType {
case 16:
b := Zbool(0)
v = strings.ToLower(v)
if v == "1" || v == "true" || v == "yes" {
b = 1
}
return &b
case 32:
b := Zu8(ui)
return &b
case 33:
b := Zu16(ui)
return &b
case 34:
b := Zu24(ui)
return &b
case 35:
b := Zu32(ui)
return &b
case 36:
b := Zu40(ui)
return &b
case 37:
b := Zu48(ui)
return &b
case 38:
b := Zu56(ui)
return &b
case 39:
b := Zu64(ui)
return &b
case 40:
b := Zs8(si)
return &b
case 41:
b := Zs16(si)
return &b
case 42:
b := Zs24(si)
return &b
case 43:
b := Zs32(si)
return &b
case 44:
b := Zs40(si)
return &b
case 45:
b := Zs48(si)
return &b
case 46:
b := Zs56(si)
return &b
case 47:
b := Zs64(si)
return &b
case 48:
b := Zenum8(ui)
return &b
case 49:
b := Zenum16(ui)
return &b
case 56:
b := Zsemi(fl)
return &b
case 57:
b := Zfloat(fl)
return &b
case 58:
b := Zdouble(fl)
return &b
case 65:
b := Zostring(hs)
return &b
case 66:
b := Zcstring(v)
return &b
case 67:
b := Zlostring(hs)
return &b
case 68:
b := Zlcstring(v)
return &b
case 224:
//return &Ztime{}
case 225:
//return &Zdate{}
case 226:
//return &Zutc{}
}
return nil
}
func NewValue(dataType uint8) Val {
switch dataType {
case 8:
return new(Zdat8)
case 9:
return new(Zdat16)
case 10:
return new(Zdat24)
case 11:
return new(Zdat32)
case 12:
return new(Zdat40)
case 13:
return new(Zdat48)
case 14:
return new(Zdat56)
case 15:
return new(Zdat64)
case 16:
return new(Zbool)
case 24:
return new(Zbmp8)
case 25:
return new(Zbmp16)
case 26:
return new(Zbmp24)
case 27:
return new(Zbmp32)
case 28:
return new(Zbmp40)
case 29:
return new(Zbmp48)
case 30:
return new(Zbmp56)
case 31:
return new(Zbmp64)
case 32:
return new(Zu8)
case 33:
return new(Zu16)
case 34:
return new(Zu24)
case 35:
return new(Zu32)
case 36:
return new(Zu40)
case 37:
return new(Zu48)
case 38:
return new(Zu56)
case 39:
return new(Zu64)
case 40:
return new(Zs8)
case 41:
return new(Zs16)
case 42:
return new(Zs24)
case 43:
return new(Zs32)
case 44:
return new(Zs40)
case 45:
return new(Zs48)
case 46:
return new(Zs56)
case 47:
return new(Zs64)
case 48:
return new(Zenum8)
case 49:
return new(Zenum16)
case 56:
return new(Zsemi)
case 57:
return new(Zfloat)
case 58:
return new(Zdouble)
case 65:
return new(Zostring)
case 66:
return new(Zcstring)
case 67:
return new(Zlostring)
case 68:
return new(Zlcstring)
case 72:
return new(Zarray)
case 76:
return new(Zstruct)
case 80:
return new(Zset)
case 81:
return new(Zbag)
case 224:
return new(Ztime)
case 225:
return new(Zdate)
case 226:
return new(Zutc)
case 232:
return new(Zcid)
case 233:
return new(Zaid)
case 234:
return new(Zoid)
case 240:
return new(Zuid)
case 241:
return new(Zseckey)
}
return nil
}
func (i TypeID) Analog() bool {
switch i {
case 32, 33, 34, 35, 36, 37, 38, 39:
// unsigned ints
return true
case 40, 41, 42, 43, 44, 45, 46, 47:
// signed ints
return true
case 56, 57, 58:
// floats
return true
case 224, 225, 226:
// time & date
return true
default:
return false
}
}
func (i TypeID) String() string {
switch i {
case 0:
return "No data"
case 8:
return "8-bit data"
case 9:
return "16-bit data"
case 10:
return "24-bit data"
case 11:
return "32-bit data"
case 12:
return "40-bit data"
case 13:
return "48-bit data"
case 14:
return "56-bit data"
case 15:
return "64-bit data"
case 16:
return "Boolean"
case 24:
return "8-bit bitmap"
case 25:
return "16-bit bitmap"
case 26:
return "24-bit bitmap"
case 27:
return "32-bit bitmap"
case 28:
return "40-bit bitmap"
case 29:
return "48-bit bitmap"
case 30:
return "56-bit bitmap"
case 31:
return "64-bit bitmap"
case 32:
return "Unsigned 8-bit integer"
case 33:
return "Unsigned 16-bit integer"
case 34:
return "Unsigned 24-bit integer"
case 35:
return "Unsigned 32-bit integer"
case 36:
return "Unsigned 40-bit integer"
case 37:
return "Unsigned 48-bit integer"
case 38:
return "Unsigned 56-bit integer"
case 39:
return "Unsigned 64-bit integer"
case 40:
return "Signed 8-bit integer"
case 41:
return "Signed 16-bit integer"
case 42:
return "Signed 24-bit integer"
case 43:
return "Signed 32-bit integer"
case 44:
return "Signed 40-bit integer"
case 45:
return "Signed 48-bit integer"
case 46:
return "Signed 56-bit integer"
case 47:
return "Signed 64-bit integer"
case 48:
return "8-bit enumeration"
case 49:
return "16-bit enumeration"
case 56:
return "Semi-precicion"
case 57:
return "Single precicion"
case 58:
return "Double precicion"
case 65:
return "Octet string"
case 66:
return "Character string"
case 67:
return "Long octet string"
case 68:
return "Long character string"
case 72:
return "Array"
case 76:
return "Structure"
case 80:
return "Set"
case 81:
return "Bag"
case 224:
return "Time of day"
case 225:
return "Date"
case 226:
return "UTCTime"
case 232:
return "Cluster ID"
case 233:
return "Attribute ID"
case 234:
return "BACnet OID"
case 240:
return "IEEE address"
case 241:
return "128-bit security key"
}
return fmt.Sprintf("Unknown(0x%02x)", uint8(i))
}