diff options
author | Johnathan Corgan <johnathan@corganlabs.com> | 2013-02-28 12:23:08 -0800 |
---|---|---|
committer | Johnathan Corgan <johnathan@corganlabs.com> | 2013-02-28 12:23:08 -0800 |
commit | 054720d5fc754882f310e44a00416bcf2d2fd239 (patch) | |
tree | 02b65173f349fa87abfcfa295d7ab4a88e602bd4 | |
parent | 33c7198b12eaae233e5e0e0a2bc266f2efb018b1 (diff) |
blocks: added gr::blocks::pdu_to_tagged_stream
-rw-r--r-- | gr-blocks/grc/blocks_block_tree.xml | 1 | ||||
-rw-r--r-- | gr-blocks/grc/blocks_pdu_to_tagged_stream.xml | 40 | ||||
-rw-r--r-- | gr-blocks/include/blocks/CMakeLists.txt | 1 | ||||
-rw-r--r-- | gr-blocks/include/blocks/pdu_to_tagged_stream.h | 53 | ||||
-rw-r--r-- | gr-blocks/lib/CMakeLists.txt | 1 | ||||
-rw-r--r-- | gr-blocks/lib/pdu_to_tagged_stream_impl.cc | 120 | ||||
-rw-r--r-- | gr-blocks/lib/pdu_to_tagged_stream_impl.h | 48 | ||||
-rw-r--r-- | gr-blocks/swig/blocks_swig.i | 4 |
8 files changed, 268 insertions, 0 deletions
diff --git a/gr-blocks/grc/blocks_block_tree.xml b/gr-blocks/grc/blocks_block_tree.xml index a2658d1466..7f7f405c1b 100644 --- a/gr-blocks/grc/blocks_block_tree.xml +++ b/gr-blocks/grc/blocks_block_tree.xml @@ -32,6 +32,7 @@ <name>Sources (New)</name> <block>blocks_file_source</block> <block>blocks_file_meta_source</block> + <block>blocks_pdu_to_tagged_stream</block> </cat> <cat> <name>Sinks (New)</name> diff --git a/gr-blocks/grc/blocks_pdu_to_tagged_stream.xml b/gr-blocks/grc/blocks_pdu_to_tagged_stream.xml new file mode 100644 index 0000000000..2a7de84759 --- /dev/null +++ b/gr-blocks/grc/blocks_pdu_to_tagged_stream.xml @@ -0,0 +1,40 @@ +<?xml version="1.0"?> +<!-- +################################################### +## PDU Message to Tagged Stream +################################################### + --> +<block> + <name>PDU to Tagged Stream</name> + <key>blocks_pdu_to_tagged_stream</key> + <import>from gnuradio import blocks</import> + <make>blocks.pdu_to_tagged_stream($type.tv)</make> + <param> + <name>Item Type</name> + <key>type</key> + <type>enum</type> + <option> + <name>Byte</name> + <key>byte</key> + <opt>tv:blocks.byte_t</opt> + </option> + <option> + <name>Complex</name> + <key>complex</key> + <opt>tv:blocks.complex_t</opt> + </option> + <option> + <name>Float</name> + <key>float</key> + <opt>tv:blocks.float_t</opt> + </option> + </param> + <sink> + <name>pdus</name> + <type>message</type> + </sink> + <source> + <name>out</name> + <type>$type</type> + </source> +</block> diff --git a/gr-blocks/include/blocks/CMakeLists.txt b/gr-blocks/include/blocks/CMakeLists.txt index 05672ecf90..eddf5ef511 100644 --- a/gr-blocks/include/blocks/CMakeLists.txt +++ b/gr-blocks/include/blocks/CMakeLists.txt @@ -128,6 +128,7 @@ install(FILES pack_k_bits_bb.h patterned_interleaver.h pdu.h + pdu_to_tagged_stream.h peak_detector2_fb.h regenerate_bb.h repeat.h diff --git a/gr-blocks/include/blocks/pdu_to_tagged_stream.h b/gr-blocks/include/blocks/pdu_to_tagged_stream.h new file mode 100644 index 0000000000..cf64f41dc2 --- /dev/null +++ b/gr-blocks/include/blocks/pdu_to_tagged_stream.h @@ -0,0 +1,53 @@ +/* -*- 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_BLOCKS_PDU_TO_TAGGED_STREAM_H +#define INCLUDED_BLOCKS_PDU_TO_TAGGED_STREAM_H + +#include <blocks/api.h> +#include <blocks/pdu.h> +#include <gr_sync_block.h> + +namespace gr { + namespace blocks { + + /*! + * \brief Turns received PDUs into a tagged stream of items + * \ingroup source_blk + */ + class BLOCKS_API pdu_to_tagged_stream : virtual public gr_sync_block + { + public: + // gr::blocks::pdu_to_tagged_stream::sptr + typedef boost::shared_ptr<pdu_to_tagged_stream> sptr; + + /*! + * \brief Construct a pdu_to_tagged_stream block + * \param type PDU type of pdu::vector_type + */ + static sptr make(pdu::vector_type type); + }; + + } /* namespace blocks */ +} /* namespace gr */ + +#endif /* INCLUDED_BLOCKS_PDU_TO_TAGGED_STREAM_H */ diff --git a/gr-blocks/lib/CMakeLists.txt b/gr-blocks/lib/CMakeLists.txt index 87f41ff289..881e8d06e1 100644 --- a/gr-blocks/lib/CMakeLists.txt +++ b/gr-blocks/lib/CMakeLists.txt @@ -167,6 +167,7 @@ list(APPEND gr_blocks_sources pack_k_bits_bb_impl.cc patterned_interleaver_impl.cc pdu.cc + pdu_to_tagged_stream_impl.cc peak_detector2_fb_impl.cc regenerate_bb_impl.cc repeat_impl.cc diff --git a/gr-blocks/lib/pdu_to_tagged_stream_impl.cc b/gr-blocks/lib/pdu_to_tagged_stream_impl.cc new file mode 100644 index 0000000000..80785b4781 --- /dev/null +++ b/gr-blocks/lib/pdu_to_tagged_stream_impl.cc @@ -0,0 +1,120 @@ +/* -*- 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 "pdu_to_tagged_stream_impl.h" +#include <blocks/pdu.h> +#include <gr_io_signature.h> + +namespace gr { + namespace blocks { + + pdu_to_tagged_stream::sptr + pdu_to_tagged_stream::make(pdu::vector_type type) + { + return gnuradio::get_initial_sptr(new pdu_to_tagged_stream_impl(type)); + } + + pdu_to_tagged_stream_impl::pdu_to_tagged_stream_impl(pdu::vector_type type) + : gr_sync_block("pdu_to_tagged_stream", + gr_make_io_signature(0, 0, 0), + gr_make_io_signature(1, 1, pdu::itemsize(type))), + d_itemsize(pdu::itemsize(type)), + d_type(type) + { + message_port_register_in(PDU_PORT_ID); + } + + int + pdu_to_tagged_stream_impl::work(int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items) + { + char *out = (char *)output_items[0]; + int nout = 0; + + // if we have remaining output, send it + if (d_remain.size() > 0) { + nout = std::min((size_t)d_remain.size()/d_itemsize, (size_t)noutput_items); + memcpy(out, &d_remain[0], nout*d_itemsize); + d_remain.erase(d_remain.begin(), d_remain.begin()+nout); + noutput_items -= nout; + out += nout*d_itemsize; + } + + // if we have space for at least one item output as much as we can + if (noutput_items > 0) { + + // grab a message if one exists + pmt::pmt_t msg(delete_head_blocking(PDU_PORT_ID)); + if (msg.get() == NULL) + return nout; + + // make sure type is valid + if (!pmt::pmt_is_pair(msg)) // TODO: implement pdu::is_valid() + throw std::runtime_error("received a malformed pdu message"); + + // grab the components of the pdu message + pmt::pmt_t meta(pmt::pmt_car(msg)); + pmt::pmt_t vect(pmt::pmt_cdr(msg)); + + // compute offset for output tag + uint64_t offset = nitems_written(0) + nout; + + // add a tag for pdu length + add_item_tag(0, offset, PDU_LENGTH_TAG, pmt::pmt_from_long(pmt::pmt_length(vect)), pmt::mp(alias())); + + // if we recieved metadata add it as tags + if (!pmt_eq(meta, pmt::PMT_NIL) ) { + pmt::pmt_t pair(pmt::pmt_dict_keys(meta)); + + while (!pmt_eq(pair, pmt::PMT_NIL) ) { + pmt::pmt_t k(pmt::pmt_cdr(pair)); + pmt::pmt_t v(pmt::pmt_dict_ref(meta, k, pmt::PMT_NIL)); + add_item_tag(0, offset, k, v, pmt::mp(alias())); + } + } + + // copy vector output + size_t ncopy = std::min((size_t)noutput_items, (size_t)pmt::pmt_length(vect)); + size_t nsave = pmt::pmt_length(vect) - ncopy; + + // copy output + size_t io(0); + nout += ncopy; + memcpy(out, pmt_uniform_vector_elements(vect,io), ncopy*d_itemsize); + + // save leftover items if needed for next work call + if (nsave > 0) { + d_remain.resize(nsave*d_itemsize, 0); + memcpy(&d_remain[0], pmt_uniform_vector_elements(vect,ncopy), nsave*d_itemsize); + } + } + + return nout; + } + + } /* namespace blocks */ +} /* namespace gr */ diff --git a/gr-blocks/lib/pdu_to_tagged_stream_impl.h b/gr-blocks/lib/pdu_to_tagged_stream_impl.h new file mode 100644 index 0000000000..ca1c6437bd --- /dev/null +++ b/gr-blocks/lib/pdu_to_tagged_stream_impl.h @@ -0,0 +1,48 @@ +/* -*- 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_PDU_TO_TAGGED_STREAM_IMPL_H +#define INCLUDED_PDU_TO_TAGGED_STREAM_IMPL_H + +#include <blocks/pdu_to_tagged_stream.h> + +namespace gr { + namespace blocks { + + class BLOCKS_API pdu_to_tagged_stream_impl : public pdu_to_tagged_stream + { + size_t d_itemsize; + pdu::vector_type d_type; + std::vector<uint8_t> d_remain; + + public: + pdu_to_tagged_stream_impl(pdu::vector_type type); + + int work(int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items); + }; + + } /* namespace blocks */ +} /* namespace gr */ + +#endif diff --git a/gr-blocks/swig/blocks_swig.i b/gr-blocks/swig/blocks_swig.i index c5ca221fbe..80fe9a4b63 100644 --- a/gr-blocks/swig/blocks_swig.i +++ b/gr-blocks/swig/blocks_swig.i @@ -23,6 +23,7 @@ #define BLOCKS_API %include "gnuradio.i" +%include "blocks/pdu.h" //load generated python docstrings %include "blocks_swig_doc.i" @@ -103,6 +104,7 @@ #include "blocks/packed_to_unpacked_bb.h" #include "blocks/packed_to_unpacked_ss.h" #include "blocks/packed_to_unpacked_ii.h" +#include "blocks/pdu_to_tagged_stream.h" #include "blocks/peak_detector2_fb.h" #include "blocks/or_bb.h" #include "blocks/or_ss.h" @@ -216,6 +218,7 @@ %include "blocks/packed_to_unpacked_ss.h" %include "blocks/packed_to_unpacked_ii.h" %include "blocks/patterned_interleaver.h" +%include "blocks/pdu_to_tagged_stream.h" %include "blocks/peak_detector2_fb.h" %include "blocks/regenerate_bb.h" %include "blocks/repeat.h" @@ -323,6 +326,7 @@ GR_SWIG_BLOCK_MAGIC2(blocks, packed_to_unpacked_bb); GR_SWIG_BLOCK_MAGIC2(blocks, packed_to_unpacked_ss); GR_SWIG_BLOCK_MAGIC2(blocks, packed_to_unpacked_ii); GR_SWIG_BLOCK_MAGIC2(blocks, peak_detector2_fb); +GR_SWIG_BLOCK_MAGIC2(blocks, pdu_to_tagged_stream); GR_SWIG_BLOCK_MAGIC2(blocks, or_bb); GR_SWIG_BLOCK_MAGIC2(blocks, or_ss); GR_SWIG_BLOCK_MAGIC2(blocks, or_ii); |