fsfw/hal/src/fsfw_hal/linux/UnixFileGuard.cpp

30 lines
795 B
C++
Raw Normal View History

2021-08-02 20:58:56 +02:00
#include "fsfw_hal/linux/UnixFileGuard.h"
2021-07-13 19:19:25 +02:00
2021-08-18 11:27:39 +02:00
#include <cerrno>
#include <cstring>
2022-02-02 10:29:30 +01:00
#include "fsfw/FSFW.h"
#include "fsfw/serviceinterface.h"
2021-07-13 19:19:25 +02:00
UnixFileGuard::UnixFileGuard(std::string device, int* fileDescriptor, int flags,
2022-02-02 10:29:30 +01:00
std::string diagnosticPrefix)
: fileDescriptor(fileDescriptor) {
if (fileDescriptor == nullptr) {
return;
}
*fileDescriptor = open(device.c_str(), flags);
if (*fileDescriptor < 0) {
2022-05-09 00:09:13 +02:00
FSFW_LOGW("{} | Opening device failed with error code {} | {}\n", diagnosticPrefix, errno,
strerror(errno));
2022-02-02 10:29:30 +01:00
openStatus = OPEN_FILE_FAILED;
}
2021-07-13 19:19:25 +02:00
}
UnixFileGuard::~UnixFileGuard() {
2022-02-02 10:29:30 +01:00
if (fileDescriptor != nullptr) {
close(*fileDescriptor);
}
2021-07-13 19:19:25 +02:00
}
2022-02-02 10:29:30 +01:00
ReturnValue_t UnixFileGuard::getOpenResult() const { return openStatus; }