-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathzdpframe.go
62 lines (53 loc) · 1.44 KB
/
zdpframe.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 (
"fmt"
)
type zdpFrame struct {
Packet
seq uint8
data []byte
}
func (f *zdpFrame) String() string {
return fmt.Sprintf(
"%v [ZDP] Seq[%d]",
f.Packet,
f.seq,
)
}
type recvZdpFrame struct {
zdpFrame
}
func (f *zdpFrame) MarshalZcl() ([]byte, error) {
return append([]byte{f.seq}, f.data...), nil
}
func (f *zdpFrame) UnmarshalZcl(b []byte) ([]byte, error) {
if len(b) < 1 {
return b, ErrNotEnoughData
}
f.seq = b[0]
f.data = b[1:]
return nil, nil
}
func (f *zdpFrame) SeqNo() uint8 { return f.seq }
func (f *zdpFrame) Command() ZdoCmdID { return ZdoCmdID(f.Cluster()) }
func (f *zdpFrame) Payload() []byte { return f.data }
func (f *recvZdpFrame) SrcAddr() Address { return f.Packet.(ReceivedPacket).SrcAddr() }
func (f *recvZdpFrame) LQI() uint8 { return f.Packet.(ReceivedPacket).LQI() }
func (f *recvZdpFrame) RSSI() int8 { return f.Packet.(ReceivedPacket).RSSI() }
func (f *recvZdpFrame) Response(cmd ZdoCommand) (frame ZdpFrame, err error) {
nf := &zdpFrame{seq: f.seq}
frame = nf
if nf.data, err = cmd.MarshalZcl(); err != nil {
return nil, err
}
nf.Packet, err = NewPacket(0, f.SrcAddr(), 0, 0, cmd.Cluster(), nf)
return
}
func NewZdpFrame(seq uint8, dstAddr Address, cmd ZdoCommand) (frame ZdpFrame, err error) {
nf := &zdpFrame{seq: seq}
if nf.data, err = cmd.MarshalZcl(); err != nil {
return nil, err
}
nf.Packet, err = NewPacket(0, dstAddr, 0, 0, cmd.Cluster(), nf)
return nf, nil
}