forked from filipkroca/teltonikaparser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.go
232 lines (193 loc) · 7.47 KB
/
commands.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Copyright 2022 Gábor Nyíri. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package teltonikaparser is an implementation of https://wiki.teltonika.lt/view/Codec Codec12 for UDP packets in GO Lang
// implemented https://wiki.teltonika-gps.com/view/Codec#Codec_12
package teltonikaparser
import (
"bytes"
"encoding/binary"
"fmt"
"github.com/basvdlei/gotsmart/crc16"
"unsafe"
)
const (
CodecID = 0x0C
RequestPreamble = 0x00000000
ResponsePreamble = 0x00000000
CommandTypeRequest = 0x05
CommandTypeResponse = 0x06
)
type commandRequestPre struct {
// Preamble - the packet starts with four zero bytes.
Preamble uint32
// Data Size - size is calculated from the Codec ID field to the second command quantity field.
DataSize uint32
// Codec ID - in Codec12 it is always 0x0C.
CodecID byte
// Command Quantity 1 - it is ignored when parsing the message.
CommandQuantity1 byte
// Type - it can be 0x05 to denote command or 0x06 to denote response.
Type byte
// Command Size – command length.
CommandSize uint32
}
type commandRequestPost struct {
// Command Quantity 2 - a byte which defines how many records (commands)
// are in the packet. This byte will not be parsed but it’s recommended
// that it should contain the same value as Command Quantity 1.
CommandQuantity2 byte
// Calculated from Codec ID to the Command Quantity 2. CRC (Cyclic
// Redundancy Check) is an error-detecting code using for detect
// accidental changes to RAW data. For calculation we are using CRC-16/IBM.
CRC uint32
}
type CommandRequest struct {
commandRequestPre
// Command - command in HEX.
Command []byte // dynamic long type. Needs to read separately.
commandRequestPost
}
type commandResponsePre struct {
// Preamble - the packet starts with four zero bytes.
Preamble uint32
// Data Size - size is calculated from the Codec ID field to the second command or response quantity field.
DataSize uint32
// Codec ID - in Codec12 it is always 0x0C.
CodecID byte
// Response Quantity 1 - it is ignored when parsing the message.
ResponseQuantity1 byte
// Type - it can be 0x05 to denote command or 0x06 to denote response.
Type byte
// Response Size – command or response length.
ResponseSize uint32
}
type commandResponsePost struct {
// Response Quantity 2 - a byte which defines how many records (responses) are in the packet.
// This byte will not be parsed but it’s recommended that it should contain the same value as Response Quantity 1.
ResponseQuantity2 byte
// calculated from Codec ID to the Command Quantity 2. CRC (Cyclic Redundancy Check) is an error-detecting
// code using for detect accidental changes to RAW data. For calculation we are using CRC-16/IBM.
CRC uint32
}
type CommandResponse struct {
commandResponsePre
// Response – response in HEX.
Response []byte // dynamic long type. Needs to read separately.
commandResponsePost
}
func EncodeCommandRequest(command string) ([]byte, error) {
buffer := new(bytes.Buffer)
commandRequest := CommandRequest{
commandRequestPre: commandRequestPre{
Preamble: RequestPreamble,
DataSize: uint32(7 + len(command) + 1), // 7 header bytes + actual command text + 1 byte more
CodecID: CodecID,
CommandQuantity1: 0x01,
Type: CommandTypeRequest, // 0x05 for command request, 0x06 for command response,
CommandSize: uint32(len(command)),
},
Command: []byte(command),
commandRequestPost: commandRequestPost{
CommandQuantity2: 0x01,
CRC: 0, // let's add it separately when rest of the package is ready
},
}
err := binary.Write(buffer, binary.BigEndian, commandRequest.commandRequestPre)
if err != nil {
return buffer.Bytes(), fmt.Errorf("%v", err)
}
err = binary.Write(buffer, binary.BigEndian, commandRequest.Command)
if err != nil {
return buffer.Bytes(), fmt.Errorf("%v", err)
}
err = binary.Write(buffer, binary.BigEndian, commandRequest.commandRequestPost)
if err != nil {
return buffer.Bytes(), fmt.Errorf("%v", err)
}
// Calculate CRC by my own and check if it is equal to the got CRC
raw := buffer.Bytes()
d := raw[8 : len(raw)-4] // drop first 8 bytes and last 4 bytes and calculate CRC for it
crc := uint32(crc16.Checksum(d))
buffer = new(bytes.Buffer)
_, err = buffer.Write(raw[:len(raw)-4])
if err != nil {
return buffer.Bytes(), fmt.Errorf("%v", err)
}
err = binary.Write(buffer, binary.BigEndian, crc)
if err != nil {
return buffer.Bytes(), fmt.Errorf("%v", err)
}
return buffer.Bytes(), nil
}
func DecodeCommandRequest(rawCommand *[]byte) (CommandRequest, error) {
var decoded CommandRequest
reader := bytes.NewReader(*rawCommand)
// Read first part of the record until the dynamic sized section.
err := binary.Read(reader, binary.BigEndian, &decoded.commandRequestPre)
if err != nil {
return decoded, fmt.Errorf("%v", err)
}
// Allocate memory for the dynamic sized section. Actual size is defined in the first block.
decoded.Command = make([]byte, decoded.CommandSize)
size, err := reader.Read(decoded.Command)
if err != nil {
return decoded, fmt.Errorf("%v", err)
}
if uint32(size) != decoded.CommandSize {
return decoded, fmt.Errorf("%d bytes were expected but got %d", decoded.CommandSize, size)
}
// Read the remaining part which is again fixed in size.
err = binary.Read(reader, binary.BigEndian, &decoded.commandRequestPost)
if err != nil {
return decoded, fmt.Errorf("%v", err)
}
// Calculate CRC by my own and check if it is equal to the got CRC
d := (*rawCommand)[8 : len(*rawCommand)-4] // drop first 8 bytes and last 4 bytes and calculate CRC
crc := crc16.Checksum(d)
expected := decoded.CRC
if uint32(crc) != expected {
return decoded, fmt.Errorf("CRC check failed! ACTUAL: %x EXPECTED: %x", crc, expected)
}
return decoded, nil
}
func DecodeCommandResponse(rawResponse *[]byte) (CommandResponse, error) {
var decoded CommandResponse
reader := bytes.NewReader(*rawResponse)
const minSize = int(unsafe.Sizeof(decoded.commandResponsePre))
if len(*rawResponse) < minSize {
return decoded, fmt.Errorf("only %d bytes received. Probably not a teltonika command response packet", len(*rawResponse))
}
// Read first part of the record until the dynamic sized section.
err := binary.Read(reader, binary.BigEndian, &decoded.commandResponsePre)
if err != nil {
return decoded, fmt.Errorf("%v", err)
}
if decoded.Preamble != ResponsePreamble {
return decoded, fmt.Errorf("wrong preamble: 0x%x", decoded.Preamble)
}
if decoded.CodecID != CodecID {
return decoded, fmt.Errorf("wrong CodecID: 0x%x", decoded.CodecID)
}
if decoded.Type != CommandTypeResponse {
return decoded, fmt.Errorf("wrong type: 0x%x", decoded.Type)
}
// Allocate memory for the dynamic sized section. Actual size is defined in the first block.
decoded.Response = make([]byte, decoded.ResponseSize)
err = binary.Read(reader, binary.BigEndian, &decoded.Response)
if err != nil {
return decoded, fmt.Errorf("%v", err)
}
err = binary.Read(reader, binary.BigEndian, &decoded.commandResponsePost)
if err != nil {
return decoded, fmt.Errorf("%v", err)
}
// Calculate CRC by my own and check if it is equal to the got CRC
d := (*rawResponse)[8 : len(*rawResponse)-4] // drop first 8 bytes and last 4 bytes and calculate CRC
calculatedCrc := crc16.Checksum(d)
expected := decoded.CRC
if uint32(calculatedCrc) != expected {
return decoded, fmt.Errorf("wrong CRC! Calculated: %x Received: %x", calculatedCrc, expected)
}
return decoded, nil
}