From 4a37d6cdbe950b5f9a95ccdff744949a07b91519 Mon Sep 17 00:00:00 2001 From: asap2Go <96501510+asap2Go@users.noreply.github.com> Date: Fri, 18 Nov 2022 23:05:24 +0100 Subject: [PATCH] continued implementation of reading out hex values added offset --- a2l/offset_4.go | 33 +++++++++------ a2l/offset_5.go | 33 +++++++++------ a2l/offset_x.go | 33 +++++++++------ a2l/offset_y.go | 27 +++++++----- a2l/offset_z.go | 33 +++++++++------ a2l/record_layout.go | 58 +++++++++++++------------- calib_data.go | 24 ++++++++++- characteristic_values.go | 51 ++++++++++++----------- offset.go | 88 ++++++++++++++++++++++++++++++++++++++++ 9 files changed, 264 insertions(+), 116 deletions(-) create mode 100644 offset.go diff --git a/a2l/offset_4.go b/a2l/offset_4.go index 6d55585..7debb5b 100644 --- a/a2l/offset_4.go +++ b/a2l/offset_4.go @@ -7,15 +7,22 @@ import ( "github.com/rs/zerolog/log" ) -type offset4 struct { - position uint16 - positionSet bool - datatype DataTypeEnum - datatypeSet bool +/* +Offset4 is the description of the 'offset' parameter in the deposit structure to compute the axis points for +fixed characteristic curves and fixed characteristic maps (see also keyword +FIX_AXIS_PAR). The axis points for fixed characteristic curves or fixed characteristic +maps are derived from the two 'offset' and 'shift' parameters as follows: +Xi = Offset + (i - 1)*2Shift i = { 1...numberofaxispts } +*/ +type Offset4 struct { + Position uint16 + PositionSet bool + Datatype DataTypeEnum + DatatypeSet bool } -func parseOffset4(tok *tokenGenerator) (offset4, error) { - o4 := offset4{} +func parseOffset4(tok *tokenGenerator) (Offset4, error) { + o4 := Offset4{} var err error forLoop: for { @@ -28,23 +35,23 @@ forLoop: err = errors.New("unexpected token " + tok.current()) log.Err(err).Msg("offset4 could not be parsed") break forLoop - } else if !o4.positionSet { + } else if !o4.PositionSet { var buf uint64 buf, err = strconv.ParseUint(tok.current(), 10, 16) if err != nil { log.Err(err).Msg("offset4 position could not be parsed") break forLoop } - o4.position = uint16(buf) - o4.positionSet = true + o4.Position = uint16(buf) + o4.PositionSet = true log.Info().Msg("offset4 position successfully parsed") - } else if !o4.datatypeSet { - o4.datatype, err = parseDataTypeEnum(tok) + } else if !o4.DatatypeSet { + o4.Datatype, err = parseDataTypeEnum(tok) if err != nil { log.Err(err).Msg("offset4 datatype could not be parsed") break forLoop } - o4.datatypeSet = true + o4.DatatypeSet = true log.Info().Msg("offset4 datatype successfully parsed") break forLoop } diff --git a/a2l/offset_5.go b/a2l/offset_5.go index 95037e4..8f9529a 100644 --- a/a2l/offset_5.go +++ b/a2l/offset_5.go @@ -7,15 +7,22 @@ import ( "github.com/rs/zerolog/log" ) -type offset5 struct { - position uint16 - positionSet bool - datatype DataTypeEnum - datatypeSet bool +/* +Offset5 is the description of the 'offset' parameter in the deposit structure to compute the axis points for +fixed characteristic curves and fixed characteristic maps (see also keyword +FIX_AXIS_PAR). The axis points for fixed characteristic curves or fixed characteristic +maps are derived from the two 'offset' and 'shift' parameters as follows: +Xi = Offset + (i - 1)*2Shift i = { 1...numberofaxispts } +*/ +type Offset5 struct { + Position uint16 + PositionSet bool + Datatype DataTypeEnum + DatatypeSet bool } -func parseOffset5(tok *tokenGenerator) (offset5, error) { - o5 := offset5{} +func parseOffset5(tok *tokenGenerator) (Offset5, error) { + o5 := Offset5{} var err error forLoop: for { @@ -28,23 +35,23 @@ forLoop: err = errors.New("unexpected token " + tok.current()) log.Err(err).Msg("offset5 could not be parsed") break forLoop - } else if !o5.positionSet { + } else if !o5.PositionSet { var buf uint64 buf, err = strconv.ParseUint(tok.current(), 10, 16) if err != nil { log.Err(err).Msg("offset5 position could not be parsed") break forLoop } - o5.position = uint16(buf) - o5.positionSet = true + o5.Position = uint16(buf) + o5.PositionSet = true log.Info().Msg("offset5 position successfully parsed") - } else if !o5.datatypeSet { - o5.datatype, err = parseDataTypeEnum(tok) + } else if !o5.DatatypeSet { + o5.Datatype, err = parseDataTypeEnum(tok) if err != nil { log.Err(err).Msg("offset5 datatype could not be parsed") break forLoop } - o5.datatypeSet = true + o5.DatatypeSet = true log.Info().Msg("offset5 datatype successfully parsed") break forLoop } diff --git a/a2l/offset_x.go b/a2l/offset_x.go index b509634..8ef52aa 100644 --- a/a2l/offset_x.go +++ b/a2l/offset_x.go @@ -7,15 +7,22 @@ import ( "github.com/rs/zerolog/log" ) -type offsetX struct { - position uint16 - positionSet bool - datatype DataTypeEnum - datatypeSet bool +/* +OffsetX is the description of the 'offset' parameter in the deposit structure to compute the axis points for +fixed characteristic curves and fixed characteristic maps (see also keyword +FIX_AXIS_PAR). The axis points for fixed characteristic curves or fixed characteristic +maps are derived from the two 'offset' and 'shift' parameters as follows: +Xi = Offset + (i - 1)*2Shift i = { 1...numberofaxispts } +*/ +type OffsetX struct { + Position uint16 + PositionSet bool + Datatype DataTypeEnum + DatatypeSet bool } -func parseOffsetX(tok *tokenGenerator) (offsetX, error) { - ox := offsetX{} +func parseOffsetX(tok *tokenGenerator) (OffsetX, error) { + ox := OffsetX{} var err error forLoop: for { @@ -28,23 +35,23 @@ forLoop: err = errors.New("unexpected token " + tok.current()) log.Err(err).Msg("offsetX could not be parsed") break forLoop - } else if !ox.positionSet { + } else if !ox.PositionSet { var buf uint64 buf, err = strconv.ParseUint(tok.current(), 10, 16) if err != nil { log.Err(err).Msg("offsetx position could not be parsed") break forLoop } - ox.position = uint16(buf) - ox.positionSet = true + ox.Position = uint16(buf) + ox.PositionSet = true log.Info().Msg("offsetx position successfully parsed") - } else if !ox.datatypeSet { - ox.datatype, err = parseDataTypeEnum(tok) + } else if !ox.DatatypeSet { + ox.Datatype, err = parseDataTypeEnum(tok) if err != nil { log.Err(err).Msg("offsetx datatype could not be parsed") break forLoop } - ox.datatypeSet = true + ox.DatatypeSet = true log.Info().Msg("offsetx datatype successfully parsed") break forLoop } diff --git a/a2l/offset_y.go b/a2l/offset_y.go index 7795cf2..0d228c3 100644 --- a/a2l/offset_y.go +++ b/a2l/offset_y.go @@ -7,11 +7,18 @@ import ( "github.com/rs/zerolog/log" ) +/* +OffsetY is the description of the 'offset' parameter in the deposit structure to compute the axis points for +fixed characteristic curves and fixed characteristic maps (see also keyword +FIX_AXIS_PAR). The axis points for fixed characteristic curves or fixed characteristic +maps are derived from the two 'offset' and 'shift' parameters as follows: +Xi = Offset + (i - 1)*2Shift i = { 1...numberofaxispts } +*/ type offsetY struct { - position uint16 - positionSet bool - datatype DataTypeEnum - datatypeSet bool + Position uint16 + PositionSet bool + Datatype DataTypeEnum + DatatypeSet bool } func parseOffsetY(tok *tokenGenerator) (offsetY, error) { @@ -28,23 +35,23 @@ forLoop: err = errors.New("unexpected token " + tok.current()) log.Err(err).Msg("offsetY could not be parsed") break forLoop - } else if !oy.positionSet { + } else if !oy.PositionSet { var buf uint64 buf, err = strconv.ParseUint(tok.current(), 10, 16) if err != nil { log.Err(err).Msg("offsety position could not be parsed") break forLoop } - oy.position = uint16(buf) - oy.positionSet = true + oy.Position = uint16(buf) + oy.PositionSet = true log.Info().Msg("offsety position successfully parsed") - } else if !oy.datatypeSet { - oy.datatype, err = parseDataTypeEnum(tok) + } else if !oy.DatatypeSet { + oy.Datatype, err = parseDataTypeEnum(tok) if err != nil { log.Err(err).Msg("offsety datatype could not be parsed") break forLoop } - oy.datatypeSet = true + oy.DatatypeSet = true log.Info().Msg("offsety datatype successfully parsed") break forLoop } diff --git a/a2l/offset_z.go b/a2l/offset_z.go index be7fb03..687a783 100644 --- a/a2l/offset_z.go +++ b/a2l/offset_z.go @@ -7,15 +7,22 @@ import ( "github.com/rs/zerolog/log" ) -type offsetZ struct { - position uint16 - positionSet bool - datatype DataTypeEnum - datatypeSet bool +/* +OffsetZ is the description of the 'offset' parameter in the deposit structure to compute the axis points for +fixed characteristic curves and fixed characteristic maps (see also keyword +FIX_AXIS_PAR). The axis points for fixed characteristic curves or fixed characteristic +maps are derived from the two 'offset' and 'shift' parameters as follows: +Xi = Offset + (i - 1)*2Shift i = { 1...numberofaxispts } +*/ +type OffsetZ struct { + Position uint16 + PositionSet bool + Datatype DataTypeEnum + DatatypeSet bool } -func parseOffsetZ(tok *tokenGenerator) (offsetZ, error) { - oz := offsetZ{} +func parseOffsetZ(tok *tokenGenerator) (OffsetZ, error) { + oz := OffsetZ{} var err error forLoop: for { @@ -28,23 +35,23 @@ forLoop: err = errors.New("unexpected token " + tok.current()) log.Err(err).Msg("offsetZ could not be parsed") break forLoop - } else if !oz.positionSet { + } else if !oz.PositionSet { var buf uint64 buf, err = strconv.ParseUint(tok.current(), 10, 16) if err != nil { log.Err(err).Msg("offsetz position could not be parsed") break forLoop } - oz.position = uint16(buf) - oz.positionSet = true + oz.Position = uint16(buf) + oz.PositionSet = true log.Info().Msg("offsetz position successfully parsed") - } else if !oz.datatypeSet { - oz.datatype, err = parseDataTypeEnum(tok) + } else if !oz.DatatypeSet { + oz.Datatype, err = parseDataTypeEnum(tok) if err != nil { log.Err(err).Msg("offsetz datatype could not be parsed") break forLoop } - oz.datatypeSet = true + oz.DatatypeSet = true log.Info().Msg("offsetz datatype successfully parsed") break forLoop } diff --git a/a2l/record_layout.go b/a2l/record_layout.go index f59d8c8..403faa5 100644 --- a/a2l/record_layout.go +++ b/a2l/record_layout.go @@ -40,11 +40,11 @@ type RecordLayout struct { NoAxisPts4 NoAxisPts4 NoAxisPts5 NoAxisPts5 NoRescaleX NoRescaleX - OffsetX offsetX + OffsetX OffsetX OffsetY offsetY - OffsetZ offsetZ - Offset4 offset4 - Offset5 offset5 + OffsetZ OffsetZ + Offset4 Offset4 + Offset5 Offset5 Reserved reserved RipAddrW ripAddrW RipAddrX ripAddrX @@ -695,54 +695,54 @@ func (rl *RecordLayout) GetRecordLayoutRelativePositions() (map[uint16]string, e order[rl.NoRescaleX.Position] = "NoRescaleX" } - if rl.Offset4.positionSet { - field, exists := order[rl.Offset4.position] + if rl.Offset4.PositionSet { + field, exists := order[rl.Offset4.Position] if exists { err = errors.New("position set twice in RecordLayout " + rl.Name + " for " + field + " and Offset4") log.Err(err).Msg("recordLayout relative positions could not be determined") return order, err } - order[rl.Offset4.position] = "Offset4" + order[rl.Offset4.Position] = "Offset4" } - if rl.Offset5.positionSet { - field, exists := order[rl.Offset5.position] + if rl.Offset5.PositionSet { + field, exists := order[rl.Offset5.Position] if exists { err = errors.New("position set twice in RecordLayout " + rl.Name + " for " + field + " and Offset5") log.Err(err).Msg("recordLayout relative positions could not be determined") return order, err } - order[rl.Offset5.position] = "Offset5" + order[rl.Offset5.Position] = "Offset5" } - if rl.OffsetX.positionSet { - field, exists := order[rl.OffsetX.position] + if rl.OffsetX.PositionSet { + field, exists := order[rl.OffsetX.Position] if exists { err = errors.New("position set twice in RecordLayout " + rl.Name + " for " + field + " and OffsetX") log.Err(err).Msg("recordLayout relative positions could not be determined") return order, err } - order[rl.OffsetX.position] = "OffsetX" + order[rl.OffsetX.Position] = "OffsetX" } - if rl.OffsetY.positionSet { - field, exists := order[rl.OffsetY.position] + if rl.OffsetY.PositionSet { + field, exists := order[rl.OffsetY.Position] if exists { err = errors.New("position set twice in RecordLayout " + rl.Name + " for " + field + " and OffsetY") log.Err(err).Msg("recordLayout relative positions could not be determined") return order, err } - order[rl.OffsetY.position] = "OffsetY" + order[rl.OffsetY.Position] = "OffsetY" } - if rl.OffsetZ.positionSet { - field, exists := order[rl.OffsetZ.position] + if rl.OffsetZ.PositionSet { + field, exists := order[rl.OffsetZ.Position] if exists { err = errors.New("position set twice in RecordLayout " + rl.Name + " for " + field + " and OffsetZ") log.Err(err).Msg("recordLayout relative positions could not be determined") return order, err } - order[rl.OffsetZ.position] = "OffsetZ" + order[rl.OffsetZ.Position] = "OffsetZ" } if rl.Reserved.positionSet { @@ -1056,40 +1056,40 @@ func (rl *RecordLayout) GetDatatypeByFieldName(name string) (DataTypeEnum, error } return rl.NoRescaleX.Datatype, nil case "OffsetX": - if !rl.OffsetX.datatypeSet { + if !rl.OffsetX.DatatypeSet { err := errors.New("no datatype set for " + name + " in record layout " + rl.Name) log.Err(err).Msg("could not get datatype") return undefinedDatatype, err } - return rl.OffsetX.datatype, nil + return rl.OffsetX.Datatype, nil case "OffsetY": - if !rl.OffsetY.datatypeSet { + if !rl.OffsetY.DatatypeSet { err := errors.New("no datatype set for " + name + " in record layout " + rl.Name) log.Err(err).Msg("could not get datatype") return undefinedDatatype, err } - return rl.OffsetY.datatype, nil + return rl.OffsetY.Datatype, nil case "OffsetZ": - if !rl.OffsetZ.datatypeSet { + if !rl.OffsetZ.DatatypeSet { err := errors.New("no datatype set for " + name + " in record layout " + rl.Name) log.Err(err).Msg("could not get datatype") return undefinedDatatype, err } - return rl.OffsetZ.datatype, nil + return rl.OffsetZ.Datatype, nil case "Offset4": - if !rl.Offset4.datatypeSet { + if !rl.Offset4.DatatypeSet { err := errors.New("no datatype set for " + name + " in record layout " + rl.Name) log.Err(err).Msg("could not get datatype") return undefinedDatatype, err } - return rl.Offset4.datatype, nil + return rl.Offset4.Datatype, nil case "Offset5": - if !rl.Offset5.datatypeSet { + if !rl.Offset5.DatatypeSet { err := errors.New("no datatype set for " + name + " in record layout " + rl.Name) log.Err(err).Msg("could not get datatype") return undefinedDatatype, err } - return rl.Offset5.datatype, nil + return rl.Offset5.Datatype, nil case "RipAddrW": if !rl.RipAddrW.datatypeSet { err := errors.New("no datatype set for " + name + " in record layout " + rl.Name) diff --git a/calib_data.go b/calib_data.go index 8c06126..43e852b 100644 --- a/calib_data.go +++ b/calib_data.go @@ -590,7 +590,7 @@ func (cd *CalibrationData) getValuesFromHex(cv *CharacteristicValues) { case "FncValues": //interesting part case "Identification": - cv.Identification, err = cd.getDistOp5(rl, &curPos) + cv.IdentificationValue, err = cd.getDistOp5(rl, &curPos) if err != nil { log.Err(err).Msg("could not get value for identification of characteristic '" + cv.characteristic.Name + "'") } @@ -622,13 +622,33 @@ func (cd *CalibrationData) getValuesFromHex(cv *CharacteristicValues) { case "NoRescaleX": 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 + "'") + 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": case "RipAddrW": case "RipAddrX": diff --git a/characteristic_values.go b/characteristic_values.go index 606493a..f588bc0 100644 --- a/characteristic_values.go +++ b/characteristic_values.go @@ -5,29 +5,34 @@ import ( ) type CharacteristicValues struct { - characteristic *a2l.Characteristic - recordLayout *a2l.RecordLayout - AxisXValues interface{} - AxisYValues interface{} - AxisZValues interface{} - Axis4Values interface{} - Axis5Values interface{} - DistOpXValue interface{} - DistOpYValue interface{} - DistOpZValue interface{} - DistOp4Value interface{} - DistOp5Value interface{} - Identification interface{} - NoAxisPtsXValue interface{} - NoAxisPtsYValue interface{} - NoAxisPtsZValue interface{} - NoAxisPts4Value interface{} - NoAxisPts5Value interface{} - NoRescaleXValue interface{} - valuesBin interface{} - valuesHex interface{} - valuesDec interface{} - valuesPhy interface{} + characteristic *a2l.Characteristic + recordLayout *a2l.RecordLayout + AxisXValues interface{} + AxisYValues interface{} + AxisZValues interface{} + Axis4Values interface{} + Axis5Values interface{} + DistOpXValue interface{} + DistOpYValue interface{} + DistOpZValue interface{} + DistOp4Value interface{} + DistOp5Value interface{} + IdentificationValue interface{} + NoAxisPtsXValue interface{} + NoAxisPtsYValue interface{} + NoAxisPtsZValue interface{} + NoAxisPts4Value interface{} + NoAxisPts5Value interface{} + NoRescaleXValue interface{} + OffsetXValue interface{} + OffsetYValue interface{} + OffsetZValue interface{} + Offset4Value interface{} + Offset5Value interface{} + valuesBin interface{} + valuesHex interface{} + valuesDec interface{} + valuesPhy interface{} } func (cv *CharacteristicValues) getCharacteristicValueDecimal() (interface{}, error) { diff --git a/offset.go b/offset.go new file mode 100644 index 0000000..9943122 --- /dev/null +++ b/offset.go @@ -0,0 +1,88 @@ +package calibrationReader + +import ( + "errors" + + "github.com/asap2Go/calibrationReader/a2l" + "github.com/rs/zerolog/log" +) + +// getOffsetX retrieves the offset operator according to its layout specified within the record layout and their values as calibrated in the hex file +func (cd *CalibrationData) getOffsetX(rl *a2l.RecordLayout, curPos *uint32) (interface{}, error) { + if !rl.OffsetX.DatatypeSet { + err := errors.New("offsetX datatype not set") + log.Err(err).Msg("could not retrieve offsetX value") + return nil, err + } + val, err := cd.getValue(curPos, rl.OffsetX.Datatype, rl) + if err != nil { + log.Err(err).Msg("could not retrieve offsetX value") + return nil, err + } + *curPos += uint32(rl.OffsetX.Datatype.GetDatatypeLength()) + return val, err +} + +// getOffsetY retrieves the offset operator according to its layout specified within the record layout and their values as calibrated in the hex file +func (cd *CalibrationData) getOffsetY(rl *a2l.RecordLayout, curPos *uint32) (interface{}, error) { + if !rl.OffsetY.DatatypeSet { + err := errors.New("offsetY datatype not set") + log.Err(err).Msg("could not retrieve offsetY value") + return nil, err + } + val, err := cd.getValue(curPos, rl.OffsetY.Datatype, rl) + if err != nil { + log.Err(err).Msg("could not retrieve offsetY value") + return nil, err + } + *curPos += uint32(rl.OffsetY.Datatype.GetDatatypeLength()) + return val, err +} + +// getOffsetZ retrieves the offset operator according to its layout specified within the record layout and their values as calibrated in the hex file +func (cd *CalibrationData) getOffsetZ(rl *a2l.RecordLayout, curPos *uint32) (interface{}, error) { + if !rl.OffsetZ.DatatypeSet { + err := errors.New("offsetZ datatype not set") + log.Err(err).Msg("could not retrieve offsetZ value") + return nil, err + } + val, err := cd.getValue(curPos, rl.OffsetZ.Datatype, rl) + if err != nil { + log.Err(err).Msg("could not retrieve offsetZ value") + return nil, err + } + *curPos += uint32(rl.OffsetZ.Datatype.GetDatatypeLength()) + return val, err +} + +// getOffset4 retrieves the offset operator according to its layout specified within the record layout and their values as calibrated in the hex file +func (cd *CalibrationData) getOffset4(rl *a2l.RecordLayout, curPos *uint32) (interface{}, error) { + if !rl.Offset4.DatatypeSet { + err := errors.New("offset4 datatype not set") + log.Err(err).Msg("could not retrieve offset4 value") + return nil, err + } + val, err := cd.getValue(curPos, rl.Offset4.Datatype, rl) + if err != nil { + log.Err(err).Msg("could not retrieve offset4 value") + return nil, err + } + *curPos += uint32(rl.Offset4.Datatype.GetDatatypeLength()) + return val, err +} + +// getOffset5 retrieves the offset operator according to its layout specified within the record layout and their values as calibrated in the hex file +func (cd *CalibrationData) getOffset5(rl *a2l.RecordLayout, curPos *uint32) (interface{}, error) { + if !rl.Offset5.DatatypeSet { + err := errors.New("offset5 datatype not set") + log.Err(err).Msg("could not retrieve offset5 value") + return nil, err + } + val, err := cd.getValue(curPos, rl.Offset5.Datatype, rl) + if err != nil { + log.Err(err).Msg("could not retrieve offset5 value") + return nil, err + } + *curPos += uint32(rl.Offset5.Datatype.GetDatatypeLength()) + return val, err +}