Skip to content

Commit

Permalink
tweak for SP parser
Browse files Browse the repository at this point in the history
  • Loading branch information
robamu committed Sep 6, 2023
1 parent f0dbf3e commit 1e8a191
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 8 additions & 4 deletions spacepackets/ccsds/spacepacket.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
of all CCSDS packets."""
from __future__ import annotations

import logging
from abc import abstractmethod, ABC
import enum
import struct
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down
12 changes: 6 additions & 6 deletions tests/ccsds/test_sp_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down Expand Up @@ -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
)
Expand All @@ -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
)
Expand Down

0 comments on commit 1e8a191

Please sign in to comment.