-
Notifications
You must be signed in to change notification settings - Fork 4
/
util.go
55 lines (47 loc) · 959 Bytes
/
util.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
package broadlink
import (
"fmt"
)
// Broadlink byte checksum
func checksum(data []byte) (sum uint16) {
sum = 0xbeaf
for _, b := range data {
sum += uint16(b)
}
return
}
// verify checksum of a packet
func packetChecksumOK(packet []byte) bool {
if len(packet) < 0x22 {
return false
}
// checksum in data packet
packetsum := uint16(packet[0x21])<<8 | uint16(packet[0x20])
// calculated checksum
sum := checksum(packet)
sum -= uint16(packet[0x20]) // remove checksum value bytes
sum -= uint16(packet[0x21])
return sum == packetsum
}
//!! debug debug debug
func printhex(data []byte) {
fmt.Printf(">00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f\n -----------------------------------------------\n ")
i := 0
for _, b := range data {
fmt.Printf("%02x", b)
i++
switch i {
case 8:
fmt.Printf(",")
case 16:
fmt.Printf("\n ")
i = 0
default:
fmt.Printf(" ")
}
}
if i != 0 {
fmt.Println()
}
fmt.Println()
}