From 7e9c132dfb1c3bf38510a75e889c3469f29f3eec Mon Sep 17 00:00:00 2001 From: Nicolas Guillaume Date: Mon, 22 Dec 2025 12:56:00 +0100 Subject: [PATCH] Add an "Append CRC32" block --- grc/CMakeLists.txt | 3 +- grc/ieee802_11_append_crc32.block.yml | 26 ++++++ include/ieee802_11/CMakeLists.txt | 2 +- include/ieee802_11/append_crc32.h | 36 ++++++++ lib/CMakeLists.txt | 1 + lib/append_crc32_impl.cc | 84 +++++++++++++++++++ lib/append_crc32_impl.h | 40 +++++++++ python/bindings/CMakeLists.txt | 2 +- python/bindings/append_crc32_python.cc | 45 ++++++++++ .../docstrings/append_crc32_pydoc_template.h | 24 ++++++ python/bindings/python_bindings.cc | 2 + 11 files changed, 262 insertions(+), 3 deletions(-) create mode 100644 grc/ieee802_11_append_crc32.block.yml create mode 100644 include/ieee802_11/append_crc32.h create mode 100644 lib/append_crc32_impl.cc create mode 100644 lib/append_crc32_impl.h create mode 100644 python/bindings/append_crc32_python.cc create mode 100644 python/bindings/docstrings/append_crc32_pydoc_template.h diff --git a/grc/CMakeLists.txt b/grc/CMakeLists.txt index 793232f..3e2b553 100644 --- a/grc/CMakeLists.txt +++ b/grc/CMakeLists.txt @@ -16,5 +16,6 @@ install(FILES ieee802_11_mapper.block.yml ieee802_11_parse_mac.block.yml ieee802_11_sync_long.block.yml - ieee802_11_sync_short.block.yml DESTINATION share/gnuradio/grc/blocks + ieee802_11_sync_short.block.yml + ieee802_11_append_crc32.block.yml DESTINATION share/gnuradio/grc/blocks ) diff --git a/grc/ieee802_11_append_crc32.block.yml b/grc/ieee802_11_append_crc32.block.yml new file mode 100644 index 0000000..514a7d6 --- /dev/null +++ b/grc/ieee802_11_append_crc32.block.yml @@ -0,0 +1,26 @@ +id: ieee802_11_append_crc32 +label: Append CRC32 +category: '[IEEE802.11]' + +parameters: +- id: append + label: Append + dtype: bool + default: 'True' + options: ['True', 'False'] + option_labels: [Enable, Disable] + +inputs: +- domain: message + id: in + +outputs: +- domain: message + id: out + optional: true + +templates: + imports: import ieee802_11 + make: ieee802_11.append_crc32(${append}) + +file_format: 1 diff --git a/include/ieee802_11/CMakeLists.txt b/include/ieee802_11/CMakeLists.txt index f1f61f9..b893474 100644 --- a/include/ieee802_11/CMakeLists.txt +++ b/include/ieee802_11/CMakeLists.txt @@ -23,5 +23,5 @@ install(FILES signal_field.h sync_long.h sync_short.h - DESTINATION include/ieee802_11 + append_crc32.h DESTINATION include/ieee802_11 ) diff --git a/include/ieee802_11/append_crc32.h b/include/ieee802_11/append_crc32.h new file mode 100644 index 0000000..25bd14e --- /dev/null +++ b/include/ieee802_11/append_crc32.h @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2025 Nicolas Guillaume + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#ifndef INCLUDED_IEEE802_11_APPEND_CRC32_H +#define INCLUDED_IEEE802_11_APPEND_CRC32_H + +#include +#include + +namespace gr { +namespace ieee802_11 { + +class IEEE802_11_API append_crc32 : virtual public block +{ +public: + typedef std::shared_ptr sptr; + static sptr make(bool append = true); +}; + +} +} + +#endif diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index c86ce0a..7edc7bc 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -40,6 +40,7 @@ list(APPEND ieee802_11_sources sync_short.cc utils.cc viterbi_decoder/base.cc + append_crc32_impl.cc ) # use SSE2 optimized viterbi implementation if SSE2 is enabled diff --git a/lib/append_crc32_impl.cc b/lib/append_crc32_impl.cc new file mode 100644 index 0000000..c9e5fe3 --- /dev/null +++ b/lib/append_crc32_impl.cc @@ -0,0 +1,84 @@ +/* + * Copyright (C) 2025 Nicolas Guillaume + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include "append_crc32_impl.h" + +#include +#include + +#include +#include + +#include +#include +#include +#include + +using namespace gr::ieee802_11; + +append_crc32::sptr append_crc32::make(bool append) +{ + return gnuradio::get_initial_sptr(new append_crc32_impl(append)); +} + +append_crc32_impl::append_crc32_impl(bool append) + : block("append_crc32", + gr::io_signature::make(0, 0, 0), + gr::io_signature::make(0, 0, 0)), + d_append(append) +{ + message_port_register_in(pmt::mp("in")); + set_msg_handler(pmt::mp("in"), boost::bind(&append_crc32_impl::handle, this, boost::placeholders::_1)); + message_port_register_out(pmt::mp("out")); +} + +void append_crc32_impl::handle(pmt::pmt_t msg) +{ + if(pmt::is_eof_object(msg)) + { + detail().get()->set_done(true); + return; + } + + if(!pmt::is_pair(msg)) + throw std::invalid_argument("append_crc32 expects PDU pair"); + + pmt::pmt_t meta = pmt::car(msg); + pmt::pmt_t blob = pmt::cdr(msg); + + if(!pmt::is_blob(blob)) + throw std::invalid_argument("append_crc32 expects blob payload"); + + if(!d_append) + { + message_port_pub(pmt::mp("out"), pmt::cons(meta, blob)); + return; + } + + size_t len = pmt::blob_length(blob); + const uint8_t* data = reinterpret_cast(pmt::blob_data(blob)); + + boost::crc_32_type result; + result.process_bytes(data, len); + uint32_t crc = result.checksum(); + + std::vector out(len + sizeof(uint32_t)); + std::memcpy(out.data(), data, len); + std::memcpy(out.data() + len, &crc, sizeof(uint32_t)); + + pmt::pmt_t out_blob = pmt::make_blob(out.data(), out.size()); + message_port_pub(pmt::mp("out"), pmt::cons(meta, out_blob)); +} diff --git a/lib/append_crc32_impl.h b/lib/append_crc32_impl.h new file mode 100644 index 0000000..798a4e7 --- /dev/null +++ b/lib/append_crc32_impl.h @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2025 Nicolas Guillaume + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#ifndef INCLUDED_IEEE802_11_APPEND_CRC32_IMPL_H +#define INCLUDED_IEEE802_11_APPEND_CRC32_IMPL_H + +#include +#include + +namespace gr { +namespace ieee802_11 { + +class append_crc32_impl : public append_crc32 +{ +public: + append_crc32_impl(bool append); + +private: + void handle(pmt::pmt_t msg); + + bool d_append; +}; + +} +} + +#endif diff --git a/python/bindings/CMakeLists.txt b/python/bindings/CMakeLists.txt index 60263a8..9d254e1 100644 --- a/python/bindings/CMakeLists.txt +++ b/python/bindings/CMakeLists.txt @@ -41,7 +41,7 @@ list(APPEND ieee802_11_python_files signal_field_python.cc sync_long_python.cc sync_short_python.cc - python_bindings.cc) + append_crc32_python.cc python_bindings.cc) GR_PYBIND_MAKE_OOT(ieee802_11 ../.. diff --git a/python/bindings/append_crc32_python.cc b/python/bindings/append_crc32_python.cc new file mode 100644 index 0000000..67bcc21 --- /dev/null +++ b/python/bindings/append_crc32_python.cc @@ -0,0 +1,45 @@ +/* + * Copyright 2025 Free Software Foundation, Inc. + * + * 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(1) */ +/* BINDTOOL_USE_PYGCCXML(0) */ +/* BINDTOOL_HEADER_FILE(append_crc32.h) */ +/* BINDTOOL_HEADER_FILE_HASH(de483541a761081a4686ff1953728f8a) */ +/***********************************************************************************/ + +#include +#include +#include + +namespace py = pybind11; + +#include +// pydoc.h is automatically generated in the build directory +#include + +void bind_append_crc32(py::module& m) +{ + + using append_crc32 = ::gr::ieee802_11::append_crc32; + + + py::class_>( + m, "append_crc32", D(append_crc32)) + + .def(py::init(&append_crc32::make), + py::arg("append") = true, + D(append_crc32, make)) + + + ; +} diff --git a/python/bindings/docstrings/append_crc32_pydoc_template.h b/python/bindings/docstrings/append_crc32_pydoc_template.h new file mode 100644 index 0000000..2b4f146 --- /dev/null +++ b/python/bindings/docstrings/append_crc32_pydoc_template.h @@ -0,0 +1,24 @@ +/* + * Copyright 2025 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ +#include "pydoc_macros.h" +#define D(...) DOC(gr, ieee802_11, __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_ieee802_11_append_crc32 = R"doc()doc"; + + +static const char* __doc_gr_ieee802_11_append_crc32_append_crc32_0 = R"doc()doc"; + + +static const char* __doc_gr_ieee802_11_append_crc32_make = R"doc()doc"; diff --git a/python/bindings/python_bindings.cc b/python/bindings/python_bindings.cc index 5f0c79f..56729b7 100644 --- a/python/bindings/python_bindings.cc +++ b/python/bindings/python_bindings.cc @@ -33,6 +33,7 @@ void bind_parse_mac(py::module& m); void bind_signal_field(py::module& m); void bind_sync_long(py::module& m); void bind_sync_short(py::module& m); +void bind_append_crc32(py::module& m); // ) END BINDING_FUNCTION_PROTOTYPES @@ -74,5 +75,6 @@ PYBIND11_MODULE(ieee802_11_python, m) bind_signal_field(m); bind_sync_long(m); bind_sync_short(m); + bind_append_crc32(m); // ) END BINDING_FUNCTION_CALLS }