blob: 32d055e4340ec4303c695f3cbb46698ac5f93847 [file] [log] [blame]
Brandon Wyman5914f652017-03-16 18:17:07 -05001#pragma once
2
3#include <sdbusplus/bus.hpp>
Brandon Wymanfef02952017-03-31 18:13:21 -05004#include <unistd.h>
Brandon Wymance822442017-04-10 17:48:02 -05005#include <fcntl.h>
Dinesh Chinari618027a2017-06-26 23:26:50 -05006#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>
Brandon Wymance822442017-04-10 17:48:02 -050010
Brandon Wyman5914f652017-03-16 18:17:07 -050011
Dinesh Chinari618027a2017-06-26 23:26:50 -050012using namespace phosphor::logging;
13using InternalFailure = sdbusplus::xyz::openbmc_project::Common::
14 Error::InternalFailure;
15
Brandon Wyman5914f652017-03-16 18:17:07 -050016namespace phosphor
17{
18namespace fan
19{
Matt Spinler5cfdf942017-04-10 14:25:47 -050020namespace util
Brandon Wyman5914f652017-03-16 18:17:07 -050021{
22
Brandon Wymanfef02952017-03-31 18:13:21 -050023class 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
Brandon Wymance822442017-04-10 17:48:02 -050044 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 {
Dinesh Chinari618027a2017-06-26 23:26:50 -050054 log<level::ERR>(
55 "Failed to open file device: ",
56 entry("PATHNAME=%s", pathname.c_str()));
57 elog<InternalFailure>();
Brandon Wymance822442017-04-10 17:48:02 -050058 }
59 }
60
Brandon Wymanfef02952017-03-31 18:13:21 -050061 bool is_open()
62 {
63 return fd != -1;
64 }
65
66 private:
67 int fd = -1;
68
69};
70
Brandon Wyman5914f652017-03-16 18:17:07 -050071/**
72 * @brief Get the inventory service name from the mapper object
73 *
74 * @return The inventory manager service name
75 */
76std::string getInvService(sdbusplus::bus::bus& bus);
77
Matt Spinler5cfdf942017-04-10 14:25:47 -050078
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 */
89std::string getService(const std::string& path,
90 const std::string& interface,
91 sdbusplus::bus::bus& bus);
92
Brandon Wyman5914f652017-03-16 18:17:07 -050093}
94}
95}