19#include <nlohmann/json.hpp>
23using json = nlohmann::json;
30 std::unique_ptr<NumericAddressedRegisterCatalogue> registerMapPointer,
31 const std::string& dataConsistencyKeyDescriptor)
32 : _registerMapPointer(
std::move(registerMapPointer)), _registerMap(*_registerMapPointer) {
34 if(!mapFileName.empty()) {
39 if(!dataConsistencyKeyDescriptor.empty()) {
42 auto jdescr = nlohmann::json::parse(dataConsistencyKeyDescriptor);
43 for(
const auto& el : jdescr.items()) {
47 catch(json::parse_error& e) {
48 throw ChimeraTK::logic_error(std::format(
"Parsing DataConsistencyKeys parameter '{}' results in JSON error: {}",
49 dataConsistencyKeyDescriptor, e.what()));
56 std::string NumericAddressedBackend::resolveMapFileName(
const std::string& mapFileName) {
58 if(mapFileName[0] ==
'/') {
59 if(std::filesystem::exists(mapFileName)) {
60 return std::filesystem::canonical(mapFileName).string();
71 if(std::filesystem::exists(candidateA)) {
72 return std::filesystem::canonical(candidateA).string();
77 if(std::filesystem::exists(candidateB)) {
78 return std::filesystem::canonical(candidateB).string();
94 [[maybe_unused]] int32_t* data, [[maybe_unused]]
size_t sizeInBytes) {
95 throw ChimeraTK::logic_error(
"NumericAddressedBackend: internal error: interface read() called w/ 32bit address");
101 [[maybe_unused]] int32_t
const* data, [[maybe_unused]]
size_t sizeInBytes) {
102 throw ChimeraTK::logic_error(
"NumericAddressedBackend: internal error: interface write() called w/ 32bit address");
109#pragma GCC diagnostic push
110#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
111 read(
static_cast<uint8_t
>(bar),
static_cast<uint32_t
>(address), data, sizeInBytes);
112#pragma GCC diagnostic pop
118#pragma GCC diagnostic push
119#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
120 write(
static_cast<uint8_t
>(bar),
static_cast<uint32_t
>(address), data, sizeInBytes);
121#pragma GCC diagnostic pop
128 return bar <= 5 || bar == 13;
133 template<
typename UserType>
134 boost::shared_ptr<NDRegisterAccessor<UserType>> NumericAddressedBackend::getRegisterAccessor_impl(
141 "Register " + registerPathName +
" does not support AccessMode::wait_for_new_data.");
145 boost::static_pointer_cast<NumericAddressedBackend>(shared_from_this()),
146 registerInfo.getQualifiedAsyncId().front(), _asyncIsActive, registerPathName, numberOfWords,
147 wordOffsetInRegister, flags);
149 return getSyncRegisterAccessor<UserType>(registerPathName, numberOfWords, wordOffsetInRegister, flags);
154 template<
typename UserType>
155 boost::shared_ptr<NDRegisterAccessor<UserType>> NumericAddressedBackend::getSyncRegisterAccessor(
156 const RegisterPath& registerPathName,
size_t numberOfWords,
size_t wordOffsetInRegister, AccessModeFlags flags) {
157 boost::shared_ptr<NDRegisterAccessor<UserType>> accessor;
160 if(registerInfo.doubleBuffer == std::nullopt) {
162 if(registerInfo.getNumberOfDimensions() <= 1) {
163 if(registerInfo.isBitRange) {
164 if(numberOfWords == 0) {
165 numberOfWords = registerInfo.nElements == 0 ? 1 : registerInfo.nElements;
167 size_t nParentElements = registerInfo.nElements == 0 ? 1 : registerInfo.nElements;
168 if(numberOfWords + wordOffsetInRegister > nParentElements) {
170 std::to_string(numberOfWords + wordOffsetInRegister) +
" exceeds register size of " +
174 uint64_t targetSizeBytes = nParentElements * registerInfo.elementPitchBits / 8;
178 auto target = getSyncRegisterAccessor<uint64_t>(targetRegisterPath, nParentElements, 0, {});
181 return boost::make_shared<detail::BitRangeAccessorDecorator<UserType, true>>(
182 shared_from_this(), targetRegisterPath, target, registerInfo, numberOfWords, wordOffsetInRegister);
184 return boost::make_shared<detail::BitRangeAccessorDecorator<UserType, false>>(
185 shared_from_this(), targetRegisterPath, target, registerInfo, numberOfWords, wordOffsetInRegister);
191 accessor = boost::shared_ptr<NDRegisterAccessor<UserType>>(
192 new NumericAddressedBackendRegisterAccessor<UserType, true>(
193 shared_from_this(), registerPathName, numberOfWords, wordOffsetInRegister, flags));
196 accessor = boost::shared_ptr<NDRegisterAccessor<UserType>>(
197 new NumericAddressedBackendRegisterAccessor<UserType, false>(
198 shared_from_this(), registerPathName, numberOfWords, wordOffsetInRegister, flags));
202 if constexpr(!std::is_same<UserType, std::string>::value) {
203 throw ChimeraTK::logic_error(
"NumericAddressedBackend: ASCII data must be read with std::string UserType.");
206 accessor = boost::shared_ptr<NDRegisterAccessor<UserType>>(
new NumericAddressedBackendASCIIAccessor(
207 shared_from_this(), registerPathName, numberOfWords, wordOffsetInRegister, flags));
211 throw ChimeraTK::logic_error(
"NumericAddressedBackend: trying to get accessor for unsupported data type");
216 flags.checkForUnknownFlags({});
218 boost::shared_ptr<NDRegisterAccessor<UserType>>(
new NumericAddressedBackendMuxedRegisterAccessor<UserType>(
219 registerPathName, numberOfWords, wordOffsetInRegister, shared_from_this()));
224 const auto& enableRegPath = registerInfo.doubleBuffer->enableRegisterPath;
225 auto& controlState = _doubleBufferMutexMap[enableRegPath];
227 controlState = std::make_shared<detail::CountedRecursiveMutex>();
229 accessor = boost::make_shared<DoubleBufferAccessor<UserType>>(*registerInfo.doubleBuffer, shared_from_this(),
230 controlState, registerPathName, numberOfWords, wordOffsetInRegister, flags);
232 accessor->setExceptionBackend(shared_from_this());
239 _asyncIsActive =
true;
242 auto activateDomain = [
this](
size_t key, boost::shared_ptr<async::Domain>& domain) {
243 auto domainImpl = boost::dynamic_pointer_cast<async::DomainImpl<std::nullptr_t>>(domain);
249 subscriptionDone.wait();
250 domainImpl->activate(
nullptr);
261 std::promise<void> subscriptionDonePromise;
262 subscriptionDonePromise.set_value();
263 return subscriptionDonePromise.get_future();
269 _asyncIsActive =
false;
291 _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.
@ 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)