GNU Radio Manual and C++ API Reference  3.10.9.1
The Free & Open Software Radio Ecosystem
rfnoc_block.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2020 Free Software Foundation, Inc.
4  * Copyright 2020 Ettus Research, A National Instruments Brand.
5  *
6  * SPDX-License-Identifier: GPL-3.0-or-later
7  */
8 
9 #ifndef INCLUDED_UHD_RFNOC_BLOCK_H
10 #define INCLUDED_UHD_RFNOC_BLOCK_H
11 
12 #include <gnuradio/block.h>
14 #include <pmt/pmt.h>
15 #include <uhd/rfnoc/noc_block_base.hpp>
16 #include <unordered_map>
17 #include <functional>
18 #include <string>
19 
20 namespace gr {
21 namespace uhd {
22 
23 /*! Base class for RFNoC blocks controlled by GNU Radio
24  *
25  * Any GNU Radio block that is meant to control an RFNoC block
26  * should be derived from this class.
27  */
29 {
30 protected:
31  // \param block_ref A reference to the underlying block controller
32  rfnoc_block(::uhd::rfnoc::noc_block_base::sptr block_ref);
33 
34  rfnoc_block() {} // For virtual subclassing
35 
36 public:
37  using sptr = std::shared_ptr<rfnoc_block>;
38 
39  //! Factory function to create a UHD block controller reference
40  //
41  // \param graph Reference to the flowgraph's RFNoC graph
42  // \param block_args Block args
43  // \param block_name Block name (e.g. "DDC")
44  // \param device_select Device index (motherboard index)
45  // \param block_select Block index
46  // \param max_ref_count Maximum number of references this block can have in
47  // the GNU Radio flow graph
48  static ::uhd::rfnoc::noc_block_base::sptr
50  const ::uhd::device_addr_t& block_args,
51  const std::string& block_name,
52  const int device_select = -1,
53  const int block_select = -1,
54  const size_t max_ref_count = 1);
55 
56  //! Return a type-cast block reference, or throw if the cast failed.
57  //
58  // \throws std::runtime_error if there is no valid block reference
59  template <typename block_type>
60  std::shared_ptr<block_type> get_block_ref()
61  {
62  auto cast_block_ref = std::dynamic_pointer_cast<block_type>(d_block_ref);
63  if (!cast_block_ref) {
64  throw std::runtime_error(
65  std::string(
66  "Unable to cast the following block into its desired type: ") +
67  d_block_ref->get_unique_id());
68  }
69  return cast_block_ref;
70  }
71 
72  /*! Return the unique ID of the underlying block
73  */
74  std::string get_unique_id() const;
75 
76  // GNU Radio-specific overrides
77 
78  //! This method should never be called by RFNoC blocks, they do the work
79  // in the FPGA.
80  int general_work(int noutput_items,
81  gr_vector_int& ninput_items,
82  gr_vector_const_void_star& input_items,
83  gr_vector_void_star& output_items) final;
84 
85  /*! Set multiple properties coming from a dictionary
86  *
87  * See the [UHD
88  * manual](https://uhd.readthedocs.io/en/latest/classuhd_1_1rfnoc_1_1node__t.html#abf34d4be8fe7a602b27927194195f1f6)
89  * for details.
90  *
91  * This function allows the client to override the \p instance parameter
92  * for each property key/value pair passed in via the \p props parameter.
93  * If the key consists of the property name, followed by a colon (':') and
94  * then a number, the number following the colon is used to determine
95  * which instance of the property this set pertains to, and the \p
96  * instance parameter is ignored for that property. (Note that if the key
97  * does not have the colon and instance number override syntax, then
98  * \p instance is still used to determine which instance of the property
99  * to set. For example, in the following call:
100  *
101  * node->set_properties("dog=10,cat:2=5,bird:0=0.5", 1)
102  *
103  * instance 1 of node's 'dog' property is set to 10, the 1 coming from the
104  * instance parameter, instance 2 of the node's 'cat' property is set to
105  * 5 due to the override syntax provided in the string, and instance 0 of
106  * the node's 'bird' property is set to 0.5 due to its override.
107  *
108  * If the instance override is malformed, that is, there is no
109  * number following the colon, or the number cannot be parsed as an
110  * integer, a value_error is thrown.
111  *
112  * If a key in \p props is not a valid property of this block, a warning is
113  * logged, but no error is raised.
114  */
115  void set_properties(const ::uhd::device_addr_t& props, const size_t instance = 0)
116  {
117  d_block_ref->set_properties(props, instance);
118  }
119 
120  /*! Set a specific user property that belongs to this block.
121  *
122  * Setting a user property will trigger a property resolution. This means
123  * that changing this block can have effects on other RFNoC blocks or nodes
124  * (like streamers).
125  *
126  * \param name The name of the property.
127  * \param value The new value of the property.
128  * \param port The port of the property.
129  */
130  template <typename T>
131  void set_property(const std::string& name, const T& value, const size_t port = 0)
132  {
133  d_block_ref->set_property<T>(name, value, port);
134  }
135 
136  /*! Get the value of a specific block argument. \p The type of an argument
137  * must be known at compile time.
138  *
139  * Note: Despite this being a "getter", this function is not declared const.
140  * This is because internally, it can resolve properties, which may cause
141  * changes within the object.
142  *
143  * \param name The name of the property.
144  * \param port The port of the property.
145  * \return The value of the property.
146  */
147  template <typename T>
148  const T get_property(const std::string& name, const size_t port = 0)
149  {
150  return d_block_ref->get_property<T>(name, port);
151  }
152 
153  std::vector<std::string> get_property_ids();
154 
155 
156 protected:
157  using cmd_handler_t = std::function<void(const pmt::pmt_t&, int, const pmt::pmt_t&)>;
158 
159  //! Register a new handler for command key \p cmd
160  void register_msg_cmd_handler(const std::string& cmd, cmd_handler_t handler);
161 
162 private:
163  /**********************************************************************
164  * Command Interface
165  **********************************************************************/
166  //! Receives commands and handles them
167  void _msg_handler_command(pmt::pmt_t msg);
168 
169  //! For a given argument, call the associated handler, or if none exists,
170  // show a warning through the logging interface.
171  void _dispatch_msg_cmd_handler(const std::string& cmd,
172  const pmt::pmt_t& val,
173  int chan,
174  pmt::pmt_t& msg);
175 
176  //! Stores the individual command handlers
177  std::unordered_map<std::string, cmd_handler_t> _msg_cmd_handlers;
178 
179  //! Reference to the underlying RFNoC block
180  ::uhd::rfnoc::noc_block_base::sptr d_block_ref;
181 };
182 
183 } // namespace uhd
184 } // namespace gr
185 
186 #endif /* INCLUDED_UHD_RFNOC_BLOCK_H */
The abstract base class for all 'terminal' processing blocks.
Definition: gnuradio-runtime/include/gnuradio/block.h:63
Definition: rfnoc_block.h:29
::uhd::rfnoc::noc_block_base::sptr make_block_ref(rfnoc_graph::sptr graph, const ::uhd::device_addr_t &block_args, const std::string &block_name, const int device_select=-1, const int block_select=-1, const size_t max_ref_count=1)
Factory function to create a UHD block controller reference.
std::vector< std::string > get_property_ids()
rfnoc_block(::uhd::rfnoc::noc_block_base::sptr block_ref)
void set_property(const std::string &name, const T &value, const size_t port=0)
Definition: rfnoc_block.h:131
void set_properties(const ::uhd::device_addr_t &props, const size_t instance=0)
Definition: rfnoc_block.h:115
std::string get_unique_id() const
void register_msg_cmd_handler(const std::string &cmd, cmd_handler_t handler)
Register a new handler for command key cmd.
int general_work(int noutput_items, gr_vector_int &ninput_items, gr_vector_const_void_star &input_items, gr_vector_void_star &output_items) final
This method should never be called by RFNoC blocks, they do the work.
std::function< void(const pmt::pmt_t &, int, const pmt::pmt_t &)> cmd_handler_t
Definition: rfnoc_block.h:157
std::shared_ptr< rfnoc_block > sptr
Definition: rfnoc_block.h:37
const T get_property(const std::string &name, const size_t port=0)
Definition: rfnoc_block.h:148
rfnoc_block()
Definition: rfnoc_block.h:34
std::shared_ptr< block_type > get_block_ref()
Return a type-cast block reference, or throw if the cast failed.
Definition: rfnoc_block.h:60
std::shared_ptr< rfnoc_graph > sptr
Definition: rfnoc_graph.h:32
#define GR_UHD_API
Definition: gr-uhd/include/gnuradio/uhd/api.h:18
GR_RUNTIME_API const pmt::pmt_t msg()
GNU Radio logging wrapper.
Definition: basic_block.h:29
std::shared_ptr< pmt_base > pmt_t
typedef for shared pointer (transparent reference counting).
Definition: pmt.h:83
std::vector< const void * > gr_vector_const_void_star
Definition: types.h:28
std::vector< void * > gr_vector_void_star
Definition: types.h:27
std::vector< int > gr_vector_int
Definition: types.h:23