Read a packet from the specified file. This function will raise EOFError when no more packets are available. :param size: Not used. Just here to follow the function signature for SuperSocket emulation. :return: A single packet read from the file or None
(self, size=CAN_MTU)
| 604 | __next__ = next |
| 605 | |
| 606 | def read_packet(self, size=CAN_MTU): |
| 607 | # type: (int) -> Optional[Packet] |
| 608 | """Read a packet from the specified file. |
| 609 | |
| 610 | This function will raise EOFError when no more packets are available. |
| 611 | |
| 612 | :param size: Not used. Just here to follow the function signature for |
| 613 | SuperSocket emulation. |
| 614 | :return: A single packet read from the file or None if filters apply |
| 615 | """ |
| 616 | line = self.f.readline() |
| 617 | line = line.lstrip() |
| 618 | if len(line) < 16: |
| 619 | raise EOFError |
| 620 | |
| 621 | is_log_file_format = line[0] == ord(b"(") |
| 622 | fd_flags = None |
| 623 | if is_log_file_format: |
| 624 | t_b, intf, f = line.split() |
| 625 | if b'##' in f: |
| 626 | idn, data = f.split(b'##') |
| 627 | fd_flags = data[0] |
| 628 | data = data[1:] |
| 629 | else: |
| 630 | idn, data = f.split(b'#') |
| 631 | le = None |
| 632 | t = float(t_b[1:-1]) # type: Optional[float] |
| 633 | else: |
| 634 | h, data = line.split(b']') |
| 635 | intf, idn, le = h.split() |
| 636 | t = None |
| 637 | |
| 638 | if self.ifilter is not None and \ |
| 639 | intf.decode('ASCII') not in self.ifilter: |
| 640 | return None |
| 641 | |
| 642 | data = data.replace(b' ', b'') |
| 643 | data = data.strip() |
| 644 | |
| 645 | if len(data) <= 8 and fd_flags is None: |
| 646 | pkt = CAN(identifier=int(idn, 16), data=hex_bytes(data)) |
| 647 | else: |
| 648 | pkt = CANFD(identifier=int(idn, 16), fd_flags=fd_flags, |
| 649 | data=hex_bytes(data)) |
| 650 | |
| 651 | if le is not None: |
| 652 | pkt.length = int(le[1:]) |
| 653 | else: |
| 654 | pkt.length = len(pkt.data) |
| 655 | |
| 656 | if len(idn) > 3: |
| 657 | pkt.flags = 0b100 |
| 658 | |
| 659 | if t is not None: |
| 660 | pkt.time = t |
| 661 | |
| 662 | return pkt |
| 663 |