-
Notifications
You must be signed in to change notification settings - Fork 1
/
command_global.go
executable file
·585 lines (533 loc) · 15.1 KB
/
command_global.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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
package cluster
// generic commands can be thought of device-level commands, like (un)binding
import (
"encoding/binary"
"io"
"strconv"
"github.com/dyrkin/composer"
"github.com/function61/hautomo/pkg/ezstack/binstruct"
)
// WARNING: many of these structs have very important order as they directly influence data
// (un)marshaling
type ReadAttributesCommand struct {
AttributeIDs []uint16
}
type TimeOfDay struct {
Hours uint8
Minutes uint8
Seconds uint8
Hundredths uint8
}
type Date struct {
Year uint8
Month uint8
DayOfMonth uint8
DayOfWeek uint8
}
// NOTE: has custom marshaling defined
type Attribute struct {
DataType ZclDataType // upon serialization this disappears (only *Value* is marshaled)
Value interface{}
}
type ReadAttributeStatus struct {
AttributeName string `transient:"true"`
AttributeID uint16
Status ZclStatus
Attribute *Attribute `cond:"uint:Status==0"`
}
type ReadAttributesResponse struct {
ReadAttributeStatuses []*ReadAttributeStatus
}
type WriteAttributeRecord struct {
AttributeName string `transient:"true"`
AttributeID uint16
Attribute *Attribute
}
type WriteAttributesCommand struct {
WriteAttributeRecords []*WriteAttributeRecord
}
type WriteAttributesUndividedCommand struct {
WriteAttributeRecords []*WriteAttributeRecord
}
type WriteAttributesNoResponseCommand struct {
WriteAttributeRecords []*WriteAttributeRecord
}
type WriteAttributeStatus struct {
Status ZclStatus
AttributeName string `transient:"true"`
AttributeID uint16
}
type WriteAttributesResponse struct {
WriteAttributeStatuses []*WriteAttributeStatus
}
type AttributeReportingConfigurationRecord struct {
Direction ReportDirection
AttributeName string `transient:"true"`
AttributeID uint16
AttributeDataType ZclDataType `cond:"uint:Direction==0"` // specified by the attribute
MinimumReportingInterval uint16 `cond:"uint:Direction==0"` // [s]
MaximumReportingInterval uint16 `cond:"uint:Direction==0"` // [s]
ReportableChange *Attribute `cond:"uint:Direction==0"` // [in units of the attribute]
TimeoutPeriod uint16 `cond:"uint:Direction==1"`
}
type ConfigureReportingCommand struct {
AttributeReportingConfigurationRecords []*AttributeReportingConfigurationRecord
}
type AttributeStatusRecord struct {
Status ZclStatus
Direction ReportDirection
AttributeName string `transient:"true"`
AttributeID uint16
}
type ConfigureReportingResponse struct {
// "In the case of successful configuration of all attributes, only a single attribute status
// record SHALL be included in the command, with the status field set to SUCCESS and the
// direction and attribute identifier fields omitted.
AttributeStatusRecords []*AttributeStatusRecord // you can think of this as "failed attribute sets"
}
type AttributeRecord struct {
Direction ReportDirection
AttributeName string `transient:"true"`
AttributeID uint16
}
type ReadReportingConfigurationCommand struct {
AttributeRecords []*AttributeRecord
}
type AttributeReportingConfigurationResponseRecord struct {
Status ZclStatus
Direction ReportDirection
AttributeName string `transient:"true"`
AttributeID uint16
AttributeDataType ZclDataType `cond:"uint:Direction==0;uint:Status==0"`
MinimumReportingInterval uint16 `cond:"uint:Direction==0;uint:Status==0"`
MaximumReportingInterval uint16 `cond:"uint:Direction==0;uint:Status==0"`
ReportableChange *Attribute `cond:"uint:Direction==0;uint:Status==0"`
TimeoutPeriod uint16 `cond:"uint:Direction==1;uint:Status==0"`
}
type ReadReportingConfigurationResponse struct {
AttributeReportingConfigurationResponseRecords []*AttributeReportingConfigurationResponseRecord
}
type AttributeReport struct {
AttributeName string `transient:"true"`
AttributeID uint16
Attribute *Attribute
}
type ReportAttributesCommand struct {
AttributeReports []*AttributeReport
}
type DefaultResponseCommand struct {
CommandID uint8
Status ZclStatus
}
type DiscoverAttributesCommand struct {
StartAttributeID uint16
MaximumAttributeIdentifiers uint8
}
type AttributeInformation struct {
AttributeName string `transient:"true"`
AttributeID uint16
AttributeDataType ZclDataType
}
type DiscoverAttributesResponse struct {
DiscoveryComplete uint8
AttributeInformations []*AttributeInformation
}
type AttributeSelector struct {
AttributeName string `transient:"true"`
AttributeID uint16
Selector []uint16 `size:"1"`
}
type ReadAttributesStructuredCommand struct {
AttributeSelectors []*AttributeSelector
}
type WriteAttributeStructuredRecord struct {
AttributeName string `transient:"true"`
AttributeID uint16
Selector []uint16 `size:"1"`
Attribute *Attribute
}
type WriteAttributesStructuredCommand struct {
WriteAttributeStructuredRecords []*WriteAttributeStructuredRecord
}
type WriteAttributeStatusRecord struct {
Status ZclStatus
AttributeName string `transient:"true"`
AttributeID uint16
Selector []uint16 `size:"1"`
}
type WriteAttributesStructuredResponse struct {
WriteAttributeStatusRecords []*WriteAttributeStatusRecord
}
type DiscoverCommandsReceivedCommand struct {
StartCommandID uint8
MaximumCommandIdentifiers uint8
}
type DiscoverCommandsReceivedResponse struct {
DiscoveryComplete uint8
CommandIdentifiers []uint8
}
type DiscoverCommandsGeneratedCommand struct {
StartCommandID uint8
MaximumCommandIdentifiers uint8
}
type DiscoverCommandsGeneratedResponse struct {
DiscoveryComplete uint8
CommandIdentifiers []uint8
}
type DiscoverAttributesExtendedCommand struct {
StartAttributeID uint16
MaximumAttributeIdentifiers uint8
}
type AttributeAccessControl struct {
Readable uint8 `bits:"0b00000001" bitmask:"start"`
Writeable uint8 `bits:"0b00000010"`
Reportable uint8 `bits:"0b00000100" bitmask:"end"`
}
type ExtendedAttributeInformation struct {
AttributeName string `transient:"true"`
AttributeID uint16
AttributeDataType ZclDataType
AttributeAccessControl *AttributeAccessControl
}
type DiscoverAttributesExtendedResponse struct {
DiscoveryComplete uint8
ExtendedAttributeInformations []*ExtendedAttributeInformation
}
func (a *Attribute) Serialize(w io.Writer) {
c := composer.NewWithW(w)
writeAttribute(c, a.DataType, a.Value)
c.Flush()
}
func writeAttribute(c *composer.Composer, dataType ZclDataType, value interface{}) {
c.Uint8(uint8(dataType))
switch dataType {
case ZclDataTypeNoData:
case ZclDataTypeData8:
b := value.([1]byte)
c.Bytes(b[:])
case ZclDataTypeData16:
b := value.([2]byte)
c.Bytes(b[:])
case ZclDataTypeData24:
b := value.([3]byte)
c.Bytes(b[:])
case ZclDataTypeData32:
b := value.([4]byte)
c.Bytes(b[:])
case ZclDataTypeData40:
b := value.([5]byte)
c.Bytes(b[:])
case ZclDataTypeData48:
b := value.([6]byte)
c.Bytes(b[:])
case ZclDataTypeData56:
b := value.([7]byte)
c.Bytes(b[:])
case ZclDataTypeData64:
b := value.([8]byte)
c.Bytes(b[:])
case ZclDataTypeBoolean:
b := value.(bool)
if b {
c.Byte(1)
} else {
c.Byte(0)
}
case ZclDataTypeBitmap8:
b := value.(uint64)
c.Uint(binary.LittleEndian, b, 1)
case ZclDataTypeBitmap16:
b := value.(uint64)
c.Uint(binary.LittleEndian, b, 2)
case ZclDataTypeBitmap24:
b := value.(uint64)
c.Uint(binary.LittleEndian, b, 3)
case ZclDataTypeBitmap32:
b := value.(uint64)
c.Uint(binary.LittleEndian, b, 4)
case ZclDataTypeBitmap40:
b := value.(uint64)
c.Uint(binary.LittleEndian, b, 5)
case ZclDataTypeBitmap48:
b := value.(uint64)
c.Uint(binary.LittleEndian, b, 6)
case ZclDataTypeBitmap56:
b := value.(uint64)
c.Uint(binary.LittleEndian, b, 7)
case ZclDataTypeBitmap64:
b := value.(uint64)
c.Uint(binary.LittleEndian, b, 8)
case ZclDataTypeUint8:
b := value.(uint64)
c.Uint(binary.LittleEndian, b, 1)
case ZclDataTypeUint16:
b := value.(uint64)
c.Uint(binary.LittleEndian, b, 2)
case ZclDataTypeUint24:
b := value.(uint64)
c.Uint(binary.LittleEndian, b, 3)
case ZclDataTypeUint32:
b := value.(uint64)
c.Uint(binary.LittleEndian, b, 4)
case ZclDataTypeUint40:
b := value.(uint64)
c.Uint(binary.LittleEndian, b, 5)
case ZclDataTypeUint48:
b := value.(uint64)
c.Uint(binary.LittleEndian, b, 6)
case ZclDataTypeUint56:
b := value.(uint64)
c.Uint(binary.LittleEndian, b, 7)
case ZclDataTypeUint64:
b := value.(uint64)
c.Uint(binary.LittleEndian, b, 8)
case ZclDataTypeInt8:
b := value.(int64)
c.Int(binary.LittleEndian, b, 1)
case ZclDataTypeInt16:
b := value.(int64)
c.Int(binary.LittleEndian, b, 2)
case ZclDataTypeInt24:
b := value.(int64)
c.Int(binary.LittleEndian, b, 3)
case ZclDataTypeInt32:
b := value.(int64)
c.Int(binary.LittleEndian, b, 4)
case ZclDataTypeInt40:
b := value.(int64)
c.Int(binary.LittleEndian, b, 5)
case ZclDataTypeInt48:
b := value.(int64)
c.Int(binary.LittleEndian, b, 6)
case ZclDataTypeInt56:
b := value.(int64)
c.Int(binary.LittleEndian, b, 7)
case ZclDataTypeInt64:
b := value.(int64)
c.Int(binary.LittleEndian, b, 8)
case ZclDataTypeEnum8:
b := value.(uint64)
c.Uint(binary.LittleEndian, b, 1)
case ZclDataTypeEnum16:
b := value.(uint64)
c.Uint(binary.LittleEndian, b, 2)
case ZclDataTypeSemiPrec:
case ZclDataTypeSinglePrec:
case ZclDataTypeDoublePrec:
case ZclDataTypeOctetStr:
b := value.(string)
c.Uint8(uint8(len(b)))
c.String(b)
case ZclDataTypeCharStr:
b := value.(string)
c.Uint8(uint8(len(b)))
c.String(b)
case ZclDataTypeLongOctetStr:
b := value.(string)
c.Uint16le(uint16(len(b)))
c.String(b)
case ZclDataTypeLongCharStr:
b := value.(string)
c.Uint16le(uint16(len(b)))
c.String(b)
case ZclDataTypeArray, ZclDataTypeSet, ZclDataTypeBag:
attributes := value.([]*Attribute)
c.Uint16le(uint16(len(attributes)))
for _, attribute := range attributes {
writeAttribute(c, attribute.DataType, attribute.Value)
}
case ZclDataTypeStruct:
case ZclDataTypeTod:
b := value.(*TimeOfDay)
c.Uint8(b.Hours)
c.Uint8(b.Minutes)
c.Uint8(b.Seconds)
c.Uint8(b.Hundredths)
case ZclDataTypeDate:
b := value.(*Date)
c.Uint8(b.Year)
c.Uint8(b.Month)
c.Uint8(b.DayOfMonth)
c.Uint8(b.DayOfWeek)
case ZclDataTypeUtc:
b := value.(uint32)
c.Uint32le(b)
case ZclDataTypeClusterId:
b := value.(uint16)
c.Uint16le(b)
case ZclDataTypeAttrId:
b := value.(uint16)
c.Uint16le(b)
case ZclDataTypeBacOid:
b := value.(uint32)
c.Uint32le(b)
case ZclDataTypeIeeeAddr:
b := value.(string)
v, _ := strconv.ParseUint(b[2:], 16, 64)
c.Uint64le(v)
case ZclDataType_128BitSecKey:
b := value.([16]byte)
c.Bytes(b[:])
case ZclDataTypeUnknown:
}
}
// WARNING: this is called magically from dyrkin/bin
func (a *Attribute) Deserialize(r io.Reader) {
c := composer.NewWithR(r)
a.DataType, a.Value = readAttribute(c)
}
func readAttribute(c *composer.Composer) (dataType ZclDataType, value interface{}) {
dt, _ := c.ReadByte()
dataType = ZclDataType(dt)
switch dataType {
case ZclDataTypeNoData:
value = nil
case ZclDataTypeData8:
var buf [1]byte
c.ReadBuf(buf[:])
value = buf
case ZclDataTypeData16:
var buf [2]byte
c.ReadBuf(buf[:])
value = buf
case ZclDataTypeData24:
var buf [3]byte
c.ReadBuf(buf[:])
value = buf
case ZclDataTypeData32:
var buf [4]byte
c.ReadBuf(buf[:])
value = buf
case ZclDataTypeData40:
var buf [5]byte
c.ReadBuf(buf[:])
value = buf
case ZclDataTypeData48:
var buf [6]byte
c.ReadBuf(buf[:])
value = buf
case ZclDataTypeData56:
var buf [7]byte
c.ReadBuf(buf[:])
value = buf
case ZclDataTypeData64:
var buf [8]byte
c.ReadBuf(buf[:])
value = buf
case ZclDataTypeBoolean:
b, _ := c.ReadByte()
value = b > 0
case ZclDataTypeBitmap8:
value = c.ReadUint(binary.LittleEndian, 1)
case ZclDataTypeBitmap16:
value = c.ReadUint(binary.LittleEndian, 2)
case ZclDataTypeBitmap24:
value = c.ReadUint(binary.LittleEndian, 3)
case ZclDataTypeBitmap32:
value = c.ReadUint(binary.LittleEndian, 4)
case ZclDataTypeBitmap40:
value = c.ReadUint(binary.LittleEndian, 5)
case ZclDataTypeBitmap48:
value = c.ReadUint(binary.LittleEndian, 6)
case ZclDataTypeBitmap56:
value = c.ReadUint(binary.LittleEndian, 7)
case ZclDataTypeBitmap64:
value = c.ReadUint(binary.LittleEndian, 8)
case ZclDataTypeUint8:
value = c.ReadUint(binary.LittleEndian, 1)
case ZclDataTypeUint16:
value = c.ReadUint(binary.LittleEndian, 2)
case ZclDataTypeUint24:
value = c.ReadUint(binary.LittleEndian, 3)
case ZclDataTypeUint32:
value = c.ReadUint(binary.LittleEndian, 4)
case ZclDataTypeUint40:
value = c.ReadUint(binary.LittleEndian, 5)
case ZclDataTypeUint48:
value = c.ReadUint(binary.LittleEndian, 6)
case ZclDataTypeUint56:
value = c.ReadUint(binary.LittleEndian, 7)
case ZclDataTypeUint64:
value = c.ReadUint(binary.LittleEndian, 8)
case ZclDataTypeInt8:
value = c.ReadInt(binary.LittleEndian, 1)
case ZclDataTypeInt16:
value = c.ReadInt(binary.LittleEndian, 2)
case ZclDataTypeInt24:
value = c.ReadInt(binary.LittleEndian, 3)
case ZclDataTypeInt32:
value = c.ReadInt(binary.LittleEndian, 4)
case ZclDataTypeInt40:
value = c.ReadInt(binary.LittleEndian, 5)
case ZclDataTypeInt48:
value = c.ReadInt(binary.LittleEndian, 6)
case ZclDataTypeInt56:
value = c.ReadInt(binary.LittleEndian, 7)
case ZclDataTypeInt64:
value = c.ReadInt(binary.LittleEndian, 8)
case ZclDataTypeEnum8:
value = c.ReadUint(binary.LittleEndian, 1)
case ZclDataTypeEnum16:
value = c.ReadUint(binary.LittleEndian, 2)
case ZclDataTypeSemiPrec:
case ZclDataTypeSinglePrec:
case ZclDataTypeDoublePrec:
case ZclDataTypeOctetStr:
len, _ := c.ReadByte()
value, _ = c.ReadString(int(len))
case ZclDataTypeCharStr:
len, _ := c.ReadByte()
value, _ = c.ReadString(int(len))
case ZclDataTypeLongOctetStr:
len, _ := c.ReadUint16le()
value, _ = c.ReadString(int(len))
case ZclDataTypeLongCharStr:
len, _ := c.ReadUint16le()
value, _ = c.ReadString(int(len))
case ZclDataTypeArray, ZclDataTypeSet, ZclDataTypeBag:
len, _ := c.ReadUint16le()
arr := make([]*Attribute, len)
for i := 0; i < int(len); i++ {
attribute := &Attribute{}
attribute.DataType, attribute.Value = readAttribute(c)
arr[i] = attribute
}
value = arr
case ZclDataTypeStruct:
case ZclDataTypeTod:
hours, _ := c.ReadUint8()
minutes, _ := c.ReadUint8()
seconds, _ := c.ReadUint8()
hundredths, _ := c.ReadUint8()
value = &TimeOfDay{hours, minutes, seconds, hundredths}
case ZclDataTypeDate:
year, _ := c.ReadUint8()
month, _ := c.ReadUint8()
dayOfMonth, _ := c.ReadUint8()
dayOfWeek, _ := c.ReadUint8()
value = &Date{year, month, dayOfMonth, dayOfWeek}
case ZclDataTypeUtc:
value, _ = c.ReadUint32le()
case ZclDataTypeClusterId:
value, _ = c.ReadUint16le()
case ZclDataTypeAttrId:
value, _ = c.ReadUint16le()
case ZclDataTypeBacOid:
value, _ = c.ReadUint32le()
case ZclDataTypeIeeeAddr:
v, _ := c.ReadUint64le()
value, _ = binstruct.UintToHexString(v, 8)
case ZclDataType_128BitSecKey:
var key [16]byte
_ = c.ReadBuf(key[:])
value = key
case ZclDataTypeUnknown:
}
return
}
func flag(boolean bool) uint8 {
if boolean {
return 1
}
return 0
}