GNU Radio 3.7.0 C++ API
block_gateway.h
Go to the documentation of this file.
00001 /* -*- c++ -*- */
00002 /*
00003  * Copyright 2011-2013 Free Software Foundation, Inc.
00004  *
00005  * This file is part of GNU Radio
00006  *
00007  * GNU Radio is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 3, or (at your option)
00010  * any later version.
00011  *
00012  * GNU Radio is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with GNU Radio; see the file COPYING.  If not, write to
00019  * the Free Software Foundation, Inc., 51 Franklin Street,
00020  * Boston, MA 02110-1301, USA.
00021  */
00022 
00023 #ifndef INCLUDED_RUNTIME_BLOCK_GATEWAY_H
00024 #define INCLUDED_RUNTIME_BLOCK_GATEWAY_H
00025 
00026 #include <gnuradio/api.h>
00027 #include <gnuradio/block.h>
00028 #include <gnuradio/feval.h>
00029 
00030 namespace gr {
00031   
00032   /*!
00033    * The work type enum tells the gateway what kind of block to
00034    * implement.  The choices are familiar gnuradio block overloads
00035    * (sync, decim, interp).
00036    */
00037   enum block_gw_work_type {
00038     GR_BLOCK_GW_WORK_GENERAL,
00039     GR_BLOCK_GW_WORK_SYNC,
00040     GR_BLOCK_GW_WORK_DECIM,
00041     GR_BLOCK_GW_WORK_INTERP,
00042   };
00043 
00044   /*!
00045    * Shared message structure between python and gateway.
00046    * Each action type represents a scheduler-called function.
00047    */
00048   struct block_gw_message_type {
00049     enum action_type {
00050       ACTION_GENERAL_WORK, //dispatch work
00051       ACTION_WORK, //dispatch work
00052       ACTION_FORECAST, //dispatch forecast
00053       ACTION_START, //dispatch start
00054       ACTION_STOP, //dispatch stop
00055     };
00056 
00057     action_type action;
00058 
00059     int general_work_args_noutput_items;
00060     std::vector<int> general_work_args_ninput_items;
00061     std::vector<void *> general_work_args_input_items; //TODO this should be const void*, but swig cant int cast it right
00062     std::vector<void *> general_work_args_output_items;
00063     int general_work_args_return_value;
00064 
00065     int work_args_ninput_items;
00066     int work_args_noutput_items;
00067     std::vector<void *> work_args_input_items; //TODO this should be const void*, but swig cant int cast it right
00068     std::vector<void *> work_args_output_items;
00069     int work_args_return_value;
00070 
00071     int forecast_args_noutput_items;
00072     std::vector<int> forecast_args_ninput_items_required;
00073 
00074     bool start_args_return_value;
00075 
00076     bool stop_args_return_value;
00077   };
00078 
00079   /*!
00080    * The gateway block which performs all the magic.
00081    *
00082    * The gateway provides access to all the gr::block routines.
00083    * The methods prefixed with gr::block__ are renamed
00084    * to class methods without the prefix in python.
00085    */
00086   class GR_RUNTIME_API block_gateway : virtual public gr::block
00087   {
00088   public:
00089     // gr::block_gateway::sptr
00090     typedef boost::shared_ptr<block_gateway> sptr;
00091     
00092     /*!
00093      * Make a new gateway block.
00094      * \param handler the swig director object with callback
00095      * \param name the name of the block (Ex: "Shirley")
00096      * \param in_sig the input signature for this block
00097      * \param out_sig the output signature for this block
00098      * \param work_type the type of block overload to implement
00099      * \param factor the decimation or interpolation factor
00100      * \return a new gateway block
00101      */
00102     static sptr make(gr::feval_ll *handler,
00103                      const std::string &name,
00104                      gr::io_signature::sptr in_sig,
00105                      gr::io_signature::sptr out_sig,
00106                      const block_gw_work_type work_type,
00107                      const unsigned factor);
00108 
00109     //! Provide access to the shared message object
00110     virtual block_gw_message_type &block_message(void) = 0;
00111 
00112     long block__unique_id(void) const {
00113       return gr::block::unique_id();
00114     }
00115 
00116     std::string block__name(void) const {
00117       return gr::block::name();
00118     }
00119 
00120     unsigned block__history(void) const {
00121       return gr::block::history();
00122     }
00123 
00124     void block__set_history(unsigned history) {
00125       return gr::block::set_history(history);
00126     }
00127 
00128     void block__set_fixed_rate(bool fixed_rate) {
00129       return gr::block::set_fixed_rate(fixed_rate);
00130     }
00131 
00132     bool block__fixed_rate(void) const {
00133       return gr::block::fixed_rate();
00134     }
00135 
00136     void block__set_output_multiple(int multiple) {
00137       return gr::block::set_output_multiple(multiple);
00138     }
00139 
00140     int block__output_multiple(void) const {
00141       return gr::block::output_multiple();
00142     }
00143 
00144     void block__consume(int which_input, int how_many_items) {
00145       return gr::block::consume(which_input, how_many_items);
00146     }
00147 
00148     void block__consume_each(int how_many_items) {
00149       return gr::block::consume_each(how_many_items);
00150     }
00151 
00152     void block__produce(int which_output, int how_many_items) {
00153       return gr::block::produce(which_output, how_many_items);
00154     }
00155 
00156     void block__set_relative_rate(double relative_rate) {
00157       return gr::block::set_relative_rate(relative_rate);
00158     }
00159 
00160     double block__relative_rate(void) const {
00161       return gr::block::relative_rate();
00162     }
00163 
00164     uint64_t block__nitems_read(unsigned int which_input) {
00165       return gr::block::nitems_read(which_input);
00166     }
00167 
00168     uint64_t block__nitems_written(unsigned int which_output) {
00169       return gr::block::nitems_written(which_output);
00170     }
00171 
00172     block::tag_propagation_policy_t block__tag_propagation_policy(void) {
00173       return gr::block::tag_propagation_policy();
00174     }
00175 
00176     void block__set_tag_propagation_policy(block::tag_propagation_policy_t p) {
00177       return gr::block::set_tag_propagation_policy(p);
00178     }
00179 
00180     void block__add_item_tag(unsigned int which_output,
00181                              const tag_t &tag)
00182     {
00183       return gr::block::add_item_tag(which_output, tag);
00184     }
00185 
00186     void block__add_item_tag(unsigned int which_output,
00187                              uint64_t abs_offset,
00188                              const pmt::pmt_t &key,
00189                              const pmt::pmt_t &value,
00190                              const pmt::pmt_t &srcid=pmt::PMT_F)
00191     {
00192       return gr::block::add_item_tag(which_output, abs_offset,
00193                                      key, value, srcid);
00194     }
00195 
00196     std::vector<tag_t> block__get_tags_in_range(unsigned int which_input,
00197                                                 uint64_t abs_start,
00198                                                 uint64_t abs_end)
00199     {
00200       std::vector<gr::tag_t> tags;
00201       gr::block::get_tags_in_range(tags, which_input, abs_start, abs_end);
00202       return tags;
00203     }
00204 
00205     std::vector<tag_t> block__get_tags_in_range(unsigned int which_input,
00206                                                 uint64_t abs_start,
00207                                                 uint64_t abs_end,
00208                                                 const pmt::pmt_t &key)
00209     {
00210       std::vector<gr::tag_t> tags;
00211       gr::block::get_tags_in_range(tags, which_input, abs_start, abs_end, key);
00212       return tags;
00213     }
00214 
00215     /* Message passing interface */
00216     void block__message_port_register_in(pmt::pmt_t port_id) {
00217       gr::basic_block::message_port_register_in(port_id);
00218     }
00219 
00220     void block__message_port_register_out(pmt::pmt_t port_id) {
00221       gr::basic_block::message_port_register_out(port_id);
00222     }
00223 
00224     void block__message_port_pub(pmt::pmt_t port_id, pmt::pmt_t msg) {
00225       gr::basic_block::message_port_pub(port_id, msg);
00226     }
00227 
00228     void block__message_port_sub(pmt::pmt_t port_id, pmt::pmt_t target) {
00229       gr::basic_block::message_port_sub(port_id, target);
00230     }
00231 
00232     void block__message_port_unsub(pmt::pmt_t port_id, pmt::pmt_t target) {
00233       gr::basic_block::message_port_unsub(port_id, target);
00234     }
00235     
00236     pmt::pmt_t block__message_ports_in() {
00237       return gr::basic_block::message_ports_in();
00238     }
00239 
00240     pmt::pmt_t block__message_ports_out() {
00241       return gr::basic_block::message_ports_out();
00242     }
00243 
00244     void set_msg_handler_feval(pmt::pmt_t which_port, gr::feval_p *msg_handler)
00245     {
00246       if(msg_queue.find(which_port) == msg_queue.end()) {
00247         throw std::runtime_error("attempt to set_msg_handler_feval() on bad input message port!"); 
00248       }
00249       d_msg_handlers_feval[which_port] = msg_handler;
00250     }
00251 
00252   protected:
00253     typedef std::map<pmt::pmt_t, feval_p *, pmt::comperator> msg_handlers_feval_t;
00254     msg_handlers_feval_t d_msg_handlers_feval;
00255 
00256     void dispatch_msg(pmt::pmt_t which_port, pmt::pmt_t msg)
00257     {
00258       // Is there a handler?
00259       if(d_msg_handlers_feval.find(which_port) != d_msg_handlers_feval.end()) {
00260         d_msg_handlers_feval[which_port]->calleval(msg); // Yes, invoke it.
00261       }
00262       else {
00263         // Pass to generic dispatcher if not found
00264         gr::basic_block::dispatch_msg(which_port, msg);
00265       }
00266     }
00267   };
00268 
00269 } /* namespace gr */
00270 
00271 #endif /* INCLUDED_RUNTIME_BLOCK_GATEWAY_H */