-
Notifications
You must be signed in to change notification settings - Fork 0
/
calib_data.go
199 lines (182 loc) · 5.86 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
/*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"
"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 a2l.A2L
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
var errChan = make(chan error, 2)
var a2lChan = make(chan a2l.A2L, 1)
var hexChan = make(chan map[uint32]byte, 1)
wgReaders := new(sync.WaitGroup)
err = configureLogger()
if err != nil {
log.Err(err).Msg("could not create logger:")
return cd, err
}
wgReaders.Add(2)
go readA2L(wgReaders, a2lChan, errChan, a2lFilePath)
go readHex(wgReaders, hexChan, errChan, hexFilePath)
wgReaders.Wait()
close(errChan)
//check if any errors have occured within the readers
var firstErr error
if len(errChan) > 0 {
for e := range errChan {
if e != nil {
firstErr = e
}
log.Err(e).Msg("reader encountered an error:")
}
return cd, firstErr
}
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
close(ca)
} else {
ca <- a
close(ca)
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()
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
close(ch)
} else {
ch <- h
close(ch)
log.Info().Msg("parsed hex file")
}
} 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
close(ch)
} else {
ch <- h
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
close(ch)
}
}
//configureLogger adds a file logger, resets previous log file and does some formatting
func configureLogger() error {
var err error
var file *os.File
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
}
func (cd *CalibrationData) getObjectsByIdent(ident string) []interface{} {
var calibrationObjects []interface{}
var buf interface{}
var exists bool
for _, m := range cd.a2l.Project.Modules {
buf, exists = m.AxisPts[ident]
if exists {
calibrationObjects = append(calibrationObjects, buf)
}
buf, exists = m.Characteristics[ident]
if exists {
calibrationObjects = append(calibrationObjects, buf)
}
buf, exists = m.CompuMethods[ident]
if exists {
calibrationObjects = append(calibrationObjects, buf)
}
buf, exists = m.CompuTabs[ident]
if exists {
calibrationObjects = append(calibrationObjects, buf)
}
buf, exists = m.CompuVTabs[ident]
if exists {
calibrationObjects = append(calibrationObjects, buf)
}
buf, exists = m.CompuVTabRanges[ident]
if exists {
calibrationObjects = append(calibrationObjects, buf)
}
buf, exists = m.Functions[ident]
if exists {
calibrationObjects = append(calibrationObjects, buf)
}
buf, exists = m.Groups[ident]
if exists {
calibrationObjects = append(calibrationObjects, buf)
}
buf, exists = m.Measurements[ident]
if exists {
calibrationObjects = append(calibrationObjects, buf)
}
buf, exists = m.RecordLayouts[ident]
if exists {
calibrationObjects = append(calibrationObjects, buf)
}
buf, exists = m.Units[ident]
if exists {
calibrationObjects = append(calibrationObjects, buf)
}
}
return calibrationObjects
}