diff options
author | cmrincon <cmrincon611@hotmail.com> | 2021-07-12 14:16:01 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-12 08:16:01 -0400 |
commit | 7c1d67d6b9e64d93d2b66615be887f23802f16a5 (patch) | |
tree | 732713d321c8540c3f7c75ffe889b1e77c3c4ab6 | |
parent | 6c36e38ce843476f214b716b25e001e96336fc58 (diff) |
digital: Added a crc16 basic block. (#4649)
Signed-off-by: cmrincon <cmrincon611@hotmail.com>
-rw-r--r-- | gr-digital/grc/digital.tree.yml | 1 | ||||
-rw-r--r-- | gr-digital/grc/digital_crc16_async_bb.block.yml | 37 | ||||
-rw-r--r-- | gr-digital/include/gnuradio/digital/CMakeLists.txt | 1 | ||||
-rw-r--r-- | gr-digital/include/gnuradio/digital/crc16_async_bb.h | 60 | ||||
-rw-r--r-- | gr-digital/lib/CMakeLists.txt | 1 | ||||
-rw-r--r-- | gr-digital/lib/crc16_async_bb_impl.cc | 113 | ||||
-rw-r--r-- | gr-digital/lib/crc16_async_bb_impl.h | 48 | ||||
-rw-r--r-- | gr-digital/python/digital/bindings/CMakeLists.txt | 1 | ||||
-rw-r--r-- | gr-digital/python/digital/bindings/crc16_async_bb_python.cc | 48 | ||||
-rw-r--r-- | gr-digital/python/digital/bindings/docstrings/crc16_async_bb_pydoc_template.h | 25 | ||||
-rw-r--r-- | gr-digital/python/digital/bindings/python_bindings.cc | 2 |
11 files changed, 337 insertions, 0 deletions
diff --git a/gr-digital/grc/digital.tree.yml b/gr-digital/grc/digital.tree.yml index 0bf31cb7ca..350ff190bf 100644 --- a/gr-digital/grc/digital.tree.yml +++ b/gr-digital/grc/digital.tree.yml @@ -26,6 +26,7 @@ - Packet Operators: - digital_correlate_access_code_tag_xx - digital_correlate_access_code_xx_ts + - digital_crc16_async_bb - digital_crc32_bb - digital_crc32_async_bb - digital_framer_sink_1 diff --git a/gr-digital/grc/digital_crc16_async_bb.block.yml b/gr-digital/grc/digital_crc16_async_bb.block.yml new file mode 100644 index 0000000000..36be216ef4 --- /dev/null +++ b/gr-digital/grc/digital_crc16_async_bb.block.yml @@ -0,0 +1,37 @@ +id: digital_crc16_async_bb +label: Async CRC16 +flags: [ python, cpp ] + +parameters: +- id: check + label: Mode + dtype: enum + options: ['False', 'True'] + option_labels: [Generate CRC, Check CRC] + +inputs: +- domain: message + id: in + optional: true + +outputs: +- domain: message + id: out + optional: true + +templates: + imports: from gnuradio import digital + make: digital.crc16_async_bb(${check}) + +cpp_templates: + includes: ['#include <gnuradio/digital/crc16_async_bb.h>'] + declarations: 'digital::crc16_async_bb::sptr ${id};' + make: |- + this->${id} = digital::crc16_async_bb::make( + ${check}); + link: ['gnuradio-digital'] + translations: + 'True': 'true' + 'False': 'false' + +file_format: 1 diff --git a/gr-digital/include/gnuradio/digital/CMakeLists.txt b/gr-digital/include/gnuradio/digital/CMakeLists.txt index a4c6b914d6..a4a7a293c1 100644 --- a/gr-digital/include/gnuradio/digital/CMakeLists.txt +++ b/gr-digital/include/gnuradio/digital/CMakeLists.txt @@ -33,6 +33,7 @@ install(FILES correlate_access_code_ff_ts.h costas_loop_cc.h cpmmod_bc.h + crc16_async_bb.h crc32_bb.h crc32_async_bb.h decision_feedback_equalizer.h diff --git a/gr-digital/include/gnuradio/digital/crc16_async_bb.h b/gr-digital/include/gnuradio/digital/crc16_async_bb.h new file mode 100644 index 0000000000..2f10e56b34 --- /dev/null +++ b/gr-digital/include/gnuradio/digital/crc16_async_bb.h @@ -0,0 +1,60 @@ +/* -*- c++ -*- */ +/* + * Copyright 2021 Cesar Martinez. + * + * This file is part of GNU Radio + * + * SPDX-License-Identifier: GPL-3.0-or-later + + * + * + */ + + +#ifndef INCLUDED_DIGITAL_CRC16_ASYNC_BB_H +#define INCLUDED_DIGITAL_CRC16_ASYNC_BB_H + +#include <gnuradio/block.h> +#include <gnuradio/digital/api.h> + +namespace gr { +namespace digital { + +/*! + * \brief Byte-stream CRC block for async messages + * \ingroup packet_operators_blk + * + * \details + * + * Processes packets (as async PDU messages) for CRC16. The \p + * check parameter determines if the block acts to check and strip + * the CRC or to calculate and append the CRC16. + * + * The input PDU is expected to be a message of packet bytes. + * + * When using check mode, if the CRC passes, the output is a + * payload of the message with the CRC stripped, so the output + * will be 2 bytes smaller than the input. + * + * When using calculate mode (check == false), then the CRC is + * calculated on the PDU and appended to it. The output is then 2 + * bytes longer than the input. + * + * This block implements the CRC16 using the Boost crc_optimal + * class for 16-bit CRCs with the standard generator 0x1021. + */ +class DIGITAL_API crc16_async_bb : virtual public block +{ +public: + typedef std::shared_ptr<crc16_async_bb> sptr; + + /*! + * \param check Set to true if you want to check CRC, false to create CRC. + */ + static sptr make(bool check = false); +}; + +} // namespace digital +} // namespace gr + +#endif /* INCLUDED_DIGITAL_CRC16_ASYNC_BB_H */ diff --git a/gr-digital/lib/CMakeLists.txt b/gr-digital/lib/CMakeLists.txt index 9a39a85267..ceee1adb9b 100644 --- a/gr-digital/lib/CMakeLists.txt +++ b/gr-digital/lib/CMakeLists.txt @@ -30,6 +30,7 @@ add_library(gnuradio-digital correlate_access_code_ff_ts_impl.cc costas_loop_cc_impl.cc cpmmod_bc_impl.cc + crc16_async_bb_impl.cc crc32_bb_impl.cc crc32_async_bb_impl.cc decision_feedback_equalizer_impl.cc diff --git a/gr-digital/lib/crc16_async_bb_impl.cc b/gr-digital/lib/crc16_async_bb_impl.cc new file mode 100644 index 0000000000..05f5f96695 --- /dev/null +++ b/gr-digital/lib/crc16_async_bb_impl.cc @@ -0,0 +1,113 @@ +/* -*- c++ -*- */ +/* + * Copyright 2021 Cesar Martinez. + * + * This file is part of GNU Radio + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "crc16_async_bb_impl.h" +#include <gnuradio/io_signature.h> +#include <volk/volk.h> + +namespace gr { +namespace digital { + +crc16_async_bb::sptr crc16_async_bb::make(bool check) +{ + return gnuradio::make_block_sptr<crc16_async_bb_impl>(check); +} + +crc16_async_bb_impl::crc16_async_bb_impl(bool check) + : block("crc16_async_bb", io_signature::make(0, 0, 0), io_signature::make(0, 0, 0)), + d_npass(0), + d_nfail(0) +{ + d_in_port = pmt::mp("in"); + d_out_port = pmt::mp("out"); + + message_port_register_in(d_in_port); + message_port_register_out(d_out_port); + + if (check) + set_msg_handler(d_in_port, [this](pmt::pmt_t msg) { this->check(msg); }); + else + set_msg_handler(d_in_port, [this](pmt::pmt_t msg) { this->calc(msg); }); +} + +crc16_async_bb_impl::~crc16_async_bb_impl() {} + +void crc16_async_bb_impl::calc(pmt::pmt_t msg) +{ + // extract input pdu + pmt::pmt_t meta(pmt::car(msg)); + pmt::pmt_t bytes(pmt::cdr(msg)); + + unsigned int crc; + size_t pkt_len(0); + const uint8_t* bytes_in = pmt::u8vector_elements(bytes, pkt_len); + std::vector<uint8_t> bytes_out(2 + pkt_len); + + crc = process_crc(bytes_in, pkt_len); + + memcpy((void*)bytes_out.data(), (const void*)bytes_in, pkt_len); + memcpy((void*)(bytes_out.data() + pkt_len), &crc, 2); + + pmt::pmt_t output = pmt::init_u8vector( + pkt_len + 2, + bytes_out.data()); // this copies the values from bytes_out into the u8vector + pmt::pmt_t msg_pair = pmt::cons(meta, output); + message_port_pub(d_out_port, msg_pair); +} + +unsigned int crc16_async_bb_impl::process_crc(const uint8_t* bytes_in, + size_t n_bytes_prcss) +{ + + unsigned int result; + + d_crc_ccitt_impl.reset(); + d_crc_ccitt_impl.process_bytes(bytes_in, n_bytes_prcss); + result = d_crc_ccitt_impl(); + return result; +} + +void crc16_async_bb_impl::check(pmt::pmt_t msg) +{ + // extract input pdu + pmt::pmt_t meta(pmt::car(msg)); + pmt::pmt_t bytes(pmt::cdr(msg)); + + unsigned int crc; + size_t pkt_len(0); + const uint8_t* bytes_in = pmt::u8vector_elements(bytes, pkt_len); + + crc = process_crc(bytes_in, pkt_len - 2); + + if (crc != *(unsigned int*)(bytes_in + pkt_len - 2)) { // Drop package + d_nfail++; + return; + } + d_npass++; + + pmt::pmt_t output = pmt::init_u8vector(pkt_len - 2, bytes_in); + pmt::pmt_t msg_pair = pmt::cons(meta, output); + message_port_pub(d_out_port, msg_pair); +} + +int crc16_async_bb_impl::general_work(int noutput_items, + gr_vector_int& ninput_items, + gr_vector_const_void_star& input_items, + gr_vector_void_star& output_items) +{ + return noutput_items; +} + +} /* namespace digital */ +} /* namespace gr */ diff --git a/gr-digital/lib/crc16_async_bb_impl.h b/gr-digital/lib/crc16_async_bb_impl.h new file mode 100644 index 0000000000..9e65164021 --- /dev/null +++ b/gr-digital/lib/crc16_async_bb_impl.h @@ -0,0 +1,48 @@ +/* -*- c++ -*- */ +/* + * Copyright 2021 Cesar Martinez. + * + * This file is part of GNU Radio + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#ifndef INCLUDED_DIGITAL_CRC16_ASYNC_BB_IMPL_H +#define INCLUDED_DIGITAL_CRC16_ASYNC_BB_IMPL_H + +#include <gnuradio/digital/crc16_async_bb.h> +#include <boost/crc.hpp> + +namespace gr { +namespace digital { + +class crc16_async_bb_impl : public crc16_async_bb +{ +private: + boost::crc_ccitt_type d_crc_ccitt_impl; + + pmt::pmt_t d_in_port; + pmt::pmt_t d_out_port; + + unsigned int process_crc(const uint8_t* bytes_in, size_t n_bytes_prcss); + void calc(pmt::pmt_t msg); + void check(pmt::pmt_t msg); + +public: + crc16_async_bb_impl(bool check); + ~crc16_async_bb_impl() override; + + int general_work(int noutput_items, + gr_vector_int& ninput_items, + gr_vector_const_void_star& input_items, + gr_vector_void_star& output_items) override; + + uint64_t d_npass; + uint64_t d_nfail; +}; + +} // namespace digital +} // namespace gr + +#endif /* INCLUDED_DIGITAL_crc16_ASYNC_BB_IMPL_H */ diff --git a/gr-digital/python/digital/bindings/CMakeLists.txt b/gr-digital/python/digital/bindings/CMakeLists.txt index 762769419b..e1a851b65d 100644 --- a/gr-digital/python/digital/bindings/CMakeLists.txt +++ b/gr-digital/python/digital/bindings/CMakeLists.txt @@ -28,6 +28,7 @@ list(APPEND digital_python_files correlate_access_code_tag_ff_python.cc costas_loop_cc_python.cc cpmmod_bc_python.cc + crc16_async_bb_python.cc crc32_async_bb_python.cc crc32_bb_python.cc decision_feedback_equalizer_python.cc diff --git a/gr-digital/python/digital/bindings/crc16_async_bb_python.cc b/gr-digital/python/digital/bindings/crc16_async_bb_python.cc new file mode 100644 index 0000000000..798fe85f04 --- /dev/null +++ b/gr-digital/python/digital/bindings/crc16_async_bb_python.cc @@ -0,0 +1,48 @@ +/* -*- c++ -*- */ +/* + * Copyright 2021 Cesar Martinez. + * + * This file is part of GNU Radio + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +/***********************************************************************************/ +/* This file is automatically generated using bindtool and can be manually edited */ +/* The following lines can be configured to regenerate this file during cmake */ +/* If manual edits are made, the following tags should be modified accordingly. */ +/* BINDTOOL_GEN_AUTOMATIC(0) */ +/* BINDTOOL_USE_PYGCCXML(0) */ +/* BINDTOOL_HEADER_FILE(crc16_async_bb.h) */ +/* BINDTOOL_HEADER_FILE_HASH(0266da27c09c311e854b9350c3761696) */ +/***********************************************************************************/ + +#include <pybind11/complex.h> +#include <pybind11/pybind11.h> +#include <pybind11/stl.h> + +namespace py = pybind11; + +#include <gnuradio/digital/crc16_async_bb.h> +// pydoc.h is automatically generated in the build directory +#include <crc16_async_bb_pydoc.h> + +void bind_crc16_async_bb(py::module& m) +{ + + using crc16_async_bb = ::gr::digital::crc16_async_bb; + + + py::class_<crc16_async_bb, + gr::block, + gr::basic_block, + std::shared_ptr<crc16_async_bb>>(m, "crc16_async_bb", D(crc16_async_bb)) + + .def(py::init(&crc16_async_bb::make), + py::arg("check") = false, + D(crc16_async_bb, make)) + + + ; +} diff --git a/gr-digital/python/digital/bindings/docstrings/crc16_async_bb_pydoc_template.h b/gr-digital/python/digital/bindings/docstrings/crc16_async_bb_pydoc_template.h new file mode 100644 index 0000000000..5edbf7cddd --- /dev/null +++ b/gr-digital/python/digital/bindings/docstrings/crc16_async_bb_pydoc_template.h @@ -0,0 +1,25 @@ +/* -*- c++ -*- */ +/* + * Copyright 2021 Cesar Martinez. + * + * This file is part of GNU Radio + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ +#include "pydoc_macros.h" +#define D(...) DOC(gr, digital, __VA_ARGS__) +/* + This file contains placeholders for docstrings for the Python bindings. + Do not edit! These were automatically extracted during the binding process + and will be overwritten during the build process + */ + + +static const char* __doc_gr_digital_crc16_async_bb = R"doc()doc"; + + +static const char* __doc_gr_digital_crc16_async_bb_crc32_async_bb = R"doc()doc"; + + +static const char* __doc_gr_digital_crc16_async_bb_make = R"doc()doc"; diff --git a/gr-digital/python/digital/bindings/python_bindings.cc b/gr-digital/python/digital/bindings/python_bindings.cc index 0eabcf63d1..8c6aff1995 100644 --- a/gr-digital/python/digital/bindings/python_bindings.cc +++ b/gr-digital/python/digital/bindings/python_bindings.cc @@ -38,6 +38,7 @@ void bind_correlate_access_code_tag_bb(py::module&); void bind_correlate_access_code_tag_ff(py::module&); void bind_costas_loop_cc(py::module&); void bind_cpmmod_bc(py::module&); +void bind_crc16_async_bb(py::module&); void bind_crc32_async_bb(py::module&); void bind_crc32_bb(py::module&); void bind_decision_feedback_equalizer(py::module&); @@ -140,6 +141,7 @@ PYBIND11_MODULE(digital_python, m) bind_correlate_access_code_tag_ff(m); bind_costas_loop_cc(m); bind_cpmmod_bc(m); + bind_crc16_async_bb(m); bind_crc32_async_bb(m); bind_crc32_bb(m); bind_decision_feedback_equalizer(m); |