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