USRP Hardware Driver and USRP Manual  Version: 4.1.0.4-3
UHD and USRP Manual
node.hpp
Go to the documentation of this file.
1 //
2 // Copyright 2019 Ettus Research, a National Instruments Brand
3 //
4 // SPDX-License-Identifier: GPL-3.0-or-later
5 //
6 
7 #pragma once
8 
9 #include <uhd/rfnoc/actions.hpp>
10 #include <uhd/rfnoc/dirtifier.hpp>
11 #include <uhd/rfnoc/property.hpp>
13 #include <uhd/types/time_spec.hpp>
14 #include <uhd/utils/log.hpp>
15 #include <uhd/utils/scope_exit.hpp>
16 #include <unordered_map>
17 #include <unordered_set>
18 #include <boost/graph/adjacency_list.hpp>
19 #include <functional>
20 #include <memory>
21 #include <mutex>
22 #include <string>
23 #include <tuple>
24 #include <vector>
25 
26 namespace uhd { namespace rfnoc {
27 
35 {
36 public:
37  using resolver_fn_t = std::function<void(void)>;
38  using resolve_callback_t = std::function<void(void)>;
40  std::function<void(const res_source_info&, action_info::sptr)>;
42  std::unordered_map<res_source_info, std::vector<res_source_info>>;
43 
45  enum class forwarding_policy_t {
47  //(e.g., if it comes from input port 0, forward it to output port 0).
48  ONE_TO_ONE,
50  ONE_TO_FAN,
52  ONE_TO_ALL_IN,
54  ONE_TO_ALL_OUT,
56  ONE_TO_ALL,
58  DROP,
60  USE_MAP
61  };
62 
63  static const size_t ANY_PORT = size_t(~0);
64 
65  /**************************************************************************
66  * Structors
67  *************************************************************************/
68  node_t();
69 
70  virtual ~node_t() {}
71 
72  /******************************************
73  * Basic Operations
74  ******************************************/
76  // no two nodes cannot have the same ID.
77  //
78  // \returns The unique ID as a string
79  virtual std::string get_unique_id() const;
80 
87  virtual size_t get_num_input_ports() const = 0;
88 
95  virtual size_t get_num_output_ports() const = 0;
96 
97  /******************************************
98  * Property Specific
99  ******************************************/
100 
109  std::vector<std::string> get_property_ids() const;
110 
126  template <typename prop_data_t>
127  void set_property(
128  const std::string& id, const prop_data_t& val, const size_t instance = 0);
129 
162  void set_properties(const uhd::device_addr_t& props, const size_t instance = 0);
163 
181  template <typename prop_data_t>
182  const prop_data_t& get_property(
183  const std::string& id, const size_t instance = 0) /* mutable */;
184 
200  virtual void set_command_time(uhd::time_spec_t time, const size_t instance);
201 
206  virtual uhd::time_spec_t get_command_time(const size_t instance) const;
207 
213  virtual void clear_command_time(const size_t instance);
214 
215 protected:
216  /******************************************
217  * Internal Registration Functions
218  ******************************************/
219  using prop_ptrs_t = std::unordered_set<property_base_t*>;
220 
240  property_base_t* prop, resolve_callback_t&& clean_callback = nullptr);
241 
275  prop_ptrs_t&& inputs, prop_ptrs_t&& outputs, resolver_fn_t&& resolver_fn);
276 
277  /**************************************************************************
278  * Property forwarding
279  *************************************************************************/
299  forwarding_policy_t policy, const std::string& prop_id = "");
300 
321 
333  template <typename prop_data_t>
334  void set_property(
335  const std::string& id, const prop_data_t& val, const res_source_info& src_info);
336 
348  template <typename prop_data_t>
349  const prop_data_t& get_property(
350  const std::string& id, const res_source_info& src_info) /* mutable */;
351 
352  /******************************************
353  * Internal action forwarding
354  ******************************************/
368  void register_action_handler(const std::string& id, action_handler_t&& handler);
369 
389  forwarding_policy_t policy, const std::string& action_key = "");
390 
414 
426  void post_action(const res_source_info& edge_info, action_info::sptr action);
427 
428  /**************************************************************************
429  * Graph Interaction
430  *************************************************************************/
451  virtual bool check_topology(const std::vector<size_t>& connected_inputs,
452  const std::vector<size_t>& connected_outputs);
453 
459  virtual void shutdown();
460 
461  /**************************************************************************
462  * Attributes
463  *************************************************************************/
466 
467 private:
468  friend class node_accessor_t;
469 
474  property_base_t* _find_property(
475  res_source_info src_info, const std::string& id) const;
476 
482  uhd::utils::scope_exit::uptr _request_property_access(
483  property_base_t* prop, property_base_t::access_t access) const;
484 
489  template <typename PredicateType>
490  prop_ptrs_t filter_props(PredicateType&& predicate)
491  {
492  prop_ptrs_t filtered_props{};
493  for (const auto& type_prop_pair : _props) {
494  for (const auto& prop : type_prop_pair.second) {
495  if (predicate(prop)) {
496  filtered_props.insert(prop);
497  }
498  }
499  }
500 
501  return filtered_props;
502  }
503 
511  property_base_t* inject_edge_property(
512  property_base_t* blueprint, res_source_info new_src_info);
513 
527  void init_props();
528 
552  void resolve_props();
553 
556  void resolve_all();
557 
563  void clean_props();
564 
568  void set_resolve_all_callback(resolve_callback_t&& resolver)
569  {
570  _resolve_all_cb = resolver;
571  }
572 
575  void clear_resolve_all_callback()
576  {
577  _resolve_all_cb = _default_resolve_all_cb;
578  }
579 
614  void forward_edge_property(
615  property_base_t* incoming_prop, const size_t incoming_port);
616 
617  /**************************************************************************
618  * Action-Related Methods
619  *************************************************************************/
623  void set_post_action_callback(action_handler_t&& post_handler)
624  {
625  _post_action_cb = std::move(post_handler);
626  }
627 
636  void receive_action(const res_source_info& src_info, action_info::sptr action);
637 
638  /**************************************************************************
639  * Private helpers
640  *************************************************************************/
642  bool _has_port(const res_source_info& port_info) const;
643 
644  /****** Attributes *******************************************************/
646  // global property mutex, this only write-protects access to the property-
647  // related containers in this class.
648  mutable std::mutex _prop_mutex;
649 
651  std::unordered_map<res_source_info::source_t,
652  std::vector<property_base_t*>,
653  std::hash<size_t>>
654  _props;
655 
657  std::unordered_map<property_base_t*, resolve_callback_t> _clean_cb_registry;
658 
659  using property_resolver_t = std::tuple<prop_ptrs_t, prop_ptrs_t, resolver_fn_t>;
661  std::vector<property_resolver_t> _prop_resolvers;
662 
664  // has changed, and that a property resolution needs to be performed.
665  resolve_callback_t _resolve_all_cb;
666 
668  // method.
669  const resolve_callback_t _default_resolve_all_cb = [this]() {
670  resolve_props();
671  clean_props();
672  };
673 
675  // explicitly.
676  //
677  // Dynamic properties include properties defined in the block descriptor
678  // file, as well as new properties that get passed in during property
679  // propagation.
680  std::unordered_set<std::unique_ptr<property_base_t>> _dynamic_props;
681 
683  //
684  // The entry with the empty-string-key is the default policy.
685  std::unordered_map<std::string, forwarding_policy_t> _prop_fwd_policies{
686  {"", forwarding_policy_t::ONE_TO_ONE}};
687 
689  forwarding_map_t _prop_fwd_map;
690 
691  /**************************************************************************
692  * Action-related attributes
693  *************************************************************************/
694  mutable std::mutex _action_mutex;
695 
697  std::unordered_map<std::string, action_handler_t> _action_handlers;
698 
700  std::unordered_map<std::string, forwarding_policy_t> _action_fwd_policies{
701  {"", forwarding_policy_t::ONE_TO_ONE}};
702 
704  //
705  // The default callback will simply drop actions
706  action_handler_t _post_action_cb = [](const res_source_info&,
707  action_info::sptr) { /* nop */ };
708 
710  forwarding_map_t _action_fwd_map;
711 
712  /**************************************************************************
713  * Other attributes
714  *************************************************************************/
715  std::vector<uhd::time_spec_t> _cmd_timespecs;
716 }; // class node_t
717 
718 }} /* namespace uhd::rfnoc */
719 
720 #include <uhd/rfnoc/node.ipp>
Definition: device_addr.hpp:38
Definition: dirtifier.hpp:19
Definition: node.hpp:35
virtual std::string get_unique_id() const
Return a unique identifier string for this node. In every RFNoC graph,.
void register_property(property_base_t *prop, resolve_callback_t &&clean_callback=nullptr)
std::unordered_map< res_source_info, std::vector< res_source_info > > forwarding_map_t
Definition: node.hpp:42
void set_action_forwarding_policy(forwarding_policy_t policy, const std::string &action_key="")
void set_prop_forwarding_map(const forwarding_map_t &map)
std::function< void(void)> resolver_fn_t
Definition: node.hpp:37
std::function< void(void)> resolve_callback_t
Definition: node.hpp:38
void register_action_handler(const std::string &id, action_handler_t &&handler)
virtual void set_command_time(uhd::time_spec_t time, const size_t instance)
void post_action(const res_source_info &edge_info, action_info::sptr action)
virtual bool check_topology(const std::vector< size_t > &connected_inputs, const std::vector< size_t > &connected_outputs)
virtual ~node_t()
Definition: node.hpp:70
void set_action_forwarding_map(const forwarding_map_t &map)
virtual size_t get_num_output_ports() const =0
virtual void shutdown()
std::unordered_set< property_base_t * > prop_ptrs_t
Definition: node.hpp:219
void set_properties(const uhd::device_addr_t &props, const size_t instance=0)
forwarding_policy_t
Types of property/action forwarding for those not defined by the block itself.
Definition: node.hpp:45
virtual size_t get_num_input_ports() const =0
void add_property_resolver(prop_ptrs_t &&inputs, prop_ptrs_t &&outputs, resolver_fn_t &&resolver_fn)
std::function< void(const res_source_info &, action_info::sptr)> action_handler_t
Definition: node.hpp:40
virtual void clear_command_time(const size_t instance)
static dirtifier_t ALWAYS_DIRTY
A dirtifyer object, useful for properties that always need updating.
Definition: node.hpp:465
virtual uhd::time_spec_t get_command_time(const size_t instance) const
std::vector< std::string > get_property_ids() const
void set_prop_forwarding_policy(forwarding_policy_t policy, const std::string &prop_id="")
Definition: property.hpp:26
access_t
Definition: property.hpp:28
Definition: time_spec.hpp:31
std::unique_ptr< scope_exit > uptr
Definition: scope_exit.hpp:25
#define UHD_API
Definition: config.h:70
Definition: build_info.hpp:12
std::shared_ptr< action_info > sptr
Definition: actions.hpp:32
Definition: res_source_info.hpp:18
source_t
Definition: res_source_info.hpp:21