ChimeraTK-DeviceAccess  03.18.00
DummyBackend.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Deutsches Elektronen-Synchrotron DESY, MSK, ChimeraTK Project <chimeratk-support@desy.de>
2 // SPDX-License-Identifier: LGPL-3.0-or-later
3 #pragma once
4 
5 #include "DummyBackendBase.h"
6 #include "Exception.h"
7 
8 #include <boost/function.hpp>
9 
10 #include <list>
11 #include <map>
12 #include <mutex>
13 #include <set>
14 #include <vector>
15 
16 namespace ChimeraTK {
17 
18  // forward declarations
19  template<typename T>
21 
22  template<typename T>
24 
26 
45  class DummyBackend : public DummyBackendBase {
46  public:
47  explicit DummyBackend(const std::string& mapFileName);
48  ~DummyBackend() override = default;
49 
50  void open() override;
51 
58  void closeImpl() override;
59 
60  using DummyBackendBase::read; // use the 32 bit version from the base class
61  using DummyBackendBase::write; // use the 32 bit version from the base class
62  void read(uint64_t bar, uint64_t address, int32_t* data, size_t sizeInBytes) override;
63  void write(uint64_t bar, uint64_t address, int32_t const* data, size_t sizeInBytes) override;
64 
65  std::string readDeviceInfo() override;
66 
67  static boost::shared_ptr<DeviceBackend> createInstance(
68  std::string address, std::map<std::string, std::string> parameters);
69 
77  DummyRegisterRawAccessor getRawAccessor(const std::string& module, const std::string& register_name);
78 
79  VersionNumber triggerInterrupt(uint32_t interruptNumber) override;
80 
81  protected:
82  struct AddressRange {
83  const uint64_t offset;
84  const uint32_t sizeInBytes;
85  const uint64_t bar;
86 
87  AddressRange(uint64_t bar_, uint64_t address, size_t sizeInBytes_)
88  : offset(address), sizeInBytes(sizeInBytes_), bar(bar_) {}
89 
91  : offset(info.address), sizeInBytes(info.nElements * info.elementPitchBits / 8), bar(info.bar) {
92  assert(info.elementPitchBits % 8 == 0);
93  }
94 
95  bool operator<(AddressRange const& right) const {
96  return (bar == right.bar ? (offset < right.offset) : bar < right.bar);
97  }
98  };
99 
101  std::string _mapFile;
102 
103  std::map<uint64_t, std::vector<int32_t>> _barContents;
104  std::set<std::pair<uint64_t, uint64_t>> _readOnlyAddresses; // bar/address combinations which are read only
105  std::multimap<AddressRange, boost::function<void(void)>> _writeCallbackFunctions;
106  std::mutex mutex;
107 
108  void resizeBarContents();
109 
111  std::list<boost::function<void(void)>> findCallbackFunctionsForAddressRange(AddressRange addressRange);
112 
113  void setReadOnly(uint64_t bar, uint64_t address, size_t sizeInWords);
114  void setReadOnly(AddressRange addressRange);
115  bool isReadOnly(uint64_t bar, uint64_t address) const;
116  void setWriteCallbackFunction(AddressRange addressRange, boost::function<void(void)> const& writeCallbackFunction);
119  bool isWriteRangeOverlap(AddressRange firstRange, AddressRange secondRange);
120 
124  void writeRegisterWithoutCallback(uint64_t bar, uint64_t address, int32_t data);
125 
127  template<typename T>
128  friend class DummyRegisterAccessor;
129 
130  template<typename T>
132 
134  friend class SharedDummyBackend;
135 
136  static std::string convertPathRelativeToDmapToAbs(std::string const& mapfileName);
137  };
138 
139 } // namespace ChimeraTK
ChimeraTK::DummyBackendBase::read
void read([[maybe_unused]] uint8_t bar, [[maybe_unused]] uint32_t address, [[maybe_unused]] int32_t *data, [[maybe_unused]] size_t sizeInBytes) final
You cannot override the read version with 32 bit address any more.
Definition: DummyBackendBase.h:55
ChimeraTK::DummyRegisterAccessor
Register accessor for accessing single word or 1D array registers internally of a DummyBackend implem...
Definition: DummyBackend.h:20
ChimeraTK::DummyBackendBase
Base class for DummyBackends, provides common functionality.
Definition: DummyBackendBase.h:31
ChimeraTK::DummyBackend::write
void write(uint64_t bar, uint64_t address, int32_t const *data, size_t sizeInBytes) override
Write function to be implemented by backends.
Definition: DummyBackend.cc:61
ChimeraTK::DummyBackend::DummyBackend
DummyBackend(const std::string &mapFileName)
Definition: DummyBackend.cc:17
ChimeraTK::SharedDummyBackend
The shared dummy device opens a mapping file defining the registers and implements them in shared mem...
Definition: SharedDummyBackend.h:47
ChimeraTK::DummyBackend::findCallbackFunctionsForAddressRange
std::list< boost::function< void(void)> > findCallbackFunctionsForAddressRange(AddressRange addressRange)
Definition: DummyBackend.cc:114
ChimeraTK::NumericAddressedRegisterInfo
Definition: NumericAddressedRegisterCatalogue.h:15
ChimeraTK::DummyMultiplexedRegisterAccessor
Register accessor for accessing multiplexed 2D array registers internally of a DummyBackend implement...
Definition: DummyBackend.h:23
ChimeraTK::DummyBackend::AddressRange::operator<
bool operator<(AddressRange const &right) const
Definition: DummyBackend.h:95
ChimeraTK::DummyBackend::writeRegisterWithoutCallback
void writeRegisterWithoutCallback(uint64_t bar, uint64_t address, int32_t data)
Not write-protected function for internal use only.
Definition: DummyBackend.cc:44
ChimeraTK::DummyBackend::isReadOnly
bool isReadOnly(uint64_t bar, uint64_t address) const
Definition: DummyBackend.cc:96
ChimeraTK::DummyBackend::_readOnlyAddresses
std::set< std::pair< uint64_t, uint64_t > > _readOnlyAddresses
Definition: DummyBackend.h:104
ChimeraTK::DummyBackend::setWriteCallbackFunction
void setWriteCallbackFunction(AddressRange addressRange, boost::function< void(void)> const &writeCallbackFunction)
Definition: DummyBackend.cc:100
ChimeraTK::DummyBackend::mutex
std::mutex mutex
Definition: DummyBackend.h:106
ChimeraTK::DummyBackend::resizeBarContents
void resizeBarContents()
Definition: DummyBackend.cc:27
ChimeraTK::DummyBackend::AddressRange
Definition: DummyBackend.h:82
ChimeraTK::DummyBackend::open
void open() override
Open the device.
Definition: DummyBackend.cc:21
ChimeraTK::DummyBackend::getRawAccessor
DummyRegisterRawAccessor getRawAccessor(const std::string &module, const std::string &register_name)
Get a raw accessor to the underlying memory with the convenience of using register names.
Definition: DummyBackend.cc:182
ChimeraTK::DummyBackend::_writeCallbackFunctions
std::multimap< AddressRange, boost::function< void(void)> > _writeCallbackFunctions
Definition: DummyBackend.h:105
ChimeraTK::DummyBackend::runWriteCallbackFunctionsForAddressRange
void runWriteCallbackFunctionsForAddressRange(AddressRange addressRange)
Definition: DummyBackend.cc:106
ChimeraTK::DummyBackend::AddressRange::bar
const uint64_t bar
Definition: DummyBackend.h:85
ChimeraTK::DummyBackendBase::write
void write([[maybe_unused]] uint8_t bar, [[maybe_unused]] uint32_t address, [[maybe_unused]] int32_t const *data, [[maybe_unused]] size_t sizeInBytes) final
You cannot override the write version with 32 bit address any more.
Definition: DummyBackendBase.h:63
ChimeraTK::DummyBackend::setReadOnly
void setReadOnly(uint64_t bar, uint64_t address, size_t sizeInWords)
Definition: DummyBackend.cc:86
ChimeraTK::DummyBackend::AddressRange::sizeInBytes
const uint32_t sizeInBytes
Definition: DummyBackend.h:84
ChimeraTK::DummyBackend::_barContents
std::map< uint64_t, std::vector< int32_t > > _barContents
Definition: DummyBackend.h:103
ChimeraTK::DummyBackend::read
void read(uint64_t bar, uint64_t address, int32_t *data, size_t sizeInBytes) override
Read function to be implemented by backends.
Definition: DummyBackend.cc:49
ChimeraTK::DummyRegisterRawAccessor
Accessor for raw 32 bit integer access to the underlying memory space.
Definition: DummyRegisterAccessor.h:324
ChimeraTK::NumericAddressedRegisterInfo::elementPitchBits
uint32_t elementPitchBits
Distance in bits (!) between two elements (of the same channel)
Definition: NumericAddressedRegisterCatalogue.h:111
ChimeraTK::DummyBackend::AddressRange::AddressRange
AddressRange(uint64_t bar_, uint64_t address, size_t sizeInBytes_)
Definition: DummyBackend.h:87
ChimeraTK::DummyBackend::~DummyBackend
~DummyBackend() override=default
ChimeraTK::DummyBackend::AddressRange::offset
const uint64_t offset
Definition: DummyBackend.h:83
ChimeraTK::VersionNumber
Class for generating and holding version numbers without exposing a numeric representation.
Definition: VersionNumber.h:23
ChimeraTK::DummyBackend
The dummy device opens a mapping file instead of a device, and implements all registers defined in th...
Definition: DummyBackend.h:45
ChimeraTK::DummyBackend::triggerInterrupt
VersionNumber triggerInterrupt(uint32_t interruptNumber) override
Simulate the arrival of an interrupt.
Definition: DummyBackend.cc:186
ChimeraTK::DummyBackend::convertPathRelativeToDmapToAbs
static std::string convertPathRelativeToDmapToAbs(std::string const &mapfileName)
Definition: DummyBackend.cc:174
ChimeraTK::DummyBackend::_mapFile
std::string _mapFile
name of the map file
Definition: DummyBackend.h:101
Exception.h
ChimeraTK::DummyBackend::isWriteRangeOverlap
bool isWriteRangeOverlap(AddressRange firstRange, AddressRange secondRange)
returns true if the ranges overlap and at least one of the overlapping registers can be written
Definition: DummyBackend.cc:138
ChimeraTK::DummyBackend::readDeviceInfo
std::string readDeviceInfo() override
Return a device information string containing hardware details like the firmware version number or th...
Definition: DummyBackend.cc:80
DummyBackendBase.h
ChimeraTK
Definition: DummyBackend.h:16
ChimeraTK::DummyBackend::AddressRange::AddressRange
AddressRange(const NumericAddressedRegisterInfo &info)
Definition: DummyBackend.h:90
ChimeraTK::DummyBackend::createInstance
static boost::shared_ptr< DeviceBackend > createInstance(std::string address, std::map< std::string, std::string > parameters)
Definition: DummyBackend.cc:159
ChimeraTK::DummyBackend::closeImpl
void closeImpl() override
This closes the device, clears all internal registers, read-only settings and callback functions.
Definition: DummyBackend.cc:38