/
protocol.go
150 lines (130 loc) · 4.03 KB
/
protocol.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package norn
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"hash/crc32"
"io"
"strconv"
"strings"
)
type Command struct {
Channel uint `json:"channel"`
Type CommandType `json:"type"`
Status bool `json:"status"`
}
type Response struct {
Success bool `json:"isOk"`
Message string `json:"message"`
Channel uint `json:"channel"`
MaxChannel int `json:"max_channel"`
Status bool `json:"status"`
}
type CommandType uint
const (
Nil CommandType = 0
Get CommandType = 1
Set CommandType = 2
Version CommandType = 3
Bootloader CommandType = 4
MaxChannels CommandType = 5
)
var (
crc32c = crc32.MakeTable(crc32.Castagnoli)
marker = []byte("E979#")
)
func protocolParser(reader *bufio.Reader) ([]byte, error) {
markerBuffer := make([]byte, len(marker))
if _, err := io.ReadFull(reader, markerBuffer); err != nil {
return nil, err
}
if !bytes.Equal(markerBuffer, marker) {
return nil, fmt.Errorf("invalid marker detected %s != %s", string(markerBuffer), string(marker))
}
const prefixLength = 9
_, err := reader.Peek(prefixLength)
if err != nil {
return nil, err
}
lengthBuffer, err := reader.ReadString(byte('#'))
if err != nil {
return nil, err
}
lengthBuffer = strings.TrimSuffix(lengthBuffer, "#")
mumPrefixBytes := len(lengthBuffer)
if mumPrefixBytes != prefixLength {
return nil, fmt.Errorf("invalid prefix length: %d expected: %d, buffer: %s", len(lengthBuffer), prefixLength, lengthBuffer)
}
crcBuffer := make([]byte, 8)
if _, err := io.ReadFull(reader, crcBuffer); err != nil {
return nil, err
}
crcOfInputData := crc32.Checksum([]byte(lengthBuffer), crc32c)
crc, err := strconv.ParseUint(string(crcBuffer), 16, 32)
if err != nil {
return nil, fmt.Errorf("unable to convert the CRC checksum: %w", err)
}
if crcOfInputData != uint32(crc) {
return nil, fmt.Errorf("crc length field checksum do not match %08x != %08x: %w", crcOfInputData, uint32(crc), err)
}
numBytes, err := strconv.Atoi(lengthBuffer)
if err != nil {
return nil, err
}
dataBuffer := make([]byte, numBytes)
if _, err := io.ReadFull(reader, dataBuffer); err != nil {
return nil, fmt.Errorf("unable to read the data buffer: %w", err)
}
if _, err := io.ReadFull(reader, crcBuffer); err != nil {
return nil, fmt.Errorf("unable to read the crc buffer: %w", err)
}
crcOfInputData = crc32.Checksum(dataBuffer, crc32c)
crc, err = strconv.ParseUint(string(crcBuffer), 16, 32)
if err != nil {
return nil, fmt.Errorf("unable to convert the CRC checksum: %w", err)
}
if crcOfInputData != uint32(crc) {
return nil, fmt.Errorf("crc checksums do not match %08x != %08x: %w", crcOfInputData, uint32(crc), err)
}
return dataBuffer, nil
}
func (command *Command) Marshal() []byte {
msg, _ := json.Marshal(command)
length := fmt.Sprintf("%09d", len(msg))
crcLength := fmt.Sprintf("%08x", crc32.Checksum([]byte(length), crc32c))
payload := string(msg)
crcPayload := fmt.Sprintf("%08x", crc32.Checksum(msg, crc32c))
data := fmt.Sprintf("%s%s#%s%s%s", string(marker), length, crcLength, payload, crcPayload)
return []byte(data)
}
func (command *Command) Parse(reader *bufio.Reader) error {
data, err := protocolParser(reader)
if err != nil {
return fmt.Errorf("error while command parsing: %w", err)
}
return json.Unmarshal(data, command)
}
func MarshalError(err error) string {
res := Response{
Success: false,
Message: err.Error(),
}
return string(res.Marshal())
}
func (response *Response) Marshal() []byte {
msg, _ := json.Marshal(response)
length := fmt.Sprintf("%09d", len(msg))
crcString := fmt.Sprintf("%08x", crc32.Checksum([]byte(length), crc32c))
payload := string(msg)
crcPayload := fmt.Sprintf("%08x", crc32.Checksum(msg, crc32c))
data := fmt.Sprintf("%s%s#%s%s%s", string(marker), length, crcString, payload, crcPayload)
return []byte(data)
}
func (response *Response) Parse(reader *bufio.Reader) error {
data, err := protocolParser(reader)
if err != nil {
return fmt.Errorf("error while response parsing: %w", err)
}
return json.Unmarshal(data, response)
}