Skip to content

Commit 7dba449

Browse files
authored
Raising CanOperationError if the bus is not open on some methods call (#1765)
1 parent 4a41409 commit 7dba449

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

can/interfaces/ics_neovi/neovi_bus.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* https://github.com/intrepidcs/python_ics
99
"""
1010

11+
import functools
1112
import logging
1213
import os
1314
import tempfile
@@ -129,6 +130,27 @@ class ICSOperationError(ICSApiError, CanOperationError):
129130
pass
130131

131132

133+
def check_if_bus_open(func):
134+
"""
135+
Decorator that checks if the bus is open before executing the function.
136+
137+
If the bus is not open, it raises a CanOperationError.
138+
"""
139+
140+
@functools.wraps(func)
141+
def wrapper(self, *args, **kwargs):
142+
"""
143+
Wrapper function that checks if the bus is open before executing the function.
144+
145+
:raises CanOperationError: If the bus is not open.
146+
"""
147+
if self._is_shutdown:
148+
raise CanOperationError("Cannot operate on a closed bus")
149+
return func(self, *args, **kwargs)
150+
151+
return wrapper
152+
153+
132154
class NeoViBus(BusABC):
133155
"""
134156
The CAN Bus implemented for the python_ics interface
@@ -312,6 +334,7 @@ def _find_device(self, type_filter=None, serial=None):
312334
msg.append("found.")
313335
raise CanInitializationError(" ".join(msg))
314336

337+
@check_if_bus_open
315338
def _process_msg_queue(self, timeout=0.1):
316339
try:
317340
messages, errors = ics.get_messages(self.dev, False, timeout)
@@ -409,6 +432,7 @@ def _recv_internal(self, timeout=0.1):
409432
return None, False
410433
return msg, False
411434

435+
@check_if_bus_open
412436
def send(self, msg, timeout=0):
413437
"""Transmit a message to the CAN bus.
414438

0 commit comments

Comments
 (0)