ChimeraTK-DeviceAccess 03.31.00
Loading...
Searching...
No Matches
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
16namespace ChimeraTK {
17 using namespace ChimeraTK;
18}
19using 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();
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
53 void checkWriteReadFromRegister(ChimeraTK::Device& rebotDevice);
54};
55
56/**********************************************************************************************************************/
57class 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
86RebotTestClass::RebotTestClass(std::string const& cardAlias)
87: _cardAlias(cardAlias), _rebotServer(getServerDetails(cardAlias)) {}
88
89RebotServerDetails RebotTestClass::getServerDetails(const std::string& cardAlias) {
90 DeviceInfo deviceDetails = getDeviceDetailsFromDMap(cardAlias);
91 return extractServerDetailsFromUri(deviceDetails.uri);
92}
93
94DeviceInfo RebotTestClass::getDeviceDetailsFromDMap(const std::string& cardAlias) {
95 std::string dmapFileLocation = ChimeraTK::BackendFactory::getInstance().getDMapFilePath();
97 boost::shared_ptr<ChimeraTK::DeviceInfoMap> listOfDevicesInDMapFile;
98 listOfDevicesInDMapFile = dMapParser.parse(dmapFileLocation);
100 listOfDevicesInDMapFile->getDeviceInfo(cardAlias, deviceDetails);
101 return deviceDetails;
102}
103
104RebotServerDetails 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
112void 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, "", 30);
116 BOOST_CHECK_EQUAL(rebotBackend.isOpen(), false);
117
118 BOOST_CHECK_NO_THROW(rebotBackend.open());
119 BOOST_CHECK_EQUAL(rebotBackend.isOpen(), true);
120
121 // it must always be possible to call open() again
122 BOOST_CHECK_NO_THROW(rebotBackend.open());
123 BOOST_CHECK_EQUAL(rebotBackend.isOpen(), true);
124
125 BOOST_CHECK_NO_THROW(rebotBackend.close());
126 BOOST_CHECK_EQUAL(rebotBackend.isOpen(), false);
127
128 // it must always be possible to call close() again
129 BOOST_CHECK_NO_THROW(rebotBackend.close());
130 BOOST_CHECK_EQUAL(rebotBackend.isOpen(), false);
131}
132
134 ChimeraTK::RebotBackend rebotBackend(_rebotServer.ip, _rebotServer.port);
135 rebotBackend.open();
136
137 // The dummy server wites 0xDEADDEAD to the start address 0x04. Use this for testing.
138 uint32_t address = 0x04;
139 int32_t readValue = 0;
140 rebotBackend.read(uint64_t(0), address, &readValue, sizeof(readValue));
141 BOOST_CHECK_EQUAL(0xDEADBEEF, readValue);
142
143 /********************************************************************************************************************/
144 // Single word read - Hardcoding addresses for now
145 uint64_t word_status_register_address = 0x8;
146 int32_t data = -987;
147 // Register
148 rebotBackend.write(0, word_status_register_address, &data, sizeof(data));
149
150 rebotBackend.read(0, word_status_register_address, &readValue, sizeof(readValue));
151
152 BOOST_CHECK_EQUAL(data, readValue);
153 /********************************************************************************************************************/
154
155 // Multiword read/write
156 uint32_t word_clk_mux_addr = 28;
157 int32_t dataToWrite[4] = {rand(), rand(), rand(), rand()};
158 int32_t readInData[4];
159
160 rebotBackend.write(0, word_clk_mux_addr, dataToWrite, sizeof(dataToWrite));
161 rebotBackend.read(0, word_clk_mux_addr, readInData, sizeof(readInData));
162
163 for(int i = 0; i < 4; i++) {
164 BOOST_CHECK_EQUAL(dataToWrite[i], readInData[i]);
165 }
166
167 uint32_t test_area_Addr = 0x00000030;
168 std::vector<int32_t> test_area_data(1024);
169 for(uint32_t i = 0; i < test_area_data.size(); ++i) {
170 test_area_data.at(i) = i;
171 }
172 std::vector<int32_t> test_area_ReadIndata(1024);
173
174 rebotBackend.write(0, test_area_Addr, test_area_data.data(), sizeof(int32_t) * test_area_data.size());
175 rebotBackend.read(0, test_area_Addr, test_area_ReadIndata.data(), sizeof(int32_t) * test_area_ReadIndata.size());
176
177 for(uint32_t i = 0; i < test_area_ReadIndata.size(); i++) {
178 BOOST_CHECK_EQUAL(test_area_data[i], test_area_ReadIndata[i]);
179 }
180}
static BackendFactory & getInstance()
Static function to get an instance of factory.
void setDMapFilePath(std::string dMapFilePath)
This function sets the _DMapFilePath.
std::string getDMapFilePath()
Returns the _DMapFilePath.
Provides method to parse DMAP file.
static DeviceInfoMapPointer parse(const std::string &file_name)
Performs parsing of specified DMAP file.
bool isOpen() override
Return whether a device has been opened or not.
Class allows to read/write registers from device.
Definition Device.h:39
Stores information about one device.
std::string uri
uri which describes the device (or name of the device file in /dev in backward compatibility mode)
void close() final
Deactivates all asynchronous accessors and calls closeImpl().
void read(uint8_t bar, uint32_t addressInBytes, int32_t *data, size_t sizeInBytes) override
Deprecated read function using 32bit address for backwards compatibility.
void open() override
The function opens the connection to the device.
void write(uint8_t bar, uint32_t addressInBytes, int32_t const *data, size_t sizeInBytes) override
Deprecated write function using 32bit address for backwards compatibility.
RebotDeviceTestSuite(std::string const &cardAlias)
void testReadWriteAPIOfRebotBackend()
RebotTestClass(std::string const &cardAlias)
DeviceDescriptor parseDeviceDesciptor(std::string cddString)
Parse a ChimeraTK device descriptor (CDD) and return the information in the DeviceDescriptor struct.
Definition Utilities.cpp:49
RegisterPath BAR()
The numeric_address::BAR() function can be used to directly access registers by numeric addresses,...
This header collects some conversion routines which should be useful both for ApplicationCore Python ...
This structure holds the information of an ChimeraTK device descriptor.
Definition Utilities.h:31
std::map< std::string, std::string > parameters
Definition Utilities.h:34
RebotServerDetails()=default
RebotServerDetails(std::string &ipAddress, std::string portNumber)
bool init_unit_test()
ChimeraTK::DeviceInfoMap::DeviceInfo DeviceInfo