Vishwanatha Subbanna | 4902a10 | 2017-04-04 14:05:09 +0530 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <unistd.h> |
| 4 | namespace phosphor |
| 5 | { |
| 6 | namespace gpio |
| 7 | { |
| 8 | /** @class FileDescriptor |
| 9 | * @brief Responsible for handling file descriptor |
| 10 | */ |
| 11 | class FileDescriptor |
| 12 | { |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame^] | 13 | private: |
| 14 | /** @brief File descriptor for the gpio input device */ |
| 15 | int fd = -1; |
Vishwanatha Subbanna | 4902a10 | 2017-04-04 14:05:09 +0530 | [diff] [blame] | 16 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame^] | 17 | public: |
| 18 | FileDescriptor() = default; |
| 19 | FileDescriptor(const FileDescriptor&) = delete; |
| 20 | FileDescriptor& operator=(const FileDescriptor&) = delete; |
| 21 | FileDescriptor(FileDescriptor&&) = delete; |
| 22 | FileDescriptor& operator=(FileDescriptor&&) = delete; |
Vishwanatha Subbanna | 4902a10 | 2017-04-04 14:05:09 +0530 | [diff] [blame] | 23 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame^] | 24 | /** @brief Saves File descriptor and uses it to do file operation |
| 25 | * |
| 26 | * @param[in] fd - File descriptor |
| 27 | */ |
| 28 | FileDescriptor(int fd) : fd(fd) |
| 29 | { |
| 30 | // Nothing |
| 31 | } |
| 32 | |
| 33 | ~FileDescriptor() |
| 34 | { |
| 35 | if (fd >= 0) |
Vishwanatha Subbanna | 4902a10 | 2017-04-04 14:05:09 +0530 | [diff] [blame] | 36 | { |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame^] | 37 | close(fd); |
Vishwanatha Subbanna | 4902a10 | 2017-04-04 14:05:09 +0530 | [diff] [blame] | 38 | } |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame^] | 39 | } |
Vishwanatha Subbanna | 4902a10 | 2017-04-04 14:05:09 +0530 | [diff] [blame] | 40 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame^] | 41 | int operator()() |
| 42 | { |
| 43 | return fd; |
| 44 | } |
Vishwanatha Subbanna | 4902a10 | 2017-04-04 14:05:09 +0530 | [diff] [blame] | 45 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame^] | 46 | operator bool() const |
| 47 | { |
| 48 | return fd != -1; |
| 49 | } |
Matt Spinler | 36df197 | 2017-05-25 10:23:05 -0500 | [diff] [blame] | 50 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame^] | 51 | void set(int descriptor) |
| 52 | { |
| 53 | fd = descriptor; |
| 54 | } |
Vishwanatha Subbanna | 4902a10 | 2017-04-04 14:05:09 +0530 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | } // namespace gpio |
| 58 | } // namespace phosphor |