Skip to content

Commit

Permalink
Raising CanOperationError if the bus is not open on some methods call (
Browse files Browse the repository at this point in the history
  • Loading branch information
pierreluctg committed Apr 17, 2024
1 parent 4a41409 commit 7dba449
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions can/interfaces/ics_neovi/neovi_bus.py
Expand Up @@ -8,6 +8,7 @@
* https://github.com/intrepidcs/python_ics
"""

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


def check_if_bus_open(func):
"""
Decorator that checks if the bus is open before executing the function.
If the bus is not open, it raises a CanOperationError.
"""

@functools.wraps(func)
def wrapper(self, *args, **kwargs):
"""
Wrapper function that checks if the bus is open before executing the function.
:raises CanOperationError: If the bus is not open.
"""
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 +334,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 +432,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 7dba449

Please sign in to comment.