forked from back0893/jt808
-
Notifications
You must be signed in to change notification settings - Fork 0
/
enc.go
241 lines (193 loc) · 3.82 KB
/
enc.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
233
234
235
236
237
238
239
240
241
package jt808
import (
"bufio"
"bytes"
"encoding"
"encoding/binary"
"fmt"
"io"
)
func JT808Split(data []byte, atEOF bool) (int, []byte, error) {
var start, end int = -1, -1
if start = bytes.IndexByte(data, 0x7e); start != -1 {
if end = bytes.IndexByte(data[start+1:], 0x7e); end != -1 {
return start + end + 2, data[start : start+end+2], nil
}
}
if atEOF {
return len(data), nil, io.EOF
}
return start, nil, nil
}
func NewScanner(r io.Reader) *bufio.Scanner {
scanner := bufio.NewScanner(r)
scanner.Split(JT808Split)
return scanner
}
type BufDecoder struct {
bs []byte
l int
s int
}
func NewBufDecoder(bs []byte) *BufDecoder {
return &BufDecoder{bs: bs, l: len(bs), s: 0}
}
func (d *BufDecoder) Read(bs []byte) (int, error) {
l := len(bs)
if err := d.check(l); err != nil {
return 0, err
}
i := copy(bs, d.bs[d.s:d.s+l])
d.s += l
return i, nil
}
func (d *BufDecoder) ReadByte() (byte, error) {
if err := d.check(1); err != nil {
return 0, err
}
b := d.bs[d.s]
d.s += 1
return b, nil
}
func (d *BufDecoder) Uint16() (uint16, error) {
if err := d.check(2); err != nil {
return 0, err
}
i := binary.BigEndian.Uint16(d.bs[d.s : d.s+2])
d.s += 2
return i, nil
}
func (d *BufDecoder) Uint32() (uint32, error) {
if err := d.check(4); err != nil {
return 0, err
}
i := binary.BigEndian.Uint32(d.bs[d.s : d.s+4])
d.s += 4
return i, nil
}
func (d *BufDecoder) Uint64() (uint64, error) {
if err := d.check(8); err != nil {
return 0, err
}
i := binary.BigEndian.Uint64(d.bs[d.s : d.s+8])
d.s += 8
return i, nil
}
func (d *BufDecoder) BCD(n int) (string, error) {
if err := d.check(n); err != nil {
return "", err
}
s := bcd2str(d.bs[d.s : d.s+n])
d.s += n
return s, nil
}
func (d *BufDecoder) check(size int) error {
if d.s+size > d.l {
return io.ErrShortBuffer
}
return nil
}
/*
type BufEncoder struct {
*bytes.Buffer
}
func NewBufEncoder() *BufEncoder {
return &BufEncoder{Buffer: new(bytes.Buffer)}
}
func (b *BufEncoder) PutUint16(i uint16) {
bs := make([]byte, 2, 2)
binary.BigEndian.PutUint16(bs, i)
b.Write(bs)
}
func (b *BufEncoder) PutUint32(i uint32) {
bs := make([]byte, 4, 4)
binary.BigEndian.PutUint32(bs, i)
b.Write(bs)
}
func (b *BufEncoder) PutUint64(i uint64) {
bs := make([]byte, 8, 8)
binary.BigEndian.PutUint64(bs, i)
b.Write(bs)
}
*/
func checkSum(msg []byte, sum byte) ([]byte, error) {
/*
var check byte = 0x00
for _, b := range msg {
check ^= b
}
if check != sum {
return nil, fmt.Errorf("check sum error")
}
*/
return msg, nil
}
func Restore(bs []byte) ([]byte, error) {
l := len(bs)
if l == 0 || bs[0] != 0x7e || bs[l-1] != 0x7e {
return nil, fmt.Errorf("parser error")
}
i := 1
l -= 1
var buf bytes.Buffer
for i < l {
if bs[i] == 0x7d {
i++
if bs[i] == 0x02 {
buf.WriteByte(0x7e)
} else if bs[i] == 0x01 {
buf.WriteByte(0x7d)
}
i++
} else {
buf.WriteByte(bs[i])
i++
}
}
msg := buf.Bytes()
l = len(msg)
if l < 13 {
return nil, io.ErrShortBuffer
}
return checkSum(msg[:l-1], msg[l-1])
}
func JT808SendMsg(writer io.Writer, msghead, msgbody encoding.BinaryMarshaler) error {
w := bufio.NewWriterSize(writer, 1024)
var head, body []byte
var err error
if head, err = msghead.MarshalBinary(); err != nil {
return err
}
if body, err = msgbody.MarshalBinary(); err != nil {
return err
}
var check byte = 0x00
w.WriteByte(0x7e)
for _, b := range head {
check ^= b
if b == 0x7e {
w.WriteByte(0x7d)
w.WriteByte(0x02)
} else if b == 0x7d {
w.WriteByte(0x7d)
w.WriteByte(0x01)
} else {
w.WriteByte(b)
}
}
for _, b := range body {
check ^= b
if b == 0x7e {
w.WriteByte(0x7d)
w.WriteByte(0x02)
} else if b == 0x7d {
w.WriteByte(0x7d)
w.WriteByte(0x01)
} else {
w.WriteByte(b)
}
}
w.WriteByte(check)
w.WriteByte(0x7e)
return w.Flush()
}