From d6b05b0c08c126e870f322d2734ce74ce577e479 Mon Sep 17 00:00:00 2001 From: Swarnava Ghosh Date: Thu, 24 Aug 2023 23:17:56 -0400 Subject: [PATCH] orgainzed more stuff. Tests failing probably due to gnuradio's stubborn not-enough-samples issue. Added temporary patch for that but further investigation is required --- Testing/Temporary/CTestCostData.txt | 1 + Testing/Temporary/LastTest.log | 3 + grc/UTAT_HERON_esttc_deframer.block.yml | 2 +- .../gnuradio/UTAT_HERON/header_format_esttc.h | 1 + lib/CMakeLists.txt | 4 + lib/esttc_deframer_impl.cc | 142 +++++------------- lib/esttc_framer_impl.cc | 18 ++- lib/header_format_esttc.cc | 6 +- lib/utils/common.cc | 12 ++ lib/utils/common.h | 24 +++ lib/utils/pdu_lambda.cc | 36 +++++ lib/utils/pdu_lambda.h | 29 ++++ .../bindings/header_format_esttc_python.cc | 2 +- python/UTAT_HERON/qa_esttc_deframer.py | 25 ++- python/UTAT_HERON/qa_esttc_framer.py | 3 +- 15 files changed, 189 insertions(+), 119 deletions(-) create mode 100644 Testing/Temporary/CTestCostData.txt create mode 100644 Testing/Temporary/LastTest.log create mode 100644 lib/utils/common.cc create mode 100644 lib/utils/common.h create mode 100644 lib/utils/pdu_lambda.cc create mode 100644 lib/utils/pdu_lambda.h diff --git a/Testing/Temporary/CTestCostData.txt b/Testing/Temporary/CTestCostData.txt new file mode 100644 index 0000000..ed97d53 --- /dev/null +++ b/Testing/Temporary/CTestCostData.txt @@ -0,0 +1 @@ +--- diff --git a/Testing/Temporary/LastTest.log b/Testing/Temporary/LastTest.log new file mode 100644 index 0000000..27f8a8a --- /dev/null +++ b/Testing/Temporary/LastTest.log @@ -0,0 +1,3 @@ +Start testing: Aug 24 20:30 EDT +---------------------------------------------------------- +End testing: Aug 24 20:30 EDT diff --git a/grc/UTAT_HERON_esttc_deframer.block.yml b/grc/UTAT_HERON_esttc_deframer.block.yml index 179339a..e9661b8 100644 --- a/grc/UTAT_HERON_esttc_deframer.block.yml +++ b/grc/UTAT_HERON_esttc_deframer.block.yml @@ -16,7 +16,7 @@ parameters: - id: samp_rate label: Sample Rate dtype: float - default: 48e3 + default: samp_rate #- id: ... # label: ... # dtype: ... diff --git a/include/gnuradio/UTAT_HERON/header_format_esttc.h b/include/gnuradio/UTAT_HERON/header_format_esttc.h index 8910f84..d30ff1c 100644 --- a/include/gnuradio/UTAT_HERON/header_format_esttc.h +++ b/include/gnuradio/UTAT_HERON/header_format_esttc.h @@ -36,6 +36,7 @@ class UTAT_HERON_API header_format_esttc : public gr::digital::header_format_def std::vector& info, int& nbits_processed) override; size_t header_nbits() const override; + size_t header_nbits_without_access_code() const; protected: int d_trailer_nbits; void enter_have_sync() override; diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 386ad2a..bee3f04 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -17,6 +17,10 @@ list(APPEND UTAT_HERON_sources utils/heron_packet.h utils/debug_logger.cc utils/debug_logger.h + utils/pdu_lambda.h + utils/pdu_lambda.cc + utils/common.h + utils/common.cc heron_rx_bb_impl.cc heron_rx_bb_impl.h header_format_esttc.cc diff --git a/lib/esttc_deframer_impl.cc b/lib/esttc_deframer_impl.cc index f1ac08c..931566a 100644 --- a/lib/esttc_deframer_impl.cc +++ b/lib/esttc_deframer_impl.cc @@ -6,6 +6,8 @@ */ #include "esttc_deframer_impl.h" +#include "utils/pdu_lambda.h" +#include "utils/common.h" #include #include @@ -25,96 +27,23 @@ esttc_deframer::sptr esttc_deframer::make(float samp_rate) return gnuradio::make_block_sptr(samp_rate); } -class prepend_length : public gr::sync_block{ - -private: - int skip_bytes; - void handle_msg(const pmt::pmt_t& pdu){ - auto data = pmt::u8vector_elements(pmt::cdr(pdu)); - auto start = data.begin(); - data.insert(start, data.size()-skip_bytes); - auto new_cdr = pmt::init_u8vector(data.size(), data); - auto new_pdu = pmt::cons(pmt::car(pdu), new_cdr); - message_port_pub(pmt::intern("pdu_out"), new_pdu); - } -public: - - prepend_length(int skip_bytes=0): - gr::sync_block( - "prepend_length", - gr::io_signature::make(0,0,0), - gr::io_signature::make(0,0,0) - ), - skip_bytes(skip_bytes) - { - message_port_register_in(pmt::intern("pdu_in")); - message_port_register_out(pmt::intern("pdu_out")); - - set_msg_handler(pmt::intern("pdu_in"), [this](const pmt::pmt_t& pdu){ - this->handle_msg(pdu); - }); - } - - int work( - int noutput_items, - gr_vector_const_void_star& input_items, - gr_vector_void_star& output_items - ) override - { - return 0; - } - - typedef std::shared_ptr sptr; - - static sptr make(int skip_bytes=0){ - return gnuradio::make_block_sptr(skip_bytes); - } -}; - -class strip_prepended_length : public gr::sync_block{ -private: - void handle_msg(const pmt::pmt_t& pdu){ - auto data = pmt::u8vector_elements(pmt::cdr(pdu)); - auto start = data.begin(); - auto end = data.end(); - data = std::vector(start+1, end); - auto new_cdr = pmt::init_u8vector(data.size(), data); - auto new_pdu = pmt::cons(pmt::car(pdu), new_cdr); - message_port_pub(pmt::intern("pdu_out"), new_pdu); - } -public: - - strip_prepended_length(): - gr::sync_block( - "strip_prepended_length", - gr::io_signature::make(0,0,0), - gr::io_signature::make(0,0,0) - ) - { - message_port_register_in(pmt::intern("pdu_in")); - message_port_register_out(pmt::intern("pdu_out")); - - set_msg_handler(pmt::intern("pdu_in"), [this](const pmt::pmt_t& pdu){ - this->handle_msg(pdu); - }); - } - - int work( - int noutput_items, - gr_vector_const_void_star& input_items, - gr_vector_void_star& output_items - ) override - { - return 0; - } - - typedef std::shared_ptr sptr; - - static sptr make(){ - return gnuradio::make_block_sptr(); - } +void prepend_len_minus_crc_bytes(pmt::pmt_t pdu){ + auto data = pmt::u8vector_elements(pmt::cdr(pdu)); + auto start = data.begin(); + uint8_t crc_nbytes = utils::crc::num_bits/8; + data.insert(start, data.size()-crc_nbytes); + auto new_cdr = pmt::init_u8vector(data.size(), data); + pmt::set_cdr(pdu, new_cdr); +} -}; +void strip_prepended_len(pmt::pmt_t pdu){ + auto data = pmt::u8vector_elements(pmt::cdr(pdu)); + auto start = data.begin(); + auto end = data.end(); + data = std::vector(start+1, end); + auto new_cdr = pmt::init_u8vector(data.size(), data.data()); + pmt::set_cdr(pdu, new_cdr); +} /* @@ -126,21 +55,28 @@ esttc_deframer_impl::esttc_deframer_impl(float samp_rate) gr::io_signature::make(0, 0, 0)) { - message_port_register_hier_out(pmt::intern("crc_ok")); - message_port_register_hier_out(pmt::intern("crc_fail")); + message_port_register_hier_out(pmt::mp("crc_ok")); + message_port_register_hier_out(pmt::mp("crc_fail")); - auto access_code = "101010101010101010101010101010101010101001111110"; - - auto hdr_format = UTAT_HERON::header_format_esttc::make(access_code, 3, 1, 16); - auto find_access_code = gr::digital::correlate_access_code_tag_bb::make(&access_code[8], 0, "time_est"); - auto hdr_payload_demux = gr::digital::header_payload_demux::make(8, 1, 0, "payload symbols", "time_est", false, sizeof(uint8_t), "rx_time", samp_rate); + auto hdr_format = UTAT_HERON::header_format_esttc::make(utils::access_code, 3, 1, utils::crc::num_bits); + auto find_access_code = gr::digital::correlate_access_code_tag_bb::make(utils::trimmed_access_code, 0, "time_est"); + auto hdr_payload_demux = gr::digital::header_payload_demux::make( + hdr_format->header_nbits_without_access_code(), + 1, 0, "payload symbols", "time_est", false, sizeof(uint8_t), "", samp_rate); auto parser = gr::digital::protocol_parser_b::make(hdr_format); auto pack_bits = gr::blocks::repack_bits_bb::make(1, 8, "", false, gr::GR_MSB_FIRST); auto scale_length = gr::blocks::tagged_stream_multiply_length::make(sizeof(uint8_t), "payload symbols", 1./8); auto stream_to_pdu = gr::pdu::tagged_stream_to_pdu::make(gr::types::byte_t, "payload symbols"); - auto prepend_length = prepend_length::make(2); - auto crc = gr::digital::crc_check::make(16, 0x1021, 0xFFFF, 0x0000, false, false, false, true, 0); - auto strip_prepended_len = strip_prepended_length::make(); + auto prepend_length = utils::pdu_lambda::make(prepend_len_minus_crc_bytes); + auto crc = gr::digital::crc_check::make( + utils::crc::num_bits, + utils::crc::poly, + utils::crc::inital_value, + utils::crc::final_xor, + utils::crc::input_reflected, + utils::crc::result_reflected, + false, true, 0); + auto strip_prepended_length = utils::pdu_lambda::make(strip_prepended_len); connect(self(), 0, find_access_code, 0); connect(find_access_code, 0, hdr_payload_demux, 0); @@ -151,10 +87,14 @@ esttc_deframer_impl::esttc_deframer_impl(float samp_rate) connect(scale_length, 0, stream_to_pdu, 0); msg_connect(stream_to_pdu, "pdus", prepend_length, "pdu_in"); msg_connect(prepend_length, "pdu_out", crc, "in"); - msg_connect(crc, "ok", strip_prepended_len, "pdu_in"); + msg_connect(crc, "ok", strip_prepended_length, "pdu_in"); msg_connect(crc, "fail", self(), "crc_fail"); - msg_connect(strip_prepended_len, "pdu_out", self(), "crc_ok"); + msg_connect(strip_prepended_length, "pdu_out", self(), "crc_ok"); + // msg_connect(stream_to_pdu, "pdus", crc, "in"); + // msg_connect(crc, "ok", self(), "crc_ok"); + // msg_connect(crc, "fail", self(), "crc_fail"); + } /* diff --git a/lib/esttc_framer_impl.cc b/lib/esttc_framer_impl.cc index 8530685..c3df341 100644 --- a/lib/esttc_framer_impl.cc +++ b/lib/esttc_framer_impl.cc @@ -6,6 +6,7 @@ */ #include "esttc_framer_impl.h" +#include "utils/common.h" #include #include @@ -33,18 +34,23 @@ esttc_framer_impl::esttc_framer_impl() gr::io_signature::make(0,0,0), gr::io_signature::make(0,0,0)) { - message_port_register_hier_in(pmt::intern("pdu_in")); - message_port_register_hier_out(pmt::intern("pdu_out")); + message_port_register_hier_in(pmt::mp("pdu_in")); + message_port_register_hier_out(pmt::mp("pdu_out")); - auto access_code = "101010101010101010101010101010101010101001111110"; - - auto hdr_format = UTAT_HERON::header_format_esttc::make(access_code, 3, 1, 16); + auto hdr_format = UTAT_HERON::header_format_esttc::make(utils::access_code, 3, 1, utils::crc::num_bits); auto formatter = gr::digital::protocol_formatter_async::make(hdr_format); auto header_stream = gr::pdu::pdu_to_tagged_stream::make(gr::types::byte_t, "packet_len"); auto payload_stream = gr::pdu::pdu_to_tagged_stream::make(gr::types::byte_t, "packet_len"); auto combine = gr::blocks::tagged_stream_mux::make(1, "packet_len"); auto stream_to_pdu = gr::pdu::tagged_stream_to_pdu::make(gr::types::byte_t, "packet_len"); - auto crc = gr::digital::crc_append::make(16, 0x1021, 0xFFFF, 0x0000, false, false, false, 6); + auto crc = gr::digital::crc_append::make( + utils::crc::num_bits, + utils::crc::poly, + utils::crc::inital_value, + utils::crc::final_xor, + utils::crc::input_reflected, + utils::crc::result_reflected, + false, 6); msg_connect(self(), "pdu_in", formatter, "in"); msg_connect(formatter, "header", header_stream, "pdus"); diff --git a/lib/header_format_esttc.cc b/lib/header_format_esttc.cc index 7f0b923..c3de6f3 100644 --- a/lib/header_format_esttc.cc +++ b/lib/header_format_esttc.cc @@ -59,7 +59,11 @@ bool header_format_esttc::parse( size_t header_format_esttc::header_nbits() const{ - return d_access_code_len + 8 * 1 * sizeof(uint8_t); + return d_access_code_len + header_nbits_without_access_code(); +} + +size_t header_format_esttc::header_nbits_without_access_code() const{ + return 8 * 1 * sizeof(uint8_t); } inline void header_format_esttc::enter_have_sync(){ diff --git a/lib/utils/common.cc b/lib/utils/common.cc new file mode 100644 index 0000000..f526af1 --- /dev/null +++ b/lib/utils/common.cc @@ -0,0 +1,12 @@ +#include "common.h" + +namespace gr{ +namespace UTAT_HERON{ +namespace utils{ + +const char* access_code = "101010101010101010101010101010101010101001111110"; +const char* trimmed_access_code = access_code + access_code_front_trim; + +} +} +} \ No newline at end of file diff --git a/lib/utils/common.h b/lib/utils/common.h new file mode 100644 index 0000000..f695982 --- /dev/null +++ b/lib/utils/common.h @@ -0,0 +1,24 @@ +#include +#include + +namespace gr{ +namespace UTAT_HERON{ +namespace utils{ +namespace crc{ + + constexpr int num_bits = 16; + constexpr uint64_t poly = 0x1021; + constexpr uint64_t inital_value = 0xFFFF; + constexpr uint64_t final_xor = 0x0000; + constexpr bool input_reflected = false; + constexpr bool result_reflected = false; + +} + +extern const char* access_code; +constexpr int access_code_front_trim = 8; +extern const char* trimmed_access_code; + +} +} +} diff --git a/lib/utils/pdu_lambda.cc b/lib/utils/pdu_lambda.cc new file mode 100644 index 0000000..42ba497 --- /dev/null +++ b/lib/utils/pdu_lambda.cc @@ -0,0 +1,36 @@ +#include "pdu_lambda.h" + +namespace gr{ +namespace UTAT_HERON{ +namespace utils{ + +pdu_lambda::sptr pdu_lambda::make(pdu_lambda::func callback){ + return gnuradio::make_block_sptr(callback); +} + +pdu_lambda::pdu_lambda(pdu_lambda::func callback): + gr::sync_block( + "pdu_lambda", + gr::io_signature::make(0,0,0), + gr::io_signature::make(0,0,0) + ), + callback(callback) +{ + message_port_register_in(pmt::mp("pdu_in")); + message_port_register_out(pmt::mp("pdu_out")); + + set_msg_handler(pmt::mp("pdu_in"), [this](pmt::pmt_t pdu){ + this->callback(pdu); + message_port_pub(pmt::mp("pdu_out"), pdu); + }); +} + +int pdu_lambda::work( + int noutput_items, + gr_vector_const_void_star& input_items, + gr_vector_void_star& output_items +){ return 0; } + +} +} +} \ No newline at end of file diff --git a/lib/utils/pdu_lambda.h b/lib/utils/pdu_lambda.h new file mode 100644 index 0000000..061d43c --- /dev/null +++ b/lib/utils/pdu_lambda.h @@ -0,0 +1,29 @@ + +#include +#include + +namespace gr{ +namespace UTAT_HERON{ +namespace utils{ + +class pdu_lambda : public gr::sync_block{ +private: + typedef std::function func; + func callback; +public: + + pdu_lambda(func callback); + + int work( + int noutput_items, + gr_vector_const_void_star& input_items, + gr_vector_void_star& output_items + ) override; + + typedef std::shared_ptr sptr; + static sptr make(func callback); +}; + +} +} +} \ No newline at end of file diff --git a/python/UTAT_HERON/bindings/header_format_esttc_python.cc b/python/UTAT_HERON/bindings/header_format_esttc_python.cc index c7b6a6a..eeba78a 100644 --- a/python/UTAT_HERON/bindings/header_format_esttc_python.cc +++ b/python/UTAT_HERON/bindings/header_format_esttc_python.cc @@ -14,7 +14,7 @@ /* BINDTOOL_GEN_AUTOMATIC(0) */ /* BINDTOOL_USE_PYGCCXML(0) */ /* BINDTOOL_HEADER_FILE(header_format_esttc.h) */ -/* BINDTOOL_HEADER_FILE_HASH(4debf424b7f86e2767073c5f2884768e) */ +/* BINDTOOL_HEADER_FILE_HASH(2c8333b07b17c7f3cbc49db2d0ba17a4) */ /***********************************************************************************/ #include diff --git a/python/UTAT_HERON/qa_esttc_deframer.py b/python/UTAT_HERON/qa_esttc_deframer.py index 87eba19..cbc4e23 100755 --- a/python/UTAT_HERON/qa_esttc_deframer.py +++ b/python/UTAT_HERON/qa_esttc_deframer.py @@ -6,9 +6,10 @@ # SPDX-License-Identifier: GPL-3.0-or-later # +import time import pmt from gnuradio import gr, gr_unittest -from gnuradio import blocks, pdu +from gnuradio import blocks try: from gnuradio.UTAT_HERON import esttc_deframer except ImportError: @@ -27,22 +28,32 @@ def tearDown(self): self.tb = None def test_instance(self): - instance = esttc_deframer(1e6) + instance = esttc_deframer(32e3) def test_001_basic(self): + + samp_rate = 32e3 input = [0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0x7e, 0x02, 0xff, 0xff, 0xbf, 0xf3] expected_output = [0xff, 0xff] - src = blocks.vector_source_b(input, False, 1, []) + src = blocks.vector_source_b(input, True, 1, []) unpack = blocks.unpack_k_bits_bb(8) - deframer = esttc_deframer(1e6) - msg_debug = blocks.message_debug(True, gr.log_levels.info) + deframer = esttc_deframer(samp_rate) + msg_debug = blocks.message_debug() + self.tb.connect(src, unpack) self.tb.connect(unpack, deframer) self.tb.msg_connect((deframer, 'crc_ok'), (msg_debug, 'store')) - self.tb.msg_connect((deframer, 'crc_fail'), (msg_debug, 'print')) - self.tb.run() + self.tb.msg_connect((deframer, 'crc_ok'), (msg_debug, 'log')) + self.tb.msg_connect((deframer, 'crc_fail'), (msg_debug, 'store')) + self.tb.msg_connect((deframer, 'crc_fail'), (msg_debug, 'log')) + self.tb.start() + start = time.time() + while msg_debug.num_messages() < 1 and time.time() - start < 5: + pass + self.tb.stop() + self.assertGreaterEqual(msg_debug.num_messages(), 1) pdu_out = msg_debug.get_message(0) output = pmt.u8vector_elements(pmt.cdr(pdu_out)) diff --git a/python/UTAT_HERON/qa_esttc_framer.py b/python/UTAT_HERON/qa_esttc_framer.py index b89f498..1250323 100755 --- a/python/UTAT_HERON/qa_esttc_framer.py +++ b/python/UTAT_HERON/qa_esttc_framer.py @@ -6,7 +6,6 @@ # SPDX-License-Identifier: GPL-3.0-or-later # - import pmt from gnuradio import gr, gr_unittest from gnuradio import blocks, pdu @@ -39,7 +38,7 @@ def test_001_basic(self): tag = blocks.stream_to_tagged_stream(gr.sizeof_char, 1, len(input), 'packet_len') to_pdu = pdu.tagged_stream_to_pdu(gr.types.byte_t, 'packet_len') framer = esttc_framer() - msg_debug = blocks.message_debug(True, gr.log_levels.info) + msg_debug = blocks.message_debug() self.tb.connect(src, tag) self.tb.connect(tag, to_pdu)