GNU Radio Manual and C++ API Reference  3.8.1.0
The Free & Open Software Radio Ecosystem
hier_block2.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2006-2009,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_GR_RUNTIME_HIER_BLOCK2_H
24 #define INCLUDED_GR_RUNTIME_HIER_BLOCK2_H
25 
26 #include <gnuradio/api.h>
27 #include <gnuradio/basic_block.h>
28 
29 namespace gr {
30 
31 /*!
32  * \brief public constructor for hier_block2
33  */
34 GR_RUNTIME_API hier_block2_sptr make_hier_block2(const std::string& name,
35  gr::io_signature::sptr input_signature,
36  gr::io_signature::sptr output_signature);
37 
38 class hier_block2_detail;
39 
40 /*!
41  * \brief Hierarchical container class for gr::block's and gr::hier_block2's
42  * \ingroup container_blk
43  * \ingroup base_blk
44  */
46 {
47 private:
48  friend class hier_block2_detail;
49  friend GR_RUNTIME_API hier_block2_sptr
50  make_hier_block2(const std::string& name,
51  gr::io_signature::sptr input_signature,
52  gr::io_signature::sptr output_signature);
53 
54  /*!
55  * \brief Private implementation details of gr::hier_block2
56  */
57  hier_block2_detail* d_detail;
58 
59 
60 protected:
61  hier_block2(void) {} // allows pure virtual interface sub-classes
62  hier_block2(const std::string& name,
63  gr::io_signature::sptr input_signature,
64  gr::io_signature::sptr output_signature);
65 
66 public:
67  virtual ~hier_block2();
68 
69  /*!
70  * \brief typedef for object returned from self().
71  *
72  * This type is only guaranteed to be passable to connect and
73  * disconnect. No other assumptions should be made about it.
74  */
75  typedef basic_block_sptr opaque_self;
76 
77  /*!
78  * \brief Return an object, representing the current block, which
79  * can be passed to connect.
80  *
81  * The returned object may only be used as an argument to connect
82  * or disconnect. Any other use of self() results in unspecified
83  * (erroneous) behavior.
84  */
85  opaque_self self();
86 
87  /*!
88  * \brief Add a stand-alone (possibly hierarchical) block to
89  * internal graph
90  *
91  * This adds a gr-block or hierarchical block to the internal
92  * graph without wiring it to anything else.
93  */
94  void connect(basic_block_sptr block);
95 
96  /*!
97  * \brief Add gr-blocks or hierarchical blocks to internal graph
98  * and wire together
99  *
100  * This adds (if not done earlier by another connect) a pair of
101  * gr-blocks or hierarchical blocks to the internal flowgraph, and
102  * wires the specified output port to the specified input port.
103  */
104  void connect(basic_block_sptr src, int src_port, basic_block_sptr dst, int dst_port);
105 
106  /*!
107  * \brief Add gr-blocks or hierarchical blocks to internal graph
108  * and wire together
109  *
110  * This adds (if not done earlier by another connect) a pair of
111  * gr-blocks or hierarchical blocks to the internal message port
112  * subscription
113  */
114  void msg_connect(basic_block_sptr src,
115  pmt::pmt_t srcport,
116  basic_block_sptr dst,
117  pmt::pmt_t dstport);
118  void msg_connect(basic_block_sptr src,
119  std::string srcport,
120  basic_block_sptr dst,
121  std::string dstport);
122  void msg_disconnect(basic_block_sptr src,
123  pmt::pmt_t srcport,
124  basic_block_sptr dst,
125  pmt::pmt_t dstport);
126  void msg_disconnect(basic_block_sptr src,
127  std::string srcport,
128  basic_block_sptr dst,
129  std::string dstport);
130 
131  /*!
132  * \brief Remove a gr-block or hierarchical block from the
133  * internal flowgraph.
134  *
135  * This removes a gr-block or hierarchical block from the internal
136  * flowgraph, disconnecting it from other blocks as needed.
137  */
138  void disconnect(basic_block_sptr block);
139 
140  /*!
141  * \brief Disconnect a pair of gr-blocks or hierarchical blocks in
142  * internal flowgraph.
143  *
144  * This disconnects the specified input port from the specified
145  * output port of a pair of gr-blocks or hierarchical blocks.
146  */
147  void
148  disconnect(basic_block_sptr src, int src_port, basic_block_sptr dst, int dst_port);
149 
150  /*!
151  * \brief Disconnect all connections in the internal flowgraph.
152  *
153  * This call removes all output port to input port connections in
154  * the internal flowgraph.
155  */
156  void disconnect_all();
157 
158  /*!
159  * Lock a flowgraph in preparation for reconfiguration. When an
160  * equal number of calls to lock() and unlock() have occurred, the
161  * flowgraph will be reconfigured.
162  *
163  * N.B. lock() and unlock() may not be called from a flowgraph
164  * thread (E.g., gr::block::work method) or deadlock will occur
165  * when reconfiguration happens.
166  */
167  virtual void lock();
168 
169  /*!
170  * Unlock a flowgraph in preparation for reconfiguration. When an
171  * equal number of calls to lock() and unlock() have occurred, the
172  * flowgraph will be reconfigured.
173  *
174  * N.B. lock() and unlock() may not be called from a flowgraph
175  * thread (E.g., gr::block::work method) or deadlock will occur
176  * when reconfiguration happens.
177  */
178  virtual void unlock();
179 
180  /*!
181  * \brief Returns max buffer size (itemcount) on output port \p i.
182  */
183  int max_output_buffer(size_t port = 0);
184 
185  /*!
186  * \brief Sets max buffer size (itemcount) on all output ports.
187  */
188  void set_max_output_buffer(int max_output_buffer);
189 
190  /*!
191  * \brief Sets max buffer size (itemcount) on output port \p port.
192  */
193  void set_max_output_buffer(size_t port, int max_output_buffer);
194 
195  /*!
196  * \brief Returns min buffer size (itemcount) on output port \p i.
197  */
198  int min_output_buffer(size_t port = 0);
199 
200  /*!
201  * \brief Sets min buffer size (itemcount) on all output ports.
202  */
203  void set_min_output_buffer(int min_output_buffer);
204 
205  /*!
206  * \brief Sets min buffer size (itemcount) on output port \p port.
207  */
208  void set_min_output_buffer(size_t port, int min_output_buffer);
209 
210 
211  // This is a public method for ease of code organization, but should be
212  // ignored by the user.
213  flat_flowgraph_sptr flatten() const;
214 
215  hier_block2_sptr to_hier_block2(); // Needed for Python type coercion
216 
217  bool has_msg_port(pmt::pmt_t which_port)
218  {
219  return message_port_is_hier(which_port) || basic_block::has_msg_port(which_port);
220  }
221 
223  {
224  return message_port_is_hier_in(port_id) || message_port_is_hier_out(port_id);
225  }
226 
228  {
229  return pmt::list_has(hier_message_ports_in, port_id);
230  }
231 
233  {
234  return pmt::list_has(hier_message_ports_out, port_id);
235  }
236 
239 
241  {
242  if (pmt::list_has(hier_message_ports_in, port_id))
243  throw std::invalid_argument(
244  "hier msg in port by this name already registered");
245  if (msg_queue.find(port_id) != msg_queue.end())
246  throw std::invalid_argument(
247  "block already has a primitive input port by this name");
248  hier_message_ports_in = pmt::list_add(hier_message_ports_in, port_id);
249  }
250 
252  {
253  if (pmt::list_has(hier_message_ports_out, port_id))
254  throw std::invalid_argument(
255  "hier msg out port by this name already registered");
256  if (pmt::dict_has_key(d_message_subscribers, port_id))
257  throw std::invalid_argument(
258  "block already has a primitive output port by this name");
259  hier_message_ports_out = pmt::list_add(hier_message_ports_out, port_id);
260  }
261 
262  /*!
263  * \brief Set the affinity of all blocks in hier_block2 to processor core \p n.
264  *
265  * \param mask a vector of ints of the core numbers available to this block.
266  */
267  void set_processor_affinity(const std::vector<int>& mask);
268 
269  /*!
270  * \brief Remove processor affinity for all blocks in hier_block2.
271  */
272  void unset_processor_affinity();
273 
274  /*!
275  * \brief Get the current processor affinity.
276  *
277  * \details This returns the processor affinity value for the first
278  * block in the hier_block2's list of blocks with the assumption
279  * that they have always only been set through the hier_block2's
280  * interface. If any block has been individually set, then this
281  * call could be misleading.
282  */
283  std::vector<int> processor_affinity();
284 
285  /*!
286  * \brief Set the logger's output level.
287  *
288  * Sets the level of the logger for all connected blocks. This takes
289  * a string that is translated to the standard levels and can be
290  * (case insensitive):
291  *
292  * \li off , notset
293  * \li debug
294  * \li info
295  * \li notice
296  * \li warn
297  * \li error
298  * \li crit
299  * \li alert
300  * \li fatal
301  * \li emerg
302  */
303  void set_log_level(std::string level);
304 
305  /*!
306  * \brief Get the logger's output level
307  */
308  std::string log_level();
309 
310  /*!
311  * \brief Get if all block min buffers should be set.
312  *
313  * \details this returns whether all the block min output buffers
314  * should be set or just the block ports connected to the hier ports.
315  */
316  bool all_min_output_buffer_p(void);
317 
318  /*!
319  * \brief Get if all block max buffers should be set.
320  *
321  * \details this returns whether all the block max output buffers
322  * should be set or just the block ports connected to the hier ports.
323  */
324  bool all_max_output_buffer_p(void);
325 };
326 
327 /*!
328  * \brief Return hierarchical block's flow graph represented in dot language
329  */
330 GR_RUNTIME_API std::string dot_graph(hier_block2_sptr hierblock2);
331 
332 inline hier_block2_sptr cast_to_hier_block2_sptr(basic_block_sptr block)
333 {
334  return boost::dynamic_pointer_cast<hier_block2, basic_block>(block);
335 }
336 
337 } /* namespace gr */
338 
339 #endif /* INCLUDED_GR_RUNTIME_HIER_BLOCK2_H */
boost::shared_ptr< io_signature > sptr
Definition: io_signature.h:46
void message_port_register_hier_out(pmt::pmt_t port_id)
Definition: hier_block2.h:251
bool message_port_is_hier(pmt::pmt_t port_id)
Definition: hier_block2.h:222
PMT_API bool list_has(pmt_t list, const pmt_t &item)
Return bool of list contains item.
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
void message_port_register_hier_in(pmt::pmt_t port_id)
Definition: hier_block2.h:240
bool has_msg_port(pmt::pmt_t which_port)
Definition: hier_block2.h:217
virtual bool has_msg_port(pmt::pmt_t which_port)
Definition: basic_block.h:286
basic_block_sptr opaque_self
typedef for object returned from self().
Definition: hier_block2.h:75
#define GR_RUNTIME_API
Definition: gnuradio-runtime/include/gnuradio/api.h:30
thread-safe message queue
Definition: msg_queue.h:36
GNU Radio logging wrapper for log4cpp library (C++ port of log4j)
Definition: basic_block.h:43
GR_RUNTIME_API std::string dot_graph(hier_block2_sptr hierblock2)
Return hierarchical block&#39;s flow graph represented in dot language.
The abstract base class for all signal processing blocks.Basic blocks are the bare abstraction of an ...
Definition: basic_block.h:56
bool message_port_is_hier_in(pmt::pmt_t port_id)
Definition: hier_block2.h:227
bool message_port_is_hier_out(pmt::pmt_t port_id)
Definition: hier_block2.h:232
hier_block2(void)
Definition: hier_block2.h:61
pmt::pmt_t hier_message_ports_in
Definition: hier_block2.h:237
pmt::pmt_t hier_message_ports_out
Definition: hier_block2.h:238
PMT_API pmt_t list_add(pmt_t list, const pmt_t &item)
Return list with item added to it.
PMT_API bool dict_has_key(const pmt_t &dict, const pmt_t &key)
Return true if key exists in dict.
The abstract base class for all &#39;terminal&#39; processing blocks.A signal processing flow is constructed ...
Definition: block.h:71
GR_RUNTIME_API hier_block2_sptr make_hier_block2(const std::string &name, gr::io_signature::sptr input_signature, gr::io_signature::sptr output_signature)
public constructor for hier_block2
Hierarchical container class for gr::block&#39;s and gr::hier_block2&#39;s.
Definition: hier_block2.h:45