diff options
author | Tim O'Shea | 2013-01-10 11:39:05 -0500 |
---|---|---|
committer | Johnathan Corgan | 2013-01-14 12:32:00 -0800 |
commit | d1d210da036af0723c15d1c1f85fbacebe456bb5 (patch) | |
tree | ed8ec98fc95705f6293f36370eb520ebed591a6d /gnuradio-core | |
parent | ca407436f4d42aee0511181e2c2e6eecdccfa7d7 (diff) | |
download | gnuradio-d1d210da036af0723c15d1c1f85fbacebe456bb5.tar.gz gnuradio-d1d210da036af0723c15d1c1f85fbacebe456bb5.tar.bz2 gnuradio-d1d210da036af0723c15d1c1f85fbacebe456bb5.zip |
core: adding gr_random_pdu message passing block
Conflicts:
gnuradio-core/src/lib/general/CMakeLists.txt
gnuradio-core/src/lib/general/general.i
Diffstat (limited to 'gnuradio-core')
-rw-r--r-- | gnuradio-core/src/lib/general/gr_random_pdu.cc | 83 | ||||
-rw-r--r-- | gnuradio-core/src/lib/general/gr_random_pdu.h | 64 | ||||
-rw-r--r-- | gnuradio-core/src/lib/general/gr_random_pdu.i | 30 |
3 files changed, 177 insertions, 0 deletions
diff --git a/gnuradio-core/src/lib/general/gr_random_pdu.cc b/gnuradio-core/src/lib/general/gr_random_pdu.cc new file mode 100644 index 000000000..9f692c72b --- /dev/null +++ b/gnuradio-core/src/lib/general/gr_random_pdu.cc @@ -0,0 +1,83 @@ +/* -*- c++ -*- */ +/* + * Copyright 2012 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_random_pdu.h> +#include <gr_io_signature.h> +#include <cstdio> +#include <errno.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <stdexcept> +#include <string.h> +#include <iostream> + +// public constructor that returns a shared_ptr + +gr_random_pdu_sptr +gr_make_random_pdu (int items_min, int items_max) +{ + return gnuradio::get_initial_sptr(new gr_random_pdu(items_min, items_max)); +} + +gr_random_pdu::gr_random_pdu (int items_min, int items_max) + : gr_block("random_pdu", + gr_make_io_signature(0, 0, 0), + gr_make_io_signature(0, 0, 0)), + urange(items_min, items_max), + brange(0, 255), + rvar(rng, urange), + bvar(rng, brange) +{ + message_port_register_out(pmt::mp("pdus")); + message_port_register_in(pmt::mp("generate")); + set_msg_handler(pmt::mp("generate"), boost::bind(&gr_random_pdu::generate_pdu, this, _1)); +} + +bool gr_random_pdu::start(){ + output_random(); + return true; +} + +void gr_random_pdu::output_random(){ + + // pick a random vector length + int len = rvar(); + + // fill it with random bytes + unsigned char vec[len]; + for(int i=0; i<len; i++){ + vec[i] = (unsigned char) bvar(); + } + + // send the vector + pmt::pmt_t vecpmt( pmt::pmt_make_blob( vec, len ) ); + pmt::pmt_t pdu( pmt::pmt_cons( pmt::PMT_NIL, vecpmt ) ); + message_port_pub( pmt::mp("pdus"), pdu ); + + std::cout << "sending new random vector of length " << len << "\n"; +} + diff --git a/gnuradio-core/src/lib/general/gr_random_pdu.h b/gnuradio-core/src/lib/general/gr_random_pdu.h new file mode 100644 index 000000000..e6457d21b --- /dev/null +++ b/gnuradio-core/src/lib/general/gr_random_pdu.h @@ -0,0 +1,64 @@ +/* -*- c++ -*- */ +/* + * Copyright 2012 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_RANDOM_PDU_H +#define INCLUDED_GR_RANDOM_PDU_H + +#include <gr_core_api.h> +#include <gr_block.h> +#include <gr_message.h> +#include <gr_msg_queue.h> + +#include <boost/random.hpp> +#include <boost/generator_iterator.hpp> + +class gr_random_pdu; +typedef boost::shared_ptr<gr_random_pdu> gr_random_pdu_sptr; + +GR_CORE_API gr_random_pdu_sptr gr_make_random_pdu (int mintime, int maxtime); + +/*! + * \brief Send message at defined interval + * \ingroup msg_blk + */ +class GR_CORE_API gr_random_pdu : public gr_block +{ + private: + friend GR_CORE_API gr_random_pdu_sptr + gr_make_random_pdu(int mintime, int maxtime); + + void output_random(); + + boost::mt19937 rng; + boost::uniform_int<> urange; + boost::uniform_int<> brange; + boost::variate_generator< boost::mt19937, boost::uniform_int<> > rvar; // pdu length + boost::variate_generator< boost::mt19937, boost::uniform_int<> > bvar; // pdu contents + + public: + gr_random_pdu (int, int); + bool start(); + void generate_pdu(pmt::pmt_t msg){ output_random(); } + void generate_pdu(){ output_random(); } +}; + +#endif /* INCLUDED_GR_RANDOM_PDU_H */ diff --git a/gnuradio-core/src/lib/general/gr_random_pdu.i b/gnuradio-core/src/lib/general/gr_random_pdu.i new file mode 100644 index 000000000..045a33060 --- /dev/null +++ b/gnuradio-core/src/lib/general/gr_random_pdu.i @@ -0,0 +1,30 @@ +/* -*- c++ -*- */ +/* + * Copyright 2005 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. + */ + +GR_SWIG_BLOCK_MAGIC(gr,random_pdu); + +%{ +#include <gr_random_pdu.h> +%} + +%include "gr_random_pdu.h" + |