Skip to content

Commit

Permalink
Fixed CcsdsSpacePacket to be able to serialize data field.
Browse files Browse the repository at this point in the history
  • Loading branch information
pxntus committed Feb 16, 2022
1 parent e744c4b commit e602cfb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/puslib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
if sys.version_info < _MIN_PYTHON:
sys.exit(f"Python {_MIN_PYTHON[0]}.{_MIN_PYTHON[1]} or later is required.\n", 1)

__version__ = "0.2.0"
__version__ = "0.2.1"


class PusPolicy:
Expand Down
17 changes: 12 additions & 5 deletions src/puslib/packet.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from dataclasses import dataclass
from typing import Optional

from .exceptions import CrcException, IncompletePacketException, InvalidPacketException, TooSmallBufferException
from .exceptions import CrcException, IncompletePacketException, InvalidPacketException
from .time import CucTime
from .crc_ccitt import calculate as crc_ccitt_calculate

Expand All @@ -13,6 +13,8 @@
TM_PACKET_PUS_VERSION_NUMBER = 2
TC_PACKET_PUS_VERSION_NUMBER = 2

IDLE_APID = 0b11111111111

_COMMON_SEC_HDR_STRUCT = struct.Struct('>BBB')
_PEC_FIELD_SIZE = 2

Expand Down Expand Up @@ -94,7 +96,12 @@ def has_pec(self):
def serialize(self):
packet_id = self.header.packet_version_number << 13 | (1 if self.header.packet_type == PacketType.TC else 0) << 12 | (1 if self.header.secondary_header_flag else 0) << 11 | self.header.apid
seq_ctrl = self.header.seq_flags << 14 | self.header.seq_count_or_name
return self._CCSDS_HDR_STRUCT.pack(packet_id, seq_ctrl, self.header.data_length)
ccsds_header = self._CCSDS_HDR_STRUCT.pack(packet_id, seq_ctrl, self.header.data_length)
if self.header.secondary_header_flag:
packet_data_field = b'' # Leave it to subclasses to handle data field
else:
packet_data_field = self.payload
return ccsds_header + packet_data_field

def request_id(self):
packet_id = self.header.packet_version_number << 13 | (1 if self.header.packet_type == PacketType.TC else 0) << 12 | (1 if self.header.secondary_header_flag else 0) << 11 | self.header.apid
Expand Down Expand Up @@ -131,7 +138,7 @@ def create(cls, **kwargs):
_validate_int_field('Packet version number', packet_version_number, 0, 0)
packet.header.packet_version_number = packet_version_number

packet_type = kwargs.get('packet_type', None)
packet_type = kwargs.get('packet_type', PacketType.TM)
if not isinstance(packet_type, PacketType):
raise TypeError("Packet type must be a PacketType")
packet.header.packet_type = packet_type
Expand All @@ -140,14 +147,14 @@ def create(cls, **kwargs):
_validate_bool_field('Secondary header flag', secondary_header_flag)
packet.header.secondary_header_flag = secondary_header_flag

apid = kwargs.get('apid', None)
apid = kwargs.get('apid', IDLE_APID)
_validate_int_field('apid', apid, 0, 0x7ff)
packet.header.apid = apid

seq_flags = kwargs.get('seq_flags', SequenceFlag.UNSEGMENTED)
packet.header.seq_flags = seq_flags

seq_count = kwargs.get('seq_count_or_name', None)
seq_count = kwargs.get('seq_count_or_name', 0)
_validate_int_field('Sequence count or name', seq_count, 0, 0x3fff)
packet.header.seq_count_or_name = seq_count

Expand Down

0 comments on commit e602cfb

Please sign in to comment.