20#include <nlohmann/json.hpp>
24using json = nlohmann::json;
31 std::unique_ptr<NumericAddressedRegisterCatalogue> registerMapPointer,
32 const std::string& dataConsistencyKeyDescriptor)
33 : _registerMapPointer(
std::move(registerMapPointer)), _registerMap(*_registerMapPointer) {
35 if(!mapFileName.empty()) {
40 if(!dataConsistencyKeyDescriptor.empty()) {
43 auto jdescr = nlohmann::json::parse(dataConsistencyKeyDescriptor);
44 for(
const auto& el : jdescr.items()) {
48 catch(json::parse_error& e) {
49 throw ChimeraTK::logic_error(std::format(
"Parsing DataConsistencyKeys parameter '{}' results in JSON error: {}",
50 dataConsistencyKeyDescriptor, e.what()));
57 std::string NumericAddressedBackend::resolveMapFileName(
const std::string& mapFileName) {
59 if(mapFileName[0] ==
'/') {
60 if(std::filesystem::exists(mapFileName)) {
61 return std::filesystem::canonical(mapFileName).string();
72 if(std::filesystem::exists(candidateA)) {
73 return std::filesystem::canonical(candidateA).string();
78 if(std::filesystem::exists(candidateB)) {
79 return std::filesystem::canonical(candidateB).string();
95 [[maybe_unused]] int32_t* data, [[maybe_unused]]
size_t sizeInBytes) {
96 throw ChimeraTK::logic_error(
"NumericAddressedBackend: internal error: interface read() called w/ 32bit address");
102 [[maybe_unused]] int32_t
const* data, [[maybe_unused]]
size_t sizeInBytes) {
103 throw ChimeraTK::logic_error(
"NumericAddressedBackend: internal error: interface write() called w/ 32bit address");
110#pragma GCC diagnostic push
111#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
112 read(
static_cast<uint8_t
>(bar),
static_cast<uint32_t
>(address), data, sizeInBytes);
113#pragma GCC diagnostic pop
119#pragma GCC diagnostic push
120#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
121 write(
static_cast<uint8_t
>(bar),
static_cast<uint32_t
>(address), data, sizeInBytes);
122#pragma GCC diagnostic pop
129 return bar <= 5 || bar == 13;
134 template<
typename UserType>
135 boost::shared_ptr<NDRegisterAccessor<UserType>> NumericAddressedBackend::getRegisterAccessor_impl(
142 "Register " + registerPathName +
" does not support AccessMode::wait_for_new_data.");
146 boost::static_pointer_cast<NumericAddressedBackend>(shared_from_this()),
147 registerInfo.getQualifiedAsyncId().front(), _asyncIsActive, registerPathName, numberOfWords,
148 wordOffsetInRegister, flags);
150 return getSyncRegisterAccessor<UserType>(registerPathName, numberOfWords, wordOffsetInRegister, flags);
155 template<
typename UserType>
156 boost::shared_ptr<NDRegisterAccessor<UserType>> NumericAddressedBackend::getSyncRegisterAccessor(
157 const RegisterPath& registerPathName,
size_t numberOfWords,
size_t wordOffsetInRegister, AccessModeFlags flags) {
158 boost::shared_ptr<NDRegisterAccessor<UserType>> accessor;
161 if(registerInfo.doubleBuffer == std::nullopt) {
163 if(registerInfo.getNumberOfDimensions() <= 1) {
164 if(registerInfo.isBitRange) {
165 if(numberOfWords == 0) {
166 numberOfWords = registerInfo.nElements == 0 ? 1 : registerInfo.nElements;
168 size_t nParentElements = registerInfo.nElements == 0 ? 1 : registerInfo.nElements;
169 if(numberOfWords + wordOffsetInRegister > nParentElements) {
171 std::to_string(numberOfWords + wordOffsetInRegister) +
" exceeds register size of " +
178 uint64_t targetSizeBytes = nParentElements * registerInfo.elementPitchBits / 8;
179 uint32_t wordWidth = registerInfo.channels.front().getRawType().getNumberOfBytes() * 8;
183 auto target = getSyncRegisterAccessor<uint64_t>(targetRegisterPath, nParentElements, 0, {});
186 return boost::make_shared<detail::BitRangeAccessorDecorator<UserType, true>>(
187 shared_from_this(), targetRegisterPath, target, registerInfo, numberOfWords, wordOffsetInRegister);
189 return boost::make_shared<detail::BitRangeAccessorDecorator<UserType, false>>(
190 shared_from_this(), targetRegisterPath, target, registerInfo, numberOfWords, wordOffsetInRegister);
196 accessor = boost::shared_ptr<NDRegisterAccessor<UserType>>(
197 new NumericAddressedBackendRegisterAccessor<UserType, true>(
198 shared_from_this(), registerPathName, numberOfWords, wordOffsetInRegister, flags));
201 accessor = boost::shared_ptr<NDRegisterAccessor<UserType>>(
202 new NumericAddressedBackendRegisterAccessor<UserType, false>(
203 shared_from_this(), registerPathName, numberOfWords, wordOffsetInRegister, flags));
207 if constexpr(!std::is_same<UserType, std::string>::value) {
208 throw ChimeraTK::logic_error(
"NumericAddressedBackend: ASCII data must be read with std::string UserType.");
211 accessor = boost::shared_ptr<NDRegisterAccessor<UserType>>(
new NumericAddressedBackendASCIIAccessor(
212 shared_from_this(), registerPathName, numberOfWords, wordOffsetInRegister, flags));
216 throw ChimeraTK::logic_error(
"NumericAddressedBackend: trying to get accessor for unsupported data type");
221 flags.checkForUnknownFlags({});
223 boost::shared_ptr<NDRegisterAccessor<UserType>>(
new NumericAddressedBackendMuxedRegisterAccessor<UserType>(
224 registerPathName, numberOfWords, wordOffsetInRegister, shared_from_this()));
229 if(numberOfWords == 0) {
230 numberOfWords = registerInfo.getNumberOfElements();
232 const auto& enableRegPath = registerInfo.doubleBuffer->enableRegisterPath;
233 auto& controlState = _doubleBufferMutexMap[enableRegPath];
235 controlState = std::make_shared<detail::CountedRecursiveMutex>();
237 accessor = boost::make_shared<DoubleBufferAccessor<UserType>>(*registerInfo.doubleBuffer, shared_from_this(),
238 controlState, registerPathName, numberOfWords, wordOffsetInRegister, flags);
240 accessor->setExceptionBackend(shared_from_this());
247 _asyncIsActive =
true;
250 auto activateDomain = [
this](
size_t key, boost::shared_ptr<async::Domain>& domain) {
251 auto domainImpl = boost::dynamic_pointer_cast<async::DomainImpl<std::nullptr_t>>(domain);
257 subscriptionDone.wait();
258 domainImpl->activate(
nullptr);
269 std::promise<void> subscriptionDonePromise;
270 subscriptionDonePromise.set_value();
271 return subscriptionDonePromise.get_future();
277 _asyncIsActive =
false;
299 _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.
static BackendFactory & getInstance()
Static function to get an instance of factory.
std::string getDMapFilePath()
Returns the _DMapFilePath.
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.
std::string _resolvedMapFileName
The resolved absolute path of the map file used for parsing and for any derived naming (e....
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.
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,...
std::string extractDirectory(std::string const &path)
Returns the path to the directory containing the file provided as the input parameter.
std::string concatenatePaths(const std::string &path1, const std::string &path2)
Concatenates two given paths using custom rules.
std::string convertToAbsolutePath(std::string const &relativePath)
Converts a relative path to its absolute path.
This header collects some conversion routines which should be useful both for ApplicationCore Python ...
@ 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...
std::string to_string(const std::string &v)