GNU Radio Manual and C++ API Reference  3.8.1.0
The Free & Open Software Radio Ecosystem
block_gateway.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2011-2013,2017 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 //! Magic return values from general_work, \ref gr::block::WORK_CALLED_PRODUCE
47  TPP_DONT = 0,
51 };
52 
53 /*!
54  * Shared message structure between python and gateway.
55  * Each action type represents a scheduler-called function.
56  */
58  enum action_type {
59  ACTION_GENERAL_WORK, // dispatch work
60  ACTION_WORK, // dispatch work
61  ACTION_FORECAST, // dispatch forecast
62  ACTION_START, // dispatch start
63  ACTION_STOP, // dispatch stop
64  };
65 
67 
70  std::vector<void*> general_work_args_input_items; // TODO this should be const void*,
71  // but swig can't int cast it right
72  std::vector<void*> general_work_args_output_items;
74 
77  std::vector<void*> work_args_input_items; // TODO this should be const void*, but swig
78  // can't int cast it right
79  std::vector<void*> work_args_output_items;
81 
84 
86 
88 };
89 
90 /*!
91  * The gateway block which performs all the magic.
92  *
93  * The gateway provides access to all the gr::block routines.
94  * The methods prefixed with gr::block__ are renamed
95  * to class methods without the prefix in python.
96  */
97 class GR_RUNTIME_API block_gateway : virtual public gr::block
98 {
99 public:
100  // gr::block_gateway::sptr
101  typedef boost::shared_ptr<block_gateway> sptr;
102 
103  /*!
104  * Make a new gateway block.
105  * \param handler the swig director object with callback
106  * \param name the name of the block (Ex: "Shirley")
107  * \param in_sig the input signature for this block
108  * \param out_sig the output signature for this block
109  * \param work_type the type of block overload to implement
110  * \param factor the decimation or interpolation factor
111  * \return a new gateway block
112  */
113  static sptr make(gr::feval_ll* handler,
114  const std::string& name,
115  gr::io_signature::sptr in_sig,
116  gr::io_signature::sptr out_sig,
117  const block_gw_work_type work_type,
118  const unsigned factor);
119 
120  //! Provide access to the shared message object
121  virtual block_gw_message_type& block_message(void) = 0;
122 
123  long block__unique_id(void) const { return gr::block::unique_id(); }
124 
125  std::string block__name(void) const { return gr::block::name(); }
126 
127  unsigned block__history(void) const { return gr::block::history(); }
128 
129  void block__set_history(unsigned history) { return gr::block::set_history(history); }
130 
131  void block__set_fixed_rate(bool fixed_rate)
132  {
133  return gr::block::set_fixed_rate(fixed_rate);
134  }
135 
136  bool block__fixed_rate(void) const { return gr::block::fixed_rate(); }
137 
138  void block__set_output_multiple(int multiple)
139  {
140  return gr::block::set_output_multiple(multiple);
141  }
142 
143  void block__set_min_output_buffer(int port, long size)
144  {
145  return gr::block::set_min_output_buffer(port, size);
146  }
147 
149  {
151  }
152 
154 
155  void block__consume(int which_input, int how_many_items)
156  {
157  return gr::block::consume(which_input, how_many_items);
158  }
159 
160  void block__consume_each(int how_many_items)
161  {
162  return gr::block::consume_each(how_many_items);
163  }
164 
165  void block__produce(int which_output, int how_many_items)
166  {
167  return gr::block::produce(which_output, how_many_items);
168  }
169 
170  void block__set_relative_rate(double relative_rate)
171  {
172  return gr::block::set_relative_rate(relative_rate);
173  }
174 
175  void block__set_inverse_relative_rate(double inverse_relative_rate)
176  {
177  return gr::block::set_inverse_relative_rate(inverse_relative_rate);
178  }
179 
180  void block__set_relative_rate(uint64_t interpolation, uint64_t decimation)
181  {
182  return gr::block::set_relative_rate(interpolation, decimation);
183  }
184 
185  double block__relative_rate(void) const { return gr::block::relative_rate(); }
186 
187  uint64_t block__relative_rate_i(void) const { return gr::block::relative_rate_i(); }
188 
189  uint64_t block__relative_rate_d(void) const { return gr::block::relative_rate_d(); }
190 
191  uint64_t block__nitems_read(unsigned int which_input)
192  {
193  return gr::block::nitems_read(which_input);
194  }
195 
196  uint64_t block__nitems_written(unsigned int which_output)
197  {
198  return gr::block::nitems_written(which_output);
199  }
200 
202  {
204  }
205 
207  {
209  }
210 
211  void block__add_item_tag(unsigned int which_output, const tag_t& tag)
212  {
213  return gr::block::add_item_tag(which_output, tag);
214  }
215 
216  void block__add_item_tag(unsigned int which_output,
217  uint64_t abs_offset,
218  const pmt::pmt_t& key,
219  const pmt::pmt_t& value,
220  const pmt::pmt_t& srcid = pmt::PMT_F)
221  {
222  return gr::block::add_item_tag(which_output, abs_offset, key, value, srcid);
223  }
224 
225  std::vector<tag_t> block__get_tags_in_range(unsigned int which_input,
226  uint64_t abs_start,
227  uint64_t abs_end)
228  {
229  std::vector<gr::tag_t> tags;
230  gr::block::get_tags_in_range(tags, which_input, abs_start, abs_end);
231  return tags;
232  }
233 
234  std::vector<tag_t> block__get_tags_in_range(unsigned int which_input,
235  uint64_t abs_start,
236  uint64_t abs_end,
237  const pmt::pmt_t& key)
238  {
239  std::vector<gr::tag_t> tags;
240  gr::block::get_tags_in_range(tags, which_input, abs_start, abs_end, key);
241  return tags;
242  }
243 
244  std::vector<tag_t> block__get_tags_in_window(unsigned int which_input,
245  uint64_t rel_start,
246  uint64_t rel_end)
247  {
248  std::vector<gr::tag_t> tags;
249  gr::block::get_tags_in_window(tags, which_input, rel_start, rel_end);
250  return tags;
251  }
252 
253  std::vector<tag_t> block__get_tags_in_window(unsigned int which_input,
254  uint64_t rel_start,
255  uint64_t rel_end,
256  const pmt::pmt_t& key)
257  {
258  std::vector<gr::tag_t> tags;
259  gr::block::get_tags_in_window(tags, which_input, rel_start, rel_end, key);
260  return tags;
261  }
262 
263  /* Message passing interface */
265  {
267  }
268 
270  {
272  }
273 
275  {
276  gr::basic_block::message_port_pub(port_id, msg);
277  }
278 
280  {
281  gr::basic_block::message_port_sub(port_id, target);
282  }
283 
285  {
286  gr::basic_block::message_port_unsub(port_id, target);
287  }
288 
290  {
291  return gr::basic_block::message_subscribers(which_port);
292  }
293 
295 
297 
299  {
300  if (msg_queue.find(which_port) == msg_queue.end()) {
301  throw std::runtime_error(
302  "attempt to set_msg_handler_feval() on bad input message port!");
303  }
304  d_msg_handlers_feval[which_port] = msg_handler;
305  }
306 
307 protected:
308  typedef std::map<pmt::pmt_t, feval_p*, pmt::comparator> msg_handlers_feval_t;
309  msg_handlers_feval_t d_msg_handlers_feval;
310 
311  bool has_msg_handler(pmt::pmt_t which_port)
312  {
313  if (d_msg_handlers_feval.find(which_port) != d_msg_handlers_feval.end()) {
314  return true;
315  } else {
316  return gr::basic_block::has_msg_handler(which_port);
317  }
318  }
319 
320  void dispatch_msg(pmt::pmt_t which_port, pmt::pmt_t msg)
321  {
322  // Is there a handler?
323  if (d_msg_handlers_feval.find(which_port) != d_msg_handlers_feval.end()) {
324  d_msg_handlers_feval[which_port]->calleval(msg); // Yes, invoke it.
325  } else {
326  // Pass to generic dispatcher if not found
327  gr::basic_block::dispatch_msg(which_port, msg);
328  }
329  }
330 };
331 
332 } /* namespace gr */
333 
334 #endif /* INCLUDED_RUNTIME_BLOCK_GATEWAY_H */
double relative_rate() const
return the approximate output rate / input rate
Definition: block.h:314
uint64_t relative_rate_i() const
return the numerator, or interpolation rate, of the approximate output rate / input rate ...
Definition: block.h:320
int general_work_args_return_value
Definition: block_gateway.h:73
boost::shared_ptr< io_signature > sptr
Definition: io_signature.h:46
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:320
void block__set_min_output_buffer(int port, long size)
Definition: block_gateway.h:143
bool stop_args_return_value
Definition: block_gateway.h:87
int block__output_multiple(void) const
Definition: block_gateway.h:153
block::tag_propagation_policy_t block__tag_propagation_policy(void)
Definition: block_gateway.h:201
action_type action
Definition: block_gateway.h:66
boost::shared_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:96
Definition: block_gateway.h:63
Definition: block_gateway.h:41
int output_multiple() const
Definition: block.h:219
pmt::pmt_t message_ports_out()
Get output message port names.
void block__set_tag_propagation_policy(block::tag_propagation_policy_t p)
Definition: block_gateway.h:206
double block__relative_rate(void) const
Definition: block_gateway.h:185
void set_fixed_rate(bool fixed_rate)
Definition: block.h:771
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:783
void set_min_output_buffer(long min_output_buffer)
Request limit on the minimum buffer size on all output ports.
Definition: block_gateway.h:62
Definition: block_gateway.h:50
void set_relative_rate(double relative_rate)
Set the approximate output rate / input rate.
void set_history(unsigned history)
std::vector< tag_t > block__get_tags_in_window(unsigned int which_input, uint64_t rel_start, uint64_t rel_end, const pmt::pmt_t &key)
Definition: block_gateway.h:253
base class for evaluating a function: long -> longThis class is designed to be subclassed in Python o...
Definition: feval.h:104
tag_propagation_policy_t tag_propagation_policy()
Asks for the policy used by the scheduler to moved tags downstream.
#define PMT_F
Definition: pmt.h:136
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:191
long block__unique_id(void) const
Definition: block_gateway.h:123
Definition: block_gateway.h:38
std::vector< int > forecast_args_ninput_items_required
Definition: block_gateway.h:83
void block__set_output_multiple(int multiple)
Definition: block_gateway.h:138
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:225
bool has_msg_handler(pmt::pmt_t which_port)
Tests if there is a handler attached to port which_port.
Definition: block_gateway.h:311
virtual bool has_msg_handler(pmt::pmt_t which_port)
Tests if there is a handler attached to port which_port.
Definition: basic_block.h:119
bool block__fixed_rate(void) const
Definition: block_gateway.h:136
int general_work_args_noutput_items
Definition: block_gateway.h:68
unsigned block__history(void) const
Definition: block_gateway.h:127
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:101
Definition: block_gateway.h:39
uint64_t nitems_read(unsigned int which_input)
Return the number of items read on input stream which_input.
Definition: block_gateway.h:57
uint64_t block__nitems_written(unsigned int which_output)
Definition: block_gateway.h:196
void block__produce(int which_output, int how_many_items)
Definition: block_gateway.h:165
Definition: block_gateway.h:45
Definition: block_gateway.h:45
msg_handlers_feval_t d_msg_handlers_feval
Definition: block_gateway.h:309
void message_port_register_in(pmt::pmt_t port_id)
unsigned history() const
block_gw_work_return_type
Magic return values from general_work, gr::block::WORK_CALLED_PRODUCE.
Definition: block_gateway.h:45
pmt::pmt_t block__message_subscribers(pmt::pmt_t which_port)
Definition: block_gateway.h:289
uint64_t nitems_written(unsigned int which_output)
Return the number of items written on output stream which_output.
Definition: block_gateway.h:48
thread-safe message queue
Definition: msg_queue.h:36
uint64_t block__relative_rate_d(void) const
Definition: block_gateway.h:189
void block__set_min_output_buffer(long size)
Definition: block_gateway.h:148
void block__consume_each(int how_many_items)
Definition: block_gateway.h:160
int work_args_return_value
Definition: block_gateway.h:80
void block__set_inverse_relative_rate(double inverse_relative_rate)
Definition: block_gateway.h:175
void block__set_fixed_rate(bool fixed_rate)
Definition: block_gateway.h:131
GNU Radio logging wrapper for log4cpp library (C++ port of log4j)
Definition: basic_block.h:43
std::vector< void * > general_work_args_output_items
Definition: block_gateway.h:72
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:216
Definition: block_gateway.h:40
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:82
std::vector< void * > work_args_output_items
Definition: block_gateway.h:79
void block__consume(int which_input, int how_many_items)
Definition: block_gateway.h:155
void block__message_port_unsub(pmt::pmt_t port_id, pmt::pmt_t target)
Definition: block_gateway.h:284
std::string block__name(void) const
Definition: block_gateway.h:125
std::map< pmt::pmt_t, feval_p *, pmt::comparator > msg_handlers_feval_t
Definition: block_gateway.h:308
Definition: block_gateway.h:61
tag_propagation_policy_t
enum to represent different tag propagation policies.
Definition: block.h:80
void message_port_sub(pmt::pmt_t port_id, pmt::pmt_t target)
void block__set_relative_rate(uint64_t interpolation, uint64_t decimation)
Definition: block_gateway.h:180
std::vector< void * > work_args_input_items
Definition: block_gateway.h:77
uint64_t block__relative_rate_i(void) const
Definition: block_gateway.h:187
std::vector< void * > general_work_args_input_items
Definition: block_gateway.h:70
Definition: block_gateway.h:60
void set_inverse_relative_rate(double inverse_relative_rate)
Set the approximate output rate / input rate using its reciprocal.
int work_args_ninput_items
Definition: block_gateway.h:75
void get_tags_in_window(std::vector< tag_t > &v, unsigned int which_input, uint64_t rel_start, uint64_t rel_end)
Gets all tags within the relative window of the current call to work.
std::vector< int > general_work_args_ninput_items
Definition: block_gateway.h:69
Definition: block_gateway.h:47
pmt::pmt_t block__message_ports_out()
Definition: block_gateway.h:296
pmt::pmt_t message_ports_in()
Get input message port names.
long unique_id() const
Definition: basic_block.h:144
int work_args_noutput_items
Definition: block_gateway.h:76
base class for evaluating a function: pmt -> 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:298
pmt::pmt_t block__message_ports_in()
Definition: block_gateway.h:294
Definition: block_gateway.h:49
void block__message_port_register_out(pmt::pmt_t port_id)
Definition: block_gateway.h:269
void block__add_item_tag(unsigned int which_output, const tag_t &tag)
Definition: block_gateway.h:211
action_type
Definition: block_gateway.h:58
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:129
void block__message_port_pub(pmt::pmt_t port_id, pmt::pmt_t msg)
Definition: block_gateway.h:274
virtual void dispatch_msg(pmt::pmt_t which_port, pmt::pmt_t msg)
Definition: basic_block.h:130
The abstract base class for all &#39;terminal&#39; processing blocks.A signal processing flow is constructed ...
Definition: block.h:71
tag_propagation_policy_t
Definition: block_gateway.h:46
bool fixed_rate() const
Return true if this block has a fixed input to output rate.
Definition: block.h:147
bool start_args_return_value
Definition: block_gateway.h:85
std::string name() const
Definition: basic_block.h:148
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:234
uint64_t relative_rate_d() const
return the denominator, or decimation rate, of the approximate output rate / input rate ...
Definition: block.h:329
std::vector< tag_t > block__get_tags_in_window(unsigned int which_input, uint64_t rel_start, uint64_t rel_end)
Definition: block_gateway.h:244
void block__message_port_sub(pmt::pmt_t port_id, pmt::pmt_t target)
Definition: block_gateway.h:279
void message_port_register_out(pmt::pmt_t port_id)
pmt::pmt_t message_subscribers(pmt::pmt_t port)
Definition: block_gateway.h:97
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:170
void block__message_port_register_in(pmt::pmt_t port_id)
Definition: block_gateway.h:264
abstract class of message handlers
Definition: msg_handler.h:38