/
mode_cxr.go
96 lines (90 loc) · 2.56 KB
/
mode_cxr.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
package main
import (
"bytes"
"encoding/hex"
"errors"
"fmt"
"log"
"strconv"
"strings"
)
func (sc syscon) sendCXRCommand(cmd string) {
checksum := countChecksum(cmd)
fcmd := fmt.Sprintf("C:%02X:%s", checksum, cmd)
maxSize := 15 // for some reason we need to split every 15 symbols
length := len(fcmd)
var j int
for i := 0; i < length; i += maxSize {
j += maxSize
if j > length {
j = length
}
fmt.Println(fcmd[i:j])
sc.writeCommand(fcmd[i:j])
}
sc.writeCommand("\r\n")
}
func (sc syscon) receiveCXRCommand() (string, error) {
buff := make([]byte, 1000)
n, err := sc.port.Read(buff)
fmt.Printf("Read %v bytes\n", n)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(buff[:n]))
respRaw := strings.TrimSpace(string(buff[:n]))
resp := strings.Split(respRaw, ":")
if len(resp) != 3 {
fmt.Println("wrong response length")
return "", errors.New("wrong response length")
}
if resp[0] != "R" && resp[0] != "E" {
fmt.Println("magic?")
return "", errors.New("magic?")
}
if resp[1] != fmt.Sprintf("%02X", countChecksum(resp[2])) {
fmt.Println("wrong checksum")
return "", errors.New("wrong chechsum")
}
respData := strings.Split(resp[2], " ")
if resp[0] == "R" && len(respData) < 2 || resp[0] == "E" && len(respData) != 2 {
fmt.Println("wrong data length")
return "", errors.New("wrong data length")
}
if respData[0] != "OK" || len(respData) < 2 {
respCode, err := strconv.Atoi(respData[1])
if err == nil && respCode == 0 {
return respData[2], nil
}
return "", errors.New("wrong response code")
}
respCode, err := strconv.Atoi(respData[1])
if err == nil && respCode == 0 {
return respData[2], nil
}
return "", errors.New("wrong response code")
}
func (sc syscon) commandCXRAuth() string {
res, err := sc.proccessCommand("AUTH1 " + auth)
resHex, _ := hex.DecodeString(res)
if err != nil || !bytes.Equal(resHex[0:0x10], auth1ResponseHeader) {
return "Wrong Auth1 response header"
}
fmt.Println("Right Auth1 response header")
data := decode(resHex[0x10:0x40])
if !bytes.Equal(data[0x8:0x10], zero[0x0:0x8]) || !bytes.Equal(data[0x10:0x20], auth1Response) || !bytes.Equal(data[0x20:0x30], zero) {
return "Wrong Auth1 response body"
}
fmt.Println("Right Auth1 response body")
newData := append(data[0x8:0x10], data[0x0:0x8]...)
newData = append(newData, zero...)
newData = append(newData, zero...)
auth2Body := encode(newData)
authBody := append(auth2RequestHeader, auth2Body...)
com := fmt.Sprintf("AUTH2 %02X", authBody)
_, err = sc.proccessCommand(com)
if err != nil {
return "Auth failed"
}
return "Auth successful"
}