Skip to content

Commit

Permalink
Merge pull request #270 from andlaus/fix_dtc_encoding
Browse files Browse the repository at this point in the history
fix encoding of DTCs
  • Loading branch information
andlaus committed Feb 22, 2024
2 parents 3342bf8 + 05957b9 commit a5d146f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 15 deletions.
10 changes: 7 additions & 3 deletions odxtools/dtcdop.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,17 @@ def convert_physical_to_bytes(self, physical_value: ParameterValue, encode_state
trouble_code = physical_value
elif isinstance(physical_value, str):
# assume that physical value is the short_name
dtc = next(filter(lambda dtc: dtc.short_name == physical_value, self.dtcs))
trouble_code = dtc.trouble_code
dtcs = [dtc for dtc in self.dtcs if dtc.short_name == physical_value]
odxassert(len(dtcs) == 1)
trouble_code = dtcs[0].trouble_code
else:
raise EncodeError(f"The DTC-DOP {self.short_name} expected a"
f" DiagnosticTroubleCode but got {physical_value!r}.")

return super().convert_physical_to_bytes(trouble_code, encode_state, bit_position)
internal_trouble_code = self.compu_method.convert_physical_to_internal(trouble_code)

return self.diag_coded_type.convert_internal_to_bytes(
internal_trouble_code, encode_state=encode_state, bit_position=bit_position)

def _build_odxlinks(self) -> Dict[OdxLinkId, Any]:
odxlinks = super()._build_odxlinks()
Expand Down
8 changes: 2 additions & 6 deletions odxtools/parameters/physicalconstantparameter.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# SPDX-License-Identifier: MIT
import warnings
from dataclasses import dataclass
from typing import TYPE_CHECKING, Any, Dict

Expand Down Expand Up @@ -70,13 +69,10 @@ def decode_from_pdu(self, decode_state: DecodeState) -> ParameterValue:

# Check if decoded value matches expected value
if phys_val != self.physical_constant_value:
warnings.warn(
odxraise(
f"Physical constant parameter does not match! "
f"The parameter {self.short_name} expected physical value "
f"{self.physical_constant_value!r} but got {phys_val!r} "
f"at byte position {decode_state.cursor_byte_position} "
f"in coded message {decode_state.coded_message.hex()}.",
DecodeError,
stacklevel=1,
)
f"in coded message {decode_state.coded_message.hex()}.", DecodeError)
return phys_val
4 changes: 3 additions & 1 deletion odxtools/parameters/valueparameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
class ValueParameter(ParameterWithDOP):
physical_default_value_raw: Optional[str]

def __post_init__(self) -> None:
self._physical_default_value: Optional[AtomicOdxType] = None

@property
def parameter_type(self) -> ParameterType:
return "VALUE"
Expand All @@ -31,7 +34,6 @@ def _resolve_odxlinks(self, odxlinks: OdxLinkDatabase) -> None:
def _resolve_snrefs(self, diag_layer: "DiagLayer") -> None:
super()._resolve_snrefs(diag_layer)

self._physical_default_value: Optional[AtomicOdxType] = None
if self.physical_default_value_raw is not None:
dop = odxrequire(self.dop)
if not isinstance(dop, DataObjectProperty):
Expand Down
15 changes: 10 additions & 5 deletions tests/test_decoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from odxtools.minmaxlengthtype import MinMaxLengthType
from odxtools.nameditemlist import NamedItemList
from odxtools.odxlink import OdxDocFragment, OdxLinkDatabase, OdxLinkId, OdxLinkRef
from odxtools.odxtypes import DataType
from odxtools.odxtypes import DataType, ParameterValueDict
from odxtools.parameters.codedconstparameter import CodedConstParameter
from odxtools.parameters.matchingrequestparameter import MatchingRequestParameter
from odxtools.parameters.physicalconstantparameter import PhysicalConstantParameter
Expand Down Expand Up @@ -1478,7 +1478,7 @@ def test_decode_response(self) -> None:
self.assertEqual(expected_message.coding_object, decoded_message.coding_object)
self.assertEqual(expected_message.param_dict, decoded_message.param_dict)

def test_decode_dtc(self) -> None:
def test_code_dtc(self) -> None:
odxlinks = OdxLinkDatabase()
diag_coded_type = StandardLengthType(
base_data_type=DataType.A_UINT32,
Expand Down Expand Up @@ -1570,9 +1570,14 @@ def test_decode_dtc(self) -> None:
dop._resolve_odxlinks(odxlinks)
pos_response._resolve_odxlinks(odxlinks)

coded_message = bytes([0x12, 0x34])
decoded_param_dict = pos_response.decode(coded_message)
self.assertEqual(decoded_param_dict["DTC_Param"], dtc1)
expected_coded_message = bytes([0x12, 0x34])
expected_param_dict: ParameterValueDict = {"SID": 0x12, "DTC_Param": dtc1}

actual_param_dict = pos_response.decode(expected_coded_message)
self.assertEqual(actual_param_dict, expected_param_dict)

actual_coded_message = pos_response.encode(coded_request=None, **expected_param_dict)
self.assertEqual(actual_coded_message, expected_coded_message)


class TestDecodingAndEncoding(unittest.TestCase):
Expand Down

0 comments on commit a5d146f

Please sign in to comment.