Skip to content

Commit

Permalink
Allow FixedValueEntry to be specified as hex
Browse files Browse the repository at this point in the history
  • Loading branch information
fqqb committed Apr 15, 2024
1 parent d6cfdd1 commit 424fbc1
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 13 deletions.
10 changes: 5 additions & 5 deletions src/yamcs/pymdb/ccsds.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,30 +165,30 @@ def add_ccsds_header(system: System) -> CcsdsHeader:
entries=[
FixedValueEntry(
name="ccsds_version",
binary=bytes.fromhex("00"),
binary="00",
bits=3,
),
FixedValueEntry(
name="ccsds_type",
binary=bytes.fromhex("01"),
binary="01",
bits=1,
),
ArgumentEntry(tc_secondary_header),
ArgumentEntry(tc_apid),
FixedValueEntry(
name="ccsds_group_flags",
binary=bytes.fromhex("03"), # Always standalone
binary="03", # Always standalone
bits=2,
),
FixedValueEntry(
name="ccsds_source_sequence_count",
binary=bytes.fromhex("0000"),
binary="0000",
bits=14,
short_description="Value set by Yamcs during link post-processing",
),
FixedValueEntry(
name="ccsds_packet_length",
binary=bytes.fromhex("0000"),
binary="0000",
bits=16,
short_description="Value set by Yamcs during link post-processing",
),
Expand Down
31 changes: 28 additions & 3 deletions src/yamcs/pymdb/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ def __init__(
class FixedValueEntry:
def __init__(
self,
binary: bytes,
binary: bytes | str,
name: str | None = None,
*,
short_description: str | None = None,
Expand All @@ -421,17 +421,42 @@ def __init__(
condition: Expression | None = None,
bits: int | None = None,
) -> None:
self.binary: bytes = binary

self.binary: bytes
"""
The fixed value to be encoded.
The value may be provided in any of these ways:
* Bytes: :python:`b\xDE\xAD\xBE\xEF`
* Hex string: :python:`"DEADBEEF"`
"""
if isinstance(binary, str):
self.binary = bytes.fromhex(binary)
else:
self.binary = binary

self.name: str | None = name
"""Optional name"""

self.short_description: str | None = short_description
"""Onleine description"""
"""Oneline description"""

self.absolute: bool = absolute
self.location_in_bits: int = location_in_bits
self.condition: Expression | None = condition

self.bits: int | None = bits
"""
If unspecified, its value is derived from the provided
:attr:`binary` value.
If less than the bit size of the :attr:`binary` attribute,
the value is left-padded with zeros.
If larger than the bit size of the :attr:`binary` attribute,
the most-significant bits are dropped.
"""


CommandEntry = ArgumentEntry | ParameterEntry | FixedValueEntry
Expand Down
4 changes: 2 additions & 2 deletions src/yamcs/pymdb/csp.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,11 +260,11 @@ def add_csp_header(
ArgumentEntry(tc_dport),
FixedValueEntry(
name=f"{prefix}sport",
binary=b"\x20", # 48
binary="20", # 48
bits=6,
short_description="Ephemeral port for outgoing connection",
),
FixedValueEntry(name=f"{prefix}reserved", binary=b"\x00", bits=4),
FixedValueEntry(name=f"{prefix}reserved", binary="00", bits=4),
ArgumentEntry(tc_hmac),
ArgumentEntry(tc_xtea),
ArgumentEntry(tc_rdp),
Expand Down
19 changes: 16 additions & 3 deletions src/yamcs/pymdb/xtce.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,12 +437,25 @@ def add_fixed_value_entry(
entry: FixedValueEntry,
):
el = ET.SubElement(parent, "FixedValueEntry")
el.attrib["binaryValue"] = hexlify(entry.binary).decode("ascii")

bitlen: int
if entry.bits is None:
el.attrib["sizeInBits"] = str(8 * len(entry.binary))
bitlen = 8 * len(entry.binary)
else:
el.attrib["sizeInBits"] = str(entry.bits)
bitlen = entry.bits

hex = hexlify(entry.binary).decode("ascii")

# XTCE requires hex to be at least as large as the bit size
bytelen: int
if bitlen % 8:
bytelen = (bitlen + (8 - bitlen % 8)) // 8
else:
bytelen = bitlen // 8
hex = hex.zfill(bytelen * 2)

el.attrib["binaryValue"] = hex
el.attrib["sizeInBits"] = str(bitlen)

if entry.name:
el.attrib["name"] = entry.name
Expand Down

0 comments on commit 424fbc1

Please sign in to comment.