Skip to content

Commit

Permalink
Support candump formatting for flags (#317)
Browse files Browse the repository at this point in the history
Fixes #316

---------

Co-authored-by: Pavel Kirienko <[email protected]>
  • Loading branch information
emrainey and pavel-kirienko authored Dec 29, 2023
1 parent 4846761 commit a6a899c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
7 changes: 7 additions & 0 deletions .readthedocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

version: 2

build:
os: ubuntu-22.04
tools:
python: "3.10"
apt_packages:
- graphviz

sphinx:
configuration: docs/conf.py
fail_on_warning: true
Expand Down
2 changes: 1 addition & 1 deletion pycyphal/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.16.0"
__version__ = "1.16.1"
33 changes: 31 additions & 2 deletions pycyphal/transport/can/media/candump/_candump.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ def list_available_interface_names(*, recurse: bool = False) -> Iterable[str]:


_RE_REC_REMOTE = re.compile(r"(?a)^\s*\((\d+\.\d+)\)\s+([\w-]+)\s+([\da-fA-F]+)#R")
_RE_REC_DATA = re.compile(r"(?a)^\s*\((\d+\.\d+)\)\s+([\w-]+)\s+([\da-fA-F]+)#([\da-fA-F]*)")
_RE_REC_DATA = re.compile(r"(?a)^\s*\((\d+\.\d+)\)\s+([\w-]+)\s+([\da-fA-F]+)#(#\d)?([\da-fA-F]*)")


@dataclasses.dataclass(frozen=True)
Expand All @@ -253,7 +253,11 @@ def parse(line: str) -> None | Record:
match = _RE_REC_DATA.match(line)
if not match:
return None
s_ts, iface_name, s_canid, s_data = match.groups()
s_ts, iface_name, s_canid, s_flags, s_data = match.groups()
if s_flags is None:
s_flags = "#0"
if s_data is None:
s_data = ""
return DataFrameRecord(
ts=Timestamp(
system_ns=int(Decimal(s_ts) * Decimal("1e9")),
Expand All @@ -263,6 +267,7 @@ def parse(line: str) -> None | Record:
fmt=FrameFormat.EXTENDED if len(s_canid) > 3 else FrameFormat.BASE,
can_id=int(s_canid, 16),
can_payload=bytes.fromhex(s_data),
can_flags=int(s_flags[1:], 16), # skip over #
)
except ValueError as ex:
_logger.debug("Cannot convert values from line %r: %r", line, ex)
Expand All @@ -281,6 +286,7 @@ class DataFrameRecord(Record):
fmt: FrameFormat
can_id: int
can_payload: bytes
can_flags: int

def __str__(self) -> str:
if self.fmt == FrameFormat.EXTENDED:
Expand Down Expand Up @@ -311,6 +317,29 @@ def _unittest_record_parse() -> None:
assert rec.can_payload == bytes()
print(rec)

rec = Record.parse("(1703173569.357659) can0 0C7D5522##556000000000000EB\n")
assert isinstance(rec, DataFrameRecord)
assert rec.ts.system_ns == 1703173569_357659000
assert rec.iface_name == "can0"
assert rec.fmt == FrameFormat.EXTENDED
assert rec.can_id == 0x0C7D5522
assert rec.can_flags == 5
assert rec.can_payload == bytes.fromhex("56000000000000EB")
print(rec)

rec = Record.parse("(1703173569.357659) can0 0C7D5522##3\n")
assert isinstance(rec, DataFrameRecord)
assert rec.ts.system_ns == 1703173569_357659000
assert rec.iface_name == "can0"
assert rec.fmt == FrameFormat.EXTENDED
assert rec.can_id == 0x0C7D5522
assert rec.can_flags == 3
assert rec.can_payload == bytes()
print(rec)

rec = Record.parse("(1703173569.357659) can0 0C7D5522##3210\n")
assert rec is None

rec = Record.parse("(1657805304.099792) slcan0 123#R\n")
assert isinstance(rec, UnsupportedRecord)

Expand Down

0 comments on commit a6a899c

Please sign in to comment.