-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpayload.go
More file actions
68 lines (57 loc) · 1.49 KB
/
payload.go
File metadata and controls
68 lines (57 loc) · 1.49 KB
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
package sphero
import (
"encoding/hex"
"fmt"
)
type Payload struct {
Flags uint8
DeviceID uint8
Command uint8
Sequence uint8
Error uint8
Payload []byte
}
func (p *Payload) Encode() []byte {
sendBytes := []byte{
DataPacketStart, // first byte is always 0x8d
p.Flags, // set the flags
p.DeviceID, // send is for the given device id
p.Command, // with the command
p.Sequence, // set the sequence id to ensure that packets are orderable
}
sendBytes = append(sendBytes, p.Payload...)
cs := calculateChecksum(sendBytes[1:])
sendBytes = append(sendBytes, cs, DataPacketEnd)
return sendBytes
}
// Decode expects the entire packet, include start, header, payload, checksum, and end bytes.
func (p *Payload) Decode(d []byte) error {
p.Flags = d[1]
p.DeviceID = d[2]
p.Command = d[3]
p.Sequence = d[4]
checksum := d[len(d)-2]
cc := calculateChecksum(d[1 : len(d)-2])
if checksum != cc {
return fmt.Errorf("decode checksum for %s invalid: expected %x, received: %x", hex.EncodeToString(d), cc, checksum)
}
if len(d) > 7 {
p.Payload = d[5 : len(d)-2]
}
return nil
}
func (p *Payload) String() string {
return fmt.Sprintf("Flags: %d, DeviceID: %d, Command: %d, Sequence: %d, Payload: %s",
p.Flags,
p.DeviceID,
p.Command,
p.Sequence,
hex.EncodeToString(p.Payload))
}
func calculateChecksum(b []byte) uint8 {
var calculatedChecksum uint16
for i := range b {
calculatedChecksum += uint16(b[i])
}
return uint8(^(calculatedChecksum % 256))
}