ChimeraTK-DeviceAccess  03.18.00
testRebotBackend.cpp
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 /**********************************************************************************************************************/
5 /* Keep this file in a way that the tests also run with real hardware. */
6 /**********************************************************************************************************************/
7 
8 #include "BackendFactory.h"
10 #include "Device.h"
11 #include "DMapFileParser.h"
12 #include "NumericAddress.h"
13 #include "RebotBackend.h"
14 #include "Utilities.h"
15 
16 namespace ChimeraTK {
17  using namespace ChimeraTK;
18 }
19 using namespace boost::unit_test_framework;
21 
23 
24 /**********************************************************************************************************************/
26  std::string ip;
27  std::string port;
28 
29  RebotServerDetails() = default;
30  RebotServerDetails(std::string& ipAddress, std::string portNumber) : ip(ipAddress), port(portNumber) {}
31 };
32 
34  private:
35  std::string _cardAlias;
36  RebotServerDetails _rebotServer;
37 
38  public:
39  explicit RebotTestClass(std::string const& cardAlias);
40  void testConnection();
41  void testReadWriteAPIOfRebotBackend();
42 
43  private:
44  /*
45  * parse the relevant dmap file to extract ip and port which would be required
46  * for testing the rebot backend
47  */
48  RebotServerDetails getServerDetails(const std::string& cardAlias);
49 
50  DeviceInfo getDeviceDetailsFromDMap(const std::string& cardAlias);
51  RebotServerDetails extractServerDetailsFromUri(std::string& uri);
52 
54 };
55 
56 /**********************************************************************************************************************/
57 class RebotDeviceTestSuite : public test_suite {
58  public:
59  explicit RebotDeviceTestSuite(std::string const& cardAlias) : test_suite("RebotDeviceTestSuite") {
60  boost::shared_ptr<RebotTestClass> rebotTest(new RebotTestClass(cardAlias));
61  add(BOOST_CLASS_TEST_CASE(&RebotTestClass::testConnection, rebotTest));
62  add(BOOST_CLASS_TEST_CASE(&RebotTestClass::testReadWriteAPIOfRebotBackend, rebotTest));
63  }
64 };
65 
67  if(framework::master_test_suite().argc < 2) {
68  std::cout << "Usage: " << framework::master_test_suite().argv[0] << " cardAlias [dmapFile]" << std::endl;
69  return false;
70  }
71  auto cardAlias = framework::master_test_suite().argv[1];
72 
73  // take dmap file location if given, else search for cardAlias in the
74  // factory default dmap file
75  if(framework::master_test_suite().argc > 2) { // there is a second argument
76  ChimeraTK::BackendFactory::getInstance().setDMapFilePath(framework::master_test_suite().argv[2]);
77  }
78 
79  framework::master_test_suite().p_name.value = "Rebot backend test suite";
80  framework::master_test_suite().add(new RebotDeviceTestSuite(cardAlias));
81  return true;
82 }
83 
84 /**********************************************************************************************************************/
85 
86 RebotTestClass::RebotTestClass(std::string const& cardAlias)
87 : _cardAlias(cardAlias), _rebotServer(getServerDetails(cardAlias)) {}
88 
89 RebotServerDetails RebotTestClass::getServerDetails(const std::string& cardAlias) {
90  DeviceInfo deviceDetails = getDeviceDetailsFromDMap(cardAlias);
91  return extractServerDetailsFromUri(deviceDetails.uri);
92 }
93 
94 DeviceInfo RebotTestClass::getDeviceDetailsFromDMap(const std::string& cardAlias) {
95  std::string dmapFileLocation = ChimeraTK::BackendFactory::getInstance().getDMapFilePath();
96  ChimeraTK::DMapFileParser dMapParser;
97  boost::shared_ptr<ChimeraTK::DeviceInfoMap> listOfDevicesInDMapFile;
98  listOfDevicesInDMapFile = dMapParser.parse(dmapFileLocation);
100  listOfDevicesInDMapFile->getDeviceInfo(cardAlias, deviceDetails);
101  return deviceDetails;
102 }
103 
104 RebotServerDetails RebotTestClass::extractServerDetailsFromUri(std::string& uri) {
106  std::map<std::string, std::string>& serverParameters = parsedSDM.parameters;
107  std::string ip = serverParameters["ip"];
108  std::string port = serverParameters["port"];
109  return RebotServerDetails(ip, port);
110 }
111 
112 void RebotTestClass::testConnection() { // BAckend test
113 
114  // create connection with good ip and port see that there are no exceptions
115  ChimeraTK::RebotBackend rebotBackend(_rebotServer.ip, _rebotServer.port);
116  ChimeraTK::RebotBackend secondConnectionToServer(_rebotServer.ip, _rebotServer.port);
117  BOOST_CHECK_EQUAL(rebotBackend.isOpen(), false);
118 
119  BOOST_CHECK_NO_THROW(rebotBackend.open());
120  BOOST_CHECK_EQUAL(rebotBackend.isOpen(), true);
121 
122  // it must always be possible to call open() again
123  BOOST_CHECK_NO_THROW(rebotBackend.open());
124  BOOST_CHECK_EQUAL(rebotBackend.isOpen(), true);
125 
126  BOOST_CHECK_NO_THROW(rebotBackend.close());
127  BOOST_CHECK_EQUAL(rebotBackend.isOpen(), false);
128 
129  // it must always be possible to call close() again
130  BOOST_CHECK_NO_THROW(rebotBackend.close());
131  BOOST_CHECK_EQUAL(rebotBackend.isOpen(), false);
132 }
133 
135  ChimeraTK::RebotBackend rebotBackend(_rebotServer.ip, _rebotServer.port);
136  rebotBackend.open();
137 
138  // The dummy server wites 0xDEADDEAD to the start address 0x04. Use this for testing.
139  uint32_t address = 0x04;
140  int32_t readValue = 0;
141  rebotBackend.read(uint64_t(0), address, &readValue, sizeof(readValue));
142  BOOST_CHECK_EQUAL(0xDEADBEEF, readValue);
143 
144  /********************************************************************************************************************/
145  // Single word read - Hardcoding addresses for now
146  uint64_t word_status_register_address = 0x8;
147  int32_t data = -987;
148  // Register
149  rebotBackend.write(0, word_status_register_address, &data, sizeof(data));
150 
151  rebotBackend.read(0, word_status_register_address, &readValue, sizeof(readValue));
152 
153  BOOST_CHECK_EQUAL(data, readValue);
154  /********************************************************************************************************************/
155 
156  // Multiword read/write
157  uint32_t word_clk_mux_addr = 28;
158  int32_t dataToWrite[4] = {rand(), rand(), rand(), rand()};
159  int32_t readInData[4];
160 
161  rebotBackend.write(0, word_clk_mux_addr, dataToWrite, sizeof(dataToWrite));
162  rebotBackend.read(0, word_clk_mux_addr, readInData, sizeof(readInData));
163 
164  for(int i = 0; i < 4; i++) {
165  BOOST_CHECK_EQUAL(dataToWrite[i], readInData[i]);
166  }
167 
168  uint32_t test_area_Addr = 0x00000030;
169  std::vector<int32_t> test_area_data(1024);
170  for(uint32_t i = 0; i < test_area_data.size(); ++i) {
171  test_area_data.at(i) = i;
172  }
173  std::vector<int32_t> test_area_ReadIndata(1024);
174 
175  rebotBackend.write(0, test_area_Addr, test_area_data.data(), sizeof(int32_t) * test_area_data.size());
176  rebotBackend.read(0, test_area_Addr, test_area_ReadIndata.data(), sizeof(int32_t) * test_area_ReadIndata.size());
177 
178  for(uint32_t i = 0; i < test_area_ReadIndata.size(); i++) {
179  BOOST_CHECK_EQUAL(test_area_data[i], test_area_ReadIndata[i]);
180  }
181 }
ChimeraTK::RebotBackend::open
void open() override
The function opens the connection to the device.
Definition: RebotBackend.cc:94
ChimeraTK::DMapFileParser::parse
static DeviceInfoMapPointer parse(const std::string &file_name)
Performs parsing of specified DMAP file.
Definition: DMapFileParser.cpp:18
ChimeraTK::NumericAddressedBackend::close
void close() final
Deactivates all asynchronous accessors and calls closeImpl().
Definition: NumericAddressedBackend.cc:220
ChimeraTK::Utilities::parseDeviceDesciptor
DeviceDescriptor parseDeviceDesciptor(std::string cddString)
Parse a ChimeraTK device descriptor (CDD) and return the information in the DeviceDescriptor struct.
Definition: Utilities.cpp:47
ChimeraTK::RebotBackend::write
void write(uint8_t bar, uint32_t addressInBytes, int32_t const *data, size_t sizeInBytes) override
Definition: RebotBackend.cc:117
ChimeraTK::RebotBackend::read
void read(uint8_t bar, uint32_t addressInBytes, int32_t *data, size_t sizeInBytes) override
Definition: RebotBackend.cc:105
ChimeraTK::BackendFactory::getInstance
static BackendFactory & getInstance()
Static function to get an instance of factory.
Definition: BackendFactory.cc:191
Utilities.h
RebotTestClass
Definition: testRebotBackend.cpp:33
RebotTestClass::RebotTestClass
RebotTestClass(std::string const &cardAlias)
Definition: testRebotBackend.cpp:86
boost_dynamic_init_test.h
ChimeraTK::DeviceDescriptor
This structure holds the information of an ChimeraTK device descriptor.
Definition: Utilities.h:31
ChimeraTK::BackendFactory::setDMapFilePath
void setDMapFilePath(std::string dMapFilePath)
This function sets the _DMapFilePath.
Definition: BackendFactory.cc:161
RebotServerDetails::port
std::string port
Definition: testRebotBackend.cpp:27
ChimeraTK::DeviceDescriptor::parameters
std::map< std::string, std::string > parameters
Definition: Utilities.h:34
ChimeraTK::BackendFactory::getDMapFilePath
std::string getDMapFilePath()
Returns the _DMapFilePath.
Definition: BackendFactory.cc:167
RebotServerDetails::RebotServerDetails
RebotServerDetails(std::string &ipAddress, std::string portNumber)
Definition: testRebotBackend.cpp:30
checkWriteReadFromRegister
void checkWriteReadFromRegister(ChimeraTK::Device &rebotDevice)
Definition: testRebotBackendCreation.cpp:89
ChimeraTK::DMapFileParser
Provides method to parse DMAP file.
Definition: DMapFileParser.h:19
RebotDeviceTestSuite
Definition: testRebotBackend.cpp:57
Device.h
RebotServerDetails
Definition: testRebotBackend.cpp:25
RebotTestClass::testReadWriteAPIOfRebotBackend
void testReadWriteAPIOfRebotBackend()
Definition: testRebotBackend.cpp:134
ChimeraTK::numeric_address::BAR
RegisterPath BAR()
The numeric_address::BAR() function can be used to directly access registers by numeric addresses,...
Definition: NumericAddress.cc:7
DMapFileParser.h
NumericAddress.h
ChimeraTK::RebotBackend
Definition: RebotBackend.h:44
ChimeraTK::DeviceInfoMap::DeviceInfo
Stores information about one device.
Definition: DeviceInfoMap.h:30
ChimeraTK::Device
Class allows to read/write registers from device.
Definition: Device.h:39
DeviceInfo
ChimeraTK::DeviceInfoMap::DeviceInfo DeviceInfo
Definition: testRebotBackend.cpp:22
RebotBackend.h
RebotTestClass::testConnection
void testConnection()
Definition: testRebotBackend.cpp:112
RebotServerDetails::ip
std::string ip
Definition: testRebotBackend.cpp:26
BackendFactory.h
init_unit_test
bool init_unit_test()
Definition: testRebotBackend.cpp:66
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::DeviceBackendImpl::isOpen
bool isOpen() override
Return whether a device has been opened or not.
Definition: DeviceBackendImpl.h:27
RebotDeviceTestSuite::RebotDeviceTestSuite
RebotDeviceTestSuite(std::string const &cardAlias)
Definition: testRebotBackend.cpp:59
ChimeraTK
Definition: DummyBackend.h:16