5 #define BOOST_TEST_DYN_LINK
6 #define BOOST_TEST_MODULE UioBackendUnifiedTest
7 #include <boost/test/unit_test.hpp>
8 using namespace boost::unit_test_framework;
30 const std::string lockfile{
"/var/run/lock/uiodummy.lock"};
32 int fd = open(lockfile.c_str(), O_WRONLY | O_CREAT, 0777);
38 int res = flock(fd, LOCK_EX);
55 BOOST_AUTO_TEST_SUITE(UioBackendUnifiedTestSuite)
59 static const std::string
cdd(
"(uio:ctkuiodummy?map=uioBackendTest.mapp)");
65 explicit RawUioAccess(
const std::string& filePath,
const std::string& mapFile);
67 void sendInterrupt()
const;
68 [[nodiscard]]
size_t getMemorySize()
const;
71 T
read(
const std::string& name);
73 void write(
const std::string& name, T value);
76 int _uioFileDescriptor;
78 std::filesystem::path _deviceFilePath;
79 size_t _deviceMemSize = 0;
80 void* _memoryPointer{
nullptr};
83 static uint64_t readUint64HexFromFile(
const std::string& filePath);
89 : _deviceFilePath(filePath.c_str()) {
90 if(std::filesystem::is_symlink(_deviceFilePath)) {
91 _deviceFilePath = std::filesystem::canonical(_deviceFilePath);
93 _uioFileDescriptor = open(filePath.c_str(), O_RDWR);
95 if(_uioFileDescriptor < 0) {
96 throw std::runtime_error(
"failed to open UIO device '" + filePath +
"'");
99 _uioProcFd = open(
"/proc/uio-dummy", O_RDWR);
101 throw std::runtime_error(
"failed to open UIO device '" + filePath +
"'");
105 std::string fileName = _deviceFilePath.filename().string();
106 _deviceMemSize = readUint64HexFromFile(
"/sys/class/uio/" + fileName +
"/maps/map0/size");
108 _memoryPointer = mmap(
nullptr, _deviceMemSize, PROT_READ | PROT_WRITE, MAP_SHARED, _uioFileDescriptor, 0);
110 if(_memoryPointer == MAP_FAILED) {
111 throw std::runtime_error(
"UioMmap construction failed");
114 auto [cat, metaCat] = p.
parse(mapFile);
115 _catalogue = std::move(cat);
123 munmap(_memoryPointer, _deviceMemSize);
124 close(_uioFileDescriptor);
131 static int enable{1};
132 std::ignore =
::write(_uioProcFd, &enable,
sizeof(enable));
138 return _deviceMemSize;
144 return _memoryPointer;
152 return *
reinterpret_cast<T*
>(
reinterpret_cast<std::byte*
>(
data()) + r.address);
160 *
reinterpret_cast<T*
>(
reinterpret_cast<std::byte*
>(
data()) + r.address) = value;
166 uint64_t RawUioAccess::readUint64HexFromFile(
const std::string& filePath) {
168 std::ifstream inputFile(filePath.c_str());
170 if(inputFile.is_open()) {
171 inputFile >> std::hex >> value;
181 template<
typename Derived>
183 Derived*
derived =
static_cast<Derived*
>(
this);
194 .disableSwitchReadOnly()
195 .disableSwitchWriteOnly()
196 .disableTestWriteNeverLosesData()
197 .disableAsyncReadInconsistency()
198 .enableTestRawTransfer();
203 template<
typename Type>
207 Type val00 = (
raw ? rawVal00 : rawToCooked<Type, rawUserType>(rawVal00));
213 template<
typename Type>
216 Type val00 = (
raw ? rawVal00 : rawToCooked<Type, rawUserType>(rawVal00));
222 auto x = generateValue<rawUserType>(
true)[0][0];
227 template<
typename UserType,
typename RawType>
229 return static_cast<RawType
>(val);
233 template<
typename UserType,
typename RawType>
235 return static_cast<UserType
>(val);
245 static std::string
path() {
return "TIMING.WORD_ID"; }
253 static std::string
path() {
return "MOTOR_CONTROL.MOTOR_POSITION"; }
269 BOOST_AUTO_TEST_SUITE_END()