Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions odxtools/multiplexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,34 @@ def decode_from_pdu(self, decode_state: DecodeState) -> ParameterValue:
decode_state.origin_byte_position = orig_origin

return result

@override
def get_static_bit_length(self) -> int | None:
"""
Returns the static bit length of the multiplexer structure, if determinable.

If all cases (including the default, if present) have the same static bit length,
the codec length is considered static and is returned.
Otherwise, returns None to indicate that the size is dynamic.
"""
reference_case = self.default_case if self.default_case else self.cases[0]
if reference_case.structure is None:
return None
case_bit_length = reference_case.structure.get_static_bit_length()
if case_bit_length is None:
return None
for mux_case in self.cases:
if mux_case.structure is None:
return None
case_size = mux_case.structure.get_static_bit_length()
if case_size != case_bit_length:
return None # Found a case with a different or unknown size

switch_key_size = self.switch_key.dop.get_static_bit_length()
if switch_key_size is None:
return None

return max(
switch_key_size + self.switch_key.byte_position * 8 +
(self.switch_key.bit_position if self.switch_key.bit_position else 0),
case_bit_length + self.byte_position * 8)
7 changes: 7 additions & 0 deletions tests/test_diag_data_dictionary_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,13 @@ def test_initialization(self) -> None:
decoded = mux.decode_from_pdu(decode_state)
self.assertEqual(decoded, ("default_case", {}))

self.assertEqual(mux.get_static_bit_length(), None) # mux default case structure is None
mux.default_case.structure_ref = OdxLinkRef.from_id(mux_case2_struct.odx_id)
ecu._resolve_odxlinks(odxlinks)
self.assertEqual(mux.get_static_bit_length(), None) # mux cases doesn't have the same structure size
mux.cases[0].structure_ref = OdxLinkRef.from_id(mux_case2_struct.odx_id)
ecu._resolve_odxlinks(odxlinks)
self.assertEqual(mux.get_static_bit_length(), 16)

if __name__ == "__main__":
unittest.main()
Loading