fsfw/src/fsfw_hal/linux/uio/UioMapper.cpp

104 lines
3.0 KiB
C++
Raw Normal View History

2022-02-03 10:14:47 +01:00
#include "UioMapper.h"
#include <fcntl.h>
2022-11-15 10:56:46 +01:00
#include <sys/stat.h>
2022-02-03 10:14:47 +01:00
#include <unistd.h>
#include <filesystem>
#include <fstream>
#include <sstream>
#include "fsfw/serviceinterface.h"
const char UioMapper::UIO_PATH_PREFIX[] = "/sys/class/uio/";
const char UioMapper::MAP_SUBSTR[] = "/maps/map";
const char UioMapper::SIZE_FILE_PATH[] = "/size";
2022-11-15 10:56:46 +01:00
UioMapper::UioMapper(std::string uioFile, int mapNum) : mapNum(mapNum) {
struct stat buf;
lstat(uioFile.c_str(), &buf);
if (S_ISLNK(buf.st_mode)) {
char* res = realpath(uioFile.c_str(), nullptr);
if (res) {
2022-11-21 15:22:25 +01:00
this->uioFile = res;
2022-11-21 14:56:08 +01:00
free(res);
2022-11-15 10:56:46 +01:00
} else {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "Could not resolve real path of UIO file " << uioFile << std::endl;
#endif
}
} else {
2022-11-21 15:22:25 +01:00
this->uioFile = std::move(uioFile);
2022-11-15 10:56:46 +01:00
}
}
2022-02-03 10:14:47 +01:00
UioMapper::~UioMapper() {}
ReturnValue_t UioMapper::getMappedAdress(uint32_t** address, Permissions permissions) {
2022-08-15 20:28:16 +02:00
ReturnValue_t result = returnvalue::OK;
2022-02-03 10:14:47 +01:00
int fd = open(uioFile.c_str(), O_RDWR);
if (fd < 1) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-11-15 10:56:46 +01:00
sif::error << "UioMapper::getMappedAdress: Invalid UIO device file " << uioFile << std::endl;
2022-02-03 10:14:47 +01:00
#endif
2022-08-15 20:28:16 +02:00
return returnvalue::FAILED;
2022-02-03 10:14:47 +01:00
}
size_t size = 0;
result = getMapSize(&size);
2022-08-15 20:28:16 +02:00
if (result != returnvalue::OK) {
2022-02-03 10:14:47 +01:00
return result;
}
*address = static_cast<uint32_t*>(
mmap(NULL, size, static_cast<int>(permissions), MAP_SHARED, fd, mapNum * getpagesize()));
if (*address == MAP_FAILED) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "UioMapper::getMappedAdress: Failed to map physical address of uio device "
2022-02-14 14:54:20 +01:00
<< uioFile.c_str() << " and map" << static_cast<int>(mapNum) << std::endl;
2022-02-03 10:14:47 +01:00
#endif
2022-08-15 20:28:16 +02:00
return returnvalue::FAILED;
2022-02-03 10:14:47 +01:00
}
2022-08-15 20:28:16 +02:00
return returnvalue::OK;
2022-02-03 10:14:47 +01:00
}
ReturnValue_t UioMapper::getMapSize(size_t* size) {
std::stringstream namestream;
namestream << UIO_PATH_PREFIX << uioFile.substr(5, std::string::npos) << MAP_SUBSTR << mapNum
<< SIZE_FILE_PATH;
FILE* fp;
fp = fopen(namestream.str().c_str(), "r");
if (fp == nullptr) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "UioMapper::getMapSize: Failed to open file " << namestream.str() << std::endl;
2022-02-03 10:14:47 +01:00
#endif
2022-08-15 20:28:16 +02:00
return returnvalue::FAILED;
2022-02-03 10:14:47 +01:00
}
char hexstring[SIZE_HEX_STRING] = "";
int items = fscanf(fp, "%s", hexstring);
if (items != 1) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "UioMapper::getMapSize: Failed with error code " << errno
2022-02-14 14:54:20 +01:00
<< " to read size "
"string from file "
<< namestream.str() << std::endl;
2022-02-03 10:14:47 +01:00
#endif
fclose(fp);
2022-08-15 20:28:16 +02:00
return returnvalue::FAILED;
2022-02-03 10:14:47 +01:00
}
2022-02-03 11:07:51 +01:00
uint32_t sizeTmp = 0;
items = sscanf(hexstring, "%x", &sizeTmp);
2022-02-14 14:54:20 +01:00
if (size != nullptr) {
2022-02-03 11:07:51 +01:00
*size = sizeTmp;
}
2022-02-03 10:14:47 +01:00
if (items != 1) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "UioMapper::getMapSize: Failed with error code " << errno << "to convert "
2022-02-14 14:54:20 +01:00
<< "size of map" << mapNum << " to integer" << std::endl;
2022-02-03 10:14:47 +01:00
#endif
fclose(fp);
2022-08-15 20:28:16 +02:00
return returnvalue::FAILED;
2022-02-03 10:14:47 +01:00
}
fclose(fp);
2022-08-15 20:28:16 +02:00
return returnvalue::OK;
2022-02-03 10:14:47 +01:00
}