-
Notifications
You must be signed in to change notification settings - Fork 2
/
calib_data.go
769 lines (738 loc) · 25.9 KB
/
calib_data.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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
/*reads characteristics information from a2l and fills it with the data from a hex file. At least that is the plan.
Currently it can parse a2l-files as well as the corresponding IntelHex32 or Motorola S19 files. And it is quite fast at that.
At the moment a real world A2L(80MB) with its corresponding Hex File(10MB) will be parsed in less than a second.
But it still lacks the last bit of work which is implementing the methods for
axis_pts, axis_descr, record_layout and fnc_values in order to understand the memory layout and position of a specific characteristic.
This is somewhat of a convoluted mess in the a2l standard due to its historic growth and will be implemented when I have a little more spare time.
The only dependency outside the go standard library is currently zerolog.*/
package calibrationReader
import (
"errors"
"fmt"
"os"
"strconv"
"strings"
"sync"
"time"
"github.com/asap2Go/calibrationReader/a2l"
"github.com/asap2Go/calibrationReader/ihex32"
"github.com/asap2Go/calibrationReader/srec19"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
// CalibrationData contains the parsed structs from the a2l as well as the byte data from the hex file
// that are parsed by ReadCalibration()
type CalibrationData struct {
//A2L defines the Metadata for a given ECU-Project and its corresponding hex file
A2l a2l.A2L
//ModuleIndex defines which module within the a2l file is being used. Default is 0
ModuleIndex uint8
//Hex contains the flashable data for the ecu.
//it is being simplified as a map that can be accessed by an address
//represented by an integer and returns a byte as value.
Hex map[uint32]byte
}
// ReadCalibration takes filepaths to the a2l file and the hex file,
// parses them in parallel and returns a CalibrationData struct
func ReadCalibration(a2lFilePath string, hexFilePath string) (CalibrationData, error) {
var err error
var cd CalibrationData
//set ModuleIndex to zero as default as it covers 99% of use cases.
cd.ModuleIndex = 0
//set up channels for concurrent parsing of a2l and hex file as well as for the communication of potential parsing errors.
var errChan = make(chan error, 2)
var a2lChan = make(chan a2l.A2L, 1)
var hexChan = make(chan map[uint32]byte, 1)
//wait group to determine when both parsers have finished
wgReaders := new(sync.WaitGroup)
//initialize logging
err = configureLogger()
if err != nil {
log.Err(err).Msg("could not create logger:")
return cd, err
}
//Log Level per Default is warning as info and debug lead to excessive log files
//and should only be used for debugging.
zerolog.SetGlobalLevel(zerolog.WarnLevel)
//start concurrent parsers as individual go routines
wgReaders.Add(2)
go readA2L(wgReaders, a2lChan, errChan, a2lFilePath)
go readHex(wgReaders, hexChan, errChan, hexFilePath)
//and wait until they're done
wgReaders.Wait()
//error channel is closed here while the other two channels are closed in the goroutines that use them exclusively.
//this is safe because the a2l and hex channels are closed after a potential error message has been sent via the error channel.
//meaning the waitgroup wgReaders blocks until all sending operations on the error channel are over.
close(errChan)
//check if any errors have occured within the readers
//only the first error is returned
var firstErr error
if len(errChan) > 0 {
for e := range errChan {
if e != nil {
firstErr = e
}
log.Err(e).Msg("parser encountered an error:")
}
return cd, firstErr
}
//in case no errors occured then read from the closed channels
cd.A2l = <-a2lChan
cd.Hex = <-hexChan
return cd, nil
}
// readA2L is a helper function intended to be run in a separate go routine to call the a2l parser
// in order to be able to parse hex and a2l in parallel
func readA2L(wg *sync.WaitGroup, ca chan a2l.A2L, ce chan error, a2lFilePath string) {
defer wg.Done()
a, err := a2l.ParseFromFile(a2lFilePath)
if err != nil {
log.Err(err).Msg("could not parse a2l:")
ce <- err //send an error via channel to signal it to the main thread
close(ca)
} else {
ca <- a
close(ca) //send the successfully parsed a2l structure to the main thread
log.Info().Msg("parsed a2l file")
}
}
// readHex is a helper function intended to be run in a separate go routine to call the hex parser
// in order to be able to parse hex and a2l in parallel
func readHex(wg *sync.WaitGroup, ch chan map[uint32]byte, ce chan error, hexFilePath string) {
defer wg.Done()
//check whether the hex or the s19 parser needs to be used.
//probably improve this by putting the determination logic into a single unified hex parsing package
//intel hex
if strings.Contains(strings.ToLower(hexFilePath), ".hex") {
h, err := ihex32.ParseFromFile(hexFilePath)
if err != nil {
log.Err(err).Msg("could not parse hex:")
ce <- err //send an error via channel to signal it to the main thread
close(ch)
} else {
ch <- h //send the successfully parsed hex map to the main thread
close(ch)
log.Info().Msg("parsed hex file")
}
//Motorola S19
} else if strings.Contains(strings.ToLower(hexFilePath), ".s19") {
h, err := srec19.ParseFromFile(hexFilePath)
if err != nil {
log.Err(err).Msg("could not parse hex:")
ce <- err //send an error via channel to signal it to the main thread
close(ch)
} else {
ch <- h //send the successfully parsed hex map to the main thread
close(ch)
log.Info().Msg("parsed hex file")
}
} else {
err := errors.New("unsupported hex file type")
log.Err(err).Msg("could not parse hex:")
ce <- err //send an error via channel to signal it to the main thread
close(ch)
}
}
// configureLogger adds a file logger, resets previous log file and does some formatting
func configureLogger() error {
file, err := os.Create("calibReader.log")
if err != nil {
log.Error().Err(err).Msg("could not create calibration reader log-file")
return err
}
fileWriter := zerolog.ConsoleWriter{Out: file, NoColor: true, TimeFormat: time.StampMicro}
consoleWriter := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.StampMicro}
consoleWriter.FormatLevel = func(i interface{}) string {
return strings.ToUpper(fmt.Sprintf("| %s |", i))
}
fileWriter.FormatLevel = func(i interface{}) string {
return strings.ToUpper(fmt.Sprintf("| %s |", i))
}
zerolog.TimeFieldFormat = zerolog.TimeFormatUnixMicro
log.Logger = zerolog.New(zerolog.MultiLevelWriter(fileWriter, consoleWriter)).With().Timestamp().Caller().Logger()
return nil
}
// getRecordLayout tries to retrieve the record layout for a specified characteristic
func (cd *CalibrationData) getRecordLayout(c *a2l.Characteristic) (*a2l.RecordLayout, error) {
if !c.DepositSet {
err := errors.New("no deposit set in characteristic " + c.Name)
log.Err(err).Msg("record layout not found")
return nil, err
}
module := cd.A2l.Project.Modules[cd.ModuleIndex]
rl, exists := module.RecordLayouts[c.Deposit]
if !exists {
err := errors.New("no record layout found for deposit identifier" + c.Deposit + " of characteristic " + c.Name)
log.Err(err).Msg("record layout not found")
return nil, err
}
return &rl, nil
}
// getNextAlignedAddress takes an adress of a record layout field and its datatype as well as a reference to the record layout itself.
// it computes whether the address given is aligned as defined by the alignemnts within the record layout for the given datatype.
// if the record layout does not provide an alignement the alignment from MOD_COMMON is used.
// if MOD_COMMON does not provide an alignment, then the default values (magic numbers below) are used.
func (cd *CalibrationData) getNextAlignedAddress(address uint32, dte a2l.DataTypeEnum, rl *a2l.RecordLayout) uint32 {
switch dte {
case a2l.UBYTE:
if rl.AlignmentByte.AlignmentBorderSet {
modulo := address % uint32(rl.AlignmentByte.AlignmentBorder)
if modulo != 0 {
return address + modulo
} else {
return address
}
}
a := cd.A2l.Project.Modules[cd.ModuleIndex].ModCommon.AlignmentByte
if a.AlignmentBorderSet {
modulo := address % uint32(a.AlignmentBorder)
if modulo != 0 {
return address + modulo
} else {
return address
}
} else {
modulo := address % uint32(1)
if modulo != 0 {
return address + modulo
} else {
return address
}
}
case a2l.SBYTE:
if rl.AlignmentByte.AlignmentBorderSet {
modulo := address % uint32(rl.AlignmentByte.AlignmentBorder)
if modulo != 0 {
return address + modulo
} else {
return address
}
}
a := cd.A2l.Project.Modules[cd.ModuleIndex].ModCommon.AlignmentByte
if a.AlignmentBorderSet {
modulo := address % uint32(a.AlignmentBorder)
if modulo != 0 {
return address + modulo
} else {
return address
}
} else {
modulo := address % uint32(1)
if modulo != 0 {
return address + modulo
} else {
return address
}
}
case a2l.UWORD:
if rl.AlignmentWord.AlignmentBorderSet {
modulo := address % uint32(rl.AlignmentWord.AlignmentBorder)
if modulo != 0 {
return address + modulo
} else {
return address
}
}
a := cd.A2l.Project.Modules[cd.ModuleIndex].ModCommon.AlignmentWord
if a.AlignmentBorderSet {
modulo := address % uint32(a.AlignmentBorder)
if modulo != 0 {
return address + modulo
} else {
return address
}
} else {
modulo := address % uint32(2)
if modulo != 0 {
return address + modulo
} else {
return address
}
}
case a2l.SWORD:
if rl.AlignmentWord.AlignmentBorderSet {
modulo := address % uint32(rl.AlignmentWord.AlignmentBorder)
if modulo != 0 {
return address + modulo
} else {
return address
}
}
a := cd.A2l.Project.Modules[cd.ModuleIndex].ModCommon.AlignmentWord
if a.AlignmentBorderSet {
modulo := address % uint32(a.AlignmentBorder)
if modulo != 0 {
return address + modulo
} else {
return address
}
} else {
modulo := address % uint32(2)
if modulo != 0 {
return address + modulo
} else {
return address
}
}
case a2l.ULONG:
if rl.AlignmentLong.AlignmentBorderSet {
modulo := address % uint32(rl.AlignmentLong.AlignmentBorder)
if modulo != 0 {
return address + modulo
} else {
return address
}
}
a := cd.A2l.Project.Modules[cd.ModuleIndex].ModCommon.AlignmentLong
if a.AlignmentBorderSet {
modulo := address % uint32(a.AlignmentBorder)
if modulo != 0 {
return address + modulo
} else {
return address
}
} else {
modulo := address % uint32(4)
if modulo != 0 {
return address + modulo
} else {
return address
}
}
case a2l.SLONG:
if rl.AlignmentLong.AlignmentBorderSet {
modulo := address % uint32(rl.AlignmentLong.AlignmentBorder)
if modulo != 0 {
return address + modulo
} else {
return address
}
}
a := cd.A2l.Project.Modules[cd.ModuleIndex].ModCommon.AlignmentLong
if a.AlignmentBorderSet {
modulo := address % uint32(a.AlignmentBorder)
if modulo != 0 {
return address + modulo
} else {
return address
}
} else {
modulo := address % uint32(4)
if modulo != 0 {
return address + modulo
} else {
return address
}
}
case a2l.AUint64:
if rl.AlignmentInt64.AlignmentBorderSet {
modulo := address % uint32(rl.AlignmentInt64.AlignmentBorder)
if modulo != 0 {
return address + modulo
} else {
return address
}
}
a := cd.A2l.Project.Modules[cd.ModuleIndex].ModCommon.AlignmentInt64
if a.AlignmentBorderSet {
modulo := address % uint32(a.AlignmentBorder)
if modulo != 0 {
return address + modulo
} else {
return address
}
} else {
modulo := address % uint32(8)
if modulo != 0 {
return address + modulo
} else {
return address
}
}
case a2l.AInt64:
if rl.AlignmentInt64.AlignmentBorderSet {
modulo := address % uint32(rl.AlignmentInt64.AlignmentBorder)
if modulo != 0 {
return address + modulo
} else {
return address
}
}
a := cd.A2l.Project.Modules[cd.ModuleIndex].ModCommon.AlignmentInt64
if a.AlignmentBorderSet {
modulo := address % uint32(a.AlignmentBorder)
if modulo != 0 {
return address + modulo
} else {
return address
}
} else {
modulo := address % uint32(8)
if modulo != 0 {
return address + modulo
} else {
return address
}
}
case a2l.Float16Ieee:
if rl.AlignmentFloat16Ieee.AlignmentBorderSet {
modulo := address % uint32(rl.AlignmentFloat16Ieee.AlignmentBorder)
if modulo != 0 {
return address + modulo
} else {
return address
}
}
a := cd.A2l.Project.Modules[cd.ModuleIndex].ModCommon.AlignmentFloat16Ieee
if a.AlignmentBorderSet {
modulo := address % uint32(a.AlignmentBorder)
if modulo != 0 {
return address + modulo
} else {
return address
}
} else {
modulo := address % uint32(2)
if modulo != 0 {
return address + modulo
} else {
return address
}
}
case a2l.Float32Ieee:
if rl.AlignmentFloat32Ieee.AlignmentBorderSet {
modulo := address % uint32(rl.AlignmentFloat32Ieee.AlignmentBorder)
if modulo != 0 {
return address + modulo
} else {
return address
}
}
a := cd.A2l.Project.Modules[cd.ModuleIndex].ModCommon.AlignmentFloat32Ieee
if a.AlignmentBorderSet {
modulo := address % uint32(a.AlignmentBorder)
if modulo != 0 {
return address + modulo
} else {
return address
}
} else {
modulo := address % uint32(4)
if modulo != 0 {
return address + modulo
} else {
return address
}
}
case a2l.Float64Ieee:
if rl.AlignmentFloat64Ieee.AlignmentBorderSet {
modulo := address % uint32(rl.AlignmentFloat64Ieee.AlignmentBorder)
if modulo != 0 {
return address + modulo
} else {
return address
}
}
a := cd.A2l.Project.Modules[cd.ModuleIndex].ModCommon.AlignmentFloat64Ieee
if a.AlignmentBorderSet {
modulo := address % uint32(a.AlignmentBorder)
if modulo != 0 {
return address + modulo
} else {
return address
}
} else {
modulo := address % uint32(8)
if modulo != 0 {
return address + modulo
} else {
return address
}
}
default:
return address
}
}
// getBytes gets a number of bytes (length) from a given address and returns a byte slice
// if the address cannot be found an error is returned
func (cd *CalibrationData) getBytes(address uint32, length uint32) ([]byte, error) {
nBytes := length / 8
bytes := make([]byte, nBytes, 0)
if length%8 != 0 {
err := errors.New("unexpected number of bits " + strconv.Itoa(int(length)) + ". Expected a multiple of 8.")
log.Error().Err(err).Msg("invalid length")
return bytes, err
}
for i := 0; i < int(nBytes); i++ {
b, exists := cd.Hex[address+uint32(i)]
if !exists {
err := errors.New("address " + strconv.Itoa(int(address+uint32(i))) + " not found in hex")
log.Error().Err(err).Msg("invalid address")
return bytes, err
}
bytes = append(bytes, b)
}
return bytes, nil
}
// getValuesFromHex Reads all values for ONE specific characteristic and its corresponding record layout from the hex file and converts it to decimal, and physical values.
func getValuesFromHex(cv *CharacteristicValues, cd *CalibrationData) {
rl, err := cd.getRecordLayout(cv.characteristic)
if err != nil {
log.Err(err).Msg("record layout for identifier '" + cv.characteristic.Deposit + "' not found")
}
//determine relative positions of each field of the record layout struct
relPos, err := rl.GetRecordLayoutRelativePositions()
if err != nil {
log.Err(err).Msg("could not retrieve positions for record layout '" + cv.characteristic.Deposit + "'")
}
rl.RelativePositions = relPos
//curPos tracks the current position within the deposit structure as an uint32 address
//with each field that gets parsed from the hex file curPos is incremented by the length of the datastructure.
var curPos uint32
//determine the start address of the characteristic
curPos, err = cd.convertStringToUint32Address(cv.characteristic.Address)
if err != nil {
log.Err(err).Msg("could not convert address of characteristic '" + cv.characteristic.Name + "'")
}
for _, field := range cv.recordLayout.RelativePositions {
switch field {
case "AxisPtsX":
cv.AxisXValues, err = cv.getAxisPointsX(cd, rl, &curPos)
if err != nil {
log.Err(err).Msg("could not get values for X-Axis of characteristic '" + cv.characteristic.Name + "'")
}
case "AxisPtsY":
cv.AxisYValues, err = cv.getAxisPointsY(cd, rl, &curPos)
if err != nil {
log.Err(err).Msg("could not get values for Y-Axis of characteristic '" + cv.characteristic.Name + "'")
}
case "AxisPtsZ":
cv.AxisZValues, err = cv.getAxisPointsZ(cd, rl, &curPos)
if err != nil {
log.Err(err).Msg("could not get values for Z-Axis of characteristic '" + cv.characteristic.Name + "'")
}
case "AxisPts4":
cv.Axis4Values, err = cv.getAxisPoints4(cd, rl, &curPos)
if err != nil {
log.Err(err).Msg("could not get values for 4-Axis of characteristic '" + cv.characteristic.Name + "'")
}
case "AxisPts5":
cv.Axis5Values, err = cv.getAxisPoints5(cd, rl, &curPos)
if err != nil {
log.Err(err).Msg("could not get values for 5-Axis of characteristic '" + cv.characteristic.Name + "'")
}
case "AxisRescaleX":
//To-Do?
err = errors.New("undefined case in record layout")
log.Err(err).Msg("axisRescaleX not implemented")
case "DistOpX":
cv.distOpXValue, err = cd.getDistOpX(rl, &curPos)
if err != nil {
log.Err(err).Msg("could not get value for distOpX of characteristic '" + cv.characteristic.Name + "'")
}
case "DistOpY":
cv.distOpYValue, err = cd.getDistOpY(rl, &curPos)
if err != nil {
log.Err(err).Msg("could not get value for distOpY of characteristic '" + cv.characteristic.Name + "'")
}
case "DistOpZ":
cv.distOpZValue, err = cd.getDistOpZ(rl, &curPos)
if err != nil {
log.Err(err).Msg("could not get value for distOpZ of characteristic '" + cv.characteristic.Name + "'")
}
case "DistOp4":
cv.distOp4Value, err = cd.getDistOp4(rl, &curPos)
if err != nil {
log.Err(err).Msg("could not get value for distOp4 of characteristic '" + cv.characteristic.Name + "'")
}
case "DistOp5":
cv.distOp5Value, err = cd.getDistOp5(rl, &curPos)
if err != nil {
log.Err(err).Msg("could not get value for distOp5 of characteristic '" + cv.characteristic.Name + "'")
}
case "FncValues":
cv.fncValues, err = cd.getFncValues(rl, &curPos, cv)
if err != nil {
log.Err(err).Msg("could not get value for fncValues of characteristic '" + cv.characteristic.Name + "'")
}
case "Identification":
cv.identificationValue, err = cd.getIdentification(rl, &curPos)
if err != nil {
log.Err(err).Msg("could not get value for identification of characteristic '" + cv.characteristic.Name + "'")
}
case "NoAxisPtsX":
cv.noAxisPtsXValue, err = cd.getNoAxisPtsX(rl, &curPos)
if err != nil {
log.Err(err).Msg("could not get value for noAxisPtsX of characteristic '" + cv.characteristic.Name + "'")
}
case "NoAxisPtsY":
cv.noAxisPtsYValue, err = cd.getNoAxisPtsY(rl, &curPos)
if err != nil {
log.Err(err).Msg("could not get value for noAxisPtsY of characteristic '" + cv.characteristic.Name + "'")
}
case "NoAxisPtsZ":
cv.noAxisPtsZValue, err = cd.getNoAxisPtsZ(rl, &curPos)
if err != nil {
log.Err(err).Msg("could not get value for noAxisPtsZ of characteristic '" + cv.characteristic.Name + "'")
}
case "NoAxisPts4":
cv.noAxisPts4Value, err = cd.getNoAxisPts4(rl, &curPos)
if err != nil {
log.Err(err).Msg("could not get value for noAxisPts4 of characteristic '" + cv.characteristic.Name + "'")
}
case "NoAxisPts5":
cv.noAxisPts5Value, err = cd.getNoAxisPts5(rl, &curPos)
if err != nil {
log.Err(err).Msg("could not get value for noAxisPts5 of characteristic '" + cv.characteristic.Name + "'")
}
case "NoRescaleX":
cv.noRescaleXValue, err = cd.getNoRescaleX(rl, &curPos)
if err != nil {
log.Err(err).Msg("could not get value for noRescaleX of characteristic '" + cv.characteristic.Name + "'")
}
case "OffsetX":
cv.offsetXValue, err = cd.getOffsetX(rl, &curPos)
if err != nil {
log.Err(err).Msg("could not get value for offsetX of characteristic '" + cv.characteristic.Name + "'")
}
case "OffsetY":
cv.offsetYValue, err = cd.getOffsetY(rl, &curPos)
if err != nil {
log.Err(err).Msg("could not get value for offsetY of characteristic '" + cv.characteristic.Name + "'")
}
case "OffsetZ":
cv.offsetZValue, err = cd.getOffsetZ(rl, &curPos)
if err != nil {
log.Err(err).Msg("could not get value for offsetZ of characteristic '" + cv.characteristic.Name + "'")
}
case "Offset4":
cv.offset4Value, err = cd.getOffset4(rl, &curPos)
if err != nil {
log.Err(err).Msg("could not get value for offset4 of characteristic '" + cv.characteristic.Name + "'")
}
case "Offset5":
cv.offset5Value, err = cd.getOffset5(rl, &curPos)
if err != nil {
log.Err(err).Msg("could not get value for offset5 of characteristic '" + cv.characteristic.Name + "'")
}
case "Reserved":
err = cd.getReserved(rl, &curPos)
if err != nil {
log.Err(err).Msg("could not get offset from reserved datasize characteristic '" + cv.characteristic.Name + "'")
}
/*RIP_ADDR_X-Y-Z-4-5-W
are only used to store interpolation results,
so they are not read from hex as there is nothing to read
case "RipAddrW":
case "RipAddrX":
case "RipAddrY":
case "RipAddrZ":
case "RipAddr4":
case "RipAddr5":
/*SRC_ADDR_X-Y-Z-4-5
define a input quantity meaning a measurement that determines which point of an axis is chosen for reading a value from a given characteristic.
e.g. given a curve where the x-Axis is the rpm of the engine and the values are
a defined relative engine load:
x 1000 2000 3000 4000 5000 6000
v 12 27 38 42 49 18
then if the input quantity for this curve would be the measurement of the current rpm of the engine
depending on the rpm a value is chosen.
case "SrcAddrX":
case "SrcAddrY":
case "SrcAddrZ":
case "SrcAddr4":
case "SrcAddr5":
*/
case "ShiftOpX":
cv.shiftOpXValue, err = cd.getShiftOpX(rl, &curPos)
if err != nil {
log.Err(err).Msg("could not get value for shiftOpX of characteristic '" + cv.characteristic.Name + "'")
}
case "ShiftOpY":
cv.shiftOpYValue, err = cd.getShiftOpY(rl, &curPos)
if err != nil {
log.Err(err).Msg("could not get value for shiftOpY of characteristic '" + cv.characteristic.Name + "'")
}
case "ShiftOpZ":
cv.shiftOpZValue, err = cd.getShiftOpZ(rl, &curPos)
if err != nil {
log.Err(err).Msg("could not get value for shiftOpZ of characteristic '" + cv.characteristic.Name + "'")
}
case "ShiftOp4":
cv.shiftOp4Value, err = cd.getShiftOp4(rl, &curPos)
if err != nil {
log.Err(err).Msg("could not get value for shiftOp4 of characteristic '" + cv.characteristic.Name + "'")
}
case "ShiftOp5":
cv.shiftOp5Value, err = cd.getShiftOp5(rl, &curPos)
if err != nil {
log.Err(err).Msg("could not get value for shiftOp5 of characteristic '" + cv.characteristic.Name + "'")
}
default:
err := errors.New("undefined case in record layout position")
if err != nil {
log.Err(err).Msg("unexpected case '" + field + "' in characteristic '" + cv.characteristic.Name + "'")
}
}
}
}
func (cd *CalibrationData) getValue(curPos *uint32, dte a2l.DataTypeEnum, rl *a2l.RecordLayout) ([]byte, error) {
bytes, err := cd.getBytes(cd.getNextAlignedAddress(*curPos, dte, rl), uint32(dte.GetDatatypeLength()))
if err != nil {
log.Err(err).Msg("could not retrieve value as byteSlice")
return nil, err
}
return bytes, nil
}
func (cd *CalibrationData) getFncValues(rl *a2l.RecordLayout, curPos *uint32, cv *CharacteristicValues) (interface{}, error) {
//check access type. DIRECT is the most used. Just read value from a given address.
//in case other access types are set this gets more complicated as either offsets or pointers are leveraged to
//define the position of the calibration objects.
//for VALUE Type: just read one value at curPos
//for higher level objects: read the number of elements defined by the matrix dim or NoAxisPts fields with the direction (row, column, alternate, ...) specified.
//for applicable objects check whether STATIC_RECORD_LAYOUT and STATIC_ ADDRESS_OFFSET are set
//to determine how to read the FNC_Values correctly, this can lead to hard to detect errors when not implemented.
//for now just read the values as they are defined in the A2L file.
if !rl.FncValues.AddresstypeSet {
rl.FncValues.Addresstype = a2l.DIRECT
}
switch rl.FncValues.Addresstype {
case a2l.DIRECT:
valBytes, err := cd.getValue(curPos, a2l.UBYTE, rl)
if err != nil {
log.Err(err).Msg("could not get byte of new adress for fncValues of characteristic '" + cv.characteristic.Name + "'")
return nil, err
}
val, err := cd.convertByteSliceToDatatype(valBytes, a2l.UBYTE)
if err != nil {
log.Err(err).Msg("could not get adress from new adress bytes for fncValues of characteristic '" + cv.characteristic.Name + "'")
return nil, err
}
log.Info().Msg(strconv.FormatFloat(val, 'f', 0, 64))
case a2l.PBYTE:
newAdressBytes, err := cd.getValue(curPos, a2l.UBYTE, rl)
if err != nil {
log.Err(err).Msg("could not get byte of new adress for fncValues of characteristic '" + cv.characteristic.Name + "'")
return nil, err
}
newAdress, err := cd.convertByteSliceToDatatype(newAdressBytes, a2l.UBYTE)
if err != nil {
log.Err(err).Msg("could not get adress from new adress bytes for fncValues of characteristic '" + cv.characteristic.Name + "'")
return nil, err
}
log.Info().Msg(strconv.FormatFloat(newAdress, 'f', 0, 64))
//the address contains an one byte pointer to the first axis point or value
case a2l.PWORD:
//the address contains an two byte pointer to the first axis point or value
case a2l.PLONG:
//the address contains an four byte pointer to the first axis point or value
case a2l.PLONGLONG:
//the address contains an eight byte pointer to the first axis point or value
default:
//no valid address type
err := errors.New("invalid address type for fncValues")
log.Err(err).Msg("invalid address type for fncValues of characteristic '" + cv.characteristic.Name + "'")
return nil, err
}
return nil, nil
}