-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathzcl.go
207 lines (191 loc) · 6.84 KB
/
zcl.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
package zcl
import (
"fmt"
"github.com/dyrkin/bin"
"github.com/dyrkin/zcl-go/cluster"
"github.com/dyrkin/zcl-go/frame"
"github.com/dyrkin/zcl-go/reflection"
"github.com/dyrkin/znp-go"
)
type CommandExtractor func(commandDescriptors map[uint8]*cluster.CommandDescriptor) (uint8, *cluster.CommandDescriptor, error)
type ClusterQuery func(c map[cluster.ClusterId]*cluster.Cluster) (cluster.ClusterId, *cluster.Cluster, error)
type CommandQuery func(c *cluster.Cluster) (uint8, *cluster.CommandDescriptor, error)
type ZclFrameControl struct {
FrameType frame.FrameType
ManufacturerSpecific bool
Direction frame.Direction
DisableDefaultResponse bool
}
type ZclFrame struct {
FrameControl *ZclFrameControl
ManufacturerCode uint16
TransactionSequenceNumber uint8
CommandIdentifier uint8
CommandName string
Command interface{}
}
type ZclIncomingMessage struct {
GroupID uint16
ClusterID uint16
SrcAddr string
SrcEndpoint uint8
DstEndpoint uint8
WasBroadcast bool
LinkQuality uint8
SecurityUse bool
Timestamp uint32
TransactionSeqNumber uint8
Data *ZclFrame
}
type Zcl struct {
library *cluster.ClusterLibrary
}
func New() *Zcl {
return &Zcl{cluster.New()}
}
func (z *Zcl) ToZclIncomingMessage(m *znp.AfIncomingMessage) (*ZclIncomingMessage, error) {
im := &ZclIncomingMessage{}
im.GroupID = m.GroupID
im.ClusterID = m.ClusterID
im.SrcAddr = m.SrcAddr
im.SrcEndpoint = m.SrcEndpoint
im.DstEndpoint = m.DstEndpoint
im.WasBroadcast = m.WasBroadcast > 0
im.LinkQuality = m.LinkQuality
im.SecurityUse = m.SecurityUse > 0
im.Timestamp = m.Timestamp
im.TransactionSeqNumber = m.TransSeqNumber
data, err := z.toZclFrame(m.Data, m.ClusterID)
im.Data = data
return im, err
}
func (z *Zcl) toZclFrame(data []uint8, clusterId uint16) (*ZclFrame, error) {
frame := frame.Decode(data)
f := &ZclFrame{}
f.FrameControl = z.toZclFrameControl(frame.FrameControl)
f.ManufacturerCode = frame.ManufacturerCode
f.TransactionSequenceNumber = frame.TransactionSequenceNumber
f.CommandIdentifier = frame.CommandIdentifier
cmd, name, err := z.toZclCommand(clusterId, frame)
f.CommandName = name
f.Command = cmd
return f, err
}
func (z *Zcl) toZclCommand(clusterId uint16, f *frame.Frame) (interface{}, string, error) {
var cd *cluster.CommandDescriptor
var ok bool
switch f.FrameControl.FrameType {
case frame.FrameTypeGlobal:
if cd, ok = z.library.Global()[f.CommandIdentifier]; !ok {
return nil, "", fmt.Errorf("unsupported global cmd identifier %d", f.CommandIdentifier)
}
cmd := cd.Command
copy := reflection.Copy(cmd)
bin.Decode(f.Payload, copy)
z.patchName(copy, clusterId, f.CommandIdentifier)
return copy, cd.Name, nil
case frame.FrameTypeLocal:
var c *cluster.Cluster
if c, ok = z.library.Clusters()[cluster.ClusterId(clusterId)]; !ok {
return nil, "", fmt.Errorf("unknown cluster %d", clusterId)
}
var commandDescriptors map[uint8]*cluster.CommandDescriptor
switch f.FrameControl.Direction {
case frame.DirectionClientServer:
commandDescriptors = c.CommandDescriptors.Received
case frame.DirectionServerClient:
commandDescriptors = c.CommandDescriptors.Generated
}
if cd, ok = commandDescriptors[f.CommandIdentifier]; !ok {
return nil, "", fmt.Errorf("cluster %d doesn't support this cmd %d", clusterId, f.CommandIdentifier)
}
cmd := cd.Command
copy := reflection.Copy(cmd)
bin.Decode(f.Payload, copy)
return copy, cd.Name, nil
}
return nil, "", fmt.Errorf("unknown frame type")
}
func (z *Zcl) patchName(cmd interface{}, clusterId uint16, commandId uint8) {
switch cmd := cmd.(type) {
case *cluster.ReadAttributesResponse:
for _, v := range cmd.ReadAttributeStatuses {
v.AttributeName = z.getAttributeName(clusterId, v.AttributeID)
}
case *cluster.WriteAttributesCommand:
for _, v := range cmd.WriteAttributeRecords {
v.AttributeName = z.getAttributeName(clusterId, v.AttributeID)
}
case *cluster.WriteAttributesUndividedCommand:
for _, v := range cmd.WriteAttributeRecords {
v.AttributeName = z.getAttributeName(clusterId, v.AttributeID)
}
case *cluster.WriteAttributesNoResponseCommand:
for _, v := range cmd.WriteAttributeRecords {
v.AttributeName = z.getAttributeName(clusterId, v.AttributeID)
}
case *cluster.WriteAttributesResponse:
for _, v := range cmd.WriteAttributeStatuses {
v.AttributeName = z.getAttributeName(clusterId, v.AttributeID)
}
case *cluster.ConfigureReportingCommand:
for _, v := range cmd.AttributeReportingConfigurationRecords {
v.AttributeName = z.getAttributeName(clusterId, v.AttributeID)
}
case *cluster.ConfigureReportingResponse:
for _, v := range cmd.AttributeStatusRecords {
v.AttributeName = z.getAttributeName(clusterId, v.AttributeID)
}
case *cluster.ReadReportingConfigurationCommand:
for _, v := range cmd.AttributeRecords {
v.AttributeName = z.getAttributeName(clusterId, v.AttributeID)
}
case *cluster.ReadReportingConfigurationResponse:
for _, v := range cmd.AttributeReportingConfigurationResponseRecords {
v.AttributeName = z.getAttributeName(clusterId, v.AttributeID)
}
case *cluster.ReportAttributesCommand:
for _, v := range cmd.AttributeReports {
v.AttributeName = z.getAttributeName(clusterId, v.AttributeID)
}
case *cluster.DiscoverAttributesResponse:
for _, v := range cmd.AttributeInformations {
v.AttributeName = z.getAttributeName(clusterId, v.AttributeID)
}
case *cluster.ReadAttributesStructuredCommand:
for _, v := range cmd.AttributeSelectors {
v.AttributeName = z.getAttributeName(clusterId, v.AttributeID)
}
case *cluster.WriteAttributesStructuredCommand:
for _, v := range cmd.WriteAttributeStructuredRecords {
v.AttributeName = z.getAttributeName(clusterId, v.AttributeID)
}
case *cluster.WriteAttributesStructuredResponse:
for _, v := range cmd.WriteAttributeStatusRecords {
v.AttributeName = z.getAttributeName(clusterId, v.AttributeID)
}
case *cluster.DiscoverAttributesExtendedResponse:
for _, v := range cmd.ExtendedAttributeInformations {
v.AttributeName = z.getAttributeName(clusterId, v.AttributeID)
}
}
}
func (z *Zcl) getAttributeName(clusterId uint16, attributeId uint16) string {
if cluster, ok := z.library.Clusters()[cluster.ClusterId(clusterId)]; ok {
if attributeDescriptor, ok := cluster.AttributeDescriptors[attributeId]; ok {
return attributeDescriptor.Name
}
}
return ""
}
func (z *Zcl) toZclFrameControl(frameControl *frame.FrameControl) *ZclFrameControl {
fc := &ZclFrameControl{}
fc.FrameType = frameControl.FrameType
fc.ManufacturerSpecific = frameControl.ManufacturerSpecific > 0
fc.Direction = frameControl.Direction
fc.DisableDefaultResponse = frameControl.DisableDefaultResponse > 0
return fc
}
func (z *Zcl) ClusterLibrary() *cluster.ClusterLibrary {
return z.library
}