This repository was archived by the owner on Feb 23, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathresponse.go
More file actions
87 lines (67 loc) · 1.65 KB
/
response.go
File metadata and controls
87 lines (67 loc) · 1.65 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"errors"
"fmt"
)
type ResponseStructure struct {
StatusByte uint8
DataStructures []DataStructure
}
func (rs ResponseStructure) String() string {
return fmt.Sprintf("ResponseStructure {StatusByte: %2x []DataStructures: %v}", rs.StatusByte, rs.DataStructures)
}
type DataStructure struct {
Identifier uint8
Data []byte
}
func (ds DataStructure) String() string {
return fmt.Sprintf("DataStructure {Identifier: %2x Data: %v}", ds.Identifier, hex(ds.Data))
}
// [ 1 f1 81 a0 3 0 0 0 22 f2 a6 f2 71 f2 f2 0 0 0 0 0 0]
func ParseResponse(frame []byte) (ResponseStructure, error) {
c := 1 // Skip USB byte...
rs := ResponseStructure{}
if len(frame) < 4 {
return rs, errors.New("Frame to small")
}
if frame[c] != StandardStartFlag {
return rs, errors.New("Bad start of frame")
}
c++
var runningChecksum byte
runningChecksum ^= frame[c]
rs.StatusByte = uint8(frame[c])
c++
var checksum byte
rs.DataStructures = make([]DataStructure, 0)
for frame[c] != StopFlag {
// Unstuff
if frame[c] == StuffFlag {
c++
frame[c] = (0xff & (0xff << 2)) | frame[c]
}
if frame[c+1] == StopFlag {
checksum = frame[c]
break
}
ds := DataStructure{}
runningChecksum ^= frame[c]
ds.Identifier = uint8(frame[c])
c++
runningChecksum ^= frame[c]
dataByteCount := uint8(frame[c])
c++
if dataByteCount > 0 {
ds.Data = frame[c : c+int(dataByteCount)]
for _, b := range ds.Data {
runningChecksum ^= b
}
c += int(dataByteCount)
}
rs.DataStructures = append(rs.DataStructures, ds)
}
if runningChecksum != checksum {
return rs, errors.New("Checksum did not match")
}
return rs, nil
}