16#include <nlohmann/json.hpp>
18using json = nlohmann::json;
25 std::unique_ptr<NumericAddressedRegisterCatalogue> registerMapPointer,
26 const std::string& dataConsistencyKeyDescriptor)
27 : _registerMapPointer(
std::move(registerMapPointer)), _registerMap(*_registerMapPointer) {
29 if(!mapFileName.empty()) {
33 if(!dataConsistencyKeyDescriptor.empty()) {
36 auto jdescr = nlohmann::json::parse(dataConsistencyKeyDescriptor);
37 for(
const auto& el : jdescr.items()) {
41 catch(json::parse_error& e) {
42 throw ChimeraTK::logic_error(std::format(
"Parsing DataConsistencyKeys parameter '{}' results in JSON error: {}",
43 dataConsistencyKeyDescriptor, e.what()));
57 if(components.size() != 3) {
60 auto bar = std::stoi(components[1]);
61 size_t pos = components[2].find_first_of(
'*');
62 auto address = std::stoi(components[2].substr(0, pos));
64 if(pos != std::string::npos) {
65 nBytes = std::stoi(components[2].substr(pos + 1));
68 nBytes =
sizeof(int32_t);
70 auto nElements = nBytes /
sizeof(int32_t);
71 if(nBytes == 0 || nBytes %
sizeof(int32_t) != 0) {
81 [[maybe_unused]] int32_t* data, [[maybe_unused]]
size_t sizeInBytes) {
82 throw ChimeraTK::logic_error(
"NumericAddressedBackend: internal error: interface read() called w/ 32bit address");
88 [[maybe_unused]] int32_t
const* data, [[maybe_unused]]
size_t sizeInBytes) {
89 throw ChimeraTK::logic_error(
"NumericAddressedBackend: internal error: interface write() called w/ 32bit address");
96#pragma GCC diagnostic push
97#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
98 read(
static_cast<uint8_t
>(bar),
static_cast<uint32_t
>(address), data, sizeInBytes);
99#pragma GCC diagnostic pop
105#pragma GCC diagnostic push
106#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
107 write(
static_cast<uint8_t
>(bar),
static_cast<uint32_t
>(address), data, sizeInBytes);
108#pragma GCC diagnostic pop
115 return bar <= 5 || bar == 13;
120 template<
typename UserType>
121 boost::shared_ptr<NDRegisterAccessor<UserType>> NumericAddressedBackend::getRegisterAccessor_impl(
128 "Register " + registerPathName +
" does not support AccessMode::wait_for_new_data.");
132 boost::static_pointer_cast<NumericAddressedBackend>(shared_from_this()),
133 registerInfo.getQualifiedAsyncId().front(), _asyncIsActive, registerPathName, numberOfWords,
134 wordOffsetInRegister, flags);
136 return getSyncRegisterAccessor<UserType>(registerPathName, numberOfWords, wordOffsetInRegister, flags);
141 template<
typename UserType>
142 boost::shared_ptr<NDRegisterAccessor<UserType>> NumericAddressedBackend::getSyncRegisterAccessor(
143 const RegisterPath& registerPathName,
size_t numberOfWords,
size_t wordOffsetInRegister, AccessModeFlags flags) {
144 boost::shared_ptr<NDRegisterAccessor<UserType>> accessor;
147 if(registerInfo.doubleBuffer == std::nullopt) {
149 if(registerInfo.getNumberOfDimensions() <= 1) {
154 accessor = boost::shared_ptr<NDRegisterAccessor<UserType>>(
155 new NumericAddressedBackendRegisterAccessor<UserType, true>(
156 shared_from_this(), registerPathName, numberOfWords, wordOffsetInRegister, flags));
159 accessor = boost::shared_ptr<NDRegisterAccessor<UserType>>(
160 new NumericAddressedBackendRegisterAccessor<UserType, false>(
161 shared_from_this(), registerPathName, numberOfWords, wordOffsetInRegister, flags));
165 if constexpr(!std::is_same<UserType, std::string>::value) {
166 throw ChimeraTK::logic_error(
"NumericAddressedBackend: ASCII data must be read with std::string UserType.");
169 accessor = boost::shared_ptr<NDRegisterAccessor<UserType>>(
new NumericAddressedBackendASCIIAccessor(
170 shared_from_this(), registerPathName, numberOfWords, wordOffsetInRegister, flags));
174 throw ChimeraTK::logic_error(
"NumericAddressedBackend: trying to get accessor for unsupported data type");
179 flags.checkForUnknownFlags({});
181 boost::shared_ptr<NDRegisterAccessor<UserType>>(
new NumericAddressedBackendMuxedRegisterAccessor<UserType>(
182 registerPathName, numberOfWords, wordOffsetInRegister, shared_from_this()));
187 const auto& enableRegPath = registerInfo.doubleBuffer->enableRegisterPath;
188 auto& controlState = _doubleBufferMutexMap[enableRegPath];
190 controlState = std::make_shared<detail::CountedRecursiveMutex>();
192 accessor = boost::make_shared<DoubleBufferAccessor<UserType>>(*registerInfo.doubleBuffer, shared_from_this(),
193 controlState, registerPathName, numberOfWords, wordOffsetInRegister, flags);
195 accessor->setExceptionBackend(shared_from_this());
202 _asyncIsActive =
true;
205 auto activateDomain = [
this](
size_t key, boost::shared_ptr<async::Domain>& domain) {
206 auto domainImpl = boost::dynamic_pointer_cast<async::DomainImpl<std::nullptr_t>>(domain);
212 subscriptionDone.wait();
213 domainImpl->activate(
nullptr);
224 std::promise<void> subscriptionDonePromise;
225 subscriptionDonePromise.set_value();
226 return subscriptionDonePromise.get_future();
232 _asyncIsActive =
false;
254 _asyncIsActive =
false;
#define FILL_VIRTUAL_FUNCTION_TEMPLATE_VTABLE(functionName)
Fill the vtable of a virtual function template defined with DEFINE_VIRTUAL_FUNCTION_TEMPLATE.
Set of AccessMode flags with additional functionality for an easier handling.
bool has(AccessMode flag) const
Check if a certain flag is in the set.
async::DomainsContainer _asyncDomainsContainer
Container for async::Domains to support wait_for_new_data.
static std::pair< NumericAddressedRegisterCatalogue, MetadataCatalogue > parse(const std::string &fileName)
Performs parsing of specified MAP file, resulting in catalogue objects describing all registers and m...
MetadataCatalogue _metadataCatalogue
metadata catalogue
RegisterCatalogue getRegisterCatalogue() const override
Return the register catalogue with detailed information on all registers.
virtual bool barIndexValid(uint64_t bar)
Function to be implemented by the backends.
NumericAddressedRegisterCatalogue & _registerMap
NumericAddressedRegisterInfo getRegisterInfo(const RegisterPath ®isterPathName)
getRegisterInfo returns a NumericAddressedRegisterInfo object for the given register.
NumericAddressedBackend(const std::string &mapFileName="", std::unique_ptr< NumericAddressedRegisterCatalogue > registerMapPointer=std::make_unique< NumericAddressedRegisterCatalogue >(), const std::string &dataConsistencyKeyDescriptor="")
void setExceptionImpl() noexcept override
Turn off the internal variable which remembers that async is active.
void close() final
Deactivates all asynchronous accessors and calls closeImpl().
MetadataCatalogue getMetadataCatalogue() const override
Return the device metadata catalogue.
void activateAsyncRead() noexcept override
Activate asyncronous read for all transfer elements where AccessMode::wait_for_new_data is set.
virtual void closeImpl()
All backends derrived from NumericAddressedBackend must implement closeImpl() instead of close.
virtual void write(uint64_t bar, uint64_t address, int32_t const *data, size_t sizeInBytes)
Write function to be implemented by backends.
virtual std::future< void > activateSubscription(uint32_t interruptNumber, boost::shared_ptr< async::DomainImpl< std::nullptr_t > > asyncDomain)
Activate/create the subscription for a given interrupt (for instance by starting the according interr...
virtual void read(uint64_t bar, uint64_t address, int32_t *data, size_t sizeInBytes)
Read function to be implemented by backends.
void addDataConsistencyRealm(const RegisterPath ®isterPath, const std::string &realmName)
NumericAddressedRegisterInfo getBackendRegister(const RegisterPath ®isterPathName) const override
Note: Override this function if backend has "hidden" registers which are not added to the map and hen...
std::unique_ptr< BackendRegisterCatalogueBase > clone() const override
Create deep copy of the catalogue.
Catalogue of register information.
Class to store a register path name.
std::vector< std::string > getComponents() const
split path into components
bool startsWith(const RegisterPath &compare) const
check if the register path starts with the given path
boost::shared_ptr< AsyncNDRegisterAccessor< UserDataType > > subscribe(boost::shared_ptr< BackendType > backend, size_t domainId, bool activate, RegisterPath name, size_t numberOfWords, size_t wordOffsetInRegister, AccessModeFlags flags)
Get an accessor from a particular domain.
void forEach(const std::function< void(size_t, boost::shared_ptr< Domain > &)> &executeMe)
Iterate all Domains under the container lock.
Exception thrown when a logic error has occured.
RegisterPath BAR()
The numeric_address::BAR() function can be used to directly access registers by numeric addresses,...
@ wait_for_new_data
Make any read blocking until new data has arrived since the last read.
@ raw
Raw access: disable any possible conversion from the original hardware data type into the given UserT...