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