GNU Radio v3.7.x-xxx-xunknown C++ API
block_gateway.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2011-2013 Free Software Foundation, Inc.
4  *
5  * This file is part of GNU Radio
6  *
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  *
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING. If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22 
23 #ifndef INCLUDED_RUNTIME_BLOCK_GATEWAY_H
24 #define INCLUDED_RUNTIME_BLOCK_GATEWAY_H
25 
26 #include <gnuradio/api.h>
27 #include <gnuradio/block.h>
28 #include <gnuradio/feval.h>
29 
30 namespace gr {
31 
32  /*!
33  * The work type enum tells the gateway what kind of block to
34  * implement. The choices are familiar gnuradio block overloads
35  * (sync, decim, interp).
36  */
42  };
43 
44  /*!
45  * Shared message structure between python and gateway.
46  * Each action type represents a scheduler-called function.
47  */
49  enum action_type {
50  ACTION_GENERAL_WORK, //dispatch work
51  ACTION_WORK, //dispatch work
52  ACTION_FORECAST, //dispatch forecast
53  ACTION_START, //dispatch start
54  ACTION_STOP, //dispatch stop
55  };
56 
58 
61  std::vector<void *> general_work_args_input_items; //TODO this should be const void*, but swig cant int cast it right
62  std::vector<void *> general_work_args_output_items;
64 
67  std::vector<void *> work_args_input_items; //TODO this should be const void*, but swig cant int cast it right
68  std::vector<void *> work_args_output_items;
70 
73 
75 
77  };
78 
79  /*!
80  * The gateway block which performs all the magic.
81  *
82  * The gateway provides access to all the gr::block routines.
83  * The methods prefixed with gr::block__ are renamed
84  * to class methods without the prefix in python.
85  */
86  class GR_RUNTIME_API block_gateway : virtual public gr::block
87  {
88  public:
89  // gr::block_gateway::sptr
91 
92  /*!
93  * Make a new gateway block.
94  * \param handler the swig director object with callback
95  * \param name the name of the block (Ex: "Shirley")
96  * \param in_sig the input signature for this block
97  * \param out_sig the output signature for this block
98  * \param work_type the type of block overload to implement
99  * \param factor the decimation or interpolation factor
100  * \return a new gateway block
101  */
102  static sptr make(gr::feval_ll *handler,
103  const std::string &name,
104  gr::io_signature::sptr in_sig,
105  gr::io_signature::sptr out_sig,
106  const block_gw_work_type work_type,
107  const unsigned factor);
108 
109  //! Provide access to the shared message object
110  virtual block_gw_message_type &block_message(void) = 0;
111 
112  long block__unique_id(void) const {
113  return gr::block::unique_id();
114  }
115 
116  std::string block__name(void) const {
117  return gr::block::name();
118  }
119 
120  unsigned block__history(void) const {
121  return gr::block::history();
122  }
123 
124  void block__set_history(unsigned history) {
125  return gr::block::set_history(history);
126  }
127 
128  void block__set_fixed_rate(bool fixed_rate) {
129  return gr::block::set_fixed_rate(fixed_rate);
130  }
131 
132  bool block__fixed_rate(void) const {
133  return gr::block::fixed_rate();
134  }
135 
136  void block__set_output_multiple(int multiple) {
137  return gr::block::set_output_multiple(multiple);
138  }
139 
140  int block__output_multiple(void) const {
142  }
143 
144  void block__consume(int which_input, int how_many_items) {
145  return gr::block::consume(which_input, how_many_items);
146  }
147 
148  void block__consume_each(int how_many_items) {
149  return gr::block::consume_each(how_many_items);
150  }
151 
152  void block__produce(int which_output, int how_many_items) {
153  return gr::block::produce(which_output, how_many_items);
154  }
155 
156  void block__set_relative_rate(double relative_rate) {
157  return gr::block::set_relative_rate(relative_rate);
158  }
159 
160  double block__relative_rate(void) const {
161  return gr::block::relative_rate();
162  }
163 
164  uint64_t block__nitems_read(unsigned int which_input) {
165  return gr::block::nitems_read(which_input);
166  }
167 
168  uint64_t block__nitems_written(unsigned int which_output) {
169  return gr::block::nitems_written(which_output);
170  }
171 
174  }
175 
178  }
179 
180  void block__add_item_tag(unsigned int which_output,
181  const tag_t &tag)
182  {
183  return gr::block::add_item_tag(which_output, tag);
184  }
185 
186  void block__add_item_tag(unsigned int which_output,
187  uint64_t abs_offset,
188  const pmt::pmt_t &key,
189  const pmt::pmt_t &value,
190  const pmt::pmt_t &srcid=pmt::PMT_F)
191  {
192  return gr::block::add_item_tag(which_output, abs_offset,
193  key, value, srcid);
194  }
195 
196  std::vector<tag_t> block__get_tags_in_range(unsigned int which_input,
197  uint64_t abs_start,
198  uint64_t abs_end)
199  {
200  std::vector<gr::tag_t> tags;
201  gr::block::get_tags_in_range(tags, which_input, abs_start, abs_end);
202  return tags;
203  }
204 
205  std::vector<tag_t> block__get_tags_in_range(unsigned int which_input,
206  uint64_t abs_start,
207  uint64_t abs_end,
208  const pmt::pmt_t &key)
209  {
210  std::vector<gr::tag_t> tags;
211  gr::block::get_tags_in_range(tags, which_input, abs_start, abs_end, key);
212  return tags;
213  }
214 
215  /* Message passing interface */
218  }
219 
222  }
223 
225  gr::basic_block::message_port_pub(port_id, msg);
226  }
227 
229  gr::basic_block::message_port_sub(port_id, target);
230  }
231 
233  gr::basic_block::message_port_unsub(port_id, target);
234  }
235 
238  }
239 
242  }
243 
245  {
246  if(msg_queue.find(which_port) == msg_queue.end()) {
247  throw std::runtime_error("attempt to set_msg_handler_feval() on bad input message port!");
248  }
249  d_msg_handlers_feval[which_port] = msg_handler;
250  }
251 
252  protected:
253  typedef std::map<pmt::pmt_t, feval_p *, pmt::comperator> msg_handlers_feval_t;
255 
256  bool has_msg_handler(pmt::pmt_t which_port)
257  {
258  return (d_msg_handlers_feval.find(which_port) != d_msg_handlers_feval.end());
259  }
260 
261  void dispatch_msg(pmt::pmt_t which_port, pmt::pmt_t msg)
262  {
263  // Is there a handler?
264  if(d_msg_handlers_feval.find(which_port) != d_msg_handlers_feval.end()) {
265  d_msg_handlers_feval[which_port]->calleval(msg); // Yes, invoke it.
266  }
267  else {
268  // Pass to generic dispatcher if not found
269  gr::basic_block::dispatch_msg(which_port, msg);
270  }
271  }
272  };
273 
274 } /* namespace gr */
275 
276 #endif /* INCLUDED_RUNTIME_BLOCK_GATEWAY_H */
int general_work_args_return_value
Definition: block_gateway.h:63
void produce(int which_output, int how_many_items)
Tell the scheduler how_many_items were produced on output stream which_output.
Definition: tags.h:31
void dispatch_msg(pmt::pmt_t which_port, pmt::pmt_t msg)
Definition: block_gateway.h:261
std::map< pmt::pmt_t, feval_p *, pmt::comperator > msg_handlers_feval_t
Definition: block_gateway.h:253
bool stop_args_return_value
Definition: block_gateway.h:76
block::tag_propagation_policy_t block__tag_propagation_policy(void)
Definition: block_gateway.h:172
action_type action
Definition: block_gateway.h:57
Definition: block_gateway.h:54
Definition: block_gateway.h:41
pmt::pmt_t message_ports_out()
Get output message port names.
unsigned block__history(void) const
Definition: block_gateway.h:120
void block__set_tag_propagation_policy(block::tag_propagation_policy_t p)
Definition: block_gateway.h:176
void set_fixed_rate(bool fixed_rate)
Definition: block.h:547
void add_item_tag(unsigned int which_output, uint64_t abs_offset, const pmt::pmt_t &key, const pmt::pmt_t &value, const pmt::pmt_t &srcid=pmt::PMT_F)
Adds a new tag onto the given output buffer.
Definition: block.h:559
Definition: block_gateway.h:53
bool block__fixed_rate(void) const
Definition: block_gateway.h:132
void set_relative_rate(double relative_rate)
Set the approximate output rate / input rate.
void set_history(unsigned history)
Definition: block.h:86
base class for evaluating a function: long -&gt; longThis class is designed to be subclassed in Python or...
Definition: feval.h:104
tag_propagation_policy_t tag_propagation_policy()
Asks for the policy used by the scheduler to moved tags downstream.
long block__unique_id(void) const
Definition: block_gateway.h:112
block_gw_work_type
Definition: block_gateway.h:37
void message_port_pub(pmt::pmt_t port_id, pmt::pmt_t msg)
uint64_t block__nitems_read(unsigned int which_input)
Definition: block_gateway.h:164
Definition: block_gateway.h:38
std::vector< void * > work_args_input_items
Definition: block_gateway.h:67
std::vector< int > forecast_args_ninput_items_required
Definition: block_gateway.h:72
void block__set_output_multiple(int multiple)
Definition: block_gateway.h:136
std::vector< tag_t > block__get_tags_in_range(unsigned int which_input, uint64_t abs_start, uint64_t abs_end)
Definition: block_gateway.h:196
bool has_msg_handler(pmt::pmt_t which_port)
Tests if there is a handler attached to port which_port.
Definition: block_gateway.h:256
std::string name() const
Definition: basic_block.h:146
int general_work_args_noutput_items
Definition: block_gateway.h:59
void message_port_unsub(pmt::pmt_t port_id, pmt::pmt_t target)
#define GR_RUNTIME_API
Definition: gnuradio-runtime/include/gnuradio/api.h:30
void set_tag_propagation_policy(tag_propagation_policy_t p)
Set the policy by the scheduler to determine how tags are moved downstream.
boost::shared_ptr< block_gateway > sptr
Definition: block_gateway.h:90
Definition: block_gateway.h:39
shared_ptr documentation stub
Definition: shared_ptr_docstub.h:15
uint64_t nitems_read(unsigned int which_input)
Return the number of items read on input stream which_input.
Definition: block_gateway.h:48
uint64_t block__nitems_written(unsigned int which_output)
Definition: block_gateway.h:168
void block__produce(int which_output, int how_many_items)
Definition: block_gateway.h:152
double block__relative_rate(void) const
Definition: block_gateway.h:160
msg_handlers_feval_t d_msg_handlers_feval
Definition: block_gateway.h:254
void message_port_register_in(pmt::pmt_t port_id)
long unique_id() const
Definition: basic_block.h:144
uint64_t nitems_written(unsigned int which_output)
Return the number of items written on output stream which_output.
thread-safe message queue
Definition: msg_queue.h:36
void block__consume_each(int how_many_items)
Definition: block_gateway.h:148
int work_args_return_value
Definition: block_gateway.h:69
void block__set_fixed_rate(bool fixed_rate)
Definition: block_gateway.h:128
void consume(int which_input, int how_many_items)
Tell the scheduler how_many_items of input stream which_input were consumed.
void block__add_item_tag(unsigned int which_output, uint64_t abs_offset, const pmt::pmt_t &key, const pmt::pmt_t &value, const pmt::pmt_t &srcid=pmt::PMT_F)
Definition: block_gateway.h:186
unsigned __int64 uint64_t
Definition: stdint.h:90
Definition: block_gateway.h:40
bool fixed_rate() const
Return true if this block has a fixed input to output rate.
Definition: block.h:93
void set_output_multiple(int multiple)
Constrain the noutput_items argument passed to forecast and general_work.
int forecast_args_noutput_items
Definition: block_gateway.h:71
int block__output_multiple(void) const
Definition: block_gateway.h:140
void block__consume(int which_input, int how_many_items)
Definition: block_gateway.h:144
void block__message_port_unsub(pmt::pmt_t port_id, pmt::pmt_t target)
Definition: block_gateway.h:232
std::vector< void * > general_work_args_output_items
Definition: block_gateway.h:62
Definition: block_gateway.h:52
tag_propagation_policy_t
Definition: block.h:70
void message_port_sub(pmt::pmt_t port_id, pmt::pmt_t target)
PMT_API const pmt_t PMT_F
std::vector< void * > general_work_args_input_items
Definition: block_gateway.h:61
Definition: block_gateway.h:51
int work_args_ninput_items
Definition: block_gateway.h:65
std::vector< int > general_work_args_ninput_items
Definition: block_gateway.h:60
pmt::pmt_t block__message_ports_out()
Definition: block_gateway.h:240
VOLK_API $kern pname $kern name
A function pointer to the dispatcher implementation.
pmt::pmt_t message_ports_in()
Get input message port names.
int work_args_noutput_items
Definition: block_gateway.h:66
unsigned history() const
Definition: block.h:85
base class for evaluating a function: pmt -&gt; voidThis class is designed to be subclassed in Python or ...
Definition: feval.h:162
void set_msg_handler_feval(pmt::pmt_t which_port, gr::feval_p *msg_handler)
Definition: block_gateway.h:244
pmt::pmt_t block__message_ports_in()
Definition: block_gateway.h:236
void block__message_port_register_out(pmt::pmt_t port_id)
Definition: block_gateway.h:220
void block__add_item_tag(unsigned int which_output, const tag_t &tag)
Definition: block_gateway.h:180
action_type
Definition: block_gateway.h:49
double relative_rate() const
return the approximate output rate / input rate
Definition: block.h:220
void get_tags_in_range(std::vector< tag_t > &v, unsigned int which_input, uint64_t abs_start, uint64_t abs_end)
Given a [start,end), returns a vector of all tags in the range.
void block__set_history(unsigned history)
Definition: block_gateway.h:124
void block__message_port_pub(pmt::pmt_t port_id, pmt::pmt_t msg)
Definition: block_gateway.h:224
boost::intrusive_ptr< pmt_base > pmt_t
typedef for shared pointer (transparent reference counting). See http://www.boost.org/libs/smart_ptr/smart_ptr.htm
Definition: pmt.h:56
std::vector< void * > work_args_output_items
Definition: block_gateway.h:68
virtual void dispatch_msg(pmt::pmt_t which_port, pmt::pmt_t msg)
Definition: basic_block.h:131
The abstract base class for all &#39;terminal&#39; processing blocks.A signal processing flow is constructed ...
Definition: block.h:60
bool start_args_return_value
Definition: block_gateway.h:74
std::string block__name(void) const
Definition: block_gateway.h:116
std::vector< tag_t > block__get_tags_in_range(unsigned int which_input, uint64_t abs_start, uint64_t abs_end, const pmt::pmt_t &key)
Definition: block_gateway.h:205
void block__message_port_sub(pmt::pmt_t port_id, pmt::pmt_t target)
Definition: block_gateway.h:228
void message_port_register_out(pmt::pmt_t port_id)
Definition: block_gateway.h:86
void consume_each(int how_many_items)
Tell the scheduler how_many_items were consumed on each input stream.
void block__set_relative_rate(double relative_rate)
Definition: block_gateway.h:156
void block__message_port_register_in(pmt::pmt_t port_id)
Definition: block_gateway.h:216
int output_multiple() const
Definition: block.h:157
abstract class of message handlers
Definition: msg_handler.h:38