1 #pragma once
2 
3 #include <sdbusplus/bus.hpp>
4 #include <unistd.h>
5 #include <fcntl.h>
6 #include <phosphor-logging/log.hpp>
7 #include <phosphor-logging/elog.hpp>
8 #include <phosphor-logging/elog-errors.hpp>
9 #include <xyz/openbmc_project/Common/error.hpp>
10 
11 
12 using namespace phosphor::logging;
13 using InternalFailure = sdbusplus::xyz::openbmc_project::Common::
14                             Error::InternalFailure;
15 
16 namespace phosphor
17 {
18 namespace fan
19 {
20 namespace util
21 {
22 
23 class FileDescriptor
24 {
25     public:
26         FileDescriptor() = delete;
27         FileDescriptor(const FileDescriptor&) = delete;
28         FileDescriptor(FileDescriptor&&) = default;
29         FileDescriptor& operator=(const FileDescriptor&) = delete;
30         FileDescriptor& operator=(FileDescriptor&&) = default;
31 
32         FileDescriptor(int fd) : fd(fd)
33         {
34         }
35 
36         ~FileDescriptor()
37         {
38             if (fd != -1)
39             {
40                 close(fd);
41             }
42         }
43 
44         int operator()()
45         {
46             return fd;
47         }
48 
49         void open(const std::string& pathname, int flags)
50         {
51             fd = ::open(pathname.c_str(), flags);
52             if (-1 == fd)
53             {
54                 log<level::ERR>(
55                      "Failed to open file device: ",
56                      entry("PATHNAME=%s", pathname.c_str()));
57                 elog<InternalFailure>();
58             }
59         }
60 
61         bool is_open()
62         {
63             return fd != -1;
64         }
65 
66     private:
67         int fd = -1;
68 
69 };
70 
71 /**
72  * @brief Get the inventory service name from the mapper object
73  *
74  * @return The inventory manager service name
75  */
76 std::string getInvService(sdbusplus::bus::bus& bus);
77 
78 
79 /**
80  * @brief Get the service name from the mapper for the
81  *        interface and path passed in.
82  *
83  * @param[in] path - the dbus path name
84  * @param[in] interface - the dbus interface name
85  * @param[in] bus - the dbus object
86  *
87  * @return The service name
88  */
89 std::string getService(const std::string& path,
90                        const std::string& interface,
91                        sdbusplus::bus::bus& bus);
92 
93 }
94 }
95 }
96