ChimeraTK-DeviceAccess 03.25.00
Loading...
Searching...
No Matches
testSharedDummyBackendUnifiedExt.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#define BOOST_TEST_DYN_LINK
5#define BOOST_TEST_MODULE SharedDummyBackendUnifiedTest
6#include "Device.h"
7#include "ProcessManagement.h"
9#include "Utilities.h"
10
11#include <boost/filesystem.hpp>
12#include <boost/interprocess/managed_shared_memory.hpp>
13#include <boost/test/unit_test.hpp>
14
15#include <unistd.h>
16
17#include <string>
18
19namespace {
20
21 using namespace ChimeraTK;
22 using namespace boost::unit_test_framework;
23
24 // Static variables
25 // Use hardcoded information from sharedDummyUnified.dmap to only use public interface here.
26 std::string mapFileName{"sharedDummyUnified.map"};
27 std::size_t instanceIdHash = Utilities::shmDummyInstanceIdHash("1", {{"map", mapFileName}});
28 std::string shmName{Utilities::createShmName(instanceIdHash, mapFileName, getUserName())};
29
30 BOOST_AUTO_TEST_SUITE(SharedDummyBackendUnifiedTestSuite)
31
32 struct TestFixture {
33 TestFixture() : argc(framework::master_test_suite().argc), argv(framework::master_test_suite().argv) {}
34
35 int argc;
36 char** argv;
37 };
38
39 static void mirrorArea(
41 processVarsFrom.read();
42 for(size_t i = 0; i < processVarsFrom.getNElements(); ++i) {
43 processVarsTo[i] = processVarsFrom[i];
44 }
45 processVarsTo.write();
46 }
47
48 /********************************************************************************************************************/
49
56 BOOST_FIXTURE_TEST_CASE(testRegisterAccessor, TestFixture) {
57 bool keepRunning = true;
58
59 setDMapFilePath("sharedDummyUnified.dmap");
60
61 {
62 Device dev;
63 BOOST_CHECK(!dev.isOpened());
64 dev.open("SHDMEMDEV");
65 BOOST_CHECK(dev.isOpened());
66
67 BOOST_CHECK(shm_exists(shmName));
68
69 ChimeraTK::OneDRegisterAccessor<int> processVarsFeature = dev.getOneDRegisterAccessor<int>("FEATURE/AREA1");
70
71 ChimeraTK::OneDRegisterAccessor<int> processVarsMirror = dev.getOneDRegisterAccessor<int>("MIRRORED/AREA1");
72
73 BOOST_CHECK_EQUAL(processVarsFeature.getNElements(), processVarsMirror.getNElements());
74
75 ScalarRegisterAccessor<int> mirrorRequest_Type = dev.getScalarRegisterAccessor<int>("MIRRORREQUEST/TYPE");
76 ScalarRegisterAccessor<int> mirrorRequest_Busy = dev.getScalarRegisterAccessor<int>("MIRRORREQUEST/BUSY");
77 ScalarRegisterAccessor<int> mirrorRequest_Updated =
78 dev.getScalarRegisterAccessor<int>("MIRRORREQUEST/UPDATED/DUMMY_WRITEABLE");
79 ScalarRegisterAccessor<int> mirrorRequest_DataInterrupt =
80 dev.getScalarRegisterAccessor<int>("MIRRORREQUEST/DATA_INTERRUPT");
81 ScalarRegisterAccessor<int> mirrorRequestUpdated_Interrupt{
82 dev.getScalarRegisterAccessor<int>("DUMMY_INTERRUPT_0")};
83
84 ScalarRegisterAccessor<int> dataInterrupt{dev.getScalarRegisterAccessor<int>("DUMMY_INTERRUPT_1")};
85
86 do {
87 // poll Busy until it is set to true, indicating a new request
88 do {
89 mirrorRequest_Busy.readLatest();
90 // we use boost::sleep because it defines an interruption point for signals
91 boost::this_thread::sleep_for(boost::chrono::milliseconds(50));
92 } while(mirrorRequest_Busy == 0);
93
94 mirrorRequest_Type.readLatest();
95 switch(static_cast<MirrorRequestType>((int)mirrorRequest_Type)) {
97 mirrorArea(processVarsMirror, processVarsFeature);
98 break;
100 mirrorArea(processVarsFeature, processVarsMirror);
101 break;
103 keepRunning = false;
104 }
105 mirrorRequest_Updated.readLatest();
106 ++mirrorRequest_Updated;
107 mirrorRequest_Updated.write();
108 // also trigger interrupt for this variable
109 mirrorRequestUpdated_Interrupt = 1;
110 mirrorRequestUpdated_Interrupt.write();
111
112 mirrorRequest_Busy = 0;
113 mirrorRequest_Busy.write();
114
115 mirrorRequest_DataInterrupt.readLatest();
116 if(mirrorRequest_DataInterrupt == 1) {
117 dataInterrupt.write();
118 mirrorRequest_DataInterrupt = 0;
119 mirrorRequest_DataInterrupt.write();
120 }
121 } while(keepRunning);
122 dev.close();
123 }
124 }
125
126 /********************************************************************************************************************/
127 BOOST_AUTO_TEST_SUITE_END()
128
129} // anonymous namespace
std::string getUserName()
Class allows to read/write registers from device.
Definition Device.h:39
bool isOpened() const
Check if the device is currently opened.
Definition Device.cc:73
void close()
Close the device.
Definition Device.cc:66
ScalarRegisterAccessor< UserType > getScalarRegisterAccessor(const RegisterPath &registerPathName, size_t wordOffsetInRegister=0, const AccessModeFlags &flags=AccessModeFlags({})) const
Get a ScalarRegisterObject object for the given register.
Definition Device.h:266
OneDRegisterAccessor< UserType > getOneDRegisterAccessor(const RegisterPath &registerPathName, size_t numberOfWords=0, size_t wordOffsetInRegister=0, const AccessModeFlags &flags=AccessModeFlags({})) const
Get a OneDRegisterAccessor object for the given register.
Definition Device.h:276
void open(std::string const &aliasName)
Open a device by the given alias name from the DMAP file.
Definition Device.cc:58
Accessor class to read and write registers transparently by using the accessor object like a vector o...
unsigned int getNElements()
Return number of elements/samples in the register.
Accessor class to read and write scalar registers transparently by using the accessor object like a v...
bool readLatest()
Read the latest value, discarding any other update since the last read if present.
bool write(ChimeraTK::VersionNumber versionNumber={})
Write the data to device.
void read()
Read the data from the device.
std::size_t shmDummyInstanceIdHash(const std::string &address, const std::map< std::string, std::string > &parameters)
Generates shm dummy instanceId hash from address and parameter map, Intended for use with parseDevice...
std::string createShmName(std::size_t instanceIdHash, const std::string &mapFileName, const std::string &userName)
Generates shm dummy name from parameter hashes.
void setDMapFilePath(std::string dmapFilePath)
Set the location of the dmap file.
MirrorRequestType
bool shm_exists(std::string shmName)
BOOST_FIXTURE_TEST_CASE(testSlowReader, DeviceFixture)