|
8 | 8 |
|
9 | 9 | import logging
|
10 | 10 |
|
11 |
| -from typing import Any, BinaryIO, Dict, Union |
| 11 | +from typing import Any, BinaryIO, Dict, Generator, Union |
12 | 12 |
|
13 | 13 | from ..message import Message
|
14 | 14 | from ..typechecking import StringPathLike, Channel
|
15 |
| -from .generic import BinaryIOMessageWriter |
16 |
| -from ..socketcan_common import build_can_frame, CAN_FRAME_HEADER_STRUCT_BE |
| 15 | +from .generic import BinaryIOMessageWriter, BinaryIOMessageReader |
| 16 | +from ..socketcan_common import build_can_frame, parse_can_frame, CAN_FRAME_HEADER_STRUCT_BE |
17 | 17 |
|
18 | 18 | logger = logging.getLogger("can.io.pcapng")
|
19 | 19 |
|
@@ -106,3 +106,44 @@ def on_message_received(self, msg: Message) -> None:
|
106 | 106 | timestamp_low=timestamp_units & 0xFFFFFFFF,
|
107 | 107 | endianness=">" # big
|
108 | 108 | ))
|
| 109 | + |
| 110 | +class PcapngReader(BinaryIOMessageReader): |
| 111 | + """ |
| 112 | + Iterator of CAN messages from a Pcapng File. |
| 113 | + """ |
| 114 | + |
| 115 | + file: BinaryIO |
| 116 | + |
| 117 | + def __init__( |
| 118 | + self, |
| 119 | + file: Union[StringPathLike, BinaryIO], |
| 120 | + **kwargs: Any, |
| 121 | + ) -> None: |
| 122 | + """ |
| 123 | + :param file: a path-like object or as file-like object to read from |
| 124 | + If this is a file-like object, is has to opened in binary |
| 125 | + read mode, not text read mode. |
| 126 | + """ |
| 127 | + super().__init__(file, mode="rb") |
| 128 | + self._scanner = pcapng.FileScanner(self.file) |
| 129 | + |
| 130 | + def __iter__(self) -> Generator[Message, None, None]: |
| 131 | + for block in self._scanner: |
| 132 | + if isinstance(block, blocks.EnhancedPacket): |
| 133 | + idn: blocks.InterfaceDescription = block.interface |
| 134 | + # We only care about the CAN packets |
| 135 | + if idn.link_type != LINKTYPE_CAN_SOCKETCAN: |
| 136 | + logger.debug("Skipping non-CAN packet, link type: %s", idn.link_type) |
| 137 | + continue |
| 138 | + |
| 139 | + msg = parse_can_frame(block.packet_data, struct=CAN_FRAME_HEADER_STRUCT_BE) |
| 140 | + |
| 141 | + timestamp64 = (block.timestamp_high << 32) + block.timestamp_low |
| 142 | + msg.timestamp = timestamp64 * idn.timestamp_resolution |
| 143 | + |
| 144 | + if 'if_name' in idn.options: |
| 145 | + msg.channel = idn.options['if_name'] |
| 146 | + else: |
| 147 | + msg.channel = block.interface_id |
| 148 | + |
| 149 | + yield msg |
0 commit comments