GNU Radio 3.7.1 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   enum tag_propagation_policy_t {
00045     TPP_DONT = 0,
00046     TPP_ALL_TO_ALL = 1,
00047     TPP_ONE_TO_ONE = 2
00048   };
00049 
00050   /*!
00051    * Shared message structure between python and gateway.
00052    * Each action type represents a scheduler-called function.
00053    */
00054   struct block_gw_message_type {
00055     enum action_type {
00056       ACTION_GENERAL_WORK, //dispatch work
00057       ACTION_WORK, //dispatch work
00058       ACTION_FORECAST, //dispatch forecast
00059       ACTION_START, //dispatch start
00060       ACTION_STOP, //dispatch stop
00061     };
00062 
00063     action_type action;
00064 
00065     int general_work_args_noutput_items;
00066     std::vector<int> general_work_args_ninput_items;
00067     std::vector<void *> general_work_args_input_items; //TODO this should be const void*, but swig cant int cast it right
00068     std::vector<void *> general_work_args_output_items;
00069     int general_work_args_return_value;
00070 
00071     int work_args_ninput_items;
00072     int work_args_noutput_items;
00073     std::vector<void *> work_args_input_items; //TODO this should be const void*, but swig cant int cast it right
00074     std::vector<void *> work_args_output_items;
00075     int work_args_return_value;
00076 
00077     int forecast_args_noutput_items;
00078     std::vector<int> forecast_args_ninput_items_required;
00079 
00080     bool start_args_return_value;
00081 
00082     bool stop_args_return_value;
00083   };
00084 
00085   /*!
00086    * The gateway block which performs all the magic.
00087    *
00088    * The gateway provides access to all the gr::block routines.
00089    * The methods prefixed with gr::block__ are renamed
00090    * to class methods without the prefix in python.
00091    */
00092   class GR_RUNTIME_API block_gateway : virtual public gr::block
00093   {
00094   public:
00095     // gr::block_gateway::sptr
00096     typedef boost::shared_ptr<block_gateway> sptr;
00097     
00098     /*!
00099      * Make a new gateway block.
00100      * \param handler the swig director object with callback
00101      * \param name the name of the block (Ex: "Shirley")
00102      * \param in_sig the input signature for this block
00103      * \param out_sig the output signature for this block
00104      * \param work_type the type of block overload to implement
00105      * \param factor the decimation or interpolation factor
00106      * \return a new gateway block
00107      */
00108     static sptr make(gr::feval_ll *handler,
00109                      const std::string &name,
00110                      gr::io_signature::sptr in_sig,
00111                      gr::io_signature::sptr out_sig,
00112                      const block_gw_work_type work_type,
00113                      const unsigned factor);
00114 
00115     //! Provide access to the shared message object
00116     virtual block_gw_message_type &block_message(void) = 0;
00117 
00118     long block__unique_id(void) const {
00119       return gr::block::unique_id();
00120     }
00121 
00122     std::string block__name(void) const {
00123       return gr::block::name();
00124     }
00125 
00126     unsigned block__history(void) const {
00127       return gr::block::history();
00128     }
00129 
00130     void block__set_history(unsigned history) {
00131       return gr::block::set_history(history);
00132     }
00133 
00134     void block__set_fixed_rate(bool fixed_rate) {
00135       return gr::block::set_fixed_rate(fixed_rate);
00136     }
00137 
00138     bool block__fixed_rate(void) const {
00139       return gr::block::fixed_rate();
00140     }
00141 
00142     void block__set_output_multiple(int multiple) {
00143       return gr::block::set_output_multiple(multiple);
00144     }
00145 
00146     int block__output_multiple(void) const {
00147       return gr::block::output_multiple();
00148     }
00149 
00150     void block__consume(int which_input, int how_many_items) {
00151       return gr::block::consume(which_input, how_many_items);
00152     }
00153 
00154     void block__consume_each(int how_many_items) {
00155       return gr::block::consume_each(how_many_items);
00156     }
00157 
00158     void block__produce(int which_output, int how_many_items) {
00159       return gr::block::produce(which_output, how_many_items);
00160     }
00161 
00162     void block__set_relative_rate(double relative_rate) {
00163       return gr::block::set_relative_rate(relative_rate);
00164     }
00165 
00166     double block__relative_rate(void) const {
00167       return gr::block::relative_rate();
00168     }
00169 
00170     uint64_t block__nitems_read(unsigned int which_input) {
00171       return gr::block::nitems_read(which_input);
00172     }
00173 
00174     uint64_t block__nitems_written(unsigned int which_output) {
00175       return gr::block::nitems_written(which_output);
00176     }
00177 
00178     block::tag_propagation_policy_t block__tag_propagation_policy(void) {
00179       return gr::block::tag_propagation_policy();
00180     }
00181 
00182     void block__set_tag_propagation_policy(block::tag_propagation_policy_t p) {
00183       return gr::block::set_tag_propagation_policy(p);
00184     }
00185 
00186     void block__add_item_tag(unsigned int which_output,
00187                              const tag_t &tag)
00188     {
00189       return gr::block::add_item_tag(which_output, tag);
00190     }
00191 
00192     void block__add_item_tag(unsigned int which_output,
00193                              uint64_t abs_offset,
00194                              const pmt::pmt_t &key,
00195                              const pmt::pmt_t &value,
00196                              const pmt::pmt_t &srcid=pmt::PMT_F)
00197     {
00198       return gr::block::add_item_tag(which_output, abs_offset,
00199                                      key, value, srcid);
00200     }
00201 
00202     std::vector<tag_t> block__get_tags_in_range(unsigned int which_input,
00203                                                 uint64_t abs_start,
00204                                                 uint64_t abs_end)
00205     {
00206       std::vector<gr::tag_t> tags;
00207       gr::block::get_tags_in_range(tags, which_input, abs_start, abs_end);
00208       return tags;
00209     }
00210 
00211     std::vector<tag_t> block__get_tags_in_range(unsigned int which_input,
00212                                                 uint64_t abs_start,
00213                                                 uint64_t abs_end,
00214                                                 const pmt::pmt_t &key)
00215     {
00216       std::vector<gr::tag_t> tags;
00217       gr::block::get_tags_in_range(tags, which_input, abs_start, abs_end, key);
00218       return tags;
00219     }
00220 
00221     /* Message passing interface */
00222     void block__message_port_register_in(pmt::pmt_t port_id) {
00223       gr::basic_block::message_port_register_in(port_id);
00224     }
00225 
00226     void block__message_port_register_out(pmt::pmt_t port_id) {
00227       gr::basic_block::message_port_register_out(port_id);
00228     }
00229 
00230     void block__message_port_pub(pmt::pmt_t port_id, pmt::pmt_t msg) {
00231       gr::basic_block::message_port_pub(port_id, msg);
00232     }
00233 
00234     void block__message_port_sub(pmt::pmt_t port_id, pmt::pmt_t target) {
00235       gr::basic_block::message_port_sub(port_id, target);
00236     }
00237 
00238     void block__message_port_unsub(pmt::pmt_t port_id, pmt::pmt_t target) {
00239       gr::basic_block::message_port_unsub(port_id, target);
00240     }
00241     
00242     pmt::pmt_t block__message_ports_in() {
00243       return gr::basic_block::message_ports_in();
00244     }
00245 
00246     pmt::pmt_t block__message_ports_out() {
00247       return gr::basic_block::message_ports_out();
00248     }
00249 
00250     void set_msg_handler_feval(pmt::pmt_t which_port, gr::feval_p *msg_handler)
00251     {
00252       if(msg_queue.find(which_port) == msg_queue.end()) {
00253         throw std::runtime_error("attempt to set_msg_handler_feval() on bad input message port!"); 
00254       }
00255       d_msg_handlers_feval[which_port] = msg_handler;
00256     }
00257 
00258   protected:
00259     typedef std::map<pmt::pmt_t, feval_p *, pmt::comperator> msg_handlers_feval_t;
00260     msg_handlers_feval_t d_msg_handlers_feval;
00261 
00262     bool has_msg_handler(pmt::pmt_t which_port)
00263     {
00264       return (d_msg_handlers_feval.find(which_port) != d_msg_handlers_feval.end());
00265     }
00266     
00267     void dispatch_msg(pmt::pmt_t which_port, pmt::pmt_t msg)
00268     {
00269       // Is there a handler?
00270       if(d_msg_handlers_feval.find(which_port) != d_msg_handlers_feval.end()) {
00271         d_msg_handlers_feval[which_port]->calleval(msg); // Yes, invoke it.
00272       }
00273       else {
00274         // Pass to generic dispatcher if not found
00275         gr::basic_block::dispatch_msg(which_port, msg);
00276       }
00277     }
00278   };
00279 
00280 } /* namespace gr */
00281 
00282 #endif /* INCLUDED_RUNTIME_BLOCK_GATEWAY_H */