From 1e8a19171f4e63227ce89a395300947788746945 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Wed, 6 Sep 2023 21:25:15 +0200 Subject: [PATCH] tweak for SP parser --- CHANGELOG.md | 5 +++++ spacepackets/ccsds/spacepacket.py | 12 ++++++++---- tests/ccsds/test_sp_parser.py | 12 ++++++------ 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 385c175..e856fb4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/). # [unreleased] +## Changed + +- The `parse_space_packets` function analysis queue argument is now expected to be filled + on the right side. + # [v0.18.0rc1] 2023-09-04 ## Added diff --git a/spacepackets/ccsds/spacepacket.py b/spacepackets/ccsds/spacepacket.py index 7800390..bd416ca 100644 --- a/spacepackets/ccsds/spacepacket.py +++ b/spacepackets/ccsds/spacepacket.py @@ -2,6 +2,7 @@ of all CCSDS packets.""" from __future__ import annotations +import logging from abc import abstractmethod, ABC import enum import struct @@ -418,9 +419,10 @@ def get_total_space_packet_len_from_len_field(len_field: int): def parse_space_packets( analysis_queue: Deque[bytearray], packet_ids: Sequence[PacketId] ) -> List[bytearray]: - """Given a deque of bytearrays, parse for space packets. Any broken headers will be removed. - If a split packet with a valid header is detected, broken tail packets will be reinserted - in the given deque. + """Given a deque of bytearrays, parse for space packets. This funtion expects the deque + to be filled on the right side, for example with :py:meth:`collections.deque.append`. + If a split packet with a valid header is detected, this function will re-insert the header into + the given deque on the right side. :param analysis_queue: :param packet_ids: @@ -433,7 +435,7 @@ def parse_space_packets( return tm_list while analysis_queue: # Put it all in one buffer - concatenated_packets.extend(analysis_queue.pop()) + concatenated_packets.extend(analysis_queue.popleft()) current_idx = 0 if len(concatenated_packets) < 6: return tm_list @@ -472,6 +474,8 @@ def __handle_packet_id_match( ) # Might be part of packet. Put back into analysis queue as whole if current_idx + total_packet_len > len(concatenated_packets): + # Clear the queue first. We are done with parsing + analysis_queue.clear() analysis_queue.append(concatenated_packets[current_idx:]) return -1, current_idx else: diff --git a/tests/ccsds/test_sp_parser.py b/tests/ccsds/test_sp_parser.py index b9d6e00..8c83471 100644 --- a/tests/ccsds/test_sp_parser.py +++ b/tests/ccsds/test_sp_parser.py @@ -33,9 +33,9 @@ def test_sp_parser_crap_data_is_skipped(self): time_provider=CdsShortTimestamp.empty(), ) other_larger_packet_raw = other_larger_packet.pack() - self.packet_deque.appendleft(self.tm_packet_raw) - self.packet_deque.appendleft(bytearray(8)) - self.packet_deque.appendleft(other_larger_packet_raw) + self.packet_deque.append(self.tm_packet_raw) + self.packet_deque.append(bytearray(8)) + self.packet_deque.append(other_larger_packet_raw) sp_list = parse_space_packets( analysis_queue=self.packet_deque, packet_ids=self.packet_ids ) @@ -64,7 +64,7 @@ def test_broken_packet(self): ) self.assertEqual(len(sp_list), 0) self.assertEqual(len(self.packet_deque), 1) - self.packet_deque.appendleft(tm_packet_second_half) + self.packet_deque.append(tm_packet_second_half) sp_list = parse_space_packets( analysis_queue=self.packet_deque, packet_ids=self.packet_ids ) @@ -73,10 +73,10 @@ def test_broken_packet(self): self.assertEqual(sp_list[0], self.tm_packet_raw) def test_broken_packet_at_end(self): - self.packet_deque.appendleft(self.tm_packet_raw) + self.packet_deque.append(self.tm_packet_raw) # slice TM packet in half tm_packet_first_half = self.tm_packet_raw[:10] - self.packet_deque.appendleft(tm_packet_first_half) + self.packet_deque.append(tm_packet_first_half) sp_list = parse_space_packets( analysis_queue=self.packet_deque, packet_ids=self.packet_ids )