Loading...
Searching...
No Matches
mock_block.hpp
Go to the documentation of this file.
//
// Copyright 2019 Ettus Research, a National Instruments Brand
//
// SPDX-License-Identifier: GPL-3.0-or-later
//
#pragma once
#include <uhd/config.hpp>
#include <uhd/property_tree.hpp>
#include <uhd/rfnoc/defaults.hpp>
#include <uhd/rfnoc/mb_controller.hpp>
#include <uhd/rfnoc/noc_block_base.hpp>
#include <uhd/rfnoc/register_iface.hpp>
#include <uhd/types/time_spec.hpp>
#include <uhd/utils/log.hpp>
#include <unordered_map>
#include <boost/format.hpp>
#include <vector>
namespace uhd { namespace rfnoc {
class UHD_API mock_reg_iface_t : public register_iface
{
public:
mock_reg_iface_t() = default;
~mock_reg_iface_t() override = default;
/**************************************************************************
* API
*************************************************************************/
void poke32(uint32_t addr, uint32_t data, uhd::time_spec_t time, bool ack) override
{
write_memory[addr] = data;
_poke_cb(addr, data, time, ack);
}
void multi_poke32(const std::vector<uint32_t> addrs,
const std::vector<uint32_t> data,
uhd::time_spec_t time,
bool ack) override
{
if (addrs.size() != data.size()) {
throw uhd::value_error("addrs and data vectors must be of the same length");
}
for (size_t i = 0; i < addrs.size(); i++) {
poke32(addrs[i], data[i], time, ack);
}
}
void block_poke32(uint32_t first_addr,
const std::vector<uint32_t> data,
uhd::time_spec_t timestamp,
bool ack) override
{
for (size_t i = 0; i < data.size(); i++) {
poke32(first_addr + 4 * i, data[i], timestamp, ack);
}
}
uint32_t peek32(uint32_t addr, uhd::time_spec_t time) override
{
_peek_cb(addr, time);
try {
return read_memory.at(addr);
} catch (const std::out_of_range&) {
throw uhd::runtime_error(
str(boost::format("No data defined for address: 0x%04X") % addr));
}
}
std::vector<uint32_t> block_peek32(
uint32_t first_addr, size_t length, uhd::time_spec_t time) override
{
std::vector<uint32_t> result(length, 0);
for (size_t i = 0; i < length; ++i) {
result[i] = peek32(first_addr + i * 4, time);
}
return result;
}
void poll32(uint32_t addr,
uint32_t data,
uint32_t mask,
uhd::time_spec_t /* timeout */,
uhd::time_spec_t time = uhd::time_spec_t::ASAP,
bool /* ack */ = false) override
{
if (force_timeout) {
throw uhd::op_timeout("timeout");
}
if ((peek32(addr, time) & mask) == data) {
UHD_LOG_INFO("MOCK_REG_IFACE", "poll32() successful at addr " << addr);
} else {
UHD_LOG_INFO("MOCK_REG_IFACE", "poll32() not successful at addr " << addr);
}
}
void sleep(uhd::time_spec_t /*duration*/, bool /*ack*/) override
{
// nop
}
void register_async_msg_validator(async_msg_validator_t /*callback_f*/) override
{
// nop
}
void register_async_msg_handler(async_msg_callback_t /*callback_f*/) override
{
// nop
}
void set_policy(const std::string& name, const uhd::device_addr_t& args) override
{
UHD_LOG_INFO("MOCK_REG_IFACE",
"Requested to set policy for " << name << " to " << args.to_string());
}
uint16_t get_src_epid() const override
{
return 0;
}
uint16_t get_port_num() const override
{
return 0;
}
void define_custom_register_space(const uint32_t /*start_addr*/,
const uint32_t /*length*/,
std::function<void(uint32_t, uint32_t)> /*poke_fn*/,
std::function<uint32_t(uint32_t)> /*peek_fn*/) override
{
// nop
}
bool force_timeout = false;
std::unordered_map<uint32_t, uint32_t> read_memory;
// been previously set.
std::unordered_map<uint32_t, uint32_t> write_memory;
protected:
virtual void _poke_cb(
uint32_t /*addr*/, uint32_t /*data*/, uhd::time_spec_t /*time*/, bool /*ack*/)
{
}
virtual void _peek_cb(uint32_t /*addr*/, uhd::time_spec_t /*time*/) {}
};
struct UHD_API mock_block_container
{
friend class get_mock_block;
std::shared_ptr<mock_reg_iface_t> reg_iface;
uhd::property_tree::sptr tree;
// the register space is appropiately primed before doing so.
template <typename block_type = noc_block_base>
std::shared_ptr<block_type> get_block()
{
return std::dynamic_pointer_cast<block_type>(factory(std::move(make_args)));
}
std::function<noc_block_base::sptr(noc_block_base::make_args_ptr)> factory;
// Note: The make args would ideally be captured by the factory function,
// but std::functions need to be CopyConstructible, and this struct doesn't,
// so it needs to live out here in the open.
noc_block_base::make_args_ptr make_args;
};
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,
mb_controller::sptr mbc = nullptr);
}}; // namespace uhd::rfnoc
uhd::device_addr_t
Definition device_addr.hpp:39
uhd::device_addr_t::to_string
std::string to_string(void) const
uhd::property_tree::sptr
std::shared_ptr< property_tree > sptr
Definition property_tree.hpp:224
uhd::rfnoc::mb_controller::sptr
std::shared_ptr< mb_controller > sptr
Definition mb_controller.hpp:33
uhd::rfnoc::mock_reg_iface_t::mock_reg_iface_t
mock_reg_iface_t()=default
uhd::rfnoc::mock_reg_iface_t::get_src_epid
uint16_t get_src_epid() const override
Definition mock_block.hpp:125
uhd::rfnoc::mock_reg_iface_t::~mock_reg_iface_t
~mock_reg_iface_t() override=default
uhd::rfnoc::mock_reg_iface_t::write_memory
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:149
uhd::rfnoc::mock_reg_iface_t::block_poke32
void block_poke32(uint32_t first_addr, const std::vector< uint32_t > data, uhd::time_spec_t timestamp, bool ack) override
Definition mock_block.hpp:55
uhd::rfnoc::mock_reg_iface_t::block_peek32
std::vector< uint32_t > block_peek32(uint32_t first_addr, size_t length, uhd::time_spec_t time) override
Definition mock_block.hpp:76
uhd::rfnoc::mock_reg_iface_t::poll32
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) override
Definition mock_block.hpp:86
uhd::rfnoc::mock_reg_iface_t::multi_poke32
void multi_poke32(const std::vector< uint32_t > addrs, const std::vector< uint32_t > data, uhd::time_spec_t time, bool ack) override
Definition mock_block.hpp:42
uhd::rfnoc::mock_reg_iface_t::peek32
uint32_t peek32(uint32_t addr, uhd::time_spec_t time) override
Definition mock_block.hpp:65
uhd::rfnoc::mock_reg_iface_t::_peek_cb
virtual void _peek_cb(uint32_t, uhd::time_spec_t)
Definition mock_block.hpp:157
uhd::rfnoc::mock_reg_iface_t::set_policy
void set_policy(const std::string &name, const uhd::device_addr_t &args) override
Definition mock_block.hpp:119
uhd::rfnoc::mock_reg_iface_t::force_timeout
bool force_timeout
Definition mock_block.hpp:143
uhd::rfnoc::mock_reg_iface_t::read_memory
std::unordered_map< uint32_t, uint32_t > read_memory
All pokes end up writing to this map.
Definition mock_block.hpp:146
uhd::rfnoc::mock_reg_iface_t::register_async_msg_validator
void register_async_msg_validator(async_msg_validator_t) override
Definition mock_block.hpp:109
uhd::rfnoc::mock_reg_iface_t::define_custom_register_space
void define_custom_register_space(const uint32_t, const uint32_t, std::function< void(uint32_t, uint32_t)>, std::function< uint32_t(uint32_t)>) override
Definition mock_block.hpp:135
uhd::rfnoc::mock_reg_iface_t::sleep
void sleep(uhd::time_spec_t, bool) override
Definition mock_block.hpp:104
uhd::rfnoc::mock_reg_iface_t::_poke_cb
virtual void _poke_cb(uint32_t, uint32_t, uhd::time_spec_t, bool)
Definition mock_block.hpp:152
uhd::rfnoc::mock_reg_iface_t::poke32
void poke32(uint32_t addr, uint32_t data, uhd::time_spec_t time, bool ack) override
Definition mock_block.hpp:36
uhd::rfnoc::mock_reg_iface_t::register_async_msg_handler
void register_async_msg_handler(async_msg_callback_t) override
Definition mock_block.hpp:114
uhd::rfnoc::mock_reg_iface_t::get_port_num
uint16_t get_port_num() const override
Definition mock_block.hpp:130
uhd::rfnoc::noc_block_base::make_args_ptr
std::unique_ptr< make_args_t > make_args_ptr
Opaque pointer to the constructor arguments.
Definition noc_block_base.hpp:54
uhd::rfnoc::noc_block_base::sptr
std::shared_ptr< noc_block_base > sptr
Definition noc_block_base.hpp:48
uhd::rfnoc::register_iface
Definition register_iface.hpp:39
uhd::rfnoc::register_iface::async_msg_callback_t
std::function< void( uint32_t addr, const std::vector< uint32_t > &data, boost::optional< uint64_t >)> async_msg_callback_t
Definition register_iface.hpp:66
uhd::rfnoc::register_iface::async_msg_validator_t
std::function< bool(uint32_t addr, const std::vector< uint32_t > &data)> async_msg_validator_t
Definition register_iface.hpp:53
uhd::time_spec_t
Definition time_spec.hpp:31
uhd::time_spec_t::ASAP
static constexpr double ASAP
Definition time_spec.hpp:34
UHD_API
#define UHD_API
Definition config.h:87
config.hpp
defaults.hpp
UHD_LOG_INFO
#define UHD_LOG_INFO(component,...)
Definition log.h:53
log.hpp
mb_controller.hpp
uhd::rfnoc
Definition actions.hpp:24
uhd::rfnoc::device_type_t
uint16_t device_type_t
Device Type.
Definition defaults.hpp:56
uhd::rfnoc::noc_id_t
uint32_t noc_id_t
Definition defaults.hpp:52
uhd::rfnoc::get_mock_block
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, mb_controller::sptr mbc=nullptr)
uhd
Definition build_info.hpp:12
noc_block_base.hpp
property_tree.hpp
register_iface.hpp
uhd::op_timeout
Definition exception.hpp:251
uhd::rfnoc::mock_block_container
Definition mock_block.hpp:163
uhd::rfnoc::mock_block_container::tree
uhd::property_tree::sptr tree
Reference to the prop tree object the block sees.
Definition mock_block.hpp:169
uhd::rfnoc::mock_block_container::get_block
std::shared_ptr< block_type > get_block()
Use this to retrieve a reference to the block controller. Make sure that.
Definition mock_block.hpp:174
uhd::rfnoc::mock_block_container::factory
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:180
uhd::rfnoc::mock_block_container::reg_iface
std::shared_ptr< mock_reg_iface_t > reg_iface
Reference to the register interface object.
Definition mock_block.hpp:166
uhd::rfnoc::mock_block_container::make_args
noc_block_base::make_args_ptr make_args
Definition mock_block.hpp:185
uhd::rfnoc::mock_block_container::get_mock_block
friend class get_mock_block
Definition mock_block.hpp:164
uhd::runtime_error
Definition exception.hpp:132
uhd::value_error
Definition exception.hpp:108
time_spec.hpp