Skip to content

Commit

Permalink
Raising :exc:~can.exceptions.CanOperationError if the bus is not op…
Browse files Browse the repository at this point in the history
…en on some methods call

Raising :exc:`~can.exceptions.CanOperationError` if the bus is not open on some methods call
  • Loading branch information
pierreluctg committed Apr 16, 2024
1 parent 4a41409 commit f45d53f
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion can/interfaces/ics_neovi/neovi_bus.py
Expand Up @@ -7,7 +7,7 @@
Implementation references:
* https://github.com/intrepidcs/python_ics
"""

import functools
import logging
import os
import tempfile
Expand Down Expand Up @@ -129,6 +129,19 @@ class ICSOperationError(ICSApiError, CanOperationError):
pass


def check_if_bus_open(func):
"""Decorator that raises :exc:`~can.exceptions.CanOperationError` if the bus is not open."""

@functools.wraps(func)
def wrapper(self, *args, **kwargs):
"""Raises :exc:`~can.exceptions.CanOperationError` if the bus is not open, else calls the wrapped function."""
if self._is_shutdown:
raise CanOperationError("Cannot operate on a closed bus")
return func(self, *args, **kwargs)

return wrapper


class NeoViBus(BusABC):
"""
The CAN Bus implemented for the python_ics interface
Expand Down Expand Up @@ -312,6 +325,7 @@ def _find_device(self, type_filter=None, serial=None):
msg.append("found.")
raise CanInitializationError(" ".join(msg))

@check_if_bus_open
def _process_msg_queue(self, timeout=0.1):
try:
messages, errors = ics.get_messages(self.dev, False, timeout)
Expand Down Expand Up @@ -409,6 +423,7 @@ def _recv_internal(self, timeout=0.1):
return None, False
return msg, False

@check_if_bus_open
def send(self, msg, timeout=0):
"""Transmit a message to the CAN bus.
Expand Down

0 comments on commit f45d53f

Please sign in to comment.