Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Serialize CAN frames when DLC is not set #681

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 3 additions & 2 deletions src/gallia/transports/can.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class CANMessage:
arbitration_id: int = 0

# TODO: Add a frametype attribute?
is_extended_id: bool = True
is_extended_id: bool = False
is_remote_frame: bool = False
is_error_frame: bool = False

Expand All @@ -55,6 +55,7 @@ class CANMessage:
bitrate_switch: bool = False
error_state_indicator: bool = False

# TODO: Is this semantically correct? Check this.
def __len__(self) -> int:
if self.dlc is None:
return len(self.data)
Expand All @@ -79,7 +80,7 @@ def pack(self) -> bytes:
flags |= CANFD_ESI
max_len = 64 if self.is_fd else 8
data = bytes(self.data).ljust(max_len, b"\x00")
return CAN_HEADER_FMT.pack(can_id, self.dlc, flags) + data
return CAN_HEADER_FMT.pack(can_id, len(self), flags) + data

@staticmethod
def _dissect_can_frame(frame: bytes) -> tuple[int, int, int, bytes]:
Expand Down
22 changes: 22 additions & 0 deletions tests/pytest/test_can_message.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# SPDX-FileCopyrightText: AISEC Pentesting Team
#
# SPDX-License-Identifier: Apache-2.0

import sys

assert sys.platform.startswith("linux"), "unsupported platform"

from gallia.transports.can import CANMessage


def test_can_message_deserialization() -> None:
test_table = [
(
b"\x00\x07\x00\x00\x04\x00\x00\x00s\x02>\x00\x00\x00\x00\x00",
CANMessage(arbitration_id=0x700, dlc=4, data=bytes([0x73, 0x02, 0x3E, 0x00])),
),
]

for input_, output in test_table:
frame = CANMessage.unpack(input_)
assert frame == output