78 lines
2.3 KiB
C++
78 lines
2.3 KiB
C++
#ifndef LINUX_UTILITY_UTILITY_H_
|
|
#define LINUX_UTILITY_UTILITY_H_
|
|
|
|
#include <cerrno>
|
|
#include <cstring>
|
|
#include <fsfw/serviceinterface/ServiceInterface.h>
|
|
|
|
#include <fcntl.h>
|
|
#include <unistd.h>
|
|
|
|
namespace utility {
|
|
|
|
void handleIoctlError(const char* const customPrintout) {
|
|
#if FSFW_VERBOSE_LEVEL >= 1
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
|
if(customPrintout != nullptr) {
|
|
sif::warning << customPrintout << std::endl;
|
|
}
|
|
sif::warning << "handleIoctlError: Error code " << errno << ", "<< strerror(errno) <<
|
|
std::endl;
|
|
#else
|
|
if(customPrintout != nullptr) {
|
|
sif::printWarning("%s\n", customPrintout);
|
|
}
|
|
sif::printWarning("handleIoctlError: Error code %d, %s\n", errno, strerror(errno));
|
|
#endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */
|
|
#endif /* FSFW_VERBOSE_LEVEL >= 1 */
|
|
|
|
}
|
|
|
|
class UnixFileHelper {
|
|
public:
|
|
static constexpr int READ_WRITE_FLAG = O_RDWR;
|
|
static constexpr int READ_ONLY_FLAG = O_RDONLY;
|
|
static constexpr int NON_BLOCKING_IO_FLAG = O_NONBLOCK;
|
|
|
|
static constexpr ReturnValue_t OPEN_FILE_FAILED = 1;
|
|
|
|
UnixFileHelper(std::string device, int* fileDescriptor, int flags,
|
|
std::string diagnosticPrefix = ""):
|
|
fileDescriptor(fileDescriptor) {
|
|
if(fileDescriptor == nullptr) {
|
|
return;
|
|
}
|
|
*fileDescriptor = open(device.c_str(), flags);
|
|
if (*fileDescriptor < 0) {
|
|
#if FSFW_VERBOSE_LEVEL >= 1
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
|
sif::warning << diagnosticPrefix <<"Opening device failed with error code " << errno <<
|
|
"." << std::endl;
|
|
sif::warning << "Error description: " << strerror(errno) << std::endl;
|
|
#else
|
|
sif::printError("%sOpening device failed with error code %d.\n", diagnosticPrefix);
|
|
sif::printWarning("Error description: %s\n", strerror(errno));
|
|
#endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */
|
|
#endif /* FSFW_VERBOSE_LEVEL >= 1 */
|
|
openStatus = OPEN_FILE_FAILED;
|
|
}
|
|
}
|
|
|
|
virtual~ UnixFileHelper() {
|
|
if(fileDescriptor != nullptr) {
|
|
close(*fileDescriptor);
|
|
}
|
|
}
|
|
|
|
ReturnValue_t getOpenResult() const {
|
|
return openStatus;
|
|
}
|
|
private:
|
|
int* fileDescriptor = nullptr;
|
|
ReturnValue_t openStatus = HasReturnvaluesIF::RETURN_OK;
|
|
};
|
|
|
|
}
|
|
|
|
#endif /* LINUX_UTILITY_UTILITY_H_ */
|