Skip to content

Commit

Permalink
improve robustness against unknown HardwareType values (#1502)
Browse files Browse the repository at this point in the history
  • Loading branch information
zariiii9003 authored Jan 25, 2023
1 parent 86a91bb commit dcf15c9
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions can/interfaces/vector/canlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,19 +299,21 @@ def _find_global_channel_idx(
channel_configs: List["VectorChannelConfig"],
) -> int:
if serial is not None:
hw_type: Optional[xldefine.XL_HardwareType] = None
serial_found = False
for channel_config in channel_configs:
if channel_config.serial_number != serial:
continue

hw_type = xldefine.XL_HardwareType(channel_config.hw_type)
serial_found = True
if channel_config.hw_channel == channel:
return channel_config.channel_index

if hw_type is None:
if not serial_found:
err_msg = f"No interface with serial {serial} found."
else:
err_msg = f"Channel {channel} not found on interface {hw_type.name} ({serial})."
err_msg = (
f"Channel {channel} not found on interface with serial {serial}."
)
raise CanInitializationError(
err_msg, error_code=xldefine.XL_Status.XL_ERR_HW_NOT_PRESENT
)
Expand Down Expand Up @@ -915,7 +917,7 @@ def popup_vector_hw_configuration(wait_for_finish: int = 0) -> None:
@staticmethod
def get_application_config(
app_name: str, app_channel: int
) -> Tuple[xldefine.XL_HardwareType, int, int]:
) -> Tuple[Union[int, xldefine.XL_HardwareType], int, int]:
"""Retrieve information for an application in Vector Hardware Configuration.
:param app_name:
Expand Down Expand Up @@ -955,13 +957,13 @@ def get_application_config(
),
function="xlGetApplConfig",
) from None
return xldefine.XL_HardwareType(hw_type.value), hw_index.value, hw_channel.value
return _hw_type(hw_type.value), hw_index.value, hw_channel.value

@staticmethod
def set_application_config(
app_name: str,
app_channel: int,
hw_type: xldefine.XL_HardwareType,
hw_type: Union[int, xldefine.XL_HardwareType],
hw_index: int,
hw_channel: int,
**kwargs: Any,
Expand Down Expand Up @@ -1055,7 +1057,7 @@ class VectorChannelConfig(NamedTuple):
"""NamedTuple which contains the channel properties from Vector XL API."""

name: str
hw_type: xldefine.XL_HardwareType
hw_type: Union[int, xldefine.XL_HardwareType]
hw_index: int
hw_channel: int
channel_index: int
Expand Down Expand Up @@ -1128,7 +1130,7 @@ def get_channel_configs() -> List[VectorChannelConfig]:
xlcc: xlclass.XLchannelConfig = driver_config.channel[i]
vcc = VectorChannelConfig(
name=xlcc.name.decode(),
hw_type=xldefine.XL_HardwareType(xlcc.hwType),
hw_type=_hw_type(xlcc.hwType),
hw_index=xlcc.hwIndex,
hw_channel=xlcc.hwChannel,
channel_index=xlcc.channelIndex,
Expand All @@ -1148,3 +1150,11 @@ def get_channel_configs() -> List[VectorChannelConfig]:
)
channel_list.append(vcc)
return channel_list


def _hw_type(hw_type: int) -> Union[int, xldefine.XL_HardwareType]:
try:
return xldefine.XL_HardwareType(hw_type)
except ValueError:
LOG.warning(f'Unknown XL_HardwareType value "{hw_type}"')
return hw_type

0 comments on commit dcf15c9

Please sign in to comment.