-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrecord_manager.go
234 lines (224 loc) · 7.7 KB
/
record_manager.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
package calibrationReader
import (
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"math"
"strconv"
"strings"
"github.com/asap2Go/calibrationReader/a2l"
"github.com/rs/zerolog/log"
"github.com/x448/float16"
)
func (cd *CalibrationData) getSystemConstant(ident string) (a2l.SystemConstant, error) {
modPar := cd.A2l.Project.Modules[cd.ModuleIndex].ModPar
s, exists := modPar.SystemConstants[ident]
if !exists {
err := errors.New("no system constant with name " + ident)
log.Err(err).Msg("system constant not found")
return s, err
}
return s, nil
}
func (cd *CalibrationData) getSystemConstantValue(ident string) (string, error) {
sc, err := cd.getSystemConstant(ident)
if err != nil {
log.Err(err).Msg("could not get value of system constant")
return "", err
}
var val string
if !sc.ValueSet {
err = errors.New("no value defined in system constant " + sc.Name)
log.Err(err).Msg("could not get value of system constant")
return "", err
}
return val, nil
}
// GetObjectByIdent returns an object with a given identifier that is defined within the a2l
// not all datastructures are checked. Only the most relevant ones
func (cd *CalibrationData) GetObjectByIdent(ident string) []interface{} {
var calibrationObjects []interface{}
var buf interface{}
var exists bool
m := cd.A2l.Project.Modules[cd.ModuleIndex]
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.ModPar.SystemConstants[ident]
if exists {
calibrationObjects = append(calibrationObjects, buf)
}
buf, exists = m.Units[ident]
if exists {
calibrationObjects = append(calibrationObjects, buf)
}
return calibrationObjects
}
// hexToByteSlice converts at least a four character hexString to a slice of several bytes. fails if input is too short or not valid hex.
func hexToByteSlice(hexVal string) ([]byte, error) {
decoded, err := hex.DecodeString(hexVal)
if err != nil {
log.Err(err).Msg("could not decode hex string " + hexVal)
}
return decoded, err
}
// convertStringToUint32Address is used to convert the adresses in string format in the characteristics to a uint32
func (cd *CalibrationData) convertStringToUint32Address(str string) (uint32, error) {
var val uint32
byteSlice, err := hexToByteSlice(strings.ReplaceAll(str, "0x", ""))
if err != nil {
log.Err(err).Msg("string '" + str + "' could not be parsed")
return val, err
}
modCom := &cd.A2l.Project.Modules[cd.ModuleIndex].ModCommon
if modCom.ByteOrder.ByteOrder == a2l.MsbFirstMswLast || modCom.ByteOrder.ByteOrder == a2l.MsbLastMswFirst {
err = errors.New("unexpected byte order")
log.Err(err).Msg("byte order " + modCom.ByteOrder.ByteOrder.String() + "not implemented")
return val, err
}
if !modCom.ByteOrder.ByteOrderSet || modCom.ByteOrder.ByteOrder == a2l.BigEndian || modCom.ByteOrder.ByteOrder == a2l.MsbFirst {
val = binary.BigEndian.Uint32(byteSlice)
} else {
val = binary.LittleEndian.Uint32(byteSlice)
}
return val, nil
}
// converts a byteSlice into a a2l.DatatypeEnum datatype.
// if not enough bytes are supplied the conversion fails.
// if MsbFirstMswLast or MsbLastMswFirst are used as binary encoding then the conversion fails
// as those are not implemented
func (cd *CalibrationData) convertByteSliceToDatatype(byteSlice []byte, dte a2l.DataTypeEnum) (float64, error) {
//bounds check
if len(byteSlice) == 0 || len(byteSlice)*8 < int(dte.GetDatatypeLength()) {
err := errors.New("byte slice holds " + fmt.Sprintf("%d", len(byteSlice)) + " bytes. " +
strconv.Itoa(int(dte.GetDatatypeLength()/8)) + " bytes necessary to convert to datatype " + dte.String())
log.Err(err).Msg("conversion failed")
return 0.0, err
}
//check which byteorder is used
modCom := &cd.A2l.Project.Modules[cd.ModuleIndex].ModCommon
if modCom.ByteOrder.ByteOrder == a2l.MsbFirstMswLast || modCom.ByteOrder.ByteOrder == a2l.MsbLastMswFirst {
err := errors.New("unexpected byte order")
log.Err(err).Msg("byte order " + modCom.ByteOrder.ByteOrder.String() + "not implemented")
return 0.0, err
}
if !modCom.ByteOrder.ByteOrderSet || modCom.ByteOrder.ByteOrder == a2l.BigEndian || modCom.ByteOrder.ByteOrder == a2l.MsbFirst {
switch dte {
case a2l.UBYTE:
return float64(byteSlice[0]), nil
case a2l.SBYTE:
return float64(int8(byteSlice[0])), nil
case a2l.UWORD:
val := binary.BigEndian.Uint32(byteSlice)
return float64(val), nil
case a2l.SWORD:
val := int32(binary.BigEndian.Uint32(byteSlice))
return float64(val), nil
case a2l.ULONG:
val := binary.BigEndian.Uint64(byteSlice)
return float64(val), nil
case a2l.SLONG:
val := int64(binary.BigEndian.Uint64(byteSlice))
return float64(val), nil
case a2l.AUint64:
val := binary.BigEndian.Uint64(byteSlice)
return float64(val), nil
case a2l.AInt64:
val := int64(binary.BigEndian.Uint64(byteSlice))
return float64(val), nil
case a2l.Float16Ieee:
val := float16.Frombits(binary.BigEndian.Uint16(byteSlice))
return float64(val), nil
case a2l.Float32Ieee:
val := math.Float32frombits(binary.BigEndian.Uint32(byteSlice))
return float64(val), nil
case a2l.Float64Ieee:
val := math.Float64frombits(binary.BigEndian.Uint64(byteSlice))
return float64(val), nil
default:
err := errors.New("unexpected datatype")
log.Err(err).Msg("datatype " + dte.String() + " not implemented")
return 0.0, err
}
} else if modCom.ByteOrder.ByteOrder == a2l.LittleEndian || modCom.ByteOrder.ByteOrder == a2l.MsbLast {
switch dte {
case a2l.UBYTE:
return float64(byteSlice[0]), nil
case a2l.SBYTE:
return float64(int8(byteSlice[0])), nil
case a2l.UWORD:
val := binary.LittleEndian.Uint32(byteSlice)
return float64(val), nil
case a2l.SWORD:
val := int32(binary.LittleEndian.Uint32(byteSlice))
return float64(val), nil
case a2l.ULONG:
val := binary.LittleEndian.Uint64(byteSlice)
return float64(val), nil
case a2l.SLONG:
val := int64(binary.LittleEndian.Uint64(byteSlice))
return float64(val), nil
case a2l.AUint64:
val := binary.LittleEndian.Uint64(byteSlice)
return float64(val), nil
case a2l.AInt64:
val := int64(binary.LittleEndian.Uint64(byteSlice))
return float64(val), nil
case a2l.Float16Ieee:
val := float16.Frombits(binary.LittleEndian.Uint16(byteSlice))
return float64(val), nil
case a2l.Float32Ieee:
val := math.Float32frombits(binary.LittleEndian.Uint32(byteSlice))
return float64(val), nil
case a2l.Float64Ieee:
val := math.Float64frombits(binary.LittleEndian.Uint64(byteSlice))
return float64(val), nil
default:
err := errors.New("unexpected datatype")
log.Err(err).Msg("datatype " + dte.String() + " not implemented")
return 0.0, err
}
} else {
err := errors.New("unexpected byte order")
log.Err(err).Msg("byte order " + modCom.ByteOrder.ByteOrder.String() + " not implemented")
return 0.0, err
}
}