GNU Radio 3.6.5 C++ API

gr_block_gateway.h

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