11# SPDX-License-Identifier: MIT
22from dataclasses import dataclass
3- from typing import List , Optional
3+ from enum import Enum
4+ from typing import List , Optional , cast
45from xml .etree import ElementTree
56
67from typing_extensions import override
1516from .utils import dataclass_fields_asdict
1617
1718
19+ class Termination (Enum ):
20+ END_OF_PDU = "END-OF-PDU"
21+ ZERO = "ZERO"
22+ HEX_FF = "HEX-FF"
23+
24+
1825@dataclass
1926class MinMaxLengthType (DiagCodedType ):
2027 min_length : int
2128 max_length : Optional [int ]
22- termination : str
29+ termination : Termination
2330
2431 @property
2532 def dct_type (self ) -> DctType :
@@ -35,7 +42,13 @@ def from_et(et_element: ElementTree.Element,
3542 max_length = None
3643 if et_element .find ("MAX-LENGTH" ) is not None :
3744 max_length = int (odxrequire (et_element .findtext ("MAX-LENGTH" )))
38- termination = odxrequire (et_element .get ("TERMINATION" ))
45+
46+ termination_str = odxrequire (et_element .get ("TERMINATION" ))
47+ try :
48+ termination = Termination (termination_str )
49+ except ValueError :
50+ termination = cast (Termination , None )
51+ odxraise (f"Encountered unknown termination type '{ termination_str } '" )
3952
4053 return MinMaxLengthType (
4154 min_length = min_length , max_length = max_length , termination = termination , ** kwargs )
@@ -49,23 +62,18 @@ def __post_init__(self) -> None:
4962 DataType .A_UNICODE2STRING ,
5063 DataType .A_UTF8STRING ,
5164 ], f"A min-max length type cannot have the base data type { self .base_data_type } ." )
52- odxassert (self .termination in [
53- "ZERO" ,
54- "HEX-FF" ,
55- "END-OF-PDU" ,
56- ], f"A min-max length type cannot have the termination { self .termination } " )
5765
5866 def __termination_sequence (self ) -> bytes :
5967 """Returns the termination byte sequence if it isn't defined."""
6068 # The termination sequence is actually not specified by ASAM
6169 # for A_BYTEFIELD but I assume it is only one byte.
6270 termination_sequence = b''
63- if self .termination == " ZERO" :
71+ if self .termination == Termination . ZERO :
6472 if self .base_data_type not in [DataType .A_UNICODE2STRING ]:
6573 termination_sequence = bytes ([0x0 ])
6674 else :
6775 termination_sequence = bytes ([0x0 , 0x0 ])
68- elif self .termination == "HEX-FF" :
76+ elif self .termination == Termination . HEX_FF :
6977 if self .base_data_type not in [DataType .A_UNICODE2STRING ]:
7078 termination_sequence = bytes ([0xFF ])
7179 else :
@@ -121,7 +129,7 @@ def encode_into_pdu(self, internal_value: AtomicOdxType, encode_state: EncodeSta
121129 # encountered within the encoded value.
122130
123131 odxassert (
124- self .termination != "END-OF-PDU" or encode_state .is_end_of_pdu ,
132+ self .termination != Termination . END_OF_PDU or encode_state .is_end_of_pdu ,
125133 "Encountered a MIN-MAX-LENGTH type with END-OF-PDU termination "
126134 "which is not located at the end of the PDU" )
127135 if encode_state .is_end_of_pdu or data_length == self .max_length :
@@ -153,7 +161,7 @@ def decode_from_pdu(self, decode_state: DecodeState) -> AtomicOdxType:
153161 if self .max_length is not None :
154162 max_terminator_pos = min (max_terminator_pos , orig_cursor_pos + self .max_length )
155163
156- if self .termination != "END-OF-PDU" :
164+ if self .termination != Termination . END_OF_PDU :
157165 # The parameter either ends after the maximum length, at
158166 # the end of the PDU or if a termination sequence is
159167 # found.
0 commit comments