ChimeraTK-DeviceAccess  03.18.00
DeviceInfoMap.cc
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 
4 #include "DeviceInfoMap.h"
5 
6 #include "Exception.h"
7 #include "predicates.h"
8 
9 #include <algorithm>
10 #include <utility>
11 
12 namespace ChimeraTK {
13 
14  DeviceInfoMap::DeviceInfoMap(std::string fileName) : _dmapFileName(std::move(fileName)) {}
15 
17  return _deviceInfoElements.size();
18  }
19 
20  std::ostream& operator<<(std::ostream& os, const DeviceInfoMap& deviceInfoMap) {
21  size_t size;
22  os << "=======================================" << std::endl;
23  os << "MAP FILE NAME: " << deviceInfoMap._dmapFileName << std::endl;
24  os << "---------------------------------------" << std::endl;
25  for(size = 0; size < deviceInfoMap._deviceInfoElements.size(); size++) {
26  os << deviceInfoMap._deviceInfoElements[size] << std::endl;
27  }
28  os << "=======================================";
29  return os;
30  }
31 
32  void DeviceInfoMap::insert(const DeviceInfo& elem) {
33  _deviceInfoElements.push_back(elem);
34  }
35 
36  void DeviceInfoMap::getDeviceInfo(const std::string& deviceName, DeviceInfo& value) {
37  std::vector<DeviceInfo>::iterator iter;
38  iter = find_if(_deviceInfoElements.begin(), _deviceInfoElements.end(), findDevByName_pred(deviceName));
39  if(iter == _deviceInfoElements.end()) {
40  throw ChimeraTK::logic_error("Cannot find device \"" + deviceName + "\" in DMAP file:" + _dmapFileName);
41  }
42  value = *iter;
43  }
44 
45  DeviceInfoMap::DeviceInfo::DeviceInfo() : dmapFileLineNumber(0) {}
46 
47  std::pair<std::string, std::string> DeviceInfoMap::DeviceInfo::getDeviceFileAndMapFileName() const {
48  return {uri, mapFileName};
49  }
50 
51  std::ostream& operator<<(std::ostream& os, const DeviceInfoMap::DeviceInfo& deviceInfo) {
52  os << "(" << deviceInfo.dmapFileName << ") NAME: " << deviceInfo.deviceName << " DEV : " << deviceInfo.uri
53  << " MAP : " << deviceInfo.mapFileName;
54  return os;
55  }
56 
57  // fixme: why is level not used?
59  std::vector<DeviceInfoMap::DeviceInfo> dmaps = _deviceInfoElements;
60  std::vector<DeviceInfoMap::DeviceInfo>::iterator iter_p, iter_n;
61  bool ret = true;
62 
63  err.clear();
64  if(dmaps.size() < 2) {
65  return true;
66  }
67 
68  std::sort(dmaps.begin(), dmaps.end(), copmaredRegisterInfosByName2_functor());
69  iter_p = dmaps.begin();
70  iter_n = iter_p + 1;
71  while(true) {
72  if((*iter_p).deviceName == (*iter_n).deviceName) {
73  if((*iter_p).uri != (*iter_n).uri || (*iter_p).mapFileName != (*iter_n).mapFileName) {
74  err.insert(ErrorList::ErrorElem(
76  ret = false;
77  }
78  }
79  iter_p = iter_n;
80  iter_n = ++iter_n;
81  if(iter_n == dmaps.end()) {
82  break;
83  }
84  }
85  return ret;
86  }
87 
88  std::ostream& operator<<(std::ostream& os, const DeviceInfoMap::ErrorList::ErrorElem::TYPE& me) {
89  switch(me) {
91  os << "ERROR";
92  break;
94  os << "WARNING";
95  break;
96  default:
97  os << "UNKNOWN";
98  break;
99  }
100  return os;
101  }
102 
104  const DeviceInfoMap::DeviceInfo& device1, const DeviceInfoMap::DeviceInfo& device2) {
105  _errorType = errorType;
106  _errorDevice1 = device1;
107  _errorDevice2 = device2;
108  _type = infoType;
109  }
110 
111  std::ostream& operator<<(std::ostream& os, const DeviceInfoMap::ErrorList::ErrorElem& me) {
112  switch(me._errorType) {
114  os << me._type << ": Found two devices with the same name but different properties: \""
115  << me._errorDevice1.deviceName << "\" in file \"" << me._errorDevice1.dmapFileName << "\" in line "
116  << me._errorDevice1.dmapFileLineNumber << " and \"" << me._errorDevice2.dmapFileName << "\" in line "
118  break;
119  }
120  return os;
121  }
122 
123  void DeviceInfoMap::ErrorList::clear() {
124  _errors.clear();
125  }
126 
127  void DeviceInfoMap::ErrorList::insert(const ErrorElem& elem) {
128  _errors.push_back(elem);
129  }
130 
131  std::ostream& operator<<(std::ostream& os, const DeviceInfoMap::ErrorList& me) {
132  std::list<DeviceInfoMap::ErrorList::ErrorElem>::const_iterator iter;
133  for(iter = me._errors.begin(); iter != me._errors.end(); ++iter) {
134  os << (*iter) << std::endl;
135  }
136  return os;
137  }
138 
140  return _deviceInfoElements.begin();
141  }
142 
144  return _deviceInfoElements.end();
145  }
146 
147  void DeviceInfoMap::addPluginLibrary(const std::string& soFile) {
148  _pluginLibraries.push_back(soFile);
149  }
150 
151  std::vector<std::string> DeviceInfoMap::getPluginLibraries() {
152  return _pluginLibraries;
153  }
154 
155 } // namespace ChimeraTK
ChimeraTK::DeviceInfoMap::DeviceInfo::DeviceInfo
DeviceInfo()
Default class constructor.
Definition: DeviceInfoMap.cc:45
ChimeraTK::DeviceInfoMap::ErrorList::ErrorElem::_errorDevice1
DeviceInfoMap::DeviceInfo _errorDevice1
Detailed information about first device that generate error or warning.
Definition: DeviceInfoMap.h:99
ChimeraTK::DeviceInfoMap::ErrorList::ErrorElem::_errorDevice2
DeviceInfoMap::DeviceInfo _errorDevice2
Detailed information about second device that generate error or warning.
Definition: DeviceInfoMap.h:101
ChimeraTK::DeviceInfoMap::iterator
std::vector< DeviceInfo >::iterator iterator
Definition: DeviceInfoMap.h:60
ChimeraTK::DeviceInfoMap::_pluginLibraries
std::vector< std::string > _pluginLibraries
Names of the so files with the plugins.
Definition: DeviceInfoMap.h:198
ChimeraTK::findDevByName_pred
Provides predicate to search device by name.
Definition: predicates.h:15
ChimeraTK::DeviceInfoMap::ErrorList::ErrorElem::_type
TYPE _type
Class of detected problem - ERROR or WARNING.
Definition: DeviceInfoMap.h:104
ChimeraTK::DeviceInfoMap
Definition: DeviceInfoMap.h:25
ChimeraTK::DeviceInfoMap::DeviceInfo::deviceName
std::string deviceName
logical name of the device
Definition: DeviceInfoMap.h:32
ChimeraTK::DeviceInfoMap::DeviceInfo::mapFileName
std::string mapFileName
name of the MAP file storing information about PCIe registers mapping
Definition: DeviceInfoMap.h:35
ChimeraTK::DeviceInfoMap::ErrorList::ErrorElem::WARNING
@ WARNING
Non-critical error was detected.
Definition: DeviceInfoMap.h:97
ChimeraTK::DeviceInfoMap::begin
iterator begin()
Return iterator to first device described in DMAP file.
Definition: DeviceInfoMap.cc:139
ChimeraTK::DeviceInfoMap::end
iterator end()
Return iterator to element after last one in DMAP file.
Definition: DeviceInfoMap.cc:143
ChimeraTK::DeviceInfoMap::getSize
size_t getSize()
Returns number of records in DMAP file.
Definition: DeviceInfoMap.cc:16
ChimeraTK::DeviceInfoMap::ErrorList::ErrorElem::ErrorElem
ErrorElem(TYPE infoType, DMAP_FILE_ERR errorType, const DeviceInfoMap::DeviceInfo &device1, const DeviceInfoMap::DeviceInfo &device2)
Creates obiect that describe one detected error or warning.
Definition: DeviceInfoMap.cc:103
ChimeraTK::DeviceInfoMap::DeviceInfo::dmapFileName
std::string dmapFileName
name of the DMAP file
Definition: DeviceInfoMap.h:37
ChimeraTK::DeviceInfoMap::ErrorList::ErrorElem::TYPE
TYPE
Defines available classes of detected problems.
Definition: DeviceInfoMap.h:95
ChimeraTK::DeviceInfoMap::ErrorList
Stores information about errors and warnings.
Definition: DeviceInfoMap.h:68
ChimeraTK::DeviceInfoMap::DeviceInfo::dmapFileLineNumber
uint32_t dmapFileLineNumber
line number in DMAP file storing listed above information
Definition: DeviceInfoMap.h:38
ChimeraTK::DeviceInfoMap::ErrorList::ErrorElem
Stores detailed information about one error or warning.
Definition: DeviceInfoMap.h:78
ChimeraTK::DeviceInfoMap::ErrorList::ErrorElem::DMAP_FILE_ERR
DMAP_FILE_ERR
Defines available types of detected problems.
Definition: DeviceInfoMap.h:83
ChimeraTK::DeviceInfoMap::check
bool check(ErrorList &err, ErrorList::ErrorElem::TYPE level)
Checks logical correctness of DMAP file.
Definition: DeviceInfoMap.cc:58
ChimeraTK::DeviceInfoMap::_dmapFileName
std::string _dmapFileName
name of DMAP file
Definition: DeviceInfoMap.h:197
ChimeraTK::DeviceInfoMap::DeviceInfoMap
DeviceInfoMap(std::string fileName)
Constructor.
Definition: DeviceInfoMap.cc:14
ChimeraTK::DeviceInfoMap::ErrorList::ErrorElem::_errorType
DMAP_FILE_ERR _errorType
Type of detected problem.
Definition: DeviceInfoMap.h:103
ChimeraTK::DeviceInfoMap::addPluginLibrary
void addPluginLibrary(const std::string &soFile)
Add the name of a library to the list.
Definition: DeviceInfoMap.cc:147
ChimeraTK::DeviceInfoMap::_deviceInfoElements
std::vector< DeviceInfo > _deviceInfoElements
vector storing parsed contents of DMAP file
Definition: DeviceInfoMap.h:196
ChimeraTK::DeviceInfoMap::DeviceInfo
Stores information about one device.
Definition: DeviceInfoMap.h:30
ChimeraTK::copmaredRegisterInfosByName2_functor
Provides predicate to compare devices by device file by name.
Definition: predicates.h:28
ChimeraTK::DeviceInfoMap::operator<<
friend std::ostream & operator<<(std::ostream &os, const DeviceInfoMap &deviceInfoMap)
Definition: DeviceInfoMap.cc:20
ChimeraTK::operator<<
std::ostream & operator<<(std::ostream &stream, const DataDescriptor::FundamentalType &fundamentalType)
Definition: DataDescriptor.cpp:195
DeviceInfoMap.h
ChimeraTK::DeviceInfoMap::ErrorList::_errors
std::list< ErrorElem > _errors
Lists of errors or warnings detected during MAP file correctness checking.
Definition: DeviceInfoMap.h:121
ChimeraTK::DeviceInfoMap::DeviceInfo::uri
std::string uri
uri which describes the device (or name of the device file in /dev in backward compatibility mode)
Definition: DeviceInfoMap.h:33
ChimeraTK::DeviceInfoMap::DeviceInfo::getDeviceFileAndMapFileName
std::pair< std::string, std::string > getDeviceFileAndMapFileName() const
Convenience function to extract the device file name and the map file name as one object (a pair).
Definition: DeviceInfoMap.cc:47
Exception.h
ChimeraTK
Definition: DummyBackend.h:16
ChimeraTK::DeviceInfoMap::insert
void insert(const DeviceInfo &elem)
Insert new element read from DMAP file.
Definition: DeviceInfoMap.cc:32
predicates.h
ChimeraTK::DeviceInfoMap::getDeviceInfo
void getDeviceInfo(const std::string &deviceName, DeviceInfo &value)
Returns information about specified device.
Definition: DeviceInfoMap.cc:36
ChimeraTK::DeviceInfoMap::ErrorList::ErrorElem::NONUNIQUE_DEVICE_NAME
@ NONUNIQUE_DEVICE_NAME
Names of two devices are the same - treated as critical error.
Definition: DeviceInfoMap.h:84
ChimeraTK::logic_error
Exception thrown when a logic error has occured.
Definition: Exception.h:51
ChimeraTK::DeviceInfoMap::getPluginLibraries
std::vector< std::string > getPluginLibraries()
You can define shared libraries with Backend plugins in the DMAP file.
Definition: DeviceInfoMap.cc:151
ChimeraTK::DeviceInfoMap::ErrorList::ErrorElem::ERROR
@ ERROR
Critical error was detected.
Definition: DeviceInfoMap.h:96