fsfw/src/fsfw_hal/linux/UnixFileGuard.h

39 lines
1009 B
C++

#ifndef LINUX_UTILITY_UNIXFILEGUARD_H_
#define LINUX_UTILITY_UNIXFILEGUARD_H_
#include <fcntl.h>
#include <fsfw/returnvalues/returnvalue.h>
#include <unistd.h>
#include <string>
class UnixFileGuard {
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;
/**
* 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,
std::string diagnosticPrefix = "");
virtual ~UnixFileGuard();
ReturnValue_t getOpenResult() const;
private:
int& fdRef;
ReturnValue_t openStatus = returnvalue::OK;
};
#endif /* LINUX_UTILITY_UNIXFILEGUARD_H_ */