-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmarshal.go
62 lines (50 loc) · 1.63 KB
/
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
package zcl
import (
"errors"
"github.com/shimmeringbee/bytecodec"
"github.com/shimmeringbee/bytecodec/bitbuffer"
"github.com/shimmeringbee/zigbee"
)
func (cr *CommandRegistry) Marshal(message Message) (zigbee.ApplicationMessage, error) {
bb := bitbuffer.NewBitBuffer()
header := Header{
Control: Control{
Reserved: 0,
DisableDefaultResponse: false,
Direction: message.Direction,
ManufacturerSpecific: message.isManufacturerSpecific(),
FrameType: message.FrameType,
},
Manufacturer: message.Manufacturer,
TransactionSequence: message.TransactionSequence,
}
switch message.FrameType {
case FrameGlobal:
commandId, err := cr.GetGlobalCommandIdentifier(message.Command)
if err != nil {
return zigbee.ApplicationMessage{}, err
}
header.CommandIdentifier = commandId
case FrameLocal:
commandId, err := cr.GetLocalCommandIdentifier(message.ClusterID, message.Manufacturer, message.Direction, message.Command)
if err != nil {
return zigbee.ApplicationMessage{}, err
}
header.CommandIdentifier = commandId
default:
return zigbee.ApplicationMessage{}, errors.New("unknown frame type encountered")
}
if err := bytecodec.MarshalToBitBuffer(bb, header); err != nil {
return zigbee.ApplicationMessage{}, err
}
if err := bytecodec.MarshalToBitBuffer(bb, message.Command); err != nil {
return zigbee.ApplicationMessage{}, err
}
msg := zigbee.ApplicationMessage{
ClusterID: message.ClusterID,
SourceEndpoint: message.SourceEndpoint,
DestinationEndpoint: message.DestinationEndpoint,
Data: bb.Bytes(),
}
return msg, nil
}