summaryrefslogtreecommitdiff
path: root/gnuradio-core/src/lib/runtime
diff options
context:
space:
mode:
authorJohnathan Corgan <johnathan@corganlabs.com>2013-03-15 09:57:31 -0700
committerJohnathan Corgan <johnathan@corganlabs.com>2013-03-15 09:57:31 -0700
commite965a5bb209ad46a509ccd21f393667fd69d95f9 (patch)
tree40d683ef8f80980f12eac6cfe7f3423c49b45f87 /gnuradio-core/src/lib/runtime
parent2bf9c4cb4b0b426690f353fc1662a13e70c0d5e0 (diff)
parent27990ca9e236931e39a830e48f0a1efe13ec085f (diff)
Merge branch 'ofdm-master' into ofdm-next
Added fixups for next branch changes Conflicts: CMakeLists.txt gnuradio-core/src/lib/io/gr_message_sink.cc gnuradio-core/src/lib/io/gr_message_sink.h gnuradio-core/src/lib/io/gr_message_sink.i gnuradio-core/src/lib/io/gr_message_source.cc gnuradio-core/src/lib/io/gr_message_source.h gnuradio-core/src/lib/io/gr_message_source.i gr-blocks/CMakeLists.txt gr-digital/CMakeLists.txt gr-digital/grc/digital_block_tree.xml gr-digital/include/digital/CMakeLists.txt gr-digital/include/digital_ofdm_cyclic_prefixer.h gr-digital/lib/CMakeLists.txt gr-digital/lib/digital_ofdm_cyclic_prefixer.cc gr-digital/lib/ofdm_cyclic_prefixer_impl.h gr-digital/python/CMakeLists.txt gr-digital/swig/CMakeLists.txt gr-digital/swig/digital_swig.i
Diffstat (limited to 'gnuradio-core/src/lib/runtime')
-rw-r--r--gnuradio-core/src/lib/runtime/CMakeLists.txt3
-rw-r--r--gnuradio-core/src/lib/runtime/gr_tagged_stream_block.cc144
-rw-r--r--gnuradio-core/src/lib/runtime/gr_tagged_stream_block.h138
-rw-r--r--gnuradio-core/src/lib/runtime/gr_tagged_stream_block.i30
-rw-r--r--gnuradio-core/src/lib/runtime/runtime.i2
5 files changed, 317 insertions, 0 deletions
diff --git a/gnuradio-core/src/lib/runtime/CMakeLists.txt b/gnuradio-core/src/lib/runtime/CMakeLists.txt
index f6fce8fb16..a322d1b998 100644
--- a/gnuradio-core/src/lib/runtime/CMakeLists.txt
+++ b/gnuradio-core/src/lib/runtime/CMakeLists.txt
@@ -79,6 +79,7 @@ list(APPEND gnuradio_core_sources
${CMAKE_CURRENT_SOURCE_DIR}/gr_sync_decimator.cc
${CMAKE_CURRENT_SOURCE_DIR}/gr_sync_interpolator.cc
${CMAKE_CURRENT_SOURCE_DIR}/gr_sys_paths.cc
+ ${CMAKE_CURRENT_SOURCE_DIR}/gr_tagged_stream_block.cc
${CMAKE_CURRENT_SOURCE_DIR}/gr_top_block.cc
${CMAKE_CURRENT_SOURCE_DIR}/gr_top_block_impl.cc
${CMAKE_CURRENT_SOURCE_DIR}/gr_tpb_detail.cc
@@ -160,6 +161,7 @@ install(FILES
${CMAKE_CURRENT_SOURCE_DIR}/gr_sync_block.h
${CMAKE_CURRENT_SOURCE_DIR}/gr_sync_decimator.h
${CMAKE_CURRENT_SOURCE_DIR}/gr_sync_interpolator.h
+ ${CMAKE_CURRENT_SOURCE_DIR}/gr_tagged_stream_block.h
${CMAKE_CURRENT_SOURCE_DIR}/gr_top_block.h
${CMAKE_CURRENT_SOURCE_DIR}/gr_top_block_impl.h
${CMAKE_CURRENT_SOURCE_DIR}/gr_tpb_detail.h
@@ -218,6 +220,7 @@ install(FILES
${CMAKE_CURRENT_SOURCE_DIR}/gr_sync_block.i
${CMAKE_CURRENT_SOURCE_DIR}/gr_sync_decimator.i
${CMAKE_CURRENT_SOURCE_DIR}/gr_sync_interpolator.i
+ ${CMAKE_CURRENT_SOURCE_DIR}/gr_tagged_stream_block.i
${CMAKE_CURRENT_SOURCE_DIR}/gr_tags.i
${CMAKE_CURRENT_SOURCE_DIR}/gr_top_block.i
${CMAKE_CURRENT_SOURCE_DIR}/runtime.i
diff --git a/gnuradio-core/src/lib/runtime/gr_tagged_stream_block.cc b/gnuradio-core/src/lib/runtime/gr_tagged_stream_block.cc
new file mode 100644
index 0000000000..32b6a09033
--- /dev/null
+++ b/gnuradio-core/src/lib/runtime/gr_tagged_stream_block.cc
@@ -0,0 +1,144 @@
+/* -*- c++ -*- */
+/*
+ * Copyright 2013 Free Software Foundation, Inc.
+ *
+ * This file is part of GNU Radio
+ *
+ * GNU Radio 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, or (at your option)
+ * any later version.
+ *
+ * GNU Radio 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 GNU Radio; see the file COPYING. If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <gr_tagged_stream_block.h>
+
+gr_tagged_stream_block::gr_tagged_stream_block (const std::string &name,
+ gr_io_signature_sptr input_signature,
+ gr_io_signature_sptr output_signature,
+ const std::string &length_tag_key)
+ : gr_block(name, input_signature, output_signature),
+ d_length_tag_key(pmt::string_to_symbol(length_tag_key)),
+ d_n_input_items_reqd(input_signature->min_streams(), 0),
+ d_length_tag_key_str(length_tag_key)
+{
+}
+
+
+// This is evil hackery: We trick the scheduler into creating the right number of input items
+void
+gr_tagged_stream_block::forecast (int noutput_items, gr_vector_int &ninput_items_required)
+{
+ unsigned ninputs = ninput_items_required.size();
+ for (unsigned i = 0; i < ninputs; i++) {
+ if (i < d_n_input_items_reqd.size() && d_n_input_items_reqd[i] != 0) {
+ ninput_items_required[i] = d_n_input_items_reqd[i];
+ } else {
+ // If there's no item, there's no tag--so there must at least be one!
+ ninput_items_required[i] = std::max(1, (int) std::floor((double) noutput_items / relative_rate() + 0.5));
+ }
+ }
+}
+
+
+void
+gr_tagged_stream_block::parse_length_tags(
+ const std::vector<std::vector<gr_tag_t> > &tags,
+ gr_vector_int &n_input_items_reqd
+){
+ for (unsigned i = 0; i < tags.size(); i++) {
+ for (unsigned k = 0; k < tags[i].size(); k++) {
+ if (tags[i][k].key == d_length_tag_key) {
+ n_input_items_reqd[i] = pmt::to_long(tags[i][k].value);
+ remove_item_tag(i, tags[i][k]);
+ }
+ }
+ }
+}
+
+
+int
+gr_tagged_stream_block::calculate_output_stream_length(const gr_vector_int &ninput_items)
+{
+ int noutput_items = *std::max_element(ninput_items.begin(), ninput_items.end());
+ return (int) std::floor(relative_rate() * noutput_items + 0.5);
+}
+
+
+void
+gr_tagged_stream_block::update_length_tags(int n_produced, int n_ports)
+{
+ for (int i = 0; i < n_ports; i++) {
+ add_item_tag(i, nitems_written(i),
+ d_length_tag_key,
+ pmt::from_long(n_produced)
+ );
+ }
+ return;
+}
+
+
+int
+gr_tagged_stream_block::general_work (int noutput_items,
+ gr_vector_int &ninput_items,
+ gr_vector_const_void_star &input_items,
+ gr_vector_void_star &output_items)
+{
+ if (d_length_tag_key_str.empty()) {
+ return work(noutput_items, ninput_items, input_items, output_items);
+ }
+
+ if (d_n_input_items_reqd[0] == 0) { // Otherwise, it's already set from a previous call
+ std::vector<std::vector<gr_tag_t> > tags(input_items.size(), std::vector<gr_tag_t>());
+ for (unsigned i = 0; i < input_items.size(); i++) {
+ get_tags_in_range(tags[i], i, nitems_read(i), nitems_read(i)+1);
+ }
+ d_n_input_items_reqd.assign(input_items.size(), -1);
+ parse_length_tags(tags, d_n_input_items_reqd);
+ }
+ for (unsigned i = 0; i < input_items.size(); i++) {
+ if (d_n_input_items_reqd[i] == -1) {
+ throw std::runtime_error("Missing length tag.");
+ }
+ if (d_n_input_items_reqd[i] > ninput_items[i]) {
+ return 0;
+ }
+ }
+
+ int min_output_size = calculate_output_stream_length(d_n_input_items_reqd);
+ if (noutput_items < min_output_size) {
+ set_min_noutput_items(min_output_size);
+ return 0;
+ }
+ set_min_noutput_items(1);
+
+ // WORK CALLED HERE //
+ int n_produced = work(noutput_items, d_n_input_items_reqd, input_items, output_items);
+ //////////////////////
+
+ if (n_produced == WORK_DONE) {
+ return n_produced;
+ }
+ for (int i = 0; i < (int) d_n_input_items_reqd.size(); i++) {
+ consume(i, d_n_input_items_reqd[i]);
+ }
+ update_length_tags(n_produced, output_items.size());
+
+ d_n_input_items_reqd.assign(input_items.size(), 0);
+
+ return n_produced;
+}
+
diff --git a/gnuradio-core/src/lib/runtime/gr_tagged_stream_block.h b/gnuradio-core/src/lib/runtime/gr_tagged_stream_block.h
new file mode 100644
index 0000000000..a9d396c06f
--- /dev/null
+++ b/gnuradio-core/src/lib/runtime/gr_tagged_stream_block.h
@@ -0,0 +1,138 @@
+/* -*- c++ -*- */
+/*
+ * Copyright 2013 Free Software Foundation, Inc.
+ *
+ * This file is part of GNU Radio
+ *
+ * GNU Radio 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, or (at your option)
+ * any later version.
+ *
+ * GNU Radio 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 GNU Radio; see the file COPYING. If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#ifndef INCLUDED_GR_TAGGED_STREAM_BLOCK_H
+#define INCLUDED_GR_TAGGED_STREAM_BLOCK_H
+
+#include <gr_core_api.h>
+#include <gr_block.h>
+
+/*!
+ * \brief Block that operates on PDUs in form of tagged streams
+ * \ingroup base_blk
+ *
+ * Override work to provide the signal processing implementation.
+ */
+class GR_CORE_API gr_tagged_stream_block : public gr_block
+{
+ private:
+ pmt::pmt_t d_length_tag_key; //! This is the key for the tag that stores the PDU length
+ gr_vector_int d_n_input_items_reqd; //! How many input items do I need to process the next PDU?
+
+ protected:
+ std::string d_length_tag_key_str;
+ gr_tagged_stream_block (void){} //allows pure virtual interface sub-classes
+ gr_tagged_stream_block (const std::string &name,
+ gr_io_signature_sptr input_signature,
+ gr_io_signature_sptr output_signature,
+ const std::string &length_tag_key);
+
+ /* \brief Parse all tags on the first sample of a PDU, return the number of items per input
+ * and prune the length tags.
+ *
+ * In most cases, you don't need to override this, unless the number of items read
+ * is not directly coded in one single tag.
+ *
+ * Default behaviour:
+ * - Go through all input ports
+ * - On every input port, search for the tag with the key specified in \p length_tag_key
+ * - Copy that value as an int to the corresponding position in \p n_input_items_reqd
+ * - Remove the length tag.
+ *
+ * \param[in] tags All the tags found on the first item of every input port.
+ * \param[out] n_input_items_reqd Number of items which will be read from every input
+ */
+ virtual void parse_length_tags(
+ const std::vector<std::vector<gr_tag_t> > &tags,
+ gr_vector_int &n_input_items_reqd
+ );
+
+ /* \brief Calculate the number of output items.
+ *
+ * This is basically the inverse function to forecast(): Given a number of input
+ * items, it returns the maximum number of output items.
+ *
+ * You most likely need to override this function, unless your block is a sync
+ * block or integer interpolator/decimator.
+ *
+ */
+ virtual int calculate_output_stream_length(const gr_vector_int &ninput_items);
+
+ /* \brief Set the new length tags on the output stream
+ *
+ * Default behaviour: Set a tag with key \p length_tag_key and
+ * the number of produced items on every output port.
+ *
+ * For anything else, override this.
+ *
+ * \param n_produced Length of the new PDU
+ * \param n_ports Number of output ports
+ */
+ virtual void update_length_tags(int n_produced, int n_ports);
+
+ public:
+
+ /* \brief Don't override this.
+ */
+ void /* final */ forecast (int noutput_items, gr_vector_int &ninput_items_required);
+
+ /* - Reads the number of input items from the tags using parse_length_tags()
+ * - Checks there's enough data on the input and output buffers
+ * - If not, inform the scheduler and do nothing
+ * - Calls work() with the exact number of items per PDU
+ * - Updates the tags using update_length_tags()
+ */
+ int general_work (int noutput_items,
+ gr_vector_int &ninput_items,
+ gr_vector_const_void_star &input_items,
+ gr_vector_void_star &output_items);
+
+ /*!
+ * \brief Just like gr_block::general_work, but makes sure the input is valid
+ *
+ * The user must override work to define the signal processing code.
+ * Check the documentation for general_work() to see what happens here.
+ *
+ * Like gr_sync_block, this calls consume() for you (it consumes ninput_items[i]
+ * items from the i-th port).
+ *
+ * A note on tag propagation: The PDU length tags are handled by other functions,
+ * but all other tags are handled just as in any other \p gr_block. So, most likely,
+ * you either set the tag propagation policy to TPP_DONT and handle the tag
+ * propagation manually, or you propagate tags through the scheduler and don't
+ * do anything here.
+ *
+ * \param noutput_items The size of the writable output buffer
+ * \param ninput_items The exact size of the items on every input for this particular PDU.
+ * These will be consumed if a length tag key is provided!
+ * \param input_items See gr_block
+ * \param output_items See gr_block
+ */
+ virtual int work (int noutput_items,
+ gr_vector_int &ninput_items,
+ gr_vector_const_void_star &input_items,
+ gr_vector_void_star &output_items) = 0;
+
+};
+
+#endif /* INCLUDED_GR_TAGGED_STREAM_BLOCK_H */
+
diff --git a/gnuradio-core/src/lib/runtime/gr_tagged_stream_block.i b/gnuradio-core/src/lib/runtime/gr_tagged_stream_block.i
new file mode 100644
index 0000000000..9fc803dca1
--- /dev/null
+++ b/gnuradio-core/src/lib/runtime/gr_tagged_stream_block.i
@@ -0,0 +1,30 @@
+/* -*- c++ -*- */
+/*
+ * Copyright 2013 Free Software Foundation, Inc.
+ *
+ * This file is part of GNU Radio
+ *
+ * GNU Radio 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, or (at your option)
+ * any later version.
+ *
+ * GNU Radio 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 GNU Radio; see the file COPYING. If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street,
+ * Boston, MA 02110-1301, USA.
+ */
+class gr_tagged_stream_block : public gr_block
+{
+ protected:
+ gr_tagged_stream_block (const std::string &name,
+ gr_io_signature_sptr input_signature,
+ gr_io_signature_sptr output_signature,
+ const std::string &length_tag_key);
+};
+
diff --git a/gnuradio-core/src/lib/runtime/runtime.i b/gnuradio-core/src/lib/runtime/runtime.i
index 73a816a14c..ac0092f917 100644
--- a/gnuradio-core/src/lib/runtime/runtime.i
+++ b/gnuradio-core/src/lib/runtime/runtime.i
@@ -39,6 +39,7 @@
#include <gr_sync_block.h>
#include <gr_sync_decimator.h>
#include <gr_sync_interpolator.h>
+#include <gr_tagged_stream_block.h>
#include <gr_top_block.h>
#include <gr_logger.h>
%}
@@ -67,6 +68,7 @@
%include <gr_sync_block.i>
%include <gr_sync_decimator.i>
%include <gr_sync_interpolator.i>
+%include <gr_tagged_stream_block.i>
%include <gr_top_block.i>
%include <gr_logger.i>