Skip to content

Commit dcf15c9

Browse files
authored
improve robustness against unknown HardwareType values (hardbyte#1502)
1 parent 86a91bb commit dcf15c9

1 file changed

Lines changed: 19 additions & 9 deletions

File tree

can/interfaces/vector/canlib.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -299,19 +299,21 @@ def _find_global_channel_idx(
299299
channel_configs: List["VectorChannelConfig"],
300300
) -> int:
301301
if serial is not None:
302-
hw_type: Optional[xldefine.XL_HardwareType] = None
302+
serial_found = False
303303
for channel_config in channel_configs:
304304
if channel_config.serial_number != serial:
305305
continue
306306

307-
hw_type = xldefine.XL_HardwareType(channel_config.hw_type)
307+
serial_found = True
308308
if channel_config.hw_channel == channel:
309309
return channel_config.channel_index
310310

311-
if hw_type is None:
311+
if not serial_found:
312312
err_msg = f"No interface with serial {serial} found."
313313
else:
314-
err_msg = f"Channel {channel} not found on interface {hw_type.name} ({serial})."
314+
err_msg = (
315+
f"Channel {channel} not found on interface with serial {serial}."
316+
)
315317
raise CanInitializationError(
316318
err_msg, error_code=xldefine.XL_Status.XL_ERR_HW_NOT_PRESENT
317319
)
@@ -915,7 +917,7 @@ def popup_vector_hw_configuration(wait_for_finish: int = 0) -> None:
915917
@staticmethod
916918
def get_application_config(
917919
app_name: str, app_channel: int
918-
) -> Tuple[xldefine.XL_HardwareType, int, int]:
920+
) -> Tuple[Union[int, xldefine.XL_HardwareType], int, int]:
919921
"""Retrieve information for an application in Vector Hardware Configuration.
920922
921923
:param app_name:
@@ -955,13 +957,13 @@ def get_application_config(
955957
),
956958
function="xlGetApplConfig",
957959
) from None
958-
return xldefine.XL_HardwareType(hw_type.value), hw_index.value, hw_channel.value
960+
return _hw_type(hw_type.value), hw_index.value, hw_channel.value
959961

960962
@staticmethod
961963
def set_application_config(
962964
app_name: str,
963965
app_channel: int,
964-
hw_type: xldefine.XL_HardwareType,
966+
hw_type: Union[int, xldefine.XL_HardwareType],
965967
hw_index: int,
966968
hw_channel: int,
967969
**kwargs: Any,
@@ -1055,7 +1057,7 @@ class VectorChannelConfig(NamedTuple):
10551057
"""NamedTuple which contains the channel properties from Vector XL API."""
10561058

10571059
name: str
1058-
hw_type: xldefine.XL_HardwareType
1060+
hw_type: Union[int, xldefine.XL_HardwareType]
10591061
hw_index: int
10601062
hw_channel: int
10611063
channel_index: int
@@ -1128,7 +1130,7 @@ def get_channel_configs() -> List[VectorChannelConfig]:
11281130
xlcc: xlclass.XLchannelConfig = driver_config.channel[i]
11291131
vcc = VectorChannelConfig(
11301132
name=xlcc.name.decode(),
1131-
hw_type=xldefine.XL_HardwareType(xlcc.hwType),
1133+
hw_type=_hw_type(xlcc.hwType),
11321134
hw_index=xlcc.hwIndex,
11331135
hw_channel=xlcc.hwChannel,
11341136
channel_index=xlcc.channelIndex,
@@ -1148,3 +1150,11 @@ def get_channel_configs() -> List[VectorChannelConfig]:
11481150
)
11491151
channel_list.append(vcc)
11501152
return channel_list
1153+
1154+
1155+
def _hw_type(hw_type: int) -> Union[int, xldefine.XL_HardwareType]:
1156+
try:
1157+
return xldefine.XL_HardwareType(hw_type)
1158+
except ValueError:
1159+
LOG.warning(f'Unknown XL_HardwareType value "{hw_type}"')
1160+
return hw_type

0 commit comments

Comments
 (0)