-
Notifications
You must be signed in to change notification settings - Fork 2
/
zclframe.go
162 lines (148 loc) · 4.27 KB
/
zclframe.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
package zcl
import (
"errors"
"fmt"
)
type zclFrame struct {
Packet
disableDefaultResponse bool
direction Direction
mnfCode Zu16
cmdType CommandType
seq uint8
commandID CommandID
data []byte
}
type recvZclFrame struct {
zclFrame
}
func (f *zclFrame) String() string {
mnf := ""
if f.mnfCode > 0 {
mnf = fmt.Sprintf("MNF[%04X] ", uint16(f.mnfCode))
}
return fmt.Sprintf(
"%v [ZCL] %s %s%s Cmd[0x%02X] Seq[%d]",
f.Packet,
f.direction,
mnf,
f.cmdType,
uint8(f.commandID),
f.seq,
)
}
func (f *zclFrame) MarshalZcl() ([]byte, error) {
data := []byte{uint8(f.cmdType) & 0x03}
if f.direction {
data[0] = data[0] | 0x08
}
if f.disableDefaultResponse {
data[0] = data[0] | 0x10
}
if f.mnfCode > 0 {
data[0] = data[0] | 0x04
t, _ := f.mnfCode.MarshalZcl()
data = append(data, t...)
}
data = append(data, f.seq, uint8(f.commandID))
data = append(data, f.data...)
return data, nil
}
func (f *zclFrame) UnmarshalZcl(b []byte) ([]byte, error) {
var err error
if len(b) < 3 {
return nil, errors.New("too few bytes(frame:0)")
}
st := b[0]
b = b[1:]
f.cmdType = CommandType(st & 0x03)
f.direction = st&0x08 == 0x08 // 1 = Server to Client
f.disableDefaultResponse = st&0x10 == 0x10
if st&0x04 == 0x04 {
if b, err = (&f.mnfCode).UnmarshalZcl(b); err != nil {
return b, err
}
}
f.seq = b[0]
f.commandID = CommandID(b[1])
f.data = b[2:]
return nil, nil
}
func (f *zclFrame) SeqNo() uint8 { return f.seq }
func (f *zclFrame) Direction() Direction { return f.direction }
func (f *zclFrame) Manufacturer() uint16 { return uint16(f.mnfCode) }
func (f *zclFrame) DisableDefaultResponse() bool { return f.disableDefaultResponse }
func (f *zclFrame) Command() CommandID { return f.commandID }
func (f *zclFrame) Payload() []byte { return f.data }
func (f *zclFrame) CommandType() CommandType { return f.cmdType }
func (f *recvZclFrame) SrcAddr() Address { return f.Packet.(ReceivedPacket).SrcAddr() }
func (f *recvZclFrame) LQI() uint8 { return f.Packet.(ReceivedPacket).LQI() }
func (f *recvZclFrame) RSSI() int8 { return f.Packet.(ReceivedPacket).RSSI() }
func (f *recvZclFrame) Response(srcEp uint8, cmd General) (frame ZclFrame, err error) {
nf := &zclFrame{
disableDefaultResponse: true,
direction: !f.direction,
cmdType: f.cmdType,
mnfCode: f.mnfCode,
seq: f.seq,
commandID: cmd.ID(),
}
frame = nf
if c, ok := cmd.(Command); ok {
if nf.cmdType != 1 {
return nil, ErrResponseClusterSpecific
}
if c.Cluster() != f.Cluster() {
return nil, ErrResponseWrongCluster
}
if c.MnfCode() != uint16(f.mnfCode) {
return nil, ErrResponseWrongMnfCode
}
} else if cmd.ID() != 0x0b { // if not Default Response
if nf.cmdType != 0 {
return nil, ErrResponseProfileWide
}
} else {
nf.cmdType = 0
}
if nf.data, err = cmd.MarshalZcl(); err != nil {
return nil, err
}
nf.Packet, err = NewPacket(f.DstEP(), f.SrcAddr(), srcEp, f.Profile(), f.Cluster(), nf)
return
}
func NewProfileFrame(seq uint8, srcEp uint8, dstAddr Address, dstEp uint8, profile ProfileID, cluster ClusterID, cmd General) (frame ZclFrame, err error) {
if _, ok := cmd.(Command); ok {
return nil, ErrCommandClusterSpecific
}
nf := &zclFrame{
seq: seq,
disableDefaultResponse: false,
direction: cmd.Direction(),
cmdType: 0,
mnfCode: 0,
commandID: cmd.ID(),
}
frame = nf
if nf.data, err = cmd.MarshalZcl(); err != nil {
return nil, err
}
nf.Packet, err = NewPacket(srcEp, dstAddr, dstEp, profile, cluster, nf)
return
}
func NewClusterFrame(seq uint8, srcEp uint8, dstAddr Address, dstEp uint8, profile ProfileID, cmd Command) (frame ZclFrame, err error) {
nf := &zclFrame{
seq: seq,
disableDefaultResponse: false,
direction: cmd.Direction(),
cmdType: 1,
mnfCode: Zu16(cmd.MnfCode()),
commandID: cmd.ID(),
}
frame = nf
if nf.data, err = cmd.MarshalZcl(); err != nil {
return nil, err
}
nf.Packet, err = NewPacket(srcEp, dstAddr, dstEp, profile, cmd.Cluster(), nf)
return
}