Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions can/interfaces/etas/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ def __init__(
)

self._oci_filters = None
self.filters = can_filters

# TX

Expand All @@ -118,7 +117,7 @@ def __init__(
self.channel_info = channel

# Super call must be after child init since super calls set_filters
super().__init__(channel=channel, **kwargs)
super().__init__(channel=channel, can_filters=can_filters, **kwargs)

def _recv_internal(self, timeout: float | None) -> tuple[can.Message | None, bool]:
ociMsgs = (ctypes.POINTER(OCI_CANMessageEx) * 1)()
Expand Down
1 change: 1 addition & 0 deletions doc/changelog.d/+etas-constructor-filters.fixed.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed ``EtasBus`` ignoring the ``can_filters`` passed to its constructor: the base class then added an accept-all filter that overrode them.
118 changes: 118 additions & 0 deletions test/test_etas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
"""
Tests for the ETAS BOA interface, with the BOA libraries replaced by stubs.
"""

import ctypes
import importlib
import sys
import unittest
from unittest.mock import Mock, patch

import can


class _FakeLibrary:
"""Every BOA function succeeds; the frame filter calls are recorded."""

def __init__(self, installed: list[tuple[int, int, int, int]]) -> None:
self._installed = installed

def __getattr__(self, name: str) -> Mock:
if name == "OCI_AddCANFrameFilterEx":
function = Mock(side_effect=self._add)
elif name == "OCI_RemoveCANFrameFilterEx":
function = Mock(side_effect=self._remove)
elif name == "OCI_GetTimerCapabilities":
function = Mock(side_effect=self._timer_capabilities)
else:
function = Mock(return_value=0)
setattr(self, name, function)
return function

@staticmethod
def _filters(filter_list, count: int) -> list[tuple[int, int, int, int]]:
return [
(
filter_list[i].contents.frameIDValue,
filter_list[i].contents.frameIDMask,
filter_list[i].contents.flagsValue,
filter_list[i].contents.flagsMask,
)
for i in range(count)
]

@staticmethod
def _timer_capabilities(_controller, capabilities) -> int:
capabilities._obj.tickFrequency = 1_000_000
return 0

def _add(self, _queue, filter_list, count: int) -> int:
self._installed.extend(self._filters(filter_list, count))
return 0

def _remove(self, _queue, filter_list, count: int) -> int:
for entry in self._filters(filter_list, count):
self._installed.remove(entry)
return 0


class EtasBusTest(unittest.TestCase):
def setUp(self) -> None:
self.installed: list[tuple[int, int, int, int]] = []
library = _FakeLibrary(self.installed)
windll = Mock()
windll.LoadLibrary.return_value = library

modules = patch.dict(sys.modules)
modules.start()
self.addCleanup(modules.stop)
for name in ("can.interfaces.etas", "can.interfaces.etas.boa"):
sys.modules.pop(name, None)
windll_patch = patch.object(ctypes, "windll", windll, create=True)
windll_patch.start()
self.addCleanup(windll_patch.stop)
self.etas = importlib.import_module("can.interfaces.etas")

def _bus(self, **kwargs) -> can.BusABC:
bus = can.Bus(
interface="etas",
channel="ETAS://USB/ES581.4:1/CAN:1",
ignore_config=True,
**kwargs,
)
self.assertIsInstance(bus, self.etas.EtasBus)
self.addCleanup(bus.shutdown)
return bus

def test_constructor_filters_are_installed(self):
selfreception = self.etas.OCI_CAN_MSG_FLAG_SELFRECEPTION
extended = self.etas.OCI_CAN_MSG_FLAG_EXTENDED
self._bus(
can_filters=[
{"can_id": 0x123, "can_mask": 0x7FF},
{"can_id": 0x18FF0000, "can_mask": 0x1FFF0000, "extended": True},
]
)
self.assertEqual(
self.installed,
[
(0x123, 0x7FF, 0, selfreception),
(0x18FF0000, 0x1FFF0000, extended, selfreception | extended),
],
)

def test_no_constructor_filters_accepts_all(self):
self._bus(receive_own_messages=True)
self.assertEqual(self.installed, [(0, 0, 0, 0)])

def test_set_filters_replaces_constructor_filters(self):
bus = self._bus(can_filters=[{"can_id": 0x123, "can_mask": 0x7FF}])
bus.set_filters([{"can_id": 0x456, "can_mask": 0x7FF}])
self.assertEqual(
self.installed,
[(0x456, 0x7FF, 0, self.etas.OCI_CAN_MSG_FLAG_SELFRECEPTION)],
)


if __name__ == "__main__":
unittest.main()
Loading