-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunmarshal.go
57 lines (46 loc) · 1.62 KB
/
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
package zcl
import (
"errors"
"fmt"
"github.com/shimmeringbee/bytecodec"
"github.com/shimmeringbee/bytecodec/bitbuffer"
"github.com/shimmeringbee/zigbee"
)
func (cr *CommandRegistry) Unmarshal(appMsg zigbee.ApplicationMessage) (Message, error) {
header := Header{}
var command interface{}
bb := bitbuffer.NewBitBufferFromBytes(appMsg.Data)
if err := bytecodec.UnmarshalFromBitBuffer(bb, &header); err != nil {
return Message{}, err
}
switch header.Control.FrameType {
case FrameGlobal:
foundCommand, err := cr.GetGlobalCommand(header.CommandIdentifier)
if err != nil {
return Message{}, fmt.Errorf("unknown ZCL global command identifier received: %d", header.CommandIdentifier)
}
command = foundCommand
case FrameLocal:
foundCommand, err := cr.GetLocalCommand(appMsg.ClusterID, header.Manufacturer, header.Control.Direction, header.CommandIdentifier)
if err != nil {
return Message{}, fmt.Errorf("unknown ZCL local command identifier received: %d", header.CommandIdentifier)
}
command = foundCommand
default:
return Message{}, errors.New("unknown frame type encountered")
}
if err := bytecodec.UnmarshalFromBitBuffer(bb, command); err != nil {
return Message{}, err
}
return Message{
FrameType: header.Control.FrameType,
Direction: header.Control.Direction,
TransactionSequence: header.TransactionSequence,
Manufacturer: header.Manufacturer,
ClusterID: appMsg.ClusterID,
SourceEndpoint: appMsg.SourceEndpoint,
DestinationEndpoint: appMsg.DestinationEndpoint,
CommandIdentifier: header.CommandIdentifier,
Command: command,
}, nil
}