USRP Hardware Driver and USRP Manual Version: 4.0.0.0
UHD and USRP Manual
mock_block.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/config.hpp>
10#include <uhd/property_tree.hpp>
15#include <uhd/utils/log.hpp>
16#include <unordered_map>
17#include <boost/format.hpp>
18#include <vector>
19
20namespace uhd { namespace rfnoc {
21
27{
28public:
29 mock_reg_iface_t() = default;
30 virtual ~mock_reg_iface_t() = default;
31
32 /**************************************************************************
33 * API
34 *************************************************************************/
35 void poke32(uint32_t addr, uint32_t data, uhd::time_spec_t time, bool ack)
36 {
37 write_memory[addr] = data;
38 _poke_cb(addr, data, time, ack);
39 }
40
41 void multi_poke32(const std::vector<uint32_t> addrs,
42 const std::vector<uint32_t> data,
44 bool ack)
45 {
46 if (addrs.size() != data.size()) {
47 throw uhd::value_error("addrs and data vectors must be of the same length");
48 }
49 for (size_t i = 0; i < addrs.size(); i++) {
50 poke32(addrs[i], data[i], time, ack);
51 }
52 }
53
54 void block_poke32(uint32_t first_addr,
55 const std::vector<uint32_t> data,
56 uhd::time_spec_t timestamp,
57 bool ack)
58 {
59 for (size_t i = 0; i < data.size(); i++) {
60 poke32(first_addr + 4 * i, data[i], timestamp, ack);
61 }
62 }
63
64 uint32_t peek32(uint32_t addr, uhd::time_spec_t time)
65 {
66 _peek_cb(addr, time);
67 try {
68 return read_memory.at(addr);
69 } catch (const std::out_of_range&) {
71 str(boost::format("No data defined for address: 0x%04X") % addr));
72 }
73 }
74
75 std::vector<uint32_t> block_peek32(
76 uint32_t first_addr, size_t length, uhd::time_spec_t time)
77 {
78 std::vector<uint32_t> result(length, 0);
79 for (size_t i = 0; i < length; ++i) {
80 result[i] = peek32(first_addr + i * 4, time);
81 }
82 return result;
83 }
84
85 void poll32(uint32_t addr,
86 uint32_t data,
87 uint32_t mask,
88 uhd::time_spec_t /* timeout */,
90 bool /* ack */ = false)
91 {
92 if (force_timeout) {
93 throw uhd::op_timeout("timeout");
94 }
95
96 if ((peek32(addr, time) & mask) == data) {
97 UHD_LOG_INFO("MOCK_REG_IFACE", "poll32() successful at addr " << addr);
98 } else {
99 UHD_LOG_INFO("MOCK_REG_IFACE", "poll32() not successful at addr " << addr);
100 }
101 }
102
103 void sleep(uhd::time_spec_t /*duration*/, bool /*ack*/)
104 {
105 // nop
106 }
107
109 {
110 // nop
111 }
112
114 {
115 // nop
116 }
117
118 void set_policy(const std::string& name, const uhd::device_addr_t& args)
119 {
120 UHD_LOG_INFO("MOCK_REG_IFACE",
121 "Requested to set policy for " << name << " to " << args.to_string());
122 }
123
124 uint16_t get_src_epid() const
125 {
126 return 0;
127 }
128
129 uint16_t get_port_num() const
130 {
131 return 0;
132 }
133
134 bool force_timeout = false;
135
137 std::unordered_map<uint32_t, uint32_t> read_memory;
139 // been previously set.
140 std::unordered_map<uint32_t, uint32_t> write_memory;
141
142protected:
143 virtual void _poke_cb(
144 uint32_t /*addr*/, uint32_t /*data*/, uhd::time_spec_t /*time*/, bool /*ack*/)
145 {
146 }
147
148 virtual void _peek_cb(uint32_t /*addr*/, uhd::time_spec_t /*time*/) {}
149};
150
154{
155 friend class get_mock_block;
157 std::shared_ptr<mock_reg_iface_t> reg_iface;
158
161
163 // the register space is appropiately primed before doing so.
164 template <typename block_type = noc_block_base>
165 std::shared_ptr<block_type> get_block()
166 {
167 return std::dynamic_pointer_cast<block_type>(factory(std::move(make_args)));
168 }
169
172
173 // Note: The make args would ideally be captured by the factory function,
174 // but std::functions need to be CopyConstructible, and this struct doesn't,
175 // so it needs to live out here in the open.
177};
178
182 const size_t num_inputs = 1,
183 const size_t num_outputs = 1,
185 const size_t mtu = 8000,
186 const device_type_t device_id = ANY_DEVICE,
187 std::shared_ptr<mock_reg_iface_t> client_reg_iface = nullptr);
188
189}}; // namespace uhd::rfnoc
Definition: device_addr.hpp:38
std::string to_string(void) const
std::shared_ptr< property_tree > sptr
Definition: property_tree.hpp:217
Definition: mock_block.hpp:27
void poll32(uint32_t addr, uint32_t data, uint32_t mask, uhd::time_spec_t, uhd::time_spec_t time=uhd::time_spec_t::ASAP, bool=false)
Definition: mock_block.hpp:85
void multi_poke32(const std::vector< uint32_t > addrs, const std::vector< uint32_t > data, uhd::time_spec_t time, bool ack)
Definition: mock_block.hpp:41
std::vector< uint32_t > block_peek32(uint32_t first_addr, size_t length, uhd::time_spec_t time)
Definition: mock_block.hpp:75
std::unordered_map< uint32_t, uint32_t > write_memory
All peeks read from this map. A peek will fail if the address has not.
Definition: mock_block.hpp:140
void register_async_msg_handler(async_msg_callback_t)
Definition: mock_block.hpp:113
virtual void _peek_cb(uint32_t, uhd::time_spec_t)
Definition: mock_block.hpp:148
uint16_t get_port_num() const
Definition: mock_block.hpp:129
uint16_t get_src_epid() const
Definition: mock_block.hpp:124
void poke32(uint32_t addr, uint32_t data, uhd::time_spec_t time, bool ack)
Definition: mock_block.hpp:35
void sleep(uhd::time_spec_t, bool)
Definition: mock_block.hpp:103
uint32_t peek32(uint32_t addr, uhd::time_spec_t time)
Definition: mock_block.hpp:64
std::unordered_map< uint32_t, uint32_t > read_memory
All pokes end up writing to this map.
Definition: mock_block.hpp:137
void set_policy(const std::string &name, const uhd::device_addr_t &args)
Definition: mock_block.hpp:118
void register_async_msg_validator(async_msg_validator_t)
Definition: mock_block.hpp:108
virtual void _poke_cb(uint32_t, uint32_t, uhd::time_spec_t, bool)
Definition: mock_block.hpp:143
void block_poke32(uint32_t first_addr, const std::vector< uint32_t > data, uhd::time_spec_t timestamp, bool ack)
Definition: mock_block.hpp:54
virtual ~mock_reg_iface_t()=default
std::unique_ptr< make_args_t > make_args_ptr
Opaque pointer to the constructor arguments.
Definition: noc_block_base.hpp:53
std::shared_ptr< noc_block_base > sptr
Definition: noc_block_base.hpp:47
Definition: register_iface.hpp:28
std::function< void(uint32_t addr, const std::vector< uint32_t > &data, boost::optional< uint64_t >)> async_msg_callback_t
Definition: register_iface.hpp:56
std::function< bool(uint32_t addr, const std::vector< uint32_t > &data)> async_msg_validator_t
Definition: register_iface.hpp:43
Definition: time_spec.hpp:31
static constexpr double ASAP
Definition: time_spec.hpp:34
#define UHD_API
Definition: config.h:67
#define UHD_LOG_INFO(component,...)
Definition: log.h:55
uint16_t device_type_t
Device Type.
Definition: defaults.hpp:58
uint32_t noc_id_t
Definition: defaults.hpp:54
UHD_API mock_block_container get_mock_block(const noc_id_t noc_id, const size_t num_inputs=1, const size_t num_outputs=1, const uhd::device_addr_t &args=uhd::device_addr_t(), const size_t mtu=8000, const device_type_t device_id=ANY_DEVICE, std::shared_ptr< mock_reg_iface_t > client_reg_iface=nullptr)
UHD_INLINE data_t mask(const soft_reg_field_t field)
Definition: soft_register.hpp:87
Definition: build_info.hpp:12
Definition: exception.hpp:252
Definition: mock_block.hpp:154
uhd::property_tree::sptr tree
Reference to the prop tree object the block sees.
Definition: mock_block.hpp:160
std::shared_ptr< block_type > get_block()
Use this to retrieve a reference to the block controller. Make sure that.
Definition: mock_block.hpp:165
std::function< noc_block_base::sptr(noc_block_base::make_args_ptr)> factory
Factory to get the block. Use get_block() instead.
Definition: mock_block.hpp:171
std::shared_ptr< mock_reg_iface_t > reg_iface
Reference to the register interface object.
Definition: mock_block.hpp:157
noc_block_base::make_args_ptr make_args
Definition: mock_block.hpp:176
Definition: exception.hpp:133
Definition: exception.hpp:109