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