blob: 880545a86c0d8db4fb98bd2a257b24d31fceb074 [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>
6
Brandon Wyman5914f652017-03-16 18:17:07 -05007
8namespace phosphor
9{
10namespace fan
11{
Matt Spinler5cfdf942017-04-10 14:25:47 -050012namespace util
Brandon Wyman5914f652017-03-16 18:17:07 -050013{
14
Brandon Wymanfef02952017-03-31 18:13:21 -050015class FileDescriptor
16{
17 public:
18 FileDescriptor() = delete;
19 FileDescriptor(const FileDescriptor&) = delete;
20 FileDescriptor(FileDescriptor&&) = default;
21 FileDescriptor& operator=(const FileDescriptor&) = delete;
22 FileDescriptor& operator=(FileDescriptor&&) = default;
23
24 FileDescriptor(int fd) : fd(fd)
25 {
26 }
27
28 ~FileDescriptor()
29 {
30 if (fd != -1)
31 {
32 close(fd);
33 }
34 }
35
Brandon Wymance822442017-04-10 17:48:02 -050036 int operator()()
37 {
38 return fd;
39 }
40
41 void open(const std::string& pathname, int flags)
42 {
43 fd = ::open(pathname.c_str(), flags);
44 if (-1 == fd)
45 {
46 throw std::runtime_error(
47 "Failed to open file device: " + pathname);
48 }
49 }
50
Brandon Wymanfef02952017-03-31 18:13:21 -050051 bool is_open()
52 {
53 return fd != -1;
54 }
55
56 private:
57 int fd = -1;
58
59};
60
Brandon Wyman5914f652017-03-16 18:17:07 -050061/**
62 * @brief Get the inventory service name from the mapper object
63 *
64 * @return The inventory manager service name
65 */
66std::string getInvService(sdbusplus::bus::bus& bus);
67
Matt Spinler5cfdf942017-04-10 14:25:47 -050068
69/**
70 * @brief Get the service name from the mapper for the
71 * interface and path passed in.
72 *
73 * @param[in] path - the dbus path name
74 * @param[in] interface - the dbus interface name
75 * @param[in] bus - the dbus object
76 *
77 * @return The service name
78 */
79std::string getService(const std::string& path,
80 const std::string& interface,
81 sdbusplus::bus::bus& bus);
82
Brandon Wyman5914f652017-03-16 18:17:07 -050083}
84}
85}