-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathframe.go
451 lines (378 loc) · 9.58 KB
/
frame.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
package gombus
import (
"encoding/binary"
"encoding/hex"
"fmt"
"strconv"
"github.com/sirupsen/logrus"
)
type Frame []byte
type ShortFrame Frame
func NewShortFrame() ShortFrame {
return ShortFrame{
0x10, // Start byte short frame
0x7b, // C field
0x00, // A field
0x00, // checksum
0x16, // stop byte
}
}
func (sf ShortFrame) SetChecksum() {
size := len(sf)
sf[size-2] = calcCheckSum(sf[1 : size-2])
}
func (sf ShortFrame) SetAddress(primary uint8) {
sf[2] = primary
}
func (sf ShortFrame) SetC(c uint8) {
sf[1] = c
}
func (sf ShortFrame) C() C {
return C(sf[1])
}
func (sf ShortFrame) SetFCB() {
sf[1] |= CONTROL_MASK_FCB
}
func (sf ShortFrame) SetFCV() {
sf[1] |= CONTROL_MASK_FCV
}
func (sf ShortFrame) ClearFCB() {
sf[1] &^= CONTROL_MASK_FCB
}
func (sf ShortFrame) ClearFCV() {
sf[1] &^= CONTROL_MASK_FCV
}
func (sf ShortFrame) A() byte {
return sf[2]
}
type LongFrame Frame
func (lf LongFrame) SetChecksum() {
size := len(lf)
lf[size-2] = calcCheckSum(lf[4 : size-2])
}
func (lf LongFrame) SetLength() {
lf[1] = byte(len(lf) - 6)
lf[2] = byte(len(lf) - 6)
}
func (lf LongFrame) L() int {
return int(lf[1])
}
func (lf LongFrame) C() C {
return C(lf[4])
}
func (lf LongFrame) SetFCB() {
lf[4] |= CONTROL_MASK_FCB
}
func (lf LongFrame) SetFCV() {
lf[4] |= CONTROL_MASK_FCV
}
func (lf LongFrame) ClearFCB() {
lf[4] &^= CONTROL_MASK_FCB
}
func (lf LongFrame) ClearFCV() {
lf[4] &^= CONTROL_MASK_FCV
}
func (lf LongFrame) A() byte {
return lf[5]
}
func (lf LongFrame) CI() byte {
return lf[6]
}
func (lf LongFrame) Decode() (*DecodedFrame, error) {
if lf.CI() != 0x72 {
return nil, fmt.Errorf("unknown longframe, only supports variable data response for now")
}
man, err := lf.DecodeManufacturer()
if err != nil {
return nil, err
}
dr, err := lf.decodeData(lf[19 : len(lf)-2])
if err != nil {
return nil, err
}
dt, err := deviceTypeLookup(lf[14])
if err != nil {
return nil, err
}
version := int(lf[13])
dFrame := &DecodedFrame{
raw: lf,
SerialNumber: bcdToInt(lf[7:11]),
Manufacturer: man,
ProductName: "", // TODO
Version: version,
DeviceType: dt,
AccessNumber: 0, // TODO
Signature: 0, // TODO
Status: 0, // TODO
DataRecords: dr,
}
return dFrame, nil
}
func (lf LongFrame) decodeData(data []byte) ([]DecodedDataRecord, error) {
records := make([]DecodedDataRecord, 0)
var dData DecodedDataRecord
// var dif byte
dif := -1
var dife []byte
lookForData := false
lookForDIFE := false
lookForVIF := false
lookForVIFE := false
remainingData := 0
customUnit := ""
var vife []byte
var vif byte
for i, v := range data {
if remainingData > 0 {
remainingData--
continue
}
// expect first one is a DIF
if dif == -1 {
// DIF Function TODO those
// 0x0f Start of manufacturer specific data structures to end of user data
// 0x3f..0x6f Reserved
// 0x7f Global readout request (all storage#, units, tariffs, function fields)
dData = DecodedDataRecord{}
dData.Function = decodeRecordFunction(v)
dData.StorageNumber = int(v) & DATA_RECORD_DIF_MASK_STORAGE_NO
// 1Fh DONE Same meaning as DIF = 0Fh + More records follow in next telegram
if v == 0x1f {
dData.HasMoreRecords = true
records = append(records, dData)
}
// 2Fh Idle Filler (not to be interpreted), following byte = DIF
if v == 0x2f {
continue
}
dif = int(v)
logrus.Tracef("dif is: % x\n", dif)
if checkKthBitSet(int(v), 7) {
lookForDIFE = true
continue
}
lookForVIF = true
continue
}
if lookForDIFE { // has another DIFE{
dife = append(dife, v)
// TODO validate we dont have more than 10 here
if checkKthBitSet(int(v), 7) {
// lookForDIFE = true
continue
}
lookForDIFE = false
lookForVIF = true
continue
}
// E111 1100 7 bits data
if lookForVIF {
vif = v
if checkKthBitSet(int(v), 7) {
lookForVIF = false
lookForVIFE = true
continue
}
// In case of VIF = 7Ch / FCh the true VIF is represented by the following ASCII string with the length given in the first byte
if vif == 0x7c {
l := int(data[i+1])
customUnit = decodeASCII(data[i+2 : i+2+l])
remainingData = l + 1
lookForVIF = false
lookForData = true
continue
}
lookForVIF = false
lookForData = true
continue
}
if lookForVIFE {
vife = append(vife, v)
// TODO validate we dont have more than 10 here
if checkKthBitSet(int(v), 7) {
// lookForVIFE = true
continue
}
lookForVIFE = false
lookForData = true
continue
}
if lookForData {
if customUnit != "" {
dData.Unit = Unit{
Exp: 1,
Unit: customUnit,
Type: VIFUnit["VARIABLE_VIF"],
VIFUnitDesc: "",
}
customUnit = ""
} else {
dData.Unit = decodeUnit(vif, vife)
}
dData.StorageNumber = decodeStorageNumber(dif, dife)
dData.Device = decodeDevice(dif, dife)
dData.Tariff = decodeTariff(dif, dife)
difCoding := dif & DATA_RECORD_DIF_MASK_DATA
logrus.Tracef("Datarecord dif mask: %b ( %#x )", difCoding, difCoding)
switch difCoding {
// 0000 No data
case 0x00:
remainingData = 0
// 0001 8 Bit Integer
case 0x01:
remainingData = 0
dData.RawValue = float64(data[i])
logrus.Tracef("data dif 0x01 is: % x\n", data[i])
// 0010 16 Bit Integer
case 0x02:
remainingData = 1
dData.RawValue = float64(binary.LittleEndian.Uint16(data[i : i+2]))
logrus.Tracef("data dif 0x02 is: % x\n", data[i:i+4])
// 0011 24 Bit Integer
case 0x03:
remainingData = 2
// 4 byte (32 bit)
case 0x04:
remainingData = 3
logrus.Tracef("data dif 0x04 is: % x\n", data[i:i+4])
v, err := int32ToInt(data[i : i+4])
if err != nil {
return nil, err
}
dData.RawValue = float64(v)
// 0101 32 Bit Real
case 0x05:
remainingData = 3
// 0110 48 Bit Integer
case 0x06:
remainingData = 5
// 0111 64 Bit Integer
case 0x07:
remainingData = 7
// 1000 Selection for Readout
case 0x08:
remainingData = 0
// 1001 2 digit BCD
case 0x09:
remainingData = 0
dData.RawValue = float64(bcdToInt(data[i : i+1]))
// 1010 4 digit BCD
case 0x0a:
remainingData = 1
dData.RawValue = float64(bcdToInt(data[i : i+2]))
// 1011 6 digit BCD
case 0x0b:
remainingData = 2
dData.RawValue = float64(bcdToInt(data[i : i+3]))
// 1100 8 digit BCD
case 0x0c:
remainingData = 3
dData.RawValue = float64(bcdToInt(data[i : i+4]))
// 1101 variable length
case 0x0d:
// With data field = 1101b several data types with variable length can be used. The length of the data is given with the first byte of data, which is here called LVAR.
// LVAR = 00h .. BFh : ASCII string with LVAR characters
// LVAR = C0h .. CFh : positive BCD number with (LVAR - C0h) · 2 digits
// LVAR = D0h .. DFH : negative BCD number with (LVAR - D0h) · 2 digits
// LVAR = E0h .. EFh : binary number with (LVAR - E0h) bytes
// LVAR = F0h .. FAh : floating point number with (LVAR - F0h) bytes [to be defined]
// LVAR = FBh .. FFh : Reserved
size := 0
if data[i] <= 0xBF {
size = int(data[i])
dData.ValueString = decodeASCII(data[i+1 : i+1+size])
} else if data[i] >= 0xC0 && data[i] <= 0xCF {
size = (int(data[i]) - 0xC0) * 2
// TODO data here
} else if data[i] >= 0xD0 && data[i] <= 0xDF {
size = (int(data[i]) - 0xD0) * 2
// TODO data here
} else if data[i] >= 0xE0 && data[i] <= 0xEF {
size = int(data[i]) - 0xE0
// TODO data here
} else if data[i] >= 0xF0 && data[i] <= 0xFA {
size = int(data[i]) - 0xF0
// TODO data here
}
remainingData = size
// 1110 12 digit BCD
case 0x0e:
remainingData = 5
dData.RawValue = float64(bcdToInt(data[i : i+6]))
// 1111 Special Functions
case 0x0f:
remainingData = 0 // TODO what here?
}
lookForData = false
dif = -1
dife = nil
vif = 0
vife = nil
logrus.Trace("rawValue: ", dData.RawValue)
logrus.Trace("valueString: ", dData.ValueString)
dData.Value = dData.Unit.Value(dData.RawValue)
records = append(records, dData)
}
}
return records, nil
}
func (lf LongFrame) DecodeManufacturer() (string, error) {
id := int(binary.LittleEndian.Uint16(lf[11:13]))
return fmt.Sprintf(
"%c%c%c",
rune(((id>>10)&0x001F)+64),
rune(((id>>5)&0x001F)+64),
rune((id&0x001F)+64),
), nil
}
type DecodedDataRecord struct {
Function string
StorageNumber int
Tariff int
Device int
Unit Unit
Exponent float64
Type string
Quantity string
Value float64
ValueString string
RawValue float64
HasMoreRecords bool
}
type DecodedFrame struct {
raw []byte
SerialNumber int
Manufacturer string
ProductName string
Version int
DeviceType string
AccessNumber int16
Signature int16
Status int
// ReadableStatus string // TODO make function on struct!
DataRecords []DecodedDataRecord
}
func (df DecodedFrame) HasMoreRecords() bool {
if len(df.DataRecords) == 0 {
return false
}
return df.DataRecords[len(df.DataRecords)-1].HasMoreRecords
}
func (df DecodedFrame) SecondaryAddressString() string {
// 4 bytes being the device ID (serial #)
// 2 bytes being the manufacturer’s identifier
// 1 byte being the device version
// 1 byte being the device media
return strconv.Itoa(df.SerialNumber) + hex.EncodeToString(df.raw[11:14])
}
const SingleCharacterFrame = 0xe5
type C byte
// FCB Frame Count-Bit.
func (c C) FCB() bool {
return (c & CONTROL_MASK_FCB) > 0
}
// FCV Frame Count Valid indicates we want to use frame counting in the following request/responses.
func (c C) FCV() bool {
return (c & CONTROL_MASK_FCV) > 0
}