-
Notifications
You must be signed in to change notification settings - Fork 2
/
message.go
121 lines (98 loc) · 2.7 KB
/
message.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
/*
* message.go
*
* Copyright (c) 2021-2022 Stavros Avramidis (@purpl3F0x). All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*
*/
package ant
import (
"errors"
"fmt"
"strings"
)
type Packet []byte
func (p Packet) String() string {
l := len(p)
if l == 0 {
return "[]"
}
l--
var b strings.Builder
b.Grow((l)*6 + 2 + 4)
b.Write(Packet(fmt.Sprint("[")))
for i := 0; i < l; i++ {
b.Write(Packet(fmt.Sprintf("0x%02X, ", p[i])))
}
b.Write(Packet(fmt.Sprintf("0x%02X]", p[l])))
return b.String()
}
type Message struct {
Id byte
Data Packet
}
func NewMessage(id byte, data Packet) *Message {
return &Message{id, data}
}
func (m Message) length() int {
return len(m.Data) + MESG_FRAME_SIZE
}
func (m Message) String() string {
return fmt.Sprintf("AntMessage (0x%02X) %s", m.Id, m.Data)
}
func (m Message) Checksum() (checksum byte) {
n := len(m.Data)
checksum = MESG_TX_SYNC ^ byte(n) ^ m.Id
for i := 0; i < n; i++ {
checksum ^= m.Data[i]
}
return checksum
}
func (m Message) Encode() Packet {
rawLen := m.length()
msgLen := len(m.Data)
raw := make(Packet, rawLen)
raw[0] = MESG_TX_SYNC
raw[1] = byte(msgLen)
raw[2] = m.Id
copy(raw[MESG_DATA_OFFSET:], m.Data)
checksum := MESG_TX_SYNC ^ byte(msgLen) ^ m.Id
for n := 2; n < msgLen; n++ {
checksum ^= m.Data[n]
}
raw[rawLen-1] = checksum
// raw[rawLen] = 0
// raw[rawLen+1] = 0
return raw
}
func Decode(buffer Packet) (m *Message, err error) {
sync := buffer[0]
length := int(buffer[MESG_SIZE_OFFSET])
id := buffer[MESG_ID_OFFSET]
data := buffer[MESG_DATA_OFFSET : len(buffer)-1]
checksum := buffer[len(buffer)-1]
if sync != MESG_TX_SYNC {
return nil, errors.New(fmt.Sprintf("Could not decode. Expected TX sync but got 0x%.2x.", sync))
}
if len(buffer) != length+MESG_FRAME_SIZE {
return nil, errors.New(fmt.Sprintf("Could not decode. Message length should be %d but was %d.", length+MESG_FRAME_SIZE, len(data)))
}
m = NewMessage(id, data)
if checksum != m.Checksum() {
return nil, errors.New(fmt.Sprintf("Could not decode. Checksum should be 0x%.2x but was 0x%.2x.", checksum, m.Checksum()))
}
return m, nil
}