USRP Hardware Driver and Device Manual  Version: 4.10.0.0-0-g2af4ddb9
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:
38  enum class action_mode_t {
39  SYNC,
40  ASYNC
41  };
42 
43  using resolver_fn_t = std::function<void(void)>;
44  using resolve_callback_t = std::function<void(void)>;
45  using graph_mutex_callback_t = std::function<std::recursive_mutex&(void)>;
47  std::function<void(const res_source_info&, action_info::sptr)>;
49  std::function<void(const res_source_info&, action_info::sptr, action_mode_t)>;
51  std::unordered_map<res_source_info, std::vector<res_source_info>>;
52 
54  enum class forwarding_policy_t {
56  //(e.g., if it comes from input port 0, forward it to output port 0).
57  ONE_TO_ONE,
59  ONE_TO_FAN,
61  ONE_TO_ALL_IN,
63  ONE_TO_ALL_OUT,
65  ONE_TO_ALL,
67  DROP,
69  USE_MAP
70  };
71 
72  static const size_t ANY_PORT = size_t(~0);
73 
74  /**************************************************************************
75  * Structors
76  *************************************************************************/
77  node_t();
78 
79  virtual ~node_t() {}
80 
81  /******************************************
82  * Basic Operations
83  ******************************************/
85  // no two nodes cannot have the same ID.
86  //
87  // \returns The unique ID as a string
88  virtual std::string get_unique_id() const;
89 
96  virtual size_t get_num_input_ports() const = 0;
97 
104  virtual size_t get_num_output_ports() const = 0;
105 
106  /******************************************
107  * Property Specific
108  ******************************************/
109 
118  std::vector<std::string> get_property_ids() const;
119 
135  template <typename prop_data_t>
136  void set_property(
137  const std::string& id, const prop_data_t& val, const size_t instance = 0);
138 
171  void set_properties(const uhd::device_addr_t& props, const size_t instance = 0);
172 
190  template <typename prop_data_t>
191  const prop_data_t& get_property(
192  const std::string& id, const size_t instance = 0) /* mutable */;
193 
209  virtual void set_command_time(uhd::time_spec_t time, const size_t instance);
210 
215  virtual uhd::time_spec_t get_command_time(const size_t instance) const;
216 
222  virtual void clear_command_time(const size_t instance);
223 
224 protected:
225  /******************************************
226  * Internal Registration Functions
227  ******************************************/
228  using prop_ptrs_t = std::vector<property_base_t*>;
229 
251  property_base_t* prop, resolve_callback_t&& clean_callback = nullptr);
252 
288  prop_ptrs_t&& inputs, prop_ptrs_t&& outputs, resolver_fn_t&& resolver_fn);
289 
290  /**************************************************************************
291  * Property forwarding
292  *************************************************************************/
314  forwarding_policy_t policy, const std::string& prop_id = "");
315 
338 
350  template <typename prop_data_t>
351  void set_property(
352  const std::string& id, const prop_data_t& val, const res_source_info& src_info);
353 
365  template <typename prop_data_t>
366  const prop_data_t& get_property(
367  const std::string& id, const res_source_info& src_info) /* mutable */;
368 
369  /******************************************
370  * Internal action forwarding
371  ******************************************/
385  void register_action_handler(const std::string& id, action_handler_t&& handler);
386 
406  forwarding_policy_t policy, const std::string& action_key = "");
407 
431 
453  void post_action(const res_source_info& edge_info,
454  action_info::sptr action,
455  action_mode_t mode = action_mode_t::SYNC);
456 
457  /**************************************************************************
458  * Graph Interaction
459  *************************************************************************/
480  virtual bool check_topology(const std::vector<size_t>& connected_inputs,
481  const std::vector<size_t>& connected_outputs);
482 
488  virtual void shutdown();
489 
490  /**************************************************************************
491  * Attributes
492  *************************************************************************/
495 
496 private:
497  friend class node_accessor_t;
498 
503  property_base_t* _find_property(
504  res_source_info src_info, const std::string& id) const;
505 
511  uhd::utils::scope_exit::uptr _request_property_access(
512  property_base_t* prop, property_base_t::access_t access) const;
513 
518  template <typename PredicateType>
519  prop_ptrs_t filter_props(PredicateType&& predicate)
520  {
521  prop_ptrs_t filtered_props{};
522  for (const auto& type_prop_pair : _props) {
523  for (const auto& prop : type_prop_pair.second) {
524  if (predicate(prop)) {
525  filtered_props.push_back(prop);
526  }
527  }
528  }
529 
530  return filtered_props;
531  }
532 
540  property_base_t* inject_edge_property(
541  property_base_t* blueprint, res_source_info new_src_info);
542 
556  void init_props();
557 
581  void resolve_props();
582 
585  void resolve_all();
586 
592  void clean_props();
593 
597  void set_resolve_all_callback(resolve_callback_t&& resolver)
598  {
599  _resolve_all_cb = resolver;
600  }
601 
604  void clear_resolve_all_callback()
605  {
606  _resolve_all_cb = _default_resolve_all_cb;
607  }
608 
612  void set_graph_mutex_callback(graph_mutex_callback_t&& mutex)
613  {
614  _graph_mutex_cb = mutex;
615  }
616 
620  void clear_graph_mutex_callback()
621  {
622  _graph_mutex_cb = NULL;
623  }
624 
659  void forward_edge_property(
660  property_base_t* incoming_prop, const size_t incoming_port);
661 
662  /**************************************************************************
663  * Action-Related Methods
664  *************************************************************************/
668  void set_post_action_callback(post_action_handler_t&& post_handler)
669  {
670  _post_action_cb = std::move(post_handler);
671  }
672 
681  void receive_action(const res_source_info& src_info, action_info::sptr action);
682 
683  /**************************************************************************
684  * Private helpers
685  *************************************************************************/
687  bool _has_port(const res_source_info& port_info) const;
688 
690  template <typename prop_data_t>
691  void _set_property(
692  const std::string& id, const prop_data_t& val, const res_source_info& src_info);
693 
694  /****** Attributes *******************************************************/
696  // global property mutex, this only write-protects access to the property-
697  // related containers in this class.
698  mutable std::mutex _prop_mutex;
699 
701  std::unordered_map<res_source_info::source_t,
702  std::vector<property_base_t*>,
703  std::hash<size_t>>
704  _props;
705 
707  std::unordered_map<property_base_t*, resolve_callback_t> _clean_cb_registry;
708 
709  using property_resolver_t = std::tuple<prop_ptrs_t, prop_ptrs_t, resolver_fn_t>;
711  std::vector<property_resolver_t> _prop_resolvers;
712 
714  // This will return a global mutex to the graph. It is required to propagate
715  // properties on multithread applications.
716  graph_mutex_callback_t _graph_mutex_cb;
717 
719  // has changed, and that a property resolution needs to be performed.
720  resolve_callback_t _resolve_all_cb;
721 
723  // method.
724  const resolve_callback_t _default_resolve_all_cb = [this]() {
725  resolve_props();
726  clean_props();
727  };
728 
729 
731  // explicitly.
732  //
733  // Dynamic properties include properties defined in the block descriptor
734  // file, as well as new properties that get passed in during property
735  // propagation.
736  std::unordered_set<std::unique_ptr<property_base_t>> _dynamic_props;
737 
739  //
740  // The entry with the empty-string-key is the default policy.
741  std::unordered_map<std::string, forwarding_policy_t> _prop_fwd_policies{
742  {"", forwarding_policy_t::ONE_TO_ONE}};
743 
745  forwarding_map_t _prop_fwd_map;
746 
747  /**************************************************************************
748  * Action-related attributes
749  *************************************************************************/
750  mutable std::mutex _action_mutex;
751 
753  std::unordered_map<std::string, action_handler_t> _action_handlers;
754 
756  std::unordered_map<std::string, forwarding_policy_t> _action_fwd_policies{
757  {"", forwarding_policy_t::ONE_TO_ONE}};
758 
760  //
761  // The default callback will simply drop actions
762  post_action_handler_t _post_action_cb =
763  [](const res_source_info&, action_info::sptr, action_mode_t) { /* nop */ };
764 
766  forwarding_map_t _action_fwd_map;
767 
768  /**************************************************************************
769  * Other attributes
770  *************************************************************************/
771  std::vector<uhd::time_spec_t> _cmd_timespecs;
772 }; // class node_t
773 
774 }} /* namespace uhd::rfnoc */
775 
776 #include <uhd/rfnoc/node.ipp>
Definition: device_addr.hpp:39
Definition: dirtifier.hpp:19
Special class which may access nodes.
Definition: node_accessor.hpp:28
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:51
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::vector< property_base_t * > prop_ptrs_t
Definition: node.hpp:228
std::function< void(void)> resolver_fn_t
Definition: node.hpp:43
std::function< void(void)> resolve_callback_t
Definition: node.hpp:44
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)
virtual bool check_topology(const std::vector< size_t > &connected_inputs, const std::vector< size_t > &connected_outputs)
virtual ~node_t()
Definition: node.hpp:79
void set_action_forwarding_map(const forwarding_map_t &map)
virtual size_t get_num_output_ports() const =0
virtual void shutdown()
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:54
std::function< void(const res_source_info &, action_info::sptr, action_mode_t)> post_action_handler_t
Definition: node.hpp:49
action_mode_t
Action execution modes.
Definition: node.hpp:38
std::function< std::recursive_mutex &(void)> graph_mutex_callback_t
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:47
virtual void clear_command_time(const size_t instance)
void post_action(const res_source_info &edge_info, action_info::sptr action, action_mode_t mode=action_mode_t::SYNC)
static dirtifier_t ALWAYS_DIRTY
A dirtifyer object, useful for properties that always need updating.
Definition: node.hpp:494
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:26
#define UHD_API
Definition: config.h:87
Definition: build_info.hpp:12
std::shared_ptr< action_info > sptr
Definition: actions.hpp:39
Definition: res_source_info.hpp:18
source_t
Definition: res_source_info.hpp:21