-
Notifications
You must be signed in to change notification settings - Fork 1
/
encoding.go
310 lines (273 loc) · 5.27 KB
/
encoding.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package iec62056
import (
"bufio"
"bytes"
"errors"
"time"
)
const defaultInactivityTo = 120 * time.Second
const (
start = 0x2f
end = 0x21
ack = 0x06
soh = 0x01
stx = 0x02
etx = 0x03
trc = 0x3f
nak = 0x15
cr = 0x0d
lf = 0x0a
fb = 0x28
rb = 0x29
star = 0x2a
)
type ProtocolMode byte
const (
ModeA ProtocolMode = 'A' + iota
ModeB
ModeC
ModeD
)
type PCC byte
const (
NormalPCC PCC = '0'
SecondaryPCC PCC = '1'
)
type Option byte
const (
DataReadOut Option = '0' + iota
ProgrammingMode
_ //reserved
_ //reserved
_ //reserved
_ //reserved
Option6
Option7
Option8
Option9
)
type CommandId int
const (
CmdP0 CommandId = iota
CmdP1
CmdP2
CmdW1
CmdW2
CmdR1
CmdR2
CmdE2
CmdB0
)
var commands = map[CommandId][2]byte{
CmdP0: {'P', '0'},
CmdP1: {'P', '1'},
CmdP2: {'P', '2'},
CmdW1: {'W', '1'},
CmdW2: {'W', '2'},
CmdR1: {'R', '1'},
CmdR2: {'R', '2'},
CmdE2: {'E', '2'},
CmdB0: {'B', '0'},
}
var crlf = []byte{cr, lf}
var breakMsg = []byte{soh, commands[CmdB0][0], commands[CmdB0][1], etx}
var ErrBCC = errors.New("checksum failed")
var ErrNAK = errors.New("nak received")
type DataBlock struct {
Lines []DataLine
}
type DataLine struct {
Sets []DataSet
}
type DataSet struct {
Address string
Value string
Unit string
}
type Command struct {
Id CommandId
Payload *DataSet
}
type OptionSelectMessage struct {
Option Option
PCC PCC
bri byte
skipHandShake bool
}
type requestMessage string
type Identity struct {
Device string
Manufacturer string
Mode ProtocolMode
bri byte
}
func (ds *DataSet) MarshalBinary() ([]byte, error) {
length := len(ds.Address)
length += len(ds.Value)
unitLen := len(ds.Unit)
if length+unitLen == 0 {
return []byte{}, nil
}
length += unitLen + 3 //fb+rb+star
rv := make([]byte, 0, length)
rv = append(rv, ds.Address...)
rv = append(rv, fb)
rv = append(rv, ds.Value...)
if unitLen != 0 {
rv = append(rv, star)
rv = append(rv, ds.Unit...)
}
rv = append(rv, rb)
return rv, nil
}
func (ds *DataSet) UnmarshalBinary(data []byte) error {
if len(data) == 0 {
return nil
}
fbIdx := bytes.IndexByte(data, fb)
if fbIdx == -1 {
return errors.New("front boundary is missing")
}
rbIdx := bytes.IndexByte(data, rb)
if rbIdx == -1 {
return errors.New("rear boundary is missing")
}
*ds = DataSet{}
ds.Address = ""
if fbIdx > 0 {
ds.Address = string(data[0:fbIdx])
}
data = data[fbIdx+1 : rbIdx]
spIdx := bytes.IndexByte(data, star)
ds.Value = ""
ds.Unit = ""
if spIdx == -1 {
ds.Value = string(data)
return nil
}
ds.Value = string(data[:spIdx])
ds.Unit = string(data[spIdx+1:])
return nil
}
func (dl *DataLine) UnmarshalBinary(data []byte) error {
s := bufio.NewScanner(bytes.NewReader(data))
s.Split(func(data []byte, atEOF bool) (advance int, token []byte, err error) {
if atEOF && len(data) == 0 {
return 0, nil, nil
}
if i := bytes.IndexByte(data, rb); i >= 0 {
return i + 1, data[:i+1], nil
}
return len(data), data, errors.New("invalid data set")
})
*dl = DataLine{}
for s.Scan() {
var ds DataSet
if err := ds.UnmarshalBinary(s.Bytes()); err != nil {
return err
}
dl.Sets = append(dl.Sets, ds)
}
if s.Err() != nil {
return s.Err()
}
return nil
}
func (db *DataBlock) UnmarshalBinary(dataIn []byte) error {
if len(dataIn) == 0 {
return nil
}
i := bytes.IndexByte(dataIn, end)
if i == 0 {
return nil
}
data := make([]byte, len(dataIn))
copy(data, dataIn)
if i > 0 {
data = data[0:i]
}
i = bytes.IndexByte(data, etx)
if i > 0 {
data = data[0:i]
}
s := bufio.NewScanner(bytes.NewReader(data))
*db = DataBlock{}
for s.Scan() {
var dl DataLine
if err := dl.UnmarshalBinary(s.Bytes()); err != nil {
return err
}
db.Lines = append(db.Lines, dl)
}
if s.Err() != nil {
return s.Err()
}
return nil
}
func (c *Command) MarshalBinary() ([]byte, error) {
var plLen int
var pl []byte
if c.Payload != nil {
pl, _ = c.Payload.MarshalBinary()
plLen = len(pl)
}
cmd, ok := commands[c.Id]
if !ok {
return nil, errors.New("invalid command")
}
rv := make([]byte, 0, plLen+6)
rv = append(rv, soh, cmd[0], cmd[1])
if plLen != 0 {
rv = append(rv, stx)
rv = append(rv, pl...)
}
return append(rv, etx), nil
}
func (o *OptionSelectMessage) MarshalBinary() ([]byte, error) {
return []byte{ack, byte(o.PCC), o.bri, byte(o.Option)}, nil
}
func (address requestMessage) MarshalBinary() ([]byte, error) {
l := len(address)
if l == 0 {
return []byte{start, trc, end}, nil
}
msg := make([]byte, 0, l+5) //cr lf will be added
msg = append(msg, start, trc)
msg = append(msg, address...)
return append(msg, end), nil
}
func (id *Identity) UnmarshalBinary(data []byte) error {
if len(data) < 4 {
return errors.New("identity message too short")
}
*id = Identity{}
id.Manufacturer = string(data[0:3])
id.bri = data[3]
id.Mode = decodeMode(data[3])
id.Device = string(data[4 : len(data)-2])
return nil
}
func decodeMode(b byte) ProtocolMode {
switch {
case '0' <= b && b <= '9':
return ModeC
case 'A' <= b && b <= 'I':
return ModeB
}
return ModeA
}
func decodeBaudRate(b byte) int {
switch b {
case 'A', '1':
return 600
case 'B', '2':
return 1200
case 'C', '3':
return 2400
case 'D', '4':
return 4800
case 'E', '5':
return 9600
}
return 300
}