fsfw/src/fsfw_hal/linux/UnixFileGuard.h

39 lines
1015 B
C
Raw Normal View History

2021-07-13 19:19:25 +02:00
#ifndef LINUX_UTILITY_UNIXFILEGUARD_H_
#define LINUX_UTILITY_UNIXFILEGUARD_H_
#include <fcntl.h>
2022-08-16 12:48:22 +02:00
#include <fsfw/returnvalues/returnvalue.h>
2021-07-13 19:19:25 +02:00
#include <unistd.h>
2022-02-02 10:29:30 +01:00
#include <string>
2021-07-13 19:19:25 +02:00
class UnixFileGuard {
2022-02-02 10:29:30 +01:00
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;
2021-07-13 19:19:25 +02:00
2022-02-02 10:29:30 +01:00
static constexpr ReturnValue_t OPEN_FILE_FAILED = 1;
2021-07-13 19:19:25 +02:00
2023-02-18 13:45:49 +01:00
/**
* Open a device and assign the given file descriptor variable
* @param device [in] Device name.
* @param fileDescriptor [in/out] Will be assigned by file guard and re-used to
* close the guard.
* @param flags
* @param diagnosticPrefix
*/
UnixFileGuard(const std::string& device, int& fileDescriptor, int flags,
2022-02-02 10:29:30 +01:00
std::string diagnosticPrefix = "");
2021-07-13 19:19:25 +02:00
2022-02-02 10:29:30 +01:00
virtual ~UnixFileGuard();
2021-07-13 19:19:25 +02:00
2022-02-02 10:29:30 +01:00
ReturnValue_t getOpenResult() const;
2021-07-13 19:19:25 +02:00
2022-02-02 10:29:30 +01:00
private:
2023-02-18 13:46:51 +01:00
int cachedFd = 0;
2022-08-15 20:28:16 +02:00
ReturnValue_t openStatus = returnvalue::OK;
2022-02-02 10:29:30 +01:00
};
2021-07-13 19:19:25 +02:00
#endif /* LINUX_UTILITY_UNIXFILEGUARD_H_ */