diff options
-rw-r--r-- | host/python/uhd/__init__.py | 1 | ||||
-rw-r--r-- | host/python/uhd/chdr.py | 60 | ||||
-rw-r--r-- | host/tests/pychdr_parse_test.py | 2 |
3 files changed, 62 insertions, 1 deletions
diff --git a/host/python/uhd/__init__.py b/host/python/uhd/__init__.py index 8b6a2b36c..99c10c3b1 100644 --- a/host/python/uhd/__init__.py +++ b/host/python/uhd/__init__.py @@ -12,4 +12,5 @@ from . import usrp from . import filters from . import rfnoc from . import dsp +from . import chdr from .libpyuhd.paths import * diff --git a/host/python/uhd/chdr.py b/host/python/uhd/chdr.py new file mode 100644 index 000000000..577606cc8 --- /dev/null +++ b/host/python/uhd/chdr.py @@ -0,0 +1,60 @@ +# +# Copyright 2020 Ettus Research, a National Instruments Brand +# +# SPDX-License-Identifier: GPL-3.0-or-later +# +from . import libpyuhd as lib + +ChdrPacket = lib.chdr.ChdrPacket +Endianness = lib.chdr.Endianness +ChdrWidth = lib.chdr.ChdrWidth +ChdrHeader = lib.chdr.ChdrHeader +CtrlPayload = lib.chdr.CtrlPayload +CtrlStatus = lib.chdr.CtrlStatus +PacketType = lib.chdr.PacketType +CtrlOpCode = lib.chdr.CtrlOpCode +MgmtPayload = lib.chdr.MgmtPayload +MgmtHop = lib.chdr.MgmtHop +MgmtOp = lib.chdr.MgmtOp +MgmtOpCode = lib.chdr.MgmtOpCode +MgmtOpSelDest = lib.chdr.MgmtOpSelDest +MgmtOpCfg = lib.chdr.MgmtOpCfg +MgmtOpNodeInfo = lib.chdr.MgmtOpNodeInfo +StrsPayload = lib.chdr.StrsPayload +StrsStatus = lib.chdr.StrsStatus +StrcPayload = lib.chdr.StrcPayload +StrcOpCode = lib.chdr.StrcOpCode + +def __get_payload(self): + pkt_type = self.get_header().pkt_type + if pkt_type == PacketType.MGMT: + return self.get_payload_mgmt() + elif pkt_type == PacketType.CTRL: + return self.get_payload_ctrl() + elif pkt_type == PacketType.STRC: + return self.get_payload_strc() + elif pkt_type == PacketType.STRS: + return self.get_payload_strs() + elif pkt_type in {PacketType.DATA_NO_TS, PacketType.DATA_WITH_TS}: + raise RuntimeError("Cannot deserialize a Data Payload") + else: + raise RuntimeError("Invalid pkt_type in ChdrHeader") + +ChdrPacket.get_payload = __get_payload + +def __to_string_with_payload(self): + pkt_type = self.get_header().pkt_type + if pkt_type == PacketType.MGMT: + return self.to_string_with_payload_mgmt() + elif pkt_type == PacketType.CTRL: + return self.to_string_with_payload_ctrl() + elif pkt_type == PacketType.STRC: + return self.to_string_with_payload_strc() + elif pkt_type == PacketType.STRS: + return self.to_string_with_payload_strs() + elif pkt_type in {PacketType.DATA_NO_TS, PacketType.DATA_WITH_TS}: + return self.__str__() + else: + raise RuntimeError("Invalid pkt_type in ChdrHeader") + +ChdrPacket.to_string_with_payload = __to_string_with_payload diff --git a/host/tests/pychdr_parse_test.py b/host/tests/pychdr_parse_test.py index 34842ad28..0a6f17c23 100644 --- a/host/tests/pychdr_parse_test.py +++ b/host/tests/pychdr_parse_test.py @@ -8,7 +8,7 @@ Unit test for libpyuhd.chdr (CHDR Parsing API) """ import unittest -from uhd.libpyuhd import chdr +from uhd import chdr from chdr_resource import hardcoded_packets from chdr_resource import rfnoc_packets_data from chdr_resource import rfnoc_packets_ctrl_mgmt |