From 919a147afcbecace5107a4d0a4da556cfd56df92 Mon Sep 17 00:00:00 2001 From: Samuel O'Brien Date: Thu, 25 Jun 2020 15:43:32 -0500 Subject: python: Add bindings for C++ CHDR Parser This commit adds pybind11 glue code for the userland chdr parsing code introduced in the uhd::utils::chdr namespace. Additionally, it moves some pybind11 adapter code to a common pybind_adaptors.hpp file which originally existed in the cal_python.hpp file. This commit also adds unit tests for the python bindings using a captured wireshark trace which is located in rfnoc_packets_*.py and some handwritten packets in hardcoded_packets.py Signed-off-by: Samuel O'Brien --- host/include/uhd/utils/CMakeLists.txt | 1 + host/include/uhd/utils/pybind_adaptors.hpp | 39 ++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 host/include/uhd/utils/pybind_adaptors.hpp (limited to 'host/include') diff --git a/host/include/uhd/utils/CMakeLists.txt b/host/include/uhd/utils/CMakeLists.txt index 08d488771..51d67b824 100644 --- a/host/include/uhd/utils/CMakeLists.txt +++ b/host/include/uhd/utils/CMakeLists.txt @@ -29,6 +29,7 @@ UHD_INSTALL(FILES paths.hpp pimpl.hpp platform.hpp + pybind_adaptors.hpp safe_call.hpp safe_main.hpp scope_exit.hpp diff --git a/host/include/uhd/utils/pybind_adaptors.hpp b/host/include/uhd/utils/pybind_adaptors.hpp new file mode 100644 index 000000000..6b095d198 --- /dev/null +++ b/host/include/uhd/utils/pybind_adaptors.hpp @@ -0,0 +1,39 @@ +// +// Copyright 2020 Ettus Research, a National Instruments Brand +// +// SPDX-License-Identifier: GPL-3.0-or-later +// + +#pragma once + +#include +#include + +namespace pybind11 { namespace detail { +template +struct type_caster> : optional_caster> +{ +}; +}} // namespace pybind11::detail + +std::vector pybytes_to_vector(const py::bytes& data) +{ + const std::string data_str = std::string(data); + return std::vector(data_str.cbegin(), data_str.cend()); +} + +py::bytes vector_to_pybytes(const std::vector& data) +{ + return py::bytes(std::string(data.cbegin(), data.cend())); +} + +std::vector pybytes_to_u64_vector(const py::bytes& data) +{ + const std::string data_str = std::string(data); + return std::vector(data_str.cbegin(), data_str.cend()); +} + +py::bytes u64_vector_to_pybytes(const std::vector& data) +{ + return py::bytes(std::string(data.cbegin(), data.cend())); +} -- cgit v1.2.3