benchtare 0.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- benchtare/__init__.py +31 -0
- benchtare/__main__.py +5 -0
- benchtare/_yaml.py +189 -0
- benchtare/backends/__init__.py +32 -0
- benchtare/backends/_can_codec.py +264 -0
- benchtare/backends/base.py +283 -0
- benchtare/backends/sim.py +244 -0
- benchtare/backends/sim_can.py +256 -0
- benchtare/bench/__init__.py +5 -0
- benchtare/bench/config.py +168 -0
- benchtare/bench/runtime.py +850 -0
- benchtare/cli.py +152 -0
- benchtare/dut/__init__.py +9 -0
- benchtare/dut/description.py +1059 -0
- benchtare/dut/identity.py +57 -0
- benchtare/errors.py +80 -0
- benchtare/harness/__init__.py +11 -0
- benchtare/harness/config.py +647 -0
- benchtare/hookspecs.py +27 -0
- benchtare/hygiene.py +247 -0
- benchtare/plugin.py +1683 -0
- benchtare/record.py +104 -0
- benchtare/reference/__init__.py +20 -0
- benchtare/reference/bench-sim.yaml +20 -0
- benchtare/reference/device.py +106 -0
- benchtare/reference/harness-can.yaml +21 -0
- benchtare/reference/harness-io.yaml +21 -0
- benchtare/reference/reference.dbc +27 -0
- benchtare/reference/reference.dut.yaml +19 -0
- benchtare/results/__init__.py +6 -0
- benchtare/results/manifest.py +172 -0
- benchtare/results/schema.py +488 -0
- benchtare/results/store.py +549 -0
- benchtare/scaffold/__init__.py +73 -0
- benchtare/scaffold/quickstart/README.md +34 -0
- benchtare/scaffold/quickstart/bench-sim.yaml +20 -0
- benchtare/scaffold/quickstart/conftest.py +27 -0
- benchtare/scaffold/quickstart/harness-can.yaml +21 -0
- benchtare/scaffold/quickstart/pytest.ini +5 -0
- benchtare/scaffold/quickstart/reference.dbc +27 -0
- benchtare/scaffold/quickstart/reference.dut.yaml +26 -0
- benchtare/scaffold/quickstart/test_reference.py +8 -0
- benchtare/selftest/__init__.py +10 -0
- benchtare/selftest/checks.py +264 -0
- benchtare/signals/__init__.py +5 -0
- benchtare/signals/model.py +182 -0
- benchtare/sim/__init__.py +1 -0
- benchtare/sim/model.py +158 -0
- benchtare/sim/registry.py +9 -0
- benchtare/sim/simulation.py +224 -0
- benchtare/testcheck.py +142 -0
- benchtare-0.0.1.dist-info/METADATA +172 -0
- benchtare-0.0.1.dist-info/RECORD +56 -0
- benchtare-0.0.1.dist-info/WHEEL +4 -0
- benchtare-0.0.1.dist-info/entry_points.txt +5 -0
- benchtare-0.0.1.dist-info/licenses/LICENSE +21 -0
benchtare/__init__.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"""BenchTare: a self-verifying HIL/SIL test framework for embedded systems.
|
|
2
|
+
|
|
3
|
+
What happened at the bench during a test goes into the test record (``benchtare.record``).
|
|
4
|
+
BenchTare's own log, for debugging BenchTare, uses the standard library's ``logging`` under the
|
|
5
|
+
``benchtare`` logger. It adds no handler of its own, not even a ``NullHandler``: with logging
|
|
6
|
+
left unconfigured, Python still prints BenchTare's warnings and errors (a bus that stopped
|
|
7
|
+
receiving, for example) to stderr.
|
|
8
|
+
|
|
9
|
+
PYTEST_DONT_REWRITE: BenchTare's own asserts are internal checks, so pytest needn't rewrite
|
|
10
|
+
them. The marker also stops a warning when ``benchtare selftest``, having imported this package,
|
|
11
|
+
starts pytest.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from benchtare.errors import (
|
|
15
|
+
BenchTareWarning,
|
|
16
|
+
DutIdentityWarning,
|
|
17
|
+
EquipmentWarning,
|
|
18
|
+
FrameMismatchWarning,
|
|
19
|
+
)
|
|
20
|
+
from benchtare.sim.model import DutModel
|
|
21
|
+
|
|
22
|
+
__version__ = "0.0.1"
|
|
23
|
+
|
|
24
|
+
__all__ = [
|
|
25
|
+
"BenchTareWarning",
|
|
26
|
+
"DutIdentityWarning",
|
|
27
|
+
"DutModel",
|
|
28
|
+
"EquipmentWarning",
|
|
29
|
+
"FrameMismatchWarning",
|
|
30
|
+
"__version__",
|
|
31
|
+
]
|
benchtare/__main__.py
ADDED
benchtare/_yaml.py
ADDED
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
"""Reading the YAML files a team writes, with errors that say where the problem is.
|
|
2
|
+
|
|
3
|
+
On top of plain YAML parsing, ``read_mapping``:
|
|
4
|
+
|
|
5
|
+
- rejects a key given twice in the same section, with both line numbers. Plain YAML silently
|
|
6
|
+
keeps the last one, so a copy-paste slip would rewire a signal without a word;
|
|
7
|
+
- reports a file that isn't UTF-8 text, with the line of the first bad byte;
|
|
8
|
+
- recognises the wrong kind of file (a harness file given as the bench file, a DBC file given
|
|
9
|
+
as the interface description) and says so.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
from pathlib import Path
|
|
15
|
+
from typing import Any
|
|
16
|
+
|
|
17
|
+
import yaml
|
|
18
|
+
|
|
19
|
+
from benchtare.errors import ConfigError, Problem
|
|
20
|
+
|
|
21
|
+
# The key each kind of file starts with, used to recognise a file given in the wrong place.
|
|
22
|
+
KIND_KEYS = {
|
|
23
|
+
"interface description": "dut",
|
|
24
|
+
"bench file": "bench",
|
|
25
|
+
"harness file": "harness",
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
YAML_VALUE_WORDS = "YAML reads unquoted on, off, yes, no, true, false and null as values"
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class _StrictLoader(yaml.SafeLoader):
|
|
32
|
+
"""A safe loader that records keys given twice in one mapping."""
|
|
33
|
+
|
|
34
|
+
def __init__(self, stream: str) -> None:
|
|
35
|
+
super().__init__(stream)
|
|
36
|
+
self.duplicates: list[tuple[Any, int, int]] = []
|
|
37
|
+
|
|
38
|
+
def construct_mapping(self, node: yaml.MappingNode, deep: bool = False) -> dict[Any, Any]:
|
|
39
|
+
seen: dict[Any, int] = {}
|
|
40
|
+
for key_node, _ in node.value:
|
|
41
|
+
if key_node.tag == "tag:yaml.org,2002:merge":
|
|
42
|
+
continue # '<<: *anchor' merges are meant to be overridden
|
|
43
|
+
key = self.construct_object(key_node, deep=True)
|
|
44
|
+
try:
|
|
45
|
+
hash(key)
|
|
46
|
+
except TypeError:
|
|
47
|
+
continue # the base class reports unhashable keys
|
|
48
|
+
line = key_node.start_mark.line + 1
|
|
49
|
+
if key in seen:
|
|
50
|
+
self.duplicates.append((key, seen[key], line))
|
|
51
|
+
else:
|
|
52
|
+
seen[key] = line
|
|
53
|
+
return super().construct_mapping(node, deep=deep)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def read_mapping(path: Path, *, kind: str) -> dict[Any, Any]:
|
|
57
|
+
"""Read a YAML file whose top level must be a mapping. Raise ``ConfigError`` otherwise."""
|
|
58
|
+
|
|
59
|
+
def fail(problem: Problem) -> ConfigError:
|
|
60
|
+
return ConfigError(path, [problem], kind=kind)
|
|
61
|
+
|
|
62
|
+
def not_a_dbc() -> ConfigError:
|
|
63
|
+
return fail(
|
|
64
|
+
Problem(
|
|
65
|
+
f"this is a DBC file, not {a(kind)}",
|
|
66
|
+
hint="DBC files are listed under 'can: dbc:' in the interface description; "
|
|
67
|
+
f"give the {kind} (.yaml) here",
|
|
68
|
+
)
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
if path.suffix.lower() == ".dbc":
|
|
72
|
+
raise not_a_dbc()
|
|
73
|
+
if path.is_dir():
|
|
74
|
+
raise fail(
|
|
75
|
+
Problem(
|
|
76
|
+
f"this is a folder, not {a(kind)}",
|
|
77
|
+
hint=f"give {a(kind)} here; the path resolved to {path.resolve()}",
|
|
78
|
+
)
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
try:
|
|
82
|
+
raw = path.read_bytes()
|
|
83
|
+
except FileNotFoundError:
|
|
84
|
+
raise fail(
|
|
85
|
+
Problem("file not found", hint=f"the path resolved to {path.resolve()}")
|
|
86
|
+
) from None
|
|
87
|
+
except OSError as exc:
|
|
88
|
+
raise fail(Problem(f"can't read the file: {exc.strerror or exc}")) from None
|
|
89
|
+
|
|
90
|
+
try:
|
|
91
|
+
text = raw.decode("utf-8")
|
|
92
|
+
except UnicodeDecodeError as exc:
|
|
93
|
+
line = raw[: exc.start].count(b"\n") + 1
|
|
94
|
+
raise fail(
|
|
95
|
+
Problem(
|
|
96
|
+
f"isn't UTF-8 text: byte 0x{raw[exc.start]:02x} can't be read",
|
|
97
|
+
where=f"line {line}",
|
|
98
|
+
hint="save the file as UTF-8; most editors offer this under 'Save with encoding'",
|
|
99
|
+
)
|
|
100
|
+
) from None
|
|
101
|
+
|
|
102
|
+
if text.lstrip(" \t\r\n").startswith(_DBC_STARTS):
|
|
103
|
+
raise not_a_dbc()
|
|
104
|
+
|
|
105
|
+
loader = _StrictLoader(text)
|
|
106
|
+
try:
|
|
107
|
+
data = loader.get_single_data()
|
|
108
|
+
except yaml.YAMLError as exc:
|
|
109
|
+
mark = getattr(exc, "problem_mark", None)
|
|
110
|
+
where = f"line {mark.line + 1}, column {mark.column + 1}" if mark else None
|
|
111
|
+
detail = getattr(exc, "problem", None) or str(exc)
|
|
112
|
+
raise fail(
|
|
113
|
+
Problem(
|
|
114
|
+
f"YAML syntax error: {detail}",
|
|
115
|
+
where=where,
|
|
116
|
+
hint="unclosed brackets or braces, indentation, and the ':' after each key",
|
|
117
|
+
)
|
|
118
|
+
) from None
|
|
119
|
+
finally:
|
|
120
|
+
loader.dispose()
|
|
121
|
+
|
|
122
|
+
if loader.duplicates:
|
|
123
|
+
raise ConfigError(
|
|
124
|
+
path,
|
|
125
|
+
[
|
|
126
|
+
Problem(
|
|
127
|
+
f"{_show_key(key)} is given twice in the same section, on line {first} "
|
|
128
|
+
f"and line {second}",
|
|
129
|
+
where=f"line {second}",
|
|
130
|
+
hint="remove one or rename it; YAML would silently keep only the last one",
|
|
131
|
+
)
|
|
132
|
+
for key, first, second in loader.duplicates
|
|
133
|
+
],
|
|
134
|
+
kind=kind,
|
|
135
|
+
)
|
|
136
|
+
|
|
137
|
+
if data is None:
|
|
138
|
+
raise fail(Problem("the file is empty"))
|
|
139
|
+
if not isinstance(data, dict):
|
|
140
|
+
raise fail(
|
|
141
|
+
Problem(f"the top level must be a set of 'key: value' entries, not {kind_of(data)}")
|
|
142
|
+
)
|
|
143
|
+
|
|
144
|
+
own_key = KIND_KEYS.get(kind)
|
|
145
|
+
if own_key is not None and own_key not in data:
|
|
146
|
+
for other_kind, other_key in KIND_KEYS.items():
|
|
147
|
+
if other_kind != kind and other_key in data:
|
|
148
|
+
raise fail(
|
|
149
|
+
Problem(
|
|
150
|
+
f"this looks like {a(other_kind)} (it has '{other_key}:'), not {a(kind)}",
|
|
151
|
+
hint=f"which file was given as the {kind}",
|
|
152
|
+
)
|
|
153
|
+
)
|
|
154
|
+
return data
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
# How a DBC file starts; used to recognise one given where a YAML file belongs.
|
|
158
|
+
_DBC_STARTS = ('VERSION "', "NS_ :", "BS_:", "BU_:", "BO_ ")
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def a(noun: str) -> str:
|
|
162
|
+
"""'a bench file', 'an interface description'."""
|
|
163
|
+
return f"{'an' if noun[:1].lower() in 'aeiou' else 'a'} {noun}"
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
def count(number: int, noun: str, plural: str | None = None) -> str:
|
|
167
|
+
"""'1 signal', '3 signals', '2 presence checks'."""
|
|
168
|
+
return f"{number} {noun if number == 1 else plural or noun + 's'}"
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
def kind_of(value: object) -> str:
|
|
172
|
+
"""Describe what YAML made of a value, for messages: 'a list', 'a number', ..."""
|
|
173
|
+
if value is None:
|
|
174
|
+
return "nothing (an empty value)"
|
|
175
|
+
if isinstance(value, bool):
|
|
176
|
+
return f"true/false ({str(value).lower()})"
|
|
177
|
+
if isinstance(value, (int, float)):
|
|
178
|
+
return f"a number ({value})"
|
|
179
|
+
if isinstance(value, str):
|
|
180
|
+
return "text"
|
|
181
|
+
if isinstance(value, list):
|
|
182
|
+
return "a list"
|
|
183
|
+
if isinstance(value, dict):
|
|
184
|
+
return "a set of 'key: value' entries"
|
|
185
|
+
return type(value).__name__
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
def _show_key(key: Any) -> str:
|
|
189
|
+
return f"'{key}'" if isinstance(key, str) else f"the key {key!r}"
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"""Backends: code that drives one kind of bench equipment.
|
|
2
|
+
|
|
3
|
+
The first slice ships two simulated backends. Hardware backends, and third-party backends as
|
|
4
|
+
plugins, come later behind the same protocol (``benchtare.backends.base.Backend``).
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
from benchtare.backends.base import (
|
|
10
|
+
Backend,
|
|
11
|
+
BackendType,
|
|
12
|
+
BenchError,
|
|
13
|
+
ComparisonFailed,
|
|
14
|
+
ReadTimeout,
|
|
15
|
+
WriteError,
|
|
16
|
+
)
|
|
17
|
+
from benchtare.backends.sim import SimIO
|
|
18
|
+
from benchtare.backends.sim_can import SimCan
|
|
19
|
+
|
|
20
|
+
BACKENDS: dict[str, BackendType] = {SimIO.name: SimIO, SimCan.name: SimCan}
|
|
21
|
+
|
|
22
|
+
__all__ = [
|
|
23
|
+
"BACKENDS",
|
|
24
|
+
"Backend",
|
|
25
|
+
"BackendType",
|
|
26
|
+
"BenchError",
|
|
27
|
+
"ComparisonFailed",
|
|
28
|
+
"ReadTimeout",
|
|
29
|
+
"SimCan",
|
|
30
|
+
"SimIO",
|
|
31
|
+
"WriteError",
|
|
32
|
+
]
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
"""CAN plumbing shared by the bench and simulated sides: encoding and decoding DUT signals in
|
|
2
|
+
frames, opening virtual buses, and receiving frames on a thread."""
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
import logging
|
|
7
|
+
import threading
|
|
8
|
+
import time
|
|
9
|
+
from collections.abc import Callable
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
from typing import Any
|
|
12
|
+
|
|
13
|
+
import can
|
|
14
|
+
import cantools
|
|
15
|
+
|
|
16
|
+
from benchtare.dut.description import CanSignalRef
|
|
17
|
+
from benchtare.signals.model import SignalType
|
|
18
|
+
|
|
19
|
+
log = logging.getLogger(__name__)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class CarryError(ValueError):
|
|
23
|
+
"""A value can't be carried by its signal's bits in the frame."""
|
|
24
|
+
|
|
25
|
+
def __init__(self, reason: str, limits: str | None = None) -> None:
|
|
26
|
+
self.reason = reason
|
|
27
|
+
self.limits = limits
|
|
28
|
+
super().__init__(reason)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class FrameError(ValueError):
|
|
32
|
+
"""A received frame doesn't match its message in the DBC, so it can't be decoded."""
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class CanCodec:
|
|
36
|
+
"""Loads DBC files once, and keeps the latest value of every signal in each sent message.
|
|
37
|
+
|
|
38
|
+
A frame always carries every signal in its message, so writing one signal resends the
|
|
39
|
+
others at their latest values (their DBC initial value until first written).
|
|
40
|
+
"""
|
|
41
|
+
|
|
42
|
+
def __init__(self) -> None:
|
|
43
|
+
self._databases: dict[Path, Any] = {}
|
|
44
|
+
self._state: dict[tuple[Path, str], dict[str, Any]] = {}
|
|
45
|
+
|
|
46
|
+
def message(self, ref: CanSignalRef) -> Any:
|
|
47
|
+
database = self._databases.get(ref.dbc)
|
|
48
|
+
if database is None:
|
|
49
|
+
database = cantools.database.load_file(str(ref.dbc), database_format="dbc")
|
|
50
|
+
self._databases[ref.dbc] = database
|
|
51
|
+
# By name: a standard and an extended frame may share an ID number.
|
|
52
|
+
return database.get_message_by_name(ref.message)
|
|
53
|
+
|
|
54
|
+
def encode(self, ref: CanSignalRef, value: Any) -> tuple[Any, bytes]:
|
|
55
|
+
"""Set one signal and return (message, frame data). Raise ``CarryError``."""
|
|
56
|
+
message = self.message(ref)
|
|
57
|
+
state = self._state.setdefault((ref.dbc, ref.message), _initial_values(message))
|
|
58
|
+
dbc_signal = message.get_signal_by_name(ref.signal)
|
|
59
|
+
if isinstance(value, str) and dbc_signal.choices:
|
|
60
|
+
codes = [code for code, name in dbc_signal.choices.items() if str(name) == value]
|
|
61
|
+
if not codes:
|
|
62
|
+
names = ", ".join(dict.fromkeys(str(v) for v in dbc_signal.choices.values()))
|
|
63
|
+
raise CarryError(
|
|
64
|
+
f"{value!r} is not one of its values ({names})",
|
|
65
|
+
"a value name, or a raw number to send an undefined code",
|
|
66
|
+
)
|
|
67
|
+
if len(codes) > 1:
|
|
68
|
+
raise CarryError(
|
|
69
|
+
f"{value!r} is the name of codes {', '.join(str(c) for c in codes)}",
|
|
70
|
+
f"write the raw code you mean, for example bench.write(signal, {codes[0]})",
|
|
71
|
+
)
|
|
72
|
+
candidate = dict(state)
|
|
73
|
+
candidate[ref.signal] = _to_dbc(value)
|
|
74
|
+
try:
|
|
75
|
+
data = message.encode(candidate, strict=False)
|
|
76
|
+
except Exception as exc: # noqa: BLE001 - cantools raises many kinds; all mean "can't encode"
|
|
77
|
+
raise CarryError(str(exc) or type(exc).__name__, _limits(dbc_signal)) from None
|
|
78
|
+
state[ref.signal] = candidate[ref.signal]
|
|
79
|
+
return message, data
|
|
80
|
+
|
|
81
|
+
def decode(self, message: Any, data: bytes) -> dict[str, Any]:
|
|
82
|
+
"""Decode a received frame's data. Raise ``FrameError`` if it doesn't match the DBC."""
|
|
83
|
+
try:
|
|
84
|
+
return message.decode(data, decode_choices=True, allow_truncated=True)
|
|
85
|
+
except Exception as exc:
|
|
86
|
+
# cantools raises many kinds; all mean "this frame isn't what the DBC says".
|
|
87
|
+
raise FrameError(f"message {message.name}: {exc}") from exc
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def open_virtual_bus(name: str, *, fd: bool) -> can.BusABC:
|
|
91
|
+
"""A python-can virtual bus that ignores python-can's own configuration.
|
|
92
|
+
|
|
93
|
+
Without ``ignore_config``, a ``~/.canrc``, a ``can.ini`` or the ``CAN_*`` environment
|
|
94
|
+
variables on the machine would quietly change how the simulated bus behaves.
|
|
95
|
+
"""
|
|
96
|
+
return can.Bus(interface="virtual", channel=name, fd=fd, ignore_config=True)
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def frame(ref: CanSignalRef, data: bytes) -> can.Message:
|
|
100
|
+
"""The CAN frame that carries a message, with its ID type and FD flag from the DBC."""
|
|
101
|
+
return can.Message(
|
|
102
|
+
arbitration_id=ref.frame_id, data=data, is_extended_id=ref.extended, is_fd=ref.fd
|
|
103
|
+
)
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def frame_key(received: can.Message) -> tuple[int, bool]:
|
|
107
|
+
return (received.arbitration_id, bool(received.is_extended_id))
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def describe_frame(received: can.Message) -> str:
|
|
111
|
+
"""A frame as an engineer would read it off a bus trace: ``0x101 [8] 07 01 00 ...``."""
|
|
112
|
+
width = 8 if received.is_extended_id else 3
|
|
113
|
+
kind = " (extended)" if received.is_extended_id else ""
|
|
114
|
+
data = bytes(received.data).hex(" ")
|
|
115
|
+
return f"0x{received.arbitration_id:0{width}x}{kind} [{len(received.data)}] {data}".rstrip()
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def frame_facts(received: can.Message) -> dict[str, Any]:
|
|
119
|
+
"""A frame as plain values, for the test record."""
|
|
120
|
+
return {
|
|
121
|
+
"frame_id": received.arbitration_id,
|
|
122
|
+
"extended": bool(received.is_extended_id),
|
|
123
|
+
"data": bytes(received.data).hex(" "),
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
class FrameReceiver:
|
|
128
|
+
"""Receives frames from a bus on a thread of its own and hands each one to ``handle``.
|
|
129
|
+
|
|
130
|
+
python-can's Notifier stops receiving for good if a handler raises, which would leave every
|
|
131
|
+
later read timing out with nothing said. Here:
|
|
132
|
+
|
|
133
|
+
- A frame that doesn't match the DBC (``FrameError``) is logged at ``bad_frame_level`` and
|
|
134
|
+
skipped. On the bench side the handler has already put it in the test record, so the
|
|
135
|
+
log needs it only for debugging.
|
|
136
|
+
- Any other error while handling a frame is logged with its traceback, and skipped.
|
|
137
|
+
- If the bus itself fails, reception stops, the failure is logged, ``failure`` says what
|
|
138
|
+
happened (so the backend's ``check()`` can report it), and ``on_failure`` is called.
|
|
139
|
+
|
|
140
|
+
In BenchTare's log, each kind of bad frame appears at most once per ``quiet_period``
|
|
141
|
+
seconds, with a count of the ones in between, so a DUT sending a bad frame every 10 ms
|
|
142
|
+
doesn't flood it. The test record keeps every one.
|
|
143
|
+
"""
|
|
144
|
+
|
|
145
|
+
quiet_period = 1.0
|
|
146
|
+
|
|
147
|
+
def __init__(
|
|
148
|
+
self,
|
|
149
|
+
bus: can.BusABC,
|
|
150
|
+
handle: Callable[[can.Message], None],
|
|
151
|
+
*,
|
|
152
|
+
who: str,
|
|
153
|
+
bad_frame_level: int = logging.WARNING,
|
|
154
|
+
on_failure: Callable[[str], None] | None = None,
|
|
155
|
+
poll: float = 0.05,
|
|
156
|
+
) -> None:
|
|
157
|
+
self.who = who
|
|
158
|
+
self.failure: str | None = None
|
|
159
|
+
self._bad_frame_level = bad_frame_level
|
|
160
|
+
self._on_failure = on_failure
|
|
161
|
+
self._bus = bus
|
|
162
|
+
self._handle = handle
|
|
163
|
+
self._poll = poll
|
|
164
|
+
self._stop = threading.Event()
|
|
165
|
+
self._last_logged: dict[tuple[int, bool], float] = {}
|
|
166
|
+
self._unlogged: dict[tuple[int, bool], int] = {}
|
|
167
|
+
self._thread = threading.Thread(target=self._run, name=f"benchtare-rx-{who}", daemon=True)
|
|
168
|
+
|
|
169
|
+
def start(self) -> None:
|
|
170
|
+
self._thread.start()
|
|
171
|
+
|
|
172
|
+
def stop(self, timeout: float = 1.0) -> None:
|
|
173
|
+
self._stop.set()
|
|
174
|
+
if self._thread.is_alive():
|
|
175
|
+
self._thread.join(timeout)
|
|
176
|
+
|
|
177
|
+
def _run(self) -> None:
|
|
178
|
+
while not self._stop.is_set():
|
|
179
|
+
try:
|
|
180
|
+
received = self._bus.recv(self._poll)
|
|
181
|
+
except Exception as exc: # noqa: BLE001 - any bus error ends reception
|
|
182
|
+
if not self._stop.is_set(): # an error while closing isn't a failure
|
|
183
|
+
self.failure = f"stopped receiving: {type(exc).__name__}: {exc}"
|
|
184
|
+
log.error("%s %s", self.who, self.failure)
|
|
185
|
+
if self._on_failure is not None:
|
|
186
|
+
self._on_failure(self.failure)
|
|
187
|
+
return
|
|
188
|
+
if received is None:
|
|
189
|
+
continue
|
|
190
|
+
try:
|
|
191
|
+
self._handle(received)
|
|
192
|
+
except FrameError as exc:
|
|
193
|
+
self._log_bad_frame(
|
|
194
|
+
received,
|
|
195
|
+
self._bad_frame_level,
|
|
196
|
+
f"{self.who}: ignored a frame that doesn't match the DBC: "
|
|
197
|
+
f"{describe_frame(received)}: {exc}\n"
|
|
198
|
+
" Check: the DBC files describe what the DUT sends",
|
|
199
|
+
)
|
|
200
|
+
except Exception as exc: # noqa: BLE001 - one bad frame mustn't stop reception
|
|
201
|
+
self._log_bad_frame(
|
|
202
|
+
received,
|
|
203
|
+
logging.ERROR,
|
|
204
|
+
f"{self.who}: failed to handle a frame: {describe_frame(received)}: "
|
|
205
|
+
f"{type(exc).__name__}: {exc}",
|
|
206
|
+
exc_info=True,
|
|
207
|
+
)
|
|
208
|
+
|
|
209
|
+
def _log_bad_frame(
|
|
210
|
+
self, received: can.Message, level: int, message: str, *, exc_info: bool = False
|
|
211
|
+
) -> None:
|
|
212
|
+
key = frame_key(received)
|
|
213
|
+
now = time.monotonic()
|
|
214
|
+
last = self._last_logged.get(key)
|
|
215
|
+
if last is not None and now - last < self.quiet_period:
|
|
216
|
+
self._unlogged[key] = self._unlogged.get(key, 0) + 1
|
|
217
|
+
return
|
|
218
|
+
self._last_logged[key] = now
|
|
219
|
+
unlogged = self._unlogged.pop(key, 0)
|
|
220
|
+
if unlogged:
|
|
221
|
+
message += f"\n ({unlogged} more like it since the last report)"
|
|
222
|
+
log.log(level, "%s", message, exc_info=exc_info)
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
def to_python(signal_type: SignalType | None, value: Any) -> Any:
|
|
226
|
+
"""Convert a decoded DBC value to the Python value tests see."""
|
|
227
|
+
if signal_type is SignalType.ENUM:
|
|
228
|
+
return value if isinstance(value, (int, float)) else str(value) # raw for unknown codes
|
|
229
|
+
if signal_type is SignalType.BOOL:
|
|
230
|
+
return bool(value)
|
|
231
|
+
if signal_type is SignalType.INT:
|
|
232
|
+
return int(value)
|
|
233
|
+
if signal_type is SignalType.FLOAT:
|
|
234
|
+
return float(value)
|
|
235
|
+
return value
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
def _to_dbc(value: Any) -> Any:
|
|
239
|
+
if isinstance(value, bool):
|
|
240
|
+
return int(value)
|
|
241
|
+
return value
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
def _initial_values(message: Any) -> dict[str, Any]:
|
|
245
|
+
values: dict[str, Any] = {}
|
|
246
|
+
for dbc_signal in message.signals:
|
|
247
|
+
if dbc_signal.initial is not None:
|
|
248
|
+
values[dbc_signal.name] = dbc_signal.initial
|
|
249
|
+
else:
|
|
250
|
+
values[dbc_signal.name] = dbc_signal.offset # raw 0
|
|
251
|
+
return values
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
def _limits(dbc_signal: Any) -> str:
|
|
255
|
+
bits = dbc_signal.length
|
|
256
|
+
if dbc_signal.is_float:
|
|
257
|
+
return f"it is a {bits}-bit float"
|
|
258
|
+
if dbc_signal.is_signed:
|
|
259
|
+
low_raw, high_raw = -(2 ** (bits - 1)), 2 ** (bits - 1) - 1
|
|
260
|
+
else:
|
|
261
|
+
low_raw, high_raw = 0, 2**bits - 1
|
|
262
|
+
scale, offset = dbc_signal.scale, dbc_signal.offset
|
|
263
|
+
low, high = sorted((low_raw * scale + offset, high_raw * scale + offset))
|
|
264
|
+
return f"its {bits} bits (scale {scale:g}, offset {offset:g}) carry {low:g} to {high:g}"
|