blob: 3ee65d4298ffeda6be266fa0ba8bc9cb1a25630d [file] [log] [blame]
Christopher Meis75ff1672025-09-23 11:40:06 +02001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright OpenBMC Authors
3
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +05304#pragma once
5
6#include <unistd.h>
7namespace phosphor
8{
9namespace gpio
10{
11/** @class FileDescriptor
12 * @brief Responsible for handling file descriptor
13 */
14class FileDescriptor
15{
Patrick Venturedace6802018-11-01 16:52:10 -070016 private:
17 /** @brief File descriptor for the gpio input device */
18 int fd = -1;
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +053019
Patrick Venturedace6802018-11-01 16:52:10 -070020 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 Subbanna4902a102017-04-04 14:05:09 +053026
Patrick Venturedace6802018-11-01 16:52:10 -070027 /** @brief Saves File descriptor and uses it to do file operation
28 *
29 * @param[in] fd - File descriptor
30 */
George Liu9b4c6cf2023-08-03 11:01:27 +080031 explicit FileDescriptor(int fd) : fd(fd)
Patrick Venturedace6802018-11-01 16:52:10 -070032 {
33 // Nothing
34 }
35
36 ~FileDescriptor()
37 {
38 if (fd >= 0)
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +053039 {
Patrick Venturedace6802018-11-01 16:52:10 -070040 close(fd);
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +053041 }
Patrick Venturedace6802018-11-01 16:52:10 -070042 }
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +053043
Patrick Venturedace6802018-11-01 16:52:10 -070044 int operator()()
45 {
46 return fd;
47 }
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +053048
Patrick Venturedace6802018-11-01 16:52:10 -070049 operator bool() const
50 {
51 return fd != -1;
52 }
Matt Spinler36df1972017-05-25 10:23:05 -050053
Patrick Venturedace6802018-11-01 16:52:10 -070054 void set(int descriptor)
55 {
56 fd = descriptor;
57 }
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +053058};
59
60} // namespace gpio
61} // namespace phosphor