From d1d210da036af0723c15d1c1f85fbacebe456bb5 Mon Sep 17 00:00:00 2001 From: Tim O'Shea Date: Thu, 10 Jan 2013 11:39:05 -0500 Subject: core: adding gr_random_pdu message passing block Conflicts: gnuradio-core/src/lib/general/CMakeLists.txt gnuradio-core/src/lib/general/general.i --- gnuradio-core/src/lib/general/gr_random_pdu.cc | 83 ++++++++++++++++++++++++++ gnuradio-core/src/lib/general/gr_random_pdu.h | 64 ++++++++++++++++++++ gnuradio-core/src/lib/general/gr_random_pdu.i | 30 ++++++++++ 3 files changed, 177 insertions(+) create mode 100644 gnuradio-core/src/lib/general/gr_random_pdu.cc create mode 100644 gnuradio-core/src/lib/general/gr_random_pdu.h create mode 100644 gnuradio-core/src/lib/general/gr_random_pdu.i (limited to 'gnuradio-core/src/lib/general') 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// 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 +#include +#include +#include + +#include +#include + +class gr_random_pdu; +typedef boost::shared_ptr 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 +%} + +%include "gr_random_pdu.h" + -- cgit From 5ab960295f00991fa9447819b3ff9eaf8d88d28e Mon Sep 17 00:00:00 2001 From: Roy Thompson Date: Thu, 31 Jan 2013 13:16:12 -0700 Subject: core: Enabling msg_connect within python blocks. --- gnuradio-core/src/lib/general/gr_block_gateway.h | 52 ++++++++++++++++++++++++ gnuradio-core/src/lib/general/gr_feval.cc | 16 ++++++++ gnuradio-core/src/lib/general/gr_feval.h | 29 +++++++++++++ gnuradio-core/src/lib/general/gr_feval.i | 29 +++++++++++++ 4 files changed, 126 insertions(+) (limited to 'gnuradio-core/src/lib/general') diff --git a/gnuradio-core/src/lib/general/gr_block_gateway.h b/gnuradio-core/src/lib/general/gr_block_gateway.h index ae91d41b5..c876ea8e1 100644 --- a/gnuradio-core/src/lib/general/gr_block_gateway.h +++ b/gnuradio-core/src/lib/general/gr_block_gateway.h @@ -188,6 +188,58 @@ public: gr_block::get_tags_in_range(tags, which_input, abs_start, abs_end, key); return tags; } + + /* Message passing interface */ + void gr_block__message_port_register_in(pmt::pmt_t port_id){ + gr_basic_block::message_port_register_in(port_id); + } + + void gr_block__message_port_register_out(pmt::pmt_t port_id){ + gr_basic_block::message_port_register_out(port_id); + } + + void gr_block__message_port_pub(pmt::pmt_t port_id, pmt::pmt_t msg){ + gr_basic_block::message_port_pub(port_id, msg); + } + + void gr_block__message_port_sub(pmt::pmt_t port_id, pmt::pmt_t target){ + gr_basic_block::message_port_sub(port_id, target); + } + + void gr_block__message_port_unsub(pmt::pmt_t port_id, pmt::pmt_t target){ + gr_basic_block::message_port_unsub(port_id, target); + } + + pmt::pmt_t gr_block__message_ports_in(){ + return gr_basic_block::message_ports_in(); + } + + pmt::pmt_t gr_block__message_ports_out(){ + return gr_basic_block::message_ports_out(); + } + + void set_msg_handler_feval(pmt::pmt_t which_port, gr_feval_p *msg_handler) + { + if(msg_queue.find(which_port) == msg_queue.end()){ + throw std::runtime_error("attempt to set_msg_handler_feval() on bad input message port!"); + } + d_msg_handlers_feval[which_port] = msg_handler; + } + +protected: + typedef std::map msg_handlers_feval_t; + msg_handlers_feval_t d_msg_handlers_feval; + + void dispatch_msg(pmt::pmt_t which_port, pmt::pmt_t msg){ + // Is there a handler? + if (d_msg_handlers_feval.find(which_port) != d_msg_handlers_feval.end()){ + d_msg_handlers_feval[which_port]->calleval(msg); // Yes, invoke it. + } + else { + // Pass to generic dispatcher if not found + gr_basic_block::dispatch_msg(which_port, msg); + } + } }; /*! diff --git a/gnuradio-core/src/lib/general/gr_feval.cc b/gnuradio-core/src/lib/general/gr_feval.cc index ca5714a79..89f09984c 100644 --- a/gnuradio-core/src/lib/general/gr_feval.cc +++ b/gnuradio-core/src/lib/general/gr_feval.cc @@ -88,6 +88,22 @@ gr_feval::calleval(void) eval(); } +// ---------------------------------------------------------------- + +gr_feval_p::~gr_feval_p(){} + +void +gr_feval_p::eval(pmt::pmt_t x) +{ + // nop +} + +void +gr_feval_p::calleval(pmt::pmt_t x) +{ + eval(x); +} + /* * Trivial examples showing C++ (transparently) calling Python */ diff --git a/gnuradio-core/src/lib/general/gr_feval.h b/gnuradio-core/src/lib/general/gr_feval.h index 1726a8a7f..a9bccfe51 100644 --- a/gnuradio-core/src/lib/general/gr_feval.h +++ b/gnuradio-core/src/lib/general/gr_feval.h @@ -24,6 +24,7 @@ #include #include +#include /*! * \brief base class for evaluating a function: double -> double @@ -137,6 +138,34 @@ public: virtual void calleval(); // invoke "eval" }; +/*! + * \brief base class for evaluating a function: pmt -> void + * \ingroup misc + * + * This class is designed to be subclassed in Python or C++ + * and is callable from both places. It uses SWIG's + * "director" feature to implement the magic. + * It's slow. Don't use it in a performance critical path. + * + * Override eval to define the behavior. + * Use calleval to invoke eval (this kludge is required to allow a + * python specific "shim" to be inserted. + */ +class GR_CORE_API gr_feval_p +{ +protected: + /*! + * \brief override this to define the function + */ + virtual void eval(pmt::pmt_t x); + +public: + gr_feval_p() {} + virtual ~gr_feval_p(); + + virtual void calleval(pmt::pmt_t x); // invoke "eval" +}; + /*! * \brief trivial examples / test cases showing C++ calling Python code */ diff --git a/gnuradio-core/src/lib/general/gr_feval.i b/gnuradio-core/src/lib/general/gr_feval.i index bc219a643..bcf4f1e64 100644 --- a/gnuradio-core/src/lib/general/gr_feval.i +++ b/gnuradio-core/src/lib/general/gr_feval.i @@ -45,23 +45,28 @@ // Directors are only supported in Python, Java and C# #ifdef SWIGPYTHON +%include "pmt_swig.i" +using namespace pmt; // Enable SWIG directors for these classes %feature("director") gr_py_feval_dd; %feature("director") gr_py_feval_cc; %feature("director") gr_py_feval_ll; %feature("director") gr_py_feval; +%feature("director") gr_py_feval_p; %feature("nodirector") gr_py_feval_dd::calleval; %feature("nodirector") gr_py_feval_cc::calleval; %feature("nodirector") gr_py_feval_ll::calleval; %feature("nodirector") gr_py_feval::calleval; +%feature("nodirector") gr_py_feval_p::calleval; %rename(feval_dd) gr_py_feval_dd; %rename(feval_cc) gr_py_feval_cc; %rename(feval_ll) gr_py_feval_ll; %rename(feval) gr_py_feval; +%rename(feval_p) gr_py_feval_p; //%exception { // try { $action } @@ -136,12 +141,26 @@ public: virtual void calleval(); }; +%ignore gr_feval_p; +class gr_feval_p +{ +protected: + virtual void eval(pmt_t x); + +public: + gr_feval_p() {} + virtual ~gr_feval_p(); + + virtual void calleval(pmt_t x); +}; + /* * These are the ones to derive from in Python. They have the magic shim * that ensures that we're holding the Python GIL when we enter Python land... */ %inline %{ +#include class gr_py_feval_dd : public gr_feval_dd { @@ -183,6 +202,16 @@ class gr_py_feval : public gr_feval } }; +class gr_py_feval_p : public gr_feval_p +{ + public: + void calleval(pmt::pmt_t x) + { + ensure_py_gil_state _lock; + eval(x); + } +}; + %} -- cgit From d56898de3f3515b43afd93b56304f9e0b99b13f8 Mon Sep 17 00:00:00 2001 From: Tim O'Shea Date: Thu, 10 Jan 2013 11:39:05 -0500 Subject: core: adding gr_random_pdu message passing block --- gnuradio-core/src/lib/general/CMakeLists.txt | 1 + gnuradio-core/src/lib/general/general.i | 2 ++ 2 files changed, 3 insertions(+) (limited to 'gnuradio-core/src/lib/general') diff --git a/gnuradio-core/src/lib/general/CMakeLists.txt b/gnuradio-core/src/lib/general/CMakeLists.txt index 4c99acfc3..ce9a80f37 100644 --- a/gnuradio-core/src/lib/general/CMakeLists.txt +++ b/gnuradio-core/src/lib/general/CMakeLists.txt @@ -259,6 +259,7 @@ set(gr_core_general_triple_threats gr_pwr_squelch_cc gr_pwr_squelch_ff gr_quadrature_demod_cf + gr_random_pdu gr_rail_ff gr_regenerate_bb gr_remez diff --git a/gnuradio-core/src/lib/general/general.i b/gnuradio-core/src/lib/general/general.i index 1446088a2..b727bc8a6 100644 --- a/gnuradio-core/src/lib/general/general.i +++ b/gnuradio-core/src/lib/general/general.i @@ -70,6 +70,7 @@ #include #include #include +#include #include #include #include @@ -194,6 +195,7 @@ %include "gr_agc_cc.i" %include "gr_agc2_ff.i" %include "gr_agc2_cc.i" +%include "gr_random_pdu.i" %include "gr_rms_cf.i" %include "gr_rms_ff.i" %include "gr_nlog10_ff.i" -- cgit From 8827ecc87eca9a1e970dd2ba5da0731cd9db0e98 Mon Sep 17 00:00:00 2001 From: Tom Rondeau Date: Sat, 16 Feb 2013 14:39:28 -0500 Subject: core: Making an all C++ version of the preferences/config file readers. This replaces the Python config parser stuff; should have same functionality that we need. --- gnuradio-core/src/lib/general/gr_prefs.cc | 128 ++++++++++++++++++++++++++++-- gnuradio-core/src/lib/general/gr_prefs.h | 10 +++ gnuradio-core/src/lib/general/gr_prefs.i | 3 - 3 files changed, 132 insertions(+), 9 deletions(-) (limited to 'gnuradio-core/src/lib/general') diff --git a/gnuradio-core/src/lib/general/gr_prefs.cc b/gnuradio-core/src/lib/general/gr_prefs.cc index 20aead8e3..40601f7c4 100644 --- a/gnuradio-core/src/lib/general/gr_prefs.cc +++ b/gnuradio-core/src/lib/general/gr_prefs.cc @@ -25,6 +25,13 @@ #endif #include +#include +#include + +#include +#include +#include +namespace fs = boost::filesystem; /* * Stub implementations @@ -45,44 +52,153 @@ gr_prefs::set_singleton(gr_prefs *p) s_singleton = p; } +gr_prefs::gr_prefs() +{ + _read_files(); +} + gr_prefs::~gr_prefs() { // nop } +std::vector +gr_prefs::_sys_prefs_filenames() +{ + fs::path dir = gr_prefsdir(); + if(!fs::is_directory(dir)) + std::runtime_error("gr_prefs: preference path does not exist.\n"); + + std::vector fnames; + fs::directory_iterator diritr(dir); + while(diritr != fs::directory_iterator()) { + fs::path p = *diritr++; + fnames.push_back(p.string()); + } + std::sort(fnames.begin(), fnames.end()); + return fnames; +} + +void +gr_prefs::_read_files() +{ + std::vector filenames = _sys_prefs_filenames(); + std::vector::iterator sitr; + char tmp[1024]; + for(sitr = filenames.begin(); sitr != filenames.end(); sitr++) { + fs::ifstream fin(*sitr); + while(!fin.eof()) { + fin.getline(tmp, 1024); + std::string t(tmp); + // ignore empty lines or lines of just comments + if((t.size() > 0) && (t[0] != '#')) { + // remove any comments in the line + size_t hash = t.find("#"); + + // Use hash marks at the end of each segment as a delimiter + d_configs += t.substr(0, hash) + '#'; + } + } + fin.close(); + } + + // Remove all whitespace + d_configs.erase(std::remove_if(d_configs.begin(), d_configs.end(), ::isspace), d_configs.end()); +} + bool gr_prefs::has_section(const std::string section) { - return false; + size_t t = d_configs.find("[" + section + "]#"); + return t != std::string::npos; } bool gr_prefs::has_option(const std::string section, const std::string option) { - return false; + if(has_section(section)) { + size_t sec = d_configs.find("[" + section + "]#"); + size_t opt = d_configs.find("#" + option + "=", sec); + return opt != std::string::npos; + } + else { + return false; + } } const std::string gr_prefs::get_string(const std::string section, const std::string option, const std::string default_val) { - return default_val; + if(has_option(section, option)) { + std::string optname = "#" + option + "="; + size_t sec = d_configs.find("[" + section + "]#"); + size_t opt = d_configs.find(optname, sec); + + size_t start = opt + optname.size(); + size_t end = d_configs.find("#", start); + size_t len = end - start; + + return d_configs.substr(start, len); + } + else { + return default_val; + } } bool gr_prefs::get_bool(const std::string section, const std::string option, bool default_val) { - return default_val; + if(has_option(section, option)) { + std::string str = get_string(section, option, ""); + if(str == "") { + return default_val; + } + std::transform(str.begin(), str.end(), str.begin(), ::tolower); + if(str == "true") + return true; + else if(str == "false") + return false; + else + return default_val; + } + else { + return default_val; + } } long gr_prefs::get_long(const std::string section, const std::string option, long default_val) { - return default_val; + if(has_option(section, option)) { + std::string str = get_string(section, option, ""); + if(str == "") { + return default_val; + } + std::stringstream sstr(str); + long n; + sstr >> n; + return n; + } + else { + return default_val; + } } double gr_prefs::get_double(const std::string section, const std::string option, double default_val) { - return default_val; + if(has_option(section, option)) { + std::string str = get_string(section, option, ""); + if(str == "") { + return default_val; + } + std::stringstream sstr(str); + double n; + sstr >> n; + return n; + } + else { + return default_val; + } } diff --git a/gnuradio-core/src/lib/general/gr_prefs.h b/gnuradio-core/src/lib/general/gr_prefs.h index b1c354bd3..90d602741 100644 --- a/gnuradio-core/src/lib/general/gr_prefs.h +++ b/gnuradio-core/src/lib/general/gr_prefs.h @@ -24,6 +24,7 @@ #include #include +#include /*! * \brief Base class for representing user preferences a la windows INI files. @@ -39,6 +40,7 @@ public: static gr_prefs *singleton(); static void set_singleton(gr_prefs *p); + gr_prefs(); virtual ~gr_prefs(); /*! @@ -78,6 +80,14 @@ public: virtual double get_double(const std::string section, const std::string option, double default_val); + + protected: + virtual std::vector _sys_prefs_filenames(); + virtual void _read_files(); + + private: + gruel::mutex d_mutex; + std::string d_configs; }; diff --git a/gnuradio-core/src/lib/general/gr_prefs.i b/gnuradio-core/src/lib/general/gr_prefs.i index f44dcc944..cfb4cdb4e 100644 --- a/gnuradio-core/src/lib/general/gr_prefs.i +++ b/gnuradio-core/src/lib/general/gr_prefs.i @@ -20,9 +20,6 @@ * Boston, MA 02110-1301, USA. */ -// Generate SWIG directors for gr_prefs. -%feature("director") gr_prefs; - class gr_prefs { public: -- cgit From 4f2481e1310dd9e936ed110d60679e71ca2059a8 Mon Sep 17 00:00:00 2001 From: Tom Rondeau Date: Sat, 16 Feb 2013 15:42:48 -0500 Subject: core: add ability to override config files with environmental variables. Env variables are named GR_CONF_
_