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

37 lines
1.1 KiB
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) {
2021-07-13 19:19:25 +02:00
#if FSFW_VERBOSE_LEVEL >= 1
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::warning << diagnosticPrefix << ": Opening device failed with error code " << errno << ": "
<< strerror(errno) << std::endl;
2021-07-13 19:19:25 +02:00
#else
2022-02-02 10:29:30 +01:00
sif::printWarning("%s: Opening device failed with error code %d: %s\n", diagnosticPrefix, errno,
strerror(errno));
2021-07-13 19:19:25 +02:00
#endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */
#endif /* FSFW_VERBOSE_LEVEL >= 1 */
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; }