-
Notifications
You must be signed in to change notification settings - Fork 0
/
packet.go
66 lines (51 loc) · 1.37 KB
/
packet.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
package insteon
import "fmt"
type packet struct {
CommandCode CommandCode
Payload []byte
Ack byte
}
// UnmarshalBinary -
func (p *packet) UnmarshalBinary(b []byte) error {
if b[0] != messageStart {
return fmt.Errorf("expected %02x but got %02x", messageStart, b[0])
}
p.CommandCode = CommandCode(b[1])
// Always treat outgoing messages as if they started with the sender
// identity so that they can be parsed as messages.
if p.CommandCode == cmdSendStandardOrExtendedMessage {
extB := make([]byte, len(b)+3)
copy(extB[3:], b)
b = extB
}
p.Payload = make([]byte, len(b)-2)
copy(p.Payload, b[2:len(b)])
if isOutgoingCommandCode(p.CommandCode) {
p.Ack = p.Payload[len(p.Payload)-1]
p.Payload = p.Payload[:len(p.Payload)-1]
}
return nil
}
// MarshalBinary -
func (p packet) MarshalBinary() ([]byte, error) {
payload := p.Payload
// Strip the sender identity for outgoing messages as it must not really be
// specified.
if p.CommandCode == cmdSendStandardOrExtendedMessage {
payload = payload[3:]
}
b := make([]byte, len(payload)+2)
b[0] = messageStart
b[1] = byte(p.CommandCode)
copy(b[2:len(payload)+2], payload)
if isOutgoingCommandCode(p.CommandCode) && p.Ack != 0 {
b = append(b, p.Ack)
}
return b, nil
}
func (p packet) IsAck() bool {
return p.Ack == messageAck
}
func (p packet) IsNak() bool {
return p.Ack == messageNak
}